1 This file is complete.def, from which is created complete.c.
2 It implements the builtins "complete" and "compgen" in Bash.
4 Copyright (C) 1999 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $DEPENDS_ON PROGRAMMABLE_COMPLETION
26 $FUNCTION complete_builtin
27 $SHORT_DOC complete [-abcdefgjkvu] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]
28 For each NAME, specify how arguments are to be completed.
29 If the -p option is supplied, or if no options are supplied, existing
30 completion specifications are printed in a way that allows them to be
31 reused as input. The -r option removes a completion specification for
32 each NAME, or, if no NAMEs are supplied, all completion specifications.
39 #include "../bashtypes.h"
41 #if defined (HAVE_UNISTD_H)
45 #include "../bashansi.h"
48 #include "../builtins.h"
49 #include "../pcomplete.h"
52 #include "bashgetopt.h"
54 #include <readline/readline.h>
56 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
58 static int find_compact __P((char *));
59 static int find_compopt __P((char *));
61 static int build_actions __P((WORD_LIST *, int *, int *, unsigned long *, unsigned long *));
63 static int remove_cmd_completions __P((WORD_LIST *));
65 static void print_one_completion __P((char *, COMPSPEC *));
66 static void print_all_completions __P((void));
67 static int print_cmd_completions __P((WORD_LIST *));
69 static char *Aarg, *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg;
71 static struct _compacts {
76 { "alias", CA_ALIAS, 'a' },
77 { "arrayvar", CA_ARRAYVAR, 0 },
78 { "binding", CA_BINDING, 0 },
79 { "builtin", CA_BUILTIN, 'b' },
80 { "command", CA_COMMAND, 'c' },
81 { "directory", CA_DIRECTORY, 'd' },
82 { "disabled", CA_DISABLED, 0 },
83 { "enabled", CA_ENABLED, 0 },
84 { "export", CA_EXPORT, 'e' },
85 { "file", CA_FILE, 'f' },
86 { "function", CA_FUNCTION, 0 },
87 { "helptopic", CA_BUILTIN, 0 }, /* for now */
88 { "hostname", CA_HOSTNAME, 0 },
89 { "group", CA_GROUP, 'g' },
90 { "job", CA_JOB, 'j' },
91 { "keyword", CA_KEYWORD, 'k' },
92 { "running", CA_RUNNING, 0 },
93 { "setopt", CA_SETOPT, 0 },
94 { "shopt", CA_SHOPT, 0 },
95 { "signal", CA_SIGNAL, 0 },
96 { "stopped", CA_STOPPED, 0 },
97 { "user", CA_USER, 'u' },
98 { "variable", CA_VARIABLE, 'v' },
99 { (char *)NULL, 0, 0 },
102 static struct _compopt {
106 { "default", COPT_DEFAULT },
107 { "dirnames", COPT_DIRNAMES },
108 { "filenames",COPT_FILENAMES},
118 for (i = 0; compacts[i].actname; i++)
119 if (STREQ (name, compacts[i].actname))
130 for (i = 0; compopts[i].optname; i++)
131 if (STREQ (name, compopts[i].optname))
136 /* Build the actions and compspec options from the options specified in LIST.
137 ACTP is a pointer to an unsigned long in which to place the bitmap of
138 actions. OPTP is a pointer to an unsigned long in which to place the
139 btmap of compspec options (arguments to `-o'). PP, if non-null, gets 1
140 if -p is supplied; RP, if non-null, gets 1 if -r is supplied.
141 If either is null, the corresponding option generates an error.
142 This also sets variables corresponding to options that take arguments as
143 a side effect; the caller should ensure that those variables are set to
144 NULL before calling build_actions. Return value:
145 EX_USAGE = bad option
146 EXECUTION_SUCCESS = some options supplied
147 EXECUTION_FAILURE = no options supplied
151 build_actions (list, pp, rp, actp, optp)
154 unsigned long *actp, *optp;
156 int opt, ind, opt_given;
157 unsigned long acts, copts;
159 acts = copts = (unsigned long)0L;
162 reset_internal_getopt ();
163 while ((opt = internal_getopt (list, "abcdefgjko:pruvA:G:W:P:S:X:F:C:")) != -1)
176 builtin_error ("illegal option: -r");
189 builtin_error ("illegal option: -p");
204 acts |= CA_DIRECTORY;
228 ind = find_compopt (list_optarg);
231 builtin_error ("%s: invalid option name", list_optarg);
234 copts |= compopts[ind].optflag;
237 ind = find_compact (list_optarg);
240 builtin_error ("%s: invalid action name", list_optarg);
243 acts |= compacts[ind].actflag;
275 return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
278 /* Add, remove, and display completion specifiers. */
280 complete_builtin (list)
283 int opt_given, pflag, rflag, rval;
284 unsigned long acts, copts;
290 print_all_completions ();
291 return (EXECUTION_SUCCESS);
294 opt_given = pflag = rflag = 0;
295 acts = copts = (unsigned long)0L;
296 Aarg = Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
297 cs = (COMPSPEC *)NULL;
299 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
300 as a side effect if they are supplied as options. */
301 rval = build_actions (list, &pflag, &rflag, &acts, &copts);
302 if (rval == EX_USAGE)
304 opt_given = rval != EXECUTION_FAILURE;
308 /* -p overrides everything else */
309 if (pflag || (list == 0 && opt_given == 0))
313 print_all_completions ();
314 return (EXECUTION_SUCCESS);
316 return (print_cmd_completions (list));
319 /* next, -r overrides everything else. */
325 return (EXECUTION_SUCCESS);
327 return (remove_cmd_completions (list));
330 if (list == 0 && opt_given)
336 /* If we get here, we need to build a compspec and add it for each
337 remaining argument. */
338 cs = alloc_compspec ();
342 cs->globpat = STRDUP (Garg);
343 cs->words = STRDUP (Warg);
344 cs->prefix = STRDUP (Parg);
345 cs->suffix = STRDUP (Sarg);
346 cs->funcname = STRDUP (Farg);
347 cs->command = STRDUP (Carg);
348 cs->filterpat = STRDUP (Xarg);
350 for (rval = EXECUTION_SUCCESS ; list; list = list->next)
352 /* Add CS as the compspec for the specified commands. */
353 cmd = list->word->word;
354 if (add_progcomp (cmd, cs) == 0)
355 rval = EXECUTION_FAILURE;
362 remove_cmd_completions (list)
368 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
370 if (remove_progcomp (l->word->word) == 0)
372 builtin_error ("%s: no completion specification", l->word->word);
373 ret = EXECUTION_FAILURE;
379 #define SQPRINTARG(a, f) \
383 x = sh_single_quote (a); \
384 printf ("%s %s ", f, x); \
389 #define PRINTARG(a, f) \
392 printf ("%s %s ", f, a); \
395 #define PRINTOPT(a, f) \
401 #define PRINTACT(a, f) \
404 printf ("-A %s ", f); \
407 #define PRINTCOMPOPT(a, f) \
410 printf ("-o %s ", f); \
414 print_one_completion (cmd, cs)
418 unsigned long acts, copts;
421 printf ("complete ");
425 /* First, print the -o options. */
426 PRINTCOMPOPT (COPT_DEFAULT, "default");
427 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
428 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
432 /* simple flags next */
433 PRINTOPT (CA_ALIAS, "-a");
434 PRINTOPT (CA_BUILTIN, "-b");
435 PRINTOPT (CA_COMMAND, "-c");
436 PRINTOPT (CA_DIRECTORY, "-d");
437 PRINTOPT (CA_EXPORT, "-e");
438 PRINTOPT (CA_FILE, "-f");
439 PRINTOPT (CA_GROUP, "-g");
440 PRINTOPT (CA_KEYWORD, "-k");
441 PRINTOPT (CA_JOB, "-j");
442 PRINTOPT (CA_USER, "-u");
443 PRINTOPT (CA_VARIABLE, "-v");
445 /* now the rest of the actions */
446 PRINTACT (CA_ARRAYVAR, "arrayvar");
447 PRINTACT (CA_BINDING, "binding");
448 PRINTACT (CA_DISABLED, "disabled");
449 PRINTACT (CA_ENABLED, "enabled");
450 PRINTACT (CA_FUNCTION, "function");
451 PRINTACT (CA_HELPTOPIC, "helptopic");
452 PRINTACT (CA_HOSTNAME, "hostname");
453 PRINTACT (CA_RUNNING, "running");
454 PRINTACT (CA_SETOPT, "setopt");
455 PRINTACT (CA_SHOPT, "shopt");
456 PRINTACT (CA_SIGNAL, "signal");
457 PRINTACT (CA_STOPPED, "stopped");
459 /* now the rest of the arguments */
461 /* arguments that require quoting */
462 SQPRINTARG (cs->globpat, "-G");
463 SQPRINTARG (cs->words, "-W");
464 SQPRINTARG (cs->prefix, "-P");
465 SQPRINTARG (cs->suffix, "-S");
466 SQPRINTARG (cs->filterpat, "-X");
468 /* simple arguments that don't require quoting */
469 PRINTARG (cs->funcname, "-F");
470 PRINTARG (cs->command, "-C");
472 printf ("%s\n", cmd);
476 print_all_completions ()
478 print_all_compspecs (print_one_completion);
482 print_cmd_completions (list)
489 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
491 cs = find_compspec (l->word->word);
493 print_one_completion (l->word->word, cs);
496 builtin_error ("%s: no completion specification", l->word->word);
497 ret = EXECUTION_FAILURE;
504 $DEPENDS_ON PROGRAMMABLE_COMPLETION
505 $FUNCTION compgen_builtin
506 $SHORT_DOC compgen [-abcdefgjkvu] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [word]
507 Display the possible completions depending on the options. Intended
508 to be used from within a shell function generating possible completions.
509 If the optional WORD argument is supplied, matches against WORD are
514 compgen_builtin (list)
518 unsigned long acts, copts;
524 return (EXECUTION_SUCCESS);
526 acts = copts = (unsigned long)0L;
527 Aarg = Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
528 cs = (COMPSPEC *)NULL;
530 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
531 as a side effect if they are supplied as options. */
532 rval = build_actions (list, (int *)NULL, (int *)NULL, &acts, &copts);
533 if (rval == EX_USAGE)
535 if (rval == EXECUTION_FAILURE)
536 return (EXECUTION_SUCCESS);
540 word = (list && list->word) ? list->word->word : "";
543 internal_warning ("compgen: -F option may not work as you expect");
545 internal_warning ("compgen: -C option may not work as you expect");
547 /* If we get here, we need to build a compspec and evaluate it. */
548 cs = alloc_compspec ();
553 cs->globpat = STRDUP (Garg);
554 cs->words = STRDUP (Warg);
555 cs->prefix = STRDUP (Parg);
556 cs->suffix = STRDUP (Sarg);
557 cs->funcname = STRDUP (Farg);
558 cs->command = STRDUP (Carg);
559 cs->filterpat = STRDUP (Xarg);
561 rval = EXECUTION_FAILURE;
562 sl = gen_compspec_completions (cs, "compgen", word, 0, 0);
564 /* This isn't perfect, but it's the best we can do, given what readline
565 exports from its set of completion utility functions. */
566 if ((sl == 0 || sl->list_len == 0) && (copts & COPT_DEFAULT))
570 matches = rl_completion_matches (word, rl_filename_completion_function);
571 sl = completions_to_stringlist (matches);
572 free_array (matches);
577 if (sl->list && sl->list_len)
579 rval = EXECUTION_SUCCESS;
580 print_stringlist (sl, (char *)NULL);
582 free_stringlist (sl);