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