Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectSet.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 "CommandObjectSet.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 17 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
| 22 | //------------------------------------------------------------------------- |
| 23 | // CommandObjectSet |
| 24 | //------------------------------------------------------------------------- |
| 25 | |
| 26 | CommandObjectSet::CommandObjectSet () : |
| 27 | CommandObject ("set", |
| 28 | "Allows the user to set or change the value of a single debugger setting variable.", |
| 29 | "set <setting_name> <value>") |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | CommandObjectSet::~CommandObjectSet() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | |
| 38 | bool |
| 39 | CommandObjectSet::Execute |
| 40 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 41 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | CommandReturnObject &result |
| 44 | ) |
| 45 | { |
| 46 | CommandInterpreter::VariableMap::iterator pos; |
| 47 | |
| 48 | const int argc = command.GetArgumentCount(); |
| 49 | |
| 50 | if (argc < 1) |
| 51 | { |
| 52 | result.AppendError ("'set' takes at least two arguments"); |
| 53 | result.SetStatus (eReturnStatusFailed); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | const char *var_name = command.GetArgumentAtIndex(0); |
| 58 | const char *var_value = command.GetArgumentAtIndex(1); |
| 59 | |
| 60 | if (var_name == NULL || var_name[0] == '\0') |
| 61 | { |
| 62 | result.AppendError ("'set' command requires a valid variable name; No value supplied"); |
| 63 | result.SetStatus (eReturnStatusFailed); |
| 64 | } |
| 65 | else if (var_value == NULL || var_value[0] == '\0') |
| 66 | { |
| 67 | // No value given: Check to see if we're trying to clear an array. |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 68 | StateVariable *var = interpreter.GetStateVariable (var_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | if (var != NULL |
| 70 | && var->GetType() == StateVariable::eTypeStringArray) |
| 71 | { |
| 72 | var->ArrayClearValues(); |
| 73 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | result.AppendError ("'set' command requires a valid variable value; No value supplied"); |
| 78 | result.SetStatus (eReturnStatusFailed); |
| 79 | } |
| 80 | } |
| 81 | else |
| 82 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 83 | StateVariable *var = interpreter.GetStateVariable(var_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | if (var == NULL) |
| 85 | { |
| 86 | result.AppendErrorWithFormat ("'%s' is not a settable internal variable.\n", var_name); |
| 87 | result.SetStatus (eReturnStatusFailed); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 92 | if (var->GetType() == StateVariable::eTypeBoolean) |
| 93 | { |
| 94 | bool success = false; |
| 95 | bool new_value = Args::StringToBoolean (var_value, false, &success); |
| 96 | |
| 97 | if (success) |
| 98 | { |
| 99 | result.SetStatus(eReturnStatusSuccessFinishResult); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 100 | if (!var->HasVerifyFunction() || var->VerifyValue (&interpreter, (void *) &new_value, result)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | var->SetBoolValue (new_value); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | result.AppendErrorWithFormat ("Invalid boolean string '%s'.\n", var_value); |
| 106 | result.SetStatus (eReturnStatusFailed); |
| 107 | } |
| 108 | } |
| 109 | else if (var->GetType() == StateVariable::eTypeInteger) |
| 110 | { |
| 111 | bool success = false; |
| 112 | int new_value = Args::StringToSInt32(var_value, -1, 0, &success); |
| 113 | |
| 114 | if (success) |
| 115 | { |
| 116 | result.SetStatus(eReturnStatusSuccessFinishResult); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 117 | if (!var->HasVerifyFunction() || var->VerifyValue (&interpreter, (void *) &new_value, result)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | var->SetIntValue (new_value); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | result.AppendErrorWithFormat ("Invalid boolean string '%s'.\n", var_value); |
| 123 | result.SetStatus (eReturnStatusFailed); |
| 124 | } |
| 125 | } |
| 126 | else if (var->GetType() == StateVariable::eTypeString) |
| 127 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 128 | if (!var->HasVerifyFunction() || var->VerifyValue (&interpreter, (void *) var_value, result)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 129 | var->SetStringValue (var_value); |
| 130 | } |
| 131 | else if (var->GetType() == StateVariable::eTypeStringArray) |
| 132 | { |
| 133 | if (var_value == NULL || var_value[0] == '\0') |
| 134 | var->ArrayClearValues (); |
| 135 | else |
| 136 | { |
| 137 | command.Shift(); // shift off variable name |
| 138 | var->ArrayClearValues(); // clear the old values |
| 139 | var->GetArgs().AppendArguments (command); // set the new values. |
| 140 | } |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | result.AppendErrorWithFormat ("Variable '%s' has unrecognized type.\n", |
| 145 | var->GetName()); |
| 146 | result.SetStatus (eReturnStatusFailed); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | return result.Succeeded(); |
| 151 | } |
| 152 | |