blob: 2b0af02e96aa7655060e7539b3fe3b88a37112e1 [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"
Todd Fialae1cfbc72016-08-11 23:51:28 +000020#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000021
22using namespace lldb;
23using namespace lldb_private;
24
Greg Claytonb5f0fea2012-09-27 22:26:11 +000025
Vince Harron1f4706c2015-02-18 23:12:26 +000026OptionValueFileSpec::OptionValueFileSpec (bool resolve) :
Greg Claytonb5f0fea2012-09-27 22:26:11 +000027 OptionValue(),
28 m_current_value (),
29 m_default_value (),
30 m_data_sp(),
Greg Clayton39fb1382015-03-06 23:46:54 +000031 m_data_mod_time (),
Vince Harron1f4706c2015-02-18 23:12:26 +000032 m_completion_mask (CommandCompletions::eDiskFileCompletion),
33 m_resolve (resolve)
Greg Claytonb5f0fea2012-09-27 22:26:11 +000034{
35}
36
Vince Harron1f4706c2015-02-18 23:12:26 +000037OptionValueFileSpec::OptionValueFileSpec (const FileSpec &value,
38 bool resolve) :
Greg Claytonb5f0fea2012-09-27 22:26:11 +000039 OptionValue(),
40 m_current_value (value),
41 m_default_value (value),
42 m_data_sp(),
Greg Clayton39fb1382015-03-06 23:46:54 +000043 m_data_mod_time (),
Vince Harron1f4706c2015-02-18 23:12:26 +000044 m_completion_mask (CommandCompletions::eDiskFileCompletion),
45 m_resolve (resolve)
Greg Claytonb5f0fea2012-09-27 22:26:11 +000046{
47}
48
49OptionValueFileSpec::OptionValueFileSpec (const FileSpec &current_value,
Vince Harron1f4706c2015-02-18 23:12:26 +000050 const FileSpec &default_value,
51 bool resolve) :
Greg Claytonb5f0fea2012-09-27 22:26:11 +000052 OptionValue(),
53 m_current_value (current_value),
54 m_default_value (default_value),
55 m_data_sp(),
Greg Clayton39fb1382015-03-06 23:46:54 +000056 m_data_mod_time (),
Vince Harron1f4706c2015-02-18 23:12:26 +000057 m_completion_mask (CommandCompletions::eDiskFileCompletion),
58 m_resolve (resolve)
Greg Claytonb5f0fea2012-09-27 22:26:11 +000059{
60}
61
Greg Clayton67cc0632012-08-22 17:17:09 +000062void
63OptionValueFileSpec::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
64{
65 if (dump_mask & eDumpOptionType)
66 strm.Printf ("(%s)", GetTypeAsCString ());
67 if (dump_mask & eDumpOptionValue)
68 {
69 if (dump_mask & eDumpOptionType)
70 strm.PutCString (" = ");
71
72 if (m_current_value)
73 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +000074 strm << '"' << m_current_value.GetPath().c_str() << '"';
Greg Clayton67cc0632012-08-22 17:17:09 +000075 }
76 }
77}
78
79Error
Pavel Labathc95f7e22015-02-20 11:14:59 +000080OptionValueFileSpec::SetValueFromString (llvm::StringRef value,
Greg Clayton67cc0632012-08-22 17:17:09 +000081 VarSetOperationType op)
82{
83 Error error;
84 switch (op)
85 {
86 case eVarSetOperationClear:
87 Clear ();
Greg Clayton332e8b12015-01-13 21:13:08 +000088 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +000089 break;
90
91 case eVarSetOperationReplace:
92 case eVarSetOperationAssign:
Pavel Labathc95f7e22015-02-20 11:14:59 +000093 if (value.size() > 0)
Greg Clayton67cc0632012-08-22 17:17:09 +000094 {
Jason Molenda5c98b1c2013-09-13 02:33:15 +000095 // The setting value may have whitespace, double-quotes, or single-quotes around the file
96 // path to indicate that internal spaces are not word breaks. Strip off any ws & quotes
97 // from the start and end of the file path - we aren't doing any word // breaking here so
98 // the quoting is unnecessary. NB this will cause a problem if someone tries to specify
99 // a file path that legitimately begins or ends with a " or ' character, or whitespace.
Pavel Labathc95f7e22015-02-20 11:14:59 +0000100 value = value.trim("\"' \t");
Jason Molenda36d44612013-08-27 04:58:31 +0000101 m_value_was_set = true;
Pavel Labathc95f7e22015-02-20 11:14:59 +0000102 m_current_value.SetFile(value.str().c_str(), m_resolve);
Jim Ingham2e3881c2014-04-04 18:06:54 +0000103 m_data_sp.reset();
Greg Clayton39fb1382015-03-06 23:46:54 +0000104 m_data_mod_time.Clear();
Greg Clayton332e8b12015-01-13 21:13:08 +0000105 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +0000106 }
107 else
108 {
109 error.SetErrorString("invalid value string");
110 }
111 break;
112
113 case eVarSetOperationInsertBefore:
114 case eVarSetOperationInsertAfter:
115 case eVarSetOperationRemove:
116 case eVarSetOperationAppend:
117 case eVarSetOperationInvalid:
Pavel Labathc95f7e22015-02-20 11:14:59 +0000118 error = OptionValue::SetValueFromString (value, op);
Greg Clayton67cc0632012-08-22 17:17:09 +0000119 break;
120 }
121 return error;
122}
123
124lldb::OptionValueSP
125OptionValueFileSpec::DeepCopy () const
126{
127 return OptionValueSP(new OptionValueFileSpec(*this));
128}
129
130
131size_t
132OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter,
133 const char *s,
134 int match_start_point,
135 int max_return_elements,
136 bool &word_complete,
137 StringList &matches)
138{
139 word_complete = false;
140 matches.Clear();
141 CommandCompletions::InvokeCommonCompletionCallbacks (interpreter,
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000142 m_completion_mask,
Greg Clayton67cc0632012-08-22 17:17:09 +0000143 s,
144 match_start_point,
145 max_return_elements,
Ed Masted78c9572014-04-20 00:31:37 +0000146 nullptr,
Greg Clayton67cc0632012-08-22 17:17:09 +0000147 word_complete,
148 matches);
149 return matches.GetSize();
150}
151
152
153
Greg Clayton6920b522012-08-22 18:39:03 +0000154const lldb::DataBufferSP &
Greg Clayton0b0b5122012-08-30 18:15:10 +0000155OptionValueFileSpec::GetFileContents(bool null_terminate)
Greg Clayton6920b522012-08-22 18:39:03 +0000156{
Greg Clayton39fb1382015-03-06 23:46:54 +0000157 if (m_current_value)
Greg Clayton0b0b5122012-08-30 18:15:10 +0000158 {
Greg Clayton39fb1382015-03-06 23:46:54 +0000159 const TimeValue file_mod_time = m_current_value.GetModificationTime();
160 if (m_data_sp && m_data_mod_time == file_mod_time)
161 return m_data_sp;
Greg Clayton0b0b5122012-08-30 18:15:10 +0000162 if (null_terminate)
163 m_data_sp = m_current_value.ReadFileContentsAsCString();
164 else
165 m_data_sp = m_current_value.ReadFileContents();
Greg Clayton39fb1382015-03-06 23:46:54 +0000166 m_data_mod_time = file_mod_time;
Greg Clayton0b0b5122012-08-30 18:15:10 +0000167 }
Greg Clayton6920b522012-08-22 18:39:03 +0000168 return m_data_sp;
169}
170
Greg Clayton67cc0632012-08-22 17:17:09 +0000171