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 */
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 void printjobcmd(struct job *);
148 static void showjob(struct job *, int);
152 * Turn job control on and off.
172 out2fmt_flush("sh: can't access tty; job control turned off\n");
181 if (on == jobctl || rootshell == 0)
186 if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
188 while (i <= 2 && !isatty(i))
191 (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) {
198 * Keep our TTY file descriptor out of the way of
199 * the user's redirections.
201 if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
208 do { /* while we are in the background */
209 initialpgrp = tcgetpgrp(ttyfd);
210 if (initialpgrp < 0) {
214 if (initialpgrp != getpgrp()) {
228 tcsetpgrp(ttyfd, rootpid);
229 } else { /* turning job control off */
230 setpgid(0, initialpgrp);
232 tcsetpgrp(ttyfd, initialpgrp);
247 fgcmd(int argc __unused, char **argv __unused)
254 jp = getjob(*argptr);
256 error("job not created under job control");
259 pgrp = jp->ps[0].pid;
261 tcsetpgrp(ttyfd, pgrp);
265 status = waitforjob(jp, (int *)NULL);
272 bgcmd(int argc __unused, char **argv __unused)
278 jp = getjob(*argptr);
280 error("job not created under job control");
281 if (jp->state == JOBDONE)
285 out1fmt("[%td] ", jp - jobtab + 1);
287 } while (*argptr != NULL && *++argptr != NULL);
293 restartjob(struct job *jp)
298 if (jp->state == JOBDONE)
302 kill(-jp->ps[0].pid, SIGCONT);
303 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
304 if (WIFSTOPPED(ps->status)) {
315 jobscmd(int argc __unused, char *argv[] __unused)
320 mode = SHOWJOBS_DEFAULT;
321 while ((ch = nextopt("lps")) != '\0') {
324 mode = SHOWJOBS_VERBOSE;
327 mode = SHOWJOBS_PGIDS;
330 mode = SHOWJOBS_PIDS;
338 while ((id = *argptr++) != NULL)
339 showjob(getjob(id), mode);
345 printjobcmd(struct job *jp)
350 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
359 showjob(struct job *jp, int mode)
363 const char *statestr, *coredump;
366 int col, curr, i, jobno, prev, procno, status;
369 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
370 jobno = jp - jobtab + 1;
373 if ((j = getcurjob(NULL)) != NULL) {
374 curr = j - jobtab + 1;
375 if ((j = getcurjob(j)) != NULL)
376 prev = j - jobtab + 1;
380 status = jp->ps[jp->nprocs - 1].status;
381 if (jp->state == 0) {
382 statestr = "Running";
384 } else if (jp->state == JOBSTOPPED) {
385 ps = jp->ps + jp->nprocs - 1;
386 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
388 if (WIFSTOPPED(ps->status))
389 i = WSTOPSIG(ps->status);
392 statestr = strsignal(i);
393 if (statestr == NULL)
394 statestr = "Suspended";
396 } else if (WIFEXITED(status)) {
397 if (WEXITSTATUS(status) == 0)
400 fmtstr(statebuf, sizeof(statebuf), "Done(%d)",
401 WEXITSTATUS(status));
405 i = WTERMSIG(status);
406 statestr = strsignal(i);
407 if (statestr == NULL)
408 statestr = "Unknown signal";
409 if (WCOREDUMP(status))
410 coredump = " (core dumped)";
413 for (ps = jp->ps ; procno > 0 ; ps++, procno--) { /* for each process */
414 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
415 out1fmt("%d\n", (int)ps->pid);
418 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
420 if (jobno == curr && ps == jp->ps)
422 else if (jobno == prev && ps == jp->ps)
427 fmtstr(s, 64, "[%d] %c ", jobno, c);
429 fmtstr(s, 64, " %c ", c);
432 if (mode == SHOWJOBS_VERBOSE) {
433 fmtstr(s, 64, "%d ", (int)ps->pid);
440 col += strlen(statestr) + strlen(coredump);
446 if (mode == SHOWJOBS_VERBOSE) {
455 * Print a list of jobs. If "change" is nonzero, only print jobs whose
456 * statuses have changed since the last call to showjobs.
458 * If the shell is interrupted in the process of creating a job, the
459 * result may be a job structure containing zero processes. Such structures
460 * will be freed here.
464 showjobs(int change, int mode)
469 TRACE(("showjobs(%d) called\n", change));
471 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
474 if (jp->nprocs == 0) {
478 if (change && ! jp->changed)
481 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
483 /* Hack: discard jobs for which $! has not been
484 * referenced in interactive mode when they terminate.
486 if (jp->state == JOBDONE && !jp->remembered &&
487 (iflag || jp != bgjob)) {
496 * Mark a job structure as unused.
500 freejob(struct job *jp)
508 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
509 if (ps->cmd != nullstr)
512 if (jp->ps != &jp->ps0)
524 waitcmd(int argc __unused, char **argv __unused)
531 return (waitcmdloop(NULL));
534 job = getjob_nonotfound(*argptr);
538 retval = waitcmdloop(job);
540 } while (*argptr != NULL);
546 waitcmdloop(struct job *job)
548 int status, retval, sig;
552 * Loop until a process is terminated or stopped, or a SIGINT is
558 if (job->state == JOBDONE) {
559 status = job->ps[job->nprocs - 1].status;
560 if (WIFEXITED(status))
561 retval = WEXITSTATUS(status);
563 retval = WTERMSIG(status) + 128;
564 if (! iflag || ! job->changed)
574 for (jp = jobtab ; jp < jobtab + njobs; jp++)
575 if (jp->used && jp->state == JOBDONE) {
576 if (! iflag || ! jp->changed)
584 for (jp = jobtab ; ; jp++) {
585 if (jp >= jobtab + njobs) { /* no running procs */
588 if (jp->used && jp->state == 0)
592 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
594 sig = pendingsig_waitcmd;
595 pendingsig_waitcmd = 0;
602 jobidcmd(int argc __unused, char **argv __unused)
608 jp = getjob(*argptr);
609 for (i = 0 ; i < jp->nprocs ; ) {
610 out1fmt("%d", (int)jp->ps[i].pid);
611 out1c(++i < jp->nprocs? ' ' : '\n');
619 * Convert a job name to a job structure.
623 getjob_nonotfound(const char *name)
626 struct job *found, *jp;
635 error("No current job");
638 if (name[0] == '%') {
639 if (is_digit(name[1])) {
640 jobno = number(name + 1);
641 if (jobno > 0 && jobno <= njobs
642 && jobtab[jobno - 1].used != 0)
643 return &jobtab[jobno - 1];
645 } else if ((name[1] == '%' || name[1] == '+') &&
647 if ((jp = getcurjob(NULL)) == NULL)
648 error("No current job");
650 } else if (name[1] == '-' && name[2] == '\0') {
651 if ((jp = getcurjob(NULL)) == NULL ||
652 (jp = getcurjob(jp)) == NULL)
653 error("No previous job");
656 } else if (name[1] == '?') {
658 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
659 if (jp->used && jp->nprocs > 0
660 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
662 error("%s: ambiguous", name);
669 namelen = strlen(name);
671 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
672 if (jp->used && jp->nprocs > 0
673 && strncmp(jp->ps[0].cmd, name + 1,
676 error("%s: ambiguous", name);
683 } else if (is_number(name)) {
684 pid = (pid_t)number(name);
685 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
686 if (jp->used && jp->nprocs > 0
687 && jp->ps[jp->nprocs - 1].pid == pid)
696 getjob(const char *name)
700 jp = getjob_nonotfound(name);
702 error("No such job: %s", name);
708 killjob(const char *name, int sig)
714 if (jp->state == JOBDONE)
717 return kill(-jp->ps[0].pid, sig);
720 for (i = 0; i < jp->nprocs; i++)
721 if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
722 if (kill(jp->ps[i].pid, sig) == 0)
730 * Return a new job structure,
734 makejob(union node *node __unused, int nprocs)
739 for (i = njobs, jp = jobtab ; ; jp++) {
743 jobtab = ckmalloc(4 * sizeof jobtab[0]);
748 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
749 memcpy(jp, jobtab, njobs * sizeof jp[0]);
751 /* Relocate `next' pointers and list head */
753 jobmru = &jp[jobmru - jobtab];
754 for (i = 0; i < njobs; i++)
755 if (jp[i].next != NULL)
756 jp[i].next = &jp[jp[i].next -
760 bgjob = &jp[bgjob - jobtab];
761 /* Relocate `ps' pointers */
762 for (i = 0; i < njobs; i++)
763 if (jp[i].ps == &jobtab[i].ps0)
764 jp[i].ps = &jp[i].ps0;
769 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
789 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
794 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
801 setcurjob(struct job *cj)
803 struct job *jp, *prev;
805 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
808 prev->next = jp->next;
821 deljob(struct job *j)
823 struct job *jp, *prev;
825 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
828 prev->next = jp->next;
837 * Return the most recently used job that isn't `nj', and preferably one
841 getcurjob(struct job *nj)
845 /* Try to find a stopped one.. */
846 for (jp = jobmru; jp != NULL; jp = jp->next)
847 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
849 /* Otherwise the most recently used job that isn't `nj' */
850 for (jp = jobmru; jp != NULL; jp = jp->next)
851 if (jp->used && jp != nj)
860 * Fork of a subshell. If we are doing job control, give the subshell its
861 * own process group. Jp is a job structure that the job is to be added to.
862 * N is the command that will be evaluated by the child. Both jp and n may
863 * be NULL. The mode parameter can be one of the following:
864 * FORK_FG - Fork off a foreground process.
865 * FORK_BG - Fork off a background process.
866 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
867 * process group even if job control is on.
869 * When job control is turned off, background processes have their standard
870 * input redirected to /dev/null (except for the second and later processes
875 forkshell(struct job *jp, union node *n, int mode)
880 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
883 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
888 TRACE(("Fork failed, errno=%d\n", errno));
890 error("Cannot fork: %s", strerror(errno));
897 TRACE(("Child shell %d\n", (int)getpid()));
900 handler = &main_handler;
906 jobctl = 0; /* do job control only in root shell */
907 if (wasroot && mode != FORK_NOJOB && mflag) {
908 if (jp == NULL || jp->nprocs == 0)
911 pgrp = jp->ps[0].pid;
912 if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
914 /*** this causes superfluous TIOCSPGRPS ***/
915 if (tcsetpgrp(ttyfd, pgrp) < 0)
916 error("tcsetpgrp failed, errno=%d", errno);
920 } else if (mode == FORK_BG) {
923 if ((jp == NULL || jp->nprocs == 0) &&
924 ! fd0_redirected_p ()) {
926 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
927 error("cannot open %s: %s",
928 _PATH_DEVNULL, strerror(errno));
932 if (mode == FORK_BG) {
935 if ((jp == NULL || jp->nprocs == 0) &&
936 ! fd0_redirected_p ()) {
938 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
939 error("cannot open %s: %s",
940 _PATH_DEVNULL, strerror(errno));
945 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
949 if (wasroot && iflag) {
956 if (rootshell && mode != FORK_NOJOB && mflag) {
957 if (jp == NULL || jp->nprocs == 0)
960 pgrp = jp->ps[0].pid;
963 if (mode == FORK_BG) {
964 if (bgjob != NULL && bgjob->state == JOBDONE &&
965 !bgjob->remembered && !iflag)
967 backgndpid = pid; /* set $! */
971 struct procstat *ps = &jp->ps[jp->nprocs++];
975 if (iflag && rootshell && n)
976 ps->cmd = commandtext(n);
977 jp->foreground = mode == FORK_FG;
983 TRACE(("In parent shell: child = %d\n", (int)pid));
989 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
992 struct jmploc jmploc;
993 struct jmploc *savehandler;
995 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
999 savehandler = handler;
1002 TRACE(("Vfork failed, errno=%d\n", errno));
1004 error("Cannot fork: %s", strerror(errno));
1007 TRACE(("Child shell %d\n", (int)getpid()));
1008 if (setjmp(jmploc.loc))
1018 shellexec(argv, envp, path, idx);
1020 handler = savehandler;
1022 struct procstat *ps = &jp->ps[jp->nprocs++];
1032 TRACE(("In parent shell: child = %d\n", (int)pid));
1038 * Wait for job to finish.
1040 * Under job control we have the problem that while a child process is
1041 * running interrupts generated by the user are sent to the child but not
1042 * to the shell. This means that an infinite loop started by an inter-
1043 * active user may be hard to kill. With job control turned off, an
1044 * interactive user may place an interactive program inside a loop. If
1045 * the interactive program catches interrupts, the user doesn't want
1046 * these interrupts to also abort the loop. The approach we take here
1047 * is to have the shell ignore interrupt signals while waiting for a
1048 * foreground process to terminate, and then send itself an interrupt
1049 * signal if the child process was terminated by an interrupt signal.
1050 * Unfortunately, some programs want to do a bit of cleanup and then
1051 * exit on interrupt; unless these processes terminate themselves by
1052 * sending a signal to themselves (instead of calling exit) they will
1053 * confuse this approach.
1057 waitforjob(struct job *jp, int *signaled)
1060 int propagate_int = jp->jobctl && jp->foreground;
1066 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1067 while (jp->state == 0)
1068 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
1069 DOWAIT_SIG_TRAP : 0), jp) == -1)
1073 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
1074 error("tcsetpgrp failed, errno=%d\n", errno);
1076 if (jp->state == JOBSTOPPED)
1079 status = jp->ps[jp->nprocs - 1].status;
1080 if (signaled != NULL)
1081 *signaled = WIFSIGNALED(status);
1082 /* convert to 8 bits */
1083 if (WIFEXITED(status))
1084 st = WEXITSTATUS(status);
1086 else if (WIFSTOPPED(status))
1087 st = WSTOPSIG(status) + 128;
1090 st = WTERMSIG(status) + 128;
1091 if (! JOBS || jp->state == JOBDONE)
1093 if (int_pending()) {
1094 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1098 else if (rootshell && propagate_int &&
1099 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1100 kill(getpid(), SIGINT);
1108 dummy_handler(int sig __unused)
1113 * Wait for a process to terminate.
1117 dowait(int mode, struct job *job)
1119 struct sigaction sa, osa;
1120 sigset_t mask, omask;
1123 struct procstat *sp;
1125 struct job *thisjob;
1132 int restore_sigchld;
1134 TRACE(("dowait(%d, %p) called\n", mode, job));
1135 restore_sigchld = 0;
1136 if ((mode & DOWAIT_SIG) != 0) {
1138 sigprocmask(SIG_BLOCK, &mask, &omask);
1140 if (!issigchldtrapped()) {
1141 restore_sigchld = 1;
1142 sa.sa_handler = dummy_handler;
1144 sigemptyset(&sa.sa_mask);
1145 sigaction(SIGCHLD, &sa, &osa);
1151 wflags = WUNTRACED | WCONTINUED;
1155 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1157 pid = wait3(&status, wflags, (struct rusage *)NULL);
1158 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1159 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1161 if (((mode & DOWAIT_SIG_TRAP) != 0 ?
1162 pendingsig : pendingsig_waitcmd) != 0) {
1170 } while (pid == -1 && errno == EINTR);
1171 if (pid == -1 && errno == ECHILD && job != NULL)
1172 job->state = JOBDONE;
1173 if ((mode & DOWAIT_SIG) != 0) {
1174 if (restore_sigchld)
1175 sigaction(SIGCHLD, &osa, NULL);
1176 sigprocmask(SIG_SETMASK, &omask, NULL);
1183 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1184 if (jp->used && jp->nprocs > 0) {
1187 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1190 if (sp->pid == pid && (sp->status == -1 ||
1191 WIFSTOPPED(sp->status))) {
1192 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1193 (int)pid, sp->status,
1195 if (WIFCONTINUED(status)) {
1199 sp->status = status;
1202 if (sp->status == -1)
1204 else if (WIFSTOPPED(sp->status))
1207 if (stopped) { /* stopped or done */
1208 int state = done? JOBDONE : JOBSTOPPED;
1209 if (jp->state != state) {
1210 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1213 if (done && !jp->remembered &&
1214 !iflag && jp != bgjob)
1226 if (!thisjob || thisjob->state == 0)
1228 else if ((!rootshell || !iflag || thisjob == job) &&
1229 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1232 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1233 if (WIFSIGNALED(sp->status)) {
1234 sig = WTERMSIG(sp->status);
1235 coredump = WCOREDUMP(sp->status);
1237 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1238 sigstr = strsignal(sig);
1242 out2str("Unknown signal");
1244 out2str(" (core dumped)");
1249 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1250 thisjob->changed = 1;
1258 * return 1 if there are stopped jobs, otherwise 0
1260 int job_warning = 0;
1269 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1272 if (jp->state == JOBSTOPPED) {
1273 out2fmt_flush("You have stopped jobs.\n");
1286 while (njobs > 0 && dowait(0, NULL) > 0)
1294 return backgndpid != -1;
1301 if (bgjob != NULL && !forcelocal)
1302 bgjob->remembered = 1;
1307 * Return a string identifying a command (to be printed by the
1311 static char *cmdnextc;
1312 static int cmdnleft;
1313 #define MAXCMDTEXT 200
1316 commandtext(union node *n)
1320 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1321 cmdnleft = MAXCMDTEXT - 4;
1329 cmdtxtdogroup(union node *n)
1338 cmdtxtredir(union node *n, const char *op, int deffd)
1342 if (n->nfile.fd != deffd) {
1343 s[0] = n->nfile.fd + '0';
1348 if (n->type == NTOFD || n->type == NFROMFD) {
1349 if (n->ndup.dupfd >= 0)
1350 s[0] = n->ndup.dupfd + '0';
1356 cmdtxt(n->nfile.fname);
1362 cmdtxt(union node *n)
1365 struct nodelist *lp;
1371 cmdtxt(n->nbinary.ch1);
1373 cmdtxt(n->nbinary.ch2);
1376 cmdtxt(n->nbinary.ch1);
1378 cmdtxt(n->nbinary.ch2);
1381 cmdtxt(n->nbinary.ch1);
1383 cmdtxt(n->nbinary.ch2);
1386 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1394 cmdtxt(n->nredir.n);
1399 cmdtxt(n->nredir.n);
1403 cmdtxt(n->nif.test);
1405 cmdtxt(n->nif.ifpart);
1410 cmdtxt(n->nbinary.ch1);
1411 cmdtxtdogroup(n->nbinary.ch2);
1415 cmdtxt(n->nbinary.ch1);
1416 cmdtxtdogroup(n->nbinary.ch2);
1420 cmdputs(n->nfor.var);
1425 cmdputs(n->ncase.expr->narg.text);
1429 cmdputs(n->narg.text);
1434 cmdtxt(n->nnot.com);
1437 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1442 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1448 cmdputs(n->narg.text);
1451 cmdtxtredir(n, ">", 1);
1454 cmdtxtredir(n, ">>", 1);
1457 cmdtxtredir(n, ">&", 1);
1460 cmdtxtredir(n, ">|", 1);
1463 cmdtxtredir(n, "<", 0);
1466 cmdtxtredir(n, "<>", 0);
1469 cmdtxtredir(n, "<&", 0);
1484 cmdputs(const char *s)
1495 while ((c = *p++) != '\0') {
1498 else if (c == CTLVAR) {
1503 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1505 } else if (c == '=' && subtype != 0) {
1506 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1511 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1512 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1516 } else if (c == CTLENDVAR) {
1518 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1528 } else if (c == CTLARI) {
1536 } else if (c == CTLENDARI) {
1537 if (--cmdnleft > 0) {
1541 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1542 cmdnleft++; /* ignore */
1545 if (--cmdnleft <= 0) {