1cfca06d7SDimitry Andric //===-- OptionValueFileSpec.cpp -------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Interpreter/OptionValueFileSpec.h"
10f034231aSEd Maste
11f034231aSEd Maste #include "lldb/DataFormatters/FormatManager.h"
1214f1b3e8SDimitry Andric #include "lldb/Host/FileSystem.h"
13f034231aSEd Maste #include "lldb/Interpreter/CommandCompletions.h"
1414f1b3e8SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
15f73363f1SDimitry Andric #include "lldb/Utility/Args.h"
1694994d37SDimitry Andric #include "lldb/Utility/State.h"
17f034231aSEd Maste
18f034231aSEd Maste using namespace lldb;
19f034231aSEd Maste using namespace lldb_private;
20f034231aSEd Maste
OptionValueFileSpec(bool resolve)21344a3780SDimitry Andric OptionValueFileSpec::OptionValueFileSpec(bool resolve) : m_resolve(resolve) {}
22f034231aSEd Maste
OptionValueFileSpec(const FileSpec & value,bool resolve)2314f1b3e8SDimitry Andric OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
24344a3780SDimitry Andric : m_current_value(value), m_default_value(value),
25145449b1SDimitry Andric
2614f1b3e8SDimitry Andric m_resolve(resolve) {}
27f034231aSEd Maste
OptionValueFileSpec(const FileSpec & current_value,const FileSpec & default_value,bool resolve)28f034231aSEd Maste OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_value,
295e95aa85SEd Maste const FileSpec &default_value,
3014f1b3e8SDimitry Andric bool resolve)
31344a3780SDimitry Andric : m_current_value(current_value), m_default_value(default_value),
32145449b1SDimitry Andric
3314f1b3e8SDimitry Andric m_resolve(resolve) {}
34f034231aSEd Maste
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)3514f1b3e8SDimitry Andric void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
3614f1b3e8SDimitry Andric Stream &strm, uint32_t dump_mask) {
37f034231aSEd Maste if (dump_mask & eDumpOptionType)
38f034231aSEd Maste strm.Printf("(%s)", GetTypeAsCString());
3914f1b3e8SDimitry Andric if (dump_mask & eDumpOptionValue) {
40f034231aSEd Maste if (dump_mask & eDumpOptionType)
41f034231aSEd Maste strm.PutCString(" = ");
42f034231aSEd Maste
4314f1b3e8SDimitry Andric if (m_current_value) {
44f034231aSEd Maste strm << '"' << m_current_value.GetPath().c_str() << '"';
45f034231aSEd Maste }
46f034231aSEd Maste }
47f034231aSEd Maste }
48f034231aSEd Maste
SetValueFromString(llvm::StringRef value,VarSetOperationType op)49b76161e4SDimitry Andric Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
5014f1b3e8SDimitry Andric VarSetOperationType op) {
51b76161e4SDimitry Andric Status error;
5214f1b3e8SDimitry Andric switch (op) {
53f034231aSEd Maste case eVarSetOperationClear:
54f034231aSEd Maste Clear();
55205afe67SEd Maste NotifyValueChanged();
56f034231aSEd Maste break;
57f034231aSEd Maste
58f034231aSEd Maste case eVarSetOperationReplace:
59f034231aSEd Maste case eVarSetOperationAssign:
6014f1b3e8SDimitry Andric if (value.size() > 0) {
615e95aa85SEd Maste value = value.trim("\"' \t");
62f034231aSEd Maste m_value_was_set = true;
6394994d37SDimitry Andric m_current_value.SetFile(value.str(), FileSpec::Style::native);
6494994d37SDimitry Andric if (m_resolve)
6594994d37SDimitry Andric FileSystem::Instance().Resolve(m_current_value);
660cac4ca3SEd Maste m_data_sp.reset();
6714f1b3e8SDimitry Andric m_data_mod_time = llvm::sys::TimePoint<>();
68205afe67SEd Maste NotifyValueChanged();
6914f1b3e8SDimitry Andric } else {
70f034231aSEd Maste error.SetErrorString("invalid value string");
71f034231aSEd Maste }
72f034231aSEd Maste break;
73f034231aSEd Maste
74f034231aSEd Maste case eVarSetOperationInsertBefore:
75f034231aSEd Maste case eVarSetOperationInsertAfter:
76f034231aSEd Maste case eVarSetOperationRemove:
77f034231aSEd Maste case eVarSetOperationAppend:
78f034231aSEd Maste case eVarSetOperationInvalid:
795e95aa85SEd Maste error = OptionValue::SetValueFromString(value, op);
80f034231aSEd Maste break;
81f034231aSEd Maste }
82f034231aSEd Maste return error;
83f034231aSEd Maste }
84f034231aSEd Maste
AutoComplete(CommandInterpreter & interpreter,CompletionRequest & request)85ead24645SDimitry Andric void OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
86f73363f1SDimitry Andric CompletionRequest &request) {
877fa27ce4SDimitry Andric lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
88f73363f1SDimitry Andric interpreter, m_completion_mask, request, nullptr);
89f034231aSEd Maste }
90f034231aSEd Maste
GetFileContents()9123629167SDimitry Andric const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
9214f1b3e8SDimitry Andric if (m_current_value) {
9394994d37SDimitry Andric const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value);
945e95aa85SEd Maste if (m_data_sp && m_data_mod_time == file_mod_time)
955e95aa85SEd Maste return m_data_sp;
9694994d37SDimitry Andric m_data_sp =
9794994d37SDimitry Andric FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath());
985e95aa85SEd Maste m_data_mod_time = file_mod_time;
99f034231aSEd Maste }
100f034231aSEd Maste return m_data_sp;
101f034231aSEd Maste }
102