1 /* shell.c -- GNU's idea of the POSIX shell specification. */
3 /* Copyright (C) 1987-2015 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 */
56 #include "mailcheck.h"
58 #include "builtins/common.h"
60 #if defined (JOB_CONTROL)
63 extern int initialize_job_control
__P((int));
64 extern int get_tty_state
__P((void));
65 #endif /* JOB_CONTROL */
68 #include "execute_cmd.h"
71 #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
72 # include <malloc/shmalloc.h>
76 # include "bashhist.h"
77 # include <readline/history.h>
80 #if defined (READLINE)
81 # include <readline/readline.h>
82 # include "bashline.h"
85 #include <tilde/tilde.h>
86 #include <glob/strmatch.h>
88 #if defined (__OPENNT)
89 # include <opennt/opennt.h>
92 #if !defined (HAVE_GETPW_DECLS)
93 extern struct passwd
*getpwuid ();
94 #endif /* !HAVE_GETPW_DECLS */
100 #if defined (NO_MAIN_ENV_ARG)
101 extern char **environ
; /* used if no third argument to main() */
104 extern char *dist_version
, *release_status
;
105 extern int patch_level
, build_version
;
106 extern int shell_level
;
107 extern int subshell_environment
;
108 extern int running_in_background
;
109 extern int last_command_exit_value
;
110 extern int line_number
;
111 extern int expand_aliases
;
112 extern int array_needs_making
;
113 extern int gnu_error_format
;
114 extern char *primary_prompt
, *secondary_prompt
;
115 extern char *this_command_name
;
117 /* Non-zero means that this shell has already been run; i.e. you should
118 call shell_reinitialize () if you need to start afresh. */
119 int shell_initialized
= 0;
121 COMMAND
*global_command
= (COMMAND
*)NULL
;
123 /* Information about the current user. */
124 struct user_info current_user
=
126 (uid_t
)-1, (uid_t
)-1, (gid_t
)-1, (gid_t
)-1,
127 (char *)NULL
, (char *)NULL
, (char *)NULL
130 /* The current host's name. */
131 char *current_host_name
= (char *)NULL
;
133 /* Non-zero means that this shell is a login shell.
136 1 = login shell from getty (or equivalent fake out)
137 -1 = login shell from "--login" (or -l) flag.
138 -2 = both from getty, and from flag.
142 /* Non-zero means that at this moment, the shell is interactive. In
143 general, this means that the shell is at this moment reading input
144 from the keyboard. */
147 /* Non-zero means that the shell was started as an interactive shell. */
148 int interactive_shell
= 0;
150 /* Non-zero means to send a SIGHUP to all jobs when an interactive login
154 /* Non-zero means to list status of running and stopped jobs at shell exit */
155 int check_jobs_at_exit
= 0;
157 /* Non-zero means to change to a directory name supplied as a command name */
160 /* Tells what state the shell was in when it started:
161 0 = non-interactive shell script
164 3 = wordexp evaluation
165 This is a superset of the information provided by interactive_shell.
167 int startup_state
= 0;
168 int reading_shell_script
= 0;
170 /* Special debugging helper. */
171 int debugging_login_shell
= 0;
173 /* The environment that the shell passes to other commands. */
174 char **shell_environment
;
176 /* Non-zero when we are executing a top-level command. */
179 /* The number of commands executed so far. */
180 int current_command_number
= 1;
182 /* Non-zero is the recursion depth for commands. */
183 int indirection_level
= 0;
185 /* The name of this shell, as taken from argv[0]. */
186 char *shell_name
= (char *)NULL
;
188 /* time in seconds when the shell was started */
189 time_t shell_start_time
;
191 /* Are we running in an emacs shell window? */
192 int running_under_emacs
;
194 /* Do we have /dev/fd? */
196 int have_devfd
= HAVE_DEV_FD
;
201 /* The name of the .(shell)rc file. */
202 static char *bashrc_file
= DEFAULT_BASHRC
;
204 /* Non-zero means to act more like the Bourne shell on startup. */
205 static int act_like_sh
;
207 /* Non-zero if this shell is being run by `su'. */
210 /* Non-zero if we have already expanded and sourced $ENV. */
211 static int sourced_env
;
213 /* Is this shell running setuid? */
214 static int running_setuid
;
216 /* Values for the long-winded argument names. */
217 static int debugging
; /* Do debugging things. */
218 static int no_rc
; /* Don't execute ~/.bashrc */
219 static int no_profile
; /* Don't execute .profile */
220 static int do_version
; /* Display interesting version info. */
221 static int make_login_shell
; /* Make this shell be a `-bash' shell. */
222 static int want_initial_help
; /* --help option */
224 int debugging_mode
= 0; /* In debugging mode with --debugger */
225 #if defined (READLINE)
226 int no_line_editing
= 0; /* non-zero -> don't do fancy line editing. */
228 int no_line_editing
= 1; /* can't have line editing without readline */
230 int dump_translatable_strings
; /* Dump strings in $"...", don't execute. */
231 int dump_po_strings
; /* Dump strings in $"..." in po format */
232 int wordexp_only
= 0; /* Do word expansion only */
233 int protected_mode
= 0; /* No command substitution with --wordexp */
235 #if defined (STRICT_POSIX)
236 int posixly_correct
= 1; /* Non-zero means posix.2 superset. */
238 int posixly_correct
= 0; /* Non-zero means posix.2 superset. */
241 /* Some long-winded argument names. These are obviously new. */
244 static const struct {
250 { "debug", Int
, &debugging
, (char **)0x0 },
251 #if defined (DEBUGGER)
252 { "debugger", Int
, &debugging_mode
, (char **)0x0 },
254 { "dump-po-strings", Int
, &dump_po_strings
, (char **)0x0 },
255 { "dump-strings", Int
, &dump_translatable_strings
, (char **)0x0 },
256 { "help", Int
, &want_initial_help
, (char **)0x0 },
257 { "init-file", Charp
, (int *)0x0, &bashrc_file
},
258 { "login", Int
, &make_login_shell
, (char **)0x0 },
259 { "noediting", Int
, &no_line_editing
, (char **)0x0 },
260 { "noprofile", Int
, &no_profile
, (char **)0x0 },
261 { "norc", Int
, &no_rc
, (char **)0x0 },
262 { "posix", Int
, &posixly_correct
, (char **)0x0 },
263 #if defined (WORDEXP_OPTION)
264 { "protected", Int
, &protected_mode
, (char **)0x0 },
266 { "rcfile", Charp
, (int *)0x0, &bashrc_file
},
267 #if defined (RESTRICTED_SHELL)
268 { "restricted", Int
, &restricted
, (char **)0x0 },
270 { "verbose", Int
, &verbose_flag
, (char **)0x0 },
271 { "version", Int
, &do_version
, (char **)0x0 },
272 #if defined (WORDEXP_OPTION)
273 { "wordexp", Int
, &wordexp_only
, (char **)0x0 },
275 { (char *)0x0, Int
, (int *)0x0, (char **)0x0 }
278 /* These are extern so execute_simple_command can set them, and then
279 longjmp back to main to execute a shell script, instead of calling
280 main () again and resulting in indefinite, possibly fatal, stack
282 procenv_t subshell_top_level
;
284 char **subshell_argv
;
285 char **subshell_envp
;
289 #if defined (BUFFERED_INPUT)
290 /* The file descriptor from which the shell is reading input. */
291 int default_buffered_input
= -1;
294 /* The following two variables are not static so they can show up in $-. */
295 int read_from_stdin
; /* -s flag supplied */
296 int want_pending_command
; /* -c flag supplied */
298 /* This variable is not static so it can be bound to $BASH_EXECUTION_STRING */
299 char *command_execution_string
; /* argument to -c option */
301 int malloc_trace_at_exit
= 0;
303 static int shell_reinitialized
= 0;
305 static FILE *default_input
;
307 static STRING_INT_ALIST
*shopt_alist
;
308 static int shopt_ind
= 0, shopt_len
= 0;
310 static int parse_long_options
__P((char **, int, int));
311 static int parse_shell_options
__P((char **, int, int));
312 static int bind_args
__P((char **, int, int, int));
314 static void start_debugger
__P((void));
316 static void add_shopt_to_alist
__P((char *, int));
317 static void run_shopt_alist
__P((void));
319 static void execute_env_file
__P((char *));
320 static void run_startup_files
__P((void));
321 static int open_shell_script
__P((char *));
322 static void set_bash_input
__P((void));
323 static int run_one_command
__P((char *));
324 #if defined (WORDEXP_OPTION)
325 static int run_wordexp
__P((char *));
328 static int uidget
__P((void));
330 static void init_interactive
__P((void));
331 static void init_noninteractive
__P((void));
332 static void init_interactive_script
__P((void));
334 static void set_shell_name
__P((char *));
335 static void shell_initialize
__P((void));
336 static void shell_reinitialize
__P((void));
338 static void show_shell_usage
__P((FILE *, int));
342 _cygwin32_check_tmp ()
346 if (stat ("/tmp", &sb
) < 0)
347 internal_warning (_("could not find /tmp, please create!"));
350 if (S_ISDIR (sb
.st_mode
) == 0)
351 internal_warning (_("/tmp must be a valid directory name"));
354 #endif /* __CYGWIN__ */
356 #if defined (NO_MAIN_ENV_ARG)
357 /* systems without third argument to main() */
362 #else /* !NO_MAIN_ENV_ARG */
364 main (argc
, argv
, env
)
367 #endif /* !NO_MAIN_ENV_ARG */
370 int code
, old_errexit_flag
;
371 #if defined (RESTRICTED_SHELL)
374 volatile int locally_skip_execution
;
375 volatile int arg_index
, top_level_arg_index
;
380 #endif /* __OPENNT */
386 USE_VAR(old_errexit_flag
);
387 #if defined (RESTRICTED_SHELL)
391 /* Catch early SIGINTs. */
392 code
= setjmp_nosigs (top_level
);
398 #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
399 malloc_set_register (1); /* XXX - change to 1 for malloc debugging */
405 _cygwin32_check_tmp ();
406 #endif /* __CYGWIN__ */
408 /* Wait forever if we are debugging a login shell. */
409 while (debugging_login_shell
) sleep (3);
411 set_default_locale ();
413 running_setuid
= uidget ();
415 if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC"))
418 #if defined (USE_GNU_MALLOC_LIBRARY)
419 mcheck (programming_error
, (void (*) ())0);
420 #endif /* USE_GNU_MALLOC_LIBRARY */
422 if (setjmp_sigs (subshell_top_level
))
424 argc
= subshell_argc
;
425 argv
= subshell_argv
;
430 shell_reinitialized
= 0;
432 /* Initialize `local' variables for all `invocations' of main (). */
434 if (arg_index
> argc
)
436 command_execution_string
= (char *)NULL
;
437 want_pending_command
= locally_skip_execution
= read_from_stdin
= 0;
438 default_input
= stdin
;
439 #if defined (BUFFERED_INPUT)
440 default_buffered_input
= -1;
443 /* Fix for the `infinite process creation' bug when running shell scripts
444 from startup files on System V. */
445 login_shell
= make_login_shell
= 0;
447 /* If this shell has already been run, then reinitialize it to a
449 if (shell_initialized
|| shell_name
)
451 /* Make sure that we do not infinitely recurse as a login shell. */
452 if (*shell_name
== '-')
455 shell_reinitialize ();
456 if (setjmp_nosigs (top_level
))
460 shell_environment
= env
;
461 set_shell_name (argv
[0]);
462 shell_start_time
= NOW
; /* NOW now defined in general.h */
464 /* Parse argument flags from the input line. */
466 /* Find full word arguments first. */
467 arg_index
= parse_long_options (argv
, arg_index
, argc
);
469 if (want_initial_help
)
471 show_shell_usage (stdout
, 1);
472 exit (EXECUTION_SUCCESS
);
477 show_shell_version (1);
478 exit (EXECUTION_SUCCESS
);
481 echo_input_at_read
= verbose_flag
; /* --verbose given */
483 /* All done with full word options; do standard shell option parsing.*/
484 this_command_name
= shell_name
; /* for error reporting */
485 arg_index
= parse_shell_options (argv
, arg_index
, argc
);
487 /* If user supplied the "--login" (or -l) flag, then set and invert
489 if (make_login_shell
)
492 login_shell
= -login_shell
;
495 set_login_shell ("login_shell", login_shell
!= 0);
498 dump_translatable_strings
= 1;
500 if (dump_translatable_strings
)
501 read_but_dont_execute
= 1;
503 if (running_setuid
&& privileged_mode
== 0)
504 disable_priv_mode ();
506 /* Need to get the argument to a -c option processed in the
507 above loop. The next arg is a command to execute, and the
508 following args are $0...$n respectively. */
509 if (want_pending_command
)
511 command_execution_string
= argv
[arg_index
];
512 if (command_execution_string
== 0)
514 report_error (_("%s: option requires an argument"), "-c");
519 this_command_name
= (char *)NULL
;
521 /* First, let the outside world know about our interactive status.
522 A shell is interactive if the `-i' flag was given, or if all of
523 the following conditions are met:
525 no arguments remaining or the -s flag given
526 standard input is a terminal
527 standard error is a terminal
528 Refer to Posix.2, the description of the `sh' utility. */
530 if (forced_interactive
|| /* -i flag */
531 (!command_execution_string
&& /* No -c command and ... */
532 wordexp_only
== 0 && /* No --wordexp and ... */
533 ((arg_index
== argc
) || /* no remaining args or... */
534 read_from_stdin
) && /* -s flag with args, and */
535 isatty (fileno (stdin
)) && /* Input is a terminal and */
536 isatty (fileno (stderr
)))) /* error output is a terminal. */
539 init_noninteractive ();
542 * Some systems have the bad habit of starting login shells with lots of open
543 * file descriptors. For instance, most systems that have picked up the
544 * pre-4.0 Sun YP code leave a file descriptor open each time you call one
545 * of the getpw* functions, and it's set to be open across execs. That
546 * means one for login, one for xterm, one for shelltool, etc. There are
547 * also systems that open persistent FDs to other agents or files as part
548 * of process startup; these need to be set to be close-on-exec.
550 if (login_shell
&& interactive_shell
)
552 for (i
= 3; i
< 20; i
++)
553 SET_CLOSE_ON_EXEC (i
);
556 /* If we're in a strict Posix.2 mode, turn on interactive comments,
557 alias expansion in non-interactive shells, and other Posix.2 things. */
560 bind_variable ("POSIXLY_CORRECT", "y", 0);
561 sv_strict_posix ("POSIXLY_CORRECT");
564 /* Now we run the shopt_alist and process the options. */
568 /* From here on in, the shell must be a normal functioning shell.
569 Variables from the environment are expected to be set, etc. */
573 set_default_locale_vars ();
576 * M-x term -> TERM=eterm-color INSIDE_EMACS='251,term:0.96' (eterm)
577 * M-x shell -> TERM='dumb' INSIDE_EMACS='25.1,comint' (no line editing)
579 * Older versions of Emacs may set EMACS to 't' or to something like
580 * '22.1 (term:0.96)' instead of (or in addition to) setting INSIDE_EMACS.
581 * They may set TERM to 'eterm' instead of 'eterm-color'. They may have
582 * a now-obsolete command that sets neither EMACS nor INSIDE_EMACS:
583 * M-x terminal -> TERM='emacs-em7955' (line editing)
585 if (interactive_shell
)
587 char *term
, *emacs
, *inside_emacs
;;
588 int emacs_term
, in_emacs
;
590 term
= get_string_value ("TERM");
591 emacs
= get_string_value ("EMACS");
592 inside_emacs
= get_string_value ("INSIDE_EMACS");
596 emacs_term
= strstr (inside_emacs
, ",term:") != 0;
601 /* Infer whether we are in an older Emacs. */
602 emacs_term
= strstr (emacs
, " (term:") != 0;
603 in_emacs
= emacs_term
|| STREQ (emacs
, "t");
606 in_emacs
= emacs_term
= 0;
608 /* Not sure any emacs terminal emulator sets TERM=emacs any more */
609 no_line_editing
|= STREQ (term
, "emacs");
610 no_line_editing
|= in_emacs
&& STREQ (term
, "dumb");
612 /* running_under_emacs == 2 for `eterm' */
613 running_under_emacs
= in_emacs
|| STREQN (term
, "emacs", 5);
614 running_under_emacs
+= emacs_term
&& STREQN (term
, "eterm", 5);
616 if (running_under_emacs
)
617 gnu_error_format
= 1;
620 top_level_arg_index
= arg_index
;
621 old_errexit_flag
= exit_immediately_on_error
;
623 /* Give this shell a place to longjmp to before executing the
624 startup files. This allows users to press C-c to abort the
626 code
= setjmp_sigs (top_level
);
629 if (code
== EXITPROG
|| code
== ERREXIT
)
630 exit_shell (last_command_exit_value
);
633 #if defined (JOB_CONTROL)
634 /* Reset job control, since run_startup_files turned it off. */
635 set_job_control (interactive_shell
);
637 /* Reset value of `set -e', since it's turned off before running
638 the startup files. */
639 exit_immediately_on_error
+= old_errexit_flag
;
640 locally_skip_execution
++;
644 arg_index
= top_level_arg_index
;
646 /* Execute the start-up scripts. */
648 if (interactive_shell
== 0)
650 unbind_variable ("PS1");
651 unbind_variable ("PS2");
654 /* This has already been done by init_noninteractive */
655 expand_aliases
= posixly_correct
;
660 change_flag ('i', FLAG_ON
);
664 #if defined (RESTRICTED_SHELL)
665 /* Set restricted_shell based on whether the basename of $0 indicates that
666 the shell should be restricted or if the `-r' option was supplied at
668 restricted_shell
= shell_is_restricted (shell_name
);
670 /* If the `-r' option is supplied at invocation, make sure that the shell
671 is not in restricted mode when running the startup files. */
672 saverst
= restricted
;
676 /* The startup files are run with `set -e' temporarily disabled. */
677 if (locally_skip_execution
== 0 && running_setuid
== 0)
679 old_errexit_flag
= exit_immediately_on_error
;
680 exit_immediately_on_error
= 0;
682 run_startup_files ();
683 exit_immediately_on_error
+= old_errexit_flag
;
686 /* If we are invoked as `sh', turn on Posix mode. */
689 bind_variable ("POSIXLY_CORRECT", "y", 0);
690 sv_strict_posix ("POSIXLY_CORRECT");
693 #if defined (RESTRICTED_SHELL)
694 /* Turn on the restrictions after executing the startup files. This
695 means that `bash -r' or `set -r' invoked from a startup file will
696 turn on the restrictions after the startup files are executed. */
697 restricted
= saverst
|| restricted
;
698 if (shell_reinitialized
== 0)
699 maybe_make_restricted (shell_name
);
700 #endif /* RESTRICTED_SHELL */
702 #if defined (WORDEXP_OPTION)
706 last_command_exit_value
= run_wordexp (argv
[arg_index
]);
707 exit_shell (last_command_exit_value
);
711 cmd_init (); /* initialize the command object caches */
714 if (command_execution_string
)
716 arg_index
= bind_args (argv
, arg_index
, argc
, 0);
722 #if defined (ONESHOT)
724 run_one_command (command_execution_string
);
725 exit_shell (last_command_exit_value
);
727 with_input_from_string (command_execution_string
, "-c");
728 goto read_and_execute
;
729 #endif /* !ONESHOT */
732 /* Get possible input filename and set up default_buffered_input or
733 default_input as appropriate. */
734 if (arg_index
!= argc
&& read_from_stdin
== 0)
736 open_shell_script (argv
[arg_index
]);
739 else if (interactive
== 0)
741 /* In this mode, bash is reading a script from stdin, which is a
742 pipe or redirected file. */
743 #if defined (BUFFERED_INPUT)
744 default_buffered_input
= fileno (stdin
); /* == 0 */
746 setbuf (default_input
, (char *)NULL
);
747 #endif /* !BUFFERED_INPUT */
750 else if (arg_index
== argc
)
751 /* "If there are no operands and the -c option is not specified, the -s
752 option shall be assumed." */
757 /* Bind remaining args to $1 ... $n */
758 arg_index
= bind_args (argv
, arg_index
, argc
, 1);
760 if (debugging_mode
&& locally_skip_execution
== 0 && running_setuid
== 0 && (reading_shell_script
|| interactive_shell
== 0))
763 /* Do the things that should be done only for interactive shells. */
764 if (interactive_shell
)
766 /* Set up for checking for presence of mail. */
770 #if defined (HISTORY)
771 /* Initialize the interactive history stuff. */
772 bash_initialize_history ();
773 /* Don't load the history from the history file if we've already
774 saved some lines in this session (e.g., by putting `history -s xx'
775 into one of the startup files). */
776 if (shell_initialized
== 0 && history_lines_this_session
== 0)
780 /* Initialize terminal state for interactive shells after the
781 .bash_profile and .bashrc are interpreted. */
785 #if !defined (ONESHOT)
787 #endif /* !ONESHOT */
789 shell_initialized
= 1;
791 /* Read commands until exit condition. */
793 exit_shell (last_command_exit_value
);
797 parse_long_options (argv
, arg_start
, arg_end
)
799 int arg_start
, arg_end
;
801 int arg_index
, longarg
, i
;
804 arg_index
= arg_start
;
805 while ((arg_index
!= arg_end
) && (arg_string
= argv
[arg_index
]) &&
806 (*arg_string
== '-'))
810 /* Make --login equivalent to -login. */
811 if (arg_string
[1] == '-' && arg_string
[2])
817 for (i
= 0; long_args
[i
].name
; i
++)
819 if (STREQ (arg_string
+ 1, long_args
[i
].name
))
821 if (long_args
[i
].type
== Int
)
822 *long_args
[i
].int_value
= 1;
823 else if (argv
[++arg_index
] == 0)
825 report_error (_("%s: option requires an argument"), long_args
[i
].name
);
829 *long_args
[i
].char_value
= argv
[arg_index
];
834 if (long_args
[i
].name
== 0)
838 report_error (_("%s: invalid option"), argv
[arg_index
]);
839 show_shell_usage (stderr
, 0);
842 break; /* No such argument. Maybe flag arg. */
852 parse_shell_options (argv
, arg_start
, arg_end
)
854 int arg_start
, arg_end
;
857 int arg_character
, on_or_off
, next_arg
, i
;
858 char *o_option
, *arg_string
;
860 arg_index
= arg_start
;
861 while (arg_index
!= arg_end
&& (arg_string
= argv
[arg_index
]) &&
862 (*arg_string
== '-' || *arg_string
== '+'))
864 /* There are flag arguments, so parse them. */
865 next_arg
= arg_index
+ 1;
867 /* A single `-' signals the end of options. From the 4.3 BSD sh.
868 An option `--' means the same thing; this is the standard
869 getopt(3) meaning. */
870 if (arg_string
[0] == '-' &&
871 (arg_string
[1] == '\0' ||
872 (arg_string
[1] == '-' && arg_string
[2] == '\0')))
876 on_or_off
= arg_string
[0];
877 while (arg_character
= arg_string
[i
++])
879 switch (arg_character
)
882 want_pending_command
= 1;
886 make_login_shell
= 1;
894 o_option
= argv
[next_arg
];
897 list_minus_o_opts (-1, (on_or_off
== '-') ? 0 : 1);
900 if (set_minus_o_option (on_or_off
, o_option
) != EXECUTION_SUCCESS
)
906 /* Since some of these can be overridden by the normal
907 interactive/non-interactive shell initialization or
908 initializing posix mode, we save the options and process
909 them after initialization. */
910 o_option
= argv
[next_arg
];
913 shopt_listopt (o_option
, (on_or_off
== '-') ? 0 : 1);
916 add_shopt_to_alist (o_option
, on_or_off
);
921 dump_translatable_strings
= 1;
925 if (change_flag (arg_character
, on_or_off
) == FLAG_ERROR
)
927 report_error (_("%c%c: invalid option"), on_or_off
, arg_character
);
928 show_shell_usage (stderr
, 0);
933 /* Can't do just a simple increment anymore -- what about
934 "bash -abouo emacs ignoreeof -hP"? */
935 arg_index
= next_arg
;
941 /* Exit the shell with status S. */
946 fflush (stdout
); /* XXX */
949 /* Clean up the terminal if we are in a state where it's been modified. */
950 #if defined (READLINE)
951 if (RL_ISSTATE (RL_STATE_TERMPREPPED
) && rl_deprep_term_function
)
952 (*rl_deprep_term_function
) ();
954 if (read_tty_modified ())
957 /* Do trap[0] if defined. Allow it to override the exit status
959 if (signal_is_trapped (0))
960 s
= run_exit_trap ();
962 #if defined (PROCESS_SUBSTITUTION)
964 #endif /* PROCESS_SUBSTITUTION */
966 #if defined (HISTORY)
967 if (remember_on_history
)
968 maybe_save_shell_history ();
971 #if defined (COPROCESS_SUPPORT)
975 #if defined (JOB_CONTROL)
976 /* If the user has run `shopt -s huponexit', hangup all jobs when we exit
977 an interactive login shell. ksh does this unconditionally. */
978 if (interactive_shell
&& login_shell
&& hup_on_exit
)
981 /* If this shell is interactive, or job control is active, terminate all
982 stopped jobs and restore the original terminal process group. Don't do
983 this if we're in a subshell and calling exit_shell after, for example,
984 a failed word expansion. We want to do this even if the shell is not
985 interactive because we set the terminal's process group when job control
986 is enabled regardless of the interactive status. */
987 if (subshell_environment
== 0)
989 #endif /* JOB_CONTROL */
991 /* Always return the exit status of the last command to our parent. */
995 /* A wrapper for exit that (optionally) can do other things, like malloc
996 statistics tracing. */
1001 #if defined (MALLOC_DEBUG) && defined (USING_BASH_MALLOC)
1002 if (malloc_trace_at_exit
)
1003 trace_malloc_stats (get_name_for_error (), (char *)NULL
);
1004 /* mlocation_write_table (); */
1010 /* Exit a subshell, which includes calling the exit trap. We don't want to
1011 do any more cleanup, since a subshell is created as an exact copy of its
1020 /* Do trap[0] if defined. Allow it to override the exit status
1022 if (signal_is_trapped (0))
1023 s
= run_exit_trap ();
1028 /* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey
1029 the Posix.2 startup file rules: $ENV is expanded, and if the file it
1030 names exists, that file is sourced. The Posix.2 rules are in effect
1031 for interactive shells only. (section 4.56.5.3) */
1033 /* Execute ~/.bashrc for most shells. Never execute it if
1034 ACT_LIKE_SH is set, or if NO_RC is set.
1036 If the executable file "/usr/gnu/src/bash/foo" contains:
1043 COMMAND EXECUTE BASHRC
1044 --------------------------------
1048 rsh machine ls YES (for rsh, which calls `bash -c')
1049 rsh machine foo YES (for shell started by rsh) NO (for foo!)
1056 execute_env_file (env_file
)
1061 if (env_file
&& *env_file
)
1063 fn
= expand_string_unsplit_to_string (env_file
, Q_DOUBLE_QUOTES
);
1065 maybe_execute_file (fn
, 1);
1071 run_startup_files ()
1073 #if defined (JOB_CONTROL)
1074 int old_job_control
;
1076 int sourced_login
, run_by_ssh
;
1078 /* get the rshd/sshd case out of the way first. */
1079 if (interactive_shell
== 0 && no_rc
== 0 && login_shell
== 0 &&
1080 act_like_sh
== 0 && command_execution_string
)
1082 #ifdef SSH_SOURCE_BASHRC
1083 run_by_ssh
= (find_variable ("SSH_CLIENT") != (SHELL_VAR
*)0) ||
1084 (find_variable ("SSH2_CLIENT") != (SHELL_VAR
*)0);
1089 /* If we were run by sshd or we think we were run by rshd, execute
1090 ~/.bashrc if we are a top-level shell. */
1091 if ((run_by_ssh
|| isnetconn (fileno (stdin
))) && shell_level
< 2)
1094 # if defined (__OPENNT)
1095 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
1097 maybe_execute_file (SYS_BASHRC
, 1);
1100 maybe_execute_file (bashrc_file
, 1);
1105 #if defined (JOB_CONTROL)
1106 /* Startup files should be run without job control enabled. */
1107 old_job_control
= interactive_shell
? set_job_control (0) : 0;
1112 /* A shell begun with the --login (or -l) flag that is not in posix mode
1113 runs the login shell startup files, no matter whether or not it is
1114 interactive. If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
1115 startup files if argv[0][0] == '-' as well. */
1116 #if defined (NON_INTERACTIVE_LOGIN_SHELLS)
1117 if (login_shell
&& posixly_correct
== 0)
1119 if (login_shell
< 0 && posixly_correct
== 0)
1122 /* We don't execute .bashrc for login shells. */
1125 /* Execute /etc/profile and one of the personal login shell
1126 initialization files. */
1127 if (no_profile
== 0)
1129 maybe_execute_file (SYS_PROFILE
, 1);
1131 if (act_like_sh
) /* sh */
1132 maybe_execute_file ("~/.profile", 1);
1133 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
1134 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
1135 maybe_execute_file ("~/.profile", 1);
1141 /* A non-interactive shell not named `sh' and not in posix mode reads and
1142 executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd'
1143 and `-su' as the name of the shell, we want to read the startup files.
1144 No other non-interactive shells read any startup files. */
1145 if (interactive_shell
== 0 && !(su_shell
&& login_shell
))
1147 if (posixly_correct
== 0 && act_like_sh
== 0 && privileged_mode
== 0 &&
1149 execute_env_file (get_string_value ("BASH_ENV"));
1153 /* Interactive shell or `-su' shell. */
1154 if (posixly_correct
== 0) /* bash, sh */
1156 if (login_shell
&& sourced_login
++ == 0)
1158 /* We don't execute .bashrc for login shells. */
1161 /* Execute /etc/profile and one of the personal login shell
1162 initialization files. */
1163 if (no_profile
== 0)
1165 maybe_execute_file (SYS_PROFILE
, 1);
1167 if (act_like_sh
) /* sh */
1168 maybe_execute_file ("~/.profile", 1);
1169 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
1170 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
1171 maybe_execute_file ("~/.profile", 1);
1176 if (act_like_sh
== 0 && no_rc
== 0)
1179 # if defined (__OPENNT)
1180 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
1182 maybe_execute_file (SYS_BASHRC
, 1);
1185 maybe_execute_file (bashrc_file
, 1);
1188 else if (act_like_sh
&& privileged_mode
== 0 && sourced_env
++ == 0)
1189 execute_env_file (get_string_value ("ENV"));
1191 else /* bash --posix, sh --posix */
1194 if (interactive_shell
&& privileged_mode
== 0 && sourced_env
++ == 0)
1195 execute_env_file (get_string_value ("ENV"));
1198 #if defined (JOB_CONTROL)
1199 set_job_control (old_job_control
);
1203 #if defined (RESTRICTED_SHELL)
1204 /* Return 1 if the shell should be a restricted one based on NAME or the
1205 value of `restricted'. Don't actually do anything, just return a
1208 shell_is_restricted (name
)
1215 temp
= base_pathname (name
);
1218 return (STREQ (temp
, RESTRICTED_SHELL_NAME
));
1221 /* Perhaps make this shell a `restricted' one, based on NAME. If the
1222 basename of NAME is "rbash", then this shell is restricted. The
1223 name of the restricted shell is a configurable option, see config.h.
1224 In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
1226 Do this also if `restricted' is already set to 1; maybe the shell was
1229 maybe_make_restricted (name
)
1234 temp
= base_pathname (name
);
1237 if (restricted
|| (STREQ (temp
, RESTRICTED_SHELL_NAME
)))
1239 set_var_read_only ("PATH");
1240 set_var_read_only ("SHELL");
1241 set_var_read_only ("ENV");
1242 set_var_read_only ("BASH_ENV");
1245 return (restricted
);
1247 #endif /* RESTRICTED_SHELL */
1249 /* Fetch the current set of uids and gids and return 1 if we're running
1250 setuid or setgid. */
1257 if (current_user
.uid
!= u
)
1259 FREE (current_user
.user_name
);
1260 FREE (current_user
.shell
);
1261 FREE (current_user
.home_dir
);
1262 current_user
.user_name
= current_user
.shell
= current_user
.home_dir
= (char *)NULL
;
1264 current_user
.uid
= u
;
1265 current_user
.gid
= getgid ();
1266 current_user
.euid
= geteuid ();
1267 current_user
.egid
= getegid ();
1269 /* See whether or not we are running setuid or setgid. */
1270 return (current_user
.uid
!= current_user
.euid
) ||
1271 (current_user
.gid
!= current_user
.egid
);
1275 disable_priv_mode ()
1279 if (setuid (current_user
.uid
) < 0)
1282 sys_error (_("cannot set uid to %d: effective uid %d"), current_user
.uid
, current_user
.euid
);
1283 #if defined (EXIT_ON_SETUID_FAILURE)
1288 if (setgid (current_user
.gid
) < 0)
1289 sys_error (_("cannot set gid to %d: effective gid %d"), current_user
.gid
, current_user
.egid
);
1291 current_user
.euid
= current_user
.uid
;
1292 current_user
.egid
= current_user
.gid
;
1295 #if defined (WORDEXP_OPTION)
1301 WORD_LIST
*wl
, *tl
, *result
;
1303 code
= setjmp_nosigs (top_level
);
1305 if (code
!= NOT_JUMPED
)
1309 /* Some kind of throw to top_level has occurred. */
1311 return last_command_exit_value
= 127;
1314 return last_command_exit_value
;
1316 return last_command_exit_value
= 1;
1318 command_error ("run_wordexp", CMDERR_BADJUMP
, code
, 0);
1322 /* Run it through the parser to get a list of words and expand them */
1323 if (words
&& *words
)
1325 with_input_from_string (words
, "--wordexp");
1326 if (parse_command () != 0)
1328 if (global_command
== 0)
1333 if (global_command
->type
!= cm_simple
)
1335 wl
= global_command
->value
.Simple
->words
;
1337 for (tl
= wl
; tl
; tl
= tl
->next
)
1338 tl
->word
->flags
|= W_NOCOMSUB
|W_NOPROCSUB
;
1339 result
= wl
? expand_words_no_vars (wl
) : (WORD_LIST
*)0;
1342 result
= (WORD_LIST
*)0;
1344 last_command_exit_value
= 0;
1352 /* Count up the number of words and bytes, and print them. Don't count
1353 the trailing NUL byte. */
1354 for (nw
= nb
= 0, wl
= result
; wl
; wl
= wl
->next
)
1357 nb
+= strlen (wl
->word
->word
);
1359 printf ("%u\n%u\n", nw
, nb
);
1360 /* Print each word on a separate line. This will have to be changed when
1361 the interface to glibc is completed. */
1362 for (wl
= result
; wl
; wl
= wl
->next
)
1363 printf ("%s\n", wl
->word
->word
);
1369 #if defined (ONESHOT)
1370 /* Run one command, given as the argument to the -c option. Tell
1371 parse_and_execute not to fork for a simple command. */
1373 run_one_command (command
)
1378 code
= setjmp_nosigs (top_level
);
1380 if (code
!= NOT_JUMPED
)
1382 #if defined (PROCESS_SUBSTITUTION)
1383 unlink_fifo_list ();
1384 #endif /* PROCESS_SUBSTITUTION */
1387 /* Some kind of throw to top_level has occurred. */
1389 return last_command_exit_value
= 127;
1392 return last_command_exit_value
;
1394 return last_command_exit_value
= 1;
1396 command_error ("run_one_command", CMDERR_BADJUMP
, code
, 0);
1399 return (parse_and_execute (savestring (command
), "-c", SEVAL_NOHIST
));
1401 #endif /* ONESHOT */
1404 bind_args (argv
, arg_start
, arg_end
, start_index
)
1406 int arg_start
, arg_end
, start_index
;
1411 for (i
= arg_start
, args
= (WORD_LIST
*)NULL
; i
< arg_end
; i
++)
1412 args
= make_word_list (make_word (argv
[i
]), args
);
1415 args
= REVERSE_LIST (args
, WORD_LIST
*);
1416 if (start_index
== 0) /* bind to $0...$n for sh -c command */
1418 /* Posix.2 4.56.3 says that the first argument after sh -c command
1419 becomes $0, and the rest of the arguments become $1...$n */
1420 shell_name
= savestring (args
->word
->word
);
1421 FREE (dollar_vars
[0]);
1422 dollar_vars
[0] = savestring (args
->word
->word
);
1423 remember_args (args
->next
, 1);
1424 push_args (args
->next
); /* BASH_ARGV and BASH_ARGC */
1426 else /* bind to $1...$n for shell script */
1428 remember_args (args
, 1);
1429 push_args (args
); /* BASH_ARGV and BASH_ARGC */
1432 dispose_words (args
);
1441 remember_args ((WORD_LIST
*)NULL
, 1);
1442 pop_args (); /* Reset BASH_ARGV and BASH_ARGC */
1448 #if defined (DEBUGGER) && defined (DEBUGGER_START_FILE)
1452 old_errexit
= exit_immediately_on_error
;
1453 exit_immediately_on_error
= 0;
1455 r
= force_execute_file (DEBUGGER_START_FILE
, 1);
1458 internal_warning (_("cannot start debugger; debugging mode disabled"));
1461 error_trace_mode
= function_trace_mode
= debugging_mode
;
1466 exit_immediately_on_error
+= old_errexit
;
1471 open_shell_script (script_name
)
1474 int fd
, e
, fd_is_tty
;
1475 char *filename
, *path_filename
, *t
;
1479 #if defined (ARRAY_VARS)
1480 SHELL_VAR
*funcname_v
, *bash_source_v
, *bash_lineno_v
;
1481 ARRAY
*funcname_a
, *bash_source_a
, *bash_lineno_a
;
1484 filename
= savestring (script_name
);
1486 fd
= open (filename
, O_RDONLY
);
1487 if ((fd
< 0) && (errno
== ENOENT
) && (absolute_program (filename
) == 0))
1490 /* If it's not in the current directory, try looking through PATH
1492 path_filename
= find_path_file (script_name
);
1496 filename
= path_filename
;
1497 fd
= open (filename
, O_RDONLY
);
1506 file_error (filename
);
1507 sh_exit ((e
== ENOENT
) ? EX_NOTFOUND
: EX_NOINPUT
);
1510 free (dollar_vars
[0]);
1511 dollar_vars
[0] = exec_argv0
? savestring (exec_argv0
) : savestring (script_name
);
1515 exec_argv0
= (char *)NULL
;
1518 if (file_isdir (filename
))
1520 #if defined (EISDIR)
1525 file_error (filename
);
1526 sh_exit (EX_NOINPUT
);
1529 #if defined (ARRAY_VARS)
1530 GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v
, funcname_a
);
1531 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v
, bash_source_a
);
1532 GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v
, bash_lineno_a
);
1534 array_push (bash_source_a
, filename
);
1537 t
= itos (executing_line_number ());
1538 array_push (bash_lineno_a
, t
);
1541 array_push (funcname_a
, "main");
1545 fd_is_tty
= isatty (fd
);
1550 /* Only do this with non-tty file descriptors we can seek on. */
1551 if (fd_is_tty
== 0 && (lseek (fd
, 0L, 1) != -1))
1553 /* Check to see if the `file' in `bash file' is a binary file
1554 according to the same tests done by execute_simple_command (),
1555 and report an error and exit if it is. */
1556 sample_len
= read (fd
, sample
, sizeof (sample
));
1560 if ((fstat (fd
, &sb
) == 0) && S_ISDIR (sb
.st_mode
))
1562 #if defined (EISDIR)
1564 file_error (filename
);
1566 internal_error (_("%s: Is a directory"), filename
);
1572 file_error (filename
);
1576 else if (sample_len
> 0 && (check_binary_file (sample
, sample_len
)))
1578 internal_error (_("%s: cannot execute binary file"), filename
);
1579 exit (EX_BINARY_FILE
);
1581 /* Now rewind the file back to the beginning. */
1585 /* Open the script. But try to move the file descriptor to a randomly
1586 large one, in the hopes that any descriptors used by the script will
1587 not match with ours. */
1588 fd
= move_to_high_fd (fd
, 1, -1);
1590 #if defined (BUFFERED_INPUT)
1591 default_buffered_input
= fd
;
1592 SET_CLOSE_ON_EXEC (default_buffered_input
);
1593 #else /* !BUFFERED_INPUT */
1594 default_input
= fdopen (fd
, "r");
1596 if (default_input
== 0)
1598 file_error (filename
);
1602 SET_CLOSE_ON_EXEC (fd
);
1603 if (fileno (default_input
) != fd
)
1604 SET_CLOSE_ON_EXEC (fileno (default_input
));
1605 #endif /* !BUFFERED_INPUT */
1607 /* Just about the only way for this code to be executed is if something
1608 like `bash -i /dev/stdin' is executed. */
1609 if (interactive_shell
&& fd_is_tty
)
1614 #if defined (BUFFERED_INPUT)
1615 default_buffered_input
= 0;
1617 fclose (default_input
);
1618 default_input
= stdin
;
1621 else if (forced_interactive
&& fd_is_tty
== 0)
1622 /* But if a script is called with something like `bash -i scriptname',
1623 we need to do a non-interactive setup here, since we didn't do it
1625 init_interactive_script ();
1629 reading_shell_script
= 1;
1633 /* Initialize the input routines for the parser. */
1637 /* Make sure the fd from which we are reading input is not in
1639 #if defined (BUFFERED_INPUT)
1640 if (interactive
== 0)
1641 sh_unset_nodelay_mode (default_buffered_input
);
1643 #endif /* !BUFFERED_INPUT */
1644 sh_unset_nodelay_mode (fileno (stdin
));
1646 /* with_input_from_stdin really means `with_input_from_readline' */
1647 if (interactive
&& no_line_editing
== 0)
1648 with_input_from_stdin ();
1649 #if defined (BUFFERED_INPUT)
1650 else if (interactive
== 0)
1651 with_input_from_buffered_stream (default_buffered_input
, dollar_vars
[0]);
1652 #endif /* BUFFERED_INPUT */
1654 with_input_from_stream (default_input
, dollar_vars
[0]);
1657 /* Close the current shell script input source and forget about it. This is
1658 extern so execute_cmd.c:initialize_subshell() can call it. If CHECK_ZERO
1659 is non-zero, we close default_buffered_input even if it's the standard
1662 unset_bash_input (check_zero
)
1665 #if defined (BUFFERED_INPUT)
1666 if ((check_zero
&& default_buffered_input
>= 0) ||
1667 (check_zero
== 0 && default_buffered_input
> 0))
1669 close_buffered_fd (default_buffered_input
);
1670 default_buffered_input
= bash_input
.location
.buffered_fd
= -1;
1671 bash_input
.type
= st_none
; /* XXX */
1673 #else /* !BUFFERED_INPUT */
1676 fclose (default_input
);
1677 default_input
= (FILE *)NULL
;
1679 #endif /* !BUFFERED_INPUT */
1683 #if !defined (PROGRAM)
1684 # define PROGRAM "bash"
1688 set_shell_name (argv0
)
1691 /* Here's a hack. If the name of this shell is "sh", then don't do
1692 any startup files; just try to be more like /bin/sh. */
1693 shell_name
= argv0
? base_pathname (argv0
) : PROGRAM
;
1695 if (argv0
&& *argv0
== '-')
1697 if (*shell_name
== '-')
1702 if (shell_name
[0] == 's' && shell_name
[1] == 'h' && shell_name
[2] == '\0')
1704 if (shell_name
[0] == 's' && shell_name
[1] == 'u' && shell_name
[2] == '\0')
1707 shell_name
= argv0
? argv0
: PROGRAM
;
1708 FREE (dollar_vars
[0]);
1709 dollar_vars
[0] = savestring (shell_name
);
1711 /* A program may start an interactive shell with
1712 "execl ("/bin/bash", "-", NULL)".
1713 If so, default the name of this shell to our name. */
1714 if (!shell_name
|| !*shell_name
|| (shell_name
[0] == '-' && !shell_name
[1]))
1715 shell_name
= PROGRAM
;
1721 expand_aliases
= interactive_shell
= startup_state
= 1;
1723 #if defined (HISTORY)
1724 remember_on_history
= enable_history_list
= 1; /* XXX */
1729 init_noninteractive ()
1731 #if defined (HISTORY)
1732 bash_history_reinit (0);
1733 #endif /* HISTORY */
1734 interactive_shell
= startup_state
= interactive
= 0;
1735 expand_aliases
= posixly_correct
; /* XXX - was 0 not posixly_correct */
1736 no_line_editing
= 1;
1737 #if defined (JOB_CONTROL)
1738 /* Even if the shell is not interactive, enable job control if the -i or
1739 -m option is supplied at startup. */
1740 set_job_control (forced_interactive
||jobs_m_flag
);
1741 #endif /* JOB_CONTROL */
1745 init_interactive_script ()
1747 init_noninteractive ();
1748 expand_aliases
= interactive_shell
= startup_state
= 1;
1749 #if defined (HISTORY)
1750 remember_on_history
= enable_history_list
= 1; /* XXX */
1755 get_current_user_info ()
1757 struct passwd
*entry
;
1759 /* Don't fetch this more than once. */
1760 if (current_user
.user_name
== 0)
1762 #if defined (__TANDEM)
1763 entry
= getpwnam (getlogin ());
1765 entry
= getpwuid (current_user
.uid
);
1769 current_user
.user_name
= savestring (entry
->pw_name
);
1770 current_user
.shell
= (entry
->pw_shell
&& entry
->pw_shell
[0])
1771 ? savestring (entry
->pw_shell
)
1772 : savestring ("/bin/sh");
1773 current_user
.home_dir
= savestring (entry
->pw_dir
);
1777 current_user
.user_name
= _("I have no name!");
1778 current_user
.user_name
= savestring (current_user
.user_name
);
1779 current_user
.shell
= savestring ("/bin/sh");
1780 current_user
.home_dir
= savestring ("/");
1782 #if defined (HAVE_GETPWENT)
1788 /* Do whatever is necessary to initialize the shell.
1789 Put new initializations in here. */
1794 int should_be_restricted
;
1796 /* Line buffer output for stderr and stdout. */
1797 if (shell_initialized
== 0)
1799 sh_setlinebuf (stderr
);
1800 sh_setlinebuf (stdout
);
1803 /* Sort the array of shell builtins so that the binary search in
1804 find_shell_builtin () works correctly. */
1805 initialize_shell_builtins ();
1807 /* Initialize the trap signal handlers before installing our own
1808 signal handlers. traps.c:restore_original_signals () is responsible
1809 for restoring the original default signal handlers. That function
1810 is called when we make a new child. */
1811 initialize_traps ();
1812 initialize_signals (0);
1814 /* It's highly unlikely that this will change. */
1815 if (current_host_name
== 0)
1817 /* Initialize current_host_name. */
1818 if (gethostname (hostname
, 255) < 0)
1819 current_host_name
= "??host??";
1821 current_host_name
= savestring (hostname
);
1824 /* Initialize the stuff in current_user that comes from the password
1825 file. We don't need to do this right away if the shell is not
1827 if (interactive_shell
)
1828 get_current_user_info ();
1830 /* Initialize our interface to the tilde expander. */
1831 tilde_initialize ();
1833 #if defined (RESTRICTED_SHELL)
1834 should_be_restricted
= shell_is_restricted (shell_name
);
1837 /* Initialize internal and environment variables. Don't import shell
1838 functions from the environment if we are running in privileged or
1839 restricted mode or if the shell is running setuid. */
1840 #if defined (RESTRICTED_SHELL)
1841 initialize_shell_variables (shell_environment
, privileged_mode
||restricted
||should_be_restricted
||running_setuid
);
1843 initialize_shell_variables (shell_environment
, privileged_mode
||running_setuid
);
1846 /* Initialize the data structures for storing and running jobs. */
1847 initialize_job_control (jobs_m_flag
);
1849 /* Initialize input streams to null. */
1850 initialize_bash_input ();
1852 initialize_flags ();
1854 /* Initialize the shell options. Don't import the shell options
1855 from the environment variables $SHELLOPTS or $BASHOPTS if we are
1856 running in privileged or restricted mode or if the shell is running
1858 #if defined (RESTRICTED_SHELL)
1859 initialize_shell_options (privileged_mode
||restricted
||should_be_restricted
||running_setuid
);
1860 initialize_bashopts (privileged_mode
||restricted
||should_be_restricted
||running_setuid
);
1862 initialize_shell_options (privileged_mode
||running_setuid
);
1863 initialize_bashopts (privileged_mode
||running_setuid
);
1867 /* Function called by main () when it appears that the shell has already
1868 had some initialization performed. This is supposed to reset the world
1869 back to a pristine state, as if we had been exec'ed. */
1871 shell_reinitialize ()
1873 /* The default shell prompts. */
1874 primary_prompt
= PPROMPT
;
1875 secondary_prompt
= SPROMPT
;
1877 /* Things that get 1. */
1878 current_command_number
= 1;
1880 /* We have decided that the ~/.bashrc file should not be executed
1881 for the invocation of each shell script. If the variable $ENV
1882 (or $BASH_ENV) is set, its value is used as the name of a file
1884 no_rc
= no_profile
= 1;
1886 /* Things that get 0. */
1887 login_shell
= make_login_shell
= interactive
= executing
= 0;
1888 debugging
= do_version
= line_number
= last_command_exit_value
= 0;
1889 forced_interactive
= interactive_shell
= 0;
1890 subshell_environment
= running_in_background
= 0;
1893 /* XXX - should we set jobs_m_flag to 0 here? */
1895 #if defined (HISTORY)
1896 bash_history_reinit (enable_history_list
= 0);
1897 #endif /* HISTORY */
1899 #if defined (RESTRICTED_SHELL)
1901 #endif /* RESTRICTED_SHELL */
1903 /* Ensure that the default startup file is used. (Except that we don't
1904 execute this file for reinitialized shells). */
1905 bashrc_file
= DEFAULT_BASHRC
;
1907 /* Delete all variables and functions. They will be reinitialized when
1908 the environment is parsed. */
1909 delete_all_contexts (shell_variables
);
1910 delete_all_variables (shell_functions
);
1912 reinit_special_variables ();
1914 #if defined (READLINE)
1915 bashline_reinitialize ();
1918 shell_reinitialized
= 1;
1922 show_shell_usage (fp
, extra
)
1927 char *set_opts
, *s
, *t
;
1930 fprintf (fp
, _("GNU bash, version %s-(%s)\n"), shell_version_string (), MACHTYPE
);
1931 fprintf (fp
, _("Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n"),
1932 shell_name
, shell_name
);
1933 fputs (_("GNU long options:\n"), fp
);
1934 for (i
= 0; long_args
[i
].name
; i
++)
1935 fprintf (fp
, "\t--%s\n", long_args
[i
].name
);
1937 fputs (_("Shell options:\n"), fp
);
1938 fputs (_("\t-ilrsD or -c command or -O shopt_option\t\t(invocation only)\n"), fp
);
1940 for (i
= 0, set_opts
= 0; shell_builtins
[i
].name
; i
++)
1941 if (STREQ (shell_builtins
[i
].name
, "set"))
1942 set_opts
= savestring (shell_builtins
[i
].short_doc
);
1945 s
= strchr (set_opts
, '[');
1950 t
= strchr (s
, ']');
1953 fprintf (fp
, _("\t-%s or -o option\n"), s
);
1959 fprintf (fp
, _("Type `%s -c \"help set\"' for more information about shell options.\n"), shell_name
);
1960 fprintf (fp
, _("Type `%s -c help' for more information about shell builtin commands.\n"), shell_name
);
1961 fprintf (fp
, _("Use the `bashbug' command to report bugs.\n"));
1963 fprintf (fp
, _("bash home page: <http://www.gnu.org/software/bash>\n"));
1964 fprintf (fp
, _("General help using GNU software: <http://www.gnu.org/gethelp/>\n"));
1969 add_shopt_to_alist (opt
, on_or_off
)
1973 if (shopt_ind
>= shopt_len
)
1976 shopt_alist
= (STRING_INT_ALIST
*)xrealloc (shopt_alist
, shopt_len
* sizeof (shopt_alist
[0]));
1978 shopt_alist
[shopt_ind
].word
= opt
;
1979 shopt_alist
[shopt_ind
].token
= on_or_off
;
1988 for (i
= 0; i
< shopt_ind
; i
++)
1989 if (shopt_setopt (shopt_alist
[i
].word
, (shopt_alist
[i
].token
== '-')) != EXECUTION_SUCCESS
)
1993 shopt_ind
= shopt_len
= 0;