1 This file is shopt.def, from which is created shopt.c.
2 It implements the Bash `shopt' builtin.
4 Copyright (C) 1994-2010 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
24 $FUNCTION shopt_builtin
25 $SHORT_DOC shopt [-pqsu] [-o] [optname ...]
26 Set and unset shell options.
28 Change the setting of each shell option OPTNAME. Without any option
29 arguments, list all shell options with an indication of whether or not each
33 -o restrict OPTNAMEs to those defined for use with `set -o'
34 -p print each shell option with an indication of its status
36 -s enable (set) each OPTNAME
37 -u disable (unset) each OPTNAME
40 Returns success if OPTNAME is enabled; fails if an invalid option is
41 given or OPTNAME is disabled.
46 #if defined (HAVE_UNISTD_H)
48 # include <sys/types.h>
57 #include "../bashintl.h"
62 #include "bashgetopt.h"
65 # include "../bashhist.h"
71 #define OPTFMT "%-15s\t%s\n"
73 extern int allow_null_glob_expansion, fail_glob_expansion, glob_dot_filenames;
74 extern int cdable_vars, mail_warning, source_uses_path;
75 extern int no_exit_on_failed_exec, print_shift_error;
76 extern int check_hashed_filenames, promptvars;
77 extern int cdspelling, expand_aliases;
78 extern int extended_quote;
79 extern int check_window_size;
80 extern int glob_ignore_case, match_ignore_case;
81 extern int hup_on_exit;
83 extern int gnu_error_format;
84 extern int check_jobs_at_exit;
87 extern int lastpipe_opt;
89 #if defined (EXTENDED_GLOB)
90 extern int extended_glob;
93 #if defined (READLINE)
94 extern int hist_verify, history_reediting, perform_hostname_completion;
95 extern int no_empty_command_completion;
96 extern int force_fignore;
97 extern int dircomplete_spelling;
99 extern int enable_hostname_completion __P((int));
102 #if defined (PROGRAMMABLE_COMPLETION)
103 extern int prog_completion_enabled;
106 #if defined (RESTRICTED_SHELL)
107 extern char *shell_name;
110 #if defined (DEBUGGER)
111 extern int debugging_mode;
114 static void shopt_error __P((char *));
116 static int set_shellopts_after_change __P((char *, int));
117 static int shopt_enable_hostname_completion __P((char *, int));
118 static int set_compatibility_level __P((char *, int));
120 #if defined (RESTRICTED_SHELL)
121 static int set_restricted_shell __P((char *, int));
124 static int shopt_login_shell;
125 static int shopt_compat31;
126 static int shopt_compat32;
127 static int shopt_compat40;
128 static int shopt_compat41;
130 typedef int shopt_set_func_t __P((char *, int));
135 shopt_set_func_t *set_func;
137 { "autocd", &autocd, (shopt_set_func_t *)NULL },
138 { "cdable_vars", &cdable_vars, (shopt_set_func_t *)NULL },
139 { "cdspell", &cdspelling, (shopt_set_func_t *)NULL },
140 { "checkhash", &check_hashed_filenames, (shopt_set_func_t *)NULL },
141 #if defined (JOB_CONTROL)
142 { "checkjobs", &check_jobs_at_exit, (shopt_set_func_t *)NULL },
144 { "checkwinsize", &check_window_size, (shopt_set_func_t *)NULL },
145 #if defined (HISTORY)
146 { "cmdhist", &command_oriented_history, (shopt_set_func_t *)NULL },
148 { "compat31", &shopt_compat31, set_compatibility_level },
149 { "compat32", &shopt_compat32, set_compatibility_level },
150 { "compat40", &shopt_compat40, set_compatibility_level },
151 { "compat41", &shopt_compat41, set_compatibility_level },
152 #if defined (READLINE)
153 { "dirspell", &dircomplete_spelling, (shopt_set_func_t *)NULL },
155 { "dotglob", &glob_dot_filenames, (shopt_set_func_t *)NULL },
156 { "execfail", &no_exit_on_failed_exec, (shopt_set_func_t *)NULL },
157 { "expand_aliases", &expand_aliases, (shopt_set_func_t *)NULL },
158 #if defined (DEBUGGER)
159 { "extdebug", &debugging_mode, (shopt_set_func_t *)NULL },
161 #if defined (EXTENDED_GLOB)
162 { "extglob", &extended_glob, (shopt_set_func_t *)NULL },
164 { "extquote", &extended_quote, (shopt_set_func_t *)NULL },
165 { "failglob", &fail_glob_expansion, (shopt_set_func_t *)NULL },
166 #if defined (READLINE)
167 { "force_fignore", &force_fignore, (shopt_set_func_t *)NULL },
169 { "globstar", &glob_star, (shopt_set_func_t *)NULL },
170 { "gnu_errfmt", &gnu_error_format, (shopt_set_func_t *)NULL },
171 #if defined (HISTORY)
172 { "histappend", &force_append_history, (shopt_set_func_t *)NULL },
174 #if defined (READLINE)
175 { "histreedit", &history_reediting, (shopt_set_func_t *)NULL },
176 { "histverify", &hist_verify, (shopt_set_func_t *)NULL },
177 { "hostcomplete", &perform_hostname_completion, shopt_enable_hostname_completion },
179 { "huponexit", &hup_on_exit, (shopt_set_func_t *)NULL },
180 { "interactive_comments", &interactive_comments, set_shellopts_after_change },
181 { "lastpipe", &lastpipe_opt, (shopt_set_func_t *)NULL },
182 #if defined (HISTORY)
183 { "lithist", &literal_history, (shopt_set_func_t *)NULL },
185 { "login_shell", &shopt_login_shell, set_login_shell },
186 { "mailwarn", &mail_warning, (shopt_set_func_t *)NULL },
187 #if defined (READLINE)
188 { "no_empty_cmd_completion", &no_empty_command_completion, (shopt_set_func_t *)NULL },
190 { "nocaseglob", &glob_ignore_case, (shopt_set_func_t *)NULL },
191 { "nocasematch", &match_ignore_case, (shopt_set_func_t *)NULL },
192 { "nullglob", &allow_null_glob_expansion, (shopt_set_func_t *)NULL },
193 #if defined (PROGRAMMABLE_COMPLETION)
194 { "progcomp", &prog_completion_enabled, (shopt_set_func_t *)NULL },
196 { "promptvars", &promptvars, (shopt_set_func_t *)NULL },
197 #if defined (RESTRICTED_SHELL)
198 { "restricted_shell", &restricted_shell, set_restricted_shell },
200 { "shift_verbose", &print_shift_error, (shopt_set_func_t *)NULL },
201 { "sourcepath", &source_uses_path, (shopt_set_func_t *)NULL },
202 { "xpg_echo", &xpg_echo, (shopt_set_func_t *)NULL },
203 { (char *)0, (int *)0, (shopt_set_func_t *)NULL }
206 #define N_SHOPT_OPTIONS (sizeof (shopt_vars) / sizeof (shopt_vars[0]))
208 #define GET_SHOPT_OPTION_VALUE(i) (*shopt_vars[i].value)
210 static const char * const on = "on";
211 static const char * const off = "off";
213 static int find_shopt __P((char *));
214 static int toggle_shopts __P((int, WORD_LIST *, int));
215 static void print_shopt __P((char *, int, int));
216 static int list_shopts __P((WORD_LIST *, int));
217 static int list_some_shopts __P((int, int));
218 static int list_shopt_o_options __P((WORD_LIST *, int));
219 static int list_some_o_options __P((int, int));
220 static int set_shopt_o_options __P((int, WORD_LIST *, int));
232 int opt, flags, rval;
235 reset_internal_getopt ();
236 while ((opt = internal_getopt (list, "psuoq")) != -1)
262 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
264 builtin_error (_("cannot set and unset shell options simultaneously"));
265 return (EXECUTION_FAILURE);
268 rval = EXECUTION_SUCCESS;
269 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
270 rval = list_shopt_o_options (list, flags);
271 else if (list && (flags & OFLAG)) /* shopt -so args */
272 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
273 else if (flags & OFLAG) /* shopt -so */
274 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
275 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
276 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
277 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
278 rval = list_shopts (list, flags);
280 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
284 /* Reset the options managed by `shopt' to the values they would have at
287 reset_shopt_options ()
289 allow_null_glob_expansion = glob_dot_filenames = 0;
290 cdable_vars = mail_warning = 0;
291 no_exit_on_failed_exec = print_shift_error = 0;
292 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
294 source_uses_path = promptvars = 1;
296 #if defined (EXTENDED_GLOB)
300 #if defined (HISTORY)
301 literal_history = force_append_history = 0;
302 command_oriented_history = 1;
305 #if defined (READLINE)
306 hist_verify = history_reediting = 0;
307 perform_hostname_completion = 1;
310 shopt_login_shell = login_shell;
319 for (i = 0; shopt_vars[i].name; i++)
320 if (STREQ (name, shopt_vars[i].name))
329 builtin_error (_("%s: invalid shell option name"), s);
333 toggle_shopts (mode, list, quiet)
341 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
343 ind = find_shopt (l->word->word);
346 shopt_error (l->word->word);
347 rval = EXECUTION_FAILURE;
351 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
352 if (shopt_vars[ind].set_func)
353 (*shopt_vars[ind].set_func) (shopt_vars[ind].name, mode);
362 print_shopt (name, val, flags)
367 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
369 printf (OPTFMT, name, val ? on : off);
372 /* List the values of all or any of the `shopt' options. Returns 0 if
373 all were listed or all variables queried were on; 1 otherwise. */
375 list_shopts (list, flags)
384 for (i = 0; shopt_vars[i].name; i++)
386 val = *shopt_vars[i].value;
387 if ((flags & QFLAG) == 0)
388 print_shopt (shopt_vars[i].name, val, flags);
390 return (sh_chkwrite (EXECUTION_SUCCESS));
393 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
395 i = find_shopt (l->word->word);
398 shopt_error (l->word->word);
399 rval = EXECUTION_FAILURE;
402 val = *shopt_vars[i].value;
404 rval = EXECUTION_FAILURE;
405 if ((flags & QFLAG) == 0)
406 print_shopt (l->word->word, val, flags);
409 return (sh_chkwrite (rval));
413 list_some_shopts (mode, flags)
418 for (i = 0; shopt_vars[i].name; i++)
420 val = *shopt_vars[i].value;
421 if (((flags & QFLAG) == 0) && mode == val)
422 print_shopt (shopt_vars[i].name, val, flags);
424 return (sh_chkwrite (EXECUTION_SUCCESS));
428 list_shopt_o_options (list, flags)
437 if ((flags & QFLAG) == 0)
438 list_minus_o_opts (-1, (flags & PFLAG));
439 return (sh_chkwrite (EXECUTION_SUCCESS));
442 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
444 val = minus_o_option_value (l->word->word);
447 sh_invalidoptname (l->word->word);
448 rval = EXECUTION_FAILURE;
452 rval = EXECUTION_FAILURE;
453 if ((flags & QFLAG) == 0)
456 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
458 printf (OPTFMT, l->word->word, val ? on : off);
461 return (sh_chkwrite (rval));
465 list_some_o_options (mode, flags)
468 if ((flags & QFLAG) == 0)
469 list_minus_o_opts (mode, (flags & PFLAG));
470 return (sh_chkwrite (EXECUTION_SUCCESS));
474 set_shopt_o_options (mode, list, quiet)
482 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
484 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
485 rval = EXECUTION_FAILURE;
491 /* If we set or unset interactive_comments with shopt, make sure the
492 change is reflected in $SHELLOPTS. */
494 set_shellopts_after_change (option_name, mode)
503 shopt_enable_hostname_completion (option_name, mode)
507 return (enable_hostname_completion (mode));
511 set_compatibility_level (option_name, mode)
515 /* Need to change logic here as we add more compatibility levels */
517 /* First, check option_name so we can turn off other compat options when
519 if (mode && option_name[6] == '3' && option_name[7] == '1')
520 shopt_compat32 = shopt_compat40 = 0;
521 else if (mode && option_name[6] == '3' && option_name[7] == '2')
522 shopt_compat31 = shopt_compat40 = 0;
523 else if (mode && option_name[6] == '4' && option_name[7] == '0')
524 shopt_compat31 = shopt_compat32 = 0;
526 /* Then set shell_compatibility_level based on what remains */
528 shell_compatibility_level = 31;
529 else if (shopt_compat32)
530 shell_compatibility_level = 32;
531 else if (shopt_compat40)
532 shell_compatibility_level = 40;
534 shell_compatibility_level = DEFAULT_COMPAT_LEVEL;
538 #if defined (RESTRICTED_SHELL)
539 /* Don't allow the value of restricted_shell to be modified. */
542 set_restricted_shell (option_name, mode)
546 static int save_restricted = -1;
548 if (save_restricted == -1)
549 save_restricted = shell_is_restricted (shell_name);
551 restricted_shell = save_restricted;
554 #endif /* RESTRICTED_SHELL */
556 /* Not static so shell.c can call it to initialize shopt_login_shell */
558 set_login_shell (option_name, mode)
562 shopt_login_shell = login_shell != 0;
572 n = sizeof (shopt_vars) / sizeof (shopt_vars[0]);
573 ret = strvec_create (n + 1);
574 for (i = 0; shopt_vars[i].name; i++)
575 ret[i] = savestring (shopt_vars[i].name);
576 ret[i] = (char *)NULL;
581 * External interface for other parts of the shell. NAME is a string option;
582 * MODE is 0 if we want to unset an option; 1 if we want to set an option.
583 * REUSABLE is 1 if we want to print output in a form that may be reused.
586 shopt_setopt (name, mode)
593 wl = add_string_to_list (name, (WORD_LIST *)NULL);
594 r = toggle_shopts (mode, wl, 0);
600 shopt_listopt (name, reusable)
607 return (list_shopts ((WORD_LIST *)NULL, reusable ? PFLAG : 0));
609 i = find_shopt (name);
613 return (EXECUTION_FAILURE);
616 print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0);
617 return (sh_chkwrite (EXECUTION_SUCCESS));
624 char tflag[N_SHOPT_OPTIONS];
625 int vsize, i, vptr, *ip, exported;
628 for (vsize = i = 0; shopt_vars[i].name; i++)
631 if (GET_SHOPT_OPTION_VALUE (i))
633 vsize += strlen (shopt_vars[i].name) + 1;
638 value = (char *)xmalloc (vsize + 1);
640 for (i = vptr = 0; shopt_vars[i].name; i++)
644 strcpy (value + vptr, shopt_vars[i].name);
645 vptr += strlen (shopt_vars[i].name);
651 vptr--; /* cut off trailing colon */
654 v = find_variable ("BASHOPTS");
656 /* Turn off the read-only attribute so we can bind the new value, and
657 note whether or not the variable was exported. */
660 VUNSETATTR (v, att_readonly);
661 exported = exported_p (v);
666 v = bind_variable ("BASHOPTS", value, 0);
668 /* Turn the read-only attribute back on, and turn off the export attribute
669 if it was set implicitly by mark_modified_vars and SHELLOPTS was not
670 exported before we bound the new value. */
671 VSETATTR (v, att_readonly);
672 if (mark_modified_vars && exported == 0 && exported_p (v))
673 VUNSETATTR (v, att_exported);
679 parse_bashopts (value)
686 while (vname = extract_colon_unit (value, &vptr))
688 ind = find_shopt (vname);
690 *shopt_vars[ind].value = 1;
696 initialize_bashopts (no_bashopts)
702 if (no_bashopts == 0)
704 var = find_variable ("BASHOPTS");
705 /* set up any shell options we may have inherited. */
706 if (var && imported_p (var))
708 temp = (array_p (var) || assoc_p (var)) ? (char *)NULL : savestring (value_cell (var));
711 parse_bashopts (temp);
717 /* Set up the $BASHOPTS variable. */