1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2022 WATANABE Yuki
//
// This program 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.
//
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
//! Wait built-in
//!
//! The **`wait`** built-in waits for asynchronous jobs to finish.
//!
//! # Synopsis
//!
//! ```sh
//! wait [job_id_or_process_id…]
//! ```
//!
//! # Description
//!
//! If you specify one or more operands, the built-in waits for the specified
//! jobs to finish. Otherwise, the built-in waits for all existing asynchronous
//! jobs. If the jobs are already finished, the built-in returns without
//! waiting.
//!
//! If a job is job-controlled (that is, running in its own process group), it
//! is considered finished not only when it has exited but also when it has been
//! suspended. (TODO: This behavior is contrary to POSIX 2024 and will be fixed
//! in the future.)
//!
//! # Options
//!
//! None
//!
//! # Operands
//!
//! An operand can be a job ID or decimal process ID, specifying which job to
//! wait for. A job ID must start with `%` and has the format described in the
//! [`yash_env::job::id`] module documentation. A process ID is a non-negative
//! decimal integer.
//!
//! If there is no job matching the operand, the built-in assumes that the
//! job has already finished with exit status 127.
//!
//! # Errors
//!
//! The following error conditions causes the built-in to return a non-zero exit
//! status without waiting for any job:
//!
//! - An operand is not a job ID or decimal process ID.
//! - A job ID matches more than one job.
//! - The shell receives a signal that has a [trap](yash_env::trap) action set.
//!
//! The trap action for the signal is executed before the built-in returns.
//!
//! # Exit status
//!
//! If you specify one or more operands, the built-in returns the exit status of
//! the job specified by the last operand. If there is no operand, the exit
//! status is 0 regardless of the awaited jobs.
//!
//! If the built-in was interrupted by a signal, the exit status indicates the
//! signal.
//!
//! The exit status is between 1 and 126 (inclusive) for any other error.
//!
//! # Portability
//!
//! The wait built-in is contained in the POSIX standard.
//!
//! The exact value of an exit status resulting from a signal is
//! implementation-dependent.
//!
//! Many existing shells behave differently on various errors. POSIX requires
//! that an unknown process ID be treated as a process that has already exited
//! with exit status 127, but the behavior for other errors should not be
//! considered portable.
//!
//! # Implementation notes
//!
//! The built-in treats disowned jobs as if they were finished with an exit
//! status of 127.
use cratereport_error;
use cratereport_simple_failure;
use crateto_single_message;
use Itertools as _;
use Pid;
use Monitor;
use ExitStatus;
use Field;
use Env;
/// Job specification (job ID or process ID)
///
/// Each operand of the `wait` built-in is parsed into a `JobSpec` value.
/// Parsed command line arguments to the `wait` built-in
/// Entry point for executing the `wait` built-in
pub async