Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandCompletions.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 | |
| 11 | // C Includes |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 12 | #include <sys/stat.h> |
| 13 | #include <dirent.h> |
Greg Clayton | 8da92a7 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 14 | #if defined(__APPLE__) || defined(__linux__) |
Jim Ingham | 84a0d33 | 2010-07-02 00:45:55 +0000 | [diff] [blame] | 15 | #include <pwd.h> |
Greg Clayton | 8da92a7 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 16 | #endif |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | // C++ Includes |
| 19 | // Other libraries and framework includes |
| 20 | // Project includes |
Greg Clayton | 5f54ac3 | 2011-02-08 05:05:52 +0000 | [diff] [blame] | 21 | #include "lldb/Host/FileSpec.h" |
Greg Clayton | 7480061 | 2011-02-01 05:15:22 +0000 | [diff] [blame] | 22 | #include "lldb/Core/FileSpecList.h" |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 23 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 7480061 | 2011-02-01 05:15:22 +0000 | [diff] [blame] | 24 | #include "lldb/Interpreter/Args.h" |
| 25 | #include "lldb/Interpreter/CommandCompletions.h" |
| 26 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 27 | #include "lldb/Target/Target.h" |
| 28 | #include "lldb/Utility/CleanUp.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace lldb_private; |
| 31 | |
| 32 | CommandCompletions::CommonCompletionElement |
| 33 | CommandCompletions::g_common_completions[] = |
| 34 | { |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 35 | {eCustomCompletion, NULL}, |
| 36 | {eSourceFileCompletion, CommandCompletions::SourceFiles}, |
| 37 | {eDiskFileCompletion, CommandCompletions::DiskFiles}, |
| 38 | {eDiskDirectoryCompletion, CommandCompletions::DiskDirectories}, |
| 39 | {eSymbolCompletion, CommandCompletions::Symbols}, |
| 40 | {eModuleCompletion, CommandCompletions::Modules}, |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 41 | {eSettingsNameCompletion, CommandCompletions::SettingsNames}, |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 42 | {ePlatformPluginCompletion, CommandCompletions::PlatformPluginNames}, |
| 43 | {eArchitectureCompletion, CommandCompletions::ArchitectureNames}, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 44 | {eNoCompletion, NULL} // This one has to be last in the list. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | bool |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 48 | CommandCompletions::InvokeCommonCompletionCallbacks |
| 49 | ( |
| 50 | CommandInterpreter &interpreter, |
| 51 | uint32_t completion_mask, |
| 52 | const char *completion_str, |
| 53 | int match_start_point, |
| 54 | int max_return_elements, |
| 55 | SearchFilter *searcher, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 56 | bool &word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 57 | StringList &matches |
| 58 | ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | { |
| 60 | bool handled = false; |
| 61 | |
| 62 | if (completion_mask & eCustomCompletion) |
| 63 | return false; |
| 64 | |
| 65 | for (int i = 0; ; i++) |
| 66 | { |
| 67 | if (g_common_completions[i].type == eNoCompletion) |
| 68 | break; |
| 69 | else if ((g_common_completions[i].type & completion_mask) == g_common_completions[i].type |
| 70 | && g_common_completions[i].callback != NULL) |
| 71 | { |
| 72 | handled = true; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 73 | g_common_completions[i].callback (interpreter, |
| 74 | completion_str, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | match_start_point, |
| 76 | max_return_elements, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | searcher, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 78 | word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | matches); |
| 80 | } |
| 81 | } |
| 82 | return handled; |
| 83 | } |
| 84 | |
| 85 | int |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 86 | CommandCompletions::SourceFiles |
| 87 | ( |
| 88 | CommandInterpreter &interpreter, |
| 89 | const char *partial_file_name, |
| 90 | int match_start_point, |
| 91 | int max_return_elements, |
| 92 | SearchFilter *searcher, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 93 | bool &word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 94 | StringList &matches |
| 95 | ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | { |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 97 | word_complete = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | // Find some way to switch "include support files..." |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 99 | SourceFileCompleter completer (interpreter, |
| 100 | false, |
| 101 | partial_file_name, |
| 102 | match_start_point, |
| 103 | max_return_elements, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | matches); |
| 105 | |
| 106 | if (searcher == NULL) |
| 107 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 108 | lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | SearchFilter null_searcher (target_sp); |
| 110 | completer.DoCompletion (&null_searcher); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | completer.DoCompletion (searcher); |
| 115 | } |
| 116 | return matches.GetSize(); |
| 117 | } |
| 118 | |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 119 | static int |
| 120 | DiskFilesOrDirectories |
| 121 | ( |
| 122 | const char *partial_file_name, |
| 123 | bool only_directories, |
| 124 | bool &saw_directory, |
| 125 | StringList &matches |
| 126 | ) |
| 127 | { |
| 128 | // I'm going to use the "glob" function with GLOB_TILDE for user directory expansion. |
| 129 | // If it is not defined on your host system, you'll need to implement it yourself... |
| 130 | |
| 131 | int partial_name_len = strlen(partial_file_name); |
| 132 | |
| 133 | if (partial_name_len >= PATH_MAX) |
| 134 | return matches.GetSize(); |
| 135 | |
| 136 | // This copy of the string will be cut up into the directory part, and the remainder. end_ptr |
| 137 | // below will point to the place of the remainder in this string. Then when we've resolved the |
| 138 | // containing directory, and opened it, we'll read the directory contents and overwrite the |
| 139 | // partial_name_copy starting from end_ptr with each of the matches. Thus we will preserve |
| 140 | // the form the user originally typed. |
| 141 | |
| 142 | char partial_name_copy[PATH_MAX]; |
Benjamin Kramer | bdbb9af | 2010-06-30 13:43:47 +0000 | [diff] [blame] | 143 | memcpy(partial_name_copy, partial_file_name, partial_name_len); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 144 | partial_name_copy[partial_name_len] = '\0'; |
| 145 | |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 146 | // We'll need to save a copy of the remainder for comparison, which we do here. |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 147 | char remainder[PATH_MAX]; |
| 148 | |
| 149 | // end_ptr will point past the last / in partial_name_copy, or if there is no slash to the beginning of the string. |
| 150 | char *end_ptr; |
| 151 | |
| 152 | end_ptr = strrchr(partial_name_copy, '/'); |
| 153 | |
| 154 | // This will store the resolved form of the containing directory |
| 155 | char containing_part[PATH_MAX]; |
| 156 | |
| 157 | if (end_ptr == NULL) |
| 158 | { |
| 159 | // There's no directory. If the thing begins with a "~" then this is a bare |
| 160 | // user name. |
| 161 | if (*partial_name_copy == '~') |
| 162 | { |
| 163 | // Nothing here but the user name. We could just put a slash on the end, |
Jim Ingham | 84a0d33 | 2010-07-02 00:45:55 +0000 | [diff] [blame] | 164 | // but for completeness sake we'll resolve the user name and only put a slash |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 165 | // on the end if it exists. |
Jim Ingham | 84a0d33 | 2010-07-02 00:45:55 +0000 | [diff] [blame] | 166 | char resolved_username[PATH_MAX]; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 167 | size_t resolved_username_len = FileSpec::ResolveUsername (partial_name_copy, resolved_username, |
Jim Ingham | 84a0d33 | 2010-07-02 00:45:55 +0000 | [diff] [blame] | 168 | sizeof (resolved_username)); |
| 169 | |
| 170 | // Not sure how this would happen, a username longer than PATH_MAX? Still... |
| 171 | if (resolved_username_len >= sizeof (resolved_username)) |
| 172 | return matches.GetSize(); |
Jim Ingham | 158842c | 2011-02-08 23:24:09 +0000 | [diff] [blame] | 173 | else if (resolved_username_len == 0) |
Jim Ingham | 84a0d33 | 2010-07-02 00:45:55 +0000 | [diff] [blame] | 174 | { |
| 175 | // The user name didn't resolve, let's look in the password database for matches. |
| 176 | // The user name database contains duplicates, and is not in alphabetical order, so |
| 177 | // we'll use a set to manage that for us. |
Jim Ingham | 158842c | 2011-02-08 23:24:09 +0000 | [diff] [blame] | 178 | FileSpec::ResolvePartialUsername (partial_name_copy, matches); |
| 179 | if (matches.GetSize() > 0) |
| 180 | saw_directory = true; |
Jim Ingham | 84a0d33 | 2010-07-02 00:45:55 +0000 | [diff] [blame] | 181 | return matches.GetSize(); |
Jim Ingham | 158842c | 2011-02-08 23:24:09 +0000 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | { |
| 185 | //The thing exists, put a '/' on the end, and return it... |
| 186 | // FIXME: complete user names here: |
| 187 | partial_name_copy[partial_name_len] = '/'; |
| 188 | partial_name_copy[partial_name_len+1] = '\0'; |
| 189 | matches.AppendString(partial_name_copy); |
| 190 | saw_directory = true; |
| 191 | return matches.GetSize(); |
| 192 | } |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 193 | } |
| 194 | else |
| 195 | { |
| 196 | // The containing part is the CWD, and the whole string is the remainder. |
| 197 | containing_part[0] = '.'; |
| 198 | containing_part[1] = '\0'; |
| 199 | strcpy(remainder, partial_name_copy); |
| 200 | end_ptr = partial_name_copy; |
| 201 | } |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | if (end_ptr == partial_name_copy) |
| 206 | { |
| 207 | // We're completing a file or directory in the root volume. |
| 208 | containing_part[0] = '/'; |
| 209 | containing_part[1] = '\0'; |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | size_t len = end_ptr - partial_name_copy; |
| 214 | memcpy(containing_part, partial_name_copy, len); |
| 215 | containing_part[len] = '\0'; |
| 216 | } |
| 217 | // Push end_ptr past the final "/" and set remainder. |
| 218 | end_ptr++; |
| 219 | strcpy(remainder, end_ptr); |
| 220 | } |
| 221 | |
Jim Ingham | 84a0d33 | 2010-07-02 00:45:55 +0000 | [diff] [blame] | 222 | // Look for a user name in the containing part, and if it's there, resolve it and stick the |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 223 | // result back into the containing_part: |
Greg Clayton | 8da92a7 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 224 | |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 225 | if (*partial_name_copy == '~') |
| 226 | { |
Jim Ingham | 158842c | 2011-02-08 23:24:09 +0000 | [diff] [blame] | 227 | size_t resolved_username_len = FileSpec::ResolveUsername(containing_part, |
| 228 | containing_part, |
| 229 | sizeof (containing_part)); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 230 | // User name doesn't exist, we're not getting any further... |
Greg Clayton | 099c797 | 2010-07-06 16:11:44 +0000 | [diff] [blame] | 231 | if (resolved_username_len == 0 || resolved_username_len >= sizeof (containing_part)) |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 232 | return matches.GetSize(); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 233 | } |
Greg Clayton | 8da92a7 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 234 | |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 235 | // Okay, containing_part is now the directory we want to open and look for files: |
Greg Clayton | 7480061 | 2011-02-01 05:15:22 +0000 | [diff] [blame] | 236 | |
| 237 | lldb_utility::CleanUp <DIR *, int> dir_stream (opendir(containing_part), NULL, closedir); |
| 238 | if (!dir_stream.is_valid()) |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 239 | return matches.GetSize(); |
Greg Clayton | 7480061 | 2011-02-01 05:15:22 +0000 | [diff] [blame] | 240 | |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 241 | struct dirent *dirent_buf; |
| 242 | |
| 243 | size_t baselen = end_ptr - partial_name_copy; |
| 244 | |
Greg Clayton | 7480061 | 2011-02-01 05:15:22 +0000 | [diff] [blame] | 245 | while ((dirent_buf = readdir(dir_stream.get())) != NULL) |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 246 | { |
| 247 | char *name = dirent_buf->d_name; |
| 248 | |
| 249 | // Omit ".", ".." and any . files if the match string doesn't start with . |
| 250 | if (name[0] == '.') |
| 251 | { |
| 252 | if (name[1] == '\0') |
| 253 | continue; |
| 254 | else if (name[1] == '.' && name[2] == '\0') |
| 255 | continue; |
| 256 | else if (remainder[0] != '.') |
| 257 | continue; |
| 258 | } |
| 259 | |
Jim Ingham | 84a0d33 | 2010-07-02 00:45:55 +0000 | [diff] [blame] | 260 | // If we found a directory, we put a "/" at the end of the name. |
| 261 | |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 262 | if (remainder[0] == '\0' || strstr(dirent_buf->d_name, remainder) == name) |
| 263 | { |
| 264 | if (strlen(name) + baselen >= PATH_MAX) |
| 265 | continue; |
| 266 | |
| 267 | strcpy(end_ptr, name); |
| 268 | |
| 269 | bool isa_directory = false; |
| 270 | if (dirent_buf->d_type & DT_DIR) |
| 271 | isa_directory = true; |
| 272 | else if (dirent_buf->d_type & DT_LNK) |
| 273 | { |
| 274 | struct stat stat_buf; |
Jason Molenda | f38b76b | 2011-07-08 00:38:03 +0000 | [diff] [blame] | 275 | if ((stat(partial_name_copy, &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode)) |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 276 | isa_directory = true; |
| 277 | } |
| 278 | |
| 279 | if (isa_directory) |
| 280 | { |
| 281 | saw_directory = true; |
| 282 | size_t len = strlen(partial_name_copy); |
| 283 | partial_name_copy[len] = '/'; |
| 284 | partial_name_copy[len + 1] = '\0'; |
| 285 | } |
| 286 | if (only_directories && !isa_directory) |
| 287 | continue; |
| 288 | matches.AppendString(partial_name_copy); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return matches.GetSize(); |
| 293 | } |
| 294 | |
| 295 | int |
| 296 | CommandCompletions::DiskFiles |
| 297 | ( |
| 298 | CommandInterpreter &interpreter, |
| 299 | const char *partial_file_name, |
| 300 | int match_start_point, |
| 301 | int max_return_elements, |
| 302 | SearchFilter *searcher, |
| 303 | bool &word_complete, |
| 304 | StringList &matches |
| 305 | ) |
| 306 | { |
| 307 | |
| 308 | int ret_val = DiskFilesOrDirectories (partial_file_name, |
| 309 | false, |
| 310 | word_complete, |
| 311 | matches); |
| 312 | word_complete = !word_complete; |
| 313 | return ret_val; |
| 314 | } |
| 315 | |
| 316 | int |
| 317 | CommandCompletions::DiskDirectories |
| 318 | ( |
| 319 | CommandInterpreter &interpreter, |
| 320 | const char *partial_file_name, |
| 321 | int match_start_point, |
| 322 | int max_return_elements, |
| 323 | SearchFilter *searcher, |
| 324 | bool &word_complete, |
| 325 | StringList &matches |
| 326 | ) |
| 327 | { |
| 328 | int ret_val = DiskFilesOrDirectories (partial_file_name, |
| 329 | true, |
| 330 | word_complete, |
| 331 | matches); |
| 332 | word_complete = false; |
| 333 | return ret_val; |
| 334 | } |
| 335 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 336 | int |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 337 | CommandCompletions::Modules |
| 338 | ( |
| 339 | CommandInterpreter &interpreter, |
| 340 | const char *partial_file_name, |
| 341 | int match_start_point, |
| 342 | int max_return_elements, |
| 343 | SearchFilter *searcher, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 344 | bool &word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 345 | StringList &matches |
| 346 | ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 347 | { |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 348 | word_complete = true; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 349 | ModuleCompleter completer (interpreter, |
| 350 | partial_file_name, |
| 351 | match_start_point, |
| 352 | max_return_elements, |
| 353 | matches); |
| 354 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 355 | if (searcher == NULL) |
| 356 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 357 | lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 358 | SearchFilter null_searcher (target_sp); |
| 359 | completer.DoCompletion (&null_searcher); |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | completer.DoCompletion (searcher); |
| 364 | } |
| 365 | return matches.GetSize(); |
| 366 | } |
| 367 | |
| 368 | int |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 369 | CommandCompletions::Symbols |
| 370 | ( |
| 371 | CommandInterpreter &interpreter, |
| 372 | const char *partial_file_name, |
| 373 | int match_start_point, |
| 374 | int max_return_elements, |
| 375 | SearchFilter *searcher, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 376 | bool &word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 377 | StringList &matches) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 378 | { |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 379 | word_complete = true; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 380 | SymbolCompleter completer (interpreter, |
| 381 | partial_file_name, |
| 382 | match_start_point, |
| 383 | max_return_elements, |
| 384 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 385 | |
| 386 | if (searcher == NULL) |
| 387 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 388 | lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 389 | SearchFilter null_searcher (target_sp); |
| 390 | completer.DoCompletion (&null_searcher); |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | completer.DoCompletion (searcher); |
| 395 | } |
| 396 | return matches.GetSize(); |
| 397 | } |
| 398 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 399 | int |
| 400 | CommandCompletions::SettingsNames (CommandInterpreter &interpreter, |
| 401 | const char *partial_setting_name, |
| 402 | int match_start_point, |
| 403 | int max_return_elements, |
| 404 | SearchFilter *searcher, |
| 405 | bool &word_complete, |
| 406 | StringList &matches) |
| 407 | { |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame^] | 408 | // Cache the full setting name list |
| 409 | static StringList g_property_names; |
| 410 | if (g_property_names.GetSize() == 0) |
| 411 | { |
| 412 | // Generate the full setting name list on demand |
| 413 | lldb::OptionValuePropertiesSP properties_sp (interpreter.GetDebugger().GetValueProperties()); |
| 414 | if (properties_sp) |
| 415 | { |
| 416 | StreamString strm; |
| 417 | properties_sp->DumpValue(NULL, strm, OptionValue::eDumpOptionName); |
| 418 | const std::string &str = strm.GetString(); |
| 419 | g_property_names.SplitIntoLines(str.c_str(), str.size()); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | size_t exact_matches_idx = SIZE_MAX; |
| 424 | const size_t num_matches = g_property_names.AutoComplete (partial_setting_name, matches, exact_matches_idx); |
| 425 | // return UserSettingsController::CompleteSettingsNames (root_settings, |
| 426 | // partial_setting_name_pieces, |
| 427 | // word_complete, |
| 428 | // matches); |
| 429 | // |
| 430 | word_complete = exact_matches_idx != SIZE_MAX; |
| 431 | return num_matches; |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 434 | |
| 435 | int |
| 436 | CommandCompletions::PlatformPluginNames (CommandInterpreter &interpreter, |
| 437 | const char *partial_name, |
| 438 | int match_start_point, |
| 439 | int max_return_elements, |
| 440 | SearchFilter *searcher, |
| 441 | bool &word_complete, |
| 442 | lldb_private::StringList &matches) |
| 443 | { |
| 444 | const uint32_t num_matches = PluginManager::AutoCompletePlatformName(partial_name, matches); |
| 445 | word_complete = num_matches == 1; |
| 446 | return num_matches; |
| 447 | } |
| 448 | |
| 449 | int |
| 450 | CommandCompletions::ArchitectureNames (CommandInterpreter &interpreter, |
| 451 | const char *partial_name, |
| 452 | int match_start_point, |
| 453 | int max_return_elements, |
| 454 | SearchFilter *searcher, |
| 455 | bool &word_complete, |
| 456 | lldb_private::StringList &matches) |
| 457 | { |
| 458 | const uint32_t num_matches = ArchSpec::AutoComplete (partial_name, matches); |
| 459 | word_complete = num_matches == 1; |
| 460 | return num_matches; |
| 461 | } |
| 462 | |
| 463 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 464 | CommandCompletions::Completer::Completer |
| 465 | ( |
| 466 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 467 | const char *completion_str, |
| 468 | int match_start_point, |
| 469 | int max_return_elements, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 470 | StringList &matches |
| 471 | ) : |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 472 | m_interpreter (interpreter), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 473 | m_completion_str (completion_str), |
| 474 | m_match_start_point (match_start_point), |
| 475 | m_max_return_elements (max_return_elements), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 476 | m_matches (matches) |
| 477 | { |
| 478 | } |
| 479 | |
| 480 | CommandCompletions::Completer::~Completer () |
| 481 | { |
| 482 | |
| 483 | } |
| 484 | |
| 485 | //---------------------------------------------------------------------- |
| 486 | // SourceFileCompleter |
| 487 | //---------------------------------------------------------------------- |
| 488 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 489 | CommandCompletions::SourceFileCompleter::SourceFileCompleter |
| 490 | ( |
| 491 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 492 | bool include_support_files, |
| 493 | const char *completion_str, |
| 494 | int match_start_point, |
| 495 | int max_return_elements, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 496 | StringList &matches |
| 497 | ) : |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 498 | CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 499 | m_include_support_files (include_support_files), |
| 500 | m_matching_files() |
| 501 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 502 | FileSpec partial_spec (m_completion_str.c_str(), false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 503 | m_file_name = partial_spec.GetFilename().GetCString(); |
| 504 | m_dir_name = partial_spec.GetDirectory().GetCString(); |
| 505 | } |
| 506 | |
| 507 | Searcher::Depth |
| 508 | CommandCompletions::SourceFileCompleter::GetDepth() |
| 509 | { |
| 510 | return eDepthCompUnit; |
| 511 | } |
| 512 | |
| 513 | Searcher::CallbackReturn |
| 514 | CommandCompletions::SourceFileCompleter::SearchCallback ( |
| 515 | SearchFilter &filter, |
| 516 | SymbolContext &context, |
| 517 | Address *addr, |
| 518 | bool complete |
| 519 | ) |
| 520 | { |
| 521 | if (context.comp_unit != NULL) |
| 522 | { |
| 523 | if (m_include_support_files) |
| 524 | { |
| 525 | FileSpecList supporting_files = context.comp_unit->GetSupportFiles(); |
| 526 | for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++) |
| 527 | { |
| 528 | const FileSpec &sfile_spec = supporting_files.GetFileSpecAtIndex(sfiles); |
| 529 | const char *sfile_file_name = sfile_spec.GetFilename().GetCString(); |
| 530 | const char *sfile_dir_name = sfile_spec.GetFilename().GetCString(); |
| 531 | bool match = false; |
| 532 | if (m_file_name && sfile_file_name |
| 533 | && strstr (sfile_file_name, m_file_name) == sfile_file_name) |
| 534 | match = true; |
| 535 | if (match && m_dir_name && sfile_dir_name |
| 536 | && strstr (sfile_dir_name, m_dir_name) != sfile_dir_name) |
| 537 | match = false; |
| 538 | |
| 539 | if (match) |
| 540 | { |
| 541 | m_matching_files.AppendIfUnique(sfile_spec); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | const char *cur_file_name = context.comp_unit->GetFilename().GetCString(); |
| 549 | const char *cur_dir_name = context.comp_unit->GetDirectory().GetCString(); |
| 550 | |
| 551 | bool match = false; |
| 552 | if (m_file_name && cur_file_name |
| 553 | && strstr (cur_file_name, m_file_name) == cur_file_name) |
| 554 | match = true; |
| 555 | |
| 556 | if (match && m_dir_name && cur_dir_name |
| 557 | && strstr (cur_dir_name, m_dir_name) != cur_dir_name) |
| 558 | match = false; |
| 559 | |
| 560 | if (match) |
| 561 | { |
| 562 | m_matching_files.AppendIfUnique(context.comp_unit); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | return Searcher::eCallbackReturnContinue; |
| 567 | } |
| 568 | |
| 569 | size_t |
| 570 | CommandCompletions::SourceFileCompleter::DoCompletion (SearchFilter *filter) |
| 571 | { |
| 572 | filter->Search (*this); |
| 573 | // Now convert the filelist to completions: |
| 574 | for (size_t i = 0; i < m_matching_files.GetSize(); i++) |
| 575 | { |
| 576 | m_matches.AppendString (m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString()); |
| 577 | } |
| 578 | return m_matches.GetSize(); |
| 579 | |
| 580 | } |
| 581 | |
| 582 | //---------------------------------------------------------------------- |
| 583 | // SymbolCompleter |
| 584 | //---------------------------------------------------------------------- |
| 585 | |
| 586 | static bool |
| 587 | regex_chars (const char comp) |
| 588 | { |
| 589 | if (comp == '[' || comp == ']' || comp == '(' || comp == ')') |
| 590 | return true; |
| 591 | else |
| 592 | return false; |
| 593 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 594 | CommandCompletions::SymbolCompleter::SymbolCompleter |
| 595 | ( |
| 596 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 597 | const char *completion_str, |
| 598 | int match_start_point, |
| 599 | int max_return_elements, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | StringList &matches |
| 601 | ) : |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 602 | CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | { |
| 604 | std::string regex_str ("^"); |
| 605 | regex_str.append(completion_str); |
| 606 | regex_str.append(".*"); |
| 607 | std::string::iterator pos; |
| 608 | |
| 609 | pos = find_if(regex_str.begin(), regex_str.end(), regex_chars); |
| 610 | while (pos < regex_str.end()) { |
| 611 | pos = regex_str.insert(pos, '\\'); |
| 612 | pos += 2; |
| 613 | pos = find_if(pos, regex_str.end(), regex_chars); |
| 614 | } |
| 615 | m_regex.Compile(regex_str.c_str()); |
| 616 | } |
| 617 | |
| 618 | Searcher::Depth |
| 619 | CommandCompletions::SymbolCompleter::GetDepth() |
| 620 | { |
| 621 | return eDepthModule; |
| 622 | } |
| 623 | |
| 624 | Searcher::CallbackReturn |
| 625 | CommandCompletions::SymbolCompleter::SearchCallback ( |
| 626 | SearchFilter &filter, |
| 627 | SymbolContext &context, |
| 628 | Address *addr, |
| 629 | bool complete |
| 630 | ) |
| 631 | { |
Greg Clayton | 28d5fcc | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 632 | if (context.module_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 633 | { |
Greg Clayton | 28d5fcc | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 634 | SymbolContextList sc_list; |
| 635 | const bool include_symbols = true; |
Sean Callanan | 302d78c | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 636 | const bool include_inlines = true; |
Greg Clayton | 28d5fcc | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 637 | const bool append = true; |
Sean Callanan | 302d78c | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 638 | context.module_sp->FindFunctions (m_regex, include_symbols, include_inlines, append, sc_list); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 639 | |
| 640 | SymbolContext sc; |
| 641 | // Now add the functions & symbols to the list - only add if unique: |
Greg Clayton | 28d5fcc | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 642 | for (uint32_t i = 0; i < sc_list.GetSize(); i++) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 643 | { |
Greg Clayton | 28d5fcc | 2011-01-27 06:44:37 +0000 | [diff] [blame] | 644 | if (sc_list.GetContextAtIndex(i, sc)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 645 | { |
Jim Ingham | a7d8951 | 2011-09-27 19:48:20 +0000 | [diff] [blame] | 646 | ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled); |
| 647 | if (!func_name.IsEmpty()) |
| 648 | m_match_set.insert (func_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | } |
| 652 | return Searcher::eCallbackReturnContinue; |
| 653 | } |
| 654 | |
| 655 | size_t |
| 656 | CommandCompletions::SymbolCompleter::DoCompletion (SearchFilter *filter) |
| 657 | { |
| 658 | filter->Search (*this); |
| 659 | collection::iterator pos = m_match_set.begin(), end = m_match_set.end(); |
| 660 | for (pos = m_match_set.begin(); pos != end; pos++) |
| 661 | m_matches.AppendString((*pos).GetCString()); |
| 662 | |
| 663 | return m_matches.GetSize(); |
| 664 | } |
| 665 | |
| 666 | //---------------------------------------------------------------------- |
| 667 | // ModuleCompleter |
| 668 | //---------------------------------------------------------------------- |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 669 | CommandCompletions::ModuleCompleter::ModuleCompleter |
| 670 | ( |
| 671 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 672 | const char *completion_str, |
| 673 | int match_start_point, |
| 674 | int max_return_elements, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 675 | StringList &matches |
| 676 | ) : |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 677 | CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 678 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 679 | FileSpec partial_spec (m_completion_str.c_str(), false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 680 | m_file_name = partial_spec.GetFilename().GetCString(); |
| 681 | m_dir_name = partial_spec.GetDirectory().GetCString(); |
| 682 | } |
| 683 | |
| 684 | Searcher::Depth |
| 685 | CommandCompletions::ModuleCompleter::GetDepth() |
| 686 | { |
| 687 | return eDepthModule; |
| 688 | } |
| 689 | |
| 690 | Searcher::CallbackReturn |
| 691 | CommandCompletions::ModuleCompleter::SearchCallback ( |
| 692 | SearchFilter &filter, |
| 693 | SymbolContext &context, |
| 694 | Address *addr, |
| 695 | bool complete |
| 696 | ) |
| 697 | { |
Greg Clayton | 6e0101c | 2011-09-17 06:21:20 +0000 | [diff] [blame] | 698 | if (context.module_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 699 | { |
| 700 | const char *cur_file_name = context.module_sp->GetFileSpec().GetFilename().GetCString(); |
| 701 | const char *cur_dir_name = context.module_sp->GetFileSpec().GetDirectory().GetCString(); |
| 702 | |
| 703 | bool match = false; |
| 704 | if (m_file_name && cur_file_name |
| 705 | && strstr (cur_file_name, m_file_name) == cur_file_name) |
| 706 | match = true; |
| 707 | |
| 708 | if (match && m_dir_name && cur_dir_name |
| 709 | && strstr (cur_dir_name, m_dir_name) != cur_dir_name) |
| 710 | match = false; |
| 711 | |
| 712 | if (match) |
| 713 | { |
| 714 | m_matches.AppendString (cur_file_name); |
| 715 | } |
| 716 | } |
| 717 | return Searcher::eCallbackReturnContinue; |
| 718 | } |
| 719 | |
| 720 | size_t |
| 721 | CommandCompletions::ModuleCompleter::DoCompletion (SearchFilter *filter) |
| 722 | { |
| 723 | filter->Search (*this); |
| 724 | return m_matches.GetSize(); |
| 725 | } |