Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1 | This file is complete.def, from which is created complete.c. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 2 | It implements the builtins "complete", "compgen", and "compopt" in Bash. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 3 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4 | Copyright (C) 1999-2011 Free Software Foundation, Inc. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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/>. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES complete.c |
| 22 | |
| 23 | $BUILTIN complete |
| 24 | $DEPENDS_ON PROGRAMMABLE_COMPLETION |
| 25 | $FUNCTION complete_builtin |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 26 | $SHORT_DOC complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 37 | -D apply the completions and actions as the default for commands |
| 38 | without any specific completion defined |
| 39 | -E apply the completions and actions to "empty" commands -- |
| 40 | completion attempted on a blank line |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 41 | |
| 42 | When completion is attempted, the actions are applied in the order the |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 43 | uppercase-letter options are listed above. The -D option takes |
| 44 | precedence over -E. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 45 | |
| 46 | Exit Status: |
| 47 | Returns success unless an invalid option is supplied or an error occurs. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 48 | $END |
| 49 | |
| 50 | #include <config.h> |
| 51 | |
| 52 | #include <stdio.h> |
| 53 | |
| 54 | #include "../bashtypes.h" |
| 55 | |
| 56 | #if defined (HAVE_UNISTD_H) |
| 57 | # include <unistd.h> |
| 58 | #endif |
| 59 | |
| 60 | #include "../bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 61 | #include "../bashintl.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 62 | |
| 63 | #include "../shell.h" |
| 64 | #include "../builtins.h" |
| 65 | #include "../pcomplete.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 66 | #include "../bashline.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 67 | |
| 68 | #include "common.h" |
| 69 | #include "bashgetopt.h" |
| 70 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 71 | #include <readline/readline.h> |
| 72 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 73 | #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL) |
| 74 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 75 | /* Structure containing all the non-action (binary) options; filled in by |
| 76 | build_actions(). */ |
| 77 | struct _optflags { |
| 78 | int pflag; |
| 79 | int rflag; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 80 | int Dflag; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 81 | int Eflag; |
| 82 | }; |
| 83 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 84 | static int find_compact __P((char *)); |
| 85 | static int find_compopt __P((char *)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 86 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 87 | static int build_actions __P((WORD_LIST *, struct _optflags *, unsigned long *, unsigned long *)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 88 | |
| 89 | static int remove_cmd_completions __P((WORD_LIST *)); |
| 90 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 91 | static int print_one_completion __P((char *, COMPSPEC *)); |
| 92 | static int print_compitem __P((BUCKET_CONTENTS *)); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 93 | static void print_compopts __P((const char *, COMPSPEC *, int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 94 | static void print_all_completions __P((void)); |
| 95 | static int print_cmd_completions __P((WORD_LIST *)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 96 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 97 | static char *Garg, *Warg, *Parg, *Sarg, *Xarg, *Farg, *Carg; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 98 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 99 | static const struct _compacts { |
| 100 | const char * const actname; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 101 | int actflag; |
| 102 | int actopt; |
| 103 | } compacts[] = { |
| 104 | { "alias", CA_ALIAS, 'a' }, |
| 105 | { "arrayvar", CA_ARRAYVAR, 0 }, |
| 106 | { "binding", CA_BINDING, 0 }, |
| 107 | { "builtin", CA_BUILTIN, 'b' }, |
| 108 | { "command", CA_COMMAND, 'c' }, |
| 109 | { "directory", CA_DIRECTORY, 'd' }, |
| 110 | { "disabled", CA_DISABLED, 0 }, |
| 111 | { "enabled", CA_ENABLED, 0 }, |
| 112 | { "export", CA_EXPORT, 'e' }, |
| 113 | { "file", CA_FILE, 'f' }, |
| 114 | { "function", CA_FUNCTION, 0 }, |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 115 | { "helptopic", CA_HELPTOPIC, 0 }, |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 116 | { "hostname", CA_HOSTNAME, 0 }, |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 117 | { "group", CA_GROUP, 'g' }, |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 118 | { "job", CA_JOB, 'j' }, |
| 119 | { "keyword", CA_KEYWORD, 'k' }, |
| 120 | { "running", CA_RUNNING, 0 }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 121 | { "service", CA_SERVICE, 's' }, |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 122 | { "setopt", CA_SETOPT, 0 }, |
| 123 | { "shopt", CA_SHOPT, 0 }, |
| 124 | { "signal", CA_SIGNAL, 0 }, |
| 125 | { "stopped", CA_STOPPED, 0 }, |
| 126 | { "user", CA_USER, 'u' }, |
| 127 | { "variable", CA_VARIABLE, 'v' }, |
| 128 | { (char *)NULL, 0, 0 }, |
| 129 | }; |
| 130 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 131 | /* This should be a STRING_INT_ALIST */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 132 | static const struct _compopt { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 133 | const char * const optname; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 134 | int optflag; |
| 135 | } compopts[] = { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 136 | { "bashdefault", COPT_BASHDEFAULT }, |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 137 | { "default", COPT_DEFAULT }, |
| 138 | { "dirnames", COPT_DIRNAMES }, |
| 139 | { "filenames",COPT_FILENAMES}, |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 140 | { "noquote", COPT_NOQUOTE }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 141 | { "nospace", COPT_NOSPACE }, |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 142 | { "plusdirs", COPT_PLUSDIRS }, |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 143 | { (char *)NULL, 0 }, |
| 144 | }; |
| 145 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 146 | static int |
| 147 | find_compact (name) |
| 148 | char *name; |
| 149 | { |
| 150 | register int i; |
| 151 | |
| 152 | for (i = 0; compacts[i].actname; i++) |
| 153 | if (STREQ (name, compacts[i].actname)) |
| 154 | return i; |
| 155 | return -1; |
| 156 | } |
| 157 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 158 | static int |
| 159 | find_compopt (name) |
| 160 | char *name; |
| 161 | { |
| 162 | register int i; |
| 163 | |
| 164 | for (i = 0; compopts[i].optname; i++) |
| 165 | if (STREQ (name, compopts[i].optname)) |
| 166 | return i; |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | /* Build the actions and compspec options from the options specified in LIST. |
| 171 | ACTP is a pointer to an unsigned long in which to place the bitmap of |
| 172 | actions. OPTP is a pointer to an unsigned long in which to place the |
| 173 | btmap of compspec options (arguments to `-o'). PP, if non-null, gets 1 |
| 174 | if -p is supplied; RP, if non-null, gets 1 if -r is supplied. |
| 175 | If either is null, the corresponding option generates an error. |
| 176 | This also sets variables corresponding to options that take arguments as |
| 177 | a side effect; the caller should ensure that those variables are set to |
| 178 | NULL before calling build_actions. Return value: |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 179 | EX_USAGE = bad option |
| 180 | EXECUTION_SUCCESS = some options supplied |
| 181 | EXECUTION_FAILURE = no options supplied |
| 182 | */ |
| 183 | |
| 184 | static int |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 185 | build_actions (list, flagp, actp, optp) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 186 | WORD_LIST *list; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 187 | struct _optflags *flagp; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 188 | unsigned long *actp, *optp; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 189 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 190 | int opt, ind, opt_given; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 191 | unsigned long acts, copts; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 192 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 193 | acts = copts = (unsigned long)0L; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 194 | opt_given = 0; |
| 195 | |
| 196 | reset_internal_getopt (); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 197 | while ((opt = internal_getopt (list, "abcdefgjko:prsuvA:G:W:P:S:X:F:C:DE")) != -1) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 198 | { |
| 199 | opt_given = 1; |
| 200 | switch (opt) |
| 201 | { |
| 202 | case 'r': |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 203 | if (flagp) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 204 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 205 | flagp->rflag = 1; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 206 | break; |
| 207 | } |
| 208 | else |
| 209 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 210 | sh_invalidopt ("-r"); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 211 | builtin_usage (); |
| 212 | return (EX_USAGE); |
| 213 | } |
| 214 | |
| 215 | case 'p': |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 216 | if (flagp) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 217 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 218 | flagp->pflag = 1; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | else |
| 222 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 223 | sh_invalidopt ("-p"); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 224 | builtin_usage (); |
| 225 | return (EX_USAGE); |
| 226 | } |
| 227 | |
| 228 | case 'a': |
| 229 | acts |= CA_ALIAS; |
| 230 | break; |
| 231 | case 'b': |
| 232 | acts |= CA_BUILTIN; |
| 233 | break; |
| 234 | case 'c': |
| 235 | acts |= CA_COMMAND; |
| 236 | break; |
| 237 | case 'd': |
| 238 | acts |= CA_DIRECTORY; |
| 239 | break; |
| 240 | case 'e': |
| 241 | acts |= CA_EXPORT; |
| 242 | break; |
| 243 | case 'f': |
| 244 | acts |= CA_FILE; |
| 245 | break; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 246 | case 'g': |
| 247 | acts |= CA_GROUP; |
| 248 | break; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 249 | case 'j': |
| 250 | acts |= CA_JOB; |
| 251 | break; |
| 252 | case 'k': |
| 253 | acts |= CA_KEYWORD; |
| 254 | break; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 255 | case 's': |
| 256 | acts |= CA_SERVICE; |
| 257 | break; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 258 | case 'u': |
| 259 | acts |= CA_USER; |
| 260 | break; |
| 261 | case 'v': |
| 262 | acts |= CA_VARIABLE; |
| 263 | break; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 264 | case 'o': |
| 265 | ind = find_compopt (list_optarg); |
| 266 | if (ind < 0) |
| 267 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 268 | sh_invalidoptname (list_optarg); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 269 | return (EX_USAGE); |
| 270 | } |
| 271 | copts |= compopts[ind].optflag; |
| 272 | break; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 273 | case 'A': |
| 274 | ind = find_compact (list_optarg); |
| 275 | if (ind < 0) |
| 276 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 277 | builtin_error (_("%s: invalid action name"), list_optarg); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 278 | return (EX_USAGE); |
| 279 | } |
| 280 | acts |= compacts[ind].actflag; |
| 281 | break; |
| 282 | case 'C': |
| 283 | Carg = list_optarg; |
| 284 | break; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 285 | case 'D': |
| 286 | if (flagp) |
| 287 | { |
| 288 | flagp->Dflag = 1; |
| 289 | break; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | sh_invalidopt ("-D"); |
| 294 | builtin_usage (); |
| 295 | return (EX_USAGE); |
| 296 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 297 | case 'E': |
| 298 | if (flagp) |
| 299 | { |
| 300 | flagp->Eflag = 1; |
| 301 | break; |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | sh_invalidopt ("-E"); |
| 306 | builtin_usage (); |
| 307 | return (EX_USAGE); |
| 308 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 309 | case 'F': |
| 310 | Farg = list_optarg; |
| 311 | break; |
| 312 | case 'G': |
| 313 | Garg = list_optarg; |
| 314 | break; |
| 315 | case 'P': |
| 316 | Parg = list_optarg; |
| 317 | break; |
| 318 | case 'S': |
| 319 | Sarg = list_optarg; |
| 320 | break; |
| 321 | case 'W': |
| 322 | Warg = list_optarg; |
| 323 | break; |
| 324 | case 'X': |
| 325 | Xarg = list_optarg; |
| 326 | break; |
| 327 | default: |
| 328 | builtin_usage (); |
| 329 | return (EX_USAGE); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | *actp = acts; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 334 | *optp = copts; |
| 335 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 336 | return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
| 337 | } |
| 338 | |
| 339 | /* Add, remove, and display completion specifiers. */ |
| 340 | int |
| 341 | complete_builtin (list) |
| 342 | WORD_LIST *list; |
| 343 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 344 | int opt_given, rval; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 345 | unsigned long acts, copts; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 346 | COMPSPEC *cs; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 347 | struct _optflags oflags; |
| 348 | WORD_LIST *l, *wl; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 349 | |
| 350 | if (list == 0) |
| 351 | { |
| 352 | print_all_completions (); |
| 353 | return (EXECUTION_SUCCESS); |
| 354 | } |
| 355 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 356 | opt_given = oflags.pflag = oflags.rflag = oflags.Dflag = oflags.Eflag = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 357 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 358 | acts = copts = (unsigned long)0L; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 359 | Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 360 | cs = (COMPSPEC *)NULL; |
| 361 | |
| 362 | /* Build the actions from the arguments. Also sets the [A-Z]arg variables |
| 363 | as a side effect if they are supplied as options. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 364 | rval = build_actions (list, &oflags, &acts, &copts); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 365 | if (rval == EX_USAGE) |
| 366 | return (rval); |
| 367 | opt_given = rval != EXECUTION_FAILURE; |
| 368 | |
| 369 | list = loptend; |
| 370 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 371 | wl = oflags.Dflag ? make_word_list (make_bare_word (DEFAULTCMD), (WORD_LIST *)NULL) |
| 372 | : (oflags.Eflag ? make_word_list (make_bare_word (EMPTYCMD), (WORD_LIST *)NULL) : 0); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 373 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 374 | /* -p overrides everything else */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 375 | if (oflags.pflag || (list == 0 && opt_given == 0)) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 376 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 377 | if (wl) |
| 378 | { |
| 379 | rval = print_cmd_completions (wl); |
| 380 | dispose_words (wl); |
| 381 | return rval; |
| 382 | } |
| 383 | else if (list == 0) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 384 | { |
| 385 | print_all_completions (); |
| 386 | return (EXECUTION_SUCCESS); |
| 387 | } |
| 388 | return (print_cmd_completions (list)); |
| 389 | } |
| 390 | |
| 391 | /* next, -r overrides everything else. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 392 | if (oflags.rflag) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 393 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 394 | if (wl) |
| 395 | { |
| 396 | rval = remove_cmd_completions (wl); |
| 397 | dispose_words (wl); |
| 398 | return rval; |
| 399 | } |
| 400 | else if (list == 0) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 401 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 402 | progcomp_flush (); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 403 | return (EXECUTION_SUCCESS); |
| 404 | } |
| 405 | return (remove_cmd_completions (list)); |
| 406 | } |
| 407 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 408 | if (wl == 0 && list == 0 && opt_given) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 409 | { |
| 410 | builtin_usage (); |
| 411 | return (EX_USAGE); |
| 412 | } |
| 413 | |
| 414 | /* If we get here, we need to build a compspec and add it for each |
| 415 | remaining argument. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 416 | cs = compspec_create (); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 417 | cs->actions = acts; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 418 | cs->options = copts; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 419 | |
| 420 | cs->globpat = STRDUP (Garg); |
| 421 | cs->words = STRDUP (Warg); |
| 422 | cs->prefix = STRDUP (Parg); |
| 423 | cs->suffix = STRDUP (Sarg); |
| 424 | cs->funcname = STRDUP (Farg); |
| 425 | cs->command = STRDUP (Carg); |
| 426 | cs->filterpat = STRDUP (Xarg); |
| 427 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 428 | for (rval = EXECUTION_SUCCESS, l = wl ? wl : list ; l; l = l->next) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 429 | { |
| 430 | /* Add CS as the compspec for the specified commands. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 431 | if (progcomp_insert (l->word->word, cs) == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 432 | rval = EXECUTION_FAILURE; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 435 | dispose_words (wl); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 436 | return (rval); |
| 437 | } |
| 438 | |
| 439 | static int |
| 440 | remove_cmd_completions (list) |
| 441 | WORD_LIST *list; |
| 442 | { |
| 443 | WORD_LIST *l; |
| 444 | int ret; |
| 445 | |
| 446 | for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next) |
| 447 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 448 | if (progcomp_remove (l->word->word) == 0) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 449 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 450 | builtin_error (_("%s: no completion specification"), l->word->word); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 451 | ret = EXECUTION_FAILURE; |
| 452 | } |
| 453 | } |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | #define SQPRINTARG(a, f) \ |
| 458 | do { \ |
| 459 | if (a) \ |
| 460 | { \ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 461 | x = sh_single_quote (a); \ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 462 | printf ("%s %s ", f, x); \ |
| 463 | free (x); \ |
| 464 | } \ |
| 465 | } while (0) |
| 466 | |
| 467 | #define PRINTARG(a, f) \ |
| 468 | do { \ |
| 469 | if (a) \ |
| 470 | printf ("%s %s ", f, a); \ |
| 471 | } while (0) |
| 472 | |
| 473 | #define PRINTOPT(a, f) \ |
| 474 | do { \ |
| 475 | if (acts & a) \ |
| 476 | printf ("%s ", f); \ |
| 477 | } while (0) |
| 478 | |
| 479 | #define PRINTACT(a, f) \ |
| 480 | do { \ |
| 481 | if (acts & a) \ |
| 482 | printf ("-A %s ", f); \ |
| 483 | } while (0) |
| 484 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 485 | #define PRINTCOMPOPT(a, f) \ |
| 486 | do { \ |
| 487 | if (copts & a) \ |
| 488 | printf ("-o %s ", f); \ |
| 489 | } while (0) |
| 490 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 491 | #define XPRINTCOMPOPT(a, f) \ |
| 492 | do { \ |
| 493 | if (copts & a) \ |
| 494 | printf ("-o %s ", f); \ |
| 495 | else \ |
| 496 | printf ("+o %s ", f); \ |
| 497 | } while (0) |
| 498 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 499 | static int |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 500 | print_one_completion (cmd, cs) |
| 501 | char *cmd; |
| 502 | COMPSPEC *cs; |
| 503 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 504 | unsigned long acts, copts; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 505 | char *x; |
| 506 | |
| 507 | printf ("complete "); |
| 508 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 509 | copts = cs->options; |
| 510 | |
| 511 | /* First, print the -o options. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 512 | PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault"); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 513 | PRINTCOMPOPT (COPT_DEFAULT, "default"); |
| 514 | PRINTCOMPOPT (COPT_DIRNAMES, "dirnames"); |
| 515 | PRINTCOMPOPT (COPT_FILENAMES, "filenames"); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 516 | PRINTCOMPOPT (COPT_NOSPACE, "nospace"); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 517 | PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs"); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 518 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 519 | acts = cs->actions; |
| 520 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 521 | /* simple flags next */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 522 | PRINTOPT (CA_ALIAS, "-a"); |
| 523 | PRINTOPT (CA_BUILTIN, "-b"); |
| 524 | PRINTOPT (CA_COMMAND, "-c"); |
| 525 | PRINTOPT (CA_DIRECTORY, "-d"); |
| 526 | PRINTOPT (CA_EXPORT, "-e"); |
| 527 | PRINTOPT (CA_FILE, "-f"); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 528 | PRINTOPT (CA_GROUP, "-g"); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 529 | PRINTOPT (CA_JOB, "-j"); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 530 | PRINTOPT (CA_KEYWORD, "-k"); |
| 531 | PRINTOPT (CA_SERVICE, "-s"); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 532 | PRINTOPT (CA_USER, "-u"); |
| 533 | PRINTOPT (CA_VARIABLE, "-v"); |
| 534 | |
| 535 | /* now the rest of the actions */ |
| 536 | PRINTACT (CA_ARRAYVAR, "arrayvar"); |
| 537 | PRINTACT (CA_BINDING, "binding"); |
| 538 | PRINTACT (CA_DISABLED, "disabled"); |
| 539 | PRINTACT (CA_ENABLED, "enabled"); |
| 540 | PRINTACT (CA_FUNCTION, "function"); |
| 541 | PRINTACT (CA_HELPTOPIC, "helptopic"); |
| 542 | PRINTACT (CA_HOSTNAME, "hostname"); |
| 543 | PRINTACT (CA_RUNNING, "running"); |
| 544 | PRINTACT (CA_SETOPT, "setopt"); |
| 545 | PRINTACT (CA_SHOPT, "shopt"); |
| 546 | PRINTACT (CA_SIGNAL, "signal"); |
| 547 | PRINTACT (CA_STOPPED, "stopped"); |
| 548 | |
| 549 | /* now the rest of the arguments */ |
| 550 | |
| 551 | /* arguments that require quoting */ |
| 552 | SQPRINTARG (cs->globpat, "-G"); |
| 553 | SQPRINTARG (cs->words, "-W"); |
| 554 | SQPRINTARG (cs->prefix, "-P"); |
| 555 | SQPRINTARG (cs->suffix, "-S"); |
| 556 | SQPRINTARG (cs->filterpat, "-X"); |
| 557 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 558 | SQPRINTARG (cs->command, "-C"); |
| 559 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 560 | /* simple arguments that don't require quoting */ |
| 561 | PRINTARG (cs->funcname, "-F"); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 562 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 563 | if (STREQ (cmd, EMPTYCMD)) |
| 564 | printf ("-E\n"); |
| 565 | else if (STREQ (cmd, DEFAULTCMD)) |
| 566 | printf ("-D\n"); |
| 567 | else |
| 568 | printf ("%s\n", cmd); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 569 | |
| 570 | return (0); |
| 571 | } |
| 572 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 573 | static void |
| 574 | print_compopts (cmd, cs, full) |
| 575 | const char *cmd; |
| 576 | COMPSPEC *cs; |
| 577 | int full; |
| 578 | { |
| 579 | int copts; |
| 580 | |
| 581 | printf ("compopt "); |
| 582 | copts = cs->options; |
| 583 | |
| 584 | if (full) |
| 585 | { |
| 586 | XPRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault"); |
| 587 | XPRINTCOMPOPT (COPT_DEFAULT, "default"); |
| 588 | XPRINTCOMPOPT (COPT_DIRNAMES, "dirnames"); |
| 589 | XPRINTCOMPOPT (COPT_FILENAMES, "filenames"); |
| 590 | XPRINTCOMPOPT (COPT_NOSPACE, "nospace"); |
| 591 | XPRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs"); |
| 592 | } |
| 593 | else |
| 594 | { |
| 595 | PRINTCOMPOPT (COPT_BASHDEFAULT, "bashdefault"); |
| 596 | PRINTCOMPOPT (COPT_DEFAULT, "default"); |
| 597 | PRINTCOMPOPT (COPT_DIRNAMES, "dirnames"); |
| 598 | PRINTCOMPOPT (COPT_FILENAMES, "filenames"); |
| 599 | PRINTCOMPOPT (COPT_NOSPACE, "nospace"); |
| 600 | PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs"); |
| 601 | } |
| 602 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 603 | if (STREQ (cmd, EMPTYCMD)) |
| 604 | printf ("-E\n"); |
| 605 | else if (STREQ (cmd, DEFAULTCMD)) |
| 606 | printf ("-D\n"); |
| 607 | else |
| 608 | printf ("%s\n", cmd); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 611 | static int |
| 612 | print_compitem (item) |
| 613 | BUCKET_CONTENTS *item; |
| 614 | { |
| 615 | COMPSPEC *cs; |
| 616 | char *cmd; |
| 617 | |
| 618 | cmd = item->key; |
| 619 | cs = (COMPSPEC *)item->data; |
| 620 | |
| 621 | return (print_one_completion (cmd, cs)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static void |
| 625 | print_all_completions () |
| 626 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 627 | progcomp_walk (print_compitem); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static int |
| 631 | print_cmd_completions (list) |
| 632 | WORD_LIST *list; |
| 633 | { |
| 634 | WORD_LIST *l; |
| 635 | COMPSPEC *cs; |
| 636 | int ret; |
| 637 | |
| 638 | for (ret = EXECUTION_SUCCESS, l = list; l; l = l->next) |
| 639 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 640 | cs = progcomp_search (l->word->word); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 641 | if (cs) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 642 | print_one_completion (l->word->word, cs); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 643 | else |
| 644 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 645 | builtin_error (_("%s: no completion specification"), l->word->word); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 646 | ret = EXECUTION_FAILURE; |
| 647 | } |
| 648 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 649 | |
| 650 | return (sh_chkwrite (ret)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | $BUILTIN compgen |
| 654 | $DEPENDS_ON PROGRAMMABLE_COMPLETION |
| 655 | $FUNCTION compgen_builtin |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 656 | $SHORT_DOC compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word] |
| 657 | Display possible completions depending on the options. |
| 658 | |
| 659 | Intended to be used from within a shell function generating possible |
| 660 | completions. If the optional WORD argument is supplied, matches against |
| 661 | WORD are generated. |
| 662 | |
| 663 | Exit Status: |
| 664 | Returns success unless an invalid option is supplied or an error occurs. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 665 | $END |
| 666 | |
| 667 | int |
| 668 | compgen_builtin (list) |
| 669 | WORD_LIST *list; |
| 670 | { |
| 671 | int rval; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 672 | unsigned long acts, copts; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 673 | COMPSPEC *cs; |
| 674 | STRINGLIST *sl; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 675 | char *word, **matches; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 676 | |
| 677 | if (list == 0) |
| 678 | return (EXECUTION_SUCCESS); |
| 679 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 680 | acts = copts = (unsigned long)0L; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 681 | Garg = Warg = Parg = Sarg = Xarg = Farg = Carg = (char *)NULL; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 682 | cs = (COMPSPEC *)NULL; |
| 683 | |
| 684 | /* Build the actions from the arguments. Also sets the [A-Z]arg variables |
| 685 | as a side effect if they are supplied as options. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 686 | rval = build_actions (list, (struct _optflags *)NULL, &acts, &copts); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 687 | if (rval == EX_USAGE) |
| 688 | return (rval); |
| 689 | if (rval == EXECUTION_FAILURE) |
| 690 | return (EXECUTION_SUCCESS); |
| 691 | |
| 692 | list = loptend; |
| 693 | |
| 694 | word = (list && list->word) ? list->word->word : ""; |
| 695 | |
| 696 | if (Farg) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 697 | builtin_error (_("warning: -F option may not work as you expect")); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 698 | if (Carg) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 699 | builtin_error (_("warning: -C option may not work as you expect")); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 700 | |
| 701 | /* If we get here, we need to build a compspec and evaluate it. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 702 | cs = compspec_create (); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 703 | cs->actions = acts; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 704 | cs->options = copts; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 705 | cs->refcount = 1; |
| 706 | |
| 707 | cs->globpat = STRDUP (Garg); |
| 708 | cs->words = STRDUP (Warg); |
| 709 | cs->prefix = STRDUP (Parg); |
| 710 | cs->suffix = STRDUP (Sarg); |
| 711 | cs->funcname = STRDUP (Farg); |
| 712 | cs->command = STRDUP (Carg); |
| 713 | cs->filterpat = STRDUP (Xarg); |
| 714 | |
| 715 | rval = EXECUTION_FAILURE; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 716 | sl = gen_compspec_completions (cs, "compgen", word, 0, 0, 0); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 717 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 718 | /* If the compspec wants the bash default completions, temporarily |
| 719 | turn off programmable completion and call the bash completion code. */ |
| 720 | if ((sl == 0 || sl->list_len == 0) && (copts & COPT_BASHDEFAULT)) |
| 721 | { |
| 722 | matches = bash_default_completion (word, 0, 0, 0, 0); |
| 723 | sl = completions_to_stringlist (matches); |
| 724 | strvec_dispose (matches); |
| 725 | } |
| 726 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 727 | /* This isn't perfect, but it's the best we can do, given what readline |
| 728 | exports from its set of completion utility functions. */ |
| 729 | if ((sl == 0 || sl->list_len == 0) && (copts & COPT_DEFAULT)) |
| 730 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 731 | matches = rl_completion_matches (word, rl_filename_completion_function); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 732 | strlist_dispose (sl); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 733 | sl = completions_to_stringlist (matches); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 734 | strvec_dispose (matches); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 737 | if (sl) |
| 738 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 739 | if (sl->list && sl->list_len) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 740 | { |
| 741 | rval = EXECUTION_SUCCESS; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 742 | strlist_print (sl, (char *)NULL); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 743 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 744 | strlist_dispose (sl); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 747 | compspec_dispose (cs); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 748 | return (rval); |
| 749 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 750 | |
| 751 | $BUILTIN compopt |
| 752 | $DEPENDS_ON PROGRAMMABLE_COMPLETION |
| 753 | $FUNCTION compopt_builtin |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 754 | $SHORT_DOC compopt [-o|+o option] [-DE] [name ...] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 755 | Modify or display completion options. |
| 756 | |
| 757 | Modify the completion options for each NAME, or, if no NAMEs are supplied, |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 758 | the completion currently being executed. If no OPTIONs are given, print |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 759 | the completion options for each NAME or the current completion specification. |
| 760 | |
| 761 | Options: |
| 762 | -o option Set completion option OPTION for each NAME |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 763 | -D Change options for the "default" command completion |
| 764 | -E Change options for the "empty" command completion |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 765 | |
| 766 | Using `+o' instead of `-o' turns off the specified option. |
| 767 | |
| 768 | Arguments: |
| 769 | |
| 770 | Each NAME refers to a command for which a completion specification must |
| 771 | have previously been defined using the `complete' builtin. If no NAMEs |
| 772 | are supplied, compopt must be called by a function currently generating |
| 773 | completions, and the options for that currently-executing completion |
| 774 | generator are modified. |
| 775 | |
| 776 | Exit Status: |
| 777 | Returns success unless an invalid option is supplied or NAME does not |
| 778 | have a completion specification defined. |
| 779 | $END |
| 780 | |
| 781 | int |
| 782 | compopt_builtin (list) |
| 783 | WORD_LIST *list; |
| 784 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 785 | int opts_on, opts_off, *opts, opt, oind, ret, Dflag, Eflag; |
| 786 | WORD_LIST *l, *wl; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 787 | COMPSPEC *cs; |
| 788 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 789 | opts_on = opts_off = Eflag = Dflag = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 790 | ret = EXECUTION_SUCCESS; |
| 791 | |
| 792 | reset_internal_getopt (); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 793 | while ((opt = internal_getopt (list, "+o:DE")) != EOF) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 794 | { |
| 795 | opts = (list_opttype == '-') ? &opts_on : &opts_off; |
| 796 | |
| 797 | switch (opt) |
| 798 | { |
| 799 | case 'o': |
| 800 | oind = find_compopt (list_optarg); |
| 801 | if (oind < 0) |
| 802 | { |
| 803 | sh_invalidoptname (list_optarg); |
| 804 | return (EX_USAGE); |
| 805 | } |
| 806 | *opts |= compopts[oind].optflag; |
| 807 | break; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 808 | case 'D': |
| 809 | Dflag = 1; |
| 810 | break; |
| 811 | case 'E': |
| 812 | Eflag = 1; |
| 813 | break; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 814 | default: |
| 815 | builtin_usage (); |
| 816 | return (EX_USAGE); |
| 817 | } |
| 818 | } |
| 819 | list = loptend; |
| 820 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 821 | wl = Dflag ? make_word_list (make_bare_word (DEFAULTCMD), (WORD_LIST *)NULL) |
| 822 | : (Eflag ? make_word_list (make_bare_word (EMPTYCMD), (WORD_LIST *)NULL) : 0); |
| 823 | |
| 824 | if (list == 0 && wl == 0) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 825 | { |
| 826 | if (RL_ISSTATE (RL_STATE_COMPLETING) == 0 || pcomp_curcs == 0) |
| 827 | { |
| 828 | builtin_error (_("not currently executing completion function")); |
| 829 | return (EXECUTION_FAILURE); |
| 830 | } |
| 831 | cs = pcomp_curcs; |
| 832 | |
| 833 | if (opts_on == 0 && opts_off == 0) |
| 834 | { |
| 835 | print_compopts (pcomp_curcmd, cs, 1); |
| 836 | return (sh_chkwrite (ret)); |
| 837 | } |
| 838 | |
| 839 | /* Set the compspec options */ |
| 840 | pcomp_set_compspec_options (cs, opts_on, 1); |
| 841 | pcomp_set_compspec_options (cs, opts_off, 0); |
| 842 | |
| 843 | /* And change the readline variables the options control */ |
| 844 | pcomp_set_readline_variables (opts_on, 1); |
| 845 | pcomp_set_readline_variables (opts_off, 0); |
| 846 | |
| 847 | return (ret); |
| 848 | } |
| 849 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 850 | for (l = wl ? wl : list; l; l = l->next) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 851 | { |
| 852 | cs = progcomp_search (l->word->word); |
| 853 | if (cs == 0) |
| 854 | { |
| 855 | builtin_error (_("%s: no completion specification"), l->word->word); |
| 856 | ret = EXECUTION_FAILURE; |
| 857 | continue; |
| 858 | } |
| 859 | if (opts_on == 0 && opts_off == 0) |
| 860 | { |
| 861 | print_compopts (l->word->word, cs, 1); |
| 862 | continue; /* XXX -- fill in later */ |
| 863 | } |
| 864 | |
| 865 | /* Set the compspec options */ |
| 866 | pcomp_set_compspec_options (cs, opts_on, 1); |
| 867 | pcomp_set_compspec_options (cs, opts_off, 0); |
| 868 | } |
| 869 | |
| 870 | return (ret); |
| 871 | } |