1435933ddSDimitry Andric //===-- OptionValueString.cpp ------------------------------------*- C++
2435933ddSDimitry Andric //-*-===//
3ac7ddfbfSEd Maste //
4ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
5ac7ddfbfSEd Maste //
6ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
7ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
8ac7ddfbfSEd Maste //
9ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
10ac7ddfbfSEd Maste 
11ac7ddfbfSEd Maste #include "lldb/Interpreter/OptionValueString.h"
12ac7ddfbfSEd Maste 
13f678e45dSDimitry Andric #include "lldb/Host/OptionParser.h"
14*4ba319b5SDimitry Andric #include "lldb/Utility/Args.h"
15f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
16ac7ddfbfSEd Maste 
17ac7ddfbfSEd Maste using namespace lldb;
18ac7ddfbfSEd Maste using namespace lldb_private;
19ac7ddfbfSEd Maste 
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)20435933ddSDimitry Andric void OptionValueString::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
21435933ddSDimitry Andric                                   uint32_t dump_mask) {
22ac7ddfbfSEd Maste   if (dump_mask & eDumpOptionType)
23ac7ddfbfSEd Maste     strm.Printf("(%s)", GetTypeAsCString());
24435933ddSDimitry Andric   if (dump_mask & eDumpOptionValue) {
25ac7ddfbfSEd Maste     if (dump_mask & eDumpOptionType)
26ac7ddfbfSEd Maste       strm.PutCString(" = ");
27435933ddSDimitry Andric     if (!m_current_value.empty() || m_value_was_set) {
28435933ddSDimitry Andric       if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {
29ac7ddfbfSEd Maste         std::string expanded_escape_value;
30435933ddSDimitry Andric         Args::ExpandEscapedCharacters(m_current_value.c_str(),
31435933ddSDimitry Andric                                       expanded_escape_value);
32ac7ddfbfSEd Maste         if (dump_mask & eDumpOptionRaw)
33ac7ddfbfSEd Maste           strm.Printf("%s", expanded_escape_value.c_str());
34ac7ddfbfSEd Maste         else
35ac7ddfbfSEd Maste           strm.Printf("\"%s\"", expanded_escape_value.c_str());
36435933ddSDimitry Andric       } else {
37ac7ddfbfSEd Maste         if (dump_mask & eDumpOptionRaw)
38ac7ddfbfSEd Maste           strm.Printf("%s", m_current_value.c_str());
39ac7ddfbfSEd Maste         else
40ac7ddfbfSEd Maste           strm.Printf("\"%s\"", m_current_value.c_str());
41ac7ddfbfSEd Maste       }
42ac7ddfbfSEd Maste     }
43ac7ddfbfSEd Maste   }
44ac7ddfbfSEd Maste }
45ac7ddfbfSEd Maste 
SetValueFromString(llvm::StringRef value,VarSetOperationType op)465517e702SDimitry Andric Status OptionValueString::SetValueFromString(llvm::StringRef value,
47435933ddSDimitry Andric                                              VarSetOperationType op) {
485517e702SDimitry Andric   Status error;
49ac7ddfbfSEd Maste 
501c3bbb01SEd Maste   std::string value_str = value.str();
511c3bbb01SEd Maste   value = value.trim();
52435933ddSDimitry Andric   if (value.size() > 0) {
53435933ddSDimitry Andric     switch (value.front()) {
54ac7ddfbfSEd Maste     case '"':
55435933ddSDimitry Andric     case '\'': {
56435933ddSDimitry Andric       if (value.size() <= 1 || value.back() != value.front()) {
57ac7ddfbfSEd Maste         error.SetErrorString("mismatched quotes");
58ac7ddfbfSEd Maste         return error;
59ac7ddfbfSEd Maste       }
601c3bbb01SEd Maste       value = value.drop_front().drop_back();
61435933ddSDimitry Andric     } break;
62ac7ddfbfSEd Maste     }
631c3bbb01SEd Maste     value_str = value.str();
64ac7ddfbfSEd Maste   }
65ac7ddfbfSEd Maste 
66435933ddSDimitry Andric   switch (op) {
67ac7ddfbfSEd Maste   case eVarSetOperationInvalid:
68ac7ddfbfSEd Maste   case eVarSetOperationInsertBefore:
69ac7ddfbfSEd Maste   case eVarSetOperationInsertAfter:
70ac7ddfbfSEd Maste   case eVarSetOperationRemove:
71435933ddSDimitry Andric     if (m_validator) {
721c3bbb01SEd Maste       error = m_validator(value_str.c_str(), m_validator_baton);
73ac7ddfbfSEd Maste       if (error.Fail())
74ac7ddfbfSEd Maste         return error;
75ac7ddfbfSEd Maste     }
761c3bbb01SEd Maste     error = OptionValue::SetValueFromString(value, op);
77ac7ddfbfSEd Maste     break;
78ac7ddfbfSEd Maste 
79435933ddSDimitry Andric   case eVarSetOperationAppend: {
80ac7ddfbfSEd Maste     std::string new_value(m_current_value);
81435933ddSDimitry Andric     if (value.size() > 0) {
82435933ddSDimitry Andric       if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {
83ac7ddfbfSEd Maste         std::string str;
841c3bbb01SEd Maste         Args::EncodeEscapeSequences(value_str.c_str(), str);
85ac7ddfbfSEd Maste         new_value.append(str);
86435933ddSDimitry Andric       } else
871c3bbb01SEd Maste         new_value.append(value);
88ac7ddfbfSEd Maste     }
89435933ddSDimitry Andric     if (m_validator) {
90ac7ddfbfSEd Maste       error = m_validator(new_value.c_str(), m_validator_baton);
91ac7ddfbfSEd Maste       if (error.Fail())
92ac7ddfbfSEd Maste         return error;
93ac7ddfbfSEd Maste     }
94ac7ddfbfSEd Maste     m_current_value.assign(new_value);
957aa51b79SEd Maste     NotifyValueChanged();
96435933ddSDimitry Andric   } break;
97ac7ddfbfSEd Maste 
98ac7ddfbfSEd Maste   case eVarSetOperationClear:
99ac7ddfbfSEd Maste     Clear();
1007aa51b79SEd Maste     NotifyValueChanged();
101ac7ddfbfSEd Maste     break;
102ac7ddfbfSEd Maste 
103ac7ddfbfSEd Maste   case eVarSetOperationReplace:
104ac7ddfbfSEd Maste   case eVarSetOperationAssign:
105435933ddSDimitry Andric     if (m_validator) {
1061c3bbb01SEd Maste       error = m_validator(value_str.c_str(), m_validator_baton);
107ac7ddfbfSEd Maste       if (error.Fail())
108ac7ddfbfSEd Maste         return error;
109ac7ddfbfSEd Maste     }
110ac7ddfbfSEd Maste     m_value_was_set = true;
111435933ddSDimitry Andric     if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {
1121c3bbb01SEd Maste       Args::EncodeEscapeSequences(value_str.c_str(), m_current_value);
113435933ddSDimitry Andric     } else {
114435933ddSDimitry Andric       SetCurrentValue(value_str);
115ac7ddfbfSEd Maste     }
1167aa51b79SEd Maste     NotifyValueChanged();
117ac7ddfbfSEd Maste     break;
118ac7ddfbfSEd Maste   }
119ac7ddfbfSEd Maste   return error;
120ac7ddfbfSEd Maste }
121ac7ddfbfSEd Maste 
DeepCopy() const122435933ddSDimitry Andric lldb::OptionValueSP OptionValueString::DeepCopy() const {
123ac7ddfbfSEd Maste   return OptionValueSP(new OptionValueString(*this));
124ac7ddfbfSEd Maste }
125ac7ddfbfSEd Maste 
SetCurrentValue(llvm::StringRef value)1265517e702SDimitry Andric Status OptionValueString::SetCurrentValue(llvm::StringRef value) {
127435933ddSDimitry Andric   if (m_validator) {
1285517e702SDimitry Andric     Status error(m_validator(value.str().c_str(), m_validator_baton));
129ac7ddfbfSEd Maste     if (error.Fail())
130ac7ddfbfSEd Maste       return error;
131ac7ddfbfSEd Maste   }
132ac7ddfbfSEd Maste   m_current_value.assign(value);
1335517e702SDimitry Andric   return Status();
134ac7ddfbfSEd Maste }
135ac7ddfbfSEd Maste 
AppendToCurrentValue(const char * value)1365517e702SDimitry Andric Status OptionValueString::AppendToCurrentValue(const char *value) {
137435933ddSDimitry Andric   if (value && value[0]) {
138435933ddSDimitry Andric     if (m_validator) {
139ac7ddfbfSEd Maste       std::string new_value(m_current_value);
140ac7ddfbfSEd Maste       new_value.append(value);
1415517e702SDimitry Andric       Status error(m_validator(value, m_validator_baton));
142ac7ddfbfSEd Maste       if (error.Fail())
143ac7ddfbfSEd Maste         return error;
144ac7ddfbfSEd Maste       m_current_value.assign(new_value);
145435933ddSDimitry Andric     } else
146ac7ddfbfSEd Maste       m_current_value.append(value);
147ac7ddfbfSEd Maste   }
1485517e702SDimitry Andric   return Status();
149ac7ddfbfSEd Maste }
150