home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_110 / mint / bash110s.zoo / bash-1.10 / shell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-03  |  37.4 KB  |  1,562 lines

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