Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- StateVariable.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 "lldb/Interpreter/CommandReturnObject.h" |
| 11 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 12 | |
| 13 | |
| 14 | #include "lldb/Interpreter/StateVariable.h" |
| 15 | |
| 16 | using namespace lldb; |
| 17 | using namespace lldb_private; |
| 18 | |
| 19 | // Variables with integer values. |
| 20 | |
| 21 | StateVariable::StateVariable |
| 22 | ( |
| 23 | const char *name, |
| 24 | int value, |
| 25 | const char *help, |
| 26 | Callback func_ptr |
| 27 | ) : |
| 28 | m_name (name), |
| 29 | m_type (eTypeInteger), |
| 30 | m_help_text (help), |
| 31 | m_verification_func_ptr (func_ptr) |
| 32 | { |
| 33 | m_int_value = value; |
| 34 | } |
| 35 | |
| 36 | // Variables with boolean values. |
| 37 | |
| 38 | StateVariable::StateVariable |
| 39 | ( |
| 40 | const char *name, |
| 41 | bool value, |
| 42 | const char *help, |
| 43 | Callback func_ptr |
| 44 | ) : |
| 45 | m_name (name), |
| 46 | m_type (eTypeBoolean), |
| 47 | m_help_text (help), |
| 48 | m_verification_func_ptr (func_ptr) |
| 49 | { |
| 50 | m_int_value = value; |
| 51 | } |
| 52 | |
| 53 | // Variables with string values. |
| 54 | |
| 55 | StateVariable::StateVariable |
| 56 | ( |
| 57 | const char *name, |
| 58 | const char *value, |
| 59 | bool can_append, |
| 60 | const char *help, |
| 61 | Callback func_ptr |
| 62 | ) : |
| 63 | m_name (name), |
| 64 | m_type (eTypeString), |
| 65 | m_int_value (0), |
| 66 | m_string_values (), |
| 67 | m_help_text (help), |
| 68 | m_verification_func_ptr (func_ptr) |
| 69 | { |
| 70 | m_string_values.AppendArgument(value); |
| 71 | } |
| 72 | |
| 73 | // Variables with array of strings values. |
| 74 | |
| 75 | StateVariable::StateVariable |
| 76 | ( |
| 77 | const char *name, |
| 78 | const Args *args, |
| 79 | const char *help, |
| 80 | Callback func_ptr |
| 81 | ) : |
| 82 | m_name (name), |
| 83 | m_type (eTypeStringArray), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | m_string_values(), |
Eli Friedman | 7991225 | 2010-07-09 23:04:08 +0000 | [diff] [blame^] | 85 | m_help_text (help), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | m_verification_func_ptr (func_ptr) |
| 87 | { |
| 88 | if (args) |
| 89 | m_string_values = *args; |
| 90 | } |
| 91 | |
| 92 | StateVariable::~StateVariable () |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | const char * |
| 97 | StateVariable::GetName () const |
| 98 | { |
| 99 | return m_name.c_str(); |
| 100 | } |
| 101 | |
| 102 | StateVariable::Type |
| 103 | StateVariable::GetType () const |
| 104 | { |
| 105 | return m_type; |
| 106 | } |
| 107 | |
| 108 | int |
| 109 | StateVariable::GetIntValue () const |
| 110 | { |
| 111 | return m_int_value; |
| 112 | } |
| 113 | |
| 114 | bool |
| 115 | StateVariable::GetBoolValue () const |
| 116 | { |
| 117 | return m_int_value; |
| 118 | } |
| 119 | |
| 120 | const char * |
| 121 | StateVariable::GetStringValue () const |
| 122 | { |
| 123 | return m_string_values.GetArgumentAtIndex(0); |
| 124 | } |
| 125 | |
| 126 | const Args & |
| 127 | StateVariable::GetArgs () const |
| 128 | { |
| 129 | return m_string_values; |
| 130 | } |
| 131 | |
| 132 | Args & |
| 133 | StateVariable::GetArgs () |
| 134 | { |
| 135 | return m_string_values; |
| 136 | } |
| 137 | |
| 138 | const char * |
| 139 | StateVariable::GetHelp () const |
| 140 | { |
| 141 | return m_help_text.c_str(); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | StateVariable::SetHelp (const char *help) |
| 146 | { |
| 147 | m_help_text = help; |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | StateVariable::AppendVariableInformation (CommandReturnObject &result) |
| 152 | { |
| 153 | switch (m_type) |
| 154 | { |
| 155 | case eTypeBoolean: |
| 156 | if (m_int_value) |
| 157 | result.AppendMessageWithFormat (" %s (bool) = True\n", m_name.c_str()); |
| 158 | else |
| 159 | result.AppendMessageWithFormat (" %s (bool) = False\n", m_name.c_str()); |
| 160 | break; |
| 161 | |
| 162 | case eTypeInteger: |
| 163 | result.AppendMessageWithFormat (" %s (int) = %d\n", m_name.c_str(), m_int_value); |
| 164 | break; |
| 165 | |
| 166 | case eTypeString: |
| 167 | { |
| 168 | const char *cstr = m_string_values.GetArgumentAtIndex(0); |
| 169 | if (cstr && cstr[0]) |
| 170 | result.AppendMessageWithFormat (" %s (str) = '%s'\n", m_name.c_str(), cstr); |
| 171 | else |
| 172 | result.AppendMessageWithFormat (" %s (str) = <no value>\n", m_name.c_str()); |
| 173 | } |
| 174 | break; |
| 175 | |
| 176 | case eTypeStringArray: |
| 177 | { |
| 178 | const size_t argc = m_string_values.GetArgumentCount(); |
| 179 | result.AppendMessageWithFormat (" %s (string vector):\n", m_name.c_str()); |
| 180 | for (size_t i = 0; i < argc; ++i) |
| 181 | result.AppendMessageWithFormat (" [%d] %s\n", i, m_string_values.GetArgumentAtIndex(i)); |
| 182 | } |
| 183 | break; |
| 184 | |
| 185 | default: |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | StateVariable::SetStringValue (const char *new_value) |
| 192 | { |
| 193 | if (m_string_values.GetArgumentCount() > 0) |
| 194 | m_string_values.ReplaceArgumentAtIndex(0, new_value); |
| 195 | else |
| 196 | m_string_values.AppendArgument(new_value); |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | StateVariable::SetIntValue (int new_value) |
| 201 | { |
| 202 | m_int_value = new_value; |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | StateVariable::SetBoolValue (bool new_value) |
| 207 | { |
| 208 | m_int_value = new_value; |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | StateVariable::AppendStringValue (const char *cstr) |
| 213 | { |
| 214 | if (cstr && cstr[0]) |
| 215 | { |
| 216 | if (m_string_values.GetArgumentCount() == 0) |
| 217 | { |
| 218 | m_string_values.AppendArgument(cstr); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | const char *curr_arg = m_string_values.GetArgumentAtIndex(0); |
| 223 | if (curr_arg != NULL) |
| 224 | { |
| 225 | std::string new_arg_str(curr_arg); |
| 226 | new_arg_str += " "; |
| 227 | new_arg_str += cstr; |
| 228 | m_string_values.ReplaceArgumentAtIndex(0, new_arg_str.c_str()); |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | m_string_values.ReplaceArgumentAtIndex(0, cstr); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | bool |
| 239 | StateVariable::VerifyValue (CommandInterpreter *interpreter, void *data, CommandReturnObject &result) |
| 240 | { |
| 241 | return (*m_verification_func_ptr) (interpreter, data, result); |
| 242 | } |
| 243 | |
| 244 | //void |
| 245 | //StateVariable::SetArrayValue (STLStringArray &new_value) |
| 246 | //{ |
| 247 | // m_string_values.AppendArgument.append(cstr); |
| 248 | // |
| 249 | // if (m_array_value != NULL) |
| 250 | // { |
| 251 | // if (m_array_value->size() > 0) |
| 252 | // { |
| 253 | // m_array_value->clear(); |
| 254 | // } |
| 255 | // } |
| 256 | // else |
| 257 | // m_array_value = new STLStringArray; |
| 258 | // |
| 259 | // for (int i = 0; i < new_value.size(); ++i) |
| 260 | // m_array_value->push_back (new_value[i]); |
| 261 | //} |
| 262 | // |
| 263 | |
| 264 | void |
| 265 | StateVariable::ArrayClearValues () |
| 266 | { |
| 267 | m_string_values.Clear(); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | void |
| 272 | StateVariable::ArrayAppendValue (const char *cstr) |
| 273 | { |
| 274 | m_string_values.AppendArgument(cstr); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | bool |
| 279 | StateVariable::HasVerifyFunction () |
| 280 | { |
| 281 | return (m_verification_func_ptr != NULL); |
| 282 | } |
| 283 | |
| 284 | // Verification functions for various command interpreter variables. |
| 285 | |
| 286 | bool |
| 287 | StateVariable::VerifyScriptLanguage (CommandInterpreter *interpreter, void *data, CommandReturnObject &result) |
| 288 | { |
| 289 | bool valid_lang = true; |
| 290 | interpreter->SetScriptLanguage (Args::StringToScriptLanguage((char *) data, eScriptLanguageDefault, &valid_lang)); |
| 291 | return valid_lang; |
| 292 | } |
| 293 | |
| 294 | bool |
| 295 | StateVariable::BroadcastPromptChange (CommandInterpreter *interpreter, void *data, CommandReturnObject &result) |
| 296 | { |
| 297 | char *prompt = (char *) data; |
| 298 | if (prompt != NULL) |
| 299 | { |
| 300 | std::string tmp_prompt = prompt ; |
| 301 | int len = tmp_prompt.size(); |
| 302 | if (len > 1 |
| 303 | && (tmp_prompt[0] == '\'' || tmp_prompt[0] == '"') |
| 304 | && (tmp_prompt[len-1] == tmp_prompt[0])) |
| 305 | { |
| 306 | tmp_prompt = tmp_prompt.substr(1,len-2); |
| 307 | } |
| 308 | len = tmp_prompt.size(); |
| 309 | if (tmp_prompt[len-1] != ' ') |
| 310 | tmp_prompt.append(" "); |
| 311 | strcpy (prompt, tmp_prompt.c_str()); |
| 312 | data = (void *) prompt; |
| 313 | } |
| 314 | EventSP new_event_sp; |
| 315 | new_event_sp.reset (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (prompt))); |
| 316 | interpreter->BroadcastEvent (new_event_sp); |
| 317 | |
| 318 | return true; |
| 319 | } |
| 320 | |