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