1 //===-- SBCommandInterpreter.h ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLDB_SBCommandInterpreter_h_
11 #define LLDB_SBCommandInterpreter_h_
15 #include "lldb/API/SBDebugger.h"
16 #include "lldb/API/SBDefines.h"
20 class LLDB_API SBCommandInterpreterRunOptions {
21 friend class SBDebugger;
22 friend class SBCommandInterpreter;
25 SBCommandInterpreterRunOptions();
26 ~SBCommandInterpreterRunOptions();
28 bool GetStopOnContinue() const;
30 void SetStopOnContinue(bool);
32 bool GetStopOnError() const;
34 void SetStopOnError(bool);
36 bool GetStopOnCrash() const;
38 void SetStopOnCrash(bool);
40 bool GetEchoCommands() const;
42 void SetEchoCommands(bool);
44 bool GetEchoCommentCommands() const;
46 void SetEchoCommentCommands(bool echo);
48 bool GetPrintResults() const;
50 void SetPrintResults(bool);
52 bool GetAddToHistory() const;
54 void SetAddToHistory(bool);
57 lldb_private::CommandInterpreterRunOptions *get() const;
59 lldb_private::CommandInterpreterRunOptions &ref() const;
61 // This is set in the constructor and will always be valid.
62 mutable std::unique_ptr<lldb_private::CommandInterpreterRunOptions>
66 class SBCommandInterpreter {
69 eBroadcastBitThreadShouldExit = (1 << 0),
70 eBroadcastBitResetPrompt = (1 << 1),
71 eBroadcastBitQuitCommandReceived = (1 << 2), // User entered quit
72 eBroadcastBitAsynchronousOutputData = (1 << 3),
73 eBroadcastBitAsynchronousErrorData = (1 << 4)
76 SBCommandInterpreter(const lldb::SBCommandInterpreter &rhs);
78 ~SBCommandInterpreter();
80 const lldb::SBCommandInterpreter &
81 operator=(const lldb::SBCommandInterpreter &rhs);
84 GetArgumentTypeAsCString(const lldb::CommandArgumentType arg_type);
87 GetArgumentDescriptionAsCString(const lldb::CommandArgumentType arg_type);
89 static bool EventIsCommandInterpreterEvent(const lldb::SBEvent &event);
93 bool CommandExists(const char *cmd);
95 bool AliasExists(const char *cmd);
97 lldb::SBBroadcaster GetBroadcaster();
99 static const char *GetBroadcasterClass();
105 bool HasAliasOptions();
107 lldb::SBProcess GetProcess();
109 lldb::SBDebugger GetDebugger();
111 lldb::SBCommand AddMultiwordCommand(const char *name, const char *help);
113 lldb::SBCommand AddCommand(const char *name,
114 lldb::SBCommandPluginInterface *impl,
117 lldb::SBCommand AddCommand(const char *name,
118 lldb::SBCommandPluginInterface *impl,
119 const char *help, const char *syntax);
121 void SourceInitFileInHomeDirectory(lldb::SBCommandReturnObject &result);
124 SourceInitFileInCurrentWorkingDirectory(lldb::SBCommandReturnObject &result);
126 lldb::ReturnStatus HandleCommand(const char *command_line,
127 lldb::SBCommandReturnObject &result,
128 bool add_to_history = false);
130 lldb::ReturnStatus HandleCommand(const char *command_line,
131 SBExecutionContext &exe_ctx,
132 SBCommandReturnObject &result,
133 bool add_to_history = false);
135 void HandleCommandsFromFile(lldb::SBFileSpec &file,
136 lldb::SBExecutionContext &override_context,
137 lldb::SBCommandInterpreterRunOptions &options,
138 lldb::SBCommandReturnObject result);
140 // The pointer based interface is not useful in SWIG, since the cursor &
141 // last_char arguments are string pointers INTO current_line and you can't do
142 // that in a scripting language interface in general...
144 // In either case, the way this works is that the you give it a line and
145 // cursor position in the line. The function will return the number of
146 // completions. The matches list will contain number_of_completions + 1
147 // elements. The first element is the common substring after the cursor
148 // position for all the matches. The rest of the elements are the matches.
149 // The first element is useful if you are emulating the common shell behavior
150 // where the tab completes to the string that is common among all the
151 // matches, then you should first check if the first element is non-empty,
152 // and if so just insert it and move the cursor to the end of the insertion.
153 // The next tab will return an empty common substring, and a list of choices
154 // (if any), at which point you should display the choices and let the user
155 // type further to disambiguate.
157 int HandleCompletion(const char *current_line, const char *cursor,
158 const char *last_char, int match_start_point,
159 int max_return_elements, lldb::SBStringList &matches);
161 int HandleCompletion(const char *current_line, uint32_t cursor_pos,
162 int match_start_point, int max_return_elements,
163 lldb::SBStringList &matches);
165 // Same as HandleCompletion, but also fills out `descriptions` with
166 // descriptions for each match.
167 int HandleCompletionWithDescriptions(
168 const char *current_line, const char *cursor, const char *last_char,
169 int match_start_point, int max_return_elements,
170 lldb::SBStringList &matches, lldb::SBStringList &descriptions);
172 int HandleCompletionWithDescriptions(const char *current_line,
174 int match_start_point,
175 int max_return_elements,
176 lldb::SBStringList &matches,
177 lldb::SBStringList &descriptions);
179 bool WasInterrupted() const;
181 // Catch commands before they execute by registering a callback that will get
182 // called when the command gets executed. This allows GUI or command line
183 // interfaces to intercept a command and stop it from happening
184 bool SetCommandOverrideCallback(const char *command_name,
185 lldb::CommandOverrideCallback callback,
188 SBCommandInterpreter(
189 lldb_private::CommandInterpreter *interpreter_ptr =
190 nullptr); // Access using SBDebugger::GetCommandInterpreter();
192 //----------------------------------------------------------------------
193 /// Return true if the command interpreter is the active IO handler.
195 /// This indicates that any input coming into the debugger handles will
196 /// go to the command interpreter and will result in LLDB command line
197 /// commands being executed.
198 //----------------------------------------------------------------------
201 //----------------------------------------------------------------------
202 /// Get the string that needs to be written to the debugger stdin file
203 /// handle when a control character is typed.
205 /// Some GUI programs will intercept "control + char" sequences and want
206 /// to have them do what normally would happen when using a real
207 /// terminal, so this function allows GUI programs to emulate this
211 /// The character that was typed along with the control key
214 /// The string that should be written into the file handle that is
215 /// feeding the input stream for the debugger, or nullptr if there is
216 /// no string for this control key.
217 //----------------------------------------------------------------------
218 const char *GetIOHandlerControlSequence(char ch);
220 bool GetPromptOnQuit();
222 void SetPromptOnQuit(bool b);
224 //----------------------------------------------------------------------
225 /// Sets whether the command interpreter should allow custom exit codes
226 /// for the 'quit' command.
227 //----------------------------------------------------------------------
228 void AllowExitCodeOnQuit(bool allow);
230 //----------------------------------------------------------------------
231 /// Returns true if the user has called the 'quit' command with a custom exit
233 //----------------------------------------------------------------------
234 bool HasCustomQuitExitCode();
236 //----------------------------------------------------------------------
237 /// Returns the exit code that the user has specified when running the
238 /// 'quit' command. Returns 0 if the user hasn't called 'quit' at all or
239 /// without a custom exit code.
240 //----------------------------------------------------------------------
243 //----------------------------------------------------------------------
244 /// Resolve the command just as HandleCommand would, expanding abbreviations
245 /// and aliases. If successful, result->GetOutput has the full expansion.
246 //----------------------------------------------------------------------
247 void ResolveCommand(const char *command_line, SBCommandReturnObject &result);
250 lldb_private::CommandInterpreter &ref();
252 lldb_private::CommandInterpreter *get();
254 void reset(lldb_private::CommandInterpreter *);
257 friend class SBDebugger;
259 static void InitializeSWIG();
261 lldb_private::CommandInterpreter *m_opaque_ptr;
264 class SBCommandPluginInterface {
266 virtual ~SBCommandPluginInterface() = default;
268 virtual bool DoExecute(lldb::SBDebugger /*debugger*/, char ** /*command*/,
269 lldb::SBCommandReturnObject & /*result*/) {
280 const char *GetName();
282 const char *GetHelp();
284 const char *GetHelpLong();
286 void SetHelp(const char *);
288 void SetHelpLong(const char *);
292 void SetFlags(uint32_t flags);
294 lldb::SBCommand AddMultiwordCommand(const char *name,
295 const char *help = nullptr);
297 lldb::SBCommand AddCommand(const char *name,
298 lldb::SBCommandPluginInterface *impl,
299 const char *help = nullptr);
301 lldb::SBCommand AddCommand(const char *name,
302 lldb::SBCommandPluginInterface *impl,
303 const char *help, const char *syntax);
306 friend class SBDebugger;
307 friend class SBCommandInterpreter;
309 SBCommand(lldb::CommandObjectSP cmd_sp);
311 lldb::CommandObjectSP m_opaque_sp;
316 #endif // LLDB_SBCommandInterpreter_h_