167cc0636SGreg Clayton //===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
267cc0636SGreg Clayton //
367cc0636SGreg Clayton //                     The LLVM Compiler Infrastructure
467cc0636SGreg Clayton //
567cc0636SGreg Clayton // This file is distributed under the University of Illinois Open Source
667cc0636SGreg Clayton // License. See LICENSE.TXT for details.
767cc0636SGreg Clayton //
867cc0636SGreg Clayton //===----------------------------------------------------------------------===//
967cc0636SGreg Clayton 
1067cc0636SGreg Clayton #include "lldb/Interpreter/OptionValueFileSpec.h"
1167cc0636SGreg Clayton 
1267cc0636SGreg Clayton #include "lldb/Core/State.h"
135548cb50SEnrico Granata #include "lldb/DataFormatters/FormatManager.h"
141408bf72SPavel Labath #include "lldb/Host/FileSystem.h"
1567cc0636SGreg Clayton #include "lldb/Interpreter/CommandCompletions.h"
16e1cfbc79STodd Fiala #include "lldb/Interpreter/CommandInterpreter.h"
17145d95c9SPavel Labath #include "lldb/Utility/Args.h"
187f6a7a37SZachary Turner #include "lldb/Utility/DataBufferLLVM.h"
1967cc0636SGreg Clayton 
2067cc0636SGreg Clayton using namespace lldb;
2167cc0636SGreg Clayton using namespace lldb_private;
2267cc0636SGreg Clayton 
23b9c1b51eSKate Stone OptionValueFileSpec::OptionValueFileSpec(bool resolve)
24b9c1b51eSKate Stone     : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
2539fb1389SGreg Clayton       m_data_mod_time(),
261f4706c3SVince Harron       m_completion_mask(CommandCompletions::eDiskFileCompletion),
27b9c1b51eSKate Stone       m_resolve(resolve) {}
28b5f0feabSGreg Clayton 
29b9c1b51eSKate Stone OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
30b9c1b51eSKate Stone     : OptionValue(), m_current_value(value), m_default_value(value),
31b9c1b51eSKate Stone       m_data_sp(), m_data_mod_time(),
321f4706c3SVince Harron       m_completion_mask(CommandCompletions::eDiskFileCompletion),
33b9c1b51eSKate Stone       m_resolve(resolve) {}
34b5f0feabSGreg Clayton 
35b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec(const FileSpec &current_value,
361f4706c3SVince Harron                                          const FileSpec &default_value,
37b9c1b51eSKate Stone                                          bool resolve)
38b9c1b51eSKate Stone     : OptionValue(), m_current_value(current_value),
39b9c1b51eSKate Stone       m_default_value(default_value), m_data_sp(), m_data_mod_time(),
401f4706c3SVince Harron       m_completion_mask(CommandCompletions::eDiskFileCompletion),
41b9c1b51eSKate Stone       m_resolve(resolve) {}
42b5f0feabSGreg Clayton 
43b9c1b51eSKate Stone void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
44b9c1b51eSKate Stone                                     Stream &strm, uint32_t dump_mask) {
4567cc0636SGreg Clayton   if (dump_mask & eDumpOptionType)
4667cc0636SGreg Clayton     strm.Printf("(%s)", GetTypeAsCString());
47b9c1b51eSKate Stone   if (dump_mask & eDumpOptionValue) {
4867cc0636SGreg Clayton     if (dump_mask & eDumpOptionType)
4967cc0636SGreg Clayton       strm.PutCString(" = ");
5067cc0636SGreg Clayton 
51b9c1b51eSKate Stone     if (m_current_value) {
52b5ad4ec7SGreg Clayton       strm << '"' << m_current_value.GetPath().c_str() << '"';
5367cc0636SGreg Clayton     }
5467cc0636SGreg Clayton   }
5567cc0636SGreg Clayton }
5667cc0636SGreg Clayton 
5797206d57SZachary Turner Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
58b9c1b51eSKate Stone                                                VarSetOperationType op) {
5997206d57SZachary Turner   Status error;
60b9c1b51eSKate Stone   switch (op) {
6167cc0636SGreg Clayton   case eVarSetOperationClear:
6267cc0636SGreg Clayton     Clear();
63332e8b1cSGreg Clayton     NotifyValueChanged();
6467cc0636SGreg Clayton     break;
6567cc0636SGreg Clayton 
6667cc0636SGreg Clayton   case eVarSetOperationReplace:
6767cc0636SGreg Clayton   case eVarSetOperationAssign:
68b9c1b51eSKate Stone     if (value.size() > 0) {
69b9c1b51eSKate Stone       // The setting value may have whitespace, double-quotes, or single-quotes
7005097246SAdrian Prantl       // around the file path to indicate that internal spaces are not word
7105097246SAdrian Prantl       // breaks.  Strip off any ws & quotes from the start and end of the file
7205097246SAdrian Prantl       // path - we aren't doing any word // breaking here so the quoting is
7305097246SAdrian Prantl       // unnecessary.  NB this will cause a problem if someone tries to specify
74b9c1b51eSKate Stone       // a file path that legitimately begins or ends with a " or ' character,
75b9c1b51eSKate Stone       // or whitespace.
76c95f7e2aSPavel Labath       value = value.trim("\"' \t");
7767cc0636SGreg Clayton       m_value_was_set = true;
78937348cdSJonas Devlieghere       m_current_value.SetFile(value.str(), m_resolve, FileSpec::Style::native);
792e3881c0SJim Ingham       m_data_sp.reset();
80e877baa8SPavel Labath       m_data_mod_time = llvm::sys::TimePoint<>();
81332e8b1cSGreg Clayton       NotifyValueChanged();
82b9c1b51eSKate Stone     } else {
8367cc0636SGreg Clayton       error.SetErrorString("invalid value string");
8467cc0636SGreg Clayton     }
8567cc0636SGreg Clayton     break;
8667cc0636SGreg Clayton 
8767cc0636SGreg Clayton   case eVarSetOperationInsertBefore:
8867cc0636SGreg Clayton   case eVarSetOperationInsertAfter:
8967cc0636SGreg Clayton   case eVarSetOperationRemove:
9067cc0636SGreg Clayton   case eVarSetOperationAppend:
9167cc0636SGreg Clayton   case eVarSetOperationInvalid:
92c95f7e2aSPavel Labath     error = OptionValue::SetValueFromString(value, op);
9367cc0636SGreg Clayton     break;
9467cc0636SGreg Clayton   }
9567cc0636SGreg Clayton   return error;
9667cc0636SGreg Clayton }
9767cc0636SGreg Clayton 
98b9c1b51eSKate Stone lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
9967cc0636SGreg Clayton   return OptionValueSP(new OptionValueFileSpec(*this));
10067cc0636SGreg Clayton }
10167cc0636SGreg Clayton 
102*a2e76c0bSRaphael Isemann size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
103*a2e76c0bSRaphael Isemann                                          CompletionRequest &request) {
104*a2e76c0bSRaphael Isemann   request.SetWordComplete(false);
105*a2e76c0bSRaphael Isemann   request.GetMatches().Clear();
106b9c1b51eSKate Stone   CommandCompletions::InvokeCommonCompletionCallbacks(
107*a2e76c0bSRaphael Isemann       interpreter, m_completion_mask, request, nullptr);
108*a2e76c0bSRaphael Isemann   return request.GetMatches().GetSize();
10967cc0636SGreg Clayton }
11067cc0636SGreg Clayton 
11150251fc7SPavel Labath const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
112b9c1b51eSKate Stone   if (m_current_value) {
113e877baa8SPavel Labath     const auto file_mod_time = FileSystem::GetModificationTime(m_current_value);
11439fb1389SGreg Clayton     if (m_data_sp && m_data_mod_time == file_mod_time)
11539fb1389SGreg Clayton       return m_data_sp;
11650251fc7SPavel Labath     m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath());
11739fb1389SGreg Clayton     m_data_mod_time = file_mod_time;
1180b0b512fSGreg Clayton   }
1196920b52bSGreg Clayton   return m_data_sp;
1206920b52bSGreg Clayton }
121