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 #include "lldb/DataFormatters/FormatManager.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Interpreter/CommandCompletions.h"
15 #include "lldb/Interpreter/CommandInterpreter.h"
16 #include "lldb/Utility/Args.h"
17 #include "lldb/Utility/State.h"
18
19 using namespace lldb;
20 using namespace lldb_private;
21
OptionValueFileSpec(bool resolve)22 OptionValueFileSpec::OptionValueFileSpec(bool resolve)
23 : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
24 m_data_mod_time(),
25 m_completion_mask(CommandCompletions::eDiskFileCompletion),
26 m_resolve(resolve) {}
27
OptionValueFileSpec(const FileSpec & value,bool resolve)28 OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
29 : OptionValue(), m_current_value(value), m_default_value(value),
30 m_data_sp(), m_data_mod_time(),
31 m_completion_mask(CommandCompletions::eDiskFileCompletion),
32 m_resolve(resolve) {}
33
OptionValueFileSpec(const FileSpec & current_value,const FileSpec & default_value,bool resolve)34 OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_value,
35 const FileSpec &default_value,
36 bool resolve)
37 : OptionValue(), m_current_value(current_value),
38 m_default_value(default_value), m_data_sp(), m_data_mod_time(),
39 m_completion_mask(CommandCompletions::eDiskFileCompletion),
40 m_resolve(resolve) {}
41
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)42 void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
43 Stream &strm, uint32_t dump_mask) {
44 if (dump_mask & eDumpOptionType)
45 strm.Printf("(%s)", GetTypeAsCString());
46 if (dump_mask & eDumpOptionValue) {
47 if (dump_mask & eDumpOptionType)
48 strm.PutCString(" = ");
49
50 if (m_current_value) {
51 strm << '"' << m_current_value.GetPath().c_str() << '"';
52 }
53 }
54 }
55
SetValueFromString(llvm::StringRef value,VarSetOperationType op)56 Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
57 VarSetOperationType op) {
58 Status error;
59 switch (op) {
60 case eVarSetOperationClear:
61 Clear();
62 NotifyValueChanged();
63 break;
64
65 case eVarSetOperationReplace:
66 case eVarSetOperationAssign:
67 if (value.size() > 0) {
68 // The setting value may have whitespace, double-quotes, or single-quotes
69 // around the file path to indicate that internal spaces are not word
70 // breaks. Strip off any ws & quotes from the start and end of the file
71 // path - we aren't doing any word // breaking here so the quoting is
72 // unnecessary. NB this will cause a problem if someone tries to specify
73 // a file path that legitimately begins or ends with a " or ' character,
74 // or whitespace.
75 value = value.trim("\"' \t");
76 m_value_was_set = true;
77 m_current_value.SetFile(value.str(), FileSpec::Style::native);
78 if (m_resolve)
79 FileSystem::Instance().Resolve(m_current_value);
80 m_data_sp.reset();
81 m_data_mod_time = llvm::sys::TimePoint<>();
82 NotifyValueChanged();
83 } else {
84 error.SetErrorString("invalid value string");
85 }
86 break;
87
88 case eVarSetOperationInsertBefore:
89 case eVarSetOperationInsertAfter:
90 case eVarSetOperationRemove:
91 case eVarSetOperationAppend:
92 case eVarSetOperationInvalid:
93 error = OptionValue::SetValueFromString(value, op);
94 break;
95 }
96 return error;
97 }
98
DeepCopy() const99 lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
100 return OptionValueSP(new OptionValueFileSpec(*this));
101 }
102
AutoComplete(CommandInterpreter & interpreter,CompletionRequest & request)103 size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
104 CompletionRequest &request) {
105 request.SetWordComplete(false);
106 CommandCompletions::InvokeCommonCompletionCallbacks(
107 interpreter, m_completion_mask, request, nullptr);
108 return request.GetNumberOfMatches();
109 }
110
GetFileContents()111 const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
112 if (m_current_value) {
113 const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value);
114 if (m_data_sp && m_data_mod_time == file_mod_time)
115 return m_data_sp;
116 m_data_sp =
117 FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath());
118 m_data_mod_time = file_mod_time;
119 }
120 return m_data_sp;
121 }
122