Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is set.def, from which is created set.c. |
| 2 | It implements the "set" and "unset" builtins in Bash. |
| 3 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4 | Copyright (C) 1987-2012 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES set.c |
| 22 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 23 | #include <config.h> |
| 24 | |
| 25 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 26 | # ifdef _MINIX |
| 27 | # include <sys/types.h> |
| 28 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 29 | # include <unistd.h> |
| 30 | #endif |
| 31 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 33 | |
| 34 | #include "../bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 35 | #include "../bashintl.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 36 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 37 | #include "../shell.h" |
| 38 | #include "../flags.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 39 | #include "common.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 40 | #include "bashgetopt.h" |
| 41 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 42 | #if defined (READLINE) |
| 43 | # include "../input.h" |
| 44 | # include "../bashline.h" |
| 45 | # include <readline/readline.h> |
| 46 | #endif |
| 47 | |
| 48 | #if defined (HISTORY) |
| 49 | # include "../bashhist.h" |
| 50 | #endif |
| 51 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 52 | extern int posixly_correct, ignoreeof, eof_encountered_limit; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 53 | #if defined (HISTORY) |
| 54 | extern int dont_save_function_defs; |
| 55 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 56 | #if defined (READLINE) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 57 | extern int no_line_editing; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 58 | #endif /* READLINE */ |
| 59 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 60 | $BUILTIN set |
| 61 | $FUNCTION set_builtin |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 62 | $SHORT_DOC set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 63 | Set or unset values of shell options and positional parameters. |
| 64 | |
| 65 | Change the value of shell attributes and positional parameters, or |
| 66 | display the names and values of shell variables. |
| 67 | |
| 68 | Options: |
| 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. |
| 78 | -o option-name |
| 79 | Set the variable corresponding to option-name: |
| 80 | allexport same as -a |
| 81 | braceexpand same as -B |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 82 | #if defined (READLINE) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 83 | emacs use an emacs-style line editing interface |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 84 | #endif /* READLINE */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 85 | errexit same as -e |
| 86 | errtrace same as -E |
| 87 | functrace same as -T |
| 88 | hashall same as -h |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 89 | #if defined (BANG_HISTORY) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 90 | histexpand same as -H |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 91 | #endif /* BANG_HISTORY */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 92 | #if defined (HISTORY) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 93 | history enable command history |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 94 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 95 | ignoreeof the shell will not exit upon reading EOF |
| 96 | interactive-comments |
| 97 | allow comments to appear in interactive commands |
| 98 | keyword same as -k |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 99 | #if defined (JOB_CONTROL) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 100 | monitor same as -m |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 101 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 102 | noclobber same as -C |
| 103 | noexec same as -n |
| 104 | noglob same as -f |
| 105 | nolog currently accepted but ignored |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 106 | #if defined (JOB_CONTROL) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 107 | notify same as -b |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 108 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 109 | nounset same as -u |
| 110 | onecmd same as -t |
| 111 | physical same as -P |
| 112 | pipefail the return value of a pipeline is the status of |
| 113 | the last command to exit with a non-zero status, |
| 114 | or zero if no command exited with a non-zero status |
| 115 | posix change the behavior of bash where the default |
| 116 | operation differs from the Posix standard to |
| 117 | match the standard |
| 118 | privileged same as -p |
| 119 | verbose same as -v |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 120 | #if defined (READLINE) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 121 | vi use a vi-style line editing interface |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 122 | #endif /* READLINE */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 123 | xtrace same as -x |
| 124 | -p Turned on whenever the real and effective user ids do not match. |
| 125 | Disables processing of the $ENV file and importing of shell |
| 126 | functions. Turning this option off causes the effective uid and |
| 127 | gid to be set to the real uid and gid. |
| 128 | -t Exit after reading and executing one command. |
| 129 | -u Treat unset variables as an error when substituting. |
| 130 | -v Print shell input lines as they are read. |
| 131 | -x Print commands and their arguments as they are executed. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 132 | #if defined (BRACE_EXPANSION) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 133 | -B the shell will perform brace expansion |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 134 | #endif /* BRACE_EXPANSION */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 135 | -C If set, disallow existing regular files to be overwritten |
| 136 | by redirection of output. |
| 137 | -E If set, the ERR trap is inherited by shell functions. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 138 | #if defined (BANG_HISTORY) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 139 | -H Enable ! style history substitution. This flag is on |
| 140 | by default when the shell is interactive. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 141 | #endif /* BANG_HISTORY */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 142 | -P If set, do not resolve symbolic links when executing commands |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 143 | such as cd which change the current directory. |
| 144 | -T If set, the DEBUG trap is inherited by shell functions. |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 145 | -- Assign any remaining arguments to the positional parameters. |
| 146 | If there are no remaining arguments, the positional parameters |
| 147 | are unset. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 148 | - Assign any remaining arguments to the positional parameters. |
| 149 | The -x and -v options are turned off. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 150 | |
| 151 | Using + rather than - causes these flags to be turned off. The |
| 152 | flags can also be used upon invocation of the shell. The current |
| 153 | set of flags may be found in $-. The remaining n ARGs are positional |
| 154 | parameters and are assigned, in order, to $1, $2, .. $n. If no |
| 155 | ARGs are given, all shell variables are printed. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 156 | |
| 157 | Exit Status: |
| 158 | Returns success unless an invalid option is given. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 159 | $END |
| 160 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 161 | typedef int setopt_set_func_t __P((int, char *)); |
| 162 | typedef int setopt_get_func_t __P((char *)); |
| 163 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 164 | static void print_minus_o_option __P((char *, int, int)); |
| 165 | static void print_all_shell_variables __P((void)); |
| 166 | |
| 167 | static int set_ignoreeof __P((int, char *)); |
| 168 | static int set_posix_mode __P((int, char *)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 169 | |
| 170 | #if defined (READLINE) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 171 | static int set_edit_mode __P((int, char *)); |
| 172 | static int get_edit_mode __P((char *)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 173 | #endif |
| 174 | |
| 175 | #if defined (HISTORY) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 176 | static int bash_set_history __P((int, char *)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 177 | #endif |
| 178 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 179 | static const char * const on = "on"; |
| 180 | static const char * const off = "off"; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 181 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 182 | /* A struct used to match long options for set -o to the corresponding |
| 183 | option letter or internal variable. The functions can be called to |
| 184 | dynamically generate values. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 185 | const struct { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 186 | char *name; |
| 187 | int letter; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 188 | int *variable; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 189 | setopt_set_func_t *set_func; |
| 190 | setopt_get_func_t *get_func; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 191 | } o_options[] = { |
| 192 | { "allexport", 'a', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 193 | #if defined (BRACE_EXPANSION) |
| 194 | { "braceexpand",'B', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 195 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 196 | #if defined (READLINE) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 197 | { "emacs", '\0', (int *)NULL, set_edit_mode, get_edit_mode }, |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 198 | #endif |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 199 | { "errexit", 'e', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 200 | { "errtrace", 'E', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 201 | { "functrace", 'T', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 202 | { "hashall", 'h', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 203 | #if defined (BANG_HISTORY) |
| 204 | { "histexpand", 'H', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 205 | #endif /* BANG_HISTORY */ |
| 206 | #if defined (HISTORY) |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 207 | { "history", '\0', &enable_history_list, bash_set_history, (setopt_get_func_t *)NULL }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 208 | #endif |
| 209 | { "ignoreeof", '\0', &ignoreeof, set_ignoreeof, (setopt_get_func_t *)NULL }, |
| 210 | { "interactive-comments", '\0', &interactive_comments, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 211 | { "keyword", 'k', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 212 | #if defined (JOB_CONTROL) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 213 | { "monitor", 'm', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 214 | #endif |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 215 | { "noclobber", 'C', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 216 | { "noexec", 'n', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 217 | { "noglob", 'f', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 218 | #if defined (HISTORY) |
| 219 | { "nolog", '\0', &dont_save_function_defs, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 220 | #endif |
| 221 | #if defined (JOB_CONTROL) |
| 222 | { "notify", 'b', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 223 | #endif /* JOB_CONTROL */ |
| 224 | { "nounset", 'u', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 225 | { "onecmd", 't', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 226 | { "physical", 'P', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 227 | { "pipefail", '\0', &pipefail_opt, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 228 | { "posix", '\0', &posixly_correct, set_posix_mode, (setopt_get_func_t *)NULL }, |
| 229 | { "privileged", 'p', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 230 | { "verbose", 'v', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 231 | #if defined (READLINE) |
| 232 | { "vi", '\0', (int *)NULL, set_edit_mode, get_edit_mode }, |
| 233 | #endif |
| 234 | { "xtrace", 'x', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
| 235 | {(char *)NULL, 0 , (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 238 | #define N_O_OPTIONS (sizeof (o_options) / sizeof (o_options[0])) |
| 239 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 240 | #define GET_BINARY_O_OPTION_VALUE(i, name) \ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 241 | ((o_options[i].get_func) ? (*o_options[i].get_func) (name) \ |
| 242 | : (*o_options[i].variable)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 243 | |
| 244 | #define SET_BINARY_O_OPTION_VALUE(i, onoff, name) \ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 245 | ((o_options[i].set_func) ? (*o_options[i].set_func) (onoff, name) \ |
| 246 | : (*o_options[i].variable = (onoff == FLAG_ON))) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 247 | |
| 248 | int |
| 249 | minus_o_option_value (name) |
| 250 | char *name; |
| 251 | { |
| 252 | register int i; |
| 253 | int *on_or_off; |
| 254 | |
| 255 | for (i = 0; o_options[i].name; i++) |
| 256 | { |
| 257 | if (STREQ (name, o_options[i].name)) |
| 258 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 259 | if (o_options[i].letter) |
| 260 | { |
| 261 | on_or_off = find_flag (o_options[i].letter); |
| 262 | return ((on_or_off == FLAG_UNKNOWN) ? -1 : *on_or_off); |
| 263 | } |
| 264 | else |
| 265 | return (GET_BINARY_O_OPTION_VALUE (i, name)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 268 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 269 | return (-1); |
| 270 | } |
| 271 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 272 | #define MINUS_O_FORMAT "%-15s\t%s\n" |
| 273 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 274 | static void |
| 275 | print_minus_o_option (name, value, pflag) |
| 276 | char *name; |
| 277 | int value, pflag; |
| 278 | { |
| 279 | if (pflag == 0) |
| 280 | printf (MINUS_O_FORMAT, name, value ? on : off); |
| 281 | else |
| 282 | printf ("set %co %s\n", value ? '-' : '+', name); |
| 283 | } |
| 284 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 285 | void |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 286 | list_minus_o_opts (mode, reusable) |
| 287 | int mode, reusable; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 288 | { |
| 289 | register int i; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 290 | int *on_or_off, value; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 291 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 292 | for (i = 0; o_options[i].name; i++) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 293 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 294 | if (o_options[i].letter) |
| 295 | { |
| 296 | value = 0; |
| 297 | on_or_off = find_flag (o_options[i].letter); |
| 298 | if (on_or_off == FLAG_UNKNOWN) |
| 299 | on_or_off = &value; |
| 300 | if (mode == -1 || mode == *on_or_off) |
| 301 | print_minus_o_option (o_options[i].name, *on_or_off, reusable); |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | value = GET_BINARY_O_OPTION_VALUE (i, o_options[i].name); |
| 306 | if (mode == -1 || mode == value) |
| 307 | print_minus_o_option (o_options[i].name, value, reusable); |
| 308 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 312 | char ** |
| 313 | get_minus_o_opts () |
| 314 | { |
| 315 | char **ret; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 316 | int i; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 317 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 318 | ret = strvec_create (N_O_OPTIONS + 1); |
| 319 | for (i = 0; o_options[i].name; i++) |
| 320 | ret[i] = o_options[i].name; |
| 321 | ret[i] = (char *)NULL; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 322 | return ret; |
| 323 | } |
| 324 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 325 | static int |
| 326 | set_ignoreeof (on_or_off, option_name) |
| 327 | int on_or_off; |
| 328 | char *option_name; |
| 329 | { |
| 330 | ignoreeof = on_or_off == FLAG_ON; |
| 331 | unbind_variable ("ignoreeof"); |
| 332 | if (ignoreeof) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 333 | bind_variable ("IGNOREEOF", "10", 0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 334 | else |
| 335 | unbind_variable ("IGNOREEOF"); |
| 336 | sv_ignoreeof ("IGNOREEOF"); |
| 337 | return 0; |
| 338 | } |
| 339 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 340 | static int |
| 341 | set_posix_mode (on_or_off, option_name) |
| 342 | int on_or_off; |
| 343 | char *option_name; |
| 344 | { |
| 345 | posixly_correct = on_or_off == FLAG_ON; |
| 346 | if (posixly_correct == 0) |
| 347 | unbind_variable ("POSIXLY_CORRECT"); |
| 348 | else |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 349 | bind_variable ("POSIXLY_CORRECT", "y", 0); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 350 | sv_strict_posix ("POSIXLY_CORRECT"); |
| 351 | return (0); |
| 352 | } |
| 353 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 354 | #if defined (READLINE) |
| 355 | /* Magic. This code `knows' how readline handles rl_editing_mode. */ |
| 356 | static int |
| 357 | set_edit_mode (on_or_off, option_name) |
| 358 | int on_or_off; |
| 359 | char *option_name; |
| 360 | { |
| 361 | int isemacs; |
| 362 | |
| 363 | if (on_or_off == FLAG_ON) |
| 364 | { |
| 365 | rl_variable_bind ("editing-mode", option_name); |
| 366 | |
| 367 | if (interactive) |
| 368 | with_input_from_stdin (); |
| 369 | no_line_editing = 0; |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | isemacs = rl_editing_mode == 1; |
| 374 | if ((isemacs && *option_name == 'e') || (!isemacs && *option_name == 'v')) |
| 375 | { |
| 376 | if (interactive) |
| 377 | with_input_from_stream (stdin, "stdin"); |
| 378 | no_line_editing = 1; |
| 379 | } |
| 380 | } |
| 381 | return 1-no_line_editing; |
| 382 | } |
| 383 | |
| 384 | static int |
| 385 | get_edit_mode (name) |
| 386 | char *name; |
| 387 | { |
| 388 | return (*name == 'e' ? no_line_editing == 0 && rl_editing_mode == 1 |
| 389 | : no_line_editing == 0 && rl_editing_mode == 0); |
| 390 | } |
| 391 | #endif /* READLINE */ |
| 392 | |
| 393 | #if defined (HISTORY) |
| 394 | static int |
| 395 | bash_set_history (on_or_off, option_name) |
| 396 | int on_or_off; |
| 397 | char *option_name; |
| 398 | { |
| 399 | if (on_or_off == FLAG_ON) |
| 400 | { |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 401 | enable_history_list = 1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 402 | bash_history_enable (); |
| 403 | if (history_lines_this_session == 0) |
| 404 | load_history (); |
| 405 | } |
| 406 | else |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 407 | { |
| 408 | enable_history_list = 0; |
| 409 | bash_history_disable (); |
| 410 | } |
| 411 | return (1 - enable_history_list); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 412 | } |
| 413 | #endif |
| 414 | |
| 415 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 416 | set_minus_o_option (on_or_off, option_name) |
| 417 | int on_or_off; |
| 418 | char *option_name; |
| 419 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 420 | register int i; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 421 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 422 | for (i = 0; o_options[i].name; i++) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 423 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 424 | if (STREQ (option_name, o_options[i].name)) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 425 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 426 | if (o_options[i].letter == 0) |
| 427 | { |
| 428 | SET_BINARY_O_OPTION_VALUE (i, on_or_off, option_name); |
| 429 | return (EXECUTION_SUCCESS); |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | if (change_flag (o_options[i].letter, on_or_off) == FLAG_ERROR) |
| 434 | { |
| 435 | sh_invalidoptname (option_name); |
| 436 | return (EXECUTION_FAILURE); |
| 437 | } |
| 438 | else |
| 439 | return (EXECUTION_SUCCESS); |
| 440 | } |
| 441 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 442 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 443 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 444 | |
| 445 | sh_invalidoptname (option_name); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 446 | return (EX_USAGE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 449 | static void |
| 450 | print_all_shell_variables () |
| 451 | { |
| 452 | SHELL_VAR **vars; |
| 453 | |
| 454 | vars = all_shell_variables (); |
| 455 | if (vars) |
| 456 | { |
| 457 | print_var_list (vars); |
| 458 | free (vars); |
| 459 | } |
| 460 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 461 | /* POSIX.2 does not allow function names and definitions to be output when |
| 462 | `set' is invoked without options (PASC Interp #202). */ |
| 463 | if (posixly_correct == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 464 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 465 | vars = all_shell_functions (); |
| 466 | if (vars) |
| 467 | { |
| 468 | print_func_list (vars); |
| 469 | free (vars); |
| 470 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
| 474 | void |
| 475 | set_shellopts () |
| 476 | { |
| 477 | char *value; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 478 | char tflag[N_O_OPTIONS]; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 479 | int vsize, i, vptr, *ip, exported; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 480 | SHELL_VAR *v; |
| 481 | |
| 482 | for (vsize = i = 0; o_options[i].name; i++) |
| 483 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 484 | tflag[i] = 0; |
| 485 | if (o_options[i].letter) |
| 486 | { |
| 487 | ip = find_flag (o_options[i].letter); |
| 488 | if (ip && *ip) |
| 489 | { |
| 490 | vsize += strlen (o_options[i].name) + 1; |
| 491 | tflag[i] = 1; |
| 492 | } |
| 493 | } |
| 494 | else if (GET_BINARY_O_OPTION_VALUE (i, o_options[i].name)) |
| 495 | { |
| 496 | vsize += strlen (o_options[i].name) + 1; |
| 497 | tflag[i] = 1; |
| 498 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 499 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 500 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 501 | value = (char *)xmalloc (vsize + 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 502 | |
| 503 | for (i = vptr = 0; o_options[i].name; i++) |
| 504 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 505 | if (tflag[i]) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 506 | { |
| 507 | strcpy (value + vptr, o_options[i].name); |
| 508 | vptr += strlen (o_options[i].name); |
| 509 | value[vptr++] = ':'; |
| 510 | } |
| 511 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 512 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 513 | if (vptr) |
| 514 | vptr--; /* cut off trailing colon */ |
| 515 | value[vptr] = '\0'; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 516 | |
| 517 | v = find_variable ("SHELLOPTS"); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 518 | |
| 519 | /* Turn off the read-only attribute so we can bind the new value, and |
| 520 | note whether or not the variable was exported. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 521 | if (v) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 522 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 523 | VUNSETATTR (v, att_readonly); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 524 | exported = exported_p (v); |
| 525 | } |
| 526 | else |
| 527 | exported = 0; |
| 528 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 529 | v = bind_variable ("SHELLOPTS", value, 0); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 530 | |
| 531 | /* Turn the read-only attribute back on, and turn off the export attribute |
| 532 | if it was set implicitly by mark_modified_vars and SHELLOPTS was not |
| 533 | exported before we bound the new value. */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 534 | VSETATTR (v, att_readonly); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 535 | if (mark_modified_vars && exported == 0 && exported_p (v)) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 536 | VUNSETATTR (v, att_exported); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 537 | |
| 538 | free (value); |
| 539 | } |
| 540 | |
| 541 | void |
| 542 | parse_shellopts (value) |
| 543 | char *value; |
| 544 | { |
| 545 | char *vname; |
| 546 | int vptr; |
| 547 | |
| 548 | vptr = 0; |
| 549 | while (vname = extract_colon_unit (value, &vptr)) |
| 550 | { |
| 551 | set_minus_o_option (FLAG_ON, vname); |
| 552 | free (vname); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | void |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 557 | initialize_shell_options (no_shellopts) |
| 558 | int no_shellopts; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 559 | { |
| 560 | char *temp; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 561 | SHELL_VAR *var; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 562 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 563 | if (no_shellopts == 0) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 564 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 565 | var = find_variable ("SHELLOPTS"); |
| 566 | /* set up any shell options we may have inherited. */ |
| 567 | if (var && imported_p (var)) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 568 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 569 | temp = (array_p (var) || assoc_p (var)) ? (char *)NULL : savestring (value_cell (var)); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 570 | if (temp) |
| 571 | { |
| 572 | parse_shellopts (temp); |
| 573 | free (temp); |
| 574 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 577 | |
| 578 | /* Set up the $SHELLOPTS variable. */ |
| 579 | set_shellopts (); |
| 580 | } |
| 581 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 582 | /* Reset the values of the -o options that are not also shell flags. This is |
| 583 | called from execute_cmd.c:initialize_subshell() when setting up a subshell |
| 584 | to run an executable shell script without a leading `#!'. */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 585 | void |
| 586 | reset_shell_options () |
| 587 | { |
| 588 | #if defined (HISTORY) |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 589 | remember_on_history = enable_history_list = 1; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 590 | #endif |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 591 | ignoreeof = 0; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 594 | /* Set some flags from the word values in the input list. If LIST is empty, |
| 595 | then print out the values of the variables instead. If LIST contains |
| 596 | non-flags, then set $1 - $9 to the successive words of LIST. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 597 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 598 | set_builtin (list) |
| 599 | WORD_LIST *list; |
| 600 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 601 | int on_or_off, flag_name, force_assignment, opts_changed, rv, r; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 602 | register char *arg; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 603 | char s[3]; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 604 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 605 | if (list == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 606 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 607 | print_all_shell_variables (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 608 | return (sh_chkwrite (EXECUTION_SUCCESS)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* Check validity of flag arguments. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 612 | rv = EXECUTION_SUCCESS; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 613 | reset_internal_getopt (); |
| 614 | while ((flag_name = internal_getopt (list, optflags)) != -1) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 615 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 616 | switch (flag_name) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 617 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 618 | case '?': |
| 619 | builtin_usage (); |
| 620 | return (list_optopt == '?' ? EXECUTION_SUCCESS : EX_USAGE); |
| 621 | default: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 622 | break; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 623 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 624 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 625 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 626 | /* Do the set command. While the list consists of words starting with |
| 627 | '-' or '+' treat them as flags, otherwise, start assigning them to |
| 628 | $1 ... $n. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 629 | for (force_assignment = opts_changed = 0; list; ) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 630 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 631 | arg = list->word->word; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 632 | |
| 633 | /* If the argument is `--' or `-' then signal the end of the list |
| 634 | and remember the remaining arguments. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 635 | if (arg[0] == '-' && (!arg[1] || (arg[1] == '-' && !arg[2]))) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 636 | { |
| 637 | list = list->next; |
| 638 | |
| 639 | /* `set --' unsets the positional parameters. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 640 | if (arg[1] == '-') |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 641 | force_assignment = 1; |
| 642 | |
| 643 | /* Until told differently, the old shell behaviour of |
| 644 | `set - [arg ...]' being equivalent to `set +xv [arg ...]' |
| 645 | stands. Posix.2 says the behaviour is marked as obsolescent. */ |
| 646 | else |
| 647 | { |
| 648 | change_flag ('x', '+'); |
| 649 | change_flag ('v', '+'); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 650 | opts_changed = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | break; |
| 654 | } |
| 655 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 656 | if ((on_or_off = *arg) && (on_or_off == '-' || on_or_off == '+')) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 657 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 658 | while (flag_name = *++arg) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 659 | { |
| 660 | if (flag_name == '?') |
| 661 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 662 | builtin_usage (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 663 | return (EXECUTION_SUCCESS); |
| 664 | } |
| 665 | else if (flag_name == 'o') /* -+o option-name */ |
| 666 | { |
| 667 | char *option_name; |
| 668 | WORD_LIST *opt; |
| 669 | |
| 670 | opt = list->next; |
| 671 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 672 | if (opt == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 673 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 674 | list_minus_o_opts (-1, (on_or_off == '+')); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 675 | rv = sh_chkwrite (rv); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 676 | continue; |
| 677 | } |
| 678 | |
| 679 | option_name = opt->word->word; |
| 680 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 681 | if (option_name == 0 || *option_name == '\0' || |
| 682 | *option_name == '-' || *option_name == '+') |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 683 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 684 | list_minus_o_opts (-1, (on_or_off == '+')); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 685 | continue; |
| 686 | } |
| 687 | list = list->next; /* Skip over option name. */ |
| 688 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 689 | opts_changed = 1; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 690 | if ((r = set_minus_o_option (on_or_off, option_name)) != EXECUTION_SUCCESS) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 691 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 692 | set_shellopts (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 693 | return (r); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 696 | else if (change_flag (flag_name, on_or_off) == FLAG_ERROR) |
| 697 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 698 | s[0] = on_or_off; |
| 699 | s[1] = flag_name; |
| 700 | s[2] = '\0'; |
| 701 | sh_invalidopt (s); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 702 | builtin_usage (); |
| 703 | set_shellopts (); |
| 704 | return (EXECUTION_FAILURE); |
| 705 | } |
| 706 | opts_changed = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | else |
| 710 | { |
| 711 | break; |
| 712 | } |
| 713 | list = list->next; |
| 714 | } |
| 715 | |
| 716 | /* Assigning $1 ... $n */ |
| 717 | if (list || force_assignment) |
| 718 | remember_args (list, 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 719 | /* Set up new value of $SHELLOPTS */ |
| 720 | if (opts_changed) |
| 721 | set_shellopts (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 722 | return (rv); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | $BUILTIN unset |
| 726 | $FUNCTION unset_builtin |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 727 | $SHORT_DOC unset [-f] [-v] [-n] [name ...] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 728 | Unset values and attributes of shell variables and functions. |
| 729 | |
| 730 | For each NAME, remove the corresponding variable or function. |
| 731 | |
| 732 | Options: |
| 733 | -f treat each NAME as a shell function |
| 734 | -v treat each NAME as a shell variable |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 735 | -n treat each NAME as a name reference and unset the variable itself |
| 736 | rather than the variable it references |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 737 | |
| 738 | Without options, unset first tries to unset a variable, and if that fails, |
| 739 | tries to unset a function. |
| 740 | |
| 741 | Some variables cannot be unset; also see `readonly'. |
| 742 | |
| 743 | Exit Status: |
| 744 | Returns success unless an invalid option is given or a NAME is read-only. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 745 | $END |
| 746 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 747 | #define NEXT_VARIABLE() any_failed++; list = list->next; continue; |
| 748 | |
| 749 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 750 | unset_builtin (list) |
| 751 | WORD_LIST *list; |
| 752 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 753 | int unset_function, unset_variable, unset_array, opt, nameref, any_failed; |
Chet Ramey | bb1e05a | 2015-05-19 14:55:02 -0400 | [diff] [blame] | 754 | int global_unset_func, global_unset_var; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 755 | char *name; |
| 756 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 757 | unset_function = unset_variable = unset_array = nameref = any_failed = 0; |
Chet Ramey | bb1e05a | 2015-05-19 14:55:02 -0400 | [diff] [blame] | 758 | global_unset_func = global_unset_var = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 759 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 760 | reset_internal_getopt (); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 761 | while ((opt = internal_getopt (list, "fnv")) != -1) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 762 | { |
| 763 | switch (opt) |
| 764 | { |
| 765 | case 'f': |
Chet Ramey | bb1e05a | 2015-05-19 14:55:02 -0400 | [diff] [blame] | 766 | global_unset_func = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 767 | break; |
| 768 | case 'v': |
Chet Ramey | bb1e05a | 2015-05-19 14:55:02 -0400 | [diff] [blame] | 769 | global_unset_var = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 770 | break; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 771 | case 'n': |
| 772 | nameref = 1; |
| 773 | break; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 774 | default: |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 775 | builtin_usage (); |
| 776 | return (EX_USAGE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
| 780 | list = loptend; |
| 781 | |
Chet Ramey | bb1e05a | 2015-05-19 14:55:02 -0400 | [diff] [blame] | 782 | if (global_unset_func && global_unset_var) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 783 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 784 | builtin_error (_("cannot simultaneously unset a function and a variable")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 785 | return (EXECUTION_FAILURE); |
| 786 | } |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 787 | else if (unset_function && nameref) |
| 788 | nameref = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 789 | |
| 790 | while (list) |
| 791 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 792 | SHELL_VAR *var; |
| 793 | int tem; |
| 794 | #if defined (ARRAY_VARS) |
| 795 | char *t; |
| 796 | #endif |
| 797 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 798 | name = list->word->word; |
| 799 | |
Chet Ramey | bb1e05a | 2015-05-19 14:55:02 -0400 | [diff] [blame] | 800 | unset_function = global_unset_func; |
| 801 | unset_variable = global_unset_var; |
| 802 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 803 | #if defined (ARRAY_VARS) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 804 | unset_array = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 805 | if (!unset_function && valid_array_reference (name)) |
| 806 | { |
| 807 | t = strchr (name, '['); |
| 808 | *t++ = '\0'; |
| 809 | unset_array++; |
| 810 | } |
| 811 | #endif |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 812 | /* Get error checking out of the way first. The low-level functions |
| 813 | just perform the unset, relying on the caller to verify. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 814 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 815 | /* Bash allows functions with names which are not valid identifiers |
| 816 | to be created when not in posix mode, so check only when in posix |
| 817 | mode when unsetting a function. */ |
| 818 | if (((unset_function && posixly_correct) || !unset_function) && legal_identifier (name) == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 819 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 820 | sh_invalidid (name); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 821 | NEXT_VARIABLE (); |
| 822 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 823 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 824 | /* Only search for functions here if -f supplied. */ |
| 825 | var = unset_function ? find_function (name) |
| 826 | : (nameref ? find_variable_last_nameref (name) : find_variable (name)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 827 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 828 | /* Some variables (but not functions yet) cannot be unset, period. */ |
| 829 | if (var && unset_function == 0 && non_unsettable_p (var)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 830 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 831 | builtin_error (_("%s: cannot unset"), name); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 832 | NEXT_VARIABLE (); |
| 833 | } |
| 834 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 835 | /* Posix.2 says try variables first, then functions. If we would |
| 836 | find a function after unsuccessfully searching for a variable, |
| 837 | note that we're acting on a function now as if -f were |
| 838 | supplied. The readonly check below takes care of it. */ |
| 839 | if (var == 0 && unset_variable == 0 && unset_function == 0) |
| 840 | { |
| 841 | if (var = find_function (name)) |
| 842 | unset_function = 1; |
| 843 | } |
| 844 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 845 | /* Posix.2 says that unsetting readonly variables is an error. */ |
| 846 | if (var && readonly_p (var)) |
| 847 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 848 | builtin_error (_("%s: cannot unset: readonly %s"), |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 849 | var->name, unset_function ? "function" : "variable"); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 850 | NEXT_VARIABLE (); |
| 851 | } |
| 852 | |
| 853 | /* Unless the -f option is supplied, the name refers to a variable. */ |
| 854 | #if defined (ARRAY_VARS) |
| 855 | if (var && unset_array) |
| 856 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 857 | if (array_p (var) == 0 && assoc_p (var) == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 858 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 859 | builtin_error (_("%s: not an array variable"), var->name); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 860 | NEXT_VARIABLE (); |
| 861 | } |
| 862 | else |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 863 | { |
| 864 | tem = unbind_array_element (var, t); |
| 865 | if (tem == -1) |
| 866 | any_failed++; |
| 867 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 868 | } |
| 869 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 870 | #endif /* ARRAY_VARS */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 871 | tem = unset_function ? unbind_func (name) : (nameref ? unbind_nameref (name) : unbind_variable (name)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 872 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 873 | /* This is what Posix.2 says: ``If neither -f nor -v |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 874 | is specified, the name refers to a variable; if a variable by |
| 875 | that name does not exist, a function by that name, if any, |
| 876 | shall be unset.'' */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 877 | if (tem == -1 && unset_function == 0 && unset_variable == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 878 | tem = unbind_func (name); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 879 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 880 | /* SUSv3, POSIX.1-2001 say: ``Unsetting a variable or function that |
| 881 | was not previously set shall not be considered an error.'' */ |
| 882 | |
| 883 | if (unset_function == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 884 | stupidly_hack_special_variables (name); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 885 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 886 | list = list->next; |
| 887 | } |
| 888 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 889 | return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 890 | } |