2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/ioctl.h>
42 #include <sys/param.h>
43 #include <sys/resource.h>
57 #undef CEOF /* syntax.h redefines this */
79 * A job structure contains information about a job. A job is either a
80 * single process or a set of processes contained in a pipeline. In the
81 * latter case, pidlist will be non-NULL, and will point to a -1 terminated
86 pid_t pid; /* process id */
87 int status; /* status flags (defined above) */
88 char *cmd; /* text of command being run */
93 #define JOBSTOPPED 1 /* all procs are stopped */
94 #define JOBDONE 2 /* all procs are completed */
98 struct procstat ps0; /* status of process */
99 struct procstat *ps; /* status or processes when more than one */
100 short nprocs; /* number of processes */
101 pid_t pgrp; /* process group of this job */
102 char state; /* true if job is finished */
103 char used; /* true if this entry is in used */
104 char changed; /* true if status has changed */
105 char foreground; /* true if running in the foreground */
106 char remembered; /* true if $! referenced */
108 char jobctl; /* job running under job control */
109 struct job *next; /* job used after this one */
114 static struct job *jobtab; /* array of jobs */
115 static int njobs; /* size of array */
116 static pid_t backgndpid = -1; /* pid of last background process */
117 static struct job *bgjob = NULL; /* last background process */
119 static struct job *jobmru; /* most recently used job list */
120 static pid_t initialpgrp; /* pgrp of shell on invocation */
122 static int ttyfd = -1;
124 /* mode flags for dowait */
125 #define DOWAIT_BLOCK 0x1 /* wait until a child exits */
126 #define DOWAIT_SIG 0x2 /* if DOWAIT_BLOCK, abort on signal */
127 #define DOWAIT_SIG_TRAP 0x4 /* if DOWAIT_SIG, abort on trapped signal only */
130 static void restartjob(struct job *);
132 static void freejob(struct job *);
133 static int waitcmdloop(struct job *);
134 static struct job *getjob_nonotfound(const char *);
135 static struct job *getjob(const char *);
136 pid_t killjob(const char *, int);
137 static pid_t dowait(int, struct job *);
138 static void checkzombies(void);
139 static void cmdtxt(union node *);
140 static void cmdputs(const char *);
142 static void setcurjob(struct job *);
143 static void deljob(struct job *);
144 static struct job *getcurjob(struct job *);
146 static void printjobcmd(struct job *);
147 static void showjob(struct job *, int);
151 * Turn job control on and off.
171 out2fmt_flush("sh: can't access tty; job control turned off\n");
180 if (on == jobctl || rootshell == 0)
185 if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
187 while (i <= 2 && !isatty(i))
190 (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) {
197 * Keep our TTY file descriptor out of the way of
198 * the user's redirections.
200 if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
207 do { /* while we are in the background */
208 initialpgrp = tcgetpgrp(ttyfd);
209 if (initialpgrp < 0) {
213 if (initialpgrp != getpgrp()) {
227 tcsetpgrp(ttyfd, rootpid);
228 } else { /* turning job control off */
229 setpgid(0, initialpgrp);
231 tcsetpgrp(ttyfd, initialpgrp);
246 fgcmd(int argc __unused, char **argv __unused)
253 jp = getjob(*argptr);
255 error("job not created under job control");
258 pgrp = jp->ps[0].pid;
260 tcsetpgrp(ttyfd, pgrp);
264 status = waitforjob(jp, (int *)NULL);
271 bgcmd(int argc __unused, char **argv __unused)
277 jp = getjob(*argptr);
279 error("job not created under job control");
280 if (jp->state == JOBDONE)
284 out1fmt("[%td] ", jp - jobtab + 1);
286 } while (*argptr != NULL && *++argptr != NULL);
292 restartjob(struct job *jp)
297 if (jp->state == JOBDONE)
301 kill(-jp->ps[0].pid, SIGCONT);
302 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
303 if (WIFSTOPPED(ps->status)) {
314 jobscmd(int argc __unused, char *argv[] __unused)
319 mode = SHOWJOBS_DEFAULT;
320 while ((ch = nextopt("lps")) != '\0') {
323 mode = SHOWJOBS_VERBOSE;
326 mode = SHOWJOBS_PGIDS;
329 mode = SHOWJOBS_PIDS;
337 while ((id = *argptr++) != NULL)
338 showjob(getjob(id), mode);
344 printjobcmd(struct job *jp)
349 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
358 showjob(struct job *jp, int mode)
362 const char *statestr, *coredump;
365 int col, curr, i, jobno, prev, procno;
368 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
369 jobno = jp - jobtab + 1;
372 if ((j = getcurjob(NULL)) != NULL) {
373 curr = j - jobtab + 1;
374 if ((j = getcurjob(j)) != NULL)
375 prev = j - jobtab + 1;
379 ps = jp->ps + jp->nprocs - 1;
380 if (jp->state == 0) {
381 statestr = "Running";
383 } else if (jp->state == JOBSTOPPED) {
384 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
386 if (WIFSTOPPED(ps->status))
387 i = WSTOPSIG(ps->status);
390 statestr = strsignal(i);
391 if (statestr == NULL)
392 statestr = "Suspended";
394 } else if (WIFEXITED(ps->status)) {
395 if (WEXITSTATUS(ps->status) == 0)
398 fmtstr(statebuf, sizeof(statebuf), "Done(%d)",
399 WEXITSTATUS(ps->status));
403 i = WTERMSIG(ps->status);
404 statestr = strsignal(i);
405 if (statestr == NULL)
406 statestr = "Unknown signal";
407 if (WCOREDUMP(ps->status))
408 coredump = " (core dumped)";
411 for (ps = jp->ps ; procno > 0 ; ps++, procno--) { /* for each process */
412 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
413 out1fmt("%d\n", (int)ps->pid);
416 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
418 if (jobno == curr && ps == jp->ps)
420 else if (jobno == prev && ps == jp->ps)
425 fmtstr(s, 64, "[%d] %c ", jobno, c);
427 fmtstr(s, 64, " %c ", c);
430 if (mode == SHOWJOBS_VERBOSE) {
431 fmtstr(s, 64, "%d ", (int)ps->pid);
438 col += strlen(statestr) + strlen(coredump);
444 if (mode == SHOWJOBS_VERBOSE) {
453 * Print a list of jobs. If "change" is nonzero, only print jobs whose
454 * statuses have changed since the last call to showjobs.
456 * If the shell is interrupted in the process of creating a job, the
457 * result may be a job structure containing zero processes. Such structures
458 * will be freed here.
462 showjobs(int change, int mode)
467 TRACE(("showjobs(%d) called\n", change));
469 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
472 if (jp->nprocs == 0) {
476 if (change && ! jp->changed)
479 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
481 /* Hack: discard jobs for which $! has not been
482 * referenced in interactive mode when they terminate.
484 if (jp->state == JOBDONE && !jp->remembered &&
485 (iflag || jp != bgjob)) {
494 * Mark a job structure as unused.
498 freejob(struct job *jp)
506 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
507 if (ps->cmd != nullstr)
510 if (jp->ps != &jp->ps0)
522 waitcmd(int argc __unused, char **argv __unused)
529 return (waitcmdloop(NULL));
532 job = getjob_nonotfound(*argptr);
536 retval = waitcmdloop(job);
538 } while (*argptr != NULL);
544 waitcmdloop(struct job *job)
546 int status, retval, sig;
550 * Loop until a process is terminated or stopped, or a SIGINT is
556 if (job->state == JOBDONE) {
557 status = job->ps[job->nprocs - 1].status;
558 if (WIFEXITED(status))
559 retval = WEXITSTATUS(status);
561 retval = WTERMSIG(status) + 128;
562 if (! iflag || ! job->changed)
572 for (jp = jobtab ; jp < jobtab + njobs; jp++)
573 if (jp->used && jp->state == JOBDONE) {
574 if (! iflag || ! jp->changed)
582 for (jp = jobtab ; ; jp++) {
583 if (jp >= jobtab + njobs) { /* no running procs */
586 if (jp->used && jp->state == 0)
590 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
592 sig = pendingsig_waitcmd;
593 pendingsig_waitcmd = 0;
600 jobidcmd(int argc __unused, char **argv __unused)
606 jp = getjob(*argptr);
607 for (i = 0 ; i < jp->nprocs ; ) {
608 out1fmt("%d", (int)jp->ps[i].pid);
609 out1c(++i < jp->nprocs? ' ' : '\n');
617 * Convert a job name to a job structure.
621 getjob_nonotfound(const char *name)
624 struct job *found, *jp;
633 error("No current job");
636 if (name[0] == '%') {
637 if (is_digit(name[1])) {
638 jobno = number(name + 1);
639 if (jobno > 0 && jobno <= njobs
640 && jobtab[jobno - 1].used != 0)
641 return &jobtab[jobno - 1];
643 } else if ((name[1] == '%' || name[1] == '+') &&
645 if ((jp = getcurjob(NULL)) == NULL)
646 error("No current job");
648 } else if (name[1] == '-' && name[2] == '\0') {
649 if ((jp = getcurjob(NULL)) == NULL ||
650 (jp = getcurjob(jp)) == NULL)
651 error("No previous job");
654 } else if (name[1] == '?') {
656 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
657 if (jp->used && jp->nprocs > 0
658 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
660 error("%s: ambiguous", name);
667 namelen = strlen(name);
669 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
670 if (jp->used && jp->nprocs > 0
671 && strncmp(jp->ps[0].cmd, name + 1,
674 error("%s: ambiguous", name);
681 } else if (is_number(name)) {
682 pid = (pid_t)number(name);
683 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
684 if (jp->used && jp->nprocs > 0
685 && jp->ps[jp->nprocs - 1].pid == pid)
694 getjob(const char *name)
698 jp = getjob_nonotfound(name);
700 error("No such job: %s", name);
706 killjob(const char *name, int sig)
712 if (jp->state == JOBDONE)
715 return kill(-jp->ps[0].pid, sig);
718 for (i = 0; i < jp->nprocs; i++)
719 if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
720 if (kill(jp->ps[i].pid, sig) == 0)
728 * Return a new job structure,
732 makejob(union node *node __unused, int nprocs)
737 for (i = njobs, jp = jobtab ; ; jp++) {
741 jobtab = ckmalloc(4 * sizeof jobtab[0]);
746 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
747 memcpy(jp, jobtab, njobs * sizeof jp[0]);
749 /* Relocate `next' pointers and list head */
751 jobmru = &jp[jobmru - jobtab];
752 for (i = 0; i < njobs; i++)
753 if (jp[i].next != NULL)
754 jp[i].next = &jp[jp[i].next -
758 bgjob = &jp[bgjob - jobtab];
759 /* Relocate `ps' pointers */
760 for (i = 0; i < njobs; i++)
761 if (jp[i].ps == &jobtab[i].ps0)
762 jp[i].ps = &jp[i].ps0;
767 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
787 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
792 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
799 setcurjob(struct job *cj)
801 struct job *jp, *prev;
803 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
806 prev->next = jp->next;
819 deljob(struct job *j)
821 struct job *jp, *prev;
823 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
826 prev->next = jp->next;
835 * Return the most recently used job that isn't `nj', and preferably one
839 getcurjob(struct job *nj)
843 /* Try to find a stopped one.. */
844 for (jp = jobmru; jp != NULL; jp = jp->next)
845 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
847 /* Otherwise the most recently used job that isn't `nj' */
848 for (jp = jobmru; jp != NULL; jp = jp->next)
849 if (jp->used && jp != nj)
858 * Fork of a subshell. If we are doing job control, give the subshell its
859 * own process group. Jp is a job structure that the job is to be added to.
860 * N is the command that will be evaluated by the child. Both jp and n may
861 * be NULL. The mode parameter can be one of the following:
862 * FORK_FG - Fork off a foreground process.
863 * FORK_BG - Fork off a background process.
864 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
865 * process group even if job control is on.
867 * When job control is turned off, background processes have their standard
868 * input redirected to /dev/null (except for the second and later processes
873 forkshell(struct job *jp, union node *n, int mode)
878 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
881 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
886 TRACE(("Fork failed, errno=%d\n", errno));
888 error("Cannot fork: %s", strerror(errno));
895 TRACE(("Child shell %d\n", (int)getpid()));
898 handler = &main_handler;
904 jobctl = 0; /* do job control only in root shell */
905 if (wasroot && mode != FORK_NOJOB && mflag) {
906 if (jp == NULL || jp->nprocs == 0)
909 pgrp = jp->ps[0].pid;
910 if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
912 /*** this causes superfluous TIOCSPGRPS ***/
913 if (tcsetpgrp(ttyfd, pgrp) < 0)
914 error("tcsetpgrp failed, errno=%d", errno);
918 } else if (mode == FORK_BG) {
921 if ((jp == NULL || jp->nprocs == 0) &&
922 ! fd0_redirected_p ()) {
924 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
925 error("cannot open %s: %s",
926 _PATH_DEVNULL, strerror(errno));
930 if (mode == FORK_BG) {
933 if ((jp == NULL || jp->nprocs == 0) &&
934 ! fd0_redirected_p ()) {
936 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
937 error("cannot open %s: %s",
938 _PATH_DEVNULL, strerror(errno));
943 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
947 if (wasroot && iflag) {
954 if (rootshell && mode != FORK_NOJOB && mflag) {
955 if (jp == NULL || jp->nprocs == 0)
958 pgrp = jp->ps[0].pid;
961 if (mode == FORK_BG) {
962 if (bgjob != NULL && bgjob->state == JOBDONE &&
963 !bgjob->remembered && !iflag)
965 backgndpid = pid; /* set $! */
969 struct procstat *ps = &jp->ps[jp->nprocs++];
973 if (iflag && rootshell && n)
974 ps->cmd = commandtext(n);
975 jp->foreground = mode == FORK_FG;
981 TRACE(("In parent shell: child = %d\n", (int)pid));
987 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
990 struct jmploc jmploc;
991 struct jmploc *savehandler;
993 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
997 savehandler = handler;
1000 TRACE(("Vfork failed, errno=%d\n", errno));
1002 error("Cannot fork: %s", strerror(errno));
1005 TRACE(("Child shell %d\n", (int)getpid()));
1006 if (setjmp(jmploc.loc))
1007 _exit(exception == EXEXEC ? exerrno : 2);
1016 shellexec(argv, envp, path, idx);
1018 handler = savehandler;
1020 struct procstat *ps = &jp->ps[jp->nprocs++];
1030 TRACE(("In parent shell: child = %d\n", (int)pid));
1036 * Wait for job to finish.
1038 * Under job control we have the problem that while a child process is
1039 * running interrupts generated by the user are sent to the child but not
1040 * to the shell. This means that an infinite loop started by an inter-
1041 * active user may be hard to kill. With job control turned off, an
1042 * interactive user may place an interactive program inside a loop. If
1043 * the interactive program catches interrupts, the user doesn't want
1044 * these interrupts to also abort the loop. The approach we take here
1045 * is to have the shell ignore interrupt signals while waiting for a
1046 * foreground process to terminate, and then send itself an interrupt
1047 * signal if the child process was terminated by an interrupt signal.
1048 * Unfortunately, some programs want to do a bit of cleanup and then
1049 * exit on interrupt; unless these processes terminate themselves by
1050 * sending a signal to themselves (instead of calling exit) they will
1051 * confuse this approach.
1055 waitforjob(struct job *jp, int *origstatus)
1058 int propagate_int = jp->jobctl && jp->foreground;
1064 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1065 while (jp->state == 0)
1066 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
1067 DOWAIT_SIG_TRAP : 0), jp) == -1)
1071 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
1072 error("tcsetpgrp failed, errno=%d\n", errno);
1074 if (jp->state == JOBSTOPPED)
1077 status = jp->ps[jp->nprocs - 1].status;
1078 if (origstatus != NULL)
1079 *origstatus = status;
1080 /* convert to 8 bits */
1081 if (WIFEXITED(status))
1082 st = WEXITSTATUS(status);
1084 else if (WIFSTOPPED(status))
1085 st = WSTOPSIG(status) + 128;
1088 st = WTERMSIG(status) + 128;
1089 if (! JOBS || jp->state == JOBDONE)
1091 if (int_pending()) {
1092 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1096 else if (rootshell && propagate_int &&
1097 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1098 kill(getpid(), SIGINT);
1106 dummy_handler(int sig __unused)
1111 * Wait for a process to terminate.
1115 dowait(int mode, struct job *job)
1117 struct sigaction sa, osa;
1118 sigset_t mask, omask;
1121 struct procstat *sp;
1123 struct job *thisjob;
1130 int restore_sigchld;
1132 TRACE(("dowait(%d, %p) called\n", mode, job));
1133 restore_sigchld = 0;
1134 if ((mode & DOWAIT_SIG) != 0) {
1136 sigprocmask(SIG_BLOCK, &mask, &omask);
1138 if (!issigchldtrapped()) {
1139 restore_sigchld = 1;
1140 sa.sa_handler = dummy_handler;
1142 sigemptyset(&sa.sa_mask);
1143 sigaction(SIGCHLD, &sa, &osa);
1149 wflags = WUNTRACED | WCONTINUED;
1153 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1155 pid = wait3(&status, wflags, (struct rusage *)NULL);
1156 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1157 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1159 if (((mode & DOWAIT_SIG_TRAP) != 0 ?
1160 pendingsig : pendingsig_waitcmd) != 0) {
1168 } while (pid == -1 && errno == EINTR);
1169 if (pid == -1 && errno == ECHILD && job != NULL)
1170 job->state = JOBDONE;
1171 if ((mode & DOWAIT_SIG) != 0) {
1172 if (restore_sigchld)
1173 sigaction(SIGCHLD, &osa, NULL);
1174 sigprocmask(SIG_SETMASK, &omask, NULL);
1181 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1182 if (jp->used && jp->nprocs > 0) {
1185 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1188 if (sp->pid == pid && (sp->status == -1 ||
1189 WIFSTOPPED(sp->status))) {
1190 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1191 (int)pid, sp->status,
1193 if (WIFCONTINUED(status)) {
1197 sp->status = status;
1200 if (sp->status == -1)
1202 else if (WIFSTOPPED(sp->status))
1205 if (stopped) { /* stopped or done */
1206 int state = done? JOBDONE : JOBSTOPPED;
1207 if (jp->state != state) {
1208 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1211 if (done && !jp->remembered &&
1212 !iflag && jp != bgjob)
1224 if (!thisjob || thisjob->state == 0)
1226 else if ((!rootshell || !iflag || thisjob == job) &&
1227 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1230 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1231 if (WIFSIGNALED(sp->status)) {
1232 sig = WTERMSIG(sp->status);
1233 coredump = WCOREDUMP(sp->status);
1235 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1236 sigstr = strsignal(sig);
1240 out2str("Unknown signal");
1242 out2str(" (core dumped)");
1247 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1248 thisjob->changed = 1;
1256 * return 1 if there are stopped jobs, otherwise 0
1258 int job_warning = 0;
1267 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1270 if (jp->state == JOBSTOPPED) {
1271 out2fmt_flush("You have stopped jobs.\n");
1284 while (njobs > 0 && dowait(0, NULL) > 0)
1292 return backgndpid != -1;
1299 if (bgjob != NULL && !forcelocal)
1300 bgjob->remembered = 1;
1305 * Return a string identifying a command (to be printed by the
1309 static char *cmdnextc;
1310 static int cmdnleft;
1311 #define MAXCMDTEXT 200
1314 commandtext(union node *n)
1318 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1319 cmdnleft = MAXCMDTEXT - 4;
1327 cmdtxtdogroup(union node *n)
1336 cmdtxtredir(union node *n, const char *op, int deffd)
1340 if (n->nfile.fd != deffd) {
1341 s[0] = n->nfile.fd + '0';
1346 if (n->type == NTOFD || n->type == NFROMFD) {
1347 if (n->ndup.dupfd >= 0)
1348 s[0] = n->ndup.dupfd + '0';
1354 cmdtxt(n->nfile.fname);
1360 cmdtxt(union node *n)
1363 struct nodelist *lp;
1369 cmdtxt(n->nbinary.ch1);
1371 cmdtxt(n->nbinary.ch2);
1374 cmdtxt(n->nbinary.ch1);
1376 cmdtxt(n->nbinary.ch2);
1379 cmdtxt(n->nbinary.ch1);
1381 cmdtxt(n->nbinary.ch2);
1384 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1392 cmdtxt(n->nredir.n);
1397 cmdtxt(n->nredir.n);
1401 cmdtxt(n->nif.test);
1403 cmdtxt(n->nif.ifpart);
1408 cmdtxt(n->nbinary.ch1);
1409 cmdtxtdogroup(n->nbinary.ch2);
1413 cmdtxt(n->nbinary.ch1);
1414 cmdtxtdogroup(n->nbinary.ch2);
1418 cmdputs(n->nfor.var);
1423 cmdputs(n->ncase.expr->narg.text);
1427 cmdputs(n->narg.text);
1432 cmdtxt(n->nnot.com);
1435 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1440 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1446 cmdputs(n->narg.text);
1449 cmdtxtredir(n, ">", 1);
1452 cmdtxtredir(n, ">>", 1);
1455 cmdtxtredir(n, ">&", 1);
1458 cmdtxtredir(n, ">|", 1);
1461 cmdtxtredir(n, "<", 0);
1464 cmdtxtredir(n, "<>", 0);
1467 cmdtxtredir(n, "<&", 0);
1482 cmdputs(const char *s)
1493 while ((c = *p++) != '\0') {
1496 else if (c == CTLVAR) {
1501 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1503 } else if (c == '=' && subtype != 0) {
1504 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1509 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1510 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1514 } else if (c == CTLENDVAR) {
1516 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1526 } else if (c == CTLARI) {
1534 } else if (c == CTLENDARI) {
1535 if (--cmdnleft > 0) {
1539 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1540 cmdnleft++; /* ignore */
1543 if (--cmdnleft <= 0) {