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-2015 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] [-DEI] [-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
41 -I apply the completions and actions to the initial (usually the
44 When completion is attempted, the actions are applied in the order the
45 uppercase-letter options are listed above. If multiple options are supplied,
46 the -D option takes precedence over -E, and both take precedence over -I.
49 Returns success unless an invalid option is supplied or an error occurs.
56 #include "../bashtypes.h"
58 #if defined (HAVE_UNISTD_H)
62 #include "../bashansi.h"
63 #include "../bashintl.h"
66 #include "../builtins.h"
67 #include "../pcomplete.h"
68 #include "../bashline.h"
71 #include "bashgetopt.h"
73 #include <readline/readline.h>
75 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
77 /* Structure containing all the non-action (binary) options; filled in by
87 static int find_compact __P((char *));
88 static int find_compopt __P((char *));
90 static int build_actions __P((WORD_LIST *, struct _optflags *, unsigned long *, unsigned long *));
92 static int remove_cmd_completions __P((WORD_LIST *));
94 static int print_one_completion __P((char *, COMPSPEC *));
95 static int print_compitem __P((BUCKET_CONTENTS *));
96 static void print_compopts __P((const char *, COMPSPEC *, int));
97 static void print_all_completions __P((void));
98 static int print_cmd_completions __P((WORD_LIST *));
100 static char *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg;
102 static const struct _compacts {
103 const char * const actname;
107 { "alias", CA_ALIAS, 'a' },
108 { "arrayvar", CA_ARRAYVAR, 0 },
109 { "binding", CA_BINDING, 0 },
110 { "builtin", CA_BUILTIN, 'b' },
111 { "command", CA_COMMAND, 'c' },
112 { "directory", CA_DIRECTORY, 'd' },
113 { "disabled", CA_DISABLED, 0 },
114 { "enabled", CA_ENABLED, 0 },
115 { "export", CA_EXPORT, 'e' },
116 { "file", CA_FILE, 'f' },
117 { "function", CA_FUNCTION, 0 },
118 { "helptopic", CA_HELPTOPIC, 0 },
119 { "hostname", CA_HOSTNAME, 0 },
120 { "group", CA_GROUP, 'g' },
121 { "job", CA_JOB, 'j' },
122 { "keyword", CA_KEYWORD, 'k' },
123 { "running", CA_RUNNING, 0 },
124 { "service", CA_SERVICE, 's' },
125 { "setopt", CA_SETOPT, 0 },
126 { "shopt", CA_SHOPT, 0 },
127 { "signal", CA_SIGNAL, 0 },
128 { "stopped", CA_STOPPED, 0 },
129 { "user", CA_USER, 'u' },
130 { "variable", CA_VARIABLE, 'v' },
131 { (char *)NULL, 0, 0 },
134 /* This should be a STRING_INT_ALIST */
135 static const struct _compopt {
136 const char * const optname;
139 { "bashdefault", COPT_BASHDEFAULT },
140 { "default", COPT_DEFAULT },
141 { "dirnames", COPT_DIRNAMES },
142 { "filenames",COPT_FILENAMES},
143 { "noquote", COPT_NOQUOTE },
144 { "nosort", COPT_NOSORT },
145 { "nospace", COPT_NOSPACE },
146 { "plusdirs", COPT_PLUSDIRS },
156 for (i = 0; compacts[i].actname; i++)
157 if (STREQ (name, compacts[i].actname))
168 for (i = 0; compopts[i].optname; i++)
169 if (STREQ (name, compopts[i].optname))
174 /* Build the actions and compspec options from the options specified in LIST.
175 ACTP is a pointer to an unsigned long in which to place the bitmap of
176 actions. OPTP is a pointer to an unsigned long in which to place the
177 bitmap of compspec options (arguments to `-o'). PP, if non-null, gets 1
178 if -p is supplied; RP, if non-null, gets 1 if -r is supplied.
179 If either is null, the corresponding option generates an error.
180 This also sets variables corresponding to options that take arguments as
181 a side effect; the caller should ensure that those variables are set to
182 NULL before calling build_actions. Return value:
183 EX_USAGE = bad option
184 EXECUTION_SUCCESS = some options supplied
185 EXECUTION_FAILURE = no options supplied
189 build_actions (list, flagp, actp, optp)
191 struct _optflags *flagp;
192 unsigned long *actp, *optp;
194 int opt, ind, opt_given;
195 unsigned long acts, copts;
197 acts = copts = (unsigned long)0L;
200 reset_internal_getopt ();
201 while ((opt = internal_getopt (list, "abcdefgjko:prsuvA:G:W:P:S:X:F:C:DEI")) != -1)
214 sh_invalidopt ("-r");
227 sh_invalidopt ("-p");
242 acts |= CA_DIRECTORY;
269 ind = find_compopt (list_optarg);
272 sh_invalidoptname (list_optarg);
275 copts |= compopts[ind].optflag;
278 ind = find_compact (list_optarg);
281 builtin_error (_("%s: invalid action name"), list_optarg);
284 acts |= compacts[ind].actflag;
297 sh_invalidopt ("-D");
309 sh_invalidopt ("-E");
321 sh_invalidopt ("-I");
353 return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
356 /* Add, remove, and display completion specifiers. */
358 complete_builtin (list)
362 unsigned long acts, copts;
364 struct _optflags oflags;
369 print_all_completions ();
370 return (EXECUTION_SUCCESS);
373 opt_given = oflags.pflag = oflags.rflag = 0;
374 oflags.Dflag = oflags.Eflag = oflags.Iflag = 0;
376 acts = copts = (unsigned long)0L;
377 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
378 cs = (COMPSPEC *)NULL;
380 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
381 as a side effect if they are supplied as options. */
382 rval = build_actions (list, &oflags, &acts, &copts);
383 if (rval == EX_USAGE)
385 opt_given = rval != EXECUTION_FAILURE;
390 wl = make_word_list (make_bare_word (DEFAULTCMD), (WORD_LIST *)NULL);
391 else if (oflags.Eflag)
392 wl = make_word_list (make_bare_word (EMPTYCMD), (WORD_LIST *)NULL);
393 else if (oflags.Iflag)
394 wl = make_word_list (make_bare_word (INITIALWORD), (WORD_LIST *)NULL);
396 wl = (WORD_LIST *)NULL;
398 /* -p overrides everything else */
399 if (oflags.pflag || (list == 0 && opt_given == 0))
403 rval = print_cmd_completions (wl);
409 print_all_completions ();
410 return (EXECUTION_SUCCESS);
412 return (print_cmd_completions (list));
415 /* next, -r overrides everything else. */
420 rval = remove_cmd_completions (wl);
427 return (EXECUTION_SUCCESS);
429 return (remove_cmd_completions (list));
432 if (wl == 0 && list == 0 && opt_given)
438 /* If we get here, we need to build a compspec and add it for each
439 remaining argument. */
440 cs = compspec_create ();
444 cs->globpat = STRDUP (Garg);
445 cs->words = STRDUP (Warg);
446 cs->prefix = STRDUP (Parg);
447 cs->suffix = STRDUP (Sarg);
448 cs->funcname = STRDUP (Farg);
449 cs->command = STRDUP (Carg);
450 cs->filterpat = STRDUP (Xarg);
452 for (rval = EXECUTION_SUCCESS, l = wl ? wl : list ; l; l = l->next)
454 /* Add CS as the compspec for the specified commands. */
455 if (progcomp_insert (l->word->word, cs) == 0)
456 rval = EXECUTION_FAILURE;
464 remove_cmd_completions (list)
470 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
472 if (progcomp_remove (l->word->word) == 0)
474 builtin_error (_("%s: no completion specification"), l->word->word);
475 ret = EXECUTION_FAILURE;
481 #define SQPRINTARG(a, f) \
485 x = sh_single_quote (a); \
486 printf ("%s %s ", f, x); \
491 #define PRINTARG(a, f) \
494 printf ("%s %s ", f, a); \
497 #define PRINTOPT(a, f) \
503 #define PRINTACT(a, f) \
506 printf ("-A %s ", f); \
509 #define PRINTCOMPOPT(a, f) \
512 printf ("-o %s ", f); \
515 #define XPRINTCOMPOPT(a, f) \
518 printf ("-o %s ", f); \
520 printf ("+o %s ", f); \
524 print_one_completion (cmd, cs)
528 unsigned long acts, copts;
531 printf ("complete ");
535 /* First, print the -o options. */
536 PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
537 PRINTCOMPOPT (COPT_DEFAULT, "default");
538 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
539 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
540 PRINTCOMPOPT (COPT_NOQUOTE, "noquote");
541 PRINTCOMPOPT (COPT_NOSORT, "nosort");
542 PRINTCOMPOPT (COPT_NOSPACE, "nospace");
543 PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
547 /* simple flags next */
548 PRINTOPT (CA_ALIAS, "-a");
549 PRINTOPT (CA_BUILTIN, "-b");
550 PRINTOPT (CA_COMMAND, "-c");
551 PRINTOPT (CA_DIRECTORY, "-d");
552 PRINTOPT (CA_EXPORT, "-e");
553 PRINTOPT (CA_FILE, "-f");
554 PRINTOPT (CA_GROUP, "-g");
555 PRINTOPT (CA_JOB, "-j");
556 PRINTOPT (CA_KEYWORD, "-k");
557 PRINTOPT (CA_SERVICE, "-s");
558 PRINTOPT (CA_USER, "-u");
559 PRINTOPT (CA_VARIABLE, "-v");
561 /* now the rest of the actions */
562 PRINTACT (CA_ARRAYVAR, "arrayvar");
563 PRINTACT (CA_BINDING, "binding");
564 PRINTACT (CA_DISABLED, "disabled");
565 PRINTACT (CA_ENABLED, "enabled");
566 PRINTACT (CA_FUNCTION, "function");
567 PRINTACT (CA_HELPTOPIC, "helptopic");
568 PRINTACT (CA_HOSTNAME, "hostname");
569 PRINTACT (CA_RUNNING, "running");
570 PRINTACT (CA_SETOPT, "setopt");
571 PRINTACT (CA_SHOPT, "shopt");
572 PRINTACT (CA_SIGNAL, "signal");
573 PRINTACT (CA_STOPPED, "stopped");
575 /* now the rest of the arguments */
577 /* arguments that require quoting */
578 SQPRINTARG (cs->globpat, "-G");
579 SQPRINTARG (cs->words, "-W");
580 SQPRINTARG (cs->prefix, "-P");
581 SQPRINTARG (cs->suffix, "-S");
582 SQPRINTARG (cs->filterpat, "-X");
584 SQPRINTARG (cs->command, "-C");
586 /* simple arguments that don't require quoting */
587 PRINTARG (cs->funcname, "-F");
589 if (STREQ (cmd, DEFAULTCMD))
591 else if (STREQ (cmd, EMPTYCMD))
593 else if (STREQ (cmd, INITIALWORD))
596 printf ("%s\n", cmd);
602 print_compopts (cmd, cs, full)
614 XPRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
615 XPRINTCOMPOPT (COPT_DEFAULT, "default");
616 XPRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
617 XPRINTCOMPOPT (COPT_FILENAMES, "filenames");
618 XPRINTCOMPOPT (COPT_NOQUOTE, "noquote");
619 XPRINTCOMPOPT (COPT_NOSORT, "nosort");
620 XPRINTCOMPOPT (COPT_NOSPACE, "nospace");
621 XPRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
625 PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
626 PRINTCOMPOPT (COPT_DEFAULT, "default");
627 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
628 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
629 PRINTCOMPOPT (COPT_NOQUOTE, "noquote");
630 PRINTCOMPOPT (COPT_NOSORT, "nosort");
631 PRINTCOMPOPT (COPT_NOSPACE, "nospace");
632 PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
635 if (STREQ (cmd, DEFAULTCMD))
637 else if (STREQ (cmd, EMPTYCMD))
639 else if (STREQ (cmd, INITIALWORD))
642 printf ("%s\n", cmd);
646 print_compitem (item)
647 BUCKET_CONTENTS *item;
653 cs = (COMPSPEC *)item->data;
655 return (print_one_completion (cmd, cs));
659 print_all_completions ()
661 progcomp_walk (print_compitem);
665 print_cmd_completions (list)
672 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
674 cs = progcomp_search (l->word->word);
676 print_one_completion (l->word->word, cs);
679 builtin_error (_("%s: no completion specification"), l->word->word);
680 ret = EXECUTION_FAILURE;
684 return (sh_chkwrite (ret));
688 $DEPENDS_ON PROGRAMMABLE_COMPLETION
689 $FUNCTION compgen_builtin
690 $SHORT_DOC compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
691 Display possible completions depending on the options.
693 Intended to be used from within a shell function generating possible
694 completions. If the optional WORD argument is supplied, matches against
698 Returns success unless an invalid option is supplied or an error occurs.
702 compgen_builtin (list)
706 unsigned long acts, copts;
709 char *word, **matches;
714 return (EXECUTION_SUCCESS);
716 acts = copts = (unsigned long)0L;
717 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
718 cs = (COMPSPEC *)NULL;
720 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
721 as a side effect if they are supplied as options. */
722 rval = build_actions (list, (struct _optflags *)NULL, &acts, &copts);
723 if (rval == EX_USAGE)
725 if (rval == EXECUTION_FAILURE)
726 return (EXECUTION_SUCCESS);
730 word = (list && list->word) ? list->word->word : "";
733 builtin_error (_("warning: -F option may not work as you expect"));
735 builtin_error (_("warning: -C option may not work as you expect"));
737 /* If we get here, we need to build a compspec and evaluate it. */
738 cs = compspec_create ();
743 cs->globpat = STRDUP (Garg);
744 cs->words = STRDUP (Warg);
745 cs->prefix = STRDUP (Parg);
746 cs->suffix = STRDUP (Sarg);
747 cs->funcname = STRDUP (Farg);
748 cs->command = STRDUP (Carg);
749 cs->filterpat = STRDUP (Xarg);
751 rval = EXECUTION_FAILURE;
753 /* probably don't have to save these, just being safe */
754 old_line = pcomp_line;
756 pcomp_line = (char *)NULL;
758 sl = gen_compspec_completions (cs, "compgen", word, 0, 0, 0);
759 pcomp_line = old_line;
762 /* If the compspec wants the bash default completions, temporarily
763 turn off programmable completion and call the bash completion code. */
764 if ((sl == 0 || sl->list_len == 0) && (copts & COPT_BASHDEFAULT))
766 matches = bash_default_completion (word, 0, 0, 0, 0);
767 sl = completions_to_stringlist (matches);
768 strvec_dispose (matches);
771 /* This isn't perfect, but it's the best we can do, given what readline
772 exports from its set of completion utility functions. */
773 if ((sl == 0 || sl->list_len == 0) && (copts & COPT_DEFAULT))
775 matches = rl_completion_matches (word, rl_filename_completion_function);
776 strlist_dispose (sl);
777 sl = completions_to_stringlist (matches);
778 strvec_dispose (matches);
783 if (sl->list && sl->list_len)
785 rval = EXECUTION_SUCCESS;
786 strlist_print (sl, (char *)NULL);
788 strlist_dispose (sl);
791 compspec_dispose (cs);
796 $DEPENDS_ON PROGRAMMABLE_COMPLETION
797 $FUNCTION compopt_builtin
798 $SHORT_DOC compopt [-o|+o option] [-DEI] [name ...]
799 Modify or display completion options.
801 Modify the completion options for each NAME, or, if no NAMEs are supplied,
802 the completion currently being executed. If no OPTIONs are given, print
803 the completion options for each NAME or the current completion specification.
806 -o option Set completion option OPTION for each NAME
807 -D Change options for the "default" command completion
808 -E Change options for the "empty" command completion
809 -I Change options for completion on the initial word
811 Using `+o' instead of `-o' turns off the specified option.
815 Each NAME refers to a command for which a completion specification must
816 have previously been defined using the `complete' builtin. If no NAMEs
817 are supplied, compopt must be called by a function currently generating
818 completions, and the options for that currently-executing completion
819 generator are modified.
822 Returns success unless an invalid option is supplied or NAME does not
823 have a completion specification defined.
827 compopt_builtin (list)
830 int opts_on, opts_off, *opts, opt, oind, ret, Dflag, Eflag, Iflag;
834 opts_on = opts_off = Eflag = Dflag = Iflag = 0;
835 ret = EXECUTION_SUCCESS;
837 reset_internal_getopt ();
838 while ((opt = internal_getopt (list, "+o:DEI")) != -1)
840 opts = (list_opttype == '-') ? &opts_on : &opts_off;
845 oind = find_compopt (list_optarg);
848 sh_invalidoptname (list_optarg);
851 *opts |= compopts[oind].optflag;
871 wl = make_word_list (make_bare_word (DEFAULTCMD), (WORD_LIST *)NULL);
873 wl = make_word_list (make_bare_word (EMPTYCMD), (WORD_LIST *)NULL);
875 wl = make_word_list (make_bare_word (INITIALWORD), (WORD_LIST *)NULL);
877 wl = (WORD_LIST *)NULL;
879 if (list == 0 && wl == 0)
881 if (RL_ISSTATE (RL_STATE_COMPLETING) == 0 || pcomp_curcs == 0)
883 builtin_error (_("not currently executing completion function"));
884 return (EXECUTION_FAILURE);
888 if (opts_on == 0 && opts_off == 0)
890 print_compopts (pcomp_curcmd, cs, 1);
891 return (sh_chkwrite (ret));
894 /* Set the compspec options */
895 pcomp_set_compspec_options (cs, opts_on, 1);
896 pcomp_set_compspec_options (cs, opts_off, 0);
898 /* And change the readline variables the options control */
899 pcomp_set_readline_variables (opts_on, 1);
900 pcomp_set_readline_variables (opts_off, 0);
905 for (l = wl ? wl : list; l; l = l->next)
907 cs = progcomp_search (l->word->word);
910 builtin_error (_("%s: no completion specification"), l->word->word);
911 ret = EXECUTION_FAILURE;
914 if (opts_on == 0 && opts_off == 0)
916 print_compopts (l->word->word, cs, 1);
917 continue; /* XXX -- fill in later */
920 /* Set the compspec options */
921 pcomp_set_compspec_options (cs, opts_on, 1);
922 pcomp_set_compspec_options (cs, opts_off, 0);