1c9af18a5SGreg Clayton //===-- CommandCompletions.cpp ----------------------------------*- C++ -*-===//
2c9af18a5SGreg Clayton //
3c9af18a5SGreg Clayton //                     The LLVM Compiler Infrastructure
4c9af18a5SGreg Clayton //
5c9af18a5SGreg Clayton // This file is distributed under the University of Illinois Open Source
6c9af18a5SGreg Clayton // License. See LICENSE.TXT for details.
7c9af18a5SGreg Clayton //
8c9af18a5SGreg Clayton //===----------------------------------------------------------------------===//
9c9af18a5SGreg Clayton 
10c9af18a5SGreg Clayton 
11c9af18a5SGreg Clayton // C Includes
12558ce124SJim Ingham #include <sys/stat.h>
13558ce124SJim Ingham #include <dirent.h>
14fd18426eSGreg Clayton #if defined(__APPLE__) || defined(__linux__)
157cc478b9SJim Ingham #include <pwd.h>
16fd18426eSGreg Clayton #endif
17558ce124SJim Ingham 
18c9af18a5SGreg Clayton // C++ Includes
19c9af18a5SGreg Clayton // Other libraries and framework includes
20c9af18a5SGreg Clayton // Project includes
217cc478b9SJim Ingham #include "lldb/Core/FileSpec.h"
229b62fd2fSGreg Clayton #include "lldb/Core/FileSpecList.h"
239b62fd2fSGreg Clayton #include "lldb/Interpreter/Args.h"
249b62fd2fSGreg Clayton #include "lldb/Interpreter/CommandCompletions.h"
259b62fd2fSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
269b62fd2fSGreg Clayton #include "lldb/Target/Target.h"
279b62fd2fSGreg Clayton #include "lldb/Utility/CleanUp.h"
28c9af18a5SGreg Clayton 
29c9af18a5SGreg Clayton using namespace lldb_private;
30c9af18a5SGreg Clayton 
31c9af18a5SGreg Clayton CommandCompletions::CommonCompletionElement
32c9af18a5SGreg Clayton CommandCompletions::g_common_completions[] =
33c9af18a5SGreg Clayton {
34c9af18a5SGreg Clayton     {eCustomCompletion,          NULL},
35c9af18a5SGreg Clayton     {eSourceFileCompletion,      CommandCompletions::SourceFiles},
36558ce124SJim Ingham     {eDiskFileCompletion,        CommandCompletions::DiskFiles},
37558ce124SJim Ingham     {eDiskDirectoryCompletion,   CommandCompletions::DiskDirectories},
38c9af18a5SGreg Clayton     {eSymbolCompletion,          CommandCompletions::Symbols},
39c9af18a5SGreg Clayton     {eModuleCompletion,          CommandCompletions::Modules},
403df9a8dfSCaroline Tice     {eSettingsNameCompletion,    CommandCompletions::SettingsNames},
41c9af18a5SGreg Clayton     {eNoCompletion,              NULL}      // This one has to be last in the list.
42c9af18a5SGreg Clayton };
43c9af18a5SGreg Clayton 
44c9af18a5SGreg Clayton bool
456611103cSGreg Clayton CommandCompletions::InvokeCommonCompletionCallbacks
466611103cSGreg Clayton (
476611103cSGreg Clayton     CommandInterpreter &interpreter,
486611103cSGreg Clayton     uint32_t completion_mask,
49c9af18a5SGreg Clayton     const char *completion_str,
50c9af18a5SGreg Clayton     int match_start_point,
51c9af18a5SGreg Clayton     int max_return_elements,
52c9af18a5SGreg Clayton     SearchFilter *searcher,
53558ce124SJim Ingham     bool &word_complete,
546611103cSGreg Clayton     StringList &matches
556611103cSGreg Clayton )
56c9af18a5SGreg Clayton {
57c9af18a5SGreg Clayton     bool handled = false;
58c9af18a5SGreg Clayton 
59c9af18a5SGreg Clayton     if (completion_mask & eCustomCompletion)
60c9af18a5SGreg Clayton         return false;
61c9af18a5SGreg Clayton 
62c9af18a5SGreg Clayton     for (int i = 0; ; i++)
63c9af18a5SGreg Clayton     {
64c9af18a5SGreg Clayton         if (g_common_completions[i].type == eNoCompletion)
65c9af18a5SGreg Clayton             break;
66c9af18a5SGreg Clayton          else if ((g_common_completions[i].type & completion_mask) == g_common_completions[i].type
67c9af18a5SGreg Clayton                    && g_common_completions[i].callback != NULL)
68c9af18a5SGreg Clayton          {
69c9af18a5SGreg Clayton             handled = true;
706611103cSGreg Clayton             g_common_completions[i].callback (interpreter,
716611103cSGreg Clayton                                               completion_str,
72c9af18a5SGreg Clayton                                               match_start_point,
73c9af18a5SGreg Clayton                                               max_return_elements,
74c9af18a5SGreg Clayton                                               searcher,
75558ce124SJim Ingham                                               word_complete,
76c9af18a5SGreg Clayton                                               matches);
77c9af18a5SGreg Clayton         }
78c9af18a5SGreg Clayton     }
79c9af18a5SGreg Clayton     return handled;
80c9af18a5SGreg Clayton }
81c9af18a5SGreg Clayton 
82c9af18a5SGreg Clayton int
836611103cSGreg Clayton CommandCompletions::SourceFiles
846611103cSGreg Clayton (
856611103cSGreg Clayton     CommandInterpreter &interpreter,
866611103cSGreg Clayton     const char *partial_file_name,
87c9af18a5SGreg Clayton     int match_start_point,
88c9af18a5SGreg Clayton     int max_return_elements,
89c9af18a5SGreg Clayton     SearchFilter *searcher,
90558ce124SJim Ingham     bool &word_complete,
916611103cSGreg Clayton     StringList &matches
926611103cSGreg Clayton )
93c9af18a5SGreg Clayton {
94558ce124SJim Ingham     word_complete = true;
95c9af18a5SGreg Clayton     // Find some way to switch "include support files..."
966611103cSGreg Clayton     SourceFileCompleter completer (interpreter,
976611103cSGreg Clayton                                    false,
986611103cSGreg Clayton                                    partial_file_name,
996611103cSGreg Clayton                                    match_start_point,
1006611103cSGreg Clayton                                    max_return_elements,
101c9af18a5SGreg Clayton                                    matches);
102c9af18a5SGreg Clayton 
103c9af18a5SGreg Clayton     if (searcher == NULL)
104c9af18a5SGreg Clayton     {
1052976d00aSJim Ingham         lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
106c9af18a5SGreg Clayton         SearchFilter null_searcher (target_sp);
107c9af18a5SGreg Clayton         completer.DoCompletion (&null_searcher);
108c9af18a5SGreg Clayton     }
109c9af18a5SGreg Clayton     else
110c9af18a5SGreg Clayton     {
111c9af18a5SGreg Clayton         completer.DoCompletion (searcher);
112c9af18a5SGreg Clayton     }
113c9af18a5SGreg Clayton     return matches.GetSize();
114c9af18a5SGreg Clayton }
115c9af18a5SGreg Clayton 
116558ce124SJim Ingham static int
117558ce124SJim Ingham DiskFilesOrDirectories
118558ce124SJim Ingham (
119558ce124SJim Ingham     const char *partial_file_name,
120558ce124SJim Ingham     bool only_directories,
121558ce124SJim Ingham     bool &saw_directory,
122558ce124SJim Ingham     StringList &matches
123558ce124SJim Ingham )
124558ce124SJim Ingham {
125558ce124SJim Ingham     // I'm going to  use the "glob" function with GLOB_TILDE for user directory expansion.
126558ce124SJim Ingham     // If it is not defined on your host system, you'll need to implement it yourself...
127558ce124SJim Ingham 
128558ce124SJim Ingham     int partial_name_len = strlen(partial_file_name);
129558ce124SJim Ingham 
130558ce124SJim Ingham     if (partial_name_len >= PATH_MAX)
131558ce124SJim Ingham         return matches.GetSize();
132558ce124SJim Ingham 
133558ce124SJim Ingham     // This copy of the string will be cut up into the directory part, and the remainder.  end_ptr
134558ce124SJim Ingham     // below will point to the place of the remainder in this string.  Then when we've resolved the
135558ce124SJim Ingham     // containing directory, and opened it, we'll read the directory contents and overwrite the
136558ce124SJim Ingham     // partial_name_copy starting from end_ptr with each of the matches.  Thus we will preserve
137558ce124SJim Ingham     // the form the user originally typed.
138558ce124SJim Ingham 
139558ce124SJim Ingham     char partial_name_copy[PATH_MAX];
140021b605dSBenjamin Kramer     memcpy(partial_name_copy, partial_file_name, partial_name_len);
141558ce124SJim Ingham     partial_name_copy[partial_name_len] = '\0';
142558ce124SJim Ingham 
143710dd5aeSGreg Clayton     // We'll need to save a copy of the remainder for comparison, which we do here.
144558ce124SJim Ingham     char remainder[PATH_MAX];
145558ce124SJim Ingham 
146558ce124SJim Ingham     // end_ptr will point past the last / in partial_name_copy, or if there is no slash to the beginning of the string.
147558ce124SJim Ingham     char *end_ptr;
148558ce124SJim Ingham 
149558ce124SJim Ingham     end_ptr = strrchr(partial_name_copy, '/');
150558ce124SJim Ingham 
151558ce124SJim Ingham     // This will store the resolved form of the containing directory
152558ce124SJim Ingham     char containing_part[PATH_MAX];
153558ce124SJim Ingham 
154558ce124SJim Ingham     if (end_ptr == NULL)
155558ce124SJim Ingham     {
156*45319466SGreg Clayton #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
157fd18426eSGreg Clayton 
158558ce124SJim Ingham         // There's no directory.  If the thing begins with a "~" then this is a bare
159558ce124SJim Ingham         // user name.
160558ce124SJim Ingham         if (*partial_name_copy == '~')
161558ce124SJim Ingham         {
162558ce124SJim Ingham             // Nothing here but the user name.  We could just put a slash on the end,
1637cc478b9SJim Ingham             // but for completeness sake we'll resolve the user name and only put a slash
164558ce124SJim Ingham             // on the end if it exists.
1657cc478b9SJim Ingham             char resolved_username[PATH_MAX];
166c982c768SGreg Clayton             size_t resolved_username_len = FileSpec::ResolveUsername (partial_name_copy, resolved_username,
1677cc478b9SJim Ingham                                                           sizeof (resolved_username));
1687cc478b9SJim Ingham 
1697cc478b9SJim Ingham            // Not sure how this would happen, a username longer than PATH_MAX?  Still...
1707cc478b9SJim Ingham             if (resolved_username_len >= sizeof (resolved_username))
171558ce124SJim Ingham                 return matches.GetSize();
172fd18426eSGreg Clayton             if (resolved_username_len == 0)
1737cc478b9SJim Ingham             {
1747cc478b9SJim Ingham                 // The user name didn't resolve, let's look in the password database for matches.
1757cc478b9SJim Ingham                 // The user name database contains duplicates, and is not in alphabetical order, so
1767cc478b9SJim Ingham                 // we'll use a set to manage that for us.
1777cc478b9SJim Ingham 
1787cc478b9SJim Ingham                 setpwent();
1797cc478b9SJim Ingham                 struct passwd *user_entry;
1807cc478b9SJim Ingham                 const char *name_start = partial_name_copy + 1;
1817cc478b9SJim Ingham                 std::set<std::string> name_list;
1827cc478b9SJim Ingham 
1837cc478b9SJim Ingham                 while ((user_entry = getpwent()) != NULL)
1847cc478b9SJim Ingham                 {
1857cc478b9SJim Ingham                     if (strstr(user_entry->pw_name, name_start) == user_entry->pw_name)
1867cc478b9SJim Ingham                     {
1877cc478b9SJim Ingham                         std::string tmp_buf("~");
1887cc478b9SJim Ingham                         tmp_buf.append(user_entry->pw_name);
1897cc478b9SJim Ingham                         tmp_buf.push_back('/');
1907cc478b9SJim Ingham                         name_list.insert(tmp_buf);
1917cc478b9SJim Ingham                         saw_directory = true;
1927cc478b9SJim Ingham                     }
1937cc478b9SJim Ingham                 }
1947cc478b9SJim Ingham                 std::set<std::string>::iterator pos, end = name_list.end();
1957cc478b9SJim Ingham                 for (pos = name_list.begin(); pos != end; pos++)
1967cc478b9SJim Ingham                 {
1977cc478b9SJim Ingham                     matches.AppendString((*pos).c_str());
1987cc478b9SJim Ingham                 }
1997cc478b9SJim Ingham                 return matches.GetSize();
2007cc478b9SJim Ingham             }
201558ce124SJim Ingham             //The thing exists, put a '/' on the end, and return it...
202558ce124SJim Ingham             // FIXME: complete user names here:
203558ce124SJim Ingham             partial_name_copy[partial_name_len] = '/';
204558ce124SJim Ingham             partial_name_copy[partial_name_len+1] = '\0';
205558ce124SJim Ingham             matches.AppendString(partial_name_copy);
206021b605dSBenjamin Kramer             saw_directory = true;
207558ce124SJim Ingham             return matches.GetSize();
208558ce124SJim Ingham         }
209558ce124SJim Ingham         else
210*45319466SGreg Clayton #endif //  #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
211fd18426eSGreg Clayton 
212558ce124SJim Ingham         {
213558ce124SJim Ingham             // The containing part is the CWD, and the whole string is the remainder.
214558ce124SJim Ingham             containing_part[0] = '.';
215558ce124SJim Ingham             containing_part[1] = '\0';
216558ce124SJim Ingham             strcpy(remainder, partial_name_copy);
217558ce124SJim Ingham             end_ptr = partial_name_copy;
218558ce124SJim Ingham         }
219558ce124SJim Ingham     }
220558ce124SJim Ingham     else
221558ce124SJim Ingham     {
222558ce124SJim Ingham         if (end_ptr == partial_name_copy)
223558ce124SJim Ingham         {
224558ce124SJim Ingham             // We're completing a file or directory in the root volume.
225558ce124SJim Ingham             containing_part[0] = '/';
226558ce124SJim Ingham             containing_part[1] = '\0';
227558ce124SJim Ingham         }
228558ce124SJim Ingham         else
229558ce124SJim Ingham         {
230558ce124SJim Ingham             size_t len = end_ptr - partial_name_copy;
231558ce124SJim Ingham             memcpy(containing_part, partial_name_copy, len);
232558ce124SJim Ingham             containing_part[len] = '\0';
233558ce124SJim Ingham         }
234558ce124SJim Ingham         // Push end_ptr past the final "/" and set remainder.
235558ce124SJim Ingham         end_ptr++;
236558ce124SJim Ingham         strcpy(remainder, end_ptr);
237558ce124SJim Ingham     }
238558ce124SJim Ingham 
2397cc478b9SJim Ingham     // Look for a user name in the containing part, and if it's there, resolve it and stick the
240558ce124SJim Ingham     // result back into the containing_part:
241558ce124SJim Ingham 
242*45319466SGreg Clayton #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
243558ce124SJim Ingham     if (*partial_name_copy == '~')
244558ce124SJim Ingham     {
245c982c768SGreg Clayton         size_t resolved_username_len = FileSpec::ResolveUsername(containing_part, containing_part, sizeof (containing_part));
246558ce124SJim Ingham         // User name doesn't exist, we're not getting any further...
247a0807356SGreg Clayton         if (resolved_username_len == 0 || resolved_username_len >= sizeof (containing_part))
248558ce124SJim Ingham             return matches.GetSize();
249558ce124SJim Ingham     }
250*45319466SGreg Clayton #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
251558ce124SJim Ingham 
252558ce124SJim Ingham     // Okay, containing_part is now the directory we want to open and look for files:
253558ce124SJim Ingham 
2549b62fd2fSGreg Clayton     lldb_utility::CleanUp <DIR *, int> dir_stream (opendir(containing_part), NULL, closedir);
2559b62fd2fSGreg Clayton     if (!dir_stream.is_valid())
256558ce124SJim Ingham         return matches.GetSize();
257558ce124SJim Ingham 
258558ce124SJim Ingham     struct dirent *dirent_buf;
259558ce124SJim Ingham 
260558ce124SJim Ingham     size_t baselen = end_ptr - partial_name_copy;
261558ce124SJim Ingham 
2629b62fd2fSGreg Clayton     while ((dirent_buf = readdir(dir_stream.get())) != NULL)
263558ce124SJim Ingham     {
264558ce124SJim Ingham         char *name = dirent_buf->d_name;
265558ce124SJim Ingham 
266558ce124SJim Ingham         // Omit ".", ".." and any . files if the match string doesn't start with .
267558ce124SJim Ingham         if (name[0] == '.')
268558ce124SJim Ingham         {
269558ce124SJim Ingham             if (name[1] == '\0')
270558ce124SJim Ingham                 continue;
271558ce124SJim Ingham             else if (name[1] == '.' && name[2] == '\0')
272558ce124SJim Ingham                 continue;
273558ce124SJim Ingham             else if (remainder[0] != '.')
274558ce124SJim Ingham                 continue;
275558ce124SJim Ingham         }
276558ce124SJim Ingham 
2777cc478b9SJim Ingham         // If we found a directory, we put a "/" at the end of the name.
2787cc478b9SJim Ingham 
279558ce124SJim Ingham         if (remainder[0] == '\0' || strstr(dirent_buf->d_name, remainder) == name)
280558ce124SJim Ingham         {
281558ce124SJim Ingham             if (strlen(name) + baselen >= PATH_MAX)
282558ce124SJim Ingham                 continue;
283558ce124SJim Ingham 
284558ce124SJim Ingham             strcpy(end_ptr, name);
285558ce124SJim Ingham 
286558ce124SJim Ingham             bool isa_directory = false;
287558ce124SJim Ingham             if (dirent_buf->d_type & DT_DIR)
288558ce124SJim Ingham                 isa_directory = true;
289558ce124SJim Ingham             else if (dirent_buf->d_type & DT_LNK)
290558ce124SJim Ingham             {
291558ce124SJim Ingham                 struct stat stat_buf;
292558ce124SJim Ingham                 if ((stat(partial_name_copy, &stat_buf) == 0) && (stat_buf.st_mode & S_IFDIR))
293558ce124SJim Ingham                     isa_directory = true;
294558ce124SJim Ingham             }
295558ce124SJim Ingham 
296558ce124SJim Ingham             if (isa_directory)
297558ce124SJim Ingham             {
298558ce124SJim Ingham                 saw_directory = true;
299558ce124SJim Ingham                 size_t len = strlen(partial_name_copy);
300558ce124SJim Ingham                 partial_name_copy[len] = '/';
301558ce124SJim Ingham                 partial_name_copy[len + 1] = '\0';
302558ce124SJim Ingham             }
303558ce124SJim Ingham             if (only_directories && !isa_directory)
304558ce124SJim Ingham                 continue;
305558ce124SJim Ingham             matches.AppendString(partial_name_copy);
306558ce124SJim Ingham         }
307558ce124SJim Ingham     }
308558ce124SJim Ingham 
309558ce124SJim Ingham     return matches.GetSize();
310558ce124SJim Ingham }
311558ce124SJim Ingham 
312558ce124SJim Ingham int
313558ce124SJim Ingham CommandCompletions::DiskFiles
314558ce124SJim Ingham (
315558ce124SJim Ingham     CommandInterpreter &interpreter,
316558ce124SJim Ingham     const char *partial_file_name,
317558ce124SJim Ingham     int match_start_point,
318558ce124SJim Ingham     int max_return_elements,
319558ce124SJim Ingham     SearchFilter *searcher,
320558ce124SJim Ingham     bool &word_complete,
321558ce124SJim Ingham     StringList &matches
322558ce124SJim Ingham )
323558ce124SJim Ingham {
324558ce124SJim Ingham 
325558ce124SJim Ingham     int ret_val = DiskFilesOrDirectories (partial_file_name,
326558ce124SJim Ingham                                           false,
327558ce124SJim Ingham                                           word_complete,
328558ce124SJim Ingham                                           matches);
329558ce124SJim Ingham     word_complete = !word_complete;
330558ce124SJim Ingham     return ret_val;
331558ce124SJim Ingham }
332558ce124SJim Ingham 
333558ce124SJim Ingham int
334558ce124SJim Ingham CommandCompletions::DiskDirectories
335558ce124SJim Ingham (
336558ce124SJim Ingham     CommandInterpreter &interpreter,
337558ce124SJim Ingham     const char *partial_file_name,
338558ce124SJim Ingham     int match_start_point,
339558ce124SJim Ingham     int max_return_elements,
340558ce124SJim Ingham     SearchFilter *searcher,
341558ce124SJim Ingham     bool &word_complete,
342558ce124SJim Ingham     StringList &matches
343558ce124SJim Ingham )
344558ce124SJim Ingham {
345558ce124SJim Ingham     int ret_val =  DiskFilesOrDirectories (partial_file_name,
346558ce124SJim Ingham                                            true,
347558ce124SJim Ingham                                            word_complete,
348558ce124SJim Ingham                                            matches);
349558ce124SJim Ingham     word_complete = false;
350558ce124SJim Ingham     return ret_val;
351558ce124SJim Ingham }
352558ce124SJim Ingham 
353c9af18a5SGreg Clayton int
3546611103cSGreg Clayton CommandCompletions::Modules
3556611103cSGreg Clayton (
3566611103cSGreg Clayton     CommandInterpreter &interpreter,
3576611103cSGreg Clayton     const char *partial_file_name,
358c9af18a5SGreg Clayton     int match_start_point,
359c9af18a5SGreg Clayton     int max_return_elements,
360c9af18a5SGreg Clayton     SearchFilter *searcher,
361558ce124SJim Ingham     bool &word_complete,
3626611103cSGreg Clayton     StringList &matches
3636611103cSGreg Clayton )
364c9af18a5SGreg Clayton {
365558ce124SJim Ingham     word_complete = true;
3666611103cSGreg Clayton     ModuleCompleter completer (interpreter,
3676611103cSGreg Clayton                                partial_file_name,
3686611103cSGreg Clayton                                match_start_point,
3696611103cSGreg Clayton                                max_return_elements,
3706611103cSGreg Clayton                                matches);
371c9af18a5SGreg Clayton 
372c9af18a5SGreg Clayton     if (searcher == NULL)
373c9af18a5SGreg Clayton     {
3742976d00aSJim Ingham         lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
375c9af18a5SGreg Clayton         SearchFilter null_searcher (target_sp);
376c9af18a5SGreg Clayton         completer.DoCompletion (&null_searcher);
377c9af18a5SGreg Clayton     }
378c9af18a5SGreg Clayton     else
379c9af18a5SGreg Clayton     {
380c9af18a5SGreg Clayton         completer.DoCompletion (searcher);
381c9af18a5SGreg Clayton     }
382c9af18a5SGreg Clayton     return matches.GetSize();
383c9af18a5SGreg Clayton }
384c9af18a5SGreg Clayton 
385c9af18a5SGreg Clayton int
3866611103cSGreg Clayton CommandCompletions::Symbols
3876611103cSGreg Clayton (
3886611103cSGreg Clayton     CommandInterpreter &interpreter,
3896611103cSGreg Clayton     const char *partial_file_name,
390c9af18a5SGreg Clayton     int match_start_point,
391c9af18a5SGreg Clayton     int max_return_elements,
392c9af18a5SGreg Clayton     SearchFilter *searcher,
393558ce124SJim Ingham     bool &word_complete,
3946611103cSGreg Clayton     StringList &matches)
395c9af18a5SGreg Clayton {
396558ce124SJim Ingham     word_complete = true;
3976611103cSGreg Clayton     SymbolCompleter completer (interpreter,
3986611103cSGreg Clayton                                partial_file_name,
3996611103cSGreg Clayton                                match_start_point,
4006611103cSGreg Clayton                                max_return_elements,
4016611103cSGreg Clayton                                matches);
402c9af18a5SGreg Clayton 
403c9af18a5SGreg Clayton     if (searcher == NULL)
404c9af18a5SGreg Clayton     {
4052976d00aSJim Ingham         lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
406c9af18a5SGreg Clayton         SearchFilter null_searcher (target_sp);
407c9af18a5SGreg Clayton         completer.DoCompletion (&null_searcher);
408c9af18a5SGreg Clayton     }
409c9af18a5SGreg Clayton     else
410c9af18a5SGreg Clayton     {
411c9af18a5SGreg Clayton         completer.DoCompletion (searcher);
412c9af18a5SGreg Clayton     }
413c9af18a5SGreg Clayton     return matches.GetSize();
414c9af18a5SGreg Clayton }
415c9af18a5SGreg Clayton 
4163df9a8dfSCaroline Tice int
4173df9a8dfSCaroline Tice CommandCompletions::SettingsNames (CommandInterpreter &interpreter,
4183df9a8dfSCaroline Tice                                    const char *partial_setting_name,
4193df9a8dfSCaroline Tice                                    int match_start_point,
4203df9a8dfSCaroline Tice                                    int max_return_elements,
4213df9a8dfSCaroline Tice                                    SearchFilter *searcher,
4223df9a8dfSCaroline Tice                                    bool &word_complete,
4233df9a8dfSCaroline Tice                                    StringList &matches)
4243df9a8dfSCaroline Tice {
4253df9a8dfSCaroline Tice     lldb::UserSettingsControllerSP root_settings = Debugger::GetSettingsController();
4263df9a8dfSCaroline Tice     Args partial_setting_name_pieces = UserSettingsController::BreakNameIntoPieces (partial_setting_name);
4273df9a8dfSCaroline Tice 
4283df9a8dfSCaroline Tice     return UserSettingsController::CompleteSettingsNames (root_settings,
4293df9a8dfSCaroline Tice                                                           partial_setting_name_pieces,
4303df9a8dfSCaroline Tice                                                           word_complete,
4313df9a8dfSCaroline Tice                                                           matches);
4323df9a8dfSCaroline Tice 
4333df9a8dfSCaroline Tice     //return matches.GetSize();
4343df9a8dfSCaroline Tice }
4353df9a8dfSCaroline Tice 
4366611103cSGreg Clayton CommandCompletions::Completer::Completer
4376611103cSGreg Clayton (
4386611103cSGreg Clayton     CommandInterpreter &interpreter,
439c9af18a5SGreg Clayton     const char *completion_str,
440c9af18a5SGreg Clayton     int match_start_point,
441c9af18a5SGreg Clayton     int max_return_elements,
442c9af18a5SGreg Clayton     StringList &matches
443c9af18a5SGreg Clayton ) :
4446611103cSGreg Clayton     m_interpreter (interpreter),
445c9af18a5SGreg Clayton     m_completion_str (completion_str),
446c9af18a5SGreg Clayton     m_match_start_point (match_start_point),
447c9af18a5SGreg Clayton     m_max_return_elements (max_return_elements),
448c9af18a5SGreg Clayton     m_matches (matches)
449c9af18a5SGreg Clayton {
450c9af18a5SGreg Clayton }
451c9af18a5SGreg Clayton 
452c9af18a5SGreg Clayton CommandCompletions::Completer::~Completer ()
453c9af18a5SGreg Clayton {
454c9af18a5SGreg Clayton 
455c9af18a5SGreg Clayton }
456c9af18a5SGreg Clayton 
457c9af18a5SGreg Clayton //----------------------------------------------------------------------
458c9af18a5SGreg Clayton // SourceFileCompleter
459c9af18a5SGreg Clayton //----------------------------------------------------------------------
460c9af18a5SGreg Clayton 
4616611103cSGreg Clayton CommandCompletions::SourceFileCompleter::SourceFileCompleter
4626611103cSGreg Clayton (
4636611103cSGreg Clayton     CommandInterpreter &interpreter,
464c9af18a5SGreg Clayton     bool include_support_files,
465c9af18a5SGreg Clayton     const char *completion_str,
466c9af18a5SGreg Clayton     int match_start_point,
467c9af18a5SGreg Clayton     int max_return_elements,
468c9af18a5SGreg Clayton     StringList &matches
469c9af18a5SGreg Clayton ) :
4706611103cSGreg Clayton     CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches),
471c9af18a5SGreg Clayton     m_include_support_files (include_support_files),
472c9af18a5SGreg Clayton     m_matching_files()
473c9af18a5SGreg Clayton {
474274060b6SGreg Clayton     FileSpec partial_spec (m_completion_str.c_str(), false);
475c9af18a5SGreg Clayton     m_file_name = partial_spec.GetFilename().GetCString();
476c9af18a5SGreg Clayton     m_dir_name = partial_spec.GetDirectory().GetCString();
477c9af18a5SGreg Clayton }
478c9af18a5SGreg Clayton 
479c9af18a5SGreg Clayton Searcher::Depth
480c9af18a5SGreg Clayton CommandCompletions::SourceFileCompleter::GetDepth()
481c9af18a5SGreg Clayton {
482c9af18a5SGreg Clayton     return eDepthCompUnit;
483c9af18a5SGreg Clayton }
484c9af18a5SGreg Clayton 
485c9af18a5SGreg Clayton Searcher::CallbackReturn
486c9af18a5SGreg Clayton CommandCompletions::SourceFileCompleter::SearchCallback (
487c9af18a5SGreg Clayton     SearchFilter &filter,
488c9af18a5SGreg Clayton     SymbolContext &context,
489c9af18a5SGreg Clayton     Address *addr,
490c9af18a5SGreg Clayton     bool complete
491c9af18a5SGreg Clayton )
492c9af18a5SGreg Clayton {
493c9af18a5SGreg Clayton     if (context.comp_unit != NULL)
494c9af18a5SGreg Clayton     {
495c9af18a5SGreg Clayton         if (m_include_support_files)
496c9af18a5SGreg Clayton         {
497c9af18a5SGreg Clayton             FileSpecList supporting_files = context.comp_unit->GetSupportFiles();
498c9af18a5SGreg Clayton             for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++)
499c9af18a5SGreg Clayton             {
500c9af18a5SGreg Clayton                 const FileSpec &sfile_spec = supporting_files.GetFileSpecAtIndex(sfiles);
501c9af18a5SGreg Clayton                 const char *sfile_file_name = sfile_spec.GetFilename().GetCString();
502c9af18a5SGreg Clayton                 const char *sfile_dir_name = sfile_spec.GetFilename().GetCString();
503c9af18a5SGreg Clayton                 bool match = false;
504c9af18a5SGreg Clayton                 if (m_file_name && sfile_file_name
505c9af18a5SGreg Clayton                     && strstr (sfile_file_name, m_file_name) == sfile_file_name)
506c9af18a5SGreg Clayton                     match = true;
507c9af18a5SGreg Clayton                 if (match && m_dir_name && sfile_dir_name
508c9af18a5SGreg Clayton                     && strstr (sfile_dir_name, m_dir_name) != sfile_dir_name)
509c9af18a5SGreg Clayton                     match = false;
510c9af18a5SGreg Clayton 
511c9af18a5SGreg Clayton                 if (match)
512c9af18a5SGreg Clayton                 {
513c9af18a5SGreg Clayton                     m_matching_files.AppendIfUnique(sfile_spec);
514c9af18a5SGreg Clayton                 }
515c9af18a5SGreg Clayton             }
516c9af18a5SGreg Clayton 
517c9af18a5SGreg Clayton         }
518c9af18a5SGreg Clayton         else
519c9af18a5SGreg Clayton         {
520c9af18a5SGreg Clayton             const char *cur_file_name = context.comp_unit->GetFilename().GetCString();
521c9af18a5SGreg Clayton             const char *cur_dir_name = context.comp_unit->GetDirectory().GetCString();
522c9af18a5SGreg Clayton 
523c9af18a5SGreg Clayton             bool match = false;
524c9af18a5SGreg Clayton             if (m_file_name && cur_file_name
525c9af18a5SGreg Clayton                 && strstr (cur_file_name, m_file_name) == cur_file_name)
526c9af18a5SGreg Clayton                 match = true;
527c9af18a5SGreg Clayton 
528c9af18a5SGreg Clayton             if (match && m_dir_name && cur_dir_name
529c9af18a5SGreg Clayton                 && strstr (cur_dir_name, m_dir_name) != cur_dir_name)
530c9af18a5SGreg Clayton                 match = false;
531c9af18a5SGreg Clayton 
532c9af18a5SGreg Clayton             if (match)
533c9af18a5SGreg Clayton             {
534c9af18a5SGreg Clayton                 m_matching_files.AppendIfUnique(context.comp_unit);
535c9af18a5SGreg Clayton             }
536c9af18a5SGreg Clayton         }
537c9af18a5SGreg Clayton     }
538c9af18a5SGreg Clayton     return Searcher::eCallbackReturnContinue;
539c9af18a5SGreg Clayton }
540c9af18a5SGreg Clayton 
541c9af18a5SGreg Clayton size_t
542c9af18a5SGreg Clayton CommandCompletions::SourceFileCompleter::DoCompletion (SearchFilter *filter)
543c9af18a5SGreg Clayton {
544c9af18a5SGreg Clayton     filter->Search (*this);
545c9af18a5SGreg Clayton     // Now convert the filelist to completions:
546c9af18a5SGreg Clayton     for (size_t i = 0; i < m_matching_files.GetSize(); i++)
547c9af18a5SGreg Clayton     {
548c9af18a5SGreg Clayton         m_matches.AppendString (m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
549c9af18a5SGreg Clayton     }
550c9af18a5SGreg Clayton     return m_matches.GetSize();
551c9af18a5SGreg Clayton 
552c9af18a5SGreg Clayton }
553c9af18a5SGreg Clayton 
554c9af18a5SGreg Clayton //----------------------------------------------------------------------
555c9af18a5SGreg Clayton // SymbolCompleter
556c9af18a5SGreg Clayton //----------------------------------------------------------------------
557c9af18a5SGreg Clayton 
558c9af18a5SGreg Clayton static bool
559c9af18a5SGreg Clayton regex_chars (const char comp)
560c9af18a5SGreg Clayton {
561c9af18a5SGreg Clayton     if (comp == '[' || comp == ']' || comp == '(' || comp == ')')
562c9af18a5SGreg Clayton         return true;
563c9af18a5SGreg Clayton     else
564c9af18a5SGreg Clayton         return false;
565c9af18a5SGreg Clayton }
5666611103cSGreg Clayton CommandCompletions::SymbolCompleter::SymbolCompleter
5676611103cSGreg Clayton (
5686611103cSGreg Clayton     CommandInterpreter &interpreter,
569c9af18a5SGreg Clayton     const char *completion_str,
570c9af18a5SGreg Clayton     int match_start_point,
571c9af18a5SGreg Clayton     int max_return_elements,
572c9af18a5SGreg Clayton     StringList &matches
573c9af18a5SGreg Clayton ) :
5746611103cSGreg Clayton     CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches)
575c9af18a5SGreg Clayton {
576c9af18a5SGreg Clayton     std::string regex_str ("^");
577c9af18a5SGreg Clayton     regex_str.append(completion_str);
578c9af18a5SGreg Clayton     regex_str.append(".*");
579c9af18a5SGreg Clayton     std::string::iterator pos;
580c9af18a5SGreg Clayton 
581c9af18a5SGreg Clayton     pos = find_if(regex_str.begin(), regex_str.end(), regex_chars);
582c9af18a5SGreg Clayton     while (pos < regex_str.end()) {
583c9af18a5SGreg Clayton         pos = regex_str.insert(pos, '\\');
584c9af18a5SGreg Clayton         pos += 2;
585c9af18a5SGreg Clayton         pos = find_if(pos, regex_str.end(), regex_chars);
586c9af18a5SGreg Clayton     }
587c9af18a5SGreg Clayton     m_regex.Compile(regex_str.c_str());
588c9af18a5SGreg Clayton }
589c9af18a5SGreg Clayton 
590c9af18a5SGreg Clayton Searcher::Depth
591c9af18a5SGreg Clayton CommandCompletions::SymbolCompleter::GetDepth()
592c9af18a5SGreg Clayton {
593c9af18a5SGreg Clayton     return eDepthModule;
594c9af18a5SGreg Clayton }
595c9af18a5SGreg Clayton 
596c9af18a5SGreg Clayton Searcher::CallbackReturn
597c9af18a5SGreg Clayton CommandCompletions::SymbolCompleter::SearchCallback (
598c9af18a5SGreg Clayton     SearchFilter &filter,
599c9af18a5SGreg Clayton     SymbolContext &context,
600c9af18a5SGreg Clayton     Address *addr,
601c9af18a5SGreg Clayton     bool complete
602c9af18a5SGreg Clayton )
603c9af18a5SGreg Clayton {
604c9af18a5SGreg Clayton     if (context.module_sp)
605c9af18a5SGreg Clayton     {
606931180e6SGreg Clayton         SymbolContextList sc_list;
607931180e6SGreg Clayton         const bool include_symbols = true;
608931180e6SGreg Clayton         const bool append = true;
609931180e6SGreg Clayton         context.module_sp->FindFunctions (m_regex, include_symbols, append, sc_list);
610c9af18a5SGreg Clayton 
611c9af18a5SGreg Clayton         SymbolContext sc;
612c9af18a5SGreg Clayton         // Now add the functions & symbols to the list - only add if unique:
613931180e6SGreg Clayton         for (uint32_t i = 0; i < sc_list.GetSize(); i++)
614c9af18a5SGreg Clayton         {
615931180e6SGreg Clayton             if (sc_list.GetContextAtIndex(i, sc))
616c9af18a5SGreg Clayton             {
617c9af18a5SGreg Clayton                 if (sc.function)
618c9af18a5SGreg Clayton                 {
619c9af18a5SGreg Clayton                     m_match_set.insert (sc.function->GetMangled().GetDemangledName());
620c9af18a5SGreg Clayton                 }
621931180e6SGreg Clayton                 else if (sc.symbol && sc.symbol->GetAddressRangePtr())
622c9af18a5SGreg Clayton                 {
623c9af18a5SGreg Clayton                     m_match_set.insert (sc.symbol->GetMangled().GetDemangledName());
624c9af18a5SGreg Clayton                 }
625c9af18a5SGreg Clayton             }
626c9af18a5SGreg Clayton         }
627c9af18a5SGreg Clayton     }
628c9af18a5SGreg Clayton     return Searcher::eCallbackReturnContinue;
629c9af18a5SGreg Clayton }
630c9af18a5SGreg Clayton 
631c9af18a5SGreg Clayton size_t
632c9af18a5SGreg Clayton CommandCompletions::SymbolCompleter::DoCompletion (SearchFilter *filter)
633c9af18a5SGreg Clayton {
634c9af18a5SGreg Clayton     filter->Search (*this);
635c9af18a5SGreg Clayton     collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
636c9af18a5SGreg Clayton     for (pos = m_match_set.begin(); pos != end; pos++)
637c9af18a5SGreg Clayton         m_matches.AppendString((*pos).GetCString());
638c9af18a5SGreg Clayton 
639c9af18a5SGreg Clayton     return m_matches.GetSize();
640c9af18a5SGreg Clayton }
641c9af18a5SGreg Clayton 
642c9af18a5SGreg Clayton //----------------------------------------------------------------------
643c9af18a5SGreg Clayton // ModuleCompleter
644c9af18a5SGreg Clayton //----------------------------------------------------------------------
6456611103cSGreg Clayton CommandCompletions::ModuleCompleter::ModuleCompleter
6466611103cSGreg Clayton (
6476611103cSGreg Clayton     CommandInterpreter &interpreter,
648c9af18a5SGreg Clayton     const char *completion_str,
649c9af18a5SGreg Clayton     int match_start_point,
650c9af18a5SGreg Clayton     int max_return_elements,
651c9af18a5SGreg Clayton     StringList &matches
652c9af18a5SGreg Clayton ) :
6536611103cSGreg Clayton     CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches)
654c9af18a5SGreg Clayton {
655274060b6SGreg Clayton     FileSpec partial_spec (m_completion_str.c_str(), false);
656c9af18a5SGreg Clayton     m_file_name = partial_spec.GetFilename().GetCString();
657c9af18a5SGreg Clayton     m_dir_name = partial_spec.GetDirectory().GetCString();
658c9af18a5SGreg Clayton }
659c9af18a5SGreg Clayton 
660c9af18a5SGreg Clayton Searcher::Depth
661c9af18a5SGreg Clayton CommandCompletions::ModuleCompleter::GetDepth()
662c9af18a5SGreg Clayton {
663c9af18a5SGreg Clayton     return eDepthModule;
664c9af18a5SGreg Clayton }
665c9af18a5SGreg Clayton 
666c9af18a5SGreg Clayton Searcher::CallbackReturn
667c9af18a5SGreg Clayton CommandCompletions::ModuleCompleter::SearchCallback (
668c9af18a5SGreg Clayton     SearchFilter &filter,
669c9af18a5SGreg Clayton     SymbolContext &context,
670c9af18a5SGreg Clayton     Address *addr,
671c9af18a5SGreg Clayton     bool complete
672c9af18a5SGreg Clayton )
673c9af18a5SGreg Clayton {
674c9af18a5SGreg Clayton     if (context.module_sp != NULL)
675c9af18a5SGreg Clayton     {
676c9af18a5SGreg Clayton         const char *cur_file_name = context.module_sp->GetFileSpec().GetFilename().GetCString();
677c9af18a5SGreg Clayton         const char *cur_dir_name = context.module_sp->GetFileSpec().GetDirectory().GetCString();
678c9af18a5SGreg Clayton 
679c9af18a5SGreg Clayton         bool match = false;
680c9af18a5SGreg Clayton         if (m_file_name && cur_file_name
681c9af18a5SGreg Clayton             && strstr (cur_file_name, m_file_name) == cur_file_name)
682c9af18a5SGreg Clayton             match = true;
683c9af18a5SGreg Clayton 
684c9af18a5SGreg Clayton         if (match && m_dir_name && cur_dir_name
685c9af18a5SGreg Clayton             && strstr (cur_dir_name, m_dir_name) != cur_dir_name)
686c9af18a5SGreg Clayton             match = false;
687c9af18a5SGreg Clayton 
688c9af18a5SGreg Clayton         if (match)
689c9af18a5SGreg Clayton         {
690c9af18a5SGreg Clayton             m_matches.AppendString (cur_file_name);
691c9af18a5SGreg Clayton         }
692c9af18a5SGreg Clayton     }
693c9af18a5SGreg Clayton     return Searcher::eCallbackReturnContinue;
694c9af18a5SGreg Clayton }
695c9af18a5SGreg Clayton 
696c9af18a5SGreg Clayton size_t
697c9af18a5SGreg Clayton CommandCompletions::ModuleCompleter::DoCompletion (SearchFilter *filter)
698c9af18a5SGreg Clayton {
699c9af18a5SGreg Clayton     filter->Search (*this);
700c9af18a5SGreg Clayton     return m_matches.GetSize();
701c9af18a5SGreg Clayton }
702