jobs.c revision 18016
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.
353044Sdg *
3618016Speter *	$Id: jobs.c,v 1.5 1996/09/01 10:20:24 peter Exp $
371556Srgrimes */
381556Srgrimes
391556Srgrimes#ifndef lint
4017987Speterstatic char sccsid[] = "@(#)jobs.c	8.5 (Berkeley) 5/4/95";
411556Srgrimes#endif /* not lint */
421556Srgrimes
4317987Speter#include <fcntl.h>
4417987Speter#include <signal.h>
4517987Speter#include <errno.h>
4617987Speter#include <unistd.h>
4717987Speter#include <stdlib.h>
4817987Speter#include <sys/types.h>
4917987Speter#include <sys/param.h>
5017987Speter#ifdef BSD
5117987Speter#include <sys/wait.h>
5217987Speter#include <sys/time.h>
5317987Speter#include <sys/resource.h>
5417987Speter#endif
5517987Speter
561556Srgrimes#include "shell.h"
571556Srgrimes#if JOBS
5817987Speter#ifdef OLD_TTY_DRIVER
591556Srgrimes#include "sgtty.h"
6017987Speter#else
6117987Speter#include <termios.h>
6217987Speter#endif
631556Srgrimes#undef CEOF			/* syntax.h redefines this */
641556Srgrimes#endif
6517987Speter#include "redir.h"
6617987Speter#include "show.h"
671556Srgrimes#include "main.h"
681556Srgrimes#include "parser.h"
691556Srgrimes#include "nodes.h"
701556Srgrimes#include "jobs.h"
711556Srgrimes#include "options.h"
721556Srgrimes#include "trap.h"
731556Srgrimes#include "syntax.h"
741556Srgrimes#include "input.h"
751556Srgrimes#include "output.h"
761556Srgrimes#include "memalloc.h"
771556Srgrimes#include "error.h"
781556Srgrimes#include "mystring.h"
791556Srgrimes
801556Srgrimes
811556Srgrimesstruct job *jobtab;		/* array of jobs */
821556Srgrimesint njobs;			/* size of array */
831556SrgrimesMKINIT short backgndpid = -1;	/* pid of last background process */
841556Srgrimes#if JOBS
851556Srgrimesint initialpgrp;		/* pgrp of shell on invocation */
861556Srgrimesshort curjob;			/* current job */
871556Srgrimes#endif
881556Srgrimes
8917987SpeterSTATIC void restartjob __P((struct job *));
9017987SpeterSTATIC void freejob __P((struct job *));
9117987SpeterSTATIC struct job *getjob __P((char *));
9217987SpeterSTATIC int dowait __P((int, struct job *));
9317987SpeterSTATIC int onsigchild __P((void));
9417987SpeterSTATIC int waitproc __P((int, int *));
9517987SpeterSTATIC void cmdtxt __P((union node *));
9617987SpeterSTATIC void cmdputs __P((char *));
971556Srgrimes
981556Srgrimes
991556Srgrimes/*
1001556Srgrimes * Turn job control on and off.
1011556Srgrimes *
1021556Srgrimes * Note:  This code assumes that the third arg to ioctl is a character
1031556Srgrimes * pointer, which is true on Berkeley systems but not System V.  Since
1041556Srgrimes * System V doesn't have job control yet, this isn't a problem now.
1051556Srgrimes */
1061556Srgrimes
1071556SrgrimesMKINIT int jobctl;
1081556Srgrimes
1091556Srgrimesvoid
11017987Spetersetjobctl(on)
11117987Speter	int on;
11217987Speter{
1131556Srgrimes#ifdef OLD_TTY_DRIVER
1141556Srgrimes	int ldisc;
1151556Srgrimes#endif
1161556Srgrimes
1171556Srgrimes	if (on == jobctl || rootshell == 0)
1181556Srgrimes		return;
1191556Srgrimes	if (on) {
1201556Srgrimes		do { /* while we are in the background */
1211556Srgrimes			if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) {
1221556Srgrimes				out2str("sh: can't access tty; job control turned off\n");
1231556Srgrimes				mflag = 0;
1241556Srgrimes				return;
1251556Srgrimes			}
1261556Srgrimes			if (initialpgrp == -1)
12717987Speter				initialpgrp = getpgrp();
12817987Speter			else if (initialpgrp != getpgrp()) {
1291556Srgrimes				killpg(initialpgrp, SIGTTIN);
1301556Srgrimes				continue;
1311556Srgrimes			}
1321556Srgrimes		} while (0);
1331556Srgrimes#ifdef OLD_TTY_DRIVER
1341556Srgrimes		if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) {
1351556Srgrimes			out2str("sh: need new tty driver to run job control; job control turned off\n");
1361556Srgrimes			mflag = 0;
1371556Srgrimes			return;
1381556Srgrimes		}
1391556Srgrimes#endif
1401556Srgrimes		setsignal(SIGTSTP);
1411556Srgrimes		setsignal(SIGTTOU);
1421556Srgrimes		setsignal(SIGTTIN);
14317987Speter		setpgid(0, rootpid);
1441556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&rootpid);
1451556Srgrimes	} else { /* turning job control off */
14617987Speter		setpgid(0, initialpgrp);
1471556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&initialpgrp);
1481556Srgrimes		setsignal(SIGTSTP);
1491556Srgrimes		setsignal(SIGTTOU);
1501556Srgrimes		setsignal(SIGTTIN);
1511556Srgrimes	}
1521556Srgrimes	jobctl = on;
1531556Srgrimes}
1541556Srgrimes
1551556Srgrimes
1561556Srgrimes#ifdef mkinit
15717987SpeterINCLUDE <stdlib.h>
1581556Srgrimes
1591556SrgrimesSHELLPROC {
1601556Srgrimes	backgndpid = -1;
1611556Srgrimes#if JOBS
1621556Srgrimes	jobctl = 0;
1631556Srgrimes#endif
1641556Srgrimes}
1651556Srgrimes
1661556Srgrimes#endif
1671556Srgrimes
1681556Srgrimes
1691556Srgrimes
1701556Srgrimes#if JOBS
17117987Speterint
17217987Speterfgcmd(argc, argv)
17317987Speter	int argc;
17417987Speter	char **argv;
17517987Speter{
1761556Srgrimes	struct job *jp;
1771556Srgrimes	int pgrp;
1781556Srgrimes	int status;
1791556Srgrimes
1801556Srgrimes	jp = getjob(argv[1]);
1811556Srgrimes	if (jp->jobctl == 0)
1821556Srgrimes		error("job not created under job control");
1831556Srgrimes	pgrp = jp->ps[0].pid;
1841556Srgrimes	ioctl(2, TIOCSPGRP, (char *)&pgrp);
1851556Srgrimes	restartjob(jp);
1861556Srgrimes	INTOFF;
1871556Srgrimes	status = waitforjob(jp);
1881556Srgrimes	INTON;
1891556Srgrimes	return status;
1901556Srgrimes}
1911556Srgrimes
1921556Srgrimes
19317987Speterint
19417987Speterbgcmd(argc, argv)
19517987Speter	int argc;
19617987Speter	char **argv;
19717987Speter{
1981556Srgrimes	struct job *jp;
1991556Srgrimes
2001556Srgrimes	do {
2011556Srgrimes		jp = getjob(*++argv);
2021556Srgrimes		if (jp->jobctl == 0)
2031556Srgrimes			error("job not created under job control");
2041556Srgrimes		restartjob(jp);
2051556Srgrimes	} while (--argc > 1);
2061556Srgrimes	return 0;
2071556Srgrimes}
2081556Srgrimes
2091556Srgrimes
2101556SrgrimesSTATIC void
2111556Srgrimesrestartjob(jp)
2121556Srgrimes	struct job *jp;
21317987Speter{
2141556Srgrimes	struct procstat *ps;
2151556Srgrimes	int i;
2161556Srgrimes
2171556Srgrimes	if (jp->state == JOBDONE)
2181556Srgrimes		return;
2191556Srgrimes	INTOFF;
2201556Srgrimes	killpg(jp->ps[0].pid, SIGCONT);
2211556Srgrimes	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
2221556Srgrimes		if ((ps->status & 0377) == 0177) {
2231556Srgrimes			ps->status = -1;
2241556Srgrimes			jp->state = 0;
2251556Srgrimes		}
2261556Srgrimes	}
2271556Srgrimes	INTON;
2281556Srgrimes}
2291556Srgrimes#endif
2301556Srgrimes
2311556Srgrimes
2321556Srgrimesint
23317987Speterjobscmd(argc, argv)
23417987Speter	int argc;
23517987Speter	char **argv;
23617987Speter{
2371556Srgrimes	showjobs(0);
2381556Srgrimes	return 0;
2391556Srgrimes}
2401556Srgrimes
2411556Srgrimes
2421556Srgrimes/*
2431556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
2441556Srgrimes * statuses have changed since the last call to showjobs.
2451556Srgrimes *
2461556Srgrimes * If the shell is interrupted in the process of creating a job, the
2471556Srgrimes * result may be a job structure containing zero processes.  Such structures
2481556Srgrimes * will be freed here.
2491556Srgrimes */
2501556Srgrimes
2511556Srgrimesvoid
25217987Spetershowjobs(change)
25317987Speter	int change;
25417987Speter{
2551556Srgrimes	int jobno;
2561556Srgrimes	int procno;
2571556Srgrimes	int i;
2581556Srgrimes	struct job *jp;
2591556Srgrimes	struct procstat *ps;
2601556Srgrimes	int col;
2611556Srgrimes	char s[64];
2621556Srgrimes
2631556Srgrimes	TRACE(("showjobs(%d) called\n", change));
2641556Srgrimes	while (dowait(0, (struct job *)NULL) > 0);
2651556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
2661556Srgrimes		if (! jp->used)
2671556Srgrimes			continue;
2681556Srgrimes		if (jp->nprocs == 0) {
2691556Srgrimes			freejob(jp);
2701556Srgrimes			continue;
2711556Srgrimes		}
2721556Srgrimes		if (change && ! jp->changed)
2731556Srgrimes			continue;
2741556Srgrimes		procno = jp->nprocs;
2751556Srgrimes		for (ps = jp->ps ; ; ps++) {	/* for each process */
2761556Srgrimes			if (ps == jp->ps)
2771556Srgrimes				fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
2781556Srgrimes			else
2791556Srgrimes				fmtstr(s, 64, "    %d ", ps->pid);
2801556Srgrimes			out1str(s);
2811556Srgrimes			col = strlen(s);
2821556Srgrimes			s[0] = '\0';
2831556Srgrimes			if (ps->status == -1) {
2841556Srgrimes				/* don't print anything */
2851556Srgrimes			} else if ((ps->status & 0xFF) == 0) {
2861556Srgrimes				fmtstr(s, 64, "Exit %d", ps->status >> 8);
2871556Srgrimes			} else {
2881556Srgrimes				i = ps->status;
2891556Srgrimes#if JOBS
2901556Srgrimes				if ((i & 0xFF) == 0177)
2911556Srgrimes					i >>= 8;
2921556Srgrimes#endif
29317987Speter				if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
29417987Speter					scopy(sys_siglist[i & 0x7F], s);
2951556Srgrimes				else
2961556Srgrimes					fmtstr(s, 64, "Signal %d", i & 0x7F);
2971556Srgrimes				if (i & 0x80)
2981556Srgrimes					strcat(s, " (core dumped)");
2991556Srgrimes			}
3001556Srgrimes			out1str(s);
3011556Srgrimes			col += strlen(s);
3021556Srgrimes			do {
3031556Srgrimes				out1c(' ');
3041556Srgrimes				col++;
3051556Srgrimes			} while (col < 30);
3061556Srgrimes			out1str(ps->cmd);
3071556Srgrimes			out1c('\n');
3081556Srgrimes			if (--procno <= 0)
3091556Srgrimes				break;
3101556Srgrimes		}
3111556Srgrimes		jp->changed = 0;
3121556Srgrimes		if (jp->state == JOBDONE) {
3131556Srgrimes			freejob(jp);
3141556Srgrimes		}
3151556Srgrimes	}
3161556Srgrimes}
3171556Srgrimes
3181556Srgrimes
3191556Srgrimes/*
3201556Srgrimes * Mark a job structure as unused.
3211556Srgrimes */
3221556Srgrimes
3231556SrgrimesSTATIC void
3241556Srgrimesfreejob(jp)
3251556Srgrimes	struct job *jp;
3261556Srgrimes	{
3271556Srgrimes	struct procstat *ps;
3281556Srgrimes	int i;
3291556Srgrimes
3301556Srgrimes	INTOFF;
3311556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
3321556Srgrimes		if (ps->cmd != nullstr)
3331556Srgrimes			ckfree(ps->cmd);
3341556Srgrimes	}
3351556Srgrimes	if (jp->ps != &jp->ps0)
3361556Srgrimes		ckfree(jp->ps);
3371556Srgrimes	jp->used = 0;
3381556Srgrimes#if JOBS
3391556Srgrimes	if (curjob == jp - jobtab + 1)
3401556Srgrimes		curjob = 0;
3411556Srgrimes#endif
3421556Srgrimes	INTON;
3431556Srgrimes}
3441556Srgrimes
3451556Srgrimes
3461556Srgrimes
3471556Srgrimesint
34817987Speterwaitcmd(argc, argv)
34917987Speter	int argc;
35017987Speter	char **argv;
35117987Speter{
3521556Srgrimes	struct job *job;
3531556Srgrimes	int status;
3541556Srgrimes	struct job *jp;
3551556Srgrimes
3561556Srgrimes	if (argc > 1) {
3571556Srgrimes		job = getjob(argv[1]);
3581556Srgrimes	} else {
3591556Srgrimes		job = NULL;
3601556Srgrimes	}
3611556Srgrimes	for (;;) {	/* loop until process terminated or stopped */
3621556Srgrimes		if (job != NULL) {
3631556Srgrimes			if (job->state) {
3641556Srgrimes				status = job->ps[job->nprocs - 1].status;
3651556Srgrimes				if ((status & 0xFF) == 0)
3661556Srgrimes					status = status >> 8 & 0xFF;
3671556Srgrimes#if JOBS
3681556Srgrimes				else if ((status & 0xFF) == 0177)
3691556Srgrimes					status = (status >> 8 & 0x7F) + 128;
3701556Srgrimes#endif
3711556Srgrimes				else
3721556Srgrimes					status = (status & 0x7F) + 128;
3731556Srgrimes				if (! iflag)
3741556Srgrimes					freejob(job);
3751556Srgrimes				return status;
3761556Srgrimes			}
3771556Srgrimes		} else {
3781556Srgrimes			for (jp = jobtab ; ; jp++) {
3791556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
3801556Srgrimes					return 0;
3811556Srgrimes				}
3821556Srgrimes				if (jp->used && jp->state == 0)
3831556Srgrimes					break;
3841556Srgrimes			}
3851556Srgrimes		}
3861556Srgrimes		dowait(1, (struct job *)NULL);
3871556Srgrimes	}
3881556Srgrimes}
3891556Srgrimes
3901556Srgrimes
3911556Srgrimes
39217987Speterint
39317987Speterjobidcmd(argc, argv)
39417987Speter	int argc;
39517987Speter	char **argv;
39617987Speter{
3971556Srgrimes	struct job *jp;
3981556Srgrimes	int i;
3991556Srgrimes
4001556Srgrimes	jp = getjob(argv[1]);
4011556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
4021556Srgrimes		out1fmt("%d", jp->ps[i].pid);
4031556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
4041556Srgrimes	}
4051556Srgrimes	return 0;
4061556Srgrimes}
4071556Srgrimes
4081556Srgrimes
4091556Srgrimes
4101556Srgrimes/*
4111556Srgrimes * Convert a job name to a job structure.
4121556Srgrimes */
4131556Srgrimes
4141556SrgrimesSTATIC struct job *
4151556Srgrimesgetjob(name)
4161556Srgrimes	char *name;
4171556Srgrimes	{
4181556Srgrimes	int jobno;
4191556Srgrimes	register struct job *jp;
4201556Srgrimes	int pid;
4211556Srgrimes	int i;
4221556Srgrimes
4231556Srgrimes	if (name == NULL) {
4241556Srgrimes#if JOBS
4251556Srgrimescurrentjob:
4261556Srgrimes		if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0)
4271556Srgrimes			error("No current job");
4281556Srgrimes		return &jobtab[jobno - 1];
4291556Srgrimes#else
4301556Srgrimes		error("No current job");
4311556Srgrimes#endif
4321556Srgrimes	} else if (name[0] == '%') {
4331556Srgrimes		if (is_digit(name[1])) {
4341556Srgrimes			jobno = number(name + 1);
4351556Srgrimes			if (jobno > 0 && jobno <= njobs
4361556Srgrimes			 && jobtab[jobno - 1].used != 0)
4371556Srgrimes				return &jobtab[jobno - 1];
4381556Srgrimes#if JOBS
4391556Srgrimes		} else if (name[1] == '%' && name[2] == '\0') {
4401556Srgrimes			goto currentjob;
4411556Srgrimes#endif
4421556Srgrimes		} else {
4431556Srgrimes			register struct job *found = NULL;
4441556Srgrimes			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
4451556Srgrimes				if (jp->used && jp->nprocs > 0
4461556Srgrimes				 && prefix(name + 1, jp->ps[0].cmd)) {
4471556Srgrimes					if (found)
4481556Srgrimes						error("%s: ambiguous", name);
4491556Srgrimes					found = jp;
4501556Srgrimes				}
4511556Srgrimes			}
4521556Srgrimes			if (found)
4531556Srgrimes				return found;
4541556Srgrimes		}
4551556Srgrimes	} else if (is_number(name)) {
4561556Srgrimes		pid = number(name);
4571556Srgrimes		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
4581556Srgrimes			if (jp->used && jp->nprocs > 0
4591556Srgrimes			 && jp->ps[jp->nprocs - 1].pid == pid)
4601556Srgrimes				return jp;
4611556Srgrimes		}
4621556Srgrimes	}
4631556Srgrimes	error("No such job: %s", name);
46417987Speter	/*NOTREACHED*/
46517987Speter	return NULL;
4661556Srgrimes}
4671556Srgrimes
4681556Srgrimes
4691556Srgrimes
4701556Srgrimes/*
4711556Srgrimes * Return a new job structure,
4721556Srgrimes */
4731556Srgrimes
4741556Srgrimesstruct job *
4751556Srgrimesmakejob(node, nprocs)
4761556Srgrimes	union node *node;
47717987Speter	int nprocs;
47817987Speter{
4791556Srgrimes	int i;
4801556Srgrimes	struct job *jp;
4811556Srgrimes
4821556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
4831556Srgrimes		if (--i < 0) {
4841556Srgrimes			INTOFF;
4851556Srgrimes			if (njobs == 0) {
4861556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
4871556Srgrimes			} else {
48810934Sbde				struct job *ojp;
48910934Sbde
4901556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
49110934Sbde				for (i = njobs, ojp = jobtab; --i >= 0;
49210934Sbde				     jp++, ojp++)
49310934Sbde					if (ojp->ps == &ojp->ps0)
49410934Sbde						ojp->ps = &jp->ps0;
49510934Sbde				jp -= njobs;
49617987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
4971556Srgrimes				ckfree(jobtab);
4981556Srgrimes				jobtab = jp;
4991556Srgrimes			}
5001556Srgrimes			jp = jobtab + njobs;
5011556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
5021556Srgrimes			INTON;
5031556Srgrimes			break;
5041556Srgrimes		}
5051556Srgrimes		if (jp->used == 0)
5061556Srgrimes			break;
5071556Srgrimes	}
5081556Srgrimes	INTOFF;
5091556Srgrimes	jp->state = 0;
5101556Srgrimes	jp->used = 1;
5111556Srgrimes	jp->changed = 0;
5121556Srgrimes	jp->nprocs = 0;
5131556Srgrimes#if JOBS
5141556Srgrimes	jp->jobctl = jobctl;
5151556Srgrimes#endif
5161556Srgrimes	if (nprocs > 1) {
5171556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
5181556Srgrimes	} else {
5191556Srgrimes		jp->ps = &jp->ps0;
5201556Srgrimes	}
5211556Srgrimes	INTON;
52217987Speter	TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
52317987Speter	    jp - jobtab + 1));
5241556Srgrimes	return jp;
5258855Srgrimes}
5261556Srgrimes
5271556Srgrimes
5281556Srgrimes/*
5291556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
5301556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
5311556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
5321556Srgrimes * be NULL.  The mode parameter can be one of the following:
5331556Srgrimes *	FORK_FG - Fork off a foreground process.
5341556Srgrimes *	FORK_BG - Fork off a background process.
5351556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
5361556Srgrimes *		     process group even if job control is on.
5371556Srgrimes *
5381556Srgrimes * When job control is turned off, background processes have their standard
5391556Srgrimes * input redirected to /dev/null (except for the second and later processes
5401556Srgrimes * in a pipeline).
5411556Srgrimes */
5421556Srgrimes
5431556Srgrimesint
5441556Srgrimesforkshell(jp, n, mode)
5451556Srgrimes	union node *n;
5461556Srgrimes	struct job *jp;
54717987Speter	int mode;
54817987Speter{
5491556Srgrimes	int pid;
5501556Srgrimes	int pgrp;
5511556Srgrimes
55217987Speter	TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
55317987Speter	    mode));
5541556Srgrimes	INTOFF;
5551556Srgrimes	pid = fork();
5561556Srgrimes	if (pid == -1) {
5571556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
5581556Srgrimes		INTON;
5591556Srgrimes		error("Cannot fork");
5601556Srgrimes	}
5611556Srgrimes	if (pid == 0) {
5621556Srgrimes		struct job *p;
5631556Srgrimes		int wasroot;
5641556Srgrimes		int i;
5651556Srgrimes
5661556Srgrimes		TRACE(("Child shell %d\n", getpid()));
5671556Srgrimes		wasroot = rootshell;
5681556Srgrimes		rootshell = 0;
5691556Srgrimes		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
5701556Srgrimes			if (p->used)
5711556Srgrimes				freejob(p);
5721556Srgrimes		closescript();
5731556Srgrimes		INTON;
5741556Srgrimes		clear_traps();
5751556Srgrimes#if JOBS
5761556Srgrimes		jobctl = 0;		/* do job control only in root shell */
5771556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
5781556Srgrimes			if (jp == NULL || jp->nprocs == 0)
5791556Srgrimes				pgrp = getpid();
5801556Srgrimes			else
5811556Srgrimes				pgrp = jp->ps[0].pid;
58217987Speter			setpgid(0, pgrp);
5831556Srgrimes			if (mode == FORK_FG) {
5841556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
5851556Srgrimes				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
58618016Speter					error("TIOCSPGRP failed, errno=%d", errno);
5871556Srgrimes			}
5881556Srgrimes			setsignal(SIGTSTP);
5891556Srgrimes			setsignal(SIGTTOU);
5901556Srgrimes		} else if (mode == FORK_BG) {
5911556Srgrimes			ignoresig(SIGINT);
5921556Srgrimes			ignoresig(SIGQUIT);
5931556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
5941556Srgrimes			    ! fd0_redirected_p ()) {
5951556Srgrimes				close(0);
5961556Srgrimes				if (open("/dev/null", O_RDONLY) != 0)
5971556Srgrimes					error("Can't open /dev/null");
5981556Srgrimes			}
5991556Srgrimes		}
6001556Srgrimes#else
6011556Srgrimes		if (mode == FORK_BG) {
6021556Srgrimes			ignoresig(SIGINT);
6031556Srgrimes			ignoresig(SIGQUIT);
6041556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
6051556Srgrimes			    ! fd0_redirected_p ()) {
6061556Srgrimes				close(0);
6071556Srgrimes				if (open("/dev/null", O_RDONLY) != 0)
6081556Srgrimes					error("Can't open /dev/null");
6091556Srgrimes			}
6101556Srgrimes		}
6111556Srgrimes#endif
6121556Srgrimes		if (wasroot && iflag) {
6131556Srgrimes			setsignal(SIGINT);
6141556Srgrimes			setsignal(SIGQUIT);
6151556Srgrimes			setsignal(SIGTERM);
6161556Srgrimes		}
6171556Srgrimes		return pid;
6181556Srgrimes	}
6191556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
6201556Srgrimes		if (jp == NULL || jp->nprocs == 0)
6211556Srgrimes			pgrp = pid;
6221556Srgrimes		else
6231556Srgrimes			pgrp = jp->ps[0].pid;
62417987Speter		setpgid(pid, pgrp);
6251556Srgrimes	}
6261556Srgrimes	if (mode == FORK_BG)
6271556Srgrimes		backgndpid = pid;		/* set $! */
6281556Srgrimes	if (jp) {
6291556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
6301556Srgrimes		ps->pid = pid;
6311556Srgrimes		ps->status = -1;
6321556Srgrimes		ps->cmd = nullstr;
6331556Srgrimes		if (iflag && rootshell && n)
6341556Srgrimes			ps->cmd = commandtext(n);
6351556Srgrimes	}
6361556Srgrimes	INTON;
6371556Srgrimes	TRACE(("In parent shell:  child = %d\n", pid));
6381556Srgrimes	return pid;
6391556Srgrimes}
6401556Srgrimes
6411556Srgrimes
6421556Srgrimes
6431556Srgrimes/*
6441556Srgrimes * Wait for job to finish.
6451556Srgrimes *
6461556Srgrimes * Under job control we have the problem that while a child process is
6471556Srgrimes * running interrupts generated by the user are sent to the child but not
6481556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
6491556Srgrimes * active user may be hard to kill.  With job control turned off, an
6501556Srgrimes * interactive user may place an interactive program inside a loop.  If
6511556Srgrimes * the interactive program catches interrupts, the user doesn't want
6521556Srgrimes * these interrupts to also abort the loop.  The approach we take here
6531556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
6541556Srgrimes * forground process to terminate, and then send itself an interrupt
6551556Srgrimes * signal if the child process was terminated by an interrupt signal.
6561556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
6571556Srgrimes * exit on interrupt; unless these processes terminate themselves by
6581556Srgrimes * sending a signal to themselves (instead of calling exit) they will
6591556Srgrimes * confuse this approach.
6601556Srgrimes */
6611556Srgrimes
6621556Srgrimesint
6631556Srgrimeswaitforjob(jp)
6641556Srgrimes	register struct job *jp;
6651556Srgrimes	{
6661556Srgrimes#if JOBS
66717987Speter	int mypgrp = getpgrp();
6681556Srgrimes#endif
6691556Srgrimes	int status;
6701556Srgrimes	int st;
6711556Srgrimes
6721556Srgrimes	INTOFF;
6731556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
6741556Srgrimes	while (jp->state == 0) {
6751556Srgrimes		dowait(1, jp);
6761556Srgrimes	}
6771556Srgrimes#if JOBS
6781556Srgrimes	if (jp->jobctl) {
6791556Srgrimes		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
68018016Speter			error("TIOCSPGRP failed, errno=%d", errno);
6811556Srgrimes	}
6821556Srgrimes	if (jp->state == JOBSTOPPED)
6831556Srgrimes		curjob = jp - jobtab + 1;
6841556Srgrimes#endif
6851556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
6861556Srgrimes	/* convert to 8 bits */
6871556Srgrimes	if ((status & 0xFF) == 0)
6881556Srgrimes		st = status >> 8 & 0xFF;
6891556Srgrimes#if JOBS
6901556Srgrimes	else if ((status & 0xFF) == 0177)
6911556Srgrimes		st = (status >> 8 & 0x7F) + 128;
6921556Srgrimes#endif
6931556Srgrimes	else
6941556Srgrimes		st = (status & 0x7F) + 128;
6951556Srgrimes	if (! JOBS || jp->state == JOBDONE)
6961556Srgrimes		freejob(jp);
6971556Srgrimes	CLEAR_PENDING_INT;
6981556Srgrimes	if ((status & 0x7F) == SIGINT)
6991556Srgrimes		kill(getpid(), SIGINT);
7001556Srgrimes	INTON;
7011556Srgrimes	return st;
7021556Srgrimes}
7031556Srgrimes
7041556Srgrimes
7051556Srgrimes
7061556Srgrimes/*
7071556Srgrimes * Wait for a process to terminate.
7081556Srgrimes */
7091556Srgrimes
7101556SrgrimesSTATIC int
7111556Srgrimesdowait(block, job)
71217987Speter	int block;
7131556Srgrimes	struct job *job;
71417987Speter{
7151556Srgrimes	int pid;
7161556Srgrimes	int status;
7171556Srgrimes	struct procstat *sp;
7181556Srgrimes	struct job *jp;
7191556Srgrimes	struct job *thisjob;
7201556Srgrimes	int done;
7211556Srgrimes	int stopped;
7221556Srgrimes	int core;
7231556Srgrimes
7241556Srgrimes	TRACE(("dowait(%d) called\n", block));
7251556Srgrimes	do {
7261556Srgrimes		pid = waitproc(block, &status);
7271556Srgrimes		TRACE(("wait returns %d, status=%d\n", pid, status));
7281556Srgrimes	} while (pid == -1 && errno == EINTR);
7291556Srgrimes	if (pid <= 0)
7301556Srgrimes		return pid;
7311556Srgrimes	INTOFF;
7321556Srgrimes	thisjob = NULL;
7331556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
7341556Srgrimes		if (jp->used) {
7351556Srgrimes			done = 1;
7361556Srgrimes			stopped = 1;
7371556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
7381556Srgrimes				if (sp->pid == -1)
7391556Srgrimes					continue;
7401556Srgrimes				if (sp->pid == pid) {
7411556Srgrimes					TRACE(("Changin status of proc %d from 0x%x to 0x%x\n", pid, sp->status, status));
7421556Srgrimes					sp->status = status;
7431556Srgrimes					thisjob = jp;
7441556Srgrimes				}
7451556Srgrimes				if (sp->status == -1)
7461556Srgrimes					stopped = 0;
7471556Srgrimes				else if ((sp->status & 0377) == 0177)
7481556Srgrimes					done = 0;
7491556Srgrimes			}
7501556Srgrimes			if (stopped) {		/* stopped or done */
7511556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
7521556Srgrimes				if (jp->state != state) {
7531556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
7541556Srgrimes					jp->state = state;
7551556Srgrimes#if JOBS
7561556Srgrimes					if (done && curjob == jp - jobtab + 1)
7571556Srgrimes						curjob = 0;		/* no current job */
7581556Srgrimes#endif
7591556Srgrimes				}
7601556Srgrimes			}
7611556Srgrimes		}
7621556Srgrimes	}
7631556Srgrimes	INTON;
7641556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
7651556Srgrimes#if JOBS
7661556Srgrimes		if ((status & 0xFF) == 0177)
7671556Srgrimes			status >>= 8;
7681556Srgrimes#endif
7691556Srgrimes		core = status & 0x80;
7701556Srgrimes		status &= 0x7F;
7711556Srgrimes		if (status != 0 && status != SIGINT && status != SIGPIPE) {
7721556Srgrimes			if (thisjob != job)
7731556Srgrimes				outfmt(out2, "%d: ", pid);
7741556Srgrimes#if JOBS
7751556Srgrimes			if (status == SIGTSTP && rootshell && iflag)
7761556Srgrimes				outfmt(out2, "%%%d ", job - jobtab + 1);
7771556Srgrimes#endif
77817987Speter			if (status < NSIG && sys_siglist[status])
77917987Speter				out2str(sys_siglist[status]);
7801556Srgrimes			else
7811556Srgrimes				outfmt(out2, "Signal %d", status);
7821556Srgrimes			if (core)
7831556Srgrimes				out2str(" - core dumped");
7841556Srgrimes			out2c('\n');
7851556Srgrimes			flushout(&errout);
7861556Srgrimes		} else {
7871556Srgrimes			TRACE(("Not printing status: status=%d\n", status));
7881556Srgrimes		}
7891556Srgrimes	} else {
7901556Srgrimes		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
7911556Srgrimes		if (thisjob)
7921556Srgrimes			thisjob->changed = 1;
7931556Srgrimes	}
7941556Srgrimes	return pid;
7951556Srgrimes}
7961556Srgrimes
7971556Srgrimes
7981556Srgrimes
7991556Srgrimes/*
8001556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
8011556Srgrimes * stopped processes.  If block is zero, we return a value of zero
8021556Srgrimes * rather than blocking.
8031556Srgrimes *
8041556Srgrimes * System V doesn't have a non-blocking wait system call.  It does
8051556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's
8061556Srgrimes * children dies.  The obvious way to use SIGCLD would be to install
8071556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD
8081556Srgrimes * was received, and have waitproc bump another counter when it got
8091556Srgrimes * the status of a process.  Waitproc would then know that a wait
8101556Srgrimes * system call would not block if the two counters were different.
8111556Srgrimes * This approach doesn't work because if a process has children that
8121556Srgrimes * have not been waited for, System V will send it a SIGCLD when it
8131556Srgrimes * installs a signal handler for SIGCLD.  What this means is that when
8141556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously
8151556Srgrimes * until is runs out of stack space, unless it does a wait call before
8161556Srgrimes * restoring the signal handler.  The code below takes advantage of
8171556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and
8181556Srgrimes * then checking to see whether it was called.  If there are any
8191556Srgrimes * children to be waited for, it will be.
8201556Srgrimes *
8211556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking
8221556Srgrimes * waits at all.  In this case, the user will not be informed when
8231556Srgrimes * a background process until the next time she runs a real program
8241556Srgrimes * (as opposed to running a builtin command or just typing return),
8251556Srgrimes * and the jobs command may give out of date information.
8261556Srgrimes */
8271556Srgrimes
8281556Srgrimes#ifdef SYSV
8291556SrgrimesSTATIC int gotsigchild;
8301556Srgrimes
8311556SrgrimesSTATIC int onsigchild() {
8321556Srgrimes	gotsigchild = 1;
8331556Srgrimes}
8341556Srgrimes#endif
8351556Srgrimes
8361556Srgrimes
8371556SrgrimesSTATIC int
8381556Srgrimeswaitproc(block, status)
83917987Speter	int block;
8401556Srgrimes	int *status;
84117987Speter{
8421556Srgrimes#ifdef BSD
8431556Srgrimes	int flags;
8441556Srgrimes
8451556Srgrimes#if JOBS
8461556Srgrimes	flags = WUNTRACED;
8471556Srgrimes#else
8481556Srgrimes	flags = 0;
8491556Srgrimes#endif
8501556Srgrimes	if (block == 0)
8511556Srgrimes		flags |= WNOHANG;
8521556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
8531556Srgrimes#else
8541556Srgrimes#ifdef SYSV
8551556Srgrimes	int (*save)();
8561556Srgrimes
8571556Srgrimes	if (block == 0) {
8581556Srgrimes		gotsigchild = 0;
8591556Srgrimes		save = signal(SIGCLD, onsigchild);
8601556Srgrimes		signal(SIGCLD, save);
8611556Srgrimes		if (gotsigchild == 0)
8621556Srgrimes			return 0;
8631556Srgrimes	}
8641556Srgrimes	return wait(status);
8651556Srgrimes#else
8661556Srgrimes	if (block == 0)
8671556Srgrimes		return 0;
8681556Srgrimes	return wait(status);
8691556Srgrimes#endif
8701556Srgrimes#endif
8711556Srgrimes}
8721556Srgrimes
8731556Srgrimes/*
8741556Srgrimes * return 1 if there are stopped jobs, otherwise 0
8751556Srgrimes */
8761556Srgrimesint job_warning = 0;
8771556Srgrimesint
8781556Srgrimesstoppedjobs()
8791556Srgrimes{
8801556Srgrimes	register int jobno;
8811556Srgrimes	register struct job *jp;
8821556Srgrimes
8831556Srgrimes	if (job_warning)
8841556Srgrimes		return (0);
8851556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
8861556Srgrimes		if (jp->used == 0)
8871556Srgrimes			continue;
8881556Srgrimes		if (jp->state == JOBSTOPPED) {
8891556Srgrimes			out2str("You have stopped jobs.\n");
8901556Srgrimes			job_warning = 2;
8911556Srgrimes			return (1);
8921556Srgrimes		}
8931556Srgrimes	}
8941556Srgrimes
8951556Srgrimes	return (0);
8961556Srgrimes}
8971556Srgrimes
8981556Srgrimes/*
8991556Srgrimes * Return a string identifying a command (to be printed by the
9001556Srgrimes * jobs command.
9011556Srgrimes */
9021556Srgrimes
9031556SrgrimesSTATIC char *cmdnextc;
9041556SrgrimesSTATIC int cmdnleft;
9051556SrgrimesSTATIC void cmdtxt(), cmdputs();
9061556Srgrimes#define MAXCMDTEXT	200
9071556Srgrimes
9081556Srgrimeschar *
9091556Srgrimescommandtext(n)
9101556Srgrimes	union node *n;
9111556Srgrimes	{
9121556Srgrimes	char *name;
9131556Srgrimes
9141556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
9151556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
9161556Srgrimes	cmdtxt(n);
9171556Srgrimes	*cmdnextc = '\0';
9181556Srgrimes	return name;
9191556Srgrimes}
9201556Srgrimes
9211556Srgrimes
9221556SrgrimesSTATIC void
9231556Srgrimescmdtxt(n)
9241556Srgrimes	union node *n;
9251556Srgrimes	{
9261556Srgrimes	union node *np;
9271556Srgrimes	struct nodelist *lp;
9281556Srgrimes	char *p;
9291556Srgrimes	int i;
9301556Srgrimes	char s[2];
9311556Srgrimes
9321556Srgrimes	if (n == NULL)
9331556Srgrimes		return;
9341556Srgrimes	switch (n->type) {
9351556Srgrimes	case NSEMI:
9361556Srgrimes		cmdtxt(n->nbinary.ch1);
9371556Srgrimes		cmdputs("; ");
9381556Srgrimes		cmdtxt(n->nbinary.ch2);
9391556Srgrimes		break;
9401556Srgrimes	case NAND:
9411556Srgrimes		cmdtxt(n->nbinary.ch1);
9421556Srgrimes		cmdputs(" && ");
9431556Srgrimes		cmdtxt(n->nbinary.ch2);
9441556Srgrimes		break;
9451556Srgrimes	case NOR:
9461556Srgrimes		cmdtxt(n->nbinary.ch1);
9471556Srgrimes		cmdputs(" || ");
9481556Srgrimes		cmdtxt(n->nbinary.ch2);
9491556Srgrimes		break;
9501556Srgrimes	case NPIPE:
9511556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
9521556Srgrimes			cmdtxt(lp->n);
9531556Srgrimes			if (lp->next)
9541556Srgrimes				cmdputs(" | ");
9551556Srgrimes		}
9561556Srgrimes		break;
9571556Srgrimes	case NSUBSHELL:
9581556Srgrimes		cmdputs("(");
9591556Srgrimes		cmdtxt(n->nredir.n);
9601556Srgrimes		cmdputs(")");
9611556Srgrimes		break;
9621556Srgrimes	case NREDIR:
9631556Srgrimes	case NBACKGND:
9641556Srgrimes		cmdtxt(n->nredir.n);
9651556Srgrimes		break;
9661556Srgrimes	case NIF:
9671556Srgrimes		cmdputs("if ");
9681556Srgrimes		cmdtxt(n->nif.test);
9691556Srgrimes		cmdputs("; then ");
9701556Srgrimes		cmdtxt(n->nif.ifpart);
9711556Srgrimes		cmdputs("...");
9721556Srgrimes		break;
9731556Srgrimes	case NWHILE:
9741556Srgrimes		cmdputs("while ");
9751556Srgrimes		goto until;
9761556Srgrimes	case NUNTIL:
9771556Srgrimes		cmdputs("until ");
9781556Srgrimesuntil:
9791556Srgrimes		cmdtxt(n->nbinary.ch1);
9801556Srgrimes		cmdputs("; do ");
9811556Srgrimes		cmdtxt(n->nbinary.ch2);
9821556Srgrimes		cmdputs("; done");
9831556Srgrimes		break;
9841556Srgrimes	case NFOR:
9851556Srgrimes		cmdputs("for ");
9861556Srgrimes		cmdputs(n->nfor.var);
9871556Srgrimes		cmdputs(" in ...");
9881556Srgrimes		break;
9891556Srgrimes	case NCASE:
9901556Srgrimes		cmdputs("case ");
9911556Srgrimes		cmdputs(n->ncase.expr->narg.text);
9921556Srgrimes		cmdputs(" in ...");
9931556Srgrimes		break;
9941556Srgrimes	case NDEFUN:
9951556Srgrimes		cmdputs(n->narg.text);
9961556Srgrimes		cmdputs("() ...");
9971556Srgrimes		break;
9981556Srgrimes	case NCMD:
9991556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
10001556Srgrimes			cmdtxt(np);
10011556Srgrimes			if (np->narg.next)
10021556Srgrimes				cmdputs(" ");
10031556Srgrimes		}
10041556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
10051556Srgrimes			cmdputs(" ");
10061556Srgrimes			cmdtxt(np);
10071556Srgrimes		}
10081556Srgrimes		break;
10091556Srgrimes	case NARG:
10101556Srgrimes		cmdputs(n->narg.text);
10111556Srgrimes		break;
10121556Srgrimes	case NTO:
10131556Srgrimes		p = ">";  i = 1;  goto redir;
10141556Srgrimes	case NAPPEND:
10151556Srgrimes		p = ">>";  i = 1;  goto redir;
10161556Srgrimes	case NTOFD:
10171556Srgrimes		p = ">&";  i = 1;  goto redir;
10181556Srgrimes	case NFROM:
10191556Srgrimes		p = "<";  i = 0;  goto redir;
10201556Srgrimes	case NFROMFD:
10211556Srgrimes		p = "<&";  i = 0;  goto redir;
10221556Srgrimesredir:
10231556Srgrimes		if (n->nfile.fd != i) {
10241556Srgrimes			s[0] = n->nfile.fd + '0';
10251556Srgrimes			s[1] = '\0';
10261556Srgrimes			cmdputs(s);
10271556Srgrimes		}
10281556Srgrimes		cmdputs(p);
10291556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
10301556Srgrimes			s[0] = n->ndup.dupfd + '0';
10311556Srgrimes			s[1] = '\0';
10321556Srgrimes			cmdputs(s);
10331556Srgrimes		} else {
10341556Srgrimes			cmdtxt(n->nfile.fname);
10351556Srgrimes		}
10361556Srgrimes		break;
10371556Srgrimes	case NHERE:
10381556Srgrimes	case NXHERE:
10391556Srgrimes		cmdputs("<<...");
10401556Srgrimes		break;
10411556Srgrimes	default:
10421556Srgrimes		cmdputs("???");
10431556Srgrimes		break;
10441556Srgrimes	}
10451556Srgrimes}
10461556Srgrimes
10471556Srgrimes
10481556Srgrimes
10491556SrgrimesSTATIC void
10501556Srgrimescmdputs(s)
10511556Srgrimes	char *s;
10521556Srgrimes	{
10531556Srgrimes	register char *p, *q;
10541556Srgrimes	register char c;
10551556Srgrimes	int subtype = 0;
10561556Srgrimes
10571556Srgrimes	if (cmdnleft <= 0)
10581556Srgrimes		return;
10591556Srgrimes	p = s;
10601556Srgrimes	q = cmdnextc;
10611556Srgrimes	while ((c = *p++) != '\0') {
10621556Srgrimes		if (c == CTLESC)
10631556Srgrimes			*q++ = *p++;
10641556Srgrimes		else if (c == CTLVAR) {
10651556Srgrimes			*q++ = '$';
10661556Srgrimes			if (--cmdnleft > 0)
10671556Srgrimes				*q++ = '{';
10681556Srgrimes			subtype = *p++;
10691556Srgrimes		} else if (c == '=' && subtype != 0) {
10701556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
10711556Srgrimes			subtype = 0;
10721556Srgrimes		} else if (c == CTLENDVAR) {
10731556Srgrimes			*q++ = '}';
10741556Srgrimes		} else if (c == CTLBACKQ | c == CTLBACKQ+CTLQUOTE)
10751556Srgrimes			cmdnleft++;		/* ignore it */
10761556Srgrimes		else
10771556Srgrimes			*q++ = c;
10781556Srgrimes		if (--cmdnleft <= 0) {
10791556Srgrimes			*q++ = '.';
10801556Srgrimes			*q++ = '.';
10811556Srgrimes			*q++ = '.';
10821556Srgrimes			break;
10831556Srgrimes		}
10841556Srgrimes	}
10851556Srgrimes	cmdnextc = q;
10861556Srgrimes}
1087