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 "posixstat.h"
47 #if defined (HAVE_UNISTD_H)
54 #include "mailcheck.h"
56 #include "builtins/common.h"
58 #if defined (JOB_CONTROL)
60 #endif /* JOB_CONTROL */
63 #include "execute_cmd.h"
66 # include "bashhist.h"
67 # include <readline/history.h>
70 #include <tilde/tilde.h>
71 #include <glob/fnmatch.h>
73 #if !defined (HAVE_GETPW_DECLS)
74 extern struct passwd
*getpwuid ();
75 #endif /* !HAVE_GETPW_DECLS */
81 extern char *dist_version
, *release_status
;
82 extern int patch_level
, build_version
;
83 extern int subshell_environment
;
84 extern int last_command_exit_value
;
85 extern int line_number
;
86 extern char *primary_prompt
, *secondary_prompt
;
87 extern int expand_aliases
;
88 extern char *this_command_name
;
90 /* Non-zero means that this shell has already been run; i.e. you should
91 call shell_reinitialize () if you need to start afresh. */
92 int shell_initialized
= 0;
94 COMMAND
*global_command
= (COMMAND
*)NULL
;
96 /* Information about the current user. */
97 struct user_info current_user
=
99 -1, -1, -1, -1, (char *)NULL
, (char *)NULL
, (char *)NULL
102 /* The current host's name. */
103 char *current_host_name
= (char *)NULL
;
105 /* Non-zero means that this shell is a login shell.
108 1 = login shell from getty (or equivalent fake out)
109 -1 = login shell from "-login" flag.
110 -2 = both from getty, and from flag.
114 /* Non-zero means that at this moment, the shell is interactive. In
115 general, this means that the shell is at this moment reading input
116 from the keyboard. */
119 /* Non-zero means that the shell was started as an interactive shell. */
120 int interactive_shell
= 0;
122 /* Tells what state the shell was in when it started:
123 0 = non-interactive shell script
126 This is a superset of the information provided by interactive_shell.
128 int startup_state
= 0;
130 /* Special debugging helper. */
131 int debugging_login_shell
= 0;
133 /* The environment that the shell passes to other commands. */
134 char **shell_environment
;
136 /* Non-zero when we are executing a top-level command. */
139 /* The number of commands executed so far. */
140 int current_command_number
= 1;
142 /* Non-zero is the recursion depth for commands. */
143 int indirection_level
= 0;
145 /* The name of this shell, as taken from argv[0]. */
146 char *shell_name
= (char *)NULL
;
148 /* time in seconds when the shell was started */
149 time_t shell_start_time
;
151 /* Are we running in an emacs shell window? */
152 int running_under_emacs
;
154 /* The name of the .(shell)rc file. */
155 static char *bashrc_file
= "~/.bashrc";
157 /* Non-zero means to act more like the Bourne shell on startup. */
158 static int act_like_sh
;
160 /* Non-zero if this shell is being run by `su'. */
163 /* Non-zero if we have already expanded and sourced $ENV. */
164 static int sourced_env
;
166 /* Is this shell running setuid? */
167 static int running_setuid
;
169 /* Values for the long-winded argument names. */
170 static int debugging
; /* Do debugging things. */
171 static int no_rc
; /* Don't execute ~/.bashrc */
172 static int no_profile
; /* Don't execute .profile */
173 static int do_version
; /* Display interesting version info. */
174 static int make_login_shell
; /* Make this shell be a `-bash' shell. */
175 static int want_initial_help
; /* --help option */
177 int no_line_editing
= 0; /* Don't do fancy line editing. */
178 int posixly_correct
= 0; /* Non-zero means posix.2 superset. */
179 int dump_translatable_strings
; /* Dump strings in $"...", don't execute. */
181 /* Some long-winded argument names. These are obviously new. */
190 { "debug", Int
, &debugging
, (char **)0x0 },
191 { "dump-strings", Int
, &dump_translatable_strings
, (char **)0x0 },
192 { "help", Int
, &want_initial_help
, (char **)0x0 },
193 { "login", Int
, &make_login_shell
, (char **)0x0 },
194 { "noediting", Int
, &no_line_editing
, (char **)0x0 },
195 { "noprofile", Int
, &no_profile
, (char **)0x0 },
196 { "norc", Int
, &no_rc
, (char **)0x0 },
197 { "posix", Int
, &posixly_correct
, (char **)0x0 },
198 { "rcfile", Charp
, (int *)0x0, &bashrc_file
},
199 #if defined (RESTRICTED_SHELL)
200 { "restricted", Int
, &restricted
, (char **)0x0 },
202 { "verbose", Int
, &echo_input_at_read
, (char **)0x0 },
203 { "version", Int
, &do_version
, (char **)0x0 },
204 { (char *)0x0, Int
, (int *)0x0, (char **)0x0 }
207 /* These are extern so execute_simple_command can set them, and then
208 longjmp back to main to execute a shell script, instead of calling
209 main () again and resulting in indefinite, possibly fatal, stack
211 procenv_t subshell_top_level
;
213 char **subshell_argv
;
214 char **subshell_envp
;
216 #if defined (BUFFERED_INPUT)
217 /* The file descriptor from which the shell is reading input. */
218 int default_buffered_input
= -1;
221 static int read_from_stdin
; /* -s flag supplied */
222 static int want_pending_command
; /* -c flag supplied */
223 static char *local_pending_command
;
225 static FILE *default_input
;
227 static int parse_long_options ();
228 static int parse_shell_options ();
229 static void run_startup_files ();
230 static int bind_args ();
231 static int open_shell_script ();
232 static void set_bash_input ();
233 static int run_one_command ();
235 static int uidget ();
236 static int isnetconn ();
238 static void init_interactive (), init_noninteractive ();
239 static void set_shell_name ();
240 static void shell_initialize ();
241 static void shell_reinitialize ();
243 static void show_shell_usage ();
246 main (argc
, argv
, env
)
252 volatile int locally_skip_execution
;
253 volatile int arg_index
, top_level_arg_index
;
255 /* Catch early SIGINTs. */
256 code
= setjmp (top_level
);
262 /* Wait forever if we are debugging a login shell. */
263 while (debugging_login_shell
);
265 set_default_locale ();
267 running_setuid
= uidget ();
269 posixly_correct
= (getenv ("POSIXLY_CORRECT") != (char *)NULL
) ||
270 (getenv ("POSIX_PEDANTIC") != (char *)NULL
);
272 #if defined (USE_GNU_MALLOC_LIBRARY)
273 mcheck (programming_error
, (void (*) ())0);
274 #endif /* USE_GNU_MALLOC_LIBRARY */
276 if (setjmp (subshell_top_level
))
278 argc
= subshell_argc
;
279 argv
= subshell_argv
;
284 /* Initialize `local' variables for all `invocations' of main (). */
286 local_pending_command
= (char *)NULL
;
287 want_pending_command
= locally_skip_execution
= read_from_stdin
= 0;
288 default_input
= stdin
;
289 #if defined (BUFFERED_INPUT)
290 default_buffered_input
= -1;
293 /* Fix for the `infinite process creation' bug when running shell scripts
294 from startup files on System V. */
295 login_shell
= make_login_shell
= 0;
297 /* If this shell has already been run, then reinitialize it to a
299 if (shell_initialized
|| shell_name
)
301 /* Make sure that we do not infinitely recurse as a login shell. */
302 if (*shell_name
== '-')
305 shell_reinitialize ();
306 if (setjmp (top_level
))
310 shell_environment
= env
;
311 set_shell_name (argv
[0]);
312 shell_start_time
= NOW
; /* NOW now defined in general.h */
314 /* Parse argument flags from the input line. */
316 /* Find full word arguments first. */
317 arg_index
= parse_long_options (argv
, arg_index
, argc
);
319 if (want_initial_help
)
321 show_shell_usage (stdout
, 1);
322 exit (EXECUTION_SUCCESS
);
327 show_shell_version (1);
328 exit (EXECUTION_SUCCESS
);
331 /* If user supplied the "--login" flag, then set and invert LOGIN_SHELL. */
332 if (make_login_shell
)
335 login_shell
= -login_shell
;
338 /* All done with full word options; do standard shell option parsing.*/
339 this_command_name
= shell_name
; /* for error reporting */
340 arg_index
= parse_shell_options (argv
, arg_index
, argc
);
342 if (dump_translatable_strings
)
343 read_but_dont_execute
= 1;
345 if (running_setuid
&& privileged_mode
== 0)
346 disable_priv_mode ();
348 /* Need to get the argument to a -c option processed in the
349 above loop. The next arg is a command to execute, and the
350 following args are $0...$n respectively. */
351 if (want_pending_command
)
353 local_pending_command
= argv
[arg_index
];
354 if (local_pending_command
== 0)
356 report_error ("option `-c' requires an argument");
361 this_command_name
= (char *)NULL
;
363 /* First, let the outside world know about our interactive status.
364 A shell is interactive if the `-i' flag was given, or if all of
365 the following conditions are met:
367 no arguments remaining or the -s flag given
368 standard input is a terminal
369 standard output is a terminal
370 Refer to Posix.2, the description of the `sh' utility. */
372 if (forced_interactive
|| /* -i flag */
373 (!local_pending_command
&& /* No -c command and ... */
374 ((arg_index
== argc
) || /* no remaining args or... */
375 read_from_stdin
) && /* -s flag with args, and */
376 isatty (fileno (stdin
)) && /* Input is a terminal and */
377 isatty (fileno (stdout
)))) /* output is a terminal. */
380 init_noninteractive ();
382 #define CLOSE_FDS_AT_LOGIN
383 #if defined (CLOSE_FDS_AT_LOGIN)
385 * Some systems have the bad habit of starting login shells with lots of open
386 * file descriptors. For instance, most systems that have picked up the
387 * pre-4.0 Sun YP code leave a file descriptor open each time you call one
388 * of the getpw* functions, and it's set to be open across execs. That
389 * means one for login, one for xterm, one for shelltool, etc.
391 if (login_shell
&& interactive_shell
)
393 for (i
= 3; i
< 20; i
++)
396 #endif /* CLOSE_FDS_AT_LOGIN */
398 /* If we're in a strict Posix.2 mode, turn on interactive comments and
399 other Posix.2 things. */
402 posix_initialize (posixly_correct
);
403 #if defined (READLINE)
404 if (interactive_shell
)
405 posix_readline_initialize (posixly_correct
);
409 /* From here on in, the shell must be a normal functioning shell.
410 Variables from the environment are expected to be set, etc. */
413 set_default_locale_vars ();
415 if (interactive_shell
)
419 term
= getenv ("TERM");
420 no_line_editing
|= term
&& (STREQ (term
, "emacs"));
421 term
= getenv ("EMACS");
422 running_under_emacs
= term
? ((fnmatch ("*term*", term
, 0) == 0) ? 2 : 1)
426 top_level_arg_index
= arg_index
;
428 /* Give this shell a place to longjmp to before executing the
429 startup files. This allows users to press C-c to abort the
431 code
= setjmp (top_level
);
434 if (code
== EXITPROG
)
435 exit_shell (last_command_exit_value
);
438 #if defined (JOB_CONTROL)
439 /* Reset job control, since run_startup_files turned it off. */
440 set_job_control (interactive_shell
);
442 locally_skip_execution
++;
446 arg_index
= top_level_arg_index
;
448 /* Execute the start-up scripts. */
450 if (interactive_shell
== 0)
452 makunbound ("PS1", shell_variables
);
453 makunbound ("PS2", shell_variables
);
454 interactive
= expand_aliases
= 0;
458 change_flag ('i', FLAG_ON
);
462 #if defined (RESTRICTED_SHELL)
463 /* If the `-r' option is supplied at invocation, make sure that the shell
464 is not in restricted mode when running the startup files. */
465 saverst
= restricted
;
469 if (locally_skip_execution
== 0 && running_setuid
== 0)
470 run_startup_files ();
472 /* If we are invoked as `sh', turn on Posix mode. */
475 posix_initialize (posixly_correct
= 1);
476 #if defined (READLINE)
477 if (interactive_shell
)
478 posix_readline_initialize (posixly_correct
);
482 #if defined (RESTRICTED_SHELL)
483 /* Turn on the restrictions after parsing the startup files. This
484 means that `bash -r' or `set -r' invoked from a startup file will
485 turn on the restrictions after the startup files are executed. */
486 restricted
= saverst
|| restricted
;
487 maybe_make_restricted (shell_name
);
488 #endif /* RESTRICTED_SHELL */
490 if (local_pending_command
)
492 arg_index
= bind_args (argv
, arg_index
, argc
, 0);
495 #if defined (ONESHOT)
496 run_one_command (local_pending_command
);
497 exit_shell (last_command_exit_value
);
499 with_input_from_string (local_pending_command
, "-c");
500 goto read_and_execute
;
501 #endif /* !ONESHOT */
504 /* Get possible input filename and set up default_buffered_input or
505 default_input as appropriate. */
506 if (arg_index
!= argc
&& read_from_stdin
== 0)
508 open_shell_script (argv
[arg_index
]);
511 else if (interactive
== 0)
512 /* In this mode, bash is reading a script from stdin, which is a
513 pipe or redirected file. */
514 #if defined (BUFFERED_INPUT)
515 default_buffered_input
= fileno (stdin
); /* == 0 */
517 setbuf (default_input
, (char *)NULL
);
518 #endif /* !BUFFERED_INPUT */
522 /* Bind remaining args to $1 ... $n */
523 arg_index
= bind_args (argv
, arg_index
, argc
, 1);
525 /* Do the things that should be done only for interactive shells. */
526 if (interactive_shell
)
528 /* Set up for checking for presence of mail. */
529 remember_mail_dates ();
532 #if defined (HISTORY)
533 /* Initialize the interactive history stuff. */
534 bash_initialize_history ();
535 if (shell_initialized
== 0)
539 /* Initialize terminal state for interactive shells after the
540 .bash_profile and .bashrc are interpreted. */
544 #if !defined (ONESHOT)
546 #endif /* !ONESHOT */
548 shell_initialized
= 1;
550 /* Read commands until exit condition. */
552 exit_shell (last_command_exit_value
);
556 parse_long_options (argv
, arg_start
, arg_end
)
558 int arg_start
, arg_end
;
560 int arg_index
, longarg
, i
;
563 arg_index
= arg_start
;
564 while ((arg_index
!= arg_end
) && (arg_string
= argv
[arg_index
]) &&
565 (*arg_string
== '-'))
569 /* Make --login equivalent to -login. */
570 if (arg_string
[1] == '-' && arg_string
[2])
576 for (i
= 0; long_args
[i
].name
; i
++)
578 if (STREQ (arg_string
+ 1, long_args
[i
].name
))
580 if (long_args
[i
].type
== Int
)
581 *long_args
[i
].int_value
= 1;
582 else if (argv
[++arg_index
] == 0)
584 report_error ("option `%s' requires an argument",
589 *long_args
[i
].char_value
= argv
[arg_index
];
594 if (long_args
[i
].name
== 0)
598 report_error ("%s: unrecognized option", argv
[arg_index
]);
599 show_shell_usage (stderr
, 0);
602 break; /* No such argument. Maybe flag arg. */
612 parse_shell_options (argv
, arg_start
, arg_end
)
614 int arg_start
, arg_end
;
617 int arg_character
, on_or_off
, next_arg
, i
;
618 char *o_option
, *arg_string
;
620 arg_index
= arg_start
;
621 while (arg_index
!= arg_end
&& (arg_string
= argv
[arg_index
]) &&
622 (*arg_string
== '-' || *arg_string
== '+'))
624 /* There are flag arguments, so parse them. */
625 next_arg
= arg_index
+ 1;
627 /* A single `-' signals the end of options. From the 4.3 BSD sh.
628 An option `--' means the same thing; this is the standard
629 getopt(3) meaning. */
630 if (arg_string
[0] == '-' &&
631 (arg_string
[1] == '\0' ||
632 (arg_string
[1] == '-' && arg_string
[2] == '\0')))
636 on_or_off
= arg_string
[0];
637 while (arg_character
= arg_string
[i
++])
639 switch (arg_character
)
642 want_pending_command
= 1;
650 o_option
= argv
[next_arg
];
653 list_minus_o_opts (-1);
656 if (set_minus_o_option (on_or_off
, o_option
) != EXECUTION_SUCCESS
)
662 dump_translatable_strings
= 1;
666 if (change_flag (arg_character
, on_or_off
) == FLAG_ERROR
)
668 report_error ("%c%c: unrecognized option", on_or_off
, arg_character
);
669 show_shell_usage (stderr
, 0);
674 /* Can't do just a simple increment anymore -- what about
675 "bash -abouo emacs ignoreeof -hP"? */
676 arg_index
= next_arg
;
682 /* Exit the shell with status S. */
687 /* Do trap[0] if defined. Allow it to override the exit status
689 if (signal_is_trapped (0))
690 s
= run_exit_trap ();
692 #if defined (PROCESS_SUBSTITUTION)
694 #endif /* PROCESS_SUBSTITUTION */
696 #if defined (HISTORY)
697 if (interactive_shell
)
698 maybe_save_shell_history ();
701 #if defined (JOB_CONTROL)
702 /* If this shell is interactive, terminate all stopped jobs and
703 restore the original terminal process group. */
705 #endif /* JOB_CONTROL */
707 /* Always return the exit status of the last command to our parent. */
711 /* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey
712 the Posix.2 startup file rules: $ENV is expanded, and if the file it
713 names exists, that file is sourced. The Posix.2 rules are in effect
714 for interactive shells only. (section 4.56.5.3) */
716 /* Execute ~/.bashrc for most shells. Never execute it if
717 ACT_LIKE_SH is set, or if NO_RC is set.
719 If the executable file "/usr/gnu/src/bash/foo" contains:
726 COMMAND EXECUTE BASHRC
727 --------------------------------
731 rsh machine ls YES (for rsh, which calls `bash -c')
732 rsh machine foo YES (for shell started by rsh) NO (for foo!)
739 execute_env_file (env_file
)
745 if (env_file
&& *env_file
)
747 list
= expand_string_unsplit (env_file
, Q_DOUBLE_QUOTES
);
750 fn
= string_list (list
);
751 dispose_words (list
);
754 maybe_execute_file (fn
, 1);
763 #if defined (JOB_CONTROL)
767 /* get the rshd case out of the way first. */
768 if (interactive_shell
== 0 && no_rc
== 0 && login_shell
== 0 &&
769 act_like_sh
== 0 && local_pending_command
&& isnetconn (fileno (stdin
)))
772 maybe_execute_file (SYS_BASHRC
, 1);
774 maybe_execute_file (bashrc_file
, 1);
778 /* A non-interactive shell not named `sh' and not in posix mode reads and
779 executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd'
780 and `-su' as the name of the shell, we want to read the startup files.
781 No other non-interactive shells read any startup files. */
782 if (interactive_shell
== 0 && !(su_shell
&& login_shell
))
784 if (posixly_correct
== 0 && act_like_sh
== 0 && privileged_mode
== 0 &&
786 execute_env_file (get_string_value ("BASH_ENV"));
790 #if defined (JOB_CONTROL)
791 /* Startup files should be run without job control enabled. */
792 old_job_control
= set_job_control (0);
795 /* Interactive shell or `-su' shell. */
796 if (posixly_correct
== 0) /* bash, sh */
798 /* We don't execute .bashrc for login shells. */
802 /* Execute /etc/profile and one of the personal login shell
803 initialization files. */
804 if (login_shell
&& no_profile
== 0)
806 maybe_execute_file (SYS_PROFILE
, 1);
808 if (act_like_sh
) /* sh */
809 maybe_execute_file ("~/.profile", 1);
810 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
811 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
812 maybe_execute_file ("~/.profile", 1);
816 if (act_like_sh
== 0 && no_rc
== 0)
819 maybe_execute_file (SYS_BASHRC
, 1);
821 maybe_execute_file (bashrc_file
, 1);
824 else if (act_like_sh
&& privileged_mode
== 0 && sourced_env
++ == 0)
825 execute_env_file (get_string_value ("ENV"));
827 else /* bash --posix, sh --posix */
830 if (interactive_shell
&& privileged_mode
== 0 && sourced_env
++ == 0)
831 execute_env_file (get_string_value ("ENV"));
834 #if defined (JOB_CONTROL)
835 set_job_control (old_job_control
);
839 #if defined (RESTRICTED_SHELL)
840 /* Perhaps make this shell a `restricted' one, based on NAME. If the
841 basename of NAME is "rbash", then this shell is restricted. The
842 name of the restricted shell is a configurable option, see config.h.
843 In a restricted shell, PATH and SHELL are read-only and non-unsettable.
844 Do this also if `restricted' is already set to 1; maybe the shell was
847 maybe_make_restricted (name
)
852 temp
= base_pathname (shell_name
);
853 if (restricted
|| (STREQ (temp
, RESTRICTED_SHELL_NAME
)))
855 set_var_read_only ("PATH");
856 set_var_read_only ("SHELL");
861 #endif /* RESTRICTED_SHELL */
863 /* Fetch the current set of uids and gids and return 1 if we're running
871 if (current_user
.uid
!= u
)
873 FREE (current_user
.user_name
);
874 FREE (current_user
.shell
);
875 FREE (current_user
.home_dir
);
876 current_user
.user_name
= current_user
.shell
= current_user
.home_dir
= (char *)NULL
;
878 current_user
.uid
= u
;
879 current_user
.gid
= getgid ();
880 current_user
.euid
= geteuid ();
881 current_user
.egid
= getegid ();
883 /* See whether or not we are running setuid or setgid. */
884 return (current_user
.uid
!= current_user
.euid
) ||
885 (current_user
.gid
!= current_user
.egid
);
891 setuid (current_user
.uid
);
892 setgid (current_user
.gid
);
893 current_user
.euid
= current_user
.uid
;
894 current_user
.egid
= current_user
.gid
;
897 #if defined (ONESHOT)
898 /* Run one command, given as the argument to the -c option. Tell
899 parse_and_execute not to fork for a simple command. */
901 run_one_command (command
)
906 code
= setjmp (top_level
);
908 if (code
!= NOT_JUMPED
)
910 #if defined (PROCESS_SUBSTITUTION)
912 #endif /* PROCESS_SUBSTITUTION */
915 /* Some kind of throw to top_level has occured. */
917 return last_command_exit_value
= 127;
919 return last_command_exit_value
;
921 return last_command_exit_value
= 1;
923 programming_error ("run_one_command: bad jump: code %d", code
);
926 return (parse_and_execute (savestring (command
), "-c", SEVAL_NOHIST
));
931 bind_args (argv
, arg_start
, arg_end
, start_index
)
933 int arg_start
, arg_end
, start_index
;
938 for (i
= arg_start
, args
= (WORD_LIST
*)NULL
; i
!= arg_end
; i
++)
939 args
= make_word_list (make_word (argv
[i
]), args
);
942 args
= REVERSE_LIST (args
, WORD_LIST
*);
943 if (start_index
== 0) /* bind to $0...$n for sh -c command */
945 /* Posix.2 4.56.3 says that the first argument after sh -c command
946 becomes $0, and the rest of the arguments become $1...$n */
947 shell_name
= savestring (args
->word
->word
);
948 FREE (dollar_vars
[0]);
949 dollar_vars
[0] = savestring (args
->word
->word
);
950 remember_args (args
->next
, 1);
952 else /* bind to $1...$n for shell script */
953 remember_args (args
, 1);
955 dispose_words (args
);
964 remember_args ((WORD_LIST
*)NULL
, 1);
968 open_shell_script (script_name
)
972 char *filename
, *path_filename
;
973 unsigned char sample
[80];
977 free (dollar_vars
[0]);
978 dollar_vars
[0] = savestring (script_name
);
979 filename
= savestring (script_name
);
981 fd
= open (filename
, O_RDONLY
);
982 if ((fd
< 0) && (errno
== ENOENT
) && (absolute_program (filename
) == 0))
985 /* If it's not in the current directory, try looking through PATH
987 path_filename
= find_path_file (script_name
);
991 filename
= path_filename
;
992 fd
= open (filename
, O_RDONLY
);
1001 file_error (filename
);
1002 exit ((e
== ENOENT
) ? EX_NOTFOUND
: EX_NOINPUT
);
1005 /* Only do this with file descriptors we can seek on. */
1006 if (lseek (fd
, 0L, 1) != -1)
1008 /* Check to see if the `file' in `bash file' is a binary file
1009 according to the same tests done by execute_simple_command (),
1010 and report an error and exit if it is. */
1011 sample_len
= read (fd
, sample
, sizeof (sample
));
1015 if ((fstat (fd
, &sb
) == 0) && S_ISDIR (sb
.st_mode
))
1016 internal_error ("%s: is a directory", filename
);
1020 file_error (filename
);
1024 else if (sample_len
> 0 && (check_binary_file (sample
, sample_len
)))
1026 internal_error ("%s: cannot execute binary file", filename
);
1027 exit (EX_BINARY_FILE
);
1029 /* Now rewind the file back to the beginning. */
1033 #if defined (BUFFERED_INPUT)
1034 default_buffered_input
= fd
;
1036 /* This is never executed. */
1037 if (default_buffered_input
== -1)
1039 file_error (filename
);
1043 SET_CLOSE_ON_EXEC (default_buffered_input
);
1044 #else /* !BUFFERED_INPUT */
1045 /* Open the script. But try to move the file descriptor to a randomly
1046 large one, in the hopes that any descriptors used by the script will
1047 not match with ours. */
1048 fd
= move_to_high_fd (fd
, 0, -1);
1050 default_input
= fdopen (fd
, "r");
1052 if (default_input
== 0)
1054 file_error (filename
);
1058 SET_CLOSE_ON_EXEC (fd
);
1059 if (fileno (default_input
) != fd
)
1060 SET_CLOSE_ON_EXEC (fileno (default_input
));
1061 #endif /* !BUFFERED_INPUT */
1063 if (interactive_shell
== 0 || isatty (fd
) == 0)
1064 /* XXX - does this really need to be called again here? */
1065 init_noninteractive ();
1068 /* I don't believe that this code is ever executed, even in
1069 the presence of /dev/fd. */
1073 #if defined (BUFFERED_INPUT)
1074 default_buffered_input
= 0;
1076 fclose (default_input
);
1077 default_input
= stdin
;
1084 /* Initialize the input routines for the parser. */
1088 /* Make sure the fd from which we are reading input is not in
1090 #if defined (BUFFERED_INPUT)
1091 if (interactive
== 0)
1092 unset_nodelay_mode (default_buffered_input
);
1094 #endif /* !BUFFERED_INPUT */
1095 unset_nodelay_mode (fileno (stdin
));
1097 /* with_input_from_stdin really means `with_input_from_readline' */
1098 if (interactive
&& no_line_editing
== 0)
1099 with_input_from_stdin ();
1101 #if defined (BUFFERED_INPUT)
1103 if (interactive
== 0)
1104 with_input_from_buffered_stream (default_buffered_input
, dollar_vars
[0]);
1106 with_input_from_stream (default_input
, dollar_vars
[0]);
1108 #else /* !BUFFERED_INPUT */
1109 with_input_from_stream (default_input
, dollar_vars
[0]);
1110 #endif /* !BUFFERED_INPUT */
1113 #if !defined (PROGRAM)
1114 # define PROGRAM "bash"
1118 set_shell_name (argv0
)
1121 /* Here's a hack. If the name of this shell is "sh", then don't do
1122 any startup files; just try to be more like /bin/sh. */
1123 shell_name
= base_pathname (argv0
);
1124 if (*shell_name
== '-')
1126 if (shell_name
[0] == 's' && shell_name
[1] == 'h' && shell_name
[2] == '\0')
1128 if (shell_name
[0] == 's' && shell_name
[1] == 'u' && shell_name
[2] == '\0')
1132 FREE (dollar_vars
[0]);
1133 dollar_vars
[0] = savestring (shell_name
);
1135 if (*shell_name
== '-')
1141 /* A program may start an interactive shell with
1142 "execl ("/bin/bash", "-", NULL)".
1143 If so, default the name of this shell to our name. */
1144 if (!shell_name
|| !*shell_name
|| (shell_name
[0] == '-' && !shell_name
[1]))
1145 shell_name
= PROGRAM
;
1151 interactive_shell
= startup_state
= interactive
= 1;
1156 init_noninteractive ()
1158 #if defined (HISTORY)
1159 bash_history_reinit (0);
1160 #endif /* HISTORY */
1161 interactive_shell
= startup_state
= interactive
= 0;
1163 no_line_editing
= 1;
1164 #if defined (JOB_CONTROL)
1165 set_job_control (0);
1166 #endif /* JOB_CONTROL */
1170 get_current_user_info ()
1172 struct passwd
*entry
;
1174 /* Don't fetch this more than once. */
1175 if (current_user
.user_name
== 0)
1177 entry
= getpwuid (current_user
.uid
);
1180 current_user
.user_name
= savestring (entry
->pw_name
);
1181 current_user
.shell
= (entry
->pw_shell
&& entry
->pw_shell
[0])
1182 ? savestring (entry
->pw_shell
)
1183 : savestring ("/bin/sh");
1184 current_user
.home_dir
= savestring (entry
->pw_dir
);
1188 current_user
.user_name
= savestring ("I have no name!");
1189 current_user
.shell
= savestring ("/bin/sh");
1190 current_user
.home_dir
= savestring ("/");
1196 /* Do whatever is necessary to initialize the shell.
1197 Put new initializations in here. */
1203 /* Line buffer output for stderr and stdout. */
1204 setlinebuf (stderr
);
1205 setlinebuf (stdout
);
1207 /* Sort the array of shell builtins so that the binary search in
1208 find_shell_builtin () works correctly. */
1209 initialize_shell_builtins ();
1211 /* Initialize the trap signal handlers before installing our own
1212 signal handlers. traps.c:restore_original_signals () is responsible
1213 for restoring the original default signal handlers. That function
1214 is called when we make a new child. */
1215 initialize_traps ();
1216 initialize_signals ();
1218 /* It's highly unlikely that this will change. */
1219 if (current_host_name
== 0)
1221 /* Initialize current_host_name. */
1222 if (gethostname (hostname
, 255) < 0)
1223 current_host_name
= "??host??";
1225 current_host_name
= savestring (hostname
);
1228 /* Initialize the stuff in current_user that comes from the password
1229 file. We don't need to do this right away if the shell is not
1231 if (interactive_shell
)
1232 get_current_user_info ();
1234 /* Initialize our interface to the tilde expander. */
1235 tilde_initialize ();
1237 /* Initialize internal and environment variables. Don't import shell
1238 functions from the environment if we are running in privileged or
1239 restricted mode or if the shell is running setuid. */
1240 #if defined (RESTRICTED_SHELL)
1241 initialize_shell_variables (shell_environment
, privileged_mode
||restricted
||running_setuid
);
1243 initialize_shell_variables (shell_environment
, privileged_mode
||running_setuid
);
1247 /* Initialize filename hash tables. */
1248 initialize_filename_hashing ();
1251 /* Initialize the data structures for storing and running jobs. */
1252 initialize_job_control (0);
1254 /* Initialize input streams to null. */
1255 initialize_bash_input ();
1257 /* Initialize the shell options. */
1258 initialize_shell_options ();
1261 /* Function called by main () when it appears that the shell has already
1262 had some initialization performed. This is supposed to reset the world
1263 back to a pristine state, as if we had been exec'ed. */
1265 shell_reinitialize ()
1267 /* The default shell prompts. */
1268 primary_prompt
= PPROMPT
;
1269 secondary_prompt
= SPROMPT
;
1271 /* Things that get 1. */
1272 current_command_number
= 1;
1274 /* We have decided that the ~/.bashrc file should not be executed
1275 for the invocation of each shell script. If the variable $ENV
1276 (or $BASH_ENV) is set, its value is used as the name of a file
1278 no_rc
= no_profile
= 1;
1280 /* Things that get 0. */
1281 login_shell
= make_login_shell
= interactive
= executing
= 0;
1282 debugging
= do_version
= line_number
= last_command_exit_value
= 0;
1283 forced_interactive
= interactive_shell
= subshell_environment
= 0;
1286 #if defined (HISTORY)
1287 bash_history_reinit (0);
1288 #endif /* HISTORY */
1290 #if defined (RESTRICTED_SHELL)
1292 #endif /* RESTRICTED_SHELL */
1294 /* Ensure that the default startup file is used. (Except that we don't
1295 execute this file for reinitialized shells). */
1296 bashrc_file
= "~/.bashrc";
1298 /* Delete all variables and functions. They will be reinitialized when
1299 the environment is parsed. */
1300 delete_all_variables (shell_variables
);
1301 delete_all_variables (shell_functions
);
1304 /* Pretend the PATH variable has changed. */
1305 flush_hashed_filenames ();
1310 show_shell_usage (fp
, extra
)
1315 char *set_opts
, *s
, *t
;
1318 fprintf (fp
, "GNU bash, version %s-(%s)\n", shell_version_string (), MACHTYPE
);
1319 fprintf (fp
, "Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n",
1320 shell_name
, shell_name
);
1321 fputs ("GNU long options:\n", fp
);
1322 for (i
= 0; long_args
[i
].name
; i
++)
1323 fprintf (fp
, "\t--%s\n", long_args
[i
].name
);
1325 fputs ("Shell options:\n", fp
);
1326 fputs ("\t-irsD or -c command\t\t(invocation only)\n", fp
);
1328 for (i
= 0, set_opts
= 0; shell_builtins
[i
].name
; i
++)
1329 if (STREQ (shell_builtins
[i
].name
, "set"))
1330 set_opts
= savestring (shell_builtins
[i
].short_doc
);
1333 s
= strchr (set_opts
, '[');
1338 t
= strchr (s
, ']');
1341 fprintf (fp
, "\t-%s or -o option\n", s
);
1347 fprintf (fp
, "Type `%s -c \"help set\"' for more information about shell options.\n", shell_name
);
1348 fprintf (fp
, "Type `%s -c help' for more information about shell builtin commands.\n", shell_name
);
1349 fprintf (fp
, "Use the `bashbug' command to report bugs.\n");
1353 /* The second and subsequent conditions must match those used to decide
1354 whether or not to call getpeername() in isnetconn(). */
1355 #if defined (HAVE_SYS_SOCKET_H) && defined (HAVE_GETPEERNAME) && !defined (SVR4_2)
1356 # include <sys/socket.h>
1359 /* Is FD a socket or network connection? */
1364 #if defined (HAVE_GETPEERNAME) && !defined (SVR4_2)
1369 rv
= getpeername(0, &sa
, &l
);
1370 /* Solaris 2.5 getpeername() returns EINVAL if the fd is not a socket. */
1371 return ((rv
< 0 && (errno
== ENOTSOCK
|| errno
== EINVAL
)) ? 0 : 1);
1372 #else /* !HAVE_GETPEERNAME || SVR4_2 */
1373 # if defined (SVR4) || defined (SVR4_2)
1374 /* Sockets on SVR4 and SVR4.2 are character special (streams) devices. */
1379 if (fstat (fd
, &sb
) < 0)
1381 # if defined (S_ISFIFO)
1382 if (S_ISFIFO (sb
.st_mode
))
1384 # endif /* S_ISFIFO */
1385 return (S_ISCHR (sb
.st_mode
));
1386 # else /* !SVR4 && !SVR4_2 */
1387 # if defined (S_ISSOCK)
1390 if (fstat (fd
, &sb
) < 0)
1392 return (S_ISSOCK (sb
.st_mode
));
1393 # else /* !S_ISSOCK */
1395 # endif /* !S_ISSOCK */
1396 # endif /* !SVR4 && !SVR4_2 */
1397 #endif /* !HAVE_GETPEERNAME || SVR4_2 */