Lines Matching +full:next +full:- +full:mode

1 /*-
74 * latter case, pidlist will be non-NULL, and will point to a -1 terminated
100 char pipefail; /* pass any non-zero status */
103 struct job *next; /* job used after this one */ member
110 static pid_t backgndpid = -1; /* pid of last background process */
116 static int ttyfd = -1;
118 /* mode flags for dowait */
157 ttyfd = -1; in jobctl_notty()
178 if (ttyfd != -1) in setjobctl()
210 initialpgrp = -1; in setjobctl()
228 ttyfd = -1; in setjobctl()
249 if (jp->jobctl == 0) in fgcmd()
253 pgrp = jp->ps[0].pid; in fgcmd()
257 jp->foreground = 1; in fgcmd()
273 if (jp->jobctl == 0) in bgcmd()
275 if (jp->state == JOBDONE) in bgcmd()
278 jp->foreground = 0; in bgcmd()
279 out1fmt("[%td] ", jp - jobtab + 1); in bgcmd()
292 if (jp->state == JOBDONE) in restartjob()
296 kill(-jp->ps[0].pid, SIGCONT); in restartjob()
297 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { in restartjob()
298 if (WIFSTOPPED(ps->status)) { in restartjob()
299 ps->status = -1; in restartjob()
300 jp->state = 0; in restartjob()
312 int ch, mode; in jobscmd() local
314 mode = SHOWJOBS_DEFAULT; in jobscmd()
318 mode = SHOWJOBS_VERBOSE; in jobscmd()
321 mode = SHOWJOBS_PGIDS; in jobscmd()
324 mode = SHOWJOBS_PIDS; in jobscmd()
330 showjobs(0, mode); in jobscmd()
333 showjob(getjob(id), mode); in jobscmd()
342 if (!jp->pipefail) in getjobstatus()
343 return (jp->ps[jp->nprocs - 1].status); in getjobstatus()
344 for (i = jp->nprocs - 1; i >= 0; i--) { in getjobstatus()
345 status = jp->ps[i].status; in getjobstatus()
358 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { in printjobcmd()
359 out1str(ps->cmd); in printjobcmd()
367 showjob(struct job *jp, int mode) in showjob() argument
377 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs; in showjob()
378 jobno = jp - jobtab + 1; in showjob()
382 curr = j - jobtab + 1; in showjob()
384 prev = j - jobtab + 1; in showjob()
389 if (jp->state == 0) { in showjob()
392 } else if (jp->state == JOBSTOPPED) { in showjob()
393 ps = jp->ps + jp->nprocs - 1; in showjob()
394 while (!WIFSTOPPED(ps->status) && ps > jp->ps) in showjob()
395 ps--; in showjob()
396 if (WIFSTOPPED(ps->status)) in showjob()
397 i = WSTOPSIG(ps->status); in showjob()
399 i = -1; in showjob()
421 for (ps = jp->ps ; procno > 0 ; ps++, procno--) { /* for each process */ in showjob()
422 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) { in showjob()
423 out1fmt("%d\n", (int)ps->pid); in showjob()
426 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps) in showjob()
428 if (jobno == curr && ps == jp->ps) in showjob()
430 else if (jobno == prev && ps == jp->ps) in showjob()
431 c = '-'; in showjob()
434 if (ps == jp->ps) in showjob()
440 if (mode == SHOWJOBS_VERBOSE) { in showjob()
441 fmtstr(s, 64, "%d ", (int)ps->pid); in showjob()
445 if (ps == jp->ps) { in showjob()
454 if (mode == SHOWJOBS_VERBOSE) { in showjob()
455 out1str(ps->cmd); in showjob()
472 showjobs(int change, int mode) in showjobs() argument
480 if (! jp->used) in showjobs()
482 if (jp->nprocs == 0) { in showjobs()
486 if (change && ! jp->changed) in showjobs()
488 showjob(jp, mode); in showjobs()
489 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) { in showjobs()
490 jp->changed = 0; in showjobs()
492 * referenced in interactive mode when they terminate. in showjobs()
494 if (jp->state == JOBDONE && !jp->remembered && in showjobs()
516 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { in freejob()
517 if (ps->cmd != nullstr) in freejob()
518 ckfree(ps->cmd); in freejob()
520 if (jp->ps != &jp->ps0) in freejob()
521 ckfree(jp->ps); in freejob()
522 jp->used = 0; in freejob()
566 if (job->state == JOBDONE) { in waitcmdloop()
572 if (! iflag || ! job->changed) in waitcmdloop()
575 job->remembered = 0; in waitcmdloop()
586 if (jp->used && jp->state == JOBDONE) { in waitcmdloop()
587 if (! iflag || ! jp->changed) in waitcmdloop()
590 jp->remembered = 0; in waitcmdloop()
599 if (jp->used && jp->state == 0) in waitcmdloop()
603 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, job) != -1); in waitcmdloop()
620 for (i = 0 ; i < jp->nprocs ; ) { in jobidcmd()
621 out1fmt("%d", (int)jp->ps[i].pid); in jobidcmd()
622 out1c(++i < jp->nprocs? ' ' : '\n'); in jobidcmd()
653 && jobtab[jobno - 1].used != 0) in getjob_nonotfound()
654 return &jobtab[jobno - 1]; in getjob_nonotfound()
661 } else if (name[1] == '-' && name[2] == '\0') { in getjob_nonotfound()
669 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { in getjob_nonotfound()
670 if (jp->used && jp->nprocs > 0 in getjob_nonotfound()
671 && strstr(jp->ps[0].cmd, name + 2) != NULL) { in getjob_nonotfound()
682 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { in getjob_nonotfound()
683 if (jp->used && jp->nprocs > 0 in getjob_nonotfound()
684 && strncmp(jp->ps[0].cmd, name + 1, in getjob_nonotfound()
685 namelen - 1) == 0) { in getjob_nonotfound()
696 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { in getjob_nonotfound()
697 if (jp->used && jp->nprocs > 0 in getjob_nonotfound()
698 && jp->ps[jp->nprocs - 1].pid == pid) in getjob_nonotfound()
725 if (jp->state == JOBDONE) in killjob()
727 if (jp->jobctl) in killjob()
728 return kill(-jp->ps[0].pid, sig); in killjob()
729 ret = -1; in killjob()
731 for (i = 0; i < jp->nprocs; i++) in killjob()
732 if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) { in killjob()
733 if (kill(jp->ps[i].pid, sig) == 0) in killjob()
751 if (--i < 0) { in makejob()
762 /* Relocate `next' pointers and list head */ in makejob()
764 jobmru = &jp[jobmru - jobtab]; in makejob()
766 if (jp[i].next != NULL) in makejob()
767 jp[i].next = &jp[jp[i].next - in makejob()
771 bgjob = &jp[bgjob - jobtab]; in makejob()
780 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0) in makejob()
785 if (jp->used == 0) in makejob()
789 jp->state = 0; in makejob()
790 jp->used = 1; in makejob()
791 jp->changed = 0; in makejob()
792 jp->nprocs = 0; in makejob()
793 jp->foreground = 0; in makejob()
794 jp->remembered = 0; in makejob()
795 jp->pipefail = pipefailflag; in makejob()
797 jp->jobctl = jobctl; in makejob()
798 jp->next = NULL; in makejob()
801 jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); in makejob()
803 jp->ps = &jp->ps0; in makejob()
807 jp - jobtab + 1)); in makejob()
817 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { in setcurjob()
820 prev->next = jp->next; in setcurjob()
822 jobmru = jp->next; in setcurjob()
823 jp->next = jobmru; in setcurjob()
828 cj->next = jobmru; in setcurjob()
837 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { in deljob()
840 prev->next = jp->next; in deljob()
842 jobmru = jp->next; in deljob()
858 for (jp = jobmru; jp != NULL; jp = jp->next) in getcurjob()
859 if (jp->used && jp != nj && jp->state == JOBSTOPPED) in getcurjob()
862 for (jp = jobmru; jp != NULL; jp = jp->next) in getcurjob()
863 if (jp->used && jp != nj) in getcurjob()
875 * be NULL. The mode parameter can be one of the following:
876 * FORK_FG - Fork off a foreground process.
877 * FORK_BG - Fork off a background process.
878 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
887 forkshell(struct job *jp, union node *n, int mode) in forkshell() argument
892 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n, in forkshell()
893 mode)); in forkshell()
895 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0)) in forkshell()
899 if (pid == -1) { in forkshell()
919 if (wasroot && mode != FORK_NOJOB && mflag) { in forkshell()
920 if (jp == NULL || jp->nprocs == 0) in forkshell()
923 pgrp = jp->ps[0].pid; in forkshell()
924 if (setpgid(0, pgrp) == 0 && mode == FORK_FG && in forkshell()
937 } else if (mode == FORK_BG) { in forkshell()
940 if ((jp == NULL || jp->nprocs == 0) && in forkshell()
949 if (mode == FORK_BG) { in forkshell()
952 if ((jp == NULL || jp->nprocs == 0) && in forkshell()
962 for (i = njobs, p = jobtab ; --i >= 0 ; p++) in forkshell()
963 if (p->used) in forkshell()
973 if (rootshell && mode != FORK_NOJOB && mflag) { in forkshell()
974 if (jp == NULL || jp->nprocs == 0) in forkshell()
977 pgrp = jp->ps[0].pid; in forkshell()
980 if (mode == FORK_BG) { in forkshell()
981 if (bgjob != NULL && bgjob->state == JOBDONE && in forkshell()
982 !bgjob->remembered && !iflag) in forkshell()
988 struct procstat *ps = &jp->ps[jp->nprocs++]; in forkshell()
989 ps->pid = pid; in forkshell()
990 ps->status = -1; in forkshell()
991 ps->cmd = nullstr; in forkshell()
993 ps->cmd = commandtext(n); in forkshell()
994 jp->foreground = mode == FORK_FG; in forkshell()
1013 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0], in vforkexecshell()
1020 if (pid == -1) { in vforkexecshell()
1041 struct procstat *ps = &jp->ps[jp->nprocs++]; in vforkexecshell()
1042 ps->pid = pid; in vforkexecshell()
1043 ps->status = -1; in vforkexecshell()
1044 ps->cmd = nullstr; in vforkexecshell()
1045 jp->foreground = 1; in vforkexecshell()
1061 * to the shell. This means that an infinite loop started by an inter-
1079 int propagate_int = jp->jobctl && jp->foreground; in waitforjob()
1086 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1)); in waitforjob()
1087 while (jp->state == 0) in waitforjob()
1089 DOWAIT_SIG_TRAP : 0), jp) == -1) { in waitforjob()
1090 jobindex = jp - jobtab; in waitforjob()
1095 if (jp->jobctl) { in waitforjob()
1099 if (jp->state == JOBSTOPPED) in waitforjob()
1114 if (! JOBS || jp->state == JOBDONE) in waitforjob()
1140 dowait(int mode, struct job *job) in dowait() argument
1157 TRACE(("dowait(%d, %p) called\n", mode, job)); in dowait()
1159 if ((mode & DOWAIT_SIG) != 0) { in dowait()
1178 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK) in dowait()
1182 if (pid == 0 && (mode & DOWAIT_SIG) != 0) { in dowait()
1183 pid = -1; in dowait()
1184 if (((mode & DOWAIT_SIG_TRAP) != 0 ? in dowait()
1193 } while (pid == -1 && errno == EINTR); in dowait()
1194 if (pid == -1 && errno == ECHILD && job != NULL) in dowait()
1195 job->state = JOBDONE; in dowait()
1196 if ((mode & DOWAIT_SIG) != 0) { in dowait()
1207 if (jp->used && jp->nprocs > 0) { in dowait()
1210 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { in dowait()
1211 if (sp->pid == -1) in dowait()
1213 if (sp->pid == pid && (sp->status == -1 || in dowait()
1214 WIFSTOPPED(sp->status))) { in dowait()
1216 (int)pid, sp->status, in dowait()
1219 sp->status = -1; in dowait()
1220 jp->state = 0; in dowait()
1222 sp->status = status; in dowait()
1225 if (sp->status == -1) in dowait()
1227 else if (WIFSTOPPED(sp->status)) in dowait()
1232 if (jp->state != state) { in dowait()
1233 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); in dowait()
1234 jp->state = state; in dowait()
1236 if (done && !jp->remembered && in dowait()
1249 if (!thisjob || thisjob->state == 0) in dowait()
1252 thisjob->foreground && thisjob->state != JOBSTOPPED) { in dowait()
1255 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++) in dowait()
1256 if (WIFSIGNALED(sp->status)) { in dowait()
1257 sig = WTERMSIG(sp->status); in dowait()
1258 coredump = WCOREDUMP(sp->status); in dowait()
1273 thisjob->changed = 1; in dowait()
1293 if (jp->used == 0) in stoppedjobs()
1295 if (jp->state == JOBSTOPPED) { in stoppedjobs()
1317 return backgndpid != -1; in backgndpidset()
1325 bgjob->remembered = 1; in backgndpidval()
1344 cmdnleft = MAXCMDTEXT - 4; in commandtext()
1365 if (n->nfile.fd != deffd) { in cmdtxtredir()
1366 s[0] = n->nfile.fd + '0'; in cmdtxtredir()
1371 if (n->type == NTOFD || n->type == NFROMFD) { in cmdtxtredir()
1372 if (n->ndup.dupfd >= 0) in cmdtxtredir()
1373 s[0] = n->ndup.dupfd + '0'; in cmdtxtredir()
1375 s[0] = '-'; in cmdtxtredir()
1379 cmdtxt(n->nfile.fname); in cmdtxtredir()
1392 switch (n->type) { in cmdtxt()
1394 cmdtxt(n->nbinary.ch1); in cmdtxt()
1396 cmdtxt(n->nbinary.ch2); in cmdtxt()
1399 cmdtxt(n->nbinary.ch1); in cmdtxt()
1401 cmdtxt(n->nbinary.ch2); in cmdtxt()
1404 cmdtxt(n->nbinary.ch1); in cmdtxt()
1406 cmdtxt(n->nbinary.ch2); in cmdtxt()
1409 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { in cmdtxt()
1410 cmdtxt(lp->n); in cmdtxt()
1411 if (lp->next) in cmdtxt()
1417 cmdtxt(n->nredir.n); in cmdtxt()
1422 cmdtxt(n->nredir.n); in cmdtxt()
1426 cmdtxt(n->nif.test); in cmdtxt()
1428 cmdtxt(n->nif.ifpart); in cmdtxt()
1433 cmdtxt(n->nbinary.ch1); in cmdtxt()
1434 cmdtxtdogroup(n->nbinary.ch2); in cmdtxt()
1438 cmdtxt(n->nbinary.ch1); in cmdtxt()
1439 cmdtxtdogroup(n->nbinary.ch2); in cmdtxt()
1443 cmdputs(n->nfor.var); in cmdtxt()
1448 cmdputs(n->ncase.expr->narg.text); in cmdtxt()
1452 cmdputs(n->narg.text); in cmdtxt()
1457 cmdtxt(n->nnot.com); in cmdtxt()
1460 for (np = n->ncmd.args ; np ; np = np->narg.next) { in cmdtxt()
1462 if (np->narg.next) in cmdtxt()
1465 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { in cmdtxt()
1471 cmdputs(n->narg.text); in cmdtxt()
1523 if (--cmdnleft > 0) in cmdputs()
1526 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0) in cmdputs()
1529 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL]; in cmdputs()
1536 --cmdnleft > 0) in cmdputs()
1537 *q = q[-1], q++; in cmdputs()
1542 cmdnleft -= 5; in cmdputs()
1552 cmdnleft -= 2; in cmdputs()
1560 if (--cmdnleft > 0) { in cmdputs()
1568 if (--cmdnleft <= 0) { in cmdputs()