1*80814287SRaphael Isemann //===-- OptionValueBoolean.cpp --------------------------------------------===//
267cc0636SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
667cc0636SGreg Clayton //
767cc0636SGreg Clayton //===----------------------------------------------------------------------===//
867cc0636SGreg Clayton 
967cc0636SGreg Clayton #include "lldb/Interpreter/OptionValueBoolean.h"
1067cc0636SGreg Clayton 
11f343968fSZachary Turner #include "lldb/Host/PosixApi.h"
1247cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h"
13bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
14573ab909SZachary Turner #include "lldb/Utility/StringList.h"
1528606954SSaleem Abdulrasool #include "llvm/ADT/STLExtras.h"
1667cc0636SGreg Clayton 
1767cc0636SGreg Clayton using namespace lldb;
1867cc0636SGreg Clayton using namespace lldb_private;
1967cc0636SGreg Clayton 
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)20b9c1b51eSKate Stone void OptionValueBoolean::DumpValue(const ExecutionContext *exe_ctx,
21b9c1b51eSKate Stone                                    Stream &strm, uint32_t dump_mask) {
2267cc0636SGreg Clayton   if (dump_mask & eDumpOptionType)
2367cc0636SGreg Clayton     strm.Printf("(%s)", GetTypeAsCString());
2467cc0636SGreg Clayton   //    if (dump_mask & eDumpOptionName)
2567cc0636SGreg Clayton   //        DumpQualifiedName (strm);
26b9c1b51eSKate Stone   if (dump_mask & eDumpOptionValue) {
2767cc0636SGreg Clayton     if (dump_mask & eDumpOptionType)
2867cc0636SGreg Clayton       strm.PutCString(" = ");
2967cc0636SGreg Clayton     strm.PutCString(m_current_value ? "true" : "false");
3067cc0636SGreg Clayton   }
3167cc0636SGreg Clayton }
3267cc0636SGreg Clayton 
SetValueFromString(llvm::StringRef value_str,VarSetOperationType op)3397206d57SZachary Turner Status OptionValueBoolean::SetValueFromString(llvm::StringRef value_str,
34b9c1b51eSKate Stone                                               VarSetOperationType op) {
3597206d57SZachary Turner   Status error;
36b9c1b51eSKate Stone   switch (op) {
3767cc0636SGreg Clayton   case eVarSetOperationClear:
3867cc0636SGreg Clayton     Clear();
39332e8b1cSGreg Clayton     NotifyValueChanged();
4067cc0636SGreg Clayton     break;
4167cc0636SGreg Clayton 
4267cc0636SGreg Clayton   case eVarSetOperationReplace:
43b9c1b51eSKate Stone   case eVarSetOperationAssign: {
4467cc0636SGreg Clayton     bool success = false;
4547cbf4a0SPavel Labath     bool value = OptionArgParser::ToBoolean(value_str, false, &success);
46b9c1b51eSKate Stone     if (success) {
4767cc0636SGreg Clayton       m_value_was_set = true;
4867cc0636SGreg Clayton       m_current_value = value;
49332e8b1cSGreg Clayton       NotifyValueChanged();
50b9c1b51eSKate Stone     } else {
51c95f7e2aSPavel Labath       if (value_str.size() == 0)
5267cc0636SGreg Clayton         error.SetErrorString("invalid boolean string value <empty>");
5367cc0636SGreg Clayton       else
54c95f7e2aSPavel Labath         error.SetErrorStringWithFormat("invalid boolean string value: '%s'",
55c95f7e2aSPavel Labath                                        value_str.str().c_str());
5667cc0636SGreg Clayton     }
57b9c1b51eSKate Stone   } break;
5867cc0636SGreg Clayton 
5967cc0636SGreg Clayton   case eVarSetOperationInsertBefore:
6067cc0636SGreg Clayton   case eVarSetOperationInsertAfter:
6167cc0636SGreg Clayton   case eVarSetOperationRemove:
6267cc0636SGreg Clayton   case eVarSetOperationAppend:
6367cc0636SGreg Clayton   case eVarSetOperationInvalid:
64c95f7e2aSPavel Labath     error = OptionValue::SetValueFromString(value_str, op);
6567cc0636SGreg Clayton     break;
6667cc0636SGreg Clayton   }
6767cc0636SGreg Clayton   return error;
6867cc0636SGreg Clayton }
6967cc0636SGreg Clayton 
AutoComplete(CommandInterpreter & interpreter,CompletionRequest & request)70ae34ed2cSRaphael Isemann void OptionValueBoolean::AutoComplete(CommandInterpreter &interpreter,
71a2e76c0bSRaphael Isemann                                       CompletionRequest &request) {
72917b8df0SAdrian Prantl   llvm::StringRef autocomplete_entries[] = {"true", "false", "on", "off",
73917b8df0SAdrian Prantl                                             "yes",  "no",    "1",  "0"};
74754a9369SGreg Clayton 
75917b8df0SAdrian Prantl   auto entries = llvm::makeArrayRef(autocomplete_entries);
764aa8753cSZachary Turner 
77754a9369SGreg Clayton   // only suggest "true" or "false" by default
78a2e76c0bSRaphael Isemann   if (request.GetCursorArgumentPrefix().empty())
794aa8753cSZachary Turner     entries = entries.take_front(2);
804aa8753cSZachary Turner 
8193ca36d7SRaphael Isemann   for (auto entry : entries)
8293ca36d7SRaphael Isemann     request.TryCompleteCurrentArg(entry);
83754a9369SGreg Clayton }
84