Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / builtins / wait.def
index 22a92be..fe6d53d 100644 (file)
@@ -1,44 +1,57 @@
 This file is wait.def, from which is created wait.c.
 It implements the builtin "wait" in Bash.
 
-Copyright (C) 1987-2005 Free Software Foundation, Inc.
+Copyright (C) 1987-2013 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
-Bash is free software; you can redistribute it and/or modify it under
-the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 2, or (at your option) any later
-version.
+Bash is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
-Bash is distributed in the hope that it will be useful, but WITHOUT ANY
-WARRANTY; without even the implied warranty of MERCHANTABILITY or
-FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-for more details.
+Bash is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
-with Bash; see the file COPYING.  If not, write to the Free Software
-Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
+You should have received a copy of the GNU General Public License
+along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 
 $BUILTIN wait
 $FUNCTION wait_builtin
 $DEPENDS_ON JOB_CONTROL
 $PRODUCES wait.c
-$SHORT_DOC wait [n]
-Wait for the specified process and report its termination status.  If
-N is not given, all currently active child processes are waited for,
-and the return code is zero.  N may be a process ID or a job
-specification; if a job spec is given, all processes in the job's
-pipeline are waited for.
+$SHORT_DOC wait [-n] [id ...]
+Wait for job completion and return exit status.
+
+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 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 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 [n]
-Wait for the specified process and report its termination status.  If
-N is not given, all currently active child processes are waited for,
-and the return code is zero.  N is a process ID; if it is not given,
-all child processes of the shell are waited for.
+$SHORT_DOC wait [pid ...]
+Wait for process completion and return exit status.
+
+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 the last PID; fails if PID is invalid or an invalid
+option is given.
 $END
 
 #include <config.h>
@@ -59,7 +72,6 @@ $END
 #include "common.h"
 #include "bashgetopt.h"
 
-extern int interrupt_immediately;
 extern int wait_signal_received;
 
 procenv_t wait_intr_buf;
@@ -73,6 +85,7 @@ procenv_t wait_intr_buf;
   do \
     { \
       interrupt_immediately = old_interrupt_immediately;\
+      wait_signal_received = 0; \
       return (s);\
     } \
   while (0)
@@ -81,17 +94,33 @@ 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
+       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
@@ -111,6 +140,16 @@ wait_builtin (list)
   /* 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)