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