blob: 3f466985a83a85076210da7efca5630f80d780bf [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
27OptionValueFileSpec::OptionValueFileSpec () :
28 OptionValue(),
29 m_current_value (),
30 m_default_value (),
31 m_data_sp(),
32 m_completion_mask (CommandCompletions::eDiskFileCompletion)
33{
34}
35
36OptionValueFileSpec::OptionValueFileSpec (const FileSpec &value) :
37 OptionValue(),
38 m_current_value (value),
39 m_default_value (value),
40 m_data_sp(),
41 m_completion_mask (CommandCompletions::eDiskFileCompletion)
42{
43}
44
45OptionValueFileSpec::OptionValueFileSpec (const FileSpec &current_value,
46 const FileSpec &default_value) :
47 OptionValue(),
48 m_current_value (current_value),
49 m_default_value (default_value),
50 m_data_sp(),
51 m_completion_mask (CommandCompletions::eDiskFileCompletion)
52{
53}
54
Greg Clayton67cc0632012-08-22 17:17:09 +000055void
56OptionValueFileSpec::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
57{
58 if (dump_mask & eDumpOptionType)
59 strm.Printf ("(%s)", GetTypeAsCString ());
60 if (dump_mask & eDumpOptionValue)
61 {
62 if (dump_mask & eDumpOptionType)
63 strm.PutCString (" = ");
64
65 if (m_current_value)
66 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +000067 strm << '"' << m_current_value.GetPath().c_str() << '"';
Greg Clayton67cc0632012-08-22 17:17:09 +000068 }
69 }
70}
71
72Error
73OptionValueFileSpec::SetValueFromCString (const char *value_cstr,
74 VarSetOperationType op)
75{
76 Error error;
77 switch (op)
78 {
79 case eVarSetOperationClear:
80 Clear ();
Greg Clayton332e8b12015-01-13 21:13:08 +000081 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +000082 break;
83
84 case eVarSetOperationReplace:
85 case eVarSetOperationAssign:
86 if (value_cstr && value_cstr[0])
87 {
Jason Molenda5c98b1c2013-09-13 02:33:15 +000088 // The setting value may have whitespace, double-quotes, or single-quotes around the file
89 // path to indicate that internal spaces are not word breaks. Strip off any ws & quotes
90 // from the start and end of the file path - we aren't doing any word // breaking here so
91 // the quoting is unnecessary. NB this will cause a problem if someone tries to specify
92 // a file path that legitimately begins or ends with a " or ' character, or whitespace.
93 std::string filepath(value_cstr);
94 auto prefix_chars_to_trim = filepath.find_first_not_of ("\"' \t");
95 if (prefix_chars_to_trim != std::string::npos && prefix_chars_to_trim > 0)
96 filepath.erase(0, prefix_chars_to_trim);
97 auto suffix_chars_to_trim = filepath.find_last_not_of ("\"' \t");
98 if (suffix_chars_to_trim != std::string::npos && suffix_chars_to_trim < filepath.size())
99 filepath.erase (suffix_chars_to_trim + 1);
100
Jason Molenda36d44612013-08-27 04:58:31 +0000101 m_value_was_set = true;
Jason Molenda5c98b1c2013-09-13 02:33:15 +0000102 m_current_value.SetFile(filepath.c_str(), true);
Jim Ingham2e3881c2014-04-04 18:06:54 +0000103 m_data_sp.reset();
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:
117 error = OptionValue::SetValueFromCString (value_cstr, op);
118 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{
156 if (!m_data_sp && m_current_value)
Greg Clayton0b0b5122012-08-30 18:15:10 +0000157 {
158 if (null_terminate)
159 m_data_sp = m_current_value.ReadFileContentsAsCString();
160 else
161 m_data_sp = m_current_value.ReadFileContents();
162 }
Greg Clayton6920b522012-08-22 18:39:03 +0000163 return m_data_sp;
164}
165
Greg Clayton67cc0632012-08-22 17:17:09 +0000166