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
33 #include <sys/ioctl.h>
34 #include <sys/param.h>
35 #include <sys/resource.h>
49 #undef CEOF /* syntax.h redefines this */
72 * A job structure contains information about a job. A job is either a
73 * single process or a set of processes contained in a pipeline. In the
74 * latter case, pidlist will be non-NULL, and will point to a -1 terminated
79 pid_t pid; /* process id */
80 int status; /* status flags (defined above) */
81 char *cmd; /* text of command being run */
86 #define JOBSTOPPED 1 /* all procs are stopped */
87 #define JOBDONE 2 /* all procs are completed */
91 struct procstat ps0; /* status of process */
92 struct procstat *ps; /* status or processes when more than one */
93 short nprocs; /* number of processes */
94 pid_t pgrp; /* process group of this job */
95 char state; /* true if job is finished */
96 char used; /* true if this entry is in used */
97 char changed; /* true if status has changed */
98 char foreground; /* true if running in the foreground */
99 char remembered; /* true if $! referenced */
100 char pipefail; /* pass any non-zero status */
102 char jobctl; /* job running under job control */
103 struct job *next; /* job used after this one */
108 static struct job *jobtab; /* array of jobs */
109 static int njobs; /* size of array */
110 static pid_t backgndpid = -1; /* pid of last background process */
111 static struct job *bgjob = NULL; /* last background process */
113 static struct job *jobmru; /* most recently used job list */
114 static pid_t initialpgrp; /* pgrp of shell on invocation */
116 static int ttyfd = -1;
118 /* mode flags for dowait */
119 #define DOWAIT_BLOCK 0x1 /* wait until a child exits */
120 #define DOWAIT_SIG 0x2 /* if DOWAIT_BLOCK, abort on signal */
121 #define DOWAIT_SIG_TRAP 0x4 /* if DOWAIT_SIG, abort on trapped signal only */
124 static void restartjob(struct job *);
126 static void freejob(struct job *);
127 static int waitcmdloop(struct job *);
128 static struct job *getjob_nonotfound(const char *);
129 static struct job *getjob(const char *);
130 pid_t killjob(const char *, int);
131 static pid_t dowait(int, struct job *);
132 static void checkzombies(void);
133 static void cmdtxt(union node *);
134 static void cmdputs(const char *);
136 static void setcurjob(struct job *);
137 static void deljob(struct job *);
138 static struct job *getcurjob(struct job *);
140 static int getjobstatus(const struct job *);
141 static void printjobcmd(struct job *);
142 static void showjob(struct job *, int);
146 * Turn job control on and off.
166 out2fmt_flush("sh: can't access tty; job control turned off\n");
175 if (on == jobctl || rootshell == 0)
180 if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
182 while (i <= 2 && !isatty(i))
185 (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) {
192 * Keep our TTY file descriptor out of the way of
193 * the user's redirections.
195 if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
202 do { /* while we are in the background */
203 initialpgrp = tcgetpgrp(ttyfd);
204 if (initialpgrp < 0) {
208 if (initialpgrp != getpgrp()) {
222 tcsetpgrp(ttyfd, rootpid);
223 } else { /* turning job control off */
224 setpgid(0, initialpgrp);
226 tcsetpgrp(ttyfd, initialpgrp);
241 fgcmd(int argc __unused, char **argv __unused)
248 jp = getjob(*argptr);
250 error("job not created under job control");
253 pgrp = jp->ps[0].pid;
255 tcsetpgrp(ttyfd, pgrp);
259 status = waitforjob(jp, (int *)NULL);
266 bgcmd(int argc __unused, char **argv __unused)
272 jp = getjob(*argptr);
274 error("job not created under job control");
275 if (jp->state == JOBDONE)
279 out1fmt("[%td] ", jp - jobtab + 1);
281 } while (*argptr != NULL && *++argptr != NULL);
287 restartjob(struct job *jp)
292 if (jp->state == JOBDONE)
296 kill(-jp->ps[0].pid, SIGCONT);
297 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
298 if (WIFSTOPPED(ps->status)) {
309 jobscmd(int argc __unused, char *argv[] __unused)
314 mode = SHOWJOBS_DEFAULT;
315 while ((ch = nextopt("lps")) != '\0') {
318 mode = SHOWJOBS_VERBOSE;
321 mode = SHOWJOBS_PGIDS;
324 mode = SHOWJOBS_PIDS;
332 while ((id = *argptr++) != NULL)
333 showjob(getjob(id), mode);
338 static int getjobstatus(const struct job *jp)
343 return (jp->ps[jp->nprocs - 1].status);
344 for (i = jp->nprocs - 1; i >= 0; i--) {
345 status = jp->ps[i].status;
353 printjobcmd(struct job *jp)
358 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
367 showjob(struct job *jp, int mode)
371 const char *statestr, *coredump;
374 int col, curr, i, jobno, prev, procno, status;
377 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
378 jobno = jp - jobtab + 1;
381 if ((j = getcurjob(NULL)) != NULL) {
382 curr = j - jobtab + 1;
383 if ((j = getcurjob(j)) != NULL)
384 prev = j - jobtab + 1;
388 status = getjobstatus(jp);
389 if (jp->state == 0) {
390 statestr = "Running";
392 } else if (jp->state == JOBSTOPPED) {
393 ps = jp->ps + jp->nprocs - 1;
394 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
396 if (WIFSTOPPED(ps->status))
397 i = WSTOPSIG(ps->status);
400 statestr = strsignal(i);
401 if (statestr == NULL)
402 statestr = "Suspended";
404 } else if (WIFEXITED(status)) {
405 if (WEXITSTATUS(status) == 0)
408 fmtstr(statebuf, sizeof(statebuf), "Done(%d)",
409 WEXITSTATUS(status));
413 i = WTERMSIG(status);
414 statestr = strsignal(i);
415 if (statestr == NULL)
416 statestr = "Unknown signal";
417 if (WCOREDUMP(status))
418 coredump = " (core dumped)";
421 for (ps = jp->ps ; procno > 0 ; ps++, procno--) { /* for each process */
422 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
423 out1fmt("%d\n", (int)ps->pid);
426 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
428 if (jobno == curr && ps == jp->ps)
430 else if (jobno == prev && ps == jp->ps)
435 fmtstr(s, 64, "[%d] %c ", jobno, c);
437 fmtstr(s, 64, " %c ", c);
440 if (mode == SHOWJOBS_VERBOSE) {
441 fmtstr(s, 64, "%d ", (int)ps->pid);
448 col += strlen(statestr) + strlen(coredump);
454 if (mode == SHOWJOBS_VERBOSE) {
463 * Print a list of jobs. If "change" is nonzero, only print jobs whose
464 * statuses have changed since the last call to showjobs.
466 * If the shell is interrupted in the process of creating a job, the
467 * result may be a job structure containing zero processes. Such structures
468 * will be freed here.
472 showjobs(int change, int mode)
477 TRACE(("showjobs(%d) called\n", change));
479 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
482 if (jp->nprocs == 0) {
486 if (change && ! jp->changed)
489 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
491 /* Hack: discard jobs for which $! has not been
492 * referenced in interactive mode when they terminate.
494 if (jp->state == JOBDONE && !jp->remembered &&
495 (iflag || jp != bgjob)) {
504 * Mark a job structure as unused.
508 freejob(struct job *jp)
516 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
517 if (ps->cmd != nullstr)
520 if (jp->ps != &jp->ps0)
532 waitcmd(int argc __unused, char **argv __unused)
539 return (waitcmdloop(NULL));
542 job = getjob_nonotfound(*argptr);
546 retval = waitcmdloop(job);
548 } while (*argptr != NULL);
554 waitcmdloop(struct job *job)
556 int status, retval, sig;
560 * Loop until a process is terminated or stopped, or a SIGINT is
566 if (job->state == JOBDONE) {
567 status = getjobstatus(job);
568 if (WIFEXITED(status))
569 retval = WEXITSTATUS(status);
571 retval = WTERMSIG(status) + 128;
572 if (! iflag || ! job->changed)
584 for (jp = jobtab ; jp < jobtab + njobs; jp++)
585 if (jp->used && jp->state == JOBDONE) {
586 if (! iflag || ! jp->changed)
594 for (jp = jobtab ; ; jp++) {
595 if (jp >= jobtab + njobs) { /* no running procs */
598 if (jp->used && jp->state == 0)
602 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
604 sig = pendingsig_waitcmd;
605 pendingsig_waitcmd = 0;
612 jobidcmd(int argc __unused, char **argv __unused)
618 jp = getjob(*argptr);
619 for (i = 0 ; i < jp->nprocs ; ) {
620 out1fmt("%d", (int)jp->ps[i].pid);
621 out1c(++i < jp->nprocs? ' ' : '\n');
629 * Convert a job name to a job structure.
633 getjob_nonotfound(const char *name)
636 struct job *found, *jp;
645 error("No current job");
648 if (name[0] == '%') {
649 if (is_digit(name[1])) {
650 jobno = number(name + 1);
651 if (jobno > 0 && jobno <= njobs
652 && jobtab[jobno - 1].used != 0)
653 return &jobtab[jobno - 1];
655 } else if ((name[1] == '%' || name[1] == '+') &&
657 if ((jp = getcurjob(NULL)) == NULL)
658 error("No current job");
660 } else if (name[1] == '-' && name[2] == '\0') {
661 if ((jp = getcurjob(NULL)) == NULL ||
662 (jp = getcurjob(jp)) == NULL)
663 error("No previous job");
666 } else if (name[1] == '?') {
668 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
669 if (jp->used && jp->nprocs > 0
670 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
672 error("%s: ambiguous", name);
679 namelen = strlen(name);
681 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
682 if (jp->used && jp->nprocs > 0
683 && strncmp(jp->ps[0].cmd, name + 1,
686 error("%s: ambiguous", name);
693 } else if (is_number(name)) {
694 pid = (pid_t)number(name);
695 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
696 if (jp->used && jp->nprocs > 0
697 && jp->ps[jp->nprocs - 1].pid == pid)
706 getjob(const char *name)
710 jp = getjob_nonotfound(name);
712 error("No such job: %s", name);
718 killjob(const char *name, int sig)
724 if (jp->state == JOBDONE)
727 return kill(-jp->ps[0].pid, sig);
730 for (i = 0; i < jp->nprocs; i++)
731 if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
732 if (kill(jp->ps[i].pid, sig) == 0)
740 * Return a new job structure,
744 makejob(union node *node __unused, int nprocs)
749 for (i = njobs, jp = jobtab ; ; jp++) {
753 jobtab = ckmalloc(4 * sizeof jobtab[0]);
758 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
759 memcpy(jp, jobtab, njobs * sizeof jp[0]);
761 /* Relocate `next' pointers and list head */
763 jobmru = &jp[jobmru - jobtab];
764 for (i = 0; i < njobs; i++)
765 if (jp[i].next != NULL)
766 jp[i].next = &jp[jp[i].next -
770 bgjob = &jp[bgjob - jobtab];
771 /* Relocate `ps' pointers */
772 for (i = 0; i < njobs; i++)
773 if (jp[i].ps == &jobtab[i].ps0)
774 jp[i].ps = &jp[i].ps0;
779 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
794 jp->pipefail = pipefailflag;
800 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
805 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
812 setcurjob(struct job *cj)
814 struct job *jp, *prev;
816 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
819 prev->next = jp->next;
832 deljob(struct job *j)
834 struct job *jp, *prev;
836 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
839 prev->next = jp->next;
848 * Return the most recently used job that isn't `nj', and preferably one
852 getcurjob(struct job *nj)
856 /* Try to find a stopped one.. */
857 for (jp = jobmru; jp != NULL; jp = jp->next)
858 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
860 /* Otherwise the most recently used job that isn't `nj' */
861 for (jp = jobmru; jp != NULL; jp = jp->next)
862 if (jp->used && jp != nj)
871 * Fork of a subshell. If we are doing job control, give the subshell its
872 * own process group. Jp is a job structure that the job is to be added to.
873 * N is the command that will be evaluated by the child. Both jp and n may
874 * be NULL. The mode parameter can be one of the following:
875 * FORK_FG - Fork off a foreground process.
876 * FORK_BG - Fork off a background process.
877 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
878 * process group even if job control is on.
880 * When job control is turned off, background processes have their standard
881 * input redirected to /dev/null (except for the second and later processes
886 forkshell(struct job *jp, union node *n, int mode)
891 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
894 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
899 TRACE(("Fork failed, errno=%d\n", errno));
901 error("Cannot fork: %s", strerror(errno));
908 TRACE(("Child shell %d\n", (int)getpid()));
911 handler = &main_handler;
917 jobctl = 0; /* do job control only in root shell */
918 if (wasroot && mode != FORK_NOJOB && mflag) {
919 if (jp == NULL || jp->nprocs == 0)
922 pgrp = jp->ps[0].pid;
923 if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
926 * Each process in a pipeline must have the tty
927 * pgrp set before running its code.
928 * Only for pipelines of three or more processes
929 * could this be reduced to two calls.
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;
1012 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
1014 inton = is_int_on();
1017 savehandler = handler;
1020 TRACE(("Vfork failed, errno=%d\n", errno));
1022 error("Cannot fork: %s", strerror(errno));
1025 TRACE(("Child shell %d\n", (int)getpid()));
1026 if (setjmp(jmploc.loc))
1036 shellexec(argv, envp, path, idx);
1038 handler = savehandler;
1040 struct procstat *ps = &jp->ps[jp->nprocs++];
1050 TRACE(("In parent shell: child = %d\n", (int)pid));
1056 * Wait for job to finish.
1058 * Under job control we have the problem that while a child process is
1059 * running interrupts generated by the user are sent to the child but not
1060 * to the shell. This means that an infinite loop started by an inter-
1061 * active user may be hard to kill. With job control turned off, an
1062 * interactive user may place an interactive program inside a loop. If
1063 * the interactive program catches interrupts, the user doesn't want
1064 * these interrupts to also abort the loop. The approach we take here
1065 * is to have the shell ignore interrupt signals while waiting for a
1066 * foreground process to terminate, and then send itself an interrupt
1067 * signal if the child process was terminated by an interrupt signal.
1068 * Unfortunately, some programs want to do a bit of cleanup and then
1069 * exit on interrupt; unless these processes terminate themselves by
1070 * sending a signal to themselves (instead of calling exit) they will
1071 * confuse this approach.
1075 waitforjob(struct job *jp, int *signaled)
1078 int propagate_int = jp->jobctl && jp->foreground;
1084 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1085 while (jp->state == 0)
1086 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
1087 DOWAIT_SIG_TRAP : 0), jp) == -1)
1091 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
1092 error("tcsetpgrp failed, errno=%d\n", errno);
1094 if (jp->state == JOBSTOPPED)
1097 status = getjobstatus(jp);
1098 if (signaled != NULL)
1099 *signaled = WIFSIGNALED(status);
1100 /* convert to 8 bits */
1101 if (WIFEXITED(status))
1102 st = WEXITSTATUS(status);
1104 else if (WIFSTOPPED(status))
1105 st = WSTOPSIG(status) + 128;
1108 st = WTERMSIG(status) + 128;
1109 if (! JOBS || jp->state == JOBDONE)
1111 if (int_pending()) {
1112 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1116 else if (rootshell && propagate_int &&
1117 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1118 kill(getpid(), SIGINT);
1126 dummy_handler(int sig __unused)
1131 * Wait for a process to terminate.
1135 dowait(int mode, struct job *job)
1137 struct sigaction sa, osa;
1138 sigset_t mask, omask;
1141 struct procstat *sp;
1143 struct job *thisjob;
1150 int restore_sigchld;
1152 TRACE(("dowait(%d, %p) called\n", mode, job));
1153 restore_sigchld = 0;
1154 if ((mode & DOWAIT_SIG) != 0) {
1156 sigprocmask(SIG_BLOCK, &mask, &omask);
1158 if (!issigchldtrapped()) {
1159 restore_sigchld = 1;
1160 sa.sa_handler = dummy_handler;
1162 sigemptyset(&sa.sa_mask);
1163 sigaction(SIGCHLD, &sa, &osa);
1169 wflags = WUNTRACED | WCONTINUED;
1173 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1175 pid = wait3(&status, wflags, (struct rusage *)NULL);
1176 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1177 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1179 if (((mode & DOWAIT_SIG_TRAP) != 0 ?
1180 pendingsig : pendingsig_waitcmd) != 0) {
1188 } while (pid == -1 && errno == EINTR);
1189 if (pid == -1 && errno == ECHILD && job != NULL)
1190 job->state = JOBDONE;
1191 if ((mode & DOWAIT_SIG) != 0) {
1192 if (restore_sigchld)
1193 sigaction(SIGCHLD, &osa, NULL);
1194 sigprocmask(SIG_SETMASK, &omask, NULL);
1201 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1202 if (jp->used && jp->nprocs > 0) {
1205 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1208 if (sp->pid == pid && (sp->status == -1 ||
1209 WIFSTOPPED(sp->status))) {
1210 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1211 (int)pid, sp->status,
1213 if (WIFCONTINUED(status)) {
1217 sp->status = status;
1220 if (sp->status == -1)
1222 else if (WIFSTOPPED(sp->status))
1225 if (stopped) { /* stopped or done */
1226 int state = done? JOBDONE : JOBSTOPPED;
1227 if (jp->state != state) {
1228 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1231 if (done && !jp->remembered &&
1232 !iflag && jp != bgjob)
1244 if (!thisjob || thisjob->state == 0)
1246 else if ((!rootshell || !iflag || thisjob == job) &&
1247 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1250 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1251 if (WIFSIGNALED(sp->status)) {
1252 sig = WTERMSIG(sp->status);
1253 coredump = WCOREDUMP(sp->status);
1255 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1256 sigstr = strsignal(sig);
1260 out2str("Unknown signal");
1262 out2str(" (core dumped)");
1267 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1268 thisjob->changed = 1;
1276 * return 1 if there are stopped jobs, otherwise 0
1278 int job_warning = 0;
1287 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1290 if (jp->state == JOBSTOPPED) {
1291 out2fmt_flush("You have stopped jobs.\n");
1304 while (njobs > 0 && dowait(0, NULL) > 0)
1312 return backgndpid != -1;
1319 if (bgjob != NULL && !forcelocal)
1320 bgjob->remembered = 1;
1325 * Return a string identifying a command (to be printed by the
1329 static char *cmdnextc;
1330 static int cmdnleft;
1331 #define MAXCMDTEXT 200
1334 commandtext(union node *n)
1338 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1339 cmdnleft = MAXCMDTEXT - 4;
1347 cmdtxtdogroup(union node *n)
1356 cmdtxtredir(union node *n, const char *op, int deffd)
1360 if (n->nfile.fd != deffd) {
1361 s[0] = n->nfile.fd + '0';
1366 if (n->type == NTOFD || n->type == NFROMFD) {
1367 if (n->ndup.dupfd >= 0)
1368 s[0] = n->ndup.dupfd + '0';
1374 cmdtxt(n->nfile.fname);
1380 cmdtxt(union node *n)
1383 struct nodelist *lp;
1389 cmdtxt(n->nbinary.ch1);
1391 cmdtxt(n->nbinary.ch2);
1394 cmdtxt(n->nbinary.ch1);
1396 cmdtxt(n->nbinary.ch2);
1399 cmdtxt(n->nbinary.ch1);
1401 cmdtxt(n->nbinary.ch2);
1404 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1412 cmdtxt(n->nredir.n);
1417 cmdtxt(n->nredir.n);
1421 cmdtxt(n->nif.test);
1423 cmdtxt(n->nif.ifpart);
1428 cmdtxt(n->nbinary.ch1);
1429 cmdtxtdogroup(n->nbinary.ch2);
1433 cmdtxt(n->nbinary.ch1);
1434 cmdtxtdogroup(n->nbinary.ch2);
1438 cmdputs(n->nfor.var);
1443 cmdputs(n->ncase.expr->narg.text);
1447 cmdputs(n->narg.text);
1452 cmdtxt(n->nnot.com);
1455 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1460 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1466 cmdputs(n->narg.text);
1469 cmdtxtredir(n, ">", 1);
1472 cmdtxtredir(n, ">>", 1);
1475 cmdtxtredir(n, ">&", 1);
1478 cmdtxtredir(n, ">|", 1);
1481 cmdtxtredir(n, "<", 0);
1484 cmdtxtredir(n, "<>", 0);
1487 cmdtxtredir(n, "<&", 0);
1502 cmdputs(const char *s)
1513 while ((c = *p++) != '\0') {
1516 else if (c == CTLVAR) {
1521 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1523 } else if (c == '=' && subtype != 0) {
1524 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1529 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1530 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1534 } else if (c == CTLENDVAR) {
1536 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1546 } else if (c == CTLARI) {
1554 } else if (c == CTLENDARI) {
1555 if (--cmdnleft > 0) {
1559 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1560 cmdnleft++; /* ignore */
1563 if (--cmdnleft <= 0) {