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 125548cb50SEnrico Granata #include "lldb/DataFormatters/FormatManager.h" 131408bf72SPavel Labath #include "lldb/Host/FileSystem.h" 1467cc0636SGreg Clayton #include "lldb/Interpreter/CommandCompletions.h" 15e1cfbc79STodd Fiala #include "lldb/Interpreter/CommandInterpreter.h" 16145d95c9SPavel Labath #include "lldb/Utility/Args.h" 17d821c997SPavel Labath #include "lldb/Utility/State.h" 1867cc0636SGreg Clayton 1967cc0636SGreg Clayton using namespace lldb; 2067cc0636SGreg Clayton using namespace lldb_private; 2167cc0636SGreg Clayton 22b9c1b51eSKate Stone OptionValueFileSpec::OptionValueFileSpec(bool resolve) 23b9c1b51eSKate Stone : OptionValue(), m_current_value(), m_default_value(), m_data_sp(), 2439fb1389SGreg Clayton m_data_mod_time(), 251f4706c3SVince Harron m_completion_mask(CommandCompletions::eDiskFileCompletion), 26b9c1b51eSKate Stone m_resolve(resolve) {} 27b5f0feabSGreg Clayton 28b9c1b51eSKate Stone OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve) 29b9c1b51eSKate Stone : OptionValue(), m_current_value(value), m_default_value(value), 30b9c1b51eSKate Stone m_data_sp(), m_data_mod_time(), 311f4706c3SVince Harron m_completion_mask(CommandCompletions::eDiskFileCompletion), 32b9c1b51eSKate Stone m_resolve(resolve) {} 33b5f0feabSGreg Clayton 34b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_value, 351f4706c3SVince Harron const FileSpec &default_value, 36b9c1b51eSKate Stone bool resolve) 37b9c1b51eSKate Stone : OptionValue(), m_current_value(current_value), 38b9c1b51eSKate Stone m_default_value(default_value), m_data_sp(), m_data_mod_time(), 391f4706c3SVince Harron m_completion_mask(CommandCompletions::eDiskFileCompletion), 40b9c1b51eSKate Stone m_resolve(resolve) {} 41b5f0feabSGreg Clayton 42b9c1b51eSKate Stone void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx, 43b9c1b51eSKate Stone Stream &strm, uint32_t dump_mask) { 4467cc0636SGreg Clayton if (dump_mask & eDumpOptionType) 4567cc0636SGreg Clayton strm.Printf("(%s)", GetTypeAsCString()); 46b9c1b51eSKate Stone if (dump_mask & eDumpOptionValue) { 4767cc0636SGreg Clayton if (dump_mask & eDumpOptionType) 4867cc0636SGreg Clayton strm.PutCString(" = "); 4967cc0636SGreg Clayton 50b9c1b51eSKate Stone if (m_current_value) { 51b5ad4ec7SGreg Clayton strm << '"' << m_current_value.GetPath().c_str() << '"'; 5267cc0636SGreg Clayton } 5367cc0636SGreg Clayton } 5467cc0636SGreg Clayton } 5567cc0636SGreg Clayton 5697206d57SZachary Turner Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value, 57b9c1b51eSKate Stone VarSetOperationType op) { 5897206d57SZachary Turner Status error; 59b9c1b51eSKate Stone switch (op) { 6067cc0636SGreg Clayton case eVarSetOperationClear: 6167cc0636SGreg Clayton Clear(); 62332e8b1cSGreg Clayton NotifyValueChanged(); 6367cc0636SGreg Clayton break; 6467cc0636SGreg Clayton 6567cc0636SGreg Clayton case eVarSetOperationReplace: 6667cc0636SGreg Clayton case eVarSetOperationAssign: 67b9c1b51eSKate Stone if (value.size() > 0) { 68b9c1b51eSKate Stone // The setting value may have whitespace, double-quotes, or single-quotes 6905097246SAdrian Prantl // around the file path to indicate that internal spaces are not word 7005097246SAdrian Prantl // breaks. Strip off any ws & quotes from the start and end of the file 7105097246SAdrian Prantl // path - we aren't doing any word // breaking here so the quoting is 7205097246SAdrian Prantl // unnecessary. NB this will cause a problem if someone tries to specify 73b9c1b51eSKate Stone // a file path that legitimately begins or ends with a " or ' character, 74b9c1b51eSKate Stone // or whitespace. 75c95f7e2aSPavel Labath value = value.trim("\"' \t"); 7667cc0636SGreg Clayton m_value_was_set = true; 778f3be7a3SJonas Devlieghere m_current_value.SetFile(value.str(), FileSpec::Style::native); 788f3be7a3SJonas Devlieghere if (m_resolve) 798f3be7a3SJonas Devlieghere FileSystem::Instance().Resolve(m_current_value); 802e3881c0SJim Ingham m_data_sp.reset(); 81e877baa8SPavel Labath m_data_mod_time = llvm::sys::TimePoint<>(); 82332e8b1cSGreg Clayton NotifyValueChanged(); 83b9c1b51eSKate Stone } else { 8467cc0636SGreg Clayton error.SetErrorString("invalid value string"); 8567cc0636SGreg Clayton } 8667cc0636SGreg Clayton break; 8767cc0636SGreg Clayton 8867cc0636SGreg Clayton case eVarSetOperationInsertBefore: 8967cc0636SGreg Clayton case eVarSetOperationInsertAfter: 9067cc0636SGreg Clayton case eVarSetOperationRemove: 9167cc0636SGreg Clayton case eVarSetOperationAppend: 9267cc0636SGreg Clayton case eVarSetOperationInvalid: 93c95f7e2aSPavel Labath error = OptionValue::SetValueFromString(value, op); 9467cc0636SGreg Clayton break; 9567cc0636SGreg Clayton } 9667cc0636SGreg Clayton return error; 9767cc0636SGreg Clayton } 9867cc0636SGreg Clayton 99b9c1b51eSKate Stone lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const { 10067cc0636SGreg Clayton return OptionValueSP(new OptionValueFileSpec(*this)); 10167cc0636SGreg Clayton } 10267cc0636SGreg Clayton 103a2e76c0bSRaphael Isemann size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter, 104a2e76c0bSRaphael Isemann CompletionRequest &request) { 105a2e76c0bSRaphael Isemann request.SetWordComplete(false); 106b9c1b51eSKate Stone CommandCompletions::InvokeCommonCompletionCallbacks( 107a2e76c0bSRaphael Isemann interpreter, m_completion_mask, request, nullptr); 1081a6d7ab5SRaphael Isemann return request.GetNumberOfMatches(); 10967cc0636SGreg Clayton } 11067cc0636SGreg Clayton 11150251fc7SPavel Labath const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() { 112b9c1b51eSKate Stone if (m_current_value) { 11346376966SJonas Devlieghere const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value); 11439fb1389SGreg Clayton if (m_data_sp && m_data_mod_time == file_mod_time) 11539fb1389SGreg Clayton return m_data_sp; 116*87e403aaSJonas Devlieghere m_data_sp = 117*87e403aaSJonas Devlieghere FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath()); 11839fb1389SGreg Clayton m_data_mod_time = file_mod_time; 1190b0b512fSGreg Clayton } 1206920b52bSGreg Clayton return m_data_sp; 1216920b52bSGreg Clayton } 122