7fc27b17782b1e091cf8752bbdeff8ea7c426b10
[deliverable/binutils-gdb.git] / gdb / completer.c
1 /* Line completion stuff for GDB, the GNU debugger.
2 Copyright (C) 2000-2015 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 3 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, see <http://www.gnu.org/licenses/>. */
18
19 #include "defs.h"
20 #include "symtab.h"
21 #include "gdbtypes.h"
22 #include "expression.h"
23 #include "filenames.h" /* For DOSish file names. */
24 #include "language.h"
25 #include "gdb_signals.h"
26 #include "target.h"
27 #include "reggroups.h"
28 #include "user-regs.h"
29 #include "arch-utils.h"
30 #include "location.h"
31
32 #include "cli/cli-decode.h"
33
34 /* FIXME: This is needed because of lookup_cmd_1 (). We should be
35 calling a hook instead so we eliminate the CLI dependency. */
36 #include "gdbcmd.h"
37
38 /* Needed for rl_completer_word_break_characters() and for
39 rl_filename_completion_function. */
40 #include "readline/readline.h"
41
42 /* readline defines this. */
43 #undef savestring
44
45 #include "completer.h"
46
47 /* An enumeration of the various things a user might
48 attempt to complete for a location. */
49
50 enum explicit_location_match_type
51 {
52 /* The filename of a source file. */
53 MATCH_SOURCE,
54
55 /* The name of a function or method. */
56 MATCH_FUNCTION,
57
58 /* The name of a label. */
59 MATCH_LABEL
60 };
61
62 /* Prototypes for local functions. */
63 static
64 char *line_completion_function (const char *text, int matches,
65 char *line_buffer,
66 int point);
67
68 /* readline uses the word breaks for two things:
69 (1) In figuring out where to point the TEXT parameter to the
70 rl_completion_entry_function. Since we don't use TEXT for much,
71 it doesn't matter a lot what the word breaks are for this purpose,
72 but it does affect how much stuff M-? lists.
73 (2) If one of the matches contains a word break character, readline
74 will quote it. That's why we switch between
75 current_language->la_word_break_characters() and
76 gdb_completer_command_word_break_characters. I'm not sure when
77 we need this behavior (perhaps for funky characters in C++
78 symbols?). */
79
80 /* Variables which are necessary for fancy command line editing. */
81
82 /* When completing on command names, we remove '-' from the list of
83 word break characters, since we use it in command names. If the
84 readline library sees one in any of the current completion strings,
85 it thinks that the string needs to be quoted and automatically
86 supplies a leading quote. */
87 static char *gdb_completer_command_word_break_characters =
88 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
89
90 /* When completing on file names, we remove from the list of word
91 break characters any characters that are commonly used in file
92 names, such as '-', '+', '~', etc. Otherwise, readline displays
93 incorrect completion candidates. */
94 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
95 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
96 programs support @foo style response files. */
97 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
98 #else
99 static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
100 #endif
101
102 /* Characters that can be used to quote completion strings. Note that
103 we can't include '"' because the gdb C parser treats such quoted
104 sequences as strings. */
105 static char *gdb_completer_quote_characters = "'";
106 \f
107 /* Accessor for some completer data that may interest other files. */
108
109 char *
110 get_gdb_completer_quote_characters (void)
111 {
112 return gdb_completer_quote_characters;
113 }
114
115 /* Line completion interface function for readline. */
116
117 char *
118 readline_line_completion_function (const char *text, int matches)
119 {
120 return line_completion_function (text, matches,
121 rl_line_buffer, rl_point);
122 }
123
124 /* This can be used for functions which don't want to complete on
125 symbols but don't want to complete on anything else either. */
126 VEC (char_ptr) *
127 noop_completer (struct cmd_list_element *ignore,
128 const char *text, const char *prefix)
129 {
130 return NULL;
131 }
132
133 /* Complete on filenames. */
134 VEC (char_ptr) *
135 filename_completer (struct cmd_list_element *ignore,
136 const char *text, const char *word)
137 {
138 int subsequent_name;
139 VEC (char_ptr) *return_val = NULL;
140
141 subsequent_name = 0;
142 while (1)
143 {
144 char *p, *q;
145
146 p = rl_filename_completion_function (text, subsequent_name);
147 if (p == NULL)
148 break;
149 /* We need to set subsequent_name to a non-zero value before the
150 continue line below, because otherwise, if the first file
151 seen by GDB is a backup file whose name ends in a `~', we
152 will loop indefinitely. */
153 subsequent_name = 1;
154 /* Like emacs, don't complete on old versions. Especially
155 useful in the "source" command. */
156 if (p[strlen (p) - 1] == '~')
157 {
158 xfree (p);
159 continue;
160 }
161
162 if (word == text)
163 /* Return exactly p. */
164 q = p;
165 else if (word > text)
166 {
167 /* Return some portion of p. */
168 q = xmalloc (strlen (p) + 5);
169 strcpy (q, p + (word - text));
170 xfree (p);
171 }
172 else
173 {
174 /* Return some of TEXT plus p. */
175 q = xmalloc (strlen (p) + (text - word) + 5);
176 strncpy (q, word, text - word);
177 q[text - word] = '\0';
178 strcat (q, p);
179 xfree (p);
180 }
181 VEC_safe_push (char_ptr, return_val, q);
182 }
183 #if 0
184 /* There is no way to do this just long enough to affect quote
185 inserting without also affecting the next completion. This
186 should be fixed in readline. FIXME. */
187 /* Ensure that readline does the right thing
188 with respect to inserting quotes. */
189 rl_completer_word_break_characters = "";
190 #endif
191 return return_val;
192 }
193
194 /* Complete on linespecs, which might be of two possible forms:
195
196 file:line
197 or
198 symbol+offset
199
200 This is intended to be used in commands that set breakpoints
201 etc. */
202
203 static VEC (char_ptr) *
204 linespec_location_completer (struct cmd_list_element *ignore,
205 const char *text, const char *word)
206 {
207 int n_syms, n_files, ix;
208 VEC (char_ptr) *fn_list = NULL;
209 VEC (char_ptr) *list = NULL;
210 const char *p;
211 int quote_found = 0;
212 int quoted = *text == '\'' || *text == '"';
213 int quote_char = '\0';
214 const char *colon = NULL;
215 char *file_to_match = NULL;
216 const char *symbol_start = text;
217 const char *orig_text = text;
218 size_t text_len;
219
220 /* Do we have an unquoted colon, as in "break foo.c:bar"? */
221 for (p = text; *p != '\0'; ++p)
222 {
223 if (*p == '\\' && p[1] == '\'')
224 p++;
225 else if (*p == '\'' || *p == '"')
226 {
227 quote_found = *p;
228 quote_char = *p++;
229 while (*p != '\0' && *p != quote_found)
230 {
231 if (*p == '\\' && p[1] == quote_found)
232 p++;
233 p++;
234 }
235
236 if (*p == quote_found)
237 quote_found = 0;
238 else
239 break; /* Hit the end of text. */
240 }
241 #if HAVE_DOS_BASED_FILE_SYSTEM
242 /* If we have a DOS-style absolute file name at the beginning of
243 TEXT, and the colon after the drive letter is the only colon
244 we found, pretend the colon is not there. */
245 else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
246 ;
247 #endif
248 else if (*p == ':' && !colon)
249 {
250 colon = p;
251 symbol_start = p + 1;
252 }
253 else if (strchr (current_language->la_word_break_characters(), *p))
254 symbol_start = p + 1;
255 }
256
257 if (quoted)
258 text++;
259 text_len = strlen (text);
260
261 /* Where is the file name? */
262 if (colon)
263 {
264 char *s;
265
266 file_to_match = (char *) xmalloc (colon - text + 1);
267 strncpy (file_to_match, text, colon - text + 1);
268 /* Remove trailing colons and quotes from the file name. */
269 for (s = file_to_match + (colon - text);
270 s > file_to_match;
271 s--)
272 if (*s == ':' || *s == quote_char)
273 *s = '\0';
274 }
275 /* If the text includes a colon, they want completion only on a
276 symbol name after the colon. Otherwise, we need to complete on
277 symbols as well as on files. */
278 if (colon)
279 {
280 list = make_file_symbol_completion_list (symbol_start, word,
281 file_to_match);
282 xfree (file_to_match);
283 }
284 else
285 {
286 list = make_symbol_completion_list (symbol_start, word);
287 /* If text includes characters which cannot appear in a file
288 name, they cannot be asking for completion on files. */
289 if (strcspn (text,
290 gdb_completer_file_name_break_characters) == text_len)
291 fn_list = make_source_files_completion_list (text, text);
292 }
293
294 n_syms = VEC_length (char_ptr, list);
295 n_files = VEC_length (char_ptr, fn_list);
296
297 /* Catenate fn_list[] onto the end of list[]. */
298 if (!n_syms)
299 {
300 VEC_free (char_ptr, list); /* Paranoia. */
301 list = fn_list;
302 fn_list = NULL;
303 }
304 else
305 {
306 char *fn;
307
308 for (ix = 0; VEC_iterate (char_ptr, fn_list, ix, fn); ++ix)
309 VEC_safe_push (char_ptr, list, fn);
310 VEC_free (char_ptr, fn_list);
311 }
312
313 if (n_syms && n_files)
314 {
315 /* Nothing. */
316 }
317 else if (n_files)
318 {
319 char *fn;
320
321 /* If we only have file names as possible completion, we should
322 bring them in sync with what rl_complete expects. The
323 problem is that if the user types "break /foo/b TAB", and the
324 possible completions are "/foo/bar" and "/foo/baz"
325 rl_complete expects us to return "bar" and "baz", without the
326 leading directories, as possible completions, because `word'
327 starts at the "b". But we ignore the value of `word' when we
328 call make_source_files_completion_list above (because that
329 would not DTRT when the completion results in both symbols
330 and file names), so make_source_files_completion_list returns
331 the full "/foo/bar" and "/foo/baz" strings. This produces
332 wrong results when, e.g., there's only one possible
333 completion, because rl_complete will prepend "/foo/" to each
334 candidate completion. The loop below removes that leading
335 part. */
336 for (ix = 0; VEC_iterate (char_ptr, list, ix, fn); ++ix)
337 {
338 memmove (fn, fn + (word - text),
339 strlen (fn) + 1 - (word - text));
340 }
341 }
342 else if (!n_syms)
343 {
344 /* No completions at all. As the final resort, try completing
345 on the entire text as a symbol. */
346 list = make_symbol_completion_list (orig_text, word);
347 }
348
349 return list;
350 }
351
352 /* A helper function to collect explicit location matches for the given
353 LOCATION, which is attempting to match on WORD. */
354
355 static VEC (char_ptr) *
356 collect_explicit_location_matches (struct event_location *location,
357 enum explicit_location_match_type what,
358 const char *word)
359 {
360 VEC (char_ptr) *matches = NULL;
361 const struct explicit_location *explicit = get_explicit_location (location);
362
363 switch (what)
364 {
365 case MATCH_SOURCE:
366 {
367 const char *text = (explicit->source_filename == NULL
368 ? "" : explicit->source_filename);
369
370 matches = make_source_files_completion_list (text, word);
371 }
372 break;
373
374 case MATCH_FUNCTION:
375 {
376 const char *text = (explicit->function_name == NULL
377 ? "" : explicit->function_name);
378
379 if (explicit->source_filename != NULL)
380 {
381 matches
382 = make_file_symbol_completion_list (text, word,
383 explicit->source_filename);
384 }
385 else
386 matches = make_symbol_completion_list (text, word);
387 }
388 break;
389
390 case MATCH_LABEL:
391 /* Not supported. */
392 break;
393
394 default:
395 gdb_assert_not_reached ("unhandled explicit_location_match_type");
396 }
397
398 return matches;
399 }
400
401 /* A convenience macro to (safely) back up P to the previous word. */
402
403 static const char *
404 backup_text_ptr (const char *p, const char *text)
405 {
406 while (p > text && isspace (*p))
407 --p;
408 for (; p > text && !isspace (p[-1]); --p)
409 ;
410
411 return p;
412 }
413
414 /* A completer function for explicit locations. This function
415 completes both options ("-source", "-line", etc) and values. */
416
417 static VEC (char_ptr) *
418 explicit_location_completer (struct cmd_list_element *ignore,
419 struct event_location *location,
420 const char *text, const char *word)
421 {
422 const char *p;
423 VEC (char_ptr) *matches = NULL;
424
425 /* Find the beginning of the word. This is necessary because
426 we need to know if we are completing an option name or value. We
427 don't get the leading '-' from the completer. */
428 p = backup_text_ptr (word, text);
429
430 if (*p == '-')
431 {
432 /* Completing on option name. */
433 static const char *const keywords[] =
434 {
435 "source",
436 "function",
437 "line",
438 "label",
439 NULL
440 };
441
442 /* Skip over the '-'. */
443 ++p;
444
445 return complete_on_enum (keywords, p, p);
446 }
447 else
448 {
449 /* Completing on value (or unknown). Get the previous word to see what
450 the user is completing on. */
451 size_t len, offset;
452 const char *new_word, *end;
453 enum explicit_location_match_type what;
454 struct explicit_location *explicit = get_explicit_location (location);
455
456 /* Backup P to the previous word, which should be the option
457 the user is attempting to complete. */
458 offset = word - p;
459 end = --p;
460 p = backup_text_ptr (p, text);
461 len = end - p;
462
463 if (strncmp (p, "-source", len) == 0)
464 {
465 what = MATCH_SOURCE;
466 new_word = explicit->source_filename + offset;
467 }
468 else if (strncmp (p, "-function", len) == 0)
469 {
470 what = MATCH_FUNCTION;
471 new_word = explicit->function_name + offset;
472 }
473 else if (strncmp (p, "-label", len) == 0)
474 {
475 what = MATCH_LABEL;
476 new_word = explicit->label_name + offset;
477 }
478 else
479 {
480 /* The user isn't completing on any valid option name,
481 e.g., "break -source foo.c [tab]". */
482 return NULL;
483 }
484
485 /* If the user hasn't entered a search expression, e.g.,
486 "break -function <TAB><TAB>", new_word will be NULL, but
487 search routines require non-NULL search words. */
488 if (new_word == NULL)
489 new_word = "";
490
491 /* Now gather matches */
492 matches = collect_explicit_location_matches (location, what, new_word);
493 }
494
495 return matches;
496 }
497
498 /* A completer for locations. */
499
500 VEC (char_ptr) *
501 location_completer (struct cmd_list_element *ignore,
502 const char *text, const char *word)
503 {
504 VEC (char_ptr) *matches = NULL;
505 const char *copy = text;
506 struct event_location *location;
507
508 location = string_to_explicit_location (&copy, current_language, 1);
509 if (location != NULL)
510 {
511 struct cleanup *cleanup;
512
513 cleanup = make_cleanup_delete_event_location (location);
514 matches = explicit_location_completer (ignore, location, text, word);
515 do_cleanups (cleanup);
516 }
517 else
518 {
519 /* This is an address or linespec location.
520 Right now both of these are handled by the (old) linespec
521 completer. */
522 matches = linespec_location_completer (ignore, text, word);
523 }
524
525 return matches;
526 }
527
528 /* Helper for expression_completer which recursively adds field and
529 method names from TYPE, a struct or union type, to the array
530 OUTPUT. */
531 static void
532 add_struct_fields (struct type *type, VEC (char_ptr) **output,
533 char *fieldname, int namelen)
534 {
535 int i;
536 int computed_type_name = 0;
537 const char *type_name = NULL;
538
539 type = check_typedef (type);
540 for (i = 0; i < TYPE_NFIELDS (type); ++i)
541 {
542 if (i < TYPE_N_BASECLASSES (type))
543 add_struct_fields (TYPE_BASECLASS (type, i),
544 output, fieldname, namelen);
545 else if (TYPE_FIELD_NAME (type, i))
546 {
547 if (TYPE_FIELD_NAME (type, i)[0] != '\0')
548 {
549 if (! strncmp (TYPE_FIELD_NAME (type, i),
550 fieldname, namelen))
551 VEC_safe_push (char_ptr, *output,
552 xstrdup (TYPE_FIELD_NAME (type, i)));
553 }
554 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
555 {
556 /* Recurse into anonymous unions. */
557 add_struct_fields (TYPE_FIELD_TYPE (type, i),
558 output, fieldname, namelen);
559 }
560 }
561 }
562
563 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
564 {
565 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
566
567 if (name && ! strncmp (name, fieldname, namelen))
568 {
569 if (!computed_type_name)
570 {
571 type_name = type_name_no_tag (type);
572 computed_type_name = 1;
573 }
574 /* Omit constructors from the completion list. */
575 if (!type_name || strcmp (type_name, name))
576 VEC_safe_push (char_ptr, *output, xstrdup (name));
577 }
578 }
579 }
580
581 /* Complete on expressions. Often this means completing on symbol
582 names, but some language parsers also have support for completing
583 field names. */
584 VEC (char_ptr) *
585 expression_completer (struct cmd_list_element *ignore,
586 const char *text, const char *word)
587 {
588 struct type *type = NULL;
589 char *fieldname;
590 const char *p;
591 enum type_code code = TYPE_CODE_UNDEF;
592
593 /* Perform a tentative parse of the expression, to see whether a
594 field completion is required. */
595 fieldname = NULL;
596 TRY
597 {
598 type = parse_expression_for_completion (text, &fieldname, &code);
599 }
600 CATCH (except, RETURN_MASK_ERROR)
601 {
602 return NULL;
603 }
604 END_CATCH
605
606 if (fieldname && type)
607 {
608 for (;;)
609 {
610 type = check_typedef (type);
611 if (TYPE_CODE (type) != TYPE_CODE_PTR
612 && TYPE_CODE (type) != TYPE_CODE_REF)
613 break;
614 type = TYPE_TARGET_TYPE (type);
615 }
616
617 if (TYPE_CODE (type) == TYPE_CODE_UNION
618 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
619 {
620 int flen = strlen (fieldname);
621 VEC (char_ptr) *result = NULL;
622
623 add_struct_fields (type, &result, fieldname, flen);
624 xfree (fieldname);
625 return result;
626 }
627 }
628 else if (fieldname && code != TYPE_CODE_UNDEF)
629 {
630 VEC (char_ptr) *result;
631 struct cleanup *cleanup = make_cleanup (xfree, fieldname);
632
633 result = make_symbol_completion_type (fieldname, fieldname, code);
634 do_cleanups (cleanup);
635 return result;
636 }
637 xfree (fieldname);
638
639 /* Commands which complete on locations want to see the entire
640 argument. */
641 for (p = word;
642 p > text && p[-1] != ' ' && p[-1] != '\t';
643 p--)
644 ;
645
646 /* Not ideal but it is what we used to do before... */
647 return location_completer (ignore, p, word);
648 }
649
650 /* See definition in completer.h. */
651
652 void
653 set_gdb_completion_word_break_characters (completer_ftype *fn)
654 {
655 /* So far we are only interested in differentiating filename
656 completers from everything else. */
657 if (fn == filename_completer)
658 rl_completer_word_break_characters
659 = gdb_completer_file_name_break_characters;
660 else
661 rl_completer_word_break_characters
662 = gdb_completer_command_word_break_characters;
663 }
664
665 /* Here are some useful test cases for completion. FIXME: These
666 should be put in the test suite. They should be tested with both
667 M-? and TAB.
668
669 "show output-" "radix"
670 "show output" "-radix"
671 "p" ambiguous (commands starting with p--path, print, printf, etc.)
672 "p " ambiguous (all symbols)
673 "info t foo" no completions
674 "info t " no completions
675 "info t" ambiguous ("info target", "info terminal", etc.)
676 "info ajksdlfk" no completions
677 "info ajksdlfk " no completions
678 "info" " "
679 "info " ambiguous (all info commands)
680 "p \"a" no completions (string constant)
681 "p 'a" ambiguous (all symbols starting with a)
682 "p b-a" ambiguous (all symbols starting with a)
683 "p b-" ambiguous (all symbols)
684 "file Make" "file" (word break hard to screw up here)
685 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
686 */
687
688 typedef enum
689 {
690 handle_brkchars,
691 handle_completions,
692 handle_help
693 }
694 complete_line_internal_reason;
695
696
697 /* Internal function used to handle completions.
698
699
700 TEXT is the caller's idea of the "word" we are looking at.
701
702 LINE_BUFFER is available to be looked at; it contains the entire
703 text of the line. POINT is the offset in that line of the cursor.
704 You should pretend that the line ends at POINT.
705
706 REASON is of type complete_line_internal_reason.
707
708 If REASON is handle_brkchars:
709 Preliminary phase, called by gdb_completion_word_break_characters
710 function, is used to determine the correct set of chars that are
711 word delimiters depending on the current command in line_buffer.
712 No completion list should be generated; the return value should be
713 NULL. This is checked by an assertion in that function.
714
715 If REASON is handle_completions:
716 Main phase, called by complete_line function, is used to get the list
717 of posible completions.
718
719 If REASON is handle_help:
720 Special case when completing a 'help' command. In this case,
721 once sub-command completions are exhausted, we simply return NULL.
722 */
723
724 static VEC (char_ptr) *
725 complete_line_internal (const char *text,
726 const char *line_buffer, int point,
727 complete_line_internal_reason reason)
728 {
729 VEC (char_ptr) *list = NULL;
730 char *tmp_command;
731 const char *p;
732 int ignore_help_classes;
733 /* Pointer within tmp_command which corresponds to text. */
734 char *word;
735 struct cmd_list_element *c, *result_list;
736
737 /* Choose the default set of word break characters to break
738 completions. If we later find out that we are doing completions
739 on command strings (as opposed to strings supplied by the
740 individual command completer functions, which can be any string)
741 then we will switch to the special word break set for command
742 strings, which leaves out the '-' character used in some
743 commands. */
744 rl_completer_word_break_characters =
745 current_language->la_word_break_characters();
746
747 /* Decide whether to complete on a list of gdb commands or on
748 symbols. */
749 tmp_command = (char *) alloca (point + 1);
750 p = tmp_command;
751
752 /* The help command should complete help aliases. */
753 ignore_help_classes = reason != handle_help;
754
755 strncpy (tmp_command, line_buffer, point);
756 tmp_command[point] = '\0';
757 /* Since text always contains some number of characters leading up
758 to point, we can find the equivalent position in tmp_command
759 by subtracting that many characters from the end of tmp_command. */
760 word = tmp_command + point - strlen (text);
761
762 if (point == 0)
763 {
764 /* An empty line we want to consider ambiguous; that is, it
765 could be any command. */
766 c = CMD_LIST_AMBIGUOUS;
767 result_list = 0;
768 }
769 else
770 {
771 c = lookup_cmd_1 (&p, cmdlist, &result_list, ignore_help_classes);
772 }
773
774 /* Move p up to the next interesting thing. */
775 while (*p == ' ' || *p == '\t')
776 {
777 p++;
778 }
779
780 if (!c)
781 {
782 /* It is an unrecognized command. So there are no
783 possible completions. */
784 list = NULL;
785 }
786 else if (c == CMD_LIST_AMBIGUOUS)
787 {
788 const char *q;
789
790 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
791 doesn't advance over that thing itself. Do so now. */
792 q = p;
793 while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
794 ++q;
795 if (q != tmp_command + point)
796 {
797 /* There is something beyond the ambiguous
798 command, so there are no possible completions. For
799 example, "info t " or "info t foo" does not complete
800 to anything, because "info t" can be "info target" or
801 "info terminal". */
802 list = NULL;
803 }
804 else
805 {
806 /* We're trying to complete on the command which was ambiguous.
807 This we can deal with. */
808 if (result_list)
809 {
810 if (reason != handle_brkchars)
811 list = complete_on_cmdlist (*result_list->prefixlist, p,
812 word, ignore_help_classes);
813 }
814 else
815 {
816 if (reason != handle_brkchars)
817 list = complete_on_cmdlist (cmdlist, p, word,
818 ignore_help_classes);
819 }
820 /* Ensure that readline does the right thing with respect to
821 inserting quotes. */
822 rl_completer_word_break_characters =
823 gdb_completer_command_word_break_characters;
824 }
825 }
826 else
827 {
828 /* We've recognized a full command. */
829
830 if (p == tmp_command + point)
831 {
832 /* There is no non-whitespace in the line beyond the
833 command. */
834
835 if (p[-1] == ' ' || p[-1] == '\t')
836 {
837 /* The command is followed by whitespace; we need to
838 complete on whatever comes after command. */
839 if (c->prefixlist)
840 {
841 /* It is a prefix command; what comes after it is
842 a subcommand (e.g. "info "). */
843 if (reason != handle_brkchars)
844 list = complete_on_cmdlist (*c->prefixlist, p, word,
845 ignore_help_classes);
846
847 /* Ensure that readline does the right thing
848 with respect to inserting quotes. */
849 rl_completer_word_break_characters =
850 gdb_completer_command_word_break_characters;
851 }
852 else if (reason == handle_help)
853 list = NULL;
854 else if (c->enums)
855 {
856 if (reason != handle_brkchars)
857 list = complete_on_enum (c->enums, p, word);
858 rl_completer_word_break_characters =
859 gdb_completer_command_word_break_characters;
860 }
861 else
862 {
863 /* It is a normal command; what comes after it is
864 completed by the command's completer function. */
865 if (c->completer == filename_completer)
866 {
867 /* Many commands which want to complete on
868 file names accept several file names, as
869 in "run foo bar >>baz". So we don't want
870 to complete the entire text after the
871 command, just the last word. To this
872 end, we need to find the beginning of the
873 file name by starting at `word' and going
874 backwards. */
875 for (p = word;
876 p > tmp_command
877 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
878 p--)
879 ;
880 rl_completer_word_break_characters =
881 gdb_completer_file_name_break_characters;
882 }
883 if (reason == handle_brkchars
884 && c->completer_handle_brkchars != NULL)
885 (*c->completer_handle_brkchars) (c, p, word);
886 if (reason != handle_brkchars && c->completer != NULL)
887 list = (*c->completer) (c, p, word);
888 }
889 }
890 else
891 {
892 /* The command is not followed by whitespace; we need to
893 complete on the command itself, e.g. "p" which is a
894 command itself but also can complete to "print", "ptype"
895 etc. */
896 const char *q;
897
898 /* Find the command we are completing on. */
899 q = p;
900 while (q > tmp_command)
901 {
902 if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
903 --q;
904 else
905 break;
906 }
907
908 if (reason != handle_brkchars)
909 list = complete_on_cmdlist (result_list, q, word,
910 ignore_help_classes);
911
912 /* Ensure that readline does the right thing
913 with respect to inserting quotes. */
914 rl_completer_word_break_characters =
915 gdb_completer_command_word_break_characters;
916 }
917 }
918 else if (reason == handle_help)
919 list = NULL;
920 else
921 {
922 /* There is non-whitespace beyond the command. */
923
924 if (c->prefixlist && !c->allow_unknown)
925 {
926 /* It is an unrecognized subcommand of a prefix command,
927 e.g. "info adsfkdj". */
928 list = NULL;
929 }
930 else if (c->enums)
931 {
932 if (reason != handle_brkchars)
933 list = complete_on_enum (c->enums, p, word);
934 }
935 else
936 {
937 /* It is a normal command. */
938 if (c->completer == filename_completer)
939 {
940 /* See the commentary above about the specifics
941 of file-name completion. */
942 for (p = word;
943 p > tmp_command
944 && strchr (gdb_completer_file_name_break_characters,
945 p[-1]) == NULL;
946 p--)
947 ;
948 rl_completer_word_break_characters =
949 gdb_completer_file_name_break_characters;
950 }
951 if (reason == handle_brkchars
952 && c->completer_handle_brkchars != NULL)
953 (*c->completer_handle_brkchars) (c, p, word);
954 if (reason != handle_brkchars && c->completer != NULL)
955 list = (*c->completer) (c, p, word);
956 }
957 }
958 }
959
960 return list;
961 }
962
963 /* See completer.h. */
964
965 int max_completions = 200;
966
967 /* See completer.h. */
968
969 completion_tracker_t
970 new_completion_tracker (void)
971 {
972 if (max_completions <= 0)
973 return NULL;
974
975 return htab_create_alloc (max_completions,
976 htab_hash_string, (htab_eq) streq,
977 NULL, xcalloc, xfree);
978 }
979
980 /* Cleanup routine to free a completion tracker and reset the pointer
981 to NULL. */
982
983 static void
984 free_completion_tracker (void *p)
985 {
986 completion_tracker_t *tracker_ptr = p;
987
988 htab_delete (*tracker_ptr);
989 *tracker_ptr = NULL;
990 }
991
992 /* See completer.h. */
993
994 struct cleanup *
995 make_cleanup_free_completion_tracker (completion_tracker_t *tracker_ptr)
996 {
997 if (*tracker_ptr == NULL)
998 return make_cleanup (null_cleanup, NULL);
999
1000 return make_cleanup (free_completion_tracker, tracker_ptr);
1001 }
1002
1003 /* See completer.h. */
1004
1005 enum maybe_add_completion_enum
1006 maybe_add_completion (completion_tracker_t tracker, char *name)
1007 {
1008 void **slot;
1009
1010 if (max_completions < 0)
1011 return MAYBE_ADD_COMPLETION_OK;
1012 if (max_completions == 0)
1013 return MAYBE_ADD_COMPLETION_MAX_REACHED;
1014
1015 gdb_assert (tracker != NULL);
1016
1017 if (htab_elements (tracker) >= max_completions)
1018 return MAYBE_ADD_COMPLETION_MAX_REACHED;
1019
1020 slot = htab_find_slot (tracker, name, INSERT);
1021
1022 if (*slot != HTAB_EMPTY_ENTRY)
1023 return MAYBE_ADD_COMPLETION_DUPLICATE;
1024
1025 *slot = name;
1026
1027 return (htab_elements (tracker) < max_completions
1028 ? MAYBE_ADD_COMPLETION_OK
1029 : MAYBE_ADD_COMPLETION_OK_MAX_REACHED);
1030 }
1031
1032 void
1033 throw_max_completions_reached_error (void)
1034 {
1035 throw_error (MAX_COMPLETIONS_REACHED_ERROR, _("Max completions reached."));
1036 }
1037
1038 /* Generate completions all at once. Returns a vector of unique strings
1039 allocated with xmalloc. Returns NULL if there are no completions
1040 or if max_completions is 0. If max_completions is non-negative, this will
1041 return at most max_completions strings.
1042
1043 TEXT is the caller's idea of the "word" we are looking at.
1044
1045 LINE_BUFFER is available to be looked at; it contains the entire
1046 text of the line.
1047
1048 POINT is the offset in that line of the cursor. You
1049 should pretend that the line ends at POINT. */
1050
1051 VEC (char_ptr) *
1052 complete_line (const char *text, const char *line_buffer, int point)
1053 {
1054 VEC (char_ptr) *list;
1055 VEC (char_ptr) *result = NULL;
1056 struct cleanup *cleanups;
1057 completion_tracker_t tracker;
1058 char *candidate;
1059 int ix, max_reached;
1060
1061 if (max_completions == 0)
1062 return NULL;
1063 list = complete_line_internal (text, line_buffer, point,
1064 handle_completions);
1065 if (max_completions < 0)
1066 return list;
1067
1068 tracker = new_completion_tracker ();
1069 cleanups = make_cleanup_free_completion_tracker (&tracker);
1070 make_cleanup_free_char_ptr_vec (list);
1071
1072 /* Do a final test for too many completions. Individual completers may
1073 do some of this, but are not required to. Duplicates are also removed
1074 here. Otherwise the user is left scratching his/her head: readline and
1075 complete_command will remove duplicates, and if removal of duplicates
1076 there brings the total under max_completions the user may think gdb quit
1077 searching too early. */
1078
1079 for (ix = 0, max_reached = 0;
1080 !max_reached && VEC_iterate (char_ptr, list, ix, candidate);
1081 ++ix)
1082 {
1083 enum maybe_add_completion_enum add_status;
1084
1085 add_status = maybe_add_completion (tracker, candidate);
1086
1087 switch (add_status)
1088 {
1089 case MAYBE_ADD_COMPLETION_OK:
1090 VEC_safe_push (char_ptr, result, xstrdup (candidate));
1091 break;
1092 case MAYBE_ADD_COMPLETION_OK_MAX_REACHED:
1093 VEC_safe_push (char_ptr, result, xstrdup (candidate));
1094 max_reached = 1;
1095 break;
1096 case MAYBE_ADD_COMPLETION_MAX_REACHED:
1097 gdb_assert_not_reached ("more than max completions reached");
1098 case MAYBE_ADD_COMPLETION_DUPLICATE:
1099 break;
1100 }
1101 }
1102
1103 do_cleanups (cleanups);
1104
1105 return result;
1106 }
1107
1108 /* Complete on command names. Used by "help". */
1109 VEC (char_ptr) *
1110 command_completer (struct cmd_list_element *ignore,
1111 const char *text, const char *word)
1112 {
1113 return complete_line_internal (word, text,
1114 strlen (text), handle_help);
1115 }
1116
1117 /* Complete on signals. */
1118
1119 VEC (char_ptr) *
1120 signal_completer (struct cmd_list_element *ignore,
1121 const char *text, const char *word)
1122 {
1123 VEC (char_ptr) *return_val = NULL;
1124 size_t len = strlen (word);
1125 int signum;
1126 const char *signame;
1127
1128 for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum)
1129 {
1130 /* Can't handle this, so skip it. */
1131 if (signum == GDB_SIGNAL_0)
1132 continue;
1133
1134 signame = gdb_signal_to_name ((enum gdb_signal) signum);
1135
1136 /* Ignore the unknown signal case. */
1137 if (!signame || strcmp (signame, "?") == 0)
1138 continue;
1139
1140 if (strncasecmp (signame, word, len) == 0)
1141 VEC_safe_push (char_ptr, return_val, xstrdup (signame));
1142 }
1143
1144 return return_val;
1145 }
1146
1147 /* Bit-flags for selecting what the register and/or register-group
1148 completer should complete on. */
1149
1150 enum reg_completer_targets
1151 {
1152 complete_register_names = 0x1,
1153 complete_reggroup_names = 0x2
1154 };
1155
1156 /* Complete register names and/or reggroup names based on the value passed
1157 in TARGETS. At least one bit in TARGETS must be set. */
1158
1159 static VEC (char_ptr) *
1160 reg_or_group_completer_1 (struct cmd_list_element *ignore,
1161 const char *text, const char *word,
1162 enum reg_completer_targets targets)
1163 {
1164 VEC (char_ptr) *result = NULL;
1165 size_t len = strlen (word);
1166 struct gdbarch *gdbarch;
1167 const char *name;
1168
1169 gdb_assert ((targets & (complete_register_names
1170 | complete_reggroup_names)) != 0);
1171 gdbarch = get_current_arch ();
1172
1173 if ((targets & complete_register_names) != 0)
1174 {
1175 int i;
1176
1177 for (i = 0;
1178 (name = user_reg_map_regnum_to_name (gdbarch, i)) != NULL;
1179 i++)
1180 {
1181 if (*name != '\0' && strncmp (word, name, len) == 0)
1182 VEC_safe_push (char_ptr, result, xstrdup (name));
1183 }
1184 }
1185
1186 if ((targets & complete_reggroup_names) != 0)
1187 {
1188 struct reggroup *group;
1189
1190 for (group = reggroup_next (gdbarch, NULL);
1191 group != NULL;
1192 group = reggroup_next (gdbarch, group))
1193 {
1194 name = reggroup_name (group);
1195 if (strncmp (word, name, len) == 0)
1196 VEC_safe_push (char_ptr, result, xstrdup (name));
1197 }
1198 }
1199
1200 return result;
1201 }
1202
1203 /* Perform completion on register and reggroup names. */
1204
1205 VEC (char_ptr) *
1206 reg_or_group_completer (struct cmd_list_element *ignore,
1207 const char *text, const char *word)
1208 {
1209 return reg_or_group_completer_1 (ignore, text, word,
1210 (complete_register_names
1211 | complete_reggroup_names));
1212 }
1213
1214 /* Perform completion on reggroup names. */
1215
1216 VEC (char_ptr) *
1217 reggroup_completer (struct cmd_list_element *ignore,
1218 const char *text, const char *word)
1219 {
1220 return reg_or_group_completer_1 (ignore, text, word,
1221 complete_reggroup_names);
1222 }
1223
1224 /* Get the list of chars that are considered as word breaks
1225 for the current command. */
1226
1227 char *
1228 gdb_completion_word_break_characters (void)
1229 {
1230 VEC (char_ptr) *list;
1231
1232 list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point,
1233 handle_brkchars);
1234 gdb_assert (list == NULL);
1235 return rl_completer_word_break_characters;
1236 }
1237
1238 /* Generate completions one by one for the completer. Each time we
1239 are called return another potential completion to the caller.
1240 line_completion just completes on commands or passes the buck to
1241 the command's completer function, the stuff specific to symbol
1242 completion is in make_symbol_completion_list.
1243
1244 TEXT is the caller's idea of the "word" we are looking at.
1245
1246 MATCHES is the number of matches that have currently been collected
1247 from calling this completion function. When zero, then we need to
1248 initialize, otherwise the initialization has already taken place
1249 and we can just return the next potential completion string.
1250
1251 LINE_BUFFER is available to be looked at; it contains the entire
1252 text of the line. POINT is the offset in that line of the cursor.
1253 You should pretend that the line ends at POINT.
1254
1255 Returns NULL if there are no more completions, else a pointer to a
1256 string which is a possible completion, it is the caller's
1257 responsibility to free the string. */
1258
1259 static char *
1260 line_completion_function (const char *text, int matches,
1261 char *line_buffer, int point)
1262 {
1263 static VEC (char_ptr) *list = NULL; /* Cache of completions. */
1264 static int index; /* Next cached completion. */
1265 char *output = NULL;
1266
1267 if (matches == 0)
1268 {
1269 /* The caller is beginning to accumulate a new set of
1270 completions, so we need to find all of them now, and cache
1271 them for returning one at a time on future calls. */
1272
1273 if (list)
1274 {
1275 /* Free the storage used by LIST, but not by the strings
1276 inside. This is because rl_complete_internal () frees
1277 the strings. As complete_line may abort by calling
1278 `error' clear LIST now. */
1279 VEC_free (char_ptr, list);
1280 }
1281 index = 0;
1282 list = complete_line (text, line_buffer, point);
1283 }
1284
1285 /* If we found a list of potential completions during initialization
1286 then dole them out one at a time. After returning the last one,
1287 return NULL (and continue to do so) each time we are called after
1288 that, until a new list is available. */
1289
1290 if (list)
1291 {
1292 if (index < VEC_length (char_ptr, list))
1293 {
1294 output = VEC_index (char_ptr, list, index);
1295 index++;
1296 }
1297 }
1298
1299 #if 0
1300 /* Can't do this because readline hasn't yet checked the word breaks
1301 for figuring out whether to insert a quote. */
1302 if (output == NULL)
1303 /* Make sure the word break characters are set back to normal for
1304 the next time that readline tries to complete something. */
1305 rl_completer_word_break_characters =
1306 current_language->la_word_break_characters();
1307 #endif
1308
1309 return (output);
1310 }
1311
1312 /* Skip over the possibly quoted word STR (as defined by the quote
1313 characters QUOTECHARS and the word break characters BREAKCHARS).
1314 Returns pointer to the location after the "word". If either
1315 QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
1316 completer. */
1317
1318 const char *
1319 skip_quoted_chars (const char *str, const char *quotechars,
1320 const char *breakchars)
1321 {
1322 char quote_char = '\0';
1323 const char *scan;
1324
1325 if (quotechars == NULL)
1326 quotechars = gdb_completer_quote_characters;
1327
1328 if (breakchars == NULL)
1329 breakchars = current_language->la_word_break_characters();
1330
1331 for (scan = str; *scan != '\0'; scan++)
1332 {
1333 if (quote_char != '\0')
1334 {
1335 /* Ignore everything until the matching close quote char. */
1336 if (*scan == quote_char)
1337 {
1338 /* Found matching close quote. */
1339 scan++;
1340 break;
1341 }
1342 }
1343 else if (strchr (quotechars, *scan))
1344 {
1345 /* Found start of a quoted string. */
1346 quote_char = *scan;
1347 }
1348 else if (strchr (breakchars, *scan))
1349 {
1350 break;
1351 }
1352 }
1353
1354 return (scan);
1355 }
1356
1357 /* Skip over the possibly quoted word STR (as defined by the quote
1358 characters and word break characters used by the completer).
1359 Returns pointer to the location after the "word". */
1360
1361 const char *
1362 skip_quoted (const char *str)
1363 {
1364 return skip_quoted_chars (str, NULL, NULL);
1365 }
1366
1367 /* Return a message indicating that the maximum number of completions
1368 has been reached and that there may be more. */
1369
1370 const char *
1371 get_max_completions_reached_message (void)
1372 {
1373 return _("*** List may be truncated, max-completions reached. ***");
1374 }
1375 \f
1376 /* GDB replacement for rl_display_match_list.
1377 Readline doesn't provide a clean interface for TUI(curses).
1378 A hack previously used was to send readline's rl_outstream through a pipe
1379 and read it from the event loop. Bleah. IWBN if readline abstracted
1380 away all the necessary bits, and this is what this code does. It
1381 replicates the parts of readline we need and then adds an abstraction
1382 layer, currently implemented as struct match_list_displayer, so that both
1383 CLI and TUI can use it. We copy all this readline code to minimize
1384 GDB-specific mods to readline. Once this code performs as desired then
1385 we can submit it to the readline maintainers.
1386
1387 N.B. A lot of the code is the way it is in order to minimize differences
1388 from readline's copy. */
1389
1390 /* Not supported here. */
1391 #undef VISIBLE_STATS
1392
1393 #if defined (HANDLE_MULTIBYTE)
1394 #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
1395 #define MB_NULLWCH(x) ((x) == 0)
1396 #endif
1397
1398 #define ELLIPSIS_LEN 3
1399
1400 /* gdb version of readline/complete.c:get_y_or_n.
1401 'y' -> returns 1, and 'n' -> returns 0.
1402 Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
1403 If FOR_PAGER is non-zero, then also supported are:
1404 NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
1405
1406 static int
1407 gdb_get_y_or_n (int for_pager, const struct match_list_displayer *displayer)
1408 {
1409 int c;
1410
1411 for (;;)
1412 {
1413 RL_SETSTATE (RL_STATE_MOREINPUT);
1414 c = displayer->read_key (displayer);
1415 RL_UNSETSTATE (RL_STATE_MOREINPUT);
1416
1417 if (c == 'y' || c == 'Y' || c == ' ')
1418 return 1;
1419 if (c == 'n' || c == 'N' || c == RUBOUT)
1420 return 0;
1421 if (c == ABORT_CHAR || c < 0)
1422 {
1423 /* Readline doesn't erase_entire_line here, but without it the
1424 --More-- prompt isn't erased and neither is the text entered
1425 thus far redisplayed. */
1426 displayer->erase_entire_line (displayer);
1427 /* Note: The arguments to rl_abort are ignored. */
1428 rl_abort (0, 0);
1429 }
1430 if (for_pager && (c == NEWLINE || c == RETURN))
1431 return 2;
1432 if (for_pager && (c == 'q' || c == 'Q'))
1433 return 0;
1434 displayer->beep (displayer);
1435 }
1436 }
1437
1438 /* Pager function for tab-completion.
1439 This is based on readline/complete.c:_rl_internal_pager.
1440 LINES is the number of lines of output displayed thus far.
1441 Returns:
1442 -1 -> user pressed 'n' or equivalent,
1443 0 -> user pressed 'y' or equivalent,
1444 N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
1445
1446 static int
1447 gdb_display_match_list_pager (int lines,
1448 const struct match_list_displayer *displayer)
1449 {
1450 int i;
1451
1452 displayer->puts (displayer, "--More--");
1453 displayer->flush (displayer);
1454 i = gdb_get_y_or_n (1, displayer);
1455 displayer->erase_entire_line (displayer);
1456 if (i == 0)
1457 return -1;
1458 else if (i == 2)
1459 return (lines - 1);
1460 else
1461 return 0;
1462 }
1463
1464 /* Return non-zero if FILENAME is a directory.
1465 Based on readline/complete.c:path_isdir. */
1466
1467 static int
1468 gdb_path_isdir (const char *filename)
1469 {
1470 struct stat finfo;
1471
1472 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
1473 }
1474
1475 /* Return the portion of PATHNAME that should be output when listing
1476 possible completions. If we are hacking filename completion, we
1477 are only interested in the basename, the portion following the
1478 final slash. Otherwise, we return what we were passed. Since
1479 printing empty strings is not very informative, if we're doing
1480 filename completion, and the basename is the empty string, we look
1481 for the previous slash and return the portion following that. If
1482 there's no previous slash, we just return what we were passed.
1483
1484 Based on readline/complete.c:printable_part. */
1485
1486 static char *
1487 gdb_printable_part (char *pathname)
1488 {
1489 char *temp, *x;
1490
1491 if (rl_filename_completion_desired == 0) /* don't need to do anything */
1492 return (pathname);
1493
1494 temp = strrchr (pathname, '/');
1495 #if defined (__MSDOS__)
1496 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
1497 temp = pathname + 1;
1498 #endif
1499
1500 if (temp == 0 || *temp == '\0')
1501 return (pathname);
1502 /* If the basename is NULL, we might have a pathname like '/usr/src/'.
1503 Look for a previous slash and, if one is found, return the portion
1504 following that slash. If there's no previous slash, just return the
1505 pathname we were passed. */
1506 else if (temp[1] == '\0')
1507 {
1508 for (x = temp - 1; x > pathname; x--)
1509 if (*x == '/')
1510 break;
1511 return ((*x == '/') ? x + 1 : pathname);
1512 }
1513 else
1514 return ++temp;
1515 }
1516
1517 /* Compute width of STRING when displayed on screen by print_filename.
1518 Based on readline/complete.c:fnwidth. */
1519
1520 static int
1521 gdb_fnwidth (const char *string)
1522 {
1523 int width, pos;
1524 #if defined (HANDLE_MULTIBYTE)
1525 mbstate_t ps;
1526 int left, w;
1527 size_t clen;
1528 wchar_t wc;
1529
1530 left = strlen (string) + 1;
1531 memset (&ps, 0, sizeof (mbstate_t));
1532 #endif
1533
1534 width = pos = 0;
1535 while (string[pos])
1536 {
1537 if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
1538 {
1539 width += 2;
1540 pos++;
1541 }
1542 else
1543 {
1544 #if defined (HANDLE_MULTIBYTE)
1545 clen = mbrtowc (&wc, string + pos, left - pos, &ps);
1546 if (MB_INVALIDCH (clen))
1547 {
1548 width++;
1549 pos++;
1550 memset (&ps, 0, sizeof (mbstate_t));
1551 }
1552 else if (MB_NULLWCH (clen))
1553 break;
1554 else
1555 {
1556 pos += clen;
1557 w = wcwidth (wc);
1558 width += (w >= 0) ? w : 1;
1559 }
1560 #else
1561 width++;
1562 pos++;
1563 #endif
1564 }
1565 }
1566
1567 return width;
1568 }
1569
1570 /* Print TO_PRINT, one matching completion.
1571 PREFIX_BYTES is number of common prefix bytes.
1572 Based on readline/complete.c:fnprint. */
1573
1574 static int
1575 gdb_fnprint (const char *to_print, int prefix_bytes,
1576 const struct match_list_displayer *displayer)
1577 {
1578 int printed_len, w;
1579 const char *s;
1580 #if defined (HANDLE_MULTIBYTE)
1581 mbstate_t ps;
1582 const char *end;
1583 size_t tlen;
1584 int width;
1585 wchar_t wc;
1586
1587 end = to_print + strlen (to_print) + 1;
1588 memset (&ps, 0, sizeof (mbstate_t));
1589 #endif
1590
1591 printed_len = 0;
1592
1593 /* Don't print only the ellipsis if the common prefix is one of the
1594 possible completions */
1595 if (to_print[prefix_bytes] == '\0')
1596 prefix_bytes = 0;
1597
1598 if (prefix_bytes)
1599 {
1600 char ellipsis;
1601
1602 ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
1603 for (w = 0; w < ELLIPSIS_LEN; w++)
1604 displayer->putch (displayer, ellipsis);
1605 printed_len = ELLIPSIS_LEN;
1606 }
1607
1608 s = to_print + prefix_bytes;
1609 while (*s)
1610 {
1611 if (CTRL_CHAR (*s))
1612 {
1613 displayer->putch (displayer, '^');
1614 displayer->putch (displayer, UNCTRL (*s));
1615 printed_len += 2;
1616 s++;
1617 #if defined (HANDLE_MULTIBYTE)
1618 memset (&ps, 0, sizeof (mbstate_t));
1619 #endif
1620 }
1621 else if (*s == RUBOUT)
1622 {
1623 displayer->putch (displayer, '^');
1624 displayer->putch (displayer, '?');
1625 printed_len += 2;
1626 s++;
1627 #if defined (HANDLE_MULTIBYTE)
1628 memset (&ps, 0, sizeof (mbstate_t));
1629 #endif
1630 }
1631 else
1632 {
1633 #if defined (HANDLE_MULTIBYTE)
1634 tlen = mbrtowc (&wc, s, end - s, &ps);
1635 if (MB_INVALIDCH (tlen))
1636 {
1637 tlen = 1;
1638 width = 1;
1639 memset (&ps, 0, sizeof (mbstate_t));
1640 }
1641 else if (MB_NULLWCH (tlen))
1642 break;
1643 else
1644 {
1645 w = wcwidth (wc);
1646 width = (w >= 0) ? w : 1;
1647 }
1648 for (w = 0; w < tlen; ++w)
1649 displayer->putch (displayer, s[w]);
1650 s += tlen;
1651 printed_len += width;
1652 #else
1653 displayer->putch (displayer, *s);
1654 s++;
1655 printed_len++;
1656 #endif
1657 }
1658 }
1659
1660 return printed_len;
1661 }
1662
1663 /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
1664 are using it, check for and output a single character for `special'
1665 filenames. Return the number of characters we output.
1666 Based on readline/complete.c:print_filename. */
1667
1668 static int
1669 gdb_print_filename (char *to_print, char *full_pathname, int prefix_bytes,
1670 const struct match_list_displayer *displayer)
1671 {
1672 int printed_len, extension_char, slen, tlen;
1673 char *s, c, *new_full_pathname, *dn;
1674 extern int _rl_complete_mark_directories;
1675
1676 extension_char = 0;
1677 printed_len = gdb_fnprint (to_print, prefix_bytes, displayer);
1678
1679 #if defined (VISIBLE_STATS)
1680 if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories))
1681 #else
1682 if (rl_filename_completion_desired && _rl_complete_mark_directories)
1683 #endif
1684 {
1685 /* If to_print != full_pathname, to_print is the basename of the
1686 path passed. In this case, we try to expand the directory
1687 name before checking for the stat character. */
1688 if (to_print != full_pathname)
1689 {
1690 /* Terminate the directory name. */
1691 c = to_print[-1];
1692 to_print[-1] = '\0';
1693
1694 /* If setting the last slash in full_pathname to a NUL results in
1695 full_pathname being the empty string, we are trying to complete
1696 files in the root directory. If we pass a null string to the
1697 bash directory completion hook, for example, it will expand it
1698 to the current directory. We just want the `/'. */
1699 if (full_pathname == 0 || *full_pathname == 0)
1700 dn = "/";
1701 else if (full_pathname[0] != '/')
1702 dn = full_pathname;
1703 else if (full_pathname[1] == 0)
1704 dn = "//"; /* restore trailing slash to `//' */
1705 else if (full_pathname[1] == '/' && full_pathname[2] == 0)
1706 dn = "/"; /* don't turn /// into // */
1707 else
1708 dn = full_pathname;
1709 s = tilde_expand (dn);
1710 if (rl_directory_completion_hook)
1711 (*rl_directory_completion_hook) (&s);
1712
1713 slen = strlen (s);
1714 tlen = strlen (to_print);
1715 new_full_pathname = (char *)xmalloc (slen + tlen + 2);
1716 strcpy (new_full_pathname, s);
1717 if (s[slen - 1] == '/')
1718 slen--;
1719 else
1720 new_full_pathname[slen] = '/';
1721 new_full_pathname[slen] = '/';
1722 strcpy (new_full_pathname + slen + 1, to_print);
1723
1724 #if defined (VISIBLE_STATS)
1725 if (rl_visible_stats)
1726 extension_char = stat_char (new_full_pathname);
1727 else
1728 #endif
1729 if (gdb_path_isdir (new_full_pathname))
1730 extension_char = '/';
1731
1732 xfree (new_full_pathname);
1733 to_print[-1] = c;
1734 }
1735 else
1736 {
1737 s = tilde_expand (full_pathname);
1738 #if defined (VISIBLE_STATS)
1739 if (rl_visible_stats)
1740 extension_char = stat_char (s);
1741 else
1742 #endif
1743 if (gdb_path_isdir (s))
1744 extension_char = '/';
1745 }
1746
1747 xfree (s);
1748 if (extension_char)
1749 {
1750 displayer->putch (displayer, extension_char);
1751 printed_len++;
1752 }
1753 }
1754
1755 return printed_len;
1756 }
1757
1758 /* GDB version of readline/complete.c:complete_get_screenwidth. */
1759
1760 static int
1761 gdb_complete_get_screenwidth (const struct match_list_displayer *displayer)
1762 {
1763 /* Readline has other stuff here which it's not clear we need. */
1764 return displayer->width;
1765 }
1766
1767 extern int _rl_completion_prefix_display_length;
1768 extern int _rl_print_completions_horizontally;
1769
1770 EXTERN_C int _rl_qsort_string_compare (const void *, const void *);
1771 typedef int QSFUNC (const void *, const void *);
1772
1773 /* GDB version of readline/complete.c:rl_display_match_list.
1774 See gdb_display_match_list for a description of MATCHES, LEN, MAX.
1775 Returns non-zero if all matches are displayed. */
1776
1777 static int
1778 gdb_display_match_list_1 (char **matches, int len, int max,
1779 const struct match_list_displayer *displayer)
1780 {
1781 int count, limit, printed_len, lines, cols;
1782 int i, j, k, l, common_length, sind;
1783 char *temp, *t;
1784 int page_completions = displayer->height != INT_MAX && pagination_enabled;
1785
1786 /* Find the length of the prefix common to all items: length as displayed
1787 characters (common_length) and as a byte index into the matches (sind) */
1788 common_length = sind = 0;
1789 if (_rl_completion_prefix_display_length > 0)
1790 {
1791 t = gdb_printable_part (matches[0]);
1792 temp = strrchr (t, '/');
1793 common_length = temp ? gdb_fnwidth (temp) : gdb_fnwidth (t);
1794 sind = temp ? strlen (temp) : strlen (t);
1795
1796 if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
1797 max -= common_length - ELLIPSIS_LEN;
1798 else
1799 common_length = sind = 0;
1800 }
1801
1802 /* How many items of MAX length can we fit in the screen window? */
1803 cols = gdb_complete_get_screenwidth (displayer);
1804 max += 2;
1805 limit = cols / max;
1806 if (limit != 1 && (limit * max == cols))
1807 limit--;
1808
1809 /* If cols == 0, limit will end up -1 */
1810 if (cols < displayer->width && limit < 0)
1811 limit = 1;
1812
1813 /* Avoid a possible floating exception. If max > cols,
1814 limit will be 0 and a divide-by-zero fault will result. */
1815 if (limit == 0)
1816 limit = 1;
1817
1818 /* How many iterations of the printing loop? */
1819 count = (len + (limit - 1)) / limit;
1820
1821 /* Watch out for special case. If LEN is less than LIMIT, then
1822 just do the inner printing loop.
1823 0 < len <= limit implies count = 1. */
1824
1825 /* Sort the items if they are not already sorted. */
1826 if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches)
1827 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
1828
1829 displayer->crlf (displayer);
1830
1831 lines = 0;
1832 if (_rl_print_completions_horizontally == 0)
1833 {
1834 /* Print the sorted items, up-and-down alphabetically, like ls. */
1835 for (i = 1; i <= count; i++)
1836 {
1837 for (j = 0, l = i; j < limit; j++)
1838 {
1839 if (l > len || matches[l] == 0)
1840 break;
1841 else
1842 {
1843 temp = gdb_printable_part (matches[l]);
1844 printed_len = gdb_print_filename (temp, matches[l], sind,
1845 displayer);
1846
1847 if (j + 1 < limit)
1848 for (k = 0; k < max - printed_len; k++)
1849 displayer->putch (displayer, ' ');
1850 }
1851 l += count;
1852 }
1853 displayer->crlf (displayer);
1854 lines++;
1855 if (page_completions && lines >= (displayer->height - 1) && i < count)
1856 {
1857 lines = gdb_display_match_list_pager (lines, displayer);
1858 if (lines < 0)
1859 return 0;
1860 }
1861 }
1862 }
1863 else
1864 {
1865 /* Print the sorted items, across alphabetically, like ls -x. */
1866 for (i = 1; matches[i]; i++)
1867 {
1868 temp = gdb_printable_part (matches[i]);
1869 printed_len = gdb_print_filename (temp, matches[i], sind, displayer);
1870 /* Have we reached the end of this line? */
1871 if (matches[i+1])
1872 {
1873 if (i && (limit > 1) && (i % limit) == 0)
1874 {
1875 displayer->crlf (displayer);
1876 lines++;
1877 if (page_completions && lines >= displayer->height - 1)
1878 {
1879 lines = gdb_display_match_list_pager (lines, displayer);
1880 if (lines < 0)
1881 return 0;
1882 }
1883 }
1884 else
1885 for (k = 0; k < max - printed_len; k++)
1886 displayer->putch (displayer, ' ');
1887 }
1888 }
1889 displayer->crlf (displayer);
1890 }
1891
1892 return 1;
1893 }
1894
1895 /* Utility for displaying completion list matches, used by both CLI and TUI.
1896
1897 MATCHES is the list of strings, in argv format, LEN is the number of
1898 strings in MATCHES, and MAX is the length of the longest string in
1899 MATCHES. */
1900
1901 void
1902 gdb_display_match_list (char **matches, int len, int max,
1903 const struct match_list_displayer *displayer)
1904 {
1905 /* Readline will never call this if complete_line returned NULL. */
1906 gdb_assert (max_completions != 0);
1907
1908 /* complete_line will never return more than this. */
1909 if (max_completions > 0)
1910 gdb_assert (len <= max_completions);
1911
1912 if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
1913 {
1914 char msg[100];
1915
1916 /* We can't use *query here because they wait for <RET> which is
1917 wrong here. This follows the readline version as closely as possible
1918 for compatibility's sake. See readline/complete.c. */
1919
1920 displayer->crlf (displayer);
1921
1922 xsnprintf (msg, sizeof (msg),
1923 "Display all %d possibilities? (y or n)", len);
1924 displayer->puts (displayer, msg);
1925 displayer->flush (displayer);
1926
1927 if (gdb_get_y_or_n (0, displayer) == 0)
1928 {
1929 displayer->crlf (displayer);
1930 return;
1931 }
1932 }
1933
1934 if (gdb_display_match_list_1 (matches, len, max, displayer))
1935 {
1936 /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */
1937 if (len == max_completions)
1938 {
1939 /* The maximum number of completions has been reached. Warn the user
1940 that there may be more. */
1941 const char *message = get_max_completions_reached_message ();
1942
1943 displayer->puts (displayer, message);
1944 displayer->crlf (displayer);
1945 }
1946 }
1947 }
1948 \f
1949 extern initialize_file_ftype _initialize_completer; /* -Wmissing-prototypes */
1950
1951 void
1952 _initialize_completer (void)
1953 {
1954 add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class,
1955 &max_completions, _("\
1956 Set maximum number of completion candidates."), _("\
1957 Show maximum number of completion candidates."), _("\
1958 Use this to limit the number of candidates considered\n\
1959 during completion. Specifying \"unlimited\" or -1\n\
1960 disables limiting. Note that setting either no limit or\n\
1961 a very large limit can make completion slow."),
1962 NULL, NULL, &setlist, &showlist);
1963 }
This page took 0.118307 seconds and 4 git commands to generate.