1 This file is shopt.def, from which is created shopt.c.
2 It implements the Bash `shopt' builtin.
4 Copyright (C) 1994-2002 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 $FUNCTION shopt_builtin
26 $SHORT_DOC shopt [-pqsu] [-o long-option] optname [optname...]
27 Toggle the values of variables controlling optional behavior.
28 The -s flag means to enable (set) each OPTNAME; the -u flag
29 unsets each OPTNAME. The -q flag suppresses output; the exit
30 status indicates whether each OPTNAME is set or unset. The -o
31 option restricts the OPTNAMEs to those defined for use with
32 `set -o'. With no options, or with the -p option, a list of all
33 settable options is displayed, with an indication of whether or
39 #if defined (HAVE_UNISTD_H)
41 # include <sys/types.h>
51 #include "bashgetopt.h"
56 #define OPTFMT "%-15s\t%s\n"
58 extern int allow_null_glob_expansion, glob_dot_filenames;
59 extern int cdable_vars, mail_warning, source_uses_path;
60 extern int no_exit_on_failed_exec, print_shift_error;
61 extern int check_hashed_filenames, promptvars;
62 extern int cdspelling, expand_aliases;
63 extern int check_window_size;
64 extern int glob_ignore_case;
65 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 int no_empty_command_completion;
80 extern int enable_hostname_completion __P((int));
83 #if defined (PROGRAMMABLE_COMPLETION)
84 extern int prog_completion_enabled;
87 #if defined (RESTRICTED_SHELL)
88 extern char *shell_name;
91 static void shopt_error __P((char *));
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))
275 builtin_error ("%s: invalid shell option name", s);
279 toggle_shopts (mode, list, quiet)
287 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
289 ind = find_shopt (l->word->word);
292 shopt_error (l->word->word);
293 rval = EXECUTION_FAILURE;
297 *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */
298 if (shopt_vars[ind].set_func)
299 (*shopt_vars[ind].set_func) (mode);
306 print_shopt (name, val, flags)
311 printf ("shopt %s %s\n", val ? "-s" : "-u", name);
313 printf (OPTFMT, name, val ? on : off);
316 /* List the values of all or any of the `shopt' options. Returns 0 if
317 all were listed or all variables queried were on; 1 otherwise. */
319 list_shopts (list, flags)
328 for (i = 0; shopt_vars[i].name; i++)
330 val = *shopt_vars[i].value;
331 if ((flags & QFLAG) == 0)
332 print_shopt (shopt_vars[i].name, val, flags);
334 return (EXECUTION_SUCCESS);
337 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
339 i = find_shopt (l->word->word);
342 shopt_error (l->word->word);
343 rval = EXECUTION_FAILURE;
346 val = *shopt_vars[i].value;
348 rval = EXECUTION_FAILURE;
349 if ((flags & QFLAG) == 0)
350 print_shopt (l->word->word, val, flags);
357 list_some_shopts (mode, flags)
362 for (i = 0; shopt_vars[i].name; i++)
364 val = *shopt_vars[i].value;
365 if (((flags & QFLAG) == 0) && mode == val)
366 print_shopt (shopt_vars[i].name, val, flags);
368 return (EXECUTION_SUCCESS);
372 list_shopt_o_options (list, flags)
381 if ((flags & QFLAG) == 0)
382 list_minus_o_opts (-1, (flags & PFLAG));
383 return (EXECUTION_SUCCESS);
386 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
388 val = minus_o_option_value (l->word->word);
391 sh_invalidoptname (l->word->word);
392 rval = EXECUTION_FAILURE;
396 rval = EXECUTION_FAILURE;
397 if ((flags & QFLAG) == 0)
400 printf ("set %co %s\n", val ? '-' : '+', l->word->word);
402 printf (OPTFMT, l->word->word, val ? on : off);
409 list_some_o_options (mode, flags)
412 if ((flags & QFLAG) == 0)
413 list_minus_o_opts (mode, (flags & PFLAG));
414 return (EXECUTION_SUCCESS);
418 set_shopt_o_options (mode, list, quiet)
426 for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next)
428 if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE)
429 rval = EXECUTION_FAILURE;
435 /* If we set or unset interactive_comments with shopt, make sure the
436 change is reflected in $SHELLOPTS. */
438 set_interactive_comments (mode)
445 #if defined (RESTRICTED_SHELL)
446 /* Don't allow the value of restricted_shell to be modified. */
449 set_restricted_shell (mode)
452 static int save_restricted = -1;
454 if (save_restricted == -1)
455 save_restricted = shell_is_restricted (shell_name);
457 restricted_shell = save_restricted;
460 #endif /* RESTRICTED_SHELL */
462 /* Not static so shell.c can call it to initialize shopt_login_shell */
464 set_login_shell (mode)
467 shopt_login_shell = login_shell != 0;
477 n = sizeof (shopt_vars) / sizeof (shopt_vars[0]);
478 ret = strvec_create (n + 1);
479 for (i = 0; shopt_vars[i].name; i++)
480 ret[i] = savestring (shopt_vars[i].name);
481 ret[i] = (char *)NULL;
486 * External interface for other parts of the shell. NAME is a string option;
487 * MODE is 0 if we want to unset an option; 1 if we want to set an option.
488 * REUSABLE is 1 if we want to print output in a form that may be reused.
491 shopt_setopt (name, mode)
498 wl = add_string_to_list (name, (WORD_LIST *)NULL);
499 r = toggle_shopts (mode, wl, 0);
505 shopt_listopt (name, reusable)
512 return (list_shopts ((WORD_LIST *)NULL, reusable ? PFLAG : 0));
514 i = find_shopt (name);
518 return (EXECUTION_FAILURE);
521 print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0);
522 return (EXECUTION_SUCCESS);