jobs.c revision 36150
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[] =
4236150Scharnier	"$Id$";
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
901556Srgrimes
9120425Ssteve#if JOBS
9217987SpeterSTATIC void restartjob __P((struct job *));
9320425Ssteve#endif
9417987SpeterSTATIC void freejob __P((struct job *));
9517987SpeterSTATIC struct job *getjob __P((char *));
9617987SpeterSTATIC int dowait __P((int, struct job *));
9720425Ssteve#if SYSV
9817987SpeterSTATIC int onsigchild __P((void));
9920425Ssteve#endif
10017987SpeterSTATIC int waitproc __P((int, int *));
10117987SpeterSTATIC void cmdtxt __P((union node *));
10217987SpeterSTATIC void cmdputs __P((char *));
1031556Srgrimes
1041556Srgrimes
1051556Srgrimes/*
1061556Srgrimes * Turn job control on and off.
1071556Srgrimes *
1081556Srgrimes * Note:  This code assumes that the third arg to ioctl is a character
1091556Srgrimes * pointer, which is true on Berkeley systems but not System V.  Since
1101556Srgrimes * System V doesn't have job control yet, this isn't a problem now.
1111556Srgrimes */
1121556Srgrimes
1131556SrgrimesMKINIT int jobctl;
1141556Srgrimes
11520425Ssteve#if JOBS
1161556Srgrimesvoid
11720425Sstevesetjobctl(on)
11817987Speter	int on;
11917987Speter{
1201556Srgrimes#ifdef OLD_TTY_DRIVER
1211556Srgrimes	int ldisc;
1221556Srgrimes#endif
1231556Srgrimes
1241556Srgrimes	if (on == jobctl || rootshell == 0)
1251556Srgrimes		return;
1261556Srgrimes	if (on) {
1271556Srgrimes		do { /* while we are in the background */
12820425Ssteve#ifdef OLD_TTY_DRIVER
1291556Srgrimes			if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) {
13020425Ssteve#else
13120425Ssteve			initialpgrp = tcgetpgrp(2);
13220425Ssteve			if (initialpgrp < 0) {
13320425Ssteve#endif
1341556Srgrimes				out2str("sh: can't access tty; job control turned off\n");
1351556Srgrimes				mflag = 0;
1361556Srgrimes				return;
1371556Srgrimes			}
1381556Srgrimes			if (initialpgrp == -1)
13917987Speter				initialpgrp = getpgrp();
14017987Speter			else if (initialpgrp != getpgrp()) {
1411556Srgrimes				killpg(initialpgrp, SIGTTIN);
1421556Srgrimes				continue;
1431556Srgrimes			}
1441556Srgrimes		} while (0);
1451556Srgrimes#ifdef OLD_TTY_DRIVER
1461556Srgrimes		if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) {
1471556Srgrimes			out2str("sh: need new tty driver to run job control; job control turned off\n");
1481556Srgrimes			mflag = 0;
1491556Srgrimes			return;
1501556Srgrimes		}
1511556Srgrimes#endif
1521556Srgrimes		setsignal(SIGTSTP);
1531556Srgrimes		setsignal(SIGTTOU);
1541556Srgrimes		setsignal(SIGTTIN);
15517987Speter		setpgid(0, rootpid);
15620425Ssteve#ifdef OLD_TTY_DRIVER
1571556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&rootpid);
15820425Ssteve#else
15920425Ssteve		tcsetpgrp(2, rootpid);
16020425Ssteve#endif
1611556Srgrimes	} else { /* turning job control off */
16217987Speter		setpgid(0, initialpgrp);
16320425Ssteve#ifdef OLD_TTY_DRIVER
1641556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&initialpgrp);
16520425Ssteve#else
16620425Ssteve		tcsetpgrp(2, initialpgrp);
16720425Ssteve#endif
1681556Srgrimes		setsignal(SIGTSTP);
1691556Srgrimes		setsignal(SIGTTOU);
1701556Srgrimes		setsignal(SIGTTIN);
1711556Srgrimes	}
1721556Srgrimes	jobctl = on;
1731556Srgrimes}
17420425Ssteve#endif
1751556Srgrimes
1761556Srgrimes
1771556Srgrimes#ifdef mkinit
17828346SsteveINCLUDE <sys/types.h>
17917987SpeterINCLUDE <stdlib.h>
1801556Srgrimes
1811556SrgrimesSHELLPROC {
1821556Srgrimes	backgndpid = -1;
1831556Srgrimes#if JOBS
1841556Srgrimes	jobctl = 0;
1851556Srgrimes#endif
1861556Srgrimes}
1871556Srgrimes
1881556Srgrimes#endif
1891556Srgrimes
1901556Srgrimes
1911556Srgrimes
1921556Srgrimes#if JOBS
19317987Speterint
19417987Speterfgcmd(argc, argv)
19525905Ssteve	int argc __unused;
19620425Ssteve	char **argv;
19717987Speter{
1981556Srgrimes	struct job *jp;
1991556Srgrimes	int pgrp;
2001556Srgrimes	int status;
2011556Srgrimes
2021556Srgrimes	jp = getjob(argv[1]);
2031556Srgrimes	if (jp->jobctl == 0)
2041556Srgrimes		error("job not created under job control");
2051556Srgrimes	pgrp = jp->ps[0].pid;
20620425Ssteve#ifdef OLD_TTY_DRIVER
2071556Srgrimes	ioctl(2, TIOCSPGRP, (char *)&pgrp);
20820425Ssteve#else
20920425Ssteve	tcsetpgrp(2, pgrp);
21020425Ssteve#endif
2111556Srgrimes	restartjob(jp);
2121556Srgrimes	INTOFF;
2131556Srgrimes	status = waitforjob(jp);
2141556Srgrimes	INTON;
2151556Srgrimes	return status;
2161556Srgrimes}
2171556Srgrimes
2181556Srgrimes
21917987Speterint
22017987Speterbgcmd(argc, argv)
22117987Speter	int argc;
22220425Ssteve	char **argv;
22317987Speter{
2241556Srgrimes	struct job *jp;
2251556Srgrimes
2261556Srgrimes	do {
2271556Srgrimes		jp = getjob(*++argv);
2281556Srgrimes		if (jp->jobctl == 0)
2291556Srgrimes			error("job not created under job control");
2301556Srgrimes		restartjob(jp);
2311556Srgrimes	} while (--argc > 1);
2321556Srgrimes	return 0;
2331556Srgrimes}
2341556Srgrimes
2351556Srgrimes
2361556SrgrimesSTATIC void
2371556Srgrimesrestartjob(jp)
2381556Srgrimes	struct job *jp;
23917987Speter{
2401556Srgrimes	struct procstat *ps;
2411556Srgrimes	int i;
2421556Srgrimes
2431556Srgrimes	if (jp->state == JOBDONE)
2441556Srgrimes		return;
2451556Srgrimes	INTOFF;
2461556Srgrimes	killpg(jp->ps[0].pid, SIGCONT);
2471556Srgrimes	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
24826104Ssteve		if (WIFSTOPPED(ps->status)) {
2491556Srgrimes			ps->status = -1;
2501556Srgrimes			jp->state = 0;
2511556Srgrimes		}
2521556Srgrimes	}
2531556Srgrimes	INTON;
2541556Srgrimes}
2551556Srgrimes#endif
2561556Srgrimes
2571556Srgrimes
2581556Srgrimesint
25917987Speterjobscmd(argc, argv)
26025905Ssteve	int argc __unused;
26125905Ssteve	char **argv __unused;
26217987Speter{
2631556Srgrimes	showjobs(0);
2641556Srgrimes	return 0;
2651556Srgrimes}
2661556Srgrimes
2671556Srgrimes
2681556Srgrimes/*
2691556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
2701556Srgrimes * statuses have changed since the last call to showjobs.
2711556Srgrimes *
2721556Srgrimes * If the shell is interrupted in the process of creating a job, the
2731556Srgrimes * result may be a job structure containing zero processes.  Such structures
2741556Srgrimes * will be freed here.
2751556Srgrimes */
2761556Srgrimes
2771556Srgrimesvoid
27820425Ssteveshowjobs(change)
27917987Speter	int change;
28017987Speter{
2811556Srgrimes	int jobno;
2821556Srgrimes	int procno;
2831556Srgrimes	int i;
2841556Srgrimes	struct job *jp;
2851556Srgrimes	struct procstat *ps;
2861556Srgrimes	int col;
2871556Srgrimes	char s[64];
2881556Srgrimes
2891556Srgrimes	TRACE(("showjobs(%d) called\n", change));
2901556Srgrimes	while (dowait(0, (struct job *)NULL) > 0);
2911556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
2921556Srgrimes		if (! jp->used)
2931556Srgrimes			continue;
2941556Srgrimes		if (jp->nprocs == 0) {
2951556Srgrimes			freejob(jp);
2961556Srgrimes			continue;
2971556Srgrimes		}
2981556Srgrimes		if (change && ! jp->changed)
2991556Srgrimes			continue;
3001556Srgrimes		procno = jp->nprocs;
3011556Srgrimes		for (ps = jp->ps ; ; ps++) {	/* for each process */
3021556Srgrimes			if (ps == jp->ps)
3031556Srgrimes				fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
3041556Srgrimes			else
3051556Srgrimes				fmtstr(s, 64, "    %d ", ps->pid);
3061556Srgrimes			out1str(s);
3071556Srgrimes			col = strlen(s);
3081556Srgrimes			s[0] = '\0';
3091556Srgrimes			if (ps->status == -1) {
3101556Srgrimes				/* don't print anything */
31126104Ssteve			} else if (WIFEXITED(ps->status)) {
31226104Ssteve				fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status));
3131556Srgrimes			} else {
3141556Srgrimes#if JOBS
31526104Ssteve				if (WIFSTOPPED(ps->status))
31626104Ssteve					i = WSTOPSIG(ps->status);
31726104Ssteve				else
3181556Srgrimes#endif
31926104Ssteve					i = WTERMSIG(ps->status);
32017987Speter				if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
32117987Speter					scopy(sys_siglist[i & 0x7F], s);
3221556Srgrimes				else
3231556Srgrimes					fmtstr(s, 64, "Signal %d", i & 0x7F);
32426104Ssteve				if (WCOREDUMP(ps->status))
3251556Srgrimes					strcat(s, " (core dumped)");
3261556Srgrimes			}
3271556Srgrimes			out1str(s);
3281556Srgrimes			col += strlen(s);
3291556Srgrimes			do {
3301556Srgrimes				out1c(' ');
3311556Srgrimes				col++;
3321556Srgrimes			} while (col < 30);
3331556Srgrimes			out1str(ps->cmd);
3341556Srgrimes			out1c('\n');
3351556Srgrimes			if (--procno <= 0)
3361556Srgrimes				break;
3371556Srgrimes		}
3381556Srgrimes		jp->changed = 0;
3391556Srgrimes		if (jp->state == JOBDONE) {
3401556Srgrimes			freejob(jp);
3411556Srgrimes		}
3421556Srgrimes	}
3431556Srgrimes}
3441556Srgrimes
3451556Srgrimes
3461556Srgrimes/*
3471556Srgrimes * Mark a job structure as unused.
3481556Srgrimes */
3491556Srgrimes
3501556SrgrimesSTATIC void
3511556Srgrimesfreejob(jp)
3521556Srgrimes	struct job *jp;
3531556Srgrimes	{
3541556Srgrimes	struct procstat *ps;
3551556Srgrimes	int i;
3561556Srgrimes
3571556Srgrimes	INTOFF;
3581556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
3591556Srgrimes		if (ps->cmd != nullstr)
3601556Srgrimes			ckfree(ps->cmd);
3611556Srgrimes	}
3621556Srgrimes	if (jp->ps != &jp->ps0)
3631556Srgrimes		ckfree(jp->ps);
3641556Srgrimes	jp->used = 0;
3651556Srgrimes#if JOBS
3661556Srgrimes	if (curjob == jp - jobtab + 1)
3671556Srgrimes		curjob = 0;
3681556Srgrimes#endif
3691556Srgrimes	INTON;
3701556Srgrimes}
3711556Srgrimes
3721556Srgrimes
3731556Srgrimes
3741556Srgrimesint
37520425Sstevewaitcmd(argc, argv)
37617987Speter	int argc;
37720425Ssteve	char **argv;
37817987Speter{
3791556Srgrimes	struct job *job;
38026104Ssteve	int status, retval;
3811556Srgrimes	struct job *jp;
3821556Srgrimes
3831556Srgrimes	if (argc > 1) {
3841556Srgrimes		job = getjob(argv[1]);
3851556Srgrimes	} else {
3861556Srgrimes		job = NULL;
3871556Srgrimes	}
3881556Srgrimes	for (;;) {	/* loop until process terminated or stopped */
3891556Srgrimes		if (job != NULL) {
3901556Srgrimes			if (job->state) {
3911556Srgrimes				status = job->ps[job->nprocs - 1].status;
39226104Ssteve				if (WIFEXITED(status))
39326104Ssteve					retval = WEXITSTATUS(status);
3941556Srgrimes#if JOBS
39526104Ssteve				else if (WIFSTOPPED(status))
39626104Ssteve					retval = WSTOPSIG(status) + 128;
3971556Srgrimes#endif
3981556Srgrimes				else
39926104Ssteve					retval = WTERMSIG(status) + 128;
4001556Srgrimes				if (! iflag)
4011556Srgrimes					freejob(job);
40226104Ssteve				return retval;
4031556Srgrimes			}
4041556Srgrimes		} else {
4051556Srgrimes			for (jp = jobtab ; ; jp++) {
4061556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
4071556Srgrimes					return 0;
4081556Srgrimes				}
4091556Srgrimes				if (jp->used && jp->state == 0)
4101556Srgrimes					break;
4111556Srgrimes			}
4121556Srgrimes		}
4131556Srgrimes		dowait(1, (struct job *)NULL);
4141556Srgrimes	}
4151556Srgrimes}
4161556Srgrimes
4171556Srgrimes
4181556Srgrimes
41917987Speterint
42020425Sstevejobidcmd(argc, argv)
42125905Ssteve	int argc __unused;
42220425Ssteve	char **argv;
42317987Speter{
4241556Srgrimes	struct job *jp;
4251556Srgrimes	int i;
4261556Srgrimes
4271556Srgrimes	jp = getjob(argv[1]);
4281556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
4291556Srgrimes		out1fmt("%d", jp->ps[i].pid);
4301556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
4311556Srgrimes	}
4321556Srgrimes	return 0;
4331556Srgrimes}
4341556Srgrimes
4351556Srgrimes
4361556Srgrimes
4371556Srgrimes/*
4381556Srgrimes * Convert a job name to a job structure.
4391556Srgrimes */
4401556Srgrimes
4411556SrgrimesSTATIC struct job *
4421556Srgrimesgetjob(name)
4431556Srgrimes	char *name;
4441556Srgrimes	{
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 *
5021556Srgrimesmakejob(node, nprocs)
50325905Ssteve	union node *node __unused;
50417987Speter	int nprocs;
50517987Speter{
5061556Srgrimes	int i;
5071556Srgrimes	struct job *jp;
5081556Srgrimes
5091556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
5101556Srgrimes		if (--i < 0) {
5111556Srgrimes			INTOFF;
5121556Srgrimes			if (njobs == 0) {
5131556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
5141556Srgrimes			} else {
5151556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
51617987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
51720425Ssteve				/* Relocate `ps' pointers */
51820425Ssteve				for (i = 0; i < njobs; i++)
51920425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
52020425Ssteve						jp[i].ps = &jp[i].ps0;
5211556Srgrimes				ckfree(jobtab);
5221556Srgrimes				jobtab = jp;
5231556Srgrimes			}
5241556Srgrimes			jp = jobtab + njobs;
5251556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
5261556Srgrimes			INTON;
5271556Srgrimes			break;
5281556Srgrimes		}
5291556Srgrimes		if (jp->used == 0)
5301556Srgrimes			break;
5311556Srgrimes	}
5321556Srgrimes	INTOFF;
5331556Srgrimes	jp->state = 0;
5341556Srgrimes	jp->used = 1;
5351556Srgrimes	jp->changed = 0;
5361556Srgrimes	jp->nprocs = 0;
5371556Srgrimes#if JOBS
5381556Srgrimes	jp->jobctl = jobctl;
5391556Srgrimes#endif
5401556Srgrimes	if (nprocs > 1) {
5411556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
5421556Srgrimes	} else {
5431556Srgrimes		jp->ps = &jp->ps0;
5441556Srgrimes	}
5451556Srgrimes	INTON;
54617987Speter	TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
54717987Speter	    jp - jobtab + 1));
5481556Srgrimes	return jp;
5498855Srgrimes}
5501556Srgrimes
5511556Srgrimes
5521556Srgrimes/*
5531556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
5541556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
5551556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
5561556Srgrimes * be NULL.  The mode parameter can be one of the following:
5571556Srgrimes *	FORK_FG - Fork off a foreground process.
5581556Srgrimes *	FORK_BG - Fork off a background process.
5591556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
5601556Srgrimes *		     process group even if job control is on.
5611556Srgrimes *
5621556Srgrimes * When job control is turned off, background processes have their standard
5631556Srgrimes * input redirected to /dev/null (except for the second and later processes
5641556Srgrimes * in a pipeline).
5651556Srgrimes */
5661556Srgrimes
5671556Srgrimesint
5681556Srgrimesforkshell(jp, n, mode)
5691556Srgrimes	union node *n;
5701556Srgrimes	struct job *jp;
57117987Speter	int mode;
57217987Speter{
5731556Srgrimes	int pid;
5741556Srgrimes	int pgrp;
5751556Srgrimes
57617987Speter	TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
57717987Speter	    mode));
5781556Srgrimes	INTOFF;
5791556Srgrimes	pid = fork();
5801556Srgrimes	if (pid == -1) {
5811556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
5821556Srgrimes		INTON;
5831556Srgrimes		error("Cannot fork");
5841556Srgrimes	}
5851556Srgrimes	if (pid == 0) {
5861556Srgrimes		struct job *p;
5871556Srgrimes		int wasroot;
5881556Srgrimes		int i;
5891556Srgrimes
5901556Srgrimes		TRACE(("Child shell %d\n", getpid()));
5911556Srgrimes		wasroot = rootshell;
5921556Srgrimes		rootshell = 0;
5931556Srgrimes		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
5941556Srgrimes			if (p->used)
5951556Srgrimes				freejob(p);
5961556Srgrimes		closescript();
5971556Srgrimes		INTON;
5981556Srgrimes		clear_traps();
5991556Srgrimes#if JOBS
6001556Srgrimes		jobctl = 0;		/* do job control only in root shell */
6011556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
6021556Srgrimes			if (jp == NULL || jp->nprocs == 0)
6031556Srgrimes				pgrp = getpid();
6041556Srgrimes			else
6051556Srgrimes				pgrp = jp->ps[0].pid;
60621352Ssteve			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
6071556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
60820425Ssteve#ifdef OLD_TTY_DRIVER
6091556Srgrimes				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
61018016Speter					error("TIOCSPGRP failed, errno=%d", errno);
61120425Ssteve#else
61220425Ssteve				if (tcsetpgrp(2, pgrp) < 0)
61320425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
61420425Ssteve#endif
6151556Srgrimes			}
6161556Srgrimes			setsignal(SIGTSTP);
6171556Srgrimes			setsignal(SIGTTOU);
6181556Srgrimes		} else if (mode == FORK_BG) {
6191556Srgrimes			ignoresig(SIGINT);
6201556Srgrimes			ignoresig(SIGQUIT);
6211556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
6221556Srgrimes			    ! fd0_redirected_p ()) {
6231556Srgrimes				close(0);
6241556Srgrimes				if (open("/dev/null", O_RDONLY) != 0)
6251556Srgrimes					error("Can't open /dev/null");
6261556Srgrimes			}
6271556Srgrimes		}
6281556Srgrimes#else
6291556Srgrimes		if (mode == FORK_BG) {
6301556Srgrimes			ignoresig(SIGINT);
6311556Srgrimes			ignoresig(SIGQUIT);
6321556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
6331556Srgrimes			    ! fd0_redirected_p ()) {
6341556Srgrimes				close(0);
6351556Srgrimes				if (open("/dev/null", O_RDONLY) != 0)
6361556Srgrimes					error("Can't open /dev/null");
6371556Srgrimes			}
6381556Srgrimes		}
6391556Srgrimes#endif
6401556Srgrimes		if (wasroot && iflag) {
6411556Srgrimes			setsignal(SIGINT);
6421556Srgrimes			setsignal(SIGQUIT);
6431556Srgrimes			setsignal(SIGTERM);
6441556Srgrimes		}
6451556Srgrimes		return pid;
6461556Srgrimes	}
6471556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
6481556Srgrimes		if (jp == NULL || jp->nprocs == 0)
6491556Srgrimes			pgrp = pid;
6501556Srgrimes		else
6511556Srgrimes			pgrp = jp->ps[0].pid;
65217987Speter		setpgid(pid, pgrp);
6531556Srgrimes	}
6541556Srgrimes	if (mode == FORK_BG)
6551556Srgrimes		backgndpid = pid;		/* set $! */
6561556Srgrimes	if (jp) {
6571556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
6581556Srgrimes		ps->pid = pid;
6591556Srgrimes		ps->status = -1;
6601556Srgrimes		ps->cmd = nullstr;
6611556Srgrimes		if (iflag && rootshell && n)
6621556Srgrimes			ps->cmd = commandtext(n);
6631556Srgrimes	}
6641556Srgrimes	INTON;
6651556Srgrimes	TRACE(("In parent shell:  child = %d\n", pid));
6661556Srgrimes	return pid;
6671556Srgrimes}
6681556Srgrimes
6691556Srgrimes
6701556Srgrimes
6711556Srgrimes/*
6721556Srgrimes * Wait for job to finish.
6731556Srgrimes *
6741556Srgrimes * Under job control we have the problem that while a child process is
6751556Srgrimes * running interrupts generated by the user are sent to the child but not
6761556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
6771556Srgrimes * active user may be hard to kill.  With job control turned off, an
6781556Srgrimes * interactive user may place an interactive program inside a loop.  If
6791556Srgrimes * the interactive program catches interrupts, the user doesn't want
6801556Srgrimes * these interrupts to also abort the loop.  The approach we take here
6811556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
6821556Srgrimes * forground process to terminate, and then send itself an interrupt
6831556Srgrimes * signal if the child process was terminated by an interrupt signal.
6841556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
6851556Srgrimes * exit on interrupt; unless these processes terminate themselves by
6861556Srgrimes * sending a signal to themselves (instead of calling exit) they will
6871556Srgrimes * confuse this approach.
6881556Srgrimes */
6891556Srgrimes
6901556Srgrimesint
6911556Srgrimeswaitforjob(jp)
69225222Ssteve	struct job *jp;
6931556Srgrimes	{
6941556Srgrimes#if JOBS
69517987Speter	int mypgrp = getpgrp();
6961556Srgrimes#endif
6971556Srgrimes	int status;
6981556Srgrimes	int st;
6991556Srgrimes
7001556Srgrimes	INTOFF;
7011556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
7021556Srgrimes	while (jp->state == 0) {
7031556Srgrimes		dowait(1, jp);
7041556Srgrimes	}
7051556Srgrimes#if JOBS
7061556Srgrimes	if (jp->jobctl) {
70720425Ssteve#ifdef OLD_TTY_DRIVER
7081556Srgrimes		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
70920425Ssteve			error("TIOCSPGRP failed, errno=%d\n", errno);
71020425Ssteve#else
71120425Ssteve		if (tcsetpgrp(2, mypgrp) < 0)
71220425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
71320425Ssteve#endif
7141556Srgrimes	}
7151556Srgrimes	if (jp->state == JOBSTOPPED)
7161556Srgrimes		curjob = jp - jobtab + 1;
7171556Srgrimes#endif
7181556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
7191556Srgrimes	/* convert to 8 bits */
72026104Ssteve	if (WIFEXITED(status))
72126104Ssteve		st = WEXITSTATUS(status);
7221556Srgrimes#if JOBS
72326104Ssteve	else if (WIFSTOPPED(status))
72426104Ssteve		st = WSTOPSIG(status) + 128;
7251556Srgrimes#endif
7261556Srgrimes	else
72726104Ssteve		st = WTERMSIG(status) + 128;
7281556Srgrimes	if (! JOBS || jp->state == JOBDONE)
7291556Srgrimes		freejob(jp);
7301556Srgrimes	CLEAR_PENDING_INT;
73126104Ssteve	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
7321556Srgrimes		kill(getpid(), SIGINT);
7331556Srgrimes	INTON;
7341556Srgrimes	return st;
7351556Srgrimes}
7361556Srgrimes
7371556Srgrimes
7381556Srgrimes
7391556Srgrimes/*
7401556Srgrimes * Wait for a process to terminate.
7411556Srgrimes */
7421556Srgrimes
7431556SrgrimesSTATIC int
7441556Srgrimesdowait(block, job)
74517987Speter	int block;
7461556Srgrimes	struct job *job;
74717987Speter{
7481556Srgrimes	int pid;
7491556Srgrimes	int status;
7501556Srgrimes	struct procstat *sp;
7511556Srgrimes	struct job *jp;
7521556Srgrimes	struct job *thisjob;
7531556Srgrimes	int done;
7541556Srgrimes	int stopped;
7551556Srgrimes	int core;
75626104Ssteve	int sig;
7571556Srgrimes
7581556Srgrimes	TRACE(("dowait(%d) called\n", block));
7591556Srgrimes	do {
7601556Srgrimes		pid = waitproc(block, &status);
7611556Srgrimes		TRACE(("wait returns %d, status=%d\n", pid, status));
7621556Srgrimes	} while (pid == -1 && errno == EINTR);
7631556Srgrimes	if (pid <= 0)
7641556Srgrimes		return pid;
7651556Srgrimes	INTOFF;
7661556Srgrimes	thisjob = NULL;
7671556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
7681556Srgrimes		if (jp->used) {
7691556Srgrimes			done = 1;
7701556Srgrimes			stopped = 1;
7711556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
7721556Srgrimes				if (sp->pid == -1)
7731556Srgrimes					continue;
7741556Srgrimes				if (sp->pid == pid) {
77526104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
77626104Ssteve						   pid, sp->status, status));
7771556Srgrimes					sp->status = status;
7781556Srgrimes					thisjob = jp;
7791556Srgrimes				}
7801556Srgrimes				if (sp->status == -1)
7811556Srgrimes					stopped = 0;
78226104Ssteve				else if (WIFSTOPPED(sp->status))
7831556Srgrimes					done = 0;
7841556Srgrimes			}
7851556Srgrimes			if (stopped) {		/* stopped or done */
7861556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
7871556Srgrimes				if (jp->state != state) {
7881556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
7891556Srgrimes					jp->state = state;
7901556Srgrimes#if JOBS
7911556Srgrimes					if (done && curjob == jp - jobtab + 1)
7921556Srgrimes						curjob = 0;		/* no current job */
7931556Srgrimes#endif
7941556Srgrimes				}
7951556Srgrimes			}
7961556Srgrimes		}
7971556Srgrimes	}
7981556Srgrimes	INTON;
7991556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
80026104Ssteve		core = WCOREDUMP(status);
8011556Srgrimes#if JOBS
80226104Ssteve		if (WIFSTOPPED(status))
80326104Ssteve			sig = WSTOPSIG(status);
80426104Ssteve		else
8051556Srgrimes#endif
80626104Ssteve			if (WIFEXITED(status))
80726104Ssteve				sig = 0;
80826104Ssteve			else
80926104Ssteve				sig = WTERMSIG(status);
81026104Ssteve
81126104Ssteve		if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
8121556Srgrimes			if (thisjob != job)
8131556Srgrimes				outfmt(out2, "%d: ", pid);
8141556Srgrimes#if JOBS
81526104Ssteve			if (sig == SIGTSTP && rootshell && iflag)
8161556Srgrimes				outfmt(out2, "%%%d ", job - jobtab + 1);
8171556Srgrimes#endif
81826104Ssteve			if (sig < NSIG && sys_siglist[sig])
81926104Ssteve				out2str(sys_siglist[sig]);
8201556Srgrimes			else
82126104Ssteve				outfmt(out2, "Signal %d", sig);
8221556Srgrimes			if (core)
8231556Srgrimes				out2str(" - core dumped");
8241556Srgrimes			out2c('\n');
8251556Srgrimes			flushout(&errout);
8261556Srgrimes		} else {
82726104Ssteve			TRACE(("Not printing status: status=%d, sig=%d\n",
82826104Ssteve				   status, sig));
8291556Srgrimes		}
8301556Srgrimes	} else {
8311556Srgrimes		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
8321556Srgrimes		if (thisjob)
8331556Srgrimes			thisjob->changed = 1;
8341556Srgrimes	}
8351556Srgrimes	return pid;
8361556Srgrimes}
8371556Srgrimes
8381556Srgrimes
8391556Srgrimes
8401556Srgrimes/*
8411556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
8421556Srgrimes * stopped processes.  If block is zero, we return a value of zero
8431556Srgrimes * rather than blocking.
8441556Srgrimes *
8451556Srgrimes * System V doesn't have a non-blocking wait system call.  It does
8461556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's
8471556Srgrimes * children dies.  The obvious way to use SIGCLD would be to install
8481556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD
8491556Srgrimes * was received, and have waitproc bump another counter when it got
8501556Srgrimes * the status of a process.  Waitproc would then know that a wait
8511556Srgrimes * system call would not block if the two counters were different.
8521556Srgrimes * This approach doesn't work because if a process has children that
8531556Srgrimes * have not been waited for, System V will send it a SIGCLD when it
8541556Srgrimes * installs a signal handler for SIGCLD.  What this means is that when
8551556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously
8561556Srgrimes * until is runs out of stack space, unless it does a wait call before
8571556Srgrimes * restoring the signal handler.  The code below takes advantage of
8581556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and
8591556Srgrimes * then checking to see whether it was called.  If there are any
8601556Srgrimes * children to be waited for, it will be.
8611556Srgrimes *
8621556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking
8631556Srgrimes * waits at all.  In this case, the user will not be informed when
8641556Srgrimes * a background process until the next time she runs a real program
8651556Srgrimes * (as opposed to running a builtin command or just typing return),
8661556Srgrimes * and the jobs command may give out of date information.
8671556Srgrimes */
8681556Srgrimes
8691556Srgrimes#ifdef SYSV
8701556SrgrimesSTATIC int gotsigchild;
8711556Srgrimes
8721556SrgrimesSTATIC int onsigchild() {
8731556Srgrimes	gotsigchild = 1;
8741556Srgrimes}
8751556Srgrimes#endif
8761556Srgrimes
8771556Srgrimes
8781556SrgrimesSTATIC int
8791556Srgrimeswaitproc(block, status)
88017987Speter	int block;
8811556Srgrimes	int *status;
88217987Speter{
8831556Srgrimes#ifdef BSD
8841556Srgrimes	int flags;
8851556Srgrimes
8861556Srgrimes#if JOBS
8871556Srgrimes	flags = WUNTRACED;
8881556Srgrimes#else
8891556Srgrimes	flags = 0;
8901556Srgrimes#endif
8911556Srgrimes	if (block == 0)
8921556Srgrimes		flags |= WNOHANG;
8931556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
8941556Srgrimes#else
8951556Srgrimes#ifdef SYSV
8961556Srgrimes	int (*save)();
8971556Srgrimes
8981556Srgrimes	if (block == 0) {
8991556Srgrimes		gotsigchild = 0;
9001556Srgrimes		save = signal(SIGCLD, onsigchild);
9011556Srgrimes		signal(SIGCLD, save);
9021556Srgrimes		if (gotsigchild == 0)
9031556Srgrimes			return 0;
9041556Srgrimes	}
9051556Srgrimes	return wait(status);
9061556Srgrimes#else
9071556Srgrimes	if (block == 0)
9081556Srgrimes		return 0;
9091556Srgrimes	return wait(status);
9101556Srgrimes#endif
9111556Srgrimes#endif
9121556Srgrimes}
9131556Srgrimes
9141556Srgrimes/*
9151556Srgrimes * return 1 if there are stopped jobs, otherwise 0
9161556Srgrimes */
9171556Srgrimesint job_warning = 0;
9181556Srgrimesint
9191556Srgrimesstoppedjobs()
9201556Srgrimes{
92125222Ssteve	int jobno;
92225222Ssteve	struct job *jp;
9231556Srgrimes
9241556Srgrimes	if (job_warning)
9251556Srgrimes		return (0);
9261556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
9271556Srgrimes		if (jp->used == 0)
9281556Srgrimes			continue;
9291556Srgrimes		if (jp->state == JOBSTOPPED) {
9301556Srgrimes			out2str("You have stopped jobs.\n");
9311556Srgrimes			job_warning = 2;
9321556Srgrimes			return (1);
9331556Srgrimes		}
9341556Srgrimes	}
9351556Srgrimes
9361556Srgrimes	return (0);
9371556Srgrimes}
9381556Srgrimes
9391556Srgrimes/*
9401556Srgrimes * Return a string identifying a command (to be printed by the
9411556Srgrimes * jobs command.
9421556Srgrimes */
9431556Srgrimes
9441556SrgrimesSTATIC char *cmdnextc;
9451556SrgrimesSTATIC int cmdnleft;
9461556SrgrimesSTATIC void cmdtxt(), cmdputs();
9471556Srgrimes#define MAXCMDTEXT	200
9481556Srgrimes
9491556Srgrimeschar *
9501556Srgrimescommandtext(n)
9511556Srgrimes	union node *n;
9521556Srgrimes	{
9531556Srgrimes	char *name;
9541556Srgrimes
9551556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
9561556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
9571556Srgrimes	cmdtxt(n);
9581556Srgrimes	*cmdnextc = '\0';
9591556Srgrimes	return name;
9601556Srgrimes}
9611556Srgrimes
9621556Srgrimes
9631556SrgrimesSTATIC void
9641556Srgrimescmdtxt(n)
9651556Srgrimes	union node *n;
9661556Srgrimes	{
9671556Srgrimes	union node *np;
9681556Srgrimes	struct nodelist *lp;
9691556Srgrimes	char *p;
9701556Srgrimes	int i;
9711556Srgrimes	char s[2];
9721556Srgrimes
9731556Srgrimes	if (n == NULL)
9741556Srgrimes		return;
9751556Srgrimes	switch (n->type) {
9761556Srgrimes	case NSEMI:
9771556Srgrimes		cmdtxt(n->nbinary.ch1);
9781556Srgrimes		cmdputs("; ");
9791556Srgrimes		cmdtxt(n->nbinary.ch2);
9801556Srgrimes		break;
9811556Srgrimes	case NAND:
9821556Srgrimes		cmdtxt(n->nbinary.ch1);
9831556Srgrimes		cmdputs(" && ");
9841556Srgrimes		cmdtxt(n->nbinary.ch2);
9851556Srgrimes		break;
9861556Srgrimes	case NOR:
9871556Srgrimes		cmdtxt(n->nbinary.ch1);
9881556Srgrimes		cmdputs(" || ");
9891556Srgrimes		cmdtxt(n->nbinary.ch2);
9901556Srgrimes		break;
9911556Srgrimes	case NPIPE:
9921556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
9931556Srgrimes			cmdtxt(lp->n);
9941556Srgrimes			if (lp->next)
9951556Srgrimes				cmdputs(" | ");
9961556Srgrimes		}
9971556Srgrimes		break;
9981556Srgrimes	case NSUBSHELL:
9991556Srgrimes		cmdputs("(");
10001556Srgrimes		cmdtxt(n->nredir.n);
10011556Srgrimes		cmdputs(")");
10021556Srgrimes		break;
10031556Srgrimes	case NREDIR:
10041556Srgrimes	case NBACKGND:
10051556Srgrimes		cmdtxt(n->nredir.n);
10061556Srgrimes		break;
10071556Srgrimes	case NIF:
10081556Srgrimes		cmdputs("if ");
10091556Srgrimes		cmdtxt(n->nif.test);
10101556Srgrimes		cmdputs("; then ");
10111556Srgrimes		cmdtxt(n->nif.ifpart);
10121556Srgrimes		cmdputs("...");
10131556Srgrimes		break;
10141556Srgrimes	case NWHILE:
10151556Srgrimes		cmdputs("while ");
10161556Srgrimes		goto until;
10171556Srgrimes	case NUNTIL:
10181556Srgrimes		cmdputs("until ");
10191556Srgrimesuntil:
10201556Srgrimes		cmdtxt(n->nbinary.ch1);
10211556Srgrimes		cmdputs("; do ");
10221556Srgrimes		cmdtxt(n->nbinary.ch2);
10231556Srgrimes		cmdputs("; done");
10241556Srgrimes		break;
10251556Srgrimes	case NFOR:
10261556Srgrimes		cmdputs("for ");
10271556Srgrimes		cmdputs(n->nfor.var);
10281556Srgrimes		cmdputs(" in ...");
10291556Srgrimes		break;
10301556Srgrimes	case NCASE:
10311556Srgrimes		cmdputs("case ");
10321556Srgrimes		cmdputs(n->ncase.expr->narg.text);
10331556Srgrimes		cmdputs(" in ...");
10341556Srgrimes		break;
10351556Srgrimes	case NDEFUN:
10361556Srgrimes		cmdputs(n->narg.text);
10371556Srgrimes		cmdputs("() ...");
10381556Srgrimes		break;
10391556Srgrimes	case NCMD:
10401556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
10411556Srgrimes			cmdtxt(np);
10421556Srgrimes			if (np->narg.next)
10431556Srgrimes				cmdputs(" ");
10441556Srgrimes		}
10451556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
10461556Srgrimes			cmdputs(" ");
10471556Srgrimes			cmdtxt(np);
10481556Srgrimes		}
10491556Srgrimes		break;
10501556Srgrimes	case NARG:
10511556Srgrimes		cmdputs(n->narg.text);
10521556Srgrimes		break;
10531556Srgrimes	case NTO:
10541556Srgrimes		p = ">";  i = 1;  goto redir;
10551556Srgrimes	case NAPPEND:
10561556Srgrimes		p = ">>";  i = 1;  goto redir;
10571556Srgrimes	case NTOFD:
10581556Srgrimes		p = ">&";  i = 1;  goto redir;
10591556Srgrimes	case NFROM:
10601556Srgrimes		p = "<";  i = 0;  goto redir;
10611556Srgrimes	case NFROMFD:
10621556Srgrimes		p = "<&";  i = 0;  goto redir;
10631556Srgrimesredir:
10641556Srgrimes		if (n->nfile.fd != i) {
10651556Srgrimes			s[0] = n->nfile.fd + '0';
10661556Srgrimes			s[1] = '\0';
10671556Srgrimes			cmdputs(s);
10681556Srgrimes		}
10691556Srgrimes		cmdputs(p);
10701556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
10711556Srgrimes			s[0] = n->ndup.dupfd + '0';
10721556Srgrimes			s[1] = '\0';
10731556Srgrimes			cmdputs(s);
10741556Srgrimes		} else {
10751556Srgrimes			cmdtxt(n->nfile.fname);
10761556Srgrimes		}
10771556Srgrimes		break;
10781556Srgrimes	case NHERE:
10791556Srgrimes	case NXHERE:
10801556Srgrimes		cmdputs("<<...");
10811556Srgrimes		break;
10821556Srgrimes	default:
10831556Srgrimes		cmdputs("???");
10841556Srgrimes		break;
10851556Srgrimes	}
10861556Srgrimes}
10871556Srgrimes
10881556Srgrimes
10891556Srgrimes
10901556SrgrimesSTATIC void
10911556Srgrimescmdputs(s)
10921556Srgrimes	char *s;
10931556Srgrimes	{
109425222Ssteve	char *p, *q;
109525222Ssteve	char c;
10961556Srgrimes	int subtype = 0;
10971556Srgrimes
10981556Srgrimes	if (cmdnleft <= 0)
10991556Srgrimes		return;
11001556Srgrimes	p = s;
11011556Srgrimes	q = cmdnextc;
11021556Srgrimes	while ((c = *p++) != '\0') {
11031556Srgrimes		if (c == CTLESC)
11041556Srgrimes			*q++ = *p++;
11051556Srgrimes		else if (c == CTLVAR) {
11061556Srgrimes			*q++ = '$';
11071556Srgrimes			if (--cmdnleft > 0)
11081556Srgrimes				*q++ = '{';
11091556Srgrimes			subtype = *p++;
11101556Srgrimes		} else if (c == '=' && subtype != 0) {
11111556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
11121556Srgrimes			subtype = 0;
11131556Srgrimes		} else if (c == CTLENDVAR) {
11141556Srgrimes			*q++ = '}';
111518954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
11161556Srgrimes			cmdnleft++;		/* ignore it */
11171556Srgrimes		else
11181556Srgrimes			*q++ = c;
11191556Srgrimes		if (--cmdnleft <= 0) {
11201556Srgrimes			*q++ = '.';
11211556Srgrimes			*q++ = '.';
11221556Srgrimes			*q++ = '.';
11231556Srgrimes			break;
11241556Srgrimes		}
11251556Srgrimes	}
11261556Srgrimes	cmdnextc = q;
11271556Srgrimes}
1128