jobs.c revision 1.23 1 1.23 christos /* $NetBSD: jobs.c,v 1.23 1997/10/08 20:31:52 christos Exp $ */
2 1.15 cgd
3 1.1 cgd /*-
4 1.8 jtc * Copyright (c) 1991, 1993
5 1.8 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.22 christos #include <sys/cdefs.h>
40 1.1 cgd #ifndef lint
41 1.15 cgd #if 0
42 1.16 christos static char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95";
43 1.15 cgd #else
44 1.23 christos __RCSID("$NetBSD: jobs.c,v 1.23 1997/10/08 20:31:52 christos Exp $");
45 1.15 cgd #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.16 christos #include <fcntl.h>
49 1.16 christos #include <signal.h>
50 1.16 christos #include <errno.h>
51 1.16 christos #include <unistd.h>
52 1.16 christos #include <stdlib.h>
53 1.16 christos #include <sys/types.h>
54 1.16 christos #include <sys/param.h>
55 1.16 christos #ifdef BSD
56 1.16 christos #include <sys/wait.h>
57 1.16 christos #include <sys/time.h>
58 1.16 christos #include <sys/resource.h>
59 1.16 christos #endif
60 1.19 christos #include <sys/ioctl.h>
61 1.16 christos
62 1.1 cgd #include "shell.h"
63 1.1 cgd #if JOBS
64 1.19 christos #if OLD_TTY_DRIVER
65 1.1 cgd #include "sgtty.h"
66 1.19 christos #else
67 1.19 christos #include <termios.h>
68 1.19 christos #endif
69 1.1 cgd #undef CEOF /* syntax.h redefines this */
70 1.1 cgd #endif
71 1.16 christos #include "redir.h"
72 1.16 christos #include "show.h"
73 1.1 cgd #include "main.h"
74 1.1 cgd #include "parser.h"
75 1.1 cgd #include "nodes.h"
76 1.1 cgd #include "jobs.h"
77 1.1 cgd #include "options.h"
78 1.1 cgd #include "trap.h"
79 1.1 cgd #include "syntax.h"
80 1.1 cgd #include "input.h"
81 1.1 cgd #include "output.h"
82 1.1 cgd #include "memalloc.h"
83 1.1 cgd #include "error.h"
84 1.1 cgd #include "mystring.h"
85 1.1 cgd
86 1.1 cgd
87 1.1 cgd struct job *jobtab; /* array of jobs */
88 1.1 cgd int njobs; /* size of array */
89 1.1 cgd MKINIT short backgndpid = -1; /* pid of last background process */
90 1.1 cgd #if JOBS
91 1.1 cgd int initialpgrp; /* pgrp of shell on invocation */
92 1.1 cgd short curjob; /* current job */
93 1.1 cgd #endif
94 1.1 cgd
95 1.16 christos STATIC void restartjob __P((struct job *));
96 1.16 christos STATIC void freejob __P((struct job *));
97 1.16 christos STATIC struct job *getjob __P((char *));
98 1.16 christos STATIC int dowait __P((int, struct job *));
99 1.16 christos STATIC int onsigchild __P((void));
100 1.16 christos STATIC int waitproc __P((int, int *));
101 1.16 christos STATIC void cmdtxt __P((union node *));
102 1.16 christos STATIC void cmdputs __P((char *));
103 1.1 cgd
104 1.8 jtc
105 1.1 cgd /*
106 1.1 cgd * Turn job control on and off.
107 1.1 cgd *
108 1.1 cgd * Note: This code assumes that the third arg to ioctl is a character
109 1.1 cgd * pointer, which is true on Berkeley systems but not System V. Since
110 1.1 cgd * System V doesn't have job control yet, this isn't a problem now.
111 1.1 cgd */
112 1.1 cgd
113 1.1 cgd MKINIT int jobctl;
114 1.1 cgd
115 1.1 cgd void
116 1.19 christos setjobctl(on)
117 1.13 cgd int on;
118 1.13 cgd {
119 1.8 jtc #ifdef OLD_TTY_DRIVER
120 1.1 cgd int ldisc;
121 1.8 jtc #endif
122 1.1 cgd
123 1.1 cgd if (on == jobctl || rootshell == 0)
124 1.1 cgd return;
125 1.1 cgd if (on) {
126 1.1 cgd do { /* while we are in the background */
127 1.18 mycroft #ifdef OLD_TTY_DRIVER
128 1.1 cgd if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) {
129 1.18 mycroft #else
130 1.18 mycroft initialpgrp = tcgetpgrp(2);
131 1.18 mycroft if (initialpgrp < 0) {
132 1.18 mycroft #endif
133 1.8 jtc out2str("sh: can't access tty; job control turned off\n");
134 1.8 jtc mflag = 0;
135 1.1 cgd return;
136 1.1 cgd }
137 1.1 cgd if (initialpgrp == -1)
138 1.10 jtc initialpgrp = getpgrp();
139 1.10 jtc else if (initialpgrp != getpgrp()) {
140 1.1 cgd killpg(initialpgrp, SIGTTIN);
141 1.1 cgd continue;
142 1.1 cgd }
143 1.1 cgd } while (0);
144 1.8 jtc #ifdef OLD_TTY_DRIVER
145 1.1 cgd if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) {
146 1.8 jtc out2str("sh: need new tty driver to run job control; job control turned off\n");
147 1.8 jtc mflag = 0;
148 1.1 cgd return;
149 1.1 cgd }
150 1.8 jtc #endif
151 1.1 cgd setsignal(SIGTSTP);
152 1.1 cgd setsignal(SIGTTOU);
153 1.8 jtc setsignal(SIGTTIN);
154 1.12 mycroft setpgid(0, rootpid);
155 1.18 mycroft #ifdef OLD_TTY_DRIVER
156 1.1 cgd ioctl(2, TIOCSPGRP, (char *)&rootpid);
157 1.18 mycroft #else
158 1.18 mycroft tcsetpgrp(2, rootpid);
159 1.18 mycroft #endif
160 1.1 cgd } else { /* turning job control off */
161 1.12 mycroft setpgid(0, initialpgrp);
162 1.18 mycroft #ifdef OLD_TTY_DRIVER
163 1.1 cgd ioctl(2, TIOCSPGRP, (char *)&initialpgrp);
164 1.18 mycroft #else
165 1.18 mycroft tcsetpgrp(2, initialpgrp);
166 1.18 mycroft #endif
167 1.1 cgd setsignal(SIGTSTP);
168 1.1 cgd setsignal(SIGTTOU);
169 1.8 jtc setsignal(SIGTTIN);
170 1.1 cgd }
171 1.1 cgd jobctl = on;
172 1.1 cgd }
173 1.1 cgd
174 1.1 cgd
175 1.1 cgd #ifdef mkinit
176 1.16 christos INCLUDE <stdlib.h>
177 1.1 cgd
178 1.1 cgd SHELLPROC {
179 1.1 cgd backgndpid = -1;
180 1.1 cgd #if JOBS
181 1.1 cgd jobctl = 0;
182 1.1 cgd #endif
183 1.1 cgd }
184 1.1 cgd
185 1.1 cgd #endif
186 1.1 cgd
187 1.1 cgd
188 1.1 cgd
189 1.1 cgd #if JOBS
190 1.13 cgd int
191 1.13 cgd fgcmd(argc, argv)
192 1.13 cgd int argc;
193 1.19 christos char **argv;
194 1.13 cgd {
195 1.1 cgd struct job *jp;
196 1.1 cgd int pgrp;
197 1.1 cgd int status;
198 1.1 cgd
199 1.1 cgd jp = getjob(argv[1]);
200 1.1 cgd if (jp->jobctl == 0)
201 1.1 cgd error("job not created under job control");
202 1.1 cgd pgrp = jp->ps[0].pid;
203 1.18 mycroft #ifdef OLD_TTY_DRIVER
204 1.1 cgd ioctl(2, TIOCSPGRP, (char *)&pgrp);
205 1.18 mycroft #else
206 1.18 mycroft tcsetpgrp(2, pgrp);
207 1.18 mycroft #endif
208 1.1 cgd restartjob(jp);
209 1.1 cgd INTOFF;
210 1.1 cgd status = waitforjob(jp);
211 1.1 cgd INTON;
212 1.1 cgd return status;
213 1.1 cgd }
214 1.1 cgd
215 1.1 cgd
216 1.13 cgd int
217 1.13 cgd bgcmd(argc, argv)
218 1.13 cgd int argc;
219 1.19 christos char **argv;
220 1.13 cgd {
221 1.1 cgd struct job *jp;
222 1.1 cgd
223 1.1 cgd do {
224 1.1 cgd jp = getjob(*++argv);
225 1.1 cgd if (jp->jobctl == 0)
226 1.1 cgd error("job not created under job control");
227 1.1 cgd restartjob(jp);
228 1.1 cgd } while (--argc > 1);
229 1.1 cgd return 0;
230 1.1 cgd }
231 1.1 cgd
232 1.1 cgd
233 1.1 cgd STATIC void
234 1.1 cgd restartjob(jp)
235 1.1 cgd struct job *jp;
236 1.13 cgd {
237 1.1 cgd struct procstat *ps;
238 1.1 cgd int i;
239 1.1 cgd
240 1.1 cgd if (jp->state == JOBDONE)
241 1.1 cgd return;
242 1.1 cgd INTOFF;
243 1.1 cgd killpg(jp->ps[0].pid, SIGCONT);
244 1.1 cgd for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
245 1.23 christos if (WIFSTOPPED(ps->status)) {
246 1.1 cgd ps->status = -1;
247 1.1 cgd jp->state = 0;
248 1.1 cgd }
249 1.1 cgd }
250 1.1 cgd INTON;
251 1.1 cgd }
252 1.1 cgd #endif
253 1.1 cgd
254 1.1 cgd
255 1.1 cgd int
256 1.13 cgd jobscmd(argc, argv)
257 1.13 cgd int argc;
258 1.19 christos char **argv;
259 1.13 cgd {
260 1.1 cgd showjobs(0);
261 1.1 cgd return 0;
262 1.1 cgd }
263 1.1 cgd
264 1.1 cgd
265 1.1 cgd /*
266 1.1 cgd * Print a list of jobs. If "change" is nonzero, only print jobs whose
267 1.1 cgd * statuses have changed since the last call to showjobs.
268 1.1 cgd *
269 1.1 cgd * If the shell is interrupted in the process of creating a job, the
270 1.1 cgd * result may be a job structure containing zero processes. Such structures
271 1.1 cgd * will be freed here.
272 1.1 cgd */
273 1.1 cgd
274 1.1 cgd void
275 1.19 christos showjobs(change)
276 1.13 cgd int change;
277 1.13 cgd {
278 1.1 cgd int jobno;
279 1.1 cgd int procno;
280 1.1 cgd int i;
281 1.1 cgd struct job *jp;
282 1.1 cgd struct procstat *ps;
283 1.1 cgd int col;
284 1.1 cgd char s[64];
285 1.1 cgd
286 1.1 cgd TRACE(("showjobs(%d) called\n", change));
287 1.1 cgd while (dowait(0, (struct job *)NULL) > 0);
288 1.1 cgd for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
289 1.1 cgd if (! jp->used)
290 1.1 cgd continue;
291 1.1 cgd if (jp->nprocs == 0) {
292 1.1 cgd freejob(jp);
293 1.1 cgd continue;
294 1.1 cgd }
295 1.1 cgd if (change && ! jp->changed)
296 1.1 cgd continue;
297 1.1 cgd procno = jp->nprocs;
298 1.1 cgd for (ps = jp->ps ; ; ps++) { /* for each process */
299 1.1 cgd if (ps == jp->ps)
300 1.1 cgd fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
301 1.1 cgd else
302 1.1 cgd fmtstr(s, 64, " %d ", ps->pid);
303 1.1 cgd out1str(s);
304 1.1 cgd col = strlen(s);
305 1.1 cgd s[0] = '\0';
306 1.1 cgd if (ps->status == -1) {
307 1.1 cgd /* don't print anything */
308 1.23 christos } else if (WIFEXITED(ps->status)) {
309 1.23 christos fmtstr(s, 64, "Exit %d",
310 1.23 christos WEXITSTATUS(ps->status));
311 1.1 cgd } else {
312 1.1 cgd #if JOBS
313 1.23 christos if (WIFSTOPPED(ps->status))
314 1.23 christos i = WSTOPSIG(ps->status);
315 1.23 christos else /* WIFSIGNALED(ps->status) */
316 1.1 cgd #endif
317 1.23 christos i = WTERMSIG(ps->status);
318 1.9 jtc if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
319 1.9 jtc scopy(sys_siglist[i & 0x7F], s);
320 1.1 cgd else
321 1.1 cgd fmtstr(s, 64, "Signal %d", i & 0x7F);
322 1.23 christos if (WCOREDUMP(ps->status))
323 1.1 cgd strcat(s, " (core dumped)");
324 1.1 cgd }
325 1.1 cgd out1str(s);
326 1.1 cgd col += strlen(s);
327 1.1 cgd do {
328 1.1 cgd out1c(' ');
329 1.1 cgd col++;
330 1.1 cgd } while (col < 30);
331 1.1 cgd out1str(ps->cmd);
332 1.1 cgd out1c('\n');
333 1.1 cgd if (--procno <= 0)
334 1.1 cgd break;
335 1.1 cgd }
336 1.1 cgd jp->changed = 0;
337 1.1 cgd if (jp->state == JOBDONE) {
338 1.1 cgd freejob(jp);
339 1.1 cgd }
340 1.1 cgd }
341 1.1 cgd }
342 1.1 cgd
343 1.1 cgd
344 1.1 cgd /*
345 1.1 cgd * Mark a job structure as unused.
346 1.1 cgd */
347 1.1 cgd
348 1.1 cgd STATIC void
349 1.1 cgd freejob(jp)
350 1.1 cgd struct job *jp;
351 1.1 cgd {
352 1.1 cgd struct procstat *ps;
353 1.1 cgd int i;
354 1.1 cgd
355 1.1 cgd INTOFF;
356 1.1 cgd for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
357 1.1 cgd if (ps->cmd != nullstr)
358 1.1 cgd ckfree(ps->cmd);
359 1.1 cgd }
360 1.1 cgd if (jp->ps != &jp->ps0)
361 1.1 cgd ckfree(jp->ps);
362 1.1 cgd jp->used = 0;
363 1.1 cgd #if JOBS
364 1.1 cgd if (curjob == jp - jobtab + 1)
365 1.1 cgd curjob = 0;
366 1.1 cgd #endif
367 1.1 cgd INTON;
368 1.1 cgd }
369 1.1 cgd
370 1.1 cgd
371 1.1 cgd
372 1.1 cgd int
373 1.19 christos waitcmd(argc, argv)
374 1.13 cgd int argc;
375 1.19 christos char **argv;
376 1.13 cgd {
377 1.1 cgd struct job *job;
378 1.23 christos int status, retval;
379 1.1 cgd struct job *jp;
380 1.1 cgd
381 1.1 cgd if (argc > 1) {
382 1.1 cgd job = getjob(argv[1]);
383 1.1 cgd } else {
384 1.1 cgd job = NULL;
385 1.1 cgd }
386 1.1 cgd for (;;) { /* loop until process terminated or stopped */
387 1.1 cgd if (job != NULL) {
388 1.1 cgd if (job->state) {
389 1.1 cgd status = job->ps[job->nprocs - 1].status;
390 1.23 christos if (WIFEXITED(status))
391 1.23 christos retval = WEXITSTATUS(status);
392 1.1 cgd #if JOBS
393 1.23 christos else if (WIFSTOPPED(status))
394 1.23 christos retval = WSTOPSIG(status) + 128;
395 1.1 cgd #endif
396 1.23 christos else {
397 1.23 christos /* XXX: limits number of signals */
398 1.23 christos retval = WTERMSIG(status) + 128;
399 1.23 christos }
400 1.1 cgd if (! iflag)
401 1.1 cgd freejob(job);
402 1.23 christos return retval;
403 1.1 cgd }
404 1.1 cgd } else {
405 1.1 cgd for (jp = jobtab ; ; jp++) {
406 1.1 cgd if (jp >= jobtab + njobs) { /* no running procs */
407 1.1 cgd return 0;
408 1.1 cgd }
409 1.1 cgd if (jp->used && jp->state == 0)
410 1.1 cgd break;
411 1.1 cgd }
412 1.1 cgd }
413 1.1 cgd dowait(1, (struct job *)NULL);
414 1.1 cgd }
415 1.1 cgd }
416 1.1 cgd
417 1.1 cgd
418 1.1 cgd
419 1.13 cgd int
420 1.19 christos jobidcmd(argc, argv)
421 1.13 cgd int argc;
422 1.19 christos char **argv;
423 1.13 cgd {
424 1.1 cgd struct job *jp;
425 1.1 cgd int i;
426 1.1 cgd
427 1.1 cgd jp = getjob(argv[1]);
428 1.1 cgd for (i = 0 ; i < jp->nprocs ; ) {
429 1.1 cgd out1fmt("%d", jp->ps[i].pid);
430 1.1 cgd out1c(++i < jp->nprocs? ' ' : '\n');
431 1.1 cgd }
432 1.1 cgd return 0;
433 1.1 cgd }
434 1.1 cgd
435 1.1 cgd
436 1.1 cgd
437 1.1 cgd /*
438 1.1 cgd * Convert a job name to a job structure.
439 1.1 cgd */
440 1.1 cgd
441 1.1 cgd STATIC struct job *
442 1.1 cgd getjob(name)
443 1.1 cgd char *name;
444 1.1 cgd {
445 1.1 cgd int jobno;
446 1.21 tls struct job *jp;
447 1.1 cgd int pid;
448 1.1 cgd int i;
449 1.1 cgd
450 1.1 cgd if (name == NULL) {
451 1.1 cgd #if JOBS
452 1.1 cgd currentjob:
453 1.1 cgd if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0)
454 1.1 cgd error("No current job");
455 1.1 cgd return &jobtab[jobno - 1];
456 1.1 cgd #else
457 1.1 cgd error("No current job");
458 1.1 cgd #endif
459 1.1 cgd } else if (name[0] == '%') {
460 1.1 cgd if (is_digit(name[1])) {
461 1.1 cgd jobno = number(name + 1);
462 1.1 cgd if (jobno > 0 && jobno <= njobs
463 1.1 cgd && jobtab[jobno - 1].used != 0)
464 1.1 cgd return &jobtab[jobno - 1];
465 1.1 cgd #if JOBS
466 1.1 cgd } else if (name[1] == '%' && name[2] == '\0') {
467 1.1 cgd goto currentjob;
468 1.1 cgd #endif
469 1.1 cgd } else {
470 1.21 tls struct job *found = NULL;
471 1.1 cgd for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
472 1.1 cgd if (jp->used && jp->nprocs > 0
473 1.1 cgd && prefix(name + 1, jp->ps[0].cmd)) {
474 1.1 cgd if (found)
475 1.1 cgd error("%s: ambiguous", name);
476 1.1 cgd found = jp;
477 1.1 cgd }
478 1.1 cgd }
479 1.1 cgd if (found)
480 1.1 cgd return found;
481 1.1 cgd }
482 1.1 cgd } else if (is_number(name)) {
483 1.1 cgd pid = number(name);
484 1.1 cgd for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
485 1.1 cgd if (jp->used && jp->nprocs > 0
486 1.1 cgd && jp->ps[jp->nprocs - 1].pid == pid)
487 1.1 cgd return jp;
488 1.1 cgd }
489 1.1 cgd }
490 1.1 cgd error("No such job: %s", name);
491 1.16 christos /*NOTREACHED*/
492 1.16 christos return NULL;
493 1.1 cgd }
494 1.1 cgd
495 1.1 cgd
496 1.1 cgd
497 1.1 cgd /*
498 1.1 cgd * Return a new job structure,
499 1.1 cgd */
500 1.1 cgd
501 1.1 cgd struct job *
502 1.1 cgd makejob(node, nprocs)
503 1.1 cgd union node *node;
504 1.13 cgd int nprocs;
505 1.13 cgd {
506 1.1 cgd int i;
507 1.1 cgd struct job *jp;
508 1.1 cgd
509 1.1 cgd for (i = njobs, jp = jobtab ; ; jp++) {
510 1.1 cgd if (--i < 0) {
511 1.1 cgd INTOFF;
512 1.1 cgd if (njobs == 0) {
513 1.1 cgd jobtab = ckmalloc(4 * sizeof jobtab[0]);
514 1.1 cgd } else {
515 1.1 cgd jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
516 1.12 mycroft memcpy(jp, jobtab, njobs * sizeof jp[0]);
517 1.17 pk /* Relocate `ps' pointers */
518 1.17 pk for (i = 0; i < njobs; i++)
519 1.17 pk if (jp[i].ps == &jobtab[i].ps0)
520 1.17 pk jp[i].ps = &jp[i].ps0;
521 1.1 cgd ckfree(jobtab);
522 1.1 cgd jobtab = jp;
523 1.1 cgd }
524 1.1 cgd jp = jobtab + njobs;
525 1.1 cgd for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
526 1.1 cgd INTON;
527 1.1 cgd break;
528 1.1 cgd }
529 1.1 cgd if (jp->used == 0)
530 1.1 cgd break;
531 1.1 cgd }
532 1.1 cgd INTOFF;
533 1.1 cgd jp->state = 0;
534 1.1 cgd jp->used = 1;
535 1.1 cgd jp->changed = 0;
536 1.1 cgd jp->nprocs = 0;
537 1.1 cgd #if JOBS
538 1.1 cgd jp->jobctl = jobctl;
539 1.1 cgd #endif
540 1.1 cgd if (nprocs > 1) {
541 1.1 cgd jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
542 1.1 cgd } else {
543 1.1 cgd jp->ps = &jp->ps0;
544 1.1 cgd }
545 1.1 cgd INTON;
546 1.14 cgd TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
547 1.14 cgd jp - jobtab + 1));
548 1.1 cgd return jp;
549 1.19 christos }
550 1.1 cgd
551 1.1 cgd
552 1.1 cgd /*
553 1.1 cgd * Fork of a subshell. If we are doing job control, give the subshell its
554 1.1 cgd * own process group. Jp is a job structure that the job is to be added to.
555 1.1 cgd * N is the command that will be evaluated by the child. Both jp and n may
556 1.1 cgd * be NULL. The mode parameter can be one of the following:
557 1.1 cgd * FORK_FG - Fork off a foreground process.
558 1.1 cgd * FORK_BG - Fork off a background process.
559 1.1 cgd * FORK_NOJOB - Like FORK_FG, but don't give the process its own
560 1.1 cgd * process group even if job control is on.
561 1.1 cgd *
562 1.1 cgd * When job control is turned off, background processes have their standard
563 1.1 cgd * input redirected to /dev/null (except for the second and later processes
564 1.1 cgd * in a pipeline).
565 1.1 cgd */
566 1.1 cgd
567 1.1 cgd int
568 1.1 cgd forkshell(jp, n, mode)
569 1.1 cgd union node *n;
570 1.1 cgd struct job *jp;
571 1.13 cgd int mode;
572 1.13 cgd {
573 1.1 cgd int pid;
574 1.1 cgd int pgrp;
575 1.1 cgd
576 1.14 cgd TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
577 1.14 cgd mode));
578 1.1 cgd INTOFF;
579 1.1 cgd pid = fork();
580 1.1 cgd if (pid == -1) {
581 1.1 cgd TRACE(("Fork failed, errno=%d\n", errno));
582 1.1 cgd INTON;
583 1.1 cgd error("Cannot fork");
584 1.1 cgd }
585 1.1 cgd if (pid == 0) {
586 1.1 cgd struct job *p;
587 1.1 cgd int wasroot;
588 1.1 cgd int i;
589 1.1 cgd
590 1.1 cgd TRACE(("Child shell %d\n", getpid()));
591 1.1 cgd wasroot = rootshell;
592 1.1 cgd rootshell = 0;
593 1.1 cgd for (i = njobs, p = jobtab ; --i >= 0 ; p++)
594 1.1 cgd if (p->used)
595 1.1 cgd freejob(p);
596 1.1 cgd closescript();
597 1.1 cgd INTON;
598 1.1 cgd clear_traps();
599 1.1 cgd #if JOBS
600 1.1 cgd jobctl = 0; /* do job control only in root shell */
601 1.8 jtc if (wasroot && mode != FORK_NOJOB && mflag) {
602 1.1 cgd if (jp == NULL || jp->nprocs == 0)
603 1.1 cgd pgrp = getpid();
604 1.1 cgd else
605 1.1 cgd pgrp = jp->ps[0].pid;
606 1.12 mycroft setpgid(0, pgrp);
607 1.1 cgd if (mode == FORK_FG) {
608 1.1 cgd /*** this causes superfluous TIOCSPGRPS ***/
609 1.18 mycroft #ifdef OLD_TTY_DRIVER
610 1.1 cgd if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
611 1.19 christos error("TIOCSPGRP failed, errno=%d", errno);
612 1.18 mycroft #else
613 1.18 mycroft if (tcsetpgrp(2, pgrp) < 0)
614 1.19 christos error("tcsetpgrp failed, errno=%d", errno);
615 1.18 mycroft #endif
616 1.1 cgd }
617 1.1 cgd setsignal(SIGTSTP);
618 1.1 cgd setsignal(SIGTTOU);
619 1.1 cgd } else if (mode == FORK_BG) {
620 1.1 cgd ignoresig(SIGINT);
621 1.1 cgd ignoresig(SIGQUIT);
622 1.8 jtc if ((jp == NULL || jp->nprocs == 0) &&
623 1.8 jtc ! fd0_redirected_p ()) {
624 1.1 cgd close(0);
625 1.1 cgd if (open("/dev/null", O_RDONLY) != 0)
626 1.1 cgd error("Can't open /dev/null");
627 1.1 cgd }
628 1.1 cgd }
629 1.1 cgd #else
630 1.1 cgd if (mode == FORK_BG) {
631 1.1 cgd ignoresig(SIGINT);
632 1.1 cgd ignoresig(SIGQUIT);
633 1.8 jtc if ((jp == NULL || jp->nprocs == 0) &&
634 1.8 jtc ! fd0_redirected_p ()) {
635 1.1 cgd close(0);
636 1.1 cgd if (open("/dev/null", O_RDONLY) != 0)
637 1.1 cgd error("Can't open /dev/null");
638 1.1 cgd }
639 1.1 cgd }
640 1.1 cgd #endif
641 1.1 cgd if (wasroot && iflag) {
642 1.1 cgd setsignal(SIGINT);
643 1.1 cgd setsignal(SIGQUIT);
644 1.1 cgd setsignal(SIGTERM);
645 1.1 cgd }
646 1.1 cgd return pid;
647 1.1 cgd }
648 1.8 jtc if (rootshell && mode != FORK_NOJOB && mflag) {
649 1.1 cgd if (jp == NULL || jp->nprocs == 0)
650 1.1 cgd pgrp = pid;
651 1.1 cgd else
652 1.1 cgd pgrp = jp->ps[0].pid;
653 1.12 mycroft setpgid(pid, pgrp);
654 1.1 cgd }
655 1.1 cgd if (mode == FORK_BG)
656 1.1 cgd backgndpid = pid; /* set $! */
657 1.1 cgd if (jp) {
658 1.1 cgd struct procstat *ps = &jp->ps[jp->nprocs++];
659 1.1 cgd ps->pid = pid;
660 1.1 cgd ps->status = -1;
661 1.1 cgd ps->cmd = nullstr;
662 1.1 cgd if (iflag && rootshell && n)
663 1.1 cgd ps->cmd = commandtext(n);
664 1.1 cgd }
665 1.1 cgd INTON;
666 1.1 cgd TRACE(("In parent shell: child = %d\n", pid));
667 1.1 cgd return pid;
668 1.1 cgd }
669 1.1 cgd
670 1.1 cgd
671 1.1 cgd
672 1.1 cgd /*
673 1.1 cgd * Wait for job to finish.
674 1.1 cgd *
675 1.1 cgd * Under job control we have the problem that while a child process is
676 1.1 cgd * running interrupts generated by the user are sent to the child but not
677 1.1 cgd * to the shell. This means that an infinite loop started by an inter-
678 1.1 cgd * active user may be hard to kill. With job control turned off, an
679 1.1 cgd * interactive user may place an interactive program inside a loop. If
680 1.1 cgd * the interactive program catches interrupts, the user doesn't want
681 1.1 cgd * these interrupts to also abort the loop. The approach we take here
682 1.1 cgd * is to have the shell ignore interrupt signals while waiting for a
683 1.1 cgd * forground process to terminate, and then send itself an interrupt
684 1.1 cgd * signal if the child process was terminated by an interrupt signal.
685 1.1 cgd * Unfortunately, some programs want to do a bit of cleanup and then
686 1.1 cgd * exit on interrupt; unless these processes terminate themselves by
687 1.1 cgd * sending a signal to themselves (instead of calling exit) they will
688 1.1 cgd * confuse this approach.
689 1.1 cgd */
690 1.1 cgd
691 1.1 cgd int
692 1.1 cgd waitforjob(jp)
693 1.21 tls struct job *jp;
694 1.1 cgd {
695 1.1 cgd #if JOBS
696 1.10 jtc int mypgrp = getpgrp();
697 1.1 cgd #endif
698 1.1 cgd int status;
699 1.1 cgd int st;
700 1.1 cgd
701 1.1 cgd INTOFF;
702 1.1 cgd TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
703 1.1 cgd while (jp->state == 0) {
704 1.1 cgd dowait(1, jp);
705 1.1 cgd }
706 1.1 cgd #if JOBS
707 1.1 cgd if (jp->jobctl) {
708 1.18 mycroft #ifdef OLD_TTY_DRIVER
709 1.1 cgd if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
710 1.1 cgd error("TIOCSPGRP failed, errno=%d\n", errno);
711 1.18 mycroft #else
712 1.18 mycroft if (tcsetpgrp(2, mypgrp) < 0)
713 1.18 mycroft error("tcsetpgrp failed, errno=%d\n", errno);
714 1.18 mycroft #endif
715 1.1 cgd }
716 1.1 cgd if (jp->state == JOBSTOPPED)
717 1.1 cgd curjob = jp - jobtab + 1;
718 1.1 cgd #endif
719 1.1 cgd status = jp->ps[jp->nprocs - 1].status;
720 1.1 cgd /* convert to 8 bits */
721 1.23 christos if (WIFEXITED(status))
722 1.23 christos st = WEXITSTATUS(status);
723 1.1 cgd #if JOBS
724 1.23 christos else if (WIFSTOPPED(status))
725 1.23 christos st = WSTOPSIG(status) + 128;
726 1.1 cgd #endif
727 1.1 cgd else
728 1.23 christos st = WTERMSIG(status) + 128;
729 1.1 cgd if (! JOBS || jp->state == JOBDONE)
730 1.1 cgd freejob(jp);
731 1.1 cgd CLEAR_PENDING_INT;
732 1.23 christos if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
733 1.1 cgd kill(getpid(), SIGINT);
734 1.1 cgd INTON;
735 1.1 cgd return st;
736 1.1 cgd }
737 1.1 cgd
738 1.1 cgd
739 1.1 cgd
740 1.1 cgd /*
741 1.1 cgd * Wait for a process to terminate.
742 1.1 cgd */
743 1.1 cgd
744 1.1 cgd STATIC int
745 1.1 cgd dowait(block, job)
746 1.13 cgd int block;
747 1.1 cgd struct job *job;
748 1.13 cgd {
749 1.1 cgd int pid;
750 1.1 cgd int status;
751 1.1 cgd struct procstat *sp;
752 1.1 cgd struct job *jp;
753 1.1 cgd struct job *thisjob;
754 1.1 cgd int done;
755 1.1 cgd int stopped;
756 1.1 cgd int core;
757 1.23 christos int sig;
758 1.1 cgd
759 1.1 cgd TRACE(("dowait(%d) called\n", block));
760 1.1 cgd do {
761 1.1 cgd pid = waitproc(block, &status);
762 1.1 cgd TRACE(("wait returns %d, status=%d\n", pid, status));
763 1.1 cgd } while (pid == -1 && errno == EINTR);
764 1.1 cgd if (pid <= 0)
765 1.1 cgd return pid;
766 1.1 cgd INTOFF;
767 1.1 cgd thisjob = NULL;
768 1.1 cgd for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
769 1.1 cgd if (jp->used) {
770 1.1 cgd done = 1;
771 1.1 cgd stopped = 1;
772 1.1 cgd for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
773 1.1 cgd if (sp->pid == -1)
774 1.1 cgd continue;
775 1.1 cgd if (sp->pid == pid) {
776 1.23 christos TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", pid, sp->status, status));
777 1.1 cgd sp->status = status;
778 1.1 cgd thisjob = jp;
779 1.1 cgd }
780 1.1 cgd if (sp->status == -1)
781 1.1 cgd stopped = 0;
782 1.23 christos else if (WIFSTOPPED(sp->status))
783 1.1 cgd done = 0;
784 1.1 cgd }
785 1.1 cgd if (stopped) { /* stopped or done */
786 1.1 cgd int state = done? JOBDONE : JOBSTOPPED;
787 1.1 cgd if (jp->state != state) {
788 1.1 cgd TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
789 1.1 cgd jp->state = state;
790 1.1 cgd #if JOBS
791 1.1 cgd if (done && curjob == jp - jobtab + 1)
792 1.1 cgd curjob = 0; /* no current job */
793 1.1 cgd #endif
794 1.1 cgd }
795 1.1 cgd }
796 1.1 cgd }
797 1.1 cgd }
798 1.1 cgd INTON;
799 1.1 cgd if (! rootshell || ! iflag || (job && thisjob == job)) {
800 1.23 christos core = WCOREDUMP(status);
801 1.1 cgd #if JOBS
802 1.23 christos if (WIFSTOPPED(status)) sig = WSTOPSIG(status);
803 1.23 christos else
804 1.1 cgd #endif
805 1.23 christos if (WIFEXITED(status)) sig = 0;
806 1.23 christos else sig = WTERMSIG(status);
807 1.23 christos
808 1.23 christos if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
809 1.1 cgd if (thisjob != job)
810 1.1 cgd outfmt(out2, "%d: ", pid);
811 1.1 cgd #if JOBS
812 1.23 christos if (sig == SIGTSTP && rootshell && iflag)
813 1.1 cgd outfmt(out2, "%%%d ", job - jobtab + 1);
814 1.1 cgd #endif
815 1.23 christos if (sig < NSIG && sys_siglist[sig])
816 1.23 christos out2str(sys_siglist[sig]);
817 1.1 cgd else
818 1.23 christos outfmt(out2, "Signal %d", sig);
819 1.1 cgd if (core)
820 1.1 cgd out2str(" - core dumped");
821 1.1 cgd out2c('\n');
822 1.1 cgd flushout(&errout);
823 1.1 cgd } else {
824 1.23 christos TRACE(("Not printing status: status=%d, sig=%d\n",
825 1.23 christos status, sig));
826 1.1 cgd }
827 1.1 cgd } else {
828 1.1 cgd TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
829 1.1 cgd if (thisjob)
830 1.1 cgd thisjob->changed = 1;
831 1.1 cgd }
832 1.1 cgd return pid;
833 1.1 cgd }
834 1.1 cgd
835 1.1 cgd
836 1.1 cgd
837 1.1 cgd /*
838 1.1 cgd * Do a wait system call. If job control is compiled in, we accept
839 1.1 cgd * stopped processes. If block is zero, we return a value of zero
840 1.1 cgd * rather than blocking.
841 1.1 cgd *
842 1.1 cgd * System V doesn't have a non-blocking wait system call. It does
843 1.1 cgd * have a SIGCLD signal that is sent to a process when one of it's
844 1.1 cgd * children dies. The obvious way to use SIGCLD would be to install
845 1.1 cgd * a handler for SIGCLD which simply bumped a counter when a SIGCLD
846 1.1 cgd * was received, and have waitproc bump another counter when it got
847 1.1 cgd * the status of a process. Waitproc would then know that a wait
848 1.1 cgd * system call would not block if the two counters were different.
849 1.1 cgd * This approach doesn't work because if a process has children that
850 1.1 cgd * have not been waited for, System V will send it a SIGCLD when it
851 1.1 cgd * installs a signal handler for SIGCLD. What this means is that when
852 1.1 cgd * a child exits, the shell will be sent SIGCLD signals continuously
853 1.1 cgd * until is runs out of stack space, unless it does a wait call before
854 1.1 cgd * restoring the signal handler. The code below takes advantage of
855 1.1 cgd * this (mis)feature by installing a signal handler for SIGCLD and
856 1.1 cgd * then checking to see whether it was called. If there are any
857 1.1 cgd * children to be waited for, it will be.
858 1.1 cgd *
859 1.1 cgd * If neither SYSV nor BSD is defined, we don't implement nonblocking
860 1.1 cgd * waits at all. In this case, the user will not be informed when
861 1.1 cgd * a background process until the next time she runs a real program
862 1.1 cgd * (as opposed to running a builtin command or just typing return),
863 1.1 cgd * and the jobs command may give out of date information.
864 1.1 cgd */
865 1.1 cgd
866 1.1 cgd #ifdef SYSV
867 1.1 cgd STATIC int gotsigchild;
868 1.1 cgd
869 1.1 cgd STATIC int onsigchild() {
870 1.1 cgd gotsigchild = 1;
871 1.1 cgd }
872 1.1 cgd #endif
873 1.1 cgd
874 1.1 cgd
875 1.1 cgd STATIC int
876 1.1 cgd waitproc(block, status)
877 1.13 cgd int block;
878 1.1 cgd int *status;
879 1.13 cgd {
880 1.1 cgd #ifdef BSD
881 1.1 cgd int flags;
882 1.1 cgd
883 1.1 cgd #if JOBS
884 1.1 cgd flags = WUNTRACED;
885 1.1 cgd #else
886 1.1 cgd flags = 0;
887 1.1 cgd #endif
888 1.1 cgd if (block == 0)
889 1.1 cgd flags |= WNOHANG;
890 1.8 jtc return wait3(status, flags, (struct rusage *)NULL);
891 1.1 cgd #else
892 1.1 cgd #ifdef SYSV
893 1.1 cgd int (*save)();
894 1.1 cgd
895 1.1 cgd if (block == 0) {
896 1.1 cgd gotsigchild = 0;
897 1.1 cgd save = signal(SIGCLD, onsigchild);
898 1.1 cgd signal(SIGCLD, save);
899 1.1 cgd if (gotsigchild == 0)
900 1.1 cgd return 0;
901 1.1 cgd }
902 1.1 cgd return wait(status);
903 1.1 cgd #else
904 1.1 cgd if (block == 0)
905 1.1 cgd return 0;
906 1.1 cgd return wait(status);
907 1.1 cgd #endif
908 1.1 cgd #endif
909 1.1 cgd }
910 1.1 cgd
911 1.8 jtc /*
912 1.8 jtc * return 1 if there are stopped jobs, otherwise 0
913 1.8 jtc */
914 1.8 jtc int job_warning = 0;
915 1.8 jtc int
916 1.8 jtc stoppedjobs()
917 1.8 jtc {
918 1.21 tls int jobno;
919 1.21 tls struct job *jp;
920 1.8 jtc
921 1.8 jtc if (job_warning)
922 1.8 jtc return (0);
923 1.8 jtc for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
924 1.8 jtc if (jp->used == 0)
925 1.8 jtc continue;
926 1.8 jtc if (jp->state == JOBSTOPPED) {
927 1.8 jtc out2str("You have stopped jobs.\n");
928 1.8 jtc job_warning = 2;
929 1.8 jtc return (1);
930 1.8 jtc }
931 1.8 jtc }
932 1.1 cgd
933 1.8 jtc return (0);
934 1.8 jtc }
935 1.1 cgd
936 1.1 cgd /*
937 1.1 cgd * Return a string identifying a command (to be printed by the
938 1.1 cgd * jobs command.
939 1.1 cgd */
940 1.1 cgd
941 1.1 cgd STATIC char *cmdnextc;
942 1.1 cgd STATIC int cmdnleft;
943 1.8 jtc #define MAXCMDTEXT 200
944 1.1 cgd
945 1.8 jtc char *
946 1.1 cgd commandtext(n)
947 1.1 cgd union node *n;
948 1.1 cgd {
949 1.1 cgd char *name;
950 1.1 cgd
951 1.8 jtc cmdnextc = name = ckmalloc(MAXCMDTEXT);
952 1.8 jtc cmdnleft = MAXCMDTEXT - 4;
953 1.1 cgd cmdtxt(n);
954 1.1 cgd *cmdnextc = '\0';
955 1.1 cgd return name;
956 1.1 cgd }
957 1.1 cgd
958 1.1 cgd
959 1.1 cgd STATIC void
960 1.1 cgd cmdtxt(n)
961 1.1 cgd union node *n;
962 1.1 cgd {
963 1.1 cgd union node *np;
964 1.1 cgd struct nodelist *lp;
965 1.1 cgd char *p;
966 1.1 cgd int i;
967 1.1 cgd char s[2];
968 1.1 cgd
969 1.8 jtc if (n == NULL)
970 1.8 jtc return;
971 1.1 cgd switch (n->type) {
972 1.1 cgd case NSEMI:
973 1.1 cgd cmdtxt(n->nbinary.ch1);
974 1.1 cgd cmdputs("; ");
975 1.1 cgd cmdtxt(n->nbinary.ch2);
976 1.1 cgd break;
977 1.1 cgd case NAND:
978 1.1 cgd cmdtxt(n->nbinary.ch1);
979 1.1 cgd cmdputs(" && ");
980 1.1 cgd cmdtxt(n->nbinary.ch2);
981 1.1 cgd break;
982 1.1 cgd case NOR:
983 1.1 cgd cmdtxt(n->nbinary.ch1);
984 1.1 cgd cmdputs(" || ");
985 1.1 cgd cmdtxt(n->nbinary.ch2);
986 1.1 cgd break;
987 1.1 cgd case NPIPE:
988 1.1 cgd for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
989 1.1 cgd cmdtxt(lp->n);
990 1.1 cgd if (lp->next)
991 1.1 cgd cmdputs(" | ");
992 1.1 cgd }
993 1.1 cgd break;
994 1.1 cgd case NSUBSHELL:
995 1.1 cgd cmdputs("(");
996 1.1 cgd cmdtxt(n->nredir.n);
997 1.1 cgd cmdputs(")");
998 1.1 cgd break;
999 1.1 cgd case NREDIR:
1000 1.1 cgd case NBACKGND:
1001 1.1 cgd cmdtxt(n->nredir.n);
1002 1.1 cgd break;
1003 1.1 cgd case NIF:
1004 1.1 cgd cmdputs("if ");
1005 1.1 cgd cmdtxt(n->nif.test);
1006 1.1 cgd cmdputs("; then ");
1007 1.1 cgd cmdtxt(n->nif.ifpart);
1008 1.1 cgd cmdputs("...");
1009 1.1 cgd break;
1010 1.1 cgd case NWHILE:
1011 1.1 cgd cmdputs("while ");
1012 1.1 cgd goto until;
1013 1.1 cgd case NUNTIL:
1014 1.1 cgd cmdputs("until ");
1015 1.1 cgd until:
1016 1.1 cgd cmdtxt(n->nbinary.ch1);
1017 1.1 cgd cmdputs("; do ");
1018 1.1 cgd cmdtxt(n->nbinary.ch2);
1019 1.1 cgd cmdputs("; done");
1020 1.1 cgd break;
1021 1.1 cgd case NFOR:
1022 1.1 cgd cmdputs("for ");
1023 1.1 cgd cmdputs(n->nfor.var);
1024 1.1 cgd cmdputs(" in ...");
1025 1.1 cgd break;
1026 1.1 cgd case NCASE:
1027 1.1 cgd cmdputs("case ");
1028 1.1 cgd cmdputs(n->ncase.expr->narg.text);
1029 1.1 cgd cmdputs(" in ...");
1030 1.1 cgd break;
1031 1.1 cgd case NDEFUN:
1032 1.1 cgd cmdputs(n->narg.text);
1033 1.1 cgd cmdputs("() ...");
1034 1.1 cgd break;
1035 1.1 cgd case NCMD:
1036 1.1 cgd for (np = n->ncmd.args ; np ; np = np->narg.next) {
1037 1.1 cgd cmdtxt(np);
1038 1.1 cgd if (np->narg.next)
1039 1.1 cgd cmdputs(" ");
1040 1.1 cgd }
1041 1.1 cgd for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1042 1.1 cgd cmdputs(" ");
1043 1.1 cgd cmdtxt(np);
1044 1.1 cgd }
1045 1.1 cgd break;
1046 1.1 cgd case NARG:
1047 1.1 cgd cmdputs(n->narg.text);
1048 1.1 cgd break;
1049 1.1 cgd case NTO:
1050 1.1 cgd p = ">"; i = 1; goto redir;
1051 1.1 cgd case NAPPEND:
1052 1.1 cgd p = ">>"; i = 1; goto redir;
1053 1.1 cgd case NTOFD:
1054 1.1 cgd p = ">&"; i = 1; goto redir;
1055 1.1 cgd case NFROM:
1056 1.1 cgd p = "<"; i = 0; goto redir;
1057 1.1 cgd case NFROMFD:
1058 1.1 cgd p = "<&"; i = 0; goto redir;
1059 1.1 cgd redir:
1060 1.1 cgd if (n->nfile.fd != i) {
1061 1.1 cgd s[0] = n->nfile.fd + '0';
1062 1.1 cgd s[1] = '\0';
1063 1.1 cgd cmdputs(s);
1064 1.1 cgd }
1065 1.1 cgd cmdputs(p);
1066 1.1 cgd if (n->type == NTOFD || n->type == NFROMFD) {
1067 1.1 cgd s[0] = n->ndup.dupfd + '0';
1068 1.1 cgd s[1] = '\0';
1069 1.1 cgd cmdputs(s);
1070 1.1 cgd } else {
1071 1.1 cgd cmdtxt(n->nfile.fname);
1072 1.1 cgd }
1073 1.1 cgd break;
1074 1.1 cgd case NHERE:
1075 1.1 cgd case NXHERE:
1076 1.1 cgd cmdputs("<<...");
1077 1.1 cgd break;
1078 1.1 cgd default:
1079 1.1 cgd cmdputs("???");
1080 1.1 cgd break;
1081 1.1 cgd }
1082 1.1 cgd }
1083 1.1 cgd
1084 1.1 cgd
1085 1.1 cgd
1086 1.1 cgd STATIC void
1087 1.1 cgd cmdputs(s)
1088 1.1 cgd char *s;
1089 1.1 cgd {
1090 1.21 tls char *p, *q;
1091 1.21 tls char c;
1092 1.1 cgd int subtype = 0;
1093 1.1 cgd
1094 1.1 cgd if (cmdnleft <= 0)
1095 1.1 cgd return;
1096 1.1 cgd p = s;
1097 1.1 cgd q = cmdnextc;
1098 1.1 cgd while ((c = *p++) != '\0') {
1099 1.1 cgd if (c == CTLESC)
1100 1.1 cgd *q++ = *p++;
1101 1.1 cgd else if (c == CTLVAR) {
1102 1.1 cgd *q++ = '$';
1103 1.1 cgd if (--cmdnleft > 0)
1104 1.1 cgd *q++ = '{';
1105 1.1 cgd subtype = *p++;
1106 1.1 cgd } else if (c == '=' && subtype != 0) {
1107 1.1 cgd *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
1108 1.1 cgd subtype = 0;
1109 1.1 cgd } else if (c == CTLENDVAR) {
1110 1.1 cgd *q++ = '}';
1111 1.20 christos } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
1112 1.1 cgd cmdnleft++; /* ignore it */
1113 1.1 cgd else
1114 1.1 cgd *q++ = c;
1115 1.1 cgd if (--cmdnleft <= 0) {
1116 1.1 cgd *q++ = '.';
1117 1.1 cgd *q++ = '.';
1118 1.1 cgd *q++ = '.';
1119 1.1 cgd break;
1120 1.1 cgd }
1121 1.1 cgd }
1122 1.1 cgd cmdnextc = q;
1123 1.1 cgd }
1124