xref: /freebsd/bin/sh/jobs.c (revision 3cddabf0bd5fa9532fe2a1653b7b30f0ffe40401)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334417f629SPeter Wemm #include <sys/ioctl.h>
348ab2e970SJohn Baldwin #include <sys/param.h>
358ab2e970SJohn Baldwin #include <sys/resource.h>
368ab2e970SJohn Baldwin #include <sys/time.h>
378ab2e970SJohn Baldwin #include <sys/wait.h>
388ab2e970SJohn Baldwin #include <errno.h>
398ab2e970SJohn Baldwin #include <fcntl.h>
408ab2e970SJohn Baldwin #include <paths.h>
418ab2e970SJohn Baldwin #include <signal.h>
429fa5f4a0SJilles Tjoelker #include <stddef.h>
438ab2e970SJohn Baldwin #include <stdlib.h>
448ab2e970SJohn Baldwin #include <unistd.h>
45aa9caaf6SPeter Wemm 
464b88c807SRodney W. Grimes #include "shell.h"
474b88c807SRodney W. Grimes #if JOBS
48aa9caaf6SPeter Wemm #include <termios.h>
494b88c807SRodney W. Grimes #undef CEOF			/* syntax.h redefines this */
504b88c807SRodney W. Grimes #endif
51aa9caaf6SPeter Wemm #include "redir.h"
52caf29fabSJilles Tjoelker #include "exec.h"
53aa9caaf6SPeter Wemm #include "show.h"
544b88c807SRodney W. Grimes #include "main.h"
554b88c807SRodney W. Grimes #include "parser.h"
564b88c807SRodney W. Grimes #include "nodes.h"
574b88c807SRodney W. Grimes #include "jobs.h"
584b88c807SRodney W. Grimes #include "options.h"
594b88c807SRodney W. Grimes #include "trap.h"
604b88c807SRodney W. Grimes #include "syntax.h"
614b88c807SRodney W. Grimes #include "input.h"
624b88c807SRodney W. Grimes #include "output.h"
634b88c807SRodney W. Grimes #include "memalloc.h"
644b88c807SRodney W. Grimes #include "error.h"
654b88c807SRodney W. Grimes #include "mystring.h"
66c543e1aeSJilles Tjoelker #include "var.h"
67454a02b3SJilles Tjoelker #include "builtins.h"
68b5532964SJilles Tjoelker #include "eval.h"
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes 
71b0125116SJilles Tjoelker /*
72b0125116SJilles Tjoelker  * A job structure contains information about a job.  A job is either a
73b0125116SJilles Tjoelker  * single process or a set of processes contained in a pipeline.  In the
74b0125116SJilles Tjoelker  * latter case, pidlist will be non-NULL, and will point to a -1 terminated
75b0125116SJilles Tjoelker  * array of pids.
76b0125116SJilles Tjoelker  */
77b0125116SJilles Tjoelker 
78b0125116SJilles Tjoelker struct procstat {
79b0125116SJilles Tjoelker 	pid_t pid;		/* process id */
80b0125116SJilles Tjoelker 	int status;		/* status flags (defined above) */
81b0125116SJilles Tjoelker 	char *cmd;		/* text of command being run */
82b0125116SJilles Tjoelker };
83b0125116SJilles Tjoelker 
84b0125116SJilles Tjoelker 
85b0125116SJilles Tjoelker /* states */
86b0125116SJilles Tjoelker #define JOBSTOPPED 1		/* all procs are stopped */
87b0125116SJilles Tjoelker #define JOBDONE 2		/* all procs are completed */
88b0125116SJilles Tjoelker 
89b0125116SJilles Tjoelker 
90b0125116SJilles Tjoelker struct job {
91b0125116SJilles Tjoelker 	struct procstat ps0;	/* status of process */
92b0125116SJilles Tjoelker 	struct procstat *ps;	/* status or processes when more than one */
93b0125116SJilles Tjoelker 	short nprocs;		/* number of processes */
94b0125116SJilles Tjoelker 	pid_t pgrp;		/* process group of this job */
95b0125116SJilles Tjoelker 	char state;		/* true if job is finished */
96*3cddabf0SFu-Cheng Wang 	char used;		/* true if this entry is in use */
97b0125116SJilles Tjoelker 	char changed;		/* true if status has changed */
98b0125116SJilles Tjoelker 	char foreground;	/* true if running in the foreground */
99b0125116SJilles Tjoelker 	char remembered;	/* true if $! referenced */
100484160a9SJilles Tjoelker 	char pipefail;		/* pass any non-zero status */
101b0125116SJilles Tjoelker #if JOBS
102b0125116SJilles Tjoelker 	char jobctl;		/* job running under job control */
103b0125116SJilles Tjoelker 	struct job *next;	/* job used after this one */
104b0125116SJilles Tjoelker #endif
105b0125116SJilles Tjoelker };
106b0125116SJilles Tjoelker 
107b0125116SJilles Tjoelker 
108aa7b6f82SDavid E. O'Brien static struct job *jobtab;	/* array of jobs */
109aa7b6f82SDavid E. O'Brien static int njobs;		/* size of array */
1100bdd3871SJilles Tjoelker static pid_t backgndpid = -1;	/* pid of last background process */
1110bdd3871SJilles Tjoelker static struct job *bgjob = NULL; /* last background process */
1124b88c807SRodney W. Grimes #if JOBS
113aa7b6f82SDavid E. O'Brien static struct job *jobmru;	/* most recently used job list */
114aa7b6f82SDavid E. O'Brien static pid_t initialpgrp;	/* pgrp of shell on invocation */
1154b88c807SRodney W. Grimes #endif
1166c8bbe74STim J. Robbins static int ttyfd = -1;
1174b88c807SRodney W. Grimes 
1181794251aSJilles Tjoelker /* mode flags for dowait */
1191794251aSJilles Tjoelker #define DOWAIT_BLOCK	0x1 /* wait until a child exits */
120edebe564SJilles Tjoelker #define DOWAIT_SIG	0x2 /* if DOWAIT_BLOCK, abort on signal */
121edebe564SJilles Tjoelker #define DOWAIT_SIG_TRAP	0x4 /* if DOWAIT_SIG, abort on trapped signal only */
1221794251aSJilles Tjoelker 
123ab0a2172SSteve Price #if JOBS
12488328642SDavid E. O'Brien static void restartjob(struct job *);
125ab0a2172SSteve Price #endif
12688328642SDavid E. O'Brien static void freejob(struct job *);
127a4099656SJilles Tjoelker static int waitcmdloop(struct job *);
12861346cbdSJilles Tjoelker static struct job *getjob_nonotfound(const char *);
12961346cbdSJilles Tjoelker static struct job *getjob(const char *);
13076961687SJilles Tjoelker pid_t killjob(const char *, int);
13188328642SDavid E. O'Brien static pid_t dowait(int, struct job *);
13288328642SDavid E. O'Brien static void checkzombies(void);
13388328642SDavid E. O'Brien static void cmdtxt(union node *);
13488328642SDavid E. O'Brien static void cmdputs(const char *);
135d86825beSTim J. Robbins #if JOBS
13688328642SDavid E. O'Brien static void setcurjob(struct job *);
13788328642SDavid E. O'Brien static void deljob(struct job *);
13888328642SDavid E. O'Brien static struct job *getcurjob(struct job *);
139d86825beSTim J. Robbins #endif
140484160a9SJilles Tjoelker static int getjobstatus(const struct job *);
1411bb49f95SJilles Tjoelker static void printjobcmd(struct job *);
1421bb49f95SJilles Tjoelker static void showjob(struct job *, int);
1434b88c807SRodney W. Grimes 
1444b88c807SRodney W. Grimes 
1454b88c807SRodney W. Grimes /*
1464b88c807SRodney W. Grimes  * Turn job control on and off.
1474b88c807SRodney W. Grimes  */
1484b88c807SRodney W. Grimes 
1490bdd3871SJilles Tjoelker static int jobctl;
1504b88c807SRodney W. Grimes 
151ab0a2172SSteve Price #if JOBS
152cd60e2c6SJilles Tjoelker static void
jobctl_notty(void)153cd60e2c6SJilles Tjoelker jobctl_notty(void)
154cd60e2c6SJilles Tjoelker {
155cd60e2c6SJilles Tjoelker 	if (ttyfd >= 0) {
156cd60e2c6SJilles Tjoelker 		close(ttyfd);
157cd60e2c6SJilles Tjoelker 		ttyfd = -1;
158cd60e2c6SJilles Tjoelker 	}
159cd60e2c6SJilles Tjoelker 	if (!iflag) {
160cd60e2c6SJilles Tjoelker 		setsignal(SIGTSTP);
161cd60e2c6SJilles Tjoelker 		setsignal(SIGTTOU);
162cd60e2c6SJilles Tjoelker 		setsignal(SIGTTIN);
163cd60e2c6SJilles Tjoelker 		jobctl = 1;
164cd60e2c6SJilles Tjoelker 		return;
165cd60e2c6SJilles Tjoelker 	}
166cd60e2c6SJilles Tjoelker 	out2fmt_flush("sh: can't access tty; job control turned off\n");
167cd60e2c6SJilles Tjoelker 	mflag = 0;
168cd60e2c6SJilles Tjoelker }
169cd60e2c6SJilles Tjoelker 
1704b88c807SRodney W. Grimes void
setjobctl(int on)1715134c3f7SWarner Losh setjobctl(int on)
172aa9caaf6SPeter Wemm {
1736c8bbe74STim J. Robbins 	int i;
1744b88c807SRodney W. Grimes 
1754b88c807SRodney W. Grimes 	if (on == jobctl || rootshell == 0)
1764b88c807SRodney W. Grimes 		return;
1774b88c807SRodney W. Grimes 	if (on) {
1786c8bbe74STim J. Robbins 		if (ttyfd != -1)
1796c8bbe74STim J. Robbins 			close(ttyfd);
1805aa6dfdaSJilles Tjoelker 		if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
1816c8bbe74STim J. Robbins 			i = 0;
1826c8bbe74STim J. Robbins 			while (i <= 2 && !isatty(i))
1836c8bbe74STim J. Robbins 				i++;
1845aa6dfdaSJilles Tjoelker 			if (i > 2 ||
185cd60e2c6SJilles Tjoelker 			    (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) {
186cd60e2c6SJilles Tjoelker 				jobctl_notty();
187cd60e2c6SJilles Tjoelker 				return;
188cd60e2c6SJilles Tjoelker 			}
1896c8bbe74STim J. Robbins 		}
190c57bc2b1STim J. Robbins 		if (ttyfd < 10) {
191c57bc2b1STim J. Robbins 			/*
192c57bc2b1STim J. Robbins 			 * Keep our TTY file descriptor out of the way of
193c57bc2b1STim J. Robbins 			 * the user's redirections.
194c57bc2b1STim J. Robbins 			 */
1955aa6dfdaSJilles Tjoelker 			if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
196cd60e2c6SJilles Tjoelker 				jobctl_notty();
197cd60e2c6SJilles Tjoelker 				return;
198c57bc2b1STim J. Robbins 			}
199c57bc2b1STim J. Robbins 			close(ttyfd);
200c57bc2b1STim J. Robbins 			ttyfd = i;
201c57bc2b1STim J. Robbins 		}
2024b88c807SRodney W. Grimes 		do { /* while we are in the background */
2036c8bbe74STim J. Robbins 			initialpgrp = tcgetpgrp(ttyfd);
204ab0a2172SSteve Price 			if (initialpgrp < 0) {
205cd60e2c6SJilles Tjoelker 				jobctl_notty();
2064b88c807SRodney W. Grimes 				return;
2074b88c807SRodney W. Grimes 			}
208b036c75bSJilles Tjoelker 			if (initialpgrp != getpgrp()) {
209cd60e2c6SJilles Tjoelker 				if (!iflag) {
210cd60e2c6SJilles Tjoelker 					initialpgrp = -1;
211cd60e2c6SJilles Tjoelker 					jobctl_notty();
212cd60e2c6SJilles Tjoelker 					return;
213cd60e2c6SJilles Tjoelker 				}
214b036c75bSJilles Tjoelker 				kill(0, SIGTTIN);
2154b88c807SRodney W. Grimes 				continue;
2164b88c807SRodney W. Grimes 			}
2174b88c807SRodney W. Grimes 		} while (0);
2184b88c807SRodney W. Grimes 		setsignal(SIGTSTP);
2194b88c807SRodney W. Grimes 		setsignal(SIGTTOU);
2204b88c807SRodney W. Grimes 		setsignal(SIGTTIN);
221aa9caaf6SPeter Wemm 		setpgid(0, rootpid);
2226c8bbe74STim J. Robbins 		tcsetpgrp(ttyfd, rootpid);
2234b88c807SRodney W. Grimes 	} else { /* turning job control off */
224aa9caaf6SPeter Wemm 		setpgid(0, initialpgrp);
225cd60e2c6SJilles Tjoelker 		if (ttyfd >= 0) {
2266c8bbe74STim J. Robbins 			tcsetpgrp(ttyfd, initialpgrp);
2276c8bbe74STim J. Robbins 			close(ttyfd);
2286c8bbe74STim J. Robbins 			ttyfd = -1;
229cd60e2c6SJilles Tjoelker 		}
2304b88c807SRodney W. Grimes 		setsignal(SIGTSTP);
2314b88c807SRodney W. Grimes 		setsignal(SIGTTOU);
2324b88c807SRodney W. Grimes 		setsignal(SIGTTIN);
2334b88c807SRodney W. Grimes 	}
2344b88c807SRodney W. Grimes 	jobctl = on;
2354b88c807SRodney W. Grimes }
236ab0a2172SSteve Price #endif
2374b88c807SRodney W. Grimes 
2384b88c807SRodney W. Grimes 
2394b88c807SRodney W. Grimes #if JOBS
240aa9caaf6SPeter Wemm int
fgcmd(int argc __unused,char ** argv __unused)241f0ef49bbSJilles Tjoelker fgcmd(int argc __unused, char **argv __unused)
242aa9caaf6SPeter Wemm {
2434b88c807SRodney W. Grimes 	struct job *jp;
244b80be282STim J. Robbins 	pid_t pgrp;
2454b88c807SRodney W. Grimes 	int status;
2464b88c807SRodney W. Grimes 
247f0ef49bbSJilles Tjoelker 	nextopt("");
248f0ef49bbSJilles Tjoelker 	jp = getjob(*argptr);
2494b88c807SRodney W. Grimes 	if (jp->jobctl == 0)
2504b88c807SRodney W. Grimes 		error("job not created under job control");
2511bb49f95SJilles Tjoelker 	printjobcmd(jp);
252addcdbb2STim J. Robbins 	flushout(&output);
2534b88c807SRodney W. Grimes 	pgrp = jp->ps[0].pid;
254cd60e2c6SJilles Tjoelker 	if (ttyfd >= 0)
2556c8bbe74STim J. Robbins 		tcsetpgrp(ttyfd, pgrp);
2564b88c807SRodney W. Grimes 	restartjob(jp);
257f1ae2c66STim J. Robbins 	jp->foreground = 1;
2584b88c807SRodney W. Grimes 	INTOFF;
25957b2932aSMartin Cracauer 	status = waitforjob(jp, (int *)NULL);
2604b88c807SRodney W. Grimes 	INTON;
2614b88c807SRodney W. Grimes 	return status;
2624b88c807SRodney W. Grimes }
2634b88c807SRodney W. Grimes 
2644b88c807SRodney W. Grimes 
265aa9caaf6SPeter Wemm int
bgcmd(int argc __unused,char ** argv __unused)26622afca9bSJilles Tjoelker bgcmd(int argc __unused, char **argv __unused)
267aa9caaf6SPeter Wemm {
2684b88c807SRodney W. Grimes 	struct job *jp;
2694b88c807SRodney W. Grimes 
270f0ef49bbSJilles Tjoelker 	nextopt("");
2714b88c807SRodney W. Grimes 	do {
272f0ef49bbSJilles Tjoelker 		jp = getjob(*argptr);
2734b88c807SRodney W. Grimes 		if (jp->jobctl == 0)
2744b88c807SRodney W. Grimes 			error("job not created under job control");
275addcdbb2STim J. Robbins 		if (jp->state == JOBDONE)
276addcdbb2STim J. Robbins 			continue;
2774b88c807SRodney W. Grimes 		restartjob(jp);
278f1ae2c66STim J. Robbins 		jp->foreground = 0;
279b036c75bSJilles Tjoelker 		out1fmt("[%td] ", jp - jobtab + 1);
2801bb49f95SJilles Tjoelker 		printjobcmd(jp);
281f0ef49bbSJilles Tjoelker 	} while (*argptr != NULL && *++argptr != NULL);
2824b88c807SRodney W. Grimes 	return 0;
2834b88c807SRodney W. Grimes }
2844b88c807SRodney W. Grimes 
2854b88c807SRodney W. Grimes 
28688328642SDavid E. O'Brien static void
restartjob(struct job * jp)2875134c3f7SWarner Losh restartjob(struct job *jp)
2884b88c807SRodney W. Grimes {
2894b88c807SRodney W. Grimes 	struct procstat *ps;
2904b88c807SRodney W. Grimes 	int i;
2914b88c807SRodney W. Grimes 
2924b88c807SRodney W. Grimes 	if (jp->state == JOBDONE)
2934b88c807SRodney W. Grimes 		return;
2943771ef59STim J. Robbins 	setcurjob(jp);
2954b88c807SRodney W. Grimes 	INTOFF;
296b036c75bSJilles Tjoelker 	kill(-jp->ps[0].pid, SIGCONT);
2974b88c807SRodney W. Grimes 	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
29800fc54b0SSteve Price 		if (WIFSTOPPED(ps->status)) {
2994b88c807SRodney W. Grimes 			ps->status = -1;
3004b88c807SRodney W. Grimes 			jp->state = 0;
3014b88c807SRodney W. Grimes 		}
3024b88c807SRodney W. Grimes 	}
3034b88c807SRodney W. Grimes 	INTON;
3044b88c807SRodney W. Grimes }
3054b88c807SRodney W. Grimes #endif
3064b88c807SRodney W. Grimes 
3074b88c807SRodney W. Grimes 
3084b88c807SRodney W. Grimes int
jobscmd(int argc __unused,char * argv[]__unused)3097cbda738SJilles Tjoelker jobscmd(int argc __unused, char *argv[] __unused)
310aa9caaf6SPeter Wemm {
311ad8a0759STim J. Robbins 	char *id;
312de37e41cSStefan Farfeleder 	int ch, mode;
313ad8a0759STim J. Robbins 
314de37e41cSStefan Farfeleder 	mode = SHOWJOBS_DEFAULT;
3157cbda738SJilles Tjoelker 	while ((ch = nextopt("lps")) != '\0') {
316ad8a0759STim J. Robbins 		switch (ch) {
317ad8a0759STim J. Robbins 		case 'l':
318de37e41cSStefan Farfeleder 			mode = SHOWJOBS_VERBOSE;
319de37e41cSStefan Farfeleder 			break;
320de37e41cSStefan Farfeleder 		case 'p':
321de37e41cSStefan Farfeleder 			mode = SHOWJOBS_PGIDS;
322ad8a0759STim J. Robbins 			break;
323ad8a0759STim J. Robbins 		case 's':
324de37e41cSStefan Farfeleder 			mode = SHOWJOBS_PIDS;
325ad8a0759STim J. Robbins 			break;
326ad8a0759STim J. Robbins 		}
327ad8a0759STim J. Robbins 	}
328ad8a0759STim J. Robbins 
3297cbda738SJilles Tjoelker 	if (*argptr == NULL)
330de37e41cSStefan Farfeleder 		showjobs(0, mode);
331ad8a0759STim J. Robbins 	else
3327cbda738SJilles Tjoelker 		while ((id = *argptr++) != NULL)
3331bb49f95SJilles Tjoelker 			showjob(getjob(id), mode);
334ad8a0759STim J. Robbins 
335ad8a0759STim J. Robbins 	return (0);
3364b88c807SRodney W. Grimes }
3374b88c807SRodney W. Grimes 
getjobstatus(const struct job * jp)338484160a9SJilles Tjoelker static int getjobstatus(const struct job *jp)
339484160a9SJilles Tjoelker {
340484160a9SJilles Tjoelker 	int i, status;
341484160a9SJilles Tjoelker 
342484160a9SJilles Tjoelker 	if (!jp->pipefail)
343484160a9SJilles Tjoelker 		return (jp->ps[jp->nprocs - 1].status);
344484160a9SJilles Tjoelker 	for (i = jp->nprocs - 1; i >= 0; i--) {
345484160a9SJilles Tjoelker 		status = jp->ps[i].status;
346484160a9SJilles Tjoelker 		if (status != 0)
347484160a9SJilles Tjoelker 			return (status);
348484160a9SJilles Tjoelker 	}
349484160a9SJilles Tjoelker 	return (0);
350484160a9SJilles Tjoelker }
351484160a9SJilles Tjoelker 
35288328642SDavid E. O'Brien static void
printjobcmd(struct job * jp)3531bb49f95SJilles Tjoelker printjobcmd(struct job *jp)
3541bb49f95SJilles Tjoelker {
3551bb49f95SJilles Tjoelker 	struct procstat *ps;
3561bb49f95SJilles Tjoelker 	int i;
3571bb49f95SJilles Tjoelker 
3581bb49f95SJilles Tjoelker 	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
3591bb49f95SJilles Tjoelker 		out1str(ps->cmd);
3601bb49f95SJilles Tjoelker 		if (i > 0)
3611bb49f95SJilles Tjoelker 			out1str(" | ");
3621bb49f95SJilles Tjoelker 	}
3631bb49f95SJilles Tjoelker 	out1c('\n');
3641bb49f95SJilles Tjoelker }
3651bb49f95SJilles Tjoelker 
3661bb49f95SJilles Tjoelker static void
showjob(struct job * jp,int mode)3671bb49f95SJilles Tjoelker showjob(struct job *jp, int mode)
368aa9caaf6SPeter Wemm {
3694b88c807SRodney W. Grimes 	char s[64];
37090a2c7eaSJilles Tjoelker 	char statebuf[16];
37190a2c7eaSJilles Tjoelker 	const char *statestr, *coredump;
372e3f30949STim J. Robbins 	struct procstat *ps;
373ad8a0759STim J. Robbins 	struct job *j;
374190bc94aSJilles Tjoelker 	int col, curr, i, jobno, prev, procno, status;
375ad8a0759STim J. Robbins 	char c;
3764b88c807SRodney W. Grimes 
377de37e41cSStefan Farfeleder 	procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
378e3f30949STim J. Robbins 	jobno = jp - jobtab + 1;
379ad8a0759STim J. Robbins 	curr = prev = 0;
380ad8a0759STim J. Robbins #if JOBS
381ad8a0759STim J. Robbins 	if ((j = getcurjob(NULL)) != NULL) {
382ad8a0759STim J. Robbins 		curr = j - jobtab + 1;
383ad8a0759STim J. Robbins 		if ((j = getcurjob(j)) != NULL)
384ad8a0759STim J. Robbins 			prev = j - jobtab + 1;
385ad8a0759STim J. Robbins 	}
386ad8a0759STim J. Robbins #endif
38790a2c7eaSJilles Tjoelker 	coredump = "";
388484160a9SJilles Tjoelker 	status = getjobstatus(jp);
3891bb49f95SJilles Tjoelker 	if (jp->state == 0) {
39090a2c7eaSJilles Tjoelker 		statestr = "Running";
3911bb49f95SJilles Tjoelker #if JOBS
3921bb49f95SJilles Tjoelker 	} else if (jp->state == JOBSTOPPED) {
393190bc94aSJilles Tjoelker 		ps = jp->ps + jp->nprocs - 1;
3941bb49f95SJilles Tjoelker 		while (!WIFSTOPPED(ps->status) && ps > jp->ps)
3951bb49f95SJilles Tjoelker 			ps--;
3961bb49f95SJilles Tjoelker 		if (WIFSTOPPED(ps->status))
3971bb49f95SJilles Tjoelker 			i = WSTOPSIG(ps->status);
3981bb49f95SJilles Tjoelker 		else
3991bb49f95SJilles Tjoelker 			i = -1;
40090a2c7eaSJilles Tjoelker 		statestr = strsignal(i);
40190a2c7eaSJilles Tjoelker 		if (statestr == NULL)
40290a2c7eaSJilles Tjoelker 			statestr = "Suspended";
4031bb49f95SJilles Tjoelker #endif
404190bc94aSJilles Tjoelker 	} else if (WIFEXITED(status)) {
405190bc94aSJilles Tjoelker 		if (WEXITSTATUS(status) == 0)
40690a2c7eaSJilles Tjoelker 			statestr = "Done";
40790a2c7eaSJilles Tjoelker 		else {
40890a2c7eaSJilles Tjoelker 			fmtstr(statebuf, sizeof(statebuf), "Done(%d)",
409190bc94aSJilles Tjoelker 			    WEXITSTATUS(status));
41090a2c7eaSJilles Tjoelker 			statestr = statebuf;
41190a2c7eaSJilles Tjoelker 		}
4121bb49f95SJilles Tjoelker 	} else {
413190bc94aSJilles Tjoelker 		i = WTERMSIG(status);
41490a2c7eaSJilles Tjoelker 		statestr = strsignal(i);
41590a2c7eaSJilles Tjoelker 		if (statestr == NULL)
41690a2c7eaSJilles Tjoelker 			statestr = "Unknown signal";
417190bc94aSJilles Tjoelker 		if (WCOREDUMP(status))
41890a2c7eaSJilles Tjoelker 			coredump = " (core dumped)";
4191bb49f95SJilles Tjoelker 	}
4201bb49f95SJilles Tjoelker 
42133c5acf0SJilles Tjoelker 	for (ps = jp->ps ; procno > 0 ; ps++, procno--) { /* for each process */
422de37e41cSStefan Farfeleder 		if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
4235af61b52SJilles Tjoelker 			out1fmt("%d\n", (int)ps->pid);
42433c5acf0SJilles Tjoelker 			continue;
425ad8a0759STim J. Robbins 		}
4261bb49f95SJilles Tjoelker 		if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
42733c5acf0SJilles Tjoelker 			continue;
428cb1a4fb4STim J. Robbins 		if (jobno == curr && ps == jp->ps)
429ad8a0759STim J. Robbins 			c = '+';
430cb1a4fb4STim J. Robbins 		else if (jobno == prev && ps == jp->ps)
431ad8a0759STim J. Robbins 			c = '-';
4324b88c807SRodney W. Grimes 		else
433ad8a0759STim J. Robbins 			c = ' ';
434ad8a0759STim J. Robbins 		if (ps == jp->ps)
435ad8a0759STim J. Robbins 			fmtstr(s, 64, "[%d] %c ", jobno, c);
436ad8a0759STim J. Robbins 		else
437ad8a0759STim J. Robbins 			fmtstr(s, 64, "    %c ", c);
4384b88c807SRodney W. Grimes 		out1str(s);
4394b88c807SRodney W. Grimes 		col = strlen(s);
440de37e41cSStefan Farfeleder 		if (mode == SHOWJOBS_VERBOSE) {
441b80be282STim J. Robbins 			fmtstr(s, 64, "%d ", (int)ps->pid);
442ad8a0759STim J. Robbins 			out1str(s);
443ad8a0759STim J. Robbins 			col += strlen(s);
444ad8a0759STim J. Robbins 		}
4451bb49f95SJilles Tjoelker 		if (ps == jp->ps) {
4461bb49f95SJilles Tjoelker 			out1str(statestr);
44790a2c7eaSJilles Tjoelker 			out1str(coredump);
44890a2c7eaSJilles Tjoelker 			col += strlen(statestr) + strlen(coredump);
4494b88c807SRodney W. Grimes 		}
4504b88c807SRodney W. Grimes 		do {
4514b88c807SRodney W. Grimes 			out1c(' ');
4524b88c807SRodney W. Grimes 			col++;
4534b88c807SRodney W. Grimes 		} while (col < 30);
4541bb49f95SJilles Tjoelker 		if (mode == SHOWJOBS_VERBOSE) {
4554b88c807SRodney W. Grimes 			out1str(ps->cmd);
4564b88c807SRodney W. Grimes 			out1c('\n');
4571bb49f95SJilles Tjoelker 		} else
4581bb49f95SJilles Tjoelker 			printjobcmd(jp);
4594b88c807SRodney W. Grimes 	}
460e3f30949STim J. Robbins }
461e3f30949STim J. Robbins 
462e3f30949STim J. Robbins /*
463e3f30949STim J. Robbins  * Print a list of jobs.  If "change" is nonzero, only print jobs whose
464e3f30949STim J. Robbins  * statuses have changed since the last call to showjobs.
465e3f30949STim J. Robbins  *
466e3f30949STim J. Robbins  * If the shell is interrupted in the process of creating a job, the
467e3f30949STim J. Robbins  * result may be a job structure containing zero processes.  Such structures
468e3f30949STim J. Robbins  * will be freed here.
469e3f30949STim J. Robbins  */
470e3f30949STim J. Robbins 
471e3f30949STim J. Robbins void
showjobs(int change,int mode)472de37e41cSStefan Farfeleder showjobs(int change, int mode)
473e3f30949STim J. Robbins {
474e3f30949STim J. Robbins 	int jobno;
475e3f30949STim J. Robbins 	struct job *jp;
476e3f30949STim J. Robbins 
477e3f30949STim J. Robbins 	TRACE(("showjobs(%d) called\n", change));
4781f65c541SJilles Tjoelker 	checkzombies();
479e3f30949STim J. Robbins 	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
480e3f30949STim J. Robbins 		if (! jp->used)
481e3f30949STim J. Robbins 			continue;
482e3f30949STim J. Robbins 		if (jp->nprocs == 0) {
483e3f30949STim J. Robbins 			freejob(jp);
484e3f30949STim J. Robbins 			continue;
485e3f30949STim J. Robbins 		}
486e3f30949STim J. Robbins 		if (change && ! jp->changed)
487e3f30949STim J. Robbins 			continue;
4881bb49f95SJilles Tjoelker 		showjob(jp, mode);
48958c73babSJilles Tjoelker 		if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
4904b88c807SRodney W. Grimes 			jp->changed = 0;
49158c73babSJilles Tjoelker 			/* Hack: discard jobs for which $! has not been
49258c73babSJilles Tjoelker 			 * referenced in interactive mode when they terminate.
493ed4c3b5fSJilles Tjoelker 			 */
494ed4c3b5fSJilles Tjoelker 			if (jp->state == JOBDONE && !jp->remembered &&
495ed4c3b5fSJilles Tjoelker 					(iflag || jp != bgjob)) {
4964b88c807SRodney W. Grimes 				freejob(jp);
4974b88c807SRodney W. Grimes 			}
4984b88c807SRodney W. Grimes 		}
4994b88c807SRodney W. Grimes 	}
50058c73babSJilles Tjoelker }
5014b88c807SRodney W. Grimes 
5024b88c807SRodney W. Grimes 
5034b88c807SRodney W. Grimes /*
5044b88c807SRodney W. Grimes  * Mark a job structure as unused.
5054b88c807SRodney W. Grimes  */
5064b88c807SRodney W. Grimes 
50788328642SDavid E. O'Brien static void
freejob(struct job * jp)5085134c3f7SWarner Losh freejob(struct job *jp)
5094b88c807SRodney W. Grimes {
5104b88c807SRodney W. Grimes 	struct procstat *ps;
5114b88c807SRodney W. Grimes 	int i;
5124b88c807SRodney W. Grimes 
5134b88c807SRodney W. Grimes 	INTOFF;
514ed4c3b5fSJilles Tjoelker 	if (bgjob == jp)
515ed4c3b5fSJilles Tjoelker 		bgjob = NULL;
5164b88c807SRodney W. Grimes 	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
5174b88c807SRodney W. Grimes 		if (ps->cmd != nullstr)
5184b88c807SRodney W. Grimes 			ckfree(ps->cmd);
5194b88c807SRodney W. Grimes 	}
5204b88c807SRodney W. Grimes 	if (jp->ps != &jp->ps0)
5214b88c807SRodney W. Grimes 		ckfree(jp->ps);
5224b88c807SRodney W. Grimes 	jp->used = 0;
5234b88c807SRodney W. Grimes #if JOBS
524d86825beSTim J. Robbins 	deljob(jp);
5254b88c807SRodney W. Grimes #endif
5264b88c807SRodney W. Grimes 	INTON;
5274b88c807SRodney W. Grimes }
5284b88c807SRodney W. Grimes 
5294b88c807SRodney W. Grimes 
5304b88c807SRodney W. Grimes 
5314b88c807SRodney W. Grimes int
waitcmd(int argc __unused,char ** argv __unused)532d70ad6f2SJilles Tjoelker waitcmd(int argc __unused, char **argv __unused)
533aa9caaf6SPeter Wemm {
5344b88c807SRodney W. Grimes 	struct job *job;
535a4099656SJilles Tjoelker 	int retval;
5364b88c807SRodney W. Grimes 
537d70ad6f2SJilles Tjoelker 	nextopt("");
538a4099656SJilles Tjoelker 	if (*argptr == NULL)
539a4099656SJilles Tjoelker 		return (waitcmdloop(NULL));
540a4099656SJilles Tjoelker 
541a4099656SJilles Tjoelker 	do {
54279b1d318SJilles Tjoelker 		job = getjob_nonotfound(*argptr);
54379b1d318SJilles Tjoelker 		if (job == NULL)
54479b1d318SJilles Tjoelker 			retval = 127;
54579b1d318SJilles Tjoelker 		else
546a4099656SJilles Tjoelker 			retval = waitcmdloop(job);
547a4099656SJilles Tjoelker 		argptr++;
548a4099656SJilles Tjoelker 	} while (*argptr != NULL);
549a4099656SJilles Tjoelker 
550a4099656SJilles Tjoelker 	return (retval);
5514b88c807SRodney W. Grimes }
5521f40b47bSMartin Cracauer 
553a4099656SJilles Tjoelker static int
waitcmdloop(struct job * job)554a4099656SJilles Tjoelker waitcmdloop(struct job *job)
555a4099656SJilles Tjoelker {
556b823fb59SJilles Tjoelker 	int status, retval, sig;
557a4099656SJilles Tjoelker 	struct job *jp;
558a4099656SJilles Tjoelker 
5591f40b47bSMartin Cracauer 	/*
5601f40b47bSMartin Cracauer 	 * Loop until a process is terminated or stopped, or a SIGINT is
5611f40b47bSMartin Cracauer 	 * received.
5627a8e920bSMartin Cracauer 	 */
5631f40b47bSMartin Cracauer 
5641f40b47bSMartin Cracauer 	do {
5654b88c807SRodney W. Grimes 		if (job != NULL) {
5667cb5a0d8SJilles Tjoelker 			if (job->state == JOBDONE) {
567484160a9SJilles Tjoelker 				status = getjobstatus(job);
56800fc54b0SSteve Price 				if (WIFEXITED(status))
56900fc54b0SSteve Price 					retval = WEXITSTATUS(status);
5704b88c807SRodney W. Grimes 				else
57100fc54b0SSteve Price 					retval = WTERMSIG(status) + 128;
572ed4c3b5fSJilles Tjoelker 				if (! iflag || ! job->changed)
5734b88c807SRodney W. Grimes 					freejob(job);
574ed4c3b5fSJilles Tjoelker 				else {
575ed4c3b5fSJilles Tjoelker 					job->remembered = 0;
576ed4c3b5fSJilles Tjoelker 					if (job == bgjob)
577ed4c3b5fSJilles Tjoelker 						bgjob = NULL;
578ed4c3b5fSJilles Tjoelker 				}
57900fc54b0SSteve Price 				return retval;
5804b88c807SRodney W. Grimes 			}
5814b88c807SRodney W. Grimes 		} else {
5825a1a07f2SJilles Tjoelker 			if (njobs == 0)
5835a1a07f2SJilles Tjoelker 				return 0;
584ed4c3b5fSJilles Tjoelker 			for (jp = jobtab ; jp < jobtab + njobs; jp++)
585ed4c3b5fSJilles Tjoelker 				if (jp->used && jp->state == JOBDONE) {
586ed4c3b5fSJilles Tjoelker 					if (! iflag || ! jp->changed)
587ed4c3b5fSJilles Tjoelker 						freejob(jp);
588ed4c3b5fSJilles Tjoelker 					else {
589ed4c3b5fSJilles Tjoelker 						jp->remembered = 0;
590ed4c3b5fSJilles Tjoelker 						if (jp == bgjob)
591ed4c3b5fSJilles Tjoelker 							bgjob = NULL;
592ed4c3b5fSJilles Tjoelker 					}
593ed4c3b5fSJilles Tjoelker 				}
5944b88c807SRodney W. Grimes 			for (jp = jobtab ; ; jp++) {
5954b88c807SRodney W. Grimes 				if (jp >= jobtab + njobs) {	/* no running procs */
5964b88c807SRodney W. Grimes 					return 0;
5974b88c807SRodney W. Grimes 				}
5984b88c807SRodney W. Grimes 				if (jp->used && jp->state == 0)
5994b88c807SRodney W. Grimes 					break;
6004b88c807SRodney W. Grimes 			}
6014b88c807SRodney W. Grimes 		}
6021794251aSJilles Tjoelker 	} while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
6037a8e920bSMartin Cracauer 
604b823fb59SJilles Tjoelker 	sig = pendingsig_waitcmd;
605b823fb59SJilles Tjoelker 	pendingsig_waitcmd = 0;
606b823fb59SJilles Tjoelker 	return sig + 128;
6074b88c807SRodney W. Grimes }
6084b88c807SRodney W. Grimes 
6094b88c807SRodney W. Grimes 
6104b88c807SRodney W. Grimes 
611aa9caaf6SPeter Wemm int
jobidcmd(int argc __unused,char ** argv __unused)612f0ef49bbSJilles Tjoelker jobidcmd(int argc __unused, char **argv __unused)
613aa9caaf6SPeter Wemm {
6144b88c807SRodney W. Grimes 	struct job *jp;
6154b88c807SRodney W. Grimes 	int i;
6164b88c807SRodney W. Grimes 
617f0ef49bbSJilles Tjoelker 	nextopt("");
618f0ef49bbSJilles Tjoelker 	jp = getjob(*argptr);
6194b88c807SRodney W. Grimes 	for (i = 0 ; i < jp->nprocs ; ) {
620b80be282STim J. Robbins 		out1fmt("%d", (int)jp->ps[i].pid);
6214b88c807SRodney W. Grimes 		out1c(++i < jp->nprocs? ' ' : '\n');
6224b88c807SRodney W. Grimes 	}
6234b88c807SRodney W. Grimes 	return 0;
6244b88c807SRodney W. Grimes }
6254b88c807SRodney W. Grimes 
6264b88c807SRodney W. Grimes 
6274b88c807SRodney W. Grimes 
6284b88c807SRodney W. Grimes /*
6294b88c807SRodney W. Grimes  * Convert a job name to a job structure.
6304b88c807SRodney W. Grimes  */
6314b88c807SRodney W. Grimes 
63288328642SDavid E. O'Brien static struct job *
getjob_nonotfound(const char * name)63361346cbdSJilles Tjoelker getjob_nonotfound(const char *name)
6344b88c807SRodney W. Grimes {
6354b88c807SRodney W. Grimes 	int jobno;
636f63d6dbfSTim J. Robbins 	struct job *found, *jp;
637e61ae4ffSJilles Tjoelker 	size_t namelen;
638b80be282STim J. Robbins 	pid_t pid;
6394b88c807SRodney W. Grimes 	int i;
6404b88c807SRodney W. Grimes 
6414b88c807SRodney W. Grimes 	if (name == NULL) {
6424b88c807SRodney W. Grimes #if JOBS
643622fdf32SJilles Tjoelker 		name = "%+";
6444b88c807SRodney W. Grimes #else
6454b88c807SRodney W. Grimes 		error("No current job");
6464b88c807SRodney W. Grimes #endif
647622fdf32SJilles Tjoelker 	}
648622fdf32SJilles Tjoelker 	if (name[0] == '%') {
6494b88c807SRodney W. Grimes 		if (is_digit(name[1])) {
6504b88c807SRodney W. Grimes 			jobno = number(name + 1);
6514b88c807SRodney W. Grimes 			if (jobno > 0 && jobno <= njobs
6524b88c807SRodney W. Grimes 			 && jobtab[jobno - 1].used != 0)
6534b88c807SRodney W. Grimes 				return &jobtab[jobno - 1];
6544b88c807SRodney W. Grimes #if JOBS
655622fdf32SJilles Tjoelker 		} else if ((name[1] == '%' || name[1] == '+') &&
656622fdf32SJilles Tjoelker 		    name[2] == '\0') {
657622fdf32SJilles Tjoelker 			if ((jp = getcurjob(NULL)) == NULL)
658622fdf32SJilles Tjoelker 				error("No current job");
659622fdf32SJilles Tjoelker 			return (jp);
660f63d6dbfSTim J. Robbins 		} else if (name[1] == '-' && name[2] == '\0') {
661f63d6dbfSTim J. Robbins 			if ((jp = getcurjob(NULL)) == NULL ||
662f63d6dbfSTim J. Robbins 			    (jp = getcurjob(jp)) == NULL)
663f63d6dbfSTim J. Robbins 				error("No previous job");
664f63d6dbfSTim J. Robbins 			return (jp);
6654b88c807SRodney W. Grimes #endif
666f63d6dbfSTim J. Robbins 		} else if (name[1] == '?') {
667f63d6dbfSTim J. Robbins 			found = NULL;
668f63d6dbfSTim J. Robbins 			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
669f63d6dbfSTim J. Robbins 				if (jp->used && jp->nprocs > 0
670f63d6dbfSTim J. Robbins 				 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
671f63d6dbfSTim J. Robbins 					if (found)
672f63d6dbfSTim J. Robbins 						error("%s: ambiguous", name);
673f63d6dbfSTim J. Robbins 					found = jp;
674f63d6dbfSTim J. Robbins 				}
675f63d6dbfSTim J. Robbins 			}
676f63d6dbfSTim J. Robbins 			if (found != NULL)
677f63d6dbfSTim J. Robbins 				return (found);
6784b88c807SRodney W. Grimes 		} else {
679e61ae4ffSJilles Tjoelker 			namelen = strlen(name);
680f63d6dbfSTim J. Robbins 			found = NULL;
6814b88c807SRodney W. Grimes 			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
6824b88c807SRodney W. Grimes 				if (jp->used && jp->nprocs > 0
683e61ae4ffSJilles Tjoelker 				 && strncmp(jp->ps[0].cmd, name + 1,
684e61ae4ffSJilles Tjoelker 				 namelen - 1) == 0) {
6854b88c807SRodney W. Grimes 					if (found)
6864b88c807SRodney W. Grimes 						error("%s: ambiguous", name);
6874b88c807SRodney W. Grimes 					found = jp;
6884b88c807SRodney W. Grimes 				}
6894b88c807SRodney W. Grimes 			}
6904b88c807SRodney W. Grimes 			if (found)
6914b88c807SRodney W. Grimes 				return found;
6924b88c807SRodney W. Grimes 		}
6934b88c807SRodney W. Grimes 	} else if (is_number(name)) {
694b80be282STim J. Robbins 		pid = (pid_t)number(name);
6954b88c807SRodney W. Grimes 		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
6964b88c807SRodney W. Grimes 			if (jp->used && jp->nprocs > 0
6974b88c807SRodney W. Grimes 			 && jp->ps[jp->nprocs - 1].pid == pid)
6984b88c807SRodney W. Grimes 				return jp;
6994b88c807SRodney W. Grimes 		}
7004b88c807SRodney W. Grimes 	}
701aa9caaf6SPeter Wemm 	return NULL;
7024b88c807SRodney W. Grimes }
7034b88c807SRodney W. Grimes 
7044b88c807SRodney W. Grimes 
70579b1d318SJilles Tjoelker static struct job *
getjob(const char * name)70661346cbdSJilles Tjoelker getjob(const char *name)
70779b1d318SJilles Tjoelker {
70879b1d318SJilles Tjoelker 	struct job *jp;
70979b1d318SJilles Tjoelker 
71079b1d318SJilles Tjoelker 	jp = getjob_nonotfound(name);
71179b1d318SJilles Tjoelker 	if (jp == NULL)
71279b1d318SJilles Tjoelker 		error("No such job: %s", name);
71379b1d318SJilles Tjoelker 	return (jp);
71479b1d318SJilles Tjoelker }
71579b1d318SJilles Tjoelker 
71679b1d318SJilles Tjoelker 
71776961687SJilles Tjoelker int
killjob(const char * name,int sig)71876961687SJilles Tjoelker killjob(const char *name, int sig)
7190a62a9caSJilles Tjoelker {
7200a62a9caSJilles Tjoelker 	struct job *jp;
72176961687SJilles Tjoelker 	int i, ret;
7220a62a9caSJilles Tjoelker 
7230a62a9caSJilles Tjoelker 	jp = getjob(name);
7244646e82dSJilles Tjoelker 	if (jp->state == JOBDONE)
7254646e82dSJilles Tjoelker 		return 0;
72676961687SJilles Tjoelker 	if (jp->jobctl)
72776961687SJilles Tjoelker 		return kill(-jp->ps[0].pid, sig);
72876961687SJilles Tjoelker 	ret = -1;
72976961687SJilles Tjoelker 	errno = ESRCH;
73076961687SJilles Tjoelker 	for (i = 0; i < jp->nprocs; i++)
73176961687SJilles Tjoelker 		if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
73276961687SJilles Tjoelker 			if (kill(jp->ps[i].pid, sig) == 0)
73376961687SJilles Tjoelker 				ret = 0;
73476961687SJilles Tjoelker 		} else
73576961687SJilles Tjoelker 			ret = 0;
73676961687SJilles Tjoelker 	return ret;
7370a62a9caSJilles Tjoelker }
7384b88c807SRodney W. Grimes 
7394b88c807SRodney W. Grimes /*
7404b88c807SRodney W. Grimes  * Return a new job structure,
7414b88c807SRodney W. Grimes  */
7424b88c807SRodney W. Grimes 
7434b88c807SRodney W. Grimes struct job *
makejob(union node * node __unused,int nprocs)7445134c3f7SWarner Losh makejob(union node *node __unused, int nprocs)
7454b88c807SRodney W. Grimes {
7464b88c807SRodney W. Grimes 	int i;
7474b88c807SRodney W. Grimes 	struct job *jp;
7484b88c807SRodney W. Grimes 
7494b88c807SRodney W. Grimes 	for (i = njobs, jp = jobtab ; ; jp++) {
7504b88c807SRodney W. Grimes 		if (--i < 0) {
7514b88c807SRodney W. Grimes 			INTOFF;
7524b88c807SRodney W. Grimes 			if (njobs == 0) {
7534b88c807SRodney W. Grimes 				jobtab = ckmalloc(4 * sizeof jobtab[0]);
754bdfc15dfSTim J. Robbins #if JOBS
755d86825beSTim J. Robbins 				jobmru = NULL;
756bdfc15dfSTim J. Robbins #endif
7574b88c807SRodney W. Grimes 			} else {
7584b88c807SRodney W. Grimes 				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
759aa9caaf6SPeter Wemm 				memcpy(jp, jobtab, njobs * sizeof jp[0]);
760d86825beSTim J. Robbins #if JOBS
761d86825beSTim J. Robbins 				/* Relocate `next' pointers and list head */
7620d3d4c12STim J. Robbins 				if (jobmru != NULL)
763d86825beSTim J. Robbins 					jobmru = &jp[jobmru - jobtab];
764d86825beSTim J. Robbins 				for (i = 0; i < njobs; i++)
765d86825beSTim J. Robbins 					if (jp[i].next != NULL)
766d86825beSTim J. Robbins 						jp[i].next = &jp[jp[i].next -
767d86825beSTim J. Robbins 						    jobtab];
768d86825beSTim J. Robbins #endif
769ed4c3b5fSJilles Tjoelker 				if (bgjob != NULL)
770ed4c3b5fSJilles Tjoelker 					bgjob = &jp[bgjob - jobtab];
771ab0a2172SSteve Price 				/* Relocate `ps' pointers */
772ab0a2172SSteve Price 				for (i = 0; i < njobs; i++)
773ab0a2172SSteve Price 					if (jp[i].ps == &jobtab[i].ps0)
774ab0a2172SSteve Price 						jp[i].ps = &jp[i].ps0;
7754b88c807SRodney W. Grimes 				ckfree(jobtab);
7764b88c807SRodney W. Grimes 				jobtab = jp;
7774b88c807SRodney W. Grimes 			}
7784b88c807SRodney W. Grimes 			jp = jobtab + njobs;
77946c6b52dSJilles Tjoelker 			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
78046c6b52dSJilles Tjoelker 				;
7814b88c807SRodney W. Grimes 			INTON;
7824b88c807SRodney W. Grimes 			break;
7834b88c807SRodney W. Grimes 		}
7844b88c807SRodney W. Grimes 		if (jp->used == 0)
7854b88c807SRodney W. Grimes 			break;
7864b88c807SRodney W. Grimes 	}
7874b88c807SRodney W. Grimes 	INTOFF;
7884b88c807SRodney W. Grimes 	jp->state = 0;
7894b88c807SRodney W. Grimes 	jp->used = 1;
7904b88c807SRodney W. Grimes 	jp->changed = 0;
7914b88c807SRodney W. Grimes 	jp->nprocs = 0;
792f1ae2c66STim J. Robbins 	jp->foreground = 0;
793ed4c3b5fSJilles Tjoelker 	jp->remembered = 0;
794484160a9SJilles Tjoelker 	jp->pipefail = pipefailflag;
7954b88c807SRodney W. Grimes #if JOBS
7964b88c807SRodney W. Grimes 	jp->jobctl = jobctl;
797d86825beSTim J. Robbins 	jp->next = NULL;
7984b88c807SRodney W. Grimes #endif
7994b88c807SRodney W. Grimes 	if (nprocs > 1) {
8004b88c807SRodney W. Grimes 		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
8014b88c807SRodney W. Grimes 	} else {
8024b88c807SRodney W. Grimes 		jp->ps = &jp->ps0;
8034b88c807SRodney W. Grimes 	}
8044b88c807SRodney W. Grimes 	INTON;
8058ab2e970SJohn Baldwin 	TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
806aa9caaf6SPeter Wemm 	    jp - jobtab + 1));
8074b88c807SRodney W. Grimes 	return jp;
8084b88c807SRodney W. Grimes }
8094b88c807SRodney W. Grimes 
810d86825beSTim J. Robbins #if JOBS
81188328642SDavid E. O'Brien static void
setcurjob(struct job * cj)812d86825beSTim J. Robbins setcurjob(struct job *cj)
813d86825beSTim J. Robbins {
814d86825beSTim J. Robbins 	struct job *jp, *prev;
815d86825beSTim J. Robbins 
816d86825beSTim J. Robbins 	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
817d86825beSTim J. Robbins 		if (jp == cj) {
818d86825beSTim J. Robbins 			if (prev != NULL)
819d86825beSTim J. Robbins 				prev->next = jp->next;
820d86825beSTim J. Robbins 			else
821d86825beSTim J. Robbins 				jobmru = jp->next;
822d86825beSTim J. Robbins 			jp->next = jobmru;
823d86825beSTim J. Robbins 			jobmru = cj;
824d86825beSTim J. Robbins 			return;
825d86825beSTim J. Robbins 		}
826d86825beSTim J. Robbins 	}
827d86825beSTim J. Robbins 	cj->next = jobmru;
828d86825beSTim J. Robbins 	jobmru = cj;
829d86825beSTim J. Robbins }
830d86825beSTim J. Robbins 
83188328642SDavid E. O'Brien static void
deljob(struct job * j)832d86825beSTim J. Robbins deljob(struct job *j)
833d86825beSTim J. Robbins {
834d86825beSTim J. Robbins 	struct job *jp, *prev;
835d86825beSTim J. Robbins 
836d86825beSTim J. Robbins 	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
837d86825beSTim J. Robbins 		if (jp == j) {
838d86825beSTim J. Robbins 			if (prev != NULL)
839d86825beSTim J. Robbins 				prev->next = jp->next;
840d86825beSTim J. Robbins 			else
841d86825beSTim J. Robbins 				jobmru = jp->next;
842d86825beSTim J. Robbins 			return;
843d86825beSTim J. Robbins 		}
844d86825beSTim J. Robbins 	}
845d86825beSTim J. Robbins }
846d86825beSTim J. Robbins 
847d86825beSTim J. Robbins /*
848d86825beSTim J. Robbins  * Return the most recently used job that isn't `nj', and preferably one
849d86825beSTim J. Robbins  * that is stopped.
850d86825beSTim J. Robbins  */
85188328642SDavid E. O'Brien static struct job *
getcurjob(struct job * nj)852d86825beSTim J. Robbins getcurjob(struct job *nj)
853d86825beSTim J. Robbins {
854d86825beSTim J. Robbins 	struct job *jp;
855d86825beSTim J. Robbins 
856d86825beSTim J. Robbins 	/* Try to find a stopped one.. */
857d86825beSTim J. Robbins 	for (jp = jobmru; jp != NULL; jp = jp->next)
858d86825beSTim J. Robbins 		if (jp->used && jp != nj && jp->state == JOBSTOPPED)
859d86825beSTim J. Robbins 			return (jp);
860d86825beSTim J. Robbins 	/* Otherwise the most recently used job that isn't `nj' */
861d86825beSTim J. Robbins 	for (jp = jobmru; jp != NULL; jp = jp->next)
862d86825beSTim J. Robbins 		if (jp->used && jp != nj)
863d86825beSTim J. Robbins 			return (jp);
864d86825beSTim J. Robbins 
865d86825beSTim J. Robbins 	return (NULL);
866d86825beSTim J. Robbins }
867d86825beSTim J. Robbins 
868d86825beSTim J. Robbins #endif
8694b88c807SRodney W. Grimes 
8704b88c807SRodney W. Grimes /*
8714b88c807SRodney W. Grimes  * Fork of a subshell.  If we are doing job control, give the subshell its
8724b88c807SRodney W. Grimes  * own process group.  Jp is a job structure that the job is to be added to.
8734b88c807SRodney W. Grimes  * N is the command that will be evaluated by the child.  Both jp and n may
8744b88c807SRodney W. Grimes  * be NULL.  The mode parameter can be one of the following:
8754b88c807SRodney W. Grimes  *	FORK_FG - Fork off a foreground process.
8764b88c807SRodney W. Grimes  *	FORK_BG - Fork off a background process.
8774b88c807SRodney W. Grimes  *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
8784b88c807SRodney W. Grimes  *		     process group even if job control is on.
8794b88c807SRodney W. Grimes  *
8804b88c807SRodney W. Grimes  * When job control is turned off, background processes have their standard
8814b88c807SRodney W. Grimes  * input redirected to /dev/null (except for the second and later processes
8824b88c807SRodney W. Grimes  * in a pipeline).
8834b88c807SRodney W. Grimes  */
8844b88c807SRodney W. Grimes 
885b80be282STim J. Robbins pid_t
forkshell(struct job * jp,union node * n,int mode)8865134c3f7SWarner Losh forkshell(struct job *jp, union node *n, int mode)
8874b88c807SRodney W. Grimes {
888b80be282STim J. Robbins 	pid_t pid;
889b80be282STim J. Robbins 	pid_t pgrp;
8904b88c807SRodney W. Grimes 
8918ab2e970SJohn Baldwin 	TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
892aa9caaf6SPeter Wemm 	    mode));
8934b88c807SRodney W. Grimes 	INTOFF;
894ff304d37SJilles Tjoelker 	if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
8951f65c541SJilles Tjoelker 		checkzombies();
8965821bf03STim J. Robbins 	flushall();
8974b88c807SRodney W. Grimes 	pid = fork();
8984b88c807SRodney W. Grimes 	if (pid == -1) {
8994b88c807SRodney W. Grimes 		TRACE(("Fork failed, errno=%d\n", errno));
9004b88c807SRodney W. Grimes 		INTON;
9016c48b6cfSMartin Cracauer 		error("Cannot fork: %s", strerror(errno));
9024b88c807SRodney W. Grimes 	}
9034b88c807SRodney W. Grimes 	if (pid == 0) {
9044b88c807SRodney W. Grimes 		struct job *p;
9054b88c807SRodney W. Grimes 		int wasroot;
9064b88c807SRodney W. Grimes 		int i;
9074b88c807SRodney W. Grimes 
908b80be282STim J. Robbins 		TRACE(("Child shell %d\n", (int)getpid()));
9094b88c807SRodney W. Grimes 		wasroot = rootshell;
9104b88c807SRodney W. Grimes 		rootshell = 0;
91129d401c2SJilles Tjoelker 		handler = &main_handler;
9124b88c807SRodney W. Grimes 		closescript();
9134b88c807SRodney W. Grimes 		INTON;
914c543e1aeSJilles Tjoelker 		forcelocal = 0;
9154b88c807SRodney W. Grimes 		clear_traps();
9164b88c807SRodney W. Grimes #if JOBS
9174b88c807SRodney W. Grimes 		jobctl = 0;		/* do job control only in root shell */
9184b88c807SRodney W. Grimes 		if (wasroot && mode != FORK_NOJOB && mflag) {
9194b88c807SRodney W. Grimes 			if (jp == NULL || jp->nprocs == 0)
9204b88c807SRodney W. Grimes 				pgrp = getpid();
9214b88c807SRodney W. Grimes 			else
9224b88c807SRodney W. Grimes 				pgrp = jp->ps[0].pid;
923cd60e2c6SJilles Tjoelker 			if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
924cd60e2c6SJilles Tjoelker 			    ttyfd >= 0) {
925ab41d7f3SJilles Tjoelker 				/*
926ab41d7f3SJilles Tjoelker 				 * Each process in a pipeline must have the tty
927ab41d7f3SJilles Tjoelker 				 * pgrp set before running its code.
928ab41d7f3SJilles Tjoelker 				 * Only for pipelines of three or more processes
929ab41d7f3SJilles Tjoelker 				 * could this be reduced to two calls.
930ab41d7f3SJilles Tjoelker 				 */
9316c8bbe74STim J. Robbins 				if (tcsetpgrp(ttyfd, pgrp) < 0)
932ab0a2172SSteve Price 					error("tcsetpgrp failed, errno=%d", errno);
9334b88c807SRodney W. Grimes 			}
9344b88c807SRodney W. Grimes 			setsignal(SIGTSTP);
9354b88c807SRodney W. Grimes 			setsignal(SIGTTOU);
9364b88c807SRodney W. Grimes 		} else if (mode == FORK_BG) {
9374b88c807SRodney W. Grimes 			ignoresig(SIGINT);
9384b88c807SRodney W. Grimes 			ignoresig(SIGQUIT);
9394b88c807SRodney W. Grimes 			if ((jp == NULL || jp->nprocs == 0) &&
9404b88c807SRodney W. Grimes 			    ! fd0_redirected_p ()) {
9414b88c807SRodney W. Grimes 				close(0);
9421a37aa56SDavid E. O'Brien 				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
94347a5ab29SJilles Tjoelker 					error("cannot open %s: %s",
9441a37aa56SDavid E. O'Brien 					    _PATH_DEVNULL, strerror(errno));
9454b88c807SRodney W. Grimes 			}
9464b88c807SRodney W. Grimes 		}
9474b88c807SRodney W. Grimes #else
9484b88c807SRodney W. Grimes 		if (mode == FORK_BG) {
9494b88c807SRodney W. Grimes 			ignoresig(SIGINT);
9504b88c807SRodney W. Grimes 			ignoresig(SIGQUIT);
9514b88c807SRodney W. Grimes 			if ((jp == NULL || jp->nprocs == 0) &&
9524b88c807SRodney W. Grimes 			    ! fd0_redirected_p ()) {
9534b88c807SRodney W. Grimes 				close(0);
9541a37aa56SDavid E. O'Brien 				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
95547a5ab29SJilles Tjoelker 					error("cannot open %s: %s",
9561a37aa56SDavid E. O'Brien 					    _PATH_DEVNULL, strerror(errno));
9574b88c807SRodney W. Grimes 			}
9584b88c807SRodney W. Grimes 		}
9594b88c807SRodney W. Grimes #endif
9604f8fa749STim J. Robbins 		INTOFF;
9614f8fa749STim J. Robbins 		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
9624f8fa749STim J. Robbins 			if (p->used)
9634f8fa749STim J. Robbins 				freejob(p);
9644f8fa749STim J. Robbins 		INTON;
9654b88c807SRodney W. Grimes 		if (wasroot && iflag) {
9664b88c807SRodney W. Grimes 			setsignal(SIGINT);
9674b88c807SRodney W. Grimes 			setsignal(SIGQUIT);
9684b88c807SRodney W. Grimes 			setsignal(SIGTERM);
9694b88c807SRodney W. Grimes 		}
9704b88c807SRodney W. Grimes 		return pid;
9714b88c807SRodney W. Grimes 	}
9724b88c807SRodney W. Grimes 	if (rootshell && mode != FORK_NOJOB && mflag) {
9734b88c807SRodney W. Grimes 		if (jp == NULL || jp->nprocs == 0)
9744b88c807SRodney W. Grimes 			pgrp = pid;
9754b88c807SRodney W. Grimes 		else
9764b88c807SRodney W. Grimes 			pgrp = jp->ps[0].pid;
977aa9caaf6SPeter Wemm 		setpgid(pid, pgrp);
9784b88c807SRodney W. Grimes 	}
979ed4c3b5fSJilles Tjoelker 	if (mode == FORK_BG) {
980ed4c3b5fSJilles Tjoelker 		if (bgjob != NULL && bgjob->state == JOBDONE &&
981ed4c3b5fSJilles Tjoelker 		    !bgjob->remembered && !iflag)
982ed4c3b5fSJilles Tjoelker 			freejob(bgjob);
9834b88c807SRodney W. Grimes 		backgndpid = pid;		/* set $! */
984ed4c3b5fSJilles Tjoelker 		bgjob = jp;
985ed4c3b5fSJilles Tjoelker 	}
9864b88c807SRodney W. Grimes 	if (jp) {
9874b88c807SRodney W. Grimes 		struct procstat *ps = &jp->ps[jp->nprocs++];
9884b88c807SRodney W. Grimes 		ps->pid = pid;
9894b88c807SRodney W. Grimes 		ps->status = -1;
9904b88c807SRodney W. Grimes 		ps->cmd = nullstr;
9914b88c807SRodney W. Grimes 		if (iflag && rootshell && n)
9924b88c807SRodney W. Grimes 			ps->cmd = commandtext(n);
993f1ae2c66STim J. Robbins 		jp->foreground = mode == FORK_FG;
994d86825beSTim J. Robbins #if JOBS
995d86825beSTim J. Robbins 		setcurjob(jp);
996d86825beSTim J. Robbins #endif
9974b88c807SRodney W. Grimes 	}
9984b88c807SRodney W. Grimes 	INTON;
999b80be282STim J. Robbins 	TRACE(("In parent shell:  child = %d\n", (int)pid));
10004b88c807SRodney W. Grimes 	return pid;
10014b88c807SRodney W. Grimes }
10024b88c807SRodney W. Grimes 
10034b88c807SRodney W. Grimes 
1004caf29fabSJilles Tjoelker pid_t
vforkexecshell(struct job * jp,char ** argv,char ** envp,const char * path,int idx,int pip[2])1005caf29fabSJilles Tjoelker vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
1006caf29fabSJilles Tjoelker {
1007caf29fabSJilles Tjoelker 	pid_t pid;
1008caf29fabSJilles Tjoelker 	struct jmploc jmploc;
1009caf29fabSJilles Tjoelker 	struct jmploc *savehandler;
10106bc7175fSJilles Tjoelker 	int inton;
1011caf29fabSJilles Tjoelker 
1012b95fca47SJilles Tjoelker 	TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
1013b95fca47SJilles Tjoelker 	    (void *)pip));
10146bc7175fSJilles Tjoelker 	inton = is_int_on();
1015caf29fabSJilles Tjoelker 	INTOFF;
1016caf29fabSJilles Tjoelker 	flushall();
1017caf29fabSJilles Tjoelker 	savehandler = handler;
1018caf29fabSJilles Tjoelker 	pid = vfork();
1019caf29fabSJilles Tjoelker 	if (pid == -1) {
1020caf29fabSJilles Tjoelker 		TRACE(("Vfork failed, errno=%d\n", errno));
1021caf29fabSJilles Tjoelker 		INTON;
1022caf29fabSJilles Tjoelker 		error("Cannot fork: %s", strerror(errno));
1023caf29fabSJilles Tjoelker 	}
1024caf29fabSJilles Tjoelker 	if (pid == 0) {
1025caf29fabSJilles Tjoelker 		TRACE(("Child shell %d\n", (int)getpid()));
1026caf29fabSJilles Tjoelker 		if (setjmp(jmploc.loc))
1027bb324af6SJilles Tjoelker 			_exit(exitstatus);
1028caf29fabSJilles Tjoelker 		if (pip != NULL) {
1029caf29fabSJilles Tjoelker 			close(pip[0]);
1030caf29fabSJilles Tjoelker 			if (pip[1] != 1) {
1031caf29fabSJilles Tjoelker 				dup2(pip[1], 1);
1032caf29fabSJilles Tjoelker 				close(pip[1]);
1033caf29fabSJilles Tjoelker 			}
1034caf29fabSJilles Tjoelker 		}
1035caf29fabSJilles Tjoelker 		handler = &jmploc;
1036caf29fabSJilles Tjoelker 		shellexec(argv, envp, path, idx);
1037caf29fabSJilles Tjoelker 	}
1038caf29fabSJilles Tjoelker 	handler = savehandler;
1039caf29fabSJilles Tjoelker 	if (jp) {
1040caf29fabSJilles Tjoelker 		struct procstat *ps = &jp->ps[jp->nprocs++];
1041caf29fabSJilles Tjoelker 		ps->pid = pid;
1042caf29fabSJilles Tjoelker 		ps->status = -1;
1043caf29fabSJilles Tjoelker 		ps->cmd = nullstr;
1044caf29fabSJilles Tjoelker 		jp->foreground = 1;
1045caf29fabSJilles Tjoelker #if JOBS
1046caf29fabSJilles Tjoelker 		setcurjob(jp);
1047caf29fabSJilles Tjoelker #endif
1048caf29fabSJilles Tjoelker 	}
10496bc7175fSJilles Tjoelker 	SETINTON(inton);
1050caf29fabSJilles Tjoelker 	TRACE(("In parent shell:  child = %d\n", (int)pid));
1051caf29fabSJilles Tjoelker 	return pid;
1052caf29fabSJilles Tjoelker }
1053caf29fabSJilles Tjoelker 
10544b88c807SRodney W. Grimes 
10554b88c807SRodney W. Grimes /*
10564b88c807SRodney W. Grimes  * Wait for job to finish.
10574b88c807SRodney W. Grimes  *
10584b88c807SRodney W. Grimes  * Under job control we have the problem that while a child process is
10594b88c807SRodney W. Grimes  * running interrupts generated by the user are sent to the child but not
10604b88c807SRodney W. Grimes  * to the shell.  This means that an infinite loop started by an inter-
10614b88c807SRodney W. Grimes  * active user may be hard to kill.  With job control turned off, an
10624b88c807SRodney W. Grimes  * interactive user may place an interactive program inside a loop.  If
10634b88c807SRodney W. Grimes  * the interactive program catches interrupts, the user doesn't want
10644b88c807SRodney W. Grimes  * these interrupts to also abort the loop.  The approach we take here
10654b88c807SRodney W. Grimes  * is to have the shell ignore interrupt signals while waiting for a
106646be34b9SKris Kennaway  * foreground process to terminate, and then send itself an interrupt
10674b88c807SRodney W. Grimes  * signal if the child process was terminated by an interrupt signal.
10684b88c807SRodney W. Grimes  * Unfortunately, some programs want to do a bit of cleanup and then
10694b88c807SRodney W. Grimes  * exit on interrupt; unless these processes terminate themselves by
10704b88c807SRodney W. Grimes  * sending a signal to themselves (instead of calling exit) they will
10714b88c807SRodney W. Grimes  * confuse this approach.
10724b88c807SRodney W. Grimes  */
10734b88c807SRodney W. Grimes 
10744b88c807SRodney W. Grimes int
waitforjob(struct job * jp,int * signaled)1075c8a5f665SJilles Tjoelker waitforjob(struct job *jp, int *signaled)
10764b88c807SRodney W. Grimes {
10774b88c807SRodney W. Grimes #if JOBS
1078f3d893fcSJilles Tjoelker 	int propagate_int = jp->jobctl && jp->foreground;
10794b88c807SRodney W. Grimes #endif
10804b88c807SRodney W. Grimes 	int status;
10814b88c807SRodney W. Grimes 	int st;
10824b88c807SRodney W. Grimes 
10834b88c807SRodney W. Grimes 	INTOFF;
10848ab2e970SJohn Baldwin 	TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1085135421ddSMartin Cracauer 	while (jp->state == 0)
1086b823fb59SJilles Tjoelker 		if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
1087edebe564SJilles Tjoelker 		    DOWAIT_SIG_TRAP : 0), jp) == -1)
1088135421ddSMartin Cracauer 			dotrap();
10894b88c807SRodney W. Grimes #if JOBS
10904b88c807SRodney W. Grimes 	if (jp->jobctl) {
1091cd60e2c6SJilles Tjoelker 		if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
1092ab0a2172SSteve Price 			error("tcsetpgrp failed, errno=%d\n", errno);
10934b88c807SRodney W. Grimes 	}
10944b88c807SRodney W. Grimes 	if (jp->state == JOBSTOPPED)
1095d86825beSTim J. Robbins 		setcurjob(jp);
10964b88c807SRodney W. Grimes #endif
1097484160a9SJilles Tjoelker 	status = getjobstatus(jp);
1098c8a5f665SJilles Tjoelker 	if (signaled != NULL)
1099c8a5f665SJilles Tjoelker 		*signaled = WIFSIGNALED(status);
11004b88c807SRodney W. Grimes 	/* convert to 8 bits */
110100fc54b0SSteve Price 	if (WIFEXITED(status))
110200fc54b0SSteve Price 		st = WEXITSTATUS(status);
11034b88c807SRodney W. Grimes #if JOBS
110400fc54b0SSteve Price 	else if (WIFSTOPPED(status))
110500fc54b0SSteve Price 		st = WSTOPSIG(status) + 128;
11064b88c807SRodney W. Grimes #endif
11074b88c807SRodney W. Grimes 	else
110800fc54b0SSteve Price 		st = WTERMSIG(status) + 128;
11094b88c807SRodney W. Grimes 	if (! JOBS || jp->state == JOBDONE)
11104b88c807SRodney W. Grimes 		freejob(jp);
11117a8e920bSMartin Cracauer 	if (int_pending()) {
1112b036c75bSJilles Tjoelker 		if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
11137a8e920bSMartin Cracauer 			CLEAR_PENDING_INT;
11147a8e920bSMartin Cracauer 	}
1115f3d893fcSJilles Tjoelker #if JOBS
1116cad3cc13SJilles Tjoelker 	else if (rootshell && propagate_int &&
1117f3d893fcSJilles Tjoelker 			WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1118f3d893fcSJilles Tjoelker 		kill(getpid(), SIGINT);
1119f3d893fcSJilles Tjoelker #endif
11204b88c807SRodney W. Grimes 	INTON;
11214b88c807SRodney W. Grimes 	return st;
11224b88c807SRodney W. Grimes }
11234b88c807SRodney W. Grimes 
11244b88c807SRodney W. Grimes 
11251794251aSJilles Tjoelker static void
dummy_handler(int sig __unused)112646c6b52dSJilles Tjoelker dummy_handler(int sig __unused)
11271794251aSJilles Tjoelker {
11281794251aSJilles Tjoelker }
11294b88c807SRodney W. Grimes 
11304b88c807SRodney W. Grimes /*
11314b88c807SRodney W. Grimes  * Wait for a process to terminate.
11324b88c807SRodney W. Grimes  */
11334b88c807SRodney W. Grimes 
113488328642SDavid E. O'Brien static pid_t
dowait(int mode,struct job * job)11351794251aSJilles Tjoelker dowait(int mode, struct job *job)
11364b88c807SRodney W. Grimes {
11371794251aSJilles Tjoelker 	struct sigaction sa, osa;
11381794251aSJilles Tjoelker 	sigset_t mask, omask;
1139b80be282STim J. Robbins 	pid_t pid;
11404b88c807SRodney W. Grimes 	int status;
11414b88c807SRodney W. Grimes 	struct procstat *sp;
11424b88c807SRodney W. Grimes 	struct job *jp;
11434b88c807SRodney W. Grimes 	struct job *thisjob;
1144cdbd40cbSJilles Tjoelker 	const char *sigstr;
11454b88c807SRodney W. Grimes 	int done;
11464b88c807SRodney W. Grimes 	int stopped;
114700fc54b0SSteve Price 	int sig;
11481bb49f95SJilles Tjoelker 	int coredump;
114961fb716aSJilles Tjoelker 	int wflags;
11501794251aSJilles Tjoelker 	int restore_sigchld;
11514b88c807SRodney W. Grimes 
11525be97e72SXin LI 	TRACE(("dowait(%d, %p) called\n", mode, job));
11531794251aSJilles Tjoelker 	restore_sigchld = 0;
11541794251aSJilles Tjoelker 	if ((mode & DOWAIT_SIG) != 0) {
11551794251aSJilles Tjoelker 		sigfillset(&mask);
11561794251aSJilles Tjoelker 		sigprocmask(SIG_BLOCK, &mask, &omask);
11571794251aSJilles Tjoelker 		INTOFF;
11581794251aSJilles Tjoelker 		if (!issigchldtrapped()) {
11591794251aSJilles Tjoelker 			restore_sigchld = 1;
11601794251aSJilles Tjoelker 			sa.sa_handler = dummy_handler;
11611794251aSJilles Tjoelker 			sa.sa_flags = 0;
11621794251aSJilles Tjoelker 			sigemptyset(&sa.sa_mask);
11631794251aSJilles Tjoelker 			sigaction(SIGCHLD, &sa, &osa);
11641794251aSJilles Tjoelker 		}
11651794251aSJilles Tjoelker 	}
11664b88c807SRodney W. Grimes 	do {
116761fb716aSJilles Tjoelker #if JOBS
1168fb05913cSJilles Tjoelker 		if (iflag)
116961fb716aSJilles Tjoelker 			wflags = WUNTRACED | WCONTINUED;
1170fb05913cSJilles Tjoelker 		else
117161fb716aSJilles Tjoelker #endif
1172fb05913cSJilles Tjoelker 			wflags = 0;
11731794251aSJilles Tjoelker 		if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
117461fb716aSJilles Tjoelker 			wflags |= WNOHANG;
117561fb716aSJilles Tjoelker 		pid = wait3(&status, wflags, (struct rusage *)NULL);
1176b80be282STim J. Robbins 		TRACE(("wait returns %d, status=%d\n", (int)pid, status));
11771794251aSJilles Tjoelker 		if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
11781794251aSJilles Tjoelker 			pid = -1;
1179edebe564SJilles Tjoelker 			if (((mode & DOWAIT_SIG_TRAP) != 0 ?
1180b823fb59SJilles Tjoelker 			    pendingsig : pendingsig_waitcmd) != 0) {
1181b823fb59SJilles Tjoelker 				errno = EINTR;
1182b823fb59SJilles Tjoelker 				break;
1183b823fb59SJilles Tjoelker 			}
1184b823fb59SJilles Tjoelker 			sigsuspend(&omask);
11851794251aSJilles Tjoelker 			if (int_pending())
11861794251aSJilles Tjoelker 				break;
11871794251aSJilles Tjoelker 		}
1188b823fb59SJilles Tjoelker 	} while (pid == -1 && errno == EINTR);
118919d099fcSMaxim Konovalov 	if (pid == -1 && errno == ECHILD && job != NULL)
119019d099fcSMaxim Konovalov 		job->state = JOBDONE;
11911794251aSJilles Tjoelker 	if ((mode & DOWAIT_SIG) != 0) {
11921794251aSJilles Tjoelker 		if (restore_sigchld)
11931794251aSJilles Tjoelker 			sigaction(SIGCHLD, &osa, NULL);
11941794251aSJilles Tjoelker 		sigprocmask(SIG_SETMASK, &omask, NULL);
11951794251aSJilles Tjoelker 		INTON;
11961794251aSJilles Tjoelker 	}
11974b88c807SRodney W. Grimes 	if (pid <= 0)
11984b88c807SRodney W. Grimes 		return pid;
11994b88c807SRodney W. Grimes 	INTOFF;
12004b88c807SRodney W. Grimes 	thisjob = NULL;
12014b88c807SRodney W. Grimes 	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1202ff304d37SJilles Tjoelker 		if (jp->used && jp->nprocs > 0) {
12034b88c807SRodney W. Grimes 			done = 1;
12044b88c807SRodney W. Grimes 			stopped = 1;
12054b88c807SRodney W. Grimes 			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
12064b88c807SRodney W. Grimes 				if (sp->pid == -1)
12074b88c807SRodney W. Grimes 					continue;
1208728e552fSJilles Tjoelker 				if (sp->pid == pid && (sp->status == -1 ||
1209728e552fSJilles Tjoelker 				    WIFSTOPPED(sp->status))) {
121000fc54b0SSteve Price 					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1211b80be282STim J. Robbins 						   (int)pid, sp->status,
1212b80be282STim J. Robbins 						   status));
1213faa787d3SJilles Tjoelker 					if (WIFCONTINUED(status)) {
1214faa787d3SJilles Tjoelker 						sp->status = -1;
1215faa787d3SJilles Tjoelker 						jp->state = 0;
1216faa787d3SJilles Tjoelker 					} else
12174b88c807SRodney W. Grimes 						sp->status = status;
12184b88c807SRodney W. Grimes 					thisjob = jp;
12194b88c807SRodney W. Grimes 				}
12204b88c807SRodney W. Grimes 				if (sp->status == -1)
12214b88c807SRodney W. Grimes 					stopped = 0;
122200fc54b0SSteve Price 				else if (WIFSTOPPED(sp->status))
12234b88c807SRodney W. Grimes 					done = 0;
12244b88c807SRodney W. Grimes 			}
12254b88c807SRodney W. Grimes 			if (stopped) {		/* stopped or done */
12264b88c807SRodney W. Grimes 				int state = done? JOBDONE : JOBSTOPPED;
12274b88c807SRodney W. Grimes 				if (jp->state != state) {
12288ab2e970SJohn Baldwin 					TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
12294b88c807SRodney W. Grimes 					jp->state = state;
1230ed4c3b5fSJilles Tjoelker 					if (jp != job) {
1231ed4c3b5fSJilles Tjoelker 						if (done && !jp->remembered &&
1232ed4c3b5fSJilles Tjoelker 						    !iflag && jp != bgjob)
1233ed4c3b5fSJilles Tjoelker 							freejob(jp);
12344b88c807SRodney W. Grimes #if JOBS
1235ed4c3b5fSJilles Tjoelker 						else if (done)
1236d86825beSTim J. Robbins 							deljob(jp);
12374b88c807SRodney W. Grimes #endif
12384b88c807SRodney W. Grimes 					}
12394b88c807SRodney W. Grimes 				}
12404b88c807SRodney W. Grimes 			}
12414b88c807SRodney W. Grimes 		}
1242ed4c3b5fSJilles Tjoelker 	}
12434b88c807SRodney W. Grimes 	INTON;
12441bb49f95SJilles Tjoelker 	if (!thisjob || thisjob->state == 0)
12451bb49f95SJilles Tjoelker 		;
12461bb49f95SJilles Tjoelker 	else if ((!rootshell || !iflag || thisjob == job) &&
12471bb49f95SJilles Tjoelker 	    thisjob->foreground && thisjob->state != JOBSTOPPED) {
124800fc54b0SSteve Price 		sig = 0;
12491bb49f95SJilles Tjoelker 		coredump = 0;
12501bb49f95SJilles Tjoelker 		for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
12511bb49f95SJilles Tjoelker 			if (WIFSIGNALED(sp->status)) {
12521bb49f95SJilles Tjoelker 				sig = WTERMSIG(sp->status);
12531bb49f95SJilles Tjoelker 				coredump = WCOREDUMP(sp->status);
12544b88c807SRodney W. Grimes 			}
12551bb49f95SJilles Tjoelker 		if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1256cdbd40cbSJilles Tjoelker 			sigstr = strsignal(sig);
1257cdbd40cbSJilles Tjoelker 			if (sigstr != NULL)
1258cdbd40cbSJilles Tjoelker 				out2str(sigstr);
1259f1ae2c66STim J. Robbins 			else
1260cdbd40cbSJilles Tjoelker 				out2str("Unknown signal");
12611bb49f95SJilles Tjoelker 			if (coredump)
1262b9f69695SJilles Tjoelker 				out2str(" (core dumped)");
1263b9f69695SJilles Tjoelker 			out2c('\n');
1264b9f69695SJilles Tjoelker 			flushout(out2);
1265f1ae2c66STim J. Robbins 		}
12664b88c807SRodney W. Grimes 	} else {
12676da31df8STim J. Robbins 		TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
12684b88c807SRodney W. Grimes 		thisjob->changed = 1;
12694b88c807SRodney W. Grimes 	}
12704b88c807SRodney W. Grimes 	return pid;
12714b88c807SRodney W. Grimes }
12724b88c807SRodney W. Grimes 
12734b88c807SRodney W. Grimes 
12744b88c807SRodney W. Grimes 
12754b88c807SRodney W. Grimes /*
12764b88c807SRodney W. Grimes  * return 1 if there are stopped jobs, otherwise 0
12774b88c807SRodney W. Grimes  */
12784b88c807SRodney W. Grimes int job_warning = 0;
12794b88c807SRodney W. Grimes int
stoppedjobs(void)12805134c3f7SWarner Losh stoppedjobs(void)
12814b88c807SRodney W. Grimes {
1282afb033d5SSteve Price 	int jobno;
1283afb033d5SSteve Price 	struct job *jp;
12844b88c807SRodney W. Grimes 
12854b88c807SRodney W. Grimes 	if (job_warning)
12864b88c807SRodney W. Grimes 		return (0);
12874b88c807SRodney W. Grimes 	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
12884b88c807SRodney W. Grimes 		if (jp->used == 0)
12894b88c807SRodney W. Grimes 			continue;
12904b88c807SRodney W. Grimes 		if (jp->state == JOBSTOPPED) {
1291c6204d4aSJilles Tjoelker 			out2fmt_flush("You have stopped jobs.\n");
12924b88c807SRodney W. Grimes 			job_warning = 2;
12934b88c807SRodney W. Grimes 			return (1);
12944b88c807SRodney W. Grimes 		}
12954b88c807SRodney W. Grimes 	}
12964b88c807SRodney W. Grimes 
12974b88c807SRodney W. Grimes 	return (0);
12984b88c807SRodney W. Grimes }
12994b88c807SRodney W. Grimes 
13001f65c541SJilles Tjoelker 
130188328642SDavid E. O'Brien static void
checkzombies(void)13021f65c541SJilles Tjoelker checkzombies(void)
13031f65c541SJilles Tjoelker {
13041f65c541SJilles Tjoelker 	while (njobs > 0 && dowait(0, NULL) > 0)
13051f65c541SJilles Tjoelker 		;
13061f65c541SJilles Tjoelker }
13071f65c541SJilles Tjoelker 
13081f65c541SJilles Tjoelker 
1309ed4c3b5fSJilles Tjoelker int
backgndpidset(void)1310ed4c3b5fSJilles Tjoelker backgndpidset(void)
1311ed4c3b5fSJilles Tjoelker {
1312ed4c3b5fSJilles Tjoelker 	return backgndpid != -1;
1313ed4c3b5fSJilles Tjoelker }
1314ed4c3b5fSJilles Tjoelker 
1315ed4c3b5fSJilles Tjoelker 
1316ed4c3b5fSJilles Tjoelker pid_t
backgndpidval(void)1317ed4c3b5fSJilles Tjoelker backgndpidval(void)
1318ed4c3b5fSJilles Tjoelker {
1319c543e1aeSJilles Tjoelker 	if (bgjob != NULL && !forcelocal)
1320ed4c3b5fSJilles Tjoelker 		bgjob->remembered = 1;
1321ed4c3b5fSJilles Tjoelker 	return backgndpid;
1322ed4c3b5fSJilles Tjoelker }
1323ed4c3b5fSJilles Tjoelker 
13244b88c807SRodney W. Grimes /*
13254b88c807SRodney W. Grimes  * Return a string identifying a command (to be printed by the
13264b88c807SRodney W. Grimes  * jobs command.
13274b88c807SRodney W. Grimes  */
13284b88c807SRodney W. Grimes 
1329aa7b6f82SDavid E. O'Brien static char *cmdnextc;
1330aa7b6f82SDavid E. O'Brien static int cmdnleft;
13314b88c807SRodney W. Grimes #define MAXCMDTEXT	200
13324b88c807SRodney W. Grimes 
13334b88c807SRodney W. Grimes char *
commandtext(union node * n)13345134c3f7SWarner Losh commandtext(union node *n)
13354b88c807SRodney W. Grimes {
13364b88c807SRodney W. Grimes 	char *name;
13374b88c807SRodney W. Grimes 
13384b88c807SRodney W. Grimes 	cmdnextc = name = ckmalloc(MAXCMDTEXT);
13394b88c807SRodney W. Grimes 	cmdnleft = MAXCMDTEXT - 4;
13404b88c807SRodney W. Grimes 	cmdtxt(n);
13414b88c807SRodney W. Grimes 	*cmdnextc = '\0';
13424b88c807SRodney W. Grimes 	return name;
13434b88c807SRodney W. Grimes }
13444b88c807SRodney W. Grimes 
13454b88c807SRodney W. Grimes 
134688328642SDavid E. O'Brien static void
cmdtxtdogroup(union node * n)1347622fdf32SJilles Tjoelker cmdtxtdogroup(union node *n)
1348622fdf32SJilles Tjoelker {
1349622fdf32SJilles Tjoelker 	cmdputs("; do ");
1350622fdf32SJilles Tjoelker 	cmdtxt(n);
1351622fdf32SJilles Tjoelker 	cmdputs("; done");
1352622fdf32SJilles Tjoelker }
1353622fdf32SJilles Tjoelker 
1354622fdf32SJilles Tjoelker 
1355622fdf32SJilles Tjoelker static void
cmdtxtredir(union node * n,const char * op,int deffd)1356622fdf32SJilles Tjoelker cmdtxtredir(union node *n, const char *op, int deffd)
1357622fdf32SJilles Tjoelker {
1358622fdf32SJilles Tjoelker 	char s[2];
1359622fdf32SJilles Tjoelker 
1360622fdf32SJilles Tjoelker 	if (n->nfile.fd != deffd) {
1361622fdf32SJilles Tjoelker 		s[0] = n->nfile.fd + '0';
1362622fdf32SJilles Tjoelker 		s[1] = '\0';
1363622fdf32SJilles Tjoelker 		cmdputs(s);
1364622fdf32SJilles Tjoelker 	}
1365622fdf32SJilles Tjoelker 	cmdputs(op);
1366622fdf32SJilles Tjoelker 	if (n->type == NTOFD || n->type == NFROMFD) {
1367622fdf32SJilles Tjoelker 		if (n->ndup.dupfd >= 0)
1368622fdf32SJilles Tjoelker 			s[0] = n->ndup.dupfd + '0';
1369622fdf32SJilles Tjoelker 		else
1370622fdf32SJilles Tjoelker 			s[0] = '-';
1371622fdf32SJilles Tjoelker 		s[1] = '\0';
1372622fdf32SJilles Tjoelker 		cmdputs(s);
1373622fdf32SJilles Tjoelker 	} else {
1374622fdf32SJilles Tjoelker 		cmdtxt(n->nfile.fname);
1375622fdf32SJilles Tjoelker 	}
1376622fdf32SJilles Tjoelker }
1377622fdf32SJilles Tjoelker 
1378622fdf32SJilles Tjoelker 
1379622fdf32SJilles Tjoelker static void
cmdtxt(union node * n)13805134c3f7SWarner Losh cmdtxt(union node *n)
13814b88c807SRodney W. Grimes {
13824b88c807SRodney W. Grimes 	union node *np;
13834b88c807SRodney W. Grimes 	struct nodelist *lp;
13844b88c807SRodney W. Grimes 
13854b88c807SRodney W. Grimes 	if (n == NULL)
13864b88c807SRodney W. Grimes 		return;
13874b88c807SRodney W. Grimes 	switch (n->type) {
13884b88c807SRodney W. Grimes 	case NSEMI:
13894b88c807SRodney W. Grimes 		cmdtxt(n->nbinary.ch1);
13904b88c807SRodney W. Grimes 		cmdputs("; ");
13914b88c807SRodney W. Grimes 		cmdtxt(n->nbinary.ch2);
13924b88c807SRodney W. Grimes 		break;
13934b88c807SRodney W. Grimes 	case NAND:
13944b88c807SRodney W. Grimes 		cmdtxt(n->nbinary.ch1);
13954b88c807SRodney W. Grimes 		cmdputs(" && ");
13964b88c807SRodney W. Grimes 		cmdtxt(n->nbinary.ch2);
13974b88c807SRodney W. Grimes 		break;
13984b88c807SRodney W. Grimes 	case NOR:
13994b88c807SRodney W. Grimes 		cmdtxt(n->nbinary.ch1);
14004b88c807SRodney W. Grimes 		cmdputs(" || ");
14014b88c807SRodney W. Grimes 		cmdtxt(n->nbinary.ch2);
14024b88c807SRodney W. Grimes 		break;
14034b88c807SRodney W. Grimes 	case NPIPE:
14044b88c807SRodney W. Grimes 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
14054b88c807SRodney W. Grimes 			cmdtxt(lp->n);
14064b88c807SRodney W. Grimes 			if (lp->next)
14074b88c807SRodney W. Grimes 				cmdputs(" | ");
14084b88c807SRodney W. Grimes 		}
14094b88c807SRodney W. Grimes 		break;
14104b88c807SRodney W. Grimes 	case NSUBSHELL:
14114b88c807SRodney W. Grimes 		cmdputs("(");
14124b88c807SRodney W. Grimes 		cmdtxt(n->nredir.n);
14134b88c807SRodney W. Grimes 		cmdputs(")");
14144b88c807SRodney W. Grimes 		break;
14154b88c807SRodney W. Grimes 	case NREDIR:
14164b88c807SRodney W. Grimes 	case NBACKGND:
14174b88c807SRodney W. Grimes 		cmdtxt(n->nredir.n);
14184b88c807SRodney W. Grimes 		break;
14194b88c807SRodney W. Grimes 	case NIF:
14204b88c807SRodney W. Grimes 		cmdputs("if ");
14214b88c807SRodney W. Grimes 		cmdtxt(n->nif.test);
14224b88c807SRodney W. Grimes 		cmdputs("; then ");
14234b88c807SRodney W. Grimes 		cmdtxt(n->nif.ifpart);
14244b88c807SRodney W. Grimes 		cmdputs("...");
14254b88c807SRodney W. Grimes 		break;
14264b88c807SRodney W. Grimes 	case NWHILE:
14274b88c807SRodney W. Grimes 		cmdputs("while ");
1428622fdf32SJilles Tjoelker 		cmdtxt(n->nbinary.ch1);
1429622fdf32SJilles Tjoelker 		cmdtxtdogroup(n->nbinary.ch2);
1430622fdf32SJilles Tjoelker 		break;
14314b88c807SRodney W. Grimes 	case NUNTIL:
14324b88c807SRodney W. Grimes 		cmdputs("until ");
14334b88c807SRodney W. Grimes 		cmdtxt(n->nbinary.ch1);
1434622fdf32SJilles Tjoelker 		cmdtxtdogroup(n->nbinary.ch2);
14354b88c807SRodney W. Grimes 		break;
14364b88c807SRodney W. Grimes 	case NFOR:
14374b88c807SRodney W. Grimes 		cmdputs("for ");
14384b88c807SRodney W. Grimes 		cmdputs(n->nfor.var);
14394b88c807SRodney W. Grimes 		cmdputs(" in ...");
14404b88c807SRodney W. Grimes 		break;
14414b88c807SRodney W. Grimes 	case NCASE:
14424b88c807SRodney W. Grimes 		cmdputs("case ");
14434b88c807SRodney W. Grimes 		cmdputs(n->ncase.expr->narg.text);
14444b88c807SRodney W. Grimes 		cmdputs(" in ...");
14454b88c807SRodney W. Grimes 		break;
14464b88c807SRodney W. Grimes 	case NDEFUN:
14474b88c807SRodney W. Grimes 		cmdputs(n->narg.text);
14484b88c807SRodney W. Grimes 		cmdputs("() ...");
14494b88c807SRodney W. Grimes 		break;
1450f9b7cc52SJilles Tjoelker 	case NNOT:
1451f9b7cc52SJilles Tjoelker 		cmdputs("! ");
1452f9b7cc52SJilles Tjoelker 		cmdtxt(n->nnot.com);
1453f9b7cc52SJilles Tjoelker 		break;
14544b88c807SRodney W. Grimes 	case NCMD:
14554b88c807SRodney W. Grimes 		for (np = n->ncmd.args ; np ; np = np->narg.next) {
14564b88c807SRodney W. Grimes 			cmdtxt(np);
14574b88c807SRodney W. Grimes 			if (np->narg.next)
14584b88c807SRodney W. Grimes 				cmdputs(" ");
14594b88c807SRodney W. Grimes 		}
14604b88c807SRodney W. Grimes 		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
14614b88c807SRodney W. Grimes 			cmdputs(" ");
14624b88c807SRodney W. Grimes 			cmdtxt(np);
14634b88c807SRodney W. Grimes 		}
14644b88c807SRodney W. Grimes 		break;
14654b88c807SRodney W. Grimes 	case NARG:
14664b88c807SRodney W. Grimes 		cmdputs(n->narg.text);
14674b88c807SRodney W. Grimes 		break;
14684b88c807SRodney W. Grimes 	case NTO:
1469622fdf32SJilles Tjoelker 		cmdtxtredir(n, ">", 1);
1470622fdf32SJilles Tjoelker 		break;
14714b88c807SRodney W. Grimes 	case NAPPEND:
1472622fdf32SJilles Tjoelker 		cmdtxtredir(n, ">>", 1);
1473622fdf32SJilles Tjoelker 		break;
14744b88c807SRodney W. Grimes 	case NTOFD:
1475622fdf32SJilles Tjoelker 		cmdtxtredir(n, ">&", 1);
1476622fdf32SJilles Tjoelker 		break;
14771a958c66STim J. Robbins 	case NCLOBBER:
1478622fdf32SJilles Tjoelker 		cmdtxtredir(n, ">|", 1);
1479622fdf32SJilles Tjoelker 		break;
14804b88c807SRodney W. Grimes 	case NFROM:
1481622fdf32SJilles Tjoelker 		cmdtxtredir(n, "<", 0);
1482622fdf32SJilles Tjoelker 		break;
14834682f420SBrian Somers 	case NFROMTO:
1484622fdf32SJilles Tjoelker 		cmdtxtredir(n, "<>", 0);
1485622fdf32SJilles Tjoelker 		break;
14864b88c807SRodney W. Grimes 	case NFROMFD:
1487622fdf32SJilles Tjoelker 		cmdtxtredir(n, "<&", 0);
14884b88c807SRodney W. Grimes 		break;
14894b88c807SRodney W. Grimes 	case NHERE:
14904b88c807SRodney W. Grimes 	case NXHERE:
14914b88c807SRodney W. Grimes 		cmdputs("<<...");
14924b88c807SRodney W. Grimes 		break;
14934b88c807SRodney W. Grimes 	default:
14944b88c807SRodney W. Grimes 		cmdputs("???");
14954b88c807SRodney W. Grimes 		break;
14964b88c807SRodney W. Grimes 	}
14974b88c807SRodney W. Grimes }
14984b88c807SRodney W. Grimes 
14994b88c807SRodney W. Grimes 
15004b88c807SRodney W. Grimes 
150188328642SDavid E. O'Brien static void
cmdputs(const char * s)1502384aedabSJilles Tjoelker cmdputs(const char *s)
15034b88c807SRodney W. Grimes {
1504384aedabSJilles Tjoelker 	const char *p;
1505384aedabSJilles Tjoelker 	char *q;
1506afb033d5SSteve Price 	char c;
15074b88c807SRodney W. Grimes 	int subtype = 0;
15084b88c807SRodney W. Grimes 
15094b88c807SRodney W. Grimes 	if (cmdnleft <= 0)
15104b88c807SRodney W. Grimes 		return;
15114b88c807SRodney W. Grimes 	p = s;
15124b88c807SRodney W. Grimes 	q = cmdnextc;
15134b88c807SRodney W. Grimes 	while ((c = *p++) != '\0') {
15144b88c807SRodney W. Grimes 		if (c == CTLESC)
15154b88c807SRodney W. Grimes 			*q++ = *p++;
15164b88c807SRodney W. Grimes 		else if (c == CTLVAR) {
15174b88c807SRodney W. Grimes 			*q++ = '$';
15184b88c807SRodney W. Grimes 			if (--cmdnleft > 0)
15194b88c807SRodney W. Grimes 				*q++ = '{';
15204b88c807SRodney W. Grimes 			subtype = *p++;
1521c67712a0SJilles Tjoelker 			if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1522c67712a0SJilles Tjoelker 				*q++ = '#';
15234b88c807SRodney W. Grimes 		} else if (c == '=' && subtype != 0) {
1524c67712a0SJilles Tjoelker 			*q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1525c67712a0SJilles Tjoelker 			if (*q)
1526c67712a0SJilles Tjoelker 				q++;
1527c67712a0SJilles Tjoelker 			else
1528c67712a0SJilles Tjoelker 				cmdnleft++;
1529c67712a0SJilles Tjoelker 			if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1530c67712a0SJilles Tjoelker 			    (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1531c67712a0SJilles Tjoelker 			    --cmdnleft > 0)
1532c67712a0SJilles Tjoelker 				*q = q[-1], q++;
15334b88c807SRodney W. Grimes 			subtype = 0;
15344b88c807SRodney W. Grimes 		} else if (c == CTLENDVAR) {
15354b88c807SRodney W. Grimes 			*q++ = '}';
1536c67712a0SJilles Tjoelker 		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1537c67712a0SJilles Tjoelker 			cmdnleft -= 5;
1538c67712a0SJilles Tjoelker 			if (cmdnleft > 0) {
1539c67712a0SJilles Tjoelker 				*q++ = '$';
1540c67712a0SJilles Tjoelker 				*q++ = '(';
1541c67712a0SJilles Tjoelker 				*q++ = '.';
1542c67712a0SJilles Tjoelker 				*q++ = '.';
1543c67712a0SJilles Tjoelker 				*q++ = '.';
1544c67712a0SJilles Tjoelker 				*q++ = ')';
1545c67712a0SJilles Tjoelker 			}
1546c67712a0SJilles Tjoelker 		} else if (c == CTLARI) {
1547c67712a0SJilles Tjoelker 			cmdnleft -= 2;
1548c67712a0SJilles Tjoelker 			if (cmdnleft > 0) {
1549c67712a0SJilles Tjoelker 				*q++ = '$';
1550c67712a0SJilles Tjoelker 				*q++ = '(';
1551c67712a0SJilles Tjoelker 				*q++ = '(';
1552c67712a0SJilles Tjoelker 			}
1553c67712a0SJilles Tjoelker 			p++;
1554c67712a0SJilles Tjoelker 		} else if (c == CTLENDARI) {
1555c67712a0SJilles Tjoelker 			if (--cmdnleft > 0) {
1556c67712a0SJilles Tjoelker 				*q++ = ')';
1557c67712a0SJilles Tjoelker 				*q++ = ')';
1558c67712a0SJilles Tjoelker 			}
1559c67712a0SJilles Tjoelker 		} else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1560c67712a0SJilles Tjoelker 			cmdnleft++; /* ignore */
15614b88c807SRodney W. Grimes 		else
15624b88c807SRodney W. Grimes 			*q++ = c;
15634b88c807SRodney W. Grimes 		if (--cmdnleft <= 0) {
15644b88c807SRodney W. Grimes 			*q++ = '.';
15654b88c807SRodney W. Grimes 			*q++ = '.';
15664b88c807SRodney W. Grimes 			*q++ = '.';
15674b88c807SRodney W. Grimes 			break;
15684b88c807SRodney W. Grimes 		}
15694b88c807SRodney W. Grimes 	}
15704b88c807SRodney W. Grimes 	cmdnextc = q;
15714b88c807SRodney W. Grimes }
1572