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 1267cc0636SGreg Clayton #include "lldb/Core/State.h" 135548cb50SEnrico Granata #include "lldb/DataFormatters/FormatManager.h" 141408bf72SPavel Labath #include "lldb/Host/FileSystem.h" 1567cc0636SGreg Clayton #include "lldb/Interpreter/Args.h" 1667cc0636SGreg Clayton #include "lldb/Interpreter/CommandCompletions.h" 17e1cfbc79STodd Fiala #include "lldb/Interpreter/CommandInterpreter.h" 18*7f6a7a37SZachary Turner #include "lldb/Utility/DataBufferLLVM.h" 1967cc0636SGreg Clayton 2067cc0636SGreg Clayton using namespace lldb; 2167cc0636SGreg Clayton using namespace lldb_private; 2267cc0636SGreg Clayton 23b9c1b51eSKate Stone OptionValueFileSpec::OptionValueFileSpec(bool resolve) 24b9c1b51eSKate Stone : OptionValue(), m_current_value(), m_default_value(), m_data_sp(), 2539fb1389SGreg Clayton m_data_mod_time(), 261f4706c3SVince Harron m_completion_mask(CommandCompletions::eDiskFileCompletion), 27b9c1b51eSKate Stone m_resolve(resolve) {} 28b5f0feabSGreg Clayton 29b9c1b51eSKate Stone OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve) 30b9c1b51eSKate Stone : OptionValue(), m_current_value(value), m_default_value(value), 31b9c1b51eSKate Stone m_data_sp(), m_data_mod_time(), 321f4706c3SVince Harron m_completion_mask(CommandCompletions::eDiskFileCompletion), 33b9c1b51eSKate Stone m_resolve(resolve) {} 34b5f0feabSGreg Clayton 35b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_value, 361f4706c3SVince Harron const FileSpec &default_value, 37b9c1b51eSKate Stone bool resolve) 38b9c1b51eSKate Stone : OptionValue(), m_current_value(current_value), 39b9c1b51eSKate Stone m_default_value(default_value), m_data_sp(), m_data_mod_time(), 401f4706c3SVince Harron m_completion_mask(CommandCompletions::eDiskFileCompletion), 41b9c1b51eSKate Stone m_resolve(resolve) {} 42b5f0feabSGreg Clayton 43b9c1b51eSKate Stone void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx, 44b9c1b51eSKate Stone Stream &strm, uint32_t dump_mask) { 4567cc0636SGreg Clayton if (dump_mask & eDumpOptionType) 4667cc0636SGreg Clayton strm.Printf("(%s)", GetTypeAsCString()); 47b9c1b51eSKate Stone if (dump_mask & eDumpOptionValue) { 4867cc0636SGreg Clayton if (dump_mask & eDumpOptionType) 4967cc0636SGreg Clayton strm.PutCString(" = "); 5067cc0636SGreg Clayton 51b9c1b51eSKate Stone if (m_current_value) { 52b5ad4ec7SGreg Clayton strm << '"' << m_current_value.GetPath().c_str() << '"'; 5367cc0636SGreg Clayton } 5467cc0636SGreg Clayton } 5567cc0636SGreg Clayton } 5667cc0636SGreg Clayton 57b9c1b51eSKate Stone Error OptionValueFileSpec::SetValueFromString(llvm::StringRef value, 58b9c1b51eSKate Stone VarSetOperationType op) { 5967cc0636SGreg Clayton Error error; 60b9c1b51eSKate Stone switch (op) { 6167cc0636SGreg Clayton case eVarSetOperationClear: 6267cc0636SGreg Clayton Clear(); 63332e8b1cSGreg Clayton NotifyValueChanged(); 6467cc0636SGreg Clayton break; 6567cc0636SGreg Clayton 6667cc0636SGreg Clayton case eVarSetOperationReplace: 6767cc0636SGreg Clayton case eVarSetOperationAssign: 68b9c1b51eSKate Stone if (value.size() > 0) { 69b9c1b51eSKate Stone // The setting value may have whitespace, double-quotes, or single-quotes 70b9c1b51eSKate Stone // around the file 71b9c1b51eSKate Stone // path to indicate that internal spaces are not word breaks. Strip off 72b9c1b51eSKate Stone // any ws & quotes 73b9c1b51eSKate Stone // from the start and end of the file path - we aren't doing any word // 74b9c1b51eSKate Stone // breaking here so 75b9c1b51eSKate Stone // the quoting is unnecessary. NB this will cause a problem if someone 76b9c1b51eSKate Stone // tries to specify 77b9c1b51eSKate Stone // a file path that legitimately begins or ends with a " or ' character, 78b9c1b51eSKate Stone // or whitespace. 79c95f7e2aSPavel Labath value = value.trim("\"' \t"); 8067cc0636SGreg Clayton m_value_was_set = true; 81771ef6d4SMalcolm Parsons m_current_value.SetFile(value.str(), m_resolve); 822e3881c0SJim Ingham m_data_sp.reset(); 83e877baa8SPavel Labath m_data_mod_time = llvm::sys::TimePoint<>(); 84332e8b1cSGreg Clayton NotifyValueChanged(); 85b9c1b51eSKate Stone } else { 8667cc0636SGreg Clayton error.SetErrorString("invalid value string"); 8767cc0636SGreg Clayton } 8867cc0636SGreg Clayton break; 8967cc0636SGreg Clayton 9067cc0636SGreg Clayton case eVarSetOperationInsertBefore: 9167cc0636SGreg Clayton case eVarSetOperationInsertAfter: 9267cc0636SGreg Clayton case eVarSetOperationRemove: 9367cc0636SGreg Clayton case eVarSetOperationAppend: 9467cc0636SGreg Clayton case eVarSetOperationInvalid: 95c95f7e2aSPavel Labath error = OptionValue::SetValueFromString(value, op); 9667cc0636SGreg Clayton break; 9767cc0636SGreg Clayton } 9867cc0636SGreg Clayton return error; 9967cc0636SGreg Clayton } 10067cc0636SGreg Clayton 101b9c1b51eSKate Stone lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const { 10267cc0636SGreg Clayton return OptionValueSP(new OptionValueFileSpec(*this)); 10367cc0636SGreg Clayton } 10467cc0636SGreg Clayton 1054aa8753cSZachary Turner size_t OptionValueFileSpec::AutoComplete( 1064aa8753cSZachary Turner CommandInterpreter &interpreter, llvm::StringRef s, int match_start_point, 1074aa8753cSZachary Turner int max_return_elements, bool &word_complete, StringList &matches) { 10867cc0636SGreg Clayton word_complete = false; 10967cc0636SGreg Clayton matches.Clear(); 110b9c1b51eSKate Stone CommandCompletions::InvokeCommonCompletionCallbacks( 111b9c1b51eSKate Stone interpreter, m_completion_mask, s, match_start_point, max_return_elements, 112b9c1b51eSKate Stone nullptr, word_complete, matches); 11367cc0636SGreg Clayton return matches.GetSize(); 11467cc0636SGreg Clayton } 11567cc0636SGreg Clayton 1166920b52bSGreg Clayton const lldb::DataBufferSP & 117b9c1b51eSKate Stone OptionValueFileSpec::GetFileContents(bool null_terminate) { 118b9c1b51eSKate Stone if (m_current_value) { 119e877baa8SPavel Labath const auto file_mod_time = FileSystem::GetModificationTime(m_current_value); 12039fb1389SGreg Clayton if (m_data_sp && m_data_mod_time == file_mod_time) 12139fb1389SGreg Clayton return m_data_sp; 122*7f6a7a37SZachary Turner m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath(), 123*7f6a7a37SZachary Turner null_terminate); 12439fb1389SGreg Clayton m_data_mod_time = file_mod_time; 1250b0b512fSGreg Clayton } 1266920b52bSGreg Clayton return m_data_sp; 1276920b52bSGreg Clayton } 128