1 /* shell.c -- GNU's idea of the POSIX shell specification. */
3 /* Copyright (C) 1987-2021 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 Sunday, January 10th, 1988.
24 Initial author: Brian Fox
26 #define INSTALL_DEBUG_MODE
30 #include "bashtypes.h"
31 #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
32 # include <sys/file.h>
34 #include "posixstat.h"
35 #include "posixtime.h"
41 #if defined (HAVE_PWD_H)
45 #if defined (HAVE_UNISTD_H)
51 #define NEED_SH_SETLINEBUF_DECL /* used in externs.h */
57 #include "mailcheck.h"
59 #include "builtins/common.h"
61 #if defined (JOB_CONTROL)
64 extern int running_in_background
;
65 extern int initialize_job_control
PARAMS((int));
66 extern int get_tty_state
PARAMS((void));
67 #endif /* JOB_CONTROL */
70 #include "execute_cmd.h"
73 #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
74 # include <malloc/shmalloc.h>
75 #elif defined (MALLOC_DEBUG) && defined (USING_BASH_MALLOC)
76 # include <malloc/shmalloc.h>
80 # include "bashhist.h"
81 # include <readline/history.h>
84 #if defined (READLINE)
85 # include <readline/readline.h>
86 # include "bashline.h"
89 #include <tilde/tilde.h>
90 #include <glob/strmatch.h>
92 #if defined (__OPENNT)
93 # include <opennt/opennt.h>
96 #if !defined (HAVE_GETPW_DECLS)
97 extern struct passwd
*getpwuid ();
98 #endif /* !HAVE_GETPW_DECLS */
104 #if defined (NO_MAIN_ENV_ARG)
105 extern char **environ
; /* used if no third argument to main() */
108 extern int gnu_error_format
;
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. */
112 int shell_initialized
= 0;
113 int bash_argv_initialized
= 0;
115 COMMAND
*global_command
= (COMMAND
*)NULL
;
117 /* Information about the current user. */
118 struct user_info current_user
=
120 (uid_t
)-1, (uid_t
)-1, (gid_t
)-1, (gid_t
)-1,
121 (char *)NULL
, (char *)NULL
, (char *)NULL
124 /* The current host's name. */
125 char *current_host_name
= (char *)NULL
;
127 /* Non-zero means that this shell is a login shell.
130 1 = login shell from getty (or equivalent fake out)
131 -1 = login shell from "--login" (or -l) flag.
132 -2 = both from getty, and from flag.
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. */
141 /* Non-zero means that the shell was started as an interactive shell. */
142 int interactive_shell
= 0;
144 /* Non-zero means to send a SIGHUP to all jobs when an interactive login
148 /* Non-zero means to list status of running and stopped jobs at shell exit */
149 int check_jobs_at_exit
= 0;
151 /* Non-zero means to change to a directory name supplied as a command name */
154 /* Tells what state the shell was in when it started:
155 0 = non-interactive shell script
158 3 = wordexp evaluation
159 This is a superset of the information provided by interactive_shell.
161 int startup_state
= 0;
162 int reading_shell_script
= 0;
164 /* Special debugging helper. */
165 int debugging_login_shell
= 0;
167 /* The environment that the shell passes to other commands. */
168 char **shell_environment
;
170 /* Non-zero when we are executing a top-level command. */
173 /* The number of commands executed so far. */
174 int current_command_number
= 1;
176 /* Non-zero is the recursion depth for commands. */
177 int indirection_level
= 0;
179 /* The name of this shell, as taken from argv[0]. */
180 char *shell_name
= (char *)NULL
;
182 /* time in seconds when the shell was started */
183 time_t shell_start_time
;
184 struct timeval shellstart
;
186 /* Are we running in an emacs shell window? */
187 int running_under_emacs
;
189 /* Do we have /dev/fd? */
191 int have_devfd
= HAVE_DEV_FD
;
196 /* The name of the .(shell)rc file. */
197 static char *bashrc_file
= DEFAULT_BASHRC
;
199 /* Non-zero means to act more like the Bourne shell on startup. */
200 static int act_like_sh
;
202 /* Non-zero if this shell is being run by `su'. */
205 /* Non-zero if we have already expanded and sourced $ENV. */
206 static int sourced_env
;
208 /* Is this shell running setuid? */
209 static int running_setuid
;
211 /* Values for the long-winded argument names. */
212 static int debugging
; /* Do debugging things. */
213 static int no_rc
; /* Don't execute ~/.bashrc */
214 static int no_profile
; /* Don't execute .profile */
215 static int do_version
; /* Display interesting version info. */
216 static int make_login_shell
; /* Make this shell be a `-bash' shell. */
217 static int want_initial_help
; /* --help option */
219 int debugging_mode
= 0; /* In debugging mode with --debugger */
220 #if defined (READLINE)
221 int no_line_editing
= 0; /* non-zero -> don't do fancy line editing. */
223 int no_line_editing
= 1; /* can't have line editing without readline */
225 #if defined (TRANSLATABLE_STRINGS)
226 int dump_translatable_strings
; /* Dump strings in $"...", don't execute. */
227 int dump_po_strings
; /* Dump strings in $"..." in po format */
229 int wordexp_only
= 0; /* Do word expansion only */
230 int protected_mode
= 0; /* No command substitution with --wordexp */
232 int pretty_print_mode
= 0; /* pretty-print a shell script */
234 #if defined (STRICT_POSIX)
235 int posixly_correct
= 1; /* Non-zero means posix.2 superset. */
237 int posixly_correct
= 0; /* Non-zero means posix.2 superset. */
240 /* Some long-winded argument names. These are obviously new. */
243 static const struct {
249 { "debug", Int
, &debugging
, (char **)0x0 },
250 #if defined (DEBUGGER)
251 { "debugger", Int
, &debugging_mode
, (char **)0x0 },
253 #if defined (TRANSLATABLE_STRINGS)
254 { "dump-po-strings", Int
, &dump_po_strings
, (char **)0x0 },
255 { "dump-strings", Int
, &dump_translatable_strings
, (char **)0x0 },
257 { "help", Int
, &want_initial_help
, (char **)0x0 },
258 { "init-file", Charp
, (int *)0x0, &bashrc_file
},
259 { "login", Int
, &make_login_shell
, (char **)0x0 },
260 { "noediting", Int
, &no_line_editing
, (char **)0x0 },
261 { "noprofile", Int
, &no_profile
, (char **)0x0 },
262 { "norc", Int
, &no_rc
, (char **)0x0 },
263 { "posix", Int
, &posixly_correct
, (char **)0x0 },
264 { "pretty-print", Int
, &pretty_print_mode
, (char **)0x0 },
265 #if defined (WORDEXP_OPTION)
266 { "protected", Int
, &protected_mode
, (char **)0x0 },
268 { "rcfile", Charp
, (int *)0x0, &bashrc_file
},
269 #if defined (RESTRICTED_SHELL)
270 { "restricted", Int
, &restricted
, (char **)0x0 },
272 { "verbose", Int
, &verbose_flag
, (char **)0x0 },
273 { "version", Int
, &do_version
, (char **)0x0 },
274 #if defined (WORDEXP_OPTION)
275 { "wordexp", Int
, &wordexp_only
, (char **)0x0 },
277 { (char *)0x0, Int
, (int *)0x0, (char **)0x0 }
280 /* These are extern so execute_simple_command can set them, and then
281 longjmp back to main to execute a shell script, instead of calling
282 main () again and resulting in indefinite, possibly fatal, stack
284 procenv_t subshell_top_level
;
286 char **subshell_argv
;
287 char **subshell_envp
;
291 #if defined (BUFFERED_INPUT)
292 /* The file descriptor from which the shell is reading input. */
293 int default_buffered_input
= -1;
296 /* The following two variables are not static so they can show up in $-. */
297 int read_from_stdin
; /* -s flag supplied */
298 int want_pending_command
; /* -c flag supplied */
300 /* This variable is not static so it can be bound to $BASH_EXECUTION_STRING */
301 char *command_execution_string
; /* argument to -c option */
302 char *shell_script_filename
; /* shell script */
304 int malloc_trace_at_exit
= 0;
306 static int shell_reinitialized
= 0;
308 static FILE *default_input
;
310 static STRING_INT_ALIST
*shopt_alist
;
311 static int shopt_ind
= 0, shopt_len
= 0;
313 static int parse_long_options
PARAMS((char **, int, int));
314 static int parse_shell_options
PARAMS((char **, int, int));
315 static int bind_args
PARAMS((char **, int, int, int));
317 static void start_debugger
PARAMS((void));
319 static void add_shopt_to_alist
PARAMS((char *, int));
320 static void run_shopt_alist
PARAMS((void));
322 static void execute_env_file
PARAMS((char *));
323 static void run_startup_files
PARAMS((void));
324 static int open_shell_script
PARAMS((char *));
325 static void set_bash_input
PARAMS((void));
326 static int run_one_command
PARAMS((char *));
327 #if defined (WORDEXP_OPTION)
328 static int run_wordexp
PARAMS((char *));
331 static int uidget
PARAMS((void));
333 static void set_option_defaults
PARAMS((void));
334 static void reset_option_defaults
PARAMS((void));
336 static void init_interactive
PARAMS((void));
337 static void init_noninteractive
PARAMS((void));
338 static void init_interactive_script
PARAMS((void));
340 static void set_shell_name
PARAMS((char *));
341 static void shell_initialize
PARAMS((void));
342 static void shell_reinitialize
PARAMS((void));
344 static void show_shell_usage
PARAMS((FILE *, int));
348 _cygwin32_check_tmp ()
352 if (stat ("/tmp", &sb
) < 0)
353 internal_warning (_("could not find /tmp, please create!"));
356 if (S_ISDIR (sb
.st_mode
) == 0)
357 internal_warning (_("/tmp must be a valid directory name"));
360 #endif /* __CYGWIN__ */
362 #if defined (NO_MAIN_ENV_ARG)
363 /* systems without third argument to main() */
368 #else /* !NO_MAIN_ENV_ARG */
370 main (argc
, argv
, env
)
373 #endif /* !NO_MAIN_ENV_ARG */
376 int code
, old_errexit_flag
;
377 #if defined (RESTRICTED_SHELL)
380 volatile int locally_skip_execution
;
381 volatile int arg_index
, top_level_arg_index
;
386 #endif /* __OPENNT */
392 USE_VAR(old_errexit_flag
);
393 #if defined (RESTRICTED_SHELL)
397 /* Catch early SIGINTs. */
398 code
= setjmp_nosigs (top_level
);
404 #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
405 malloc_set_register (1); /* XXX - change to 1 for malloc debugging */
411 _cygwin32_check_tmp ();
412 #endif /* __CYGWIN__ */
414 /* Wait forever if we are debugging a login shell. */
415 while (debugging_login_shell
) sleep (3);
417 set_default_locale ();
419 running_setuid
= uidget ();
421 if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC"))
424 #if defined (USE_GNU_MALLOC_LIBRARY)
425 mcheck (programming_error
, (void (*) ())0);
426 #endif /* USE_GNU_MALLOC_LIBRARY */
428 if (setjmp_sigs (subshell_top_level
))
430 argc
= subshell_argc
;
431 argv
= subshell_argv
;
436 shell_reinitialized
= 0;
438 /* Initialize `local' variables for all `invocations' of main (). */
440 if (arg_index
> argc
)
442 command_execution_string
= shell_script_filename
= (char *)NULL
;
443 want_pending_command
= locally_skip_execution
= read_from_stdin
= 0;
444 default_input
= stdin
;
445 #if defined (BUFFERED_INPUT)
446 default_buffered_input
= -1;
449 /* Fix for the `infinite process creation' bug when running shell scripts
450 from startup files on System V. */
451 login_shell
= make_login_shell
= 0;
453 /* If this shell has already been run, then reinitialize it to a
455 if (shell_initialized
|| shell_name
)
457 /* Make sure that we do not infinitely recurse as a login shell. */
458 if (*shell_name
== '-')
461 shell_reinitialize ();
462 if (setjmp_nosigs (top_level
))
466 shell_environment
= env
;
467 set_shell_name (argv
[0]);
469 gettimeofday (&shellstart
, 0);
470 shell_start_time
= shellstart
.tv_sec
;
472 /* Parse argument flags from the input line. */
474 /* Find full word arguments first. */
475 arg_index
= parse_long_options (argv
, arg_index
, argc
);
477 if (want_initial_help
)
479 show_shell_usage (stdout
, 1);
480 exit (EXECUTION_SUCCESS
);
485 show_shell_version (1);
486 exit (EXECUTION_SUCCESS
);
489 echo_input_at_read
= verbose_flag
; /* --verbose given */
491 /* All done with full word options; do standard shell option parsing.*/
492 this_command_name
= shell_name
; /* for error reporting */
493 arg_index
= parse_shell_options (argv
, arg_index
, argc
);
495 /* If user supplied the "--login" (or -l) flag, then set and invert
497 if (make_login_shell
)
500 login_shell
= -login_shell
;
503 set_login_shell ("login_shell", login_shell
!= 0);
505 #if defined (TRANSLATABLE_STRINGS)
507 dump_translatable_strings
= 1;
509 if (dump_translatable_strings
)
510 read_but_dont_execute
= 1;
513 if (running_setuid
&& privileged_mode
== 0)
514 disable_priv_mode ();
516 /* Need to get the argument to a -c option processed in the
517 above loop. The next arg is a command to execute, and the
518 following args are $0...$n respectively. */
519 if (want_pending_command
)
521 command_execution_string
= argv
[arg_index
];
522 if (command_execution_string
== 0)
524 report_error (_("%s: option requires an argument"), "-c");
529 this_command_name
= (char *)NULL
;
531 /* First, let the outside world know about our interactive status.
532 A shell is interactive if the `-i' flag was given, or if all of
533 the following conditions are met:
535 no arguments remaining or the -s flag given
536 standard input is a terminal
537 standard error is a terminal
538 Refer to Posix.2, the description of the `sh' utility. */
540 if (forced_interactive
|| /* -i flag */
541 (!command_execution_string
&& /* No -c command and ... */
542 wordexp_only
== 0 && /* No --wordexp and ... */
543 ((arg_index
== argc
) || /* no remaining args or... */
544 read_from_stdin
) && /* -s flag with args, and */
545 isatty (fileno (stdin
)) && /* Input is a terminal and */
546 isatty (fileno (stderr
)))) /* error output is a terminal. */
549 init_noninteractive ();
552 * Some systems have the bad habit of starting login shells with lots of open
553 * file descriptors. For instance, most systems that have picked up the
554 * pre-4.0 Sun YP code leave a file descriptor open each time you call one
555 * of the getpw* functions, and it's set to be open across execs. That
556 * means one for login, one for xterm, one for shelltool, etc. There are
557 * also systems that open persistent FDs to other agents or files as part
558 * of process startup; these need to be set to be close-on-exec.
560 if (login_shell
&& interactive_shell
)
562 for (i
= 3; i
< 20; i
++)
563 SET_CLOSE_ON_EXEC (i
);
566 /* If we're in a strict Posix.2 mode, turn on interactive comments,
567 alias expansion in non-interactive shells, and other Posix.2 things. */
570 bind_variable ("POSIXLY_CORRECT", "y", 0);
571 sv_strict_posix ("POSIXLY_CORRECT");
574 /* Now we run the shopt_alist and process the options. */
578 /* From here on in, the shell must be a normal functioning shell.
579 Variables from the environment are expected to be set, etc. */
583 set_default_locale_vars ();
586 * M-x term -> TERM=eterm-color INSIDE_EMACS='251,term:0.96' (eterm)
587 * M-x shell -> TERM='dumb' INSIDE_EMACS='25.1,comint' (no line editing)
589 * Older versions of Emacs may set EMACS to 't' or to something like
590 * '22.1 (term:0.96)' instead of (or in addition to) setting INSIDE_EMACS.
591 * They may set TERM to 'eterm' instead of 'eterm-color'. They may have
592 * a now-obsolete command that sets neither EMACS nor INSIDE_EMACS:
593 * M-x terminal -> TERM='emacs-em7955' (line editing)
595 if (interactive_shell
)
597 char *term
, *emacs
, *inside_emacs
;
598 int emacs_term
, in_emacs
;
600 term
= get_string_value ("TERM");
601 emacs
= get_string_value ("EMACS");
602 inside_emacs
= get_string_value ("INSIDE_EMACS");
606 emacs_term
= strstr (inside_emacs
, ",term:") != 0;
611 /* Infer whether we are in an older Emacs. */
612 emacs_term
= strstr (emacs
, " (term:") != 0;
613 in_emacs
= emacs_term
|| STREQ (emacs
, "t");
616 in_emacs
= emacs_term
= 0;
618 /* Not sure any emacs terminal emulator sets TERM=emacs any more */
619 no_line_editing
|= STREQ (term
, "emacs");
620 no_line_editing
|= in_emacs
&& STREQ (term
, "dumb");
622 /* running_under_emacs == 2 for `eterm' */
623 running_under_emacs
= in_emacs
|| STREQN (term
, "emacs", 5);
624 running_under_emacs
+= emacs_term
&& STREQN (term
, "eterm", 5);
626 if (running_under_emacs
)
627 gnu_error_format
= 1;
630 top_level_arg_index
= arg_index
;
631 old_errexit_flag
= exit_immediately_on_error
;
633 /* Give this shell a place to longjmp to before executing the
634 startup files. This allows users to press C-c to abort the
636 code
= setjmp_sigs (top_level
);
639 if (code
== EXITPROG
|| code
== ERREXIT
|| code
== EXITBLTIN
)
640 exit_shell (last_command_exit_value
);
643 #if defined (JOB_CONTROL)
644 /* Reset job control, since run_startup_files turned it off. */
645 set_job_control (interactive_shell
);
647 /* Reset value of `set -e', since it's turned off before running
648 the startup files. */
649 exit_immediately_on_error
+= old_errexit_flag
;
650 locally_skip_execution
++;
654 arg_index
= top_level_arg_index
;
656 /* Execute the start-up scripts. */
658 if (interactive_shell
== 0)
660 unbind_variable ("PS1");
661 unbind_variable ("PS2");
664 /* This has already been done by init_noninteractive */
665 expand_aliases
= posixly_correct
;
670 change_flag ('i', FLAG_ON
);
674 #if defined (RESTRICTED_SHELL)
675 /* Set restricted_shell based on whether the basename of $0 indicates that
676 the shell should be restricted or if the `-r' option was supplied at
678 restricted_shell
= shell_is_restricted (shell_name
);
680 /* If the `-r' option is supplied at invocation, make sure that the shell
681 is not in restricted mode when running the startup files. */
682 saverst
= restricted
;
686 /* Set positional parameters before running startup files. top_level_arg_index
687 holds the index of the current argument before setting the positional
688 parameters, so any changes performed in the startup files won't affect
689 later option processing. */
692 else if (command_execution_string
)
693 arg_index
= bind_args (argv
, arg_index
, argc
, 0); /* $0 ... $n */
694 else if (arg_index
!= argc
&& read_from_stdin
== 0)
696 shell_script_filename
= argv
[arg_index
++];
697 arg_index
= bind_args (argv
, arg_index
, argc
, 1); /* $1 ... $n */
700 arg_index
= bind_args (argv
, arg_index
, argc
, 1); /* $1 ... $n */
702 /* The startup files are run with `set -e' temporarily disabled. */
703 if (locally_skip_execution
== 0 && running_setuid
== 0)
707 old_errexit_flag
= exit_immediately_on_error
;
708 exit_immediately_on_error
= 0;
710 /* Temporarily set $0 while running startup files, then restore it so
711 we get better error messages when trying to open script files. */
712 if (shell_script_filename
)
715 dollar_vars
[0] = exec_argv0
? savestring (exec_argv0
) : savestring (shell_script_filename
);
717 run_startup_files ();
718 if (shell_script_filename
)
720 free (dollar_vars
[0]);
723 exit_immediately_on_error
+= old_errexit_flag
;
726 /* If we are invoked as `sh', turn on Posix mode. */
729 bind_variable ("POSIXLY_CORRECT", "y", 0);
730 sv_strict_posix ("POSIXLY_CORRECT");
733 #if defined (RESTRICTED_SHELL)
734 /* Turn on the restrictions after executing the startup files. This
735 means that `bash -r' or `set -r' invoked from a startup file will
736 turn on the restrictions after the startup files are executed. */
737 restricted
= saverst
|| restricted
;
738 if (shell_reinitialized
== 0)
739 maybe_make_restricted (shell_name
);
740 #endif /* RESTRICTED_SHELL */
742 #if defined (WORDEXP_OPTION)
746 last_command_exit_value
= run_wordexp (argv
[top_level_arg_index
]);
747 exit_shell (last_command_exit_value
);
751 cmd_init (); /* initialize the command object caches */
754 if (command_execution_string
)
761 #if defined (ONESHOT)
763 run_one_command (command_execution_string
);
764 exit_shell (last_command_exit_value
);
766 with_input_from_string (command_execution_string
, "-c");
767 goto read_and_execute
;
768 #endif /* !ONESHOT */
771 /* Get possible input filename and set up default_buffered_input or
772 default_input as appropriate. */
773 if (shell_script_filename
)
774 open_shell_script (shell_script_filename
);
775 else if (interactive
== 0)
777 /* In this mode, bash is reading a script from stdin, which is a
778 pipe or redirected file. */
779 #if defined (BUFFERED_INPUT)
780 default_buffered_input
= fileno (stdin
); /* == 0 */
782 setbuf (default_input
, (char *)NULL
);
783 #endif /* !BUFFERED_INPUT */
786 else if (top_level_arg_index
== argc
) /* arg index before startup files */
787 /* "If there are no operands and the -c option is not specified, the -s
788 option shall be assumed." */
793 if (debugging_mode
&& locally_skip_execution
== 0 && running_setuid
== 0 && (reading_shell_script
|| interactive_shell
== 0))
796 /* Do the things that should be done only for interactive shells. */
797 if (interactive_shell
)
799 /* Set up for checking for presence of mail. */
803 #if defined (HISTORY)
804 /* Initialize the interactive history stuff. */
805 bash_initialize_history ();
806 /* Don't load the history from the history file if we've already
807 saved some lines in this session (e.g., by putting `history -s xx'
808 into one of the startup files). */
809 if (shell_initialized
== 0 && history_lines_this_session
== 0)
813 /* Initialize terminal state for interactive shells after the
814 .bash_profile and .bashrc are interpreted. */
818 #if !defined (ONESHOT)
820 #endif /* !ONESHOT */
822 shell_initialized
= 1;
824 if (pretty_print_mode
&& interactive_shell
)
826 internal_warning (_("pretty-printing mode ignored in interactive shells"));
827 pretty_print_mode
= 0;
829 if (pretty_print_mode
)
830 exit_shell (pretty_print_loop ());
832 /* Read commands until exit condition. */
834 exit_shell (last_command_exit_value
);
838 parse_long_options (argv
, arg_start
, arg_end
)
840 int arg_start
, arg_end
;
842 int arg_index
, longarg
, i
;
845 arg_index
= arg_start
;
846 while ((arg_index
!= arg_end
) && (arg_string
= argv
[arg_index
]) &&
847 (*arg_string
== '-'))
851 /* Make --login equivalent to -login. */
852 if (arg_string
[1] == '-' && arg_string
[2])
858 for (i
= 0; long_args
[i
].name
; i
++)
860 if (STREQ (arg_string
+ 1, long_args
[i
].name
))
862 if (long_args
[i
].type
== Int
)
863 *long_args
[i
].int_value
= 1;
864 else if (argv
[++arg_index
] == 0)
866 report_error (_("%s: option requires an argument"), long_args
[i
].name
);
870 *long_args
[i
].char_value
= argv
[arg_index
];
875 if (long_args
[i
].name
== 0)
879 report_error (_("%s: invalid option"), argv
[arg_index
]);
880 show_shell_usage (stderr
, 0);
883 break; /* No such argument. Maybe flag arg. */
893 parse_shell_options (argv
, arg_start
, arg_end
)
895 int arg_start
, arg_end
;
898 int arg_character
, on_or_off
, next_arg
, i
;
899 char *o_option
, *arg_string
;
901 arg_index
= arg_start
;
902 while (arg_index
!= arg_end
&& (arg_string
= argv
[arg_index
]) &&
903 (*arg_string
== '-' || *arg_string
== '+'))
905 /* There are flag arguments, so parse them. */
906 next_arg
= arg_index
+ 1;
908 /* A single `-' signals the end of options. From the 4.3 BSD sh.
909 An option `--' means the same thing; this is the standard
910 getopt(3) meaning. */
911 if (arg_string
[0] == '-' &&
912 (arg_string
[1] == '\0' ||
913 (arg_string
[1] == '-' && arg_string
[2] == '\0')))
917 on_or_off
= arg_string
[0];
918 while (arg_character
= arg_string
[i
++])
920 switch (arg_character
)
923 want_pending_command
= 1;
927 make_login_shell
= 1;
935 o_option
= argv
[next_arg
];
938 set_option_defaults ();
939 list_minus_o_opts (-1, (on_or_off
== '-') ? 0 : 1);
940 reset_option_defaults ();
943 if (set_minus_o_option (on_or_off
, o_option
) != EXECUTION_SUCCESS
)
949 /* Since some of these can be overridden by the normal
950 interactive/non-interactive shell initialization or
951 initializing posix mode, we save the options and process
952 them after initialization. */
953 o_option
= argv
[next_arg
];
956 shopt_listopt (o_option
, (on_or_off
== '-') ? 0 : 1);
959 add_shopt_to_alist (o_option
, on_or_off
);
964 #if defined (TRANSLATABLE_STRINGS)
965 dump_translatable_strings
= 1;
970 if (change_flag (arg_character
, on_or_off
) == FLAG_ERROR
)
972 report_error (_("%c%c: invalid option"), on_or_off
, arg_character
);
973 show_shell_usage (stderr
, 0);
978 /* Can't do just a simple increment anymore -- what about
979 "bash -abouo emacs ignoreeof -hP"? */
980 arg_index
= next_arg
;
986 /* Exit the shell with status S. */
991 fflush (stdout
); /* XXX */
994 /* Clean up the terminal if we are in a state where it's been modified. */
995 #if defined (READLINE)
996 if (RL_ISSTATE (RL_STATE_TERMPREPPED
) && rl_deprep_term_function
)
997 (*rl_deprep_term_function
) ();
999 if (read_tty_modified ())
1000 read_tty_cleanup ();
1002 /* Do trap[0] if defined. Allow it to override the exit status
1004 if (signal_is_trapped (0))
1005 s
= run_exit_trap ();
1007 #if defined (PROCESS_SUBSTITUTION)
1008 unlink_all_fifos ();
1009 #endif /* PROCESS_SUBSTITUTION */
1011 #if defined (HISTORY)
1012 if (remember_on_history
)
1013 maybe_save_shell_history ();
1014 #endif /* HISTORY */
1016 #if defined (COPROCESS_SUPPORT)
1020 #if defined (JOB_CONTROL)
1021 /* If the user has run `shopt -s huponexit', hangup all jobs when we exit
1022 an interactive login shell. ksh does this unconditionally. */
1023 if (interactive_shell
&& login_shell
&& hup_on_exit
)
1026 /* If this shell is interactive, or job control is active, terminate all
1027 stopped jobs and restore the original terminal process group. Don't do
1028 this if we're in a subshell and calling exit_shell after, for example,
1029 a failed word expansion. We want to do this even if the shell is not
1030 interactive because we set the terminal's process group when job control
1031 is enabled regardless of the interactive status. */
1032 if (subshell_environment
== 0)
1034 #endif /* JOB_CONTROL */
1036 /* Always return the exit status of the last command to our parent. */
1040 /* A wrapper for exit that (optionally) can do other things, like malloc
1041 statistics tracing. */
1046 #if defined (MALLOC_DEBUG) && defined (USING_BASH_MALLOC)
1047 if (malloc_trace_at_exit
&& (subshell_environment
& (SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
)) == 0)
1048 trace_malloc_stats (get_name_for_error (), (char *)NULL
);
1049 /* mlocation_write_table (); */
1055 /* Exit a subshell, which includes calling the exit trap. We don't want to
1056 do any more cleanup, since a subshell is created as an exact copy of its
1065 /* Do trap[0] if defined. Allow it to override the exit status
1067 last_command_exit_value
= s
;
1068 if (signal_is_trapped (0))
1069 s
= run_exit_trap ();
1078 set_pipestatus_from_exit (last_command_exit_value
= s
);
1081 /* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey
1082 the Posix.2 startup file rules: $ENV is expanded, and if the file it
1083 names exists, that file is sourced. The Posix.2 rules are in effect
1084 for interactive shells only. (section 4.56.5.3) */
1086 /* Execute ~/.bashrc for most shells. Never execute it if
1087 ACT_LIKE_SH is set, or if NO_RC is set.
1089 If the executable file "/usr/gnu/src/bash/foo" contains:
1096 COMMAND EXECUTE BASHRC
1097 --------------------------------
1101 rsh machine ls YES (for rsh, which calls `bash -c')
1102 rsh machine foo YES (for shell started by rsh) NO (for foo!)
1109 execute_env_file (env_file
)
1114 if (env_file
&& *env_file
)
1116 fn
= expand_string_unsplit_to_string (env_file
, Q_DOUBLE_QUOTES
);
1118 maybe_execute_file (fn
, 1);
1124 run_startup_files ()
1126 #if defined (JOB_CONTROL)
1127 int old_job_control
;
1129 int sourced_login
, run_by_ssh
;
1131 #if 1 /* TAG:bash-5.3 andrew.gregory.8@gmail.com 2/21/2022 */
1132 /* get the rshd/sshd case out of the way first. */
1133 if (interactive_shell
== 0 && no_rc
== 0 && login_shell
== 0 &&
1134 act_like_sh
== 0 && command_execution_string
)
1136 #ifdef SSH_SOURCE_BASHRC
1137 run_by_ssh
= (find_variable ("SSH_CLIENT") != (SHELL_VAR
*)0) ||
1138 (find_variable ("SSH2_CLIENT") != (SHELL_VAR
*)0);
1144 /* If we were run by sshd or we think we were run by rshd, execute
1145 ~/.bashrc if we are a top-level shell. */
1146 #if 1 /* TAG:bash-5.3 */
1147 if ((run_by_ssh
|| isnetconn (fileno (stdin
))) && shell_level
< 2)
1149 if (isnetconn (fileno (stdin
) && shell_level
< 2)
1153 # if defined (__OPENNT)
1154 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
1156 maybe_execute_file (SYS_BASHRC
, 1);
1159 maybe_execute_file (bashrc_file
, 1);
1164 #if defined (JOB_CONTROL)
1165 /* Startup files should be run without job control enabled. */
1166 old_job_control
= interactive_shell
? set_job_control (0) : 0;
1171 /* A shell begun with the --login (or -l) flag that is not in posix mode
1172 runs the login shell startup files, no matter whether or not it is
1173 interactive. If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
1174 startup files if argv[0][0] == '-' as well. */
1175 #if defined (NON_INTERACTIVE_LOGIN_SHELLS)
1176 if (login_shell
&& posixly_correct
== 0)
1178 if (login_shell
< 0 && posixly_correct
== 0)
1181 /* We don't execute .bashrc for login shells. */
1184 /* Execute /etc/profile and one of the personal login shell
1185 initialization files. */
1186 if (no_profile
== 0)
1188 maybe_execute_file (SYS_PROFILE
, 1);
1190 if (act_like_sh
) /* sh */
1191 maybe_execute_file ("~/.profile", 1);
1192 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
1193 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
1194 maybe_execute_file ("~/.profile", 1);
1200 /* A non-interactive shell not named `sh' and not in posix mode reads and
1201 executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd'
1202 and `-su' as the name of the shell, we want to read the startup files.
1203 No other non-interactive shells read any startup files. */
1204 if (interactive_shell
== 0 && !(su_shell
&& login_shell
))
1206 if (posixly_correct
== 0 && act_like_sh
== 0 && privileged_mode
== 0 &&
1208 execute_env_file (get_string_value ("BASH_ENV"));
1212 /* Interactive shell or `-su' shell. */
1213 if (posixly_correct
== 0) /* bash, sh */
1215 if (login_shell
&& sourced_login
++ == 0)
1217 /* We don't execute .bashrc for login shells. */
1220 /* Execute /etc/profile and one of the personal login shell
1221 initialization files. */
1222 if (no_profile
== 0)
1224 maybe_execute_file (SYS_PROFILE
, 1);
1226 if (act_like_sh
) /* sh */
1227 maybe_execute_file ("~/.profile", 1);
1228 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
1229 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
1230 maybe_execute_file ("~/.profile", 1);
1235 if (act_like_sh
== 0 && no_rc
== 0)
1238 # if defined (__OPENNT)
1239 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
1241 maybe_execute_file (SYS_BASHRC
, 1);
1244 maybe_execute_file (bashrc_file
, 1);
1247 else if (act_like_sh
&& privileged_mode
== 0 && sourced_env
++ == 0)
1248 execute_env_file (get_string_value ("ENV"));
1250 else /* bash --posix, sh --posix */
1253 if (interactive_shell
&& privileged_mode
== 0 && sourced_env
++ == 0)
1254 execute_env_file (get_string_value ("ENV"));
1257 #if defined (JOB_CONTROL)
1258 set_job_control (old_job_control
);
1262 #if defined (RESTRICTED_SHELL)
1263 /* Return 1 if the shell should be a restricted one based on NAME or the
1264 value of `restricted'. Don't actually do anything, just return a
1267 shell_is_restricted (name
)
1274 temp
= base_pathname (name
);
1277 return (STREQ (temp
, RESTRICTED_SHELL_NAME
));
1280 /* Perhaps make this shell a `restricted' one, based on NAME. If the
1281 basename of NAME is "rbash", then this shell is restricted. The
1282 name of the restricted shell is a configurable option, see config.h.
1283 In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
1285 Do this also if `restricted' is already set to 1; maybe the shell was
1288 maybe_make_restricted (name
)
1293 temp
= base_pathname (name
);
1296 if (restricted
|| (STREQ (temp
, RESTRICTED_SHELL_NAME
)))
1298 #if defined (RBASH_STATIC_PATH_VALUE)
1299 bind_variable ("PATH", RBASH_STATIC_PATH_VALUE
, 0);
1300 stupidly_hack_special_variables ("PATH"); /* clear hash table */
1302 set_var_read_only ("PATH");
1303 set_var_read_only ("SHELL");
1304 set_var_read_only ("ENV");
1305 set_var_read_only ("BASH_ENV");
1306 set_var_read_only ("HISTFILE");
1309 return (restricted
);
1311 #endif /* RESTRICTED_SHELL */
1313 /* Fetch the current set of uids and gids and return 1 if we're running
1314 setuid or setgid. */
1321 if (current_user
.uid
!= u
)
1323 FREE (current_user
.user_name
);
1324 FREE (current_user
.shell
);
1325 FREE (current_user
.home_dir
);
1326 current_user
.user_name
= current_user
.shell
= current_user
.home_dir
= (char *)NULL
;
1328 current_user
.uid
= u
;
1329 current_user
.gid
= getgid ();
1330 current_user
.euid
= geteuid ();
1331 current_user
.egid
= getegid ();
1333 /* See whether or not we are running setuid or setgid. */
1334 return (current_user
.uid
!= current_user
.euid
) ||
1335 (current_user
.gid
!= current_user
.egid
);
1339 disable_priv_mode ()
1344 if (setresuid (current_user
.uid
, current_user
.uid
, current_user
.uid
) < 0)
1346 if (setuid (current_user
.uid
) < 0)
1350 sys_error (_("cannot set uid to %d: effective uid %d"), current_user
.uid
, current_user
.euid
);
1351 #if defined (EXIT_ON_SETUID_FAILURE)
1357 if (setresgid (current_user
.gid
, current_user
.gid
, current_user
.gid
) < 0)
1359 if (setgid (current_user
.gid
) < 0)
1361 sys_error (_("cannot set gid to %d: effective gid %d"), current_user
.gid
, current_user
.egid
);
1363 current_user
.euid
= current_user
.uid
;
1364 current_user
.egid
= current_user
.gid
;
1367 #if defined (WORDEXP_OPTION)
1373 WORD_LIST
*wl
, *tl
, *result
;
1375 code
= setjmp_nosigs (top_level
);
1377 if (code
!= NOT_JUMPED
)
1381 /* Some kind of throw to top_level has occurred. */
1383 return last_command_exit_value
= 127;
1387 return last_command_exit_value
;
1389 return last_command_exit_value
= 1;
1391 command_error ("run_wordexp", CMDERR_BADJUMP
, code
, 0);
1395 /* Run it through the parser to get a list of words and expand them */
1396 if (words
&& *words
)
1398 with_input_from_string (words
, "--wordexp");
1399 if (parse_command () != 0)
1401 if (global_command
== 0)
1406 if (global_command
->type
!= cm_simple
)
1408 wl
= global_command
->value
.Simple
->words
;
1410 for (tl
= wl
; tl
; tl
= tl
->next
)
1411 tl
->word
->flags
|= W_NOCOMSUB
|W_NOPROCSUB
;
1412 result
= wl
? expand_words_no_vars (wl
) : (WORD_LIST
*)0;
1415 result
= (WORD_LIST
*)0;
1417 last_command_exit_value
= 0;
1425 /* Count up the number of words and bytes, and print them. Don't count
1426 the trailing NUL byte. */
1427 for (nw
= nb
= 0, wl
= result
; wl
; wl
= wl
->next
)
1430 nb
+= strlen (wl
->word
->word
);
1432 printf ("%u\n%u\n", nw
, nb
);
1433 /* Print each word on a separate line. This will have to be changed when
1434 the interface to glibc is completed. */
1435 for (wl
= result
; wl
; wl
= wl
->next
)
1436 printf ("%s\n", wl
->word
->word
);
1442 #if defined (ONESHOT)
1443 /* Run one command, given as the argument to the -c option. Tell
1444 parse_and_execute not to fork for a simple command. */
1446 run_one_command (command
)
1451 code
= setjmp_nosigs (top_level
);
1453 if (code
!= NOT_JUMPED
)
1455 #if defined (PROCESS_SUBSTITUTION)
1456 unlink_fifo_list ();
1457 #endif /* PROCESS_SUBSTITUTION */
1460 /* Some kind of throw to top_level has occurred. */
1462 return last_command_exit_value
= 127;
1466 return last_command_exit_value
;
1468 return last_command_exit_value
= 1;
1470 command_error ("run_one_command", CMDERR_BADJUMP
, code
, 0);
1473 return (parse_and_execute (savestring (command
), "-c", SEVAL_NOHIST
|SEVAL_RESETLINE
));
1475 #endif /* ONESHOT */
1478 bind_args (argv
, arg_start
, arg_end
, start_index
)
1480 int arg_start
, arg_end
, start_index
;
1483 WORD_LIST
*args
, *tl
;
1485 for (i
= arg_start
, args
= tl
= (WORD_LIST
*)NULL
; i
< arg_end
; i
++)
1488 args
= tl
= make_word_list (make_word (argv
[i
]), args
);
1491 tl
->next
= make_word_list (make_word (argv
[i
]), (WORD_LIST
*)NULL
);
1498 if (start_index
== 0) /* bind to $0...$n for sh -c command */
1500 /* Posix.2 4.56.3 says that the first argument after sh -c command
1501 becomes $0, and the rest of the arguments become $1...$n */
1502 shell_name
= savestring (args
->word
->word
);
1503 FREE (dollar_vars
[0]);
1504 dollar_vars
[0] = savestring (args
->word
->word
);
1505 remember_args (args
->next
, 1);
1508 push_args (args
->next
); /* BASH_ARGV and BASH_ARGC */
1509 bash_argv_initialized
= 1;
1512 else /* bind to $1...$n for shell script */
1514 remember_args (args
, 1);
1515 /* We do this unconditionally so something like -O extdebug doesn't
1516 do it first. We're setting the definitive positional params
1520 push_args (args
); /* BASH_ARGV and BASH_ARGC */
1521 bash_argv_initialized
= 1;
1525 dispose_words (args
);
1534 remember_args ((WORD_LIST
*)NULL
, 1);
1535 pop_args (); /* Reset BASH_ARGV and BASH_ARGC */
1541 #if defined (DEBUGGER) && defined (DEBUGGER_START_FILE)
1545 old_errexit
= exit_immediately_on_error
;
1546 exit_immediately_on_error
= 0;
1548 r
= force_execute_file (DEBUGGER_START_FILE
, 1);
1551 internal_warning (_("cannot start debugger; debugging mode disabled"));
1554 error_trace_mode
= function_trace_mode
= debugging_mode
;
1559 exit_immediately_on_error
+= old_errexit
;
1564 open_shell_script (script_name
)
1567 int fd
, e
, fd_is_tty
;
1568 char *filename
, *path_filename
, *t
;
1572 #if defined (ARRAY_VARS)
1573 SHELL_VAR
*funcname_v
, *bash_source_v
, *bash_lineno_v
;
1574 ARRAY
*funcname_a
, *bash_source_a
, *bash_lineno_a
;
1577 filename
= savestring (script_name
);
1579 fd
= open (filename
, O_RDONLY
);
1580 if ((fd
< 0) && (errno
== ENOENT
) && (absolute_program (filename
) == 0))
1583 /* If it's not in the current directory, try looking through PATH
1585 path_filename
= find_path_file (script_name
);
1589 filename
= path_filename
;
1590 fd
= open (filename
, O_RDONLY
);
1599 file_error (filename
);
1600 #if defined (JOB_CONTROL)
1601 end_job_control (); /* just in case we were run as bash -i script */
1603 sh_exit ((e
== ENOENT
) ? EX_NOTFOUND
: EX_NOINPUT
);
1606 free (dollar_vars
[0]);
1607 dollar_vars
[0] = exec_argv0
? savestring (exec_argv0
) : savestring (script_name
);
1611 exec_argv0
= (char *)NULL
;
1614 if (file_isdir (filename
))
1616 #if defined (EISDIR)
1621 file_error (filename
);
1622 #if defined (JOB_CONTROL)
1623 end_job_control (); /* just in case we were run as bash -i script */
1625 sh_exit (EX_NOINPUT
);
1628 #if defined (ARRAY_VARS)
1629 GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v
, funcname_a
);
1630 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v
, bash_source_a
);
1631 GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v
, bash_lineno_a
);
1633 array_push (bash_source_a
, filename
);
1636 t
= itos (executing_line_number ());
1637 array_push (bash_lineno_a
, t
);
1640 array_push (funcname_a
, "main");
1644 fd_is_tty
= isatty (fd
);
1649 /* Only do this with non-tty file descriptors we can seek on. */
1650 if (fd_is_tty
== 0 && (lseek (fd
, 0L, 1) != -1))
1652 /* Check to see if the `file' in `bash file' is a binary file
1653 according to the same tests done by execute_simple_command (),
1654 and report an error and exit if it is. */
1655 sample_len
= read (fd
, sample
, sizeof (sample
));
1659 if ((fstat (fd
, &sb
) == 0) && S_ISDIR (sb
.st_mode
))
1661 #if defined (EISDIR)
1663 file_error (filename
);
1665 internal_error (_("%s: Is a directory"), filename
);
1671 file_error (filename
);
1673 #if defined (JOB_CONTROL)
1674 end_job_control (); /* just in case we were run as bash -i script */
1678 else if (sample_len
> 0 && (check_binary_file (sample
, sample_len
)))
1680 internal_error (_("%s: cannot execute binary file"), filename
);
1681 #if defined (JOB_CONTROL)
1682 end_job_control (); /* just in case we were run as bash -i script */
1684 exit (EX_BINARY_FILE
);
1686 /* Now rewind the file back to the beginning. */
1690 /* Open the script. But try to move the file descriptor to a randomly
1691 large one, in the hopes that any descriptors used by the script will
1692 not match with ours. */
1693 fd
= move_to_high_fd (fd
, 1, -1);
1695 #if defined (BUFFERED_INPUT)
1696 default_buffered_input
= fd
;
1697 SET_CLOSE_ON_EXEC (default_buffered_input
);
1698 #else /* !BUFFERED_INPUT */
1699 default_input
= fdopen (fd
, "r");
1701 if (default_input
== 0)
1703 file_error (filename
);
1707 SET_CLOSE_ON_EXEC (fd
);
1708 if (fileno (default_input
) != fd
)
1709 SET_CLOSE_ON_EXEC (fileno (default_input
));
1710 #endif /* !BUFFERED_INPUT */
1712 /* Just about the only way for this code to be executed is if something
1713 like `bash -i /dev/stdin' is executed. */
1714 if (interactive_shell
&& fd_is_tty
)
1719 #if defined (BUFFERED_INPUT)
1720 default_buffered_input
= 0;
1722 fclose (default_input
);
1723 default_input
= stdin
;
1726 else if (forced_interactive
&& fd_is_tty
== 0)
1727 /* But if a script is called with something like `bash -i scriptname',
1728 we need to do a non-interactive setup here, since we didn't do it
1730 init_interactive_script ();
1734 reading_shell_script
= 1;
1738 /* Initialize the input routines for the parser. */
1742 /* Make sure the fd from which we are reading input is not in
1744 #if defined (BUFFERED_INPUT)
1745 if (interactive
== 0)
1746 sh_unset_nodelay_mode (default_buffered_input
);
1748 #endif /* !BUFFERED_INPUT */
1749 sh_unset_nodelay_mode (fileno (stdin
));
1751 /* with_input_from_stdin really means `with_input_from_readline' */
1752 if (interactive
&& no_line_editing
== 0)
1753 with_input_from_stdin ();
1754 #if defined (BUFFERED_INPUT)
1755 else if (interactive
== 0)
1756 with_input_from_buffered_stream (default_buffered_input
, dollar_vars
[0]);
1757 #endif /* BUFFERED_INPUT */
1759 with_input_from_stream (default_input
, dollar_vars
[0]);
1762 /* Close the current shell script input source and forget about it. This is
1763 extern so execute_cmd.c:initialize_subshell() can call it. If CHECK_ZERO
1764 is non-zero, we close default_buffered_input even if it's the standard
1767 unset_bash_input (check_zero
)
1770 #if defined (BUFFERED_INPUT)
1771 if ((check_zero
&& default_buffered_input
>= 0) ||
1772 (check_zero
== 0 && default_buffered_input
> 0))
1774 close_buffered_fd (default_buffered_input
);
1775 default_buffered_input
= bash_input
.location
.buffered_fd
= -1;
1776 bash_input
.type
= st_none
; /* XXX */
1778 #else /* !BUFFERED_INPUT */
1781 fclose (default_input
);
1782 default_input
= (FILE *)NULL
;
1784 #endif /* !BUFFERED_INPUT */
1788 #if !defined (PROGRAM)
1789 # define PROGRAM "bash"
1793 set_shell_name (argv0
)
1796 /* Here's a hack. If the name of this shell is "sh", then don't do
1797 any startup files; just try to be more like /bin/sh. */
1798 shell_name
= argv0
? base_pathname (argv0
) : PROGRAM
;
1800 if (argv0
&& *argv0
== '-')
1802 if (*shell_name
== '-')
1807 if (shell_name
[0] == 's' && shell_name
[1] == 'h' && shell_name
[2] == '\0')
1809 if (shell_name
[0] == 's' && shell_name
[1] == 'u' && shell_name
[2] == '\0')
1812 shell_name
= argv0
? argv0
: PROGRAM
;
1813 FREE (dollar_vars
[0]);
1814 dollar_vars
[0] = savestring (shell_name
);
1816 /* A program may start an interactive shell with
1817 "execl ("/bin/bash", "-", NULL)".
1818 If so, default the name of this shell to our name. */
1819 if (!shell_name
|| !*shell_name
|| (shell_name
[0] == '-' && !shell_name
[1]))
1820 shell_name
= PROGRAM
;
1823 /* Some options are initialized to -1 so we have a way to determine whether
1824 they were set on the command line. This is an issue when listing the option
1825 values at invocation (`bash -o'), so we set the defaults here and reset
1826 them after the call to list_minus_o_options (). */
1827 /* XXX - could also do this for histexp_flag, jobs_m_flag */
1829 set_option_defaults ()
1831 #if defined (HISTORY)
1832 enable_history_list
= 0;
1837 reset_option_defaults ()
1839 #if defined (HISTORY)
1840 enable_history_list
= -1;
1847 expand_aliases
= expaliases_flag
= 1;
1848 interactive_shell
= startup_state
= interactive
= 1;
1849 #if defined (HISTORY)
1850 if (enable_history_list
== -1)
1851 enable_history_list
= 1; /* set default */
1852 remember_on_history
= enable_history_list
;
1853 # if defined (BANG_HISTORY)
1854 histexp_flag
= history_expansion
; /* XXX */
1860 init_noninteractive ()
1862 #if defined (HISTORY)
1863 if (enable_history_list
== -1) /* set default */
1864 enable_history_list
= 0;
1865 bash_history_reinit (0);
1866 #endif /* HISTORY */
1867 interactive_shell
= startup_state
= interactive
= 0;
1868 expand_aliases
= expaliases_flag
= posixly_correct
; /* XXX - was 0 not posixly_correct */
1869 no_line_editing
= 1;
1870 #if defined (JOB_CONTROL)
1871 /* Even if the shell is not interactive, enable job control if the -i or
1872 -m option is supplied at startup. */
1873 set_job_control (forced_interactive
||jobs_m_flag
);
1874 #endif /* JOB_CONTROL */
1878 init_interactive_script ()
1880 #if defined (HISTORY)
1881 if (enable_history_list
== -1)
1882 enable_history_list
= 1;
1884 init_noninteractive ();
1885 expand_aliases
= expaliases_flag
= interactive_shell
= startup_state
= 1;
1886 #if defined (HISTORY)
1887 remember_on_history
= enable_history_list
; /* XXX */
1892 get_current_user_info ()
1894 struct passwd
*entry
;
1896 /* Don't fetch this more than once. */
1897 if (current_user
.user_name
== 0)
1899 #if defined (__TANDEM)
1900 entry
= getpwnam (getlogin ());
1902 entry
= getpwuid (current_user
.uid
);
1906 current_user
.user_name
= savestring (entry
->pw_name
);
1907 current_user
.shell
= (entry
->pw_shell
&& entry
->pw_shell
[0])
1908 ? savestring (entry
->pw_shell
)
1909 : savestring ("/bin/sh");
1910 current_user
.home_dir
= savestring (entry
->pw_dir
);
1914 current_user
.user_name
= _("I have no name!");
1915 current_user
.user_name
= savestring (current_user
.user_name
);
1916 current_user
.shell
= savestring ("/bin/sh");
1917 current_user
.home_dir
= savestring ("/");
1919 #if defined (HAVE_GETPWENT)
1925 /* Do whatever is necessary to initialize the shell.
1926 Put new initializations in here. */
1931 int should_be_restricted
;
1933 /* Line buffer output for stderr and stdout. */
1934 if (shell_initialized
== 0)
1936 sh_setlinebuf (stderr
);
1937 sh_setlinebuf (stdout
);
1940 /* Sort the array of shell builtins so that the binary search in
1941 find_shell_builtin () works correctly. */
1942 initialize_shell_builtins ();
1944 /* Initialize the trap signal handlers before installing our own
1945 signal handlers. traps.c:restore_original_signals () is responsible
1946 for restoring the original default signal handlers. That function
1947 is called when we make a new child. */
1948 initialize_traps ();
1949 initialize_signals (0);
1951 /* It's highly unlikely that this will change. */
1952 if (current_host_name
== 0)
1954 /* Initialize current_host_name. */
1955 if (gethostname (hostname
, 255) < 0)
1956 current_host_name
= "??host??";
1958 current_host_name
= savestring (hostname
);
1961 /* Initialize the stuff in current_user that comes from the password
1962 file. We don't need to do this right away if the shell is not
1964 if (interactive_shell
)
1965 get_current_user_info ();
1967 /* Initialize our interface to the tilde expander. */
1968 tilde_initialize ();
1970 #if defined (RESTRICTED_SHELL)
1971 should_be_restricted
= shell_is_restricted (shell_name
);
1974 /* Initialize internal and environment variables. Don't import shell
1975 functions from the environment if we are running in privileged or
1976 restricted mode or if the shell is running setuid. */
1977 #if defined (RESTRICTED_SHELL)
1978 initialize_shell_variables (shell_environment
, privileged_mode
||restricted
||should_be_restricted
||running_setuid
);
1980 initialize_shell_variables (shell_environment
, privileged_mode
||running_setuid
);
1983 /* Initialize the data structures for storing and running jobs. */
1984 initialize_job_control (jobs_m_flag
);
1986 /* Initialize input streams to null. */
1987 initialize_bash_input ();
1989 initialize_flags ();
1991 /* Initialize the shell options. Don't import the shell options
1992 from the environment variables $SHELLOPTS or $BASHOPTS if we are
1993 running in privileged or restricted mode or if the shell is running
1995 #if defined (RESTRICTED_SHELL)
1996 initialize_shell_options (privileged_mode
||restricted
||should_be_restricted
||running_setuid
);
1997 initialize_bashopts (privileged_mode
||restricted
||should_be_restricted
||running_setuid
);
1999 initialize_shell_options (privileged_mode
||running_setuid
);
2000 initialize_bashopts (privileged_mode
||running_setuid
);
2004 /* Function called by main () when it appears that the shell has already
2005 had some initialization performed. This is supposed to reset the world
2006 back to a pristine state, as if we had been exec'ed. */
2008 shell_reinitialize ()
2010 /* The default shell prompts. */
2011 primary_prompt
= PPROMPT
;
2012 secondary_prompt
= SPROMPT
;
2014 /* Things that get 1. */
2015 current_command_number
= 1;
2017 /* We have decided that the ~/.bashrc file should not be executed
2018 for the invocation of each shell script. If the variable $ENV
2019 (or $BASH_ENV) is set, its value is used as the name of a file
2021 no_rc
= no_profile
= 1;
2023 /* Things that get 0. */
2024 login_shell
= make_login_shell
= interactive
= executing
= 0;
2025 debugging
= do_version
= line_number
= last_command_exit_value
= 0;
2026 forced_interactive
= interactive_shell
= 0;
2027 subshell_environment
= running_in_background
= 0;
2028 expand_aliases
= expaliases_flag
= 0;
2029 bash_argv_initialized
= 0;
2031 /* XXX - should we set jobs_m_flag to 0 here? */
2033 #if defined (HISTORY)
2034 bash_history_reinit (enable_history_list
= 0);
2035 #endif /* HISTORY */
2037 #if defined (RESTRICTED_SHELL)
2039 #endif /* RESTRICTED_SHELL */
2041 /* Ensure that the default startup file is used. (Except that we don't
2042 execute this file for reinitialized shells). */
2043 bashrc_file
= DEFAULT_BASHRC
;
2045 /* Delete all variables and functions. They will be reinitialized when
2046 the environment is parsed. */
2047 delete_all_contexts (shell_variables
);
2048 delete_all_variables (shell_functions
);
2050 reinit_special_variables ();
2052 #if defined (READLINE)
2053 bashline_reinitialize ();
2056 shell_reinitialized
= 1;
2060 show_shell_usage (fp
, extra
)
2065 char *set_opts
, *s
, *t
;
2068 fprintf (fp
, _("GNU bash, version %s-(%s)\n"), shell_version_string (), MACHTYPE
);
2069 fprintf (fp
, _("Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n"),
2070 shell_name
, shell_name
);
2071 fputs (_("GNU long options:\n"), fp
);
2072 for (i
= 0; long_args
[i
].name
; i
++)
2073 fprintf (fp
, "\t--%s\n", long_args
[i
].name
);
2075 fputs (_("Shell options:\n"), fp
);
2076 fputs (_("\t-ilrsD or -c command or -O shopt_option\t\t(invocation only)\n"), fp
);
2078 for (i
= 0, set_opts
= 0; shell_builtins
[i
].name
; i
++)
2079 if (STREQ (shell_builtins
[i
].name
, "set"))
2081 set_opts
= savestring (shell_builtins
[i
].short_doc
);
2087 s
= strchr (set_opts
, '[');
2092 t
= strchr (s
, ']');
2095 fprintf (fp
, _("\t-%s or -o option\n"), s
);
2101 fprintf (fp
, _("Type `%s -c \"help set\"' for more information about shell options.\n"), shell_name
);
2102 fprintf (fp
, _("Type `%s -c help' for more information about shell builtin commands.\n"), shell_name
);
2103 fprintf (fp
, _("Use the `bashbug' command to report bugs.\n"));
2105 fprintf (fp
, _("bash home page: <http://www.gnu.org/software/bash>\n"));
2106 fprintf (fp
, _("General help using GNU software: <http://www.gnu.org/gethelp/>\n"));
2111 add_shopt_to_alist (opt
, on_or_off
)
2115 if (shopt_ind
>= shopt_len
)
2118 shopt_alist
= (STRING_INT_ALIST
*)xrealloc (shopt_alist
, shopt_len
* sizeof (shopt_alist
[0]));
2120 shopt_alist
[shopt_ind
].word
= opt
;
2121 shopt_alist
[shopt_ind
].token
= on_or_off
;
2130 for (i
= 0; i
< shopt_ind
; i
++)
2131 if (shopt_setopt (shopt_alist
[i
].word
, (shopt_alist
[i
].token
== '-')) != EXECUTION_SUCCESS
)
2135 shopt_ind
= shopt_len
= 0;