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