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