blob: e9d2d5fa94084f3451fa4774050aa45017dd726b [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Clayton67cc0632012-08-22 17:17:09 +000012#include "lldb/Interpreter/OptionValueFileSpec.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Greg Clayton67cc0632012-08-22 17:17:09 +000018#include "lldb/Core/State.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000019#include "lldb/DataFormatters/FormatManager.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000020#include "lldb/Interpreter/Args.h"
21#include "lldb/Interpreter/CommandCompletions.h"
22
23using namespace lldb;
24using namespace lldb_private;
25
Greg Claytonb5f0fea2012-09-27 22:26:11 +000026
Vince Harron1f4706c2015-02-18 23:12:26 +000027OptionValueFileSpec::OptionValueFileSpec (bool resolve) :
Greg Claytonb5f0fea2012-09-27 22:26:11 +000028 OptionValue(),
29 m_current_value (),
30 m_default_value (),
31 m_data_sp(),
Greg Clayton39fb1382015-03-06 23:46:54 +000032 m_data_mod_time (),
Vince Harron1f4706c2015-02-18 23:12:26 +000033 m_completion_mask (CommandCompletions::eDiskFileCompletion),
34 m_resolve (resolve)
Greg Claytonb5f0fea2012-09-27 22:26:11 +000035{
36}
37
Vince Harron1f4706c2015-02-18 23:12:26 +000038OptionValueFileSpec::OptionValueFileSpec (const FileSpec &value,
39 bool resolve) :
Greg Claytonb5f0fea2012-09-27 22:26:11 +000040 OptionValue(),
41 m_current_value (value),
42 m_default_value (value),
43 m_data_sp(),
Greg Clayton39fb1382015-03-06 23:46:54 +000044 m_data_mod_time (),
Vince Harron1f4706c2015-02-18 23:12:26 +000045 m_completion_mask (CommandCompletions::eDiskFileCompletion),
46 m_resolve (resolve)
Greg Claytonb5f0fea2012-09-27 22:26:11 +000047{
48}
49
50OptionValueFileSpec::OptionValueFileSpec (const FileSpec &current_value,
Vince Harron1f4706c2015-02-18 23:12:26 +000051 const FileSpec &default_value,
52 bool resolve) :
Greg Claytonb5f0fea2012-09-27 22:26:11 +000053 OptionValue(),
54 m_current_value (current_value),
55 m_default_value (default_value),
56 m_data_sp(),
Greg Clayton39fb1382015-03-06 23:46:54 +000057 m_data_mod_time (),
Vince Harron1f4706c2015-02-18 23:12:26 +000058 m_completion_mask (CommandCompletions::eDiskFileCompletion),
59 m_resolve (resolve)
Greg Claytonb5f0fea2012-09-27 22:26:11 +000060{
61}
62
Greg Clayton67cc0632012-08-22 17:17:09 +000063void
64OptionValueFileSpec::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
65{
66 if (dump_mask & eDumpOptionType)
67 strm.Printf ("(%s)", GetTypeAsCString ());
68 if (dump_mask & eDumpOptionValue)
69 {
70 if (dump_mask & eDumpOptionType)
71 strm.PutCString (" = ");
72
73 if (m_current_value)
74 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +000075 strm << '"' << m_current_value.GetPath().c_str() << '"';
Greg Clayton67cc0632012-08-22 17:17:09 +000076 }
77 }
78}
79
80Error
Pavel Labathc95f7e22015-02-20 11:14:59 +000081OptionValueFileSpec::SetValueFromString (llvm::StringRef value,
Greg Clayton67cc0632012-08-22 17:17:09 +000082 VarSetOperationType op)
83{
84 Error error;
85 switch (op)
86 {
87 case eVarSetOperationClear:
88 Clear ();
Greg Clayton332e8b12015-01-13 21:13:08 +000089 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +000090 break;
91
92 case eVarSetOperationReplace:
93 case eVarSetOperationAssign:
Pavel Labathc95f7e22015-02-20 11:14:59 +000094 if (value.size() > 0)
Greg Clayton67cc0632012-08-22 17:17:09 +000095 {
Jason Molenda5c98b1c2013-09-13 02:33:15 +000096 // The setting value may have whitespace, double-quotes, or single-quotes around the file
97 // path to indicate that internal spaces are not word breaks. Strip off any ws & quotes
98 // from the start and end of the file path - we aren't doing any word // breaking here so
99 // the quoting is unnecessary. NB this will cause a problem if someone tries to specify
100 // a file path that legitimately begins or ends with a " or ' character, or whitespace.
Pavel Labathc95f7e22015-02-20 11:14:59 +0000101 value = value.trim("\"' \t");
Jason Molenda36d44612013-08-27 04:58:31 +0000102 m_value_was_set = true;
Pavel Labathc95f7e22015-02-20 11:14:59 +0000103 m_current_value.SetFile(value.str().c_str(), m_resolve);
Jim Ingham2e3881c2014-04-04 18:06:54 +0000104 m_data_sp.reset();
Greg Clayton39fb1382015-03-06 23:46:54 +0000105 m_data_mod_time.Clear();
Greg Clayton332e8b12015-01-13 21:13:08 +0000106 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +0000107 }
108 else
109 {
110 error.SetErrorString("invalid value string");
111 }
112 break;
113
114 case eVarSetOperationInsertBefore:
115 case eVarSetOperationInsertAfter:
116 case eVarSetOperationRemove:
117 case eVarSetOperationAppend:
118 case eVarSetOperationInvalid:
Pavel Labathc95f7e22015-02-20 11:14:59 +0000119 error = OptionValue::SetValueFromString (value, op);
Greg Clayton67cc0632012-08-22 17:17:09 +0000120 break;
121 }
122 return error;
123}
124
125lldb::OptionValueSP
126OptionValueFileSpec::DeepCopy () const
127{
128 return OptionValueSP(new OptionValueFileSpec(*this));
129}
130
131
132size_t
133OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter,
134 const char *s,
135 int match_start_point,
136 int max_return_elements,
137 bool &word_complete,
138 StringList &matches)
139{
140 word_complete = false;
141 matches.Clear();
142 CommandCompletions::InvokeCommonCompletionCallbacks (interpreter,
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000143 m_completion_mask,
Greg Clayton67cc0632012-08-22 17:17:09 +0000144 s,
145 match_start_point,
146 max_return_elements,
Ed Masted78c9572014-04-20 00:31:37 +0000147 nullptr,
Greg Clayton67cc0632012-08-22 17:17:09 +0000148 word_complete,
149 matches);
150 return matches.GetSize();
151}
152
153
154
Greg Clayton6920b522012-08-22 18:39:03 +0000155const lldb::DataBufferSP &
Greg Clayton0b0b5122012-08-30 18:15:10 +0000156OptionValueFileSpec::GetFileContents(bool null_terminate)
Greg Clayton6920b522012-08-22 18:39:03 +0000157{
Greg Clayton39fb1382015-03-06 23:46:54 +0000158 if (m_current_value)
Greg Clayton0b0b5122012-08-30 18:15:10 +0000159 {
Greg Clayton39fb1382015-03-06 23:46:54 +0000160 const TimeValue file_mod_time = m_current_value.GetModificationTime();
161 if (m_data_sp && m_data_mod_time == file_mod_time)
162 return m_data_sp;
Greg Clayton0b0b5122012-08-30 18:15:10 +0000163 if (null_terminate)
164 m_data_sp = m_current_value.ReadFileContentsAsCString();
165 else
166 m_data_sp = m_current_value.ReadFileContents();
Greg Clayton39fb1382015-03-06 23:46:54 +0000167 m_data_mod_time = file_mod_time;
Greg Clayton0b0b5122012-08-30 18:15:10 +0000168 }
Greg Clayton6920b522012-08-22 18:39:03 +0000169 return m_data_sp;
170}
171
Greg Clayton67cc0632012-08-22 17:17:09 +0000172