1 This file is shopt.def, from which is created shopt.c.
2 It implements the Bash `shopt' builtin.
4 Copyright (C) 1994 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $DOCNAME shopt_builtin
26 $FUNCTION shopt_builtin
27 $SHORT_DOC shopt [-pqsu] [-o long-option] optname [optname...]
28 Toggle the values of variables controlling optional behavior.
29 The -s flag means to enable (set) each OPTNAME; the -u flag
30 unsets each OPTNAME. The -q flag suppresses output; the exit
31 status indicates whether each OPTNAME is set or unset. The -o
32 option restricts the OPTNAMEs to those defined for use with
33 `set -o'. With no options, or with the -p option, a list of all
34 settable options is displayed, with an indication of whether or
40 #if defined (HAVE_UNISTD_H)
42 # include <sys/types.h>
52 #include "bashgetopt.h"
57 #define OPTFMT "%-15s\t%s\n"
59 extern int allow_null_glob_expansion, glob_dot_filenames;
60 extern int cdable_vars, mail_warning, source_uses_path;
61 extern int no_exit_on_failed_exec, print_shift_error;
62 extern int check_hashed_filenames, promptvars, interactive_comments;
63 extern int cdspelling, expand_aliases;
64 extern int check_window_size;
65 extern int glob_ignore_case;
66 extern int hup_on_exit;
68 #if defined (EXTENDED_GLOB)
69 extern int extended_glob;
73 extern int literal_history, command_oriented_history;
74 extern int force_append_history;
77 #if defined (READLINE)
78 extern int hist_verify, history_reediting, perform_hostname_completion;
79 extern void enable_hostname_completion ();
82 #if defined (RESTRICTED_SHELL)
83 extern int restricted_shell;
84 extern char *shell_name;
87 extern void set_shellopts ();
89 static int set_interactive_comments ();
91 #if defined (RESTRICTED_SHELL)
92 static int set_restricted_shell ();
100 { "cdable_vars", &cdable_vars, (Function *)NULL },
101 { "cdspell", &cdspelling, (Function *)NULL },
102 { "checkhash", &check_hashed_filenames, (Function *)NULL },
103 { "checkwinsize", &check_window_size, (Function *)NULL },
104 #if defined (HISTORY)
105 { "cmdhist", &command_oriented_history, (Function *)NULL },
107 { "dotglob", &glob_dot_filenames, (Function *)NULL },
108 { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
109 { "expand_aliases", &expand_aliases, (Function *)NULL },
110 #if defined (EXTENDED_GLOB)
111 { "extglob", &extended_glob, (Function *)NULL },
113 #if defined (READLINE)
114 { "histreedit", &history_reediting, (Function *)NULL },
116 #if defined (HISTORY)
117 { "histappend", &force_append_history, (Function *)NULL },
119 #if defined (READLINE)
120 { "histverify", &hist_verify, (Function *)NULL },
121 { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
123 { "huponexit", &hup_on_exit, (Function *)NULL },
124 { "interactive_comments", &interactive_comments, set_interactive_comments },
125 #if defined (HISTORY)
126 { "lithist", &literal_history, (Function *)NULL },
128 { "mailwarn", &mail_warning, (Function *)NULL },
129 { "nocaseglob", &glob_ignore_case, (Function *)NULL },
130 { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
131 { "promptvars", &promptvars, (Function *)NULL },
132 #if defined (RESTRICTED_SHELL)
133 { "restricted_shell", &restricted_shell, set_restricted_shell },
135 { "shift_verbose", &print_shift_error, (Function *)NULL },
136 { "sourcepath", &source_uses_path, (Function *)NULL },
137 { (char *)0, (int *)0, (Function *)NULL }
140 static char *on = "on";
141 static char *off = "off";
143 static int list_shopt_o_options ();
144 static int list_some_o_options (), list_some_shopts ();
145 static int toggle_shopts (), list_shopts (), set_shopt_o_options ();
157 int opt, flags, rval;
160 reset_internal_getopt ();
161 while ((opt = internal_getopt (list, "psuoq")) != -1)
187 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
189 builtin_error ("cannot set and unset shell options simultaneously");
190 return (EXECUTION_FAILURE);
193 rval = EXECUTION_SUCCESS;
194 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
195 rval = list_shopt_o_options (list, flags);
196 else if (list && (flags & OFLAG)) /* shopt -so args */
197 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
198 else if (flags & OFLAG) /* shopt -so */
199 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
200 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
201 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
202 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
203 rval = list_shopts (list, flags);
205 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
209 /* Reset the options managed by `shopt' to the values they would have at
212 reset_shopt_options ()
214 allow_null_glob_expansion = glob_dot_filenames = 0;
215 cdable_vars = mail_warning = 0;
216 no_exit_on_failed_exec = print_shift_error = 0;
217 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
219 source_uses_path = promptvars = 1;
221 #if defined (EXTENDED_GLOB)
225 #if defined (HISTORY)
226 literal_history = force_append_history = 0;
227 command_oriented_history = 1;
230 #if defined (READLINE)
231 hist_verify = history_reediting = 0;
232 perform_hostname_completion = 1;
242 for (i = 0; shopt_vars[i].name; i++)
243 if (STREQ (name, shopt_vars[i].name))
248 #define SHOPT_ERROR(str) builtin_error ("%s: unknown shell option name", str)
251 toggle_shopts (mode, list, quiet)
259 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
261 ind = find_shopt (l->word->word);
264 SHOPT_ERROR (l->word->word);
265 rval = EXECUTION_FAILURE;
269 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
270 if (shopt_vars[ind].set_func)
271 (*shopt_vars[ind].set_func) (mode);
278 print_shopt (name, val, flags)
283 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
285 printf (OPTFMT, name, val ? on : off);
288 /* List the values of all or any of the `shopt' options. Returns 0 if
289 all were listed or all variables queried were on; 1 otherwise. */
291 list_shopts (list, flags)
300 for (i = 0; shopt_vars[i].name; i++)
302 val = *shopt_vars[i].value;
303 if ((flags & QFLAG) == 0)
304 print_shopt (shopt_vars[i].name, val, flags);
306 return (EXECUTION_SUCCESS);
309 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
311 i = find_shopt (l->word->word);
314 SHOPT_ERROR (l->word->word);
315 rval = EXECUTION_FAILURE;
318 val = *shopt_vars[i].value;
320 rval = EXECUTION_FAILURE;
321 if ((flags & QFLAG) == 0)
322 print_shopt (l->word->word, val, flags);
328 list_some_shopts (mode, flags)
333 for (i = 0; shopt_vars[i].name; i++)
335 val = *shopt_vars[i].value;
336 if (((flags & QFLAG) == 0) && mode == val)
337 print_shopt (shopt_vars[i].name, val, flags);
339 return (EXECUTION_SUCCESS);
343 list_shopt_o_options (list, flags)
352 if ((flags & QFLAG) == 0)
353 list_minus_o_opts (-1, (flags & PFLAG));
354 return (EXECUTION_SUCCESS);
357 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
359 val = minus_o_option_value (l->word->word);
362 builtin_error ("%s: unknown option name", l->word->word);
363 rval = EXECUTION_FAILURE;
367 rval = EXECUTION_FAILURE;
368 if ((flags & QFLAG) == 0)
371 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
373 printf (OPTFMT, l->word->word, val ? on : off);
380 list_some_o_options (mode, flags)
383 if ((flags & QFLAG) == 0)
384 list_minus_o_opts (mode, (flags & PFLAG));
385 return (EXECUTION_SUCCESS);
389 set_shopt_o_options (mode, list, quiet)
397 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
399 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
400 rval = EXECUTION_FAILURE;
406 /* If we set or unset interactive_comments with shopt, make sure the
407 change is reflected in $SHELLOPTS. */
409 set_interactive_comments (mode)
416 #if defined (RESTRICTED_SHELL)
417 /* Don't allow the value of restricted_shell to be modified. */
420 set_restricted_shell (mode)
423 static int save_restricted = -1;
425 if (save_restricted == -1)
426 save_restricted = shell_is_restricted (shell_name);
428 restricted_shell = save_restricted;
431 #endif /* RESTRICTED_SHELL */