Imported from ../bash-1.14.7.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
23 $BUILTIN wait
24 $FUNCTION wait_builtin
25 $DEPENDS_ON JOB_CONTROL
26 $PRODUCES wait.c
27 $SHORT_DOC wait [n]
28 Wait for the specified process and report its termination status.  If
29 N is not given, all currently active child processes are waited for,
30 and the return code is zero.  N may be a process ID or a job
31 specification; if a job spec is given, all processes in the job's
32 pipeline are waited for.
33 $END
34
35 $BUILTIN wait
36 $FUNCTION wait_builtin
37 $DEPENDS_ON !JOB_CONTROL
38 $SHORT_DOC wait [n]
39 Wait for the specified process and report its termination status.  If
40 N is not given, all currently active child processes are waited for,
41 and the return code is zero.  N is a process ID; if it is not given,
42 all child processes of the shell are waited for.
43 $END
44
45 #include <sys/types.h>
46 #include <signal.h>
47 #include "../shell.h"
48 #include "../jobs.h"
49
50 extern int interrupt_immediately;
51
52 /* Wait for the pid in LIST to stop or die.  If no arguments are given, then
53    wait for all of the active background processes of the shell and return
54    0.  If a list of pids or job specs are given, return the exit status of
55    the last one waited for. */
56 wait_builtin (list)
57      WORD_LIST *list;
58 {
59   int status = EXECUTION_SUCCESS;
60
61   begin_unwind_frame ("wait_builtin");
62   unwind_protect_int (interrupt_immediately);
63   interrupt_immediately++;
64
65   /* We support jobs or pids.
66      wait <pid-or-job> [pid-or-job ...] */
67
68   /* But wait without any arguments means to wait for all of the shell's
69      currently active background processes. */
70   if (!list)
71     {
72       wait_for_background_pids ();
73       status = EXECUTION_SUCCESS;
74       goto return_status;
75     }
76
77   while (list)
78     {
79       pid_t pid;
80       char *w;
81
82       w = list->word->word;
83       if (digit (*w))
84         {
85           if (all_digits (w + 1))
86             {
87               pid = (pid_t)atoi (w);
88               status = wait_for_single_pid (pid);
89             }
90           else
91             {
92               builtin_error ("`%s' is not a pid or legal job spec", w);
93               status = EXECUTION_FAILURE;
94               goto return_status;
95             }
96         }
97 #if defined (JOB_CONTROL)
98       else if (job_control && *w)
99         /* Must be a job spec.  Check it out. */
100         {
101           int job;
102           sigset_t set, oset;
103
104           BLOCK_CHILD (set, oset);
105           job = get_job_spec (list);
106
107           if (job < 0 || job >= job_slots || !jobs[job])
108             {
109               if (job != DUP_JOB)
110                 builtin_error ("No such job %s", list->word->word);
111               UNBLOCK_CHILD (oset);
112               status = 127;     /* As per Posix.2, section 4.70.2 */
113               list = list->next;
114               continue;
115             }
116
117           /* Job spec used.  Wait for the last pid in the pipeline. */
118           UNBLOCK_CHILD (oset);
119           status = wait_for_job (job);
120         }
121 #endif /* JOB_CONTROL */
122       else
123         {
124           builtin_error ("`%s' is not a pid or legal job spec", w);
125           status = EXECUTION_FAILURE;
126         }
127       list = list->next;
128     }
129  return_status:
130   run_unwind_frame ("wait_builtin");
131   return (status);
132 }