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