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