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" 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 56b9c1b51eSKate Stone Error OptionValueFileSpec::SetValueFromString(llvm::StringRef value, 57b9c1b51eSKate Stone VarSetOperationType op) { 5867cc0636SGreg Clayton Error 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 69b9c1b51eSKate Stone // around the file 70b9c1b51eSKate Stone // path to indicate that internal spaces are not word breaks. Strip off 71b9c1b51eSKate Stone // any ws & quotes 72b9c1b51eSKate Stone // from the start and end of the file path - we aren't doing any word // 73b9c1b51eSKate Stone // breaking here so 74b9c1b51eSKate Stone // the quoting is unnecessary. NB this will cause a problem if someone 75b9c1b51eSKate Stone // tries to specify 76b9c1b51eSKate Stone // a file path that legitimately begins or ends with a " or ' character, 77b9c1b51eSKate Stone // or whitespace. 78c95f7e2aSPavel Labath value = value.trim("\"' \t"); 7967cc0636SGreg Clayton m_value_was_set = true; 80*771ef6d4SMalcolm Parsons m_current_value.SetFile(value.str(), m_resolve); 812e3881c0SJim Ingham m_data_sp.reset(); 8239fb1389SGreg Clayton m_data_mod_time.Clear(); 83332e8b1cSGreg Clayton NotifyValueChanged(); 84b9c1b51eSKate Stone } else { 8567cc0636SGreg Clayton error.SetErrorString("invalid value string"); 8667cc0636SGreg Clayton } 8767cc0636SGreg Clayton break; 8867cc0636SGreg Clayton 8967cc0636SGreg Clayton case eVarSetOperationInsertBefore: 9067cc0636SGreg Clayton case eVarSetOperationInsertAfter: 9167cc0636SGreg Clayton case eVarSetOperationRemove: 9267cc0636SGreg Clayton case eVarSetOperationAppend: 9367cc0636SGreg Clayton case eVarSetOperationInvalid: 94c95f7e2aSPavel Labath error = OptionValue::SetValueFromString(value, op); 9567cc0636SGreg Clayton break; 9667cc0636SGreg Clayton } 9767cc0636SGreg Clayton return error; 9867cc0636SGreg Clayton } 9967cc0636SGreg Clayton 100b9c1b51eSKate Stone lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const { 10167cc0636SGreg Clayton return OptionValueSP(new OptionValueFileSpec(*this)); 10267cc0636SGreg Clayton } 10367cc0636SGreg Clayton 104b9c1b51eSKate Stone size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter, 105b9c1b51eSKate Stone const char *s, int match_start_point, 10667cc0636SGreg Clayton int max_return_elements, 10767cc0636SGreg Clayton bool &word_complete, 108b9c1b51eSKate Stone StringList &matches) { 10967cc0636SGreg Clayton word_complete = false; 11067cc0636SGreg Clayton matches.Clear(); 111b9c1b51eSKate Stone CommandCompletions::InvokeCommonCompletionCallbacks( 112b9c1b51eSKate Stone interpreter, m_completion_mask, s, match_start_point, max_return_elements, 113b9c1b51eSKate Stone nullptr, word_complete, matches); 11467cc0636SGreg Clayton return matches.GetSize(); 11567cc0636SGreg Clayton } 11667cc0636SGreg Clayton 1176920b52bSGreg Clayton const lldb::DataBufferSP & 118b9c1b51eSKate Stone OptionValueFileSpec::GetFileContents(bool null_terminate) { 119b9c1b51eSKate Stone if (m_current_value) { 1201408bf72SPavel Labath const TimeValue file_mod_time = 1211408bf72SPavel Labath FileSystem::GetModificationTime(m_current_value); 12239fb1389SGreg Clayton if (m_data_sp && m_data_mod_time == file_mod_time) 12339fb1389SGreg Clayton return m_data_sp; 1240b0b512fSGreg Clayton if (null_terminate) 1250b0b512fSGreg Clayton m_data_sp = m_current_value.ReadFileContentsAsCString(); 1260b0b512fSGreg Clayton else 1276920b52bSGreg Clayton m_data_sp = m_current_value.ReadFileContents(); 12839fb1389SGreg Clayton m_data_mod_time = file_mod_time; 1290b0b512fSGreg Clayton } 1306920b52bSGreg Clayton return m_data_sp; 1316920b52bSGreg Clayton } 132