jobs.c revision 97816
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3836150Scharnier#if 0
3936150Scharnierstatic char sccsid[] = "@(#)jobs.c	8.5 (Berkeley) 5/4/95";
4036150Scharnier#endif
4136150Scharnierstatic const char rcsid[] =
4250471Speter  "$FreeBSD: head/bin/sh/jobs.c 97816 2002-06-04 13:01:51Z tjr $";
431556Srgrimes#endif /* not lint */
441556Srgrimes
4517987Speter#include <fcntl.h>
4617987Speter#include <signal.h>
4717987Speter#include <errno.h>
4817987Speter#include <unistd.h>
4917987Speter#include <stdlib.h>
5017987Speter#include <sys/param.h>
5117987Speter#ifdef BSD
5217987Speter#include <sys/wait.h>
5317987Speter#include <sys/time.h>
5417987Speter#include <sys/resource.h>
5569793Sobrien#include <paths.h>
5617987Speter#endif
5718018Speter#include <sys/ioctl.h>
5817987Speter
591556Srgrimes#include "shell.h"
601556Srgrimes#if JOBS
6125222Ssteve#if OLD_TTY_DRIVER
621556Srgrimes#include "sgtty.h"
6317987Speter#else
6417987Speter#include <termios.h>
6517987Speter#endif
661556Srgrimes#undef CEOF			/* syntax.h redefines this */
671556Srgrimes#endif
6817987Speter#include "redir.h"
6917987Speter#include "show.h"
701556Srgrimes#include "main.h"
711556Srgrimes#include "parser.h"
721556Srgrimes#include "nodes.h"
731556Srgrimes#include "jobs.h"
741556Srgrimes#include "options.h"
751556Srgrimes#include "trap.h"
761556Srgrimes#include "syntax.h"
771556Srgrimes#include "input.h"
781556Srgrimes#include "output.h"
791556Srgrimes#include "memalloc.h"
801556Srgrimes#include "error.h"
811556Srgrimes#include "mystring.h"
821556Srgrimes
831556Srgrimes
841556Srgrimesstruct job *jobtab;		/* array of jobs */
851556Srgrimesint njobs;			/* size of array */
8628346SsteveMKINIT pid_t backgndpid = -1;	/* pid of last background process */
871556Srgrimes#if JOBS
8897659Stjrstruct job *jobmru;		/* most recently used job list */
891556Srgrimesint initialpgrp;		/* pgrp of shell on invocation */
901556Srgrimes#endif
9138536Scracauerint in_waitcmd = 0;		/* are we in waitcmd()? */
9238950Scracauerint in_dowait = 0;		/* are we in dowait()? */
9338536Scracauervolatile sig_atomic_t breakwaitcmd = 0;	/* should wait be terminated? */
941556Srgrimes
9520425Ssteve#if JOBS
9690111SimpSTATIC void restartjob(struct job *);
9720425Ssteve#endif
9890111SimpSTATIC void freejob(struct job *);
9990111SimpSTATIC struct job *getjob(char *);
10090111SimpSTATIC int dowait(int, struct job *);
10120425Ssteve#if SYSV
10290111SimpSTATIC int onsigchild(void);
10320425Ssteve#endif
10490111SimpSTATIC int waitproc(int, int *);
10590111SimpSTATIC void cmdtxt(union node *);
10690111SimpSTATIC void cmdputs(char *);
10797659Stjr#if JOBS
10897659StjrSTATIC void setcurjob(struct job *);
10997659StjrSTATIC void deljob(struct job *);
11097659StjrSTATIC struct job *getcurjob(struct job *);
11197659Stjr#endif
11297669StjrSTATIC void showjob(struct job *, int, int);
1131556Srgrimes
1141556Srgrimes
1151556Srgrimes/*
1161556Srgrimes * Turn job control on and off.
1171556Srgrimes *
1181556Srgrimes * Note:  This code assumes that the third arg to ioctl is a character
1191556Srgrimes * pointer, which is true on Berkeley systems but not System V.  Since
1201556Srgrimes * System V doesn't have job control yet, this isn't a problem now.
1211556Srgrimes */
1221556Srgrimes
1231556SrgrimesMKINIT int jobctl;
1241556Srgrimes
12520425Ssteve#if JOBS
1261556Srgrimesvoid
12790111Simpsetjobctl(int on)
12817987Speter{
1291556Srgrimes#ifdef OLD_TTY_DRIVER
1301556Srgrimes	int ldisc;
1311556Srgrimes#endif
1321556Srgrimes
1331556Srgrimes	if (on == jobctl || rootshell == 0)
1341556Srgrimes		return;
1351556Srgrimes	if (on) {
1361556Srgrimes		do { /* while we are in the background */
13720425Ssteve#ifdef OLD_TTY_DRIVER
1381556Srgrimes			if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) {
13920425Ssteve#else
14020425Ssteve			initialpgrp = tcgetpgrp(2);
14120425Ssteve			if (initialpgrp < 0) {
14220425Ssteve#endif
1431556Srgrimes				out2str("sh: can't access tty; job control turned off\n");
1441556Srgrimes				mflag = 0;
1451556Srgrimes				return;
1461556Srgrimes			}
1471556Srgrimes			if (initialpgrp == -1)
14817987Speter				initialpgrp = getpgrp();
14917987Speter			else if (initialpgrp != getpgrp()) {
1501556Srgrimes				killpg(initialpgrp, SIGTTIN);
1511556Srgrimes				continue;
1521556Srgrimes			}
1531556Srgrimes		} while (0);
1541556Srgrimes#ifdef OLD_TTY_DRIVER
1551556Srgrimes		if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) {
1561556Srgrimes			out2str("sh: need new tty driver to run job control; job control turned off\n");
1571556Srgrimes			mflag = 0;
1581556Srgrimes			return;
1591556Srgrimes		}
1601556Srgrimes#endif
1611556Srgrimes		setsignal(SIGTSTP);
1621556Srgrimes		setsignal(SIGTTOU);
1631556Srgrimes		setsignal(SIGTTIN);
16417987Speter		setpgid(0, rootpid);
16520425Ssteve#ifdef OLD_TTY_DRIVER
1661556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&rootpid);
16720425Ssteve#else
16820425Ssteve		tcsetpgrp(2, rootpid);
16920425Ssteve#endif
1701556Srgrimes	} else { /* turning job control off */
17117987Speter		setpgid(0, initialpgrp);
17220425Ssteve#ifdef OLD_TTY_DRIVER
1731556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&initialpgrp);
17420425Ssteve#else
17520425Ssteve		tcsetpgrp(2, initialpgrp);
17620425Ssteve#endif
1771556Srgrimes		setsignal(SIGTSTP);
1781556Srgrimes		setsignal(SIGTTOU);
1791556Srgrimes		setsignal(SIGTTIN);
1801556Srgrimes	}
1811556Srgrimes	jobctl = on;
1821556Srgrimes}
18320425Ssteve#endif
1841556Srgrimes
1851556Srgrimes
1861556Srgrimes#ifdef mkinit
18728346SsteveINCLUDE <sys/types.h>
18817987SpeterINCLUDE <stdlib.h>
1891556Srgrimes
1901556SrgrimesSHELLPROC {
1911556Srgrimes	backgndpid = -1;
1921556Srgrimes#if JOBS
1931556Srgrimes	jobctl = 0;
1941556Srgrimes#endif
1951556Srgrimes}
1961556Srgrimes
1971556Srgrimes#endif
1981556Srgrimes
1991556Srgrimes
2001556Srgrimes
2011556Srgrimes#if JOBS
20217987Speterint
20390111Simpfgcmd(int argc __unused, char **argv)
20417987Speter{
2051556Srgrimes	struct job *jp;
2061556Srgrimes	int pgrp;
2071556Srgrimes	int status;
2081556Srgrimes
2091556Srgrimes	jp = getjob(argv[1]);
2101556Srgrimes	if (jp->jobctl == 0)
2111556Srgrimes		error("job not created under job control");
21296933Stjr	out1str(jp->ps[0].cmd);
21396933Stjr	out1c('\n');
21496933Stjr	flushout(&output);
2151556Srgrimes	pgrp = jp->ps[0].pid;
21620425Ssteve#ifdef OLD_TTY_DRIVER
2171556Srgrimes	ioctl(2, TIOCSPGRP, (char *)&pgrp);
21820425Ssteve#else
21920425Ssteve	tcsetpgrp(2, pgrp);
22020425Ssteve#endif
2211556Srgrimes	restartjob(jp);
2221556Srgrimes	INTOFF;
22345916Scracauer	status = waitforjob(jp, (int *)NULL);
2241556Srgrimes	INTON;
2251556Srgrimes	return status;
2261556Srgrimes}
2271556Srgrimes
2281556Srgrimes
22917987Speterint
23090111Simpbgcmd(int argc, char **argv)
23117987Speter{
23296933Stjr	char s[64];
2331556Srgrimes	struct job *jp;
2341556Srgrimes
2351556Srgrimes	do {
2361556Srgrimes		jp = getjob(*++argv);
2371556Srgrimes		if (jp->jobctl == 0)
2381556Srgrimes			error("job not created under job control");
23996933Stjr		if (jp->state == JOBDONE)
24096933Stjr			continue;
2411556Srgrimes		restartjob(jp);
24296933Stjr		fmtstr(s, 64, "[%d] ", jp - jobtab + 1);
24396933Stjr		out1str(s);
24496933Stjr		out1str(jp->ps[0].cmd);
24596933Stjr		out1c('\n');
2461556Srgrimes	} while (--argc > 1);
2471556Srgrimes	return 0;
2481556Srgrimes}
2491556Srgrimes
2501556Srgrimes
2511556SrgrimesSTATIC void
25290111Simprestartjob(struct job *jp)
25317987Speter{
2541556Srgrimes	struct procstat *ps;
2551556Srgrimes	int i;
2561556Srgrimes
2571556Srgrimes	if (jp->state == JOBDONE)
2581556Srgrimes		return;
25997660Stjr	setcurjob(jp);
2601556Srgrimes	INTOFF;
2611556Srgrimes	killpg(jp->ps[0].pid, SIGCONT);
2621556Srgrimes	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
26326104Ssteve		if (WIFSTOPPED(ps->status)) {
2641556Srgrimes			ps->status = -1;
2651556Srgrimes			jp->state = 0;
2661556Srgrimes		}
2671556Srgrimes	}
2681556Srgrimes	INTON;
2691556Srgrimes}
2701556Srgrimes#endif
2711556Srgrimes
2721556Srgrimes
2731556Srgrimesint
27497669Stjrjobscmd(int argc, char *argv[])
27517987Speter{
27697669Stjr	struct job *jp;
27797669Stjr	char *id;
27897669Stjr	int ch, sformat, lformat;
27997669Stjr
28097669Stjr	optind = optreset = 1;
28197669Stjr	sformat = lformat = 0;
28297669Stjr	while ((ch = getopt(argc, argv, "ls")) != -1) {
28397669Stjr		switch (ch) {
28497669Stjr		case 'l':
28597669Stjr			lformat = 1;
28697669Stjr			break;
28797669Stjr		case 's':
28897669Stjr			sformat = 1;
28997669Stjr			break;
29097669Stjr		case '?':
29197669Stjr		default:
29297669Stjr			error("unknown option: -%c", optopt);
29397669Stjr		}
29497669Stjr	}
29597669Stjr	argc -= optind;
29697669Stjr	argv += optind;
29797669Stjr
29897669Stjr	if (argc == 0)
29997669Stjr		showjobs(0, sformat, lformat);
30097669Stjr	else
30197669Stjr		while ((id = *argv++) != NULL)
30297669Stjr			showjob(getjob(id), sformat, lformat);
30397669Stjr
30497669Stjr	return (0);
3051556Srgrimes}
3061556Srgrimes
30797663StjrSTATIC void
30897669Stjrshowjob(struct job *jp, int sformat, int lformat)
30997663Stjr{
31097663Stjr	char s[64];
31197663Stjr	struct procstat *ps;
31297669Stjr	struct job *j;
31397669Stjr	int col, curr, i, jobno, prev, procno;
31497669Stjr	char c;
3151556Srgrimes
31697663Stjr	procno = jp->nprocs;
31797663Stjr	jobno = jp - jobtab + 1;
31897669Stjr	curr = prev = 0;
31997669Stjr#if JOBS
32097669Stjr	if ((j = getcurjob(NULL)) != NULL) {
32197669Stjr		curr = j - jobtab + 1;
32297669Stjr		if ((j = getcurjob(j)) != NULL)
32397669Stjr			prev = j - jobtab + 1;
32497669Stjr	}
32597669Stjr#endif
32697663Stjr	for (ps = jp->ps ; ; ps++) {	/* for each process */
32797669Stjr		if (sformat) {
32897669Stjr			out1fmt("%d\n", ps->pid);
32997669Stjr			goto skip;
33097669Stjr		}
33197669Stjr		if (!lformat && ps != jp->ps)
33297669Stjr			goto skip;
33397669Stjr		if (jobno == curr)
33497669Stjr			c = '+';
33597669Stjr		else if (jobno == prev)
33697669Stjr			c = '-';
33797669Stjr		else
33897669Stjr			c = ' ';
33997663Stjr		if (ps == jp->ps)
34097669Stjr			fmtstr(s, 64, "[%d] %c ", jobno, c);
34197663Stjr		else
34297816Stjr			fmtstr(s, 64, "    %c ", c);
34397663Stjr		out1str(s);
34497663Stjr		col = strlen(s);
34597669Stjr		if (lformat) {
34697669Stjr			fmtstr(s, 64, "%d ", ps->pid);
34797669Stjr			out1str(s);
34897669Stjr			col += strlen(s);
34997669Stjr		}
35097663Stjr		s[0] = '\0';
35197663Stjr		if (ps->status == -1) {
35297669Stjr			strcpy(s, "Running");
35397663Stjr		} else if (WIFEXITED(ps->status)) {
35497663Stjr			fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status));
35597663Stjr		} else {
35697663Stjr#if JOBS
35797663Stjr			if (WIFSTOPPED(ps->status))
35897663Stjr				i = WSTOPSIG(ps->status);
35997663Stjr			else
36097663Stjr#endif
36197663Stjr				i = WTERMSIG(ps->status);
36297663Stjr			if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
36397663Stjr				scopy(sys_siglist[i & 0x7F], s);
36497663Stjr			else
36597663Stjr				fmtstr(s, 64, "Signal %d", i & 0x7F);
36697663Stjr			if (WCOREDUMP(ps->status))
36797663Stjr				strcat(s, " (core dumped)");
36897663Stjr		}
36997663Stjr		out1str(s);
37097663Stjr		col += strlen(s);
37197663Stjr		do {
37297663Stjr			out1c(' ');
37397663Stjr			col++;
37497663Stjr		} while (col < 30);
37597663Stjr		out1str(ps->cmd);
37697663Stjr		out1c('\n');
37797669Stjrskip:		if (--procno <= 0)
37897663Stjr			break;
37997663Stjr	}
38097663Stjr}
38197663Stjr
3821556Srgrimes/*
3831556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
3841556Srgrimes * statuses have changed since the last call to showjobs.
3851556Srgrimes *
3861556Srgrimes * If the shell is interrupted in the process of creating a job, the
3871556Srgrimes * result may be a job structure containing zero processes.  Such structures
3881556Srgrimes * will be freed here.
3891556Srgrimes */
3901556Srgrimes
3911556Srgrimesvoid
39297669Stjrshowjobs(int change, int sformat, int lformat)
39317987Speter{
3941556Srgrimes	int jobno;
3951556Srgrimes	struct job *jp;
3961556Srgrimes
3971556Srgrimes	TRACE(("showjobs(%d) called\n", change));
3981556Srgrimes	while (dowait(0, (struct job *)NULL) > 0);
3991556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
4001556Srgrimes		if (! jp->used)
4011556Srgrimes			continue;
4021556Srgrimes		if (jp->nprocs == 0) {
4031556Srgrimes			freejob(jp);
4041556Srgrimes			continue;
4051556Srgrimes		}
4061556Srgrimes		if (change && ! jp->changed)
4071556Srgrimes			continue;
40897669Stjr		showjob(jp, sformat, lformat);
4091556Srgrimes		jp->changed = 0;
4101556Srgrimes		if (jp->state == JOBDONE) {
4111556Srgrimes			freejob(jp);
4121556Srgrimes		}
4131556Srgrimes	}
4141556Srgrimes}
4151556Srgrimes
4161556Srgrimes
4171556Srgrimes/*
4181556Srgrimes * Mark a job structure as unused.
4191556Srgrimes */
4201556Srgrimes
4211556SrgrimesSTATIC void
42290111Simpfreejob(struct job *jp)
42390111Simp{
4241556Srgrimes	struct procstat *ps;
4251556Srgrimes	int i;
4261556Srgrimes
4271556Srgrimes	INTOFF;
4281556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
4291556Srgrimes		if (ps->cmd != nullstr)
4301556Srgrimes			ckfree(ps->cmd);
4311556Srgrimes	}
4321556Srgrimes	if (jp->ps != &jp->ps0)
4331556Srgrimes		ckfree(jp->ps);
4341556Srgrimes	jp->used = 0;
4351556Srgrimes#if JOBS
43697659Stjr	deljob(jp);
4371556Srgrimes#endif
4381556Srgrimes	INTON;
4391556Srgrimes}
4401556Srgrimes
4411556Srgrimes
4421556Srgrimes
4431556Srgrimesint
44490111Simpwaitcmd(int argc, char **argv)
44517987Speter{
4461556Srgrimes	struct job *job;
44726104Ssteve	int status, retval;
4481556Srgrimes	struct job *jp;
4491556Srgrimes
4501556Srgrimes	if (argc > 1) {
4511556Srgrimes		job = getjob(argv[1]);
4521556Srgrimes	} else {
4531556Srgrimes		job = NULL;
4541556Srgrimes	}
45538536Scracauer
45638536Scracauer	/*
45738536Scracauer	 * Loop until a process is terminated or stopped, or a SIGINT is
45838536Scracauer	 * received.
45938536Scracauer	 */
46038536Scracauer
46138521Scracauer	in_waitcmd++;
46238536Scracauer	do {
4631556Srgrimes		if (job != NULL) {
4641556Srgrimes			if (job->state) {
4651556Srgrimes				status = job->ps[job->nprocs - 1].status;
46626104Ssteve				if (WIFEXITED(status))
46726104Ssteve					retval = WEXITSTATUS(status);
4681556Srgrimes#if JOBS
46926104Ssteve				else if (WIFSTOPPED(status))
47026104Ssteve					retval = WSTOPSIG(status) + 128;
4711556Srgrimes#endif
4721556Srgrimes				else
47326104Ssteve					retval = WTERMSIG(status) + 128;
4741556Srgrimes				if (! iflag)
4751556Srgrimes					freejob(job);
47638536Scracauer				in_waitcmd--;
47726104Ssteve				return retval;
4781556Srgrimes			}
4791556Srgrimes		} else {
4801556Srgrimes			for (jp = jobtab ; ; jp++) {
4811556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
48238536Scracauer					in_waitcmd--;
4831556Srgrimes					return 0;
4841556Srgrimes				}
4851556Srgrimes				if (jp->used && jp->state == 0)
4861556Srgrimes					break;
4871556Srgrimes			}
4881556Srgrimes		}
48938521Scracauer	} while (dowait(1, (struct job *)NULL) != -1);
49038521Scracauer	in_waitcmd--;
49138521Scracauer
49238521Scracauer	return 0;
4931556Srgrimes}
4941556Srgrimes
4951556Srgrimes
4961556Srgrimes
49717987Speterint
49890111Simpjobidcmd(int argc __unused, char **argv)
49917987Speter{
5001556Srgrimes	struct job *jp;
5011556Srgrimes	int i;
5021556Srgrimes
5031556Srgrimes	jp = getjob(argv[1]);
5041556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
5051556Srgrimes		out1fmt("%d", jp->ps[i].pid);
5061556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
5071556Srgrimes	}
5081556Srgrimes	return 0;
5091556Srgrimes}
5101556Srgrimes
5111556Srgrimes
5121556Srgrimes
5131556Srgrimes/*
5141556Srgrimes * Convert a job name to a job structure.
5151556Srgrimes */
5161556Srgrimes
5171556SrgrimesSTATIC struct job *
51890111Simpgetjob(char *name)
51990111Simp{
5201556Srgrimes	int jobno;
52197688Stjr	struct job *found, *jp;
5221556Srgrimes	int pid;
5231556Srgrimes	int i;
5241556Srgrimes
5251556Srgrimes	if (name == NULL) {
5261556Srgrimes#if JOBS
52797659Stjrcurrentjob:	if ((jp = getcurjob(NULL)) == NULL)
5281556Srgrimes			error("No current job");
52997659Stjr		return (jp);
5301556Srgrimes#else
5311556Srgrimes		error("No current job");
5321556Srgrimes#endif
5331556Srgrimes	} else if (name[0] == '%') {
5341556Srgrimes		if (is_digit(name[1])) {
5351556Srgrimes			jobno = number(name + 1);
5361556Srgrimes			if (jobno > 0 && jobno <= njobs
5371556Srgrimes			 && jobtab[jobno - 1].used != 0)
5381556Srgrimes				return &jobtab[jobno - 1];
5391556Srgrimes#if JOBS
5401556Srgrimes		} else if (name[1] == '%' && name[2] == '\0') {
5411556Srgrimes			goto currentjob;
54297688Stjr		} else if (name[1] == '+' && name[2] == '\0') {
54397688Stjr			goto currentjob;
54497688Stjr		} else if (name[1] == '-' && name[2] == '\0') {
54597688Stjr			if ((jp = getcurjob(NULL)) == NULL ||
54697688Stjr			    (jp = getcurjob(jp)) == NULL)
54797688Stjr				error("No previous job");
54897688Stjr			return (jp);
5491556Srgrimes#endif
55097688Stjr		} else if (name[1] == '?') {
55197688Stjr			found = NULL;
55297688Stjr			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
55397688Stjr				if (jp->used && jp->nprocs > 0
55497688Stjr				 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
55597688Stjr					if (found)
55697688Stjr						error("%s: ambiguous", name);
55797688Stjr					found = jp;
55897688Stjr				}
55997688Stjr			}
56097688Stjr			if (found != NULL)
56197688Stjr				return (found);
5621556Srgrimes		} else {
56397688Stjr			found = NULL;
5641556Srgrimes			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
5651556Srgrimes				if (jp->used && jp->nprocs > 0
5661556Srgrimes				 && prefix(name + 1, jp->ps[0].cmd)) {
5671556Srgrimes					if (found)
5681556Srgrimes						error("%s: ambiguous", name);
5691556Srgrimes					found = jp;
5701556Srgrimes				}
5711556Srgrimes			}
5721556Srgrimes			if (found)
5731556Srgrimes				return found;
5741556Srgrimes		}
5751556Srgrimes	} else if (is_number(name)) {
5761556Srgrimes		pid = number(name);
5771556Srgrimes		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
5781556Srgrimes			if (jp->used && jp->nprocs > 0
5791556Srgrimes			 && jp->ps[jp->nprocs - 1].pid == pid)
5801556Srgrimes				return jp;
5811556Srgrimes		}
5821556Srgrimes	}
5831556Srgrimes	error("No such job: %s", name);
58417987Speter	/*NOTREACHED*/
58517987Speter	return NULL;
5861556Srgrimes}
5871556Srgrimes
5881556Srgrimes
5891556Srgrimes
5901556Srgrimes/*
5911556Srgrimes * Return a new job structure,
5921556Srgrimes */
5931556Srgrimes
5941556Srgrimesstruct job *
59590111Simpmakejob(union node *node __unused, int nprocs)
59617987Speter{
5971556Srgrimes	int i;
5981556Srgrimes	struct job *jp;
5991556Srgrimes
6001556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
6011556Srgrimes		if (--i < 0) {
6021556Srgrimes			INTOFF;
6031556Srgrimes			if (njobs == 0) {
6041556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
60597664Stjr#if JOBS
60697659Stjr				jobmru = NULL;
60797664Stjr#endif
6081556Srgrimes			} else {
6091556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
61017987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
61197659Stjr#if JOBS
61297659Stjr				/* Relocate `next' pointers and list head */
61397659Stjr				jobmru = &jp[jobmru - jobtab];
61497659Stjr				for (i = 0; i < njobs; i++)
61597659Stjr					if (jp[i].next != NULL)
61697659Stjr						jp[i].next = &jp[jp[i].next -
61797659Stjr						    jobtab];
61897659Stjr#endif
61920425Ssteve				/* Relocate `ps' pointers */
62020425Ssteve				for (i = 0; i < njobs; i++)
62120425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
62220425Ssteve						jp[i].ps = &jp[i].ps0;
6231556Srgrimes				ckfree(jobtab);
6241556Srgrimes				jobtab = jp;
6251556Srgrimes			}
6261556Srgrimes			jp = jobtab + njobs;
6271556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
6281556Srgrimes			INTON;
6291556Srgrimes			break;
6301556Srgrimes		}
6311556Srgrimes		if (jp->used == 0)
6321556Srgrimes			break;
6331556Srgrimes	}
6341556Srgrimes	INTOFF;
6351556Srgrimes	jp->state = 0;
6361556Srgrimes	jp->used = 1;
6371556Srgrimes	jp->changed = 0;
6381556Srgrimes	jp->nprocs = 0;
6391556Srgrimes#if JOBS
6401556Srgrimes	jp->jobctl = jobctl;
64197659Stjr	jp->next = NULL;
6421556Srgrimes#endif
6431556Srgrimes	if (nprocs > 1) {
6441556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
6451556Srgrimes	} else {
6461556Srgrimes		jp->ps = &jp->ps0;
6471556Srgrimes	}
6481556Srgrimes	INTON;
64917987Speter	TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
65017987Speter	    jp - jobtab + 1));
6511556Srgrimes	return jp;
6528855Srgrimes}
6531556Srgrimes
65497659Stjr#if JOBS
65597659StjrSTATIC void
65697659Stjrsetcurjob(struct job *cj)
65797659Stjr{
65897659Stjr	struct job *jp, *prev;
6591556Srgrimes
66097659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
66197659Stjr		if (jp == cj) {
66297659Stjr			if (prev != NULL)
66397659Stjr				prev->next = jp->next;
66497659Stjr			else
66597659Stjr				jobmru = jp->next;
66697659Stjr			jp->next = jobmru;
66797659Stjr			jobmru = cj;
66897659Stjr			return;
66997659Stjr		}
67097659Stjr	}
67197659Stjr	cj->next = jobmru;
67297659Stjr	jobmru = cj;
67397659Stjr}
67497659Stjr
67597659StjrSTATIC void
67697659Stjrdeljob(struct job *j)
67797659Stjr{
67897659Stjr	struct job *jp, *prev;
67997659Stjr
68097659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
68197659Stjr		if (jp == j) {
68297659Stjr			if (prev != NULL)
68397659Stjr				prev->next = jp->next;
68497659Stjr			else
68597659Stjr				jobmru = jp->next;
68697659Stjr			return;
68797659Stjr		}
68897659Stjr	}
68997659Stjr}
69097659Stjr
6911556Srgrimes/*
69297659Stjr * Return the most recently used job that isn't `nj', and preferably one
69397659Stjr * that is stopped.
69497659Stjr */
69597659StjrSTATIC struct job *
69697659Stjrgetcurjob(struct job *nj)
69797659Stjr{
69897659Stjr	struct job *jp;
69997659Stjr
70097659Stjr	/* Try to find a stopped one.. */
70197659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
70297659Stjr		if (jp->used && jp != nj && jp->state == JOBSTOPPED)
70397659Stjr			return (jp);
70497659Stjr	/* Otherwise the most recently used job that isn't `nj' */
70597659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
70697659Stjr		if (jp->used && jp != nj)
70797659Stjr			return (jp);
70897659Stjr
70997659Stjr	return (NULL);
71097659Stjr}
71197659Stjr
71297659Stjr#endif
71397659Stjr
71497659Stjr/*
7151556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
7161556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
7171556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
7181556Srgrimes * be NULL.  The mode parameter can be one of the following:
7191556Srgrimes *	FORK_FG - Fork off a foreground process.
7201556Srgrimes *	FORK_BG - Fork off a background process.
7211556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
7221556Srgrimes *		     process group even if job control is on.
7231556Srgrimes *
7241556Srgrimes * When job control is turned off, background processes have their standard
7251556Srgrimes * input redirected to /dev/null (except for the second and later processes
7261556Srgrimes * in a pipeline).
7271556Srgrimes */
7281556Srgrimes
7291556Srgrimesint
73090111Simpforkshell(struct job *jp, union node *n, int mode)
73117987Speter{
7321556Srgrimes	int pid;
7331556Srgrimes	int pgrp;
7341556Srgrimes
73517987Speter	TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
73617987Speter	    mode));
7371556Srgrimes	INTOFF;
7381556Srgrimes	pid = fork();
7391556Srgrimes	if (pid == -1) {
7401556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
7411556Srgrimes		INTON;
74253891Scracauer		error("Cannot fork: %s", strerror(errno));
7431556Srgrimes	}
7441556Srgrimes	if (pid == 0) {
7451556Srgrimes		struct job *p;
7461556Srgrimes		int wasroot;
7471556Srgrimes		int i;
7481556Srgrimes
7491556Srgrimes		TRACE(("Child shell %d\n", getpid()));
7501556Srgrimes		wasroot = rootshell;
7511556Srgrimes		rootshell = 0;
7521556Srgrimes		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
7531556Srgrimes			if (p->used)
7541556Srgrimes				freejob(p);
7551556Srgrimes		closescript();
7561556Srgrimes		INTON;
7571556Srgrimes		clear_traps();
7581556Srgrimes#if JOBS
7591556Srgrimes		jobctl = 0;		/* do job control only in root shell */
7601556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
7611556Srgrimes			if (jp == NULL || jp->nprocs == 0)
7621556Srgrimes				pgrp = getpid();
7631556Srgrimes			else
7641556Srgrimes				pgrp = jp->ps[0].pid;
76521352Ssteve			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
7661556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
76720425Ssteve#ifdef OLD_TTY_DRIVER
7681556Srgrimes				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
76918016Speter					error("TIOCSPGRP failed, errno=%d", errno);
77020425Ssteve#else
77120425Ssteve				if (tcsetpgrp(2, pgrp) < 0)
77220425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
77320425Ssteve#endif
7741556Srgrimes			}
7751556Srgrimes			setsignal(SIGTSTP);
7761556Srgrimes			setsignal(SIGTTOU);
7771556Srgrimes		} else if (mode == FORK_BG) {
7781556Srgrimes			ignoresig(SIGINT);
7791556Srgrimes			ignoresig(SIGQUIT);
7801556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
7811556Srgrimes			    ! fd0_redirected_p ()) {
7821556Srgrimes				close(0);
78369793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
78469793Sobrien					error("Can't open %s: %s",
78569793Sobrien					    _PATH_DEVNULL, strerror(errno));
7861556Srgrimes			}
7871556Srgrimes		}
7881556Srgrimes#else
7891556Srgrimes		if (mode == FORK_BG) {
7901556Srgrimes			ignoresig(SIGINT);
7911556Srgrimes			ignoresig(SIGQUIT);
7921556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
7931556Srgrimes			    ! fd0_redirected_p ()) {
7941556Srgrimes				close(0);
79569793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
79669793Sobrien					error("Can't open %s: %s",
79769793Sobrien					    _PATH_DEVNULL, strerror(errno));
7981556Srgrimes			}
7991556Srgrimes		}
8001556Srgrimes#endif
8011556Srgrimes		if (wasroot && iflag) {
8021556Srgrimes			setsignal(SIGINT);
8031556Srgrimes			setsignal(SIGQUIT);
8041556Srgrimes			setsignal(SIGTERM);
8051556Srgrimes		}
8061556Srgrimes		return pid;
8071556Srgrimes	}
8081556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
8091556Srgrimes		if (jp == NULL || jp->nprocs == 0)
8101556Srgrimes			pgrp = pid;
8111556Srgrimes		else
8121556Srgrimes			pgrp = jp->ps[0].pid;
81317987Speter		setpgid(pid, pgrp);
8141556Srgrimes	}
8151556Srgrimes	if (mode == FORK_BG)
8161556Srgrimes		backgndpid = pid;		/* set $! */
8171556Srgrimes	if (jp) {
8181556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
8191556Srgrimes		ps->pid = pid;
8201556Srgrimes		ps->status = -1;
8211556Srgrimes		ps->cmd = nullstr;
8221556Srgrimes		if (iflag && rootshell && n)
8231556Srgrimes			ps->cmd = commandtext(n);
82497659Stjr#if JOBS
82597659Stjr		setcurjob(jp);
82697659Stjr#endif
8271556Srgrimes	}
8281556Srgrimes	INTON;
8291556Srgrimes	TRACE(("In parent shell:  child = %d\n", pid));
8301556Srgrimes	return pid;
8311556Srgrimes}
8321556Srgrimes
8331556Srgrimes
8341556Srgrimes
8351556Srgrimes/*
8361556Srgrimes * Wait for job to finish.
8371556Srgrimes *
8381556Srgrimes * Under job control we have the problem that while a child process is
8391556Srgrimes * running interrupts generated by the user are sent to the child but not
8401556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
8411556Srgrimes * active user may be hard to kill.  With job control turned off, an
8421556Srgrimes * interactive user may place an interactive program inside a loop.  If
8431556Srgrimes * the interactive program catches interrupts, the user doesn't want
8441556Srgrimes * these interrupts to also abort the loop.  The approach we take here
8451556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
84646684Skris * foreground process to terminate, and then send itself an interrupt
8471556Srgrimes * signal if the child process was terminated by an interrupt signal.
8481556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
8491556Srgrimes * exit on interrupt; unless these processes terminate themselves by
8501556Srgrimes * sending a signal to themselves (instead of calling exit) they will
8511556Srgrimes * confuse this approach.
8521556Srgrimes */
8531556Srgrimes
8541556Srgrimesint
85590111Simpwaitforjob(struct job *jp, int *origstatus)
85645916Scracauer{
8571556Srgrimes#if JOBS
85817987Speter	int mypgrp = getpgrp();
8591556Srgrimes#endif
8601556Srgrimes	int status;
8611556Srgrimes	int st;
8621556Srgrimes
8631556Srgrimes	INTOFF;
8641556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
86538950Scracauer	while (jp->state == 0)
86638950Scracauer		if (dowait(1, jp) == -1)
86738950Scracauer			dotrap();
8681556Srgrimes#if JOBS
8691556Srgrimes	if (jp->jobctl) {
87020425Ssteve#ifdef OLD_TTY_DRIVER
8711556Srgrimes		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
87220425Ssteve			error("TIOCSPGRP failed, errno=%d\n", errno);
87320425Ssteve#else
87420425Ssteve		if (tcsetpgrp(2, mypgrp) < 0)
87520425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
87620425Ssteve#endif
8771556Srgrimes	}
8781556Srgrimes	if (jp->state == JOBSTOPPED)
87997659Stjr		setcurjob(jp);
8801556Srgrimes#endif
8811556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
88245916Scracauer	if (origstatus != NULL)
88345916Scracauer		*origstatus = status;
8841556Srgrimes	/* convert to 8 bits */
88526104Ssteve	if (WIFEXITED(status))
88626104Ssteve		st = WEXITSTATUS(status);
8871556Srgrimes#if JOBS
88826104Ssteve	else if (WIFSTOPPED(status))
88926104Ssteve		st = WSTOPSIG(status) + 128;
8901556Srgrimes#endif
8911556Srgrimes	else
89226104Ssteve		st = WTERMSIG(status) + 128;
8931556Srgrimes	if (! JOBS || jp->state == JOBDONE)
8941556Srgrimes		freejob(jp);
89538521Scracauer	if (int_pending()) {
89638521Scracauer		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
89738521Scracauer			kill(getpid(), SIGINT);
89838521Scracauer		else
89938521Scracauer			CLEAR_PENDING_INT;
90038521Scracauer	}
9011556Srgrimes	INTON;
9021556Srgrimes	return st;
9031556Srgrimes}
9041556Srgrimes
9051556Srgrimes
9061556Srgrimes
9071556Srgrimes/*
9081556Srgrimes * Wait for a process to terminate.
9091556Srgrimes */
9101556Srgrimes
9111556SrgrimesSTATIC int
91290111Simpdowait(int block, struct job *job)
91317987Speter{
9141556Srgrimes	int pid;
9151556Srgrimes	int status;
9161556Srgrimes	struct procstat *sp;
9171556Srgrimes	struct job *jp;
9181556Srgrimes	struct job *thisjob;
9191556Srgrimes	int done;
9201556Srgrimes	int stopped;
9211556Srgrimes	int core;
92226104Ssteve	int sig;
9231556Srgrimes
92438950Scracauer	in_dowait++;
9251556Srgrimes	TRACE(("dowait(%d) called\n", block));
9261556Srgrimes	do {
9271556Srgrimes		pid = waitproc(block, &status);
9281556Srgrimes		TRACE(("wait returns %d, status=%d\n", pid, status));
92972086Scracauer	} while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
93072086Scracauer	    (WIFSTOPPED(status) && !iflag));
93138950Scracauer	in_dowait--;
93238521Scracauer	if (breakwaitcmd != 0) {
93338521Scracauer		breakwaitcmd = 0;
93438521Scracauer		return -1;
93538521Scracauer	}
9361556Srgrimes	if (pid <= 0)
9371556Srgrimes		return pid;
9381556Srgrimes	INTOFF;
9391556Srgrimes	thisjob = NULL;
9401556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
9411556Srgrimes		if (jp->used) {
9421556Srgrimes			done = 1;
9431556Srgrimes			stopped = 1;
9441556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
9451556Srgrimes				if (sp->pid == -1)
9461556Srgrimes					continue;
9471556Srgrimes				if (sp->pid == pid) {
94826104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
94926104Ssteve						   pid, sp->status, status));
9501556Srgrimes					sp->status = status;
9511556Srgrimes					thisjob = jp;
9521556Srgrimes				}
9531556Srgrimes				if (sp->status == -1)
9541556Srgrimes					stopped = 0;
95526104Ssteve				else if (WIFSTOPPED(sp->status))
9561556Srgrimes					done = 0;
9571556Srgrimes			}
9581556Srgrimes			if (stopped) {		/* stopped or done */
9591556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
9601556Srgrimes				if (jp->state != state) {
9611556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
9621556Srgrimes					jp->state = state;
9631556Srgrimes#if JOBS
96497659Stjr					if (done)
96597659Stjr						deljob(jp);
9661556Srgrimes#endif
9671556Srgrimes				}
9681556Srgrimes			}
9691556Srgrimes		}
9701556Srgrimes	}
9711556Srgrimes	INTON;
9721556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
9731556Srgrimes#if JOBS
97426104Ssteve		if (WIFSTOPPED(status))
97526104Ssteve			sig = WSTOPSIG(status);
97626104Ssteve		else
9771556Srgrimes#endif
97897663Stjr		{
97926104Ssteve			if (WIFEXITED(status))
98026104Ssteve				sig = 0;
98126104Ssteve			else
98226104Ssteve				sig = WTERMSIG(status);
9831556Srgrimes		}
98497663Stjr		if (sig != 0 && sig != SIGINT && sig != SIGPIPE)
98597669Stjr			showjob(thisjob, 0, 0);
9861556Srgrimes	} else {
9871556Srgrimes		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
9881556Srgrimes		if (thisjob)
9891556Srgrimes			thisjob->changed = 1;
9901556Srgrimes	}
9911556Srgrimes	return pid;
9921556Srgrimes}
9931556Srgrimes
9941556Srgrimes
9951556Srgrimes
9961556Srgrimes/*
9971556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
9981556Srgrimes * stopped processes.  If block is zero, we return a value of zero
9991556Srgrimes * rather than blocking.
10001556Srgrimes *
10011556Srgrimes * System V doesn't have a non-blocking wait system call.  It does
10021556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's
10031556Srgrimes * children dies.  The obvious way to use SIGCLD would be to install
10041556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD
10051556Srgrimes * was received, and have waitproc bump another counter when it got
10061556Srgrimes * the status of a process.  Waitproc would then know that a wait
10071556Srgrimes * system call would not block if the two counters were different.
10081556Srgrimes * This approach doesn't work because if a process has children that
10091556Srgrimes * have not been waited for, System V will send it a SIGCLD when it
10101556Srgrimes * installs a signal handler for SIGCLD.  What this means is that when
10111556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously
10121556Srgrimes * until is runs out of stack space, unless it does a wait call before
10131556Srgrimes * restoring the signal handler.  The code below takes advantage of
10141556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and
10151556Srgrimes * then checking to see whether it was called.  If there are any
10161556Srgrimes * children to be waited for, it will be.
10171556Srgrimes *
10181556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking
10191556Srgrimes * waits at all.  In this case, the user will not be informed when
10201556Srgrimes * a background process until the next time she runs a real program
10211556Srgrimes * (as opposed to running a builtin command or just typing return),
10221556Srgrimes * and the jobs command may give out of date information.
10231556Srgrimes */
10241556Srgrimes
10251556Srgrimes#ifdef SYSV
102638521ScracauerSTATIC sig_atomic_t gotsigchild;
10271556Srgrimes
10281556SrgrimesSTATIC int onsigchild() {
10291556Srgrimes	gotsigchild = 1;
10301556Srgrimes}
10311556Srgrimes#endif
10321556Srgrimes
10331556Srgrimes
10341556SrgrimesSTATIC int
103590111Simpwaitproc(int block, int *status)
103617987Speter{
10371556Srgrimes#ifdef BSD
10381556Srgrimes	int flags;
10391556Srgrimes
10401556Srgrimes#if JOBS
10411556Srgrimes	flags = WUNTRACED;
10421556Srgrimes#else
10431556Srgrimes	flags = 0;
10441556Srgrimes#endif
10451556Srgrimes	if (block == 0)
10461556Srgrimes		flags |= WNOHANG;
10471556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
10481556Srgrimes#else
10491556Srgrimes#ifdef SYSV
10501556Srgrimes	int (*save)();
10511556Srgrimes
10521556Srgrimes	if (block == 0) {
10531556Srgrimes		gotsigchild = 0;
10541556Srgrimes		save = signal(SIGCLD, onsigchild);
10551556Srgrimes		signal(SIGCLD, save);
10561556Srgrimes		if (gotsigchild == 0)
10571556Srgrimes			return 0;
10581556Srgrimes	}
10591556Srgrimes	return wait(status);
10601556Srgrimes#else
10611556Srgrimes	if (block == 0)
10621556Srgrimes		return 0;
10631556Srgrimes	return wait(status);
10641556Srgrimes#endif
10651556Srgrimes#endif
10661556Srgrimes}
10671556Srgrimes
10681556Srgrimes/*
10691556Srgrimes * return 1 if there are stopped jobs, otherwise 0
10701556Srgrimes */
10711556Srgrimesint job_warning = 0;
10721556Srgrimesint
107390111Simpstoppedjobs(void)
10741556Srgrimes{
107525222Ssteve	int jobno;
107625222Ssteve	struct job *jp;
10771556Srgrimes
10781556Srgrimes	if (job_warning)
10791556Srgrimes		return (0);
10801556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
10811556Srgrimes		if (jp->used == 0)
10821556Srgrimes			continue;
10831556Srgrimes		if (jp->state == JOBSTOPPED) {
10841556Srgrimes			out2str("You have stopped jobs.\n");
10851556Srgrimes			job_warning = 2;
10861556Srgrimes			return (1);
10871556Srgrimes		}
10881556Srgrimes	}
10891556Srgrimes
10901556Srgrimes	return (0);
10911556Srgrimes}
10921556Srgrimes
10931556Srgrimes/*
10941556Srgrimes * Return a string identifying a command (to be printed by the
10951556Srgrimes * jobs command.
10961556Srgrimes */
10971556Srgrimes
10981556SrgrimesSTATIC char *cmdnextc;
10991556SrgrimesSTATIC int cmdnleft;
11001556Srgrimes#define MAXCMDTEXT	200
11011556Srgrimes
11021556Srgrimeschar *
110390111Simpcommandtext(union node *n)
110490111Simp{
11051556Srgrimes	char *name;
11061556Srgrimes
11071556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
11081556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
11091556Srgrimes	cmdtxt(n);
11101556Srgrimes	*cmdnextc = '\0';
11111556Srgrimes	return name;
11121556Srgrimes}
11131556Srgrimes
11141556Srgrimes
11151556SrgrimesSTATIC void
111690111Simpcmdtxt(union node *n)
111790111Simp{
11181556Srgrimes	union node *np;
11191556Srgrimes	struct nodelist *lp;
11201556Srgrimes	char *p;
11211556Srgrimes	int i;
11221556Srgrimes	char s[2];
11231556Srgrimes
11241556Srgrimes	if (n == NULL)
11251556Srgrimes		return;
11261556Srgrimes	switch (n->type) {
11271556Srgrimes	case NSEMI:
11281556Srgrimes		cmdtxt(n->nbinary.ch1);
11291556Srgrimes		cmdputs("; ");
11301556Srgrimes		cmdtxt(n->nbinary.ch2);
11311556Srgrimes		break;
11321556Srgrimes	case NAND:
11331556Srgrimes		cmdtxt(n->nbinary.ch1);
11341556Srgrimes		cmdputs(" && ");
11351556Srgrimes		cmdtxt(n->nbinary.ch2);
11361556Srgrimes		break;
11371556Srgrimes	case NOR:
11381556Srgrimes		cmdtxt(n->nbinary.ch1);
11391556Srgrimes		cmdputs(" || ");
11401556Srgrimes		cmdtxt(n->nbinary.ch2);
11411556Srgrimes		break;
11421556Srgrimes	case NPIPE:
11431556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
11441556Srgrimes			cmdtxt(lp->n);
11451556Srgrimes			if (lp->next)
11461556Srgrimes				cmdputs(" | ");
11471556Srgrimes		}
11481556Srgrimes		break;
11491556Srgrimes	case NSUBSHELL:
11501556Srgrimes		cmdputs("(");
11511556Srgrimes		cmdtxt(n->nredir.n);
11521556Srgrimes		cmdputs(")");
11531556Srgrimes		break;
11541556Srgrimes	case NREDIR:
11551556Srgrimes	case NBACKGND:
11561556Srgrimes		cmdtxt(n->nredir.n);
11571556Srgrimes		break;
11581556Srgrimes	case NIF:
11591556Srgrimes		cmdputs("if ");
11601556Srgrimes		cmdtxt(n->nif.test);
11611556Srgrimes		cmdputs("; then ");
11621556Srgrimes		cmdtxt(n->nif.ifpart);
11631556Srgrimes		cmdputs("...");
11641556Srgrimes		break;
11651556Srgrimes	case NWHILE:
11661556Srgrimes		cmdputs("while ");
11671556Srgrimes		goto until;
11681556Srgrimes	case NUNTIL:
11691556Srgrimes		cmdputs("until ");
11701556Srgrimesuntil:
11711556Srgrimes		cmdtxt(n->nbinary.ch1);
11721556Srgrimes		cmdputs("; do ");
11731556Srgrimes		cmdtxt(n->nbinary.ch2);
11741556Srgrimes		cmdputs("; done");
11751556Srgrimes		break;
11761556Srgrimes	case NFOR:
11771556Srgrimes		cmdputs("for ");
11781556Srgrimes		cmdputs(n->nfor.var);
11791556Srgrimes		cmdputs(" in ...");
11801556Srgrimes		break;
11811556Srgrimes	case NCASE:
11821556Srgrimes		cmdputs("case ");
11831556Srgrimes		cmdputs(n->ncase.expr->narg.text);
11841556Srgrimes		cmdputs(" in ...");
11851556Srgrimes		break;
11861556Srgrimes	case NDEFUN:
11871556Srgrimes		cmdputs(n->narg.text);
11881556Srgrimes		cmdputs("() ...");
11891556Srgrimes		break;
11901556Srgrimes	case NCMD:
11911556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
11921556Srgrimes			cmdtxt(np);
11931556Srgrimes			if (np->narg.next)
11941556Srgrimes				cmdputs(" ");
11951556Srgrimes		}
11961556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
11971556Srgrimes			cmdputs(" ");
11981556Srgrimes			cmdtxt(np);
11991556Srgrimes		}
12001556Srgrimes		break;
12011556Srgrimes	case NARG:
12021556Srgrimes		cmdputs(n->narg.text);
12031556Srgrimes		break;
12041556Srgrimes	case NTO:
12051556Srgrimes		p = ">";  i = 1;  goto redir;
12061556Srgrimes	case NAPPEND:
12071556Srgrimes		p = ">>";  i = 1;  goto redir;
12081556Srgrimes	case NTOFD:
12091556Srgrimes		p = ">&";  i = 1;  goto redir;
121096922Stjr	case NCLOBBER:
121196922Stjr		p = ">|"; i = 1; goto redir;
12121556Srgrimes	case NFROM:
12131556Srgrimes		p = "<";  i = 0;  goto redir;
121466612Sbrian	case NFROMTO:
121566612Sbrian		p = "<>";  i = 0;  goto redir;
12161556Srgrimes	case NFROMFD:
12171556Srgrimes		p = "<&";  i = 0;  goto redir;
12181556Srgrimesredir:
12191556Srgrimes		if (n->nfile.fd != i) {
12201556Srgrimes			s[0] = n->nfile.fd + '0';
12211556Srgrimes			s[1] = '\0';
12221556Srgrimes			cmdputs(s);
12231556Srgrimes		}
12241556Srgrimes		cmdputs(p);
12251556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
12261556Srgrimes			s[0] = n->ndup.dupfd + '0';
12271556Srgrimes			s[1] = '\0';
12281556Srgrimes			cmdputs(s);
12291556Srgrimes		} else {
12301556Srgrimes			cmdtxt(n->nfile.fname);
12311556Srgrimes		}
12321556Srgrimes		break;
12331556Srgrimes	case NHERE:
12341556Srgrimes	case NXHERE:
12351556Srgrimes		cmdputs("<<...");
12361556Srgrimes		break;
12371556Srgrimes	default:
12381556Srgrimes		cmdputs("???");
12391556Srgrimes		break;
12401556Srgrimes	}
12411556Srgrimes}
12421556Srgrimes
12431556Srgrimes
12441556Srgrimes
12451556SrgrimesSTATIC void
124690111Simpcmdputs(char *s)
124790111Simp{
124825222Ssteve	char *p, *q;
124925222Ssteve	char c;
12501556Srgrimes	int subtype = 0;
12511556Srgrimes
12521556Srgrimes	if (cmdnleft <= 0)
12531556Srgrimes		return;
12541556Srgrimes	p = s;
12551556Srgrimes	q = cmdnextc;
12561556Srgrimes	while ((c = *p++) != '\0') {
12571556Srgrimes		if (c == CTLESC)
12581556Srgrimes			*q++ = *p++;
12591556Srgrimes		else if (c == CTLVAR) {
12601556Srgrimes			*q++ = '$';
12611556Srgrimes			if (--cmdnleft > 0)
12621556Srgrimes				*q++ = '{';
12631556Srgrimes			subtype = *p++;
12641556Srgrimes		} else if (c == '=' && subtype != 0) {
12651556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
12661556Srgrimes			subtype = 0;
12671556Srgrimes		} else if (c == CTLENDVAR) {
12681556Srgrimes			*q++ = '}';
126918954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
12701556Srgrimes			cmdnleft++;		/* ignore it */
12711556Srgrimes		else
12721556Srgrimes			*q++ = c;
12731556Srgrimes		if (--cmdnleft <= 0) {
12741556Srgrimes			*q++ = '.';
12751556Srgrimes			*q++ = '.';
12761556Srgrimes			*q++ = '.';
12771556Srgrimes			break;
12781556Srgrimes		}
12791556Srgrimes	}
12801556Srgrimes	cmdnextc = q;
12811556Srgrimes}
1282