Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 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 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 12 | #include "lldb/Core/State.h" |
Enrico Granata | 5548cb5 | 2013-01-28 23:47:25 +0000 | [diff] [blame] | 13 | #include "lldb/DataFormatters/FormatManager.h" |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 14 | #include "lldb/Host/FileSystem.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 15 | #include "lldb/Interpreter/Args.h" |
| 16 | #include "lldb/Interpreter/CommandCompletions.h" |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | OptionValueFileSpec::OptionValueFileSpec(bool resolve) |
| 23 | : OptionValue(), m_current_value(), m_default_value(), m_data_sp(), |
| 24 | m_data_mod_time(), |
| 25 | m_completion_mask(CommandCompletions::eDiskFileCompletion), |
| 26 | m_resolve(resolve) {} |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve) |
| 29 | : OptionValue(), m_current_value(value), m_default_value(value), |
| 30 | m_data_sp(), m_data_mod_time(), |
| 31 | m_completion_mask(CommandCompletions::eDiskFileCompletion), |
| 32 | m_resolve(resolve) {} |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 33 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_value, |
| 35 | const FileSpec &default_value, |
| 36 | bool resolve) |
| 37 | : OptionValue(), m_current_value(current_value), |
| 38 | m_default_value(default_value), m_data_sp(), m_data_mod_time(), |
| 39 | m_completion_mask(CommandCompletions::eDiskFileCompletion), |
| 40 | m_resolve(resolve) {} |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 41 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx, |
| 43 | Stream &strm, uint32_t dump_mask) { |
| 44 | if (dump_mask & eDumpOptionType) |
| 45 | strm.Printf("(%s)", GetTypeAsCString()); |
| 46 | if (dump_mask & eDumpOptionValue) { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 47 | if (dump_mask & eDumpOptionType) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | strm.PutCString(" = "); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | if (m_current_value) { |
| 51 | strm << '"' << m_current_value.GetPath().c_str() << '"'; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 52 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | Error OptionValueFileSpec::SetValueFromString(llvm::StringRef value, |
| 57 | VarSetOperationType op) { |
| 58 | Error error; |
| 59 | switch (op) { |
| 60 | case eVarSetOperationClear: |
| 61 | Clear(); |
| 62 | NotifyValueChanged(); |
| 63 | break; |
| 64 | |
| 65 | case eVarSetOperationReplace: |
| 66 | case eVarSetOperationAssign: |
| 67 | if (value.size() > 0) { |
| 68 | // The setting value may have whitespace, double-quotes, or single-quotes |
| 69 | // around the file |
| 70 | // path to indicate that internal spaces are not word breaks. Strip off |
| 71 | // any ws & quotes |
| 72 | // from the start and end of the file path - we aren't doing any word // |
| 73 | // breaking here so |
| 74 | // the quoting is unnecessary. NB this will cause a problem if someone |
| 75 | // tries to specify |
| 76 | // a file path that legitimately begins or ends with a " or ' character, |
| 77 | // or whitespace. |
| 78 | value = value.trim("\"' \t"); |
| 79 | m_value_was_set = true; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame^] | 80 | m_current_value.SetFile(value.str(), m_resolve); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | m_data_sp.reset(); |
| 82 | m_data_mod_time.Clear(); |
| 83 | NotifyValueChanged(); |
| 84 | } else { |
| 85 | error.SetErrorString("invalid value string"); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 86 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 100 | lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const { |
| 101 | return OptionValueSP(new OptionValueFileSpec(*this)); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter, |
| 105 | const char *s, int match_start_point, |
| 106 | int max_return_elements, |
| 107 | bool &word_complete, |
| 108 | StringList &matches) { |
| 109 | word_complete = false; |
| 110 | matches.Clear(); |
| 111 | CommandCompletions::InvokeCommonCompletionCallbacks( |
| 112 | interpreter, m_completion_mask, s, match_start_point, max_return_elements, |
| 113 | nullptr, word_complete, matches); |
| 114 | return matches.GetSize(); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 117 | const lldb::DataBufferSP & |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | OptionValueFileSpec::GetFileContents(bool null_terminate) { |
| 119 | if (m_current_value) { |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 120 | const TimeValue file_mod_time = |
| 121 | FileSystem::GetModificationTime(m_current_value); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | if (m_data_sp && m_data_mod_time == file_mod_time) |
| 123 | return m_data_sp; |
| 124 | if (null_terminate) |
| 125 | m_data_sp = m_current_value.ReadFileContentsAsCString(); |
| 126 | else |
| 127 | m_data_sp = m_current_value.ReadFileContents(); |
| 128 | m_data_mod_time = file_mod_time; |
| 129 | } |
| 130 | return m_data_sp; |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 131 | } |