Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandInterpreter.cpp ----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <string> |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 11 | #include <vector> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | |
| 13 | #include <getopt.h> |
| 14 | #include <stdlib.h> |
| 15 | |
Greg Clayton | 5c28dd1 | 2011-06-23 17:59:56 +0000 | [diff] [blame] | 16 | #include "CommandObjectScript.h" |
Peter Collingbourne | 921fac0 | 2011-06-23 20:37:26 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/CommandObjectRegexCommand.h" |
Greg Clayton | 5c28dd1 | 2011-06-23 17:59:56 +0000 | [diff] [blame] | 18 | |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 19 | #include "../Commands/CommandObjectApropos.h" |
| 20 | #include "../Commands/CommandObjectArgs.h" |
| 21 | #include "../Commands/CommandObjectBreakpoint.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 22 | #include "../Commands/CommandObjectDisassemble.h" |
| 23 | #include "../Commands/CommandObjectExpression.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 24 | #include "../Commands/CommandObjectFrame.h" |
| 25 | #include "../Commands/CommandObjectHelp.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 26 | #include "../Commands/CommandObjectLog.h" |
| 27 | #include "../Commands/CommandObjectMemory.h" |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 28 | #include "../Commands/CommandObjectPlatform.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 29 | #include "../Commands/CommandObjectProcess.h" |
| 30 | #include "../Commands/CommandObjectQuit.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 31 | #include "../Commands/CommandObjectRegister.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" |
Greg Clayton | 5c28dd1 | 2011-06-23 17:59:56 +0000 | [diff] [blame] | 38 | #include "../Commands/CommandObjectType.h" |
Johnny Chen | 902e018 | 2010-12-23 20:21:44 +0000 | [diff] [blame] | 39 | #include "../Commands/CommandObjectVersion.h" |
Johnny Chen | 01acfa7 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 40 | #include "../Commands/CommandObjectWatchpoint.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 42 | #include "lldb/Interpreter/Args.h" |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 43 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | #include "lldb/Core/Debugger.h" |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 45 | #include "lldb/Core/InputReader.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | #include "lldb/Core/Stream.h" |
| 47 | #include "lldb/Core/Timer.h" |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 48 | #include "lldb/Host/Host.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | #include "lldb/Target/Process.h" |
| 50 | #include "lldb/Target/Thread.h" |
| 51 | #include "lldb/Target/TargetList.h" |
Greg Clayton | e98ac25 | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 52 | #include "lldb/Utility/CleanUp.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | |
| 54 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 55 | #include "lldb/Interpreter/CommandInterpreter.h" |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 56 | #include "lldb/Interpreter/ScriptInterpreterNone.h" |
| 57 | #include "lldb/Interpreter/ScriptInterpreterPython.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | |
| 59 | using namespace lldb; |
| 60 | using namespace lldb_private; |
| 61 | |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 62 | ConstString & |
| 63 | CommandInterpreter::GetStaticBroadcasterClass () |
| 64 | { |
| 65 | static ConstString class_name ("lldb.commandInterpreter"); |
| 66 | return class_name; |
| 67 | } |
| 68 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | CommandInterpreter::CommandInterpreter |
| 70 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 71 | Debugger &debugger, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | ScriptLanguage script_language, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 73 | bool synchronous_execution |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | ) : |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 75 | Broadcaster (&debugger, "lldb.command-interpreter"), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 76 | m_debugger (debugger), |
Greg Clayton | 887aa28 | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 77 | m_synchronous_execution (synchronous_execution), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 78 | m_skip_lldbinit_files (false), |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 79 | m_skip_app_init_files (false), |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 80 | m_script_interpreter_ap (), |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 81 | m_comment_char ('#'), |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 82 | m_repeat_char ('!'), |
Enrico Granata | 01bc2d4 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 83 | m_truncation_warning(eNoTruncation), |
| 84 | m_command_source_depth (0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 86 | const char *dbg_name = debugger.GetInstanceName().AsCString(); |
| 87 | std::string lang_name = ScriptInterpreter::LanguageToString (script_language); |
| 88 | StreamString var_name; |
| 89 | var_name.Printf ("[%s].script-lang", dbg_name); |
Caroline Tice | 1d2aefd | 2010-09-09 06:25:08 +0000 | [diff] [blame] | 90 | debugger.GetSettingsController()->SetVariable (var_name.GetData(), lang_name.c_str(), |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 91 | eVarSetOperationAssign, false, |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 92 | m_debugger.GetInstanceName().AsCString()); |
| 93 | SetEventName (eBroadcastBitThreadShouldExit, "thread-should-exit"); |
| 94 | SetEventName (eBroadcastBitResetPrompt, "reset-prompt"); |
| 95 | SetEventName (eBroadcastBitQuitCommandReceived, "quit"); |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 96 | |
| 97 | CheckInWithManager (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | CommandInterpreter::Initialize () |
| 102 | { |
| 103 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 104 | |
| 105 | CommandReturnObject result; |
| 106 | |
| 107 | LoadCommandDictionary (); |
| 108 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | // Set up some initial aliases. |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 110 | CommandObjectSP cmd_obj_sp = GetCommandSPExact ("quit", false); |
| 111 | if (cmd_obj_sp) |
| 112 | { |
| 113 | AddAlias ("q", cmd_obj_sp); |
| 114 | AddAlias ("exit", cmd_obj_sp); |
| 115 | } |
Sean Callanan | fc58af2 | 2012-05-04 23:15:02 +0000 | [diff] [blame] | 116 | |
| 117 | cmd_obj_sp = GetCommandSPExact ("process attach", false); |
| 118 | if (cmd_obj_sp) |
| 119 | { |
| 120 | AddAlias ("attach", cmd_obj_sp); |
| 121 | } |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 122 | |
| 123 | cmd_obj_sp = GetCommandSPExact ("process continue", false); |
| 124 | if (cmd_obj_sp) |
| 125 | { |
| 126 | AddAlias ("c", cmd_obj_sp); |
| 127 | AddAlias ("continue", cmd_obj_sp); |
| 128 | } |
| 129 | |
| 130 | cmd_obj_sp = GetCommandSPExact ("_regexp-break",false); |
| 131 | if (cmd_obj_sp) |
| 132 | AddAlias ("b", cmd_obj_sp); |
| 133 | |
| 134 | cmd_obj_sp = GetCommandSPExact ("thread backtrace", false); |
| 135 | if (cmd_obj_sp) |
| 136 | AddAlias ("bt", cmd_obj_sp); |
| 137 | |
| 138 | cmd_obj_sp = GetCommandSPExact ("thread step-inst", false); |
| 139 | if (cmd_obj_sp) |
Jason Molenda | 47eb00e | 2011-10-22 00:47:41 +0000 | [diff] [blame] | 140 | { |
| 141 | AddAlias ("stepi", cmd_obj_sp); |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 142 | AddAlias ("si", cmd_obj_sp); |
Jason Molenda | 47eb00e | 2011-10-22 00:47:41 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | cmd_obj_sp = GetCommandSPExact ("thread step-inst-over", false); |
| 146 | if (cmd_obj_sp) |
| 147 | { |
| 148 | AddAlias ("nexti", cmd_obj_sp); |
| 149 | AddAlias ("ni", cmd_obj_sp); |
| 150 | } |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 151 | |
| 152 | cmd_obj_sp = GetCommandSPExact ("thread step-in", false); |
| 153 | if (cmd_obj_sp) |
| 154 | { |
| 155 | AddAlias ("s", cmd_obj_sp); |
| 156 | AddAlias ("step", cmd_obj_sp); |
| 157 | } |
| 158 | |
| 159 | cmd_obj_sp = GetCommandSPExact ("thread step-over", false); |
| 160 | if (cmd_obj_sp) |
| 161 | { |
| 162 | AddAlias ("n", cmd_obj_sp); |
| 163 | AddAlias ("next", cmd_obj_sp); |
| 164 | } |
| 165 | |
| 166 | cmd_obj_sp = GetCommandSPExact ("thread step-out", false); |
| 167 | if (cmd_obj_sp) |
| 168 | { |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 169 | AddAlias ("finish", cmd_obj_sp); |
| 170 | } |
| 171 | |
Jim Ingham | 5935525 | 2011-12-02 01:12:59 +0000 | [diff] [blame] | 172 | cmd_obj_sp = GetCommandSPExact ("frame select", false); |
| 173 | if (cmd_obj_sp) |
| 174 | { |
| 175 | AddAlias ("f", cmd_obj_sp); |
| 176 | } |
| 177 | |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 178 | cmd_obj_sp = GetCommandSPExact ("source list", false); |
| 179 | if (cmd_obj_sp) |
| 180 | { |
| 181 | AddAlias ("l", cmd_obj_sp); |
| 182 | AddAlias ("list", cmd_obj_sp); |
| 183 | } |
| 184 | |
| 185 | cmd_obj_sp = GetCommandSPExact ("memory read", false); |
| 186 | if (cmd_obj_sp) |
| 187 | AddAlias ("x", cmd_obj_sp); |
| 188 | |
| 189 | cmd_obj_sp = GetCommandSPExact ("_regexp-up", false); |
| 190 | if (cmd_obj_sp) |
| 191 | AddAlias ("up", cmd_obj_sp); |
| 192 | |
| 193 | cmd_obj_sp = GetCommandSPExact ("_regexp-down", false); |
| 194 | if (cmd_obj_sp) |
| 195 | AddAlias ("down", cmd_obj_sp); |
| 196 | |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 197 | cmd_obj_sp = GetCommandSPExact ("_regexp-display", false); |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 198 | if (cmd_obj_sp) |
| 199 | AddAlias ("display", cmd_obj_sp); |
Jim Ingham | 9d1acc1 | 2011-10-24 18:37:00 +0000 | [diff] [blame] | 200 | |
| 201 | cmd_obj_sp = GetCommandSPExact ("disassemble", false); |
| 202 | if (cmd_obj_sp) |
| 203 | AddAlias ("dis", cmd_obj_sp); |
| 204 | |
| 205 | cmd_obj_sp = GetCommandSPExact ("disassemble", false); |
| 206 | if (cmd_obj_sp) |
| 207 | AddAlias ("di", cmd_obj_sp); |
| 208 | |
| 209 | |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 210 | |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 211 | cmd_obj_sp = GetCommandSPExact ("_regexp-undisplay", false); |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 212 | if (cmd_obj_sp) |
| 213 | AddAlias ("undisplay", cmd_obj_sp); |
| 214 | |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 215 | cmd_obj_sp = GetCommandSPExact ("target create", false); |
| 216 | if (cmd_obj_sp) |
| 217 | AddAlias ("file", cmd_obj_sp); |
| 218 | |
| 219 | cmd_obj_sp = GetCommandSPExact ("target modules", false); |
| 220 | if (cmd_obj_sp) |
| 221 | AddAlias ("image", cmd_obj_sp); |
| 222 | |
| 223 | |
| 224 | OptionArgVectorSP alias_arguments_vector_sp (new OptionArgVector); |
Jim Ingham | e56493f | 2011-03-22 02:29:32 +0000 | [diff] [blame] | 225 | |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 226 | cmd_obj_sp = GetCommandSPExact ("expression", false); |
| 227 | if (cmd_obj_sp) |
| 228 | { |
| 229 | AddAlias ("expr", cmd_obj_sp); |
| 230 | |
| 231 | ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp); |
| 232 | AddAlias ("p", cmd_obj_sp); |
| 233 | AddAlias ("print", cmd_obj_sp); |
Sean Callanan | 59959eb | 2012-08-08 01:30:34 +0000 | [diff] [blame^] | 234 | AddAlias ("call", cmd_obj_sp); |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 235 | AddOrReplaceAliasOptions ("p", alias_arguments_vector_sp); |
| 236 | AddOrReplaceAliasOptions ("print", alias_arguments_vector_sp); |
Sean Callanan | 59959eb | 2012-08-08 01:30:34 +0000 | [diff] [blame^] | 237 | AddOrReplaceAliasOptions ("call", alias_arguments_vector_sp); |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 238 | |
| 239 | alias_arguments_vector_sp.reset (new OptionArgVector); |
| 240 | ProcessAliasOptionsArgs (cmd_obj_sp, "-o --", alias_arguments_vector_sp); |
| 241 | AddAlias ("po", cmd_obj_sp); |
| 242 | AddOrReplaceAliasOptions ("po", alias_arguments_vector_sp); |
| 243 | } |
| 244 | |
Sean Callanan | ee301fa | 2012-06-01 23:29:32 +0000 | [diff] [blame] | 245 | cmd_obj_sp = GetCommandSPExact ("process kill", false); |
| 246 | if (cmd_obj_sp) |
| 247 | AddAlias ("kill", cmd_obj_sp); |
| 248 | |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 249 | cmd_obj_sp = GetCommandSPExact ("process launch", false); |
| 250 | if (cmd_obj_sp) |
| 251 | { |
| 252 | alias_arguments_vector_sp.reset (new OptionArgVector); |
Jason Molenda | 36eb7c0 | 2012-07-06 02:46:23 +0000 | [diff] [blame] | 253 | #if defined (__arm__) |
| 254 | ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp); |
| 255 | #else |
Greg Clayton | 86c50d7 | 2012-05-18 00:04:38 +0000 | [diff] [blame] | 256 | ProcessAliasOptionsArgs (cmd_obj_sp, "--shell=/bin/bash --", alias_arguments_vector_sp); |
Jason Molenda | 36eb7c0 | 2012-07-06 02:46:23 +0000 | [diff] [blame] | 257 | #endif |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 258 | AddAlias ("r", cmd_obj_sp); |
| 259 | AddAlias ("run", cmd_obj_sp); |
| 260 | AddOrReplaceAliasOptions ("r", alias_arguments_vector_sp); |
| 261 | AddOrReplaceAliasOptions ("run", alias_arguments_vector_sp); |
| 262 | } |
Greg Clayton | c84623f | 2012-03-29 21:47:51 +0000 | [diff] [blame] | 263 | |
| 264 | cmd_obj_sp = GetCommandSPExact ("target symbols add", false); |
| 265 | if (cmd_obj_sp) |
| 266 | { |
| 267 | AddAlias ("add-dsym", cmd_obj_sp); |
| 268 | } |
Sean Callanan | 7b71b17 | 2012-05-21 18:25:19 +0000 | [diff] [blame] | 269 | |
| 270 | cmd_obj_sp = GetCommandSPExact ("breakpoint set", false); |
| 271 | if (cmd_obj_sp) |
| 272 | { |
| 273 | alias_arguments_vector_sp.reset (new OptionArgVector); |
| 274 | ProcessAliasOptionsArgs (cmd_obj_sp, "--func-regex %1", alias_arguments_vector_sp); |
| 275 | AddAlias ("rb", cmd_obj_sp); |
| 276 | AddOrReplaceAliasOptions("rb", alias_arguments_vector_sp); |
| 277 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 280 | const char * |
| 281 | CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg) |
| 282 | { |
| 283 | // This function has not yet been implemented. |
| 284 | |
| 285 | // Look for any embedded script command |
| 286 | // If found, |
| 287 | // get interpreter object from the command dictionary, |
| 288 | // call execute_one_command on it, |
| 289 | // get the results as a string, |
| 290 | // substitute that string for current stuff. |
| 291 | |
| 292 | return arg; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | void |
| 297 | CommandInterpreter::LoadCommandDictionary () |
| 298 | { |
| 299 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 300 | |
| 301 | // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT *** |
| 302 | // |
| 303 | // Command objects that are used as cross reference objects (i.e. they inherit from CommandObjectCrossref) |
| 304 | // *MUST* be created and put into the command dictionary *BEFORE* any multi-word commands (which may use |
| 305 | // the cross-referencing stuff) are created!!! |
| 306 | // |
| 307 | // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT *** |
| 308 | |
| 309 | |
| 310 | // Command objects that inherit from CommandObjectCrossref must be created before other command objects |
| 311 | // are created. This is so that when another command is created that needs to go into a crossref object, |
| 312 | // the crossref object exists and is ready to take the cross reference. Put the cross referencing command |
| 313 | // objects into the CommandDictionary now, so they are ready for use when the other commands get created. |
| 314 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | // Non-CommandObjectCrossref commands can now be created. |
| 316 | |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 317 | lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage(); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 318 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 319 | m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 320 | m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 321 | //m_command_dict["call"] = CommandObjectSP (new CommandObjectCall (*this)); |
Johnny Chen | 9e4c3d7 | 2011-04-21 00:39:18 +0000 | [diff] [blame] | 322 | m_command_dict["command"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 323 | m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this)); |
| 324 | m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this)); |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 325 | // m_command_dict["file"] = CommandObjectSP (new CommandObjectFile (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 326 | m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 327 | m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this)); |
Greg Clayton | e1f50b9 | 2011-05-03 22:09:39 +0000 | [diff] [blame] | 328 | /// m_command_dict["image"] = CommandObjectSP (new CommandObjectImage (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 329 | m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this)); |
| 330 | m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this)); |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 331 | m_command_dict["platform"] = CommandObjectSP (new CommandObjectPlatform (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 332 | m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 333 | m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 334 | m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 335 | m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language)); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 336 | m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this)); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 337 | m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 338 | m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this)); |
| 339 | m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this)); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 340 | m_command_dict["type"] = CommandObjectSP (new CommandObjectType (*this)); |
Johnny Chen | 902e018 | 2010-12-23 20:21:44 +0000 | [diff] [blame] | 341 | m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this)); |
Johnny Chen | 01acfa7 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 342 | m_command_dict["watchpoint"]= CommandObjectSP (new CommandObjectMultiwordWatchpoint (*this)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 343 | |
| 344 | std::auto_ptr<CommandObjectRegexCommand> |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 345 | break_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 346 | "_regexp-break", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 347 | "Set a breakpoint using a regular expression to specify the location.", |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 348 | "_regexp-break [<filename>:<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>", 2)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 349 | if (break_regex_cmd_ap.get()) |
| 350 | { |
| 351 | if (break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2") && |
| 352 | break_regex_cmd_ap->AddRegexCommand("^(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1") && |
| 353 | break_regex_cmd_ap->AddRegexCommand("^[\"']?([-+]\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'") && |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 354 | break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full") && |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 355 | break_regex_cmd_ap->AddRegexCommand("^(-.*)$", "breakpoint set %1") && |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 356 | break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%2' --shlib '%1'") && |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 357 | break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1'")) |
| 358 | { |
| 359 | CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release()); |
| 360 | m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp; |
| 361 | } |
| 362 | } |
Jim Ingham | e56493f | 2011-03-22 02:29:32 +0000 | [diff] [blame] | 363 | |
| 364 | std::auto_ptr<CommandObjectRegexCommand> |
| 365 | down_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 366 | "_regexp-down", |
| 367 | "Go down \"n\" frames in the stack (1 frame by default).", |
| 368 | "_regexp-down [n]", 2)); |
Jim Ingham | e56493f | 2011-03-22 02:29:32 +0000 | [diff] [blame] | 369 | if (down_regex_cmd_ap.get()) |
| 370 | { |
| 371 | if (down_regex_cmd_ap->AddRegexCommand("^$", "frame select -r -1") && |
| 372 | down_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r -%1")) |
| 373 | { |
| 374 | CommandObjectSP down_regex_cmd_sp(down_regex_cmd_ap.release()); |
| 375 | m_command_dict[down_regex_cmd_sp->GetCommandName ()] = down_regex_cmd_sp; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | std::auto_ptr<CommandObjectRegexCommand> |
| 380 | up_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 381 | "_regexp-up", |
| 382 | "Go up \"n\" frames in the stack (1 frame by default).", |
| 383 | "_regexp-up [n]", 2)); |
Jim Ingham | e56493f | 2011-03-22 02:29:32 +0000 | [diff] [blame] | 384 | if (up_regex_cmd_ap.get()) |
| 385 | { |
| 386 | if (up_regex_cmd_ap->AddRegexCommand("^$", "frame select -r 1") && |
| 387 | up_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r %1")) |
| 388 | { |
| 389 | CommandObjectSP up_regex_cmd_sp(up_regex_cmd_ap.release()); |
| 390 | m_command_dict[up_regex_cmd_sp->GetCommandName ()] = up_regex_cmd_sp; |
| 391 | } |
| 392 | } |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 393 | |
| 394 | std::auto_ptr<CommandObjectRegexCommand> |
| 395 | display_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 396 | "_regexp-display", |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 397 | "Add an expression evaluation stop-hook.", |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 398 | "_regexp-display expression", 2)); |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 399 | if (display_regex_cmd_ap.get()) |
| 400 | { |
| 401 | if (display_regex_cmd_ap->AddRegexCommand("^(.+)$", "target stop-hook add -o \"expr -- %1\"")) |
| 402 | { |
| 403 | CommandObjectSP display_regex_cmd_sp(display_regex_cmd_ap.release()); |
| 404 | m_command_dict[display_regex_cmd_sp->GetCommandName ()] = display_regex_cmd_sp; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | std::auto_ptr<CommandObjectRegexCommand> |
| 409 | undisplay_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 410 | "_regexp-undisplay", |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 411 | "Remove an expression evaluation stop-hook.", |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 412 | "_regexp-undisplay stop-hook-number", 2)); |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 413 | if (undisplay_regex_cmd_ap.get()) |
| 414 | { |
| 415 | if (undisplay_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "target stop-hook delete %1")) |
| 416 | { |
| 417 | CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_ap.release()); |
| 418 | m_command_dict[undisplay_regex_cmd_sp->GetCommandName ()] = undisplay_regex_cmd_sp; |
| 419 | } |
| 420 | } |
| 421 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | int |
| 425 | CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases, |
| 426 | StringList &matches) |
| 427 | { |
| 428 | CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches); |
| 429 | |
| 430 | if (include_aliases) |
| 431 | { |
| 432 | CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches); |
| 433 | } |
| 434 | |
| 435 | return matches.GetSize(); |
| 436 | } |
| 437 | |
| 438 | CommandObjectSP |
| 439 | CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches) |
| 440 | { |
| 441 | CommandObject::CommandMap::iterator pos; |
| 442 | CommandObjectSP ret_val; |
| 443 | |
| 444 | std::string cmd(cmd_cstr); |
| 445 | |
| 446 | if (HasCommands()) |
| 447 | { |
| 448 | pos = m_command_dict.find(cmd); |
| 449 | if (pos != m_command_dict.end()) |
| 450 | ret_val = pos->second; |
| 451 | } |
| 452 | |
| 453 | if (include_aliases && HasAliases()) |
| 454 | { |
| 455 | pos = m_alias_dict.find(cmd); |
| 456 | if (pos != m_alias_dict.end()) |
| 457 | ret_val = pos->second; |
| 458 | } |
| 459 | |
| 460 | if (HasUserCommands()) |
| 461 | { |
| 462 | pos = m_user_dict.find(cmd); |
| 463 | if (pos != m_user_dict.end()) |
| 464 | ret_val = pos->second; |
| 465 | } |
| 466 | |
| 467 | if (!exact && ret_val == NULL) |
| 468 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 469 | // We will only get into here if we didn't find any exact matches. |
| 470 | |
| 471 | CommandObjectSP user_match_sp, alias_match_sp, real_match_sp; |
| 472 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 473 | StringList local_matches; |
| 474 | if (matches == NULL) |
| 475 | matches = &local_matches; |
| 476 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 477 | unsigned int num_cmd_matches = 0; |
| 478 | unsigned int num_alias_matches = 0; |
| 479 | unsigned int num_user_matches = 0; |
| 480 | |
| 481 | // Look through the command dictionaries one by one, and if we get only one match from any of |
| 482 | // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches. |
| 483 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 484 | if (HasCommands()) |
| 485 | { |
| 486 | num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches); |
| 487 | } |
| 488 | |
| 489 | if (num_cmd_matches == 1) |
| 490 | { |
| 491 | cmd.assign(matches->GetStringAtIndex(0)); |
| 492 | pos = m_command_dict.find(cmd); |
| 493 | if (pos != m_command_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 494 | real_match_sp = pos->second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Jim Ingham | 9a57417 | 2010-06-24 20:28:42 +0000 | [diff] [blame] | 497 | if (include_aliases && HasAliases()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 498 | { |
| 499 | num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches); |
| 500 | |
| 501 | } |
| 502 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 503 | if (num_alias_matches == 1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 504 | { |
| 505 | cmd.assign(matches->GetStringAtIndex (num_cmd_matches)); |
| 506 | pos = m_alias_dict.find(cmd); |
| 507 | if (pos != m_alias_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 508 | alias_match_sp = pos->second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Jim Ingham | 9a57417 | 2010-06-24 20:28:42 +0000 | [diff] [blame] | 511 | if (HasUserCommands()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 512 | { |
| 513 | num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches); |
| 514 | } |
| 515 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 516 | if (num_user_matches == 1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 517 | { |
| 518 | cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches)); |
| 519 | |
| 520 | pos = m_user_dict.find (cmd); |
| 521 | if (pos != m_user_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 522 | user_match_sp = pos->second; |
| 523 | } |
| 524 | |
| 525 | // If we got exactly one match, return that, otherwise return the match list. |
| 526 | |
| 527 | if (num_user_matches + num_cmd_matches + num_alias_matches == 1) |
| 528 | { |
| 529 | if (num_cmd_matches) |
| 530 | return real_match_sp; |
| 531 | else if (num_alias_matches) |
| 532 | return alias_match_sp; |
| 533 | else |
| 534 | return user_match_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 537 | else if (matches && ret_val != NULL) |
| 538 | { |
| 539 | matches->AppendString (cmd_cstr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
| 543 | return ret_val; |
| 544 | } |
| 545 | |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 546 | bool |
| 547 | CommandInterpreter::AddCommand (const char *name, const lldb::CommandObjectSP &cmd_sp, bool can_replace) |
| 548 | { |
| 549 | if (name && name[0]) |
| 550 | { |
| 551 | std::string name_sstr(name); |
| 552 | if (!can_replace) |
| 553 | { |
| 554 | if (m_command_dict.find (name_sstr) != m_command_dict.end()) |
| 555 | return false; |
| 556 | } |
| 557 | m_command_dict[name_sstr] = cmd_sp; |
| 558 | return true; |
| 559 | } |
| 560 | return false; |
| 561 | } |
| 562 | |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 563 | bool |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 564 | CommandInterpreter::AddUserCommand (std::string name, |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 565 | const lldb::CommandObjectSP &cmd_sp, |
| 566 | bool can_replace) |
| 567 | { |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 568 | if (!name.empty()) |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 569 | { |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 570 | |
| 571 | const char* name_cstr = name.c_str(); |
| 572 | |
| 573 | // do not allow replacement of internal commands |
| 574 | if (CommandExists(name_cstr)) |
| 575 | return false; |
| 576 | |
| 577 | if (can_replace == false && UserCommandExists(name_cstr)) |
| 578 | return false; |
| 579 | |
| 580 | m_user_dict[name] = cmd_sp; |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 581 | return true; |
| 582 | } |
| 583 | return false; |
| 584 | } |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 585 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 586 | CommandObjectSP |
| 587 | CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 588 | { |
Caroline Tice | 56d2fc4 | 2010-12-14 18:51:39 +0000 | [diff] [blame] | 589 | Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command. |
| 590 | CommandObjectSP ret_val; // Possibly empty return value. |
| 591 | |
| 592 | if (cmd_cstr == NULL) |
| 593 | return ret_val; |
| 594 | |
| 595 | if (cmd_words.GetArgumentCount() == 1) |
| 596 | return GetCommandSP(cmd_cstr, include_aliases, true, NULL); |
| 597 | else |
| 598 | { |
| 599 | // We have a multi-word command (seemingly), so we need to do more work. |
| 600 | // First, get the cmd_obj_sp for the first word in the command. |
| 601 | CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, NULL); |
| 602 | if (cmd_obj_sp.get() != NULL) |
| 603 | { |
| 604 | // Loop through the rest of the words in the command (everything passed in was supposed to be part of a |
| 605 | // command name), and find the appropriate sub-command SP for each command word.... |
| 606 | size_t end = cmd_words.GetArgumentCount(); |
| 607 | for (size_t j= 1; j < end; ++j) |
| 608 | { |
| 609 | if (cmd_obj_sp->IsMultiwordObject()) |
| 610 | { |
| 611 | cmd_obj_sp = ((CommandObjectMultiword *) cmd_obj_sp.get())->GetSubcommandSP |
| 612 | (cmd_words.GetArgumentAtIndex (j)); |
| 613 | if (cmd_obj_sp.get() == NULL) |
| 614 | // The sub-command name was invalid. Fail and return the empty 'ret_val'. |
| 615 | return ret_val; |
| 616 | } |
| 617 | else |
| 618 | // We have more words in the command name, but we don't have a multiword object. Fail and return |
| 619 | // empty 'ret_val'. |
| 620 | return ret_val; |
| 621 | } |
| 622 | // We successfully looped through all the command words and got valid command objects for them. Assign the |
| 623 | // last object retrieved to 'ret_val'. |
| 624 | ret_val = cmd_obj_sp; |
| 625 | } |
| 626 | } |
| 627 | return ret_val; |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | CommandObject * |
| 631 | CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases) |
| 632 | { |
| 633 | return GetCommandSPExact (cmd_cstr, include_aliases).get(); |
| 634 | } |
| 635 | |
| 636 | CommandObject * |
| 637 | CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches) |
| 638 | { |
| 639 | CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get(); |
| 640 | |
| 641 | // If we didn't find an exact match to the command string in the commands, look in |
| 642 | // the aliases. |
| 643 | |
| 644 | if (command_obj == NULL) |
| 645 | { |
| 646 | command_obj = GetCommandSP (cmd_cstr, true, true, matches).get(); |
| 647 | } |
| 648 | |
| 649 | // Finally, if there wasn't an exact match among the aliases, look for an inexact match |
| 650 | // in both the commands and the aliases. |
| 651 | |
| 652 | if (command_obj == NULL) |
| 653 | command_obj = GetCommandSP(cmd_cstr, true, false, matches).get(); |
| 654 | |
| 655 | return command_obj; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | bool |
| 659 | CommandInterpreter::CommandExists (const char *cmd) |
| 660 | { |
| 661 | return m_command_dict.find(cmd) != m_command_dict.end(); |
| 662 | } |
| 663 | |
| 664 | bool |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 665 | CommandInterpreter::ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp, |
| 666 | const char *options_args, |
| 667 | OptionArgVectorSP &option_arg_vector_sp) |
| 668 | { |
| 669 | bool success = true; |
| 670 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 671 | |
| 672 | if (!options_args || (strlen (options_args) < 1)) |
| 673 | return true; |
| 674 | |
| 675 | std::string options_string (options_args); |
| 676 | Args args (options_args); |
| 677 | CommandReturnObject result; |
| 678 | // Check to see if the command being aliased can take any command options. |
| 679 | Options *options = cmd_obj_sp->GetOptions (); |
| 680 | if (options) |
| 681 | { |
| 682 | // See if any options were specified as part of the alias; if so, handle them appropriately. |
| 683 | options->NotifyOptionParsingStarting (); |
| 684 | args.Unshift ("dummy_arg"); |
| 685 | args.ParseAliasOptions (*options, result, option_arg_vector, options_string); |
| 686 | args.Shift (); |
| 687 | if (result.Succeeded()) |
| 688 | options->VerifyPartialOptions (result); |
| 689 | if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted) |
| 690 | { |
| 691 | result.AppendError ("Unable to create requested alias.\n"); |
| 692 | return false; |
| 693 | } |
| 694 | } |
| 695 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 696 | if (!options_string.empty()) |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 697 | { |
| 698 | if (cmd_obj_sp->WantsRawCommandString ()) |
| 699 | option_arg_vector->push_back (OptionArgPair ("<argument>", |
| 700 | OptionArgValue (-1, |
| 701 | options_string))); |
| 702 | else |
| 703 | { |
| 704 | int argc = args.GetArgumentCount(); |
| 705 | for (size_t i = 0; i < argc; ++i) |
| 706 | if (strcmp (args.GetArgumentAtIndex (i), "") != 0) |
| 707 | option_arg_vector->push_back |
| 708 | (OptionArgPair ("<argument>", |
| 709 | OptionArgValue (-1, |
| 710 | std::string (args.GetArgumentAtIndex (i))))); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | return success; |
| 715 | } |
| 716 | |
| 717 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 718 | CommandInterpreter::AliasExists (const char *cmd) |
| 719 | { |
| 720 | return m_alias_dict.find(cmd) != m_alias_dict.end(); |
| 721 | } |
| 722 | |
| 723 | bool |
| 724 | CommandInterpreter::UserCommandExists (const char *cmd) |
| 725 | { |
| 726 | return m_user_dict.find(cmd) != m_user_dict.end(); |
| 727 | } |
| 728 | |
| 729 | void |
| 730 | CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp) |
| 731 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 732 | command_obj_sp->SetIsAlias (true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 733 | m_alias_dict[alias_name] = command_obj_sp; |
| 734 | } |
| 735 | |
| 736 | bool |
| 737 | CommandInterpreter::RemoveAlias (const char *alias_name) |
| 738 | { |
| 739 | CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name); |
| 740 | if (pos != m_alias_dict.end()) |
| 741 | { |
| 742 | m_alias_dict.erase(pos); |
| 743 | return true; |
| 744 | } |
| 745 | return false; |
| 746 | } |
| 747 | bool |
| 748 | CommandInterpreter::RemoveUser (const char *alias_name) |
| 749 | { |
| 750 | CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name); |
| 751 | if (pos != m_user_dict.end()) |
| 752 | { |
| 753 | m_user_dict.erase(pos); |
| 754 | return true; |
| 755 | } |
| 756 | return false; |
| 757 | } |
| 758 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 759 | void |
| 760 | CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string) |
| 761 | { |
| 762 | help_string.Printf ("'%s", command_name); |
| 763 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
| 764 | |
| 765 | if (option_arg_vector_sp != NULL) |
| 766 | { |
| 767 | OptionArgVector *options = option_arg_vector_sp.get(); |
| 768 | for (int i = 0; i < options->size(); ++i) |
| 769 | { |
| 770 | OptionArgPair cur_option = (*options)[i]; |
| 771 | std::string opt = cur_option.first; |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 772 | OptionArgValue value_pair = cur_option.second; |
| 773 | std::string value = value_pair.second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 774 | if (opt.compare("<argument>") == 0) |
| 775 | { |
| 776 | help_string.Printf (" %s", value.c_str()); |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | help_string.Printf (" %s", opt.c_str()); |
| 781 | if ((value.compare ("<no-argument>") != 0) |
| 782 | && (value.compare ("<need-argument") != 0)) |
| 783 | { |
| 784 | help_string.Printf (" %s", value.c_str()); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | help_string.Printf ("'"); |
| 791 | } |
| 792 | |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 793 | size_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 794 | CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict) |
| 795 | { |
| 796 | CommandObject::CommandMap::const_iterator pos; |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 797 | CommandObject::CommandMap::const_iterator end = dict.end(); |
| 798 | size_t max_len = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 799 | |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 800 | for (pos = dict.begin(); pos != end; ++pos) |
| 801 | { |
| 802 | size_t len = pos->first.size(); |
| 803 | if (max_len < len) |
| 804 | max_len = len; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 805 | } |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 806 | return max_len; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | void |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 810 | CommandInterpreter::GetHelp (CommandReturnObject &result, |
Enrico Granata | 1ac6d1f | 2011-09-09 17:49:36 +0000 | [diff] [blame] | 811 | uint32_t cmd_types) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 812 | { |
| 813 | CommandObject::CommandMap::const_iterator pos; |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 814 | uint32_t max_len = FindLongestCommandWord (m_command_dict); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 815 | |
| 816 | if ( (cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 817 | { |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 818 | |
| 819 | result.AppendMessage("The following is a list of built-in, permanent debugger commands:"); |
| 820 | result.AppendMessage(""); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 821 | |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 822 | for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) |
| 823 | { |
| 824 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(), |
| 825 | max_len); |
| 826 | } |
| 827 | result.AppendMessage(""); |
| 828 | |
| 829 | } |
| 830 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 831 | if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases )) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 832 | { |
Jim Ingham | e3663e8 | 2010-10-22 18:47:16 +0000 | [diff] [blame] | 833 | result.AppendMessage("The following is a list of your current command abbreviations " |
Johnny Chen | 9e4c3d7 | 2011-04-21 00:39:18 +0000 | [diff] [blame] | 834 | "(see 'help command alias' for more info):"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 835 | result.AppendMessage(""); |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 836 | max_len = FindLongestCommandWord (m_alias_dict); |
| 837 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 838 | for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos) |
| 839 | { |
| 840 | StreamString sstr; |
| 841 | StreamString translation_and_help; |
| 842 | std::string entry_name = pos->first; |
| 843 | std::string second_entry = pos->second.get()->GetCommandName(); |
| 844 | GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr); |
| 845 | |
| 846 | translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp()); |
| 847 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", |
| 848 | translation_and_help.GetData(), max_len); |
| 849 | } |
| 850 | result.AppendMessage(""); |
| 851 | } |
| 852 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 853 | if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef )) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 854 | { |
| 855 | result.AppendMessage ("The following is a list of your current user-defined commands:"); |
| 856 | result.AppendMessage(""); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 857 | max_len = FindLongestCommandWord (m_user_dict); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 858 | for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos) |
| 859 | { |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 860 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(), |
| 861 | max_len); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 862 | } |
| 863 | result.AppendMessage(""); |
| 864 | } |
| 865 | |
| 866 | result.AppendMessage("For more information on any particular command, try 'help <command-name>'."); |
| 867 | } |
| 868 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 869 | CommandObject * |
| 870 | CommandInterpreter::GetCommandObjectForCommand (std::string &command_string) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 871 | { |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 872 | // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will |
| 873 | // eventually be invoked by the given command line. |
| 874 | |
| 875 | CommandObject *cmd_obj = NULL; |
| 876 | std::string white_space (" \t\v"); |
| 877 | size_t start = command_string.find_first_not_of (white_space); |
| 878 | size_t end = 0; |
| 879 | bool done = false; |
| 880 | while (!done) |
| 881 | { |
| 882 | if (start != std::string::npos) |
| 883 | { |
| 884 | // Get the next word from command_string. |
| 885 | end = command_string.find_first_of (white_space, start); |
| 886 | if (end == std::string::npos) |
| 887 | end = command_string.size(); |
| 888 | std::string cmd_word = command_string.substr (start, end - start); |
| 889 | |
| 890 | if (cmd_obj == NULL) |
| 891 | // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid |
| 892 | // command or alias. |
| 893 | cmd_obj = GetCommandObject (cmd_word.c_str()); |
| 894 | else if (cmd_obj->IsMultiwordObject ()) |
| 895 | { |
| 896 | // Our current object is a multi-word object; see if the cmd_word is a valid sub-command for our object. |
| 897 | CommandObject *sub_cmd_obj = |
| 898 | ((CommandObjectMultiword *) cmd_obj)->GetSubcommandObject (cmd_word.c_str()); |
| 899 | if (sub_cmd_obj) |
| 900 | cmd_obj = sub_cmd_obj; |
| 901 | else // cmd_word was not a valid sub-command word, so we are donee |
| 902 | done = true; |
| 903 | } |
| 904 | else |
| 905 | // We have a cmd_obj and it is not a multi-word object, so we are done. |
| 906 | done = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 907 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 908 | // If we didn't find a valid command object, or our command object is not a multi-word object, or |
| 909 | // we are at the end of the command_string, then we are done. Otherwise, find the start of the |
| 910 | // next word. |
| 911 | |
| 912 | if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size()) |
| 913 | done = true; |
| 914 | else |
| 915 | start = command_string.find_first_not_of (white_space, end); |
| 916 | } |
| 917 | else |
| 918 | // Unable to find any more words. |
| 919 | done = true; |
| 920 | } |
| 921 | |
| 922 | if (end == command_string.size()) |
| 923 | command_string.clear(); |
| 924 | else |
| 925 | command_string = command_string.substr(end); |
| 926 | |
| 927 | return cmd_obj; |
| 928 | } |
| 929 | |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 930 | static const char *k_white_space = " \t\v"; |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 931 | static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 932 | static void |
| 933 | StripLeadingSpaces (std::string &s) |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 934 | { |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 935 | if (!s.empty()) |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 936 | { |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 937 | size_t pos = s.find_first_not_of (k_white_space); |
| 938 | if (pos == std::string::npos) |
| 939 | s.clear(); |
| 940 | else if (pos == 0) |
| 941 | return; |
| 942 | s.erase (0, pos); |
| 943 | } |
| 944 | } |
| 945 | |
Greg Clayton | 3840cd7 | 2011-11-09 23:25:03 +0000 | [diff] [blame] | 946 | static size_t |
| 947 | FindArgumentTerminator (const std::string &s) |
| 948 | { |
Greg Clayton | 3840cd7 | 2011-11-09 23:25:03 +0000 | [diff] [blame] | 949 | const size_t s_len = s.size(); |
| 950 | size_t offset = 0; |
| 951 | while (offset < s_len) |
| 952 | { |
| 953 | size_t pos = s.find ("--", offset); |
| 954 | if (pos == std::string::npos) |
| 955 | break; |
| 956 | if (pos > 0) |
| 957 | { |
| 958 | if (isspace(s[pos-1])) |
| 959 | { |
| 960 | // Check if the string ends "\s--" (where \s is a space character) |
| 961 | // or if we have "\s--\s". |
| 962 | if ((pos + 2 >= s_len) || isspace(s[pos+2])) |
| 963 | { |
Greg Clayton | 3840cd7 | 2011-11-09 23:25:03 +0000 | [diff] [blame] | 964 | return pos; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | offset = pos + 2; |
| 969 | } |
Greg Clayton | 3840cd7 | 2011-11-09 23:25:03 +0000 | [diff] [blame] | 970 | return std::string::npos; |
| 971 | } |
| 972 | |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 973 | static bool |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 974 | ExtractCommand (std::string &command_string, std::string &command, std::string &suffix, char "e_char) |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 975 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 976 | command.clear(); |
| 977 | suffix.clear(); |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 978 | StripLeadingSpaces (command_string); |
| 979 | |
| 980 | bool result = false; |
| 981 | quote_char = '\0'; |
| 982 | |
| 983 | if (!command_string.empty()) |
| 984 | { |
| 985 | const char first_char = command_string[0]; |
| 986 | if (first_char == '\'' || first_char == '"') |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 987 | { |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 988 | quote_char = first_char; |
| 989 | const size_t end_quote_pos = command_string.find (quote_char, 1); |
| 990 | if (end_quote_pos == std::string::npos) |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 991 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 992 | command.swap (command_string); |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 993 | command_string.erase (); |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 994 | } |
| 995 | else |
| 996 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 997 | command.assign (command_string, 1, end_quote_pos - 1); |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 998 | if (end_quote_pos + 1 < command_string.size()) |
| 999 | command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1)); |
| 1000 | else |
| 1001 | command_string.erase (); |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1002 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1003 | } |
| 1004 | else |
| 1005 | { |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1006 | const size_t first_space_pos = command_string.find_first_of (k_white_space); |
| 1007 | if (first_space_pos == std::string::npos) |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1008 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1009 | command.swap (command_string); |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1010 | command_string.erase(); |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1011 | } |
| 1012 | else |
| 1013 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1014 | command.assign (command_string, 0, first_space_pos); |
| 1015 | command_string.erase(0, command_string.find_first_not_of (k_white_space, first_space_pos)); |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1016 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1017 | } |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1018 | result = true; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1019 | } |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1020 | |
| 1021 | |
| 1022 | if (!command.empty()) |
| 1023 | { |
| 1024 | // actual commands can't start with '-' or '_' |
| 1025 | if (command[0] != '-' && command[0] != '_') |
| 1026 | { |
| 1027 | size_t pos = command.find_first_not_of(k_valid_command_chars); |
| 1028 | if (pos > 0 && pos != std::string::npos) |
| 1029 | { |
| 1030 | suffix.assign (command.begin() + pos, command.end()); |
| 1031 | command.erase (pos); |
| 1032 | } |
| 1033 | } |
| 1034 | } |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1035 | |
| 1036 | return result; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1039 | CommandObject * |
| 1040 | CommandInterpreter::BuildAliasResult (const char *alias_name, |
| 1041 | std::string &raw_input_string, |
| 1042 | std::string &alias_result, |
| 1043 | CommandReturnObject &result) |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1044 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1045 | CommandObject *alias_cmd_obj = NULL; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1046 | Args cmd_args (raw_input_string.c_str()); |
| 1047 | alias_cmd_obj = GetCommandObject (alias_name); |
| 1048 | StreamString result_str; |
| 1049 | |
| 1050 | if (alias_cmd_obj) |
| 1051 | { |
| 1052 | std::string alias_name_str = alias_name; |
| 1053 | if ((cmd_args.GetArgumentCount() == 0) |
| 1054 | || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)) |
| 1055 | cmd_args.Unshift (alias_name); |
| 1056 | |
| 1057 | result_str.Printf ("%s", alias_cmd_obj->GetCommandName ()); |
| 1058 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
| 1059 | |
| 1060 | if (option_arg_vector_sp.get()) |
| 1061 | { |
| 1062 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 1063 | |
| 1064 | for (int i = 0; i < option_arg_vector->size(); ++i) |
| 1065 | { |
| 1066 | OptionArgPair option_pair = (*option_arg_vector)[i]; |
| 1067 | OptionArgValue value_pair = option_pair.second; |
| 1068 | int value_type = value_pair.first; |
| 1069 | std::string option = option_pair.first; |
| 1070 | std::string value = value_pair.second; |
| 1071 | if (option.compare ("<argument>") == 0) |
| 1072 | result_str.Printf (" %s", value.c_str()); |
| 1073 | else |
| 1074 | { |
| 1075 | result_str.Printf (" %s", option.c_str()); |
| 1076 | if (value_type != optional_argument) |
| 1077 | result_str.Printf (" "); |
| 1078 | if (value.compare ("<no_argument>") != 0) |
| 1079 | { |
| 1080 | int index = GetOptionArgumentPosition (value.c_str()); |
| 1081 | if (index == 0) |
| 1082 | result_str.Printf ("%s", value.c_str()); |
| 1083 | else if (index >= cmd_args.GetArgumentCount()) |
| 1084 | { |
| 1085 | |
| 1086 | result.AppendErrorWithFormat |
| 1087 | ("Not enough arguments provided; you need at least %d arguments to use this alias.\n", |
| 1088 | index); |
| 1089 | result.SetStatus (eReturnStatusFailed); |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1090 | return alias_cmd_obj; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1091 | } |
| 1092 | else |
| 1093 | { |
| 1094 | size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index)); |
| 1095 | if (strpos != std::string::npos) |
| 1096 | raw_input_string = raw_input_string.erase (strpos, |
| 1097 | strlen (cmd_args.GetArgumentAtIndex (index))); |
| 1098 | result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index)); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | alias_result = result_str.GetData(); |
| 1106 | } |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1107 | return alias_cmd_obj; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Greg Clayton | f5c0c72 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1110 | Error |
| 1111 | CommandInterpreter::PreprocessCommand (std::string &command) |
| 1112 | { |
| 1113 | // The command preprocessor needs to do things to the command |
| 1114 | // line before any parsing of arguments or anything else is done. |
| 1115 | // The only current stuff that gets proprocessed is anyting enclosed |
| 1116 | // in backtick ('`') characters is evaluated as an expression and |
| 1117 | // the result of the expression must be a scalar that can be substituted |
| 1118 | // into the command. An example would be: |
| 1119 | // (lldb) memory read `$rsp + 20` |
| 1120 | Error error; // Error for any expressions that might not evaluate |
| 1121 | size_t start_backtick; |
| 1122 | size_t pos = 0; |
| 1123 | while ((start_backtick = command.find ('`', pos)) != std::string::npos) |
| 1124 | { |
| 1125 | if (start_backtick > 0 && command[start_backtick-1] == '\\') |
| 1126 | { |
| 1127 | // The backtick was preceeded by a '\' character, remove the slash |
| 1128 | // and don't treat the backtick as the start of an expression |
| 1129 | command.erase(start_backtick-1, 1); |
| 1130 | // No need to add one to start_backtick since we just deleted a char |
| 1131 | pos = start_backtick; |
| 1132 | } |
| 1133 | else |
| 1134 | { |
| 1135 | const size_t expr_content_start = start_backtick + 1; |
| 1136 | const size_t end_backtick = command.find ('`', expr_content_start); |
| 1137 | if (end_backtick == std::string::npos) |
| 1138 | return error; |
| 1139 | else if (end_backtick == expr_content_start) |
| 1140 | { |
| 1141 | // Empty expression (two backticks in a row) |
| 1142 | command.erase (start_backtick, 2); |
| 1143 | } |
| 1144 | else |
| 1145 | { |
| 1146 | std::string expr_str (command, expr_content_start, end_backtick - expr_content_start); |
| 1147 | |
Greg Clayton | bcaf99a | 2012-07-12 20:32:19 +0000 | [diff] [blame] | 1148 | ExecutionContext exe_ctx(GetExecutionContext()); |
| 1149 | Target *target = exe_ctx.GetTargetPtr(); |
Johnny Chen | b09f847 | 2011-10-29 00:21:50 +0000 | [diff] [blame] | 1150 | // Get a dummy target to allow for calculator mode while processing backticks. |
| 1151 | // This also helps break the infinite loop caused when target is null. |
| 1152 | if (!target) |
| 1153 | target = Host::GetDummyTarget(GetDebugger()).get(); |
Greg Clayton | f5c0c72 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1154 | if (target) |
| 1155 | { |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 1156 | const bool coerce_to_id = false; |
Greg Clayton | f5c0c72 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1157 | const bool unwind_on_error = true; |
| 1158 | const bool keep_in_memory = false; |
| 1159 | ValueObjectSP expr_result_valobj_sp; |
| 1160 | ExecutionResults expr_result = target->EvaluateExpression (expr_str.c_str(), |
Greg Clayton | bcaf99a | 2012-07-12 20:32:19 +0000 | [diff] [blame] | 1161 | exe_ctx.GetFramePtr(), |
Greg Clayton | f5c0c72 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1162 | eExecutionPolicyOnlyWhenNeeded, |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 1163 | coerce_to_id, |
| 1164 | unwind_on_error, |
| 1165 | keep_in_memory, |
Greg Clayton | f5c0c72 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1166 | eNoDynamicValues, |
Enrico Granata | 6cca969 | 2012-07-16 23:10:35 +0000 | [diff] [blame] | 1167 | expr_result_valobj_sp, |
| 1168 | 0 /* no timeout */); |
Greg Clayton | f5c0c72 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1169 | if (expr_result == eExecutionCompleted) |
| 1170 | { |
| 1171 | Scalar scalar; |
| 1172 | if (expr_result_valobj_sp->ResolveValue (scalar)) |
| 1173 | { |
| 1174 | command.erase (start_backtick, end_backtick - start_backtick + 1); |
| 1175 | StreamString value_strm; |
| 1176 | const bool show_type = false; |
| 1177 | scalar.GetValue (&value_strm, show_type); |
| 1178 | size_t value_string_size = value_strm.GetSize(); |
| 1179 | if (value_string_size) |
| 1180 | { |
| 1181 | command.insert (start_backtick, value_strm.GetData(), value_string_size); |
| 1182 | pos = start_backtick + value_string_size; |
| 1183 | continue; |
| 1184 | } |
| 1185 | else |
| 1186 | { |
| 1187 | error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str()); |
| 1188 | } |
| 1189 | } |
| 1190 | else |
| 1191 | { |
| 1192 | error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str()); |
| 1193 | } |
| 1194 | } |
| 1195 | else |
| 1196 | { |
| 1197 | if (expr_result_valobj_sp) |
| 1198 | error = expr_result_valobj_sp->GetError(); |
| 1199 | if (error.Success()) |
| 1200 | { |
| 1201 | |
| 1202 | switch (expr_result) |
| 1203 | { |
| 1204 | case eExecutionSetupError: |
| 1205 | error.SetErrorStringWithFormat("expression setup error for the expression '%s'", expr_str.c_str()); |
| 1206 | break; |
| 1207 | case eExecutionCompleted: |
| 1208 | break; |
| 1209 | case eExecutionDiscarded: |
| 1210 | error.SetErrorStringWithFormat("expression discarded for the expression '%s'", expr_str.c_str()); |
| 1211 | break; |
| 1212 | case eExecutionInterrupted: |
| 1213 | error.SetErrorStringWithFormat("expression interrupted for the expression '%s'", expr_str.c_str()); |
| 1214 | break; |
| 1215 | case eExecutionTimedOut: |
| 1216 | error.SetErrorStringWithFormat("expression timed out for the expression '%s'", expr_str.c_str()); |
| 1217 | break; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | if (error.Fail()) |
| 1224 | break; |
| 1225 | } |
| 1226 | } |
| 1227 | return error; |
| 1228 | } |
| 1229 | |
| 1230 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1231 | bool |
| 1232 | CommandInterpreter::HandleCommand (const char *command_line, |
Enrico Granata | 01bc2d4 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 1233 | LazyBool lazy_add_to_history, |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1234 | CommandReturnObject &result, |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1235 | ExecutionContext *override_context, |
Johnny Chen | 8bdf57c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 1236 | bool repeat_on_empty_command, |
| 1237 | bool no_context_switching) |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1238 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1239 | { |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1240 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1241 | bool done = false; |
| 1242 | CommandObject *cmd_obj = NULL; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1243 | bool wants_raw_input = false; |
| 1244 | std::string command_string (command_line); |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1245 | std::string original_command_string (command_line); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1246 | |
| 1247 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS)); |
Greg Clayton | e98ac25 | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 1248 | Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line); |
| 1249 | |
| 1250 | // Make a scoped cleanup object that will clear the crash description string |
| 1251 | // on exit of this function. |
Enrico Granata | 1a10208 | 2011-07-12 00:18:11 +0000 | [diff] [blame] | 1252 | lldb_utility::CleanUp <const char *> crash_description_cleanup(NULL, Host::SetCrashDescription); |
Greg Clayton | e98ac25 | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 1253 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1254 | if (log) |
| 1255 | log->Printf ("Processing command: %s", command_line); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1256 | |
Jim Ingham | abab14b | 2010-11-04 23:08:45 +0000 | [diff] [blame] | 1257 | Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line); |
| 1258 | |
Johnny Chen | 8bdf57c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 1259 | if (!no_context_switching) |
| 1260 | UpdateExecutionContext (override_context); |
Enrico Granata | 01bc2d4 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 1261 | |
| 1262 | // <rdar://problem/11328896> |
| 1263 | bool add_to_history; |
| 1264 | if (lazy_add_to_history == eLazyBoolCalculate) |
| 1265 | add_to_history = (m_command_source_depth == 0); |
| 1266 | else |
| 1267 | add_to_history = (lazy_add_to_history == eLazyBoolYes); |
| 1268 | |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1269 | bool empty_command = false; |
| 1270 | bool comment_command = false; |
| 1271 | if (command_string.empty()) |
| 1272 | empty_command = true; |
| 1273 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1274 | { |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1275 | const char *k_space_characters = "\t\n\v\f\r "; |
| 1276 | |
| 1277 | size_t non_space = command_string.find_first_not_of (k_space_characters); |
| 1278 | // Check for empty line or comment line (lines whose first |
| 1279 | // non-space character is the comment character for this interpreter) |
| 1280 | if (non_space == std::string::npos) |
| 1281 | empty_command = true; |
| 1282 | else if (command_string[non_space] == m_comment_char) |
| 1283 | comment_command = true; |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1284 | else if (command_string[non_space] == m_repeat_char) |
| 1285 | { |
| 1286 | const char *history_string = FindHistoryString (command_string.c_str() + non_space); |
| 1287 | if (history_string == NULL) |
| 1288 | { |
| 1289 | result.AppendErrorWithFormat ("Could not find entry: %s in history", command_string.c_str()); |
| 1290 | result.SetStatus(eReturnStatusFailed); |
| 1291 | return false; |
| 1292 | } |
| 1293 | add_to_history = false; |
| 1294 | command_string = history_string; |
| 1295 | original_command_string = history_string; |
| 1296 | } |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | if (empty_command) |
| 1300 | { |
| 1301 | if (repeat_on_empty_command) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1302 | { |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1303 | if (m_command_history.empty()) |
| 1304 | { |
| 1305 | result.AppendError ("empty command"); |
| 1306 | result.SetStatus(eReturnStatusFailed); |
| 1307 | return false; |
| 1308 | } |
| 1309 | else |
| 1310 | { |
| 1311 | command_line = m_repeat_command.c_str(); |
| 1312 | command_string = command_line; |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1313 | original_command_string = command_line; |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1314 | if (m_repeat_command.empty()) |
| 1315 | { |
| 1316 | result.AppendErrorWithFormat("No auto repeat.\n"); |
| 1317 | result.SetStatus (eReturnStatusFailed); |
| 1318 | return false; |
| 1319 | } |
| 1320 | } |
| 1321 | add_to_history = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1322 | } |
| 1323 | else |
| 1324 | { |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1325 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1326 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1327 | } |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1328 | } |
| 1329 | else if (comment_command) |
| 1330 | { |
| 1331 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1332 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1333 | } |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1334 | |
Greg Clayton | f5c0c72 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1335 | |
| 1336 | Error error (PreprocessCommand (command_string)); |
| 1337 | |
| 1338 | if (error.Fail()) |
| 1339 | { |
| 1340 | result.AppendError (error.AsCString()); |
| 1341 | result.SetStatus(eReturnStatusFailed); |
| 1342 | return false; |
| 1343 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1344 | // Phase 1. |
| 1345 | |
| 1346 | // Before we do ANY kind of argument processing, etc. we need to figure out what the real/final command object |
| 1347 | // is for the specified command, and whether or not it wants raw input. This gets complicated by the fact that |
| 1348 | // the user could have specified an alias, and in translating the alias there may also be command options and/or |
| 1349 | // even data (including raw text strings) that need to be found and inserted into the command line as part of |
| 1350 | // the translation. So this first step is plain look-up & replacement, resulting in three things: 1). the command |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 1351 | // object whose Execute method will actually be called; 2). a revised command string, with all substitutions & |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1352 | // replacements taken care of; 3). whether or not the Execute function wants raw input or not. |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1353 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1354 | StreamString revised_command_line; |
Caroline Tice | ea6e3df | 2010-12-11 08:16:56 +0000 | [diff] [blame] | 1355 | size_t actual_cmd_name_len = 0; |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1356 | std::string next_word; |
Filipe Cabecinhas | 4bc8d16 | 2012-05-16 23:25:54 +0000 | [diff] [blame] | 1357 | StringList matches; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1358 | while (!done) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1359 | { |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1360 | char quote_char = '\0'; |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1361 | std::string suffix; |
| 1362 | ExtractCommand (command_string, next_word, suffix, quote_char); |
| 1363 | if (cmd_obj == NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1364 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1365 | if (AliasExists (next_word.c_str())) |
Caroline Tice | 56d2fc4 | 2010-12-14 18:51:39 +0000 | [diff] [blame] | 1366 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1367 | std::string alias_result; |
| 1368 | cmd_obj = BuildAliasResult (next_word.c_str(), command_string, alias_result, result); |
| 1369 | revised_command_line.Printf ("%s", alias_result.c_str()); |
| 1370 | if (cmd_obj) |
| 1371 | { |
| 1372 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
| 1373 | actual_cmd_name_len = strlen (cmd_obj->GetCommandName()); |
| 1374 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1375 | } |
| 1376 | else |
| 1377 | { |
Filipe Cabecinhas | 4bc8d16 | 2012-05-16 23:25:54 +0000 | [diff] [blame] | 1378 | cmd_obj = GetCommandObject (next_word.c_str(), &matches); |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1379 | if (cmd_obj) |
| 1380 | { |
| 1381 | actual_cmd_name_len += next_word.length(); |
| 1382 | revised_command_line.Printf ("%s", next_word.c_str()); |
| 1383 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
| 1384 | } |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1385 | else |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1386 | { |
| 1387 | revised_command_line.Printf ("%s", next_word.c_str()); |
| 1388 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | else |
| 1392 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1393 | if (cmd_obj->IsMultiwordObject ()) |
| 1394 | { |
| 1395 | CommandObject *sub_cmd_obj = ((CommandObjectMultiword *) cmd_obj)->GetSubcommandObject (next_word.c_str()); |
| 1396 | if (sub_cmd_obj) |
| 1397 | { |
| 1398 | actual_cmd_name_len += next_word.length() + 1; |
| 1399 | revised_command_line.Printf (" %s", next_word.c_str()); |
| 1400 | cmd_obj = sub_cmd_obj; |
| 1401 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
| 1402 | } |
| 1403 | else |
| 1404 | { |
| 1405 | if (quote_char) |
| 1406 | revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char); |
| 1407 | else |
| 1408 | revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str()); |
| 1409 | done = true; |
| 1410 | } |
| 1411 | } |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1412 | else |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1413 | { |
| 1414 | if (quote_char) |
| 1415 | revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char); |
| 1416 | else |
| 1417 | revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str()); |
| 1418 | done = true; |
| 1419 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | if (cmd_obj == NULL) |
| 1423 | { |
Filipe Cabecinhas | 4bc8d16 | 2012-05-16 23:25:54 +0000 | [diff] [blame] | 1424 | uint32_t num_matches = matches.GetSize(); |
| 1425 | if (matches.GetSize() > 1) { |
| 1426 | std::string error_msg; |
| 1427 | error_msg.assign ("Ambiguous command '"); |
| 1428 | error_msg.append(next_word.c_str()); |
| 1429 | error_msg.append ("'."); |
| 1430 | |
| 1431 | error_msg.append (" Possible matches:"); |
| 1432 | |
| 1433 | for (uint32_t i = 0; i < num_matches; ++i) { |
| 1434 | error_msg.append ("\n\t"); |
| 1435 | error_msg.append (matches.GetStringAtIndex(i)); |
| 1436 | } |
| 1437 | error_msg.append ("\n"); |
| 1438 | result.AppendRawError (error_msg.c_str(), error_msg.size()); |
| 1439 | } else { |
| 1440 | // We didn't have only one match, otherwise we wouldn't get here. |
| 1441 | assert(num_matches == 0); |
| 1442 | result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str()); |
| 1443 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1444 | result.SetStatus (eReturnStatusFailed); |
| 1445 | return false; |
| 1446 | } |
| 1447 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1448 | if (cmd_obj->IsMultiwordObject ()) |
| 1449 | { |
| 1450 | if (!suffix.empty()) |
| 1451 | { |
| 1452 | |
| 1453 | result.AppendErrorWithFormat ("multi-word commands ('%s') can't have shorthand suffixes: '%s'\n", |
| 1454 | next_word.c_str(), |
| 1455 | suffix.c_str()); |
| 1456 | result.SetStatus (eReturnStatusFailed); |
| 1457 | return false; |
| 1458 | } |
| 1459 | } |
| 1460 | else |
| 1461 | { |
| 1462 | // If we found a normal command, we are done |
| 1463 | done = true; |
| 1464 | if (!suffix.empty()) |
| 1465 | { |
| 1466 | switch (suffix[0]) |
| 1467 | { |
| 1468 | case '/': |
| 1469 | // GDB format suffixes |
Greg Clayton | d8a218d | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 1470 | { |
| 1471 | Options *command_options = cmd_obj->GetOptions(); |
| 1472 | if (command_options && command_options->SupportsLongOption("gdb-format")) |
| 1473 | { |
Greg Clayton | 3840cd7 | 2011-11-09 23:25:03 +0000 | [diff] [blame] | 1474 | std::string gdb_format_option ("--gdb-format="); |
| 1475 | gdb_format_option += (suffix.c_str() + 1); |
| 1476 | |
| 1477 | bool inserted = false; |
| 1478 | std::string &cmd = revised_command_line.GetString(); |
| 1479 | size_t arg_terminator_idx = FindArgumentTerminator (cmd); |
| 1480 | if (arg_terminator_idx != std::string::npos) |
| 1481 | { |
| 1482 | // Insert the gdb format option before the "--" that terminates options |
| 1483 | gdb_format_option.append(1,' '); |
| 1484 | cmd.insert(arg_terminator_idx, gdb_format_option); |
| 1485 | inserted = true; |
| 1486 | } |
| 1487 | |
| 1488 | if (!inserted) |
| 1489 | revised_command_line.Printf (" %s", gdb_format_option.c_str()); |
| 1490 | |
| 1491 | if (wants_raw_input && FindArgumentTerminator(cmd) == std::string::npos) |
| 1492 | revised_command_line.PutCString (" --"); |
Greg Clayton | d8a218d | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 1493 | } |
| 1494 | else |
| 1495 | { |
| 1496 | result.AppendErrorWithFormat ("the '%s' command doesn't support the --gdb-format option\n", |
| 1497 | cmd_obj->GetCommandName()); |
| 1498 | result.SetStatus (eReturnStatusFailed); |
| 1499 | return false; |
| 1500 | } |
| 1501 | } |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1502 | break; |
Johnny Chen | 8ca450b | 2011-10-31 22:22:06 +0000 | [diff] [blame] | 1503 | |
| 1504 | default: |
| 1505 | result.AppendErrorWithFormat ("unknown command shorthand suffix: '%s'\n", |
| 1506 | suffix.c_str()); |
| 1507 | result.SetStatus (eReturnStatusFailed); |
| 1508 | return false; |
| 1509 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1513 | if (command_string.length() == 0) |
| 1514 | done = true; |
| 1515 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1516 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1517 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1518 | if (!command_string.empty()) |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1519 | revised_command_line.Printf (" %s", command_string.c_str()); |
| 1520 | |
| 1521 | // End of Phase 1. |
| 1522 | // At this point cmd_obj should contain the CommandObject whose Execute method will be called, if the command |
| 1523 | // specified was valid; revised_command_line contains the complete command line (including command name(s)), |
| 1524 | // fully translated with all substitutions & translations taken care of (still in raw text format); and |
| 1525 | // wants_raw_input specifies whether the Execute method expects raw input or not. |
| 1526 | |
| 1527 | |
| 1528 | if (log) |
| 1529 | { |
| 1530 | log->Printf ("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>"); |
| 1531 | log->Printf ("HandleCommand, revised_command_line: '%s'", revised_command_line.GetData()); |
| 1532 | log->Printf ("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False"); |
| 1533 | } |
| 1534 | |
| 1535 | // Phase 2. |
| 1536 | // Take care of things like setting up the history command & calling the appropriate Execute method on the |
| 1537 | // CommandObject, with the appropriate arguments. |
| 1538 | |
| 1539 | if (cmd_obj != NULL) |
| 1540 | { |
| 1541 | if (add_to_history) |
| 1542 | { |
| 1543 | Args command_args (revised_command_line.GetData()); |
| 1544 | const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0); |
| 1545 | if (repeat_command != NULL) |
| 1546 | m_repeat_command.assign(repeat_command); |
| 1547 | else |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1548 | m_repeat_command.assign(original_command_string.c_str()); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1549 | |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1550 | // Don't keep pushing the same command onto the history... |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1551 | if (m_command_history.empty() || m_command_history.back() != original_command_string) |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1552 | m_command_history.push_back (original_command_string); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | command_string = revised_command_line.GetData(); |
| 1556 | std::string command_name (cmd_obj->GetCommandName()); |
Caroline Tice | ea6e3df | 2010-12-11 08:16:56 +0000 | [diff] [blame] | 1557 | std::string remainder; |
| 1558 | if (actual_cmd_name_len < command_string.length()) |
| 1559 | remainder = command_string.substr (actual_cmd_name_len); // Note: 'actual_cmd_name_len' may be considerably shorter |
| 1560 | // than cmd_obj->GetCommandName(), because name completion |
| 1561 | // allows users to enter short versions of the names, |
| 1562 | // e.g. 'br s' for 'breakpoint set'. |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1563 | |
| 1564 | // Remove any initial spaces |
| 1565 | std::string white_space (" \t\v"); |
| 1566 | size_t pos = remainder.find_first_not_of (white_space); |
| 1567 | if (pos != 0 && pos != std::string::npos) |
Greg Clayton | 91c9dcf | 2011-04-22 20:58:45 +0000 | [diff] [blame] | 1568 | remainder.erase(0, pos); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1569 | |
| 1570 | if (log) |
Jason Molenda | 24c991c | 2011-08-25 00:20:04 +0000 | [diff] [blame] | 1571 | log->Printf ("HandleCommand, command line after removing command name(s): '%s'", remainder.c_str()); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1572 | |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1573 | cmd_obj->Execute (remainder.c_str(), result); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1574 | } |
| 1575 | else |
| 1576 | { |
| 1577 | // We didn't find the first command object, so complete the first argument. |
| 1578 | Args command_args (revised_command_line.GetData()); |
| 1579 | StringList matches; |
| 1580 | int num_matches; |
| 1581 | int cursor_index = 0; |
| 1582 | int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0)); |
| 1583 | bool word_complete; |
| 1584 | num_matches = HandleCompletionMatches (command_args, |
| 1585 | cursor_index, |
| 1586 | cursor_char_position, |
| 1587 | 0, |
| 1588 | -1, |
| 1589 | word_complete, |
| 1590 | matches); |
| 1591 | |
| 1592 | if (num_matches > 0) |
| 1593 | { |
| 1594 | std::string error_msg; |
| 1595 | error_msg.assign ("ambiguous command '"); |
| 1596 | error_msg.append(command_args.GetArgumentAtIndex(0)); |
| 1597 | error_msg.append ("'."); |
| 1598 | |
| 1599 | error_msg.append (" Possible completions:"); |
| 1600 | for (int i = 0; i < num_matches; i++) |
| 1601 | { |
| 1602 | error_msg.append ("\n\t"); |
| 1603 | error_msg.append (matches.GetStringAtIndex (i)); |
| 1604 | } |
| 1605 | error_msg.append ("\n"); |
| 1606 | result.AppendRawError (error_msg.c_str(), error_msg.size()); |
| 1607 | } |
| 1608 | else |
| 1609 | result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0)); |
| 1610 | |
| 1611 | result.SetStatus (eReturnStatusFailed); |
| 1612 | } |
| 1613 | |
Jason Molenda | 24c991c | 2011-08-25 00:20:04 +0000 | [diff] [blame] | 1614 | if (log) |
| 1615 | log->Printf ("HandleCommand, command %s", (result.Succeeded() ? "succeeded" : "did not succeed")); |
| 1616 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1617 | return result.Succeeded(); |
| 1618 | } |
| 1619 | |
| 1620 | int |
| 1621 | CommandInterpreter::HandleCompletionMatches (Args &parsed_line, |
| 1622 | int &cursor_index, |
| 1623 | int &cursor_char_position, |
| 1624 | int match_start_point, |
| 1625 | int max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1626 | bool &word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1627 | StringList &matches) |
| 1628 | { |
| 1629 | int num_command_matches = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1630 | bool look_for_subcommand = false; |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1631 | |
| 1632 | // For any of the command completions a unique match will be a complete word. |
| 1633 | word_complete = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1634 | |
| 1635 | if (cursor_index == -1) |
| 1636 | { |
| 1637 | // 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] | 1638 | bool include_aliases = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1639 | num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches); |
| 1640 | } |
| 1641 | else if (cursor_index == 0) |
| 1642 | { |
| 1643 | // 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] | 1644 | CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1645 | num_command_matches = matches.GetSize(); |
| 1646 | |
| 1647 | if (num_command_matches == 1 |
| 1648 | && cmd_obj && cmd_obj->IsMultiwordObject() |
| 1649 | && matches.GetStringAtIndex(0) != NULL |
| 1650 | && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0) |
| 1651 | { |
| 1652 | look_for_subcommand = true; |
| 1653 | num_command_matches = 0; |
| 1654 | matches.DeleteStringAtIndex(0); |
| 1655 | parsed_line.AppendArgument (""); |
| 1656 | cursor_index++; |
| 1657 | cursor_char_position = 0; |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | if (cursor_index > 0 || look_for_subcommand) |
| 1662 | { |
| 1663 | // We are completing further on into a commands arguments, so find the command and tell it |
| 1664 | // to complete the command. |
| 1665 | // First see if there is a matching initial command: |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 1666 | CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1667 | if (command_object == NULL) |
| 1668 | { |
| 1669 | return 0; |
| 1670 | } |
| 1671 | else |
| 1672 | { |
| 1673 | parsed_line.Shift(); |
| 1674 | cursor_index--; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1675 | num_command_matches = command_object->HandleCompletion (parsed_line, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1676 | cursor_index, |
| 1677 | cursor_char_position, |
| 1678 | match_start_point, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1679 | max_return_elements, |
| 1680 | word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1681 | matches); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | return num_command_matches; |
| 1686 | |
| 1687 | } |
| 1688 | |
| 1689 | int |
| 1690 | CommandInterpreter::HandleCompletion (const char *current_line, |
| 1691 | const char *cursor, |
| 1692 | const char *last_char, |
| 1693 | int match_start_point, |
| 1694 | int max_return_elements, |
| 1695 | StringList &matches) |
| 1696 | { |
| 1697 | // We parse the argument up to the cursor, so the last argument in parsed_line is |
| 1698 | // the one containing the cursor, and the cursor is after the last character. |
| 1699 | |
| 1700 | Args parsed_line(current_line, last_char - current_line); |
| 1701 | Args partial_parsed_line(current_line, cursor - current_line); |
| 1702 | |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1703 | // Don't complete comments, and if the line we are completing is just the history repeat character, |
| 1704 | // substitute the appropriate history line. |
| 1705 | const char *first_arg = parsed_line.GetArgumentAtIndex(0); |
| 1706 | if (first_arg) |
| 1707 | { |
| 1708 | if (first_arg[0] == m_comment_char) |
| 1709 | return 0; |
| 1710 | else if (first_arg[0] == m_repeat_char) |
| 1711 | { |
| 1712 | const char *history_string = FindHistoryString (first_arg); |
| 1713 | if (history_string != NULL) |
| 1714 | { |
| 1715 | matches.Clear(); |
| 1716 | matches.InsertStringAtIndex(0, history_string); |
| 1717 | return -2; |
| 1718 | } |
| 1719 | else |
| 1720 | return 0; |
| 1721 | |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1726 | int num_args = partial_parsed_line.GetArgumentCount(); |
| 1727 | int cursor_index = partial_parsed_line.GetArgumentCount() - 1; |
| 1728 | int cursor_char_position; |
| 1729 | |
| 1730 | if (cursor_index == -1) |
| 1731 | cursor_char_position = 0; |
| 1732 | else |
| 1733 | cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index)); |
Jim Ingham | cf03765 | 2010-12-14 19:56:01 +0000 | [diff] [blame] | 1734 | |
| 1735 | if (cursor > current_line && cursor[-1] == ' ') |
| 1736 | { |
| 1737 | // We are just after a space. If we are in an argument, then we will continue |
| 1738 | // parsing, but if we are between arguments, then we have to complete whatever the next |
| 1739 | // element would be. |
| 1740 | // We can distinguish the two cases because if we are in an argument (e.g. because the space is |
| 1741 | // protected by a quote) then the space will also be in the parsed argument... |
| 1742 | |
| 1743 | const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index); |
| 1744 | if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ') |
| 1745 | { |
| 1746 | parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '"'); |
| 1747 | cursor_index++; |
| 1748 | cursor_char_position = 0; |
| 1749 | } |
| 1750 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1751 | |
| 1752 | int num_command_matches; |
| 1753 | |
| 1754 | matches.Clear(); |
| 1755 | |
| 1756 | // Only max_return_elements == -1 is supported at present: |
| 1757 | assert (max_return_elements == -1); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1758 | bool word_complete; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1759 | num_command_matches = HandleCompletionMatches (parsed_line, |
| 1760 | cursor_index, |
| 1761 | cursor_char_position, |
| 1762 | match_start_point, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1763 | max_return_elements, |
| 1764 | word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1765 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1766 | |
| 1767 | if (num_command_matches <= 0) |
| 1768 | return num_command_matches; |
| 1769 | |
| 1770 | if (num_args == 0) |
| 1771 | { |
| 1772 | // If we got an empty string, insert nothing. |
| 1773 | matches.InsertStringAtIndex(0, ""); |
| 1774 | } |
| 1775 | else |
| 1776 | { |
| 1777 | // Now figure out if there is a common substring, and if so put that in element 0, otherwise |
| 1778 | // put an empty string in element 0. |
| 1779 | std::string command_partial_str; |
| 1780 | if (cursor_index >= 0) |
Jim Ingham | e3663e8 | 2010-10-22 18:47:16 +0000 | [diff] [blame] | 1781 | command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index), |
| 1782 | parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1783 | |
| 1784 | std::string common_prefix; |
| 1785 | matches.LongestCommonPrefix (common_prefix); |
| 1786 | int partial_name_len = command_partial_str.size(); |
| 1787 | |
| 1788 | // If we matched a unique single command, add a space... |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1789 | // Only do this if the completer told us this was a complete word, however... |
| 1790 | if (num_command_matches == 1 && word_complete) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1791 | { |
| 1792 | char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index); |
| 1793 | if (quote_char != '\0') |
| 1794 | common_prefix.push_back(quote_char); |
| 1795 | |
| 1796 | common_prefix.push_back(' '); |
| 1797 | } |
| 1798 | common_prefix.erase (0, partial_name_len); |
| 1799 | matches.InsertStringAtIndex(0, common_prefix.c_str()); |
| 1800 | } |
| 1801 | return num_command_matches; |
| 1802 | } |
| 1803 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1804 | |
| 1805 | CommandInterpreter::~CommandInterpreter () |
| 1806 | { |
| 1807 | } |
| 1808 | |
| 1809 | const char * |
| 1810 | CommandInterpreter::GetPrompt () |
| 1811 | { |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1812 | return m_debugger.GetPrompt(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | void |
| 1816 | CommandInterpreter::SetPrompt (const char *new_prompt) |
| 1817 | { |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1818 | m_debugger.SetPrompt (new_prompt); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1821 | size_t |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1822 | CommandInterpreter::GetConfirmationInputReaderCallback |
| 1823 | ( |
| 1824 | void *baton, |
| 1825 | InputReader &reader, |
| 1826 | lldb::InputReaderAction action, |
| 1827 | const char *bytes, |
| 1828 | size_t bytes_len |
| 1829 | ) |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1830 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1831 | File &out_file = reader.GetDebugger().GetOutputFile(); |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1832 | bool *response_ptr = (bool *) baton; |
| 1833 | |
| 1834 | switch (action) |
| 1835 | { |
| 1836 | case eInputReaderActivate: |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1837 | if (out_file.IsValid()) |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1838 | { |
| 1839 | if (reader.GetPrompt()) |
Caroline Tice | 22a6009 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 1840 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1841 | out_file.Printf ("%s", reader.GetPrompt()); |
| 1842 | out_file.Flush (); |
Caroline Tice | 22a6009 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 1843 | } |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1844 | } |
| 1845 | break; |
| 1846 | |
| 1847 | case eInputReaderDeactivate: |
| 1848 | break; |
| 1849 | |
| 1850 | case eInputReaderReactivate: |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1851 | if (out_file.IsValid() && reader.GetPrompt()) |
Caroline Tice | 22a6009 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 1852 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1853 | out_file.Printf ("%s", reader.GetPrompt()); |
| 1854 | out_file.Flush (); |
Caroline Tice | 22a6009 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 1855 | } |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1856 | break; |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 1857 | |
| 1858 | case eInputReaderAsynchronousOutputWritten: |
| 1859 | break; |
| 1860 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1861 | case eInputReaderGotToken: |
| 1862 | if (bytes_len == 0) |
| 1863 | { |
| 1864 | reader.SetIsDone(true); |
| 1865 | } |
Jim Ingham | 36fe991 | 2011-11-14 20:02:01 +0000 | [diff] [blame] | 1866 | else if (bytes[0] == 'y' || bytes[0] == 'Y') |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1867 | { |
| 1868 | *response_ptr = true; |
| 1869 | reader.SetIsDone(true); |
| 1870 | } |
Jim Ingham | 36fe991 | 2011-11-14 20:02:01 +0000 | [diff] [blame] | 1871 | else if (bytes[0] == 'n' || bytes[0] == 'N') |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1872 | { |
| 1873 | *response_ptr = false; |
| 1874 | reader.SetIsDone(true); |
| 1875 | } |
| 1876 | else |
| 1877 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1878 | if (out_file.IsValid() && !reader.IsDone() && reader.GetPrompt()) |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1879 | { |
Jim Ingham | 2618380 | 2011-11-17 01:22:00 +0000 | [diff] [blame] | 1880 | out_file.Printf ("Please answer \"y\" or \"n\".\n%s", reader.GetPrompt()); |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1881 | out_file.Flush (); |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | break; |
| 1885 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1886 | case eInputReaderInterrupt: |
| 1887 | case eInputReaderEndOfFile: |
| 1888 | *response_ptr = false; // Assume ^C or ^D means cancel the proposed action |
| 1889 | reader.SetIsDone (true); |
| 1890 | break; |
| 1891 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1892 | case eInputReaderDone: |
| 1893 | break; |
| 1894 | } |
| 1895 | |
| 1896 | return bytes_len; |
| 1897 | |
| 1898 | } |
| 1899 | |
| 1900 | bool |
| 1901 | CommandInterpreter::Confirm (const char *message, bool default_answer) |
| 1902 | { |
Jim Ingham | 9305747 | 2010-10-04 22:44:14 +0000 | [diff] [blame] | 1903 | // Check AutoConfirm first: |
| 1904 | if (m_debugger.GetAutoConfirm()) |
| 1905 | return default_answer; |
| 1906 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1907 | InputReaderSP reader_sp (new InputReader(GetDebugger())); |
| 1908 | bool response = default_answer; |
| 1909 | if (reader_sp) |
| 1910 | { |
| 1911 | std::string prompt(message); |
| 1912 | prompt.append(": ["); |
| 1913 | if (default_answer) |
| 1914 | prompt.append ("Y/n] "); |
| 1915 | else |
| 1916 | prompt.append ("y/N] "); |
| 1917 | |
| 1918 | Error err (reader_sp->Initialize (CommandInterpreter::GetConfirmationInputReaderCallback, |
| 1919 | &response, // baton |
| 1920 | eInputReaderGranularityLine, // token size, to pass to callback function |
| 1921 | NULL, // end token |
| 1922 | prompt.c_str(), // prompt |
| 1923 | true)); // echo input |
| 1924 | if (err.Success()) |
| 1925 | { |
| 1926 | GetDebugger().PushInputReader (reader_sp); |
| 1927 | } |
| 1928 | reader_sp->WaitOnReaderIsDone(); |
| 1929 | } |
| 1930 | return response; |
| 1931 | } |
| 1932 | |
| 1933 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1934 | void |
| 1935 | CommandInterpreter::CrossRegisterCommand (const char * dest_cmd, const char * object_type) |
| 1936 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 1937 | CommandObjectSP cmd_obj_sp = GetCommandSPExact (dest_cmd, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1938 | |
| 1939 | if (cmd_obj_sp != NULL) |
| 1940 | { |
| 1941 | CommandObject *cmd_obj = cmd_obj_sp.get(); |
| 1942 | if (cmd_obj->IsCrossRefObject ()) |
| 1943 | cmd_obj->AddObject (object_type); |
| 1944 | } |
| 1945 | } |
| 1946 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1947 | OptionArgVectorSP |
| 1948 | CommandInterpreter::GetAliasOptions (const char *alias_name) |
| 1949 | { |
| 1950 | OptionArgMap::iterator pos; |
| 1951 | OptionArgVectorSP ret_val; |
| 1952 | |
| 1953 | std::string alias (alias_name); |
| 1954 | |
| 1955 | if (HasAliasOptions()) |
| 1956 | { |
| 1957 | pos = m_alias_options.find (alias); |
| 1958 | if (pos != m_alias_options.end()) |
| 1959 | ret_val = pos->second; |
| 1960 | } |
| 1961 | |
| 1962 | return ret_val; |
| 1963 | } |
| 1964 | |
| 1965 | void |
| 1966 | CommandInterpreter::RemoveAliasOptions (const char *alias_name) |
| 1967 | { |
| 1968 | OptionArgMap::iterator pos = m_alias_options.find(alias_name); |
| 1969 | if (pos != m_alias_options.end()) |
| 1970 | { |
| 1971 | m_alias_options.erase (pos); |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | void |
| 1976 | CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp) |
| 1977 | { |
| 1978 | m_alias_options[alias_name] = option_arg_vector_sp; |
| 1979 | } |
| 1980 | |
| 1981 | bool |
| 1982 | CommandInterpreter::HasCommands () |
| 1983 | { |
| 1984 | return (!m_command_dict.empty()); |
| 1985 | } |
| 1986 | |
| 1987 | bool |
| 1988 | CommandInterpreter::HasAliases () |
| 1989 | { |
| 1990 | return (!m_alias_dict.empty()); |
| 1991 | } |
| 1992 | |
| 1993 | bool |
| 1994 | CommandInterpreter::HasUserCommands () |
| 1995 | { |
| 1996 | return (!m_user_dict.empty()); |
| 1997 | } |
| 1998 | |
| 1999 | bool |
| 2000 | CommandInterpreter::HasAliasOptions () |
| 2001 | { |
| 2002 | return (!m_alias_options.empty()); |
| 2003 | } |
| 2004 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2005 | void |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 2006 | CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj, |
| 2007 | const char *alias_name, |
| 2008 | Args &cmd_args, |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2009 | std::string &raw_input_string, |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 2010 | CommandReturnObject &result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2011 | { |
| 2012 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2013 | |
| 2014 | bool wants_raw_input = alias_cmd_obj->WantsRawCommandString(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2015 | |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2016 | // Make sure that the alias name is the 0th element in cmd_args |
| 2017 | std::string alias_name_str = alias_name; |
| 2018 | if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0) |
| 2019 | cmd_args.Unshift (alias_name); |
| 2020 | |
| 2021 | Args new_args (alias_cmd_obj->GetCommandName()); |
| 2022 | if (new_args.GetArgumentCount() == 2) |
| 2023 | new_args.Shift(); |
| 2024 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2025 | if (option_arg_vector_sp.get()) |
| 2026 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2027 | if (wants_raw_input) |
| 2028 | { |
| 2029 | // We have a command that both has command options and takes raw input. Make *sure* it has a |
| 2030 | // " -- " in the right place in the raw_input_string. |
| 2031 | size_t pos = raw_input_string.find(" -- "); |
| 2032 | if (pos == std::string::npos) |
| 2033 | { |
| 2034 | // None found; assume it goes at the beginning of the raw input string |
| 2035 | raw_input_string.insert (0, " -- "); |
| 2036 | } |
| 2037 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2038 | |
| 2039 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 2040 | int old_size = cmd_args.GetArgumentCount(); |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 2041 | std::vector<bool> used (old_size + 1, false); |
| 2042 | |
| 2043 | used[0] = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2044 | |
| 2045 | for (int i = 0; i < option_arg_vector->size(); ++i) |
| 2046 | { |
| 2047 | OptionArgPair option_pair = (*option_arg_vector)[i]; |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2048 | OptionArgValue value_pair = option_pair.second; |
| 2049 | int value_type = value_pair.first; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2050 | std::string option = option_pair.first; |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2051 | std::string value = value_pair.second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2052 | if (option.compare ("<argument>") == 0) |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2053 | { |
| 2054 | if (!wants_raw_input |
| 2055 | || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice |
| 2056 | new_args.AppendArgument (value.c_str()); |
| 2057 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2058 | else |
| 2059 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2060 | if (value_type != optional_argument) |
| 2061 | new_args.AppendArgument (option.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2062 | if (value.compare ("<no-argument>") != 0) |
| 2063 | { |
| 2064 | int index = GetOptionArgumentPosition (value.c_str()); |
| 2065 | if (index == 0) |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2066 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2067 | // value was NOT a positional argument; must be a real value |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2068 | if (value_type != optional_argument) |
| 2069 | new_args.AppendArgument (value.c_str()); |
| 2070 | else |
| 2071 | { |
| 2072 | char buffer[255]; |
| 2073 | ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str()); |
| 2074 | new_args.AppendArgument (buffer); |
| 2075 | } |
| 2076 | |
| 2077 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2078 | else if (index >= cmd_args.GetArgumentCount()) |
| 2079 | { |
| 2080 | result.AppendErrorWithFormat |
| 2081 | ("Not enough arguments provided; you need at least %d arguments to use this alias.\n", |
| 2082 | index); |
| 2083 | result.SetStatus (eReturnStatusFailed); |
| 2084 | return; |
| 2085 | } |
| 2086 | else |
| 2087 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2088 | // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string |
| 2089 | size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index)); |
| 2090 | if (strpos != std::string::npos) |
| 2091 | { |
| 2092 | raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index))); |
| 2093 | } |
| 2094 | |
| 2095 | if (value_type != optional_argument) |
| 2096 | new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index)); |
| 2097 | else |
| 2098 | { |
| 2099 | char buffer[255]; |
| 2100 | ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(), |
| 2101 | cmd_args.GetArgumentAtIndex (index)); |
| 2102 | new_args.AppendArgument (buffer); |
| 2103 | } |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 2104 | used[index] = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | for (int j = 0; j < cmd_args.GetArgumentCount(); ++j) |
| 2111 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2112 | if (!used[j] && !wants_raw_input) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2113 | new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j)); |
| 2114 | } |
| 2115 | |
| 2116 | cmd_args.Clear(); |
| 2117 | cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector()); |
| 2118 | } |
| 2119 | else |
| 2120 | { |
| 2121 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2122 | // This alias was not created with any options; nothing further needs to be done, unless it is a command that |
| 2123 | // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw |
| 2124 | // input string. |
| 2125 | if (wants_raw_input) |
| 2126 | { |
| 2127 | cmd_args.Clear(); |
| 2128 | cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector()); |
| 2129 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2130 | return; |
| 2131 | } |
| 2132 | |
| 2133 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 2134 | return; |
| 2135 | } |
| 2136 | |
| 2137 | |
| 2138 | int |
| 2139 | CommandInterpreter::GetOptionArgumentPosition (const char *in_string) |
| 2140 | { |
| 2141 | int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position |
| 2142 | // of zero. |
| 2143 | |
| 2144 | char *cptr = (char *) in_string; |
| 2145 | |
| 2146 | // Does it start with '%' |
| 2147 | if (cptr[0] == '%') |
| 2148 | { |
| 2149 | ++cptr; |
| 2150 | |
| 2151 | // Is the rest of it entirely digits? |
| 2152 | if (isdigit (cptr[0])) |
| 2153 | { |
| 2154 | const char *start = cptr; |
| 2155 | while (isdigit (cptr[0])) |
| 2156 | ++cptr; |
| 2157 | |
| 2158 | // We've gotten to the end of the digits; are we at the end of the string? |
| 2159 | if (cptr[0] == '\0') |
| 2160 | position = atoi (start); |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | return position; |
| 2165 | } |
| 2166 | |
| 2167 | void |
| 2168 | CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result) |
| 2169 | { |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2170 | FileSpec init_file; |
Greg Clayton | d6edcb5 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2171 | if (in_cwd) |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2172 | { |
Greg Clayton | d6edcb5 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2173 | // In the current working directory we don't load any program specific |
| 2174 | // .lldbinit files, we only look for a "./.lldbinit" file. |
| 2175 | if (m_skip_lldbinit_files) |
| 2176 | return; |
| 2177 | |
| 2178 | init_file.SetFile ("./.lldbinit", true); |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2179 | } |
Greg Clayton | d6edcb5 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2180 | else |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2181 | { |
Greg Clayton | d6edcb5 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2182 | // If we aren't looking in the current working directory we are looking |
| 2183 | // in the home directory. We will first see if there is an application |
| 2184 | // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a |
| 2185 | // "-" and the name of the program. If this file doesn't exist, we fall |
| 2186 | // back to just the "~/.lldbinit" file. We also obey any requests to not |
| 2187 | // load the init files. |
| 2188 | const char *init_file_path = "~/.lldbinit"; |
| 2189 | |
| 2190 | if (m_skip_app_init_files == false) |
| 2191 | { |
| 2192 | FileSpec program_file_spec (Host::GetProgramFileSpec()); |
| 2193 | const char *program_name = program_file_spec.GetFilename().AsCString(); |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2194 | |
Greg Clayton | d6edcb5 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2195 | if (program_name) |
| 2196 | { |
| 2197 | char program_init_file_name[PATH_MAX]; |
| 2198 | ::snprintf (program_init_file_name, sizeof(program_init_file_name), "%s-%s", init_file_path, program_name); |
| 2199 | init_file.SetFile (program_init_file_name, true); |
| 2200 | if (!init_file.Exists()) |
| 2201 | init_file.Clear(); |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | if (!init_file && !m_skip_lldbinit_files) |
| 2206 | init_file.SetFile (init_file_path, true); |
| 2207 | } |
| 2208 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2209 | // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting |
| 2210 | // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details). |
| 2211 | |
| 2212 | if (init_file.Exists()) |
| 2213 | { |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2214 | ExecutionContext *exe_ctx = NULL; // We don't have any context yet. |
| 2215 | bool stop_on_continue = true; |
| 2216 | bool stop_on_error = false; |
| 2217 | bool echo_commands = false; |
| 2218 | bool print_results = false; |
| 2219 | |
Enrico Granata | 01bc2d4 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 2220 | HandleCommandsFromFile (init_file, exe_ctx, stop_on_continue, stop_on_error, echo_commands, print_results, eLazyBoolNo, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2221 | } |
| 2222 | else |
| 2223 | { |
| 2224 | // nothing to be done if the file doesn't exist |
| 2225 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 2226 | } |
| 2227 | } |
| 2228 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2229 | PlatformSP |
| 2230 | CommandInterpreter::GetPlatform (bool prefer_target_platform) |
| 2231 | { |
| 2232 | PlatformSP platform_sp; |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2233 | if (prefer_target_platform) |
| 2234 | { |
Greg Clayton | bcaf99a | 2012-07-12 20:32:19 +0000 | [diff] [blame] | 2235 | ExecutionContext exe_ctx(GetExecutionContext()); |
| 2236 | Target *target = exe_ctx.GetTargetPtr(); |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2237 | if (target) |
| 2238 | platform_sp = target->GetPlatform(); |
| 2239 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2240 | |
| 2241 | if (!platform_sp) |
| 2242 | platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform(); |
| 2243 | return platform_sp; |
| 2244 | } |
| 2245 | |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2246 | void |
Jim Ingham | a4fede3 | 2011-03-11 01:51:49 +0000 | [diff] [blame] | 2247 | CommandInterpreter::HandleCommands (const StringList &commands, |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2248 | ExecutionContext *override_context, |
| 2249 | bool stop_on_continue, |
| 2250 | bool stop_on_error, |
| 2251 | bool echo_commands, |
| 2252 | bool print_results, |
Enrico Granata | 01bc2d4 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 2253 | LazyBool add_to_history, |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2254 | CommandReturnObject &result) |
| 2255 | { |
| 2256 | size_t num_lines = commands.GetSize(); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2257 | |
| 2258 | // If we are going to continue past a "continue" then we need to run the commands synchronously. |
| 2259 | // Make sure you reset this value anywhere you return from the function. |
| 2260 | |
| 2261 | bool old_async_execution = m_debugger.GetAsyncExecution(); |
| 2262 | |
| 2263 | // If we've been given an execution context, set it at the start, but don't keep resetting it or we will |
| 2264 | // cause series of commands that change the context, then do an operation that relies on that context to fail. |
| 2265 | |
| 2266 | if (override_context != NULL) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2267 | UpdateExecutionContext (override_context); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2268 | |
| 2269 | if (!stop_on_continue) |
| 2270 | { |
| 2271 | m_debugger.SetAsyncExecution (false); |
| 2272 | } |
| 2273 | |
| 2274 | for (int idx = 0; idx < num_lines; idx++) |
| 2275 | { |
| 2276 | const char *cmd = commands.GetStringAtIndex(idx); |
| 2277 | if (cmd[0] == '\0') |
| 2278 | continue; |
| 2279 | |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2280 | if (echo_commands) |
| 2281 | { |
| 2282 | result.AppendMessageWithFormat ("%s %s\n", |
| 2283 | GetPrompt(), |
| 2284 | cmd); |
| 2285 | } |
| 2286 | |
Greg Clayton | aa378b1 | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 2287 | CommandReturnObject tmp_result; |
Johnny Chen | 8bdf57c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 2288 | // If override_context is not NULL, pass no_context_switching = true for |
| 2289 | // HandleCommand() since we updated our context already. |
Enrico Granata | 01bc2d4 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 2290 | bool success = HandleCommand(cmd, add_to_history, tmp_result, |
Johnny Chen | 8bdf57c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 2291 | NULL, /* override_context */ |
| 2292 | true, /* repeat_on_empty_command */ |
| 2293 | override_context != NULL /* no_context_switching */); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2294 | |
| 2295 | if (print_results) |
| 2296 | { |
| 2297 | if (tmp_result.Succeeded()) |
Jim Ingham | 2e8cb8a | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 2298 | result.AppendMessageWithFormat("%s", tmp_result.GetOutputData()); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | if (!success || !tmp_result.Succeeded()) |
| 2302 | { |
Jim Ingham | 862fd5c | 2012-04-24 02:25:07 +0000 | [diff] [blame] | 2303 | const char *error_msg = tmp_result.GetErrorData(); |
| 2304 | if (error_msg == NULL || error_msg[0] == '\0') |
| 2305 | error_msg = "<unknown error>.\n"; |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2306 | if (stop_on_error) |
| 2307 | { |
Jim Ingham | 862fd5c | 2012-04-24 02:25:07 +0000 | [diff] [blame] | 2308 | result.AppendErrorWithFormat("Aborting reading of commands after command #%d: '%s' failed with %s", |
| 2309 | idx, cmd, error_msg); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2310 | result.SetStatus (eReturnStatusFailed); |
| 2311 | m_debugger.SetAsyncExecution (old_async_execution); |
| 2312 | return; |
| 2313 | } |
| 2314 | else if (print_results) |
| 2315 | { |
Jim Ingham | 862fd5c | 2012-04-24 02:25:07 +0000 | [diff] [blame] | 2316 | result.AppendMessageWithFormat ("Command #%d '%s' failed with %s", |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2317 | idx + 1, |
| 2318 | cmd, |
Jim Ingham | 862fd5c | 2012-04-24 02:25:07 +0000 | [diff] [blame] | 2319 | error_msg); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2320 | } |
| 2321 | } |
| 2322 | |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 2323 | if (result.GetImmediateOutputStream()) |
| 2324 | result.GetImmediateOutputStream()->Flush(); |
| 2325 | |
| 2326 | if (result.GetImmediateErrorStream()) |
| 2327 | result.GetImmediateErrorStream()->Flush(); |
| 2328 | |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2329 | // N.B. Can't depend on DidChangeProcessState, because the state coming into the command execution |
| 2330 | // could be running (for instance in Breakpoint Commands. |
| 2331 | // So we check the return value to see if it is has running in it. |
| 2332 | if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult) |
| 2333 | || (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult)) |
| 2334 | { |
| 2335 | if (stop_on_continue) |
| 2336 | { |
| 2337 | // If we caused the target to proceed, and we're going to stop in that case, set the |
| 2338 | // status in our real result before returning. This is an error if the continue was not the |
| 2339 | // last command in the set of commands to be run. |
| 2340 | if (idx != num_lines - 1) |
| 2341 | result.AppendErrorWithFormat("Aborting reading of commands after command #%d: '%s' continued the target.\n", |
| 2342 | idx + 1, cmd); |
| 2343 | else |
| 2344 | result.AppendMessageWithFormat ("Command #%d '%s' continued the target.\n", idx + 1, cmd); |
| 2345 | |
| 2346 | result.SetStatus(tmp_result.GetStatus()); |
| 2347 | m_debugger.SetAsyncExecution (old_async_execution); |
| 2348 | |
| 2349 | return; |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | } |
| 2354 | |
| 2355 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 2356 | m_debugger.SetAsyncExecution (old_async_execution); |
| 2357 | |
| 2358 | return; |
| 2359 | } |
| 2360 | |
| 2361 | void |
| 2362 | CommandInterpreter::HandleCommandsFromFile (FileSpec &cmd_file, |
| 2363 | ExecutionContext *context, |
| 2364 | bool stop_on_continue, |
| 2365 | bool stop_on_error, |
| 2366 | bool echo_command, |
| 2367 | bool print_result, |
Enrico Granata | 01bc2d4 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 2368 | LazyBool add_to_history, |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2369 | CommandReturnObject &result) |
| 2370 | { |
| 2371 | if (cmd_file.Exists()) |
| 2372 | { |
| 2373 | bool success; |
| 2374 | StringList commands; |
| 2375 | success = commands.ReadFileLines(cmd_file); |
| 2376 | if (!success) |
| 2377 | { |
| 2378 | result.AppendErrorWithFormat ("Error reading commands from file: %s.\n", cmd_file.GetFilename().AsCString()); |
| 2379 | result.SetStatus (eReturnStatusFailed); |
| 2380 | return; |
| 2381 | } |
Enrico Granata | 01bc2d4 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 2382 | m_command_source_depth++; |
| 2383 | HandleCommands (commands, context, stop_on_continue, stop_on_error, echo_command, print_result, add_to_history, result); |
| 2384 | m_command_source_depth--; |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2385 | } |
| 2386 | else |
| 2387 | { |
| 2388 | result.AppendErrorWithFormat ("Error reading commands from file %s - file not found.\n", |
| 2389 | cmd_file.GetFilename().AsCString()); |
| 2390 | result.SetStatus (eReturnStatusFailed); |
| 2391 | return; |
| 2392 | } |
| 2393 | } |
| 2394 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2395 | ScriptInterpreter * |
| 2396 | CommandInterpreter::GetScriptInterpreter () |
| 2397 | { |
Enrico Granata | c5c10a4 | 2012-07-10 18:23:48 +0000 | [diff] [blame] | 2398 | // <rdar://problem/11751427> |
| 2399 | // we need to protect the initialization of the script interpreter |
| 2400 | // otherwise we could end up with two threads both trying to create |
| 2401 | // their instance of it, and for some languages (e.g. Python) |
| 2402 | // this is a bulletproof recipe for disaster! |
| 2403 | // this needs to be a function-level static because multiple Debugger instances living in the same process |
| 2404 | // still need to be isolated and not try to initialize Python concurrently |
Enrico Granata | b88c0a9 | 2012-07-10 19:04:14 +0000 | [diff] [blame] | 2405 | static Mutex g_interpreter_mutex(Mutex::eMutexTypeRecursive); |
| 2406 | Mutex::Locker interpreter_lock(g_interpreter_mutex); |
Enrico Granata | c5c10a4 | 2012-07-10 18:23:48 +0000 | [diff] [blame] | 2407 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2408 | if (m_script_interpreter_ap.get() != NULL) |
| 2409 | return m_script_interpreter_ap.get(); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 2410 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2411 | lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage(); |
| 2412 | switch (script_lang) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2413 | { |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 2414 | case eScriptLanguagePython: |
| 2415 | #ifndef LLDB_DISABLE_PYTHON |
| 2416 | m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this)); |
| 2417 | break; |
| 2418 | #else |
| 2419 | // Fall through to the None case when python is disabled |
| 2420 | #endif |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2421 | case eScriptLanguageNone: |
| 2422 | m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this)); |
| 2423 | break; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2424 | default: |
| 2425 | break; |
| 2426 | }; |
| 2427 | |
| 2428 | return m_script_interpreter_ap.get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | |
| 2432 | |
| 2433 | bool |
| 2434 | CommandInterpreter::GetSynchronous () |
| 2435 | { |
| 2436 | return m_synchronous_execution; |
| 2437 | } |
| 2438 | |
| 2439 | void |
| 2440 | CommandInterpreter::SetSynchronous (bool value) |
| 2441 | { |
Johnny Chen | d7a4eb0 | 2010-10-14 01:22:03 +0000 | [diff] [blame] | 2442 | m_synchronous_execution = value; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | void |
| 2446 | CommandInterpreter::OutputFormattedHelpText (Stream &strm, |
| 2447 | const char *word_text, |
| 2448 | const char *separator, |
| 2449 | const char *help_text, |
| 2450 | uint32_t max_word_len) |
| 2451 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 2452 | const uint32_t max_columns = m_debugger.GetTerminalWidth(); |
| 2453 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2454 | int indent_size = max_word_len + strlen (separator) + 2; |
| 2455 | |
| 2456 | strm.IndentMore (indent_size); |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 2457 | |
| 2458 | StreamString text_strm; |
| 2459 | text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text); |
| 2460 | |
| 2461 | size_t len = text_strm.GetSize(); |
| 2462 | const char *text = text_strm.GetData(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2463 | if (text[len - 1] == '\n') |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 2464 | { |
| 2465 | text_strm.EOL(); |
| 2466 | len = text_strm.GetSize(); |
| 2467 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2468 | |
| 2469 | if (len < max_columns) |
| 2470 | { |
| 2471 | // Output it as a single line. |
| 2472 | strm.Printf ("%s", text); |
| 2473 | } |
| 2474 | else |
| 2475 | { |
| 2476 | // We need to break it up into multiple lines. |
| 2477 | bool first_line = true; |
| 2478 | int text_width; |
| 2479 | int start = 0; |
| 2480 | int end = start; |
| 2481 | int final_end = strlen (text); |
| 2482 | int sub_len; |
| 2483 | |
| 2484 | while (end < final_end) |
| 2485 | { |
| 2486 | if (first_line) |
| 2487 | text_width = max_columns - 1; |
| 2488 | else |
| 2489 | text_width = max_columns - indent_size - 1; |
| 2490 | |
| 2491 | // Don't start the 'text' on a space, since we're already outputting the indentation. |
| 2492 | if (!first_line) |
| 2493 | { |
| 2494 | while ((start < final_end) && (text[start] == ' ')) |
| 2495 | start++; |
| 2496 | } |
| 2497 | |
| 2498 | end = start + text_width; |
| 2499 | if (end > final_end) |
| 2500 | end = final_end; |
| 2501 | else |
| 2502 | { |
| 2503 | // If we're not at the end of the text, make sure we break the line on white space. |
| 2504 | while (end > start |
| 2505 | && text[end] != ' ' && text[end] != '\t' && text[end] != '\n') |
| 2506 | end--; |
| 2507 | } |
| 2508 | |
| 2509 | sub_len = end - start; |
| 2510 | if (start != 0) |
| 2511 | strm.EOL(); |
| 2512 | if (!first_line) |
| 2513 | strm.Indent(); |
| 2514 | else |
| 2515 | first_line = false; |
| 2516 | assert (start <= final_end); |
| 2517 | assert (start + sub_len <= final_end); |
| 2518 | if (sub_len > 0) |
| 2519 | strm.Write (text + start, sub_len); |
| 2520 | start = end + 1; |
| 2521 | } |
| 2522 | } |
| 2523 | strm.EOL(); |
| 2524 | strm.IndentLess(indent_size); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | void |
Enrico Granata | 1bba6e5 | 2011-07-07 00:38:40 +0000 | [diff] [blame] | 2528 | CommandInterpreter::OutputHelpText (Stream &strm, |
| 2529 | const char *word_text, |
| 2530 | const char *separator, |
| 2531 | const char *help_text, |
| 2532 | uint32_t max_word_len) |
| 2533 | { |
| 2534 | int indent_size = max_word_len + strlen (separator) + 2; |
| 2535 | |
| 2536 | strm.IndentMore (indent_size); |
| 2537 | |
| 2538 | StreamString text_strm; |
| 2539 | text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text); |
| 2540 | |
| 2541 | const uint32_t max_columns = m_debugger.GetTerminalWidth(); |
Enrico Granata | 1bba6e5 | 2011-07-07 00:38:40 +0000 | [diff] [blame] | 2542 | |
| 2543 | size_t len = text_strm.GetSize(); |
| 2544 | const char *text = text_strm.GetData(); |
| 2545 | |
| 2546 | uint32_t chars_left = max_columns; |
| 2547 | |
| 2548 | for (uint32_t i = 0; i < len; i++) |
| 2549 | { |
| 2550 | if ((text[i] == ' ' && ::strchr((text+i+1), ' ') && chars_left < ::strchr((text+i+1), ' ')-(text+i)) || text[i] == '\n') |
| 2551 | { |
Enrico Granata | 1bba6e5 | 2011-07-07 00:38:40 +0000 | [diff] [blame] | 2552 | chars_left = max_columns - indent_size; |
| 2553 | strm.EOL(); |
| 2554 | strm.Indent(); |
| 2555 | } |
| 2556 | else |
| 2557 | { |
| 2558 | strm.PutChar(text[i]); |
| 2559 | chars_left--; |
| 2560 | } |
| 2561 | |
| 2562 | } |
| 2563 | |
| 2564 | strm.EOL(); |
| 2565 | strm.IndentLess(indent_size); |
| 2566 | } |
| 2567 | |
| 2568 | void |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2569 | CommandInterpreter::AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word, |
| 2570 | StringList &commands_found, StringList &commands_help) |
| 2571 | { |
| 2572 | CommandObject::CommandMap::const_iterator pos; |
| 2573 | CommandObject::CommandMap sub_cmd_dict = ((CommandObjectMultiword *) cmd_obj)->m_subcommand_dict; |
| 2574 | CommandObject *sub_cmd_obj; |
| 2575 | |
| 2576 | for (pos = sub_cmd_dict.begin(); pos != sub_cmd_dict.end(); ++pos) |
| 2577 | { |
| 2578 | const char * command_name = pos->first.c_str(); |
| 2579 | sub_cmd_obj = pos->second.get(); |
| 2580 | StreamString complete_command_name; |
| 2581 | |
| 2582 | complete_command_name.Printf ("%s %s", prefix, command_name); |
| 2583 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 2584 | if (sub_cmd_obj->HelpTextContainsWord (search_word)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2585 | { |
| 2586 | commands_found.AppendString (complete_command_name.GetData()); |
| 2587 | commands_help.AppendString (sub_cmd_obj->GetHelp()); |
| 2588 | } |
| 2589 | |
| 2590 | if (sub_cmd_obj->IsMultiwordObject()) |
| 2591 | AproposAllSubCommands (sub_cmd_obj, complete_command_name.GetData(), search_word, commands_found, |
| 2592 | commands_help); |
| 2593 | } |
| 2594 | |
| 2595 | } |
| 2596 | |
| 2597 | void |
| 2598 | CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found, |
| 2599 | StringList &commands_help) |
| 2600 | { |
| 2601 | CommandObject::CommandMap::const_iterator pos; |
| 2602 | |
| 2603 | for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) |
| 2604 | { |
| 2605 | const char *command_name = pos->first.c_str(); |
| 2606 | CommandObject *cmd_obj = pos->second.get(); |
| 2607 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 2608 | if (cmd_obj->HelpTextContainsWord (search_word)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2609 | { |
| 2610 | commands_found.AppendString (command_name); |
| 2611 | commands_help.AppendString (cmd_obj->GetHelp()); |
| 2612 | } |
| 2613 | |
| 2614 | if (cmd_obj->IsMultiwordObject()) |
| 2615 | AproposAllSubCommands (cmd_obj, command_name, search_word, commands_found, commands_help); |
| 2616 | |
| 2617 | } |
| 2618 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2619 | |
| 2620 | |
| 2621 | void |
| 2622 | CommandInterpreter::UpdateExecutionContext (ExecutionContext *override_context) |
| 2623 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2624 | if (override_context != NULL) |
| 2625 | { |
Greg Clayton | bcaf99a | 2012-07-12 20:32:19 +0000 | [diff] [blame] | 2626 | m_exe_ctx_ref = *override_context; |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2627 | } |
| 2628 | else |
| 2629 | { |
Greg Clayton | bcaf99a | 2012-07-12 20:32:19 +0000 | [diff] [blame] | 2630 | const bool adopt_selected = true; |
| 2631 | m_exe_ctx_ref.SetTargetPtr (m_debugger.GetSelectedTarget().get(), adopt_selected); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2632 | } |
| 2633 | } |
| 2634 | |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 2635 | void |
| 2636 | CommandInterpreter::DumpHistory (Stream &stream, uint32_t count) const |
| 2637 | { |
| 2638 | DumpHistory (stream, 0, count - 1); |
| 2639 | } |
| 2640 | |
| 2641 | void |
| 2642 | CommandInterpreter::DumpHistory (Stream &stream, uint32_t start, uint32_t end) const |
| 2643 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 2644 | const size_t last_idx = std::min<size_t>(m_command_history.size(), end + 1); |
| 2645 | for (size_t i = start; i < last_idx; i++) |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 2646 | { |
| 2647 | if (!m_command_history[i].empty()) |
| 2648 | { |
| 2649 | stream.Indent(); |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 2650 | stream.Printf ("%4zu: %s\n", i, m_command_history[i].c_str()); |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 2651 | } |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | const char * |
| 2656 | CommandInterpreter::FindHistoryString (const char *input_str) const |
| 2657 | { |
| 2658 | if (input_str[0] != m_repeat_char) |
| 2659 | return NULL; |
| 2660 | if (input_str[1] == '-') |
| 2661 | { |
| 2662 | bool success; |
| 2663 | uint32_t idx = Args::StringToUInt32 (input_str+2, 0, 0, &success); |
| 2664 | if (!success) |
| 2665 | return NULL; |
| 2666 | if (idx > m_command_history.size()) |
| 2667 | return NULL; |
| 2668 | idx = m_command_history.size() - idx; |
| 2669 | return m_command_history[idx].c_str(); |
| 2670 | |
| 2671 | } |
| 2672 | else if (input_str[1] == m_repeat_char) |
| 2673 | { |
| 2674 | if (m_command_history.empty()) |
| 2675 | return NULL; |
| 2676 | else |
| 2677 | return m_command_history.back().c_str(); |
| 2678 | } |
| 2679 | else |
| 2680 | { |
| 2681 | bool success; |
| 2682 | uint32_t idx = Args::StringToUInt32 (input_str+1, 0, 0, &success); |
| 2683 | if (!success) |
| 2684 | return NULL; |
| 2685 | if (idx >= m_command_history.size()) |
| 2686 | return NULL; |
| 2687 | return m_command_history[idx].c_str(); |
| 2688 | } |
| 2689 | } |