198944Sobrien/* Line completion stuff for GDB, the GNU debugger.
298944Sobrien   Copyright 2000, 2001 Free Software Foundation, Inc.
398944Sobrien
498944Sobrien   This file is part of GDB.
598944Sobrien
698944Sobrien   This program is free software; you can redistribute it and/or modify
798944Sobrien   it under the terms of the GNU General Public License as published by
898944Sobrien   the Free Software Foundation; either version 2 of the License, or
998944Sobrien   (at your option) any later version.
1098944Sobrien
1198944Sobrien   This program is distributed in the hope that it will be useful,
1298944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1398944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1498944Sobrien   GNU General Public License for more details.
1598944Sobrien
1698944Sobrien   You should have received a copy of the GNU General Public License
1798944Sobrien   along with this program; if not, write to the Free Software
1898944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
1998944Sobrien   Boston, MA 02111-1307, USA.  */
2098944Sobrien
2198944Sobrien#include "defs.h"
2298944Sobrien#include "symtab.h"
2398944Sobrien#include "gdbtypes.h"
2498944Sobrien#include "expression.h"
2598944Sobrien#include "filenames.h"		/* for DOSish file names */
26130803Smarcel#include "language.h"
2798944Sobrien
28130803Smarcel#include "cli/cli-decode.h"
29130803Smarcel
3098944Sobrien/* FIXME: This is needed because of lookup_cmd_1().
3198944Sobrien   We should be calling a hook instead so we eliminate the CLI dependency. */
3298944Sobrien#include "gdbcmd.h"
3398944Sobrien
3498944Sobrien/* Needed for rl_completer_word_break_characters() and for
35130803Smarcel   rl_filename_completion_function.  */
36130803Smarcel#include "readline/readline.h"
3798944Sobrien
3898944Sobrien/* readline defines this.  */
3998944Sobrien#undef savestring
4098944Sobrien
4198944Sobrien#include "completer.h"
4298944Sobrien
4398944Sobrien/* Prototypes for local functions */
44130803Smarcelstatic
45130803Smarcelchar *line_completion_function (const char *text, int matches, char *line_buffer,
4698944Sobrien				int point);
4798944Sobrien
4898944Sobrien/* readline uses the word breaks for two things:
4998944Sobrien   (1) In figuring out where to point the TEXT parameter to the
5098944Sobrien   rl_completion_entry_function.  Since we don't use TEXT for much,
5198944Sobrien   it doesn't matter a lot what the word breaks are for this purpose, but
5298944Sobrien   it does affect how much stuff M-? lists.
5398944Sobrien   (2) If one of the matches contains a word break character, readline
5498944Sobrien   will quote it.  That's why we switch between
55130803Smarcel   current_language->la_word_break_characters() and
5698944Sobrien   gdb_completer_command_word_break_characters.  I'm not sure when
5798944Sobrien   we need this behavior (perhaps for funky characters in C++ symbols?).  */
5898944Sobrien
5998944Sobrien/* Variables which are necessary for fancy command line editing.  */
6098944Sobrien
6198944Sobrien/* When completing on command names, we remove '-' from the list of
6298944Sobrien   word break characters, since we use it in command names.  If the
6398944Sobrien   readline library sees one in any of the current completion strings,
6498944Sobrien   it thinks that the string needs to be quoted and automatically supplies
6598944Sobrien   a leading quote. */
6698944Sobrienstatic char *gdb_completer_command_word_break_characters =
6798944Sobrien" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
6898944Sobrien
6998944Sobrien/* When completing on file names, we remove from the list of word
7098944Sobrien   break characters any characters that are commonly used in file
7198944Sobrien   names, such as '-', '+', '~', etc.  Otherwise, readline displays
7298944Sobrien   incorrect completion candidates.  */
7398944Sobrien#ifdef HAVE_DOS_BASED_FILE_SYSTEM
7498944Sobrien/* MS-DOS and MS-Windows use colon as part of the drive spec, and most
7598944Sobrien   programs support @foo style response files.  */
7698944Sobrienstatic char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
7798944Sobrien#else
7898944Sobrienstatic char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
7998944Sobrien#endif
8098944Sobrien
8198944Sobrien/* These are used when completing on locations, which can mix file
8298944Sobrien   names and symbol names separated by a colon.  */
8398944Sobrienstatic char *gdb_completer_loc_break_characters = " \t\n*|\"';:?><,";
8498944Sobrien
8598944Sobrien/* Characters that can be used to quote completion strings.  Note that we
8698944Sobrien   can't include '"' because the gdb C parser treats such quoted sequences
8798944Sobrien   as strings. */
8898944Sobrienstatic char *gdb_completer_quote_characters = "'";
8998944Sobrien
9098944Sobrien/* Accessor for some completer data that may interest other files. */
9198944Sobrien
9298944Sobrienchar *
9398944Sobrienget_gdb_completer_quote_characters (void)
9498944Sobrien{
9598944Sobrien  return gdb_completer_quote_characters;
9698944Sobrien}
9798944Sobrien
9898944Sobrien/* Line completion interface function for readline.  */
9998944Sobrien
10098944Sobrienchar *
101130803Smarcelreadline_line_completion_function (const char *text, int matches)
10298944Sobrien{
10398944Sobrien  return line_completion_function (text, matches, rl_line_buffer, rl_point);
10498944Sobrien}
10598944Sobrien
10698944Sobrien/* This can be used for functions which don't want to complete on symbols
10798944Sobrien   but don't want to complete on anything else either.  */
10898944Sobrienchar **
10998944Sobriennoop_completer (char *text, char *prefix)
11098944Sobrien{
11198944Sobrien  return NULL;
11298944Sobrien}
11398944Sobrien
11498944Sobrien/* Complete on filenames.  */
11598944Sobrienchar **
11698944Sobrienfilename_completer (char *text, char *word)
11798944Sobrien{
11898944Sobrien  int subsequent_name;
11998944Sobrien  char **return_val;
12098944Sobrien  int return_val_used;
12198944Sobrien  int return_val_alloced;
12298944Sobrien
12398944Sobrien  return_val_used = 0;
12498944Sobrien  /* Small for testing.  */
12598944Sobrien  return_val_alloced = 1;
12698944Sobrien  return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
12798944Sobrien
12898944Sobrien  subsequent_name = 0;
12998944Sobrien  while (1)
13098944Sobrien    {
13198944Sobrien      char *p;
132130803Smarcel      p = rl_filename_completion_function (text, subsequent_name);
13398944Sobrien      if (return_val_used >= return_val_alloced)
13498944Sobrien	{
13598944Sobrien	  return_val_alloced *= 2;
13698944Sobrien	  return_val =
13798944Sobrien	    (char **) xrealloc (return_val,
13898944Sobrien				return_val_alloced * sizeof (char *));
13998944Sobrien	}
14098944Sobrien      if (p == NULL)
14198944Sobrien	{
14298944Sobrien	  return_val[return_val_used++] = p;
14398944Sobrien	  break;
14498944Sobrien	}
14598944Sobrien      /* We need to set subsequent_name to a non-zero value before the
14698944Sobrien	 continue line below, because otherwise, if the first file seen
14798944Sobrien	 by GDB is a backup file whose name ends in a `~', we will loop
14898944Sobrien	 indefinitely.  */
14998944Sobrien      subsequent_name = 1;
15098944Sobrien      /* Like emacs, don't complete on old versions.  Especially useful
15198944Sobrien         in the "source" command.  */
15298944Sobrien      if (p[strlen (p) - 1] == '~')
15398944Sobrien	continue;
15498944Sobrien
15598944Sobrien      {
15698944Sobrien	char *q;
15798944Sobrien	if (word == text)
15898944Sobrien	  /* Return exactly p.  */
15998944Sobrien	  return_val[return_val_used++] = p;
16098944Sobrien	else if (word > text)
16198944Sobrien	  {
16298944Sobrien	    /* Return some portion of p.  */
16398944Sobrien	    q = xmalloc (strlen (p) + 5);
16498944Sobrien	    strcpy (q, p + (word - text));
16598944Sobrien	    return_val[return_val_used++] = q;
16698944Sobrien	    xfree (p);
16798944Sobrien	  }
16898944Sobrien	else
16998944Sobrien	  {
17098944Sobrien	    /* Return some of TEXT plus p.  */
17198944Sobrien	    q = xmalloc (strlen (p) + (text - word) + 5);
17298944Sobrien	    strncpy (q, word, text - word);
17398944Sobrien	    q[text - word] = '\0';
17498944Sobrien	    strcat (q, p);
17598944Sobrien	    return_val[return_val_used++] = q;
17698944Sobrien	    xfree (p);
17798944Sobrien	  }
17898944Sobrien      }
17998944Sobrien    }
18098944Sobrien#if 0
18198944Sobrien  /* There is no way to do this just long enough to affect quote inserting
18298944Sobrien     without also affecting the next completion.  This should be fixed in
18398944Sobrien     readline.  FIXME.  */
18498944Sobrien  /* Insure that readline does the right thing
18598944Sobrien     with respect to inserting quotes.  */
18698944Sobrien  rl_completer_word_break_characters = "";
18798944Sobrien#endif
18898944Sobrien  return return_val;
18998944Sobrien}
19098944Sobrien
19198944Sobrien/* Complete on locations, which might be of two possible forms:
19298944Sobrien
19398944Sobrien       file:line
19498944Sobrien   or
19598944Sobrien       symbol+offset
19698944Sobrien
19798944Sobrien   This is intended to be used in commands that set breakpoints etc.  */
19898944Sobrienchar **
19998944Sobrienlocation_completer (char *text, char *word)
20098944Sobrien{
20198944Sobrien  int n_syms = 0, n_files = 0;
20298944Sobrien  char ** fn_list = NULL;
20398944Sobrien  char ** list = NULL;
20498944Sobrien  char *p;
20598944Sobrien  int quote_found = 0;
20698944Sobrien  int quoted = *text == '\'' || *text == '"';
20798944Sobrien  int quote_char = '\0';
20898944Sobrien  char *colon = NULL;
20998944Sobrien  char *file_to_match = NULL;
21098944Sobrien  char *symbol_start = text;
21198944Sobrien  char *orig_text = text;
21298944Sobrien  size_t text_len;
21398944Sobrien
21498944Sobrien  /* Do we have an unquoted colon, as in "break foo.c::bar"?  */
21598944Sobrien  for (p = text; *p != '\0'; ++p)
21698944Sobrien    {
21798944Sobrien      if (*p == '\\' && p[1] == '\'')
21898944Sobrien	p++;
21998944Sobrien      else if (*p == '\'' || *p == '"')
22098944Sobrien	{
22198944Sobrien	  quote_found = *p;
22298944Sobrien	  quote_char = *p++;
22398944Sobrien	  while (*p != '\0' && *p != quote_found)
22498944Sobrien	    {
22598944Sobrien	      if (*p == '\\' && p[1] == quote_found)
22698944Sobrien		p++;
22798944Sobrien	      p++;
22898944Sobrien	    }
22998944Sobrien
23098944Sobrien	  if (*p == quote_found)
23198944Sobrien	    quote_found = 0;
23298944Sobrien	  else
23398944Sobrien	    break;		/* hit the end of text */
23498944Sobrien	}
23598944Sobrien#if HAVE_DOS_BASED_FILE_SYSTEM
23698944Sobrien      /* If we have a DOS-style absolute file name at the beginning of
23798944Sobrien	 TEXT, and the colon after the drive letter is the only colon
23898944Sobrien	 we found, pretend the colon is not there.  */
23998944Sobrien      else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
24098944Sobrien	;
24198944Sobrien#endif
24298944Sobrien      else if (*p == ':' && !colon)
24398944Sobrien	{
24498944Sobrien	  colon = p;
24598944Sobrien	  symbol_start = p + 1;
24698944Sobrien	}
247130803Smarcel      else if (strchr (current_language->la_word_break_characters(), *p))
24898944Sobrien	symbol_start = p + 1;
24998944Sobrien    }
25098944Sobrien
25198944Sobrien  if (quoted)
25298944Sobrien    text++;
25398944Sobrien  text_len = strlen (text);
25498944Sobrien
25598944Sobrien  /* Where is the file name?  */
25698944Sobrien  if (colon)
25798944Sobrien    {
25898944Sobrien      char *s;
25998944Sobrien
26098944Sobrien      file_to_match = (char *) xmalloc (colon - text + 1);
26198944Sobrien      strncpy (file_to_match, text, colon - text + 1);
26298944Sobrien      /* Remove trailing colons and quotes from the file name.  */
26398944Sobrien      for (s = file_to_match + (colon - text);
26498944Sobrien	   s > file_to_match;
26598944Sobrien	   s--)
26698944Sobrien	if (*s == ':' || *s == quote_char)
26798944Sobrien	  *s = '\0';
26898944Sobrien    }
26998944Sobrien  /* If the text includes a colon, they want completion only on a
27098944Sobrien     symbol name after the colon.  Otherwise, we need to complete on
27198944Sobrien     symbols as well as on files.  */
27298944Sobrien  if (colon)
27398944Sobrien    {
27498944Sobrien      list = make_file_symbol_completion_list (symbol_start, word,
27598944Sobrien					       file_to_match);
27698944Sobrien      xfree (file_to_match);
27798944Sobrien    }
27898944Sobrien  else
27998944Sobrien    {
28098944Sobrien      list = make_symbol_completion_list (symbol_start, word);
28198944Sobrien      /* If text includes characters which cannot appear in a file
28298944Sobrien	 name, they cannot be asking for completion on files.  */
28398944Sobrien      if (strcspn (text, gdb_completer_file_name_break_characters) == text_len)
28498944Sobrien	fn_list = make_source_files_completion_list (text, text);
28598944Sobrien    }
28698944Sobrien
28798944Sobrien  /* How many completions do we have in both lists?  */
28898944Sobrien  if (fn_list)
28998944Sobrien    for ( ; fn_list[n_files]; n_files++)
29098944Sobrien      ;
29198944Sobrien  if (list)
29298944Sobrien    for ( ; list[n_syms]; n_syms++)
29398944Sobrien      ;
29498944Sobrien
29598944Sobrien  /* Make list[] large enough to hold both lists, then catenate
29698944Sobrien     fn_list[] onto the end of list[].  */
29798944Sobrien  if (n_syms && n_files)
29898944Sobrien    {
29998944Sobrien      list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *));
30098944Sobrien      memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *));
30198944Sobrien      xfree (fn_list);
30298944Sobrien    }
30398944Sobrien  else if (n_files)
30498944Sobrien    {
30598944Sobrien      /* If we only have file names as possible completion, we should
30698944Sobrien	 bring them in sync with what rl_complete expects.  The
30798944Sobrien	 problem is that if the user types "break /foo/b TAB", and the
30898944Sobrien	 possible completions are "/foo/bar" and "/foo/baz"
30998944Sobrien	 rl_complete expects us to return "bar" and "baz", without the
31098944Sobrien	 leading directories, as possible completions, because `word'
31198944Sobrien	 starts at the "b".  But we ignore the value of `word' when we
31298944Sobrien	 call make_source_files_completion_list above (because that
31398944Sobrien	 would not DTRT when the completion results in both symbols
31498944Sobrien	 and file names), so make_source_files_completion_list returns
31598944Sobrien	 the full "/foo/bar" and "/foo/baz" strings.  This produces
31698944Sobrien	 wrong results when, e.g., there's only one possible
31798944Sobrien	 completion, because rl_complete will prepend "/foo/" to each
31898944Sobrien	 candidate completion.  The loop below removes that leading
31998944Sobrien	 part.  */
32098944Sobrien      for (n_files = 0; fn_list[n_files]; n_files++)
32198944Sobrien	{
32298944Sobrien	  memmove (fn_list[n_files], fn_list[n_files] + (word - text),
32398944Sobrien		   strlen (fn_list[n_files]) + 1 - (word - text));
32498944Sobrien	}
32598944Sobrien      /* Return just the file-name list as the result.  */
32698944Sobrien      list = fn_list;
32798944Sobrien    }
32898944Sobrien  else if (!n_syms)
32998944Sobrien    {
33098944Sobrien      /* No completions at all.  As the final resort, try completing
33198944Sobrien	 on the entire text as a symbol.  */
33298944Sobrien      list = make_symbol_completion_list (orig_text, word);
33398944Sobrien    }
33498944Sobrien
33598944Sobrien  return list;
33698944Sobrien}
33798944Sobrien
33898944Sobrien/* Complete on command names.  Used by "help".  */
33998944Sobrienchar **
34098944Sobriencommand_completer (char *text, char *word)
34198944Sobrien{
34298944Sobrien  return complete_on_cmdlist (cmdlist, text, word);
34398944Sobrien}
34498944Sobrien
34598944Sobrien
34698944Sobrien/* Here are some useful test cases for completion.  FIXME: These should
34798944Sobrien   be put in the test suite.  They should be tested with both M-? and TAB.
34898944Sobrien
34998944Sobrien   "show output-" "radix"
35098944Sobrien   "show output" "-radix"
35198944Sobrien   "p" ambiguous (commands starting with p--path, print, printf, etc.)
35298944Sobrien   "p "  ambiguous (all symbols)
35398944Sobrien   "info t foo" no completions
35498944Sobrien   "info t " no completions
35598944Sobrien   "info t" ambiguous ("info target", "info terminal", etc.)
35698944Sobrien   "info ajksdlfk" no completions
35798944Sobrien   "info ajksdlfk " no completions
35898944Sobrien   "info" " "
35998944Sobrien   "info " ambiguous (all info commands)
36098944Sobrien   "p \"a" no completions (string constant)
36198944Sobrien   "p 'a" ambiguous (all symbols starting with a)
36298944Sobrien   "p b-a" ambiguous (all symbols starting with a)
36398944Sobrien   "p b-" ambiguous (all symbols)
36498944Sobrien   "file Make" "file" (word break hard to screw up here)
36598944Sobrien   "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
36698944Sobrien */
36798944Sobrien
36898944Sobrien/* Generate completions all at once.  Returns a NULL-terminated array
36998944Sobrien   of strings.  Both the array and each element are allocated with
37098944Sobrien   xmalloc.  It can also return NULL if there are no completions.
37198944Sobrien
37298944Sobrien   TEXT is the caller's idea of the "word" we are looking at.
37398944Sobrien
37498944Sobrien   LINE_BUFFER is available to be looked at; it contains the entire text
37598944Sobrien   of the line.  POINT is the offset in that line of the cursor.  You
37698944Sobrien   should pretend that the line ends at POINT.  */
37798944Sobrien
37898944Sobrienchar **
379130803Smarcelcomplete_line (const char *text, char *line_buffer, int point)
38098944Sobrien{
38198944Sobrien  char **list = NULL;
38298944Sobrien  char *tmp_command, *p;
38398944Sobrien  /* Pointer within tmp_command which corresponds to text.  */
38498944Sobrien  char *word;
38598944Sobrien  struct cmd_list_element *c, *result_list;
38698944Sobrien
38798944Sobrien  /* Choose the default set of word break characters to break completions.
38898944Sobrien     If we later find out that we are doing completions on command strings
38998944Sobrien     (as opposed to strings supplied by the individual command completer
39098944Sobrien     functions, which can be any string) then we will switch to the
39198944Sobrien     special word break set for command strings, which leaves out the
39298944Sobrien     '-' character used in some commands.  */
39398944Sobrien
39498944Sobrien  rl_completer_word_break_characters =
395130803Smarcel    current_language->la_word_break_characters();
39698944Sobrien
39798944Sobrien      /* Decide whether to complete on a list of gdb commands or on symbols. */
39898944Sobrien  tmp_command = (char *) alloca (point + 1);
39998944Sobrien  p = tmp_command;
40098944Sobrien
40198944Sobrien  strncpy (tmp_command, line_buffer, point);
40298944Sobrien  tmp_command[point] = '\0';
40398944Sobrien  /* Since text always contains some number of characters leading up
40498944Sobrien     to point, we can find the equivalent position in tmp_command
40598944Sobrien     by subtracting that many characters from the end of tmp_command.  */
40698944Sobrien  word = tmp_command + point - strlen (text);
40798944Sobrien
40898944Sobrien  if (point == 0)
40998944Sobrien    {
41098944Sobrien      /* An empty line we want to consider ambiguous; that is, it
41198944Sobrien	 could be any command.  */
41298944Sobrien      c = (struct cmd_list_element *) -1;
41398944Sobrien      result_list = 0;
41498944Sobrien    }
41598944Sobrien  else
41698944Sobrien    {
41798944Sobrien      c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
41898944Sobrien    }
41998944Sobrien
42098944Sobrien  /* Move p up to the next interesting thing.  */
42198944Sobrien  while (*p == ' ' || *p == '\t')
42298944Sobrien    {
42398944Sobrien      p++;
42498944Sobrien    }
42598944Sobrien
42698944Sobrien  if (!c)
42798944Sobrien    {
42898944Sobrien      /* It is an unrecognized command.  So there are no
42998944Sobrien	 possible completions.  */
43098944Sobrien      list = NULL;
43198944Sobrien    }
43298944Sobrien  else if (c == (struct cmd_list_element *) -1)
43398944Sobrien    {
43498944Sobrien      char *q;
43598944Sobrien
43698944Sobrien      /* lookup_cmd_1 advances p up to the first ambiguous thing, but
43798944Sobrien	 doesn't advance over that thing itself.  Do so now.  */
43898944Sobrien      q = p;
43998944Sobrien      while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
44098944Sobrien	++q;
44198944Sobrien      if (q != tmp_command + point)
44298944Sobrien	{
44398944Sobrien	  /* There is something beyond the ambiguous
44498944Sobrien	     command, so there are no possible completions.  For
44598944Sobrien	     example, "info t " or "info t foo" does not complete
44698944Sobrien	     to anything, because "info t" can be "info target" or
44798944Sobrien	     "info terminal".  */
44898944Sobrien	  list = NULL;
44998944Sobrien	}
45098944Sobrien      else
45198944Sobrien	{
45298944Sobrien	  /* We're trying to complete on the command which was ambiguous.
45398944Sobrien	     This we can deal with.  */
45498944Sobrien	  if (result_list)
45598944Sobrien	    {
45698944Sobrien	      list = complete_on_cmdlist (*result_list->prefixlist, p,
45798944Sobrien					  word);
45898944Sobrien	    }
45998944Sobrien	  else
46098944Sobrien	    {
46198944Sobrien	      list = complete_on_cmdlist (cmdlist, p, word);
46298944Sobrien	    }
46398944Sobrien	  /* Insure that readline does the right thing with respect to
46498944Sobrien	     inserting quotes.  */
46598944Sobrien	  rl_completer_word_break_characters =
46698944Sobrien	    gdb_completer_command_word_break_characters;
46798944Sobrien	}
46898944Sobrien    }
46998944Sobrien  else
47098944Sobrien    {
47198944Sobrien      /* We've recognized a full command.  */
47298944Sobrien
47398944Sobrien      if (p == tmp_command + point)
47498944Sobrien	{
47598944Sobrien	  /* There is no non-whitespace in the line beyond the command.  */
47698944Sobrien
47798944Sobrien	  if (p[-1] == ' ' || p[-1] == '\t')
47898944Sobrien	    {
47998944Sobrien	      /* The command is followed by whitespace; we need to complete
48098944Sobrien		 on whatever comes after command.  */
48198944Sobrien	      if (c->prefixlist)
48298944Sobrien		{
48398944Sobrien		  /* It is a prefix command; what comes after it is
48498944Sobrien		     a subcommand (e.g. "info ").  */
48598944Sobrien		  list = complete_on_cmdlist (*c->prefixlist, p, word);
48698944Sobrien
48798944Sobrien		  /* Insure that readline does the right thing
48898944Sobrien		         with respect to inserting quotes.  */
48998944Sobrien		  rl_completer_word_break_characters =
49098944Sobrien		    gdb_completer_command_word_break_characters;
49198944Sobrien		}
49298944Sobrien	      else if (c->enums)
49398944Sobrien		{
49498944Sobrien		  list = complete_on_enum (c->enums, p, word);
49598944Sobrien		  rl_completer_word_break_characters =
49698944Sobrien		    gdb_completer_command_word_break_characters;
49798944Sobrien		}
49898944Sobrien	      else
49998944Sobrien		{
50098944Sobrien		  /* It is a normal command; what comes after it is
50198944Sobrien		     completed by the command's completer function.  */
50298944Sobrien		  if (c->completer == filename_completer)
50398944Sobrien		    {
50498944Sobrien		      /* Many commands which want to complete on
50598944Sobrien			 file names accept several file names, as
50698944Sobrien			 in "run foo bar >>baz".  So we don't want
50798944Sobrien			 to complete the entire text after the
50898944Sobrien			 command, just the last word.  To this
50998944Sobrien			 end, we need to find the beginning of the
51098944Sobrien			 file name by starting at `word' and going
51198944Sobrien			 backwards.  */
51298944Sobrien		      for (p = word;
51398944Sobrien			   p > tmp_command
51498944Sobrien			     && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
51598944Sobrien			   p--)
51698944Sobrien			;
51798944Sobrien		      rl_completer_word_break_characters =
51898944Sobrien			gdb_completer_file_name_break_characters;
51998944Sobrien		    }
52098944Sobrien		  else if (c->completer == location_completer)
52198944Sobrien		    {
52298944Sobrien		      /* Commands which complete on locations want to
52398944Sobrien			 see the entire argument.  */
52498944Sobrien		      for (p = word;
52598944Sobrien			   p > tmp_command
52698944Sobrien			     && p[-1] != ' ' && p[-1] != '\t';
52798944Sobrien			   p--)
52898944Sobrien			;
52998944Sobrien		    }
53098944Sobrien		  list = (*c->completer) (p, word);
53198944Sobrien		}
53298944Sobrien	    }
53398944Sobrien	  else
53498944Sobrien	    {
53598944Sobrien	      /* The command is not followed by whitespace; we need to
53698944Sobrien		 complete on the command itself.  e.g. "p" which is a
53798944Sobrien		 command itself but also can complete to "print", "ptype"
53898944Sobrien		 etc.  */
53998944Sobrien	      char *q;
54098944Sobrien
54198944Sobrien	      /* Find the command we are completing on.  */
54298944Sobrien	      q = p;
54398944Sobrien	      while (q > tmp_command)
54498944Sobrien		{
54598944Sobrien		  if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
54698944Sobrien		    --q;
54798944Sobrien		  else
54898944Sobrien		    break;
54998944Sobrien		}
55098944Sobrien
55198944Sobrien	      list = complete_on_cmdlist (result_list, q, word);
55298944Sobrien
55398944Sobrien		  /* Insure that readline does the right thing
55498944Sobrien		     with respect to inserting quotes.  */
55598944Sobrien	      rl_completer_word_break_characters =
55698944Sobrien		gdb_completer_command_word_break_characters;
55798944Sobrien	    }
55898944Sobrien	}
55998944Sobrien      else
56098944Sobrien	{
56198944Sobrien	  /* There is non-whitespace beyond the command.  */
56298944Sobrien
56398944Sobrien	  if (c->prefixlist && !c->allow_unknown)
56498944Sobrien	    {
56598944Sobrien	      /* It is an unrecognized subcommand of a prefix command,
56698944Sobrien		 e.g. "info adsfkdj".  */
56798944Sobrien	      list = NULL;
56898944Sobrien	    }
56998944Sobrien	  else if (c->enums)
57098944Sobrien	    {
57198944Sobrien	      list = complete_on_enum (c->enums, p, word);
57298944Sobrien	    }
57398944Sobrien	  else
57498944Sobrien	    {
57598944Sobrien	      /* It is a normal command.  */
57698944Sobrien	      if (c->completer == filename_completer)
57798944Sobrien		{
57898944Sobrien		  /* See the commentary above about the specifics
57998944Sobrien		     of file-name completion.  */
58098944Sobrien		  for (p = word;
58198944Sobrien		       p > tmp_command
58298944Sobrien			 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
58398944Sobrien		       p--)
58498944Sobrien		    ;
58598944Sobrien		  rl_completer_word_break_characters =
58698944Sobrien		    gdb_completer_file_name_break_characters;
58798944Sobrien		}
58898944Sobrien	      else if (c->completer == location_completer)
58998944Sobrien		{
59098944Sobrien		  for (p = word;
59198944Sobrien		       p > tmp_command
59298944Sobrien			 && p[-1] != ' ' && p[-1] != '\t';
59398944Sobrien		       p--)
59498944Sobrien		    ;
59598944Sobrien		}
59698944Sobrien	      list = (*c->completer) (p, word);
59798944Sobrien	    }
59898944Sobrien	}
59998944Sobrien    }
60098944Sobrien
60198944Sobrien  return list;
60298944Sobrien}
60398944Sobrien
60498944Sobrien/* Generate completions one by one for the completer.  Each time we are
60598944Sobrien   called return another potential completion to the caller.
60698944Sobrien   line_completion just completes on commands or passes the buck to the
60798944Sobrien   command's completer function, the stuff specific to symbol completion
60898944Sobrien   is in make_symbol_completion_list.
60998944Sobrien
61098944Sobrien   TEXT is the caller's idea of the "word" we are looking at.
61198944Sobrien
61298944Sobrien   MATCHES is the number of matches that have currently been collected from
61398944Sobrien   calling this completion function.  When zero, then we need to initialize,
61498944Sobrien   otherwise the initialization has already taken place and we can just
61598944Sobrien   return the next potential completion string.
61698944Sobrien
61798944Sobrien   LINE_BUFFER is available to be looked at; it contains the entire text
61898944Sobrien   of the line.  POINT is the offset in that line of the cursor.  You
61998944Sobrien   should pretend that the line ends at POINT.
62098944Sobrien
62198944Sobrien   Returns NULL if there are no more completions, else a pointer to a string
62298944Sobrien   which is a possible completion, it is the caller's responsibility to
62398944Sobrien   free the string.  */
62498944Sobrien
625130803Smarcelstatic char *
626130803Smarcelline_completion_function (const char *text, int matches, char *line_buffer, int point)
62798944Sobrien{
62898944Sobrien  static char **list = (char **) NULL;	/* Cache of completions */
62998944Sobrien  static int index;		/* Next cached completion */
63098944Sobrien  char *output = NULL;
63198944Sobrien
63298944Sobrien  if (matches == 0)
63398944Sobrien    {
63498944Sobrien      /* The caller is beginning to accumulate a new set of completions, so
63598944Sobrien         we need to find all of them now, and cache them for returning one at
63698944Sobrien         a time on future calls. */
63798944Sobrien
63898944Sobrien      if (list)
63998944Sobrien	{
64098944Sobrien	  /* Free the storage used by LIST, but not by the strings inside.
64198944Sobrien	     This is because rl_complete_internal () frees the strings. */
64298944Sobrien	  xfree (list);
64398944Sobrien	}
64498944Sobrien      index = 0;
64598944Sobrien      list = complete_line (text, line_buffer, point);
64698944Sobrien    }
64798944Sobrien
64898944Sobrien  /* If we found a list of potential completions during initialization then
64998944Sobrien     dole them out one at a time.  The vector of completions is NULL
65098944Sobrien     terminated, so after returning the last one, return NULL (and continue
65198944Sobrien     to do so) each time we are called after that, until a new list is
65298944Sobrien     available. */
65398944Sobrien
65498944Sobrien  if (list)
65598944Sobrien    {
65698944Sobrien      output = list[index];
65798944Sobrien      if (output)
65898944Sobrien	{
65998944Sobrien	  index++;
66098944Sobrien	}
66198944Sobrien    }
66298944Sobrien
66398944Sobrien#if 0
66498944Sobrien  /* Can't do this because readline hasn't yet checked the word breaks
66598944Sobrien     for figuring out whether to insert a quote.  */
66698944Sobrien  if (output == NULL)
66798944Sobrien    /* Make sure the word break characters are set back to normal for the
66898944Sobrien       next time that readline tries to complete something.  */
66998944Sobrien    rl_completer_word_break_characters =
670130803Smarcel      current_language->la_word_break_characters();
67198944Sobrien#endif
67298944Sobrien
67398944Sobrien  return (output);
67498944Sobrien}
67598944Sobrien
676130803Smarcel/* Skip over the possibly quoted word STR (as defined by the quote
677130803Smarcel   characters QUOTECHARS and the the word break characters
678130803Smarcel   BREAKCHARS).  Returns pointer to the location after the "word".  If
679130803Smarcel   either QUOTECHARS or BREAKCHARS is NULL, use the same values used
680130803Smarcel   by the completer.  */
681130803Smarcel
68298944Sobrienchar *
683130803Smarcelskip_quoted_chars (char *str, char *quotechars, char *breakchars)
68498944Sobrien{
68598944Sobrien  char quote_char = '\0';
68698944Sobrien  char *scan;
68798944Sobrien
688130803Smarcel  if (quotechars == NULL)
689130803Smarcel    quotechars = gdb_completer_quote_characters;
690130803Smarcel
691130803Smarcel  if (breakchars == NULL)
692130803Smarcel    breakchars = current_language->la_word_break_characters();
693130803Smarcel
69498944Sobrien  for (scan = str; *scan != '\0'; scan++)
69598944Sobrien    {
69698944Sobrien      if (quote_char != '\0')
69798944Sobrien	{
69898944Sobrien	  /* Ignore everything until the matching close quote char */
69998944Sobrien	  if (*scan == quote_char)
70098944Sobrien	    {
70198944Sobrien	      /* Found matching close quote. */
70298944Sobrien	      scan++;
70398944Sobrien	      break;
70498944Sobrien	    }
70598944Sobrien	}
706130803Smarcel      else if (strchr (quotechars, *scan))
70798944Sobrien	{
70898944Sobrien	  /* Found start of a quoted string. */
70998944Sobrien	  quote_char = *scan;
71098944Sobrien	}
711130803Smarcel      else if (strchr (breakchars, *scan))
71298944Sobrien	{
71398944Sobrien	  break;
71498944Sobrien	}
71598944Sobrien    }
716130803Smarcel
71798944Sobrien  return (scan);
71898944Sobrien}
71998944Sobrien
720130803Smarcel/* Skip over the possibly quoted word STR (as defined by the quote
721130803Smarcel   characters and word break characters used by the completer).
722130803Smarcel   Returns pointer to the location after the "word". */
723130803Smarcel
724130803Smarcelchar *
725130803Smarcelskip_quoted (char *str)
726130803Smarcel{
727130803Smarcel  return skip_quoted_chars (str, NULL, NULL);
728130803Smarcel}
729