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-2010 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] [-DE] [-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
37 -D apply the completions and actions as the default for commands
38 without any specific completion defined
39 -E apply the completions and actions to "empty" commands --
40 completion attempted on a blank line
42 When completion is attempted, the actions are applied in the order the
43 uppercase-letter options are listed above. The -D option takes
47 Returns success unless an invalid option is supplied or an error occurs.
54 #include "../bashtypes.h"
56 #if defined (HAVE_UNISTD_H)
60 #include "../bashansi.h"
61 #include "../bashintl.h"
64 #include "../builtins.h"
65 #include "../pcomplete.h"
66 #include "../bashline.h"
69 #include "bashgetopt.h"
71 #include <readline/readline.h>
73 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
75 /* Structure containing all the non-action (binary) options; filled in by
84 static int find_compact __P((char *));
85 static int find_compopt __P((char *));
87 static int build_actions __P((WORD_LIST *, struct _optflags *, unsigned long *, unsigned long *));
89 static int remove_cmd_completions __P((WORD_LIST *));
91 static int print_one_completion __P((char *, COMPSPEC *));
92 static int print_compitem __P((BUCKET_CONTENTS *));
93 static void print_compopts __P((const char *, COMPSPEC *, int));
94 static void print_all_completions __P((void));
95 static int print_cmd_completions __P((WORD_LIST *));
97 static char *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg;
99 static const struct _compacts {
100 const char * const actname;
104 { "alias", CA_ALIAS, 'a' },
105 { "arrayvar", CA_ARRAYVAR, 0 },
106 { "binding", CA_BINDING, 0 },
107 { "builtin", CA_BUILTIN, 'b' },
108 { "command", CA_COMMAND, 'c' },
109 { "directory", CA_DIRECTORY, 'd' },
110 { "disabled", CA_DISABLED, 0 },
111 { "enabled", CA_ENABLED, 0 },
112 { "export", CA_EXPORT, 'e' },
113 { "file", CA_FILE, 'f' },
114 { "function", CA_FUNCTION, 0 },
115 { "helptopic", CA_BUILTIN, 0 }, /* for now */
116 { "hostname", CA_HOSTNAME, 0 },
117 { "group", CA_GROUP, 'g' },
118 { "job", CA_JOB, 'j' },
119 { "keyword", CA_KEYWORD, 'k' },
120 { "running", CA_RUNNING, 0 },
121 { "service", CA_SERVICE, 's' },
122 { "setopt", CA_SETOPT, 0 },
123 { "shopt", CA_SHOPT, 0 },
124 { "signal", CA_SIGNAL, 0 },
125 { "stopped", CA_STOPPED, 0 },
126 { "user", CA_USER, 'u' },
127 { "variable", CA_VARIABLE, 'v' },
128 { (char *)NULL, 0, 0 },
131 /* This should be a STRING_INT_ALIST */
132 const static struct _compopt {
133 const char * const optname;
136 { "bashdefault", COPT_BASHDEFAULT },
137 { "default", COPT_DEFAULT },
138 { "dirnames", COPT_DIRNAMES },
139 { "filenames",COPT_FILENAMES},
140 { "nospace", COPT_NOSPACE },
141 { "plusdirs", COPT_PLUSDIRS },
151 for (i = 0; compacts[i].actname; i++)
152 if (STREQ (name, compacts[i].actname))
163 for (i = 0; compopts[i].optname; i++)
164 if (STREQ (name, compopts[i].optname))
169 /* Build the actions and compspec options from the options specified in LIST.
170 ACTP is a pointer to an unsigned long in which to place the bitmap of
171 actions. OPTP is a pointer to an unsigned long in which to place the
172 btmap of compspec options (arguments to `-o'). PP, if non-null, gets 1
173 if -p is supplied; RP, if non-null, gets 1 if -r is supplied.
174 If either is null, the corresponding option generates an error.
175 This also sets variables corresponding to options that take arguments as
176 a side effect; the caller should ensure that those variables are set to
177 NULL before calling build_actions. Return value:
178 EX_USAGE = bad option
179 EXECUTION_SUCCESS = some options supplied
180 EXECUTION_FAILURE = no options supplied
184 build_actions (list, flagp, actp, optp)
186 struct _optflags *flagp;
187 unsigned long *actp, *optp;
189 int opt, ind, opt_given;
190 unsigned long acts, copts;
192 acts = copts = (unsigned long)0L;
195 reset_internal_getopt ();
196 while ((opt = internal_getopt (list, "abcdefgjko:prsuvA:G:W:P:S:X:F:C:DE")) != -1)
209 sh_invalidopt ("-r");
222 sh_invalidopt ("-p");
237 acts |= CA_DIRECTORY;
264 ind = find_compopt (list_optarg);
267 sh_invalidoptname (list_optarg);
270 copts |= compopts[ind].optflag;
273 ind = find_compact (list_optarg);
276 builtin_error (_("%s: invalid action name"), list_optarg);
279 acts |= compacts[ind].actflag;
292 sh_invalidopt ("-D");
304 sh_invalidopt ("-E");
335 return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
338 /* Add, remove, and display completion specifiers. */
340 complete_builtin (list)
344 unsigned long acts, copts;
346 struct _optflags oflags;
351 print_all_completions ();
352 return (EXECUTION_SUCCESS);
355 opt_given = oflags.pflag = oflags.rflag = oflags.Dflag = oflags.Eflag = 0;
357 acts = copts = (unsigned long)0L;
358 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
359 cs = (COMPSPEC *)NULL;
361 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
362 as a side effect if they are supplied as options. */
363 rval = build_actions (list, &oflags, &acts, &copts);
364 if (rval == EX_USAGE)
366 opt_given = rval != EXECUTION_FAILURE;
370 wl = oflags.Dflag ? make_word_list (make_bare_word (DEFAULTCMD), (WORD_LIST *)NULL)
371 : (oflags.Eflag ? make_word_list (make_bare_word (EMPTYCMD), (WORD_LIST *)NULL) : 0);
373 /* -p overrides everything else */
374 if (oflags.pflag || (list == 0 && opt_given == 0))
378 rval = print_cmd_completions (wl);
384 print_all_completions ();
385 return (EXECUTION_SUCCESS);
387 return (print_cmd_completions (list));
390 /* next, -r overrides everything else. */
395 rval = remove_cmd_completions (wl);
402 return (EXECUTION_SUCCESS);
404 return (remove_cmd_completions (list));
407 if (wl == 0 && list == 0 && opt_given)
413 /* If we get here, we need to build a compspec and add it for each
414 remaining argument. */
415 cs = compspec_create ();
419 cs->globpat = STRDUP (Garg);
420 cs->words = STRDUP (Warg);
421 cs->prefix = STRDUP (Parg);
422 cs->suffix = STRDUP (Sarg);
423 cs->funcname = STRDUP (Farg);
424 cs->command = STRDUP (Carg);
425 cs->filterpat = STRDUP (Xarg);
427 for (rval = EXECUTION_SUCCESS, l = wl ? wl : list ; l; l = l->next)
429 /* Add CS as the compspec for the specified commands. */
430 if (progcomp_insert (l->word->word, cs) == 0)
431 rval = EXECUTION_FAILURE;
439 remove_cmd_completions (list)
445 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
447 if (progcomp_remove (l->word->word) == 0)
449 builtin_error (_("%s: no completion specification"), l->word->word);
450 ret = EXECUTION_FAILURE;
456 #define SQPRINTARG(a, f) \
460 x = sh_single_quote (a); \
461 printf ("%s %s ", f, x); \
466 #define PRINTARG(a, f) \
469 printf ("%s %s ", f, a); \
472 #define PRINTOPT(a, f) \
478 #define PRINTACT(a, f) \
481 printf ("-A %s ", f); \
484 #define PRINTCOMPOPT(a, f) \
487 printf ("-o %s ", f); \
490 #define XPRINTCOMPOPT(a, f) \
493 printf ("-o %s ", f); \
495 printf ("+o %s ", f); \
499 print_one_completion (cmd, cs)
503 unsigned long acts, copts;
506 printf ("complete ");
510 /* First, print the -o options. */
511 PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
512 PRINTCOMPOPT (COPT_DEFAULT, "default");
513 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
514 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
515 PRINTCOMPOPT (COPT_NOSPACE, "nospace");
516 PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
520 /* simple flags next */
521 PRINTOPT (CA_ALIAS, "-a");
522 PRINTOPT (CA_BUILTIN, "-b");
523 PRINTOPT (CA_COMMAND, "-c");
524 PRINTOPT (CA_DIRECTORY, "-d");
525 PRINTOPT (CA_EXPORT, "-e");
526 PRINTOPT (CA_FILE, "-f");
527 PRINTOPT (CA_GROUP, "-g");
528 PRINTOPT (CA_JOB, "-j");
529 PRINTOPT (CA_KEYWORD, "-k");
530 PRINTOPT (CA_SERVICE, "-s");
531 PRINTOPT (CA_USER, "-u");
532 PRINTOPT (CA_VARIABLE, "-v");
534 /* now the rest of the actions */
535 PRINTACT (CA_ARRAYVAR, "arrayvar");
536 PRINTACT (CA_BINDING, "binding");
537 PRINTACT (CA_DISABLED, "disabled");
538 PRINTACT (CA_ENABLED, "enabled");
539 PRINTACT (CA_FUNCTION, "function");
540 PRINTACT (CA_HELPTOPIC, "helptopic");
541 PRINTACT (CA_HOSTNAME, "hostname");
542 PRINTACT (CA_RUNNING, "running");
543 PRINTACT (CA_SETOPT, "setopt");
544 PRINTACT (CA_SHOPT, "shopt");
545 PRINTACT (CA_SIGNAL, "signal");
546 PRINTACT (CA_STOPPED, "stopped");
548 /* now the rest of the arguments */
550 /* arguments that require quoting */
551 SQPRINTARG (cs->globpat, "-G");
552 SQPRINTARG (cs->words, "-W");
553 SQPRINTARG (cs->prefix, "-P");
554 SQPRINTARG (cs->suffix, "-S");
555 SQPRINTARG (cs->filterpat, "-X");
557 SQPRINTARG (cs->command, "-C");
559 /* simple arguments that don't require quoting */
560 PRINTARG (cs->funcname, "-F");
562 if (STREQ (cmd, EMPTYCMD))
564 else if (STREQ (cmd, DEFAULTCMD))
567 printf ("%s\n", cmd);
573 print_compopts (cmd, cs, full)
585 XPRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
586 XPRINTCOMPOPT (COPT_DEFAULT, "default");
587 XPRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
588 XPRINTCOMPOPT (COPT_FILENAMES, "filenames");
589 XPRINTCOMPOPT (COPT_NOSPACE, "nospace");
590 XPRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
594 PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
595 PRINTCOMPOPT (COPT_DEFAULT, "default");
596 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
597 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
598 PRINTCOMPOPT (COPT_NOSPACE, "nospace");
599 PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
602 if (STREQ (cmd, EMPTYCMD))
604 else if (STREQ (cmd, DEFAULTCMD))
607 printf ("%s\n", cmd);
611 print_compitem (item)
612 BUCKET_CONTENTS *item;
618 cs = (COMPSPEC *)item->data;
620 return (print_one_completion (cmd, cs));
624 print_all_completions ()
626 progcomp_walk (print_compitem);
630 print_cmd_completions (list)
637 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
639 cs = progcomp_search (l->word->word);
641 print_one_completion (l->word->word, cs);
644 builtin_error (_("%s: no completion specification"), l->word->word);
645 ret = EXECUTION_FAILURE;
649 return (sh_chkwrite (ret));
653 $DEPENDS_ON PROGRAMMABLE_COMPLETION
654 $FUNCTION compgen_builtin
655 $SHORT_DOC compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
656 Display possible completions depending on the options.
658 Intended to be used from within a shell function generating possible
659 completions. If the optional WORD argument is supplied, matches against
663 Returns success unless an invalid option is supplied or an error occurs.
667 compgen_builtin (list)
671 unsigned long acts, copts;
674 char *word, **matches;
677 return (EXECUTION_SUCCESS);
679 acts = copts = (unsigned long)0L;
680 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
681 cs = (COMPSPEC *)NULL;
683 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
684 as a side effect if they are supplied as options. */
685 rval = build_actions (list, (struct _optflags *)NULL, &acts, &copts);
686 if (rval == EX_USAGE)
688 if (rval == EXECUTION_FAILURE)
689 return (EXECUTION_SUCCESS);
693 word = (list && list->word) ? list->word->word : "";
696 builtin_error (_("warning: -F option may not work as you expect"));
698 builtin_error (_("warning: -C option may not work as you expect"));
700 /* If we get here, we need to build a compspec and evaluate it. */
701 cs = compspec_create ();
706 cs->globpat = STRDUP (Garg);
707 cs->words = STRDUP (Warg);
708 cs->prefix = STRDUP (Parg);
709 cs->suffix = STRDUP (Sarg);
710 cs->funcname = STRDUP (Farg);
711 cs->command = STRDUP (Carg);
712 cs->filterpat = STRDUP (Xarg);
714 rval = EXECUTION_FAILURE;
715 sl = gen_compspec_completions (cs, "compgen", word, 0, 0, 0);
717 /* If the compspec wants the bash default completions, temporarily
718 turn off programmable completion and call the bash completion code. */
719 if ((sl == 0 || sl->list_len == 0) && (copts & COPT_BASHDEFAULT))
721 matches = bash_default_completion (word, 0, 0, 0, 0);
722 sl = completions_to_stringlist (matches);
723 strvec_dispose (matches);
726 /* This isn't perfect, but it's the best we can do, given what readline
727 exports from its set of completion utility functions. */
728 if ((sl == 0 || sl->list_len == 0) && (copts & COPT_DEFAULT))
730 matches = rl_completion_matches (word, rl_filename_completion_function);
731 sl = completions_to_stringlist (matches);
732 strvec_dispose (matches);
737 if (sl->list && sl->list_len)
739 rval = EXECUTION_SUCCESS;
740 strlist_print (sl, (char *)NULL);
742 strlist_dispose (sl);
745 compspec_dispose (cs);
750 $DEPENDS_ON PROGRAMMABLE_COMPLETION
751 $FUNCTION compopt_builtin
752 $SHORT_DOC compopt [-o|+o option] [-DE] [name ...]
753 Modify or display completion options.
755 Modify the completion options for each NAME, or, if no NAMEs are supplied,
756 the completion currently being executed. If no OPTIONs are given, print
757 the completion options for each NAME or the current completion specification.
760 -o option Set completion option OPTION for each NAME
761 -D Change options for the "default" command completion
762 -E Change options for the "empty" command completion
764 Using `+o' instead of `-o' turns off the specified option.
768 Each NAME refers to a command for which a completion specification must
769 have previously been defined using the `complete' builtin. If no NAMEs
770 are supplied, compopt must be called by a function currently generating
771 completions, and the options for that currently-executing completion
772 generator are modified.
775 Returns success unless an invalid option is supplied or NAME does not
776 have a completion specification defined.
780 compopt_builtin (list)
783 int opts_on, opts_off, *opts, opt, oind, ret, Dflag, Eflag;
787 opts_on = opts_off = Eflag = Dflag = 0;
788 ret = EXECUTION_SUCCESS;
790 reset_internal_getopt ();
791 while ((opt = internal_getopt (list, "+o:DE")) != EOF)
793 opts = (list_opttype == '-') ? &opts_on : &opts_off;
798 oind = find_compopt (list_optarg);
801 sh_invalidoptname (list_optarg);
804 *opts |= compopts[oind].optflag;
819 wl = Dflag ? make_word_list (make_bare_word (DEFAULTCMD), (WORD_LIST *)NULL)
820 : (Eflag ? make_word_list (make_bare_word (EMPTYCMD), (WORD_LIST *)NULL) : 0);
822 if (list == 0 && wl == 0)
824 if (RL_ISSTATE (RL_STATE_COMPLETING) == 0 || pcomp_curcs == 0)
826 builtin_error (_("not currently executing completion function"));
827 return (EXECUTION_FAILURE);
831 if (opts_on == 0 && opts_off == 0)
833 print_compopts (pcomp_curcmd, cs, 1);
834 return (sh_chkwrite (ret));
837 /* Set the compspec options */
838 pcomp_set_compspec_options (cs, opts_on, 1);
839 pcomp_set_compspec_options (cs, opts_off, 0);
841 /* And change the readline variables the options control */
842 pcomp_set_readline_variables (opts_on, 1);
843 pcomp_set_readline_variables (opts_off, 0);
848 for (l = wl ? wl : list; l; l = l->next)
850 cs = progcomp_search (l->word->word);
853 builtin_error (_("%s: no completion specification"), l->word->word);
854 ret = EXECUTION_FAILURE;
857 if (opts_on == 0 && opts_off == 0)
859 print_compopts (l->word->word, cs, 1);
860 continue; /* XXX -- fill in later */
863 /* Set the compspec options */
864 pcomp_set_compspec_options (cs, opts_on, 1);
865 pcomp_set_compspec_options (cs, opts_off, 0);