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> |
| 11 | |
| 12 | #include <getopt.h> |
| 13 | #include <stdlib.h> |
| 14 | |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 15 | #include "../Commands/CommandObjectApropos.h" |
| 16 | #include "../Commands/CommandObjectArgs.h" |
| 17 | #include "../Commands/CommandObjectBreakpoint.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 18 | //#include "../Commands/CommandObjectCall.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 19 | #include "../Commands/CommandObjectDisassemble.h" |
| 20 | #include "../Commands/CommandObjectExpression.h" |
| 21 | #include "../Commands/CommandObjectFile.h" |
| 22 | #include "../Commands/CommandObjectFrame.h" |
| 23 | #include "../Commands/CommandObjectHelp.h" |
| 24 | #include "../Commands/CommandObjectImage.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 25 | #include "../Commands/CommandObjectLog.h" |
| 26 | #include "../Commands/CommandObjectMemory.h" |
| 27 | #include "../Commands/CommandObjectProcess.h" |
| 28 | #include "../Commands/CommandObjectQuit.h" |
Eli Friedman | b34d2a2 | 2010-06-09 22:08:29 +0000 | [diff] [blame] | 29 | #include "lldb/Interpreter/CommandObjectRegexCommand.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 30 | #include "../Commands/CommandObjectRegister.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "CommandObjectScript.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 32 | #include "../Commands/CommandObjectSettings.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 33 | #include "../Commands/CommandObjectSource.h" |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 34 | #include "../Commands/CommandObjectCommands.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 35 | #include "../Commands/CommandObjectSyntax.h" |
| 36 | #include "../Commands/CommandObjectTarget.h" |
| 37 | #include "../Commands/CommandObjectThread.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 39 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | #include "lldb/Core/Debugger.h" |
| 41 | #include "lldb/Core/Stream.h" |
| 42 | #include "lldb/Core/Timer.h" |
| 43 | #include "lldb/Target/Process.h" |
| 44 | #include "lldb/Target/Thread.h" |
| 45 | #include "lldb/Target/TargetList.h" |
| 46 | |
| 47 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 48 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 49 | |
| 50 | using namespace lldb; |
| 51 | using namespace lldb_private; |
| 52 | |
| 53 | CommandInterpreter::CommandInterpreter |
| 54 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 55 | Debugger &debugger, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | ScriptLanguage script_language, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 57 | bool synchronous_execution |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | ) : |
| 59 | Broadcaster ("CommandInterpreter"), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 60 | m_debugger (debugger), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 61 | m_synchronous_execution (synchronous_execution) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 63 | const char *dbg_name = debugger.GetInstanceName().AsCString(); |
| 64 | std::string lang_name = ScriptInterpreter::LanguageToString (script_language); |
| 65 | StreamString var_name; |
| 66 | var_name.Printf ("[%s].script-lang", dbg_name); |
Caroline Tice | 1d2aefd | 2010-09-09 06:25:08 +0000 | [diff] [blame] | 67 | debugger.GetSettingsController()->SetVariable (var_name.GetData(), lang_name.c_str(), |
| 68 | lldb::eVarSetOperationAssign, false, |
| 69 | m_debugger.GetInstanceName().AsCString()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
| 73 | CommandInterpreter::Initialize () |
| 74 | { |
| 75 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 76 | |
| 77 | CommandReturnObject result; |
| 78 | |
| 79 | LoadCommandDictionary (); |
| 80 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | // Set up some initial aliases. |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 82 | result.Clear(); HandleCommand ("command alias q quit", false, result); |
| 83 | result.Clear(); HandleCommand ("command alias run process launch", false, result); |
| 84 | result.Clear(); HandleCommand ("command alias r process launch", false, result); |
| 85 | result.Clear(); HandleCommand ("command alias c process continue", false, result); |
| 86 | result.Clear(); HandleCommand ("command alias continue process continue", false, result); |
| 87 | result.Clear(); HandleCommand ("command alias expr expression", false, result); |
| 88 | result.Clear(); HandleCommand ("command alias exit quit", false, result); |
Greg Clayton | 0f3a8eb | 2010-09-16 17:09:23 +0000 | [diff] [blame^] | 89 | result.Clear(); HandleCommand ("command alias b regexp-break", false, result); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 90 | result.Clear(); HandleCommand ("command alias bt thread backtrace", false, result); |
| 91 | result.Clear(); HandleCommand ("command alias si thread step-inst", false, result); |
| 92 | result.Clear(); HandleCommand ("command alias step thread step-in", false, result); |
| 93 | result.Clear(); HandleCommand ("command alias s thread step-in", false, result); |
| 94 | result.Clear(); HandleCommand ("command alias next thread step-over", false, result); |
| 95 | result.Clear(); HandleCommand ("command alias n thread step-over", false, result); |
| 96 | result.Clear(); HandleCommand ("command alias finish thread step-out", false, result); |
| 97 | result.Clear(); HandleCommand ("command alias x memory read", false, result); |
| 98 | result.Clear(); HandleCommand ("command alias l source list", false, result); |
| 99 | result.Clear(); HandleCommand ("command alias list source list", false, result); |
Greg Clayton | 0f3a8eb | 2010-09-16 17:09:23 +0000 | [diff] [blame^] | 100 | result.Clear(); HandleCommand ("command alias p frame variable", false, result); |
| 101 | result.Clear(); HandleCommand ("command alias print frame variable", false, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | const char * |
| 105 | CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg) |
| 106 | { |
| 107 | // This function has not yet been implemented. |
| 108 | |
| 109 | // Look for any embedded script command |
| 110 | // If found, |
| 111 | // get interpreter object from the command dictionary, |
| 112 | // call execute_one_command on it, |
| 113 | // get the results as a string, |
| 114 | // substitute that string for current stuff. |
| 115 | |
| 116 | return arg; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void |
| 121 | CommandInterpreter::LoadCommandDictionary () |
| 122 | { |
| 123 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 124 | |
| 125 | // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT *** |
| 126 | // |
| 127 | // Command objects that are used as cross reference objects (i.e. they inherit from CommandObjectCrossref) |
| 128 | // *MUST* be created and put into the command dictionary *BEFORE* any multi-word commands (which may use |
| 129 | // the cross-referencing stuff) are created!!! |
| 130 | // |
| 131 | // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT *** |
| 132 | |
| 133 | |
| 134 | // Command objects that inherit from CommandObjectCrossref must be created before other command objects |
| 135 | // are created. This is so that when another command is created that needs to go into a crossref object, |
| 136 | // the crossref object exists and is ready to take the cross reference. Put the cross referencing command |
| 137 | // objects into the CommandDictionary now, so they are ready for use when the other commands get created. |
| 138 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | // Non-CommandObjectCrossref commands can now be created. |
| 140 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 141 | lldb::ScriptLanguage script_language; |
| 142 | lldb::SettableVariableType var_type = lldb::eSetVarTypeString; |
| 143 | StringList value; |
| 144 | const char *dbg_name = GetDebugger().GetInstanceName().AsCString(); |
| 145 | StreamString var_name; |
| 146 | var_name.Printf ("[%s].script-lang", dbg_name); |
Caroline Tice | 1d2aefd | 2010-09-09 06:25:08 +0000 | [diff] [blame] | 147 | value = Debugger::GetSettingsController()->GetVariable (var_name.GetData(), var_type, |
| 148 | m_debugger.GetInstanceName().AsCString()); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 149 | bool success; |
| 150 | script_language = Args::StringToScriptLanguage (value.GetStringAtIndex(0), lldb::eScriptLanguageDefault, &success); |
| 151 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos ()); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 153 | m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 154 | //m_command_dict["call"] = CommandObjectSP (new CommandObjectCall ()); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 155 | m_command_dict["commands"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 156 | m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble ()); |
| 157 | m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression ()); |
| 158 | m_command_dict["file"] = CommandObjectSP (new CommandObjectFile ()); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 159 | m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp ()); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 161 | m_command_dict["image"] = CommandObjectSP (new CommandObjectImage (*this)); |
| 162 | m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this)); |
| 163 | m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this)); |
| 164 | m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit ()); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 166 | m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this)); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 167 | m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (script_language)); |
| 168 | m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this)); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 169 | m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 170 | m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this)); |
| 171 | m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 172 | |
| 173 | std::auto_ptr<CommandObjectRegexCommand> |
| 174 | break_regex_cmd_ap(new CommandObjectRegexCommand ("regexp-break", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 175 | "Set a breakpoint using a regular expression to specify the location.", |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | "regexp-break [<file>:<line>]\nregexp-break [<address>]\nregexp-break <...>", 2)); |
| 177 | if (break_regex_cmd_ap.get()) |
| 178 | { |
| 179 | if (break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2") && |
| 180 | break_regex_cmd_ap->AddRegexCommand("^(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1") && |
| 181 | break_regex_cmd_ap->AddRegexCommand("^[\"']?([-+]\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'") && |
| 182 | break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list") && |
| 183 | break_regex_cmd_ap->AddRegexCommand("^(-.*)$", "breakpoint set %1") && |
| 184 | break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1'")) |
| 185 | { |
| 186 | CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release()); |
| 187 | m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | int |
| 193 | CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases, |
| 194 | StringList &matches) |
| 195 | { |
| 196 | CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches); |
| 197 | |
| 198 | if (include_aliases) |
| 199 | { |
| 200 | CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches); |
| 201 | } |
| 202 | |
| 203 | return matches.GetSize(); |
| 204 | } |
| 205 | |
| 206 | CommandObjectSP |
| 207 | CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches) |
| 208 | { |
| 209 | CommandObject::CommandMap::iterator pos; |
| 210 | CommandObjectSP ret_val; |
| 211 | |
| 212 | std::string cmd(cmd_cstr); |
| 213 | |
| 214 | if (HasCommands()) |
| 215 | { |
| 216 | pos = m_command_dict.find(cmd); |
| 217 | if (pos != m_command_dict.end()) |
| 218 | ret_val = pos->second; |
| 219 | } |
| 220 | |
| 221 | if (include_aliases && HasAliases()) |
| 222 | { |
| 223 | pos = m_alias_dict.find(cmd); |
| 224 | if (pos != m_alias_dict.end()) |
| 225 | ret_val = pos->second; |
| 226 | } |
| 227 | |
| 228 | if (HasUserCommands()) |
| 229 | { |
| 230 | pos = m_user_dict.find(cmd); |
| 231 | if (pos != m_user_dict.end()) |
| 232 | ret_val = pos->second; |
| 233 | } |
| 234 | |
| 235 | if (!exact && ret_val == NULL) |
| 236 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 237 | // We will only get into here if we didn't find any exact matches. |
| 238 | |
| 239 | CommandObjectSP user_match_sp, alias_match_sp, real_match_sp; |
| 240 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | StringList local_matches; |
| 242 | if (matches == NULL) |
| 243 | matches = &local_matches; |
| 244 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 245 | unsigned int num_cmd_matches = 0; |
| 246 | unsigned int num_alias_matches = 0; |
| 247 | unsigned int num_user_matches = 0; |
| 248 | |
| 249 | // Look through the command dictionaries one by one, and if we get only one match from any of |
| 250 | // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches. |
| 251 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | if (HasCommands()) |
| 253 | { |
| 254 | num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches); |
| 255 | } |
| 256 | |
| 257 | if (num_cmd_matches == 1) |
| 258 | { |
| 259 | cmd.assign(matches->GetStringAtIndex(0)); |
| 260 | pos = m_command_dict.find(cmd); |
| 261 | if (pos != m_command_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 262 | real_match_sp = pos->second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Jim Ingham | 9a57417 | 2010-06-24 20:28:42 +0000 | [diff] [blame] | 265 | if (include_aliases && HasAliases()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 266 | { |
| 267 | num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches); |
| 268 | |
| 269 | } |
| 270 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 271 | if (num_alias_matches == 1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | { |
| 273 | cmd.assign(matches->GetStringAtIndex (num_cmd_matches)); |
| 274 | pos = m_alias_dict.find(cmd); |
| 275 | if (pos != m_alias_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 276 | alias_match_sp = pos->second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Jim Ingham | 9a57417 | 2010-06-24 20:28:42 +0000 | [diff] [blame] | 279 | if (HasUserCommands()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 280 | { |
| 281 | num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches); |
| 282 | } |
| 283 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 284 | if (num_user_matches == 1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | { |
| 286 | cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches)); |
| 287 | |
| 288 | pos = m_user_dict.find (cmd); |
| 289 | if (pos != m_user_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 290 | user_match_sp = pos->second; |
| 291 | } |
| 292 | |
| 293 | // If we got exactly one match, return that, otherwise return the match list. |
| 294 | |
| 295 | if (num_user_matches + num_cmd_matches + num_alias_matches == 1) |
| 296 | { |
| 297 | if (num_cmd_matches) |
| 298 | return real_match_sp; |
| 299 | else if (num_alias_matches) |
| 300 | return alias_match_sp; |
| 301 | else |
| 302 | return user_match_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 305 | else if (matches && ret_val != NULL) |
| 306 | { |
| 307 | matches->AppendString (cmd_cstr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | |
| 311 | return ret_val; |
| 312 | } |
| 313 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 314 | CommandObjectSP |
| 315 | CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 316 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 317 | return GetCommandSP(cmd_cstr, include_aliases, true, NULL); |
| 318 | } |
| 319 | |
| 320 | CommandObject * |
| 321 | CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases) |
| 322 | { |
| 323 | return GetCommandSPExact (cmd_cstr, include_aliases).get(); |
| 324 | } |
| 325 | |
| 326 | CommandObject * |
| 327 | CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches) |
| 328 | { |
| 329 | CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get(); |
| 330 | |
| 331 | // If we didn't find an exact match to the command string in the commands, look in |
| 332 | // the aliases. |
| 333 | |
| 334 | if (command_obj == NULL) |
| 335 | { |
| 336 | command_obj = GetCommandSP (cmd_cstr, true, true, matches).get(); |
| 337 | } |
| 338 | |
| 339 | // Finally, if there wasn't an exact match among the aliases, look for an inexact match |
| 340 | // in both the commands and the aliases. |
| 341 | |
| 342 | if (command_obj == NULL) |
| 343 | command_obj = GetCommandSP(cmd_cstr, true, false, matches).get(); |
| 344 | |
| 345 | return command_obj; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | bool |
| 349 | CommandInterpreter::CommandExists (const char *cmd) |
| 350 | { |
| 351 | return m_command_dict.find(cmd) != m_command_dict.end(); |
| 352 | } |
| 353 | |
| 354 | bool |
| 355 | CommandInterpreter::AliasExists (const char *cmd) |
| 356 | { |
| 357 | return m_alias_dict.find(cmd) != m_alias_dict.end(); |
| 358 | } |
| 359 | |
| 360 | bool |
| 361 | CommandInterpreter::UserCommandExists (const char *cmd) |
| 362 | { |
| 363 | return m_user_dict.find(cmd) != m_user_dict.end(); |
| 364 | } |
| 365 | |
| 366 | void |
| 367 | CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp) |
| 368 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 369 | command_obj_sp->SetIsAlias (true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 370 | m_alias_dict[alias_name] = command_obj_sp; |
| 371 | } |
| 372 | |
| 373 | bool |
| 374 | CommandInterpreter::RemoveAlias (const char *alias_name) |
| 375 | { |
| 376 | CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name); |
| 377 | if (pos != m_alias_dict.end()) |
| 378 | { |
| 379 | m_alias_dict.erase(pos); |
| 380 | return true; |
| 381 | } |
| 382 | return false; |
| 383 | } |
| 384 | bool |
| 385 | CommandInterpreter::RemoveUser (const char *alias_name) |
| 386 | { |
| 387 | CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name); |
| 388 | if (pos != m_user_dict.end()) |
| 389 | { |
| 390 | m_user_dict.erase(pos); |
| 391 | return true; |
| 392 | } |
| 393 | return false; |
| 394 | } |
| 395 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 396 | void |
| 397 | CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string) |
| 398 | { |
| 399 | help_string.Printf ("'%s", command_name); |
| 400 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
| 401 | |
| 402 | if (option_arg_vector_sp != NULL) |
| 403 | { |
| 404 | OptionArgVector *options = option_arg_vector_sp.get(); |
| 405 | for (int i = 0; i < options->size(); ++i) |
| 406 | { |
| 407 | OptionArgPair cur_option = (*options)[i]; |
| 408 | std::string opt = cur_option.first; |
| 409 | std::string value = cur_option.second; |
| 410 | if (opt.compare("<argument>") == 0) |
| 411 | { |
| 412 | help_string.Printf (" %s", value.c_str()); |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | help_string.Printf (" %s", opt.c_str()); |
| 417 | if ((value.compare ("<no-argument>") != 0) |
| 418 | && (value.compare ("<need-argument") != 0)) |
| 419 | { |
| 420 | help_string.Printf (" %s", value.c_str()); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | help_string.Printf ("'"); |
| 427 | } |
| 428 | |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 429 | size_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 430 | CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict) |
| 431 | { |
| 432 | CommandObject::CommandMap::const_iterator pos; |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 433 | CommandObject::CommandMap::const_iterator end = dict.end(); |
| 434 | size_t max_len = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 435 | |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 436 | for (pos = dict.begin(); pos != end; ++pos) |
| 437 | { |
| 438 | size_t len = pos->first.size(); |
| 439 | if (max_len < len) |
| 440 | max_len = len; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 441 | } |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 442 | return max_len; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | void |
| 446 | CommandInterpreter::GetHelp (CommandReturnObject &result) |
| 447 | { |
| 448 | CommandObject::CommandMap::const_iterator pos; |
| 449 | result.AppendMessage("The following is a list of built-in, permanent debugger commands:"); |
| 450 | result.AppendMessage(""); |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 451 | uint32_t max_len = FindLongestCommandWord (m_command_dict); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 452 | |
| 453 | for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) |
| 454 | { |
| 455 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(), |
| 456 | max_len); |
| 457 | } |
| 458 | result.AppendMessage(""); |
| 459 | |
| 460 | if (m_alias_dict.size() > 0) |
| 461 | { |
Caroline Tice | 00edd3a | 2010-09-13 05:27:16 +0000 | [diff] [blame] | 462 | result.AppendMessage("The following is a list of your current command abbreviations (see 'help commands alias' for more info):"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 463 | result.AppendMessage(""); |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 464 | max_len = FindLongestCommandWord (m_alias_dict); |
| 465 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 466 | for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos) |
| 467 | { |
| 468 | StreamString sstr; |
| 469 | StreamString translation_and_help; |
| 470 | std::string entry_name = pos->first; |
| 471 | std::string second_entry = pos->second.get()->GetCommandName(); |
| 472 | GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr); |
| 473 | |
| 474 | translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp()); |
| 475 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", |
| 476 | translation_and_help.GetData(), max_len); |
| 477 | } |
| 478 | result.AppendMessage(""); |
| 479 | } |
| 480 | |
| 481 | if (m_user_dict.size() > 0) |
| 482 | { |
| 483 | result.AppendMessage ("The following is a list of your current user-defined commands:"); |
| 484 | result.AppendMessage(""); |
| 485 | for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos) |
| 486 | { |
| 487 | result.AppendMessageWithFormat ("%s -- %s\n", pos->first.c_str(), pos->second->GetHelp()); |
| 488 | } |
| 489 | result.AppendMessage(""); |
| 490 | } |
| 491 | |
| 492 | result.AppendMessage("For more information on any particular command, try 'help <command-name>'."); |
| 493 | } |
| 494 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 495 | bool |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 496 | CommandInterpreter::HandleCommand |
| 497 | ( |
| 498 | const char *command_line, |
| 499 | bool add_to_history, |
| 500 | CommandReturnObject &result, |
| 501 | ExecutionContext *override_context |
| 502 | ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 503 | { |
| 504 | // FIXME: there should probably be a mutex to make sure only one thread can |
| 505 | // run the interpreter at a time. |
| 506 | |
| 507 | // TODO: this should be a logging channel in lldb. |
| 508 | // if (DebugSelf()) |
| 509 | // { |
| 510 | // result.AppendMessageWithFormat ("Processing command: %s\n", command_line); |
| 511 | // } |
| 512 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 513 | m_debugger.UpdateExecutionContext (override_context); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 514 | |
| 515 | if (command_line == NULL || command_line[0] == '\0') |
| 516 | { |
| 517 | if (m_command_history.empty()) |
| 518 | { |
| 519 | result.AppendError ("empty command"); |
| 520 | result.SetStatus(eReturnStatusFailed); |
| 521 | return false; |
| 522 | } |
| 523 | else |
| 524 | { |
Jim Ingham | 5d9cbd4 | 2010-07-06 23:48:33 +0000 | [diff] [blame] | 525 | command_line = m_repeat_command.c_str(); |
| 526 | if (m_repeat_command.empty()) |
| 527 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 528 | result.AppendErrorWithFormat("No auto repeat.\n"); |
Jim Ingham | 5d9cbd4 | 2010-07-06 23:48:33 +0000 | [diff] [blame] | 529 | result.SetStatus (eReturnStatusFailed); |
| 530 | return false; |
| 531 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 532 | } |
| 533 | add_to_history = false; |
| 534 | } |
| 535 | |
| 536 | Args command_args(command_line); |
| 537 | |
| 538 | if (command_args.GetArgumentCount() > 0) |
| 539 | { |
| 540 | const char *command_cstr = command_args.GetArgumentAtIndex(0); |
| 541 | if (command_cstr) |
| 542 | { |
| 543 | |
| 544 | // We're looking up the command object here. So first find an exact match to the |
| 545 | // command in the commands. |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 546 | CommandObject *command_obj = GetCommandObject(command_cstr); |
| 547 | |
| 548 | if (command_obj != NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 549 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 550 | if (command_obj->IsAlias()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 551 | { |
| 552 | BuildAliasCommandArgs (command_obj, command_cstr, command_args, result); |
| 553 | if (!result.Succeeded()) |
| 554 | return false; |
| 555 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 556 | |
Jim Ingham | 5d9cbd4 | 2010-07-06 23:48:33 +0000 | [diff] [blame] | 557 | if (add_to_history) |
| 558 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 559 | const char *repeat_command = command_obj->GetRepeatCommand(command_args, 0); |
| 560 | if (repeat_command != NULL) |
Jim Ingham | 5d9cbd4 | 2010-07-06 23:48:33 +0000 | [diff] [blame] | 561 | m_repeat_command.assign(repeat_command); |
| 562 | else |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 563 | m_repeat_command.assign(command_line); |
Jim Ingham | 5d9cbd4 | 2010-07-06 23:48:33 +0000 | [diff] [blame] | 564 | |
| 565 | m_command_history.push_back (command_line); |
| 566 | } |
| 567 | |
| 568 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 569 | if (command_obj->WantsRawCommandString()) |
| 570 | { |
| 571 | const char *stripped_command = ::strstr (command_line, command_cstr); |
| 572 | if (stripped_command) |
| 573 | { |
| 574 | stripped_command += strlen(command_cstr); |
| 575 | while (isspace(*stripped_command)) |
| 576 | ++stripped_command; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 577 | command_obj->ExecuteRawCommandString (*this, stripped_command, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | else |
| 581 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 582 | // Remove the command from the args. |
| 583 | command_args.Shift(); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 584 | command_obj->ExecuteWithOptions (*this, command_args, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | else |
| 588 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 589 | // We didn't find the first command object, so complete the first argument. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 590 | StringList matches; |
| 591 | int num_matches; |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 592 | int cursor_index = 0; |
| 593 | int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0)); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 594 | bool word_complete; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 595 | num_matches = HandleCompletionMatches (command_args, |
| 596 | cursor_index, |
| 597 | cursor_char_position, |
| 598 | 0, |
| 599 | -1, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 600 | word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 601 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 602 | |
| 603 | if (num_matches > 0) |
| 604 | { |
| 605 | std::string error_msg; |
| 606 | error_msg.assign ("ambiguous command '"); |
| 607 | error_msg.append(command_cstr); |
| 608 | error_msg.append ("'."); |
| 609 | |
| 610 | error_msg.append (" Possible completions:"); |
| 611 | for (int i = 0; i < num_matches; i++) |
| 612 | { |
| 613 | error_msg.append ("\n\t"); |
| 614 | error_msg.append (matches.GetStringAtIndex (i)); |
| 615 | } |
| 616 | error_msg.append ("\n"); |
| 617 | result.AppendRawError (error_msg.c_str(), error_msg.size()); |
| 618 | } |
| 619 | else |
| 620 | result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_cstr); |
| 621 | |
| 622 | result.SetStatus (eReturnStatusFailed); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | return result.Succeeded(); |
| 627 | } |
| 628 | |
| 629 | int |
| 630 | CommandInterpreter::HandleCompletionMatches (Args &parsed_line, |
| 631 | int &cursor_index, |
| 632 | int &cursor_char_position, |
| 633 | int match_start_point, |
| 634 | int max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 635 | bool &word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 636 | StringList &matches) |
| 637 | { |
| 638 | int num_command_matches = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 639 | bool look_for_subcommand = false; |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 640 | |
| 641 | // For any of the command completions a unique match will be a complete word. |
| 642 | word_complete = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 643 | |
| 644 | if (cursor_index == -1) |
| 645 | { |
| 646 | // 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] | 647 | bool include_aliases = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 648 | num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches); |
| 649 | } |
| 650 | else if (cursor_index == 0) |
| 651 | { |
| 652 | // The cursor is in the first argument, so just do a lookup in the dictionary. |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 653 | CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 654 | num_command_matches = matches.GetSize(); |
| 655 | |
| 656 | if (num_command_matches == 1 |
| 657 | && cmd_obj && cmd_obj->IsMultiwordObject() |
| 658 | && matches.GetStringAtIndex(0) != NULL |
| 659 | && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0) |
| 660 | { |
| 661 | look_for_subcommand = true; |
| 662 | num_command_matches = 0; |
| 663 | matches.DeleteStringAtIndex(0); |
| 664 | parsed_line.AppendArgument (""); |
| 665 | cursor_index++; |
| 666 | cursor_char_position = 0; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | if (cursor_index > 0 || look_for_subcommand) |
| 671 | { |
| 672 | // We are completing further on into a commands arguments, so find the command and tell it |
| 673 | // to complete the command. |
| 674 | // First see if there is a matching initial command: |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 675 | CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 676 | if (command_object == NULL) |
| 677 | { |
| 678 | return 0; |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | parsed_line.Shift(); |
| 683 | cursor_index--; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 684 | num_command_matches = command_object->HandleCompletion (*this, |
| 685 | parsed_line, |
| 686 | cursor_index, |
| 687 | cursor_char_position, |
| 688 | match_start_point, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 689 | max_return_elements, |
| 690 | word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 691 | matches); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | return num_command_matches; |
| 696 | |
| 697 | } |
| 698 | |
| 699 | int |
| 700 | CommandInterpreter::HandleCompletion (const char *current_line, |
| 701 | const char *cursor, |
| 702 | const char *last_char, |
| 703 | int match_start_point, |
| 704 | int max_return_elements, |
| 705 | StringList &matches) |
| 706 | { |
| 707 | // We parse the argument up to the cursor, so the last argument in parsed_line is |
| 708 | // the one containing the cursor, and the cursor is after the last character. |
| 709 | |
| 710 | Args parsed_line(current_line, last_char - current_line); |
| 711 | Args partial_parsed_line(current_line, cursor - current_line); |
| 712 | |
| 713 | int num_args = partial_parsed_line.GetArgumentCount(); |
| 714 | int cursor_index = partial_parsed_line.GetArgumentCount() - 1; |
| 715 | int cursor_char_position; |
| 716 | |
| 717 | if (cursor_index == -1) |
| 718 | cursor_char_position = 0; |
| 719 | else |
| 720 | cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index)); |
| 721 | |
| 722 | int num_command_matches; |
| 723 | |
| 724 | matches.Clear(); |
| 725 | |
| 726 | // Only max_return_elements == -1 is supported at present: |
| 727 | assert (max_return_elements == -1); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 728 | bool word_complete; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 729 | num_command_matches = HandleCompletionMatches (parsed_line, |
| 730 | cursor_index, |
| 731 | cursor_char_position, |
| 732 | match_start_point, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 733 | max_return_elements, |
| 734 | word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 735 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 736 | |
| 737 | if (num_command_matches <= 0) |
| 738 | return num_command_matches; |
| 739 | |
| 740 | if (num_args == 0) |
| 741 | { |
| 742 | // If we got an empty string, insert nothing. |
| 743 | matches.InsertStringAtIndex(0, ""); |
| 744 | } |
| 745 | else |
| 746 | { |
| 747 | // Now figure out if there is a common substring, and if so put that in element 0, otherwise |
| 748 | // put an empty string in element 0. |
| 749 | std::string command_partial_str; |
| 750 | if (cursor_index >= 0) |
| 751 | command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index), parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position); |
| 752 | |
| 753 | std::string common_prefix; |
| 754 | matches.LongestCommonPrefix (common_prefix); |
| 755 | int partial_name_len = command_partial_str.size(); |
| 756 | |
| 757 | // If we matched a unique single command, add a space... |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 758 | // Only do this if the completer told us this was a complete word, however... |
| 759 | if (num_command_matches == 1 && word_complete) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 760 | { |
| 761 | char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index); |
| 762 | if (quote_char != '\0') |
| 763 | common_prefix.push_back(quote_char); |
| 764 | |
| 765 | common_prefix.push_back(' '); |
| 766 | } |
| 767 | common_prefix.erase (0, partial_name_len); |
| 768 | matches.InsertStringAtIndex(0, common_prefix.c_str()); |
| 769 | } |
| 770 | return num_command_matches; |
| 771 | } |
| 772 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 773 | |
| 774 | CommandInterpreter::~CommandInterpreter () |
| 775 | { |
| 776 | } |
| 777 | |
| 778 | const char * |
| 779 | CommandInterpreter::GetPrompt () |
| 780 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 781 | lldb::SettableVariableType var_type; |
| 782 | const char *instance_name = GetDebugger().GetInstanceName().AsCString(); |
| 783 | StreamString var_name; |
| 784 | var_name.Printf ("[%s].prompt", instance_name); |
Caroline Tice | 1d2aefd | 2010-09-09 06:25:08 +0000 | [diff] [blame] | 785 | return Debugger::GetSettingsController()->GetVariable (var_name.GetData(), var_type, instance_name).GetStringAtIndex(0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | void |
| 789 | CommandInterpreter::SetPrompt (const char *new_prompt) |
| 790 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 791 | const char *instance_name = GetDebugger().GetInstanceName().AsCString(); |
| 792 | StreamString name_str; |
| 793 | name_str.Printf ("[%s].prompt", instance_name); |
Caroline Tice | 1d2aefd | 2010-09-09 06:25:08 +0000 | [diff] [blame] | 794 | Debugger::GetSettingsController()->SetVariable (name_str.GetData(), new_prompt, lldb::eVarSetOperationAssign, |
| 795 | false, m_debugger.GetInstanceName().AsCString()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | void |
| 799 | CommandInterpreter::CrossRegisterCommand (const char * dest_cmd, const char * object_type) |
| 800 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 801 | CommandObjectSP cmd_obj_sp = GetCommandSPExact (dest_cmd, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 802 | |
| 803 | if (cmd_obj_sp != NULL) |
| 804 | { |
| 805 | CommandObject *cmd_obj = cmd_obj_sp.get(); |
| 806 | if (cmd_obj->IsCrossRefObject ()) |
| 807 | cmd_obj->AddObject (object_type); |
| 808 | } |
| 809 | } |
| 810 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 811 | OptionArgVectorSP |
| 812 | CommandInterpreter::GetAliasOptions (const char *alias_name) |
| 813 | { |
| 814 | OptionArgMap::iterator pos; |
| 815 | OptionArgVectorSP ret_val; |
| 816 | |
| 817 | std::string alias (alias_name); |
| 818 | |
| 819 | if (HasAliasOptions()) |
| 820 | { |
| 821 | pos = m_alias_options.find (alias); |
| 822 | if (pos != m_alias_options.end()) |
| 823 | ret_val = pos->second; |
| 824 | } |
| 825 | |
| 826 | return ret_val; |
| 827 | } |
| 828 | |
| 829 | void |
| 830 | CommandInterpreter::RemoveAliasOptions (const char *alias_name) |
| 831 | { |
| 832 | OptionArgMap::iterator pos = m_alias_options.find(alias_name); |
| 833 | if (pos != m_alias_options.end()) |
| 834 | { |
| 835 | m_alias_options.erase (pos); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | void |
| 840 | CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp) |
| 841 | { |
| 842 | m_alias_options[alias_name] = option_arg_vector_sp; |
| 843 | } |
| 844 | |
| 845 | bool |
| 846 | CommandInterpreter::HasCommands () |
| 847 | { |
| 848 | return (!m_command_dict.empty()); |
| 849 | } |
| 850 | |
| 851 | bool |
| 852 | CommandInterpreter::HasAliases () |
| 853 | { |
| 854 | return (!m_alias_dict.empty()); |
| 855 | } |
| 856 | |
| 857 | bool |
| 858 | CommandInterpreter::HasUserCommands () |
| 859 | { |
| 860 | return (!m_user_dict.empty()); |
| 861 | } |
| 862 | |
| 863 | bool |
| 864 | CommandInterpreter::HasAliasOptions () |
| 865 | { |
| 866 | return (!m_alias_options.empty()); |
| 867 | } |
| 868 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 869 | void |
| 870 | CommandInterpreter::BuildAliasCommandArgs |
| 871 | ( |
| 872 | CommandObject *alias_cmd_obj, |
| 873 | const char *alias_name, |
| 874 | Args &cmd_args, |
| 875 | CommandReturnObject &result |
| 876 | ) |
| 877 | { |
| 878 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
| 879 | |
| 880 | if (option_arg_vector_sp.get()) |
| 881 | { |
| 882 | // Make sure that the alias name is the 0th element in cmd_args |
| 883 | std::string alias_name_str = alias_name; |
| 884 | if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0) |
| 885 | cmd_args.Unshift (alias_name); |
| 886 | |
| 887 | Args new_args (alias_cmd_obj->GetCommandName()); |
| 888 | if (new_args.GetArgumentCount() == 2) |
| 889 | new_args.Shift(); |
| 890 | |
| 891 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 892 | int old_size = cmd_args.GetArgumentCount(); |
| 893 | int *used = (int *) malloc ((old_size + 1) * sizeof (int)); |
| 894 | |
| 895 | memset (used, 0, (old_size + 1) * sizeof (int)); |
| 896 | used[0] = 1; |
| 897 | |
| 898 | for (int i = 0; i < option_arg_vector->size(); ++i) |
| 899 | { |
| 900 | OptionArgPair option_pair = (*option_arg_vector)[i]; |
| 901 | std::string option = option_pair.first; |
| 902 | std::string value = option_pair.second; |
| 903 | if (option.compare ("<argument>") == 0) |
| 904 | new_args.AppendArgument (value.c_str()); |
| 905 | else |
| 906 | { |
| 907 | new_args.AppendArgument (option.c_str()); |
| 908 | if (value.compare ("<no-argument>") != 0) |
| 909 | { |
| 910 | int index = GetOptionArgumentPosition (value.c_str()); |
| 911 | if (index == 0) |
| 912 | // value was NOT a positional argument; must be a real value |
| 913 | new_args.AppendArgument (value.c_str()); |
| 914 | else if (index >= cmd_args.GetArgumentCount()) |
| 915 | { |
| 916 | result.AppendErrorWithFormat |
| 917 | ("Not enough arguments provided; you need at least %d arguments to use this alias.\n", |
| 918 | index); |
| 919 | result.SetStatus (eReturnStatusFailed); |
| 920 | return; |
| 921 | } |
| 922 | else |
| 923 | { |
| 924 | new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index)); |
| 925 | used[index] = 1; |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | for (int j = 0; j < cmd_args.GetArgumentCount(); ++j) |
| 932 | { |
| 933 | if (!used[j]) |
| 934 | new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j)); |
| 935 | } |
| 936 | |
| 937 | cmd_args.Clear(); |
| 938 | cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector()); |
| 939 | } |
| 940 | else |
| 941 | { |
| 942 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 943 | // This alias was not created with any options; nothing further needs to be done. |
| 944 | return; |
| 945 | } |
| 946 | |
| 947 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 948 | return; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | int |
| 953 | CommandInterpreter::GetOptionArgumentPosition (const char *in_string) |
| 954 | { |
| 955 | int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position |
| 956 | // of zero. |
| 957 | |
| 958 | char *cptr = (char *) in_string; |
| 959 | |
| 960 | // Does it start with '%' |
| 961 | if (cptr[0] == '%') |
| 962 | { |
| 963 | ++cptr; |
| 964 | |
| 965 | // Is the rest of it entirely digits? |
| 966 | if (isdigit (cptr[0])) |
| 967 | { |
| 968 | const char *start = cptr; |
| 969 | while (isdigit (cptr[0])) |
| 970 | ++cptr; |
| 971 | |
| 972 | // We've gotten to the end of the digits; are we at the end of the string? |
| 973 | if (cptr[0] == '\0') |
| 974 | position = atoi (start); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | return position; |
| 979 | } |
| 980 | |
| 981 | void |
| 982 | CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result) |
| 983 | { |
| 984 | const char *init_file_path = in_cwd ? "./.lldbinit" : "~/.lldbinit"; |
| 985 | FileSpec init_file (init_file_path); |
| 986 | // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting |
| 987 | // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details). |
| 988 | |
| 989 | if (init_file.Exists()) |
| 990 | { |
| 991 | char path[PATH_MAX]; |
| 992 | init_file.GetPath(path, sizeof(path)); |
| 993 | StreamString source_command; |
Johnny Chen | 7c98424 | 2010-07-28 21:16:11 +0000 | [diff] [blame] | 994 | source_command.Printf ("command source '%s'", path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 995 | HandleCommand (source_command.GetData(), false, result); |
| 996 | } |
| 997 | else |
| 998 | { |
| 999 | // nothing to be done if the file doesn't exist |
| 1000 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | ScriptInterpreter * |
| 1005 | CommandInterpreter::GetScriptInterpreter () |
| 1006 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1007 | CommandObject::CommandMap::iterator pos; |
| 1008 | |
| 1009 | pos = m_command_dict.find ("script"); |
| 1010 | if (pos != m_command_dict.end()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1011 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1012 | CommandObject *script_cmd_obj = pos->second.get(); |
| 1013 | return ((CommandObjectScript *) script_cmd_obj)->GetInterpreter (*this); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1014 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1015 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | bool |
| 1021 | CommandInterpreter::GetSynchronous () |
| 1022 | { |
| 1023 | return m_synchronous_execution; |
| 1024 | } |
| 1025 | |
| 1026 | void |
| 1027 | CommandInterpreter::SetSynchronous (bool value) |
| 1028 | { |
| 1029 | static bool value_set_once = false; |
| 1030 | if (!value_set_once) |
| 1031 | { |
| 1032 | value_set_once = true; |
| 1033 | m_synchronous_execution = value; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | void |
| 1038 | CommandInterpreter::OutputFormattedHelpText (Stream &strm, |
| 1039 | const char *word_text, |
| 1040 | const char *separator, |
| 1041 | const char *help_text, |
| 1042 | uint32_t max_word_len) |
| 1043 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1044 | lldb::SettableVariableType var_type; |
| 1045 | const char *width_value = |
Caroline Tice | 1d2aefd | 2010-09-09 06:25:08 +0000 | [diff] [blame] | 1046 | Debugger::GetSettingsController()->GetVariable ("term-width", var_type, |
| 1047 | m_debugger.GetInstanceName().AsCString()).GetStringAtIndex(0); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1048 | int max_columns = atoi (width_value); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1049 | // Sanity check max_columns, to cope with emacs shell mode with TERM=dumb |
| 1050 | // (0 rows; 0 columns;). |
| 1051 | if (max_columns <= 0) max_columns = 80; |
| 1052 | |
| 1053 | int indent_size = max_word_len + strlen (separator) + 2; |
| 1054 | |
| 1055 | strm.IndentMore (indent_size); |
| 1056 | |
| 1057 | int len = indent_size + strlen (help_text) + 1; |
| 1058 | char *text = (char *) malloc (len); |
| 1059 | sprintf (text, "%-*s %s %s", max_word_len, word_text, separator, help_text); |
| 1060 | if (text[len - 1] == '\n') |
| 1061 | text[--len] = '\0'; |
| 1062 | |
| 1063 | if (len < max_columns) |
| 1064 | { |
| 1065 | // Output it as a single line. |
| 1066 | strm.Printf ("%s", text); |
| 1067 | } |
| 1068 | else |
| 1069 | { |
| 1070 | // We need to break it up into multiple lines. |
| 1071 | bool first_line = true; |
| 1072 | int text_width; |
| 1073 | int start = 0; |
| 1074 | int end = start; |
| 1075 | int final_end = strlen (text); |
| 1076 | int sub_len; |
| 1077 | |
| 1078 | while (end < final_end) |
| 1079 | { |
| 1080 | if (first_line) |
| 1081 | text_width = max_columns - 1; |
| 1082 | else |
| 1083 | text_width = max_columns - indent_size - 1; |
| 1084 | |
| 1085 | // Don't start the 'text' on a space, since we're already outputting the indentation. |
| 1086 | if (!first_line) |
| 1087 | { |
| 1088 | while ((start < final_end) && (text[start] == ' ')) |
| 1089 | start++; |
| 1090 | } |
| 1091 | |
| 1092 | end = start + text_width; |
| 1093 | if (end > final_end) |
| 1094 | end = final_end; |
| 1095 | else |
| 1096 | { |
| 1097 | // If we're not at the end of the text, make sure we break the line on white space. |
| 1098 | while (end > start |
| 1099 | && text[end] != ' ' && text[end] != '\t' && text[end] != '\n') |
| 1100 | end--; |
| 1101 | } |
| 1102 | |
| 1103 | sub_len = end - start; |
| 1104 | if (start != 0) |
| 1105 | strm.EOL(); |
| 1106 | if (!first_line) |
| 1107 | strm.Indent(); |
| 1108 | else |
| 1109 | first_line = false; |
| 1110 | assert (start <= final_end); |
| 1111 | assert (start + sub_len <= final_end); |
| 1112 | if (sub_len > 0) |
| 1113 | strm.Write (text + start, sub_len); |
| 1114 | start = end + 1; |
| 1115 | } |
| 1116 | } |
| 1117 | strm.EOL(); |
| 1118 | strm.IndentLess(indent_size); |
| 1119 | free (text); |
| 1120 | } |
| 1121 | |
| 1122 | void |
| 1123 | CommandInterpreter::AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word, |
| 1124 | StringList &commands_found, StringList &commands_help) |
| 1125 | { |
| 1126 | CommandObject::CommandMap::const_iterator pos; |
| 1127 | CommandObject::CommandMap sub_cmd_dict = ((CommandObjectMultiword *) cmd_obj)->m_subcommand_dict; |
| 1128 | CommandObject *sub_cmd_obj; |
| 1129 | |
| 1130 | for (pos = sub_cmd_dict.begin(); pos != sub_cmd_dict.end(); ++pos) |
| 1131 | { |
| 1132 | const char * command_name = pos->first.c_str(); |
| 1133 | sub_cmd_obj = pos->second.get(); |
| 1134 | StreamString complete_command_name; |
| 1135 | |
| 1136 | complete_command_name.Printf ("%s %s", prefix, command_name); |
| 1137 | |
Caroline Tice | 1d2aefd | 2010-09-09 06:25:08 +0000 | [diff] [blame] | 1138 | if (sub_cmd_obj->HelpTextContainsWord (search_word, *this)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1139 | { |
| 1140 | commands_found.AppendString (complete_command_name.GetData()); |
| 1141 | commands_help.AppendString (sub_cmd_obj->GetHelp()); |
| 1142 | } |
| 1143 | |
| 1144 | if (sub_cmd_obj->IsMultiwordObject()) |
| 1145 | AproposAllSubCommands (sub_cmd_obj, complete_command_name.GetData(), search_word, commands_found, |
| 1146 | commands_help); |
| 1147 | } |
| 1148 | |
| 1149 | } |
| 1150 | |
| 1151 | void |
| 1152 | CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found, |
| 1153 | StringList &commands_help) |
| 1154 | { |
| 1155 | CommandObject::CommandMap::const_iterator pos; |
| 1156 | |
| 1157 | for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) |
| 1158 | { |
| 1159 | const char *command_name = pos->first.c_str(); |
| 1160 | CommandObject *cmd_obj = pos->second.get(); |
| 1161 | |
Caroline Tice | 1d2aefd | 2010-09-09 06:25:08 +0000 | [diff] [blame] | 1162 | if (cmd_obj->HelpTextContainsWord (search_word, *this)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1163 | { |
| 1164 | commands_found.AppendString (command_name); |
| 1165 | commands_help.AppendString (cmd_obj->GetHelp()); |
| 1166 | } |
| 1167 | |
| 1168 | if (cmd_obj->IsMultiwordObject()) |
| 1169 | AproposAllSubCommands (cmd_obj, command_name, search_word, commands_found, commands_help); |
| 1170 | |
| 1171 | } |
| 1172 | } |