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)
49 #include "bashgetopt.h"
54 #define OPTFMT "%-15s\t%s\n"
56 extern int allow_null_glob_expansion, glob_dot_filenames;
57 extern int cdable_vars, mail_warning, source_uses_path;
58 extern int no_exit_on_failed_exec, print_shift_error;
59 extern int check_hashed_filenames, promptvars, interactive_comments;
60 extern int cdspelling, expand_aliases;
61 extern int check_window_size;
64 extern int literal_history, command_oriented_history;
65 extern int force_append_history;
68 #if defined (READLINE)
69 extern int hist_verify, history_reediting, perform_hostname_completion;
70 extern void enable_hostname_completion ();
73 extern void set_shellopts ();
75 static int set_interactive_comments ();
82 { "cdable_vars", &cdable_vars, (Function *)NULL },
83 { "cdspell", &cdspelling, (Function *)NULL },
84 { "checkhash", &check_hashed_filenames, (Function *)NULL },
85 { "checkwinsize", &check_window_size, (Function *)NULL },
87 { "cmdhist", &command_oriented_history, (Function *)NULL },
89 { "dotglob", &glob_dot_filenames, (Function *)NULL },
90 { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
91 { "expand_aliases", &expand_aliases, (Function *)NULL },
92 #if defined (READLINE)
93 { "histreedit", &history_reediting, (Function *)NULL },
96 { "histappend", &force_append_history, (Function *)NULL },
98 #if defined (READLINE)
99 { "histverify", &hist_verify, (Function *)NULL },
100 { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
102 { "interactive_comments", &interactive_comments, set_interactive_comments },
103 #if defined (HISTORY)
104 { "lithist", &literal_history, (Function *)NULL },
106 { "mailwarn", &mail_warning, (Function *)NULL },
107 { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
108 { "promptvars", &promptvars, (Function *)NULL },
109 { "shift_verbose", &print_shift_error, (Function *)NULL },
110 { "sourcepath", &source_uses_path, (Function *)NULL },
111 { (char *)0, (int *)0, (Function *)NULL }
114 static char *on = "on";
115 static char *off = "off";
117 static int list_shopt_o_options ();
118 static int list_some_o_options (), list_some_shopts ();
119 static int toggle_shopts (), list_shopts (), set_shopt_o_options ();
131 int opt, flags, rval;
134 reset_internal_getopt ();
135 while ((opt = internal_getopt (list, "psuoq")) != -1)
161 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
163 builtin_error ("cannot set and unset shell options simultaneously");
164 return (EXECUTION_FAILURE);
167 rval = EXECUTION_SUCCESS;
168 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
169 rval = list_shopt_o_options (list, flags & QFLAG);
170 else if (list && (flags & OFLAG)) /* shopt -so args */
171 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
172 else if (flags & OFLAG) /* shopt -so */
173 rval = list_some_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, flags & QFLAG);
174 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
175 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
176 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
177 rval = list_shopts (list, flags & QFLAG);
179 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags & QFLAG);
183 /* Reset the options managed by `shopt' to the values they would have at
186 reset_shopt_options ()
188 allow_null_glob_expansion = glob_dot_filenames = 0;
189 cdable_vars = mail_warning = 0;
190 no_exit_on_failed_exec = print_shift_error = 0;
191 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
193 source_uses_path = promptvars = 1;
195 #if defined (HISTORY)
196 literal_history = force_append_history = 0;
197 command_oriented_history = 1;
200 #if defined (READLINE)
201 hist_verify = history_reediting = 0;
202 perform_hostname_completion = 1;
212 for (i = 0; shopt_vars[i].name; i++)
213 if (STREQ (name, shopt_vars[i].name))
218 #define SHOPT_ERROR(str) builtin_error ("%s: unknown shell option name", str)
221 toggle_shopts (mode, list, quiet)
229 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
231 ind = find_shopt (l->word->word);
234 SHOPT_ERROR (l->word->word);
235 rval = EXECUTION_FAILURE;
239 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
240 if (shopt_vars[ind].set_func)
241 (*shopt_vars[ind].set_func) (mode);
247 /* List the values of all or any of the `shopt' options. Returns 0 if
248 all were listed or all variables queried were on; 1 otherwise. */
250 list_shopts (list, quiet)
259 for (i = 0; shopt_vars[i].name; i++)
261 val = *shopt_vars[i].value;
263 printf (OPTFMT, shopt_vars[i].name, val ? on : off);
265 return (EXECUTION_SUCCESS);
268 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
270 i = find_shopt (l->word->word);
273 SHOPT_ERROR (l->word->word);
274 rval = EXECUTION_FAILURE;
277 val = *shopt_vars[i].value;
279 rval = EXECUTION_FAILURE;
281 printf (OPTFMT, l->word->word, val ? on : off);
287 list_some_shopts (mode, quiet)
292 for (i = 0; shopt_vars[i].name; i++)
294 val = *shopt_vars[i].value;
295 if (quiet == 0 && mode == val)
296 printf (OPTFMT, shopt_vars[i].name, val ? on : off);
298 return (EXECUTION_SUCCESS);
302 list_shopt_o_options (list, quiet)
312 list_minus_o_opts (-1);
313 return (EXECUTION_SUCCESS);
316 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
318 val = minus_o_option_value (l->word->word);
321 builtin_error ("%s: unknown option name", l->word->word);
322 rval = EXECUTION_FAILURE;
326 rval = EXECUTION_FAILURE;
328 printf (OPTFMT, l->word->word, val ? "on" : "off");
334 list_some_o_options (mode, quiet)
338 list_minus_o_opts (mode);
339 return (EXECUTION_SUCCESS);
343 set_shopt_o_options (mode, list, quiet)
351 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
353 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
354 rval = EXECUTION_FAILURE;
360 /* If we set or unset interactive_comments with shopt, make sure the
361 change is reflected in $SHELLOPTS. */
363 set_interactive_comments (mode)