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