jobs.c revision 97660
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 97660 2002-05-31 12:35:34Z 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
1121556Srgrimes
1131556Srgrimes
1141556Srgrimes/*
1151556Srgrimes * Turn job control on and off.
1161556Srgrimes *
1171556Srgrimes * Note:  This code assumes that the third arg to ioctl is a character
1181556Srgrimes * pointer, which is true on Berkeley systems but not System V.  Since
1191556Srgrimes * System V doesn't have job control yet, this isn't a problem now.
1201556Srgrimes */
1211556Srgrimes
1221556SrgrimesMKINIT int jobctl;
1231556Srgrimes
12420425Ssteve#if JOBS
1251556Srgrimesvoid
12690111Simpsetjobctl(int on)
12717987Speter{
1281556Srgrimes#ifdef OLD_TTY_DRIVER
1291556Srgrimes	int ldisc;
1301556Srgrimes#endif
1311556Srgrimes
1321556Srgrimes	if (on == jobctl || rootshell == 0)
1331556Srgrimes		return;
1341556Srgrimes	if (on) {
1351556Srgrimes		do { /* while we are in the background */
13620425Ssteve#ifdef OLD_TTY_DRIVER
1371556Srgrimes			if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) {
13820425Ssteve#else
13920425Ssteve			initialpgrp = tcgetpgrp(2);
14020425Ssteve			if (initialpgrp < 0) {
14120425Ssteve#endif
1421556Srgrimes				out2str("sh: can't access tty; job control turned off\n");
1431556Srgrimes				mflag = 0;
1441556Srgrimes				return;
1451556Srgrimes			}
1461556Srgrimes			if (initialpgrp == -1)
14717987Speter				initialpgrp = getpgrp();
14817987Speter			else if (initialpgrp != getpgrp()) {
1491556Srgrimes				killpg(initialpgrp, SIGTTIN);
1501556Srgrimes				continue;
1511556Srgrimes			}
1521556Srgrimes		} while (0);
1531556Srgrimes#ifdef OLD_TTY_DRIVER
1541556Srgrimes		if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) {
1551556Srgrimes			out2str("sh: need new tty driver to run job control; job control turned off\n");
1561556Srgrimes			mflag = 0;
1571556Srgrimes			return;
1581556Srgrimes		}
1591556Srgrimes#endif
1601556Srgrimes		setsignal(SIGTSTP);
1611556Srgrimes		setsignal(SIGTTOU);
1621556Srgrimes		setsignal(SIGTTIN);
16317987Speter		setpgid(0, rootpid);
16420425Ssteve#ifdef OLD_TTY_DRIVER
1651556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&rootpid);
16620425Ssteve#else
16720425Ssteve		tcsetpgrp(2, rootpid);
16820425Ssteve#endif
1691556Srgrimes	} else { /* turning job control off */
17017987Speter		setpgid(0, initialpgrp);
17120425Ssteve#ifdef OLD_TTY_DRIVER
1721556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&initialpgrp);
17320425Ssteve#else
17420425Ssteve		tcsetpgrp(2, initialpgrp);
17520425Ssteve#endif
1761556Srgrimes		setsignal(SIGTSTP);
1771556Srgrimes		setsignal(SIGTTOU);
1781556Srgrimes		setsignal(SIGTTIN);
1791556Srgrimes	}
1801556Srgrimes	jobctl = on;
1811556Srgrimes}
18220425Ssteve#endif
1831556Srgrimes
1841556Srgrimes
1851556Srgrimes#ifdef mkinit
18628346SsteveINCLUDE <sys/types.h>
18717987SpeterINCLUDE <stdlib.h>
1881556Srgrimes
1891556SrgrimesSHELLPROC {
1901556Srgrimes	backgndpid = -1;
1911556Srgrimes#if JOBS
1921556Srgrimes	jobctl = 0;
1931556Srgrimes#endif
1941556Srgrimes}
1951556Srgrimes
1961556Srgrimes#endif
1971556Srgrimes
1981556Srgrimes
1991556Srgrimes
2001556Srgrimes#if JOBS
20117987Speterint
20290111Simpfgcmd(int argc __unused, char **argv)
20317987Speter{
2041556Srgrimes	struct job *jp;
2051556Srgrimes	int pgrp;
2061556Srgrimes	int status;
2071556Srgrimes
2081556Srgrimes	jp = getjob(argv[1]);
2091556Srgrimes	if (jp->jobctl == 0)
2101556Srgrimes		error("job not created under job control");
21196933Stjr	out1str(jp->ps[0].cmd);
21296933Stjr	out1c('\n');
21396933Stjr	flushout(&output);
2141556Srgrimes	pgrp = jp->ps[0].pid;
21520425Ssteve#ifdef OLD_TTY_DRIVER
2161556Srgrimes	ioctl(2, TIOCSPGRP, (char *)&pgrp);
21720425Ssteve#else
21820425Ssteve	tcsetpgrp(2, pgrp);
21920425Ssteve#endif
2201556Srgrimes	restartjob(jp);
2211556Srgrimes	INTOFF;
22245916Scracauer	status = waitforjob(jp, (int *)NULL);
2231556Srgrimes	INTON;
2241556Srgrimes	return status;
2251556Srgrimes}
2261556Srgrimes
2271556Srgrimes
22817987Speterint
22990111Simpbgcmd(int argc, char **argv)
23017987Speter{
23196933Stjr	char s[64];
2321556Srgrimes	struct job *jp;
2331556Srgrimes
2341556Srgrimes	do {
2351556Srgrimes		jp = getjob(*++argv);
2361556Srgrimes		if (jp->jobctl == 0)
2371556Srgrimes			error("job not created under job control");
23896933Stjr		if (jp->state == JOBDONE)
23996933Stjr			continue;
2401556Srgrimes		restartjob(jp);
24196933Stjr		fmtstr(s, 64, "[%d] ", jp - jobtab + 1);
24296933Stjr		out1str(s);
24396933Stjr		out1str(jp->ps[0].cmd);
24496933Stjr		out1c('\n');
2451556Srgrimes	} while (--argc > 1);
2461556Srgrimes	return 0;
2471556Srgrimes}
2481556Srgrimes
2491556Srgrimes
2501556SrgrimesSTATIC void
25190111Simprestartjob(struct job *jp)
25217987Speter{
2531556Srgrimes	struct procstat *ps;
2541556Srgrimes	int i;
2551556Srgrimes
2561556Srgrimes	if (jp->state == JOBDONE)
2571556Srgrimes		return;
25897660Stjr	setcurjob(jp);
2591556Srgrimes	INTOFF;
2601556Srgrimes	killpg(jp->ps[0].pid, SIGCONT);
2611556Srgrimes	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
26226104Ssteve		if (WIFSTOPPED(ps->status)) {
2631556Srgrimes			ps->status = -1;
2641556Srgrimes			jp->state = 0;
2651556Srgrimes		}
2661556Srgrimes	}
2671556Srgrimes	INTON;
2681556Srgrimes}
2691556Srgrimes#endif
2701556Srgrimes
2711556Srgrimes
2721556Srgrimesint
27390111Simpjobscmd(int argc __unused, char **argv __unused)
27417987Speter{
2751556Srgrimes	showjobs(0);
2761556Srgrimes	return 0;
2771556Srgrimes}
2781556Srgrimes
2791556Srgrimes
2801556Srgrimes/*
2811556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
2821556Srgrimes * statuses have changed since the last call to showjobs.
2831556Srgrimes *
2841556Srgrimes * If the shell is interrupted in the process of creating a job, the
2851556Srgrimes * result may be a job structure containing zero processes.  Such structures
2861556Srgrimes * will be freed here.
2871556Srgrimes */
2881556Srgrimes
2891556Srgrimesvoid
29090111Simpshowjobs(int change)
29117987Speter{
2921556Srgrimes	int jobno;
2931556Srgrimes	int procno;
2941556Srgrimes	int i;
2951556Srgrimes	struct job *jp;
2961556Srgrimes	struct procstat *ps;
2971556Srgrimes	int col;
2981556Srgrimes	char s[64];
2991556Srgrimes
3001556Srgrimes	TRACE(("showjobs(%d) called\n", change));
3011556Srgrimes	while (dowait(0, (struct job *)NULL) > 0);
3021556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
3031556Srgrimes		if (! jp->used)
3041556Srgrimes			continue;
3051556Srgrimes		if (jp->nprocs == 0) {
3061556Srgrimes			freejob(jp);
3071556Srgrimes			continue;
3081556Srgrimes		}
3091556Srgrimes		if (change && ! jp->changed)
3101556Srgrimes			continue;
3111556Srgrimes		procno = jp->nprocs;
3121556Srgrimes		for (ps = jp->ps ; ; ps++) {	/* for each process */
3131556Srgrimes			if (ps == jp->ps)
3141556Srgrimes				fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
3151556Srgrimes			else
3161556Srgrimes				fmtstr(s, 64, "    %d ", ps->pid);
3171556Srgrimes			out1str(s);
3181556Srgrimes			col = strlen(s);
3191556Srgrimes			s[0] = '\0';
3201556Srgrimes			if (ps->status == -1) {
3211556Srgrimes				/* don't print anything */
32226104Ssteve			} else if (WIFEXITED(ps->status)) {
32326104Ssteve				fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status));
3241556Srgrimes			} else {
3251556Srgrimes#if JOBS
32626104Ssteve				if (WIFSTOPPED(ps->status))
32726104Ssteve					i = WSTOPSIG(ps->status);
32826104Ssteve				else
3291556Srgrimes#endif
33026104Ssteve					i = WTERMSIG(ps->status);
33117987Speter				if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
33217987Speter					scopy(sys_siglist[i & 0x7F], s);
3331556Srgrimes				else
3341556Srgrimes					fmtstr(s, 64, "Signal %d", i & 0x7F);
33526104Ssteve				if (WCOREDUMP(ps->status))
3361556Srgrimes					strcat(s, " (core dumped)");
3371556Srgrimes			}
3381556Srgrimes			out1str(s);
3391556Srgrimes			col += strlen(s);
3401556Srgrimes			do {
3411556Srgrimes				out1c(' ');
3421556Srgrimes				col++;
3431556Srgrimes			} while (col < 30);
3441556Srgrimes			out1str(ps->cmd);
3451556Srgrimes			out1c('\n');
3461556Srgrimes			if (--procno <= 0)
3471556Srgrimes				break;
3481556Srgrimes		}
3491556Srgrimes		jp->changed = 0;
3501556Srgrimes		if (jp->state == JOBDONE) {
3511556Srgrimes			freejob(jp);
3521556Srgrimes		}
3531556Srgrimes	}
3541556Srgrimes}
3551556Srgrimes
3561556Srgrimes
3571556Srgrimes/*
3581556Srgrimes * Mark a job structure as unused.
3591556Srgrimes */
3601556Srgrimes
3611556SrgrimesSTATIC void
36290111Simpfreejob(struct job *jp)
36390111Simp{
3641556Srgrimes	struct procstat *ps;
3651556Srgrimes	int i;
3661556Srgrimes
3671556Srgrimes	INTOFF;
3681556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
3691556Srgrimes		if (ps->cmd != nullstr)
3701556Srgrimes			ckfree(ps->cmd);
3711556Srgrimes	}
3721556Srgrimes	if (jp->ps != &jp->ps0)
3731556Srgrimes		ckfree(jp->ps);
3741556Srgrimes	jp->used = 0;
3751556Srgrimes#if JOBS
37697659Stjr	deljob(jp);
3771556Srgrimes#endif
3781556Srgrimes	INTON;
3791556Srgrimes}
3801556Srgrimes
3811556Srgrimes
3821556Srgrimes
3831556Srgrimesint
38490111Simpwaitcmd(int argc, char **argv)
38517987Speter{
3861556Srgrimes	struct job *job;
38726104Ssteve	int status, retval;
3881556Srgrimes	struct job *jp;
3891556Srgrimes
3901556Srgrimes	if (argc > 1) {
3911556Srgrimes		job = getjob(argv[1]);
3921556Srgrimes	} else {
3931556Srgrimes		job = NULL;
3941556Srgrimes	}
39538536Scracauer
39638536Scracauer	/*
39738536Scracauer	 * Loop until a process is terminated or stopped, or a SIGINT is
39838536Scracauer	 * received.
39938536Scracauer	 */
40038536Scracauer
40138521Scracauer	in_waitcmd++;
40238536Scracauer	do {
4031556Srgrimes		if (job != NULL) {
4041556Srgrimes			if (job->state) {
4051556Srgrimes				status = job->ps[job->nprocs - 1].status;
40626104Ssteve				if (WIFEXITED(status))
40726104Ssteve					retval = WEXITSTATUS(status);
4081556Srgrimes#if JOBS
40926104Ssteve				else if (WIFSTOPPED(status))
41026104Ssteve					retval = WSTOPSIG(status) + 128;
4111556Srgrimes#endif
4121556Srgrimes				else
41326104Ssteve					retval = WTERMSIG(status) + 128;
4141556Srgrimes				if (! iflag)
4151556Srgrimes					freejob(job);
41638536Scracauer				in_waitcmd--;
41726104Ssteve				return retval;
4181556Srgrimes			}
4191556Srgrimes		} else {
4201556Srgrimes			for (jp = jobtab ; ; jp++) {
4211556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
42238536Scracauer					in_waitcmd--;
4231556Srgrimes					return 0;
4241556Srgrimes				}
4251556Srgrimes				if (jp->used && jp->state == 0)
4261556Srgrimes					break;
4271556Srgrimes			}
4281556Srgrimes		}
42938521Scracauer	} while (dowait(1, (struct job *)NULL) != -1);
43038521Scracauer	in_waitcmd--;
43138521Scracauer
43238521Scracauer	return 0;
4331556Srgrimes}
4341556Srgrimes
4351556Srgrimes
4361556Srgrimes
43717987Speterint
43890111Simpjobidcmd(int argc __unused, char **argv)
43917987Speter{
4401556Srgrimes	struct job *jp;
4411556Srgrimes	int i;
4421556Srgrimes
4431556Srgrimes	jp = getjob(argv[1]);
4441556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
4451556Srgrimes		out1fmt("%d", jp->ps[i].pid);
4461556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
4471556Srgrimes	}
4481556Srgrimes	return 0;
4491556Srgrimes}
4501556Srgrimes
4511556Srgrimes
4521556Srgrimes
4531556Srgrimes/*
4541556Srgrimes * Convert a job name to a job structure.
4551556Srgrimes */
4561556Srgrimes
4571556SrgrimesSTATIC struct job *
45890111Simpgetjob(char *name)
45990111Simp{
4601556Srgrimes	int jobno;
46125222Ssteve	struct job *jp;
4621556Srgrimes	int pid;
4631556Srgrimes	int i;
4641556Srgrimes
4651556Srgrimes	if (name == NULL) {
4661556Srgrimes#if JOBS
46797659Stjrcurrentjob:	if ((jp = getcurjob(NULL)) == NULL)
4681556Srgrimes			error("No current job");
46997659Stjr		return (jp);
4701556Srgrimes#else
4711556Srgrimes		error("No current job");
4721556Srgrimes#endif
4731556Srgrimes	} else if (name[0] == '%') {
4741556Srgrimes		if (is_digit(name[1])) {
4751556Srgrimes			jobno = number(name + 1);
4761556Srgrimes			if (jobno > 0 && jobno <= njobs
4771556Srgrimes			 && jobtab[jobno - 1].used != 0)
4781556Srgrimes				return &jobtab[jobno - 1];
4791556Srgrimes#if JOBS
4801556Srgrimes		} else if (name[1] == '%' && name[2] == '\0') {
4811556Srgrimes			goto currentjob;
4821556Srgrimes#endif
4831556Srgrimes		} else {
48425222Ssteve			struct job *found = NULL;
4851556Srgrimes			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
4861556Srgrimes				if (jp->used && jp->nprocs > 0
4871556Srgrimes				 && prefix(name + 1, jp->ps[0].cmd)) {
4881556Srgrimes					if (found)
4891556Srgrimes						error("%s: ambiguous", name);
4901556Srgrimes					found = jp;
4911556Srgrimes				}
4921556Srgrimes			}
4931556Srgrimes			if (found)
4941556Srgrimes				return found;
4951556Srgrimes		}
4961556Srgrimes	} else if (is_number(name)) {
4971556Srgrimes		pid = number(name);
4981556Srgrimes		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
4991556Srgrimes			if (jp->used && jp->nprocs > 0
5001556Srgrimes			 && jp->ps[jp->nprocs - 1].pid == pid)
5011556Srgrimes				return jp;
5021556Srgrimes		}
5031556Srgrimes	}
5041556Srgrimes	error("No such job: %s", name);
50517987Speter	/*NOTREACHED*/
50617987Speter	return NULL;
5071556Srgrimes}
5081556Srgrimes
5091556Srgrimes
5101556Srgrimes
5111556Srgrimes/*
5121556Srgrimes * Return a new job structure,
5131556Srgrimes */
5141556Srgrimes
5151556Srgrimesstruct job *
51690111Simpmakejob(union node *node __unused, int nprocs)
51717987Speter{
5181556Srgrimes	int i;
5191556Srgrimes	struct job *jp;
5201556Srgrimes
5211556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
5221556Srgrimes		if (--i < 0) {
5231556Srgrimes			INTOFF;
5241556Srgrimes			if (njobs == 0) {
5251556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
52697659Stjr				jobmru = NULL;
5271556Srgrimes			} else {
5281556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
52917987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
53097659Stjr#if JOBS
53197659Stjr				/* Relocate `next' pointers and list head */
53297659Stjr				jobmru = &jp[jobmru - jobtab];
53397659Stjr				for (i = 0; i < njobs; i++)
53497659Stjr					if (jp[i].next != NULL)
53597659Stjr						jp[i].next = &jp[jp[i].next -
53697659Stjr						    jobtab];
53797659Stjr#endif
53820425Ssteve				/* Relocate `ps' pointers */
53920425Ssteve				for (i = 0; i < njobs; i++)
54020425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
54120425Ssteve						jp[i].ps = &jp[i].ps0;
5421556Srgrimes				ckfree(jobtab);
5431556Srgrimes				jobtab = jp;
5441556Srgrimes			}
5451556Srgrimes			jp = jobtab + njobs;
5461556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
5471556Srgrimes			INTON;
5481556Srgrimes			break;
5491556Srgrimes		}
5501556Srgrimes		if (jp->used == 0)
5511556Srgrimes			break;
5521556Srgrimes	}
5531556Srgrimes	INTOFF;
5541556Srgrimes	jp->state = 0;
5551556Srgrimes	jp->used = 1;
5561556Srgrimes	jp->changed = 0;
5571556Srgrimes	jp->nprocs = 0;
5581556Srgrimes#if JOBS
5591556Srgrimes	jp->jobctl = jobctl;
56097659Stjr	jp->next = NULL;
5611556Srgrimes#endif
5621556Srgrimes	if (nprocs > 1) {
5631556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
5641556Srgrimes	} else {
5651556Srgrimes		jp->ps = &jp->ps0;
5661556Srgrimes	}
5671556Srgrimes	INTON;
56817987Speter	TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
56917987Speter	    jp - jobtab + 1));
5701556Srgrimes	return jp;
5718855Srgrimes}
5721556Srgrimes
57397659Stjr#if JOBS
57497659StjrSTATIC void
57597659Stjrsetcurjob(struct job *cj)
57697659Stjr{
57797659Stjr	struct job *jp, *prev;
5781556Srgrimes
57997659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
58097659Stjr		if (jp == cj) {
58197659Stjr			if (prev != NULL)
58297659Stjr				prev->next = jp->next;
58397659Stjr			else
58497659Stjr				jobmru = jp->next;
58597659Stjr			jp->next = jobmru;
58697659Stjr			jobmru = cj;
58797659Stjr			return;
58897659Stjr		}
58997659Stjr	}
59097659Stjr	cj->next = jobmru;
59197659Stjr	jobmru = cj;
59297659Stjr}
59397659Stjr
59497659StjrSTATIC void
59597659Stjrdeljob(struct job *j)
59697659Stjr{
59797659Stjr	struct job *jp, *prev;
59897659Stjr
59997659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
60097659Stjr		if (jp == j) {
60197659Stjr			if (prev != NULL)
60297659Stjr				prev->next = jp->next;
60397659Stjr			else
60497659Stjr				jobmru = jp->next;
60597659Stjr			return;
60697659Stjr		}
60797659Stjr	}
60897659Stjr}
60997659Stjr
6101556Srgrimes/*
61197659Stjr * Return the most recently used job that isn't `nj', and preferably one
61297659Stjr * that is stopped.
61397659Stjr */
61497659StjrSTATIC struct job *
61597659Stjrgetcurjob(struct job *nj)
61697659Stjr{
61797659Stjr	struct job *jp;
61897659Stjr
61997659Stjr	/* Try to find a stopped one.. */
62097659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
62197659Stjr		if (jp->used && jp != nj && jp->state == JOBSTOPPED)
62297659Stjr			return (jp);
62397659Stjr	/* Otherwise the most recently used job that isn't `nj' */
62497659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
62597659Stjr		if (jp->used && jp != nj)
62697659Stjr			return (jp);
62797659Stjr
62897659Stjr	return (NULL);
62997659Stjr}
63097659Stjr
63197659Stjr#endif
63297659Stjr
63397659Stjr/*
6341556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
6351556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
6361556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
6371556Srgrimes * be NULL.  The mode parameter can be one of the following:
6381556Srgrimes *	FORK_FG - Fork off a foreground process.
6391556Srgrimes *	FORK_BG - Fork off a background process.
6401556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
6411556Srgrimes *		     process group even if job control is on.
6421556Srgrimes *
6431556Srgrimes * When job control is turned off, background processes have their standard
6441556Srgrimes * input redirected to /dev/null (except for the second and later processes
6451556Srgrimes * in a pipeline).
6461556Srgrimes */
6471556Srgrimes
6481556Srgrimesint
64990111Simpforkshell(struct job *jp, union node *n, int mode)
65017987Speter{
6511556Srgrimes	int pid;
6521556Srgrimes	int pgrp;
6531556Srgrimes
65417987Speter	TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
65517987Speter	    mode));
6561556Srgrimes	INTOFF;
6571556Srgrimes	pid = fork();
6581556Srgrimes	if (pid == -1) {
6591556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
6601556Srgrimes		INTON;
66153891Scracauer		error("Cannot fork: %s", strerror(errno));
6621556Srgrimes	}
6631556Srgrimes	if (pid == 0) {
6641556Srgrimes		struct job *p;
6651556Srgrimes		int wasroot;
6661556Srgrimes		int i;
6671556Srgrimes
6681556Srgrimes		TRACE(("Child shell %d\n", getpid()));
6691556Srgrimes		wasroot = rootshell;
6701556Srgrimes		rootshell = 0;
6711556Srgrimes		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
6721556Srgrimes			if (p->used)
6731556Srgrimes				freejob(p);
6741556Srgrimes		closescript();
6751556Srgrimes		INTON;
6761556Srgrimes		clear_traps();
6771556Srgrimes#if JOBS
6781556Srgrimes		jobctl = 0;		/* do job control only in root shell */
6791556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
6801556Srgrimes			if (jp == NULL || jp->nprocs == 0)
6811556Srgrimes				pgrp = getpid();
6821556Srgrimes			else
6831556Srgrimes				pgrp = jp->ps[0].pid;
68421352Ssteve			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
6851556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
68620425Ssteve#ifdef OLD_TTY_DRIVER
6871556Srgrimes				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
68818016Speter					error("TIOCSPGRP failed, errno=%d", errno);
68920425Ssteve#else
69020425Ssteve				if (tcsetpgrp(2, pgrp) < 0)
69120425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
69220425Ssteve#endif
6931556Srgrimes			}
6941556Srgrimes			setsignal(SIGTSTP);
6951556Srgrimes			setsignal(SIGTTOU);
6961556Srgrimes		} else if (mode == FORK_BG) {
6971556Srgrimes			ignoresig(SIGINT);
6981556Srgrimes			ignoresig(SIGQUIT);
6991556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
7001556Srgrimes			    ! fd0_redirected_p ()) {
7011556Srgrimes				close(0);
70269793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
70369793Sobrien					error("Can't open %s: %s",
70469793Sobrien					    _PATH_DEVNULL, strerror(errno));
7051556Srgrimes			}
7061556Srgrimes		}
7071556Srgrimes#else
7081556Srgrimes		if (mode == FORK_BG) {
7091556Srgrimes			ignoresig(SIGINT);
7101556Srgrimes			ignoresig(SIGQUIT);
7111556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
7121556Srgrimes			    ! fd0_redirected_p ()) {
7131556Srgrimes				close(0);
71469793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
71569793Sobrien					error("Can't open %s: %s",
71669793Sobrien					    _PATH_DEVNULL, strerror(errno));
7171556Srgrimes			}
7181556Srgrimes		}
7191556Srgrimes#endif
7201556Srgrimes		if (wasroot && iflag) {
7211556Srgrimes			setsignal(SIGINT);
7221556Srgrimes			setsignal(SIGQUIT);
7231556Srgrimes			setsignal(SIGTERM);
7241556Srgrimes		}
7251556Srgrimes		return pid;
7261556Srgrimes	}
7271556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
7281556Srgrimes		if (jp == NULL || jp->nprocs == 0)
7291556Srgrimes			pgrp = pid;
7301556Srgrimes		else
7311556Srgrimes			pgrp = jp->ps[0].pid;
73217987Speter		setpgid(pid, pgrp);
7331556Srgrimes	}
7341556Srgrimes	if (mode == FORK_BG)
7351556Srgrimes		backgndpid = pid;		/* set $! */
7361556Srgrimes	if (jp) {
7371556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
7381556Srgrimes		ps->pid = pid;
7391556Srgrimes		ps->status = -1;
7401556Srgrimes		ps->cmd = nullstr;
7411556Srgrimes		if (iflag && rootshell && n)
7421556Srgrimes			ps->cmd = commandtext(n);
74397659Stjr#if JOBS
74497659Stjr		setcurjob(jp);
74597659Stjr#endif
7461556Srgrimes	}
7471556Srgrimes	INTON;
7481556Srgrimes	TRACE(("In parent shell:  child = %d\n", pid));
7491556Srgrimes	return pid;
7501556Srgrimes}
7511556Srgrimes
7521556Srgrimes
7531556Srgrimes
7541556Srgrimes/*
7551556Srgrimes * Wait for job to finish.
7561556Srgrimes *
7571556Srgrimes * Under job control we have the problem that while a child process is
7581556Srgrimes * running interrupts generated by the user are sent to the child but not
7591556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
7601556Srgrimes * active user may be hard to kill.  With job control turned off, an
7611556Srgrimes * interactive user may place an interactive program inside a loop.  If
7621556Srgrimes * the interactive program catches interrupts, the user doesn't want
7631556Srgrimes * these interrupts to also abort the loop.  The approach we take here
7641556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
76546684Skris * foreground process to terminate, and then send itself an interrupt
7661556Srgrimes * signal if the child process was terminated by an interrupt signal.
7671556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
7681556Srgrimes * exit on interrupt; unless these processes terminate themselves by
7691556Srgrimes * sending a signal to themselves (instead of calling exit) they will
7701556Srgrimes * confuse this approach.
7711556Srgrimes */
7721556Srgrimes
7731556Srgrimesint
77490111Simpwaitforjob(struct job *jp, int *origstatus)
77545916Scracauer{
7761556Srgrimes#if JOBS
77717987Speter	int mypgrp = getpgrp();
7781556Srgrimes#endif
7791556Srgrimes	int status;
7801556Srgrimes	int st;
7811556Srgrimes
7821556Srgrimes	INTOFF;
7831556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
78438950Scracauer	while (jp->state == 0)
78538950Scracauer		if (dowait(1, jp) == -1)
78638950Scracauer			dotrap();
7871556Srgrimes#if JOBS
7881556Srgrimes	if (jp->jobctl) {
78920425Ssteve#ifdef OLD_TTY_DRIVER
7901556Srgrimes		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
79120425Ssteve			error("TIOCSPGRP failed, errno=%d\n", errno);
79220425Ssteve#else
79320425Ssteve		if (tcsetpgrp(2, mypgrp) < 0)
79420425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
79520425Ssteve#endif
7961556Srgrimes	}
7971556Srgrimes	if (jp->state == JOBSTOPPED)
79897659Stjr		setcurjob(jp);
7991556Srgrimes#endif
8001556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
80145916Scracauer	if (origstatus != NULL)
80245916Scracauer		*origstatus = status;
8031556Srgrimes	/* convert to 8 bits */
80426104Ssteve	if (WIFEXITED(status))
80526104Ssteve		st = WEXITSTATUS(status);
8061556Srgrimes#if JOBS
80726104Ssteve	else if (WIFSTOPPED(status))
80826104Ssteve		st = WSTOPSIG(status) + 128;
8091556Srgrimes#endif
8101556Srgrimes	else
81126104Ssteve		st = WTERMSIG(status) + 128;
8121556Srgrimes	if (! JOBS || jp->state == JOBDONE)
8131556Srgrimes		freejob(jp);
81438521Scracauer	if (int_pending()) {
81538521Scracauer		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
81638521Scracauer			kill(getpid(), SIGINT);
81738521Scracauer		else
81838521Scracauer			CLEAR_PENDING_INT;
81938521Scracauer	}
8201556Srgrimes	INTON;
8211556Srgrimes	return st;
8221556Srgrimes}
8231556Srgrimes
8241556Srgrimes
8251556Srgrimes
8261556Srgrimes/*
8271556Srgrimes * Wait for a process to terminate.
8281556Srgrimes */
8291556Srgrimes
8301556SrgrimesSTATIC int
83190111Simpdowait(int block, struct job *job)
83217987Speter{
8331556Srgrimes	int pid;
8341556Srgrimes	int status;
8351556Srgrimes	struct procstat *sp;
8361556Srgrimes	struct job *jp;
8371556Srgrimes	struct job *thisjob;
8381556Srgrimes	int done;
8391556Srgrimes	int stopped;
8401556Srgrimes	int core;
84126104Ssteve	int sig;
8421556Srgrimes
84338950Scracauer	in_dowait++;
8441556Srgrimes	TRACE(("dowait(%d) called\n", block));
8451556Srgrimes	do {
8461556Srgrimes		pid = waitproc(block, &status);
8471556Srgrimes		TRACE(("wait returns %d, status=%d\n", pid, status));
84872086Scracauer	} while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
84972086Scracauer	    (WIFSTOPPED(status) && !iflag));
85038950Scracauer	in_dowait--;
85138521Scracauer	if (breakwaitcmd != 0) {
85238521Scracauer		breakwaitcmd = 0;
85338521Scracauer		return -1;
85438521Scracauer	}
8551556Srgrimes	if (pid <= 0)
8561556Srgrimes		return pid;
8571556Srgrimes	INTOFF;
8581556Srgrimes	thisjob = NULL;
8591556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
8601556Srgrimes		if (jp->used) {
8611556Srgrimes			done = 1;
8621556Srgrimes			stopped = 1;
8631556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
8641556Srgrimes				if (sp->pid == -1)
8651556Srgrimes					continue;
8661556Srgrimes				if (sp->pid == pid) {
86726104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
86826104Ssteve						   pid, sp->status, status));
8691556Srgrimes					sp->status = status;
8701556Srgrimes					thisjob = jp;
8711556Srgrimes				}
8721556Srgrimes				if (sp->status == -1)
8731556Srgrimes					stopped = 0;
87426104Ssteve				else if (WIFSTOPPED(sp->status))
8751556Srgrimes					done = 0;
8761556Srgrimes			}
8771556Srgrimes			if (stopped) {		/* stopped or done */
8781556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
8791556Srgrimes				if (jp->state != state) {
8801556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
8811556Srgrimes					jp->state = state;
8821556Srgrimes#if JOBS
88397659Stjr					if (done)
88497659Stjr						deljob(jp);
8851556Srgrimes#endif
8861556Srgrimes				}
8871556Srgrimes			}
8881556Srgrimes		}
8891556Srgrimes	}
8901556Srgrimes	INTON;
8911556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
89226104Ssteve		core = WCOREDUMP(status);
8931556Srgrimes#if JOBS
89426104Ssteve		if (WIFSTOPPED(status))
89526104Ssteve			sig = WSTOPSIG(status);
89626104Ssteve		else
8971556Srgrimes#endif
89826104Ssteve			if (WIFEXITED(status))
89926104Ssteve				sig = 0;
90026104Ssteve			else
90126104Ssteve				sig = WTERMSIG(status);
90226104Ssteve
90326104Ssteve		if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
9041556Srgrimes			if (thisjob != job)
9051556Srgrimes				outfmt(out2, "%d: ", pid);
9061556Srgrimes#if JOBS
90726104Ssteve			if (sig == SIGTSTP && rootshell && iflag)
9081556Srgrimes				outfmt(out2, "%%%d ", job - jobtab + 1);
9091556Srgrimes#endif
91026104Ssteve			if (sig < NSIG && sys_siglist[sig])
91126104Ssteve				out2str(sys_siglist[sig]);
9121556Srgrimes			else
91326104Ssteve				outfmt(out2, "Signal %d", sig);
9141556Srgrimes			if (core)
9151556Srgrimes				out2str(" - core dumped");
9161556Srgrimes			out2c('\n');
9171556Srgrimes			flushout(&errout);
9181556Srgrimes		} else {
91926104Ssteve			TRACE(("Not printing status: status=%d, sig=%d\n",
92026104Ssteve				   status, sig));
9211556Srgrimes		}
9221556Srgrimes	} else {
9231556Srgrimes		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
9241556Srgrimes		if (thisjob)
9251556Srgrimes			thisjob->changed = 1;
9261556Srgrimes	}
9271556Srgrimes	return pid;
9281556Srgrimes}
9291556Srgrimes
9301556Srgrimes
9311556Srgrimes
9321556Srgrimes/*
9331556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
9341556Srgrimes * stopped processes.  If block is zero, we return a value of zero
9351556Srgrimes * rather than blocking.
9361556Srgrimes *
9371556Srgrimes * System V doesn't have a non-blocking wait system call.  It does
9381556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's
9391556Srgrimes * children dies.  The obvious way to use SIGCLD would be to install
9401556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD
9411556Srgrimes * was received, and have waitproc bump another counter when it got
9421556Srgrimes * the status of a process.  Waitproc would then know that a wait
9431556Srgrimes * system call would not block if the two counters were different.
9441556Srgrimes * This approach doesn't work because if a process has children that
9451556Srgrimes * have not been waited for, System V will send it a SIGCLD when it
9461556Srgrimes * installs a signal handler for SIGCLD.  What this means is that when
9471556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously
9481556Srgrimes * until is runs out of stack space, unless it does a wait call before
9491556Srgrimes * restoring the signal handler.  The code below takes advantage of
9501556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and
9511556Srgrimes * then checking to see whether it was called.  If there are any
9521556Srgrimes * children to be waited for, it will be.
9531556Srgrimes *
9541556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking
9551556Srgrimes * waits at all.  In this case, the user will not be informed when
9561556Srgrimes * a background process until the next time she runs a real program
9571556Srgrimes * (as opposed to running a builtin command or just typing return),
9581556Srgrimes * and the jobs command may give out of date information.
9591556Srgrimes */
9601556Srgrimes
9611556Srgrimes#ifdef SYSV
96238521ScracauerSTATIC sig_atomic_t gotsigchild;
9631556Srgrimes
9641556SrgrimesSTATIC int onsigchild() {
9651556Srgrimes	gotsigchild = 1;
9661556Srgrimes}
9671556Srgrimes#endif
9681556Srgrimes
9691556Srgrimes
9701556SrgrimesSTATIC int
97190111Simpwaitproc(int block, int *status)
97217987Speter{
9731556Srgrimes#ifdef BSD
9741556Srgrimes	int flags;
9751556Srgrimes
9761556Srgrimes#if JOBS
9771556Srgrimes	flags = WUNTRACED;
9781556Srgrimes#else
9791556Srgrimes	flags = 0;
9801556Srgrimes#endif
9811556Srgrimes	if (block == 0)
9821556Srgrimes		flags |= WNOHANG;
9831556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
9841556Srgrimes#else
9851556Srgrimes#ifdef SYSV
9861556Srgrimes	int (*save)();
9871556Srgrimes
9881556Srgrimes	if (block == 0) {
9891556Srgrimes		gotsigchild = 0;
9901556Srgrimes		save = signal(SIGCLD, onsigchild);
9911556Srgrimes		signal(SIGCLD, save);
9921556Srgrimes		if (gotsigchild == 0)
9931556Srgrimes			return 0;
9941556Srgrimes	}
9951556Srgrimes	return wait(status);
9961556Srgrimes#else
9971556Srgrimes	if (block == 0)
9981556Srgrimes		return 0;
9991556Srgrimes	return wait(status);
10001556Srgrimes#endif
10011556Srgrimes#endif
10021556Srgrimes}
10031556Srgrimes
10041556Srgrimes/*
10051556Srgrimes * return 1 if there are stopped jobs, otherwise 0
10061556Srgrimes */
10071556Srgrimesint job_warning = 0;
10081556Srgrimesint
100990111Simpstoppedjobs(void)
10101556Srgrimes{
101125222Ssteve	int jobno;
101225222Ssteve	struct job *jp;
10131556Srgrimes
10141556Srgrimes	if (job_warning)
10151556Srgrimes		return (0);
10161556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
10171556Srgrimes		if (jp->used == 0)
10181556Srgrimes			continue;
10191556Srgrimes		if (jp->state == JOBSTOPPED) {
10201556Srgrimes			out2str("You have stopped jobs.\n");
10211556Srgrimes			job_warning = 2;
10221556Srgrimes			return (1);
10231556Srgrimes		}
10241556Srgrimes	}
10251556Srgrimes
10261556Srgrimes	return (0);
10271556Srgrimes}
10281556Srgrimes
10291556Srgrimes/*
10301556Srgrimes * Return a string identifying a command (to be printed by the
10311556Srgrimes * jobs command.
10321556Srgrimes */
10331556Srgrimes
10341556SrgrimesSTATIC char *cmdnextc;
10351556SrgrimesSTATIC int cmdnleft;
10361556Srgrimes#define MAXCMDTEXT	200
10371556Srgrimes
10381556Srgrimeschar *
103990111Simpcommandtext(union node *n)
104090111Simp{
10411556Srgrimes	char *name;
10421556Srgrimes
10431556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
10441556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
10451556Srgrimes	cmdtxt(n);
10461556Srgrimes	*cmdnextc = '\0';
10471556Srgrimes	return name;
10481556Srgrimes}
10491556Srgrimes
10501556Srgrimes
10511556SrgrimesSTATIC void
105290111Simpcmdtxt(union node *n)
105390111Simp{
10541556Srgrimes	union node *np;
10551556Srgrimes	struct nodelist *lp;
10561556Srgrimes	char *p;
10571556Srgrimes	int i;
10581556Srgrimes	char s[2];
10591556Srgrimes
10601556Srgrimes	if (n == NULL)
10611556Srgrimes		return;
10621556Srgrimes	switch (n->type) {
10631556Srgrimes	case NSEMI:
10641556Srgrimes		cmdtxt(n->nbinary.ch1);
10651556Srgrimes		cmdputs("; ");
10661556Srgrimes		cmdtxt(n->nbinary.ch2);
10671556Srgrimes		break;
10681556Srgrimes	case NAND:
10691556Srgrimes		cmdtxt(n->nbinary.ch1);
10701556Srgrimes		cmdputs(" && ");
10711556Srgrimes		cmdtxt(n->nbinary.ch2);
10721556Srgrimes		break;
10731556Srgrimes	case NOR:
10741556Srgrimes		cmdtxt(n->nbinary.ch1);
10751556Srgrimes		cmdputs(" || ");
10761556Srgrimes		cmdtxt(n->nbinary.ch2);
10771556Srgrimes		break;
10781556Srgrimes	case NPIPE:
10791556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
10801556Srgrimes			cmdtxt(lp->n);
10811556Srgrimes			if (lp->next)
10821556Srgrimes				cmdputs(" | ");
10831556Srgrimes		}
10841556Srgrimes		break;
10851556Srgrimes	case NSUBSHELL:
10861556Srgrimes		cmdputs("(");
10871556Srgrimes		cmdtxt(n->nredir.n);
10881556Srgrimes		cmdputs(")");
10891556Srgrimes		break;
10901556Srgrimes	case NREDIR:
10911556Srgrimes	case NBACKGND:
10921556Srgrimes		cmdtxt(n->nredir.n);
10931556Srgrimes		break;
10941556Srgrimes	case NIF:
10951556Srgrimes		cmdputs("if ");
10961556Srgrimes		cmdtxt(n->nif.test);
10971556Srgrimes		cmdputs("; then ");
10981556Srgrimes		cmdtxt(n->nif.ifpart);
10991556Srgrimes		cmdputs("...");
11001556Srgrimes		break;
11011556Srgrimes	case NWHILE:
11021556Srgrimes		cmdputs("while ");
11031556Srgrimes		goto until;
11041556Srgrimes	case NUNTIL:
11051556Srgrimes		cmdputs("until ");
11061556Srgrimesuntil:
11071556Srgrimes		cmdtxt(n->nbinary.ch1);
11081556Srgrimes		cmdputs("; do ");
11091556Srgrimes		cmdtxt(n->nbinary.ch2);
11101556Srgrimes		cmdputs("; done");
11111556Srgrimes		break;
11121556Srgrimes	case NFOR:
11131556Srgrimes		cmdputs("for ");
11141556Srgrimes		cmdputs(n->nfor.var);
11151556Srgrimes		cmdputs(" in ...");
11161556Srgrimes		break;
11171556Srgrimes	case NCASE:
11181556Srgrimes		cmdputs("case ");
11191556Srgrimes		cmdputs(n->ncase.expr->narg.text);
11201556Srgrimes		cmdputs(" in ...");
11211556Srgrimes		break;
11221556Srgrimes	case NDEFUN:
11231556Srgrimes		cmdputs(n->narg.text);
11241556Srgrimes		cmdputs("() ...");
11251556Srgrimes		break;
11261556Srgrimes	case NCMD:
11271556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
11281556Srgrimes			cmdtxt(np);
11291556Srgrimes			if (np->narg.next)
11301556Srgrimes				cmdputs(" ");
11311556Srgrimes		}
11321556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
11331556Srgrimes			cmdputs(" ");
11341556Srgrimes			cmdtxt(np);
11351556Srgrimes		}
11361556Srgrimes		break;
11371556Srgrimes	case NARG:
11381556Srgrimes		cmdputs(n->narg.text);
11391556Srgrimes		break;
11401556Srgrimes	case NTO:
11411556Srgrimes		p = ">";  i = 1;  goto redir;
11421556Srgrimes	case NAPPEND:
11431556Srgrimes		p = ">>";  i = 1;  goto redir;
11441556Srgrimes	case NTOFD:
11451556Srgrimes		p = ">&";  i = 1;  goto redir;
114696922Stjr	case NCLOBBER:
114796922Stjr		p = ">|"; i = 1; goto redir;
11481556Srgrimes	case NFROM:
11491556Srgrimes		p = "<";  i = 0;  goto redir;
115066612Sbrian	case NFROMTO:
115166612Sbrian		p = "<>";  i = 0;  goto redir;
11521556Srgrimes	case NFROMFD:
11531556Srgrimes		p = "<&";  i = 0;  goto redir;
11541556Srgrimesredir:
11551556Srgrimes		if (n->nfile.fd != i) {
11561556Srgrimes			s[0] = n->nfile.fd + '0';
11571556Srgrimes			s[1] = '\0';
11581556Srgrimes			cmdputs(s);
11591556Srgrimes		}
11601556Srgrimes		cmdputs(p);
11611556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
11621556Srgrimes			s[0] = n->ndup.dupfd + '0';
11631556Srgrimes			s[1] = '\0';
11641556Srgrimes			cmdputs(s);
11651556Srgrimes		} else {
11661556Srgrimes			cmdtxt(n->nfile.fname);
11671556Srgrimes		}
11681556Srgrimes		break;
11691556Srgrimes	case NHERE:
11701556Srgrimes	case NXHERE:
11711556Srgrimes		cmdputs("<<...");
11721556Srgrimes		break;
11731556Srgrimes	default:
11741556Srgrimes		cmdputs("???");
11751556Srgrimes		break;
11761556Srgrimes	}
11771556Srgrimes}
11781556Srgrimes
11791556Srgrimes
11801556Srgrimes
11811556SrgrimesSTATIC void
118290111Simpcmdputs(char *s)
118390111Simp{
118425222Ssteve	char *p, *q;
118525222Ssteve	char c;
11861556Srgrimes	int subtype = 0;
11871556Srgrimes
11881556Srgrimes	if (cmdnleft <= 0)
11891556Srgrimes		return;
11901556Srgrimes	p = s;
11911556Srgrimes	q = cmdnextc;
11921556Srgrimes	while ((c = *p++) != '\0') {
11931556Srgrimes		if (c == CTLESC)
11941556Srgrimes			*q++ = *p++;
11951556Srgrimes		else if (c == CTLVAR) {
11961556Srgrimes			*q++ = '$';
11971556Srgrimes			if (--cmdnleft > 0)
11981556Srgrimes				*q++ = '{';
11991556Srgrimes			subtype = *p++;
12001556Srgrimes		} else if (c == '=' && subtype != 0) {
12011556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
12021556Srgrimes			subtype = 0;
12031556Srgrimes		} else if (c == CTLENDVAR) {
12041556Srgrimes			*q++ = '}';
120518954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
12061556Srgrimes			cmdnleft++;		/* ignore it */
12071556Srgrimes		else
12081556Srgrimes			*q++ = c;
12091556Srgrimes		if (--cmdnleft <= 0) {
12101556Srgrimes			*q++ = '.';
12111556Srgrimes			*q++ = '.';
12121556Srgrimes			*q++ = '.';
12131556Srgrimes			break;
12141556Srgrimes		}
12151556Srgrimes	}
12161556Srgrimes	cmdnextc = q;
12171556Srgrimes}
1218