jobs.c revision 100308
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
411556Srgrimes#endif /* not lint */
4299110Sobrien#include <sys/cdefs.h>
4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/jobs.c 100308 2002-07-18 10:22:42Z tjr $");
441556Srgrimes
4517987Speter#include <fcntl.h>
4617987Speter#include <signal.h>
4717987Speter#include <errno.h>
4899762Stjr#include <paths.h>
4917987Speter#include <unistd.h>
5017987Speter#include <stdlib.h>
5117987Speter#include <sys/param.h>
5217987Speter#ifdef BSD
5317987Speter#include <sys/wait.h>
5417987Speter#include <sys/time.h>
5517987Speter#include <sys/resource.h>
5669793Sobrien#include <paths.h>
5717987Speter#endif
5818018Speter#include <sys/ioctl.h>
5917987Speter
601556Srgrimes#include "shell.h"
611556Srgrimes#if JOBS
6217987Speter#include <termios.h>
631556Srgrimes#undef CEOF			/* syntax.h redefines this */
641556Srgrimes#endif
6517987Speter#include "redir.h"
6617987Speter#include "show.h"
671556Srgrimes#include "main.h"
681556Srgrimes#include "parser.h"
691556Srgrimes#include "nodes.h"
701556Srgrimes#include "jobs.h"
711556Srgrimes#include "options.h"
721556Srgrimes#include "trap.h"
731556Srgrimes#include "syntax.h"
741556Srgrimes#include "input.h"
751556Srgrimes#include "output.h"
761556Srgrimes#include "memalloc.h"
771556Srgrimes#include "error.h"
781556Srgrimes#include "mystring.h"
791556Srgrimes
801556Srgrimes
811556Srgrimesstruct job *jobtab;		/* array of jobs */
821556Srgrimesint njobs;			/* size of array */
8328346SsteveMKINIT pid_t backgndpid = -1;	/* pid of last background process */
841556Srgrimes#if JOBS
8597659Stjrstruct job *jobmru;		/* most recently used job list */
86100308Stjrpid_t initialpgrp;		/* pgrp of shell on invocation */
871556Srgrimes#endif
8838536Scracauerint in_waitcmd = 0;		/* are we in waitcmd()? */
8938950Scracauerint in_dowait = 0;		/* are we in dowait()? */
9038536Scracauervolatile sig_atomic_t breakwaitcmd = 0;	/* should wait be terminated? */
9199762Stjrstatic int ttyfd = -1;
921556Srgrimes
9320425Ssteve#if JOBS
9490111SimpSTATIC void restartjob(struct job *);
9520425Ssteve#endif
9690111SimpSTATIC void freejob(struct job *);
9790111SimpSTATIC struct job *getjob(char *);
98100308StjrSTATIC pid_t dowait(int, struct job *);
9920425Ssteve#if SYSV
10090111SimpSTATIC int onsigchild(void);
10120425Ssteve#endif
102100308StjrSTATIC pid_t waitproc(int, int *);
10390111SimpSTATIC void cmdtxt(union node *);
10490111SimpSTATIC void cmdputs(char *);
10597659Stjr#if JOBS
10697659StjrSTATIC void setcurjob(struct job *);
10797659StjrSTATIC void deljob(struct job *);
10897659StjrSTATIC struct job *getcurjob(struct job *);
10997659Stjr#endif
11097822StjrSTATIC void showjob(struct job *, pid_t, int, int);
1111556Srgrimes
1121556Srgrimes
1131556Srgrimes/*
1141556Srgrimes * Turn job control on and off.
1151556Srgrimes *
1161556Srgrimes * Note:  This code assumes that the third arg to ioctl is a character
1171556Srgrimes * pointer, which is true on Berkeley systems but not System V.  Since
1181556Srgrimes * System V doesn't have job control yet, this isn't a problem now.
1191556Srgrimes */
1201556Srgrimes
1211556SrgrimesMKINIT int jobctl;
1221556Srgrimes
12320425Ssteve#if JOBS
1241556Srgrimesvoid
12590111Simpsetjobctl(int on)
12617987Speter{
12799762Stjr	int i;
1281556Srgrimes
1291556Srgrimes	if (on == jobctl || rootshell == 0)
1301556Srgrimes		return;
1311556Srgrimes	if (on) {
13299762Stjr		if (ttyfd != -1)
13399762Stjr			close(ttyfd);
13499762Stjr		if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) {
13599762Stjr			i = 0;
13699762Stjr			while (i <= 2 && !isatty(i))
13799762Stjr				i++;
13899762Stjr			if (i > 2 || (ttyfd = dup(i)) < 0)
13999762Stjr				goto out;
14099762Stjr		}
14199762Stjr		if (fcntl(ttyfd, FD_CLOEXEC, 1) < 0) {
14299762Stjr			close(ttyfd);
14399762Stjr			ttyfd = -1;
14499762Stjr			goto out;
14599762Stjr		}
1461556Srgrimes		do { /* while we are in the background */
14799762Stjr			initialpgrp = tcgetpgrp(ttyfd);
14820425Ssteve			if (initialpgrp < 0) {
14999762Stjrout:				out2str("sh: can't access tty; job control turned off\n");
1501556Srgrimes				mflag = 0;
1511556Srgrimes				return;
1521556Srgrimes			}
1531556Srgrimes			if (initialpgrp == -1)
15417987Speter				initialpgrp = getpgrp();
15517987Speter			else if (initialpgrp != getpgrp()) {
15699762Stjr				killpg(0, SIGTTIN);
1571556Srgrimes				continue;
1581556Srgrimes			}
1591556Srgrimes		} while (0);
1601556Srgrimes		setsignal(SIGTSTP);
1611556Srgrimes		setsignal(SIGTTOU);
1621556Srgrimes		setsignal(SIGTTIN);
16317987Speter		setpgid(0, rootpid);
16499762Stjr		tcsetpgrp(ttyfd, rootpid);
1651556Srgrimes	} else { /* turning job control off */
16617987Speter		setpgid(0, initialpgrp);
16799762Stjr		tcsetpgrp(ttyfd, initialpgrp);
16899762Stjr		close(ttyfd);
16999762Stjr		ttyfd = -1;
1701556Srgrimes		setsignal(SIGTSTP);
1711556Srgrimes		setsignal(SIGTTOU);
1721556Srgrimes		setsignal(SIGTTIN);
1731556Srgrimes	}
1741556Srgrimes	jobctl = on;
1751556Srgrimes}
17620425Ssteve#endif
1771556Srgrimes
1781556Srgrimes
1791556Srgrimes#ifdef mkinit
18028346SsteveINCLUDE <sys/types.h>
18117987SpeterINCLUDE <stdlib.h>
1821556Srgrimes
1831556SrgrimesSHELLPROC {
1841556Srgrimes	backgndpid = -1;
1851556Srgrimes#if JOBS
1861556Srgrimes	jobctl = 0;
1871556Srgrimes#endif
1881556Srgrimes}
1891556Srgrimes
1901556Srgrimes#endif
1911556Srgrimes
1921556Srgrimes
1931556Srgrimes
1941556Srgrimes#if JOBS
19517987Speterint
19690111Simpfgcmd(int argc __unused, char **argv)
19717987Speter{
1981556Srgrimes	struct job *jp;
199100308Stjr	pid_t pgrp;
2001556Srgrimes	int status;
2011556Srgrimes
2021556Srgrimes	jp = getjob(argv[1]);
2031556Srgrimes	if (jp->jobctl == 0)
2041556Srgrimes		error("job not created under job control");
20596933Stjr	out1str(jp->ps[0].cmd);
20696933Stjr	out1c('\n');
20796933Stjr	flushout(&output);
2081556Srgrimes	pgrp = jp->ps[0].pid;
20999762Stjr	tcsetpgrp(ttyfd, pgrp);
2101556Srgrimes	restartjob(jp);
211100305Stjr	jp->foreground = 1;
2121556Srgrimes	INTOFF;
21345916Scracauer	status = waitforjob(jp, (int *)NULL);
2141556Srgrimes	INTON;
2151556Srgrimes	return status;
2161556Srgrimes}
2171556Srgrimes
2181556Srgrimes
21917987Speterint
22090111Simpbgcmd(int argc, char **argv)
22117987Speter{
22296933Stjr	char s[64];
2231556Srgrimes	struct job *jp;
2241556Srgrimes
2251556Srgrimes	do {
2261556Srgrimes		jp = getjob(*++argv);
2271556Srgrimes		if (jp->jobctl == 0)
2281556Srgrimes			error("job not created under job control");
22996933Stjr		if (jp->state == JOBDONE)
23096933Stjr			continue;
2311556Srgrimes		restartjob(jp);
232100305Stjr		jp->foreground = 0;
23396933Stjr		fmtstr(s, 64, "[%d] ", jp - jobtab + 1);
23496933Stjr		out1str(s);
23596933Stjr		out1str(jp->ps[0].cmd);
23696933Stjr		out1c('\n');
2371556Srgrimes	} while (--argc > 1);
2381556Srgrimes	return 0;
2391556Srgrimes}
2401556Srgrimes
2411556Srgrimes
2421556SrgrimesSTATIC void
24390111Simprestartjob(struct job *jp)
24417987Speter{
2451556Srgrimes	struct procstat *ps;
2461556Srgrimes	int i;
2471556Srgrimes
2481556Srgrimes	if (jp->state == JOBDONE)
2491556Srgrimes		return;
25097660Stjr	setcurjob(jp);
2511556Srgrimes	INTOFF;
2521556Srgrimes	killpg(jp->ps[0].pid, SIGCONT);
2531556Srgrimes	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
25426104Ssteve		if (WIFSTOPPED(ps->status)) {
2551556Srgrimes			ps->status = -1;
2561556Srgrimes			jp->state = 0;
2571556Srgrimes		}
2581556Srgrimes	}
2591556Srgrimes	INTON;
2601556Srgrimes}
2611556Srgrimes#endif
2621556Srgrimes
2631556Srgrimes
2641556Srgrimesint
26597669Stjrjobscmd(int argc, char *argv[])
26617987Speter{
26797669Stjr	char *id;
26897669Stjr	int ch, sformat, lformat;
26997669Stjr
27097669Stjr	optind = optreset = 1;
27197669Stjr	sformat = lformat = 0;
27297669Stjr	while ((ch = getopt(argc, argv, "ls")) != -1) {
27397669Stjr		switch (ch) {
27497669Stjr		case 'l':
27597669Stjr			lformat = 1;
27697669Stjr			break;
27797669Stjr		case 's':
27897669Stjr			sformat = 1;
27997669Stjr			break;
28097669Stjr		case '?':
28197669Stjr		default:
28297669Stjr			error("unknown option: -%c", optopt);
28397669Stjr		}
28497669Stjr	}
28597669Stjr	argc -= optind;
28697669Stjr	argv += optind;
28797669Stjr
28897669Stjr	if (argc == 0)
28997669Stjr		showjobs(0, sformat, lformat);
29097669Stjr	else
29197669Stjr		while ((id = *argv++) != NULL)
29297822Stjr			showjob(getjob(id), 0, sformat, lformat);
29397669Stjr
29497669Stjr	return (0);
2951556Srgrimes}
2961556Srgrimes
29797663StjrSTATIC void
29897822Stjrshowjob(struct job *jp, pid_t pid, int sformat, int lformat)
29997663Stjr{
30097663Stjr	char s[64];
30197663Stjr	struct procstat *ps;
30297669Stjr	struct job *j;
30397669Stjr	int col, curr, i, jobno, prev, procno;
30497669Stjr	char c;
3051556Srgrimes
30697663Stjr	procno = jp->nprocs;
30797663Stjr	jobno = jp - jobtab + 1;
30897669Stjr	curr = prev = 0;
30997669Stjr#if JOBS
31097669Stjr	if ((j = getcurjob(NULL)) != NULL) {
31197669Stjr		curr = j - jobtab + 1;
31297669Stjr		if ((j = getcurjob(j)) != NULL)
31397669Stjr			prev = j - jobtab + 1;
31497669Stjr	}
31597669Stjr#endif
31697663Stjr	for (ps = jp->ps ; ; ps++) {	/* for each process */
31797669Stjr		if (sformat) {
318100308Stjr			out1fmt("%d\n", (int)ps->pid);
31997669Stjr			goto skip;
32097669Stjr		}
32197822Stjr		if (!lformat && ps != jp->ps && pid == 0)
32297669Stjr			goto skip;
32397822Stjr		if (pid != 0 && pid != ps->pid)
32497822Stjr			goto skip;
32597819Stjr		if (jobno == curr && ps == jp->ps)
32697669Stjr			c = '+';
32797819Stjr		else if (jobno == prev && ps == jp->ps)
32897669Stjr			c = '-';
32997669Stjr		else
33097669Stjr			c = ' ';
33197663Stjr		if (ps == jp->ps)
33297669Stjr			fmtstr(s, 64, "[%d] %c ", jobno, c);
33397663Stjr		else
33497816Stjr			fmtstr(s, 64, "    %c ", c);
33597663Stjr		out1str(s);
33697663Stjr		col = strlen(s);
33797669Stjr		if (lformat) {
338100308Stjr			fmtstr(s, 64, "%d ", (int)ps->pid);
33997669Stjr			out1str(s);
34097669Stjr			col += strlen(s);
34197669Stjr		}
34297663Stjr		s[0] = '\0';
34397819Stjr		if (ps != jp->ps) {
34497819Stjr			*s = '\0';
34597819Stjr		} else if (ps->status == -1) {
34697669Stjr			strcpy(s, "Running");
34797663Stjr		} else if (WIFEXITED(ps->status)) {
34897820Stjr			if (WEXITSTATUS(ps->status) == 0)
34997820Stjr				strcpy(s, "Done");
35097820Stjr			else
35197820Stjr				fmtstr(s, 64, "Done (%d)",
35297820Stjr				    WEXITSTATUS(ps->status));
35397663Stjr		} else {
35497663Stjr#if JOBS
35597663Stjr			if (WIFSTOPPED(ps->status))
35697663Stjr				i = WSTOPSIG(ps->status);
35797663Stjr			else
35897663Stjr#endif
35997663Stjr				i = WTERMSIG(ps->status);
36097663Stjr			if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
36197663Stjr				scopy(sys_siglist[i & 0x7F], s);
36297663Stjr			else
36397663Stjr				fmtstr(s, 64, "Signal %d", i & 0x7F);
36497663Stjr			if (WCOREDUMP(ps->status))
36597663Stjr				strcat(s, " (core dumped)");
36697663Stjr		}
36797663Stjr		out1str(s);
36897663Stjr		col += strlen(s);
36997663Stjr		do {
37097663Stjr			out1c(' ');
37197663Stjr			col++;
37297663Stjr		} while (col < 30);
37397663Stjr		out1str(ps->cmd);
37497663Stjr		out1c('\n');
37597669Stjrskip:		if (--procno <= 0)
37697663Stjr			break;
37797663Stjr	}
37897663Stjr}
37997663Stjr
3801556Srgrimes/*
3811556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
3821556Srgrimes * statuses have changed since the last call to showjobs.
3831556Srgrimes *
3841556Srgrimes * If the shell is interrupted in the process of creating a job, the
3851556Srgrimes * result may be a job structure containing zero processes.  Such structures
3861556Srgrimes * will be freed here.
3871556Srgrimes */
3881556Srgrimes
3891556Srgrimesvoid
39097669Stjrshowjobs(int change, int sformat, int lformat)
39117987Speter{
3921556Srgrimes	int jobno;
3931556Srgrimes	struct job *jp;
3941556Srgrimes
3951556Srgrimes	TRACE(("showjobs(%d) called\n", change));
3961556Srgrimes	while (dowait(0, (struct job *)NULL) > 0);
3971556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
3981556Srgrimes		if (! jp->used)
3991556Srgrimes			continue;
4001556Srgrimes		if (jp->nprocs == 0) {
4011556Srgrimes			freejob(jp);
4021556Srgrimes			continue;
4031556Srgrimes		}
4041556Srgrimes		if (change && ! jp->changed)
4051556Srgrimes			continue;
40697822Stjr		showjob(jp, 0, sformat, lformat);
4071556Srgrimes		jp->changed = 0;
4081556Srgrimes		if (jp->state == JOBDONE) {
4091556Srgrimes			freejob(jp);
4101556Srgrimes		}
4111556Srgrimes	}
4121556Srgrimes}
4131556Srgrimes
4141556Srgrimes
4151556Srgrimes/*
4161556Srgrimes * Mark a job structure as unused.
4171556Srgrimes */
4181556Srgrimes
4191556SrgrimesSTATIC void
42090111Simpfreejob(struct job *jp)
42190111Simp{
4221556Srgrimes	struct procstat *ps;
4231556Srgrimes	int i;
4241556Srgrimes
4251556Srgrimes	INTOFF;
4261556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
4271556Srgrimes		if (ps->cmd != nullstr)
4281556Srgrimes			ckfree(ps->cmd);
4291556Srgrimes	}
4301556Srgrimes	if (jp->ps != &jp->ps0)
4311556Srgrimes		ckfree(jp->ps);
4321556Srgrimes	jp->used = 0;
4331556Srgrimes#if JOBS
43497659Stjr	deljob(jp);
4351556Srgrimes#endif
4361556Srgrimes	INTON;
4371556Srgrimes}
4381556Srgrimes
4391556Srgrimes
4401556Srgrimes
4411556Srgrimesint
44290111Simpwaitcmd(int argc, char **argv)
44317987Speter{
4441556Srgrimes	struct job *job;
44526104Ssteve	int status, retval;
4461556Srgrimes	struct job *jp;
4471556Srgrimes
4481556Srgrimes	if (argc > 1) {
4491556Srgrimes		job = getjob(argv[1]);
4501556Srgrimes	} else {
4511556Srgrimes		job = NULL;
4521556Srgrimes	}
45338536Scracauer
45438536Scracauer	/*
45538536Scracauer	 * Loop until a process is terminated or stopped, or a SIGINT is
45638536Scracauer	 * received.
45738536Scracauer	 */
45838536Scracauer
45938521Scracauer	in_waitcmd++;
46038536Scracauer	do {
4611556Srgrimes		if (job != NULL) {
4621556Srgrimes			if (job->state) {
4631556Srgrimes				status = job->ps[job->nprocs - 1].status;
46426104Ssteve				if (WIFEXITED(status))
46526104Ssteve					retval = WEXITSTATUS(status);
4661556Srgrimes#if JOBS
46726104Ssteve				else if (WIFSTOPPED(status))
46826104Ssteve					retval = WSTOPSIG(status) + 128;
4691556Srgrimes#endif
4701556Srgrimes				else
47126104Ssteve					retval = WTERMSIG(status) + 128;
4721556Srgrimes				if (! iflag)
4731556Srgrimes					freejob(job);
47438536Scracauer				in_waitcmd--;
47526104Ssteve				return retval;
4761556Srgrimes			}
4771556Srgrimes		} else {
4781556Srgrimes			for (jp = jobtab ; ; jp++) {
4791556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
48038536Scracauer					in_waitcmd--;
4811556Srgrimes					return 0;
4821556Srgrimes				}
4831556Srgrimes				if (jp->used && jp->state == 0)
4841556Srgrimes					break;
4851556Srgrimes			}
4861556Srgrimes		}
48738521Scracauer	} while (dowait(1, (struct job *)NULL) != -1);
48838521Scracauer	in_waitcmd--;
48938521Scracauer
49038521Scracauer	return 0;
4911556Srgrimes}
4921556Srgrimes
4931556Srgrimes
4941556Srgrimes
49517987Speterint
49690111Simpjobidcmd(int argc __unused, char **argv)
49717987Speter{
4981556Srgrimes	struct job *jp;
4991556Srgrimes	int i;
5001556Srgrimes
5011556Srgrimes	jp = getjob(argv[1]);
5021556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
503100308Stjr		out1fmt("%d", (int)jp->ps[i].pid);
5041556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
5051556Srgrimes	}
5061556Srgrimes	return 0;
5071556Srgrimes}
5081556Srgrimes
5091556Srgrimes
5101556Srgrimes
5111556Srgrimes/*
5121556Srgrimes * Convert a job name to a job structure.
5131556Srgrimes */
5141556Srgrimes
5151556SrgrimesSTATIC struct job *
51690111Simpgetjob(char *name)
51790111Simp{
5181556Srgrimes	int jobno;
51997688Stjr	struct job *found, *jp;
520100308Stjr	pid_t pid;
5211556Srgrimes	int i;
5221556Srgrimes
5231556Srgrimes	if (name == NULL) {
5241556Srgrimes#if JOBS
52597659Stjrcurrentjob:	if ((jp = getcurjob(NULL)) == NULL)
5261556Srgrimes			error("No current job");
52797659Stjr		return (jp);
5281556Srgrimes#else
5291556Srgrimes		error("No current job");
5301556Srgrimes#endif
5311556Srgrimes	} else if (name[0] == '%') {
5321556Srgrimes		if (is_digit(name[1])) {
5331556Srgrimes			jobno = number(name + 1);
5341556Srgrimes			if (jobno > 0 && jobno <= njobs
5351556Srgrimes			 && jobtab[jobno - 1].used != 0)
5361556Srgrimes				return &jobtab[jobno - 1];
5371556Srgrimes#if JOBS
5381556Srgrimes		} else if (name[1] == '%' && name[2] == '\0') {
5391556Srgrimes			goto currentjob;
54097688Stjr		} else if (name[1] == '+' && name[2] == '\0') {
54197688Stjr			goto currentjob;
54297688Stjr		} else if (name[1] == '-' && name[2] == '\0') {
54397688Stjr			if ((jp = getcurjob(NULL)) == NULL ||
54497688Stjr			    (jp = getcurjob(jp)) == NULL)
54597688Stjr				error("No previous job");
54697688Stjr			return (jp);
5471556Srgrimes#endif
54897688Stjr		} else if (name[1] == '?') {
54997688Stjr			found = NULL;
55097688Stjr			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
55197688Stjr				if (jp->used && jp->nprocs > 0
55297688Stjr				 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
55397688Stjr					if (found)
55497688Stjr						error("%s: ambiguous", name);
55597688Stjr					found = jp;
55697688Stjr				}
55797688Stjr			}
55897688Stjr			if (found != NULL)
55997688Stjr				return (found);
5601556Srgrimes		} else {
56197688Stjr			found = NULL;
5621556Srgrimes			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
5631556Srgrimes				if (jp->used && jp->nprocs > 0
5641556Srgrimes				 && prefix(name + 1, jp->ps[0].cmd)) {
5651556Srgrimes					if (found)
5661556Srgrimes						error("%s: ambiguous", name);
5671556Srgrimes					found = jp;
5681556Srgrimes				}
5691556Srgrimes			}
5701556Srgrimes			if (found)
5711556Srgrimes				return found;
5721556Srgrimes		}
5731556Srgrimes	} else if (is_number(name)) {
574100308Stjr		pid = (pid_t)number(name);
5751556Srgrimes		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
5761556Srgrimes			if (jp->used && jp->nprocs > 0
5771556Srgrimes			 && jp->ps[jp->nprocs - 1].pid == pid)
5781556Srgrimes				return jp;
5791556Srgrimes		}
5801556Srgrimes	}
5811556Srgrimes	error("No such job: %s", name);
58217987Speter	/*NOTREACHED*/
58317987Speter	return NULL;
5841556Srgrimes}
5851556Srgrimes
5861556Srgrimes
5871556Srgrimes
5881556Srgrimes/*
5891556Srgrimes * Return a new job structure,
5901556Srgrimes */
5911556Srgrimes
5921556Srgrimesstruct job *
59390111Simpmakejob(union node *node __unused, int nprocs)
59417987Speter{
5951556Srgrimes	int i;
5961556Srgrimes	struct job *jp;
5971556Srgrimes
5981556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
5991556Srgrimes		if (--i < 0) {
6001556Srgrimes			INTOFF;
6011556Srgrimes			if (njobs == 0) {
6021556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
60397664Stjr#if JOBS
60497659Stjr				jobmru = NULL;
60597664Stjr#endif
6061556Srgrimes			} else {
6071556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
60817987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
60997659Stjr#if JOBS
61097659Stjr				/* Relocate `next' pointers and list head */
61199760Stjr				if (jobmru != NULL)
61299760Stjr					jobmru = &jp[jobmru - jobtab];
61397659Stjr				for (i = 0; i < njobs; i++)
61497659Stjr					if (jp[i].next != NULL)
61597659Stjr						jp[i].next = &jp[jp[i].next -
61697659Stjr						    jobtab];
61797659Stjr#endif
61820425Ssteve				/* Relocate `ps' pointers */
61920425Ssteve				for (i = 0; i < njobs; i++)
62020425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
62120425Ssteve						jp[i].ps = &jp[i].ps0;
6221556Srgrimes				ckfree(jobtab);
6231556Srgrimes				jobtab = jp;
6241556Srgrimes			}
6251556Srgrimes			jp = jobtab + njobs;
6261556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
6271556Srgrimes			INTON;
6281556Srgrimes			break;
6291556Srgrimes		}
6301556Srgrimes		if (jp->used == 0)
6311556Srgrimes			break;
6321556Srgrimes	}
6331556Srgrimes	INTOFF;
6341556Srgrimes	jp->state = 0;
6351556Srgrimes	jp->used = 1;
6361556Srgrimes	jp->changed = 0;
6371556Srgrimes	jp->nprocs = 0;
638100305Stjr	jp->foreground = 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
729100308Stjrpid_t
73090111Simpforkshell(struct job *jp, union node *n, int mode)
73117987Speter{
732100308Stjr	pid_t pid;
733100308Stjr	pid_t 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
749100308Stjr		TRACE(("Child shell %d\n", (int)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 ***/
76799762Stjr				if (tcsetpgrp(ttyfd, pgrp) < 0)
76820425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
7691556Srgrimes			}
7701556Srgrimes			setsignal(SIGTSTP);
7711556Srgrimes			setsignal(SIGTTOU);
7721556Srgrimes		} else if (mode == FORK_BG) {
7731556Srgrimes			ignoresig(SIGINT);
7741556Srgrimes			ignoresig(SIGQUIT);
7751556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
7761556Srgrimes			    ! fd0_redirected_p ()) {
7771556Srgrimes				close(0);
77869793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
77969793Sobrien					error("Can't open %s: %s",
78069793Sobrien					    _PATH_DEVNULL, strerror(errno));
7811556Srgrimes			}
7821556Srgrimes		}
7831556Srgrimes#else
7841556Srgrimes		if (mode == FORK_BG) {
7851556Srgrimes			ignoresig(SIGINT);
7861556Srgrimes			ignoresig(SIGQUIT);
7871556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
7881556Srgrimes			    ! fd0_redirected_p ()) {
7891556Srgrimes				close(0);
79069793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
79169793Sobrien					error("Can't open %s: %s",
79269793Sobrien					    _PATH_DEVNULL, strerror(errno));
7931556Srgrimes			}
7941556Srgrimes		}
7951556Srgrimes#endif
7961556Srgrimes		if (wasroot && iflag) {
7971556Srgrimes			setsignal(SIGINT);
7981556Srgrimes			setsignal(SIGQUIT);
7991556Srgrimes			setsignal(SIGTERM);
8001556Srgrimes		}
8011556Srgrimes		return pid;
8021556Srgrimes	}
8031556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
8041556Srgrimes		if (jp == NULL || jp->nprocs == 0)
8051556Srgrimes			pgrp = pid;
8061556Srgrimes		else
8071556Srgrimes			pgrp = jp->ps[0].pid;
80817987Speter		setpgid(pid, pgrp);
8091556Srgrimes	}
8101556Srgrimes	if (mode == FORK_BG)
8111556Srgrimes		backgndpid = pid;		/* set $! */
8121556Srgrimes	if (jp) {
8131556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
8141556Srgrimes		ps->pid = pid;
8151556Srgrimes		ps->status = -1;
8161556Srgrimes		ps->cmd = nullstr;
8171556Srgrimes		if (iflag && rootshell && n)
8181556Srgrimes			ps->cmd = commandtext(n);
819100305Stjr		jp->foreground = mode == FORK_FG;
82097659Stjr#if JOBS
82197659Stjr		setcurjob(jp);
82297659Stjr#endif
8231556Srgrimes	}
8241556Srgrimes	INTON;
825100308Stjr	TRACE(("In parent shell:  child = %d\n", (int)pid));
8261556Srgrimes	return pid;
8271556Srgrimes}
8281556Srgrimes
8291556Srgrimes
8301556Srgrimes
8311556Srgrimes/*
8321556Srgrimes * Wait for job to finish.
8331556Srgrimes *
8341556Srgrimes * Under job control we have the problem that while a child process is
8351556Srgrimes * running interrupts generated by the user are sent to the child but not
8361556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
8371556Srgrimes * active user may be hard to kill.  With job control turned off, an
8381556Srgrimes * interactive user may place an interactive program inside a loop.  If
8391556Srgrimes * the interactive program catches interrupts, the user doesn't want
8401556Srgrimes * these interrupts to also abort the loop.  The approach we take here
8411556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
84246684Skris * foreground process to terminate, and then send itself an interrupt
8431556Srgrimes * signal if the child process was terminated by an interrupt signal.
8441556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
8451556Srgrimes * exit on interrupt; unless these processes terminate themselves by
8461556Srgrimes * sending a signal to themselves (instead of calling exit) they will
8471556Srgrimes * confuse this approach.
8481556Srgrimes */
8491556Srgrimes
8501556Srgrimesint
85190111Simpwaitforjob(struct job *jp, int *origstatus)
85245916Scracauer{
8531556Srgrimes#if JOBS
854100308Stjr	pid_t mypgrp = getpgrp();
8551556Srgrimes#endif
8561556Srgrimes	int status;
8571556Srgrimes	int st;
8581556Srgrimes
8591556Srgrimes	INTOFF;
8601556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
86138950Scracauer	while (jp->state == 0)
86238950Scracauer		if (dowait(1, jp) == -1)
86338950Scracauer			dotrap();
8641556Srgrimes#if JOBS
8651556Srgrimes	if (jp->jobctl) {
86699762Stjr		if (tcsetpgrp(ttyfd, mypgrp) < 0)
86720425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
8681556Srgrimes	}
8691556Srgrimes	if (jp->state == JOBSTOPPED)
87097659Stjr		setcurjob(jp);
8711556Srgrimes#endif
8721556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
87345916Scracauer	if (origstatus != NULL)
87445916Scracauer		*origstatus = status;
8751556Srgrimes	/* convert to 8 bits */
87626104Ssteve	if (WIFEXITED(status))
87726104Ssteve		st = WEXITSTATUS(status);
8781556Srgrimes#if JOBS
87926104Ssteve	else if (WIFSTOPPED(status))
88026104Ssteve		st = WSTOPSIG(status) + 128;
8811556Srgrimes#endif
8821556Srgrimes	else
88326104Ssteve		st = WTERMSIG(status) + 128;
8841556Srgrimes	if (! JOBS || jp->state == JOBDONE)
8851556Srgrimes		freejob(jp);
88638521Scracauer	if (int_pending()) {
88738521Scracauer		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
88838521Scracauer			kill(getpid(), SIGINT);
88938521Scracauer		else
89038521Scracauer			CLEAR_PENDING_INT;
89138521Scracauer	}
8921556Srgrimes	INTON;
8931556Srgrimes	return st;
8941556Srgrimes}
8951556Srgrimes
8961556Srgrimes
8971556Srgrimes
8981556Srgrimes/*
8991556Srgrimes * Wait for a process to terminate.
9001556Srgrimes */
9011556Srgrimes
902100308StjrSTATIC pid_t
90390111Simpdowait(int block, struct job *job)
90417987Speter{
905100308Stjr	pid_t pid;
9061556Srgrimes	int status;
9071556Srgrimes	struct procstat *sp;
9081556Srgrimes	struct job *jp;
9091556Srgrimes	struct job *thisjob;
9101556Srgrimes	int done;
9111556Srgrimes	int stopped;
91226104Ssteve	int sig;
913100305Stjr	int i;
9141556Srgrimes
91538950Scracauer	in_dowait++;
9161556Srgrimes	TRACE(("dowait(%d) called\n", block));
9171556Srgrimes	do {
9181556Srgrimes		pid = waitproc(block, &status);
919100308Stjr		TRACE(("wait returns %d, status=%d\n", (int)pid, status));
92072086Scracauer	} while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
92172086Scracauer	    (WIFSTOPPED(status) && !iflag));
92238950Scracauer	in_dowait--;
92338521Scracauer	if (breakwaitcmd != 0) {
92438521Scracauer		breakwaitcmd = 0;
92538521Scracauer		return -1;
92638521Scracauer	}
9271556Srgrimes	if (pid <= 0)
9281556Srgrimes		return pid;
9291556Srgrimes	INTOFF;
9301556Srgrimes	thisjob = NULL;
9311556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
9321556Srgrimes		if (jp->used) {
9331556Srgrimes			done = 1;
9341556Srgrimes			stopped = 1;
9351556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
9361556Srgrimes				if (sp->pid == -1)
9371556Srgrimes					continue;
9381556Srgrimes				if (sp->pid == pid) {
93926104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
940100308Stjr						   (int)pid, sp->status,
941100308Stjr						   status));
9421556Srgrimes					sp->status = status;
9431556Srgrimes					thisjob = jp;
9441556Srgrimes				}
9451556Srgrimes				if (sp->status == -1)
9461556Srgrimes					stopped = 0;
94726104Ssteve				else if (WIFSTOPPED(sp->status))
9481556Srgrimes					done = 0;
9491556Srgrimes			}
9501556Srgrimes			if (stopped) {		/* stopped or done */
9511556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
9521556Srgrimes				if (jp->state != state) {
9531556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
9541556Srgrimes					jp->state = state;
9551556Srgrimes#if JOBS
95697659Stjr					if (done)
95797659Stjr						deljob(jp);
9581556Srgrimes#endif
9591556Srgrimes				}
9601556Srgrimes			}
9611556Srgrimes		}
9621556Srgrimes	}
9631556Srgrimes	INTON;
9641556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
9651556Srgrimes#if JOBS
96626104Ssteve		if (WIFSTOPPED(status))
96726104Ssteve			sig = WSTOPSIG(status);
96826104Ssteve		else
9691556Srgrimes#endif
97097663Stjr		{
97126104Ssteve			if (WIFEXITED(status))
97226104Ssteve				sig = 0;
97326104Ssteve			else
97426104Ssteve				sig = WTERMSIG(status);
9751556Srgrimes		}
976100305Stjr		if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
977100305Stjr			if (jp->foreground) {
978100305Stjr#if JOBS
979100305Stjr				if (WIFSTOPPED(status))
980100305Stjr					i = WSTOPSIG(status);
981100305Stjr				else
982100305Stjr#endif
983100305Stjr					i = WTERMSIG(status);
984100305Stjr				if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
985100305Stjr					out1str(sys_siglist[i & 0x7F]);
986100305Stjr				else
987100305Stjr					out1fmt("Signal %d", i & 0x7F);
988100305Stjr				if (WCOREDUMP(status))
989100305Stjr					out1str(" (core dumped)");
990100305Stjr				out1c('\n');
991100305Stjr			} else
992100305Stjr				showjob(thisjob, pid, 0, 1);
993100305Stjr		}
9941556Srgrimes	} else {
9951556Srgrimes		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
9961556Srgrimes		if (thisjob)
9971556Srgrimes			thisjob->changed = 1;
9981556Srgrimes	}
9991556Srgrimes	return pid;
10001556Srgrimes}
10011556Srgrimes
10021556Srgrimes
10031556Srgrimes
10041556Srgrimes/*
10051556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
10061556Srgrimes * stopped processes.  If block is zero, we return a value of zero
10071556Srgrimes * rather than blocking.
10081556Srgrimes *
10091556Srgrimes * System V doesn't have a non-blocking wait system call.  It does
10101556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's
10111556Srgrimes * children dies.  The obvious way to use SIGCLD would be to install
10121556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD
10131556Srgrimes * was received, and have waitproc bump another counter when it got
10141556Srgrimes * the status of a process.  Waitproc would then know that a wait
10151556Srgrimes * system call would not block if the two counters were different.
10161556Srgrimes * This approach doesn't work because if a process has children that
10171556Srgrimes * have not been waited for, System V will send it a SIGCLD when it
10181556Srgrimes * installs a signal handler for SIGCLD.  What this means is that when
10191556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously
10201556Srgrimes * until is runs out of stack space, unless it does a wait call before
10211556Srgrimes * restoring the signal handler.  The code below takes advantage of
10221556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and
10231556Srgrimes * then checking to see whether it was called.  If there are any
10241556Srgrimes * children to be waited for, it will be.
10251556Srgrimes *
10261556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking
10271556Srgrimes * waits at all.  In this case, the user will not be informed when
10281556Srgrimes * a background process until the next time she runs a real program
10291556Srgrimes * (as opposed to running a builtin command or just typing return),
10301556Srgrimes * and the jobs command may give out of date information.
10311556Srgrimes */
10321556Srgrimes
10331556Srgrimes#ifdef SYSV
103438521ScracauerSTATIC sig_atomic_t gotsigchild;
10351556Srgrimes
10361556SrgrimesSTATIC int onsigchild() {
10371556Srgrimes	gotsigchild = 1;
10381556Srgrimes}
10391556Srgrimes#endif
10401556Srgrimes
10411556Srgrimes
1042100308StjrSTATIC pid_t
104390111Simpwaitproc(int block, int *status)
104417987Speter{
10451556Srgrimes#ifdef BSD
10461556Srgrimes	int flags;
10471556Srgrimes
10481556Srgrimes#if JOBS
10491556Srgrimes	flags = WUNTRACED;
10501556Srgrimes#else
10511556Srgrimes	flags = 0;
10521556Srgrimes#endif
10531556Srgrimes	if (block == 0)
10541556Srgrimes		flags |= WNOHANG;
10551556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
10561556Srgrimes#else
10571556Srgrimes#ifdef SYSV
10581556Srgrimes	int (*save)();
10591556Srgrimes
10601556Srgrimes	if (block == 0) {
10611556Srgrimes		gotsigchild = 0;
10621556Srgrimes		save = signal(SIGCLD, onsigchild);
10631556Srgrimes		signal(SIGCLD, save);
10641556Srgrimes		if (gotsigchild == 0)
10651556Srgrimes			return 0;
10661556Srgrimes	}
10671556Srgrimes	return wait(status);
10681556Srgrimes#else
10691556Srgrimes	if (block == 0)
10701556Srgrimes		return 0;
10711556Srgrimes	return wait(status);
10721556Srgrimes#endif
10731556Srgrimes#endif
10741556Srgrimes}
10751556Srgrimes
10761556Srgrimes/*
10771556Srgrimes * return 1 if there are stopped jobs, otherwise 0
10781556Srgrimes */
10791556Srgrimesint job_warning = 0;
10801556Srgrimesint
108190111Simpstoppedjobs(void)
10821556Srgrimes{
108325222Ssteve	int jobno;
108425222Ssteve	struct job *jp;
10851556Srgrimes
10861556Srgrimes	if (job_warning)
10871556Srgrimes		return (0);
10881556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
10891556Srgrimes		if (jp->used == 0)
10901556Srgrimes			continue;
10911556Srgrimes		if (jp->state == JOBSTOPPED) {
10921556Srgrimes			out2str("You have stopped jobs.\n");
10931556Srgrimes			job_warning = 2;
10941556Srgrimes			return (1);
10951556Srgrimes		}
10961556Srgrimes	}
10971556Srgrimes
10981556Srgrimes	return (0);
10991556Srgrimes}
11001556Srgrimes
11011556Srgrimes/*
11021556Srgrimes * Return a string identifying a command (to be printed by the
11031556Srgrimes * jobs command.
11041556Srgrimes */
11051556Srgrimes
11061556SrgrimesSTATIC char *cmdnextc;
11071556SrgrimesSTATIC int cmdnleft;
11081556Srgrimes#define MAXCMDTEXT	200
11091556Srgrimes
11101556Srgrimeschar *
111190111Simpcommandtext(union node *n)
111290111Simp{
11131556Srgrimes	char *name;
11141556Srgrimes
11151556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
11161556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
11171556Srgrimes	cmdtxt(n);
11181556Srgrimes	*cmdnextc = '\0';
11191556Srgrimes	return name;
11201556Srgrimes}
11211556Srgrimes
11221556Srgrimes
11231556SrgrimesSTATIC void
112490111Simpcmdtxt(union node *n)
112590111Simp{
11261556Srgrimes	union node *np;
11271556Srgrimes	struct nodelist *lp;
11281556Srgrimes	char *p;
11291556Srgrimes	int i;
11301556Srgrimes	char s[2];
11311556Srgrimes
11321556Srgrimes	if (n == NULL)
11331556Srgrimes		return;
11341556Srgrimes	switch (n->type) {
11351556Srgrimes	case NSEMI:
11361556Srgrimes		cmdtxt(n->nbinary.ch1);
11371556Srgrimes		cmdputs("; ");
11381556Srgrimes		cmdtxt(n->nbinary.ch2);
11391556Srgrimes		break;
11401556Srgrimes	case NAND:
11411556Srgrimes		cmdtxt(n->nbinary.ch1);
11421556Srgrimes		cmdputs(" && ");
11431556Srgrimes		cmdtxt(n->nbinary.ch2);
11441556Srgrimes		break;
11451556Srgrimes	case NOR:
11461556Srgrimes		cmdtxt(n->nbinary.ch1);
11471556Srgrimes		cmdputs(" || ");
11481556Srgrimes		cmdtxt(n->nbinary.ch2);
11491556Srgrimes		break;
11501556Srgrimes	case NPIPE:
11511556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
11521556Srgrimes			cmdtxt(lp->n);
11531556Srgrimes			if (lp->next)
11541556Srgrimes				cmdputs(" | ");
11551556Srgrimes		}
11561556Srgrimes		break;
11571556Srgrimes	case NSUBSHELL:
11581556Srgrimes		cmdputs("(");
11591556Srgrimes		cmdtxt(n->nredir.n);
11601556Srgrimes		cmdputs(")");
11611556Srgrimes		break;
11621556Srgrimes	case NREDIR:
11631556Srgrimes	case NBACKGND:
11641556Srgrimes		cmdtxt(n->nredir.n);
11651556Srgrimes		break;
11661556Srgrimes	case NIF:
11671556Srgrimes		cmdputs("if ");
11681556Srgrimes		cmdtxt(n->nif.test);
11691556Srgrimes		cmdputs("; then ");
11701556Srgrimes		cmdtxt(n->nif.ifpart);
11711556Srgrimes		cmdputs("...");
11721556Srgrimes		break;
11731556Srgrimes	case NWHILE:
11741556Srgrimes		cmdputs("while ");
11751556Srgrimes		goto until;
11761556Srgrimes	case NUNTIL:
11771556Srgrimes		cmdputs("until ");
11781556Srgrimesuntil:
11791556Srgrimes		cmdtxt(n->nbinary.ch1);
11801556Srgrimes		cmdputs("; do ");
11811556Srgrimes		cmdtxt(n->nbinary.ch2);
11821556Srgrimes		cmdputs("; done");
11831556Srgrimes		break;
11841556Srgrimes	case NFOR:
11851556Srgrimes		cmdputs("for ");
11861556Srgrimes		cmdputs(n->nfor.var);
11871556Srgrimes		cmdputs(" in ...");
11881556Srgrimes		break;
11891556Srgrimes	case NCASE:
11901556Srgrimes		cmdputs("case ");
11911556Srgrimes		cmdputs(n->ncase.expr->narg.text);
11921556Srgrimes		cmdputs(" in ...");
11931556Srgrimes		break;
11941556Srgrimes	case NDEFUN:
11951556Srgrimes		cmdputs(n->narg.text);
11961556Srgrimes		cmdputs("() ...");
11971556Srgrimes		break;
11981556Srgrimes	case NCMD:
11991556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
12001556Srgrimes			cmdtxt(np);
12011556Srgrimes			if (np->narg.next)
12021556Srgrimes				cmdputs(" ");
12031556Srgrimes		}
12041556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
12051556Srgrimes			cmdputs(" ");
12061556Srgrimes			cmdtxt(np);
12071556Srgrimes		}
12081556Srgrimes		break;
12091556Srgrimes	case NARG:
12101556Srgrimes		cmdputs(n->narg.text);
12111556Srgrimes		break;
12121556Srgrimes	case NTO:
12131556Srgrimes		p = ">";  i = 1;  goto redir;
12141556Srgrimes	case NAPPEND:
12151556Srgrimes		p = ">>";  i = 1;  goto redir;
12161556Srgrimes	case NTOFD:
12171556Srgrimes		p = ">&";  i = 1;  goto redir;
121896922Stjr	case NCLOBBER:
121996922Stjr		p = ">|"; i = 1; goto redir;
12201556Srgrimes	case NFROM:
12211556Srgrimes		p = "<";  i = 0;  goto redir;
122266612Sbrian	case NFROMTO:
122366612Sbrian		p = "<>";  i = 0;  goto redir;
12241556Srgrimes	case NFROMFD:
12251556Srgrimes		p = "<&";  i = 0;  goto redir;
12261556Srgrimesredir:
12271556Srgrimes		if (n->nfile.fd != i) {
12281556Srgrimes			s[0] = n->nfile.fd + '0';
12291556Srgrimes			s[1] = '\0';
12301556Srgrimes			cmdputs(s);
12311556Srgrimes		}
12321556Srgrimes		cmdputs(p);
12331556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
123499634Stjr			if (n->ndup.dupfd >= 0)
123599634Stjr				s[0] = n->ndup.dupfd + '0';
123699634Stjr			else
123799634Stjr				s[0] = '-';
12381556Srgrimes			s[1] = '\0';
12391556Srgrimes			cmdputs(s);
12401556Srgrimes		} else {
12411556Srgrimes			cmdtxt(n->nfile.fname);
12421556Srgrimes		}
12431556Srgrimes		break;
12441556Srgrimes	case NHERE:
12451556Srgrimes	case NXHERE:
12461556Srgrimes		cmdputs("<<...");
12471556Srgrimes		break;
12481556Srgrimes	default:
12491556Srgrimes		cmdputs("???");
12501556Srgrimes		break;
12511556Srgrimes	}
12521556Srgrimes}
12531556Srgrimes
12541556Srgrimes
12551556Srgrimes
12561556SrgrimesSTATIC void
125790111Simpcmdputs(char *s)
125890111Simp{
125925222Ssteve	char *p, *q;
126025222Ssteve	char c;
12611556Srgrimes	int subtype = 0;
12621556Srgrimes
12631556Srgrimes	if (cmdnleft <= 0)
12641556Srgrimes		return;
12651556Srgrimes	p = s;
12661556Srgrimes	q = cmdnextc;
12671556Srgrimes	while ((c = *p++) != '\0') {
12681556Srgrimes		if (c == CTLESC)
12691556Srgrimes			*q++ = *p++;
12701556Srgrimes		else if (c == CTLVAR) {
12711556Srgrimes			*q++ = '$';
12721556Srgrimes			if (--cmdnleft > 0)
12731556Srgrimes				*q++ = '{';
12741556Srgrimes			subtype = *p++;
12751556Srgrimes		} else if (c == '=' && subtype != 0) {
12761556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
12771556Srgrimes			subtype = 0;
12781556Srgrimes		} else if (c == CTLENDVAR) {
12791556Srgrimes			*q++ = '}';
128018954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
12811556Srgrimes			cmdnleft++;		/* ignore it */
12821556Srgrimes		else
12831556Srgrimes			*q++ = c;
12841556Srgrimes		if (--cmdnleft <= 0) {
12851556Srgrimes			*q++ = '.';
12861556Srgrimes			*q++ = '.';
12871556Srgrimes			*q++ = '.';
12881556Srgrimes			break;
12891556Srgrimes		}
12901556Srgrimes	}
12911556Srgrimes	cmdnextc = q;
12921556Srgrimes}
1293