]>
Commit | Line | Data |
---|---|---|
bb70624e | 1 | This file is complete.def, from which is created complete.c. |
3185942a | 2 | It implements the builtins "complete", "compgen", and "compopt" in Bash. |
bb70624e | 3 | |
3185942a | 4 | Copyright (C) 1999-2009 Free Software Foundation, Inc. |
bb70624e JA |
5 | |
6 | This file is part of GNU Bash, the Bourne Again SHell. | |
7 | ||
3185942a JA |
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. | |
bb70624e | 12 | |
3185942a JA |
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. | |
bb70624e | 17 | |
3185942a JA |
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/>. | |
bb70624e JA |
20 | |
21 | $PRODUCES complete.c | |
22 | ||
23 | $BUILTIN complete | |
24 | $DEPENDS_ON PROGRAMMABLE_COMPLETION | |
25 | $FUNCTION complete_builtin | |
3185942a JA |
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. | |
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 | ||
38 | When completion is attempted, the actions are applied in the order the | |
39 | uppercase-letter options are listed above. | |
40 | ||
41 | Exit Status: | |
42 | Returns success unless an invalid option is supplied or an error occurs. | |
bb70624e JA |
43 | $END |
44 | ||
45 | #include <config.h> | |
46 | ||
47 | #include <stdio.h> | |
48 | ||
49 | #include "../bashtypes.h" | |
50 | ||
51 | #if defined (HAVE_UNISTD_H) | |
52 | # include <unistd.h> | |
53 | #endif | |
54 | ||
55 | #include "../bashansi.h" | |
b80f6443 | 56 | #include "../bashintl.h" |
bb70624e JA |
57 | |
58 | #include "../shell.h" | |
59 | #include "../builtins.h" | |
60 | #include "../pcomplete.h" | |
b80f6443 | 61 | #include "../bashline.h" |
bb70624e JA |
62 | |
63 | #include "common.h" | |
64 | #include "bashgetopt.h" | |
65 | ||
f73dda09 JA |
66 | #include <readline/readline.h> |
67 | ||
bb70624e JA |
68 | #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL) |
69 | ||
3185942a JA |
70 | /* Structure containing all the non-action (binary) options; filled in by |
71 | build_actions(). */ | |
72 | struct _optflags { | |
73 | int pflag; | |
74 | int rflag; | |
75 | int Eflag; | |
76 | }; | |
77 | ||
f73dda09 JA |
78 | static int find_compact __P((char *)); |
79 | static int find_compopt __P((char *)); | |
80 | ||
3185942a | 81 | static int build_actions __P((WORD_LIST *, struct _optflags *, unsigned long *, unsigned long *)); |
f73dda09 JA |
82 | |
83 | static int remove_cmd_completions __P((WORD_LIST *)); | |
bb70624e | 84 | |
7117c2d2 JA |
85 | static int print_one_completion __P((char *, COMPSPEC *)); |
86 | static int print_compitem __P((BUCKET_CONTENTS *)); | |
3185942a | 87 | static void print_compopts __P((const char *, COMPSPEC *, int)); |
f73dda09 JA |
88 | static void print_all_completions __P((void)); |
89 | static int print_cmd_completions __P((WORD_LIST *)); | |
bb70624e | 90 | |
7117c2d2 | 91 | static char *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg; |
bb70624e | 92 | |
3185942a JA |
93 | static const struct _compacts { |
94 | const char * const actname; | |
bb70624e JA |
95 | int actflag; |
96 | int actopt; | |
97 | } compacts[] = { | |
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 }, | |
f73dda09 | 111 | { "group", CA_GROUP, 'g' }, |
bb70624e JA |
112 | { "job", CA_JOB, 'j' }, |
113 | { "keyword", CA_KEYWORD, 'k' }, | |
114 | { "running", CA_RUNNING, 0 }, | |
7117c2d2 | 115 | { "service", CA_SERVICE, 's' }, |
bb70624e JA |
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 }, | |
123 | }; | |
124 | ||
7117c2d2 | 125 | /* This should be a STRING_INT_ALIST */ |
3185942a JA |
126 | const static struct _compopt { |
127 | const char * const optname; | |
28ef6c31 JA |
128 | int optflag; |
129 | } compopts[] = { | |
b80f6443 | 130 | { "bashdefault", COPT_BASHDEFAULT }, |
28ef6c31 JA |
131 | { "default", COPT_DEFAULT }, |
132 | { "dirnames", COPT_DIRNAMES }, | |
133 | { "filenames",COPT_FILENAMES}, | |
7117c2d2 | 134 | { "nospace", COPT_NOSPACE }, |
b80f6443 | 135 | { "plusdirs", COPT_PLUSDIRS }, |
28ef6c31 JA |
136 | { (char *)NULL, 0 }, |
137 | }; | |
138 | ||
bb70624e JA |
139 | static int |
140 | find_compact (name) | |
141 | char *name; | |
142 | { | |
143 | register int i; | |
144 | ||
145 | for (i = 0; compacts[i].actname; i++) | |
146 | if (STREQ (name, compacts[i].actname)) | |
147 | return i; | |
148 | return -1; | |
149 | } | |
150 | ||
28ef6c31 JA |
151 | static int |
152 | find_compopt (name) | |
153 | char *name; | |
154 | { | |
155 | register int i; | |
156 | ||
157 | for (i = 0; compopts[i].optname; i++) | |
158 | if (STREQ (name, compopts[i].optname)) | |
159 | return i; | |
160 | return -1; | |
161 | } | |
162 | ||
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: | |
bb70624e JA |
172 | EX_USAGE = bad option |
173 | EXECUTION_SUCCESS = some options supplied | |
174 | EXECUTION_FAILURE = no options supplied | |
175 | */ | |
176 | ||
177 | static int | |
3185942a | 178 | build_actions (list, flagp, actp, optp) |
bb70624e | 179 | WORD_LIST *list; |
3185942a | 180 | struct _optflags *flagp; |
28ef6c31 | 181 | unsigned long *actp, *optp; |
bb70624e | 182 | { |
f73dda09 | 183 | int opt, ind, opt_given; |
28ef6c31 | 184 | unsigned long acts, copts; |
bb70624e | 185 | |
28ef6c31 | 186 | acts = copts = (unsigned long)0L; |
bb70624e JA |
187 | opt_given = 0; |
188 | ||
189 | reset_internal_getopt (); | |
3185942a | 190 | while ((opt = internal_getopt (list, "abcdefgjko:prsuvA:G:W:P:S:X:F:C:E")) != -1) |
bb70624e JA |
191 | { |
192 | opt_given = 1; | |
193 | switch (opt) | |
194 | { | |
195 | case 'r': | |
3185942a | 196 | if (flagp) |
bb70624e | 197 | { |
3185942a | 198 | flagp->rflag = 1; |
bb70624e JA |
199 | break; |
200 | } | |
201 | else | |
202 | { | |
7117c2d2 | 203 | sh_invalidopt ("-r"); |
bb70624e JA |
204 | builtin_usage (); |
205 | return (EX_USAGE); | |
206 | } | |
207 | ||
208 | case 'p': | |
3185942a | 209 | if (flagp) |
bb70624e | 210 | { |
3185942a | 211 | flagp->pflag = 1; |
bb70624e JA |
212 | break; |
213 | } | |
214 | else | |
215 | { | |
7117c2d2 | 216 | sh_invalidopt ("-p"); |
bb70624e JA |
217 | builtin_usage (); |
218 | return (EX_USAGE); | |
219 | } | |
220 | ||
221 | case 'a': | |
222 | acts |= CA_ALIAS; | |
223 | break; | |
224 | case 'b': | |
225 | acts |= CA_BUILTIN; | |
226 | break; | |
227 | case 'c': | |
228 | acts |= CA_COMMAND; | |
229 | break; | |
230 | case 'd': | |
231 | acts |= CA_DIRECTORY; | |
232 | break; | |
233 | case 'e': | |
234 | acts |= CA_EXPORT; | |
235 | break; | |
236 | case 'f': | |
237 | acts |= CA_FILE; | |
238 | break; | |
f73dda09 JA |
239 | case 'g': |
240 | acts |= CA_GROUP; | |
241 | break; | |
bb70624e JA |
242 | case 'j': |
243 | acts |= CA_JOB; | |
244 | break; | |
245 | case 'k': | |
246 | acts |= CA_KEYWORD; | |
247 | break; | |
7117c2d2 JA |
248 | case 's': |
249 | acts |= CA_SERVICE; | |
250 | break; | |
bb70624e JA |
251 | case 'u': |
252 | acts |= CA_USER; | |
253 | break; | |
254 | case 'v': | |
255 | acts |= CA_VARIABLE; | |
256 | break; | |
28ef6c31 JA |
257 | case 'o': |
258 | ind = find_compopt (list_optarg); | |
259 | if (ind < 0) | |
260 | { | |
7117c2d2 | 261 | sh_invalidoptname (list_optarg); |
28ef6c31 JA |
262 | return (EX_USAGE); |
263 | } | |
264 | copts |= compopts[ind].optflag; | |
265 | break; | |
bb70624e JA |
266 | case 'A': |
267 | ind = find_compact (list_optarg); | |
268 | if (ind < 0) | |
269 | { | |
b80f6443 | 270 | builtin_error (_("%s: invalid action name"), list_optarg); |
bb70624e JA |
271 | return (EX_USAGE); |
272 | } | |
273 | acts |= compacts[ind].actflag; | |
274 | break; | |
275 | case 'C': | |
276 | Carg = list_optarg; | |
277 | break; | |
3185942a JA |
278 | case 'E': |
279 | if (flagp) | |
280 | { | |
281 | flagp->Eflag = 1; | |
282 | break; | |
283 | } | |
284 | else | |
285 | { | |
286 | sh_invalidopt ("-E"); | |
287 | builtin_usage (); | |
288 | return (EX_USAGE); | |
289 | } | |
bb70624e JA |
290 | case 'F': |
291 | Farg = list_optarg; | |
292 | break; | |
293 | case 'G': | |
294 | Garg = list_optarg; | |
295 | break; | |
296 | case 'P': | |
297 | Parg = list_optarg; | |
298 | break; | |
299 | case 'S': | |
300 | Sarg = list_optarg; | |
301 | break; | |
302 | case 'W': | |
303 | Warg = list_optarg; | |
304 | break; | |
305 | case 'X': | |
306 | Xarg = list_optarg; | |
307 | break; | |
308 | default: | |
309 | builtin_usage (); | |
310 | return (EX_USAGE); | |
311 | } | |
312 | } | |
313 | ||
314 | *actp = acts; | |
28ef6c31 JA |
315 | *optp = copts; |
316 | ||
bb70624e JA |
317 | return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
318 | } | |
319 | ||
320 | /* Add, remove, and display completion specifiers. */ | |
321 | int | |
322 | complete_builtin (list) | |
323 | WORD_LIST *list; | |
324 | { | |
3185942a | 325 | int opt_given, rval; |
28ef6c31 | 326 | unsigned long acts, copts; |
bb70624e | 327 | COMPSPEC *cs; |
3185942a JA |
328 | struct _optflags oflags; |
329 | WORD_LIST *l, *wl; | |
bb70624e JA |
330 | |
331 | if (list == 0) | |
332 | { | |
333 | print_all_completions (); | |
334 | return (EXECUTION_SUCCESS); | |
335 | } | |
336 | ||
3185942a JA |
337 | opt_given = oflags.pflag = oflags.rflag = oflags.Eflag = 0; |
338 | ||
28ef6c31 | 339 | acts = copts = (unsigned long)0L; |
7117c2d2 | 340 | Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL; |
bb70624e JA |
341 | cs = (COMPSPEC *)NULL; |
342 | ||
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. */ | |
3185942a | 345 | rval = build_actions (list, &oflags, &acts, &copts); |
bb70624e JA |
346 | if (rval == EX_USAGE) |
347 | return (rval); | |
348 | opt_given = rval != EXECUTION_FAILURE; | |
349 | ||
350 | list = loptend; | |
351 | ||
3185942a JA |
352 | wl = oflags.Eflag ? make_word_list (make_bare_word ("_EmptycmD_"), (WORD_LIST *)NULL) : 0; |
353 | ||
bb70624e | 354 | /* -p overrides everything else */ |
3185942a | 355 | if (oflags.pflag || (list == 0 && opt_given == 0)) |
bb70624e | 356 | { |
3185942a JA |
357 | if (wl) |
358 | { | |
359 | rval = print_cmd_completions (wl); | |
360 | dispose_words (wl); | |
361 | return rval; | |
362 | } | |
363 | else if (list == 0) | |
bb70624e JA |
364 | { |
365 | print_all_completions (); | |
366 | return (EXECUTION_SUCCESS); | |
367 | } | |
368 | return (print_cmd_completions (list)); | |
369 | } | |
370 | ||
371 | /* next, -r overrides everything else. */ | |
3185942a | 372 | if (oflags.rflag) |
bb70624e | 373 | { |
3185942a JA |
374 | if (wl) |
375 | { | |
376 | rval = remove_cmd_completions (wl); | |
377 | dispose_words (wl); | |
378 | return rval; | |
379 | } | |
380 | else if (list == 0) | |
bb70624e | 381 | { |
7117c2d2 | 382 | progcomp_flush (); |
bb70624e JA |
383 | return (EXECUTION_SUCCESS); |
384 | } | |
385 | return (remove_cmd_completions (list)); | |
386 | } | |
387 | ||
3185942a | 388 | if (wl == 0 && list == 0 && opt_given) |
bb70624e JA |
389 | { |
390 | builtin_usage (); | |
391 | return (EX_USAGE); | |
392 | } | |
393 | ||
394 | /* If we get here, we need to build a compspec and add it for each | |
395 | remaining argument. */ | |
7117c2d2 | 396 | cs = compspec_create (); |
bb70624e | 397 | cs->actions = acts; |
28ef6c31 | 398 | cs->options = copts; |
bb70624e JA |
399 | |
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); | |
407 | ||
3185942a | 408 | for (rval = EXECUTION_SUCCESS, l = wl ? wl : list ; l; l = l->next) |
bb70624e JA |
409 | { |
410 | /* Add CS as the compspec for the specified commands. */ | |
3185942a | 411 | if (progcomp_insert (l->word->word, cs) == 0) |
28ef6c31 | 412 | rval = EXECUTION_FAILURE; |
bb70624e JA |
413 | } |
414 | ||
3185942a | 415 | dispose_words (wl); |
bb70624e JA |
416 | return (rval); |
417 | } | |
418 | ||
419 | static int | |
420 | remove_cmd_completions (list) | |
421 | WORD_LIST *list; | |
422 | { | |
423 | WORD_LIST *l; | |
424 | int ret; | |
425 | ||
426 | for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next) | |
427 | { | |
7117c2d2 | 428 | if (progcomp_remove (l->word->word) == 0) |
bb70624e | 429 | { |
b80f6443 | 430 | builtin_error (_("%s: no completion specification"), l->word->word); |
bb70624e JA |
431 | ret = EXECUTION_FAILURE; |
432 | } | |
433 | } | |
434 | return ret; | |
435 | } | |
436 | ||
437 | #define SQPRINTARG(a, f) \ | |
438 | do { \ | |
439 | if (a) \ | |
440 | { \ | |
28ef6c31 | 441 | x = sh_single_quote (a); \ |
bb70624e JA |
442 | printf ("%s %s ", f, x); \ |
443 | free (x); \ | |
444 | } \ | |
445 | } while (0) | |
446 | ||
447 | #define PRINTARG(a, f) \ | |
448 | do { \ | |
449 | if (a) \ | |
450 | printf ("%s %s ", f, a); \ | |
451 | } while (0) | |
452 | ||
453 | #define PRINTOPT(a, f) \ | |
454 | do { \ | |
455 | if (acts & a) \ | |
456 | printf ("%s ", f); \ | |
457 | } while (0) | |
458 | ||
459 | #define PRINTACT(a, f) \ | |
460 | do { \ | |
461 | if (acts & a) \ | |
462 | printf ("-A %s ", f); \ | |
463 | } while (0) | |
464 | ||
28ef6c31 JA |
465 | #define PRINTCOMPOPT(a, f) \ |
466 | do { \ | |
467 | if (copts & a) \ | |
468 | printf ("-o %s ", f); \ | |
469 | } while (0) | |
470 | ||
3185942a JA |
471 | #define XPRINTCOMPOPT(a, f) \ |
472 | do { \ | |
473 | if (copts & a) \ | |
474 | printf ("-o %s ", f); \ | |
475 | else \ | |
476 | printf ("+o %s ", f); \ | |
477 | } while (0) | |
478 | ||
7117c2d2 | 479 | static int |
bb70624e JA |
480 | print_one_completion (cmd, cs) |
481 | char *cmd; | |
482 | COMPSPEC *cs; | |
483 | { | |
28ef6c31 | 484 | unsigned long acts, copts; |
bb70624e JA |
485 | char *x; |
486 | ||
487 | printf ("complete "); | |
488 | ||
28ef6c31 JA |
489 | copts = cs->options; |
490 | ||
491 | /* First, print the -o options. */ | |
b80f6443 | 492 | PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault"); |
28ef6c31 JA |
493 | PRINTCOMPOPT (COPT_DEFAULT, "default"); |
494 | PRINTCOMPOPT (COPT_DIRNAMES, "dirnames"); | |
495 | PRINTCOMPOPT (COPT_FILENAMES, "filenames"); | |
7117c2d2 | 496 | PRINTCOMPOPT (COPT_NOSPACE, "nospace"); |
b80f6443 | 497 | PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs"); |
28ef6c31 | 498 | |
bb70624e JA |
499 | acts = cs->actions; |
500 | ||
28ef6c31 | 501 | /* simple flags next */ |
bb70624e JA |
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"); | |
f73dda09 | 508 | PRINTOPT (CA_GROUP, "-g"); |
bb70624e | 509 | PRINTOPT (CA_JOB, "-j"); |
7117c2d2 JA |
510 | PRINTOPT (CA_KEYWORD, "-k"); |
511 | PRINTOPT (CA_SERVICE, "-s"); | |
bb70624e JA |
512 | PRINTOPT (CA_USER, "-u"); |
513 | PRINTOPT (CA_VARIABLE, "-v"); | |
514 | ||
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"); | |
528 | ||
529 | /* now the rest of the arguments */ | |
530 | ||
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"); | |
537 | ||
3185942a JA |
538 | SQPRINTARG (cs->command, "-C"); |
539 | ||
bb70624e JA |
540 | /* simple arguments that don't require quoting */ |
541 | PRINTARG (cs->funcname, "-F"); | |
bb70624e JA |
542 | |
543 | printf ("%s\n", cmd); | |
7117c2d2 JA |
544 | |
545 | return (0); | |
546 | } | |
547 | ||
3185942a JA |
548 | static void |
549 | print_compopts (cmd, cs, full) | |
550 | const char *cmd; | |
551 | COMPSPEC *cs; | |
552 | int full; | |
553 | { | |
554 | int copts; | |
555 | ||
556 | printf ("compopt "); | |
557 | copts = cs->options; | |
558 | ||
559 | if (full) | |
560 | { | |
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"); | |
567 | } | |
568 | else | |
569 | { | |
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"); | |
576 | } | |
577 | ||
578 | printf ("%s\n", cmd); | |
579 | } | |
580 | ||
7117c2d2 JA |
581 | static int |
582 | print_compitem (item) | |
583 | BUCKET_CONTENTS *item; | |
584 | { | |
585 | COMPSPEC *cs; | |
586 | char *cmd; | |
587 | ||
588 | cmd = item->key; | |
589 | cs = (COMPSPEC *)item->data; | |
590 | ||
591 | return (print_one_completion (cmd, cs)); | |
bb70624e JA |
592 | } |
593 | ||
594 | static void | |
595 | print_all_completions () | |
596 | { | |
7117c2d2 | 597 | progcomp_walk (print_compitem); |
bb70624e JA |
598 | } |
599 | ||
600 | static int | |
601 | print_cmd_completions (list) | |
602 | WORD_LIST *list; | |
603 | { | |
604 | WORD_LIST *l; | |
605 | COMPSPEC *cs; | |
606 | int ret; | |
607 | ||
608 | for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next) | |
609 | { | |
7117c2d2 | 610 | cs = progcomp_search (l->word->word); |
bb70624e | 611 | if (cs) |
28ef6c31 | 612 | print_one_completion (l->word->word, cs); |
bb70624e JA |
613 | else |
614 | { | |
b80f6443 | 615 | builtin_error (_("%s: no completion specification"), l->word->word); |
bb70624e JA |
616 | ret = EXECUTION_FAILURE; |
617 | } | |
618 | } | |
3185942a JA |
619 | |
620 | return (sh_chkwrite (ret)); | |
bb70624e JA |
621 | } |
622 | ||
623 | $BUILTIN compgen | |
624 | $DEPENDS_ON PROGRAMMABLE_COMPLETION | |
625 | $FUNCTION compgen_builtin | |
3185942a JA |
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. | |
628 | ||
629 | Intended to be used from within a shell function generating possible | |
630 | completions. If the optional WORD argument is supplied, matches against | |
631 | WORD are generated. | |
632 | ||
633 | Exit Status: | |
634 | Returns success unless an invalid option is supplied or an error occurs. | |
bb70624e JA |
635 | $END |
636 | ||
637 | int | |
638 | compgen_builtin (list) | |
639 | WORD_LIST *list; | |
640 | { | |
641 | int rval; | |
28ef6c31 | 642 | unsigned long acts, copts; |
bb70624e JA |
643 | COMPSPEC *cs; |
644 | STRINGLIST *sl; | |
b80f6443 | 645 | char *word, **matches; |
bb70624e JA |
646 | |
647 | if (list == 0) | |
648 | return (EXECUTION_SUCCESS); | |
649 | ||
28ef6c31 | 650 | acts = copts = (unsigned long)0L; |
7117c2d2 | 651 | Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL; |
bb70624e JA |
652 | cs = (COMPSPEC *)NULL; |
653 | ||
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. */ | |
3185942a | 656 | rval = build_actions (list, (struct _optflags *)NULL, &acts, &copts); |
bb70624e JA |
657 | if (rval == EX_USAGE) |
658 | return (rval); | |
659 | if (rval == EXECUTION_FAILURE) | |
660 | return (EXECUTION_SUCCESS); | |
661 | ||
662 | list = loptend; | |
663 | ||
664 | word = (list && list->word) ? list->word->word : ""; | |
665 | ||
666 | if (Farg) | |
b80f6443 | 667 | builtin_error (_("warning: -F option may not work as you expect")); |
bb70624e | 668 | if (Carg) |
b80f6443 | 669 | builtin_error (_("warning: -C option may not work as you expect")); |
bb70624e JA |
670 | |
671 | /* If we get here, we need to build a compspec and evaluate it. */ | |
7117c2d2 | 672 | cs = compspec_create (); |
bb70624e | 673 | cs->actions = acts; |
28ef6c31 | 674 | cs->options = copts; |
bb70624e JA |
675 | cs->refcount = 1; |
676 | ||
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); | |
684 | ||
685 | rval = EXECUTION_FAILURE; | |
686 | sl = gen_compspec_completions (cs, "compgen", word, 0, 0); | |
f73dda09 | 687 | |
b80f6443 JA |
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)) | |
691 | { | |
692 | matches = bash_default_completion (word, 0, 0, 0, 0); | |
693 | sl = completions_to_stringlist (matches); | |
694 | strvec_dispose (matches); | |
695 | } | |
696 | ||
f73dda09 JA |
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)) | |
700 | { | |
f73dda09 JA |
701 | matches = rl_completion_matches (word, rl_filename_completion_function); |
702 | sl = completions_to_stringlist (matches); | |
7117c2d2 | 703 | strvec_dispose (matches); |
f73dda09 JA |
704 | } |
705 | ||
bb70624e JA |
706 | if (sl) |
707 | { | |
f73dda09 | 708 | if (sl->list && sl->list_len) |
bb70624e JA |
709 | { |
710 | rval = EXECUTION_SUCCESS; | |
7117c2d2 | 711 | strlist_print (sl, (char *)NULL); |
bb70624e | 712 | } |
7117c2d2 | 713 | strlist_dispose (sl); |
bb70624e JA |
714 | } |
715 | ||
7117c2d2 | 716 | compspec_dispose (cs); |
bb70624e JA |
717 | return (rval); |
718 | } | |
3185942a JA |
719 | |
720 | $BUILTIN compopt | |
721 | $DEPENDS_ON PROGRAMMABLE_COMPLETION | |
722 | $FUNCTION compopt_builtin | |
723 | $SHORT_DOC compopt [-o|+o option] [name ...] | |
724 | Modify or display completion options. | |
725 | ||
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. | |
729 | ||
730 | Options: | |
731 | -o option Set completion option OPTION for each NAME | |
732 | ||
733 | Using `+o' instead of `-o' turns off the specified option. | |
734 | ||
735 | Arguments: | |
736 | ||
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. | |
742 | ||
743 | Exit Status: | |
744 | Returns success unless an invalid option is supplied or NAME does not | |
745 | have a completion specification defined. | |
746 | $END | |
747 | ||
748 | int | |
749 | compopt_builtin (list) | |
750 | WORD_LIST *list; | |
751 | { | |
752 | int opts_on, opts_off, *opts, opt, oind, ret, Eflag; | |
753 | WORD_LIST *l; | |
754 | COMPSPEC *cs; | |
755 | ||
756 | opts_on = opts_off = 0; | |
757 | ret = EXECUTION_SUCCESS; | |
758 | ||
759 | reset_internal_getopt (); | |
760 | while ((opt = internal_getopt (list, "+o:")) != EOF) | |
761 | { | |
762 | opts = (list_opttype == '-') ? &opts_on : &opts_off; | |
763 | ||
764 | switch (opt) | |
765 | { | |
766 | case 'o': | |
767 | oind = find_compopt (list_optarg); | |
768 | if (oind < 0) | |
769 | { | |
770 | sh_invalidoptname (list_optarg); | |
771 | return (EX_USAGE); | |
772 | } | |
773 | *opts |= compopts[oind].optflag; | |
774 | break; | |
775 | default: | |
776 | builtin_usage (); | |
777 | return (EX_USAGE); | |
778 | } | |
779 | } | |
780 | list = loptend; | |
781 | ||
782 | if (list == 0) | |
783 | { | |
784 | if (RL_ISSTATE (RL_STATE_COMPLETING) == 0 || pcomp_curcs == 0) | |
785 | { | |
786 | builtin_error (_("not currently executing completion function")); | |
787 | return (EXECUTION_FAILURE); | |
788 | } | |
789 | cs = pcomp_curcs; | |
790 | ||
791 | if (opts_on == 0 && opts_off == 0) | |
792 | { | |
793 | print_compopts (pcomp_curcmd, cs, 1); | |
794 | return (sh_chkwrite (ret)); | |
795 | } | |
796 | ||
797 | /* Set the compspec options */ | |
798 | pcomp_set_compspec_options (cs, opts_on, 1); | |
799 | pcomp_set_compspec_options (cs, opts_off, 0); | |
800 | ||
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); | |
804 | ||
805 | return (ret); | |
806 | } | |
807 | ||
808 | for (l = list; l; l = l->next) | |
809 | { | |
810 | cs = progcomp_search (l->word->word); | |
811 | if (cs == 0) | |
812 | { | |
813 | builtin_error (_("%s: no completion specification"), l->word->word); | |
814 | ret = EXECUTION_FAILURE; | |
815 | continue; | |
816 | } | |
817 | if (opts_on == 0 && opts_off == 0) | |
818 | { | |
819 | print_compopts (l->word->word, cs, 1); | |
820 | continue; /* XXX -- fill in later */ | |
821 | } | |
822 | ||
823 | /* Set the compspec options */ | |
824 | pcomp_set_compspec_options (cs, opts_on, 1); | |
825 | pcomp_set_compspec_options (cs, opts_off, 0); | |
826 | } | |
827 | ||
828 | return (ret); | |
829 | } |