OptionValueFileSpec.cpp revision 314564
1//===-- 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
12#include "lldb/Core/State.h"
13#include "lldb/DataFormatters/FormatManager.h"
14#include "lldb/Host/FileSystem.h"
15#include "lldb/Interpreter/Args.h"
16#include "lldb/Interpreter/CommandCompletions.h"
17#include "lldb/Interpreter/CommandInterpreter.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
22OptionValueFileSpec::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) {}
27
28OptionValueFileSpec::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) {}
33
34OptionValueFileSpec::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) {}
41
42void 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) {
47    if (dump_mask & eDumpOptionType)
48      strm.PutCString(" = ");
49
50    if (m_current_value) {
51      strm << '"' << m_current_value.GetPath().c_str() << '"';
52    }
53  }
54}
55
56Error 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;
80      m_current_value.SetFile(value.str(), m_resolve);
81      m_data_sp.reset();
82      m_data_mod_time = llvm::sys::TimePoint<>();
83      NotifyValueChanged();
84    } else {
85      error.SetErrorString("invalid value string");
86    }
87    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;
98}
99
100lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
101  return OptionValueSP(new OptionValueFileSpec(*this));
102}
103
104size_t OptionValueFileSpec::AutoComplete(
105    CommandInterpreter &interpreter, llvm::StringRef s, int match_start_point,
106    int max_return_elements, bool &word_complete, StringList &matches) {
107  word_complete = false;
108  matches.Clear();
109  CommandCompletions::InvokeCommonCompletionCallbacks(
110      interpreter, m_completion_mask, s, match_start_point, max_return_elements,
111      nullptr, word_complete, matches);
112  return matches.GetSize();
113}
114
115const lldb::DataBufferSP &
116OptionValueFileSpec::GetFileContents(bool null_terminate) {
117  if (m_current_value) {
118    const auto file_mod_time = FileSystem::GetModificationTime(m_current_value);
119    if (m_data_sp && m_data_mod_time == file_mod_time)
120      return m_data_sp;
121    if (null_terminate)
122      m_data_sp = m_current_value.ReadFileContentsAsCString();
123    else
124      m_data_sp = m_current_value.ReadFileContents();
125    m_data_mod_time = file_mod_time;
126  }
127  return m_data_sp;
128}
129