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