| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1 | /* shell.c -- GNU's idea of the POSIX shell specification. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 2 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3 | /* Copyright (C) 1987-2012 Free Software Foundation, Inc. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 6 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 7 | Bash is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 11 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 12 | Bash is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 16 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 17 | You should have received a copy of the GNU General Public License |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 21 | /* |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 22 | Birthdate: |
| 23 | Sunday, January 10th, 1988. |
| 24 | Initial author: Brian Fox |
| 25 | */ |
| 26 | #define INSTALL_DEBUG_MODE |
| 27 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 28 | #include "config.h" |
| 29 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 30 | #include "bashtypes.h" |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 31 | #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 32 | # include <sys/file.h> |
| 33 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 34 | #include "posixstat.h" |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 35 | #include "posixtime.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 36 | #include "bashansi.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 37 | #include <stdio.h> |
| 38 | #include <signal.h> |
| 39 | #include <errno.h> |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 40 | #include "filecntl.h" |
| 41 | #include <pwd.h> |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 42 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 43 | #if defined (HAVE_UNISTD_H) |
| 44 | # include <unistd.h> |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 47 | #include "bashintl.h" |
| 48 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 49 | #define NEED_SH_SETLINEBUF_DECL /* used in externs.h */ |
| 50 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 51 | #include "shell.h" |
| 52 | #include "flags.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 53 | #include "trap.h" |
| 54 | #include "mailcheck.h" |
| 55 | #include "builtins.h" |
| 56 | #include "builtins/common.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 57 | |
| 58 | #if defined (JOB_CONTROL) |
| 59 | #include "jobs.h" |
| 60 | #endif /* JOB_CONTROL */ |
| 61 | |
| 62 | #include "input.h" |
| 63 | #include "execute_cmd.h" |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 64 | #include "findcmd.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 65 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 66 | #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS) |
| 67 | # include <malloc/shmalloc.h> |
| 68 | #endif |
| 69 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 70 | #if defined (HISTORY) |
| 71 | # include "bashhist.h" |
| 72 | # include <readline/history.h> |
| 73 | #endif |
| 74 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 75 | #if defined (READLINE) |
| Chet Ramey | 84c617e | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 76 | # include <readline/readline.h> |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 77 | # include "bashline.h" |
| 78 | #endif |
| 79 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 80 | #include <tilde/tilde.h> |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 81 | #include <glob/strmatch.h> |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 82 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 83 | #if defined (__OPENNT) |
| 84 | # include <opennt/opennt.h> |
| 85 | #endif |
| 86 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 87 | #if !defined (HAVE_GETPW_DECLS) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 88 | extern struct passwd *getpwuid (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 89 | #endif /* !HAVE_GETPW_DECLS */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 90 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 91 | #if !defined (errno) |
| 92 | extern int errno; |
| 93 | #endif |
| 94 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 95 | #if defined (NO_MAIN_ENV_ARG) |
| 96 | extern char **environ; /* used if no third argument to main() */ |
| 97 | #endif |
| 98 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 99 | extern char *dist_version, *release_status; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 100 | extern int patch_level, build_version; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 101 | extern int shell_level; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 102 | extern int subshell_environment; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 103 | extern int last_command_exit_value; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 104 | extern int line_number; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 105 | extern int expand_aliases; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 106 | extern int array_needs_making; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 107 | extern int gnu_error_format; |
| 108 | extern char *primary_prompt, *secondary_prompt; |
| 109 | extern char *this_command_name; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 110 | |
| 111 | /* Non-zero means that this shell has already been run; i.e. you should |
| 112 | call shell_reinitialize () if you need to start afresh. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 113 | int shell_initialized = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 114 | |
| 115 | COMMAND *global_command = (COMMAND *)NULL; |
| 116 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 117 | /* Information about the current user. */ |
| 118 | struct user_info current_user = |
| 119 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 120 | (uid_t)-1, (uid_t)-1, (gid_t)-1, (gid_t)-1, |
| 121 | (char *)NULL, (char *)NULL, (char *)NULL |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | /* The current host's name. */ |
| 125 | char *current_host_name = (char *)NULL; |
| 126 | |
| 127 | /* Non-zero means that this shell is a login shell. |
| 128 | Specifically: |
| 129 | 0 = not login shell. |
| 130 | 1 = login shell from getty (or equivalent fake out) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 131 | -1 = login shell from "--login" (or -l) flag. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 132 | -2 = both from getty, and from flag. |
| 133 | */ |
| 134 | int login_shell = 0; |
| 135 | |
| 136 | /* Non-zero means that at this moment, the shell is interactive. In |
| 137 | general, this means that the shell is at this moment reading input |
| 138 | from the keyboard. */ |
| 139 | int interactive = 0; |
| 140 | |
| 141 | /* Non-zero means that the shell was started as an interactive shell. */ |
| 142 | int interactive_shell = 0; |
| 143 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 144 | /* Non-zero means to send a SIGHUP to all jobs when an interactive login |
| 145 | shell exits. */ |
| 146 | int hup_on_exit = 0; |
| 147 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 148 | /* Non-zero means to list status of running and stopped jobs at shell exit */ |
| 149 | int check_jobs_at_exit = 0; |
| 150 | |
| 151 | /* Non-zero means to change to a directory name supplied as a command name */ |
| 152 | int autocd = 0; |
| 153 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 154 | /* Tells what state the shell was in when it started: |
| 155 | 0 = non-interactive shell script |
| 156 | 1 = interactive |
| 157 | 2 = -c command |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 158 | 3 = wordexp evaluation |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 159 | This is a superset of the information provided by interactive_shell. |
| 160 | */ |
| 161 | int startup_state = 0; |
| 162 | |
| 163 | /* Special debugging helper. */ |
| 164 | int debugging_login_shell = 0; |
| 165 | |
| 166 | /* The environment that the shell passes to other commands. */ |
| 167 | char **shell_environment; |
| 168 | |
| 169 | /* Non-zero when we are executing a top-level command. */ |
| 170 | int executing = 0; |
| 171 | |
| 172 | /* The number of commands executed so far. */ |
| 173 | int current_command_number = 1; |
| 174 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 175 | /* Non-zero is the recursion depth for commands. */ |
| 176 | int indirection_level = 0; |
| 177 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 178 | /* The name of this shell, as taken from argv[0]. */ |
| 179 | char *shell_name = (char *)NULL; |
| 180 | |
| 181 | /* time in seconds when the shell was started */ |
| 182 | time_t shell_start_time; |
| 183 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 184 | /* Are we running in an emacs shell window? */ |
| 185 | int running_under_emacs; |
| 186 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 187 | /* Do we have /dev/fd? */ |
| 188 | #ifdef HAVE_DEV_FD |
| 189 | int have_devfd = HAVE_DEV_FD; |
| 190 | #else |
| 191 | int have_devfd = 0; |
| 192 | #endif |
| 193 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 194 | /* The name of the .(shell)rc file. */ |
| 195 | static char *bashrc_file = "~/.bashrc"; |
| 196 | |
| 197 | /* Non-zero means to act more like the Bourne shell on startup. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 198 | static int act_like_sh; |
| 199 | |
| 200 | /* Non-zero if this shell is being run by `su'. */ |
| 201 | static int su_shell; |
| 202 | |
| 203 | /* Non-zero if we have already expanded and sourced $ENV. */ |
| 204 | static int sourced_env; |
| 205 | |
| 206 | /* Is this shell running setuid? */ |
| 207 | static int running_setuid; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 208 | |
| 209 | /* Values for the long-winded argument names. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 210 | static int debugging; /* Do debugging things. */ |
| 211 | static int no_rc; /* Don't execute ~/.bashrc */ |
| 212 | static int no_profile; /* Don't execute .profile */ |
| 213 | static int do_version; /* Display interesting version info. */ |
| 214 | static int make_login_shell; /* Make this shell be a `-bash' shell. */ |
| 215 | static int want_initial_help; /* --help option */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 216 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 217 | int debugging_mode = 0; /* In debugging mode with --debugger */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 218 | #if defined (READLINE) |
| 219 | int no_line_editing = 0; /* non-zero -> don't do fancy line editing. */ |
| 220 | #else |
| 221 | int no_line_editing = 1; /* can't have line editing without readline */ |
| 222 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 223 | int dump_translatable_strings; /* Dump strings in $"...", don't execute. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 224 | int dump_po_strings; /* Dump strings in $"..." in po format */ |
| 225 | int wordexp_only = 0; /* Do word expansion only */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 226 | int protected_mode = 0; /* No command substitution with --wordexp */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 227 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 228 | #if defined (STRICT_POSIX) |
| 229 | int posixly_correct = 1; /* Non-zero means posix.2 superset. */ |
| 230 | #else |
| 231 | int posixly_correct = 0; /* Non-zero means posix.2 superset. */ |
| 232 | #endif |
| 233 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 234 | /* Some long-winded argument names. These are obviously new. */ |
| 235 | #define Int 1 |
| 236 | #define Charp 2 |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 237 | static const struct { |
| 238 | const char *name; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 239 | int type; |
| 240 | int *int_value; |
| 241 | char **char_value; |
| 242 | } long_args[] = { |
| 243 | { "debug", Int, &debugging, (char **)0x0 }, |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 244 | #if defined (DEBUGGER) |
| 245 | { "debugger", Int, &debugging_mode, (char **)0x0 }, |
| 246 | #endif |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 247 | { "dump-po-strings", Int, &dump_po_strings, (char **)0x0 }, |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 248 | { "dump-strings", Int, &dump_translatable_strings, (char **)0x0 }, |
| 249 | { "help", Int, &want_initial_help, (char **)0x0 }, |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 250 | { "init-file", Charp, (int *)0x0, &bashrc_file }, |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 251 | { "login", Int, &make_login_shell, (char **)0x0 }, |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 252 | { "noediting", Int, &no_line_editing, (char **)0x0 }, |
| 253 | { "noprofile", Int, &no_profile, (char **)0x0 }, |
| 254 | { "norc", Int, &no_rc, (char **)0x0 }, |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 255 | { "posix", Int, &posixly_correct, (char **)0x0 }, |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 256 | #if defined (WORDEXP_OPTION) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 257 | { "protected", Int, &protected_mode, (char **)0x0 }, |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 258 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 259 | { "rcfile", Charp, (int *)0x0, &bashrc_file }, |
| 260 | #if defined (RESTRICTED_SHELL) |
| 261 | { "restricted", Int, &restricted, (char **)0x0 }, |
| 262 | #endif |
| 263 | { "verbose", Int, &echo_input_at_read, (char **)0x0 }, |
| 264 | { "version", Int, &do_version, (char **)0x0 }, |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 265 | #if defined (WORDEXP_OPTION) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 266 | { "wordexp", Int, &wordexp_only, (char **)0x0 }, |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 267 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 268 | { (char *)0x0, Int, (int *)0x0, (char **)0x0 } |
| 269 | }; |
| 270 | |
| 271 | /* These are extern so execute_simple_command can set them, and then |
| 272 | longjmp back to main to execute a shell script, instead of calling |
| 273 | main () again and resulting in indefinite, possibly fatal, stack |
| 274 | growth. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 275 | procenv_t subshell_top_level; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 276 | int subshell_argc; |
| 277 | char **subshell_argv; |
| 278 | char **subshell_envp; |
| 279 | |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 280 | char *exec_argv0; |
| 281 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 282 | #if defined (BUFFERED_INPUT) |
| 283 | /* The file descriptor from which the shell is reading input. */ |
| 284 | int default_buffered_input = -1; |
| 285 | #endif |
| 286 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 287 | /* The following two variables are not static so they can show up in $-. */ |
| 288 | int read_from_stdin; /* -s flag supplied */ |
| 289 | int want_pending_command; /* -c flag supplied */ |
| 290 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 291 | /* This variable is not static so it can be bound to $BASH_EXECUTION_STRING */ |
| 292 | char *command_execution_string; /* argument to -c option */ |
| 293 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 294 | int malloc_trace_at_exit = 0; |
| 295 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 296 | static int shell_reinitialized = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 297 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 298 | static FILE *default_input; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 299 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 300 | static STRING_INT_ALIST *shopt_alist; |
| 301 | static int shopt_ind = 0, shopt_len = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 302 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 303 | static int parse_long_options __P((char **, int, int)); |
| 304 | static int parse_shell_options __P((char **, int, int)); |
| 305 | static int bind_args __P((char **, int, int, int)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 306 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 307 | static void start_debugger __P((void)); |
| 308 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 309 | static void add_shopt_to_alist __P((char *, int)); |
| 310 | static void run_shopt_alist __P((void)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 311 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 312 | static void execute_env_file __P((char *)); |
| 313 | static void run_startup_files __P((void)); |
| 314 | static int open_shell_script __P((char *)); |
| 315 | static void set_bash_input __P((void)); |
| 316 | static int run_one_command __P((char *)); |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 317 | #if defined (WORDEXP_OPTION) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 318 | static int run_wordexp __P((char *)); |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 319 | #endif |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 320 | |
| 321 | static int uidget __P((void)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 322 | |
| 323 | static void init_interactive __P((void)); |
| 324 | static void init_noninteractive __P((void)); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 325 | static void init_interactive_script __P((void)); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 326 | |
| 327 | static void set_shell_name __P((char *)); |
| 328 | static void shell_initialize __P((void)); |
| 329 | static void shell_reinitialize __P((void)); |
| 330 | |
| 331 | static void show_shell_usage __P((FILE *, int)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 332 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 333 | #ifdef __CYGWIN__ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 334 | static void |
| 335 | _cygwin32_check_tmp () |
| 336 | { |
| 337 | struct stat sb; |
| 338 | |
| 339 | if (stat ("/tmp", &sb) < 0) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 340 | internal_warning (_("could not find /tmp, please create!")); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 341 | else |
| 342 | { |
| 343 | if (S_ISDIR (sb.st_mode) == 0) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 344 | internal_warning (_("/tmp must be a valid directory name")); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 347 | #endif /* __CYGWIN__ */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 348 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 349 | #if defined (NO_MAIN_ENV_ARG) |
| 350 | /* systems without third argument to main() */ |
| 351 | int |
| 352 | main (argc, argv) |
| 353 | int argc; |
| 354 | char **argv; |
| 355 | #else /* !NO_MAIN_ENV_ARG */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 356 | int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 357 | main (argc, argv, env) |
| 358 | int argc; |
| 359 | char **argv, **env; |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 360 | #endif /* !NO_MAIN_ENV_ARG */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 361 | { |
| 362 | register int i; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 363 | int code, old_errexit_flag; |
| 364 | #if defined (RESTRICTED_SHELL) |
| 365 | int saverst; |
| 366 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 367 | volatile int locally_skip_execution; |
| 368 | volatile int arg_index, top_level_arg_index; |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 369 | #ifdef __OPENNT |
| 370 | char **env; |
| 371 | |
| 372 | env = environ; |
| 373 | #endif /* __OPENNT */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 374 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 375 | USE_VAR(argc); |
| 376 | USE_VAR(argv); |
| 377 | USE_VAR(env); |
| 378 | USE_VAR(code); |
| 379 | USE_VAR(old_errexit_flag); |
| 380 | #if defined (RESTRICTED_SHELL) |
| 381 | USE_VAR(saverst); |
| 382 | #endif |
| 383 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 384 | /* Catch early SIGINTs. */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 385 | code = setjmp_nosigs (top_level); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 386 | if (code) |
| 387 | exit (2); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 388 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 389 | xtrace_init (); |
| 390 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 391 | #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS) |
| 392 | # if 1 |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 393 | malloc_set_register (1); |
| 394 | # endif |
| 395 | #endif |
| 396 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 397 | check_dev_tty (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 398 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 399 | #ifdef __CYGWIN__ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 400 | _cygwin32_check_tmp (); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 401 | #endif /* __CYGWIN__ */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 402 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 403 | /* Wait forever if we are debugging a login shell. */ |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 404 | while (debugging_login_shell) sleep (3); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 405 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 406 | set_default_locale (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 407 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 408 | running_setuid = uidget (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 409 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 410 | if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC")) |
| 411 | posixly_correct = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 412 | |
| 413 | #if defined (USE_GNU_MALLOC_LIBRARY) |
| 414 | mcheck (programming_error, (void (*) ())0); |
| 415 | #endif /* USE_GNU_MALLOC_LIBRARY */ |
| 416 | |
| 417 | if (setjmp (subshell_top_level)) |
| 418 | { |
| 419 | argc = subshell_argc; |
| 420 | argv = subshell_argv; |
| 421 | env = subshell_envp; |
| 422 | sourced_env = 0; |
| 423 | } |
| 424 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 425 | shell_reinitialized = 0; |
| 426 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 427 | /* Initialize `local' variables for all `invocations' of main (). */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 428 | arg_index = 1; |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 429 | if (arg_index > argc) |
| 430 | arg_index = argc; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 431 | command_execution_string = (char *)NULL; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 432 | want_pending_command = locally_skip_execution = read_from_stdin = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 433 | default_input = stdin; |
| 434 | #if defined (BUFFERED_INPUT) |
| 435 | default_buffered_input = -1; |
| 436 | #endif |
| 437 | |
| 438 | /* Fix for the `infinite process creation' bug when running shell scripts |
| 439 | from startup files on System V. */ |
| 440 | login_shell = make_login_shell = 0; |
| 441 | |
| 442 | /* If this shell has already been run, then reinitialize it to a |
| 443 | vanilla state. */ |
| 444 | if (shell_initialized || shell_name) |
| 445 | { |
| 446 | /* Make sure that we do not infinitely recurse as a login shell. */ |
| 447 | if (*shell_name == '-') |
| 448 | shell_name++; |
| 449 | |
| 450 | shell_reinitialize (); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 451 | if (setjmp_nosigs (top_level)) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 452 | exit (2); |
| 453 | } |
| 454 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 455 | shell_environment = env; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 456 | set_shell_name (argv[0]); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 457 | shell_start_time = NOW; /* NOW now defined in general.h */ |
| 458 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 459 | /* Parse argument flags from the input line. */ |
| 460 | |
| 461 | /* Find full word arguments first. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 462 | arg_index = parse_long_options (argv, arg_index, argc); |
| 463 | |
| 464 | if (want_initial_help) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 465 | { |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 466 | show_shell_usage (stdout, 1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 467 | exit (EXECUTION_SUCCESS); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 470 | if (do_version) |
| 471 | { |
| 472 | show_shell_version (1); |
| 473 | exit (EXECUTION_SUCCESS); |
| 474 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 475 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 476 | /* All done with full word options; do standard shell option parsing.*/ |
| 477 | this_command_name = shell_name; /* for error reporting */ |
| 478 | arg_index = parse_shell_options (argv, arg_index, argc); |
| 479 | |
| 480 | /* If user supplied the "--login" (or -l) flag, then set and invert |
| 481 | LOGIN_SHELL. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 482 | if (make_login_shell) |
| 483 | { |
| 484 | login_shell++; |
| 485 | login_shell = -login_shell; |
| 486 | } |
| 487 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 488 | set_login_shell ("login_shell", login_shell != 0); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 489 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 490 | if (dump_po_strings) |
| 491 | dump_translatable_strings = 1; |
| 492 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 493 | if (dump_translatable_strings) |
| 494 | read_but_dont_execute = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 495 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 496 | if (running_setuid && privileged_mode == 0) |
| 497 | disable_priv_mode (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 498 | |
| 499 | /* Need to get the argument to a -c option processed in the |
| 500 | above loop. The next arg is a command to execute, and the |
| 501 | following args are $0...$n respectively. */ |
| 502 | if (want_pending_command) |
| 503 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 504 | command_execution_string = argv[arg_index]; |
| 505 | if (command_execution_string == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 506 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 507 | report_error (_("%s: option requires an argument"), "-c"); |
| 508 | exit (EX_BADUSAGE); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 509 | } |
| 510 | arg_index++; |
| 511 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 512 | this_command_name = (char *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 513 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 514 | cmd_init(); /* initialize the command object caches */ |
| 515 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 516 | /* First, let the outside world know about our interactive status. |
| 517 | A shell is interactive if the `-i' flag was given, or if all of |
| 518 | the following conditions are met: |
| 519 | no -c command |
| 520 | no arguments remaining or the -s flag given |
| 521 | standard input is a terminal |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 522 | standard error is a terminal |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 523 | Refer to Posix.2, the description of the `sh' utility. */ |
| 524 | |
| 525 | if (forced_interactive || /* -i flag */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 526 | (!command_execution_string && /* No -c command and ... */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 527 | wordexp_only == 0 && /* No --wordexp and ... */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 528 | ((arg_index == argc) || /* no remaining args or... */ |
| 529 | read_from_stdin) && /* -s flag with args, and */ |
| 530 | isatty (fileno (stdin)) && /* Input is a terminal and */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 531 | isatty (fileno (stderr)))) /* error output is a terminal. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 532 | init_interactive (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 533 | else |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 534 | init_noninteractive (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 535 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 536 | /* |
| 537 | * Some systems have the bad habit of starting login shells with lots of open |
| 538 | * file descriptors. For instance, most systems that have picked up the |
| 539 | * pre-4.0 Sun YP code leave a file descriptor open each time you call one |
| 540 | * of the getpw* functions, and it's set to be open across execs. That |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 541 | * means one for login, one for xterm, one for shelltool, etc. There are |
| 542 | * also systems that open persistent FDs to other agents or files as part |
| 543 | * of process startup; these need to be set to be close-on-exec. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 544 | */ |
| 545 | if (login_shell && interactive_shell) |
| 546 | { |
| 547 | for (i = 3; i < 20; i++) |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 548 | SET_CLOSE_ON_EXEC (i); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 549 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 550 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 551 | /* If we're in a strict Posix.2 mode, turn on interactive comments, |
| 552 | alias expansion in non-interactive shells, and other Posix.2 things. */ |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 553 | if (posixly_correct) |
| 554 | { |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 555 | bind_variable ("POSIXLY_CORRECT", "y", 0); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 556 | sv_strict_posix ("POSIXLY_CORRECT"); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 559 | /* Now we run the shopt_alist and process the options. */ |
| 560 | if (shopt_alist) |
| 561 | run_shopt_alist (); |
| 562 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 563 | /* From here on in, the shell must be a normal functioning shell. |
| 564 | Variables from the environment are expected to be set, etc. */ |
| 565 | shell_initialize (); |
| 566 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 567 | set_default_lang (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 568 | set_default_locale_vars (); |
| 569 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 570 | /* |
| 571 | * M-x term -> TERM=eterm EMACS=22.1 (term:0.96) (eterm) |
| 572 | * M-x shell -> TERM=dumb EMACS=t (no line editing) |
| 573 | * M-x terminal -> TERM=emacs-em7955 EMACS= (line editing) |
| 574 | */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 575 | if (interactive_shell) |
| 576 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 577 | char *term, *emacs; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 578 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 579 | term = get_string_value ("TERM"); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 580 | emacs = get_string_value ("EMACS"); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 581 | |
| 582 | /* Not sure any emacs terminal emulator sets TERM=emacs any more */ |
| 583 | no_line_editing |= term && (STREQ (term, "emacs")); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 584 | no_line_editing |= emacs && emacs[0] == 't' && emacs[1] == '\0' && STREQ (term, "dumb"); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 585 | |
| 586 | /* running_under_emacs == 2 for `eterm' */ |
| 587 | running_under_emacs = (emacs != 0) || (term && STREQN (term, "emacs", 5)); |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 588 | running_under_emacs += term && STREQN (term, "eterm", 5) && emacs && strstr (emacs, "term"); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 589 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 590 | if (running_under_emacs) |
| 591 | gnu_error_format = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | top_level_arg_index = arg_index; |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 595 | old_errexit_flag = exit_immediately_on_error; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 596 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 597 | /* Give this shell a place to longjmp to before executing the |
| 598 | startup files. This allows users to press C-c to abort the |
| 599 | lengthy startup. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 600 | code = setjmp (top_level); |
| 601 | if (code) |
| 602 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 603 | if (code == EXITPROG || code == ERREXIT) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 604 | exit_shell (last_command_exit_value); |
| 605 | else |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 606 | { |
| 607 | #if defined (JOB_CONTROL) |
| 608 | /* Reset job control, since run_startup_files turned it off. */ |
| 609 | set_job_control (interactive_shell); |
| 610 | #endif |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 611 | /* Reset value of `set -e', since it's turned off before running |
| 612 | the startup files. */ |
| 613 | exit_immediately_on_error += old_errexit_flag; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 614 | locally_skip_execution++; |
| 615 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 616 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 617 | |
| 618 | arg_index = top_level_arg_index; |
| 619 | |
| 620 | /* Execute the start-up scripts. */ |
| 621 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 622 | if (interactive_shell == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 623 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 624 | unbind_variable ("PS1"); |
| 625 | unbind_variable ("PS2"); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 626 | interactive = 0; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 627 | #if 0 |
| 628 | /* This has already been done by init_noninteractive */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 629 | expand_aliases = posixly_correct; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 630 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 631 | } |
| 632 | else |
| 633 | { |
| 634 | change_flag ('i', FLAG_ON); |
| 635 | interactive = 1; |
| 636 | } |
| 637 | |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 638 | #if defined (RESTRICTED_SHELL) |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 639 | /* Set restricted_shell based on whether the basename of $0 indicates that |
| 640 | the shell should be restricted or if the `-r' option was supplied at |
| 641 | startup. */ |
| 642 | restricted_shell = shell_is_restricted (shell_name); |
| 643 | |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 644 | /* If the `-r' option is supplied at invocation, make sure that the shell |
| 645 | is not in restricted mode when running the startup files. */ |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 646 | saverst = restricted; |
| 647 | restricted = 0; |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 648 | #endif |
| 649 | |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 650 | /* The startup files are run with `set -e' temporarily disabled. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 651 | if (locally_skip_execution == 0 && running_setuid == 0) |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 652 | { |
| 653 | old_errexit_flag = exit_immediately_on_error; |
| 654 | exit_immediately_on_error = 0; |
| 655 | |
| 656 | run_startup_files (); |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 657 | exit_immediately_on_error += old_errexit_flag; |
| 658 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 659 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 660 | /* If we are invoked as `sh', turn on Posix mode. */ |
| 661 | if (act_like_sh) |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 662 | { |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 663 | bind_variable ("POSIXLY_CORRECT", "y", 0); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 664 | sv_strict_posix ("POSIXLY_CORRECT"); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 665 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 666 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 667 | #if defined (RESTRICTED_SHELL) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 668 | /* Turn on the restrictions after executing the startup files. This |
| Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 669 | means that `bash -r' or `set -r' invoked from a startup file will |
| 670 | turn on the restrictions after the startup files are executed. */ |
| 671 | restricted = saverst || restricted; |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 672 | if (shell_reinitialized == 0) |
| 673 | maybe_make_restricted (shell_name); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 674 | #endif /* RESTRICTED_SHELL */ |
| 675 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 676 | #if defined (WORDEXP_OPTION) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 677 | if (wordexp_only) |
| 678 | { |
| 679 | startup_state = 3; |
| 680 | last_command_exit_value = run_wordexp (argv[arg_index]); |
| 681 | exit_shell (last_command_exit_value); |
| 682 | } |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 683 | #endif |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 684 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 685 | if (command_execution_string) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 686 | { |
| 687 | arg_index = bind_args (argv, arg_index, argc, 0); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 688 | startup_state = 2; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 689 | |
| 690 | if (debugging_mode) |
| 691 | start_debugger (); |
| 692 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 693 | #if defined (ONESHOT) |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 694 | executing = 1; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 695 | run_one_command (command_execution_string); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 696 | exit_shell (last_command_exit_value); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 697 | #else /* ONESHOT */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 698 | with_input_from_string (command_execution_string, "-c"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 699 | goto read_and_execute; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 700 | #endif /* !ONESHOT */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /* Get possible input filename and set up default_buffered_input or |
| 704 | default_input as appropriate. */ |
| 705 | if (arg_index != argc && read_from_stdin == 0) |
| 706 | { |
| 707 | open_shell_script (argv[arg_index]); |
| 708 | arg_index++; |
| 709 | } |
| 710 | else if (interactive == 0) |
| 711 | /* In this mode, bash is reading a script from stdin, which is a |
| 712 | pipe or redirected file. */ |
| 713 | #if defined (BUFFERED_INPUT) |
| 714 | default_buffered_input = fileno (stdin); /* == 0 */ |
| 715 | #else |
| 716 | setbuf (default_input, (char *)NULL); |
| 717 | #endif /* !BUFFERED_INPUT */ |
| 718 | |
| 719 | set_bash_input (); |
| 720 | |
| 721 | /* Bind remaining args to $1 ... $n */ |
| 722 | arg_index = bind_args (argv, arg_index, argc, 1); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 723 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 724 | if (debugging_mode && locally_skip_execution == 0 && running_setuid == 0 && dollar_vars[1]) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 725 | start_debugger (); |
| 726 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 727 | /* Do the things that should be done only for interactive shells. */ |
| 728 | if (interactive_shell) |
| 729 | { |
| 730 | /* Set up for checking for presence of mail. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 731 | reset_mail_timer (); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 732 | init_mail_dates (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 733 | |
| 734 | #if defined (HISTORY) |
| 735 | /* Initialize the interactive history stuff. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 736 | bash_initialize_history (); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 737 | /* Don't load the history from the history file if we've already |
| 738 | saved some lines in this session (e.g., by putting `history -s xx' |
| 739 | into one of the startup files). */ |
| 740 | if (shell_initialized == 0 && history_lines_this_session == 0) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 741 | load_history (); |
| 742 | #endif /* HISTORY */ |
| 743 | |
| 744 | /* Initialize terminal state for interactive shells after the |
| 745 | .bash_profile and .bashrc are interpreted. */ |
| 746 | get_tty_state (); |
| 747 | } |
| 748 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 749 | #if !defined (ONESHOT) |
| 750 | read_and_execute: |
| 751 | #endif /* !ONESHOT */ |
| 752 | |
| 753 | shell_initialized = 1; |
| 754 | |
| 755 | /* Read commands until exit condition. */ |
| 756 | reader_loop (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 757 | exit_shell (last_command_exit_value); |
| 758 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 759 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 760 | static int |
| 761 | parse_long_options (argv, arg_start, arg_end) |
| 762 | char **argv; |
| 763 | int arg_start, arg_end; |
| 764 | { |
| 765 | int arg_index, longarg, i; |
| 766 | char *arg_string; |
| 767 | |
| 768 | arg_index = arg_start; |
| 769 | while ((arg_index != arg_end) && (arg_string = argv[arg_index]) && |
| 770 | (*arg_string == '-')) |
| 771 | { |
| 772 | longarg = 0; |
| 773 | |
| 774 | /* Make --login equivalent to -login. */ |
| 775 | if (arg_string[1] == '-' && arg_string[2]) |
| 776 | { |
| 777 | longarg = 1; |
| 778 | arg_string++; |
| 779 | } |
| 780 | |
| 781 | for (i = 0; long_args[i].name; i++) |
| 782 | { |
| 783 | if (STREQ (arg_string + 1, long_args[i].name)) |
| 784 | { |
| 785 | if (long_args[i].type == Int) |
| 786 | *long_args[i].int_value = 1; |
| 787 | else if (argv[++arg_index] == 0) |
| 788 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 789 | report_error (_("%s: option requires an argument"), long_args[i].name); |
| 790 | exit (EX_BADUSAGE); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 791 | } |
| 792 | else |
| 793 | *long_args[i].char_value = argv[arg_index]; |
| 794 | |
| 795 | break; |
| 796 | } |
| 797 | } |
| 798 | if (long_args[i].name == 0) |
| 799 | { |
| 800 | if (longarg) |
| 801 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 802 | report_error (_("%s: invalid option"), argv[arg_index]); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 803 | show_shell_usage (stderr, 0); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 804 | exit (EX_BADUSAGE); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 805 | } |
| 806 | break; /* No such argument. Maybe flag arg. */ |
| 807 | } |
| 808 | |
| 809 | arg_index++; |
| 810 | } |
| 811 | |
| 812 | return (arg_index); |
| 813 | } |
| 814 | |
| 815 | static int |
| 816 | parse_shell_options (argv, arg_start, arg_end) |
| 817 | char **argv; |
| 818 | int arg_start, arg_end; |
| 819 | { |
| 820 | int arg_index; |
| 821 | int arg_character, on_or_off, next_arg, i; |
| 822 | char *o_option, *arg_string; |
| 823 | |
| 824 | arg_index = arg_start; |
| 825 | while (arg_index != arg_end && (arg_string = argv[arg_index]) && |
| 826 | (*arg_string == '-' || *arg_string == '+')) |
| 827 | { |
| 828 | /* There are flag arguments, so parse them. */ |
| 829 | next_arg = arg_index + 1; |
| 830 | |
| 831 | /* A single `-' signals the end of options. From the 4.3 BSD sh. |
| 832 | An option `--' means the same thing; this is the standard |
| 833 | getopt(3) meaning. */ |
| 834 | if (arg_string[0] == '-' && |
| 835 | (arg_string[1] == '\0' || |
| 836 | (arg_string[1] == '-' && arg_string[2] == '\0'))) |
| 837 | return (next_arg); |
| 838 | |
| 839 | i = 1; |
| 840 | on_or_off = arg_string[0]; |
| 841 | while (arg_character = arg_string[i++]) |
| 842 | { |
| 843 | switch (arg_character) |
| 844 | { |
| 845 | case 'c': |
| 846 | want_pending_command = 1; |
| 847 | break; |
| 848 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 849 | case 'l': |
| 850 | make_login_shell = 1; |
| 851 | break; |
| 852 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 853 | case 's': |
| 854 | read_from_stdin = 1; |
| 855 | break; |
| 856 | |
| 857 | case 'o': |
| 858 | o_option = argv[next_arg]; |
| 859 | if (o_option == 0) |
| 860 | { |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 861 | list_minus_o_opts (-1, (on_or_off == '-') ? 0 : 1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 862 | break; |
| 863 | } |
| 864 | if (set_minus_o_option (on_or_off, o_option) != EXECUTION_SUCCESS) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 865 | exit (EX_BADUSAGE); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 866 | next_arg++; |
| 867 | break; |
| 868 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 869 | case 'O': |
| 870 | /* Since some of these can be overridden by the normal |
| 871 | interactive/non-interactive shell initialization or |
| 872 | initializing posix mode, we save the options and process |
| 873 | them after initialization. */ |
| 874 | o_option = argv[next_arg]; |
| 875 | if (o_option == 0) |
| 876 | { |
| 877 | shopt_listopt (o_option, (on_or_off == '-') ? 0 : 1); |
| 878 | break; |
| 879 | } |
| 880 | add_shopt_to_alist (o_option, on_or_off); |
| 881 | next_arg++; |
| 882 | break; |
| 883 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 884 | case 'D': |
| 885 | dump_translatable_strings = 1; |
| 886 | break; |
| 887 | |
| 888 | default: |
| 889 | if (change_flag (arg_character, on_or_off) == FLAG_ERROR) |
| 890 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 891 | report_error (_("%c%c: invalid option"), on_or_off, arg_character); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 892 | show_shell_usage (stderr, 0); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 893 | exit (EX_BADUSAGE); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | } |
| 897 | /* Can't do just a simple increment anymore -- what about |
| 898 | "bash -abouo emacs ignoreeof -hP"? */ |
| 899 | arg_index = next_arg; |
| 900 | } |
| 901 | |
| 902 | return (arg_index); |
| 903 | } |
| 904 | |
| 905 | /* Exit the shell with status S. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 906 | void |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 907 | exit_shell (s) |
| 908 | int s; |
| 909 | { |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 910 | fflush (stdout); /* XXX */ |
| 911 | fflush (stderr); |
| 912 | |
| Chet Ramey | 84c617e | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 913 | /* Clean up the terminal if we are in a state where it's been modified. */ |
| 914 | #if defined (READLINE) |
| 915 | if (RL_ISSTATE (RL_STATE_TERMPREPPED) && rl_deprep_term_function) |
| 916 | (*rl_deprep_term_function) (); |
| 917 | #endif |
| 918 | if (read_tty_modified ()) |
| 919 | read_tty_cleanup (); |
| 920 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 921 | /* Do trap[0] if defined. Allow it to override the exit status |
| 922 | passed to us. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 923 | if (signal_is_trapped (0)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 924 | s = run_exit_trap (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 925 | |
| 926 | #if defined (PROCESS_SUBSTITUTION) |
| 927 | unlink_fifo_list (); |
| 928 | #endif /* PROCESS_SUBSTITUTION */ |
| 929 | |
| 930 | #if defined (HISTORY) |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 931 | if (remember_on_history) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 932 | maybe_save_shell_history (); |
| 933 | #endif /* HISTORY */ |
| 934 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 935 | #if defined (COPROCESS_SUPPORT) |
| 936 | coproc_flush (); |
| 937 | #endif |
| 938 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 939 | #if defined (JOB_CONTROL) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 940 | /* If the user has run `shopt -s huponexit', hangup all jobs when we exit |
| 941 | an interactive login shell. ksh does this unconditionally. */ |
| 942 | if (interactive_shell && login_shell && hup_on_exit) |
| 943 | hangup_all_jobs (); |
| 944 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 945 | /* If this shell is interactive, terminate all stopped jobs and |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 946 | restore the original terminal process group. Don't do this if we're |
| 947 | in a subshell and calling exit_shell after, for example, a failed |
| 948 | word expansion. */ |
| 949 | if (subshell_environment == 0) |
| 950 | end_job_control (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 951 | #endif /* JOB_CONTROL */ |
| 952 | |
| 953 | /* Always return the exit status of the last command to our parent. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 954 | sh_exit (s); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 957 | /* A wrapper for exit that (optionally) can do other things, like malloc |
| 958 | statistics tracing. */ |
| 959 | void |
| 960 | sh_exit (s) |
| 961 | int s; |
| 962 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 963 | #if defined (MALLOC_DEBUG) && defined (USING_BASH_MALLOC) |
| 964 | if (malloc_trace_at_exit) |
| 965 | trace_malloc_stats (get_name_for_error (), (char *)NULL); |
| 966 | #endif |
| 967 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 968 | exit (s); |
| 969 | } |
| 970 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 971 | /* Exit a subshell, which includes calling the exit trap. We don't want to |
| 972 | do any more cleanup, since a subshell is created as an exact copy of its |
| 973 | parent. */ |
| 974 | void |
| 975 | subshell_exit (s) |
| 976 | int s; |
| 977 | { |
| 978 | fflush (stdout); |
| 979 | fflush (stderr); |
| 980 | |
| 981 | /* Do trap[0] if defined. Allow it to override the exit status |
| 982 | passed to us. */ |
| 983 | if (signal_is_trapped (0)) |
| 984 | s = run_exit_trap (); |
| 985 | |
| 986 | sh_exit (s); |
| 987 | } |
| 988 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 989 | /* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey |
| 990 | the Posix.2 startup file rules: $ENV is expanded, and if the file it |
| 991 | names exists, that file is sourced. The Posix.2 rules are in effect |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 992 | for interactive shells only. (section 4.56.5.3) */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 993 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 994 | /* Execute ~/.bashrc for most shells. Never execute it if |
| 995 | ACT_LIKE_SH is set, or if NO_RC is set. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 996 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 997 | If the executable file "/usr/gnu/src/bash/foo" contains: |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 998 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 999 | #!/usr/gnu/bin/bash |
| 1000 | echo hello |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1001 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1002 | then: |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1003 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1004 | COMMAND EXECUTE BASHRC |
| 1005 | -------------------------------- |
| 1006 | bash -c foo NO |
| 1007 | bash foo NO |
| 1008 | foo NO |
| 1009 | rsh machine ls YES (for rsh, which calls `bash -c') |
| 1010 | rsh machine foo YES (for shell started by rsh) NO (for foo!) |
| 1011 | echo ls | bash NO |
| 1012 | login NO |
| 1013 | bash YES |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1014 | */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1015 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1016 | static void |
| 1017 | execute_env_file (env_file) |
| 1018 | char *env_file; |
| 1019 | { |
| 1020 | char *fn; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1021 | |
| 1022 | if (env_file && *env_file) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1023 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1024 | fn = expand_string_unsplit_to_string (env_file, Q_DOUBLE_QUOTES); |
| 1025 | if (fn && *fn) |
| 1026 | maybe_execute_file (fn, 1); |
| 1027 | FREE (fn); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1031 | static void |
| 1032 | run_startup_files () |
| 1033 | { |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1034 | #if defined (JOB_CONTROL) |
| 1035 | int old_job_control; |
| 1036 | #endif |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1037 | int sourced_login, run_by_ssh; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1038 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1039 | /* get the rshd/sshd case out of the way first. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1040 | if (interactive_shell == 0 && no_rc == 0 && login_shell == 0 && |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1041 | act_like_sh == 0 && command_execution_string) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1042 | { |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1043 | #ifdef SSH_SOURCE_BASHRC |
| 1044 | run_by_ssh = (find_variable ("SSH_CLIENT") != (SHELL_VAR *)0) || |
| 1045 | (find_variable ("SSH2_CLIENT") != (SHELL_VAR *)0); |
| 1046 | #else |
| 1047 | run_by_ssh = 0; |
| 1048 | #endif |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1049 | |
| 1050 | /* If we were run by sshd or we think we were run by rshd, execute |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1051 | ~/.bashrc if we are a top-level shell. */ |
| 1052 | if ((run_by_ssh || isnetconn (fileno (stdin))) && shell_level < 2) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1053 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1054 | #ifdef SYS_BASHRC |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1055 | # if defined (__OPENNT) |
| 1056 | maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1); |
| 1057 | # else |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1058 | maybe_execute_file (SYS_BASHRC, 1); |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1059 | # endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1060 | #endif |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1061 | maybe_execute_file (bashrc_file, 1); |
| 1062 | return; |
| 1063 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1066 | #if defined (JOB_CONTROL) |
| 1067 | /* Startup files should be run without job control enabled. */ |
| 1068 | old_job_control = interactive_shell ? set_job_control (0) : 0; |
| 1069 | #endif |
| 1070 | |
| 1071 | sourced_login = 0; |
| 1072 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1073 | /* A shell begun with the --login (or -l) flag that is not in posix mode |
| 1074 | runs the login shell startup files, no matter whether or not it is |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1075 | interactive. If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the |
| 1076 | startup files if argv[0][0] == '-' as well. */ |
| 1077 | #if defined (NON_INTERACTIVE_LOGIN_SHELLS) |
| 1078 | if (login_shell && posixly_correct == 0) |
| 1079 | #else |
| 1080 | if (login_shell < 0 && posixly_correct == 0) |
| 1081 | #endif |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1082 | { |
| 1083 | /* We don't execute .bashrc for login shells. */ |
| 1084 | no_rc++; |
| 1085 | |
| 1086 | /* Execute /etc/profile and one of the personal login shell |
| 1087 | initialization files. */ |
| 1088 | if (no_profile == 0) |
| 1089 | { |
| 1090 | maybe_execute_file (SYS_PROFILE, 1); |
| 1091 | |
| 1092 | if (act_like_sh) /* sh */ |
| 1093 | maybe_execute_file ("~/.profile", 1); |
| 1094 | else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) && |
| 1095 | (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */ |
| 1096 | maybe_execute_file ("~/.profile", 1); |
| 1097 | } |
| 1098 | |
| 1099 | sourced_login = 1; |
| 1100 | } |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1101 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1102 | /* A non-interactive shell not named `sh' and not in posix mode reads and |
| 1103 | executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd' |
| 1104 | and `-su' as the name of the shell, we want to read the startup files. |
| 1105 | No other non-interactive shells read any startup files. */ |
| 1106 | if (interactive_shell == 0 && !(su_shell && login_shell)) |
| 1107 | { |
| 1108 | if (posixly_correct == 0 && act_like_sh == 0 && privileged_mode == 0 && |
| 1109 | sourced_env++ == 0) |
| 1110 | execute_env_file (get_string_value ("BASH_ENV")); |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | /* Interactive shell or `-su' shell. */ |
| 1115 | if (posixly_correct == 0) /* bash, sh */ |
| 1116 | { |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1117 | if (login_shell && sourced_login++ == 0) |
| 1118 | { |
| 1119 | /* We don't execute .bashrc for login shells. */ |
| 1120 | no_rc++; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1121 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1122 | /* Execute /etc/profile and one of the personal login shell |
| 1123 | initialization files. */ |
| 1124 | if (no_profile == 0) |
| 1125 | { |
| 1126 | maybe_execute_file (SYS_PROFILE, 1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1127 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1128 | if (act_like_sh) /* sh */ |
| 1129 | maybe_execute_file ("~/.profile", 1); |
| 1130 | else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) && |
| 1131 | (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */ |
| 1132 | maybe_execute_file ("~/.profile", 1); |
| 1133 | } |
| 1134 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1135 | |
| 1136 | /* bash */ |
| 1137 | if (act_like_sh == 0 && no_rc == 0) |
| 1138 | { |
| 1139 | #ifdef SYS_BASHRC |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1140 | # if defined (__OPENNT) |
| 1141 | maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1); |
| 1142 | # else |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1143 | maybe_execute_file (SYS_BASHRC, 1); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1144 | # endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1145 | #endif |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1146 | maybe_execute_file (bashrc_file, 1); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1147 | } |
| 1148 | /* sh */ |
| 1149 | else if (act_like_sh && privileged_mode == 0 && sourced_env++ == 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1150 | execute_env_file (get_string_value ("ENV")); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1151 | } |
| 1152 | else /* bash --posix, sh --posix */ |
| 1153 | { |
| 1154 | /* bash and sh */ |
| 1155 | if (interactive_shell && privileged_mode == 0 && sourced_env++ == 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1156 | execute_env_file (get_string_value ("ENV")); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1157 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1158 | |
| 1159 | #if defined (JOB_CONTROL) |
| 1160 | set_job_control (old_job_control); |
| 1161 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1164 | #if defined (RESTRICTED_SHELL) |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1165 | /* Return 1 if the shell should be a restricted one based on NAME or the |
| 1166 | value of `restricted'. Don't actually do anything, just return a |
| 1167 | boolean value. */ |
| 1168 | int |
| 1169 | shell_is_restricted (name) |
| 1170 | char *name; |
| 1171 | { |
| 1172 | char *temp; |
| 1173 | |
| 1174 | if (restricted) |
| 1175 | return 1; |
| 1176 | temp = base_pathname (name); |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1177 | if (*temp == '-') |
| 1178 | temp++; |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1179 | return (STREQ (temp, RESTRICTED_SHELL_NAME)); |
| 1180 | } |
| 1181 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1182 | /* Perhaps make this shell a `restricted' one, based on NAME. If the |
| 1183 | basename of NAME is "rbash", then this shell is restricted. The |
| 1184 | name of the restricted shell is a configurable option, see config.h. |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1185 | In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only |
| 1186 | and non-unsettable. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1187 | Do this also if `restricted' is already set to 1; maybe the shell was |
| 1188 | started with -r. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1189 | int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1190 | maybe_make_restricted (name) |
| 1191 | char *name; |
| 1192 | { |
| 1193 | char *temp; |
| 1194 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1195 | temp = base_pathname (name); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1196 | if (*temp == '-') |
| 1197 | temp++; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1198 | if (restricted || (STREQ (temp, RESTRICTED_SHELL_NAME))) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1199 | { |
| 1200 | set_var_read_only ("PATH"); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1201 | set_var_read_only ("SHELL"); |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1202 | set_var_read_only ("ENV"); |
| 1203 | set_var_read_only ("BASH_ENV"); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1204 | restricted = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1205 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1206 | return (restricted); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1207 | } |
| 1208 | #endif /* RESTRICTED_SHELL */ |
| 1209 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1210 | /* Fetch the current set of uids and gids and return 1 if we're running |
| 1211 | setuid or setgid. */ |
| 1212 | static int |
| 1213 | uidget () |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1214 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1215 | uid_t u; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1216 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1217 | u = getuid (); |
| 1218 | if (current_user.uid != u) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1219 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1220 | FREE (current_user.user_name); |
| 1221 | FREE (current_user.shell); |
| 1222 | FREE (current_user.home_dir); |
| 1223 | current_user.user_name = current_user.shell = current_user.home_dir = (char *)NULL; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1224 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1225 | current_user.uid = u; |
| 1226 | current_user.gid = getgid (); |
| 1227 | current_user.euid = geteuid (); |
| 1228 | current_user.egid = getegid (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1229 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1230 | /* See whether or not we are running setuid or setgid. */ |
| 1231 | return (current_user.uid != current_user.euid) || |
| 1232 | (current_user.gid != current_user.egid); |
| 1233 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1234 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1235 | void |
| 1236 | disable_priv_mode () |
| 1237 | { |
| 1238 | setuid (current_user.uid); |
| 1239 | setgid (current_user.gid); |
| 1240 | current_user.euid = current_user.uid; |
| 1241 | current_user.egid = current_user.gid; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1244 | #if defined (WORDEXP_OPTION) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1245 | static int |
| 1246 | run_wordexp (words) |
| 1247 | char *words; |
| 1248 | { |
| 1249 | int code, nw, nb; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1250 | WORD_LIST *wl, *tl, *result; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1251 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1252 | code = setjmp_nosigs (top_level); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1253 | |
| 1254 | if (code != NOT_JUMPED) |
| 1255 | { |
| 1256 | switch (code) |
| 1257 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1258 | /* Some kind of throw to top_level has occurred. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1259 | case FORCE_EOF: |
| 1260 | return last_command_exit_value = 127; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1261 | case ERREXIT: |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1262 | case EXITPROG: |
| 1263 | return last_command_exit_value; |
| 1264 | case DISCARD: |
| 1265 | return last_command_exit_value = 1; |
| 1266 | default: |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1267 | command_error ("run_wordexp", CMDERR_BADJUMP, code, 0); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /* Run it through the parser to get a list of words and expand them */ |
| 1272 | if (words && *words) |
| 1273 | { |
| 1274 | with_input_from_string (words, "--wordexp"); |
| 1275 | if (parse_command () != 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1276 | return (126); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1277 | if (global_command == 0) |
| 1278 | { |
| 1279 | printf ("0\n0\n"); |
| 1280 | return (0); |
| 1281 | } |
| 1282 | if (global_command->type != cm_simple) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1283 | return (126); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1284 | wl = global_command->value.Simple->words; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1285 | if (protected_mode) |
| 1286 | for (tl = wl; tl; tl = tl->next) |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1287 | tl->word->flags |= W_NOCOMSUB|W_NOPROCSUB; |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1288 | result = wl ? expand_words_no_vars (wl) : (WORD_LIST *)0; |
| 1289 | } |
| 1290 | else |
| 1291 | result = (WORD_LIST *)0; |
| 1292 | |
| 1293 | last_command_exit_value = 0; |
| 1294 | |
| 1295 | if (result == 0) |
| 1296 | { |
| 1297 | printf ("0\n0\n"); |
| 1298 | return (0); |
| 1299 | } |
| 1300 | |
| 1301 | /* Count up the number of words and bytes, and print them. Don't count |
| 1302 | the trailing NUL byte. */ |
| 1303 | for (nw = nb = 0, wl = result; wl; wl = wl->next) |
| 1304 | { |
| 1305 | nw++; |
| 1306 | nb += strlen (wl->word->word); |
| 1307 | } |
| 1308 | printf ("%u\n%u\n", nw, nb); |
| 1309 | /* Print each word on a separate line. This will have to be changed when |
| 1310 | the interface to glibc is completed. */ |
| 1311 | for (wl = result; wl; wl = wl->next) |
| 1312 | printf ("%s\n", wl->word->word); |
| 1313 | |
| 1314 | return (0); |
| 1315 | } |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1316 | #endif |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1317 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1318 | #if defined (ONESHOT) |
| 1319 | /* Run one command, given as the argument to the -c option. Tell |
| 1320 | parse_and_execute not to fork for a simple command. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1321 | static int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1322 | run_one_command (command) |
| 1323 | char *command; |
| 1324 | { |
| 1325 | int code; |
| 1326 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1327 | code = setjmp_nosigs (top_level); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1328 | |
| 1329 | if (code != NOT_JUMPED) |
| 1330 | { |
| 1331 | #if defined (PROCESS_SUBSTITUTION) |
| 1332 | unlink_fifo_list (); |
| 1333 | #endif /* PROCESS_SUBSTITUTION */ |
| 1334 | switch (code) |
| 1335 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1336 | /* Some kind of throw to top_level has occurred. */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1337 | case FORCE_EOF: |
| 1338 | return last_command_exit_value = 127; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1339 | case ERREXIT: |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1340 | case EXITPROG: |
| 1341 | return last_command_exit_value; |
| 1342 | case DISCARD: |
| 1343 | return last_command_exit_value = 1; |
| 1344 | default: |
| Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1345 | command_error ("run_one_command", CMDERR_BADJUMP, code, 0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1348 | return (parse_and_execute (savestring (command), "-c", SEVAL_NOHIST)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1349 | } |
| 1350 | #endif /* ONESHOT */ |
| 1351 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1352 | static int |
| 1353 | bind_args (argv, arg_start, arg_end, start_index) |
| 1354 | char **argv; |
| 1355 | int arg_start, arg_end, start_index; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1356 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1357 | register int i; |
| 1358 | WORD_LIST *args; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1359 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1360 | for (i = arg_start, args = (WORD_LIST *)NULL; i < arg_end; i++) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1361 | args = make_word_list (make_word (argv[i]), args); |
| 1362 | if (args) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1363 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1364 | args = REVERSE_LIST (args, WORD_LIST *); |
| 1365 | if (start_index == 0) /* bind to $0...$n for sh -c command */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1366 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1367 | /* Posix.2 4.56.3 says that the first argument after sh -c command |
| 1368 | becomes $0, and the rest of the arguments become $1...$n */ |
| 1369 | shell_name = savestring (args->word->word); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1370 | FREE (dollar_vars[0]); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1371 | dollar_vars[0] = savestring (args->word->word); |
| 1372 | remember_args (args->next, 1); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1373 | push_args (args->next); /* BASH_ARGV and BASH_ARGC */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1374 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1375 | else /* bind to $1...$n for shell script */ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1376 | { |
| 1377 | remember_args (args, 1); |
| 1378 | push_args (args); /* BASH_ARGV and BASH_ARGC */ |
| 1379 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1380 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1381 | dispose_words (args); |
| 1382 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1383 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1384 | return (i); |
| 1385 | } |
| 1386 | |
| 1387 | void |
| 1388 | unbind_args () |
| 1389 | { |
| 1390 | remember_args ((WORD_LIST *)NULL, 1); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1391 | pop_args (); /* Reset BASH_ARGV and BASH_ARGC */ |
| 1392 | } |
| 1393 | |
| 1394 | static void |
| 1395 | start_debugger () |
| 1396 | { |
| 1397 | #if defined (DEBUGGER) && defined (DEBUGGER_START_FILE) |
| 1398 | int old_errexit; |
| 1399 | |
| 1400 | old_errexit = exit_immediately_on_error; |
| 1401 | exit_immediately_on_error = 0; |
| 1402 | |
| 1403 | maybe_execute_file (DEBUGGER_START_FILE, 1); |
| 1404 | function_trace_mode = 1; |
| 1405 | |
| 1406 | exit_immediately_on_error += old_errexit; |
| 1407 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | static int |
| 1411 | open_shell_script (script_name) |
| 1412 | char *script_name; |
| 1413 | { |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1414 | int fd, e, fd_is_tty; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1415 | char *filename, *path_filename, *t; |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1416 | char sample[80]; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1417 | int sample_len; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1418 | struct stat sb; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1419 | #if defined (ARRAY_VARS) |
| 1420 | SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v; |
| 1421 | ARRAY *funcname_a, *bash_source_a, *bash_lineno_a; |
| 1422 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1423 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1424 | filename = savestring (script_name); |
| 1425 | |
| 1426 | fd = open (filename, O_RDONLY); |
| 1427 | if ((fd < 0) && (errno == ENOENT) && (absolute_program (filename) == 0)) |
| 1428 | { |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1429 | e = errno; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1430 | /* If it's not in the current directory, try looking through PATH |
| 1431 | for it. */ |
| 1432 | path_filename = find_path_file (script_name); |
| 1433 | if (path_filename) |
| 1434 | { |
| 1435 | free (filename); |
| 1436 | filename = path_filename; |
| 1437 | fd = open (filename, O_RDONLY); |
| 1438 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1439 | else |
| 1440 | errno = e; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | if (fd < 0) |
| 1444 | { |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1445 | e = errno; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1446 | file_error (filename); |
| 1447 | exit ((e == ENOENT) ? EX_NOTFOUND : EX_NOINPUT); |
| 1448 | } |
| 1449 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1450 | free (dollar_vars[0]); |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1451 | dollar_vars[0] = exec_argv0 ? savestring (exec_argv0) : savestring (script_name); |
| 1452 | if (exec_argv0) |
| 1453 | { |
| 1454 | free (exec_argv0); |
| 1455 | exec_argv0 = (char *)NULL; |
| 1456 | } |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1457 | |
| 1458 | #if defined (ARRAY_VARS) |
| 1459 | GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a); |
| 1460 | GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a); |
| 1461 | GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a); |
| 1462 | |
| 1463 | array_push (bash_source_a, filename); |
| 1464 | if (bash_lineno_a) |
| 1465 | { |
| 1466 | t = itos (executing_line_number ()); |
| 1467 | array_push (bash_lineno_a, t); |
| 1468 | free (t); |
| 1469 | } |
| 1470 | array_push (funcname_a, "main"); |
| 1471 | #endif |
| 1472 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1473 | #ifdef HAVE_DEV_FD |
| 1474 | fd_is_tty = isatty (fd); |
| 1475 | #else |
| 1476 | fd_is_tty = 0; |
| 1477 | #endif |
| 1478 | |
| 1479 | /* Only do this with non-tty file descriptors we can seek on. */ |
| 1480 | if (fd_is_tty == 0 && (lseek (fd, 0L, 1) != -1)) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1481 | { |
| 1482 | /* Check to see if the `file' in `bash file' is a binary file |
| 1483 | according to the same tests done by execute_simple_command (), |
| 1484 | and report an error and exit if it is. */ |
| 1485 | sample_len = read (fd, sample, sizeof (sample)); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1486 | if (sample_len < 0) |
| 1487 | { |
| 1488 | e = errno; |
| 1489 | if ((fstat (fd, &sb) == 0) && S_ISDIR (sb.st_mode)) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1490 | internal_error (_("%s: is a directory"), filename); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1491 | else |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1492 | { |
| 1493 | errno = e; |
| 1494 | file_error (filename); |
| 1495 | } |
| 1496 | exit (EX_NOEXEC); |
| 1497 | } |
| 1498 | else if (sample_len > 0 && (check_binary_file (sample, sample_len))) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1499 | { |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1500 | internal_error (_("%s: cannot execute binary file"), filename); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1501 | exit (EX_BINARY_FILE); |
| 1502 | } |
| 1503 | /* Now rewind the file back to the beginning. */ |
| 1504 | lseek (fd, 0L, 0); |
| 1505 | } |
| 1506 | |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1507 | /* Open the script. But try to move the file descriptor to a randomly |
| 1508 | large one, in the hopes that any descriptors used by the script will |
| 1509 | not match with ours. */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1510 | fd = move_to_high_fd (fd, 1, -1); |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1511 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1512 | #if defined (BUFFERED_INPUT) |
| 1513 | default_buffered_input = fd; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1514 | SET_CLOSE_ON_EXEC (default_buffered_input); |
| 1515 | #else /* !BUFFERED_INPUT */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1516 | default_input = fdopen (fd, "r"); |
| 1517 | |
| 1518 | if (default_input == 0) |
| 1519 | { |
| 1520 | file_error (filename); |
| 1521 | exit (EX_NOTFOUND); |
| 1522 | } |
| 1523 | |
| 1524 | SET_CLOSE_ON_EXEC (fd); |
| 1525 | if (fileno (default_input) != fd) |
| 1526 | SET_CLOSE_ON_EXEC (fileno (default_input)); |
| 1527 | #endif /* !BUFFERED_INPUT */ |
| 1528 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1529 | /* Just about the only way for this code to be executed is if something |
| 1530 | like `bash -i /dev/stdin' is executed. */ |
| 1531 | if (interactive_shell && fd_is_tty) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1532 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1533 | dup2 (fd, 0); |
| 1534 | close (fd); |
| 1535 | fd = 0; |
| 1536 | #if defined (BUFFERED_INPUT) |
| 1537 | default_buffered_input = 0; |
| 1538 | #else |
| 1539 | fclose (default_input); |
| 1540 | default_input = stdin; |
| 1541 | #endif |
| 1542 | } |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1543 | else if (forced_interactive && fd_is_tty == 0) |
| 1544 | /* But if a script is called with something like `bash -i scriptname', |
| 1545 | we need to do a non-interactive setup here, since we didn't do it |
| 1546 | before. */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1547 | init_interactive_script (); |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1548 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1549 | free (filename); |
| 1550 | return (fd); |
| 1551 | } |
| 1552 | |
| 1553 | /* Initialize the input routines for the parser. */ |
| 1554 | static void |
| 1555 | set_bash_input () |
| 1556 | { |
| 1557 | /* Make sure the fd from which we are reading input is not in |
| 1558 | no-delay mode. */ |
| 1559 | #if defined (BUFFERED_INPUT) |
| 1560 | if (interactive == 0) |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1561 | sh_unset_nodelay_mode (default_buffered_input); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1562 | else |
| 1563 | #endif /* !BUFFERED_INPUT */ |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1564 | sh_unset_nodelay_mode (fileno (stdin)); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1565 | |
| 1566 | /* with_input_from_stdin really means `with_input_from_readline' */ |
| 1567 | if (interactive && no_line_editing == 0) |
| 1568 | with_input_from_stdin (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1569 | #if defined (BUFFERED_INPUT) |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1570 | else if (interactive == 0) |
| 1571 | with_input_from_buffered_stream (default_buffered_input, dollar_vars[0]); |
| 1572 | #endif /* BUFFERED_INPUT */ |
| 1573 | else |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1574 | with_input_from_stream (default_input, dollar_vars[0]); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1577 | /* Close the current shell script input source and forget about it. This is |
| 1578 | extern so execute_cmd.c:initialize_subshell() can call it. If CHECK_ZERO |
| 1579 | is non-zero, we close default_buffered_input even if it's the standard |
| 1580 | input (fd 0). */ |
| 1581 | void |
| 1582 | unset_bash_input (check_zero) |
| 1583 | int check_zero; |
| 1584 | { |
| 1585 | #if defined (BUFFERED_INPUT) |
| 1586 | if ((check_zero && default_buffered_input >= 0) || |
| 1587 | (check_zero == 0 && default_buffered_input > 0)) |
| 1588 | { |
| 1589 | close_buffered_fd (default_buffered_input); |
| 1590 | default_buffered_input = bash_input.location.buffered_fd = -1; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1591 | bash_input.type = st_none; /* XXX */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1592 | } |
| 1593 | #else /* !BUFFERED_INPUT */ |
| 1594 | if (default_input) |
| 1595 | { |
| 1596 | fclose (default_input); |
| 1597 | default_input = (FILE *)NULL; |
| 1598 | } |
| 1599 | #endif /* !BUFFERED_INPUT */ |
| 1600 | } |
| 1601 | |
| 1602 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1603 | #if !defined (PROGRAM) |
| 1604 | # define PROGRAM "bash" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1605 | #endif |
| 1606 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1607 | static void |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1608 | set_shell_name (argv0) |
| 1609 | char *argv0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1610 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1611 | /* Here's a hack. If the name of this shell is "sh", then don't do |
| 1612 | any startup files; just try to be more like /bin/sh. */ |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1613 | shell_name = argv0 ? base_pathname (argv0) : PROGRAM; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1614 | |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1615 | if (argv0 && *argv0 == '-') |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1616 | { |
| Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1617 | if (*shell_name == '-') |
| 1618 | shell_name++; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1619 | login_shell = 1; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1622 | if (shell_name[0] == 's' && shell_name[1] == 'h' && shell_name[2] == '\0') |
| 1623 | act_like_sh++; |
| 1624 | if (shell_name[0] == 's' && shell_name[1] == 'u' && shell_name[2] == '\0') |
| 1625 | su_shell++; |
| 1626 | |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1627 | shell_name = argv0 ? argv0 : PROGRAM; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1628 | FREE (dollar_vars[0]); |
| 1629 | dollar_vars[0] = savestring (shell_name); |
| 1630 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1631 | /* A program may start an interactive shell with |
| 1632 | "execl ("/bin/bash", "-", NULL)". |
| 1633 | If so, default the name of this shell to our name. */ |
| 1634 | if (!shell_name || !*shell_name || (shell_name[0] == '-' && !shell_name[1])) |
| 1635 | shell_name = PROGRAM; |
| 1636 | } |
| 1637 | |
| 1638 | static void |
| 1639 | init_interactive () |
| 1640 | { |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1641 | expand_aliases = interactive_shell = startup_state = 1; |
| 1642 | interactive = 1; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | static void |
| 1646 | init_noninteractive () |
| 1647 | { |
| 1648 | #if defined (HISTORY) |
| 1649 | bash_history_reinit (0); |
| 1650 | #endif /* HISTORY */ |
| 1651 | interactive_shell = startup_state = interactive = 0; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1652 | expand_aliases = posixly_correct; /* XXX - was 0 not posixly_correct */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1653 | no_line_editing = 1; |
| 1654 | #if defined (JOB_CONTROL) |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1655 | /* Even if the shell is not interactive, enable job control if the -i or |
| 1656 | -m option is supplied at startup. */ |
| 1657 | set_job_control (forced_interactive||jobs_m_flag); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1658 | #endif /* JOB_CONTROL */ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1661 | static void |
| 1662 | init_interactive_script () |
| 1663 | { |
| 1664 | init_noninteractive (); |
| 1665 | expand_aliases = interactive_shell = startup_state = 1; |
| 1666 | } |
| 1667 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1668 | void |
| 1669 | get_current_user_info () |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1670 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1671 | struct passwd *entry; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1672 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1673 | /* Don't fetch this more than once. */ |
| 1674 | if (current_user.user_name == 0) |
| 1675 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1676 | #if defined (__TANDEM) |
| 1677 | entry = getpwnam (getlogin ()); |
| 1678 | #else |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1679 | entry = getpwuid (current_user.uid); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1680 | #endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1681 | if (entry) |
| 1682 | { |
| 1683 | current_user.user_name = savestring (entry->pw_name); |
| 1684 | current_user.shell = (entry->pw_shell && entry->pw_shell[0]) |
| 1685 | ? savestring (entry->pw_shell) |
| 1686 | : savestring ("/bin/sh"); |
| 1687 | current_user.home_dir = savestring (entry->pw_dir); |
| 1688 | } |
| 1689 | else |
| 1690 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1691 | current_user.user_name = _("I have no name!"); |
| 1692 | current_user.user_name = savestring (current_user.user_name); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1693 | current_user.shell = savestring ("/bin/sh"); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1694 | current_user.home_dir = savestring ("/"); |
| 1695 | } |
| 1696 | endpwent (); |
| 1697 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /* Do whatever is necessary to initialize the shell. |
| 1701 | Put new initializations in here. */ |
| 1702 | static void |
| 1703 | shell_initialize () |
| 1704 | { |
| 1705 | char hostname[256]; |
| 1706 | |
| 1707 | /* Line buffer output for stderr and stdout. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1708 | if (shell_initialized == 0) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1709 | { |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1710 | sh_setlinebuf (stderr); |
| 1711 | sh_setlinebuf (stdout); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1712 | } |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1713 | |
| 1714 | /* Sort the array of shell builtins so that the binary search in |
| 1715 | find_shell_builtin () works correctly. */ |
| 1716 | initialize_shell_builtins (); |
| 1717 | |
| 1718 | /* Initialize the trap signal handlers before installing our own |
| 1719 | signal handlers. traps.c:restore_original_signals () is responsible |
| 1720 | for restoring the original default signal handlers. That function |
| 1721 | is called when we make a new child. */ |
| 1722 | initialize_traps (); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1723 | initialize_signals (0); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1724 | |
| 1725 | /* It's highly unlikely that this will change. */ |
| 1726 | if (current_host_name == 0) |
| 1727 | { |
| 1728 | /* Initialize current_host_name. */ |
| 1729 | if (gethostname (hostname, 255) < 0) |
| 1730 | current_host_name = "??host??"; |
| 1731 | else |
| 1732 | current_host_name = savestring (hostname); |
| 1733 | } |
| 1734 | |
| 1735 | /* Initialize the stuff in current_user that comes from the password |
| 1736 | file. We don't need to do this right away if the shell is not |
| 1737 | interactive. */ |
| 1738 | if (interactive_shell) |
| 1739 | get_current_user_info (); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1740 | |
| 1741 | /* Initialize our interface to the tilde expander. */ |
| 1742 | tilde_initialize (); |
| 1743 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1744 | /* Initialize internal and environment variables. Don't import shell |
| 1745 | functions from the environment if we are running in privileged or |
| 1746 | restricted mode or if the shell is running setuid. */ |
| 1747 | #if defined (RESTRICTED_SHELL) |
| 1748 | initialize_shell_variables (shell_environment, privileged_mode||restricted||running_setuid); |
| 1749 | #else |
| 1750 | initialize_shell_variables (shell_environment, privileged_mode||running_setuid); |
| 1751 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1752 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1753 | /* Initialize the data structures for storing and running jobs. */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1754 | initialize_job_control (jobs_m_flag); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1755 | |
| 1756 | /* Initialize input streams to null. */ |
| 1757 | initialize_bash_input (); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1758 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1759 | initialize_flags (); |
| 1760 | |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1761 | /* Initialize the shell options. Don't import the shell options |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1762 | from the environment variables $SHELLOPTS or $BASHOPTS if we are |
| 1763 | running in privileged or restricted mode or if the shell is running |
| 1764 | setuid. */ |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1765 | #if defined (RESTRICTED_SHELL) |
| 1766 | initialize_shell_options (privileged_mode||restricted||running_setuid); |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1767 | initialize_bashopts (privileged_mode||restricted||running_setuid); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1768 | #else |
| 1769 | initialize_shell_options (privileged_mode||running_setuid); |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1770 | initialize_bashopts (privileged_mode||running_setuid); |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1771 | #endif |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | /* Function called by main () when it appears that the shell has already |
| 1775 | had some initialization performed. This is supposed to reset the world |
| 1776 | back to a pristine state, as if we had been exec'ed. */ |
| 1777 | static void |
| 1778 | shell_reinitialize () |
| 1779 | { |
| 1780 | /* The default shell prompts. */ |
| 1781 | primary_prompt = PPROMPT; |
| 1782 | secondary_prompt = SPROMPT; |
| 1783 | |
| 1784 | /* Things that get 1. */ |
| 1785 | current_command_number = 1; |
| 1786 | |
| 1787 | /* We have decided that the ~/.bashrc file should not be executed |
| 1788 | for the invocation of each shell script. If the variable $ENV |
| 1789 | (or $BASH_ENV) is set, its value is used as the name of a file |
| 1790 | to source. */ |
| 1791 | no_rc = no_profile = 1; |
| 1792 | |
| 1793 | /* Things that get 0. */ |
| 1794 | login_shell = make_login_shell = interactive = executing = 0; |
| 1795 | debugging = do_version = line_number = last_command_exit_value = 0; |
| 1796 | forced_interactive = interactive_shell = subshell_environment = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1797 | expand_aliases = 0; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1798 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1799 | /* XXX - should we set jobs_m_flag to 0 here? */ |
| 1800 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1801 | #if defined (HISTORY) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1802 | bash_history_reinit (0); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1803 | #endif /* HISTORY */ |
| 1804 | |
| 1805 | #if defined (RESTRICTED_SHELL) |
| 1806 | restricted = 0; |
| 1807 | #endif /* RESTRICTED_SHELL */ |
| 1808 | |
| 1809 | /* Ensure that the default startup file is used. (Except that we don't |
| 1810 | execute this file for reinitialized shells). */ |
| 1811 | bashrc_file = "~/.bashrc"; |
| 1812 | |
| 1813 | /* Delete all variables and functions. They will be reinitialized when |
| 1814 | the environment is parsed. */ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1815 | delete_all_contexts (shell_variables); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1816 | delete_all_variables (shell_functions); |
| 1817 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1818 | reinit_special_variables (); |
| 1819 | |
| 1820 | #if defined (READLINE) |
| 1821 | bashline_reinitialize (); |
| 1822 | #endif |
| 1823 | |
| Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1824 | shell_reinitialized = 1; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | static void |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1828 | show_shell_usage (fp, extra) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1829 | FILE *fp; |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1830 | int extra; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1831 | { |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1832 | int i; |
| 1833 | char *set_opts, *s, *t; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1834 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1835 | if (extra) |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1836 | fprintf (fp, _("GNU bash, version %s-(%s)\n"), shell_version_string (), MACHTYPE); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1837 | fprintf (fp, _("Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n"), |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1838 | shell_name, shell_name); |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1839 | fputs (_("GNU long options:\n"), fp); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1840 | for (i = 0; long_args[i].name; i++) |
| 1841 | fprintf (fp, "\t--%s\n", long_args[i].name); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1842 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1843 | fputs (_("Shell options:\n"), fp); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1844 | fputs (_("\t-ilrsD or -c command or -O shopt_option\t\t(invocation only)\n"), fp); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1845 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1846 | for (i = 0, set_opts = 0; shell_builtins[i].name; i++) |
| 1847 | if (STREQ (shell_builtins[i].name, "set")) |
| 1848 | set_opts = savestring (shell_builtins[i].short_doc); |
| 1849 | if (set_opts) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1850 | { |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1851 | s = strchr (set_opts, '['); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1852 | if (s == 0) |
| 1853 | s = set_opts; |
| 1854 | while (*++s == '-') |
| 1855 | ; |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1856 | t = strchr (s, ']'); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1857 | if (t) |
| 1858 | *t = '\0'; |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1859 | fprintf (fp, _("\t-%s or -o option\n"), s); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1860 | free (set_opts); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1863 | if (extra) |
| 1864 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1865 | fprintf (fp, _("Type `%s -c \"help set\"' for more information about shell options.\n"), shell_name); |
| 1866 | fprintf (fp, _("Type `%s -c help' for more information about shell builtin commands.\n"), shell_name); |
| 1867 | fprintf (fp, _("Use the `bashbug' command to report bugs.\n")); |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1868 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1871 | static void |
| 1872 | add_shopt_to_alist (opt, on_or_off) |
| 1873 | char *opt; |
| 1874 | int on_or_off; |
| 1875 | { |
| 1876 | if (shopt_ind >= shopt_len) |
| 1877 | { |
| 1878 | shopt_len += 8; |
| 1879 | shopt_alist = (STRING_INT_ALIST *)xrealloc (shopt_alist, shopt_len * sizeof (shopt_alist[0])); |
| 1880 | } |
| 1881 | shopt_alist[shopt_ind].word = opt; |
| 1882 | shopt_alist[shopt_ind].token = on_or_off; |
| 1883 | shopt_ind++; |
| 1884 | } |
| 1885 | |
| 1886 | static void |
| 1887 | run_shopt_alist () |
| 1888 | { |
| 1889 | register int i; |
| 1890 | |
| 1891 | for (i = 0; i < shopt_ind; i++) |
| 1892 | if (shopt_setopt (shopt_alist[i].word, (shopt_alist[i].token == '-')) != EXECUTION_SUCCESS) |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1893 | exit (EX_BADUSAGE); |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1894 | free (shopt_alist); |
| 1895 | shopt_alist = 0; |
| 1896 | shopt_ind = shopt_len = 0; |
| 1897 | } |