1 /* shell.c -- GNU's idea of the POSIX shell specification.
3 This file is part of Bash, the Bourne Again SHell. Bash is free
4 software; no one can prevent you from reading the source code, or
5 giving it to someone else. This file is copyrighted under the GNU
6 General Public License, which can be found in the file called
9 Copyright (C) 1988, 1991 Free Software Foundation, Inc.
11 This file is part of GNU Bash.
13 Bash is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY. No author or distributor accepts responsibility to
15 anyone for the consequences of using it or for whether it serves
16 any particular purpose or works at all, unless he says so in
17 writing. Refer to the GNU Emacs General Public License for full
20 Everyone is granted permission to copy, modify and redistribute
21 Bash, but only under the conditions described in the GNU General
22 Public License. A copy of this license is supposed to have been
23 given to you along with GNU Emacs so you can know your rights and
24 responsibilities. It should be in a file named COPYING.
26 Among other things, the copyright notice and this notice must be
27 preserved on all copies.
30 Sunday, January 10th, 1988.
31 Initial author: Brian Fox
33 #define INSTALL_DEBUG_MODE
37 #include "bashtypes.h"
39 # include <sys/file.h>
41 #include "posixstat.h"
49 #if defined (HAVE_UNISTD_H)
56 #include "mailcheck.h"
58 #include "builtins/common.h"
60 #if defined (JOB_CONTROL)
62 #endif /* JOB_CONTROL */
65 #include "execute_cmd.h"
69 # include "bashhist.h"
70 # include <readline/history.h>
73 #include <tilde/tilde.h>
74 #include <glob/fnmatch.h>
76 #if defined (__OPENNT)
77 # include <opennt/opennt.h>
80 #if !defined (HAVE_GETPW_DECLS)
81 extern struct passwd
*getpwuid ();
82 #endif /* !HAVE_GETPW_DECLS */
88 #if defined (NO_MAIN_ENV_ARG)
89 extern char **environ
; /* used if no third argument to main() */
92 extern char *dist_version
, *release_status
;
93 extern int patch_level
, build_version
;
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 -1, -1, -1, -1, (char *)NULL
, (char *)NULL
, (char *)NULL
114 /* The current host's name. */
115 char *current_host_name
= (char *)NULL
;
117 /* Non-zero means that this shell is a login shell.
120 1 = login shell from getty (or equivalent fake out)
121 -1 = login shell from "--login" flag.
122 -2 = both from getty, and from flag.
126 /* Non-zero means that at this moment, the shell is interactive. In
127 general, this means that the shell is at this moment reading input
128 from the keyboard. */
131 /* Non-zero means that the shell was started as an interactive shell. */
132 int interactive_shell
= 0;
134 /* Non-zero means to send a SIGHUP to all jobs when an interactive login
138 /* Tells what state the shell was in when it started:
139 0 = non-interactive shell script
142 This is a superset of the information provided by interactive_shell.
144 int startup_state
= 0;
146 /* Special debugging helper. */
147 int debugging_login_shell
= 0;
149 /* The environment that the shell passes to other commands. */
150 char **shell_environment
;
152 /* Non-zero when we are executing a top-level command. */
155 /* The number of commands executed so far. */
156 int current_command_number
= 1;
158 /* Non-zero is the recursion depth for commands. */
159 int indirection_level
= 0;
161 /* The name of this shell, as taken from argv[0]. */
162 char *shell_name
= (char *)NULL
;
164 /* time in seconds when the shell was started */
165 time_t shell_start_time
;
167 /* Are we running in an emacs shell window? */
168 int running_under_emacs
;
170 /* The name of the .(shell)rc file. */
171 static char *bashrc_file
= "~/.bashrc";
173 /* Non-zero means to act more like the Bourne shell on startup. */
174 static int act_like_sh
;
176 /* Non-zero if this shell is being run by `su'. */
179 /* Non-zero if we have already expanded and sourced $ENV. */
180 static int sourced_env
;
182 /* Is this shell running setuid? */
183 static int running_setuid
;
185 /* Values for the long-winded argument names. */
186 static int debugging
; /* Do debugging things. */
187 static int no_rc
; /* Don't execute ~/.bashrc */
188 static int no_profile
; /* Don't execute .profile */
189 static int do_version
; /* Display interesting version info. */
190 static int make_login_shell
; /* Make this shell be a `-bash' shell. */
191 static int want_initial_help
; /* --help option */
193 int no_line_editing
= 0; /* Don't do fancy line editing. */
194 int posixly_correct
= 0; /* Non-zero means posix.2 superset. */
195 int dump_translatable_strings
; /* Dump strings in $"...", don't execute. */
196 int dump_po_strings
; /* Dump strings in $"..." in po format */
197 int wordexp_only
= 0; /* Do word expansion only */
199 /* Some long-winded argument names. These are obviously new. */
208 { "debug", Int
, &debugging
, (char **)0x0 },
209 { "dump-po-strings", Int
, &dump_po_strings
, (char **)0x0 },
210 { "dump-strings", Int
, &dump_translatable_strings
, (char **)0x0 },
211 { "help", Int
, &want_initial_help
, (char **)0x0 },
212 { "login", Int
, &make_login_shell
, (char **)0x0 },
213 { "noediting", Int
, &no_line_editing
, (char **)0x0 },
214 { "noprofile", Int
, &no_profile
, (char **)0x0 },
215 { "norc", Int
, &no_rc
, (char **)0x0 },
216 { "posix", Int
, &posixly_correct
, (char **)0x0 },
217 { "rcfile", Charp
, (int *)0x0, &bashrc_file
},
218 #if defined (RESTRICTED_SHELL)
219 { "restricted", Int
, &restricted
, (char **)0x0 },
221 { "verbose", Int
, &echo_input_at_read
, (char **)0x0 },
222 { "version", Int
, &do_version
, (char **)0x0 },
223 { "wordexp", Int
, &wordexp_only
, (char **)0x0 },
224 { (char *)0x0, Int
, (int *)0x0, (char **)0x0 }
227 /* These are extern so execute_simple_command can set them, and then
228 longjmp back to main to execute a shell script, instead of calling
229 main () again and resulting in indefinite, possibly fatal, stack
231 procenv_t subshell_top_level
;
233 char **subshell_argv
;
234 char **subshell_envp
;
236 #if defined (BUFFERED_INPUT)
237 /* The file descriptor from which the shell is reading input. */
238 int default_buffered_input
= -1;
241 static int read_from_stdin
; /* -s flag supplied */
242 static int want_pending_command
; /* -c flag supplied */
243 static char *local_pending_command
;
245 static FILE *default_input
;
247 static int parse_long_options ();
248 static int parse_shell_options ();
249 static void run_startup_files ();
250 static int bind_args ();
251 static int open_shell_script ();
252 static void set_bash_input ();
253 static int run_one_command ();
254 static int run_wordexp ();
256 static int uidget ();
257 static int isnetconn ();
259 static void init_interactive (), init_noninteractive ();
260 static void set_shell_name ();
261 static void shell_initialize ();
262 static void shell_reinitialize ();
264 static void show_shell_usage ();
268 _cygwin32_check_tmp ()
272 if (stat ("/tmp", &sb
) < 0)
273 internal_warning ("could not find /tmp, please create!");
276 if (S_ISDIR (sb
.st_mode
) == 0)
277 internal_warning ("/tmp must be a valid directory name");
280 #endif /* __CYGWIN32__ */
282 #if defined (NO_MAIN_ENV_ARG)
283 /* systems without third argument to main() */
288 #else /* !NO_MAIN_ENV_ARG */
290 main (argc
, argv
, env
)
293 #endif /* !NO_MAIN_ENV_ARG */
296 int code
, saverst
, old_errexit_flag
;
297 volatile int locally_skip_execution
;
298 volatile int arg_index
, top_level_arg_index
;
303 #endif /* __OPENNT */
305 /* Catch early SIGINTs. */
306 code
= setjmp (top_level
);
313 _cygwin32_check_tmp ();
316 /* Wait forever if we are debugging a login shell. */
317 while (debugging_login_shell
);
319 set_default_locale ();
321 running_setuid
= uidget ();
323 posixly_correct
= (getenv ("POSIXLY_CORRECT") != (char *)NULL
) ||
324 (getenv ("POSIX_PEDANTIC") != (char *)NULL
);
326 #if defined (USE_GNU_MALLOC_LIBRARY)
327 mcheck (programming_error
, (void (*) ())0);
328 #endif /* USE_GNU_MALLOC_LIBRARY */
330 if (setjmp (subshell_top_level
))
332 argc
= subshell_argc
;
333 argv
= subshell_argv
;
338 /* Initialize `local' variables for all `invocations' of main (). */
340 local_pending_command
= (char *)NULL
;
341 want_pending_command
= locally_skip_execution
= read_from_stdin
= 0;
342 default_input
= stdin
;
343 #if defined (BUFFERED_INPUT)
344 default_buffered_input
= -1;
347 /* Fix for the `infinite process creation' bug when running shell scripts
348 from startup files on System V. */
349 login_shell
= make_login_shell
= 0;
351 /* If this shell has already been run, then reinitialize it to a
353 if (shell_initialized
|| shell_name
)
355 /* Make sure that we do not infinitely recurse as a login shell. */
356 if (*shell_name
== '-')
359 shell_reinitialize ();
360 if (setjmp (top_level
))
364 shell_environment
= env
;
365 set_shell_name (argv
[0]);
366 shell_start_time
= NOW
; /* NOW now defined in general.h */
368 /* Parse argument flags from the input line. */
370 /* Find full word arguments first. */
371 arg_index
= parse_long_options (argv
, arg_index
, argc
);
373 if (want_initial_help
)
375 show_shell_usage (stdout
, 1);
376 exit (EXECUTION_SUCCESS
);
381 show_shell_version (1);
382 exit (EXECUTION_SUCCESS
);
385 /* If user supplied the "--login" flag, then set and invert LOGIN_SHELL. */
386 if (make_login_shell
)
389 login_shell
= -login_shell
;
392 /* All done with full word options; do standard shell option parsing.*/
393 this_command_name
= shell_name
; /* for error reporting */
394 arg_index
= parse_shell_options (argv
, arg_index
, argc
);
397 dump_translatable_strings
= 1;
399 if (dump_translatable_strings
)
400 read_but_dont_execute
= 1;
402 if (running_setuid
&& privileged_mode
== 0)
403 disable_priv_mode ();
405 /* Need to get the argument to a -c option processed in the
406 above loop. The next arg is a command to execute, and the
407 following args are $0...$n respectively. */
408 if (want_pending_command
)
410 local_pending_command
= argv
[arg_index
];
411 if (local_pending_command
== 0)
413 report_error ("option `-c' requires an argument");
418 this_command_name
= (char *)NULL
;
420 /* First, let the outside world know about our interactive status.
421 A shell is interactive if the `-i' flag was given, or if all of
422 the following conditions are met:
424 no arguments remaining or the -s flag given
425 standard input is a terminal
426 standard output is a terminal
427 Refer to Posix.2, the description of the `sh' utility. */
429 if (forced_interactive
|| /* -i flag */
430 (!local_pending_command
&& /* No -c command and ... */
431 wordexp_only
== 0 && /* No --wordexp and ... */
432 ((arg_index
== argc
) || /* no remaining args or... */
433 read_from_stdin
) && /* -s flag with args, and */
434 isatty (fileno (stdin
)) && /* Input is a terminal and */
435 isatty (fileno (stdout
)))) /* output is a terminal. */
438 init_noninteractive ();
440 #define CLOSE_FDS_AT_LOGIN
441 #if defined (CLOSE_FDS_AT_LOGIN)
443 * Some systems have the bad habit of starting login shells with lots of open
444 * file descriptors. For instance, most systems that have picked up the
445 * pre-4.0 Sun YP code leave a file descriptor open each time you call one
446 * of the getpw* functions, and it's set to be open across execs. That
447 * means one for login, one for xterm, one for shelltool, etc.
449 if (login_shell
&& interactive_shell
)
451 for (i
= 3; i
< 20; i
++)
454 #endif /* CLOSE_FDS_AT_LOGIN */
456 /* If we're in a strict Posix.2 mode, turn on interactive comments and
457 other Posix.2 things. */
460 posix_initialize (posixly_correct
);
461 #if defined (READLINE)
462 if (interactive_shell
)
463 posix_readline_initialize (posixly_correct
);
467 /* From here on in, the shell must be a normal functioning shell.
468 Variables from the environment are expected to be set, etc. */
471 set_default_locale_vars ();
473 if (interactive_shell
)
477 term
= getenv ("TERM");
478 no_line_editing
|= term
&& (STREQ (term
, "emacs"));
479 term
= getenv ("EMACS");
480 running_under_emacs
= term
? ((fnmatch ("*term*", term
, 0) == 0) ? 2 : 1)
484 top_level_arg_index
= arg_index
;
485 old_errexit_flag
= exit_immediately_on_error
;
487 /* Give this shell a place to longjmp to before executing the
488 startup files. This allows users to press C-c to abort the
490 code
= setjmp (top_level
);
493 if (code
== EXITPROG
)
494 exit_shell (last_command_exit_value
);
497 #if defined (JOB_CONTROL)
498 /* Reset job control, since run_startup_files turned it off. */
499 set_job_control (interactive_shell
);
501 /* Reset value of `set -e', since it's turned off before running
502 the startup files. */
503 exit_immediately_on_error
+= old_errexit_flag
;
504 locally_skip_execution
++;
508 arg_index
= top_level_arg_index
;
510 /* Execute the start-up scripts. */
512 if (interactive_shell
== 0)
514 makunbound ("PS1", shell_variables
);
515 makunbound ("PS2", shell_variables
);
516 interactive
= expand_aliases
= 0;
520 change_flag ('i', FLAG_ON
);
524 #if defined (RESTRICTED_SHELL)
525 /* Set restricted_shell based on whether the basename of $0 indicates that
526 the shell should be restricted or if the `-r' option was supplied at
528 restricted_shell
= shell_is_restricted (shell_name
);
530 /* If the `-r' option is supplied at invocation, make sure that the shell
531 is not in restricted mode when running the startup files. */
532 saverst
= restricted
;
536 /* The startup files are run with `set -e' temporarily disabled. */
537 if (locally_skip_execution
== 0 && running_setuid
== 0)
539 old_errexit_flag
= exit_immediately_on_error
;
540 exit_immediately_on_error
= 0;
542 run_startup_files ();
544 exit_immediately_on_error
+= old_errexit_flag
;
547 /* If we are invoked as `sh', turn on Posix mode. */
550 posix_initialize (posixly_correct
= 1);
551 #if defined (READLINE)
552 if (interactive_shell
)
553 posix_readline_initialize (posixly_correct
);
557 #if defined (RESTRICTED_SHELL)
558 /* Turn on the restrictions after executing the startup files. This
559 means that `bash -r' or `set -r' invoked from a startup file will
560 turn on the restrictions after the startup files are executed. */
561 restricted
= saverst
|| restricted
;
562 maybe_make_restricted (shell_name
);
563 #endif /* RESTRICTED_SHELL */
568 last_command_exit_value
= run_wordexp (argv
[arg_index
]);
569 exit_shell (last_command_exit_value
);
572 if (local_pending_command
)
574 arg_index
= bind_args (argv
, arg_index
, argc
, 0);
577 #if defined (ONESHOT)
578 run_one_command (local_pending_command
);
579 exit_shell (last_command_exit_value
);
581 with_input_from_string (local_pending_command
, "-c");
582 goto read_and_execute
;
583 #endif /* !ONESHOT */
586 /* Get possible input filename and set up default_buffered_input or
587 default_input as appropriate. */
588 if (arg_index
!= argc
&& read_from_stdin
== 0)
590 open_shell_script (argv
[arg_index
]);
593 else if (interactive
== 0)
594 /* In this mode, bash is reading a script from stdin, which is a
595 pipe or redirected file. */
596 #if defined (BUFFERED_INPUT)
597 default_buffered_input
= fileno (stdin
); /* == 0 */
599 setbuf (default_input
, (char *)NULL
);
600 #endif /* !BUFFERED_INPUT */
604 /* Bind remaining args to $1 ... $n */
605 arg_index
= bind_args (argv
, arg_index
, argc
, 1);
607 /* Do the things that should be done only for interactive shells. */
608 if (interactive_shell
)
610 /* Set up for checking for presence of mail. */
611 remember_mail_dates ();
614 #if defined (HISTORY)
615 /* Initialize the interactive history stuff. */
616 bash_initialize_history ();
617 if (shell_initialized
== 0)
621 /* Initialize terminal state for interactive shells after the
622 .bash_profile and .bashrc are interpreted. */
626 #if !defined (ONESHOT)
628 #endif /* !ONESHOT */
630 shell_initialized
= 1;
632 /* Read commands until exit condition. */
634 exit_shell (last_command_exit_value
);
638 parse_long_options (argv
, arg_start
, arg_end
)
640 int arg_start
, arg_end
;
642 int arg_index
, longarg
, i
;
645 arg_index
= arg_start
;
646 while ((arg_index
!= arg_end
) && (arg_string
= argv
[arg_index
]) &&
647 (*arg_string
== '-'))
651 /* Make --login equivalent to -login. */
652 if (arg_string
[1] == '-' && arg_string
[2])
658 for (i
= 0; long_args
[i
].name
; i
++)
660 if (STREQ (arg_string
+ 1, long_args
[i
].name
))
662 if (long_args
[i
].type
== Int
)
663 *long_args
[i
].int_value
= 1;
664 else if (argv
[++arg_index
] == 0)
666 report_error ("option `%s' requires an argument",
671 *long_args
[i
].char_value
= argv
[arg_index
];
676 if (long_args
[i
].name
== 0)
680 report_error ("%s: unrecognized option", argv
[arg_index
]);
681 show_shell_usage (stderr
, 0);
684 break; /* No such argument. Maybe flag arg. */
694 parse_shell_options (argv
, arg_start
, arg_end
)
696 int arg_start
, arg_end
;
699 int arg_character
, on_or_off
, next_arg
, i
;
700 char *o_option
, *arg_string
;
702 arg_index
= arg_start
;
703 while (arg_index
!= arg_end
&& (arg_string
= argv
[arg_index
]) &&
704 (*arg_string
== '-' || *arg_string
== '+'))
706 /* There are flag arguments, so parse them. */
707 next_arg
= arg_index
+ 1;
709 /* A single `-' signals the end of options. From the 4.3 BSD sh.
710 An option `--' means the same thing; this is the standard
711 getopt(3) meaning. */
712 if (arg_string
[0] == '-' &&
713 (arg_string
[1] == '\0' ||
714 (arg_string
[1] == '-' && arg_string
[2] == '\0')))
718 on_or_off
= arg_string
[0];
719 while (arg_character
= arg_string
[i
++])
721 switch (arg_character
)
724 want_pending_command
= 1;
732 o_option
= argv
[next_arg
];
735 list_minus_o_opts (-1, (on_or_off
== '-') ? 0 : 1);
738 if (set_minus_o_option (on_or_off
, o_option
) != EXECUTION_SUCCESS
)
744 dump_translatable_strings
= 1;
748 if (change_flag (arg_character
, on_or_off
) == FLAG_ERROR
)
750 report_error ("%c%c: unrecognized option", on_or_off
, arg_character
);
751 show_shell_usage (stderr
, 0);
756 /* Can't do just a simple increment anymore -- what about
757 "bash -abouo emacs ignoreeof -hP"? */
758 arg_index
= next_arg
;
764 /* Exit the shell with status S. */
769 /* Do trap[0] if defined. Allow it to override the exit status
771 if (signal_is_trapped (0))
772 s
= run_exit_trap ();
774 #if defined (PROCESS_SUBSTITUTION)
776 #endif /* PROCESS_SUBSTITUTION */
778 #if defined (HISTORY)
779 if (interactive_shell
)
780 maybe_save_shell_history ();
783 #if defined (JOB_CONTROL)
784 /* If the user has run `shopt -s huponexit', hangup all jobs when we exit
785 an interactive login shell. ksh does this unconditionally. */
786 if (interactive_shell
&& login_shell
&& hup_on_exit
)
789 /* If this shell is interactive, terminate all stopped jobs and
790 restore the original terminal process group. */
792 #endif /* JOB_CONTROL */
794 /* Always return the exit status of the last command to our parent. */
798 /* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey
799 the Posix.2 startup file rules: $ENV is expanded, and if the file it
800 names exists, that file is sourced. The Posix.2 rules are in effect
801 for interactive shells only. (section 4.56.5.3) */
803 /* Execute ~/.bashrc for most shells. Never execute it if
804 ACT_LIKE_SH is set, or if NO_RC is set.
806 If the executable file "/usr/gnu/src/bash/foo" contains:
813 COMMAND EXECUTE BASHRC
814 --------------------------------
818 rsh machine ls YES (for rsh, which calls `bash -c')
819 rsh machine foo YES (for shell started by rsh) NO (for foo!)
826 execute_env_file (env_file
)
832 if (env_file
&& *env_file
)
834 list
= expand_string_unsplit (env_file
, Q_DOUBLE_QUOTES
);
837 fn
= string_list (list
);
838 dispose_words (list
);
841 maybe_execute_file (fn
, 1);
850 #if defined (JOB_CONTROL)
853 int sourced_login
, run_by_ssh
;
856 /* get the rshd/sshd case out of the way first. */
857 if (interactive_shell
== 0 && no_rc
== 0 && login_shell
== 0 &&
858 act_like_sh
== 0 && local_pending_command
)
860 /* Find out if we were invoked by ssh. If so, set RUN_BY_SSH to 1. */
861 sshvar
= find_variable ("SSH_CLIENT");
865 /* Now that we've tested the variable, we need to unexport it. */
866 sshvar
->attributes
&= ~att_exported
;
867 array_needs_making
= 1;
872 /* If we were run by sshd or we think we were run by rshd, execute
874 if (run_by_ssh
|| isnetconn (fileno (stdin
)))
877 # if defined (__OPENNT)
878 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
880 maybe_execute_file (SYS_BASHRC
, 1);
883 maybe_execute_file (bashrc_file
, 1);
888 #if defined (JOB_CONTROL)
889 /* Startup files should be run without job control enabled. */
890 old_job_control
= interactive_shell
? set_job_control (0) : 0;
895 if (login_shell
< 0 && posixly_correct
== 0) /* --login flag and not posix */
897 /* We don't execute .bashrc for login shells. */
900 /* Execute /etc/profile and one of the personal login shell
901 initialization files. */
904 maybe_execute_file (SYS_PROFILE
, 1);
906 if (act_like_sh
) /* sh */
907 maybe_execute_file ("~/.profile", 1);
908 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
909 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
910 maybe_execute_file ("~/.profile", 1);
916 /* A non-interactive shell not named `sh' and not in posix mode reads and
917 executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd'
918 and `-su' as the name of the shell, we want to read the startup files.
919 No other non-interactive shells read any startup files. */
920 if (interactive_shell
== 0 && !(su_shell
&& login_shell
))
922 if (posixly_correct
== 0 && act_like_sh
== 0 && privileged_mode
== 0 &&
924 execute_env_file (get_string_value ("BASH_ENV"));
928 /* Interactive shell or `-su' shell. */
929 if (posixly_correct
== 0) /* bash, sh */
931 if (login_shell
&& sourced_login
++ == 0)
933 /* We don't execute .bashrc for login shells. */
936 /* Execute /etc/profile and one of the personal login shell
937 initialization files. */
940 maybe_execute_file (SYS_PROFILE
, 1);
942 if (act_like_sh
) /* sh */
943 maybe_execute_file ("~/.profile", 1);
944 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
945 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
946 maybe_execute_file ("~/.profile", 1);
951 if (act_like_sh
== 0 && no_rc
== 0)
954 # if defined (__OPENNT)
955 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
957 maybe_execute_file (SYS_BASHRC
, 1);
960 maybe_execute_file (bashrc_file
, 1);
963 else if (act_like_sh
&& privileged_mode
== 0 && sourced_env
++ == 0)
964 execute_env_file (get_string_value ("ENV"));
966 else /* bash --posix, sh --posix */
969 if (interactive_shell
&& privileged_mode
== 0 && sourced_env
++ == 0)
970 execute_env_file (get_string_value ("ENV"));
973 #if defined (JOB_CONTROL)
974 set_job_control (old_job_control
);
978 #if defined (RESTRICTED_SHELL)
979 /* Return 1 if the shell should be a restricted one based on NAME or the
980 value of `restricted'. Don't actually do anything, just return a
983 shell_is_restricted (name
)
990 temp
= base_pathname (name
);
991 return (STREQ (temp
, RESTRICTED_SHELL_NAME
));
994 /* Perhaps make this shell a `restricted' one, based on NAME. If the
995 basename of NAME is "rbash", then this shell is restricted. The
996 name of the restricted shell is a configurable option, see config.h.
997 In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
999 Do this also if `restricted' is already set to 1; maybe the shell was
1002 maybe_make_restricted (name
)
1007 temp
= base_pathname (shell_name
);
1008 if (restricted
|| (STREQ (temp
, RESTRICTED_SHELL_NAME
)))
1010 set_var_read_only ("PATH");
1011 set_var_read_only ("SHELL");
1012 set_var_read_only ("ENV");
1013 set_var_read_only ("BASH_ENV");
1016 return (restricted
);
1018 #endif /* RESTRICTED_SHELL */
1020 /* Fetch the current set of uids and gids and return 1 if we're running
1021 setuid or setgid. */
1028 if (current_user
.uid
!= u
)
1030 FREE (current_user
.user_name
);
1031 FREE (current_user
.shell
);
1032 FREE (current_user
.home_dir
);
1033 current_user
.user_name
= current_user
.shell
= current_user
.home_dir
= (char *)NULL
;
1035 current_user
.uid
= u
;
1036 current_user
.gid
= getgid ();
1037 current_user
.euid
= geteuid ();
1038 current_user
.egid
= getegid ();
1040 /* See whether or not we are running setuid or setgid. */
1041 return (current_user
.uid
!= current_user
.euid
) ||
1042 (current_user
.gid
!= current_user
.egid
);
1046 disable_priv_mode ()
1048 setuid (current_user
.uid
);
1049 setgid (current_user
.gid
);
1050 current_user
.euid
= current_user
.uid
;
1051 current_user
.egid
= current_user
.gid
;
1060 WORD_LIST
*wl
, *result
;
1062 code
= setjmp (top_level
);
1064 if (code
!= NOT_JUMPED
)
1068 /* Some kind of throw to top_level has occured. */
1070 return last_command_exit_value
= 127;
1072 return last_command_exit_value
;
1074 return last_command_exit_value
= 1;
1076 command_error ("run_wordexp", CMDERR_BADJUMP
, code
, 0);
1080 /* Run it through the parser to get a list of words and expand them */
1081 if (words
&& *words
)
1083 with_input_from_string (words
, "--wordexp");
1084 if (parse_command () != 0)
1086 if (global_command
== 0)
1091 if (global_command
->type
!= cm_simple
)
1093 wl
= global_command
->value
.Simple
->words
;
1094 result
= wl
? expand_words_no_vars (wl
) : (WORD_LIST
*)0;
1097 result
= (WORD_LIST
*)0;
1099 last_command_exit_value
= 0;
1107 /* Count up the number of words and bytes, and print them. Don't count
1108 the trailing NUL byte. */
1109 for (nw
= nb
= 0, wl
= result
; wl
; wl
= wl
->next
)
1112 nb
+= strlen (wl
->word
->word
);
1114 printf ("%u\n%u\n", nw
, nb
);
1115 /* Print each word on a separate line. This will have to be changed when
1116 the interface to glibc is completed. */
1117 for (wl
= result
; wl
; wl
= wl
->next
)
1118 printf ("%s\n", wl
->word
->word
);
1123 #if defined (ONESHOT)
1124 /* Run one command, given as the argument to the -c option. Tell
1125 parse_and_execute not to fork for a simple command. */
1127 run_one_command (command
)
1132 code
= setjmp (top_level
);
1134 if (code
!= NOT_JUMPED
)
1136 #if defined (PROCESS_SUBSTITUTION)
1137 unlink_fifo_list ();
1138 #endif /* PROCESS_SUBSTITUTION */
1141 /* Some kind of throw to top_level has occured. */
1143 return last_command_exit_value
= 127;
1145 return last_command_exit_value
;
1147 return last_command_exit_value
= 1;
1149 command_error ("run_one_command", CMDERR_BADJUMP
, code
, 0);
1152 return (parse_and_execute (savestring (command
), "-c", SEVAL_NOHIST
));
1154 #endif /* ONESHOT */
1157 bind_args (argv
, arg_start
, arg_end
, start_index
)
1159 int arg_start
, arg_end
, start_index
;
1164 for (i
= arg_start
, args
= (WORD_LIST
*)NULL
; i
!= arg_end
; i
++)
1165 args
= make_word_list (make_word (argv
[i
]), args
);
1168 args
= REVERSE_LIST (args
, WORD_LIST
*);
1169 if (start_index
== 0) /* bind to $0...$n for sh -c command */
1171 /* Posix.2 4.56.3 says that the first argument after sh -c command
1172 becomes $0, and the rest of the arguments become $1...$n */
1173 shell_name
= savestring (args
->word
->word
);
1174 FREE (dollar_vars
[0]);
1175 dollar_vars
[0] = savestring (args
->word
->word
);
1176 remember_args (args
->next
, 1);
1178 else /* bind to $1...$n for shell script */
1179 remember_args (args
, 1);
1181 dispose_words (args
);
1190 remember_args ((WORD_LIST
*)NULL
, 1);
1194 open_shell_script (script_name
)
1198 char *filename
, *path_filename
;
1199 unsigned char sample
[80];
1203 free (dollar_vars
[0]);
1204 dollar_vars
[0] = savestring (script_name
);
1205 filename
= savestring (script_name
);
1207 fd
= open (filename
, O_RDONLY
);
1208 if ((fd
< 0) && (errno
== ENOENT
) && (absolute_program (filename
) == 0))
1211 /* If it's not in the current directory, try looking through PATH
1213 path_filename
= find_path_file (script_name
);
1217 filename
= path_filename
;
1218 fd
= open (filename
, O_RDONLY
);
1227 file_error (filename
);
1228 exit ((e
== ENOENT
) ? EX_NOTFOUND
: EX_NOINPUT
);
1231 /* Only do this with file descriptors we can seek on. */
1232 if (lseek (fd
, 0L, 1) != -1)
1234 /* Check to see if the `file' in `bash file' is a binary file
1235 according to the same tests done by execute_simple_command (),
1236 and report an error and exit if it is. */
1237 sample_len
= read (fd
, sample
, sizeof (sample
));
1241 if ((fstat (fd
, &sb
) == 0) && S_ISDIR (sb
.st_mode
))
1242 internal_error ("%s: is a directory", filename
);
1246 file_error (filename
);
1250 else if (sample_len
> 0 && (check_binary_file (sample
, sample_len
)))
1252 internal_error ("%s: cannot execute binary file", filename
);
1253 exit (EX_BINARY_FILE
);
1255 /* Now rewind the file back to the beginning. */
1259 #if defined (BUFFERED_INPUT)
1260 default_buffered_input
= fd
;
1262 /* This is never executed. */
1263 if (default_buffered_input
== -1)
1265 file_error (filename
);
1269 SET_CLOSE_ON_EXEC (default_buffered_input
);
1270 #else /* !BUFFERED_INPUT */
1271 /* Open the script. But try to move the file descriptor to a randomly
1272 large one, in the hopes that any descriptors used by the script will
1273 not match with ours. */
1274 fd
= move_to_high_fd (fd
, 0, -1);
1276 default_input
= fdopen (fd
, "r");
1278 if (default_input
== 0)
1280 file_error (filename
);
1284 SET_CLOSE_ON_EXEC (fd
);
1285 if (fileno (default_input
) != fd
)
1286 SET_CLOSE_ON_EXEC (fileno (default_input
));
1287 #endif /* !BUFFERED_INPUT */
1289 if (interactive_shell
== 0 || isatty (fd
) == 0)
1290 /* XXX - does this really need to be called again here? */
1291 init_noninteractive ();
1294 /* I don't believe that this code is ever executed, even in
1295 the presence of /dev/fd. */
1299 #if defined (BUFFERED_INPUT)
1300 default_buffered_input
= 0;
1302 fclose (default_input
);
1303 default_input
= stdin
;
1310 /* Initialize the input routines for the parser. */
1314 /* Make sure the fd from which we are reading input is not in
1316 #if defined (BUFFERED_INPUT)
1317 if (interactive
== 0)
1318 unset_nodelay_mode (default_buffered_input
);
1320 #endif /* !BUFFERED_INPUT */
1321 unset_nodelay_mode (fileno (stdin
));
1323 /* with_input_from_stdin really means `with_input_from_readline' */
1324 if (interactive
&& no_line_editing
== 0)
1325 with_input_from_stdin ();
1327 #if defined (BUFFERED_INPUT)
1329 if (interactive
== 0)
1330 with_input_from_buffered_stream (default_buffered_input
, dollar_vars
[0]);
1332 with_input_from_stream (default_input
, dollar_vars
[0]);
1334 #else /* !BUFFERED_INPUT */
1335 with_input_from_stream (default_input
, dollar_vars
[0]);
1336 #endif /* !BUFFERED_INPUT */
1339 /* Close the current shell script input source and forget about it. This is
1340 extern so execute_cmd.c:initialize_subshell() can call it. If CHECK_ZERO
1341 is non-zero, we close default_buffered_input even if it's the standard
1344 unset_bash_input (check_zero
)
1347 #if defined (BUFFERED_INPUT)
1348 if ((check_zero
&& default_buffered_input
>= 0) ||
1349 (check_zero
== 0 && default_buffered_input
> 0))
1351 close_buffered_fd (default_buffered_input
);
1352 default_buffered_input
= bash_input
.location
.buffered_fd
= -1;
1354 #else /* !BUFFERED_INPUT */
1357 fclose (default_input
);
1358 default_input
= (FILE *)NULL
;
1360 #endif /* !BUFFERED_INPUT */
1364 #if !defined (PROGRAM)
1365 # define PROGRAM "bash"
1369 set_shell_name (argv0
)
1372 /* Here's a hack. If the name of this shell is "sh", then don't do
1373 any startup files; just try to be more like /bin/sh. */
1374 shell_name
= base_pathname (argv0
);
1375 if (*shell_name
== '-')
1377 if (shell_name
[0] == 's' && shell_name
[1] == 'h' && shell_name
[2] == '\0')
1379 if (shell_name
[0] == 's' && shell_name
[1] == 'u' && shell_name
[2] == '\0')
1383 FREE (dollar_vars
[0]);
1384 dollar_vars
[0] = savestring (shell_name
);
1386 if (*shell_name
== '-')
1392 /* A program may start an interactive shell with
1393 "execl ("/bin/bash", "-", NULL)".
1394 If so, default the name of this shell to our name. */
1395 if (!shell_name
|| !*shell_name
|| (shell_name
[0] == '-' && !shell_name
[1]))
1396 shell_name
= PROGRAM
;
1402 interactive_shell
= startup_state
= interactive
= 1;
1407 init_noninteractive ()
1409 #if defined (HISTORY)
1410 bash_history_reinit (0);
1411 #endif /* HISTORY */
1412 interactive_shell
= startup_state
= interactive
= 0;
1414 no_line_editing
= 1;
1415 #if defined (JOB_CONTROL)
1416 set_job_control (0);
1417 #endif /* JOB_CONTROL */
1421 get_current_user_info ()
1423 struct passwd
*entry
;
1425 /* Don't fetch this more than once. */
1426 if (current_user
.user_name
== 0)
1428 entry
= getpwuid (current_user
.uid
);
1431 current_user
.user_name
= savestring (entry
->pw_name
);
1432 current_user
.shell
= (entry
->pw_shell
&& entry
->pw_shell
[0])
1433 ? savestring (entry
->pw_shell
)
1434 : savestring ("/bin/sh");
1435 current_user
.home_dir
= savestring (entry
->pw_dir
);
1439 current_user
.user_name
= savestring ("I have no name!");
1440 current_user
.shell
= savestring ("/bin/sh");
1441 current_user
.home_dir
= savestring ("/");
1447 /* Do whatever is necessary to initialize the shell.
1448 Put new initializations in here. */
1454 /* Line buffer output for stderr and stdout. */
1455 if (shell_initialized
== 0)
1457 setlinebuf (stderr
);
1458 setlinebuf (stdout
);
1461 /* Sort the array of shell builtins so that the binary search in
1462 find_shell_builtin () works correctly. */
1463 initialize_shell_builtins ();
1465 /* Initialize the trap signal handlers before installing our own
1466 signal handlers. traps.c:restore_original_signals () is responsible
1467 for restoring the original default signal handlers. That function
1468 is called when we make a new child. */
1469 initialize_traps ();
1470 initialize_signals ();
1472 /* It's highly unlikely that this will change. */
1473 if (current_host_name
== 0)
1475 /* Initialize current_host_name. */
1476 if (gethostname (hostname
, 255) < 0)
1477 current_host_name
= "??host??";
1479 current_host_name
= savestring (hostname
);
1482 /* Initialize the stuff in current_user that comes from the password
1483 file. We don't need to do this right away if the shell is not
1485 if (interactive_shell
)
1486 get_current_user_info ();
1488 /* Initialize our interface to the tilde expander. */
1489 tilde_initialize ();
1491 /* Initialize internal and environment variables. Don't import shell
1492 functions from the environment if we are running in privileged or
1493 restricted mode or if the shell is running setuid. */
1494 #if defined (RESTRICTED_SHELL)
1495 initialize_shell_variables (shell_environment
, privileged_mode
||restricted
||running_setuid
);
1497 initialize_shell_variables (shell_environment
, privileged_mode
||running_setuid
);
1501 /* Initialize filename hash tables. */
1502 initialize_filename_hashing ();
1505 /* Initialize the data structures for storing and running jobs. */
1506 initialize_job_control (0);
1508 /* Initialize input streams to null. */
1509 initialize_bash_input ();
1511 /* Initialize the shell options. Don't import the shell options
1512 from the environment variable $SHELLOPTS if we are running in
1513 privileged or restricted mode or if the shell is running setuid. */
1514 #if defined (RESTRICTED_SHELL)
1515 initialize_shell_options (privileged_mode
||restricted
||running_setuid
);
1517 initialize_shell_options (privileged_mode
||running_setuid
);
1521 /* Function called by main () when it appears that the shell has already
1522 had some initialization performed. This is supposed to reset the world
1523 back to a pristine state, as if we had been exec'ed. */
1525 shell_reinitialize ()
1527 /* The default shell prompts. */
1528 primary_prompt
= PPROMPT
;
1529 secondary_prompt
= SPROMPT
;
1531 /* Things that get 1. */
1532 current_command_number
= 1;
1534 /* We have decided that the ~/.bashrc file should not be executed
1535 for the invocation of each shell script. If the variable $ENV
1536 (or $BASH_ENV) is set, its value is used as the name of a file
1538 no_rc
= no_profile
= 1;
1540 /* Things that get 0. */
1541 login_shell
= make_login_shell
= interactive
= executing
= 0;
1542 debugging
= do_version
= line_number
= last_command_exit_value
= 0;
1543 forced_interactive
= interactive_shell
= subshell_environment
= 0;
1546 #if defined (HISTORY)
1547 bash_history_reinit (0);
1548 #endif /* HISTORY */
1550 #if defined (RESTRICTED_SHELL)
1552 #endif /* RESTRICTED_SHELL */
1554 /* Ensure that the default startup file is used. (Except that we don't
1555 execute this file for reinitialized shells). */
1556 bashrc_file
= "~/.bashrc";
1558 /* Delete all variables and functions. They will be reinitialized when
1559 the environment is parsed. */
1560 delete_all_variables (shell_variables
);
1561 delete_all_variables (shell_functions
);
1564 /* Pretend the PATH variable has changed. */
1565 flush_hashed_filenames ();
1570 show_shell_usage (fp
, extra
)
1575 char *set_opts
, *s
, *t
;
1578 fprintf (fp
, "GNU bash, version %s-(%s)\n", shell_version_string (), MACHTYPE
);
1579 fprintf (fp
, "Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n",
1580 shell_name
, shell_name
);
1581 fputs ("GNU long options:\n", fp
);
1582 for (i
= 0; long_args
[i
].name
; i
++)
1583 fprintf (fp
, "\t--%s\n", long_args
[i
].name
);
1585 fputs ("Shell options:\n", fp
);
1586 fputs ("\t-irsD or -c command\t\t(invocation only)\n", fp
);
1588 for (i
= 0, set_opts
= 0; shell_builtins
[i
].name
; i
++)
1589 if (STREQ (shell_builtins
[i
].name
, "set"))
1590 set_opts
= savestring (shell_builtins
[i
].short_doc
);
1593 s
= strchr (set_opts
, '[');
1598 t
= strchr (s
, ']');
1601 fprintf (fp
, "\t-%s or -o option\n", s
);
1607 fprintf (fp
, "Type `%s -c \"help set\"' for more information about shell options.\n", shell_name
);
1608 fprintf (fp
, "Type `%s -c help' for more information about shell builtin commands.\n", shell_name
);
1609 fprintf (fp
, "Use the `bashbug' command to report bugs.\n");
1613 /* The second and subsequent conditions must match those used to decide
1614 whether or not to call getpeername() in isnetconn(). */
1615 #if defined (HAVE_SYS_SOCKET_H) && defined (HAVE_GETPEERNAME) && !defined (SVR4_2)
1616 # include <sys/socket.h>
1619 /* Is FD a socket or network connection? */
1624 #if defined (HAVE_GETPEERNAME) && !defined (SVR4_2) && !defined (__BEOS__)
1629 rv
= getpeername(fd
, &sa
, &l
);
1630 /* Solaris 2.5 getpeername() returns EINVAL if the fd is not a socket. */
1631 return ((rv
< 0 && (errno
== ENOTSOCK
|| errno
== EINVAL
)) ? 0 : 1);
1632 #else /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */
1633 # if defined (SVR4) || defined (SVR4_2)
1634 /* Sockets on SVR4 and SVR4.2 are character special (streams) devices. */
1639 if (fstat (fd
, &sb
) < 0)
1641 # if defined (S_ISFIFO)
1642 if (S_ISFIFO (sb
.st_mode
))
1644 # endif /* S_ISFIFO */
1645 return (S_ISCHR (sb
.st_mode
));
1646 # else /* !SVR4 && !SVR4_2 */
1647 # if defined (S_ISSOCK) && !defined (__BEOS__)
1650 if (fstat (fd
, &sb
) < 0)
1652 return (S_ISSOCK (sb
.st_mode
));
1653 # else /* !S_ISSOCK || __BEOS__ */
1655 # endif /* !S_ISSOCK || __BEOS__ */
1656 # endif /* !SVR4 && !SVR4_2 */
1657 #endif /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */