CommandInterpreter.h revision 65124eac53b7a214906e8931be314e429bbbeb30
1//===-- CommandInterpreter.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_CommandInterpreter_h_
11#define liblldb_CommandInterpreter_h_
12
13// C Includes
14// C++ Includes
15// Other libraries and framework includes
16// Project includes
17#include "lldb/lldb-private.h"
18#include "lldb/Core/Broadcaster.h"
19#include "lldb/Core/Debugger.h"
20#include "lldb/Core/Log.h"
21#include "lldb/Interpreter/CommandObject.h"
22#include "lldb/Interpreter/ScriptInterpreter.h"
23#include "lldb/Interpreter/StateVariable.h"
24#include "lldb/Core/Event.h"
25#include "lldb/Interpreter/Args.h"
26#include "lldb/Core/StringList.h"
27
28namespace lldb_private {
29
30class CommandInterpreter : public Broadcaster
31{
32public:
33    typedef std::map<std::string, lldb::StateVariableSP> VariableMap;
34    typedef std::map<std::string, OptionArgVectorSP> OptionArgMap;
35
36    enum
37    {
38        eBroadcastBitThreadShouldExit       = (1 << 0),
39        eBroadcastBitResetPrompt            = (1 << 1),
40        eBroadcastBitQuitCommandReceived    = (1 << 2)   // User entered quit
41    };
42
43    void
44    SourceInitFile (bool in_cwd, CommandReturnObject &result);
45
46    CommandInterpreter (Debugger &debugger,
47                        lldb::ScriptLanguage script_language,
48                        bool synchronous_execution);
49
50    virtual
51    ~CommandInterpreter ();
52
53    lldb::CommandObjectSP
54    GetCommandSPExact (const char *cmd, bool include_aliases);
55
56    CommandObject *
57    GetCommandObjectExact (const char *cmd_cstr, bool include_aliases);
58
59    CommandObject *
60    GetCommandObject (const char *cmd, StringList *matches = NULL);
61
62    StateVariable *
63    GetStateVariable(const char *name);
64
65    bool
66    CommandExists (const char *cmd);
67
68    bool
69    AliasExists (const char *cmd);
70
71    bool
72    UserCommandExists (const char *cmd);
73
74    void
75    AddAlias (const char *alias_name, lldb::CommandObjectSP& command_obj_sp);
76
77    bool
78    RemoveAlias (const char *alias_name);
79
80    bool
81    RemoveUser (const char *alias_name);
82
83    OptionArgVectorSP
84    GetAliasOptions (const char *alias_name);
85
86    void
87    RemoveAliasOptions (const char *alias_name);
88
89    void
90    AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp);
91
92    bool
93    HandleCommand (const char *command_line,
94                   bool add_to_history,
95                   CommandReturnObject &result,
96                   ExecutionContext *override_context = NULL);
97
98    // This handles command line completion.  You are given a pointer to the command string buffer, to the current cursor,
99    // and to the end of the string (in case it is not NULL terminated).
100    // You also passed in an Args object to fill with the returns.
101    // The first element of the array will be filled with the string that you would need to insert at
102    // the cursor point to complete the cursor point to the longest common matching prefix.
103    // If you want to limit the number of elements returned, set max_return_elements to the number of elements
104    // you want returned.  Otherwise set max_return_elements to -1.
105    // If you want to start some way into the match list, then set match_start_point to the desired start
106    // point.
107    // Returns the total number of completions, or -1 if the completion character should be inserted, or
108    // INT_MAX if the number of matches is > max_return_elements, but it is expensive to compute.
109    //
110    // FIXME: Only max_return_elements == -1 is supported at present.
111
112    int
113    HandleCompletion (const char *current_line,
114                                      const char *cursor,
115                                      const char *last_char,
116                                      int match_start_point,
117                                      int max_return_elements,
118                                      StringList &matches);
119
120    // This version just returns matches, and doesn't compute the substring.  It is here so the
121    // Help command can call it for the first argument.
122    // word_complete tells whether a the completions are considered a "complete" response (so the
123    // completer should complete the quote & put a space after the word.
124
125    int
126    HandleCompletionMatches (Args &input,
127                      int &cursor_index,
128                      int &cursor_char_position,
129                      int match_start_point,
130                      int max_return_elements,
131                      bool &word_complete,
132                      StringList &matches);
133
134
135    int
136    GetCommandNamesMatchingPartialString (const char *cmd_cstr, bool include_aliases, StringList &matches);
137
138    void
139    GetHelp (CommandReturnObject &result);
140
141    void
142    GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string);
143
144    void
145    OutputFormattedHelpText (Stream &stream,
146                             const char *command_word,
147                             const char *separator,
148                             const char *help_text,
149                             uint32_t max_word_len);
150
151    void
152    ShowVariableValues (CommandReturnObject &result);
153
154    void
155    ShowVariableHelp (CommandReturnObject &result);
156
157    Debugger &
158    GetDebugger ()
159    {
160        return m_debugger;
161    }
162
163    const Args *
164    GetProgramArguments ();
165
166    const Args *
167    GetEnvironmentVariables ();
168
169    const char *
170    ProcessEmbeddedScriptCommands (const char *arg);
171
172    const char *
173    GetPrompt ();
174
175    void
176    SetPrompt (const char *);
177
178    void
179    LoadCommandDictionary ();
180
181    void
182    Initialize ();
183
184    void
185    InitializeVariables ();
186
187    void
188    CrossRegisterCommand (const char * dest_cmd, const char * object_type);
189
190    void
191    SetScriptLanguage (lldb::ScriptLanguage lang);
192
193
194    bool
195    HasCommands ();
196
197    bool
198    HasAliases ();
199
200    bool
201    HasUserCommands ();
202
203    bool
204    HasAliasOptions ();
205
206    bool
207    HasInterpreterVariables ();
208
209    void
210    BuildAliasCommandArgs (CommandObject *alias_cmd_obj, const char *alias_name, Args &cmd_args,
211                           CommandReturnObject &result);
212
213    int
214    GetOptionArgumentPosition (const char *in_string);
215
216    ScriptInterpreter *
217    GetScriptInterpreter ();
218
219    bool
220    GetSynchronous ();
221
222#ifndef SWIG
223    void
224    AddLogChannel (const char *name, const Log::Callbacks &log_callbacks);
225
226    bool
227    GetLogChannelCallbacks (const char *channel, Log::Callbacks &log_callbacks);
228
229    bool
230    RemoveLogChannel (const char *name);
231#endif
232
233    size_t
234    FindLongestCommandWord (CommandObject::CommandMap &dict);
235
236    void
237    FindCommandsForApropos (const char *word, StringList &commands_found, StringList &commands_help);
238
239    void
240    AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word,
241                           StringList &commands_found, StringList &commands_help);
242
243protected:
244    friend class Debugger;
245
246    void
247    SetSynchronous (bool value);
248
249    lldb::CommandObjectSP
250    GetCommandSP (const char *cmd, bool include_aliases = true, bool exact = true, StringList *matches = NULL);
251
252private:
253
254    Debugger &m_debugger;   // The debugger session that this interpreter is associated with
255    lldb::ScriptLanguage m_script_language;
256    bool m_synchronous_execution;
257    CommandObject::CommandMap m_command_dict; // Stores basic built-in commands (they cannot be deleted, removed or overwritten).
258    CommandObject::CommandMap m_alias_dict;   // Stores user aliases/abbreviations for commands
259    CommandObject::CommandMap m_user_dict;    // Stores user-defined commands
260    VariableMap m_variables;
261    OptionArgMap m_alias_options; // Stores any options (with or without arguments) that go with any alias.
262    std::vector<std::string> m_command_history;
263    std::string m_repeat_command;  // Stores the command that will be executed for an empty command string.
264};
265
266
267} // namespace lldb_private
268
269#endif  // liblldb_CommandInterpreter_h_
270