home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / BASH_112.ZIP / BASH-112.TAR / bash-1.12 / shell.c < prev    next >
C/C++ Source or Header  |  1993-06-30  |  42KB  |  1,648 lines

  1. /* shell.c -- GNU's idea of the POSIX shell specification.
  2.  
  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
  7.    COPYING.
  8.  
  9.    Copyright (C) 1988, 1991 Free Software Foundation, Inc.
  10.  
  11.    This file is part of GNU Bash.
  12.  
  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
  18.    details.
  19.  
  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.
  25.  
  26.    Among other things, the copyright notice and this notice must be
  27.    preserved on all copies.
  28.  
  29.   Birthdate:
  30.   Sunday, January 10th, 1988.
  31.   Initial author: Brian Fox
  32. */
  33.  
  34. #include <stdio.h>
  35. #include <sys/types.h>
  36. #include <signal.h>
  37. #include <errno.h>
  38. #include <sys/file.h>
  39. #include <pwd.h>
  40. #include "posixstat.h"
  41. #include "filecntl.h"
  42.  
  43. #if defined (HAVE_VFPRINTF)
  44. #include <varargs.h>
  45. #endif
  46.  
  47. #include "shell.h"
  48. #include "flags.h"
  49.  
  50. #if defined (JOB_CONTROL)
  51. #include "jobs.h"
  52. #endif /* JOB_CONTROL */
  53.  
  54. #if defined (USG) && !defined (isc386) && !defined (sgi)
  55. struct passwd *getpwuid ();
  56. #endif
  57.  
  58. extern char *dist_version;
  59. extern int build_version;
  60. extern void using_history ();
  61.  
  62. #if defined(__DEBUG__)
  63. extern FILE        *debugFile;
  64. #endif
  65.  
  66. extern int yydebug;
  67. #if !defined (errno)
  68. extern int errno;
  69. #endif
  70.  
  71. /* Non-zero means that this shell has already been run; i.e. you should
  72.    call shell_reinitialize () if you need to start afresh. */
  73. int shell_initialized = 0;
  74.  
  75. /* The current maintainer of the shell.  You change this in the
  76.    Makefile. */
  77. #if !defined (MAINTAINER)
  78. #define MAINTAINER "deliberately-anonymous"
  79. #endif
  80.  
  81. char *the_current_maintainer = MAINTAINER;
  82.  
  83. #ifndef PPROMPT
  84. #define PPROMPT "bash\\$ "
  85. #endif
  86. char *primary_prompt = PPROMPT;
  87.  
  88. #if !defined (SPROMPT)
  89. #  define SPROMPT "> "
  90. #endif
  91.  
  92. char *secondary_prompt = SPROMPT;
  93.  
  94. COMMAND *global_command = (COMMAND *)NULL;
  95.  
  96. /* Non-zero after SIGINT. */
  97. int interrupt_state = 0;
  98.  
  99. /* The current user's name. */
  100. char *current_user_name = (char *)NULL;
  101.  
  102. /* The current host's name. */
  103. char *current_host_name = (char *)NULL;
  104.  
  105. /* Non-zero means that this shell is a login shell.
  106.    Specifically:
  107.    0 = not 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.
  111.  */
  112. int login_shell = 0;
  113.  
  114. /* Non-zero means that at this moment, the shell is interactive. */
  115. int interactive = 0;
  116.  
  117. /* Non-zero means that the shell was started as an interactive shell. */
  118. int interactive_shell = 0;
  119.  
  120. /* Non-zero means to remember lines typed to the shell on the history
  121.    list.  This is different than the user-controlled behaviour; this
  122.    becomes zero when we read lines from a file, for example. */
  123. int remember_on_history = 1;
  124.  
  125. /* Non-zero means this shell is restricted. */
  126. int restricted = 0;
  127.  
  128. /* Special debugging helper. */
  129. int debugging_login_shell = 0;
  130.  
  131. /* The environment that the shell passes to other commands. */
  132. char **shell_environment;
  133.  
  134. /* Non-zero when we are executing a top-level command. */
  135. int executing = 0;
  136.  
  137. /* The number of commands executed so far. */
  138. int current_command_number = 1;
  139.  
  140. /* The environment at the top-level REP loop.  We use this in the case of
  141.    error return. */
  142. jmp_buf top_level, catch;
  143.  
  144. #if defined (JOB_CONTROL) || defined (_POSIX_VERSION)
  145. /* The signal masks that this shell runs with. */
  146. sigset_t top_level_mask;
  147. #endif /* JOB_CONTROL */
  148.  
  149. /* Non-zero is the recursion depth for commands. */
  150. int indirection_level = 0;
  151.  
  152. /* The number of times BASH has been executed.  This is set
  153.    by initialize_variables () in variables.c. */
  154. int shell_level = 0;
  155.  
  156. /* The name of this shell, as taken from argv[0]. */
  157. char *shell_name = (char *)NULL;
  158.  
  159. /* time in seconds when the shell was started */
  160. time_t shell_start_time;
  161.  
  162. /* The name of the .(shell)rc file. */
  163. char *bashrc_file = "~/.bashrc";
  164.  
  165. /* Non-zero means to act more like the Bourne shell on startup. */
  166. int act_like_sh = 0;
  167.  
  168. /* Values for the long-winded argument names. */
  169. int debugging = 0;        /* Do debugging things. */
  170. int no_rc = 0;            /* Don't execute ~/.bashrc */
  171. int no_profile = 0;        /* Don't execute .profile */
  172. int do_version = 0;        /* Display interesting version info. */
  173. int quiet = 0;            /* Be quiet when starting up. */
  174. int make_login_shell = 0;    /* Make this shell be a `-bash' shell. */
  175. int no_line_editing = 0;    /* Don't do fancy line editing. */
  176. int no_brace_expansion = 0;    /* Non-zero means no foo{a,b} -> fooa fooa. */
  177.  
  178. /* Some long-winded argument names.  These are obviously new. */
  179. #define Int 1
  180. #define Charp 2
  181. struct {
  182.   char *name;
  183.   int *value;
  184.   int type;
  185. } long_args[] = {
  186.   { "debug", &debugging, Int },
  187.   { "norc", &no_rc, Int },
  188.   { "noprofile", &no_profile, Int },
  189.   { "rcfile", (int *)&bashrc_file, Charp},
  190.   { "version", &do_version, Int},
  191.   { "quiet", &quiet, Int},
  192.   { "login", &make_login_shell, Int},
  193.   { "nolineediting", &no_line_editing, Int},
  194.   { "nobraceexpansion", &no_brace_expansion, Int},
  195.   { (char *)NULL, (int *)0x0, 0 }
  196. };
  197.  
  198. /* The number of lines that Bash has added to this history session. */
  199. int history_lines_this_session = 0;
  200.  
  201. /* The number of lines that Bash has read from the history file. */
  202. int history_lines_in_file = 0;
  203.  
  204. /* These are extern so execute_simple_command can set them, and then
  205.    longjmp back to main to execute a shell script, instead of calling
  206.    main () again and resulting in indefinite, possibly fatal, stack
  207.    growth. */
  208. jmp_buf subshell_top_level;
  209. int subshell_argc;
  210. char **subshell_argv;
  211. char **subshell_envp;
  212.  
  213. /* A bunch of stuff for job control                2/93 ROB */
  214. #if defined(__EMX__)
  215. int        current_job,
  216.         job_slots,
  217.         jobs[50],
  218.         previous_job;
  219. #endif
  220.  
  221. main (argc, argv, env)
  222.      int argc;
  223.      char **argv, **env;
  224. {
  225.   extern int last_command_exit_value;
  226.   extern char *base_pathname ();
  227.   register int i;
  228.   int arg_index, locally_skip_execution;
  229.   int top_level_arg_index, read_from_stdin;
  230.   FILE *default_input;
  231.   char *local_pending_command = (char *)NULL;
  232. #if defined (JOB_CONTROL)
  233.   extern int job_control;
  234. #endif /* JOB_CONTROL */
  235.  
  236. #if defined (AUX)
  237. #include <compat.h>
  238.   set42sig ();
  239.   setcompat (getcompat() | COMPAT_BSDGROUPS | COMPAT_BSDSIGNALS |
  240.          COMPAT_BSDTTY | COMPAT_EXEC | COMPAT_SYSCALLS);
  241. #endif /* AUX */
  242.  
  243.   /* There is a bug in the NeXT 2.1 rlogind that causes opens
  244.      of /dev/tty to fail. */
  245. #if defined (NeXT)
  246.   {
  247.     int tty_fd;
  248.  
  249.     tty_fd = open ("/dev/tty", O_RDWR);
  250.  
  251.     if (tty_fd < 0)
  252.       {
  253.     char *tty;
  254.     tty = (char *)ttyname (fileno (stdin));
  255.     tty_fd = open (tty, O_RDWR);
  256.       }
  257.     close (tty_fd);
  258.   }
  259. #endif /* NeXT */
  260.  
  261. #if defined(__DEBUG__)
  262. debugFile = fopen("debug.out", "w");
  263. #endif
  264.  
  265. #if defined(__OS2__)
  266.     {
  267. #include <os2emx.h>
  268.     APIRET        rc;
  269.     ULONG        ReqCount,
  270.                 CurMaxFH;
  271.     
  272.     ReqCount = 0;
  273.     rc = DosSetRelMaxFH(&ReqCount, &CurMaxFH);
  274.     if(CurMaxFH < 60)
  275.         {ReqCount = 60 - CurMaxFH;
  276.         rc = DosSetRelMaxFH(&ReqCount, &CurMaxFH);
  277.         }
  278.     }
  279. #endif
  280.  
  281.   /* Wait forever if we are debugging a login shell. */
  282.   while (debugging_login_shell);
  283.  
  284.   if (setjmp (subshell_top_level))
  285.     {
  286.       argc = subshell_argc;
  287.       argv = subshell_argv;
  288.       env = subshell_envp;
  289.     }
  290.  
  291.   /* Initialize local variables for all `invocations' of main (). */
  292.   arg_index = 1;
  293.   local_pending_command = (char *)NULL;
  294.   locally_skip_execution = 0;
  295.   read_from_stdin = 0;
  296.   default_input = stdin;
  297.  
  298.   /* Fix for the `infinite process creation' bug when running shell scripts
  299.      from startup files on System V. */
  300.   login_shell = make_login_shell = 0;
  301.  
  302.   /* If this shell has already been run, then reinitialize it to a
  303.      vanilla state. */
  304.   if (shell_initialized || shell_name)
  305.     {
  306.       /* Make sure that we do not infinitely recurse as a login shell. */
  307.       if (*shell_name == '-')
  308.     shell_name++;
  309.  
  310.       shell_reinitialize ();
  311.       if (setjmp (top_level))
  312.     exit (2);
  313.     }
  314.  
  315.   /* Here's a hack.  If the name of this shell is "sh", then don't do
  316.      any startup files; just try to be more like /bin/sh. */
  317.   {
  318.     char *tshell_name = base_pathname (argv[0]);
  319.  
  320.     if (*tshell_name == '-')
  321.       tshell_name++;
  322.  
  323.     if (strcmp (tshell_name, "sh") == 0)
  324.       act_like_sh++;
  325.   }
  326.  
  327.   yydebug = 0;
  328.  
  329.   shell_environment = env;
  330.   shell_name = argv[0];
  331.   dollar_vars[0] = savestring (shell_name);
  332.  
  333.   if (*shell_name == '-')
  334.     {
  335.       shell_name++;
  336.       login_shell++;
  337.     }
  338.  
  339. #if defined (JOB_CONTROL)
  340.   if (act_like_sh)
  341.     job_control = 0;
  342. #endif /* JOB_CONTROL */
  343.  
  344.   shell_start_time = NOW;    /* NOW now defined in general.h */
  345.  
  346.   /* A program may start an interactive shell with
  347.         "execl ("/bin/bash", "-", NULL)".
  348.      If so, default the name of this shell to our name. */
  349.   if (!shell_name || !*shell_name || (strcmp (shell_name, "-") == 0))
  350.     shell_name = "bash";
  351.  
  352.   /* Parse argument flags from the input line. */
  353.  
  354.   /* Find full word arguments first. */
  355.   while ((arg_index != argc) && *(argv[arg_index]) == '-')
  356.     {
  357.       for (i = 0; long_args[i].name; i++)
  358.     {
  359.       if (strcmp (&(argv[arg_index][1]), long_args[i].name) == 0)
  360.         {
  361.           if (long_args[i].type == Int)
  362.         *(long_args[i].value) = 1;
  363.           else
  364.         {
  365.           if (!argv[++arg_index])
  366.             {
  367.               report_error ("%s: Flag `%s' expected an argument",
  368.                     shell_name, long_args[i].name);
  369.               exit (1);
  370.             }
  371.           else
  372.             *long_args[i].value = (int)argv[arg_index];
  373.         }
  374.           goto handle_next_arg;
  375.         }
  376.     }
  377.       break;            /* No such argument.  Maybe flag arg. */
  378.     handle_next_arg:
  379.       arg_index++;
  380.     }
  381.  
  382.   /* If user supplied the "-login" flag, then set and invert LOGIN_SHELL. */
  383.   if (make_login_shell)
  384.     login_shell = -++login_shell;
  385.  
  386.   /* All done with full word args; do standard shell arg parsing.*/
  387.   while (arg_index != argc && argv[arg_index] &&
  388.      (*(argv[arg_index]) == '-' || (*argv[arg_index] == '+')))
  389.     {
  390.       /* There are flag arguments, so parse them. */
  391.       int arg_character;
  392.       int on_or_off = (*argv[arg_index]);
  393.       char *o_option;
  394.       int next_arg = arg_index + 1;
  395.  
  396.       i = 1;
  397.  
  398.       /* A single `-' signals the end of options.  From the 4.3 BSD sh.
  399.      An option `--' means the same thing; this is the standard
  400.      getopt () meaning. */
  401.       if (((argv[arg_index][0] == '-') && (argv[arg_index][1] == '\0')) ||
  402.       (strcmp (argv[arg_index], "--") == 0))
  403.     {
  404.       arg_index++;
  405.       goto after_flags;
  406.     }
  407.  
  408.       while (arg_character = (argv[arg_index])[i++])
  409.     {
  410.       switch (arg_character)
  411.         {
  412.         case 'c':
  413.           /* The next arg is a command to execute, and the following args
  414.          are $1 .. $n respectively. */
  415.         local_pending_command = argv[++arg_index];
  416.         if (!local_pending_command)
  417.           {
  418.             report_error ("`%cc' requires an argument", on_or_off);
  419.             exit (1);
  420.           }
  421.         arg_index++;
  422.         goto after_flags;
  423.  
  424.         case 's':
  425.         read_from_stdin = 1;
  426.         break;
  427.  
  428.         case 'o':
  429.         o_option = argv[next_arg++];
  430.         if (!o_option)
  431.           {
  432.             report_error ("`%co' requires an argument", on_or_off);
  433.             exit (1);
  434.           }
  435.  
  436.         if (set_minus_o_option (on_or_off, o_option) != EXECUTION_SUCCESS)
  437.           exit (1);
  438.         break;
  439.  
  440.         default:
  441.           if (change_flag_char (arg_character, on_or_off) == FLAG_ERROR)
  442.         {
  443.           report_error ("%c%c: bad option", on_or_off, arg_character);
  444.           exit (1);
  445.         }
  446.  
  447.         }
  448.     }
  449.       /* Can't do just a simple increment anymore -- what about
  450.      "bash -abouo emacs ignoreeof -hO"? */
  451.       arg_index = next_arg;
  452.     }
  453.  
  454.  after_flags:
  455.  
  456.   /* First, let the outside world know about our interactive status.
  457.      A shell is interactive if the `-i' flag was given, or if all of
  458.      the following conditions are met:
  459.     no -c command
  460.     no arguments remaining or the -s flag given
  461.     standard input is a terminal
  462.     standard output is a terminal
  463.      Refer to Posix.2, the description of the `sh' utility. */
  464.  
  465.   if (forced_interactive ||        /* -i flag */
  466.       (!local_pending_command &&    /* No -c command and ... */
  467.        ((arg_index == argc) ||        /*   no remaining args or... */
  468.     read_from_stdin) &&        /*   -s flag with args, and */
  469.        isatty (fileno (stdin)) &&    /* Input is a terminal and */
  470.        isatty (fileno (stdout))))    /* output is a terminal. */
  471.     {
  472.       interactive_shell = 1;
  473.       interactive = 1;
  474.     }
  475.   else
  476.     {
  477.       history_expansion = 0;
  478.       remember_on_history = 0;
  479.       interactive_shell = 0;
  480.       interactive = 0;
  481. #if defined (JOB_CONTROL)
  482.       job_control = 0;
  483. #endif /* JOB_CONTROL */
  484.     }
  485.  
  486. #define CLOSE_FDS_AT_LOGIN
  487.  
  488. #if defined (CLOSE_FDS_AT_LOGIN)
  489.   /*
  490.    * Some systems have the bad habit of starting login shells with lots of open
  491.    * file descriptors.  For instance, most systems that have picked up the
  492.    * pre-4.0 Sun YP code leave a file descriptor open each time you call one
  493.    * of the getpw* functions, and it's set to be open across execs.  That
  494.    * means one for login, one for xterm, one for shelltool, etc.
  495.    */
  496.   if (login_shell && interactive_shell)
  497.     {
  498.       for (i = 3; i < 20; i++)
  499.     close (i);
  500.     }
  501. #endif /* CLOSE_FDS_AT_LOGIN */
  502.  
  503.   /* From here on in, the shell must be a normal functioning shell.
  504.      Variables from the environment are expected to be set, etc. */
  505.   shell_initialize ();
  506.  
  507.   if (interactive_shell)
  508.     {
  509.       char *term = (char *)getenv ("TERM");
  510.       if (term && (strcmp (term, "emacs") == 0))
  511.     no_line_editing = 1;
  512.     }
  513.  
  514.   top_level_arg_index = arg_index;
  515.  
  516.   if (!quiet && do_version)
  517.     show_shell_version ();
  518.  
  519.   /* Give this shell a place to longjmp to before executing the
  520.      startup files.  This allows users to press C-c to abort the
  521.      lengthy startup. */
  522.   {
  523.     int code;
  524.  
  525.     code = setjmp (top_level);
  526.  
  527.     if (code)
  528.       {
  529.     if (code == EXITPROG)
  530.       goto exit_shell;
  531.     else
  532.       locally_skip_execution++;
  533.       }
  534.   }
  535.  
  536.   arg_index = top_level_arg_index;
  537.  
  538.   /* Execute the start-up scripts. */
  539.  
  540.   if (!interactive_shell)
  541.     {
  542.       makunbound ("PS1", shell_variables);
  543.       makunbound ("PS2", shell_variables);
  544.       interactive = 0;
  545.     }
  546.   else
  547.     {
  548.       change_flag_char ('i', FLAG_ON);
  549.       interactive = 1;
  550.     }
  551.  
  552.   if (!locally_skip_execution)
  553.     {
  554.       if (login_shell)
  555.     {
  556.       /* We don't execute .bashrc for login shells. */
  557.       no_rc++;
  558. #if defined (NOTDEF)
  559.       if (getenv ("POSIXLY_CORRECT"))
  560. #endif /* NOTDEF */
  561.         maybe_execute_file ("/etc/profile");
  562.     }
  563.  
  564.       if (login_shell && !no_profile)
  565.     {
  566.       if (act_like_sh)
  567.         maybe_execute_file ("~/.profile");
  568.       else
  569.         {
  570.           if (maybe_execute_file ("~/.bash_profile") == 0)
  571.         if (maybe_execute_file ("~/.bash_login") == 0)
  572.           maybe_execute_file ("~/.profile");
  573.         }
  574.  
  575.     /* I turn on the restrictions afterwards because it is explictly
  576.        stated in the POSIX spec that PATH cannot be set in a restricted
  577.        shell, except in .profile. */
  578.       if (*++(argv[0]) == 'r')
  579.         {
  580.           set_var_read_only ("PATH");
  581.           set_var_read_only ("SHELL");
  582.           restricted++;
  583.         }
  584.     }
  585.  
  586.       /* Execute ~/.bashrc for most shells.  Never execute it if
  587.      ACT_LIKE_SH is set, or if NO_RC is set.
  588.  
  589.      If the executable file "/usr/gnu/src/bash/foo" contains:
  590.  
  591.        #!/usr/gnu/bin/bash
  592.        echo hello
  593.  
  594.      then:
  595.  
  596.      COMMAND        EXECUTE BASHRC
  597.      --------------------------------
  598.      bash -c foo        NO
  599.      bash foo        NO
  600.      foo            NO
  601.      rsh machine ls        YES (for rsh, which calls `bash -c')
  602.      rsh machine foo    YES (for shell started by rsh) NO (for foo!)
  603.      echo ls | bash        NO
  604.      login            YES
  605.      bash            YES
  606.       */
  607.       if (!act_like_sh && !no_rc &&
  608.       (interactive_shell || (!isatty (fileno (stdin)) &&
  609.                  local_pending_command)))
  610.     maybe_execute_file (bashrc_file);
  611.  
  612.       /* Try a TMB suggestion.  If running a script, then execute the
  613.      file mentioned in the ENV variable. */
  614.       if (!interactive_shell)
  615.     {
  616.       char *env_file = (char *)getenv ("ENV");
  617.       if (env_file && *env_file)
  618.         {
  619.           WORD_LIST *list, *expand_string_unsplit ();
  620.           char *expanded_file_name, *string_list ();
  621.  
  622.           list = expand_string_unsplit (env_file, 1);
  623.           if (list)
  624.         {
  625.           expanded_file_name = string_list (list);
  626.           dispose_words (list);
  627.           if (expanded_file_name && *expanded_file_name)
  628.             maybe_execute_file (expanded_file_name);
  629.           free (expanded_file_name);
  630.         }
  631.         }
  632.     }
  633.  
  634.       if (local_pending_command)
  635.     {
  636.       /* Bind remaining args to $1 ... $n */
  637.       WORD_LIST *args = (WORD_LIST *)NULL;
  638.       while (arg_index != argc)
  639.         args = make_word_list (make_word (argv[arg_index++]), args);
  640.       args = (WORD_LIST *)reverse_list (args);
  641.       remember_args (args, 1);
  642.       dispose_words (args);
  643.  
  644.       with_input_from_string (local_pending_command, "-c");
  645.       goto read_and_execute;
  646.     }
  647.     }
  648.  
  649.   /* Do the things that should be done only for interactive shells. */
  650.   if (interactive_shell)
  651.     {
  652.       /* Set up for checking for presence of mail. */
  653. #if defined (USG)
  654.       /* Under System V, we can only tell if you have mail if the
  655.      modification date has changed.  So remember the current
  656.      modification dates. */
  657.       remember_mail_dates ();
  658. #else
  659.       /* Under 4.x, you have mail if there is something in your inbox.
  660.      I set the remembered mail dates to 1900.  */
  661.       reset_mail_files ();
  662. #endif /* USG */
  663.  
  664.       /* If this was a login shell, then assume that /bin/login has already
  665.      taken care of informing the user that they have new mail.  Otherwise,
  666.      we want to check right away. */
  667.       if (login_shell == 1)
  668.     {
  669. #if !defined (USG)
  670.       remember_mail_dates ();
  671. #endif
  672.     }
  673.  
  674.       reset_mail_timer ();
  675.  
  676.       change_flag_char ('i', FLAG_ON);
  677.  
  678.       /* Initialize the interactive history stuff. */
  679.       if (!shell_initialized)
  680.     load_history ();
  681.  
  682.       /* Initialize terminal state for interactive shells after the
  683.      .bash_profile and .bashrc are interpreted. */
  684.       get_tty_state ();
  685.     }
  686.  
  687.   /* Get possible input filename. */
  688.   if ((arg_index != argc) && !read_from_stdin)
  689.     {
  690.       int fd;
  691.       char *filename;
  692.       extern char *find_path_file ();
  693.  
  694.       free (dollar_vars[0]);
  695.       dollar_vars[0] = savestring (argv[arg_index]);
  696.       filename = savestring (argv[arg_index]);
  697.  
  698.       fd = open (filename, O_RDONLY);
  699.       if ((fd < 0) && (errno == ENOENT))
  700.     {
  701.       char *path_filename;
  702.       /* If it's not in the current directory, try looking through PATH
  703.          for it. */
  704.       path_filename = find_path_file (argv[arg_index]);
  705.       if (path_filename)
  706.         {
  707.           free (filename);
  708.           filename = path_filename;
  709.           fd = open (filename, O_RDONLY);
  710.         }
  711.     }
  712.  
  713.       arg_index++;
  714.       if (fd < 0)
  715.     {
  716.       file_error (filename);
  717.       exit (1);
  718.     }
  719.  
  720.       /* Only do this with file descriptors we can seek on. */
  721.       if (lseek (fd, 0L, 1) != -1)
  722.     {
  723.       unsigned char sample[80];
  724.       int sample_len;
  725.  
  726.       /* Check to see if the `file' in `bash file' is a binary file
  727.          according to the same tests done by execute_simple_command (),
  728.          and report an error and exit if it is. */
  729.       sample_len = read (fd, sample, sizeof (sample));
  730.       if (sample_len > 0)
  731.         if (check_binary_file (sample, sample_len))
  732.           {
  733.         report_error ("%s: cannot execute binary file", filename);
  734.         exit (EX_BINARY_FILE);
  735.           }
  736.       /* Now rewind the file back to the beginning. */
  737.       lseek (fd, 0L, 0);
  738.     }
  739.  
  740.       default_input = fdopen (fd, "r");
  741.  
  742.       if (!default_input)
  743.     {
  744.       file_error (filename);
  745.       exit (127);
  746.     }
  747.  
  748.       SET_CLOSE_ON_EXEC (fd);
  749.       if (fileno (default_input) != fd)
  750.     SET_CLOSE_ON_EXEC (fileno (default_input));
  751.  
  752.       if (!interactive_shell || (!isatty (fd)))
  753.     {
  754.       history_expansion = 0;
  755.       remember_on_history = 0;
  756.       interactive = interactive_shell = 0;
  757. #if defined (JOB_CONTROL)
  758.       set_job_control (0);
  759. #endif /* JOB_CONTROL */
  760.     }
  761.       else
  762.     {
  763.       /* I don't believe that this code is ever executed, even in
  764.          the presence of /dev/fd. */
  765.       dup2 (fd, 0);
  766.       close (fd);
  767.       fclose (default_input);
  768.     }
  769.     }
  770.  
  771.   /* Bind remaining args to $1 ... $n */
  772.   {
  773.     WORD_LIST *args = (WORD_LIST *)NULL;
  774.     while (arg_index != argc)
  775.       args = make_word_list (make_word (argv[arg_index++]), args);
  776.     args = (WORD_LIST *)reverse_list (args);
  777.     remember_args (args, 1);
  778.     dispose_words (args);
  779.   }
  780.  
  781.   unset_nodelay_mode (fileno (stdin));
  782.  
  783.   /* with_input_from_stdin really means `with_input_from_readline' */
  784.   if (interactive && !no_line_editing)
  785.     with_input_from_stdin ();
  786.   else
  787.     with_input_from_stream (default_input, dollar_vars[0]);
  788.  
  789.  read_and_execute:
  790.  
  791.   shell_initialized = 1;
  792.  
  793.   /* Read commands until exit condition. */
  794.   reader_loop ();
  795.  
  796.   exit_shell:
  797.   /* Do trap[0] if defined. */
  798.   run_exit_trap ();
  799.  
  800.   maybe_save_shell_history ();
  801.  
  802. #if defined (JOB_CONTROL)
  803.   /* If this shell is interactive, terminate all stopped jobs and
  804.      restore the original terminal process group. */
  805.   if (interactive_shell)
  806.     {
  807.       terminate_stopped_jobs ();
  808.  
  809.       if (original_pgrp >= 0)
  810.     give_terminal_to (original_pgrp);
  811.     }
  812. #endif /* JOB_CONTROL */
  813.  
  814.   /* Always return the exit status of the last command to our parent. */
  815.   exit (last_command_exit_value);
  816. }
  817.  
  818. /* If this is an interactive shell, then append the lines executed
  819.    this session to the history file. */
  820. int
  821. maybe_save_shell_history ()
  822. {
  823.   int result = 0;
  824.  
  825.   if (interactive && history_lines_this_session)
  826.     {
  827.       void using_history ();
  828.       char *hf = get_string_value ("HISTFILE");
  829.  
  830.       if (hf && *hf)
  831.     {
  832.       struct stat buf;
  833.  
  834.       /* If the file doesn't exist, then create it. */
  835.       if (stat (hf, &buf) == -1)
  836.         {
  837.           int file = open (hf, O_CREAT | O_TRUNC | O_WRONLY, 0666);
  838.           if (file != -1)
  839.         close (file);
  840.         }
  841.  
  842.       /* Now actually append the lines if the history hasn't been
  843.          stifled. */
  844.       using_history ();
  845.       if (history_lines_this_session <= where_history ())
  846.         {
  847.           result = append_history (history_lines_this_session, hf);
  848.           history_lines_in_file += history_lines_this_session;
  849.           history_lines_this_session = 0;
  850.         }
  851.     }
  852.     }
  853.   return (result);
  854. }
  855.  
  856. /* Try to execute the contents of FNAME.  If FNAME doesn't exist,
  857.    that is not an error, but other kinds of errors are.  Returns
  858.    -1 in the case of an error, 0 in the case that the file was not
  859.    found, and 1 if the file was found and executed. */
  860. maybe_execute_file (fname)
  861.      char *fname;
  862. {
  863.   extern char *tilde_expand ();
  864.   extern int return_catch_flag;
  865.   extern jmp_buf return_catch;
  866.   jmp_buf old_return_catch;
  867.   int return_val, fd, tresult;
  868.   char *filename, *string;
  869.   struct stat file_info;
  870.  
  871.   filename = tilde_expand (fname);
  872.   fd = open (filename, O_RDONLY);
  873.  
  874.   if (fd < 0)
  875.     {
  876. file_error_and_exit:
  877.       if (errno != ENOENT)
  878.     file_error (filename);
  879.       free (filename);
  880.       return ((errno == ENOENT) ? 0 : -1);
  881.     }
  882.  
  883.   if (fstat (fd, &file_info) == -1)
  884.     goto file_error_and_exit;
  885.  
  886.   string = (char *)xmalloc (1 + file_info.st_size);
  887.   tresult = read (fd, string, file_info.st_size);
  888.  
  889.   {
  890.     int tt = errno;
  891.     close (fd);
  892.     errno = tt;
  893.   }
  894.  
  895.   if (tresult != file_info.st_size)
  896.     {
  897.       free (string);
  898.       goto file_error_and_exit;
  899.     }
  900.   string[file_info.st_size] = '\0';
  901.  
  902.   return_catch_flag++;
  903.   bcopy ((char *)return_catch, (char *)old_return_catch, sizeof (jmp_buf));
  904.  
  905.   return_val = setjmp (return_catch);
  906.  
  907.   /* If `return' was seen outside of a function, but in the script, then
  908.      force parse_and_execute () to clean up. */
  909.   if (return_val)
  910.     parse_and_execute_cleanup ();
  911.   else
  912.     tresult = parse_and_execute (string, filename);
  913.  
  914.   return_catch_flag--;
  915.   bcopy ((char *)old_return_catch, (char *)return_catch, sizeof (jmp_buf));
  916.  
  917.   free (filename);
  918.  
  919.   return (1);
  920. }
  921.  
  922. reader_loop ()
  923. {
  924.   extern int indirection_level;
  925.   int our_indirection_level;
  926.   COMMAND *current_command = (COMMAND *)NULL;
  927.  
  928.   our_indirection_level = ++indirection_level;
  929.  
  930.   while (!EOF_Reached)
  931.     {
  932.       extern char *trap_list[];
  933.       sighandler sigint_sighandler ();
  934.       int code;
  935.  
  936.       code = setjmp (top_level);
  937.  
  938.       if (interactive_shell)
  939.     {
  940. #if defined (_POSIX_VERSION)
  941.       /* If we are running on a posix-compliant system, then do
  942.          things the Posix way. */
  943.       struct sigaction act;
  944.  
  945.       act.sa_handler = sigint_sighandler;
  946.       act.sa_flags = 0;
  947.       sigemptyset (&act.sa_mask);
  948.       sigaction (SIGINT, &act, (struct sigaction *)NULL);
  949. #else /* !_POSIX_VERSION */
  950.       signal (SIGINT, sigint_sighandler);
  951. #endif /* !_POSIX_VERSION */
  952.     }
  953.  
  954.       if (code != NOT_JUMPED)
  955.     {
  956.       indirection_level = our_indirection_level;
  957.  
  958.       switch (code)
  959.         {
  960.           /* Some kind of throw to top_level has occured. */
  961.         case FORCE_EOF:
  962.         case EXITPROG:
  963.           current_command = (COMMAND *)NULL;
  964.           EOF_Reached = EOF;
  965.           goto exec_done;
  966.  
  967.         case DISCARD:
  968.           /* Obstack free command elements, etc. */
  969.           break;
  970.  
  971.         default:
  972.           programming_error ("Bad jump %d", code);
  973.         }
  974.     }
  975.  
  976.       executing = 0;
  977.       dispose_used_env_vars ();
  978.  
  979. #if (defined (Ultrix) && defined (mips)) || !defined (HAVE_ALLOCA)
  980.       /* Attempt to reclaim memory allocated with alloca (). */
  981.       (void) alloca (0);
  982. #endif
  983.  
  984.       if (read_command () == 0)
  985.     {
  986.       if (global_command)
  987.         {
  988.           current_command = global_command;
  989.  
  990.           current_command_number++;
  991.  
  992.           /* POSIX spec: "-n: The shell reads commands but does
  993.          not execute them; this can be used to check for shell
  994.          script syntax errors.  The shell ignores the -n option
  995.          for interactive shells. " */
  996.           if (interactive_shell || !read_but_dont_execute)
  997.         {
  998.           executing = 1;
  999.           execute_command (current_command);
  1000.         }
  1001.  
  1002.         exec_done:
  1003.           if (current_command)
  1004.         dispose_command (current_command);
  1005.           QUIT;
  1006.         }
  1007.     }
  1008.       else
  1009.     {
  1010.       /* Parse error, maybe discard rest of stream if not interactive. */
  1011.       if (!interactive)
  1012.         EOF_Reached = EOF;
  1013.     }
  1014.       if (just_one_command)
  1015.     EOF_Reached = EOF;
  1016.     }
  1017.   indirection_level--;
  1018. }
  1019.  
  1020. /* Return a string denoting what our indirection level is. */
  1021. static char indirection_string[100];
  1022.  
  1023. char *
  1024. indirection_level_string ()
  1025. {
  1026.   register int i, j;
  1027.   char *get_string_value (), *ps4 = get_string_value ("PS4");
  1028.   extern char *decode_prompt_string ();
  1029.  
  1030.   if (!ps4)
  1031.     ps4 = savestring ("+ ");
  1032.   else
  1033.     ps4 = decode_prompt_string (ps4);
  1034.  
  1035.   for (i = 0; i < indirection_level && i < 99; i++)
  1036.     indirection_string[i] = *ps4;
  1037.  
  1038.   for (j = 1; ps4[j] && i < 99; i++, j++)
  1039.     indirection_string[i] = ps4[j];
  1040.  
  1041.   indirection_string[i] = '\0';
  1042.   free (ps4);
  1043.   return (indirection_string);
  1044. }
  1045.  
  1046. static sighandler 
  1047. alrm_catcher(i)
  1048.      int i;
  1049. {
  1050.   printf ("%ctimed out waiting for input: auto-logout\n", '\07');
  1051.   longjmp (top_level, EXITPROG);
  1052. #if !defined (VOID_SIGHANDLER)
  1053.   return (0);
  1054. #endif /* !VOID_SIGHANDLER */
  1055. }
  1056.  
  1057. parse_command ()
  1058. {
  1059.   extern int need_here_doc, current_command_line_count;
  1060.   extern REDIRECT *redirection_needing_here_doc;
  1061.   int r;
  1062.  
  1063.   need_here_doc = 0;
  1064.   redirection_needing_here_doc = (REDIRECT *)NULL;
  1065.  
  1066.   run_pending_traps ();
  1067.  
  1068.   current_command_line_count = 0;
  1069.   r = yyparse ();
  1070.  
  1071.   if (need_here_doc)
  1072.     make_here_document (redirection_needing_here_doc);
  1073.   need_here_doc = 0;
  1074.  
  1075.   return (r);
  1076. }
  1077.  
  1078. read_command ()
  1079. {
  1080.   extern char *ps1_prompt, **prompt_string_pointer;
  1081.   extern int current_command_line_count;
  1082.   SHELL_VAR *tmout_var = (SHELL_VAR *)NULL;
  1083.   int tmout_len = 0, result;
  1084.   SigHandler *old_alrm = (SigHandler *)NULL;
  1085.  
  1086.   prompt_string_pointer = &ps1_prompt;
  1087.   global_command = (COMMAND *)NULL;
  1088.  
  1089.   /* Only do timeouts if interactive. */
  1090.   if (interactive)
  1091.     {
  1092.       tmout_var = find_variable ("TMOUT");
  1093.  
  1094.       if (tmout_var && tmout_var->value)
  1095.     {
  1096.       tmout_len = atoi (tmout_var->value);
  1097.       if (tmout_len > 0)
  1098.         {
  1099.           old_alrm = signal (SIGALRM, alrm_catcher);
  1100.           alarm (tmout_len);
  1101.         }
  1102.     }
  1103.     }
  1104.  
  1105.   QUIT;
  1106.  
  1107.   current_command_line_count = 0;
  1108.   result = parse_command ();
  1109.  
  1110.   if (interactive && tmout_var && (tmout_len > 0))
  1111.     {
  1112.       alarm(0);
  1113.       signal (SIGALRM, old_alrm);
  1114.     }
  1115.   return (result);
  1116. }
  1117.  
  1118. /* Do whatever is necessary to initialize the shell.
  1119.    Put new initializations in here. */
  1120. shell_initialize ()
  1121. {
  1122.   /* Line buffer output for stderr.
  1123.      If your machine doesn't have either of setlinebuf or setvbuf,
  1124.      you can just comment out the buffering commands, and the shell
  1125.      will still work.  It will take more cycles, though. */
  1126. #if defined (HAVE_SETLINEBUF)
  1127.   setlinebuf (stderr);
  1128.   setlinebuf (stdout);
  1129. #else
  1130. #  if defined (_IOLBF)
  1131. #    if defined (REVERSED_SETVBUF_ARGS)
  1132.   setvbuf (stderr, _IOLBF, (char *)NULL, BUFSIZ);
  1133.   setvbuf (stdout, _IOLBF, (char *)NULL, BUFSIZ);
  1134. #    else
  1135.   setvbuf (stderr, (char *)NULL, _IOLBF, BUFSIZ);
  1136.   setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
  1137. #    endif /* !REVERSED_SETVBUF_ARGS */
  1138. #  endif /* _IOLBF */
  1139. #endif /* HAVE_SETLINEBUF */
  1140.  
  1141.   /* Sort the array of shell builtins so that the binary search in
  1142.      find_shell_builtin () works correctly. */
  1143.   initialize_shell_builtins ();
  1144.  
  1145.   /* Initialize the trap signal handlers before installing our own
  1146.      signal handlers.  traps.c:restore_default_signal () is responsible
  1147.      for restoring the original default signal handler.  That function
  1148.      is called from jobs.c when we make a new child. */
  1149.   initialize_traps ();
  1150.   initialize_signals ();
  1151.  
  1152.   /* Initialize current_user_name and current_host_name. */
  1153.   {
  1154.     struct passwd *entry = getpwuid (getuid ());
  1155.     char hostname[256];
  1156.  
  1157.     if (gethostname (hostname, 255) < 0)
  1158.       current_host_name = "??host??";
  1159.     else
  1160.       current_host_name = savestring (hostname);
  1161.  
  1162.     if (entry)
  1163.       current_user_name = savestring (entry->pw_name);
  1164.     else
  1165.       current_user_name = savestring ("I have no name!");
  1166.     endpwent ();
  1167.   }
  1168.  
  1169.   /* Initialize our interface to the tilde expander. */
  1170.   tilde_initialize ();
  1171.  
  1172.   /* Initialize internal and environment variables. */
  1173.   initialize_shell_variables (shell_environment);
  1174.  
  1175.   /* Initialize filename hash tables. */
  1176.   initialize_filename_hashing ();
  1177.  
  1178.   /* Initialize the data structures for storing and running jobs. */
  1179.   initialize_jobs ();
  1180.  
  1181.   /* Initialize input streams to null. */
  1182.   initialize_bash_input ();
  1183. }
  1184.  
  1185. /* Function called by main () when it appears that the shell has already
  1186.    had some initialization preformed.  This is supposed to reset the world
  1187.    back to a pristine state, as if we had been exec'ed. */
  1188. shell_reinitialize ()
  1189. {
  1190.   extern int line_number, last_command_exit_value;
  1191.  
  1192.   /* The default shell prompts. */
  1193.   primary_prompt = PPROMPT;
  1194.   secondary_prompt = SPROMPT;
  1195.  
  1196.   /* Things that get 1. */
  1197.   current_command_number = 1;
  1198.  
  1199.   /* We have decided that the ~/.bashrc file should not be executed
  1200.      for the invocation of each shell script.  Perhaps some other file
  1201.      should.  */
  1202.   act_like_sh = 1;
  1203.  
  1204.   /* Things that get 0. */
  1205.   login_shell = make_login_shell = interactive = restricted = executing = 0;
  1206.   debugging = no_rc = no_profile = do_version = line_number = 0;
  1207.   last_command_exit_value = remember_on_history = 0;
  1208.   forced_interactive = interactive_shell = 0;
  1209.  
  1210.   /* Ensure that the default startup file is used.  (Except that we don't
  1211.      execute this file for reinitialized shells). */
  1212.   bashrc_file = "~/.bashrc";
  1213.  
  1214.   /* Delete all variables and functions.  They will be reinitialized when
  1215.      the environment is parsed. */
  1216.  
  1217.   delete_all_variables (shell_variables);
  1218.   delete_all_variables (shell_functions);
  1219.  
  1220.   /* Pretend the PATH variable has changed. */
  1221.   sv_path ("PATH");
  1222. }
  1223.  
  1224. initialize_signals ()
  1225. {
  1226.   initialize_terminating_signals ();
  1227.   initialize_job_signals ();
  1228. #if defined (INITIALIZE_SIGLIST)
  1229.   initialize_siglist ();
  1230. #endif
  1231. }
  1232.  
  1233. /* The list of signals that would terminate the shell if not caught.
  1234.    We catch them, but just so that we can write the history file,
  1235.    and so forth. */
  1236. int terminating_signals[] = {
  1237. #ifdef SIGHUP
  1238.   SIGHUP,
  1239. #endif
  1240.  
  1241. #ifdef SIGINT
  1242.   SIGINT,
  1243. #endif
  1244.  
  1245. #ifdef SIGQUIT
  1246.   SIGQUIT,
  1247. #endif
  1248.  
  1249. #ifdef SIGILL
  1250.   SIGILL,
  1251. #endif
  1252.  
  1253. #ifdef SIGTRAP
  1254.   SIGTRAP,
  1255. #endif
  1256.  
  1257. #ifdef SIGIOT
  1258.   SIGIOT,
  1259. #endif
  1260.  
  1261. #ifdef SIGDANGER
  1262.   SIGDANGER,
  1263. #endif
  1264.  
  1265. #ifdef SIGEMT
  1266.   SIGEMT,
  1267. #endif
  1268.  
  1269. #ifdef SIGFPE
  1270.   SIGFPE,
  1271. #endif
  1272.  
  1273. #ifdef SIGKILL
  1274.   SIGKILL,
  1275. #endif
  1276.  
  1277. #ifdef SIGBUS
  1278.   SIGBUS,
  1279. #endif
  1280.  
  1281. #ifdef SIGSEGV
  1282.   SIGSEGV,
  1283. #endif
  1284.  
  1285. #ifdef SIGSYS
  1286.   SIGSYS,
  1287. #endif
  1288.  
  1289. #ifdef SIGPIPE
  1290.   SIGPIPE,
  1291. #endif
  1292.  
  1293. #ifdef SIGALRM
  1294.   SIGALRM,
  1295. #endif
  1296.  
  1297. #ifdef SIGTERM
  1298.   SIGTERM,
  1299. #endif
  1300.  
  1301. #ifdef SIGXCPU
  1302.   SIGXCPU,
  1303. #endif
  1304.  
  1305. #ifdef SIGXFSZ
  1306.   SIGXFSZ,
  1307. #endif
  1308.  
  1309. #ifdef SIGVTALRM
  1310.   SIGVTALRM,
  1311. #endif
  1312.  
  1313. #ifdef SIGPROF
  1314.   SIGPROF,
  1315. #endif
  1316.  
  1317. #ifdef SIGLOST
  1318.   SIGLOST,
  1319. #endif
  1320.  
  1321. #ifdef SIGUSR1
  1322.   SIGUSR1, SIGUSR2
  1323. #endif
  1324.     };
  1325.  
  1326. #define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (int))
  1327.  
  1328. /* This function belongs here? */
  1329. sighandler
  1330. termination_unwind_protect (sig)
  1331.      int sig;
  1332. {
  1333.   if (sig == SIGINT)
  1334.     run_interrupt_trap ();
  1335.  
  1336.   maybe_save_shell_history ();
  1337.  
  1338. #if defined (JOB_CONTROL)
  1339.   if (sig == SIGHUP)
  1340.     {
  1341.       extern void hangup_all_jobs ();
  1342.  
  1343.       hangup_all_jobs ();
  1344.     }
  1345. #endif /* JOB_CONTROL */
  1346.  
  1347.   run_exit_trap ();
  1348.   signal (sig, SIG_DFL);
  1349.   kill (getpid (), sig);
  1350.  
  1351. #if !defined (VOID_SIGHANDLER)
  1352.   return (0);
  1353. #endif /* VOID_SIGHANDLER */
  1354. }
  1355.  
  1356. /* Initialize signals that will terminate the shell to do some
  1357.    unwind protection. */
  1358. initialize_terminating_signals ()
  1359. {
  1360.   register int i;
  1361.  
  1362. #if defined (_POSIX_VERSION)
  1363.   /* If we're running on a Posix-compliant system, do things the Posix way. */
  1364.  
  1365.   struct sigaction act;
  1366.  
  1367.   act.sa_handler = termination_unwind_protect;
  1368.   act.sa_flags = 0;
  1369.   sigemptyset (&act.sa_mask);
  1370.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  1371.     sigaddset (&act.sa_mask, terminating_signals[i]);
  1372.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  1373.     sigaction (terminating_signals[i], &act, (struct sigaction *)NULL);
  1374.  
  1375.   /* For interactive login shells, use an empty signal mask.  Other
  1376.      shells use what they have been given. */
  1377.   sigemptyset (&top_level_mask);
  1378.  
  1379.   if (login_shell)
  1380.     {
  1381.       sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
  1382.     }
  1383.   else
  1384.     {
  1385.       sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &top_level_mask);
  1386.       sigdelset (&top_level_mask, SIGCHLD);
  1387.     }
  1388. #else /* !_POSIX_VERSION */
  1389.  
  1390.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  1391.     signal (terminating_signals[i], termination_unwind_protect);
  1392.  
  1393. #endif /* !_POSIX_VERSION */
  1394.  
  1395.   /* And, some signals that are specifically ignored by the shell. */
  1396.   signal (SIGQUIT, SIG_IGN);
  1397.  
  1398.   if (interactive)
  1399.     signal (SIGTERM, SIG_IGN);
  1400. }
  1401.  
  1402. /* What to do when we've been interrupted, and it is safe to handle it. */
  1403. void
  1404. throw_to_top_level ()
  1405. {
  1406.   extern int last_command_exit_value, loop_level, continuing, breaking;
  1407.   extern int return_catch_flag;
  1408.   extern int parse_and_execute_level;
  1409.   int print_newline = 0;
  1410.  
  1411.   if (interrupt_state)
  1412.     {
  1413.       print_newline = 1;
  1414.       interrupt_state--;
  1415.     }
  1416.  
  1417.   if (interrupt_state)
  1418.     return;
  1419.  
  1420.   /* Run any traps set on SIGINT. */
  1421.   run_interrupt_trap ();
  1422.  
  1423.   /* Cleanup string parser environment. */
  1424.   while (parse_and_execute_level)
  1425.     parse_and_execute_cleanup ();
  1426.  
  1427. #if defined (JOB_CONTROL)
  1428.   give_terminal_to (shell_pgrp);
  1429.   sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
  1430. #endif /* JOB_CONTROL */
  1431.  
  1432.   reset_parser ();
  1433.  
  1434. #if defined (READLINE)
  1435.   if (interactive)
  1436.     bashline_reinitialize ();
  1437. #endif /* READLINE */
  1438.  
  1439.   run_unwind_protects ();
  1440.   loop_level = continuing = breaking = 0;
  1441.   return_catch_flag = 0;
  1442.  
  1443.   if (interactive && print_newline)
  1444.     {
  1445.       fflush (stdout);
  1446.       fprintf (stderr, "\n");
  1447.     }
  1448.  
  1449.   last_command_exit_value |= 128;
  1450.  
  1451.   if (interactive)
  1452.     longjmp (top_level, DISCARD);
  1453.   else
  1454.     longjmp (top_level, EXITPROG);
  1455. }
  1456.  
  1457. /* When non-zero, we throw_to_top_level (). */
  1458. int interrupt_immediately = 0;
  1459.  
  1460. /* What we really do when SIGINT occurs. */
  1461. sighandler
  1462. sigint_sighandler (sig)
  1463.      int sig;
  1464. {
  1465. #if defined (USG) && !defined (_POSIX_VERSION)
  1466.   signal (sig, sigint_sighandler);
  1467. #endif
  1468.  
  1469.   /* interrupt_state needs to be set for the stack of interrupts to work
  1470.      right.  Should it be set unconditionally? */
  1471.   if (!interrupt_state)
  1472.     interrupt_state++;
  1473.   if (interrupt_immediately)
  1474.     {
  1475.       interrupt_immediately = 0;
  1476.       throw_to_top_level ();
  1477.     }
  1478. #if !defined (VOID_SIGHANDLER)
  1479.   return (0);
  1480. #endif /* VOID_SIGHANDLER */
  1481. }
  1482.  
  1483. /* Load the history list from the history file. */
  1484. load_history ()
  1485. {
  1486.   char *hf;
  1487.  
  1488.   /* Truncate history file for interactive shells which desire it.
  1489.      Note that the history file is automatically truncated to the
  1490.      size of HISTSIZE if the user does not explicitly set the size
  1491.      differently. */
  1492.   set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE"));
  1493.   stupidly_hack_special_variables ("HISTFILESIZE");
  1494.  
  1495.   /* Read the history in HISTFILE into the history list. */
  1496.   hf = get_string_value ("HISTFILE");
  1497.  
  1498.   if (hf && *hf)
  1499.     {
  1500.       struct stat buf;
  1501.  
  1502.       if (stat (hf, &buf) == 0)
  1503.     {
  1504.       read_history (hf);
  1505.       using_history ();
  1506.       history_lines_in_file = where_history ();
  1507.     }
  1508.     }
  1509. }
  1510.  
  1511. /* Write the existing history out to the history file. */
  1512. save_history ()
  1513. {
  1514.   char *hf = get_string_value ("HISTFILE");
  1515.  
  1516.   if (hf && *hf)
  1517.     {
  1518.       struct stat buf;
  1519.  
  1520.       if (stat (hf, &buf) == 0)
  1521.     {
  1522.       /* Append only the lines that occurred this session to
  1523.          the history file. */
  1524.       using_history ();
  1525.  
  1526.       if (history_lines_this_session < where_history ())
  1527.         append_history (history_lines_this_session, hf);
  1528.       else
  1529.         write_history (hf);
  1530.     }
  1531.     }
  1532.  
  1533. }
  1534.  
  1535. #if defined (MAKE_BUG_REPORTS)
  1536. /* Make a bug report, even to the extent of mailing it.  Hope that it
  1537.    gets where it is supposed to go.  If not, maybe the user will send
  1538.    it back to me. */
  1539. #include <readline/history.h>
  1540. /* Number of commands to place in the bug report. */
  1541. #define LAST_INTERESTING_HISTORY_COUNT 6
  1542.  
  1543. #if defined (HAVE_VFPRINTF)
  1544. make_bug_report (va_alist)
  1545.      va_dcl
  1546. #else
  1547. make_bug_report (reason, arg1, arg2)
  1548.      char *reason;
  1549. #endif /* HAVE_VFPRINTF */
  1550. {
  1551.   extern char *current_host_name, *current_user_name, *the_current_maintainer;
  1552.   extern int interactive, login_shell;
  1553.   register int len = where_history ();
  1554.   register int i = len - LAST_INTERESTING_HISTORY_COUNT;
  1555.   FILE *stream, *popen ();
  1556.   HIST_ENTRY **list = history_list ();
  1557.  
  1558. #if defined (HAVE_VFPRINTF)
  1559.   char *reason;
  1560.   va_list args;
  1561. #endif /* HAVE_VFPRINTF */
  1562.  
  1563.   stream = popen ("/bin/rmail bash-maintainers@ai.mit.edu", "w");
  1564.  
  1565.   save_history ();
  1566.   if (i < 0) i = 0;
  1567.  
  1568.   if (stream)
  1569.     {
  1570.       fprintf (stream, "To: bash-maintainers@ai.mit.edu\n");
  1571.       fprintf (stream, "Subject: Bash-%s.%d bug-report: ",
  1572.            dist_version, build_version);
  1573.  
  1574. #if defined (HAVE_VFPRINTF)
  1575.       va_start (args);
  1576.       reason = va_arg (args, char *);
  1577.       vfprintf (stream, reason, args);
  1578.       va_end (args);
  1579. #else
  1580.       fprintf (stream, reason, arg1, arg2);
  1581. #endif /* HAVE_VFPRINTF */
  1582.  
  1583.       fprintf (stream, "\n");
  1584.  
  1585.       /* Write the history into the mail file.  Maybe we can recreate
  1586.      the bug? */
  1587.       fprintf (stream,
  1588.            "This is a Bash bug report.  Bash maintainers should be getting this report.\n\
  1589. If this mail has bounced, for right now please send it to:\n\
  1590. \n\
  1591.     %s\n\
  1592. \n\
  1593. since he is the current maintainer of this version of the shell.\n\
  1594. \n\
  1595. This is %s (invoked as `%s'), version %s.%d, on host %s, used by %s.\n\
  1596. This shell is %sinteractive, and it is %sa login shell.\n\
  1597. \n\
  1598. The host is a %s running %s.\n\
  1599. \n\
  1600. The current environment is:\n",
  1601.            the_current_maintainer,
  1602.            get_string_value ("BASH"), full_pathname (dollar_vars[0]),
  1603.            dist_version, build_version, current_host_name,
  1604.            current_user_name, interactive ? "" : "not ",
  1605.            login_shell ? "" : "not ", SYSTEM_NAME, OS_NAME);
  1606.  
  1607.       {
  1608.     SHELL_VAR **vlist, *var;
  1609.     register int i;
  1610.  
  1611.     vlist = all_shell_variables ();
  1612.  
  1613.     for (i = 0; vlist && var = vlist[i]; i++)
  1614.       {
  1615.         if (!invisible_p (var) && exported_p (var))
  1616.           {
  1617.         fprintf (stream, "%s=%s", var->name, value_cell (var));
  1618.         fprintf (stream, "\n");
  1619.           }
  1620.       }
  1621.       }
  1622.  
  1623.       fprintf (stream, "\nAnd here are the last %d commands.\n\n",
  1624.            LAST_INTERESTING_HISTORY_COUNT);
  1625.  
  1626.       for (; i < len; i++)
  1627.     fprintf (stream, "%s\n", list[i]->line);
  1628.  
  1629.       pclose (stream);
  1630.     }
  1631.   else
  1632.     {
  1633.       fprintf (stderr, "Can't mail bug report!\n");
  1634.     }
  1635. }
  1636. #endif /* MAKE_BUG_REPORTS */
  1637.  
  1638. /* Give version information about this shell. */
  1639. show_shell_version ()
  1640. {
  1641.   extern char *base_pathname ();
  1642.   extern char *shell_name;
  1643.   extern int version;
  1644.  
  1645.   printf ("GNU %s, version %s.%d\n", base_pathname (shell_name),
  1646.       dist_version, build_version);
  1647. }
  1648.