OptionValueFileSpec.cpp revision 314564
1254721Semaste//===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/Interpreter/OptionValueFileSpec.h"
11254721Semaste
12254721Semaste#include "lldb/Core/State.h"
13254721Semaste#include "lldb/DataFormatters/FormatManager.h"
14314564Sdim#include "lldb/Host/FileSystem.h"
15254721Semaste#include "lldb/Interpreter/Args.h"
16254721Semaste#include "lldb/Interpreter/CommandCompletions.h"
17314564Sdim#include "lldb/Interpreter/CommandInterpreter.h"
18254721Semaste
19254721Semasteusing namespace lldb;
20254721Semasteusing namespace lldb_private;
21254721Semaste
22314564SdimOptionValueFileSpec::OptionValueFileSpec(bool resolve)
23314564Sdim    : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
24314564Sdim      m_data_mod_time(),
25314564Sdim      m_completion_mask(CommandCompletions::eDiskFileCompletion),
26314564Sdim      m_resolve(resolve) {}
27254721Semaste
28314564SdimOptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
29314564Sdim    : OptionValue(), m_current_value(value), m_default_value(value),
30314564Sdim      m_data_sp(), m_data_mod_time(),
31314564Sdim      m_completion_mask(CommandCompletions::eDiskFileCompletion),
32314564Sdim      m_resolve(resolve) {}
33254721Semaste
34314564SdimOptionValueFileSpec::OptionValueFileSpec(const FileSpec &current_value,
35314564Sdim                                         const FileSpec &default_value,
36314564Sdim                                         bool resolve)
37314564Sdim    : OptionValue(), m_current_value(current_value),
38314564Sdim      m_default_value(default_value), m_data_sp(), m_data_mod_time(),
39314564Sdim      m_completion_mask(CommandCompletions::eDiskFileCompletion),
40314564Sdim      m_resolve(resolve) {}
41254721Semaste
42314564Sdimvoid OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
43314564Sdim                                    Stream &strm, uint32_t dump_mask) {
44314564Sdim  if (dump_mask & eDumpOptionType)
45314564Sdim    strm.Printf("(%s)", GetTypeAsCString());
46314564Sdim  if (dump_mask & eDumpOptionValue) {
47254721Semaste    if (dump_mask & eDumpOptionType)
48314564Sdim      strm.PutCString(" = ");
49254721Semaste
50314564Sdim    if (m_current_value) {
51314564Sdim      strm << '"' << m_current_value.GetPath().c_str() << '"';
52254721Semaste    }
53314564Sdim  }
54254721Semaste}
55254721Semaste
56314564SdimError OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
57314564Sdim                                              VarSetOperationType op) {
58314564Sdim  Error error;
59314564Sdim  switch (op) {
60314564Sdim  case eVarSetOperationClear:
61314564Sdim    Clear();
62314564Sdim    NotifyValueChanged();
63314564Sdim    break;
64314564Sdim
65314564Sdim  case eVarSetOperationReplace:
66314564Sdim  case eVarSetOperationAssign:
67314564Sdim    if (value.size() > 0) {
68314564Sdim      // The setting value may have whitespace, double-quotes, or single-quotes
69314564Sdim      // around the file
70314564Sdim      // path to indicate that internal spaces are not word breaks.  Strip off
71314564Sdim      // any ws & quotes
72314564Sdim      // from the start and end of the file path - we aren't doing any word //
73314564Sdim      // breaking here so
74314564Sdim      // the quoting is unnecessary.  NB this will cause a problem if someone
75314564Sdim      // tries to specify
76314564Sdim      // a file path that legitimately begins or ends with a " or ' character,
77314564Sdim      // or whitespace.
78314564Sdim      value = value.trim("\"' \t");
79314564Sdim      m_value_was_set = true;
80314564Sdim      m_current_value.SetFile(value.str(), m_resolve);
81314564Sdim      m_data_sp.reset();
82314564Sdim      m_data_mod_time = llvm::sys::TimePoint<>();
83314564Sdim      NotifyValueChanged();
84314564Sdim    } else {
85314564Sdim      error.SetErrorString("invalid value string");
86254721Semaste    }
87314564Sdim    break;
88314564Sdim
89314564Sdim  case eVarSetOperationInsertBefore:
90314564Sdim  case eVarSetOperationInsertAfter:
91314564Sdim  case eVarSetOperationRemove:
92314564Sdim  case eVarSetOperationAppend:
93314564Sdim  case eVarSetOperationInvalid:
94314564Sdim    error = OptionValue::SetValueFromString(value, op);
95314564Sdim    break;
96314564Sdim  }
97314564Sdim  return error;
98254721Semaste}
99254721Semaste
100314564Sdimlldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
101314564Sdim  return OptionValueSP(new OptionValueFileSpec(*this));
102254721Semaste}
103254721Semaste
104314564Sdimsize_t OptionValueFileSpec::AutoComplete(
105314564Sdim    CommandInterpreter &interpreter, llvm::StringRef s, int match_start_point,
106314564Sdim    int max_return_elements, bool &word_complete, StringList &matches) {
107314564Sdim  word_complete = false;
108314564Sdim  matches.Clear();
109314564Sdim  CommandCompletions::InvokeCommonCompletionCallbacks(
110314564Sdim      interpreter, m_completion_mask, s, match_start_point, max_return_elements,
111314564Sdim      nullptr, word_complete, matches);
112314564Sdim  return matches.GetSize();
113254721Semaste}
114254721Semaste
115254721Semasteconst lldb::DataBufferSP &
116314564SdimOptionValueFileSpec::GetFileContents(bool null_terminate) {
117314564Sdim  if (m_current_value) {
118314564Sdim    const auto file_mod_time = FileSystem::GetModificationTime(m_current_value);
119314564Sdim    if (m_data_sp && m_data_mod_time == file_mod_time)
120314564Sdim      return m_data_sp;
121314564Sdim    if (null_terminate)
122314564Sdim      m_data_sp = m_current_value.ReadFileContentsAsCString();
123314564Sdim    else
124314564Sdim      m_data_sp = m_current_value.ReadFileContents();
125314564Sdim    m_data_mod_time = file_mod_time;
126314564Sdim  }
127314564Sdim  return m_data_sp;
128254721Semaste}
129