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 * 3. 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 */
80 * A job structure contains information about a job. A job is either a
81 * single process or a set of processes contained in a pipeline. In the
82 * latter case, pidlist will be non-NULL, and will point to a -1 terminated
87 pid_t pid; /* process id */
88 int status; /* status flags (defined above) */
89 char *cmd; /* text of command being run */
94 #define JOBSTOPPED 1 /* all procs are stopped */
95 #define JOBDONE 2 /* all procs are completed */
99 struct procstat ps0; /* status of process */
100 struct procstat *ps; /* status or processes when more than one */
101 short nprocs; /* number of processes */
102 pid_t pgrp; /* process group of this job */
103 char state; /* true if job is finished */
104 char used; /* true if this entry is in used */
105 char changed; /* true if status has changed */
106 char foreground; /* true if running in the foreground */
107 char remembered; /* true if $! referenced */
108 char pipefail; /* pass any non-zero status */
110 char jobctl; /* job running under job control */
111 struct job *next; /* job used after this one */
116 static struct job *jobtab; /* array of jobs */
117 static int njobs; /* size of array */
118 static pid_t backgndpid = -1; /* pid of last background process */
119 static struct job *bgjob = NULL; /* last background process */
121 static struct job *jobmru; /* most recently used job list */
122 static pid_t initialpgrp; /* pgrp of shell on invocation */
124 static int ttyfd = -1;
126 /* mode flags for dowait */
127 #define DOWAIT_BLOCK 0x1 /* wait until a child exits */
128 #define DOWAIT_SIG 0x2 /* if DOWAIT_BLOCK, abort on signal */
129 #define DOWAIT_SIG_TRAP 0x4 /* if DOWAIT_SIG, abort on trapped signal only */
132 static void restartjob(struct job *);
134 static void freejob(struct job *);
135 static int waitcmdloop(struct job *);
136 static struct job *getjob_nonotfound(const char *);
137 static struct job *getjob(const char *);
138 pid_t killjob(const char *, int);
139 static pid_t dowait(int, struct job *);
140 static void checkzombies(void);
141 static void cmdtxt(union node *);
142 static void cmdputs(const char *);
144 static void setcurjob(struct job *);
145 static void deljob(struct job *);
146 static struct job *getcurjob(struct job *);
148 static int getjobstatus(const struct job *);
149 static void printjobcmd(struct job *);
150 static void showjob(struct job *, int);
154 * Turn job control on and off.
174 out2fmt_flush("sh: can't access tty; job control turned off\n");
183 if (on == jobctl || rootshell == 0)
188 if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
190 while (i <= 2 && !isatty(i))
193 (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) {
200 * Keep our TTY file descriptor out of the way of
201 * the user's redirections.
203 if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
210 do { /* while we are in the background */
211 initialpgrp = tcgetpgrp(ttyfd);
212 if (initialpgrp < 0) {
216 if (initialpgrp != getpgrp()) {
230 tcsetpgrp(ttyfd, rootpid);
231 } else { /* turning job control off */
232 setpgid(0, initialpgrp);
234 tcsetpgrp(ttyfd, initialpgrp);
249 fgcmd(int argc __unused, char **argv __unused)
256 jp = getjob(*argptr);
258 error("job not created under job control");
261 pgrp = jp->ps[0].pid;
263 tcsetpgrp(ttyfd, pgrp);
267 status = waitforjob(jp, (int *)NULL);
274 bgcmd(int argc __unused, char **argv __unused)
280 jp = getjob(*argptr);
282 error("job not created under job control");
283 if (jp->state == JOBDONE)
287 out1fmt("[%td] ", jp - jobtab + 1);
289 } while (*argptr != NULL && *++argptr != NULL);
295 restartjob(struct job *jp)
300 if (jp->state == JOBDONE)
304 kill(-jp->ps[0].pid, SIGCONT);
305 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
306 if (WIFSTOPPED(ps->status)) {
317 jobscmd(int argc __unused, char *argv[] __unused)
322 mode = SHOWJOBS_DEFAULT;
323 while ((ch = nextopt("lps")) != '\0') {
326 mode = SHOWJOBS_VERBOSE;
329 mode = SHOWJOBS_PGIDS;
332 mode = SHOWJOBS_PIDS;
340 while ((id = *argptr++) != NULL)
341 showjob(getjob(id), mode);
346 static int getjobstatus(const struct job *jp)
351 return (jp->ps[jp->nprocs - 1].status);
352 for (i = jp->nprocs - 1; i >= 0; i--) {
353 status = jp->ps[i].status;
361 printjobcmd(struct job *jp)
366 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
375 showjob(struct job *jp, int mode)
379 const char *statestr, *coredump;
382 int col, curr, i, jobno, prev, procno, status;
385 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
386 jobno = jp - jobtab + 1;
389 if ((j = getcurjob(NULL)) != NULL) {
390 curr = j - jobtab + 1;
391 if ((j = getcurjob(j)) != NULL)
392 prev = j - jobtab + 1;
396 status = getjobstatus(jp);
397 if (jp->state == 0) {
398 statestr = "Running";
400 } else if (jp->state == JOBSTOPPED) {
401 ps = jp->ps + jp->nprocs - 1;
402 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
404 if (WIFSTOPPED(ps->status))
405 i = WSTOPSIG(ps->status);
408 statestr = strsignal(i);
409 if (statestr == NULL)
410 statestr = "Suspended";
412 } else if (WIFEXITED(status)) {
413 if (WEXITSTATUS(status) == 0)
416 fmtstr(statebuf, sizeof(statebuf), "Done(%d)",
417 WEXITSTATUS(status));
421 i = WTERMSIG(status);
422 statestr = strsignal(i);
423 if (statestr == NULL)
424 statestr = "Unknown signal";
425 if (WCOREDUMP(status))
426 coredump = " (core dumped)";
429 for (ps = jp->ps ; procno > 0 ; ps++, procno--) { /* for each process */
430 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
431 out1fmt("%d\n", (int)ps->pid);
434 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
436 if (jobno == curr && ps == jp->ps)
438 else if (jobno == prev && ps == jp->ps)
443 fmtstr(s, 64, "[%d] %c ", jobno, c);
445 fmtstr(s, 64, " %c ", c);
448 if (mode == SHOWJOBS_VERBOSE) {
449 fmtstr(s, 64, "%d ", (int)ps->pid);
456 col += strlen(statestr) + strlen(coredump);
462 if (mode == SHOWJOBS_VERBOSE) {
471 * Print a list of jobs. If "change" is nonzero, only print jobs whose
472 * statuses have changed since the last call to showjobs.
474 * If the shell is interrupted in the process of creating a job, the
475 * result may be a job structure containing zero processes. Such structures
476 * will be freed here.
480 showjobs(int change, int mode)
485 TRACE(("showjobs(%d) called\n", change));
487 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
490 if (jp->nprocs == 0) {
494 if (change && ! jp->changed)
497 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
499 /* Hack: discard jobs for which $! has not been
500 * referenced in interactive mode when they terminate.
502 if (jp->state == JOBDONE && !jp->remembered &&
503 (iflag || jp != bgjob)) {
512 * Mark a job structure as unused.
516 freejob(struct job *jp)
524 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
525 if (ps->cmd != nullstr)
528 if (jp->ps != &jp->ps0)
540 waitcmd(int argc __unused, char **argv __unused)
547 return (waitcmdloop(NULL));
550 job = getjob_nonotfound(*argptr);
554 retval = waitcmdloop(job);
556 } while (*argptr != NULL);
562 waitcmdloop(struct job *job)
564 int status, retval, sig;
568 * Loop until a process is terminated or stopped, or a SIGINT is
574 if (job->state == JOBDONE) {
575 status = getjobstatus(job);
576 if (WIFEXITED(status))
577 retval = WEXITSTATUS(status);
579 retval = WTERMSIG(status) + 128;
580 if (! iflag || ! job->changed)
590 for (jp = jobtab ; jp < jobtab + njobs; jp++)
591 if (jp->used && jp->state == JOBDONE) {
592 if (! iflag || ! jp->changed)
600 for (jp = jobtab ; ; jp++) {
601 if (jp >= jobtab + njobs) { /* no running procs */
604 if (jp->used && jp->state == 0)
608 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
610 sig = pendingsig_waitcmd;
611 pendingsig_waitcmd = 0;
618 jobidcmd(int argc __unused, char **argv __unused)
624 jp = getjob(*argptr);
625 for (i = 0 ; i < jp->nprocs ; ) {
626 out1fmt("%d", (int)jp->ps[i].pid);
627 out1c(++i < jp->nprocs? ' ' : '\n');
635 * Convert a job name to a job structure.
639 getjob_nonotfound(const char *name)
642 struct job *found, *jp;
651 error("No current job");
654 if (name[0] == '%') {
655 if (is_digit(name[1])) {
656 jobno = number(name + 1);
657 if (jobno > 0 && jobno <= njobs
658 && jobtab[jobno - 1].used != 0)
659 return &jobtab[jobno - 1];
661 } else if ((name[1] == '%' || name[1] == '+') &&
663 if ((jp = getcurjob(NULL)) == NULL)
664 error("No current job");
666 } else if (name[1] == '-' && name[2] == '\0') {
667 if ((jp = getcurjob(NULL)) == NULL ||
668 (jp = getcurjob(jp)) == NULL)
669 error("No previous job");
672 } else if (name[1] == '?') {
674 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
675 if (jp->used && jp->nprocs > 0
676 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
678 error("%s: ambiguous", name);
685 namelen = strlen(name);
687 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
688 if (jp->used && jp->nprocs > 0
689 && strncmp(jp->ps[0].cmd, name + 1,
692 error("%s: ambiguous", name);
699 } else if (is_number(name)) {
700 pid = (pid_t)number(name);
701 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
702 if (jp->used && jp->nprocs > 0
703 && jp->ps[jp->nprocs - 1].pid == pid)
712 getjob(const char *name)
716 jp = getjob_nonotfound(name);
718 error("No such job: %s", name);
724 killjob(const char *name, int sig)
730 if (jp->state == JOBDONE)
733 return kill(-jp->ps[0].pid, sig);
736 for (i = 0; i < jp->nprocs; i++)
737 if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
738 if (kill(jp->ps[i].pid, sig) == 0)
746 * Return a new job structure,
750 makejob(union node *node __unused, int nprocs)
755 for (i = njobs, jp = jobtab ; ; jp++) {
759 jobtab = ckmalloc(4 * sizeof jobtab[0]);
764 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
765 memcpy(jp, jobtab, njobs * sizeof jp[0]);
767 /* Relocate `next' pointers and list head */
769 jobmru = &jp[jobmru - jobtab];
770 for (i = 0; i < njobs; i++)
771 if (jp[i].next != NULL)
772 jp[i].next = &jp[jp[i].next -
776 bgjob = &jp[bgjob - jobtab];
777 /* Relocate `ps' pointers */
778 for (i = 0; i < njobs; i++)
779 if (jp[i].ps == &jobtab[i].ps0)
780 jp[i].ps = &jp[i].ps0;
785 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
800 jp->pipefail = pipefailflag;
806 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
811 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
818 setcurjob(struct job *cj)
820 struct job *jp, *prev;
822 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
825 prev->next = jp->next;
838 deljob(struct job *j)
840 struct job *jp, *prev;
842 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
845 prev->next = jp->next;
854 * Return the most recently used job that isn't `nj', and preferably one
858 getcurjob(struct job *nj)
862 /* Try to find a stopped one.. */
863 for (jp = jobmru; jp != NULL; jp = jp->next)
864 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
866 /* Otherwise the most recently used job that isn't `nj' */
867 for (jp = jobmru; jp != NULL; jp = jp->next)
868 if (jp->used && jp != nj)
877 * Fork of a subshell. If we are doing job control, give the subshell its
878 * own process group. Jp is a job structure that the job is to be added to.
879 * N is the command that will be evaluated by the child. Both jp and n may
880 * be NULL. The mode parameter can be one of the following:
881 * FORK_FG - Fork off a foreground process.
882 * FORK_BG - Fork off a background process.
883 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
884 * process group even if job control is on.
886 * When job control is turned off, background processes have their standard
887 * input redirected to /dev/null (except for the second and later processes
892 forkshell(struct job *jp, union node *n, int mode)
897 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
900 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
905 TRACE(("Fork failed, errno=%d\n", errno));
907 error("Cannot fork: %s", strerror(errno));
914 TRACE(("Child shell %d\n", (int)getpid()));
917 handler = &main_handler;
923 jobctl = 0; /* do job control only in root shell */
924 if (wasroot && mode != FORK_NOJOB && mflag) {
925 if (jp == NULL || jp->nprocs == 0)
928 pgrp = jp->ps[0].pid;
929 if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
931 /*** this causes superfluous TIOCSPGRPS ***/
932 if (tcsetpgrp(ttyfd, pgrp) < 0)
933 error("tcsetpgrp failed, errno=%d", errno);
937 } else if (mode == FORK_BG) {
940 if ((jp == NULL || jp->nprocs == 0) &&
941 ! fd0_redirected_p ()) {
943 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
944 error("cannot open %s: %s",
945 _PATH_DEVNULL, strerror(errno));
949 if (mode == FORK_BG) {
952 if ((jp == NULL || jp->nprocs == 0) &&
953 ! fd0_redirected_p ()) {
955 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
956 error("cannot open %s: %s",
957 _PATH_DEVNULL, strerror(errno));
962 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
966 if (wasroot && iflag) {
973 if (rootshell && mode != FORK_NOJOB && mflag) {
974 if (jp == NULL || jp->nprocs == 0)
977 pgrp = jp->ps[0].pid;
980 if (mode == FORK_BG) {
981 if (bgjob != NULL && bgjob->state == JOBDONE &&
982 !bgjob->remembered && !iflag)
984 backgndpid = pid; /* set $! */
988 struct procstat *ps = &jp->ps[jp->nprocs++];
992 if (iflag && rootshell && n)
993 ps->cmd = commandtext(n);
994 jp->foreground = mode == FORK_FG;
1000 TRACE(("In parent shell: child = %d\n", (int)pid));
1006 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
1009 struct jmploc jmploc;
1010 struct jmploc *savehandler;
1013 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
1015 inton = is_int_on();
1018 savehandler = handler;
1021 TRACE(("Vfork failed, errno=%d\n", errno));
1023 error("Cannot fork: %s", strerror(errno));
1026 TRACE(("Child shell %d\n", (int)getpid()));
1027 if (setjmp(jmploc.loc))
1037 shellexec(argv, envp, path, idx);
1039 handler = savehandler;
1041 struct procstat *ps = &jp->ps[jp->nprocs++];
1051 TRACE(("In parent shell: child = %d\n", (int)pid));
1057 * Wait for job to finish.
1059 * Under job control we have the problem that while a child process is
1060 * running interrupts generated by the user are sent to the child but not
1061 * to the shell. This means that an infinite loop started by an inter-
1062 * active user may be hard to kill. With job control turned off, an
1063 * interactive user may place an interactive program inside a loop. If
1064 * the interactive program catches interrupts, the user doesn't want
1065 * these interrupts to also abort the loop. The approach we take here
1066 * is to have the shell ignore interrupt signals while waiting for a
1067 * foreground process to terminate, and then send itself an interrupt
1068 * signal if the child process was terminated by an interrupt signal.
1069 * Unfortunately, some programs want to do a bit of cleanup and then
1070 * exit on interrupt; unless these processes terminate themselves by
1071 * sending a signal to themselves (instead of calling exit) they will
1072 * confuse this approach.
1076 waitforjob(struct job *jp, int *signaled)
1079 int propagate_int = jp->jobctl && jp->foreground;
1085 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1086 while (jp->state == 0)
1087 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
1088 DOWAIT_SIG_TRAP : 0), jp) == -1)
1092 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
1093 error("tcsetpgrp failed, errno=%d\n", errno);
1095 if (jp->state == JOBSTOPPED)
1098 status = getjobstatus(jp);
1099 if (signaled != NULL)
1100 *signaled = WIFSIGNALED(status);
1101 /* convert to 8 bits */
1102 if (WIFEXITED(status))
1103 st = WEXITSTATUS(status);
1105 else if (WIFSTOPPED(status))
1106 st = WSTOPSIG(status) + 128;
1109 st = WTERMSIG(status) + 128;
1110 if (! JOBS || jp->state == JOBDONE)
1112 if (int_pending()) {
1113 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1117 else if (rootshell && propagate_int &&
1118 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1119 kill(getpid(), SIGINT);
1127 dummy_handler(int sig __unused)
1132 * Wait for a process to terminate.
1136 dowait(int mode, struct job *job)
1138 struct sigaction sa, osa;
1139 sigset_t mask, omask;
1142 struct procstat *sp;
1144 struct job *thisjob;
1151 int restore_sigchld;
1153 TRACE(("dowait(%d, %p) called\n", mode, job));
1154 restore_sigchld = 0;
1155 if ((mode & DOWAIT_SIG) != 0) {
1157 sigprocmask(SIG_BLOCK, &mask, &omask);
1159 if (!issigchldtrapped()) {
1160 restore_sigchld = 1;
1161 sa.sa_handler = dummy_handler;
1163 sigemptyset(&sa.sa_mask);
1164 sigaction(SIGCHLD, &sa, &osa);
1170 wflags = WUNTRACED | WCONTINUED;
1174 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1176 pid = wait3(&status, wflags, (struct rusage *)NULL);
1177 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1178 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1180 if (((mode & DOWAIT_SIG_TRAP) != 0 ?
1181 pendingsig : pendingsig_waitcmd) != 0) {
1189 } while (pid == -1 && errno == EINTR);
1190 if (pid == -1 && errno == ECHILD && job != NULL)
1191 job->state = JOBDONE;
1192 if ((mode & DOWAIT_SIG) != 0) {
1193 if (restore_sigchld)
1194 sigaction(SIGCHLD, &osa, NULL);
1195 sigprocmask(SIG_SETMASK, &omask, NULL);
1202 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1203 if (jp->used && jp->nprocs > 0) {
1206 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1209 if (sp->pid == pid && (sp->status == -1 ||
1210 WIFSTOPPED(sp->status))) {
1211 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1212 (int)pid, sp->status,
1214 if (WIFCONTINUED(status)) {
1218 sp->status = status;
1221 if (sp->status == -1)
1223 else if (WIFSTOPPED(sp->status))
1226 if (stopped) { /* stopped or done */
1227 int state = done? JOBDONE : JOBSTOPPED;
1228 if (jp->state != state) {
1229 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1232 if (done && !jp->remembered &&
1233 !iflag && jp != bgjob)
1245 if (!thisjob || thisjob->state == 0)
1247 else if ((!rootshell || !iflag || thisjob == job) &&
1248 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1251 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1252 if (WIFSIGNALED(sp->status)) {
1253 sig = WTERMSIG(sp->status);
1254 coredump = WCOREDUMP(sp->status);
1256 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1257 sigstr = strsignal(sig);
1261 out2str("Unknown signal");
1263 out2str(" (core dumped)");
1268 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1269 thisjob->changed = 1;
1277 * return 1 if there are stopped jobs, otherwise 0
1279 int job_warning = 0;
1288 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1291 if (jp->state == JOBSTOPPED) {
1292 out2fmt_flush("You have stopped jobs.\n");
1305 while (njobs > 0 && dowait(0, NULL) > 0)
1313 return backgndpid != -1;
1320 if (bgjob != NULL && !forcelocal)
1321 bgjob->remembered = 1;
1326 * Return a string identifying a command (to be printed by the
1330 static char *cmdnextc;
1331 static int cmdnleft;
1332 #define MAXCMDTEXT 200
1335 commandtext(union node *n)
1339 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1340 cmdnleft = MAXCMDTEXT - 4;
1348 cmdtxtdogroup(union node *n)
1357 cmdtxtredir(union node *n, const char *op, int deffd)
1361 if (n->nfile.fd != deffd) {
1362 s[0] = n->nfile.fd + '0';
1367 if (n->type == NTOFD || n->type == NFROMFD) {
1368 if (n->ndup.dupfd >= 0)
1369 s[0] = n->ndup.dupfd + '0';
1375 cmdtxt(n->nfile.fname);
1381 cmdtxt(union node *n)
1384 struct nodelist *lp;
1390 cmdtxt(n->nbinary.ch1);
1392 cmdtxt(n->nbinary.ch2);
1395 cmdtxt(n->nbinary.ch1);
1397 cmdtxt(n->nbinary.ch2);
1400 cmdtxt(n->nbinary.ch1);
1402 cmdtxt(n->nbinary.ch2);
1405 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1413 cmdtxt(n->nredir.n);
1418 cmdtxt(n->nredir.n);
1422 cmdtxt(n->nif.test);
1424 cmdtxt(n->nif.ifpart);
1429 cmdtxt(n->nbinary.ch1);
1430 cmdtxtdogroup(n->nbinary.ch2);
1434 cmdtxt(n->nbinary.ch1);
1435 cmdtxtdogroup(n->nbinary.ch2);
1439 cmdputs(n->nfor.var);
1444 cmdputs(n->ncase.expr->narg.text);
1448 cmdputs(n->narg.text);
1453 cmdtxt(n->nnot.com);
1456 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1461 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1467 cmdputs(n->narg.text);
1470 cmdtxtredir(n, ">", 1);
1473 cmdtxtredir(n, ">>", 1);
1476 cmdtxtredir(n, ">&", 1);
1479 cmdtxtredir(n, ">|", 1);
1482 cmdtxtredir(n, "<", 0);
1485 cmdtxtredir(n, "<>", 0);
1488 cmdtxtredir(n, "<&", 0);
1503 cmdputs(const char *s)
1514 while ((c = *p++) != '\0') {
1517 else if (c == CTLVAR) {
1522 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1524 } else if (c == '=' && subtype != 0) {
1525 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1530 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1531 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1535 } else if (c == CTLENDVAR) {
1537 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1547 } else if (c == CTLARI) {
1555 } else if (c == CTLENDARI) {
1556 if (--cmdnleft > 0) {
1560 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1561 cmdnleft++; /* ignore */
1564 if (--cmdnleft <= 0) {