jobs.c revision 97822
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 97822 2002-06-04 15:26:00Z 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
11297822StjrSTATIC void showjob(struct job *, pid_t, 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)
30297822Stjr			showjob(getjob(id), 0, sformat, lformat);
30397669Stjr
30497669Stjr	return (0);
3051556Srgrimes}
3061556Srgrimes
30797663StjrSTATIC void
30897822Stjrshowjob(struct job *jp, pid_t pid, 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		}
33197822Stjr		if (!lformat && ps != jp->ps && pid == 0)
33297669Stjr			goto skip;
33397822Stjr		if (pid != 0 && pid != ps->pid)
33497822Stjr			goto skip;
33597819Stjr		if (jobno == curr && ps == jp->ps)
33697669Stjr			c = '+';
33797819Stjr		else if (jobno == prev && ps == jp->ps)
33897669Stjr			c = '-';
33997669Stjr		else
34097669Stjr			c = ' ';
34197663Stjr		if (ps == jp->ps)
34297669Stjr			fmtstr(s, 64, "[%d] %c ", jobno, c);
34397663Stjr		else
34497816Stjr			fmtstr(s, 64, "    %c ", c);
34597663Stjr		out1str(s);
34697663Stjr		col = strlen(s);
34797669Stjr		if (lformat) {
34897669Stjr			fmtstr(s, 64, "%d ", ps->pid);
34997669Stjr			out1str(s);
35097669Stjr			col += strlen(s);
35197669Stjr		}
35297663Stjr		s[0] = '\0';
35397819Stjr		if (ps != jp->ps) {
35497819Stjr			*s = '\0';
35597819Stjr		} else if (ps->status == -1) {
35697669Stjr			strcpy(s, "Running");
35797663Stjr		} else if (WIFEXITED(ps->status)) {
35897820Stjr			if (WEXITSTATUS(ps->status) == 0)
35997820Stjr				strcpy(s, "Done");
36097820Stjr			else
36197820Stjr				fmtstr(s, 64, "Done (%d)",
36297820Stjr				    WEXITSTATUS(ps->status));
36397663Stjr		} else {
36497663Stjr#if JOBS
36597663Stjr			if (WIFSTOPPED(ps->status))
36697663Stjr				i = WSTOPSIG(ps->status);
36797663Stjr			else
36897663Stjr#endif
36997663Stjr				i = WTERMSIG(ps->status);
37097663Stjr			if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
37197663Stjr				scopy(sys_siglist[i & 0x7F], s);
37297663Stjr			else
37397663Stjr				fmtstr(s, 64, "Signal %d", i & 0x7F);
37497663Stjr			if (WCOREDUMP(ps->status))
37597663Stjr				strcat(s, " (core dumped)");
37697663Stjr		}
37797663Stjr		out1str(s);
37897663Stjr		col += strlen(s);
37997663Stjr		do {
38097663Stjr			out1c(' ');
38197663Stjr			col++;
38297663Stjr		} while (col < 30);
38397663Stjr		out1str(ps->cmd);
38497663Stjr		out1c('\n');
38597669Stjrskip:		if (--procno <= 0)
38697663Stjr			break;
38797663Stjr	}
38897663Stjr}
38997663Stjr
3901556Srgrimes/*
3911556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
3921556Srgrimes * statuses have changed since the last call to showjobs.
3931556Srgrimes *
3941556Srgrimes * If the shell is interrupted in the process of creating a job, the
3951556Srgrimes * result may be a job structure containing zero processes.  Such structures
3961556Srgrimes * will be freed here.
3971556Srgrimes */
3981556Srgrimes
3991556Srgrimesvoid
40097669Stjrshowjobs(int change, int sformat, int lformat)
40117987Speter{
4021556Srgrimes	int jobno;
4031556Srgrimes	struct job *jp;
4041556Srgrimes
4051556Srgrimes	TRACE(("showjobs(%d) called\n", change));
4061556Srgrimes	while (dowait(0, (struct job *)NULL) > 0);
4071556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
4081556Srgrimes		if (! jp->used)
4091556Srgrimes			continue;
4101556Srgrimes		if (jp->nprocs == 0) {
4111556Srgrimes			freejob(jp);
4121556Srgrimes			continue;
4131556Srgrimes		}
4141556Srgrimes		if (change && ! jp->changed)
4151556Srgrimes			continue;
41697822Stjr		showjob(jp, 0, sformat, lformat);
4171556Srgrimes		jp->changed = 0;
4181556Srgrimes		if (jp->state == JOBDONE) {
4191556Srgrimes			freejob(jp);
4201556Srgrimes		}
4211556Srgrimes	}
4221556Srgrimes}
4231556Srgrimes
4241556Srgrimes
4251556Srgrimes/*
4261556Srgrimes * Mark a job structure as unused.
4271556Srgrimes */
4281556Srgrimes
4291556SrgrimesSTATIC void
43090111Simpfreejob(struct job *jp)
43190111Simp{
4321556Srgrimes	struct procstat *ps;
4331556Srgrimes	int i;
4341556Srgrimes
4351556Srgrimes	INTOFF;
4361556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
4371556Srgrimes		if (ps->cmd != nullstr)
4381556Srgrimes			ckfree(ps->cmd);
4391556Srgrimes	}
4401556Srgrimes	if (jp->ps != &jp->ps0)
4411556Srgrimes		ckfree(jp->ps);
4421556Srgrimes	jp->used = 0;
4431556Srgrimes#if JOBS
44497659Stjr	deljob(jp);
4451556Srgrimes#endif
4461556Srgrimes	INTON;
4471556Srgrimes}
4481556Srgrimes
4491556Srgrimes
4501556Srgrimes
4511556Srgrimesint
45290111Simpwaitcmd(int argc, char **argv)
45317987Speter{
4541556Srgrimes	struct job *job;
45526104Ssteve	int status, retval;
4561556Srgrimes	struct job *jp;
4571556Srgrimes
4581556Srgrimes	if (argc > 1) {
4591556Srgrimes		job = getjob(argv[1]);
4601556Srgrimes	} else {
4611556Srgrimes		job = NULL;
4621556Srgrimes	}
46338536Scracauer
46438536Scracauer	/*
46538536Scracauer	 * Loop until a process is terminated or stopped, or a SIGINT is
46638536Scracauer	 * received.
46738536Scracauer	 */
46838536Scracauer
46938521Scracauer	in_waitcmd++;
47038536Scracauer	do {
4711556Srgrimes		if (job != NULL) {
4721556Srgrimes			if (job->state) {
4731556Srgrimes				status = job->ps[job->nprocs - 1].status;
47426104Ssteve				if (WIFEXITED(status))
47526104Ssteve					retval = WEXITSTATUS(status);
4761556Srgrimes#if JOBS
47726104Ssteve				else if (WIFSTOPPED(status))
47826104Ssteve					retval = WSTOPSIG(status) + 128;
4791556Srgrimes#endif
4801556Srgrimes				else
48126104Ssteve					retval = WTERMSIG(status) + 128;
4821556Srgrimes				if (! iflag)
4831556Srgrimes					freejob(job);
48438536Scracauer				in_waitcmd--;
48526104Ssteve				return retval;
4861556Srgrimes			}
4871556Srgrimes		} else {
4881556Srgrimes			for (jp = jobtab ; ; jp++) {
4891556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
49038536Scracauer					in_waitcmd--;
4911556Srgrimes					return 0;
4921556Srgrimes				}
4931556Srgrimes				if (jp->used && jp->state == 0)
4941556Srgrimes					break;
4951556Srgrimes			}
4961556Srgrimes		}
49738521Scracauer	} while (dowait(1, (struct job *)NULL) != -1);
49838521Scracauer	in_waitcmd--;
49938521Scracauer
50038521Scracauer	return 0;
5011556Srgrimes}
5021556Srgrimes
5031556Srgrimes
5041556Srgrimes
50517987Speterint
50690111Simpjobidcmd(int argc __unused, char **argv)
50717987Speter{
5081556Srgrimes	struct job *jp;
5091556Srgrimes	int i;
5101556Srgrimes
5111556Srgrimes	jp = getjob(argv[1]);
5121556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
5131556Srgrimes		out1fmt("%d", jp->ps[i].pid);
5141556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
5151556Srgrimes	}
5161556Srgrimes	return 0;
5171556Srgrimes}
5181556Srgrimes
5191556Srgrimes
5201556Srgrimes
5211556Srgrimes/*
5221556Srgrimes * Convert a job name to a job structure.
5231556Srgrimes */
5241556Srgrimes
5251556SrgrimesSTATIC struct job *
52690111Simpgetjob(char *name)
52790111Simp{
5281556Srgrimes	int jobno;
52997688Stjr	struct job *found, *jp;
5301556Srgrimes	int pid;
5311556Srgrimes	int i;
5321556Srgrimes
5331556Srgrimes	if (name == NULL) {
5341556Srgrimes#if JOBS
53597659Stjrcurrentjob:	if ((jp = getcurjob(NULL)) == NULL)
5361556Srgrimes			error("No current job");
53797659Stjr		return (jp);
5381556Srgrimes#else
5391556Srgrimes		error("No current job");
5401556Srgrimes#endif
5411556Srgrimes	} else if (name[0] == '%') {
5421556Srgrimes		if (is_digit(name[1])) {
5431556Srgrimes			jobno = number(name + 1);
5441556Srgrimes			if (jobno > 0 && jobno <= njobs
5451556Srgrimes			 && jobtab[jobno - 1].used != 0)
5461556Srgrimes				return &jobtab[jobno - 1];
5471556Srgrimes#if JOBS
5481556Srgrimes		} else if (name[1] == '%' && name[2] == '\0') {
5491556Srgrimes			goto currentjob;
55097688Stjr		} else if (name[1] == '+' && name[2] == '\0') {
55197688Stjr			goto currentjob;
55297688Stjr		} else if (name[1] == '-' && name[2] == '\0') {
55397688Stjr			if ((jp = getcurjob(NULL)) == NULL ||
55497688Stjr			    (jp = getcurjob(jp)) == NULL)
55597688Stjr				error("No previous job");
55697688Stjr			return (jp);
5571556Srgrimes#endif
55897688Stjr		} else if (name[1] == '?') {
55997688Stjr			found = NULL;
56097688Stjr			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
56197688Stjr				if (jp->used && jp->nprocs > 0
56297688Stjr				 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
56397688Stjr					if (found)
56497688Stjr						error("%s: ambiguous", name);
56597688Stjr					found = jp;
56697688Stjr				}
56797688Stjr			}
56897688Stjr			if (found != NULL)
56997688Stjr				return (found);
5701556Srgrimes		} else {
57197688Stjr			found = NULL;
5721556Srgrimes			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
5731556Srgrimes				if (jp->used && jp->nprocs > 0
5741556Srgrimes				 && prefix(name + 1, jp->ps[0].cmd)) {
5751556Srgrimes					if (found)
5761556Srgrimes						error("%s: ambiguous", name);
5771556Srgrimes					found = jp;
5781556Srgrimes				}
5791556Srgrimes			}
5801556Srgrimes			if (found)
5811556Srgrimes				return found;
5821556Srgrimes		}
5831556Srgrimes	} else if (is_number(name)) {
5841556Srgrimes		pid = number(name);
5851556Srgrimes		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
5861556Srgrimes			if (jp->used && jp->nprocs > 0
5871556Srgrimes			 && jp->ps[jp->nprocs - 1].pid == pid)
5881556Srgrimes				return jp;
5891556Srgrimes		}
5901556Srgrimes	}
5911556Srgrimes	error("No such job: %s", name);
59217987Speter	/*NOTREACHED*/
59317987Speter	return NULL;
5941556Srgrimes}
5951556Srgrimes
5961556Srgrimes
5971556Srgrimes
5981556Srgrimes/*
5991556Srgrimes * Return a new job structure,
6001556Srgrimes */
6011556Srgrimes
6021556Srgrimesstruct job *
60390111Simpmakejob(union node *node __unused, int nprocs)
60417987Speter{
6051556Srgrimes	int i;
6061556Srgrimes	struct job *jp;
6071556Srgrimes
6081556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
6091556Srgrimes		if (--i < 0) {
6101556Srgrimes			INTOFF;
6111556Srgrimes			if (njobs == 0) {
6121556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
61397664Stjr#if JOBS
61497659Stjr				jobmru = NULL;
61597664Stjr#endif
6161556Srgrimes			} else {
6171556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
61817987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
61997659Stjr#if JOBS
62097659Stjr				/* Relocate `next' pointers and list head */
62197659Stjr				jobmru = &jp[jobmru - jobtab];
62297659Stjr				for (i = 0; i < njobs; i++)
62397659Stjr					if (jp[i].next != NULL)
62497659Stjr						jp[i].next = &jp[jp[i].next -
62597659Stjr						    jobtab];
62697659Stjr#endif
62720425Ssteve				/* Relocate `ps' pointers */
62820425Ssteve				for (i = 0; i < njobs; i++)
62920425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
63020425Ssteve						jp[i].ps = &jp[i].ps0;
6311556Srgrimes				ckfree(jobtab);
6321556Srgrimes				jobtab = jp;
6331556Srgrimes			}
6341556Srgrimes			jp = jobtab + njobs;
6351556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
6361556Srgrimes			INTON;
6371556Srgrimes			break;
6381556Srgrimes		}
6391556Srgrimes		if (jp->used == 0)
6401556Srgrimes			break;
6411556Srgrimes	}
6421556Srgrimes	INTOFF;
6431556Srgrimes	jp->state = 0;
6441556Srgrimes	jp->used = 1;
6451556Srgrimes	jp->changed = 0;
6461556Srgrimes	jp->nprocs = 0;
6471556Srgrimes#if JOBS
6481556Srgrimes	jp->jobctl = jobctl;
64997659Stjr	jp->next = NULL;
6501556Srgrimes#endif
6511556Srgrimes	if (nprocs > 1) {
6521556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
6531556Srgrimes	} else {
6541556Srgrimes		jp->ps = &jp->ps0;
6551556Srgrimes	}
6561556Srgrimes	INTON;
65717987Speter	TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
65817987Speter	    jp - jobtab + 1));
6591556Srgrimes	return jp;
6608855Srgrimes}
6611556Srgrimes
66297659Stjr#if JOBS
66397659StjrSTATIC void
66497659Stjrsetcurjob(struct job *cj)
66597659Stjr{
66697659Stjr	struct job *jp, *prev;
6671556Srgrimes
66897659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
66997659Stjr		if (jp == cj) {
67097659Stjr			if (prev != NULL)
67197659Stjr				prev->next = jp->next;
67297659Stjr			else
67397659Stjr				jobmru = jp->next;
67497659Stjr			jp->next = jobmru;
67597659Stjr			jobmru = cj;
67697659Stjr			return;
67797659Stjr		}
67897659Stjr	}
67997659Stjr	cj->next = jobmru;
68097659Stjr	jobmru = cj;
68197659Stjr}
68297659Stjr
68397659StjrSTATIC void
68497659Stjrdeljob(struct job *j)
68597659Stjr{
68697659Stjr	struct job *jp, *prev;
68797659Stjr
68897659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
68997659Stjr		if (jp == j) {
69097659Stjr			if (prev != NULL)
69197659Stjr				prev->next = jp->next;
69297659Stjr			else
69397659Stjr				jobmru = jp->next;
69497659Stjr			return;
69597659Stjr		}
69697659Stjr	}
69797659Stjr}
69897659Stjr
6991556Srgrimes/*
70097659Stjr * Return the most recently used job that isn't `nj', and preferably one
70197659Stjr * that is stopped.
70297659Stjr */
70397659StjrSTATIC struct job *
70497659Stjrgetcurjob(struct job *nj)
70597659Stjr{
70697659Stjr	struct job *jp;
70797659Stjr
70897659Stjr	/* Try to find a stopped one.. */
70997659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
71097659Stjr		if (jp->used && jp != nj && jp->state == JOBSTOPPED)
71197659Stjr			return (jp);
71297659Stjr	/* Otherwise the most recently used job that isn't `nj' */
71397659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
71497659Stjr		if (jp->used && jp != nj)
71597659Stjr			return (jp);
71697659Stjr
71797659Stjr	return (NULL);
71897659Stjr}
71997659Stjr
72097659Stjr#endif
72197659Stjr
72297659Stjr/*
7231556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
7241556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
7251556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
7261556Srgrimes * be NULL.  The mode parameter can be one of the following:
7271556Srgrimes *	FORK_FG - Fork off a foreground process.
7281556Srgrimes *	FORK_BG - Fork off a background process.
7291556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
7301556Srgrimes *		     process group even if job control is on.
7311556Srgrimes *
7321556Srgrimes * When job control is turned off, background processes have their standard
7331556Srgrimes * input redirected to /dev/null (except for the second and later processes
7341556Srgrimes * in a pipeline).
7351556Srgrimes */
7361556Srgrimes
7371556Srgrimesint
73890111Simpforkshell(struct job *jp, union node *n, int mode)
73917987Speter{
7401556Srgrimes	int pid;
7411556Srgrimes	int pgrp;
7421556Srgrimes
74317987Speter	TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
74417987Speter	    mode));
7451556Srgrimes	INTOFF;
7461556Srgrimes	pid = fork();
7471556Srgrimes	if (pid == -1) {
7481556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
7491556Srgrimes		INTON;
75053891Scracauer		error("Cannot fork: %s", strerror(errno));
7511556Srgrimes	}
7521556Srgrimes	if (pid == 0) {
7531556Srgrimes		struct job *p;
7541556Srgrimes		int wasroot;
7551556Srgrimes		int i;
7561556Srgrimes
7571556Srgrimes		TRACE(("Child shell %d\n", getpid()));
7581556Srgrimes		wasroot = rootshell;
7591556Srgrimes		rootshell = 0;
7601556Srgrimes		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
7611556Srgrimes			if (p->used)
7621556Srgrimes				freejob(p);
7631556Srgrimes		closescript();
7641556Srgrimes		INTON;
7651556Srgrimes		clear_traps();
7661556Srgrimes#if JOBS
7671556Srgrimes		jobctl = 0;		/* do job control only in root shell */
7681556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
7691556Srgrimes			if (jp == NULL || jp->nprocs == 0)
7701556Srgrimes				pgrp = getpid();
7711556Srgrimes			else
7721556Srgrimes				pgrp = jp->ps[0].pid;
77321352Ssteve			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
7741556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
77520425Ssteve#ifdef OLD_TTY_DRIVER
7761556Srgrimes				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
77718016Speter					error("TIOCSPGRP failed, errno=%d", errno);
77820425Ssteve#else
77920425Ssteve				if (tcsetpgrp(2, pgrp) < 0)
78020425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
78120425Ssteve#endif
7821556Srgrimes			}
7831556Srgrimes			setsignal(SIGTSTP);
7841556Srgrimes			setsignal(SIGTTOU);
7851556Srgrimes		} else if (mode == FORK_BG) {
7861556Srgrimes			ignoresig(SIGINT);
7871556Srgrimes			ignoresig(SIGQUIT);
7881556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
7891556Srgrimes			    ! fd0_redirected_p ()) {
7901556Srgrimes				close(0);
79169793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
79269793Sobrien					error("Can't open %s: %s",
79369793Sobrien					    _PATH_DEVNULL, strerror(errno));
7941556Srgrimes			}
7951556Srgrimes		}
7961556Srgrimes#else
7971556Srgrimes		if (mode == FORK_BG) {
7981556Srgrimes			ignoresig(SIGINT);
7991556Srgrimes			ignoresig(SIGQUIT);
8001556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
8011556Srgrimes			    ! fd0_redirected_p ()) {
8021556Srgrimes				close(0);
80369793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
80469793Sobrien					error("Can't open %s: %s",
80569793Sobrien					    _PATH_DEVNULL, strerror(errno));
8061556Srgrimes			}
8071556Srgrimes		}
8081556Srgrimes#endif
8091556Srgrimes		if (wasroot && iflag) {
8101556Srgrimes			setsignal(SIGINT);
8111556Srgrimes			setsignal(SIGQUIT);
8121556Srgrimes			setsignal(SIGTERM);
8131556Srgrimes		}
8141556Srgrimes		return pid;
8151556Srgrimes	}
8161556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
8171556Srgrimes		if (jp == NULL || jp->nprocs == 0)
8181556Srgrimes			pgrp = pid;
8191556Srgrimes		else
8201556Srgrimes			pgrp = jp->ps[0].pid;
82117987Speter		setpgid(pid, pgrp);
8221556Srgrimes	}
8231556Srgrimes	if (mode == FORK_BG)
8241556Srgrimes		backgndpid = pid;		/* set $! */
8251556Srgrimes	if (jp) {
8261556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
8271556Srgrimes		ps->pid = pid;
8281556Srgrimes		ps->status = -1;
8291556Srgrimes		ps->cmd = nullstr;
8301556Srgrimes		if (iflag && rootshell && n)
8311556Srgrimes			ps->cmd = commandtext(n);
83297659Stjr#if JOBS
83397659Stjr		setcurjob(jp);
83497659Stjr#endif
8351556Srgrimes	}
8361556Srgrimes	INTON;
8371556Srgrimes	TRACE(("In parent shell:  child = %d\n", pid));
8381556Srgrimes	return pid;
8391556Srgrimes}
8401556Srgrimes
8411556Srgrimes
8421556Srgrimes
8431556Srgrimes/*
8441556Srgrimes * Wait for job to finish.
8451556Srgrimes *
8461556Srgrimes * Under job control we have the problem that while a child process is
8471556Srgrimes * running interrupts generated by the user are sent to the child but not
8481556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
8491556Srgrimes * active user may be hard to kill.  With job control turned off, an
8501556Srgrimes * interactive user may place an interactive program inside a loop.  If
8511556Srgrimes * the interactive program catches interrupts, the user doesn't want
8521556Srgrimes * these interrupts to also abort the loop.  The approach we take here
8531556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
85446684Skris * foreground process to terminate, and then send itself an interrupt
8551556Srgrimes * signal if the child process was terminated by an interrupt signal.
8561556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
8571556Srgrimes * exit on interrupt; unless these processes terminate themselves by
8581556Srgrimes * sending a signal to themselves (instead of calling exit) they will
8591556Srgrimes * confuse this approach.
8601556Srgrimes */
8611556Srgrimes
8621556Srgrimesint
86390111Simpwaitforjob(struct job *jp, int *origstatus)
86445916Scracauer{
8651556Srgrimes#if JOBS
86617987Speter	int mypgrp = getpgrp();
8671556Srgrimes#endif
8681556Srgrimes	int status;
8691556Srgrimes	int st;
8701556Srgrimes
8711556Srgrimes	INTOFF;
8721556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
87338950Scracauer	while (jp->state == 0)
87438950Scracauer		if (dowait(1, jp) == -1)
87538950Scracauer			dotrap();
8761556Srgrimes#if JOBS
8771556Srgrimes	if (jp->jobctl) {
87820425Ssteve#ifdef OLD_TTY_DRIVER
8791556Srgrimes		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
88020425Ssteve			error("TIOCSPGRP failed, errno=%d\n", errno);
88120425Ssteve#else
88220425Ssteve		if (tcsetpgrp(2, mypgrp) < 0)
88320425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
88420425Ssteve#endif
8851556Srgrimes	}
8861556Srgrimes	if (jp->state == JOBSTOPPED)
88797659Stjr		setcurjob(jp);
8881556Srgrimes#endif
8891556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
89045916Scracauer	if (origstatus != NULL)
89145916Scracauer		*origstatus = status;
8921556Srgrimes	/* convert to 8 bits */
89326104Ssteve	if (WIFEXITED(status))
89426104Ssteve		st = WEXITSTATUS(status);
8951556Srgrimes#if JOBS
89626104Ssteve	else if (WIFSTOPPED(status))
89726104Ssteve		st = WSTOPSIG(status) + 128;
8981556Srgrimes#endif
8991556Srgrimes	else
90026104Ssteve		st = WTERMSIG(status) + 128;
9011556Srgrimes	if (! JOBS || jp->state == JOBDONE)
9021556Srgrimes		freejob(jp);
90338521Scracauer	if (int_pending()) {
90438521Scracauer		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
90538521Scracauer			kill(getpid(), SIGINT);
90638521Scracauer		else
90738521Scracauer			CLEAR_PENDING_INT;
90838521Scracauer	}
9091556Srgrimes	INTON;
9101556Srgrimes	return st;
9111556Srgrimes}
9121556Srgrimes
9131556Srgrimes
9141556Srgrimes
9151556Srgrimes/*
9161556Srgrimes * Wait for a process to terminate.
9171556Srgrimes */
9181556Srgrimes
9191556SrgrimesSTATIC int
92090111Simpdowait(int block, struct job *job)
92117987Speter{
9221556Srgrimes	int pid;
9231556Srgrimes	int status;
9241556Srgrimes	struct procstat *sp;
9251556Srgrimes	struct job *jp;
9261556Srgrimes	struct job *thisjob;
9271556Srgrimes	int done;
9281556Srgrimes	int stopped;
9291556Srgrimes	int core;
93026104Ssteve	int sig;
9311556Srgrimes
93238950Scracauer	in_dowait++;
9331556Srgrimes	TRACE(("dowait(%d) called\n", block));
9341556Srgrimes	do {
9351556Srgrimes		pid = waitproc(block, &status);
9361556Srgrimes		TRACE(("wait returns %d, status=%d\n", pid, status));
93772086Scracauer	} while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
93872086Scracauer	    (WIFSTOPPED(status) && !iflag));
93938950Scracauer	in_dowait--;
94038521Scracauer	if (breakwaitcmd != 0) {
94138521Scracauer		breakwaitcmd = 0;
94238521Scracauer		return -1;
94338521Scracauer	}
9441556Srgrimes	if (pid <= 0)
9451556Srgrimes		return pid;
9461556Srgrimes	INTOFF;
9471556Srgrimes	thisjob = NULL;
9481556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
9491556Srgrimes		if (jp->used) {
9501556Srgrimes			done = 1;
9511556Srgrimes			stopped = 1;
9521556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
9531556Srgrimes				if (sp->pid == -1)
9541556Srgrimes					continue;
9551556Srgrimes				if (sp->pid == pid) {
95626104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
95726104Ssteve						   pid, sp->status, status));
9581556Srgrimes					sp->status = status;
9591556Srgrimes					thisjob = jp;
9601556Srgrimes				}
9611556Srgrimes				if (sp->status == -1)
9621556Srgrimes					stopped = 0;
96326104Ssteve				else if (WIFSTOPPED(sp->status))
9641556Srgrimes					done = 0;
9651556Srgrimes			}
9661556Srgrimes			if (stopped) {		/* stopped or done */
9671556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
9681556Srgrimes				if (jp->state != state) {
9691556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
9701556Srgrimes					jp->state = state;
9711556Srgrimes#if JOBS
97297659Stjr					if (done)
97397659Stjr						deljob(jp);
9741556Srgrimes#endif
9751556Srgrimes				}
9761556Srgrimes			}
9771556Srgrimes		}
9781556Srgrimes	}
9791556Srgrimes	INTON;
9801556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
9811556Srgrimes#if JOBS
98226104Ssteve		if (WIFSTOPPED(status))
98326104Ssteve			sig = WSTOPSIG(status);
98426104Ssteve		else
9851556Srgrimes#endif
98697663Stjr		{
98726104Ssteve			if (WIFEXITED(status))
98826104Ssteve				sig = 0;
98926104Ssteve			else
99026104Ssteve				sig = WTERMSIG(status);
9911556Srgrimes		}
99297663Stjr		if (sig != 0 && sig != SIGINT && sig != SIGPIPE)
99397822Stjr			showjob(thisjob, pid, 0, 1);
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
10421556SrgrimesSTATIC int
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) {
12341556Srgrimes			s[0] = n->ndup.dupfd + '0';
12351556Srgrimes			s[1] = '\0';
12361556Srgrimes			cmdputs(s);
12371556Srgrimes		} else {
12381556Srgrimes			cmdtxt(n->nfile.fname);
12391556Srgrimes		}
12401556Srgrimes		break;
12411556Srgrimes	case NHERE:
12421556Srgrimes	case NXHERE:
12431556Srgrimes		cmdputs("<<...");
12441556Srgrimes		break;
12451556Srgrimes	default:
12461556Srgrimes		cmdputs("???");
12471556Srgrimes		break;
12481556Srgrimes	}
12491556Srgrimes}
12501556Srgrimes
12511556Srgrimes
12521556Srgrimes
12531556SrgrimesSTATIC void
125490111Simpcmdputs(char *s)
125590111Simp{
125625222Ssteve	char *p, *q;
125725222Ssteve	char c;
12581556Srgrimes	int subtype = 0;
12591556Srgrimes
12601556Srgrimes	if (cmdnleft <= 0)
12611556Srgrimes		return;
12621556Srgrimes	p = s;
12631556Srgrimes	q = cmdnextc;
12641556Srgrimes	while ((c = *p++) != '\0') {
12651556Srgrimes		if (c == CTLESC)
12661556Srgrimes			*q++ = *p++;
12671556Srgrimes		else if (c == CTLVAR) {
12681556Srgrimes			*q++ = '$';
12691556Srgrimes			if (--cmdnleft > 0)
12701556Srgrimes				*q++ = '{';
12711556Srgrimes			subtype = *p++;
12721556Srgrimes		} else if (c == '=' && subtype != 0) {
12731556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
12741556Srgrimes			subtype = 0;
12751556Srgrimes		} else if (c == CTLENDVAR) {
12761556Srgrimes			*q++ = '}';
127718954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
12781556Srgrimes			cmdnleft++;		/* ignore it */
12791556Srgrimes		else
12801556Srgrimes			*q++ = c;
12811556Srgrimes		if (--cmdnleft <= 0) {
12821556Srgrimes			*q++ = '.';
12831556Srgrimes			*q++ = '.';
12841556Srgrimes			*q++ = '.';
12851556Srgrimes			break;
12861556Srgrimes		}
12871556Srgrimes	}
12881556Srgrimes	cmdnextc = q;
12891556Srgrimes}
1290