jobs.c revision 90111
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 90111 2002-02-02 06:50:57Z imp $";
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");
2061556Srgrimes	pgrp = jp->ps[0].pid;
20720425Ssteve#ifdef OLD_TTY_DRIVER
2081556Srgrimes	ioctl(2, TIOCSPGRP, (char *)&pgrp);
20920425Ssteve#else
21020425Ssteve	tcsetpgrp(2, pgrp);
21120425Ssteve#endif
2121556Srgrimes	restartjob(jp);
2131556Srgrimes	INTOFF;
21445916Scracauer	status = waitforjob(jp, (int *)NULL);
2151556Srgrimes	INTON;
2161556Srgrimes	return status;
2171556Srgrimes}
2181556Srgrimes
2191556Srgrimes
22017987Speterint
22190111Simpbgcmd(int argc, char **argv)
22217987Speter{
2231556Srgrimes	struct job *jp;
2241556Srgrimes
2251556Srgrimes	do {
2261556Srgrimes		jp = getjob(*++argv);
2271556Srgrimes		if (jp->jobctl == 0)
2281556Srgrimes			error("job not created under job control");
2291556Srgrimes		restartjob(jp);
2301556Srgrimes	} while (--argc > 1);
2311556Srgrimes	return 0;
2321556Srgrimes}
2331556Srgrimes
2341556Srgrimes
2351556SrgrimesSTATIC void
23690111Simprestartjob(struct job *jp)
23717987Speter{
2381556Srgrimes	struct procstat *ps;
2391556Srgrimes	int i;
2401556Srgrimes
2411556Srgrimes	if (jp->state == JOBDONE)
2421556Srgrimes		return;
2431556Srgrimes	INTOFF;
2441556Srgrimes	killpg(jp->ps[0].pid, SIGCONT);
2451556Srgrimes	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
24626104Ssteve		if (WIFSTOPPED(ps->status)) {
2471556Srgrimes			ps->status = -1;
2481556Srgrimes			jp->state = 0;
2491556Srgrimes		}
2501556Srgrimes	}
2511556Srgrimes	INTON;
2521556Srgrimes}
2531556Srgrimes#endif
2541556Srgrimes
2551556Srgrimes
2561556Srgrimesint
25790111Simpjobscmd(int argc __unused, char **argv __unused)
25817987Speter{
2591556Srgrimes	showjobs(0);
2601556Srgrimes	return 0;
2611556Srgrimes}
2621556Srgrimes
2631556Srgrimes
2641556Srgrimes/*
2651556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
2661556Srgrimes * statuses have changed since the last call to showjobs.
2671556Srgrimes *
2681556Srgrimes * If the shell is interrupted in the process of creating a job, the
2691556Srgrimes * result may be a job structure containing zero processes.  Such structures
2701556Srgrimes * will be freed here.
2711556Srgrimes */
2721556Srgrimes
2731556Srgrimesvoid
27490111Simpshowjobs(int change)
27517987Speter{
2761556Srgrimes	int jobno;
2771556Srgrimes	int procno;
2781556Srgrimes	int i;
2791556Srgrimes	struct job *jp;
2801556Srgrimes	struct procstat *ps;
2811556Srgrimes	int col;
2821556Srgrimes	char s[64];
2831556Srgrimes
2841556Srgrimes	TRACE(("showjobs(%d) called\n", change));
2851556Srgrimes	while (dowait(0, (struct job *)NULL) > 0);
2861556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
2871556Srgrimes		if (! jp->used)
2881556Srgrimes			continue;
2891556Srgrimes		if (jp->nprocs == 0) {
2901556Srgrimes			freejob(jp);
2911556Srgrimes			continue;
2921556Srgrimes		}
2931556Srgrimes		if (change && ! jp->changed)
2941556Srgrimes			continue;
2951556Srgrimes		procno = jp->nprocs;
2961556Srgrimes		for (ps = jp->ps ; ; ps++) {	/* for each process */
2971556Srgrimes			if (ps == jp->ps)
2981556Srgrimes				fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
2991556Srgrimes			else
3001556Srgrimes				fmtstr(s, 64, "    %d ", ps->pid);
3011556Srgrimes			out1str(s);
3021556Srgrimes			col = strlen(s);
3031556Srgrimes			s[0] = '\0';
3041556Srgrimes			if (ps->status == -1) {
3051556Srgrimes				/* don't print anything */
30626104Ssteve			} else if (WIFEXITED(ps->status)) {
30726104Ssteve				fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status));
3081556Srgrimes			} else {
3091556Srgrimes#if JOBS
31026104Ssteve				if (WIFSTOPPED(ps->status))
31126104Ssteve					i = WSTOPSIG(ps->status);
31226104Ssteve				else
3131556Srgrimes#endif
31426104Ssteve					i = WTERMSIG(ps->status);
31517987Speter				if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
31617987Speter					scopy(sys_siglist[i & 0x7F], s);
3171556Srgrimes				else
3181556Srgrimes					fmtstr(s, 64, "Signal %d", i & 0x7F);
31926104Ssteve				if (WCOREDUMP(ps->status))
3201556Srgrimes					strcat(s, " (core dumped)");
3211556Srgrimes			}
3221556Srgrimes			out1str(s);
3231556Srgrimes			col += strlen(s);
3241556Srgrimes			do {
3251556Srgrimes				out1c(' ');
3261556Srgrimes				col++;
3271556Srgrimes			} while (col < 30);
3281556Srgrimes			out1str(ps->cmd);
3291556Srgrimes			out1c('\n');
3301556Srgrimes			if (--procno <= 0)
3311556Srgrimes				break;
3321556Srgrimes		}
3331556Srgrimes		jp->changed = 0;
3341556Srgrimes		if (jp->state == JOBDONE) {
3351556Srgrimes			freejob(jp);
3361556Srgrimes		}
3371556Srgrimes	}
3381556Srgrimes}
3391556Srgrimes
3401556Srgrimes
3411556Srgrimes/*
3421556Srgrimes * Mark a job structure as unused.
3431556Srgrimes */
3441556Srgrimes
3451556SrgrimesSTATIC void
34690111Simpfreejob(struct job *jp)
34790111Simp{
3481556Srgrimes	struct procstat *ps;
3491556Srgrimes	int i;
3501556Srgrimes
3511556Srgrimes	INTOFF;
3521556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
3531556Srgrimes		if (ps->cmd != nullstr)
3541556Srgrimes			ckfree(ps->cmd);
3551556Srgrimes	}
3561556Srgrimes	if (jp->ps != &jp->ps0)
3571556Srgrimes		ckfree(jp->ps);
3581556Srgrimes	jp->used = 0;
3591556Srgrimes#if JOBS
3601556Srgrimes	if (curjob == jp - jobtab + 1)
3611556Srgrimes		curjob = 0;
3621556Srgrimes#endif
3631556Srgrimes	INTON;
3641556Srgrimes}
3651556Srgrimes
3661556Srgrimes
3671556Srgrimes
3681556Srgrimesint
36990111Simpwaitcmd(int argc, char **argv)
37017987Speter{
3711556Srgrimes	struct job *job;
37226104Ssteve	int status, retval;
3731556Srgrimes	struct job *jp;
3741556Srgrimes
3751556Srgrimes	if (argc > 1) {
3761556Srgrimes		job = getjob(argv[1]);
3771556Srgrimes	} else {
3781556Srgrimes		job = NULL;
3791556Srgrimes	}
38038536Scracauer
38138536Scracauer	/*
38238536Scracauer	 * Loop until a process is terminated or stopped, or a SIGINT is
38338536Scracauer	 * received.
38438536Scracauer	 */
38538536Scracauer
38638521Scracauer	in_waitcmd++;
38738536Scracauer	do {
3881556Srgrimes		if (job != NULL) {
3891556Srgrimes			if (job->state) {
3901556Srgrimes				status = job->ps[job->nprocs - 1].status;
39126104Ssteve				if (WIFEXITED(status))
39226104Ssteve					retval = WEXITSTATUS(status);
3931556Srgrimes#if JOBS
39426104Ssteve				else if (WIFSTOPPED(status))
39526104Ssteve					retval = WSTOPSIG(status) + 128;
3961556Srgrimes#endif
3971556Srgrimes				else
39826104Ssteve					retval = WTERMSIG(status) + 128;
3991556Srgrimes				if (! iflag)
4001556Srgrimes					freejob(job);
40138536Scracauer				in_waitcmd--;
40226104Ssteve				return retval;
4031556Srgrimes			}
4041556Srgrimes		} else {
4051556Srgrimes			for (jp = jobtab ; ; jp++) {
4061556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
40738536Scracauer					in_waitcmd--;
4081556Srgrimes					return 0;
4091556Srgrimes				}
4101556Srgrimes				if (jp->used && jp->state == 0)
4111556Srgrimes					break;
4121556Srgrimes			}
4131556Srgrimes		}
41438521Scracauer	} while (dowait(1, (struct job *)NULL) != -1);
41538521Scracauer	in_waitcmd--;
41638521Scracauer
41738521Scracauer	return 0;
4181556Srgrimes}
4191556Srgrimes
4201556Srgrimes
4211556Srgrimes
42217987Speterint
42390111Simpjobidcmd(int argc __unused, char **argv)
42417987Speter{
4251556Srgrimes	struct job *jp;
4261556Srgrimes	int i;
4271556Srgrimes
4281556Srgrimes	jp = getjob(argv[1]);
4291556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
4301556Srgrimes		out1fmt("%d", jp->ps[i].pid);
4311556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
4321556Srgrimes	}
4331556Srgrimes	return 0;
4341556Srgrimes}
4351556Srgrimes
4361556Srgrimes
4371556Srgrimes
4381556Srgrimes/*
4391556Srgrimes * Convert a job name to a job structure.
4401556Srgrimes */
4411556Srgrimes
4421556SrgrimesSTATIC struct job *
44390111Simpgetjob(char *name)
44490111Simp{
4451556Srgrimes	int jobno;
44625222Ssteve	struct job *jp;
4471556Srgrimes	int pid;
4481556Srgrimes	int i;
4491556Srgrimes
4501556Srgrimes	if (name == NULL) {
4511556Srgrimes#if JOBS
4521556Srgrimescurrentjob:
4531556Srgrimes		if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0)
4541556Srgrimes			error("No current job");
4551556Srgrimes		return &jobtab[jobno - 1];
4561556Srgrimes#else
4571556Srgrimes		error("No current job");
4581556Srgrimes#endif
4591556Srgrimes	} else if (name[0] == '%') {
4601556Srgrimes		if (is_digit(name[1])) {
4611556Srgrimes			jobno = number(name + 1);
4621556Srgrimes			if (jobno > 0 && jobno <= njobs
4631556Srgrimes			 && jobtab[jobno - 1].used != 0)
4641556Srgrimes				return &jobtab[jobno - 1];
4651556Srgrimes#if JOBS
4661556Srgrimes		} else if (name[1] == '%' && name[2] == '\0') {
4671556Srgrimes			goto currentjob;
4681556Srgrimes#endif
4691556Srgrimes		} else {
47025222Ssteve			struct job *found = NULL;
4711556Srgrimes			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
4721556Srgrimes				if (jp->used && jp->nprocs > 0
4731556Srgrimes				 && prefix(name + 1, jp->ps[0].cmd)) {
4741556Srgrimes					if (found)
4751556Srgrimes						error("%s: ambiguous", name);
4761556Srgrimes					found = jp;
4771556Srgrimes				}
4781556Srgrimes			}
4791556Srgrimes			if (found)
4801556Srgrimes				return found;
4811556Srgrimes		}
4821556Srgrimes	} else if (is_number(name)) {
4831556Srgrimes		pid = number(name);
4841556Srgrimes		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
4851556Srgrimes			if (jp->used && jp->nprocs > 0
4861556Srgrimes			 && jp->ps[jp->nprocs - 1].pid == pid)
4871556Srgrimes				return jp;
4881556Srgrimes		}
4891556Srgrimes	}
4901556Srgrimes	error("No such job: %s", name);
49117987Speter	/*NOTREACHED*/
49217987Speter	return NULL;
4931556Srgrimes}
4941556Srgrimes
4951556Srgrimes
4961556Srgrimes
4971556Srgrimes/*
4981556Srgrimes * Return a new job structure,
4991556Srgrimes */
5001556Srgrimes
5011556Srgrimesstruct job *
50290111Simpmakejob(union node *node __unused, int nprocs)
50317987Speter{
5041556Srgrimes	int i;
5051556Srgrimes	struct job *jp;
5061556Srgrimes
5071556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
5081556Srgrimes		if (--i < 0) {
5091556Srgrimes			INTOFF;
5101556Srgrimes			if (njobs == 0) {
5111556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
5121556Srgrimes			} else {
5131556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
51417987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
51520425Ssteve				/* Relocate `ps' pointers */
51620425Ssteve				for (i = 0; i < njobs; i++)
51720425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
51820425Ssteve						jp[i].ps = &jp[i].ps0;
5191556Srgrimes				ckfree(jobtab);
5201556Srgrimes				jobtab = jp;
5211556Srgrimes			}
5221556Srgrimes			jp = jobtab + njobs;
5231556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
5241556Srgrimes			INTON;
5251556Srgrimes			break;
5261556Srgrimes		}
5271556Srgrimes		if (jp->used == 0)
5281556Srgrimes			break;
5291556Srgrimes	}
5301556Srgrimes	INTOFF;
5311556Srgrimes	jp->state = 0;
5321556Srgrimes	jp->used = 1;
5331556Srgrimes	jp->changed = 0;
5341556Srgrimes	jp->nprocs = 0;
5351556Srgrimes#if JOBS
5361556Srgrimes	jp->jobctl = jobctl;
5371556Srgrimes#endif
5381556Srgrimes	if (nprocs > 1) {
5391556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
5401556Srgrimes	} else {
5411556Srgrimes		jp->ps = &jp->ps0;
5421556Srgrimes	}
5431556Srgrimes	INTON;
54417987Speter	TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
54517987Speter	    jp - jobtab + 1));
5461556Srgrimes	return jp;
5478855Srgrimes}
5481556Srgrimes
5491556Srgrimes
5501556Srgrimes/*
5511556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
5521556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
5531556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
5541556Srgrimes * be NULL.  The mode parameter can be one of the following:
5551556Srgrimes *	FORK_FG - Fork off a foreground process.
5561556Srgrimes *	FORK_BG - Fork off a background process.
5571556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
5581556Srgrimes *		     process group even if job control is on.
5591556Srgrimes *
5601556Srgrimes * When job control is turned off, background processes have their standard
5611556Srgrimes * input redirected to /dev/null (except for the second and later processes
5621556Srgrimes * in a pipeline).
5631556Srgrimes */
5641556Srgrimes
5651556Srgrimesint
56690111Simpforkshell(struct job *jp, union node *n, int mode)
56717987Speter{
5681556Srgrimes	int pid;
5691556Srgrimes	int pgrp;
5701556Srgrimes
57117987Speter	TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
57217987Speter	    mode));
5731556Srgrimes	INTOFF;
5741556Srgrimes	pid = fork();
5751556Srgrimes	if (pid == -1) {
5761556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
5771556Srgrimes		INTON;
57853891Scracauer		error("Cannot fork: %s", strerror(errno));
5791556Srgrimes	}
5801556Srgrimes	if (pid == 0) {
5811556Srgrimes		struct job *p;
5821556Srgrimes		int wasroot;
5831556Srgrimes		int i;
5841556Srgrimes
5851556Srgrimes		TRACE(("Child shell %d\n", getpid()));
5861556Srgrimes		wasroot = rootshell;
5871556Srgrimes		rootshell = 0;
5881556Srgrimes		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
5891556Srgrimes			if (p->used)
5901556Srgrimes				freejob(p);
5911556Srgrimes		closescript();
5921556Srgrimes		INTON;
5931556Srgrimes		clear_traps();
5941556Srgrimes#if JOBS
5951556Srgrimes		jobctl = 0;		/* do job control only in root shell */
5961556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
5971556Srgrimes			if (jp == NULL || jp->nprocs == 0)
5981556Srgrimes				pgrp = getpid();
5991556Srgrimes			else
6001556Srgrimes				pgrp = jp->ps[0].pid;
60121352Ssteve			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
6021556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
60320425Ssteve#ifdef OLD_TTY_DRIVER
6041556Srgrimes				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
60518016Speter					error("TIOCSPGRP failed, errno=%d", errno);
60620425Ssteve#else
60720425Ssteve				if (tcsetpgrp(2, pgrp) < 0)
60820425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
60920425Ssteve#endif
6101556Srgrimes			}
6111556Srgrimes			setsignal(SIGTSTP);
6121556Srgrimes			setsignal(SIGTTOU);
6131556Srgrimes		} else if (mode == FORK_BG) {
6141556Srgrimes			ignoresig(SIGINT);
6151556Srgrimes			ignoresig(SIGQUIT);
6161556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
6171556Srgrimes			    ! fd0_redirected_p ()) {
6181556Srgrimes				close(0);
61969793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
62069793Sobrien					error("Can't open %s: %s",
62169793Sobrien					    _PATH_DEVNULL, strerror(errno));
6221556Srgrimes			}
6231556Srgrimes		}
6241556Srgrimes#else
6251556Srgrimes		if (mode == FORK_BG) {
6261556Srgrimes			ignoresig(SIGINT);
6271556Srgrimes			ignoresig(SIGQUIT);
6281556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
6291556Srgrimes			    ! fd0_redirected_p ()) {
6301556Srgrimes				close(0);
63169793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
63269793Sobrien					error("Can't open %s: %s",
63369793Sobrien					    _PATH_DEVNULL, strerror(errno));
6341556Srgrimes			}
6351556Srgrimes		}
6361556Srgrimes#endif
6371556Srgrimes		if (wasroot && iflag) {
6381556Srgrimes			setsignal(SIGINT);
6391556Srgrimes			setsignal(SIGQUIT);
6401556Srgrimes			setsignal(SIGTERM);
6411556Srgrimes		}
6421556Srgrimes		return pid;
6431556Srgrimes	}
6441556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
6451556Srgrimes		if (jp == NULL || jp->nprocs == 0)
6461556Srgrimes			pgrp = pid;
6471556Srgrimes		else
6481556Srgrimes			pgrp = jp->ps[0].pid;
64917987Speter		setpgid(pid, pgrp);
6501556Srgrimes	}
6511556Srgrimes	if (mode == FORK_BG)
6521556Srgrimes		backgndpid = pid;		/* set $! */
6531556Srgrimes	if (jp) {
6541556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
6551556Srgrimes		ps->pid = pid;
6561556Srgrimes		ps->status = -1;
6571556Srgrimes		ps->cmd = nullstr;
6581556Srgrimes		if (iflag && rootshell && n)
6591556Srgrimes			ps->cmd = commandtext(n);
6601556Srgrimes	}
6611556Srgrimes	INTON;
6621556Srgrimes	TRACE(("In parent shell:  child = %d\n", pid));
6631556Srgrimes	return pid;
6641556Srgrimes}
6651556Srgrimes
6661556Srgrimes
6671556Srgrimes
6681556Srgrimes/*
6691556Srgrimes * Wait for job to finish.
6701556Srgrimes *
6711556Srgrimes * Under job control we have the problem that while a child process is
6721556Srgrimes * running interrupts generated by the user are sent to the child but not
6731556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
6741556Srgrimes * active user may be hard to kill.  With job control turned off, an
6751556Srgrimes * interactive user may place an interactive program inside a loop.  If
6761556Srgrimes * the interactive program catches interrupts, the user doesn't want
6771556Srgrimes * these interrupts to also abort the loop.  The approach we take here
6781556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
67946684Skris * foreground process to terminate, and then send itself an interrupt
6801556Srgrimes * signal if the child process was terminated by an interrupt signal.
6811556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
6821556Srgrimes * exit on interrupt; unless these processes terminate themselves by
6831556Srgrimes * sending a signal to themselves (instead of calling exit) they will
6841556Srgrimes * confuse this approach.
6851556Srgrimes */
6861556Srgrimes
6871556Srgrimesint
68890111Simpwaitforjob(struct job *jp, int *origstatus)
68945916Scracauer{
6901556Srgrimes#if JOBS
69117987Speter	int mypgrp = getpgrp();
6921556Srgrimes#endif
6931556Srgrimes	int status;
6941556Srgrimes	int st;
6951556Srgrimes
6961556Srgrimes	INTOFF;
6971556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
69838950Scracauer	while (jp->state == 0)
69938950Scracauer		if (dowait(1, jp) == -1)
70038950Scracauer			dotrap();
7011556Srgrimes#if JOBS
7021556Srgrimes	if (jp->jobctl) {
70320425Ssteve#ifdef OLD_TTY_DRIVER
7041556Srgrimes		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
70520425Ssteve			error("TIOCSPGRP failed, errno=%d\n", errno);
70620425Ssteve#else
70720425Ssteve		if (tcsetpgrp(2, mypgrp) < 0)
70820425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
70920425Ssteve#endif
7101556Srgrimes	}
7111556Srgrimes	if (jp->state == JOBSTOPPED)
7121556Srgrimes		curjob = jp - jobtab + 1;
7131556Srgrimes#endif
7141556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
71545916Scracauer	if (origstatus != NULL)
71645916Scracauer		*origstatus = status;
7171556Srgrimes	/* convert to 8 bits */
71826104Ssteve	if (WIFEXITED(status))
71926104Ssteve		st = WEXITSTATUS(status);
7201556Srgrimes#if JOBS
72126104Ssteve	else if (WIFSTOPPED(status))
72226104Ssteve		st = WSTOPSIG(status) + 128;
7231556Srgrimes#endif
7241556Srgrimes	else
72526104Ssteve		st = WTERMSIG(status) + 128;
7261556Srgrimes	if (! JOBS || jp->state == JOBDONE)
7271556Srgrimes		freejob(jp);
72838521Scracauer	if (int_pending()) {
72938521Scracauer		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
73038521Scracauer			kill(getpid(), SIGINT);
73138521Scracauer		else
73238521Scracauer			CLEAR_PENDING_INT;
73338521Scracauer	}
7341556Srgrimes	INTON;
7351556Srgrimes	return st;
7361556Srgrimes}
7371556Srgrimes
7381556Srgrimes
7391556Srgrimes
7401556Srgrimes/*
7411556Srgrimes * Wait for a process to terminate.
7421556Srgrimes */
7431556Srgrimes
7441556SrgrimesSTATIC int
74590111Simpdowait(int block, struct job *job)
74617987Speter{
7471556Srgrimes	int pid;
7481556Srgrimes	int status;
7491556Srgrimes	struct procstat *sp;
7501556Srgrimes	struct job *jp;
7511556Srgrimes	struct job *thisjob;
7521556Srgrimes	int done;
7531556Srgrimes	int stopped;
7541556Srgrimes	int core;
75526104Ssteve	int sig;
7561556Srgrimes
75738950Scracauer	in_dowait++;
7581556Srgrimes	TRACE(("dowait(%d) called\n", block));
7591556Srgrimes	do {
7601556Srgrimes		pid = waitproc(block, &status);
7611556Srgrimes		TRACE(("wait returns %d, status=%d\n", pid, status));
76272086Scracauer	} while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
76372086Scracauer	    (WIFSTOPPED(status) && !iflag));
76438950Scracauer	in_dowait--;
76538521Scracauer	if (breakwaitcmd != 0) {
76638521Scracauer		breakwaitcmd = 0;
76738521Scracauer		return -1;
76838521Scracauer	}
7691556Srgrimes	if (pid <= 0)
7701556Srgrimes		return pid;
7711556Srgrimes	INTOFF;
7721556Srgrimes	thisjob = NULL;
7731556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
7741556Srgrimes		if (jp->used) {
7751556Srgrimes			done = 1;
7761556Srgrimes			stopped = 1;
7771556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
7781556Srgrimes				if (sp->pid == -1)
7791556Srgrimes					continue;
7801556Srgrimes				if (sp->pid == pid) {
78126104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
78226104Ssteve						   pid, sp->status, status));
7831556Srgrimes					sp->status = status;
7841556Srgrimes					thisjob = jp;
7851556Srgrimes				}
7861556Srgrimes				if (sp->status == -1)
7871556Srgrimes					stopped = 0;
78826104Ssteve				else if (WIFSTOPPED(sp->status))
7891556Srgrimes					done = 0;
7901556Srgrimes			}
7911556Srgrimes			if (stopped) {		/* stopped or done */
7921556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
7931556Srgrimes				if (jp->state != state) {
7941556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
7951556Srgrimes					jp->state = state;
7961556Srgrimes#if JOBS
7971556Srgrimes					if (done && curjob == jp - jobtab + 1)
7981556Srgrimes						curjob = 0;		/* no current job */
7991556Srgrimes#endif
8001556Srgrimes				}
8011556Srgrimes			}
8021556Srgrimes		}
8031556Srgrimes	}
8041556Srgrimes	INTON;
8051556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
80626104Ssteve		core = WCOREDUMP(status);
8071556Srgrimes#if JOBS
80826104Ssteve		if (WIFSTOPPED(status))
80926104Ssteve			sig = WSTOPSIG(status);
81026104Ssteve		else
8111556Srgrimes#endif
81226104Ssteve			if (WIFEXITED(status))
81326104Ssteve				sig = 0;
81426104Ssteve			else
81526104Ssteve				sig = WTERMSIG(status);
81626104Ssteve
81726104Ssteve		if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
8181556Srgrimes			if (thisjob != job)
8191556Srgrimes				outfmt(out2, "%d: ", pid);
8201556Srgrimes#if JOBS
82126104Ssteve			if (sig == SIGTSTP && rootshell && iflag)
8221556Srgrimes				outfmt(out2, "%%%d ", job - jobtab + 1);
8231556Srgrimes#endif
82426104Ssteve			if (sig < NSIG && sys_siglist[sig])
82526104Ssteve				out2str(sys_siglist[sig]);
8261556Srgrimes			else
82726104Ssteve				outfmt(out2, "Signal %d", sig);
8281556Srgrimes			if (core)
8291556Srgrimes				out2str(" - core dumped");
8301556Srgrimes			out2c('\n');
8311556Srgrimes			flushout(&errout);
8321556Srgrimes		} else {
83326104Ssteve			TRACE(("Not printing status: status=%d, sig=%d\n",
83426104Ssteve				   status, sig));
8351556Srgrimes		}
8361556Srgrimes	} else {
8371556Srgrimes		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
8381556Srgrimes		if (thisjob)
8391556Srgrimes			thisjob->changed = 1;
8401556Srgrimes	}
8411556Srgrimes	return pid;
8421556Srgrimes}
8431556Srgrimes
8441556Srgrimes
8451556Srgrimes
8461556Srgrimes/*
8471556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
8481556Srgrimes * stopped processes.  If block is zero, we return a value of zero
8491556Srgrimes * rather than blocking.
8501556Srgrimes *
8511556Srgrimes * System V doesn't have a non-blocking wait system call.  It does
8521556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's
8531556Srgrimes * children dies.  The obvious way to use SIGCLD would be to install
8541556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD
8551556Srgrimes * was received, and have waitproc bump another counter when it got
8561556Srgrimes * the status of a process.  Waitproc would then know that a wait
8571556Srgrimes * system call would not block if the two counters were different.
8581556Srgrimes * This approach doesn't work because if a process has children that
8591556Srgrimes * have not been waited for, System V will send it a SIGCLD when it
8601556Srgrimes * installs a signal handler for SIGCLD.  What this means is that when
8611556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously
8621556Srgrimes * until is runs out of stack space, unless it does a wait call before
8631556Srgrimes * restoring the signal handler.  The code below takes advantage of
8641556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and
8651556Srgrimes * then checking to see whether it was called.  If there are any
8661556Srgrimes * children to be waited for, it will be.
8671556Srgrimes *
8681556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking
8691556Srgrimes * waits at all.  In this case, the user will not be informed when
8701556Srgrimes * a background process until the next time she runs a real program
8711556Srgrimes * (as opposed to running a builtin command or just typing return),
8721556Srgrimes * and the jobs command may give out of date information.
8731556Srgrimes */
8741556Srgrimes
8751556Srgrimes#ifdef SYSV
87638521ScracauerSTATIC sig_atomic_t gotsigchild;
8771556Srgrimes
8781556SrgrimesSTATIC int onsigchild() {
8791556Srgrimes	gotsigchild = 1;
8801556Srgrimes}
8811556Srgrimes#endif
8821556Srgrimes
8831556Srgrimes
8841556SrgrimesSTATIC int
88590111Simpwaitproc(int block, int *status)
88617987Speter{
8871556Srgrimes#ifdef BSD
8881556Srgrimes	int flags;
8891556Srgrimes
8901556Srgrimes#if JOBS
8911556Srgrimes	flags = WUNTRACED;
8921556Srgrimes#else
8931556Srgrimes	flags = 0;
8941556Srgrimes#endif
8951556Srgrimes	if (block == 0)
8961556Srgrimes		flags |= WNOHANG;
8971556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
8981556Srgrimes#else
8991556Srgrimes#ifdef SYSV
9001556Srgrimes	int (*save)();
9011556Srgrimes
9021556Srgrimes	if (block == 0) {
9031556Srgrimes		gotsigchild = 0;
9041556Srgrimes		save = signal(SIGCLD, onsigchild);
9051556Srgrimes		signal(SIGCLD, save);
9061556Srgrimes		if (gotsigchild == 0)
9071556Srgrimes			return 0;
9081556Srgrimes	}
9091556Srgrimes	return wait(status);
9101556Srgrimes#else
9111556Srgrimes	if (block == 0)
9121556Srgrimes		return 0;
9131556Srgrimes	return wait(status);
9141556Srgrimes#endif
9151556Srgrimes#endif
9161556Srgrimes}
9171556Srgrimes
9181556Srgrimes/*
9191556Srgrimes * return 1 if there are stopped jobs, otherwise 0
9201556Srgrimes */
9211556Srgrimesint job_warning = 0;
9221556Srgrimesint
92390111Simpstoppedjobs(void)
9241556Srgrimes{
92525222Ssteve	int jobno;
92625222Ssteve	struct job *jp;
9271556Srgrimes
9281556Srgrimes	if (job_warning)
9291556Srgrimes		return (0);
9301556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
9311556Srgrimes		if (jp->used == 0)
9321556Srgrimes			continue;
9331556Srgrimes		if (jp->state == JOBSTOPPED) {
9341556Srgrimes			out2str("You have stopped jobs.\n");
9351556Srgrimes			job_warning = 2;
9361556Srgrimes			return (1);
9371556Srgrimes		}
9381556Srgrimes	}
9391556Srgrimes
9401556Srgrimes	return (0);
9411556Srgrimes}
9421556Srgrimes
9431556Srgrimes/*
9441556Srgrimes * Return a string identifying a command (to be printed by the
9451556Srgrimes * jobs command.
9461556Srgrimes */
9471556Srgrimes
9481556SrgrimesSTATIC char *cmdnextc;
9491556SrgrimesSTATIC int cmdnleft;
9501556Srgrimes#define MAXCMDTEXT	200
9511556Srgrimes
9521556Srgrimeschar *
95390111Simpcommandtext(union node *n)
95490111Simp{
9551556Srgrimes	char *name;
9561556Srgrimes
9571556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
9581556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
9591556Srgrimes	cmdtxt(n);
9601556Srgrimes	*cmdnextc = '\0';
9611556Srgrimes	return name;
9621556Srgrimes}
9631556Srgrimes
9641556Srgrimes
9651556SrgrimesSTATIC void
96690111Simpcmdtxt(union node *n)
96790111Simp{
9681556Srgrimes	union node *np;
9691556Srgrimes	struct nodelist *lp;
9701556Srgrimes	char *p;
9711556Srgrimes	int i;
9721556Srgrimes	char s[2];
9731556Srgrimes
9741556Srgrimes	if (n == NULL)
9751556Srgrimes		return;
9761556Srgrimes	switch (n->type) {
9771556Srgrimes	case NSEMI:
9781556Srgrimes		cmdtxt(n->nbinary.ch1);
9791556Srgrimes		cmdputs("; ");
9801556Srgrimes		cmdtxt(n->nbinary.ch2);
9811556Srgrimes		break;
9821556Srgrimes	case NAND:
9831556Srgrimes		cmdtxt(n->nbinary.ch1);
9841556Srgrimes		cmdputs(" && ");
9851556Srgrimes		cmdtxt(n->nbinary.ch2);
9861556Srgrimes		break;
9871556Srgrimes	case NOR:
9881556Srgrimes		cmdtxt(n->nbinary.ch1);
9891556Srgrimes		cmdputs(" || ");
9901556Srgrimes		cmdtxt(n->nbinary.ch2);
9911556Srgrimes		break;
9921556Srgrimes	case NPIPE:
9931556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
9941556Srgrimes			cmdtxt(lp->n);
9951556Srgrimes			if (lp->next)
9961556Srgrimes				cmdputs(" | ");
9971556Srgrimes		}
9981556Srgrimes		break;
9991556Srgrimes	case NSUBSHELL:
10001556Srgrimes		cmdputs("(");
10011556Srgrimes		cmdtxt(n->nredir.n);
10021556Srgrimes		cmdputs(")");
10031556Srgrimes		break;
10041556Srgrimes	case NREDIR:
10051556Srgrimes	case NBACKGND:
10061556Srgrimes		cmdtxt(n->nredir.n);
10071556Srgrimes		break;
10081556Srgrimes	case NIF:
10091556Srgrimes		cmdputs("if ");
10101556Srgrimes		cmdtxt(n->nif.test);
10111556Srgrimes		cmdputs("; then ");
10121556Srgrimes		cmdtxt(n->nif.ifpart);
10131556Srgrimes		cmdputs("...");
10141556Srgrimes		break;
10151556Srgrimes	case NWHILE:
10161556Srgrimes		cmdputs("while ");
10171556Srgrimes		goto until;
10181556Srgrimes	case NUNTIL:
10191556Srgrimes		cmdputs("until ");
10201556Srgrimesuntil:
10211556Srgrimes		cmdtxt(n->nbinary.ch1);
10221556Srgrimes		cmdputs("; do ");
10231556Srgrimes		cmdtxt(n->nbinary.ch2);
10241556Srgrimes		cmdputs("; done");
10251556Srgrimes		break;
10261556Srgrimes	case NFOR:
10271556Srgrimes		cmdputs("for ");
10281556Srgrimes		cmdputs(n->nfor.var);
10291556Srgrimes		cmdputs(" in ...");
10301556Srgrimes		break;
10311556Srgrimes	case NCASE:
10321556Srgrimes		cmdputs("case ");
10331556Srgrimes		cmdputs(n->ncase.expr->narg.text);
10341556Srgrimes		cmdputs(" in ...");
10351556Srgrimes		break;
10361556Srgrimes	case NDEFUN:
10371556Srgrimes		cmdputs(n->narg.text);
10381556Srgrimes		cmdputs("() ...");
10391556Srgrimes		break;
10401556Srgrimes	case NCMD:
10411556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
10421556Srgrimes			cmdtxt(np);
10431556Srgrimes			if (np->narg.next)
10441556Srgrimes				cmdputs(" ");
10451556Srgrimes		}
10461556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
10471556Srgrimes			cmdputs(" ");
10481556Srgrimes			cmdtxt(np);
10491556Srgrimes		}
10501556Srgrimes		break;
10511556Srgrimes	case NARG:
10521556Srgrimes		cmdputs(n->narg.text);
10531556Srgrimes		break;
10541556Srgrimes	case NTO:
10551556Srgrimes		p = ">";  i = 1;  goto redir;
10561556Srgrimes	case NAPPEND:
10571556Srgrimes		p = ">>";  i = 1;  goto redir;
10581556Srgrimes	case NTOFD:
10591556Srgrimes		p = ">&";  i = 1;  goto redir;
10601556Srgrimes	case NFROM:
10611556Srgrimes		p = "<";  i = 0;  goto redir;
106266612Sbrian	case NFROMTO:
106366612Sbrian		p = "<>";  i = 0;  goto redir;
10641556Srgrimes	case NFROMFD:
10651556Srgrimes		p = "<&";  i = 0;  goto redir;
10661556Srgrimesredir:
10671556Srgrimes		if (n->nfile.fd != i) {
10681556Srgrimes			s[0] = n->nfile.fd + '0';
10691556Srgrimes			s[1] = '\0';
10701556Srgrimes			cmdputs(s);
10711556Srgrimes		}
10721556Srgrimes		cmdputs(p);
10731556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
10741556Srgrimes			s[0] = n->ndup.dupfd + '0';
10751556Srgrimes			s[1] = '\0';
10761556Srgrimes			cmdputs(s);
10771556Srgrimes		} else {
10781556Srgrimes			cmdtxt(n->nfile.fname);
10791556Srgrimes		}
10801556Srgrimes		break;
10811556Srgrimes	case NHERE:
10821556Srgrimes	case NXHERE:
10831556Srgrimes		cmdputs("<<...");
10841556Srgrimes		break;
10851556Srgrimes	default:
10861556Srgrimes		cmdputs("???");
10871556Srgrimes		break;
10881556Srgrimes	}
10891556Srgrimes}
10901556Srgrimes
10911556Srgrimes
10921556Srgrimes
10931556SrgrimesSTATIC void
109490111Simpcmdputs(char *s)
109590111Simp{
109625222Ssteve	char *p, *q;
109725222Ssteve	char c;
10981556Srgrimes	int subtype = 0;
10991556Srgrimes
11001556Srgrimes	if (cmdnleft <= 0)
11011556Srgrimes		return;
11021556Srgrimes	p = s;
11031556Srgrimes	q = cmdnextc;
11041556Srgrimes	while ((c = *p++) != '\0') {
11051556Srgrimes		if (c == CTLESC)
11061556Srgrimes			*q++ = *p++;
11071556Srgrimes		else if (c == CTLVAR) {
11081556Srgrimes			*q++ = '$';
11091556Srgrimes			if (--cmdnleft > 0)
11101556Srgrimes				*q++ = '{';
11111556Srgrimes			subtype = *p++;
11121556Srgrimes		} else if (c == '=' && subtype != 0) {
11131556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
11141556Srgrimes			subtype = 0;
11151556Srgrimes		} else if (c == CTLENDVAR) {
11161556Srgrimes			*q++ = '}';
111718954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
11181556Srgrimes			cmdnleft++;		/* ignore it */
11191556Srgrimes		else
11201556Srgrimes			*q++ = c;
11211556Srgrimes		if (--cmdnleft <= 0) {
11221556Srgrimes			*q++ = '.';
11231556Srgrimes			*q++ = '.';
11241556Srgrimes			*q++ = '.';
11251556Srgrimes			break;
11261556Srgrimes		}
11271556Srgrimes	}
11281556Srgrimes	cmdnextc = q;
11291556Srgrimes}
1130