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, 1989, 1991 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 noclobber, 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 -i Force the shell to be an "interactive" one. Interactive shells
69 always read `~/.bashrc' on startup.
70 -k All assignment arguments are placed in the environment for a
71 command, not just those that precede the command name.
72 -m Job control is enabled.
73 -n Read commands but do not execute them.
75 Set the variable corresponding to option-name:
77 braceexpand same as -B
78 #if defined (READLINE)
79 emacs use an emacs-style line editing interface
83 #if defined (BANG_HISTORY)
85 #endif /* BANG_HISTORY */
87 history enable command history
89 ignoreeof the shell will not exit upon reading EOF
91 allow comments to appear in interactive commands
97 nolog currently accepted but ignored
102 posix change the behavior of bash where the default
103 operation differs from the 1003.2 standard to
105 privileged same as -p
107 #if defined (READLINE)
108 vi use a vi-style line editing interface
109 #endif /* READLINE */
111 -p Turned on whenever the real and effective user ids do not match.
112 Disables processing of the $ENV file and importing of shell
113 functions. Turning this option off causes the effective uid and
114 gid to be set to the real uid and gid.
115 -t Exit after reading and executing one command.
116 -u Treat unset variables as an error when substituting.
117 -v Print shell input lines as they are read.
118 -x Print commands and their arguments as they are executed.
119 #if defined (BRACE_EXPANSION)
120 -B the shell will perform brace expansion
121 #endif /* BRACE_EXPANSION */
122 -C If set, disallow existing regular files to be overwritten
123 by redirection of output.
124 #if defined (BANG_HISTORY)
125 -H Enable ! style history substitution. This flag is on
127 #endif /* BANG_HISTORY */
128 -P If set, do not follow symbolic links when executing commands
129 such as cd which change the current directory.
131 Using + rather than - causes these flags to be turned off. The
132 flags can also be used upon invocation of the shell. The current
133 set of flags may be found in $-. The remaining n ARGs are positional
134 parameters and are assigned, in order, to $1, $2, .. $n. If no
135 ARGs are given, all shell variables are printed.
138 static void print_minus_o_option __P((char *, int, int));
139 static void print_all_shell_variables __P((void));
141 static int set_ignoreeof __P((int, char *));
142 static int set_posix_mode __P((int, char *));
144 #if defined (READLINE)
145 static int set_edit_mode __P((int, char *));
146 static int get_edit_mode __P((char *));
149 #if defined (HISTORY)
150 static int bash_set_history __P((int, char *));
153 static char *on = "on";
154 static char *off = "off";
156 /* An a-list used to match long options for set -o to the corresponding
162 { "allexport", 'a' },
163 #if defined (BRACE_EXPANSION)
164 { "braceexpand",'B' },
168 #if defined (BANG_HISTORY)
169 { "histexpand", 'H' },
170 #endif /* BANG_HISTORY */
173 { "noclobber", 'C' },
176 #if defined (JOB_CONTROL)
178 #endif /* JOB_CONTROL */
182 { "privileged", 'p' },
188 typedef int setopt_set_func_t __P((int, char *));
189 typedef int setopt_get_func_t __P((char *));
194 setopt_set_func_t *set_func;
195 setopt_get_func_t *get_func;
196 } binary_o_options[] = {
197 #if defined (HISTORY)
198 { "history", &remember_on_history, bash_set_history, (setopt_get_func_t *)NULL },
200 { "ignoreeof", &ignoreeof, set_ignoreeof, (setopt_get_func_t *)NULL },
201 { "interactive-comments", &interactive_comments, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
202 #if defined (HISTORY)
203 { "nolog", &dont_save_function_defs, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },
205 { "posix", &posixly_correct, set_posix_mode, (setopt_get_func_t *)NULL },
206 #if defined (READLINE)
207 { "emacs", (int *)NULL, set_edit_mode, get_edit_mode },
208 { "vi", (int *)NULL, set_edit_mode, get_edit_mode },
210 { (char *)NULL, (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }
213 #define GET_BINARY_O_OPTION_VALUE(i, name) \
214 ((binary_o_options[i].get_func) ? (*binary_o_options[i].get_func) (name) \
215 : (*binary_o_options[i].variable))
217 #define SET_BINARY_O_OPTION_VALUE(i, onoff, name) \
218 ((binary_o_options[i].set_func) ? (*binary_o_options[i].set_func) (onoff, name) \
219 : (*binary_o_options[i].variable = (onoff == FLAG_ON)))
222 minus_o_option_value (name)
228 for (i = 0; o_options[i].name; i++)
230 if (STREQ (name, o_options[i].name))
232 on_or_off = find_flag (o_options[i].letter);
233 return ((on_or_off == FLAG_UNKNOWN) ? -1 : *on_or_off);
236 for (i = 0; binary_o_options[i].name; i++)
238 if (STREQ (name, binary_o_options[i].name))
239 return (GET_BINARY_O_OPTION_VALUE (i, name));
245 #define MINUS_O_FORMAT "%-15s\t%s\n"
248 print_minus_o_option (name, value, pflag)
253 printf (MINUS_O_FORMAT, name, value ? on : off);
255 printf ("set %co %s\n", value ? '-' : '+', name);
259 list_minus_o_opts (mode, reusable)
263 int *on_or_off, value;
265 for (value = i = 0; o_options[i].name; i++)
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);
273 for (i = 0; binary_o_options[i].name; i++)
275 value = GET_BINARY_O_OPTION_VALUE (i, binary_o_options[i].name);
276 if (mode == -1 || mode == value)
277 print_minus_o_option (binary_o_options[i].name, value, reusable);
287 n = (sizeof (o_options) / sizeof (o_options[0])) +
288 (sizeof (binary_o_options) / sizeof (binary_o_options[0]));
289 ret = alloc_array (n + 1);
290 for (i = ind = 0; o_options[i].name; i++)
291 ret[ind++] = o_options[i].name;
292 for (i = 0; binary_o_options[i].name; i++)
293 ret[ind++] = binary_o_options[i].name;
294 ret[ind] = (char *)NULL;
299 set_ignoreeof (on_or_off, option_name)
303 ignoreeof = on_or_off == FLAG_ON;
304 unbind_variable ("ignoreeof");
306 bind_variable ("IGNOREEOF", "10");
308 unbind_variable ("IGNOREEOF");
309 sv_ignoreeof ("IGNOREEOF");
314 set_posix_mode (on_or_off, option_name)
318 posixly_correct = on_or_off == FLAG_ON;
319 if (posixly_correct == 0)
320 unbind_variable ("POSIXLY_CORRECT");
322 bind_variable ("POSIXLY_CORRECT", "y");
323 sv_strict_posix ("POSIXLY_CORRECT");
327 #if defined (READLINE)
328 /* Magic. This code `knows' how readline handles rl_editing_mode. */
330 set_edit_mode (on_or_off, option_name)
336 if (on_or_off == FLAG_ON)
338 rl_variable_bind ("editing-mode", option_name);
341 with_input_from_stdin ();
346 isemacs = rl_editing_mode == 1;
347 if ((isemacs && *option_name == 'e') || (!isemacs && *option_name == 'v'))
350 with_input_from_stream (stdin, "stdin");
354 return 1-no_line_editing;
361 return (*name == 'e' ? no_line_editing == 0 && rl_editing_mode == 1
362 : no_line_editing == 0 && rl_editing_mode == 0);
364 #endif /* READLINE */
366 #if defined (HISTORY)
368 bash_set_history (on_or_off, option_name)
372 if (on_or_off == FLAG_ON)
374 bash_history_enable ();
375 if (history_lines_this_session == 0)
379 bash_history_disable ();
380 return (1 - remember_on_history);
385 set_minus_o_option (on_or_off, option_name)
392 for (i = 0; binary_o_options[i].name; i++)
394 if (STREQ (option_name, binary_o_options[i].name))
396 SET_BINARY_O_OPTION_VALUE (i, on_or_off, option_name);
397 return (EXECUTION_SUCCESS);
401 for (i = 0, option_char = -1; o_options[i].name; i++)
403 if (STREQ (option_name, o_options[i].name))
405 option_char = o_options[i].letter;
409 if (option_char == -1)
411 builtin_error ("%s: unknown option name", option_name);
412 return (EXECUTION_FAILURE);
414 if (change_flag (option_char, on_or_off) == FLAG_ERROR)
416 bad_option (option_name);
417 return (EXECUTION_FAILURE);
419 return (EXECUTION_SUCCESS);
423 print_all_shell_variables ()
427 vars = all_shell_variables ();
430 print_var_list (vars);
434 /* POSIX.2 does not allow function names and definitions to be output when
435 `set' is invoked without options (PASC Interp #202). */
436 if (posixly_correct == 0)
438 vars = all_shell_functions ();
441 print_func_list (vars);
451 int vsize, i, vptr, *ip, exported;
454 for (vsize = i = 0; o_options[i].name; i++)
456 ip = find_flag (o_options[i].letter);
458 vsize += strlen (o_options[i].name) + 1;
460 for (i = 0; binary_o_options[i].name; i++)
461 if (GET_BINARY_O_OPTION_VALUE (i, binary_o_options[i].name))
462 vsize += strlen (binary_o_options[i].name) + 1;
464 value = (char *)xmalloc (vsize + 1);
466 for (i = vptr = 0; o_options[i].name; i++)
468 ip = find_flag (o_options[i].letter);
471 strcpy (value + vptr, o_options[i].name);
472 vptr += strlen (o_options[i].name);
476 for (i = 0; binary_o_options[i].name; i++)
477 if (GET_BINARY_O_OPTION_VALUE (i, binary_o_options[i].name))
479 strcpy (value + vptr, binary_o_options[i].name);
480 vptr += strlen (binary_o_options[i].name);
484 vptr--; /* cut off trailing colon */
487 v = find_variable ("SHELLOPTS");
489 /* Turn off the read-only attribute so we can bind the new value, and
490 note whether or not the variable was exported. */
493 VUNSETATTR (v, att_readonly);
494 exported = exported_p (v);
499 v = bind_variable ("SHELLOPTS", value);
501 /* Turn the read-only attribute back on, and turn off the export attribute
502 if it was set implicitly by mark_modified_vars and SHELLOPTS was not
503 exported before we bound the new value. */
504 VSETATTR (v, att_readonly);
505 if (mark_modified_vars && exported == 0 && exported_p (v))
506 VUNSETATTR (v, att_exported);
512 parse_shellopts (value)
519 while (vname = extract_colon_unit (value, &vptr))
521 set_minus_o_option (FLAG_ON, vname);
527 initialize_shell_options (no_shellopts)
533 if (no_shellopts == 0)
535 var = find_variable ("SHELLOPTS");
536 /* set up any shell options we may have inherited. */
537 if (var && imported_p (var))
539 temp = (array_p (var)) ? (char *)NULL : savestring (value_cell (var));
542 parse_shellopts (temp);
548 /* Set up the $SHELLOPTS variable. */
552 /* Reset the values of the -o options that are not also shell flags. This is
553 called from execute_cmd.c:initialize_subshell() when setting up a subshell
554 to run an executable shell script without a leading `#!'. */
556 reset_shell_options ()
558 #if defined (HISTORY)
559 remember_on_history = 1;
564 /* Set some flags from the word values in the input list. If LIST is empty,
565 then print out the values of the variables instead. If LIST contains
566 non-flags, then set $1 - $9 to the successive words of LIST. */
571 int on_or_off, flag_name, force_assignment, opts_changed;
577 print_all_shell_variables ();
578 return (EXECUTION_SUCCESS);
581 /* Check validity of flag arguments. */
582 if (*list->word->word == '-' || *list->word->word == '+')
584 for (l = list; l && (arg = l->word->word); l = l->next)
588 if (arg[0] != '-' && arg[0] != '+')
591 /* `-' or `--' signifies end of flag arguments. */
592 if (arg[0] == '-' && (!arg[1] || (arg[1] == '-' && !arg[2])))
597 if (find_flag (c) == FLAG_UNKNOWN && c != 'o')
600 s[0] = c; s[1] = '\0';
604 return (c == '?' ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
610 /* Do the set command. While the list consists of words starting with
611 '-' or '+' treat them as flags, otherwise, start assigning them to
613 for (force_assignment = opts_changed = 0; list; )
615 arg = list->word->word;
617 /* If the argument is `--' or `-' then signal the end of the list
618 and remember the remaining arguments. */
619 if (arg[0] == '-' && (!arg[1] || (arg[1] == '-' && !arg[2])))
623 /* `set --' unsets the positional parameters. */
625 force_assignment = 1;
627 /* Until told differently, the old shell behaviour of
628 `set - [arg ...]' being equivalent to `set +xv [arg ...]'
629 stands. Posix.2 says the behaviour is marked as obsolescent. */
632 change_flag ('x', '+');
633 change_flag ('v', '+');
640 if ((on_or_off = *arg) && (on_or_off == '-' || on_or_off == '+'))
642 while (flag_name = *++arg)
644 if (flag_name == '?')
647 return (EXECUTION_SUCCESS);
649 else if (flag_name == 'o') /* -+o option-name */
658 list_minus_o_opts (-1, (on_or_off == '+'));
662 option_name = opt->word->word;
664 if (option_name == 0 || *option_name == '\0' ||
665 *option_name == '-' || *option_name == '+')
667 list_minus_o_opts (-1, (on_or_off == '+'));
670 list = list->next; /* Skip over option name. */
673 if (set_minus_o_option (on_or_off, option_name) != EXECUTION_SUCCESS)
676 return (EXECUTION_FAILURE);
679 else if (change_flag (flag_name, on_or_off) == FLAG_ERROR)
688 return (EXECUTION_FAILURE);
700 /* Assigning $1 ... $n */
701 if (list || force_assignment)
702 remember_args (list, 1);
703 /* Set up new value of $SHELLOPTS */
706 return (EXECUTION_SUCCESS);
710 $FUNCTION unset_builtin
711 $SHORT_DOC unset [-f] [-v] [name ...]
712 For each NAME, remove the corresponding variable or function. Given
713 the `-v', unset will only act on variables. Given the `-f' flag,
714 unset will only act on functions. With neither flag, unset first
715 tries to unset a variable, and if that fails, then tries to unset a
716 function. Some variables cannot be unset; also see readonly.
719 #define NEXT_VARIABLE() any_failed++; list = list->next; continue;
725 int unset_function, unset_variable, unset_array, opt, any_failed;
728 unset_function = unset_variable = unset_array = any_failed = 0;
730 reset_internal_getopt ();
731 while ((opt = internal_getopt (list, "fv")) != -1)
749 if (unset_function && unset_variable)
751 builtin_error ("cannot simultaneously unset a function and a variable");
752 return (EXECUTION_FAILURE);
759 #if defined (ARRAY_VARS)
763 name = list->word->word;
765 #if defined (ARRAY_VARS)
767 if (!unset_function && valid_array_reference (name))
769 t = strchr (name, '[');
775 /* Bash allows functions with names which are not valid identifiers
776 to be created when not in posix mode, so check only when in posix
777 mode when unsetting a function. */
778 if (((unset_function && posixly_correct) || !unset_function) && legal_identifier (name) == 0)
780 builtin_error ("`%s': not a valid identifier", name);
784 var = unset_function ? find_function (name) : find_variable (name);
786 if (var && !unset_function && non_unsettable_p (var))
788 builtin_error ("%s: cannot unset", name);
792 /* Posix.2 says that unsetting readonly variables is an error. */
793 if (var && readonly_p (var))
795 builtin_error ("%s: cannot unset: readonly %s",
796 name, unset_function ? "function" : "variable");
800 /* Unless the -f option is supplied, the name refers to a variable. */
801 #if defined (ARRAY_VARS)
802 if (var && unset_array)
804 if (array_p (var) == 0)
806 builtin_error ("%s: not an array variable", name);
810 tem = unbind_array_element (var, t);
813 #endif /* ARRAY_VARS */
814 tem = makunbound (name, unset_function ? shell_functions : shell_variables);
816 /* This is what Posix.2 draft 11+ says. ``If neither -f nor -v
817 is specified, the name refers to a variable; if a variable by
818 that name does not exist, a function by that name, if any,
820 if (tem == -1 && !unset_function && !unset_variable)
821 tem = makunbound (name, shell_functions);
825 else if (!unset_function)
826 stupidly_hack_special_variables (name);
831 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);