summaryrefslogtreecommitdiffstats
path: root/lldb/source/Commands/CommandObjectSet.cpp
blob: 46ad049fd1b5a3a7382c3e18e4adc5d296353f97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//===-- CommandObjectSet.cpp ------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "CommandObjectSet.h"

// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"

using namespace lldb;
using namespace lldb_private;

//-------------------------------------------------------------------------
// CommandObjectSet
//-------------------------------------------------------------------------

CommandObjectSet::CommandObjectSet () :
    CommandObject ("set",
                   "Allows the user to set or change the value of a single debugger setting variable.",
                   "set <setting_name> <value>")
{
}

CommandObjectSet::~CommandObjectSet()
{
}


bool
CommandObjectSet::Execute
(
    Args& command,
    CommandContext *context,
    CommandInterpreter *interpreter,
    CommandReturnObject &result
)
{
    CommandInterpreter::VariableMap::iterator pos;

    const int argc = command.GetArgumentCount();

    if (argc < 1)
    {
        result.AppendError ("'set' takes at least two arguments");
        result.SetStatus (eReturnStatusFailed);
        return false;
    }

    const char *var_name = command.GetArgumentAtIndex(0);
    const char *var_value = command.GetArgumentAtIndex(1);

    if (var_name == NULL || var_name[0] == '\0')
    {
        result.AppendError ("'set' command requires a valid variable name; No value supplied");
        result.SetStatus (eReturnStatusFailed);
    }
    else if (var_value == NULL || var_value[0] == '\0')
    {
        // No value given:  Check to see if we're trying to clear an array.
        StateVariable *var = interpreter->GetStateVariable (var_name);
        if (var != NULL
            && var->GetType() == StateVariable::eTypeStringArray)
        {
            var->ArrayClearValues();
            result.SetStatus (eReturnStatusSuccessFinishNoResult);
        }
        else
        {
            result.AppendError ("'set' command requires a valid variable value; No value supplied");
            result.SetStatus (eReturnStatusFailed);
        }
    }
    else
    {
        StateVariable *var = interpreter->GetStateVariable(var_name);
        if (var == NULL)
        {
            result.AppendErrorWithFormat ("'%s' is not a settable internal variable.\n", var_name);
            result.SetStatus (eReturnStatusFailed);
        }
        else
        {
            result.SetStatus (eReturnStatusSuccessFinishNoResult);
            if (var->GetType() == StateVariable::eTypeBoolean)
            {
                bool success = false;
                bool new_value = Args::StringToBoolean (var_value, false, &success);

                if (success)
                {
                    result.SetStatus(eReturnStatusSuccessFinishResult);
                    if (!var->HasVerifyFunction() || var->VerifyValue (interpreter, (void *) &new_value, result))
                        var->SetBoolValue (new_value);
                }
                else
                {
                    result.AppendErrorWithFormat ("Invalid boolean string '%s'.\n", var_value);
                    result.SetStatus (eReturnStatusFailed);
                }
            }
            else if (var->GetType() == StateVariable::eTypeInteger)
            {
                bool success = false;
                int new_value = Args::StringToSInt32(var_value, -1, 0, &success);

                if (success)
                {
                    result.SetStatus(eReturnStatusSuccessFinishResult);
                    if (!var->HasVerifyFunction() || var->VerifyValue (interpreter, (void *) &new_value, result))
                        var->SetIntValue (new_value);
                }
                else
                {
                    result.AppendErrorWithFormat ("Invalid boolean string '%s'.\n", var_value);
                    result.SetStatus (eReturnStatusFailed);
                }
            }
            else if (var->GetType() == StateVariable::eTypeString)
            {
                if (!var->HasVerifyFunction() || var->VerifyValue (interpreter, (void *) var_value, result))
                    var->SetStringValue (var_value);
            }
            else if (var->GetType() == StateVariable::eTypeStringArray)
            {
                if (var_value == NULL || var_value[0] == '\0')
                    var->ArrayClearValues ();
                else
                {
                    command.Shift(); // shift off variable name
                    var->ArrayClearValues(); // clear the old values
                    var->GetArgs().AppendArguments (command); // set the new values.
                }
            }
            else
            {
                result.AppendErrorWithFormat ("Variable '%s' has unrecognized type.\n",
                                              var->GetName());
                result.SetStatus (eReturnStatusFailed);
            }
        }
    }
    return result.Succeeded();
}

OpenPOWER on IntegriCloud