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