180814287SRaphael Isemann //===-- OptionValueFileSpec.cpp -------------------------------------------===//
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)
22*8cdcd41eSTatyana Krasnukha     : m_completion_mask(CommandCompletions::eDiskFileCompletion),
23b9c1b51eSKate Stone       m_resolve(resolve) {}
24b5f0feabSGreg Clayton 
25b9c1b51eSKate Stone OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
26*8cdcd41eSTatyana Krasnukha     : m_current_value(value), m_default_value(value),
271f4706c3SVince Harron       m_completion_mask(CommandCompletions::eDiskFileCompletion),
28b9c1b51eSKate Stone       m_resolve(resolve) {}
29b5f0feabSGreg Clayton 
30b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec(const FileSpec &current_value,
311f4706c3SVince Harron                                          const FileSpec &default_value,
32b9c1b51eSKate Stone                                          bool resolve)
33*8cdcd41eSTatyana Krasnukha     : m_current_value(current_value), m_default_value(default_value),
341f4706c3SVince Harron       m_completion_mask(CommandCompletions::eDiskFileCompletion),
35b9c1b51eSKate Stone       m_resolve(resolve) {}
36b5f0feabSGreg Clayton 
37b9c1b51eSKate Stone void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
38b9c1b51eSKate Stone                                     Stream &strm, uint32_t dump_mask) {
3967cc0636SGreg Clayton   if (dump_mask & eDumpOptionType)
4067cc0636SGreg Clayton     strm.Printf("(%s)", GetTypeAsCString());
41b9c1b51eSKate Stone   if (dump_mask & eDumpOptionValue) {
4267cc0636SGreg Clayton     if (dump_mask & eDumpOptionType)
4367cc0636SGreg Clayton       strm.PutCString(" = ");
4467cc0636SGreg Clayton 
45b9c1b51eSKate Stone     if (m_current_value) {
46b5ad4ec7SGreg Clayton       strm << '"' << m_current_value.GetPath().c_str() << '"';
4767cc0636SGreg Clayton     }
4867cc0636SGreg Clayton   }
4967cc0636SGreg Clayton }
5067cc0636SGreg Clayton 
5197206d57SZachary Turner Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
52b9c1b51eSKate Stone                                                VarSetOperationType op) {
5397206d57SZachary Turner   Status error;
54b9c1b51eSKate Stone   switch (op) {
5567cc0636SGreg Clayton   case eVarSetOperationClear:
5667cc0636SGreg Clayton     Clear();
57332e8b1cSGreg Clayton     NotifyValueChanged();
5867cc0636SGreg Clayton     break;
5967cc0636SGreg Clayton 
6067cc0636SGreg Clayton   case eVarSetOperationReplace:
6167cc0636SGreg Clayton   case eVarSetOperationAssign:
62b9c1b51eSKate Stone     if (value.size() > 0) {
63c95f7e2aSPavel Labath       value = value.trim("\"' \t");
6467cc0636SGreg Clayton       m_value_was_set = true;
658f3be7a3SJonas Devlieghere       m_current_value.SetFile(value.str(), FileSpec::Style::native);
668f3be7a3SJonas Devlieghere       if (m_resolve)
678f3be7a3SJonas Devlieghere         FileSystem::Instance().Resolve(m_current_value);
682e3881c0SJim Ingham       m_data_sp.reset();
69e877baa8SPavel Labath       m_data_mod_time = llvm::sys::TimePoint<>();
70332e8b1cSGreg Clayton       NotifyValueChanged();
71b9c1b51eSKate Stone     } else {
7267cc0636SGreg Clayton       error.SetErrorString("invalid value string");
7367cc0636SGreg Clayton     }
7467cc0636SGreg Clayton     break;
7567cc0636SGreg Clayton 
7667cc0636SGreg Clayton   case eVarSetOperationInsertBefore:
7767cc0636SGreg Clayton   case eVarSetOperationInsertAfter:
7867cc0636SGreg Clayton   case eVarSetOperationRemove:
7967cc0636SGreg Clayton   case eVarSetOperationAppend:
8067cc0636SGreg Clayton   case eVarSetOperationInvalid:
81c95f7e2aSPavel Labath     error = OptionValue::SetValueFromString(value, op);
8267cc0636SGreg Clayton     break;
8367cc0636SGreg Clayton   }
8467cc0636SGreg Clayton   return error;
8567cc0636SGreg Clayton }
8667cc0636SGreg Clayton 
87b9c1b51eSKate Stone lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
8867cc0636SGreg Clayton   return OptionValueSP(new OptionValueFileSpec(*this));
8967cc0636SGreg Clayton }
9067cc0636SGreg Clayton 
91ae34ed2cSRaphael Isemann void OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
92a2e76c0bSRaphael Isemann                                        CompletionRequest &request) {
93b9c1b51eSKate Stone   CommandCompletions::InvokeCommonCompletionCallbacks(
94a2e76c0bSRaphael Isemann       interpreter, m_completion_mask, request, nullptr);
9567cc0636SGreg Clayton }
9667cc0636SGreg Clayton 
9750251fc7SPavel Labath const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
98b9c1b51eSKate Stone   if (m_current_value) {
9946376966SJonas Devlieghere     const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value);
10039fb1389SGreg Clayton     if (m_data_sp && m_data_mod_time == file_mod_time)
10139fb1389SGreg Clayton       return m_data_sp;
10287e403aaSJonas Devlieghere     m_data_sp =
10387e403aaSJonas Devlieghere         FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath());
10439fb1389SGreg Clayton     m_data_mod_time = file_mod_time;
1050b0b512fSGreg Clayton   }
1066920b52bSGreg Clayton   return m_data_sp;
1076920b52bSGreg Clayton }
108