Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1 | //===-- OptionValueBoolean.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/OptionValueBoolean.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Core/Stream.h" |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 17 | #include "lldb/Core/StringList.h" |
Zachary Turner | f343968f | 2016-08-09 23:06:08 +0000 | [diff] [blame^] | 18 | #include "lldb/Host/PosixApi.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 19 | #include "lldb/Interpreter/Args.h" |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
| 25 | void |
| 26 | OptionValueBoolean::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) |
| 27 | { |
| 28 | if (dump_mask & eDumpOptionType) |
| 29 | strm.Printf ("(%s)", GetTypeAsCString ()); |
| 30 | // if (dump_mask & eDumpOptionName) |
| 31 | // DumpQualifiedName (strm); |
| 32 | if (dump_mask & eDumpOptionValue) |
| 33 | { |
| 34 | if (dump_mask & eDumpOptionType) |
| 35 | strm.PutCString (" = "); |
| 36 | strm.PutCString (m_current_value ? "true" : "false"); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | Error |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 41 | OptionValueBoolean::SetValueFromString (llvm::StringRef value_str, |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 42 | VarSetOperationType op) |
| 43 | { |
| 44 | Error error; |
| 45 | switch (op) |
| 46 | { |
| 47 | case eVarSetOperationClear: |
| 48 | Clear(); |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 49 | NotifyValueChanged(); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 50 | break; |
| 51 | |
| 52 | case eVarSetOperationReplace: |
| 53 | case eVarSetOperationAssign: |
| 54 | { |
| 55 | bool success = false; |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 56 | bool value = Args::StringToBoolean(value_str.str().c_str(), false, &success); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 57 | if (success) |
| 58 | { |
| 59 | m_value_was_set = true; |
| 60 | m_current_value = value; |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 61 | NotifyValueChanged(); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 62 | } |
| 63 | else |
| 64 | { |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 65 | if (value_str.size() == 0) |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 66 | error.SetErrorString ("invalid boolean string value <empty>"); |
| 67 | else |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 68 | error.SetErrorStringWithFormat ("invalid boolean string value: '%s'", |
| 69 | value_str.str().c_str()); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | break; |
| 73 | |
| 74 | case eVarSetOperationInsertBefore: |
| 75 | case eVarSetOperationInsertAfter: |
| 76 | case eVarSetOperationRemove: |
| 77 | case eVarSetOperationAppend: |
| 78 | case eVarSetOperationInvalid: |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 79 | error = OptionValue::SetValueFromString (value_str, op); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 80 | break; |
| 81 | } |
| 82 | return error; |
| 83 | } |
| 84 | |
| 85 | lldb::OptionValueSP |
| 86 | OptionValueBoolean::DeepCopy () const |
| 87 | { |
| 88 | return OptionValueSP(new OptionValueBoolean(*this)); |
| 89 | } |
| 90 | |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 91 | size_t |
| 92 | OptionValueBoolean::AutoComplete (CommandInterpreter &interpreter, |
| 93 | const char *s, |
| 94 | int match_start_point, |
| 95 | int max_return_elements, |
| 96 | bool &word_complete, |
| 97 | StringList &matches) |
| 98 | { |
| 99 | word_complete = false; |
| 100 | matches.Clear(); |
| 101 | struct StringEntry { |
| 102 | const char *string; |
| 103 | const size_t length; |
| 104 | }; |
| 105 | static const StringEntry g_autocomplete_entries[] = |
| 106 | { |
| 107 | { "true" , 4 }, |
| 108 | { "false", 5 }, |
| 109 | { "on" , 2 }, |
| 110 | { "off" , 3 }, |
| 111 | { "yes" , 3 }, |
| 112 | { "no" , 2 }, |
| 113 | { "1" , 1 }, |
| 114 | { "0" , 1 }, |
| 115 | }; |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 116 | const size_t k_num_autocomplete_entries = llvm::array_lengthof(g_autocomplete_entries); |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 117 | |
| 118 | if (s && s[0]) |
| 119 | { |
| 120 | const size_t s_len = strlen(s); |
| 121 | for (size_t i=0; i<k_num_autocomplete_entries; ++i) |
| 122 | { |
| 123 | if (s_len <= g_autocomplete_entries[i].length) |
| 124 | if (::strncasecmp(s, g_autocomplete_entries[i].string, s_len) == 0) |
| 125 | matches.AppendString(g_autocomplete_entries[i].string); |
| 126 | } |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | // only suggest "true" or "false" by default |
| 131 | for (size_t i=0; i<2; ++i) |
| 132 | matches.AppendString(g_autocomplete_entries[i].string); |
| 133 | } |
| 134 | return matches.GetSize(); |
| 135 | } |
| 136 | |
| 137 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 138 | |