CommandInterpreter.h revision 949d5acde6684b5824a26034457410cdf3823dfe
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/Core/Event.h"
24#include "lldb/Interpreter/Args.h"
25#include "lldb/Core/StringList.h"
26
27namespace lldb_private {
28
29class CommandInterpreter : public Broadcaster
30{
31public:
32    typedef std::map<std::string, OptionArgVectorSP> OptionArgMap;
33
34    enum
35    {
36        eBroadcastBitThreadShouldExit       = (1 << 0),
37        eBroadcastBitResetPrompt            = (1 << 1),
38        eBroadcastBitQuitCommandReceived    = (1 << 2)   // User entered quit
39    };
40
41    void
42    SourceInitFile (bool in_cwd, CommandReturnObject &result);
43
44    CommandInterpreter (Debugger &debugger,
45                        lldb::ScriptLanguage script_language,
46                        bool synchronous_execution);
47
48    virtual
49    ~CommandInterpreter ();
50
51    lldb::CommandObjectSP
52    GetCommandSPExact (const char *cmd, bool include_aliases);
53
54    CommandObject *
55    GetCommandObjectExact (const char *cmd_cstr, bool include_aliases);
56
57    CommandObject *
58    GetCommandObject (const char *cmd, StringList *matches = NULL);
59
60    bool
61    CommandExists (const char *cmd);
62
63    bool
64    AliasExists (const char *cmd);
65
66    bool
67    UserCommandExists (const char *cmd);
68
69    void
70    AddAlias (const char *alias_name, lldb::CommandObjectSP& command_obj_sp);
71
72    bool
73    RemoveAlias (const char *alias_name);
74
75    bool
76    RemoveUser (const char *alias_name);
77
78    OptionArgVectorSP
79    GetAliasOptions (const char *alias_name);
80
81    void
82    RemoveAliasOptions (const char *alias_name);
83
84    void
85    AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp);
86
87    bool
88    StripFirstWord (std::string &command_string, std::string &next_word);
89
90    void
91    BuildAliasResult (const char *alias_name, std::string &raw_input_string, std::string &alias_result,
92                      CommandObject *&alias_cmd_obj, CommandReturnObject &result);
93
94    bool
95    HandleCommand (const char *command_line,
96                   bool add_to_history,
97                   CommandReturnObject &result,
98                   ExecutionContext *override_context = NULL,
99                   bool repeat_on_empty_command = true);
100
101    //------------------------------------------------------------------
102    /// Execute a list of commands in sequence.
103    ///
104    /// @param[in] commands
105    ///    The list of commands to execute.
106    /// @param[in/out] context
107    ///    The execution context in which to run the commands.  Can be NULL in which case the default
108    ///    context will be used.
109    /// @param[in] stop_on_continue
110    ///    If \b true execution will end on the first command that causes the process in the
111    ///    execution context to continue.  If \false, we won't check the execution status.
112    /// @param[in] stop_on_error
113    ///    If \b true execution will end on the first command that causes an error.
114    /// @param[in] echo_commands
115    ///    If \b true echo the command before executing it.  If \false, execute silently.
116    /// @param[in] print_results
117    ///    If \b true print the results of the command after executing it.  If \false, execute silently.
118    /// @param[out] result
119    ///    This is marked as succeeding with no output if all commands execute safely,
120    ///    and failed with some explanation if we aborted executing the commands at some point.
121    //------------------------------------------------------------------
122    void
123    HandleCommands (StringList &commands,
124                    ExecutionContext *context,
125                    bool stop_on_continue,
126                    bool stop_on_error,
127                    bool echo_commands,
128                    bool print_results,
129                    CommandReturnObject &result);
130
131    //------------------------------------------------------------------
132    /// Execute a list of commands from a file.
133    ///
134    /// @param[in] file
135    ///    The file from which to read in commands.
136    /// @param[in/out] context
137    ///    The execution context in which to run the commands.  Can be NULL in which case the default
138    ///    context will be used.
139    /// @param[in] stop_on_continue
140    ///    If \b true execution will end on the first command that causes the process in the
141    ///    execution context to continue.  If \false, we won't check the execution status.
142    /// @param[in] stop_on_error
143    ///    If \b true execution will end on the first command that causes an error.
144    /// @param[in] echo_commands
145    ///    If \b true echo the command before executing it.  If \false, execute silently.
146    /// @param[in] print_results
147    ///    If \b true print the results of the command after executing it.  If \false, execute silently.
148    /// @param[out] result
149    ///    This is marked as succeeding with no output if all commands execute safely,
150    ///    and failed with some explanation if we aborted executing the commands at some point.
151    //------------------------------------------------------------------
152    void
153    HandleCommandsFromFile (FileSpec &file,
154                    ExecutionContext *context,
155                    bool stop_on_continue,
156                    bool stop_on_error,
157                    bool echo_commands,
158                    bool print_results,
159                    CommandReturnObject &result);
160
161    CommandObject *
162    GetCommandObjectForCommand (std::string &command_line);
163
164    // This handles command line completion.  You are given a pointer to the command string buffer, to the current cursor,
165    // and to the end of the string (in case it is not NULL terminated).
166    // You also passed in an Args object to fill with the returns.
167    // The first element of the array will be filled with the string that you would need to insert at
168    // the cursor point to complete the cursor point to the longest common matching prefix.
169    // If you want to limit the number of elements returned, set max_return_elements to the number of elements
170    // you want returned.  Otherwise set max_return_elements to -1.
171    // If you want to start some way into the match list, then set match_start_point to the desired start
172    // point.
173    // Returns the total number of completions, or -1 if the completion character should be inserted, or
174    // INT_MAX if the number of matches is > max_return_elements, but it is expensive to compute.
175    //
176    // FIXME: Only max_return_elements == -1 is supported at present.
177
178    int
179    HandleCompletion (const char *current_line,
180                                      const char *cursor,
181                                      const char *last_char,
182                                      int match_start_point,
183                                      int max_return_elements,
184                                      StringList &matches);
185
186    // This version just returns matches, and doesn't compute the substring.  It is here so the
187    // Help command can call it for the first argument.
188    // word_complete tells whether a the completions are considered a "complete" response (so the
189    // completer should complete the quote & put a space after the word.
190
191    int
192    HandleCompletionMatches (Args &input,
193                      int &cursor_index,
194                      int &cursor_char_position,
195                      int match_start_point,
196                      int max_return_elements,
197                      bool &word_complete,
198                      StringList &matches);
199
200
201    int
202    GetCommandNamesMatchingPartialString (const char *cmd_cstr, bool include_aliases, StringList &matches);
203
204    void
205    GetHelp (CommandReturnObject &result);
206
207    void
208    GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string);
209
210    void
211    OutputFormattedHelpText (Stream &stream,
212                             const char *command_word,
213                             const char *separator,
214                             const char *help_text,
215                             uint32_t max_word_len);
216
217    Debugger &
218    GetDebugger ()
219    {
220        return m_debugger;
221    }
222
223    const char *
224    ProcessEmbeddedScriptCommands (const char *arg);
225
226    const char *
227    GetPrompt ();
228
229    void
230    SetPrompt (const char *);
231
232    bool Confirm (const char *message, bool default_answer);
233
234    static size_t
235    GetConfirmationInputReaderCallback (void *baton,
236                                        InputReader &reader,
237                                        lldb::InputReaderAction action,
238                                        const char *bytes,
239                                        size_t bytes_len);
240
241    void
242    LoadCommandDictionary ();
243
244    void
245    Initialize ();
246
247    void
248    CrossRegisterCommand (const char * dest_cmd, const char * object_type);
249
250    void
251    SetScriptLanguage (lldb::ScriptLanguage lang);
252
253
254    bool
255    HasCommands ();
256
257    bool
258    HasAliases ();
259
260    bool
261    HasUserCommands ();
262
263    bool
264    HasAliasOptions ();
265
266    void
267    BuildAliasCommandArgs (CommandObject *alias_cmd_obj, const char *alias_name, Args &cmd_args,
268                           std::string &raw_input_string, CommandReturnObject &result);
269
270    int
271    GetOptionArgumentPosition (const char *in_string);
272
273    ScriptInterpreter *
274    GetScriptInterpreter ();
275
276    void
277    SkipLLDBInitFiles (bool skip_lldbinit_files)
278    {
279        m_skip_lldbinit_files = skip_lldbinit_files;
280    }
281
282    bool
283    GetSynchronous ();
284
285#ifndef SWIG
286    void
287    AddLogChannel (const char *name, const Log::Callbacks &log_callbacks);
288
289    bool
290    GetLogChannelCallbacks (const char *channel, Log::Callbacks &log_callbacks);
291
292    bool
293    RemoveLogChannel (const char *name);
294#endif
295
296    size_t
297    FindLongestCommandWord (CommandObject::CommandMap &dict);
298
299    void
300    FindCommandsForApropos (const char *word, StringList &commands_found, StringList &commands_help);
301
302    void
303    AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word,
304                           StringList &commands_found, StringList &commands_help);
305
306protected:
307    friend class Debugger;
308
309    void
310    SetSynchronous (bool value);
311
312    lldb::CommandObjectSP
313    GetCommandSP (const char *cmd, bool include_aliases = true, bool exact = true, StringList *matches = NULL);
314
315private:
316
317    Debugger &m_debugger;   // The debugger session that this interpreter is associated with
318    bool m_synchronous_execution;
319    bool m_skip_lldbinit_files;
320    CommandObject::CommandMap m_command_dict; // Stores basic built-in commands (they cannot be deleted, removed or overwritten).
321    CommandObject::CommandMap m_alias_dict;   // Stores user aliases/abbreviations for commands
322    CommandObject::CommandMap m_user_dict;    // Stores user-defined commands
323    OptionArgMap m_alias_options; // Stores any options (with or without arguments) that go with any alias.
324    std::vector<std::string> m_command_history;
325    std::string m_repeat_command;  // Stores the command that will be executed for an empty command string.
326    std::auto_ptr<ScriptInterpreter> m_script_interpreter_ap;
327    char m_comment_char;
328};
329
330
331} // namespace lldb_private
332
333#endif  // liblldb_CommandInterpreter_h_
334