jobs.c revision 53891
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 53891 1999-11-29 19:11:01Z cracauer $";
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>
5517987Speter#endif
5618018Speter#include <sys/ioctl.h>
5717987Speter
581556Srgrimes#include "shell.h"
591556Srgrimes#if JOBS
6025222Ssteve#if OLD_TTY_DRIVER
611556Srgrimes#include "sgtty.h"
6217987Speter#else
6317987Speter#include <termios.h>
6417987Speter#endif
651556Srgrimes#undef CEOF			/* syntax.h redefines this */
661556Srgrimes#endif
6717987Speter#include "redir.h"
6817987Speter#include "show.h"
691556Srgrimes#include "main.h"
701556Srgrimes#include "parser.h"
711556Srgrimes#include "nodes.h"
721556Srgrimes#include "jobs.h"
731556Srgrimes#include "options.h"
741556Srgrimes#include "trap.h"
751556Srgrimes#include "syntax.h"
761556Srgrimes#include "input.h"
771556Srgrimes#include "output.h"
781556Srgrimes#include "memalloc.h"
791556Srgrimes#include "error.h"
801556Srgrimes#include "mystring.h"
811556Srgrimes
821556Srgrimes
831556Srgrimesstruct job *jobtab;		/* array of jobs */
841556Srgrimesint njobs;			/* size of array */
8528346SsteveMKINIT pid_t backgndpid = -1;	/* pid of last background process */
861556Srgrimes#if JOBS
871556Srgrimesint initialpgrp;		/* pgrp of shell on invocation */
8828346Ssteveint curjob;			/* current job */
891556Srgrimes#endif
9038536Scracauerint in_waitcmd = 0;		/* are we in waitcmd()? */
9138950Scracauerint in_dowait = 0;		/* are we in dowait()? */
9238536Scracauervolatile sig_atomic_t breakwaitcmd = 0;	/* should wait be terminated? */
931556Srgrimes
9420425Ssteve#if JOBS
9517987SpeterSTATIC void restartjob __P((struct job *));
9620425Ssteve#endif
9717987SpeterSTATIC void freejob __P((struct job *));
9817987SpeterSTATIC struct job *getjob __P((char *));
9917987SpeterSTATIC int dowait __P((int, struct job *));
10020425Ssteve#if SYSV
10138536ScracauerSTATIC int onsigchild __P((void));
10220425Ssteve#endif
10317987SpeterSTATIC int waitproc __P((int, int *));
10417987SpeterSTATIC void cmdtxt __P((union node *));
10517987SpeterSTATIC void cmdputs __P((char *));
1061556Srgrimes
1071556Srgrimes
1081556Srgrimes/*
1091556Srgrimes * Turn job control on and off.
1101556Srgrimes *
1111556Srgrimes * Note:  This code assumes that the third arg to ioctl is a character
1121556Srgrimes * pointer, which is true on Berkeley systems but not System V.  Since
1131556Srgrimes * System V doesn't have job control yet, this isn't a problem now.
1141556Srgrimes */
1151556Srgrimes
1161556SrgrimesMKINIT int jobctl;
1171556Srgrimes
11820425Ssteve#if JOBS
1191556Srgrimesvoid
12020425Sstevesetjobctl(on)
12117987Speter	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
19717987Speterfgcmd(argc, argv)
19825905Ssteve	int argc __unused;
19920425Ssteve	char **argv;
20017987Speter{
2011556Srgrimes	struct job *jp;
2021556Srgrimes	int pgrp;
2031556Srgrimes	int status;
2041556Srgrimes
2051556Srgrimes	jp = getjob(argv[1]);
2061556Srgrimes	if (jp->jobctl == 0)
2071556Srgrimes		error("job not created under job control");
2081556Srgrimes	pgrp = jp->ps[0].pid;
20920425Ssteve#ifdef OLD_TTY_DRIVER
2101556Srgrimes	ioctl(2, TIOCSPGRP, (char *)&pgrp);
21120425Ssteve#else
21220425Ssteve	tcsetpgrp(2, pgrp);
21320425Ssteve#endif
2141556Srgrimes	restartjob(jp);
2151556Srgrimes	INTOFF;
21645916Scracauer	status = waitforjob(jp, (int *)NULL);
2171556Srgrimes	INTON;
2181556Srgrimes	return status;
2191556Srgrimes}
2201556Srgrimes
2211556Srgrimes
22217987Speterint
22317987Speterbgcmd(argc, argv)
22417987Speter	int argc;
22520425Ssteve	char **argv;
22617987Speter{
2271556Srgrimes	struct job *jp;
2281556Srgrimes
2291556Srgrimes	do {
2301556Srgrimes		jp = getjob(*++argv);
2311556Srgrimes		if (jp->jobctl == 0)
2321556Srgrimes			error("job not created under job control");
2331556Srgrimes		restartjob(jp);
2341556Srgrimes	} while (--argc > 1);
2351556Srgrimes	return 0;
2361556Srgrimes}
2371556Srgrimes
2381556Srgrimes
2391556SrgrimesSTATIC void
2401556Srgrimesrestartjob(jp)
2411556Srgrimes	struct job *jp;
24217987Speter{
2431556Srgrimes	struct procstat *ps;
2441556Srgrimes	int i;
2451556Srgrimes
2461556Srgrimes	if (jp->state == JOBDONE)
2471556Srgrimes		return;
2481556Srgrimes	INTOFF;
2491556Srgrimes	killpg(jp->ps[0].pid, SIGCONT);
2501556Srgrimes	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
25126104Ssteve		if (WIFSTOPPED(ps->status)) {
2521556Srgrimes			ps->status = -1;
2531556Srgrimes			jp->state = 0;
2541556Srgrimes		}
2551556Srgrimes	}
2561556Srgrimes	INTON;
2571556Srgrimes}
2581556Srgrimes#endif
2591556Srgrimes
2601556Srgrimes
2611556Srgrimesint
26217987Speterjobscmd(argc, argv)
26325905Ssteve	int argc __unused;
26425905Ssteve	char **argv __unused;
26517987Speter{
2661556Srgrimes	showjobs(0);
2671556Srgrimes	return 0;
2681556Srgrimes}
2691556Srgrimes
2701556Srgrimes
2711556Srgrimes/*
2721556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
2731556Srgrimes * statuses have changed since the last call to showjobs.
2741556Srgrimes *
2751556Srgrimes * If the shell is interrupted in the process of creating a job, the
2761556Srgrimes * result may be a job structure containing zero processes.  Such structures
2771556Srgrimes * will be freed here.
2781556Srgrimes */
2791556Srgrimes
2801556Srgrimesvoid
28120425Ssteveshowjobs(change)
28217987Speter	int change;
28317987Speter{
2841556Srgrimes	int jobno;
2851556Srgrimes	int procno;
2861556Srgrimes	int i;
2871556Srgrimes	struct job *jp;
2881556Srgrimes	struct procstat *ps;
2891556Srgrimes	int col;
2901556Srgrimes	char s[64];
2911556Srgrimes
2921556Srgrimes	TRACE(("showjobs(%d) called\n", change));
2931556Srgrimes	while (dowait(0, (struct job *)NULL) > 0);
2941556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
2951556Srgrimes		if (! jp->used)
2961556Srgrimes			continue;
2971556Srgrimes		if (jp->nprocs == 0) {
2981556Srgrimes			freejob(jp);
2991556Srgrimes			continue;
3001556Srgrimes		}
3011556Srgrimes		if (change && ! jp->changed)
3021556Srgrimes			continue;
3031556Srgrimes		procno = jp->nprocs;
3041556Srgrimes		for (ps = jp->ps ; ; ps++) {	/* for each process */
3051556Srgrimes			if (ps == jp->ps)
3061556Srgrimes				fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
3071556Srgrimes			else
3081556Srgrimes				fmtstr(s, 64, "    %d ", ps->pid);
3091556Srgrimes			out1str(s);
3101556Srgrimes			col = strlen(s);
3111556Srgrimes			s[0] = '\0';
3121556Srgrimes			if (ps->status == -1) {
3131556Srgrimes				/* don't print anything */
31426104Ssteve			} else if (WIFEXITED(ps->status)) {
31526104Ssteve				fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status));
3161556Srgrimes			} else {
3171556Srgrimes#if JOBS
31826104Ssteve				if (WIFSTOPPED(ps->status))
31926104Ssteve					i = WSTOPSIG(ps->status);
32026104Ssteve				else
3211556Srgrimes#endif
32226104Ssteve					i = WTERMSIG(ps->status);
32317987Speter				if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
32417987Speter					scopy(sys_siglist[i & 0x7F], s);
3251556Srgrimes				else
3261556Srgrimes					fmtstr(s, 64, "Signal %d", i & 0x7F);
32726104Ssteve				if (WCOREDUMP(ps->status))
3281556Srgrimes					strcat(s, " (core dumped)");
3291556Srgrimes			}
3301556Srgrimes			out1str(s);
3311556Srgrimes			col += strlen(s);
3321556Srgrimes			do {
3331556Srgrimes				out1c(' ');
3341556Srgrimes				col++;
3351556Srgrimes			} while (col < 30);
3361556Srgrimes			out1str(ps->cmd);
3371556Srgrimes			out1c('\n');
3381556Srgrimes			if (--procno <= 0)
3391556Srgrimes				break;
3401556Srgrimes		}
3411556Srgrimes		jp->changed = 0;
3421556Srgrimes		if (jp->state == JOBDONE) {
3431556Srgrimes			freejob(jp);
3441556Srgrimes		}
3451556Srgrimes	}
3461556Srgrimes}
3471556Srgrimes
3481556Srgrimes
3491556Srgrimes/*
3501556Srgrimes * Mark a job structure as unused.
3511556Srgrimes */
3521556Srgrimes
3531556SrgrimesSTATIC void
3541556Srgrimesfreejob(jp)
3551556Srgrimes	struct job *jp;
3561556Srgrimes	{
3571556Srgrimes	struct procstat *ps;
3581556Srgrimes	int i;
3591556Srgrimes
3601556Srgrimes	INTOFF;
3611556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
3621556Srgrimes		if (ps->cmd != nullstr)
3631556Srgrimes			ckfree(ps->cmd);
3641556Srgrimes	}
3651556Srgrimes	if (jp->ps != &jp->ps0)
3661556Srgrimes		ckfree(jp->ps);
3671556Srgrimes	jp->used = 0;
3681556Srgrimes#if JOBS
3691556Srgrimes	if (curjob == jp - jobtab + 1)
3701556Srgrimes		curjob = 0;
3711556Srgrimes#endif
3721556Srgrimes	INTON;
3731556Srgrimes}
3741556Srgrimes
3751556Srgrimes
3761556Srgrimes
3771556Srgrimesint
37820425Sstevewaitcmd(argc, argv)
37917987Speter	int argc;
38020425Ssteve	char **argv;
38117987Speter{
3821556Srgrimes	struct job *job;
38326104Ssteve	int status, retval;
3841556Srgrimes	struct job *jp;
3851556Srgrimes
3861556Srgrimes	if (argc > 1) {
3871556Srgrimes		job = getjob(argv[1]);
3881556Srgrimes	} else {
3891556Srgrimes		job = NULL;
3901556Srgrimes	}
39138536Scracauer
39238536Scracauer	/*
39338536Scracauer	 * Loop until a process is terminated or stopped, or a SIGINT is
39438536Scracauer	 * received.
39538536Scracauer	 */
39638536Scracauer
39738521Scracauer	in_waitcmd++;
39838536Scracauer	do {
3991556Srgrimes		if (job != NULL) {
4001556Srgrimes			if (job->state) {
4011556Srgrimes				status = job->ps[job->nprocs - 1].status;
40226104Ssteve				if (WIFEXITED(status))
40326104Ssteve					retval = WEXITSTATUS(status);
4041556Srgrimes#if JOBS
40526104Ssteve				else if (WIFSTOPPED(status))
40626104Ssteve					retval = WSTOPSIG(status) + 128;
4071556Srgrimes#endif
4081556Srgrimes				else
40926104Ssteve					retval = WTERMSIG(status) + 128;
4101556Srgrimes				if (! iflag)
4111556Srgrimes					freejob(job);
41238536Scracauer				in_waitcmd--;
41326104Ssteve				return retval;
4141556Srgrimes			}
4151556Srgrimes		} else {
4161556Srgrimes			for (jp = jobtab ; ; jp++) {
4171556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
41838536Scracauer					in_waitcmd--;
4191556Srgrimes					return 0;
4201556Srgrimes				}
4211556Srgrimes				if (jp->used && jp->state == 0)
4221556Srgrimes					break;
4231556Srgrimes			}
4241556Srgrimes		}
42538521Scracauer	} while (dowait(1, (struct job *)NULL) != -1);
42638521Scracauer	in_waitcmd--;
42738521Scracauer
42838521Scracauer	return 0;
4291556Srgrimes}
4301556Srgrimes
4311556Srgrimes
4321556Srgrimes
43317987Speterint
43420425Sstevejobidcmd(argc, argv)
43525905Ssteve	int argc __unused;
43620425Ssteve	char **argv;
43717987Speter{
4381556Srgrimes	struct job *jp;
4391556Srgrimes	int i;
4401556Srgrimes
4411556Srgrimes	jp = getjob(argv[1]);
4421556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
4431556Srgrimes		out1fmt("%d", jp->ps[i].pid);
4441556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
4451556Srgrimes	}
4461556Srgrimes	return 0;
4471556Srgrimes}
4481556Srgrimes
4491556Srgrimes
4501556Srgrimes
4511556Srgrimes/*
4521556Srgrimes * Convert a job name to a job structure.
4531556Srgrimes */
4541556Srgrimes
4551556SrgrimesSTATIC struct job *
4561556Srgrimesgetjob(name)
4571556Srgrimes	char *name;
4581556Srgrimes	{
4591556Srgrimes	int jobno;
46025222Ssteve	struct job *jp;
4611556Srgrimes	int pid;
4621556Srgrimes	int i;
4631556Srgrimes
4641556Srgrimes	if (name == NULL) {
4651556Srgrimes#if JOBS
4661556Srgrimescurrentjob:
4671556Srgrimes		if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0)
4681556Srgrimes			error("No current job");
4691556Srgrimes		return &jobtab[jobno - 1];
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 *
5161556Srgrimesmakejob(node, nprocs)
51725905Ssteve	union node *node __unused;
51817987Speter	int nprocs;
51917987Speter{
5201556Srgrimes	int i;
5211556Srgrimes	struct job *jp;
5221556Srgrimes
5231556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
5241556Srgrimes		if (--i < 0) {
5251556Srgrimes			INTOFF;
5261556Srgrimes			if (njobs == 0) {
5271556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
5281556Srgrimes			} else {
5291556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
53017987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
53120425Ssteve				/* Relocate `ps' pointers */
53220425Ssteve				for (i = 0; i < njobs; i++)
53320425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
53420425Ssteve						jp[i].ps = &jp[i].ps0;
5351556Srgrimes				ckfree(jobtab);
5361556Srgrimes				jobtab = jp;
5371556Srgrimes			}
5381556Srgrimes			jp = jobtab + njobs;
5391556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
5401556Srgrimes			INTON;
5411556Srgrimes			break;
5421556Srgrimes		}
5431556Srgrimes		if (jp->used == 0)
5441556Srgrimes			break;
5451556Srgrimes	}
5461556Srgrimes	INTOFF;
5471556Srgrimes	jp->state = 0;
5481556Srgrimes	jp->used = 1;
5491556Srgrimes	jp->changed = 0;
5501556Srgrimes	jp->nprocs = 0;
5511556Srgrimes#if JOBS
5521556Srgrimes	jp->jobctl = jobctl;
5531556Srgrimes#endif
5541556Srgrimes	if (nprocs > 1) {
5551556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
5561556Srgrimes	} else {
5571556Srgrimes		jp->ps = &jp->ps0;
5581556Srgrimes	}
5591556Srgrimes	INTON;
56017987Speter	TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
56117987Speter	    jp - jobtab + 1));
5621556Srgrimes	return jp;
5638855Srgrimes}
5641556Srgrimes
5651556Srgrimes
5661556Srgrimes/*
5671556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
5681556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
5691556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
5701556Srgrimes * be NULL.  The mode parameter can be one of the following:
5711556Srgrimes *	FORK_FG - Fork off a foreground process.
5721556Srgrimes *	FORK_BG - Fork off a background process.
5731556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
5741556Srgrimes *		     process group even if job control is on.
5751556Srgrimes *
5761556Srgrimes * When job control is turned off, background processes have their standard
5771556Srgrimes * input redirected to /dev/null (except for the second and later processes
5781556Srgrimes * in a pipeline).
5791556Srgrimes */
5801556Srgrimes
5811556Srgrimesint
5821556Srgrimesforkshell(jp, n, mode)
5831556Srgrimes	union node *n;
5841556Srgrimes	struct job *jp;
58517987Speter	int mode;
58617987Speter{
5871556Srgrimes	int pid;
5881556Srgrimes	int pgrp;
5891556Srgrimes
59017987Speter	TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
59117987Speter	    mode));
5921556Srgrimes	INTOFF;
5931556Srgrimes	pid = fork();
5941556Srgrimes	if (pid == -1) {
5951556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
5961556Srgrimes		INTON;
59753891Scracauer		error("Cannot fork: %s", strerror(errno));
5981556Srgrimes	}
5991556Srgrimes	if (pid == 0) {
6001556Srgrimes		struct job *p;
6011556Srgrimes		int wasroot;
6021556Srgrimes		int i;
6031556Srgrimes
6041556Srgrimes		TRACE(("Child shell %d\n", getpid()));
6051556Srgrimes		wasroot = rootshell;
6061556Srgrimes		rootshell = 0;
6071556Srgrimes		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
6081556Srgrimes			if (p->used)
6091556Srgrimes				freejob(p);
6101556Srgrimes		closescript();
6111556Srgrimes		INTON;
6121556Srgrimes		clear_traps();
6131556Srgrimes#if JOBS
6141556Srgrimes		jobctl = 0;		/* do job control only in root shell */
6151556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
6161556Srgrimes			if (jp == NULL || jp->nprocs == 0)
6171556Srgrimes				pgrp = getpid();
6181556Srgrimes			else
6191556Srgrimes				pgrp = jp->ps[0].pid;
62021352Ssteve			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
6211556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
62220425Ssteve#ifdef OLD_TTY_DRIVER
6231556Srgrimes				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
62418016Speter					error("TIOCSPGRP failed, errno=%d", errno);
62520425Ssteve#else
62620425Ssteve				if (tcsetpgrp(2, pgrp) < 0)
62720425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
62820425Ssteve#endif
6291556Srgrimes			}
6301556Srgrimes			setsignal(SIGTSTP);
6311556Srgrimes			setsignal(SIGTTOU);
6321556Srgrimes		} else if (mode == FORK_BG) {
6331556Srgrimes			ignoresig(SIGINT);
6341556Srgrimes			ignoresig(SIGQUIT);
6351556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
6361556Srgrimes			    ! fd0_redirected_p ()) {
6371556Srgrimes				close(0);
6381556Srgrimes				if (open("/dev/null", O_RDONLY) != 0)
63953891Scracauer					error("Can't open /dev/null: %s",
64053891Scracauer					    strerror(errno));
6411556Srgrimes			}
6421556Srgrimes		}
6431556Srgrimes#else
6441556Srgrimes		if (mode == FORK_BG) {
6451556Srgrimes			ignoresig(SIGINT);
6461556Srgrimes			ignoresig(SIGQUIT);
6471556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
6481556Srgrimes			    ! fd0_redirected_p ()) {
6491556Srgrimes				close(0);
6501556Srgrimes				if (open("/dev/null", O_RDONLY) != 0)
65153891Scracauer					error("Can't open /dev/null: %s",
65253891Scracauer					    strerror(errno));
6531556Srgrimes			}
6541556Srgrimes		}
6551556Srgrimes#endif
6561556Srgrimes		if (wasroot && iflag) {
6571556Srgrimes			setsignal(SIGINT);
6581556Srgrimes			setsignal(SIGQUIT);
6591556Srgrimes			setsignal(SIGTERM);
6601556Srgrimes		}
6611556Srgrimes		return pid;
6621556Srgrimes	}
6631556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
6641556Srgrimes		if (jp == NULL || jp->nprocs == 0)
6651556Srgrimes			pgrp = pid;
6661556Srgrimes		else
6671556Srgrimes			pgrp = jp->ps[0].pid;
66817987Speter		setpgid(pid, pgrp);
6691556Srgrimes	}
6701556Srgrimes	if (mode == FORK_BG)
6711556Srgrimes		backgndpid = pid;		/* set $! */
6721556Srgrimes	if (jp) {
6731556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
6741556Srgrimes		ps->pid = pid;
6751556Srgrimes		ps->status = -1;
6761556Srgrimes		ps->cmd = nullstr;
6771556Srgrimes		if (iflag && rootshell && n)
6781556Srgrimes			ps->cmd = commandtext(n);
6791556Srgrimes	}
6801556Srgrimes	INTON;
6811556Srgrimes	TRACE(("In parent shell:  child = %d\n", pid));
6821556Srgrimes	return pid;
6831556Srgrimes}
6841556Srgrimes
6851556Srgrimes
6861556Srgrimes
6871556Srgrimes/*
6881556Srgrimes * Wait for job to finish.
6891556Srgrimes *
6901556Srgrimes * Under job control we have the problem that while a child process is
6911556Srgrimes * running interrupts generated by the user are sent to the child but not
6921556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
6931556Srgrimes * active user may be hard to kill.  With job control turned off, an
6941556Srgrimes * interactive user may place an interactive program inside a loop.  If
6951556Srgrimes * the interactive program catches interrupts, the user doesn't want
6961556Srgrimes * these interrupts to also abort the loop.  The approach we take here
6971556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
69846684Skris * foreground process to terminate, and then send itself an interrupt
6991556Srgrimes * signal if the child process was terminated by an interrupt signal.
7001556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
7011556Srgrimes * exit on interrupt; unless these processes terminate themselves by
7021556Srgrimes * sending a signal to themselves (instead of calling exit) they will
7031556Srgrimes * confuse this approach.
7041556Srgrimes */
7051556Srgrimes
7061556Srgrimesint
70745916Scracauerwaitforjob(jp, origstatus)
70825222Ssteve	struct job *jp;
70945916Scracauer	int *origstatus;
71045916Scracauer{
7111556Srgrimes#if JOBS
71217987Speter	int mypgrp = getpgrp();
7131556Srgrimes#endif
7141556Srgrimes	int status;
7151556Srgrimes	int st;
7161556Srgrimes
7171556Srgrimes	INTOFF;
7181556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
71938950Scracauer	while (jp->state == 0)
72038950Scracauer		if (dowait(1, jp) == -1)
72138950Scracauer			dotrap();
7221556Srgrimes#if JOBS
7231556Srgrimes	if (jp->jobctl) {
72420425Ssteve#ifdef OLD_TTY_DRIVER
7251556Srgrimes		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
72620425Ssteve			error("TIOCSPGRP failed, errno=%d\n", errno);
72720425Ssteve#else
72820425Ssteve		if (tcsetpgrp(2, mypgrp) < 0)
72920425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
73020425Ssteve#endif
7311556Srgrimes	}
7321556Srgrimes	if (jp->state == JOBSTOPPED)
7331556Srgrimes		curjob = jp - jobtab + 1;
7341556Srgrimes#endif
7351556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
73645916Scracauer	if (origstatus != NULL)
73745916Scracauer		*origstatus = status;
7381556Srgrimes	/* convert to 8 bits */
73926104Ssteve	if (WIFEXITED(status))
74026104Ssteve		st = WEXITSTATUS(status);
7411556Srgrimes#if JOBS
74226104Ssteve	else if (WIFSTOPPED(status))
74326104Ssteve		st = WSTOPSIG(status) + 128;
7441556Srgrimes#endif
7451556Srgrimes	else
74626104Ssteve		st = WTERMSIG(status) + 128;
7471556Srgrimes	if (! JOBS || jp->state == JOBDONE)
7481556Srgrimes		freejob(jp);
74938521Scracauer	if (int_pending()) {
75038521Scracauer		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
75138521Scracauer			kill(getpid(), SIGINT);
75238521Scracauer		else
75338521Scracauer			CLEAR_PENDING_INT;
75438521Scracauer	}
7551556Srgrimes	INTON;
7561556Srgrimes	return st;
7571556Srgrimes}
7581556Srgrimes
7591556Srgrimes
7601556Srgrimes
7611556Srgrimes/*
7621556Srgrimes * Wait for a process to terminate.
7631556Srgrimes */
7641556Srgrimes
7651556SrgrimesSTATIC int
7661556Srgrimesdowait(block, job)
76717987Speter	int block;
7681556Srgrimes	struct job *job;
76917987Speter{
7701556Srgrimes	int pid;
7711556Srgrimes	int status;
7721556Srgrimes	struct procstat *sp;
7731556Srgrimes	struct job *jp;
7741556Srgrimes	struct job *thisjob;
7751556Srgrimes	int done;
7761556Srgrimes	int stopped;
7771556Srgrimes	int core;
77826104Ssteve	int sig;
7791556Srgrimes
78038950Scracauer	in_dowait++;
7811556Srgrimes	TRACE(("dowait(%d) called\n", block));
7821556Srgrimes	do {
7831556Srgrimes		pid = waitproc(block, &status);
7841556Srgrimes		TRACE(("wait returns %d, status=%d\n", pid, status));
78538521Scracauer	} while (pid == -1 && errno == EINTR && breakwaitcmd == 0);
78638950Scracauer	in_dowait--;
78738521Scracauer	if (breakwaitcmd != 0) {
78838521Scracauer		breakwaitcmd = 0;
78938521Scracauer		return -1;
79038521Scracauer	}
7911556Srgrimes	if (pid <= 0)
7921556Srgrimes		return pid;
7931556Srgrimes	INTOFF;
7941556Srgrimes	thisjob = NULL;
7951556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
7961556Srgrimes		if (jp->used) {
7971556Srgrimes			done = 1;
7981556Srgrimes			stopped = 1;
7991556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
8001556Srgrimes				if (sp->pid == -1)
8011556Srgrimes					continue;
8021556Srgrimes				if (sp->pid == pid) {
80326104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
80426104Ssteve						   pid, sp->status, status));
8051556Srgrimes					sp->status = status;
8061556Srgrimes					thisjob = jp;
8071556Srgrimes				}
8081556Srgrimes				if (sp->status == -1)
8091556Srgrimes					stopped = 0;
81026104Ssteve				else if (WIFSTOPPED(sp->status))
8111556Srgrimes					done = 0;
8121556Srgrimes			}
8131556Srgrimes			if (stopped) {		/* stopped or done */
8141556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
8151556Srgrimes				if (jp->state != state) {
8161556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
8171556Srgrimes					jp->state = state;
8181556Srgrimes#if JOBS
8191556Srgrimes					if (done && curjob == jp - jobtab + 1)
8201556Srgrimes						curjob = 0;		/* no current job */
8211556Srgrimes#endif
8221556Srgrimes				}
8231556Srgrimes			}
8241556Srgrimes		}
8251556Srgrimes	}
8261556Srgrimes	INTON;
8271556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
82826104Ssteve		core = WCOREDUMP(status);
8291556Srgrimes#if JOBS
83026104Ssteve		if (WIFSTOPPED(status))
83126104Ssteve			sig = WSTOPSIG(status);
83226104Ssteve		else
8331556Srgrimes#endif
83426104Ssteve			if (WIFEXITED(status))
83526104Ssteve				sig = 0;
83626104Ssteve			else
83726104Ssteve				sig = WTERMSIG(status);
83826104Ssteve
83926104Ssteve		if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
8401556Srgrimes			if (thisjob != job)
8411556Srgrimes				outfmt(out2, "%d: ", pid);
8421556Srgrimes#if JOBS
84326104Ssteve			if (sig == SIGTSTP && rootshell && iflag)
8441556Srgrimes				outfmt(out2, "%%%d ", job - jobtab + 1);
8451556Srgrimes#endif
84626104Ssteve			if (sig < NSIG && sys_siglist[sig])
84726104Ssteve				out2str(sys_siglist[sig]);
8481556Srgrimes			else
84926104Ssteve				outfmt(out2, "Signal %d", sig);
8501556Srgrimes			if (core)
8511556Srgrimes				out2str(" - core dumped");
8521556Srgrimes			out2c('\n');
8531556Srgrimes			flushout(&errout);
8541556Srgrimes		} else {
85526104Ssteve			TRACE(("Not printing status: status=%d, sig=%d\n",
85626104Ssteve				   status, sig));
8571556Srgrimes		}
8581556Srgrimes	} else {
8591556Srgrimes		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
8601556Srgrimes		if (thisjob)
8611556Srgrimes			thisjob->changed = 1;
8621556Srgrimes	}
8631556Srgrimes	return pid;
8641556Srgrimes}
8651556Srgrimes
8661556Srgrimes
8671556Srgrimes
8681556Srgrimes/*
8691556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
8701556Srgrimes * stopped processes.  If block is zero, we return a value of zero
8711556Srgrimes * rather than blocking.
8721556Srgrimes *
8731556Srgrimes * System V doesn't have a non-blocking wait system call.  It does
8741556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's
8751556Srgrimes * children dies.  The obvious way to use SIGCLD would be to install
8761556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD
8771556Srgrimes * was received, and have waitproc bump another counter when it got
8781556Srgrimes * the status of a process.  Waitproc would then know that a wait
8791556Srgrimes * system call would not block if the two counters were different.
8801556Srgrimes * This approach doesn't work because if a process has children that
8811556Srgrimes * have not been waited for, System V will send it a SIGCLD when it
8821556Srgrimes * installs a signal handler for SIGCLD.  What this means is that when
8831556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously
8841556Srgrimes * until is runs out of stack space, unless it does a wait call before
8851556Srgrimes * restoring the signal handler.  The code below takes advantage of
8861556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and
8871556Srgrimes * then checking to see whether it was called.  If there are any
8881556Srgrimes * children to be waited for, it will be.
8891556Srgrimes *
8901556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking
8911556Srgrimes * waits at all.  In this case, the user will not be informed when
8921556Srgrimes * a background process until the next time she runs a real program
8931556Srgrimes * (as opposed to running a builtin command or just typing return),
8941556Srgrimes * and the jobs command may give out of date information.
8951556Srgrimes */
8961556Srgrimes
8971556Srgrimes#ifdef SYSV
89838521ScracauerSTATIC sig_atomic_t gotsigchild;
8991556Srgrimes
9001556SrgrimesSTATIC int onsigchild() {
9011556Srgrimes	gotsigchild = 1;
9021556Srgrimes}
9031556Srgrimes#endif
9041556Srgrimes
9051556Srgrimes
9061556SrgrimesSTATIC int
9071556Srgrimeswaitproc(block, status)
90817987Speter	int block;
9091556Srgrimes	int *status;
91017987Speter{
9111556Srgrimes#ifdef BSD
9121556Srgrimes	int flags;
9131556Srgrimes
9141556Srgrimes#if JOBS
9151556Srgrimes	flags = WUNTRACED;
9161556Srgrimes#else
9171556Srgrimes	flags = 0;
9181556Srgrimes#endif
9191556Srgrimes	if (block == 0)
9201556Srgrimes		flags |= WNOHANG;
9211556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
9221556Srgrimes#else
9231556Srgrimes#ifdef SYSV
9241556Srgrimes	int (*save)();
9251556Srgrimes
9261556Srgrimes	if (block == 0) {
9271556Srgrimes		gotsigchild = 0;
9281556Srgrimes		save = signal(SIGCLD, onsigchild);
9291556Srgrimes		signal(SIGCLD, save);
9301556Srgrimes		if (gotsigchild == 0)
9311556Srgrimes			return 0;
9321556Srgrimes	}
9331556Srgrimes	return wait(status);
9341556Srgrimes#else
9351556Srgrimes	if (block == 0)
9361556Srgrimes		return 0;
9371556Srgrimes	return wait(status);
9381556Srgrimes#endif
9391556Srgrimes#endif
9401556Srgrimes}
9411556Srgrimes
9421556Srgrimes/*
9431556Srgrimes * return 1 if there are stopped jobs, otherwise 0
9441556Srgrimes */
9451556Srgrimesint job_warning = 0;
9461556Srgrimesint
9471556Srgrimesstoppedjobs()
9481556Srgrimes{
94925222Ssteve	int jobno;
95025222Ssteve	struct job *jp;
9511556Srgrimes
9521556Srgrimes	if (job_warning)
9531556Srgrimes		return (0);
9541556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
9551556Srgrimes		if (jp->used == 0)
9561556Srgrimes			continue;
9571556Srgrimes		if (jp->state == JOBSTOPPED) {
9581556Srgrimes			out2str("You have stopped jobs.\n");
9591556Srgrimes			job_warning = 2;
9601556Srgrimes			return (1);
9611556Srgrimes		}
9621556Srgrimes	}
9631556Srgrimes
9641556Srgrimes	return (0);
9651556Srgrimes}
9661556Srgrimes
9671556Srgrimes/*
9681556Srgrimes * Return a string identifying a command (to be printed by the
9691556Srgrimes * jobs command.
9701556Srgrimes */
9711556Srgrimes
9721556SrgrimesSTATIC char *cmdnextc;
9731556SrgrimesSTATIC int cmdnleft;
9741556SrgrimesSTATIC void cmdtxt(), cmdputs();
9751556Srgrimes#define MAXCMDTEXT	200
9761556Srgrimes
9771556Srgrimeschar *
9781556Srgrimescommandtext(n)
9791556Srgrimes	union node *n;
9801556Srgrimes	{
9811556Srgrimes	char *name;
9821556Srgrimes
9831556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
9841556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
9851556Srgrimes	cmdtxt(n);
9861556Srgrimes	*cmdnextc = '\0';
9871556Srgrimes	return name;
9881556Srgrimes}
9891556Srgrimes
9901556Srgrimes
9911556SrgrimesSTATIC void
9921556Srgrimescmdtxt(n)
9931556Srgrimes	union node *n;
9941556Srgrimes	{
9951556Srgrimes	union node *np;
9961556Srgrimes	struct nodelist *lp;
9971556Srgrimes	char *p;
9981556Srgrimes	int i;
9991556Srgrimes	char s[2];
10001556Srgrimes
10011556Srgrimes	if (n == NULL)
10021556Srgrimes		return;
10031556Srgrimes	switch (n->type) {
10041556Srgrimes	case NSEMI:
10051556Srgrimes		cmdtxt(n->nbinary.ch1);
10061556Srgrimes		cmdputs("; ");
10071556Srgrimes		cmdtxt(n->nbinary.ch2);
10081556Srgrimes		break;
10091556Srgrimes	case NAND:
10101556Srgrimes		cmdtxt(n->nbinary.ch1);
10111556Srgrimes		cmdputs(" && ");
10121556Srgrimes		cmdtxt(n->nbinary.ch2);
10131556Srgrimes		break;
10141556Srgrimes	case NOR:
10151556Srgrimes		cmdtxt(n->nbinary.ch1);
10161556Srgrimes		cmdputs(" || ");
10171556Srgrimes		cmdtxt(n->nbinary.ch2);
10181556Srgrimes		break;
10191556Srgrimes	case NPIPE:
10201556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
10211556Srgrimes			cmdtxt(lp->n);
10221556Srgrimes			if (lp->next)
10231556Srgrimes				cmdputs(" | ");
10241556Srgrimes		}
10251556Srgrimes		break;
10261556Srgrimes	case NSUBSHELL:
10271556Srgrimes		cmdputs("(");
10281556Srgrimes		cmdtxt(n->nredir.n);
10291556Srgrimes		cmdputs(")");
10301556Srgrimes		break;
10311556Srgrimes	case NREDIR:
10321556Srgrimes	case NBACKGND:
10331556Srgrimes		cmdtxt(n->nredir.n);
10341556Srgrimes		break;
10351556Srgrimes	case NIF:
10361556Srgrimes		cmdputs("if ");
10371556Srgrimes		cmdtxt(n->nif.test);
10381556Srgrimes		cmdputs("; then ");
10391556Srgrimes		cmdtxt(n->nif.ifpart);
10401556Srgrimes		cmdputs("...");
10411556Srgrimes		break;
10421556Srgrimes	case NWHILE:
10431556Srgrimes		cmdputs("while ");
10441556Srgrimes		goto until;
10451556Srgrimes	case NUNTIL:
10461556Srgrimes		cmdputs("until ");
10471556Srgrimesuntil:
10481556Srgrimes		cmdtxt(n->nbinary.ch1);
10491556Srgrimes		cmdputs("; do ");
10501556Srgrimes		cmdtxt(n->nbinary.ch2);
10511556Srgrimes		cmdputs("; done");
10521556Srgrimes		break;
10531556Srgrimes	case NFOR:
10541556Srgrimes		cmdputs("for ");
10551556Srgrimes		cmdputs(n->nfor.var);
10561556Srgrimes		cmdputs(" in ...");
10571556Srgrimes		break;
10581556Srgrimes	case NCASE:
10591556Srgrimes		cmdputs("case ");
10601556Srgrimes		cmdputs(n->ncase.expr->narg.text);
10611556Srgrimes		cmdputs(" in ...");
10621556Srgrimes		break;
10631556Srgrimes	case NDEFUN:
10641556Srgrimes		cmdputs(n->narg.text);
10651556Srgrimes		cmdputs("() ...");
10661556Srgrimes		break;
10671556Srgrimes	case NCMD:
10681556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
10691556Srgrimes			cmdtxt(np);
10701556Srgrimes			if (np->narg.next)
10711556Srgrimes				cmdputs(" ");
10721556Srgrimes		}
10731556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
10741556Srgrimes			cmdputs(" ");
10751556Srgrimes			cmdtxt(np);
10761556Srgrimes		}
10771556Srgrimes		break;
10781556Srgrimes	case NARG:
10791556Srgrimes		cmdputs(n->narg.text);
10801556Srgrimes		break;
10811556Srgrimes	case NTO:
10821556Srgrimes		p = ">";  i = 1;  goto redir;
10831556Srgrimes	case NAPPEND:
10841556Srgrimes		p = ">>";  i = 1;  goto redir;
10851556Srgrimes	case NTOFD:
10861556Srgrimes		p = ">&";  i = 1;  goto redir;
10871556Srgrimes	case NFROM:
10881556Srgrimes		p = "<";  i = 0;  goto redir;
10891556Srgrimes	case NFROMFD:
10901556Srgrimes		p = "<&";  i = 0;  goto redir;
10911556Srgrimesredir:
10921556Srgrimes		if (n->nfile.fd != i) {
10931556Srgrimes			s[0] = n->nfile.fd + '0';
10941556Srgrimes			s[1] = '\0';
10951556Srgrimes			cmdputs(s);
10961556Srgrimes		}
10971556Srgrimes		cmdputs(p);
10981556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
10991556Srgrimes			s[0] = n->ndup.dupfd + '0';
11001556Srgrimes			s[1] = '\0';
11011556Srgrimes			cmdputs(s);
11021556Srgrimes		} else {
11031556Srgrimes			cmdtxt(n->nfile.fname);
11041556Srgrimes		}
11051556Srgrimes		break;
11061556Srgrimes	case NHERE:
11071556Srgrimes	case NXHERE:
11081556Srgrimes		cmdputs("<<...");
11091556Srgrimes		break;
11101556Srgrimes	default:
11111556Srgrimes		cmdputs("???");
11121556Srgrimes		break;
11131556Srgrimes	}
11141556Srgrimes}
11151556Srgrimes
11161556Srgrimes
11171556Srgrimes
11181556SrgrimesSTATIC void
11191556Srgrimescmdputs(s)
11201556Srgrimes	char *s;
11211556Srgrimes	{
112225222Ssteve	char *p, *q;
112325222Ssteve	char c;
11241556Srgrimes	int subtype = 0;
11251556Srgrimes
11261556Srgrimes	if (cmdnleft <= 0)
11271556Srgrimes		return;
11281556Srgrimes	p = s;
11291556Srgrimes	q = cmdnextc;
11301556Srgrimes	while ((c = *p++) != '\0') {
11311556Srgrimes		if (c == CTLESC)
11321556Srgrimes			*q++ = *p++;
11331556Srgrimes		else if (c == CTLVAR) {
11341556Srgrimes			*q++ = '$';
11351556Srgrimes			if (--cmdnleft > 0)
11361556Srgrimes				*q++ = '{';
11371556Srgrimes			subtype = *p++;
11381556Srgrimes		} else if (c == '=' && subtype != 0) {
11391556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
11401556Srgrimes			subtype = 0;
11411556Srgrimes		} else if (c == CTLENDVAR) {
11421556Srgrimes			*q++ = '}';
114318954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
11441556Srgrimes			cmdnleft++;		/* ignore it */
11451556Srgrimes		else
11461556Srgrimes			*q++ = c;
11471556Srgrimes		if (--cmdnleft <= 0) {
11481556Srgrimes			*q++ = '.';
11491556Srgrimes			*q++ = '.';
11501556Srgrimes			*q++ = '.';
11511556Srgrimes			break;
11521556Srgrimes		}
11531556Srgrimes	}
11541556Srgrimes	cmdnextc = q;
11551556Srgrimes}
1156