1cfca06d7SDimitry Andric //===-- OptionValueString.cpp ---------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Interpreter/OptionValueString.h"
10f034231aSEd Maste
1174a628f7SDimitry Andric #include "lldb/Host/OptionParser.h"
12f73363f1SDimitry Andric #include "lldb/Utility/Args.h"
1374a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
14f034231aSEd Maste
15f034231aSEd Maste using namespace lldb;
16f034231aSEd Maste using namespace lldb_private;
17f034231aSEd Maste
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)1814f1b3e8SDimitry Andric void OptionValueString::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
1914f1b3e8SDimitry Andric uint32_t dump_mask) {
20f034231aSEd Maste if (dump_mask & eDumpOptionType)
21f034231aSEd Maste strm.Printf("(%s)", GetTypeAsCString());
2214f1b3e8SDimitry Andric if (dump_mask & eDumpOptionValue) {
23f034231aSEd Maste if (dump_mask & eDumpOptionType)
24f034231aSEd Maste strm.PutCString(" = ");
2514f1b3e8SDimitry Andric if (!m_current_value.empty() || m_value_was_set) {
2614f1b3e8SDimitry Andric if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {
27f034231aSEd Maste std::string expanded_escape_value;
2814f1b3e8SDimitry Andric Args::ExpandEscapedCharacters(m_current_value.c_str(),
2914f1b3e8SDimitry Andric expanded_escape_value);
30f034231aSEd Maste if (dump_mask & eDumpOptionRaw)
31f034231aSEd Maste strm.Printf("%s", expanded_escape_value.c_str());
32f034231aSEd Maste else
33f034231aSEd Maste strm.Printf("\"%s\"", expanded_escape_value.c_str());
3414f1b3e8SDimitry Andric } else {
35f034231aSEd Maste if (dump_mask & eDumpOptionRaw)
36f034231aSEd Maste strm.Printf("%s", m_current_value.c_str());
37f034231aSEd Maste else
38f034231aSEd Maste strm.Printf("\"%s\"", m_current_value.c_str());
39f034231aSEd Maste }
40f034231aSEd Maste }
41f034231aSEd Maste }
42f034231aSEd Maste }
43f034231aSEd Maste
SetValueFromString(llvm::StringRef value,VarSetOperationType op)44b76161e4SDimitry Andric Status OptionValueString::SetValueFromString(llvm::StringRef value,
4514f1b3e8SDimitry Andric VarSetOperationType op) {
46b76161e4SDimitry Andric Status error;
47f034231aSEd Maste
485e95aa85SEd Maste std::string value_str = value.str();
495e95aa85SEd Maste value = value.trim();
5014f1b3e8SDimitry Andric if (value.size() > 0) {
5114f1b3e8SDimitry Andric switch (value.front()) {
52f034231aSEd Maste case '"':
5314f1b3e8SDimitry Andric case '\'': {
5414f1b3e8SDimitry Andric if (value.size() <= 1 || value.back() != value.front()) {
55f034231aSEd Maste error.SetErrorString("mismatched quotes");
56f034231aSEd Maste return error;
57f034231aSEd Maste }
585e95aa85SEd Maste value = value.drop_front().drop_back();
5914f1b3e8SDimitry Andric } break;
60f034231aSEd Maste }
615e95aa85SEd Maste value_str = value.str();
62f034231aSEd Maste }
63f034231aSEd Maste
6414f1b3e8SDimitry Andric switch (op) {
65f034231aSEd Maste case eVarSetOperationInvalid:
66f034231aSEd Maste case eVarSetOperationInsertBefore:
67f034231aSEd Maste case eVarSetOperationInsertAfter:
68f034231aSEd Maste case eVarSetOperationRemove:
6914f1b3e8SDimitry Andric if (m_validator) {
705e95aa85SEd Maste error = m_validator(value_str.c_str(), m_validator_baton);
71f034231aSEd Maste if (error.Fail())
72f034231aSEd Maste return error;
73f034231aSEd Maste }
745e95aa85SEd Maste error = OptionValue::SetValueFromString(value, op);
75f034231aSEd Maste break;
76f034231aSEd Maste
7714f1b3e8SDimitry Andric case eVarSetOperationAppend: {
78f034231aSEd Maste std::string new_value(m_current_value);
7914f1b3e8SDimitry Andric if (value.size() > 0) {
8014f1b3e8SDimitry Andric if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {
81f034231aSEd Maste std::string str;
825e95aa85SEd Maste Args::EncodeEscapeSequences(value_str.c_str(), str);
83f034231aSEd Maste new_value.append(str);
8414f1b3e8SDimitry Andric } else
85cfca06d7SDimitry Andric new_value.append(std::string(value));
86f034231aSEd Maste }
8714f1b3e8SDimitry Andric if (m_validator) {
88f034231aSEd Maste error = m_validator(new_value.c_str(), m_validator_baton);
89f034231aSEd Maste if (error.Fail())
90f034231aSEd Maste return error;
91f034231aSEd Maste }
92f034231aSEd Maste m_current_value.assign(new_value);
93205afe67SEd Maste NotifyValueChanged();
9414f1b3e8SDimitry Andric } break;
95f034231aSEd Maste
96f034231aSEd Maste case eVarSetOperationClear:
97f034231aSEd Maste Clear();
98205afe67SEd Maste NotifyValueChanged();
99f034231aSEd Maste break;
100f034231aSEd Maste
101f034231aSEd Maste case eVarSetOperationReplace:
102f034231aSEd Maste case eVarSetOperationAssign:
10314f1b3e8SDimitry Andric if (m_validator) {
1045e95aa85SEd Maste error = m_validator(value_str.c_str(), m_validator_baton);
105f034231aSEd Maste if (error.Fail())
106f034231aSEd Maste return error;
107f034231aSEd Maste }
108f034231aSEd Maste m_value_was_set = true;
10914f1b3e8SDimitry Andric if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {
1105e95aa85SEd Maste Args::EncodeEscapeSequences(value_str.c_str(), m_current_value);
11114f1b3e8SDimitry Andric } else {
11214f1b3e8SDimitry Andric SetCurrentValue(value_str);
113f034231aSEd Maste }
114205afe67SEd Maste NotifyValueChanged();
115f034231aSEd Maste break;
116f034231aSEd Maste }
117f034231aSEd Maste return error;
118f034231aSEd Maste }
119f034231aSEd Maste
SetCurrentValue(llvm::StringRef value)120b76161e4SDimitry Andric Status OptionValueString::SetCurrentValue(llvm::StringRef value) {
12114f1b3e8SDimitry Andric if (m_validator) {
122b76161e4SDimitry Andric Status error(m_validator(value.str().c_str(), m_validator_baton));
123f034231aSEd Maste if (error.Fail())
124f034231aSEd Maste return error;
125f034231aSEd Maste }
126cfca06d7SDimitry Andric m_current_value.assign(std::string(value));
127b76161e4SDimitry Andric return Status();
128f034231aSEd Maste }
129f034231aSEd Maste
AppendToCurrentValue(const char * value)130b76161e4SDimitry Andric Status OptionValueString::AppendToCurrentValue(const char *value) {
13114f1b3e8SDimitry Andric if (value && value[0]) {
13214f1b3e8SDimitry Andric if (m_validator) {
133f034231aSEd Maste std::string new_value(m_current_value);
134f034231aSEd Maste new_value.append(value);
135b76161e4SDimitry Andric Status error(m_validator(value, m_validator_baton));
136f034231aSEd Maste if (error.Fail())
137f034231aSEd Maste return error;
138f034231aSEd Maste m_current_value.assign(new_value);
13914f1b3e8SDimitry Andric } else
140f034231aSEd Maste m_current_value.append(value);
141f034231aSEd Maste }
142b76161e4SDimitry Andric return Status();
143f034231aSEd Maste }
144