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 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 16 | #include "lldb/Core/State.h" |
Enrico Granata | 5548cb5 | 2013-01-28 23:47:25 +0000 | [diff] [blame] | 17 | #include "lldb/DataFormatters/FormatManager.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/Args.h" |
| 19 | #include "lldb/Interpreter/CommandCompletions.h" |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame^] | 20 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 25 | |
Vince Harron | 1f4706c | 2015-02-18 23:12:26 +0000 | [diff] [blame] | 26 | OptionValueFileSpec::OptionValueFileSpec (bool resolve) : |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 27 | OptionValue(), |
| 28 | m_current_value (), |
| 29 | m_default_value (), |
| 30 | m_data_sp(), |
Greg Clayton | 39fb138 | 2015-03-06 23:46:54 +0000 | [diff] [blame] | 31 | m_data_mod_time (), |
Vince Harron | 1f4706c | 2015-02-18 23:12:26 +0000 | [diff] [blame] | 32 | m_completion_mask (CommandCompletions::eDiskFileCompletion), |
| 33 | m_resolve (resolve) |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
Vince Harron | 1f4706c | 2015-02-18 23:12:26 +0000 | [diff] [blame] | 37 | OptionValueFileSpec::OptionValueFileSpec (const FileSpec &value, |
| 38 | bool resolve) : |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 39 | OptionValue(), |
| 40 | m_current_value (value), |
| 41 | m_default_value (value), |
| 42 | m_data_sp(), |
Greg Clayton | 39fb138 | 2015-03-06 23:46:54 +0000 | [diff] [blame] | 43 | m_data_mod_time (), |
Vince Harron | 1f4706c | 2015-02-18 23:12:26 +0000 | [diff] [blame] | 44 | m_completion_mask (CommandCompletions::eDiskFileCompletion), |
| 45 | m_resolve (resolve) |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | OptionValueFileSpec::OptionValueFileSpec (const FileSpec ¤t_value, |
Vince Harron | 1f4706c | 2015-02-18 23:12:26 +0000 | [diff] [blame] | 50 | const FileSpec &default_value, |
| 51 | bool resolve) : |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 52 | OptionValue(), |
| 53 | m_current_value (current_value), |
| 54 | m_default_value (default_value), |
| 55 | m_data_sp(), |
Greg Clayton | 39fb138 | 2015-03-06 23:46:54 +0000 | [diff] [blame] | 56 | m_data_mod_time (), |
Vince Harron | 1f4706c | 2015-02-18 23:12:26 +0000 | [diff] [blame] | 57 | m_completion_mask (CommandCompletions::eDiskFileCompletion), |
| 58 | m_resolve (resolve) |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 62 | void |
| 63 | OptionValueFileSpec::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) |
| 64 | { |
| 65 | if (dump_mask & eDumpOptionType) |
| 66 | strm.Printf ("(%s)", GetTypeAsCString ()); |
| 67 | if (dump_mask & eDumpOptionValue) |
| 68 | { |
| 69 | if (dump_mask & eDumpOptionType) |
| 70 | strm.PutCString (" = "); |
| 71 | |
| 72 | if (m_current_value) |
| 73 | { |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 74 | strm << '"' << m_current_value.GetPath().c_str() << '"'; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | Error |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 80 | OptionValueFileSpec::SetValueFromString (llvm::StringRef value, |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 81 | VarSetOperationType op) |
| 82 | { |
| 83 | Error error; |
| 84 | switch (op) |
| 85 | { |
| 86 | case eVarSetOperationClear: |
| 87 | Clear (); |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 88 | NotifyValueChanged(); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 89 | break; |
| 90 | |
| 91 | case eVarSetOperationReplace: |
| 92 | case eVarSetOperationAssign: |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 93 | if (value.size() > 0) |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 94 | { |
Jason Molenda | 5c98b1c | 2013-09-13 02:33:15 +0000 | [diff] [blame] | 95 | // The setting value may have whitespace, double-quotes, or single-quotes around the file |
| 96 | // path to indicate that internal spaces are not word breaks. Strip off any ws & quotes |
| 97 | // from the start and end of the file path - we aren't doing any word // breaking here so |
| 98 | // the quoting is unnecessary. NB this will cause a problem if someone tries to specify |
| 99 | // a file path that legitimately begins or ends with a " or ' character, or whitespace. |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 100 | value = value.trim("\"' \t"); |
Jason Molenda | 36d4461 | 2013-08-27 04:58:31 +0000 | [diff] [blame] | 101 | m_value_was_set = true; |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 102 | m_current_value.SetFile(value.str().c_str(), m_resolve); |
Jim Ingham | 2e3881c | 2014-04-04 18:06:54 +0000 | [diff] [blame] | 103 | m_data_sp.reset(); |
Greg Clayton | 39fb138 | 2015-03-06 23:46:54 +0000 | [diff] [blame] | 104 | m_data_mod_time.Clear(); |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 105 | NotifyValueChanged(); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 106 | } |
| 107 | else |
| 108 | { |
| 109 | error.SetErrorString("invalid value string"); |
| 110 | } |
| 111 | break; |
| 112 | |
| 113 | case eVarSetOperationInsertBefore: |
| 114 | case eVarSetOperationInsertAfter: |
| 115 | case eVarSetOperationRemove: |
| 116 | case eVarSetOperationAppend: |
| 117 | case eVarSetOperationInvalid: |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 118 | error = OptionValue::SetValueFromString (value, op); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | return error; |
| 122 | } |
| 123 | |
| 124 | lldb::OptionValueSP |
| 125 | OptionValueFileSpec::DeepCopy () const |
| 126 | { |
| 127 | return OptionValueSP(new OptionValueFileSpec(*this)); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | size_t |
| 132 | OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter, |
| 133 | const char *s, |
| 134 | int match_start_point, |
| 135 | int max_return_elements, |
| 136 | bool &word_complete, |
| 137 | StringList &matches) |
| 138 | { |
| 139 | word_complete = false; |
| 140 | matches.Clear(); |
| 141 | CommandCompletions::InvokeCommonCompletionCallbacks (interpreter, |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 142 | m_completion_mask, |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 143 | s, |
| 144 | match_start_point, |
| 145 | max_return_elements, |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 146 | nullptr, |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 147 | word_complete, |
| 148 | matches); |
| 149 | return matches.GetSize(); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 154 | const lldb::DataBufferSP & |
Greg Clayton | 0b0b512 | 2012-08-30 18:15:10 +0000 | [diff] [blame] | 155 | OptionValueFileSpec::GetFileContents(bool null_terminate) |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 156 | { |
Greg Clayton | 39fb138 | 2015-03-06 23:46:54 +0000 | [diff] [blame] | 157 | if (m_current_value) |
Greg Clayton | 0b0b512 | 2012-08-30 18:15:10 +0000 | [diff] [blame] | 158 | { |
Greg Clayton | 39fb138 | 2015-03-06 23:46:54 +0000 | [diff] [blame] | 159 | const TimeValue file_mod_time = m_current_value.GetModificationTime(); |
| 160 | if (m_data_sp && m_data_mod_time == file_mod_time) |
| 161 | return m_data_sp; |
Greg Clayton | 0b0b512 | 2012-08-30 18:15:10 +0000 | [diff] [blame] | 162 | if (null_terminate) |
| 163 | m_data_sp = m_current_value.ReadFileContentsAsCString(); |
| 164 | else |
| 165 | m_data_sp = m_current_value.ReadFileContents(); |
Greg Clayton | 39fb138 | 2015-03-06 23:46:54 +0000 | [diff] [blame] | 166 | m_data_mod_time = file_mod_time; |
Greg Clayton | 0b0b512 | 2012-08-30 18:15:10 +0000 | [diff] [blame] | 167 | } |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 168 | return m_data_sp; |
| 169 | } |
| 170 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 171 | |