blob: 0da474b5bad023c591e93e2815e8ac5667d07add [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>
11
12#include <getopt.h>
13#include <stdlib.h>
14
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000015#include "../Commands/CommandObjectApropos.h"
16#include "../Commands/CommandObjectArgs.h"
17#include "../Commands/CommandObjectBreakpoint.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000018//#include "../Commands/CommandObjectCall.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000019#include "../Commands/CommandObjectDisassemble.h"
20#include "../Commands/CommandObjectExpression.h"
21#include "../Commands/CommandObjectFile.h"
22#include "../Commands/CommandObjectFrame.h"
23#include "../Commands/CommandObjectHelp.h"
24#include "../Commands/CommandObjectImage.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000025#include "../Commands/CommandObjectLog.h"
26#include "../Commands/CommandObjectMemory.h"
27#include "../Commands/CommandObjectProcess.h"
28#include "../Commands/CommandObjectQuit.h"
Eli Friedmanb34d2a22010-06-09 22:08:29 +000029#include "lldb/Interpreter/CommandObjectRegexCommand.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000030#include "../Commands/CommandObjectRegister.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "CommandObjectScript.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000032#include "../Commands/CommandObjectSettings.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000033#include "../Commands/CommandObjectSource.h"
Jim Ingham767af882010-07-07 03:36:20 +000034#include "../Commands/CommandObjectCommands.h"
Eli Friedmanccdb9ec2010-06-13 02:17:17 +000035#include "../Commands/CommandObjectSyntax.h"
36#include "../Commands/CommandObjectTarget.h"
37#include "../Commands/CommandObjectThread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038
Jim Ingham84cdc152010-06-15 19:49:27 +000039#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000040#include "lldb/Core/Debugger.h"
41#include "lldb/Core/Stream.h"
42#include "lldb/Core/Timer.h"
43#include "lldb/Target/Process.h"
44#include "lldb/Target/Thread.h"
45#include "lldb/Target/TargetList.h"
46
47#include "lldb/Interpreter/CommandReturnObject.h"
48#include "lldb/Interpreter/CommandInterpreter.h"
49
50using namespace lldb;
51using namespace lldb_private;
52
53CommandInterpreter::CommandInterpreter
54(
Greg Clayton63094e02010-06-23 01:19:29 +000055 Debugger &debugger,
Chris Lattner24943d22010-06-08 16:52:24 +000056 ScriptLanguage script_language,
Greg Clayton63094e02010-06-23 01:19:29 +000057 bool synchronous_execution
Chris Lattner24943d22010-06-08 16:52:24 +000058) :
59 Broadcaster ("CommandInterpreter"),
Greg Clayton63094e02010-06-23 01:19:29 +000060 m_debugger (debugger),
Greg Clayton63094e02010-06-23 01:19:29 +000061 m_synchronous_execution (synchronous_execution)
Chris Lattner24943d22010-06-08 16:52:24 +000062{
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000063 const char *dbg_name = debugger.GetInstanceName().AsCString();
64 std::string lang_name = ScriptInterpreter::LanguageToString (script_language);
65 StreamString var_name;
66 var_name.Printf ("[%s].script-lang", dbg_name);
Caroline Tice1d2aefd2010-09-09 06:25:08 +000067 debugger.GetSettingsController()->SetVariable (var_name.GetData(), lang_name.c_str(),
68 lldb::eVarSetOperationAssign, false,
69 m_debugger.GetInstanceName().AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +000070}
71
72void
73CommandInterpreter::Initialize ()
74{
75 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
76
77 CommandReturnObject result;
78
79 LoadCommandDictionary ();
80
Chris Lattner24943d22010-06-08 16:52:24 +000081 // Set up some initial aliases.
Jim Ingham767af882010-07-07 03:36:20 +000082 result.Clear(); HandleCommand ("command alias q quit", false, result);
83 result.Clear(); HandleCommand ("command alias run process launch", false, result);
84 result.Clear(); HandleCommand ("command alias r process launch", false, result);
85 result.Clear(); HandleCommand ("command alias c process continue", false, result);
86 result.Clear(); HandleCommand ("command alias continue process continue", false, result);
87 result.Clear(); HandleCommand ("command alias expr expression", false, result);
88 result.Clear(); HandleCommand ("command alias exit quit", false, result);
89 result.Clear(); HandleCommand ("command alias b breakpoint", false, result);
90 result.Clear(); HandleCommand ("command alias bt thread backtrace", false, result);
91 result.Clear(); HandleCommand ("command alias si thread step-inst", false, result);
92 result.Clear(); HandleCommand ("command alias step thread step-in", false, result);
93 result.Clear(); HandleCommand ("command alias s thread step-in", false, result);
94 result.Clear(); HandleCommand ("command alias next thread step-over", false, result);
95 result.Clear(); HandleCommand ("command alias n thread step-over", false, result);
96 result.Clear(); HandleCommand ("command alias finish thread step-out", false, result);
97 result.Clear(); HandleCommand ("command alias x memory read", false, result);
98 result.Clear(); HandleCommand ("command alias l source list", false, result);
99 result.Clear(); HandleCommand ("command alias list source list", false, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000100}
101
Chris Lattner24943d22010-06-08 16:52:24 +0000102const char *
103CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg)
104{
105 // This function has not yet been implemented.
106
107 // Look for any embedded script command
108 // If found,
109 // get interpreter object from the command dictionary,
110 // call execute_one_command on it,
111 // get the results as a string,
112 // substitute that string for current stuff.
113
114 return arg;
115}
116
117
118void
119CommandInterpreter::LoadCommandDictionary ()
120{
121 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
122
123 // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT ***
124 //
125 // Command objects that are used as cross reference objects (i.e. they inherit from CommandObjectCrossref)
126 // *MUST* be created and put into the command dictionary *BEFORE* any multi-word commands (which may use
127 // the cross-referencing stuff) are created!!!
128 //
129 // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT ***
130
131
132 // Command objects that inherit from CommandObjectCrossref must be created before other command objects
133 // are created. This is so that when another command is created that needs to go into a crossref object,
134 // the crossref object exists and is ready to take the cross reference. Put the cross referencing command
135 // objects into the CommandDictionary now, so they are ready for use when the other commands get created.
136
Chris Lattner24943d22010-06-08 16:52:24 +0000137 // Non-CommandObjectCrossref commands can now be created.
138
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000139 lldb::ScriptLanguage script_language;
140 lldb::SettableVariableType var_type = lldb::eSetVarTypeString;
141 StringList value;
142 const char *dbg_name = GetDebugger().GetInstanceName().AsCString();
143 StreamString var_name;
144 var_name.Printf ("[%s].script-lang", dbg_name);
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000145 value = Debugger::GetSettingsController()->GetVariable (var_name.GetData(), var_type,
146 m_debugger.GetInstanceName().AsCString());
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000147 bool success;
148 script_language = Args::StringToScriptLanguage (value.GetStringAtIndex(0), lldb::eScriptLanguageDefault, &success);
149
Chris Lattner24943d22010-06-08 16:52:24 +0000150 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos ());
Greg Clayton63094e02010-06-23 01:19:29 +0000151 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
Sean Callanan65dafa82010-08-27 01:01:44 +0000152 //m_command_dict["call"] = CommandObjectSP (new CommandObjectCall ());
Jim Ingham767af882010-07-07 03:36:20 +0000153 m_command_dict["commands"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000154 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble ());
155 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression ());
156 m_command_dict["file"] = CommandObjectSP (new CommandObjectFile ());
Greg Clayton63094e02010-06-23 01:19:29 +0000157 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000158 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp ());
Greg Clayton63094e02010-06-23 01:19:29 +0000159 m_command_dict["image"] = CommandObjectSP (new CommandObjectImage (*this));
160 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
161 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
162 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000163 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit ());
Greg Clayton63094e02010-06-23 01:19:29 +0000164 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000165 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (script_language));
166 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
Jim Ingham767af882010-07-07 03:36:20 +0000167 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000168 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
169 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000170
171 std::auto_ptr<CommandObjectRegexCommand>
172 break_regex_cmd_ap(new CommandObjectRegexCommand ("regexp-break",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000173 "Set a breakpoint using a regular expression to specify the location.",
Chris Lattner24943d22010-06-08 16:52:24 +0000174 "regexp-break [<file>:<line>]\nregexp-break [<address>]\nregexp-break <...>", 2));
175 if (break_regex_cmd_ap.get())
176 {
177 if (break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2") &&
178 break_regex_cmd_ap->AddRegexCommand("^(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1") &&
179 break_regex_cmd_ap->AddRegexCommand("^[\"']?([-+]\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'") &&
180 break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list") &&
181 break_regex_cmd_ap->AddRegexCommand("^(-.*)$", "breakpoint set %1") &&
182 break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1'"))
183 {
184 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
185 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
186 }
187 }
188}
189
190int
191CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
192 StringList &matches)
193{
194 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
195
196 if (include_aliases)
197 {
198 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
199 }
200
201 return matches.GetSize();
202}
203
204CommandObjectSP
205CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
206{
207 CommandObject::CommandMap::iterator pos;
208 CommandObjectSP ret_val;
209
210 std::string cmd(cmd_cstr);
211
212 if (HasCommands())
213 {
214 pos = m_command_dict.find(cmd);
215 if (pos != m_command_dict.end())
216 ret_val = pos->second;
217 }
218
219 if (include_aliases && HasAliases())
220 {
221 pos = m_alias_dict.find(cmd);
222 if (pos != m_alias_dict.end())
223 ret_val = pos->second;
224 }
225
226 if (HasUserCommands())
227 {
228 pos = m_user_dict.find(cmd);
229 if (pos != m_user_dict.end())
230 ret_val = pos->second;
231 }
232
233 if (!exact && ret_val == NULL)
234 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000235 // We will only get into here if we didn't find any exact matches.
236
237 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
238
Chris Lattner24943d22010-06-08 16:52:24 +0000239 StringList local_matches;
240 if (matches == NULL)
241 matches = &local_matches;
242
Jim Inghamd40f8a62010-07-06 22:46:59 +0000243 unsigned int num_cmd_matches = 0;
244 unsigned int num_alias_matches = 0;
245 unsigned int num_user_matches = 0;
246
247 // Look through the command dictionaries one by one, and if we get only one match from any of
248 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
249
Chris Lattner24943d22010-06-08 16:52:24 +0000250 if (HasCommands())
251 {
252 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
253 }
254
255 if (num_cmd_matches == 1)
256 {
257 cmd.assign(matches->GetStringAtIndex(0));
258 pos = m_command_dict.find(cmd);
259 if (pos != m_command_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000260 real_match_sp = pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000261 }
262
Jim Ingham9a574172010-06-24 20:28:42 +0000263 if (include_aliases && HasAliases())
Chris Lattner24943d22010-06-08 16:52:24 +0000264 {
265 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
266
267 }
268
Jim Inghamd40f8a62010-07-06 22:46:59 +0000269 if (num_alias_matches == 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000270 {
271 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
272 pos = m_alias_dict.find(cmd);
273 if (pos != m_alias_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000274 alias_match_sp = pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000275 }
276
Jim Ingham9a574172010-06-24 20:28:42 +0000277 if (HasUserCommands())
Chris Lattner24943d22010-06-08 16:52:24 +0000278 {
279 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
280 }
281
Jim Inghamd40f8a62010-07-06 22:46:59 +0000282 if (num_user_matches == 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000283 {
284 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
285
286 pos = m_user_dict.find (cmd);
287 if (pos != m_user_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000288 user_match_sp = pos->second;
289 }
290
291 // If we got exactly one match, return that, otherwise return the match list.
292
293 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
294 {
295 if (num_cmd_matches)
296 return real_match_sp;
297 else if (num_alias_matches)
298 return alias_match_sp;
299 else
300 return user_match_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000301 }
302 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000303 else if (matches && ret_val != NULL)
304 {
305 matches->AppendString (cmd_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +0000306 }
307
308
309 return ret_val;
310}
311
Jim Inghamd40f8a62010-07-06 22:46:59 +0000312CommandObjectSP
313CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
Chris Lattner24943d22010-06-08 16:52:24 +0000314{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000315 return GetCommandSP(cmd_cstr, include_aliases, true, NULL);
316}
317
318CommandObject *
319CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
320{
321 return GetCommandSPExact (cmd_cstr, include_aliases).get();
322}
323
324CommandObject *
325CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
326{
327 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
328
329 // If we didn't find an exact match to the command string in the commands, look in
330 // the aliases.
331
332 if (command_obj == NULL)
333 {
334 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
335 }
336
337 // Finally, if there wasn't an exact match among the aliases, look for an inexact match
338 // in both the commands and the aliases.
339
340 if (command_obj == NULL)
341 command_obj = GetCommandSP(cmd_cstr, true, false, matches).get();
342
343 return command_obj;
Chris Lattner24943d22010-06-08 16:52:24 +0000344}
345
346bool
347CommandInterpreter::CommandExists (const char *cmd)
348{
349 return m_command_dict.find(cmd) != m_command_dict.end();
350}
351
352bool
353CommandInterpreter::AliasExists (const char *cmd)
354{
355 return m_alias_dict.find(cmd) != m_alias_dict.end();
356}
357
358bool
359CommandInterpreter::UserCommandExists (const char *cmd)
360{
361 return m_user_dict.find(cmd) != m_user_dict.end();
362}
363
364void
365CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
366{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000367 command_obj_sp->SetIsAlias (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000368 m_alias_dict[alias_name] = command_obj_sp;
369}
370
371bool
372CommandInterpreter::RemoveAlias (const char *alias_name)
373{
374 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
375 if (pos != m_alias_dict.end())
376 {
377 m_alias_dict.erase(pos);
378 return true;
379 }
380 return false;
381}
382bool
383CommandInterpreter::RemoveUser (const char *alias_name)
384{
385 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
386 if (pos != m_user_dict.end())
387 {
388 m_user_dict.erase(pos);
389 return true;
390 }
391 return false;
392}
393
Chris Lattner24943d22010-06-08 16:52:24 +0000394void
395CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
396{
397 help_string.Printf ("'%s", command_name);
398 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
399
400 if (option_arg_vector_sp != NULL)
401 {
402 OptionArgVector *options = option_arg_vector_sp.get();
403 for (int i = 0; i < options->size(); ++i)
404 {
405 OptionArgPair cur_option = (*options)[i];
406 std::string opt = cur_option.first;
407 std::string value = cur_option.second;
408 if (opt.compare("<argument>") == 0)
409 {
410 help_string.Printf (" %s", value.c_str());
411 }
412 else
413 {
414 help_string.Printf (" %s", opt.c_str());
415 if ((value.compare ("<no-argument>") != 0)
416 && (value.compare ("<need-argument") != 0))
417 {
418 help_string.Printf (" %s", value.c_str());
419 }
420 }
421 }
422 }
423
424 help_string.Printf ("'");
425}
426
Greg Clayton65124ea2010-08-26 22:05:43 +0000427size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000428CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
429{
430 CommandObject::CommandMap::const_iterator pos;
Greg Clayton65124ea2010-08-26 22:05:43 +0000431 CommandObject::CommandMap::const_iterator end = dict.end();
432 size_t max_len = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000433
Greg Clayton65124ea2010-08-26 22:05:43 +0000434 for (pos = dict.begin(); pos != end; ++pos)
435 {
436 size_t len = pos->first.size();
437 if (max_len < len)
438 max_len = len;
Chris Lattner24943d22010-06-08 16:52:24 +0000439 }
Greg Clayton65124ea2010-08-26 22:05:43 +0000440 return max_len;
Chris Lattner24943d22010-06-08 16:52:24 +0000441}
442
443void
444CommandInterpreter::GetHelp (CommandReturnObject &result)
445{
446 CommandObject::CommandMap::const_iterator pos;
447 result.AppendMessage("The following is a list of built-in, permanent debugger commands:");
448 result.AppendMessage("");
Greg Clayton65124ea2010-08-26 22:05:43 +0000449 uint32_t max_len = FindLongestCommandWord (m_command_dict);
Chris Lattner24943d22010-06-08 16:52:24 +0000450
451 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
452 {
453 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
454 max_len);
455 }
456 result.AppendMessage("");
457
458 if (m_alias_dict.size() > 0)
459 {
Sean Callanana3aff732010-08-09 18:50:15 +0000460 result.AppendMessage("The following is a list of your current command abbreviations (see 'commands alias' for more info):");
Chris Lattner24943d22010-06-08 16:52:24 +0000461 result.AppendMessage("");
Greg Clayton65124ea2010-08-26 22:05:43 +0000462 max_len = FindLongestCommandWord (m_alias_dict);
463
Chris Lattner24943d22010-06-08 16:52:24 +0000464 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
465 {
466 StreamString sstr;
467 StreamString translation_and_help;
468 std::string entry_name = pos->first;
469 std::string second_entry = pos->second.get()->GetCommandName();
470 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
471
472 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
473 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
474 translation_and_help.GetData(), max_len);
475 }
476 result.AppendMessage("");
477 }
478
479 if (m_user_dict.size() > 0)
480 {
481 result.AppendMessage ("The following is a list of your current user-defined commands:");
482 result.AppendMessage("");
483 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
484 {
485 result.AppendMessageWithFormat ("%s -- %s\n", pos->first.c_str(), pos->second->GetHelp());
486 }
487 result.AppendMessage("");
488 }
489
490 result.AppendMessage("For more information on any particular command, try 'help <command-name>'.");
491}
492
Chris Lattner24943d22010-06-08 16:52:24 +0000493bool
Greg Clayton63094e02010-06-23 01:19:29 +0000494CommandInterpreter::HandleCommand
495(
496 const char *command_line,
497 bool add_to_history,
498 CommandReturnObject &result,
499 ExecutionContext *override_context
500)
Chris Lattner24943d22010-06-08 16:52:24 +0000501{
502 // FIXME: there should probably be a mutex to make sure only one thread can
503 // run the interpreter at a time.
504
505 // TODO: this should be a logging channel in lldb.
506// if (DebugSelf())
507// {
508// result.AppendMessageWithFormat ("Processing command: %s\n", command_line);
509// }
510
Greg Clayton63094e02010-06-23 01:19:29 +0000511 m_debugger.UpdateExecutionContext (override_context);
Chris Lattner24943d22010-06-08 16:52:24 +0000512
513 if (command_line == NULL || command_line[0] == '\0')
514 {
515 if (m_command_history.empty())
516 {
517 result.AppendError ("empty command");
518 result.SetStatus(eReturnStatusFailed);
519 return false;
520 }
521 else
522 {
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000523 command_line = m_repeat_command.c_str();
524 if (m_repeat_command.empty())
525 {
Jim Ingham767af882010-07-07 03:36:20 +0000526 result.AppendErrorWithFormat("No auto repeat.\n");
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000527 result.SetStatus (eReturnStatusFailed);
528 return false;
529 }
Chris Lattner24943d22010-06-08 16:52:24 +0000530 }
531 add_to_history = false;
532 }
533
534 Args command_args(command_line);
535
536 if (command_args.GetArgumentCount() > 0)
537 {
538 const char *command_cstr = command_args.GetArgumentAtIndex(0);
539 if (command_cstr)
540 {
541
542 // We're looking up the command object here. So first find an exact match to the
543 // command in the commands.
Jim Inghamd40f8a62010-07-06 22:46:59 +0000544 CommandObject *command_obj = GetCommandObject(command_cstr);
545
546 if (command_obj != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000547 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000548 if (command_obj->IsAlias())
Chris Lattner24943d22010-06-08 16:52:24 +0000549 {
550 BuildAliasCommandArgs (command_obj, command_cstr, command_args, result);
551 if (!result.Succeeded())
552 return false;
553 }
Chris Lattner24943d22010-06-08 16:52:24 +0000554
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000555 if (add_to_history)
556 {
Jim Ingham767af882010-07-07 03:36:20 +0000557 const char *repeat_command = command_obj->GetRepeatCommand(command_args, 0);
558 if (repeat_command != NULL)
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000559 m_repeat_command.assign(repeat_command);
560 else
Jim Ingham767af882010-07-07 03:36:20 +0000561 m_repeat_command.assign(command_line);
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000562
563 m_command_history.push_back (command_line);
564 }
565
566
Chris Lattner24943d22010-06-08 16:52:24 +0000567 if (command_obj->WantsRawCommandString())
568 {
569 const char *stripped_command = ::strstr (command_line, command_cstr);
570 if (stripped_command)
571 {
572 stripped_command += strlen(command_cstr);
573 while (isspace(*stripped_command))
574 ++stripped_command;
Greg Clayton63094e02010-06-23 01:19:29 +0000575 command_obj->ExecuteRawCommandString (*this, stripped_command, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000576 }
577 }
578 else
579 {
Chris Lattner24943d22010-06-08 16:52:24 +0000580 // Remove the command from the args.
581 command_args.Shift();
Greg Clayton63094e02010-06-23 01:19:29 +0000582 command_obj->ExecuteWithOptions (*this, command_args, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000583 }
584 }
585 else
586 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000587 // We didn't find the first command object, so complete the first argument.
Chris Lattner24943d22010-06-08 16:52:24 +0000588 StringList matches;
589 int num_matches;
Jim Inghamd40f8a62010-07-06 22:46:59 +0000590 int cursor_index = 0;
591 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
Jim Ingham802f8b02010-06-30 05:02:46 +0000592 bool word_complete;
Greg Clayton63094e02010-06-23 01:19:29 +0000593 num_matches = HandleCompletionMatches (command_args,
594 cursor_index,
595 cursor_char_position,
596 0,
597 -1,
Jim Ingham802f8b02010-06-30 05:02:46 +0000598 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000599 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000600
601 if (num_matches > 0)
602 {
603 std::string error_msg;
604 error_msg.assign ("ambiguous command '");
605 error_msg.append(command_cstr);
606 error_msg.append ("'.");
607
608 error_msg.append (" Possible completions:");
609 for (int i = 0; i < num_matches; i++)
610 {
611 error_msg.append ("\n\t");
612 error_msg.append (matches.GetStringAtIndex (i));
613 }
614 error_msg.append ("\n");
615 result.AppendRawError (error_msg.c_str(), error_msg.size());
616 }
617 else
618 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_cstr);
619
620 result.SetStatus (eReturnStatusFailed);
621 }
622 }
623 }
624 return result.Succeeded();
625}
626
627int
628CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
629 int &cursor_index,
630 int &cursor_char_position,
631 int match_start_point,
632 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000633 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000634 StringList &matches)
635{
636 int num_command_matches = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000637 bool look_for_subcommand = false;
Jim Ingham802f8b02010-06-30 05:02:46 +0000638
639 // For any of the command completions a unique match will be a complete word.
640 word_complete = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000641
642 if (cursor_index == -1)
643 {
644 // We got nothing on the command line, so return the list of commands
Jim Inghamd40f8a62010-07-06 22:46:59 +0000645 bool include_aliases = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000646 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
647 }
648 else if (cursor_index == 0)
649 {
650 // The cursor is in the first argument, so just do a lookup in the dictionary.
Jim Inghamd40f8a62010-07-06 22:46:59 +0000651 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000652 num_command_matches = matches.GetSize();
653
654 if (num_command_matches == 1
655 && cmd_obj && cmd_obj->IsMultiwordObject()
656 && matches.GetStringAtIndex(0) != NULL
657 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
658 {
659 look_for_subcommand = true;
660 num_command_matches = 0;
661 matches.DeleteStringAtIndex(0);
662 parsed_line.AppendArgument ("");
663 cursor_index++;
664 cursor_char_position = 0;
665 }
666 }
667
668 if (cursor_index > 0 || look_for_subcommand)
669 {
670 // We are completing further on into a commands arguments, so find the command and tell it
671 // to complete the command.
672 // First see if there is a matching initial command:
Jim Inghamd40f8a62010-07-06 22:46:59 +0000673 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000674 if (command_object == NULL)
675 {
676 return 0;
677 }
678 else
679 {
680 parsed_line.Shift();
681 cursor_index--;
Greg Clayton63094e02010-06-23 01:19:29 +0000682 num_command_matches = command_object->HandleCompletion (*this,
683 parsed_line,
684 cursor_index,
685 cursor_char_position,
686 match_start_point,
Jim Ingham802f8b02010-06-30 05:02:46 +0000687 max_return_elements,
688 word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000689 matches);
690 }
691 }
692
693 return num_command_matches;
694
695}
696
697int
698CommandInterpreter::HandleCompletion (const char *current_line,
699 const char *cursor,
700 const char *last_char,
701 int match_start_point,
702 int max_return_elements,
703 StringList &matches)
704{
705 // We parse the argument up to the cursor, so the last argument in parsed_line is
706 // the one containing the cursor, and the cursor is after the last character.
707
708 Args parsed_line(current_line, last_char - current_line);
709 Args partial_parsed_line(current_line, cursor - current_line);
710
711 int num_args = partial_parsed_line.GetArgumentCount();
712 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
713 int cursor_char_position;
714
715 if (cursor_index == -1)
716 cursor_char_position = 0;
717 else
718 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
719
720 int num_command_matches;
721
722 matches.Clear();
723
724 // Only max_return_elements == -1 is supported at present:
725 assert (max_return_elements == -1);
Jim Ingham802f8b02010-06-30 05:02:46 +0000726 bool word_complete;
Greg Clayton63094e02010-06-23 01:19:29 +0000727 num_command_matches = HandleCompletionMatches (parsed_line,
728 cursor_index,
729 cursor_char_position,
730 match_start_point,
Jim Ingham802f8b02010-06-30 05:02:46 +0000731 max_return_elements,
732 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000733 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000734
735 if (num_command_matches <= 0)
736 return num_command_matches;
737
738 if (num_args == 0)
739 {
740 // If we got an empty string, insert nothing.
741 matches.InsertStringAtIndex(0, "");
742 }
743 else
744 {
745 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
746 // put an empty string in element 0.
747 std::string command_partial_str;
748 if (cursor_index >= 0)
749 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index), parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
750
751 std::string common_prefix;
752 matches.LongestCommonPrefix (common_prefix);
753 int partial_name_len = command_partial_str.size();
754
755 // If we matched a unique single command, add a space...
Jim Ingham802f8b02010-06-30 05:02:46 +0000756 // Only do this if the completer told us this was a complete word, however...
757 if (num_command_matches == 1 && word_complete)
Chris Lattner24943d22010-06-08 16:52:24 +0000758 {
759 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
760 if (quote_char != '\0')
761 common_prefix.push_back(quote_char);
762
763 common_prefix.push_back(' ');
764 }
765 common_prefix.erase (0, partial_name_len);
766 matches.InsertStringAtIndex(0, common_prefix.c_str());
767 }
768 return num_command_matches;
769}
770
Chris Lattner24943d22010-06-08 16:52:24 +0000771
772CommandInterpreter::~CommandInterpreter ()
773{
774}
775
776const char *
777CommandInterpreter::GetPrompt ()
778{
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000779 lldb::SettableVariableType var_type;
780 const char *instance_name = GetDebugger().GetInstanceName().AsCString();
781 StreamString var_name;
782 var_name.Printf ("[%s].prompt", instance_name);
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000783 return Debugger::GetSettingsController()->GetVariable (var_name.GetData(), var_type, instance_name).GetStringAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000784}
785
786void
787CommandInterpreter::SetPrompt (const char *new_prompt)
788{
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000789 const char *instance_name = GetDebugger().GetInstanceName().AsCString();
790 StreamString name_str;
791 name_str.Printf ("[%s].prompt", instance_name);
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000792 Debugger::GetSettingsController()->SetVariable (name_str.GetData(), new_prompt, lldb::eVarSetOperationAssign,
793 false, m_debugger.GetInstanceName().AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000794}
795
796void
797CommandInterpreter::CrossRegisterCommand (const char * dest_cmd, const char * object_type)
798{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000799 CommandObjectSP cmd_obj_sp = GetCommandSPExact (dest_cmd, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000800
801 if (cmd_obj_sp != NULL)
802 {
803 CommandObject *cmd_obj = cmd_obj_sp.get();
804 if (cmd_obj->IsCrossRefObject ())
805 cmd_obj->AddObject (object_type);
806 }
807}
808
Chris Lattner24943d22010-06-08 16:52:24 +0000809OptionArgVectorSP
810CommandInterpreter::GetAliasOptions (const char *alias_name)
811{
812 OptionArgMap::iterator pos;
813 OptionArgVectorSP ret_val;
814
815 std::string alias (alias_name);
816
817 if (HasAliasOptions())
818 {
819 pos = m_alias_options.find (alias);
820 if (pos != m_alias_options.end())
821 ret_val = pos->second;
822 }
823
824 return ret_val;
825}
826
827void
828CommandInterpreter::RemoveAliasOptions (const char *alias_name)
829{
830 OptionArgMap::iterator pos = m_alias_options.find(alias_name);
831 if (pos != m_alias_options.end())
832 {
833 m_alias_options.erase (pos);
834 }
835}
836
837void
838CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp)
839{
840 m_alias_options[alias_name] = option_arg_vector_sp;
841}
842
843bool
844CommandInterpreter::HasCommands ()
845{
846 return (!m_command_dict.empty());
847}
848
849bool
850CommandInterpreter::HasAliases ()
851{
852 return (!m_alias_dict.empty());
853}
854
855bool
856CommandInterpreter::HasUserCommands ()
857{
858 return (!m_user_dict.empty());
859}
860
861bool
862CommandInterpreter::HasAliasOptions ()
863{
864 return (!m_alias_options.empty());
865}
866
Chris Lattner24943d22010-06-08 16:52:24 +0000867void
868CommandInterpreter::BuildAliasCommandArgs
869(
870 CommandObject *alias_cmd_obj,
871 const char *alias_name,
872 Args &cmd_args,
873 CommandReturnObject &result
874)
875{
876 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
877
878 if (option_arg_vector_sp.get())
879 {
880 // Make sure that the alias name is the 0th element in cmd_args
881 std::string alias_name_str = alias_name;
882 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
883 cmd_args.Unshift (alias_name);
884
885 Args new_args (alias_cmd_obj->GetCommandName());
886 if (new_args.GetArgumentCount() == 2)
887 new_args.Shift();
888
889 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
890 int old_size = cmd_args.GetArgumentCount();
891 int *used = (int *) malloc ((old_size + 1) * sizeof (int));
892
893 memset (used, 0, (old_size + 1) * sizeof (int));
894 used[0] = 1;
895
896 for (int i = 0; i < option_arg_vector->size(); ++i)
897 {
898 OptionArgPair option_pair = (*option_arg_vector)[i];
899 std::string option = option_pair.first;
900 std::string value = option_pair.second;
901 if (option.compare ("<argument>") == 0)
902 new_args.AppendArgument (value.c_str());
903 else
904 {
905 new_args.AppendArgument (option.c_str());
906 if (value.compare ("<no-argument>") != 0)
907 {
908 int index = GetOptionArgumentPosition (value.c_str());
909 if (index == 0)
910 // value was NOT a positional argument; must be a real value
911 new_args.AppendArgument (value.c_str());
912 else if (index >= cmd_args.GetArgumentCount())
913 {
914 result.AppendErrorWithFormat
915 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
916 index);
917 result.SetStatus (eReturnStatusFailed);
918 return;
919 }
920 else
921 {
922 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
923 used[index] = 1;
924 }
925 }
926 }
927 }
928
929 for (int j = 0; j < cmd_args.GetArgumentCount(); ++j)
930 {
931 if (!used[j])
932 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
933 }
934
935 cmd_args.Clear();
936 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
937 }
938 else
939 {
940 result.SetStatus (eReturnStatusSuccessFinishNoResult);
941 // This alias was not created with any options; nothing further needs to be done.
942 return;
943 }
944
945 result.SetStatus (eReturnStatusSuccessFinishNoResult);
946 return;
947}
948
949
950int
951CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
952{
953 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
954 // of zero.
955
956 char *cptr = (char *) in_string;
957
958 // Does it start with '%'
959 if (cptr[0] == '%')
960 {
961 ++cptr;
962
963 // Is the rest of it entirely digits?
964 if (isdigit (cptr[0]))
965 {
966 const char *start = cptr;
967 while (isdigit (cptr[0]))
968 ++cptr;
969
970 // We've gotten to the end of the digits; are we at the end of the string?
971 if (cptr[0] == '\0')
972 position = atoi (start);
973 }
974 }
975
976 return position;
977}
978
979void
980CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
981{
982 const char *init_file_path = in_cwd ? "./.lldbinit" : "~/.lldbinit";
983 FileSpec init_file (init_file_path);
984 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
985 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
986
987 if (init_file.Exists())
988 {
989 char path[PATH_MAX];
990 init_file.GetPath(path, sizeof(path));
991 StreamString source_command;
Johnny Chen7c984242010-07-28 21:16:11 +0000992 source_command.Printf ("command source '%s'", path);
Chris Lattner24943d22010-06-08 16:52:24 +0000993 HandleCommand (source_command.GetData(), false, result);
994 }
995 else
996 {
997 // nothing to be done if the file doesn't exist
998 result.SetStatus(eReturnStatusSuccessFinishNoResult);
999 }
1000}
1001
1002ScriptInterpreter *
1003CommandInterpreter::GetScriptInterpreter ()
1004{
Greg Clayton63094e02010-06-23 01:19:29 +00001005 CommandObject::CommandMap::iterator pos;
1006
1007 pos = m_command_dict.find ("script");
1008 if (pos != m_command_dict.end())
Chris Lattner24943d22010-06-08 16:52:24 +00001009 {
Greg Clayton63094e02010-06-23 01:19:29 +00001010 CommandObject *script_cmd_obj = pos->second.get();
1011 return ((CommandObjectScript *) script_cmd_obj)->GetInterpreter (*this);
Chris Lattner24943d22010-06-08 16:52:24 +00001012 }
Greg Clayton63094e02010-06-23 01:19:29 +00001013 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +00001014}
1015
1016
1017
1018bool
1019CommandInterpreter::GetSynchronous ()
1020{
1021 return m_synchronous_execution;
1022}
1023
1024void
1025CommandInterpreter::SetSynchronous (bool value)
1026{
1027 static bool value_set_once = false;
1028 if (!value_set_once)
1029 {
1030 value_set_once = true;
1031 m_synchronous_execution = value;
1032 }
1033}
1034
1035void
1036CommandInterpreter::OutputFormattedHelpText (Stream &strm,
1037 const char *word_text,
1038 const char *separator,
1039 const char *help_text,
1040 uint32_t max_word_len)
1041{
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00001042 lldb::SettableVariableType var_type;
1043 const char *width_value =
Caroline Tice1d2aefd2010-09-09 06:25:08 +00001044 Debugger::GetSettingsController()->GetVariable ("term-width", var_type,
1045 m_debugger.GetInstanceName().AsCString()).GetStringAtIndex(0);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00001046 int max_columns = atoi (width_value);
Chris Lattner24943d22010-06-08 16:52:24 +00001047 // Sanity check max_columns, to cope with emacs shell mode with TERM=dumb
1048 // (0 rows; 0 columns;).
1049 if (max_columns <= 0) max_columns = 80;
1050
1051 int indent_size = max_word_len + strlen (separator) + 2;
1052
1053 strm.IndentMore (indent_size);
1054
1055 int len = indent_size + strlen (help_text) + 1;
1056 char *text = (char *) malloc (len);
1057 sprintf (text, "%-*s %s %s", max_word_len, word_text, separator, help_text);
1058 if (text[len - 1] == '\n')
1059 text[--len] = '\0';
1060
1061 if (len < max_columns)
1062 {
1063 // Output it as a single line.
1064 strm.Printf ("%s", text);
1065 }
1066 else
1067 {
1068 // We need to break it up into multiple lines.
1069 bool first_line = true;
1070 int text_width;
1071 int start = 0;
1072 int end = start;
1073 int final_end = strlen (text);
1074 int sub_len;
1075
1076 while (end < final_end)
1077 {
1078 if (first_line)
1079 text_width = max_columns - 1;
1080 else
1081 text_width = max_columns - indent_size - 1;
1082
1083 // Don't start the 'text' on a space, since we're already outputting the indentation.
1084 if (!first_line)
1085 {
1086 while ((start < final_end) && (text[start] == ' '))
1087 start++;
1088 }
1089
1090 end = start + text_width;
1091 if (end > final_end)
1092 end = final_end;
1093 else
1094 {
1095 // If we're not at the end of the text, make sure we break the line on white space.
1096 while (end > start
1097 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
1098 end--;
1099 }
1100
1101 sub_len = end - start;
1102 if (start != 0)
1103 strm.EOL();
1104 if (!first_line)
1105 strm.Indent();
1106 else
1107 first_line = false;
1108 assert (start <= final_end);
1109 assert (start + sub_len <= final_end);
1110 if (sub_len > 0)
1111 strm.Write (text + start, sub_len);
1112 start = end + 1;
1113 }
1114 }
1115 strm.EOL();
1116 strm.IndentLess(indent_size);
1117 free (text);
1118}
1119
1120void
1121CommandInterpreter::AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word,
1122 StringList &commands_found, StringList &commands_help)
1123{
1124 CommandObject::CommandMap::const_iterator pos;
1125 CommandObject::CommandMap sub_cmd_dict = ((CommandObjectMultiword *) cmd_obj)->m_subcommand_dict;
1126 CommandObject *sub_cmd_obj;
1127
1128 for (pos = sub_cmd_dict.begin(); pos != sub_cmd_dict.end(); ++pos)
1129 {
1130 const char * command_name = pos->first.c_str();
1131 sub_cmd_obj = pos->second.get();
1132 StreamString complete_command_name;
1133
1134 complete_command_name.Printf ("%s %s", prefix, command_name);
1135
Caroline Tice1d2aefd2010-09-09 06:25:08 +00001136 if (sub_cmd_obj->HelpTextContainsWord (search_word, *this))
Chris Lattner24943d22010-06-08 16:52:24 +00001137 {
1138 commands_found.AppendString (complete_command_name.GetData());
1139 commands_help.AppendString (sub_cmd_obj->GetHelp());
1140 }
1141
1142 if (sub_cmd_obj->IsMultiwordObject())
1143 AproposAllSubCommands (sub_cmd_obj, complete_command_name.GetData(), search_word, commands_found,
1144 commands_help);
1145 }
1146
1147}
1148
1149void
1150CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
1151 StringList &commands_help)
1152{
1153 CommandObject::CommandMap::const_iterator pos;
1154
1155 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1156 {
1157 const char *command_name = pos->first.c_str();
1158 CommandObject *cmd_obj = pos->second.get();
1159
Caroline Tice1d2aefd2010-09-09 06:25:08 +00001160 if (cmd_obj->HelpTextContainsWord (search_word, *this))
Chris Lattner24943d22010-06-08 16:52:24 +00001161 {
1162 commands_found.AppendString (command_name);
1163 commands_help.AppendString (cmd_obj->GetHelp());
1164 }
1165
1166 if (cmd_obj->IsMultiwordObject())
1167 AproposAllSubCommands (cmd_obj, command_name, search_word, commands_found, commands_help);
1168
1169 }
1170}