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 hist_verify, literal_history, command_oriented_history;
65 extern int force_append_history;
68 #if defined (READLINE)
69 extern int history_reediting, perform_hostname_completion;
70 extern void enable_hostname_completion ();
73 static int set_interactive_comments ();
80 { "cdable_vars", &cdable_vars, (Function *)NULL },
81 { "cdspell", &cdspelling, (Function *)NULL },
82 { "checkhash", &check_hashed_filenames, (Function *)NULL },
83 { "checkwinsize", &check_window_size, (Function *)NULL },
85 { "cmdhist", &command_oriented_history, (Function *)NULL },
87 { "dotglob", &glob_dot_filenames, (Function *)NULL },
88 { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
89 { "expand_aliases", &expand_aliases, (Function *)NULL },
90 #if defined (READLINE)
91 { "histreedit", &history_reediting, (Function *)NULL },
94 { "histappend", &force_append_history, (Function *)NULL },
95 { "histverify", &hist_verify, (Function *)NULL },
97 #if defined (READLINE)
98 { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
100 { "interactive_comments", &interactive_comments, set_interactive_comments },
101 #if defined (HISTORY)
102 { "lithist", &literal_history, (Function *)NULL },
104 { "mailwarn", &mail_warning, (Function *)NULL },
105 { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
106 { "promptvars", &promptvars, (Function *)NULL },
107 { "shift_verbose", &print_shift_error, (Function *)NULL },
108 { "sourcepath", &source_uses_path, (Function *)NULL },
109 { (char *)0, (int *)0, (Function *)NULL }
112 static char *on = "on";
113 static char *off = "off";
115 static int list_shopt_o_options ();
116 static int list_some_o_options (), list_some_shopts ();
117 static int toggle_shopts (), list_shopts (), set_shopt_o_options ();
129 int opt, flags, rval;
132 reset_internal_getopt ();
133 while ((opt = internal_getopt (list, "psuoq")) != -1)
159 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
161 builtin_error ("cannot set and unset shell options simultaneously");
162 return (EXECUTION_FAILURE);
165 rval = EXECUTION_SUCCESS;
166 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
167 rval = list_shopt_o_options (list, flags & QFLAG);
168 else if (list && (flags & OFLAG)) /* shopt -so args */
169 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
170 else if (flags & OFLAG) /* shopt -so */
171 rval = list_some_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, flags & QFLAG);
172 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
173 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
174 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
175 rval = list_shopts (list, flags & QFLAG);
177 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags & QFLAG);
187 for (i = 0; shopt_vars[i].name; i++)
188 if (STREQ (name, shopt_vars[i].name))
193 #define SHOPT_ERROR(str) builtin_error ("%s: unknown shell option name", str)
196 toggle_shopts (mode, list, quiet)
204 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
206 ind = find_shopt (l->word->word);
209 SHOPT_ERROR (l->word->word);
210 rval = EXECUTION_FAILURE;
214 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
215 if (shopt_vars[ind].set_func)
216 (*shopt_vars[ind].set_func) (mode);
222 /* List the values of all or any of the `shopt' options. Returns 0 if
223 all were listed or all variables queried were on; 1 otherwise. */
225 list_shopts (list, quiet)
234 for (i = 0; shopt_vars[i].name; i++)
236 val = *shopt_vars[i].value;
238 printf (OPTFMT, shopt_vars[i].name, val ? on : off);
240 return (EXECUTION_SUCCESS);
243 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
245 i = find_shopt (l->word->word);
248 SHOPT_ERROR (l->word->word);
249 rval = EXECUTION_FAILURE;
252 val = *shopt_vars[i].value;
254 rval = EXECUTION_FAILURE;
256 printf (OPTFMT, l->word->word, val ? on : off);
262 list_some_shopts (mode, quiet)
267 for (i = 0; shopt_vars[i].name; i++)
269 val = *shopt_vars[i].value;
270 if (quiet == 0 && mode == val)
271 printf (OPTFMT, shopt_vars[i].name, val ? on : off);
273 return (EXECUTION_SUCCESS);
277 list_shopt_o_options (list, quiet)
287 list_minus_o_opts (-1);
288 return (EXECUTION_SUCCESS);
291 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
293 val = minus_o_option_value (l->word->word);
296 builtin_error ("%s: unknown option name", l->word->word);
297 rval = EXECUTION_FAILURE;
301 rval = EXECUTION_FAILURE;
303 printf (OPTFMT, l->word->word, val ? "on" : "off");
309 list_some_o_options (mode, quiet)
313 list_minus_o_opts (mode);
314 return (EXECUTION_SUCCESS);
318 set_shopt_o_options (mode, list, quiet)
326 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
328 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
329 rval = EXECUTION_FAILURE;
335 /* If we set or unset interactive_comments with shopt, make sure the
336 change is reflected in $SHELLOPTS. */
338 set_interactive_comments (mode)