Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 1 | //===-- CommandObjectSource.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 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 12 | #include "CommandObjectCommands.h" |
| 13 | |
| 14 | // C Includes |
| 15 | // C++ Includes |
| 16 | // Other libraries and framework includes |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
| 18 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 19 | // Project includes |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/IOHandler.h" |
Enrico Granata | be93a35 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 22 | #include "lldb/Core/StringList.h" |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 23 | #include "lldb/Interpreter/Args.h" |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 24 | #include "lldb/Interpreter/CommandHistory.h" |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 26 | #include "lldb/Interpreter/CommandObjectRegexCommand.h" |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 27 | #include "lldb/Interpreter/CommandReturnObject.h" |
Enrico Granata | 012d4fc | 2013-06-11 01:26:35 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/OptionValueBoolean.h" |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 29 | #include "lldb/Interpreter/OptionValueUInt64.h" |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 30 | #include "lldb/Interpreter/Options.h" |
Enrico Granata | 99f0b8f | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 31 | #include "lldb/Interpreter/ScriptInterpreter.h" |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace lldb; |
| 34 | using namespace lldb_private; |
| 35 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 36 | //------------------------------------------------------------------------- |
| 37 | // CommandObjectCommandsSource |
| 38 | //------------------------------------------------------------------------- |
| 39 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 40 | class CommandObjectCommandsHistory : public CommandObjectParsed |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 41 | { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 42 | public: |
| 43 | CommandObjectCommandsHistory(CommandInterpreter &interpreter) : |
| 44 | CommandObjectParsed (interpreter, |
| 45 | "command history", |
| 46 | "Dump the history of commands in this session.", |
| 47 | NULL), |
| 48 | m_options (interpreter) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | ~CommandObjectCommandsHistory () {} |
| 53 | |
| 54 | virtual Options * |
| 55 | GetOptions () |
| 56 | { |
| 57 | return &m_options; |
| 58 | } |
| 59 | |
| 60 | protected: |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 61 | |
| 62 | class CommandOptions : public Options |
| 63 | { |
| 64 | public: |
| 65 | |
| 66 | CommandOptions (CommandInterpreter &interpreter) : |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 67 | Options (interpreter), |
| 68 | m_start_idx(0), |
| 69 | m_stop_idx(0), |
| 70 | m_count(0), |
Enrico Granata | 63123b6 | 2013-06-17 23:28:27 +0000 | [diff] [blame] | 71 | m_clear(false) |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 72 | { |
| 73 | } |
| 74 | |
| 75 | virtual |
| 76 | ~CommandOptions (){} |
| 77 | |
| 78 | virtual Error |
| 79 | SetOptionValue (uint32_t option_idx, const char *option_arg) |
| 80 | { |
| 81 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 82 | const int short_option = m_getopt_table[option_idx].val; |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 83 | |
| 84 | switch (short_option) |
| 85 | { |
| 86 | case 'c': |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 87 | error = m_count.SetValueFromString(option_arg,eVarSetOperationAssign); |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 88 | break; |
| 89 | case 's': |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 90 | if (option_arg && strcmp("end", option_arg) == 0) |
| 91 | { |
| 92 | m_start_idx.SetCurrentValue(UINT64_MAX); |
| 93 | m_start_idx.SetOptionWasSet(); |
| 94 | } |
| 95 | else |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 96 | error = m_start_idx.SetValueFromString(option_arg,eVarSetOperationAssign); |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 97 | break; |
| 98 | case 'e': |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 99 | error = m_stop_idx.SetValueFromString(option_arg,eVarSetOperationAssign); |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 100 | break; |
Enrico Granata | 63123b6 | 2013-06-17 23:28:27 +0000 | [diff] [blame] | 101 | case 'C': |
| 102 | m_clear.SetCurrentValue(true); |
| 103 | m_clear.SetOptionWasSet(); |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 104 | break; |
| 105 | default: |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 106 | error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | |
| 110 | return error; |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | OptionParsingStarting () |
| 115 | { |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 116 | m_start_idx.Clear(); |
| 117 | m_stop_idx.Clear(); |
| 118 | m_count.Clear(); |
Enrico Granata | 63123b6 | 2013-06-17 23:28:27 +0000 | [diff] [blame] | 119 | m_clear.Clear(); |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | const OptionDefinition* |
| 123 | GetDefinitions () |
| 124 | { |
| 125 | return g_option_table; |
| 126 | } |
| 127 | |
| 128 | // Options table: Required for subclasses of Options. |
| 129 | |
| 130 | static OptionDefinition g_option_table[]; |
| 131 | |
| 132 | // Instance variables to hold the values for command options. |
| 133 | |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 134 | OptionValueUInt64 m_start_idx; |
| 135 | OptionValueUInt64 m_stop_idx; |
| 136 | OptionValueUInt64 m_count; |
Enrico Granata | 63123b6 | 2013-06-17 23:28:27 +0000 | [diff] [blame] | 137 | OptionValueBoolean m_clear; |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 140 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 141 | DoExecute (Args& command, CommandReturnObject &result) |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 142 | { |
Enrico Granata | 63123b6 | 2013-06-17 23:28:27 +0000 | [diff] [blame] | 143 | if (m_options.m_clear.GetCurrentValue() && m_options.m_clear.OptionWasSet()) |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 144 | { |
| 145 | m_interpreter.GetCommandHistory().Clear(); |
| 146 | result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | if (m_options.m_start_idx.OptionWasSet() && m_options.m_stop_idx.OptionWasSet() && m_options.m_count.OptionWasSet()) |
| 151 | { |
| 152 | result.AppendError("--count, --start-index and --end-index cannot be all specified in the same invocation"); |
| 153 | result.SetStatus(lldb::eReturnStatusFailed); |
| 154 | } |
| 155 | else |
| 156 | { |
Virgile Bello | 84400ec | 2013-08-27 16:22:29 +0000 | [diff] [blame] | 157 | std::pair<bool,uint64_t> start_idx(m_options.m_start_idx.OptionWasSet(),m_options.m_start_idx.GetCurrentValue()); |
| 158 | std::pair<bool,uint64_t> stop_idx(m_options.m_stop_idx.OptionWasSet(),m_options.m_stop_idx.GetCurrentValue()); |
| 159 | std::pair<bool,uint64_t> count(m_options.m_count.OptionWasSet(),m_options.m_count.GetCurrentValue()); |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 160 | |
| 161 | const CommandHistory& history(m_interpreter.GetCommandHistory()); |
| 162 | |
| 163 | if (start_idx.first && start_idx.second == UINT64_MAX) |
| 164 | { |
| 165 | if (count.first) |
| 166 | { |
| 167 | start_idx.second = history.GetSize() - count.second; |
| 168 | stop_idx.second = history.GetSize() - 1; |
| 169 | } |
| 170 | else if (stop_idx.first) |
| 171 | { |
| 172 | start_idx.second = stop_idx.second; |
| 173 | stop_idx.second = history.GetSize() - 1; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | start_idx.second = 0; |
| 178 | stop_idx.second = history.GetSize() - 1; |
| 179 | } |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | if (!start_idx.first && !stop_idx.first && !count.first) |
| 184 | { |
| 185 | start_idx.second = 0; |
| 186 | stop_idx.second = history.GetSize() - 1; |
| 187 | } |
| 188 | else if (start_idx.first) |
| 189 | { |
| 190 | if (count.first) |
| 191 | { |
| 192 | stop_idx.second = start_idx.second + count.second - 1; |
| 193 | } |
| 194 | else if (!stop_idx.first) |
| 195 | { |
| 196 | stop_idx.second = history.GetSize() - 1; |
| 197 | } |
| 198 | } |
| 199 | else if (stop_idx.first) |
| 200 | { |
| 201 | if (count.first) |
| 202 | { |
| 203 | if (stop_idx.second >= count.second) |
| 204 | start_idx.second = stop_idx.second - count.second + 1; |
| 205 | else |
| 206 | start_idx.second = 0; |
| 207 | } |
| 208 | } |
| 209 | else /* if (count.first) */ |
| 210 | { |
| 211 | start_idx.second = 0; |
| 212 | stop_idx.second = count.second - 1; |
| 213 | } |
| 214 | } |
| 215 | history.Dump(result.GetOutputStream(), start_idx.second, stop_idx.second); |
| 216 | } |
| 217 | } |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 218 | return result.Succeeded(); |
| 219 | |
| 220 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 221 | |
| 222 | CommandOptions m_options; |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | OptionDefinition |
| 226 | CommandObjectCommandsHistory::CommandOptions::g_option_table[] = |
| 227 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 228 | { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeUnsignedInteger, "How many history commands to print."}, |
| 229 | { LLDB_OPT_SET_1, false, "start-index", 's', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeUnsignedInteger, "Index at which to start printing history commands (or end to mean tail mode)."}, |
| 230 | { LLDB_OPT_SET_1, false, "end-index", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeUnsignedInteger, "Index at which to stop printing history commands."}, |
| 231 | { LLDB_OPT_SET_2, false, "clear", 'C', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeBoolean, "Clears the current command history."}, |
| 232 | { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL } |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | |
| 236 | //------------------------------------------------------------------------- |
| 237 | // CommandObjectCommandsSource |
| 238 | //------------------------------------------------------------------------- |
| 239 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 240 | class CommandObjectCommandsSource : public CommandObjectParsed |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 241 | { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 242 | public: |
| 243 | CommandObjectCommandsSource(CommandInterpreter &interpreter) : |
| 244 | CommandObjectParsed (interpreter, |
| 245 | "command source", |
| 246 | "Read in debugger commands from the file <filename> and execute them.", |
| 247 | NULL), |
| 248 | m_options (interpreter) |
| 249 | { |
| 250 | CommandArgumentEntry arg; |
| 251 | CommandArgumentData file_arg; |
| 252 | |
| 253 | // Define the first (and only) variant of this arg. |
| 254 | file_arg.arg_type = eArgTypeFilename; |
| 255 | file_arg.arg_repetition = eArgRepeatPlain; |
| 256 | |
| 257 | // There is only one variant this argument could be; put it into the argument entry. |
| 258 | arg.push_back (file_arg); |
| 259 | |
| 260 | // Push the data for the first argument into the m_arguments vector. |
| 261 | m_arguments.push_back (arg); |
| 262 | } |
| 263 | |
| 264 | ~CommandObjectCommandsSource () {} |
| 265 | |
| 266 | virtual const char* |
| 267 | GetRepeatCommand (Args ¤t_command_args, uint32_t index) |
| 268 | { |
| 269 | return ""; |
| 270 | } |
| 271 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 272 | virtual int |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 273 | HandleArgumentCompletion (Args &input, |
| 274 | int &cursor_index, |
| 275 | int &cursor_char_position, |
| 276 | OptionElementVector &opt_element_vector, |
| 277 | int match_start_point, |
| 278 | int max_return_elements, |
| 279 | bool &word_complete, |
| 280 | StringList &matches) |
| 281 | { |
| 282 | std::string completion_str (input.GetArgumentAtIndex(cursor_index)); |
| 283 | completion_str.erase (cursor_char_position); |
| 284 | |
| 285 | CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, |
| 286 | CommandCompletions::eDiskFileCompletion, |
| 287 | completion_str.c_str(), |
| 288 | match_start_point, |
| 289 | max_return_elements, |
| 290 | NULL, |
| 291 | word_complete, |
| 292 | matches); |
| 293 | return matches.GetSize(); |
| 294 | } |
| 295 | |
| 296 | virtual Options * |
| 297 | GetOptions () |
| 298 | { |
| 299 | return &m_options; |
| 300 | } |
| 301 | |
| 302 | protected: |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 303 | |
| 304 | class CommandOptions : public Options |
| 305 | { |
| 306 | public: |
| 307 | |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 308 | CommandOptions (CommandInterpreter &interpreter) : |
Enrico Granata | 012d4fc | 2013-06-11 01:26:35 +0000 | [diff] [blame] | 309 | Options (interpreter), |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 310 | m_stop_on_error (true), |
| 311 | m_silent_run (false), |
| 312 | m_stop_on_continue (true) |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 313 | { |
| 314 | } |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 315 | |
| 316 | virtual |
| 317 | ~CommandOptions (){} |
| 318 | |
| 319 | virtual Error |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 320 | SetOptionValue (uint32_t option_idx, const char *option_arg) |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 321 | { |
| 322 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 323 | const int short_option = m_getopt_table[option_idx].val; |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 324 | |
| 325 | switch (short_option) |
| 326 | { |
| 327 | case 'e': |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 328 | error = m_stop_on_error.SetValueFromString(option_arg); |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 329 | break; |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 330 | |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 331 | case 'c': |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 332 | error = m_stop_on_continue.SetValueFromString(option_arg); |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 333 | break; |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 334 | |
Michael Sartain | 6098617 | 2013-07-09 23:22:53 +0000 | [diff] [blame] | 335 | case 's': |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 336 | error = m_silent_run.SetValueFromString(option_arg); |
Michael Sartain | 6098617 | 2013-07-09 23:22:53 +0000 | [diff] [blame] | 337 | break; |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 338 | |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 339 | default: |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 340 | error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 341 | break; |
| 342 | } |
| 343 | |
| 344 | return error; |
| 345 | } |
| 346 | |
| 347 | void |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 348 | OptionParsingStarting () |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 349 | { |
Enrico Granata | 012d4fc | 2013-06-11 01:26:35 +0000 | [diff] [blame] | 350 | m_stop_on_error.Clear(); |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 351 | m_silent_run.Clear(); |
| 352 | m_stop_on_continue.Clear(); |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 355 | const OptionDefinition* |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 356 | GetDefinitions () |
| 357 | { |
| 358 | return g_option_table; |
| 359 | } |
| 360 | |
| 361 | // Options table: Required for subclasses of Options. |
| 362 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 363 | static OptionDefinition g_option_table[]; |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 364 | |
| 365 | // Instance variables to hold the values for command options. |
| 366 | |
Enrico Granata | 012d4fc | 2013-06-11 01:26:35 +0000 | [diff] [blame] | 367 | OptionValueBoolean m_stop_on_error; |
Jim Ingham | 7d8555c | 2014-11-18 19:12:13 +0000 | [diff] [blame] | 368 | OptionValueBoolean m_silent_run; |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 369 | OptionValueBoolean m_stop_on_continue; |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 372 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 373 | DoExecute(Args& command, CommandReturnObject &result) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 374 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 375 | const size_t argc = command.GetArgumentCount(); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 376 | if (argc == 1) |
| 377 | { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 378 | const char *filename = command.GetArgumentAtIndex(0); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 379 | |
Johnny Chen | 1ee3853 | 2010-10-20 21:40:50 +0000 | [diff] [blame] | 380 | FileSpec cmd_file (filename, true); |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 381 | ExecutionContext *exe_ctx = NULL; // Just use the default context. |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 382 | |
| 383 | // If any options were set, then use them |
| 384 | if (m_options.m_stop_on_error.OptionWasSet() || |
| 385 | m_options.m_silent_run.OptionWasSet() || |
| 386 | m_options.m_stop_on_continue.OptionWasSet()) |
| 387 | { |
| 388 | // Use user set settings |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 389 | CommandInterpreterRunOptions options; |
| 390 | options.SetStopOnContinue(m_options.m_stop_on_continue.GetCurrentValue()); |
| 391 | options.SetStopOnError (m_options.m_stop_on_error.GetCurrentValue()); |
Jim Ingham | 7d8555c | 2014-11-18 19:12:13 +0000 | [diff] [blame] | 392 | options.SetEchoCommands (!m_options.m_silent_run.GetCurrentValue()); |
| 393 | options.SetPrintResults (!m_options.m_silent_run.GetCurrentValue()); |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 394 | |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 395 | m_interpreter.HandleCommandsFromFile (cmd_file, |
| 396 | exe_ctx, |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 397 | options, |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 398 | result); |
| 399 | |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | // No options were set, inherit any settings from nested "command source" commands, |
| 404 | // or set to sane default settings... |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 405 | CommandInterpreterRunOptions options; |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 406 | m_interpreter.HandleCommandsFromFile (cmd_file, |
| 407 | exe_ctx, |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 408 | options, |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 409 | result); |
| 410 | |
| 411 | } |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 412 | } |
| 413 | else |
| 414 | { |
| 415 | result.AppendErrorWithFormat("'%s' takes exactly one executable filename argument.\n", GetCommandName()); |
| 416 | result.SetStatus (eReturnStatusFailed); |
| 417 | } |
| 418 | return result.Succeeded(); |
| 419 | |
| 420 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 421 | CommandOptions m_options; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 422 | }; |
| 423 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 424 | OptionDefinition |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 425 | CommandObjectCommandsSource::CommandOptions::g_option_table[] = |
| 426 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 427 | { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "If true, stop executing commands on error."}, |
| 428 | { LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "If true, stop executing commands on continue."}, |
| 429 | { LLDB_OPT_SET_ALL, false, "silent-run", 's', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "If true don't echo commands while executing."}, |
| 430 | { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL } |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 431 | }; |
| 432 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 433 | #pragma mark CommandObjectCommandsAlias |
| 434 | //------------------------------------------------------------------------- |
| 435 | // CommandObjectCommandsAlias |
| 436 | //------------------------------------------------------------------------- |
| 437 | |
Enrico Granata | be93a35 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 438 | static const char *g_python_command_instructions = "Enter your Python command(s). Type 'DONE' to end.\n" |
| 439 | "You must define a Python function with this signature:\n" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 440 | "def my_command_impl(debugger, args, result, internal_dict):\n"; |
Enrico Granata | be93a35 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 441 | |
| 442 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 443 | class CommandObjectCommandsAlias : public CommandObjectRaw |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 444 | { |
Enrico Granata | be93a35 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 445 | |
Enrico Granata | be93a35 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 446 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 447 | public: |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 448 | CommandObjectCommandsAlias (CommandInterpreter &interpreter) : |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 449 | CommandObjectRaw (interpreter, |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 450 | "command alias", |
Caroline Tice | e3d2631 | 2010-09-08 21:06:11 +0000 | [diff] [blame] | 451 | "Allow users to define their own debugger command abbreviations.", |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 452 | NULL) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 453 | { |
| 454 | SetHelpLong( |
| 455 | "'alias' allows the user to create a short-cut or abbreviation for long \n\ |
| 456 | commands, multi-word commands, and commands that take particular options. \n\ |
| 457 | Below are some simple examples of how one might use the 'alias' command: \n\ |
Jason Molenda | 69c12cc | 2011-11-10 22:43:35 +0000 | [diff] [blame] | 458 | \n 'command alias sc script' // Creates the abbreviation 'sc' for the 'script' \n\ |
Caroline Tice | 09799af | 2010-09-08 22:08:58 +0000 | [diff] [blame] | 459 | // command. \n\ |
Jason Molenda | 69c12cc | 2011-11-10 22:43:35 +0000 | [diff] [blame] | 460 | 'command alias bp breakpoint' // Creates the abbreviation 'bp' for the 'breakpoint' \n\ |
Caroline Tice | 09799af | 2010-09-08 22:08:58 +0000 | [diff] [blame] | 461 | // command. Since breakpoint commands are two-word \n\ |
| 462 | // commands, the user will still need to enter the \n\ |
| 463 | // second word after 'bp', e.g. 'bp enable' or \n\ |
| 464 | // 'bp delete'. \n\ |
Jason Molenda | 69c12cc | 2011-11-10 22:43:35 +0000 | [diff] [blame] | 465 | 'command alias bpl breakpoint list' // Creates the abbreviation 'bpl' for the \n\ |
Caroline Tice | 09799af | 2010-09-08 22:08:58 +0000 | [diff] [blame] | 466 | // two-word command 'breakpoint list'. \n\ |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 467 | \nAn alias can include some options for the command, with the values either \n\ |
| 468 | filled in at the time the alias is created, or specified as positional \n\ |
| 469 | arguments, to be filled in when the alias is invoked. The following example \n\ |
| 470 | shows how to create aliases with options: \n\ |
| 471 | \n\ |
Jason Molenda | 69c12cc | 2011-11-10 22:43:35 +0000 | [diff] [blame] | 472 | 'command alias bfl breakpoint set -f %1 -l %2' \n\ |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 473 | \nThis creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \n\ |
| 474 | options already part of the alias. So if the user wants to set a breakpoint \n\ |
| 475 | by file and line without explicitly having to use the -f and -l options, the \n\ |
| 476 | user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \n\ |
| 477 | for the actual arguments that will be passed when the alias command is used. \n\ |
| 478 | The number in the placeholder refers to the position/order the actual value \n\ |
Jim Ingham | 81ded93 | 2011-08-18 02:29:05 +0000 | [diff] [blame] | 479 | occupies when the alias is used. All the occurrences of '%1' in the alias \n\ |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 480 | will be replaced with the first argument, all the occurrences of '%2' in the \n\ |
| 481 | alias will be replaced with the second argument, and so on. This also allows \n\ |
| 482 | actual arguments to be used multiple times within an alias (see 'process \n\ |
Jim Ingham | 81ded93 | 2011-08-18 02:29:05 +0000 | [diff] [blame] | 483 | launch' example below). \n\ |
| 484 | Note: the positional arguments must substitute as whole words in the resultant\n\ |
| 485 | command, so you can't at present do something like:\n\ |
| 486 | \n\ |
Jason Molenda | 69c12cc | 2011-11-10 22:43:35 +0000 | [diff] [blame] | 487 | command alias bcppfl breakpoint set -f %1.cpp -l %2\n\ |
Jim Ingham | 81ded93 | 2011-08-18 02:29:05 +0000 | [diff] [blame] | 488 | \n\ |
| 489 | to get the file extension \".cpp\" automatically appended. For more complex\n\ |
| 490 | aliasing, use the \"command regex\" command instead.\n\ |
| 491 | \nSo in the 'bfl' case, the actual file value will be \n\ |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 492 | filled in with the first argument following 'bfl' and the actual line number \n\ |
| 493 | value will be filled in with the second argument. The user would use this \n\ |
| 494 | alias as follows: \n\ |
Jason Molenda | 69c12cc | 2011-11-10 22:43:35 +0000 | [diff] [blame] | 495 | \n (lldb) command alias bfl breakpoint set -f %1 -l %2 \n\ |
Sean Callanan | 0708e2c | 2010-08-09 18:50:15 +0000 | [diff] [blame] | 496 | <... some time later ...> \n\ |
Caroline Tice | 09799af | 2010-09-08 22:08:58 +0000 | [diff] [blame] | 497 | (lldb) bfl my-file.c 137 \n\ |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 498 | \nThis would be the same as if the user had entered \n\ |
| 499 | 'breakpoint set -f my-file.c -l 137'. \n\ |
| 500 | \nAnother example: \n\ |
Jason Molenda | 69c12cc | 2011-11-10 22:43:35 +0000 | [diff] [blame] | 501 | \n (lldb) command alias pltty process launch -s -o %1 -e %1 \n\ |
Caroline Tice | 09799af | 2010-09-08 22:08:58 +0000 | [diff] [blame] | 502 | (lldb) pltty /dev/tty0 \n\ |
Sean Callanan | 0708e2c | 2010-08-09 18:50:15 +0000 | [diff] [blame] | 503 | // becomes 'process launch -s -o /dev/tty0 -e /dev/tty0' \n\ |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 504 | \nIf the user always wanted to pass the same value to a particular option, the \n\ |
| 505 | alias could be defined with that value directly in the alias as a constant, \n\ |
| 506 | rather than using a positional placeholder: \n\ |
Jason Molenda | 69c12cc | 2011-11-10 22:43:35 +0000 | [diff] [blame] | 507 | \n command alias bl3 breakpoint set -f %1 -l 3 // Always sets a breakpoint on line \n\ |
Sean Callanan | 0708e2c | 2010-08-09 18:50:15 +0000 | [diff] [blame] | 508 | // 3 of whatever file is indicated. \n"); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 509 | |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 510 | CommandArgumentEntry arg1; |
| 511 | CommandArgumentEntry arg2; |
| 512 | CommandArgumentEntry arg3; |
| 513 | CommandArgumentData alias_arg; |
| 514 | CommandArgumentData cmd_arg; |
| 515 | CommandArgumentData options_arg; |
| 516 | |
| 517 | // Define the first (and only) variant of this arg. |
| 518 | alias_arg.arg_type = eArgTypeAliasName; |
| 519 | alias_arg.arg_repetition = eArgRepeatPlain; |
| 520 | |
| 521 | // There is only one variant this argument could be; put it into the argument entry. |
| 522 | arg1.push_back (alias_arg); |
| 523 | |
| 524 | // Define the first (and only) variant of this arg. |
| 525 | cmd_arg.arg_type = eArgTypeCommandName; |
| 526 | cmd_arg.arg_repetition = eArgRepeatPlain; |
| 527 | |
| 528 | // There is only one variant this argument could be; put it into the argument entry. |
| 529 | arg2.push_back (cmd_arg); |
| 530 | |
| 531 | // Define the first (and only) variant of this arg. |
| 532 | options_arg.arg_type = eArgTypeAliasOptions; |
| 533 | options_arg.arg_repetition = eArgRepeatOptional; |
| 534 | |
| 535 | // There is only one variant this argument could be; put it into the argument entry. |
| 536 | arg3.push_back (options_arg); |
| 537 | |
| 538 | // Push the data for the first argument into the m_arguments vector. |
| 539 | m_arguments.push_back (arg1); |
| 540 | m_arguments.push_back (arg2); |
| 541 | m_arguments.push_back (arg3); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | ~CommandObjectCommandsAlias () |
| 545 | { |
| 546 | } |
| 547 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 548 | protected: |
| 549 | virtual bool |
| 550 | DoExecute (const char *raw_command_line, CommandReturnObject &result) |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 551 | { |
| 552 | Args args (raw_command_line); |
| 553 | std::string raw_command_string (raw_command_line); |
| 554 | |
| 555 | size_t argc = args.GetArgumentCount(); |
| 556 | |
| 557 | if (argc < 2) |
| 558 | { |
| 559 | result.AppendError ("'alias' requires at least two arguments"); |
| 560 | result.SetStatus (eReturnStatusFailed); |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | // Get the alias command. |
| 565 | |
| 566 | const std::string alias_command = args.GetArgumentAtIndex (0); |
Enrico Granata | be93a35 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 567 | |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 568 | // Strip the new alias name off 'raw_command_string' (leave it on args, which gets passed to 'Execute', which |
| 569 | // does the stripping itself. |
| 570 | size_t pos = raw_command_string.find (alias_command); |
| 571 | if (pos == 0) |
| 572 | { |
| 573 | raw_command_string = raw_command_string.substr (alias_command.size()); |
| 574 | pos = raw_command_string.find_first_not_of (' '); |
| 575 | if ((pos != std::string::npos) && (pos > 0)) |
| 576 | raw_command_string = raw_command_string.substr (pos); |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | result.AppendError ("Error parsing command string. No alias created."); |
| 581 | result.SetStatus (eReturnStatusFailed); |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | // Verify that the command is alias-able. |
| 587 | if (m_interpreter.CommandExists (alias_command.c_str())) |
| 588 | { |
| 589 | result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n", |
| 590 | alias_command.c_str()); |
| 591 | result.SetStatus (eReturnStatusFailed); |
| 592 | return false; |
| 593 | } |
| 594 | |
| 595 | // Get CommandObject that is being aliased. The command name is read from the front of raw_command_string. |
| 596 | // raw_command_string is returned with the name of the command object stripped off the front. |
| 597 | CommandObject *cmd_obj = m_interpreter.GetCommandObjectForCommand (raw_command_string); |
| 598 | |
| 599 | if (!cmd_obj) |
| 600 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 601 | result.AppendErrorWithFormat ("invalid command given to 'alias'. '%s' does not begin with a valid command." |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 602 | " No alias created.", raw_command_string.c_str()); |
| 603 | result.SetStatus (eReturnStatusFailed); |
| 604 | return false; |
| 605 | } |
| 606 | else if (!cmd_obj->WantsRawCommandString ()) |
| 607 | { |
| 608 | // Note that args was initialized with the original command, and has not been updated to this point. |
| 609 | // Therefore can we pass it to the version of Execute that does not need/expect raw input in the alias. |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 610 | return HandleAliasingNormalCommand (args, result); |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 611 | } |
| 612 | else |
| 613 | { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 614 | return HandleAliasingRawCommand (alias_command, raw_command_string, *cmd_obj, result); |
| 615 | } |
| 616 | return result.Succeeded(); |
| 617 | } |
| 618 | |
| 619 | bool |
| 620 | HandleAliasingRawCommand (const std::string &alias_command, std::string &raw_command_string, CommandObject &cmd_obj, CommandReturnObject &result) |
| 621 | { |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 622 | // Verify & handle any options/arguments passed to the alias command |
| 623 | |
| 624 | OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector); |
| 625 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 626 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 627 | CommandObjectSP cmd_obj_sp = m_interpreter.GetCommandSPExact (cmd_obj.GetCommandName(), false); |
Caroline Tice | ca90c47 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 628 | |
| 629 | if (!m_interpreter.ProcessAliasOptionsArgs (cmd_obj_sp, raw_command_string.c_str(), option_arg_vector_sp)) |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 630 | { |
Caroline Tice | ca90c47 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 631 | result.AppendError ("Unable to create requested alias.\n"); |
| 632 | result.SetStatus (eReturnStatusFailed); |
| 633 | return false; |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // Create the alias |
| 637 | if (m_interpreter.AliasExists (alias_command.c_str()) |
| 638 | || m_interpreter.UserCommandExists (alias_command.c_str())) |
| 639 | { |
| 640 | OptionArgVectorSP temp_option_arg_sp (m_interpreter.GetAliasOptions (alias_command.c_str())); |
| 641 | if (temp_option_arg_sp.get()) |
| 642 | { |
| 643 | if (option_arg_vector->size() == 0) |
| 644 | m_interpreter.RemoveAliasOptions (alias_command.c_str()); |
| 645 | } |
| 646 | result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n", |
| 647 | alias_command.c_str()); |
| 648 | } |
| 649 | |
Caroline Tice | 472362e | 2010-12-14 18:51:39 +0000 | [diff] [blame] | 650 | if (cmd_obj_sp) |
| 651 | { |
| 652 | m_interpreter.AddAlias (alias_command.c_str(), cmd_obj_sp); |
| 653 | if (option_arg_vector->size() > 0) |
| 654 | m_interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp); |
| 655 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 656 | } |
| 657 | else |
| 658 | { |
| 659 | result.AppendError ("Unable to create requested alias.\n"); |
| 660 | result.SetStatus (eReturnStatusFailed); |
| 661 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 662 | return result.Succeeded (); |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 663 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 664 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 665 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 666 | HandleAliasingNormalCommand (Args& args, CommandReturnObject &result) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 667 | { |
Caroline Tice | 867b185d | 2010-09-21 23:25:40 +0000 | [diff] [blame] | 668 | size_t argc = args.GetArgumentCount(); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 669 | |
| 670 | if (argc < 2) |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 671 | { |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 672 | result.AppendError ("'alias' requires at least two arguments"); |
| 673 | result.SetStatus (eReturnStatusFailed); |
| 674 | return false; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 675 | } |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 676 | |
| 677 | const std::string alias_command = args.GetArgumentAtIndex(0); |
| 678 | const std::string actual_command = args.GetArgumentAtIndex(1); |
| 679 | |
| 680 | args.Shift(); // Shift the alias command word off the argument vector. |
| 681 | args.Shift(); // Shift the old command word off the argument vector. |
| 682 | |
| 683 | // Verify that the command is alias'able, and get the appropriate command object. |
| 684 | |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 685 | if (m_interpreter.CommandExists (alias_command.c_str())) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 686 | { |
| 687 | result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n", |
| 688 | alias_command.c_str()); |
| 689 | result.SetStatus (eReturnStatusFailed); |
| 690 | } |
| 691 | else |
| 692 | { |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 693 | CommandObjectSP command_obj_sp(m_interpreter.GetCommandSPExact (actual_command.c_str(), true)); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 694 | CommandObjectSP subcommand_obj_sp; |
| 695 | bool use_subcommand = false; |
| 696 | if (command_obj_sp.get()) |
| 697 | { |
| 698 | CommandObject *cmd_obj = command_obj_sp.get(); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 699 | CommandObject *sub_cmd_obj = NULL; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 700 | OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector); |
| 701 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 702 | |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 703 | while (cmd_obj->IsMultiwordObject() && args.GetArgumentCount() > 0) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 704 | { |
| 705 | if (argc >= 3) |
| 706 | { |
| 707 | const std::string sub_command = args.GetArgumentAtIndex(0); |
| 708 | assert (sub_command.length() != 0); |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 709 | subcommand_obj_sp = cmd_obj->GetSubcommandSP (sub_command.c_str()); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 710 | if (subcommand_obj_sp.get()) |
| 711 | { |
| 712 | sub_cmd_obj = subcommand_obj_sp.get(); |
| 713 | use_subcommand = true; |
| 714 | args.Shift(); // Shift the sub_command word off the argument vector. |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 715 | cmd_obj = sub_cmd_obj; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 716 | } |
| 717 | else |
| 718 | { |
Caroline Tice | f415eeb | 2010-11-02 19:00:04 +0000 | [diff] [blame] | 719 | result.AppendErrorWithFormat("'%s' is not a valid sub-command of '%s'. " |
| 720 | "Unable to create alias.\n", |
| 721 | sub_command.c_str(), actual_command.c_str()); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 722 | result.SetStatus (eReturnStatusFailed); |
| 723 | return false; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | // Verify & handle any options/arguments passed to the alias command |
| 729 | |
| 730 | if (args.GetArgumentCount () > 0) |
| 731 | { |
Caroline Tice | ca90c47 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 732 | CommandObjectSP tmp_sp = m_interpreter.GetCommandSPExact (cmd_obj->GetCommandName(), false); |
| 733 | if (use_subcommand) |
| 734 | tmp_sp = m_interpreter.GetCommandSPExact (sub_cmd_obj->GetCommandName(), false); |
| 735 | |
| 736 | std::string args_string; |
| 737 | args.GetCommandString (args_string); |
| 738 | |
| 739 | if (!m_interpreter.ProcessAliasOptionsArgs (tmp_sp, args_string.c_str(), option_arg_vector_sp)) |
| 740 | { |
| 741 | result.AppendError ("Unable to create requested alias.\n"); |
| 742 | result.SetStatus (eReturnStatusFailed); |
| 743 | return false; |
| 744 | } |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | // Create the alias. |
| 748 | |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 749 | if (m_interpreter.AliasExists (alias_command.c_str()) |
| 750 | || m_interpreter.UserCommandExists (alias_command.c_str())) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 751 | { |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 752 | OptionArgVectorSP tmp_option_arg_sp (m_interpreter.GetAliasOptions (alias_command.c_str())); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 753 | if (tmp_option_arg_sp.get()) |
| 754 | { |
| 755 | if (option_arg_vector->size() == 0) |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 756 | m_interpreter.RemoveAliasOptions (alias_command.c_str()); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 757 | } |
| 758 | result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n", |
| 759 | alias_command.c_str()); |
| 760 | } |
| 761 | |
| 762 | if (use_subcommand) |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 763 | m_interpreter.AddAlias (alias_command.c_str(), subcommand_obj_sp); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 764 | else |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 765 | m_interpreter.AddAlias (alias_command.c_str(), command_obj_sp); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 766 | if (option_arg_vector->size() > 0) |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 767 | m_interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 768 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 769 | } |
| 770 | else |
| 771 | { |
| 772 | result.AppendErrorWithFormat ("'%s' is not an existing command.\n", actual_command.c_str()); |
| 773 | result.SetStatus (eReturnStatusFailed); |
Caroline Tice | e794179 | 2010-10-28 23:17:48 +0000 | [diff] [blame] | 774 | return false; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
| 778 | return result.Succeeded(); |
| 779 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 780 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 781 | }; |
| 782 | |
| 783 | #pragma mark CommandObjectCommandsUnalias |
| 784 | //------------------------------------------------------------------------- |
| 785 | // CommandObjectCommandsUnalias |
| 786 | //------------------------------------------------------------------------- |
| 787 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 788 | class CommandObjectCommandsUnalias : public CommandObjectParsed |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 789 | { |
| 790 | public: |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 791 | CommandObjectCommandsUnalias (CommandInterpreter &interpreter) : |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 792 | CommandObjectParsed (interpreter, |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 793 | "command unalias", |
Caroline Tice | 86ddae5 | 2010-09-12 04:56:10 +0000 | [diff] [blame] | 794 | "Allow the user to remove/delete a user-defined command abbreviation.", |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 795 | NULL) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 796 | { |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 797 | CommandArgumentEntry arg; |
| 798 | CommandArgumentData alias_arg; |
| 799 | |
| 800 | // Define the first (and only) variant of this arg. |
| 801 | alias_arg.arg_type = eArgTypeAliasName; |
| 802 | alias_arg.arg_repetition = eArgRepeatPlain; |
| 803 | |
| 804 | // There is only one variant this argument could be; put it into the argument entry. |
| 805 | arg.push_back (alias_arg); |
| 806 | |
| 807 | // Push the data for the first argument into the m_arguments vector. |
| 808 | m_arguments.push_back (arg); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | ~CommandObjectCommandsUnalias() |
| 812 | { |
| 813 | } |
| 814 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 815 | protected: |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 816 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 817 | DoExecute (Args& args, CommandReturnObject &result) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 818 | { |
| 819 | CommandObject::CommandMap::iterator pos; |
| 820 | CommandObject *cmd_obj; |
| 821 | |
| 822 | if (args.GetArgumentCount() != 0) |
| 823 | { |
| 824 | const char *command_name = args.GetArgumentAtIndex(0); |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 825 | cmd_obj = m_interpreter.GetCommandObject(command_name); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 826 | if (cmd_obj) |
| 827 | { |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 828 | if (m_interpreter.CommandExists (command_name)) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 829 | { |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 830 | if (cmd_obj->IsRemovable()) |
| 831 | { |
| 832 | result.AppendErrorWithFormat ("'%s' is not an alias, it is a debugger command which can be removed using the 'command delete' command.\n", |
| 833 | command_name); |
| 834 | } |
| 835 | else |
| 836 | { |
| 837 | result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be removed.\n", |
| 838 | command_name); |
| 839 | } |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 840 | result.SetStatus (eReturnStatusFailed); |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 845 | if (m_interpreter.RemoveAlias (command_name) == false) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 846 | { |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 847 | if (m_interpreter.AliasExists (command_name)) |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 848 | result.AppendErrorWithFormat ("Error occurred while attempting to unalias '%s'.\n", |
| 849 | command_name); |
| 850 | else |
| 851 | result.AppendErrorWithFormat ("'%s' is not an existing alias.\n", command_name); |
| 852 | result.SetStatus (eReturnStatusFailed); |
| 853 | } |
| 854 | else |
| 855 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 856 | } |
| 857 | } |
| 858 | else |
| 859 | { |
| 860 | result.AppendErrorWithFormat ("'%s' is not a known command.\nTry 'help' to see a " |
| 861 | "current list of commands.\n", |
| 862 | command_name); |
| 863 | result.SetStatus (eReturnStatusFailed); |
| 864 | } |
| 865 | } |
| 866 | else |
| 867 | { |
| 868 | result.AppendError ("must call 'unalias' with a valid alias"); |
| 869 | result.SetStatus (eReturnStatusFailed); |
| 870 | } |
| 871 | |
| 872 | return result.Succeeded(); |
| 873 | } |
| 874 | }; |
| 875 | |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 876 | #pragma mark CommandObjectCommandsDelete |
| 877 | //------------------------------------------------------------------------- |
| 878 | // CommandObjectCommandsDelete |
| 879 | //------------------------------------------------------------------------- |
| 880 | |
| 881 | class CommandObjectCommandsDelete : public CommandObjectParsed |
| 882 | { |
| 883 | public: |
| 884 | CommandObjectCommandsDelete (CommandInterpreter &interpreter) : |
| 885 | CommandObjectParsed (interpreter, |
| 886 | "command delete", |
| 887 | "Allow the user to delete user-defined regular expression, python or multi-word commands.", |
| 888 | NULL) |
| 889 | { |
| 890 | CommandArgumentEntry arg; |
| 891 | CommandArgumentData alias_arg; |
| 892 | |
| 893 | // Define the first (and only) variant of this arg. |
| 894 | alias_arg.arg_type = eArgTypeCommandName; |
| 895 | alias_arg.arg_repetition = eArgRepeatPlain; |
| 896 | |
| 897 | // There is only one variant this argument could be; put it into the argument entry. |
| 898 | arg.push_back (alias_arg); |
| 899 | |
| 900 | // Push the data for the first argument into the m_arguments vector. |
| 901 | m_arguments.push_back (arg); |
| 902 | } |
| 903 | |
| 904 | ~CommandObjectCommandsDelete() |
| 905 | { |
| 906 | } |
| 907 | |
| 908 | protected: |
| 909 | bool |
| 910 | DoExecute (Args& args, CommandReturnObject &result) |
| 911 | { |
| 912 | CommandObject::CommandMap::iterator pos; |
| 913 | |
| 914 | if (args.GetArgumentCount() != 0) |
| 915 | { |
| 916 | const char *command_name = args.GetArgumentAtIndex(0); |
| 917 | if (m_interpreter.CommandExists (command_name)) |
| 918 | { |
| 919 | if (m_interpreter.RemoveCommand (command_name)) |
| 920 | { |
| 921 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 922 | } |
| 923 | else |
| 924 | { |
| 925 | result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be removed.\n", |
| 926 | command_name); |
| 927 | result.SetStatus (eReturnStatusFailed); |
| 928 | } |
| 929 | } |
| 930 | else |
| 931 | { |
| 932 | result.AppendErrorWithFormat ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", |
| 933 | command_name); |
| 934 | result.SetStatus (eReturnStatusFailed); |
| 935 | } |
| 936 | } |
| 937 | else |
| 938 | { |
| 939 | result.AppendErrorWithFormat ("must call '%s' with one or more valid user defined regular expression, python or multi-word command names", GetCommandName ()); |
| 940 | result.SetStatus (eReturnStatusFailed); |
| 941 | } |
| 942 | |
| 943 | return result.Succeeded(); |
| 944 | } |
| 945 | }; |
| 946 | |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 947 | //------------------------------------------------------------------------- |
| 948 | // CommandObjectCommandsAddRegex |
| 949 | //------------------------------------------------------------------------- |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 950 | #pragma mark CommandObjectCommandsAddRegex |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 951 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 952 | class CommandObjectCommandsAddRegex : |
| 953 | public CommandObjectParsed, |
Greg Clayton | ea50863 | 2014-11-18 00:43:17 +0000 | [diff] [blame] | 954 | public IOHandlerDelegateMultiline |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 955 | { |
| 956 | public: |
| 957 | CommandObjectCommandsAddRegex (CommandInterpreter &interpreter) : |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 958 | CommandObjectParsed (interpreter, |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 959 | "command regex", |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 960 | "Allow the user to create a regular expression command.", |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 961 | "command regex <cmd-name> [s/<regex>/<subst>/ ...]"), |
Greg Clayton | ea50863 | 2014-11-18 00:43:17 +0000 | [diff] [blame] | 962 | IOHandlerDelegateMultiline ("", IOHandlerDelegate::Completion::LLDBCommand), |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 963 | m_options (interpreter) |
| 964 | { |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 965 | SetHelpLong( |
| 966 | "This command allows the user to create powerful regular expression commands\n" |
| 967 | "with substitutions. The regular expressions and substitutions are specified\n" |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 968 | "using the regular expression substitution format of:\n" |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 969 | "\n" |
| 970 | " s/<regex>/<subst>/\n" |
| 971 | "\n" |
| 972 | "<regex> is a regular expression that can use parenthesis to capture regular\n" |
| 973 | "expression input and substitute the captured matches in the output using %1\n" |
| 974 | "for the first match, %2 for the second, and so on.\n" |
| 975 | "\n" |
| 976 | "The regular expressions can all be specified on the command line if more than\n" |
| 977 | "one argument is provided. If just the command name is provided on the command\n" |
| 978 | "line, then the regular expressions and substitutions can be entered on separate\n" |
| 979 | " lines, followed by an empty line to terminate the command definition.\n" |
| 980 | "\n" |
| 981 | "EXAMPLES\n" |
| 982 | "\n" |
Sean Callanan | adc43c9 | 2012-08-16 21:46:58 +0000 | [diff] [blame] | 983 | "The following example will define a regular expression command named 'f' that\n" |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 984 | "will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if\n" |
| 985 | "a number follows 'f':\n" |
Sean Callanan | adc43c9 | 2012-08-16 21:46:58 +0000 | [diff] [blame] | 986 | "\n" |
| 987 | " (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/'\n" |
| 988 | "\n" |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 989 | ); |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | ~CommandObjectCommandsAddRegex() |
| 993 | { |
| 994 | } |
| 995 | |
| 996 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 997 | protected: |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 998 | |
Greg Clayton | ea50863 | 2014-11-18 00:43:17 +0000 | [diff] [blame] | 999 | void |
| 1000 | IOHandlerActivated (IOHandler &io_handler) override |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1001 | { |
| 1002 | StreamFileSP output_sp(io_handler.GetOutputStreamFile()); |
| 1003 | if (output_sp) |
| 1004 | { |
| 1005 | output_sp->PutCString("Enter one of more sed substitution commands in the form: 's/<regex>/<subst>/'.\nTerminate the substitution list with an empty line.\n"); |
| 1006 | output_sp->Flush(); |
| 1007 | } |
| 1008 | } |
| 1009 | |
Greg Clayton | ea50863 | 2014-11-18 00:43:17 +0000 | [diff] [blame] | 1010 | void |
| 1011 | IOHandlerInputComplete (IOHandler &io_handler, std::string &data) override |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1012 | { |
| 1013 | io_handler.SetIsDone(true); |
| 1014 | if (m_regex_cmd_ap.get()) |
| 1015 | { |
| 1016 | StringList lines; |
| 1017 | if (lines.SplitIntoLines (data)) |
| 1018 | { |
| 1019 | const size_t num_lines = lines.GetSize(); |
| 1020 | bool check_only = false; |
| 1021 | for (size_t i=0; i<num_lines; ++i) |
| 1022 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1023 | llvm::StringRef bytes_strref (lines[i]); |
| 1024 | Error error = AppendRegexSubstitution (bytes_strref, check_only); |
| 1025 | if (error.Fail()) |
| 1026 | { |
| 1027 | if (!m_interpreter.GetDebugger().GetCommandInterpreter().GetBatchCommandMode()) |
| 1028 | { |
| 1029 | StreamSP out_stream = m_interpreter.GetDebugger().GetAsyncOutputStream(); |
| 1030 | out_stream->Printf("error: %s\n", error.AsCString()); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | if (m_regex_cmd_ap->HasRegexEntries()) |
| 1036 | { |
| 1037 | CommandObjectSP cmd_sp (m_regex_cmd_ap.release()); |
| 1038 | m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true); |
| 1039 | } |
| 1040 | } |
| 1041 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1042 | |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1043 | bool |
Eric Christopher | b0a1814 | 2014-11-18 22:40:27 +0000 | [diff] [blame] | 1044 | DoExecute (Args& command, CommandReturnObject &result) override |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1045 | { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1046 | const size_t argc = command.GetArgumentCount(); |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1047 | if (argc == 0) |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1048 | { |
Jason Molenda | 69c12cc | 2011-11-10 22:43:35 +0000 | [diff] [blame] | 1049 | result.AppendError ("usage: 'command regex <command-name> [s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n"); |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1050 | result.SetStatus (eReturnStatusFailed); |
| 1051 | } |
| 1052 | else |
| 1053 | { |
| 1054 | Error error; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1055 | const char *name = command.GetArgumentAtIndex(0); |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1056 | m_regex_cmd_ap.reset (new CommandObjectRegexCommand (m_interpreter, |
| 1057 | name, |
| 1058 | m_options.GetHelp (), |
| 1059 | m_options.GetSyntax (), |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 1060 | 10, |
| 1061 | 0, |
| 1062 | true)); |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (argc == 1) |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1065 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1066 | Debugger &debugger = m_interpreter.GetDebugger(); |
Kate Stone | e30f11d | 2014-11-17 19:06:59 +0000 | [diff] [blame] | 1067 | bool color_prompt = debugger.GetUseColor(); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1068 | const bool multiple_lines = true; // Get multiple lines |
| 1069 | IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger, |
Kate Stone | e30f11d | 2014-11-17 19:06:59 +0000 | [diff] [blame] | 1070 | IOHandler::Type::Other, |
Greg Clayton | 73d80fa | 2014-07-09 20:18:54 +0000 | [diff] [blame] | 1071 | "lldb-regex", // Name of input reader for history |
Greg Clayton | ea50863 | 2014-11-18 00:43:17 +0000 | [diff] [blame] | 1072 | "> ", // Prompt |
Kate Stone | e30f11d | 2014-11-17 19:06:59 +0000 | [diff] [blame] | 1073 | NULL, // Continuation prompt |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1074 | multiple_lines, |
Kate Stone | e30f11d | 2014-11-17 19:06:59 +0000 | [diff] [blame] | 1075 | color_prompt, |
Greg Clayton | f6913cd | 2014-03-07 00:53:24 +0000 | [diff] [blame] | 1076 | 0, // Don't show line numbers |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1077 | *this)); |
| 1078 | |
| 1079 | if (io_handler_sp) |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1080 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1081 | debugger.PushIOHandler(io_handler_sp); |
| 1082 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1085 | else |
| 1086 | { |
| 1087 | for (size_t arg_idx = 1; arg_idx < argc; ++arg_idx) |
| 1088 | { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1089 | llvm::StringRef arg_strref (command.GetArgumentAtIndex(arg_idx)); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1090 | bool check_only = false; |
| 1091 | error = AppendRegexSubstitution (arg_strref, check_only); |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1092 | if (error.Fail()) |
| 1093 | break; |
| 1094 | } |
| 1095 | |
| 1096 | if (error.Success()) |
| 1097 | { |
| 1098 | AddRegexCommandToInterpreter(); |
| 1099 | } |
| 1100 | } |
| 1101 | if (error.Fail()) |
| 1102 | { |
| 1103 | result.AppendError (error.AsCString()); |
| 1104 | result.SetStatus (eReturnStatusFailed); |
| 1105 | } |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1106 | } |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1107 | |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1108 | return result.Succeeded(); |
| 1109 | } |
| 1110 | |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1111 | Error |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1112 | AppendRegexSubstitution (const llvm::StringRef ®ex_sed, bool check_only) |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1113 | { |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1114 | Error error; |
| 1115 | |
| 1116 | if (m_regex_cmd_ap.get() == NULL) |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1117 | { |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1118 | error.SetErrorStringWithFormat("invalid regular expression command object for: '%.*s'", |
| 1119 | (int)regex_sed.size(), |
| 1120 | regex_sed.data()); |
| 1121 | return error; |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1122 | } |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1123 | |
| 1124 | size_t regex_sed_size = regex_sed.size(); |
| 1125 | |
| 1126 | if (regex_sed_size <= 1) |
| 1127 | { |
| 1128 | error.SetErrorStringWithFormat("regular expression substitution string is too short: '%.*s'", |
| 1129 | (int)regex_sed.size(), |
| 1130 | regex_sed.data()); |
| 1131 | return error; |
| 1132 | } |
| 1133 | |
| 1134 | if (regex_sed[0] != 's') |
| 1135 | { |
| 1136 | error.SetErrorStringWithFormat("regular expression substitution string doesn't start with 's': '%.*s'", |
| 1137 | (int)regex_sed.size(), |
| 1138 | regex_sed.data()); |
| 1139 | return error; |
| 1140 | } |
| 1141 | const size_t first_separator_char_pos = 1; |
| 1142 | // use the char that follows 's' as the regex separator character |
| 1143 | // so we can have "s/<regex>/<subst>/" or "s|<regex>|<subst>|" |
| 1144 | const char separator_char = regex_sed[first_separator_char_pos]; |
| 1145 | const size_t second_separator_char_pos = regex_sed.find (separator_char, first_separator_char_pos + 1); |
| 1146 | |
| 1147 | if (second_separator_char_pos == std::string::npos) |
| 1148 | { |
Greg Clayton | ea50863 | 2014-11-18 00:43:17 +0000 | [diff] [blame] | 1149 | error.SetErrorStringWithFormat("missing second '%c' separator char after '%.*s' in '%.*s'", |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1150 | separator_char, |
| 1151 | (int)(regex_sed.size() - first_separator_char_pos - 1), |
Greg Clayton | ea50863 | 2014-11-18 00:43:17 +0000 | [diff] [blame] | 1152 | regex_sed.data() + (first_separator_char_pos + 1), |
| 1153 | (int)regex_sed.size(), |
| 1154 | regex_sed.data()); |
| 1155 | return error; |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | const size_t third_separator_char_pos = regex_sed.find (separator_char, second_separator_char_pos + 1); |
| 1159 | |
| 1160 | if (third_separator_char_pos == std::string::npos) |
| 1161 | { |
Greg Clayton | ea50863 | 2014-11-18 00:43:17 +0000 | [diff] [blame] | 1162 | error.SetErrorStringWithFormat("missing third '%c' separator char after '%.*s' in '%.*s'", |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1163 | separator_char, |
| 1164 | (int)(regex_sed.size() - second_separator_char_pos - 1), |
Greg Clayton | ea50863 | 2014-11-18 00:43:17 +0000 | [diff] [blame] | 1165 | regex_sed.data() + (second_separator_char_pos + 1), |
| 1166 | (int)regex_sed.size(), |
| 1167 | regex_sed.data()); |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1168 | return error; |
| 1169 | } |
| 1170 | |
| 1171 | if (third_separator_char_pos != regex_sed_size - 1) |
| 1172 | { |
| 1173 | // Make sure that everything that follows the last regex |
| 1174 | // separator char |
| 1175 | if (regex_sed.find_first_not_of("\t\n\v\f\r ", third_separator_char_pos + 1) != std::string::npos) |
| 1176 | { |
| 1177 | error.SetErrorStringWithFormat("extra data found after the '%.*s' regular expression substitution string: '%.*s'", |
| 1178 | (int)third_separator_char_pos + 1, |
| 1179 | regex_sed.data(), |
| 1180 | (int)(regex_sed.size() - third_separator_char_pos - 1), |
| 1181 | regex_sed.data() + (third_separator_char_pos + 1)); |
| 1182 | return error; |
| 1183 | } |
| 1184 | |
| 1185 | } |
| 1186 | else if (first_separator_char_pos + 1 == second_separator_char_pos) |
| 1187 | { |
| 1188 | error.SetErrorStringWithFormat("<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'", |
| 1189 | separator_char, |
| 1190 | separator_char, |
| 1191 | separator_char, |
| 1192 | (int)regex_sed.size(), |
| 1193 | regex_sed.data()); |
| 1194 | return error; |
| 1195 | } |
| 1196 | else if (second_separator_char_pos + 1 == third_separator_char_pos) |
| 1197 | { |
| 1198 | error.SetErrorStringWithFormat("<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'", |
| 1199 | separator_char, |
| 1200 | separator_char, |
| 1201 | separator_char, |
| 1202 | (int)regex_sed.size(), |
| 1203 | regex_sed.data()); |
| 1204 | return error; |
| 1205 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1206 | |
| 1207 | if (check_only == false) |
| 1208 | { |
| 1209 | std::string regex(regex_sed.substr(first_separator_char_pos + 1, second_separator_char_pos - first_separator_char_pos - 1)); |
| 1210 | std::string subst(regex_sed.substr(second_separator_char_pos + 1, third_separator_char_pos - second_separator_char_pos - 1)); |
| 1211 | m_regex_cmd_ap->AddRegexCommand (regex.c_str(), |
| 1212 | subst.c_str()); |
| 1213 | } |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1214 | return error; |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | void |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1218 | AddRegexCommandToInterpreter() |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1219 | { |
| 1220 | if (m_regex_cmd_ap.get()) |
| 1221 | { |
| 1222 | if (m_regex_cmd_ap->HasRegexEntries()) |
| 1223 | { |
| 1224 | CommandObjectSP cmd_sp (m_regex_cmd_ap.release()); |
| 1225 | m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true); |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1230 | private: |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 1231 | std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_ap; |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1232 | |
| 1233 | class CommandOptions : public Options |
| 1234 | { |
| 1235 | public: |
| 1236 | |
| 1237 | CommandOptions (CommandInterpreter &interpreter) : |
| 1238 | Options (interpreter) |
| 1239 | { |
| 1240 | } |
| 1241 | |
| 1242 | virtual |
| 1243 | ~CommandOptions (){} |
| 1244 | |
| 1245 | virtual Error |
| 1246 | SetOptionValue (uint32_t option_idx, const char *option_arg) |
| 1247 | { |
| 1248 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 1249 | const int short_option = m_getopt_table[option_idx].val; |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1250 | |
| 1251 | switch (short_option) |
| 1252 | { |
| 1253 | case 'h': |
| 1254 | m_help.assign (option_arg); |
| 1255 | break; |
| 1256 | case 's': |
| 1257 | m_syntax.assign (option_arg); |
| 1258 | break; |
| 1259 | |
| 1260 | default: |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 1261 | error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1262 | break; |
| 1263 | } |
| 1264 | |
| 1265 | return error; |
| 1266 | } |
| 1267 | |
| 1268 | void |
| 1269 | OptionParsingStarting () |
| 1270 | { |
| 1271 | m_help.clear(); |
| 1272 | m_syntax.clear(); |
| 1273 | } |
| 1274 | |
| 1275 | const OptionDefinition* |
| 1276 | GetDefinitions () |
| 1277 | { |
| 1278 | return g_option_table; |
| 1279 | } |
| 1280 | |
| 1281 | // Options table: Required for subclasses of Options. |
| 1282 | |
| 1283 | static OptionDefinition g_option_table[]; |
| 1284 | |
| 1285 | const char * |
| 1286 | GetHelp () |
| 1287 | { |
| 1288 | if (m_help.empty()) |
| 1289 | return NULL; |
| 1290 | return m_help.c_str(); |
| 1291 | } |
| 1292 | const char * |
| 1293 | GetSyntax () |
| 1294 | { |
| 1295 | if (m_syntax.empty()) |
| 1296 | return NULL; |
| 1297 | return m_syntax.c_str(); |
| 1298 | } |
| 1299 | // Instance variables to hold the values for command options. |
| 1300 | protected: |
| 1301 | std::string m_help; |
| 1302 | std::string m_syntax; |
| 1303 | }; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1304 | |
Eric Christopher | b0a1814 | 2014-11-18 22:40:27 +0000 | [diff] [blame] | 1305 | Options * |
| 1306 | GetOptions () override |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1307 | { |
| 1308 | return &m_options; |
| 1309 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1310 | |
| 1311 | CommandOptions m_options; |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1312 | }; |
| 1313 | |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1314 | OptionDefinition |
| 1315 | CommandObjectCommandsAddRegex::CommandOptions::g_option_table[] = |
| 1316 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 1317 | { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeNone, "The help text to display for this command."}, |
| 1318 | { LLDB_OPT_SET_1, false, "syntax", 's', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeNone, "A syntax string showing the typical usage syntax."}, |
| 1319 | { 0 , false, NULL , 0 , 0 , NULL, NULL, 0, eArgTypeNone, NULL } |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1320 | }; |
| 1321 | |
| 1322 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1323 | class CommandObjectPythonFunction : public CommandObjectRaw |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1324 | { |
| 1325 | private: |
| 1326 | std::string m_function_name; |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1327 | ScriptedCommandSynchronicity m_synchro; |
Enrico Granata | fac939e | 2012-09-18 21:53:02 +0000 | [diff] [blame] | 1328 | bool m_fetched_help_long; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1329 | |
| 1330 | public: |
| 1331 | |
| 1332 | CommandObjectPythonFunction (CommandInterpreter &interpreter, |
| 1333 | std::string name, |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1334 | std::string funct, |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1335 | std::string help, |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1336 | ScriptedCommandSynchronicity synch) : |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1337 | CommandObjectRaw (interpreter, |
| 1338 | name.c_str(), |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1339 | NULL, |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1340 | NULL), |
| 1341 | m_function_name(funct), |
Enrico Granata | fac939e | 2012-09-18 21:53:02 +0000 | [diff] [blame] | 1342 | m_synchro(synch), |
| 1343 | m_fetched_help_long(false) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1344 | { |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1345 | if (!help.empty()) |
| 1346 | SetHelp(help.c_str()); |
| 1347 | else |
| 1348 | { |
| 1349 | StreamString stream; |
| 1350 | stream.Printf("For more information run 'help %s'",name.c_str()); |
| 1351 | SetHelp(stream.GetData()); |
| 1352 | } |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | virtual |
| 1356 | ~CommandObjectPythonFunction () |
| 1357 | { |
| 1358 | } |
| 1359 | |
| 1360 | virtual bool |
Greg Clayton | 3a18e31 | 2012-10-08 22:41:53 +0000 | [diff] [blame] | 1361 | IsRemovable () const |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1362 | { |
| 1363 | return true; |
| 1364 | } |
| 1365 | |
| 1366 | const std::string& |
| 1367 | GetFunctionName () |
| 1368 | { |
| 1369 | return m_function_name; |
| 1370 | } |
| 1371 | |
| 1372 | ScriptedCommandSynchronicity |
| 1373 | GetSynchronicity () |
| 1374 | { |
| 1375 | return m_synchro; |
| 1376 | } |
| 1377 | |
Enrico Granata | fac939e | 2012-09-18 21:53:02 +0000 | [diff] [blame] | 1378 | virtual const char * |
| 1379 | GetHelpLong () |
| 1380 | { |
| 1381 | if (!m_fetched_help_long) |
| 1382 | { |
| 1383 | ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter(); |
| 1384 | if (scripter) |
| 1385 | { |
| 1386 | std::string docstring; |
| 1387 | m_fetched_help_long = scripter->GetDocumentationForItem(m_function_name.c_str(),docstring); |
| 1388 | if (!docstring.empty()) |
| 1389 | SetHelpLong(docstring); |
| 1390 | } |
| 1391 | } |
| 1392 | return CommandObjectRaw::GetHelpLong(); |
| 1393 | } |
| 1394 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1395 | protected: |
| 1396 | virtual bool |
| 1397 | DoExecute (const char *raw_command_line, CommandReturnObject &result) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1398 | { |
| 1399 | ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter(); |
| 1400 | |
| 1401 | Error error; |
| 1402 | |
Jim Ingham | 70f11f8 | 2012-06-27 17:25:36 +0000 | [diff] [blame] | 1403 | result.SetStatus(eReturnStatusInvalid); |
| 1404 | |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1405 | if (!scripter || scripter->RunScriptBasedCommand(m_function_name.c_str(), |
| 1406 | raw_command_line, |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1407 | m_synchro, |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1408 | result, |
Enrico Granata | 06be059 | 2014-10-01 21:47:29 +0000 | [diff] [blame] | 1409 | error, |
| 1410 | m_exe_ctx) == false) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1411 | { |
| 1412 | result.AppendError(error.AsCString()); |
| 1413 | result.SetStatus(eReturnStatusFailed); |
| 1414 | } |
| 1415 | else |
Jim Ingham | 70f11f8 | 2012-06-27 17:25:36 +0000 | [diff] [blame] | 1416 | { |
| 1417 | // Don't change the status if the command already set it... |
| 1418 | if (result.GetStatus() == eReturnStatusInvalid) |
| 1419 | { |
Daniel Malea | 9a71a7d | 2013-07-03 17:58:31 +0000 | [diff] [blame] | 1420 | if (result.GetOutputData() == NULL || result.GetOutputData()[0] == '\0') |
Jim Ingham | 70f11f8 | 2012-06-27 17:25:36 +0000 | [diff] [blame] | 1421 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1422 | else |
| 1423 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1424 | } |
| 1425 | } |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1426 | |
| 1427 | return result.Succeeded(); |
| 1428 | } |
| 1429 | |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1430 | }; |
| 1431 | |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1432 | class CommandObjectScriptingObject : public CommandObjectRaw |
| 1433 | { |
| 1434 | private: |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame^] | 1435 | StructuredData::GenericSP m_cmd_obj_sp; |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1436 | ScriptedCommandSynchronicity m_synchro; |
Enrico Granata | 6f79bb2 | 2015-03-13 22:22:28 +0000 | [diff] [blame] | 1437 | bool m_fetched_help_short:1; |
| 1438 | bool m_fetched_help_long:1; |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1439 | |
| 1440 | public: |
| 1441 | |
| 1442 | CommandObjectScriptingObject (CommandInterpreter &interpreter, |
| 1443 | std::string name, |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame^] | 1444 | StructuredData::GenericSP cmd_obj_sp, |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1445 | ScriptedCommandSynchronicity synch) : |
| 1446 | CommandObjectRaw (interpreter, |
| 1447 | name.c_str(), |
| 1448 | NULL, |
| 1449 | NULL), |
| 1450 | m_cmd_obj_sp(cmd_obj_sp), |
Enrico Granata | 6f79bb2 | 2015-03-13 22:22:28 +0000 | [diff] [blame] | 1451 | m_synchro(synch), |
| 1452 | m_fetched_help_short(false), |
| 1453 | m_fetched_help_long(false) |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1454 | { |
| 1455 | StreamString stream; |
| 1456 | stream.Printf("For more information run 'help %s'",name.c_str()); |
| 1457 | SetHelp(stream.GetData()); |
| 1458 | } |
| 1459 | |
| 1460 | virtual |
| 1461 | ~CommandObjectScriptingObject () |
| 1462 | { |
| 1463 | } |
| 1464 | |
| 1465 | virtual bool |
| 1466 | IsRemovable () const |
| 1467 | { |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame^] | 1471 | StructuredData::GenericSP |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1472 | GetImplementingObject () |
| 1473 | { |
| 1474 | return m_cmd_obj_sp; |
| 1475 | } |
| 1476 | |
| 1477 | ScriptedCommandSynchronicity |
| 1478 | GetSynchronicity () |
| 1479 | { |
| 1480 | return m_synchro; |
| 1481 | } |
Enrico Granata | 6f79bb2 | 2015-03-13 22:22:28 +0000 | [diff] [blame] | 1482 | |
| 1483 | virtual const char * |
| 1484 | GetHelp () |
| 1485 | { |
| 1486 | if (!m_fetched_help_short) |
| 1487 | { |
| 1488 | ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter(); |
| 1489 | if (scripter) |
| 1490 | { |
| 1491 | std::string docstring; |
| 1492 | m_fetched_help_short = scripter->GetShortHelpForCommandObject(m_cmd_obj_sp,docstring); |
| 1493 | if (!docstring.empty()) |
| 1494 | SetHelp(docstring); |
| 1495 | } |
| 1496 | } |
| 1497 | return CommandObjectRaw::GetHelp(); |
| 1498 | } |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1499 | |
| 1500 | virtual const char * |
| 1501 | GetHelpLong () |
| 1502 | { |
Enrico Granata | 6f79bb2 | 2015-03-13 22:22:28 +0000 | [diff] [blame] | 1503 | if (!m_fetched_help_long) |
| 1504 | { |
| 1505 | ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter(); |
| 1506 | if (scripter) |
| 1507 | { |
| 1508 | std::string docstring; |
| 1509 | m_fetched_help_long = scripter->GetLongHelpForCommandObject(m_cmd_obj_sp,docstring); |
| 1510 | if (!docstring.empty()) |
| 1511 | SetHelpLong(docstring); |
| 1512 | } |
| 1513 | } |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1514 | return CommandObjectRaw::GetHelpLong(); |
| 1515 | } |
| 1516 | |
| 1517 | protected: |
| 1518 | virtual bool |
| 1519 | DoExecute (const char *raw_command_line, CommandReturnObject &result) |
| 1520 | { |
| 1521 | ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter(); |
| 1522 | |
| 1523 | Error error; |
| 1524 | |
| 1525 | result.SetStatus(eReturnStatusInvalid); |
| 1526 | |
| 1527 | if (!scripter || scripter->RunScriptBasedCommand(m_cmd_obj_sp, |
| 1528 | raw_command_line, |
| 1529 | m_synchro, |
| 1530 | result, |
| 1531 | error, |
| 1532 | m_exe_ctx) == false) |
| 1533 | { |
| 1534 | result.AppendError(error.AsCString()); |
| 1535 | result.SetStatus(eReturnStatusFailed); |
| 1536 | } |
| 1537 | else |
| 1538 | { |
| 1539 | // Don't change the status if the command already set it... |
| 1540 | if (result.GetStatus() == eReturnStatusInvalid) |
| 1541 | { |
| 1542 | if (result.GetOutputData() == NULL || result.GetOutputData()[0] == '\0') |
| 1543 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1544 | else |
| 1545 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | return result.Succeeded(); |
| 1550 | } |
| 1551 | |
| 1552 | }; |
| 1553 | |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1554 | //------------------------------------------------------------------------- |
| 1555 | // CommandObjectCommandsScriptImport |
| 1556 | //------------------------------------------------------------------------- |
| 1557 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1558 | class CommandObjectCommandsScriptImport : public CommandObjectParsed |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1559 | { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1560 | public: |
| 1561 | CommandObjectCommandsScriptImport (CommandInterpreter &interpreter) : |
| 1562 | CommandObjectParsed (interpreter, |
| 1563 | "command script import", |
| 1564 | "Import a scripting module in LLDB.", |
| 1565 | NULL), |
| 1566 | m_options(interpreter) |
| 1567 | { |
| 1568 | CommandArgumentEntry arg1; |
| 1569 | CommandArgumentData cmd_arg; |
| 1570 | |
| 1571 | // Define the first (and only) variant of this arg. |
| 1572 | cmd_arg.arg_type = eArgTypeFilename; |
| 1573 | cmd_arg.arg_repetition = eArgRepeatPlain; |
| 1574 | |
| 1575 | // There is only one variant this argument could be; put it into the argument entry. |
| 1576 | arg1.push_back (cmd_arg); |
| 1577 | |
| 1578 | // Push the data for the first argument into the m_arguments vector. |
| 1579 | m_arguments.push_back (arg1); |
| 1580 | } |
| 1581 | |
| 1582 | ~CommandObjectCommandsScriptImport () |
| 1583 | { |
| 1584 | } |
| 1585 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1586 | virtual int |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1587 | HandleArgumentCompletion (Args &input, |
| 1588 | int &cursor_index, |
| 1589 | int &cursor_char_position, |
| 1590 | OptionElementVector &opt_element_vector, |
| 1591 | int match_start_point, |
| 1592 | int max_return_elements, |
| 1593 | bool &word_complete, |
| 1594 | StringList &matches) |
| 1595 | { |
| 1596 | std::string completion_str (input.GetArgumentAtIndex(cursor_index)); |
| 1597 | completion_str.erase (cursor_char_position); |
| 1598 | |
| 1599 | CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, |
| 1600 | CommandCompletions::eDiskFileCompletion, |
| 1601 | completion_str.c_str(), |
| 1602 | match_start_point, |
| 1603 | max_return_elements, |
| 1604 | NULL, |
| 1605 | word_complete, |
| 1606 | matches); |
| 1607 | return matches.GetSize(); |
| 1608 | } |
| 1609 | |
| 1610 | virtual Options * |
| 1611 | GetOptions () |
| 1612 | { |
| 1613 | return &m_options; |
| 1614 | } |
| 1615 | |
| 1616 | protected: |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1617 | |
| 1618 | class CommandOptions : public Options |
| 1619 | { |
| 1620 | public: |
| 1621 | |
| 1622 | CommandOptions (CommandInterpreter &interpreter) : |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1623 | Options (interpreter) |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1624 | { |
| 1625 | } |
| 1626 | |
| 1627 | virtual |
| 1628 | ~CommandOptions (){} |
| 1629 | |
| 1630 | virtual Error |
| 1631 | SetOptionValue (uint32_t option_idx, const char *option_arg) |
| 1632 | { |
| 1633 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 1634 | const int short_option = m_getopt_table[option_idx].val; |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1635 | |
| 1636 | switch (short_option) |
| 1637 | { |
| 1638 | case 'r': |
| 1639 | m_allow_reload = true; |
| 1640 | break; |
| 1641 | default: |
| 1642 | error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); |
| 1643 | break; |
| 1644 | } |
| 1645 | |
| 1646 | return error; |
| 1647 | } |
| 1648 | |
| 1649 | void |
| 1650 | OptionParsingStarting () |
| 1651 | { |
Enrico Granata | e0c70f1 | 2013-05-31 01:03:09 +0000 | [diff] [blame] | 1652 | m_allow_reload = true; |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | const OptionDefinition* |
| 1656 | GetDefinitions () |
| 1657 | { |
| 1658 | return g_option_table; |
| 1659 | } |
| 1660 | |
| 1661 | // Options table: Required for subclasses of Options. |
| 1662 | |
| 1663 | static OptionDefinition g_option_table[]; |
| 1664 | |
| 1665 | // Instance variables to hold the values for command options. |
| 1666 | |
| 1667 | bool m_allow_reload; |
| 1668 | }; |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1669 | |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1670 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1671 | DoExecute (Args& command, CommandReturnObject &result) |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1672 | { |
| 1673 | |
| 1674 | if (m_interpreter.GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython) |
| 1675 | { |
| 1676 | result.AppendError ("only scripting language supported for module importing is currently Python"); |
| 1677 | result.SetStatus (eReturnStatusFailed); |
| 1678 | return false; |
| 1679 | } |
| 1680 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1681 | size_t argc = command.GetArgumentCount(); |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1682 | |
| 1683 | if (argc != 1) |
| 1684 | { |
| 1685 | result.AppendError ("'command script import' requires one argument"); |
| 1686 | result.SetStatus (eReturnStatusFailed); |
| 1687 | return false; |
| 1688 | } |
| 1689 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1690 | std::string path = command.GetArgumentAtIndex(0); |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1691 | Error error; |
| 1692 | |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 1693 | const bool init_session = true; |
Enrico Granata | 078551c | 2013-05-09 19:33:49 +0000 | [diff] [blame] | 1694 | // FIXME: this is necessary because CommandObject::CheckRequirements() assumes that |
| 1695 | // commands won't ever be recursively invoked, but it's actually possible to craft |
| 1696 | // a Python script that does other "command script imports" in __lldb_init_module |
| 1697 | // the real fix is to have recursive commands possible with a CommandInvocation object |
| 1698 | // separate from the CommandObject itself, so that recursive command invocations |
| 1699 | // won't stomp on each other (wrt to execution contents, options, and more) |
| 1700 | m_exe_ctx.Clear(); |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1701 | if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule(path.c_str(), |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1702 | m_options.m_allow_reload, |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 1703 | init_session, |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1704 | error)) |
| 1705 | { |
| 1706 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1707 | } |
| 1708 | else |
| 1709 | { |
| 1710 | result.AppendErrorWithFormat("module importing failed: %s", error.AsCString()); |
| 1711 | result.SetStatus (eReturnStatusFailed); |
| 1712 | } |
| 1713 | |
| 1714 | return result.Succeeded(); |
| 1715 | } |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1716 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1717 | CommandOptions m_options; |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1718 | }; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1719 | |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1720 | OptionDefinition |
| 1721 | CommandObjectCommandsScriptImport::CommandOptions::g_option_table[] = |
| 1722 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 1723 | { LLDB_OPT_SET_1, false, "allow-reload", 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Allow the script to be loaded even if it was already loaded before. This argument exists for backwards compatibility, but reloading is always allowed, whether you specify it or not."}, |
| 1724 | { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL } |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1725 | }; |
| 1726 | |
| 1727 | |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1728 | //------------------------------------------------------------------------- |
| 1729 | // CommandObjectCommandsScriptAdd |
| 1730 | //------------------------------------------------------------------------- |
| 1731 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1732 | class CommandObjectCommandsScriptAdd : |
| 1733 | public CommandObjectParsed, |
| 1734 | public IOHandlerDelegateMultiline |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1735 | { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1736 | public: |
| 1737 | CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter) : |
| 1738 | CommandObjectParsed (interpreter, |
| 1739 | "command script add", |
| 1740 | "Add a scripted function as an LLDB command.", |
| 1741 | NULL), |
Greg Clayton | c3d874a | 2014-05-08 16:59:00 +0000 | [diff] [blame] | 1742 | IOHandlerDelegateMultiline ("DONE"), |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1743 | m_options (interpreter) |
| 1744 | { |
| 1745 | CommandArgumentEntry arg1; |
| 1746 | CommandArgumentData cmd_arg; |
| 1747 | |
| 1748 | // Define the first (and only) variant of this arg. |
| 1749 | cmd_arg.arg_type = eArgTypeCommandName; |
| 1750 | cmd_arg.arg_repetition = eArgRepeatPlain; |
| 1751 | |
| 1752 | // There is only one variant this argument could be; put it into the argument entry. |
| 1753 | arg1.push_back (cmd_arg); |
| 1754 | |
| 1755 | // Push the data for the first argument into the m_arguments vector. |
| 1756 | m_arguments.push_back (arg1); |
| 1757 | } |
| 1758 | |
| 1759 | ~CommandObjectCommandsScriptAdd () |
| 1760 | { |
| 1761 | } |
| 1762 | |
| 1763 | virtual Options * |
| 1764 | GetOptions () |
| 1765 | { |
| 1766 | return &m_options; |
| 1767 | } |
| 1768 | |
| 1769 | protected: |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1770 | |
| 1771 | class CommandOptions : public Options |
| 1772 | { |
| 1773 | public: |
| 1774 | |
| 1775 | CommandOptions (CommandInterpreter &interpreter) : |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1776 | Options (interpreter), |
| 1777 | m_class_name(), |
| 1778 | m_funct_name(), |
| 1779 | m_short_help(), |
| 1780 | m_synchronicity(eScriptedCommandSynchronicitySynchronous) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1781 | { |
| 1782 | } |
| 1783 | |
| 1784 | virtual |
| 1785 | ~CommandOptions (){} |
| 1786 | |
| 1787 | virtual Error |
| 1788 | SetOptionValue (uint32_t option_idx, const char *option_arg) |
| 1789 | { |
| 1790 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 1791 | const int short_option = m_getopt_table[option_idx].val; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1792 | |
| 1793 | switch (short_option) |
| 1794 | { |
| 1795 | case 'f': |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1796 | if (option_arg) |
| 1797 | m_funct_name.assign(option_arg); |
| 1798 | break; |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1799 | case 'c': |
| 1800 | if (option_arg) |
| 1801 | m_class_name.assign(option_arg); |
| 1802 | break; |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1803 | case 'h': |
| 1804 | if (option_arg) |
| 1805 | m_short_help.assign(option_arg); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1806 | break; |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1807 | case 's': |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1808 | m_synchronicity = (ScriptedCommandSynchronicity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error); |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1809 | if (!error.Success()) |
| 1810 | error.SetErrorStringWithFormat ("unrecognized value for synchronicity '%s'", option_arg); |
| 1811 | break; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1812 | default: |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 1813 | error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1814 | break; |
| 1815 | } |
| 1816 | |
| 1817 | return error; |
| 1818 | } |
| 1819 | |
| 1820 | void |
| 1821 | OptionParsingStarting () |
| 1822 | { |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1823 | m_class_name.clear(); |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1824 | m_funct_name.clear(); |
| 1825 | m_short_help.clear(); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1826 | m_synchronicity = eScriptedCommandSynchronicitySynchronous; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | const OptionDefinition* |
| 1830 | GetDefinitions () |
| 1831 | { |
| 1832 | return g_option_table; |
| 1833 | } |
| 1834 | |
| 1835 | // Options table: Required for subclasses of Options. |
| 1836 | |
| 1837 | static OptionDefinition g_option_table[]; |
| 1838 | |
| 1839 | // Instance variables to hold the values for command options. |
| 1840 | |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1841 | std::string m_class_name; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1842 | std::string m_funct_name; |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1843 | std::string m_short_help; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1844 | ScriptedCommandSynchronicity m_synchronicity; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1845 | }; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1846 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1847 | virtual void |
| 1848 | IOHandlerActivated (IOHandler &io_handler) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1849 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1850 | StreamFileSP output_sp(io_handler.GetOutputStreamFile()); |
| 1851 | if (output_sp) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1852 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1853 | output_sp->PutCString(g_python_command_instructions); |
| 1854 | output_sp->Flush(); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1855 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1856 | } |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1857 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1858 | |
| 1859 | virtual void |
| 1860 | IOHandlerInputComplete (IOHandler &io_handler, std::string &data) |
| 1861 | { |
| 1862 | StreamFileSP error_sp = io_handler.GetErrorStreamFile(); |
| 1863 | |
| 1864 | ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter(); |
| 1865 | if (interpreter) |
| 1866 | { |
| 1867 | |
| 1868 | StringList lines; |
| 1869 | lines.SplitIntoLines(data); |
| 1870 | if (lines.GetSize() > 0) |
| 1871 | { |
| 1872 | std::string funct_name_str; |
| 1873 | if (interpreter->GenerateScriptAliasFunction (lines, funct_name_str)) |
| 1874 | { |
| 1875 | if (funct_name_str.empty()) |
| 1876 | { |
| 1877 | error_sp->Printf ("error: unable to obtain a function name, didn't add python command.\n"); |
| 1878 | error_sp->Flush(); |
| 1879 | } |
| 1880 | else |
| 1881 | { |
| 1882 | // everything should be fine now, let's add this alias |
| 1883 | |
| 1884 | CommandObjectSP command_obj_sp(new CommandObjectPythonFunction (m_interpreter, |
| 1885 | m_cmd_name, |
| 1886 | funct_name_str.c_str(), |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1887 | m_short_help, |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1888 | m_synchronicity)); |
| 1889 | |
| 1890 | if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp, true)) |
| 1891 | { |
| 1892 | error_sp->Printf ("error: unable to add selected command, didn't add python command.\n"); |
| 1893 | error_sp->Flush(); |
| 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | else |
| 1898 | { |
| 1899 | error_sp->Printf ("error: unable to create function, didn't add python command.\n"); |
| 1900 | error_sp->Flush(); |
| 1901 | } |
| 1902 | } |
| 1903 | else |
| 1904 | { |
| 1905 | error_sp->Printf ("error: empty function, didn't add python command.\n"); |
| 1906 | error_sp->Flush(); |
| 1907 | } |
| 1908 | } |
| 1909 | else |
| 1910 | { |
| 1911 | error_sp->Printf ("error: script interpreter missing, didn't add python command.\n"); |
| 1912 | error_sp->Flush(); |
| 1913 | } |
| 1914 | |
| 1915 | io_handler.SetIsDone(true); |
| 1916 | |
| 1917 | |
| 1918 | } |
| 1919 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1920 | protected: |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1921 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1922 | DoExecute (Args& command, CommandReturnObject &result) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1923 | { |
Enrico Granata | 99f0b8f | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 1924 | |
| 1925 | if (m_interpreter.GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython) |
| 1926 | { |
| 1927 | result.AppendError ("only scripting language supported for scripted commands is currently Python"); |
| 1928 | result.SetStatus (eReturnStatusFailed); |
| 1929 | return false; |
| 1930 | } |
| 1931 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1932 | size_t argc = command.GetArgumentCount(); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1933 | |
| 1934 | if (argc != 1) |
| 1935 | { |
| 1936 | result.AppendError ("'command script add' requires one argument"); |
| 1937 | result.SetStatus (eReturnStatusFailed); |
| 1938 | return false; |
| 1939 | } |
| 1940 | |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1941 | // Store the options in case we get multi-line input |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1942 | m_cmd_name = command.GetArgumentAtIndex(0); |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1943 | m_short_help.assign(m_options.m_short_help); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1944 | m_synchronicity = m_options.m_synchronicity; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1945 | |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1946 | if (m_options.m_class_name.empty()) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1947 | { |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1948 | if (m_options.m_funct_name.empty()) |
| 1949 | { |
| 1950 | m_interpreter.GetPythonCommandsFromIOHandler (" ", // Prompt |
| 1951 | *this, // IOHandlerDelegate |
| 1952 | true, // Run IOHandler in async mode |
| 1953 | NULL); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions |
| 1954 | } |
| 1955 | else |
| 1956 | { |
| 1957 | CommandObjectSP new_cmd(new CommandObjectPythonFunction(m_interpreter, |
| 1958 | m_cmd_name, |
| 1959 | m_options.m_funct_name, |
| 1960 | m_options.m_short_help, |
| 1961 | m_synchronicity)); |
| 1962 | if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) |
| 1963 | { |
| 1964 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1965 | } |
| 1966 | else |
| 1967 | { |
| 1968 | result.AppendError("cannot add command"); |
| 1969 | result.SetStatus (eReturnStatusFailed); |
| 1970 | } |
| 1971 | } |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1972 | } |
| 1973 | else |
| 1974 | { |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1975 | ScriptInterpreter *interpreter = GetCommandInterpreter().GetScriptInterpreter(); |
| 1976 | if (!interpreter) |
| 1977 | { |
| 1978 | result.AppendError("cannot find ScriptInterpreter"); |
| 1979 | result.SetStatus(eReturnStatusFailed); |
| 1980 | return false; |
| 1981 | } |
| 1982 | |
| 1983 | auto cmd_obj_sp = interpreter->CreateScriptCommandObject(m_options.m_class_name.c_str()); |
| 1984 | if (!cmd_obj_sp) |
| 1985 | { |
| 1986 | result.AppendError("cannot create helper object"); |
| 1987 | result.SetStatus(eReturnStatusFailed); |
| 1988 | return false; |
| 1989 | } |
| 1990 | |
| 1991 | CommandObjectSP new_cmd(new CommandObjectScriptingObject(m_interpreter, |
| 1992 | m_cmd_name, |
| 1993 | cmd_obj_sp, |
| 1994 | m_synchronicity)); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1995 | if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1996 | { |
| 1997 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1998 | } |
| 1999 | else |
| 2000 | { |
| 2001 | result.AppendError("cannot add command"); |
| 2002 | result.SetStatus (eReturnStatusFailed); |
| 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | return result.Succeeded(); |
| 2007 | |
| 2008 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2009 | |
| 2010 | CommandOptions m_options; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 2011 | std::string m_cmd_name; |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 2012 | std::string m_short_help; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 2013 | ScriptedCommandSynchronicity m_synchronicity; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2014 | }; |
| 2015 | |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2016 | static OptionEnumValueElement g_script_synchro_type[] = |
| 2017 | { |
| 2018 | { eScriptedCommandSynchronicitySynchronous, "synchronous", "Run synchronous"}, |
| 2019 | { eScriptedCommandSynchronicityAsynchronous, "asynchronous", "Run asynchronous"}, |
| 2020 | { eScriptedCommandSynchronicityCurrentValue, "current", "Do not alter current setting"}, |
| 2021 | { 0, NULL, NULL } |
| 2022 | }; |
| 2023 | |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2024 | OptionDefinition |
| 2025 | CommandObjectCommandsScriptAdd::CommandOptions::g_option_table[] = |
| 2026 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 2027 | { LLDB_OPT_SET_1, false, "function", 'f', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePythonFunction, "Name of the Python function to bind to this command name."}, |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 2028 | { LLDB_OPT_SET_2, false, "class", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePythonClass, "Name of the Python class to bind to this command name."}, |
Enrico Granata | 9370d27 | 2015-03-13 22:35:44 +0000 | [diff] [blame] | 2029 | { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeHelpText, "The help text to display for this command."}, |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 2030 | { LLDB_OPT_SET_ALL, false, "synchronicity", 's', OptionParser::eRequiredArgument, NULL, g_script_synchro_type, 0, eArgTypeScriptedCommandSynchronicity, "Set the synchronicity of this command's executions with regard to LLDB event system."}, |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 2031 | { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL } |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2032 | }; |
| 2033 | |
| 2034 | //------------------------------------------------------------------------- |
| 2035 | // CommandObjectCommandsScriptList |
| 2036 | //------------------------------------------------------------------------- |
| 2037 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2038 | class CommandObjectCommandsScriptList : public CommandObjectParsed |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2039 | { |
| 2040 | private: |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2041 | |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2042 | public: |
| 2043 | CommandObjectCommandsScriptList(CommandInterpreter &interpreter) : |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2044 | CommandObjectParsed (interpreter, |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2045 | "command script list", |
| 2046 | "List defined scripted commands.", |
| 2047 | NULL) |
| 2048 | { |
| 2049 | } |
| 2050 | |
| 2051 | ~CommandObjectCommandsScriptList () |
| 2052 | { |
| 2053 | } |
| 2054 | |
| 2055 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2056 | DoExecute (Args& command, CommandReturnObject &result) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2057 | { |
| 2058 | |
| 2059 | m_interpreter.GetHelp(result, |
| 2060 | CommandInterpreter::eCommandTypesUserDef); |
| 2061 | |
| 2062 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 2063 | |
| 2064 | return true; |
| 2065 | |
| 2066 | |
| 2067 | } |
| 2068 | }; |
| 2069 | |
| 2070 | //------------------------------------------------------------------------- |
| 2071 | // CommandObjectCommandsScriptClear |
| 2072 | //------------------------------------------------------------------------- |
| 2073 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2074 | class CommandObjectCommandsScriptClear : public CommandObjectParsed |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2075 | { |
| 2076 | private: |
| 2077 | |
| 2078 | public: |
| 2079 | CommandObjectCommandsScriptClear(CommandInterpreter &interpreter) : |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2080 | CommandObjectParsed (interpreter, |
| 2081 | "command script clear", |
| 2082 | "Delete all scripted commands.", |
| 2083 | NULL) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2084 | { |
| 2085 | } |
| 2086 | |
| 2087 | ~CommandObjectCommandsScriptClear () |
| 2088 | { |
| 2089 | } |
| 2090 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2091 | protected: |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2092 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2093 | DoExecute (Args& command, CommandReturnObject &result) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2094 | { |
| 2095 | |
| 2096 | m_interpreter.RemoveAllUser(); |
| 2097 | |
| 2098 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 2099 | |
| 2100 | return true; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2101 | } |
| 2102 | }; |
| 2103 | |
| 2104 | //------------------------------------------------------------------------- |
| 2105 | // CommandObjectCommandsScriptDelete |
| 2106 | //------------------------------------------------------------------------- |
| 2107 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2108 | class CommandObjectCommandsScriptDelete : public CommandObjectParsed |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2109 | { |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2110 | public: |
| 2111 | CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter) : |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2112 | CommandObjectParsed (interpreter, |
| 2113 | "command script delete", |
| 2114 | "Delete a scripted command.", |
| 2115 | NULL) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2116 | { |
| 2117 | CommandArgumentEntry arg1; |
| 2118 | CommandArgumentData cmd_arg; |
| 2119 | |
| 2120 | // Define the first (and only) variant of this arg. |
| 2121 | cmd_arg.arg_type = eArgTypeCommandName; |
| 2122 | cmd_arg.arg_repetition = eArgRepeatPlain; |
| 2123 | |
| 2124 | // There is only one variant this argument could be; put it into the argument entry. |
| 2125 | arg1.push_back (cmd_arg); |
| 2126 | |
| 2127 | // Push the data for the first argument into the m_arguments vector. |
| 2128 | m_arguments.push_back (arg1); |
| 2129 | } |
| 2130 | |
| 2131 | ~CommandObjectCommandsScriptDelete () |
| 2132 | { |
| 2133 | } |
| 2134 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2135 | protected: |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2136 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2137 | DoExecute (Args& command, CommandReturnObject &result) |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2138 | { |
| 2139 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2140 | size_t argc = command.GetArgumentCount(); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2141 | |
| 2142 | if (argc != 1) |
| 2143 | { |
| 2144 | result.AppendError ("'command script delete' requires one argument"); |
| 2145 | result.SetStatus (eReturnStatusFailed); |
| 2146 | return false; |
| 2147 | } |
| 2148 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 2149 | const char* cmd_name = command.GetArgumentAtIndex(0); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2150 | |
| 2151 | if (cmd_name && *cmd_name && m_interpreter.HasUserCommands() && m_interpreter.UserCommandExists(cmd_name)) |
| 2152 | { |
| 2153 | m_interpreter.RemoveUser(cmd_name); |
| 2154 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 2155 | } |
| 2156 | else |
| 2157 | { |
| 2158 | result.AppendErrorWithFormat ("command %s not found", cmd_name); |
| 2159 | result.SetStatus (eReturnStatusFailed); |
| 2160 | } |
| 2161 | |
| 2162 | return result.Succeeded(); |
| 2163 | |
| 2164 | } |
| 2165 | }; |
| 2166 | |
| 2167 | #pragma mark CommandObjectMultiwordCommandsScript |
| 2168 | |
| 2169 | //------------------------------------------------------------------------- |
| 2170 | // CommandObjectMultiwordCommandsScript |
| 2171 | //------------------------------------------------------------------------- |
| 2172 | |
| 2173 | class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword |
| 2174 | { |
| 2175 | public: |
| 2176 | CommandObjectMultiwordCommandsScript (CommandInterpreter &interpreter) : |
| 2177 | CommandObjectMultiword (interpreter, |
| 2178 | "command script", |
| 2179 | "A set of commands for managing or customizing script commands.", |
| 2180 | "command script <subcommand> [<subcommand-options>]") |
| 2181 | { |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 2182 | LoadSubCommand ("add", CommandObjectSP (new CommandObjectCommandsScriptAdd (interpreter))); |
| 2183 | LoadSubCommand ("delete", CommandObjectSP (new CommandObjectCommandsScriptDelete (interpreter))); |
| 2184 | LoadSubCommand ("clear", CommandObjectSP (new CommandObjectCommandsScriptClear (interpreter))); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2185 | LoadSubCommand ("list", CommandObjectSP (new CommandObjectCommandsScriptList (interpreter))); |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 2186 | LoadSubCommand ("import", CommandObjectSP (new CommandObjectCommandsScriptImport (interpreter))); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | ~CommandObjectMultiwordCommandsScript () |
| 2190 | { |
| 2191 | } |
| 2192 | |
| 2193 | }; |
| 2194 | |
| 2195 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 2196 | #pragma mark CommandObjectMultiwordCommands |
| 2197 | |
| 2198 | //------------------------------------------------------------------------- |
| 2199 | // CommandObjectMultiwordCommands |
| 2200 | //------------------------------------------------------------------------- |
| 2201 | |
| 2202 | CommandObjectMultiwordCommands::CommandObjectMultiwordCommands (CommandInterpreter &interpreter) : |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 2203 | CommandObjectMultiword (interpreter, |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 2204 | "command", |
Caroline Tice | 3f4c09c | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 2205 | "A set of commands for managing or customizing the debugger commands.", |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 2206 | "command <subcommand> [<subcommand-options>]") |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 2207 | { |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 2208 | LoadSubCommand ("source", CommandObjectSP (new CommandObjectCommandsSource (interpreter))); |
| 2209 | LoadSubCommand ("alias", CommandObjectSP (new CommandObjectCommandsAlias (interpreter))); |
| 2210 | LoadSubCommand ("unalias", CommandObjectSP (new CommandObjectCommandsUnalias (interpreter))); |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 2211 | LoadSubCommand ("delete", CommandObjectSP (new CommandObjectCommandsDelete (interpreter))); |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 2212 | LoadSubCommand ("regex", CommandObjectSP (new CommandObjectCommandsAddRegex (interpreter))); |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 2213 | LoadSubCommand ("history", CommandObjectSP (new CommandObjectCommandsHistory (interpreter))); |
| 2214 | LoadSubCommand ("script", CommandObjectSP (new CommandObjectMultiwordCommandsScript (interpreter))); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands () |
| 2218 | { |
| 2219 | } |
| 2220 | |