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"
17 #include "lldb/Core/StringList.h"
18 #include "lldb/Host/PosixApi.h"
19 #include "lldb/Interpreter/Args.h"
20 #include "llvm/ADT/STLExtras.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 void OptionValueBoolean::DumpValue(const ExecutionContext *exe_ctx,
26                                    Stream &strm, uint32_t dump_mask) {
27   if (dump_mask & eDumpOptionType)
28     strm.Printf("(%s)", GetTypeAsCString());
29   //    if (dump_mask & eDumpOptionName)
30   //        DumpQualifiedName (strm);
31   if (dump_mask & eDumpOptionValue) {
32     if (dump_mask & eDumpOptionType)
33       strm.PutCString(" = ");
34     strm.PutCString(m_current_value ? "true" : "false");
35   }
36 }
37 
38 Error OptionValueBoolean::SetValueFromString(llvm::StringRef value_str,
39                                              VarSetOperationType op) {
40   Error error;
41   switch (op) {
42   case eVarSetOperationClear:
43     Clear();
44     NotifyValueChanged();
45     break;
46 
47   case eVarSetOperationReplace:
48   case eVarSetOperationAssign: {
49     bool success = false;
50     bool value = Args::StringToBoolean(value_str, false, &success);
51     if (success) {
52       m_value_was_set = true;
53       m_current_value = value;
54       NotifyValueChanged();
55     } else {
56       if (value_str.size() == 0)
57         error.SetErrorString("invalid boolean string value <empty>");
58       else
59         error.SetErrorStringWithFormat("invalid boolean string value: '%s'",
60                                        value_str.str().c_str());
61     }
62   } break;
63 
64   case eVarSetOperationInsertBefore:
65   case eVarSetOperationInsertAfter:
66   case eVarSetOperationRemove:
67   case eVarSetOperationAppend:
68   case eVarSetOperationInvalid:
69     error = OptionValue::SetValueFromString(value_str, op);
70     break;
71   }
72   return error;
73 }
74 
75 lldb::OptionValueSP OptionValueBoolean::DeepCopy() const {
76   return OptionValueSP(new OptionValueBoolean(*this));
77 }
78 
79 size_t OptionValueBoolean::AutoComplete(CommandInterpreter &interpreter,
80                                         const char *s, int match_start_point,
81                                         int max_return_elements,
82                                         bool &word_complete,
83                                         StringList &matches) {
84   word_complete = false;
85   matches.Clear();
86   struct StringEntry {
87     const char *string;
88     const size_t length;
89   };
90   static const StringEntry g_autocomplete_entries[] = {
91       {"true", 4}, {"false", 5}, {"on", 2}, {"off", 3},
92       {"yes", 3},  {"no", 2},    {"1", 1},  {"0", 1},
93   };
94   const size_t k_num_autocomplete_entries =
95       llvm::array_lengthof(g_autocomplete_entries);
96 
97   if (s && s[0]) {
98     const size_t s_len = strlen(s);
99     for (size_t i = 0; i < k_num_autocomplete_entries; ++i) {
100       if (s_len <= g_autocomplete_entries[i].length)
101         if (::strncasecmp(s, g_autocomplete_entries[i].string, s_len) == 0)
102           matches.AppendString(g_autocomplete_entries[i].string);
103     }
104   } else {
105     // only suggest "true" or "false" by default
106     for (size_t i = 0; i < 2; ++i)
107       matches.AppendString(g_autocomplete_entries[i].string);
108   }
109   return matches.GetSize();
110 }
111