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-2002 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 [-abcdefgjksuv] [-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 int print_one_completion __P((char *, COMPSPEC *));
66 static int print_compitem __P((BUCKET_CONTENTS *));
67 static void print_all_completions __P((void));
68 static int print_cmd_completions __P((WORD_LIST *));
70 static char *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg;
72 static struct _compacts {
77 { "alias", CA_ALIAS, 'a' },
78 { "arrayvar", CA_ARRAYVAR, 0 },
79 { "binding", CA_BINDING, 0 },
80 { "builtin", CA_BUILTIN, 'b' },
81 { "command", CA_COMMAND, 'c' },
82 { "directory", CA_DIRECTORY, 'd' },
83 { "disabled", CA_DISABLED, 0 },
84 { "enabled", CA_ENABLED, 0 },
85 { "export", CA_EXPORT, 'e' },
86 { "file", CA_FILE, 'f' },
87 { "function", CA_FUNCTION, 0 },
88 { "helptopic", CA_BUILTIN, 0 }, /* for now */
89 { "hostname", CA_HOSTNAME, 0 },
90 { "group", CA_GROUP, 'g' },
91 { "job", CA_JOB, 'j' },
92 { "keyword", CA_KEYWORD, 'k' },
93 { "running", CA_RUNNING, 0 },
94 { "service", CA_SERVICE, 's' },
95 { "setopt", CA_SETOPT, 0 },
96 { "shopt", CA_SHOPT, 0 },
97 { "signal", CA_SIGNAL, 0 },
98 { "stopped", CA_STOPPED, 0 },
99 { "user", CA_USER, 'u' },
100 { "variable", CA_VARIABLE, 'v' },
101 { (char *)NULL, 0, 0 },
104 /* This should be a STRING_INT_ALIST */
105 static struct _compopt {
109 { "default", COPT_DEFAULT },
110 { "dirnames", COPT_DIRNAMES },
111 { "filenames",COPT_FILENAMES},
112 { "nospace", COPT_NOSPACE },
122 for (i = 0; compacts[i].actname; i++)
123 if (STREQ (name, compacts[i].actname))
134 for (i = 0; compopts[i].optname; i++)
135 if (STREQ (name, compopts[i].optname))
140 /* Build the actions and compspec options from the options specified in LIST.
141 ACTP is a pointer to an unsigned long in which to place the bitmap of
142 actions. OPTP is a pointer to an unsigned long in which to place the
143 btmap of compspec options (arguments to `-o'). PP, if non-null, gets 1
144 if -p is supplied; RP, if non-null, gets 1 if -r is supplied.
145 If either is null, the corresponding option generates an error.
146 This also sets variables corresponding to options that take arguments as
147 a side effect; the caller should ensure that those variables are set to
148 NULL before calling build_actions. Return value:
149 EX_USAGE = bad option
150 EXECUTION_SUCCESS = some options supplied
151 EXECUTION_FAILURE = no options supplied
155 build_actions (list, pp, rp, actp, optp)
158 unsigned long *actp, *optp;
160 int opt, ind, opt_given;
161 unsigned long acts, copts;
163 acts = copts = (unsigned long)0L;
166 reset_internal_getopt ();
167 while ((opt = internal_getopt (list, "abcdefgjko:prsuvA:G:W:P:S:X:F:C:")) != -1)
180 sh_invalidopt ("-r");
193 sh_invalidopt ("-p");
208 acts |= CA_DIRECTORY;
235 ind = find_compopt (list_optarg);
238 sh_invalidoptname (list_optarg);
241 copts |= compopts[ind].optflag;
244 ind = find_compact (list_optarg);
247 builtin_error ("%s: invalid action name", list_optarg);
250 acts |= compacts[ind].actflag;
282 return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
285 /* Add, remove, and display completion specifiers. */
287 complete_builtin (list)
290 int opt_given, pflag, rflag, rval;
291 unsigned long acts, copts;
296 print_all_completions ();
297 return (EXECUTION_SUCCESS);
300 opt_given = pflag = rflag = 0;
301 acts = copts = (unsigned long)0L;
302 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
303 cs = (COMPSPEC *)NULL;
305 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
306 as a side effect if they are supplied as options. */
307 rval = build_actions (list, &pflag, &rflag, &acts, &copts);
308 if (rval == EX_USAGE)
310 opt_given = rval != EXECUTION_FAILURE;
314 /* -p overrides everything else */
315 if (pflag || (list == 0 && opt_given == 0))
319 print_all_completions ();
320 return (EXECUTION_SUCCESS);
322 return (print_cmd_completions (list));
325 /* next, -r overrides everything else. */
331 return (EXECUTION_SUCCESS);
333 return (remove_cmd_completions (list));
336 if (list == 0 && opt_given)
342 /* If we get here, we need to build a compspec and add it for each
343 remaining argument. */
344 cs = compspec_create ();
348 cs->globpat = STRDUP (Garg);
349 cs->words = STRDUP (Warg);
350 cs->prefix = STRDUP (Parg);
351 cs->suffix = STRDUP (Sarg);
352 cs->funcname = STRDUP (Farg);
353 cs->command = STRDUP (Carg);
354 cs->filterpat = STRDUP (Xarg);
356 for (rval = EXECUTION_SUCCESS ; list; list = list->next)
358 /* Add CS as the compspec for the specified commands. */
359 if (progcomp_insert (list->word->word, cs) == 0)
360 rval = EXECUTION_FAILURE;
367 remove_cmd_completions (list)
373 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
375 if (progcomp_remove (l->word->word) == 0)
377 builtin_error ("%s: no completion specification", l->word->word);
378 ret = EXECUTION_FAILURE;
384 #define SQPRINTARG(a, f) \
388 x = sh_single_quote (a); \
389 printf ("%s %s ", f, x); \
394 #define PRINTARG(a, f) \
397 printf ("%s %s ", f, a); \
400 #define PRINTOPT(a, f) \
406 #define PRINTACT(a, f) \
409 printf ("-A %s ", f); \
412 #define PRINTCOMPOPT(a, f) \
415 printf ("-o %s ", f); \
419 print_one_completion (cmd, cs)
423 unsigned long acts, copts;
426 printf ("complete ");
430 /* First, print the -o options. */
431 PRINTCOMPOPT (COPT_DEFAULT, "default");
432 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
433 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
434 PRINTCOMPOPT (COPT_NOSPACE, "nospace");
438 /* simple flags next */
439 PRINTOPT (CA_ALIAS, "-a");
440 PRINTOPT (CA_BUILTIN, "-b");
441 PRINTOPT (CA_COMMAND, "-c");
442 PRINTOPT (CA_DIRECTORY, "-d");
443 PRINTOPT (CA_EXPORT, "-e");
444 PRINTOPT (CA_FILE, "-f");
445 PRINTOPT (CA_GROUP, "-g");
446 PRINTOPT (CA_JOB, "-j");
447 PRINTOPT (CA_KEYWORD, "-k");
448 PRINTOPT (CA_SERVICE, "-s");
449 PRINTOPT (CA_USER, "-u");
450 PRINTOPT (CA_VARIABLE, "-v");
452 /* now the rest of the actions */
453 PRINTACT (CA_ARRAYVAR, "arrayvar");
454 PRINTACT (CA_BINDING, "binding");
455 PRINTACT (CA_DISABLED, "disabled");
456 PRINTACT (CA_ENABLED, "enabled");
457 PRINTACT (CA_FUNCTION, "function");
458 PRINTACT (CA_HELPTOPIC, "helptopic");
459 PRINTACT (CA_HOSTNAME, "hostname");
460 PRINTACT (CA_RUNNING, "running");
461 PRINTACT (CA_SETOPT, "setopt");
462 PRINTACT (CA_SHOPT, "shopt");
463 PRINTACT (CA_SIGNAL, "signal");
464 PRINTACT (CA_STOPPED, "stopped");
466 /* now the rest of the arguments */
468 /* arguments that require quoting */
469 SQPRINTARG (cs->globpat, "-G");
470 SQPRINTARG (cs->words, "-W");
471 SQPRINTARG (cs->prefix, "-P");
472 SQPRINTARG (cs->suffix, "-S");
473 SQPRINTARG (cs->filterpat, "-X");
475 /* simple arguments that don't require quoting */
476 PRINTARG (cs->funcname, "-F");
477 PRINTARG (cs->command, "-C");
479 printf ("%s\n", cmd);
485 print_compitem (item)
486 BUCKET_CONTENTS *item;
492 cs = (COMPSPEC *)item->data;
494 return (print_one_completion (cmd, cs));
498 print_all_completions ()
500 progcomp_walk (print_compitem);
504 print_cmd_completions (list)
511 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
513 cs = progcomp_search (l->word->word);
515 print_one_completion (l->word->word, cs);
518 builtin_error ("%s: no completion specification", l->word->word);
519 ret = EXECUTION_FAILURE;
526 $DEPENDS_ON PROGRAMMABLE_COMPLETION
527 $FUNCTION compgen_builtin
528 $SHORT_DOC compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [word]
529 Display the possible completions depending on the options. Intended
530 to be used from within a shell function generating possible completions.
531 If the optional WORD argument is supplied, matches against WORD are
536 compgen_builtin (list)
540 unsigned long acts, copts;
546 return (EXECUTION_SUCCESS);
548 acts = copts = (unsigned long)0L;
549 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
550 cs = (COMPSPEC *)NULL;
552 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
553 as a side effect if they are supplied as options. */
554 rval = build_actions (list, (int *)NULL, (int *)NULL, &acts, &copts);
555 if (rval == EX_USAGE)
557 if (rval == EXECUTION_FAILURE)
558 return (EXECUTION_SUCCESS);
562 word = (list && list->word) ? list->word->word : "";
565 internal_warning ("compgen: -F option may not work as you expect");
567 internal_warning ("compgen: -C option may not work as you expect");
569 /* If we get here, we need to build a compspec and evaluate it. */
570 cs = compspec_create ();
575 cs->globpat = STRDUP (Garg);
576 cs->words = STRDUP (Warg);
577 cs->prefix = STRDUP (Parg);
578 cs->suffix = STRDUP (Sarg);
579 cs->funcname = STRDUP (Farg);
580 cs->command = STRDUP (Carg);
581 cs->filterpat = STRDUP (Xarg);
583 rval = EXECUTION_FAILURE;
584 sl = gen_compspec_completions (cs, "compgen", word, 0, 0);
586 /* This isn't perfect, but it's the best we can do, given what readline
587 exports from its set of completion utility functions. */
588 if ((sl == 0 || sl->list_len == 0) && (copts & COPT_DEFAULT))
592 matches = rl_completion_matches (word, rl_filename_completion_function);
593 sl = completions_to_stringlist (matches);
594 strvec_dispose (matches);
599 if (sl->list && sl->list_len)
601 rval = EXECUTION_SUCCESS;
602 strlist_print (sl, (char *)NULL);
604 strlist_dispose (sl);
607 compspec_dispose (cs);