blob: b1b0ae8bfebc593ce64810f993075bff6765ca1b [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandObjectSettings.cpp -------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "CommandObjectSettings.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
Eugene Zelenko3f18ea02016-02-24 02:05:55 +000015#include "llvm/ADT/StringRef.h"
16
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017// Project includes
Zachary Turner3eb2b442017-03-22 23:33:16 +000018#include "lldb/Host/OptionParser.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000019#include "lldb/Interpreter/CommandCompletions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Interpreter/CommandInterpreter.h"
21#include "lldb/Interpreter/CommandReturnObject.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000022#include "lldb/Interpreter/OptionValueProperties.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24using namespace lldb;
25using namespace lldb_private;
Jim Ingham5a988412012-06-08 21:56:10 +000026
Jim Ingham5a988412012-06-08 21:56:10 +000027//-------------------------------------------------------------------------
28// CommandObjectSettingsSet
29//-------------------------------------------------------------------------
30
Zachary Turner1f0f5b52016-09-22 20:22:55 +000031static OptionDefinition g_settings_set_options[] = {
32 // clang-format off
33 { LLDB_OPT_SET_2, false, "global", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Apply the new value to the global default value." }
34 // clang-format on
35};
36
Kate Stoneb9c1b512016-09-06 20:57:50 +000037class CommandObjectSettingsSet : public CommandObjectRaw {
Jim Ingham5a988412012-06-08 21:56:10 +000038public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 CommandObjectSettingsSet(CommandInterpreter &interpreter)
40 : CommandObjectRaw(interpreter, "settings set",
Zachary Turnera4496982016-10-05 21:14:38 +000041 "Set the value of the specified debugger setting."),
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 m_options() {
43 CommandArgumentEntry arg1;
44 CommandArgumentEntry arg2;
45 CommandArgumentData var_name_arg;
46 CommandArgumentData value_arg;
Jim Ingham5a988412012-06-08 21:56:10 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 // Define the first (and only) variant of this arg.
49 var_name_arg.arg_type = eArgTypeSettingVariableName;
50 var_name_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +000051
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 // There is only one variant this argument could be; put it into the
53 // argument entry.
54 arg1.push_back(var_name_arg);
Jim Ingham5a988412012-06-08 21:56:10 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 // Define the first (and only) variant of this arg.
57 value_arg.arg_type = eArgTypeValue;
58 value_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 // There is only one variant this argument could be; put it into the
61 // argument entry.
62 arg2.push_back(value_arg);
Jim Ingham5a988412012-06-08 21:56:10 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 // Push the data for the first argument into the m_arguments vector.
65 m_arguments.push_back(arg1);
66 m_arguments.push_back(arg2);
67
68 SetHelpLong(
69 "\nWhen setting a dictionary or array variable, you can set multiple entries \
70at once by giving the values to the set command. For example:"
71 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +000072
73(lldb) settings set target.run-args value1 value2 value3
74(lldb) settings set target.env-vars MYPATH=~/.:/usr/bin SOME_ENV_VAR=12345
75
76(lldb) settings show target.run-args
77 [0]: 'value1'
78 [1]: 'value2'
79 [3]: 'value3'
80(lldb) settings show target.env-vars
81 'MYPATH=~/.:/usr/bin'
82 'SOME_ENV_VAR=12345'
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084)"
85 "Warning: The 'set' command re-sets the entire array or dictionary. If you \
Kate Stoneea671fb2015-07-14 05:48:36 +000086just want to add, remove or update individual values (or add something to \
87the end), use one of the other settings sub-commands: append, replace, \
Kate Stoneb9c1b512016-09-06 20:57:50 +000088insert-before or insert-after.");
89 }
Jim Ingham5a988412012-06-08 21:56:10 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 ~CommandObjectSettingsSet() override = default;
92
93 // Overrides base class's behavior where WantsCompletion =
94 // !WantsRawCommandString.
95 bool WantsCompletion() override { return true; }
96
97 Options *GetOptions() override { return &m_options; }
98
99 class CommandOptions : public Options {
100 public:
101 CommandOptions() : Options(), m_global(false) {}
102
103 ~CommandOptions() override = default;
104
Zachary Turner97206d52017-05-12 04:51:55 +0000105 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
106 ExecutionContext *execution_context) override {
107 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 const int short_option = m_getopt_table[option_idx].val;
109
110 switch (short_option) {
111 case 'g':
112 m_global = true;
113 break;
114 default:
115 error.SetErrorStringWithFormat("unrecognized options '%c'",
116 short_option);
117 break;
118 }
119
120 return error;
Jim Ingham5a988412012-06-08 21:56:10 +0000121 }
122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 void OptionParsingStarting(ExecutionContext *execution_context) override {
124 m_global = false;
Jim Ingham5a988412012-06-08 21:56:10 +0000125 }
Jim Ingham5a988412012-06-08 21:56:10 +0000126
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000127 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +0000128 return llvm::makeArrayRef(g_settings_set_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000129 }
Jim Ingham5a988412012-06-08 21:56:10 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 // Instance variables to hold the values for command options.
Jim Ingham5a988412012-06-08 21:56:10 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 bool m_global;
134 };
Jim Ingham5a988412012-06-08 21:56:10 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 int HandleArgumentCompletion(Args &input, int &cursor_index,
137 int &cursor_char_position,
138 OptionElementVector &opt_element_vector,
139 int match_start_point, int max_return_elements,
140 bool &word_complete,
141 StringList &matches) override {
142 std::string completion_str(input.GetArgumentAtIndex(cursor_index),
143 cursor_char_position);
Jim Ingham5a988412012-06-08 21:56:10 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 const size_t argc = input.GetArgumentCount();
146 const char *arg = nullptr;
147 int setting_var_idx;
Pavel Labath5f56fca2018-03-09 10:39:40 +0000148 for (setting_var_idx = 0; setting_var_idx < static_cast<int>(argc);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 ++setting_var_idx) {
150 arg = input.GetArgumentAtIndex(setting_var_idx);
151 if (arg && arg[0] != '-')
152 break; // We found our setting variable name index
Jim Ingham5a988412012-06-08 21:56:10 +0000153 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 if (cursor_index == setting_var_idx) {
155 // Attempting to complete setting variable name
156 CommandCompletions::InvokeCommonCompletionCallbacks(
157 GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion,
158 completion_str.c_str(), match_start_point, max_return_elements,
159 nullptr, word_complete, matches);
160 } else {
161 arg = input.GetArgumentAtIndex(cursor_index);
162
163 if (arg) {
164 if (arg[0] == '-') {
165 // Complete option name
166 } else {
167 // Complete setting value
168 const char *setting_var_name =
169 input.GetArgumentAtIndex(setting_var_idx);
Zachary Turner97206d52017-05-12 04:51:55 +0000170 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 lldb::OptionValueSP value_sp(
172 m_interpreter.GetDebugger().GetPropertyValue(
173 &m_exe_ctx, setting_var_name, false, error));
174 if (value_sp) {
175 value_sp->AutoComplete(m_interpreter, completion_str.c_str(),
176 match_start_point, max_return_elements,
177 word_complete, matches);
178 }
179 }
180 }
181 }
182 return matches.GetSize();
183 }
184
Jim Ingham5a988412012-06-08 21:56:10 +0000185protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 bool DoExecute(const char *command, CommandReturnObject &result) override {
187 Args cmd_args(command);
Jim Ingham5a988412012-06-08 21:56:10 +0000188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 // Process possible options.
190 if (!ParseOptions(cmd_args, result))
191 return false;
Jim Ingham5a988412012-06-08 21:56:10 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 const size_t argc = cmd_args.GetArgumentCount();
194 if ((argc < 2) && (!m_options.m_global)) {
195 result.AppendError("'settings set' takes more arguments");
196 result.SetStatus(eReturnStatusFailed);
197 return false;
Jim Ingham5a988412012-06-08 21:56:10 +0000198 }
Eugene Zelenko3f18ea02016-02-24 02:05:55 +0000199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 const char *var_name = cmd_args.GetArgumentAtIndex(0);
201 if ((var_name == nullptr) || (var_name[0] == '\0')) {
202 result.AppendError(
203 "'settings set' command requires a valid variable name");
204 result.SetStatus(eReturnStatusFailed);
205 return false;
206 }
207
208 // Split the raw command into var_name and value pair.
209 llvm::StringRef raw_str(command);
210 std::string var_value_string = raw_str.split(var_name).second.str();
211 const char *var_value_cstr =
212 Args::StripSpaces(var_value_string, true, false, false);
213
Zachary Turner97206d52017-05-12 04:51:55 +0000214 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 if (m_options.m_global) {
216 error = m_interpreter.GetDebugger().SetPropertyValue(
217 nullptr, eVarSetOperationAssign, var_name, var_value_cstr);
218 }
219
220 if (error.Success()) {
221 // FIXME this is the same issue as the one in commands script import
222 // we could be setting target.load-script-from-symbol-file which would
Adrian Prantl05097242018-04-30 16:49:04 +0000223 // cause Python scripts to be loaded, which could run LLDB commands (e.g.
224 // settings set target.process.python-os-plugin-path) and cause a crash
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 // if we did not clear the command's exe_ctx first
226 ExecutionContext exe_ctx(m_exe_ctx);
227 m_exe_ctx.Clear();
228 error = m_interpreter.GetDebugger().SetPropertyValue(
229 &exe_ctx, eVarSetOperationAssign, var_name, var_value_cstr);
230 }
231
232 if (error.Fail()) {
233 result.AppendError(error.AsCString());
234 result.SetStatus(eReturnStatusFailed);
235 return false;
236 } else {
237 result.SetStatus(eReturnStatusSuccessFinishResult);
238 }
239
240 return result.Succeeded();
241 }
242
Jim Ingham5a988412012-06-08 21:56:10 +0000243private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 CommandOptions m_options;
Jim Ingham5a988412012-06-08 21:56:10 +0000245};
246
Jim Ingham5a988412012-06-08 21:56:10 +0000247//-------------------------------------------------------------------------
248// CommandObjectSettingsShow -- Show current values
249//-------------------------------------------------------------------------
250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251class CommandObjectSettingsShow : public CommandObjectParsed {
Jim Ingham5a988412012-06-08 21:56:10 +0000252public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 CommandObjectSettingsShow(CommandInterpreter &interpreter)
254 : CommandObjectParsed(interpreter, "settings show",
255 "Show matching debugger settings and their current "
256 "values. Defaults to showing all settings.",
257 nullptr) {
258 CommandArgumentEntry arg1;
259 CommandArgumentData var_name_arg;
Jim Ingham5a988412012-06-08 21:56:10 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 // Define the first (and only) variant of this arg.
262 var_name_arg.arg_type = eArgTypeSettingVariableName;
263 var_name_arg.arg_repetition = eArgRepeatOptional;
Jim Ingham5a988412012-06-08 21:56:10 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 // There is only one variant this argument could be; put it into the
266 // argument entry.
267 arg1.push_back(var_name_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 // Push the data for the first argument into the m_arguments vector.
270 m_arguments.push_back(arg1);
271 }
Jim Ingham5a988412012-06-08 21:56:10 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 ~CommandObjectSettingsShow() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +0000274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 int HandleArgumentCompletion(Args &input, int &cursor_index,
276 int &cursor_char_position,
277 OptionElementVector &opt_element_vector,
278 int match_start_point, int max_return_elements,
279 bool &word_complete,
280 StringList &matches) override {
281 std::string completion_str(input.GetArgumentAtIndex(cursor_index),
282 cursor_char_position);
Jim Ingham5a988412012-06-08 21:56:10 +0000283
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 CommandCompletions::InvokeCommonCompletionCallbacks(
285 GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion,
286 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
287 word_complete, matches);
288 return matches.GetSize();
289 }
Jim Ingham5a988412012-06-08 21:56:10 +0000290
291protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 bool DoExecute(Args &args, CommandReturnObject &result) override {
293 result.SetStatus(eReturnStatusSuccessFinishResult);
Jim Ingham5a988412012-06-08 21:56:10 +0000294
Zachary Turner97d2c402016-10-05 23:40:23 +0000295 if (!args.empty()) {
Zachary Turnerd6a24752016-11-22 17:10:15 +0000296 for (const auto &arg : args) {
Zachary Turner97206d52017-05-12 04:51:55 +0000297 Status error(m_interpreter.GetDebugger().DumpPropertyValue(
Zachary Turnerd6a24752016-11-22 17:10:15 +0000298 &m_exe_ctx, result.GetOutputStream(), arg.ref,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 OptionValue::eDumpGroupValue));
300 if (error.Success()) {
301 result.GetOutputStream().EOL();
302 } else {
303 result.AppendError(error.AsCString());
304 result.SetStatus(eReturnStatusFailed);
Jim Ingham5a988412012-06-08 21:56:10 +0000305 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 }
307 } else {
308 m_interpreter.GetDebugger().DumpAllPropertyValues(
309 &m_exe_ctx, result.GetOutputStream(), OptionValue::eDumpGroupValue);
Jim Ingham5a988412012-06-08 21:56:10 +0000310 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311
312 return result.Succeeded();
313 }
Jim Ingham5a988412012-06-08 21:56:10 +0000314};
315
316//-------------------------------------------------------------------------
317// CommandObjectSettingsList -- List settable variables
318//-------------------------------------------------------------------------
319
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320class CommandObjectSettingsList : public CommandObjectParsed {
Kate Stone7428a182016-07-14 22:03:10 +0000321public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322 CommandObjectSettingsList(CommandInterpreter &interpreter)
323 : CommandObjectParsed(interpreter, "settings list",
324 "List and describe matching debugger settings. "
325 "Defaults to all listing all settings.",
326 nullptr) {
327 CommandArgumentEntry arg;
328 CommandArgumentData var_name_arg;
329 CommandArgumentData prefix_name_arg;
Jim Ingham5a988412012-06-08 21:56:10 +0000330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 // Define the first variant of this arg.
332 var_name_arg.arg_type = eArgTypeSettingVariableName;
333 var_name_arg.arg_repetition = eArgRepeatOptional;
Jim Ingham5a988412012-06-08 21:56:10 +0000334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 // Define the second variant of this arg.
336 prefix_name_arg.arg_type = eArgTypeSettingPrefix;
337 prefix_name_arg.arg_repetition = eArgRepeatOptional;
Jim Ingham5a988412012-06-08 21:56:10 +0000338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 arg.push_back(var_name_arg);
340 arg.push_back(prefix_name_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 // Push the data for the first argument into the m_arguments vector.
343 m_arguments.push_back(arg);
344 }
Jim Ingham5a988412012-06-08 21:56:10 +0000345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 ~CommandObjectSettingsList() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +0000347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 int HandleArgumentCompletion(Args &input, int &cursor_index,
349 int &cursor_char_position,
350 OptionElementVector &opt_element_vector,
351 int match_start_point, int max_return_elements,
352 bool &word_complete,
353 StringList &matches) override {
354 std::string completion_str(input.GetArgumentAtIndex(cursor_index),
355 cursor_char_position);
Jim Ingham5a988412012-06-08 21:56:10 +0000356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 CommandCompletions::InvokeCommonCompletionCallbacks(
358 GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion,
359 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
360 word_complete, matches);
361 return matches.GetSize();
362 }
Jim Ingham5a988412012-06-08 21:56:10 +0000363
364protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 bool DoExecute(Args &args, CommandReturnObject &result) override {
366 result.SetStatus(eReturnStatusSuccessFinishResult);
Jim Ingham5a988412012-06-08 21:56:10 +0000367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 const bool will_modify = false;
369 const size_t argc = args.GetArgumentCount();
370 if (argc > 0) {
371 const bool dump_qualified_name = true;
Jim Ingham5a988412012-06-08 21:56:10 +0000372
Zachary Turner97d2c402016-10-05 23:40:23 +0000373 // TODO: Convert to StringRef based enumeration. Requires converting
374 // GetPropertyAtPath first.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 for (size_t i = 0; i < argc; ++i) {
376 const char *property_path = args.GetArgumentAtIndex(i);
Greg Clayton67cc0632012-08-22 17:17:09 +0000377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 const Property *property =
379 m_interpreter.GetDebugger().GetValueProperties()->GetPropertyAtPath(
380 &m_exe_ctx, will_modify, property_path);
381
382 if (property) {
383 property->DumpDescription(m_interpreter, result.GetOutputStream(), 0,
384 dump_qualified_name);
385 } else {
386 result.AppendErrorWithFormat("invalid property path '%s'",
387 property_path);
388 result.SetStatus(eReturnStatusFailed);
Jim Ingham5a988412012-06-08 21:56:10 +0000389 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 }
391 } else {
392 m_interpreter.GetDebugger().DumpAllDescriptions(m_interpreter,
393 result.GetOutputStream());
Jim Ingham5a988412012-06-08 21:56:10 +0000394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395
396 return result.Succeeded();
397 }
Jim Ingham5a988412012-06-08 21:56:10 +0000398};
399
400//-------------------------------------------------------------------------
401// CommandObjectSettingsRemove
402//-------------------------------------------------------------------------
403
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404class CommandObjectSettingsRemove : public CommandObjectRaw {
Jim Ingham5a988412012-06-08 21:56:10 +0000405public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 CommandObjectSettingsRemove(CommandInterpreter &interpreter)
407 : CommandObjectRaw(interpreter, "settings remove",
408 "Remove a value from a setting, specified by array "
Zachary Turnera4496982016-10-05 21:14:38 +0000409 "index or dictionary key.") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 CommandArgumentEntry arg1;
411 CommandArgumentEntry arg2;
412 CommandArgumentData var_name_arg;
413 CommandArgumentData index_arg;
414 CommandArgumentData key_arg;
Jim Ingham5a988412012-06-08 21:56:10 +0000415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416 // Define the first (and only) variant of this arg.
417 var_name_arg.arg_type = eArgTypeSettingVariableName;
418 var_name_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 // There is only one variant this argument could be; put it into the
421 // argument entry.
422 arg1.push_back(var_name_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 // Define the first variant of this arg.
425 index_arg.arg_type = eArgTypeSettingIndex;
426 index_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 // Define the second variant of this arg.
429 key_arg.arg_type = eArgTypeSettingKey;
430 key_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 // Push both variants into this arg
433 arg2.push_back(index_arg);
434 arg2.push_back(key_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 // Push the data for the first argument into the m_arguments vector.
437 m_arguments.push_back(arg1);
438 m_arguments.push_back(arg2);
439 }
Jim Ingham5a988412012-06-08 21:56:10 +0000440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 ~CommandObjectSettingsRemove() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +0000442
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 int HandleArgumentCompletion(Args &input, int &cursor_index,
444 int &cursor_char_position,
445 OptionElementVector &opt_element_vector,
446 int match_start_point, int max_return_elements,
447 bool &word_complete,
448 StringList &matches) override {
449 std::string completion_str(input.GetArgumentAtIndex(cursor_index),
450 cursor_char_position);
Jim Ingham5a988412012-06-08 21:56:10 +0000451
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452 // Attempting to complete variable name
453 if (cursor_index < 2)
454 CommandCompletions::InvokeCommonCompletionCallbacks(
455 GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion,
456 completion_str.c_str(), match_start_point, max_return_elements,
457 nullptr, word_complete, matches);
458 return matches.GetSize();
459 }
Jim Ingham5a988412012-06-08 21:56:10 +0000460
461protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 bool DoExecute(const char *command, CommandReturnObject &result) override {
463 result.SetStatus(eReturnStatusSuccessFinishNoResult);
464
465 Args cmd_args(command);
466
467 // Process possible options.
468 if (!ParseOptions(cmd_args, result))
469 return false;
470
471 const size_t argc = cmd_args.GetArgumentCount();
472 if (argc == 0) {
473 result.AppendError("'settings set' takes an array or dictionary item, or "
474 "an array followed by one or more indexes, or a "
475 "dictionary followed by one or more key names to "
476 "remove");
477 result.SetStatus(eReturnStatusFailed);
478 return false;
Jim Ingham5a988412012-06-08 21:56:10 +0000479 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480
481 const char *var_name = cmd_args.GetArgumentAtIndex(0);
482 if ((var_name == nullptr) || (var_name[0] == '\0')) {
483 result.AppendError(
484 "'settings set' command requires a valid variable name");
485 result.SetStatus(eReturnStatusFailed);
486 return false;
487 }
488
489 // Split the raw command into var_name and value pair.
490 llvm::StringRef raw_str(command);
491 std::string var_value_string = raw_str.split(var_name).second.str();
492 const char *var_value_cstr =
493 Args::StripSpaces(var_value_string, true, true, false);
494
Zachary Turner97206d52017-05-12 04:51:55 +0000495 Status error(m_interpreter.GetDebugger().SetPropertyValue(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 &m_exe_ctx, eVarSetOperationRemove, var_name, var_value_cstr));
497 if (error.Fail()) {
498 result.AppendError(error.AsCString());
499 result.SetStatus(eReturnStatusFailed);
500 return false;
501 }
502
503 return result.Succeeded();
504 }
Jim Ingham5a988412012-06-08 21:56:10 +0000505};
506
507//-------------------------------------------------------------------------
508// CommandObjectSettingsReplace
509//-------------------------------------------------------------------------
510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511class CommandObjectSettingsReplace : public CommandObjectRaw {
Jim Ingham5a988412012-06-08 21:56:10 +0000512public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 CommandObjectSettingsReplace(CommandInterpreter &interpreter)
514 : CommandObjectRaw(interpreter, "settings replace",
515 "Replace the debugger setting value specified by "
Zachary Turnera4496982016-10-05 21:14:38 +0000516 "array index or dictionary key.") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 CommandArgumentEntry arg1;
518 CommandArgumentEntry arg2;
519 CommandArgumentEntry arg3;
520 CommandArgumentData var_name_arg;
521 CommandArgumentData index_arg;
522 CommandArgumentData key_arg;
523 CommandArgumentData value_arg;
Jim Ingham5a988412012-06-08 21:56:10 +0000524
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 // Define the first (and only) variant of this arg.
526 var_name_arg.arg_type = eArgTypeSettingVariableName;
527 var_name_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000528
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 // There is only one variant this argument could be; put it into the
530 // argument entry.
531 arg1.push_back(var_name_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000532
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 // Define the first (variant of this arg.
534 index_arg.arg_type = eArgTypeSettingIndex;
535 index_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000536
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537 // Define the second (variant of this arg.
538 key_arg.arg_type = eArgTypeSettingKey;
539 key_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000540
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541 // Put both variants into this arg
542 arg2.push_back(index_arg);
543 arg2.push_back(key_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 // Define the first (and only) variant of this arg.
546 value_arg.arg_type = eArgTypeValue;
547 value_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000548
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549 // There is only one variant this argument could be; put it into the
550 // argument entry.
551 arg3.push_back(value_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553 // Push the data for the first argument into the m_arguments vector.
554 m_arguments.push_back(arg1);
555 m_arguments.push_back(arg2);
556 m_arguments.push_back(arg3);
557 }
Jim Ingham5a988412012-06-08 21:56:10 +0000558
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 ~CommandObjectSettingsReplace() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +0000560
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 // Overrides base class's behavior where WantsCompletion =
562 // !WantsRawCommandString.
563 bool WantsCompletion() override { return true; }
Jim Ingham5a988412012-06-08 21:56:10 +0000564
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 int HandleArgumentCompletion(Args &input, int &cursor_index,
566 int &cursor_char_position,
567 OptionElementVector &opt_element_vector,
568 int match_start_point, int max_return_elements,
569 bool &word_complete,
570 StringList &matches) override {
571 std::string completion_str(input.GetArgumentAtIndex(cursor_index),
572 cursor_char_position);
Jim Ingham5a988412012-06-08 21:56:10 +0000573
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 // Attempting to complete variable name
575 if (cursor_index < 2)
576 CommandCompletions::InvokeCommonCompletionCallbacks(
577 GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion,
578 completion_str.c_str(), match_start_point, max_return_elements,
579 nullptr, word_complete, matches);
Jim Ingham5a988412012-06-08 21:56:10 +0000580
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 return matches.GetSize();
582 }
Jim Ingham5a988412012-06-08 21:56:10 +0000583
584protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 bool DoExecute(const char *command, CommandReturnObject &result) override {
586 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Jim Ingham5a988412012-06-08 21:56:10 +0000587
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 Args cmd_args(command);
589 const char *var_name = cmd_args.GetArgumentAtIndex(0);
590 if ((var_name == nullptr) || (var_name[0] == '\0')) {
591 result.AppendError("'settings replace' command requires a valid variable "
592 "name; No value supplied");
593 result.SetStatus(eReturnStatusFailed);
594 return false;
Jim Ingham5a988412012-06-08 21:56:10 +0000595 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596
597 // Split the raw command into var_name, index_value, and value triple.
598 llvm::StringRef raw_str(command);
599 std::string var_value_string = raw_str.split(var_name).second.str();
600 const char *var_value_cstr =
601 Args::StripSpaces(var_value_string, true, true, false);
602
Zachary Turner97206d52017-05-12 04:51:55 +0000603 Status error(m_interpreter.GetDebugger().SetPropertyValue(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604 &m_exe_ctx, eVarSetOperationReplace, var_name, var_value_cstr));
605 if (error.Fail()) {
606 result.AppendError(error.AsCString());
607 result.SetStatus(eReturnStatusFailed);
608 return false;
609 } else {
610 result.SetStatus(eReturnStatusSuccessFinishNoResult);
611 }
612
613 return result.Succeeded();
614 }
Jim Ingham5a988412012-06-08 21:56:10 +0000615};
616
617//-------------------------------------------------------------------------
618// CommandObjectSettingsInsertBefore
619//-------------------------------------------------------------------------
620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621class CommandObjectSettingsInsertBefore : public CommandObjectRaw {
Jim Ingham5a988412012-06-08 21:56:10 +0000622public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 CommandObjectSettingsInsertBefore(CommandInterpreter &interpreter)
624 : CommandObjectRaw(interpreter, "settings insert-before",
625 "Insert one or more values into an debugger array "
626 "setting immediately before the specified element "
Zachary Turnera4496982016-10-05 21:14:38 +0000627 "index.") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 CommandArgumentEntry arg1;
629 CommandArgumentEntry arg2;
630 CommandArgumentEntry arg3;
631 CommandArgumentData var_name_arg;
632 CommandArgumentData index_arg;
633 CommandArgumentData value_arg;
Jim Ingham5a988412012-06-08 21:56:10 +0000634
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 // Define the first (and only) variant of this arg.
636 var_name_arg.arg_type = eArgTypeSettingVariableName;
637 var_name_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000638
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639 // There is only one variant this argument could be; put it into the
640 // argument entry.
641 arg1.push_back(var_name_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000642
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 // Define the first (variant of this arg.
644 index_arg.arg_type = eArgTypeSettingIndex;
645 index_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 // There is only one variant this argument could be; put it into the
648 // argument entry.
649 arg2.push_back(index_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 // Define the first (and only) variant of this arg.
652 value_arg.arg_type = eArgTypeValue;
653 value_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000654
Kate Stoneb9c1b512016-09-06 20:57:50 +0000655 // There is only one variant this argument could be; put it into the
656 // argument entry.
657 arg3.push_back(value_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000658
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 // Push the data for the first argument into the m_arguments vector.
660 m_arguments.push_back(arg1);
661 m_arguments.push_back(arg2);
662 m_arguments.push_back(arg3);
663 }
Jim Ingham5a988412012-06-08 21:56:10 +0000664
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665 ~CommandObjectSettingsInsertBefore() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +0000666
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 // Overrides base class's behavior where WantsCompletion =
668 // !WantsRawCommandString.
669 bool WantsCompletion() override { return true; }
Jim Ingham5a988412012-06-08 21:56:10 +0000670
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671 int HandleArgumentCompletion(Args &input, int &cursor_index,
672 int &cursor_char_position,
673 OptionElementVector &opt_element_vector,
674 int match_start_point, int max_return_elements,
675 bool &word_complete,
676 StringList &matches) override {
677 std::string completion_str(input.GetArgumentAtIndex(cursor_index),
678 cursor_char_position);
Jim Ingham5a988412012-06-08 21:56:10 +0000679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 // Attempting to complete variable name
681 if (cursor_index < 2)
682 CommandCompletions::InvokeCommonCompletionCallbacks(
683 GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion,
684 completion_str.c_str(), match_start_point, max_return_elements,
685 nullptr, word_complete, matches);
Jim Ingham5a988412012-06-08 21:56:10 +0000686
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687 return matches.GetSize();
688 }
Jim Ingham5a988412012-06-08 21:56:10 +0000689
690protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 bool DoExecute(const char *command, CommandReturnObject &result) override {
692 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Jim Ingham5a988412012-06-08 21:56:10 +0000693
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694 Args cmd_args(command);
695 const size_t argc = cmd_args.GetArgumentCount();
Jim Ingham5a988412012-06-08 21:56:10 +0000696
Kate Stoneb9c1b512016-09-06 20:57:50 +0000697 if (argc < 3) {
698 result.AppendError("'settings insert-before' takes more arguments");
699 result.SetStatus(eReturnStatusFailed);
700 return false;
Jim Ingham5a988412012-06-08 21:56:10 +0000701 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702
703 const char *var_name = cmd_args.GetArgumentAtIndex(0);
704 if ((var_name == nullptr) || (var_name[0] == '\0')) {
705 result.AppendError("'settings insert-before' command requires a valid "
706 "variable name; No value supplied");
707 result.SetStatus(eReturnStatusFailed);
708 return false;
709 }
710
711 // Split the raw command into var_name, index_value, and value triple.
712 llvm::StringRef raw_str(command);
713 std::string var_value_string = raw_str.split(var_name).second.str();
714 const char *var_value_cstr =
715 Args::StripSpaces(var_value_string, true, true, false);
716
Zachary Turner97206d52017-05-12 04:51:55 +0000717 Status error(m_interpreter.GetDebugger().SetPropertyValue(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718 &m_exe_ctx, eVarSetOperationInsertBefore, var_name, var_value_cstr));
719 if (error.Fail()) {
720 result.AppendError(error.AsCString());
721 result.SetStatus(eReturnStatusFailed);
722 return false;
723 }
724
725 return result.Succeeded();
726 }
Jim Ingham5a988412012-06-08 21:56:10 +0000727};
728
729//-------------------------------------------------------------------------
730// CommandObjectSettingInsertAfter
731//-------------------------------------------------------------------------
732
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733class CommandObjectSettingsInsertAfter : public CommandObjectRaw {
Jim Ingham5a988412012-06-08 21:56:10 +0000734public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 CommandObjectSettingsInsertAfter(CommandInterpreter &interpreter)
736 : CommandObjectRaw(interpreter, "settings insert-after",
737 "Insert one or more values into a debugger array "
Zachary Turnera4496982016-10-05 21:14:38 +0000738 "settings after the specified element index.") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 CommandArgumentEntry arg1;
740 CommandArgumentEntry arg2;
741 CommandArgumentEntry arg3;
742 CommandArgumentData var_name_arg;
743 CommandArgumentData index_arg;
744 CommandArgumentData value_arg;
Jim Ingham5a988412012-06-08 21:56:10 +0000745
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 // Define the first (and only) variant of this arg.
747 var_name_arg.arg_type = eArgTypeSettingVariableName;
748 var_name_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000749
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750 // There is only one variant this argument could be; put it into the
751 // argument entry.
752 arg1.push_back(var_name_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000753
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 // Define the first (variant of this arg.
755 index_arg.arg_type = eArgTypeSettingIndex;
756 index_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000757
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 // There is only one variant this argument could be; put it into the
759 // argument entry.
760 arg2.push_back(index_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000761
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 // Define the first (and only) variant of this arg.
763 value_arg.arg_type = eArgTypeValue;
764 value_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000765
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 // There is only one variant this argument could be; put it into the
767 // argument entry.
768 arg3.push_back(value_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000769
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 // Push the data for the first argument into the m_arguments vector.
771 m_arguments.push_back(arg1);
772 m_arguments.push_back(arg2);
773 m_arguments.push_back(arg3);
774 }
Jim Ingham5a988412012-06-08 21:56:10 +0000775
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 ~CommandObjectSettingsInsertAfter() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +0000777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 // Overrides base class's behavior where WantsCompletion =
779 // !WantsRawCommandString.
780 bool WantsCompletion() override { return true; }
Jim Ingham5a988412012-06-08 21:56:10 +0000781
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782 int HandleArgumentCompletion(Args &input, int &cursor_index,
783 int &cursor_char_position,
784 OptionElementVector &opt_element_vector,
785 int match_start_point, int max_return_elements,
786 bool &word_complete,
787 StringList &matches) override {
788 std::string completion_str(input.GetArgumentAtIndex(cursor_index),
789 cursor_char_position);
Jim Ingham5a988412012-06-08 21:56:10 +0000790
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791 // Attempting to complete variable name
792 if (cursor_index < 2)
793 CommandCompletions::InvokeCommonCompletionCallbacks(
794 GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion,
795 completion_str.c_str(), match_start_point, max_return_elements,
796 nullptr, word_complete, matches);
Jim Ingham5a988412012-06-08 21:56:10 +0000797
Kate Stoneb9c1b512016-09-06 20:57:50 +0000798 return matches.GetSize();
799 }
800
Jim Ingham5a988412012-06-08 21:56:10 +0000801protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802 bool DoExecute(const char *command, CommandReturnObject &result) override {
803 result.SetStatus(eReturnStatusSuccessFinishNoResult);
Jim Ingham5a988412012-06-08 21:56:10 +0000804
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805 Args cmd_args(command);
806 const size_t argc = cmd_args.GetArgumentCount();
Jim Ingham5a988412012-06-08 21:56:10 +0000807
Kate Stoneb9c1b512016-09-06 20:57:50 +0000808 if (argc < 3) {
809 result.AppendError("'settings insert-after' takes more arguments");
810 result.SetStatus(eReturnStatusFailed);
811 return false;
Jim Ingham5a988412012-06-08 21:56:10 +0000812 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000813
814 const char *var_name = cmd_args.GetArgumentAtIndex(0);
815 if ((var_name == nullptr) || (var_name[0] == '\0')) {
816 result.AppendError("'settings insert-after' command requires a valid "
817 "variable name; No value supplied");
818 result.SetStatus(eReturnStatusFailed);
819 return false;
820 }
821
822 // Split the raw command into var_name, index_value, and value triple.
823 llvm::StringRef raw_str(command);
824 std::string var_value_string = raw_str.split(var_name).second.str();
825 const char *var_value_cstr =
826 Args::StripSpaces(var_value_string, true, true, false);
827
Zachary Turner97206d52017-05-12 04:51:55 +0000828 Status error(m_interpreter.GetDebugger().SetPropertyValue(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829 &m_exe_ctx, eVarSetOperationInsertAfter, var_name, var_value_cstr));
830 if (error.Fail()) {
831 result.AppendError(error.AsCString());
832 result.SetStatus(eReturnStatusFailed);
833 return false;
834 }
835
836 return result.Succeeded();
837 }
Jim Ingham5a988412012-06-08 21:56:10 +0000838};
839
840//-------------------------------------------------------------------------
841// CommandObjectSettingsAppend
842//-------------------------------------------------------------------------
843
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844class CommandObjectSettingsAppend : public CommandObjectRaw {
Jim Ingham5a988412012-06-08 21:56:10 +0000845public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846 CommandObjectSettingsAppend(CommandInterpreter &interpreter)
847 : CommandObjectRaw(interpreter, "settings append",
848 "Append one or more values to a debugger array, "
Zachary Turnera4496982016-10-05 21:14:38 +0000849 "dictionary, or string setting.") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850 CommandArgumentEntry arg1;
851 CommandArgumentEntry arg2;
852 CommandArgumentData var_name_arg;
853 CommandArgumentData value_arg;
Jim Ingham5a988412012-06-08 21:56:10 +0000854
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855 // Define the first (and only) variant of this arg.
856 var_name_arg.arg_type = eArgTypeSettingVariableName;
857 var_name_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000858
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 // There is only one variant this argument could be; put it into the
860 // argument entry.
861 arg1.push_back(var_name_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000862
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863 // Define the first (and only) variant of this arg.
864 value_arg.arg_type = eArgTypeValue;
865 value_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000866
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 // There is only one variant this argument could be; put it into the
868 // argument entry.
869 arg2.push_back(value_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000870
Kate Stoneb9c1b512016-09-06 20:57:50 +0000871 // Push the data for the first argument into the m_arguments vector.
872 m_arguments.push_back(arg1);
873 m_arguments.push_back(arg2);
874 }
Jim Ingham5a988412012-06-08 21:56:10 +0000875
Kate Stoneb9c1b512016-09-06 20:57:50 +0000876 ~CommandObjectSettingsAppend() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +0000877
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 // Overrides base class's behavior where WantsCompletion =
879 // !WantsRawCommandString.
880 bool WantsCompletion() override { return true; }
Jim Ingham5a988412012-06-08 21:56:10 +0000881
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882 int HandleArgumentCompletion(Args &input, int &cursor_index,
883 int &cursor_char_position,
884 OptionElementVector &opt_element_vector,
885 int match_start_point, int max_return_elements,
886 bool &word_complete,
887 StringList &matches) override {
888 std::string completion_str(input.GetArgumentAtIndex(cursor_index),
889 cursor_char_position);
Jim Ingham5a988412012-06-08 21:56:10 +0000890
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891 // Attempting to complete variable name
892 if (cursor_index < 2)
893 CommandCompletions::InvokeCommonCompletionCallbacks(
894 GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion,
895 completion_str.c_str(), match_start_point, max_return_elements,
896 nullptr, word_complete, matches);
Jim Ingham5a988412012-06-08 21:56:10 +0000897
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898 return matches.GetSize();
899 }
Jim Ingham5a988412012-06-08 21:56:10 +0000900
901protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902 bool DoExecute(const char *command, CommandReturnObject &result) override {
903 result.SetStatus(eReturnStatusSuccessFinishNoResult);
904 Args cmd_args(command);
905 const size_t argc = cmd_args.GetArgumentCount();
Jim Ingham5a988412012-06-08 21:56:10 +0000906
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907 if (argc < 2) {
908 result.AppendError("'settings append' takes more arguments");
909 result.SetStatus(eReturnStatusFailed);
910 return false;
Jim Ingham5a988412012-06-08 21:56:10 +0000911 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912
913 const char *var_name = cmd_args.GetArgumentAtIndex(0);
914 if ((var_name == nullptr) || (var_name[0] == '\0')) {
915 result.AppendError("'settings append' command requires a valid variable "
916 "name; No value supplied");
917 result.SetStatus(eReturnStatusFailed);
918 return false;
919 }
920
Adrian Prantl05097242018-04-30 16:49:04 +0000921 // Do not perform cmd_args.Shift() since StringRef is manipulating the raw
922 // character string later on.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923
924 // Split the raw command into var_name and value pair.
925 llvm::StringRef raw_str(command);
926 std::string var_value_string = raw_str.split(var_name).second.str();
927 const char *var_value_cstr =
928 Args::StripSpaces(var_value_string, true, true, false);
929
Zachary Turner97206d52017-05-12 04:51:55 +0000930 Status error(m_interpreter.GetDebugger().SetPropertyValue(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931 &m_exe_ctx, eVarSetOperationAppend, var_name, var_value_cstr));
932 if (error.Fail()) {
933 result.AppendError(error.AsCString());
934 result.SetStatus(eReturnStatusFailed);
935 return false;
936 }
937
938 return result.Succeeded();
939 }
Jim Ingham5a988412012-06-08 21:56:10 +0000940};
941
942//-------------------------------------------------------------------------
943// CommandObjectSettingsClear
944//-------------------------------------------------------------------------
945
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946class CommandObjectSettingsClear : public CommandObjectParsed {
Jim Ingham5a988412012-06-08 21:56:10 +0000947public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948 CommandObjectSettingsClear(CommandInterpreter &interpreter)
949 : CommandObjectParsed(
950 interpreter, "settings clear",
951 "Clear a debugger setting array, dictionary, or string.", nullptr) {
952 CommandArgumentEntry arg;
953 CommandArgumentData var_name_arg;
Jim Ingham5a988412012-06-08 21:56:10 +0000954
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 // Define the first (and only) variant of this arg.
956 var_name_arg.arg_type = eArgTypeSettingVariableName;
957 var_name_arg.arg_repetition = eArgRepeatPlain;
Jim Ingham5a988412012-06-08 21:56:10 +0000958
Kate Stoneb9c1b512016-09-06 20:57:50 +0000959 // There is only one variant this argument could be; put it into the
960 // argument entry.
961 arg.push_back(var_name_arg);
Jim Ingham5a988412012-06-08 21:56:10 +0000962
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 // Push the data for the first argument into the m_arguments vector.
964 m_arguments.push_back(arg);
965 }
Jim Ingham5a988412012-06-08 21:56:10 +0000966
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967 ~CommandObjectSettingsClear() override = default;
Jim Ingham5a988412012-06-08 21:56:10 +0000968
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 int HandleArgumentCompletion(Args &input, int &cursor_index,
970 int &cursor_char_position,
971 OptionElementVector &opt_element_vector,
972 int match_start_point, int max_return_elements,
973 bool &word_complete,
974 StringList &matches) override {
975 std::string completion_str(input.GetArgumentAtIndex(cursor_index),
976 cursor_char_position);
Jim Ingham5a988412012-06-08 21:56:10 +0000977
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 // Attempting to complete variable name
979 if (cursor_index < 2)
980 CommandCompletions::InvokeCommonCompletionCallbacks(
981 GetCommandInterpreter(), CommandCompletions::eSettingsNameCompletion,
982 completion_str.c_str(), match_start_point, max_return_elements,
983 nullptr, word_complete, matches);
Jim Ingham5a988412012-06-08 21:56:10 +0000984
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985 return matches.GetSize();
986 }
Jim Ingham5a988412012-06-08 21:56:10 +0000987
988protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000989 bool DoExecute(Args &command, CommandReturnObject &result) override {
990 result.SetStatus(eReturnStatusSuccessFinishNoResult);
991 const size_t argc = command.GetArgumentCount();
Jim Ingham5a988412012-06-08 21:56:10 +0000992
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 if (argc != 1) {
994 result.AppendError("'settings clear' takes exactly one argument");
995 result.SetStatus(eReturnStatusFailed);
996 return false;
Jim Ingham5a988412012-06-08 21:56:10 +0000997 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998
999 const char *var_name = command.GetArgumentAtIndex(0);
1000 if ((var_name == nullptr) || (var_name[0] == '\0')) {
1001 result.AppendError("'settings clear' command requires a valid variable "
1002 "name; No value supplied");
1003 result.SetStatus(eReturnStatusFailed);
1004 return false;
1005 }
1006
Zachary Turner97206d52017-05-12 04:51:55 +00001007 Status error(m_interpreter.GetDebugger().SetPropertyValue(
Zachary Turner31d97a52016-11-17 18:08:12 +00001008 &m_exe_ctx, eVarSetOperationClear, var_name, llvm::StringRef()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 if (error.Fail()) {
1010 result.AppendError(error.AsCString());
1011 result.SetStatus(eReturnStatusFailed);
1012 return false;
1013 }
1014
1015 return result.Succeeded();
1016 }
Jim Ingham5a988412012-06-08 21:56:10 +00001017};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018
1019//-------------------------------------------------------------------------
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001020// CommandObjectMultiwordSettings
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021//-------------------------------------------------------------------------
1022
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023CommandObjectMultiwordSettings::CommandObjectMultiwordSettings(
1024 CommandInterpreter &interpreter)
1025 : CommandObjectMultiword(interpreter, "settings",
1026 "Commands for managing LLDB settings.",
1027 "settings <subcommand> [<command-options>]") {
1028 LoadSubCommand("set",
1029 CommandObjectSP(new CommandObjectSettingsSet(interpreter)));
1030 LoadSubCommand("show",
1031 CommandObjectSP(new CommandObjectSettingsShow(interpreter)));
1032 LoadSubCommand("list",
1033 CommandObjectSP(new CommandObjectSettingsList(interpreter)));
1034 LoadSubCommand("remove",
1035 CommandObjectSP(new CommandObjectSettingsRemove(interpreter)));
1036 LoadSubCommand("replace", CommandObjectSP(
1037 new CommandObjectSettingsReplace(interpreter)));
1038 LoadSubCommand(
1039 "insert-before",
1040 CommandObjectSP(new CommandObjectSettingsInsertBefore(interpreter)));
1041 LoadSubCommand(
1042 "insert-after",
1043 CommandObjectSP(new CommandObjectSettingsInsertAfter(interpreter)));
1044 LoadSubCommand("append",
1045 CommandObjectSP(new CommandObjectSettingsAppend(interpreter)));
1046 LoadSubCommand("clear",
1047 CommandObjectSP(new CommandObjectSettingsClear(interpreter)));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001048}
1049
Eugene Zelenko3f18ea02016-02-24 02:05:55 +00001050CommandObjectMultiwordSettings::~CommandObjectMultiwordSettings() = default;