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);
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 we're in a strict Posix.2 mode, turn on interactive comments and
346 other Posix.2 things. */
348 posix_initialize (posixly_correct);
350 if (running_setuid && privileged_mode == 0)
351 disable_priv_mode ();
353 /* Need to get the argument to a -c option processed in the
354 above loop. The next arg is a command to execute, and the
355 following args are $0...$n respectively. */
356 if (want_pending_command)
358 local_pending_command = argv[arg_index];
359 if (local_pending_command == 0)
361 report_error ("option `-c' requires an argument");
366 this_command_name = (char *)NULL;
368 /* First, let the outside world know about our interactive status.
369 A shell is interactive if the `-i' flag was given, or if all of
370 the following conditions are met:
372 no arguments remaining or the -s flag given
373 standard input is a terminal
374 standard output is a terminal
375 Refer to Posix.2, the description of the `sh' utility. */
377 if (forced_interactive || /* -i flag */
378 (!local_pending_command && /* No -c command and ... */
379 ((arg_index == argc) || /* no remaining args or... */
380 read_from_stdin) && /* -s flag with args, and */
381 isatty (fileno (stdin)) && /* Input is a terminal and */
382 isatty (fileno (stdout)))) /* output is a terminal. */
385 init_noninteractive ();
387 #define CLOSE_FDS_AT_LOGIN
388 #if defined (CLOSE_FDS_AT_LOGIN)
390 * Some systems have the bad habit of starting login shells with lots of open
391 * file descriptors. For instance, most systems that have picked up the
392 * pre-4.0 Sun YP code leave a file descriptor open each time you call one
393 * of the getpw* functions, and it's set to be open across execs. That
394 * means one for login, one for xterm, one for shelltool, etc.
396 if (login_shell && interactive_shell)
398 for (i = 3; i < 20; i++)
401 #endif /* CLOSE_FDS_AT_LOGIN */
403 /* From here on in, the shell must be a normal functioning shell.
404 Variables from the environment are expected to be set, etc. */
407 set_default_locale_vars ();
409 if (interactive_shell)
413 term = getenv ("TERM");
414 no_line_editing |= term && (STREQ (term, "emacs"));
415 term = getenv ("EMACS");
416 running_under_emacs = term ? ((fnmatch ("*term*", term, 0) == 0) ? 2 : 1)
420 top_level_arg_index = arg_index;
422 /* Give this shell a place to longjmp to before executing the
423 startup files. This allows users to press C-c to abort the
425 code = setjmp (top_level);
428 if (code == EXITPROG)
429 exit_shell (last_command_exit_value);
431 locally_skip_execution++;
434 arg_index = top_level_arg_index;
436 /* Execute the start-up scripts. */
438 if (interactive_shell == 0)
440 makunbound ("PS1", shell_variables);
441 makunbound ("PS2", shell_variables);
442 interactive = expand_aliases = 0;
446 change_flag ('i', FLAG_ON);
450 if (locally_skip_execution == 0 && running_setuid == 0)
451 run_startup_files ();
453 /* If we are invoked as `sh', turn on Posix mode. */
455 posix_initialize (posixly_correct = 1);
457 #if defined (RESTRICTED_SHELL)
458 /* Turn on the restrictions after parsing the startup files. */
459 maybe_make_restricted (shell_name);
460 #endif /* RESTRICTED_SHELL */
462 if (local_pending_command)
464 arg_index = bind_args (argv, arg_index, argc, 0);
467 #if defined (ONESHOT)
468 run_one_command (local_pending_command);
469 exit_shell (last_command_exit_value);
471 with_input_from_string (local_pending_command, "-c");
472 goto read_and_execute;
473 #endif /* !ONESHOT */
476 /* Get possible input filename and set up default_buffered_input or
477 default_input as appropriate. */
478 if (arg_index != argc && read_from_stdin == 0)
480 open_shell_script (argv[arg_index]);
483 else if (interactive == 0)
484 /* In this mode, bash is reading a script from stdin, which is a
485 pipe or redirected file. */
486 #if defined (BUFFERED_INPUT)
487 default_buffered_input = fileno (stdin); /* == 0 */
489 setbuf (default_input, (char *)NULL);
490 #endif /* !BUFFERED_INPUT */
494 /* Bind remaining args to $1 ... $n */
495 arg_index = bind_args (argv, arg_index, argc, 1);
497 /* Do the things that should be done only for interactive shells. */
498 if (interactive_shell)
500 /* Set up for checking for presence of mail. */
501 remember_mail_dates ();
504 #if defined (HISTORY)
505 /* Initialize the interactive history stuff. */
506 bash_initialize_history ();
507 if (shell_initialized == 0)
511 /* Initialize terminal state for interactive shells after the
512 .bash_profile and .bashrc are interpreted. */
516 #if !defined (ONESHOT)
518 #endif /* !ONESHOT */
520 shell_initialized = 1;
522 /* Read commands until exit condition. */
524 exit_shell (last_command_exit_value);
528 parse_long_options (argv, arg_start, arg_end)
530 int arg_start, arg_end;
532 int arg_index, longarg, i;
535 arg_index = arg_start;
536 while ((arg_index != arg_end) && (arg_string = argv[arg_index]) &&
537 (*arg_string == '-'))
541 /* Make --login equivalent to -login. */
542 if (arg_string[1] == '-' && arg_string[2])
548 for (i = 0; long_args[i].name; i++)
550 if (STREQ (arg_string + 1, long_args[i].name))
552 if (long_args[i].type == Int)
553 *long_args[i].int_value = 1;
554 else if (argv[++arg_index] == 0)
556 report_error ("option `%s' requires an argument",
561 *long_args[i].char_value = argv[arg_index];
566 if (long_args[i].name == 0)
570 report_error ("%s: unrecognized option", argv[arg_index]);
573 break; /* No such argument. Maybe flag arg. */
583 parse_shell_options (argv, arg_start, arg_end)
585 int arg_start, arg_end;
588 int arg_character, on_or_off, next_arg, i;
589 char *o_option, *arg_string;
591 arg_index = arg_start;
592 while (arg_index != arg_end && (arg_string = argv[arg_index]) &&
593 (*arg_string == '-' || *arg_string == '+'))
595 /* There are flag arguments, so parse them. */
596 next_arg = arg_index + 1;
598 /* A single `-' signals the end of options. From the 4.3 BSD sh.
599 An option `--' means the same thing; this is the standard
600 getopt(3) meaning. */
601 if (arg_string[0] == '-' &&
602 (arg_string[1] == '\0' ||
603 (arg_string[1] == '-' && arg_string[2] == '\0')))
607 on_or_off = arg_string[0];
608 while (arg_character = arg_string[i++])
610 switch (arg_character)
613 want_pending_command = 1;
621 o_option = argv[next_arg];
624 list_minus_o_opts ();
627 if (set_minus_o_option (on_or_off, o_option) != EXECUTION_SUCCESS)
633 dump_translatable_strings = 1;
637 if (change_flag (arg_character, on_or_off) == FLAG_ERROR)
639 report_error ("%c%c: unrecognized option", on_or_off, arg_character);
644 /* Can't do just a simple increment anymore -- what about
645 "bash -abouo emacs ignoreeof -hP"? */
646 arg_index = next_arg;
652 /* Exit the shell with status S. */
657 /* Do trap[0] if defined. Allow it to override the exit status
659 if (signal_is_trapped (0))
660 s = run_exit_trap ();
662 #if defined (PROCESS_SUBSTITUTION)
664 #endif /* PROCESS_SUBSTITUTION */
666 #if defined (HISTORY)
667 if (interactive_shell)
668 maybe_save_shell_history ();
671 #if defined (JOB_CONTROL)
672 /* If this shell is interactive, terminate all stopped jobs and
673 restore the original terminal process group. */
675 #endif /* JOB_CONTROL */
677 /* Always return the exit status of the last command to our parent. */
681 /* Source the bash startup files. If POSIXLY_CORRECT is non-zero, we obey
682 the Posix.2 startup file rules: $ENV is expanded, and if the file it
683 names exists, that file is sourced. The Posix.2 rules are in effect
684 for interactive shells only. (section 4.56.5.3) */
686 /* Execute ~/.bashrc for most shells. Never execute it if
687 ACT_LIKE_SH is set, or if NO_RC is set.
689 If the executable file "/usr/gnu/src/bash/foo" contains:
696 COMMAND EXECUTE BASHRC
697 --------------------------------
701 rsh machine ls YES (for rsh, which calls `bash -c')
702 rsh machine foo YES (for shell started by rsh) NO (for foo!)
709 execute_env_file (env_file)
715 if (env_file && *env_file)
717 list = expand_string_unsplit (env_file, Q_DOUBLE_QUOTES);
720 fn = string_list (list);
721 dispose_words (list);
724 maybe_execute_file (fn, 1);
733 /* get the rshd case out of the way first. */
734 if (interactive_shell == 0 && no_rc == 0 && login_shell == 0 &&
735 act_like_sh == 0 && local_pending_command && isnetconn (fileno (stdin)))
738 maybe_execute_file (SYS_BASHRC, 1);
740 maybe_execute_file (bashrc_file, 1);
744 /* A non-interactive shell not named `sh' and not in posix mode reads and
745 executes commands from $BASH_ENV. If `su' starts a shell with `-c cmd'
746 and `-su' as the name of the shell, we want to read the startup files.
747 No other non-interactive shells read any startup files. */
748 if (interactive_shell == 0 && !(su_shell && login_shell))
750 if (posixly_correct == 0 && act_like_sh == 0 && privileged_mode == 0 &&
752 execute_env_file (get_string_value ("BASH_ENV"));
756 /* Interactive shell or `-su' shell. */
757 if (posixly_correct == 0) /* bash, sh */
759 /* We don't execute .bashrc for login shells. */
763 /* Execute /etc/profile and one of the personal login shell
764 initialization files. */
765 if (login_shell && no_profile == 0)
767 maybe_execute_file (SYS_PROFILE, 1);
769 if (act_like_sh) /* sh */
770 maybe_execute_file ("~/.profile", 1);
771 else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
772 (maybe_execute_file ("~/.bash_login", 1) == 0)) /* bash */
773 maybe_execute_file ("~/.profile", 1);
777 if (act_like_sh == 0 && no_rc == 0)
780 maybe_execute_file (SYS_BASHRC, 1);
782 maybe_execute_file (bashrc_file, 1);
785 else if (act_like_sh && privileged_mode == 0 && sourced_env++ == 0)
786 execute_env_file (get_string_value ("ENV"));
788 else /* bash --posix, sh --posix */
791 if (interactive_shell && privileged_mode == 0 && sourced_env++ == 0)
792 execute_env_file (get_string_value ("ENV"));
796 #if defined (RESTRICTED_SHELL)
797 /* Perhaps make this shell a `restricted' one, based on NAME. If the
798 basename of NAME is "rbash", then this shell is restricted. The
799 name of the restricted shell is a configurable option, see config.h.
800 In a restricted shell, PATH and SHELL are read-only and non-unsettable.
801 Do this also if `restricted' is already set to 1; maybe the shell was
804 maybe_make_restricted (name)
809 temp = base_pathname (shell_name);
810 if (restricted || (STREQ (temp, RESTRICTED_SHELL_NAME)))
812 set_var_read_only ("PATH");
813 set_var_read_only ("SHELL");
818 #endif /* RESTRICTED_SHELL */
820 /* Fetch the current set of uids and gids and return 1 if we're running
828 if (current_user.uid != u)
830 FREE (current_user.user_name);
831 FREE (current_user.shell);
832 FREE (current_user.home_dir);
833 current_user.user_name = current_user.shell = current_user.home_dir = (char *)NULL;
835 current_user.uid = u;
836 current_user.gid = getgid ();
837 current_user.euid = geteuid ();
838 current_user.egid = getegid ();
840 /* See whether or not we are running setuid or setgid. */
841 return (current_user.uid != current_user.euid) ||
842 (current_user.gid != current_user.egid);
848 setuid (current_user.uid);
849 setgid (current_user.gid);
850 current_user.euid = current_user.uid;
851 current_user.egid = current_user.gid;
854 #if defined (ONESHOT)
855 /* Run one command, given as the argument to the -c option. Tell
856 parse_and_execute not to fork for a simple command. */
858 run_one_command (command)
863 code = setjmp (top_level);
865 if (code != NOT_JUMPED)
867 #if defined (PROCESS_SUBSTITUTION)
869 #endif /* PROCESS_SUBSTITUTION */
872 /* Some kind of throw to top_level has occured. */
874 return last_command_exit_value = 127;
876 return last_command_exit_value;
878 return last_command_exit_value = 1;
880 programming_error ("run_one_command: bad jump: code %d", code);
883 return (parse_and_execute (savestring (command), "-c", -1));
888 bind_args (argv, arg_start, arg_end, start_index)
890 int arg_start, arg_end, start_index;
895 for (i = arg_start, args = (WORD_LIST *)NULL; i != arg_end; i++)
896 args = make_word_list (make_word (argv[i]), args);
899 args = REVERSE_LIST (args, WORD_LIST *);
900 if (start_index == 0) /* bind to $0...$n for sh -c command */
902 /* Posix.2 4.56.3 says that the first argument after sh -c command
903 becomes $0, and the rest of the arguments become $1...$n */
904 shell_name = savestring (args->word->word);
905 dollar_vars[0] = savestring (args->word->word);
906 remember_args (args->next, 1);
908 else /* bind to $1...$n for shell script */
909 remember_args (args, 1);
911 dispose_words (args);
920 remember_args ((WORD_LIST *)NULL, 1);
924 open_shell_script (script_name)
928 char *filename, *path_filename;
929 unsigned char sample[80];
932 free (dollar_vars[0]);
933 dollar_vars[0] = savestring (script_name);
934 filename = savestring (script_name);
936 fd = open (filename, O_RDONLY);
937 if ((fd < 0) && (errno == ENOENT) && (absolute_program (filename) == 0))
939 /* If it's not in the current directory, try looking through PATH
941 path_filename = find_path_file (script_name);
945 filename = path_filename;
946 fd = open (filename, O_RDONLY);
953 file_error (filename);
954 exit ((e == ENOENT) ? EX_NOTFOUND : EX_NOINPUT);
957 /* Only do this with file descriptors we can seek on. */
958 if (lseek (fd, 0L, 1) != -1)
960 /* Check to see if the `file' in `bash file' is a binary file
961 according to the same tests done by execute_simple_command (),
962 and report an error and exit if it is. */
963 sample_len = read (fd, sample, sizeof (sample));
964 if (sample_len > 0 && (check_binary_file (sample, sample_len)))
966 internal_error ("%s: cannot execute binary file", filename);
967 exit (EX_BINARY_FILE);
969 /* Now rewind the file back to the beginning. */
973 #if defined (BUFFERED_INPUT)
974 default_buffered_input = fd;
975 if (default_buffered_input == -1)
977 file_error (filename);
980 SET_CLOSE_ON_EXEC (default_buffered_input);
981 #else /* !BUFFERED_INPUT */
982 /* Open the script. But try to move the file descriptor to a randomly
983 large one, in the hopes that any descriptors used by the script will
984 not match with ours. */
985 fd = move_to_high_fd (fd, 0);
987 default_input = fdopen (fd, "r");
989 if (default_input == 0)
991 file_error (filename);
995 SET_CLOSE_ON_EXEC (fd);
996 if (fileno (default_input) != fd)
997 SET_CLOSE_ON_EXEC (fileno (default_input));
998 #endif /* !BUFFERED_INPUT */
1000 if (interactive_shell == 0 || isatty (fd) == 0)
1001 init_noninteractive ();
1004 /* I don't believe that this code is ever executed, even in
1005 the presence of /dev/fd. */
1009 #if defined (BUFFERED_INPUT)
1010 default_buffered_input = 0;
1012 fclose (default_input);
1013 default_input = stdin;
1020 /* Initialize the input routines for the parser. */
1024 /* Make sure the fd from which we are reading input is not in
1026 #if defined (BUFFERED_INPUT)
1027 if (interactive == 0)
1028 unset_nodelay_mode (default_buffered_input);
1030 #endif /* !BUFFERED_INPUT */
1031 unset_nodelay_mode (fileno (stdin));
1033 /* with_input_from_stdin really means `with_input_from_readline' */
1034 if (interactive && no_line_editing == 0)
1035 with_input_from_stdin ();
1037 #if defined (BUFFERED_INPUT)
1039 if (interactive == 0)
1040 with_input_from_buffered_stream (default_buffered_input, dollar_vars[0]);
1042 with_input_from_stream (default_input, dollar_vars[0]);
1044 #else /* !BUFFERED_INPUT */
1045 with_input_from_stream (default_input, dollar_vars[0]);
1046 #endif /* !BUFFERED_INPUT */
1049 #if !defined (PROGRAM)
1050 # define PROGRAM "bash"
1054 set_shell_name (argv0)
1057 /* Here's a hack. If the name of this shell is "sh", then don't do
1058 any startup files; just try to be more like /bin/sh. */
1059 shell_name = base_pathname (argv0);
1060 if (*shell_name == '-')
1062 if (shell_name[0] == 's' && shell_name[1] == 'h' && shell_name[2] == '\0')
1064 if (shell_name[0] == 's' && shell_name[1] == 'u' && shell_name[2] == '\0')
1068 FREE (dollar_vars[0]);
1069 dollar_vars[0] = savestring (shell_name);
1071 if (*shell_name == '-')
1077 /* A program may start an interactive shell with
1078 "execl ("/bin/bash", "-", NULL)".
1079 If so, default the name of this shell to our name. */
1080 if (!shell_name || !*shell_name || (shell_name[0] == '-' && !shell_name[1]))
1081 shell_name = PROGRAM;
1087 interactive_shell = startup_state = interactive = 1;
1092 init_noninteractive ()
1094 #if defined (HISTORY)
1095 bash_history_reinit (0);
1096 #endif /* HISTORY */
1097 interactive_shell = startup_state = interactive = 0;
1099 no_line_editing = 1;
1100 #if defined (JOB_CONTROL)
1101 set_job_control (0);
1102 #endif /* JOB_CONTROL */
1105 /* Do whatever is necessary to initialize the shell.
1106 Put new initializations in here. */
1110 struct passwd *entry;
1113 /* Line buffer output for stderr and stdout. */
1114 setlinebuf (stderr);
1115 setlinebuf (stdout);
1117 /* Sort the array of shell builtins so that the binary search in
1118 find_shell_builtin () works correctly. */
1119 initialize_shell_builtins ();
1121 /* Initialize the trap signal handlers before installing our own
1122 signal handlers. traps.c:restore_original_signals () is responsible
1123 for restoring the original default signal handlers. That function
1124 is called when we make a new child. */
1125 initialize_traps ();
1126 initialize_signals ();
1128 /* It's highly unlikely that this will change. */
1129 if (current_host_name == 0)
1131 /* Initialize current_user.name and current_host_name. */
1132 if (gethostname (hostname, 255) < 0)
1133 current_host_name = "??host??";
1135 current_host_name = savestring (hostname);
1138 /* Don't fetch this more than once. */
1139 if (current_user.user_name == 0)
1141 entry = getpwuid (current_user.uid);
1144 current_user.user_name = savestring (entry->pw_name);
1145 current_user.shell = (entry->pw_shell && entry->pw_shell[0])
1146 ? savestring (entry->pw_shell)
1147 : savestring ("/bin/sh");
1148 current_user.home_dir = savestring (entry->pw_dir);
1152 current_user.user_name = savestring ("I have no name!");
1153 current_user.shell = savestring ("/bin/sh");
1154 current_user.home_dir = savestring ("/");
1159 /* Initialize our interface to the tilde expander. */
1160 tilde_initialize ();
1162 /* Initialize internal and environment variables. Don't import shell
1163 functions from the environment if we are running in privileged or
1164 restricted mode or if the shell is running setuid. */
1165 #if defined (RESTRICTED_SHELL)
1166 initialize_shell_variables (shell_environment, privileged_mode||restricted||running_setuid);
1168 initialize_shell_variables (shell_environment, privileged_mode||running_setuid);
1171 /* Initialize filename hash tables. */
1172 initialize_filename_hashing ();
1174 /* Initialize the data structures for storing and running jobs. */
1177 /* Initialize input streams to null. */
1178 initialize_bash_input ();
1180 /* Initialize the shell options. */
1181 initialize_shell_options ();
1184 /* Function called by main () when it appears that the shell has already
1185 had some initialization performed. This is supposed to reset the world
1186 back to a pristine state, as if we had been exec'ed. */
1188 shell_reinitialize ()
1190 /* The default shell prompts. */
1191 primary_prompt = PPROMPT;
1192 secondary_prompt = SPROMPT;
1194 /* Things that get 1. */
1195 current_command_number = 1;
1197 /* We have decided that the ~/.bashrc file should not be executed
1198 for the invocation of each shell script. If the variable $ENV
1199 (or $BASH_ENV) is set, its value is used as the name of a file
1201 no_rc = no_profile = 1;
1203 /* Things that get 0. */
1204 login_shell = make_login_shell = interactive = executing = 0;
1205 debugging = do_version = line_number = last_command_exit_value = 0;
1206 forced_interactive = interactive_shell = subshell_environment = 0;
1209 #if defined (HISTORY)
1210 bash_history_reinit (0);
1211 #endif /* HISTORY */
1213 #if defined (RESTRICTED_SHELL)
1215 #endif /* RESTRICTED_SHELL */
1217 /* Ensure that the default startup file is used. (Except that we don't
1218 execute this file for reinitialized shells). */
1219 bashrc_file = "~/.bashrc";
1221 /* Delete all variables and functions. They will be reinitialized when
1222 the environment is parsed. */
1223 delete_all_variables (shell_variables);
1224 delete_all_variables (shell_functions);
1227 /* Pretend the PATH variable has changed. */
1228 flush_hashed_filenames ();
1233 show_shell_usage (fp)
1237 char *set_opts, *s, *t;
1239 fprintf (fp, "GNU bash, version %s-(%s)\n", shell_version_string (), MACHTYPE);
1240 fprintf (fp, "Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n",
1241 shell_name, shell_name);
1242 fputs ("GNU long options:\n", fp);
1243 for (i = 0; long_args[i].name; i++)
1244 fprintf (fp, "\t--%s\n", long_args[i].name);
1246 fputs ("Shell options:\n", fp);
1247 fputs ("\t-irsD or -c command\t\t(invocation only)\n", fp);
1249 for (i = 0, set_opts = 0; shell_builtins[i].name; i++)
1250 if (STREQ (shell_builtins[i].name, "set"))
1251 set_opts = savestring (shell_builtins[i].short_doc);
1254 s = strchr (set_opts, '[');
1259 t = strchr (s, ']');
1262 fprintf (fp, "\t-%s or -o option\n", s);
1266 fprintf (fp, "Type `%s -c \"help set\"' for more information about shell options.\n", shell_name);
1267 fprintf (fp, "Type `%s -c help' for more information about shell builtin commands.\n", shell_name);
1268 fprintf (fp, "Use the `bashbug' command to report bugs.\n");
1271 /* The second and subsequent conditions must match those used to decide
1272 whether or not to call getpeername() in isnetconn(). */
1273 #if defined (HAVE_SYS_SOCKET_H) && defined (HAVE_GETPEERNAME) && !defined (SVR4_2)
1274 # include <sys/socket.h>
1277 /* Is FD a socket or network connection? */
1282 #if defined (HAVE_GETPEERNAME) && !defined (SVR4_2)
1287 rv = getpeername(0, &sa, &l);
1288 /* Solaris 2.5 getpeername() returns EINVAL if the fd is not a socket. */
1289 return ((rv < 0 && (errno == ENOTSOCK || errno == EINVAL)) ? 0 : 1);
1290 #else /* !HAVE_GETPEERNAME || SVR4_2 */
1291 # if defined (SVR4) || defined (SVR4_2)
1292 /* Sockets on SVR4 and SVR4.2 are character special (streams) devices. */
1297 if (fstat (fd, &sb) < 0)
1299 # if defined (S_ISFIFO)
1300 if (S_ISFIFO (sb.st_mode))
1302 # endif /* S_ISFIFO */
1303 return (S_ISCHR (sb.st_mode));
1304 # else /* !SVR4 && !SVR4_2 */
1305 # if defined (S_ISSOCK)
1308 if (fstat (fd, &sb) < 0)
1310 return (S_ISSOCK (sb.st_mode));
1311 # else /* !S_ISSOCK */
1313 # endif /* !S_ISSOCK */
1314 # endif /* !SVR4 && !SVR4_2 */
1315 #endif /* !HAVE_GETPEERNAME || SVR4_2 */