167cc0636SGreg Clayton //===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
267cc0636SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
667cc0636SGreg Clayton //
767cc0636SGreg Clayton //===----------------------------------------------------------------------===//
867cc0636SGreg Clayton 
967cc0636SGreg Clayton #include "lldb/Interpreter/OptionValueFileSpec.h"
1067cc0636SGreg Clayton 
115548cb50SEnrico Granata #include "lldb/DataFormatters/FormatManager.h"
121408bf72SPavel Labath #include "lldb/Host/FileSystem.h"
1367cc0636SGreg Clayton #include "lldb/Interpreter/CommandCompletions.h"
14e1cfbc79STodd Fiala #include "lldb/Interpreter/CommandInterpreter.h"
15145d95c9SPavel Labath #include "lldb/Utility/Args.h"
16d821c997SPavel Labath #include "lldb/Utility/State.h"
1767cc0636SGreg Clayton 
1867cc0636SGreg Clayton using namespace lldb;
1967cc0636SGreg Clayton using namespace lldb_private;
2067cc0636SGreg Clayton 
21b9c1b51eSKate Stone OptionValueFileSpec::OptionValueFileSpec(bool resolve)
22b9c1b51eSKate Stone     : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
2339fb1389SGreg Clayton       m_data_mod_time(),
241f4706c3SVince Harron       m_completion_mask(CommandCompletions::eDiskFileCompletion),
25b9c1b51eSKate Stone       m_resolve(resolve) {}
26b5f0feabSGreg Clayton 
27b9c1b51eSKate Stone OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
28b9c1b51eSKate Stone     : OptionValue(), m_current_value(value), m_default_value(value),
29b9c1b51eSKate Stone       m_data_sp(), m_data_mod_time(),
301f4706c3SVince Harron       m_completion_mask(CommandCompletions::eDiskFileCompletion),
31b9c1b51eSKate Stone       m_resolve(resolve) {}
32b5f0feabSGreg Clayton 
33b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec(const FileSpec &current_value,
341f4706c3SVince Harron                                          const FileSpec &default_value,
35b9c1b51eSKate Stone                                          bool resolve)
36b9c1b51eSKate Stone     : OptionValue(), m_current_value(current_value),
37b9c1b51eSKate Stone       m_default_value(default_value), m_data_sp(), m_data_mod_time(),
381f4706c3SVince Harron       m_completion_mask(CommandCompletions::eDiskFileCompletion),
39b9c1b51eSKate Stone       m_resolve(resolve) {}
40b5f0feabSGreg Clayton 
41b9c1b51eSKate Stone void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
42b9c1b51eSKate Stone                                     Stream &strm, uint32_t dump_mask) {
4367cc0636SGreg Clayton   if (dump_mask & eDumpOptionType)
4467cc0636SGreg Clayton     strm.Printf("(%s)", GetTypeAsCString());
45b9c1b51eSKate Stone   if (dump_mask & eDumpOptionValue) {
4667cc0636SGreg Clayton     if (dump_mask & eDumpOptionType)
4767cc0636SGreg Clayton       strm.PutCString(" = ");
4867cc0636SGreg Clayton 
49b9c1b51eSKate Stone     if (m_current_value) {
50b5ad4ec7SGreg Clayton       strm << '"' << m_current_value.GetPath().c_str() << '"';
5167cc0636SGreg Clayton     }
5267cc0636SGreg Clayton   }
5367cc0636SGreg Clayton }
5467cc0636SGreg Clayton 
5597206d57SZachary Turner Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
56b9c1b51eSKate Stone                                                VarSetOperationType op) {
5797206d57SZachary Turner   Status error;
58b9c1b51eSKate Stone   switch (op) {
5967cc0636SGreg Clayton   case eVarSetOperationClear:
6067cc0636SGreg Clayton     Clear();
61332e8b1cSGreg Clayton     NotifyValueChanged();
6267cc0636SGreg Clayton     break;
6367cc0636SGreg Clayton 
6467cc0636SGreg Clayton   case eVarSetOperationReplace:
6567cc0636SGreg Clayton   case eVarSetOperationAssign:
66b9c1b51eSKate Stone     if (value.size() > 0) {
67b9c1b51eSKate Stone       // The setting value may have whitespace, double-quotes, or single-quotes
6805097246SAdrian Prantl       // around the file path to indicate that internal spaces are not word
6905097246SAdrian Prantl       // breaks.  Strip off any ws & quotes from the start and end of the file
7005097246SAdrian Prantl       // path - we aren't doing any word // breaking here so the quoting is
7105097246SAdrian Prantl       // unnecessary.  NB this will cause a problem if someone tries to specify
72b9c1b51eSKate Stone       // a file path that legitimately begins or ends with a " or ' character,
73b9c1b51eSKate Stone       // or whitespace.
74c95f7e2aSPavel Labath       value = value.trim("\"' \t");
7567cc0636SGreg Clayton       m_value_was_set = true;
768f3be7a3SJonas Devlieghere       m_current_value.SetFile(value.str(), FileSpec::Style::native);
778f3be7a3SJonas Devlieghere       if (m_resolve)
788f3be7a3SJonas Devlieghere         FileSystem::Instance().Resolve(m_current_value);
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*ae34ed2cSRaphael Isemann void OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
103a2e76c0bSRaphael Isemann                                        CompletionRequest &request) {
104b9c1b51eSKate Stone   CommandCompletions::InvokeCommonCompletionCallbacks(
105a2e76c0bSRaphael Isemann       interpreter, m_completion_mask, request, nullptr);
10667cc0636SGreg Clayton }
10767cc0636SGreg Clayton 
10850251fc7SPavel Labath const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
109b9c1b51eSKate Stone   if (m_current_value) {
11046376966SJonas Devlieghere     const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value);
11139fb1389SGreg Clayton     if (m_data_sp && m_data_mod_time == file_mod_time)
11239fb1389SGreg Clayton       return m_data_sp;
11387e403aaSJonas Devlieghere     m_data_sp =
11487e403aaSJonas Devlieghere         FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath());
11539fb1389SGreg Clayton     m_data_mod_time = file_mod_time;
1160b0b512fSGreg Clayton   }
1176920b52bSGreg Clayton   return m_data_sp;
1186920b52bSGreg Clayton }
119