1*b725ae77Skettenis /* Line completion stuff for GDB, the GNU debugger.
2*b725ae77Skettenis Copyright 2000, 2001 Free Software Foundation, Inc.
3*b725ae77Skettenis
4*b725ae77Skettenis This file is part of GDB.
5*b725ae77Skettenis
6*b725ae77Skettenis This program is free software; you can redistribute it and/or modify
7*b725ae77Skettenis it under the terms of the GNU General Public License as published by
8*b725ae77Skettenis the Free Software Foundation; either version 2 of the License, or
9*b725ae77Skettenis (at your option) any later version.
10*b725ae77Skettenis
11*b725ae77Skettenis This program is distributed in the hope that it will be useful,
12*b725ae77Skettenis but WITHOUT ANY WARRANTY; without even the implied warranty of
13*b725ae77Skettenis MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*b725ae77Skettenis GNU General Public License for more details.
15*b725ae77Skettenis
16*b725ae77Skettenis You should have received a copy of the GNU General Public License
17*b725ae77Skettenis along with this program; if not, write to the Free Software
18*b725ae77Skettenis Foundation, Inc., 59 Temple Place - Suite 330,
19*b725ae77Skettenis Boston, MA 02111-1307, USA. */
20*b725ae77Skettenis
21*b725ae77Skettenis #include "defs.h"
22*b725ae77Skettenis #include "symtab.h"
23*b725ae77Skettenis #include "gdbtypes.h"
24*b725ae77Skettenis #include "expression.h"
25*b725ae77Skettenis #include "filenames.h" /* for DOSish file names */
26*b725ae77Skettenis #include "language.h"
27*b725ae77Skettenis
28*b725ae77Skettenis #include "cli/cli-decode.h"
29*b725ae77Skettenis
30*b725ae77Skettenis /* FIXME: This is needed because of lookup_cmd_1().
31*b725ae77Skettenis We should be calling a hook instead so we eliminate the CLI dependency. */
32*b725ae77Skettenis #include "gdbcmd.h"
33*b725ae77Skettenis
34*b725ae77Skettenis /* Needed for rl_completer_word_break_characters() and for
35*b725ae77Skettenis rl_filename_completion_function. */
36*b725ae77Skettenis #include "readline/readline.h"
37*b725ae77Skettenis
38*b725ae77Skettenis /* readline defines this. */
39*b725ae77Skettenis #undef savestring
40*b725ae77Skettenis
41*b725ae77Skettenis #include "completer.h"
42*b725ae77Skettenis
43*b725ae77Skettenis /* Prototypes for local functions */
44*b725ae77Skettenis static
45*b725ae77Skettenis char *line_completion_function (const char *text, int matches, char *line_buffer,
46*b725ae77Skettenis int point);
47*b725ae77Skettenis
48*b725ae77Skettenis /* readline uses the word breaks for two things:
49*b725ae77Skettenis (1) In figuring out where to point the TEXT parameter to the
50*b725ae77Skettenis rl_completion_entry_function. Since we don't use TEXT for much,
51*b725ae77Skettenis it doesn't matter a lot what the word breaks are for this purpose, but
52*b725ae77Skettenis it does affect how much stuff M-? lists.
53*b725ae77Skettenis (2) If one of the matches contains a word break character, readline
54*b725ae77Skettenis will quote it. That's why we switch between
55*b725ae77Skettenis current_language->la_word_break_characters() and
56*b725ae77Skettenis gdb_completer_command_word_break_characters. I'm not sure when
57*b725ae77Skettenis we need this behavior (perhaps for funky characters in C++ symbols?). */
58*b725ae77Skettenis
59*b725ae77Skettenis /* Variables which are necessary for fancy command line editing. */
60*b725ae77Skettenis
61*b725ae77Skettenis /* When completing on command names, we remove '-' from the list of
62*b725ae77Skettenis word break characters, since we use it in command names. If the
63*b725ae77Skettenis readline library sees one in any of the current completion strings,
64*b725ae77Skettenis it thinks that the string needs to be quoted and automatically supplies
65*b725ae77Skettenis a leading quote. */
66*b725ae77Skettenis static char *gdb_completer_command_word_break_characters =
67*b725ae77Skettenis " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
68*b725ae77Skettenis
69*b725ae77Skettenis /* When completing on file names, we remove from the list of word
70*b725ae77Skettenis break characters any characters that are commonly used in file
71*b725ae77Skettenis names, such as '-', '+', '~', etc. Otherwise, readline displays
72*b725ae77Skettenis incorrect completion candidates. */
73*b725ae77Skettenis #ifdef HAVE_DOS_BASED_FILE_SYSTEM
74*b725ae77Skettenis /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
75*b725ae77Skettenis programs support @foo style response files. */
76*b725ae77Skettenis static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
77*b725ae77Skettenis #else
78*b725ae77Skettenis static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
79*b725ae77Skettenis #endif
80*b725ae77Skettenis
81*b725ae77Skettenis /* These are used when completing on locations, which can mix file
82*b725ae77Skettenis names and symbol names separated by a colon. */
83*b725ae77Skettenis static char *gdb_completer_loc_break_characters = " \t\n*|\"';:?><,";
84*b725ae77Skettenis
85*b725ae77Skettenis /* Characters that can be used to quote completion strings. Note that we
86*b725ae77Skettenis can't include '"' because the gdb C parser treats such quoted sequences
87*b725ae77Skettenis as strings. */
88*b725ae77Skettenis static char *gdb_completer_quote_characters = "'";
89*b725ae77Skettenis
90*b725ae77Skettenis /* Accessor for some completer data that may interest other files. */
91*b725ae77Skettenis
92*b725ae77Skettenis char *
get_gdb_completer_quote_characters(void)93*b725ae77Skettenis get_gdb_completer_quote_characters (void)
94*b725ae77Skettenis {
95*b725ae77Skettenis return gdb_completer_quote_characters;
96*b725ae77Skettenis }
97*b725ae77Skettenis
98*b725ae77Skettenis /* Line completion interface function for readline. */
99*b725ae77Skettenis
100*b725ae77Skettenis char *
readline_line_completion_function(const char * text,int matches)101*b725ae77Skettenis readline_line_completion_function (const char *text, int matches)
102*b725ae77Skettenis {
103*b725ae77Skettenis return line_completion_function (text, matches, rl_line_buffer, rl_point);
104*b725ae77Skettenis }
105*b725ae77Skettenis
106*b725ae77Skettenis /* This can be used for functions which don't want to complete on symbols
107*b725ae77Skettenis but don't want to complete on anything else either. */
108*b725ae77Skettenis char **
noop_completer(char * text,char * prefix)109*b725ae77Skettenis noop_completer (char *text, char *prefix)
110*b725ae77Skettenis {
111*b725ae77Skettenis return NULL;
112*b725ae77Skettenis }
113*b725ae77Skettenis
114*b725ae77Skettenis /* Complete on filenames. */
115*b725ae77Skettenis char **
filename_completer(char * text,char * word)116*b725ae77Skettenis filename_completer (char *text, char *word)
117*b725ae77Skettenis {
118*b725ae77Skettenis int subsequent_name;
119*b725ae77Skettenis char **return_val;
120*b725ae77Skettenis int return_val_used;
121*b725ae77Skettenis int return_val_alloced;
122*b725ae77Skettenis
123*b725ae77Skettenis return_val_used = 0;
124*b725ae77Skettenis /* Small for testing. */
125*b725ae77Skettenis return_val_alloced = 1;
126*b725ae77Skettenis return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
127*b725ae77Skettenis
128*b725ae77Skettenis subsequent_name = 0;
129*b725ae77Skettenis while (1)
130*b725ae77Skettenis {
131*b725ae77Skettenis char *p;
132*b725ae77Skettenis p = rl_filename_completion_function (text, subsequent_name);
133*b725ae77Skettenis if (return_val_used >= return_val_alloced)
134*b725ae77Skettenis {
135*b725ae77Skettenis return_val_alloced *= 2;
136*b725ae77Skettenis return_val =
137*b725ae77Skettenis (char **) xrealloc (return_val,
138*b725ae77Skettenis return_val_alloced * sizeof (char *));
139*b725ae77Skettenis }
140*b725ae77Skettenis if (p == NULL)
141*b725ae77Skettenis {
142*b725ae77Skettenis return_val[return_val_used++] = p;
143*b725ae77Skettenis break;
144*b725ae77Skettenis }
145*b725ae77Skettenis /* We need to set subsequent_name to a non-zero value before the
146*b725ae77Skettenis continue line below, because otherwise, if the first file seen
147*b725ae77Skettenis by GDB is a backup file whose name ends in a `~', we will loop
148*b725ae77Skettenis indefinitely. */
149*b725ae77Skettenis subsequent_name = 1;
150*b725ae77Skettenis /* Like emacs, don't complete on old versions. Especially useful
151*b725ae77Skettenis in the "source" command. */
152*b725ae77Skettenis if (p[strlen (p) - 1] == '~')
153*b725ae77Skettenis continue;
154*b725ae77Skettenis
155*b725ae77Skettenis {
156*b725ae77Skettenis char *q;
157*b725ae77Skettenis if (word == text)
158*b725ae77Skettenis /* Return exactly p. */
159*b725ae77Skettenis return_val[return_val_used++] = p;
160*b725ae77Skettenis else if (word > text)
161*b725ae77Skettenis {
162*b725ae77Skettenis /* Return some portion of p. */
163*b725ae77Skettenis q = xmalloc (strlen (p) + 5);
164*b725ae77Skettenis strcpy (q, p + (word - text));
165*b725ae77Skettenis return_val[return_val_used++] = q;
166*b725ae77Skettenis xfree (p);
167*b725ae77Skettenis }
168*b725ae77Skettenis else
169*b725ae77Skettenis {
170*b725ae77Skettenis /* Return some of TEXT plus p. */
171*b725ae77Skettenis q = xmalloc (strlen (p) + (text - word) + 5);
172*b725ae77Skettenis strncpy (q, word, text - word);
173*b725ae77Skettenis q[text - word] = '\0';
174*b725ae77Skettenis strcat (q, p);
175*b725ae77Skettenis return_val[return_val_used++] = q;
176*b725ae77Skettenis xfree (p);
177*b725ae77Skettenis }
178*b725ae77Skettenis }
179*b725ae77Skettenis }
180*b725ae77Skettenis #if 0
181*b725ae77Skettenis /* There is no way to do this just long enough to affect quote inserting
182*b725ae77Skettenis without also affecting the next completion. This should be fixed in
183*b725ae77Skettenis readline. FIXME. */
184*b725ae77Skettenis /* Insure that readline does the right thing
185*b725ae77Skettenis with respect to inserting quotes. */
186*b725ae77Skettenis rl_completer_word_break_characters = "";
187*b725ae77Skettenis #endif
188*b725ae77Skettenis return return_val;
189*b725ae77Skettenis }
190*b725ae77Skettenis
191*b725ae77Skettenis /* Complete on locations, which might be of two possible forms:
192*b725ae77Skettenis
193*b725ae77Skettenis file:line
194*b725ae77Skettenis or
195*b725ae77Skettenis symbol+offset
196*b725ae77Skettenis
197*b725ae77Skettenis This is intended to be used in commands that set breakpoints etc. */
198*b725ae77Skettenis char **
location_completer(char * text,char * word)199*b725ae77Skettenis location_completer (char *text, char *word)
200*b725ae77Skettenis {
201*b725ae77Skettenis int n_syms = 0, n_files = 0;
202*b725ae77Skettenis char ** fn_list = NULL;
203*b725ae77Skettenis char ** list = NULL;
204*b725ae77Skettenis char *p;
205*b725ae77Skettenis int quote_found = 0;
206*b725ae77Skettenis int quoted = *text == '\'' || *text == '"';
207*b725ae77Skettenis int quote_char = '\0';
208*b725ae77Skettenis char *colon = NULL;
209*b725ae77Skettenis char *file_to_match = NULL;
210*b725ae77Skettenis char *symbol_start = text;
211*b725ae77Skettenis char *orig_text = text;
212*b725ae77Skettenis size_t text_len;
213*b725ae77Skettenis
214*b725ae77Skettenis /* Do we have an unquoted colon, as in "break foo.c::bar"? */
215*b725ae77Skettenis for (p = text; *p != '\0'; ++p)
216*b725ae77Skettenis {
217*b725ae77Skettenis if (*p == '\\' && p[1] == '\'')
218*b725ae77Skettenis p++;
219*b725ae77Skettenis else if (*p == '\'' || *p == '"')
220*b725ae77Skettenis {
221*b725ae77Skettenis quote_found = *p;
222*b725ae77Skettenis quote_char = *p++;
223*b725ae77Skettenis while (*p != '\0' && *p != quote_found)
224*b725ae77Skettenis {
225*b725ae77Skettenis if (*p == '\\' && p[1] == quote_found)
226*b725ae77Skettenis p++;
227*b725ae77Skettenis p++;
228*b725ae77Skettenis }
229*b725ae77Skettenis
230*b725ae77Skettenis if (*p == quote_found)
231*b725ae77Skettenis quote_found = 0;
232*b725ae77Skettenis else
233*b725ae77Skettenis break; /* hit the end of text */
234*b725ae77Skettenis }
235*b725ae77Skettenis #if HAVE_DOS_BASED_FILE_SYSTEM
236*b725ae77Skettenis /* If we have a DOS-style absolute file name at the beginning of
237*b725ae77Skettenis TEXT, and the colon after the drive letter is the only colon
238*b725ae77Skettenis we found, pretend the colon is not there. */
239*b725ae77Skettenis else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
240*b725ae77Skettenis ;
241*b725ae77Skettenis #endif
242*b725ae77Skettenis else if (*p == ':' && !colon)
243*b725ae77Skettenis {
244*b725ae77Skettenis colon = p;
245*b725ae77Skettenis symbol_start = p + 1;
246*b725ae77Skettenis }
247*b725ae77Skettenis else if (strchr (current_language->la_word_break_characters(), *p))
248*b725ae77Skettenis symbol_start = p + 1;
249*b725ae77Skettenis }
250*b725ae77Skettenis
251*b725ae77Skettenis if (quoted)
252*b725ae77Skettenis text++;
253*b725ae77Skettenis text_len = strlen (text);
254*b725ae77Skettenis
255*b725ae77Skettenis /* Where is the file name? */
256*b725ae77Skettenis if (colon)
257*b725ae77Skettenis {
258*b725ae77Skettenis char *s;
259*b725ae77Skettenis
260*b725ae77Skettenis file_to_match = (char *) xmalloc (colon - text + 1);
261*b725ae77Skettenis strncpy (file_to_match, text, colon - text + 1);
262*b725ae77Skettenis /* Remove trailing colons and quotes from the file name. */
263*b725ae77Skettenis for (s = file_to_match + (colon - text);
264*b725ae77Skettenis s > file_to_match;
265*b725ae77Skettenis s--)
266*b725ae77Skettenis if (*s == ':' || *s == quote_char)
267*b725ae77Skettenis *s = '\0';
268*b725ae77Skettenis }
269*b725ae77Skettenis /* If the text includes a colon, they want completion only on a
270*b725ae77Skettenis symbol name after the colon. Otherwise, we need to complete on
271*b725ae77Skettenis symbols as well as on files. */
272*b725ae77Skettenis if (colon)
273*b725ae77Skettenis {
274*b725ae77Skettenis list = make_file_symbol_completion_list (symbol_start, word,
275*b725ae77Skettenis file_to_match);
276*b725ae77Skettenis xfree (file_to_match);
277*b725ae77Skettenis }
278*b725ae77Skettenis else
279*b725ae77Skettenis {
280*b725ae77Skettenis list = make_symbol_completion_list (symbol_start, word);
281*b725ae77Skettenis /* If text includes characters which cannot appear in a file
282*b725ae77Skettenis name, they cannot be asking for completion on files. */
283*b725ae77Skettenis if (strcspn (text, gdb_completer_file_name_break_characters) == text_len)
284*b725ae77Skettenis fn_list = make_source_files_completion_list (text, text);
285*b725ae77Skettenis }
286*b725ae77Skettenis
287*b725ae77Skettenis /* How many completions do we have in both lists? */
288*b725ae77Skettenis if (fn_list)
289*b725ae77Skettenis for ( ; fn_list[n_files]; n_files++)
290*b725ae77Skettenis ;
291*b725ae77Skettenis if (list)
292*b725ae77Skettenis for ( ; list[n_syms]; n_syms++)
293*b725ae77Skettenis ;
294*b725ae77Skettenis
295*b725ae77Skettenis /* Make list[] large enough to hold both lists, then catenate
296*b725ae77Skettenis fn_list[] onto the end of list[]. */
297*b725ae77Skettenis if (n_syms && n_files)
298*b725ae77Skettenis {
299*b725ae77Skettenis list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *));
300*b725ae77Skettenis memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *));
301*b725ae77Skettenis xfree (fn_list);
302*b725ae77Skettenis }
303*b725ae77Skettenis else if (n_files)
304*b725ae77Skettenis {
305*b725ae77Skettenis /* If we only have file names as possible completion, we should
306*b725ae77Skettenis bring them in sync with what rl_complete expects. The
307*b725ae77Skettenis problem is that if the user types "break /foo/b TAB", and the
308*b725ae77Skettenis possible completions are "/foo/bar" and "/foo/baz"
309*b725ae77Skettenis rl_complete expects us to return "bar" and "baz", without the
310*b725ae77Skettenis leading directories, as possible completions, because `word'
311*b725ae77Skettenis starts at the "b". But we ignore the value of `word' when we
312*b725ae77Skettenis call make_source_files_completion_list above (because that
313*b725ae77Skettenis would not DTRT when the completion results in both symbols
314*b725ae77Skettenis and file names), so make_source_files_completion_list returns
315*b725ae77Skettenis the full "/foo/bar" and "/foo/baz" strings. This produces
316*b725ae77Skettenis wrong results when, e.g., there's only one possible
317*b725ae77Skettenis completion, because rl_complete will prepend "/foo/" to each
318*b725ae77Skettenis candidate completion. The loop below removes that leading
319*b725ae77Skettenis part. */
320*b725ae77Skettenis for (n_files = 0; fn_list[n_files]; n_files++)
321*b725ae77Skettenis {
322*b725ae77Skettenis memmove (fn_list[n_files], fn_list[n_files] + (word - text),
323*b725ae77Skettenis strlen (fn_list[n_files]) + 1 - (word - text));
324*b725ae77Skettenis }
325*b725ae77Skettenis /* Return just the file-name list as the result. */
326*b725ae77Skettenis list = fn_list;
327*b725ae77Skettenis }
328*b725ae77Skettenis else if (!n_syms)
329*b725ae77Skettenis {
330*b725ae77Skettenis /* No completions at all. As the final resort, try completing
331*b725ae77Skettenis on the entire text as a symbol. */
332*b725ae77Skettenis list = make_symbol_completion_list (orig_text, word);
333*b725ae77Skettenis }
334*b725ae77Skettenis
335*b725ae77Skettenis return list;
336*b725ae77Skettenis }
337*b725ae77Skettenis
338*b725ae77Skettenis /* Complete on command names. Used by "help". */
339*b725ae77Skettenis char **
command_completer(char * text,char * word)340*b725ae77Skettenis command_completer (char *text, char *word)
341*b725ae77Skettenis {
342*b725ae77Skettenis return complete_on_cmdlist (cmdlist, text, word);
343*b725ae77Skettenis }
344*b725ae77Skettenis
345*b725ae77Skettenis
346*b725ae77Skettenis /* Here are some useful test cases for completion. FIXME: These should
347*b725ae77Skettenis be put in the test suite. They should be tested with both M-? and TAB.
348*b725ae77Skettenis
349*b725ae77Skettenis "show output-" "radix"
350*b725ae77Skettenis "show output" "-radix"
351*b725ae77Skettenis "p" ambiguous (commands starting with p--path, print, printf, etc.)
352*b725ae77Skettenis "p " ambiguous (all symbols)
353*b725ae77Skettenis "info t foo" no completions
354*b725ae77Skettenis "info t " no completions
355*b725ae77Skettenis "info t" ambiguous ("info target", "info terminal", etc.)
356*b725ae77Skettenis "info ajksdlfk" no completions
357*b725ae77Skettenis "info ajksdlfk " no completions
358*b725ae77Skettenis "info" " "
359*b725ae77Skettenis "info " ambiguous (all info commands)
360*b725ae77Skettenis "p \"a" no completions (string constant)
361*b725ae77Skettenis "p 'a" ambiguous (all symbols starting with a)
362*b725ae77Skettenis "p b-a" ambiguous (all symbols starting with a)
363*b725ae77Skettenis "p b-" ambiguous (all symbols)
364*b725ae77Skettenis "file Make" "file" (word break hard to screw up here)
365*b725ae77Skettenis "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
366*b725ae77Skettenis */
367*b725ae77Skettenis
368*b725ae77Skettenis /* Generate completions all at once. Returns a NULL-terminated array
369*b725ae77Skettenis of strings. Both the array and each element are allocated with
370*b725ae77Skettenis xmalloc. It can also return NULL if there are no completions.
371*b725ae77Skettenis
372*b725ae77Skettenis TEXT is the caller's idea of the "word" we are looking at.
373*b725ae77Skettenis
374*b725ae77Skettenis LINE_BUFFER is available to be looked at; it contains the entire text
375*b725ae77Skettenis of the line. POINT is the offset in that line of the cursor. You
376*b725ae77Skettenis should pretend that the line ends at POINT. */
377*b725ae77Skettenis
378*b725ae77Skettenis char **
complete_line(const char * text,char * line_buffer,int point)379*b725ae77Skettenis complete_line (const char *text, char *line_buffer, int point)
380*b725ae77Skettenis {
381*b725ae77Skettenis char **list = NULL;
382*b725ae77Skettenis char *tmp_command, *p;
383*b725ae77Skettenis /* Pointer within tmp_command which corresponds to text. */
384*b725ae77Skettenis char *word;
385*b725ae77Skettenis struct cmd_list_element *c, *result_list;
386*b725ae77Skettenis
387*b725ae77Skettenis /* Choose the default set of word break characters to break completions.
388*b725ae77Skettenis If we later find out that we are doing completions on command strings
389*b725ae77Skettenis (as opposed to strings supplied by the individual command completer
390*b725ae77Skettenis functions, which can be any string) then we will switch to the
391*b725ae77Skettenis special word break set for command strings, which leaves out the
392*b725ae77Skettenis '-' character used in some commands. */
393*b725ae77Skettenis
394*b725ae77Skettenis rl_completer_word_break_characters =
395*b725ae77Skettenis current_language->la_word_break_characters();
396*b725ae77Skettenis
397*b725ae77Skettenis /* Decide whether to complete on a list of gdb commands or on symbols. */
398*b725ae77Skettenis tmp_command = (char *) alloca (point + 1);
399*b725ae77Skettenis p = tmp_command;
400*b725ae77Skettenis
401*b725ae77Skettenis strncpy (tmp_command, line_buffer, point);
402*b725ae77Skettenis tmp_command[point] = '\0';
403*b725ae77Skettenis /* Since text always contains some number of characters leading up
404*b725ae77Skettenis to point, we can find the equivalent position in tmp_command
405*b725ae77Skettenis by subtracting that many characters from the end of tmp_command. */
406*b725ae77Skettenis word = tmp_command + point - strlen (text);
407*b725ae77Skettenis
408*b725ae77Skettenis if (point == 0)
409*b725ae77Skettenis {
410*b725ae77Skettenis /* An empty line we want to consider ambiguous; that is, it
411*b725ae77Skettenis could be any command. */
412*b725ae77Skettenis c = (struct cmd_list_element *) -1;
413*b725ae77Skettenis result_list = 0;
414*b725ae77Skettenis }
415*b725ae77Skettenis else
416*b725ae77Skettenis {
417*b725ae77Skettenis c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
418*b725ae77Skettenis }
419*b725ae77Skettenis
420*b725ae77Skettenis /* Move p up to the next interesting thing. */
421*b725ae77Skettenis while (*p == ' ' || *p == '\t')
422*b725ae77Skettenis {
423*b725ae77Skettenis p++;
424*b725ae77Skettenis }
425*b725ae77Skettenis
426*b725ae77Skettenis if (!c)
427*b725ae77Skettenis {
428*b725ae77Skettenis /* It is an unrecognized command. So there are no
429*b725ae77Skettenis possible completions. */
430*b725ae77Skettenis list = NULL;
431*b725ae77Skettenis }
432*b725ae77Skettenis else if (c == (struct cmd_list_element *) -1)
433*b725ae77Skettenis {
434*b725ae77Skettenis char *q;
435*b725ae77Skettenis
436*b725ae77Skettenis /* lookup_cmd_1 advances p up to the first ambiguous thing, but
437*b725ae77Skettenis doesn't advance over that thing itself. Do so now. */
438*b725ae77Skettenis q = p;
439*b725ae77Skettenis while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
440*b725ae77Skettenis ++q;
441*b725ae77Skettenis if (q != tmp_command + point)
442*b725ae77Skettenis {
443*b725ae77Skettenis /* There is something beyond the ambiguous
444*b725ae77Skettenis command, so there are no possible completions. For
445*b725ae77Skettenis example, "info t " or "info t foo" does not complete
446*b725ae77Skettenis to anything, because "info t" can be "info target" or
447*b725ae77Skettenis "info terminal". */
448*b725ae77Skettenis list = NULL;
449*b725ae77Skettenis }
450*b725ae77Skettenis else
451*b725ae77Skettenis {
452*b725ae77Skettenis /* We're trying to complete on the command which was ambiguous.
453*b725ae77Skettenis This we can deal with. */
454*b725ae77Skettenis if (result_list)
455*b725ae77Skettenis {
456*b725ae77Skettenis list = complete_on_cmdlist (*result_list->prefixlist, p,
457*b725ae77Skettenis word);
458*b725ae77Skettenis }
459*b725ae77Skettenis else
460*b725ae77Skettenis {
461*b725ae77Skettenis list = complete_on_cmdlist (cmdlist, p, word);
462*b725ae77Skettenis }
463*b725ae77Skettenis /* Insure that readline does the right thing with respect to
464*b725ae77Skettenis inserting quotes. */
465*b725ae77Skettenis rl_completer_word_break_characters =
466*b725ae77Skettenis gdb_completer_command_word_break_characters;
467*b725ae77Skettenis }
468*b725ae77Skettenis }
469*b725ae77Skettenis else
470*b725ae77Skettenis {
471*b725ae77Skettenis /* We've recognized a full command. */
472*b725ae77Skettenis
473*b725ae77Skettenis if (p == tmp_command + point)
474*b725ae77Skettenis {
475*b725ae77Skettenis /* There is no non-whitespace in the line beyond the command. */
476*b725ae77Skettenis
477*b725ae77Skettenis if (p[-1] == ' ' || p[-1] == '\t')
478*b725ae77Skettenis {
479*b725ae77Skettenis /* The command is followed by whitespace; we need to complete
480*b725ae77Skettenis on whatever comes after command. */
481*b725ae77Skettenis if (c->prefixlist)
482*b725ae77Skettenis {
483*b725ae77Skettenis /* It is a prefix command; what comes after it is
484*b725ae77Skettenis a subcommand (e.g. "info "). */
485*b725ae77Skettenis list = complete_on_cmdlist (*c->prefixlist, p, word);
486*b725ae77Skettenis
487*b725ae77Skettenis /* Insure that readline does the right thing
488*b725ae77Skettenis with respect to inserting quotes. */
489*b725ae77Skettenis rl_completer_word_break_characters =
490*b725ae77Skettenis gdb_completer_command_word_break_characters;
491*b725ae77Skettenis }
492*b725ae77Skettenis else if (c->enums)
493*b725ae77Skettenis {
494*b725ae77Skettenis list = complete_on_enum (c->enums, p, word);
495*b725ae77Skettenis rl_completer_word_break_characters =
496*b725ae77Skettenis gdb_completer_command_word_break_characters;
497*b725ae77Skettenis }
498*b725ae77Skettenis else
499*b725ae77Skettenis {
500*b725ae77Skettenis /* It is a normal command; what comes after it is
501*b725ae77Skettenis completed by the command's completer function. */
502*b725ae77Skettenis if (c->completer == filename_completer)
503*b725ae77Skettenis {
504*b725ae77Skettenis /* Many commands which want to complete on
505*b725ae77Skettenis file names accept several file names, as
506*b725ae77Skettenis in "run foo bar >>baz". So we don't want
507*b725ae77Skettenis to complete the entire text after the
508*b725ae77Skettenis command, just the last word. To this
509*b725ae77Skettenis end, we need to find the beginning of the
510*b725ae77Skettenis file name by starting at `word' and going
511*b725ae77Skettenis backwards. */
512*b725ae77Skettenis for (p = word;
513*b725ae77Skettenis p > tmp_command
514*b725ae77Skettenis && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
515*b725ae77Skettenis p--)
516*b725ae77Skettenis ;
517*b725ae77Skettenis rl_completer_word_break_characters =
518*b725ae77Skettenis gdb_completer_file_name_break_characters;
519*b725ae77Skettenis }
520*b725ae77Skettenis else if (c->completer == location_completer)
521*b725ae77Skettenis {
522*b725ae77Skettenis /* Commands which complete on locations want to
523*b725ae77Skettenis see the entire argument. */
524*b725ae77Skettenis for (p = word;
525*b725ae77Skettenis p > tmp_command
526*b725ae77Skettenis && p[-1] != ' ' && p[-1] != '\t';
527*b725ae77Skettenis p--)
528*b725ae77Skettenis ;
529*b725ae77Skettenis }
530*b725ae77Skettenis list = (*c->completer) (p, word);
531*b725ae77Skettenis }
532*b725ae77Skettenis }
533*b725ae77Skettenis else
534*b725ae77Skettenis {
535*b725ae77Skettenis /* The command is not followed by whitespace; we need to
536*b725ae77Skettenis complete on the command itself. e.g. "p" which is a
537*b725ae77Skettenis command itself but also can complete to "print", "ptype"
538*b725ae77Skettenis etc. */
539*b725ae77Skettenis char *q;
540*b725ae77Skettenis
541*b725ae77Skettenis /* Find the command we are completing on. */
542*b725ae77Skettenis q = p;
543*b725ae77Skettenis while (q > tmp_command)
544*b725ae77Skettenis {
545*b725ae77Skettenis if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
546*b725ae77Skettenis --q;
547*b725ae77Skettenis else
548*b725ae77Skettenis break;
549*b725ae77Skettenis }
550*b725ae77Skettenis
551*b725ae77Skettenis list = complete_on_cmdlist (result_list, q, word);
552*b725ae77Skettenis
553*b725ae77Skettenis /* Insure that readline does the right thing
554*b725ae77Skettenis with respect to inserting quotes. */
555*b725ae77Skettenis rl_completer_word_break_characters =
556*b725ae77Skettenis gdb_completer_command_word_break_characters;
557*b725ae77Skettenis }
558*b725ae77Skettenis }
559*b725ae77Skettenis else
560*b725ae77Skettenis {
561*b725ae77Skettenis /* There is non-whitespace beyond the command. */
562*b725ae77Skettenis
563*b725ae77Skettenis if (c->prefixlist && !c->allow_unknown)
564*b725ae77Skettenis {
565*b725ae77Skettenis /* It is an unrecognized subcommand of a prefix command,
566*b725ae77Skettenis e.g. "info adsfkdj". */
567*b725ae77Skettenis list = NULL;
568*b725ae77Skettenis }
569*b725ae77Skettenis else if (c->enums)
570*b725ae77Skettenis {
571*b725ae77Skettenis list = complete_on_enum (c->enums, p, word);
572*b725ae77Skettenis }
573*b725ae77Skettenis else
574*b725ae77Skettenis {
575*b725ae77Skettenis /* It is a normal command. */
576*b725ae77Skettenis if (c->completer == filename_completer)
577*b725ae77Skettenis {
578*b725ae77Skettenis /* See the commentary above about the specifics
579*b725ae77Skettenis of file-name completion. */
580*b725ae77Skettenis for (p = word;
581*b725ae77Skettenis p > tmp_command
582*b725ae77Skettenis && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
583*b725ae77Skettenis p--)
584*b725ae77Skettenis ;
585*b725ae77Skettenis rl_completer_word_break_characters =
586*b725ae77Skettenis gdb_completer_file_name_break_characters;
587*b725ae77Skettenis }
588*b725ae77Skettenis else if (c->completer == location_completer)
589*b725ae77Skettenis {
590*b725ae77Skettenis for (p = word;
591*b725ae77Skettenis p > tmp_command
592*b725ae77Skettenis && p[-1] != ' ' && p[-1] != '\t';
593*b725ae77Skettenis p--)
594*b725ae77Skettenis ;
595*b725ae77Skettenis }
596*b725ae77Skettenis list = (*c->completer) (p, word);
597*b725ae77Skettenis }
598*b725ae77Skettenis }
599*b725ae77Skettenis }
600*b725ae77Skettenis
601*b725ae77Skettenis return list;
602*b725ae77Skettenis }
603*b725ae77Skettenis
604*b725ae77Skettenis /* Generate completions one by one for the completer. Each time we are
605*b725ae77Skettenis called return another potential completion to the caller.
606*b725ae77Skettenis line_completion just completes on commands or passes the buck to the
607*b725ae77Skettenis command's completer function, the stuff specific to symbol completion
608*b725ae77Skettenis is in make_symbol_completion_list.
609*b725ae77Skettenis
610*b725ae77Skettenis TEXT is the caller's idea of the "word" we are looking at.
611*b725ae77Skettenis
612*b725ae77Skettenis MATCHES is the number of matches that have currently been collected from
613*b725ae77Skettenis calling this completion function. When zero, then we need to initialize,
614*b725ae77Skettenis otherwise the initialization has already taken place and we can just
615*b725ae77Skettenis return the next potential completion string.
616*b725ae77Skettenis
617*b725ae77Skettenis LINE_BUFFER is available to be looked at; it contains the entire text
618*b725ae77Skettenis of the line. POINT is the offset in that line of the cursor. You
619*b725ae77Skettenis should pretend that the line ends at POINT.
620*b725ae77Skettenis
621*b725ae77Skettenis Returns NULL if there are no more completions, else a pointer to a string
622*b725ae77Skettenis which is a possible completion, it is the caller's responsibility to
623*b725ae77Skettenis free the string. */
624*b725ae77Skettenis
625*b725ae77Skettenis static char *
line_completion_function(const char * text,int matches,char * line_buffer,int point)626*b725ae77Skettenis line_completion_function (const char *text, int matches, char *line_buffer, int point)
627*b725ae77Skettenis {
628*b725ae77Skettenis static char **list = (char **) NULL; /* Cache of completions */
629*b725ae77Skettenis static int index; /* Next cached completion */
630*b725ae77Skettenis char *output = NULL;
631*b725ae77Skettenis
632*b725ae77Skettenis if (matches == 0)
633*b725ae77Skettenis {
634*b725ae77Skettenis /* The caller is beginning to accumulate a new set of completions, so
635*b725ae77Skettenis we need to find all of them now, and cache them for returning one at
636*b725ae77Skettenis a time on future calls. */
637*b725ae77Skettenis
638*b725ae77Skettenis if (list)
639*b725ae77Skettenis {
640*b725ae77Skettenis /* Free the storage used by LIST, but not by the strings inside.
641*b725ae77Skettenis This is because rl_complete_internal () frees the strings. */
642*b725ae77Skettenis xfree (list);
643*b725ae77Skettenis }
644*b725ae77Skettenis index = 0;
645*b725ae77Skettenis list = complete_line (text, line_buffer, point);
646*b725ae77Skettenis }
647*b725ae77Skettenis
648*b725ae77Skettenis /* If we found a list of potential completions during initialization then
649*b725ae77Skettenis dole them out one at a time. The vector of completions is NULL
650*b725ae77Skettenis terminated, so after returning the last one, return NULL (and continue
651*b725ae77Skettenis to do so) each time we are called after that, until a new list is
652*b725ae77Skettenis available. */
653*b725ae77Skettenis
654*b725ae77Skettenis if (list)
655*b725ae77Skettenis {
656*b725ae77Skettenis output = list[index];
657*b725ae77Skettenis if (output)
658*b725ae77Skettenis {
659*b725ae77Skettenis index++;
660*b725ae77Skettenis }
661*b725ae77Skettenis }
662*b725ae77Skettenis
663*b725ae77Skettenis #if 0
664*b725ae77Skettenis /* Can't do this because readline hasn't yet checked the word breaks
665*b725ae77Skettenis for figuring out whether to insert a quote. */
666*b725ae77Skettenis if (output == NULL)
667*b725ae77Skettenis /* Make sure the word break characters are set back to normal for the
668*b725ae77Skettenis next time that readline tries to complete something. */
669*b725ae77Skettenis rl_completer_word_break_characters =
670*b725ae77Skettenis current_language->la_word_break_characters();
671*b725ae77Skettenis #endif
672*b725ae77Skettenis
673*b725ae77Skettenis return (output);
674*b725ae77Skettenis }
675*b725ae77Skettenis
676*b725ae77Skettenis /* Skip over the possibly quoted word STR (as defined by the quote
677*b725ae77Skettenis characters QUOTECHARS and the the word break characters
678*b725ae77Skettenis BREAKCHARS). Returns pointer to the location after the "word". If
679*b725ae77Skettenis either QUOTECHARS or BREAKCHARS is NULL, use the same values used
680*b725ae77Skettenis by the completer. */
681*b725ae77Skettenis
682*b725ae77Skettenis char *
skip_quoted_chars(char * str,char * quotechars,char * breakchars)683*b725ae77Skettenis skip_quoted_chars (char *str, char *quotechars, char *breakchars)
684*b725ae77Skettenis {
685*b725ae77Skettenis char quote_char = '\0';
686*b725ae77Skettenis char *scan;
687*b725ae77Skettenis
688*b725ae77Skettenis if (quotechars == NULL)
689*b725ae77Skettenis quotechars = gdb_completer_quote_characters;
690*b725ae77Skettenis
691*b725ae77Skettenis if (breakchars == NULL)
692*b725ae77Skettenis breakchars = current_language->la_word_break_characters();
693*b725ae77Skettenis
694*b725ae77Skettenis for (scan = str; *scan != '\0'; scan++)
695*b725ae77Skettenis {
696*b725ae77Skettenis if (quote_char != '\0')
697*b725ae77Skettenis {
698*b725ae77Skettenis /* Ignore everything until the matching close quote char */
699*b725ae77Skettenis if (*scan == quote_char)
700*b725ae77Skettenis {
701*b725ae77Skettenis /* Found matching close quote. */
702*b725ae77Skettenis scan++;
703*b725ae77Skettenis break;
704*b725ae77Skettenis }
705*b725ae77Skettenis }
706*b725ae77Skettenis else if (strchr (quotechars, *scan))
707*b725ae77Skettenis {
708*b725ae77Skettenis /* Found start of a quoted string. */
709*b725ae77Skettenis quote_char = *scan;
710*b725ae77Skettenis }
711*b725ae77Skettenis else if (strchr (breakchars, *scan))
712*b725ae77Skettenis {
713*b725ae77Skettenis break;
714*b725ae77Skettenis }
715*b725ae77Skettenis }
716*b725ae77Skettenis
717*b725ae77Skettenis return (scan);
718*b725ae77Skettenis }
719*b725ae77Skettenis
720*b725ae77Skettenis /* Skip over the possibly quoted word STR (as defined by the quote
721*b725ae77Skettenis characters and word break characters used by the completer).
722*b725ae77Skettenis Returns pointer to the location after the "word". */
723*b725ae77Skettenis
724*b725ae77Skettenis char *
skip_quoted(char * str)725*b725ae77Skettenis skip_quoted (char *str)
726*b725ae77Skettenis {
727*b725ae77Skettenis return skip_quoted_chars (str, NULL, NULL);
728*b725ae77Skettenis }
729