1 This file is shopt.def, from which is created shopt.c.
2 It implements the Bash `shopt' builtin.
4 Copyright (C) 1994-2005 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 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
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
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.
25 $FUNCTION shopt_builtin
26 $SHORT_DOC shopt [-pqsu] [-o long-option] optname [optname...]
27 Toggle the values of variables controlling optional behavior.
28 The -s flag means to enable (set) each OPTNAME; the -u flag
29 unsets each OPTNAME. The -q flag suppresses output; the exit
30 status indicates whether each OPTNAME is set or unset. The -o
31 option restricts the OPTNAMEs to those defined for use with
32 `set -o'. With no options, or with the -p option, a list of all
33 settable options is displayed, with an indication of whether or
39 #if defined (HAVE_UNISTD_H)
41 # include <sys/types.h>
48 #include "../bashintl.h"
53 #include "bashgetopt.h"
58 #define OPTFMT "%-15s\t%s\n"
60 extern int allow_null_glob_expansion, fail_glob_expansion, glob_dot_filenames;
61 extern int cdable_vars, mail_warning, source_uses_path;
62 extern int no_exit_on_failed_exec, print_shift_error;
63 extern int check_hashed_filenames, promptvars;
64 extern int cdspelling, expand_aliases;
65 extern int extended_quote;
66 extern int check_window_size;
67 extern int glob_ignore_case, match_ignore_case;
68 extern int hup_on_exit;
70 extern int gnu_error_format;
72 #if defined (EXTENDED_GLOB)
73 extern int extended_glob;
77 extern int literal_history, command_oriented_history;
78 extern int force_append_history;
81 #if defined (READLINE)
82 extern int hist_verify, history_reediting, perform_hostname_completion;
83 extern int no_empty_command_completion;
84 extern int force_fignore;
85 extern int enable_hostname_completion __P((int));
88 #if defined (PROGRAMMABLE_COMPLETION)
89 extern int prog_completion_enabled;
92 #if defined (RESTRICTED_SHELL)
93 extern char *shell_name;
96 #if defined (DEBUGGER)
97 extern int debugging_mode;
100 static void shopt_error __P((char *));
102 static int set_shellopts_after_change __P((int));
104 static int set_compatibility_level __P((int));
106 #if defined (RESTRICTED_SHELL)
107 static int set_restricted_shell __P((int));
110 static int shopt_login_shell;
111 static int shopt_compat31;
113 typedef int shopt_set_func_t __P((int));
118 shopt_set_func_t *set_func;
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 },
124 #if defined (HISTORY)
125 { "cmdhist", &command_oriented_history, (shopt_set_func_t *)NULL },
127 { "compat31", &shopt_compat31, set_compatibility_level },
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 },
131 #if defined (DEBUGGER)
132 { "extdebug", &debugging_mode, (shopt_set_func_t *)NULL },
134 #if defined (EXTENDED_GLOB)
135 { "extglob", &extended_glob, (shopt_set_func_t *)NULL },
137 { "extquote", &extended_quote, (shopt_set_func_t *)NULL },
138 { "failglob", &fail_glob_expansion, (shopt_set_func_t *)NULL },
139 #if defined (READLINE)
140 { "force_fignore", &force_fignore, (shopt_set_func_t *)NULL },
142 { "gnu_errfmt", &gnu_error_format, (shopt_set_func_t *)NULL },
143 #if defined (HISTORY)
144 { "histappend", &force_append_history, (shopt_set_func_t *)NULL },
146 #if defined (READLINE)
147 { "histreedit", &history_reediting, (shopt_set_func_t *)NULL },
148 { "histverify", &hist_verify, (shopt_set_func_t *)NULL },
149 { "hostcomplete", &perform_hostname_completion, enable_hostname_completion },
151 { "huponexit", &hup_on_exit, (shopt_set_func_t *)NULL },
152 { "interactive_comments", &interactive_comments, set_shellopts_after_change },
153 #if defined (HISTORY)
154 { "lithist", &literal_history, (shopt_set_func_t *)NULL },
156 { "login_shell", &shopt_login_shell, set_login_shell },
157 { "mailwarn", &mail_warning, (shopt_set_func_t *)NULL },
158 #if defined (READLINE)
159 { "no_empty_cmd_completion", &no_empty_command_completion, (shopt_set_func_t *)NULL },
161 { "nocaseglob", &glob_ignore_case, (shopt_set_func_t *)NULL },
162 { "nocasematch", &match_ignore_case, (shopt_set_func_t *)NULL },
163 { "nullglob", &allow_null_glob_expansion, (shopt_set_func_t *)NULL },
164 #if defined (PROGRAMMABLE_COMPLETION)
165 { "progcomp", &prog_completion_enabled, (shopt_set_func_t *)NULL },
167 { "promptvars", &promptvars, (shopt_set_func_t *)NULL },
168 #if defined (RESTRICTED_SHELL)
169 { "restricted_shell", &restricted_shell, set_restricted_shell },
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 }
177 static char *on = "on";
178 static char *off = "off";
180 static int find_shopt __P((char *));
181 static int toggle_shopts __P((int, WORD_LIST *, int));
182 static void print_shopt __P((char *, int, int));
183 static int list_shopts __P((WORD_LIST *, int));
184 static int list_some_shopts __P((int, int));
185 static int list_shopt_o_options __P((WORD_LIST *, int));
186 static int list_some_o_options __P((int, int));
187 static int set_shopt_o_options __P((int, WORD_LIST *, int));
199 int opt, flags, rval;
202 reset_internal_getopt ();
203 while ((opt = internal_getopt (list, "psuoq")) != -1)
229 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
231 builtin_error (_("cannot set and unset shell options simultaneously"));
232 return (EXECUTION_FAILURE);
235 rval = EXECUTION_SUCCESS;
236 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
237 rval = list_shopt_o_options (list, flags);
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 */
241 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
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] */
245 rval = list_shopts (list, flags);
247 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
251 /* Reset the options managed by `shopt' to the values they would have at
254 reset_shopt_options ()
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;
261 source_uses_path = promptvars = 1;
263 #if defined (EXTENDED_GLOB)
267 #if defined (HISTORY)
268 literal_history = force_append_history = 0;
269 command_oriented_history = 1;
272 #if defined (READLINE)
273 hist_verify = history_reediting = 0;
274 perform_hostname_completion = 1;
277 shopt_login_shell = login_shell;
286 for (i = 0; shopt_vars[i].name; i++)
287 if (STREQ (name, shopt_vars[i].name))
296 builtin_error (_("%s: invalid shell option name"), s);
300 toggle_shopts (mode, list, quiet)
308 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
310 ind = find_shopt (l->word->word);
313 shopt_error (l->word->word);
314 rval = EXECUTION_FAILURE;
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);
327 print_shopt (name, val, flags)
332 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
334 printf (OPTFMT, name, val ? on : off);
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. */
340 list_shopts (list, flags)
349 for (i = 0; shopt_vars[i].name; i++)
351 val = *shopt_vars[i].value;
352 if ((flags & QFLAG) == 0)
353 print_shopt (shopt_vars[i].name, val, flags);
355 return (EXECUTION_SUCCESS);
358 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
360 i = find_shopt (l->word->word);
363 shopt_error (l->word->word);
364 rval = EXECUTION_FAILURE;
367 val = *shopt_vars[i].value;
369 rval = EXECUTION_FAILURE;
370 if ((flags & QFLAG) == 0)
371 print_shopt (l->word->word, val, flags);
378 list_some_shopts (mode, flags)
383 for (i = 0; shopt_vars[i].name; i++)
385 val = *shopt_vars[i].value;
386 if (((flags & QFLAG) == 0) && mode == val)
387 print_shopt (shopt_vars[i].name, val, flags);
389 return (EXECUTION_SUCCESS);
393 list_shopt_o_options (list, flags)
402 if ((flags & QFLAG) == 0)
403 list_minus_o_opts (-1, (flags & PFLAG));
404 return (EXECUTION_SUCCESS);
407 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
409 val = minus_o_option_value (l->word->word);
412 sh_invalidoptname (l->word->word);
413 rval = EXECUTION_FAILURE;
417 rval = EXECUTION_FAILURE;
418 if ((flags & QFLAG) == 0)
421 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
423 printf (OPTFMT, l->word->word, val ? on : off);
430 list_some_o_options (mode, flags)
433 if ((flags & QFLAG) == 0)
434 list_minus_o_opts (mode, (flags & PFLAG));
435 return (EXECUTION_SUCCESS);
439 set_shopt_o_options (mode, list, quiet)
447 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
449 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
450 rval = EXECUTION_FAILURE;
456 /* If we set or unset interactive_comments with shopt, make sure the
457 change is reflected in $SHELLOPTS. */
459 set_shellopts_after_change (mode)
467 set_compatibility_level (mode)
470 /* Need to change logic here as we add more compatibility levels */
472 shell_compatibility_level = 31;
474 shell_compatibility_level = 32;
478 #if defined (RESTRICTED_SHELL)
479 /* Don't allow the value of restricted_shell to be modified. */
482 set_restricted_shell (mode)
485 static int save_restricted = -1;
487 if (save_restricted == -1)
488 save_restricted = shell_is_restricted (shell_name);
490 restricted_shell = save_restricted;
493 #endif /* RESTRICTED_SHELL */
495 /* Not static so shell.c can call it to initialize shopt_login_shell */
497 set_login_shell (mode)
500 shopt_login_shell = login_shell != 0;
510 n = sizeof (shopt_vars) / sizeof (shopt_vars[0]);
511 ret = strvec_create (n + 1);
512 for (i = 0; shopt_vars[i].name; i++)
513 ret[i] = savestring (shopt_vars[i].name);
514 ret[i] = (char *)NULL;
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.
524 shopt_setopt (name, mode)
531 wl = add_string_to_list (name, (WORD_LIST *)NULL);
532 r = toggle_shopts (mode, wl, 0);
538 shopt_listopt (name, reusable)
545 return (list_shopts ((WORD_LIST *)NULL, reusable ? PFLAG : 0));
547 i = find_shopt (name);
551 return (EXECUTION_FAILURE);
554 print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0);
555 return (EXECUTION_SUCCESS);