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 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 
25 OptionValueFileSpec::OptionValueFileSpec (bool resolve) :
26     OptionValue(),
27     m_current_value (),
28     m_default_value (),
29     m_data_sp(),
30     m_data_mod_time (),
31     m_completion_mask (CommandCompletions::eDiskFileCompletion),
32     m_resolve (resolve)
33 {
34 }
35 
36 OptionValueFileSpec::OptionValueFileSpec (const FileSpec &value,
37                                           bool resolve) :
38     OptionValue(),
39     m_current_value (value),
40     m_default_value (value),
41     m_data_sp(),
42     m_data_mod_time (),
43     m_completion_mask (CommandCompletions::eDiskFileCompletion),
44     m_resolve (resolve)
45 {
46 }
47 
48 OptionValueFileSpec::OptionValueFileSpec (const FileSpec &current_value,
49                                           const FileSpec &default_value,
50                                           bool resolve) :
51     OptionValue(),
52     m_current_value (current_value),
53     m_default_value (default_value),
54     m_data_sp(),
55     m_data_mod_time (),
56     m_completion_mask (CommandCompletions::eDiskFileCompletion),
57     m_resolve (resolve)
58 {
59 }
60 
61 void
62 OptionValueFileSpec::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
63 {
64     if (dump_mask & eDumpOptionType)
65         strm.Printf ("(%s)", GetTypeAsCString ());
66     if (dump_mask & eDumpOptionValue)
67     {
68         if (dump_mask & eDumpOptionType)
69             strm.PutCString (" = ");
70 
71         if (m_current_value)
72         {
73             strm << '"' << m_current_value.GetPath().c_str() << '"';
74         }
75     }
76 }
77 
78 Error
79 OptionValueFileSpec::SetValueFromString (llvm::StringRef value,
80                                           VarSetOperationType op)
81 {
82     Error error;
83     switch (op)
84     {
85     case eVarSetOperationClear:
86         Clear ();
87         NotifyValueChanged();
88         break;
89 
90     case eVarSetOperationReplace:
91     case eVarSetOperationAssign:
92         if (value.size() > 0)
93         {
94             // The setting value may have whitespace, double-quotes, or single-quotes around the file
95             // path to indicate that internal spaces are not word breaks.  Strip off any ws & quotes
96             // from the start and end of the file path - we aren't doing any word // breaking here so
97             // the quoting is unnecessary.  NB this will cause a problem if someone tries to specify
98             // a file path that legitimately begins or ends with a " or ' character, or whitespace.
99             value = value.trim("\"' \t");
100             m_value_was_set = true;
101             m_current_value.SetFile(value.str().c_str(), m_resolve);
102             m_data_sp.reset();
103             m_data_mod_time.Clear();
104             NotifyValueChanged();
105         }
106         else
107         {
108             error.SetErrorString("invalid value string");
109         }
110         break;
111 
112     case eVarSetOperationInsertBefore:
113     case eVarSetOperationInsertAfter:
114     case eVarSetOperationRemove:
115     case eVarSetOperationAppend:
116     case eVarSetOperationInvalid:
117         error = OptionValue::SetValueFromString (value, op);
118         break;
119     }
120     return error;
121 }
122 
123 lldb::OptionValueSP
124 OptionValueFileSpec::DeepCopy () const
125 {
126     return OptionValueSP(new OptionValueFileSpec(*this));
127 }
128 
129 
130 size_t
131 OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter,
132                                    const char *s,
133                                    int match_start_point,
134                                    int max_return_elements,
135                                    bool &word_complete,
136                                    StringList &matches)
137 {
138     word_complete = false;
139     matches.Clear();
140     CommandCompletions::InvokeCommonCompletionCallbacks (interpreter,
141                                                          m_completion_mask,
142                                                          s,
143                                                          match_start_point,
144                                                          max_return_elements,
145                                                          nullptr,
146                                                          word_complete,
147                                                          matches);
148     return matches.GetSize();
149 }
150 
151 
152 
153 const lldb::DataBufferSP &
154 OptionValueFileSpec::GetFileContents(bool null_terminate)
155 {
156     if (m_current_value)
157     {
158         const TimeValue file_mod_time = m_current_value.GetModificationTime();
159         if (m_data_sp && m_data_mod_time == file_mod_time)
160             return m_data_sp;
161         if (null_terminate)
162             m_data_sp = m_current_value.ReadFileContentsAsCString();
163         else
164             m_data_sp = m_current_value.ReadFileContents();
165         m_data_mod_time = file_mod_time;
166     }
167     return m_data_sp;
168 }
169 
170 
171