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