]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/complete.def
Bash-4.4 patch 11
[thirdparty/bash.git] / builtins / complete.def
1 This file is complete.def, from which is created complete.c.
2 It implements the builtins "complete", "compgen", and "compopt" in Bash.
3
4 Copyright (C) 1999-2015 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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.
12
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.
17
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/>.
20
21 $PRODUCES complete.c
22
23 $BUILTIN complete
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.
28
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.
32
33 Options:
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
42 When completion is attempted, the actions are applied in the order the
43 uppercase-letter options are listed above. The -D option takes
44 precedence over -E.
45
46 Exit Status:
47 Returns success unless an invalid option is supplied or an error occurs.
48 $END
49
50 #include <config.h>
51
52 #include <stdio.h>
53
54 #include "../bashtypes.h"
55
56 #if defined (HAVE_UNISTD_H)
57 # include <unistd.h>
58 #endif
59
60 #include "../bashansi.h"
61 #include "../bashintl.h"
62
63 #include "../shell.h"
64 #include "../builtins.h"
65 #include "../pcomplete.h"
66 #include "../bashline.h"
67
68 #include "common.h"
69 #include "bashgetopt.h"
70
71 #include <readline/readline.h>
72
73 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
74
75 /* Structure containing all the non-action (binary) options; filled in by
76 build_actions(). */
77 struct _optflags {
78 int pflag;
79 int rflag;
80 int Dflag;
81 int Eflag;
82 };
83
84 static int find_compact __P((char *));
85 static int find_compopt __P((char *));
86
87 static int build_actions __P((WORD_LIST *, struct _optflags *, unsigned long *, unsigned long *));
88
89 static int remove_cmd_completions __P((WORD_LIST *));
90
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 *));
96
97 static char *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg;
98
99 static const struct _compacts {
100 const char * const actname;
101 int actflag;
102 int actopt;
103 } compacts[] = {
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_HELPTOPIC, 0 },
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 },
129 };
130
131 /* This should be a STRING_INT_ALIST */
132 static const struct _compopt {
133 const char * const optname;
134 int optflag;
135 } compopts[] = {
136 { "bashdefault", COPT_BASHDEFAULT },
137 { "default", COPT_DEFAULT },
138 { "dirnames", COPT_DIRNAMES },
139 { "filenames",COPT_FILENAMES},
140 { "noquote", COPT_NOQUOTE },
141 { "nosort", COPT_NOSORT },
142 { "nospace", COPT_NOSPACE },
143 { "plusdirs", COPT_PLUSDIRS },
144 { (char *)NULL, 0 },
145 };
146
147 static int
148 find_compact (name)
149 char *name;
150 {
151 register int i;
152
153 for (i = 0; compacts[i].actname; i++)
154 if (STREQ (name, compacts[i].actname))
155 return i;
156 return -1;
157 }
158
159 static int
160 find_compopt (name)
161 char *name;
162 {
163 register int i;
164
165 for (i = 0; compopts[i].optname; i++)
166 if (STREQ (name, compopts[i].optname))
167 return i;
168 return -1;
169 }
170
171 /* Build the actions and compspec options from the options specified in LIST.
172 ACTP is a pointer to an unsigned long in which to place the bitmap of
173 actions. OPTP is a pointer to an unsigned long in which to place the
174 bitmap of compspec options (arguments to `-o'). PP, if non-null, gets 1
175 if -p is supplied; RP, if non-null, gets 1 if -r is supplied.
176 If either is null, the corresponding option generates an error.
177 This also sets variables corresponding to options that take arguments as
178 a side effect; the caller should ensure that those variables are set to
179 NULL before calling build_actions. Return value:
180 EX_USAGE = bad option
181 EXECUTION_SUCCESS = some options supplied
182 EXECUTION_FAILURE = no options supplied
183 */
184
185 static int
186 build_actions (list, flagp, actp, optp)
187 WORD_LIST *list;
188 struct _optflags *flagp;
189 unsigned long *actp, *optp;
190 {
191 int opt, ind, opt_given;
192 unsigned long acts, copts;
193
194 acts = copts = (unsigned long)0L;
195 opt_given = 0;
196
197 reset_internal_getopt ();
198 while ((opt = internal_getopt (list, "abcdefgjko:prsuvA:G:W:P:S:X:F:C:DE")) != -1)
199 {
200 opt_given = 1;
201 switch (opt)
202 {
203 case 'r':
204 if (flagp)
205 {
206 flagp->rflag = 1;
207 break;
208 }
209 else
210 {
211 sh_invalidopt ("-r");
212 builtin_usage ();
213 return (EX_USAGE);
214 }
215
216 case 'p':
217 if (flagp)
218 {
219 flagp->pflag = 1;
220 break;
221 }
222 else
223 {
224 sh_invalidopt ("-p");
225 builtin_usage ();
226 return (EX_USAGE);
227 }
228
229 case 'a':
230 acts |= CA_ALIAS;
231 break;
232 case 'b':
233 acts |= CA_BUILTIN;
234 break;
235 case 'c':
236 acts |= CA_COMMAND;
237 break;
238 case 'd':
239 acts |= CA_DIRECTORY;
240 break;
241 case 'e':
242 acts |= CA_EXPORT;
243 break;
244 case 'f':
245 acts |= CA_FILE;
246 break;
247 case 'g':
248 acts |= CA_GROUP;
249 break;
250 case 'j':
251 acts |= CA_JOB;
252 break;
253 case 'k':
254 acts |= CA_KEYWORD;
255 break;
256 case 's':
257 acts |= CA_SERVICE;
258 break;
259 case 'u':
260 acts |= CA_USER;
261 break;
262 case 'v':
263 acts |= CA_VARIABLE;
264 break;
265 case 'o':
266 ind = find_compopt (list_optarg);
267 if (ind < 0)
268 {
269 sh_invalidoptname (list_optarg);
270 return (EX_USAGE);
271 }
272 copts |= compopts[ind].optflag;
273 break;
274 case 'A':
275 ind = find_compact (list_optarg);
276 if (ind < 0)
277 {
278 builtin_error (_("%s: invalid action name"), list_optarg);
279 return (EX_USAGE);
280 }
281 acts |= compacts[ind].actflag;
282 break;
283 case 'C':
284 Carg = list_optarg;
285 break;
286 case 'D':
287 if (flagp)
288 {
289 flagp->Dflag = 1;
290 break;
291 }
292 else
293 {
294 sh_invalidopt ("-D");
295 builtin_usage ();
296 return (EX_USAGE);
297 }
298 case 'E':
299 if (flagp)
300 {
301 flagp->Eflag = 1;
302 break;
303 }
304 else
305 {
306 sh_invalidopt ("-E");
307 builtin_usage ();
308 return (EX_USAGE);
309 }
310 case 'F':
311 Farg = list_optarg;
312 break;
313 case 'G':
314 Garg = list_optarg;
315 break;
316 case 'P':
317 Parg = list_optarg;
318 break;
319 case 'S':
320 Sarg = list_optarg;
321 break;
322 case 'W':
323 Warg = list_optarg;
324 break;
325 case 'X':
326 Xarg = list_optarg;
327 break;
328 CASE_HELPOPT;
329 default:
330 builtin_usage ();
331 return (EX_USAGE);
332 }
333 }
334
335 *actp = acts;
336 *optp = copts;
337
338 return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
339 }
340
341 /* Add, remove, and display completion specifiers. */
342 int
343 complete_builtin (list)
344 WORD_LIST *list;
345 {
346 int opt_given, rval;
347 unsigned long acts, copts;
348 COMPSPEC *cs;
349 struct _optflags oflags;
350 WORD_LIST *l, *wl;
351
352 if (list == 0)
353 {
354 print_all_completions ();
355 return (EXECUTION_SUCCESS);
356 }
357
358 opt_given = oflags.pflag = oflags.rflag = oflags.Dflag = oflags.Eflag = 0;
359
360 acts = copts = (unsigned long)0L;
361 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
362 cs = (COMPSPEC *)NULL;
363
364 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
365 as a side effect if they are supplied as options. */
366 rval = build_actions (list, &oflags, &acts, &copts);
367 if (rval == EX_USAGE)
368 return (rval);
369 opt_given = rval != EXECUTION_FAILURE;
370
371 list = loptend;
372
373 wl = oflags.Dflag ? make_word_list (make_bare_word (DEFAULTCMD), (WORD_LIST *)NULL)
374 : (oflags.Eflag ? make_word_list (make_bare_word (EMPTYCMD), (WORD_LIST *)NULL) : 0);
375
376 /* -p overrides everything else */
377 if (oflags.pflag || (list == 0 && opt_given == 0))
378 {
379 if (wl)
380 {
381 rval = print_cmd_completions (wl);
382 dispose_words (wl);
383 return rval;
384 }
385 else if (list == 0)
386 {
387 print_all_completions ();
388 return (EXECUTION_SUCCESS);
389 }
390 return (print_cmd_completions (list));
391 }
392
393 /* next, -r overrides everything else. */
394 if (oflags.rflag)
395 {
396 if (wl)
397 {
398 rval = remove_cmd_completions (wl);
399 dispose_words (wl);
400 return rval;
401 }
402 else if (list == 0)
403 {
404 progcomp_flush ();
405 return (EXECUTION_SUCCESS);
406 }
407 return (remove_cmd_completions (list));
408 }
409
410 if (wl == 0 && list == 0 && opt_given)
411 {
412 builtin_usage ();
413 return (EX_USAGE);
414 }
415
416 /* If we get here, we need to build a compspec and add it for each
417 remaining argument. */
418 cs = compspec_create ();
419 cs->actions = acts;
420 cs->options = copts;
421
422 cs->globpat = STRDUP (Garg);
423 cs->words = STRDUP (Warg);
424 cs->prefix = STRDUP (Parg);
425 cs->suffix = STRDUP (Sarg);
426 cs->funcname = STRDUP (Farg);
427 cs->command = STRDUP (Carg);
428 cs->filterpat = STRDUP (Xarg);
429
430 for (rval = EXECUTION_SUCCESS, l = wl ? wl : list ; l; l = l->next)
431 {
432 /* Add CS as the compspec for the specified commands. */
433 if (progcomp_insert (l->word->word, cs) == 0)
434 rval = EXECUTION_FAILURE;
435 }
436
437 dispose_words (wl);
438 return (rval);
439 }
440
441 static int
442 remove_cmd_completions (list)
443 WORD_LIST *list;
444 {
445 WORD_LIST *l;
446 int ret;
447
448 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
449 {
450 if (progcomp_remove (l->word->word) == 0)
451 {
452 builtin_error (_("%s: no completion specification"), l->word->word);
453 ret = EXECUTION_FAILURE;
454 }
455 }
456 return ret;
457 }
458
459 #define SQPRINTARG(a, f) \
460 do { \
461 if (a) \
462 { \
463 x = sh_single_quote (a); \
464 printf ("%s %s ", f, x); \
465 free (x); \
466 } \
467 } while (0)
468
469 #define PRINTARG(a, f) \
470 do { \
471 if (a) \
472 printf ("%s %s ", f, a); \
473 } while (0)
474
475 #define PRINTOPT(a, f) \
476 do { \
477 if (acts & a) \
478 printf ("%s ", f); \
479 } while (0)
480
481 #define PRINTACT(a, f) \
482 do { \
483 if (acts & a) \
484 printf ("-A %s ", f); \
485 } while (0)
486
487 #define PRINTCOMPOPT(a, f) \
488 do { \
489 if (copts & a) \
490 printf ("-o %s ", f); \
491 } while (0)
492
493 #define XPRINTCOMPOPT(a, f) \
494 do { \
495 if (copts & a) \
496 printf ("-o %s ", f); \
497 else \
498 printf ("+o %s ", f); \
499 } while (0)
500
501 static int
502 print_one_completion (cmd, cs)
503 char *cmd;
504 COMPSPEC *cs;
505 {
506 unsigned long acts, copts;
507 char *x;
508
509 printf ("complete ");
510
511 copts = cs->options;
512
513 /* First, print the -o options. */
514 PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
515 PRINTCOMPOPT (COPT_DEFAULT, "default");
516 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
517 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
518 PRINTCOMPOPT (COPT_NOSPACE, "nospace");
519 PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
520
521 acts = cs->actions;
522
523 /* simple flags next */
524 PRINTOPT (CA_ALIAS, "-a");
525 PRINTOPT (CA_BUILTIN, "-b");
526 PRINTOPT (CA_COMMAND, "-c");
527 PRINTOPT (CA_DIRECTORY, "-d");
528 PRINTOPT (CA_EXPORT, "-e");
529 PRINTOPT (CA_FILE, "-f");
530 PRINTOPT (CA_GROUP, "-g");
531 PRINTOPT (CA_JOB, "-j");
532 PRINTOPT (CA_KEYWORD, "-k");
533 PRINTOPT (CA_SERVICE, "-s");
534 PRINTOPT (CA_USER, "-u");
535 PRINTOPT (CA_VARIABLE, "-v");
536
537 /* now the rest of the actions */
538 PRINTACT (CA_ARRAYVAR, "arrayvar");
539 PRINTACT (CA_BINDING, "binding");
540 PRINTACT (CA_DISABLED, "disabled");
541 PRINTACT (CA_ENABLED, "enabled");
542 PRINTACT (CA_FUNCTION, "function");
543 PRINTACT (CA_HELPTOPIC, "helptopic");
544 PRINTACT (CA_HOSTNAME, "hostname");
545 PRINTACT (CA_RUNNING, "running");
546 PRINTACT (CA_SETOPT, "setopt");
547 PRINTACT (CA_SHOPT, "shopt");
548 PRINTACT (CA_SIGNAL, "signal");
549 PRINTACT (CA_STOPPED, "stopped");
550
551 /* now the rest of the arguments */
552
553 /* arguments that require quoting */
554 SQPRINTARG (cs->globpat, "-G");
555 SQPRINTARG (cs->words, "-W");
556 SQPRINTARG (cs->prefix, "-P");
557 SQPRINTARG (cs->suffix, "-S");
558 SQPRINTARG (cs->filterpat, "-X");
559
560 SQPRINTARG (cs->command, "-C");
561
562 /* simple arguments that don't require quoting */
563 PRINTARG (cs->funcname, "-F");
564
565 if (STREQ (cmd, EMPTYCMD))
566 printf ("-E\n");
567 else if (STREQ (cmd, DEFAULTCMD))
568 printf ("-D\n");
569 else
570 printf ("%s\n", cmd);
571
572 return (0);
573 }
574
575 static void
576 print_compopts (cmd, cs, full)
577 const char *cmd;
578 COMPSPEC *cs;
579 int full;
580 {
581 int copts;
582
583 printf ("compopt ");
584 copts = cs->options;
585
586 if (full)
587 {
588 XPRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
589 XPRINTCOMPOPT (COPT_DEFAULT, "default");
590 XPRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
591 XPRINTCOMPOPT (COPT_FILENAMES, "filenames");
592 XPRINTCOMPOPT (COPT_NOSPACE, "nospace");
593 XPRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
594 }
595 else
596 {
597 PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault");
598 PRINTCOMPOPT (COPT_DEFAULT, "default");
599 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
600 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
601 PRINTCOMPOPT (COPT_NOSPACE, "nospace");
602 PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
603 }
604
605 if (STREQ (cmd, EMPTYCMD))
606 printf ("-E\n");
607 else if (STREQ (cmd, DEFAULTCMD))
608 printf ("-D\n");
609 else
610 printf ("%s\n", cmd);
611 }
612
613 static int
614 print_compitem (item)
615 BUCKET_CONTENTS *item;
616 {
617 COMPSPEC *cs;
618 char *cmd;
619
620 cmd = item->key;
621 cs = (COMPSPEC *)item->data;
622
623 return (print_one_completion (cmd, cs));
624 }
625
626 static void
627 print_all_completions ()
628 {
629 progcomp_walk (print_compitem);
630 }
631
632 static int
633 print_cmd_completions (list)
634 WORD_LIST *list;
635 {
636 WORD_LIST *l;
637 COMPSPEC *cs;
638 int ret;
639
640 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
641 {
642 cs = progcomp_search (l->word->word);
643 if (cs)
644 print_one_completion (l->word->word, cs);
645 else
646 {
647 builtin_error (_("%s: no completion specification"), l->word->word);
648 ret = EXECUTION_FAILURE;
649 }
650 }
651
652 return (sh_chkwrite (ret));
653 }
654
655 $BUILTIN compgen
656 $DEPENDS_ON PROGRAMMABLE_COMPLETION
657 $FUNCTION compgen_builtin
658 $SHORT_DOC compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
659 Display possible completions depending on the options.
660
661 Intended to be used from within a shell function generating possible
662 completions. If the optional WORD argument is supplied, matches against
663 WORD are generated.
664
665 Exit Status:
666 Returns success unless an invalid option is supplied or an error occurs.
667 $END
668
669 int
670 compgen_builtin (list)
671 WORD_LIST *list;
672 {
673 int rval;
674 unsigned long acts, copts;
675 COMPSPEC *cs;
676 STRINGLIST *sl;
677 char *word, **matches;
678
679 if (list == 0)
680 return (EXECUTION_SUCCESS);
681
682 acts = copts = (unsigned long)0L;
683 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
684 cs = (COMPSPEC *)NULL;
685
686 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
687 as a side effect if they are supplied as options. */
688 rval = build_actions (list, (struct _optflags *)NULL, &acts, &copts);
689 if (rval == EX_USAGE)
690 return (rval);
691 if (rval == EXECUTION_FAILURE)
692 return (EXECUTION_SUCCESS);
693
694 list = loptend;
695
696 word = (list && list->word) ? list->word->word : "";
697
698 if (Farg)
699 builtin_error (_("warning: -F option may not work as you expect"));
700 if (Carg)
701 builtin_error (_("warning: -C option may not work as you expect"));
702
703 /* If we get here, we need to build a compspec and evaluate it. */
704 cs = compspec_create ();
705 cs->actions = acts;
706 cs->options = copts;
707 cs->refcount = 1;
708
709 cs->globpat = STRDUP (Garg);
710 cs->words = STRDUP (Warg);
711 cs->prefix = STRDUP (Parg);
712 cs->suffix = STRDUP (Sarg);
713 cs->funcname = STRDUP (Farg);
714 cs->command = STRDUP (Carg);
715 cs->filterpat = STRDUP (Xarg);
716
717 rval = EXECUTION_FAILURE;
718 sl = gen_compspec_completions (cs, "compgen", word, 0, 0, 0);
719
720 /* If the compspec wants the bash default completions, temporarily
721 turn off programmable completion and call the bash completion code. */
722 if ((sl == 0 || sl->list_len == 0) && (copts & COPT_BASHDEFAULT))
723 {
724 matches = bash_default_completion (word, 0, 0, 0, 0);
725 sl = completions_to_stringlist (matches);
726 strvec_dispose (matches);
727 }
728
729 /* This isn't perfect, but it's the best we can do, given what readline
730 exports from its set of completion utility functions. */
731 if ((sl == 0 || sl->list_len == 0) && (copts & COPT_DEFAULT))
732 {
733 matches = rl_completion_matches (word, rl_filename_completion_function);
734 strlist_dispose (sl);
735 sl = completions_to_stringlist (matches);
736 strvec_dispose (matches);
737 }
738
739 if (sl)
740 {
741 if (sl->list && sl->list_len)
742 {
743 rval = EXECUTION_SUCCESS;
744 strlist_print (sl, (char *)NULL);
745 }
746 strlist_dispose (sl);
747 }
748
749 compspec_dispose (cs);
750 return (rval);
751 }
752
753 $BUILTIN compopt
754 $DEPENDS_ON PROGRAMMABLE_COMPLETION
755 $FUNCTION compopt_builtin
756 $SHORT_DOC compopt [-o|+o option] [-DE] [name ...]
757 Modify or display completion options.
758
759 Modify the completion options for each NAME, or, if no NAMEs are supplied,
760 the completion currently being executed. If no OPTIONs are given, print
761 the completion options for each NAME or the current completion specification.
762
763 Options:
764 -o option Set completion option OPTION for each NAME
765 -D Change options for the "default" command completion
766 -E Change options for the "empty" command completion
767
768 Using `+o' instead of `-o' turns off the specified option.
769
770 Arguments:
771
772 Each NAME refers to a command for which a completion specification must
773 have previously been defined using the `complete' builtin. If no NAMEs
774 are supplied, compopt must be called by a function currently generating
775 completions, and the options for that currently-executing completion
776 generator are modified.
777
778 Exit Status:
779 Returns success unless an invalid option is supplied or NAME does not
780 have a completion specification defined.
781 $END
782
783 int
784 compopt_builtin (list)
785 WORD_LIST *list;
786 {
787 int opts_on, opts_off, *opts, opt, oind, ret, Dflag, Eflag;
788 WORD_LIST *l, *wl;
789 COMPSPEC *cs;
790
791 opts_on = opts_off = Eflag = Dflag = 0;
792 ret = EXECUTION_SUCCESS;
793
794 reset_internal_getopt ();
795 while ((opt = internal_getopt (list, "+o:DE")) != -1)
796 {
797 opts = (list_opttype == '-') ? &opts_on : &opts_off;
798
799 switch (opt)
800 {
801 case 'o':
802 oind = find_compopt (list_optarg);
803 if (oind < 0)
804 {
805 sh_invalidoptname (list_optarg);
806 return (EX_USAGE);
807 }
808 *opts |= compopts[oind].optflag;
809 break;
810 case 'D':
811 Dflag = 1;
812 break;
813 case 'E':
814 Eflag = 1;
815 break;
816 CASE_HELPOPT;
817 default:
818 builtin_usage ();
819 return (EX_USAGE);
820 }
821 }
822 list = loptend;
823
824 wl = Dflag ? make_word_list (make_bare_word (DEFAULTCMD), (WORD_LIST *)NULL)
825 : (Eflag ? make_word_list (make_bare_word (EMPTYCMD), (WORD_LIST *)NULL) : 0);
826
827 if (list == 0 && wl == 0)
828 {
829 if (RL_ISSTATE (RL_STATE_COMPLETING) == 0 || pcomp_curcs == 0)
830 {
831 builtin_error (_("not currently executing completion function"));
832 return (EXECUTION_FAILURE);
833 }
834 cs = pcomp_curcs;
835
836 if (opts_on == 0 && opts_off == 0)
837 {
838 print_compopts (pcomp_curcmd, cs, 1);
839 return (sh_chkwrite (ret));
840 }
841
842 /* Set the compspec options */
843 pcomp_set_compspec_options (cs, opts_on, 1);
844 pcomp_set_compspec_options (cs, opts_off, 0);
845
846 /* And change the readline variables the options control */
847 pcomp_set_readline_variables (opts_on, 1);
848 pcomp_set_readline_variables (opts_off, 0);
849
850 return (ret);
851 }
852
853 for (l = wl ? wl : list; l; l = l->next)
854 {
855 cs = progcomp_search (l->word->word);
856 if (cs == 0)
857 {
858 builtin_error (_("%s: no completion specification"), l->word->word);
859 ret = EXECUTION_FAILURE;
860 continue;
861 }
862 if (opts_on == 0 && opts_off == 0)
863 {
864 print_compopts (l->word->word, cs, 1);
865 continue; /* XXX -- fill in later */
866 }
867
868 /* Set the compspec options */
869 pcomp_set_compspec_options (cs, opts_on, 1);
870 pcomp_set_compspec_options (cs, opts_off, 0);
871 }
872
873 if (wl)
874 dispose_words (wl);
875
876 return (ret);
877 }