]>
Commit | Line | Data |
---|---|---|
c5f0f3d0 FN |
1 | /* Line completion stuff for GDB, the GNU debugger. |
2 | Copyright 2000 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "defs.h" | |
22 | #include "symtab.h" | |
23 | #include "gdbtypes.h" | |
24 | #include "expression.h" | |
25 | ||
26 | /* FIXME: This is needed because of lookup_cmd_1(). | |
27 | We should be calling a hook instead so we eliminate the CLI dependency. */ | |
28 | #include "gdbcmd.h" | |
29 | ||
30 | /* Needed for rl_completer_word_break_characters() */ | |
31 | #include <readline/readline.h> | |
32 | ||
33 | /* readline defines this. */ | |
34 | #undef savestring | |
35 | ||
36 | #include "completer.h" | |
37 | ||
38 | /* Prototypes for local functions */ | |
39 | ||
40 | /* readline uses the word breaks for two things: | |
41 | (1) In figuring out where to point the TEXT parameter to the | |
42 | rl_completion_entry_function. Since we don't use TEXT for much, | |
43 | it doesn't matter a lot what the word breaks are for this purpose, but | |
44 | it does affect how much stuff M-? lists. | |
45 | (2) If one of the matches contains a word break character, readline | |
46 | will quote it. That's why we switch between | |
47 | gdb_completer_word_break_characters and | |
48 | gdb_completer_command_word_break_characters. I'm not sure when | |
49 | we need this behavior (perhaps for funky characters in C++ symbols?). */ | |
50 | ||
51 | /* Variables which are necessary for fancy command line editing. */ | |
52 | static char *gdb_completer_word_break_characters = | |
53 | " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-"; | |
54 | ||
55 | /* When completing on command names, we remove '-' from the list of | |
56 | word break characters, since we use it in command names. If the | |
57 | readline library sees one in any of the current completion strings, | |
58 | it thinks that the string needs to be quoted and automatically supplies | |
59 | a leading quote. */ | |
60 | static char *gdb_completer_command_word_break_characters = | |
61 | " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,"; | |
62 | ||
63 | /* When completing on file names, we remove from the list of word | |
64 | break characters any characters that are commonly used in file | |
65 | names, such as '-', '+', '~', etc. Otherwise, readline displays | |
66 | incorrect completion candidates. */ | |
67 | static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?/><"; | |
68 | ||
69 | /* Characters that can be used to quote completion strings. Note that we | |
70 | can't include '"' because the gdb C parser treats such quoted sequences | |
71 | as strings. */ | |
72 | static char *gdb_completer_quote_characters = "'"; | |
73 | \f | |
74 | /* Accessor for some completer data that may interest other files. */ | |
75 | ||
76 | char * | |
77 | get_gdb_completer_word_break_characters (void) | |
78 | { | |
79 | return gdb_completer_word_break_characters; | |
80 | } | |
81 | ||
82 | char * | |
83 | get_gdb_completer_quote_characters (void) | |
84 | { | |
85 | return gdb_completer_quote_characters; | |
86 | } | |
87 | ||
88 | /* Complete on filenames. */ | |
89 | char ** | |
90 | filename_completer (char *text, char *word) | |
91 | { | |
92 | /* From readline. */ | |
93 | extern char *filename_completion_function (char *, int); | |
94 | int subsequent_name; | |
95 | char **return_val; | |
96 | int return_val_used; | |
97 | int return_val_alloced; | |
98 | ||
99 | return_val_used = 0; | |
100 | /* Small for testing. */ | |
101 | return_val_alloced = 1; | |
102 | return_val = (char **) xmalloc (return_val_alloced * sizeof (char *)); | |
103 | ||
104 | subsequent_name = 0; | |
105 | while (1) | |
106 | { | |
107 | char *p; | |
108 | p = filename_completion_function (text, subsequent_name); | |
109 | if (return_val_used >= return_val_alloced) | |
110 | { | |
111 | return_val_alloced *= 2; | |
112 | return_val = | |
113 | (char **) xrealloc (return_val, | |
114 | return_val_alloced * sizeof (char *)); | |
115 | } | |
116 | if (p == NULL) | |
117 | { | |
118 | return_val[return_val_used++] = p; | |
119 | break; | |
120 | } | |
121 | /* We need to set subsequent_name to a non-zero value before the | |
122 | continue line below, because otherwise, if the first file seen | |
123 | by GDB is a backup file whose name ends in a `~', we will loop | |
124 | indefinitely. */ | |
125 | subsequent_name = 1; | |
126 | /* Like emacs, don't complete on old versions. Especially useful | |
127 | in the "source" command. */ | |
128 | if (p[strlen (p) - 1] == '~') | |
129 | continue; | |
130 | ||
131 | { | |
132 | char *q; | |
133 | if (word == text) | |
134 | /* Return exactly p. */ | |
135 | return_val[return_val_used++] = p; | |
136 | else if (word > text) | |
137 | { | |
138 | /* Return some portion of p. */ | |
139 | q = xmalloc (strlen (p) + 5); | |
140 | strcpy (q, p + (word - text)); | |
141 | return_val[return_val_used++] = q; | |
142 | free (p); | |
143 | } | |
144 | else | |
145 | { | |
146 | /* Return some of TEXT plus p. */ | |
147 | q = xmalloc (strlen (p) + (text - word) + 5); | |
148 | strncpy (q, word, text - word); | |
149 | q[text - word] = '\0'; | |
150 | strcat (q, p); | |
151 | return_val[return_val_used++] = q; | |
152 | free (p); | |
153 | } | |
154 | } | |
155 | } | |
156 | #if 0 | |
157 | /* There is no way to do this just long enough to affect quote inserting | |
158 | without also affecting the next completion. This should be fixed in | |
159 | readline. FIXME. */ | |
160 | /* Insure that readline does the right thing | |
161 | with respect to inserting quotes. */ | |
162 | rl_completer_word_break_characters = ""; | |
163 | #endif | |
164 | return return_val; | |
165 | } | |
166 | ||
167 | /* Here are some useful test cases for completion. FIXME: These should | |
168 | be put in the test suite. They should be tested with both M-? and TAB. | |
169 | ||
170 | "show output-" "radix" | |
171 | "show output" "-radix" | |
172 | "p" ambiguous (commands starting with p--path, print, printf, etc.) | |
173 | "p " ambiguous (all symbols) | |
174 | "info t foo" no completions | |
175 | "info t " no completions | |
176 | "info t" ambiguous ("info target", "info terminal", etc.) | |
177 | "info ajksdlfk" no completions | |
178 | "info ajksdlfk " no completions | |
179 | "info" " " | |
180 | "info " ambiguous (all info commands) | |
181 | "p \"a" no completions (string constant) | |
182 | "p 'a" ambiguous (all symbols starting with a) | |
183 | "p b-a" ambiguous (all symbols starting with a) | |
184 | "p b-" ambiguous (all symbols) | |
185 | "file Make" "file" (word break hard to screw up here) | |
186 | "file ../gdb.stabs/we" "ird" (needs to not break word at slash) | |
187 | */ | |
188 | ||
189 | /* Generate completions one by one for the completer. Each time we are | |
190 | called return another potential completion to the caller. | |
191 | line_completion just completes on commands or passes the buck to the | |
192 | command's completer function, the stuff specific to symbol completion | |
193 | is in make_symbol_completion_list. | |
194 | ||
195 | TEXT is the caller's idea of the "word" we are looking at. | |
196 | ||
197 | MATCHES is the number of matches that have currently been collected from | |
198 | calling this completion function. When zero, then we need to initialize, | |
199 | otherwise the initialization has already taken place and we can just | |
200 | return the next potential completion string. | |
201 | ||
202 | LINE_BUFFER is available to be looked at; it contains the entire text | |
203 | of the line. POINT is the offset in that line of the cursor. You | |
204 | should pretend that the line ends at POINT. | |
205 | ||
206 | Returns NULL if there are no more completions, else a pointer to a string | |
207 | which is a possible completion, it is the caller's responsibility to | |
208 | free the string. */ | |
209 | ||
210 | char * | |
211 | line_completion_function (char *text, int matches, char *line_buffer, int point) | |
212 | { | |
213 | static char **list = (char **) NULL; /* Cache of completions */ | |
214 | static int index; /* Next cached completion */ | |
215 | char *output = NULL; | |
216 | char *tmp_command, *p; | |
217 | /* Pointer within tmp_command which corresponds to text. */ | |
218 | char *word; | |
219 | struct cmd_list_element *c, *result_list; | |
220 | ||
221 | if (matches == 0) | |
222 | { | |
223 | /* The caller is beginning to accumulate a new set of completions, so | |
224 | we need to find all of them now, and cache them for returning one at | |
225 | a time on future calls. */ | |
226 | ||
227 | if (list) | |
228 | { | |
229 | /* Free the storage used by LIST, but not by the strings inside. | |
230 | This is because rl_complete_internal () frees the strings. */ | |
231 | free ((PTR) list); | |
232 | } | |
233 | list = 0; | |
234 | index = 0; | |
235 | ||
236 | /* Choose the default set of word break characters to break completions. | |
237 | If we later find out that we are doing completions on command strings | |
238 | (as opposed to strings supplied by the individual command completer | |
239 | functions, which can be any string) then we will switch to the | |
240 | special word break set for command strings, which leaves out the | |
241 | '-' character used in some commands. */ | |
242 | ||
243 | rl_completer_word_break_characters = | |
244 | gdb_completer_word_break_characters; | |
245 | ||
246 | /* Decide whether to complete on a list of gdb commands or on symbols. */ | |
247 | tmp_command = (char *) alloca (point + 1); | |
248 | p = tmp_command; | |
249 | ||
250 | strncpy (tmp_command, line_buffer, point); | |
251 | tmp_command[point] = '\0'; | |
252 | /* Since text always contains some number of characters leading up | |
253 | to point, we can find the equivalent position in tmp_command | |
254 | by subtracting that many characters from the end of tmp_command. */ | |
255 | word = tmp_command + point - strlen (text); | |
256 | ||
257 | if (point == 0) | |
258 | { | |
259 | /* An empty line we want to consider ambiguous; that is, it | |
260 | could be any command. */ | |
261 | c = (struct cmd_list_element *) -1; | |
262 | result_list = 0; | |
263 | } | |
264 | else | |
265 | { | |
266 | c = lookup_cmd_1 (&p, cmdlist, &result_list, 1); | |
267 | } | |
268 | ||
269 | /* Move p up to the next interesting thing. */ | |
270 | while (*p == ' ' || *p == '\t') | |
271 | { | |
272 | p++; | |
273 | } | |
274 | ||
275 | if (!c) | |
276 | { | |
277 | /* It is an unrecognized command. So there are no | |
278 | possible completions. */ | |
279 | list = NULL; | |
280 | } | |
281 | else if (c == (struct cmd_list_element *) -1) | |
282 | { | |
283 | char *q; | |
284 | ||
285 | /* lookup_cmd_1 advances p up to the first ambiguous thing, but | |
286 | doesn't advance over that thing itself. Do so now. */ | |
287 | q = p; | |
288 | while (*q && (isalnum (*q) || *q == '-' || *q == '_')) | |
289 | ++q; | |
290 | if (q != tmp_command + point) | |
291 | { | |
292 | /* There is something beyond the ambiguous | |
293 | command, so there are no possible completions. For | |
294 | example, "info t " or "info t foo" does not complete | |
295 | to anything, because "info t" can be "info target" or | |
296 | "info terminal". */ | |
297 | list = NULL; | |
298 | } | |
299 | else | |
300 | { | |
301 | /* We're trying to complete on the command which was ambiguous. | |
302 | This we can deal with. */ | |
303 | if (result_list) | |
304 | { | |
305 | list = complete_on_cmdlist (*result_list->prefixlist, p, | |
306 | word); | |
307 | } | |
308 | else | |
309 | { | |
310 | list = complete_on_cmdlist (cmdlist, p, word); | |
311 | } | |
312 | /* Insure that readline does the right thing with respect to | |
313 | inserting quotes. */ | |
314 | rl_completer_word_break_characters = | |
315 | gdb_completer_command_word_break_characters; | |
316 | } | |
317 | } | |
318 | else | |
319 | { | |
320 | /* We've recognized a full command. */ | |
321 | ||
322 | if (p == tmp_command + point) | |
323 | { | |
324 | /* There is no non-whitespace in the line beyond the command. */ | |
325 | ||
326 | if (p[-1] == ' ' || p[-1] == '\t') | |
327 | { | |
328 | /* The command is followed by whitespace; we need to complete | |
329 | on whatever comes after command. */ | |
330 | if (c->prefixlist) | |
331 | { | |
332 | /* It is a prefix command; what comes after it is | |
333 | a subcommand (e.g. "info "). */ | |
334 | list = complete_on_cmdlist (*c->prefixlist, p, word); | |
335 | ||
336 | /* Insure that readline does the right thing | |
337 | with respect to inserting quotes. */ | |
338 | rl_completer_word_break_characters = | |
339 | gdb_completer_command_word_break_characters; | |
340 | } | |
341 | else if (c->enums) | |
342 | { | |
343 | list = complete_on_enum (c->enums, p, word); | |
344 | rl_completer_word_break_characters = | |
345 | gdb_completer_command_word_break_characters; | |
346 | } | |
347 | else | |
348 | { | |
349 | /* It is a normal command; what comes after it is | |
350 | completed by the command's completer function. */ | |
351 | list = (*c->completer) (p, word); | |
352 | if (c->completer == filename_completer) | |
353 | rl_completer_word_break_characters = | |
354 | gdb_completer_file_name_break_characters; | |
355 | } | |
356 | } | |
357 | else | |
358 | { | |
359 | /* The command is not followed by whitespace; we need to | |
360 | complete on the command itself. e.g. "p" which is a | |
361 | command itself but also can complete to "print", "ptype" | |
362 | etc. */ | |
363 | char *q; | |
364 | ||
365 | /* Find the command we are completing on. */ | |
366 | q = p; | |
367 | while (q > tmp_command) | |
368 | { | |
369 | if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_') | |
370 | --q; | |
371 | else | |
372 | break; | |
373 | } | |
374 | ||
375 | list = complete_on_cmdlist (result_list, q, word); | |
376 | ||
377 | /* Insure that readline does the right thing | |
378 | with respect to inserting quotes. */ | |
379 | rl_completer_word_break_characters = | |
380 | gdb_completer_command_word_break_characters; | |
381 | } | |
382 | } | |
383 | else | |
384 | { | |
385 | /* There is non-whitespace beyond the command. */ | |
386 | ||
387 | if (c->prefixlist && !c->allow_unknown) | |
388 | { | |
389 | /* It is an unrecognized subcommand of a prefix command, | |
390 | e.g. "info adsfkdj". */ | |
391 | list = NULL; | |
392 | } | |
393 | else if (c->enums) | |
394 | { | |
395 | list = complete_on_enum (c->enums, p, word); | |
396 | } | |
397 | else | |
398 | { | |
399 | /* It is a normal command. */ | |
400 | list = (*c->completer) (p, word); | |
401 | if (c->completer == filename_completer) | |
402 | rl_completer_word_break_characters = | |
403 | gdb_completer_file_name_break_characters; | |
404 | } | |
405 | } | |
406 | } | |
407 | } | |
408 | ||
409 | /* If we found a list of potential completions during initialization then | |
410 | dole them out one at a time. The vector of completions is NULL | |
411 | terminated, so after returning the last one, return NULL (and continue | |
412 | to do so) each time we are called after that, until a new list is | |
413 | available. */ | |
414 | ||
415 | if (list) | |
416 | { | |
417 | output = list[index]; | |
418 | if (output) | |
419 | { | |
420 | index++; | |
421 | } | |
422 | } | |
423 | ||
424 | #if 0 | |
425 | /* Can't do this because readline hasn't yet checked the word breaks | |
426 | for figuring out whether to insert a quote. */ | |
427 | if (output == NULL) | |
428 | /* Make sure the word break characters are set back to normal for the | |
429 | next time that readline tries to complete something. */ | |
430 | rl_completer_word_break_characters = | |
431 | gdb_completer_word_break_characters; | |
432 | #endif | |
433 | ||
434 | return (output); | |
435 | } | |
436 | /* Skip over a possibly quoted word (as defined by the quote characters | |
437 | and word break characters the completer uses). Returns pointer to the | |
438 | location after the "word". */ | |
439 | ||
440 | char * | |
441 | skip_quoted (char *str) | |
442 | { | |
443 | char quote_char = '\0'; | |
444 | char *scan; | |
445 | ||
446 | for (scan = str; *scan != '\0'; scan++) | |
447 | { | |
448 | if (quote_char != '\0') | |
449 | { | |
450 | /* Ignore everything until the matching close quote char */ | |
451 | if (*scan == quote_char) | |
452 | { | |
453 | /* Found matching close quote. */ | |
454 | scan++; | |
455 | break; | |
456 | } | |
457 | } | |
458 | else if (strchr (gdb_completer_quote_characters, *scan)) | |
459 | { | |
460 | /* Found start of a quoted string. */ | |
461 | quote_char = *scan; | |
462 | } | |
463 | else if (strchr (gdb_completer_word_break_characters, *scan)) | |
464 | { | |
465 | break; | |
466 | } | |
467 | } | |
468 | return (scan); | |
469 | } | |
470 |