1 This file is set.def, from which is created set.c.
2 It implements the "set" and "unset" builtins in Bash.
4 Copyright (C) 1987-2009 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
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
25 #if defined (HAVE_UNISTD_H)
27 # include <sys/types.h>
34 #include "../bashansi.h"
35 #include "../bashintl.h"
40 #include "bashgetopt.h"
42 #if defined (READLINE)
43 # include "../input.h"
44 # include "../bashline.h"
45 # include <readline/readline.h>
49 # include "../bashhist.h"
52 extern int posixly_correct, ignoreeof, eof_encountered_limit;
54 extern int dont_save_function_defs;
56 #if defined (READLINE)
57 extern int no_line_editing;
62 $SHORT_DOC set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
63 Set or unset values of shell options and positional parameters.
65 Change the value of shell attributes and positional parameters, or
66 display the names and values of shell variables.
69 -a Mark variables which are modified or created for export.
70 -b Notify of job termination immediately.
71 -e Exit immediately if a command exits with a non-zero status.
72 -f Disable file name generation (globbing).
73 -h Remember the location of commands as they are looked up.
74 -k All assignment arguments are placed in the environment for a
75 command, not just those that precede the command name.
76 -m Job control is enabled.
77 -n Read commands but do not execute them.
79 Set the variable corresponding to option-name:
81 braceexpand same as -B
82 #if defined (READLINE)
83 emacs use an emacs-style line editing interface
89 #if defined (BANG_HISTORY)
91 #endif /* BANG_HISTORY */
93 history enable command history
95 ignoreeof the shell will not exit upon reading EOF
97 allow comments to appear in interactive commands
103 nolog currently accepted but ignored
108 pipefail the return value of a pipeline is the status of
109 the last command to exit with a non-zero status,
110 or zero if no command exited with a non-zero status
111 posix change the behavior of bash where the default
112 operation differs from the Posix standard to
114 privileged same as -p
116 #if defined (READLINE)
117 vi use a vi-style line editing interface
118 #endif /* READLINE */
120 -p Turned on whenever the real and effective user ids do not match.
121 Disables processing of the $ENV file and importing of shell
122 functions. Turning this option off causes the effective uid and
123 gid to be set to the real uid and gid.
124 -t Exit after reading and executing one command.
125 -u Treat unset variables as an error when substituting.
126 -v Print shell input lines as they are read.
127 -x Print commands and their arguments as they are executed.
128 #if defined (BRACE_EXPANSION)
129 -B the shell will perform brace expansion
130 #endif /* BRACE_EXPANSION */
131 -C If set, disallow existing regular files to be overwritten
132 by redirection of output.
133 -E If set, the ERR trap is inherited by shell functions.
134 #if defined (BANG_HISTORY)
135 -H Enable ! style history substitution. This flag is on
136 by default when the shell is interactive.
137 #endif /* BANG_HISTORY */
138 -P If set, do not follow symbolic links when executing commands
139 such as cd which change the current directory.
140 -T If set, the DEBUG trap is inherited by shell functions.
141 -- Assign any remaining arguments to the positional parameters.
142 If there are no remaining arguments, the positional parameters
144 - Assign any remaining arguments to the positional parameters.
145 The -x and -v options are turned off.
147 Using + rather than - causes these flags to be turned off. The
148 flags can also be used upon invocation of the shell. The current
149 set of flags may be found in $-. The remaining n ARGs are positional
150 parameters and are assigned, in order, to $1, $2, .. $n. If no
151 ARGs are given, all shell variables are printed.
154 Returns success unless an invalid option is given.
157 typedef int setopt_set_func_t __P((int, char *));
158 typedef int setopt_get_func_t __P((char *));
160 static void print_minus_o_option __P((char *, int, int));
161 static void print_all_shell_variables __P((void));
163 static int set_ignoreeof __P((int, char *));
164 static int set_posix_mode __P((int, char *));
166 #if defined (READLINE)
167 static int set_edit_mode __P((int, char *));
168 static int get_edit_mode __P((char *));
171 #if defined (HISTORY)
172 static int bash_set_history __P((int, char *));
175 static const char * const on = "on";
176 static const char * const off = "off";
178 /* A struct used to match long options for set -o to the corresponding
179 option letter or internal variable. The functions can be called to
180 dynamically generate values. */
185 setopt_set_func_t *set_func;
186 setopt_get_func_t *get_func;
188 { "allexport", 'a', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
189 #if defined (BRACE_EXPANSION)
190 { "braceexpand",'B', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
192 #if defined (READLINE)
193 { "emacs", '\0', (int *)NULL, set_edit_mode, get_edit_mode },
195 { "errexit", 'e', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
196 { "errtrace", 'E', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
197 { "functrace", 'T', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
198 { "hashall", 'h', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
199 #if defined (BANG_HISTORY)
200 { "histexpand", 'H', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
201 #endif /* BANG_HISTORY */
202 #if defined (HISTORY)
203 { "history", '\0', &enable_history_list, bash_set_history, (setopt_get_func_t *)NULL },
205 { "ignoreeof", '\0', &ignoreeof, set_ignoreeof, (setopt_get_func_t *)NULL },
206 { "interactive-comments", '\0', &interactive_comments, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
207 { "keyword", 'k', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
208 { "monitor", 'm', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
209 { "noclobber", 'C', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
210 { "noexec", 'n', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
211 { "noglob", 'f', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
212 #if defined (HISTORY)
213 { "nolog", '\0', &dont_save_function_defs, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
215 #if defined (JOB_CONTROL)
216 { "notify", 'b', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
217 #endif /* JOB_CONTROL */
218 { "nounset", 'u', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
219 { "onecmd", 't', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
220 { "physical", 'P', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
221 { "pipefail", '\0', &pipefail_opt, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
222 { "posix", '\0', &posixly_correct, set_posix_mode, (setopt_get_func_t *)NULL },
223 { "privileged", 'p', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
224 { "verbose", 'v', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
225 #if defined (READLINE)
226 { "vi", '\0', (int *)NULL, set_edit_mode, get_edit_mode },
228 { "xtrace", 'x', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
229 {(char *)NULL, 0 , (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
232 #define N_O_OPTIONS (sizeof (o_options) / sizeof (o_options[0]))
234 #define GET_BINARY_O_OPTION_VALUE(i, name) \
235 ((o_options[i].get_func) ? (*o_options[i].get_func) (name) \
236 : (*o_options[i].variable))
238 #define SET_BINARY_O_OPTION_VALUE(i, onoff, name) \
239 ((o_options[i].set_func) ? (*o_options[i].set_func) (onoff, name) \
240 : (*o_options[i].variable = (onoff == FLAG_ON)))
243 minus_o_option_value (name)
249 for (i = 0; o_options[i].name; i++)
251 if (STREQ (name, o_options[i].name))
253 if (o_options[i].letter)
255 on_or_off = find_flag (o_options[i].letter);
256 return ((on_or_off == FLAG_UNKNOWN) ? -1 : *on_or_off);
259 return (GET_BINARY_O_OPTION_VALUE (i, name));
266 #define MINUS_O_FORMAT "%-15s\t%s\n"
269 print_minus_o_option (name, value, pflag)
274 printf (MINUS_O_FORMAT, name, value ? on : off);
276 printf ("set %co %s\n", value ? '-' : '+', name);
280 list_minus_o_opts (mode, reusable)
284 int *on_or_off, value;
286 for (i = 0; o_options[i].name; i++)
288 if (o_options[i].letter)
291 on_or_off = find_flag (o_options[i].letter);
292 if (on_or_off == FLAG_UNKNOWN)
294 if (mode == -1 || mode == *on_or_off)
295 print_minus_o_option (o_options[i].name, *on_or_off, reusable);
299 value = GET_BINARY_O_OPTION_VALUE (i, o_options[i].name);
300 if (mode == -1 || mode == value)
301 print_minus_o_option (o_options[i].name, value, reusable);
312 ret = strvec_create (N_O_OPTIONS + 1);
313 for (i = 0; o_options[i].name; i++)
314 ret[i] = o_options[i].name;
315 ret[i] = (char *)NULL;
320 set_ignoreeof (on_or_off, option_name)
324 ignoreeof = on_or_off == FLAG_ON;
325 unbind_variable ("ignoreeof");
327 bind_variable ("IGNOREEOF", "10", 0);
329 unbind_variable ("IGNOREEOF");
330 sv_ignoreeof ("IGNOREEOF");
335 set_posix_mode (on_or_off, option_name)
339 posixly_correct = on_or_off == FLAG_ON;
340 if (posixly_correct == 0)
341 unbind_variable ("POSIXLY_CORRECT");
343 bind_variable ("POSIXLY_CORRECT", "y", 0);
344 sv_strict_posix ("POSIXLY_CORRECT");
348 #if defined (READLINE)
349 /* Magic. This code `knows' how readline handles rl_editing_mode. */
351 set_edit_mode (on_or_off, option_name)
357 if (on_or_off == FLAG_ON)
359 rl_variable_bind ("editing-mode", option_name);
362 with_input_from_stdin ();
367 isemacs = rl_editing_mode == 1;
368 if ((isemacs && *option_name == 'e') || (!isemacs && *option_name == 'v'))
371 with_input_from_stream (stdin, "stdin");
375 return 1-no_line_editing;
382 return (*name == 'e' ? no_line_editing == 0 && rl_editing_mode == 1
383 : no_line_editing == 0 && rl_editing_mode == 0);
385 #endif /* READLINE */
387 #if defined (HISTORY)
389 bash_set_history (on_or_off, option_name)
393 if (on_or_off == FLAG_ON)
395 enable_history_list = 1;
396 bash_history_enable ();
397 if (history_lines_this_session == 0)
402 enable_history_list = 0;
403 bash_history_disable ();
405 return (1 - enable_history_list);
410 set_minus_o_option (on_or_off, option_name)
416 for (i = 0; o_options[i].name; i++)
418 if (STREQ (option_name, o_options[i].name))
420 if (o_options[i].letter == 0)
422 SET_BINARY_O_OPTION_VALUE (i, on_or_off, option_name);
423 return (EXECUTION_SUCCESS);
427 if (change_flag (o_options[i].letter, on_or_off) == FLAG_ERROR)
429 sh_invalidoptname (option_name);
430 return (EXECUTION_FAILURE);
433 return (EXECUTION_SUCCESS);
439 sh_invalidoptname (option_name);
444 print_all_shell_variables ()
448 vars = all_shell_variables ();
451 print_var_list (vars);
455 /* POSIX.2 does not allow function names and definitions to be output when
456 `set' is invoked without options (PASC Interp #202). */
457 if (posixly_correct == 0)
459 vars = all_shell_functions ();
462 print_func_list (vars);
472 char tflag[N_O_OPTIONS];
473 int vsize, i, vptr, *ip, exported;
476 for (vsize = i = 0; o_options[i].name; i++)
479 if (o_options[i].letter)
481 ip = find_flag (o_options[i].letter);
484 vsize += strlen (o_options[i].name) + 1;
488 else if (GET_BINARY_O_OPTION_VALUE (i, o_options[i].name))
490 vsize += strlen (o_options[i].name) + 1;
495 value = (char *)xmalloc (vsize + 1);
497 for (i = vptr = 0; o_options[i].name; i++)
501 strcpy (value + vptr, o_options[i].name);
502 vptr += strlen (o_options[i].name);
508 vptr--; /* cut off trailing colon */
511 v = find_variable ("SHELLOPTS");
513 /* Turn off the read-only attribute so we can bind the new value, and
514 note whether or not the variable was exported. */
517 VUNSETATTR (v, att_readonly);
518 exported = exported_p (v);
523 v = bind_variable ("SHELLOPTS", value, 0);
525 /* Turn the read-only attribute back on, and turn off the export attribute
526 if it was set implicitly by mark_modified_vars and SHELLOPTS was not
527 exported before we bound the new value. */
528 VSETATTR (v, att_readonly);
529 if (mark_modified_vars && exported == 0 && exported_p (v))
530 VUNSETATTR (v, att_exported);
536 parse_shellopts (value)
543 while (vname = extract_colon_unit (value, &vptr))
545 set_minus_o_option (FLAG_ON, vname);
551 initialize_shell_options (no_shellopts)
557 if (no_shellopts == 0)
559 var = find_variable ("SHELLOPTS");
560 /* set up any shell options we may have inherited. */
561 if (var && imported_p (var))
563 temp = (array_p (var) || assoc_p (var)) ? (char *)NULL : savestring (value_cell (var));
566 parse_shellopts (temp);
572 /* Set up the $SHELLOPTS variable. */
576 /* Reset the values of the -o options that are not also shell flags. This is
577 called from execute_cmd.c:initialize_subshell() when setting up a subshell
578 to run an executable shell script without a leading `#!'. */
580 reset_shell_options ()
582 #if defined (HISTORY)
583 remember_on_history = enable_history_list = 1;
588 /* Set some flags from the word values in the input list. If LIST is empty,
589 then print out the values of the variables instead. If LIST contains
590 non-flags, then set $1 - $9 to the successive words of LIST. */
595 int on_or_off, flag_name, force_assignment, opts_changed, rv, r;
601 print_all_shell_variables ();
602 return (sh_chkwrite (EXECUTION_SUCCESS));
605 /* Check validity of flag arguments. */
606 rv = EXECUTION_SUCCESS;
607 reset_internal_getopt ();
608 while ((flag_name = internal_getopt (list, optflags)) != -1)
614 return (list_optopt == '?' ? EXECUTION_SUCCESS : EX_USAGE);
620 /* Do the set command. While the list consists of words starting with
621 '-' or '+' treat them as flags, otherwise, start assigning them to
623 for (force_assignment = opts_changed = 0; list; )
625 arg = list->word->word;
627 /* If the argument is `--' or `-' then signal the end of the list
628 and remember the remaining arguments. */
629 if (arg[0] == '-' && (!arg[1] || (arg[1] == '-' && !arg[2])))
633 /* `set --' unsets the positional parameters. */
635 force_assignment = 1;
637 /* Until told differently, the old shell behaviour of
638 `set - [arg ...]' being equivalent to `set +xv [arg ...]'
639 stands. Posix.2 says the behaviour is marked as obsolescent. */
642 change_flag ('x', '+');
643 change_flag ('v', '+');
650 if ((on_or_off = *arg) && (on_or_off == '-' || on_or_off == '+'))
652 while (flag_name = *++arg)
654 if (flag_name == '?')
657 return (EXECUTION_SUCCESS);
659 else if (flag_name == 'o') /* -+o option-name */
668 list_minus_o_opts (-1, (on_or_off == '+'));
669 rv = sh_chkwrite (rv);
673 option_name = opt->word->word;
675 if (option_name == 0 || *option_name == '\0' ||
676 *option_name == '-' || *option_name == '+')
678 list_minus_o_opts (-1, (on_or_off == '+'));
681 list = list->next; /* Skip over option name. */
684 if ((r = set_minus_o_option (on_or_off, option_name)) != EXECUTION_SUCCESS)
690 else if (change_flag (flag_name, on_or_off) == FLAG_ERROR)
698 return (EXECUTION_FAILURE);
710 /* Assigning $1 ... $n */
711 if (list || force_assignment)
712 remember_args (list, 1);
713 /* Set up new value of $SHELLOPTS */
720 $FUNCTION unset_builtin
721 $SHORT_DOC unset [-f] [-v] [name ...]
722 Unset values and attributes of shell variables and functions.
724 For each NAME, remove the corresponding variable or function.
727 -f treat each NAME as a shell function
728 -v treat each NAME as a shell variable
730 Without options, unset first tries to unset a variable, and if that fails,
731 tries to unset a function.
733 Some variables cannot be unset; also see `readonly'.
736 Returns success unless an invalid option is given or a NAME is read-only.
739 #define NEXT_VARIABLE() any_failed++; list = list->next; continue;
745 int unset_function, unset_variable, unset_array, opt, any_failed;
748 unset_function = unset_variable = unset_array = any_failed = 0;
750 reset_internal_getopt ();
751 while ((opt = internal_getopt (list, "fv")) != -1)
769 if (unset_function && unset_variable)
771 builtin_error (_("cannot simultaneously unset a function and a variable"));
772 return (EXECUTION_FAILURE);
779 #if defined (ARRAY_VARS)
783 name = list->word->word;
785 #if defined (ARRAY_VARS)
787 if (!unset_function && valid_array_reference (name))
789 t = strchr (name, '[');
795 /* Bash allows functions with names which are not valid identifiers
796 to be created when not in posix mode, so check only when in posix
797 mode when unsetting a function. */
798 if (((unset_function && posixly_correct) || !unset_function) && legal_identifier (name) == 0)
804 var = unset_function ? find_function (name) : find_variable (name);
806 if (var && !unset_function && non_unsettable_p (var))
808 builtin_error (_("%s: cannot unset"), name);
812 /* Posix.2 says that unsetting readonly variables is an error. */
813 if (var && readonly_p (var))
815 builtin_error (_("%s: cannot unset: readonly %s"),
816 name, unset_function ? "function" : "variable");
820 /* Unless the -f option is supplied, the name refers to a variable. */
821 #if defined (ARRAY_VARS)
822 if (var && unset_array)
824 if (array_p (var) == 0 && assoc_p (var) == 0)
826 builtin_error (_("%s: not an array variable"), name);
831 tem = unbind_array_element (var, t);
837 #endif /* ARRAY_VARS */
838 tem = unset_function ? unbind_func (name) : unbind_variable (name);
840 /* This is what Posix.2 draft 11+ says. ``If neither -f nor -v
841 is specified, the name refers to a variable; if a variable by
842 that name does not exist, a function by that name, if any,
844 if (tem == -1 && !unset_function && !unset_variable)
845 tem = unbind_func (name);
847 /* SUSv3, POSIX.1-2001 say: ``Unsetting a variable or function that
848 was not previously set shall not be considered an error.'' */
850 if (unset_function == 0)
851 stupidly_hack_special_variables (name);
856 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);