1 /* shell.c -- GNU's idea of the POSIX shell specification. */
3 /* Copyright (C) 1987-2017 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
__P((int));
66 extern int get_tty_state
__P((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>
78 # include "bashhist.h"
79 # include <readline/history.h>
82 #if defined (READLINE)
83 # include <readline/readline.h>
84 # include "bashline.h"
87 #include <tilde/tilde.h>
88 #include <glob/strmatch.h>
90 #if defined (__OPENNT)
91 # include <opennt/opennt.h>
94 #if !defined (HAVE_GETPW_DECLS)
95 extern struct passwd
*getpwuid ();
96 #endif /* !HAVE_GETPW_DECLS */
102 #if defined (NO_MAIN_ENV_ARG)
103 extern char **environ
; /* used if no third argument to main() */
106 extern int gnu_error_format
;
108 /* Non-zero means that this shell has already been run; i.e. you should
109 call shell_reinitialize () if you need to start afresh. */
110 int shell_initialized
= 0;
111 int bash_argv_initialized
= 0;
113 COMMAND
*global_command
= (COMMAND
*)NULL
;
115 /* Information about the current user. */
116 struct user_info current_user
=
118 (uid_t
)-1, (uid_t
)-1, (gid_t
)-1, (gid_t
)-1,
119 (char *)NULL
, (char *)NULL
, (char *)NULL
122 /* The current host's name. */
123 char *current_host_name
= (char *)NULL
;
125 /* Non-zero means that this shell is a login shell.
128 1 = login shell from getty (or equivalent fake out)
129 -1 = login shell from "--login" (or -l) flag.
130 -2 = both from getty, and from flag.
134 /* Non-zero means that at this moment, the shell is interactive. In
135 general, this means that the shell is at this moment reading input
136 from the keyboard. */
139 /* Non-zero means that the shell was started as an interactive shell. */
140 int interactive_shell
= 0;
142 /* Non-zero means to send a SIGHUP to all jobs when an interactive login
146 /* Non-zero means to list status of running and stopped jobs at shell exit */
147 int check_jobs_at_exit
= 0;
149 /* Non-zero means to change to a directory name supplied as a command name */
152 /* Tells what state the shell was in when it started:
153 0 = non-interactive shell script
156 3 = wordexp evaluation
157 This is a superset of the information provided by interactive_shell.
159 int startup_state
= 0;
160 int reading_shell_script
= 0;
162 /* Special debugging helper. */
163 int debugging_login_shell
= 0;
165 /* The environment that the shell passes to other commands. */
166 char **shell_environment
;
168 /* Non-zero when we are executing a top-level command. */
171 /* The number of commands executed so far. */
172 int current_command_number
= 1;
174 /* Non-zero is the recursion depth for commands. */
175 int indirection_level
= 0;
177 /* The name of this shell, as taken from argv[0]. */
178 char *shell_name
= (char *)NULL
;
180 /* time in seconds when the shell was started */
181 time_t shell_start_time
;
183 /* Are we running in an emacs shell window? */
184 int running_under_emacs
;
186 /* Do we have /dev/fd? */
188 int have_devfd
= HAVE_DEV_FD
;
193 /* The name of the .(shell)rc file. */
194 static char *bashrc_file
= DEFAULT_BASHRC
;
196 /* Non-zero means to act more like the Bourne shell on startup. */
197 static int act_like_sh
;
199 /* Non-zero if this shell is being run by `su'. */
202 /* Non-zero if we have already expanded and sourced $ENV. */
203 static int sourced_env
;
205 /* Is this shell running setuid? */
206 static int running_setuid
;
208 /* Values for the long-winded argument names. */
209 static int debugging
; /* Do debugging things. */
210 static int no_rc
; /* Don't execute ~/.bashrc */
211 static int no_profile
; /* Don't execute .profile */
212 static int do_version
; /* Display interesting version info. */
213 static int make_login_shell
; /* Make this shell be a `-bash' shell. */
214 static int want_initial_help
; /* --help option */
216 int debugging_mode
= 0; /* In debugging mode with --debugger */
217 #if defined (READLINE)
218 int no_line_editing
= 0; /* non-zero -> don't do fancy line editing. */
220 int no_line_editing
= 1; /* can't have line editing without readline */
222 int dump_translatable_strings
; /* Dump strings in $"...", don't execute. */
223 int dump_po_strings
; /* Dump strings in $"..." in po format */
224 int wordexp_only
= 0; /* Do word expansion only */
225 int protected_mode
= 0; /* No command substitution with --wordexp */
227 int pretty_print_mode
= 0; /* pretty-print a shell script */
229 #if defined (STRICT_POSIX)
230 int posixly_correct
= 1; /* Non-zero means posix.2 superset. */
232 int posixly_correct
= 0; /* Non-zero means posix.2 superset. */
235 /* Some long-winded argument names. These are obviously new. */
238 static const struct {
244 { "debug", Int
, &debugging
, (char **)0x0 },
245 #if defined (DEBUGGER)
246 { "debugger", Int
, &debugging_mode
, (char **)0x0 },
248 { "dump-po-strings", Int
, &dump_po_strings
, (char **)0x0 },
249 { "dump-strings", Int
, &dump_translatable_strings
, (char **)0x0 },
250 { "help", Int
, &want_initial_help
, (char **)0x0 },
251 { "init-file", Charp
, (int *)0x0, &bashrc_file
},
252 { "login", Int
, &make_login_shell
, (char **)0x0 },
253 { "noediting", Int
, &no_line_editing
, (char **)0x0 },
254 { "noprofile", Int
, &no_profile
, (char **)0x0 },
255 { "norc", Int
, &no_rc
, (char **)0x0 },
256 { "posix", Int
, &posixly_correct
, (char **)0x0 },
257 { "pretty-print", Int
, &pretty_print_mode
, (char **)0x0 },
258 #if defined (WORDEXP_OPTION)
259 { "protected", Int
, &protected_mode
, (char **)0x0 },
261 { "rcfile", Charp
, (int *)0x0, &bashrc_file
},
262 #if defined (RESTRICTED_SHELL)
263 { "restricted", Int
, &restricted
, (char **)0x0 },
265 { "verbose", Int
, &verbose_flag
, (char **)0x0 },
266 { "version", Int
, &do_version
, (char **)0x0 },
267 #if defined (WORDEXP_OPTION)
268 { "wordexp", Int
, &wordexp_only
, (char **)0x0 },
270 { (char *)0x0, Int
, (int *)0x0, (char **)0x0 }
273 /* These are extern so execute_simple_command can set them, and then
274 longjmp back to main to execute a shell script, instead of calling
275 main () again and resulting in indefinite, possibly fatal, stack
277 procenv_t subshell_top_level
;
279 char **subshell_argv
;
280 char **subshell_envp
;
284 #if defined (BUFFERED_INPUT)
285 /* The file descriptor from which the shell is reading input. */
286 int default_buffered_input
= -1;
289 /* The following two variables are not static so they can show up in $-. */
290 int read_from_stdin
; /* -s flag supplied */
291 int want_pending_command
; /* -c flag supplied */
293 /* This variable is not static so it can be bound to $BASH_EXECUTION_STRING */
294 char *command_execution_string
; /* argument to -c option */
295 char *shell_script_filename
; /* shell script */
297 int malloc_trace_at_exit
= 0;
299 static int shell_reinitialized
= 0;
301 static FILE *default_input
;
303 static STRING_INT_ALIST
*shopt_alist
;
304 static int shopt_ind
= 0, shopt_len
= 0;
306 static int parse_long_options
__P((char **, int, int));
307 static int parse_shell_options
__P((char **, int, int));
308 static int bind_args
__P((char **, int, int, int));
310 static void start_debugger
__P((void));
312 static void add_shopt_to_alist
__P((char *, int));
313 static void run_shopt_alist
__P((void));
315 static void execute_env_file
__P((char *));
316 static void run_startup_files
__P((void));
317 static int open_shell_script
__P((char *));
318 static void set_bash_input
__P((void));
319 static int run_one_command
__P((char *));
320 #if defined (WORDEXP_OPTION)
321 static int run_wordexp
__P((char *));
324 static int uidget
__P((void));
326 static void init_interactive
__P((void));
327 static void init_noninteractive
__P((void));
328 static void init_interactive_script
__P((void));
330 static void set_shell_name
__P((char *));
331 static void shell_initialize
__P((void));
332 static void shell_reinitialize
__P((void));
334 static void show_shell_usage
__P((FILE *, int));
338 _cygwin32_check_tmp ()
342 if (stat ("/tmp", &sb
) < 0)
343 internal_warning (_("could not find /tmp, please create!"));
346 if (S_ISDIR (sb
.st_mode
) == 0)
347 internal_warning (_("/tmp must be a valid directory name"));
350 #endif /* __CYGWIN__ */
352 #if defined (NO_MAIN_ENV_ARG)
353 /* systems without third argument to main() */
358 #else /* !NO_MAIN_ENV_ARG */
360 main (argc
, argv
, env
)
363 #endif /* !NO_MAIN_ENV_ARG */
366 int code
, old_errexit_flag
;
367 #if defined (RESTRICTED_SHELL)
370 volatile int locally_skip_execution
;
371 volatile int arg_index
, top_level_arg_index
;
376 #endif /* __OPENNT */
382 USE_VAR(old_errexit_flag
);
383 #if defined (RESTRICTED_SHELL)
387 /* Catch early SIGINTs. */
388 code
= setjmp_nosigs (top_level
);
394 #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
395 malloc_set_register (1); /* XXX - change to 1 for malloc debugging */
401 _cygwin32_check_tmp ();
402 #endif /* __CYGWIN__ */
404 /* Wait forever if we are debugging a login shell. */
405 while (debugging_login_shell
) sleep (3);
407 set_default_locale ();
409 running_setuid
= uidget ();
411 if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC"))
414 #if defined (USE_GNU_MALLOC_LIBRARY)
415 mcheck (programming_error
, (void (*) ())0);
416 #endif /* USE_GNU_MALLOC_LIBRARY */
418 if (setjmp_sigs (subshell_top_level
))
420 argc
= subshell_argc
;
421 argv
= subshell_argv
;
426 shell_reinitialized
= 0;
428 /* Initialize `local' variables for all `invocations' of main (). */
430 if (arg_index
> argc
)
432 command_execution_string
= shell_script_filename
= (char *)NULL
;
433 want_pending_command
= locally_skip_execution
= read_from_stdin
= 0;
434 default_input
= stdin
;
435 #if defined (BUFFERED_INPUT)
436 default_buffered_input
= -1;
439 /* Fix for the `infinite process creation' bug when running shell scripts
440 from startup files on System V. */
441 login_shell
= make_login_shell
= 0;
443 /* If this shell has already been run, then reinitialize it to a
445 if (shell_initialized
|| shell_name
)
447 /* Make sure that we do not infinitely recurse as a login shell. */
448 if (*shell_name
== '-')
451 shell_reinitialize ();
452 if (setjmp_nosigs (top_level
))
456 shell_environment
= env
;
457 set_shell_name (argv
[0]);
458 shell_start_time
= NOW
; /* NOW now defined in general.h */
460 /* Parse argument flags from the input line. */
462 /* Find full word arguments first. */
463 arg_index
= parse_long_options (argv
, arg_index
, argc
);
465 if (want_initial_help
)
467 show_shell_usage (stdout
, 1);
468 exit (EXECUTION_SUCCESS
);
473 show_shell_version (1);
474 exit (EXECUTION_SUCCESS
);
477 echo_input_at_read
= verbose_flag
; /* --verbose given */
479 /* All done with full word options; do standard shell option parsing.*/
480 this_command_name
= shell_name
; /* for error reporting */
481 arg_index
= parse_shell_options (argv
, arg_index
, argc
);
483 /* If user supplied the "--login" (or -l) flag, then set and invert
485 if (make_login_shell
)
488 login_shell
= -login_shell
;
491 set_login_shell ("login_shell", login_shell
!= 0);
494 dump_translatable_strings
= 1;
496 if (dump_translatable_strings
)
497 read_but_dont_execute
= 1;
499 if (running_setuid
&& privileged_mode
== 0)
500 disable_priv_mode ();
502 /* Need to get the argument to a -c option processed in the
503 above loop. The next arg is a command to execute, and the
504 following args are $0...$n respectively. */
505 if (want_pending_command
)
507 command_execution_string
= argv
[arg_index
];
508 if (command_execution_string
== 0)
510 report_error (_("%s: option requires an argument"), "-c");
515 this_command_name
= (char *)NULL
;
517 /* First, let the outside world know about our interactive status.
518 A shell is interactive if the `-i' flag was given, or if all of
519 the following conditions are met:
521 no arguments remaining or the -s flag given
522 standard input is a terminal
523 standard error is a terminal
524 Refer to Posix.2, the description of the `sh' utility. */
526 if (forced_interactive
|| /* -i flag */
527 (!command_execution_string
&& /* No -c command and ... */
528 wordexp_only
== 0 && /* No --wordexp and ... */
529 ((arg_index
== argc
) || /* no remaining args or... */
530 read_from_stdin
) && /* -s flag with args, and */
531 isatty (fileno (stdin
)) && /* Input is a terminal and */
532 isatty (fileno (stderr
)))) /* error output is a terminal. */
535 init_noninteractive ();
538 * Some systems have the bad habit of starting login shells with lots of open
539 * file descriptors. For instance, most systems that have picked up the
540 * pre-4.0 Sun YP code leave a file descriptor open each time you call one
541 * of the getpw* functions, and it's set to be open across execs. That
542 * means one for login, one for xterm, one for shelltool, etc. There are
543 * also systems that open persistent FDs to other agents or files as part
544 * of process startup; these need to be set to be close-on-exec.
546 if (login_shell
&& interactive_shell
)
548 for (i
= 3; i
< 20; i
++)
549 SET_CLOSE_ON_EXEC (i
);
552 /* If we're in a strict Posix.2 mode, turn on interactive comments,
553 alias expansion in non-interactive shells, and other Posix.2 things. */
556 bind_variable ("POSIXLY_CORRECT", "y", 0);
557 sv_strict_posix ("POSIXLY_CORRECT");
560 /* Now we run the shopt_alist and process the options. */
564 /* From here on in, the shell must be a normal functioning shell.
565 Variables from the environment are expected to be set, etc. */
569 set_default_locale_vars ();
572 * M-x term -> TERM=eterm-color INSIDE_EMACS='251,term:0.96' (eterm)
573 * M-x shell -> TERM='dumb' INSIDE_EMACS='25.1,comint' (no line editing)
575 * Older versions of Emacs may set EMACS to 't' or to something like
576 * '22.1 (term:0.96)' instead of (or in addition to) setting INSIDE_EMACS.
577 * They may set TERM to 'eterm' instead of 'eterm-color'. They may have
578 * a now-obsolete command that sets neither EMACS nor INSIDE_EMACS:
579 * M-x terminal -> TERM='emacs-em7955' (line editing)
581 if (interactive_shell
)
583 char *term
, *emacs
, *inside_emacs
;
584 int emacs_term
, in_emacs
;
586 term
= get_string_value ("TERM");
587 emacs
= get_string_value ("EMACS");
588 inside_emacs
= get_string_value ("INSIDE_EMACS");
592 emacs_term
= strstr (inside_emacs
, ",term:") != 0;
597 /* Infer whether we are in an older Emacs. */
598 emacs_term
= strstr (emacs
, " (term:") != 0;
599 in_emacs
= emacs_term
|| STREQ (emacs
, "t");
602 in_emacs
= emacs_term
= 0;
604 /* Not sure any emacs terminal emulator sets TERM=emacs any more */
605 no_line_editing
|= STREQ (term
, "emacs");
606 no_line_editing
|= in_emacs
&& STREQ (term
, "dumb");
608 /* running_under_emacs == 2 for `eterm' */
609 running_under_emacs
= in_emacs
|| STREQN (term
, "emacs", 5);
610 running_under_emacs
+= emacs_term
&& STREQN (term
, "eterm", 5);
612 if (running_under_emacs
)
613 gnu_error_format
= 1;
616 top_level_arg_index
= arg_index
;
617 old_errexit_flag
= exit_immediately_on_error
;
619 /* Give this shell a place to longjmp to before executing the
620 startup files. This allows users to press C-c to abort the
622 code
= setjmp_sigs (top_level
);
625 if (code
== EXITPROG
|| code
== ERREXIT
)
626 exit_shell (last_command_exit_value
);
629 #if defined (JOB_CONTROL)
630 /* Reset job control, since run_startup_files turned it off. */
631 set_job_control (interactive_shell
);
633 /* Reset value of `set -e', since it's turned off before running
634 the startup files. */
635 exit_immediately_on_error
+= old_errexit_flag
;
636 locally_skip_execution
++;
640 arg_index
= top_level_arg_index
;
642 /* Execute the start-up scripts. */
644 if (interactive_shell
== 0)
646 unbind_variable ("PS1");
647 unbind_variable ("PS2");
650 /* This has already been done by init_noninteractive */
651 expand_aliases
= posixly_correct
;
656 change_flag ('i', FLAG_ON
);
660 #if defined (RESTRICTED_SHELL)
661 /* Set restricted_shell based on whether the basename of $0 indicates that
662 the shell should be restricted or if the `-r' option was supplied at
664 restricted_shell
= shell_is_restricted (shell_name
);
666 /* If the `-r' option is supplied at invocation, make sure that the shell
667 is not in restricted mode when running the startup files. */
668 saverst
= restricted
;
672 /* Set positional parameters before running startup files. top_level_arg_index
673 holds the index of the current argument before setting the positional
674 parameters, so any changes performed in the startup files won't affect
675 later option processing. */
678 else if (command_execution_string
)
679 arg_index
= bind_args (argv
, arg_index
, argc
, 0); /* $0 ... $n */
680 else if (arg_index
!= argc
&& read_from_stdin
== 0)
682 shell_script_filename
= argv
[arg_index
++];
683 arg_index
= bind_args (argv
, arg_index
, argc
, 1); /* $1 ... $n */
686 arg_index
= bind_args (argv
, arg_index
, argc
, 1); /* $1 ... $n */
688 /* The startup files are run with `set -e' temporarily disabled. */
689 if (locally_skip_execution
== 0 && running_setuid
== 0)
691 old_errexit_flag
= exit_immediately_on_error
;
692 exit_immediately_on_error
= 0;
694 run_startup_files ();
695 exit_immediately_on_error
+= old_errexit_flag
;
698 /* If we are invoked as `sh', turn on Posix mode. */
701 bind_variable ("POSIXLY_CORRECT", "y", 0);
702 sv_strict_posix ("POSIXLY_CORRECT");
705 #if defined (RESTRICTED_SHELL)
706 /* Turn on the restrictions after executing the startup files. This
707 means that `bash -r' or `set -r' invoked from a startup file will
708 turn on the restrictions after the startup files are executed. */
709 restricted
= saverst
|| restricted
;
710 if (shell_reinitialized
== 0)
711 maybe_make_restricted (shell_name
);
712 #endif /* RESTRICTED_SHELL */
714 #if defined (WORDEXP_OPTION)
718 last_command_exit_value
= run_wordexp (argv
[top_level_arg_index
]);
719 exit_shell (last_command_exit_value
);
723 cmd_init (); /* initialize the command object caches */
726 if (command_execution_string
)
733 #if defined (ONESHOT)
735 run_one_command (command_execution_string
);
736 exit_shell (last_command_exit_value
);
738 with_input_from_string (command_execution_string
, "-c");
739 goto read_and_execute
;
740 #endif /* !ONESHOT */
743 /* Get possible input filename and set up default_buffered_input or
744 default_input as appropriate. */
745 if (shell_script_filename
)
746 open_shell_script (shell_script_filename
);
747 else if (interactive
== 0)
749 /* In this mode, bash is reading a script from stdin, which is a
750 pipe or redirected file. */
751 #if defined (BUFFERED_INPUT)
752 default_buffered_input
= fileno (stdin
); /* == 0 */
754 setbuf (default_input
, (char *)NULL
);
755 #endif /* !BUFFERED_INPUT */
758 else if (top_level_arg_index
== argc
) /* arg index before startup files */
759 /* "If there are no operands and the -c option is not specified, the -s
760 option shall be assumed." */
765 if (debugging_mode
&& locally_skip_execution
== 0 && running_setuid
== 0 && (reading_shell_script
|| interactive_shell
== 0))
768 /* Do the things that should be done only for interactive shells. */
769 if (interactive_shell
)
771 /* Set up for checking for presence of mail. */
775 #if defined (HISTORY)
776 /* Initialize the interactive history stuff. */
777 bash_initialize_history ();
778 /* Don't load the history from the history file if we've already
779 saved some lines in this session (e.g., by putting `history -s xx'
780 into one of the startup files). */
781 if (shell_initialized
== 0 && history_lines_this_session
== 0)
785 /* Initialize terminal state for interactive shells after the
786 .bash_profile and .bashrc are interpreted. */
790 #if !defined (ONESHOT)
792 #endif /* !ONESHOT */
794 shell_initialized
= 1;
796 if (pretty_print_mode
&& interactive_shell
)
798 internal_warning (_("pretty-printing mode ignored in interactive shells"));
799 pretty_print_mode
= 0;
801 if (pretty_print_mode
)
802 exit_shell (pretty_print_loop ());
804 /* Read commands until exit condition. */
806 exit_shell (last_command_exit_value
);
810 parse_long_options (argv
, arg_start
, arg_end
)
812 int arg_start
, arg_end
;
814 int arg_index
, longarg
, i
;
817 arg_index
= arg_start
;
818 while ((arg_index
!= arg_end
) && (arg_string
= argv
[arg_index
]) &&
819 (*arg_string
== '-'))
823 /* Make --login equivalent to -login. */
824 if (arg_string
[1] == '-' && arg_string
[2])
830 for (i
= 0; long_args
[i
].name
; i
++)
832 if (STREQ (arg_string
+ 1, long_args
[i
].name
))
834 if (long_args
[i
].type
== Int
)
835 *long_args
[i
].int_value
= 1;
836 else if (argv
[++arg_index
] == 0)
838 report_error (_("%s: option requires an argument"), long_args
[i
].name
);
842 *long_args
[i
].char_value
= argv
[arg_index
];
847 if (long_args
[i
].name
== 0)
851 report_error (_("%s: invalid option"), argv
[arg_index
]);
852 show_shell_usage (stderr
, 0);
855 break; /* No such argument. Maybe flag arg. */
865 parse_shell_options (argv
, arg_start
, arg_end
)
867 int arg_start
, arg_end
;
870 int arg_character
, on_or_off
, next_arg
, i
;
871 char *o_option
, *arg_string
;
873 arg_index
= arg_start
;
874 while (arg_index
!= arg_end
&& (arg_string
= argv
[arg_index
]) &&
875 (*arg_string
== '-' || *arg_string
== '+'))
877 /* There are flag arguments, so parse them. */
878 next_arg
= arg_index
+ 1;
880 /* A single `-' signals the end of options. From the 4.3 BSD sh.
881 An option `--' means the same thing; this is the standard
882 getopt(3) meaning. */
883 if (arg_string
[0] == '-' &&
884 (arg_string
[1] == '\0' ||
885 (arg_string
[1] == '-' && arg_string
[2] == '\0')))
889 on_or_off
= arg_string
[0];
890 while (arg_character
= arg_string
[i
++])
892 switch (arg_character
)
895 want_pending_command
= 1;
899 make_login_shell
= 1;
907 o_option
= argv
[next_arg
];
910 list_minus_o_opts (-1, (on_or_off
== '-') ? 0 : 1);
913 if (set_minus_o_option (on_or_off
, o_option
) != EXECUTION_SUCCESS
)
919 /* Since some of these can be overridden by the normal
920 interactive/non-interactive shell initialization or
921 initializing posix mode, we save the options and process
922 them after initialization. */
923 o_option
= argv
[next_arg
];
926 shopt_listopt (o_option
, (on_or_off
== '-') ? 0 : 1);
929 add_shopt_to_alist (o_option
, on_or_off
);
934 dump_translatable_strings
= 1;
938 if (change_flag (arg_character
, on_or_off
) == FLAG_ERROR
)
940 report_error (_("%c%c: invalid option"), on_or_off
, arg_character
);
941 show_shell_usage (stderr
, 0);
946 /* Can't do just a simple increment anymore -- what about
947 "bash -abouo emacs ignoreeof -hP"? */
948 arg_index
= next_arg
;
954 /* Exit the shell with status S. */
959 fflush (stdout
); /* XXX */
962 /* Clean up the terminal if we are in a state where it's been modified. */
963 #if defined (READLINE)
964 if (RL_ISSTATE (RL_STATE_TERMPREPPED
) && rl_deprep_term_function
)
965 (*rl_deprep_term_function
) ();
967 if (read_tty_modified ())
970 /* Do trap[0] if defined. Allow it to override the exit status
972 if (signal_is_trapped (0))
973 s
= run_exit_trap ();
975 #if defined (PROCESS_SUBSTITUTION)
977 #endif /* PROCESS_SUBSTITUTION */
979 #if defined (HISTORY)
980 if (remember_on_history
)
981 maybe_save_shell_history ();
984 #if defined (COPROCESS_SUPPORT)
988 #if defined (JOB_CONTROL)
989 /* If the user has run `shopt -s huponexit', hangup all jobs when we exit
990 an interactive login shell. ksh does this unconditionally. */
991 if (interactive_shell
&& login_shell
&& hup_on_exit
)
994 /* If this shell is interactive, or job control is active, terminate all
995 stopped jobs and restore the original terminal process group. Don't do
996 this if we're in a subshell and calling exit_shell after, for example,
997 a failed word expansion. We want to do this even if the shell is not
998 interactive because we set the terminal's process group when job control
999 is enabled regardless of the interactive status. */
1000 if (subshell_environment
== 0)
1002 #endif /* JOB_CONTROL */
1004 /* Always return the exit status of the last command to our parent. */
1008 /* A wrapper for exit that (optionally) can do other things, like malloc
1009 statistics tracing. */
1014 #if defined (MALLOC_DEBUG) && defined (USING_BASH_MALLOC)
1015 if (malloc_trace_at_exit
&& (subshell_environment
& (SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
)) == 0)
1016 trace_malloc_stats (get_name_for_error (), (char *)NULL
);
1017 /* mlocation_write_table (); */
1023 /* Exit a subshell, which includes calling the exit trap. We don't want to
1024 do any more cleanup, since a subshell is created as an exact copy of its
1033 /* Do trap[0] if defined. Allow it to override the exit status
1035 if (signal_is_trapped (0))
1036 s
= run_exit_trap ();
1041 /* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey
1042 the Posix.2 startup file rules: $ENV is expanded, and if the file it
1043 names exists, that file is sourced. The Posix.2 rules are in effect
1044 for interactive shells only. (section 4.56.5.3) */
1046 /* Execute ~/.bashrc for most shells. Never execute it if
1047 ACT_LIKE_SH is set, or if NO_RC is set.
1049 If the executable file "/usr/gnu/src/bash/foo" contains:
1056 COMMAND EXECUTE BASHRC
1057 --------------------------------
1061 rsh machine ls YES (for rsh, which calls `bash -c')
1062 rsh machine foo YES (for shell started by rsh) NO (for foo!)
1069 execute_env_file (env_file
)
1074 if (env_file
&& *env_file
)
1076 fn
= expand_string_unsplit_to_string (env_file
, Q_DOUBLE_QUOTES
);
1078 maybe_execute_file (fn
, 1);
1084 run_startup_files ()
1086 #if defined (JOB_CONTROL)
1087 int old_job_control
;
1089 int sourced_login
, run_by_ssh
;
1091 /* get the rshd/sshd case out of the way first. */
1092 if (interactive_shell
== 0 && no_rc
== 0 && login_shell
== 0 &&
1093 act_like_sh
== 0 && command_execution_string
)
1095 #ifdef SSH_SOURCE_BASHRC
1096 run_by_ssh
= (find_variable ("SSH_CLIENT") != (SHELL_VAR
*)0) ||
1097 (find_variable ("SSH2_CLIENT") != (SHELL_VAR
*)0);
1102 /* If we were run by sshd or we think we were run by rshd, execute
1103 ~/.bashrc if we are a top-level shell. */
1104 if ((run_by_ssh
|| isnetconn (fileno (stdin
))) && shell_level
< 2)
1107 # if defined (__OPENNT)
1108 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
1110 maybe_execute_file (SYS_BASHRC
, 1);
1113 maybe_execute_file (bashrc_file
, 1);
1118 #if defined (JOB_CONTROL)
1119 /* Startup files should be run without job control enabled. */
1120 old_job_control
= interactive_shell
? set_job_control (0) : 0;
1125 /* A shell begun with the --login (or -l) flag that is not in posix mode
1126 runs the login shell startup files, no matter whether or not it is
1127 interactive. If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
1128 startup files if argv[0][0] == '-' as well. */
1129 #if defined (NON_INTERACTIVE_LOGIN_SHELLS)
1130 if (login_shell
&& posixly_correct
== 0)
1132 if (login_shell
< 0 && posixly_correct
== 0)
1135 /* We don't execute .bashrc for login shells. */
1138 /* Execute /etc/profile and one of the personal login shell
1139 initialization files. */
1140 if (no_profile
== 0)
1142 maybe_execute_file (SYS_PROFILE
, 1);
1144 if (act_like_sh
) /* sh */
1145 maybe_execute_file ("~/.profile", 1);
1146 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
1147 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
1148 maybe_execute_file ("~/.profile", 1);
1154 /* A non-interactive shell not named `sh' and not in posix mode reads and
1155 executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd'
1156 and `-su' as the name of the shell, we want to read the startup files.
1157 No other non-interactive shells read any startup files. */
1158 if (interactive_shell
== 0 && !(su_shell
&& login_shell
))
1160 if (posixly_correct
== 0 && act_like_sh
== 0 && privileged_mode
== 0 &&
1162 execute_env_file (get_string_value ("BASH_ENV"));
1166 /* Interactive shell or `-su' shell. */
1167 if (posixly_correct
== 0) /* bash, sh */
1169 if (login_shell
&& sourced_login
++ == 0)
1171 /* We don't execute .bashrc for login shells. */
1174 /* Execute /etc/profile and one of the personal login shell
1175 initialization files. */
1176 if (no_profile
== 0)
1178 maybe_execute_file (SYS_PROFILE
, 1);
1180 if (act_like_sh
) /* sh */
1181 maybe_execute_file ("~/.profile", 1);
1182 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
1183 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
1184 maybe_execute_file ("~/.profile", 1);
1189 if (act_like_sh
== 0 && no_rc
== 0)
1192 # if defined (__OPENNT)
1193 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
1195 maybe_execute_file (SYS_BASHRC
, 1);
1198 maybe_execute_file (bashrc_file
, 1);
1201 else if (act_like_sh
&& privileged_mode
== 0 && sourced_env
++ == 0)
1202 execute_env_file (get_string_value ("ENV"));
1204 else /* bash --posix, sh --posix */
1207 if (interactive_shell
&& privileged_mode
== 0 && sourced_env
++ == 0)
1208 execute_env_file (get_string_value ("ENV"));
1211 #if defined (JOB_CONTROL)
1212 set_job_control (old_job_control
);
1216 #if defined (RESTRICTED_SHELL)
1217 /* Return 1 if the shell should be a restricted one based on NAME or the
1218 value of `restricted'. Don't actually do anything, just return a
1221 shell_is_restricted (name
)
1228 temp
= base_pathname (name
);
1231 return (STREQ (temp
, RESTRICTED_SHELL_NAME
));
1234 /* Perhaps make this shell a `restricted' one, based on NAME. If the
1235 basename of NAME is "rbash", then this shell is restricted. The
1236 name of the restricted shell is a configurable option, see config.h.
1237 In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
1239 Do this also if `restricted' is already set to 1; maybe the shell was
1242 maybe_make_restricted (name
)
1247 temp
= base_pathname (name
);
1250 if (restricted
|| (STREQ (temp
, RESTRICTED_SHELL_NAME
)))
1252 #if defined (RBASH_STATIC_PATH_VALUE)
1253 bind_variable ("PATH", RBASH_STATIC_PATH_VALUE
, 0);
1254 stupidly_hack_special_variables ("PATH"); /* clear hash table */
1256 set_var_read_only ("PATH");
1257 set_var_read_only ("SHELL");
1258 set_var_read_only ("ENV");
1259 set_var_read_only ("BASH_ENV");
1262 return (restricted
);
1264 #endif /* RESTRICTED_SHELL */
1266 /* Fetch the current set of uids and gids and return 1 if we're running
1267 setuid or setgid. */
1274 if (current_user
.uid
!= u
)
1276 FREE (current_user
.user_name
);
1277 FREE (current_user
.shell
);
1278 FREE (current_user
.home_dir
);
1279 current_user
.user_name
= current_user
.shell
= current_user
.home_dir
= (char *)NULL
;
1281 current_user
.uid
= u
;
1282 current_user
.gid
= getgid ();
1283 current_user
.euid
= geteuid ();
1284 current_user
.egid
= getegid ();
1286 /* See whether or not we are running setuid or setgid. */
1287 return (current_user
.uid
!= current_user
.euid
) ||
1288 (current_user
.gid
!= current_user
.egid
);
1292 disable_priv_mode ()
1296 if (setuid (current_user
.uid
) < 0)
1299 sys_error (_("cannot set uid to %d: effective uid %d"), current_user
.uid
, current_user
.euid
);
1300 #if defined (EXIT_ON_SETUID_FAILURE)
1305 if (setgid (current_user
.gid
) < 0)
1306 sys_error (_("cannot set gid to %d: effective gid %d"), current_user
.gid
, current_user
.egid
);
1308 current_user
.euid
= current_user
.uid
;
1309 current_user
.egid
= current_user
.gid
;
1312 #if defined (WORDEXP_OPTION)
1318 WORD_LIST
*wl
, *tl
, *result
;
1320 code
= setjmp_nosigs (top_level
);
1322 if (code
!= NOT_JUMPED
)
1326 /* Some kind of throw to top_level has occurred. */
1328 return last_command_exit_value
= 127;
1331 return last_command_exit_value
;
1333 return last_command_exit_value
= 1;
1335 command_error ("run_wordexp", CMDERR_BADJUMP
, code
, 0);
1339 /* Run it through the parser to get a list of words and expand them */
1340 if (words
&& *words
)
1342 with_input_from_string (words
, "--wordexp");
1343 if (parse_command () != 0)
1345 if (global_command
== 0)
1350 if (global_command
->type
!= cm_simple
)
1352 wl
= global_command
->value
.Simple
->words
;
1354 for (tl
= wl
; tl
; tl
= tl
->next
)
1355 tl
->word
->flags
|= W_NOCOMSUB
|W_NOPROCSUB
;
1356 result
= wl
? expand_words_no_vars (wl
) : (WORD_LIST
*)0;
1359 result
= (WORD_LIST
*)0;
1361 last_command_exit_value
= 0;
1369 /* Count up the number of words and bytes, and print them. Don't count
1370 the trailing NUL byte. */
1371 for (nw
= nb
= 0, wl
= result
; wl
; wl
= wl
->next
)
1374 nb
+= strlen (wl
->word
->word
);
1376 printf ("%u\n%u\n", nw
, nb
);
1377 /* Print each word on a separate line. This will have to be changed when
1378 the interface to glibc is completed. */
1379 for (wl
= result
; wl
; wl
= wl
->next
)
1380 printf ("%s\n", wl
->word
->word
);
1386 #if defined (ONESHOT)
1387 /* Run one command, given as the argument to the -c option. Tell
1388 parse_and_execute not to fork for a simple command. */
1390 run_one_command (command
)
1395 code
= setjmp_nosigs (top_level
);
1397 if (code
!= NOT_JUMPED
)
1399 #if defined (PROCESS_SUBSTITUTION)
1400 unlink_fifo_list ();
1401 #endif /* PROCESS_SUBSTITUTION */
1404 /* Some kind of throw to top_level has occurred. */
1406 return last_command_exit_value
= 127;
1409 return last_command_exit_value
;
1411 return last_command_exit_value
= 1;
1413 command_error ("run_one_command", CMDERR_BADJUMP
, code
, 0);
1416 return (parse_and_execute (savestring (command
), "-c", SEVAL_NOHIST
));
1418 #endif /* ONESHOT */
1421 bind_args (argv
, arg_start
, arg_end
, start_index
)
1423 int arg_start
, arg_end
, start_index
;
1426 WORD_LIST
*args
, *tl
;
1428 for (i
= arg_start
, args
= tl
= (WORD_LIST
*)NULL
; i
< arg_end
; i
++)
1431 args
= tl
= make_word_list (make_word (argv
[i
]), args
);
1434 tl
->next
= make_word_list (make_word (argv
[i
]), (WORD_LIST
*)NULL
);
1441 if (start_index
== 0) /* bind to $0...$n for sh -c command */
1443 /* Posix.2 4.56.3 says that the first argument after sh -c command
1444 becomes $0, and the rest of the arguments become $1...$n */
1445 shell_name
= savestring (args
->word
->word
);
1446 FREE (dollar_vars
[0]);
1447 dollar_vars
[0] = savestring (args
->word
->word
);
1448 remember_args (args
->next
, 1);
1451 push_args (args
->next
); /* BASH_ARGV and BASH_ARGC */
1452 bash_argv_initialized
= 1;
1455 else /* bind to $1...$n for shell script */
1457 remember_args (args
, 1);
1458 /* We do this unconditionally so something like -O extdebug doesn't
1459 do it first. We're setting the definitive positional params
1463 push_args (args
); /* BASH_ARGV and BASH_ARGC */
1464 bash_argv_initialized
= 1;
1468 dispose_words (args
);
1477 remember_args ((WORD_LIST
*)NULL
, 1);
1478 pop_args (); /* Reset BASH_ARGV and BASH_ARGC */
1484 #if defined (DEBUGGER) && defined (DEBUGGER_START_FILE)
1488 old_errexit
= exit_immediately_on_error
;
1489 exit_immediately_on_error
= 0;
1491 r
= force_execute_file (DEBUGGER_START_FILE
, 1);
1494 internal_warning (_("cannot start debugger; debugging mode disabled"));
1497 error_trace_mode
= function_trace_mode
= debugging_mode
;
1502 exit_immediately_on_error
+= old_errexit
;
1507 open_shell_script (script_name
)
1510 int fd
, e
, fd_is_tty
;
1511 char *filename
, *path_filename
, *t
;
1515 #if defined (ARRAY_VARS)
1516 SHELL_VAR
*funcname_v
, *bash_source_v
, *bash_lineno_v
;
1517 ARRAY
*funcname_a
, *bash_source_a
, *bash_lineno_a
;
1520 filename
= savestring (script_name
);
1522 fd
= open (filename
, O_RDONLY
);
1523 if ((fd
< 0) && (errno
== ENOENT
) && (absolute_program (filename
) == 0))
1526 /* If it's not in the current directory, try looking through PATH
1528 path_filename
= find_path_file (script_name
);
1532 filename
= path_filename
;
1533 fd
= open (filename
, O_RDONLY
);
1542 file_error (filename
);
1543 #if defined (JOB_CONTROL)
1544 end_job_control (); /* just in case we were run as bash -i script */
1546 sh_exit ((e
== ENOENT
) ? EX_NOTFOUND
: EX_NOINPUT
);
1549 free (dollar_vars
[0]);
1550 dollar_vars
[0] = exec_argv0
? savestring (exec_argv0
) : savestring (script_name
);
1554 exec_argv0
= (char *)NULL
;
1557 if (file_isdir (filename
))
1559 #if defined (EISDIR)
1564 file_error (filename
);
1565 #if defined (JOB_CONTROL)
1566 end_job_control (); /* just in case we were run as bash -i script */
1568 sh_exit (EX_NOINPUT
);
1571 #if defined (ARRAY_VARS)
1572 GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v
, funcname_a
);
1573 GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v
, bash_source_a
);
1574 GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v
, bash_lineno_a
);
1576 array_push (bash_source_a
, filename
);
1579 t
= itos (executing_line_number ());
1580 array_push (bash_lineno_a
, t
);
1583 array_push (funcname_a
, "main");
1587 fd_is_tty
= isatty (fd
);
1592 /* Only do this with non-tty file descriptors we can seek on. */
1593 if (fd_is_tty
== 0 && (lseek (fd
, 0L, 1) != -1))
1595 /* Check to see if the `file' in `bash file' is a binary file
1596 according to the same tests done by execute_simple_command (),
1597 and report an error and exit if it is. */
1598 sample_len
= read (fd
, sample
, sizeof (sample
));
1602 if ((fstat (fd
, &sb
) == 0) && S_ISDIR (sb
.st_mode
))
1604 #if defined (EISDIR)
1606 file_error (filename
);
1608 internal_error (_("%s: Is a directory"), filename
);
1614 file_error (filename
);
1616 #if defined (JOB_CONTROL)
1617 end_job_control (); /* just in case we were run as bash -i script */
1621 else if (sample_len
> 0 && (check_binary_file (sample
, sample_len
)))
1623 internal_error (_("%s: cannot execute binary file"), filename
);
1624 #if defined (JOB_CONTROL)
1625 end_job_control (); /* just in case we were run as bash -i script */
1627 exit (EX_BINARY_FILE
);
1629 /* Now rewind the file back to the beginning. */
1633 /* Open the script. But try to move the file descriptor to a randomly
1634 large one, in the hopes that any descriptors used by the script will
1635 not match with ours. */
1636 fd
= move_to_high_fd (fd
, 1, -1);
1638 #if defined (BUFFERED_INPUT)
1639 default_buffered_input
= fd
;
1640 SET_CLOSE_ON_EXEC (default_buffered_input
);
1641 #else /* !BUFFERED_INPUT */
1642 default_input
= fdopen (fd
, "r");
1644 if (default_input
== 0)
1646 file_error (filename
);
1650 SET_CLOSE_ON_EXEC (fd
);
1651 if (fileno (default_input
) != fd
)
1652 SET_CLOSE_ON_EXEC (fileno (default_input
));
1653 #endif /* !BUFFERED_INPUT */
1655 /* Just about the only way for this code to be executed is if something
1656 like `bash -i /dev/stdin' is executed. */
1657 if (interactive_shell
&& fd_is_tty
)
1662 #if defined (BUFFERED_INPUT)
1663 default_buffered_input
= 0;
1665 fclose (default_input
);
1666 default_input
= stdin
;
1669 else if (forced_interactive
&& fd_is_tty
== 0)
1670 /* But if a script is called with something like `bash -i scriptname',
1671 we need to do a non-interactive setup here, since we didn't do it
1673 init_interactive_script ();
1677 reading_shell_script
= 1;
1681 /* Initialize the input routines for the parser. */
1685 /* Make sure the fd from which we are reading input is not in
1687 #if defined (BUFFERED_INPUT)
1688 if (interactive
== 0)
1689 sh_unset_nodelay_mode (default_buffered_input
);
1691 #endif /* !BUFFERED_INPUT */
1692 sh_unset_nodelay_mode (fileno (stdin
));
1694 /* with_input_from_stdin really means `with_input_from_readline' */
1695 if (interactive
&& no_line_editing
== 0)
1696 with_input_from_stdin ();
1697 #if defined (BUFFERED_INPUT)
1698 else if (interactive
== 0)
1699 with_input_from_buffered_stream (default_buffered_input
, dollar_vars
[0]);
1700 #endif /* BUFFERED_INPUT */
1702 with_input_from_stream (default_input
, dollar_vars
[0]);
1705 /* Close the current shell script input source and forget about it. This is
1706 extern so execute_cmd.c:initialize_subshell() can call it. If CHECK_ZERO
1707 is non-zero, we close default_buffered_input even if it's the standard
1710 unset_bash_input (check_zero
)
1713 #if defined (BUFFERED_INPUT)
1714 if ((check_zero
&& default_buffered_input
>= 0) ||
1715 (check_zero
== 0 && default_buffered_input
> 0))
1717 close_buffered_fd (default_buffered_input
);
1718 default_buffered_input
= bash_input
.location
.buffered_fd
= -1;
1719 bash_input
.type
= st_none
; /* XXX */
1721 #else /* !BUFFERED_INPUT */
1724 fclose (default_input
);
1725 default_input
= (FILE *)NULL
;
1727 #endif /* !BUFFERED_INPUT */
1731 #if !defined (PROGRAM)
1732 # define PROGRAM "bash"
1736 set_shell_name (argv0
)
1739 /* Here's a hack. If the name of this shell is "sh", then don't do
1740 any startup files; just try to be more like /bin/sh. */
1741 shell_name
= argv0
? base_pathname (argv0
) : PROGRAM
;
1743 if (argv0
&& *argv0
== '-')
1745 if (*shell_name
== '-')
1750 if (shell_name
[0] == 's' && shell_name
[1] == 'h' && shell_name
[2] == '\0')
1752 if (shell_name
[0] == 's' && shell_name
[1] == 'u' && shell_name
[2] == '\0')
1755 shell_name
= argv0
? argv0
: PROGRAM
;
1756 FREE (dollar_vars
[0]);
1757 dollar_vars
[0] = savestring (shell_name
);
1759 /* A program may start an interactive shell with
1760 "execl ("/bin/bash", "-", NULL)".
1761 If so, default the name of this shell to our name. */
1762 if (!shell_name
|| !*shell_name
|| (shell_name
[0] == '-' && !shell_name
[1]))
1763 shell_name
= PROGRAM
;
1769 expand_aliases
= interactive_shell
= startup_state
= 1;
1771 #if defined (HISTORY)
1772 remember_on_history
= enable_history_list
= 1; /* XXX */
1773 histexp_flag
= history_expansion
; /* XXX */
1778 init_noninteractive ()
1780 #if defined (HISTORY)
1781 bash_history_reinit (0);
1782 #endif /* HISTORY */
1783 interactive_shell
= startup_state
= interactive
= 0;
1784 expand_aliases
= posixly_correct
; /* XXX - was 0 not posixly_correct */
1785 no_line_editing
= 1;
1786 #if defined (JOB_CONTROL)
1787 /* Even if the shell is not interactive, enable job control if the -i or
1788 -m option is supplied at startup. */
1789 set_job_control (forced_interactive
||jobs_m_flag
);
1790 #endif /* JOB_CONTROL */
1794 init_interactive_script ()
1796 init_noninteractive ();
1797 expand_aliases
= interactive_shell
= startup_state
= 1;
1798 #if defined (HISTORY)
1799 remember_on_history
= enable_history_list
= 1; /* XXX */
1804 get_current_user_info ()
1806 struct passwd
*entry
;
1808 /* Don't fetch this more than once. */
1809 if (current_user
.user_name
== 0)
1811 #if defined (__TANDEM)
1812 entry
= getpwnam (getlogin ());
1814 entry
= getpwuid (current_user
.uid
);
1818 current_user
.user_name
= savestring (entry
->pw_name
);
1819 current_user
.shell
= (entry
->pw_shell
&& entry
->pw_shell
[0])
1820 ? savestring (entry
->pw_shell
)
1821 : savestring ("/bin/sh");
1822 current_user
.home_dir
= savestring (entry
->pw_dir
);
1826 current_user
.user_name
= _("I have no name!");
1827 current_user
.user_name
= savestring (current_user
.user_name
);
1828 current_user
.shell
= savestring ("/bin/sh");
1829 current_user
.home_dir
= savestring ("/");
1831 #if defined (HAVE_GETPWENT)
1837 /* Do whatever is necessary to initialize the shell.
1838 Put new initializations in here. */
1843 int should_be_restricted
;
1845 /* Line buffer output for stderr and stdout. */
1846 if (shell_initialized
== 0)
1848 sh_setlinebuf (stderr
);
1849 sh_setlinebuf (stdout
);
1852 /* Sort the array of shell builtins so that the binary search in
1853 find_shell_builtin () works correctly. */
1854 initialize_shell_builtins ();
1856 /* Initialize the trap signal handlers before installing our own
1857 signal handlers. traps.c:restore_original_signals () is responsible
1858 for restoring the original default signal handlers. That function
1859 is called when we make a new child. */
1860 initialize_traps ();
1861 initialize_signals (0);
1863 /* It's highly unlikely that this will change. */
1864 if (current_host_name
== 0)
1866 /* Initialize current_host_name. */
1867 if (gethostname (hostname
, 255) < 0)
1868 current_host_name
= "??host??";
1870 current_host_name
= savestring (hostname
);
1873 /* Initialize the stuff in current_user that comes from the password
1874 file. We don't need to do this right away if the shell is not
1876 if (interactive_shell
)
1877 get_current_user_info ();
1879 /* Initialize our interface to the tilde expander. */
1880 tilde_initialize ();
1882 #if defined (RESTRICTED_SHELL)
1883 should_be_restricted
= shell_is_restricted (shell_name
);
1886 /* Initialize internal and environment variables. Don't import shell
1887 functions from the environment if we are running in privileged or
1888 restricted mode or if the shell is running setuid. */
1889 #if defined (RESTRICTED_SHELL)
1890 initialize_shell_variables (shell_environment
, privileged_mode
||restricted
||should_be_restricted
||running_setuid
);
1892 initialize_shell_variables (shell_environment
, privileged_mode
||running_setuid
);
1895 /* Initialize the data structures for storing and running jobs. */
1896 initialize_job_control (jobs_m_flag
);
1898 /* Initialize input streams to null. */
1899 initialize_bash_input ();
1901 initialize_flags ();
1903 /* Initialize the shell options. Don't import the shell options
1904 from the environment variables $SHELLOPTS or $BASHOPTS if we are
1905 running in privileged or restricted mode or if the shell is running
1907 #if defined (RESTRICTED_SHELL)
1908 initialize_shell_options (privileged_mode
||restricted
||should_be_restricted
||running_setuid
);
1909 initialize_bashopts (privileged_mode
||restricted
||should_be_restricted
||running_setuid
);
1911 initialize_shell_options (privileged_mode
||running_setuid
);
1912 initialize_bashopts (privileged_mode
||running_setuid
);
1916 /* Function called by main () when it appears that the shell has already
1917 had some initialization performed. This is supposed to reset the world
1918 back to a pristine state, as if we had been exec'ed. */
1920 shell_reinitialize ()
1922 /* The default shell prompts. */
1923 primary_prompt
= PPROMPT
;
1924 secondary_prompt
= SPROMPT
;
1926 /* Things that get 1. */
1927 current_command_number
= 1;
1929 /* We have decided that the ~/.bashrc file should not be executed
1930 for the invocation of each shell script. If the variable $ENV
1931 (or $BASH_ENV) is set, its value is used as the name of a file
1933 no_rc
= no_profile
= 1;
1935 /* Things that get 0. */
1936 login_shell
= make_login_shell
= interactive
= executing
= 0;
1937 debugging
= do_version
= line_number
= last_command_exit_value
= 0;
1938 forced_interactive
= interactive_shell
= 0;
1939 subshell_environment
= running_in_background
= 0;
1941 bash_argv_initialized
= 0;
1943 /* XXX - should we set jobs_m_flag to 0 here? */
1945 #if defined (HISTORY)
1946 bash_history_reinit (enable_history_list
= 0);
1947 #endif /* HISTORY */
1949 #if defined (RESTRICTED_SHELL)
1951 #endif /* RESTRICTED_SHELL */
1953 /* Ensure that the default startup file is used. (Except that we don't
1954 execute this file for reinitialized shells). */
1955 bashrc_file
= DEFAULT_BASHRC
;
1957 /* Delete all variables and functions. They will be reinitialized when
1958 the environment is parsed. */
1959 delete_all_contexts (shell_variables
);
1960 delete_all_variables (shell_functions
);
1962 reinit_special_variables ();
1964 #if defined (READLINE)
1965 bashline_reinitialize ();
1968 shell_reinitialized
= 1;
1972 show_shell_usage (fp
, extra
)
1977 char *set_opts
, *s
, *t
;
1980 fprintf (fp
, _("GNU bash, version %s-(%s)\n"), shell_version_string (), MACHTYPE
);
1981 fprintf (fp
, _("Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n"),
1982 shell_name
, shell_name
);
1983 fputs (_("GNU long options:\n"), fp
);
1984 for (i
= 0; long_args
[i
].name
; i
++)
1985 fprintf (fp
, "\t--%s\n", long_args
[i
].name
);
1987 fputs (_("Shell options:\n"), fp
);
1988 fputs (_("\t-ilrsD or -c command or -O shopt_option\t\t(invocation only)\n"), fp
);
1990 for (i
= 0, set_opts
= 0; shell_builtins
[i
].name
; i
++)
1991 if (STREQ (shell_builtins
[i
].name
, "set"))
1993 set_opts
= savestring (shell_builtins
[i
].short_doc
);
1999 s
= strchr (set_opts
, '[');
2004 t
= strchr (s
, ']');
2007 fprintf (fp
, _("\t-%s or -o option\n"), s
);
2013 fprintf (fp
, _("Type `%s -c \"help set\"' for more information about shell options.\n"), shell_name
);
2014 fprintf (fp
, _("Type `%s -c help' for more information about shell builtin commands.\n"), shell_name
);
2015 fprintf (fp
, _("Use the `bashbug' command to report bugs.\n"));
2017 fprintf (fp
, _("bash home page: <http://www.gnu.org/software/bash>\n"));
2018 fprintf (fp
, _("General help using GNU software: <http://www.gnu.org/gethelp/>\n"));
2023 add_shopt_to_alist (opt
, on_or_off
)
2027 if (shopt_ind
>= shopt_len
)
2030 shopt_alist
= (STRING_INT_ALIST
*)xrealloc (shopt_alist
, shopt_len
* sizeof (shopt_alist
[0]));
2032 shopt_alist
[shopt_ind
].word
= opt
;
2033 shopt_alist
[shopt_ind
].token
= on_or_off
;
2042 for (i
= 0; i
< shopt_ind
; i
++)
2043 if (shopt_setopt (shopt_alist
[i
].word
, (shopt_alist
[i
].token
== '-')) != EXECUTION_SUCCESS
)
2047 shopt_ind
= shopt_len
= 0;