| 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-2003 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 | #include "../bashintl.h"
|
|---|
| 47 |
|
|---|
| 48 | #include "../shell.h"
|
|---|
| 49 | #include "../builtins.h"
|
|---|
| 50 | #include "../pcomplete.h"
|
|---|
| 51 | #include "../bashline.h"
|
|---|
| 52 |
|
|---|
| 53 | #include "common.h"
|
|---|
| 54 | #include "bashgetopt.h"
|
|---|
| 55 |
|
|---|
| 56 | #include <readline/readline.h>
|
|---|
| 57 |
|
|---|
| 58 | #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
|
|---|
| 59 |
|
|---|
| 60 | static int find_compact __P((char *));
|
|---|
| 61 | static int find_compopt __P((char *));
|
|---|
| 62 |
|
|---|
| 63 | static int build_actions __P((WORD_LIST *, int *, int *, unsigned long *, unsigned long *));
|
|---|
| 64 |
|
|---|
| 65 | static int remove_cmd_completions __P((WORD_LIST *));
|
|---|
| 66 |
|
|---|
| 67 | static int print_one_completion __P((char *, COMPSPEC *));
|
|---|
| 68 | static int print_compitem __P((BUCKET_CONTENTS *));
|
|---|
| 69 | static void print_all_completions __P((void));
|
|---|
| 70 | static int print_cmd_completions __P((WORD_LIST *));
|
|---|
| 71 |
|
|---|
| 72 | static char *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg;
|
|---|
| 73 |
|
|---|
| 74 | static struct _compacts {
|
|---|
| 75 | char *actname;
|
|---|
| 76 | int actflag;
|
|---|
| 77 | int actopt;
|
|---|
| 78 | } compacts[] = {
|
|---|
| 79 | { "alias", CA_ALIAS, 'a' },
|
|---|
| 80 | { "arrayvar", CA_ARRAYVAR, 0 },
|
|---|
| 81 | { "binding", CA_BINDING, 0 },
|
|---|
| 82 | { "builtin", CA_BUILTIN, 'b' },
|
|---|
| 83 | { "command", CA_COMMAND, 'c' },
|
|---|
| 84 | { "directory", CA_DIRECTORY, 'd' },
|
|---|
| 85 | { "disabled", CA_DISABLED, 0 },
|
|---|
| 86 | { "enabled", CA_ENABLED, 0 },
|
|---|
| 87 | { "export", CA_EXPORT, 'e' },
|
|---|
| 88 | { "file", CA_FILE, 'f' },
|
|---|
| 89 | { "function", CA_FUNCTION, 0 },
|
|---|
| 90 | { "helptopic", CA_BUILTIN, 0 }, /* for now */
|
|---|
| 91 | { "hostname", CA_HOSTNAME, 0 },
|
|---|
| 92 | { "group", CA_GROUP, 'g' },
|
|---|
| 93 | { "job", CA_JOB, 'j' },
|
|---|
| 94 | { "keyword", CA_KEYWORD, 'k' },
|
|---|
| 95 | { "running", CA_RUNNING, 0 },
|
|---|
| 96 | { "service", CA_SERVICE, 's' },
|
|---|
| 97 | { "setopt", CA_SETOPT, 0 },
|
|---|
| 98 | { "shopt", CA_SHOPT, 0 },
|
|---|
| 99 | { "signal", CA_SIGNAL, 0 },
|
|---|
| 100 | { "stopped", CA_STOPPED, 0 },
|
|---|
| 101 | { "user", CA_USER, 'u' },
|
|---|
| 102 | { "variable", CA_VARIABLE, 'v' },
|
|---|
| 103 | { (char *)NULL, 0, 0 },
|
|---|
| 104 | };
|
|---|
| 105 |
|
|---|
| 106 | /* This should be a STRING_INT_ALIST */
|
|---|
| 107 | static struct _compopt {
|
|---|
| 108 | char *optname;
|
|---|
| 109 | int optflag;
|
|---|
| 110 | } compopts[] = {
|
|---|
| 111 | { "bashdefault", COPT_BASHDEFAULT },
|
|---|
| 112 | { "default", COPT_DEFAULT },
|
|---|
| 113 | { "dirnames", COPT_DIRNAMES },
|
|---|
| 114 | { "filenames",COPT_FILENAMES},
|
|---|
| 115 | { "nospace", COPT_NOSPACE },
|
|---|
| 116 | { "plusdirs", COPT_PLUSDIRS },
|
|---|
| 117 | { (char *)NULL, 0 },
|
|---|
| 118 | };
|
|---|
| 119 |
|
|---|
| 120 | static int
|
|---|
| 121 | find_compact (name)
|
|---|
| 122 | char *name;
|
|---|
| 123 | {
|
|---|
| 124 | register int i;
|
|---|
| 125 |
|
|---|
| 126 | for (i = 0; compacts[i].actname; i++)
|
|---|
| 127 | if (STREQ (name, compacts[i].actname))
|
|---|
| 128 | return i;
|
|---|
| 129 | return -1;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | static int
|
|---|
| 133 | find_compopt (name)
|
|---|
| 134 | char *name;
|
|---|
| 135 | {
|
|---|
| 136 | register int i;
|
|---|
| 137 |
|
|---|
| 138 | for (i = 0; compopts[i].optname; i++)
|
|---|
| 139 | if (STREQ (name, compopts[i].optname))
|
|---|
| 140 | return i;
|
|---|
| 141 | return -1;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /* Build the actions and compspec options from the options specified in LIST.
|
|---|
| 145 | ACTP is a pointer to an unsigned long in which to place the bitmap of
|
|---|
| 146 | actions. OPTP is a pointer to an unsigned long in which to place the
|
|---|
| 147 | btmap of compspec options (arguments to `-o'). PP, if non-null, gets 1
|
|---|
| 148 | if -p is supplied; RP, if non-null, gets 1 if -r is supplied.
|
|---|
| 149 | If either is null, the corresponding option generates an error.
|
|---|
| 150 | This also sets variables corresponding to options that take arguments as
|
|---|
| 151 | a side effect; the caller should ensure that those variables are set to
|
|---|
| 152 | NULL before calling build_actions. Return value:
|
|---|
| 153 | EX_USAGE = bad option
|
|---|
| 154 | EXECUTION_SUCCESS = some options supplied
|
|---|
| 155 | EXECUTION_FAILURE = no options supplied
|
|---|
| 156 | */
|
|---|
| 157 |
|
|---|
| 158 | static int
|
|---|
| 159 | build_actions (list, pp, rp, actp, optp)
|
|---|
| 160 | WORD_LIST *list;
|
|---|
| 161 | int *pp, *rp;
|
|---|
| 162 | unsigned long *actp, *optp;
|
|---|
| 163 | {
|
|---|
| 164 | int opt, ind, opt_given;
|
|---|
| 165 | unsigned long acts, copts;
|
|---|
| 166 |
|
|---|
| 167 | acts = copts = (unsigned long)0L;
|
|---|
| 168 | opt_given = 0;
|
|---|
| 169 |
|
|---|
| 170 | reset_internal_getopt ();
|
|---|
| 171 | while ((opt = internal_getopt (list, "abcdefgjko:prsuvA:G:W:P:S:X:F:C:")) != -1)
|
|---|
| 172 | {
|
|---|
| 173 | opt_given = 1;
|
|---|
| 174 | switch (opt)
|
|---|
| 175 | {
|
|---|
| 176 | case 'r':
|
|---|
| 177 | if (rp)
|
|---|
| 178 | {
|
|---|
| 179 | *rp = 1;
|
|---|
| 180 | break;
|
|---|
| 181 | }
|
|---|
| 182 | else
|
|---|
| 183 | {
|
|---|
| 184 | sh_invalidopt ("-r");
|
|---|
| 185 | builtin_usage ();
|
|---|
| 186 | return (EX_USAGE);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | case 'p':
|
|---|
| 190 | if (pp)
|
|---|
| 191 | {
|
|---|
| 192 | *pp = 1;
|
|---|
| 193 | break;
|
|---|
| 194 | }
|
|---|
| 195 | else
|
|---|
| 196 | {
|
|---|
| 197 | sh_invalidopt ("-p");
|
|---|
| 198 | builtin_usage ();
|
|---|
| 199 | return (EX_USAGE);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | case 'a':
|
|---|
| 203 | acts |= CA_ALIAS;
|
|---|
| 204 | break;
|
|---|
| 205 | case 'b':
|
|---|
| 206 | acts |= CA_BUILTIN;
|
|---|
| 207 | break;
|
|---|
| 208 | case 'c':
|
|---|
| 209 | acts |= CA_COMMAND;
|
|---|
| 210 | break;
|
|---|
| 211 | case 'd':
|
|---|
| 212 | acts |= CA_DIRECTORY;
|
|---|
| 213 | break;
|
|---|
| 214 | case 'e':
|
|---|
| 215 | acts |= CA_EXPORT;
|
|---|
| 216 | break;
|
|---|
| 217 | case 'f':
|
|---|
| 218 | acts |= CA_FILE;
|
|---|
| 219 | break;
|
|---|
| 220 | case 'g':
|
|---|
| 221 | acts |= CA_GROUP;
|
|---|
| 222 | break;
|
|---|
| 223 | case 'j':
|
|---|
| 224 | acts |= CA_JOB;
|
|---|
| 225 | break;
|
|---|
| 226 | case 'k':
|
|---|
| 227 | acts |= CA_KEYWORD;
|
|---|
| 228 | break;
|
|---|
| 229 | case 's':
|
|---|
| 230 | acts |= CA_SERVICE;
|
|---|
| 231 | break;
|
|---|
| 232 | case 'u':
|
|---|
| 233 | acts |= CA_USER;
|
|---|
| 234 | break;
|
|---|
| 235 | case 'v':
|
|---|
| 236 | acts |= CA_VARIABLE;
|
|---|
| 237 | break;
|
|---|
| 238 | case 'o':
|
|---|
|
|---|