blob: 2fd8179ba10db10a558cff34df5b38bf8c8f91c7 [file] [log] [blame]
Jari Aaltobb706242000-03-17 21:46:59 +00001/* shell.c -- GNU's idea of the POSIX shell specification. */
Jari Aalto726f6381996-08-26 18:22:31 +00002
Chet Rameyac50fba2014-02-26 09:36:43 -05003/* Copyright (C) 1987-2012 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
Jari Aaltobb706242000-03-17 21:46:59 +00005 This file is part of GNU Bash, the Bourne Again SHell.
Jari Aalto726f6381996-08-26 18:22:31 +00006
Jari Aalto31859422009-01-12 13:36:28 +00007 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000016
Jari Aaltobb706242000-03-17 21:46:59 +000017 You should have received a copy of the GNU General Public License
Jari Aalto31859422009-01-12 13:36:28 +000018 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aalto726f6381996-08-26 18:22:31 +000020
Jari Aalto31859422009-01-12 13:36:28 +000021/*
Jari Aalto726f6381996-08-26 18:22:31 +000022 Birthdate:
23 Sunday, January 10th, 1988.
24 Initial author: Brian Fox
25*/
26#define INSTALL_DEBUG_MODE
27
Jari Aaltoccc6cda1996-12-23 17:02:34 +000028#include "config.h"
29
Jari Aalto726f6381996-08-26 18:22:31 +000030#include "bashtypes.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000031#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000032# include <sys/file.h>
33#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000034#include "posixstat.h"
Jari Aaltof73dda02001-11-13 17:56:06 +000035#include "posixtime.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000036#include "bashansi.h"
Jari Aalto726f6381996-08-26 18:22:31 +000037#include <stdio.h>
38#include <signal.h>
39#include <errno.h>
Jari Aalto726f6381996-08-26 18:22:31 +000040#include "filecntl.h"
41#include <pwd.h>
Jari Aalto726f6381996-08-26 18:22:31 +000042
Jari Aaltoccc6cda1996-12-23 17:02:34 +000043#if defined (HAVE_UNISTD_H)
44# include <unistd.h>
Jari Aalto726f6381996-08-26 18:22:31 +000045#endif
46
Jari Aaltob80f6442004-07-27 13:29:18 +000047#include "bashintl.h"
48
Jari Aaltof73dda02001-11-13 17:56:06 +000049#define NEED_SH_SETLINEBUF_DECL /* used in externs.h */
50
Jari Aalto726f6381996-08-26 18:22:31 +000051#include "shell.h"
52#include "flags.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000053#include "trap.h"
54#include "mailcheck.h"
55#include "builtins.h"
56#include "builtins/common.h"
Jari Aalto726f6381996-08-26 18:22:31 +000057
58#if defined (JOB_CONTROL)
59#include "jobs.h"
60#endif /* JOB_CONTROL */
61
62#include "input.h"
63#include "execute_cmd.h"
Jari Aaltocce855b1998-04-17 19:52:44 +000064#include "findcmd.h"
Jari Aalto726f6381996-08-26 18:22:31 +000065
Jari Aalto7117c2d2002-07-17 14:10:11 +000066#if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
67# include <malloc/shmalloc.h>
68#endif
69
Jari Aalto726f6381996-08-26 18:22:31 +000070#if defined (HISTORY)
71# include "bashhist.h"
72# include <readline/history.h>
73#endif
74
Jari Aalto31859422009-01-12 13:36:28 +000075#if defined (READLINE)
Chet Ramey84c617e2015-01-15 10:21:08 -050076# include <readline/readline.h>
Jari Aalto31859422009-01-12 13:36:28 +000077# include "bashline.h"
78#endif
79
Jari Aalto726f6381996-08-26 18:22:31 +000080#include <tilde/tilde.h>
Jari Aaltof73dda02001-11-13 17:56:06 +000081#include <glob/strmatch.h>
Jari Aalto726f6381996-08-26 18:22:31 +000082
Jari Aaltob72432f1999-02-19 17:11:39 +000083#if defined (__OPENNT)
84# include <opennt/opennt.h>
85#endif
86
Jari Aaltoccc6cda1996-12-23 17:02:34 +000087#if !defined (HAVE_GETPW_DECLS)
Jari Aalto726f6381996-08-26 18:22:31 +000088extern struct passwd *getpwuid ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +000089#endif /* !HAVE_GETPW_DECLS */
Jari Aalto726f6381996-08-26 18:22:31 +000090
Jari Aalto726f6381996-08-26 18:22:31 +000091#if !defined (errno)
92extern int errno;
93#endif
94
Jari Aaltob72432f1999-02-19 17:11:39 +000095#if defined (NO_MAIN_ENV_ARG)
96extern char **environ; /* used if no third argument to main() */
97#endif
98
Jari Aaltoccc6cda1996-12-23 17:02:34 +000099extern char *dist_version, *release_status;
Jari Aalto726f6381996-08-26 18:22:31 +0000100extern int patch_level, build_version;
Jari Aaltobb706242000-03-17 21:46:59 +0000101extern int shell_level;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000102extern int subshell_environment;
Jari Aalto726f6381996-08-26 18:22:31 +0000103extern int last_command_exit_value;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000104extern int line_number;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000105extern int expand_aliases;
Jari Aaltocce855b1998-04-17 19:52:44 +0000106extern int array_needs_making;
Jari Aalto95732b42005-12-07 14:08:12 +0000107extern int gnu_error_format;
108extern char *primary_prompt, *secondary_prompt;
109extern char *this_command_name;
Jari Aalto726f6381996-08-26 18:22:31 +0000110
111/* Non-zero means that this shell has already been run; i.e. you should
112 call shell_reinitialize () if you need to start afresh. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000113int shell_initialized = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000114
115COMMAND *global_command = (COMMAND *)NULL;
116
Jari Aalto726f6381996-08-26 18:22:31 +0000117/* Information about the current user. */
118struct user_info current_user =
119{
Jari Aaltof73dda02001-11-13 17:56:06 +0000120 (uid_t)-1, (uid_t)-1, (gid_t)-1, (gid_t)-1,
121 (char *)NULL, (char *)NULL, (char *)NULL
Jari Aalto726f6381996-08-26 18:22:31 +0000122};
123
124/* The current host's name. */
125char *current_host_name = (char *)NULL;
126
127/* Non-zero means that this shell is a login shell.
128 Specifically:
129 0 = not login shell.
130 1 = login shell from getty (or equivalent fake out)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000131 -1 = login shell from "--login" (or -l) flag.
Jari Aalto726f6381996-08-26 18:22:31 +0000132 -2 = both from getty, and from flag.
133 */
134int login_shell = 0;
135
136/* Non-zero means that at this moment, the shell is interactive. In
137 general, this means that the shell is at this moment reading input
138 from the keyboard. */
139int interactive = 0;
140
141/* Non-zero means that the shell was started as an interactive shell. */
142int interactive_shell = 0;
143
Jari Aaltocce855b1998-04-17 19:52:44 +0000144/* Non-zero means to send a SIGHUP to all jobs when an interactive login
145 shell exits. */
146int hup_on_exit = 0;
147
Jari Aalto31859422009-01-12 13:36:28 +0000148/* Non-zero means to list status of running and stopped jobs at shell exit */
149int check_jobs_at_exit = 0;
150
151/* Non-zero means to change to a directory name supplied as a command name */
152int autocd = 0;
153
Jari Aalto726f6381996-08-26 18:22:31 +0000154/* Tells what state the shell was in when it started:
155 0 = non-interactive shell script
156 1 = interactive
157 2 = -c command
Jari Aalto7117c2d2002-07-17 14:10:11 +0000158 3 = wordexp evaluation
Jari Aalto726f6381996-08-26 18:22:31 +0000159 This is a superset of the information provided by interactive_shell.
160*/
161int startup_state = 0;
162
163/* Special debugging helper. */
164int debugging_login_shell = 0;
165
166/* The environment that the shell passes to other commands. */
167char **shell_environment;
168
169/* Non-zero when we are executing a top-level command. */
170int executing = 0;
171
172/* The number of commands executed so far. */
173int current_command_number = 1;
174
Jari Aalto726f6381996-08-26 18:22:31 +0000175/* Non-zero is the recursion depth for commands. */
176int indirection_level = 0;
177
Jari Aalto726f6381996-08-26 18:22:31 +0000178/* The name of this shell, as taken from argv[0]. */
179char *shell_name = (char *)NULL;
180
181/* time in seconds when the shell was started */
182time_t shell_start_time;
183
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000184/* Are we running in an emacs shell window? */
185int running_under_emacs;
186
Jari Aalto31859422009-01-12 13:36:28 +0000187/* Do we have /dev/fd? */
188#ifdef HAVE_DEV_FD
189int have_devfd = HAVE_DEV_FD;
190#else
191int have_devfd = 0;
192#endif
193
Jari Aalto726f6381996-08-26 18:22:31 +0000194/* The name of the .(shell)rc file. */
195static char *bashrc_file = "~/.bashrc";
196
197/* Non-zero means to act more like the Bourne shell on startup. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000198static int act_like_sh;
199
200/* Non-zero if this shell is being run by `su'. */
201static int su_shell;
202
203/* Non-zero if we have already expanded and sourced $ENV. */
204static int sourced_env;
205
206/* Is this shell running setuid? */
207static int running_setuid;
Jari Aalto726f6381996-08-26 18:22:31 +0000208
209/* Values for the long-winded argument names. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000210static int debugging; /* Do debugging things. */
211static int no_rc; /* Don't execute ~/.bashrc */
212static int no_profile; /* Don't execute .profile */
213static int do_version; /* Display interesting version info. */
214static int make_login_shell; /* Make this shell be a `-bash' shell. */
215static int want_initial_help; /* --help option */
Jari Aalto726f6381996-08-26 18:22:31 +0000216
Jari Aaltob80f6442004-07-27 13:29:18 +0000217int debugging_mode = 0; /* In debugging mode with --debugger */
Chet Rameyac50fba2014-02-26 09:36:43 -0500218#if defined (READLINE)
219int no_line_editing = 0; /* non-zero -> don't do fancy line editing. */
220#else
221int no_line_editing = 1; /* can't have line editing without readline */
222#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000223int dump_translatable_strings; /* Dump strings in $"...", don't execute. */
Jari Aaltocce855b1998-04-17 19:52:44 +0000224int dump_po_strings; /* Dump strings in $"..." in po format */
225int wordexp_only = 0; /* Do word expansion only */
Jari Aaltob80f6442004-07-27 13:29:18 +0000226int protected_mode = 0; /* No command substitution with --wordexp */
Jari Aalto726f6381996-08-26 18:22:31 +0000227
Jari Aalto95732b42005-12-07 14:08:12 +0000228#if defined (STRICT_POSIX)
229int posixly_correct = 1; /* Non-zero means posix.2 superset. */
230#else
231int posixly_correct = 0; /* Non-zero means posix.2 superset. */
232#endif
233
Jari Aalto726f6381996-08-26 18:22:31 +0000234/* Some long-winded argument names. These are obviously new. */
235#define Int 1
236#define Charp 2
Jari Aalto31859422009-01-12 13:36:28 +0000237static const struct {
238 const char *name;
Jari Aalto726f6381996-08-26 18:22:31 +0000239 int type;
240 int *int_value;
241 char **char_value;
242} long_args[] = {
243 { "debug", Int, &debugging, (char **)0x0 },
Jari Aaltob80f6442004-07-27 13:29:18 +0000244#if defined (DEBUGGER)
245 { "debugger", Int, &debugging_mode, (char **)0x0 },
246#endif
Jari Aaltocce855b1998-04-17 19:52:44 +0000247 { "dump-po-strings", Int, &dump_po_strings, (char **)0x0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000248 { "dump-strings", Int, &dump_translatable_strings, (char **)0x0 },
249 { "help", Int, &want_initial_help, (char **)0x0 },
Jari Aalto28ef6c32001-04-06 19:14:31 +0000250 { "init-file", Charp, (int *)0x0, &bashrc_file },
Jari Aalto726f6381996-08-26 18:22:31 +0000251 { "login", Int, &make_login_shell, (char **)0x0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000252 { "noediting", Int, &no_line_editing, (char **)0x0 },
253 { "noprofile", Int, &no_profile, (char **)0x0 },
254 { "norc", Int, &no_rc, (char **)0x0 },
Jari Aalto726f6381996-08-26 18:22:31 +0000255 { "posix", Int, &posixly_correct, (char **)0x0 },
Chet Rameyac50fba2014-02-26 09:36:43 -0500256#if defined (WORDEXP_OPTION)
Jari Aaltob80f6442004-07-27 13:29:18 +0000257 { "protected", Int, &protected_mode, (char **)0x0 },
Chet Rameyac50fba2014-02-26 09:36:43 -0500258#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000259 { "rcfile", Charp, (int *)0x0, &bashrc_file },
260#if defined (RESTRICTED_SHELL)
261 { "restricted", Int, &restricted, (char **)0x0 },
262#endif
263 { "verbose", Int, &echo_input_at_read, (char **)0x0 },
264 { "version", Int, &do_version, (char **)0x0 },
Chet Ramey00018032011-11-21 20:51:19 -0500265#if defined (WORDEXP_OPTION)
Jari Aaltocce855b1998-04-17 19:52:44 +0000266 { "wordexp", Int, &wordexp_only, (char **)0x0 },
Chet Ramey00018032011-11-21 20:51:19 -0500267#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000268 { (char *)0x0, Int, (int *)0x0, (char **)0x0 }
269};
270
271/* These are extern so execute_simple_command can set them, and then
272 longjmp back to main to execute a shell script, instead of calling
273 main () again and resulting in indefinite, possibly fatal, stack
274 growth. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000275procenv_t subshell_top_level;
Jari Aalto726f6381996-08-26 18:22:31 +0000276int subshell_argc;
277char **subshell_argv;
278char **subshell_envp;
279
Chet Ramey495aee42011-11-22 19:11:26 -0500280char *exec_argv0;
281
Jari Aalto726f6381996-08-26 18:22:31 +0000282#if defined (BUFFERED_INPUT)
283/* The file descriptor from which the shell is reading input. */
284int default_buffered_input = -1;
285#endif
286
Jari Aaltobb706242000-03-17 21:46:59 +0000287/* The following two variables are not static so they can show up in $-. */
288int read_from_stdin; /* -s flag supplied */
289int want_pending_command; /* -c flag supplied */
290
Jari Aaltob80f6442004-07-27 13:29:18 +0000291/* This variable is not static so it can be bound to $BASH_EXECUTION_STRING */
292char *command_execution_string; /* argument to -c option */
293
Jari Aalto7117c2d2002-07-17 14:10:11 +0000294int malloc_trace_at_exit = 0;
295
Jari Aalto28ef6c32001-04-06 19:14:31 +0000296static int shell_reinitialized = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000297
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000298static FILE *default_input;
Jari Aalto726f6381996-08-26 18:22:31 +0000299
Jari Aaltof73dda02001-11-13 17:56:06 +0000300static STRING_INT_ALIST *shopt_alist;
301static int shopt_ind = 0, shopt_len = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000302
Jari Aaltof73dda02001-11-13 17:56:06 +0000303static int parse_long_options __P((char **, int, int));
304static int parse_shell_options __P((char **, int, int));
305static int bind_args __P((char **, int, int, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000306
Jari Aaltob80f6442004-07-27 13:29:18 +0000307static void start_debugger __P((void));
308
Jari Aaltof73dda02001-11-13 17:56:06 +0000309static void add_shopt_to_alist __P((char *, int));
310static void run_shopt_alist __P((void));
Jari Aalto726f6381996-08-26 18:22:31 +0000311
Jari Aaltof73dda02001-11-13 17:56:06 +0000312static void execute_env_file __P((char *));
313static void run_startup_files __P((void));
314static int open_shell_script __P((char *));
315static void set_bash_input __P((void));
316static int run_one_command __P((char *));
Chet Ramey00018032011-11-21 20:51:19 -0500317#if defined (WORDEXP_OPTION)
Jari Aaltof73dda02001-11-13 17:56:06 +0000318static int run_wordexp __P((char *));
Chet Ramey00018032011-11-21 20:51:19 -0500319#endif
Jari Aaltof73dda02001-11-13 17:56:06 +0000320
321static int uidget __P((void));
Jari Aaltof73dda02001-11-13 17:56:06 +0000322
323static void init_interactive __P((void));
324static void init_noninteractive __P((void));
Jari Aalto31859422009-01-12 13:36:28 +0000325static void init_interactive_script __P((void));
Jari Aaltof73dda02001-11-13 17:56:06 +0000326
327static void set_shell_name __P((char *));
328static void shell_initialize __P((void));
329static void shell_reinitialize __P((void));
330
331static void show_shell_usage __P((FILE *, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000332
Jari Aalto28ef6c32001-04-06 19:14:31 +0000333#ifdef __CYGWIN__
Jari Aaltocce855b1998-04-17 19:52:44 +0000334static void
335_cygwin32_check_tmp ()
336{
337 struct stat sb;
338
339 if (stat ("/tmp", &sb) < 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000340 internal_warning (_("could not find /tmp, please create!"));
Jari Aaltocce855b1998-04-17 19:52:44 +0000341 else
342 {
343 if (S_ISDIR (sb.st_mode) == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000344 internal_warning (_("/tmp must be a valid directory name"));
Jari Aaltocce855b1998-04-17 19:52:44 +0000345 }
346}
Jari Aalto28ef6c32001-04-06 19:14:31 +0000347#endif /* __CYGWIN__ */
Jari Aaltocce855b1998-04-17 19:52:44 +0000348
Jari Aaltob72432f1999-02-19 17:11:39 +0000349#if defined (NO_MAIN_ENV_ARG)
350/* systems without third argument to main() */
351int
352main (argc, argv)
353 int argc;
354 char **argv;
355#else /* !NO_MAIN_ENV_ARG */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000356int
Jari Aalto726f6381996-08-26 18:22:31 +0000357main (argc, argv, env)
358 int argc;
359 char **argv, **env;
Jari Aaltob72432f1999-02-19 17:11:39 +0000360#endif /* !NO_MAIN_ENV_ARG */
Jari Aalto726f6381996-08-26 18:22:31 +0000361{
362 register int i;
Jari Aaltof73dda02001-11-13 17:56:06 +0000363 int code, old_errexit_flag;
364#if defined (RESTRICTED_SHELL)
365 int saverst;
366#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000367 volatile int locally_skip_execution;
368 volatile int arg_index, top_level_arg_index;
Jari Aaltob72432f1999-02-19 17:11:39 +0000369#ifdef __OPENNT
370 char **env;
371
372 env = environ;
373#endif /* __OPENNT */
Jari Aalto726f6381996-08-26 18:22:31 +0000374
Jari Aaltof73dda02001-11-13 17:56:06 +0000375 USE_VAR(argc);
376 USE_VAR(argv);
377 USE_VAR(env);
378 USE_VAR(code);
379 USE_VAR(old_errexit_flag);
380#if defined (RESTRICTED_SHELL)
381 USE_VAR(saverst);
382#endif
383
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000384 /* Catch early SIGINTs. */
Chet Rameyac50fba2014-02-26 09:36:43 -0500385 code = setjmp_nosigs (top_level);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000386 if (code)
387 exit (2);
Jari Aalto726f6381996-08-26 18:22:31 +0000388
Chet Ramey00018032011-11-21 20:51:19 -0500389 xtrace_init ();
390
Jari Aalto7117c2d2002-07-17 14:10:11 +0000391#if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
392# if 1
Jari Aaltof73dda02001-11-13 17:56:06 +0000393 malloc_set_register (1);
394# endif
395#endif
396
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000397 check_dev_tty ();
Jari Aalto726f6381996-08-26 18:22:31 +0000398
Jari Aalto28ef6c32001-04-06 19:14:31 +0000399#ifdef __CYGWIN__
Jari Aaltocce855b1998-04-17 19:52:44 +0000400 _cygwin32_check_tmp ();
Jari Aalto28ef6c32001-04-06 19:14:31 +0000401#endif /* __CYGWIN__ */
Jari Aaltocce855b1998-04-17 19:52:44 +0000402
Jari Aalto726f6381996-08-26 18:22:31 +0000403 /* Wait forever if we are debugging a login shell. */
Jari Aalto95732b42005-12-07 14:08:12 +0000404 while (debugging_login_shell) sleep (3);
Jari Aalto726f6381996-08-26 18:22:31 +0000405
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000406 set_default_locale ();
Jari Aalto726f6381996-08-26 18:22:31 +0000407
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000408 running_setuid = uidget ();
Jari Aalto726f6381996-08-26 18:22:31 +0000409
Jari Aalto28ef6c32001-04-06 19:14:31 +0000410 if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC"))
411 posixly_correct = 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000412
413#if defined (USE_GNU_MALLOC_LIBRARY)
414 mcheck (programming_error, (void (*) ())0);
415#endif /* USE_GNU_MALLOC_LIBRARY */
416
417 if (setjmp (subshell_top_level))
418 {
419 argc = subshell_argc;
420 argv = subshell_argv;
421 env = subshell_envp;
422 sourced_env = 0;
423 }
424
Jari Aalto28ef6c32001-04-06 19:14:31 +0000425 shell_reinitialized = 0;
426
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000427 /* Initialize `local' variables for all `invocations' of main (). */
Jari Aalto726f6381996-08-26 18:22:31 +0000428 arg_index = 1;
Jari Aalto95732b42005-12-07 14:08:12 +0000429 if (arg_index > argc)
430 arg_index = argc;
Jari Aaltob80f6442004-07-27 13:29:18 +0000431 command_execution_string = (char *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000432 want_pending_command = locally_skip_execution = read_from_stdin = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000433 default_input = stdin;
434#if defined (BUFFERED_INPUT)
435 default_buffered_input = -1;
436#endif
437
438 /* Fix for the `infinite process creation' bug when running shell scripts
439 from startup files on System V. */
440 login_shell = make_login_shell = 0;
441
442 /* If this shell has already been run, then reinitialize it to a
443 vanilla state. */
444 if (shell_initialized || shell_name)
445 {
446 /* Make sure that we do not infinitely recurse as a login shell. */
447 if (*shell_name == '-')
448 shell_name++;
449
450 shell_reinitialize ();
Chet Rameyac50fba2014-02-26 09:36:43 -0500451 if (setjmp_nosigs (top_level))
Jari Aalto726f6381996-08-26 18:22:31 +0000452 exit (2);
453 }
454
Jari Aalto726f6381996-08-26 18:22:31 +0000455 shell_environment = env;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000456 set_shell_name (argv[0]);
Jari Aalto726f6381996-08-26 18:22:31 +0000457 shell_start_time = NOW; /* NOW now defined in general.h */
458
Jari Aalto726f6381996-08-26 18:22:31 +0000459 /* Parse argument flags from the input line. */
460
461 /* Find full word arguments first. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000462 arg_index = parse_long_options (argv, arg_index, argc);
463
464 if (want_initial_help)
Jari Aalto726f6381996-08-26 18:22:31 +0000465 {
Jari Aaltod166f041997-06-05 14:59:13 +0000466 show_shell_usage (stdout, 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000467 exit (EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +0000468 }
469
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000470 if (do_version)
471 {
472 show_shell_version (1);
473 exit (EXECUTION_SUCCESS);
474 }
Jari Aalto726f6381996-08-26 18:22:31 +0000475
Jari Aalto7117c2d2002-07-17 14:10:11 +0000476 /* All done with full word options; do standard shell option parsing.*/
477 this_command_name = shell_name; /* for error reporting */
478 arg_index = parse_shell_options (argv, arg_index, argc);
479
480 /* If user supplied the "--login" (or -l) flag, then set and invert
481 LOGIN_SHELL. */
Jari Aalto726f6381996-08-26 18:22:31 +0000482 if (make_login_shell)
483 {
484 login_shell++;
485 login_shell = -login_shell;
486 }
487
Chet Ramey00018032011-11-21 20:51:19 -0500488 set_login_shell ("login_shell", login_shell != 0);
Jari Aaltof73dda02001-11-13 17:56:06 +0000489
Jari Aaltocce855b1998-04-17 19:52:44 +0000490 if (dump_po_strings)
491 dump_translatable_strings = 1;
492
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000493 if (dump_translatable_strings)
494 read_but_dont_execute = 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000495
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000496 if (running_setuid && privileged_mode == 0)
497 disable_priv_mode ();
Jari Aalto726f6381996-08-26 18:22:31 +0000498
499 /* Need to get the argument to a -c option processed in the
500 above loop. The next arg is a command to execute, and the
501 following args are $0...$n respectively. */
502 if (want_pending_command)
503 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000504 command_execution_string = argv[arg_index];
505 if (command_execution_string == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000506 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000507 report_error (_("%s: option requires an argument"), "-c");
508 exit (EX_BADUSAGE);
Jari Aalto726f6381996-08-26 18:22:31 +0000509 }
510 arg_index++;
511 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000512 this_command_name = (char *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000513
Jari Aalto7117c2d2002-07-17 14:10:11 +0000514 cmd_init(); /* initialize the command object caches */
515
Jari Aalto726f6381996-08-26 18:22:31 +0000516 /* First, let the outside world know about our interactive status.
517 A shell is interactive if the `-i' flag was given, or if all of
518 the following conditions are met:
519 no -c command
520 no arguments remaining or the -s flag given
521 standard input is a terminal
Jari Aaltob80f6442004-07-27 13:29:18 +0000522 standard error is a terminal
Jari Aalto726f6381996-08-26 18:22:31 +0000523 Refer to Posix.2, the description of the `sh' utility. */
524
525 if (forced_interactive || /* -i flag */
Jari Aaltob80f6442004-07-27 13:29:18 +0000526 (!command_execution_string && /* No -c command and ... */
Jari Aaltocce855b1998-04-17 19:52:44 +0000527 wordexp_only == 0 && /* No --wordexp and ... */
Jari Aalto726f6381996-08-26 18:22:31 +0000528 ((arg_index == argc) || /* no remaining args or... */
529 read_from_stdin) && /* -s flag with args, and */
530 isatty (fileno (stdin)) && /* Input is a terminal and */
Jari Aaltob80f6442004-07-27 13:29:18 +0000531 isatty (fileno (stderr)))) /* error output is a terminal. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000532 init_interactive ();
Jari Aalto726f6381996-08-26 18:22:31 +0000533 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000534 init_noninteractive ();
Jari Aalto726f6381996-08-26 18:22:31 +0000535
Jari Aalto726f6381996-08-26 18:22:31 +0000536 /*
537 * Some systems have the bad habit of starting login shells with lots of open
538 * file descriptors. For instance, most systems that have picked up the
539 * pre-4.0 Sun YP code leave a file descriptor open each time you call one
540 * of the getpw* functions, and it's set to be open across execs. That
Chet Ramey495aee42011-11-22 19:11:26 -0500541 * means one for login, one for xterm, one for shelltool, etc. There are
542 * also systems that open persistent FDs to other agents or files as part
543 * of process startup; these need to be set to be close-on-exec.
Jari Aalto726f6381996-08-26 18:22:31 +0000544 */
545 if (login_shell && interactive_shell)
546 {
547 for (i = 3; i < 20; i++)
Chet Ramey495aee42011-11-22 19:11:26 -0500548 SET_CLOSE_ON_EXEC (i);
Jari Aalto726f6381996-08-26 18:22:31 +0000549 }
Jari Aalto726f6381996-08-26 18:22:31 +0000550
Jari Aalto7117c2d2002-07-17 14:10:11 +0000551 /* If we're in a strict Posix.2 mode, turn on interactive comments,
552 alias expansion in non-interactive shells, and other Posix.2 things. */
Jari Aaltod166f041997-06-05 14:59:13 +0000553 if (posixly_correct)
554 {
Jari Aalto95732b42005-12-07 14:08:12 +0000555 bind_variable ("POSIXLY_CORRECT", "y", 0);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000556 sv_strict_posix ("POSIXLY_CORRECT");
Jari Aaltod166f041997-06-05 14:59:13 +0000557 }
558
Jari Aaltof73dda02001-11-13 17:56:06 +0000559 /* Now we run the shopt_alist and process the options. */
560 if (shopt_alist)
561 run_shopt_alist ();
562
Jari Aalto726f6381996-08-26 18:22:31 +0000563 /* From here on in, the shell must be a normal functioning shell.
564 Variables from the environment are expected to be set, etc. */
565 shell_initialize ();
566
Jari Aalto06285672006-10-10 14:15:34 +0000567 set_default_lang ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000568 set_default_locale_vars ();
569
Jari Aalto31859422009-01-12 13:36:28 +0000570 /*
571 * M-x term -> TERM=eterm EMACS=22.1 (term:0.96) (eterm)
572 * M-x shell -> TERM=dumb EMACS=t (no line editing)
573 * M-x terminal -> TERM=emacs-em7955 EMACS= (line editing)
574 */
Jari Aalto726f6381996-08-26 18:22:31 +0000575 if (interactive_shell)
576 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000577 char *term, *emacs;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000578
Jari Aaltob80f6442004-07-27 13:29:18 +0000579 term = get_string_value ("TERM");
Jari Aaltob80f6442004-07-27 13:29:18 +0000580 emacs = get_string_value ("EMACS");
Jari Aalto31859422009-01-12 13:36:28 +0000581
582 /* Not sure any emacs terminal emulator sets TERM=emacs any more */
583 no_line_editing |= term && (STREQ (term, "emacs"));
Jari Aaltob80f6442004-07-27 13:29:18 +0000584 no_line_editing |= emacs && emacs[0] == 't' && emacs[1] == '\0' && STREQ (term, "dumb");
Jari Aalto31859422009-01-12 13:36:28 +0000585
586 /* running_under_emacs == 2 for `eterm' */
587 running_under_emacs = (emacs != 0) || (term && STREQN (term, "emacs", 5));
Chet Ramey00018032011-11-21 20:51:19 -0500588 running_under_emacs += term && STREQN (term, "eterm", 5) && emacs && strstr (emacs, "term");
Jari Aalto31859422009-01-12 13:36:28 +0000589
Jari Aalto95732b42005-12-07 14:08:12 +0000590 if (running_under_emacs)
591 gnu_error_format = 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000592 }
593
594 top_level_arg_index = arg_index;
Jari Aaltob72432f1999-02-19 17:11:39 +0000595 old_errexit_flag = exit_immediately_on_error;
Jari Aalto726f6381996-08-26 18:22:31 +0000596
Jari Aalto726f6381996-08-26 18:22:31 +0000597 /* Give this shell a place to longjmp to before executing the
598 startup files. This allows users to press C-c to abort the
599 lengthy startup. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000600 code = setjmp (top_level);
601 if (code)
602 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000603 if (code == EXITPROG || code == ERREXIT)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000604 exit_shell (last_command_exit_value);
605 else
Jari Aaltod166f041997-06-05 14:59:13 +0000606 {
607#if defined (JOB_CONTROL)
608 /* Reset job control, since run_startup_files turned it off. */
609 set_job_control (interactive_shell);
610#endif
Jari Aaltob72432f1999-02-19 17:11:39 +0000611 /* Reset value of `set -e', since it's turned off before running
612 the startup files. */
613 exit_immediately_on_error += old_errexit_flag;
Jari Aaltod166f041997-06-05 14:59:13 +0000614 locally_skip_execution++;
615 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000616 }
Jari Aalto726f6381996-08-26 18:22:31 +0000617
618 arg_index = top_level_arg_index;
619
620 /* Execute the start-up scripts. */
621
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000622 if (interactive_shell == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000623 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000624 unbind_variable ("PS1");
625 unbind_variable ("PS2");
Jari Aalto28ef6c32001-04-06 19:14:31 +0000626 interactive = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000627#if 0
628 /* This has already been done by init_noninteractive */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000629 expand_aliases = posixly_correct;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000630#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000631 }
632 else
633 {
634 change_flag ('i', FLAG_ON);
635 interactive = 1;
636 }
637
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000638#if defined (RESTRICTED_SHELL)
Jari Aaltob72432f1999-02-19 17:11:39 +0000639 /* Set restricted_shell based on whether the basename of $0 indicates that
640 the shell should be restricted or if the `-r' option was supplied at
641 startup. */
642 restricted_shell = shell_is_restricted (shell_name);
643
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000644 /* If the `-r' option is supplied at invocation, make sure that the shell
645 is not in restricted mode when running the startup files. */
Jari Aaltob72432f1999-02-19 17:11:39 +0000646 saverst = restricted;
647 restricted = 0;
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000648#endif
649
Jari Aaltob72432f1999-02-19 17:11:39 +0000650 /* The startup files are run with `set -e' temporarily disabled. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000651 if (locally_skip_execution == 0 && running_setuid == 0)
Jari Aaltob72432f1999-02-19 17:11:39 +0000652 {
653 old_errexit_flag = exit_immediately_on_error;
654 exit_immediately_on_error = 0;
655
656 run_startup_files ();
Jari Aaltob72432f1999-02-19 17:11:39 +0000657 exit_immediately_on_error += old_errexit_flag;
658 }
Jari Aalto726f6381996-08-26 18:22:31 +0000659
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000660 /* If we are invoked as `sh', turn on Posix mode. */
661 if (act_like_sh)
Jari Aaltod166f041997-06-05 14:59:13 +0000662 {
Jari Aalto95732b42005-12-07 14:08:12 +0000663 bind_variable ("POSIXLY_CORRECT", "y", 0);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000664 sv_strict_posix ("POSIXLY_CORRECT");
Jari Aaltod166f041997-06-05 14:59:13 +0000665 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000666
Jari Aalto726f6381996-08-26 18:22:31 +0000667#if defined (RESTRICTED_SHELL)
Jari Aaltocce855b1998-04-17 19:52:44 +0000668 /* Turn on the restrictions after executing the startup files. This
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000669 means that `bash -r' or `set -r' invoked from a startup file will
670 turn on the restrictions after the startup files are executed. */
671 restricted = saverst || restricted;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000672 if (shell_reinitialized == 0)
673 maybe_make_restricted (shell_name);
Jari Aalto726f6381996-08-26 18:22:31 +0000674#endif /* RESTRICTED_SHELL */
675
Chet Ramey00018032011-11-21 20:51:19 -0500676#if defined (WORDEXP_OPTION)
Jari Aaltocce855b1998-04-17 19:52:44 +0000677 if (wordexp_only)
678 {
679 startup_state = 3;
680 last_command_exit_value = run_wordexp (argv[arg_index]);
681 exit_shell (last_command_exit_value);
682 }
Chet Ramey00018032011-11-21 20:51:19 -0500683#endif
Jari Aaltocce855b1998-04-17 19:52:44 +0000684
Jari Aaltob80f6442004-07-27 13:29:18 +0000685 if (command_execution_string)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000686 {
687 arg_index = bind_args (argv, arg_index, argc, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000688 startup_state = 2;
Jari Aaltob80f6442004-07-27 13:29:18 +0000689
690 if (debugging_mode)
691 start_debugger ();
692
Jari Aalto726f6381996-08-26 18:22:31 +0000693#if defined (ONESHOT)
Jari Aaltof73dda02001-11-13 17:56:06 +0000694 executing = 1;
Jari Aaltob80f6442004-07-27 13:29:18 +0000695 run_one_command (command_execution_string);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000696 exit_shell (last_command_exit_value);
Jari Aalto726f6381996-08-26 18:22:31 +0000697#else /* ONESHOT */
Jari Aaltob80f6442004-07-27 13:29:18 +0000698 with_input_from_string (command_execution_string, "-c");
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000699 goto read_and_execute;
Jari Aalto726f6381996-08-26 18:22:31 +0000700#endif /* !ONESHOT */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000701 }
702
703 /* Get possible input filename and set up default_buffered_input or
704 default_input as appropriate. */
705 if (arg_index != argc && read_from_stdin == 0)
706 {
707 open_shell_script (argv[arg_index]);
708 arg_index++;
709 }
710 else if (interactive == 0)
711 /* In this mode, bash is reading a script from stdin, which is a
712 pipe or redirected file. */
713#if defined (BUFFERED_INPUT)
714 default_buffered_input = fileno (stdin); /* == 0 */
715#else
716 setbuf (default_input, (char *)NULL);
717#endif /* !BUFFERED_INPUT */
718
719 set_bash_input ();
720
721 /* Bind remaining args to $1 ... $n */
722 arg_index = bind_args (argv, arg_index, argc, 1);
Jari Aaltob80f6442004-07-27 13:29:18 +0000723
Chet Rameyac50fba2014-02-26 09:36:43 -0500724 if (debugging_mode && locally_skip_execution == 0 && running_setuid == 0 && dollar_vars[1])
Jari Aaltob80f6442004-07-27 13:29:18 +0000725 start_debugger ();
726
Jari Aalto726f6381996-08-26 18:22:31 +0000727 /* Do the things that should be done only for interactive shells. */
728 if (interactive_shell)
729 {
730 /* Set up for checking for presence of mail. */
Jari Aalto726f6381996-08-26 18:22:31 +0000731 reset_mail_timer ();
Jari Aalto31859422009-01-12 13:36:28 +0000732 init_mail_dates ();
Jari Aalto726f6381996-08-26 18:22:31 +0000733
734#if defined (HISTORY)
735 /* Initialize the interactive history stuff. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000736 bash_initialize_history ();
Jari Aalto7117c2d2002-07-17 14:10:11 +0000737 /* Don't load the history from the history file if we've already
738 saved some lines in this session (e.g., by putting `history -s xx'
739 into one of the startup files). */
740 if (shell_initialized == 0 && history_lines_this_session == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000741 load_history ();
742#endif /* HISTORY */
743
744 /* Initialize terminal state for interactive shells after the
745 .bash_profile and .bashrc are interpreted. */
746 get_tty_state ();
747 }
748
Jari Aalto726f6381996-08-26 18:22:31 +0000749#if !defined (ONESHOT)
750 read_and_execute:
751#endif /* !ONESHOT */
752
753 shell_initialized = 1;
754
755 /* Read commands until exit condition. */
756 reader_loop ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000757 exit_shell (last_command_exit_value);
758}
Jari Aalto726f6381996-08-26 18:22:31 +0000759
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000760static int
761parse_long_options (argv, arg_start, arg_end)
762 char **argv;
763 int arg_start, arg_end;
764{
765 int arg_index, longarg, i;
766 char *arg_string;
767
768 arg_index = arg_start;
769 while ((arg_index != arg_end) && (arg_string = argv[arg_index]) &&
770 (*arg_string == '-'))
771 {
772 longarg = 0;
773
774 /* Make --login equivalent to -login. */
775 if (arg_string[1] == '-' && arg_string[2])
776 {
777 longarg = 1;
778 arg_string++;
779 }
780
781 for (i = 0; long_args[i].name; i++)
782 {
783 if (STREQ (arg_string + 1, long_args[i].name))
784 {
785 if (long_args[i].type == Int)
786 *long_args[i].int_value = 1;
787 else if (argv[++arg_index] == 0)
788 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000789 report_error (_("%s: option requires an argument"), long_args[i].name);
790 exit (EX_BADUSAGE);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000791 }
792 else
793 *long_args[i].char_value = argv[arg_index];
794
795 break;
796 }
797 }
798 if (long_args[i].name == 0)
799 {
800 if (longarg)
801 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000802 report_error (_("%s: invalid option"), argv[arg_index]);
Jari Aaltod166f041997-06-05 14:59:13 +0000803 show_shell_usage (stderr, 0);
Jari Aaltob80f6442004-07-27 13:29:18 +0000804 exit (EX_BADUSAGE);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000805 }
806 break; /* No such argument. Maybe flag arg. */
807 }
808
809 arg_index++;
810 }
811
812 return (arg_index);
813}
814
815static int
816parse_shell_options (argv, arg_start, arg_end)
817 char **argv;
818 int arg_start, arg_end;
819{
820 int arg_index;
821 int arg_character, on_or_off, next_arg, i;
822 char *o_option, *arg_string;
823
824 arg_index = arg_start;
825 while (arg_index != arg_end && (arg_string = argv[arg_index]) &&
826 (*arg_string == '-' || *arg_string == '+'))
827 {
828 /* There are flag arguments, so parse them. */
829 next_arg = arg_index + 1;
830
831 /* A single `-' signals the end of options. From the 4.3 BSD sh.
832 An option `--' means the same thing; this is the standard
833 getopt(3) meaning. */
834 if (arg_string[0] == '-' &&
835 (arg_string[1] == '\0' ||
836 (arg_string[1] == '-' && arg_string[2] == '\0')))
837 return (next_arg);
838
839 i = 1;
840 on_or_off = arg_string[0];
841 while (arg_character = arg_string[i++])
842 {
843 switch (arg_character)
844 {
845 case 'c':
846 want_pending_command = 1;
847 break;
848
Jari Aalto7117c2d2002-07-17 14:10:11 +0000849 case 'l':
850 make_login_shell = 1;
851 break;
852
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000853 case 's':
854 read_from_stdin = 1;
855 break;
856
857 case 'o':
858 o_option = argv[next_arg];
859 if (o_option == 0)
860 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000861 list_minus_o_opts (-1, (on_or_off == '-') ? 0 : 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000862 break;
863 }
864 if (set_minus_o_option (on_or_off, o_option) != EXECUTION_SUCCESS)
Jari Aaltob80f6442004-07-27 13:29:18 +0000865 exit (EX_BADUSAGE);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000866 next_arg++;
867 break;
868
Jari Aaltof73dda02001-11-13 17:56:06 +0000869 case 'O':
870 /* Since some of these can be overridden by the normal
871 interactive/non-interactive shell initialization or
872 initializing posix mode, we save the options and process
873 them after initialization. */
874 o_option = argv[next_arg];
875 if (o_option == 0)
876 {
877 shopt_listopt (o_option, (on_or_off == '-') ? 0 : 1);
878 break;
879 }
880 add_shopt_to_alist (o_option, on_or_off);
881 next_arg++;
882 break;
883
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000884 case 'D':
885 dump_translatable_strings = 1;
886 break;
887
888 default:
889 if (change_flag (arg_character, on_or_off) == FLAG_ERROR)
890 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000891 report_error (_("%c%c: invalid option"), on_or_off, arg_character);
Jari Aaltod166f041997-06-05 14:59:13 +0000892 show_shell_usage (stderr, 0);
Jari Aaltob80f6442004-07-27 13:29:18 +0000893 exit (EX_BADUSAGE);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000894 }
895 }
896 }
897 /* Can't do just a simple increment anymore -- what about
898 "bash -abouo emacs ignoreeof -hP"? */
899 arg_index = next_arg;
900 }
901
902 return (arg_index);
903}
904
905/* Exit the shell with status S. */
Jari Aaltocce855b1998-04-17 19:52:44 +0000906void
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000907exit_shell (s)
908 int s;
909{
Chet Ramey00018032011-11-21 20:51:19 -0500910 fflush (stdout); /* XXX */
911 fflush (stderr);
912
Chet Ramey84c617e2015-01-15 10:21:08 -0500913 /* Clean up the terminal if we are in a state where it's been modified. */
914#if defined (READLINE)
915 if (RL_ISSTATE (RL_STATE_TERMPREPPED) && rl_deprep_term_function)
916 (*rl_deprep_term_function) ();
917#endif
918 if (read_tty_modified ())
919 read_tty_cleanup ();
920
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000921 /* Do trap[0] if defined. Allow it to override the exit status
922 passed to us. */
Jari Aalto726f6381996-08-26 18:22:31 +0000923 if (signal_is_trapped (0))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000924 s = run_exit_trap ();
Jari Aalto726f6381996-08-26 18:22:31 +0000925
926#if defined (PROCESS_SUBSTITUTION)
927 unlink_fifo_list ();
928#endif /* PROCESS_SUBSTITUTION */
929
930#if defined (HISTORY)
Chet Rameyac50fba2014-02-26 09:36:43 -0500931 if (remember_on_history)
Jari Aalto726f6381996-08-26 18:22:31 +0000932 maybe_save_shell_history ();
933#endif /* HISTORY */
934
Jari Aalto31859422009-01-12 13:36:28 +0000935#if defined (COPROCESS_SUPPORT)
936 coproc_flush ();
937#endif
938
Jari Aalto726f6381996-08-26 18:22:31 +0000939#if defined (JOB_CONTROL)
Jari Aaltocce855b1998-04-17 19:52:44 +0000940 /* If the user has run `shopt -s huponexit', hangup all jobs when we exit
941 an interactive login shell. ksh does this unconditionally. */
942 if (interactive_shell && login_shell && hup_on_exit)
943 hangup_all_jobs ();
944
Jari Aalto726f6381996-08-26 18:22:31 +0000945 /* If this shell is interactive, terminate all stopped jobs and
Jari Aalto28ef6c32001-04-06 19:14:31 +0000946 restore the original terminal process group. Don't do this if we're
947 in a subshell and calling exit_shell after, for example, a failed
948 word expansion. */
949 if (subshell_environment == 0)
950 end_job_control ();
Jari Aalto726f6381996-08-26 18:22:31 +0000951#endif /* JOB_CONTROL */
952
953 /* Always return the exit status of the last command to our parent. */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000954 sh_exit (s);
Jari Aalto726f6381996-08-26 18:22:31 +0000955}
956
Jari Aaltobb706242000-03-17 21:46:59 +0000957/* A wrapper for exit that (optionally) can do other things, like malloc
958 statistics tracing. */
959void
960sh_exit (s)
961 int s;
962{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000963#if defined (MALLOC_DEBUG) && defined (USING_BASH_MALLOC)
964 if (malloc_trace_at_exit)
965 trace_malloc_stats (get_name_for_error (), (char *)NULL);
966#endif
967
Jari Aaltobb706242000-03-17 21:46:59 +0000968 exit (s);
969}
970
Chet Rameyac50fba2014-02-26 09:36:43 -0500971/* Exit a subshell, which includes calling the exit trap. We don't want to
972 do any more cleanup, since a subshell is created as an exact copy of its
973 parent. */
974void
975subshell_exit (s)
976 int s;
977{
978 fflush (stdout);
979 fflush (stderr);
980
981 /* Do trap[0] if defined. Allow it to override the exit status
982 passed to us. */
983 if (signal_is_trapped (0))
984 s = run_exit_trap ();
985
986 sh_exit (s);
987}
988
Jari Aalto726f6381996-08-26 18:22:31 +0000989/* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey
990 the Posix.2 startup file rules: $ENV is expanded, and if the file it
991 names exists, that file is sourced. The Posix.2 rules are in effect
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000992 for interactive shells only. (section 4.56.5.3) */
Jari Aalto726f6381996-08-26 18:22:31 +0000993
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000994/* Execute ~/.bashrc for most shells. Never execute it if
995 ACT_LIKE_SH is set, or if NO_RC is set.
Jari Aalto726f6381996-08-26 18:22:31 +0000996
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000997 If the executable file "/usr/gnu/src/bash/foo" contains:
Jari Aalto726f6381996-08-26 18:22:31 +0000998
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000999 #!/usr/gnu/bin/bash
1000 echo hello
Jari Aalto726f6381996-08-26 18:22:31 +00001001
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001002 then:
Jari Aalto726f6381996-08-26 18:22:31 +00001003
Jari Aalto28ef6c32001-04-06 19:14:31 +00001004 COMMAND EXECUTE BASHRC
1005 --------------------------------
1006 bash -c foo NO
1007 bash foo NO
1008 foo NO
1009 rsh machine ls YES (for rsh, which calls `bash -c')
1010 rsh machine foo YES (for shell started by rsh) NO (for foo!)
1011 echo ls | bash NO
1012 login NO
1013 bash YES
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001014*/
Jari Aalto726f6381996-08-26 18:22:31 +00001015
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001016static void
1017execute_env_file (env_file)
1018 char *env_file;
1019{
1020 char *fn;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001021
1022 if (env_file && *env_file)
Jari Aalto726f6381996-08-26 18:22:31 +00001023 {
Jari Aaltof73dda02001-11-13 17:56:06 +00001024 fn = expand_string_unsplit_to_string (env_file, Q_DOUBLE_QUOTES);
1025 if (fn && *fn)
1026 maybe_execute_file (fn, 1);
1027 FREE (fn);
Jari Aalto726f6381996-08-26 18:22:31 +00001028 }
1029}
1030
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001031static void
1032run_startup_files ()
1033{
Jari Aaltod166f041997-06-05 14:59:13 +00001034#if defined (JOB_CONTROL)
1035 int old_job_control;
1036#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00001037 int sourced_login, run_by_ssh;
Jari Aaltod166f041997-06-05 14:59:13 +00001038
Jari Aaltocce855b1998-04-17 19:52:44 +00001039 /* get the rshd/sshd case out of the way first. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001040 if (interactive_shell == 0 && no_rc == 0 && login_shell == 0 &&
Jari Aaltob80f6442004-07-27 13:29:18 +00001041 act_like_sh == 0 && command_execution_string)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001042 {
Jari Aaltof73dda02001-11-13 17:56:06 +00001043#ifdef SSH_SOURCE_BASHRC
1044 run_by_ssh = (find_variable ("SSH_CLIENT") != (SHELL_VAR *)0) ||
1045 (find_variable ("SSH2_CLIENT") != (SHELL_VAR *)0);
1046#else
1047 run_by_ssh = 0;
1048#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00001049
1050 /* If we were run by sshd or we think we were run by rshd, execute
Jari Aaltobb706242000-03-17 21:46:59 +00001051 ~/.bashrc if we are a top-level shell. */
1052 if ((run_by_ssh || isnetconn (fileno (stdin))) && shell_level < 2)
Jari Aaltocce855b1998-04-17 19:52:44 +00001053 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001054#ifdef SYS_BASHRC
Jari Aaltob72432f1999-02-19 17:11:39 +00001055# if defined (__OPENNT)
1056 maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
1057# else
Jari Aaltocce855b1998-04-17 19:52:44 +00001058 maybe_execute_file (SYS_BASHRC, 1);
Jari Aaltob72432f1999-02-19 17:11:39 +00001059# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001060#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00001061 maybe_execute_file (bashrc_file, 1);
1062 return;
1063 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001064 }
1065
Jari Aaltocce855b1998-04-17 19:52:44 +00001066#if defined (JOB_CONTROL)
1067 /* Startup files should be run without job control enabled. */
1068 old_job_control = interactive_shell ? set_job_control (0) : 0;
1069#endif
1070
1071 sourced_login = 0;
1072
Jari Aalto7117c2d2002-07-17 14:10:11 +00001073 /* A shell begun with the --login (or -l) flag that is not in posix mode
1074 runs the login shell startup files, no matter whether or not it is
Jari Aaltobb706242000-03-17 21:46:59 +00001075 interactive. If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
1076 startup files if argv[0][0] == '-' as well. */
1077#if defined (NON_INTERACTIVE_LOGIN_SHELLS)
1078 if (login_shell && posixly_correct == 0)
1079#else
1080 if (login_shell < 0 && posixly_correct == 0)
1081#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00001082 {
1083 /* We don't execute .bashrc for login shells. */
1084 no_rc++;
1085
1086 /* Execute /etc/profile and one of the personal login shell
1087 initialization files. */
1088 if (no_profile == 0)
1089 {
1090 maybe_execute_file (SYS_PROFILE, 1);
1091
1092 if (act_like_sh) /* sh */
1093 maybe_execute_file ("~/.profile", 1);
1094 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
1095 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
1096 maybe_execute_file ("~/.profile", 1);
1097 }
1098
1099 sourced_login = 1;
1100 }
Jari Aaltocce855b1998-04-17 19:52:44 +00001101
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001102 /* A non-interactive shell not named `sh' and not in posix mode reads and
1103 executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd'
1104 and `-su' as the name of the shell, we want to read the startup files.
1105 No other non-interactive shells read any startup files. */
1106 if (interactive_shell == 0 && !(su_shell && login_shell))
1107 {
1108 if (posixly_correct == 0 && act_like_sh == 0 && privileged_mode == 0 &&
1109 sourced_env++ == 0)
1110 execute_env_file (get_string_value ("BASH_ENV"));
1111 return;
1112 }
1113
1114 /* Interactive shell or `-su' shell. */
1115 if (posixly_correct == 0) /* bash, sh */
1116 {
Jari Aaltocce855b1998-04-17 19:52:44 +00001117 if (login_shell && sourced_login++ == 0)
1118 {
1119 /* We don't execute .bashrc for login shells. */
1120 no_rc++;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001121
Jari Aaltocce855b1998-04-17 19:52:44 +00001122 /* Execute /etc/profile and one of the personal login shell
1123 initialization files. */
1124 if (no_profile == 0)
1125 {
1126 maybe_execute_file (SYS_PROFILE, 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001127
Jari Aaltocce855b1998-04-17 19:52:44 +00001128 if (act_like_sh) /* sh */
1129 maybe_execute_file ("~/.profile", 1);
1130 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
1131 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
1132 maybe_execute_file ("~/.profile", 1);
1133 }
1134 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001135
1136 /* bash */
1137 if (act_like_sh == 0 && no_rc == 0)
1138 {
1139#ifdef SYS_BASHRC
Jari Aaltob72432f1999-02-19 17:11:39 +00001140# if defined (__OPENNT)
1141 maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
1142# else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001143 maybe_execute_file (SYS_BASHRC, 1);
Jari Aaltobb706242000-03-17 21:46:59 +00001144# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001145#endif
Jari Aalto28ef6c32001-04-06 19:14:31 +00001146 maybe_execute_file (bashrc_file, 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001147 }
1148 /* sh */
1149 else if (act_like_sh && privileged_mode == 0 && sourced_env++ == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001150 execute_env_file (get_string_value ("ENV"));
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001151 }
1152 else /* bash --posix, sh --posix */
1153 {
1154 /* bash and sh */
1155 if (interactive_shell && privileged_mode == 0 && sourced_env++ == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001156 execute_env_file (get_string_value ("ENV"));
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001157 }
Jari Aaltod166f041997-06-05 14:59:13 +00001158
1159#if defined (JOB_CONTROL)
1160 set_job_control (old_job_control);
1161#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001162}
1163
Jari Aalto726f6381996-08-26 18:22:31 +00001164#if defined (RESTRICTED_SHELL)
Jari Aaltob72432f1999-02-19 17:11:39 +00001165/* Return 1 if the shell should be a restricted one based on NAME or the
1166 value of `restricted'. Don't actually do anything, just return a
1167 boolean value. */
1168int
1169shell_is_restricted (name)
1170 char *name;
1171{
1172 char *temp;
1173
1174 if (restricted)
1175 return 1;
1176 temp = base_pathname (name);
Jari Aalto95732b42005-12-07 14:08:12 +00001177 if (*temp == '-')
1178 temp++;
Jari Aaltob72432f1999-02-19 17:11:39 +00001179 return (STREQ (temp, RESTRICTED_SHELL_NAME));
1180}
1181
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001182/* Perhaps make this shell a `restricted' one, based on NAME. If the
1183 basename of NAME is "rbash", then this shell is restricted. The
1184 name of the restricted shell is a configurable option, see config.h.
Jari Aaltob72432f1999-02-19 17:11:39 +00001185 In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
1186 and non-unsettable.
Jari Aalto726f6381996-08-26 18:22:31 +00001187 Do this also if `restricted' is already set to 1; maybe the shell was
1188 started with -r. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001189int
Jari Aalto726f6381996-08-26 18:22:31 +00001190maybe_make_restricted (name)
1191 char *name;
1192{
1193 char *temp;
1194
Jari Aalto7117c2d2002-07-17 14:10:11 +00001195 temp = base_pathname (name);
Jari Aaltob80f6442004-07-27 13:29:18 +00001196 if (*temp == '-')
1197 temp++;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001198 if (restricted || (STREQ (temp, RESTRICTED_SHELL_NAME)))
Jari Aalto726f6381996-08-26 18:22:31 +00001199 {
1200 set_var_read_only ("PATH");
Jari Aalto726f6381996-08-26 18:22:31 +00001201 set_var_read_only ("SHELL");
Jari Aaltob72432f1999-02-19 17:11:39 +00001202 set_var_read_only ("ENV");
1203 set_var_read_only ("BASH_ENV");
Jari Aalto28ef6c32001-04-06 19:14:31 +00001204 restricted = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00001205 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001206 return (restricted);
Jari Aalto726f6381996-08-26 18:22:31 +00001207}
1208#endif /* RESTRICTED_SHELL */
1209
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001210/* Fetch the current set of uids and gids and return 1 if we're running
1211 setuid or setgid. */
1212static int
1213uidget ()
Jari Aalto726f6381996-08-26 18:22:31 +00001214{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001215 uid_t u;
Jari Aalto726f6381996-08-26 18:22:31 +00001216
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001217 u = getuid ();
1218 if (current_user.uid != u)
Jari Aalto726f6381996-08-26 18:22:31 +00001219 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001220 FREE (current_user.user_name);
1221 FREE (current_user.shell);
1222 FREE (current_user.home_dir);
1223 current_user.user_name = current_user.shell = current_user.home_dir = (char *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +00001224 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001225 current_user.uid = u;
1226 current_user.gid = getgid ();
1227 current_user.euid = geteuid ();
1228 current_user.egid = getegid ();
Jari Aalto726f6381996-08-26 18:22:31 +00001229
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001230 /* See whether or not we are running setuid or setgid. */
1231 return (current_user.uid != current_user.euid) ||
1232 (current_user.gid != current_user.egid);
1233}
Jari Aalto726f6381996-08-26 18:22:31 +00001234
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001235void
1236disable_priv_mode ()
1237{
1238 setuid (current_user.uid);
1239 setgid (current_user.gid);
1240 current_user.euid = current_user.uid;
1241 current_user.egid = current_user.gid;
Jari Aalto726f6381996-08-26 18:22:31 +00001242}
1243
Chet Ramey00018032011-11-21 20:51:19 -05001244#if defined (WORDEXP_OPTION)
Jari Aaltocce855b1998-04-17 19:52:44 +00001245static int
1246run_wordexp (words)
1247 char *words;
1248{
1249 int code, nw, nb;
Jari Aaltob80f6442004-07-27 13:29:18 +00001250 WORD_LIST *wl, *tl, *result;
Jari Aaltocce855b1998-04-17 19:52:44 +00001251
Chet Rameyac50fba2014-02-26 09:36:43 -05001252 code = setjmp_nosigs (top_level);
Jari Aaltocce855b1998-04-17 19:52:44 +00001253
1254 if (code != NOT_JUMPED)
1255 {
1256 switch (code)
1257 {
Chet Rameyac50fba2014-02-26 09:36:43 -05001258 /* Some kind of throw to top_level has occurred. */
Jari Aaltocce855b1998-04-17 19:52:44 +00001259 case FORCE_EOF:
1260 return last_command_exit_value = 127;
Jari Aaltob80f6442004-07-27 13:29:18 +00001261 case ERREXIT:
Jari Aaltocce855b1998-04-17 19:52:44 +00001262 case EXITPROG:
1263 return last_command_exit_value;
1264 case DISCARD:
1265 return last_command_exit_value = 1;
1266 default:
Jari Aaltob72432f1999-02-19 17:11:39 +00001267 command_error ("run_wordexp", CMDERR_BADJUMP, code, 0);
Jari Aaltocce855b1998-04-17 19:52:44 +00001268 }
1269 }
1270
1271 /* Run it through the parser to get a list of words and expand them */
1272 if (words && *words)
1273 {
1274 with_input_from_string (words, "--wordexp");
1275 if (parse_command () != 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001276 return (126);
Jari Aaltocce855b1998-04-17 19:52:44 +00001277 if (global_command == 0)
1278 {
1279 printf ("0\n0\n");
1280 return (0);
1281 }
1282 if (global_command->type != cm_simple)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001283 return (126);
Jari Aaltocce855b1998-04-17 19:52:44 +00001284 wl = global_command->value.Simple->words;
Jari Aaltob80f6442004-07-27 13:29:18 +00001285 if (protected_mode)
1286 for (tl = wl; tl; tl = tl->next)
Jari Aalto06285672006-10-10 14:15:34 +00001287 tl->word->flags |= W_NOCOMSUB|W_NOPROCSUB;
Jari Aaltocce855b1998-04-17 19:52:44 +00001288 result = wl ? expand_words_no_vars (wl) : (WORD_LIST *)0;
1289 }
1290 else
1291 result = (WORD_LIST *)0;
1292
1293 last_command_exit_value = 0;
1294
1295 if (result == 0)
1296 {
1297 printf ("0\n0\n");
1298 return (0);
1299 }
1300
1301 /* Count up the number of words and bytes, and print them. Don't count
1302 the trailing NUL byte. */
1303 for (nw = nb = 0, wl = result; wl; wl = wl->next)
1304 {
1305 nw++;
1306 nb += strlen (wl->word->word);
1307 }
1308 printf ("%u\n%u\n", nw, nb);
1309 /* Print each word on a separate line. This will have to be changed when
1310 the interface to glibc is completed. */
1311 for (wl = result; wl; wl = wl->next)
1312 printf ("%s\n", wl->word->word);
1313
1314 return (0);
1315}
Chet Ramey00018032011-11-21 20:51:19 -05001316#endif
Jari Aaltocce855b1998-04-17 19:52:44 +00001317
Jari Aalto726f6381996-08-26 18:22:31 +00001318#if defined (ONESHOT)
1319/* Run one command, given as the argument to the -c option. Tell
1320 parse_and_execute not to fork for a simple command. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001321static int
Jari Aalto726f6381996-08-26 18:22:31 +00001322run_one_command (command)
1323 char *command;
1324{
1325 int code;
1326
Chet Rameyac50fba2014-02-26 09:36:43 -05001327 code = setjmp_nosigs (top_level);
Jari Aalto726f6381996-08-26 18:22:31 +00001328
1329 if (code != NOT_JUMPED)
1330 {
1331#if defined (PROCESS_SUBSTITUTION)
1332 unlink_fifo_list ();
1333#endif /* PROCESS_SUBSTITUTION */
1334 switch (code)
1335 {
Chet Rameyac50fba2014-02-26 09:36:43 -05001336 /* Some kind of throw to top_level has occurred. */
Jari Aalto726f6381996-08-26 18:22:31 +00001337 case FORCE_EOF:
1338 return last_command_exit_value = 127;
Jari Aaltob80f6442004-07-27 13:29:18 +00001339 case ERREXIT:
Jari Aalto726f6381996-08-26 18:22:31 +00001340 case EXITPROG:
1341 return last_command_exit_value;
1342 case DISCARD:
1343 return last_command_exit_value = 1;
1344 default:
Jari Aaltob72432f1999-02-19 17:11:39 +00001345 command_error ("run_one_command", CMDERR_BADJUMP, code, 0);
Jari Aalto726f6381996-08-26 18:22:31 +00001346 }
1347 }
Jari Aaltod166f041997-06-05 14:59:13 +00001348 return (parse_and_execute (savestring (command), "-c", SEVAL_NOHIST));
Jari Aalto726f6381996-08-26 18:22:31 +00001349}
1350#endif /* ONESHOT */
1351
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001352static int
1353bind_args (argv, arg_start, arg_end, start_index)
1354 char **argv;
1355 int arg_start, arg_end, start_index;
Jari Aalto726f6381996-08-26 18:22:31 +00001356{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001357 register int i;
1358 WORD_LIST *args;
Jari Aalto726f6381996-08-26 18:22:31 +00001359
Jari Aalto95732b42005-12-07 14:08:12 +00001360 for (i = arg_start, args = (WORD_LIST *)NULL; i < arg_end; i++)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001361 args = make_word_list (make_word (argv[i]), args);
1362 if (args)
Jari Aalto726f6381996-08-26 18:22:31 +00001363 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001364 args = REVERSE_LIST (args, WORD_LIST *);
1365 if (start_index == 0) /* bind to $0...$n for sh -c command */
Jari Aalto726f6381996-08-26 18:22:31 +00001366 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001367 /* Posix.2 4.56.3 says that the first argument after sh -c command
1368 becomes $0, and the rest of the arguments become $1...$n */
1369 shell_name = savestring (args->word->word);
Jari Aaltod166f041997-06-05 14:59:13 +00001370 FREE (dollar_vars[0]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001371 dollar_vars[0] = savestring (args->word->word);
1372 remember_args (args->next, 1);
Jari Aaltob80f6442004-07-27 13:29:18 +00001373 push_args (args->next); /* BASH_ARGV and BASH_ARGC */
Jari Aalto726f6381996-08-26 18:22:31 +00001374 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001375 else /* bind to $1...$n for shell script */
Jari Aaltob80f6442004-07-27 13:29:18 +00001376 {
1377 remember_args (args, 1);
1378 push_args (args); /* BASH_ARGV and BASH_ARGC */
1379 }
Jari Aalto726f6381996-08-26 18:22:31 +00001380
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001381 dispose_words (args);
1382 }
Jari Aalto726f6381996-08-26 18:22:31 +00001383
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001384 return (i);
1385}
1386
1387void
1388unbind_args ()
1389{
1390 remember_args ((WORD_LIST *)NULL, 1);
Jari Aaltob80f6442004-07-27 13:29:18 +00001391 pop_args (); /* Reset BASH_ARGV and BASH_ARGC */
1392}
1393
1394static void
1395start_debugger ()
1396{
1397#if defined (DEBUGGER) && defined (DEBUGGER_START_FILE)
1398 int old_errexit;
1399
1400 old_errexit = exit_immediately_on_error;
1401 exit_immediately_on_error = 0;
1402
1403 maybe_execute_file (DEBUGGER_START_FILE, 1);
1404 function_trace_mode = 1;
1405
1406 exit_immediately_on_error += old_errexit;
1407#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001408}
1409
1410static int
1411open_shell_script (script_name)
1412 char *script_name;
1413{
Jari Aalto28ef6c32001-04-06 19:14:31 +00001414 int fd, e, fd_is_tty;
Jari Aaltob80f6442004-07-27 13:29:18 +00001415 char *filename, *path_filename, *t;
Jari Aaltof73dda02001-11-13 17:56:06 +00001416 char sample[80];
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001417 int sample_len;
Jari Aaltod166f041997-06-05 14:59:13 +00001418 struct stat sb;
Jari Aaltob80f6442004-07-27 13:29:18 +00001419#if defined (ARRAY_VARS)
1420 SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
1421 ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
1422#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001423
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001424 filename = savestring (script_name);
1425
1426 fd = open (filename, O_RDONLY);
1427 if ((fd < 0) && (errno == ENOENT) && (absolute_program (filename) == 0))
1428 {
Jari Aaltod166f041997-06-05 14:59:13 +00001429 e = errno;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001430 /* If it's not in the current directory, try looking through PATH
1431 for it. */
1432 path_filename = find_path_file (script_name);
1433 if (path_filename)
1434 {
1435 free (filename);
1436 filename = path_filename;
1437 fd = open (filename, O_RDONLY);
1438 }
Jari Aaltod166f041997-06-05 14:59:13 +00001439 else
1440 errno = e;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001441 }
1442
1443 if (fd < 0)
1444 {
Jari Aaltod166f041997-06-05 14:59:13 +00001445 e = errno;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001446 file_error (filename);
1447 exit ((e == ENOENT) ? EX_NOTFOUND : EX_NOINPUT);
1448 }
1449
Jari Aaltob80f6442004-07-27 13:29:18 +00001450 free (dollar_vars[0]);
Chet Ramey495aee42011-11-22 19:11:26 -05001451 dollar_vars[0] = exec_argv0 ? savestring (exec_argv0) : savestring (script_name);
1452 if (exec_argv0)
1453 {
1454 free (exec_argv0);
1455 exec_argv0 = (char *)NULL;
1456 }
Jari Aaltob80f6442004-07-27 13:29:18 +00001457
1458#if defined (ARRAY_VARS)
1459 GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
1460 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
1461 GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
1462
1463 array_push (bash_source_a, filename);
1464 if (bash_lineno_a)
1465 {
1466 t = itos (executing_line_number ());
1467 array_push (bash_lineno_a, t);
1468 free (t);
1469 }
1470 array_push (funcname_a, "main");
1471#endif
1472
Jari Aalto28ef6c32001-04-06 19:14:31 +00001473#ifdef HAVE_DEV_FD
1474 fd_is_tty = isatty (fd);
1475#else
1476 fd_is_tty = 0;
1477#endif
1478
1479 /* Only do this with non-tty file descriptors we can seek on. */
1480 if (fd_is_tty == 0 && (lseek (fd, 0L, 1) != -1))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001481 {
1482 /* Check to see if the `file' in `bash file' is a binary file
1483 according to the same tests done by execute_simple_command (),
1484 and report an error and exit if it is. */
1485 sample_len = read (fd, sample, sizeof (sample));
Jari Aaltod166f041997-06-05 14:59:13 +00001486 if (sample_len < 0)
1487 {
1488 e = errno;
1489 if ((fstat (fd, &sb) == 0) && S_ISDIR (sb.st_mode))
Jari Aaltob80f6442004-07-27 13:29:18 +00001490 internal_error (_("%s: is a directory"), filename);
Jari Aalto28ef6c32001-04-06 19:14:31 +00001491 else
Jari Aaltod166f041997-06-05 14:59:13 +00001492 {
1493 errno = e;
1494 file_error (filename);
1495 }
1496 exit (EX_NOEXEC);
1497 }
1498 else if (sample_len > 0 && (check_binary_file (sample, sample_len)))
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001499 {
Jari Aalto31859422009-01-12 13:36:28 +00001500 internal_error (_("%s: cannot execute binary file"), filename);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001501 exit (EX_BINARY_FILE);
1502 }
1503 /* Now rewind the file back to the beginning. */
1504 lseek (fd, 0L, 0);
1505 }
1506
Jari Aaltobb706242000-03-17 21:46:59 +00001507 /* Open the script. But try to move the file descriptor to a randomly
1508 large one, in the hopes that any descriptors used by the script will
1509 not match with ours. */
Jari Aalto31859422009-01-12 13:36:28 +00001510 fd = move_to_high_fd (fd, 1, -1);
Jari Aaltobb706242000-03-17 21:46:59 +00001511
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001512#if defined (BUFFERED_INPUT)
1513 default_buffered_input = fd;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001514 SET_CLOSE_ON_EXEC (default_buffered_input);
1515#else /* !BUFFERED_INPUT */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001516 default_input = fdopen (fd, "r");
1517
1518 if (default_input == 0)
1519 {
1520 file_error (filename);
1521 exit (EX_NOTFOUND);
1522 }
1523
1524 SET_CLOSE_ON_EXEC (fd);
1525 if (fileno (default_input) != fd)
1526 SET_CLOSE_ON_EXEC (fileno (default_input));
1527#endif /* !BUFFERED_INPUT */
1528
Jari Aalto28ef6c32001-04-06 19:14:31 +00001529 /* Just about the only way for this code to be executed is if something
1530 like `bash -i /dev/stdin' is executed. */
1531 if (interactive_shell && fd_is_tty)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001532 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001533 dup2 (fd, 0);
1534 close (fd);
1535 fd = 0;
1536#if defined (BUFFERED_INPUT)
1537 default_buffered_input = 0;
1538#else
1539 fclose (default_input);
1540 default_input = stdin;
1541#endif
1542 }
Jari Aaltof73dda02001-11-13 17:56:06 +00001543 else if (forced_interactive && fd_is_tty == 0)
1544 /* But if a script is called with something like `bash -i scriptname',
1545 we need to do a non-interactive setup here, since we didn't do it
1546 before. */
Jari Aalto31859422009-01-12 13:36:28 +00001547 init_interactive_script ();
Jari Aalto28ef6c32001-04-06 19:14:31 +00001548
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001549 free (filename);
1550 return (fd);
1551}
1552
1553/* Initialize the input routines for the parser. */
1554static void
1555set_bash_input ()
1556{
1557 /* Make sure the fd from which we are reading input is not in
1558 no-delay mode. */
1559#if defined (BUFFERED_INPUT)
1560 if (interactive == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +00001561 sh_unset_nodelay_mode (default_buffered_input);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001562 else
1563#endif /* !BUFFERED_INPUT */
Jari Aalto28ef6c32001-04-06 19:14:31 +00001564 sh_unset_nodelay_mode (fileno (stdin));
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001565
1566 /* with_input_from_stdin really means `with_input_from_readline' */
1567 if (interactive && no_line_editing == 0)
1568 with_input_from_stdin ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001569#if defined (BUFFERED_INPUT)
Jari Aalto95732b42005-12-07 14:08:12 +00001570 else if (interactive == 0)
1571 with_input_from_buffered_stream (default_buffered_input, dollar_vars[0]);
1572#endif /* BUFFERED_INPUT */
1573 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001574 with_input_from_stream (default_input, dollar_vars[0]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001575}
1576
Jari Aaltocce855b1998-04-17 19:52:44 +00001577/* Close the current shell script input source and forget about it. This is
1578 extern so execute_cmd.c:initialize_subshell() can call it. If CHECK_ZERO
1579 is non-zero, we close default_buffered_input even if it's the standard
1580 input (fd 0). */
1581void
1582unset_bash_input (check_zero)
1583 int check_zero;
1584{
1585#if defined (BUFFERED_INPUT)
1586 if ((check_zero && default_buffered_input >= 0) ||
1587 (check_zero == 0 && default_buffered_input > 0))
1588 {
1589 close_buffered_fd (default_buffered_input);
1590 default_buffered_input = bash_input.location.buffered_fd = -1;
Jari Aalto31859422009-01-12 13:36:28 +00001591 bash_input.type = st_none; /* XXX */
Jari Aaltocce855b1998-04-17 19:52:44 +00001592 }
1593#else /* !BUFFERED_INPUT */
1594 if (default_input)
1595 {
1596 fclose (default_input);
1597 default_input = (FILE *)NULL;
1598 }
1599#endif /* !BUFFERED_INPUT */
1600}
1601
1602
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001603#if !defined (PROGRAM)
1604# define PROGRAM "bash"
Jari Aalto726f6381996-08-26 18:22:31 +00001605#endif
1606
Jari Aalto726f6381996-08-26 18:22:31 +00001607static void
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001608set_shell_name (argv0)
1609 char *argv0;
Jari Aalto726f6381996-08-26 18:22:31 +00001610{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001611 /* Here's a hack. If the name of this shell is "sh", then don't do
1612 any startup files; just try to be more like /bin/sh. */
Jari Aalto95732b42005-12-07 14:08:12 +00001613 shell_name = argv0 ? base_pathname (argv0) : PROGRAM;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001614
Jari Aalto06285672006-10-10 14:15:34 +00001615 if (argv0 && *argv0 == '-')
Jari Aalto7117c2d2002-07-17 14:10:11 +00001616 {
Jari Aalto06285672006-10-10 14:15:34 +00001617 if (*shell_name == '-')
1618 shell_name++;
Chet Rameyac50fba2014-02-26 09:36:43 -05001619 login_shell = 1;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001620 }
1621
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001622 if (shell_name[0] == 's' && shell_name[1] == 'h' && shell_name[2] == '\0')
1623 act_like_sh++;
1624 if (shell_name[0] == 's' && shell_name[1] == 'u' && shell_name[2] == '\0')
1625 su_shell++;
1626
Jari Aalto95732b42005-12-07 14:08:12 +00001627 shell_name = argv0 ? argv0 : PROGRAM;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001628 FREE (dollar_vars[0]);
1629 dollar_vars[0] = savestring (shell_name);
1630
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001631 /* A program may start an interactive shell with
1632 "execl ("/bin/bash", "-", NULL)".
1633 If so, default the name of this shell to our name. */
1634 if (!shell_name || !*shell_name || (shell_name[0] == '-' && !shell_name[1]))
1635 shell_name = PROGRAM;
1636}
1637
1638static void
1639init_interactive ()
1640{
Jari Aalto31859422009-01-12 13:36:28 +00001641 expand_aliases = interactive_shell = startup_state = 1;
1642 interactive = 1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001643}
1644
1645static void
1646init_noninteractive ()
1647{
1648#if defined (HISTORY)
1649 bash_history_reinit (0);
1650#endif /* HISTORY */
1651 interactive_shell = startup_state = interactive = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001652 expand_aliases = posixly_correct; /* XXX - was 0 not posixly_correct */
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001653 no_line_editing = 1;
1654#if defined (JOB_CONTROL)
Chet Rameyac50fba2014-02-26 09:36:43 -05001655 /* Even if the shell is not interactive, enable job control if the -i or
1656 -m option is supplied at startup. */
1657 set_job_control (forced_interactive||jobs_m_flag);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001658#endif /* JOB_CONTROL */
Jari Aalto726f6381996-08-26 18:22:31 +00001659}
1660
Jari Aalto31859422009-01-12 13:36:28 +00001661static void
1662init_interactive_script ()
1663{
1664 init_noninteractive ();
1665 expand_aliases = interactive_shell = startup_state = 1;
1666}
1667
Jari Aaltod166f041997-06-05 14:59:13 +00001668void
1669get_current_user_info ()
Jari Aalto726f6381996-08-26 18:22:31 +00001670{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001671 struct passwd *entry;
Jari Aalto726f6381996-08-26 18:22:31 +00001672
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001673 /* Don't fetch this more than once. */
1674 if (current_user.user_name == 0)
1675 {
Chet Rameyac50fba2014-02-26 09:36:43 -05001676#if defined (__TANDEM)
1677 entry = getpwnam (getlogin ());
1678#else
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001679 entry = getpwuid (current_user.uid);
Chet Rameyac50fba2014-02-26 09:36:43 -05001680#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001681 if (entry)
1682 {
1683 current_user.user_name = savestring (entry->pw_name);
1684 current_user.shell = (entry->pw_shell && entry->pw_shell[0])
1685 ? savestring (entry->pw_shell)
1686 : savestring ("/bin/sh");
1687 current_user.home_dir = savestring (entry->pw_dir);
1688 }
1689 else
1690 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001691 current_user.user_name = _("I have no name!");
1692 current_user.user_name = savestring (current_user.user_name);
Jari Aalto726f6381996-08-26 18:22:31 +00001693 current_user.shell = savestring ("/bin/sh");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001694 current_user.home_dir = savestring ("/");
1695 }
1696 endpwent ();
1697 }
Jari Aaltod166f041997-06-05 14:59:13 +00001698}
1699
1700/* Do whatever is necessary to initialize the shell.
1701 Put new initializations in here. */
1702static void
1703shell_initialize ()
1704{
1705 char hostname[256];
1706
1707 /* Line buffer output for stderr and stdout. */
Jari Aaltocce855b1998-04-17 19:52:44 +00001708 if (shell_initialized == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +00001709 {
Jari Aaltobb706242000-03-17 21:46:59 +00001710 sh_setlinebuf (stderr);
1711 sh_setlinebuf (stdout);
Jari Aaltocce855b1998-04-17 19:52:44 +00001712 }
Jari Aaltod166f041997-06-05 14:59:13 +00001713
1714 /* Sort the array of shell builtins so that the binary search in
1715 find_shell_builtin () works correctly. */
1716 initialize_shell_builtins ();
1717
1718 /* Initialize the trap signal handlers before installing our own
1719 signal handlers. traps.c:restore_original_signals () is responsible
1720 for restoring the original default signal handlers. That function
1721 is called when we make a new child. */
1722 initialize_traps ();
Jari Aalto7117c2d2002-07-17 14:10:11 +00001723 initialize_signals (0);
Jari Aaltod166f041997-06-05 14:59:13 +00001724
1725 /* It's highly unlikely that this will change. */
1726 if (current_host_name == 0)
1727 {
1728 /* Initialize current_host_name. */
1729 if (gethostname (hostname, 255) < 0)
1730 current_host_name = "??host??";
1731 else
1732 current_host_name = savestring (hostname);
1733 }
1734
1735 /* Initialize the stuff in current_user that comes from the password
1736 file. We don't need to do this right away if the shell is not
1737 interactive. */
1738 if (interactive_shell)
1739 get_current_user_info ();
Jari Aalto726f6381996-08-26 18:22:31 +00001740
1741 /* Initialize our interface to the tilde expander. */
1742 tilde_initialize ();
1743
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001744 /* Initialize internal and environment variables. Don't import shell
1745 functions from the environment if we are running in privileged or
1746 restricted mode or if the shell is running setuid. */
1747#if defined (RESTRICTED_SHELL)
1748 initialize_shell_variables (shell_environment, privileged_mode||restricted||running_setuid);
1749#else
1750 initialize_shell_variables (shell_environment, privileged_mode||running_setuid);
1751#endif
Jari Aalto726f6381996-08-26 18:22:31 +00001752
Jari Aalto726f6381996-08-26 18:22:31 +00001753 /* Initialize the data structures for storing and running jobs. */
Chet Rameyac50fba2014-02-26 09:36:43 -05001754 initialize_job_control (jobs_m_flag);
Jari Aalto726f6381996-08-26 18:22:31 +00001755
1756 /* Initialize input streams to null. */
1757 initialize_bash_input ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001758
Jari Aalto7117c2d2002-07-17 14:10:11 +00001759 initialize_flags ();
1760
Jari Aaltocce855b1998-04-17 19:52:44 +00001761 /* Initialize the shell options. Don't import the shell options
Chet Ramey495aee42011-11-22 19:11:26 -05001762 from the environment variables $SHELLOPTS or $BASHOPTS if we are
1763 running in privileged or restricted mode or if the shell is running
1764 setuid. */
Jari Aaltocce855b1998-04-17 19:52:44 +00001765#if defined (RESTRICTED_SHELL)
1766 initialize_shell_options (privileged_mode||restricted||running_setuid);
Chet Ramey00018032011-11-21 20:51:19 -05001767 initialize_bashopts (privileged_mode||restricted||running_setuid);
Jari Aaltocce855b1998-04-17 19:52:44 +00001768#else
1769 initialize_shell_options (privileged_mode||running_setuid);
Chet Ramey00018032011-11-21 20:51:19 -05001770 initialize_bashopts (privileged_mode||running_setuid);
Jari Aaltocce855b1998-04-17 19:52:44 +00001771#endif
Jari Aalto726f6381996-08-26 18:22:31 +00001772}
1773
1774/* Function called by main () when it appears that the shell has already
1775 had some initialization performed. This is supposed to reset the world
1776 back to a pristine state, as if we had been exec'ed. */
1777static void
1778shell_reinitialize ()
1779{
1780 /* The default shell prompts. */
1781 primary_prompt = PPROMPT;
1782 secondary_prompt = SPROMPT;
1783
1784 /* Things that get 1. */
1785 current_command_number = 1;
1786
1787 /* We have decided that the ~/.bashrc file should not be executed
1788 for the invocation of each shell script. If the variable $ENV
1789 (or $BASH_ENV) is set, its value is used as the name of a file
1790 to source. */
1791 no_rc = no_profile = 1;
1792
1793 /* Things that get 0. */
1794 login_shell = make_login_shell = interactive = executing = 0;
1795 debugging = do_version = line_number = last_command_exit_value = 0;
1796 forced_interactive = interactive_shell = subshell_environment = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001797 expand_aliases = 0;
Jari Aalto726f6381996-08-26 18:22:31 +00001798
Chet Rameyac50fba2014-02-26 09:36:43 -05001799 /* XXX - should we set jobs_m_flag to 0 here? */
1800
Jari Aalto726f6381996-08-26 18:22:31 +00001801#if defined (HISTORY)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001802 bash_history_reinit (0);
Jari Aalto726f6381996-08-26 18:22:31 +00001803#endif /* HISTORY */
1804
1805#if defined (RESTRICTED_SHELL)
1806 restricted = 0;
1807#endif /* RESTRICTED_SHELL */
1808
1809 /* Ensure that the default startup file is used. (Except that we don't
1810 execute this file for reinitialized shells). */
1811 bashrc_file = "~/.bashrc";
1812
1813 /* Delete all variables and functions. They will be reinitialized when
1814 the environment is parsed. */
Jari Aalto7117c2d2002-07-17 14:10:11 +00001815 delete_all_contexts (shell_variables);
Jari Aalto726f6381996-08-26 18:22:31 +00001816 delete_all_variables (shell_functions);
1817
Jari Aalto31859422009-01-12 13:36:28 +00001818 reinit_special_variables ();
1819
1820#if defined (READLINE)
1821 bashline_reinitialize ();
1822#endif
1823
Jari Aalto28ef6c32001-04-06 19:14:31 +00001824 shell_reinitialized = 1;
Jari Aalto726f6381996-08-26 18:22:31 +00001825}
1826
1827static void
Jari Aaltod166f041997-06-05 14:59:13 +00001828show_shell_usage (fp, extra)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001829 FILE *fp;
Jari Aaltod166f041997-06-05 14:59:13 +00001830 int extra;
Jari Aalto726f6381996-08-26 18:22:31 +00001831{
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001832 int i;
1833 char *set_opts, *s, *t;
Jari Aalto726f6381996-08-26 18:22:31 +00001834
Jari Aaltod166f041997-06-05 14:59:13 +00001835 if (extra)
Jari Aalto31859422009-01-12 13:36:28 +00001836 fprintf (fp, _("GNU bash, version %s-(%s)\n"), shell_version_string (), MACHTYPE);
Jari Aaltob80f6442004-07-27 13:29:18 +00001837 fprintf (fp, _("Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n"),
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001838 shell_name, shell_name);
Jari Aaltob80f6442004-07-27 13:29:18 +00001839 fputs (_("GNU long options:\n"), fp);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001840 for (i = 0; long_args[i].name; i++)
1841 fprintf (fp, "\t--%s\n", long_args[i].name);
Jari Aalto726f6381996-08-26 18:22:31 +00001842
Jari Aaltob80f6442004-07-27 13:29:18 +00001843 fputs (_("Shell options:\n"), fp);
Chet Rameyac50fba2014-02-26 09:36:43 -05001844 fputs (_("\t-ilrsD or -c command or -O shopt_option\t\t(invocation only)\n"), fp);
Jari Aalto726f6381996-08-26 18:22:31 +00001845
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001846 for (i = 0, set_opts = 0; shell_builtins[i].name; i++)
1847 if (STREQ (shell_builtins[i].name, "set"))
1848 set_opts = savestring (shell_builtins[i].short_doc);
1849 if (set_opts)
Jari Aalto726f6381996-08-26 18:22:31 +00001850 {
Chet Ramey00018032011-11-21 20:51:19 -05001851 s = strchr (set_opts, '[');
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001852 if (s == 0)
1853 s = set_opts;
1854 while (*++s == '-')
1855 ;
Chet Ramey00018032011-11-21 20:51:19 -05001856 t = strchr (s, ']');
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001857 if (t)
1858 *t = '\0';
Jari Aaltob80f6442004-07-27 13:29:18 +00001859 fprintf (fp, _("\t-%s or -o option\n"), s);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001860 free (set_opts);
Jari Aalto726f6381996-08-26 18:22:31 +00001861 }
1862
Jari Aaltod166f041997-06-05 14:59:13 +00001863 if (extra)
1864 {
Jari Aaltob80f6442004-07-27 13:29:18 +00001865 fprintf (fp, _("Type `%s -c \"help set\"' for more information about shell options.\n"), shell_name);
1866 fprintf (fp, _("Type `%s -c help' for more information about shell builtin commands.\n"), shell_name);
1867 fprintf (fp, _("Use the `bashbug' command to report bugs.\n"));
Jari Aaltod166f041997-06-05 14:59:13 +00001868 }
Jari Aalto726f6381996-08-26 18:22:31 +00001869}
1870
Jari Aaltof73dda02001-11-13 17:56:06 +00001871static void
1872add_shopt_to_alist (opt, on_or_off)
1873 char *opt;
1874 int on_or_off;
1875{
1876 if (shopt_ind >= shopt_len)
1877 {
1878 shopt_len += 8;
1879 shopt_alist = (STRING_INT_ALIST *)xrealloc (shopt_alist, shopt_len * sizeof (shopt_alist[0]));
1880 }
1881 shopt_alist[shopt_ind].word = opt;
1882 shopt_alist[shopt_ind].token = on_or_off;
1883 shopt_ind++;
1884}
1885
1886static void
1887run_shopt_alist ()
1888{
1889 register int i;
1890
1891 for (i = 0; i < shopt_ind; i++)
1892 if (shopt_setopt (shopt_alist[i].word, (shopt_alist[i].token == '-')) != EXECUTION_SUCCESS)
Jari Aaltob80f6442004-07-27 13:29:18 +00001893 exit (EX_BADUSAGE);
Jari Aaltof73dda02001-11-13 17:56:06 +00001894 free (shopt_alist);
1895 shopt_alist = 0;
1896 shopt_ind = shopt_len = 0;
1897}