]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - builtins/wait.def
Bash-4.4 patch 19
[thirdparty/bash.git] / builtins / wait.def
index 0206926f3f9fe48e9609de38d6ccda7265fbb898..974f959b7f53db95dc812361c486a8a7a331156c 100644 (file)
@@ -1,7 +1,7 @@
 This file is wait.def, from which is created wait.c.
 It implements the builtin "wait" in Bash.
 
-Copyright (C) 1987-2009 Free Software Foundation, Inc.
+Copyright (C) 1987-2015 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -22,33 +22,36 @@ $BUILTIN wait
 $FUNCTION wait_builtin
 $DEPENDS_ON JOB_CONTROL
 $PRODUCES wait.c
-$SHORT_DOC wait [id]
+$SHORT_DOC wait [-n] [id ...]
 Wait for job completion and return exit status.
 
-Waits for the process identified by ID, which may be a process ID or a
+Waits for each process identified by an ID, which may be a process ID or a
 job specification, and reports its termination status.  If ID is not
 given, waits for all currently active child processes, and the return
 status is zero.  If ID is a a job specification, waits for all processes
-in the job's pipeline.
+in that job's pipeline.
+
+If the -n option is supplied, waits for the next job to terminate and
+returns its exit status.
 
 Exit Status:
-Returns the status of ID; fails if ID is invalid or an invalid option is
-given.
+Returns the status of the last ID; fails if ID is invalid or an invalid
+option is given.
 $END
 
 $BUILTIN wait
 $FUNCTION wait_builtin
 $DEPENDS_ON !JOB_CONTROL
-$SHORT_DOC wait [pid]
+$SHORT_DOC wait [pid ...]
 Wait for process completion and return exit status.
 
-Waits for the specified process and reports its termination status.  If
-PID is not given, all currently active child processes are waited for,
-and the return code is zero.  PID must be a process ID.
+Waits for each process specified by a PID and reports its termination status.
+If PID is not given, waits for all currently active child processes,
+and the return status is zero.  PID must be a process ID.
 
 Exit Status:
-Returns the status of ID; fails if ID is invalid or an invalid option is
-given.
+Returns the status of the last PID; fails if PID is invalid or an invalid
+option is given.
 $END
 
 #include <config.h>
@@ -70,8 +73,10 @@ $END
 #include "bashgetopt.h"
 
 extern int wait_signal_received;
+extern int last_command_exit_signal;
 
 procenv_t wait_intr_buf;
+int wait_intr_flag;
 
 /* Wait for the pid in LIST to stop or die.  If no arguments are given, then
    wait for all of the active background processes of the shell and return
@@ -82,6 +87,8 @@ procenv_t wait_intr_buf;
   do \
     { \
       interrupt_immediately = old_interrupt_immediately;\
+      wait_signal_received = 0; \
+      wait_intr_flag = 0; \
       return (s);\
     } \
   while (0)
@@ -90,17 +97,34 @@ int
 wait_builtin (list)
      WORD_LIST *list;
 {
-  int status, code;
+  int status, code, opt, nflag;
   volatile int old_interrupt_immediately;
 
   USE_VAR(list);
 
-  if (no_options (list))
-    return (EX_USAGE);
+  nflag = 0;
+  reset_internal_getopt ();
+  while ((opt = internal_getopt (list, "n")) != -1)
+    {
+      switch (opt)
+       {
+#if defined (JOB_CONTROL)
+       case 'n':
+         nflag = 1;
+         break;
+#endif
+       CASE_HELPOPT;
+       default:
+         builtin_usage ();
+         return (EX_USAGE);
+       }
+    }
   list = loptend;
 
   old_interrupt_immediately = interrupt_immediately;
+#if 0
   interrupt_immediately++;
+#endif
 
   /* POSIX.2 says:  When the shell is waiting (by means of the wait utility)
      for asynchronous commands to complete, the reception of a signal for
@@ -110,16 +134,30 @@ wait_builtin (list)
 
      We handle SIGINT here; it's the only one that needs to be treated
      specially (I think), since it's handled specially in {no,}jobs.c. */
-  code = setjmp (wait_intr_buf);
+  wait_intr_flag = 1;
+  code = setjmp_sigs (wait_intr_buf);
+
   if (code)
     {
+      last_command_exit_signal = wait_signal_received;
       status = 128 + wait_signal_received;
+      wait_sigint_cleanup ();
       WAIT_RETURN (status);
     }
 
   /* We support jobs or pids.
      wait <pid-or-job> [pid-or-job ...] */
 
+#if defined (JOB_CONTROL)
+  if (nflag)
+    {
+      status = wait_for_any_job ();
+      if (status < 0)
+       status = 127;
+      WAIT_RETURN (status);
+    }
+#endif
+      
   /* But wait without any arguments means to wait for all of the shell's
      currently active background processes. */
   if (list == 0)
@@ -141,7 +179,7 @@ wait_builtin (list)
          if (legal_number (w, &pid_value) && pid_value == (pid_t)pid_value)
            {
              pid = (pid_t)pid_value;
-             status = wait_for_single_pid (pid);
+             status = wait_for_single_pid (pid, 1);
            }
          else
            {