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 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 $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;
69 #if defined (EXTENDED_GLOB)
70 extern int extended_glob;
74 extern int literal_history, command_oriented_history;
75 extern int force_append_history;
78 #if defined (READLINE)
79 extern int hist_verify, history_reediting, perform_hostname_completion;
80 extern int no_empty_command_completion;
81 extern void enable_hostname_completion ();
84 #if defined (PROGRAMMABLE_COMPLETION)
85 extern int prog_completion_enabled;
88 #if defined (RESTRICTED_SHELL)
89 extern int restricted_shell;
90 extern char *shell_name;
93 extern void set_shellopts ();
95 static int set_interactive_comments ();
97 #if defined (RESTRICTED_SHELL)
98 static int set_restricted_shell ();
106 { "cdable_vars", &cdable_vars, (Function *)NULL },
107 { "cdspell", &cdspelling, (Function *)NULL },
108 { "checkhash", &check_hashed_filenames, (Function *)NULL },
109 { "checkwinsize", &check_window_size, (Function *)NULL },
110 #if defined (HISTORY)
111 { "cmdhist", &command_oriented_history, (Function *)NULL },
113 { "dotglob", &glob_dot_filenames, (Function *)NULL },
114 { "execfail", &no_exit_on_failed_exec, (Function *)NULL },
115 { "expand_aliases", &expand_aliases, (Function *)NULL },
116 #if defined (EXTENDED_GLOB)
117 { "extglob", &extended_glob, (Function *)NULL },
119 #if defined (READLINE)
120 { "histreedit", &history_reediting, (Function *)NULL },
122 #if defined (HISTORY)
123 { "histappend", &force_append_history, (Function *)NULL },
125 #if defined (READLINE)
126 { "histverify", &hist_verify, (Function *)NULL },
127 { "hostcomplete", &perform_hostname_completion, (Function *)enable_hostname_completion },
129 { "huponexit", &hup_on_exit, (Function *)NULL },
130 { "interactive_comments", &interactive_comments, set_interactive_comments },
131 #if defined (HISTORY)
132 { "lithist", &literal_history, (Function *)NULL },
134 { "mailwarn", &mail_warning, (Function *)NULL },
135 #if defined (READLINE)
136 { "no_empty_cmd_completion", &no_empty_command_completion, (Function *)NULL },
138 { "nocaseglob", &glob_ignore_case, (Function *)NULL },
139 { "nullglob", &allow_null_glob_expansion, (Function *)NULL },
140 #if defined (PROGRAMMABLE_COMPLETION)
141 { "progcomp", &prog_completion_enabled, (Function *)NULL },
143 { "promptvars", &promptvars, (Function *)NULL },
144 #if defined (RESTRICTED_SHELL)
145 { "restricted_shell", &restricted_shell, set_restricted_shell },
147 { "shift_verbose", &print_shift_error, (Function *)NULL },
148 { "sourcepath", &source_uses_path, (Function *)NULL },
149 { "xpg_echo", &xpg_echo, (Function *)NULL },
150 { (char *)0, (int *)0, (Function *)NULL }
153 static char *on = "on";
154 static char *off = "off";
156 static int list_shopt_o_options ();
157 static int list_some_o_options (), list_some_shopts ();
158 static int toggle_shopts (), list_shopts (), set_shopt_o_options ();
170 int opt, flags, rval;
173 reset_internal_getopt ();
174 while ((opt = internal_getopt (list, "psuoq")) != -1)
200 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
202 builtin_error ("cannot set and unset shell options simultaneously");
203 return (EXECUTION_FAILURE);
206 rval = EXECUTION_SUCCESS;
207 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
208 rval = list_shopt_o_options (list, flags);
209 else if (list && (flags & OFLAG)) /* shopt -so args */
210 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
211 else if (flags & OFLAG) /* shopt -so */
212 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
213 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
214 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
215 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
216 rval = list_shopts (list, flags);
218 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
222 /* Reset the options managed by `shopt' to the values they would have at
225 reset_shopt_options ()
227 allow_null_glob_expansion = glob_dot_filenames = 0;
228 cdable_vars = mail_warning = 0;
229 no_exit_on_failed_exec = print_shift_error = 0;
230 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
232 source_uses_path = promptvars = 1;
234 #if defined (EXTENDED_GLOB)
238 #if defined (HISTORY)
239 literal_history = force_append_history = 0;
240 command_oriented_history = 1;
243 #if defined (READLINE)
244 hist_verify = history_reediting = 0;
245 perform_hostname_completion = 1;
255 for (i = 0; shopt_vars[i].name; i++)
256 if (STREQ (name, shopt_vars[i].name))
261 #define SHOPT_ERROR(str) builtin_error ("%s: unknown shell option name", str)
264 toggle_shopts (mode, list, quiet)
272 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
274 ind = find_shopt (l->word->word);
277 SHOPT_ERROR (l->word->word);
278 rval = EXECUTION_FAILURE;
282 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
283 if (shopt_vars[ind].set_func)
284 (*shopt_vars[ind].set_func) (mode);
291 print_shopt (name, val, flags)
296 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
298 printf (OPTFMT, name, val ? on : off);
301 /* List the values of all or any of the `shopt' options. Returns 0 if
302 all were listed or all variables queried were on; 1 otherwise. */
304 list_shopts (list, flags)
313 for (i = 0; shopt_vars[i].name; i++)
315 val = *shopt_vars[i].value;
316 if ((flags & QFLAG) == 0)
317 print_shopt (shopt_vars[i].name, val, flags);
319 return (EXECUTION_SUCCESS);
322 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
324 i = find_shopt (l->word->word);
327 SHOPT_ERROR (l->word->word);
328 rval = EXECUTION_FAILURE;
331 val = *shopt_vars[i].value;
333 rval = EXECUTION_FAILURE;
334 if ((flags & QFLAG) == 0)
335 print_shopt (l->word->word, val, flags);
342 list_some_shopts (mode, flags)
347 for (i = 0; shopt_vars[i].name; i++)
349 val = *shopt_vars[i].value;
350 if (((flags & QFLAG) == 0) && mode == val)
351 print_shopt (shopt_vars[i].name, val, flags);
353 return (EXECUTION_SUCCESS);
357 list_shopt_o_options (list, flags)
366 if ((flags & QFLAG) == 0)
367 list_minus_o_opts (-1, (flags & PFLAG));
368 return (EXECUTION_SUCCESS);
371 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
373 val = minus_o_option_value (l->word->word);
376 builtin_error ("%s: unknown option name", l->word->word);
377 rval = EXECUTION_FAILURE;
381 rval = EXECUTION_FAILURE;
382 if ((flags & QFLAG) == 0)
385 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
387 printf (OPTFMT, l->word->word, val ? on : off);
394 list_some_o_options (mode, flags)
397 if ((flags & QFLAG) == 0)
398 list_minus_o_opts (mode, (flags & PFLAG));
399 return (EXECUTION_SUCCESS);
403 set_shopt_o_options (mode, list, quiet)
411 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
413 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
414 rval = EXECUTION_FAILURE;
420 /* If we set or unset interactive_comments with shopt, make sure the
421 change is reflected in $SHELLOPTS. */
423 set_interactive_comments (mode)
430 #if defined (RESTRICTED_SHELL)
431 /* Don't allow the value of restricted_shell to be modified. */
434 set_restricted_shell (mode)
437 static int save_restricted = -1;
439 if (save_restricted == -1)
440 save_restricted = shell_is_restricted (shell_name);
442 restricted_shell = save_restricted;
445 #endif /* RESTRICTED_SHELL */
453 n = sizeof (shopt_vars) / sizeof (shopt_vars[0]);
454 ret = alloc_array (n + 1);
455 for (i = 0; shopt_vars[i].name; i++)
456 ret[i] = savestring (shopt_vars[i].name);
457 ret[i] = (char *)NULL;