blob: 149ccbc7b518c849615818e3537525ac7d7dd99d [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- OptionValueString.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/OptionValueString.h"
12
13// C Includes
14// C++ Includes
15// Other libraries and framework includes
16// Project includes
Zachary Turner3eb2b442017-03-22 23:33:16 +000017#include "lldb/Host/OptionParser.h"
Greg Clayton4c054102012-09-01 00:38:36 +000018#include "lldb/Interpreter/Args.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000019#include "lldb/Utility/Stream.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000020
21using namespace lldb;
22using namespace lldb_private;
23
Kate Stoneb9c1b512016-09-06 20:57:50 +000024void OptionValueString::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
25 uint32_t dump_mask) {
26 if (dump_mask & eDumpOptionType)
27 strm.Printf("(%s)", GetTypeAsCString());
28 if (dump_mask & eDumpOptionValue) {
Greg Clayton67cc0632012-08-22 17:17:09 +000029 if (dump_mask & eDumpOptionType)
Kate Stoneb9c1b512016-09-06 20:57:50 +000030 strm.PutCString(" = ");
31 if (!m_current_value.empty() || m_value_was_set) {
32 if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {
33 std::string expanded_escape_value;
34 Args::ExpandEscapedCharacters(m_current_value.c_str(),
35 expanded_escape_value);
36 if (dump_mask & eDumpOptionRaw)
37 strm.Printf("%s", expanded_escape_value.c_str());
Greg Clayton4c054102012-09-01 00:38:36 +000038 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 strm.Printf("\"%s\"", expanded_escape_value.c_str());
40 } else {
41 if (dump_mask & eDumpOptionRaw)
42 strm.Printf("%s", m_current_value.c_str());
Enrico Granata9d140842012-12-11 22:42:19 +000043 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 strm.Printf("\"%s\"", m_current_value.c_str());
45 }
Enrico Granata9d140842012-12-11 22:42:19 +000046 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 }
48}
49
50Error OptionValueString::SetValueFromString(llvm::StringRef value,
51 VarSetOperationType op) {
52 Error error;
53
54 std::string value_str = value.str();
55 value = value.trim();
56 if (value.size() > 0) {
57 switch (value.front()) {
58 case '"':
59 case '\'': {
60 if (value.size() <= 1 || value.back() != value.front()) {
61 error.SetErrorString("mismatched quotes");
62 return error;
63 }
64 value = value.drop_front().drop_back();
65 } break;
66 }
67 value_str = value.str();
68 }
69
70 switch (op) {
71 case eVarSetOperationInvalid:
72 case eVarSetOperationInsertBefore:
73 case eVarSetOperationInsertAfter:
74 case eVarSetOperationRemove:
75 if (m_validator) {
76 error = m_validator(value_str.c_str(), m_validator_baton);
77 if (error.Fail())
78 return error;
79 }
80 error = OptionValue::SetValueFromString(value, op);
81 break;
82
83 case eVarSetOperationAppend: {
84 std::string new_value(m_current_value);
85 if (value.size() > 0) {
86 if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {
87 std::string str;
88 Args::EncodeEscapeSequences(value_str.c_str(), str);
89 new_value.append(str);
90 } else
91 new_value.append(value);
92 }
93 if (m_validator) {
94 error = m_validator(new_value.c_str(), m_validator_baton);
95 if (error.Fail())
96 return error;
97 }
98 m_current_value.assign(new_value);
99 NotifyValueChanged();
100 } break;
101
102 case eVarSetOperationClear:
103 Clear();
104 NotifyValueChanged();
105 break;
106
107 case eVarSetOperationReplace:
108 case eVarSetOperationAssign:
109 if (m_validator) {
110 error = m_validator(value_str.c_str(), m_validator_baton);
111 if (error.Fail())
112 return error;
113 }
114 m_value_was_set = true;
115 if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {
116 Args::EncodeEscapeSequences(value_str.c_str(), m_current_value);
117 } else {
Zachary Turner8cef4b02016-09-23 17:48:13 +0000118 SetCurrentValue(value_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 }
120 NotifyValueChanged();
121 break;
122 }
123 return error;
124}
125
126lldb::OptionValueSP OptionValueString::DeepCopy() const {
127 return OptionValueSP(new OptionValueString(*this));
128}
129
Zachary Turner8cef4b02016-09-23 17:48:13 +0000130Error OptionValueString::SetCurrentValue(llvm::StringRef value) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 if (m_validator) {
Zachary Turner8cef4b02016-09-23 17:48:13 +0000132 Error error(m_validator(value.str().c_str(), m_validator_baton));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 if (error.Fail())
134 return error;
135 }
Zachary Turner8cef4b02016-09-23 17:48:13 +0000136 m_current_value.assign(value);
Mehdi Aminic1edf562016-11-11 04:29:25 +0000137 return Error();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138}
139
140Error OptionValueString::AppendToCurrentValue(const char *value) {
141 if (value && value[0]) {
142 if (m_validator) {
143 std::string new_value(m_current_value);
144 new_value.append(value);
145 Error error(m_validator(value, m_validator_baton));
146 if (error.Fail())
147 return error;
148 m_current_value.assign(new_value);
149 } else
150 m_current_value.append(value);
151 }
Mehdi Aminic1edf562016-11-11 04:29:25 +0000152 return Error();
Enrico Granata9d140842012-12-11 22:42:19 +0000153}