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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
26 #if defined (HAVE_UNISTD_H)
32 #include "../bashansi.h"
37 #include "bashgetopt.h"
39 #if defined (READLINE)
40 # include "../input.h"
41 # include "../bashline.h"
42 # include <readline/readline.h>
46 # include "../bashhist.h"
49 extern int interactive;
50 extern int noclobber, posixly_correct, ignoreeof, eof_encountered_limit;
51 #if defined (READLINE)
52 extern int rl_editing_mode, no_line_editing;
57 $SHORT_DOC set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
58 -a Mark variables which are modified or created for export.
59 -b Notify of job termination immediately.
60 -e Exit immediately if a command exits with a non-zero status.
61 -f Disable file name generation (globbing).
62 -h Remember the location of commands as they are looked up.
63 -i Force the shell to be an "interactive" one. Interactive shells
64 always read `~/.bashrc' on startup.
65 -k All assignment arguments are placed in the environment for a
66 command, not just those that precede the command name.
67 -m Job control is enabled.
68 -n Read commands but do not execute them.
70 Set the variable corresponding to option-name:
72 braceexpand same as -B
73 #if defined (READLINE)
74 emacs use an emacs-style line editing interface
78 #if defined (BANG_HISTORY)
80 #endif /* BANG_HISTORY */
82 history enable command history
84 ignoreeof the shell will not exit upon reading EOF
86 allow comments to appear in interactive commands
96 posix change the behavior of bash where the default
97 operation differs from the 1003.2 standard to
101 #if defined (READLINE)
102 vi use a vi-style line editing interface
103 #endif /* READLINE */
105 -p Turned on whenever the real and effective user ids do not match.
106 Disables processing of the $ENV file and importing of shell
107 functions. Turning this option off causes the effective uid and
108 gid to be set to the real uid and gid.
109 -t Exit after reading and executing one command.
110 -u Treat unset variables as an error when substituting.
111 -v Print shell input lines as they are read.
112 -x Print commands and their arguments as they are executed.
113 #if defined (BRACE_EXPANSION)
114 -B the shell will perform brace expansion
115 #endif /* BRACE_EXPANSION */
116 -C If set, disallow existing regular files to be overwritten
117 by redirection of output.
118 #if defined (BANG_HISTORY)
119 -H Enable ! style history substitution. This flag is on
121 #endif /* BANG_HISTORY */
122 -P If set, do not follow symbolic links when executing commands
123 such as cd which change the current directory.
125 Using + rather than - causes these flags to be turned off. The
126 flags can also be used upon invocation of the shell. The current
127 set of flags may be found in $-. The remaining n ARGs are positional
128 parameters and are assigned, in order, to $1, $2, .. $n. If no
129 ARGs are given, all shell variables are printed.
132 static int set_ignoreeof ();
133 static int set_posix_mode ();
135 #if defined (READLINE)
136 static int set_edit_mode ();
137 static int get_edit_mode ();
140 #if defined (HISTORY)
141 static int bash_set_history ();
144 static char *on = "on";
145 static char *off = "off";
147 /* An a-list used to match long options for set -o to the corresponding
153 { "allexport", 'a' },
154 #if defined (BRACE_EXPANSION)
155 { "braceexpand",'B' },
159 #if defined (BANG_HISTORY)
160 { "histexpand", 'H' },
161 #endif /* BANG_HISTORY */
164 { "noclobber", 'C' },
167 #if defined (JOB_CONTROL)
169 #endif /* JOB_CONTROL */
173 { "privileged", 'p' },
184 } binary_o_options[] = {
185 #if defined (HISTORY)
186 { "history", &remember_on_history, bash_set_history, (Function *)NULL },
188 { "ignoreeof", &ignoreeof, set_ignoreeof, (Function *)NULL },
189 { "interactive-comments", &interactive_comments, (Function *)NULL, (Function *)NULL },
190 { "posix", &posixly_correct, set_posix_mode, (Function *)NULL },
191 #if defined (READLINE)
192 { "emacs", (int *)NULL, set_edit_mode, get_edit_mode },
193 { "vi", (int *)NULL, set_edit_mode, get_edit_mode },
195 { (char *)NULL, (int *)NULL }
198 #define GET_BINARY_O_OPTION_VALUE(i, name) \
199 ((binary_o_options[i].get_func) ? (*binary_o_options[i].get_func) (name) \
200 : (*binary_o_options[i].variable))
202 #define SET_BINARY_O_OPTION_VALUE(i, onoff, name) \
203 ((binary_o_options[i].set_func) ? (*binary_o_options[i].set_func) (onoff, name) \
204 : (*binary_o_options[i].variable = (onoff == FLAG_ON)))
207 minus_o_option_value (name)
213 for (i = 0; o_options[i].name; i++)
215 if (STREQ (name, o_options[i].name))
217 on_or_off = find_flag (o_options[i].letter);
218 return ((on_or_off == FLAG_UNKNOWN) ? -1 : *on_or_off);
221 for (i = 0; binary_o_options[i].name; i++)
223 if (STREQ (name, binary_o_options[i].name))
224 return (GET_BINARY_O_OPTION_VALUE (i, name));
230 #define MINUS_O_FORMAT "%-15s\t%s\n"
233 list_minus_o_opts (mode)
237 int *on_or_off, value;
239 for (value = i = 0; o_options[i].name; i++)
241 on_or_off = find_flag (o_options[i].letter);
242 if (on_or_off == FLAG_UNKNOWN)
244 if (mode == -1 || mode == *on_or_off)
245 printf (MINUS_O_FORMAT, o_options[i].name, *on_or_off ? on : off);
247 for (i = 0; binary_o_options[i].name; i++)
249 value = GET_BINARY_O_OPTION_VALUE (i, binary_o_options[i].name);
250 if (mode == -1 || mode == value)
251 printf (MINUS_O_FORMAT, binary_o_options[i].name, value ? on : off);
256 minus_o_option_commands ()
259 int *on_or_off, value;
261 for (value = i = 0; o_options[i].name; i++)
263 on_or_off = find_flag (o_options[i].letter);
264 if (on_or_off == FLAG_UNKNOWN)
266 printf ("set %co %s\n", *on_or_off ? '-' : '+', o_options[i].name);
268 for (i = 0; binary_o_options[i].name; i++)
270 value = GET_BINARY_O_OPTION_VALUE (i, binary_o_options[i].name);
271 printf ("set %co %s\n", value ? '-' : '+', binary_o_options[i].name);
276 set_ignoreeof (on_or_off, option_name)
280 ignoreeof = on_or_off == FLAG_ON;
281 unbind_variable ("ignoreeof");
283 bind_variable ("IGNOREEOF", "10");
285 unbind_variable ("IGNOREEOF");
286 sv_ignoreeof ("IGNOREEOF");
291 set_posix_mode (on_or_off, option_name)
295 posixly_correct = on_or_off == FLAG_ON;
296 if (posixly_correct == 0)
297 unbind_variable ("POSIXLY_CORRECT");
299 bind_variable ("POSIXLY_CORRECT", "y");
300 sv_strict_posix ("POSIXLY_CORRECT");
304 #if defined (READLINE)
305 /* Magic. This code `knows' how readline handles rl_editing_mode. */
307 set_edit_mode (on_or_off, option_name)
313 if (on_or_off == FLAG_ON)
315 rl_variable_bind ("editing-mode", option_name);
318 with_input_from_stdin ();
323 isemacs = rl_editing_mode == 1;
324 if ((isemacs && *option_name == 'e') || (!isemacs && *option_name == 'v'))
327 with_input_from_stream (stdin, "stdin");
331 return 1-no_line_editing;
338 return (*name == 'e' ? no_line_editing == 0 && rl_editing_mode == 1
339 : no_line_editing == 0 && rl_editing_mode == 0);
341 #endif /* READLINE */
343 #if defined (HISTORY)
345 bash_set_history (on_or_off, option_name)
349 if (on_or_off == FLAG_ON)
351 bash_history_enable ();
352 if (history_lines_this_session == 0)
356 bash_history_disable ();
357 return (1 - remember_on_history);
362 set_minus_o_option (on_or_off, option_name)
370 for (i = 0; binary_o_options[i].name; i++)
372 if (STREQ (option_name, binary_o_options[i].name))
374 SET_BINARY_O_OPTION_VALUE (i, on_or_off, option_name);
375 return (EXECUTION_SUCCESS);
379 for (i = 0, option_char = -1, set_func = 0; o_options[i].name; i++)
381 if (STREQ (option_name, o_options[i].name))
383 option_char = o_options[i].letter;
387 if (option_char == -1)
389 builtin_error ("%s: unknown option name", option_name);
390 return (EXECUTION_FAILURE);
392 if (change_flag (option_char, on_or_off) == FLAG_ERROR)
394 bad_option (option_name);
395 return (EXECUTION_FAILURE);
397 return (EXECUTION_SUCCESS);
401 print_all_shell_variables ()
405 vars = all_shell_variables ();
408 print_var_list (vars);
412 vars = all_shell_functions ();
415 print_var_list (vars);
424 int vsize, i, vptr, *ip;
427 for (vsize = i = 0; o_options[i].name; i++)
429 ip = find_flag (o_options[i].letter);
431 vsize += strlen (o_options[i].name) + 1;
433 for (i = 0; binary_o_options[i].name; i++)
434 if (GET_BINARY_O_OPTION_VALUE (i, binary_o_options[i].name))
435 vsize += strlen (binary_o_options[i].name) + 1;
437 value = xmalloc (vsize + 1);
439 for (i = vptr = 0; o_options[i].name; i++)
441 ip = find_flag (o_options[i].letter);
444 strcpy (value + vptr, o_options[i].name);
445 vptr += strlen (o_options[i].name);
449 for (i = 0; binary_o_options[i].name; i++)
450 if (GET_BINARY_O_OPTION_VALUE (i, binary_o_options[i].name))
452 strcpy (value + vptr, binary_o_options[i].name);
453 vptr += strlen (binary_o_options[i].name);
457 vptr--; /* cut off trailing colon */
460 v = find_variable ("SHELLOPTS");
462 v->attributes &= ~att_readonly;
463 v = bind_variable ("SHELLOPTS", value);
464 v->attributes |= att_readonly;
470 parse_shellopts (value)
477 while (vname = extract_colon_unit (value, &vptr))
479 set_minus_o_option (FLAG_ON, vname);
485 initialize_shell_options ()
490 var = find_variable ("SHELLOPTS");
491 /* set up any shell options we may have inherited. */
492 if (var && imported_p (var))
494 temp = (array_p (var)) ? (char *)NULL : savestring (value_cell (var));
497 parse_shellopts (temp);
502 /* Set up the $SHELLOPTS variable. */
506 /* Reset the values of the -o options that are not also shell flags. */
508 reset_shell_options ()
510 #if defined (HISTORY)
511 remember_on_history = 1;
513 ignoreeof = posixly_correct = 0;
516 /* Set some flags from the word values in the input list. If LIST is empty,
517 then print out the values of the variables instead. If LIST contains
518 non-flags, then set $1 - $9 to the successive words of LIST. */
523 int on_or_off, flag_name, force_assignment, opts_changed;
529 print_all_shell_variables ();
530 return (EXECUTION_SUCCESS);
533 /* Check validity of flag arguments. */
534 if (*list->word->word == '-' || *list->word->word == '+')
536 for (l = list; l && (arg = l->word->word); l = l->next)
540 if (arg[0] != '-' && arg[0] != '+')
543 /* `-' or `--' signifies end of flag arguments. */
544 if (arg[0] == '-' && (!arg[1] || (arg[1] == '-' && !arg[2])))
549 if (find_flag (c) == FLAG_UNKNOWN && c != 'o')
552 s[0] = c; s[1] = '\0';
556 return (c == '?' ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
562 /* Do the set command. While the list consists of words starting with
563 '-' or '+' treat them as flags, otherwise, start assigning them to
565 for (force_assignment = opts_changed = 0; list; )
567 arg = list->word->word;
569 /* If the argument is `--' or `-' then signal the end of the list
570 and remember the remaining arguments. */
571 if (arg[0] == '-' && (!arg[1] || (arg[1] == '-' && !arg[2])))
575 /* `set --' unsets the positional parameters. */
577 force_assignment = 1;
579 /* Until told differently, the old shell behaviour of
580 `set - [arg ...]' being equivalent to `set +xv [arg ...]'
581 stands. Posix.2 says the behaviour is marked as obsolescent. */
584 change_flag ('x', '+');
585 change_flag ('v', '+');
592 if ((on_or_off = *arg) && (on_or_off == '-' || on_or_off == '+'))
594 while (flag_name = *++arg)
596 if (flag_name == '?')
599 return (EXECUTION_SUCCESS);
601 else if (flag_name == 'o') /* -+o option-name */
610 if (on_or_off == '-')
611 list_minus_o_opts (-1);
613 minus_o_option_commands ();
617 option_name = opt->word->word;
619 if (option_name == 0 || *option_name == '\0' ||
620 *option_name == '-' || *option_name == '+')
622 if (on_or_off == '-')
623 list_minus_o_opts (-1);
625 minus_o_option_commands ();
628 list = list->next; /* Skip over option name. */
631 if (set_minus_o_option (on_or_off, option_name) != EXECUTION_SUCCESS)
634 return (EXECUTION_FAILURE);
637 else if (change_flag (flag_name, on_or_off) == FLAG_ERROR)
646 return (EXECUTION_FAILURE);
658 /* Assigning $1 ... $n */
659 if (list || force_assignment)
660 remember_args (list, 1);
661 /* Set up new value of $SHELLOPTS */
664 return (EXECUTION_SUCCESS);
668 $FUNCTION unset_builtin
669 $SHORT_DOC unset [-f] [-v] [name ...]
670 For each NAME, remove the corresponding variable or function. Given
671 the `-v', unset will only act on variables. Given the `-f' flag,
672 unset will only act on functions. With neither flag, unset first
673 tries to unset a variable, and if that fails, then tries to unset a
674 function. Some variables (such as PATH and IFS) cannot be unset; also
678 #define NEXT_VARIABLE() any_failed++; list = list->next; continue;
684 int unset_function, unset_variable, unset_array, opt, any_failed;
687 unset_function = unset_variable = unset_array = any_failed = 0;
689 reset_internal_getopt ();
690 while ((opt = internal_getopt (list, "fv")) != -1)
708 if (unset_function && unset_variable)
710 builtin_error ("cannot simultaneously unset a function and a variable");
711 return (EXECUTION_FAILURE);
718 #if defined (ARRAY_VARS)
722 name = list->word->word;
724 #if defined (ARRAY_VARS)
725 if (!unset_function && valid_array_reference (name))
727 t = strchr (name, '[');
733 if (legal_identifier (name) == 0)
735 builtin_error ("`%s': not a valid identifier", name);
739 var = unset_function ? find_function (name) : find_variable (name);
741 if (var && !unset_function && non_unsettable_p (var))
743 builtin_error ("%s: cannot unset", name);
747 /* Posix.2 says that unsetting readonly variables is an error. */
748 if (var && readonly_p (var))
750 builtin_error ("%s: cannot unset: readonly %s",
751 name, unset_function ? "function" : "variable");
755 /* Unless the -f option is supplied, the name refers to a variable. */
756 #if defined (ARRAY_VARS)
757 if (var && unset_array)
759 if (array_p (var) == 0)
761 builtin_error ("%s: not an array variable", name);
765 tem = unbind_array_element (var, t);
768 #endif /* ARRAY_VARS */
769 tem = makunbound (name, unset_function ? shell_functions : shell_variables);
771 /* This is what Posix.2 draft 11+ says. ``If neither -f nor -v
772 is specified, the name refers to a variable; if a variable by
773 that name does not exist, a function by that name, if any,
775 if (tem == -1 && !unset_function && !unset_variable)
776 tem = makunbound (name, shell_functions);
780 else if (!unset_function)
781 stupidly_hack_special_variables (name);
786 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);