blob: 3a282f177fea14d809ad445d1b387aeb83f37fbd [file] [log] [blame]
Greg Clayton67cc0632012-08-22 17:17:09 +00001//===-- 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
Greg Clayton67cc0632012-08-22 17:17:09 +000016#include "lldb/Core/State.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000017#include "lldb/DataFormatters/FormatManager.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000018#include "lldb/Interpreter/Args.h"
19#include "lldb/Interpreter/CommandCompletions.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
Greg Claytonb5f0fea2012-09-27 22:26:11 +000024
Vince Harron1f4706c2015-02-18 23:12:26 +000025OptionValueFileSpec::OptionValueFileSpec (bool resolve) :
Greg Claytonb5f0fea2012-09-27 22:26:11 +000026 OptionValue(),
27 m_current_value (),
28 m_default_value (),
29 m_data_sp(),
Greg Clayton39fb1382015-03-06 23:46:54 +000030 m_data_mod_time (),
Vince Harron1f4706c2015-02-18 23:12:26 +000031 m_completion_mask (CommandCompletions::eDiskFileCompletion),
32 m_resolve (resolve)
Greg Claytonb5f0fea2012-09-27 22:26:11 +000033{
34}
35
Vince Harron1f4706c2015-02-18 23:12:26 +000036OptionValueFileSpec::OptionValueFileSpec (const FileSpec &value,
37 bool resolve) :
Greg Claytonb5f0fea2012-09-27 22:26:11 +000038 OptionValue(),
39 m_current_value (value),
40 m_default_value (value),
41 m_data_sp(),
Greg Clayton39fb1382015-03-06 23:46:54 +000042 m_data_mod_time (),
Vince Harron1f4706c2015-02-18 23:12:26 +000043 m_completion_mask (CommandCompletions::eDiskFileCompletion),
44 m_resolve (resolve)
Greg Claytonb5f0fea2012-09-27 22:26:11 +000045{
46}
47
48OptionValueFileSpec::OptionValueFileSpec (const FileSpec &current_value,
Vince Harron1f4706c2015-02-18 23:12:26 +000049 const FileSpec &default_value,
50 bool resolve) :
Greg Claytonb5f0fea2012-09-27 22:26:11 +000051 OptionValue(),
52 m_current_value (current_value),
53 m_default_value (default_value),
54 m_data_sp(),
Greg Clayton39fb1382015-03-06 23:46:54 +000055 m_data_mod_time (),
Vince Harron1f4706c2015-02-18 23:12:26 +000056 m_completion_mask (CommandCompletions::eDiskFileCompletion),
57 m_resolve (resolve)
Greg Claytonb5f0fea2012-09-27 22:26:11 +000058{
59}
60
Greg Clayton67cc0632012-08-22 17:17:09 +000061void
62OptionValueFileSpec::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 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +000073 strm << '"' << m_current_value.GetPath().c_str() << '"';
Greg Clayton67cc0632012-08-22 17:17:09 +000074 }
75 }
76}
77
78Error
Pavel Labathc95f7e22015-02-20 11:14:59 +000079OptionValueFileSpec::SetValueFromString (llvm::StringRef value,
Greg Clayton67cc0632012-08-22 17:17:09 +000080 VarSetOperationType op)
81{
82 Error error;
83 switch (op)
84 {
85 case eVarSetOperationClear:
86 Clear ();
Greg Clayton332e8b12015-01-13 21:13:08 +000087 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +000088 break;
89
90 case eVarSetOperationReplace:
91 case eVarSetOperationAssign:
Pavel Labathc95f7e22015-02-20 11:14:59 +000092 if (value.size() > 0)
Greg Clayton67cc0632012-08-22 17:17:09 +000093 {
Jason Molenda5c98b1c2013-09-13 02:33:15 +000094 // 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.
Pavel Labathc95f7e22015-02-20 11:14:59 +000099 value = value.trim("\"' \t");
Jason Molenda36d44612013-08-27 04:58:31 +0000100 m_value_was_set = true;
Pavel Labathc95f7e22015-02-20 11:14:59 +0000101 m_current_value.SetFile(value.str().c_str(), m_resolve);
Jim Ingham2e3881c2014-04-04 18:06:54 +0000102 m_data_sp.reset();
Greg Clayton39fb1382015-03-06 23:46:54 +0000103 m_data_mod_time.Clear();
Greg Clayton332e8b12015-01-13 21:13:08 +0000104 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +0000105 }
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:
Pavel Labathc95f7e22015-02-20 11:14:59 +0000117 error = OptionValue::SetValueFromString (value, op);
Greg Clayton67cc0632012-08-22 17:17:09 +0000118 break;
119 }
120 return error;
121}
122
123lldb::OptionValueSP
124OptionValueFileSpec::DeepCopy () const
125{
126 return OptionValueSP(new OptionValueFileSpec(*this));
127}
128
129
130size_t
131OptionValueFileSpec::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,
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000141 m_completion_mask,
Greg Clayton67cc0632012-08-22 17:17:09 +0000142 s,
143 match_start_point,
144 max_return_elements,
Ed Masted78c9572014-04-20 00:31:37 +0000145 nullptr,
Greg Clayton67cc0632012-08-22 17:17:09 +0000146 word_complete,
147 matches);
148 return matches.GetSize();
149}
150
151
152
Greg Clayton6920b522012-08-22 18:39:03 +0000153const lldb::DataBufferSP &
Greg Clayton0b0b5122012-08-30 18:15:10 +0000154OptionValueFileSpec::GetFileContents(bool null_terminate)
Greg Clayton6920b522012-08-22 18:39:03 +0000155{
Greg Clayton39fb1382015-03-06 23:46:54 +0000156 if (m_current_value)
Greg Clayton0b0b5122012-08-30 18:15:10 +0000157 {
Greg Clayton39fb1382015-03-06 23:46:54 +0000158 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;
Greg Clayton0b0b5122012-08-30 18:15:10 +0000161 if (null_terminate)
162 m_data_sp = m_current_value.ReadFileContentsAsCString();
163 else
164 m_data_sp = m_current_value.ReadFileContents();
Greg Clayton39fb1382015-03-06 23:46:54 +0000165 m_data_mod_time = file_mod_time;
Greg Clayton0b0b5122012-08-30 18:15:10 +0000166 }
Greg Clayton6920b522012-08-22 18:39:03 +0000167 return m_data_sp;
168}
169
Greg Clayton67cc0632012-08-22 17:17:09 +0000170