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 [-abcdefjkvu] [-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 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
56 static int remove_cmd_completions ();
58 static void print_all_completions ();
59 static int print_cmd_completions ();
61 static char *Aarg, *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg;
63 static struct _compacts {
68 { "alias", CA_ALIAS, 'a' },
69 { "arrayvar", CA_ARRAYVAR, 0 },
70 { "binding", CA_BINDING, 0 },
71 { "builtin", CA_BUILTIN, 'b' },
72 { "command", CA_COMMAND, 'c' },
73 { "directory", CA_DIRECTORY, 'd' },
74 { "disabled", CA_DISABLED, 0 },
75 { "enabled", CA_ENABLED, 0 },
76 { "export", CA_EXPORT, 'e' },
77 { "file", CA_FILE, 'f' },
78 { "function", CA_FUNCTION, 0 },
79 { "helptopic", CA_BUILTIN, 0 }, /* for now */
80 { "hostname", CA_HOSTNAME, 0 },
81 { "job", CA_JOB, 'j' },
82 { "keyword", CA_KEYWORD, 'k' },
83 { "running", CA_RUNNING, 0 },
84 { "setopt", CA_SETOPT, 0 },
85 { "shopt", CA_SHOPT, 0 },
86 { "signal", CA_SIGNAL, 0 },
87 { "stopped", CA_STOPPED, 0 },
88 { "user", CA_USER, 'u' },
89 { "variable", CA_VARIABLE, 'v' },
90 { (char *)NULL, 0, 0 },
93 static struct _compopt {
97 { "default", COPT_DEFAULT },
98 { "dirnames", COPT_DIRNAMES },
99 { "filenames",COPT_FILENAMES},
109 for (i = 0; compacts[i].actname; i++)
110 if (STREQ (name, compacts[i].actname))
121 for (i = 0; compopts[i].optname; i++)
122 if (STREQ (name, compopts[i].optname))
127 /* Build the actions and compspec options from the options specified in LIST.
128 ACTP is a pointer to an unsigned long in which to place the bitmap of
129 actions. OPTP is a pointer to an unsigned long in which to place the
130 btmap of compspec options (arguments to `-o'). PP, if non-null, gets 1
131 if -p is supplied; RP, if non-null, gets 1 if -r is supplied.
132 If either is null, the corresponding option generates an error.
133 This also sets variables corresponding to options that take arguments as
134 a side effect; the caller should ensure that those variables are set to
135 NULL before calling build_actions. Return value:
136 EX_USAGE = bad option
137 EXECUTION_SUCCESS = some options supplied
138 EXECUTION_FAILURE = no options supplied
142 build_actions (list, pp, rp, actp, optp)
145 unsigned long *actp, *optp;
147 int opt, ind, pflag, rflag, opt_given;
148 unsigned long acts, copts;
150 acts = copts = (unsigned long)0L;
153 reset_internal_getopt ();
154 while ((opt = internal_getopt (list, "abcdefjko:pruvA:G:W:P:S:X:F:C:")) != -1)
167 builtin_error ("illegal option: -r");
180 builtin_error ("illegal option: -p");
195 acts |= CA_DIRECTORY;
216 ind = find_compopt (list_optarg);
219 builtin_error ("%s: invalid option name", list_optarg);
222 copts |= compopts[ind].optflag;
225 ind = find_compact (list_optarg);
228 builtin_error ("%s: invalid action name", list_optarg);
231 acts |= compacts[ind].actflag;
263 return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
266 /* Add, remove, and display completion specifiers. */
268 complete_builtin (list)
271 int opt_given, pflag, rflag, rval;
272 unsigned long acts, copts;
278 print_all_completions ();
279 return (EXECUTION_SUCCESS);
282 opt_given = pflag = rflag = 0;
283 acts = copts = (unsigned long)0L;
284 Aarg = Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
285 cs = (COMPSPEC *)NULL;
287 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
288 as a side effect if they are supplied as options. */
289 rval = build_actions (list, &pflag, &rflag, &acts, &copts);
290 if (rval == EX_USAGE)
292 opt_given = rval != EXECUTION_FAILURE;
296 /* -p overrides everything else */
297 if (pflag || (list == 0 && opt_given == 0))
301 print_all_completions ();
302 return (EXECUTION_SUCCESS);
304 return (print_cmd_completions (list));
307 /* next, -r overrides everything else. */
313 return (EXECUTION_SUCCESS);
315 return (remove_cmd_completions (list));
318 if (list == 0 && opt_given)
324 /* If we get here, we need to build a compspec and add it for each
325 remaining argument. */
326 cs = alloc_compspec ();
330 cs->globpat = STRDUP (Garg);
331 cs->words = STRDUP (Warg);
332 cs->prefix = STRDUP (Parg);
333 cs->suffix = STRDUP (Sarg);
334 cs->funcname = STRDUP (Farg);
335 cs->command = STRDUP (Carg);
336 cs->filterpat = STRDUP (Xarg);
338 for (rval = EXECUTION_SUCCESS ; list; list = list->next)
340 /* Add CS as the compspec for the specified commands. */
341 cmd = list->word->word;
342 if (add_progcomp (cmd, cs) == 0)
343 rval = EXECUTION_FAILURE;
350 remove_cmd_completions (list)
356 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
358 if (remove_progcomp (l->word->word) == 0)
360 builtin_error ("%s: no completion specification", l->word->word);
361 ret = EXECUTION_FAILURE;
367 #define SQPRINTARG(a, f) \
371 x = sh_single_quote (a); \
372 printf ("%s %s ", f, x); \
377 #define PRINTARG(a, f) \
380 printf ("%s %s ", f, a); \
383 #define PRINTOPT(a, f) \
389 #define PRINTACT(a, f) \
392 printf ("-A %s ", f); \
395 #define PRINTCOMPOPT(a, f) \
398 printf ("-o %s ", f); \
402 print_one_completion (cmd, cs)
406 unsigned long acts, copts;
409 printf ("complete ");
413 /* First, print the -o options. */
414 PRINTCOMPOPT (COPT_DEFAULT, "default");
415 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
416 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
420 /* simple flags next */
421 PRINTOPT (CA_ALIAS, "-a");
422 PRINTOPT (CA_BUILTIN, "-b");
423 PRINTOPT (CA_COMMAND, "-c");
424 PRINTOPT (CA_DIRECTORY, "-d");
425 PRINTOPT (CA_EXPORT, "-e");
426 PRINTOPT (CA_FILE, "-f");
427 PRINTOPT (CA_KEYWORD, "-k");
428 PRINTOPT (CA_JOB, "-j");
429 PRINTOPT (CA_USER, "-u");
430 PRINTOPT (CA_VARIABLE, "-v");
432 /* now the rest of the actions */
433 PRINTACT (CA_ARRAYVAR, "arrayvar");
434 PRINTACT (CA_BINDING, "binding");
435 PRINTACT (CA_DISABLED, "disabled");
436 PRINTACT (CA_ENABLED, "enabled");
437 PRINTACT (CA_FUNCTION, "function");
438 PRINTACT (CA_HELPTOPIC, "helptopic");
439 PRINTACT (CA_HOSTNAME, "hostname");
440 PRINTACT (CA_RUNNING, "running");
441 PRINTACT (CA_SETOPT, "setopt");
442 PRINTACT (CA_SHOPT, "shopt");
443 PRINTACT (CA_SIGNAL, "signal");
444 PRINTACT (CA_STOPPED, "stopped");
446 /* now the rest of the arguments */
448 /* arguments that require quoting */
449 SQPRINTARG (cs->globpat, "-G");
450 SQPRINTARG (cs->words, "-W");
451 SQPRINTARG (cs->prefix, "-P");
452 SQPRINTARG (cs->suffix, "-S");
453 SQPRINTARG (cs->filterpat, "-X");
455 /* simple arguments that don't require quoting */
456 PRINTARG (cs->funcname, "-F");
457 PRINTARG (cs->command, "-C");
459 printf ("%s\n", cmd);
463 print_all_completions ()
465 print_all_compspecs (print_one_completion);
469 print_cmd_completions (list)
476 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
478 cs = find_compspec (l->word->word);
480 print_one_completion (l->word->word, cs);
483 builtin_error ("%s: no completion specification", l->word->word);
484 ret = EXECUTION_FAILURE;
491 $DEPENDS_ON PROGRAMMABLE_COMPLETION
492 $FUNCTION compgen_builtin
493 $SHORT_DOC compgen [-abcdefjkvu] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [word]
494 Display the possible completions depending on the options. Intended
495 to be used from within a shell function generating possible completions.
496 If the optional WORD argument is supplied, matches against WORD are
501 compgen_builtin (list)
505 unsigned long acts, copts;
511 return (EXECUTION_SUCCESS);
513 acts = copts = (unsigned long)0L;
514 Aarg = Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
515 cs = (COMPSPEC *)NULL;
517 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
518 as a side effect if they are supplied as options. */
519 rval = build_actions (list, (int *)NULL, (int *)NULL, &acts, &copts);
520 if (rval == EX_USAGE)
522 if (rval == EXECUTION_FAILURE)
523 return (EXECUTION_SUCCESS);
527 word = (list && list->word) ? list->word->word : "";
530 internal_warning ("compgen: -F option may not work as you expect");
532 internal_warning ("compgen: -C option may not work as you expect");
534 /* If we get here, we need to build a compspec and evaluate it. */
535 cs = alloc_compspec ();
540 cs->globpat = STRDUP (Garg);
541 cs->words = STRDUP (Warg);
542 cs->prefix = STRDUP (Parg);
543 cs->suffix = STRDUP (Sarg);
544 cs->funcname = STRDUP (Farg);
545 cs->command = STRDUP (Carg);
546 cs->filterpat = STRDUP (Xarg);
548 rval = EXECUTION_FAILURE;
549 sl = gen_compspec_completions (cs, "compgen", word, 0, 0);
554 rval = EXECUTION_SUCCESS;
555 print_stringlist (sl, (char *)NULL);
557 free_stringlist (sl);