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