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"
64 #if defined (READLINE)
65 # include "../bashline.h"
69 # include "../bashhist.h"
75 #define OPTFMT "%-15s\t%s\n"
77 extern int allow_null_glob_expansion, fail_glob_expansion, glob_dot_filenames;
78 extern int cdable_vars, mail_warning, source_uses_path;
79 extern int no_exit_on_failed_exec, print_shift_error;
80 extern int check_hashed_filenames, promptvars;
81 extern int cdspelling, expand_aliases;
82 extern int extended_quote;
83 extern int check_window_size;
84 extern int glob_ignore_case, match_ignore_case;
85 extern int hup_on_exit;
87 extern int gnu_error_format;
88 extern int check_jobs_at_exit;
91 extern int lastpipe_opt;
93 #if defined (EXTENDED_GLOB)
94 extern int extended_glob;
97 #if defined (READLINE)
98 extern int hist_verify, history_reediting, perform_hostname_completion;
99 extern int no_empty_command_completion;
100 extern int force_fignore;
101 extern int dircomplete_spelling, dircomplete_expand;
103 extern int enable_hostname_completion __P((int));
106 #if defined (PROGRAMMABLE_COMPLETION)
107 extern int prog_completion_enabled;
110 #if defined (RESTRICTED_SHELL)
111 extern char *shell_name;
114 #if defined (DEBUGGER)
115 extern int debugging_mode;
118 static void shopt_error __P((char *));
120 static int set_shellopts_after_change __P((char *, int));
121 static int shopt_enable_hostname_completion __P((char *, int));
122 static int set_compatibility_level __P((char *, int));
124 #if defined (RESTRICTED_SHELL)
125 static int set_restricted_shell __P((char *, int));
128 #if defined (READLINE)
129 static int shopt_set_complete_direxpand __P((char *, int));
132 static int shopt_login_shell;
133 static int shopt_compat31;
134 static int shopt_compat32;
135 static int shopt_compat40;
136 static int shopt_compat41;
138 typedef int shopt_set_func_t __P((char *, int));
143 shopt_set_func_t *set_func;
145 { "autocd", &autocd, (shopt_set_func_t *)NULL },
146 { "cdable_vars", &cdable_vars, (shopt_set_func_t *)NULL },
147 { "cdspell", &cdspelling, (shopt_set_func_t *)NULL },
148 { "checkhash", &check_hashed_filenames, (shopt_set_func_t *)NULL },
149 #if defined (JOB_CONTROL)
150 { "checkjobs", &check_jobs_at_exit, (shopt_set_func_t *)NULL },
152 { "checkwinsize", &check_window_size, (shopt_set_func_t *)NULL },
153 #if defined (HISTORY)
154 { "cmdhist", &command_oriented_history, (shopt_set_func_t *)NULL },
156 { "compat31", &shopt_compat31, set_compatibility_level },
157 { "compat32", &shopt_compat32, set_compatibility_level },
158 { "compat40", &shopt_compat40, set_compatibility_level },
159 { "compat41", &shopt_compat41, set_compatibility_level },
160 #if defined (READLINE)
161 { "direxpand", &dircomplete_expand, shopt_set_complete_direxpand },
162 { "dirspell", &dircomplete_spelling, (shopt_set_func_t *)NULL },
164 { "dotglob", &glob_dot_filenames, (shopt_set_func_t *)NULL },
165 { "execfail", &no_exit_on_failed_exec, (shopt_set_func_t *)NULL },
166 { "expand_aliases", &expand_aliases, (shopt_set_func_t *)NULL },
167 #if defined (DEBUGGER)
168 { "extdebug", &debugging_mode, (shopt_set_func_t *)NULL },
170 #if defined (EXTENDED_GLOB)
171 { "extglob", &extended_glob, (shopt_set_func_t *)NULL },
173 { "extquote", &extended_quote, (shopt_set_func_t *)NULL },
174 { "failglob", &fail_glob_expansion, (shopt_set_func_t *)NULL },
175 #if defined (READLINE)
176 { "force_fignore", &force_fignore, (shopt_set_func_t *)NULL },
178 { "globstar", &glob_star, (shopt_set_func_t *)NULL },
179 { "gnu_errfmt", &gnu_error_format, (shopt_set_func_t *)NULL },
180 #if defined (HISTORY)
181 { "histappend", &force_append_history, (shopt_set_func_t *)NULL },
183 #if defined (READLINE)
184 { "histreedit", &history_reediting, (shopt_set_func_t *)NULL },
185 { "histverify", &hist_verify, (shopt_set_func_t *)NULL },
186 { "hostcomplete", &perform_hostname_completion, shopt_enable_hostname_completion },
188 { "huponexit", &hup_on_exit, (shopt_set_func_t *)NULL },
189 { "interactive_comments", &interactive_comments, set_shellopts_after_change },
190 { "lastpipe", &lastpipe_opt, (shopt_set_func_t *)NULL },
191 #if defined (HISTORY)
192 { "lithist", &literal_history, (shopt_set_func_t *)NULL },
194 { "login_shell", &shopt_login_shell, set_login_shell },
195 { "mailwarn", &mail_warning, (shopt_set_func_t *)NULL },
196 #if defined (READLINE)
197 { "no_empty_cmd_completion", &no_empty_command_completion, (shopt_set_func_t *)NULL },
199 { "nocaseglob", &glob_ignore_case, (shopt_set_func_t *)NULL },
200 { "nocasematch", &match_ignore_case, (shopt_set_func_t *)NULL },
201 { "nullglob", &allow_null_glob_expansion, (shopt_set_func_t *)NULL },
202 #if defined (PROGRAMMABLE_COMPLETION)
203 { "progcomp", &prog_completion_enabled, (shopt_set_func_t *)NULL },
205 { "promptvars", &promptvars, (shopt_set_func_t *)NULL },
206 #if defined (RESTRICTED_SHELL)
207 { "restricted_shell", &restricted_shell, set_restricted_shell },
209 { "shift_verbose", &print_shift_error, (shopt_set_func_t *)NULL },
210 { "sourcepath", &source_uses_path, (shopt_set_func_t *)NULL },
211 { "xpg_echo", &xpg_echo, (shopt_set_func_t *)NULL },
212 { (char *)0, (int *)0, (shopt_set_func_t *)NULL }
215 #define N_SHOPT_OPTIONS (sizeof (shopt_vars) / sizeof (shopt_vars[0]))
217 #define GET_SHOPT_OPTION_VALUE(i) (*shopt_vars[i].value)
219 static const char * const on = "on";
220 static const char * const off = "off";
222 static int find_shopt __P((char *));
223 static int toggle_shopts __P((int, WORD_LIST *, int));
224 static void print_shopt __P((char *, int, int));
225 static int list_shopts __P((WORD_LIST *, int));
226 static int list_some_shopts __P((int, int));
227 static int list_shopt_o_options __P((WORD_LIST *, int));
228 static int list_some_o_options __P((int, int));
229 static int set_shopt_o_options __P((int, WORD_LIST *, int));
241 int opt, flags, rval;
244 reset_internal_getopt ();
245 while ((opt = internal_getopt (list, "psuoq")) != -1)
271 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
273 builtin_error (_("cannot set and unset shell options simultaneously"));
274 return (EXECUTION_FAILURE);
277 rval = EXECUTION_SUCCESS;
278 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
279 rval = list_shopt_o_options (list, flags);
280 else if (list && (flags & OFLAG)) /* shopt -so args */
281 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
282 else if (flags & OFLAG) /* shopt -so */
283 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
284 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
285 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
286 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
287 rval = list_shopts (list, flags);
289 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
293 /* Reset the options managed by `shopt' to the values they would have at
296 reset_shopt_options ()
298 allow_null_glob_expansion = glob_dot_filenames = 0;
299 cdable_vars = mail_warning = 0;
300 no_exit_on_failed_exec = print_shift_error = 0;
301 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
303 source_uses_path = promptvars = 1;
305 #if defined (EXTENDED_GLOB)
309 #if defined (HISTORY)
310 literal_history = force_append_history = 0;
311 command_oriented_history = 1;
314 #if defined (READLINE)
315 hist_verify = history_reediting = 0;
316 perform_hostname_completion = 1;
319 shopt_login_shell = login_shell;
328 for (i = 0; shopt_vars[i].name; i++)
329 if (STREQ (name, shopt_vars[i].name))
338 builtin_error (_("%s: invalid shell option name"), s);
342 toggle_shopts (mode, list, quiet)
350 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
352 ind = find_shopt (l->word->word);
355 shopt_error (l->word->word);
356 rval = EXECUTION_FAILURE;
360 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
361 if (shopt_vars[ind].set_func)
362 (*shopt_vars[ind].set_func) (shopt_vars[ind].name, mode);
371 print_shopt (name, val, flags)
376 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
378 printf (OPTFMT, name, val ? on : off);
381 /* List the values of all or any of the `shopt' options. Returns 0 if
382 all were listed or all variables queried were on; 1 otherwise. */
384 list_shopts (list, flags)
393 for (i = 0; shopt_vars[i].name; i++)
395 val = *shopt_vars[i].value;
396 if ((flags & QFLAG) == 0)
397 print_shopt (shopt_vars[i].name, val, flags);
399 return (sh_chkwrite (EXECUTION_SUCCESS));
402 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
404 i = find_shopt (l->word->word);
407 shopt_error (l->word->word);
408 rval = EXECUTION_FAILURE;
411 val = *shopt_vars[i].value;
413 rval = EXECUTION_FAILURE;
414 if ((flags & QFLAG) == 0)
415 print_shopt (l->word->word, val, flags);
418 return (sh_chkwrite (rval));
422 list_some_shopts (mode, flags)
427 for (i = 0; shopt_vars[i].name; i++)
429 val = *shopt_vars[i].value;
430 if (((flags & QFLAG) == 0) && mode == val)
431 print_shopt (shopt_vars[i].name, val, flags);
433 return (sh_chkwrite (EXECUTION_SUCCESS));
437 list_shopt_o_options (list, flags)
446 if ((flags & QFLAG) == 0)
447 list_minus_o_opts (-1, (flags & PFLAG));
448 return (sh_chkwrite (EXECUTION_SUCCESS));
451 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
453 val = minus_o_option_value (l->word->word);
456 sh_invalidoptname (l->word->word);
457 rval = EXECUTION_FAILURE;
461 rval = EXECUTION_FAILURE;
462 if ((flags & QFLAG) == 0)
465 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
467 printf (OPTFMT, l->word->word, val ? on : off);
470 return (sh_chkwrite (rval));
474 list_some_o_options (mode, flags)
477 if ((flags & QFLAG) == 0)
478 list_minus_o_opts (mode, (flags & PFLAG));
479 return (sh_chkwrite (EXECUTION_SUCCESS));
483 set_shopt_o_options (mode, list, quiet)
491 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
493 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
494 rval = EXECUTION_FAILURE;
500 /* If we set or unset interactive_comments with shopt, make sure the
501 change is reflected in $SHELLOPTS. */
503 set_shellopts_after_change (option_name, mode)
512 shopt_enable_hostname_completion (option_name, mode)
516 return (enable_hostname_completion (mode));
520 set_compatibility_level (option_name, mode)
524 /* Need to change logic here as we add more compatibility levels */
526 /* First, check option_name so we can turn off other compat options when
528 if (mode && option_name[6] == '3' && option_name[7] == '1')
529 shopt_compat32 = shopt_compat40 = 0;
530 else if (mode && option_name[6] == '3' && option_name[7] == '2')
531 shopt_compat31 = shopt_compat40 = 0;
532 else if (mode && option_name[6] == '4' && option_name[7] == '0')
533 shopt_compat31 = shopt_compat32 = 0;
535 /* Then set shell_compatibility_level based on what remains */
537 shell_compatibility_level = 31;
538 else if (shopt_compat32)
539 shell_compatibility_level = 32;
540 else if (shopt_compat40)
541 shell_compatibility_level = 40;
543 shell_compatibility_level = DEFAULT_COMPAT_LEVEL;
547 #if defined (READLINE)
549 shopt_set_complete_direxpand (option_name, mode)
553 set_directory_hook ();
558 #if defined (RESTRICTED_SHELL)
559 /* Don't allow the value of restricted_shell to be modified. */
562 set_restricted_shell (option_name, mode)
566 static int save_restricted = -1;
568 if (save_restricted == -1)
569 save_restricted = shell_is_restricted (shell_name);
571 restricted_shell = save_restricted;
574 #endif /* RESTRICTED_SHELL */
576 /* Not static so shell.c can call it to initialize shopt_login_shell */
578 set_login_shell (option_name, mode)
582 shopt_login_shell = login_shell != 0;
592 n = sizeof (shopt_vars) / sizeof (shopt_vars[0]);
593 ret = strvec_create (n + 1);
594 for (i = 0; shopt_vars[i].name; i++)
595 ret[i] = savestring (shopt_vars[i].name);
596 ret[i] = (char *)NULL;
601 * External interface for other parts of the shell. NAME is a string option;
602 * MODE is 0 if we want to unset an option; 1 if we want to set an option.
603 * REUSABLE is 1 if we want to print output in a form that may be reused.
606 shopt_setopt (name, mode)
613 wl = add_string_to_list (name, (WORD_LIST *)NULL);
614 r = toggle_shopts (mode, wl, 0);
620 shopt_listopt (name, reusable)
627 return (list_shopts ((WORD_LIST *)NULL, reusable ? PFLAG : 0));
629 i = find_shopt (name);
633 return (EXECUTION_FAILURE);
636 print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0);
637 return (sh_chkwrite (EXECUTION_SUCCESS));
644 char tflag[N_SHOPT_OPTIONS];
645 int vsize, i, vptr, *ip, exported;
648 for (vsize = i = 0; shopt_vars[i].name; i++)
651 if (GET_SHOPT_OPTION_VALUE (i))
653 vsize += strlen (shopt_vars[i].name) + 1;
658 value = (char *)xmalloc (vsize + 1);
660 for (i = vptr = 0; shopt_vars[i].name; i++)
664 strcpy (value + vptr, shopt_vars[i].name);
665 vptr += strlen (shopt_vars[i].name);
671 vptr--; /* cut off trailing colon */
674 v = find_variable ("BASHOPTS");
676 /* Turn off the read-only attribute so we can bind the new value, and
677 note whether or not the variable was exported. */
680 VUNSETATTR (v, att_readonly);
681 exported = exported_p (v);
686 v = bind_variable ("BASHOPTS", value, 0);
688 /* Turn the read-only attribute back on, and turn off the export attribute
689 if it was set implicitly by mark_modified_vars and SHELLOPTS was not
690 exported before we bound the new value. */
691 VSETATTR (v, att_readonly);
692 if (mark_modified_vars && exported == 0 && exported_p (v))
693 VUNSETATTR (v, att_exported);
699 parse_bashopts (value)
706 while (vname = extract_colon_unit (value, &vptr))
708 ind = find_shopt (vname);
710 *shopt_vars[ind].value = 1;
716 initialize_bashopts (no_bashopts)
722 if (no_bashopts == 0)
724 var = find_variable ("BASHOPTS");
725 /* set up any shell options we may have inherited. */
726 if (var && imported_p (var))
728 temp = (array_p (var) || assoc_p (var)) ? (char *)NULL : savestring (value_cell (var));
731 parse_bashopts (temp);
737 /* Set up the $BASHOPTS variable. */