1 This file is complete.def, from which is created complete.c.
2 It implements the builtins "complete", "compgen", and "compopt" in Bash.
4 Copyright (C) 1999-2009 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
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 $DEPENDS_ON PROGRAMMABLE_COMPLETION
25 $FUNCTION complete_builtin
26 $SHORT_DOC complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...]
27 Specify how arguments are to be completed by Readline.
29 For each NAME, specify how arguments are to be completed. If no options
30 are supplied, existing completion specifications are printed in a way that
31 allows them to be reused as input.
34 -p print existing completion specifications in a reusable format
35 -r remove a completion specification for each NAME, or, if no
36 NAMEs are supplied, all completion specifications
38 When completion is attempted, the actions are applied in the order the
39 uppercase-letter options are listed above.
42 Returns success unless an invalid option is supplied or an error occurs.
49 #include "../bashtypes.h"
51 #if defined (HAVE_UNISTD_H)
55 #include "../bashansi.h"
56 #include "../bashintl.h"
59 #include "../builtins.h"
60 #include "../pcomplete.h"
61 #include "../bashline.h"
64 #include "bashgetopt.h"
66 #include <readline/readline.h>
68 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
70 /* Structure containing all the non-action (binary) options; filled in by
78 static int find_compact __P((char *));
79 static int find_compopt __P((char *));
81 static int build_actions __P((WORD_LIST *, struct _optflags *, unsigned long *, unsigned long *));
83 static int remove_cmd_completions __P((WORD_LIST *));
85 static int print_one_completion __P((char *, COMPSPEC *));
86 static int print_compitem __P((BUCKET_CONTENTS *));
87 static void print_compopts __P((const char *, COMPSPEC *, int));
88 static void print_all_completions __P((void));
89 static int print_cmd_completions __P((WORD_LIST *));
91 static char *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg;
93 static const struct _compacts {
94 const char * const actname;
98 { "alias", CA_ALIAS, 'a' },
99 { "arrayvar", CA_ARRAYVAR, 0 },
100 { "binding", CA_BINDING, 0 },
101 { "builtin", CA_BUILTIN, 'b' },
102 { "command", CA_COMMAND, 'c' },
103 { "directory", CA_DIRECTORY, 'd' },
104 { "disabled", CA_DISABLED, 0 },
105 { "enabled", CA_ENABLED, 0 },
106 { "export", CA_EXPORT, 'e' },
107 { "file", CA_FILE, 'f' },
108 { "function", CA_FUNCTION, 0 },
109 { "helptopic", CA_BUILTIN, 0 }, /* for now */
110 { "hostname", CA_HOSTNAME, 0 },
111 { "group", CA_GROUP, 'g' },
112 { "job", CA_JOB, 'j' },
113 { "keyword", CA_KEYWORD, 'k' },
114 { "running", CA_RUNNING, 0 },
115 { "service", CA_SERVICE, 's' },
116 { "setopt", CA_SETOPT, 0 },
117 { "shopt", CA_SHOPT, 0 },
118 { "signal", CA_SIGNAL, 0 },
119 { "stopped", CA_STOPPED, 0 },
120 { "user", CA_USER, 'u' },
121 { "variable", CA_VARIABLE, 'v' },
122 { (char *)NULL, 0, 0 },
125 /* This should be a STRING_INT_ALIST */
126 const static struct _compopt {
127 const char * const optname;
130 { "bashdefault", COPT_BASHDEFAULT },
131 { "default", COPT_DEFAULT },
132 { "dirnames", COPT_DIRNAMES },
133 { "filenames",COPT_FILENAMES},
134 { "nospace", COPT_NOSPACE },
135 { "plusdirs", COPT_PLUSDIRS },
145 for (i = 0; compacts[i].actname; i++)
146 if (STREQ (name, compacts[i].actname))
157 for (i = 0; compopts[i].optname; i++)
158 if (STREQ (name, compopts[i].optname))
163 /* Build the actions and compspec options from the options specified in LIST.
164 ACTP is a pointer to an unsigned long in which to place the bitmap of
165 actions. OPTP is a pointer to an unsigned long in which to place the
166 btmap of compspec options (arguments to `-o'). PP, if non-null, gets 1
167 if -p is supplied; RP, if non-null, gets 1 if -r is supplied.
168 If either is null, the corresponding option generates an error.
169 This also sets variables corresponding to options that take arguments as
170 a side effect; the caller should ensure that those variables are set to
171 NULL before calling build_actions. Return value:
172 EX_USAGE = bad option
173 EXECUTION_SUCCESS = some options supplied
174 EXECUTION_FAILURE = no options supplied
178 build_actions (list, flagp, actp, optp)
180 struct _optflags *flagp;
181 unsigned long *actp, *optp;
183 int opt, ind, opt_given;
184 unsigned long acts, copts;
186 acts = copts = (unsigned long)0L;
189 reset_internal_getopt ();
190 while ((opt = internal_getopt (list, "abcdefgjko:prsuvA:G:W:P:S:X:F:C:E")) != -1)
203 sh_invalidopt ("-r");
216 sh_invalidopt ("-p");
231 acts |= CA_DIRECTORY;
258 ind = find_compopt (list_optarg);
261 sh_invalidoptname (list_optarg);
264 copts |= compopts[ind].optflag;
267 ind = find_compact (list_optarg);
270 builtin_error (_("%s: invalid action name"), list_optarg);
273 acts |= compacts[ind].actflag;
286 sh_invalidopt ("-E");
317 return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
320 /* Add, remove, and display completion specifiers. */
322 complete_builtin (list)
326 unsigned long acts, copts;
328 struct _optflags oflags;
333 print_all_completions ();
334 return (EXECUTION_SUCCESS);
337 opt_given = oflags.pflag = oflags.rflag = oflags.Eflag = 0;
339 acts = copts = (unsigned long)0L;
340 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
341 cs = (COMPSPEC *)NULL;
343 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
344 as a side effect if they are supplied as options. */
345 rval = build_actions (list, &oflags, &acts, &copts);
346 if (rval == EX_USAGE)
348 opt_given = rval != EXECUTION_FAILURE;
352 wl = oflags.Eflag ? make_word_list (make_bare_word ("_EmptycmD_"), (WORD_LIST *)NULL) : 0;
354 /* -p overrides everything else */
355 if (oflags.pflag || (list == 0 && opt_given == 0))
359 rval = print_cmd_completions (wl);
365 print_all_completions ();
366 return (EXECUTION_SUCCESS);
368 return (print_cmd_completions (list));
371 /* next, -r overrides everything else. */
376 rval = remove_cmd_completions (wl);
383 return (EXECUTION_SUCCESS);
385 return (remove_cmd_completions (list));
388 if (wl == 0 && list == 0 && opt_given)
394 /* If we get here, we need to build a compspec and add it for each
395 remaining argument. */
396 cs = compspec_create ();
400 cs->globpat = STRDUP (Garg);
401 cs->words = STRDUP (Warg);
402 cs->prefix = STRDUP (Parg);
403 cs->suffix = STRDUP (Sarg);
404 cs->funcname = STRDUP (Farg);
405 cs->command = STRDUP (Carg);
406 cs->filterpat = STRDUP (Xarg);
408 for (rval = EXECUTION_SUCCESS, l = wl ? wl : list ; l; l = l->next)
410 /* Add CS as the compspec for the specified commands. */
411 if (progcomp_insert (l->word->word, cs) == 0)
412 rval = EXECUTION_FAILURE;
420 remove_cmd_completions (list)
426 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
428 if (progcomp_remove (l->word->word) == 0)
430 builtin_error (_("%s: no completion specification"), l->word->word);
431 ret = EXECUTION_FAILURE;
437 #define SQPRINTARG(a, f) \
441 x = sh_single_quote (a); \
442 printf ("%s %s ", f, x); \
447 #define PRINTARG(a, f) \
450 printf ("%s %s ", f, a); \
453 #define PRINTOPT(a, f) \
459 #define PRINTACT(a, f) \
462 printf ("-A %s ", f); \
465 #define PRINTCOMPOPT(a, f) \
468 printf ("-o %s ", f); \
471 #define XPRINTCOMPOPT(a, f) \
474 printf ("-o %s ", f); \
476 printf ("+o %s ", f); \
480 print_one_completion (cmd, cs)
484 unsigned long acts, copts;
487 printf ("complete ");
491 /* First, print the -o options. */
492 PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
493 PRINTCOMPOPT (COPT_DEFAULT, "default");
494 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
495 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
496 PRINTCOMPOPT (COPT_NOSPACE, "nospace");
497 PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
501 /* simple flags next */
502 PRINTOPT (CA_ALIAS, "-a");
503 PRINTOPT (CA_BUILTIN, "-b");
504 PRINTOPT (CA_COMMAND, "-c");
505 PRINTOPT (CA_DIRECTORY, "-d");
506 PRINTOPT (CA_EXPORT, "-e");
507 PRINTOPT (CA_FILE, "-f");
508 PRINTOPT (CA_GROUP, "-g");
509 PRINTOPT (CA_JOB, "-j");
510 PRINTOPT (CA_KEYWORD, "-k");
511 PRINTOPT (CA_SERVICE, "-s");
512 PRINTOPT (CA_USER, "-u");
513 PRINTOPT (CA_VARIABLE, "-v");
515 /* now the rest of the actions */
516 PRINTACT (CA_ARRAYVAR, "arrayvar");
517 PRINTACT (CA_BINDING, "binding");
518 PRINTACT (CA_DISABLED, "disabled");
519 PRINTACT (CA_ENABLED, "enabled");
520 PRINTACT (CA_FUNCTION, "function");
521 PRINTACT (CA_HELPTOPIC, "helptopic");
522 PRINTACT (CA_HOSTNAME, "hostname");
523 PRINTACT (CA_RUNNING, "running");
524 PRINTACT (CA_SETOPT, "setopt");
525 PRINTACT (CA_SHOPT, "shopt");
526 PRINTACT (CA_SIGNAL, "signal");
527 PRINTACT (CA_STOPPED, "stopped");
529 /* now the rest of the arguments */
531 /* arguments that require quoting */
532 SQPRINTARG (cs->globpat, "-G");
533 SQPRINTARG (cs->words, "-W");
534 SQPRINTARG (cs->prefix, "-P");
535 SQPRINTARG (cs->suffix, "-S");
536 SQPRINTARG (cs->filterpat, "-X");
538 SQPRINTARG (cs->command, "-C");
540 /* simple arguments that don't require quoting */
541 PRINTARG (cs->funcname, "-F");
543 printf ("%s\n", cmd);
549 print_compopts (cmd, cs, full)
561 XPRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
562 XPRINTCOMPOPT (COPT_DEFAULT, "default");
563 XPRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
564 XPRINTCOMPOPT (COPT_FILENAMES, "filenames");
565 XPRINTCOMPOPT (COPT_NOSPACE, "nospace");
566 XPRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
570 PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
571 PRINTCOMPOPT (COPT_DEFAULT, "default");
572 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
573 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
574 PRINTCOMPOPT (COPT_NOSPACE, "nospace");
575 PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
578 printf ("%s\n", cmd);
582 print_compitem (item)
583 BUCKET_CONTENTS *item;
589 cs = (COMPSPEC *)item->data;
591 return (print_one_completion (cmd, cs));
595 print_all_completions ()
597 progcomp_walk (print_compitem);
601 print_cmd_completions (list)
608 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
610 cs = progcomp_search (l->word->word);
612 print_one_completion (l->word->word, cs);
615 builtin_error (_("%s: no completion specification"), l->word->word);
616 ret = EXECUTION_FAILURE;
620 return (sh_chkwrite (ret));
624 $DEPENDS_ON PROGRAMMABLE_COMPLETION
625 $FUNCTION compgen_builtin
626 $SHORT_DOC compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
627 Display possible completions depending on the options.
629 Intended to be used from within a shell function generating possible
630 completions. If the optional WORD argument is supplied, matches against
634 Returns success unless an invalid option is supplied or an error occurs.
638 compgen_builtin (list)
642 unsigned long acts, copts;
645 char *word, **matches;
648 return (EXECUTION_SUCCESS);
650 acts = copts = (unsigned long)0L;
651 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
652 cs = (COMPSPEC *)NULL;
654 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
655 as a side effect if they are supplied as options. */
656 rval = build_actions (list, (struct _optflags *)NULL, &acts, &copts);
657 if (rval == EX_USAGE)
659 if (rval == EXECUTION_FAILURE)
660 return (EXECUTION_SUCCESS);
664 word = (list && list->word) ? list->word->word : "";
667 builtin_error (_("warning: -F option may not work as you expect"));
669 builtin_error (_("warning: -C option may not work as you expect"));
671 /* If we get here, we need to build a compspec and evaluate it. */
672 cs = compspec_create ();
677 cs->globpat = STRDUP (Garg);
678 cs->words = STRDUP (Warg);
679 cs->prefix = STRDUP (Parg);
680 cs->suffix = STRDUP (Sarg);
681 cs->funcname = STRDUP (Farg);
682 cs->command = STRDUP (Carg);
683 cs->filterpat = STRDUP (Xarg);
685 rval = EXECUTION_FAILURE;
686 sl = gen_compspec_completions (cs, "compgen", word, 0, 0);
688 /* If the compspec wants the bash default completions, temporarily
689 turn off programmable completion and call the bash completion code. */
690 if ((sl == 0 || sl->list_len == 0) && (copts & COPT_BASHDEFAULT))
692 matches = bash_default_completion (word, 0, 0, 0, 0);
693 sl = completions_to_stringlist (matches);
694 strvec_dispose (matches);
697 /* This isn't perfect, but it's the best we can do, given what readline
698 exports from its set of completion utility functions. */
699 if ((sl == 0 || sl->list_len == 0) && (copts & COPT_DEFAULT))
701 matches = rl_completion_matches (word, rl_filename_completion_function);
702 sl = completions_to_stringlist (matches);
703 strvec_dispose (matches);
708 if (sl->list && sl->list_len)
710 rval = EXECUTION_SUCCESS;
711 strlist_print (sl, (char *)NULL);
713 strlist_dispose (sl);
716 compspec_dispose (cs);
721 $DEPENDS_ON PROGRAMMABLE_COMPLETION
722 $FUNCTION compopt_builtin
723 $SHORT_DOC compopt [-o|+o option] [name ...]
724 Modify or display completion options.
726 Modify the completion options for each NAME, or, if no NAMEs are supplied,
727 the completion currently begin executed. If no OPTIONs are givenm, print
728 the completion options for each NAME or the current completion specification.
731 -o option Set completion option OPTION for each NAME
733 Using `+o' instead of `-o' turns off the specified option.
737 Each NAME refers to a command for which a completion specification must
738 have previously been defined using the `complete' builtin. If no NAMEs
739 are supplied, compopt must be called by a function currently generating
740 completions, and the options for that currently-executing completion
741 generator are modified.
744 Returns success unless an invalid option is supplied or NAME does not
745 have a completion specification defined.
749 compopt_builtin (list)
752 int opts_on, opts_off, *opts, opt, oind, ret, Eflag;
756 opts_on = opts_off = 0;
757 ret = EXECUTION_SUCCESS;
759 reset_internal_getopt ();
760 while ((opt = internal_getopt (list, "+o:")) != EOF)
762 opts = (list_opttype == '-') ? &opts_on : &opts_off;
767 oind = find_compopt (list_optarg);
770 sh_invalidoptname (list_optarg);
773 *opts |= compopts[oind].optflag;
784 if (RL_ISSTATE (RL_STATE_COMPLETING) == 0 || pcomp_curcs == 0)
786 builtin_error (_("not currently executing completion function"));
787 return (EXECUTION_FAILURE);
791 if (opts_on == 0 && opts_off == 0)
793 print_compopts (pcomp_curcmd, cs, 1);
794 return (sh_chkwrite (ret));
797 /* Set the compspec options */
798 pcomp_set_compspec_options (cs, opts_on, 1);
799 pcomp_set_compspec_options (cs, opts_off, 0);
801 /* And change the readline variables the options control */
802 pcomp_set_readline_variables (opts_on, 1);
803 pcomp_set_readline_variables (opts_off, 0);
808 for (l = list; l; l = l->next)
810 cs = progcomp_search (l->word->word);
813 builtin_error (_("%s: no completion specification"), l->word->word);
814 ret = EXECUTION_FAILURE;
817 if (opts_on == 0 && opts_off == 0)
819 print_compopts (l->word->word, cs, 1);
820 continue; /* XXX -- fill in later */
823 /* Set the compspec options */
824 pcomp_set_compspec_options (cs, opts_on, 1);
825 pcomp_set_compspec_options (cs, opts_off, 0);