blob: 89f154a083e4aad64590fa2058c4b2504ae3e091 [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(
Greg Clayton63094e02010-06-23 01:19:29 +000041 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +000042 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +000043 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 Clayton63094e02010-06-23 01:19:29 +000068 StateVariable *var = interpreter.GetStateVariable (var_name);
Chris Lattner24943d22010-06-08 16:52:24 +000069 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 Clayton63094e02010-06-23 01:19:29 +000083 StateVariable *var = interpreter.GetStateVariable(var_name);
Chris Lattner24943d22010-06-08 16:52:24 +000084 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 Clayton63094e02010-06-23 01:19:29 +0000100 if (!var->HasVerifyFunction() || var->VerifyValue (&interpreter, (void *) &new_value, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000101 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 Clayton63094e02010-06-23 01:19:29 +0000117 if (!var->HasVerifyFunction() || var->VerifyValue (&interpreter, (void *) &new_value, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000118 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 Clayton63094e02010-06-23 01:19:29 +0000128 if (!var->HasVerifyFunction() || var->VerifyValue (&interpreter, (void *) var_value, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000129 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