Imported from ../bash-2.01.tar.gz.
[platform/upstream/bash.git] / builtins / wait.def
1 This file is wait.def, from which is created wait.c.
2 It implements the builtin "wait" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 1, or (at your option) any later
11 version.
12
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING.  If not, write to the Free Software
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 $BUILTIN wait
23 $FUNCTION wait_builtin
24 $DEPENDS_ON JOB_CONTROL
25 $PRODUCES wait.c
26 $SHORT_DOC wait [n]
27 Wait for the specified process and report its termination status.  If
28 N is not given, all currently active child processes are waited for,
29 and the return code is zero.  N may be a process ID or a job
30 specification; if a job spec is given, all processes in the job's
31 pipeline are waited for.
32 $END
33
34 $BUILTIN wait
35 $FUNCTION wait_builtin
36 $DEPENDS_ON !JOB_CONTROL
37 $SHORT_DOC wait [n]
38 Wait for the specified process and report its termination status.  If
39 N is not given, all currently active child processes are waited for,
40 and the return code is zero.  N is a process ID; if it is not given,
41 all child processes of the shell are waited for.
42 $END
43
44 #include <config.h>
45
46 #include "../bashtypes.h"
47 #include <signal.h>
48
49 #if defined (HAVE_UNISTD_H)
50 #  include <unistd.h>
51 #endif
52
53 #include "../bashansi.h"
54
55 #include "../shell.h"
56 #include "../jobs.h"
57 #include "common.h"
58 #include "bashgetopt.h"
59
60 extern int interrupt_immediately;
61
62 /* Wait for the pid in LIST to stop or die.  If no arguments are given, then
63    wait for all of the active background processes of the shell and return
64    0.  If a list of pids or job specs are given, return the exit status of
65    the last one waited for. */
66
67 #define WAIT_RETURN(s) do { run_unwind_frame ("wait_builtin"); return (s); } while (0)
68
69 int
70 wait_builtin (list)
71      WORD_LIST *list;
72 {
73   int status;
74
75   if (no_options (list))
76     return (EX_USAGE);
77   if (list != loptend)
78     list = loptend;
79
80   begin_unwind_frame ("wait_builtin");
81   unwind_protect_int (interrupt_immediately);
82   interrupt_immediately++;
83
84   /* We support jobs or pids.
85      wait <pid-or-job> [pid-or-job ...] */
86
87   /* But wait without any arguments means to wait for all of the shell's
88      currently active background processes. */
89   if (list == 0)
90     {
91       wait_for_background_pids ();
92       WAIT_RETURN (EXECUTION_SUCCESS);
93     }
94
95   status = EXECUTION_SUCCESS;
96   while (list)
97     {
98       pid_t pid;
99       char *w;
100
101       w = list->word->word;
102       if (digit (*w))
103         {
104           if (all_digits (w + 1))
105             {
106               pid = (pid_t)atoi (w);
107               status = wait_for_single_pid (pid);
108             }
109           else
110             {
111               builtin_error ("`%s' is not a pid or valid job spec", w);
112               WAIT_RETURN (EXECUTION_FAILURE);
113             }
114         }
115 #if defined (JOB_CONTROL)
116       else if (job_control && *w)
117         /* Must be a job spec.  Check it out. */
118         {
119           int job;
120           sigset_t set, oset;
121
122           BLOCK_CHILD (set, oset);
123           job = get_job_spec (list);
124
125           if (job < 0 || job >= job_slots || !jobs[job])
126             {
127               if (job != DUP_JOB)
128                 builtin_error ("%s: no such job", list->word->word);
129               UNBLOCK_CHILD (oset);
130               status = 127;     /* As per Posix.2, section 4.70.2 */
131               list = list->next;
132               continue;
133             }
134
135           /* Job spec used.  Wait for the last pid in the pipeline. */
136           UNBLOCK_CHILD (oset);
137           status = wait_for_job (job);
138         }
139 #endif /* JOB_CONTROL */
140       else
141         {
142           builtin_error ("`%s' is not a pid or valid job spec", w);
143           status = EXECUTION_FAILURE;
144         }
145       list = list->next;
146     }
147
148   WAIT_RETURN (status);
149 }