98d8303c0685c24e6c3387121432cf9ab61d564c
[platform/upstream/bash.git] / shell.c
1 /* shell.c -- GNU's idea of the POSIX shell specification. */
2
3 /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
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)
10    any later version.
11
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.
16
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.
20
21   Birthdate:
22   Sunday, January 10th, 1988.
23   Initial author: Brian Fox
24 */
25 #define INSTALL_DEBUG_MODE
26
27 #include "config.h"
28
29 #include "bashtypes.h"
30 #ifndef _MINIX
31 #  include <sys/file.h>
32 #endif
33 #include "posixstat.h"
34 #include "posixtime.h"
35 #include "bashansi.h"
36 #include <stdio.h>
37 #include <signal.h>
38 #include <errno.h>
39 #include "filecntl.h"
40 #include <pwd.h>
41
42 #if defined (HAVE_UNISTD_H)
43 #  include <unistd.h>
44 #endif
45
46 #define NEED_SH_SETLINEBUF_DECL         /* used in externs.h */
47
48 #include "shell.h"
49 #include "flags.h"
50 #include "trap.h"
51 #include "mailcheck.h"
52 #include "builtins.h"
53 #include "builtins/common.h"
54
55 #if defined (JOB_CONTROL)
56 #include "jobs.h"
57 #endif /* JOB_CONTROL */
58
59 #include "input.h"
60 #include "execute_cmd.h"
61 #include "findcmd.h"
62
63 #if defined (HISTORY)
64 #  include "bashhist.h"
65 #  include <readline/history.h>
66 #endif
67
68 #include <tilde/tilde.h>
69 #include <glob/strmatch.h>
70
71 #if defined (__OPENNT)
72 #  include <opennt/opennt.h>
73 #endif
74
75 #if !defined (HAVE_GETPW_DECLS)
76 extern struct passwd *getpwuid ();
77 #endif /* !HAVE_GETPW_DECLS */
78
79 #if !defined (errno)
80 extern int errno;
81 #endif
82
83 #if defined (NO_MAIN_ENV_ARG)
84 extern char **environ;  /* used if no third argument to main() */
85 #endif
86
87 extern char *dist_version, *release_status;
88 extern int patch_level, build_version;
89 extern int shell_level;
90 extern int subshell_environment;
91 extern int last_command_exit_value;
92 extern int line_number;
93 extern char *primary_prompt, *secondary_prompt;
94 extern int expand_aliases;
95 extern char *this_command_name;
96 extern int array_needs_making;
97
98 /* Non-zero means that this shell has already been run; i.e. you should
99    call shell_reinitialize () if you need to start afresh. */
100 int shell_initialized = 0;
101
102 COMMAND *global_command = (COMMAND *)NULL;
103
104 /* Information about the current user. */
105 struct user_info current_user =
106 {
107   (uid_t)-1, (uid_t)-1, (gid_t)-1, (gid_t)-1,
108   (char *)NULL, (char *)NULL, (char *)NULL
109 };
110
111 /* The current host's name. */
112 char *current_host_name = (char *)NULL;
113
114 /* Non-zero means that this shell is a login shell.
115    Specifically:
116    0 = not login shell.
117    1 = login shell from getty (or equivalent fake out)
118   -1 = login shell from "--login" flag.
119   -2 = both from getty, and from flag.
120  */
121 int login_shell = 0;
122
123 /* Non-zero means that at this moment, the shell is interactive.  In
124    general, this means that the shell is at this moment reading input
125    from the keyboard. */
126 int interactive = 0;
127
128 /* Non-zero means that the shell was started as an interactive shell. */
129 int interactive_shell = 0;
130
131 /* Non-zero means to send a SIGHUP to all jobs when an interactive login
132    shell exits. */
133 int hup_on_exit = 0;
134
135 /* Tells what state the shell was in when it started:
136         0 = non-interactive shell script
137         1 = interactive
138         2 = -c command
139    This is a superset of the information provided by interactive_shell.
140 */
141 int startup_state = 0;
142
143 /* Special debugging helper. */
144 int debugging_login_shell = 0;
145
146 /* The environment that the shell passes to other commands. */
147 char **shell_environment;
148
149 /* Non-zero when we are executing a top-level command. */
150 int executing = 0;
151
152 /* The number of commands executed so far. */
153 int current_command_number = 1;
154
155 /* Non-zero is the recursion depth for commands. */
156 int indirection_level = 0;
157
158 /* The name of this shell, as taken from argv[0]. */
159 char *shell_name = (char *)NULL;
160
161 /* time in seconds when the shell was started */
162 time_t shell_start_time;
163
164 /* Are we running in an emacs shell window? */
165 int running_under_emacs;
166
167 /* The name of the .(shell)rc file. */
168 static char *bashrc_file = "~/.bashrc";
169
170 /* Non-zero means to act more like the Bourne shell on startup. */
171 static int act_like_sh;
172
173 /* Non-zero if this shell is being run by `su'. */
174 static int su_shell;
175
176 /* Non-zero if we have already expanded and sourced $ENV. */
177 static int sourced_env;
178
179 /* Is this shell running setuid? */
180 static int running_setuid;
181
182 /* Values for the long-winded argument names. */
183 static int debugging;                   /* Do debugging things. */
184 static int no_rc;                       /* Don't execute ~/.bashrc */
185 static int no_profile;                  /* Don't execute .profile */
186 static int do_version;                  /* Display interesting version info. */
187 static int make_login_shell;            /* Make this shell be a `-bash' shell. */
188 static int want_initial_help;           /* --help option */
189
190 int no_line_editing = 0;        /* Don't do fancy line editing. */
191 int posixly_correct = 0;        /* Non-zero means posix.2 superset. */
192 int dump_translatable_strings;  /* Dump strings in $"...", don't execute. */
193 int dump_po_strings;            /* Dump strings in $"..." in po format */
194 int wordexp_only = 0;           /* Do word expansion only */
195
196 /* Some long-winded argument names.  These are obviously new. */
197 #define Int 1
198 #define Charp 2
199 struct {
200   char *name;
201   int type;
202   int *int_value;
203   char **char_value;
204 } long_args[] = {
205   { "debug", Int, &debugging, (char **)0x0 },
206   { "dump-po-strings", Int, &dump_po_strings, (char **)0x0 },
207   { "dump-strings", Int, &dump_translatable_strings, (char **)0x0 },
208   { "help", Int, &want_initial_help, (char **)0x0 },
209   { "init-file", Charp, (int *)0x0, &bashrc_file },
210   { "login", Int, &make_login_shell, (char **)0x0 },
211   { "noediting", Int, &no_line_editing, (char **)0x0 },
212   { "noprofile", Int, &no_profile, (char **)0x0 },
213   { "norc", Int, &no_rc, (char **)0x0 },
214   { "posix", Int, &posixly_correct, (char **)0x0 },
215   { "rcfile", Charp, (int *)0x0, &bashrc_file },
216 #if defined (RESTRICTED_SHELL)
217   { "restricted", Int, &restricted, (char **)0x0 },
218 #endif
219   { "verbose", Int, &echo_input_at_read, (char **)0x0 },
220   { "version", Int, &do_version, (char **)0x0 },
221   { "wordexp", Int, &wordexp_only, (char **)0x0 },
222   { (char *)0x0, Int, (int *)0x0, (char **)0x0 }
223 };
224
225 /* These are extern so execute_simple_command can set them, and then
226    longjmp back to main to execute a shell script, instead of calling
227    main () again and resulting in indefinite, possibly fatal, stack
228    growth. */
229 procenv_t subshell_top_level;
230 int subshell_argc;
231 char **subshell_argv;
232 char **subshell_envp;
233
234 #if defined (BUFFERED_INPUT)
235 /* The file descriptor from which the shell is reading input. */
236 int default_buffered_input = -1;
237 #endif
238
239 /* The following two variables are not static so they can show up in $-. */
240 int read_from_stdin;            /* -s flag supplied */
241 int want_pending_command;       /* -c flag supplied */
242
243 static int shell_reinitialized = 0;
244 static char *local_pending_command;
245
246 static FILE *default_input;
247
248 static STRING_INT_ALIST *shopt_alist;
249 static int shopt_ind = 0, shopt_len = 0;
250
251 static int parse_long_options __P((char **, int, int));
252 static int parse_shell_options __P((char **, int, int));
253 static int bind_args __P((char **, int, int, int));
254
255 static void add_shopt_to_alist __P((char *, int));
256 static void run_shopt_alist __P((void));
257
258 static void execute_env_file __P((char *));
259 static void run_startup_files __P((void));
260 static int open_shell_script __P((char *));
261 static void set_bash_input __P((void));
262 static int run_one_command __P((char *));
263 static int run_wordexp __P((char *));
264
265 static int uidget __P((void));
266 static int isnetconn __P((int));
267
268 static void init_interactive __P((void));
269 static void init_noninteractive __P((void));
270
271 static void set_shell_name __P((char *));
272 static void shell_initialize __P((void));
273 static void shell_reinitialize __P((void));
274
275 static void show_shell_usage __P((FILE *, int));
276
277 #ifdef __CYGWIN__
278 static void
279 _cygwin32_check_tmp ()
280 {
281   struct stat sb;
282
283   if (stat ("/tmp", &sb) < 0)
284     internal_warning ("could not find /tmp, please create!");
285   else
286     {
287       if (S_ISDIR (sb.st_mode) == 0)
288         internal_warning ("/tmp must be a valid directory name");
289     }
290 }
291 #endif /* __CYGWIN__ */
292
293 #if defined (NO_MAIN_ENV_ARG)
294 /* systems without third argument to main() */
295 int
296 main (argc, argv)
297      int argc;
298      char **argv;
299 #else /* !NO_MAIN_ENV_ARG */
300 int
301 main (argc, argv, env)
302      int argc;
303      char **argv, **env;
304 #endif /* !NO_MAIN_ENV_ARG */
305 {
306   register int i;
307   int code, old_errexit_flag;
308 #if defined (RESTRICTED_SHELL)
309   int saverst;
310 #endif
311   volatile int locally_skip_execution;
312   volatile int arg_index, top_level_arg_index;
313 #ifdef __OPENNT
314   char **env;
315
316   env = environ;
317 #endif /* __OPENNT */
318
319   USE_VAR(argc);
320   USE_VAR(argv);
321   USE_VAR(env);
322   USE_VAR(code);
323   USE_VAR(old_errexit_flag);
324 #if defined (RESTRICTED_SHELL)
325   USE_VAR(saverst);
326 #endif
327
328   /* Catch early SIGINTs. */
329   code = setjmp (top_level);
330   if (code)
331     exit (2);
332
333 #if defined (USING_BASH_MALLOC) && defined (DEBUG)
334 #  if 0         /* memory tracing */
335   malloc_set_trace(1);
336 #  endif
337
338 #  if 0
339   malloc_set_register (1);
340 #  endif
341 #endif
342
343   check_dev_tty ();
344
345 #ifdef __CYGWIN__
346   _cygwin32_check_tmp ();
347 #endif /* __CYGWIN__ */
348
349   /* Wait forever if we are debugging a login shell. */
350   while (debugging_login_shell);
351
352   set_default_locale ();
353
354   running_setuid = uidget ();
355
356   if (getenv ("POSIXLY_CORRECT") || getenv ("POSIX_PEDANTIC"))
357     posixly_correct = 1;
358
359 #if defined (USE_GNU_MALLOC_LIBRARY)
360   mcheck (programming_error, (void (*) ())0);
361 #endif /* USE_GNU_MALLOC_LIBRARY */
362
363   if (setjmp (subshell_top_level))
364     {
365       argc = subshell_argc;
366       argv = subshell_argv;
367       env = subshell_envp;
368       sourced_env = 0;
369     }
370
371   shell_reinitialized = 0;
372
373   /* Initialize `local' variables for all `invocations' of main (). */
374   arg_index = 1;
375   local_pending_command = (char *)NULL;
376   want_pending_command = locally_skip_execution = read_from_stdin = 0;
377   default_input = stdin;
378 #if defined (BUFFERED_INPUT)
379   default_buffered_input = -1;
380 #endif
381
382   /* Fix for the `infinite process creation' bug when running shell scripts
383      from startup files on System V. */
384   login_shell = make_login_shell = 0;
385
386   /* If this shell has already been run, then reinitialize it to a
387      vanilla state. */
388   if (shell_initialized || shell_name)
389     {
390       /* Make sure that we do not infinitely recurse as a login shell. */
391       if (*shell_name == '-')
392         shell_name++;
393
394       shell_reinitialize ();
395       if (setjmp (top_level))
396         exit (2);
397     }
398
399   shell_environment = env;
400   set_shell_name (argv[0]);
401   shell_start_time = NOW;       /* NOW now defined in general.h */
402
403   /* Parse argument flags from the input line. */
404
405   /* Find full word arguments first. */
406   arg_index = parse_long_options (argv, arg_index, argc);
407
408   if (want_initial_help)
409     {
410       show_shell_usage (stdout, 1);
411       exit (EXECUTION_SUCCESS);
412     }
413
414   if (do_version)
415     {
416       show_shell_version (1);
417       exit (EXECUTION_SUCCESS);
418     }
419
420   /* If user supplied the "--login" flag, then set and invert LOGIN_SHELL. */
421   if (make_login_shell)
422     {
423       login_shell++;
424       login_shell = -login_shell;
425     }
426
427   set_login_shell (login_shell != 0);
428
429   /* All done with full word options; do standard shell option parsing.*/
430   this_command_name = shell_name;       /* for error reporting */
431   arg_index = parse_shell_options (argv, arg_index, argc);
432
433   if (dump_po_strings)
434     dump_translatable_strings = 1;
435
436   if (dump_translatable_strings)
437     read_but_dont_execute = 1;
438
439   if (running_setuid && privileged_mode == 0)
440     disable_priv_mode ();
441
442   /* Need to get the argument to a -c option processed in the
443      above loop.  The next arg is a command to execute, and the
444      following args are $0...$n respectively. */
445   if (want_pending_command)
446     {
447       local_pending_command = argv[arg_index];
448       if (local_pending_command == 0)
449         {
450           report_error ("option `-c' requires an argument");
451           exit (EX_USAGE);
452         }
453       arg_index++;
454     }
455   this_command_name = (char *)NULL;
456
457   /* First, let the outside world know about our interactive status.
458      A shell is interactive if the `-i' flag was given, or if all of
459      the following conditions are met:
460         no -c command
461         no arguments remaining or the -s flag given
462         standard input is a terminal
463         standard output is a terminal
464      Refer to Posix.2, the description of the `sh' utility. */
465
466   if (forced_interactive ||             /* -i flag */
467       (!local_pending_command &&        /* No -c command and ... */
468        wordexp_only == 0 &&             /* No --wordexp and ... */
469        ((arg_index == argc) ||          /*   no remaining args or... */
470         read_from_stdin) &&             /*   -s flag with args, and */
471        isatty (fileno (stdin)) &&       /* Input is a terminal and */
472        isatty (fileno (stdout))))       /* output is a terminal. */
473     init_interactive ();
474   else
475     init_noninteractive ();
476
477 #define CLOSE_FDS_AT_LOGIN
478 #if defined (CLOSE_FDS_AT_LOGIN)
479   /*
480    * Some systems have the bad habit of starting login shells with lots of open
481    * file descriptors.  For instance, most systems that have picked up the
482    * pre-4.0 Sun YP code leave a file descriptor open each time you call one
483    * of the getpw* functions, and it's set to be open across execs.  That
484    * means one for login, one for xterm, one for shelltool, etc.
485    */
486   if (login_shell && interactive_shell)
487     {
488       for (i = 3; i < 20; i++)
489         close (i);
490     }
491 #endif /* CLOSE_FDS_AT_LOGIN */
492
493   /* If we're in a strict Posix.2 mode, turn on interactive comments and
494      other Posix.2 things. */
495   if (posixly_correct)
496     {
497       bind_variable ("POSIXLY_CORRECT", "y");
498       sv_strict_posix ("POSIXLY_CORRECT");
499     }
500
501   /* Now we run the shopt_alist and process the options. */
502   if (shopt_alist)
503     run_shopt_alist ();
504
505   /* From here on in, the shell must be a normal functioning shell.
506      Variables from the environment are expected to be set, etc. */
507   shell_initialize ();
508
509   set_default_locale_vars ();
510
511   if (interactive_shell)
512     {
513       char *term;
514
515       term = getenv ("TERM");
516       no_line_editing |= term && (STREQ (term, "emacs"));
517       term = getenv ("EMACS");
518       running_under_emacs = term ? ((strmatch ("*term*", term, 0) == 0) ? 2 : 1)
519                                  : 0;
520     }
521
522   top_level_arg_index = arg_index;
523   old_errexit_flag = exit_immediately_on_error;
524
525   /* Give this shell a place to longjmp to before executing the
526      startup files.  This allows users to press C-c to abort the
527      lengthy startup. */
528   code = setjmp (top_level);
529   if (code)
530     {
531       if (code == EXITPROG)
532         exit_shell (last_command_exit_value);
533       else
534         {
535 #if defined (JOB_CONTROL)
536           /* Reset job control, since run_startup_files turned it off. */
537           set_job_control (interactive_shell);
538 #endif
539           /* Reset value of `set -e', since it's turned off before running
540              the startup files. */
541           exit_immediately_on_error += old_errexit_flag;
542           locally_skip_execution++;
543         }
544     }
545
546   arg_index = top_level_arg_index;
547
548   /* Execute the start-up scripts. */
549
550   if (interactive_shell == 0)
551     {
552       makunbound ("PS1", shell_variables);
553       makunbound ("PS2", shell_variables);
554       interactive = 0;
555       expand_aliases = posixly_correct;
556     }
557   else
558     {
559       change_flag ('i', FLAG_ON);
560       interactive = 1;
561     }
562
563 #if defined (RESTRICTED_SHELL)
564   /* Set restricted_shell based on whether the basename of $0 indicates that
565      the shell should be restricted or if the `-r' option was supplied at
566      startup. */
567   restricted_shell = shell_is_restricted (shell_name);
568
569   /* If the `-r' option is supplied at invocation, make sure that the shell
570      is not in restricted mode when running the startup files. */
571   saverst = restricted;
572   restricted = 0;
573 #endif
574
575   /* The startup files are run with `set -e' temporarily disabled. */
576   if (locally_skip_execution == 0 && running_setuid == 0)
577     {
578       old_errexit_flag = exit_immediately_on_error;
579       exit_immediately_on_error = 0;
580
581       run_startup_files ();
582       exit_immediately_on_error += old_errexit_flag;
583     }
584
585   /* If we are invoked as `sh', turn on Posix mode. */
586   if (act_like_sh)
587     {
588       bind_variable ("POSIXLY_CORRECT", "y");
589       sv_strict_posix ("POSIXLY_CORRECT");
590     }
591
592 #if defined (RESTRICTED_SHELL)
593   /* Turn on the restrictions after executing the startup files.  This
594      means that `bash -r' or `set -r' invoked from a startup file will
595      turn on the restrictions after the startup files are executed. */
596   restricted = saverst || restricted;
597   if (shell_reinitialized == 0)
598     maybe_make_restricted (shell_name);
599 #endif /* RESTRICTED_SHELL */
600
601   if (wordexp_only)
602     {
603       startup_state = 3;
604       last_command_exit_value = run_wordexp (argv[arg_index]);
605       exit_shell (last_command_exit_value);
606     }
607
608   if (local_pending_command)
609     {
610       arg_index = bind_args (argv, arg_index, argc, 0);
611       startup_state = 2;
612 #if defined (ONESHOT)
613       executing = 1;
614       run_one_command (local_pending_command);
615       exit_shell (last_command_exit_value);
616 #else /* ONESHOT */
617       with_input_from_string (local_pending_command, "-c");
618       goto read_and_execute;
619 #endif /* !ONESHOT */
620     }
621
622   /* Get possible input filename and set up default_buffered_input or
623      default_input as appropriate. */
624   if (arg_index != argc && read_from_stdin == 0)
625     {
626       open_shell_script (argv[arg_index]);
627       arg_index++;
628     }
629   else if (interactive == 0)
630     /* In this mode, bash is reading a script from stdin, which is a
631        pipe or redirected file. */
632 #if defined (BUFFERED_INPUT)
633     default_buffered_input = fileno (stdin);    /* == 0 */
634 #else
635     setbuf (default_input, (char *)NULL);
636 #endif /* !BUFFERED_INPUT */
637
638   set_bash_input ();
639
640   /* Bind remaining args to $1 ... $n */
641   arg_index = bind_args (argv, arg_index, argc, 1);
642   /* Do the things that should be done only for interactive shells. */
643   if (interactive_shell)
644     {
645       /* Set up for checking for presence of mail. */
646       remember_mail_dates ();
647       reset_mail_timer ();
648
649 #if defined (HISTORY)
650       /* Initialize the interactive history stuff. */
651       bash_initialize_history ();
652       if (shell_initialized == 0)
653         load_history ();
654 #endif /* HISTORY */
655
656       /* Initialize terminal state for interactive shells after the
657          .bash_profile and .bashrc are interpreted. */
658       get_tty_state ();
659     }
660
661 #if !defined (ONESHOT)
662  read_and_execute:
663 #endif /* !ONESHOT */
664
665   shell_initialized = 1;
666
667   /* Read commands until exit condition. */
668   reader_loop ();
669   exit_shell (last_command_exit_value);
670 }
671
672 static int
673 parse_long_options (argv, arg_start, arg_end)
674      char **argv;
675      int arg_start, arg_end;
676 {
677   int arg_index, longarg, i;
678   char *arg_string;
679
680   arg_index = arg_start;
681   while ((arg_index != arg_end) && (arg_string = argv[arg_index]) &&
682          (*arg_string == '-'))
683     {
684       longarg = 0;
685
686       /* Make --login equivalent to -login. */
687       if (arg_string[1] == '-' && arg_string[2])
688         {
689           longarg = 1;
690           arg_string++;
691         }
692
693       for (i = 0; long_args[i].name; i++)
694         {
695           if (STREQ (arg_string + 1, long_args[i].name))
696             {
697               if (long_args[i].type == Int)
698                 *long_args[i].int_value = 1;
699               else if (argv[++arg_index] == 0)
700                 {
701                   report_error ("option `%s' requires an argument",
702                                 long_args[i].name);
703                   exit (EX_USAGE);
704                 }
705               else
706                 *long_args[i].char_value = argv[arg_index];
707
708               break;
709             }
710         }
711       if (long_args[i].name == 0)
712         {
713           if (longarg)
714             {
715               report_error ("%s: unrecognized option", argv[arg_index]);
716               show_shell_usage (stderr, 0);
717               exit (EX_USAGE);
718             }
719           break;                /* No such argument.  Maybe flag arg. */
720         }
721
722       arg_index++;
723     }
724
725   return (arg_index);
726 }
727
728 static int
729 parse_shell_options (argv, arg_start, arg_end)
730      char **argv;
731      int arg_start, arg_end;
732 {
733   int arg_index;
734   int arg_character, on_or_off, next_arg, i;
735   char *o_option, *arg_string;
736
737   arg_index = arg_start;
738   while (arg_index != arg_end && (arg_string = argv[arg_index]) &&
739          (*arg_string == '-' || *arg_string == '+'))
740     {
741       /* There are flag arguments, so parse them. */
742       next_arg = arg_index + 1;
743
744       /* A single `-' signals the end of options.  From the 4.3 BSD sh.
745          An option `--' means the same thing; this is the standard
746          getopt(3) meaning. */
747       if (arg_string[0] == '-' &&
748            (arg_string[1] == '\0' ||
749              (arg_string[1] == '-' && arg_string[2] == '\0')))
750         return (next_arg);
751
752       i = 1;
753       on_or_off = arg_string[0];
754       while (arg_character = arg_string[i++])
755         {
756           switch (arg_character)
757             {
758             case 'c':
759               want_pending_command = 1;
760               break;
761
762             case 's':
763               read_from_stdin = 1;
764               break;
765
766             case 'o':
767               o_option = argv[next_arg];
768               if (o_option == 0)
769                 {
770                   list_minus_o_opts (-1, (on_or_off == '-') ? 0 : 1);
771                   break;
772                 }
773               if (set_minus_o_option (on_or_off, o_option) != EXECUTION_SUCCESS)
774                 exit (EX_USAGE);
775               next_arg++;
776               break;
777
778             case 'O':
779               /* Since some of these can be overridden by the normal
780                  interactive/non-interactive shell initialization or
781                  initializing posix mode, we save the options and process
782                  them after initialization. */
783               o_option = argv[next_arg];
784               if (o_option == 0)
785                 {
786                   shopt_listopt (o_option, (on_or_off == '-') ? 0 : 1);
787                   break;
788                 }
789               add_shopt_to_alist (o_option, on_or_off);
790               next_arg++;
791               break;
792
793             case 'D':
794               dump_translatable_strings = 1;
795               break;
796
797             default:
798               if (change_flag (arg_character, on_or_off) == FLAG_ERROR)
799                 {
800                   report_error ("%c%c: unrecognized option", on_or_off, arg_character);
801                   show_shell_usage (stderr, 0);
802                   exit (EX_USAGE);
803                 }
804             }
805         }
806       /* Can't do just a simple increment anymore -- what about
807          "bash -abouo emacs ignoreeof -hP"? */
808       arg_index = next_arg;
809     }
810
811   return (arg_index);
812 }
813
814 /* Exit the shell with status S. */
815 void
816 exit_shell (s)
817      int s;
818 {
819   /* Do trap[0] if defined.  Allow it to override the exit status
820      passed to us. */
821   if (signal_is_trapped (0))
822     s = run_exit_trap ();
823
824 #if defined (PROCESS_SUBSTITUTION)
825   unlink_fifo_list ();
826 #endif /* PROCESS_SUBSTITUTION */
827
828 #if defined (HISTORY)
829   if (interactive_shell)
830     maybe_save_shell_history ();
831 #endif /* HISTORY */
832
833 #if defined (JOB_CONTROL)
834   /* If the user has run `shopt -s huponexit', hangup all jobs when we exit
835      an interactive login shell.  ksh does this unconditionally. */
836   if (interactive_shell && login_shell && hup_on_exit)
837     hangup_all_jobs ();
838
839   /* If this shell is interactive, terminate all stopped jobs and
840      restore the original terminal process group.  Don't do this if we're
841      in a subshell and calling exit_shell after, for example, a failed
842      word expansion. */
843   if (subshell_environment == 0)
844     end_job_control ();
845 #endif /* JOB_CONTROL */
846
847   /* Always return the exit status of the last command to our parent. */
848   exit (s);
849 }
850
851 #ifdef INCLUDE_UNUSED
852 /* A wrapper for exit that (optionally) can do other things, like malloc
853    statistics tracing. */
854 void
855 sh_exit (s)
856      int s;
857 {
858   exit (s);
859 }
860 #endif
861
862 /* Source the bash startup files.  If POSIXLY_CORRECT is non-zero, we obey
863    the Posix.2 startup file rules:  $ENV is expanded, and if the file it
864    names exists, that file is sourced.  The Posix.2 rules are in effect
865    for interactive shells only. (section 4.56.5.3) */
866
867 /* Execute ~/.bashrc for most shells.  Never execute it if
868    ACT_LIKE_SH is set, or if NO_RC is set.
869
870    If the executable file "/usr/gnu/src/bash/foo" contains:
871
872    #!/usr/gnu/bin/bash
873    echo hello
874
875    then:
876
877          COMMAND            EXECUTE BASHRC
878          --------------------------------
879          bash -c foo            NO
880          bash foo               NO
881          foo                    NO
882          rsh machine ls         YES (for rsh, which calls `bash -c')
883          rsh machine foo        YES (for shell started by rsh) NO (for foo!)
884          echo ls | bash         NO
885          login                  NO
886          bash                   YES
887 */
888
889 static void
890 execute_env_file (env_file)
891       char *env_file;
892 {
893   char *fn;
894
895   if (env_file && *env_file)
896     {
897       fn = expand_string_unsplit_to_string (env_file, Q_DOUBLE_QUOTES);
898       if (fn && *fn)
899         maybe_execute_file (fn, 1);
900       FREE (fn);
901     }
902 }
903
904 static void
905 run_startup_files ()
906 {
907 #if defined (JOB_CONTROL)
908   int old_job_control;
909 #endif
910   int sourced_login, run_by_ssh;
911
912   /* get the rshd/sshd case out of the way first. */
913   if (interactive_shell == 0 && no_rc == 0 && login_shell == 0 &&
914       act_like_sh == 0 && local_pending_command)
915     {
916 #ifdef SSH_SOURCE_BASHRC
917       run_by_ssh = (find_variable ("SSH_CLIENT") != (SHELL_VAR *)0) ||
918                    (find_variable ("SSH2_CLIENT") != (SHELL_VAR *)0);
919 #else
920       run_by_ssh = 0;
921 #endif
922
923       /* If we were run by sshd or we think we were run by rshd, execute
924          ~/.bashrc if we are a top-level shell. */
925       if ((run_by_ssh || isnetconn (fileno (stdin))) && shell_level < 2)
926         {
927 #ifdef SYS_BASHRC
928 #  if defined (__OPENNT)
929           maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
930 #  else
931           maybe_execute_file (SYS_BASHRC, 1);
932 #  endif
933 #endif
934           maybe_execute_file (bashrc_file, 1);
935           return;
936         }
937     }
938
939 #if defined (JOB_CONTROL)
940   /* Startup files should be run without job control enabled. */
941   old_job_control = interactive_shell ? set_job_control (0) : 0;
942 #endif
943
944   sourced_login = 0;
945
946   /* A shell begun with the --login flag that is not in posix mode runs
947      the login shell startup files, no matter whether or not it is
948      interactive.  If NON_INTERACTIVE_LOGIN_SHELLS is defined, run the
949      startup files if argv[0][0] == '-' as well. */
950 #if defined (NON_INTERACTIVE_LOGIN_SHELLS)
951   if (login_shell && posixly_correct == 0)
952 #else
953   if (login_shell < 0 && posixly_correct == 0)
954 #endif
955     {
956       /* We don't execute .bashrc for login shells. */
957       no_rc++;
958
959       /* Execute /etc/profile and one of the personal login shell
960          initialization files. */
961       if (no_profile == 0)
962         {
963           maybe_execute_file (SYS_PROFILE, 1);
964
965           if (act_like_sh)      /* sh */
966             maybe_execute_file ("~/.profile", 1);
967           else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
968                    (maybe_execute_file ("~/.bash_login", 1) == 0))      /* bash */
969             maybe_execute_file ("~/.profile", 1);
970         }
971
972       sourced_login = 1;
973     }
974
975   /* A non-interactive shell not named `sh' and not in posix mode reads and
976      executes commands from $BASH_ENV.  If `su' starts a shell with `-c cmd'
977      and `-su' as the name of the shell, we want to read the startup files.
978      No other non-interactive shells read any startup files. */
979   if (interactive_shell == 0 && !(su_shell && login_shell))
980     {
981       if (posixly_correct == 0 && act_like_sh == 0 && privileged_mode == 0 &&
982             sourced_env++ == 0)
983         execute_env_file (get_string_value ("BASH_ENV"));
984       return;
985     }
986
987   /* Interactive shell or `-su' shell. */
988   if (posixly_correct == 0)               /* bash, sh */
989     {
990       if (login_shell && sourced_login++ == 0)
991         {
992           /* We don't execute .bashrc for login shells. */
993           no_rc++;
994
995           /* Execute /etc/profile and one of the personal login shell
996              initialization files. */
997           if (no_profile == 0)
998             {
999               maybe_execute_file (SYS_PROFILE, 1);
1000
1001               if (act_like_sh)  /* sh */
1002                 maybe_execute_file ("~/.profile", 1);
1003               else if ((maybe_execute_file ("~/.bash_profile", 1) == 0) &&
1004                        (maybe_execute_file ("~/.bash_login", 1) == 0))  /* bash */
1005                 maybe_execute_file ("~/.profile", 1);
1006             }
1007         }
1008
1009       /* bash */
1010       if (act_like_sh == 0 && no_rc == 0)
1011         {
1012 #ifdef SYS_BASHRC
1013 #  if defined (__OPENNT)
1014           maybe_execute_file (_prefixInstallPath(SYS_BASHRC, NULL, 0), 1);
1015 #  else
1016           maybe_execute_file (SYS_BASHRC, 1);
1017 #  endif
1018 #endif
1019           maybe_execute_file (bashrc_file, 1);
1020         }
1021       /* sh */
1022       else if (act_like_sh && privileged_mode == 0 && sourced_env++ == 0)
1023         execute_env_file (get_string_value ("ENV"));
1024     }
1025   else          /* bash --posix, sh --posix */
1026     {
1027       /* bash and sh */
1028       if (interactive_shell && privileged_mode == 0 && sourced_env++ == 0)
1029         execute_env_file (get_string_value ("ENV"));
1030     }
1031
1032 #if defined (JOB_CONTROL)
1033   set_job_control (old_job_control);
1034 #endif
1035 }
1036
1037 #if defined (RESTRICTED_SHELL)
1038 /* Return 1 if the shell should be a restricted one based on NAME or the
1039    value of `restricted'.  Don't actually do anything, just return a
1040    boolean value. */
1041 int
1042 shell_is_restricted (name)
1043      char *name;
1044 {
1045   char *temp;
1046
1047   if (restricted)
1048     return 1;
1049   temp = base_pathname (name);
1050   return (STREQ (temp, RESTRICTED_SHELL_NAME));
1051 }
1052
1053 /* Perhaps make this shell a `restricted' one, based on NAME.  If the
1054    basename of NAME is "rbash", then this shell is restricted.  The
1055    name of the restricted shell is a configurable option, see config.h.
1056    In a restricted shell, PATH, SHELL, ENV, and BASH_ENV are read-only
1057    and non-unsettable.
1058    Do this also if `restricted' is already set to 1; maybe the shell was
1059    started with -r. */
1060 int
1061 maybe_make_restricted (name)
1062      char *name;
1063 {
1064   char *temp;
1065
1066   temp = base_pathname (shell_name);
1067   if (restricted || (STREQ (temp, RESTRICTED_SHELL_NAME)))
1068     {
1069       set_var_read_only ("PATH");
1070       set_var_read_only ("SHELL");
1071       set_var_read_only ("ENV");
1072       set_var_read_only ("BASH_ENV");
1073       restricted = 1;
1074     }
1075   return (restricted);
1076 }
1077 #endif /* RESTRICTED_SHELL */
1078
1079 /* Fetch the current set of uids and gids and return 1 if we're running
1080    setuid or setgid. */
1081 static int
1082 uidget ()
1083 {
1084   uid_t u;
1085
1086   u = getuid ();
1087   if (current_user.uid != u)
1088     {
1089       FREE (current_user.user_name);
1090       FREE (current_user.shell);
1091       FREE (current_user.home_dir);
1092       current_user.user_name = current_user.shell = current_user.home_dir = (char *)NULL;
1093     }
1094   current_user.uid = u;
1095   current_user.gid = getgid ();
1096   current_user.euid = geteuid ();
1097   current_user.egid = getegid ();
1098
1099   /* See whether or not we are running setuid or setgid. */
1100   return (current_user.uid != current_user.euid) ||
1101            (current_user.gid != current_user.egid);
1102 }
1103
1104 void
1105 disable_priv_mode ()
1106 {
1107   setuid (current_user.uid);
1108   setgid (current_user.gid);
1109   current_user.euid = current_user.uid;
1110   current_user.egid = current_user.gid;
1111 }
1112
1113 static int
1114 run_wordexp (words)
1115      char *words;
1116 {
1117   int code, nw, nb;
1118   WORD_LIST *wl, *result;
1119
1120   code = setjmp (top_level);
1121
1122   if (code != NOT_JUMPED)
1123     {
1124       switch (code)
1125         {
1126           /* Some kind of throw to top_level has occured. */
1127         case FORCE_EOF:
1128           return last_command_exit_value = 127;
1129         case EXITPROG:
1130           return last_command_exit_value;
1131         case DISCARD:
1132           return last_command_exit_value = 1;
1133         default:
1134           command_error ("run_wordexp", CMDERR_BADJUMP, code, 0);
1135         }
1136     }
1137
1138   /* Run it through the parser to get a list of words and expand them */
1139   if (words && *words)
1140     {
1141       with_input_from_string (words, "--wordexp");
1142       if (parse_command () != 0)
1143         return (126);
1144       if (global_command == 0)
1145         {
1146           printf ("0\n0\n");
1147           return (0);
1148         }
1149       if (global_command->type != cm_simple)
1150         return (126);
1151       wl = global_command->value.Simple->words;
1152       result = wl ? expand_words_no_vars (wl) : (WORD_LIST *)0;
1153     }
1154   else
1155     result = (WORD_LIST *)0;
1156
1157   last_command_exit_value = 0;
1158
1159   if (result == 0)
1160     {
1161       printf ("0\n0\n");
1162       return (0);
1163     }
1164
1165   /* Count up the number of words and bytes, and print them.  Don't count
1166      the trailing NUL byte. */
1167   for (nw = nb = 0, wl = result; wl; wl = wl->next)
1168     {
1169       nw++;
1170       nb += strlen (wl->word->word);
1171     }
1172   printf ("%u\n%u\n", nw, nb);
1173   /* Print each word on a separate line.  This will have to be changed when
1174      the interface to glibc is completed. */
1175   for (wl = result; wl; wl = wl->next)
1176     printf ("%s\n", wl->word->word);
1177
1178   return (0);
1179 }
1180
1181 #if defined (ONESHOT)
1182 /* Run one command, given as the argument to the -c option.  Tell
1183    parse_and_execute not to fork for a simple command. */
1184 static int
1185 run_one_command (command)
1186      char *command;
1187 {
1188   int code;
1189
1190   code = setjmp (top_level);
1191
1192   if (code != NOT_JUMPED)
1193     {
1194 #if defined (PROCESS_SUBSTITUTION)
1195       unlink_fifo_list ();
1196 #endif /* PROCESS_SUBSTITUTION */
1197       switch (code)
1198         {
1199           /* Some kind of throw to top_level has occured. */
1200         case FORCE_EOF:
1201           return last_command_exit_value = 127;
1202         case EXITPROG:
1203           return last_command_exit_value;
1204         case DISCARD:
1205           return last_command_exit_value = 1;
1206         default:
1207           command_error ("run_one_command", CMDERR_BADJUMP, code, 0);
1208         }
1209     }
1210    return (parse_and_execute (savestring (command), "-c", SEVAL_NOHIST));
1211 }
1212 #endif /* ONESHOT */
1213
1214 static int
1215 bind_args (argv, arg_start, arg_end, start_index)
1216      char **argv;
1217      int arg_start, arg_end, start_index;
1218 {
1219   register int i;
1220   WORD_LIST *args;
1221
1222   for (i = arg_start, args = (WORD_LIST *)NULL; i != arg_end; i++)
1223     args = make_word_list (make_word (argv[i]), args);
1224   if (args)
1225     {
1226       args = REVERSE_LIST (args, WORD_LIST *);
1227       if (start_index == 0)     /* bind to $0...$n for sh -c command */
1228         {
1229           /* Posix.2 4.56.3 says that the first argument after sh -c command
1230              becomes $0, and the rest of the arguments become $1...$n */
1231           shell_name = savestring (args->word->word);
1232           FREE (dollar_vars[0]);
1233           dollar_vars[0] = savestring (args->word->word);
1234           remember_args (args->next, 1);
1235         }
1236       else                      /* bind to $1...$n for shell script */
1237         remember_args (args, 1);
1238
1239       dispose_words (args);
1240     }
1241
1242   return (i);
1243 }
1244
1245 void
1246 unbind_args ()
1247 {
1248   remember_args ((WORD_LIST *)NULL, 1);
1249 }
1250
1251 static int
1252 open_shell_script (script_name)
1253      char *script_name;
1254 {
1255   int fd, e, fd_is_tty;
1256   char *filename, *path_filename;
1257   char sample[80];
1258   int sample_len;
1259   struct stat sb;
1260
1261   free (dollar_vars[0]);
1262   dollar_vars[0] = savestring (script_name);
1263   filename = savestring (script_name);
1264
1265   fd = open (filename, O_RDONLY);
1266   if ((fd < 0) && (errno == ENOENT) && (absolute_program (filename) == 0))
1267     {
1268       e = errno;
1269       /* If it's not in the current directory, try looking through PATH
1270          for it. */
1271       path_filename = find_path_file (script_name);
1272       if (path_filename)
1273         {
1274           free (filename);
1275           filename = path_filename;
1276           fd = open (filename, O_RDONLY);
1277         }
1278       else
1279         errno = e;
1280     }
1281
1282   if (fd < 0)
1283     {
1284       e = errno;
1285       file_error (filename);
1286       exit ((e == ENOENT) ? EX_NOTFOUND : EX_NOINPUT);
1287     }
1288
1289 #ifdef HAVE_DEV_FD
1290   fd_is_tty = isatty (fd);
1291 #else
1292   fd_is_tty = 0;
1293 #endif
1294
1295   /* Only do this with non-tty file descriptors we can seek on. */
1296   if (fd_is_tty == 0 && (lseek (fd, 0L, 1) != -1))
1297     {
1298       /* Check to see if the `file' in `bash file' is a binary file
1299          according to the same tests done by execute_simple_command (),
1300          and report an error and exit if it is. */
1301       sample_len = read (fd, sample, sizeof (sample));
1302       if (sample_len < 0)
1303         {
1304           e = errno;
1305           if ((fstat (fd, &sb) == 0) && S_ISDIR (sb.st_mode))
1306             internal_error ("%s: is a directory", filename);
1307           else
1308             {
1309               errno = e;
1310               file_error (filename);
1311             }
1312           exit (EX_NOEXEC);
1313         }
1314       else if (sample_len > 0 && (check_binary_file (sample, sample_len)))
1315         {
1316           internal_error ("%s: cannot execute binary file", filename);
1317           exit (EX_BINARY_FILE);
1318         }
1319       /* Now rewind the file back to the beginning. */
1320       lseek (fd, 0L, 0);
1321     }
1322
1323   /* Open the script.  But try to move the file descriptor to a randomly
1324      large one, in the hopes that any descriptors used by the script will
1325      not match with ours. */
1326   fd = move_to_high_fd (fd, 0, -1);
1327
1328 #if defined (__CYGWIN__) && defined (O_TEXT)
1329   setmode (fd, O_TEXT);
1330 #endif
1331
1332 #if defined (BUFFERED_INPUT)
1333   default_buffered_input = fd;
1334   SET_CLOSE_ON_EXEC (default_buffered_input);
1335 #else /* !BUFFERED_INPUT */
1336   default_input = fdopen (fd, "r");
1337
1338   if (default_input == 0)
1339     {
1340       file_error (filename);
1341       exit (EX_NOTFOUND);
1342     }
1343
1344   SET_CLOSE_ON_EXEC (fd);
1345   if (fileno (default_input) != fd)
1346     SET_CLOSE_ON_EXEC (fileno (default_input));
1347 #endif /* !BUFFERED_INPUT */
1348
1349   /* Just about the only way for this code to be executed is if something
1350      like `bash -i /dev/stdin' is executed. */
1351   if (interactive_shell && fd_is_tty)
1352     {
1353       dup2 (fd, 0);
1354       close (fd);
1355       fd = 0;
1356 #if defined (BUFFERED_INPUT)
1357       default_buffered_input = 0;
1358 #else
1359       fclose (default_input);
1360       default_input = stdin;
1361 #endif
1362     }
1363   else if (forced_interactive && fd_is_tty == 0)
1364     /* But if a script is called with something like `bash -i scriptname',
1365        we need to do a non-interactive setup here, since we didn't do it
1366        before. */
1367     init_noninteractive ();
1368
1369   free (filename);
1370   return (fd);
1371 }
1372
1373 /* Initialize the input routines for the parser. */
1374 static void
1375 set_bash_input ()
1376 {
1377   /* Make sure the fd from which we are reading input is not in
1378      no-delay mode. */
1379 #if defined (BUFFERED_INPUT)
1380   if (interactive == 0)
1381     sh_unset_nodelay_mode (default_buffered_input);
1382   else
1383 #endif /* !BUFFERED_INPUT */
1384     sh_unset_nodelay_mode (fileno (stdin));
1385
1386   /* with_input_from_stdin really means `with_input_from_readline' */
1387   if (interactive && no_line_editing == 0)
1388     with_input_from_stdin ();
1389   else
1390 #if defined (BUFFERED_INPUT)
1391     {
1392       if (interactive == 0)
1393         with_input_from_buffered_stream (default_buffered_input, dollar_vars[0]);
1394       else
1395         with_input_from_stream (default_input, dollar_vars[0]);
1396     }
1397 #else /* !BUFFERED_INPUT */
1398     with_input_from_stream (default_input, dollar_vars[0]);
1399 #endif /* !BUFFERED_INPUT */
1400 }
1401
1402 /* Close the current shell script input source and forget about it.  This is
1403    extern so execute_cmd.c:initialize_subshell() can call it.  If CHECK_ZERO
1404    is non-zero, we close default_buffered_input even if it's the standard
1405    input (fd 0). */
1406 void
1407 unset_bash_input (check_zero)
1408      int check_zero;
1409 {
1410 #if defined (BUFFERED_INPUT)
1411   if ((check_zero && default_buffered_input >= 0) ||
1412       (check_zero == 0 && default_buffered_input > 0))
1413     {
1414       close_buffered_fd (default_buffered_input);
1415       default_buffered_input = bash_input.location.buffered_fd = -1;
1416     }
1417 #else /* !BUFFERED_INPUT */
1418   if (default_input)
1419     {
1420       fclose (default_input);
1421       default_input = (FILE *)NULL;
1422     }
1423 #endif /* !BUFFERED_INPUT */
1424 }
1425       
1426
1427 #if !defined (PROGRAM)
1428 #  define PROGRAM "bash"
1429 #endif
1430
1431 static void
1432 set_shell_name (argv0)
1433      char *argv0;
1434 {
1435   /* Here's a hack.  If the name of this shell is "sh", then don't do
1436      any startup files; just try to be more like /bin/sh. */
1437   shell_name = base_pathname (argv0);
1438   if (*shell_name == '-')
1439     shell_name++;
1440   if (shell_name[0] == 's' && shell_name[1] == 'h' && shell_name[2] == '\0')
1441     act_like_sh++;
1442   if (shell_name[0] == 's' && shell_name[1] == 'u' && shell_name[2] == '\0')
1443     su_shell++;
1444
1445   shell_name = argv0;
1446   FREE (dollar_vars[0]);
1447   dollar_vars[0] = savestring (shell_name);
1448
1449   if (*shell_name == '-')
1450     {
1451       shell_name++;
1452       login_shell++;
1453     }
1454
1455   /* A program may start an interactive shell with
1456           "execl ("/bin/bash", "-", NULL)".
1457      If so, default the name of this shell to our name. */
1458   if (!shell_name || !*shell_name || (shell_name[0] == '-' && !shell_name[1]))
1459     shell_name = PROGRAM;
1460 }
1461
1462 static void
1463 init_interactive ()
1464 {
1465   interactive_shell = startup_state = interactive = 1;
1466   expand_aliases = 1;
1467 }
1468
1469 static void
1470 init_noninteractive ()
1471 {
1472 #if defined (HISTORY)
1473   bash_history_reinit (0);
1474 #endif /* HISTORY */
1475   interactive_shell = startup_state = interactive = 0;
1476   expand_aliases = 0;
1477   no_line_editing = 1;
1478 #if defined (JOB_CONTROL)
1479   set_job_control (0);
1480 #endif /* JOB_CONTROL */
1481 }
1482
1483 void
1484 get_current_user_info ()
1485 {
1486   struct passwd *entry;
1487
1488   /* Don't fetch this more than once. */
1489   if (current_user.user_name == 0)
1490     {
1491       entry = getpwuid (current_user.uid);
1492       if (entry)
1493         {
1494           current_user.user_name = savestring (entry->pw_name);
1495           current_user.shell = (entry->pw_shell && entry->pw_shell[0])
1496                                 ? savestring (entry->pw_shell)
1497                                 : savestring ("/bin/sh");
1498           current_user.home_dir = savestring (entry->pw_dir);
1499         }
1500       else
1501         {
1502           current_user.user_name = savestring ("I have no name!");
1503           current_user.shell = savestring ("/bin/sh");
1504           current_user.home_dir = savestring ("/");
1505         }
1506       endpwent ();
1507     }
1508 }
1509
1510 /* Do whatever is necessary to initialize the shell.
1511    Put new initializations in here. */
1512 static void
1513 shell_initialize ()
1514 {
1515   char hostname[256];
1516
1517   /* Line buffer output for stderr and stdout. */
1518   if (shell_initialized == 0)
1519     {
1520       sh_setlinebuf (stderr);
1521       sh_setlinebuf (stdout);
1522     }
1523
1524   /* Sort the array of shell builtins so that the binary search in
1525      find_shell_builtin () works correctly. */
1526   initialize_shell_builtins ();
1527
1528   /* Initialize the trap signal handlers before installing our own
1529      signal handlers.  traps.c:restore_original_signals () is responsible
1530      for restoring the original default signal handlers.  That function
1531      is called when we make a new child. */
1532   initialize_traps ();
1533   initialize_signals ();
1534
1535   /* It's highly unlikely that this will change. */
1536   if (current_host_name == 0)
1537     {
1538       /* Initialize current_host_name. */
1539       if (gethostname (hostname, 255) < 0)
1540         current_host_name = "??host??";
1541       else
1542         current_host_name = savestring (hostname);
1543     }
1544
1545   /* Initialize the stuff in current_user that comes from the password
1546      file.  We don't need to do this right away if the shell is not
1547      interactive. */
1548   if (interactive_shell)
1549     get_current_user_info ();
1550
1551   /* Initialize our interface to the tilde expander. */
1552   tilde_initialize ();
1553
1554   /* Initialize internal and environment variables.  Don't import shell
1555      functions from the environment if we are running in privileged or
1556      restricted mode or if the shell is running setuid. */
1557 #if defined (RESTRICTED_SHELL)
1558   initialize_shell_variables (shell_environment, privileged_mode||restricted||running_setuid);
1559 #else
1560   initialize_shell_variables (shell_environment, privileged_mode||running_setuid);
1561 #endif
1562
1563 #if 0
1564   /* Initialize filename hash tables. */
1565   initialize_filename_hashing ();
1566 #endif
1567
1568   /* Initialize the data structures for storing and running jobs. */
1569   initialize_job_control (0);
1570
1571   /* Initialize input streams to null. */
1572   initialize_bash_input ();
1573
1574   /* Initialize the shell options.  Don't import the shell options
1575      from the environment variable $SHELLOPTS if we are running in
1576      privileged or restricted mode or if the shell is running setuid. */
1577 #if defined (RESTRICTED_SHELL)
1578   initialize_shell_options (privileged_mode||restricted||running_setuid);
1579 #else
1580   initialize_shell_options (privileged_mode||running_setuid);
1581 #endif
1582 }
1583
1584 /* Function called by main () when it appears that the shell has already
1585    had some initialization performed.  This is supposed to reset the world
1586    back to a pristine state, as if we had been exec'ed. */
1587 static void
1588 shell_reinitialize ()
1589 {
1590   /* The default shell prompts. */
1591   primary_prompt = PPROMPT;
1592   secondary_prompt = SPROMPT;
1593
1594   /* Things that get 1. */
1595   current_command_number = 1;
1596
1597   /* We have decided that the ~/.bashrc file should not be executed
1598      for the invocation of each shell script.  If the variable $ENV
1599      (or $BASH_ENV) is set, its value is used as the name of a file
1600      to source. */
1601   no_rc = no_profile = 1;
1602
1603   /* Things that get 0. */
1604   login_shell = make_login_shell = interactive = executing = 0;
1605   debugging = do_version = line_number = last_command_exit_value = 0;
1606   forced_interactive = interactive_shell = subshell_environment = 0;
1607   expand_aliases = 0;
1608
1609 #if defined (HISTORY)
1610   bash_history_reinit (0);
1611 #endif /* HISTORY */
1612
1613 #if defined (RESTRICTED_SHELL)
1614   restricted = 0;
1615 #endif /* RESTRICTED_SHELL */
1616
1617   /* Ensure that the default startup file is used.  (Except that we don't
1618      execute this file for reinitialized shells). */
1619   bashrc_file = "~/.bashrc";
1620
1621   /* Delete all variables and functions.  They will be reinitialized when
1622      the environment is parsed. */
1623   delete_all_variables (shell_variables);
1624   delete_all_variables (shell_functions);
1625
1626   shell_reinitialized = 1;
1627 }
1628
1629 static void
1630 show_shell_usage (fp, extra)
1631      FILE *fp;
1632      int extra;
1633 {
1634   int i;
1635   char *set_opts, *s, *t;
1636
1637   if (extra)
1638     fprintf (fp, "GNU bash, version %s-(%s)\n", shell_version_string (), MACHTYPE);
1639   fprintf (fp, "Usage:\t%s [GNU long option] [option] ...\n\t%s [GNU long option] [option] script-file ...\n",
1640              shell_name, shell_name);
1641   fputs ("GNU long options:\n", fp);
1642   for (i = 0; long_args[i].name; i++)
1643     fprintf (fp, "\t--%s\n", long_args[i].name);
1644
1645   fputs ("Shell options:\n", fp);
1646   fputs ("\t-irsD or -c command or -O shopt_option\t\t(invocation only)\n", fp);
1647
1648   for (i = 0, set_opts = 0; shell_builtins[i].name; i++)
1649     if (STREQ (shell_builtins[i].name, "set"))
1650       set_opts = savestring (shell_builtins[i].short_doc);
1651   if (set_opts)
1652     {
1653       s = strchr (set_opts, '[');
1654       if (s == 0)
1655         s = set_opts;
1656       while (*++s == '-')
1657         ;
1658       t = strchr (s, ']');
1659       if (t)
1660         *t = '\0';
1661       fprintf (fp, "\t-%s or -o option\n", s);
1662       free (set_opts);
1663     }
1664
1665   if (extra)
1666     {
1667       fprintf (fp, "Type `%s -c \"help set\"' for more information about shell options.\n", shell_name);
1668       fprintf (fp, "Type `%s -c help' for more information about shell builtin commands.\n", shell_name);
1669       fprintf (fp, "Use the `bashbug' command to report bugs.\n");
1670     }
1671 }
1672
1673 static void
1674 add_shopt_to_alist (opt, on_or_off)
1675      char *opt;
1676      int on_or_off;
1677 {
1678   if (shopt_ind >= shopt_len)
1679     {
1680       shopt_len += 8;
1681       shopt_alist = (STRING_INT_ALIST *)xrealloc (shopt_alist, shopt_len * sizeof (shopt_alist[0]));
1682     }
1683   shopt_alist[shopt_ind].word = opt;
1684   shopt_alist[shopt_ind].token = on_or_off;
1685   shopt_ind++;
1686 }
1687
1688 static void
1689 run_shopt_alist ()
1690 {
1691   register int i;
1692
1693   for (i = 0; i < shopt_ind; i++)
1694     if (shopt_setopt (shopt_alist[i].word, (shopt_alist[i].token == '-')) != EXECUTION_SUCCESS)
1695       exit (EX_USAGE);
1696   free (shopt_alist);
1697   shopt_alist = 0;
1698   shopt_ind = shopt_len = 0;
1699 }
1700
1701 /* The second and subsequent conditions must match those used to decide
1702    whether or not to call getpeername() in isnetconn(). */
1703 #if defined (HAVE_SYS_SOCKET_H) && defined (HAVE_GETPEERNAME) && !defined (SVR4_2)
1704 #  include <sys/socket.h>
1705 #endif
1706
1707 /* Is FD a socket or network connection? */
1708 static int
1709 isnetconn (fd)
1710      int fd;
1711 {
1712 #if defined (HAVE_GETPEERNAME) && !defined (SVR4_2) && !defined (__BEOS__)
1713   int rv;
1714   socklen_t l;
1715   struct sockaddr sa;
1716
1717   l = sizeof(sa);
1718   rv = getpeername(fd, &sa, &l);
1719   /* Solaris 2.5 getpeername() returns EINVAL if the fd is not a socket. */
1720   return ((rv < 0 && (errno == ENOTSOCK || errno == EINVAL)) ? 0 : 1);
1721 #else /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */
1722 #  if defined (SVR4) || defined (SVR4_2)
1723   /* Sockets on SVR4 and SVR4.2 are character special (streams) devices. */
1724   struct stat sb;
1725
1726   if (isatty (fd))
1727     return (0);
1728   if (fstat (fd, &sb) < 0)
1729     return (0);
1730 #    if defined (S_ISFIFO)
1731   if (S_ISFIFO (sb.st_mode))
1732     return (0);
1733 #    endif /* S_ISFIFO */
1734   return (S_ISCHR (sb.st_mode));
1735 #  else /* !SVR4 && !SVR4_2 */
1736 #    if defined (S_ISSOCK) && !defined (__BEOS__)
1737   struct stat sb;
1738
1739   if (fstat (fd, &sb) < 0)
1740     return (0);
1741   return (S_ISSOCK (sb.st_mode));
1742 #    else /* !S_ISSOCK || __BEOS__ */
1743   return (0);
1744 #    endif /* !S_ISSOCK || __BEOS__ */
1745 #  endif /* !SVR4 && !SVR4_2 */
1746 #endif /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */
1747 }