OptionValueFileSpec.cpp revision 344779
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/DataFormatters/FormatManager.h"
13314564Sdim#include "lldb/Host/FileSystem.h"
14254721Semaste#include "lldb/Interpreter/CommandCompletions.h"
15314564Sdim#include "lldb/Interpreter/CommandInterpreter.h"
16341825Sdim#include "lldb/Utility/Args.h"
17344779Sdim#include "lldb/Utility/State.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
56321369SdimStatus OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
57321369Sdim                                               VarSetOperationType op) {
58321369Sdim  Status 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
69341825Sdim      // around the file path to indicate that internal spaces are not word
70341825Sdim      // breaks.  Strip off any ws & quotes from the start and end of the file
71341825Sdim      // path - we aren't doing any word // breaking here so the quoting is
72341825Sdim      // unnecessary.  NB this will cause a problem if someone tries to specify
73314564Sdim      // a file path that legitimately begins or ends with a " or ' character,
74314564Sdim      // or whitespace.
75314564Sdim      value = value.trim("\"' \t");
76314564Sdim      m_value_was_set = true;
77344779Sdim      m_current_value.SetFile(value.str(), FileSpec::Style::native);
78344779Sdim      if (m_resolve)
79344779Sdim        FileSystem::Instance().Resolve(m_current_value);
80314564Sdim      m_data_sp.reset();
81314564Sdim      m_data_mod_time = llvm::sys::TimePoint<>();
82314564Sdim      NotifyValueChanged();
83314564Sdim    } else {
84314564Sdim      error.SetErrorString("invalid value string");
85254721Semaste    }
86314564Sdim    break;
87314564Sdim
88314564Sdim  case eVarSetOperationInsertBefore:
89314564Sdim  case eVarSetOperationInsertAfter:
90314564Sdim  case eVarSetOperationRemove:
91314564Sdim  case eVarSetOperationAppend:
92314564Sdim  case eVarSetOperationInvalid:
93314564Sdim    error = OptionValue::SetValueFromString(value, op);
94314564Sdim    break;
95314564Sdim  }
96314564Sdim  return error;
97254721Semaste}
98254721Semaste
99314564Sdimlldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
100314564Sdim  return OptionValueSP(new OptionValueFileSpec(*this));
101254721Semaste}
102254721Semaste
103341825Sdimsize_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
104341825Sdim                                         CompletionRequest &request) {
105341825Sdim  request.SetWordComplete(false);
106314564Sdim  CommandCompletions::InvokeCommonCompletionCallbacks(
107341825Sdim      interpreter, m_completion_mask, request, nullptr);
108341825Sdim  return request.GetNumberOfMatches();
109254721Semaste}
110254721Semaste
111327952Sdimconst lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
112314564Sdim  if (m_current_value) {
113344779Sdim    const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value);
114314564Sdim    if (m_data_sp && m_data_mod_time == file_mod_time)
115314564Sdim      return m_data_sp;
116344779Sdim    m_data_sp =
117344779Sdim        FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath());
118314564Sdim    m_data_mod_time = file_mod_time;
119314564Sdim  }
120314564Sdim  return m_data_sp;
121254721Semaste}
122