]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/complete.def
bash-5.0 distribution sources and documentation
[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] [-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.
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 -I apply the completions and actions to the initial (usually the
42 command) word
43
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.
47
48 Exit Status:
49 Returns success unless an invalid option is supplied or an error occurs.
50 $END
51
52 #include <config.h>
53
54 #include <stdio.h>
55
56 #include "../bashtypes.h"
57
58 #if defined (HAVE_UNISTD_H)
59 # include <unistd.h>
60 #endif
61
62 #include "../bashansi.h"
63 #include "../bashintl.h"
64
65 #include "../shell.h"
66 #include "../builtins.h"
67 #include "../pcomplete.h"
68 #include "../bashline.h"
69
70 #include "common.h"
71 #include "bashgetopt.h"
72
73 #include <readline/readline.h>
74
75 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
76
77 /* Structure containing all the non-action (binary) options; filled in by
78 build_actions(). */
79 struct _optflags {
80 int pflag;
81 int rflag;
82 int Dflag;
83 int Eflag;
84 int Iflag;
85 };
86
87 static int find_compact __P((char *));
88 static int find_compopt __P((char *));
89
90 static int build_actions __P((WORD_LIST *, struct _optflags *, unsigned long *, unsigned long *));
91
92 static int remove_cmd_completions __P((WORD_LIST *));
93
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 *));
99
100 static char *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg;
101
102 static const struct _compacts {
103 const char * const actname;
104 int actflag;
105 int actopt;
106 } compacts[] = {
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 },
132 };
133
134 /* This should be a STRING_INT_ALIST */
135 static const struct _compopt {
136 const char * const optname;
137 int optflag;
138 } compopts[] = {
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 },
147 { (char *)NULL, 0 },
148 };
149
150 static int
151 find_compact (name)
152 char *name;
153 {
154 register int i;
155
156 for (i = 0; compacts[i].actname; i++)
157 if (STREQ (name, compacts[i].actname))
158 return i;
159 return -1;
160 }
161
162 static int
163 find_compopt (name)
164 char *name;
165 {
166 register int i;
167
168 for (i = 0; compopts[i].optname; i++)
169 if (STREQ (name, compopts[i].optname))
170 return i;
171 return -1;
172 }
173
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
186 */
187
188 static int
189 build_actions (list, flagp, actp, optp)
190 WORD_LIST *list;
191 struct _optflags *flagp;
192 unsigned long *actp, *optp;
193 {
194 int opt, ind, opt_given;
195 unsigned long acts, copts;
196
197 acts = copts = (unsigned long)0L;
198 opt_given = 0;
199
200 reset_internal_getopt ();
201 while ((opt = internal_getopt (list, "abcdefgjko:prsuvA:G:W:P:S:X:F:C:DEI")) != -1)
202 {
203 opt_given = 1;
204 switch (opt)
205 {
206 case 'r':
207 if (flagp)
208 {
209 flagp->rflag = 1;
210 break;
211 }
212 else
213 {
214 sh_invalidopt ("-r");
215 builtin_usage ();
216 return (EX_USAGE);
217 }
218
219 case 'p':
220 if (flagp)
221 {
222 flagp->pflag = 1;
223 break;
224 }
225 else
226 {
227 sh_invalidopt ("-p");
228 builtin_usage ();
229 return (EX_USAGE);
230 }
231
232 case 'a':
233 acts |= CA_ALIAS;
234 break;
235 case 'b':
236 acts |= CA_BUILTIN;
237 break;
238 case 'c':
239 acts |= CA_COMMAND;
240 break;
241 case 'd':
242 acts |= CA_DIRECTORY;
243 break;
244 case 'e':
245 acts |= CA_EXPORT;
246 break;
247 case 'f':
248 acts |= CA_FILE;
249 break;
250 case 'g':
251 acts |= CA_GROUP;
252 break;
253 case 'j':
254 acts |= CA_JOB;
255 break;
256 case 'k':
257 acts |= CA_KEYWORD;
258 break;
259 case 's':
260 acts |= CA_SERVICE;
261 break;
262 case 'u':
263 acts |= CA_USER;
264 break;
265 case 'v':
266 acts |= CA_VARIABLE;
267 break;
268 case 'o':
269 ind = find_compopt (list_optarg);
270 if (ind < 0)
271 {
272 sh_invalidoptname (list_optarg);
273 return (EX_USAGE);
274 }
275 copts |= compopts[ind].optflag;
276 break;
277 case 'A':
278 ind = find_compact (list_optarg);
279 if (ind < 0)
280 {
281 builtin_error (_("%s: invalid action name"), list_optarg);
282 return (EX_USAGE);
283 }
284 acts |= compacts[ind].actflag;
285 break;
286 case 'C':
287 Carg = list_optarg;
288 break;
289 case 'D':
290 if (flagp)
291 {
292 flagp->Dflag = 1;
293 break;
294 }
295 else
296 {
297 sh_invalidopt ("-D");
298 builtin_usage ();
299 return (EX_USAGE);
300 }
301 case 'E':
302 if (flagp)
303 {
304 flagp->Eflag = 1;
305 break;
306 }
307 else
308 {
309 sh_invalidopt ("-E");
310 builtin_usage ();
311 return (EX_USAGE);
312 }
313 case 'I':
314 if (flagp)
315 {
316 flagp->Iflag = 1;
317 break;
318 }
319 else
320 {
321 sh_invalidopt ("-I");
322 builtin_usage ();
323 return (EX_USAGE);
324 }
325 case 'F':
326 Farg = list_optarg;
327 break;
328 case 'G':
329 Garg = list_optarg;
330 break;
331 case 'P':
332 Parg = list_optarg;
333 break;
334 case 'S':
335 Sarg = list_optarg;
336 break;
337 case 'W':
338 Warg = list_optarg;
339 break;
340 case 'X':
341 Xarg = list_optarg;
342 break;
343 CASE_HELPOPT;
344 default:
345 builtin_usage ();
346 return (EX_USAGE);
347 }
348 }
349
350 *actp = acts;
351 *optp = copts;
352
353 return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
354 }
355
356 /* Add, remove, and display completion specifiers. */
357 int
358 complete_builtin (list)
359 WORD_LIST *list;
360 {
361 int opt_given, rval;
362 unsigned long acts, copts;
363 COMPSPEC *cs;
364 struct _optflags oflags;
365 WORD_LIST *l, *wl;
366
367 if (list == 0)
368 {
369 print_all_completions ();
370 return (EXECUTION_SUCCESS);
371 }
372
373 opt_given = oflags.pflag = oflags.rflag = 0;
374 oflags.Dflag = oflags.Eflag = oflags.Iflag = 0;
375
376 acts = copts = (unsigned long)0L;
377 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
378 cs = (COMPSPEC *)NULL;
379
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)
384 return (rval);
385 opt_given = rval != EXECUTION_FAILURE;
386
387 list = loptend;
388
389 if (oflags.Dflag)
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);
395 else
396 wl = (WORD_LIST *)NULL;
397
398 /* -p overrides everything else */
399 if (oflags.pflag || (list == 0 && opt_given == 0))
400 {
401 if (wl)
402 {
403 rval = print_cmd_completions (wl);
404 dispose_words (wl);
405 return rval;
406 }
407 else if (list == 0)
408 {
409 print_all_completions ();
410 return (EXECUTION_SUCCESS);
411 }
412 return (print_cmd_completions (list));
413 }
414
415 /* next, -r overrides everything else. */
416 if (oflags.rflag)
417 {
418 if (wl)
419 {
420 rval = remove_cmd_completions (wl);
421 dispose_words (wl);
422 return rval;
423 }
424 else if (list == 0)
425 {
426 progcomp_flush ();
427 return (EXECUTION_SUCCESS);
428 }
429 return (remove_cmd_completions (list));
430 }
431
432 if (wl == 0 && list == 0 && opt_given)
433 {
434 builtin_usage ();
435 return (EX_USAGE);
436 }
437
438 /* If we get here, we need to build a compspec and add it for each
439 remaining argument. */
440 cs = compspec_create ();
441 cs->actions = acts;
442 cs->options = copts;
443
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);
451
452 for (rval = EXECUTION_SUCCESS, l = wl ? wl : list ; l; l = l->next)
453 {
454 /* Add CS as the compspec for the specified commands. */
455 if (progcomp_insert (l->word->word, cs) == 0)
456 rval = EXECUTION_FAILURE;
457 }
458
459 dispose_words (wl);
460 return (rval);
461 }
462
463 static int
464 remove_cmd_completions (list)
465 WORD_LIST *list;
466 {
467 WORD_LIST *l;
468 int ret;
469
470 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
471 {
472 if (progcomp_remove (l->word->word) == 0)
473 {
474 builtin_error (_("%s: no completion specification"), l->word->word);
475 ret = EXECUTION_FAILURE;
476 }
477 }
478 return ret;
479 }
480
481 #define SQPRINTARG(a, f) \
482 do { \
483 if (a) \
484 { \
485 x = sh_single_quote (a); \
486 printf ("%s %s ", f, x); \
487 free (x); \
488 } \
489 } while (0)
490
491 #define PRINTARG(a, f) \
492 do { \
493 if (a) \
494 printf ("%s %s ", f, a); \
495 } while (0)
496
497 #define PRINTOPT(a, f) \
498 do { \
499 if (acts & a) \
500 printf ("%s ", f); \
501 } while (0)
502
503 #define PRINTACT(a, f) \
504 do { \
505 if (acts & a) \
506 printf ("-A %s ", f); \
507 } while (0)
508
509 #define PRINTCOMPOPT(a, f) \
510 do { \
511 if (copts & a) \
512 printf ("-o %s ", f); \
513 } while (0)
514
515 #define XPRINTCOMPOPT(a, f) \
516 do { \
517 if (copts & a) \
518 printf ("-o %s ", f); \
519 else \
520 printf ("+o %s ", f); \
521 } while (0)
522
523 static int
524 print_one_completion (cmd, cs)
525 char *cmd;
526 COMPSPEC *cs;
527 {
528 unsigned long acts, copts;
529 char *x;
530
531 printf ("complete ");
532
533 copts = cs->options;
534
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");
544
545 acts = cs->actions;
546
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");
560
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");
574
575 /* now the rest of the arguments */
576
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");
583
584 SQPRINTARG (cs->command, "-C");
585
586 /* simple arguments that don't require quoting */
587 PRINTARG (cs->funcname, "-F");
588
589 if (STREQ (cmd, DEFAULTCMD))
590 printf ("-D\n");
591 else if (STREQ (cmd, EMPTYCMD))
592 printf ("-E\n");
593 else if (STREQ (cmd, INITIALWORD))
594 printf ("-I\n");
595 else
596 printf ("%s\n", cmd);
597
598 return (0);
599 }
600
601 static void
602 print_compopts (cmd, cs, full)
603 const char *cmd;
604 COMPSPEC *cs;
605 int full;
606 {
607 int copts;
608
609 printf ("compopt ");
610 copts = cs->options;
611
612 if (full)
613 {
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");
622 }
623 else
624 {
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");
633 }
634
635 if (STREQ (cmd, DEFAULTCMD))
636 printf ("-D\n");
637 else if (STREQ (cmd, EMPTYCMD))
638 printf ("-E\n");
639 else if (STREQ (cmd, INITIALWORD))
640 printf ("-I\n");
641 else
642 printf ("%s\n", cmd);
643 }
644
645 static int
646 print_compitem (item)
647 BUCKET_CONTENTS *item;
648 {
649 COMPSPEC *cs;
650 char *cmd;
651
652 cmd = item->key;
653 cs = (COMPSPEC *)item->data;
654
655 return (print_one_completion (cmd, cs));
656 }
657
658 static void
659 print_all_completions ()
660 {
661 progcomp_walk (print_compitem);
662 }
663
664 static int
665 print_cmd_completions (list)
666 WORD_LIST *list;
667 {
668 WORD_LIST *l;
669 COMPSPEC *cs;
670 int ret;
671
672 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
673 {
674 cs = progcomp_search (l->word->word);
675 if (cs)
676 print_one_completion (l->word->word, cs);
677 else
678 {
679 builtin_error (_("%s: no completion specification"), l->word->word);
680 ret = EXECUTION_FAILURE;
681 }
682 }
683
684 return (sh_chkwrite (ret));
685 }
686
687 $BUILTIN compgen
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.
692
693 Intended to be used from within a shell function generating possible
694 completions. If the optional WORD argument is supplied, matches against
695 WORD are generated.
696
697 Exit Status:
698 Returns success unless an invalid option is supplied or an error occurs.
699 $END
700
701 int
702 compgen_builtin (list)
703 WORD_LIST *list;
704 {
705 int rval;
706 unsigned long acts, copts;
707 COMPSPEC *cs;
708 STRINGLIST *sl;
709 char *word, **matches;
710 char *old_line;
711 int old_ind;
712
713 if (list == 0)
714 return (EXECUTION_SUCCESS);
715
716 acts = copts = (unsigned long)0L;
717 Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
718 cs = (COMPSPEC *)NULL;
719
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)
724 return (rval);
725 if (rval == EXECUTION_FAILURE)
726 return (EXECUTION_SUCCESS);
727
728 list = loptend;
729
730 word = (list && list->word) ? list->word->word : "";
731
732 if (Farg)
733 builtin_error (_("warning: -F option may not work as you expect"));
734 if (Carg)
735 builtin_error (_("warning: -C option may not work as you expect"));
736
737 /* If we get here, we need to build a compspec and evaluate it. */
738 cs = compspec_create ();
739 cs->actions = acts;
740 cs->options = copts;
741 cs->refcount = 1;
742
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);
750
751 rval = EXECUTION_FAILURE;
752
753 /* probably don't have to save these, just being safe */
754 old_line = pcomp_line;
755 old_ind = pcomp_ind;
756 pcomp_line = (char *)NULL;
757 pcomp_ind = 0;
758 sl = gen_compspec_completions (cs, "compgen", word, 0, 0, 0);
759 pcomp_line = old_line;
760 pcomp_ind = old_ind;
761
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))
765 {
766 matches = bash_default_completion (word, 0, 0, 0, 0);
767 sl = completions_to_stringlist (matches);
768 strvec_dispose (matches);
769 }
770
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))
774 {
775 matches = rl_completion_matches (word, rl_filename_completion_function);
776 strlist_dispose (sl);
777 sl = completions_to_stringlist (matches);
778 strvec_dispose (matches);
779 }
780
781 if (sl)
782 {
783 if (sl->list && sl->list_len)
784 {
785 rval = EXECUTION_SUCCESS;
786 strlist_print (sl, (char *)NULL);
787 }
788 strlist_dispose (sl);
789 }
790
791 compspec_dispose (cs);
792 return (rval);
793 }
794
795 $BUILTIN compopt
796 $DEPENDS_ON PROGRAMMABLE_COMPLETION
797 $FUNCTION compopt_builtin
798 $SHORT_DOC compopt [-o|+o option] [-DEI] [name ...]
799 Modify or display completion options.
800
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.
804
805 Options:
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
810
811 Using `+o' instead of `-o' turns off the specified option.
812
813 Arguments:
814
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.
820
821 Exit Status:
822 Returns success unless an invalid option is supplied or NAME does not
823 have a completion specification defined.
824 $END
825
826 int
827 compopt_builtin (list)
828 WORD_LIST *list;
829 {
830 int opts_on, opts_off, *opts, opt, oind, ret, Dflag, Eflag, Iflag;
831 WORD_LIST *l, *wl;
832 COMPSPEC *cs;
833
834 opts_on = opts_off = Eflag = Dflag = Iflag = 0;
835 ret = EXECUTION_SUCCESS;
836
837 reset_internal_getopt ();
838 while ((opt = internal_getopt (list, "+o:DEI")) != -1)
839 {
840 opts = (list_opttype == '-') ? &opts_on : &opts_off;
841
842 switch (opt)
843 {
844 case 'o':
845 oind = find_compopt (list_optarg);
846 if (oind < 0)
847 {
848 sh_invalidoptname (list_optarg);
849 return (EX_USAGE);
850 }
851 *opts |= compopts[oind].optflag;
852 break;
853 case 'D':
854 Dflag = 1;
855 break;
856 case 'E':
857 Eflag = 1;
858 break;
859 case 'I':
860 Iflag = 1;
861 break;
862 CASE_HELPOPT;
863 default:
864 builtin_usage ();
865 return (EX_USAGE);
866 }
867 }
868 list = loptend;
869
870 if (Dflag)
871 wl = make_word_list (make_bare_word (DEFAULTCMD), (WORD_LIST *)NULL);
872 else if (Eflag)
873 wl = make_word_list (make_bare_word (EMPTYCMD), (WORD_LIST *)NULL);
874 else if (Iflag)
875 wl = make_word_list (make_bare_word (INITIALWORD), (WORD_LIST *)NULL);
876 else
877 wl = (WORD_LIST *)NULL;
878
879 if (list == 0 && wl == 0)
880 {
881 if (RL_ISSTATE (RL_STATE_COMPLETING) == 0 || pcomp_curcs == 0)
882 {
883 builtin_error (_("not currently executing completion function"));
884 return (EXECUTION_FAILURE);
885 }
886 cs = pcomp_curcs;
887
888 if (opts_on == 0 && opts_off == 0)
889 {
890 print_compopts (pcomp_curcmd, cs, 1);
891 return (sh_chkwrite (ret));
892 }
893
894 /* Set the compspec options */
895 pcomp_set_compspec_options (cs, opts_on, 1);
896 pcomp_set_compspec_options (cs, opts_off, 0);
897
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);
901
902 return (ret);
903 }
904
905 for (l = wl ? wl : list; l; l = l->next)
906 {
907 cs = progcomp_search (l->word->word);
908 if (cs == 0)
909 {
910 builtin_error (_("%s: no completion specification"), l->word->word);
911 ret = EXECUTION_FAILURE;
912 continue;
913 }
914 if (opts_on == 0 && opts_off == 0)
915 {
916 print_compopts (l->word->word, cs, 1);
917 continue; /* XXX -- fill in later */
918 }
919
920 /* Set the compspec options */
921 pcomp_set_compspec_options (cs, opts_on, 1);
922 pcomp_set_compspec_options (cs, opts_off, 0);
923 }
924
925 if (wl)
926 dispose_words (wl);
927
928 return (ret);
929 }