Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandInterpreter.cpp ----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <string> |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 11 | #include <vector> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | |
| 13 | #include <getopt.h> |
| 14 | #include <stdlib.h> |
| 15 | |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 16 | #include "../Commands/CommandObjectApropos.h" |
| 17 | #include "../Commands/CommandObjectArgs.h" |
| 18 | #include "../Commands/CommandObjectBreakpoint.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 19 | //#include "../Commands/CommandObjectCall.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 20 | #include "../Commands/CommandObjectDisassemble.h" |
| 21 | #include "../Commands/CommandObjectExpression.h" |
| 22 | #include "../Commands/CommandObjectFile.h" |
| 23 | #include "../Commands/CommandObjectFrame.h" |
| 24 | #include "../Commands/CommandObjectHelp.h" |
| 25 | #include "../Commands/CommandObjectImage.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 26 | #include "../Commands/CommandObjectLog.h" |
| 27 | #include "../Commands/CommandObjectMemory.h" |
| 28 | #include "../Commands/CommandObjectProcess.h" |
| 29 | #include "../Commands/CommandObjectQuit.h" |
Eli Friedman | b34d2a2 | 2010-06-09 22:08:29 +0000 | [diff] [blame] | 30 | #include "lldb/Interpreter/CommandObjectRegexCommand.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 31 | #include "../Commands/CommandObjectRegister.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | #include "CommandObjectScript.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 33 | #include "../Commands/CommandObjectSettings.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 34 | #include "../Commands/CommandObjectSource.h" |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 35 | #include "../Commands/CommandObjectCommands.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 36 | #include "../Commands/CommandObjectSyntax.h" |
| 37 | #include "../Commands/CommandObjectTarget.h" |
| 38 | #include "../Commands/CommandObjectThread.h" |
Johnny Chen | 902e018 | 2010-12-23 20:21:44 +0000 | [diff] [blame] | 39 | #include "../Commands/CommandObjectVersion.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 41 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | #include "lldb/Core/Debugger.h" |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 43 | #include "lldb/Core/InputReader.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | #include "lldb/Core/Stream.h" |
| 45 | #include "lldb/Core/Timer.h" |
| 46 | #include "lldb/Target/Process.h" |
| 47 | #include "lldb/Target/Thread.h" |
| 48 | #include "lldb/Target/TargetList.h" |
Greg Clayton | e98ac25 | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 49 | #include "lldb/Utility/CleanUp.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | |
| 51 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 52 | #include "lldb/Interpreter/CommandInterpreter.h" |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 53 | #include "lldb/Interpreter/ScriptInterpreterNone.h" |
| 54 | #include "lldb/Interpreter/ScriptInterpreterPython.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | |
| 56 | using namespace lldb; |
| 57 | using namespace lldb_private; |
| 58 | |
| 59 | CommandInterpreter::CommandInterpreter |
| 60 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 61 | Debugger &debugger, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | ScriptLanguage script_language, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 63 | bool synchronous_execution |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | ) : |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 65 | Broadcaster ("lldb.command-interpreter"), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 66 | m_debugger (debugger), |
Greg Clayton | 887aa28 | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 67 | m_synchronous_execution (synchronous_execution), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 68 | m_skip_lldbinit_files (false), |
| 69 | m_script_interpreter_ap () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 71 | const char *dbg_name = debugger.GetInstanceName().AsCString(); |
| 72 | std::string lang_name = ScriptInterpreter::LanguageToString (script_language); |
| 73 | StreamString var_name; |
| 74 | var_name.Printf ("[%s].script-lang", dbg_name); |
Caroline Tice | 1d2aefd | 2010-09-09 06:25:08 +0000 | [diff] [blame] | 75 | debugger.GetSettingsController()->SetVariable (var_name.GetData(), lang_name.c_str(), |
| 76 | lldb::eVarSetOperationAssign, false, |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 77 | m_debugger.GetInstanceName().AsCString()); |
| 78 | SetEventName (eBroadcastBitThreadShouldExit, "thread-should-exit"); |
| 79 | SetEventName (eBroadcastBitResetPrompt, "reset-prompt"); |
| 80 | SetEventName (eBroadcastBitQuitCommandReceived, "quit"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
| 84 | CommandInterpreter::Initialize () |
| 85 | { |
| 86 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 87 | |
| 88 | CommandReturnObject result; |
| 89 | |
| 90 | LoadCommandDictionary (); |
| 91 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | // Set up some initial aliases. |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 93 | result.Clear(); HandleCommand ("command alias q quit", false, result); |
Jim Ingham | e3663e8 | 2010-10-22 18:47:16 +0000 | [diff] [blame] | 94 | result.Clear(); HandleCommand ("command alias run process launch --", false, result); |
| 95 | result.Clear(); HandleCommand ("command alias r process launch --", false, result); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 96 | result.Clear(); HandleCommand ("command alias c process continue", false, result); |
| 97 | result.Clear(); HandleCommand ("command alias continue process continue", false, result); |
| 98 | result.Clear(); HandleCommand ("command alias expr expression", false, result); |
| 99 | result.Clear(); HandleCommand ("command alias exit quit", false, result); |
Greg Clayton | 0f3a8eb | 2010-09-16 17:09:23 +0000 | [diff] [blame] | 100 | result.Clear(); HandleCommand ("command alias b regexp-break", false, result); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 101 | result.Clear(); HandleCommand ("command alias bt thread backtrace", false, result); |
| 102 | result.Clear(); HandleCommand ("command alias si thread step-inst", false, result); |
| 103 | result.Clear(); HandleCommand ("command alias step thread step-in", false, result); |
| 104 | result.Clear(); HandleCommand ("command alias s thread step-in", false, result); |
| 105 | result.Clear(); HandleCommand ("command alias next thread step-over", false, result); |
| 106 | result.Clear(); HandleCommand ("command alias n thread step-over", false, result); |
| 107 | result.Clear(); HandleCommand ("command alias finish thread step-out", false, result); |
| 108 | result.Clear(); HandleCommand ("command alias x memory read", false, result); |
| 109 | result.Clear(); HandleCommand ("command alias l source list", false, result); |
| 110 | result.Clear(); HandleCommand ("command alias list source list", false, result); |
Greg Clayton | 0f3a8eb | 2010-09-16 17:09:23 +0000 | [diff] [blame] | 111 | result.Clear(); HandleCommand ("command alias p frame variable", false, result); |
| 112 | result.Clear(); HandleCommand ("command alias print frame variable", false, result); |
Jim Ingham | e3663e8 | 2010-10-22 18:47:16 +0000 | [diff] [blame] | 113 | result.Clear(); HandleCommand ("command alias po expression -o --", false, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | const char * |
| 117 | CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg) |
| 118 | { |
| 119 | // This function has not yet been implemented. |
| 120 | |
| 121 | // Look for any embedded script command |
| 122 | // If found, |
| 123 | // get interpreter object from the command dictionary, |
| 124 | // call execute_one_command on it, |
| 125 | // get the results as a string, |
| 126 | // substitute that string for current stuff. |
| 127 | |
| 128 | return arg; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void |
| 133 | CommandInterpreter::LoadCommandDictionary () |
| 134 | { |
| 135 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 136 | |
| 137 | // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT *** |
| 138 | // |
| 139 | // Command objects that are used as cross reference objects (i.e. they inherit from CommandObjectCrossref) |
| 140 | // *MUST* be created and put into the command dictionary *BEFORE* any multi-word commands (which may use |
| 141 | // the cross-referencing stuff) are created!!! |
| 142 | // |
| 143 | // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT *** |
| 144 | |
| 145 | |
| 146 | // Command objects that inherit from CommandObjectCrossref must be created before other command objects |
| 147 | // are created. This is so that when another command is created that needs to go into a crossref object, |
| 148 | // the crossref object exists and is ready to take the cross reference. Put the cross referencing command |
| 149 | // objects into the CommandDictionary now, so they are ready for use when the other commands get created. |
| 150 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | // Non-CommandObjectCrossref commands can now be created. |
| 152 | |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 153 | lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage(); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 154 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 155 | m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 156 | m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 157 | //m_command_dict["call"] = CommandObjectSP (new CommandObjectCall (*this)); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 158 | m_command_dict["commands"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 159 | m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this)); |
| 160 | m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this)); |
| 161 | m_command_dict["file"] = CommandObjectSP (new CommandObjectFile (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 162 | m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 163 | m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 164 | m_command_dict["image"] = CommandObjectSP (new CommandObjectImage (*this)); |
| 165 | m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this)); |
| 166 | m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this)); |
| 167 | m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 168 | m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 169 | m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 170 | m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language)); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 171 | m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this)); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 172 | m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 173 | m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this)); |
| 174 | m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this)); |
Johnny Chen | 902e018 | 2010-12-23 20:21:44 +0000 | [diff] [blame] | 175 | m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | |
| 177 | std::auto_ptr<CommandObjectRegexCommand> |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 178 | break_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
| 179 | "regexp-break", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 180 | "Set a breakpoint using a regular expression to specify the location.", |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 181 | "regexp-break [<filename>:<linenum>]\nregexp-break [<address>]\nregexp-break <...>", 2)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 182 | if (break_regex_cmd_ap.get()) |
| 183 | { |
| 184 | if (break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2") && |
| 185 | break_regex_cmd_ap->AddRegexCommand("^(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1") && |
| 186 | break_regex_cmd_ap->AddRegexCommand("^[\"']?([-+]\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'") && |
| 187 | break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list") && |
| 188 | break_regex_cmd_ap->AddRegexCommand("^(-.*)$", "breakpoint set %1") && |
| 189 | break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1'")) |
| 190 | { |
| 191 | CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release()); |
| 192 | m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | int |
| 198 | CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases, |
| 199 | StringList &matches) |
| 200 | { |
| 201 | CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches); |
| 202 | |
| 203 | if (include_aliases) |
| 204 | { |
| 205 | CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches); |
| 206 | } |
| 207 | |
| 208 | return matches.GetSize(); |
| 209 | } |
| 210 | |
| 211 | CommandObjectSP |
| 212 | CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches) |
| 213 | { |
| 214 | CommandObject::CommandMap::iterator pos; |
| 215 | CommandObjectSP ret_val; |
| 216 | |
| 217 | std::string cmd(cmd_cstr); |
| 218 | |
| 219 | if (HasCommands()) |
| 220 | { |
| 221 | pos = m_command_dict.find(cmd); |
| 222 | if (pos != m_command_dict.end()) |
| 223 | ret_val = pos->second; |
| 224 | } |
| 225 | |
| 226 | if (include_aliases && HasAliases()) |
| 227 | { |
| 228 | pos = m_alias_dict.find(cmd); |
| 229 | if (pos != m_alias_dict.end()) |
| 230 | ret_val = pos->second; |
| 231 | } |
| 232 | |
| 233 | if (HasUserCommands()) |
| 234 | { |
| 235 | pos = m_user_dict.find(cmd); |
| 236 | if (pos != m_user_dict.end()) |
| 237 | ret_val = pos->second; |
| 238 | } |
| 239 | |
| 240 | if (!exact && ret_val == NULL) |
| 241 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 242 | // We will only get into here if we didn't find any exact matches. |
| 243 | |
| 244 | CommandObjectSP user_match_sp, alias_match_sp, real_match_sp; |
| 245 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | StringList local_matches; |
| 247 | if (matches == NULL) |
| 248 | matches = &local_matches; |
| 249 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 250 | unsigned int num_cmd_matches = 0; |
| 251 | unsigned int num_alias_matches = 0; |
| 252 | unsigned int num_user_matches = 0; |
| 253 | |
| 254 | // Look through the command dictionaries one by one, and if we get only one match from any of |
| 255 | // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches. |
| 256 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 257 | if (HasCommands()) |
| 258 | { |
| 259 | num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches); |
| 260 | } |
| 261 | |
| 262 | if (num_cmd_matches == 1) |
| 263 | { |
| 264 | cmd.assign(matches->GetStringAtIndex(0)); |
| 265 | pos = m_command_dict.find(cmd); |
| 266 | if (pos != m_command_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 267 | real_match_sp = pos->second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Jim Ingham | 9a57417 | 2010-06-24 20:28:42 +0000 | [diff] [blame] | 270 | if (include_aliases && HasAliases()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 271 | { |
| 272 | num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches); |
| 273 | |
| 274 | } |
| 275 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 276 | if (num_alias_matches == 1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | { |
| 278 | cmd.assign(matches->GetStringAtIndex (num_cmd_matches)); |
| 279 | pos = m_alias_dict.find(cmd); |
| 280 | if (pos != m_alias_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 281 | alias_match_sp = pos->second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Jim Ingham | 9a57417 | 2010-06-24 20:28:42 +0000 | [diff] [blame] | 284 | if (HasUserCommands()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | { |
| 286 | num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches); |
| 287 | } |
| 288 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 289 | if (num_user_matches == 1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 290 | { |
| 291 | cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches)); |
| 292 | |
| 293 | pos = m_user_dict.find (cmd); |
| 294 | if (pos != m_user_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 295 | user_match_sp = pos->second; |
| 296 | } |
| 297 | |
| 298 | // If we got exactly one match, return that, otherwise return the match list. |
| 299 | |
| 300 | if (num_user_matches + num_cmd_matches + num_alias_matches == 1) |
| 301 | { |
| 302 | if (num_cmd_matches) |
| 303 | return real_match_sp; |
| 304 | else if (num_alias_matches) |
| 305 | return alias_match_sp; |
| 306 | else |
| 307 | return user_match_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 310 | else if (matches && ret_val != NULL) |
| 311 | { |
| 312 | matches->AppendString (cmd_cstr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
| 316 | return ret_val; |
| 317 | } |
| 318 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 319 | CommandObjectSP |
| 320 | CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | { |
Caroline Tice | 56d2fc4 | 2010-12-14 18:51:39 +0000 | [diff] [blame] | 322 | Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command. |
| 323 | CommandObjectSP ret_val; // Possibly empty return value. |
| 324 | |
| 325 | if (cmd_cstr == NULL) |
| 326 | return ret_val; |
| 327 | |
| 328 | if (cmd_words.GetArgumentCount() == 1) |
| 329 | return GetCommandSP(cmd_cstr, include_aliases, true, NULL); |
| 330 | else |
| 331 | { |
| 332 | // We have a multi-word command (seemingly), so we need to do more work. |
| 333 | // First, get the cmd_obj_sp for the first word in the command. |
| 334 | CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, NULL); |
| 335 | if (cmd_obj_sp.get() != NULL) |
| 336 | { |
| 337 | // Loop through the rest of the words in the command (everything passed in was supposed to be part of a |
| 338 | // command name), and find the appropriate sub-command SP for each command word.... |
| 339 | size_t end = cmd_words.GetArgumentCount(); |
| 340 | for (size_t j= 1; j < end; ++j) |
| 341 | { |
| 342 | if (cmd_obj_sp->IsMultiwordObject()) |
| 343 | { |
| 344 | cmd_obj_sp = ((CommandObjectMultiword *) cmd_obj_sp.get())->GetSubcommandSP |
| 345 | (cmd_words.GetArgumentAtIndex (j)); |
| 346 | if (cmd_obj_sp.get() == NULL) |
| 347 | // The sub-command name was invalid. Fail and return the empty 'ret_val'. |
| 348 | return ret_val; |
| 349 | } |
| 350 | else |
| 351 | // We have more words in the command name, but we don't have a multiword object. Fail and return |
| 352 | // empty 'ret_val'. |
| 353 | return ret_val; |
| 354 | } |
| 355 | // We successfully looped through all the command words and got valid command objects for them. Assign the |
| 356 | // last object retrieved to 'ret_val'. |
| 357 | ret_val = cmd_obj_sp; |
| 358 | } |
| 359 | } |
| 360 | return ret_val; |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | CommandObject * |
| 364 | CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases) |
| 365 | { |
| 366 | return GetCommandSPExact (cmd_cstr, include_aliases).get(); |
| 367 | } |
| 368 | |
| 369 | CommandObject * |
| 370 | CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches) |
| 371 | { |
| 372 | CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get(); |
| 373 | |
| 374 | // If we didn't find an exact match to the command string in the commands, look in |
| 375 | // the aliases. |
| 376 | |
| 377 | if (command_obj == NULL) |
| 378 | { |
| 379 | command_obj = GetCommandSP (cmd_cstr, true, true, matches).get(); |
| 380 | } |
| 381 | |
| 382 | // Finally, if there wasn't an exact match among the aliases, look for an inexact match |
| 383 | // in both the commands and the aliases. |
| 384 | |
| 385 | if (command_obj == NULL) |
| 386 | command_obj = GetCommandSP(cmd_cstr, true, false, matches).get(); |
| 387 | |
| 388 | return command_obj; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | bool |
| 392 | CommandInterpreter::CommandExists (const char *cmd) |
| 393 | { |
| 394 | return m_command_dict.find(cmd) != m_command_dict.end(); |
| 395 | } |
| 396 | |
| 397 | bool |
| 398 | CommandInterpreter::AliasExists (const char *cmd) |
| 399 | { |
| 400 | return m_alias_dict.find(cmd) != m_alias_dict.end(); |
| 401 | } |
| 402 | |
| 403 | bool |
| 404 | CommandInterpreter::UserCommandExists (const char *cmd) |
| 405 | { |
| 406 | return m_user_dict.find(cmd) != m_user_dict.end(); |
| 407 | } |
| 408 | |
| 409 | void |
| 410 | CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp) |
| 411 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 412 | command_obj_sp->SetIsAlias (true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 413 | m_alias_dict[alias_name] = command_obj_sp; |
| 414 | } |
| 415 | |
| 416 | bool |
| 417 | CommandInterpreter::RemoveAlias (const char *alias_name) |
| 418 | { |
| 419 | CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name); |
| 420 | if (pos != m_alias_dict.end()) |
| 421 | { |
| 422 | m_alias_dict.erase(pos); |
| 423 | return true; |
| 424 | } |
| 425 | return false; |
| 426 | } |
| 427 | bool |
| 428 | CommandInterpreter::RemoveUser (const char *alias_name) |
| 429 | { |
| 430 | CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name); |
| 431 | if (pos != m_user_dict.end()) |
| 432 | { |
| 433 | m_user_dict.erase(pos); |
| 434 | return true; |
| 435 | } |
| 436 | return false; |
| 437 | } |
| 438 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 439 | void |
| 440 | CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string) |
| 441 | { |
| 442 | help_string.Printf ("'%s", command_name); |
| 443 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
| 444 | |
| 445 | if (option_arg_vector_sp != NULL) |
| 446 | { |
| 447 | OptionArgVector *options = option_arg_vector_sp.get(); |
| 448 | for (int i = 0; i < options->size(); ++i) |
| 449 | { |
| 450 | OptionArgPair cur_option = (*options)[i]; |
| 451 | std::string opt = cur_option.first; |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 452 | OptionArgValue value_pair = cur_option.second; |
| 453 | std::string value = value_pair.second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 454 | if (opt.compare("<argument>") == 0) |
| 455 | { |
| 456 | help_string.Printf (" %s", value.c_str()); |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | help_string.Printf (" %s", opt.c_str()); |
| 461 | if ((value.compare ("<no-argument>") != 0) |
| 462 | && (value.compare ("<need-argument") != 0)) |
| 463 | { |
| 464 | help_string.Printf (" %s", value.c_str()); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | help_string.Printf ("'"); |
| 471 | } |
| 472 | |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 473 | size_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 474 | CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict) |
| 475 | { |
| 476 | CommandObject::CommandMap::const_iterator pos; |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 477 | CommandObject::CommandMap::const_iterator end = dict.end(); |
| 478 | size_t max_len = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 479 | |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 480 | for (pos = dict.begin(); pos != end; ++pos) |
| 481 | { |
| 482 | size_t len = pos->first.size(); |
| 483 | if (max_len < len) |
| 484 | max_len = len; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 485 | } |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 486 | return max_len; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void |
| 490 | CommandInterpreter::GetHelp (CommandReturnObject &result) |
| 491 | { |
| 492 | CommandObject::CommandMap::const_iterator pos; |
| 493 | result.AppendMessage("The following is a list of built-in, permanent debugger commands:"); |
| 494 | result.AppendMessage(""); |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 495 | uint32_t max_len = FindLongestCommandWord (m_command_dict); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 496 | |
| 497 | for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) |
| 498 | { |
| 499 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(), |
| 500 | max_len); |
| 501 | } |
| 502 | result.AppendMessage(""); |
| 503 | |
| 504 | if (m_alias_dict.size() > 0) |
| 505 | { |
Jim Ingham | e3663e8 | 2010-10-22 18:47:16 +0000 | [diff] [blame] | 506 | result.AppendMessage("The following is a list of your current command abbreviations " |
| 507 | "(see 'help commands alias' for more info):"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 508 | result.AppendMessage(""); |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 509 | max_len = FindLongestCommandWord (m_alias_dict); |
| 510 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 511 | for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos) |
| 512 | { |
| 513 | StreamString sstr; |
| 514 | StreamString translation_and_help; |
| 515 | std::string entry_name = pos->first; |
| 516 | std::string second_entry = pos->second.get()->GetCommandName(); |
| 517 | GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr); |
| 518 | |
| 519 | translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp()); |
| 520 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", |
| 521 | translation_and_help.GetData(), max_len); |
| 522 | } |
| 523 | result.AppendMessage(""); |
| 524 | } |
| 525 | |
| 526 | if (m_user_dict.size() > 0) |
| 527 | { |
| 528 | result.AppendMessage ("The following is a list of your current user-defined commands:"); |
| 529 | result.AppendMessage(""); |
| 530 | for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos) |
| 531 | { |
| 532 | result.AppendMessageWithFormat ("%s -- %s\n", pos->first.c_str(), pos->second->GetHelp()); |
| 533 | } |
| 534 | result.AppendMessage(""); |
| 535 | } |
| 536 | |
| 537 | result.AppendMessage("For more information on any particular command, try 'help <command-name>'."); |
| 538 | } |
| 539 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 540 | CommandObject * |
| 541 | CommandInterpreter::GetCommandObjectForCommand (std::string &command_string) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 542 | { |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 543 | // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will |
| 544 | // eventually be invoked by the given command line. |
| 545 | |
| 546 | CommandObject *cmd_obj = NULL; |
| 547 | std::string white_space (" \t\v"); |
| 548 | size_t start = command_string.find_first_not_of (white_space); |
| 549 | size_t end = 0; |
| 550 | bool done = false; |
| 551 | while (!done) |
| 552 | { |
| 553 | if (start != std::string::npos) |
| 554 | { |
| 555 | // Get the next word from command_string. |
| 556 | end = command_string.find_first_of (white_space, start); |
| 557 | if (end == std::string::npos) |
| 558 | end = command_string.size(); |
| 559 | std::string cmd_word = command_string.substr (start, end - start); |
| 560 | |
| 561 | if (cmd_obj == NULL) |
| 562 | // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid |
| 563 | // command or alias. |
| 564 | cmd_obj = GetCommandObject (cmd_word.c_str()); |
| 565 | else if (cmd_obj->IsMultiwordObject ()) |
| 566 | { |
| 567 | // Our current object is a multi-word object; see if the cmd_word is a valid sub-command for our object. |
| 568 | CommandObject *sub_cmd_obj = |
| 569 | ((CommandObjectMultiword *) cmd_obj)->GetSubcommandObject (cmd_word.c_str()); |
| 570 | if (sub_cmd_obj) |
| 571 | cmd_obj = sub_cmd_obj; |
| 572 | else // cmd_word was not a valid sub-command word, so we are donee |
| 573 | done = true; |
| 574 | } |
| 575 | else |
| 576 | // We have a cmd_obj and it is not a multi-word object, so we are done. |
| 577 | done = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 578 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 579 | // If we didn't find a valid command object, or our command object is not a multi-word object, or |
| 580 | // we are at the end of the command_string, then we are done. Otherwise, find the start of the |
| 581 | // next word. |
| 582 | |
| 583 | if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size()) |
| 584 | done = true; |
| 585 | else |
| 586 | start = command_string.find_first_not_of (white_space, end); |
| 587 | } |
| 588 | else |
| 589 | // Unable to find any more words. |
| 590 | done = true; |
| 591 | } |
| 592 | |
| 593 | if (end == command_string.size()) |
| 594 | command_string.clear(); |
| 595 | else |
| 596 | command_string = command_string.substr(end); |
| 597 | |
| 598 | return cmd_obj; |
| 599 | } |
| 600 | |
| 601 | bool |
| 602 | CommandInterpreter::StripFirstWord (std::string &command_string, std::string &word) |
| 603 | { |
| 604 | std::string white_space (" \t\v"); |
| 605 | size_t start; |
| 606 | size_t end; |
| 607 | |
| 608 | start = command_string.find_first_not_of (white_space); |
| 609 | if (start != std::string::npos) |
| 610 | { |
| 611 | end = command_string.find_first_of (white_space, start); |
| 612 | if (end != std::string::npos) |
| 613 | { |
| 614 | word = command_string.substr (start, end - start); |
| 615 | command_string = command_string.substr (end); |
| 616 | size_t pos = command_string.find_first_not_of (white_space); |
| 617 | if ((pos != 0) && (pos != std::string::npos)) |
| 618 | command_string = command_string.substr (pos); |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | word = command_string.substr (start); |
| 623 | command_string.erase(); |
| 624 | } |
| 625 | |
| 626 | } |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | void |
| 631 | CommandInterpreter::BuildAliasResult (const char *alias_name, std::string &raw_input_string, std::string &alias_result, |
| 632 | CommandObject *&alias_cmd_obj, CommandReturnObject &result) |
| 633 | { |
| 634 | Args cmd_args (raw_input_string.c_str()); |
| 635 | alias_cmd_obj = GetCommandObject (alias_name); |
| 636 | StreamString result_str; |
| 637 | |
| 638 | if (alias_cmd_obj) |
| 639 | { |
| 640 | std::string alias_name_str = alias_name; |
| 641 | if ((cmd_args.GetArgumentCount() == 0) |
| 642 | || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)) |
| 643 | cmd_args.Unshift (alias_name); |
| 644 | |
| 645 | result_str.Printf ("%s", alias_cmd_obj->GetCommandName ()); |
| 646 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
| 647 | |
| 648 | if (option_arg_vector_sp.get()) |
| 649 | { |
| 650 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 651 | |
| 652 | for (int i = 0; i < option_arg_vector->size(); ++i) |
| 653 | { |
| 654 | OptionArgPair option_pair = (*option_arg_vector)[i]; |
| 655 | OptionArgValue value_pair = option_pair.second; |
| 656 | int value_type = value_pair.first; |
| 657 | std::string option = option_pair.first; |
| 658 | std::string value = value_pair.second; |
| 659 | if (option.compare ("<argument>") == 0) |
| 660 | result_str.Printf (" %s", value.c_str()); |
| 661 | else |
| 662 | { |
| 663 | result_str.Printf (" %s", option.c_str()); |
| 664 | if (value_type != optional_argument) |
| 665 | result_str.Printf (" "); |
| 666 | if (value.compare ("<no_argument>") != 0) |
| 667 | { |
| 668 | int index = GetOptionArgumentPosition (value.c_str()); |
| 669 | if (index == 0) |
| 670 | result_str.Printf ("%s", value.c_str()); |
| 671 | else if (index >= cmd_args.GetArgumentCount()) |
| 672 | { |
| 673 | |
| 674 | result.AppendErrorWithFormat |
| 675 | ("Not enough arguments provided; you need at least %d arguments to use this alias.\n", |
| 676 | index); |
| 677 | result.SetStatus (eReturnStatusFailed); |
| 678 | return; |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index)); |
| 683 | if (strpos != std::string::npos) |
| 684 | raw_input_string = raw_input_string.erase (strpos, |
| 685 | strlen (cmd_args.GetArgumentAtIndex (index))); |
| 686 | result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index)); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | alias_result = result_str.GetData(); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | bool |
| 698 | CommandInterpreter::HandleCommand (const char *command_line, |
| 699 | bool add_to_history, |
| 700 | CommandReturnObject &result, |
| 701 | ExecutionContext *override_context) |
| 702 | { |
| 703 | bool done = false; |
| 704 | CommandObject *cmd_obj = NULL; |
| 705 | std::string next_word; |
| 706 | bool wants_raw_input = false; |
| 707 | std::string command_string (command_line); |
| 708 | |
| 709 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS)); |
Greg Clayton | e98ac25 | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 710 | Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line); |
| 711 | |
| 712 | // Make a scoped cleanup object that will clear the crash description string |
| 713 | // on exit of this function. |
| 714 | lldb_utility::CleanUp <const char *, void> crash_description_cleanup(NULL, Host::SetCrashDescription); |
| 715 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 716 | if (log) |
| 717 | log->Printf ("Processing command: %s", command_line); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 718 | |
Jim Ingham | abab14b | 2010-11-04 23:08:45 +0000 | [diff] [blame] | 719 | Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line); |
| 720 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 721 | m_debugger.UpdateExecutionContext (override_context); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 722 | |
| 723 | if (command_line == NULL || command_line[0] == '\0') |
| 724 | { |
| 725 | if (m_command_history.empty()) |
| 726 | { |
| 727 | result.AppendError ("empty command"); |
| 728 | result.SetStatus(eReturnStatusFailed); |
| 729 | return false; |
| 730 | } |
| 731 | else |
| 732 | { |
Jim Ingham | 5d9cbd4 | 2010-07-06 23:48:33 +0000 | [diff] [blame] | 733 | command_line = m_repeat_command.c_str(); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 734 | command_string = command_line; |
Jim Ingham | 5d9cbd4 | 2010-07-06 23:48:33 +0000 | [diff] [blame] | 735 | if (m_repeat_command.empty()) |
| 736 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 737 | result.AppendErrorWithFormat("No auto repeat.\n"); |
Jim Ingham | 5d9cbd4 | 2010-07-06 23:48:33 +0000 | [diff] [blame] | 738 | result.SetStatus (eReturnStatusFailed); |
| 739 | return false; |
| 740 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 741 | } |
| 742 | add_to_history = false; |
| 743 | } |
| 744 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 745 | // Phase 1. |
| 746 | |
| 747 | // Before we do ANY kind of argument processing, etc. we need to figure out what the real/final command object |
| 748 | // is for the specified command, and whether or not it wants raw input. This gets complicated by the fact that |
| 749 | // the user could have specified an alias, and in translating the alias there may also be command options and/or |
| 750 | // even data (including raw text strings) that need to be found and inserted into the command line as part of |
| 751 | // the translation. So this first step is plain look-up & replacement, resulting in three things: 1). the command |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 752 | // object whose Execute method will actually be called; 2). a revised command string, with all substitutions & |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 753 | // replacements taken care of; 3). whether or not the Execute function wants raw input or not. |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 754 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 755 | StreamString revised_command_line; |
Caroline Tice | ea6e3df | 2010-12-11 08:16:56 +0000 | [diff] [blame] | 756 | size_t actual_cmd_name_len = 0; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 757 | while (!done) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 758 | { |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 759 | StripFirstWord (command_string, next_word); |
| 760 | if (!cmd_obj && AliasExists (next_word.c_str())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 761 | { |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 762 | std::string alias_result; |
| 763 | BuildAliasResult (next_word.c_str(), command_string, alias_result, cmd_obj, result); |
| 764 | revised_command_line.Printf ("%s", alias_result.c_str()); |
| 765 | if (cmd_obj) |
Caroline Tice | 56d2fc4 | 2010-12-14 18:51:39 +0000 | [diff] [blame] | 766 | { |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 767 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
Caroline Tice | 56d2fc4 | 2010-12-14 18:51:39 +0000 | [diff] [blame] | 768 | actual_cmd_name_len = strlen (cmd_obj->GetCommandName()); |
| 769 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 770 | } |
| 771 | else if (!cmd_obj) |
| 772 | { |
| 773 | cmd_obj = GetCommandObject (next_word.c_str()); |
| 774 | if (cmd_obj) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 775 | { |
Caroline Tice | ea6e3df | 2010-12-11 08:16:56 +0000 | [diff] [blame] | 776 | actual_cmd_name_len += next_word.length(); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 777 | revised_command_line.Printf ("%s", next_word.c_str()); |
| 778 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 779 | } |
| 780 | else |
| 781 | { |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 782 | revised_command_line.Printf ("%s", next_word.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 785 | else if (cmd_obj->IsMultiwordObject ()) |
| 786 | { |
| 787 | CommandObject *sub_cmd_obj = ((CommandObjectMultiword *) cmd_obj)->GetSubcommandObject (next_word.c_str()); |
| 788 | if (sub_cmd_obj) |
| 789 | { |
Caroline Tice | ea6e3df | 2010-12-11 08:16:56 +0000 | [diff] [blame] | 790 | actual_cmd_name_len += next_word.length() + 1; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 791 | revised_command_line.Printf (" %s", next_word.c_str()); |
| 792 | cmd_obj = sub_cmd_obj; |
| 793 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
| 794 | } |
| 795 | else |
| 796 | { |
| 797 | revised_command_line.Printf (" %s", next_word.c_str()); |
| 798 | done = true; |
| 799 | } |
| 800 | } |
| 801 | else |
| 802 | { |
| 803 | revised_command_line.Printf (" %s", next_word.c_str()); |
| 804 | done = true; |
| 805 | } |
| 806 | |
| 807 | if (cmd_obj == NULL) |
| 808 | { |
| 809 | result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str()); |
| 810 | result.SetStatus (eReturnStatusFailed); |
| 811 | return false; |
| 812 | } |
| 813 | |
| 814 | next_word.erase (); |
| 815 | if (command_string.length() == 0) |
| 816 | done = true; |
| 817 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 818 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 819 | |
| 820 | if (command_string.size() > 0) |
| 821 | revised_command_line.Printf (" %s", command_string.c_str()); |
| 822 | |
| 823 | // End of Phase 1. |
| 824 | // At this point cmd_obj should contain the CommandObject whose Execute method will be called, if the command |
| 825 | // specified was valid; revised_command_line contains the complete command line (including command name(s)), |
| 826 | // fully translated with all substitutions & translations taken care of (still in raw text format); and |
| 827 | // wants_raw_input specifies whether the Execute method expects raw input or not. |
| 828 | |
| 829 | |
| 830 | if (log) |
| 831 | { |
| 832 | log->Printf ("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>"); |
| 833 | log->Printf ("HandleCommand, revised_command_line: '%s'", revised_command_line.GetData()); |
| 834 | log->Printf ("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False"); |
| 835 | } |
| 836 | |
| 837 | // Phase 2. |
| 838 | // Take care of things like setting up the history command & calling the appropriate Execute method on the |
| 839 | // CommandObject, with the appropriate arguments. |
| 840 | |
| 841 | if (cmd_obj != NULL) |
| 842 | { |
| 843 | if (add_to_history) |
| 844 | { |
| 845 | Args command_args (revised_command_line.GetData()); |
| 846 | const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0); |
| 847 | if (repeat_command != NULL) |
| 848 | m_repeat_command.assign(repeat_command); |
| 849 | else |
| 850 | m_repeat_command.assign(command_line); |
| 851 | |
| 852 | m_command_history.push_back (command_line); |
| 853 | } |
| 854 | |
| 855 | command_string = revised_command_line.GetData(); |
| 856 | std::string command_name (cmd_obj->GetCommandName()); |
Caroline Tice | ea6e3df | 2010-12-11 08:16:56 +0000 | [diff] [blame] | 857 | std::string remainder; |
| 858 | if (actual_cmd_name_len < command_string.length()) |
| 859 | remainder = command_string.substr (actual_cmd_name_len); // Note: 'actual_cmd_name_len' may be considerably shorter |
| 860 | // than cmd_obj->GetCommandName(), because name completion |
| 861 | // allows users to enter short versions of the names, |
| 862 | // e.g. 'br s' for 'breakpoint set'. |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 863 | |
| 864 | // Remove any initial spaces |
| 865 | std::string white_space (" \t\v"); |
| 866 | size_t pos = remainder.find_first_not_of (white_space); |
| 867 | if (pos != 0 && pos != std::string::npos) |
| 868 | remainder = remainder.substr (pos); |
| 869 | |
| 870 | if (log) |
| 871 | log->Printf ("HandleCommand, command line after removing command name(s): '%s'\n", remainder.c_str()); |
| 872 | |
| 873 | |
| 874 | if (wants_raw_input) |
| 875 | cmd_obj->ExecuteRawCommandString (remainder.c_str(), result); |
| 876 | else |
| 877 | { |
| 878 | Args cmd_args (remainder.c_str()); |
| 879 | cmd_obj->ExecuteWithOptions (cmd_args, result); |
| 880 | } |
| 881 | } |
| 882 | else |
| 883 | { |
| 884 | // We didn't find the first command object, so complete the first argument. |
| 885 | Args command_args (revised_command_line.GetData()); |
| 886 | StringList matches; |
| 887 | int num_matches; |
| 888 | int cursor_index = 0; |
| 889 | int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0)); |
| 890 | bool word_complete; |
| 891 | num_matches = HandleCompletionMatches (command_args, |
| 892 | cursor_index, |
| 893 | cursor_char_position, |
| 894 | 0, |
| 895 | -1, |
| 896 | word_complete, |
| 897 | matches); |
| 898 | |
| 899 | if (num_matches > 0) |
| 900 | { |
| 901 | std::string error_msg; |
| 902 | error_msg.assign ("ambiguous command '"); |
| 903 | error_msg.append(command_args.GetArgumentAtIndex(0)); |
| 904 | error_msg.append ("'."); |
| 905 | |
| 906 | error_msg.append (" Possible completions:"); |
| 907 | for (int i = 0; i < num_matches; i++) |
| 908 | { |
| 909 | error_msg.append ("\n\t"); |
| 910 | error_msg.append (matches.GetStringAtIndex (i)); |
| 911 | } |
| 912 | error_msg.append ("\n"); |
| 913 | result.AppendRawError (error_msg.c_str(), error_msg.size()); |
| 914 | } |
| 915 | else |
| 916 | result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0)); |
| 917 | |
| 918 | result.SetStatus (eReturnStatusFailed); |
| 919 | } |
| 920 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 921 | return result.Succeeded(); |
| 922 | } |
| 923 | |
| 924 | int |
| 925 | CommandInterpreter::HandleCompletionMatches (Args &parsed_line, |
| 926 | int &cursor_index, |
| 927 | int &cursor_char_position, |
| 928 | int match_start_point, |
| 929 | int max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 930 | bool &word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 931 | StringList &matches) |
| 932 | { |
| 933 | int num_command_matches = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 934 | bool look_for_subcommand = false; |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 935 | |
| 936 | // For any of the command completions a unique match will be a complete word. |
| 937 | word_complete = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 938 | |
| 939 | if (cursor_index == -1) |
| 940 | { |
| 941 | // We got nothing on the command line, so return the list of commands |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 942 | bool include_aliases = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 943 | num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches); |
| 944 | } |
| 945 | else if (cursor_index == 0) |
| 946 | { |
| 947 | // The cursor is in the first argument, so just do a lookup in the dictionary. |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 948 | CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 949 | num_command_matches = matches.GetSize(); |
| 950 | |
| 951 | if (num_command_matches == 1 |
| 952 | && cmd_obj && cmd_obj->IsMultiwordObject() |
| 953 | && matches.GetStringAtIndex(0) != NULL |
| 954 | && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0) |
| 955 | { |
| 956 | look_for_subcommand = true; |
| 957 | num_command_matches = 0; |
| 958 | matches.DeleteStringAtIndex(0); |
| 959 | parsed_line.AppendArgument (""); |
| 960 | cursor_index++; |
| 961 | cursor_char_position = 0; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | if (cursor_index > 0 || look_for_subcommand) |
| 966 | { |
| 967 | // We are completing further on into a commands arguments, so find the command and tell it |
| 968 | // to complete the command. |
| 969 | // First see if there is a matching initial command: |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 970 | CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 971 | if (command_object == NULL) |
| 972 | { |
| 973 | return 0; |
| 974 | } |
| 975 | else |
| 976 | { |
| 977 | parsed_line.Shift(); |
| 978 | cursor_index--; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 979 | num_command_matches = command_object->HandleCompletion (parsed_line, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 980 | cursor_index, |
| 981 | cursor_char_position, |
| 982 | match_start_point, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 983 | max_return_elements, |
| 984 | word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 985 | matches); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | return num_command_matches; |
| 990 | |
| 991 | } |
| 992 | |
| 993 | int |
| 994 | CommandInterpreter::HandleCompletion (const char *current_line, |
| 995 | const char *cursor, |
| 996 | const char *last_char, |
| 997 | int match_start_point, |
| 998 | int max_return_elements, |
| 999 | StringList &matches) |
| 1000 | { |
| 1001 | // We parse the argument up to the cursor, so the last argument in parsed_line is |
| 1002 | // the one containing the cursor, and the cursor is after the last character. |
| 1003 | |
| 1004 | Args parsed_line(current_line, last_char - current_line); |
| 1005 | Args partial_parsed_line(current_line, cursor - current_line); |
| 1006 | |
| 1007 | int num_args = partial_parsed_line.GetArgumentCount(); |
| 1008 | int cursor_index = partial_parsed_line.GetArgumentCount() - 1; |
| 1009 | int cursor_char_position; |
| 1010 | |
| 1011 | if (cursor_index == -1) |
| 1012 | cursor_char_position = 0; |
| 1013 | else |
| 1014 | cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index)); |
Jim Ingham | cf03765 | 2010-12-14 19:56:01 +0000 | [diff] [blame] | 1015 | |
| 1016 | if (cursor > current_line && cursor[-1] == ' ') |
| 1017 | { |
| 1018 | // We are just after a space. If we are in an argument, then we will continue |
| 1019 | // parsing, but if we are between arguments, then we have to complete whatever the next |
| 1020 | // element would be. |
| 1021 | // We can distinguish the two cases because if we are in an argument (e.g. because the space is |
| 1022 | // protected by a quote) then the space will also be in the parsed argument... |
| 1023 | |
| 1024 | const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index); |
| 1025 | if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ') |
| 1026 | { |
| 1027 | parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '"'); |
| 1028 | cursor_index++; |
| 1029 | cursor_char_position = 0; |
| 1030 | } |
| 1031 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1032 | |
| 1033 | int num_command_matches; |
| 1034 | |
| 1035 | matches.Clear(); |
| 1036 | |
| 1037 | // Only max_return_elements == -1 is supported at present: |
| 1038 | assert (max_return_elements == -1); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1039 | bool word_complete; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1040 | num_command_matches = HandleCompletionMatches (parsed_line, |
| 1041 | cursor_index, |
| 1042 | cursor_char_position, |
| 1043 | match_start_point, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1044 | max_return_elements, |
| 1045 | word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1046 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (num_command_matches <= 0) |
| 1049 | return num_command_matches; |
| 1050 | |
| 1051 | if (num_args == 0) |
| 1052 | { |
| 1053 | // If we got an empty string, insert nothing. |
| 1054 | matches.InsertStringAtIndex(0, ""); |
| 1055 | } |
| 1056 | else |
| 1057 | { |
| 1058 | // Now figure out if there is a common substring, and if so put that in element 0, otherwise |
| 1059 | // put an empty string in element 0. |
| 1060 | std::string command_partial_str; |
| 1061 | if (cursor_index >= 0) |
Jim Ingham | e3663e8 | 2010-10-22 18:47:16 +0000 | [diff] [blame] | 1062 | command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index), |
| 1063 | parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1064 | |
| 1065 | std::string common_prefix; |
| 1066 | matches.LongestCommonPrefix (common_prefix); |
| 1067 | int partial_name_len = command_partial_str.size(); |
| 1068 | |
| 1069 | // If we matched a unique single command, add a space... |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1070 | // Only do this if the completer told us this was a complete word, however... |
| 1071 | if (num_command_matches == 1 && word_complete) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1072 | { |
| 1073 | char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index); |
| 1074 | if (quote_char != '\0') |
| 1075 | common_prefix.push_back(quote_char); |
| 1076 | |
| 1077 | common_prefix.push_back(' '); |
| 1078 | } |
| 1079 | common_prefix.erase (0, partial_name_len); |
| 1080 | matches.InsertStringAtIndex(0, common_prefix.c_str()); |
| 1081 | } |
| 1082 | return num_command_matches; |
| 1083 | } |
| 1084 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1085 | |
| 1086 | CommandInterpreter::~CommandInterpreter () |
| 1087 | { |
| 1088 | } |
| 1089 | |
| 1090 | const char * |
| 1091 | CommandInterpreter::GetPrompt () |
| 1092 | { |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1093 | return m_debugger.GetPrompt(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | void |
| 1097 | CommandInterpreter::SetPrompt (const char *new_prompt) |
| 1098 | { |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1099 | m_debugger.SetPrompt (new_prompt); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1102 | size_t |
| 1103 | CommandInterpreter::GetConfirmationInputReaderCallback (void *baton, |
| 1104 | InputReader &reader, |
| 1105 | lldb::InputReaderAction action, |
| 1106 | const char *bytes, |
| 1107 | size_t bytes_len) |
| 1108 | { |
| 1109 | FILE *out_fh = reader.GetDebugger().GetOutputFileHandle(); |
| 1110 | bool *response_ptr = (bool *) baton; |
| 1111 | |
| 1112 | switch (action) |
| 1113 | { |
| 1114 | case eInputReaderActivate: |
| 1115 | if (out_fh) |
| 1116 | { |
| 1117 | if (reader.GetPrompt()) |
| 1118 | ::fprintf (out_fh, "%s", reader.GetPrompt()); |
| 1119 | } |
| 1120 | break; |
| 1121 | |
| 1122 | case eInputReaderDeactivate: |
| 1123 | break; |
| 1124 | |
| 1125 | case eInputReaderReactivate: |
| 1126 | if (out_fh && reader.GetPrompt()) |
| 1127 | ::fprintf (out_fh, "%s", reader.GetPrompt()); |
| 1128 | break; |
| 1129 | |
| 1130 | case eInputReaderGotToken: |
| 1131 | if (bytes_len == 0) |
| 1132 | { |
| 1133 | reader.SetIsDone(true); |
| 1134 | } |
| 1135 | else if (bytes[0] == 'y') |
| 1136 | { |
| 1137 | *response_ptr = true; |
| 1138 | reader.SetIsDone(true); |
| 1139 | } |
| 1140 | else if (bytes[0] == 'n') |
| 1141 | { |
| 1142 | *response_ptr = false; |
| 1143 | reader.SetIsDone(true); |
| 1144 | } |
| 1145 | else |
| 1146 | { |
| 1147 | if (out_fh && !reader.IsDone() && reader.GetPrompt()) |
| 1148 | { |
| 1149 | ::fprintf (out_fh, "Please answer \"y\" or \"n\"\n"); |
| 1150 | ::fprintf (out_fh, "%s", reader.GetPrompt()); |
| 1151 | } |
| 1152 | } |
| 1153 | break; |
| 1154 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1155 | case eInputReaderInterrupt: |
| 1156 | case eInputReaderEndOfFile: |
| 1157 | *response_ptr = false; // Assume ^C or ^D means cancel the proposed action |
| 1158 | reader.SetIsDone (true); |
| 1159 | break; |
| 1160 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1161 | case eInputReaderDone: |
| 1162 | break; |
| 1163 | } |
| 1164 | |
| 1165 | return bytes_len; |
| 1166 | |
| 1167 | } |
| 1168 | |
| 1169 | bool |
| 1170 | CommandInterpreter::Confirm (const char *message, bool default_answer) |
| 1171 | { |
Jim Ingham | 9305747 | 2010-10-04 22:44:14 +0000 | [diff] [blame] | 1172 | // Check AutoConfirm first: |
| 1173 | if (m_debugger.GetAutoConfirm()) |
| 1174 | return default_answer; |
| 1175 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1176 | InputReaderSP reader_sp (new InputReader(GetDebugger())); |
| 1177 | bool response = default_answer; |
| 1178 | if (reader_sp) |
| 1179 | { |
| 1180 | std::string prompt(message); |
| 1181 | prompt.append(": ["); |
| 1182 | if (default_answer) |
| 1183 | prompt.append ("Y/n] "); |
| 1184 | else |
| 1185 | prompt.append ("y/N] "); |
| 1186 | |
| 1187 | Error err (reader_sp->Initialize (CommandInterpreter::GetConfirmationInputReaderCallback, |
| 1188 | &response, // baton |
| 1189 | eInputReaderGranularityLine, // token size, to pass to callback function |
| 1190 | NULL, // end token |
| 1191 | prompt.c_str(), // prompt |
| 1192 | true)); // echo input |
| 1193 | if (err.Success()) |
| 1194 | { |
| 1195 | GetDebugger().PushInputReader (reader_sp); |
| 1196 | } |
| 1197 | reader_sp->WaitOnReaderIsDone(); |
| 1198 | } |
| 1199 | return response; |
| 1200 | } |
| 1201 | |
| 1202 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1203 | void |
| 1204 | CommandInterpreter::CrossRegisterCommand (const char * dest_cmd, const char * object_type) |
| 1205 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 1206 | CommandObjectSP cmd_obj_sp = GetCommandSPExact (dest_cmd, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1207 | |
| 1208 | if (cmd_obj_sp != NULL) |
| 1209 | { |
| 1210 | CommandObject *cmd_obj = cmd_obj_sp.get(); |
| 1211 | if (cmd_obj->IsCrossRefObject ()) |
| 1212 | cmd_obj->AddObject (object_type); |
| 1213 | } |
| 1214 | } |
| 1215 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1216 | OptionArgVectorSP |
| 1217 | CommandInterpreter::GetAliasOptions (const char *alias_name) |
| 1218 | { |
| 1219 | OptionArgMap::iterator pos; |
| 1220 | OptionArgVectorSP ret_val; |
| 1221 | |
| 1222 | std::string alias (alias_name); |
| 1223 | |
| 1224 | if (HasAliasOptions()) |
| 1225 | { |
| 1226 | pos = m_alias_options.find (alias); |
| 1227 | if (pos != m_alias_options.end()) |
| 1228 | ret_val = pos->second; |
| 1229 | } |
| 1230 | |
| 1231 | return ret_val; |
| 1232 | } |
| 1233 | |
| 1234 | void |
| 1235 | CommandInterpreter::RemoveAliasOptions (const char *alias_name) |
| 1236 | { |
| 1237 | OptionArgMap::iterator pos = m_alias_options.find(alias_name); |
| 1238 | if (pos != m_alias_options.end()) |
| 1239 | { |
| 1240 | m_alias_options.erase (pos); |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | void |
| 1245 | CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp) |
| 1246 | { |
| 1247 | m_alias_options[alias_name] = option_arg_vector_sp; |
| 1248 | } |
| 1249 | |
| 1250 | bool |
| 1251 | CommandInterpreter::HasCommands () |
| 1252 | { |
| 1253 | return (!m_command_dict.empty()); |
| 1254 | } |
| 1255 | |
| 1256 | bool |
| 1257 | CommandInterpreter::HasAliases () |
| 1258 | { |
| 1259 | return (!m_alias_dict.empty()); |
| 1260 | } |
| 1261 | |
| 1262 | bool |
| 1263 | CommandInterpreter::HasUserCommands () |
| 1264 | { |
| 1265 | return (!m_user_dict.empty()); |
| 1266 | } |
| 1267 | |
| 1268 | bool |
| 1269 | CommandInterpreter::HasAliasOptions () |
| 1270 | { |
| 1271 | return (!m_alias_options.empty()); |
| 1272 | } |
| 1273 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1274 | void |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 1275 | CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj, |
| 1276 | const char *alias_name, |
| 1277 | Args &cmd_args, |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1278 | std::string &raw_input_string, |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 1279 | CommandReturnObject &result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1280 | { |
| 1281 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1282 | |
| 1283 | bool wants_raw_input = alias_cmd_obj->WantsRawCommandString(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1284 | |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1285 | // Make sure that the alias name is the 0th element in cmd_args |
| 1286 | std::string alias_name_str = alias_name; |
| 1287 | if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0) |
| 1288 | cmd_args.Unshift (alias_name); |
| 1289 | |
| 1290 | Args new_args (alias_cmd_obj->GetCommandName()); |
| 1291 | if (new_args.GetArgumentCount() == 2) |
| 1292 | new_args.Shift(); |
| 1293 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1294 | if (option_arg_vector_sp.get()) |
| 1295 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1296 | if (wants_raw_input) |
| 1297 | { |
| 1298 | // We have a command that both has command options and takes raw input. Make *sure* it has a |
| 1299 | // " -- " in the right place in the raw_input_string. |
| 1300 | size_t pos = raw_input_string.find(" -- "); |
| 1301 | if (pos == std::string::npos) |
| 1302 | { |
| 1303 | // None found; assume it goes at the beginning of the raw input string |
| 1304 | raw_input_string.insert (0, " -- "); |
| 1305 | } |
| 1306 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1307 | |
| 1308 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 1309 | int old_size = cmd_args.GetArgumentCount(); |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 1310 | std::vector<bool> used (old_size + 1, false); |
| 1311 | |
| 1312 | used[0] = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1313 | |
| 1314 | for (int i = 0; i < option_arg_vector->size(); ++i) |
| 1315 | { |
| 1316 | OptionArgPair option_pair = (*option_arg_vector)[i]; |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1317 | OptionArgValue value_pair = option_pair.second; |
| 1318 | int value_type = value_pair.first; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1319 | std::string option = option_pair.first; |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1320 | std::string value = value_pair.second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1321 | if (option.compare ("<argument>") == 0) |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1322 | { |
| 1323 | if (!wants_raw_input |
| 1324 | || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice |
| 1325 | new_args.AppendArgument (value.c_str()); |
| 1326 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1327 | else |
| 1328 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1329 | if (value_type != optional_argument) |
| 1330 | new_args.AppendArgument (option.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1331 | if (value.compare ("<no-argument>") != 0) |
| 1332 | { |
| 1333 | int index = GetOptionArgumentPosition (value.c_str()); |
| 1334 | if (index == 0) |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1335 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1336 | // value was NOT a positional argument; must be a real value |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1337 | if (value_type != optional_argument) |
| 1338 | new_args.AppendArgument (value.c_str()); |
| 1339 | else |
| 1340 | { |
| 1341 | char buffer[255]; |
| 1342 | ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str()); |
| 1343 | new_args.AppendArgument (buffer); |
| 1344 | } |
| 1345 | |
| 1346 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1347 | else if (index >= cmd_args.GetArgumentCount()) |
| 1348 | { |
| 1349 | result.AppendErrorWithFormat |
| 1350 | ("Not enough arguments provided; you need at least %d arguments to use this alias.\n", |
| 1351 | index); |
| 1352 | result.SetStatus (eReturnStatusFailed); |
| 1353 | return; |
| 1354 | } |
| 1355 | else |
| 1356 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1357 | // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string |
| 1358 | size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index)); |
| 1359 | if (strpos != std::string::npos) |
| 1360 | { |
| 1361 | raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index))); |
| 1362 | } |
| 1363 | |
| 1364 | if (value_type != optional_argument) |
| 1365 | new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index)); |
| 1366 | else |
| 1367 | { |
| 1368 | char buffer[255]; |
| 1369 | ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(), |
| 1370 | cmd_args.GetArgumentAtIndex (index)); |
| 1371 | new_args.AppendArgument (buffer); |
| 1372 | } |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 1373 | used[index] = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | for (int j = 0; j < cmd_args.GetArgumentCount(); ++j) |
| 1380 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1381 | if (!used[j] && !wants_raw_input) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1382 | new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j)); |
| 1383 | } |
| 1384 | |
| 1385 | cmd_args.Clear(); |
| 1386 | cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector()); |
| 1387 | } |
| 1388 | else |
| 1389 | { |
| 1390 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1391 | // This alias was not created with any options; nothing further needs to be done, unless it is a command that |
| 1392 | // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw |
| 1393 | // input string. |
| 1394 | if (wants_raw_input) |
| 1395 | { |
| 1396 | cmd_args.Clear(); |
| 1397 | cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector()); |
| 1398 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1399 | return; |
| 1400 | } |
| 1401 | |
| 1402 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1403 | return; |
| 1404 | } |
| 1405 | |
| 1406 | |
| 1407 | int |
| 1408 | CommandInterpreter::GetOptionArgumentPosition (const char *in_string) |
| 1409 | { |
| 1410 | int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position |
| 1411 | // of zero. |
| 1412 | |
| 1413 | char *cptr = (char *) in_string; |
| 1414 | |
| 1415 | // Does it start with '%' |
| 1416 | if (cptr[0] == '%') |
| 1417 | { |
| 1418 | ++cptr; |
| 1419 | |
| 1420 | // Is the rest of it entirely digits? |
| 1421 | if (isdigit (cptr[0])) |
| 1422 | { |
| 1423 | const char *start = cptr; |
| 1424 | while (isdigit (cptr[0])) |
| 1425 | ++cptr; |
| 1426 | |
| 1427 | // We've gotten to the end of the digits; are we at the end of the string? |
| 1428 | if (cptr[0] == '\0') |
| 1429 | position = atoi (start); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | return position; |
| 1434 | } |
| 1435 | |
| 1436 | void |
| 1437 | CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result) |
| 1438 | { |
Greg Clayton | 887aa28 | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 1439 | // Don't parse any .lldbinit files if we were asked not to |
| 1440 | if (m_skip_lldbinit_files) |
| 1441 | return; |
| 1442 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1443 | const char *init_file_path = in_cwd ? "./.lldbinit" : "~/.lldbinit"; |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 1444 | FileSpec init_file (init_file_path, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1445 | // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting |
| 1446 | // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details). |
| 1447 | |
| 1448 | if (init_file.Exists()) |
| 1449 | { |
| 1450 | char path[PATH_MAX]; |
| 1451 | init_file.GetPath(path, sizeof(path)); |
| 1452 | StreamString source_command; |
Johnny Chen | 7c98424 | 2010-07-28 21:16:11 +0000 | [diff] [blame] | 1453 | source_command.Printf ("command source '%s'", path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1454 | HandleCommand (source_command.GetData(), false, result); |
| 1455 | } |
| 1456 | else |
| 1457 | { |
| 1458 | // nothing to be done if the file doesn't exist |
| 1459 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | ScriptInterpreter * |
| 1464 | CommandInterpreter::GetScriptInterpreter () |
| 1465 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1466 | if (m_script_interpreter_ap.get() != NULL) |
| 1467 | return m_script_interpreter_ap.get(); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1468 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1469 | lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage(); |
| 1470 | switch (script_lang) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1471 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1472 | case eScriptLanguageNone: |
| 1473 | m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this)); |
| 1474 | break; |
| 1475 | case eScriptLanguagePython: |
| 1476 | m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this)); |
| 1477 | break; |
| 1478 | default: |
| 1479 | break; |
| 1480 | }; |
| 1481 | |
| 1482 | return m_script_interpreter_ap.get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | |
| 1486 | |
| 1487 | bool |
| 1488 | CommandInterpreter::GetSynchronous () |
| 1489 | { |
| 1490 | return m_synchronous_execution; |
| 1491 | } |
| 1492 | |
| 1493 | void |
| 1494 | CommandInterpreter::SetSynchronous (bool value) |
| 1495 | { |
Johnny Chen | d7a4eb0 | 2010-10-14 01:22:03 +0000 | [diff] [blame] | 1496 | m_synchronous_execution = value; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | void |
| 1500 | CommandInterpreter::OutputFormattedHelpText (Stream &strm, |
| 1501 | const char *word_text, |
| 1502 | const char *separator, |
| 1503 | const char *help_text, |
| 1504 | uint32_t max_word_len) |
| 1505 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1506 | const uint32_t max_columns = m_debugger.GetTerminalWidth(); |
| 1507 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1508 | int indent_size = max_word_len + strlen (separator) + 2; |
| 1509 | |
| 1510 | strm.IndentMore (indent_size); |
| 1511 | |
| 1512 | int len = indent_size + strlen (help_text) + 1; |
| 1513 | char *text = (char *) malloc (len); |
| 1514 | sprintf (text, "%-*s %s %s", max_word_len, word_text, separator, help_text); |
| 1515 | if (text[len - 1] == '\n') |
| 1516 | text[--len] = '\0'; |
| 1517 | |
| 1518 | if (len < max_columns) |
| 1519 | { |
| 1520 | // Output it as a single line. |
| 1521 | strm.Printf ("%s", text); |
| 1522 | } |
| 1523 | else |
| 1524 | { |
| 1525 | // We need to break it up into multiple lines. |
| 1526 | bool first_line = true; |
| 1527 | int text_width; |
| 1528 | int start = 0; |
| 1529 | int end = start; |
| 1530 | int final_end = strlen (text); |
| 1531 | int sub_len; |
| 1532 | |
| 1533 | while (end < final_end) |
| 1534 | { |
| 1535 | if (first_line) |
| 1536 | text_width = max_columns - 1; |
| 1537 | else |
| 1538 | text_width = max_columns - indent_size - 1; |
| 1539 | |
| 1540 | // Don't start the 'text' on a space, since we're already outputting the indentation. |
| 1541 | if (!first_line) |
| 1542 | { |
| 1543 | while ((start < final_end) && (text[start] == ' ')) |
| 1544 | start++; |
| 1545 | } |
| 1546 | |
| 1547 | end = start + text_width; |
| 1548 | if (end > final_end) |
| 1549 | end = final_end; |
| 1550 | else |
| 1551 | { |
| 1552 | // If we're not at the end of the text, make sure we break the line on white space. |
| 1553 | while (end > start |
| 1554 | && text[end] != ' ' && text[end] != '\t' && text[end] != '\n') |
| 1555 | end--; |
| 1556 | } |
| 1557 | |
| 1558 | sub_len = end - start; |
| 1559 | if (start != 0) |
| 1560 | strm.EOL(); |
| 1561 | if (!first_line) |
| 1562 | strm.Indent(); |
| 1563 | else |
| 1564 | first_line = false; |
| 1565 | assert (start <= final_end); |
| 1566 | assert (start + sub_len <= final_end); |
| 1567 | if (sub_len > 0) |
| 1568 | strm.Write (text + start, sub_len); |
| 1569 | start = end + 1; |
| 1570 | } |
| 1571 | } |
| 1572 | strm.EOL(); |
| 1573 | strm.IndentLess(indent_size); |
| 1574 | free (text); |
| 1575 | } |
| 1576 | |
| 1577 | void |
| 1578 | CommandInterpreter::AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word, |
| 1579 | StringList &commands_found, StringList &commands_help) |
| 1580 | { |
| 1581 | CommandObject::CommandMap::const_iterator pos; |
| 1582 | CommandObject::CommandMap sub_cmd_dict = ((CommandObjectMultiword *) cmd_obj)->m_subcommand_dict; |
| 1583 | CommandObject *sub_cmd_obj; |
| 1584 | |
| 1585 | for (pos = sub_cmd_dict.begin(); pos != sub_cmd_dict.end(); ++pos) |
| 1586 | { |
| 1587 | const char * command_name = pos->first.c_str(); |
| 1588 | sub_cmd_obj = pos->second.get(); |
| 1589 | StreamString complete_command_name; |
| 1590 | |
| 1591 | complete_command_name.Printf ("%s %s", prefix, command_name); |
| 1592 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1593 | if (sub_cmd_obj->HelpTextContainsWord (search_word)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1594 | { |
| 1595 | commands_found.AppendString (complete_command_name.GetData()); |
| 1596 | commands_help.AppendString (sub_cmd_obj->GetHelp()); |
| 1597 | } |
| 1598 | |
| 1599 | if (sub_cmd_obj->IsMultiwordObject()) |
| 1600 | AproposAllSubCommands (sub_cmd_obj, complete_command_name.GetData(), search_word, commands_found, |
| 1601 | commands_help); |
| 1602 | } |
| 1603 | |
| 1604 | } |
| 1605 | |
| 1606 | void |
| 1607 | CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found, |
| 1608 | StringList &commands_help) |
| 1609 | { |
| 1610 | CommandObject::CommandMap::const_iterator pos; |
| 1611 | |
| 1612 | for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) |
| 1613 | { |
| 1614 | const char *command_name = pos->first.c_str(); |
| 1615 | CommandObject *cmd_obj = pos->second.get(); |
| 1616 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1617 | if (cmd_obj->HelpTextContainsWord (search_word)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1618 | { |
| 1619 | commands_found.AppendString (command_name); |
| 1620 | commands_help.AppendString (cmd_obj->GetHelp()); |
| 1621 | } |
| 1622 | |
| 1623 | if (cmd_obj->IsMultiwordObject()) |
| 1624 | AproposAllSubCommands (cmd_obj, command_name, search_word, commands_found, commands_help); |
| 1625 | |
| 1626 | } |
| 1627 | } |