jobs.c revision 97669
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 97669 2002-05-31 14:04:23Z 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
34297669Stjr			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;
52125222Ssteve	struct job *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;
5421556Srgrimes#endif
5431556Srgrimes		} else {
54425222Ssteve			struct job *found = NULL;
5451556Srgrimes			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
5461556Srgrimes				if (jp->used && jp->nprocs > 0
5471556Srgrimes				 && prefix(name + 1, jp->ps[0].cmd)) {
5481556Srgrimes					if (found)
5491556Srgrimes						error("%s: ambiguous", name);
5501556Srgrimes					found = jp;
5511556Srgrimes				}
5521556Srgrimes			}
5531556Srgrimes			if (found)
5541556Srgrimes				return found;
5551556Srgrimes		}
5561556Srgrimes	} else if (is_number(name)) {
5571556Srgrimes		pid = number(name);
5581556Srgrimes		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
5591556Srgrimes			if (jp->used && jp->nprocs > 0
5601556Srgrimes			 && jp->ps[jp->nprocs - 1].pid == pid)
5611556Srgrimes				return jp;
5621556Srgrimes		}
5631556Srgrimes	}
5641556Srgrimes	error("No such job: %s", name);
56517987Speter	/*NOTREACHED*/
56617987Speter	return NULL;
5671556Srgrimes}
5681556Srgrimes
5691556Srgrimes
5701556Srgrimes
5711556Srgrimes/*
5721556Srgrimes * Return a new job structure,
5731556Srgrimes */
5741556Srgrimes
5751556Srgrimesstruct job *
57690111Simpmakejob(union node *node __unused, int nprocs)
57717987Speter{
5781556Srgrimes	int i;
5791556Srgrimes	struct job *jp;
5801556Srgrimes
5811556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
5821556Srgrimes		if (--i < 0) {
5831556Srgrimes			INTOFF;
5841556Srgrimes			if (njobs == 0) {
5851556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
58697664Stjr#if JOBS
58797659Stjr				jobmru = NULL;
58897664Stjr#endif
5891556Srgrimes			} else {
5901556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
59117987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
59297659Stjr#if JOBS
59397659Stjr				/* Relocate `next' pointers and list head */
59497659Stjr				jobmru = &jp[jobmru - jobtab];
59597659Stjr				for (i = 0; i < njobs; i++)
59697659Stjr					if (jp[i].next != NULL)
59797659Stjr						jp[i].next = &jp[jp[i].next -
59897659Stjr						    jobtab];
59997659Stjr#endif
60020425Ssteve				/* Relocate `ps' pointers */
60120425Ssteve				for (i = 0; i < njobs; i++)
60220425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
60320425Ssteve						jp[i].ps = &jp[i].ps0;
6041556Srgrimes				ckfree(jobtab);
6051556Srgrimes				jobtab = jp;
6061556Srgrimes			}
6071556Srgrimes			jp = jobtab + njobs;
6081556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
6091556Srgrimes			INTON;
6101556Srgrimes			break;
6111556Srgrimes		}
6121556Srgrimes		if (jp->used == 0)
6131556Srgrimes			break;
6141556Srgrimes	}
6151556Srgrimes	INTOFF;
6161556Srgrimes	jp->state = 0;
6171556Srgrimes	jp->used = 1;
6181556Srgrimes	jp->changed = 0;
6191556Srgrimes	jp->nprocs = 0;
6201556Srgrimes#if JOBS
6211556Srgrimes	jp->jobctl = jobctl;
62297659Stjr	jp->next = NULL;
6231556Srgrimes#endif
6241556Srgrimes	if (nprocs > 1) {
6251556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
6261556Srgrimes	} else {
6271556Srgrimes		jp->ps = &jp->ps0;
6281556Srgrimes	}
6291556Srgrimes	INTON;
63017987Speter	TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
63117987Speter	    jp - jobtab + 1));
6321556Srgrimes	return jp;
6338855Srgrimes}
6341556Srgrimes
63597659Stjr#if JOBS
63697659StjrSTATIC void
63797659Stjrsetcurjob(struct job *cj)
63897659Stjr{
63997659Stjr	struct job *jp, *prev;
6401556Srgrimes
64197659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
64297659Stjr		if (jp == cj) {
64397659Stjr			if (prev != NULL)
64497659Stjr				prev->next = jp->next;
64597659Stjr			else
64697659Stjr				jobmru = jp->next;
64797659Stjr			jp->next = jobmru;
64897659Stjr			jobmru = cj;
64997659Stjr			return;
65097659Stjr		}
65197659Stjr	}
65297659Stjr	cj->next = jobmru;
65397659Stjr	jobmru = cj;
65497659Stjr}
65597659Stjr
65697659StjrSTATIC void
65797659Stjrdeljob(struct job *j)
65897659Stjr{
65997659Stjr	struct job *jp, *prev;
66097659Stjr
66197659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
66297659Stjr		if (jp == j) {
66397659Stjr			if (prev != NULL)
66497659Stjr				prev->next = jp->next;
66597659Stjr			else
66697659Stjr				jobmru = jp->next;
66797659Stjr			return;
66897659Stjr		}
66997659Stjr	}
67097659Stjr}
67197659Stjr
6721556Srgrimes/*
67397659Stjr * Return the most recently used job that isn't `nj', and preferably one
67497659Stjr * that is stopped.
67597659Stjr */
67697659StjrSTATIC struct job *
67797659Stjrgetcurjob(struct job *nj)
67897659Stjr{
67997659Stjr	struct job *jp;
68097659Stjr
68197659Stjr	/* Try to find a stopped one.. */
68297659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
68397659Stjr		if (jp->used && jp != nj && jp->state == JOBSTOPPED)
68497659Stjr			return (jp);
68597659Stjr	/* Otherwise the most recently used job that isn't `nj' */
68697659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
68797659Stjr		if (jp->used && jp != nj)
68897659Stjr			return (jp);
68997659Stjr
69097659Stjr	return (NULL);
69197659Stjr}
69297659Stjr
69397659Stjr#endif
69497659Stjr
69597659Stjr/*
6961556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
6971556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
6981556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
6991556Srgrimes * be NULL.  The mode parameter can be one of the following:
7001556Srgrimes *	FORK_FG - Fork off a foreground process.
7011556Srgrimes *	FORK_BG - Fork off a background process.
7021556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
7031556Srgrimes *		     process group even if job control is on.
7041556Srgrimes *
7051556Srgrimes * When job control is turned off, background processes have their standard
7061556Srgrimes * input redirected to /dev/null (except for the second and later processes
7071556Srgrimes * in a pipeline).
7081556Srgrimes */
7091556Srgrimes
7101556Srgrimesint
71190111Simpforkshell(struct job *jp, union node *n, int mode)
71217987Speter{
7131556Srgrimes	int pid;
7141556Srgrimes	int pgrp;
7151556Srgrimes
71617987Speter	TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
71717987Speter	    mode));
7181556Srgrimes	INTOFF;
7191556Srgrimes	pid = fork();
7201556Srgrimes	if (pid == -1) {
7211556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
7221556Srgrimes		INTON;
72353891Scracauer		error("Cannot fork: %s", strerror(errno));
7241556Srgrimes	}
7251556Srgrimes	if (pid == 0) {
7261556Srgrimes		struct job *p;
7271556Srgrimes		int wasroot;
7281556Srgrimes		int i;
7291556Srgrimes
7301556Srgrimes		TRACE(("Child shell %d\n", getpid()));
7311556Srgrimes		wasroot = rootshell;
7321556Srgrimes		rootshell = 0;
7331556Srgrimes		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
7341556Srgrimes			if (p->used)
7351556Srgrimes				freejob(p);
7361556Srgrimes		closescript();
7371556Srgrimes		INTON;
7381556Srgrimes		clear_traps();
7391556Srgrimes#if JOBS
7401556Srgrimes		jobctl = 0;		/* do job control only in root shell */
7411556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
7421556Srgrimes			if (jp == NULL || jp->nprocs == 0)
7431556Srgrimes				pgrp = getpid();
7441556Srgrimes			else
7451556Srgrimes				pgrp = jp->ps[0].pid;
74621352Ssteve			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
7471556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
74820425Ssteve#ifdef OLD_TTY_DRIVER
7491556Srgrimes				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
75018016Speter					error("TIOCSPGRP failed, errno=%d", errno);
75120425Ssteve#else
75220425Ssteve				if (tcsetpgrp(2, pgrp) < 0)
75320425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
75420425Ssteve#endif
7551556Srgrimes			}
7561556Srgrimes			setsignal(SIGTSTP);
7571556Srgrimes			setsignal(SIGTTOU);
7581556Srgrimes		} else if (mode == FORK_BG) {
7591556Srgrimes			ignoresig(SIGINT);
7601556Srgrimes			ignoresig(SIGQUIT);
7611556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
7621556Srgrimes			    ! fd0_redirected_p ()) {
7631556Srgrimes				close(0);
76469793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
76569793Sobrien					error("Can't open %s: %s",
76669793Sobrien					    _PATH_DEVNULL, strerror(errno));
7671556Srgrimes			}
7681556Srgrimes		}
7691556Srgrimes#else
7701556Srgrimes		if (mode == FORK_BG) {
7711556Srgrimes			ignoresig(SIGINT);
7721556Srgrimes			ignoresig(SIGQUIT);
7731556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
7741556Srgrimes			    ! fd0_redirected_p ()) {
7751556Srgrimes				close(0);
77669793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
77769793Sobrien					error("Can't open %s: %s",
77869793Sobrien					    _PATH_DEVNULL, strerror(errno));
7791556Srgrimes			}
7801556Srgrimes		}
7811556Srgrimes#endif
7821556Srgrimes		if (wasroot && iflag) {
7831556Srgrimes			setsignal(SIGINT);
7841556Srgrimes			setsignal(SIGQUIT);
7851556Srgrimes			setsignal(SIGTERM);
7861556Srgrimes		}
7871556Srgrimes		return pid;
7881556Srgrimes	}
7891556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
7901556Srgrimes		if (jp == NULL || jp->nprocs == 0)
7911556Srgrimes			pgrp = pid;
7921556Srgrimes		else
7931556Srgrimes			pgrp = jp->ps[0].pid;
79417987Speter		setpgid(pid, pgrp);
7951556Srgrimes	}
7961556Srgrimes	if (mode == FORK_BG)
7971556Srgrimes		backgndpid = pid;		/* set $! */
7981556Srgrimes	if (jp) {
7991556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
8001556Srgrimes		ps->pid = pid;
8011556Srgrimes		ps->status = -1;
8021556Srgrimes		ps->cmd = nullstr;
8031556Srgrimes		if (iflag && rootshell && n)
8041556Srgrimes			ps->cmd = commandtext(n);
80597659Stjr#if JOBS
80697659Stjr		setcurjob(jp);
80797659Stjr#endif
8081556Srgrimes	}
8091556Srgrimes	INTON;
8101556Srgrimes	TRACE(("In parent shell:  child = %d\n", pid));
8111556Srgrimes	return pid;
8121556Srgrimes}
8131556Srgrimes
8141556Srgrimes
8151556Srgrimes
8161556Srgrimes/*
8171556Srgrimes * Wait for job to finish.
8181556Srgrimes *
8191556Srgrimes * Under job control we have the problem that while a child process is
8201556Srgrimes * running interrupts generated by the user are sent to the child but not
8211556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
8221556Srgrimes * active user may be hard to kill.  With job control turned off, an
8231556Srgrimes * interactive user may place an interactive program inside a loop.  If
8241556Srgrimes * the interactive program catches interrupts, the user doesn't want
8251556Srgrimes * these interrupts to also abort the loop.  The approach we take here
8261556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
82746684Skris * foreground process to terminate, and then send itself an interrupt
8281556Srgrimes * signal if the child process was terminated by an interrupt signal.
8291556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
8301556Srgrimes * exit on interrupt; unless these processes terminate themselves by
8311556Srgrimes * sending a signal to themselves (instead of calling exit) they will
8321556Srgrimes * confuse this approach.
8331556Srgrimes */
8341556Srgrimes
8351556Srgrimesint
83690111Simpwaitforjob(struct job *jp, int *origstatus)
83745916Scracauer{
8381556Srgrimes#if JOBS
83917987Speter	int mypgrp = getpgrp();
8401556Srgrimes#endif
8411556Srgrimes	int status;
8421556Srgrimes	int st;
8431556Srgrimes
8441556Srgrimes	INTOFF;
8451556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
84638950Scracauer	while (jp->state == 0)
84738950Scracauer		if (dowait(1, jp) == -1)
84838950Scracauer			dotrap();
8491556Srgrimes#if JOBS
8501556Srgrimes	if (jp->jobctl) {
85120425Ssteve#ifdef OLD_TTY_DRIVER
8521556Srgrimes		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
85320425Ssteve			error("TIOCSPGRP failed, errno=%d\n", errno);
85420425Ssteve#else
85520425Ssteve		if (tcsetpgrp(2, mypgrp) < 0)
85620425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
85720425Ssteve#endif
8581556Srgrimes	}
8591556Srgrimes	if (jp->state == JOBSTOPPED)
86097659Stjr		setcurjob(jp);
8611556Srgrimes#endif
8621556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
86345916Scracauer	if (origstatus != NULL)
86445916Scracauer		*origstatus = status;
8651556Srgrimes	/* convert to 8 bits */
86626104Ssteve	if (WIFEXITED(status))
86726104Ssteve		st = WEXITSTATUS(status);
8681556Srgrimes#if JOBS
86926104Ssteve	else if (WIFSTOPPED(status))
87026104Ssteve		st = WSTOPSIG(status) + 128;
8711556Srgrimes#endif
8721556Srgrimes	else
87326104Ssteve		st = WTERMSIG(status) + 128;
8741556Srgrimes	if (! JOBS || jp->state == JOBDONE)
8751556Srgrimes		freejob(jp);
87638521Scracauer	if (int_pending()) {
87738521Scracauer		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
87838521Scracauer			kill(getpid(), SIGINT);
87938521Scracauer		else
88038521Scracauer			CLEAR_PENDING_INT;
88138521Scracauer	}
8821556Srgrimes	INTON;
8831556Srgrimes	return st;
8841556Srgrimes}
8851556Srgrimes
8861556Srgrimes
8871556Srgrimes
8881556Srgrimes/*
8891556Srgrimes * Wait for a process to terminate.
8901556Srgrimes */
8911556Srgrimes
8921556SrgrimesSTATIC int
89390111Simpdowait(int block, struct job *job)
89417987Speter{
8951556Srgrimes	int pid;
8961556Srgrimes	int status;
8971556Srgrimes	struct procstat *sp;
8981556Srgrimes	struct job *jp;
8991556Srgrimes	struct job *thisjob;
9001556Srgrimes	int done;
9011556Srgrimes	int stopped;
9021556Srgrimes	int core;
90326104Ssteve	int sig;
9041556Srgrimes
90538950Scracauer	in_dowait++;
9061556Srgrimes	TRACE(("dowait(%d) called\n", block));
9071556Srgrimes	do {
9081556Srgrimes		pid = waitproc(block, &status);
9091556Srgrimes		TRACE(("wait returns %d, status=%d\n", pid, status));
91072086Scracauer	} while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
91172086Scracauer	    (WIFSTOPPED(status) && !iflag));
91238950Scracauer	in_dowait--;
91338521Scracauer	if (breakwaitcmd != 0) {
91438521Scracauer		breakwaitcmd = 0;
91538521Scracauer		return -1;
91638521Scracauer	}
9171556Srgrimes	if (pid <= 0)
9181556Srgrimes		return pid;
9191556Srgrimes	INTOFF;
9201556Srgrimes	thisjob = NULL;
9211556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
9221556Srgrimes		if (jp->used) {
9231556Srgrimes			done = 1;
9241556Srgrimes			stopped = 1;
9251556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
9261556Srgrimes				if (sp->pid == -1)
9271556Srgrimes					continue;
9281556Srgrimes				if (sp->pid == pid) {
92926104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
93026104Ssteve						   pid, sp->status, status));
9311556Srgrimes					sp->status = status;
9321556Srgrimes					thisjob = jp;
9331556Srgrimes				}
9341556Srgrimes				if (sp->status == -1)
9351556Srgrimes					stopped = 0;
93626104Ssteve				else if (WIFSTOPPED(sp->status))
9371556Srgrimes					done = 0;
9381556Srgrimes			}
9391556Srgrimes			if (stopped) {		/* stopped or done */
9401556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
9411556Srgrimes				if (jp->state != state) {
9421556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
9431556Srgrimes					jp->state = state;
9441556Srgrimes#if JOBS
94597659Stjr					if (done)
94697659Stjr						deljob(jp);
9471556Srgrimes#endif
9481556Srgrimes				}
9491556Srgrimes			}
9501556Srgrimes		}
9511556Srgrimes	}
9521556Srgrimes	INTON;
9531556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
9541556Srgrimes#if JOBS
95526104Ssteve		if (WIFSTOPPED(status))
95626104Ssteve			sig = WSTOPSIG(status);
95726104Ssteve		else
9581556Srgrimes#endif
95997663Stjr		{
96026104Ssteve			if (WIFEXITED(status))
96126104Ssteve				sig = 0;
96226104Ssteve			else
96326104Ssteve				sig = WTERMSIG(status);
9641556Srgrimes		}
96597663Stjr		if (sig != 0 && sig != SIGINT && sig != SIGPIPE)
96697669Stjr			showjob(thisjob, 0, 0);
9671556Srgrimes	} else {
9681556Srgrimes		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
9691556Srgrimes		if (thisjob)
9701556Srgrimes			thisjob->changed = 1;
9711556Srgrimes	}
9721556Srgrimes	return pid;
9731556Srgrimes}
9741556Srgrimes
9751556Srgrimes
9761556Srgrimes
9771556Srgrimes/*
9781556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
9791556Srgrimes * stopped processes.  If block is zero, we return a value of zero
9801556Srgrimes * rather than blocking.
9811556Srgrimes *
9821556Srgrimes * System V doesn't have a non-blocking wait system call.  It does
9831556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's
9841556Srgrimes * children dies.  The obvious way to use SIGCLD would be to install
9851556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD
9861556Srgrimes * was received, and have waitproc bump another counter when it got
9871556Srgrimes * the status of a process.  Waitproc would then know that a wait
9881556Srgrimes * system call would not block if the two counters were different.
9891556Srgrimes * This approach doesn't work because if a process has children that
9901556Srgrimes * have not been waited for, System V will send it a SIGCLD when it
9911556Srgrimes * installs a signal handler for SIGCLD.  What this means is that when
9921556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously
9931556Srgrimes * until is runs out of stack space, unless it does a wait call before
9941556Srgrimes * restoring the signal handler.  The code below takes advantage of
9951556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and
9961556Srgrimes * then checking to see whether it was called.  If there are any
9971556Srgrimes * children to be waited for, it will be.
9981556Srgrimes *
9991556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking
10001556Srgrimes * waits at all.  In this case, the user will not be informed when
10011556Srgrimes * a background process until the next time she runs a real program
10021556Srgrimes * (as opposed to running a builtin command or just typing return),
10031556Srgrimes * and the jobs command may give out of date information.
10041556Srgrimes */
10051556Srgrimes
10061556Srgrimes#ifdef SYSV
100738521ScracauerSTATIC sig_atomic_t gotsigchild;
10081556Srgrimes
10091556SrgrimesSTATIC int onsigchild() {
10101556Srgrimes	gotsigchild = 1;
10111556Srgrimes}
10121556Srgrimes#endif
10131556Srgrimes
10141556Srgrimes
10151556SrgrimesSTATIC int
101690111Simpwaitproc(int block, int *status)
101717987Speter{
10181556Srgrimes#ifdef BSD
10191556Srgrimes	int flags;
10201556Srgrimes
10211556Srgrimes#if JOBS
10221556Srgrimes	flags = WUNTRACED;
10231556Srgrimes#else
10241556Srgrimes	flags = 0;
10251556Srgrimes#endif
10261556Srgrimes	if (block == 0)
10271556Srgrimes		flags |= WNOHANG;
10281556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
10291556Srgrimes#else
10301556Srgrimes#ifdef SYSV
10311556Srgrimes	int (*save)();
10321556Srgrimes
10331556Srgrimes	if (block == 0) {
10341556Srgrimes		gotsigchild = 0;
10351556Srgrimes		save = signal(SIGCLD, onsigchild);
10361556Srgrimes		signal(SIGCLD, save);
10371556Srgrimes		if (gotsigchild == 0)
10381556Srgrimes			return 0;
10391556Srgrimes	}
10401556Srgrimes	return wait(status);
10411556Srgrimes#else
10421556Srgrimes	if (block == 0)
10431556Srgrimes		return 0;
10441556Srgrimes	return wait(status);
10451556Srgrimes#endif
10461556Srgrimes#endif
10471556Srgrimes}
10481556Srgrimes
10491556Srgrimes/*
10501556Srgrimes * return 1 if there are stopped jobs, otherwise 0
10511556Srgrimes */
10521556Srgrimesint job_warning = 0;
10531556Srgrimesint
105490111Simpstoppedjobs(void)
10551556Srgrimes{
105625222Ssteve	int jobno;
105725222Ssteve	struct job *jp;
10581556Srgrimes
10591556Srgrimes	if (job_warning)
10601556Srgrimes		return (0);
10611556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
10621556Srgrimes		if (jp->used == 0)
10631556Srgrimes			continue;
10641556Srgrimes		if (jp->state == JOBSTOPPED) {
10651556Srgrimes			out2str("You have stopped jobs.\n");
10661556Srgrimes			job_warning = 2;
10671556Srgrimes			return (1);
10681556Srgrimes		}
10691556Srgrimes	}
10701556Srgrimes
10711556Srgrimes	return (0);
10721556Srgrimes}
10731556Srgrimes
10741556Srgrimes/*
10751556Srgrimes * Return a string identifying a command (to be printed by the
10761556Srgrimes * jobs command.
10771556Srgrimes */
10781556Srgrimes
10791556SrgrimesSTATIC char *cmdnextc;
10801556SrgrimesSTATIC int cmdnleft;
10811556Srgrimes#define MAXCMDTEXT	200
10821556Srgrimes
10831556Srgrimeschar *
108490111Simpcommandtext(union node *n)
108590111Simp{
10861556Srgrimes	char *name;
10871556Srgrimes
10881556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
10891556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
10901556Srgrimes	cmdtxt(n);
10911556Srgrimes	*cmdnextc = '\0';
10921556Srgrimes	return name;
10931556Srgrimes}
10941556Srgrimes
10951556Srgrimes
10961556SrgrimesSTATIC void
109790111Simpcmdtxt(union node *n)
109890111Simp{
10991556Srgrimes	union node *np;
11001556Srgrimes	struct nodelist *lp;
11011556Srgrimes	char *p;
11021556Srgrimes	int i;
11031556Srgrimes	char s[2];
11041556Srgrimes
11051556Srgrimes	if (n == NULL)
11061556Srgrimes		return;
11071556Srgrimes	switch (n->type) {
11081556Srgrimes	case NSEMI:
11091556Srgrimes		cmdtxt(n->nbinary.ch1);
11101556Srgrimes		cmdputs("; ");
11111556Srgrimes		cmdtxt(n->nbinary.ch2);
11121556Srgrimes		break;
11131556Srgrimes	case NAND:
11141556Srgrimes		cmdtxt(n->nbinary.ch1);
11151556Srgrimes		cmdputs(" && ");
11161556Srgrimes		cmdtxt(n->nbinary.ch2);
11171556Srgrimes		break;
11181556Srgrimes	case NOR:
11191556Srgrimes		cmdtxt(n->nbinary.ch1);
11201556Srgrimes		cmdputs(" || ");
11211556Srgrimes		cmdtxt(n->nbinary.ch2);
11221556Srgrimes		break;
11231556Srgrimes	case NPIPE:
11241556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
11251556Srgrimes			cmdtxt(lp->n);
11261556Srgrimes			if (lp->next)
11271556Srgrimes				cmdputs(" | ");
11281556Srgrimes		}
11291556Srgrimes		break;
11301556Srgrimes	case NSUBSHELL:
11311556Srgrimes		cmdputs("(");
11321556Srgrimes		cmdtxt(n->nredir.n);
11331556Srgrimes		cmdputs(")");
11341556Srgrimes		break;
11351556Srgrimes	case NREDIR:
11361556Srgrimes	case NBACKGND:
11371556Srgrimes		cmdtxt(n->nredir.n);
11381556Srgrimes		break;
11391556Srgrimes	case NIF:
11401556Srgrimes		cmdputs("if ");
11411556Srgrimes		cmdtxt(n->nif.test);
11421556Srgrimes		cmdputs("; then ");
11431556Srgrimes		cmdtxt(n->nif.ifpart);
11441556Srgrimes		cmdputs("...");
11451556Srgrimes		break;
11461556Srgrimes	case NWHILE:
11471556Srgrimes		cmdputs("while ");
11481556Srgrimes		goto until;
11491556Srgrimes	case NUNTIL:
11501556Srgrimes		cmdputs("until ");
11511556Srgrimesuntil:
11521556Srgrimes		cmdtxt(n->nbinary.ch1);
11531556Srgrimes		cmdputs("; do ");
11541556Srgrimes		cmdtxt(n->nbinary.ch2);
11551556Srgrimes		cmdputs("; done");
11561556Srgrimes		break;
11571556Srgrimes	case NFOR:
11581556Srgrimes		cmdputs("for ");
11591556Srgrimes		cmdputs(n->nfor.var);
11601556Srgrimes		cmdputs(" in ...");
11611556Srgrimes		break;
11621556Srgrimes	case NCASE:
11631556Srgrimes		cmdputs("case ");
11641556Srgrimes		cmdputs(n->ncase.expr->narg.text);
11651556Srgrimes		cmdputs(" in ...");
11661556Srgrimes		break;
11671556Srgrimes	case NDEFUN:
11681556Srgrimes		cmdputs(n->narg.text);
11691556Srgrimes		cmdputs("() ...");
11701556Srgrimes		break;
11711556Srgrimes	case NCMD:
11721556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
11731556Srgrimes			cmdtxt(np);
11741556Srgrimes			if (np->narg.next)
11751556Srgrimes				cmdputs(" ");
11761556Srgrimes		}
11771556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
11781556Srgrimes			cmdputs(" ");
11791556Srgrimes			cmdtxt(np);
11801556Srgrimes		}
11811556Srgrimes		break;
11821556Srgrimes	case NARG:
11831556Srgrimes		cmdputs(n->narg.text);
11841556Srgrimes		break;
11851556Srgrimes	case NTO:
11861556Srgrimes		p = ">";  i = 1;  goto redir;
11871556Srgrimes	case NAPPEND:
11881556Srgrimes		p = ">>";  i = 1;  goto redir;
11891556Srgrimes	case NTOFD:
11901556Srgrimes		p = ">&";  i = 1;  goto redir;
119196922Stjr	case NCLOBBER:
119296922Stjr		p = ">|"; i = 1; goto redir;
11931556Srgrimes	case NFROM:
11941556Srgrimes		p = "<";  i = 0;  goto redir;
119566612Sbrian	case NFROMTO:
119666612Sbrian		p = "<>";  i = 0;  goto redir;
11971556Srgrimes	case NFROMFD:
11981556Srgrimes		p = "<&";  i = 0;  goto redir;
11991556Srgrimesredir:
12001556Srgrimes		if (n->nfile.fd != i) {
12011556Srgrimes			s[0] = n->nfile.fd + '0';
12021556Srgrimes			s[1] = '\0';
12031556Srgrimes			cmdputs(s);
12041556Srgrimes		}
12051556Srgrimes		cmdputs(p);
12061556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
12071556Srgrimes			s[0] = n->ndup.dupfd + '0';
12081556Srgrimes			s[1] = '\0';
12091556Srgrimes			cmdputs(s);
12101556Srgrimes		} else {
12111556Srgrimes			cmdtxt(n->nfile.fname);
12121556Srgrimes		}
12131556Srgrimes		break;
12141556Srgrimes	case NHERE:
12151556Srgrimes	case NXHERE:
12161556Srgrimes		cmdputs("<<...");
12171556Srgrimes		break;
12181556Srgrimes	default:
12191556Srgrimes		cmdputs("???");
12201556Srgrimes		break;
12211556Srgrimes	}
12221556Srgrimes}
12231556Srgrimes
12241556Srgrimes
12251556Srgrimes
12261556SrgrimesSTATIC void
122790111Simpcmdputs(char *s)
122890111Simp{
122925222Ssteve	char *p, *q;
123025222Ssteve	char c;
12311556Srgrimes	int subtype = 0;
12321556Srgrimes
12331556Srgrimes	if (cmdnleft <= 0)
12341556Srgrimes		return;
12351556Srgrimes	p = s;
12361556Srgrimes	q = cmdnextc;
12371556Srgrimes	while ((c = *p++) != '\0') {
12381556Srgrimes		if (c == CTLESC)
12391556Srgrimes			*q++ = *p++;
12401556Srgrimes		else if (c == CTLVAR) {
12411556Srgrimes			*q++ = '$';
12421556Srgrimes			if (--cmdnleft > 0)
12431556Srgrimes				*q++ = '{';
12441556Srgrimes			subtype = *p++;
12451556Srgrimes		} else if (c == '=' && subtype != 0) {
12461556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
12471556Srgrimes			subtype = 0;
12481556Srgrimes		} else if (c == CTLENDVAR) {
12491556Srgrimes			*q++ = '}';
125018954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
12511556Srgrimes			cmdnleft++;		/* ignore it */
12521556Srgrimes		else
12531556Srgrimes			*q++ = c;
12541556Srgrimes		if (--cmdnleft <= 0) {
12551556Srgrimes			*q++ = '.';
12561556Srgrimes			*q++ = '.';
12571556Srgrimes			*q++ = '.';
12581556Srgrimes			break;
12591556Srgrimes		}
12601556Srgrimes	}
12611556Srgrimes	cmdnextc = q;
12621556Srgrimes}
1263