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