blob: dbbd46ea09380583156a5a949009cb996fd196bd [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- OptionValueBoolean.cpp ------------------------------------*- C++
2//-*-===//
Greg Clayton67cc0632012-08-22 17:17:09 +00003//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#include "lldb/Interpreter/OptionValueBoolean.h"
12
13// C Includes
14// C++ Includes
15// Other libraries and framework includes
16// Project includes
17#include "lldb/Core/Stream.h"
Greg Clayton754a9362012-08-23 00:22:02 +000018#include "lldb/Core/StringList.h"
Zachary Turnerf343968f2016-08-09 23:06:08 +000019#include "lldb/Host/PosixApi.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000020#include "lldb/Interpreter/Args.h"
Saleem Abdulrasool28606952014-06-27 05:17:41 +000021#include "llvm/ADT/STLExtras.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000022
23using namespace lldb;
24using namespace lldb_private;
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026void OptionValueBoolean::DumpValue(const ExecutionContext *exe_ctx,
27 Stream &strm, uint32_t dump_mask) {
28 if (dump_mask & eDumpOptionType)
29 strm.Printf("(%s)", GetTypeAsCString());
30 // if (dump_mask & eDumpOptionName)
31 // DumpQualifiedName (strm);
32 if (dump_mask & eDumpOptionValue) {
Greg Clayton67cc0632012-08-22 17:17:09 +000033 if (dump_mask & eDumpOptionType)
Kate Stoneb9c1b512016-09-06 20:57:50 +000034 strm.PutCString(" = ");
35 strm.PutCString(m_current_value ? "true" : "false");
36 }
Greg Clayton67cc0632012-08-22 17:17:09 +000037}
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039Error OptionValueBoolean::SetValueFromString(llvm::StringRef value_str,
40 VarSetOperationType op) {
41 Error error;
42 switch (op) {
43 case eVarSetOperationClear:
44 Clear();
45 NotifyValueChanged();
46 break;
Greg Clayton67cc0632012-08-22 17:17:09 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 case eVarSetOperationReplace:
49 case eVarSetOperationAssign: {
50 bool success = false;
51 bool value =
52 Args::StringToBoolean(value_str.str().c_str(), false, &success);
53 if (success) {
54 m_value_was_set = true;
55 m_current_value = value;
56 NotifyValueChanged();
57 } else {
58 if (value_str.size() == 0)
59 error.SetErrorString("invalid boolean string value <empty>");
60 else
61 error.SetErrorStringWithFormat("invalid boolean string value: '%s'",
62 value_str.str().c_str());
Greg Clayton67cc0632012-08-22 17:17:09 +000063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 } break;
65
66 case eVarSetOperationInsertBefore:
67 case eVarSetOperationInsertAfter:
68 case eVarSetOperationRemove:
69 case eVarSetOperationAppend:
70 case eVarSetOperationInvalid:
71 error = OptionValue::SetValueFromString(value_str, op);
72 break;
73 }
74 return error;
Greg Clayton67cc0632012-08-22 17:17:09 +000075}
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077lldb::OptionValueSP OptionValueBoolean::DeepCopy() const {
78 return OptionValueSP(new OptionValueBoolean(*this));
Greg Clayton67cc0632012-08-22 17:17:09 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081size_t OptionValueBoolean::AutoComplete(CommandInterpreter &interpreter,
82 const char *s, int match_start_point,
83 int max_return_elements,
84 bool &word_complete,
85 StringList &matches) {
86 word_complete = false;
87 matches.Clear();
88 struct StringEntry {
89 const char *string;
90 const size_t length;
91 };
92 static const StringEntry g_autocomplete_entries[] = {
93 {"true", 4}, {"false", 5}, {"on", 2}, {"off", 3},
94 {"yes", 3}, {"no", 2}, {"1", 1}, {"0", 1},
95 };
96 const size_t k_num_autocomplete_entries =
97 llvm::array_lengthof(g_autocomplete_entries);
98
99 if (s && s[0]) {
100 const size_t s_len = strlen(s);
101 for (size_t i = 0; i < k_num_autocomplete_entries; ++i) {
102 if (s_len <= g_autocomplete_entries[i].length)
103 if (::strncasecmp(s, g_autocomplete_entries[i].string, s_len) == 0)
104 matches.AppendString(g_autocomplete_entries[i].string);
Greg Clayton754a9362012-08-23 00:22:02 +0000105 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 } else {
107 // only suggest "true" or "false" by default
108 for (size_t i = 0; i < 2; ++i)
109 matches.AppendString(g_autocomplete_entries[i].string);
110 }
111 return matches.GetSize();
Greg Clayton754a9362012-08-23 00:22:02 +0000112}