blob: 8a302feccab2bd5b20d14cfe682c3c02d62a681f [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandInterpreter.cpp ----------------------------------*- 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#include <string>
Caroline Ticebd5c63e2010-10-12 21:57:09 +000011#include <vector>
Chris Lattner24943d22010-06-08 16:52:24 +000012
13#include <getopt.h>
14#include <stdlib.h>
15
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000016#include "../Commands/CommandObjectApropos.h"
17#include "../Commands/CommandObjectArgs.h"
18#include "../Commands/CommandObjectBreakpoint.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000019//#include "../Commands/CommandObjectCall.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000020#include "../Commands/CommandObjectDisassemble.h"
21#include "../Commands/CommandObjectExpression.h"
22#include "../Commands/CommandObjectFile.h"
23#include "../Commands/CommandObjectFrame.h"
24#include "../Commands/CommandObjectHelp.h"
25#include "../Commands/CommandObjectImage.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000026#include "../Commands/CommandObjectLog.h"
27#include "../Commands/CommandObjectMemory.h"
28#include "../Commands/CommandObjectProcess.h"
29#include "../Commands/CommandObjectQuit.h"
Eli Friedmanb34d2a22010-06-09 22:08:29 +000030#include "lldb/Interpreter/CommandObjectRegexCommand.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000031#include "../Commands/CommandObjectRegister.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032#include "CommandObjectScript.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000033#include "../Commands/CommandObjectSettings.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000034#include "../Commands/CommandObjectSource.h"
Jim Ingham767af882010-07-07 03:36:20 +000035#include "../Commands/CommandObjectCommands.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000036#include "../Commands/CommandObjectSyntax.h"
37#include "../Commands/CommandObjectTarget.h"
38#include "../Commands/CommandObjectThread.h"
Johnny Chen902e0182010-12-23 20:21:44 +000039#include "../Commands/CommandObjectVersion.h"
Chris Lattner24943d22010-06-08 16:52:24 +000040
Jim Ingham84cdc152010-06-15 19:49:27 +000041#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000042#include "lldb/Core/Debugger.h"
Jim Ingham5e16ef52010-10-04 19:49:29 +000043#include "lldb/Core/InputReader.h"
Chris Lattner24943d22010-06-08 16:52:24 +000044#include "lldb/Core/Stream.h"
45#include "lldb/Core/Timer.h"
Greg Claytoncd548032011-02-01 01:31:41 +000046#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000047#include "lldb/Target/Process.h"
48#include "lldb/Target/Thread.h"
49#include "lldb/Target/TargetList.h"
Greg Claytone98ac252010-11-10 04:57:04 +000050#include "lldb/Utility/CleanUp.h"
Chris Lattner24943d22010-06-08 16:52:24 +000051
52#include "lldb/Interpreter/CommandReturnObject.h"
53#include "lldb/Interpreter/CommandInterpreter.h"
Caroline Tice0aa2e552011-01-14 00:29:16 +000054#include "lldb/Interpreter/ScriptInterpreterNone.h"
55#include "lldb/Interpreter/ScriptInterpreterPython.h"
Chris Lattner24943d22010-06-08 16:52:24 +000056
57using namespace lldb;
58using namespace lldb_private;
59
60CommandInterpreter::CommandInterpreter
61(
Greg Clayton63094e02010-06-23 01:19:29 +000062 Debugger &debugger,
Chris Lattner24943d22010-06-08 16:52:24 +000063 ScriptLanguage script_language,
Greg Clayton63094e02010-06-23 01:19:29 +000064 bool synchronous_execution
Chris Lattner24943d22010-06-08 16:52:24 +000065) :
Greg Clayton49ce6822010-10-31 03:01:06 +000066 Broadcaster ("lldb.command-interpreter"),
Greg Clayton63094e02010-06-23 01:19:29 +000067 m_debugger (debugger),
Greg Clayton887aa282010-10-11 01:05:37 +000068 m_synchronous_execution (synchronous_execution),
Caroline Tice0aa2e552011-01-14 00:29:16 +000069 m_skip_lldbinit_files (false),
70 m_script_interpreter_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000071{
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000072 const char *dbg_name = debugger.GetInstanceName().AsCString();
73 std::string lang_name = ScriptInterpreter::LanguageToString (script_language);
74 StreamString var_name;
75 var_name.Printf ("[%s].script-lang", dbg_name);
Caroline Tice1d2aefd2010-09-09 06:25:08 +000076 debugger.GetSettingsController()->SetVariable (var_name.GetData(), lang_name.c_str(),
77 lldb::eVarSetOperationAssign, false,
Greg Clayton49ce6822010-10-31 03:01:06 +000078 m_debugger.GetInstanceName().AsCString());
79 SetEventName (eBroadcastBitThreadShouldExit, "thread-should-exit");
80 SetEventName (eBroadcastBitResetPrompt, "reset-prompt");
81 SetEventName (eBroadcastBitQuitCommandReceived, "quit");
Chris Lattner24943d22010-06-08 16:52:24 +000082}
83
84void
85CommandInterpreter::Initialize ()
86{
87 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
88
89 CommandReturnObject result;
90
91 LoadCommandDictionary ();
92
Chris Lattner24943d22010-06-08 16:52:24 +000093 // Set up some initial aliases.
Jim Ingham767af882010-07-07 03:36:20 +000094 result.Clear(); HandleCommand ("command alias q quit", false, result);
Jim Inghame3663e82010-10-22 18:47:16 +000095 result.Clear(); HandleCommand ("command alias run process launch --", false, result);
96 result.Clear(); HandleCommand ("command alias r process launch --", false, result);
Jim Ingham767af882010-07-07 03:36:20 +000097 result.Clear(); HandleCommand ("command alias c process continue", false, result);
98 result.Clear(); HandleCommand ("command alias continue process continue", false, result);
99 result.Clear(); HandleCommand ("command alias expr expression", false, result);
100 result.Clear(); HandleCommand ("command alias exit quit", false, result);
Greg Clayton0f3a8eb2010-09-16 17:09:23 +0000101 result.Clear(); HandleCommand ("command alias b regexp-break", false, result);
Jim Ingham767af882010-07-07 03:36:20 +0000102 result.Clear(); HandleCommand ("command alias bt thread backtrace", false, result);
103 result.Clear(); HandleCommand ("command alias si thread step-inst", false, result);
104 result.Clear(); HandleCommand ("command alias step thread step-in", false, result);
105 result.Clear(); HandleCommand ("command alias s thread step-in", false, result);
106 result.Clear(); HandleCommand ("command alias next thread step-over", false, result);
107 result.Clear(); HandleCommand ("command alias n thread step-over", false, result);
108 result.Clear(); HandleCommand ("command alias finish thread step-out", false, result);
109 result.Clear(); HandleCommand ("command alias x memory read", false, result);
110 result.Clear(); HandleCommand ("command alias l source list", false, result);
111 result.Clear(); HandleCommand ("command alias list source list", false, result);
Greg Clayton0f3a8eb2010-09-16 17:09:23 +0000112 result.Clear(); HandleCommand ("command alias p frame variable", false, result);
113 result.Clear(); HandleCommand ("command alias print frame variable", false, result);
Jim Inghame3663e82010-10-22 18:47:16 +0000114 result.Clear(); HandleCommand ("command alias po expression -o --", false, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000115}
116
Chris Lattner24943d22010-06-08 16:52:24 +0000117const char *
118CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg)
119{
120 // This function has not yet been implemented.
121
122 // Look for any embedded script command
123 // If found,
124 // get interpreter object from the command dictionary,
125 // call execute_one_command on it,
126 // get the results as a string,
127 // substitute that string for current stuff.
128
129 return arg;
130}
131
132
133void
134CommandInterpreter::LoadCommandDictionary ()
135{
136 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
137
138 // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT ***
139 //
140 // Command objects that are used as cross reference objects (i.e. they inherit from CommandObjectCrossref)
141 // *MUST* be created and put into the command dictionary *BEFORE* any multi-word commands (which may use
142 // the cross-referencing stuff) are created!!!
143 //
144 // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT ***
145
146
147 // Command objects that inherit from CommandObjectCrossref must be created before other command objects
148 // are created. This is so that when another command is created that needs to go into a crossref object,
149 // the crossref object exists and is ready to take the cross reference. Put the cross referencing command
150 // objects into the CommandDictionary now, so they are ready for use when the other commands get created.
151
Chris Lattner24943d22010-06-08 16:52:24 +0000152 // Non-CommandObjectCrossref commands can now be created.
153
Caroline Tice5bc8c972010-09-20 20:44:43 +0000154 lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage();
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000155
Greg Clayton238c0a12010-09-18 01:14:36 +0000156 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000157 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000158 //m_command_dict["call"] = CommandObjectSP (new CommandObjectCall (*this));
Jim Ingham767af882010-07-07 03:36:20 +0000159 m_command_dict["commands"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000160 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this));
161 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this));
162 m_command_dict["file"] = CommandObjectSP (new CommandObjectFile (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000163 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000164 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000165 m_command_dict["image"] = CommandObjectSP (new CommandObjectImage (*this));
166 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
167 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
168 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000169 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000170 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000171 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000172 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
Jim Ingham767af882010-07-07 03:36:20 +0000173 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000174 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
175 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
Johnny Chen902e0182010-12-23 20:21:44 +0000176 m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000177
178 std::auto_ptr<CommandObjectRegexCommand>
Greg Clayton238c0a12010-09-18 01:14:36 +0000179 break_regex_cmd_ap(new CommandObjectRegexCommand (*this,
180 "regexp-break",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000181 "Set a breakpoint using a regular expression to specify the location.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000182 "regexp-break [<filename>:<linenum>]\nregexp-break [<address>]\nregexp-break <...>", 2));
Chris Lattner24943d22010-06-08 16:52:24 +0000183 if (break_regex_cmd_ap.get())
184 {
185 if (break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2") &&
186 break_regex_cmd_ap->AddRegexCommand("^(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1") &&
187 break_regex_cmd_ap->AddRegexCommand("^[\"']?([-+]\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'") &&
188 break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list") &&
189 break_regex_cmd_ap->AddRegexCommand("^(-.*)$", "breakpoint set %1") &&
Greg Claytonb01000f2011-01-17 03:46:26 +0000190 break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%2' --shlib '%1'") &&
Chris Lattner24943d22010-06-08 16:52:24 +0000191 break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1'"))
192 {
193 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
194 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
195 }
196 }
197}
198
199int
200CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
201 StringList &matches)
202{
203 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
204
205 if (include_aliases)
206 {
207 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
208 }
209
210 return matches.GetSize();
211}
212
213CommandObjectSP
214CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
215{
216 CommandObject::CommandMap::iterator pos;
217 CommandObjectSP ret_val;
218
219 std::string cmd(cmd_cstr);
220
221 if (HasCommands())
222 {
223 pos = m_command_dict.find(cmd);
224 if (pos != m_command_dict.end())
225 ret_val = pos->second;
226 }
227
228 if (include_aliases && HasAliases())
229 {
230 pos = m_alias_dict.find(cmd);
231 if (pos != m_alias_dict.end())
232 ret_val = pos->second;
233 }
234
235 if (HasUserCommands())
236 {
237 pos = m_user_dict.find(cmd);
238 if (pos != m_user_dict.end())
239 ret_val = pos->second;
240 }
241
242 if (!exact && ret_val == NULL)
243 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000244 // We will only get into here if we didn't find any exact matches.
245
246 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
247
Chris Lattner24943d22010-06-08 16:52:24 +0000248 StringList local_matches;
249 if (matches == NULL)
250 matches = &local_matches;
251
Jim Inghamd40f8a62010-07-06 22:46:59 +0000252 unsigned int num_cmd_matches = 0;
253 unsigned int num_alias_matches = 0;
254 unsigned int num_user_matches = 0;
255
256 // Look through the command dictionaries one by one, and if we get only one match from any of
257 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
258
Chris Lattner24943d22010-06-08 16:52:24 +0000259 if (HasCommands())
260 {
261 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
262 }
263
264 if (num_cmd_matches == 1)
265 {
266 cmd.assign(matches->GetStringAtIndex(0));
267 pos = m_command_dict.find(cmd);
268 if (pos != m_command_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000269 real_match_sp = pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000270 }
271
Jim Ingham9a574172010-06-24 20:28:42 +0000272 if (include_aliases && HasAliases())
Chris Lattner24943d22010-06-08 16:52:24 +0000273 {
274 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
275
276 }
277
Jim Inghamd40f8a62010-07-06 22:46:59 +0000278 if (num_alias_matches == 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000279 {
280 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
281 pos = m_alias_dict.find(cmd);
282 if (pos != m_alias_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000283 alias_match_sp = pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000284 }
285
Jim Ingham9a574172010-06-24 20:28:42 +0000286 if (HasUserCommands())
Chris Lattner24943d22010-06-08 16:52:24 +0000287 {
288 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
289 }
290
Jim Inghamd40f8a62010-07-06 22:46:59 +0000291 if (num_user_matches == 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000292 {
293 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
294
295 pos = m_user_dict.find (cmd);
296 if (pos != m_user_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000297 user_match_sp = pos->second;
298 }
299
300 // If we got exactly one match, return that, otherwise return the match list.
301
302 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
303 {
304 if (num_cmd_matches)
305 return real_match_sp;
306 else if (num_alias_matches)
307 return alias_match_sp;
308 else
309 return user_match_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000310 }
311 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000312 else if (matches && ret_val != NULL)
313 {
314 matches->AppendString (cmd_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +0000315 }
316
317
318 return ret_val;
319}
320
Jim Inghamd40f8a62010-07-06 22:46:59 +0000321CommandObjectSP
322CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
Chris Lattner24943d22010-06-08 16:52:24 +0000323{
Caroline Tice56d2fc42010-12-14 18:51:39 +0000324 Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command.
325 CommandObjectSP ret_val; // Possibly empty return value.
326
327 if (cmd_cstr == NULL)
328 return ret_val;
329
330 if (cmd_words.GetArgumentCount() == 1)
331 return GetCommandSP(cmd_cstr, include_aliases, true, NULL);
332 else
333 {
334 // We have a multi-word command (seemingly), so we need to do more work.
335 // First, get the cmd_obj_sp for the first word in the command.
336 CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, NULL);
337 if (cmd_obj_sp.get() != NULL)
338 {
339 // Loop through the rest of the words in the command (everything passed in was supposed to be part of a
340 // command name), and find the appropriate sub-command SP for each command word....
341 size_t end = cmd_words.GetArgumentCount();
342 for (size_t j= 1; j < end; ++j)
343 {
344 if (cmd_obj_sp->IsMultiwordObject())
345 {
346 cmd_obj_sp = ((CommandObjectMultiword *) cmd_obj_sp.get())->GetSubcommandSP
347 (cmd_words.GetArgumentAtIndex (j));
348 if (cmd_obj_sp.get() == NULL)
349 // The sub-command name was invalid. Fail and return the empty 'ret_val'.
350 return ret_val;
351 }
352 else
353 // We have more words in the command name, but we don't have a multiword object. Fail and return
354 // empty 'ret_val'.
355 return ret_val;
356 }
357 // We successfully looped through all the command words and got valid command objects for them. Assign the
358 // last object retrieved to 'ret_val'.
359 ret_val = cmd_obj_sp;
360 }
361 }
362 return ret_val;
Jim Inghamd40f8a62010-07-06 22:46:59 +0000363}
364
365CommandObject *
366CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
367{
368 return GetCommandSPExact (cmd_cstr, include_aliases).get();
369}
370
371CommandObject *
372CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
373{
374 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
375
376 // If we didn't find an exact match to the command string in the commands, look in
377 // the aliases.
378
379 if (command_obj == NULL)
380 {
381 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
382 }
383
384 // Finally, if there wasn't an exact match among the aliases, look for an inexact match
385 // in both the commands and the aliases.
386
387 if (command_obj == NULL)
388 command_obj = GetCommandSP(cmd_cstr, true, false, matches).get();
389
390 return command_obj;
Chris Lattner24943d22010-06-08 16:52:24 +0000391}
392
393bool
394CommandInterpreter::CommandExists (const char *cmd)
395{
396 return m_command_dict.find(cmd) != m_command_dict.end();
397}
398
399bool
400CommandInterpreter::AliasExists (const char *cmd)
401{
402 return m_alias_dict.find(cmd) != m_alias_dict.end();
403}
404
405bool
406CommandInterpreter::UserCommandExists (const char *cmd)
407{
408 return m_user_dict.find(cmd) != m_user_dict.end();
409}
410
411void
412CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
413{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000414 command_obj_sp->SetIsAlias (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000415 m_alias_dict[alias_name] = command_obj_sp;
416}
417
418bool
419CommandInterpreter::RemoveAlias (const char *alias_name)
420{
421 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
422 if (pos != m_alias_dict.end())
423 {
424 m_alias_dict.erase(pos);
425 return true;
426 }
427 return false;
428}
429bool
430CommandInterpreter::RemoveUser (const char *alias_name)
431{
432 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
433 if (pos != m_user_dict.end())
434 {
435 m_user_dict.erase(pos);
436 return true;
437 }
438 return false;
439}
440
Chris Lattner24943d22010-06-08 16:52:24 +0000441void
442CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
443{
444 help_string.Printf ("'%s", command_name);
445 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
446
447 if (option_arg_vector_sp != NULL)
448 {
449 OptionArgVector *options = option_arg_vector_sp.get();
450 for (int i = 0; i < options->size(); ++i)
451 {
452 OptionArgPair cur_option = (*options)[i];
453 std::string opt = cur_option.first;
Caroline Tice44c841d2010-12-07 19:58:26 +0000454 OptionArgValue value_pair = cur_option.second;
455 std::string value = value_pair.second;
Chris Lattner24943d22010-06-08 16:52:24 +0000456 if (opt.compare("<argument>") == 0)
457 {
458 help_string.Printf (" %s", value.c_str());
459 }
460 else
461 {
462 help_string.Printf (" %s", opt.c_str());
463 if ((value.compare ("<no-argument>") != 0)
464 && (value.compare ("<need-argument") != 0))
465 {
466 help_string.Printf (" %s", value.c_str());
467 }
468 }
469 }
470 }
471
472 help_string.Printf ("'");
473}
474
Greg Clayton65124ea2010-08-26 22:05:43 +0000475size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000476CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
477{
478 CommandObject::CommandMap::const_iterator pos;
Greg Clayton65124ea2010-08-26 22:05:43 +0000479 CommandObject::CommandMap::const_iterator end = dict.end();
480 size_t max_len = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000481
Greg Clayton65124ea2010-08-26 22:05:43 +0000482 for (pos = dict.begin(); pos != end; ++pos)
483 {
484 size_t len = pos->first.size();
485 if (max_len < len)
486 max_len = len;
Chris Lattner24943d22010-06-08 16:52:24 +0000487 }
Greg Clayton65124ea2010-08-26 22:05:43 +0000488 return max_len;
Chris Lattner24943d22010-06-08 16:52:24 +0000489}
490
491void
492CommandInterpreter::GetHelp (CommandReturnObject &result)
493{
494 CommandObject::CommandMap::const_iterator pos;
495 result.AppendMessage("The following is a list of built-in, permanent debugger commands:");
496 result.AppendMessage("");
Greg Clayton65124ea2010-08-26 22:05:43 +0000497 uint32_t max_len = FindLongestCommandWord (m_command_dict);
Chris Lattner24943d22010-06-08 16:52:24 +0000498
499 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
500 {
501 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
502 max_len);
503 }
504 result.AppendMessage("");
505
506 if (m_alias_dict.size() > 0)
507 {
Jim Inghame3663e82010-10-22 18:47:16 +0000508 result.AppendMessage("The following is a list of your current command abbreviations "
509 "(see 'help commands alias' for more info):");
Chris Lattner24943d22010-06-08 16:52:24 +0000510 result.AppendMessage("");
Greg Clayton65124ea2010-08-26 22:05:43 +0000511 max_len = FindLongestCommandWord (m_alias_dict);
512
Chris Lattner24943d22010-06-08 16:52:24 +0000513 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
514 {
515 StreamString sstr;
516 StreamString translation_and_help;
517 std::string entry_name = pos->first;
518 std::string second_entry = pos->second.get()->GetCommandName();
519 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
520
521 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
522 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
523 translation_and_help.GetData(), max_len);
524 }
525 result.AppendMessage("");
526 }
527
528 if (m_user_dict.size() > 0)
529 {
530 result.AppendMessage ("The following is a list of your current user-defined commands:");
531 result.AppendMessage("");
532 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
533 {
534 result.AppendMessageWithFormat ("%s -- %s\n", pos->first.c_str(), pos->second->GetHelp());
535 }
536 result.AppendMessage("");
537 }
538
539 result.AppendMessage("For more information on any particular command, try 'help <command-name>'.");
540}
541
Caroline Ticee0da7a52010-12-09 22:52:49 +0000542CommandObject *
543CommandInterpreter::GetCommandObjectForCommand (std::string &command_string)
Chris Lattner24943d22010-06-08 16:52:24 +0000544{
Caroline Ticee0da7a52010-12-09 22:52:49 +0000545 // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will
546 // eventually be invoked by the given command line.
547
548 CommandObject *cmd_obj = NULL;
549 std::string white_space (" \t\v");
550 size_t start = command_string.find_first_not_of (white_space);
551 size_t end = 0;
552 bool done = false;
553 while (!done)
554 {
555 if (start != std::string::npos)
556 {
557 // Get the next word from command_string.
558 end = command_string.find_first_of (white_space, start);
559 if (end == std::string::npos)
560 end = command_string.size();
561 std::string cmd_word = command_string.substr (start, end - start);
562
563 if (cmd_obj == NULL)
564 // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid
565 // command or alias.
566 cmd_obj = GetCommandObject (cmd_word.c_str());
567 else if (cmd_obj->IsMultiwordObject ())
568 {
569 // Our current object is a multi-word object; see if the cmd_word is a valid sub-command for our object.
570 CommandObject *sub_cmd_obj =
571 ((CommandObjectMultiword *) cmd_obj)->GetSubcommandObject (cmd_word.c_str());
572 if (sub_cmd_obj)
573 cmd_obj = sub_cmd_obj;
574 else // cmd_word was not a valid sub-command word, so we are donee
575 done = true;
576 }
577 else
578 // We have a cmd_obj and it is not a multi-word object, so we are done.
579 done = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000580
Caroline Ticee0da7a52010-12-09 22:52:49 +0000581 // If we didn't find a valid command object, or our command object is not a multi-word object, or
582 // we are at the end of the command_string, then we are done. Otherwise, find the start of the
583 // next word.
584
585 if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size())
586 done = true;
587 else
588 start = command_string.find_first_not_of (white_space, end);
589 }
590 else
591 // Unable to find any more words.
592 done = true;
593 }
594
595 if (end == command_string.size())
596 command_string.clear();
597 else
598 command_string = command_string.substr(end);
599
600 return cmd_obj;
601}
602
603bool
604CommandInterpreter::StripFirstWord (std::string &command_string, std::string &word)
605{
606 std::string white_space (" \t\v");
607 size_t start;
608 size_t end;
609
610 start = command_string.find_first_not_of (white_space);
611 if (start != std::string::npos)
612 {
613 end = command_string.find_first_of (white_space, start);
614 if (end != std::string::npos)
615 {
616 word = command_string.substr (start, end - start);
617 command_string = command_string.substr (end);
618 size_t pos = command_string.find_first_not_of (white_space);
619 if ((pos != 0) && (pos != std::string::npos))
620 command_string = command_string.substr (pos);
621 }
622 else
623 {
624 word = command_string.substr (start);
625 command_string.erase();
626 }
627
628 }
629 return true;
630}
631
632void
633CommandInterpreter::BuildAliasResult (const char *alias_name, std::string &raw_input_string, std::string &alias_result,
634 CommandObject *&alias_cmd_obj, CommandReturnObject &result)
635{
636 Args cmd_args (raw_input_string.c_str());
637 alias_cmd_obj = GetCommandObject (alias_name);
638 StreamString result_str;
639
640 if (alias_cmd_obj)
641 {
642 std::string alias_name_str = alias_name;
643 if ((cmd_args.GetArgumentCount() == 0)
644 || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0))
645 cmd_args.Unshift (alias_name);
646
647 result_str.Printf ("%s", alias_cmd_obj->GetCommandName ());
648 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
649
650 if (option_arg_vector_sp.get())
651 {
652 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
653
654 for (int i = 0; i < option_arg_vector->size(); ++i)
655 {
656 OptionArgPair option_pair = (*option_arg_vector)[i];
657 OptionArgValue value_pair = option_pair.second;
658 int value_type = value_pair.first;
659 std::string option = option_pair.first;
660 std::string value = value_pair.second;
661 if (option.compare ("<argument>") == 0)
662 result_str.Printf (" %s", value.c_str());
663 else
664 {
665 result_str.Printf (" %s", option.c_str());
666 if (value_type != optional_argument)
667 result_str.Printf (" ");
668 if (value.compare ("<no_argument>") != 0)
669 {
670 int index = GetOptionArgumentPosition (value.c_str());
671 if (index == 0)
672 result_str.Printf ("%s", value.c_str());
673 else if (index >= cmd_args.GetArgumentCount())
674 {
675
676 result.AppendErrorWithFormat
677 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
678 index);
679 result.SetStatus (eReturnStatusFailed);
680 return;
681 }
682 else
683 {
684 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
685 if (strpos != std::string::npos)
686 raw_input_string = raw_input_string.erase (strpos,
687 strlen (cmd_args.GetArgumentAtIndex (index)));
688 result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index));
689 }
690 }
691 }
692 }
693 }
694
695 alias_result = result_str.GetData();
696 }
697}
698
699bool
700CommandInterpreter::HandleCommand (const char *command_line,
701 bool add_to_history,
702 CommandReturnObject &result,
703 ExecutionContext *override_context)
704{
705 bool done = false;
706 CommandObject *cmd_obj = NULL;
707 std::string next_word;
708 bool wants_raw_input = false;
709 std::string command_string (command_line);
710
711 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS));
Greg Claytone98ac252010-11-10 04:57:04 +0000712 Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line);
713
714 // Make a scoped cleanup object that will clear the crash description string
715 // on exit of this function.
716 lldb_utility::CleanUp <const char *, void> crash_description_cleanup(NULL, Host::SetCrashDescription);
717
Caroline Ticee0da7a52010-12-09 22:52:49 +0000718 if (log)
719 log->Printf ("Processing command: %s", command_line);
Chris Lattner24943d22010-06-08 16:52:24 +0000720
Jim Inghamabab14b2010-11-04 23:08:45 +0000721 Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line);
722
Greg Clayton63094e02010-06-23 01:19:29 +0000723 m_debugger.UpdateExecutionContext (override_context);
Chris Lattner24943d22010-06-08 16:52:24 +0000724
725 if (command_line == NULL || command_line[0] == '\0')
726 {
727 if (m_command_history.empty())
728 {
729 result.AppendError ("empty command");
730 result.SetStatus(eReturnStatusFailed);
731 return false;
732 }
733 else
734 {
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000735 command_line = m_repeat_command.c_str();
Caroline Ticee0da7a52010-12-09 22:52:49 +0000736 command_string = command_line;
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000737 if (m_repeat_command.empty())
738 {
Jim Ingham767af882010-07-07 03:36:20 +0000739 result.AppendErrorWithFormat("No auto repeat.\n");
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000740 result.SetStatus (eReturnStatusFailed);
741 return false;
742 }
Chris Lattner24943d22010-06-08 16:52:24 +0000743 }
744 add_to_history = false;
745 }
746
Caroline Ticee0da7a52010-12-09 22:52:49 +0000747 // Phase 1.
748
749 // Before we do ANY kind of argument processing, etc. we need to figure out what the real/final command object
750 // is for the specified command, and whether or not it wants raw input. This gets complicated by the fact that
751 // the user could have specified an alias, and in translating the alias there may also be command options and/or
752 // even data (including raw text strings) that need to be found and inserted into the command line as part of
753 // the translation. So this first step is plain look-up & replacement, resulting in three things: 1). the command
Greg Clayton5d187e52011-01-08 20:28:42 +0000754 // object whose Execute method will actually be called; 2). a revised command string, with all substitutions &
Caroline Ticee0da7a52010-12-09 22:52:49 +0000755 // replacements taken care of; 3). whether or not the Execute function wants raw input or not.
Caroline Tice44c841d2010-12-07 19:58:26 +0000756
Caroline Ticee0da7a52010-12-09 22:52:49 +0000757 StreamString revised_command_line;
Caroline Ticeea6e3df2010-12-11 08:16:56 +0000758 size_t actual_cmd_name_len = 0;
Caroline Ticee0da7a52010-12-09 22:52:49 +0000759 while (!done)
Chris Lattner24943d22010-06-08 16:52:24 +0000760 {
Caroline Ticee0da7a52010-12-09 22:52:49 +0000761 StripFirstWord (command_string, next_word);
762 if (!cmd_obj && AliasExists (next_word.c_str()))
Chris Lattner24943d22010-06-08 16:52:24 +0000763 {
Caroline Ticee0da7a52010-12-09 22:52:49 +0000764 std::string alias_result;
765 BuildAliasResult (next_word.c_str(), command_string, alias_result, cmd_obj, result);
766 revised_command_line.Printf ("%s", alias_result.c_str());
767 if (cmd_obj)
Caroline Tice56d2fc42010-12-14 18:51:39 +0000768 {
Caroline Ticee0da7a52010-12-09 22:52:49 +0000769 wants_raw_input = cmd_obj->WantsRawCommandString ();
Caroline Tice56d2fc42010-12-14 18:51:39 +0000770 actual_cmd_name_len = strlen (cmd_obj->GetCommandName());
771 }
Caroline Ticee0da7a52010-12-09 22:52:49 +0000772 }
773 else if (!cmd_obj)
774 {
775 cmd_obj = GetCommandObject (next_word.c_str());
776 if (cmd_obj)
Chris Lattner24943d22010-06-08 16:52:24 +0000777 {
Caroline Ticeea6e3df2010-12-11 08:16:56 +0000778 actual_cmd_name_len += next_word.length();
Caroline Ticee0da7a52010-12-09 22:52:49 +0000779 revised_command_line.Printf ("%s", next_word.c_str());
780 wants_raw_input = cmd_obj->WantsRawCommandString ();
Chris Lattner24943d22010-06-08 16:52:24 +0000781 }
782 else
783 {
Caroline Ticee0da7a52010-12-09 22:52:49 +0000784 revised_command_line.Printf ("%s", next_word.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000785 }
786 }
Caroline Ticee0da7a52010-12-09 22:52:49 +0000787 else if (cmd_obj->IsMultiwordObject ())
788 {
789 CommandObject *sub_cmd_obj = ((CommandObjectMultiword *) cmd_obj)->GetSubcommandObject (next_word.c_str());
790 if (sub_cmd_obj)
791 {
Caroline Ticeea6e3df2010-12-11 08:16:56 +0000792 actual_cmd_name_len += next_word.length() + 1;
Caroline Ticee0da7a52010-12-09 22:52:49 +0000793 revised_command_line.Printf (" %s", next_word.c_str());
794 cmd_obj = sub_cmd_obj;
795 wants_raw_input = cmd_obj->WantsRawCommandString ();
796 }
797 else
798 {
799 revised_command_line.Printf (" %s", next_word.c_str());
800 done = true;
801 }
802 }
803 else
804 {
805 revised_command_line.Printf (" %s", next_word.c_str());
806 done = true;
807 }
808
809 if (cmd_obj == NULL)
810 {
811 result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str());
812 result.SetStatus (eReturnStatusFailed);
813 return false;
814 }
815
816 next_word.erase ();
817 if (command_string.length() == 0)
818 done = true;
819
Chris Lattner24943d22010-06-08 16:52:24 +0000820 }
Caroline Ticee0da7a52010-12-09 22:52:49 +0000821
822 if (command_string.size() > 0)
823 revised_command_line.Printf (" %s", command_string.c_str());
824
825 // End of Phase 1.
826 // At this point cmd_obj should contain the CommandObject whose Execute method will be called, if the command
827 // specified was valid; revised_command_line contains the complete command line (including command name(s)),
828 // fully translated with all substitutions & translations taken care of (still in raw text format); and
829 // wants_raw_input specifies whether the Execute method expects raw input or not.
830
831
832 if (log)
833 {
834 log->Printf ("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>");
835 log->Printf ("HandleCommand, revised_command_line: '%s'", revised_command_line.GetData());
836 log->Printf ("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False");
837 }
838
839 // Phase 2.
840 // Take care of things like setting up the history command & calling the appropriate Execute method on the
841 // CommandObject, with the appropriate arguments.
842
843 if (cmd_obj != NULL)
844 {
845 if (add_to_history)
846 {
847 Args command_args (revised_command_line.GetData());
848 const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0);
849 if (repeat_command != NULL)
850 m_repeat_command.assign(repeat_command);
851 else
852 m_repeat_command.assign(command_line);
853
854 m_command_history.push_back (command_line);
855 }
856
857 command_string = revised_command_line.GetData();
858 std::string command_name (cmd_obj->GetCommandName());
Caroline Ticeea6e3df2010-12-11 08:16:56 +0000859 std::string remainder;
860 if (actual_cmd_name_len < command_string.length())
861 remainder = command_string.substr (actual_cmd_name_len); // Note: 'actual_cmd_name_len' may be considerably shorter
862 // than cmd_obj->GetCommandName(), because name completion
863 // allows users to enter short versions of the names,
864 // e.g. 'br s' for 'breakpoint set'.
Caroline Ticee0da7a52010-12-09 22:52:49 +0000865
866 // Remove any initial spaces
867 std::string white_space (" \t\v");
868 size_t pos = remainder.find_first_not_of (white_space);
869 if (pos != 0 && pos != std::string::npos)
870 remainder = remainder.substr (pos);
871
872 if (log)
873 log->Printf ("HandleCommand, command line after removing command name(s): '%s'\n", remainder.c_str());
874
875
876 if (wants_raw_input)
877 cmd_obj->ExecuteRawCommandString (remainder.c_str(), result);
878 else
879 {
880 Args cmd_args (remainder.c_str());
881 cmd_obj->ExecuteWithOptions (cmd_args, result);
882 }
883 }
884 else
885 {
886 // We didn't find the first command object, so complete the first argument.
887 Args command_args (revised_command_line.GetData());
888 StringList matches;
889 int num_matches;
890 int cursor_index = 0;
891 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
892 bool word_complete;
893 num_matches = HandleCompletionMatches (command_args,
894 cursor_index,
895 cursor_char_position,
896 0,
897 -1,
898 word_complete,
899 matches);
900
901 if (num_matches > 0)
902 {
903 std::string error_msg;
904 error_msg.assign ("ambiguous command '");
905 error_msg.append(command_args.GetArgumentAtIndex(0));
906 error_msg.append ("'.");
907
908 error_msg.append (" Possible completions:");
909 for (int i = 0; i < num_matches; i++)
910 {
911 error_msg.append ("\n\t");
912 error_msg.append (matches.GetStringAtIndex (i));
913 }
914 error_msg.append ("\n");
915 result.AppendRawError (error_msg.c_str(), error_msg.size());
916 }
917 else
918 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0));
919
920 result.SetStatus (eReturnStatusFailed);
921 }
922
Chris Lattner24943d22010-06-08 16:52:24 +0000923 return result.Succeeded();
924}
925
926int
927CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
928 int &cursor_index,
929 int &cursor_char_position,
930 int match_start_point,
931 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000932 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000933 StringList &matches)
934{
935 int num_command_matches = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000936 bool look_for_subcommand = false;
Jim Ingham802f8b02010-06-30 05:02:46 +0000937
938 // For any of the command completions a unique match will be a complete word.
939 word_complete = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000940
941 if (cursor_index == -1)
942 {
943 // We got nothing on the command line, so return the list of commands
Jim Inghamd40f8a62010-07-06 22:46:59 +0000944 bool include_aliases = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000945 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
946 }
947 else if (cursor_index == 0)
948 {
949 // The cursor is in the first argument, so just do a lookup in the dictionary.
Jim Inghamd40f8a62010-07-06 22:46:59 +0000950 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000951 num_command_matches = matches.GetSize();
952
953 if (num_command_matches == 1
954 && cmd_obj && cmd_obj->IsMultiwordObject()
955 && matches.GetStringAtIndex(0) != NULL
956 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
957 {
958 look_for_subcommand = true;
959 num_command_matches = 0;
960 matches.DeleteStringAtIndex(0);
961 parsed_line.AppendArgument ("");
962 cursor_index++;
963 cursor_char_position = 0;
964 }
965 }
966
967 if (cursor_index > 0 || look_for_subcommand)
968 {
969 // We are completing further on into a commands arguments, so find the command and tell it
970 // to complete the command.
971 // First see if there is a matching initial command:
Jim Inghamd40f8a62010-07-06 22:46:59 +0000972 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000973 if (command_object == NULL)
974 {
975 return 0;
976 }
977 else
978 {
979 parsed_line.Shift();
980 cursor_index--;
Greg Clayton238c0a12010-09-18 01:14:36 +0000981 num_command_matches = command_object->HandleCompletion (parsed_line,
Greg Clayton63094e02010-06-23 01:19:29 +0000982 cursor_index,
983 cursor_char_position,
984 match_start_point,
Jim Ingham802f8b02010-06-30 05:02:46 +0000985 max_return_elements,
986 word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000987 matches);
988 }
989 }
990
991 return num_command_matches;
992
993}
994
995int
996CommandInterpreter::HandleCompletion (const char *current_line,
997 const char *cursor,
998 const char *last_char,
999 int match_start_point,
1000 int max_return_elements,
1001 StringList &matches)
1002{
1003 // We parse the argument up to the cursor, so the last argument in parsed_line is
1004 // the one containing the cursor, and the cursor is after the last character.
1005
1006 Args parsed_line(current_line, last_char - current_line);
1007 Args partial_parsed_line(current_line, cursor - current_line);
1008
1009 int num_args = partial_parsed_line.GetArgumentCount();
1010 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
1011 int cursor_char_position;
1012
1013 if (cursor_index == -1)
1014 cursor_char_position = 0;
1015 else
1016 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
Jim Inghamcf037652010-12-14 19:56:01 +00001017
1018 if (cursor > current_line && cursor[-1] == ' ')
1019 {
1020 // We are just after a space. If we are in an argument, then we will continue
1021 // parsing, but if we are between arguments, then we have to complete whatever the next
1022 // element would be.
1023 // We can distinguish the two cases because if we are in an argument (e.g. because the space is
1024 // protected by a quote) then the space will also be in the parsed argument...
1025
1026 const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index);
1027 if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ')
1028 {
1029 parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '"');
1030 cursor_index++;
1031 cursor_char_position = 0;
1032 }
1033 }
Chris Lattner24943d22010-06-08 16:52:24 +00001034
1035 int num_command_matches;
1036
1037 matches.Clear();
1038
1039 // Only max_return_elements == -1 is supported at present:
1040 assert (max_return_elements == -1);
Jim Ingham802f8b02010-06-30 05:02:46 +00001041 bool word_complete;
Greg Clayton63094e02010-06-23 01:19:29 +00001042 num_command_matches = HandleCompletionMatches (parsed_line,
1043 cursor_index,
1044 cursor_char_position,
1045 match_start_point,
Jim Ingham802f8b02010-06-30 05:02:46 +00001046 max_return_elements,
1047 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +00001048 matches);
Chris Lattner24943d22010-06-08 16:52:24 +00001049
1050 if (num_command_matches <= 0)
1051 return num_command_matches;
1052
1053 if (num_args == 0)
1054 {
1055 // If we got an empty string, insert nothing.
1056 matches.InsertStringAtIndex(0, "");
1057 }
1058 else
1059 {
1060 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
1061 // put an empty string in element 0.
1062 std::string command_partial_str;
1063 if (cursor_index >= 0)
Jim Inghame3663e82010-10-22 18:47:16 +00001064 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index),
1065 parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
Chris Lattner24943d22010-06-08 16:52:24 +00001066
1067 std::string common_prefix;
1068 matches.LongestCommonPrefix (common_prefix);
1069 int partial_name_len = command_partial_str.size();
1070
1071 // If we matched a unique single command, add a space...
Jim Ingham802f8b02010-06-30 05:02:46 +00001072 // Only do this if the completer told us this was a complete word, however...
1073 if (num_command_matches == 1 && word_complete)
Chris Lattner24943d22010-06-08 16:52:24 +00001074 {
1075 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
1076 if (quote_char != '\0')
1077 common_prefix.push_back(quote_char);
1078
1079 common_prefix.push_back(' ');
1080 }
1081 common_prefix.erase (0, partial_name_len);
1082 matches.InsertStringAtIndex(0, common_prefix.c_str());
1083 }
1084 return num_command_matches;
1085}
1086
Chris Lattner24943d22010-06-08 16:52:24 +00001087
1088CommandInterpreter::~CommandInterpreter ()
1089{
1090}
1091
1092const char *
1093CommandInterpreter::GetPrompt ()
1094{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001095 return m_debugger.GetPrompt();
Chris Lattner24943d22010-06-08 16:52:24 +00001096}
1097
1098void
1099CommandInterpreter::SetPrompt (const char *new_prompt)
1100{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001101 m_debugger.SetPrompt (new_prompt);
Chris Lattner24943d22010-06-08 16:52:24 +00001102}
1103
Jim Ingham5e16ef52010-10-04 19:49:29 +00001104size_t
Greg Clayton58928562011-02-09 01:08:52 +00001105CommandInterpreter::GetConfirmationInputReaderCallback
1106(
1107 void *baton,
1108 InputReader &reader,
1109 lldb::InputReaderAction action,
1110 const char *bytes,
1111 size_t bytes_len
1112)
Jim Ingham5e16ef52010-10-04 19:49:29 +00001113{
Greg Clayton58928562011-02-09 01:08:52 +00001114 File &out_file = reader.GetDebugger().GetOutputFile();
Jim Ingham5e16ef52010-10-04 19:49:29 +00001115 bool *response_ptr = (bool *) baton;
1116
1117 switch (action)
1118 {
1119 case eInputReaderActivate:
Greg Clayton58928562011-02-09 01:08:52 +00001120 if (out_file.IsValid())
Jim Ingham5e16ef52010-10-04 19:49:29 +00001121 {
1122 if (reader.GetPrompt())
Caroline Tice22a60092011-02-02 01:17:56 +00001123 {
Greg Clayton58928562011-02-09 01:08:52 +00001124 out_file.Printf ("%s", reader.GetPrompt());
1125 out_file.Flush ();
Caroline Tice22a60092011-02-02 01:17:56 +00001126 }
Jim Ingham5e16ef52010-10-04 19:49:29 +00001127 }
1128 break;
1129
1130 case eInputReaderDeactivate:
1131 break;
1132
1133 case eInputReaderReactivate:
Greg Clayton58928562011-02-09 01:08:52 +00001134 if (out_file.IsValid() && reader.GetPrompt())
Caroline Tice22a60092011-02-02 01:17:56 +00001135 {
Greg Clayton58928562011-02-09 01:08:52 +00001136 out_file.Printf ("%s", reader.GetPrompt());
1137 out_file.Flush ();
Caroline Tice22a60092011-02-02 01:17:56 +00001138 }
Jim Ingham5e16ef52010-10-04 19:49:29 +00001139 break;
1140
1141 case eInputReaderGotToken:
1142 if (bytes_len == 0)
1143 {
1144 reader.SetIsDone(true);
1145 }
1146 else if (bytes[0] == 'y')
1147 {
1148 *response_ptr = true;
1149 reader.SetIsDone(true);
1150 }
1151 else if (bytes[0] == 'n')
1152 {
1153 *response_ptr = false;
1154 reader.SetIsDone(true);
1155 }
1156 else
1157 {
Greg Clayton58928562011-02-09 01:08:52 +00001158 if (out_file.IsValid() && !reader.IsDone() && reader.GetPrompt())
Jim Ingham5e16ef52010-10-04 19:49:29 +00001159 {
Greg Clayton58928562011-02-09 01:08:52 +00001160 out_file.Printf ("Please answer \"y\" or \"n\"\n%s", reader.GetPrompt());
1161 out_file.Flush ();
Jim Ingham5e16ef52010-10-04 19:49:29 +00001162 }
1163 }
1164 break;
1165
Caroline Ticec4f55fe2010-11-19 20:47:54 +00001166 case eInputReaderInterrupt:
1167 case eInputReaderEndOfFile:
1168 *response_ptr = false; // Assume ^C or ^D means cancel the proposed action
1169 reader.SetIsDone (true);
1170 break;
1171
Jim Ingham5e16ef52010-10-04 19:49:29 +00001172 case eInputReaderDone:
1173 break;
1174 }
1175
1176 return bytes_len;
1177
1178}
1179
1180bool
1181CommandInterpreter::Confirm (const char *message, bool default_answer)
1182{
Jim Ingham93057472010-10-04 22:44:14 +00001183 // Check AutoConfirm first:
1184 if (m_debugger.GetAutoConfirm())
1185 return default_answer;
1186
Jim Ingham5e16ef52010-10-04 19:49:29 +00001187 InputReaderSP reader_sp (new InputReader(GetDebugger()));
1188 bool response = default_answer;
1189 if (reader_sp)
1190 {
1191 std::string prompt(message);
1192 prompt.append(": [");
1193 if (default_answer)
1194 prompt.append ("Y/n] ");
1195 else
1196 prompt.append ("y/N] ");
1197
1198 Error err (reader_sp->Initialize (CommandInterpreter::GetConfirmationInputReaderCallback,
1199 &response, // baton
1200 eInputReaderGranularityLine, // token size, to pass to callback function
1201 NULL, // end token
1202 prompt.c_str(), // prompt
1203 true)); // echo input
1204 if (err.Success())
1205 {
1206 GetDebugger().PushInputReader (reader_sp);
1207 }
1208 reader_sp->WaitOnReaderIsDone();
1209 }
1210 return response;
1211}
1212
1213
Chris Lattner24943d22010-06-08 16:52:24 +00001214void
1215CommandInterpreter::CrossRegisterCommand (const char * dest_cmd, const char * object_type)
1216{
Jim Inghamd40f8a62010-07-06 22:46:59 +00001217 CommandObjectSP cmd_obj_sp = GetCommandSPExact (dest_cmd, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001218
1219 if (cmd_obj_sp != NULL)
1220 {
1221 CommandObject *cmd_obj = cmd_obj_sp.get();
1222 if (cmd_obj->IsCrossRefObject ())
1223 cmd_obj->AddObject (object_type);
1224 }
1225}
1226
Chris Lattner24943d22010-06-08 16:52:24 +00001227OptionArgVectorSP
1228CommandInterpreter::GetAliasOptions (const char *alias_name)
1229{
1230 OptionArgMap::iterator pos;
1231 OptionArgVectorSP ret_val;
1232
1233 std::string alias (alias_name);
1234
1235 if (HasAliasOptions())
1236 {
1237 pos = m_alias_options.find (alias);
1238 if (pos != m_alias_options.end())
1239 ret_val = pos->second;
1240 }
1241
1242 return ret_val;
1243}
1244
1245void
1246CommandInterpreter::RemoveAliasOptions (const char *alias_name)
1247{
1248 OptionArgMap::iterator pos = m_alias_options.find(alias_name);
1249 if (pos != m_alias_options.end())
1250 {
1251 m_alias_options.erase (pos);
1252 }
1253}
1254
1255void
1256CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp)
1257{
1258 m_alias_options[alias_name] = option_arg_vector_sp;
1259}
1260
1261bool
1262CommandInterpreter::HasCommands ()
1263{
1264 return (!m_command_dict.empty());
1265}
1266
1267bool
1268CommandInterpreter::HasAliases ()
1269{
1270 return (!m_alias_dict.empty());
1271}
1272
1273bool
1274CommandInterpreter::HasUserCommands ()
1275{
1276 return (!m_user_dict.empty());
1277}
1278
1279bool
1280CommandInterpreter::HasAliasOptions ()
1281{
1282 return (!m_alias_options.empty());
1283}
1284
Chris Lattner24943d22010-06-08 16:52:24 +00001285void
Caroline Ticebd5c63e2010-10-12 21:57:09 +00001286CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
1287 const char *alias_name,
1288 Args &cmd_args,
Caroline Tice44c841d2010-12-07 19:58:26 +00001289 std::string &raw_input_string,
Caroline Ticebd5c63e2010-10-12 21:57:09 +00001290 CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +00001291{
1292 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
Caroline Tice44c841d2010-12-07 19:58:26 +00001293
1294 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
Chris Lattner24943d22010-06-08 16:52:24 +00001295
Caroline Tice44c841d2010-12-07 19:58:26 +00001296 // Make sure that the alias name is the 0th element in cmd_args
1297 std::string alias_name_str = alias_name;
1298 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
1299 cmd_args.Unshift (alias_name);
1300
1301 Args new_args (alias_cmd_obj->GetCommandName());
1302 if (new_args.GetArgumentCount() == 2)
1303 new_args.Shift();
1304
Chris Lattner24943d22010-06-08 16:52:24 +00001305 if (option_arg_vector_sp.get())
1306 {
Caroline Tice44c841d2010-12-07 19:58:26 +00001307 if (wants_raw_input)
1308 {
1309 // We have a command that both has command options and takes raw input. Make *sure* it has a
1310 // " -- " in the right place in the raw_input_string.
1311 size_t pos = raw_input_string.find(" -- ");
1312 if (pos == std::string::npos)
1313 {
1314 // None found; assume it goes at the beginning of the raw input string
1315 raw_input_string.insert (0, " -- ");
1316 }
1317 }
Chris Lattner24943d22010-06-08 16:52:24 +00001318
1319 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1320 int old_size = cmd_args.GetArgumentCount();
Caroline Ticebd5c63e2010-10-12 21:57:09 +00001321 std::vector<bool> used (old_size + 1, false);
1322
1323 used[0] = true;
Chris Lattner24943d22010-06-08 16:52:24 +00001324
1325 for (int i = 0; i < option_arg_vector->size(); ++i)
1326 {
1327 OptionArgPair option_pair = (*option_arg_vector)[i];
Caroline Tice44c841d2010-12-07 19:58:26 +00001328 OptionArgValue value_pair = option_pair.second;
1329 int value_type = value_pair.first;
Chris Lattner24943d22010-06-08 16:52:24 +00001330 std::string option = option_pair.first;
Caroline Tice44c841d2010-12-07 19:58:26 +00001331 std::string value = value_pair.second;
Chris Lattner24943d22010-06-08 16:52:24 +00001332 if (option.compare ("<argument>") == 0)
Caroline Tice44c841d2010-12-07 19:58:26 +00001333 {
1334 if (!wants_raw_input
1335 || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice
1336 new_args.AppendArgument (value.c_str());
1337 }
Chris Lattner24943d22010-06-08 16:52:24 +00001338 else
1339 {
Caroline Tice44c841d2010-12-07 19:58:26 +00001340 if (value_type != optional_argument)
1341 new_args.AppendArgument (option.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +00001342 if (value.compare ("<no-argument>") != 0)
1343 {
1344 int index = GetOptionArgumentPosition (value.c_str());
1345 if (index == 0)
Caroline Tice44c841d2010-12-07 19:58:26 +00001346 {
Chris Lattner24943d22010-06-08 16:52:24 +00001347 // value was NOT a positional argument; must be a real value
Caroline Tice44c841d2010-12-07 19:58:26 +00001348 if (value_type != optional_argument)
1349 new_args.AppendArgument (value.c_str());
1350 else
1351 {
1352 char buffer[255];
1353 ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str());
1354 new_args.AppendArgument (buffer);
1355 }
1356
1357 }
Chris Lattner24943d22010-06-08 16:52:24 +00001358 else if (index >= cmd_args.GetArgumentCount())
1359 {
1360 result.AppendErrorWithFormat
1361 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
1362 index);
1363 result.SetStatus (eReturnStatusFailed);
1364 return;
1365 }
1366 else
1367 {
Caroline Tice44c841d2010-12-07 19:58:26 +00001368 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
1369 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
1370 if (strpos != std::string::npos)
1371 {
1372 raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index)));
1373 }
1374
1375 if (value_type != optional_argument)
1376 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
1377 else
1378 {
1379 char buffer[255];
1380 ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(),
1381 cmd_args.GetArgumentAtIndex (index));
1382 new_args.AppendArgument (buffer);
1383 }
Caroline Ticebd5c63e2010-10-12 21:57:09 +00001384 used[index] = true;
Chris Lattner24943d22010-06-08 16:52:24 +00001385 }
1386 }
1387 }
1388 }
1389
1390 for (int j = 0; j < cmd_args.GetArgumentCount(); ++j)
1391 {
Caroline Tice44c841d2010-12-07 19:58:26 +00001392 if (!used[j] && !wants_raw_input)
Chris Lattner24943d22010-06-08 16:52:24 +00001393 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
1394 }
1395
1396 cmd_args.Clear();
1397 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
1398 }
1399 else
1400 {
1401 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Caroline Tice44c841d2010-12-07 19:58:26 +00001402 // This alias was not created with any options; nothing further needs to be done, unless it is a command that
1403 // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw
1404 // input string.
1405 if (wants_raw_input)
1406 {
1407 cmd_args.Clear();
1408 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
1409 }
Chris Lattner24943d22010-06-08 16:52:24 +00001410 return;
1411 }
1412
1413 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1414 return;
1415}
1416
1417
1418int
1419CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
1420{
1421 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
1422 // of zero.
1423
1424 char *cptr = (char *) in_string;
1425
1426 // Does it start with '%'
1427 if (cptr[0] == '%')
1428 {
1429 ++cptr;
1430
1431 // Is the rest of it entirely digits?
1432 if (isdigit (cptr[0]))
1433 {
1434 const char *start = cptr;
1435 while (isdigit (cptr[0]))
1436 ++cptr;
1437
1438 // We've gotten to the end of the digits; are we at the end of the string?
1439 if (cptr[0] == '\0')
1440 position = atoi (start);
1441 }
1442 }
1443
1444 return position;
1445}
1446
1447void
1448CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
1449{
Greg Clayton887aa282010-10-11 01:05:37 +00001450 // Don't parse any .lldbinit files if we were asked not to
1451 if (m_skip_lldbinit_files)
1452 return;
1453
Chris Lattner24943d22010-06-08 16:52:24 +00001454 const char *init_file_path = in_cwd ? "./.lldbinit" : "~/.lldbinit";
Greg Clayton537a7a82010-10-20 20:54:39 +00001455 FileSpec init_file (init_file_path, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001456 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
1457 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
1458
1459 if (init_file.Exists())
1460 {
1461 char path[PATH_MAX];
1462 init_file.GetPath(path, sizeof(path));
1463 StreamString source_command;
Johnny Chen7c984242010-07-28 21:16:11 +00001464 source_command.Printf ("command source '%s'", path);
Chris Lattner24943d22010-06-08 16:52:24 +00001465 HandleCommand (source_command.GetData(), false, result);
1466 }
1467 else
1468 {
1469 // nothing to be done if the file doesn't exist
1470 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1471 }
1472}
1473
1474ScriptInterpreter *
1475CommandInterpreter::GetScriptInterpreter ()
1476{
Caroline Tice0aa2e552011-01-14 00:29:16 +00001477 if (m_script_interpreter_ap.get() != NULL)
1478 return m_script_interpreter_ap.get();
Greg Clayton63094e02010-06-23 01:19:29 +00001479
Caroline Tice0aa2e552011-01-14 00:29:16 +00001480 lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage();
1481 switch (script_lang)
Chris Lattner24943d22010-06-08 16:52:24 +00001482 {
Caroline Tice0aa2e552011-01-14 00:29:16 +00001483 case eScriptLanguageNone:
1484 m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this));
1485 break;
1486 case eScriptLanguagePython:
1487 m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this));
1488 break;
1489 default:
1490 break;
1491 };
1492
1493 return m_script_interpreter_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +00001494}
1495
1496
1497
1498bool
1499CommandInterpreter::GetSynchronous ()
1500{
1501 return m_synchronous_execution;
1502}
1503
1504void
1505CommandInterpreter::SetSynchronous (bool value)
1506{
Johnny Chend7a4eb02010-10-14 01:22:03 +00001507 m_synchronous_execution = value;
Chris Lattner24943d22010-06-08 16:52:24 +00001508}
1509
1510void
1511CommandInterpreter::OutputFormattedHelpText (Stream &strm,
1512 const char *word_text,
1513 const char *separator,
1514 const char *help_text,
1515 uint32_t max_word_len)
1516{
Greg Clayton238c0a12010-09-18 01:14:36 +00001517 const uint32_t max_columns = m_debugger.GetTerminalWidth();
1518
Chris Lattner24943d22010-06-08 16:52:24 +00001519 int indent_size = max_word_len + strlen (separator) + 2;
1520
1521 strm.IndentMore (indent_size);
1522
1523 int len = indent_size + strlen (help_text) + 1;
1524 char *text = (char *) malloc (len);
1525 sprintf (text, "%-*s %s %s", max_word_len, word_text, separator, help_text);
1526 if (text[len - 1] == '\n')
1527 text[--len] = '\0';
1528
1529 if (len < max_columns)
1530 {
1531 // Output it as a single line.
1532 strm.Printf ("%s", text);
1533 }
1534 else
1535 {
1536 // We need to break it up into multiple lines.
1537 bool first_line = true;
1538 int text_width;
1539 int start = 0;
1540 int end = start;
1541 int final_end = strlen (text);
1542 int sub_len;
1543
1544 while (end < final_end)
1545 {
1546 if (first_line)
1547 text_width = max_columns - 1;
1548 else
1549 text_width = max_columns - indent_size - 1;
1550
1551 // Don't start the 'text' on a space, since we're already outputting the indentation.
1552 if (!first_line)
1553 {
1554 while ((start < final_end) && (text[start] == ' '))
1555 start++;
1556 }
1557
1558 end = start + text_width;
1559 if (end > final_end)
1560 end = final_end;
1561 else
1562 {
1563 // If we're not at the end of the text, make sure we break the line on white space.
1564 while (end > start
1565 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
1566 end--;
1567 }
1568
1569 sub_len = end - start;
1570 if (start != 0)
1571 strm.EOL();
1572 if (!first_line)
1573 strm.Indent();
1574 else
1575 first_line = false;
1576 assert (start <= final_end);
1577 assert (start + sub_len <= final_end);
1578 if (sub_len > 0)
1579 strm.Write (text + start, sub_len);
1580 start = end + 1;
1581 }
1582 }
1583 strm.EOL();
1584 strm.IndentLess(indent_size);
1585 free (text);
1586}
1587
1588void
1589CommandInterpreter::AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word,
1590 StringList &commands_found, StringList &commands_help)
1591{
1592 CommandObject::CommandMap::const_iterator pos;
1593 CommandObject::CommandMap sub_cmd_dict = ((CommandObjectMultiword *) cmd_obj)->m_subcommand_dict;
1594 CommandObject *sub_cmd_obj;
1595
1596 for (pos = sub_cmd_dict.begin(); pos != sub_cmd_dict.end(); ++pos)
1597 {
1598 const char * command_name = pos->first.c_str();
1599 sub_cmd_obj = pos->second.get();
1600 StreamString complete_command_name;
1601
1602 complete_command_name.Printf ("%s %s", prefix, command_name);
1603
Greg Clayton238c0a12010-09-18 01:14:36 +00001604 if (sub_cmd_obj->HelpTextContainsWord (search_word))
Chris Lattner24943d22010-06-08 16:52:24 +00001605 {
1606 commands_found.AppendString (complete_command_name.GetData());
1607 commands_help.AppendString (sub_cmd_obj->GetHelp());
1608 }
1609
1610 if (sub_cmd_obj->IsMultiwordObject())
1611 AproposAllSubCommands (sub_cmd_obj, complete_command_name.GetData(), search_word, commands_found,
1612 commands_help);
1613 }
1614
1615}
1616
1617void
1618CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
1619 StringList &commands_help)
1620{
1621 CommandObject::CommandMap::const_iterator pos;
1622
1623 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1624 {
1625 const char *command_name = pos->first.c_str();
1626 CommandObject *cmd_obj = pos->second.get();
1627
Greg Clayton238c0a12010-09-18 01:14:36 +00001628 if (cmd_obj->HelpTextContainsWord (search_word))
Chris Lattner24943d22010-06-08 16:52:24 +00001629 {
1630 commands_found.AppendString (command_name);
1631 commands_help.AppendString (cmd_obj->GetHelp());
1632 }
1633
1634 if (cmd_obj->IsMultiwordObject())
1635 AproposAllSubCommands (cmd_obj, command_name, search_word, commands_found, commands_help);
1636
1637 }
1638}