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 int enable_hostname_completion __P((int));
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 static int set_interactive_comments __P((int));
95 #if defined (RESTRICTED_SHELL)
96 static int set_restricted_shell __P((int));
99 static int shopt_login_shell;
101 typedef int shopt_set_func_t __P((int));
106 shopt_set_func_t *set_func;
108 { "cdable_vars", &cdable_vars, (shopt_set_func_t *)NULL },
109 { "cdspell", &cdspelling, (shopt_set_func_t *)NULL },
110 { "checkhash", &check_hashed_filenames, (shopt_set_func_t *)NULL },
111 { "checkwinsize", &check_window_size, (shopt_set_func_t *)NULL },
112 #if defined (HISTORY)
113 { "cmdhist", &command_oriented_history, (shopt_set_func_t *)NULL },
115 { "dotglob", &glob_dot_filenames, (shopt_set_func_t *)NULL },
116 { "execfail", &no_exit_on_failed_exec, (shopt_set_func_t *)NULL },
117 { "expand_aliases", &expand_aliases, (shopt_set_func_t *)NULL },
118 #if defined (EXTENDED_GLOB)
119 { "extglob", &extended_glob, (shopt_set_func_t *)NULL },
121 #if defined (READLINE)
122 { "histreedit", &history_reediting, (shopt_set_func_t *)NULL },
124 #if defined (HISTORY)
125 { "histappend", &force_append_history, (shopt_set_func_t *)NULL },
127 #if defined (READLINE)
128 { "histverify", &hist_verify, (shopt_set_func_t *)NULL },
129 { "hostcomplete", &perform_hostname_completion, enable_hostname_completion },
131 { "huponexit", &hup_on_exit, (shopt_set_func_t *)NULL },
132 { "interactive_comments", &interactive_comments, set_interactive_comments },
133 #if defined (HISTORY)
134 { "lithist", &literal_history, (shopt_set_func_t *)NULL },
136 { "login_shell", &shopt_login_shell, set_login_shell },
137 { "mailwarn", &mail_warning, (shopt_set_func_t *)NULL },
138 #if defined (READLINE)
139 { "no_empty_cmd_completion", &no_empty_command_completion, (shopt_set_func_t *)NULL },
141 { "nocaseglob", &glob_ignore_case, (shopt_set_func_t *)NULL },
142 { "nullglob", &allow_null_glob_expansion, (shopt_set_func_t *)NULL },
143 #if defined (PROGRAMMABLE_COMPLETION)
144 { "progcomp", &prog_completion_enabled, (shopt_set_func_t *)NULL },
146 { "promptvars", &promptvars, (shopt_set_func_t *)NULL },
147 #if defined (RESTRICTED_SHELL)
148 { "restricted_shell", &restricted_shell, set_restricted_shell },
150 { "shift_verbose", &print_shift_error, (shopt_set_func_t *)NULL },
151 { "sourcepath", &source_uses_path, (shopt_set_func_t *)NULL },
152 { "xpg_echo", &xpg_echo, (shopt_set_func_t *)NULL },
153 { (char *)0, (int *)0, (shopt_set_func_t *)NULL }
156 static char *on = "on";
157 static char *off = "off";
159 static int find_shopt __P((char *));
160 static int toggle_shopts __P((int, WORD_LIST *, int));
161 static void print_shopt __P((char *, int, int));
162 static int list_shopts __P((WORD_LIST *, int));
163 static int list_some_shopts __P((int, int));
164 static int list_shopt_o_options __P((WORD_LIST *, int));
165 static int list_some_o_options __P((int, int));
166 static int set_shopt_o_options __P((int, WORD_LIST *, int));
178 int opt, flags, rval;
181 reset_internal_getopt ();
182 while ((opt = internal_getopt (list, "psuoq")) != -1)
208 if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG))
210 builtin_error ("cannot set and unset shell options simultaneously");
211 return (EXECUTION_FAILURE);
214 rval = EXECUTION_SUCCESS;
215 if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */
216 rval = list_shopt_o_options (list, flags);
217 else if (list && (flags & OFLAG)) /* shopt -so args */
218 rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG);
219 else if (flags & OFLAG) /* shopt -so */
220 rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags);
221 else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */
222 rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG);
223 else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */
224 rval = list_shopts (list, flags);
226 rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags);
230 /* Reset the options managed by `shopt' to the values they would have at
233 reset_shopt_options ()
235 allow_null_glob_expansion = glob_dot_filenames = 0;
236 cdable_vars = mail_warning = 0;
237 no_exit_on_failed_exec = print_shift_error = 0;
238 check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
240 source_uses_path = promptvars = 1;
242 #if defined (EXTENDED_GLOB)
246 #if defined (HISTORY)
247 literal_history = force_append_history = 0;
248 command_oriented_history = 1;
251 #if defined (READLINE)
252 hist_verify = history_reediting = 0;
253 perform_hostname_completion = 1;
256 shopt_login_shell = login_shell;
265 for (i = 0; shopt_vars[i].name; i++)
266 if (STREQ (name, shopt_vars[i].name))
271 #define SHOPT_ERROR(str) builtin_error ("%s: unknown shell option name", str)
274 toggle_shopts (mode, list, quiet)
282 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
284 ind = find_shopt (l->word->word);
287 SHOPT_ERROR (l->word->word);
288 rval = EXECUTION_FAILURE;
292 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
293 if (shopt_vars[ind].set_func)
294 (*shopt_vars[ind].set_func) (mode);
301 print_shopt (name, val, flags)
306 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
308 printf (OPTFMT, name, val ? on : off);
311 /* List the values of all or any of the `shopt' options. Returns 0 if
312 all were listed or all variables queried were on; 1 otherwise. */
314 list_shopts (list, flags)
323 for (i = 0; shopt_vars[i].name; i++)
325 val = *shopt_vars[i].value;
326 if ((flags & QFLAG) == 0)
327 print_shopt (shopt_vars[i].name, val, flags);
329 return (EXECUTION_SUCCESS);
332 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
334 i = find_shopt (l->word->word);
337 SHOPT_ERROR (l->word->word);
338 rval = EXECUTION_FAILURE;
341 val = *shopt_vars[i].value;
343 rval = EXECUTION_FAILURE;
344 if ((flags & QFLAG) == 0)
345 print_shopt (l->word->word, val, flags);
352 list_some_shopts (mode, flags)
357 for (i = 0; shopt_vars[i].name; i++)
359 val = *shopt_vars[i].value;
360 if (((flags & QFLAG) == 0) && mode == val)
361 print_shopt (shopt_vars[i].name, val, flags);
363 return (EXECUTION_SUCCESS);
367 list_shopt_o_options (list, flags)
376 if ((flags & QFLAG) == 0)
377 list_minus_o_opts (-1, (flags & PFLAG));
378 return (EXECUTION_SUCCESS);
381 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
383 val = minus_o_option_value (l->word->word);
386 builtin_error ("%s: unknown option name", l->word->word);
387 rval = EXECUTION_FAILURE;
391 rval = EXECUTION_FAILURE;
392 if ((flags & QFLAG) == 0)
395 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
397 printf (OPTFMT, l->word->word, val ? on : off);
404 list_some_o_options (mode, flags)
407 if ((flags & QFLAG) == 0)
408 list_minus_o_opts (mode, (flags & PFLAG));
409 return (EXECUTION_SUCCESS);
413 set_shopt_o_options (mode, list, quiet)
421 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
423 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
424 rval = EXECUTION_FAILURE;
430 /* If we set or unset interactive_comments with shopt, make sure the
431 change is reflected in $SHELLOPTS. */
433 set_interactive_comments (mode)
440 #if defined (RESTRICTED_SHELL)
441 /* Don't allow the value of restricted_shell to be modified. */
444 set_restricted_shell (mode)
447 static int save_restricted = -1;
449 if (save_restricted == -1)
450 save_restricted = shell_is_restricted (shell_name);
452 restricted_shell = save_restricted;
455 #endif /* RESTRICTED_SHELL */
457 /* Not static so shell.c can call it to initialize shopt_login_shell */
459 set_login_shell (mode)
462 shopt_login_shell = login_shell != 0;
472 n = sizeof (shopt_vars) / sizeof (shopt_vars[0]);
473 ret = alloc_array (n + 1);
474 for (i = 0; shopt_vars[i].name; i++)
475 ret[i] = savestring (shopt_vars[i].name);
476 ret[i] = (char *)NULL;
481 * External interface for other parts of the shell. NAME is a string option;
482 * MODE is 0 if we want to unset an option; 1 if we want to set an option.
483 * REUSABLE is 1 if we want to print output in a form that may be reused.
486 shopt_setopt (name, mode)
493 wl = add_string_to_list (name, (WORD_LIST *)NULL);
494 r = toggle_shopts (mode, wl, 0);
500 shopt_listopt (name, reusable)
507 return (list_shopts ((WORD_LIST *)NULL, reusable ? PFLAG : 0));
509 i = find_shopt (name);
513 return (EXECUTION_FAILURE);
516 print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0);
517 return (EXECUTION_SUCCESS);