]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/shopt.def
Imported from ../bash-3.2.48.tar.gz.
[thirdparty/bash.git] / builtins / shopt.def
CommitLineData
ccc6cda3
JA
1This file is shopt.def, from which is created shopt.c.
2It implements the Bash `shopt' builtin.
3
95732b49 4Copyright (C) 1994-2005 Free Software Foundation, Inc.
ccc6cda3
JA
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
bb70624e 10Software Foundation; either version 2, or (at your option) any later
ccc6cda3
JA
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
bb70624e 20Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
ccc6cda3
JA
21
22$PRODUCES shopt.c
23
24$BUILTIN shopt
ccc6cda3
JA
25$FUNCTION shopt_builtin
26$SHORT_DOC shopt [-pqsu] [-o long-option] optname [optname...]
27Toggle the values of variables controlling optional behavior.
28The -s flag means to enable (set) each OPTNAME; the -u flag
29unsets each OPTNAME. The -q flag suppresses output; the exit
30status indicates whether each OPTNAME is set or unset. The -o
31option restricts the OPTNAMEs to those defined for use with
32`set -o'. With no options, or with the -p option, a list of all
33settable options is displayed, with an indication of whether or
34not each is set.
35$END
36
37#include <config.h>
38
39#if defined (HAVE_UNISTD_H)
cce855bc
JA
40# ifdef _MINIX
41# include <sys/types.h>
42# endif
ccc6cda3
JA
43# include <unistd.h>
44#endif
45
46#include <stdio.h>
47
b80f6443
JA
48#include "../bashintl.h"
49
ccc6cda3
JA
50#include "../shell.h"
51#include "../flags.h"
52#include "common.h"
53#include "bashgetopt.h"
54
55#define UNSETOPT 0
56#define SETOPT 1
57
58#define OPTFMT "%-15s\t%s\n"
59
b80f6443 60extern int allow_null_glob_expansion, fail_glob_expansion, glob_dot_filenames;
ccc6cda3
JA
61extern int cdable_vars, mail_warning, source_uses_path;
62extern int no_exit_on_failed_exec, print_shift_error;
7117c2d2 63extern int check_hashed_filenames, promptvars;
ccc6cda3 64extern int cdspelling, expand_aliases;
b80f6443 65extern int extended_quote;
ccc6cda3 66extern int check_window_size;
95732b49 67extern int glob_ignore_case, match_ignore_case;
cce855bc 68extern int hup_on_exit;
bb70624e 69extern int xpg_echo;
b80f6443 70extern int gnu_error_format;
cce855bc
JA
71
72#if defined (EXTENDED_GLOB)
73extern int extended_glob;
74#endif
ccc6cda3
JA
75
76#if defined (HISTORY)
d166f048 77extern int literal_history, command_oriented_history;
ccc6cda3
JA
78extern int force_append_history;
79#endif
80
81#if defined (READLINE)
d166f048 82extern int hist_verify, history_reediting, perform_hostname_completion;
bb70624e 83extern int no_empty_command_completion;
b80f6443 84extern int force_fignore;
f73dda09 85extern int enable_hostname_completion __P((int));
ccc6cda3
JA
86#endif
87
bb70624e
JA
88#if defined (PROGRAMMABLE_COMPLETION)
89extern int prog_completion_enabled;
90#endif
91
b72432fd 92#if defined (RESTRICTED_SHELL)
b72432fd
JA
93extern char *shell_name;
94#endif
95
b80f6443
JA
96#if defined (DEBUGGER)
97extern int debugging_mode;
98#endif
99
7117c2d2
JA
100static void shopt_error __P((char *));
101
b80f6443 102static int set_shellopts_after_change __P((int));
ccc6cda3 103
f1be666c
JA
104static int set_compatibility_level __P((int));
105
b72432fd 106#if defined (RESTRICTED_SHELL)
f73dda09 107static int set_restricted_shell __P((int));
b72432fd
JA
108#endif
109
f73dda09 110static int shopt_login_shell;
f1be666c 111static int shopt_compat31;
f73dda09
JA
112
113typedef int shopt_set_func_t __P((int));
114
ccc6cda3
JA
115static struct {
116 char *name;
117 int *value;
f73dda09 118 shopt_set_func_t *set_func;
ccc6cda3 119} shopt_vars[] = {
f73dda09
JA
120 { "cdable_vars", &cdable_vars, (shopt_set_func_t *)NULL },
121 { "cdspell", &cdspelling, (shopt_set_func_t *)NULL },
122 { "checkhash", &check_hashed_filenames, (shopt_set_func_t *)NULL },
123 { "checkwinsize", &check_window_size, (shopt_set_func_t *)NULL },
ccc6cda3 124#if defined (HISTORY)
f73dda09 125 { "cmdhist", &command_oriented_history, (shopt_set_func_t *)NULL },
ccc6cda3 126#endif
f1be666c 127 { "compat31", &shopt_compat31, set_compatibility_level },
f73dda09
JA
128 { "dotglob", &glob_dot_filenames, (shopt_set_func_t *)NULL },
129 { "execfail", &no_exit_on_failed_exec, (shopt_set_func_t *)NULL },
130 { "expand_aliases", &expand_aliases, (shopt_set_func_t *)NULL },
b80f6443
JA
131#if defined (DEBUGGER)
132 { "extdebug", &debugging_mode, (shopt_set_func_t *)NULL },
133#endif
cce855bc 134#if defined (EXTENDED_GLOB)
f73dda09 135 { "extglob", &extended_glob, (shopt_set_func_t *)NULL },
cce855bc 136#endif
b80f6443
JA
137 { "extquote", &extended_quote, (shopt_set_func_t *)NULL },
138 { "failglob", &fail_glob_expansion, (shopt_set_func_t *)NULL },
ccc6cda3 139#if defined (READLINE)
b80f6443 140 { "force_fignore", &force_fignore, (shopt_set_func_t *)NULL },
ccc6cda3 141#endif
95732b49 142 { "gnu_errfmt", &gnu_error_format, (shopt_set_func_t *)NULL },
ccc6cda3 143#if defined (HISTORY)
f73dda09 144 { "histappend", &force_append_history, (shopt_set_func_t *)NULL },
ccc6cda3
JA
145#endif
146#if defined (READLINE)
95732b49 147 { "histreedit", &history_reediting, (shopt_set_func_t *)NULL },
f73dda09
JA
148 { "histverify", &hist_verify, (shopt_set_func_t *)NULL },
149 { "hostcomplete", &perform_hostname_completion, enable_hostname_completion },
ccc6cda3 150#endif
f73dda09 151 { "huponexit", &hup_on_exit, (shopt_set_func_t *)NULL },
b80f6443 152 { "interactive_comments", &interactive_comments, set_shellopts_after_change },
ccc6cda3 153#if defined (HISTORY)
f73dda09 154 { "lithist", &literal_history, (shopt_set_func_t *)NULL },
ccc6cda3 155#endif
f73dda09
JA
156 { "login_shell", &shopt_login_shell, set_login_shell },
157 { "mailwarn", &mail_warning, (shopt_set_func_t *)NULL },
bb70624e 158#if defined (READLINE)
f73dda09 159 { "no_empty_cmd_completion", &no_empty_command_completion, (shopt_set_func_t *)NULL },
bb70624e 160#endif
f73dda09 161 { "nocaseglob", &glob_ignore_case, (shopt_set_func_t *)NULL },
95732b49 162 { "nocasematch", &match_ignore_case, (shopt_set_func_t *)NULL },
f73dda09 163 { "nullglob", &allow_null_glob_expansion, (shopt_set_func_t *)NULL },
bb70624e 164#if defined (PROGRAMMABLE_COMPLETION)
f73dda09 165 { "progcomp", &prog_completion_enabled, (shopt_set_func_t *)NULL },
bb70624e 166#endif
f73dda09 167 { "promptvars", &promptvars, (shopt_set_func_t *)NULL },
b72432fd
JA
168#if defined (RESTRICTED_SHELL)
169 { "restricted_shell", &restricted_shell, set_restricted_shell },
170#endif
f73dda09
JA
171 { "shift_verbose", &print_shift_error, (shopt_set_func_t *)NULL },
172 { "sourcepath", &source_uses_path, (shopt_set_func_t *)NULL },
173 { "xpg_echo", &xpg_echo, (shopt_set_func_t *)NULL },
174 { (char *)0, (int *)0, (shopt_set_func_t *)NULL }
ccc6cda3
JA
175};
176
177static char *on = "on";
178static char *off = "off";
179
f73dda09
JA
180static int find_shopt __P((char *));
181static int toggle_shopts __P((int, WORD_LIST *, int));
182static void print_shopt __P((char *, int, int));
183static int list_shopts __P((WORD_LIST *, int));
184static int list_some_shopts __P((int, int));
185static int list_shopt_o_options __P((WORD_LIST *, int));
186static int list_some_o_options __P((int, int));
187static int set_shopt_o_options __P((int, WORD_LIST *, int));
ccc6cda3
JA
188
189#define SFLAG 0x01
190#define UFLAG 0x02
191#define QFLAG 0x04
192#define OFLAG 0x08
193#define PFLAG 0x10
194
195int
196shopt_builtin (list)
197 WORD_LIST *list;
198{
199 int opt, flags, rval;
200
201 flags = 0;
202 reset_internal_getopt ();
203 while ((opt = internal_getopt (list, "psuoq")) != -1)
204 {
205 switch (opt)
206 {
207 case 's':
208 flags |= SFLAG;
209 break;
210 case 'u':
211 flags |= UFLAG;
212 break;
213 case 'q':
214 flags |= QFLAG;
215 break;
216 case 'o':
217 flags |= OFLAG;
218 break;
219 case 'p':
220 flags |= PFLAG;
221 break;
222 default:
223 builtin_usage ();
224 return (EX_USAGE);
225 }
226 }
227 list = loptend;
228
229 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
230 {
b80f6443 231 builtin_error (_("cannot set and unset shell options simultaneously"));
ccc6cda3
JA
232 return (EXECUTION_FAILURE);
233 }
234
235 rval = EXECUTION_SUCCESS;
236 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
cce855bc 237 rval = list_shopt_o_options (list, flags);
ccc6cda3
JA
238 else if (list && (flags & OFLAG)) /* shopt -so args */
239 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
240 else if (flags & OFLAG) /* shopt -so */
cce855bc 241 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
ccc6cda3
JA
242 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
243 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
244 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
cce855bc 245 rval = list_shopts (list, flags);
ccc6cda3 246 else /* shopt -su */
cce855bc 247 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
ccc6cda3
JA
248 return (rval);
249}
250
d166f048
JA
251/* Reset the options managed by `shopt' to the values they would have at
252 shell startup. */
253void
254reset_shopt_options ()
255{
256 allow_null_glob_expansion = glob_dot_filenames = 0;
257 cdable_vars = mail_warning = 0;
258 no_exit_on_failed_exec = print_shift_error = 0;
259 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
260
261 source_uses_path = promptvars = 1;
262
cce855bc
JA
263#if defined (EXTENDED_GLOB)
264 extended_glob = 0;
265#endif
266
d166f048
JA
267#if defined (HISTORY)
268 literal_history = force_append_history = 0;
269 command_oriented_history = 1;
270#endif
271
272#if defined (READLINE)
273 hist_verify = history_reediting = 0;
274 perform_hostname_completion = 1;
275#endif
f73dda09
JA
276
277 shopt_login_shell = login_shell;
d166f048
JA
278}
279
ccc6cda3
JA
280static int
281find_shopt (name)
282 char *name;
283{
284 int i;
285
286 for (i = 0; shopt_vars[i].name; i++)
287 if (STREQ (name, shopt_vars[i].name))
288 return i;
289 return -1;
290}
291
7117c2d2
JA
292static void
293shopt_error (s)
294 char *s;
295{
b80f6443 296 builtin_error (_("%s: invalid shell option name"), s);
7117c2d2 297}
ccc6cda3
JA
298
299static int
300toggle_shopts (mode, list, quiet)
301 int mode;
302 WORD_LIST *list;
303 int quiet;
304{
305 WORD_LIST *l;
306 int ind, rval;
307
308 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
309 {
310 ind = find_shopt (l->word->word);
311 if (ind < 0)
312 {
7117c2d2 313 shopt_error (l->word->word);
ccc6cda3
JA
314 rval = EXECUTION_FAILURE;
315 }
316 else
317 {
318 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
319 if (shopt_vars[ind].set_func)
320 (*shopt_vars[ind].set_func) (mode);
321 }
322 }
323 return (rval);
324}
325
b72432fd 326static void
cce855bc
JA
327print_shopt (name, val, flags)
328 char *name;
329 int val, flags;
330{
331 if (flags & PFLAG)
332 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
333 else
334 printf (OPTFMT, name, val ? on : off);
335}
336
ccc6cda3
JA
337/* List the values of all or any of the `shopt' options. Returns 0 if
338 all were listed or all variables queried were on; 1 otherwise. */
339static int
cce855bc 340list_shopts (list, flags)
ccc6cda3 341 WORD_LIST *list;
cce855bc 342 int flags;
ccc6cda3
JA
343{
344 WORD_LIST *l;
345 int i, val, rval;
346
347 if (list == 0)
348 {
349 for (i = 0; shopt_vars[i].name; i++)
350 {
351 val = *shopt_vars[i].value;
cce855bc
JA
352 if ((flags & QFLAG) == 0)
353 print_shopt (shopt_vars[i].name, val, flags);
ccc6cda3
JA
354 }
355 return (EXECUTION_SUCCESS);
356 }
357
358 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
359 {
360 i = find_shopt (l->word->word);
361 if (i < 0)
362 {
7117c2d2 363 shopt_error (l->word->word);
ccc6cda3
JA
364 rval = EXECUTION_FAILURE;
365 continue;
366 }
367 val = *shopt_vars[i].value;
368 if (val == 0)
369 rval = EXECUTION_FAILURE;
cce855bc 370 if ((flags & QFLAG) == 0)
28ef6c31 371 print_shopt (l->word->word, val, flags);
ccc6cda3 372 }
28ef6c31 373
ccc6cda3
JA
374 return (rval);
375}
376
377static int
cce855bc
JA
378list_some_shopts (mode, flags)
379 int mode, flags;
ccc6cda3
JA
380{
381 int val, i;
382
383 for (i = 0; shopt_vars[i].name; i++)
384 {
385 val = *shopt_vars[i].value;
cce855bc
JA
386 if (((flags & QFLAG) == 0) && mode == val)
387 print_shopt (shopt_vars[i].name, val, flags);
ccc6cda3
JA
388 }
389 return (EXECUTION_SUCCESS);
390}
391
392static int
cce855bc 393list_shopt_o_options (list, flags)
ccc6cda3 394 WORD_LIST *list;
cce855bc 395 int flags;
ccc6cda3
JA
396{
397 WORD_LIST *l;
398 int val, rval;
399
400 if (list == 0)
401 {
cce855bc
JA
402 if ((flags & QFLAG) == 0)
403 list_minus_o_opts (-1, (flags & PFLAG));
ccc6cda3
JA
404 return (EXECUTION_SUCCESS);
405 }
406
407 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
408 {
409 val = minus_o_option_value (l->word->word);
410 if (val == -1)
411 {
7117c2d2 412 sh_invalidoptname (l->word->word);
ccc6cda3
JA
413 rval = EXECUTION_FAILURE;
414 continue;
415 }
416 if (val == 0)
417 rval = EXECUTION_FAILURE;
cce855bc
JA
418 if ((flags & QFLAG) == 0)
419 {
420 if (flags & PFLAG)
421 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
422 else
423 printf (OPTFMT, l->word->word, val ? on : off);
424 }
ccc6cda3
JA
425 }
426 return (rval);
427}
428
429static int
cce855bc
JA
430list_some_o_options (mode, flags)
431 int mode, flags;
ccc6cda3 432{
cce855bc
JA
433 if ((flags & QFLAG) == 0)
434 list_minus_o_opts (mode, (flags & PFLAG));
ccc6cda3
JA
435 return (EXECUTION_SUCCESS);
436}
437
438static int
439set_shopt_o_options (mode, list, quiet)
440 int mode;
441 WORD_LIST *list;
442 int quiet;
443{
444 WORD_LIST *l;
445 int rval;
446
447 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
448 {
449 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
450 rval = EXECUTION_FAILURE;
451 }
452 set_shellopts ();
453 return rval;
454}
455
456/* If we set or unset interactive_comments with shopt, make sure the
457 change is reflected in $SHELLOPTS. */
458static int
b80f6443 459set_shellopts_after_change (mode)
ccc6cda3
JA
460 int mode;
461{
462 set_shellopts ();
463 return (0);
464}
b72432fd 465
f1be666c
JA
466static int
467set_compatibility_level (mode)
468 int mode;
469{
470 /* Need to change logic here as we add more compatibility levels */
471 if (shopt_compat31)
472 shell_compatibility_level = 31;
473 else
474 shell_compatibility_level = 32;
475 return 0;
476}
477
b72432fd
JA
478#if defined (RESTRICTED_SHELL)
479/* Don't allow the value of restricted_shell to be modified. */
480
481static int
482set_restricted_shell (mode)
483 int mode;
484{
485 static int save_restricted = -1;
486
487 if (save_restricted == -1)
488 save_restricted = shell_is_restricted (shell_name);
489
490 restricted_shell = save_restricted;
491 return (0);
492}
493#endif /* RESTRICTED_SHELL */
bb70624e 494
f73dda09
JA
495/* Not static so shell.c can call it to initialize shopt_login_shell */
496int
497set_login_shell (mode)
498 int mode;
499{
500 shopt_login_shell = login_shell != 0;
501 return (0);
502}
503
bb70624e
JA
504char **
505get_shopt_options ()
506{
507 char **ret;
508 int n, i;
509
510 n = sizeof (shopt_vars) / sizeof (shopt_vars[0]);
7117c2d2 511 ret = strvec_create (n + 1);
bb70624e
JA
512 for (i = 0; shopt_vars[i].name; i++)
513 ret[i] = savestring (shopt_vars[i].name);
514 ret[i] = (char *)NULL;
515 return ret;
516}
f73dda09
JA
517
518/*
519 * External interface for other parts of the shell. NAME is a string option;
520 * MODE is 0 if we want to unset an option; 1 if we want to set an option.
521 * REUSABLE is 1 if we want to print output in a form that may be reused.
522 */
523int
524shopt_setopt (name, mode)
525 char *name;
526 int mode;
527{
528 WORD_LIST *wl;
529 int r;
530
531 wl = add_string_to_list (name, (WORD_LIST *)NULL);
532 r = toggle_shopts (mode, wl, 0);
533 dispose_words (wl);
534 return r;
535}
536
537int
538shopt_listopt (name, reusable)
539 char *name;
540 int reusable;
541{
542 int i;
543
544 if (name == 0)
545 return (list_shopts ((WORD_LIST *)NULL, reusable ? PFLAG : 0));
546
547 i = find_shopt (name);
548 if (i < 0)
549 {
7117c2d2 550 shopt_error (name);
f73dda09
JA
551 return (EXECUTION_FAILURE);
552 }
553
554 print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0);
555 return (EXECUTION_SUCCESS);
556}