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 
OptionValueFileSpec(bool resolve)219494c510SJonas Devlieghere OptionValueFileSpec::OptionValueFileSpec(bool resolve) : m_resolve(resolve) {}
22b5f0feabSGreg Clayton 
OptionValueFileSpec(const FileSpec & value,bool resolve)23b9c1b51eSKate Stone OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
248cdcd41eSTatyana Krasnukha     : m_current_value(value), m_default_value(value),
25*28c878aeSShafik Yaghmour 
26b9c1b51eSKate Stone       m_resolve(resolve) {}
27b5f0feabSGreg Clayton 
OptionValueFileSpec(const FileSpec & current_value,const FileSpec & default_value,bool resolve)28b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec(const FileSpec &current_value,
291f4706c3SVince Harron                                          const FileSpec &default_value,
30b9c1b51eSKate Stone                                          bool resolve)
318cdcd41eSTatyana Krasnukha     : m_current_value(current_value), m_default_value(default_value),
32*28c878aeSShafik Yaghmour 
33b9c1b51eSKate Stone       m_resolve(resolve) {}
34b5f0feabSGreg Clayton 
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)35b9c1b51eSKate Stone void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
36b9c1b51eSKate Stone                                     Stream &strm, uint32_t dump_mask) {
3767cc0636SGreg Clayton   if (dump_mask & eDumpOptionType)
3867cc0636SGreg Clayton     strm.Printf("(%s)", GetTypeAsCString());
39b9c1b51eSKate Stone   if (dump_mask & eDumpOptionValue) {
4067cc0636SGreg Clayton     if (dump_mask & eDumpOptionType)
4167cc0636SGreg Clayton       strm.PutCString(" = ");
4267cc0636SGreg Clayton 
43b9c1b51eSKate Stone     if (m_current_value) {
44b5ad4ec7SGreg Clayton       strm << '"' << m_current_value.GetPath().c_str() << '"';
4567cc0636SGreg Clayton     }
4667cc0636SGreg Clayton   }
4767cc0636SGreg Clayton }
4867cc0636SGreg Clayton 
SetValueFromString(llvm::StringRef value,VarSetOperationType op)4997206d57SZachary Turner Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
50b9c1b51eSKate Stone                                                VarSetOperationType op) {
5197206d57SZachary Turner   Status error;
52b9c1b51eSKate Stone   switch (op) {
5367cc0636SGreg Clayton   case eVarSetOperationClear:
5467cc0636SGreg Clayton     Clear();
55332e8b1cSGreg Clayton     NotifyValueChanged();
5667cc0636SGreg Clayton     break;
5767cc0636SGreg Clayton 
5867cc0636SGreg Clayton   case eVarSetOperationReplace:
5967cc0636SGreg Clayton   case eVarSetOperationAssign:
60b9c1b51eSKate Stone     if (value.size() > 0) {
61c95f7e2aSPavel Labath       value = value.trim("\"' \t");
6267cc0636SGreg Clayton       m_value_was_set = true;
638f3be7a3SJonas Devlieghere       m_current_value.SetFile(value.str(), FileSpec::Style::native);
648f3be7a3SJonas Devlieghere       if (m_resolve)
658f3be7a3SJonas Devlieghere         FileSystem::Instance().Resolve(m_current_value);
662e3881c0SJim Ingham       m_data_sp.reset();
67e877baa8SPavel Labath       m_data_mod_time = llvm::sys::TimePoint<>();
68332e8b1cSGreg Clayton       NotifyValueChanged();
69b9c1b51eSKate Stone     } else {
7067cc0636SGreg Clayton       error.SetErrorString("invalid value string");
7167cc0636SGreg Clayton     }
7267cc0636SGreg Clayton     break;
7367cc0636SGreg Clayton 
7467cc0636SGreg Clayton   case eVarSetOperationInsertBefore:
7567cc0636SGreg Clayton   case eVarSetOperationInsertAfter:
7667cc0636SGreg Clayton   case eVarSetOperationRemove:
7767cc0636SGreg Clayton   case eVarSetOperationAppend:
7867cc0636SGreg Clayton   case eVarSetOperationInvalid:
79c95f7e2aSPavel Labath     error = OptionValue::SetValueFromString(value, op);
8067cc0636SGreg Clayton     break;
8167cc0636SGreg Clayton   }
8267cc0636SGreg Clayton   return error;
8367cc0636SGreg Clayton }
8467cc0636SGreg Clayton 
AutoComplete(CommandInterpreter & interpreter,CompletionRequest & request)85ae34ed2cSRaphael Isemann void OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
86a2e76c0bSRaphael Isemann                                        CompletionRequest &request) {
87b9c1b51eSKate Stone   CommandCompletions::InvokeCommonCompletionCallbacks(
88a2e76c0bSRaphael Isemann       interpreter, m_completion_mask, request, nullptr);
8967cc0636SGreg Clayton }
9067cc0636SGreg Clayton 
GetFileContents()9150251fc7SPavel Labath const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
92b9c1b51eSKate Stone   if (m_current_value) {
9346376966SJonas Devlieghere     const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value);
9439fb1389SGreg Clayton     if (m_data_sp && m_data_mod_time == file_mod_time)
9539fb1389SGreg Clayton       return m_data_sp;
9687e403aaSJonas Devlieghere     m_data_sp =
9787e403aaSJonas Devlieghere         FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath());
9839fb1389SGreg Clayton     m_data_mod_time = file_mod_time;
990b0b512fSGreg Clayton   }
1006920b52bSGreg Clayton   return m_data_sp;
1016920b52bSGreg Clayton }
102