CommandObject.h revision 767af88aa617288e584afcfed055f7755e408542
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/Core/StringList.h"
21#include "lldb/Core/Flags.h"
22
23namespace lldb_private {
24
25class CommandObject
26{
27public:
28    typedef std::map<std::string, lldb::CommandObjectSP> CommandMap;
29
30
31    CommandObject (const char *name,
32                   const char *help = NULL,
33                   const char *syntax = NULL,
34                   uint32_t flags = 0);
35
36    virtual
37    ~CommandObject ();
38
39    const char *
40    GetHelp ();
41
42    const char *
43    GetHelpLong ();
44
45    const char *
46    GetSyntax ();
47
48    const char *
49    Translate ();
50
51    const char *
52    GetCommandName ();
53
54    void
55    SetHelp (const char * str);
56
57    void
58    SetHelpLong (const char * str);
59
60    void
61    SetSyntax (const char *str);
62
63    virtual void
64    AddObject (const char *obj_name) {}
65
66    virtual bool
67    IsCrossRefObject () { return false; }
68
69    bool
70    IsAlias () { return m_is_alias; }
71
72    void
73    SetIsAlias (bool value) { m_is_alias = value; }
74
75    virtual bool
76    IsMultiwordObject () { return false; }
77
78    virtual bool
79    WantsRawCommandString() { return false; }
80
81    virtual Options *
82    GetOptions ();
83
84    enum
85    {
86        eFlagProcessMustBeLaunched = (1 << 0),
87        eFlagProcessMustBePaused = (1 << 1)
88    };
89
90    // Do not override this
91    bool
92    ExecuteCommandString (CommandInterpreter &interpreter,
93                          const char *command,
94                          CommandReturnObject &result);
95
96    bool
97    ParseOptions (CommandInterpreter &interpreter,
98                  Args& args,
99                  CommandReturnObject &result);
100
101    bool
102    ExecuteWithOptions (CommandInterpreter &interpreter,
103                        Args& command,
104                        CommandReturnObject &result);
105
106    virtual bool
107    ExecuteRawCommandString (CommandInterpreter &interpreter,
108                             const char *command,
109                             CommandReturnObject &result)
110    {
111        return false;
112    }
113
114
115    virtual bool
116    Execute (CommandInterpreter &interpreter,
117             Args& command,
118             CommandReturnObject &result) = 0;
119
120    void
121    SetCommandName (const char *name);
122
123    // This function really deals with CommandObjectLists, but we didn't make a
124    // CommandObjectList class, so I'm sticking it here.  But we really should have
125    // such a class.  Anyway, it looks up the commands in the map that match the partial
126    // string cmd_str, inserts the matches into matches, and returns the number added.
127
128    static int
129    AddNamesMatchingPartialString (CommandMap &in_map, const char *cmd_str, StringList &matches);
130
131    //------------------------------------------------------------------
132    /// The input array contains a parsed version of the line.  The insertion
133    /// point is given by cursor_index (the index in input of the word containing
134    /// the cursor) and cursor_char_position (the position of the cursor in that word.)
135    /// This default version handles calling option argument completions and then calls
136    /// HandleArgumentCompletion if the cursor is on an argument, not an option.
137    /// Don't override this method, override HandleArgumentCompletion instead unless
138    /// you have special reasons.
139    ///
140    /// @param[in] interpreter
141    ///    The command interpreter doing the completion.
142    ///
143    /// @param[in] input
144    ///    The command line parsed into words
145    ///
146    /// @param[in] cursor_index
147    ///     The index in \ainput of the word in which the cursor lies.
148    ///
149    /// @param[in] cursor_char_pos
150    ///     The character position of the cursor in its argument word.
151    ///
152    /// @param[in] match_start_point
153    /// @param[in] match_return_elements
154    ///     FIXME: Not yet implemented...  If there is a match that is expensive to compute, these are
155    ///     here to allow you to compute the completions in batches.  Start the completion from \amatch_start_point,
156    ///     and return \amatch_return_elements elements.
157    ///
158    /// @param[out] word_complete
159    ///     \btrue if this is a complete option value (a space will be inserted after the
160    ///     completion.)  \bfalse otherwise.
161    ///
162    /// @param[out] matches
163    ///     The array of matches returned.
164    ///
165    /// FIXME: This is the wrong return value, since we also need to make a distinction between
166    /// total number of matches, and the window the user wants returned.
167    ///
168    /// @return
169    ///     \btrue if we were in an option, \bfalse otherwise.
170    //------------------------------------------------------------------
171    virtual int
172    HandleCompletion (CommandInterpreter &interpreter,
173                      Args &input,
174                      int &cursor_index,
175                      int &cursor_char_position,
176                      int match_start_point,
177                      int max_return_elements,
178                      bool &word_complete,
179                      StringList &matches);
180
181    //------------------------------------------------------------------
182    /// The input array contains a parsed version of the line.  The insertion
183    /// point is given by cursor_index (the index in input of the word containing
184    /// the cursor) and cursor_char_position (the position of the cursor in that word.)
185    /// We've constructed the map of options and their arguments as well if that is
186    /// helpful for the completion.
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] opt_element_vector
201    ///     The results of the options parse of \a input.
202    ///
203    /// @param[in] match_start_point
204    /// @param[in] match_return_elements
205    ///     See CommandObject::HandleCompletions for a description of how these work.
206    ///
207    /// @param[out] word_complete
208    ///     \btrue if this is a complete option value (a space will be inserted after the
209    ///     completion.)  \bfalse otherwise.
210    ///
211    /// @param[out] matches
212    ///     The array of matches returned.
213    ///
214    /// FIXME: This is the wrong return value, since we also need to make a distinction between
215    /// total number of matches, and the window the user wants returned.
216    ///
217    /// @return
218    ///     \btrue if we were in an option, \bfalse otherwise.
219    //------------------------------------------------------------------
220
221    virtual int
222    HandleArgumentCompletion (CommandInterpreter &interpreter,
223                              Args &input,
224                              int &cursor_index,
225                              int &cursor_char_position,
226                              OptionElementVector &opt_element_vector,
227                              int match_start_point,
228                              int max_return_elements,
229                              bool &word_complete,
230                              StringList &matches)
231    {
232        return 0;
233    }
234
235    bool
236    HelpTextContainsWord (const char *search_word);
237
238    //------------------------------------------------------------------
239    /// The flags accessor.
240    ///
241    /// @return
242    ///     A reference to the Flags member variable.
243    //------------------------------------------------------------------
244    Flags&
245    GetFlags();
246
247    //------------------------------------------------------------------
248    /// The flags const accessor.
249    ///
250    /// @return
251    ///     A const reference to the Flags member variable.
252    //------------------------------------------------------------------
253    const Flags&
254    GetFlags() const;
255
256    //------------------------------------------------------------------
257    /// Get the command that appropriate for a "repeat" of the current command.
258    ///
259    /// @param[in] current_command_line
260    ///    The complete current command line.
261    ///
262    /// @return
263    ///     NULL if there is no special repeat command - it will use the current command line.
264    ///     Otherwise a pointer to the command to be repeated.
265    ///     If the returned string is the empty string, the command won't be repeated.
266    //------------------------------------------------------------------
267    virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
268    {
269        return NULL;
270    }
271
272protected:
273    std::string m_cmd_name;
274    std::string m_cmd_help_short;
275    std::string m_cmd_help_long;
276    std::string m_cmd_syntax;
277    bool m_is_alias;
278    Flags       m_flags;
279};
280
281} // namespace lldb_private
282
283
284#endif  // liblldb_CommandObject_h_
285