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