blob: 85622069236c19b37795d8710e32579a23411972 [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
Chris Lattner24943d22010-06-08 16:52:24 +0000152 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos ());
Greg Clayton63094e02010-06-23 01:19:29 +0000153 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
Sean Callanan65dafa82010-08-27 01:01:44 +0000154 //m_command_dict["call"] = CommandObjectSP (new CommandObjectCall ());
Jim Ingham767af882010-07-07 03:36:20 +0000155 m_command_dict["commands"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000156 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble ());
157 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression ());
158 m_command_dict["file"] = CommandObjectSP (new CommandObjectFile ());
Greg Clayton63094e02010-06-23 01:19:29 +0000159 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000160 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp ());
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));
Chris Lattner24943d22010-06-08 16:52:24 +0000165 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit ());
Greg Clayton63094e02010-06-23 01:19:29 +0000166 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
Caroline Tice6e4c5ce2010-09-04 00:03:46 +0000167 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (script_language));
168 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>
174 break_regex_cmd_ap(new CommandObjectRegexCommand ("regexp-break",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000175 "Set a breakpoint using a regular expression to specify the location.",
Chris Lattner24943d22010-06-08 16:52:24 +0000176 "regexp-break [<file>:<line>]\nregexp-break [<address>]\nregexp-break <...>", 2));
177 if (break_regex_cmd_ap.get())
178 {
179 if (break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2") &&
180 break_regex_cmd_ap->AddRegexCommand("^(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1") &&
181 break_regex_cmd_ap->AddRegexCommand("^[\"']?([-+]\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'") &&
182 break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list") &&
183 break_regex_cmd_ap->AddRegexCommand("^(-.*)$", "breakpoint set %1") &&
184 break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1'"))
185 {
186 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
187 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
188 }
189 }
190}
191
192int
193CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
194 StringList &matches)
195{
196 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
197
198 if (include_aliases)
199 {
200 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
201 }
202
203 return matches.GetSize();
204}
205
206CommandObjectSP
207CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
208{
209 CommandObject::CommandMap::iterator pos;
210 CommandObjectSP ret_val;
211
212 std::string cmd(cmd_cstr);
213
214 if (HasCommands())
215 {
216 pos = m_command_dict.find(cmd);
217 if (pos != m_command_dict.end())
218 ret_val = pos->second;
219 }
220
221 if (include_aliases && HasAliases())
222 {
223 pos = m_alias_dict.find(cmd);
224 if (pos != m_alias_dict.end())
225 ret_val = pos->second;
226 }
227
228 if (HasUserCommands())
229 {
230 pos = m_user_dict.find(cmd);
231 if (pos != m_user_dict.end())
232 ret_val = pos->second;
233 }
234
235 if (!exact && ret_val == NULL)
236 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000237 // We will only get into here if we didn't find any exact matches.
238
239 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
240
Chris Lattner24943d22010-06-08 16:52:24 +0000241 StringList local_matches;
242 if (matches == NULL)
243 matches = &local_matches;
244
Jim Inghamd40f8a62010-07-06 22:46:59 +0000245 unsigned int num_cmd_matches = 0;
246 unsigned int num_alias_matches = 0;
247 unsigned int num_user_matches = 0;
248
249 // Look through the command dictionaries one by one, and if we get only one match from any of
250 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
251
Chris Lattner24943d22010-06-08 16:52:24 +0000252 if (HasCommands())
253 {
254 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
255 }
256
257 if (num_cmd_matches == 1)
258 {
259 cmd.assign(matches->GetStringAtIndex(0));
260 pos = m_command_dict.find(cmd);
261 if (pos != m_command_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000262 real_match_sp = pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000263 }
264
Jim Ingham9a574172010-06-24 20:28:42 +0000265 if (include_aliases && HasAliases())
Chris Lattner24943d22010-06-08 16:52:24 +0000266 {
267 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
268
269 }
270
Jim Inghamd40f8a62010-07-06 22:46:59 +0000271 if (num_alias_matches == 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000272 {
273 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
274 pos = m_alias_dict.find(cmd);
275 if (pos != m_alias_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000276 alias_match_sp = pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000277 }
278
Jim Ingham9a574172010-06-24 20:28:42 +0000279 if (HasUserCommands())
Chris Lattner24943d22010-06-08 16:52:24 +0000280 {
281 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
282 }
283
Jim Inghamd40f8a62010-07-06 22:46:59 +0000284 if (num_user_matches == 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000285 {
286 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
287
288 pos = m_user_dict.find (cmd);
289 if (pos != m_user_dict.end())
Jim Inghamd40f8a62010-07-06 22:46:59 +0000290 user_match_sp = pos->second;
291 }
292
293 // If we got exactly one match, return that, otherwise return the match list.
294
295 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
296 {
297 if (num_cmd_matches)
298 return real_match_sp;
299 else if (num_alias_matches)
300 return alias_match_sp;
301 else
302 return user_match_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000303 }
304 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000305 else if (matches && ret_val != NULL)
306 {
307 matches->AppendString (cmd_cstr);
Chris Lattner24943d22010-06-08 16:52:24 +0000308 }
309
310
311 return ret_val;
312}
313
Jim Inghamd40f8a62010-07-06 22:46:59 +0000314CommandObjectSP
315CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
Chris Lattner24943d22010-06-08 16:52:24 +0000316{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000317 return GetCommandSP(cmd_cstr, include_aliases, true, NULL);
318}
319
320CommandObject *
321CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
322{
323 return GetCommandSPExact (cmd_cstr, include_aliases).get();
324}
325
326CommandObject *
327CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
328{
329 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
330
331 // If we didn't find an exact match to the command string in the commands, look in
332 // the aliases.
333
334 if (command_obj == NULL)
335 {
336 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
337 }
338
339 // Finally, if there wasn't an exact match among the aliases, look for an inexact match
340 // in both the commands and the aliases.
341
342 if (command_obj == NULL)
343 command_obj = GetCommandSP(cmd_cstr, true, false, matches).get();
344
345 return command_obj;
Chris Lattner24943d22010-06-08 16:52:24 +0000346}
347
348bool
349CommandInterpreter::CommandExists (const char *cmd)
350{
351 return m_command_dict.find(cmd) != m_command_dict.end();
352}
353
354bool
355CommandInterpreter::AliasExists (const char *cmd)
356{
357 return m_alias_dict.find(cmd) != m_alias_dict.end();
358}
359
360bool
361CommandInterpreter::UserCommandExists (const char *cmd)
362{
363 return m_user_dict.find(cmd) != m_user_dict.end();
364}
365
366void
367CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
368{
Jim Inghamd40f8a62010-07-06 22:46:59 +0000369 command_obj_sp->SetIsAlias (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000370 m_alias_dict[alias_name] = command_obj_sp;
371}
372
373bool
374CommandInterpreter::RemoveAlias (const char *alias_name)
375{
376 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
377 if (pos != m_alias_dict.end())
378 {
379 m_alias_dict.erase(pos);
380 return true;
381 }
382 return false;
383}
384bool
385CommandInterpreter::RemoveUser (const char *alias_name)
386{
387 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
388 if (pos != m_user_dict.end())
389 {
390 m_user_dict.erase(pos);
391 return true;
392 }
393 return false;
394}
395
Chris Lattner24943d22010-06-08 16:52:24 +0000396void
397CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
398{
399 help_string.Printf ("'%s", command_name);
400 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
401
402 if (option_arg_vector_sp != NULL)
403 {
404 OptionArgVector *options = option_arg_vector_sp.get();
405 for (int i = 0; i < options->size(); ++i)
406 {
407 OptionArgPair cur_option = (*options)[i];
408 std::string opt = cur_option.first;
409 std::string value = cur_option.second;
410 if (opt.compare("<argument>") == 0)
411 {
412 help_string.Printf (" %s", value.c_str());
413 }
414 else
415 {
416 help_string.Printf (" %s", opt.c_str());
417 if ((value.compare ("<no-argument>") != 0)
418 && (value.compare ("<need-argument") != 0))
419 {
420 help_string.Printf (" %s", value.c_str());
421 }
422 }
423 }
424 }
425
426 help_string.Printf ("'");
427}
428
Greg Clayton65124ea2010-08-26 22:05:43 +0000429size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000430CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
431{
432 CommandObject::CommandMap::const_iterator pos;
Greg Clayton65124ea2010-08-26 22:05:43 +0000433 CommandObject::CommandMap::const_iterator end = dict.end();
434 size_t max_len = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000435
Greg Clayton65124ea2010-08-26 22:05:43 +0000436 for (pos = dict.begin(); pos != end; ++pos)
437 {
438 size_t len = pos->first.size();
439 if (max_len < len)
440 max_len = len;
Chris Lattner24943d22010-06-08 16:52:24 +0000441 }
Greg Clayton65124ea2010-08-26 22:05:43 +0000442 return max_len;
Chris Lattner24943d22010-06-08 16:52:24 +0000443}
444
445void
446CommandInterpreter::GetHelp (CommandReturnObject &result)
447{
448 CommandObject::CommandMap::const_iterator pos;
449 result.AppendMessage("The following is a list of built-in, permanent debugger commands:");
450 result.AppendMessage("");
Greg Clayton65124ea2010-08-26 22:05:43 +0000451 uint32_t max_len = FindLongestCommandWord (m_command_dict);
Chris Lattner24943d22010-06-08 16:52:24 +0000452
453 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
454 {
455 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
456 max_len);
457 }
458 result.AppendMessage("");
459
460 if (m_alias_dict.size() > 0)
461 {
Caroline Tice00edd3a2010-09-13 05:27:16 +0000462 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 +0000463 result.AppendMessage("");
Greg Clayton65124ea2010-08-26 22:05:43 +0000464 max_len = FindLongestCommandWord (m_alias_dict);
465
Chris Lattner24943d22010-06-08 16:52:24 +0000466 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
467 {
468 StreamString sstr;
469 StreamString translation_and_help;
470 std::string entry_name = pos->first;
471 std::string second_entry = pos->second.get()->GetCommandName();
472 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
473
474 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
475 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
476 translation_and_help.GetData(), max_len);
477 }
478 result.AppendMessage("");
479 }
480
481 if (m_user_dict.size() > 0)
482 {
483 result.AppendMessage ("The following is a list of your current user-defined commands:");
484 result.AppendMessage("");
485 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
486 {
487 result.AppendMessageWithFormat ("%s -- %s\n", pos->first.c_str(), pos->second->GetHelp());
488 }
489 result.AppendMessage("");
490 }
491
492 result.AppendMessage("For more information on any particular command, try 'help <command-name>'.");
493}
494
Chris Lattner24943d22010-06-08 16:52:24 +0000495bool
Greg Clayton63094e02010-06-23 01:19:29 +0000496CommandInterpreter::HandleCommand
497(
498 const char *command_line,
499 bool add_to_history,
500 CommandReturnObject &result,
501 ExecutionContext *override_context
502)
Chris Lattner24943d22010-06-08 16:52:24 +0000503{
504 // FIXME: there should probably be a mutex to make sure only one thread can
505 // run the interpreter at a time.
506
507 // TODO: this should be a logging channel in lldb.
508// if (DebugSelf())
509// {
510// result.AppendMessageWithFormat ("Processing command: %s\n", command_line);
511// }
512
Greg Clayton63094e02010-06-23 01:19:29 +0000513 m_debugger.UpdateExecutionContext (override_context);
Chris Lattner24943d22010-06-08 16:52:24 +0000514
515 if (command_line == NULL || command_line[0] == '\0')
516 {
517 if (m_command_history.empty())
518 {
519 result.AppendError ("empty command");
520 result.SetStatus(eReturnStatusFailed);
521 return false;
522 }
523 else
524 {
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000525 command_line = m_repeat_command.c_str();
526 if (m_repeat_command.empty())
527 {
Jim Ingham767af882010-07-07 03:36:20 +0000528 result.AppendErrorWithFormat("No auto repeat.\n");
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000529 result.SetStatus (eReturnStatusFailed);
530 return false;
531 }
Chris Lattner24943d22010-06-08 16:52:24 +0000532 }
533 add_to_history = false;
534 }
535
536 Args command_args(command_line);
537
538 if (command_args.GetArgumentCount() > 0)
539 {
540 const char *command_cstr = command_args.GetArgumentAtIndex(0);
541 if (command_cstr)
542 {
543
544 // We're looking up the command object here. So first find an exact match to the
545 // command in the commands.
Jim Inghamd40f8a62010-07-06 22:46:59 +0000546 CommandObject *command_obj = GetCommandObject(command_cstr);
547
548 if (command_obj != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000549 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000550 if (command_obj->IsAlias())
Chris Lattner24943d22010-06-08 16:52:24 +0000551 {
552 BuildAliasCommandArgs (command_obj, command_cstr, command_args, result);
553 if (!result.Succeeded())
554 return false;
555 }
Chris Lattner24943d22010-06-08 16:52:24 +0000556
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000557 if (add_to_history)
558 {
Jim Ingham767af882010-07-07 03:36:20 +0000559 const char *repeat_command = command_obj->GetRepeatCommand(command_args, 0);
560 if (repeat_command != NULL)
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000561 m_repeat_command.assign(repeat_command);
562 else
Jim Ingham767af882010-07-07 03:36:20 +0000563 m_repeat_command.assign(command_line);
Jim Ingham5d9cbd42010-07-06 23:48:33 +0000564
565 m_command_history.push_back (command_line);
566 }
567
568
Chris Lattner24943d22010-06-08 16:52:24 +0000569 if (command_obj->WantsRawCommandString())
570 {
571 const char *stripped_command = ::strstr (command_line, command_cstr);
572 if (stripped_command)
573 {
574 stripped_command += strlen(command_cstr);
575 while (isspace(*stripped_command))
576 ++stripped_command;
Greg Clayton63094e02010-06-23 01:19:29 +0000577 command_obj->ExecuteRawCommandString (*this, stripped_command, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000578 }
579 }
580 else
581 {
Chris Lattner24943d22010-06-08 16:52:24 +0000582 // Remove the command from the args.
583 command_args.Shift();
Greg Clayton63094e02010-06-23 01:19:29 +0000584 command_obj->ExecuteWithOptions (*this, command_args, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000585 }
586 }
587 else
588 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000589 // We didn't find the first command object, so complete the first argument.
Chris Lattner24943d22010-06-08 16:52:24 +0000590 StringList matches;
591 int num_matches;
Jim Inghamd40f8a62010-07-06 22:46:59 +0000592 int cursor_index = 0;
593 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
Jim Ingham802f8b02010-06-30 05:02:46 +0000594 bool word_complete;
Greg Clayton63094e02010-06-23 01:19:29 +0000595 num_matches = HandleCompletionMatches (command_args,
596 cursor_index,
597 cursor_char_position,
598 0,
599 -1,
Jim Ingham802f8b02010-06-30 05:02:46 +0000600 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000601 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000602
603 if (num_matches > 0)
604 {
605 std::string error_msg;
606 error_msg.assign ("ambiguous command '");
607 error_msg.append(command_cstr);
608 error_msg.append ("'.");
609
610 error_msg.append (" Possible completions:");
611 for (int i = 0; i < num_matches; i++)
612 {
613 error_msg.append ("\n\t");
614 error_msg.append (matches.GetStringAtIndex (i));
615 }
616 error_msg.append ("\n");
617 result.AppendRawError (error_msg.c_str(), error_msg.size());
618 }
619 else
620 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_cstr);
621
622 result.SetStatus (eReturnStatusFailed);
623 }
624 }
625 }
626 return result.Succeeded();
627}
628
629int
630CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
631 int &cursor_index,
632 int &cursor_char_position,
633 int match_start_point,
634 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000635 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000636 StringList &matches)
637{
638 int num_command_matches = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000639 bool look_for_subcommand = false;
Jim Ingham802f8b02010-06-30 05:02:46 +0000640
641 // For any of the command completions a unique match will be a complete word.
642 word_complete = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000643
644 if (cursor_index == -1)
645 {
646 // We got nothing on the command line, so return the list of commands
Jim Inghamd40f8a62010-07-06 22:46:59 +0000647 bool include_aliases = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000648 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
649 }
650 else if (cursor_index == 0)
651 {
652 // The cursor is in the first argument, so just do a lookup in the dictionary.
Jim Inghamd40f8a62010-07-06 22:46:59 +0000653 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000654 num_command_matches = matches.GetSize();
655
656 if (num_command_matches == 1
657 && cmd_obj && cmd_obj->IsMultiwordObject()
658 && matches.GetStringAtIndex(0) != NULL
659 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
660 {
661 look_for_subcommand = true;
662 num_command_matches = 0;
663 matches.DeleteStringAtIndex(0);
664 parsed_line.AppendArgument ("");
665 cursor_index++;
666 cursor_char_position = 0;
667 }
668 }
669
670 if (cursor_index > 0 || look_for_subcommand)
671 {
672 // We are completing further on into a commands arguments, so find the command and tell it
673 // to complete the command.
674 // First see if there is a matching initial command:
Jim Inghamd40f8a62010-07-06 22:46:59 +0000675 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000676 if (command_object == NULL)
677 {
678 return 0;
679 }
680 else
681 {
682 parsed_line.Shift();
683 cursor_index--;
Greg Clayton63094e02010-06-23 01:19:29 +0000684 num_command_matches = command_object->HandleCompletion (*this,
685 parsed_line,
686 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();
1013 return ((CommandObjectScript *) script_cmd_obj)->GetInterpreter (*this);
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{
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00001044 lldb::SettableVariableType var_type;
1045 const char *width_value =
Caroline Tice1d2aefd2010-09-09 06:25:08 +00001046 Debugger::GetSettingsController()->GetVariable ("term-width", var_type,
1047 m_debugger.GetInstanceName().AsCString()).GetStringAtIndex(0);
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00001048 int max_columns = atoi (width_value);
Chris Lattner24943d22010-06-08 16:52:24 +00001049 // Sanity check max_columns, to cope with emacs shell mode with TERM=dumb
1050 // (0 rows; 0 columns;).
1051 if (max_columns <= 0) max_columns = 80;
1052
1053 int indent_size = max_word_len + strlen (separator) + 2;
1054
1055 strm.IndentMore (indent_size);
1056
1057 int len = indent_size + strlen (help_text) + 1;
1058 char *text = (char *) malloc (len);
1059 sprintf (text, "%-*s %s %s", max_word_len, word_text, separator, help_text);
1060 if (text[len - 1] == '\n')
1061 text[--len] = '\0';
1062
1063 if (len < max_columns)
1064 {
1065 // Output it as a single line.
1066 strm.Printf ("%s", text);
1067 }
1068 else
1069 {
1070 // We need to break it up into multiple lines.
1071 bool first_line = true;
1072 int text_width;
1073 int start = 0;
1074 int end = start;
1075 int final_end = strlen (text);
1076 int sub_len;
1077
1078 while (end < final_end)
1079 {
1080 if (first_line)
1081 text_width = max_columns - 1;
1082 else
1083 text_width = max_columns - indent_size - 1;
1084
1085 // Don't start the 'text' on a space, since we're already outputting the indentation.
1086 if (!first_line)
1087 {
1088 while ((start < final_end) && (text[start] == ' '))
1089 start++;
1090 }
1091
1092 end = start + text_width;
1093 if (end > final_end)
1094 end = final_end;
1095 else
1096 {
1097 // If we're not at the end of the text, make sure we break the line on white space.
1098 while (end > start
1099 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
1100 end--;
1101 }
1102
1103 sub_len = end - start;
1104 if (start != 0)
1105 strm.EOL();
1106 if (!first_line)
1107 strm.Indent();
1108 else
1109 first_line = false;
1110 assert (start <= final_end);
1111 assert (start + sub_len <= final_end);
1112 if (sub_len > 0)
1113 strm.Write (text + start, sub_len);
1114 start = end + 1;
1115 }
1116 }
1117 strm.EOL();
1118 strm.IndentLess(indent_size);
1119 free (text);
1120}
1121
1122void
1123CommandInterpreter::AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word,
1124 StringList &commands_found, StringList &commands_help)
1125{
1126 CommandObject::CommandMap::const_iterator pos;
1127 CommandObject::CommandMap sub_cmd_dict = ((CommandObjectMultiword *) cmd_obj)->m_subcommand_dict;
1128 CommandObject *sub_cmd_obj;
1129
1130 for (pos = sub_cmd_dict.begin(); pos != sub_cmd_dict.end(); ++pos)
1131 {
1132 const char * command_name = pos->first.c_str();
1133 sub_cmd_obj = pos->second.get();
1134 StreamString complete_command_name;
1135
1136 complete_command_name.Printf ("%s %s", prefix, command_name);
1137
Caroline Tice1d2aefd2010-09-09 06:25:08 +00001138 if (sub_cmd_obj->HelpTextContainsWord (search_word, *this))
Chris Lattner24943d22010-06-08 16:52:24 +00001139 {
1140 commands_found.AppendString (complete_command_name.GetData());
1141 commands_help.AppendString (sub_cmd_obj->GetHelp());
1142 }
1143
1144 if (sub_cmd_obj->IsMultiwordObject())
1145 AproposAllSubCommands (sub_cmd_obj, complete_command_name.GetData(), search_word, commands_found,
1146 commands_help);
1147 }
1148
1149}
1150
1151void
1152CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
1153 StringList &commands_help)
1154{
1155 CommandObject::CommandMap::const_iterator pos;
1156
1157 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1158 {
1159 const char *command_name = pos->first.c_str();
1160 CommandObject *cmd_obj = pos->second.get();
1161
Caroline Tice1d2aefd2010-09-09 06:25:08 +00001162 if (cmd_obj->HelpTextContainsWord (search_word, *this))
Chris Lattner24943d22010-06-08 16:52:24 +00001163 {
1164 commands_found.AppendString (command_name);
1165 commands_help.AppendString (cmd_obj->GetHelp());
1166 }
1167
1168 if (cmd_obj->IsMultiwordObject())
1169 AproposAllSubCommands (cmd_obj, command_name, search_word, commands_found, commands_help);
1170
1171 }
1172}