1 //===-- OptionValuePathMappings.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/OptionValuePathMappings.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Host/FileSystem.h" 17 #include "lldb/Host/StringConvert.h" 18 #include "lldb/Utility/Args.h" 19 #include "lldb/Utility/FileSpec.h" 20 #include "lldb/Utility/Stream.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 namespace { 25 static bool VerifyPathExists(const char *path) { 26 if (path && path[0]) 27 return FileSystem::Instance().Exists(path); 28 else 29 return false; 30 } 31 } 32 33 void OptionValuePathMappings::DumpValue(const ExecutionContext *exe_ctx, 34 Stream &strm, uint32_t dump_mask) { 35 if (dump_mask & eDumpOptionType) 36 strm.Printf("(%s)", GetTypeAsCString()); 37 if (dump_mask & eDumpOptionValue) { 38 if (dump_mask & eDumpOptionType) 39 strm.Printf(" =%s", (m_path_mappings.GetSize() > 0) ? "\n" : ""); 40 m_path_mappings.Dump(&strm); 41 } 42 } 43 44 Status OptionValuePathMappings::SetValueFromString(llvm::StringRef value, 45 VarSetOperationType op) { 46 Status error; 47 Args args(value.str()); 48 const size_t argc = args.GetArgumentCount(); 49 50 switch (op) { 51 case eVarSetOperationClear: 52 Clear(); 53 NotifyValueChanged(); 54 break; 55 56 case eVarSetOperationReplace: 57 // Must be at least one index + 1 pair of paths, and the pair count must be 58 // even 59 if (argc >= 3 && (((argc - 1) & 1) == 0)) { 60 uint32_t idx = 61 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX); 62 const uint32_t count = m_path_mappings.GetSize(); 63 if (idx > count) { 64 error.SetErrorStringWithFormat( 65 "invalid file list index %u, index must be 0 through %u", idx, 66 count); 67 } else { 68 bool changed = false; 69 for (size_t i = 1; i < argc; i += 2, ++idx) { 70 const char *orginal_path = args.GetArgumentAtIndex(i); 71 const char *replace_path = args.GetArgumentAtIndex(i + 1); 72 if (VerifyPathExists(replace_path)) { 73 ConstString a(orginal_path); 74 ConstString b(replace_path); 75 if (!m_path_mappings.Replace(a, b, idx, m_notify_changes)) 76 m_path_mappings.Append(a, b, m_notify_changes); 77 changed = true; 78 } else { 79 error.SetErrorStringWithFormat( 80 "the replacement path doesn't exist: \"%s\"", replace_path); 81 break; 82 } 83 } 84 if (changed) 85 NotifyValueChanged(); 86 } 87 } else { 88 error.SetErrorString("replace operation takes an array index followed by " 89 "one or more path pairs"); 90 } 91 break; 92 93 case eVarSetOperationAssign: 94 if (argc < 2 || (argc & 1)) { 95 error.SetErrorString("assign operation takes one or more path pairs"); 96 break; 97 } 98 m_path_mappings.Clear(m_notify_changes); 99 // Fall through to append case 100 LLVM_FALLTHROUGH; 101 case eVarSetOperationAppend: 102 if (argc < 2 || (argc & 1)) { 103 error.SetErrorString("append operation takes one or more path pairs"); 104 break; 105 } else { 106 bool changed = false; 107 for (size_t i = 0; i < argc; i += 2) { 108 const char *orginal_path = args.GetArgumentAtIndex(i); 109 const char *replace_path = args.GetArgumentAtIndex(i + 1); 110 if (VerifyPathExists(replace_path)) { 111 ConstString a(orginal_path); 112 ConstString b(replace_path); 113 m_path_mappings.Append(a, b, m_notify_changes); 114 m_value_was_set = true; 115 changed = true; 116 } else { 117 error.SetErrorStringWithFormat( 118 "the replacement path doesn't exist: \"%s\"", replace_path); 119 break; 120 } 121 } 122 if (changed) 123 NotifyValueChanged(); 124 } 125 break; 126 127 case eVarSetOperationInsertBefore: 128 case eVarSetOperationInsertAfter: 129 // Must be at least one index + 1 pair of paths, and the pair count must be 130 // even 131 if (argc >= 3 && (((argc - 1) & 1) == 0)) { 132 uint32_t idx = 133 StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX); 134 const uint32_t count = m_path_mappings.GetSize(); 135 if (idx > count) { 136 error.SetErrorStringWithFormat( 137 "invalid file list index %u, index must be 0 through %u", idx, 138 count); 139 } else { 140 bool changed = false; 141 if (op == eVarSetOperationInsertAfter) 142 ++idx; 143 for (size_t i = 1; i < argc; i += 2, ++idx) { 144 const char *orginal_path = args.GetArgumentAtIndex(i); 145 const char *replace_path = args.GetArgumentAtIndex(i + 1); 146 if (VerifyPathExists(replace_path)) { 147 ConstString a(orginal_path); 148 ConstString b(replace_path); 149 m_path_mappings.Insert(a, b, idx, m_notify_changes); 150 changed = true; 151 } else { 152 error.SetErrorStringWithFormat( 153 "the replacement path doesn't exist: \"%s\"", replace_path); 154 break; 155 } 156 } 157 if (changed) 158 NotifyValueChanged(); 159 } 160 } else { 161 error.SetErrorString("insert operation takes an array index followed by " 162 "one or more path pairs"); 163 } 164 break; 165 166 case eVarSetOperationRemove: 167 if (argc > 0) { 168 std::vector<int> remove_indexes; 169 bool all_indexes_valid = true; 170 size_t i; 171 for (i = 0; all_indexes_valid && i < argc; ++i) { 172 const int idx = 173 StringConvert::ToSInt32(args.GetArgumentAtIndex(i), INT32_MAX); 174 if (idx == INT32_MAX) 175 all_indexes_valid = false; 176 else 177 remove_indexes.push_back(idx); 178 } 179 180 if (all_indexes_valid) { 181 size_t num_remove_indexes = remove_indexes.size(); 182 if (num_remove_indexes) { 183 // Sort and then erase in reverse so indexes are always valid 184 std::sort(remove_indexes.begin(), remove_indexes.end()); 185 for (size_t j = num_remove_indexes - 1; j < num_remove_indexes; ++j) { 186 m_path_mappings.Remove(j, m_notify_changes); 187 } 188 } 189 NotifyValueChanged(); 190 } else { 191 error.SetErrorStringWithFormat( 192 "invalid array index '%s', aborting remove operation", 193 args.GetArgumentAtIndex(i)); 194 } 195 } else { 196 error.SetErrorString("remove operation takes one or more array index"); 197 } 198 break; 199 200 case eVarSetOperationInvalid: 201 error = OptionValue::SetValueFromString(value, op); 202 break; 203 } 204 return error; 205 } 206 207 lldb::OptionValueSP OptionValuePathMappings::DeepCopy() const { 208 return OptionValueSP(new OptionValuePathMappings(*this)); 209 } 210