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-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.
26 #if defined (HAVE_UNISTD_H)
28 # include <sys/types.h>
35 #include "../bashansi.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] [arg ...]
63 -a Mark variables which are modified or created for export.
64 -b Notify of job termination immediately.
65 -e Exit immediately if a command exits with a non-zero status.
66 -f Disable file name generation (globbing).
67 -h Remember the location of commands as they are looked up.
68 -k All assignment arguments are placed in the environment for a
69 command, not just those that precede the command name.
70 -m Job control is enabled.
71 -n Read commands but do not execute them.
73 Set the variable corresponding to option-name:
75 braceexpand same as -B
76 #if defined (READLINE)
77 emacs use an emacs-style line editing interface
81 #if defined (BANG_HISTORY)
83 #endif /* BANG_HISTORY */
85 history enable command history
87 ignoreeof the shell will not exit upon reading EOF
89 allow comments to appear in interactive commands
95 nolog currently accepted but ignored
100 posix change the behavior of bash where the default
101 operation differs from the 1003.2 standard to
103 privileged same as -p
105 #if defined (READLINE)
106 vi use a vi-style line editing interface
107 #endif /* READLINE */
109 -p Turned on whenever the real and effective user ids do not match.
110 Disables processing of the $ENV file and importing of shell
111 functions. Turning this option off causes the effective uid and
112 gid to be set to the real uid and gid.
113 -t Exit after reading and executing one command.
114 -u Treat unset variables as an error when substituting.
115 -v Print shell input lines as they are read.
116 -x Print commands and their arguments as they are executed.
117 #if defined (BRACE_EXPANSION)
118 -B the shell will perform brace expansion
119 #endif /* BRACE_EXPANSION */
120 -C If set, disallow existing regular files to be overwritten
121 by redirection of output.
122 #if defined (BANG_HISTORY)
123 -H Enable ! style history substitution. This flag is on
125 #endif /* BANG_HISTORY */
126 -P If set, do not follow symbolic links when executing commands
127 such as cd which change the current directory.
129 Using + rather than - causes these flags to be turned off. The
130 flags can also be used upon invocation of the shell. The current
131 set of flags may be found in $-. The remaining n ARGs are positional
132 parameters and are assigned, in order, to $1, $2, .. $n. If no
133 ARGs are given, all shell variables are printed.
136 typedef int setopt_set_func_t __P((int, char *));
137 typedef int setopt_get_func_t __P((char *));
139 static void print_minus_o_option __P((char *, int, int));
140 static void print_all_shell_variables __P((void));
142 static int set_ignoreeof __P((int, char *));
143 static int set_posix_mode __P((int, char *));
145 #if defined (READLINE)
146 static int set_edit_mode __P((int, char *));
147 static int get_edit_mode __P((char *));
150 #if defined (HISTORY)
151 static int bash_set_history __P((int, char *));
154 static char *on = "on";
155 static char *off = "off";
157 /* A struct used to match long options for set -o to the corresponding
158 option letter or internal variable. The functions can be called to
159 dynamically generate values. */
164 setopt_set_func_t *set_func;
165 setopt_get_func_t *get_func;
167 { "allexport", 'a', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
168 #if defined (BRACE_EXPANSION)
169 { "braceexpand",'B', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
171 #if defined (READLINE)
172 { "emacs", '\0', (int *)NULL, set_edit_mode, get_edit_mode },
174 { "errexit", 'e', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
175 { "hashall", 'h', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
176 #if defined (BANG_HISTORY)
177 { "histexpand", 'H', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
178 #endif /* BANG_HISTORY */
179 #if defined (HISTORY)
180 { "history", '\0', &remember_on_history, bash_set_history, (setopt_get_func_t *)NULL },
182 { "ignoreeof", '\0', &ignoreeof, set_ignoreeof, (setopt_get_func_t *)NULL },
183 { "interactive-comments", '\0', &interactive_comments, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
184 { "keyword", 'k', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
185 { "monitor", 'm', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
186 { "noclobber", 'C', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
187 { "noexec", 'n', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
188 { "noglob", 'f', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
189 #if defined (HISTORY)
190 { "nolog", '\0', &dont_save_function_defs, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
192 #if defined (JOB_CONTROL)
193 { "notify", 'b', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
194 #endif /* JOB_CONTROL */
195 { "nounset", 'u', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
196 { "onecmd", 't', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
197 { "physical", 'P', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
198 { "posix", '\0', &posixly_correct, set_posix_mode, (setopt_get_func_t *)NULL },
199 { "privileged", 'p', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
200 { "verbose", 'v', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
201 #if defined (READLINE)
202 { "vi", '\0', (int *)NULL, set_edit_mode, get_edit_mode },
204 { "xtrace", 'x', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
205 {(char *)NULL, 0 , (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
208 #define N_O_OPTIONS (sizeof (o_options) / sizeof (o_options[0]))
210 #define GET_BINARY_O_OPTION_VALUE(i, name) \
211 ((o_options[i].get_func) ? (*o_options[i].get_func) (name) \
212 : (*o_options[i].variable))
214 #define SET_BINARY_O_OPTION_VALUE(i, onoff, name) \
215 ((o_options[i].set_func) ? (*o_options[i].set_func) (onoff, name) \
216 : (*o_options[i].variable = (onoff == FLAG_ON)))
219 minus_o_option_value (name)
225 for (i = 0; o_options[i].name; i++)
227 if (STREQ (name, o_options[i].name))
229 if (o_options[i].letter)
231 on_or_off = find_flag (o_options[i].letter);
232 return ((on_or_off == FLAG_UNKNOWN) ? -1 : *on_or_off);
235 return (GET_BINARY_O_OPTION_VALUE (i, name));
242 #define MINUS_O_FORMAT "%-15s\t%s\n"
245 print_minus_o_option (name, value, pflag)
250 printf (MINUS_O_FORMAT, name, value ? on : off);
252 printf ("set %co %s\n", value ? '-' : '+', name);
256 list_minus_o_opts (mode, reusable)
260 int *on_or_off, value;
262 for (i = 0; o_options[i].name; i++)
264 if (o_options[i].letter)
267 on_or_off = find_flag (o_options[i].letter);
268 if (on_or_off == FLAG_UNKNOWN)
270 if (mode == -1 || mode == *on_or_off)
271 print_minus_o_option (o_options[i].name, *on_or_off, reusable);
275 value = GET_BINARY_O_OPTION_VALUE (i, o_options[i].name);
276 if (mode == -1 || mode == value)
277 print_minus_o_option (o_options[i].name, value, reusable);
288 ret = strvec_create (N_O_OPTIONS + 1);
289 for (i = 0; o_options[i].name; i++)
290 ret[i] = o_options[i].name;
291 ret[i] = (char *)NULL;
296 set_ignoreeof (on_or_off, option_name)
300 ignoreeof = on_or_off == FLAG_ON;
301 unbind_variable ("ignoreeof");
303 bind_variable ("IGNOREEOF", "10");
305 unbind_variable ("IGNOREEOF");
306 sv_ignoreeof ("IGNOREEOF");
311 set_posix_mode (on_or_off, option_name)
315 posixly_correct = on_or_off == FLAG_ON;
316 if (posixly_correct == 0)
317 unbind_variable ("POSIXLY_CORRECT");
319 bind_variable ("POSIXLY_CORRECT", "y");
320 sv_strict_posix ("POSIXLY_CORRECT");
324 #if defined (READLINE)
325 /* Magic. This code `knows' how readline handles rl_editing_mode. */
327 set_edit_mode (on_or_off, option_name)
333 if (on_or_off == FLAG_ON)
335 rl_variable_bind ("editing-mode", option_name);
338 with_input_from_stdin ();
343 isemacs = rl_editing_mode == 1;
344 if ((isemacs && *option_name == 'e') || (!isemacs && *option_name == 'v'))
347 with_input_from_stream (stdin, "stdin");
351 return 1-no_line_editing;
358 return (*name == 'e' ? no_line_editing == 0 && rl_editing_mode == 1
359 : no_line_editing == 0 && rl_editing_mode == 0);
361 #endif /* READLINE */
363 #if defined (HISTORY)
365 bash_set_history (on_or_off, option_name)
369 if (on_or_off == FLAG_ON)
371 bash_history_enable ();
372 if (history_lines_this_session == 0)
376 bash_history_disable ();
377 return (1 - remember_on_history);
382 set_minus_o_option (on_or_off, option_name)
388 for (i = 0; o_options[i].name; i++)
390 if (STREQ (option_name, o_options[i].name))
392 if (o_options[i].letter == 0)
394 SET_BINARY_O_OPTION_VALUE (i, on_or_off, option_name);
395 return (EXECUTION_SUCCESS);
399 if (change_flag (o_options[i].letter, on_or_off) == FLAG_ERROR)
401 sh_invalidoptname (option_name);
402 return (EXECUTION_FAILURE);
405 return (EXECUTION_SUCCESS);
411 sh_invalidoptname (option_name);
412 return (EXECUTION_FAILURE);
416 print_all_shell_variables ()
420 vars = all_shell_variables ();
423 print_var_list (vars);
427 /* POSIX.2 does not allow function names and definitions to be output when
428 `set' is invoked without options (PASC Interp #202). */
429 if (posixly_correct == 0)
431 vars = all_shell_functions ();
434 print_func_list (vars);
444 char tflag[N_O_OPTIONS];
445 int vsize, i, vptr, *ip, exported;
448 for (vsize = i = 0; o_options[i].name; i++)
451 if (o_options[i].letter)
453 ip = find_flag (o_options[i].letter);
456 vsize += strlen (o_options[i].name) + 1;
460 else if (GET_BINARY_O_OPTION_VALUE (i, o_options[i].name))
462 vsize += strlen (o_options[i].name) + 1;
467 value = (char *)xmalloc (vsize + 1);
469 for (i = vptr = 0; o_options[i].name; i++)
473 strcpy (value + vptr, o_options[i].name);
474 vptr += strlen (o_options[i].name);
480 vptr--; /* cut off trailing colon */
483 v = find_variable ("SHELLOPTS");
485 /* Turn off the read-only attribute so we can bind the new value, and
486 note whether or not the variable was exported. */
489 VUNSETATTR (v, att_readonly);
490 exported = exported_p (v);
495 v = bind_variable ("SHELLOPTS", value);
497 /* Turn the read-only attribute back on, and turn off the export attribute
498 if it was set implicitly by mark_modified_vars and SHELLOPTS was not
499 exported before we bound the new value. */
500 VSETATTR (v, att_readonly);
501 if (mark_modified_vars && exported == 0 && exported_p (v))
502 VUNSETATTR (v, att_exported);
508 parse_shellopts (value)
515 while (vname = extract_colon_unit (value, &vptr))
517 set_minus_o_option (FLAG_ON, vname);
523 initialize_shell_options (no_shellopts)
529 if (no_shellopts == 0)
531 var = find_variable ("SHELLOPTS");
532 /* set up any shell options we may have inherited. */
533 if (var && imported_p (var))
535 temp = (array_p (var)) ? (char *)NULL : savestring (value_cell (var));
538 parse_shellopts (temp);
544 /* Set up the $SHELLOPTS variable. */
548 /* Reset the values of the -o options that are not also shell flags. This is
549 called from execute_cmd.c:initialize_subshell() when setting up a subshell
550 to run an executable shell script without a leading `#!'. */
552 reset_shell_options ()
554 #if defined (HISTORY)
555 remember_on_history = 1;
560 /* Set some flags from the word values in the input list. If LIST is empty,
561 then print out the values of the variables instead. If LIST contains
562 non-flags, then set $1 - $9 to the successive words of LIST. */
567 int on_or_off, flag_name, force_assignment, opts_changed;
574 print_all_shell_variables ();
575 return (EXECUTION_SUCCESS);
578 /* Check validity of flag arguments. */
579 reset_internal_getopt ();
580 while ((flag_name = internal_getopt (list, optflags)) != -1)
586 return (list_optopt == '?' ? EXECUTION_SUCCESS : EX_USAGE);
592 /* Do the set command. While the list consists of words starting with
593 '-' or '+' treat them as flags, otherwise, start assigning them to
595 for (force_assignment = opts_changed = 0; list; )
597 arg = list->word->word;
599 /* If the argument is `--' or `-' then signal the end of the list
600 and remember the remaining arguments. */
601 if (arg[0] == '-' && (!arg[1] || (arg[1] == '-' && !arg[2])))
605 /* `set --' unsets the positional parameters. */
607 force_assignment = 1;
609 /* Until told differently, the old shell behaviour of
610 `set - [arg ...]' being equivalent to `set +xv [arg ...]'
611 stands. Posix.2 says the behaviour is marked as obsolescent. */
614 change_flag ('x', '+');
615 change_flag ('v', '+');
622 if ((on_or_off = *arg) && (on_or_off == '-' || on_or_off == '+'))
624 while (flag_name = *++arg)
626 if (flag_name == '?')
629 return (EXECUTION_SUCCESS);
631 else if (flag_name == 'o') /* -+o option-name */
640 list_minus_o_opts (-1, (on_or_off == '+'));
644 option_name = opt->word->word;
646 if (option_name == 0 || *option_name == '\0' ||
647 *option_name == '-' || *option_name == '+')
649 list_minus_o_opts (-1, (on_or_off == '+'));
652 list = list->next; /* Skip over option name. */
655 if (set_minus_o_option (on_or_off, option_name) != EXECUTION_SUCCESS)
658 return (EXECUTION_FAILURE);
661 else if (change_flag (flag_name, on_or_off) == FLAG_ERROR)
669 return (EXECUTION_FAILURE);
681 /* Assigning $1 ... $n */
682 if (list || force_assignment)
683 remember_args (list, 1);
684 /* Set up new value of $SHELLOPTS */
687 return (EXECUTION_SUCCESS);
691 $FUNCTION unset_builtin
692 $SHORT_DOC unset [-f] [-v] [name ...]
693 For each NAME, remove the corresponding variable or function. Given
694 the `-v', unset will only act on variables. Given the `-f' flag,
695 unset will only act on functions. With neither flag, unset first
696 tries to unset a variable, and if that fails, then tries to unset a
697 function. Some variables cannot be unset; also see readonly.
700 #define NEXT_VARIABLE() any_failed++; list = list->next; continue;
706 int unset_function, unset_variable, unset_array, opt, any_failed;
709 unset_function = unset_variable = unset_array = any_failed = 0;
711 reset_internal_getopt ();
712 while ((opt = internal_getopt (list, "fv")) != -1)
730 if (unset_function && unset_variable)
732 builtin_error ("cannot simultaneously unset a function and a variable");
733 return (EXECUTION_FAILURE);
740 #if defined (ARRAY_VARS)
744 name = list->word->word;
746 #if defined (ARRAY_VARS)
748 if (!unset_function && valid_array_reference (name))
750 t = strchr (name, '[');
756 /* Bash allows functions with names which are not valid identifiers
757 to be created when not in posix mode, so check only when in posix
758 mode when unsetting a function. */
759 if (((unset_function && posixly_correct) || !unset_function) && legal_identifier (name) == 0)
765 var = unset_function ? find_function (name) : find_variable (name);
767 if (var && !unset_function && non_unsettable_p (var))
769 builtin_error ("%s: cannot unset", name);
773 /* Posix.2 says that unsetting readonly variables is an error. */
774 if (var && readonly_p (var))
776 builtin_error ("%s: cannot unset: readonly %s",
777 name, unset_function ? "function" : "variable");
781 /* Unless the -f option is supplied, the name refers to a variable. */
782 #if defined (ARRAY_VARS)
783 if (var && unset_array)
785 if (array_p (var) == 0)
787 builtin_error ("%s: not an array variable", name);
792 tem = unbind_array_element (var, t);
798 #endif /* ARRAY_VARS */
799 tem = unset_function ? unbind_func (name) : unbind_variable (name);
801 /* This is what Posix.2 draft 11+ says. ``If neither -f nor -v
802 is specified, the name refers to a variable; if a variable by
803 that name does not exist, a function by that name, if any,
805 if (tem == -1 && !unset_function && !unset_variable)
806 tem = unbind_func (name);
808 /* SUSv3, POSIX.1-2001 say: ``Unsetting a variable or function that
809 was not previously set shall not be considered an error.'' */
811 if (unset_function == 0)
812 stupidly_hack_special_variables (name);
817 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);