1 //===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Interpreter/OptionValueFileSpec.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/State.h"
17 #include "lldb/DataFormatters/FormatManager.h"
18 #include "lldb/Interpreter/Args.h"
19 #include "lldb/Interpreter/CommandCompletions.h"
20 #include "lldb/Interpreter/CommandInterpreter.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 OptionValueFileSpec::OptionValueFileSpec(bool resolve)
26     : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
27       m_data_mod_time(),
28       m_completion_mask(CommandCompletions::eDiskFileCompletion),
29       m_resolve(resolve) {}
30 
31 OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
32     : OptionValue(), m_current_value(value), m_default_value(value),
33       m_data_sp(), m_data_mod_time(),
34       m_completion_mask(CommandCompletions::eDiskFileCompletion),
35       m_resolve(resolve) {}
36 
37 OptionValueFileSpec::OptionValueFileSpec(const FileSpec &current_value,
38                                          const FileSpec &default_value,
39                                          bool resolve)
40     : OptionValue(), m_current_value(current_value),
41       m_default_value(default_value), m_data_sp(), m_data_mod_time(),
42       m_completion_mask(CommandCompletions::eDiskFileCompletion),
43       m_resolve(resolve) {}
44 
45 void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
46                                     Stream &strm, uint32_t dump_mask) {
47   if (dump_mask & eDumpOptionType)
48     strm.Printf("(%s)", GetTypeAsCString());
49   if (dump_mask & eDumpOptionValue) {
50     if (dump_mask & eDumpOptionType)
51       strm.PutCString(" = ");
52 
53     if (m_current_value) {
54       strm << '"' << m_current_value.GetPath().c_str() << '"';
55     }
56   }
57 }
58 
59 Error OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
60                                               VarSetOperationType op) {
61   Error error;
62   switch (op) {
63   case eVarSetOperationClear:
64     Clear();
65     NotifyValueChanged();
66     break;
67 
68   case eVarSetOperationReplace:
69   case eVarSetOperationAssign:
70     if (value.size() > 0) {
71       // The setting value may have whitespace, double-quotes, or single-quotes
72       // around the file
73       // path to indicate that internal spaces are not word breaks.  Strip off
74       // any ws & quotes
75       // from the start and end of the file path - we aren't doing any word //
76       // breaking here so
77       // the quoting is unnecessary.  NB this will cause a problem if someone
78       // tries to specify
79       // a file path that legitimately begins or ends with a " or ' character,
80       // or whitespace.
81       value = value.trim("\"' \t");
82       m_value_was_set = true;
83       m_current_value.SetFile(value.str().c_str(), m_resolve);
84       m_data_sp.reset();
85       m_data_mod_time.Clear();
86       NotifyValueChanged();
87     } else {
88       error.SetErrorString("invalid value string");
89     }
90     break;
91 
92   case eVarSetOperationInsertBefore:
93   case eVarSetOperationInsertAfter:
94   case eVarSetOperationRemove:
95   case eVarSetOperationAppend:
96   case eVarSetOperationInvalid:
97     error = OptionValue::SetValueFromString(value, op);
98     break;
99   }
100   return error;
101 }
102 
103 lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
104   return OptionValueSP(new OptionValueFileSpec(*this));
105 }
106 
107 size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
108                                          const char *s, int match_start_point,
109                                          int max_return_elements,
110                                          bool &word_complete,
111                                          StringList &matches) {
112   word_complete = false;
113   matches.Clear();
114   CommandCompletions::InvokeCommonCompletionCallbacks(
115       interpreter, m_completion_mask, s, match_start_point, max_return_elements,
116       nullptr, word_complete, matches);
117   return matches.GetSize();
118 }
119 
120 const lldb::DataBufferSP &
121 OptionValueFileSpec::GetFileContents(bool null_terminate) {
122   if (m_current_value) {
123     const TimeValue file_mod_time = m_current_value.GetModificationTime();
124     if (m_data_sp && m_data_mod_time == file_mod_time)
125       return m_data_sp;
126     if (null_terminate)
127       m_data_sp = m_current_value.ReadFileContentsAsCString();
128     else
129       m_data_sp = m_current_value.ReadFileContents();
130     m_data_mod_time = file_mod_time;
131   }
132   return m_data_sp;
133 }
134