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