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