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