jobs.c revision 96933
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 96933 2002-05-19 07:27:26Z 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
881556Srgrimesint initialpgrp;		/* pgrp of shell on invocation */
8928346Ssteveint curjob;			/* current job */
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 *);
1071556Srgrimes
1081556Srgrimes
1091556Srgrimes/*
1101556Srgrimes * Turn job control on and off.
1111556Srgrimes *
1121556Srgrimes * Note:  This code assumes that the third arg to ioctl is a character
1131556Srgrimes * pointer, which is true on Berkeley systems but not System V.  Since
1141556Srgrimes * System V doesn't have job control yet, this isn't a problem now.
1151556Srgrimes */
1161556Srgrimes
1171556SrgrimesMKINIT int jobctl;
1181556Srgrimes
11920425Ssteve#if JOBS
1201556Srgrimesvoid
12190111Simpsetjobctl(int on)
12217987Speter{
1231556Srgrimes#ifdef OLD_TTY_DRIVER
1241556Srgrimes	int ldisc;
1251556Srgrimes#endif
1261556Srgrimes
1271556Srgrimes	if (on == jobctl || rootshell == 0)
1281556Srgrimes		return;
1291556Srgrimes	if (on) {
1301556Srgrimes		do { /* while we are in the background */
13120425Ssteve#ifdef OLD_TTY_DRIVER
1321556Srgrimes			if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) {
13320425Ssteve#else
13420425Ssteve			initialpgrp = tcgetpgrp(2);
13520425Ssteve			if (initialpgrp < 0) {
13620425Ssteve#endif
1371556Srgrimes				out2str("sh: can't access tty; job control turned off\n");
1381556Srgrimes				mflag = 0;
1391556Srgrimes				return;
1401556Srgrimes			}
1411556Srgrimes			if (initialpgrp == -1)
14217987Speter				initialpgrp = getpgrp();
14317987Speter			else if (initialpgrp != getpgrp()) {
1441556Srgrimes				killpg(initialpgrp, SIGTTIN);
1451556Srgrimes				continue;
1461556Srgrimes			}
1471556Srgrimes		} while (0);
1481556Srgrimes#ifdef OLD_TTY_DRIVER
1491556Srgrimes		if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) {
1501556Srgrimes			out2str("sh: need new tty driver to run job control; job control turned off\n");
1511556Srgrimes			mflag = 0;
1521556Srgrimes			return;
1531556Srgrimes		}
1541556Srgrimes#endif
1551556Srgrimes		setsignal(SIGTSTP);
1561556Srgrimes		setsignal(SIGTTOU);
1571556Srgrimes		setsignal(SIGTTIN);
15817987Speter		setpgid(0, rootpid);
15920425Ssteve#ifdef OLD_TTY_DRIVER
1601556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&rootpid);
16120425Ssteve#else
16220425Ssteve		tcsetpgrp(2, rootpid);
16320425Ssteve#endif
1641556Srgrimes	} else { /* turning job control off */
16517987Speter		setpgid(0, initialpgrp);
16620425Ssteve#ifdef OLD_TTY_DRIVER
1671556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&initialpgrp);
16820425Ssteve#else
16920425Ssteve		tcsetpgrp(2, initialpgrp);
17020425Ssteve#endif
1711556Srgrimes		setsignal(SIGTSTP);
1721556Srgrimes		setsignal(SIGTTOU);
1731556Srgrimes		setsignal(SIGTTIN);
1741556Srgrimes	}
1751556Srgrimes	jobctl = on;
1761556Srgrimes}
17720425Ssteve#endif
1781556Srgrimes
1791556Srgrimes
1801556Srgrimes#ifdef mkinit
18128346SsteveINCLUDE <sys/types.h>
18217987SpeterINCLUDE <stdlib.h>
1831556Srgrimes
1841556SrgrimesSHELLPROC {
1851556Srgrimes	backgndpid = -1;
1861556Srgrimes#if JOBS
1871556Srgrimes	jobctl = 0;
1881556Srgrimes#endif
1891556Srgrimes}
1901556Srgrimes
1911556Srgrimes#endif
1921556Srgrimes
1931556Srgrimes
1941556Srgrimes
1951556Srgrimes#if JOBS
19617987Speterint
19790111Simpfgcmd(int argc __unused, char **argv)
19817987Speter{
1991556Srgrimes	struct job *jp;
2001556Srgrimes	int pgrp;
2011556Srgrimes	int status;
2021556Srgrimes
2031556Srgrimes	jp = getjob(argv[1]);
2041556Srgrimes	if (jp->jobctl == 0)
2051556Srgrimes		error("job not created under job control");
20696933Stjr	out1str(jp->ps[0].cmd);
20796933Stjr	out1c('\n');
20896933Stjr	flushout(&output);
2091556Srgrimes	pgrp = jp->ps[0].pid;
21020425Ssteve#ifdef OLD_TTY_DRIVER
2111556Srgrimes	ioctl(2, TIOCSPGRP, (char *)&pgrp);
21220425Ssteve#else
21320425Ssteve	tcsetpgrp(2, pgrp);
21420425Ssteve#endif
2151556Srgrimes	restartjob(jp);
2161556Srgrimes	INTOFF;
21745916Scracauer	status = waitforjob(jp, (int *)NULL);
2181556Srgrimes	INTON;
2191556Srgrimes	return status;
2201556Srgrimes}
2211556Srgrimes
2221556Srgrimes
22317987Speterint
22490111Simpbgcmd(int argc, char **argv)
22517987Speter{
22696933Stjr	char s[64];
2271556Srgrimes	struct job *jp;
2281556Srgrimes
2291556Srgrimes	do {
2301556Srgrimes		jp = getjob(*++argv);
2311556Srgrimes		if (jp->jobctl == 0)
2321556Srgrimes			error("job not created under job control");
23396933Stjr		if (jp->state == JOBDONE)
23496933Stjr			continue;
2351556Srgrimes		restartjob(jp);
23696933Stjr		fmtstr(s, 64, "[%d] ", jp - jobtab + 1);
23796933Stjr		out1str(s);
23896933Stjr		out1str(jp->ps[0].cmd);
23996933Stjr		out1c('\n');
2401556Srgrimes	} while (--argc > 1);
2411556Srgrimes	return 0;
2421556Srgrimes}
2431556Srgrimes
2441556Srgrimes
2451556SrgrimesSTATIC void
24690111Simprestartjob(struct job *jp)
24717987Speter{
2481556Srgrimes	struct procstat *ps;
2491556Srgrimes	int i;
2501556Srgrimes
2511556Srgrimes	if (jp->state == JOBDONE)
2521556Srgrimes		return;
2531556Srgrimes	INTOFF;
2541556Srgrimes	killpg(jp->ps[0].pid, SIGCONT);
2551556Srgrimes	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
25626104Ssteve		if (WIFSTOPPED(ps->status)) {
2571556Srgrimes			ps->status = -1;
2581556Srgrimes			jp->state = 0;
2591556Srgrimes		}
2601556Srgrimes	}
2611556Srgrimes	INTON;
2621556Srgrimes}
2631556Srgrimes#endif
2641556Srgrimes
2651556Srgrimes
2661556Srgrimesint
26790111Simpjobscmd(int argc __unused, char **argv __unused)
26817987Speter{
2691556Srgrimes	showjobs(0);
2701556Srgrimes	return 0;
2711556Srgrimes}
2721556Srgrimes
2731556Srgrimes
2741556Srgrimes/*
2751556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
2761556Srgrimes * statuses have changed since the last call to showjobs.
2771556Srgrimes *
2781556Srgrimes * If the shell is interrupted in the process of creating a job, the
2791556Srgrimes * result may be a job structure containing zero processes.  Such structures
2801556Srgrimes * will be freed here.
2811556Srgrimes */
2821556Srgrimes
2831556Srgrimesvoid
28490111Simpshowjobs(int change)
28517987Speter{
2861556Srgrimes	int jobno;
2871556Srgrimes	int procno;
2881556Srgrimes	int i;
2891556Srgrimes	struct job *jp;
2901556Srgrimes	struct procstat *ps;
2911556Srgrimes	int col;
2921556Srgrimes	char s[64];
2931556Srgrimes
2941556Srgrimes	TRACE(("showjobs(%d) called\n", change));
2951556Srgrimes	while (dowait(0, (struct job *)NULL) > 0);
2961556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
2971556Srgrimes		if (! jp->used)
2981556Srgrimes			continue;
2991556Srgrimes		if (jp->nprocs == 0) {
3001556Srgrimes			freejob(jp);
3011556Srgrimes			continue;
3021556Srgrimes		}
3031556Srgrimes		if (change && ! jp->changed)
3041556Srgrimes			continue;
3051556Srgrimes		procno = jp->nprocs;
3061556Srgrimes		for (ps = jp->ps ; ; ps++) {	/* for each process */
3071556Srgrimes			if (ps == jp->ps)
3081556Srgrimes				fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
3091556Srgrimes			else
3101556Srgrimes				fmtstr(s, 64, "    %d ", ps->pid);
3111556Srgrimes			out1str(s);
3121556Srgrimes			col = strlen(s);
3131556Srgrimes			s[0] = '\0';
3141556Srgrimes			if (ps->status == -1) {
3151556Srgrimes				/* don't print anything */
31626104Ssteve			} else if (WIFEXITED(ps->status)) {
31726104Ssteve				fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status));
3181556Srgrimes			} else {
3191556Srgrimes#if JOBS
32026104Ssteve				if (WIFSTOPPED(ps->status))
32126104Ssteve					i = WSTOPSIG(ps->status);
32226104Ssteve				else
3231556Srgrimes#endif
32426104Ssteve					i = WTERMSIG(ps->status);
32517987Speter				if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
32617987Speter					scopy(sys_siglist[i & 0x7F], s);
3271556Srgrimes				else
3281556Srgrimes					fmtstr(s, 64, "Signal %d", i & 0x7F);
32926104Ssteve				if (WCOREDUMP(ps->status))
3301556Srgrimes					strcat(s, " (core dumped)");
3311556Srgrimes			}
3321556Srgrimes			out1str(s);
3331556Srgrimes			col += strlen(s);
3341556Srgrimes			do {
3351556Srgrimes				out1c(' ');
3361556Srgrimes				col++;
3371556Srgrimes			} while (col < 30);
3381556Srgrimes			out1str(ps->cmd);
3391556Srgrimes			out1c('\n');
3401556Srgrimes			if (--procno <= 0)
3411556Srgrimes				break;
3421556Srgrimes		}
3431556Srgrimes		jp->changed = 0;
3441556Srgrimes		if (jp->state == JOBDONE) {
3451556Srgrimes			freejob(jp);
3461556Srgrimes		}
3471556Srgrimes	}
3481556Srgrimes}
3491556Srgrimes
3501556Srgrimes
3511556Srgrimes/*
3521556Srgrimes * Mark a job structure as unused.
3531556Srgrimes */
3541556Srgrimes
3551556SrgrimesSTATIC void
35690111Simpfreejob(struct job *jp)
35790111Simp{
3581556Srgrimes	struct procstat *ps;
3591556Srgrimes	int i;
3601556Srgrimes
3611556Srgrimes	INTOFF;
3621556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
3631556Srgrimes		if (ps->cmd != nullstr)
3641556Srgrimes			ckfree(ps->cmd);
3651556Srgrimes	}
3661556Srgrimes	if (jp->ps != &jp->ps0)
3671556Srgrimes		ckfree(jp->ps);
3681556Srgrimes	jp->used = 0;
3691556Srgrimes#if JOBS
3701556Srgrimes	if (curjob == jp - jobtab + 1)
3711556Srgrimes		curjob = 0;
3721556Srgrimes#endif
3731556Srgrimes	INTON;
3741556Srgrimes}
3751556Srgrimes
3761556Srgrimes
3771556Srgrimes
3781556Srgrimesint
37990111Simpwaitcmd(int argc, char **argv)
38017987Speter{
3811556Srgrimes	struct job *job;
38226104Ssteve	int status, retval;
3831556Srgrimes	struct job *jp;
3841556Srgrimes
3851556Srgrimes	if (argc > 1) {
3861556Srgrimes		job = getjob(argv[1]);
3871556Srgrimes	} else {
3881556Srgrimes		job = NULL;
3891556Srgrimes	}
39038536Scracauer
39138536Scracauer	/*
39238536Scracauer	 * Loop until a process is terminated or stopped, or a SIGINT is
39338536Scracauer	 * received.
39438536Scracauer	 */
39538536Scracauer
39638521Scracauer	in_waitcmd++;
39738536Scracauer	do {
3981556Srgrimes		if (job != NULL) {
3991556Srgrimes			if (job->state) {
4001556Srgrimes				status = job->ps[job->nprocs - 1].status;
40126104Ssteve				if (WIFEXITED(status))
40226104Ssteve					retval = WEXITSTATUS(status);
4031556Srgrimes#if JOBS
40426104Ssteve				else if (WIFSTOPPED(status))
40526104Ssteve					retval = WSTOPSIG(status) + 128;
4061556Srgrimes#endif
4071556Srgrimes				else
40826104Ssteve					retval = WTERMSIG(status) + 128;
4091556Srgrimes				if (! iflag)
4101556Srgrimes					freejob(job);
41138536Scracauer				in_waitcmd--;
41226104Ssteve				return retval;
4131556Srgrimes			}
4141556Srgrimes		} else {
4151556Srgrimes			for (jp = jobtab ; ; jp++) {
4161556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
41738536Scracauer					in_waitcmd--;
4181556Srgrimes					return 0;
4191556Srgrimes				}
4201556Srgrimes				if (jp->used && jp->state == 0)
4211556Srgrimes					break;
4221556Srgrimes			}
4231556Srgrimes		}
42438521Scracauer	} while (dowait(1, (struct job *)NULL) != -1);
42538521Scracauer	in_waitcmd--;
42638521Scracauer
42738521Scracauer	return 0;
4281556Srgrimes}
4291556Srgrimes
4301556Srgrimes
4311556Srgrimes
43217987Speterint
43390111Simpjobidcmd(int argc __unused, char **argv)
43417987Speter{
4351556Srgrimes	struct job *jp;
4361556Srgrimes	int i;
4371556Srgrimes
4381556Srgrimes	jp = getjob(argv[1]);
4391556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
4401556Srgrimes		out1fmt("%d", jp->ps[i].pid);
4411556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
4421556Srgrimes	}
4431556Srgrimes	return 0;
4441556Srgrimes}
4451556Srgrimes
4461556Srgrimes
4471556Srgrimes
4481556Srgrimes/*
4491556Srgrimes * Convert a job name to a job structure.
4501556Srgrimes */
4511556Srgrimes
4521556SrgrimesSTATIC struct job *
45390111Simpgetjob(char *name)
45490111Simp{
4551556Srgrimes	int jobno;
45625222Ssteve	struct job *jp;
4571556Srgrimes	int pid;
4581556Srgrimes	int i;
4591556Srgrimes
4601556Srgrimes	if (name == NULL) {
4611556Srgrimes#if JOBS
4621556Srgrimescurrentjob:
4631556Srgrimes		if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0)
4641556Srgrimes			error("No current job");
4651556Srgrimes		return &jobtab[jobno - 1];
4661556Srgrimes#else
4671556Srgrimes		error("No current job");
4681556Srgrimes#endif
4691556Srgrimes	} else if (name[0] == '%') {
4701556Srgrimes		if (is_digit(name[1])) {
4711556Srgrimes			jobno = number(name + 1);
4721556Srgrimes			if (jobno > 0 && jobno <= njobs
4731556Srgrimes			 && jobtab[jobno - 1].used != 0)
4741556Srgrimes				return &jobtab[jobno - 1];
4751556Srgrimes#if JOBS
4761556Srgrimes		} else if (name[1] == '%' && name[2] == '\0') {
4771556Srgrimes			goto currentjob;
4781556Srgrimes#endif
4791556Srgrimes		} else {
48025222Ssteve			struct job *found = NULL;
4811556Srgrimes			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
4821556Srgrimes				if (jp->used && jp->nprocs > 0
4831556Srgrimes				 && prefix(name + 1, jp->ps[0].cmd)) {
4841556Srgrimes					if (found)
4851556Srgrimes						error("%s: ambiguous", name);
4861556Srgrimes					found = jp;
4871556Srgrimes				}
4881556Srgrimes			}
4891556Srgrimes			if (found)
4901556Srgrimes				return found;
4911556Srgrimes		}
4921556Srgrimes	} else if (is_number(name)) {
4931556Srgrimes		pid = number(name);
4941556Srgrimes		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
4951556Srgrimes			if (jp->used && jp->nprocs > 0
4961556Srgrimes			 && jp->ps[jp->nprocs - 1].pid == pid)
4971556Srgrimes				return jp;
4981556Srgrimes		}
4991556Srgrimes	}
5001556Srgrimes	error("No such job: %s", name);
50117987Speter	/*NOTREACHED*/
50217987Speter	return NULL;
5031556Srgrimes}
5041556Srgrimes
5051556Srgrimes
5061556Srgrimes
5071556Srgrimes/*
5081556Srgrimes * Return a new job structure,
5091556Srgrimes */
5101556Srgrimes
5111556Srgrimesstruct job *
51290111Simpmakejob(union node *node __unused, int nprocs)
51317987Speter{
5141556Srgrimes	int i;
5151556Srgrimes	struct job *jp;
5161556Srgrimes
5171556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
5181556Srgrimes		if (--i < 0) {
5191556Srgrimes			INTOFF;
5201556Srgrimes			if (njobs == 0) {
5211556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
5221556Srgrimes			} else {
5231556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
52417987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
52520425Ssteve				/* Relocate `ps' pointers */
52620425Ssteve				for (i = 0; i < njobs; i++)
52720425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
52820425Ssteve						jp[i].ps = &jp[i].ps0;
5291556Srgrimes				ckfree(jobtab);
5301556Srgrimes				jobtab = jp;
5311556Srgrimes			}
5321556Srgrimes			jp = jobtab + njobs;
5331556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
5341556Srgrimes			INTON;
5351556Srgrimes			break;
5361556Srgrimes		}
5371556Srgrimes		if (jp->used == 0)
5381556Srgrimes			break;
5391556Srgrimes	}
5401556Srgrimes	INTOFF;
5411556Srgrimes	jp->state = 0;
5421556Srgrimes	jp->used = 1;
5431556Srgrimes	jp->changed = 0;
5441556Srgrimes	jp->nprocs = 0;
5451556Srgrimes#if JOBS
5461556Srgrimes	jp->jobctl = jobctl;
5471556Srgrimes#endif
5481556Srgrimes	if (nprocs > 1) {
5491556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
5501556Srgrimes	} else {
5511556Srgrimes		jp->ps = &jp->ps0;
5521556Srgrimes	}
5531556Srgrimes	INTON;
55417987Speter	TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
55517987Speter	    jp - jobtab + 1));
5561556Srgrimes	return jp;
5578855Srgrimes}
5581556Srgrimes
5591556Srgrimes
5601556Srgrimes/*
5611556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
5621556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
5631556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
5641556Srgrimes * be NULL.  The mode parameter can be one of the following:
5651556Srgrimes *	FORK_FG - Fork off a foreground process.
5661556Srgrimes *	FORK_BG - Fork off a background process.
5671556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
5681556Srgrimes *		     process group even if job control is on.
5691556Srgrimes *
5701556Srgrimes * When job control is turned off, background processes have their standard
5711556Srgrimes * input redirected to /dev/null (except for the second and later processes
5721556Srgrimes * in a pipeline).
5731556Srgrimes */
5741556Srgrimes
5751556Srgrimesint
57690111Simpforkshell(struct job *jp, union node *n, int mode)
57717987Speter{
5781556Srgrimes	int pid;
5791556Srgrimes	int pgrp;
5801556Srgrimes
58117987Speter	TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
58217987Speter	    mode));
5831556Srgrimes	INTOFF;
5841556Srgrimes	pid = fork();
5851556Srgrimes	if (pid == -1) {
5861556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
5871556Srgrimes		INTON;
58853891Scracauer		error("Cannot fork: %s", strerror(errno));
5891556Srgrimes	}
5901556Srgrimes	if (pid == 0) {
5911556Srgrimes		struct job *p;
5921556Srgrimes		int wasroot;
5931556Srgrimes		int i;
5941556Srgrimes
5951556Srgrimes		TRACE(("Child shell %d\n", getpid()));
5961556Srgrimes		wasroot = rootshell;
5971556Srgrimes		rootshell = 0;
5981556Srgrimes		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
5991556Srgrimes			if (p->used)
6001556Srgrimes				freejob(p);
6011556Srgrimes		closescript();
6021556Srgrimes		INTON;
6031556Srgrimes		clear_traps();
6041556Srgrimes#if JOBS
6051556Srgrimes		jobctl = 0;		/* do job control only in root shell */
6061556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
6071556Srgrimes			if (jp == NULL || jp->nprocs == 0)
6081556Srgrimes				pgrp = getpid();
6091556Srgrimes			else
6101556Srgrimes				pgrp = jp->ps[0].pid;
61121352Ssteve			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
6121556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
61320425Ssteve#ifdef OLD_TTY_DRIVER
6141556Srgrimes				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
61518016Speter					error("TIOCSPGRP failed, errno=%d", errno);
61620425Ssteve#else
61720425Ssteve				if (tcsetpgrp(2, pgrp) < 0)
61820425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
61920425Ssteve#endif
6201556Srgrimes			}
6211556Srgrimes			setsignal(SIGTSTP);
6221556Srgrimes			setsignal(SIGTTOU);
6231556Srgrimes		} else if (mode == FORK_BG) {
6241556Srgrimes			ignoresig(SIGINT);
6251556Srgrimes			ignoresig(SIGQUIT);
6261556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
6271556Srgrimes			    ! fd0_redirected_p ()) {
6281556Srgrimes				close(0);
62969793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
63069793Sobrien					error("Can't open %s: %s",
63169793Sobrien					    _PATH_DEVNULL, strerror(errno));
6321556Srgrimes			}
6331556Srgrimes		}
6341556Srgrimes#else
6351556Srgrimes		if (mode == FORK_BG) {
6361556Srgrimes			ignoresig(SIGINT);
6371556Srgrimes			ignoresig(SIGQUIT);
6381556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
6391556Srgrimes			    ! fd0_redirected_p ()) {
6401556Srgrimes				close(0);
64169793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
64269793Sobrien					error("Can't open %s: %s",
64369793Sobrien					    _PATH_DEVNULL, strerror(errno));
6441556Srgrimes			}
6451556Srgrimes		}
6461556Srgrimes#endif
6471556Srgrimes		if (wasroot && iflag) {
6481556Srgrimes			setsignal(SIGINT);
6491556Srgrimes			setsignal(SIGQUIT);
6501556Srgrimes			setsignal(SIGTERM);
6511556Srgrimes		}
6521556Srgrimes		return pid;
6531556Srgrimes	}
6541556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
6551556Srgrimes		if (jp == NULL || jp->nprocs == 0)
6561556Srgrimes			pgrp = pid;
6571556Srgrimes		else
6581556Srgrimes			pgrp = jp->ps[0].pid;
65917987Speter		setpgid(pid, pgrp);
6601556Srgrimes	}
6611556Srgrimes	if (mode == FORK_BG)
6621556Srgrimes		backgndpid = pid;		/* set $! */
6631556Srgrimes	if (jp) {
6641556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
6651556Srgrimes		ps->pid = pid;
6661556Srgrimes		ps->status = -1;
6671556Srgrimes		ps->cmd = nullstr;
6681556Srgrimes		if (iflag && rootshell && n)
6691556Srgrimes			ps->cmd = commandtext(n);
6701556Srgrimes	}
6711556Srgrimes	INTON;
6721556Srgrimes	TRACE(("In parent shell:  child = %d\n", pid));
6731556Srgrimes	return pid;
6741556Srgrimes}
6751556Srgrimes
6761556Srgrimes
6771556Srgrimes
6781556Srgrimes/*
6791556Srgrimes * Wait for job to finish.
6801556Srgrimes *
6811556Srgrimes * Under job control we have the problem that while a child process is
6821556Srgrimes * running interrupts generated by the user are sent to the child but not
6831556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
6841556Srgrimes * active user may be hard to kill.  With job control turned off, an
6851556Srgrimes * interactive user may place an interactive program inside a loop.  If
6861556Srgrimes * the interactive program catches interrupts, the user doesn't want
6871556Srgrimes * these interrupts to also abort the loop.  The approach we take here
6881556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
68946684Skris * foreground process to terminate, and then send itself an interrupt
6901556Srgrimes * signal if the child process was terminated by an interrupt signal.
6911556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
6921556Srgrimes * exit on interrupt; unless these processes terminate themselves by
6931556Srgrimes * sending a signal to themselves (instead of calling exit) they will
6941556Srgrimes * confuse this approach.
6951556Srgrimes */
6961556Srgrimes
6971556Srgrimesint
69890111Simpwaitforjob(struct job *jp, int *origstatus)
69945916Scracauer{
7001556Srgrimes#if JOBS
70117987Speter	int mypgrp = getpgrp();
7021556Srgrimes#endif
7031556Srgrimes	int status;
7041556Srgrimes	int st;
7051556Srgrimes
7061556Srgrimes	INTOFF;
7071556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
70838950Scracauer	while (jp->state == 0)
70938950Scracauer		if (dowait(1, jp) == -1)
71038950Scracauer			dotrap();
7111556Srgrimes#if JOBS
7121556Srgrimes	if (jp->jobctl) {
71320425Ssteve#ifdef OLD_TTY_DRIVER
7141556Srgrimes		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
71520425Ssteve			error("TIOCSPGRP failed, errno=%d\n", errno);
71620425Ssteve#else
71720425Ssteve		if (tcsetpgrp(2, mypgrp) < 0)
71820425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
71920425Ssteve#endif
7201556Srgrimes	}
7211556Srgrimes	if (jp->state == JOBSTOPPED)
7221556Srgrimes		curjob = jp - jobtab + 1;
7231556Srgrimes#endif
7241556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
72545916Scracauer	if (origstatus != NULL)
72645916Scracauer		*origstatus = status;
7271556Srgrimes	/* convert to 8 bits */
72826104Ssteve	if (WIFEXITED(status))
72926104Ssteve		st = WEXITSTATUS(status);
7301556Srgrimes#if JOBS
73126104Ssteve	else if (WIFSTOPPED(status))
73226104Ssteve		st = WSTOPSIG(status) + 128;
7331556Srgrimes#endif
7341556Srgrimes	else
73526104Ssteve		st = WTERMSIG(status) + 128;
7361556Srgrimes	if (! JOBS || jp->state == JOBDONE)
7371556Srgrimes		freejob(jp);
73838521Scracauer	if (int_pending()) {
73938521Scracauer		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
74038521Scracauer			kill(getpid(), SIGINT);
74138521Scracauer		else
74238521Scracauer			CLEAR_PENDING_INT;
74338521Scracauer	}
7441556Srgrimes	INTON;
7451556Srgrimes	return st;
7461556Srgrimes}
7471556Srgrimes
7481556Srgrimes
7491556Srgrimes
7501556Srgrimes/*
7511556Srgrimes * Wait for a process to terminate.
7521556Srgrimes */
7531556Srgrimes
7541556SrgrimesSTATIC int
75590111Simpdowait(int block, struct job *job)
75617987Speter{
7571556Srgrimes	int pid;
7581556Srgrimes	int status;
7591556Srgrimes	struct procstat *sp;
7601556Srgrimes	struct job *jp;
7611556Srgrimes	struct job *thisjob;
7621556Srgrimes	int done;
7631556Srgrimes	int stopped;
7641556Srgrimes	int core;
76526104Ssteve	int sig;
7661556Srgrimes
76738950Scracauer	in_dowait++;
7681556Srgrimes	TRACE(("dowait(%d) called\n", block));
7691556Srgrimes	do {
7701556Srgrimes		pid = waitproc(block, &status);
7711556Srgrimes		TRACE(("wait returns %d, status=%d\n", pid, status));
77272086Scracauer	} while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
77372086Scracauer	    (WIFSTOPPED(status) && !iflag));
77438950Scracauer	in_dowait--;
77538521Scracauer	if (breakwaitcmd != 0) {
77638521Scracauer		breakwaitcmd = 0;
77738521Scracauer		return -1;
77838521Scracauer	}
7791556Srgrimes	if (pid <= 0)
7801556Srgrimes		return pid;
7811556Srgrimes	INTOFF;
7821556Srgrimes	thisjob = NULL;
7831556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
7841556Srgrimes		if (jp->used) {
7851556Srgrimes			done = 1;
7861556Srgrimes			stopped = 1;
7871556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
7881556Srgrimes				if (sp->pid == -1)
7891556Srgrimes					continue;
7901556Srgrimes				if (sp->pid == pid) {
79126104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
79226104Ssteve						   pid, sp->status, status));
7931556Srgrimes					sp->status = status;
7941556Srgrimes					thisjob = jp;
7951556Srgrimes				}
7961556Srgrimes				if (sp->status == -1)
7971556Srgrimes					stopped = 0;
79826104Ssteve				else if (WIFSTOPPED(sp->status))
7991556Srgrimes					done = 0;
8001556Srgrimes			}
8011556Srgrimes			if (stopped) {		/* stopped or done */
8021556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
8031556Srgrimes				if (jp->state != state) {
8041556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
8051556Srgrimes					jp->state = state;
8061556Srgrimes#if JOBS
8071556Srgrimes					if (done && curjob == jp - jobtab + 1)
8081556Srgrimes						curjob = 0;		/* no current job */
8091556Srgrimes#endif
8101556Srgrimes				}
8111556Srgrimes			}
8121556Srgrimes		}
8131556Srgrimes	}
8141556Srgrimes	INTON;
8151556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
81626104Ssteve		core = WCOREDUMP(status);
8171556Srgrimes#if JOBS
81826104Ssteve		if (WIFSTOPPED(status))
81926104Ssteve			sig = WSTOPSIG(status);
82026104Ssteve		else
8211556Srgrimes#endif
82226104Ssteve			if (WIFEXITED(status))
82326104Ssteve				sig = 0;
82426104Ssteve			else
82526104Ssteve				sig = WTERMSIG(status);
82626104Ssteve
82726104Ssteve		if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
8281556Srgrimes			if (thisjob != job)
8291556Srgrimes				outfmt(out2, "%d: ", pid);
8301556Srgrimes#if JOBS
83126104Ssteve			if (sig == SIGTSTP && rootshell && iflag)
8321556Srgrimes				outfmt(out2, "%%%d ", job - jobtab + 1);
8331556Srgrimes#endif
83426104Ssteve			if (sig < NSIG && sys_siglist[sig])
83526104Ssteve				out2str(sys_siglist[sig]);
8361556Srgrimes			else
83726104Ssteve				outfmt(out2, "Signal %d", sig);
8381556Srgrimes			if (core)
8391556Srgrimes				out2str(" - core dumped");
8401556Srgrimes			out2c('\n');
8411556Srgrimes			flushout(&errout);
8421556Srgrimes		} else {
84326104Ssteve			TRACE(("Not printing status: status=%d, sig=%d\n",
84426104Ssteve				   status, sig));
8451556Srgrimes		}
8461556Srgrimes	} else {
8471556Srgrimes		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
8481556Srgrimes		if (thisjob)
8491556Srgrimes			thisjob->changed = 1;
8501556Srgrimes	}
8511556Srgrimes	return pid;
8521556Srgrimes}
8531556Srgrimes
8541556Srgrimes
8551556Srgrimes
8561556Srgrimes/*
8571556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
8581556Srgrimes * stopped processes.  If block is zero, we return a value of zero
8591556Srgrimes * rather than blocking.
8601556Srgrimes *
8611556Srgrimes * System V doesn't have a non-blocking wait system call.  It does
8621556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's
8631556Srgrimes * children dies.  The obvious way to use SIGCLD would be to install
8641556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD
8651556Srgrimes * was received, and have waitproc bump another counter when it got
8661556Srgrimes * the status of a process.  Waitproc would then know that a wait
8671556Srgrimes * system call would not block if the two counters were different.
8681556Srgrimes * This approach doesn't work because if a process has children that
8691556Srgrimes * have not been waited for, System V will send it a SIGCLD when it
8701556Srgrimes * installs a signal handler for SIGCLD.  What this means is that when
8711556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously
8721556Srgrimes * until is runs out of stack space, unless it does a wait call before
8731556Srgrimes * restoring the signal handler.  The code below takes advantage of
8741556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and
8751556Srgrimes * then checking to see whether it was called.  If there are any
8761556Srgrimes * children to be waited for, it will be.
8771556Srgrimes *
8781556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking
8791556Srgrimes * waits at all.  In this case, the user will not be informed when
8801556Srgrimes * a background process until the next time she runs a real program
8811556Srgrimes * (as opposed to running a builtin command or just typing return),
8821556Srgrimes * and the jobs command may give out of date information.
8831556Srgrimes */
8841556Srgrimes
8851556Srgrimes#ifdef SYSV
88638521ScracauerSTATIC sig_atomic_t gotsigchild;
8871556Srgrimes
8881556SrgrimesSTATIC int onsigchild() {
8891556Srgrimes	gotsigchild = 1;
8901556Srgrimes}
8911556Srgrimes#endif
8921556Srgrimes
8931556Srgrimes
8941556SrgrimesSTATIC int
89590111Simpwaitproc(int block, int *status)
89617987Speter{
8971556Srgrimes#ifdef BSD
8981556Srgrimes	int flags;
8991556Srgrimes
9001556Srgrimes#if JOBS
9011556Srgrimes	flags = WUNTRACED;
9021556Srgrimes#else
9031556Srgrimes	flags = 0;
9041556Srgrimes#endif
9051556Srgrimes	if (block == 0)
9061556Srgrimes		flags |= WNOHANG;
9071556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
9081556Srgrimes#else
9091556Srgrimes#ifdef SYSV
9101556Srgrimes	int (*save)();
9111556Srgrimes
9121556Srgrimes	if (block == 0) {
9131556Srgrimes		gotsigchild = 0;
9141556Srgrimes		save = signal(SIGCLD, onsigchild);
9151556Srgrimes		signal(SIGCLD, save);
9161556Srgrimes		if (gotsigchild == 0)
9171556Srgrimes			return 0;
9181556Srgrimes	}
9191556Srgrimes	return wait(status);
9201556Srgrimes#else
9211556Srgrimes	if (block == 0)
9221556Srgrimes		return 0;
9231556Srgrimes	return wait(status);
9241556Srgrimes#endif
9251556Srgrimes#endif
9261556Srgrimes}
9271556Srgrimes
9281556Srgrimes/*
9291556Srgrimes * return 1 if there are stopped jobs, otherwise 0
9301556Srgrimes */
9311556Srgrimesint job_warning = 0;
9321556Srgrimesint
93390111Simpstoppedjobs(void)
9341556Srgrimes{
93525222Ssteve	int jobno;
93625222Ssteve	struct job *jp;
9371556Srgrimes
9381556Srgrimes	if (job_warning)
9391556Srgrimes		return (0);
9401556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
9411556Srgrimes		if (jp->used == 0)
9421556Srgrimes			continue;
9431556Srgrimes		if (jp->state == JOBSTOPPED) {
9441556Srgrimes			out2str("You have stopped jobs.\n");
9451556Srgrimes			job_warning = 2;
9461556Srgrimes			return (1);
9471556Srgrimes		}
9481556Srgrimes	}
9491556Srgrimes
9501556Srgrimes	return (0);
9511556Srgrimes}
9521556Srgrimes
9531556Srgrimes/*
9541556Srgrimes * Return a string identifying a command (to be printed by the
9551556Srgrimes * jobs command.
9561556Srgrimes */
9571556Srgrimes
9581556SrgrimesSTATIC char *cmdnextc;
9591556SrgrimesSTATIC int cmdnleft;
9601556Srgrimes#define MAXCMDTEXT	200
9611556Srgrimes
9621556Srgrimeschar *
96390111Simpcommandtext(union node *n)
96490111Simp{
9651556Srgrimes	char *name;
9661556Srgrimes
9671556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
9681556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
9691556Srgrimes	cmdtxt(n);
9701556Srgrimes	*cmdnextc = '\0';
9711556Srgrimes	return name;
9721556Srgrimes}
9731556Srgrimes
9741556Srgrimes
9751556SrgrimesSTATIC void
97690111Simpcmdtxt(union node *n)
97790111Simp{
9781556Srgrimes	union node *np;
9791556Srgrimes	struct nodelist *lp;
9801556Srgrimes	char *p;
9811556Srgrimes	int i;
9821556Srgrimes	char s[2];
9831556Srgrimes
9841556Srgrimes	if (n == NULL)
9851556Srgrimes		return;
9861556Srgrimes	switch (n->type) {
9871556Srgrimes	case NSEMI:
9881556Srgrimes		cmdtxt(n->nbinary.ch1);
9891556Srgrimes		cmdputs("; ");
9901556Srgrimes		cmdtxt(n->nbinary.ch2);
9911556Srgrimes		break;
9921556Srgrimes	case NAND:
9931556Srgrimes		cmdtxt(n->nbinary.ch1);
9941556Srgrimes		cmdputs(" && ");
9951556Srgrimes		cmdtxt(n->nbinary.ch2);
9961556Srgrimes		break;
9971556Srgrimes	case NOR:
9981556Srgrimes		cmdtxt(n->nbinary.ch1);
9991556Srgrimes		cmdputs(" || ");
10001556Srgrimes		cmdtxt(n->nbinary.ch2);
10011556Srgrimes		break;
10021556Srgrimes	case NPIPE:
10031556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
10041556Srgrimes			cmdtxt(lp->n);
10051556Srgrimes			if (lp->next)
10061556Srgrimes				cmdputs(" | ");
10071556Srgrimes		}
10081556Srgrimes		break;
10091556Srgrimes	case NSUBSHELL:
10101556Srgrimes		cmdputs("(");
10111556Srgrimes		cmdtxt(n->nredir.n);
10121556Srgrimes		cmdputs(")");
10131556Srgrimes		break;
10141556Srgrimes	case NREDIR:
10151556Srgrimes	case NBACKGND:
10161556Srgrimes		cmdtxt(n->nredir.n);
10171556Srgrimes		break;
10181556Srgrimes	case NIF:
10191556Srgrimes		cmdputs("if ");
10201556Srgrimes		cmdtxt(n->nif.test);
10211556Srgrimes		cmdputs("; then ");
10221556Srgrimes		cmdtxt(n->nif.ifpart);
10231556Srgrimes		cmdputs("...");
10241556Srgrimes		break;
10251556Srgrimes	case NWHILE:
10261556Srgrimes		cmdputs("while ");
10271556Srgrimes		goto until;
10281556Srgrimes	case NUNTIL:
10291556Srgrimes		cmdputs("until ");
10301556Srgrimesuntil:
10311556Srgrimes		cmdtxt(n->nbinary.ch1);
10321556Srgrimes		cmdputs("; do ");
10331556Srgrimes		cmdtxt(n->nbinary.ch2);
10341556Srgrimes		cmdputs("; done");
10351556Srgrimes		break;
10361556Srgrimes	case NFOR:
10371556Srgrimes		cmdputs("for ");
10381556Srgrimes		cmdputs(n->nfor.var);
10391556Srgrimes		cmdputs(" in ...");
10401556Srgrimes		break;
10411556Srgrimes	case NCASE:
10421556Srgrimes		cmdputs("case ");
10431556Srgrimes		cmdputs(n->ncase.expr->narg.text);
10441556Srgrimes		cmdputs(" in ...");
10451556Srgrimes		break;
10461556Srgrimes	case NDEFUN:
10471556Srgrimes		cmdputs(n->narg.text);
10481556Srgrimes		cmdputs("() ...");
10491556Srgrimes		break;
10501556Srgrimes	case NCMD:
10511556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
10521556Srgrimes			cmdtxt(np);
10531556Srgrimes			if (np->narg.next)
10541556Srgrimes				cmdputs(" ");
10551556Srgrimes		}
10561556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
10571556Srgrimes			cmdputs(" ");
10581556Srgrimes			cmdtxt(np);
10591556Srgrimes		}
10601556Srgrimes		break;
10611556Srgrimes	case NARG:
10621556Srgrimes		cmdputs(n->narg.text);
10631556Srgrimes		break;
10641556Srgrimes	case NTO:
10651556Srgrimes		p = ">";  i = 1;  goto redir;
10661556Srgrimes	case NAPPEND:
10671556Srgrimes		p = ">>";  i = 1;  goto redir;
10681556Srgrimes	case NTOFD:
10691556Srgrimes		p = ">&";  i = 1;  goto redir;
107096922Stjr	case NCLOBBER:
107196922Stjr		p = ">|"; i = 1; goto redir;
10721556Srgrimes	case NFROM:
10731556Srgrimes		p = "<";  i = 0;  goto redir;
107466612Sbrian	case NFROMTO:
107566612Sbrian		p = "<>";  i = 0;  goto redir;
10761556Srgrimes	case NFROMFD:
10771556Srgrimes		p = "<&";  i = 0;  goto redir;
10781556Srgrimesredir:
10791556Srgrimes		if (n->nfile.fd != i) {
10801556Srgrimes			s[0] = n->nfile.fd + '0';
10811556Srgrimes			s[1] = '\0';
10821556Srgrimes			cmdputs(s);
10831556Srgrimes		}
10841556Srgrimes		cmdputs(p);
10851556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
10861556Srgrimes			s[0] = n->ndup.dupfd + '0';
10871556Srgrimes			s[1] = '\0';
10881556Srgrimes			cmdputs(s);
10891556Srgrimes		} else {
10901556Srgrimes			cmdtxt(n->nfile.fname);
10911556Srgrimes		}
10921556Srgrimes		break;
10931556Srgrimes	case NHERE:
10941556Srgrimes	case NXHERE:
10951556Srgrimes		cmdputs("<<...");
10961556Srgrimes		break;
10971556Srgrimes	default:
10981556Srgrimes		cmdputs("???");
10991556Srgrimes		break;
11001556Srgrimes	}
11011556Srgrimes}
11021556Srgrimes
11031556Srgrimes
11041556Srgrimes
11051556SrgrimesSTATIC void
110690111Simpcmdputs(char *s)
110790111Simp{
110825222Ssteve	char *p, *q;
110925222Ssteve	char c;
11101556Srgrimes	int subtype = 0;
11111556Srgrimes
11121556Srgrimes	if (cmdnleft <= 0)
11131556Srgrimes		return;
11141556Srgrimes	p = s;
11151556Srgrimes	q = cmdnextc;
11161556Srgrimes	while ((c = *p++) != '\0') {
11171556Srgrimes		if (c == CTLESC)
11181556Srgrimes			*q++ = *p++;
11191556Srgrimes		else if (c == CTLVAR) {
11201556Srgrimes			*q++ = '$';
11211556Srgrimes			if (--cmdnleft > 0)
11221556Srgrimes				*q++ = '{';
11231556Srgrimes			subtype = *p++;
11241556Srgrimes		} else if (c == '=' && subtype != 0) {
11251556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
11261556Srgrimes			subtype = 0;
11271556Srgrimes		} else if (c == CTLENDVAR) {
11281556Srgrimes			*q++ = '}';
112918954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
11301556Srgrimes			cmdnleft++;		/* ignore it */
11311556Srgrimes		else
11321556Srgrimes			*q++ = c;
11331556Srgrimes		if (--cmdnleft <= 0) {
11341556Srgrimes			*q++ = '.';
11351556Srgrimes			*q++ = '.';
11361556Srgrimes			*q++ = '.';
11371556Srgrimes			break;
11381556Srgrimes		}
11391556Srgrimes	}
11401556Srgrimes	cmdnextc = q;
11411556Srgrimes}
1142