home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / make-3.70-src.lha / src / amiga / make-3.70 / job.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  37.1 KB  |  1,501 lines

  1. /* Job execution and handling for GNU Make.
  2. Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "job.h"
  22. #include "file.h"
  23. #include "variable.h"
  24.  
  25. /* Default path to search for executables.  */
  26. static char default_path[] = ":/bin";
  27.  
  28. /* Default shell to use.  */
  29. char default_shell[] = "/bin/sh";
  30.  
  31. /* If NGROUPS_MAX == 0 then try other methods for finding a real value.  */
  32. #if defined (NGROUPS_MAX) && NGROUPS_MAX == 0
  33. #undef NGROUPS_MAX
  34. #endif /* NGROUPS_MAX == 0 */
  35.  
  36. #ifndef    NGROUPS_MAX
  37. #ifdef    POSIX
  38. #define    GET_NGROUPS_MAX    sysconf (_SC_NGROUPS_MAX)
  39. #else    /* Not POSIX.  */
  40. #define    NGROUPS_MAX    NGROUPS
  41. #endif    /* POSIX.  */
  42. #endif
  43.  
  44. #ifdef    HAVE_SYS_WAIT_H
  45. #include <sys/wait.h>
  46. #endif
  47.  
  48. #ifdef    HAVE_WAITPID
  49. #define    WAIT_NOHANG(status)    waitpid (-1, (int *)(status), WNOHANG)
  50. #else    /* Don't have waitpid.  */
  51. #ifdef    HAVE_WAIT3
  52. #ifndef    wait3
  53. extern int wait3 ();
  54. #endif
  55. #define    WAIT_NOHANG(status)    wait3 ((status), WNOHANG, (struct rusage *) 0)
  56. #endif    /* Have wait3.  */
  57. #endif    /* Have waitpid.  */
  58.  
  59. #if    !defined (wait) && !defined (POSIX)
  60. extern int wait ();
  61. #endif
  62.  
  63. #ifndef    HAVE_UNION_WAIT
  64.  
  65. #define    WAIT_T int
  66.  
  67. #ifndef    WTERMSIG
  68. #define WTERMSIG(x) ((x) & 0x7f)
  69. #endif
  70. #ifndef    WCOREDUMP
  71. #define WCOREDUMP(x) ((x) & 0x80)
  72. #endif
  73. #ifndef    WEXITSTATUS
  74. #define WEXITSTATUS(x) (((x) >> 8) & 0xff)
  75. #endif
  76. #ifndef    WIFSIGNALED
  77. #define WIFSIGNALED(x) (WTERMSIG (x) != 0)
  78. #endif
  79. #ifndef    WIFEXITED
  80. #define WIFEXITED(x) (WTERMSIG (x) == 0)
  81. #endif
  82.  
  83. #else    /* Have `union wait'.  */
  84.  
  85. #define WAIT_T union wait
  86. #ifndef    WTERMSIG
  87. #define WTERMSIG(x)    ((x).w_termsig)
  88. #endif
  89. #ifndef    WCOREDUMP
  90. #define WCOREDUMP(x)    ((x).w_coredump)
  91. #endif
  92. #ifndef WEXITSTATUS
  93. #define WEXITSTATUS(x)    ((x).w_retcode)
  94. #endif
  95. #ifndef    WIFSIGNALED
  96. #define    WIFSIGNALED(x)    (WTERMSIG(x) != 0)
  97. #endif
  98. #ifndef    WIFEXITED
  99. #define    WIFEXITED(x)    (WTERMSIG(x) == 0)
  100. #endif
  101.  
  102. #endif    /* Don't have `union wait'.  */
  103.  
  104.  
  105. #ifndef    HAVE_UNISTD_H
  106. extern int dup2 ();
  107. extern int execve ();
  108. extern void _exit ();
  109. extern int geteuid (), getegid ();
  110. extern int setgid (), getgid ();
  111. #endif
  112.  
  113. #ifndef    getdtablesize
  114. #ifdef HAVE_GETDTABLESIZE
  115. extern int getdtablesize ();
  116. #else
  117. #include <sys/param.h>
  118. #define getdtablesize() NOFILE
  119. #if !defined (NOFILE) && defined (NOFILES_MAX)
  120. /* SCO 3.2 "devsys 4.2" defines NOFILES_{MIN,MAX} in lieu of NOFILE.  */
  121. #define NOFILE    NOFILES_MAX
  122. #endif
  123. #endif
  124. #endif
  125.  
  126. extern int getloadavg ();
  127. extern int start_remote_job_p ();
  128. extern int start_remote_job (), remote_status ();
  129.  
  130. RETSIGTYPE child_handler ();
  131. static void free_child (), start_job_command ();
  132. static int load_too_high (), job_next_command ();
  133.  
  134. /* Chain of all live (or recently deceased) children.  */
  135.  
  136. struct child *children = 0;
  137.  
  138. /* Number of children currently running.  */
  139.  
  140. unsigned int job_slots_used = 0;
  141.  
  142. /* Nonzero if the `good' standard input is in use.  */
  143.  
  144. static int good_stdin_used = 0;
  145.  
  146. /* Chain of children waiting to run until the load average goes down.  */
  147.  
  148. static struct child *waiting_jobs = 0;
  149.  
  150. /* Write an error message describing the exit status given in
  151.    EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
  152.    Append "(ignored)" if IGNORED is nonzero.  */
  153.  
  154. static void
  155. child_error (target_name, exit_code, exit_sig, coredump, ignored)
  156.      char *target_name;
  157.      int exit_code, exit_sig, coredump;
  158.      int ignored;
  159. {
  160.   if (exit_sig == 0)
  161.     error (ignored ? "[%s] Error %d (ignored)" :
  162.        "*** [%s] Error %d",
  163.        target_name, exit_code);
  164.   else
  165.     {
  166.       char *coredump_string = coredump ? " (core dumped)" : "";
  167.       if (exit_sig > 0 && exit_sig < NSIG)
  168.     error ("*** [%s] %s%s",
  169.            target_name, sys_siglist[exit_sig], coredump_string);
  170.       else
  171.     error ("*** [%s] Signal %d%s", target_name, exit_sig, coredump_string);
  172.     }
  173. }
  174.  
  175. static unsigned int dead_children = 0;
  176.  
  177. /* Notice that a child died.
  178.    reap_children should be called when convenient.  */
  179. RETSIGTYPE
  180. child_handler (sig)
  181.      int sig;
  182. {
  183.   ++dead_children;
  184.  
  185.   if (debug_flag)
  186.     printf ("Got a SIGCHLD; %d unreaped children.\n", dead_children);
  187. }
  188.  
  189. extern int shell_function_pid, shell_function_completed;
  190.  
  191. /* Reap dead children, storing the returned status and the new command
  192.    state (`cs_finished') in the `file' member of the `struct child' for the
  193.    dead child, and removing the child from the chain.  If BLOCK nonzero,
  194.    reap at least one child, waiting for it to die if necessary.  If ERR is
  195.    nonzero, print an error message first.  */
  196.  
  197. void
  198. reap_children (block, err)
  199.      int block, err;
  200. {
  201.   WAIT_T status;
  202.  
  203.   while ((children != 0 || shell_function_pid != 0) &&
  204.      (block || dead_children > 0))
  205.     {
  206.       int remote = 0;
  207.       register int pid;
  208.       int exit_code, exit_sig, coredump;
  209.       register struct child *lastc, *c;
  210.       int child_failed;
  211.       int any_remote, any_local;
  212.  
  213.       if (err && dead_children == 0)
  214.     {
  215.       /* We might block for a while, so let the user know why.  */
  216.       fflush (stdout);
  217.       error ("*** Waiting for unfinished jobs....");
  218.     }
  219.  
  220.       /* We have one less dead child to reap.
  221.      The test and decrement are not atomic; if it is compiled into:
  222.          register = dead_children - 1;
  223.         dead_children = register;
  224.      a SIGCHLD could come between the two instructions.
  225.      child_handler increments dead_children.
  226.      The second instruction here would lose that increment.  But the
  227.      only effect of dead_children being wrong is that we might wait
  228.      longer than necessary to reap a child, and lose some parallelism;
  229.      and we might print the "Waiting for unfinished jobs" message above
  230.      when not necessary.  */
  231.  
  232.       if (dead_children != 0)
  233.     --dead_children;
  234.  
  235.       any_remote = 0;
  236.       any_local = shell_function_pid != -1;
  237.       for (c = children; c != 0; c = c->next)
  238.     {
  239.       any_remote |= c->remote;
  240.       any_local |= ! c->remote;
  241.       if (debug_flag)
  242.         printf ("Live child 0x%08lx PID %d%s\n",
  243.             (unsigned long int) c,
  244.             c->pid, c->remote ? " (remote)" : "");
  245.     }
  246.  
  247.       /* First, check for remote children.  */
  248.       if (any_remote)
  249.     pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
  250.       else
  251.     pid = 0;
  252.       if (pid < 0)
  253.     {
  254.     remote_status_lose:
  255. #ifdef    EINTR
  256.       if (errno == EINTR)
  257.         continue;
  258. #endif
  259.       pfatal_with_name ("remote_status");
  260.     }
  261.       else if (pid == 0)
  262.     {
  263.       /* No remote children.  Check for local children.  */
  264.  
  265.       if (any_local)
  266.         {
  267. #ifdef    WAIT_NOHANG
  268.           if (!block)
  269.         pid = WAIT_NOHANG (&status);
  270.           else
  271. #endif
  272.         pid = wait ((int *)&status);
  273.         }
  274.       else
  275.         pid = 0;
  276.  
  277.       if (pid < 0)
  278.         {
  279. #ifdef    EINTR
  280.           if (errno == EINTR)
  281.         continue;
  282. #endif
  283.           pfatal_with_name ("wait");
  284.         }
  285.       else if (pid == 0)
  286.         {
  287.           /* No local children.  */
  288.           if (block && any_remote)
  289.         {
  290.           /* Now try a blocking wait for a remote child.  */
  291.           pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
  292.           if (pid < 0)
  293.             goto remote_status_lose;
  294.           else if (pid == 0)
  295.             /* No remote children either.  Finally give up.  */
  296.             break;
  297.           else
  298.             /* We got a remote child.  */
  299.             remote = 1;
  300.         }
  301.           else
  302.         break;
  303.         }
  304.       else
  305.         {
  306.           /* Chop the status word up.  */
  307.           exit_code = WEXITSTATUS (status);
  308.           exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
  309.           coredump = WCOREDUMP (status);
  310.         }
  311.     }
  312.       else
  313.     /* We got a remote child.  */
  314.     remote = 1;
  315.  
  316.       /* Check if this is the child of the `shell' function.  */
  317.       if (!remote && pid == shell_function_pid)
  318.     {
  319.       /* It is.  Leave an indicator for the `shell' function.  */
  320.       if (exit_sig == 0 && exit_code == 127)
  321.         shell_function_completed = -1;
  322.       else
  323.         shell_function_completed = 1;
  324.       break;
  325.     }
  326.  
  327.       child_failed = exit_sig != 0 || exit_code != 0;
  328.  
  329.       /* Search for a child matching the deceased one.  */
  330.       lastc = 0;
  331.       for (c = children; c != 0; lastc = c, c = c->next)
  332.     if (c->remote == remote && c->pid == pid)
  333.       break;
  334.  
  335.       if (c == 0)
  336.     {
  337.       /* An unknown child died.  */
  338.       char buf[100];
  339.       sprintf (buf, "Unknown%s job %d", remote ? " remote" : "", pid);
  340.       if (child_failed)
  341.         child_error (buf, exit_code, exit_sig, coredump,
  342.              ignore_errors_flag);
  343.       else
  344.         error ("%s finished.", buf);
  345.     }
  346.       else
  347.     {
  348.       if (debug_flag)
  349.         printf ("Reaping %s child 0x%08lx PID %d%s\n",
  350.             child_failed ? "losing" : "winning",
  351.             (unsigned long int) c,
  352.             c->pid, c->remote ? " (remote)" : "");
  353.  
  354.       /* If this child had the good stdin, say it is now free.  */
  355.       if (c->good_stdin)
  356.         good_stdin_used = 0;
  357.  
  358.       if (child_failed && !c->noerror && !ignore_errors_flag)
  359.         {
  360.           /* The commands failed.  Write an error message,
  361.          delete non-precious targets, and abort.  */
  362.           child_error (c->file->name, exit_code, exit_sig, coredump, 0);
  363.           c->file->update_status = 1;
  364.           if (exit_sig != 0)
  365.         delete_child_targets (c);
  366.         }
  367.       else
  368.         {
  369.           if (child_failed)
  370.         {
  371.           /* The commands failed, but we don't care.  */
  372.           child_error (c->file->name,
  373.                    exit_code, exit_sig, coredump, 1);
  374.           child_failed = 0;
  375.         }
  376.  
  377.           /* If there are more commands to run, try to start them.  */
  378.           if (job_next_command (c))
  379.         {
  380.           if (handling_fatal_signal)
  381.             {
  382.               /* Never start new commands while we are dying.
  383.              Since there are more commands that wanted to be run,
  384.              the target was not completely remade.  So we treat
  385.              this as if a command had failed.  */
  386.               c->file->command_state = cs_finished;
  387.               c->file->update_status = 1;
  388.             }
  389.           else
  390.             {
  391.               /* Check again whether to start remotely.
  392.              Whether or not we want to changes over time.
  393.              Also, start_remote_job may need state set up
  394.              by start_remote_job_p.  */
  395.               c->remote = start_remote_job_p ();
  396.               start_job_command (c);
  397.             }
  398.         }
  399.  
  400.           switch (c->file->command_state)
  401.         {
  402.         case cs_running:
  403.           /* Successfully started.  Loop to reap more children.  */
  404.           continue;
  405.  
  406.         case cs_finished:
  407.           if (c->file->update_status != 0)
  408.             /* We failed to start the commands.  */
  409.             delete_child_targets (c);
  410.           break;
  411.  
  412.         default:
  413.           error ("internal error: `%s' has bogus command_state \
  414. %d in reap_children",
  415.              c->file->name, (int) c->file->command_state);
  416.           abort ();
  417.           break;
  418.         }
  419.         }
  420.  
  421.       if (! handling_fatal_signal)
  422.         /* Notice if the target of the commands has been changed.  */
  423.         notice_finished_file (c->file);
  424.  
  425.       if (debug_flag)
  426.         printf ("Removing child 0x%08lx PID %d%s from chain.\n",
  427.             (unsigned long int) c,
  428.             c->pid, c->remote ? " (remote)" : "");
  429.  
  430.       /* Remove the child from the chain and free it.  */
  431.       if (lastc == 0)
  432.         children = c->next;
  433.       else
  434.         lastc->next = c->next;
  435.       if (! handling_fatal_signal) /* Avoid nonreentrancy.  */
  436.         free_child (c);
  437.  
  438.       /* There is now another slot open.  */
  439.       --job_slots_used;
  440.  
  441.       /* If the job failed, and the -k flag was not given, die,
  442.          unless we are already in the process of dying.  */
  443.       if (!err && child_failed && !keep_going_flag)
  444.         die (1);
  445.     }
  446.  
  447.       /* Only block for one child.  */
  448.       block = 0;
  449.     }
  450. }
  451.  
  452. /* Free the storage allocated for CHILD.  */
  453.  
  454. static void
  455. free_child (child)
  456.      register struct child *child;
  457. {
  458.   if (child->command_lines != 0)
  459.     {
  460.       register unsigned int i;
  461.       for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
  462.     free (child->command_lines[i]);
  463.       free ((char *) child->command_lines);
  464.     }
  465.  
  466.   if (child->environment != 0)
  467.     {
  468.       register char **ep = child->environment;
  469.       while (*ep != 0)
  470.     free (*ep++);
  471.       free ((char *) child->environment);
  472.     }
  473.  
  474.   free ((char *) child);
  475. }
  476.  
  477. #ifdef    POSIX
  478. extern sigset_t fatal_signal_set;
  479.  
  480. void
  481. unblock_sigs ()
  482. {
  483.   sigset_t empty;
  484.   sigemptyset (&empty);
  485.   sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
  486. }
  487. #endif
  488.  
  489. /* Start a job to run the commands specified in CHILD.
  490.    CHILD is updated to reflect the commands and ID of the child process.  */
  491.  
  492. static void
  493. start_job_command (child)
  494.      register struct child *child;
  495. {
  496.   static int bad_stdin = -1;
  497.   register char *p;
  498.   int flags = child->file->cmds->lines_flags[child->command_line - 1];
  499.   char **argv;
  500.  
  501.   p = child->command_ptr;
  502.   child->noerror = flags & COMMANDS_NOERROR;
  503.   while (*p != '\0')
  504.     {
  505.       if (*p == '@')
  506.     flags |= COMMANDS_SILENT;
  507.       else if (*p == '+')
  508.     flags |= COMMANDS_RECURSE;
  509.       else if (*p == '-')
  510.     child->noerror = 1;
  511.       else if (!isblank (*p) && *p != '+')
  512.     break;
  513.       ++p;
  514.     }
  515.  
  516.   /* If -q was given, just say that updating `failed'.  */
  517.   if (question_flag && !(flags & COMMANDS_RECURSE))
  518.     goto error;
  519.  
  520.   /* There may be some preceding whitespace left if there
  521.      was nothing but a backslash on the first line.  */
  522.   p = next_token (p);
  523.   
  524.   /* Figure out an argument list from this command line.  */
  525.   
  526.   {
  527.     char *end;
  528.     argv = construct_command_argv (p, &end, child->file);
  529.     if (end == NULL)
  530.       child->command_ptr = NULL;
  531.     else
  532.       {
  533.     *end++ = '\0';
  534.     child->command_ptr = end;
  535.       }
  536.   }
  537.  
  538.   if (touch_flag && !(flags & COMMANDS_RECURSE))
  539.     {
  540.       /* Go on to the next command.  It might be the recursive one.
  541.      We construct ARGV only to find the end of the command line.  */
  542.       free (argv[0]);
  543.       free ((char *) argv);
  544.       argv = 0;
  545.     }
  546.  
  547.   if (argv == 0)
  548.     {
  549.       /* This line has no commands.  Go to the next.  */
  550.       if (job_next_command (child))
  551.     start_job_command (child);
  552.       return;
  553.     }
  554.  
  555.   /* Print out the command.  */
  556.  
  557.   if (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
  558.     puts (p);
  559.  
  560.   /* Tell update_goal_chain that a command has been started on behalf of
  561.      this target.  It is important that this happens here and not in
  562.      reap_children (where we used to do it), because reap_children might be
  563.      reaping children from a different target.  We want this increment to
  564.      guaranteedly indicate that a command was started for the dependency
  565.      chain (i.e., update_file recursion chain) we are processing.  */
  566.  
  567.   ++commands_started;
  568.  
  569.   /* If -n was given, recurse to get the next line in the sequence.  */
  570.  
  571.   if (just_print_flag && !(flags & COMMANDS_RECURSE))
  572.     {
  573.       free (argv[0]);
  574.       free ((char *) argv);
  575.       if (job_next_command (child))
  576.     start_job_command (child);
  577.       return;
  578.     }
  579.  
  580.   /* Flush the output streams so they won't have things written twice.  */
  581.  
  582.   fflush (stdout);
  583.   fflush (stderr);
  584.   
  585.   /* Set up a bad standard input that reads from a broken pipe.  */
  586.  
  587.   if (bad_stdin == -1)
  588.     {
  589.       /* Make a file descriptor that is the read end of a broken pipe.
  590.      This will be used for some children's standard inputs.  */
  591.       int pd[2];
  592.       if (pipe (pd) == 0)
  593.     {
  594.       /* Close the write side.  */
  595.       (void) close (pd[1]);
  596.       /* Save the read side.  */
  597.       bad_stdin = pd[0];
  598.     }
  599.     }
  600.  
  601.   /* Decide whether to give this child the `good' standard input
  602.      (one that points to the terminal or whatever), or the `bad' one
  603.      that points to the read side of a broken pipe.  */
  604.  
  605.   child->good_stdin = !good_stdin_used;
  606.   if (child->good_stdin)
  607.     good_stdin_used = 1;
  608.  
  609.   child->deleted = 0;
  610.  
  611.   /* Set up the environment for the child.  */
  612.   if (child->environment == 0)
  613.     child->environment = target_environment (child->file);
  614.  
  615.   /* start_waiting_job has set CHILD->remote if we can start a remote job.  */
  616.   if (child->remote)
  617.     {
  618.       int is_remote, id, used_stdin;
  619.       if (start_remote_job (argv, child->environment,
  620.                 child->good_stdin ? 0 : bad_stdin,
  621.                 &is_remote, &id, &used_stdin))
  622.     goto error;
  623.       else
  624.     {
  625.       if (child->good_stdin && !used_stdin)
  626.         {
  627.           child->good_stdin = 0;
  628.           good_stdin_used = 0;
  629.         }
  630.       child->remote = is_remote;
  631.       child->pid = id;
  632.     }
  633.     }
  634.   else
  635.     {
  636.       /* Fork the child process.  */
  637.  
  638. #ifdef     POSIX
  639.       (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
  640. #else
  641. #ifdef    HAVE_SIGSETMASK
  642.       (void) sigblock (fatal_signal_mask);
  643. #endif
  644. #endif
  645.  
  646.       child->remote = 0;
  647.       child->pid = vfork ();
  648.       if (child->pid == 0)
  649.     {
  650.       /* We are the child side.  */
  651.       unblock_sigs ();
  652.       child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
  653.                  argv, child->environment);
  654.     }
  655.       else if (child->pid < 0)
  656.     {
  657.       /* Fork failed!  */
  658.       unblock_sigs ();
  659.       perror_with_name ("vfork", "");
  660.       goto error;
  661.     }
  662.     }
  663.  
  664.   /* We are the parent side.  Set the state to
  665.      say the commands are running and return.  */
  666.  
  667.   child->file->command_state = cs_running;
  668.  
  669.   /* Free the storage used by the child's argument list.  */
  670.  
  671.   free (argv[0]);
  672.   free ((char *) argv);
  673.  
  674.   return;
  675.  
  676.  error:;
  677.   child->file->update_status = 1;
  678.   child->file->command_state = cs_finished;
  679. }
  680.  
  681. /* Try to start a child running.
  682.    Returns nonzero if the child was started (and maybe finished), or zero if
  683.    the load was too high and the child was put on the `waiting_jobs' chain.  */
  684.  
  685. static int
  686. start_waiting_job (c)
  687.      struct child *c;
  688. {
  689.   /* If we can start a job remotely, we always want to, and don't care about
  690.      the local load average.  We record that the job should be started
  691.      remotely in C->remote for start_job_command to test.  */
  692.  
  693.   c->remote = start_remote_job_p ();
  694.  
  695.   /* If this job is to be started locally, and we are already running
  696.      some jobs, make this one wait if the load average is too high.  */
  697.   if (!c->remote && job_slots_used > 0 && load_too_high ())
  698.     {
  699.       /* Put this child on the chain of children waiting
  700.      for the load average to go down.  */
  701.       c->file->command_state = cs_running;
  702.       c->next = waiting_jobs;
  703.       waiting_jobs = c;
  704.       return 0;
  705.     }
  706.  
  707.   /* Start the first command; reap_children will run later command lines.  */
  708.   start_job_command (c);
  709.  
  710.   switch (c->file->command_state)
  711.     {
  712.     case cs_running:
  713.       c->next = children;
  714.       if (debug_flag)
  715.     printf ("Putting child 0x%08lx PID %05d%s on the chain.\n",
  716.         (unsigned long int) c,
  717.         c->pid, c->remote ? " (remote)" : "");
  718.       children = c;
  719.       /* One more job slot is in use.  */
  720.       ++job_slots_used;
  721.       unblock_sigs ();
  722.       break;
  723.  
  724.     case cs_finished:
  725.       notice_finished_file (c->file);
  726.       free_child (c);
  727.       break;
  728.  
  729.     default:
  730.       error ("internal error: `%s' command_state == %d in new_job",
  731.          c->file->name, (int) c->file->command_state);
  732.       abort ();
  733.       break;
  734.     }
  735.  
  736.   return 1;
  737. }
  738.  
  739. /* Create a `struct child' for FILE and start its commands running.  */
  740.  
  741. void
  742. new_job (file)
  743.      register struct file *file;
  744. {
  745.   register struct commands *cmds = file->cmds;
  746.   register struct child *c;
  747.   char **lines;
  748.   register unsigned int i;
  749.  
  750.   /* Let any previously decided-upon jobs that are waiting
  751.      for the load to go down start before this new one.  */
  752.   start_waiting_jobs ();
  753.  
  754.   /* Reap any children that might have finished recently.  */
  755.   reap_children (0, 0);
  756.  
  757.   /* Chop the commands up into lines if they aren't already.  */
  758.   chop_commands (cmds);
  759.  
  760.   if (job_slots != 0)
  761.     /* Wait for a job slot to be freed up.  */
  762.     while (job_slots_used == job_slots)
  763.       reap_children (1, 0);
  764.  
  765.   /* Expand the command lines and store the results in LINES.  */
  766.   lines = (char **) xmalloc (cmds->ncommand_lines * sizeof (char *));
  767.   for (i = 0; i < cmds->ncommand_lines; ++i)
  768.     {
  769.       /* Collapse backslash-newline combinations that are inside variable
  770.      or function references.  These are left alone by the parser so
  771.      that they will appear in the echoing of commands (where they look
  772.      nice); and collapsed by construct_command_argv when it tokenizes.
  773.      But letting them survive inside function invocations loses because
  774.      we don't want the functions to see them as part of the text.  */
  775.  
  776.       char *in, *out, *ref;
  777.  
  778.       /* IN points to where in the line we are scanning.
  779.      OUT points to where in the line we are writing.
  780.      When we collapse a backslash-newline combination,
  781.      IN gets ahead out OUT.  */
  782.  
  783.       in = out = cmds->command_lines[i];
  784.       while ((ref = index (in, '$')) != 0)
  785.     {
  786.       ++ref;        /* Move past the $.  */
  787.  
  788.       if (out != in)
  789.         /* Copy the text between the end of the last chunk
  790.            we processed (where IN points) and the new chunk
  791.            we are about to process (where REF points).  */
  792.         bcopy (in, out, ref - in);
  793.  
  794.       /* Move both pointers past the boring stuff.  */
  795.       out += ref - in;
  796.       in = ref;
  797.  
  798.       if (*ref == '(' || *ref == '{')
  799.         {
  800.           char openparen = *ref;
  801.           char closeparen = openparen == '(' ? ')' : '}';
  802.           int count;
  803.           char *p;
  804.  
  805.           *out++ = *in++;    /* Copy OPENPAREN.  */
  806.           /* IN now points past the opening paren or brace.
  807.          Count parens or braces until it is matched.  */
  808.           count = 0;
  809.           while (*in != '\0')
  810.         {
  811.           if (*in == closeparen && --count < 0)
  812.             break;
  813.           else if (*in == '\\' && in[1] == '\n')
  814.             {
  815.               /* We have found a backslash-newline inside a
  816.              variable or function reference.  Eat it and
  817.              any following whitespace.  */
  818.  
  819.               int quoted = 0;
  820.               for (p = in - 1; p > ref && *p == '\\'; --p)
  821.             quoted = !quoted;
  822.  
  823.               if (quoted)
  824.             /* There were two or more backslashes, so this is
  825.                not really a continuation line.  We don't collapse
  826.                the quoting backslashes here as is done in
  827.                collapse_continuations, because the line will
  828.                be collapsed again after expansion.  */
  829.             *out++ = *in++;
  830.               else
  831.             {
  832.               /* Skip the backslash, newline and
  833.                  any following whitespace.  */
  834.               in = next_token (in + 2);
  835.  
  836.               /* Discard any preceding whitespace that has
  837.                  already been written to the output.  */
  838.               while (out > ref && isblank (out[-1]))
  839.                 --out;
  840.  
  841.               /* Replace it all with a single space.  */
  842.               *out++ = ' ';
  843.             }
  844.             }
  845.           else
  846.             {
  847.               if (*in == openparen)
  848.             ++count;
  849.  
  850.               *out++ = *in++;
  851.             }
  852.         }
  853.         }
  854.     }
  855.  
  856.       /* There are no more references in this line to worry about.
  857.      Copy the remaining uninteresting text to the output.  */
  858.       if (out != in)
  859.     strcpy (out, in);
  860.  
  861.       /* Finally, expand the line.  */
  862.       lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
  863.                              file);
  864.     }
  865.  
  866.   /* Start the command sequence, record it in a new
  867.      `struct child', and add that to the chain.  */
  868.  
  869.   c = (struct child *) xmalloc (sizeof (struct child));
  870.   c->file = file;
  871.   c->command_lines = lines;
  872.   c->command_line = 0;
  873.   c->command_ptr = 0;
  874.   c->environment = 0;
  875.  
  876.   /* Fetch the first command line to be run.  */
  877.   if (! job_next_command (c))
  878.     /* There were no commands!  */
  879.     free_child (c);
  880.   else
  881.     {
  882.       /* The job is now primed.  Start it running.  */
  883.       start_waiting_job (c);
  884.  
  885.       if (job_slots == 1)
  886.     /* Since there is only one job slot, make things run linearly.
  887.        Wait for the child to die, setting the state to `cs_finished'.  */
  888.     while (file->command_state == cs_running)
  889.       reap_children (1, 0);
  890.     }
  891. }
  892.  
  893. /* Move CHILD's pointers to the next command for it to execute.
  894.    Returns nonzero if there is another command.  */
  895.  
  896. static int
  897. job_next_command (child)
  898.      struct child *child;
  899. {
  900.   if (child->command_ptr == 0 || *child->command_ptr == '\0')
  901.     {
  902.       /* There are no more lines in the expansion of this line.  */
  903.       if (child->command_line == child->file->cmds->ncommand_lines)
  904.     {
  905.       /* There are no more lines to be expanded.  */
  906.       child->command_ptr = 0;
  907.       child->file->command_state = cs_finished;
  908.       child->file->update_status = 0;
  909.       return 0;
  910.     }
  911.       else
  912.     /* Get the next line to run.  */
  913.     child->command_ptr = child->command_lines[child->command_line++];
  914.     }
  915.   return 1;
  916. }
  917.  
  918. static int
  919. load_too_high ()
  920. {
  921.   extern int getloadavg ();
  922.   double load;
  923.  
  924.   if (max_load_average < 0)
  925.     return 0;
  926.  
  927.   make_access ();
  928.   if (getloadavg (&load, 1) != 1)
  929.     {
  930.       static int lossage = -1;
  931.       /* Complain only once for the same error.  */
  932.       if (lossage == -1 || errno != lossage)
  933.     {
  934.       if (errno == 0)
  935.         /* An errno value of zero means getloadavg is just unsupported.  */
  936.         error ("cannot enforce load limits on this operating system");
  937.       else
  938.         perror_with_name ("cannot enforce load limit: ", "getloadavg");
  939.     }
  940.       lossage = errno;
  941.       load = 0;
  942.     }
  943.   user_access ();
  944.  
  945.   return load >= max_load_average;
  946. }
  947.  
  948. /* Start jobs that are waiting for the load to be lower.  */
  949.  
  950. void
  951. start_waiting_jobs ()
  952. {
  953.   struct child *job;
  954.  
  955.   if (waiting_jobs == 0)
  956.     return;
  957.  
  958.   do
  959.     {
  960.       /* Check for recently deceased descendants.  */
  961.       reap_children (0, 0);
  962.  
  963.       /* Take a job off the waiting list.  */
  964.       job = waiting_jobs;
  965.       waiting_jobs = job->next;
  966.  
  967.       /* Try to start that job.  We break out of the loop as soon
  968.      as start_waiting_job puts one back on the waiting list.  */
  969.     } while (start_waiting_job (job) && waiting_jobs != 0);
  970. }
  971.  
  972. /* Replace the current process with one executing the command in ARGV.
  973.    STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
  974.    the environment of the new program.  This function does not return.  */
  975.  
  976. void
  977. child_execute_job (stdin_fd, stdout_fd, argv, envp)
  978.      int stdin_fd, stdout_fd;
  979.      char **argv, **envp;
  980. {
  981.   if (stdin_fd != 0)
  982.     (void) dup2 (stdin_fd, 0);
  983.   if (stdout_fd != 1)
  984.     (void) dup2 (stdout_fd, 1);
  985.  
  986.   /* Free up file descriptors.  */
  987.   {
  988.     register int d;
  989.     int max = getdtablesize ();
  990.     for (d = 3; d < max; ++d)
  991.       (void) close (d);
  992.   }
  993.  
  994.   /* Run the command.  */
  995.   exec_command (argv, envp);
  996. }
  997.  
  998. /* Search PATH for FILE.
  999.    If successful, store the full pathname in PROGRAM and return 1.
  1000.    If not sucessful, return zero.  */
  1001.  
  1002. static int
  1003. search_path (file, path, program)
  1004.      char *file, *path, *program;
  1005. {
  1006.   if (path == 0 || path[0] == '\0')
  1007.     path = default_path;
  1008.  
  1009.   if (index (file, '/') != 0)
  1010.     {
  1011.       strcpy (program, file);
  1012.       return 1;
  1013.     }
  1014.   else
  1015.     {
  1016.       unsigned int len;
  1017.  
  1018. #ifdef    HAVE_GETGROUPS
  1019. #ifndef    HAVE_UNISTD_H
  1020.       extern int getgroups ();
  1021. #endif
  1022.       static int ngroups = -1;
  1023. #ifdef    NGROUPS_MAX
  1024.       static GETGROUPS_T groups[NGROUPS_MAX];
  1025. #define    ngroups_max    NGROUPS_MAX
  1026. #else
  1027.       static GETGROUPS_T *groups = 0;
  1028.       static int ngroups_max;
  1029.       if (groups == 0)
  1030.     {
  1031.       ngroups_max = GET_NGROUPS_MAX;
  1032.       groups = (GETGROUPS_T *) malloc (ngroups_max * sizeof (GETGROUPS_T));
  1033.     }
  1034. #endif
  1035.       if (groups != 0 && ngroups == -1)
  1036.     ngroups = getgroups (ngroups_max, groups);
  1037. #endif    /* Have getgroups.  */
  1038.  
  1039.       len = strlen (file) + 1;
  1040.       do
  1041.     {
  1042.       struct stat st;
  1043.       int perm;
  1044.       char *p;
  1045.  
  1046.       p = index (path, ':');
  1047.       if (p == 0)
  1048.         p = path + strlen (path);
  1049.  
  1050.       if (p == path)
  1051.         bcopy (file, program, len);
  1052.       else
  1053.         {
  1054.           bcopy (path, program, p - path);
  1055.           program[p - path] = '/';
  1056.           bcopy (file, program + (p - path) + 1, len);
  1057.         }
  1058.  
  1059.       if (stat (program, &st) == 0
  1060.           && S_ISREG (st.st_mode))
  1061.         {
  1062.           if (st.st_uid == geteuid ())
  1063.         perm = (st.st_mode & 0100);
  1064.           else if (st.st_gid == getegid ())
  1065.         perm = (st.st_mode & 0010);
  1066.           else
  1067.         {
  1068. #ifdef    HAVE_GETGROUPS
  1069.           register int i;
  1070.           for (i = 0; i < ngroups; ++i)
  1071.             if (groups[i] == st.st_gid)
  1072.               break;
  1073.           if (i < ngroups)
  1074.             perm = (st.st_mode & 0010);
  1075.           else
  1076. #endif    /* Have getgroups.  */
  1077.             perm = (st.st_mode & 0001);
  1078.         }
  1079.  
  1080.           if (perm != 0)
  1081.         return 1;
  1082.         }
  1083.  
  1084.       path = p + 1;
  1085.     } while (*path != '\0');
  1086.     }
  1087.  
  1088.   return 0;
  1089. }
  1090.  
  1091. /* Replace the current process with one running the command in ARGV,
  1092.    with environment ENVP.  This function does not return.  */
  1093.  
  1094. void
  1095. exec_command (argv, envp)
  1096.      char **argv, **envp;
  1097. {
  1098.   char *shell, *path;
  1099.   PATH_VAR (program);
  1100.   register char **ep;
  1101.  
  1102.   shell = path = 0;
  1103.   for (ep = envp; *ep != 0; ++ep)
  1104.     {
  1105.       if (shell == 0 && !strncmp(*ep, "SHELL=", 6))
  1106.     shell = &(*ep)[6];
  1107.       else if (path == 0 && !strncmp(*ep, "PATH=", 5))
  1108.     path = &(*ep)[5];
  1109.       else if (path != 0 && shell != 0)
  1110.     break;
  1111.     }
  1112.  
  1113.   /* Be the user, permanently.  */
  1114.   child_access ();
  1115.  
  1116.   if (!search_path (argv[0], path, program))
  1117.     error ("%s: Command not found", argv[0]);
  1118.   else
  1119.     {
  1120.       /* Run the program.  */
  1121.       execve (program, argv, envp);
  1122.  
  1123.       if (errno == ENOEXEC)
  1124.     {
  1125.       PATH_VAR (shell_program);
  1126.       char *shell_path;
  1127.       if (shell == 0)
  1128.         shell_path = default_shell;
  1129.       else
  1130.         {
  1131.           if (search_path (shell, path, shell_program))
  1132.         shell_path = shell_program;
  1133.           else
  1134.         {
  1135.           shell_path = 0;
  1136.           error ("%s: Shell program not found", shell);
  1137.         }
  1138.         }
  1139.  
  1140.       if (shell_path != 0)
  1141.         {
  1142.           char **new_argv;
  1143.           int argc;
  1144.  
  1145.           argc = 1;
  1146.           while (argv[argc] != 0)
  1147.         ++argc;
  1148.  
  1149.           new_argv = (char **) alloca ((1 + argc + 1) * sizeof (char *));
  1150.           new_argv[0] = shell_path;
  1151.           new_argv[1] = program;
  1152.           while (argc > 0)
  1153.         {
  1154.           new_argv[1 + argc] = argv[argc];
  1155.           --argc;
  1156.         }
  1157.  
  1158.           execve (shell_path, new_argv, envp);
  1159.           perror_with_name ("execve: ", shell_path);
  1160.         }
  1161.     }
  1162.       else
  1163.     perror_with_name ("execve: ", program);
  1164.     }
  1165.  
  1166.   _exit (127);
  1167. }
  1168.  
  1169. /* Figure out the argument list necessary to run LINE as a command.
  1170.    Try to avoid using a shell.  This routine handles only ' quoting.
  1171.    Starting quotes may be escaped with a backslash.  If any of the
  1172.    characters in sh_chars[] is seen, or any of the builtin commands
  1173.    listed in sh_cmds[] is the first word of a line, the shell is used.
  1174.  
  1175.    If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
  1176.    If *RESTP is NULL, newlines will be ignored.
  1177.  
  1178.    SHELL is the shell to use, or nil to use the default shell.
  1179.    IFS is the value of $IFS, or nil (meaning the default).  */
  1180.  
  1181. static char **
  1182. construct_command_argv_internal (line, restp, shell, ifs)
  1183.      char *line, **restp;
  1184.      char *shell, *ifs;
  1185. {
  1186.   static char sh_chars[] = "#;\"*?[]&|<>(){}$`^";
  1187.   static char *sh_cmds[] = { "cd", "eval", "exec", "exit", "login",
  1188.                  "logout", "set", "umask", "wait", "while", "for",
  1189.                  "case", "if", ":", ".", "break", "continue",
  1190.                  "export", "read", "readonly", "shift", "times",
  1191.                  "trap", "switch", 0 };
  1192.   register int i;
  1193.   register char *p;
  1194.   register char *ap;
  1195.   char *end;
  1196.   int instring, word_has_equals, seen_nonequals;
  1197.   char **new_argv = 0;
  1198.  
  1199.   if (restp != NULL)
  1200.     *restp = NULL;
  1201.  
  1202.   /* Make sure not to bother processing an empty line.  */
  1203.   while (isblank (*line))
  1204.     ++line;
  1205.   if (*line == '\0')
  1206.     return 0;
  1207.  
  1208.   /* See if it is safe to parse commands internally.  */
  1209.   if (shell == 0)
  1210.     shell = default_shell;
  1211.   else if (strcmp (shell, default_shell))
  1212.     goto slow;
  1213.  
  1214.   if (ifs != 0)
  1215.     for (ap = ifs; *ap != '\0'; ++ap)
  1216.       if (*ap != ' ' && *ap != '\t' && *ap != '\n')
  1217.     goto slow;
  1218.  
  1219.   i = strlen (line) + 1;
  1220.  
  1221.   /* More than 1 arg per character is impossible.  */
  1222.   new_argv = (char **) xmalloc (i * sizeof (char *));
  1223.  
  1224.   /* All the args can fit in a buffer as big as LINE is.   */
  1225.   ap = new_argv[0] = (char *) xmalloc (i);
  1226.   end = ap + i;
  1227.  
  1228.   /* I is how many complete arguments have been found.  */
  1229.   i = 0;
  1230.   instring = word_has_equals = seen_nonequals = 0;
  1231.   for (p = line; *p != '\0'; ++p)
  1232.     {
  1233.       if (ap > end)
  1234.     abort ();
  1235.  
  1236.       if (instring)
  1237.     {
  1238.       /* Inside a string, just copy any char except a closing quote.  */
  1239.       if (*p == '\'')
  1240.         instring = 0;
  1241.       else
  1242.         *ap++ = *p;
  1243.     }
  1244.       else if (index (sh_chars, *p) != 0)
  1245.     /* Not inside a string, but it's a special char.  */
  1246.     goto slow;
  1247.       else
  1248.     /* Not a special char.  */
  1249.     switch (*p)
  1250.       {
  1251.       case '=':
  1252.         /* Equals is a special character in leading words before the
  1253.            first word with no equals sign in it.  This is not the case
  1254.            with sh -k, but we never get here when using nonstandard
  1255.            shell flags.  */
  1256.         if (! seen_nonequals)
  1257.           goto slow;
  1258.         word_has_equals = 1;
  1259.         *ap++ = '=';
  1260.         break;
  1261.  
  1262.       case '\\':
  1263.         /* Backslash-newline combinations are eaten.  */
  1264.         if (p[1] == '\n')
  1265.           {
  1266.         /* Eat the backslash, the newline, and following whitespace,
  1267.            replacing it all with a single space.  */
  1268.         p += 2;
  1269.  
  1270.         /* If there is a tab after a backslash-newline,
  1271.            remove it from the source line which will be echoed,
  1272.            since it was most likely used to line
  1273.            up the continued line with the previous one.  */
  1274.         if (*p == '\t')
  1275.           strcpy (p, p + 1);
  1276.  
  1277.         if (ap != new_argv[i])
  1278.           /* Treat this as a space, ending the arg.
  1279.              But if it's at the beginning of the arg, it should
  1280.              just get eaten, rather than becoming an empty arg. */
  1281.           goto end_of_arg;
  1282.         else
  1283.           p = next_token (p) - 1;
  1284.           }
  1285.         else if (p[1] != '\0')
  1286.           /* Copy and skip the following char.  */
  1287.           *ap++ = *++p;
  1288.         break;
  1289.  
  1290.       case '\'':
  1291.         instring = 1;
  1292.         break;
  1293.  
  1294.       case '\n':
  1295.         if (restp != NULL)
  1296.           {
  1297.         /* End of the command line.  */
  1298.         *restp = p;
  1299.         goto end_of_line;
  1300.           }
  1301.         else
  1302.           /* Newlines are not special.  */
  1303.           *ap++ = '\n';
  1304.         break;
  1305.  
  1306.       case ' ':
  1307.       case '\t':
  1308.       end_of_arg:
  1309.         /* We have the end of an argument.
  1310.            Terminate the text of the argument.  */
  1311.         *ap++ = '\0';
  1312.         new_argv[++i] = ap;
  1313.  
  1314.         /* Update SEEN_NONEQUALS, which tells us if every word
  1315.            heretofore has contained an `='.  */
  1316.         seen_nonequals |= ! word_has_equals;
  1317.         if (word_has_equals && ! seen_nonequals)
  1318.           /* An `=' in a word before the first
  1319.          word without one is magical.  */
  1320.           goto slow;
  1321.         word_has_equals = 0; /* Prepare for the next word.  */
  1322.  
  1323.         /* If this argument is the command name,
  1324.            see if it is a built-in shell command.
  1325.            If so, have the shell handle it.  */
  1326.         if (i == 1)
  1327.           {
  1328.         register int j;
  1329.         for (j = 0; sh_cmds[j] != 0; ++j)
  1330.           if (streq (sh_cmds[j], new_argv[0]))
  1331.             goto slow;
  1332.           }
  1333.  
  1334.         /* Ignore multiple whitespace chars.  */
  1335.         p = next_token (p);
  1336.         /* Next iteration should examine the first nonwhite char.  */
  1337.         --p;
  1338.         break;
  1339.  
  1340.       default:
  1341.         *ap++ = *p;
  1342.         break;
  1343.       }
  1344.     }
  1345.  end_of_line:
  1346.  
  1347.   if (instring)
  1348.     /* Let the shell deal with an unterminated quote.  */
  1349.     goto slow;
  1350.  
  1351.   /* Terminate the last argument and the argument list.  */
  1352.  
  1353.   *ap = '\0';
  1354.   if (new_argv[i][0] != '\0')
  1355.     ++i;
  1356.   new_argv[i] = 0;
  1357.  
  1358.   if (i == 1)
  1359.     {
  1360.       register int j;
  1361.       for (j = 0; sh_cmds[j] != 0; ++j)
  1362.     if (streq (sh_cmds[j], new_argv[0]))
  1363.       goto slow;
  1364.     }
  1365.  
  1366.   if (new_argv[0] == 0)
  1367.     /* Line was empty.  */
  1368.     return 0;
  1369.   else
  1370.     return new_argv;
  1371.  
  1372.  slow:;
  1373.   /* We must use the shell.  */
  1374.  
  1375.   if (new_argv != 0)
  1376.     {
  1377.       /* Free the old argument list we were working on.  */
  1378.       free (new_argv[0]);
  1379.       free (new_argv);
  1380.     }
  1381.  
  1382.   {
  1383.     /* SHELL may be a multi-word command.  Construct a command line
  1384.        "SHELL -c LINE", with all special chars in LINE escaped.
  1385.        Then recurse, expanding this command line to get the final
  1386.        argument list.  */
  1387.     
  1388.     unsigned int shell_len = strlen (shell);
  1389.     static char minus_c[] = " -c ";
  1390.     unsigned int line_len = strlen (line);
  1391.     
  1392.     char *new_line = (char *) alloca (shell_len + (sizeof (minus_c) - 1)
  1393.                       + (line_len * 2) + 1);
  1394.     
  1395.     ap = new_line;
  1396.     bcopy (shell, ap, shell_len);
  1397.     ap += shell_len;
  1398.     bcopy (minus_c, ap, sizeof (minus_c) - 1);
  1399.     ap += sizeof (minus_c) - 1;
  1400.     for (p = line; *p != '\0'; ++p)
  1401.       {
  1402.     if (restp != NULL && *p == '\n')
  1403.       {
  1404.         *restp = p;
  1405.         break;
  1406.       }
  1407.     else if (*p == '\\' && p[1] == '\n')
  1408.       {
  1409.         /* Eat the backslash, the newline, and following whitespace,
  1410.            replacing it all with a single space (which is escaped
  1411.            from the shell).  */
  1412.         p += 2;
  1413.  
  1414.         /* If there is a tab after a backslash-newline,
  1415.            remove it from the source line which will be echoed,
  1416.            since it was most likely used to line
  1417.            up the continued line with the previous one.  */
  1418.         if (*p == '\t')
  1419.           strcpy (p, p + 1);
  1420.  
  1421.         p = next_token (p);
  1422.         --p;
  1423.         *ap++ = '\\';
  1424.         *ap++ = ' ';
  1425.         continue;
  1426.       }
  1427.  
  1428.     if (*p == '\\' || *p == '\''
  1429.         || isspace (*p)
  1430.         || index (sh_chars, *p) != 0)
  1431.       *ap++ = '\\';
  1432.     *ap++ = *p;
  1433.       }
  1434.     *ap = '\0';
  1435.     
  1436.     new_argv = construct_command_argv_internal (new_line, (char **) NULL,
  1437.                         (char *) 0, (char *) 0);
  1438.   }
  1439.  
  1440.   return new_argv;
  1441. }
  1442.  
  1443. /* Figure out the argument list necessary to run LINE as a command.
  1444.    Try to avoid using a shell.  This routine handles only ' quoting.
  1445.    Starting quotes may be escaped with a backslash.  If any of the
  1446.    characters in sh_chars[] is seen, or any of the builtin commands
  1447.    listed in sh_cmds[] is the first word of a line, the shell is used.
  1448.  
  1449.    If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
  1450.    If *RESTP is NULL, newlines will be ignored.
  1451.  
  1452.    FILE is the target whose commands these are.  It is used for
  1453.    variable expansion for $(SHELL) and $(IFS).  */
  1454.  
  1455. char **
  1456. construct_command_argv (line, restp, file)
  1457.      char *line, **restp;
  1458.      struct file *file;
  1459. {
  1460.   char *shell, *ifs;
  1461.   char **argv;
  1462.  
  1463.   {
  1464.     /* Turn off --warn-undefined-variables while we expand SHELL and IFS.  */
  1465.     int save = warn_undefined_variables_flag;
  1466.     warn_undefined_variables_flag = 0;
  1467.  
  1468.     shell = allocated_variable_expand_for_file ("$(SHELL)", file);
  1469.     ifs = allocated_variable_expand_for_file ("$(IFS)", file);
  1470.  
  1471.     warn_undefined_variables_flag = save;
  1472.   }
  1473.  
  1474.   argv = construct_command_argv_internal (line, restp, shell, ifs);
  1475.  
  1476.   free (shell);
  1477.   free (ifs);
  1478.  
  1479.   return argv;
  1480. }
  1481.  
  1482. #ifndef    HAVE_DUP2
  1483. int
  1484. dup2 (old, new)
  1485.      int old, new;
  1486. {
  1487.   int fd;
  1488.  
  1489.   (void) close (new);
  1490.   fd = dup (old);
  1491.   if (fd != new)
  1492.     {
  1493.       (void) close (fd);
  1494.       errno = EMFILE;
  1495.       return -1;
  1496.     }
  1497.  
  1498.   return fd;
  1499. }
  1500. #endif
  1501.