blob: d44d668850a0b7392e2787ffb59916a2fc40b7e2 [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>
14#include <libgen.h>
15#include <glob.h>
Jim Ingham84a0d332010-07-02 00:45:55 +000016#include <pwd.h>
17#include <sys/types.h>
Jim Ingham802f8b02010-06-30 05:02:46 +000018
Chris Lattner24943d22010-06-08 16:52:24 +000019// C++ Includes
20// Other libraries and framework includes
21// Project includes
Jim Ingham84a0d332010-07-02 00:45:55 +000022#include "lldb/Core/FileSpec.h"
Greg Clayton74800612011-02-01 05:15:22 +000023#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 Lattner24943d22010-06-08 16:52:24 +000029
30using namespace lldb_private;
31
32CommandCompletions::CommonCompletionElement
33CommandCompletions::g_common_completions[] =
34{
Jim Ingham802f8b02010-06-30 05:02:46 +000035 {eCustomCompletion, NULL},
36 {eSourceFileCompletion, CommandCompletions::SourceFiles},
37 {eDiskFileCompletion, CommandCompletions::DiskFiles},
38 {eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
39 {eSymbolCompletion, CommandCompletions::Symbols},
40 {eModuleCompletion, CommandCompletions::Modules},
Caroline Tice6e4c5ce2010-09-04 00:03:46 +000041 {eSettingsNameCompletion, CommandCompletions::SettingsNames},
Jim Ingham802f8b02010-06-30 05:02:46 +000042 {eNoCompletion, NULL} // This one has to be last in the list.
Chris Lattner24943d22010-06-08 16:52:24 +000043};
44
45bool
Greg Clayton63094e02010-06-23 01:19:29 +000046CommandCompletions::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 Ingham802f8b02010-06-30 05:02:46 +000054 bool &word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +000055 StringList &matches
56)
Chris Lattner24943d22010-06-08 16:52:24 +000057{
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 Clayton63094e02010-06-23 01:19:29 +000071 g_common_completions[i].callback (interpreter,
72 completion_str,
Chris Lattner24943d22010-06-08 16:52:24 +000073 match_start_point,
74 max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +000075 searcher,
Jim Ingham802f8b02010-06-30 05:02:46 +000076 word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +000077 matches);
78 }
79 }
80 return handled;
81}
82
83int
Greg Clayton63094e02010-06-23 01:19:29 +000084CommandCompletions::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 Ingham802f8b02010-06-30 05:02:46 +000091 bool &word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +000092 StringList &matches
93)
Chris Lattner24943d22010-06-08 16:52:24 +000094{
Jim Ingham802f8b02010-06-30 05:02:46 +000095 word_complete = true;
Chris Lattner24943d22010-06-08 16:52:24 +000096 // Find some way to switch "include support files..."
Greg Clayton63094e02010-06-23 01:19:29 +000097 SourceFileCompleter completer (interpreter,
98 false,
99 partial_file_name,
100 match_start_point,
101 max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000102 matches);
103
104 if (searcher == NULL)
105 {
Jim Inghamc8332952010-08-26 21:32:51 +0000106 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
Chris Lattner24943d22010-06-08 16:52:24 +0000107 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 Ingham802f8b02010-06-30 05:02:46 +0000117static int
118DiskFilesOrDirectories
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 Kramerbdbb9af2010-06-30 13:43:47 +0000141 memcpy(partial_name_copy, partial_file_name, partial_name_len);
Jim Ingham802f8b02010-06-30 05:02:46 +0000142 partial_name_copy[partial_name_len] = '\0';
143
Greg Clayton5d187e52011-01-08 20:28:42 +0000144 // We'll need to save a copy of the remainder for comparison, which we do here.
Jim Ingham802f8b02010-06-30 05:02:46 +0000145 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 Ingham84a0d332010-07-02 00:45:55 +0000162 // but for completeness sake we'll resolve the user name and only put a slash
Jim Ingham802f8b02010-06-30 05:02:46 +0000163 // on the end if it exists.
Jim Ingham84a0d332010-07-02 00:45:55 +0000164 char resolved_username[PATH_MAX];
Greg Clayton54e7afa2010-07-09 20:39:50 +0000165 size_t resolved_username_len = FileSpec::ResolveUsername (partial_name_copy, resolved_username,
Jim Ingham84a0d332010-07-02 00:45:55 +0000166 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 Ingham802f8b02010-06-30 05:02:46 +0000208 }
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 Ingham84a0d332010-07-02 00:45:55 +0000237 // 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 +0000238 // result back into the containing_part:
239
240 if (*partial_name_copy == '~')
241 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000242 size_t resolved_username_len = FileSpec::ResolveUsername(containing_part, containing_part, sizeof (containing_part));
Jim Ingham802f8b02010-06-30 05:02:46 +0000243 // User name doesn't exist, we're not getting any further...
Greg Clayton099c7972010-07-06 16:11:44 +0000244 if (resolved_username_len == 0 || resolved_username_len >= sizeof (containing_part))
Jim Ingham802f8b02010-06-30 05:02:46 +0000245 return matches.GetSize();
Jim Ingham802f8b02010-06-30 05:02:46 +0000246 }
247
248 // Okay, containing_part is now the directory we want to open and look for files:
Greg Clayton74800612011-02-01 05:15:22 +0000249
250 lldb_utility::CleanUp <DIR *, int> dir_stream (opendir(containing_part), NULL, closedir);
251 if (!dir_stream.is_valid())
Jim Ingham802f8b02010-06-30 05:02:46 +0000252 return matches.GetSize();
Greg Clayton74800612011-02-01 05:15:22 +0000253
Jim Ingham802f8b02010-06-30 05:02:46 +0000254 struct dirent *dirent_buf;
255
256 size_t baselen = end_ptr - partial_name_copy;
257
Greg Clayton74800612011-02-01 05:15:22 +0000258 while ((dirent_buf = readdir(dir_stream.get())) != NULL)
Jim Ingham802f8b02010-06-30 05:02:46 +0000259 {
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 Ingham84a0d332010-07-02 00:45:55 +0000273 // If we found a directory, we put a "/" at the end of the name.
274
Jim Ingham802f8b02010-06-30 05:02:46 +0000275 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
308int
309CommandCompletions::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
329int
330CommandCompletions::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 Lattner24943d22010-06-08 16:52:24 +0000349int
Greg Clayton63094e02010-06-23 01:19:29 +0000350CommandCompletions::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 Ingham802f8b02010-06-30 05:02:46 +0000357 bool &word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000358 StringList &matches
359)
Chris Lattner24943d22010-06-08 16:52:24 +0000360{
Jim Ingham802f8b02010-06-30 05:02:46 +0000361 word_complete = true;
Greg Clayton63094e02010-06-23 01:19:29 +0000362 ModuleCompleter completer (interpreter,
363 partial_file_name,
364 match_start_point,
365 max_return_elements,
366 matches);
367
Chris Lattner24943d22010-06-08 16:52:24 +0000368 if (searcher == NULL)
369 {
Jim Inghamc8332952010-08-26 21:32:51 +0000370 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
Chris Lattner24943d22010-06-08 16:52:24 +0000371 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
381int
Greg Clayton63094e02010-06-23 01:19:29 +0000382CommandCompletions::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 Ingham802f8b02010-06-30 05:02:46 +0000389 bool &word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000390 StringList &matches)
Chris Lattner24943d22010-06-08 16:52:24 +0000391{
Jim Ingham802f8b02010-06-30 05:02:46 +0000392 word_complete = true;
Greg Clayton63094e02010-06-23 01:19:29 +0000393 SymbolCompleter completer (interpreter,
394 partial_file_name,
395 match_start_point,
396 max_return_elements,
397 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000398
399 if (searcher == NULL)
400 {
Jim Inghamc8332952010-08-26 21:32:51 +0000401 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
Chris Lattner24943d22010-06-08 16:52:24 +0000402 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 Tice6e4c5ce2010-09-04 00:03:46 +0000412int
413CommandCompletions::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 Clayton63094e02010-06-23 01:19:29 +0000432CommandCompletions::Completer::Completer
433(
434 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000435 const char *completion_str,
436 int match_start_point,
437 int max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000438 StringList &matches
439) :
Greg Clayton63094e02010-06-23 01:19:29 +0000440 m_interpreter (interpreter),
Chris Lattner24943d22010-06-08 16:52:24 +0000441 m_completion_str (completion_str),
442 m_match_start_point (match_start_point),
443 m_max_return_elements (max_return_elements),
Chris Lattner24943d22010-06-08 16:52:24 +0000444 m_matches (matches)
445{
446}
447
448CommandCompletions::Completer::~Completer ()
449{
450
451}
452
453//----------------------------------------------------------------------
454// SourceFileCompleter
455//----------------------------------------------------------------------
456
Greg Clayton63094e02010-06-23 01:19:29 +0000457CommandCompletions::SourceFileCompleter::SourceFileCompleter
458(
459 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000460 bool include_support_files,
461 const char *completion_str,
462 int match_start_point,
463 int max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000464 StringList &matches
465) :
Greg Clayton63094e02010-06-23 01:19:29 +0000466 CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches),
Chris Lattner24943d22010-06-08 16:52:24 +0000467 m_include_support_files (include_support_files),
468 m_matching_files()
469{
Greg Clayton537a7a82010-10-20 20:54:39 +0000470 FileSpec partial_spec (m_completion_str.c_str(), false);
Chris Lattner24943d22010-06-08 16:52:24 +0000471 m_file_name = partial_spec.GetFilename().GetCString();
472 m_dir_name = partial_spec.GetDirectory().GetCString();
473}
474
475Searcher::Depth
476CommandCompletions::SourceFileCompleter::GetDepth()
477{
478 return eDepthCompUnit;
479}
480
481Searcher::CallbackReturn
482CommandCompletions::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
537size_t
538CommandCompletions::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
554static bool
555regex_chars (const char comp)
556{
557 if (comp == '[' || comp == ']' || comp == '(' || comp == ')')
558 return true;
559 else
560 return false;
561}
Greg Clayton63094e02010-06-23 01:19:29 +0000562CommandCompletions::SymbolCompleter::SymbolCompleter
563(
564 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000565 const char *completion_str,
566 int match_start_point,
567 int max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000568 StringList &matches
569) :
Greg Clayton63094e02010-06-23 01:19:29 +0000570 CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches)
Chris Lattner24943d22010-06-08 16:52:24 +0000571{
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
586Searcher::Depth
587CommandCompletions::SymbolCompleter::GetDepth()
588{
589 return eDepthModule;
590}
591
592Searcher::CallbackReturn
593CommandCompletions::SymbolCompleter::SearchCallback (
594 SearchFilter &filter,
595 SymbolContext &context,
596 Address *addr,
597 bool complete
598)
599{
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000600 if (context.module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000601 {
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000602 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 Lattner24943d22010-06-08 16:52:24 +0000606
607 SymbolContext sc;
608 // Now add the functions & symbols to the list - only add if unique:
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000609 for (uint32_t i = 0; i < sc_list.GetSize(); i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000610 {
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000611 if (sc_list.GetContextAtIndex(i, sc))
Chris Lattner24943d22010-06-08 16:52:24 +0000612 {
613 if (sc.function)
614 {
615 m_match_set.insert (sc.function->GetMangled().GetDemangledName());
616 }
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000617 else if (sc.symbol && sc.symbol->GetAddressRangePtr())
Chris Lattner24943d22010-06-08 16:52:24 +0000618 {
619 m_match_set.insert (sc.symbol->GetMangled().GetDemangledName());
620 }
621 }
622 }
623 }
624 return Searcher::eCallbackReturnContinue;
625}
626
627size_t
628CommandCompletions::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 Clayton63094e02010-06-23 01:19:29 +0000641CommandCompletions::ModuleCompleter::ModuleCompleter
642(
643 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000644 const char *completion_str,
645 int match_start_point,
646 int max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000647 StringList &matches
648) :
Greg Clayton63094e02010-06-23 01:19:29 +0000649 CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches)
Chris Lattner24943d22010-06-08 16:52:24 +0000650{
Greg Clayton537a7a82010-10-20 20:54:39 +0000651 FileSpec partial_spec (m_completion_str.c_str(), false);
Chris Lattner24943d22010-06-08 16:52:24 +0000652 m_file_name = partial_spec.GetFilename().GetCString();
653 m_dir_name = partial_spec.GetDirectory().GetCString();
654}
655
656Searcher::Depth
657CommandCompletions::ModuleCompleter::GetDepth()
658{
659 return eDepthModule;
660}
661
662Searcher::CallbackReturn
663CommandCompletions::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
692size_t
693CommandCompletions::ModuleCompleter::DoCompletion (SearchFilter *filter)
694{
695 filter->Search (*this);
696 return m_matches.GetSize();
697}