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