OptionValueFileSpec.cpp revision 341825
1178481Sjb//===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
2178481Sjb//
3178481Sjb//                     The LLVM Compiler Infrastructure
4178481Sjb//
5178481Sjb// This file is distributed under the University of Illinois Open Source
6178481Sjb// License. See LICENSE.TXT for details.
7178481Sjb//
8178481Sjb//===----------------------------------------------------------------------===//
9178481Sjb
10178481Sjb#include "lldb/Interpreter/OptionValueFileSpec.h"
11178481Sjb
12178481Sjb#include "lldb/Core/State.h"
13178481Sjb#include "lldb/DataFormatters/FormatManager.h"
14178481Sjb#include "lldb/Host/FileSystem.h"
15178481Sjb#include "lldb/Interpreter/CommandCompletions.h"
16178481Sjb#include "lldb/Interpreter/CommandInterpreter.h"
17178481Sjb#include "lldb/Utility/Args.h"
18178481Sjb#include "lldb/Utility/DataBufferLLVM.h"
19178481Sjb
20178481Sjbusing namespace lldb;
21178481Sjbusing namespace lldb_private;
22210767Srpaulo
23178481SjbOptionValueFileSpec::OptionValueFileSpec(bool resolve)
24178481Sjb    : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
25178481Sjb      m_data_mod_time(),
26178481Sjb      m_completion_mask(CommandCompletions::eDiskFileCompletion),
27178481Sjb      m_resolve(resolve) {}
28178481Sjb
29178481SjbOptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
30178481Sjb    : OptionValue(), m_current_value(value), m_default_value(value),
31178481Sjb      m_data_sp(), m_data_mod_time(),
32178481Sjb      m_completion_mask(CommandCompletions::eDiskFileCompletion),
33178481Sjb      m_resolve(resolve) {}
34178481Sjb
35178481SjbOptionValueFileSpec::OptionValueFileSpec(const FileSpec &current_value,
36178481Sjb                                         const FileSpec &default_value,
37178481Sjb                                         bool resolve)
38178481Sjb    : OptionValue(), m_current_value(current_value),
39178481Sjb      m_default_value(default_value), m_data_sp(), m_data_mod_time(),
40178481Sjb      m_completion_mask(CommandCompletions::eDiskFileCompletion),
41178481Sjb      m_resolve(resolve) {}
42178481Sjb
43178481Sjbvoid OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
44178481Sjb                                    Stream &strm, uint32_t dump_mask) {
45178481Sjb  if (dump_mask & eDumpOptionType)
46178481Sjb    strm.Printf("(%s)", GetTypeAsCString());
47178481Sjb  if (dump_mask & eDumpOptionValue) {
48178481Sjb    if (dump_mask & eDumpOptionType)
49178481Sjb      strm.PutCString(" = ");
50178481Sjb
51178481Sjb    if (m_current_value) {
52178481Sjb      strm << '"' << m_current_value.GetPath().c_str() << '"';
53178481Sjb    }
54178481Sjb  }
55178481Sjb}
56178481Sjb
57178481SjbStatus OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
58178481Sjb                                               VarSetOperationType op) {
59178481Sjb  Status error;
60178481Sjb  switch (op) {
61178481Sjb  case eVarSetOperationClear:
62178481Sjb    Clear();
63178481Sjb    NotifyValueChanged();
64178481Sjb    break;
65178481Sjb
66178481Sjb  case eVarSetOperationReplace:
67178481Sjb  case eVarSetOperationAssign:
68178481Sjb    if (value.size() > 0) {
69178481Sjb      // The setting value may have whitespace, double-quotes, or single-quotes
70178481Sjb      // around the file path to indicate that internal spaces are not word
71178481Sjb      // breaks.  Strip off any ws & quotes from the start and end of the file
72178481Sjb      // path - we aren't doing any word // breaking here so the quoting is
73178481Sjb      // unnecessary.  NB this will cause a problem if someone tries to specify
74178481Sjb      // a file path that legitimately begins or ends with a " or ' character,
75178481Sjb      // or whitespace.
76178481Sjb      value = value.trim("\"' \t");
77178481Sjb      m_value_was_set = true;
78178481Sjb      m_current_value.SetFile(value.str(), m_resolve, FileSpec::Style::native);
79178481Sjb      m_data_sp.reset();
80178481Sjb      m_data_mod_time = llvm::sys::TimePoint<>();
81178481Sjb      NotifyValueChanged();
82178481Sjb    } else {
83178481Sjb      error.SetErrorString("invalid value string");
84178481Sjb    }
85178481Sjb    break;
86178481Sjb
87210767Srpaulo  case eVarSetOperationInsertBefore:
88178481Sjb  case eVarSetOperationInsertAfter:
89210767Srpaulo  case eVarSetOperationRemove:
90210767Srpaulo  case eVarSetOperationAppend:
91178481Sjb  case eVarSetOperationInvalid:
92178481Sjb    error = OptionValue::SetValueFromString(value, op);
93178481Sjb    break;
94178481Sjb  }
95178481Sjb  return error;
96178481Sjb}
97178481Sjb
98178481Sjblldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
99178481Sjb  return OptionValueSP(new OptionValueFileSpec(*this));
100178481Sjb}
101178481Sjb
102178481Sjbsize_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
103178481Sjb                                         CompletionRequest &request) {
104178481Sjb  request.SetWordComplete(false);
105178481Sjb  CommandCompletions::InvokeCommonCompletionCallbacks(
106178481Sjb      interpreter, m_completion_mask, request, nullptr);
107178481Sjb  return request.GetNumberOfMatches();
108178481Sjb}
109178481Sjb
110178481Sjbconst lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
111178481Sjb  if (m_current_value) {
112178481Sjb    const auto file_mod_time = FileSystem::GetModificationTime(m_current_value);
113178481Sjb    if (m_data_sp && m_data_mod_time == file_mod_time)
114178481Sjb      return m_data_sp;
115178481Sjb    m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath());
116178481Sjb    m_data_mod_time = file_mod_time;
117178481Sjb  }
118178481Sjb  return m_data_sp;
119178481Sjb}
120178481Sjb