blob: 675bd53f275b1384192e73079ea17a46cea3ab3c [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Ingham802f8b02010-06-30 05:02:46 +000012#include <sys/stat.h>
13#include <dirent.h>
Greg Clayton8da92a72011-02-05 02:27:52 +000014#if defined(__APPLE__) || defined(__linux__)
Jim Ingham84a0d332010-07-02 00:45:55 +000015#include <pwd.h>
Greg Clayton8da92a72011-02-05 02:27:52 +000016#endif
Jim Ingham802f8b02010-06-30 05:02:46 +000017
Chris Lattner24943d22010-06-08 16:52:24 +000018// C++ Includes
19// Other libraries and framework includes
20// Project includes
Jim Ingham84a0d332010-07-02 00:45:55 +000021#include "lldb/Core/FileSpec.h"
Greg Clayton74800612011-02-01 05:15:22 +000022#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 Lattner24943d22010-06-08 16:52:24 +000028
29using namespace lldb_private;
30
31CommandCompletions::CommonCompletionElement
32CommandCompletions::g_common_completions[] =
33{
Jim Ingham802f8b02010-06-30 05:02:46 +000034 {eCustomCompletion, NULL},
35 {eSourceFileCompletion, CommandCompletions::SourceFiles},
36 {eDiskFileCompletion, CommandCompletions::DiskFiles},
37 {eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
38 {eSymbolCompletion, CommandCompletions::Symbols},
39 {eModuleCompletion, CommandCompletions::Modules},
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000040 {eSettingsNameCompletion, CommandCompletions::SettingsNames},
Jim Ingham802f8b02010-06-30 05:02:46 +000041 {eNoCompletion, NULL} // This one has to be last in the list.
Chris Lattner24943d22010-06-08 16:52:24 +000042};
43
44bool
Greg Clayton63094e02010-06-23 01:19:29 +000045CommandCompletions::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 Ingham802f8b02010-06-30 05:02:46 +000053 bool &word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +000054 StringList &matches
55)
Chris Lattner24943d22010-06-08 16:52:24 +000056{
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 Clayton63094e02010-06-23 01:19:29 +000070 g_common_completions[i].callback (interpreter,
71 completion_str,
Chris Lattner24943d22010-06-08 16:52:24 +000072 match_start_point,
73 max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +000074 searcher,
Jim Ingham802f8b02010-06-30 05:02:46 +000075 word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +000076 matches);
77 }
78 }
79 return handled;
80}
81
82int
Greg Clayton63094e02010-06-23 01:19:29 +000083CommandCompletions::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 Ingham802f8b02010-06-30 05:02:46 +000090 bool &word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +000091 StringList &matches
92)
Chris Lattner24943d22010-06-08 16:52:24 +000093{
Jim Ingham802f8b02010-06-30 05:02:46 +000094 word_complete = true;
Chris Lattner24943d22010-06-08 16:52:24 +000095 // Find some way to switch "include support files..."
Greg Clayton63094e02010-06-23 01:19:29 +000096 SourceFileCompleter completer (interpreter,
97 false,
98 partial_file_name,
99 match_start_point,
100 max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000101 matches);
102
103 if (searcher == NULL)
104 {
Jim Inghamc8332952010-08-26 21:32:51 +0000105 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
Chris Lattner24943d22010-06-08 16:52:24 +0000106 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 Ingham802f8b02010-06-30 05:02:46 +0000116static int
117DiskFilesOrDirectories
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 Kramerbdbb9af2010-06-30 13:43:47 +0000140 memcpy(partial_name_copy, partial_file_name, partial_name_len);
Jim Ingham802f8b02010-06-30 05:02:46 +0000141 partial_name_copy[partial_name_len] = '\0';
142
Greg Clayton5d187e52011-01-08 20:28:42 +0000143 // We'll need to save a copy of the remainder for comparison, which we do here.
Jim Ingham802f8b02010-06-30 05:02:46 +0000144 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 Clayton8da92a72011-02-05 02:27:52 +0000156#if LLDB_CONFIG_TILDE_RESOLVES_TO_USER
157
Jim Ingham802f8b02010-06-30 05:02:46 +0000158 // 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 Ingham84a0d332010-07-02 00:45:55 +0000163 // but for completeness sake we'll resolve the user name and only put a slash
Jim Ingham802f8b02010-06-30 05:02:46 +0000164 // on the end if it exists.
Jim Ingham84a0d332010-07-02 00:45:55 +0000165 char resolved_username[PATH_MAX];
Greg Clayton54e7afa2010-07-09 20:39:50 +0000166 size_t resolved_username_len = FileSpec::ResolveUsername (partial_name_copy, resolved_username,
Jim Ingham84a0d332010-07-02 00:45:55 +0000167 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 Clayton8da92a72011-02-05 02:27:52 +0000172 if (resolved_username_len == 0)
Jim Ingham84a0d332010-07-02 00:45:55 +0000173 {
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 Ingham84a0d332010-07-02 00:45:55 +0000201 //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 Ingham802f8b02010-06-30 05:02:46 +0000208 }
209 else
Greg Clayton8da92a72011-02-05 02:27:52 +0000210#endif // LLDB_CONFIG_TILDE_RESOLVES_TO_USER
211
Jim Ingham802f8b02010-06-30 05:02:46 +0000212 {
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 Ingham84a0d332010-07-02 00:45:55 +0000239 // Look for a user name in the containing part, and if it's there, resolve it and stick the
Jim Ingham802f8b02010-06-30 05:02:46 +0000240 // result back into the containing_part:
Greg Clayton8da92a72011-02-05 02:27:52 +0000241
242#if LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Jim Ingham802f8b02010-06-30 05:02:46 +0000243 if (*partial_name_copy == '~')
244 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000245 size_t resolved_username_len = FileSpec::ResolveUsername(containing_part, containing_part, sizeof (containing_part));
Jim Ingham802f8b02010-06-30 05:02:46 +0000246 // User name doesn't exist, we're not getting any further...
Greg Clayton099c7972010-07-06 16:11:44 +0000247 if (resolved_username_len == 0 || resolved_username_len >= sizeof (containing_part))
Jim Ingham802f8b02010-06-30 05:02:46 +0000248 return matches.GetSize();
Jim Ingham802f8b02010-06-30 05:02:46 +0000249 }
Greg Clayton8da92a72011-02-05 02:27:52 +0000250#endif // #if LLDB_CONFIG_TILDE_RESOLVES_TO_USER
251
Jim Ingham802f8b02010-06-30 05:02:46 +0000252 // Okay, containing_part is now the directory we want to open and look for files:
Greg Clayton74800612011-02-01 05:15:22 +0000253
254 lldb_utility::CleanUp <DIR *, int> dir_stream (opendir(containing_part), NULL, closedir);
255 if (!dir_stream.is_valid())
Jim Ingham802f8b02010-06-30 05:02:46 +0000256 return matches.GetSize();
Greg Clayton74800612011-02-01 05:15:22 +0000257
Jim Ingham802f8b02010-06-30 05:02:46 +0000258 struct dirent *dirent_buf;
259
260 size_t baselen = end_ptr - partial_name_copy;
261
Greg Clayton74800612011-02-01 05:15:22 +0000262 while ((dirent_buf = readdir(dir_stream.get())) != NULL)
Jim Ingham802f8b02010-06-30 05:02:46 +0000263 {
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 Ingham84a0d332010-07-02 00:45:55 +0000277 // If we found a directory, we put a "/" at the end of the name.
278
Jim Ingham802f8b02010-06-30 05:02:46 +0000279 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
312int
313CommandCompletions::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
333int
334CommandCompletions::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 Lattner24943d22010-06-08 16:52:24 +0000353int
Greg Clayton63094e02010-06-23 01:19:29 +0000354CommandCompletions::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 Ingham802f8b02010-06-30 05:02:46 +0000361 bool &word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000362 StringList &matches
363)
Chris Lattner24943d22010-06-08 16:52:24 +0000364{
Jim Ingham802f8b02010-06-30 05:02:46 +0000365 word_complete = true;
Greg Clayton63094e02010-06-23 01:19:29 +0000366 ModuleCompleter completer (interpreter,
367 partial_file_name,
368 match_start_point,
369 max_return_elements,
370 matches);
371
Chris Lattner24943d22010-06-08 16:52:24 +0000372 if (searcher == NULL)
373 {
Jim Inghamc8332952010-08-26 21:32:51 +0000374 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
Chris Lattner24943d22010-06-08 16:52:24 +0000375 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
385int
Greg Clayton63094e02010-06-23 01:19:29 +0000386CommandCompletions::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 Ingham802f8b02010-06-30 05:02:46 +0000393 bool &word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000394 StringList &matches)
Chris Lattner24943d22010-06-08 16:52:24 +0000395{
Jim Ingham802f8b02010-06-30 05:02:46 +0000396 word_complete = true;
Greg Clayton63094e02010-06-23 01:19:29 +0000397 SymbolCompleter completer (interpreter,
398 partial_file_name,
399 match_start_point,
400 max_return_elements,
401 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000402
403 if (searcher == NULL)
404 {
Jim Inghamc8332952010-08-26 21:32:51 +0000405 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
Chris Lattner24943d22010-06-08 16:52:24 +0000406 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 Tice6e4c5ce2010-09-04 00:03:46 +0000416int
417CommandCompletions::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 Clayton63094e02010-06-23 01:19:29 +0000436CommandCompletions::Completer::Completer
437(
438 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000439 const char *completion_str,
440 int match_start_point,
441 int max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000442 StringList &matches
443) :
Greg Clayton63094e02010-06-23 01:19:29 +0000444 m_interpreter (interpreter),
Chris Lattner24943d22010-06-08 16:52:24 +0000445 m_completion_str (completion_str),
446 m_match_start_point (match_start_point),
447 m_max_return_elements (max_return_elements),
Chris Lattner24943d22010-06-08 16:52:24 +0000448 m_matches (matches)
449{
450}
451
452CommandCompletions::Completer::~Completer ()
453{
454
455}
456
457//----------------------------------------------------------------------
458// SourceFileCompleter
459//----------------------------------------------------------------------
460
Greg Clayton63094e02010-06-23 01:19:29 +0000461CommandCompletions::SourceFileCompleter::SourceFileCompleter
462(
463 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000464 bool include_support_files,
465 const char *completion_str,
466 int match_start_point,
467 int max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000468 StringList &matches
469) :
Greg Clayton63094e02010-06-23 01:19:29 +0000470 CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches),
Chris Lattner24943d22010-06-08 16:52:24 +0000471 m_include_support_files (include_support_files),
472 m_matching_files()
473{
Greg Clayton537a7a82010-10-20 20:54:39 +0000474 FileSpec partial_spec (m_completion_str.c_str(), false);
Chris Lattner24943d22010-06-08 16:52:24 +0000475 m_file_name = partial_spec.GetFilename().GetCString();
476 m_dir_name = partial_spec.GetDirectory().GetCString();
477}
478
479Searcher::Depth
480CommandCompletions::SourceFileCompleter::GetDepth()
481{
482 return eDepthCompUnit;
483}
484
485Searcher::CallbackReturn
486CommandCompletions::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
541size_t
542CommandCompletions::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
558static bool
559regex_chars (const char comp)
560{
561 if (comp == '[' || comp == ']' || comp == '(' || comp == ')')
562 return true;
563 else
564 return false;
565}
Greg Clayton63094e02010-06-23 01:19:29 +0000566CommandCompletions::SymbolCompleter::SymbolCompleter
567(
568 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000569 const char *completion_str,
570 int match_start_point,
571 int max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000572 StringList &matches
573) :
Greg Clayton63094e02010-06-23 01:19:29 +0000574 CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches)
Chris Lattner24943d22010-06-08 16:52:24 +0000575{
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
590Searcher::Depth
591CommandCompletions::SymbolCompleter::GetDepth()
592{
593 return eDepthModule;
594}
595
596Searcher::CallbackReturn
597CommandCompletions::SymbolCompleter::SearchCallback (
598 SearchFilter &filter,
599 SymbolContext &context,
600 Address *addr,
601 bool complete
602)
603{
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000604 if (context.module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000605 {
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000606 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 Lattner24943d22010-06-08 16:52:24 +0000610
611 SymbolContext sc;
612 // Now add the functions & symbols to the list - only add if unique:
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000613 for (uint32_t i = 0; i < sc_list.GetSize(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000614 {
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000615 if (sc_list.GetContextAtIndex(i, sc))
Chris Lattner24943d22010-06-08 16:52:24 +0000616 {
617 if (sc.function)
618 {
619 m_match_set.insert (sc.function->GetMangled().GetDemangledName());
620 }
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000621 else if (sc.symbol && sc.symbol->GetAddressRangePtr())
Chris Lattner24943d22010-06-08 16:52:24 +0000622 {
623 m_match_set.insert (sc.symbol->GetMangled().GetDemangledName());
624 }
625 }
626 }
627 }
628 return Searcher::eCallbackReturnContinue;
629}
630
631size_t
632CommandCompletions::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 Clayton63094e02010-06-23 01:19:29 +0000645CommandCompletions::ModuleCompleter::ModuleCompleter
646(
647 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000648 const char *completion_str,
649 int match_start_point,
650 int max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000651 StringList &matches
652) :
Greg Clayton63094e02010-06-23 01:19:29 +0000653 CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches)
Chris Lattner24943d22010-06-08 16:52:24 +0000654{
Greg Clayton537a7a82010-10-20 20:54:39 +0000655 FileSpec partial_spec (m_completion_str.c_str(), false);
Chris Lattner24943d22010-06-08 16:52:24 +0000656 m_file_name = partial_spec.GetFilename().GetCString();
657 m_dir_name = partial_spec.GetDirectory().GetCString();
658}
659
660Searcher::Depth
661CommandCompletions::ModuleCompleter::GetDepth()
662{
663 return eDepthModule;
664}
665
666Searcher::CallbackReturn
667CommandCompletions::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
696size_t
697CommandCompletions::ModuleCompleter::DoCompletion (SearchFilter *filter)
698{
699 filter->Search (*this);
700 return m_matches.GetSize();
701}