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] [-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 },
99 for (i = 0; compacts[i].actname; i++)
100 if (STREQ (name, compacts[i].actname))
105 /* Build the actions from the options specified in LIST. ACTP is a pointer
106 to an unsigned long in which to place the bitmap of actions. PP, if
107 non-null, gets 1 if -p is supplied; RP, if non-null, gets 1 if -r is
108 supplied. If either is null, the corresponding option generates an
109 error. This also sets variables corresponding to options that take
110 arguments as a side effect; the caller should ensure that those variables
111 are set to NULL before calling build_actions. Return value:
112 EX_USAGE = bad option
113 EXECUTION_SUCCESS = some options supplied
114 EXECUTION_FAILURE = no options supplied
118 build_actions (list, pp, rp, actp)
123 int opt, ind, pflag, rflag, opt_given;
126 acts = (unsigned long)0L;
129 reset_internal_getopt ();
130 while ((opt = internal_getopt (list, "abcdefjkpruvA:G:W:P:S:X:F:C:")) != -1)
143 builtin_error ("illegal option: -r");
156 builtin_error ("illegal option: -p");
171 acts |= CA_DIRECTORY;
192 ind = find_compact (list_optarg);
195 builtin_error ("%s: invalid action name", list_optarg);
198 acts |= compacts[ind].actflag;
228 return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
231 /* Add, remove, and display completion specifiers. */
233 complete_builtin (list)
236 int opt_given, pflag, rflag, rval;
243 print_all_completions ();
244 return (EXECUTION_SUCCESS);
247 opt_given = pflag = rflag = 0;
248 acts = (unsigned long)0L;
249 Aarg = Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
250 cs = (COMPSPEC *)NULL;
252 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
253 as a side effect if they are supplied as options. */
254 rval = build_actions (list, &pflag, &rflag, &acts);
255 if (rval == EX_USAGE)
257 opt_given = rval != EXECUTION_FAILURE;
261 /* -p overrides everything else */
262 if (pflag || (list == 0 && opt_given == 0))
266 print_all_completions ();
267 return (EXECUTION_SUCCESS);
269 return (print_cmd_completions (list));
272 /* next, -r overrides everything else. */
278 return (EXECUTION_SUCCESS);
280 return (remove_cmd_completions (list));
283 if (list == 0 && opt_given)
289 /* If we get here, we need to build a compspec and add it for each
290 remaining argument. */
291 cs = alloc_compspec ();
294 cs->globpat = STRDUP (Garg);
295 cs->words = STRDUP (Warg);
296 cs->prefix = STRDUP (Parg);
297 cs->suffix = STRDUP (Sarg);
298 cs->funcname = STRDUP (Farg);
299 cs->command = STRDUP (Carg);
300 cs->filterpat = STRDUP (Xarg);
302 for (rval = EXECUTION_SUCCESS ; list; list = list->next)
304 /* Add CS as the compspec for the specified commands. */
305 cmd = list->word->word;
306 if (add_progcomp (cmd, cs) == 0)
307 rval = EXECUTION_FAILURE;
314 remove_cmd_completions (list)
320 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
322 if (remove_progcomp (l->word->word) == 0)
324 builtin_error ("%s: no completion specification", l->word->word);
325 ret = EXECUTION_FAILURE;
331 #define SQPRINTARG(a, f) \
335 x = single_quote (a); \
336 printf ("%s %s ", f, x); \
341 #define PRINTARG(a, f) \
344 printf ("%s %s ", f, a); \
347 #define PRINTOPT(a, f) \
353 #define PRINTACT(a, f) \
356 printf ("-A %s ", f); \
360 print_one_completion (cmd, cs)
367 printf ("complete ");
371 /* simple flags first */
372 PRINTOPT (CA_ALIAS, "-a");
373 PRINTOPT (CA_BUILTIN, "-b");
374 PRINTOPT (CA_COMMAND, "-c");
375 PRINTOPT (CA_DIRECTORY, "-d");
376 PRINTOPT (CA_EXPORT, "-e");
377 PRINTOPT (CA_FILE, "-f");
378 PRINTOPT (CA_KEYWORD, "-k");
379 PRINTOPT (CA_JOB, "-j");
380 PRINTOPT (CA_USER, "-u");
381 PRINTOPT (CA_VARIABLE, "-v");
383 /* now the rest of the actions */
384 PRINTACT (CA_ARRAYVAR, "arrayvar");
385 PRINTACT (CA_BINDING, "binding");
386 PRINTACT (CA_DISABLED, "disabled");
387 PRINTACT (CA_ENABLED, "enabled");
388 PRINTACT (CA_FUNCTION, "function");
389 PRINTACT (CA_HELPTOPIC, "helptopic");
390 PRINTACT (CA_HOSTNAME, "hostname");
391 PRINTACT (CA_RUNNING, "running");
392 PRINTACT (CA_SETOPT, "setopt");
393 PRINTACT (CA_SHOPT, "shopt");
394 PRINTACT (CA_SIGNAL, "signal");
395 PRINTACT (CA_STOPPED, "stopped");
397 /* now the rest of the arguments */
399 /* arguments that require quoting */
400 SQPRINTARG (cs->globpat, "-G");
401 SQPRINTARG (cs->words, "-W");
402 SQPRINTARG (cs->prefix, "-P");
403 SQPRINTARG (cs->suffix, "-S");
404 SQPRINTARG (cs->filterpat, "-X");
406 /* simple arguments that don't require quoting */
407 PRINTARG (cs->funcname, "-F");
408 PRINTARG (cs->command, "-C");
410 printf ("%s\n", cmd);
414 print_all_completions ()
416 print_all_compspecs (print_one_completion);
420 print_cmd_completions (list)
427 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
429 cs = find_compspec (l->word->word);
431 print_one_completion (l->word->word, cs);
434 builtin_error ("%s: no completion specification", l->word->word);
435 ret = EXECUTION_FAILURE;
442 $DEPENDS_ON PROGRAMMABLE_COMPLETION
443 $FUNCTION compgen_builtin
444 $SHORT_DOC compgen [-abcdefjkvu] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [word]
445 Display the possible completions depending on the options. Intended
446 to be used from within a shell function generating possible completions.
447 If the optional WORD argument is supplied, matches against WORD are
452 compgen_builtin (list)
462 return (EXECUTION_SUCCESS);
464 acts = (unsigned long)0L;
465 Aarg = Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
466 cs = (COMPSPEC *)NULL;
468 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
469 as a side effect if they are supplied as options. */
470 rval = build_actions (list, (int *)NULL, (int *)NULL, &acts);
471 if (rval == EX_USAGE)
473 if (rval == EXECUTION_FAILURE)
474 return (EXECUTION_SUCCESS);
478 word = (list && list->word) ? list->word->word : "";
481 internal_warning ("compgen: -F option may not work as you expect");
483 internal_warning ("compgen: -C option may not work as you expect");
485 /* If we get here, we need to build a compspec and evaluate it. */
486 cs = alloc_compspec ();
490 cs->globpat = STRDUP (Garg);
491 cs->words = STRDUP (Warg);
492 cs->prefix = STRDUP (Parg);
493 cs->suffix = STRDUP (Sarg);
494 cs->funcname = STRDUP (Farg);
495 cs->command = STRDUP (Carg);
496 cs->filterpat = STRDUP (Xarg);
498 rval = EXECUTION_FAILURE;
499 sl = gen_compspec_completions (cs, "compgen", word, 0, 0);
504 rval = EXECUTION_SUCCESS;
505 print_stringlist (sl, (char *)NULL);
507 free_stringlist (sl);