blob: 48176f76b28731338c614f916747c5ab6c7ee218 [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 Tice6e4c5ce2010-09-04 00:03:46 +0000141 lldb::ScriptLanguage script_language;
142 lldb::SettableVariableType var_type = lldb::eSetVarTypeString;
143 StringList value;
144 const char *dbg_name = GetDebugger().GetInstanceName().AsCString();
145 StreamString var_name;
146 var_name.Printf ("[%s].script-lang", dbg_name);
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000147 value = Debugger::GetSettingsController()->GetVariable (var_name.GetData(), var_type,
148 m_debugger.GetInstanceName().AsCString());
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000149 bool success;
150 script_language = Args::StringToScriptLanguage (value.GetStringAtIndex(0), lldb::eScriptLanguageDefault, &success);
151
Greg Clayton238c0a12010-09-18 01:14:36 +0000152 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000153 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000154 //m_command_dict["call"] = CommandObjectSP (new CommandObjectCall (*this));
Jim Ingham767af882010-07-07 03:36:20 +0000155 m_command_dict["commands"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000156 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this));
157 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this));
158 m_command_dict["file"] = CommandObjectSP (new CommandObjectFile (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000159 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000160 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000161 m_command_dict["image"] = CommandObjectSP (new CommandObjectImage (*this));
162 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
163 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
164 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000165 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000166 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
Greg Clayton238c0a12010-09-18 01:14:36 +0000167 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000168 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
Jim Ingham767af882010-07-07 03:36:20 +0000169 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
Greg Clayton63094e02010-06-23 01:19:29 +0000170 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
171 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000172
173 std::auto_ptr<CommandObjectRegexCommand>
Greg Clayton238c0a12010-09-18 01:14:36 +0000174 break_regex_cmd_ap(new CommandObjectRegexCommand (*this,
175 "regexp-break",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000176 "Set a breakpoint using a regular expression to specify the location.",
Chris Lattner24943d22010-06-08 16:52:24 +0000177 "regexp-break [<file>:<line>]\nregexp-break [<address>]\nregexp-break <...>", 2));
178 if (break_regex_cmd_ap.get())
179 {
180 if (break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2") &&
181 break_regex_cmd_ap->AddRegexCommand("^(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1") &&
182 break_regex_cmd_ap->AddRegexCommand("^[\"']?([-+]\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'") &&
183 break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list") &&
184 break_regex_cmd_ap->AddRegexCommand("^(-.*)$", "breakpoint set %1") &&
185 break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1'"))
186 {
187 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
188 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
189 }
190 }
191}
192
193int
194CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
195 StringList &matches)
196{
197 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
198
199 if (include_aliases)
200 {
201 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
202 }
203
204 return matches.GetSize();
205}
206
207CommandObjectSP
208CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
209{
210 CommandObject::CommandMap::iterator pos;
211 CommandObjectSP ret_val;
212
213 std::string cmd(cmd_cstr);
214
215 if (HasCommands())
216 {
217 pos = m_command_dict.find(cmd);
218 if (pos != m_command_dict.end())
219 ret_val = pos->second;
220 }
221
222 if (include_aliases && HasAliases())
223 {
224 pos = m_alias_dict.find(cmd);
225 if (pos != m_alias_dict.end())
226 ret_val = pos->second;
227 }
228
229 if (HasUserCommands())
230 {
231 pos = m_user_dict.find(cmd);
232 if (pos != m_user_dict.end())
233 ret_val = pos->second;
234 }
235
236 if (!exact && ret_val == NULL)
237 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000238 // We will only get into here if we didn't find any exact matches.
239
240 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
241
Chris Lattner24943d22010-06-08 16:52:24 +0000242 StringList local_matches;
243 if (matches == NULL)
244 matches = &local_matches;
245
Jim Inghamd40f8a62010-07-06 22:46:59 +0000246 unsigned int num_cmd_matches = 0;
247 unsigned int num_alias_matches = 0;
248 unsigned int num_user_matches = 0;
249
250 // Look through the command dictionaries one by one, and if we get only one match from any of
251 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
252
Chris Lattner24943d22010-06-08 16:52:24 +0000253 if (HasCommands())
254 {
255 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
256 }
257
258 if (num_cmd_matches == 1)
259 {
260 cmd.assign(matches->GetStringAtIndex(0));
261 pos = m_command_dict.find(cmd);
262 if (pos != m_command_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000263 real_match_sp = pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000264 }
265
Jim Ingham9a574172010-06-24 20:28:42 +0000266 if (include_aliases && HasAliases())
Chris Lattner24943d22010-06-08 16:52:24 +0000267 {
268 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
269
270 }
271
Jim Inghamd40f8a62010-07-06 22:46:59 +0000272 if (num_alias_matches == 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000273 {
274 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
275 pos = m_alias_dict.find(cmd);
276 if (pos != m_alias_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000277 alias_match_sp = pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000278 }
279
Jim Ingham9a574172010-06-24 20:28:42 +0000280 if (HasUserCommands())
Chris Lattner24943d22010-06-08 16:52:24 +0000281 {
282 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
283 }
284
Jim Inghamd40f8a62010-07-06 22:46:59 +0000285 if (num_user_matches == 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000286 {
287 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
288
289 pos = m_user_dict.find (cmd);
290 if (pos != m_user_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000291 user_match_sp = pos->second;
292 }
293
294 // If we got exactly one match, return that, otherwise return the match list.
295
296 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
297 {
298 if (num_cmd_matches)
299 return real_match_sp;
300 else if (num_alias_matches)
301 return alias_match_sp;
302 else
303 return user_match_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000304 }
305 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000306 else if (matches && ret_val != NULL)
307 {
308 matches->AppendString (cmd_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +0000309 }
310
311
312 return ret_val;
313}
314
Jim Inghamd40f8a62010-07-06 22:46:59 +0000315CommandObjectSP
316CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
Chris Lattner24943d22010-06-08 16:52:24 +0000317{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000318 return GetCommandSP(cmd_cstr, include_aliases, true, NULL);
319}
320
321CommandObject *
322CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
323{
324 return GetCommandSPExact (cmd_cstr, include_aliases).get();
325}
326
327CommandObject *
328CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
329{
330 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
331
332 // If we didn't find an exact match to the command string in the commands, look in
333 // the aliases.
334
335 if (command_obj == NULL)
336 {
337 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
338 }
339
340 // Finally, if there wasn't an exact match among the aliases, look for an inexact match
341 // in both the commands and the aliases.
342
343 if (command_obj == NULL)
344 command_obj = GetCommandSP(cmd_cstr, true, false, matches).get();
345
346 return command_obj;
Chris Lattner24943d22010-06-08 16:52:24 +0000347}
348
349bool
350CommandInterpreter::CommandExists (const char *cmd)
351{
352 return m_command_dict.find(cmd) != m_command_dict.end();
353}
354
355bool
356CommandInterpreter::AliasExists (const char *cmd)
357{
358 return m_alias_dict.find(cmd) != m_alias_dict.end();
359}
360
361bool
362CommandInterpreter::UserCommandExists (const char *cmd)
363{
364 return m_user_dict.find(cmd) != m_user_dict.end();
365}
366
367void
368CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
369{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000370 command_obj_sp->SetIsAlias (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000371 m_alias_dict[alias_name] = command_obj_sp;
372}
373
374bool
375CommandInterpreter::RemoveAlias (const char *alias_name)
376{
377 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
378 if (pos != m_alias_dict.end())
379 {
380 m_alias_dict.erase(pos);
381 return true;
382 }
383 return false;
384}
385bool
386CommandInterpreter::RemoveUser (const char *alias_name)
387{
388 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
389 if (pos != m_user_dict.end())
390 {
391 m_user_dict.erase(pos);
392 return true;
393 }
394 return false;
395}
396
Chris Lattner24943d22010-06-08 16:52:24 +0000397void
398CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
399{
400 help_string.Printf ("'%s", command_name);
401 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
402
403 if (option_arg_vector_sp != NULL)
404 {
405 OptionArgVector *options = option_arg_vector_sp.get();
406 for (int i = 0; i < options->size(); ++i)
407 {
408 OptionArgPair cur_option = (*options)[i];
409 std::string opt = cur_option.first;
410 std::string value = cur_option.second;
411 if (opt.compare("<argument>") == 0)
412 {
413 help_string.Printf (" %s", value.c_str());
414 }
415 else
416 {
417 help_string.Printf (" %s", opt.c_str());
418 if ((value.compare ("<no-argument>") != 0)
419 && (value.compare ("<need-argument") != 0))
420 {
421 help_string.Printf (" %s", value.c_str());
422 }
423 }
424 }
425 }
426
427 help_string.Printf ("'");
428}
429
Greg Clayton65124ea2010-08-26 22:05:43 +0000430size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000431CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
432{
433 CommandObject::CommandMap::const_iterator pos;
Greg Clayton65124ea2010-08-26 22:05:43 +0000434 CommandObject::CommandMap::const_iterator end = dict.end();
435 size_t max_len = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000436
Greg Clayton65124ea2010-08-26 22:05:43 +0000437 for (pos = dict.begin(); pos != end; ++pos)
438 {
439 size_t len = pos->first.size();
440 if (max_len < len)
441 max_len = len;
Chris Lattner24943d22010-06-08 16:52:24 +0000442 }
Greg Clayton65124ea2010-08-26 22:05:43 +0000443 return max_len;
Chris Lattner24943d22010-06-08 16:52:24 +0000444}
445
446void
447CommandInterpreter::GetHelp (CommandReturnObject &result)
448{
449 CommandObject::CommandMap::const_iterator pos;
450 result.AppendMessage("The following is a list of built-in, permanent debugger commands:");
451 result.AppendMessage("");
Greg Clayton65124ea2010-08-26 22:05:43 +0000452 uint32_t max_len = FindLongestCommandWord (m_command_dict);
Chris Lattner24943d22010-06-08 16:52:24 +0000453
454 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
455 {
456 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
457 max_len);
458 }
459 result.AppendMessage("");
460
461 if (m_alias_dict.size() > 0)
462 {
Caroline Tice00edd3a2010-09-13 05:27:16 +0000463 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 +0000464 result.AppendMessage("");
Greg Clayton65124ea2010-08-26 22:05:43 +0000465 max_len = FindLongestCommandWord (m_alias_dict);
466
Chris Lattner24943d22010-06-08 16:52:24 +0000467 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
468 {
469 StreamString sstr;
470 StreamString translation_and_help;
471 std::string entry_name = pos->first;
472 std::string second_entry = pos->second.get()->GetCommandName();
473 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
474
475 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
476 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
477 translation_and_help.GetData(), max_len);
478 }
479 result.AppendMessage("");
480 }
481
482 if (m_user_dict.size() > 0)
483 {
484 result.AppendMessage ("The following is a list of your current user-defined commands:");
485 result.AppendMessage("");
486 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
487 {
488 result.AppendMessageWithFormat ("%s -- %s\n", pos->first.c_str(), pos->second->GetHelp());
489 }
490 result.AppendMessage("");
491 }
492
493 result.AppendMessage("For more information on any particular command, try 'help <command-name>'.");
494}
495
Chris Lattner24943d22010-06-08 16:52:24 +0000496bool
Greg Clayton63094e02010-06-23 01:19:29 +0000497CommandInterpreter::HandleCommand
498(
499 const char *command_line,
500 bool add_to_history,
501 CommandReturnObject &result,
502 ExecutionContext *override_context
503)
Chris Lattner24943d22010-06-08 16:52:24 +0000504{
505 // FIXME: there should probably be a mutex to make sure only one thread can
506 // run the interpreter at a time.
507
508 // TODO: this should be a logging channel in lldb.
509// if (DebugSelf())
510// {
511// result.AppendMessageWithFormat ("Processing command: %s\n", command_line);
512// }
513
Greg Clayton63094e02010-06-23 01:19:29 +0000514 m_debugger.UpdateExecutionContext (override_context);
Chris Lattner24943d22010-06-08 16:52:24 +0000515
516 if (command_line == NULL || command_line[0] == '\0')
517 {
518 if (m_command_history.empty())
519 {
520 result.AppendError ("empty command");
521 result.SetStatus(eReturnStatusFailed);
522 return false;
523 }
524 else
525 {
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000526 command_line = m_repeat_command.c_str();
527 if (m_repeat_command.empty())
528 {
Jim Ingham767af882010-07-07 03:36:20 +0000529 result.AppendErrorWithFormat("No auto repeat.\n");
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000530 result.SetStatus (eReturnStatusFailed);
531 return false;
532 }
Chris Lattner24943d22010-06-08 16:52:24 +0000533 }
534 add_to_history = false;
535 }
536
537 Args command_args(command_line);
538
539 if (command_args.GetArgumentCount() > 0)
540 {
541 const char *command_cstr = command_args.GetArgumentAtIndex(0);
542 if (command_cstr)
543 {
544
545 // We're looking up the command object here. So first find an exact match to the
546 // command in the commands.
Jim Inghamd40f8a62010-07-06 22:46:59 +0000547 CommandObject *command_obj = GetCommandObject(command_cstr);
548
549 if (command_obj != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000550 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000551 if (command_obj->IsAlias())
Chris Lattner24943d22010-06-08 16:52:24 +0000552 {
553 BuildAliasCommandArgs (command_obj, command_cstr, command_args, result);
554 if (!result.Succeeded())
555 return false;
556 }
Chris Lattner24943d22010-06-08 16:52:24 +0000557
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000558 if (add_to_history)
559 {
Jim Ingham767af882010-07-07 03:36:20 +0000560 const char *repeat_command = command_obj->GetRepeatCommand(command_args, 0);
561 if (repeat_command != NULL)
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000562 m_repeat_command.assign(repeat_command);
563 else
Jim Ingham767af882010-07-07 03:36:20 +0000564 m_repeat_command.assign(command_line);
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000565
566 m_command_history.push_back (command_line);
567 }
568
569
Chris Lattner24943d22010-06-08 16:52:24 +0000570 if (command_obj->WantsRawCommandString())
571 {
572 const char *stripped_command = ::strstr (command_line, command_cstr);
573 if (stripped_command)
574 {
575 stripped_command += strlen(command_cstr);
576 while (isspace(*stripped_command))
577 ++stripped_command;
Greg Clayton238c0a12010-09-18 01:14:36 +0000578 command_obj->ExecuteRawCommandString (stripped_command, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000579 }
580 }
581 else
582 {
Chris Lattner24943d22010-06-08 16:52:24 +0000583 // Remove the command from the args.
584 command_args.Shift();
Greg Clayton238c0a12010-09-18 01:14:36 +0000585 command_obj->ExecuteWithOptions (command_args, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000586 }
587 }
588 else
589 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000590 // We didn't find the first command object, so complete the first argument.
Chris Lattner24943d22010-06-08 16:52:24 +0000591 StringList matches;
592 int num_matches;
Jim Inghamd40f8a62010-07-06 22:46:59 +0000593 int cursor_index = 0;
594 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
Jim Ingham802f8b02010-06-30 05:02:46 +0000595 bool word_complete;
Greg Clayton63094e02010-06-23 01:19:29 +0000596 num_matches = HandleCompletionMatches (command_args,
597 cursor_index,
598 cursor_char_position,
599 0,
600 -1,
Jim Ingham802f8b02010-06-30 05:02:46 +0000601 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000602 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000603
604 if (num_matches > 0)
605 {
606 std::string error_msg;
607 error_msg.assign ("ambiguous command '");
608 error_msg.append(command_cstr);
609 error_msg.append ("'.");
610
611 error_msg.append (" Possible completions:");
612 for (int i = 0; i < num_matches; i++)
613 {
614 error_msg.append ("\n\t");
615 error_msg.append (matches.GetStringAtIndex (i));
616 }
617 error_msg.append ("\n");
618 result.AppendRawError (error_msg.c_str(), error_msg.size());
619 }
620 else
621 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_cstr);
622
623 result.SetStatus (eReturnStatusFailed);
624 }
625 }
626 }
627 return result.Succeeded();
628}
629
630int
631CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
632 int &cursor_index,
633 int &cursor_char_position,
634 int match_start_point,
635 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000636 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000637 StringList &matches)
638{
639 int num_command_matches = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000640 bool look_for_subcommand = false;
Jim Ingham802f8b02010-06-30 05:02:46 +0000641
642 // For any of the command completions a unique match will be a complete word.
643 word_complete = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000644
645 if (cursor_index == -1)
646 {
647 // We got nothing on the command line, so return the list of commands
Jim Inghamd40f8a62010-07-06 22:46:59 +0000648 bool include_aliases = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000649 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
650 }
651 else if (cursor_index == 0)
652 {
653 // The cursor is in the first argument, so just do a lookup in the dictionary.
Jim Inghamd40f8a62010-07-06 22:46:59 +0000654 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000655 num_command_matches = matches.GetSize();
656
657 if (num_command_matches == 1
658 && cmd_obj && cmd_obj->IsMultiwordObject()
659 && matches.GetStringAtIndex(0) != NULL
660 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
661 {
662 look_for_subcommand = true;
663 num_command_matches = 0;
664 matches.DeleteStringAtIndex(0);
665 parsed_line.AppendArgument ("");
666 cursor_index++;
667 cursor_char_position = 0;
668 }
669 }
670
671 if (cursor_index > 0 || look_for_subcommand)
672 {
673 // We are completing further on into a commands arguments, so find the command and tell it
674 // to complete the command.
675 // First see if there is a matching initial command:
Jim Inghamd40f8a62010-07-06 22:46:59 +0000676 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000677 if (command_object == NULL)
678 {
679 return 0;
680 }
681 else
682 {
683 parsed_line.Shift();
684 cursor_index--;
Greg Clayton238c0a12010-09-18 01:14:36 +0000685 num_command_matches = command_object->HandleCompletion (parsed_line,
Greg Clayton63094e02010-06-23 01:19:29 +0000686 cursor_index,
687 cursor_char_position,
688 match_start_point,
Jim Ingham802f8b02010-06-30 05:02:46 +0000689 max_return_elements,
690 word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000691 matches);
692 }
693 }
694
695 return num_command_matches;
696
697}
698
699int
700CommandInterpreter::HandleCompletion (const char *current_line,
701 const char *cursor,
702 const char *last_char,
703 int match_start_point,
704 int max_return_elements,
705 StringList &matches)
706{
707 // We parse the argument up to the cursor, so the last argument in parsed_line is
708 // the one containing the cursor, and the cursor is after the last character.
709
710 Args parsed_line(current_line, last_char - current_line);
711 Args partial_parsed_line(current_line, cursor - current_line);
712
713 int num_args = partial_parsed_line.GetArgumentCount();
714 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
715 int cursor_char_position;
716
717 if (cursor_index == -1)
718 cursor_char_position = 0;
719 else
720 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
721
722 int num_command_matches;
723
724 matches.Clear();
725
726 // Only max_return_elements == -1 is supported at present:
727 assert (max_return_elements == -1);
Jim Ingham802f8b02010-06-30 05:02:46 +0000728 bool word_complete;
Greg Clayton63094e02010-06-23 01:19:29 +0000729 num_command_matches = HandleCompletionMatches (parsed_line,
730 cursor_index,
731 cursor_char_position,
732 match_start_point,
Jim Ingham802f8b02010-06-30 05:02:46 +0000733 max_return_elements,
734 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000735 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000736
737 if (num_command_matches <= 0)
738 return num_command_matches;
739
740 if (num_args == 0)
741 {
742 // If we got an empty string, insert nothing.
743 matches.InsertStringAtIndex(0, "");
744 }
745 else
746 {
747 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
748 // put an empty string in element 0.
749 std::string command_partial_str;
750 if (cursor_index >= 0)
751 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index), parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
752
753 std::string common_prefix;
754 matches.LongestCommonPrefix (common_prefix);
755 int partial_name_len = command_partial_str.size();
756
757 // If we matched a unique single command, add a space...
Jim Ingham802f8b02010-06-30 05:02:46 +0000758 // Only do this if the completer told us this was a complete word, however...
759 if (num_command_matches == 1 && word_complete)
Chris Lattner24943d22010-06-08 16:52:24 +0000760 {
761 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
762 if (quote_char != '\0')
763 common_prefix.push_back(quote_char);
764
765 common_prefix.push_back(' ');
766 }
767 common_prefix.erase (0, partial_name_len);
768 matches.InsertStringAtIndex(0, common_prefix.c_str());
769 }
770 return num_command_matches;
771}
772
Chris Lattner24943d22010-06-08 16:52:24 +0000773
774CommandInterpreter::~CommandInterpreter ()
775{
776}
777
778const char *
779CommandInterpreter::GetPrompt ()
780{
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000781 lldb::SettableVariableType var_type;
782 const char *instance_name = GetDebugger().GetInstanceName().AsCString();
783 StreamString var_name;
784 var_name.Printf ("[%s].prompt", instance_name);
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000785 return Debugger::GetSettingsController()->GetVariable (var_name.GetData(), var_type, instance_name).GetStringAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000786}
787
788void
789CommandInterpreter::SetPrompt (const char *new_prompt)
790{
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000791 const char *instance_name = GetDebugger().GetInstanceName().AsCString();
792 StreamString name_str;
793 name_str.Printf ("[%s].prompt", instance_name);
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000794 Debugger::GetSettingsController()->SetVariable (name_str.GetData(), new_prompt, lldb::eVarSetOperationAssign,
795 false, m_debugger.GetInstanceName().AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000796}
797
798void
799CommandInterpreter::CrossRegisterCommand (const char * dest_cmd, const char * object_type)
800{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000801 CommandObjectSP cmd_obj_sp = GetCommandSPExact (dest_cmd, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000802
803 if (cmd_obj_sp != NULL)
804 {
805 CommandObject *cmd_obj = cmd_obj_sp.get();
806 if (cmd_obj->IsCrossRefObject ())
807 cmd_obj->AddObject (object_type);
808 }
809}
810
Chris Lattner24943d22010-06-08 16:52:24 +0000811OptionArgVectorSP
812CommandInterpreter::GetAliasOptions (const char *alias_name)
813{
814 OptionArgMap::iterator pos;
815 OptionArgVectorSP ret_val;
816
817 std::string alias (alias_name);
818
819 if (HasAliasOptions())
820 {
821 pos = m_alias_options.find (alias);
822 if (pos != m_alias_options.end())
823 ret_val = pos->second;
824 }
825
826 return ret_val;
827}
828
829void
830CommandInterpreter::RemoveAliasOptions (const char *alias_name)
831{
832 OptionArgMap::iterator pos = m_alias_options.find(alias_name);
833 if (pos != m_alias_options.end())
834 {
835 m_alias_options.erase (pos);
836 }
837}
838
839void
840CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp)
841{
842 m_alias_options[alias_name] = option_arg_vector_sp;
843}
844
845bool
846CommandInterpreter::HasCommands ()
847{
848 return (!m_command_dict.empty());
849}
850
851bool
852CommandInterpreter::HasAliases ()
853{
854 return (!m_alias_dict.empty());
855}
856
857bool
858CommandInterpreter::HasUserCommands ()
859{
860 return (!m_user_dict.empty());
861}
862
863bool
864CommandInterpreter::HasAliasOptions ()
865{
866 return (!m_alias_options.empty());
867}
868
Chris Lattner24943d22010-06-08 16:52:24 +0000869void
870CommandInterpreter::BuildAliasCommandArgs
871(
872 CommandObject *alias_cmd_obj,
873 const char *alias_name,
874 Args &cmd_args,
875 CommandReturnObject &result
876)
877{
878 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
879
880 if (option_arg_vector_sp.get())
881 {
882 // Make sure that the alias name is the 0th element in cmd_args
883 std::string alias_name_str = alias_name;
884 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
885 cmd_args.Unshift (alias_name);
886
887 Args new_args (alias_cmd_obj->GetCommandName());
888 if (new_args.GetArgumentCount() == 2)
889 new_args.Shift();
890
891 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
892 int old_size = cmd_args.GetArgumentCount();
893 int *used = (int *) malloc ((old_size + 1) * sizeof (int));
894
895 memset (used, 0, (old_size + 1) * sizeof (int));
896 used[0] = 1;
897
898 for (int i = 0; i < option_arg_vector->size(); ++i)
899 {
900 OptionArgPair option_pair = (*option_arg_vector)[i];
901 std::string option = option_pair.first;
902 std::string value = option_pair.second;
903 if (option.compare ("<argument>") == 0)
904 new_args.AppendArgument (value.c_str());
905 else
906 {
907 new_args.AppendArgument (option.c_str());
908 if (value.compare ("<no-argument>") != 0)
909 {
910 int index = GetOptionArgumentPosition (value.c_str());
911 if (index == 0)
912 // value was NOT a positional argument; must be a real value
913 new_args.AppendArgument (value.c_str());
914 else if (index >= cmd_args.GetArgumentCount())
915 {
916 result.AppendErrorWithFormat
917 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
918 index);
919 result.SetStatus (eReturnStatusFailed);
920 return;
921 }
922 else
923 {
924 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
925 used[index] = 1;
926 }
927 }
928 }
929 }
930
931 for (int j = 0; j < cmd_args.GetArgumentCount(); ++j)
932 {
933 if (!used[j])
934 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
935 }
936
937 cmd_args.Clear();
938 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
939 }
940 else
941 {
942 result.SetStatus (eReturnStatusSuccessFinishNoResult);
943 // This alias was not created with any options; nothing further needs to be done.
944 return;
945 }
946
947 result.SetStatus (eReturnStatusSuccessFinishNoResult);
948 return;
949}
950
951
952int
953CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
954{
955 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
956 // of zero.
957
958 char *cptr = (char *) in_string;
959
960 // Does it start with '%'
961 if (cptr[0] == '%')
962 {
963 ++cptr;
964
965 // Is the rest of it entirely digits?
966 if (isdigit (cptr[0]))
967 {
968 const char *start = cptr;
969 while (isdigit (cptr[0]))
970 ++cptr;
971
972 // We've gotten to the end of the digits; are we at the end of the string?
973 if (cptr[0] == '\0')
974 position = atoi (start);
975 }
976 }
977
978 return position;
979}
980
981void
982CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
983{
984 const char *init_file_path = in_cwd ? "./.lldbinit" : "~/.lldbinit";
985 FileSpec init_file (init_file_path);
986 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
987 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
988
989 if (init_file.Exists())
990 {
991 char path[PATH_MAX];
992 init_file.GetPath(path, sizeof(path));
993 StreamString source_command;
Johnny Chen7c984242010-07-28 21:16:11 +0000994 source_command.Printf ("command source '%s'", path);
Chris Lattner24943d22010-06-08 16:52:24 +0000995 HandleCommand (source_command.GetData(), false, result);
996 }
997 else
998 {
999 // nothing to be done if the file doesn't exist
1000 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1001 }
1002}
1003
1004ScriptInterpreter *
1005CommandInterpreter::GetScriptInterpreter ()
1006{
Greg Clayton63094e02010-06-23 01:19:29 +00001007 CommandObject::CommandMap::iterator pos;
1008
1009 pos = m_command_dict.find ("script");
1010 if (pos != m_command_dict.end())
Chris Lattner24943d22010-06-08 16:52:24 +00001011 {
Greg Clayton63094e02010-06-23 01:19:29 +00001012 CommandObject *script_cmd_obj = pos->second.get();
Greg Clayton238c0a12010-09-18 01:14:36 +00001013 return ((CommandObjectScript *) script_cmd_obj)->GetInterpreter ();
Chris Lattner24943d22010-06-08 16:52:24 +00001014 }
Greg Clayton63094e02010-06-23 01:19:29 +00001015 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +00001016}
1017
1018
1019
1020bool
1021CommandInterpreter::GetSynchronous ()
1022{
1023 return m_synchronous_execution;
1024}
1025
1026void
1027CommandInterpreter::SetSynchronous (bool value)
1028{
1029 static bool value_set_once = false;
1030 if (!value_set_once)
1031 {
1032 value_set_once = true;
1033 m_synchronous_execution = value;
1034 }
1035}
1036
1037void
1038CommandInterpreter::OutputFormattedHelpText (Stream &strm,
1039 const char *word_text,
1040 const char *separator,
1041 const char *help_text,
1042 uint32_t max_word_len)
1043{
Greg Clayton238c0a12010-09-18 01:14:36 +00001044 const uint32_t max_columns = m_debugger.GetTerminalWidth();
1045
Chris Lattner24943d22010-06-08 16:52:24 +00001046 int indent_size = max_word_len + strlen (separator) + 2;
1047
1048 strm.IndentMore (indent_size);
1049
1050 int len = indent_size + strlen (help_text) + 1;
1051 char *text = (char *) malloc (len);
1052 sprintf (text, "%-*s %s %s", max_word_len, word_text, separator, help_text);
1053 if (text[len - 1] == '\n')
1054 text[--len] = '\0';
1055
1056 if (len < max_columns)
1057 {
1058 // Output it as a single line.
1059 strm.Printf ("%s", text);
1060 }
1061 else
1062 {
1063 // We need to break it up into multiple lines.
1064 bool first_line = true;
1065 int text_width;
1066 int start = 0;
1067 int end = start;
1068 int final_end = strlen (text);
1069 int sub_len;
1070
1071 while (end < final_end)
1072 {
1073 if (first_line)
1074 text_width = max_columns - 1;
1075 else
1076 text_width = max_columns - indent_size - 1;
1077
1078 // Don't start the 'text' on a space, since we're already outputting the indentation.
1079 if (!first_line)
1080 {
1081 while ((start < final_end) && (text[start] == ' '))
1082 start++;
1083 }
1084
1085 end = start + text_width;
1086 if (end > final_end)
1087 end = final_end;
1088 else
1089 {
1090 // If we're not at the end of the text, make sure we break the line on white space.
1091 while (end > start
1092 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
1093 end--;
1094 }
1095
1096 sub_len = end - start;
1097 if (start != 0)
1098 strm.EOL();
1099 if (!first_line)
1100 strm.Indent();
1101 else
1102 first_line = false;
1103 assert (start <= final_end);
1104 assert (start + sub_len <= final_end);
1105 if (sub_len > 0)
1106 strm.Write (text + start, sub_len);
1107 start = end + 1;
1108 }
1109 }
1110 strm.EOL();
1111 strm.IndentLess(indent_size);
1112 free (text);
1113}
1114
1115void
1116CommandInterpreter::AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word,
1117 StringList &commands_found, StringList &commands_help)
1118{
1119 CommandObject::CommandMap::const_iterator pos;
1120 CommandObject::CommandMap sub_cmd_dict = ((CommandObjectMultiword *) cmd_obj)->m_subcommand_dict;
1121 CommandObject *sub_cmd_obj;
1122
1123 for (pos = sub_cmd_dict.begin(); pos != sub_cmd_dict.end(); ++pos)
1124 {
1125 const char * command_name = pos->first.c_str();
1126 sub_cmd_obj = pos->second.get();
1127 StreamString complete_command_name;
1128
1129 complete_command_name.Printf ("%s %s", prefix, command_name);
1130
Greg Clayton238c0a12010-09-18 01:14:36 +00001131 if (sub_cmd_obj->HelpTextContainsWord (search_word))
Chris Lattner24943d22010-06-08 16:52:24 +00001132 {
1133 commands_found.AppendString (complete_command_name.GetData());
1134 commands_help.AppendString (sub_cmd_obj->GetHelp());
1135 }
1136
1137 if (sub_cmd_obj->IsMultiwordObject())
1138 AproposAllSubCommands (sub_cmd_obj, complete_command_name.GetData(), search_word, commands_found,
1139 commands_help);
1140 }
1141
1142}
1143
1144void
1145CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
1146 StringList &commands_help)
1147{
1148 CommandObject::CommandMap::const_iterator pos;
1149
1150 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1151 {
1152 const char *command_name = pos->first.c_str();
1153 CommandObject *cmd_obj = pos->second.get();
1154
Greg Clayton238c0a12010-09-18 01:14:36 +00001155 if (cmd_obj->HelpTextContainsWord (search_word))
Chris Lattner24943d22010-06-08 16:52:24 +00001156 {
1157 commands_found.AppendString (command_name);
1158 commands_help.AppendString (cmd_obj->GetHelp());
1159 }
1160
1161 if (cmd_obj->IsMultiwordObject())
1162 AproposAllSubCommands (cmd_obj, command_name, search_word, commands_found, commands_help);
1163
1164 }
1165}