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