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