1 //===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Interpreter/OptionValueFileSpec.h" 11 12 #include "lldb/DataFormatters/FormatManager.h" 13 #include "lldb/Host/FileSystem.h" 14 #include "lldb/Interpreter/CommandCompletions.h" 15 #include "lldb/Interpreter/CommandInterpreter.h" 16 #include "lldb/Utility/Args.h" 17 #include "lldb/Utility/DataBufferLLVM.h" 18 #include "lldb/Utility/State.h" 19 20 using namespace lldb; 21 using namespace lldb_private; 22 23 OptionValueFileSpec::OptionValueFileSpec(bool resolve) 24 : OptionValue(), m_current_value(), m_default_value(), m_data_sp(), 25 m_data_mod_time(), 26 m_completion_mask(CommandCompletions::eDiskFileCompletion), 27 m_resolve(resolve) {} 28 29 OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve) 30 : OptionValue(), m_current_value(value), m_default_value(value), 31 m_data_sp(), m_data_mod_time(), 32 m_completion_mask(CommandCompletions::eDiskFileCompletion), 33 m_resolve(resolve) {} 34 35 OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_value, 36 const FileSpec &default_value, 37 bool resolve) 38 : OptionValue(), m_current_value(current_value), 39 m_default_value(default_value), m_data_sp(), m_data_mod_time(), 40 m_completion_mask(CommandCompletions::eDiskFileCompletion), 41 m_resolve(resolve) {} 42 43 void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx, 44 Stream &strm, uint32_t dump_mask) { 45 if (dump_mask & eDumpOptionType) 46 strm.Printf("(%s)", GetTypeAsCString()); 47 if (dump_mask & eDumpOptionValue) { 48 if (dump_mask & eDumpOptionType) 49 strm.PutCString(" = "); 50 51 if (m_current_value) { 52 strm << '"' << m_current_value.GetPath().c_str() << '"'; 53 } 54 } 55 } 56 57 Status OptionValueFileSpec::SetValueFromString(llvm::StringRef value, 58 VarSetOperationType op) { 59 Status error; 60 switch (op) { 61 case eVarSetOperationClear: 62 Clear(); 63 NotifyValueChanged(); 64 break; 65 66 case eVarSetOperationReplace: 67 case eVarSetOperationAssign: 68 if (value.size() > 0) { 69 // The setting value may have whitespace, double-quotes, or single-quotes 70 // around the file path to indicate that internal spaces are not word 71 // breaks. Strip off any ws & quotes from the start and end of the file 72 // path - we aren't doing any word // breaking here so the quoting is 73 // unnecessary. NB this will cause a problem if someone tries to specify 74 // a file path that legitimately begins or ends with a " or ' character, 75 // or whitespace. 76 value = value.trim("\"' \t"); 77 m_value_was_set = true; 78 m_current_value.SetFile(value.str(), FileSpec::Style::native); 79 if (m_resolve) 80 FileSystem::Instance().Resolve(m_current_value); 81 m_data_sp.reset(); 82 m_data_mod_time = llvm::sys::TimePoint<>(); 83 NotifyValueChanged(); 84 } else { 85 error.SetErrorString("invalid value string"); 86 } 87 break; 88 89 case eVarSetOperationInsertBefore: 90 case eVarSetOperationInsertAfter: 91 case eVarSetOperationRemove: 92 case eVarSetOperationAppend: 93 case eVarSetOperationInvalid: 94 error = OptionValue::SetValueFromString(value, op); 95 break; 96 } 97 return error; 98 } 99 100 lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const { 101 return OptionValueSP(new OptionValueFileSpec(*this)); 102 } 103 104 size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter, 105 CompletionRequest &request) { 106 request.SetWordComplete(false); 107 CommandCompletions::InvokeCommonCompletionCallbacks( 108 interpreter, m_completion_mask, request, nullptr); 109 return request.GetNumberOfMatches(); 110 } 111 112 const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() { 113 if (m_current_value) { 114 const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value); 115 if (m_data_sp && m_data_mod_time == file_mod_time) 116 return m_data_sp; 117 m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath()); 118 m_data_mod_time = file_mod_time; 119 } 120 return m_data_sp; 121 } 122