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