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