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 extern void set_shellopts ();
84 static int set_interactive_comments ();
91 { "cdable_vars", &cdable_vars, (Function *)NULL },
92 { "cdspell", &cdspelling, (Function *)NULL },
93 { "checkhash", &check_hashed_filenames, (Function *)NULL },
94 { "checkwinsize", &check_window_size, (Function *)NULL },
96 { "cmdhist", &command_oriented_history, (Function *)NULL },
98 { "dotglob", &glob_dot_filenames, (Function *)NULL },
99 { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
100 { "expand_aliases", &expand_aliases, (Function *)NULL },
101 #if defined (EXTENDED_GLOB)
102 { "extglob", &extended_glob, (Function *)NULL },
104 #if defined (READLINE)
105 { "histreedit", &history_reediting, (Function *)NULL },
107 #if defined (HISTORY)
108 { "histappend", &force_append_history, (Function *)NULL },
110 #if defined (READLINE)
111 { "histverify", &hist_verify, (Function *)NULL },
112 { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
114 { "huponexit", &hup_on_exit, (Function *)NULL },
115 { "interactive_comments", &interactive_comments, set_interactive_comments },
116 #if defined (HISTORY)
117 { "lithist", &literal_history, (Function *)NULL },
119 { "mailwarn", &mail_warning, (Function *)NULL },
120 { "nocaseglob", &glob_ignore_case, (Function *)NULL },
121 { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
122 { "promptvars", &promptvars, (Function *)NULL },
123 { "shift_verbose", &print_shift_error, (Function *)NULL },
124 { "sourcepath", &source_uses_path, (Function *)NULL },
125 { (char *)0, (int *)0, (Function *)NULL }
128 static char *on = "on";
129 static char *off = "off";
131 static int list_shopt_o_options ();
132 static int list_some_o_options (), list_some_shopts ();
133 static int toggle_shopts (), list_shopts (), set_shopt_o_options ();
145 int opt, flags, rval;
148 reset_internal_getopt ();
149 while ((opt = internal_getopt (list, "psuoq")) != -1)
175 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
177 builtin_error ("cannot set and unset shell options simultaneously");
178 return (EXECUTION_FAILURE);
181 rval = EXECUTION_SUCCESS;
182 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
183 rval = list_shopt_o_options (list, flags);
184 else if (list && (flags & OFLAG)) /* shopt -so args */
185 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
186 else if (flags & OFLAG) /* shopt -so */
187 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
188 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
189 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
190 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
191 rval = list_shopts (list, flags);
193 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
197 /* Reset the options managed by `shopt' to the values they would have at
200 reset_shopt_options ()
202 allow_null_glob_expansion = glob_dot_filenames = 0;
203 cdable_vars = mail_warning = 0;
204 no_exit_on_failed_exec = print_shift_error = 0;
205 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
207 source_uses_path = promptvars = 1;
209 #if defined (EXTENDED_GLOB)
213 #if defined (HISTORY)
214 literal_history = force_append_history = 0;
215 command_oriented_history = 1;
218 #if defined (READLINE)
219 hist_verify = history_reediting = 0;
220 perform_hostname_completion = 1;
230 for (i = 0; shopt_vars[i].name; i++)
231 if (STREQ (name, shopt_vars[i].name))
236 #define SHOPT_ERROR(str) builtin_error ("%s: unknown shell option name", str)
239 toggle_shopts (mode, list, quiet)
247 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
249 ind = find_shopt (l->word->word);
252 SHOPT_ERROR (l->word->word);
253 rval = EXECUTION_FAILURE;
257 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
258 if (shopt_vars[ind].set_func)
259 (*shopt_vars[ind].set_func) (mode);
266 print_shopt (name, val, flags)
271 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
273 printf (OPTFMT, name, val ? on : off);
276 /* List the values of all or any of the `shopt' options. Returns 0 if
277 all were listed or all variables queried were on; 1 otherwise. */
279 list_shopts (list, flags)
288 for (i = 0; shopt_vars[i].name; i++)
290 val = *shopt_vars[i].value;
291 if ((flags & QFLAG) == 0)
292 print_shopt (shopt_vars[i].name, val, flags);
294 return (EXECUTION_SUCCESS);
297 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
299 i = find_shopt (l->word->word);
302 SHOPT_ERROR (l->word->word);
303 rval = EXECUTION_FAILURE;
306 val = *shopt_vars[i].value;
308 rval = EXECUTION_FAILURE;
309 if ((flags & QFLAG) == 0)
310 print_shopt (l->word->word, val, flags);
316 list_some_shopts (mode, flags)
321 for (i = 0; shopt_vars[i].name; i++)
323 val = *shopt_vars[i].value;
324 if (((flags & QFLAG) == 0) && mode == val)
325 print_shopt (shopt_vars[i].name, val, flags);
327 return (EXECUTION_SUCCESS);
331 list_shopt_o_options (list, flags)
340 if ((flags & QFLAG) == 0)
341 list_minus_o_opts (-1, (flags & PFLAG));
342 return (EXECUTION_SUCCESS);
345 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
347 val = minus_o_option_value (l->word->word);
350 builtin_error ("%s: unknown option name", l->word->word);
351 rval = EXECUTION_FAILURE;
355 rval = EXECUTION_FAILURE;
356 if ((flags & QFLAG) == 0)
359 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
361 printf (OPTFMT, l->word->word, val ? on : off);
368 list_some_o_options (mode, flags)
371 if ((flags & QFLAG) == 0)
372 list_minus_o_opts (mode, (flags & PFLAG));
373 return (EXECUTION_SUCCESS);
377 set_shopt_o_options (mode, list, quiet)
385 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
387 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
388 rval = EXECUTION_FAILURE;
394 /* If we set or unset interactive_comments with shopt, make sure the
395 change is reflected in $SHELLOPTS. */
397 set_interactive_comments (mode)