CommandObject.h revision 47f07856222a1d2032ecfb346f04836b3660d449
1//===-- CommandObject.h -----------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_CommandObject_h_
11#define liblldb_CommandObject_h_
12
13#include <map>
14#include <set>
15#include <string>
16#include <vector>
17
18#include "lldb/lldb-private.h"
19#include "lldb/Interpreter/Args.h"
20#include "lldb/Interpreter/CommandCompletions.h"
21#include "lldb/Core/StringList.h"
22#include "lldb/Core/Flags.h"
23
24namespace lldb_private {
25
26class CommandObject
27{
28public:
29
30    typedef const char *(ArgumentHelpCallbackFunction) ();
31
32    struct ArgumentTableEntry  // Entries in the main argument information table
33    {
34        lldb::CommandArgumentType  arg_type;
35        const char *arg_name;
36        CommandCompletions::CommonCompletionTypes completion_type;
37        ArgumentHelpCallbackFunction  *help_function;
38        const char *help_text;
39    };
40
41    struct CommandArgumentData  // Used to build individual command argument lists
42    {
43        lldb::CommandArgumentType arg_type;
44        lldb::ArgumentRepetitionType arg_repetition;
45    };
46
47    typedef std::vector<CommandArgumentData> CommandArgumentEntry; // Used to build individual command argument lists
48
49    static ArgumentTableEntry g_arguments_data[lldb::eArgTypeLastArg];   // Main argument information table
50
51    typedef std::map<std::string, lldb::CommandObjectSP> CommandMap;
52
53    CommandObject (CommandInterpreter &interpreter,
54                   const char *name,
55                   const char *help = NULL,
56                   const char *syntax = NULL,
57                   uint32_t flags = 0);
58
59    virtual
60    ~CommandObject ();
61
62
63    static const char *
64    GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type);
65
66    static const char *
67    GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type);
68
69    CommandInterpreter &
70    GetCommandInterpreter ()
71    {
72        return m_interpreter;
73    }
74
75    const char *
76    GetHelp ();
77
78    const char *
79    GetHelpLong ();
80
81    const char *
82    GetSyntax ();
83
84    const char *
85    Translate ();
86
87    const char *
88    GetCommandName ();
89
90    void
91    SetHelp (const char * str);
92
93    void
94    SetHelpLong (const char * str);
95
96    void
97    SetSyntax (const char *str);
98
99    virtual void
100    AddObject (const char *obj_name) {}
101
102    virtual bool
103    IsCrossRefObject () { return false; }
104
105    bool
106    IsAlias () { return m_is_alias; }
107
108    void
109    SetIsAlias (bool value) { m_is_alias = value; }
110
111    virtual bool
112    IsMultiwordObject () { return false; }
113
114    virtual bool
115    WantsRawCommandString() { return false; }
116
117    virtual Options *
118    GetOptions ();
119
120    static const ArgumentTableEntry*
121    GetArgumentTable ();
122
123    static lldb::CommandArgumentType
124    LookupArgumentName (const char *arg_name);
125
126    static ArgumentTableEntry *
127    FindArgumentDataByType (lldb::CommandArgumentType arg_type);
128
129    int
130    GetNumArgumentEntries ();
131
132    CommandArgumentEntry *
133    GetArgumentEntryAtIndex (int idx);
134
135    static void
136    GetArgumentHelp (Stream &str, lldb::CommandArgumentType arg_type, CommandInterpreter &interpreter);
137
138    static const char *
139    GetArgumentName (lldb::CommandArgumentType arg_type);
140
141    void
142    GetFormattedCommandArguments (Stream &str);
143
144    bool
145    IsPairType (lldb::ArgumentRepetitionType arg_repeat_type);
146
147    enum
148    {
149        eFlagProcessMustBeLaunched = (1 << 0),
150        eFlagProcessMustBePaused = (1 << 1)
151    };
152
153    // Do not override this
154    bool
155    ExecuteCommandString (const char *command,
156                          CommandReturnObject &result);
157
158    bool
159    ParseOptions (Args& args,
160                  CommandReturnObject &result);
161
162    bool
163    ExecuteWithOptions (Args& command,
164                        CommandReturnObject &result);
165
166    virtual bool
167    ExecuteRawCommandString (const char *command,
168                             CommandReturnObject &result)
169    {
170        return false;
171    }
172
173
174    virtual bool
175    Execute (Args& command,
176             CommandReturnObject &result) = 0;
177
178    void
179    SetCommandName (const char *name);
180
181    // This function really deals with CommandObjectLists, but we didn't make a
182    // CommandObjectList class, so I'm sticking it here.  But we really should have
183    // such a class.  Anyway, it looks up the commands in the map that match the partial
184    // string cmd_str, inserts the matches into matches, and returns the number added.
185
186    static int
187    AddNamesMatchingPartialString (CommandMap &in_map, const char *cmd_str, StringList &matches);
188
189    //------------------------------------------------------------------
190    /// The input array contains a parsed version of the line.  The insertion
191    /// point is given by cursor_index (the index in input of the word containing
192    /// the cursor) and cursor_char_position (the position of the cursor in that word.)
193    /// This default version handles calling option argument completions and then calls
194    /// HandleArgumentCompletion if the cursor is on an argument, not an option.
195    /// Don't override this method, override HandleArgumentCompletion instead unless
196    /// you have special reasons.
197    ///
198    /// @param[in] interpreter
199    ///    The command interpreter doing the completion.
200    ///
201    /// @param[in] input
202    ///    The command line parsed into words
203    ///
204    /// @param[in] cursor_index
205    ///     The index in \ainput of the word in which the cursor lies.
206    ///
207    /// @param[in] cursor_char_pos
208    ///     The character position of the cursor in its argument word.
209    ///
210    /// @param[in] match_start_point
211    /// @param[in] match_return_elements
212    ///     FIXME: Not yet implemented...  If there is a match that is expensive to compute, these are
213    ///     here to allow you to compute the completions in batches.  Start the completion from \amatch_start_point,
214    ///     and return \amatch_return_elements elements.
215    ///
216    /// @param[out] word_complete
217    ///     \btrue if this is a complete option value (a space will be inserted after the
218    ///     completion.)  \bfalse otherwise.
219    ///
220    /// @param[out] matches
221    ///     The array of matches returned.
222    ///
223    /// FIXME: This is the wrong return value, since we also need to make a distinction between
224    /// total number of matches, and the window the user wants returned.
225    ///
226    /// @return
227    ///     \btrue if we were in an option, \bfalse otherwise.
228    //------------------------------------------------------------------
229    virtual int
230    HandleCompletion (Args &input,
231                      int &cursor_index,
232                      int &cursor_char_position,
233                      int match_start_point,
234                      int max_return_elements,
235                      bool &word_complete,
236                      StringList &matches);
237
238    //------------------------------------------------------------------
239    /// The input array contains a parsed version of the line.  The insertion
240    /// point is given by cursor_index (the index in input of the word containing
241    /// the cursor) and cursor_char_position (the position of the cursor in that word.)
242    /// We've constructed the map of options and their arguments as well if that is
243    /// helpful for the completion.
244    ///
245    /// @param[in] interpreter
246    ///    The command interpreter doing the completion.
247    ///
248    /// @param[in] input
249    ///    The command line parsed into words
250    ///
251    /// @param[in] cursor_index
252    ///     The index in \ainput of the word in which the cursor lies.
253    ///
254    /// @param[in] cursor_char_pos
255    ///     The character position of the cursor in its argument word.
256    ///
257    /// @param[in] opt_element_vector
258    ///     The results of the options parse of \a input.
259    ///
260    /// @param[in] match_start_point
261    /// @param[in] match_return_elements
262    ///     See CommandObject::HandleCompletions for a description of how these work.
263    ///
264    /// @param[out] word_complete
265    ///     \btrue if this is a complete option value (a space will be inserted after the
266    ///     completion.)  \bfalse otherwise.
267    ///
268    /// @param[out] matches
269    ///     The array of matches returned.
270    ///
271    /// FIXME: This is the wrong return value, since we also need to make a distinction between
272    /// total number of matches, and the window the user wants returned.
273    ///
274    /// @return
275    ///     \btrue if we were in an option, \bfalse otherwise.
276    //------------------------------------------------------------------
277
278    virtual int
279    HandleArgumentCompletion (Args &input,
280                              int &cursor_index,
281                              int &cursor_char_position,
282                              OptionElementVector &opt_element_vector,
283                              int match_start_point,
284                              int max_return_elements,
285                              bool &word_complete,
286                              StringList &matches)
287    {
288        return 0;
289    }
290
291    bool
292    HelpTextContainsWord (const char *search_word);
293
294    //------------------------------------------------------------------
295    /// The flags accessor.
296    ///
297    /// @return
298    ///     A reference to the Flags member variable.
299    //------------------------------------------------------------------
300    Flags&
301    GetFlags();
302
303    //------------------------------------------------------------------
304    /// The flags const accessor.
305    ///
306    /// @return
307    ///     A const reference to the Flags member variable.
308    //------------------------------------------------------------------
309    const Flags&
310    GetFlags() const;
311
312    //------------------------------------------------------------------
313    /// Get the command that appropriate for a "repeat" of the current command.
314    ///
315    /// @param[in] current_command_line
316    ///    The complete current command line.
317    ///
318    /// @return
319    ///     NULL if there is no special repeat command - it will use the current command line.
320    ///     Otherwise a pointer to the command to be repeated.
321    ///     If the returned string is the empty string, the command won't be repeated.
322    //------------------------------------------------------------------
323    virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
324    {
325        return NULL;
326    }
327
328protected:
329    CommandInterpreter &m_interpreter;
330    std::string m_cmd_name;
331    std::string m_cmd_help_short;
332    std::string m_cmd_help_long;
333    std::string m_cmd_syntax;
334    bool m_is_alias;
335    Flags       m_flags;
336    std::vector<CommandArgumentEntry> m_arguments;
337};
338
339} // namespace lldb_private
340
341
342#endif  // liblldb_CommandObject_h_
343