15796c8dcSSimon Schubert /* Line completion stuff for GDB, the GNU debugger.
2*ef5ccd6cSJohn Marino Copyright (C) 2000-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert
45796c8dcSSimon Schubert This file is part of GDB.
55796c8dcSSimon Schubert
65796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
75796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
85796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
95796c8dcSSimon Schubert (at your option) any later version.
105796c8dcSSimon Schubert
115796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
125796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
135796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
145796c8dcSSimon Schubert GNU General Public License for more details.
155796c8dcSSimon Schubert
165796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
175796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
185796c8dcSSimon Schubert
195796c8dcSSimon Schubert #include "defs.h"
205796c8dcSSimon Schubert #include "symtab.h"
215796c8dcSSimon Schubert #include "gdbtypes.h"
225796c8dcSSimon Schubert #include "expression.h"
235796c8dcSSimon Schubert #include "filenames.h" /* For DOSish file names. */
245796c8dcSSimon Schubert #include "language.h"
255796c8dcSSimon Schubert #include "gdb_assert.h"
26c50c785cSJohn Marino #include "exceptions.h"
27*ef5ccd6cSJohn Marino #include "gdb_signals.h"
285796c8dcSSimon Schubert
295796c8dcSSimon Schubert #include "cli/cli-decode.h"
305796c8dcSSimon Schubert
315796c8dcSSimon Schubert /* FIXME: This is needed because of lookup_cmd_1 (). We should be
325796c8dcSSimon Schubert calling a hook instead so we eliminate the CLI dependency. */
335796c8dcSSimon Schubert #include "gdbcmd.h"
345796c8dcSSimon Schubert
355796c8dcSSimon Schubert /* Needed for rl_completer_word_break_characters() and for
365796c8dcSSimon Schubert rl_filename_completion_function. */
375796c8dcSSimon Schubert #include "readline/readline.h"
385796c8dcSSimon Schubert
395796c8dcSSimon Schubert /* readline defines this. */
405796c8dcSSimon Schubert #undef savestring
415796c8dcSSimon Schubert
425796c8dcSSimon Schubert #include "completer.h"
435796c8dcSSimon Schubert
445796c8dcSSimon Schubert /* Prototypes for local functions. */
455796c8dcSSimon Schubert static
465796c8dcSSimon Schubert char *line_completion_function (const char *text, int matches,
475796c8dcSSimon Schubert char *line_buffer,
485796c8dcSSimon Schubert int point);
495796c8dcSSimon Schubert
505796c8dcSSimon Schubert /* readline uses the word breaks for two things:
515796c8dcSSimon Schubert (1) In figuring out where to point the TEXT parameter to the
525796c8dcSSimon Schubert rl_completion_entry_function. Since we don't use TEXT for much,
53c50c785cSJohn Marino it doesn't matter a lot what the word breaks are for this purpose,
54c50c785cSJohn Marino but it does affect how much stuff M-? lists.
555796c8dcSSimon Schubert (2) If one of the matches contains a word break character, readline
565796c8dcSSimon Schubert will quote it. That's why we switch between
575796c8dcSSimon Schubert current_language->la_word_break_characters() and
585796c8dcSSimon Schubert gdb_completer_command_word_break_characters. I'm not sure when
59c50c785cSJohn Marino we need this behavior (perhaps for funky characters in C++
60c50c785cSJohn Marino symbols?). */
615796c8dcSSimon Schubert
625796c8dcSSimon Schubert /* Variables which are necessary for fancy command line editing. */
635796c8dcSSimon Schubert
645796c8dcSSimon Schubert /* When completing on command names, we remove '-' from the list of
655796c8dcSSimon Schubert word break characters, since we use it in command names. If the
665796c8dcSSimon Schubert readline library sees one in any of the current completion strings,
67c50c785cSJohn Marino it thinks that the string needs to be quoted and automatically
68c50c785cSJohn Marino supplies a leading quote. */
695796c8dcSSimon Schubert static char *gdb_completer_command_word_break_characters =
705796c8dcSSimon Schubert " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
715796c8dcSSimon Schubert
725796c8dcSSimon Schubert /* When completing on file names, we remove from the list of word
735796c8dcSSimon Schubert break characters any characters that are commonly used in file
745796c8dcSSimon Schubert names, such as '-', '+', '~', etc. Otherwise, readline displays
755796c8dcSSimon Schubert incorrect completion candidates. */
765796c8dcSSimon Schubert #ifdef HAVE_DOS_BASED_FILE_SYSTEM
775796c8dcSSimon Schubert /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
785796c8dcSSimon Schubert programs support @foo style response files. */
795796c8dcSSimon Schubert static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
805796c8dcSSimon Schubert #else
815796c8dcSSimon Schubert static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
825796c8dcSSimon Schubert #endif
835796c8dcSSimon Schubert
84c50c785cSJohn Marino /* Characters that can be used to quote completion strings. Note that
85c50c785cSJohn Marino we can't include '"' because the gdb C parser treats such quoted
86c50c785cSJohn Marino sequences as strings. */
875796c8dcSSimon Schubert static char *gdb_completer_quote_characters = "'";
885796c8dcSSimon Schubert
895796c8dcSSimon Schubert /* Accessor for some completer data that may interest other files. */
905796c8dcSSimon Schubert
915796c8dcSSimon Schubert char *
get_gdb_completer_quote_characters(void)925796c8dcSSimon Schubert get_gdb_completer_quote_characters (void)
935796c8dcSSimon Schubert {
945796c8dcSSimon Schubert return gdb_completer_quote_characters;
955796c8dcSSimon Schubert }
965796c8dcSSimon Schubert
975796c8dcSSimon Schubert /* Line completion interface function for readline. */
985796c8dcSSimon Schubert
995796c8dcSSimon Schubert char *
readline_line_completion_function(const char * text,int matches)1005796c8dcSSimon Schubert readline_line_completion_function (const char *text, int matches)
1015796c8dcSSimon Schubert {
102c50c785cSJohn Marino return line_completion_function (text, matches,
103c50c785cSJohn Marino rl_line_buffer, rl_point);
1045796c8dcSSimon Schubert }
1055796c8dcSSimon Schubert
106c50c785cSJohn Marino /* This can be used for functions which don't want to complete on
107c50c785cSJohn Marino symbols but don't want to complete on anything else either. */
VEC(char_ptr)108*ef5ccd6cSJohn Marino VEC (char_ptr) *
109c50c785cSJohn Marino noop_completer (struct cmd_list_element *ignore,
110c50c785cSJohn Marino char *text, char *prefix)
1115796c8dcSSimon Schubert {
1125796c8dcSSimon Schubert return NULL;
1135796c8dcSSimon Schubert }
1145796c8dcSSimon Schubert
1155796c8dcSSimon Schubert /* Complete on filenames. */
VEC(char_ptr)116*ef5ccd6cSJohn Marino VEC (char_ptr) *
117c50c785cSJohn Marino filename_completer (struct cmd_list_element *ignore,
118c50c785cSJohn Marino char *text, char *word)
1195796c8dcSSimon Schubert {
1205796c8dcSSimon Schubert int subsequent_name;
121*ef5ccd6cSJohn Marino VEC (char_ptr) *return_val = NULL;
1225796c8dcSSimon Schubert
1235796c8dcSSimon Schubert subsequent_name = 0;
1245796c8dcSSimon Schubert while (1)
1255796c8dcSSimon Schubert {
1265796c8dcSSimon Schubert char *p, *q;
127cf7f2e2dSJohn Marino
1285796c8dcSSimon Schubert p = rl_filename_completion_function (text, subsequent_name);
1295796c8dcSSimon Schubert if (p == NULL)
1305796c8dcSSimon Schubert break;
1315796c8dcSSimon Schubert /* We need to set subsequent_name to a non-zero value before the
132c50c785cSJohn Marino continue line below, because otherwise, if the first file
133c50c785cSJohn Marino seen by GDB is a backup file whose name ends in a `~', we
134c50c785cSJohn Marino will loop indefinitely. */
1355796c8dcSSimon Schubert subsequent_name = 1;
136c50c785cSJohn Marino /* Like emacs, don't complete on old versions. Especially
137c50c785cSJohn Marino useful in the "source" command. */
1385796c8dcSSimon Schubert if (p[strlen (p) - 1] == '~')
1395796c8dcSSimon Schubert {
1405796c8dcSSimon Schubert xfree (p);
1415796c8dcSSimon Schubert continue;
1425796c8dcSSimon Schubert }
1435796c8dcSSimon Schubert
1445796c8dcSSimon Schubert if (word == text)
1455796c8dcSSimon Schubert /* Return exactly p. */
146*ef5ccd6cSJohn Marino q = p;
1475796c8dcSSimon Schubert else if (word > text)
1485796c8dcSSimon Schubert {
1495796c8dcSSimon Schubert /* Return some portion of p. */
1505796c8dcSSimon Schubert q = xmalloc (strlen (p) + 5);
1515796c8dcSSimon Schubert strcpy (q, p + (word - text));
1525796c8dcSSimon Schubert xfree (p);
1535796c8dcSSimon Schubert }
1545796c8dcSSimon Schubert else
1555796c8dcSSimon Schubert {
1565796c8dcSSimon Schubert /* Return some of TEXT plus p. */
1575796c8dcSSimon Schubert q = xmalloc (strlen (p) + (text - word) + 5);
1585796c8dcSSimon Schubert strncpy (q, word, text - word);
1595796c8dcSSimon Schubert q[text - word] = '\0';
1605796c8dcSSimon Schubert strcat (q, p);
1615796c8dcSSimon Schubert xfree (p);
1625796c8dcSSimon Schubert }
163*ef5ccd6cSJohn Marino VEC_safe_push (char_ptr, return_val, q);
1645796c8dcSSimon Schubert }
1655796c8dcSSimon Schubert #if 0
166c50c785cSJohn Marino /* There is no way to do this just long enough to affect quote
167c50c785cSJohn Marino inserting without also affecting the next completion. This
168c50c785cSJohn Marino should be fixed in readline. FIXME. */
1695796c8dcSSimon Schubert /* Ensure that readline does the right thing
1705796c8dcSSimon Schubert with respect to inserting quotes. */
1715796c8dcSSimon Schubert rl_completer_word_break_characters = "";
1725796c8dcSSimon Schubert #endif
1735796c8dcSSimon Schubert return return_val;
1745796c8dcSSimon Schubert }
1755796c8dcSSimon Schubert
1765796c8dcSSimon Schubert /* Complete on locations, which might be of two possible forms:
1775796c8dcSSimon Schubert
1785796c8dcSSimon Schubert file:line
1795796c8dcSSimon Schubert or
1805796c8dcSSimon Schubert symbol+offset
1815796c8dcSSimon Schubert
182c50c785cSJohn Marino This is intended to be used in commands that set breakpoints
183c50c785cSJohn Marino etc. */
184c50c785cSJohn Marino
VEC(char_ptr)185*ef5ccd6cSJohn Marino VEC (char_ptr) *
186c50c785cSJohn Marino location_completer (struct cmd_list_element *ignore,
187c50c785cSJohn Marino char *text, char *word)
1885796c8dcSSimon Schubert {
189*ef5ccd6cSJohn Marino int n_syms, n_files, ix;
190*ef5ccd6cSJohn Marino VEC (char_ptr) *fn_list = NULL;
191*ef5ccd6cSJohn Marino VEC (char_ptr) *list = NULL;
1925796c8dcSSimon Schubert char *p;
1935796c8dcSSimon Schubert int quote_found = 0;
1945796c8dcSSimon Schubert int quoted = *text == '\'' || *text == '"';
1955796c8dcSSimon Schubert int quote_char = '\0';
1965796c8dcSSimon Schubert char *colon = NULL;
1975796c8dcSSimon Schubert char *file_to_match = NULL;
1985796c8dcSSimon Schubert char *symbol_start = text;
1995796c8dcSSimon Schubert char *orig_text = text;
2005796c8dcSSimon Schubert size_t text_len;
2015796c8dcSSimon Schubert
202*ef5ccd6cSJohn Marino /* Do we have an unquoted colon, as in "break foo.c:bar"? */
2035796c8dcSSimon Schubert for (p = text; *p != '\0'; ++p)
2045796c8dcSSimon Schubert {
2055796c8dcSSimon Schubert if (*p == '\\' && p[1] == '\'')
2065796c8dcSSimon Schubert p++;
2075796c8dcSSimon Schubert else if (*p == '\'' || *p == '"')
2085796c8dcSSimon Schubert {
2095796c8dcSSimon Schubert quote_found = *p;
2105796c8dcSSimon Schubert quote_char = *p++;
2115796c8dcSSimon Schubert while (*p != '\0' && *p != quote_found)
2125796c8dcSSimon Schubert {
2135796c8dcSSimon Schubert if (*p == '\\' && p[1] == quote_found)
2145796c8dcSSimon Schubert p++;
2155796c8dcSSimon Schubert p++;
2165796c8dcSSimon Schubert }
2175796c8dcSSimon Schubert
2185796c8dcSSimon Schubert if (*p == quote_found)
2195796c8dcSSimon Schubert quote_found = 0;
2205796c8dcSSimon Schubert else
2215796c8dcSSimon Schubert break; /* Hit the end of text. */
2225796c8dcSSimon Schubert }
2235796c8dcSSimon Schubert #if HAVE_DOS_BASED_FILE_SYSTEM
2245796c8dcSSimon Schubert /* If we have a DOS-style absolute file name at the beginning of
2255796c8dcSSimon Schubert TEXT, and the colon after the drive letter is the only colon
2265796c8dcSSimon Schubert we found, pretend the colon is not there. */
2275796c8dcSSimon Schubert else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
2285796c8dcSSimon Schubert ;
2295796c8dcSSimon Schubert #endif
2305796c8dcSSimon Schubert else if (*p == ':' && !colon)
2315796c8dcSSimon Schubert {
2325796c8dcSSimon Schubert colon = p;
2335796c8dcSSimon Schubert symbol_start = p + 1;
2345796c8dcSSimon Schubert }
2355796c8dcSSimon Schubert else if (strchr (current_language->la_word_break_characters(), *p))
2365796c8dcSSimon Schubert symbol_start = p + 1;
2375796c8dcSSimon Schubert }
2385796c8dcSSimon Schubert
2395796c8dcSSimon Schubert if (quoted)
2405796c8dcSSimon Schubert text++;
2415796c8dcSSimon Schubert text_len = strlen (text);
2425796c8dcSSimon Schubert
2435796c8dcSSimon Schubert /* Where is the file name? */
2445796c8dcSSimon Schubert if (colon)
2455796c8dcSSimon Schubert {
2465796c8dcSSimon Schubert char *s;
2475796c8dcSSimon Schubert
2485796c8dcSSimon Schubert file_to_match = (char *) xmalloc (colon - text + 1);
2495796c8dcSSimon Schubert strncpy (file_to_match, text, colon - text + 1);
2505796c8dcSSimon Schubert /* Remove trailing colons and quotes from the file name. */
2515796c8dcSSimon Schubert for (s = file_to_match + (colon - text);
2525796c8dcSSimon Schubert s > file_to_match;
2535796c8dcSSimon Schubert s--)
2545796c8dcSSimon Schubert if (*s == ':' || *s == quote_char)
2555796c8dcSSimon Schubert *s = '\0';
2565796c8dcSSimon Schubert }
2575796c8dcSSimon Schubert /* If the text includes a colon, they want completion only on a
2585796c8dcSSimon Schubert symbol name after the colon. Otherwise, we need to complete on
2595796c8dcSSimon Schubert symbols as well as on files. */
2605796c8dcSSimon Schubert if (colon)
2615796c8dcSSimon Schubert {
2625796c8dcSSimon Schubert list = make_file_symbol_completion_list (symbol_start, word,
2635796c8dcSSimon Schubert file_to_match);
2645796c8dcSSimon Schubert xfree (file_to_match);
2655796c8dcSSimon Schubert }
2665796c8dcSSimon Schubert else
2675796c8dcSSimon Schubert {
2685796c8dcSSimon Schubert list = make_symbol_completion_list (symbol_start, word);
2695796c8dcSSimon Schubert /* If text includes characters which cannot appear in a file
2705796c8dcSSimon Schubert name, they cannot be asking for completion on files. */
2715796c8dcSSimon Schubert if (strcspn (text,
2725796c8dcSSimon Schubert gdb_completer_file_name_break_characters) == text_len)
2735796c8dcSSimon Schubert fn_list = make_source_files_completion_list (text, text);
2745796c8dcSSimon Schubert }
2755796c8dcSSimon Schubert
276*ef5ccd6cSJohn Marino n_syms = VEC_length (char_ptr, list);
277*ef5ccd6cSJohn Marino n_files = VEC_length (char_ptr, fn_list);
2785796c8dcSSimon Schubert
279*ef5ccd6cSJohn Marino /* Catenate fn_list[] onto the end of list[]. */
280*ef5ccd6cSJohn Marino if (!n_syms)
281*ef5ccd6cSJohn Marino {
282*ef5ccd6cSJohn Marino VEC_free (char_ptr, list); /* Paranoia. */
283*ef5ccd6cSJohn Marino list = fn_list;
284*ef5ccd6cSJohn Marino fn_list = NULL;
285*ef5ccd6cSJohn Marino }
286*ef5ccd6cSJohn Marino else
287*ef5ccd6cSJohn Marino {
288*ef5ccd6cSJohn Marino for (ix = 0; VEC_iterate (char_ptr, fn_list, ix, p); ++ix)
289*ef5ccd6cSJohn Marino VEC_safe_push (char_ptr, list, p);
290*ef5ccd6cSJohn Marino VEC_free (char_ptr, fn_list);
291*ef5ccd6cSJohn Marino }
292*ef5ccd6cSJohn Marino
2935796c8dcSSimon Schubert if (n_syms && n_files)
2945796c8dcSSimon Schubert {
295*ef5ccd6cSJohn Marino /* Nothing. */
2965796c8dcSSimon Schubert }
2975796c8dcSSimon Schubert else if (n_files)
2985796c8dcSSimon Schubert {
2995796c8dcSSimon Schubert /* If we only have file names as possible completion, we should
3005796c8dcSSimon Schubert bring them in sync with what rl_complete expects. The
3015796c8dcSSimon Schubert problem is that if the user types "break /foo/b TAB", and the
3025796c8dcSSimon Schubert possible completions are "/foo/bar" and "/foo/baz"
3035796c8dcSSimon Schubert rl_complete expects us to return "bar" and "baz", without the
3045796c8dcSSimon Schubert leading directories, as possible completions, because `word'
3055796c8dcSSimon Schubert starts at the "b". But we ignore the value of `word' when we
3065796c8dcSSimon Schubert call make_source_files_completion_list above (because that
3075796c8dcSSimon Schubert would not DTRT when the completion results in both symbols
3085796c8dcSSimon Schubert and file names), so make_source_files_completion_list returns
3095796c8dcSSimon Schubert the full "/foo/bar" and "/foo/baz" strings. This produces
3105796c8dcSSimon Schubert wrong results when, e.g., there's only one possible
3115796c8dcSSimon Schubert completion, because rl_complete will prepend "/foo/" to each
3125796c8dcSSimon Schubert candidate completion. The loop below removes that leading
3135796c8dcSSimon Schubert part. */
314*ef5ccd6cSJohn Marino for (ix = 0; VEC_iterate (char_ptr, list, ix, p); ++ix)
3155796c8dcSSimon Schubert {
316*ef5ccd6cSJohn Marino memmove (p, p + (word - text),
317*ef5ccd6cSJohn Marino strlen (p) + 1 - (word - text));
3185796c8dcSSimon Schubert }
3195796c8dcSSimon Schubert }
3205796c8dcSSimon Schubert else if (!n_syms)
3215796c8dcSSimon Schubert {
3225796c8dcSSimon Schubert /* No completions at all. As the final resort, try completing
3235796c8dcSSimon Schubert on the entire text as a symbol. */
3245796c8dcSSimon Schubert list = make_symbol_completion_list (orig_text, word);
3255796c8dcSSimon Schubert }
3265796c8dcSSimon Schubert
3275796c8dcSSimon Schubert return list;
3285796c8dcSSimon Schubert }
3295796c8dcSSimon Schubert
3305796c8dcSSimon Schubert /* Helper for expression_completer which recursively adds field and
3315796c8dcSSimon Schubert method names from TYPE, a struct or union type, to the array
332*ef5ccd6cSJohn Marino OUTPUT. */
3335796c8dcSSimon Schubert static void
add_struct_fields(struct type * type,VEC (char_ptr)** output,char * fieldname,int namelen)334*ef5ccd6cSJohn Marino add_struct_fields (struct type *type, VEC (char_ptr) **output,
3355796c8dcSSimon Schubert char *fieldname, int namelen)
3365796c8dcSSimon Schubert {
3375796c8dcSSimon Schubert int i;
3385796c8dcSSimon Schubert int computed_type_name = 0;
339*ef5ccd6cSJohn Marino const char *type_name = NULL;
3405796c8dcSSimon Schubert
3415796c8dcSSimon Schubert CHECK_TYPEDEF (type);
3425796c8dcSSimon Schubert for (i = 0; i < TYPE_NFIELDS (type); ++i)
3435796c8dcSSimon Schubert {
3445796c8dcSSimon Schubert if (i < TYPE_N_BASECLASSES (type))
345*ef5ccd6cSJohn Marino add_struct_fields (TYPE_BASECLASS (type, i),
346c50c785cSJohn Marino output, fieldname, namelen);
347c50c785cSJohn Marino else if (TYPE_FIELD_NAME (type, i))
348c50c785cSJohn Marino {
349c50c785cSJohn Marino if (TYPE_FIELD_NAME (type, i)[0] != '\0')
350c50c785cSJohn Marino {
351c50c785cSJohn Marino if (! strncmp (TYPE_FIELD_NAME (type, i),
352c50c785cSJohn Marino fieldname, namelen))
353*ef5ccd6cSJohn Marino VEC_safe_push (char_ptr, *output,
354*ef5ccd6cSJohn Marino xstrdup (TYPE_FIELD_NAME (type, i)));
3555796c8dcSSimon Schubert }
356c50c785cSJohn Marino else if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
357c50c785cSJohn Marino {
358c50c785cSJohn Marino /* Recurse into anonymous unions. */
359*ef5ccd6cSJohn Marino add_struct_fields (TYPE_FIELD_TYPE (type, i),
360c50c785cSJohn Marino output, fieldname, namelen);
361c50c785cSJohn Marino }
362c50c785cSJohn Marino }
363c50c785cSJohn Marino }
3645796c8dcSSimon Schubert
3655796c8dcSSimon Schubert for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
3665796c8dcSSimon Schubert {
367*ef5ccd6cSJohn Marino const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
368cf7f2e2dSJohn Marino
3695796c8dcSSimon Schubert if (name && ! strncmp (name, fieldname, namelen))
3705796c8dcSSimon Schubert {
3715796c8dcSSimon Schubert if (!computed_type_name)
3725796c8dcSSimon Schubert {
3735796c8dcSSimon Schubert type_name = type_name_no_tag (type);
3745796c8dcSSimon Schubert computed_type_name = 1;
3755796c8dcSSimon Schubert }
3765796c8dcSSimon Schubert /* Omit constructors from the completion list. */
377cf7f2e2dSJohn Marino if (!type_name || strcmp (type_name, name))
378*ef5ccd6cSJohn Marino VEC_safe_push (char_ptr, *output, xstrdup (name));
3795796c8dcSSimon Schubert }
3805796c8dcSSimon Schubert }
3815796c8dcSSimon Schubert }
3825796c8dcSSimon Schubert
3835796c8dcSSimon Schubert /* Complete on expressions. Often this means completing on symbol
3845796c8dcSSimon Schubert names, but some language parsers also have support for completing
3855796c8dcSSimon Schubert field names. */
VEC(char_ptr)386*ef5ccd6cSJohn Marino VEC (char_ptr) *
387c50c785cSJohn Marino expression_completer (struct cmd_list_element *ignore,
388c50c785cSJohn Marino char *text, char *word)
3895796c8dcSSimon Schubert {
390c50c785cSJohn Marino struct type *type = NULL;
3915796c8dcSSimon Schubert char *fieldname, *p;
392c50c785cSJohn Marino volatile struct gdb_exception except;
393*ef5ccd6cSJohn Marino enum type_code code = TYPE_CODE_UNDEF;
3945796c8dcSSimon Schubert
3955796c8dcSSimon Schubert /* Perform a tentative parse of the expression, to see whether a
3965796c8dcSSimon Schubert field completion is required. */
3975796c8dcSSimon Schubert fieldname = NULL;
398c50c785cSJohn Marino TRY_CATCH (except, RETURN_MASK_ERROR)
399c50c785cSJohn Marino {
400*ef5ccd6cSJohn Marino type = parse_expression_for_completion (text, &fieldname, &code);
401c50c785cSJohn Marino }
402c50c785cSJohn Marino if (except.reason < 0)
403c50c785cSJohn Marino return NULL;
4045796c8dcSSimon Schubert if (fieldname && type)
4055796c8dcSSimon Schubert {
4065796c8dcSSimon Schubert for (;;)
4075796c8dcSSimon Schubert {
4085796c8dcSSimon Schubert CHECK_TYPEDEF (type);
4095796c8dcSSimon Schubert if (TYPE_CODE (type) != TYPE_CODE_PTR
4105796c8dcSSimon Schubert && TYPE_CODE (type) != TYPE_CODE_REF)
4115796c8dcSSimon Schubert break;
4125796c8dcSSimon Schubert type = TYPE_TARGET_TYPE (type);
4135796c8dcSSimon Schubert }
4145796c8dcSSimon Schubert
4155796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_UNION
4165796c8dcSSimon Schubert || TYPE_CODE (type) == TYPE_CODE_STRUCT)
4175796c8dcSSimon Schubert {
4185796c8dcSSimon Schubert int flen = strlen (fieldname);
419*ef5ccd6cSJohn Marino VEC (char_ptr) *result = NULL;
4205796c8dcSSimon Schubert
421*ef5ccd6cSJohn Marino add_struct_fields (type, &result, fieldname, flen);
4225796c8dcSSimon Schubert xfree (fieldname);
4235796c8dcSSimon Schubert return result;
4245796c8dcSSimon Schubert }
4255796c8dcSSimon Schubert }
426*ef5ccd6cSJohn Marino else if (fieldname && code != TYPE_CODE_UNDEF)
427*ef5ccd6cSJohn Marino {
428*ef5ccd6cSJohn Marino VEC (char_ptr) *result;
429*ef5ccd6cSJohn Marino struct cleanup *cleanup = make_cleanup (xfree, fieldname);
430*ef5ccd6cSJohn Marino
431*ef5ccd6cSJohn Marino result = make_symbol_completion_type (fieldname, fieldname, code);
432*ef5ccd6cSJohn Marino do_cleanups (cleanup);
433*ef5ccd6cSJohn Marino return result;
434*ef5ccd6cSJohn Marino }
4355796c8dcSSimon Schubert xfree (fieldname);
4365796c8dcSSimon Schubert
4375796c8dcSSimon Schubert /* Commands which complete on locations want to see the entire
4385796c8dcSSimon Schubert argument. */
4395796c8dcSSimon Schubert for (p = word;
4405796c8dcSSimon Schubert p > text && p[-1] != ' ' && p[-1] != '\t';
4415796c8dcSSimon Schubert p--)
4425796c8dcSSimon Schubert ;
4435796c8dcSSimon Schubert
4445796c8dcSSimon Schubert /* Not ideal but it is what we used to do before... */
4455796c8dcSSimon Schubert return location_completer (ignore, p, word);
4465796c8dcSSimon Schubert }
4475796c8dcSSimon Schubert
448c50c785cSJohn Marino /* Here are some useful test cases for completion. FIXME: These
449c50c785cSJohn Marino should be put in the test suite. They should be tested with both
450c50c785cSJohn Marino M-? and TAB.
4515796c8dcSSimon Schubert
4525796c8dcSSimon Schubert "show output-" "radix"
4535796c8dcSSimon Schubert "show output" "-radix"
4545796c8dcSSimon Schubert "p" ambiguous (commands starting with p--path, print, printf, etc.)
4555796c8dcSSimon Schubert "p " ambiguous (all symbols)
4565796c8dcSSimon Schubert "info t foo" no completions
4575796c8dcSSimon Schubert "info t " no completions
4585796c8dcSSimon Schubert "info t" ambiguous ("info target", "info terminal", etc.)
4595796c8dcSSimon Schubert "info ajksdlfk" no completions
4605796c8dcSSimon Schubert "info ajksdlfk " no completions
4615796c8dcSSimon Schubert "info" " "
4625796c8dcSSimon Schubert "info " ambiguous (all info commands)
4635796c8dcSSimon Schubert "p \"a" no completions (string constant)
4645796c8dcSSimon Schubert "p 'a" ambiguous (all symbols starting with a)
4655796c8dcSSimon Schubert "p b-a" ambiguous (all symbols starting with a)
4665796c8dcSSimon Schubert "p b-" ambiguous (all symbols)
4675796c8dcSSimon Schubert "file Make" "file" (word break hard to screw up here)
4685796c8dcSSimon Schubert "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
4695796c8dcSSimon Schubert */
4705796c8dcSSimon Schubert
4715796c8dcSSimon Schubert typedef enum
4725796c8dcSSimon Schubert {
4735796c8dcSSimon Schubert handle_brkchars,
4745796c8dcSSimon Schubert handle_completions,
4755796c8dcSSimon Schubert handle_help
4765796c8dcSSimon Schubert }
4775796c8dcSSimon Schubert complete_line_internal_reason;
4785796c8dcSSimon Schubert
4795796c8dcSSimon Schubert
4805796c8dcSSimon Schubert /* Internal function used to handle completions.
4815796c8dcSSimon Schubert
4825796c8dcSSimon Schubert
4835796c8dcSSimon Schubert TEXT is the caller's idea of the "word" we are looking at.
4845796c8dcSSimon Schubert
485c50c785cSJohn Marino LINE_BUFFER is available to be looked at; it contains the entire
486c50c785cSJohn Marino text of the line. POINT is the offset in that line of the cursor.
487c50c785cSJohn Marino You should pretend that the line ends at POINT.
4885796c8dcSSimon Schubert
4895796c8dcSSimon Schubert REASON is of type complete_line_internal_reason.
4905796c8dcSSimon Schubert
4915796c8dcSSimon Schubert If REASON is handle_brkchars:
492c50c785cSJohn Marino Preliminary phase, called by gdb_completion_word_break_characters
493c50c785cSJohn Marino function, is used to determine the correct set of chars that are
494c50c785cSJohn Marino word delimiters depending on the current command in line_buffer.
495c50c785cSJohn Marino No completion list should be generated; the return value should be
496c50c785cSJohn Marino NULL. This is checked by an assertion in that function.
4975796c8dcSSimon Schubert
4985796c8dcSSimon Schubert If REASON is handle_completions:
4995796c8dcSSimon Schubert Main phase, called by complete_line function, is used to get the list
5005796c8dcSSimon Schubert of posible completions.
5015796c8dcSSimon Schubert
5025796c8dcSSimon Schubert If REASON is handle_help:
5035796c8dcSSimon Schubert Special case when completing a 'help' command. In this case,
5045796c8dcSSimon Schubert once sub-command completions are exhausted, we simply return NULL.
5055796c8dcSSimon Schubert */
5065796c8dcSSimon Schubert
VEC(char_ptr)507*ef5ccd6cSJohn Marino static VEC (char_ptr) *
508c50c785cSJohn Marino complete_line_internal (const char *text,
509c50c785cSJohn Marino char *line_buffer, int point,
5105796c8dcSSimon Schubert complete_line_internal_reason reason)
5115796c8dcSSimon Schubert {
512*ef5ccd6cSJohn Marino VEC (char_ptr) *list = NULL;
5135796c8dcSSimon Schubert char *tmp_command, *p;
514*ef5ccd6cSJohn Marino int ignore_help_classes;
5155796c8dcSSimon Schubert /* Pointer within tmp_command which corresponds to text. */
5165796c8dcSSimon Schubert char *word;
5175796c8dcSSimon Schubert struct cmd_list_element *c, *result_list;
5185796c8dcSSimon Schubert
519c50c785cSJohn Marino /* Choose the default set of word break characters to break
520c50c785cSJohn Marino completions. If we later find out that we are doing completions
521c50c785cSJohn Marino on command strings (as opposed to strings supplied by the
522c50c785cSJohn Marino individual command completer functions, which can be any string)
523c50c785cSJohn Marino then we will switch to the special word break set for command
524c50c785cSJohn Marino strings, which leaves out the '-' character used in some
525c50c785cSJohn Marino commands. */
5265796c8dcSSimon Schubert rl_completer_word_break_characters =
5275796c8dcSSimon Schubert current_language->la_word_break_characters();
5285796c8dcSSimon Schubert
529c50c785cSJohn Marino /* Decide whether to complete on a list of gdb commands or on
530c50c785cSJohn Marino symbols. */
5315796c8dcSSimon Schubert tmp_command = (char *) alloca (point + 1);
5325796c8dcSSimon Schubert p = tmp_command;
5335796c8dcSSimon Schubert
534*ef5ccd6cSJohn Marino /* The help command should complete help aliases. */
535*ef5ccd6cSJohn Marino ignore_help_classes = reason != handle_help;
536*ef5ccd6cSJohn Marino
5375796c8dcSSimon Schubert strncpy (tmp_command, line_buffer, point);
5385796c8dcSSimon Schubert tmp_command[point] = '\0';
5395796c8dcSSimon Schubert /* Since text always contains some number of characters leading up
5405796c8dcSSimon Schubert to point, we can find the equivalent position in tmp_command
5415796c8dcSSimon Schubert by subtracting that many characters from the end of tmp_command. */
5425796c8dcSSimon Schubert word = tmp_command + point - strlen (text);
5435796c8dcSSimon Schubert
5445796c8dcSSimon Schubert if (point == 0)
5455796c8dcSSimon Schubert {
5465796c8dcSSimon Schubert /* An empty line we want to consider ambiguous; that is, it
5475796c8dcSSimon Schubert could be any command. */
548c50c785cSJohn Marino c = CMD_LIST_AMBIGUOUS;
5495796c8dcSSimon Schubert result_list = 0;
5505796c8dcSSimon Schubert }
5515796c8dcSSimon Schubert else
5525796c8dcSSimon Schubert {
553*ef5ccd6cSJohn Marino c = lookup_cmd_1 (&p, cmdlist, &result_list, ignore_help_classes);
5545796c8dcSSimon Schubert }
5555796c8dcSSimon Schubert
5565796c8dcSSimon Schubert /* Move p up to the next interesting thing. */
5575796c8dcSSimon Schubert while (*p == ' ' || *p == '\t')
5585796c8dcSSimon Schubert {
5595796c8dcSSimon Schubert p++;
5605796c8dcSSimon Schubert }
5615796c8dcSSimon Schubert
5625796c8dcSSimon Schubert if (!c)
5635796c8dcSSimon Schubert {
5645796c8dcSSimon Schubert /* It is an unrecognized command. So there are no
5655796c8dcSSimon Schubert possible completions. */
5665796c8dcSSimon Schubert list = NULL;
5675796c8dcSSimon Schubert }
568c50c785cSJohn Marino else if (c == CMD_LIST_AMBIGUOUS)
5695796c8dcSSimon Schubert {
5705796c8dcSSimon Schubert char *q;
5715796c8dcSSimon Schubert
5725796c8dcSSimon Schubert /* lookup_cmd_1 advances p up to the first ambiguous thing, but
5735796c8dcSSimon Schubert doesn't advance over that thing itself. Do so now. */
5745796c8dcSSimon Schubert q = p;
5755796c8dcSSimon Schubert while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
5765796c8dcSSimon Schubert ++q;
5775796c8dcSSimon Schubert if (q != tmp_command + point)
5785796c8dcSSimon Schubert {
5795796c8dcSSimon Schubert /* There is something beyond the ambiguous
5805796c8dcSSimon Schubert command, so there are no possible completions. For
5815796c8dcSSimon Schubert example, "info t " or "info t foo" does not complete
5825796c8dcSSimon Schubert to anything, because "info t" can be "info target" or
5835796c8dcSSimon Schubert "info terminal". */
5845796c8dcSSimon Schubert list = NULL;
5855796c8dcSSimon Schubert }
5865796c8dcSSimon Schubert else
5875796c8dcSSimon Schubert {
5885796c8dcSSimon Schubert /* We're trying to complete on the command which was ambiguous.
5895796c8dcSSimon Schubert This we can deal with. */
5905796c8dcSSimon Schubert if (result_list)
5915796c8dcSSimon Schubert {
5925796c8dcSSimon Schubert if (reason != handle_brkchars)
5935796c8dcSSimon Schubert list = complete_on_cmdlist (*result_list->prefixlist, p,
594*ef5ccd6cSJohn Marino word, ignore_help_classes);
5955796c8dcSSimon Schubert }
5965796c8dcSSimon Schubert else
5975796c8dcSSimon Schubert {
5985796c8dcSSimon Schubert if (reason != handle_brkchars)
599*ef5ccd6cSJohn Marino list = complete_on_cmdlist (cmdlist, p, word,
600*ef5ccd6cSJohn Marino ignore_help_classes);
6015796c8dcSSimon Schubert }
6025796c8dcSSimon Schubert /* Ensure that readline does the right thing with respect to
6035796c8dcSSimon Schubert inserting quotes. */
6045796c8dcSSimon Schubert rl_completer_word_break_characters =
6055796c8dcSSimon Schubert gdb_completer_command_word_break_characters;
6065796c8dcSSimon Schubert }
6075796c8dcSSimon Schubert }
6085796c8dcSSimon Schubert else
6095796c8dcSSimon Schubert {
6105796c8dcSSimon Schubert /* We've recognized a full command. */
6115796c8dcSSimon Schubert
6125796c8dcSSimon Schubert if (p == tmp_command + point)
6135796c8dcSSimon Schubert {
614c50c785cSJohn Marino /* There is no non-whitespace in the line beyond the
615c50c785cSJohn Marino command. */
6165796c8dcSSimon Schubert
6175796c8dcSSimon Schubert if (p[-1] == ' ' || p[-1] == '\t')
6185796c8dcSSimon Schubert {
619c50c785cSJohn Marino /* The command is followed by whitespace; we need to
620c50c785cSJohn Marino complete on whatever comes after command. */
6215796c8dcSSimon Schubert if (c->prefixlist)
6225796c8dcSSimon Schubert {
6235796c8dcSSimon Schubert /* It is a prefix command; what comes after it is
6245796c8dcSSimon Schubert a subcommand (e.g. "info "). */
6255796c8dcSSimon Schubert if (reason != handle_brkchars)
626*ef5ccd6cSJohn Marino list = complete_on_cmdlist (*c->prefixlist, p, word,
627*ef5ccd6cSJohn Marino ignore_help_classes);
6285796c8dcSSimon Schubert
6295796c8dcSSimon Schubert /* Ensure that readline does the right thing
6305796c8dcSSimon Schubert with respect to inserting quotes. */
6315796c8dcSSimon Schubert rl_completer_word_break_characters =
6325796c8dcSSimon Schubert gdb_completer_command_word_break_characters;
6335796c8dcSSimon Schubert }
6345796c8dcSSimon Schubert else if (reason == handle_help)
6355796c8dcSSimon Schubert list = NULL;
6365796c8dcSSimon Schubert else if (c->enums)
6375796c8dcSSimon Schubert {
6385796c8dcSSimon Schubert if (reason != handle_brkchars)
6395796c8dcSSimon Schubert list = complete_on_enum (c->enums, p, word);
6405796c8dcSSimon Schubert rl_completer_word_break_characters =
6415796c8dcSSimon Schubert gdb_completer_command_word_break_characters;
6425796c8dcSSimon Schubert }
6435796c8dcSSimon Schubert else
6445796c8dcSSimon Schubert {
6455796c8dcSSimon Schubert /* It is a normal command; what comes after it is
6465796c8dcSSimon Schubert completed by the command's completer function. */
6475796c8dcSSimon Schubert if (c->completer == filename_completer)
6485796c8dcSSimon Schubert {
6495796c8dcSSimon Schubert /* Many commands which want to complete on
6505796c8dcSSimon Schubert file names accept several file names, as
6515796c8dcSSimon Schubert in "run foo bar >>baz". So we don't want
6525796c8dcSSimon Schubert to complete the entire text after the
6535796c8dcSSimon Schubert command, just the last word. To this
6545796c8dcSSimon Schubert end, we need to find the beginning of the
6555796c8dcSSimon Schubert file name by starting at `word' and going
6565796c8dcSSimon Schubert backwards. */
6575796c8dcSSimon Schubert for (p = word;
6585796c8dcSSimon Schubert p > tmp_command
6595796c8dcSSimon Schubert && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
6605796c8dcSSimon Schubert p--)
6615796c8dcSSimon Schubert ;
6625796c8dcSSimon Schubert rl_completer_word_break_characters =
6635796c8dcSSimon Schubert gdb_completer_file_name_break_characters;
6645796c8dcSSimon Schubert }
6655796c8dcSSimon Schubert else if (c->completer == location_completer)
6665796c8dcSSimon Schubert {
6675796c8dcSSimon Schubert /* Commands which complete on locations want to
6685796c8dcSSimon Schubert see the entire argument. */
6695796c8dcSSimon Schubert for (p = word;
6705796c8dcSSimon Schubert p > tmp_command
6715796c8dcSSimon Schubert && p[-1] != ' ' && p[-1] != '\t';
6725796c8dcSSimon Schubert p--)
6735796c8dcSSimon Schubert ;
6745796c8dcSSimon Schubert }
675cf7f2e2dSJohn Marino if (reason != handle_brkchars && c->completer != NULL)
6765796c8dcSSimon Schubert list = (*c->completer) (c, p, word);
6775796c8dcSSimon Schubert }
6785796c8dcSSimon Schubert }
6795796c8dcSSimon Schubert else
6805796c8dcSSimon Schubert {
6815796c8dcSSimon Schubert /* The command is not followed by whitespace; we need to
682c50c785cSJohn Marino complete on the command itself, e.g. "p" which is a
6835796c8dcSSimon Schubert command itself but also can complete to "print", "ptype"
6845796c8dcSSimon Schubert etc. */
6855796c8dcSSimon Schubert char *q;
6865796c8dcSSimon Schubert
6875796c8dcSSimon Schubert /* Find the command we are completing on. */
6885796c8dcSSimon Schubert q = p;
6895796c8dcSSimon Schubert while (q > tmp_command)
6905796c8dcSSimon Schubert {
6915796c8dcSSimon Schubert if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
6925796c8dcSSimon Schubert --q;
6935796c8dcSSimon Schubert else
6945796c8dcSSimon Schubert break;
6955796c8dcSSimon Schubert }
6965796c8dcSSimon Schubert
6975796c8dcSSimon Schubert if (reason != handle_brkchars)
698*ef5ccd6cSJohn Marino list = complete_on_cmdlist (result_list, q, word,
699*ef5ccd6cSJohn Marino ignore_help_classes);
7005796c8dcSSimon Schubert
7015796c8dcSSimon Schubert /* Ensure that readline does the right thing
7025796c8dcSSimon Schubert with respect to inserting quotes. */
7035796c8dcSSimon Schubert rl_completer_word_break_characters =
7045796c8dcSSimon Schubert gdb_completer_command_word_break_characters;
7055796c8dcSSimon Schubert }
7065796c8dcSSimon Schubert }
7075796c8dcSSimon Schubert else if (reason == handle_help)
7085796c8dcSSimon Schubert list = NULL;
7095796c8dcSSimon Schubert else
7105796c8dcSSimon Schubert {
7115796c8dcSSimon Schubert /* There is non-whitespace beyond the command. */
7125796c8dcSSimon Schubert
7135796c8dcSSimon Schubert if (c->prefixlist && !c->allow_unknown)
7145796c8dcSSimon Schubert {
7155796c8dcSSimon Schubert /* It is an unrecognized subcommand of a prefix command,
7165796c8dcSSimon Schubert e.g. "info adsfkdj". */
7175796c8dcSSimon Schubert list = NULL;
7185796c8dcSSimon Schubert }
7195796c8dcSSimon Schubert else if (c->enums)
7205796c8dcSSimon Schubert {
7215796c8dcSSimon Schubert if (reason != handle_brkchars)
7225796c8dcSSimon Schubert list = complete_on_enum (c->enums, p, word);
7235796c8dcSSimon Schubert }
7245796c8dcSSimon Schubert else
7255796c8dcSSimon Schubert {
7265796c8dcSSimon Schubert /* It is a normal command. */
7275796c8dcSSimon Schubert if (c->completer == filename_completer)
7285796c8dcSSimon Schubert {
7295796c8dcSSimon Schubert /* See the commentary above about the specifics
7305796c8dcSSimon Schubert of file-name completion. */
7315796c8dcSSimon Schubert for (p = word;
7325796c8dcSSimon Schubert p > tmp_command
733c50c785cSJohn Marino && strchr (gdb_completer_file_name_break_characters,
734c50c785cSJohn Marino p[-1]) == NULL;
7355796c8dcSSimon Schubert p--)
7365796c8dcSSimon Schubert ;
7375796c8dcSSimon Schubert rl_completer_word_break_characters =
7385796c8dcSSimon Schubert gdb_completer_file_name_break_characters;
7395796c8dcSSimon Schubert }
7405796c8dcSSimon Schubert else if (c->completer == location_completer)
7415796c8dcSSimon Schubert {
7425796c8dcSSimon Schubert for (p = word;
7435796c8dcSSimon Schubert p > tmp_command
7445796c8dcSSimon Schubert && p[-1] != ' ' && p[-1] != '\t';
7455796c8dcSSimon Schubert p--)
7465796c8dcSSimon Schubert ;
7475796c8dcSSimon Schubert }
748cf7f2e2dSJohn Marino if (reason != handle_brkchars && c->completer != NULL)
7495796c8dcSSimon Schubert list = (*c->completer) (c, p, word);
7505796c8dcSSimon Schubert }
7515796c8dcSSimon Schubert }
7525796c8dcSSimon Schubert }
7535796c8dcSSimon Schubert
7545796c8dcSSimon Schubert return list;
7555796c8dcSSimon Schubert }
756*ef5ccd6cSJohn Marino /* Generate completions all at once. Returns a vector of strings.
757*ef5ccd6cSJohn Marino Each element is allocated with xmalloc. It can also return NULL if
758*ef5ccd6cSJohn Marino there are no completions.
7595796c8dcSSimon Schubert
7605796c8dcSSimon Schubert TEXT is the caller's idea of the "word" we are looking at.
7615796c8dcSSimon Schubert
762c50c785cSJohn Marino LINE_BUFFER is available to be looked at; it contains the entire
763c50c785cSJohn Marino text of the line.
7645796c8dcSSimon Schubert
7655796c8dcSSimon Schubert POINT is the offset in that line of the cursor. You
7665796c8dcSSimon Schubert should pretend that the line ends at POINT. */
7675796c8dcSSimon Schubert
VEC(char_ptr)768*ef5ccd6cSJohn Marino VEC (char_ptr) *
7695796c8dcSSimon Schubert complete_line (const char *text, char *line_buffer, int point)
7705796c8dcSSimon Schubert {
771c50c785cSJohn Marino return complete_line_internal (text, line_buffer,
772c50c785cSJohn Marino point, handle_completions);
7735796c8dcSSimon Schubert }
7745796c8dcSSimon Schubert
7755796c8dcSSimon Schubert /* Complete on command names. Used by "help". */
VEC(char_ptr)776*ef5ccd6cSJohn Marino VEC (char_ptr) *
777c50c785cSJohn Marino command_completer (struct cmd_list_element *ignore,
778c50c785cSJohn Marino char *text, char *word)
7795796c8dcSSimon Schubert {
780c50c785cSJohn Marino return complete_line_internal (word, text,
781c50c785cSJohn Marino strlen (text), handle_help);
7825796c8dcSSimon Schubert }
7835796c8dcSSimon Schubert
784*ef5ccd6cSJohn Marino /* Complete on signals. */
785*ef5ccd6cSJohn Marino
VEC(char_ptr)786*ef5ccd6cSJohn Marino VEC (char_ptr) *
787*ef5ccd6cSJohn Marino signal_completer (struct cmd_list_element *ignore,
788*ef5ccd6cSJohn Marino char *text, char *word)
789*ef5ccd6cSJohn Marino {
790*ef5ccd6cSJohn Marino VEC (char_ptr) *return_val = NULL;
791*ef5ccd6cSJohn Marino size_t len = strlen (word);
792*ef5ccd6cSJohn Marino enum gdb_signal signum;
793*ef5ccd6cSJohn Marino const char *signame;
794*ef5ccd6cSJohn Marino
795*ef5ccd6cSJohn Marino for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum)
796*ef5ccd6cSJohn Marino {
797*ef5ccd6cSJohn Marino /* Can't handle this, so skip it. */
798*ef5ccd6cSJohn Marino if (signum == GDB_SIGNAL_0)
799*ef5ccd6cSJohn Marino continue;
800*ef5ccd6cSJohn Marino
801*ef5ccd6cSJohn Marino signame = gdb_signal_to_name (signum);
802*ef5ccd6cSJohn Marino
803*ef5ccd6cSJohn Marino /* Ignore the unknown signal case. */
804*ef5ccd6cSJohn Marino if (!signame || strcmp (signame, "?") == 0)
805*ef5ccd6cSJohn Marino continue;
806*ef5ccd6cSJohn Marino
807*ef5ccd6cSJohn Marino if (strncasecmp (signame, word, len) == 0)
808*ef5ccd6cSJohn Marino VEC_safe_push (char_ptr, return_val, xstrdup (signame));
809*ef5ccd6cSJohn Marino }
810*ef5ccd6cSJohn Marino
811*ef5ccd6cSJohn Marino return return_val;
812*ef5ccd6cSJohn Marino }
813*ef5ccd6cSJohn Marino
8145796c8dcSSimon Schubert /* Get the list of chars that are considered as word breaks
8155796c8dcSSimon Schubert for the current command. */
8165796c8dcSSimon Schubert
8175796c8dcSSimon Schubert char *
gdb_completion_word_break_characters(void)8185796c8dcSSimon Schubert gdb_completion_word_break_characters (void)
8195796c8dcSSimon Schubert {
820*ef5ccd6cSJohn Marino VEC (char_ptr) *list;
821cf7f2e2dSJohn Marino
8225796c8dcSSimon Schubert list = complete_line_internal (rl_line_buffer, rl_line_buffer, rl_point,
8235796c8dcSSimon Schubert handle_brkchars);
8245796c8dcSSimon Schubert gdb_assert (list == NULL);
8255796c8dcSSimon Schubert return rl_completer_word_break_characters;
8265796c8dcSSimon Schubert }
8275796c8dcSSimon Schubert
828c50c785cSJohn Marino /* Generate completions one by one for the completer. Each time we
829c50c785cSJohn Marino are called return another potential completion to the caller.
830c50c785cSJohn Marino line_completion just completes on commands or passes the buck to
831c50c785cSJohn Marino the command's completer function, the stuff specific to symbol
832c50c785cSJohn Marino completion is in make_symbol_completion_list.
8335796c8dcSSimon Schubert
8345796c8dcSSimon Schubert TEXT is the caller's idea of the "word" we are looking at.
8355796c8dcSSimon Schubert
836c50c785cSJohn Marino MATCHES is the number of matches that have currently been collected
837c50c785cSJohn Marino from calling this completion function. When zero, then we need to
838c50c785cSJohn Marino initialize, otherwise the initialization has already taken place
839c50c785cSJohn Marino and we can just return the next potential completion string.
8405796c8dcSSimon Schubert
841c50c785cSJohn Marino LINE_BUFFER is available to be looked at; it contains the entire
842c50c785cSJohn Marino text of the line. POINT is the offset in that line of the cursor.
843c50c785cSJohn Marino You should pretend that the line ends at POINT.
8445796c8dcSSimon Schubert
845c50c785cSJohn Marino Returns NULL if there are no more completions, else a pointer to a
846c50c785cSJohn Marino string which is a possible completion, it is the caller's
847c50c785cSJohn Marino responsibility to free the string. */
8485796c8dcSSimon Schubert
8495796c8dcSSimon Schubert static char *
line_completion_function(const char * text,int matches,char * line_buffer,int point)8505796c8dcSSimon Schubert line_completion_function (const char *text, int matches,
8515796c8dcSSimon Schubert char *line_buffer, int point)
8525796c8dcSSimon Schubert {
853*ef5ccd6cSJohn Marino static VEC (char_ptr) *list = NULL; /* Cache of completions. */
8545796c8dcSSimon Schubert static int index; /* Next cached completion. */
8555796c8dcSSimon Schubert char *output = NULL;
8565796c8dcSSimon Schubert
8575796c8dcSSimon Schubert if (matches == 0)
8585796c8dcSSimon Schubert {
859c50c785cSJohn Marino /* The caller is beginning to accumulate a new set of
860c50c785cSJohn Marino completions, so we need to find all of them now, and cache
861c50c785cSJohn Marino them for returning one at a time on future calls. */
8625796c8dcSSimon Schubert
8635796c8dcSSimon Schubert if (list)
8645796c8dcSSimon Schubert {
865c50c785cSJohn Marino /* Free the storage used by LIST, but not by the strings
866c50c785cSJohn Marino inside. This is because rl_complete_internal () frees
867c50c785cSJohn Marino the strings. As complete_line may abort by calling
868c50c785cSJohn Marino `error' clear LIST now. */
869*ef5ccd6cSJohn Marino VEC_free (char_ptr, list);
8705796c8dcSSimon Schubert }
8715796c8dcSSimon Schubert index = 0;
8725796c8dcSSimon Schubert list = complete_line (text, line_buffer, point);
8735796c8dcSSimon Schubert }
8745796c8dcSSimon Schubert
875c50c785cSJohn Marino /* If we found a list of potential completions during initialization
876*ef5ccd6cSJohn Marino then dole them out one at a time. After returning the last one,
877*ef5ccd6cSJohn Marino return NULL (and continue to do so) each time we are called after
878*ef5ccd6cSJohn Marino that, until a new list is available. */
8795796c8dcSSimon Schubert
8805796c8dcSSimon Schubert if (list)
8815796c8dcSSimon Schubert {
882*ef5ccd6cSJohn Marino if (index < VEC_length (char_ptr, list))
8835796c8dcSSimon Schubert {
884*ef5ccd6cSJohn Marino output = VEC_index (char_ptr, list, index);
8855796c8dcSSimon Schubert index++;
8865796c8dcSSimon Schubert }
8875796c8dcSSimon Schubert }
8885796c8dcSSimon Schubert
8895796c8dcSSimon Schubert #if 0
8905796c8dcSSimon Schubert /* Can't do this because readline hasn't yet checked the word breaks
8915796c8dcSSimon Schubert for figuring out whether to insert a quote. */
8925796c8dcSSimon Schubert if (output == NULL)
893c50c785cSJohn Marino /* Make sure the word break characters are set back to normal for
894c50c785cSJohn Marino the next time that readline tries to complete something. */
8955796c8dcSSimon Schubert rl_completer_word_break_characters =
8965796c8dcSSimon Schubert current_language->la_word_break_characters();
8975796c8dcSSimon Schubert #endif
8985796c8dcSSimon Schubert
8995796c8dcSSimon Schubert return (output);
9005796c8dcSSimon Schubert }
9015796c8dcSSimon Schubert
9025796c8dcSSimon Schubert /* Skip over the possibly quoted word STR (as defined by the quote
903c50c785cSJohn Marino characters QUOTECHARS and the word break characters BREAKCHARS).
904c50c785cSJohn Marino Returns pointer to the location after the "word". If either
905c50c785cSJohn Marino QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
906c50c785cSJohn Marino completer. */
9075796c8dcSSimon Schubert
9085796c8dcSSimon Schubert char *
skip_quoted_chars(char * str,char * quotechars,char * breakchars)9095796c8dcSSimon Schubert skip_quoted_chars (char *str, char *quotechars, char *breakchars)
9105796c8dcSSimon Schubert {
9115796c8dcSSimon Schubert char quote_char = '\0';
9125796c8dcSSimon Schubert char *scan;
9135796c8dcSSimon Schubert
9145796c8dcSSimon Schubert if (quotechars == NULL)
9155796c8dcSSimon Schubert quotechars = gdb_completer_quote_characters;
9165796c8dcSSimon Schubert
9175796c8dcSSimon Schubert if (breakchars == NULL)
9185796c8dcSSimon Schubert breakchars = current_language->la_word_break_characters();
9195796c8dcSSimon Schubert
9205796c8dcSSimon Schubert for (scan = str; *scan != '\0'; scan++)
9215796c8dcSSimon Schubert {
9225796c8dcSSimon Schubert if (quote_char != '\0')
9235796c8dcSSimon Schubert {
9245796c8dcSSimon Schubert /* Ignore everything until the matching close quote char. */
9255796c8dcSSimon Schubert if (*scan == quote_char)
9265796c8dcSSimon Schubert {
9275796c8dcSSimon Schubert /* Found matching close quote. */
9285796c8dcSSimon Schubert scan++;
9295796c8dcSSimon Schubert break;
9305796c8dcSSimon Schubert }
9315796c8dcSSimon Schubert }
9325796c8dcSSimon Schubert else if (strchr (quotechars, *scan))
9335796c8dcSSimon Schubert {
9345796c8dcSSimon Schubert /* Found start of a quoted string. */
9355796c8dcSSimon Schubert quote_char = *scan;
9365796c8dcSSimon Schubert }
9375796c8dcSSimon Schubert else if (strchr (breakchars, *scan))
9385796c8dcSSimon Schubert {
9395796c8dcSSimon Schubert break;
9405796c8dcSSimon Schubert }
9415796c8dcSSimon Schubert }
9425796c8dcSSimon Schubert
9435796c8dcSSimon Schubert return (scan);
9445796c8dcSSimon Schubert }
9455796c8dcSSimon Schubert
9465796c8dcSSimon Schubert /* Skip over the possibly quoted word STR (as defined by the quote
9475796c8dcSSimon Schubert characters and word break characters used by the completer).
9485796c8dcSSimon Schubert Returns pointer to the location after the "word". */
9495796c8dcSSimon Schubert
9505796c8dcSSimon Schubert char *
skip_quoted(char * str)9515796c8dcSSimon Schubert skip_quoted (char *str)
9525796c8dcSSimon Schubert {
9535796c8dcSSimon Schubert return skip_quoted_chars (str, NULL, NULL);
9545796c8dcSSimon Schubert }
955