summaryrefslogtreecommitdiffstats
path: root/lldb/source/Interpreter/StateVariable.cpp
blob: 3f3c3fdb1e0e8a2fd6eba4e74ff1d015a153d76b (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//===-- StateVariable.cpp ---------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/CommandInterpreter.h"


#include "lldb/Interpreter/StateVariable.h"

using namespace lldb;
using namespace lldb_private;

// Variables with integer values.

StateVariable::StateVariable
(
    const char *name,
    int value,
    const char *help,
    Callback func_ptr
) :
    m_name (name),
    m_type (eTypeInteger),
    m_help_text (help),
    m_verification_func_ptr (func_ptr)
{
    m_int_value = value;
}

// Variables with boolean values.

StateVariable::StateVariable
(
    const char *name,
    bool value,
    const char *help,
    Callback func_ptr
 ) :
    m_name (name),
    m_type (eTypeBoolean),
    m_help_text (help),
    m_verification_func_ptr (func_ptr)
{
    m_int_value = value;
}

// Variables with string values.

StateVariable::StateVariable
(
    const char *name,
    const char *value,
    bool can_append,
    const char *help,
    Callback func_ptr
 ) :
    m_name (name),
    m_type (eTypeString),
    m_int_value (0),
    m_string_values (),
    m_help_text (help),
    m_verification_func_ptr (func_ptr)
{
    m_string_values.AppendArgument(value);
}

// Variables with array of strings values.

StateVariable::StateVariable
(
    const char *name,
    const Args *args,
    const char *help,
    Callback func_ptr
 ) :
    m_name (name),
    m_type (eTypeStringArray),
    m_help_text (help),
    m_string_values(),
    m_verification_func_ptr (func_ptr)
{
    if (args)
        m_string_values = *args;
}

StateVariable::~StateVariable ()
{
}

const char *
StateVariable::GetName () const
{
    return m_name.c_str();
}

StateVariable::Type
StateVariable::GetType () const
{
    return m_type;
}

int
StateVariable::GetIntValue () const
{
    return m_int_value;
}

bool
StateVariable::GetBoolValue () const
{
    return m_int_value;
}

const char *
StateVariable::GetStringValue () const
{
    return m_string_values.GetArgumentAtIndex(0);
}

const Args &
StateVariable::GetArgs () const
{
    return m_string_values;
}

Args &
StateVariable::GetArgs ()
{
    return m_string_values;
}

const char *
StateVariable::GetHelp () const
{
    return m_help_text.c_str();
}

void
StateVariable::SetHelp (const char *help)
{
    m_help_text = help;
}

void
StateVariable::AppendVariableInformation (CommandReturnObject &result)
{
    switch (m_type)
    {
    case eTypeBoolean:
        if (m_int_value)
            result.AppendMessageWithFormat ("    %s (bool) = True\n", m_name.c_str());
        else
            result.AppendMessageWithFormat ("    %s (bool) = False\n", m_name.c_str());
        break;

    case eTypeInteger:
        result.AppendMessageWithFormat ("    %s (int)  = %d\n", m_name.c_str(), m_int_value);
        break;

    case eTypeString:
        {
            const char *cstr = m_string_values.GetArgumentAtIndex(0);
            if (cstr && cstr[0])
                result.AppendMessageWithFormat ("    %s (str)  = '%s'\n", m_name.c_str(), cstr);
            else
                result.AppendMessageWithFormat ("    %s (str)  = <no value>\n", m_name.c_str());
        }
        break;

    case eTypeStringArray:
        {
            const size_t argc = m_string_values.GetArgumentCount();
            result.AppendMessageWithFormat ("    %s (string vector):\n", m_name.c_str());
            for (size_t i = 0; i < argc; ++i)
                result.AppendMessageWithFormat ("      [%d] %s\n", i, m_string_values.GetArgumentAtIndex(i));
        }
        break;

    default:
        break;
    }
}

void
StateVariable::SetStringValue (const char *new_value)
{
    if (m_string_values.GetArgumentCount() > 0)
        m_string_values.ReplaceArgumentAtIndex(0, new_value);
    else
        m_string_values.AppendArgument(new_value);
}

void
StateVariable::SetIntValue (int new_value)
{
    m_int_value = new_value;
}

void
StateVariable::SetBoolValue (bool new_value)
{
    m_int_value = new_value;
}

void
StateVariable::AppendStringValue (const char *cstr)
{
    if (cstr && cstr[0])
    {
        if (m_string_values.GetArgumentCount() == 0)
        {
            m_string_values.AppendArgument(cstr);
        }
        else
        {
            const char *curr_arg = m_string_values.GetArgumentAtIndex(0);
            if (curr_arg != NULL)
            {
                std::string new_arg_str(curr_arg);
                new_arg_str += " ";
                new_arg_str += cstr;
                m_string_values.ReplaceArgumentAtIndex(0, new_arg_str.c_str());
            }
            else
            {
                m_string_values.ReplaceArgumentAtIndex(0, cstr);
            }
        }
    }
}

bool
StateVariable::VerifyValue (CommandInterpreter *interpreter, void *data, CommandReturnObject &result)
{
    return (*m_verification_func_ptr) (interpreter, data, result);
}

//void
//StateVariable::SetArrayValue (STLStringArray &new_value)
//{
//    m_string_values.AppendArgument.append(cstr);
//
//    if (m_array_value != NULL)
//    {
//      if (m_array_value->size() > 0)
//      {
//          m_array_value->clear();
//      }
//    }
//    else
//        m_array_value = new STLStringArray;
//
//    for (int i = 0; i < new_value.size(); ++i)
//        m_array_value->push_back (new_value[i]);
//}
//

void
StateVariable::ArrayClearValues ()
{
    m_string_values.Clear();
}


void
StateVariable::ArrayAppendValue (const char *cstr)
{
    m_string_values.AppendArgument(cstr);
}


bool
StateVariable::HasVerifyFunction ()
{
    return (m_verification_func_ptr != NULL);
}

// Verification functions for various command interpreter variables.

bool
StateVariable::VerifyScriptLanguage (CommandInterpreter *interpreter, void *data, CommandReturnObject &result)
{
    bool valid_lang = true;
    interpreter->SetScriptLanguage (Args::StringToScriptLanguage((char *) data, eScriptLanguageDefault, &valid_lang));
    return valid_lang;
}

bool
StateVariable::BroadcastPromptChange (CommandInterpreter *interpreter, void *data, CommandReturnObject &result)
{
    char *prompt = (char *) data;
    if (prompt != NULL)
    {
        std::string tmp_prompt = prompt ;
        int len = tmp_prompt.size();
        if (len > 1
            && (tmp_prompt[0] == '\'' || tmp_prompt[0] == '"')
            && (tmp_prompt[len-1] == tmp_prompt[0]))
        {
            tmp_prompt = tmp_prompt.substr(1,len-2);
        }
        len = tmp_prompt.size();
        if (tmp_prompt[len-1] != ' ')
            tmp_prompt.append(" ");
        strcpy (prompt, tmp_prompt.c_str());
        data = (void *) prompt;
    }
    EventSP new_event_sp;
    new_event_sp.reset (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (prompt)));
    interpreter->BroadcastEvent (new_event_sp);

    return true;
}

OpenPOWER on IntegriCloud