1 /* shell.c -- GNU's idea of the POSIX shell specification. */
3 /* Copyright (C) 1987-2002 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 it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bash is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
22 Sunday, January 10th, 1988.
23 Initial author: Brian Fox
25 #define INSTALL_DEBUG_MODE
29 #include "bashtypes.h"
31 # include <sys/file.h>
33 #include "posixstat.h"
34 #include "posixtime.h"
42 #if defined (HAVE_UNISTD_H)
46 #define NEED_SH_SETLINEBUF_DECL /* used in externs.h */
51 #include "mailcheck.h"
53 #include "builtins/common.h"
55 #if defined (JOB_CONTROL)
57 #endif /* JOB_CONTROL */
60 #include "execute_cmd.h"
63 #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
64 # include <malloc/shmalloc.h>
68 # include "bashhist.h"
69 # include <readline/history.h>
72 #include <tilde/tilde.h>
73 #include <glob/strmatch.h>
75 #if defined (__OPENNT)
76 # include <opennt/opennt.h>
79 #if !defined (HAVE_GETPW_DECLS)
80 extern struct passwd
*getpwuid ();
81 #endif /* !HAVE_GETPW_DECLS */
87 #if defined (NO_MAIN_ENV_ARG)
88 extern char **environ
; /* used if no third argument to main() */
91 extern char *dist_version
, *release_status
;
92 extern int patch_level
, build_version
;
93 extern int shell_level
;
94 extern int subshell_environment
;
95 extern int last_command_exit_value
;
96 extern int line_number
;
97 extern char *primary_prompt
, *secondary_prompt
;
98 extern int expand_aliases
;
99 extern char *this_command_name
;
100 extern int array_needs_making
;
102 /* Non-zero means that this shell has already been run; i.e. you should
103 call shell_reinitialize () if you need to start afresh. */
104 int shell_initialized
= 0;
106 COMMAND
*global_command
= (COMMAND
*)NULL
;
108 /* Information about the current user. */
109 struct user_info current_user
=
111 (uid_t
)-1, (uid_t
)-1, (gid_t
)-1, (gid_t
)-1,
112 (char *)NULL
, (char *)NULL
, (char *)NULL
115 /* The current host's name. */
116 char *current_host_name
= (char *)NULL
;
118 /* Non-zero means that this shell is a login shell.
121 1 = login shell from getty (or equivalent fake out)
122 -1 = login shell from "--login" (or -l) flag.
123 -2 = both from getty, and from flag.
127 /* Non-zero means that at this moment, the shell is interactive. In
128 general, this means that the shell is at this moment reading input
129 from the keyboard. */
132 /* Non-zero means that the shell was started as an interactive shell. */
133 int interactive_shell
= 0;
135 /* Non-zero means to send a SIGHUP to all jobs when an interactive login
139 /* Tells what state the shell was in when it started:
140 0 = non-interactive shell script
143 3 = wordexp evaluation
144 This is a superset of the information provided by interactive_shell.
146 int startup_state
= 0;
148 /* Special debugging helper. */
149 int debugging_login_shell
= 0;
151 /* The environment that the shell passes to other commands. */
152 char **shell_environment
;
154 /* Non-zero when we are executing a top-level command. */
157 /* The number of commands executed so far. */
158 int current_command_number
= 1;
160 /* Non-zero is the recursion depth for commands. */
161 int indirection_level
= 0;
163 /* The name of this shell, as taken from argv[0]. */
164 char *shell_name
= (char *)NULL
;
166 /* time in seconds when the shell was started */
167 time_t shell_start_time
;
169 /* Are we running in an emacs shell window? */
170 int running_under_emacs
;
172 /* The name of the .(shell)rc file. */
173 static char *bashrc_file
= "~/.bashrc";
175 /* Non-zero means to act more like the Bourne shell on startup. */
176 static int act_like_sh
;
178 /* Non-zero if this shell is being run by `su'. */
181 /* Non-zero if we have already expanded and sourced $ENV. */
182 static int sourced_env
;
184 /* Is this shell running setuid? */
185 static int running_setuid
;
187 /* Values for the long-winded argument names. */
188 static int debugging
; /* Do debugging things. */
189 static int no_rc
; /* Don't execute ~/.bashrc */
190 static int no_profile
; /* Don't execute .profile */
191 static int do_version
; /* Display interesting version info. */
192 static int make_login_shell
; /* Make this shell be a `-bash' shell. */
193 static int want_initial_help
; /* --help option */
195 int no_line_editing
= 0; /* Don't do fancy line editing. */
196 int posixly_correct
= 0; /* Non-zero means posix.2 superset. */
197 int dump_translatable_strings
; /* Dump strings in $"...", don't execute. */
198 int dump_po_strings
; /* Dump strings in $"..." in po format */
199 int wordexp_only
= 0; /* Do word expansion only */
201 /* Some long-winded argument names. These are obviously new. */
210 { "debug", Int
, &debugging
, (char **)0x0 },
211 { "dump-po-strings", Int
, &dump_po_strings
, (char **)0x0 },
212 { "dump-strings", Int
, &dump_translatable_strings
, (char **)0x0 },
213 { "help", Int
, &want_initial_help
, (char **)0x0 },
214 { "init-file", Charp
, (int *)0x0, &bashrc_file
},
215 { "login", Int
, &make_login_shell
, (char **)0x0 },
216 { "noediting", Int
, &no_line_editing
, (char **)0x0 },
217 { "noprofile", Int
, &no_profile
, (char **)0x0 },
218 { "norc", Int
, &no_rc
, (char **)0x0 },
219 { "posix", Int
, &posixly_correct
, (char **)0x0 },
220 { "rcfile", Charp
, (int *)0x0, &bashrc_file
},
221 #if defined (RESTRICTED_SHELL)
222 { "restricted", Int
, &restricted
, (char **)0x0 },
224 { "verbose", Int
, &echo_input_at_read
, (char **)0x0 },
225 { "version", Int
, &do_version
, (char **)0x0 },
226 { "wordexp", Int
, &wordexp_only
, (char **)0x0 },
227 { (char *)0x0, Int
, (int *)0x0, (char **)0x0 }
230 /* These are extern so execute_simple_command can set them, and then
231 longjmp back to main to execute a shell script, instead of calling
232 main () again and resulting in indefinite, possibly fatal, stack
234 procenv_t subshell_top_level
;
236 char **subshell_argv
;
237 char **subshell_envp
;
239 #if defined (BUFFERED_INPUT)
240 /* The file descriptor from which the shell is reading input. */
241 int default_buffered_input
= -1;
244 /* The following two variables are not static so they can show up in $-. */
245 int read_from_stdin
; /* -s flag supplied */
246 int want_pending_command
; /* -c flag supplied */
248 int malloc_trace_at_exit
= 0;
250 static int shell_reinitialized
= 0;
251 static char *local_pending_command
;
253 static FILE *default_input
;
255 static STRING_INT_ALIST
*shopt_alist
;
256 static int shopt_ind
= 0, shopt_len
= 0;
258 static int parse_long_options
__P((char **, int, int));
259 static int parse_shell_options
__P((char **, int, int));
260 static int bind_args
__P((char **, int, int, int));
262 static void add_shopt_to_alist
__P((char *, int));
263 static void run_shopt_alist
__P((void));
265 static void execute_env_file
__P((char *));
266 static void run_startup_files
__P((void));
267 static int open_shell_script
__P((char *));
268 static void set_bash_input
__P((void));
269 static int run_one_command
__P((char *));
270 static int run_wordexp
__P((char *));
272 static int uidget
__P((void));
274 static void init_interactive
__P((void));
275 static void init_noninteractive
__P((void));
277 static void set_shell_name
__P((char *));
278 static void shell_initialize
__P((void));
279 static void shell_reinitialize
__P((void));
281 static void show_shell_usage
__P((FILE *, int));
285 _cygwin32_check_tmp ()
289 if (stat ("/tmp", &sb
) < 0)
290 internal_warning ("could not find /tmp, please create!");
293 if (S_ISDIR (sb
.st_mode
) == 0)
294 internal_warning ("/tmp must be a valid directory name");
297 #endif /* __CYGWIN__ */
299 #if defined (NO_MAIN_ENV_ARG)
300 /* systems without third argument to main() */
305 #else /* !NO_MAIN_ENV_ARG */
307 main (argc
, argv
, env
)
310 #endif /* !NO_MAIN_ENV_ARG */
313 int code
, old_errexit_flag
;
314 #if defined (RESTRICTED_SHELL)
317 volatile int locally_skip_execution
;
318 volatile int arg_index
, top_level_arg_index
;
323 #endif /* __OPENNT */
329 USE_VAR(old_errexit_flag
);
330 #if defined (RESTRICTED_SHELL)
334 /* Catch early SIGINTs. */
335 code
= setjmp (top_level
);
339 #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
341 malloc_set_register (1);
348 _cygwin32_check_tmp ();
349 #endif /* __CYGWIN__ */
351 /* Wait forever if we are debugging a login shell. */
352 while (debugging_login_shell
);
354 set_default_locale ();
356 running_setuid
= uidget ();
358 if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC"))
361 #if defined (USE_GNU_MALLOC_LIBRARY)
362 mcheck (programming_error
, (void (*) ())0);
363 #endif /* USE_GNU_MALLOC_LIBRARY */
365 if (setjmp (subshell_top_level
))
367 argc
= subshell_argc
;
368 argv
= subshell_argv
;
373 shell_reinitialized
= 0;
375 /* Initialize `local' variables for all `invocations' of main (). */
377 local_pending_command
= (char *)NULL
;
378 want_pending_command
= locally_skip_execution
= read_from_stdin
= 0;
379 default_input
= stdin
;
380 #if defined (BUFFERED_INPUT)
381 default_buffered_input
= -1;
384 /* Fix for the `infinite process creation' bug when running shell scripts
385 from startup files on System V. */
386 login_shell
= make_login_shell
= 0;
388 /* If this shell has already been run, then reinitialize it to a
390 if (shell_initialized
|| shell_name
)
392 /* Make sure that we do not infinitely recurse as a login shell. */
393 if (*shell_name
== '-')
396 shell_reinitialize ();
397 if (setjmp (top_level
))
401 shell_environment
= env
;
402 set_shell_name (argv
[0]);
403 shell_start_time
= NOW
; /* NOW now defined in general.h */
405 /* Parse argument flags from the input line. */
407 /* Find full word arguments first. */
408 arg_index
= parse_long_options (argv
, arg_index
, argc
);
410 if (want_initial_help
)
412 show_shell_usage (stdout
, 1);
413 exit (EXECUTION_SUCCESS
);
418 show_shell_version (1);
419 exit (EXECUTION_SUCCESS
);
422 /* All done with full word options; do standard shell option parsing.*/
423 this_command_name
= shell_name
; /* for error reporting */
424 arg_index
= parse_shell_options (argv
, arg_index
, argc
);
426 /* If user supplied the "--login" (or -l) flag, then set and invert
428 if (make_login_shell
)
431 login_shell
= -login_shell
;
434 set_login_shell (login_shell
!= 0);
437 dump_translatable_strings
= 1;
439 if (dump_translatable_strings
)
440 read_but_dont_execute
= 1;
442 if (running_setuid
&& privileged_mode
== 0)
443 disable_priv_mode ();
445 /* Need to get the argument to a -c option processed in the
446 above loop. The next arg is a command to execute, and the
447 following args are $0...$n respectively. */
448 if (want_pending_command
)
450 local_pending_command
= argv
[arg_index
];
451 if (local_pending_command
== 0)
453 report_error ("-c: option requires an argument");
458 this_command_name
= (char *)NULL
;
460 cmd_init(); /* initialize the command object caches */
462 /* First, let the outside world know about our interactive status.
463 A shell is interactive if the `-i' flag was given, or if all of
464 the following conditions are met:
466 no arguments remaining or the -s flag given
467 standard input is a terminal
468 standard output is a terminal
469 Refer to Posix.2, the description of the `sh' utility. */
471 if (forced_interactive
|| /* -i flag */
472 (!local_pending_command
&& /* No -c command and ... */
473 wordexp_only
== 0 && /* No --wordexp and ... */
474 ((arg_index
== argc
) || /* no remaining args or... */
475 read_from_stdin
) && /* -s flag with args, and */
476 isatty (fileno (stdin
)) && /* Input is a terminal and */
477 isatty (fileno (stdout
)))) /* output is a terminal. */
480 init_noninteractive ();
482 #define CLOSE_FDS_AT_LOGIN
483 #if defined (CLOSE_FDS_AT_LOGIN)
485 * Some systems have the bad habit of starting login shells with lots of open
486 * file descriptors. For instance, most systems that have picked up the
487 * pre-4.0 Sun YP code leave a file descriptor open each time you call one
488 * of the getpw* functions, and it's set to be open across execs. That
489 * means one for login, one for xterm, one for shelltool, etc.
491 if (login_shell
&& interactive_shell
)
493 for (i
= 3; i
< 20; i
++)
496 #endif /* CLOSE_FDS_AT_LOGIN */
498 /* If we're in a strict Posix.2 mode, turn on interactive comments,
499 alias expansion in non-interactive shells, and other Posix.2 things. */
502 bind_variable ("POSIXLY_CORRECT", "y");
503 sv_strict_posix ("POSIXLY_CORRECT");
506 /* Now we run the shopt_alist and process the options. */
510 /* From here on in, the shell must be a normal functioning shell.
511 Variables from the environment are expected to be set, etc. */
514 set_default_locale_vars ();
516 if (interactive_shell
)
520 term
= getenv ("TERM");
521 no_line_editing
|= term
&& (STREQ (term
, "emacs"));
522 term
= getenv ("EMACS");
523 running_under_emacs
= term
? ((strmatch ("*term*", term
, 0) == 0) ? 2 : 1)
525 no_line_editing
|= term
&& term
[0] == 't' && term
[1] == '\0';
528 top_level_arg_index
= arg_index
;
529 old_errexit_flag
= exit_immediately_on_error
;
531 /* Give this shell a place to longjmp to before executing the
532 startup files. This allows users to press C-c to abort the
534 code
= setjmp (top_level
);
537 if (code
== EXITPROG
)
538 exit_shell (last_command_exit_value
);
541 #if defined (JOB_CONTROL)
542 /* Reset job control, since run_startup_files turned it off. */
543 set_job_control (interactive_shell
);
545 /* Reset value of `set -e', since it's turned off before running
546 the startup files. */
547 exit_immediately_on_error
+= old_errexit_flag
;
548 locally_skip_execution
++;
552 arg_index
= top_level_arg_index
;
554 /* Execute the start-up scripts. */
556 if (interactive_shell
== 0)
558 unbind_variable ("PS1");
559 unbind_variable ("PS2");
562 /* This has already been done by init_noninteractive */
563 expand_aliases
= posixly_correct
;
568 change_flag ('i', FLAG_ON
);
572 #if defined (RESTRICTED_SHELL)
573 /* Set restricted_shell based on whether the basename of $0 indicates that
574 the shell should be restricted or if the `-r' option was supplied at
576 restricted_shell
= shell_is_restricted (shell_name
);
578 /* If the `-r' option is supplied at invocation, make sure that the shell
579 is not in restricted mode when running the startup files. */
580 saverst
= restricted
;
584 /* The startup files are run with `set -e' temporarily disabled. */
585 if (locally_skip_execution
== 0 && running_setuid
== 0)
587 old_errexit_flag
= exit_immediately_on_error
;
588 exit_immediately_on_error
= 0;
590 run_startup_files ();
591 exit_immediately_on_error
+= old_errexit_flag
;
594 /* If we are invoked as `sh', turn on Posix mode. */
597 bind_variable ("POSIXLY_CORRECT", "y");
598 sv_strict_posix ("POSIXLY_CORRECT");
601 #if defined (RESTRICTED_SHELL)
602 /* Turn on the restrictions after executing the startup files. This
603 means that `bash -r' or `set -r' invoked from a startup file will
604 turn on the restrictions after the startup files are executed. */
605 restricted
= saverst
|| restricted
;
606 if (shell_reinitialized
== 0)
607 maybe_make_restricted (shell_name
);
608 #endif /* RESTRICTED_SHELL */
613 last_command_exit_value
= run_wordexp (argv
[arg_index
]);
614 exit_shell (last_command_exit_value
);
617 if (local_pending_command
)
619 arg_index
= bind_args (argv
, arg_index
, argc
, 0);
621 #if defined (ONESHOT)
623 run_one_command (local_pending_command
);
624 exit_shell (last_command_exit_value
);
626 with_input_from_string (local_pending_command
, "-c");
627 goto read_and_execute
;
628 #endif /* !ONESHOT */
631 /* Get possible input filename and set up default_buffered_input or
632 default_input as appropriate. */
633 if (arg_index
!= argc
&& read_from_stdin
== 0)
635 open_shell_script (argv
[arg_index
]);
638 else if (interactive
== 0)
639 /* In this mode, bash is reading a script from stdin, which is a
640 pipe or redirected file. */
641 #if defined (BUFFERED_INPUT)
642 default_buffered_input
= fileno (stdin
); /* == 0 */
644 setbuf (default_input
, (char *)NULL
);
645 #endif /* !BUFFERED_INPUT */
649 /* Bind remaining args to $1 ... $n */
650 arg_index
= bind_args (argv
, arg_index
, argc
, 1);
651 /* Do the things that should be done only for interactive shells. */
652 if (interactive_shell
)
654 /* Set up for checking for presence of mail. */
655 remember_mail_dates ();
658 #if defined (HISTORY)
659 /* Initialize the interactive history stuff. */
660 bash_initialize_history ();
661 /* Don't load the history from the history file if we've already
662 saved some lines in this session (e.g., by putting `history -s xx'
663 into one of the startup files). */
664 if (shell_initialized
== 0 && history_lines_this_session
== 0)
668 /* Initialize terminal state for interactive shells after the
669 .bash_profile and .bashrc are interpreted. */
673 #if !defined (ONESHOT)
675 #endif /* !ONESHOT */
677 shell_initialized
= 1;
679 /* Read commands until exit condition. */
681 exit_shell (last_command_exit_value
);
685 parse_long_options (argv
, arg_start
, arg_end
)
687 int arg_start
, arg_end
;
689 int arg_index
, longarg
, i
;
692 arg_index
= arg_start
;
693 while ((arg_index
!= arg_end
) && (arg_string
= argv
[arg_index
]) &&
694 (*arg_string
== '-'))
698 /* Make --login equivalent to -login. */
699 if (arg_string
[1] == '-' && arg_string
[2])
705 for (i
= 0; long_args
[i
].name
; i
++)
707 if (STREQ (arg_string
+ 1, long_args
[i
].name
))
709 if (long_args
[i
].type
== Int
)
710 *long_args
[i
].int_value
= 1;
711 else if (argv
[++arg_index
] == 0)
713 report_error ("%s: option requires an argument", long_args
[i
].name
);
717 *long_args
[i
].char_value
= argv
[arg_index
];
722 if (long_args
[i
].name
== 0)
726 report_error ("%s: invalid option", argv
[arg_index
]);
727 show_shell_usage (stderr
, 0);
730 break; /* No such argument. Maybe flag arg. */
740 parse_shell_options (argv
, arg_start
, arg_end
)
742 int arg_start
, arg_end
;
745 int arg_character
, on_or_off
, next_arg
, i
;
746 char *o_option
, *arg_string
;
748 arg_index
= arg_start
;
749 while (arg_index
!= arg_end
&& (arg_string
= argv
[arg_index
]) &&
750 (*arg_string
== '-' || *arg_string
== '+'))
752 /* There are flag arguments, so parse them. */
753 next_arg
= arg_index
+ 1;
755 /* A single `-' signals the end of options. From the 4.3 BSD sh.
756 An option `--' means the same thing; this is the standard
757 getopt(3) meaning. */
758 if (arg_string
[0] == '-' &&
759 (arg_string
[1] == '\0' ||
760 (arg_string
[1] == '-' && arg_string
[2] == '\0')))
764 on_or_off
= arg_string
[0];
765 while (arg_character
= arg_string
[i
++])
767 switch (arg_character
)
770 want_pending_command
= 1;
774 make_login_shell
= 1;
782 o_option
= argv
[next_arg
];
785 list_minus_o_opts (-1, (on_or_off
== '-') ? 0 : 1);
788 if (set_minus_o_option (on_or_off
, o_option
) != EXECUTION_SUCCESS
)
794 /* Since some of these can be overridden by the normal
795 interactive/non-interactive shell initialization or
796 initializing posix mode, we save the options and process
797 them after initialization. */
798 o_option
= argv
[next_arg
];
801 shopt_listopt (o_option
, (on_or_off
== '-') ? 0 : 1);
804 add_shopt_to_alist (o_option
, on_or_off
);
809 dump_translatable_strings
= 1;
813 if (change_flag (arg_character
, on_or_off
) == FLAG_ERROR
)
815 report_error ("%c%c: invalid option", on_or_off
, arg_character
);
816 show_shell_usage (stderr
, 0);
821 /* Can't do just a simple increment anymore -- what about
822 "bash -abouo emacs ignoreeof -hP"? */
823 arg_index
= next_arg
;
829 /* Exit the shell with status S. */
834 /* Do trap[0] if defined. Allow it to override the exit status
836 if (signal_is_trapped (0))
837 s
= run_exit_trap ();
839 #if defined (PROCESS_SUBSTITUTION)
841 #endif /* PROCESS_SUBSTITUTION */
843 #if defined (HISTORY)
844 if (interactive_shell
)
845 maybe_save_shell_history ();
848 #if defined (JOB_CONTROL)
849 /* If the user has run `shopt -s huponexit', hangup all jobs when we exit
850 an interactive login shell. ksh does this unconditionally. */
851 if (interactive_shell
&& login_shell
&& hup_on_exit
)
854 /* If this shell is interactive, terminate all stopped jobs and
855 restore the original terminal process group. Don't do this if we're
856 in a subshell and calling exit_shell after, for example, a failed
858 if (subshell_environment
== 0)
860 #endif /* JOB_CONTROL */
862 /* Always return the exit status of the last command to our parent. */
866 /* A wrapper for exit that (optionally) can do other things, like malloc
867 statistics tracing. */
872 #if defined (MALLOC_DEBUG) && defined (USING_BASH_MALLOC)
873 if (malloc_trace_at_exit
)
874 trace_malloc_stats (get_name_for_error (), (char *)NULL
);
880 /* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey
881 the Posix.2 startup file rules: $ENV is expanded, and if the file it
882 names exists, that file is sourced. The Posix.2 rules are in effect
883 for interactive shells only. (section 4.56.5.3) */
885 /* Execute ~/.bashrc for most shells. Never execute it if
886 ACT_LIKE_SH is set, or if NO_RC is set.
888 If the executable file "/usr/gnu/src/bash/foo" contains:
895 COMMAND EXECUTE BASHRC
896 --------------------------------
900 rsh machine ls YES (for rsh, which calls `bash -c')
901 rsh machine foo YES (for shell started by rsh) NO (for foo!)
908 execute_env_file (env_file
)
913 if (env_file
&& *env_file
)
915 fn
= expand_string_unsplit_to_string (env_file
, Q_DOUBLE_QUOTES
);
917 maybe_execute_file (fn
, 1);
925 #if defined (JOB_CONTROL)
928 int sourced_login
, run_by_ssh
;
930 /* get the rshd/sshd case out of the way first. */
931 if (interactive_shell
== 0 && no_rc
== 0 && login_shell
== 0 &&
932 act_like_sh
== 0 && local_pending_command
)
934 #ifdef SSH_SOURCE_BASHRC
935 run_by_ssh
= (find_variable ("SSH_CLIENT") != (SHELL_VAR
*)0) ||
936 (find_variable ("SSH2_CLIENT") != (SHELL_VAR
*)0);
941 /* If we were run by sshd or we think we were run by rshd, execute
942 ~/.bashrc if we are a top-level shell. */
943 if ((run_by_ssh
|| isnetconn (fileno (stdin
))) && shell_level
< 2)
946 # if defined (__OPENNT)
947 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
949 maybe_execute_file (SYS_BASHRC
, 1);
952 maybe_execute_file (bashrc_file
, 1);
957 #if defined (JOB_CONTROL)
958 /* Startup files should be run without job control enabled. */
959 old_job_control
= interactive_shell
? set_job_control (0) : 0;
964 /* A shell begun with the --login (or -l) flag that is not in posix mode
965 runs the login shell startup files, no matter whether or not it is
966 interactive. If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
967 startup files if argv[0][0] == '-' as well. */
968 #if defined (NON_INTERACTIVE_LOGIN_SHELLS)
969 if (login_shell
&& posixly_correct
== 0)
971 if (login_shell
< 0 && posixly_correct
== 0)
974 /* We don't execute .bashrc for login shells. */
977 /* Execute /etc/profile and one of the personal login shell
978 initialization files. */
981 maybe_execute_file (SYS_PROFILE
, 1);
983 if (act_like_sh
) /* sh */
984 maybe_execute_file ("~/.profile", 1);
985 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
986 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
987 maybe_execute_file ("~/.profile", 1);
993 /* A non-interactive shell not named `sh' and not in posix mode reads and
994 executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd'
995 and `-su' as the name of the shell, we want to read the startup files.
996 No other non-interactive shells read any startup files. */
997 if (interactive_shell
== 0 && !(su_shell
&& login_shell
))
999 if (posixly_correct
== 0 && act_like_sh
== 0 && privileged_mode
== 0 &&
1001 execute_env_file (get_string_value ("BASH_ENV"));
1005 /* Interactive shell or `-su' shell. */
1006 if (posixly_correct
== 0) /* bash, sh */
1008 if (login_shell
&& sourced_login
++ == 0)
1010 /* We don't execute .bashrc for login shells. */
1013 /* Execute /etc/profile and one of the personal login shell
1014 initialization files. */
1015 if (no_profile
== 0)
1017 maybe_execute_file (SYS_PROFILE
, 1);
1019 if (act_like_sh
) /* sh */
1020 maybe_execute_file ("~/.profile", 1);
1021 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
1022 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
1023 maybe_execute_file ("~/.profile", 1);
1028 if (act_like_sh
== 0 && no_rc
== 0)
1031 # if defined (__OPENNT)
1032 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
1034 maybe_execute_file (SYS_BASHRC
, 1);
1037 maybe_execute_file (bashrc_file
, 1);
1040 else if (act_like_sh
&& privileged_mode
== 0 && sourced_env
++ == 0)
1041 execute_env_file (get_string_value ("ENV"));
1043 else /* bash --posix, sh --posix */
1046 if (interactive_shell
&& privileged_mode
== 0 && sourced_env
++ == 0)
1047 execute_env_file (get_string_value ("ENV"));
1050 #if defined (JOB_CONTROL)
1051 set_job_control (old_job_control
);
1055 #if defined (RESTRICTED_SHELL)
1056 /* Return 1 if the shell should be a restricted one based on NAME or the
1057 value of `restricted'. Don't actually do anything, just return a
1060 shell_is_restricted (name
)
1067 temp
= base_pathname (name
);
1068 return (STREQ (temp
, RESTRICTED_SHELL_NAME
));
1071 /* Perhaps make this shell a `restricted' one, based on NAME. If the
1072 basename of NAME is "rbash", then this shell is restricted. The
1073 name of the restricted shell is a configurable option, see config.h.
1074 In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
1076 Do this also if `restricted' is already set to 1; maybe the shell was
1079 maybe_make_restricted (name
)
1084 temp
= base_pathname (name
);
1085 if (restricted
|| (STREQ (temp
, RESTRICTED_SHELL_NAME
)))
1087 set_var_read_only ("PATH");
1088 set_var_read_only ("SHELL");
1089 set_var_read_only ("ENV");
1090 set_var_read_only ("BASH_ENV");
1093 return (restricted
);
1095 #endif /* RESTRICTED_SHELL */
1097 /* Fetch the current set of uids and gids and return 1 if we're running
1098 setuid or setgid. */
1105 if (current_user
.uid
!= u
)
1107 FREE (current_user
.user_name
);
1108 FREE (current_user
.shell
);
1109 FREE (current_user
.home_dir
);
1110 current_user
.user_name
= current_user
.shell
= current_user
.home_dir
= (char *)NULL
;
1112 current_user
.uid
= u
;
1113 current_user
.gid
= getgid ();
1114 current_user
.euid
= geteuid ();
1115 current_user
.egid
= getegid ();
1117 /* See whether or not we are running setuid or setgid. */
1118 return (current_user
.uid
!= current_user
.euid
) ||
1119 (current_user
.gid
!= current_user
.egid
);
1123 disable_priv_mode ()
1125 setuid (current_user
.uid
);
1126 setgid (current_user
.gid
);
1127 current_user
.euid
= current_user
.uid
;
1128 current_user
.egid
= current_user
.gid
;
1136 WORD_LIST
*wl
, *result
;
1138 code
= setjmp (top_level
);
1140 if (code
!= NOT_JUMPED
)
1144 /* Some kind of throw to top_level has occured. */
1146 return last_command_exit_value
= 127;
1148 return last_command_exit_value
;
1150 return last_command_exit_value
= 1;
1152 command_error ("run_wordexp", CMDERR_BADJUMP
, code
, 0);
1156 /* Run it through the parser to get a list of words and expand them */
1157 if (words
&& *words
)
1159 with_input_from_string (words
, "--wordexp");
1160 if (parse_command () != 0)
1162 if (global_command
== 0)
1167 if (global_command
->type
!= cm_simple
)
1169 wl
= global_command
->value
.Simple
->words
;
1170 result
= wl
? expand_words_no_vars (wl
) : (WORD_LIST
*)0;
1173 result
= (WORD_LIST
*)0;
1175 last_command_exit_value
= 0;
1183 /* Count up the number of words and bytes, and print them. Don't count
1184 the trailing NUL byte. */
1185 for (nw
= nb
= 0, wl
= result
; wl
; wl
= wl
->next
)
1188 nb
+= strlen (wl
->word
->word
);
1190 printf ("%u\n%u\n", nw
, nb
);
1191 /* Print each word on a separate line. This will have to be changed when
1192 the interface to glibc is completed. */
1193 for (wl
= result
; wl
; wl
= wl
->next
)
1194 printf ("%s\n", wl
->word
->word
);
1199 #if defined (ONESHOT)
1200 /* Run one command, given as the argument to the -c option. Tell
1201 parse_and_execute not to fork for a simple command. */
1203 run_one_command (command
)
1208 code
= setjmp (top_level
);
1210 if (code
!= NOT_JUMPED
)
1212 #if defined (PROCESS_SUBSTITUTION)
1213 unlink_fifo_list ();
1214 #endif /* PROCESS_SUBSTITUTION */
1217 /* Some kind of throw to top_level has occured. */
1219 return last_command_exit_value
= 127;
1221 return last_command_exit_value
;
1223 return last_command_exit_value
= 1;
1225 command_error ("run_one_command", CMDERR_BADJUMP
, code
, 0);
1228 return (parse_and_execute (savestring (command
), "-c", SEVAL_NOHIST
));
1230 #endif /* ONESHOT */
1233 bind_args (argv
, arg_start
, arg_end
, start_index
)
1235 int arg_start
, arg_end
, start_index
;
1240 for (i
= arg_start
, args
= (WORD_LIST
*)NULL
; i
!= arg_end
; i
++)
1241 args
= make_word_list (make_word (argv
[i
]), args
);
1244 args
= REVERSE_LIST (args
, WORD_LIST
*);
1245 if (start_index
== 0) /* bind to $0...$n for sh -c command */
1247 /* Posix.2 4.56.3 says that the first argument after sh -c command
1248 becomes $0, and the rest of the arguments become $1...$n */
1249 shell_name
= savestring (args
->word
->word
);
1250 FREE (dollar_vars
[0]);
1251 dollar_vars
[0] = savestring (args
->word
->word
);
1252 remember_args (args
->next
, 1);
1254 else /* bind to $1...$n for shell script */
1255 remember_args (args
, 1);
1257 dispose_words (args
);
1266 remember_args ((WORD_LIST
*)NULL
, 1);
1270 open_shell_script (script_name
)
1273 int fd
, e
, fd_is_tty
;
1274 char *filename
, *path_filename
;
1279 free (dollar_vars
[0]);
1280 dollar_vars
[0] = savestring (script_name
);
1281 filename
= savestring (script_name
);
1283 fd
= open (filename
, O_RDONLY
);
1284 if ((fd
< 0) && (errno
== ENOENT
) && (absolute_program (filename
) == 0))
1287 /* If it's not in the current directory, try looking through PATH
1289 path_filename
= find_path_file (script_name
);
1293 filename
= path_filename
;
1294 fd
= open (filename
, O_RDONLY
);
1303 file_error (filename
);
1304 exit ((e
== ENOENT
) ? EX_NOTFOUND
: EX_NOINPUT
);
1308 fd_is_tty
= isatty (fd
);
1313 /* Only do this with non-tty file descriptors we can seek on. */
1314 if (fd_is_tty
== 0 && (lseek (fd
, 0L, 1) != -1))
1316 /* Check to see if the `file' in `bash file' is a binary file
1317 according to the same tests done by execute_simple_command (),
1318 and report an error and exit if it is. */
1319 sample_len
= read (fd
, sample
, sizeof (sample
));
1323 if ((fstat (fd
, &sb
) == 0) && S_ISDIR (sb
.st_mode
))
1324 internal_error ("%s: is a directory", filename
);
1328 file_error (filename
);
1332 else if (sample_len
> 0 && (check_binary_file (sample
, sample_len
)))
1334 internal_error ("%s: cannot execute binary file", filename
);
1335 exit (EX_BINARY_FILE
);
1337 /* Now rewind the file back to the beginning. */
1341 /* Open the script. But try to move the file descriptor to a randomly
1342 large one, in the hopes that any descriptors used by the script will
1343 not match with ours. */
1344 fd
= move_to_high_fd (fd
, 0, -1);
1346 #if defined (__CYGWIN__) && defined (O_TEXT)
1347 setmode (fd
, O_TEXT
);
1350 #if defined (BUFFERED_INPUT)
1351 default_buffered_input
= fd
;
1352 SET_CLOSE_ON_EXEC (default_buffered_input
);
1353 #else /* !BUFFERED_INPUT */
1354 default_input
= fdopen (fd
, "r");
1356 if (default_input
== 0)
1358 file_error (filename
);
1362 SET_CLOSE_ON_EXEC (fd
);
1363 if (fileno (default_input
) != fd
)
1364 SET_CLOSE_ON_EXEC (fileno (default_input
));
1365 #endif /* !BUFFERED_INPUT */
1367 /* Just about the only way for this code to be executed is if something
1368 like `bash -i /dev/stdin' is executed. */
1369 if (interactive_shell
&& fd_is_tty
)
1374 #if defined (BUFFERED_INPUT)
1375 default_buffered_input
= 0;
1377 fclose (default_input
);
1378 default_input
= stdin
;
1381 else if (forced_interactive
&& fd_is_tty
== 0)
1382 /* But if a script is called with something like `bash -i scriptname',
1383 we need to do a non-interactive setup here, since we didn't do it
1385 init_noninteractive ();
1391 /* Initialize the input routines for the parser. */
1395 /* Make sure the fd from which we are reading input is not in
1397 #if defined (BUFFERED_INPUT)
1398 if (interactive
== 0)
1399 sh_unset_nodelay_mode (default_buffered_input
);
1401 #endif /* !BUFFERED_INPUT */
1402 sh_unset_nodelay_mode (fileno (stdin
));
1404 /* with_input_from_stdin really means `with_input_from_readline' */
1405 if (interactive
&& no_line_editing
== 0)
1406 with_input_from_stdin ();
1408 #if defined (BUFFERED_INPUT)
1410 if (interactive
== 0)
1411 with_input_from_buffered_stream (default_buffered_input
, dollar_vars
[0]);
1413 with_input_from_stream (default_input
, dollar_vars
[0]);
1415 #else /* !BUFFERED_INPUT */
1416 with_input_from_stream (default_input
, dollar_vars
[0]);
1417 #endif /* !BUFFERED_INPUT */
1420 /* Close the current shell script input source and forget about it. This is
1421 extern so execute_cmd.c:initialize_subshell() can call it. If CHECK_ZERO
1422 is non-zero, we close default_buffered_input even if it's the standard
1425 unset_bash_input (check_zero
)
1428 #if defined (BUFFERED_INPUT)
1429 if ((check_zero
&& default_buffered_input
>= 0) ||
1430 (check_zero
== 0 && default_buffered_input
> 0))
1432 close_buffered_fd (default_buffered_input
);
1433 default_buffered_input
= bash_input
.location
.buffered_fd
= -1;
1435 #else /* !BUFFERED_INPUT */
1438 fclose (default_input
);
1439 default_input
= (FILE *)NULL
;
1441 #endif /* !BUFFERED_INPUT */
1445 #if !defined (PROGRAM)
1446 # define PROGRAM "bash"
1450 set_shell_name (argv0
)
1453 /* Here's a hack. If the name of this shell is "sh", then don't do
1454 any startup files; just try to be more like /bin/sh. */
1455 shell_name
= base_pathname (argv0
);
1457 if (*shell_name
== '-')
1463 if (shell_name
[0] == 's' && shell_name
[1] == 'h' && shell_name
[2] == '\0')
1465 if (shell_name
[0] == 's' && shell_name
[1] == 'u' && shell_name
[2] == '\0')
1469 FREE (dollar_vars
[0]);
1470 dollar_vars
[0] = savestring (shell_name
);
1472 /* A program may start an interactive shell with
1473 "execl ("/bin/bash", "-", NULL)".
1474 If so, default the name of this shell to our name. */
1475 if (!shell_name
|| !*shell_name
|| (shell_name
[0] == '-' && !shell_name
[1]))
1476 shell_name
= PROGRAM
;
1482 interactive_shell
= startup_state
= interactive
= 1;
1487 init_noninteractive ()
1489 #if defined (HISTORY)
1490 bash_history_reinit (0);
1491 #endif /* HISTORY */
1492 interactive_shell
= startup_state
= interactive
= 0;
1493 expand_aliases
= posixly_correct
; /* XXX - was 0 not posixly_correct */
1494 no_line_editing
= 1;
1495 #if defined (JOB_CONTROL)
1496 set_job_control (0);
1497 #endif /* JOB_CONTROL */
1501 get_current_user_info ()
1503 struct passwd
*entry
;
1505 /* Don't fetch this more than once. */
1506 if (current_user
.user_name
== 0)
1508 entry
= getpwuid (current_user
.uid
);
1511 current_user
.user_name
= savestring (entry
->pw_name
);
1512 current_user
.shell
= (entry
->pw_shell
&& entry
->pw_shell
[0])
1513 ? savestring (entry
->pw_shell
)
1514 : savestring ("/bin/sh");
1515 current_user
.home_dir
= savestring (entry
->pw_dir
);
1519 current_user
.user_name
= savestring ("I have no name!");
1520 current_user
.shell
= savestring ("/bin/sh");
1521 current_user
.home_dir
= savestring ("/");
1527 /* Do whatever is necessary to initialize the shell.
1528 Put new initializations in here. */
1534 /* Line buffer output for stderr and stdout. */
1535 if (shell_initialized
== 0)
1537 sh_setlinebuf (stderr
);
1538 sh_setlinebuf (stdout
);
1541 /* Sort the array of shell builtins so that the binary search in
1542 find_shell_builtin () works correctly. */
1543 initialize_shell_builtins ();
1545 /* Initialize the trap signal handlers before installing our own
1546 signal handlers. traps.c:restore_original_signals () is responsible
1547 for restoring the original default signal handlers. That function
1548 is called when we make a new child. */
1549 initialize_traps ();
1550 initialize_signals (0);
1552 /* It's highly unlikely that this will change. */
1553 if (current_host_name
== 0)
1555 /* Initialize current_host_name. */
1556 if (gethostname (hostname
, 255) < 0)
1557 current_host_name
= "??host??";
1559 current_host_name
= savestring (hostname
);
1562 /* Initialize the stuff in current_user that comes from the password
1563 file. We don't need to do this right away if the shell is not
1565 if (interactive_shell
)
1566 get_current_user_info ();
1568 /* Initialize our interface to the tilde expander. */
1569 tilde_initialize ();
1571 /* Initialize internal and environment variables. Don't import shell
1572 functions from the environment if we are running in privileged or
1573 restricted mode or if the shell is running setuid. */
1574 #if defined (RESTRICTED_SHELL)
1575 initialize_shell_variables (shell_environment
, privileged_mode
||restricted
||running_setuid
);
1577 initialize_shell_variables (shell_environment
, privileged_mode
||running_setuid
);
1580 /* Initialize the data structures for storing and running jobs. */
1581 initialize_job_control (0);
1583 /* Initialize input streams to null. */
1584 initialize_bash_input ();
1586 initialize_flags ();
1588 /* Initialize the shell options. Don't import the shell options
1589 from the environment variable $SHELLOPTS if we are running in
1590 privileged or restricted mode or if the shell is running setuid. */
1591 #if defined (RESTRICTED_SHELL)
1592 initialize_shell_options (privileged_mode
||restricted
||running_setuid
);
1594 initialize_shell_options (privileged_mode
||running_setuid
);
1598 /* Function called by main () when it appears that the shell has already
1599 had some initialization performed. This is supposed to reset the world
1600 back to a pristine state, as if we had been exec'ed. */
1602 shell_reinitialize ()
1604 /* The default shell prompts. */
1605 primary_prompt
= PPROMPT
;
1606 secondary_prompt
= SPROMPT
;
1608 /* Things that get 1. */
1609 current_command_number
= 1;
1611 /* We have decided that the ~/.bashrc file should not be executed
1612 for the invocation of each shell script. If the variable $ENV
1613 (or $BASH_ENV) is set, its value is used as the name of a file
1615 no_rc
= no_profile
= 1;
1617 /* Things that get 0. */
1618 login_shell
= make_login_shell
= interactive
= executing
= 0;
1619 debugging
= do_version
= line_number
= last_command_exit_value
= 0;
1620 forced_interactive
= interactive_shell
= subshell_environment
= 0;
1623 #if defined (HISTORY)
1624 bash_history_reinit (0);
1625 #endif /* HISTORY */
1627 #if defined (RESTRICTED_SHELL)
1629 #endif /* RESTRICTED_SHELL */
1631 /* Ensure that the default startup file is used. (Except that we don't
1632 execute this file for reinitialized shells). */
1633 bashrc_file
= "~/.bashrc";
1635 /* Delete all variables and functions. They will be reinitialized when
1636 the environment is parsed. */
1637 delete_all_contexts (shell_variables
);
1638 delete_all_variables (shell_functions
);
1640 shell_reinitialized
= 1;
1644 show_shell_usage (fp
, extra
)
1649 char *set_opts
, *s
, *t
;
1652 fprintf (fp
, "GNU bash, version %s-(%s)\n", shell_version_string (), MACHTYPE
);
1653 fprintf (fp
, "Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n",
1654 shell_name
, shell_name
);
1655 fputs ("GNU long options:\n", fp
);
1656 for (i
= 0; long_args
[i
].name
; i
++)
1657 fprintf (fp
, "\t--%s\n", long_args
[i
].name
);
1659 fputs ("Shell options:\n", fp
);
1660 fputs ("\t-irsD or -c command or -O shopt_option\t\t(invocation only)\n", fp
);
1662 for (i
= 0, set_opts
= 0; shell_builtins
[i
].name
; i
++)
1663 if (STREQ (shell_builtins
[i
].name
, "set"))
1664 set_opts
= savestring (shell_builtins
[i
].short_doc
);
1667 s
= xstrchr (set_opts
, '[');
1672 t
= xstrchr (s
, ']');
1675 fprintf (fp
, "\t-%s or -o option\n", s
);
1681 fprintf (fp
, "Type `%s -c \"help set\"' for more information about shell options.\n", shell_name
);
1682 fprintf (fp
, "Type `%s -c help' for more information about shell builtin commands.\n", shell_name
);
1683 fprintf (fp
, "Use the `bashbug' command to report bugs.\n");
1688 add_shopt_to_alist (opt
, on_or_off
)
1692 if (shopt_ind
>= shopt_len
)
1695 shopt_alist
= (STRING_INT_ALIST
*)xrealloc (shopt_alist
, shopt_len
* sizeof (shopt_alist
[0]));
1697 shopt_alist
[shopt_ind
].word
= opt
;
1698 shopt_alist
[shopt_ind
].token
= on_or_off
;
1707 for (i
= 0; i
< shopt_ind
; i
++)
1708 if (shopt_setopt (shopt_alist
[i
].word
, (shopt_alist
[i
].token
== '-')) != EXECUTION_SUCCESS
)
1712 shopt_ind
= shopt_len
= 0;