jobs.c revision 97659
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 97659 2002-05-31 12:31:23Z tjr $";
431556Srgrimes#endif /* not lint */
441556Srgrimes
4517987Speter#include <fcntl.h>
4617987Speter#include <signal.h>
4717987Speter#include <errno.h>
4817987Speter#include <unistd.h>
4917987Speter#include <stdlib.h>
5017987Speter#include <sys/param.h>
5117987Speter#ifdef BSD
5217987Speter#include <sys/wait.h>
5317987Speter#include <sys/time.h>
5417987Speter#include <sys/resource.h>
5569793Sobrien#include <paths.h>
5617987Speter#endif
5718018Speter#include <sys/ioctl.h>
5817987Speter
591556Srgrimes#include "shell.h"
601556Srgrimes#if JOBS
6125222Ssteve#if OLD_TTY_DRIVER
621556Srgrimes#include "sgtty.h"
6317987Speter#else
6417987Speter#include <termios.h>
6517987Speter#endif
661556Srgrimes#undef CEOF			/* syntax.h redefines this */
671556Srgrimes#endif
6817987Speter#include "redir.h"
6917987Speter#include "show.h"
701556Srgrimes#include "main.h"
711556Srgrimes#include "parser.h"
721556Srgrimes#include "nodes.h"
731556Srgrimes#include "jobs.h"
741556Srgrimes#include "options.h"
751556Srgrimes#include "trap.h"
761556Srgrimes#include "syntax.h"
771556Srgrimes#include "input.h"
781556Srgrimes#include "output.h"
791556Srgrimes#include "memalloc.h"
801556Srgrimes#include "error.h"
811556Srgrimes#include "mystring.h"
821556Srgrimes
831556Srgrimes
841556Srgrimesstruct job *jobtab;		/* array of jobs */
851556Srgrimesint njobs;			/* size of array */
8628346SsteveMKINIT pid_t backgndpid = -1;	/* pid of last background process */
871556Srgrimes#if JOBS
8897659Stjrstruct job *jobmru;		/* most recently used job list */
891556Srgrimesint initialpgrp;		/* pgrp of shell on invocation */
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 *);
10797659Stjr#if JOBS
10897659StjrSTATIC void setcurjob(struct job *);
10997659StjrSTATIC void deljob(struct job *);
11097659StjrSTATIC struct job *getcurjob(struct job *);
11197659Stjr#endif
1121556Srgrimes
1131556Srgrimes
1141556Srgrimes/*
1151556Srgrimes * Turn job control on and off.
1161556Srgrimes *
1171556Srgrimes * Note:  This code assumes that the third arg to ioctl is a character
1181556Srgrimes * pointer, which is true on Berkeley systems but not System V.  Since
1191556Srgrimes * System V doesn't have job control yet, this isn't a problem now.
1201556Srgrimes */
1211556Srgrimes
1221556SrgrimesMKINIT int jobctl;
1231556Srgrimes
12420425Ssteve#if JOBS
1251556Srgrimesvoid
12690111Simpsetjobctl(int on)
12717987Speter{
1281556Srgrimes#ifdef OLD_TTY_DRIVER
1291556Srgrimes	int ldisc;
1301556Srgrimes#endif
1311556Srgrimes
1321556Srgrimes	if (on == jobctl || rootshell == 0)
1331556Srgrimes		return;
1341556Srgrimes	if (on) {
1351556Srgrimes		do { /* while we are in the background */
13620425Ssteve#ifdef OLD_TTY_DRIVER
1371556Srgrimes			if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) {
13820425Ssteve#else
13920425Ssteve			initialpgrp = tcgetpgrp(2);
14020425Ssteve			if (initialpgrp < 0) {
14120425Ssteve#endif
1421556Srgrimes				out2str("sh: can't access tty; job control turned off\n");
1431556Srgrimes				mflag = 0;
1441556Srgrimes				return;
1451556Srgrimes			}
1461556Srgrimes			if (initialpgrp == -1)
14717987Speter				initialpgrp = getpgrp();
14817987Speter			else if (initialpgrp != getpgrp()) {
1491556Srgrimes				killpg(initialpgrp, SIGTTIN);
1501556Srgrimes				continue;
1511556Srgrimes			}
1521556Srgrimes		} while (0);
1531556Srgrimes#ifdef OLD_TTY_DRIVER
1541556Srgrimes		if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) {
1551556Srgrimes			out2str("sh: need new tty driver to run job control; job control turned off\n");
1561556Srgrimes			mflag = 0;
1571556Srgrimes			return;
1581556Srgrimes		}
1591556Srgrimes#endif
1601556Srgrimes		setsignal(SIGTSTP);
1611556Srgrimes		setsignal(SIGTTOU);
1621556Srgrimes		setsignal(SIGTTIN);
16317987Speter		setpgid(0, rootpid);
16420425Ssteve#ifdef OLD_TTY_DRIVER
1651556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&rootpid);
16620425Ssteve#else
16720425Ssteve		tcsetpgrp(2, rootpid);
16820425Ssteve#endif
1691556Srgrimes	} else { /* turning job control off */
17017987Speter		setpgid(0, initialpgrp);
17120425Ssteve#ifdef OLD_TTY_DRIVER
1721556Srgrimes		ioctl(2, TIOCSPGRP, (char *)&initialpgrp);
17320425Ssteve#else
17420425Ssteve		tcsetpgrp(2, initialpgrp);
17520425Ssteve#endif
1761556Srgrimes		setsignal(SIGTSTP);
1771556Srgrimes		setsignal(SIGTTOU);
1781556Srgrimes		setsignal(SIGTTIN);
1791556Srgrimes	}
1801556Srgrimes	jobctl = on;
1811556Srgrimes}
18220425Ssteve#endif
1831556Srgrimes
1841556Srgrimes
1851556Srgrimes#ifdef mkinit
18628346SsteveINCLUDE <sys/types.h>
18717987SpeterINCLUDE <stdlib.h>
1881556Srgrimes
1891556SrgrimesSHELLPROC {
1901556Srgrimes	backgndpid = -1;
1911556Srgrimes#if JOBS
1921556Srgrimes	jobctl = 0;
1931556Srgrimes#endif
1941556Srgrimes}
1951556Srgrimes
1961556Srgrimes#endif
1971556Srgrimes
1981556Srgrimes
1991556Srgrimes
2001556Srgrimes#if JOBS
20117987Speterint
20290111Simpfgcmd(int argc __unused, char **argv)
20317987Speter{
2041556Srgrimes	struct job *jp;
2051556Srgrimes	int pgrp;
2061556Srgrimes	int status;
2071556Srgrimes
2081556Srgrimes	jp = getjob(argv[1]);
2091556Srgrimes	if (jp->jobctl == 0)
2101556Srgrimes		error("job not created under job control");
21196933Stjr	out1str(jp->ps[0].cmd);
21296933Stjr	out1c('\n');
21396933Stjr	flushout(&output);
2141556Srgrimes	pgrp = jp->ps[0].pid;
21520425Ssteve#ifdef OLD_TTY_DRIVER
2161556Srgrimes	ioctl(2, TIOCSPGRP, (char *)&pgrp);
21720425Ssteve#else
21820425Ssteve	tcsetpgrp(2, pgrp);
21920425Ssteve#endif
2201556Srgrimes	restartjob(jp);
2211556Srgrimes	INTOFF;
22245916Scracauer	status = waitforjob(jp, (int *)NULL);
2231556Srgrimes	INTON;
2241556Srgrimes	return status;
2251556Srgrimes}
2261556Srgrimes
2271556Srgrimes
22817987Speterint
22990111Simpbgcmd(int argc, char **argv)
23017987Speter{
23196933Stjr	char s[64];
2321556Srgrimes	struct job *jp;
2331556Srgrimes
2341556Srgrimes	do {
2351556Srgrimes		jp = getjob(*++argv);
2361556Srgrimes		if (jp->jobctl == 0)
2371556Srgrimes			error("job not created under job control");
23896933Stjr		if (jp->state == JOBDONE)
23996933Stjr			continue;
2401556Srgrimes		restartjob(jp);
24196933Stjr		fmtstr(s, 64, "[%d] ", jp - jobtab + 1);
24296933Stjr		out1str(s);
24396933Stjr		out1str(jp->ps[0].cmd);
24496933Stjr		out1c('\n');
2451556Srgrimes	} while (--argc > 1);
2461556Srgrimes	return 0;
2471556Srgrimes}
2481556Srgrimes
2491556Srgrimes
2501556SrgrimesSTATIC void
25190111Simprestartjob(struct job *jp)
25217987Speter{
2531556Srgrimes	struct procstat *ps;
2541556Srgrimes	int i;
2551556Srgrimes
2561556Srgrimes	if (jp->state == JOBDONE)
2571556Srgrimes		return;
2581556Srgrimes	INTOFF;
2591556Srgrimes	killpg(jp->ps[0].pid, SIGCONT);
2601556Srgrimes	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
26126104Ssteve		if (WIFSTOPPED(ps->status)) {
2621556Srgrimes			ps->status = -1;
2631556Srgrimes			jp->state = 0;
2641556Srgrimes		}
2651556Srgrimes	}
2661556Srgrimes	INTON;
2671556Srgrimes}
2681556Srgrimes#endif
2691556Srgrimes
2701556Srgrimes
2711556Srgrimesint
27290111Simpjobscmd(int argc __unused, char **argv __unused)
27317987Speter{
2741556Srgrimes	showjobs(0);
2751556Srgrimes	return 0;
2761556Srgrimes}
2771556Srgrimes
2781556Srgrimes
2791556Srgrimes/*
2801556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
2811556Srgrimes * statuses have changed since the last call to showjobs.
2821556Srgrimes *
2831556Srgrimes * If the shell is interrupted in the process of creating a job, the
2841556Srgrimes * result may be a job structure containing zero processes.  Such structures
2851556Srgrimes * will be freed here.
2861556Srgrimes */
2871556Srgrimes
2881556Srgrimesvoid
28990111Simpshowjobs(int change)
29017987Speter{
2911556Srgrimes	int jobno;
2921556Srgrimes	int procno;
2931556Srgrimes	int i;
2941556Srgrimes	struct job *jp;
2951556Srgrimes	struct procstat *ps;
2961556Srgrimes	int col;
2971556Srgrimes	char s[64];
2981556Srgrimes
2991556Srgrimes	TRACE(("showjobs(%d) called\n", change));
3001556Srgrimes	while (dowait(0, (struct job *)NULL) > 0);
3011556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
3021556Srgrimes		if (! jp->used)
3031556Srgrimes			continue;
3041556Srgrimes		if (jp->nprocs == 0) {
3051556Srgrimes			freejob(jp);
3061556Srgrimes			continue;
3071556Srgrimes		}
3081556Srgrimes		if (change && ! jp->changed)
3091556Srgrimes			continue;
3101556Srgrimes		procno = jp->nprocs;
3111556Srgrimes		for (ps = jp->ps ; ; ps++) {	/* for each process */
3121556Srgrimes			if (ps == jp->ps)
3131556Srgrimes				fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
3141556Srgrimes			else
3151556Srgrimes				fmtstr(s, 64, "    %d ", ps->pid);
3161556Srgrimes			out1str(s);
3171556Srgrimes			col = strlen(s);
3181556Srgrimes			s[0] = '\0';
3191556Srgrimes			if (ps->status == -1) {
3201556Srgrimes				/* don't print anything */
32126104Ssteve			} else if (WIFEXITED(ps->status)) {
32226104Ssteve				fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status));
3231556Srgrimes			} else {
3241556Srgrimes#if JOBS
32526104Ssteve				if (WIFSTOPPED(ps->status))
32626104Ssteve					i = WSTOPSIG(ps->status);
32726104Ssteve				else
3281556Srgrimes#endif
32926104Ssteve					i = WTERMSIG(ps->status);
33017987Speter				if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
33117987Speter					scopy(sys_siglist[i & 0x7F], s);
3321556Srgrimes				else
3331556Srgrimes					fmtstr(s, 64, "Signal %d", i & 0x7F);
33426104Ssteve				if (WCOREDUMP(ps->status))
3351556Srgrimes					strcat(s, " (core dumped)");
3361556Srgrimes			}
3371556Srgrimes			out1str(s);
3381556Srgrimes			col += strlen(s);
3391556Srgrimes			do {
3401556Srgrimes				out1c(' ');
3411556Srgrimes				col++;
3421556Srgrimes			} while (col < 30);
3431556Srgrimes			out1str(ps->cmd);
3441556Srgrimes			out1c('\n');
3451556Srgrimes			if (--procno <= 0)
3461556Srgrimes				break;
3471556Srgrimes		}
3481556Srgrimes		jp->changed = 0;
3491556Srgrimes		if (jp->state == JOBDONE) {
3501556Srgrimes			freejob(jp);
3511556Srgrimes		}
3521556Srgrimes	}
3531556Srgrimes}
3541556Srgrimes
3551556Srgrimes
3561556Srgrimes/*
3571556Srgrimes * Mark a job structure as unused.
3581556Srgrimes */
3591556Srgrimes
3601556SrgrimesSTATIC void
36190111Simpfreejob(struct job *jp)
36290111Simp{
3631556Srgrimes	struct procstat *ps;
3641556Srgrimes	int i;
3651556Srgrimes
3661556Srgrimes	INTOFF;
3671556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
3681556Srgrimes		if (ps->cmd != nullstr)
3691556Srgrimes			ckfree(ps->cmd);
3701556Srgrimes	}
3711556Srgrimes	if (jp->ps != &jp->ps0)
3721556Srgrimes		ckfree(jp->ps);
3731556Srgrimes	jp->used = 0;
3741556Srgrimes#if JOBS
37597659Stjr	deljob(jp);
3761556Srgrimes#endif
3771556Srgrimes	INTON;
3781556Srgrimes}
3791556Srgrimes
3801556Srgrimes
3811556Srgrimes
3821556Srgrimesint
38390111Simpwaitcmd(int argc, char **argv)
38417987Speter{
3851556Srgrimes	struct job *job;
38626104Ssteve	int status, retval;
3871556Srgrimes	struct job *jp;
3881556Srgrimes
3891556Srgrimes	if (argc > 1) {
3901556Srgrimes		job = getjob(argv[1]);
3911556Srgrimes	} else {
3921556Srgrimes		job = NULL;
3931556Srgrimes	}
39438536Scracauer
39538536Scracauer	/*
39638536Scracauer	 * Loop until a process is terminated or stopped, or a SIGINT is
39738536Scracauer	 * received.
39838536Scracauer	 */
39938536Scracauer
40038521Scracauer	in_waitcmd++;
40138536Scracauer	do {
4021556Srgrimes		if (job != NULL) {
4031556Srgrimes			if (job->state) {
4041556Srgrimes				status = job->ps[job->nprocs - 1].status;
40526104Ssteve				if (WIFEXITED(status))
40626104Ssteve					retval = WEXITSTATUS(status);
4071556Srgrimes#if JOBS
40826104Ssteve				else if (WIFSTOPPED(status))
40926104Ssteve					retval = WSTOPSIG(status) + 128;
4101556Srgrimes#endif
4111556Srgrimes				else
41226104Ssteve					retval = WTERMSIG(status) + 128;
4131556Srgrimes				if (! iflag)
4141556Srgrimes					freejob(job);
41538536Scracauer				in_waitcmd--;
41626104Ssteve				return retval;
4171556Srgrimes			}
4181556Srgrimes		} else {
4191556Srgrimes			for (jp = jobtab ; ; jp++) {
4201556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
42138536Scracauer					in_waitcmd--;
4221556Srgrimes					return 0;
4231556Srgrimes				}
4241556Srgrimes				if (jp->used && jp->state == 0)
4251556Srgrimes					break;
4261556Srgrimes			}
4271556Srgrimes		}
42838521Scracauer	} while (dowait(1, (struct job *)NULL) != -1);
42938521Scracauer	in_waitcmd--;
43038521Scracauer
43138521Scracauer	return 0;
4321556Srgrimes}
4331556Srgrimes
4341556Srgrimes
4351556Srgrimes
43617987Speterint
43790111Simpjobidcmd(int argc __unused, char **argv)
43817987Speter{
4391556Srgrimes	struct job *jp;
4401556Srgrimes	int i;
4411556Srgrimes
4421556Srgrimes	jp = getjob(argv[1]);
4431556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
4441556Srgrimes		out1fmt("%d", jp->ps[i].pid);
4451556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
4461556Srgrimes	}
4471556Srgrimes	return 0;
4481556Srgrimes}
4491556Srgrimes
4501556Srgrimes
4511556Srgrimes
4521556Srgrimes/*
4531556Srgrimes * Convert a job name to a job structure.
4541556Srgrimes */
4551556Srgrimes
4561556SrgrimesSTATIC struct job *
45790111Simpgetjob(char *name)
45890111Simp{
4591556Srgrimes	int jobno;
46025222Ssteve	struct job *jp;
4611556Srgrimes	int pid;
4621556Srgrimes	int i;
4631556Srgrimes
4641556Srgrimes	if (name == NULL) {
4651556Srgrimes#if JOBS
46697659Stjrcurrentjob:	if ((jp = getcurjob(NULL)) == NULL)
4671556Srgrimes			error("No current job");
46897659Stjr		return (jp);
4691556Srgrimes#else
4701556Srgrimes		error("No current job");
4711556Srgrimes#endif
4721556Srgrimes	} else if (name[0] == '%') {
4731556Srgrimes		if (is_digit(name[1])) {
4741556Srgrimes			jobno = number(name + 1);
4751556Srgrimes			if (jobno > 0 && jobno <= njobs
4761556Srgrimes			 && jobtab[jobno - 1].used != 0)
4771556Srgrimes				return &jobtab[jobno - 1];
4781556Srgrimes#if JOBS
4791556Srgrimes		} else if (name[1] == '%' && name[2] == '\0') {
4801556Srgrimes			goto currentjob;
4811556Srgrimes#endif
4821556Srgrimes		} else {
48325222Ssteve			struct job *found = NULL;
4841556Srgrimes			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
4851556Srgrimes				if (jp->used && jp->nprocs > 0
4861556Srgrimes				 && prefix(name + 1, jp->ps[0].cmd)) {
4871556Srgrimes					if (found)
4881556Srgrimes						error("%s: ambiguous", name);
4891556Srgrimes					found = jp;
4901556Srgrimes				}
4911556Srgrimes			}
4921556Srgrimes			if (found)
4931556Srgrimes				return found;
4941556Srgrimes		}
4951556Srgrimes	} else if (is_number(name)) {
4961556Srgrimes		pid = number(name);
4971556Srgrimes		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
4981556Srgrimes			if (jp->used && jp->nprocs > 0
4991556Srgrimes			 && jp->ps[jp->nprocs - 1].pid == pid)
5001556Srgrimes				return jp;
5011556Srgrimes		}
5021556Srgrimes	}
5031556Srgrimes	error("No such job: %s", name);
50417987Speter	/*NOTREACHED*/
50517987Speter	return NULL;
5061556Srgrimes}
5071556Srgrimes
5081556Srgrimes
5091556Srgrimes
5101556Srgrimes/*
5111556Srgrimes * Return a new job structure,
5121556Srgrimes */
5131556Srgrimes
5141556Srgrimesstruct job *
51590111Simpmakejob(union node *node __unused, int nprocs)
51617987Speter{
5171556Srgrimes	int i;
5181556Srgrimes	struct job *jp;
5191556Srgrimes
5201556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
5211556Srgrimes		if (--i < 0) {
5221556Srgrimes			INTOFF;
5231556Srgrimes			if (njobs == 0) {
5241556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
52597659Stjr				jobmru = NULL;
5261556Srgrimes			} else {
5271556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
52817987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
52997659Stjr#if JOBS
53097659Stjr				/* Relocate `next' pointers and list head */
53197659Stjr				jobmru = &jp[jobmru - jobtab];
53297659Stjr				for (i = 0; i < njobs; i++)
53397659Stjr					if (jp[i].next != NULL)
53497659Stjr						jp[i].next = &jp[jp[i].next -
53597659Stjr						    jobtab];
53697659Stjr#endif
53720425Ssteve				/* Relocate `ps' pointers */
53820425Ssteve				for (i = 0; i < njobs; i++)
53920425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
54020425Ssteve						jp[i].ps = &jp[i].ps0;
5411556Srgrimes				ckfree(jobtab);
5421556Srgrimes				jobtab = jp;
5431556Srgrimes			}
5441556Srgrimes			jp = jobtab + njobs;
5451556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
5461556Srgrimes			INTON;
5471556Srgrimes			break;
5481556Srgrimes		}
5491556Srgrimes		if (jp->used == 0)
5501556Srgrimes			break;
5511556Srgrimes	}
5521556Srgrimes	INTOFF;
5531556Srgrimes	jp->state = 0;
5541556Srgrimes	jp->used = 1;
5551556Srgrimes	jp->changed = 0;
5561556Srgrimes	jp->nprocs = 0;
5571556Srgrimes#if JOBS
5581556Srgrimes	jp->jobctl = jobctl;
55997659Stjr	jp->next = NULL;
5601556Srgrimes#endif
5611556Srgrimes	if (nprocs > 1) {
5621556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
5631556Srgrimes	} else {
5641556Srgrimes		jp->ps = &jp->ps0;
5651556Srgrimes	}
5661556Srgrimes	INTON;
56717987Speter	TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
56817987Speter	    jp - jobtab + 1));
5691556Srgrimes	return jp;
5708855Srgrimes}
5711556Srgrimes
57297659Stjr#if JOBS
57397659StjrSTATIC void
57497659Stjrsetcurjob(struct job *cj)
57597659Stjr{
57697659Stjr	struct job *jp, *prev;
5771556Srgrimes
57897659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
57997659Stjr		if (jp == cj) {
58097659Stjr			if (prev != NULL)
58197659Stjr				prev->next = jp->next;
58297659Stjr			else
58397659Stjr				jobmru = jp->next;
58497659Stjr			jp->next = jobmru;
58597659Stjr			jobmru = cj;
58697659Stjr			return;
58797659Stjr		}
58897659Stjr	}
58997659Stjr	cj->next = jobmru;
59097659Stjr	jobmru = cj;
59197659Stjr}
59297659Stjr
59397659StjrSTATIC void
59497659Stjrdeljob(struct job *j)
59597659Stjr{
59697659Stjr	struct job *jp, *prev;
59797659Stjr
59897659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
59997659Stjr		if (jp == j) {
60097659Stjr			if (prev != NULL)
60197659Stjr				prev->next = jp->next;
60297659Stjr			else
60397659Stjr				jobmru = jp->next;
60497659Stjr			return;
60597659Stjr		}
60697659Stjr	}
60797659Stjr}
60897659Stjr
6091556Srgrimes/*
61097659Stjr * Return the most recently used job that isn't `nj', and preferably one
61197659Stjr * that is stopped.
61297659Stjr */
61397659StjrSTATIC struct job *
61497659Stjrgetcurjob(struct job *nj)
61597659Stjr{
61697659Stjr	struct job *jp;
61797659Stjr
61897659Stjr	/* Try to find a stopped one.. */
61997659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
62097659Stjr		if (jp->used && jp != nj && jp->state == JOBSTOPPED)
62197659Stjr			return (jp);
62297659Stjr	/* Otherwise the most recently used job that isn't `nj' */
62397659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
62497659Stjr		if (jp->used && jp != nj)
62597659Stjr			return (jp);
62697659Stjr
62797659Stjr	return (NULL);
62897659Stjr}
62997659Stjr
63097659Stjr#endif
63197659Stjr
63297659Stjr/*
6331556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
6341556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
6351556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
6361556Srgrimes * be NULL.  The mode parameter can be one of the following:
6371556Srgrimes *	FORK_FG - Fork off a foreground process.
6381556Srgrimes *	FORK_BG - Fork off a background process.
6391556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
6401556Srgrimes *		     process group even if job control is on.
6411556Srgrimes *
6421556Srgrimes * When job control is turned off, background processes have their standard
6431556Srgrimes * input redirected to /dev/null (except for the second and later processes
6441556Srgrimes * in a pipeline).
6451556Srgrimes */
6461556Srgrimes
6471556Srgrimesint
64890111Simpforkshell(struct job *jp, union node *n, int mode)
64917987Speter{
6501556Srgrimes	int pid;
6511556Srgrimes	int pgrp;
6521556Srgrimes
65317987Speter	TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
65417987Speter	    mode));
6551556Srgrimes	INTOFF;
6561556Srgrimes	pid = fork();
6571556Srgrimes	if (pid == -1) {
6581556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
6591556Srgrimes		INTON;
66053891Scracauer		error("Cannot fork: %s", strerror(errno));
6611556Srgrimes	}
6621556Srgrimes	if (pid == 0) {
6631556Srgrimes		struct job *p;
6641556Srgrimes		int wasroot;
6651556Srgrimes		int i;
6661556Srgrimes
6671556Srgrimes		TRACE(("Child shell %d\n", getpid()));
6681556Srgrimes		wasroot = rootshell;
6691556Srgrimes		rootshell = 0;
6701556Srgrimes		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
6711556Srgrimes			if (p->used)
6721556Srgrimes				freejob(p);
6731556Srgrimes		closescript();
6741556Srgrimes		INTON;
6751556Srgrimes		clear_traps();
6761556Srgrimes#if JOBS
6771556Srgrimes		jobctl = 0;		/* do job control only in root shell */
6781556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
6791556Srgrimes			if (jp == NULL || jp->nprocs == 0)
6801556Srgrimes				pgrp = getpid();
6811556Srgrimes			else
6821556Srgrimes				pgrp = jp->ps[0].pid;
68321352Ssteve			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
6841556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
68520425Ssteve#ifdef OLD_TTY_DRIVER
6861556Srgrimes				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
68718016Speter					error("TIOCSPGRP failed, errno=%d", errno);
68820425Ssteve#else
68920425Ssteve				if (tcsetpgrp(2, pgrp) < 0)
69020425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
69120425Ssteve#endif
6921556Srgrimes			}
6931556Srgrimes			setsignal(SIGTSTP);
6941556Srgrimes			setsignal(SIGTTOU);
6951556Srgrimes		} else if (mode == FORK_BG) {
6961556Srgrimes			ignoresig(SIGINT);
6971556Srgrimes			ignoresig(SIGQUIT);
6981556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
6991556Srgrimes			    ! fd0_redirected_p ()) {
7001556Srgrimes				close(0);
70169793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
70269793Sobrien					error("Can't open %s: %s",
70369793Sobrien					    _PATH_DEVNULL, strerror(errno));
7041556Srgrimes			}
7051556Srgrimes		}
7061556Srgrimes#else
7071556Srgrimes		if (mode == FORK_BG) {
7081556Srgrimes			ignoresig(SIGINT);
7091556Srgrimes			ignoresig(SIGQUIT);
7101556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
7111556Srgrimes			    ! fd0_redirected_p ()) {
7121556Srgrimes				close(0);
71369793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
71469793Sobrien					error("Can't open %s: %s",
71569793Sobrien					    _PATH_DEVNULL, strerror(errno));
7161556Srgrimes			}
7171556Srgrimes		}
7181556Srgrimes#endif
7191556Srgrimes		if (wasroot && iflag) {
7201556Srgrimes			setsignal(SIGINT);
7211556Srgrimes			setsignal(SIGQUIT);
7221556Srgrimes			setsignal(SIGTERM);
7231556Srgrimes		}
7241556Srgrimes		return pid;
7251556Srgrimes	}
7261556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
7271556Srgrimes		if (jp == NULL || jp->nprocs == 0)
7281556Srgrimes			pgrp = pid;
7291556Srgrimes		else
7301556Srgrimes			pgrp = jp->ps[0].pid;
73117987Speter		setpgid(pid, pgrp);
7321556Srgrimes	}
7331556Srgrimes	if (mode == FORK_BG)
7341556Srgrimes		backgndpid = pid;		/* set $! */
7351556Srgrimes	if (jp) {
7361556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
7371556Srgrimes		ps->pid = pid;
7381556Srgrimes		ps->status = -1;
7391556Srgrimes		ps->cmd = nullstr;
7401556Srgrimes		if (iflag && rootshell && n)
7411556Srgrimes			ps->cmd = commandtext(n);
74297659Stjr#if JOBS
74397659Stjr		setcurjob(jp);
74497659Stjr#endif
7451556Srgrimes	}
7461556Srgrimes	INTON;
7471556Srgrimes	TRACE(("In parent shell:  child = %d\n", pid));
7481556Srgrimes	return pid;
7491556Srgrimes}
7501556Srgrimes
7511556Srgrimes
7521556Srgrimes
7531556Srgrimes/*
7541556Srgrimes * Wait for job to finish.
7551556Srgrimes *
7561556Srgrimes * Under job control we have the problem that while a child process is
7571556Srgrimes * running interrupts generated by the user are sent to the child but not
7581556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
7591556Srgrimes * active user may be hard to kill.  With job control turned off, an
7601556Srgrimes * interactive user may place an interactive program inside a loop.  If
7611556Srgrimes * the interactive program catches interrupts, the user doesn't want
7621556Srgrimes * these interrupts to also abort the loop.  The approach we take here
7631556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
76446684Skris * foreground process to terminate, and then send itself an interrupt
7651556Srgrimes * signal if the child process was terminated by an interrupt signal.
7661556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
7671556Srgrimes * exit on interrupt; unless these processes terminate themselves by
7681556Srgrimes * sending a signal to themselves (instead of calling exit) they will
7691556Srgrimes * confuse this approach.
7701556Srgrimes */
7711556Srgrimes
7721556Srgrimesint
77390111Simpwaitforjob(struct job *jp, int *origstatus)
77445916Scracauer{
7751556Srgrimes#if JOBS
77617987Speter	int mypgrp = getpgrp();
7771556Srgrimes#endif
7781556Srgrimes	int status;
7791556Srgrimes	int st;
7801556Srgrimes
7811556Srgrimes	INTOFF;
7821556Srgrimes	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
78338950Scracauer	while (jp->state == 0)
78438950Scracauer		if (dowait(1, jp) == -1)
78538950Scracauer			dotrap();
7861556Srgrimes#if JOBS
7871556Srgrimes	if (jp->jobctl) {
78820425Ssteve#ifdef OLD_TTY_DRIVER
7891556Srgrimes		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
79020425Ssteve			error("TIOCSPGRP failed, errno=%d\n", errno);
79120425Ssteve#else
79220425Ssteve		if (tcsetpgrp(2, mypgrp) < 0)
79320425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
79420425Ssteve#endif
7951556Srgrimes	}
7961556Srgrimes	if (jp->state == JOBSTOPPED)
79797659Stjr		setcurjob(jp);
7981556Srgrimes#endif
7991556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
80045916Scracauer	if (origstatus != NULL)
80145916Scracauer		*origstatus = status;
8021556Srgrimes	/* convert to 8 bits */
80326104Ssteve	if (WIFEXITED(status))
80426104Ssteve		st = WEXITSTATUS(status);
8051556Srgrimes#if JOBS
80626104Ssteve	else if (WIFSTOPPED(status))
80726104Ssteve		st = WSTOPSIG(status) + 128;
8081556Srgrimes#endif
8091556Srgrimes	else
81026104Ssteve		st = WTERMSIG(status) + 128;
8111556Srgrimes	if (! JOBS || jp->state == JOBDONE)
8121556Srgrimes		freejob(jp);
81338521Scracauer	if (int_pending()) {
81438521Scracauer		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
81538521Scracauer			kill(getpid(), SIGINT);
81638521Scracauer		else
81738521Scracauer			CLEAR_PENDING_INT;
81838521Scracauer	}
8191556Srgrimes	INTON;
8201556Srgrimes	return st;
8211556Srgrimes}
8221556Srgrimes
8231556Srgrimes
8241556Srgrimes
8251556Srgrimes/*
8261556Srgrimes * Wait for a process to terminate.
8271556Srgrimes */
8281556Srgrimes
8291556SrgrimesSTATIC int
83090111Simpdowait(int block, struct job *job)
83117987Speter{
8321556Srgrimes	int pid;
8331556Srgrimes	int status;
8341556Srgrimes	struct procstat *sp;
8351556Srgrimes	struct job *jp;
8361556Srgrimes	struct job *thisjob;
8371556Srgrimes	int done;
8381556Srgrimes	int stopped;
8391556Srgrimes	int core;
84026104Ssteve	int sig;
8411556Srgrimes
84238950Scracauer	in_dowait++;
8431556Srgrimes	TRACE(("dowait(%d) called\n", block));
8441556Srgrimes	do {
8451556Srgrimes		pid = waitproc(block, &status);
8461556Srgrimes		TRACE(("wait returns %d, status=%d\n", pid, status));
84772086Scracauer	} while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
84872086Scracauer	    (WIFSTOPPED(status) && !iflag));
84938950Scracauer	in_dowait--;
85038521Scracauer	if (breakwaitcmd != 0) {
85138521Scracauer		breakwaitcmd = 0;
85238521Scracauer		return -1;
85338521Scracauer	}
8541556Srgrimes	if (pid <= 0)
8551556Srgrimes		return pid;
8561556Srgrimes	INTOFF;
8571556Srgrimes	thisjob = NULL;
8581556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
8591556Srgrimes		if (jp->used) {
8601556Srgrimes			done = 1;
8611556Srgrimes			stopped = 1;
8621556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
8631556Srgrimes				if (sp->pid == -1)
8641556Srgrimes					continue;
8651556Srgrimes				if (sp->pid == pid) {
86626104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
86726104Ssteve						   pid, sp->status, status));
8681556Srgrimes					sp->status = status;
8691556Srgrimes					thisjob = jp;
8701556Srgrimes				}
8711556Srgrimes				if (sp->status == -1)
8721556Srgrimes					stopped = 0;
87326104Ssteve				else if (WIFSTOPPED(sp->status))
8741556Srgrimes					done = 0;
8751556Srgrimes			}
8761556Srgrimes			if (stopped) {		/* stopped or done */
8771556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
8781556Srgrimes				if (jp->state != state) {
8791556Srgrimes					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
8801556Srgrimes					jp->state = state;
8811556Srgrimes#if JOBS
88297659Stjr					if (done)
88397659Stjr						deljob(jp);
8841556Srgrimes#endif
8851556Srgrimes				}
8861556Srgrimes			}
8871556Srgrimes		}
8881556Srgrimes	}
8891556Srgrimes	INTON;
8901556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
89126104Ssteve		core = WCOREDUMP(status);
8921556Srgrimes#if JOBS
89326104Ssteve		if (WIFSTOPPED(status))
89426104Ssteve			sig = WSTOPSIG(status);
89526104Ssteve		else
8961556Srgrimes#endif
89726104Ssteve			if (WIFEXITED(status))
89826104Ssteve				sig = 0;
89926104Ssteve			else
90026104Ssteve				sig = WTERMSIG(status);
90126104Ssteve
90226104Ssteve		if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
9031556Srgrimes			if (thisjob != job)
9041556Srgrimes				outfmt(out2, "%d: ", pid);
9051556Srgrimes#if JOBS
90626104Ssteve			if (sig == SIGTSTP && rootshell && iflag)
9071556Srgrimes				outfmt(out2, "%%%d ", job - jobtab + 1);
9081556Srgrimes#endif
90926104Ssteve			if (sig < NSIG && sys_siglist[sig])
91026104Ssteve				out2str(sys_siglist[sig]);
9111556Srgrimes			else
91226104Ssteve				outfmt(out2, "Signal %d", sig);
9131556Srgrimes			if (core)
9141556Srgrimes				out2str(" - core dumped");
9151556Srgrimes			out2c('\n');
9161556Srgrimes			flushout(&errout);
9171556Srgrimes		} else {
91826104Ssteve			TRACE(("Not printing status: status=%d, sig=%d\n",
91926104Ssteve				   status, sig));
9201556Srgrimes		}
9211556Srgrimes	} else {
9221556Srgrimes		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
9231556Srgrimes		if (thisjob)
9241556Srgrimes			thisjob->changed = 1;
9251556Srgrimes	}
9261556Srgrimes	return pid;
9271556Srgrimes}
9281556Srgrimes
9291556Srgrimes
9301556Srgrimes
9311556Srgrimes/*
9321556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
9331556Srgrimes * stopped processes.  If block is zero, we return a value of zero
9341556Srgrimes * rather than blocking.
9351556Srgrimes *
9361556Srgrimes * System V doesn't have a non-blocking wait system call.  It does
9371556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's
9381556Srgrimes * children dies.  The obvious way to use SIGCLD would be to install
9391556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD
9401556Srgrimes * was received, and have waitproc bump another counter when it got
9411556Srgrimes * the status of a process.  Waitproc would then know that a wait
9421556Srgrimes * system call would not block if the two counters were different.
9431556Srgrimes * This approach doesn't work because if a process has children that
9441556Srgrimes * have not been waited for, System V will send it a SIGCLD when it
9451556Srgrimes * installs a signal handler for SIGCLD.  What this means is that when
9461556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously
9471556Srgrimes * until is runs out of stack space, unless it does a wait call before
9481556Srgrimes * restoring the signal handler.  The code below takes advantage of
9491556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and
9501556Srgrimes * then checking to see whether it was called.  If there are any
9511556Srgrimes * children to be waited for, it will be.
9521556Srgrimes *
9531556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking
9541556Srgrimes * waits at all.  In this case, the user will not be informed when
9551556Srgrimes * a background process until the next time she runs a real program
9561556Srgrimes * (as opposed to running a builtin command or just typing return),
9571556Srgrimes * and the jobs command may give out of date information.
9581556Srgrimes */
9591556Srgrimes
9601556Srgrimes#ifdef SYSV
96138521ScracauerSTATIC sig_atomic_t gotsigchild;
9621556Srgrimes
9631556SrgrimesSTATIC int onsigchild() {
9641556Srgrimes	gotsigchild = 1;
9651556Srgrimes}
9661556Srgrimes#endif
9671556Srgrimes
9681556Srgrimes
9691556SrgrimesSTATIC int
97090111Simpwaitproc(int block, int *status)
97117987Speter{
9721556Srgrimes#ifdef BSD
9731556Srgrimes	int flags;
9741556Srgrimes
9751556Srgrimes#if JOBS
9761556Srgrimes	flags = WUNTRACED;
9771556Srgrimes#else
9781556Srgrimes	flags = 0;
9791556Srgrimes#endif
9801556Srgrimes	if (block == 0)
9811556Srgrimes		flags |= WNOHANG;
9821556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
9831556Srgrimes#else
9841556Srgrimes#ifdef SYSV
9851556Srgrimes	int (*save)();
9861556Srgrimes
9871556Srgrimes	if (block == 0) {
9881556Srgrimes		gotsigchild = 0;
9891556Srgrimes		save = signal(SIGCLD, onsigchild);
9901556Srgrimes		signal(SIGCLD, save);
9911556Srgrimes		if (gotsigchild == 0)
9921556Srgrimes			return 0;
9931556Srgrimes	}
9941556Srgrimes	return wait(status);
9951556Srgrimes#else
9961556Srgrimes	if (block == 0)
9971556Srgrimes		return 0;
9981556Srgrimes	return wait(status);
9991556Srgrimes#endif
10001556Srgrimes#endif
10011556Srgrimes}
10021556Srgrimes
10031556Srgrimes/*
10041556Srgrimes * return 1 if there are stopped jobs, otherwise 0
10051556Srgrimes */
10061556Srgrimesint job_warning = 0;
10071556Srgrimesint
100890111Simpstoppedjobs(void)
10091556Srgrimes{
101025222Ssteve	int jobno;
101125222Ssteve	struct job *jp;
10121556Srgrimes
10131556Srgrimes	if (job_warning)
10141556Srgrimes		return (0);
10151556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
10161556Srgrimes		if (jp->used == 0)
10171556Srgrimes			continue;
10181556Srgrimes		if (jp->state == JOBSTOPPED) {
10191556Srgrimes			out2str("You have stopped jobs.\n");
10201556Srgrimes			job_warning = 2;
10211556Srgrimes			return (1);
10221556Srgrimes		}
10231556Srgrimes	}
10241556Srgrimes
10251556Srgrimes	return (0);
10261556Srgrimes}
10271556Srgrimes
10281556Srgrimes/*
10291556Srgrimes * Return a string identifying a command (to be printed by the
10301556Srgrimes * jobs command.
10311556Srgrimes */
10321556Srgrimes
10331556SrgrimesSTATIC char *cmdnextc;
10341556SrgrimesSTATIC int cmdnleft;
10351556Srgrimes#define MAXCMDTEXT	200
10361556Srgrimes
10371556Srgrimeschar *
103890111Simpcommandtext(union node *n)
103990111Simp{
10401556Srgrimes	char *name;
10411556Srgrimes
10421556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
10431556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
10441556Srgrimes	cmdtxt(n);
10451556Srgrimes	*cmdnextc = '\0';
10461556Srgrimes	return name;
10471556Srgrimes}
10481556Srgrimes
10491556Srgrimes
10501556SrgrimesSTATIC void
105190111Simpcmdtxt(union node *n)
105290111Simp{
10531556Srgrimes	union node *np;
10541556Srgrimes	struct nodelist *lp;
10551556Srgrimes	char *p;
10561556Srgrimes	int i;
10571556Srgrimes	char s[2];
10581556Srgrimes
10591556Srgrimes	if (n == NULL)
10601556Srgrimes		return;
10611556Srgrimes	switch (n->type) {
10621556Srgrimes	case NSEMI:
10631556Srgrimes		cmdtxt(n->nbinary.ch1);
10641556Srgrimes		cmdputs("; ");
10651556Srgrimes		cmdtxt(n->nbinary.ch2);
10661556Srgrimes		break;
10671556Srgrimes	case NAND:
10681556Srgrimes		cmdtxt(n->nbinary.ch1);
10691556Srgrimes		cmdputs(" && ");
10701556Srgrimes		cmdtxt(n->nbinary.ch2);
10711556Srgrimes		break;
10721556Srgrimes	case NOR:
10731556Srgrimes		cmdtxt(n->nbinary.ch1);
10741556Srgrimes		cmdputs(" || ");
10751556Srgrimes		cmdtxt(n->nbinary.ch2);
10761556Srgrimes		break;
10771556Srgrimes	case NPIPE:
10781556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
10791556Srgrimes			cmdtxt(lp->n);
10801556Srgrimes			if (lp->next)
10811556Srgrimes				cmdputs(" | ");
10821556Srgrimes		}
10831556Srgrimes		break;
10841556Srgrimes	case NSUBSHELL:
10851556Srgrimes		cmdputs("(");
10861556Srgrimes		cmdtxt(n->nredir.n);
10871556Srgrimes		cmdputs(")");
10881556Srgrimes		break;
10891556Srgrimes	case NREDIR:
10901556Srgrimes	case NBACKGND:
10911556Srgrimes		cmdtxt(n->nredir.n);
10921556Srgrimes		break;
10931556Srgrimes	case NIF:
10941556Srgrimes		cmdputs("if ");
10951556Srgrimes		cmdtxt(n->nif.test);
10961556Srgrimes		cmdputs("; then ");
10971556Srgrimes		cmdtxt(n->nif.ifpart);
10981556Srgrimes		cmdputs("...");
10991556Srgrimes		break;
11001556Srgrimes	case NWHILE:
11011556Srgrimes		cmdputs("while ");
11021556Srgrimes		goto until;
11031556Srgrimes	case NUNTIL:
11041556Srgrimes		cmdputs("until ");
11051556Srgrimesuntil:
11061556Srgrimes		cmdtxt(n->nbinary.ch1);
11071556Srgrimes		cmdputs("; do ");
11081556Srgrimes		cmdtxt(n->nbinary.ch2);
11091556Srgrimes		cmdputs("; done");
11101556Srgrimes		break;
11111556Srgrimes	case NFOR:
11121556Srgrimes		cmdputs("for ");
11131556Srgrimes		cmdputs(n->nfor.var);
11141556Srgrimes		cmdputs(" in ...");
11151556Srgrimes		break;
11161556Srgrimes	case NCASE:
11171556Srgrimes		cmdputs("case ");
11181556Srgrimes		cmdputs(n->ncase.expr->narg.text);
11191556Srgrimes		cmdputs(" in ...");
11201556Srgrimes		break;
11211556Srgrimes	case NDEFUN:
11221556Srgrimes		cmdputs(n->narg.text);
11231556Srgrimes		cmdputs("() ...");
11241556Srgrimes		break;
11251556Srgrimes	case NCMD:
11261556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
11271556Srgrimes			cmdtxt(np);
11281556Srgrimes			if (np->narg.next)
11291556Srgrimes				cmdputs(" ");
11301556Srgrimes		}
11311556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
11321556Srgrimes			cmdputs(" ");
11331556Srgrimes			cmdtxt(np);
11341556Srgrimes		}
11351556Srgrimes		break;
11361556Srgrimes	case NARG:
11371556Srgrimes		cmdputs(n->narg.text);
11381556Srgrimes		break;
11391556Srgrimes	case NTO:
11401556Srgrimes		p = ">";  i = 1;  goto redir;
11411556Srgrimes	case NAPPEND:
11421556Srgrimes		p = ">>";  i = 1;  goto redir;
11431556Srgrimes	case NTOFD:
11441556Srgrimes		p = ">&";  i = 1;  goto redir;
114596922Stjr	case NCLOBBER:
114696922Stjr		p = ">|"; i = 1; goto redir;
11471556Srgrimes	case NFROM:
11481556Srgrimes		p = "<";  i = 0;  goto redir;
114966612Sbrian	case NFROMTO:
115066612Sbrian		p = "<>";  i = 0;  goto redir;
11511556Srgrimes	case NFROMFD:
11521556Srgrimes		p = "<&";  i = 0;  goto redir;
11531556Srgrimesredir:
11541556Srgrimes		if (n->nfile.fd != i) {
11551556Srgrimes			s[0] = n->nfile.fd + '0';
11561556Srgrimes			s[1] = '\0';
11571556Srgrimes			cmdputs(s);
11581556Srgrimes		}
11591556Srgrimes		cmdputs(p);
11601556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
11611556Srgrimes			s[0] = n->ndup.dupfd + '0';
11621556Srgrimes			s[1] = '\0';
11631556Srgrimes			cmdputs(s);
11641556Srgrimes		} else {
11651556Srgrimes			cmdtxt(n->nfile.fname);
11661556Srgrimes		}
11671556Srgrimes		break;
11681556Srgrimes	case NHERE:
11691556Srgrimes	case NXHERE:
11701556Srgrimes		cmdputs("<<...");
11711556Srgrimes		break;
11721556Srgrimes	default:
11731556Srgrimes		cmdputs("???");
11741556Srgrimes		break;
11751556Srgrimes	}
11761556Srgrimes}
11771556Srgrimes
11781556Srgrimes
11791556Srgrimes
11801556SrgrimesSTATIC void
118190111Simpcmdputs(char *s)
118290111Simp{
118325222Ssteve	char *p, *q;
118425222Ssteve	char c;
11851556Srgrimes	int subtype = 0;
11861556Srgrimes
11871556Srgrimes	if (cmdnleft <= 0)
11881556Srgrimes		return;
11891556Srgrimes	p = s;
11901556Srgrimes	q = cmdnextc;
11911556Srgrimes	while ((c = *p++) != '\0') {
11921556Srgrimes		if (c == CTLESC)
11931556Srgrimes			*q++ = *p++;
11941556Srgrimes		else if (c == CTLVAR) {
11951556Srgrimes			*q++ = '$';
11961556Srgrimes			if (--cmdnleft > 0)
11971556Srgrimes				*q++ = '{';
11981556Srgrimes			subtype = *p++;
11991556Srgrimes		} else if (c == '=' && subtype != 0) {
12001556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
12011556Srgrimes			subtype = 0;
12021556Srgrimes		} else if (c == CTLENDVAR) {
12031556Srgrimes			*q++ = '}';
120418954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
12051556Srgrimes			cmdnleft++;		/* ignore it */
12061556Srgrimes		else
12071556Srgrimes			*q++ = c;
12081556Srgrimes		if (--cmdnleft <= 0) {
12091556Srgrimes			*q++ = '.';
12101556Srgrimes			*q++ = '.';
12111556Srgrimes			*q++ = '.';
12121556Srgrimes			break;
12131556Srgrimes		}
12141556Srgrimes	}
12151556Srgrimes	cmdnextc = q;
12161556Srgrimes}
1217