blob: 479d57eb27f56b07e1ca241626ead95864901e16 [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
Greg Clayton67cc0632012-08-22 17:17:09 +000012#include "lldb/Core/State.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000013#include "lldb/DataFormatters/FormatManager.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000014#include "lldb/Host/FileSystem.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000015#include "lldb/Interpreter/Args.h"
16#include "lldb/Interpreter/CommandCompletions.h"
Todd Fialae1cfbc72016-08-11 23:51:28 +000017#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000018
19using namespace lldb;
20using namespace lldb_private;
21
Kate Stoneb9c1b512016-09-06 20:57:50 +000022OptionValueFileSpec::OptionValueFileSpec(bool resolve)
23 : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
24 m_data_mod_time(),
25 m_completion_mask(CommandCompletions::eDiskFileCompletion),
26 m_resolve(resolve) {}
Greg Claytonb5f0fea2012-09-27 22:26:11 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
29 : OptionValue(), m_current_value(value), m_default_value(value),
30 m_data_sp(), m_data_mod_time(),
31 m_completion_mask(CommandCompletions::eDiskFileCompletion),
32 m_resolve(resolve) {}
Greg Claytonb5f0fea2012-09-27 22:26:11 +000033
Kate Stoneb9c1b512016-09-06 20:57:50 +000034OptionValueFileSpec::OptionValueFileSpec(const FileSpec &current_value,
35 const FileSpec &default_value,
36 bool resolve)
37 : OptionValue(), m_current_value(current_value),
38 m_default_value(default_value), m_data_sp(), m_data_mod_time(),
39 m_completion_mask(CommandCompletions::eDiskFileCompletion),
40 m_resolve(resolve) {}
Greg Claytonb5f0fea2012-09-27 22:26:11 +000041
Kate Stoneb9c1b512016-09-06 20:57:50 +000042void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
43 Stream &strm, uint32_t dump_mask) {
44 if (dump_mask & eDumpOptionType)
45 strm.Printf("(%s)", GetTypeAsCString());
46 if (dump_mask & eDumpOptionValue) {
Greg Clayton67cc0632012-08-22 17:17:09 +000047 if (dump_mask & eDumpOptionType)
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 strm.PutCString(" = ");
Greg Clayton67cc0632012-08-22 17:17:09 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 if (m_current_value) {
51 strm << '"' << m_current_value.GetPath().c_str() << '"';
Greg Clayton67cc0632012-08-22 17:17:09 +000052 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 }
Greg Clayton67cc0632012-08-22 17:17:09 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056Error OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
57 VarSetOperationType op) {
58 Error error;
59 switch (op) {
60 case eVarSetOperationClear:
61 Clear();
62 NotifyValueChanged();
63 break;
64
65 case eVarSetOperationReplace:
66 case eVarSetOperationAssign:
67 if (value.size() > 0) {
68 // The setting value may have whitespace, double-quotes, or single-quotes
69 // around the file
70 // path to indicate that internal spaces are not word breaks. Strip off
71 // any ws & quotes
72 // from the start and end of the file path - we aren't doing any word //
73 // breaking here so
74 // the quoting is unnecessary. NB this will cause a problem if someone
75 // tries to specify
76 // a file path that legitimately begins or ends with a " or ' character,
77 // or whitespace.
78 value = value.trim("\"' \t");
79 m_value_was_set = true;
Malcolm Parsons771ef6d2016-11-02 20:34:10 +000080 m_current_value.SetFile(value.str(), m_resolve);
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 m_data_sp.reset();
82 m_data_mod_time.Clear();
83 NotifyValueChanged();
84 } else {
85 error.SetErrorString("invalid value string");
Greg Clayton67cc0632012-08-22 17:17:09 +000086 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 break;
88
89 case eVarSetOperationInsertBefore:
90 case eVarSetOperationInsertAfter:
91 case eVarSetOperationRemove:
92 case eVarSetOperationAppend:
93 case eVarSetOperationInvalid:
94 error = OptionValue::SetValueFromString(value, op);
95 break;
96 }
97 return error;
Greg Clayton67cc0632012-08-22 17:17:09 +000098}
99
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
101 return OptionValueSP(new OptionValueFileSpec(*this));
Greg Clayton67cc0632012-08-22 17:17:09 +0000102}
103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
105 const char *s, int match_start_point,
106 int max_return_elements,
107 bool &word_complete,
108 StringList &matches) {
109 word_complete = false;
110 matches.Clear();
111 CommandCompletions::InvokeCommonCompletionCallbacks(
112 interpreter, m_completion_mask, s, match_start_point, max_return_elements,
113 nullptr, word_complete, matches);
114 return matches.GetSize();
Greg Clayton67cc0632012-08-22 17:17:09 +0000115}
116
Greg Clayton6920b522012-08-22 18:39:03 +0000117const lldb::DataBufferSP &
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118OptionValueFileSpec::GetFileContents(bool null_terminate) {
119 if (m_current_value) {
Pavel Labath1408bf72016-11-01 16:11:14 +0000120 const TimeValue file_mod_time =
121 FileSystem::GetModificationTime(m_current_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 if (m_data_sp && m_data_mod_time == file_mod_time)
123 return m_data_sp;
124 if (null_terminate)
125 m_data_sp = m_current_value.ReadFileContentsAsCString();
126 else
127 m_data_sp = m_current_value.ReadFileContents();
128 m_data_mod_time = file_mod_time;
129 }
130 return m_data_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000131}