blob: 7b742c8ff1f45538685c290bd5f8fcc217952f64 [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);
Greg Clayton0f3a8eb2010-09-16 17:09:23 +000089 result.Clear(); HandleCommand ("command alias b regexp-break", false, result);
Jim Ingham767af882010-07-07 03:36:20 +000090 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);
Greg Clayton0f3a8eb2010-09-16 17:09:23 +0000100 result.Clear(); HandleCommand ("command alias p frame variable", false, result);
101 result.Clear(); HandleCommand ("command alias print frame variable", false, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000102}
103
Chris Lattner24943d22010-06-08 16:52:24 +0000104const char *
105CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg)
106{
107 // This function has not yet been implemented.
108
109 // Look for any embedded script command
110 // If found,
111 // get interpreter object from the command dictionary,
112 // call execute_one_command on it,
113 // get the results as a string,
114 // substitute that string for current stuff.
115
116 return arg;
117}
118
119
120void
121CommandInterpreter::LoadCommandDictionary ()
122{
123 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
124
125 // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT ***
126 //
127 // Command objects that are used as cross reference objects (i.e. they inherit from CommandObjectCrossref)
128 // *MUST* be created and put into the command dictionary *BEFORE* any multi-word commands (which may use
129 // the cross-referencing stuff) are created!!!
130 //
131 // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT ***
132
133
134 // Command objects that inherit from CommandObjectCrossref must be created before other command objects
135 // are created. This is so that when another command is created that needs to go into a crossref object,
136 // the crossref object exists and is ready to take the cross reference. Put the cross referencing command
137 // objects into the CommandDictionary now, so they are ready for use when the other commands get created.
138
Chris Lattner24943d22010-06-08 16:52:24 +0000139 // Non-CommandObjectCrossref commands can now be created.
140
Caroline Tice5bc8c972010-09-20 20:44:43 +0000141 lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage();
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000142
Greg Clayton238c0a12010-09-18 01:14:36 +0000143 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000144 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000145 //m_command_dict["call"] = CommandObjectSP (new CommandObjectCall (*this));
Jim Ingham767af882010-07-07 03:36:20 +0000146 m_command_dict["commands"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000147 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this));
148 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this));
149 m_command_dict["file"] = CommandObjectSP (new CommandObjectFile (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000150 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000151 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000152 m_command_dict["image"] = CommandObjectSP (new CommandObjectImage (*this));
153 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
154 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
155 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000156 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000157 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000158 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000159 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
Jim Ingham767af882010-07-07 03:36:20 +0000160 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000161 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
162 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000163
164 std::auto_ptr<CommandObjectRegexCommand>
Greg Clayton238c0a12010-09-18 01:14:36 +0000165 break_regex_cmd_ap(new CommandObjectRegexCommand (*this,
166 "regexp-break",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000167 "Set a breakpoint using a regular expression to specify the location.",
Chris Lattner24943d22010-06-08 16:52:24 +0000168 "regexp-break [<file>:<line>]\nregexp-break [<address>]\nregexp-break <...>", 2));
169 if (break_regex_cmd_ap.get())
170 {
171 if (break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2") &&
172 break_regex_cmd_ap->AddRegexCommand("^(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1") &&
173 break_regex_cmd_ap->AddRegexCommand("^[\"']?([-+]\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'") &&
174 break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list") &&
175 break_regex_cmd_ap->AddRegexCommand("^(-.*)$", "breakpoint set %1") &&
176 break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1'"))
177 {
178 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
179 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
180 }
181 }
182}
183
184int
185CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
186 StringList &matches)
187{
188 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
189
190 if (include_aliases)
191 {
192 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
193 }
194
195 return matches.GetSize();
196}
197
198CommandObjectSP
199CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
200{
201 CommandObject::CommandMap::iterator pos;
202 CommandObjectSP ret_val;
203
204 std::string cmd(cmd_cstr);
205
206 if (HasCommands())
207 {
208 pos = m_command_dict.find(cmd);
209 if (pos != m_command_dict.end())
210 ret_val = pos->second;
211 }
212
213 if (include_aliases && HasAliases())
214 {
215 pos = m_alias_dict.find(cmd);
216 if (pos != m_alias_dict.end())
217 ret_val = pos->second;
218 }
219
220 if (HasUserCommands())
221 {
222 pos = m_user_dict.find(cmd);
223 if (pos != m_user_dict.end())
224 ret_val = pos->second;
225 }
226
227 if (!exact && ret_val == NULL)
228 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000229 // We will only get into here if we didn't find any exact matches.
230
231 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
232
Chris Lattner24943d22010-06-08 16:52:24 +0000233 StringList local_matches;
234 if (matches == NULL)
235 matches = &local_matches;
236
Jim Inghamd40f8a62010-07-06 22:46:59 +0000237 unsigned int num_cmd_matches = 0;
238 unsigned int num_alias_matches = 0;
239 unsigned int num_user_matches = 0;
240
241 // Look through the command dictionaries one by one, and if we get only one match from any of
242 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
243
Chris Lattner24943d22010-06-08 16:52:24 +0000244 if (HasCommands())
245 {
246 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
247 }
248
249 if (num_cmd_matches == 1)
250 {
251 cmd.assign(matches->GetStringAtIndex(0));
252 pos = m_command_dict.find(cmd);
253 if (pos != m_command_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000254 real_match_sp = pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000255 }
256
Jim Ingham9a574172010-06-24 20:28:42 +0000257 if (include_aliases && HasAliases())
Chris Lattner24943d22010-06-08 16:52:24 +0000258 {
259 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
260
261 }
262
Jim Inghamd40f8a62010-07-06 22:46:59 +0000263 if (num_alias_matches == 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000264 {
265 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
266 pos = m_alias_dict.find(cmd);
267 if (pos != m_alias_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000268 alias_match_sp = pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000269 }
270
Jim Ingham9a574172010-06-24 20:28:42 +0000271 if (HasUserCommands())
Chris Lattner24943d22010-06-08 16:52:24 +0000272 {
273 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
274 }
275
Jim Inghamd40f8a62010-07-06 22:46:59 +0000276 if (num_user_matches == 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000277 {
278 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
279
280 pos = m_user_dict.find (cmd);
281 if (pos != m_user_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000282 user_match_sp = pos->second;
283 }
284
285 // If we got exactly one match, return that, otherwise return the match list.
286
287 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
288 {
289 if (num_cmd_matches)
290 return real_match_sp;
291 else if (num_alias_matches)
292 return alias_match_sp;
293 else
294 return user_match_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000295 }
296 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000297 else if (matches && ret_val != NULL)
298 {
299 matches->AppendString (cmd_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +0000300 }
301
302
303 return ret_val;
304}
305
Jim Inghamd40f8a62010-07-06 22:46:59 +0000306CommandObjectSP
307CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
Chris Lattner24943d22010-06-08 16:52:24 +0000308{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000309 return GetCommandSP(cmd_cstr, include_aliases, true, NULL);
310}
311
312CommandObject *
313CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
314{
315 return GetCommandSPExact (cmd_cstr, include_aliases).get();
316}
317
318CommandObject *
319CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
320{
321 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
322
323 // If we didn't find an exact match to the command string in the commands, look in
324 // the aliases.
325
326 if (command_obj == NULL)
327 {
328 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
329 }
330
331 // Finally, if there wasn't an exact match among the aliases, look for an inexact match
332 // in both the commands and the aliases.
333
334 if (command_obj == NULL)
335 command_obj = GetCommandSP(cmd_cstr, true, false, matches).get();
336
337 return command_obj;
Chris Lattner24943d22010-06-08 16:52:24 +0000338}
339
340bool
341CommandInterpreter::CommandExists (const char *cmd)
342{
343 return m_command_dict.find(cmd) != m_command_dict.end();
344}
345
346bool
347CommandInterpreter::AliasExists (const char *cmd)
348{
349 return m_alias_dict.find(cmd) != m_alias_dict.end();
350}
351
352bool
353CommandInterpreter::UserCommandExists (const char *cmd)
354{
355 return m_user_dict.find(cmd) != m_user_dict.end();
356}
357
358void
359CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
360{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000361 command_obj_sp->SetIsAlias (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000362 m_alias_dict[alias_name] = command_obj_sp;
363}
364
365bool
366CommandInterpreter::RemoveAlias (const char *alias_name)
367{
368 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
369 if (pos != m_alias_dict.end())
370 {
371 m_alias_dict.erase(pos);
372 return true;
373 }
374 return false;
375}
376bool
377CommandInterpreter::RemoveUser (const char *alias_name)
378{
379 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
380 if (pos != m_user_dict.end())
381 {
382 m_user_dict.erase(pos);
383 return true;
384 }
385 return false;
386}
387
Chris Lattner24943d22010-06-08 16:52:24 +0000388void
389CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
390{
391 help_string.Printf ("'%s", command_name);
392 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
393
394 if (option_arg_vector_sp != NULL)
395 {
396 OptionArgVector *options = option_arg_vector_sp.get();
397 for (int i = 0; i < options->size(); ++i)
398 {
399 OptionArgPair cur_option = (*options)[i];
400 std::string opt = cur_option.first;
401 std::string value = cur_option.second;
402 if (opt.compare("<argument>") == 0)
403 {
404 help_string.Printf (" %s", value.c_str());
405 }
406 else
407 {
408 help_string.Printf (" %s", opt.c_str());
409 if ((value.compare ("<no-argument>") != 0)
410 && (value.compare ("<need-argument") != 0))
411 {
412 help_string.Printf (" %s", value.c_str());
413 }
414 }
415 }
416 }
417
418 help_string.Printf ("'");
419}
420
Greg Clayton65124ea2010-08-26 22:05:43 +0000421size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000422CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
423{
424 CommandObject::CommandMap::const_iterator pos;
Greg Clayton65124ea2010-08-26 22:05:43 +0000425 CommandObject::CommandMap::const_iterator end = dict.end();
426 size_t max_len = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000427
Greg Clayton65124ea2010-08-26 22:05:43 +0000428 for (pos = dict.begin(); pos != end; ++pos)
429 {
430 size_t len = pos->first.size();
431 if (max_len < len)
432 max_len = len;
Chris Lattner24943d22010-06-08 16:52:24 +0000433 }
Greg Clayton65124ea2010-08-26 22:05:43 +0000434 return max_len;
Chris Lattner24943d22010-06-08 16:52:24 +0000435}
436
437void
438CommandInterpreter::GetHelp (CommandReturnObject &result)
439{
440 CommandObject::CommandMap::const_iterator pos;
441 result.AppendMessage("The following is a list of built-in, permanent debugger commands:");
442 result.AppendMessage("");
Greg Clayton65124ea2010-08-26 22:05:43 +0000443 uint32_t max_len = FindLongestCommandWord (m_command_dict);
Chris Lattner24943d22010-06-08 16:52:24 +0000444
445 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
446 {
447 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
448 max_len);
449 }
450 result.AppendMessage("");
451
452 if (m_alias_dict.size() > 0)
453 {
Caroline Tice00edd3a2010-09-13 05:27:16 +0000454 result.AppendMessage("The following is a list of your current command abbreviations (see 'help commands alias' for more info):");
Chris Lattner24943d22010-06-08 16:52:24 +0000455 result.AppendMessage("");
Greg Clayton65124ea2010-08-26 22:05:43 +0000456 max_len = FindLongestCommandWord (m_alias_dict);
457
Chris Lattner24943d22010-06-08 16:52:24 +0000458 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
459 {
460 StreamString sstr;
461 StreamString translation_and_help;
462 std::string entry_name = pos->first;
463 std::string second_entry = pos->second.get()->GetCommandName();
464 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
465
466 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
467 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
468 translation_and_help.GetData(), max_len);
469 }
470 result.AppendMessage("");
471 }
472
473 if (m_user_dict.size() > 0)
474 {
475 result.AppendMessage ("The following is a list of your current user-defined commands:");
476 result.AppendMessage("");
477 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
478 {
479 result.AppendMessageWithFormat ("%s -- %s\n", pos->first.c_str(), pos->second->GetHelp());
480 }
481 result.AppendMessage("");
482 }
483
484 result.AppendMessage("For more information on any particular command, try 'help <command-name>'.");
485}
486
Chris Lattner24943d22010-06-08 16:52:24 +0000487bool
Greg Clayton63094e02010-06-23 01:19:29 +0000488CommandInterpreter::HandleCommand
489(
490 const char *command_line,
491 bool add_to_history,
492 CommandReturnObject &result,
493 ExecutionContext *override_context
494)
Chris Lattner24943d22010-06-08 16:52:24 +0000495{
496 // FIXME: there should probably be a mutex to make sure only one thread can
497 // run the interpreter at a time.
498
499 // TODO: this should be a logging channel in lldb.
500// if (DebugSelf())
501// {
502// result.AppendMessageWithFormat ("Processing command: %s\n", command_line);
503// }
504
Greg Clayton63094e02010-06-23 01:19:29 +0000505 m_debugger.UpdateExecutionContext (override_context);
Chris Lattner24943d22010-06-08 16:52:24 +0000506
507 if (command_line == NULL || command_line[0] == '\0')
508 {
509 if (m_command_history.empty())
510 {
511 result.AppendError ("empty command");
512 result.SetStatus(eReturnStatusFailed);
513 return false;
514 }
515 else
516 {
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000517 command_line = m_repeat_command.c_str();
518 if (m_repeat_command.empty())
519 {
Jim Ingham767af882010-07-07 03:36:20 +0000520 result.AppendErrorWithFormat("No auto repeat.\n");
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000521 result.SetStatus (eReturnStatusFailed);
522 return false;
523 }
Chris Lattner24943d22010-06-08 16:52:24 +0000524 }
525 add_to_history = false;
526 }
527
528 Args command_args(command_line);
529
530 if (command_args.GetArgumentCount() > 0)
531 {
532 const char *command_cstr = command_args.GetArgumentAtIndex(0);
533 if (command_cstr)
534 {
535
536 // We're looking up the command object here. So first find an exact match to the
537 // command in the commands.
Jim Inghamd40f8a62010-07-06 22:46:59 +0000538 CommandObject *command_obj = GetCommandObject(command_cstr);
539
540 if (command_obj != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000541 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000542 if (command_obj->IsAlias())
Chris Lattner24943d22010-06-08 16:52:24 +0000543 {
544 BuildAliasCommandArgs (command_obj, command_cstr, command_args, result);
545 if (!result.Succeeded())
546 return false;
547 }
Chris Lattner24943d22010-06-08 16:52:24 +0000548
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000549 if (add_to_history)
550 {
Jim Ingham767af882010-07-07 03:36:20 +0000551 const char *repeat_command = command_obj->GetRepeatCommand(command_args, 0);
552 if (repeat_command != NULL)
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000553 m_repeat_command.assign(repeat_command);
554 else
Jim Ingham767af882010-07-07 03:36:20 +0000555 m_repeat_command.assign(command_line);
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000556
557 m_command_history.push_back (command_line);
558 }
559
560
Chris Lattner24943d22010-06-08 16:52:24 +0000561 if (command_obj->WantsRawCommandString())
562 {
563 const char *stripped_command = ::strstr (command_line, command_cstr);
564 if (stripped_command)
565 {
566 stripped_command += strlen(command_cstr);
567 while (isspace(*stripped_command))
568 ++stripped_command;
Greg Clayton238c0a12010-09-18 01:14:36 +0000569 command_obj->ExecuteRawCommandString (stripped_command, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000570 }
571 }
572 else
573 {
Chris Lattner24943d22010-06-08 16:52:24 +0000574 // Remove the command from the args.
575 command_args.Shift();
Greg Clayton238c0a12010-09-18 01:14:36 +0000576 command_obj->ExecuteWithOptions (command_args, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000577 }
578 }
579 else
580 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000581 // We didn't find the first command object, so complete the first argument.
Chris Lattner24943d22010-06-08 16:52:24 +0000582 StringList matches;
583 int num_matches;
Jim Inghamd40f8a62010-07-06 22:46:59 +0000584 int cursor_index = 0;
585 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
Jim Ingham802f8b02010-06-30 05:02:46 +0000586 bool word_complete;
Greg Clayton63094e02010-06-23 01:19:29 +0000587 num_matches = HandleCompletionMatches (command_args,
588 cursor_index,
589 cursor_char_position,
590 0,
591 -1,
Jim Ingham802f8b02010-06-30 05:02:46 +0000592 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000593 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000594
595 if (num_matches > 0)
596 {
597 std::string error_msg;
598 error_msg.assign ("ambiguous command '");
599 error_msg.append(command_cstr);
600 error_msg.append ("'.");
601
602 error_msg.append (" Possible completions:");
603 for (int i = 0; i < num_matches; i++)
604 {
605 error_msg.append ("\n\t");
606 error_msg.append (matches.GetStringAtIndex (i));
607 }
608 error_msg.append ("\n");
609 result.AppendRawError (error_msg.c_str(), error_msg.size());
610 }
611 else
612 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_cstr);
613
614 result.SetStatus (eReturnStatusFailed);
615 }
616 }
617 }
618 return result.Succeeded();
619}
620
621int
622CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
623 int &cursor_index,
624 int &cursor_char_position,
625 int match_start_point,
626 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000627 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000628 StringList &matches)
629{
630 int num_command_matches = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000631 bool look_for_subcommand = false;
Jim Ingham802f8b02010-06-30 05:02:46 +0000632
633 // For any of the command completions a unique match will be a complete word.
634 word_complete = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000635
636 if (cursor_index == -1)
637 {
638 // We got nothing on the command line, so return the list of commands
Jim Inghamd40f8a62010-07-06 22:46:59 +0000639 bool include_aliases = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000640 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
641 }
642 else if (cursor_index == 0)
643 {
644 // The cursor is in the first argument, so just do a lookup in the dictionary.
Jim Inghamd40f8a62010-07-06 22:46:59 +0000645 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000646 num_command_matches = matches.GetSize();
647
648 if (num_command_matches == 1
649 && cmd_obj && cmd_obj->IsMultiwordObject()
650 && matches.GetStringAtIndex(0) != NULL
651 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
652 {
653 look_for_subcommand = true;
654 num_command_matches = 0;
655 matches.DeleteStringAtIndex(0);
656 parsed_line.AppendArgument ("");
657 cursor_index++;
658 cursor_char_position = 0;
659 }
660 }
661
662 if (cursor_index > 0 || look_for_subcommand)
663 {
664 // We are completing further on into a commands arguments, so find the command and tell it
665 // to complete the command.
666 // First see if there is a matching initial command:
Jim Inghamd40f8a62010-07-06 22:46:59 +0000667 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000668 if (command_object == NULL)
669 {
670 return 0;
671 }
672 else
673 {
674 parsed_line.Shift();
675 cursor_index--;
Greg Clayton238c0a12010-09-18 01:14:36 +0000676 num_command_matches = command_object->HandleCompletion (parsed_line,
Greg Clayton63094e02010-06-23 01:19:29 +0000677 cursor_index,
678 cursor_char_position,
679 match_start_point,
Jim Ingham802f8b02010-06-30 05:02:46 +0000680 max_return_elements,
681 word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000682 matches);
683 }
684 }
685
686 return num_command_matches;
687
688}
689
690int
691CommandInterpreter::HandleCompletion (const char *current_line,
692 const char *cursor,
693 const char *last_char,
694 int match_start_point,
695 int max_return_elements,
696 StringList &matches)
697{
698 // We parse the argument up to the cursor, so the last argument in parsed_line is
699 // the one containing the cursor, and the cursor is after the last character.
700
701 Args parsed_line(current_line, last_char - current_line);
702 Args partial_parsed_line(current_line, cursor - current_line);
703
704 int num_args = partial_parsed_line.GetArgumentCount();
705 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
706 int cursor_char_position;
707
708 if (cursor_index == -1)
709 cursor_char_position = 0;
710 else
711 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
712
713 int num_command_matches;
714
715 matches.Clear();
716
717 // Only max_return_elements == -1 is supported at present:
718 assert (max_return_elements == -1);
Jim Ingham802f8b02010-06-30 05:02:46 +0000719 bool word_complete;
Greg Clayton63094e02010-06-23 01:19:29 +0000720 num_command_matches = HandleCompletionMatches (parsed_line,
721 cursor_index,
722 cursor_char_position,
723 match_start_point,
Jim Ingham802f8b02010-06-30 05:02:46 +0000724 max_return_elements,
725 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000726 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000727
728 if (num_command_matches <= 0)
729 return num_command_matches;
730
731 if (num_args == 0)
732 {
733 // If we got an empty string, insert nothing.
734 matches.InsertStringAtIndex(0, "");
735 }
736 else
737 {
738 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
739 // put an empty string in element 0.
740 std::string command_partial_str;
741 if (cursor_index >= 0)
742 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index), parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
743
744 std::string common_prefix;
745 matches.LongestCommonPrefix (common_prefix);
746 int partial_name_len = command_partial_str.size();
747
748 // If we matched a unique single command, add a space...
Jim Ingham802f8b02010-06-30 05:02:46 +0000749 // Only do this if the completer told us this was a complete word, however...
750 if (num_command_matches == 1 && word_complete)
Chris Lattner24943d22010-06-08 16:52:24 +0000751 {
752 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
753 if (quote_char != '\0')
754 common_prefix.push_back(quote_char);
755
756 common_prefix.push_back(' ');
757 }
758 common_prefix.erase (0, partial_name_len);
759 matches.InsertStringAtIndex(0, common_prefix.c_str());
760 }
761 return num_command_matches;
762}
763
Chris Lattner24943d22010-06-08 16:52:24 +0000764
765CommandInterpreter::~CommandInterpreter ()
766{
767}
768
769const char *
770CommandInterpreter::GetPrompt ()
771{
Caroline Tice5bc8c972010-09-20 20:44:43 +0000772 return m_debugger.GetPrompt();
Chris Lattner24943d22010-06-08 16:52:24 +0000773}
774
775void
776CommandInterpreter::SetPrompt (const char *new_prompt)
777{
Caroline Tice5bc8c972010-09-20 20:44:43 +0000778 m_debugger.SetPrompt (new_prompt);
Chris Lattner24943d22010-06-08 16:52:24 +0000779}
780
781void
782CommandInterpreter::CrossRegisterCommand (const char * dest_cmd, const char * object_type)
783{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000784 CommandObjectSP cmd_obj_sp = GetCommandSPExact (dest_cmd, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000785
786 if (cmd_obj_sp != NULL)
787 {
788 CommandObject *cmd_obj = cmd_obj_sp.get();
789 if (cmd_obj->IsCrossRefObject ())
790 cmd_obj->AddObject (object_type);
791 }
792}
793
Chris Lattner24943d22010-06-08 16:52:24 +0000794OptionArgVectorSP
795CommandInterpreter::GetAliasOptions (const char *alias_name)
796{
797 OptionArgMap::iterator pos;
798 OptionArgVectorSP ret_val;
799
800 std::string alias (alias_name);
801
802 if (HasAliasOptions())
803 {
804 pos = m_alias_options.find (alias);
805 if (pos != m_alias_options.end())
806 ret_val = pos->second;
807 }
808
809 return ret_val;
810}
811
812void
813CommandInterpreter::RemoveAliasOptions (const char *alias_name)
814{
815 OptionArgMap::iterator pos = m_alias_options.find(alias_name);
816 if (pos != m_alias_options.end())
817 {
818 m_alias_options.erase (pos);
819 }
820}
821
822void
823CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp)
824{
825 m_alias_options[alias_name] = option_arg_vector_sp;
826}
827
828bool
829CommandInterpreter::HasCommands ()
830{
831 return (!m_command_dict.empty());
832}
833
834bool
835CommandInterpreter::HasAliases ()
836{
837 return (!m_alias_dict.empty());
838}
839
840bool
841CommandInterpreter::HasUserCommands ()
842{
843 return (!m_user_dict.empty());
844}
845
846bool
847CommandInterpreter::HasAliasOptions ()
848{
849 return (!m_alias_options.empty());
850}
851
Chris Lattner24943d22010-06-08 16:52:24 +0000852void
853CommandInterpreter::BuildAliasCommandArgs
854(
855 CommandObject *alias_cmd_obj,
856 const char *alias_name,
857 Args &cmd_args,
858 CommandReturnObject &result
859)
860{
861 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
862
863 if (option_arg_vector_sp.get())
864 {
865 // Make sure that the alias name is the 0th element in cmd_args
866 std::string alias_name_str = alias_name;
867 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
868 cmd_args.Unshift (alias_name);
869
870 Args new_args (alias_cmd_obj->GetCommandName());
871 if (new_args.GetArgumentCount() == 2)
872 new_args.Shift();
873
874 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
875 int old_size = cmd_args.GetArgumentCount();
876 int *used = (int *) malloc ((old_size + 1) * sizeof (int));
877
878 memset (used, 0, (old_size + 1) * sizeof (int));
879 used[0] = 1;
880
881 for (int i = 0; i < option_arg_vector->size(); ++i)
882 {
883 OptionArgPair option_pair = (*option_arg_vector)[i];
884 std::string option = option_pair.first;
885 std::string value = option_pair.second;
886 if (option.compare ("<argument>") == 0)
887 new_args.AppendArgument (value.c_str());
888 else
889 {
890 new_args.AppendArgument (option.c_str());
891 if (value.compare ("<no-argument>") != 0)
892 {
893 int index = GetOptionArgumentPosition (value.c_str());
894 if (index == 0)
895 // value was NOT a positional argument; must be a real value
896 new_args.AppendArgument (value.c_str());
897 else if (index >= cmd_args.GetArgumentCount())
898 {
899 result.AppendErrorWithFormat
900 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
901 index);
902 result.SetStatus (eReturnStatusFailed);
903 return;
904 }
905 else
906 {
907 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
908 used[index] = 1;
909 }
910 }
911 }
912 }
913
914 for (int j = 0; j < cmd_args.GetArgumentCount(); ++j)
915 {
916 if (!used[j])
917 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
918 }
919
920 cmd_args.Clear();
921 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
922 }
923 else
924 {
925 result.SetStatus (eReturnStatusSuccessFinishNoResult);
926 // This alias was not created with any options; nothing further needs to be done.
927 return;
928 }
929
930 result.SetStatus (eReturnStatusSuccessFinishNoResult);
931 return;
932}
933
934
935int
936CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
937{
938 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
939 // of zero.
940
941 char *cptr = (char *) in_string;
942
943 // Does it start with '%'
944 if (cptr[0] == '%')
945 {
946 ++cptr;
947
948 // Is the rest of it entirely digits?
949 if (isdigit (cptr[0]))
950 {
951 const char *start = cptr;
952 while (isdigit (cptr[0]))
953 ++cptr;
954
955 // We've gotten to the end of the digits; are we at the end of the string?
956 if (cptr[0] == '\0')
957 position = atoi (start);
958 }
959 }
960
961 return position;
962}
963
964void
965CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
966{
967 const char *init_file_path = in_cwd ? "./.lldbinit" : "~/.lldbinit";
968 FileSpec init_file (init_file_path);
969 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
970 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
971
972 if (init_file.Exists())
973 {
974 char path[PATH_MAX];
975 init_file.GetPath(path, sizeof(path));
976 StreamString source_command;
Johnny Chen7c984242010-07-28 21:16:11 +0000977 source_command.Printf ("command source '%s'", path);
Chris Lattner24943d22010-06-08 16:52:24 +0000978 HandleCommand (source_command.GetData(), false, result);
979 }
980 else
981 {
982 // nothing to be done if the file doesn't exist
983 result.SetStatus(eReturnStatusSuccessFinishNoResult);
984 }
985}
986
987ScriptInterpreter *
988CommandInterpreter::GetScriptInterpreter ()
989{
Greg Clayton63094e02010-06-23 01:19:29 +0000990 CommandObject::CommandMap::iterator pos;
991
992 pos = m_command_dict.find ("script");
993 if (pos != m_command_dict.end())
Chris Lattner24943d22010-06-08 16:52:24 +0000994 {
Greg Clayton63094e02010-06-23 01:19:29 +0000995 CommandObject *script_cmd_obj = pos->second.get();
Greg Clayton238c0a12010-09-18 01:14:36 +0000996 return ((CommandObjectScript *) script_cmd_obj)->GetInterpreter ();
Chris Lattner24943d22010-06-08 16:52:24 +0000997 }
Greg Clayton63094e02010-06-23 01:19:29 +0000998 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000999}
1000
1001
1002
1003bool
1004CommandInterpreter::GetSynchronous ()
1005{
1006 return m_synchronous_execution;
1007}
1008
1009void
1010CommandInterpreter::SetSynchronous (bool value)
1011{
1012 static bool value_set_once = false;
1013 if (!value_set_once)
1014 {
1015 value_set_once = true;
1016 m_synchronous_execution = value;
1017 }
1018}
1019
1020void
1021CommandInterpreter::OutputFormattedHelpText (Stream &strm,
1022 const char *word_text,
1023 const char *separator,
1024 const char *help_text,
1025 uint32_t max_word_len)
1026{
Greg Clayton238c0a12010-09-18 01:14:36 +00001027 const uint32_t max_columns = m_debugger.GetTerminalWidth();
1028
Chris Lattner24943d22010-06-08 16:52:24 +00001029 int indent_size = max_word_len + strlen (separator) + 2;
1030
1031 strm.IndentMore (indent_size);
1032
1033 int len = indent_size + strlen (help_text) + 1;
1034 char *text = (char *) malloc (len);
1035 sprintf (text, "%-*s %s %s", max_word_len, word_text, separator, help_text);
1036 if (text[len - 1] == '\n')
1037 text[--len] = '\0';
1038
1039 if (len < max_columns)
1040 {
1041 // Output it as a single line.
1042 strm.Printf ("%s", text);
1043 }
1044 else
1045 {
1046 // We need to break it up into multiple lines.
1047 bool first_line = true;
1048 int text_width;
1049 int start = 0;
1050 int end = start;
1051 int final_end = strlen (text);
1052 int sub_len;
1053
1054 while (end < final_end)
1055 {
1056 if (first_line)
1057 text_width = max_columns - 1;
1058 else
1059 text_width = max_columns - indent_size - 1;
1060
1061 // Don't start the 'text' on a space, since we're already outputting the indentation.
1062 if (!first_line)
1063 {
1064 while ((start < final_end) && (text[start] == ' '))
1065 start++;
1066 }
1067
1068 end = start + text_width;
1069 if (end > final_end)
1070 end = final_end;
1071 else
1072 {
1073 // If we're not at the end of the text, make sure we break the line on white space.
1074 while (end > start
1075 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
1076 end--;
1077 }
1078
1079 sub_len = end - start;
1080 if (start != 0)
1081 strm.EOL();
1082 if (!first_line)
1083 strm.Indent();
1084 else
1085 first_line = false;
1086 assert (start <= final_end);
1087 assert (start + sub_len <= final_end);
1088 if (sub_len > 0)
1089 strm.Write (text + start, sub_len);
1090 start = end + 1;
1091 }
1092 }
1093 strm.EOL();
1094 strm.IndentLess(indent_size);
1095 free (text);
1096}
1097
1098void
1099CommandInterpreter::AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word,
1100 StringList &commands_found, StringList &commands_help)
1101{
1102 CommandObject::CommandMap::const_iterator pos;
1103 CommandObject::CommandMap sub_cmd_dict = ((CommandObjectMultiword *) cmd_obj)->m_subcommand_dict;
1104 CommandObject *sub_cmd_obj;
1105
1106 for (pos = sub_cmd_dict.begin(); pos != sub_cmd_dict.end(); ++pos)
1107 {
1108 const char * command_name = pos->first.c_str();
1109 sub_cmd_obj = pos->second.get();
1110 StreamString complete_command_name;
1111
1112 complete_command_name.Printf ("%s %s", prefix, command_name);
1113
Greg Clayton238c0a12010-09-18 01:14:36 +00001114 if (sub_cmd_obj->HelpTextContainsWord (search_word))
Chris Lattner24943d22010-06-08 16:52:24 +00001115 {
1116 commands_found.AppendString (complete_command_name.GetData());
1117 commands_help.AppendString (sub_cmd_obj->GetHelp());
1118 }
1119
1120 if (sub_cmd_obj->IsMultiwordObject())
1121 AproposAllSubCommands (sub_cmd_obj, complete_command_name.GetData(), search_word, commands_found,
1122 commands_help);
1123 }
1124
1125}
1126
1127void
1128CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
1129 StringList &commands_help)
1130{
1131 CommandObject::CommandMap::const_iterator pos;
1132
1133 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1134 {
1135 const char *command_name = pos->first.c_str();
1136 CommandObject *cmd_obj = pos->second.get();
1137
Greg Clayton238c0a12010-09-18 01:14:36 +00001138 if (cmd_obj->HelpTextContainsWord (search_word))
Chris Lattner24943d22010-06-08 16:52:24 +00001139 {
1140 commands_found.AppendString (command_name);
1141 commands_help.AppendString (cmd_obj->GetHelp());
1142 }
1143
1144 if (cmd_obj->IsMultiwordObject())
1145 AproposAllSubCommands (cmd_obj, command_name, search_word, commands_found, commands_help);
1146
1147 }
1148}