1 /* shell.c -- GNU's idea of the POSIX shell specification. */
3 /* Copyright (C) 1987,1991 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"
41 #if defined (HAVE_UNISTD_H)
48 #include "mailcheck.h"
50 #include "builtins/common.h"
52 #if defined (JOB_CONTROL)
54 #endif /* JOB_CONTROL */
57 #include "execute_cmd.h"
61 # include "bashhist.h"
62 # include <readline/history.h>
65 #include <tilde/tilde.h>
66 #include <glob/fnmatch.h>
68 #if defined (__OPENNT)
69 # include <opennt/opennt.h>
72 #if !defined (HAVE_GETPW_DECLS)
73 extern struct passwd
*getpwuid ();
74 #endif /* !HAVE_GETPW_DECLS */
80 #if defined (NO_MAIN_ENV_ARG)
81 extern char **environ
; /* used if no third argument to main() */
84 extern char *dist_version
, *release_status
;
85 extern int patch_level
, build_version
;
86 extern int shell_level
;
87 extern int subshell_environment
;
88 extern int last_command_exit_value
;
89 extern int line_number
;
90 extern char *primary_prompt
, *secondary_prompt
;
91 extern int expand_aliases
;
92 extern char *this_command_name
;
93 extern int array_needs_making
;
95 /* Non-zero means that this shell has already been run; i.e. you should
96 call shell_reinitialize () if you need to start afresh. */
97 int shell_initialized
= 0;
99 COMMAND
*global_command
= (COMMAND
*)NULL
;
101 /* Information about the current user. */
102 struct user_info current_user
=
104 -1, -1, -1, -1, (char *)NULL
, (char *)NULL
, (char *)NULL
107 /* The current host's name. */
108 char *current_host_name
= (char *)NULL
;
110 /* Non-zero means that this shell is a login shell.
113 1 = login shell from getty (or equivalent fake out)
114 -1 = login shell from "--login" flag.
115 -2 = both from getty, and from flag.
119 /* Non-zero means that at this moment, the shell is interactive. In
120 general, this means that the shell is at this moment reading input
121 from the keyboard. */
124 /* Non-zero means that the shell was started as an interactive shell. */
125 int interactive_shell
= 0;
127 /* Non-zero means to send a SIGHUP to all jobs when an interactive login
131 /* Tells what state the shell was in when it started:
132 0 = non-interactive shell script
135 This is a superset of the information provided by interactive_shell.
137 int startup_state
= 0;
139 /* Special debugging helper. */
140 int debugging_login_shell
= 0;
142 /* The environment that the shell passes to other commands. */
143 char **shell_environment
;
145 /* Non-zero when we are executing a top-level command. */
148 /* The number of commands executed so far. */
149 int current_command_number
= 1;
151 /* Non-zero is the recursion depth for commands. */
152 int indirection_level
= 0;
154 /* The name of this shell, as taken from argv[0]. */
155 char *shell_name
= (char *)NULL
;
157 /* time in seconds when the shell was started */
158 time_t shell_start_time
;
160 /* Are we running in an emacs shell window? */
161 int running_under_emacs
;
163 /* The name of the .(shell)rc file. */
164 static char *bashrc_file
= "~/.bashrc";
166 /* Non-zero means to act more like the Bourne shell on startup. */
167 static int act_like_sh
;
169 /* Non-zero if this shell is being run by `su'. */
172 /* Non-zero if we have already expanded and sourced $ENV. */
173 static int sourced_env
;
175 /* Is this shell running setuid? */
176 static int running_setuid
;
178 /* Values for the long-winded argument names. */
179 static int debugging
; /* Do debugging things. */
180 static int no_rc
; /* Don't execute ~/.bashrc */
181 static int no_profile
; /* Don't execute .profile */
182 static int do_version
; /* Display interesting version info. */
183 static int make_login_shell
; /* Make this shell be a `-bash' shell. */
184 static int want_initial_help
; /* --help option */
186 int no_line_editing
= 0; /* Don't do fancy line editing. */
187 int posixly_correct
= 0; /* Non-zero means posix.2 superset. */
188 int dump_translatable_strings
; /* Dump strings in $"...", don't execute. */
189 int dump_po_strings
; /* Dump strings in $"..." in po format */
190 int wordexp_only
= 0; /* Do word expansion only */
192 /* Some long-winded argument names. These are obviously new. */
201 { "debug", Int
, &debugging
, (char **)0x0 },
202 { "dump-po-strings", Int
, &dump_po_strings
, (char **)0x0 },
203 { "dump-strings", Int
, &dump_translatable_strings
, (char **)0x0 },
204 { "help", Int
, &want_initial_help
, (char **)0x0 },
205 { "init-file", Charp
, (int *)0x0, &bashrc_file
},
206 { "login", Int
, &make_login_shell
, (char **)0x0 },
207 { "noediting", Int
, &no_line_editing
, (char **)0x0 },
208 { "noprofile", Int
, &no_profile
, (char **)0x0 },
209 { "norc", Int
, &no_rc
, (char **)0x0 },
210 { "posix", Int
, &posixly_correct
, (char **)0x0 },
211 { "rcfile", Charp
, (int *)0x0, &bashrc_file
},
212 #if defined (RESTRICTED_SHELL)
213 { "restricted", Int
, &restricted
, (char **)0x0 },
215 { "verbose", Int
, &echo_input_at_read
, (char **)0x0 },
216 { "version", Int
, &do_version
, (char **)0x0 },
217 { "wordexp", Int
, &wordexp_only
, (char **)0x0 },
218 { (char *)0x0, Int
, (int *)0x0, (char **)0x0 }
221 /* These are extern so execute_simple_command can set them, and then
222 longjmp back to main to execute a shell script, instead of calling
223 main () again and resulting in indefinite, possibly fatal, stack
225 procenv_t subshell_top_level
;
227 char **subshell_argv
;
228 char **subshell_envp
;
230 #if defined (BUFFERED_INPUT)
231 /* The file descriptor from which the shell is reading input. */
232 int default_buffered_input
= -1;
235 /* The following two variables are not static so they can show up in $-. */
236 int read_from_stdin
; /* -s flag supplied */
237 int want_pending_command
; /* -c flag supplied */
239 static int shell_reinitialized
= 0;
240 static char *local_pending_command
;
242 static FILE *default_input
;
244 static int parse_long_options ();
245 static int parse_shell_options ();
246 static void run_startup_files ();
247 static int bind_args ();
248 static int open_shell_script ();
249 static void set_bash_input ();
250 static int run_one_command ();
251 static int run_wordexp ();
253 static int uidget ();
254 static int isnetconn ();
256 static void init_interactive (), init_noninteractive ();
257 static void set_shell_name ();
258 static void shell_initialize ();
259 static void shell_reinitialize ();
261 static void show_shell_usage ();
265 _cygwin32_check_tmp ()
269 if (stat ("/tmp", &sb
) < 0)
270 internal_warning ("could not find /tmp, please create!");
273 if (S_ISDIR (sb
.st_mode
) == 0)
274 internal_warning ("/tmp must be a valid directory name");
277 #endif /* __CYGWIN__ */
279 #if defined (NO_MAIN_ENV_ARG)
280 /* systems without third argument to main() */
285 #else /* !NO_MAIN_ENV_ARG */
287 main (argc
, argv
, env
)
290 #endif /* !NO_MAIN_ENV_ARG */
293 int code
, saverst
, old_errexit_flag
;
294 volatile int locally_skip_execution
;
295 volatile int arg_index
, top_level_arg_index
;
300 #endif /* __OPENNT */
302 /* Catch early SIGINTs. */
303 code
= setjmp (top_level
);
310 _cygwin32_check_tmp ();
311 #endif /* __CYGWIN__ */
313 /* Wait forever if we are debugging a login shell. */
314 while (debugging_login_shell
);
316 set_default_locale ();
318 running_setuid
= uidget ();
320 if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC"))
323 #if defined (USE_GNU_MALLOC_LIBRARY)
324 mcheck (programming_error
, (void (*) ())0);
325 #endif /* USE_GNU_MALLOC_LIBRARY */
327 if (setjmp (subshell_top_level
))
329 argc
= subshell_argc
;
330 argv
= subshell_argv
;
335 shell_reinitialized
= 0;
337 /* Initialize `local' variables for all `invocations' of main (). */
339 local_pending_command
= (char *)NULL
;
340 want_pending_command
= locally_skip_execution
= read_from_stdin
= 0;
341 default_input
= stdin
;
342 #if defined (BUFFERED_INPUT)
343 default_buffered_input
= -1;
346 /* Fix for the `infinite process creation' bug when running shell scripts
347 from startup files on System V. */
348 login_shell
= make_login_shell
= 0;
350 /* If this shell has already been run, then reinitialize it to a
352 if (shell_initialized
|| shell_name
)
354 /* Make sure that we do not infinitely recurse as a login shell. */
355 if (*shell_name
== '-')
358 shell_reinitialize ();
359 if (setjmp (top_level
))
363 shell_environment
= env
;
364 set_shell_name (argv
[0]);
365 shell_start_time
= NOW
; /* NOW now defined in general.h */
367 /* Parse argument flags from the input line. */
369 /* Find full word arguments first. */
370 arg_index
= parse_long_options (argv
, arg_index
, argc
);
372 if (want_initial_help
)
374 show_shell_usage (stdout
, 1);
375 exit (EXECUTION_SUCCESS
);
380 show_shell_version (1);
381 exit (EXECUTION_SUCCESS
);
384 /* If user supplied the "--login" flag, then set and invert LOGIN_SHELL. */
385 if (make_login_shell
)
388 login_shell
= -login_shell
;
391 /* All done with full word options; do standard shell option parsing.*/
392 this_command_name
= shell_name
; /* for error reporting */
393 arg_index
= parse_shell_options (argv
, arg_index
, argc
);
396 dump_translatable_strings
= 1;
398 if (dump_translatable_strings
)
399 read_but_dont_execute
= 1;
401 if (running_setuid
&& privileged_mode
== 0)
402 disable_priv_mode ();
404 /* Need to get the argument to a -c option processed in the
405 above loop. The next arg is a command to execute, and the
406 following args are $0...$n respectively. */
407 if (want_pending_command
)
409 local_pending_command
= argv
[arg_index
];
410 if (local_pending_command
== 0)
412 report_error ("option `-c' requires an argument");
417 this_command_name
= (char *)NULL
;
419 /* First, let the outside world know about our interactive status.
420 A shell is interactive if the `-i' flag was given, or if all of
421 the following conditions are met:
423 no arguments remaining or the -s flag given
424 standard input is a terminal
425 standard output is a terminal
426 Refer to Posix.2, the description of the `sh' utility. */
428 if (forced_interactive
|| /* -i flag */
429 (!local_pending_command
&& /* No -c command and ... */
430 wordexp_only
== 0 && /* No --wordexp and ... */
431 ((arg_index
== argc
) || /* no remaining args or... */
432 read_from_stdin
) && /* -s flag with args, and */
433 isatty (fileno (stdin
)) && /* Input is a terminal and */
434 isatty (fileno (stdout
)))) /* output is a terminal. */
437 init_noninteractive ();
439 #define CLOSE_FDS_AT_LOGIN
440 #if defined (CLOSE_FDS_AT_LOGIN)
442 * Some systems have the bad habit of starting login shells with lots of open
443 * file descriptors. For instance, most systems that have picked up the
444 * pre-4.0 Sun YP code leave a file descriptor open each time you call one
445 * of the getpw* functions, and it's set to be open across execs. That
446 * means one for login, one for xterm, one for shelltool, etc.
448 if (login_shell
&& interactive_shell
)
450 for (i
= 3; i
< 20; i
++)
453 #endif /* CLOSE_FDS_AT_LOGIN */
455 /* If we're in a strict Posix.2 mode, turn on interactive comments and
456 other Posix.2 things. */
459 bind_variable ("POSIXLY_CORRECT", "y");
460 sv_strict_posix ("POSIXLY_CORRECT");
463 /* From here on in, the shell must be a normal functioning shell.
464 Variables from the environment are expected to be set, etc. */
467 set_default_locale_vars ();
469 if (interactive_shell
)
473 term
= getenv ("TERM");
474 no_line_editing
|= term
&& (STREQ (term
, "emacs"));
475 term
= getenv ("EMACS");
476 running_under_emacs
= term
? ((fnmatch ("*term*", term
, 0) == 0) ? 2 : 1)
480 top_level_arg_index
= arg_index
;
481 old_errexit_flag
= exit_immediately_on_error
;
483 /* Give this shell a place to longjmp to before executing the
484 startup files. This allows users to press C-c to abort the
486 code
= setjmp (top_level
);
489 if (code
== EXITPROG
)
490 exit_shell (last_command_exit_value
);
493 #if defined (JOB_CONTROL)
494 /* Reset job control, since run_startup_files turned it off. */
495 set_job_control (interactive_shell
);
497 /* Reset value of `set -e', since it's turned off before running
498 the startup files. */
499 exit_immediately_on_error
+= old_errexit_flag
;
500 locally_skip_execution
++;
504 arg_index
= top_level_arg_index
;
506 /* Execute the start-up scripts. */
508 if (interactive_shell
== 0)
510 makunbound ("PS1", shell_variables
);
511 makunbound ("PS2", shell_variables
);
513 expand_aliases
= posixly_correct
;
517 change_flag ('i', FLAG_ON
);
521 #if defined (RESTRICTED_SHELL)
522 /* Set restricted_shell based on whether the basename of $0 indicates that
523 the shell should be restricted or if the `-r' option was supplied at
525 restricted_shell
= shell_is_restricted (shell_name
);
527 /* If the `-r' option is supplied at invocation, make sure that the shell
528 is not in restricted mode when running the startup files. */
529 saverst
= restricted
;
533 /* The startup files are run with `set -e' temporarily disabled. */
534 if (locally_skip_execution
== 0 && running_setuid
== 0)
536 old_errexit_flag
= exit_immediately_on_error
;
537 exit_immediately_on_error
= 0;
539 run_startup_files ();
540 exit_immediately_on_error
+= old_errexit_flag
;
543 /* If we are invoked as `sh', turn on Posix mode. */
546 bind_variable ("POSIXLY_CORRECT", "y");
547 sv_strict_posix ("POSIXLY_CORRECT");
550 #if defined (RESTRICTED_SHELL)
551 /* Turn on the restrictions after executing the startup files. This
552 means that `bash -r' or `set -r' invoked from a startup file will
553 turn on the restrictions after the startup files are executed. */
554 restricted
= saverst
|| restricted
;
555 if (shell_reinitialized
== 0)
556 maybe_make_restricted (shell_name
);
557 #endif /* RESTRICTED_SHELL */
562 last_command_exit_value
= run_wordexp (argv
[arg_index
]);
563 exit_shell (last_command_exit_value
);
566 if (local_pending_command
)
568 arg_index
= bind_args (argv
, arg_index
, argc
, 0);
570 #if defined (ONESHOT)
571 run_one_command (local_pending_command
);
572 exit_shell (last_command_exit_value
);
574 with_input_from_string (local_pending_command
, "-c");
575 goto read_and_execute
;
576 #endif /* !ONESHOT */
579 /* Get possible input filename and set up default_buffered_input or
580 default_input as appropriate. */
581 if (arg_index
!= argc
&& read_from_stdin
== 0)
583 open_shell_script (argv
[arg_index
]);
586 else if (interactive
== 0)
587 /* In this mode, bash is reading a script from stdin, which is a
588 pipe or redirected file. */
589 #if defined (BUFFERED_INPUT)
590 default_buffered_input
= fileno (stdin
); /* == 0 */
592 setbuf (default_input
, (char *)NULL
);
593 #endif /* !BUFFERED_INPUT */
597 /* Bind remaining args to $1 ... $n */
598 arg_index
= bind_args (argv
, arg_index
, argc
, 1);
599 /* Do the things that should be done only for interactive shells. */
600 if (interactive_shell
)
602 /* Set up for checking for presence of mail. */
603 remember_mail_dates ();
606 #if defined (HISTORY)
607 /* Initialize the interactive history stuff. */
608 bash_initialize_history ();
609 if (shell_initialized
== 0)
613 /* Initialize terminal state for interactive shells after the
614 .bash_profile and .bashrc are interpreted. */
618 #if !defined (ONESHOT)
620 #endif /* !ONESHOT */
622 shell_initialized
= 1;
624 /* Read commands until exit condition. */
626 exit_shell (last_command_exit_value
);
630 parse_long_options (argv
, arg_start
, arg_end
)
632 int arg_start
, arg_end
;
634 int arg_index
, longarg
, i
;
637 arg_index
= arg_start
;
638 while ((arg_index
!= arg_end
) && (arg_string
= argv
[arg_index
]) &&
639 (*arg_string
== '-'))
643 /* Make --login equivalent to -login. */
644 if (arg_string
[1] == '-' && arg_string
[2])
650 for (i
= 0; long_args
[i
].name
; i
++)
652 if (STREQ (arg_string
+ 1, long_args
[i
].name
))
654 if (long_args
[i
].type
== Int
)
655 *long_args
[i
].int_value
= 1;
656 else if (argv
[++arg_index
] == 0)
658 report_error ("option `%s' requires an argument",
663 *long_args
[i
].char_value
= argv
[arg_index
];
668 if (long_args
[i
].name
== 0)
672 report_error ("%s: unrecognized option", argv
[arg_index
]);
673 show_shell_usage (stderr
, 0);
676 break; /* No such argument. Maybe flag arg. */
686 parse_shell_options (argv
, arg_start
, arg_end
)
688 int arg_start
, arg_end
;
691 int arg_character
, on_or_off
, next_arg
, i
;
692 char *o_option
, *arg_string
;
694 arg_index
= arg_start
;
695 while (arg_index
!= arg_end
&& (arg_string
= argv
[arg_index
]) &&
696 (*arg_string
== '-' || *arg_string
== '+'))
698 /* There are flag arguments, so parse them. */
699 next_arg
= arg_index
+ 1;
701 /* A single `-' signals the end of options. From the 4.3 BSD sh.
702 An option `--' means the same thing; this is the standard
703 getopt(3) meaning. */
704 if (arg_string
[0] == '-' &&
705 (arg_string
[1] == '\0' ||
706 (arg_string
[1] == '-' && arg_string
[2] == '\0')))
710 on_or_off
= arg_string
[0];
711 while (arg_character
= arg_string
[i
++])
713 switch (arg_character
)
716 want_pending_command
= 1;
724 o_option
= argv
[next_arg
];
727 list_minus_o_opts (-1, (on_or_off
== '-') ? 0 : 1);
730 if (set_minus_o_option (on_or_off
, o_option
) != EXECUTION_SUCCESS
)
736 dump_translatable_strings
= 1;
740 if (change_flag (arg_character
, on_or_off
) == FLAG_ERROR
)
742 report_error ("%c%c: unrecognized option", on_or_off
, arg_character
);
743 show_shell_usage (stderr
, 0);
748 /* Can't do just a simple increment anymore -- what about
749 "bash -abouo emacs ignoreeof -hP"? */
750 arg_index
= next_arg
;
756 /* Exit the shell with status S. */
761 /* Do trap[0] if defined. Allow it to override the exit status
763 if (signal_is_trapped (0))
764 s
= run_exit_trap ();
766 #if defined (PROCESS_SUBSTITUTION)
768 #endif /* PROCESS_SUBSTITUTION */
770 #if defined (HISTORY)
771 if (interactive_shell
)
772 maybe_save_shell_history ();
775 #if defined (JOB_CONTROL)
776 /* If the user has run `shopt -s huponexit', hangup all jobs when we exit
777 an interactive login shell. ksh does this unconditionally. */
778 if (interactive_shell
&& login_shell
&& hup_on_exit
)
781 /* If this shell is interactive, terminate all stopped jobs and
782 restore the original terminal process group. Don't do this if we're
783 in a subshell and calling exit_shell after, for example, a failed
785 if (subshell_environment
== 0)
787 #endif /* JOB_CONTROL */
789 /* Always return the exit status of the last command to our parent. */
793 /* A wrapper for exit that (optionally) can do other things, like malloc
794 statistics tracing. */
802 /* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey
803 the Posix.2 startup file rules: $ENV is expanded, and if the file it
804 names exists, that file is sourced. The Posix.2 rules are in effect
805 for interactive shells only. (section 4.56.5.3) */
807 /* Execute ~/.bashrc for most shells. Never execute it if
808 ACT_LIKE_SH is set, or if NO_RC is set.
810 If the executable file "/usr/gnu/src/bash/foo" contains:
817 COMMAND EXECUTE BASHRC
818 --------------------------------
822 rsh machine ls YES (for rsh, which calls `bash -c')
823 rsh machine foo YES (for shell started by rsh) NO (for foo!)
830 execute_env_file (env_file
)
836 if (env_file
&& *env_file
)
838 list
= expand_string_unsplit (env_file
, Q_DOUBLE_QUOTES
);
841 fn
= string_list (list
);
842 dispose_words (list
);
845 maybe_execute_file (fn
, 1);
854 #if defined (JOB_CONTROL)
857 int sourced_login
, run_by_ssh
;
859 /* get the rshd/sshd case out of the way first. */
860 if (interactive_shell
== 0 && no_rc
== 0 && login_shell
== 0 &&
861 act_like_sh
== 0 && local_pending_command
)
863 run_by_ssh
= find_variable ("SSH_CLIENT") != (SHELL_VAR
*)0;
864 run_by_ssh
|= find_variable ("SSH2_CLIENT") != (SHELL_VAR
*)0;
866 /* If we were run by sshd or we think we were run by rshd, execute
867 ~/.bashrc if we are a top-level shell. */
868 if ((run_by_ssh
|| isnetconn (fileno (stdin
))) && shell_level
< 2)
871 # if defined (__OPENNT)
872 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
874 maybe_execute_file (SYS_BASHRC
, 1);
877 maybe_execute_file (bashrc_file
, 1);
882 #if defined (JOB_CONTROL)
883 /* Startup files should be run without job control enabled. */
884 old_job_control
= interactive_shell
? set_job_control (0) : 0;
889 /* A shell begun with the --login flag that is not in posix mode runs
890 the login shell startup files, no matter whether or not it is
891 interactive. If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
892 startup files if argv[0][0] == '-' as well. */
893 #if defined (NON_INTERACTIVE_LOGIN_SHELLS)
894 if (login_shell
&& posixly_correct
== 0)
896 if (login_shell
< 0 && posixly_correct
== 0)
899 /* We don't execute .bashrc for login shells. */
902 /* Execute /etc/profile and one of the personal login shell
903 initialization files. */
906 maybe_execute_file (SYS_PROFILE
, 1);
908 if (act_like_sh
) /* sh */
909 maybe_execute_file ("~/.profile", 1);
910 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
911 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
912 maybe_execute_file ("~/.profile", 1);
918 /* A non-interactive shell not named `sh' and not in posix mode reads and
919 executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd'
920 and `-su' as the name of the shell, we want to read the startup files.
921 No other non-interactive shells read any startup files. */
922 if (interactive_shell
== 0 && !(su_shell
&& login_shell
))
924 if (posixly_correct
== 0 && act_like_sh
== 0 && privileged_mode
== 0 &&
926 execute_env_file (get_string_value ("BASH_ENV"));
930 /* Interactive shell or `-su' shell. */
931 if (posixly_correct
== 0) /* bash, sh */
933 if (login_shell
&& sourced_login
++ == 0)
935 /* We don't execute .bashrc for login shells. */
938 /* Execute /etc/profile and one of the personal login shell
939 initialization files. */
942 maybe_execute_file (SYS_PROFILE
, 1);
944 if (act_like_sh
) /* sh */
945 maybe_execute_file ("~/.profile", 1);
946 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
947 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
948 maybe_execute_file ("~/.profile", 1);
953 if (act_like_sh
== 0 && no_rc
== 0)
956 # if defined (__OPENNT)
957 maybe_execute_file (_prefixInstallPath(SYS_BASHRC
, NULL
, 0), 1);
959 maybe_execute_file (SYS_BASHRC
, 1);
962 maybe_execute_file (bashrc_file
, 1);
965 else if (act_like_sh
&& privileged_mode
== 0 && sourced_env
++ == 0)
966 execute_env_file (get_string_value ("ENV"));
968 else /* bash --posix, sh --posix */
971 if (interactive_shell
&& privileged_mode
== 0 && sourced_env
++ == 0)
972 execute_env_file (get_string_value ("ENV"));
975 #if defined (JOB_CONTROL)
976 set_job_control (old_job_control
);
980 #if defined (RESTRICTED_SHELL)
981 /* Return 1 if the shell should be a restricted one based on NAME or the
982 value of `restricted'. Don't actually do anything, just return a
985 shell_is_restricted (name
)
992 temp
= base_pathname (name
);
993 return (STREQ (temp
, RESTRICTED_SHELL_NAME
));
996 /* Perhaps make this shell a `restricted' one, based on NAME. If the
997 basename of NAME is "rbash", then this shell is restricted. The
998 name of the restricted shell is a configurable option, see config.h.
999 In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
1001 Do this also if `restricted' is already set to 1; maybe the shell was
1004 maybe_make_restricted (name
)
1009 temp
= base_pathname (shell_name
);
1010 if (restricted
|| (STREQ (temp
, RESTRICTED_SHELL_NAME
)))
1012 set_var_read_only ("PATH");
1013 set_var_read_only ("SHELL");
1014 set_var_read_only ("ENV");
1015 set_var_read_only ("BASH_ENV");
1018 return (restricted
);
1020 #endif /* RESTRICTED_SHELL */
1022 /* Fetch the current set of uids and gids and return 1 if we're running
1023 setuid or setgid. */
1030 if (current_user
.uid
!= u
)
1032 FREE (current_user
.user_name
);
1033 FREE (current_user
.shell
);
1034 FREE (current_user
.home_dir
);
1035 current_user
.user_name
= current_user
.shell
= current_user
.home_dir
= (char *)NULL
;
1037 current_user
.uid
= u
;
1038 current_user
.gid
= getgid ();
1039 current_user
.euid
= geteuid ();
1040 current_user
.egid
= getegid ();
1042 /* See whether or not we are running setuid or setgid. */
1043 return (current_user
.uid
!= current_user
.euid
) ||
1044 (current_user
.gid
!= current_user
.egid
);
1048 disable_priv_mode ()
1050 setuid (current_user
.uid
);
1051 setgid (current_user
.gid
);
1052 current_user
.euid
= current_user
.uid
;
1053 current_user
.egid
= current_user
.gid
;
1062 WORD_LIST
*wl
, *result
;
1064 code
= setjmp (top_level
);
1066 if (code
!= NOT_JUMPED
)
1070 /* Some kind of throw to top_level has occured. */
1072 return last_command_exit_value
= 127;
1074 return last_command_exit_value
;
1076 return last_command_exit_value
= 1;
1078 command_error ("run_wordexp", CMDERR_BADJUMP
, code
, 0);
1082 /* Run it through the parser to get a list of words and expand them */
1083 if (words
&& *words
)
1085 with_input_from_string (words
, "--wordexp");
1086 if (parse_command () != 0)
1088 if (global_command
== 0)
1093 if (global_command
->type
!= cm_simple
)
1095 wl
= global_command
->value
.Simple
->words
;
1096 result
= wl
? expand_words_no_vars (wl
) : (WORD_LIST
*)0;
1099 result
= (WORD_LIST
*)0;
1101 last_command_exit_value
= 0;
1109 /* Count up the number of words and bytes, and print them. Don't count
1110 the trailing NUL byte. */
1111 for (nw
= nb
= 0, wl
= result
; wl
; wl
= wl
->next
)
1114 nb
+= strlen (wl
->word
->word
);
1116 printf ("%u\n%u\n", nw
, nb
);
1117 /* Print each word on a separate line. This will have to be changed when
1118 the interface to glibc is completed. */
1119 for (wl
= result
; wl
; wl
= wl
->next
)
1120 printf ("%s\n", wl
->word
->word
);
1125 #if defined (ONESHOT)
1126 /* Run one command, given as the argument to the -c option. Tell
1127 parse_and_execute not to fork for a simple command. */
1129 run_one_command (command
)
1134 code
= setjmp (top_level
);
1136 if (code
!= NOT_JUMPED
)
1138 #if defined (PROCESS_SUBSTITUTION)
1139 unlink_fifo_list ();
1140 #endif /* PROCESS_SUBSTITUTION */
1143 /* Some kind of throw to top_level has occured. */
1145 return last_command_exit_value
= 127;
1147 return last_command_exit_value
;
1149 return last_command_exit_value
= 1;
1151 command_error ("run_one_command", CMDERR_BADJUMP
, code
, 0);
1154 return (parse_and_execute (savestring (command
), "-c", SEVAL_NOHIST
));
1156 #endif /* ONESHOT */
1159 bind_args (argv
, arg_start
, arg_end
, start_index
)
1161 int arg_start
, arg_end
, start_index
;
1166 for (i
= arg_start
, args
= (WORD_LIST
*)NULL
; i
!= arg_end
; i
++)
1167 args
= make_word_list (make_word (argv
[i
]), args
);
1170 args
= REVERSE_LIST (args
, WORD_LIST
*);
1171 if (start_index
== 0) /* bind to $0...$n for sh -c command */
1173 /* Posix.2 4.56.3 says that the first argument after sh -c command
1174 becomes $0, and the rest of the arguments become $1...$n */
1175 shell_name
= savestring (args
->word
->word
);
1176 FREE (dollar_vars
[0]);
1177 dollar_vars
[0] = savestring (args
->word
->word
);
1178 remember_args (args
->next
, 1);
1180 else /* bind to $1...$n for shell script */
1181 remember_args (args
, 1);
1183 dispose_words (args
);
1192 remember_args ((WORD_LIST
*)NULL
, 1);
1196 open_shell_script (script_name
)
1199 int fd
, e
, fd_is_tty
;
1200 char *filename
, *path_filename
;
1201 unsigned char sample
[80];
1205 free (dollar_vars
[0]);
1206 dollar_vars
[0] = savestring (script_name
);
1207 filename
= savestring (script_name
);
1209 fd
= open (filename
, O_RDONLY
);
1210 if ((fd
< 0) && (errno
== ENOENT
) && (absolute_program (filename
) == 0))
1213 /* If it's not in the current directory, try looking through PATH
1215 path_filename
= find_path_file (script_name
);
1219 filename
= path_filename
;
1220 fd
= open (filename
, O_RDONLY
);
1229 file_error (filename
);
1230 exit ((e
== ENOENT
) ? EX_NOTFOUND
: EX_NOINPUT
);
1234 fd_is_tty
= isatty (fd
);
1239 /* Only do this with non-tty file descriptors we can seek on. */
1240 if (fd_is_tty
== 0 && (lseek (fd
, 0L, 1) != -1))
1242 /* Check to see if the `file' in `bash file' is a binary file
1243 according to the same tests done by execute_simple_command (),
1244 and report an error and exit if it is. */
1245 sample_len
= read (fd
, sample
, sizeof (sample
));
1249 if ((fstat (fd
, &sb
) == 0) && S_ISDIR (sb
.st_mode
))
1250 internal_error ("%s: is a directory", filename
);
1254 file_error (filename
);
1258 else if (sample_len
> 0 && (check_binary_file (sample
, sample_len
)))
1260 internal_error ("%s: cannot execute binary file", filename
);
1261 exit (EX_BINARY_FILE
);
1263 /* Now rewind the file back to the beginning. */
1267 /* Open the script. But try to move the file descriptor to a randomly
1268 large one, in the hopes that any descriptors used by the script will
1269 not match with ours. */
1270 fd
= move_to_high_fd (fd
, 0, -1);
1272 #if defined (__CYGWIN__) && defined (O_TEXT)
1273 setmode (fd
, O_TEXT
);
1276 #if defined (BUFFERED_INPUT)
1277 default_buffered_input
= fd
;
1278 SET_CLOSE_ON_EXEC (default_buffered_input
);
1279 #else /* !BUFFERED_INPUT */
1280 default_input
= fdopen (fd
, "r");
1282 if (default_input
== 0)
1284 file_error (filename
);
1288 SET_CLOSE_ON_EXEC (fd
);
1289 if (fileno (default_input
) != fd
)
1290 SET_CLOSE_ON_EXEC (fileno (default_input
));
1291 #endif /* !BUFFERED_INPUT */
1293 /* Just about the only way for this code to be executed is if something
1294 like `bash -i /dev/stdin' is executed. */
1295 if (interactive_shell
&& fd_is_tty
)
1300 #if defined (BUFFERED_INPUT)
1301 default_buffered_input
= 0;
1303 fclose (default_input
);
1304 default_input
= stdin
;
1312 /* Initialize the input routines for the parser. */
1316 /* Make sure the fd from which we are reading input is not in
1318 #if defined (BUFFERED_INPUT)
1319 if (interactive
== 0)
1320 sh_unset_nodelay_mode (default_buffered_input
);
1322 #endif /* !BUFFERED_INPUT */
1323 sh_unset_nodelay_mode (fileno (stdin
));
1325 /* with_input_from_stdin really means `with_input_from_readline' */
1326 if (interactive
&& no_line_editing
== 0)
1327 with_input_from_stdin ();
1329 #if defined (BUFFERED_INPUT)
1331 if (interactive
== 0)
1332 with_input_from_buffered_stream (default_buffered_input
, dollar_vars
[0]);
1334 with_input_from_stream (default_input
, dollar_vars
[0]);
1336 #else /* !BUFFERED_INPUT */
1337 with_input_from_stream (default_input
, dollar_vars
[0]);
1338 #endif /* !BUFFERED_INPUT */
1341 /* Close the current shell script input source and forget about it. This is
1342 extern so execute_cmd.c:initialize_subshell() can call it. If CHECK_ZERO
1343 is non-zero, we close default_buffered_input even if it's the standard
1346 unset_bash_input (check_zero
)
1349 #if defined (BUFFERED_INPUT)
1350 if ((check_zero
&& default_buffered_input
>= 0) ||
1351 (check_zero
== 0 && default_buffered_input
> 0))
1353 close_buffered_fd (default_buffered_input
);
1354 default_buffered_input
= bash_input
.location
.buffered_fd
= -1;
1356 #else /* !BUFFERED_INPUT */
1359 fclose (default_input
);
1360 default_input
= (FILE *)NULL
;
1362 #endif /* !BUFFERED_INPUT */
1366 #if !defined (PROGRAM)
1367 # define PROGRAM "bash"
1371 set_shell_name (argv0
)
1374 /* Here's a hack. If the name of this shell is "sh", then don't do
1375 any startup files; just try to be more like /bin/sh. */
1376 shell_name
= base_pathname (argv0
);
1377 if (*shell_name
== '-')
1379 if (shell_name
[0] == 's' && shell_name
[1] == 'h' && shell_name
[2] == '\0')
1381 if (shell_name
[0] == 's' && shell_name
[1] == 'u' && shell_name
[2] == '\0')
1385 FREE (dollar_vars
[0]);
1386 dollar_vars
[0] = savestring (shell_name
);
1388 if (*shell_name
== '-')
1394 /* A program may start an interactive shell with
1395 "execl ("/bin/bash", "-", NULL)".
1396 If so, default the name of this shell to our name. */
1397 if (!shell_name
|| !*shell_name
|| (shell_name
[0] == '-' && !shell_name
[1]))
1398 shell_name
= PROGRAM
;
1404 interactive_shell
= startup_state
= interactive
= 1;
1409 init_noninteractive ()
1411 #if defined (HISTORY)
1412 bash_history_reinit (0);
1413 #endif /* HISTORY */
1414 interactive_shell
= startup_state
= interactive
= 0;
1416 no_line_editing
= 1;
1417 #if defined (JOB_CONTROL)
1418 set_job_control (0);
1419 #endif /* JOB_CONTROL */
1423 get_current_user_info ()
1425 struct passwd
*entry
;
1427 /* Don't fetch this more than once. */
1428 if (current_user
.user_name
== 0)
1430 entry
= getpwuid (current_user
.uid
);
1433 current_user
.user_name
= savestring (entry
->pw_name
);
1434 current_user
.shell
= (entry
->pw_shell
&& entry
->pw_shell
[0])
1435 ? savestring (entry
->pw_shell
)
1436 : savestring ("/bin/sh");
1437 current_user
.home_dir
= savestring (entry
->pw_dir
);
1441 current_user
.user_name
= savestring ("I have no name!");
1442 current_user
.shell
= savestring ("/bin/sh");
1443 current_user
.home_dir
= savestring ("/");
1449 /* Do whatever is necessary to initialize the shell.
1450 Put new initializations in here. */
1456 /* Line buffer output for stderr and stdout. */
1457 if (shell_initialized
== 0)
1459 sh_setlinebuf (stderr
);
1460 sh_setlinebuf (stdout
);
1463 /* Sort the array of shell builtins so that the binary search in
1464 find_shell_builtin () works correctly. */
1465 initialize_shell_builtins ();
1467 /* Initialize the trap signal handlers before installing our own
1468 signal handlers. traps.c:restore_original_signals () is responsible
1469 for restoring the original default signal handlers. That function
1470 is called when we make a new child. */
1471 initialize_traps ();
1472 initialize_signals ();
1474 /* It's highly unlikely that this will change. */
1475 if (current_host_name
== 0)
1477 /* Initialize current_host_name. */
1478 if (gethostname (hostname
, 255) < 0)
1479 current_host_name
= "??host??";
1481 current_host_name
= savestring (hostname
);
1484 /* Initialize the stuff in current_user that comes from the password
1485 file. We don't need to do this right away if the shell is not
1487 if (interactive_shell
)
1488 get_current_user_info ();
1490 /* Initialize our interface to the tilde expander. */
1491 tilde_initialize ();
1493 /* Initialize internal and environment variables. Don't import shell
1494 functions from the environment if we are running in privileged or
1495 restricted mode or if the shell is running setuid. */
1496 #if defined (RESTRICTED_SHELL)
1497 initialize_shell_variables (shell_environment
, privileged_mode
||restricted
||running_setuid
);
1499 initialize_shell_variables (shell_environment
, privileged_mode
||running_setuid
);
1503 /* Initialize filename hash tables. */
1504 initialize_filename_hashing ();
1507 /* Initialize the data structures for storing and running jobs. */
1508 initialize_job_control (0);
1510 /* Initialize input streams to null. */
1511 initialize_bash_input ();
1513 /* Initialize the shell options. Don't import the shell options
1514 from the environment variable $SHELLOPTS if we are running in
1515 privileged or restricted mode or if the shell is running setuid. */
1516 #if defined (RESTRICTED_SHELL)
1517 initialize_shell_options (privileged_mode
||restricted
||running_setuid
);
1519 initialize_shell_options (privileged_mode
||running_setuid
);
1523 /* Function called by main () when it appears that the shell has already
1524 had some initialization performed. This is supposed to reset the world
1525 back to a pristine state, as if we had been exec'ed. */
1527 shell_reinitialize ()
1529 /* The default shell prompts. */
1530 primary_prompt
= PPROMPT
;
1531 secondary_prompt
= SPROMPT
;
1533 /* Things that get 1. */
1534 current_command_number
= 1;
1536 /* We have decided that the ~/.bashrc file should not be executed
1537 for the invocation of each shell script. If the variable $ENV
1538 (or $BASH_ENV) is set, its value is used as the name of a file
1540 no_rc
= no_profile
= 1;
1542 /* Things that get 0. */
1543 login_shell
= make_login_shell
= interactive
= executing
= 0;
1544 debugging
= do_version
= line_number
= last_command_exit_value
= 0;
1545 forced_interactive
= interactive_shell
= subshell_environment
= 0;
1548 #if defined (HISTORY)
1549 bash_history_reinit (0);
1550 #endif /* HISTORY */
1552 #if defined (RESTRICTED_SHELL)
1554 #endif /* RESTRICTED_SHELL */
1556 /* Ensure that the default startup file is used. (Except that we don't
1557 execute this file for reinitialized shells). */
1558 bashrc_file
= "~/.bashrc";
1560 /* Delete all variables and functions. They will be reinitialized when
1561 the environment is parsed. */
1562 delete_all_variables (shell_variables
);
1563 delete_all_variables (shell_functions
);
1565 shell_reinitialized
= 1;
1569 show_shell_usage (fp
, extra
)
1574 char *set_opts
, *s
, *t
;
1577 fprintf (fp
, "GNU bash, version %s-(%s)\n", shell_version_string (), MACHTYPE
);
1578 fprintf (fp
, "Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n",
1579 shell_name
, shell_name
);
1580 fputs ("GNU long options:\n", fp
);
1581 for (i
= 0; long_args
[i
].name
; i
++)
1582 fprintf (fp
, "\t--%s\n", long_args
[i
].name
);
1584 fputs ("Shell options:\n", fp
);
1585 fputs ("\t-irsD or -c command\t\t(invocation only)\n", fp
);
1587 for (i
= 0, set_opts
= 0; shell_builtins
[i
].name
; i
++)
1588 if (STREQ (shell_builtins
[i
].name
, "set"))
1589 set_opts
= savestring (shell_builtins
[i
].short_doc
);
1592 s
= strchr (set_opts
, '[');
1597 t
= strchr (s
, ']');
1600 fprintf (fp
, "\t-%s or -o option\n", s
);
1606 fprintf (fp
, "Type `%s -c \"help set\"' for more information about shell options.\n", shell_name
);
1607 fprintf (fp
, "Type `%s -c help' for more information about shell builtin commands.\n", shell_name
);
1608 fprintf (fp
, "Use the `bashbug' command to report bugs.\n");
1612 /* The second and subsequent conditions must match those used to decide
1613 whether or not to call getpeername() in isnetconn(). */
1614 #if defined (HAVE_SYS_SOCKET_H) && defined (HAVE_GETPEERNAME) && !defined (SVR4_2)
1615 # include <sys/socket.h>
1618 /* Is FD a socket or network connection? */
1623 #if defined (HAVE_GETPEERNAME) && !defined (SVR4_2) && !defined (__BEOS__)
1628 rv
= getpeername(fd
, &sa
, &l
);
1629 /* Solaris 2.5 getpeername() returns EINVAL if the fd is not a socket. */
1630 return ((rv
< 0 && (errno
== ENOTSOCK
|| errno
== EINVAL
)) ? 0 : 1);
1631 #else /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */
1632 # if defined (SVR4) || defined (SVR4_2)
1633 /* Sockets on SVR4 and SVR4.2 are character special (streams) devices. */
1638 if (fstat (fd
, &sb
) < 0)
1640 # if defined (S_ISFIFO)
1641 if (S_ISFIFO (sb
.st_mode
))
1643 # endif /* S_ISFIFO */
1644 return (S_ISCHR (sb
.st_mode
));
1645 # else /* !SVR4 && !SVR4_2 */
1646 # if defined (S_ISSOCK) && !defined (__BEOS__)
1649 if (fstat (fd
, &sb
) < 0)
1651 return (S_ISSOCK (sb
.st_mode
));
1652 # else /* !S_ISSOCK || __BEOS__ */
1654 # endif /* !S_ISSOCK || __BEOS__ */
1655 # endif /* !SVR4 && !SVR4_2 */
1656 #endif /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */