]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/complete.def
Imported from ../bash-2.05.tar.gz.
[thirdparty/bash.git] / builtins / complete.def
CommitLineData
bb70624e
JA
1This file is complete.def, from which is created complete.c.
2It implements the builtins "complete" and "compgen" in Bash.
3
4Copyright (C) 1999 Free Software Foundation, Inc.
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
8Bash is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
12
13Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License along
19with Bash; see the file COPYING. If not, write to the Free Software
20Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21
22$PRODUCES complete.c
23
24$BUILTIN complete
25$DEPENDS_ON PROGRAMMABLE_COMPLETION
26$FUNCTION complete_builtin
28ef6c31 27$SHORT_DOC complete [-abcdefjkvu] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]
bb70624e
JA
28For each NAME, specify how arguments are to be completed.
29If the -p option is supplied, or if no options are supplied, existing
30completion specifications are printed in a way that allows them to be
31reused as input. The -r option removes a completion specification for
32each NAME, or, if no NAMEs are supplied, all completion specifications.
33$END
34
35#include <config.h>
36
37#include <stdio.h>
38
39#include "../bashtypes.h"
40
41#if defined (HAVE_UNISTD_H)
42# include <unistd.h>
43#endif
44
45#include "../bashansi.h"
46
47#include "../shell.h"
48#include "../builtins.h"
49#include "../pcomplete.h"
50
51#include "common.h"
52#include "bashgetopt.h"
53
54#define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
55
56static int remove_cmd_completions ();
57
58static void print_all_completions ();
59static int print_cmd_completions ();
60
61static char *Aarg, *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg;
62
63static struct _compacts {
64 char *actname;
65 int actflag;
66 int actopt;
67} compacts[] = {
68 { "alias", CA_ALIAS, 'a' },
69 { "arrayvar", CA_ARRAYVAR, 0 },
70 { "binding", CA_BINDING, 0 },
71 { "builtin", CA_BUILTIN, 'b' },
72 { "command", CA_COMMAND, 'c' },
73 { "directory", CA_DIRECTORY, 'd' },
74 { "disabled", CA_DISABLED, 0 },
75 { "enabled", CA_ENABLED, 0 },
76 { "export", CA_EXPORT, 'e' },
77 { "file", CA_FILE, 'f' },
78 { "function", CA_FUNCTION, 0 },
79 { "helptopic", CA_BUILTIN, 0 }, /* for now */
80 { "hostname", CA_HOSTNAME, 0 },
81 { "job", CA_JOB, 'j' },
82 { "keyword", CA_KEYWORD, 'k' },
83 { "running", CA_RUNNING, 0 },
84 { "setopt", CA_SETOPT, 0 },
85 { "shopt", CA_SHOPT, 0 },
86 { "signal", CA_SIGNAL, 0 },
87 { "stopped", CA_STOPPED, 0 },
88 { "user", CA_USER, 'u' },
89 { "variable", CA_VARIABLE, 'v' },
90 { (char *)NULL, 0, 0 },
91};
92
28ef6c31
JA
93static struct _compopt {
94 char *optname;
95 int optflag;
96} compopts[] = {
97 { "default", COPT_DEFAULT },
98 { "dirnames", COPT_DIRNAMES },
99 { "filenames",COPT_FILENAMES},
100 { (char *)NULL, 0 },
101};
102
bb70624e
JA
103static int
104find_compact (name)
105 char *name;
106{
107 register int i;
108
109 for (i = 0; compacts[i].actname; i++)
110 if (STREQ (name, compacts[i].actname))
111 return i;
112 return -1;
113}
114
28ef6c31
JA
115static int
116find_compopt (name)
117 char *name;
118{
119 register int i;
120
121 for (i = 0; compopts[i].optname; i++)
122 if (STREQ (name, compopts[i].optname))
123 return i;
124 return -1;
125}
126
127/* Build the actions and compspec options from the options specified in LIST.
128 ACTP is a pointer to an unsigned long in which to place the bitmap of
129 actions. OPTP is a pointer to an unsigned long in which to place the
130 btmap of compspec options (arguments to `-o'). PP, if non-null, gets 1
131 if -p is supplied; RP, if non-null, gets 1 if -r is supplied.
132 If either is null, the corresponding option generates an error.
133 This also sets variables corresponding to options that take arguments as
134 a side effect; the caller should ensure that those variables are set to
135 NULL before calling build_actions. Return value:
bb70624e
JA
136 EX_USAGE = bad option
137 EXECUTION_SUCCESS = some options supplied
138 EXECUTION_FAILURE = no options supplied
139*/
140
141static int
28ef6c31 142build_actions (list, pp, rp, actp, optp)
bb70624e
JA
143 WORD_LIST *list;
144 int *pp, *rp;
28ef6c31 145 unsigned long *actp, *optp;
bb70624e
JA
146{
147 int opt, ind, pflag, rflag, opt_given;
28ef6c31 148 unsigned long acts, copts;
bb70624e 149
28ef6c31 150 acts = copts = (unsigned long)0L;
bb70624e
JA
151 opt_given = 0;
152
153 reset_internal_getopt ();
28ef6c31 154 while ((opt = internal_getopt (list, "abcdefjko:pruvA:G:W:P:S:X:F:C:")) != -1)
bb70624e
JA
155 {
156 opt_given = 1;
157 switch (opt)
158 {
159 case 'r':
160 if (rp)
161 {
162 *rp = 1;
163 break;
164 }
165 else
166 {
167 builtin_error ("illegal option: -r");
168 builtin_usage ();
169 return (EX_USAGE);
170 }
171
172 case 'p':
173 if (pp)
174 {
175 *pp = 1;
176 break;
177 }
178 else
179 {
180 builtin_error ("illegal option: -p");
181 builtin_usage ();
182 return (EX_USAGE);
183 }
184
185 case 'a':
186 acts |= CA_ALIAS;
187 break;
188 case 'b':
189 acts |= CA_BUILTIN;
190 break;
191 case 'c':
192 acts |= CA_COMMAND;
193 break;
194 case 'd':
195 acts |= CA_DIRECTORY;
196 break;
197 case 'e':
198 acts |= CA_EXPORT;
199 break;
200 case 'f':
201 acts |= CA_FILE;
202 break;
203 case 'j':
204 acts |= CA_JOB;
205 break;
206 case 'k':
207 acts |= CA_KEYWORD;
208 break;
209 case 'u':
210 acts |= CA_USER;
211 break;
212 case 'v':
213 acts |= CA_VARIABLE;
214 break;
28ef6c31
JA
215 case 'o':
216 ind = find_compopt (list_optarg);
217 if (ind < 0)
218 {
219 builtin_error ("%s: invalid option name", list_optarg);
220 return (EX_USAGE);
221 }
222 copts |= compopts[ind].optflag;
223 break;
bb70624e
JA
224 case 'A':
225 ind = find_compact (list_optarg);
226 if (ind < 0)
227 {
228 builtin_error ("%s: invalid action name", list_optarg);
229 return (EX_USAGE);
230 }
231 acts |= compacts[ind].actflag;
232 break;
233 case 'C':
234 Carg = list_optarg;
235 break;
236 case 'F':
237 Farg = list_optarg;
238 break;
239 case 'G':
240 Garg = list_optarg;
241 break;
242 case 'P':
243 Parg = list_optarg;
244 break;
245 case 'S':
246 Sarg = list_optarg;
247 break;
248 case 'W':
249 Warg = list_optarg;
250 break;
251 case 'X':
252 Xarg = list_optarg;
253 break;
254 default:
255 builtin_usage ();
256 return (EX_USAGE);
257 }
258 }
259
260 *actp = acts;
28ef6c31
JA
261 *optp = copts;
262
bb70624e
JA
263 return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
264}
265
266/* Add, remove, and display completion specifiers. */
267int
268complete_builtin (list)
269 WORD_LIST *list;
270{
271 int opt_given, pflag, rflag, rval;
28ef6c31 272 unsigned long acts, copts;
bb70624e
JA
273 char *cmd;
274 COMPSPEC *cs;
275
276 if (list == 0)
277 {
278 print_all_completions ();
279 return (EXECUTION_SUCCESS);
280 }
281
282 opt_given = pflag = rflag = 0;
28ef6c31 283 acts = copts = (unsigned long)0L;
bb70624e
JA
284 Aarg = Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
285 cs = (COMPSPEC *)NULL;
286
287 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
288 as a side effect if they are supplied as options. */
28ef6c31 289 rval = build_actions (list, &pflag, &rflag, &acts, &copts);
bb70624e
JA
290 if (rval == EX_USAGE)
291 return (rval);
292 opt_given = rval != EXECUTION_FAILURE;
293
294 list = loptend;
295
296 /* -p overrides everything else */
297 if (pflag || (list == 0 && opt_given == 0))
298 {
299 if (list == 0)
300 {
301 print_all_completions ();
302 return (EXECUTION_SUCCESS);
303 }
304 return (print_cmd_completions (list));
305 }
306
307 /* next, -r overrides everything else. */
308 if (rflag)
309 {
310 if (list == 0)
311 {
312 clear_progcomps ();
313 return (EXECUTION_SUCCESS);
314 }
315 return (remove_cmd_completions (list));
316 }
317
318 if (list == 0 && opt_given)
319 {
320 builtin_usage ();
321 return (EX_USAGE);
322 }
323
324 /* If we get here, we need to build a compspec and add it for each
325 remaining argument. */
326 cs = alloc_compspec ();
327 cs->actions = acts;
28ef6c31 328 cs->options = copts;
bb70624e
JA
329
330 cs->globpat = STRDUP (Garg);
331 cs->words = STRDUP (Warg);
332 cs->prefix = STRDUP (Parg);
333 cs->suffix = STRDUP (Sarg);
334 cs->funcname = STRDUP (Farg);
335 cs->command = STRDUP (Carg);
336 cs->filterpat = STRDUP (Xarg);
337
338 for (rval = EXECUTION_SUCCESS ; list; list = list->next)
339 {
340 /* Add CS as the compspec for the specified commands. */
341 cmd = list->word->word;
342 if (add_progcomp (cmd, cs) == 0)
28ef6c31 343 rval = EXECUTION_FAILURE;
bb70624e
JA
344 }
345
346 return (rval);
347}
348
349static int
350remove_cmd_completions (list)
351 WORD_LIST *list;
352{
353 WORD_LIST *l;
354 int ret;
355
356 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
357 {
358 if (remove_progcomp (l->word->word) == 0)
359 {
360 builtin_error ("%s: no completion specification", l->word->word);
361 ret = EXECUTION_FAILURE;
362 }
363 }
364 return ret;
365}
366
367#define SQPRINTARG(a, f) \
368 do { \
369 if (a) \
370 { \
28ef6c31 371 x = sh_single_quote (a); \
bb70624e
JA
372 printf ("%s %s ", f, x); \
373 free (x); \
374 } \
375 } while (0)
376
377#define PRINTARG(a, f) \
378 do { \
379 if (a) \
380 printf ("%s %s ", f, a); \
381 } while (0)
382
383#define PRINTOPT(a, f) \
384 do { \
385 if (acts & a) \
386 printf ("%s ", f); \
387 } while (0)
388
389#define PRINTACT(a, f) \
390 do { \
391 if (acts & a) \
392 printf ("-A %s ", f); \
393 } while (0)
394
28ef6c31
JA
395#define PRINTCOMPOPT(a, f) \
396 do { \
397 if (copts & a) \
398 printf ("-o %s ", f); \
399 } while (0)
400
bb70624e
JA
401static void
402print_one_completion (cmd, cs)
403 char *cmd;
404 COMPSPEC *cs;
405{
28ef6c31 406 unsigned long acts, copts;
bb70624e
JA
407 char *x;
408
409 printf ("complete ");
410
28ef6c31
JA
411 copts = cs->options;
412
413 /* First, print the -o options. */
414 PRINTCOMPOPT (COPT_DEFAULT, "default");
415 PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
416 PRINTCOMPOPT (COPT_FILENAMES, "filenames");
417
bb70624e
JA
418 acts = cs->actions;
419
28ef6c31 420 /* simple flags next */
bb70624e
JA
421 PRINTOPT (CA_ALIAS, "-a");
422 PRINTOPT (CA_BUILTIN, "-b");
423 PRINTOPT (CA_COMMAND, "-c");
424 PRINTOPT (CA_DIRECTORY, "-d");
425 PRINTOPT (CA_EXPORT, "-e");
426 PRINTOPT (CA_FILE, "-f");
427 PRINTOPT (CA_KEYWORD, "-k");
428 PRINTOPT (CA_JOB, "-j");
429 PRINTOPT (CA_USER, "-u");
430 PRINTOPT (CA_VARIABLE, "-v");
431
432 /* now the rest of the actions */
433 PRINTACT (CA_ARRAYVAR, "arrayvar");
434 PRINTACT (CA_BINDING, "binding");
435 PRINTACT (CA_DISABLED, "disabled");
436 PRINTACT (CA_ENABLED, "enabled");
437 PRINTACT (CA_FUNCTION, "function");
438 PRINTACT (CA_HELPTOPIC, "helptopic");
439 PRINTACT (CA_HOSTNAME, "hostname");
440 PRINTACT (CA_RUNNING, "running");
441 PRINTACT (CA_SETOPT, "setopt");
442 PRINTACT (CA_SHOPT, "shopt");
443 PRINTACT (CA_SIGNAL, "signal");
444 PRINTACT (CA_STOPPED, "stopped");
445
446 /* now the rest of the arguments */
447
448 /* arguments that require quoting */
449 SQPRINTARG (cs->globpat, "-G");
450 SQPRINTARG (cs->words, "-W");
451 SQPRINTARG (cs->prefix, "-P");
452 SQPRINTARG (cs->suffix, "-S");
453 SQPRINTARG (cs->filterpat, "-X");
454
455 /* simple arguments that don't require quoting */
456 PRINTARG (cs->funcname, "-F");
457 PRINTARG (cs->command, "-C");
458
459 printf ("%s\n", cmd);
460}
461
462static void
463print_all_completions ()
464{
465 print_all_compspecs (print_one_completion);
466}
467
468static int
469print_cmd_completions (list)
470 WORD_LIST *list;
471{
472 WORD_LIST *l;
473 COMPSPEC *cs;
474 int ret;
475
476 for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next)
477 {
478 cs = find_compspec (l->word->word);
479 if (cs)
28ef6c31 480 print_one_completion (l->word->word, cs);
bb70624e
JA
481 else
482 {
483 builtin_error ("%s: no completion specification", l->word->word);
484 ret = EXECUTION_FAILURE;
485 }
486 }
487 return (ret);
488}
489
490$BUILTIN compgen
491$DEPENDS_ON PROGRAMMABLE_COMPLETION
492$FUNCTION compgen_builtin
28ef6c31 493$SHORT_DOC compgen [-abcdefjkvu] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [word]
bb70624e
JA
494Display the possible completions depending on the options. Intended
495to be used from within a shell function generating possible completions.
496If the optional WORD argument is supplied, matches against WORD are
497generated.
498$END
499
500int
501compgen_builtin (list)
502 WORD_LIST *list;
503{
504 int rval;
28ef6c31 505 unsigned long acts, copts;
bb70624e
JA
506 COMPSPEC *cs;
507 STRINGLIST *sl;
508 char *word;
509
510 if (list == 0)
511 return (EXECUTION_SUCCESS);
512
28ef6c31 513 acts = copts = (unsigned long)0L;
bb70624e
JA
514 Aarg = Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL;
515 cs = (COMPSPEC *)NULL;
516
517 /* Build the actions from the arguments. Also sets the [A-Z]arg variables
518 as a side effect if they are supplied as options. */
28ef6c31 519 rval = build_actions (list, (int *)NULL, (int *)NULL, &acts, &copts);
bb70624e
JA
520 if (rval == EX_USAGE)
521 return (rval);
522 if (rval == EXECUTION_FAILURE)
523 return (EXECUTION_SUCCESS);
524
525 list = loptend;
526
527 word = (list && list->word) ? list->word->word : "";
528
529 if (Farg)
530 internal_warning ("compgen: -F option may not work as you expect");
531 if (Carg)
532 internal_warning ("compgen: -C option may not work as you expect");
533
534 /* If we get here, we need to build a compspec and evaluate it. */
535 cs = alloc_compspec ();
536 cs->actions = acts;
28ef6c31 537 cs->options = copts;
bb70624e
JA
538 cs->refcount = 1;
539
540 cs->globpat = STRDUP (Garg);
541 cs->words = STRDUP (Warg);
542 cs->prefix = STRDUP (Parg);
543 cs->suffix = STRDUP (Sarg);
544 cs->funcname = STRDUP (Farg);
545 cs->command = STRDUP (Carg);
546 cs->filterpat = STRDUP (Xarg);
547
548 rval = EXECUTION_FAILURE;
549 sl = gen_compspec_completions (cs, "compgen", word, 0, 0);
550 if (sl)
551 {
552 if (sl->list)
553 {
554 rval = EXECUTION_SUCCESS;
555 print_stringlist (sl, (char *)NULL);
556 }
557 free_stringlist (sl);
558 }
559
560 free_compspec (cs);
561 return (rval);
562}