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 */
107 char pipefail; /* pass any non-zero status */
109 char jobctl; /* job running under job control */
110 struct job *next; /* job used after this one */
115 static struct job *jobtab; /* array of jobs */
116 static int njobs; /* size of array */
117 static pid_t backgndpid = -1; /* pid of last background process */
118 static struct job *bgjob = NULL; /* last background process */
120 static struct job *jobmru; /* most recently used job list */
121 static pid_t initialpgrp; /* pgrp of shell on invocation */
123 static int ttyfd = -1;
125 /* mode flags for dowait */
126 #define DOWAIT_BLOCK 0x1 /* wait until a child exits */
127 #define DOWAIT_SIG 0x2 /* if DOWAIT_BLOCK, abort on signal */
128 #define DOWAIT_SIG_TRAP 0x4 /* if DOWAIT_SIG, abort on trapped signal only */
131 static void restartjob(struct job *);
133 static void freejob(struct job *);
134 static int waitcmdloop(struct job *);
135 static struct job *getjob_nonotfound(const char *);
136 static struct job *getjob(const char *);
137 pid_t killjob(const char *, int);
138 static pid_t dowait(int, struct job *);
139 static void checkzombies(void);
140 static void cmdtxt(union node *);
141 static void cmdputs(const char *);
143 static void setcurjob(struct job *);
144 static void deljob(struct job *);
145 static struct job *getcurjob(struct job *);
147 static int getjobstatus(const struct job *);
148 static void printjobcmd(struct job *);
149 static void showjob(struct job *, int);
153 * Turn job control on and off.
173 out2fmt_flush("sh: can't access tty; job control turned off\n");
182 if (on == jobctl || rootshell == 0)
187 if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
189 while (i <= 2 && !isatty(i))
192 (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) {
199 * Keep our TTY file descriptor out of the way of
200 * the user's redirections.
202 if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
209 do { /* while we are in the background */
210 initialpgrp = tcgetpgrp(ttyfd);
211 if (initialpgrp < 0) {
215 if (initialpgrp != getpgrp()) {
229 tcsetpgrp(ttyfd, rootpid);
230 } else { /* turning job control off */
231 setpgid(0, initialpgrp);
233 tcsetpgrp(ttyfd, initialpgrp);
248 fgcmd(int argc __unused, char **argv __unused)
255 jp = getjob(*argptr);
257 error("job not created under job control");
260 pgrp = jp->ps[0].pid;
262 tcsetpgrp(ttyfd, pgrp);
266 status = waitforjob(jp, (int *)NULL);
273 bgcmd(int argc __unused, char **argv __unused)
279 jp = getjob(*argptr);
281 error("job not created under job control");
282 if (jp->state == JOBDONE)
286 out1fmt("[%td] ", jp - jobtab + 1);
288 } while (*argptr != NULL && *++argptr != NULL);
294 restartjob(struct job *jp)
299 if (jp->state == JOBDONE)
303 kill(-jp->ps[0].pid, SIGCONT);
304 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
305 if (WIFSTOPPED(ps->status)) {
316 jobscmd(int argc __unused, char *argv[] __unused)
321 mode = SHOWJOBS_DEFAULT;
322 while ((ch = nextopt("lps")) != '\0') {
325 mode = SHOWJOBS_VERBOSE;
328 mode = SHOWJOBS_PGIDS;
331 mode = SHOWJOBS_PIDS;
339 while ((id = *argptr++) != NULL)
340 showjob(getjob(id), mode);
345 static int getjobstatus(const struct job *jp)
350 return (jp->ps[jp->nprocs - 1].status);
351 for (i = jp->nprocs - 1; i >= 0; i--) {
352 status = jp->ps[i].status;
360 printjobcmd(struct job *jp)
365 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
374 showjob(struct job *jp, int mode)
378 const char *statestr, *coredump;
381 int col, curr, i, jobno, prev, procno, status;
384 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
385 jobno = jp - jobtab + 1;
388 if ((j = getcurjob(NULL)) != NULL) {
389 curr = j - jobtab + 1;
390 if ((j = getcurjob(j)) != NULL)
391 prev = j - jobtab + 1;
395 status = getjobstatus(jp);
396 if (jp->state == 0) {
397 statestr = "Running";
399 } else if (jp->state == JOBSTOPPED) {
400 ps = jp->ps + jp->nprocs - 1;
401 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
403 if (WIFSTOPPED(ps->status))
404 i = WSTOPSIG(ps->status);
407 statestr = strsignal(i);
408 if (statestr == NULL)
409 statestr = "Suspended";
411 } else if (WIFEXITED(status)) {
412 if (WEXITSTATUS(status) == 0)
415 fmtstr(statebuf, sizeof(statebuf), "Done(%d)",
416 WEXITSTATUS(status));
420 i = WTERMSIG(status);
421 statestr = strsignal(i);
422 if (statestr == NULL)
423 statestr = "Unknown signal";
424 if (WCOREDUMP(status))
425 coredump = " (core dumped)";
428 for (ps = jp->ps ; procno > 0 ; ps++, procno--) { /* for each process */
429 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
430 out1fmt("%d\n", (int)ps->pid);
433 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
435 if (jobno == curr && ps == jp->ps)
437 else if (jobno == prev && ps == jp->ps)
442 fmtstr(s, 64, "[%d] %c ", jobno, c);
444 fmtstr(s, 64, " %c ", c);
447 if (mode == SHOWJOBS_VERBOSE) {
448 fmtstr(s, 64, "%d ", (int)ps->pid);
455 col += strlen(statestr) + strlen(coredump);
461 if (mode == SHOWJOBS_VERBOSE) {
470 * Print a list of jobs. If "change" is nonzero, only print jobs whose
471 * statuses have changed since the last call to showjobs.
473 * If the shell is interrupted in the process of creating a job, the
474 * result may be a job structure containing zero processes. Such structures
475 * will be freed here.
479 showjobs(int change, int mode)
484 TRACE(("showjobs(%d) called\n", change));
486 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
489 if (jp->nprocs == 0) {
493 if (change && ! jp->changed)
496 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
498 /* Hack: discard jobs for which $! has not been
499 * referenced in interactive mode when they terminate.
501 if (jp->state == JOBDONE && !jp->remembered &&
502 (iflag || jp != bgjob)) {
511 * Mark a job structure as unused.
515 freejob(struct job *jp)
523 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
524 if (ps->cmd != nullstr)
527 if (jp->ps != &jp->ps0)
539 waitcmd(int argc __unused, char **argv __unused)
546 return (waitcmdloop(NULL));
549 job = getjob_nonotfound(*argptr);
553 retval = waitcmdloop(job);
555 } while (*argptr != NULL);
561 waitcmdloop(struct job *job)
563 int status, retval, sig;
567 * Loop until a process is terminated or stopped, or a SIGINT is
573 if (job->state == JOBDONE) {
574 status = getjobstatus(job);
575 if (WIFEXITED(status))
576 retval = WEXITSTATUS(status);
578 retval = WTERMSIG(status) + 128;
579 if (! iflag || ! job->changed)
589 for (jp = jobtab ; jp < jobtab + njobs; jp++)
590 if (jp->used && jp->state == JOBDONE) {
591 if (! iflag || ! jp->changed)
599 for (jp = jobtab ; ; jp++) {
600 if (jp >= jobtab + njobs) { /* no running procs */
603 if (jp->used && jp->state == 0)
607 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
609 sig = pendingsig_waitcmd;
610 pendingsig_waitcmd = 0;
617 jobidcmd(int argc __unused, char **argv __unused)
623 jp = getjob(*argptr);
624 for (i = 0 ; i < jp->nprocs ; ) {
625 out1fmt("%d", (int)jp->ps[i].pid);
626 out1c(++i < jp->nprocs? ' ' : '\n');
634 * Convert a job name to a job structure.
638 getjob_nonotfound(const char *name)
641 struct job *found, *jp;
650 error("No current job");
653 if (name[0] == '%') {
654 if (is_digit(name[1])) {
655 jobno = number(name + 1);
656 if (jobno > 0 && jobno <= njobs
657 && jobtab[jobno - 1].used != 0)
658 return &jobtab[jobno - 1];
660 } else if ((name[1] == '%' || name[1] == '+') &&
662 if ((jp = getcurjob(NULL)) == NULL)
663 error("No current job");
665 } else if (name[1] == '-' && name[2] == '\0') {
666 if ((jp = getcurjob(NULL)) == NULL ||
667 (jp = getcurjob(jp)) == NULL)
668 error("No previous job");
671 } else if (name[1] == '?') {
673 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
674 if (jp->used && jp->nprocs > 0
675 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
677 error("%s: ambiguous", name);
684 namelen = strlen(name);
686 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
687 if (jp->used && jp->nprocs > 0
688 && strncmp(jp->ps[0].cmd, name + 1,
691 error("%s: ambiguous", name);
698 } else if (is_number(name)) {
699 pid = (pid_t)number(name);
700 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
701 if (jp->used && jp->nprocs > 0
702 && jp->ps[jp->nprocs - 1].pid == pid)
711 getjob(const char *name)
715 jp = getjob_nonotfound(name);
717 error("No such job: %s", name);
723 killjob(const char *name, int sig)
729 if (jp->state == JOBDONE)
732 return kill(-jp->ps[0].pid, sig);
735 for (i = 0; i < jp->nprocs; i++)
736 if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
737 if (kill(jp->ps[i].pid, sig) == 0)
745 * Return a new job structure,
749 makejob(union node *node __unused, int nprocs)
754 for (i = njobs, jp = jobtab ; ; jp++) {
758 jobtab = ckmalloc(4 * sizeof jobtab[0]);
763 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
764 memcpy(jp, jobtab, njobs * sizeof jp[0]);
766 /* Relocate `next' pointers and list head */
768 jobmru = &jp[jobmru - jobtab];
769 for (i = 0; i < njobs; i++)
770 if (jp[i].next != NULL)
771 jp[i].next = &jp[jp[i].next -
775 bgjob = &jp[bgjob - jobtab];
776 /* Relocate `ps' pointers */
777 for (i = 0; i < njobs; i++)
778 if (jp[i].ps == &jobtab[i].ps0)
779 jp[i].ps = &jp[i].ps0;
784 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
799 jp->pipefail = pipefailflag;
805 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
810 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
817 setcurjob(struct job *cj)
819 struct job *jp, *prev;
821 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
824 prev->next = jp->next;
837 deljob(struct job *j)
839 struct job *jp, *prev;
841 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
844 prev->next = jp->next;
853 * Return the most recently used job that isn't `nj', and preferably one
857 getcurjob(struct job *nj)
861 /* Try to find a stopped one.. */
862 for (jp = jobmru; jp != NULL; jp = jp->next)
863 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
865 /* Otherwise the most recently used job that isn't `nj' */
866 for (jp = jobmru; jp != NULL; jp = jp->next)
867 if (jp->used && jp != nj)
876 * Fork of a subshell. If we are doing job control, give the subshell its
877 * own process group. Jp is a job structure that the job is to be added to.
878 * N is the command that will be evaluated by the child. Both jp and n may
879 * be NULL. The mode parameter can be one of the following:
880 * FORK_FG - Fork off a foreground process.
881 * FORK_BG - Fork off a background process.
882 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
883 * process group even if job control is on.
885 * When job control is turned off, background processes have their standard
886 * input redirected to /dev/null (except for the second and later processes
891 forkshell(struct job *jp, union node *n, int mode)
896 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
899 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
904 TRACE(("Fork failed, errno=%d\n", errno));
906 error("Cannot fork: %s", strerror(errno));
913 TRACE(("Child shell %d\n", (int)getpid()));
916 handler = &main_handler;
922 jobctl = 0; /* do job control only in root shell */
923 if (wasroot && mode != FORK_NOJOB && mflag) {
924 if (jp == NULL || jp->nprocs == 0)
927 pgrp = jp->ps[0].pid;
928 if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
930 /*** this causes superfluous TIOCSPGRPS ***/
931 if (tcsetpgrp(ttyfd, pgrp) < 0)
932 error("tcsetpgrp failed, errno=%d", errno);
936 } else if (mode == FORK_BG) {
939 if ((jp == NULL || jp->nprocs == 0) &&
940 ! fd0_redirected_p ()) {
942 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
943 error("cannot open %s: %s",
944 _PATH_DEVNULL, strerror(errno));
948 if (mode == FORK_BG) {
951 if ((jp == NULL || jp->nprocs == 0) &&
952 ! fd0_redirected_p ()) {
954 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
955 error("cannot open %s: %s",
956 _PATH_DEVNULL, strerror(errno));
961 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
965 if (wasroot && iflag) {
972 if (rootshell && mode != FORK_NOJOB && mflag) {
973 if (jp == NULL || jp->nprocs == 0)
976 pgrp = jp->ps[0].pid;
979 if (mode == FORK_BG) {
980 if (bgjob != NULL && bgjob->state == JOBDONE &&
981 !bgjob->remembered && !iflag)
983 backgndpid = pid; /* set $! */
987 struct procstat *ps = &jp->ps[jp->nprocs++];
991 if (iflag && rootshell && n)
992 ps->cmd = commandtext(n);
993 jp->foreground = mode == FORK_FG;
999 TRACE(("In parent shell: child = %d\n", (int)pid));
1005 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
1008 struct jmploc jmploc;
1009 struct jmploc *savehandler;
1011 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
1015 savehandler = handler;
1018 TRACE(("Vfork failed, errno=%d\n", errno));
1020 error("Cannot fork: %s", strerror(errno));
1023 TRACE(("Child shell %d\n", (int)getpid()));
1024 if (setjmp(jmploc.loc))
1025 _exit(exception == EXEXEC ? exerrno : 2);
1034 shellexec(argv, envp, path, idx);
1036 handler = savehandler;
1038 struct procstat *ps = &jp->ps[jp->nprocs++];
1048 TRACE(("In parent shell: child = %d\n", (int)pid));
1054 * Wait for job to finish.
1056 * Under job control we have the problem that while a child process is
1057 * running interrupts generated by the user are sent to the child but not
1058 * to the shell. This means that an infinite loop started by an inter-
1059 * active user may be hard to kill. With job control turned off, an
1060 * interactive user may place an interactive program inside a loop. If
1061 * the interactive program catches interrupts, the user doesn't want
1062 * these interrupts to also abort the loop. The approach we take here
1063 * is to have the shell ignore interrupt signals while waiting for a
1064 * foreground process to terminate, and then send itself an interrupt
1065 * signal if the child process was terminated by an interrupt signal.
1066 * Unfortunately, some programs want to do a bit of cleanup and then
1067 * exit on interrupt; unless these processes terminate themselves by
1068 * sending a signal to themselves (instead of calling exit) they will
1069 * confuse this approach.
1073 waitforjob(struct job *jp, int *origstatus)
1076 int propagate_int = jp->jobctl && jp->foreground;
1082 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1083 while (jp->state == 0)
1084 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
1085 DOWAIT_SIG_TRAP : 0), jp) == -1)
1089 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
1090 error("tcsetpgrp failed, errno=%d\n", errno);
1092 if (jp->state == JOBSTOPPED)
1095 status = getjobstatus(jp);
1096 if (origstatus != NULL)
1097 *origstatus = status;
1098 /* convert to 8 bits */
1099 if (WIFEXITED(status))
1100 st = WEXITSTATUS(status);
1102 else if (WIFSTOPPED(status))
1103 st = WSTOPSIG(status) + 128;
1106 st = WTERMSIG(status) + 128;
1107 if (! JOBS || jp->state == JOBDONE)
1109 if (int_pending()) {
1110 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1114 else if (rootshell && propagate_int &&
1115 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1116 kill(getpid(), SIGINT);
1124 dummy_handler(int sig __unused)
1129 * Wait for a process to terminate.
1133 dowait(int mode, struct job *job)
1135 struct sigaction sa, osa;
1136 sigset_t mask, omask;
1139 struct procstat *sp;
1141 struct job *thisjob;
1148 int restore_sigchld;
1150 TRACE(("dowait(%d, %p) called\n", mode, job));
1151 restore_sigchld = 0;
1152 if ((mode & DOWAIT_SIG) != 0) {
1154 sigprocmask(SIG_BLOCK, &mask, &omask);
1156 if (!issigchldtrapped()) {
1157 restore_sigchld = 1;
1158 sa.sa_handler = dummy_handler;
1160 sigemptyset(&sa.sa_mask);
1161 sigaction(SIGCHLD, &sa, &osa);
1167 wflags = WUNTRACED | WCONTINUED;
1171 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1173 pid = wait3(&status, wflags, (struct rusage *)NULL);
1174 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1175 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1177 if (((mode & DOWAIT_SIG_TRAP) != 0 ?
1178 pendingsig : pendingsig_waitcmd) != 0) {
1186 } while (pid == -1 && errno == EINTR);
1187 if (pid == -1 && errno == ECHILD && job != NULL)
1188 job->state = JOBDONE;
1189 if ((mode & DOWAIT_SIG) != 0) {
1190 if (restore_sigchld)
1191 sigaction(SIGCHLD, &osa, NULL);
1192 sigprocmask(SIG_SETMASK, &omask, NULL);
1199 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1200 if (jp->used && jp->nprocs > 0) {
1203 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1206 if (sp->pid == pid && (sp->status == -1 ||
1207 WIFSTOPPED(sp->status))) {
1208 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1209 (int)pid, sp->status,
1211 if (WIFCONTINUED(status)) {
1215 sp->status = status;
1218 if (sp->status == -1)
1220 else if (WIFSTOPPED(sp->status))
1223 if (stopped) { /* stopped or done */
1224 int state = done? JOBDONE : JOBSTOPPED;
1225 if (jp->state != state) {
1226 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1229 if (done && !jp->remembered &&
1230 !iflag && jp != bgjob)
1242 if (!thisjob || thisjob->state == 0)
1244 else if ((!rootshell || !iflag || thisjob == job) &&
1245 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1248 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1249 if (WIFSIGNALED(sp->status)) {
1250 sig = WTERMSIG(sp->status);
1251 coredump = WCOREDUMP(sp->status);
1253 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1254 sigstr = strsignal(sig);
1258 out2str("Unknown signal");
1260 out2str(" (core dumped)");
1265 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1266 thisjob->changed = 1;
1274 * return 1 if there are stopped jobs, otherwise 0
1276 int job_warning = 0;
1285 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1288 if (jp->state == JOBSTOPPED) {
1289 out2fmt_flush("You have stopped jobs.\n");
1302 while (njobs > 0 && dowait(0, NULL) > 0)
1310 return backgndpid != -1;
1317 if (bgjob != NULL && !forcelocal)
1318 bgjob->remembered = 1;
1323 * Return a string identifying a command (to be printed by the
1327 static char *cmdnextc;
1328 static int cmdnleft;
1329 #define MAXCMDTEXT 200
1332 commandtext(union node *n)
1336 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1337 cmdnleft = MAXCMDTEXT - 4;
1345 cmdtxtdogroup(union node *n)
1354 cmdtxtredir(union node *n, const char *op, int deffd)
1358 if (n->nfile.fd != deffd) {
1359 s[0] = n->nfile.fd + '0';
1364 if (n->type == NTOFD || n->type == NFROMFD) {
1365 if (n->ndup.dupfd >= 0)
1366 s[0] = n->ndup.dupfd + '0';
1372 cmdtxt(n->nfile.fname);
1378 cmdtxt(union node *n)
1381 struct nodelist *lp;
1387 cmdtxt(n->nbinary.ch1);
1389 cmdtxt(n->nbinary.ch2);
1392 cmdtxt(n->nbinary.ch1);
1394 cmdtxt(n->nbinary.ch2);
1397 cmdtxt(n->nbinary.ch1);
1399 cmdtxt(n->nbinary.ch2);
1402 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1410 cmdtxt(n->nredir.n);
1415 cmdtxt(n->nredir.n);
1419 cmdtxt(n->nif.test);
1421 cmdtxt(n->nif.ifpart);
1426 cmdtxt(n->nbinary.ch1);
1427 cmdtxtdogroup(n->nbinary.ch2);
1431 cmdtxt(n->nbinary.ch1);
1432 cmdtxtdogroup(n->nbinary.ch2);
1436 cmdputs(n->nfor.var);
1441 cmdputs(n->ncase.expr->narg.text);
1445 cmdputs(n->narg.text);
1450 cmdtxt(n->nnot.com);
1453 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1458 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1464 cmdputs(n->narg.text);
1467 cmdtxtredir(n, ">", 1);
1470 cmdtxtredir(n, ">>", 1);
1473 cmdtxtredir(n, ">&", 1);
1476 cmdtxtredir(n, ">|", 1);
1479 cmdtxtredir(n, "<", 0);
1482 cmdtxtredir(n, "<>", 0);
1485 cmdtxtredir(n, "<&", 0);
1500 cmdputs(const char *s)
1511 while ((c = *p++) != '\0') {
1514 else if (c == CTLVAR) {
1519 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1521 } else if (c == '=' && subtype != 0) {
1522 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1527 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1528 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1532 } else if (c == CTLENDVAR) {
1534 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1544 } else if (c == CTLARI) {
1552 } else if (c == CTLENDARI) {
1553 if (--cmdnleft > 0) {
1557 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1558 cmdnleft++; /* ignore */
1561 if (--cmdnleft <= 0) {