jobs.c revision 99760
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836150Scharnier#if 0 3936150Scharnierstatic char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95"; 4036150Scharnier#endif 411556Srgrimes#endif /* not lint */ 4299110Sobrien#include <sys/cdefs.h> 4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/jobs.c 99760 2002-07-11 04:22:41Z tjr $"); 441556Srgrimes 4517987Speter#include <fcntl.h> 4617987Speter#include <signal.h> 4717987Speter#include <errno.h> 4817987Speter#include <unistd.h> 4917987Speter#include <stdlib.h> 5017987Speter#include <sys/param.h> 5117987Speter#ifdef BSD 5217987Speter#include <sys/wait.h> 5317987Speter#include <sys/time.h> 5417987Speter#include <sys/resource.h> 5569793Sobrien#include <paths.h> 5617987Speter#endif 5718018Speter#include <sys/ioctl.h> 5817987Speter 591556Srgrimes#include "shell.h" 601556Srgrimes#if JOBS 6125222Ssteve#if OLD_TTY_DRIVER 621556Srgrimes#include "sgtty.h" 6317987Speter#else 6417987Speter#include <termios.h> 6517987Speter#endif 661556Srgrimes#undef CEOF /* syntax.h redefines this */ 671556Srgrimes#endif 6817987Speter#include "redir.h" 6917987Speter#include "show.h" 701556Srgrimes#include "main.h" 711556Srgrimes#include "parser.h" 721556Srgrimes#include "nodes.h" 731556Srgrimes#include "jobs.h" 741556Srgrimes#include "options.h" 751556Srgrimes#include "trap.h" 761556Srgrimes#include "syntax.h" 771556Srgrimes#include "input.h" 781556Srgrimes#include "output.h" 791556Srgrimes#include "memalloc.h" 801556Srgrimes#include "error.h" 811556Srgrimes#include "mystring.h" 821556Srgrimes 831556Srgrimes 841556Srgrimesstruct job *jobtab; /* array of jobs */ 851556Srgrimesint njobs; /* size of array */ 8628346SsteveMKINIT pid_t backgndpid = -1; /* pid of last background process */ 871556Srgrimes#if JOBS 8897659Stjrstruct job *jobmru; /* most recently used job list */ 891556Srgrimesint initialpgrp; /* pgrp of shell on invocation */ 901556Srgrimes#endif 9138536Scracauerint in_waitcmd = 0; /* are we in waitcmd()? */ 9238950Scracauerint in_dowait = 0; /* are we in dowait()? */ 9338536Scracauervolatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */ 941556Srgrimes 9520425Ssteve#if JOBS 9690111SimpSTATIC void restartjob(struct job *); 9720425Ssteve#endif 9890111SimpSTATIC void freejob(struct job *); 9990111SimpSTATIC struct job *getjob(char *); 10090111SimpSTATIC int dowait(int, struct job *); 10120425Ssteve#if SYSV 10290111SimpSTATIC int onsigchild(void); 10320425Ssteve#endif 10490111SimpSTATIC int waitproc(int, int *); 10590111SimpSTATIC void cmdtxt(union node *); 10690111SimpSTATIC void cmdputs(char *); 10797659Stjr#if JOBS 10897659StjrSTATIC void setcurjob(struct job *); 10997659StjrSTATIC void deljob(struct job *); 11097659StjrSTATIC struct job *getcurjob(struct job *); 11197659Stjr#endif 11297822StjrSTATIC void showjob(struct job *, pid_t, int, int); 1131556Srgrimes 1141556Srgrimes 1151556Srgrimes/* 1161556Srgrimes * Turn job control on and off. 1171556Srgrimes * 1181556Srgrimes * Note: This code assumes that the third arg to ioctl is a character 1191556Srgrimes * pointer, which is true on Berkeley systems but not System V. Since 1201556Srgrimes * System V doesn't have job control yet, this isn't a problem now. 1211556Srgrimes */ 1221556Srgrimes 1231556SrgrimesMKINIT int jobctl; 1241556Srgrimes 12520425Ssteve#if JOBS 1261556Srgrimesvoid 12790111Simpsetjobctl(int on) 12817987Speter{ 1291556Srgrimes#ifdef OLD_TTY_DRIVER 1301556Srgrimes int ldisc; 1311556Srgrimes#endif 1321556Srgrimes 1331556Srgrimes if (on == jobctl || rootshell == 0) 1341556Srgrimes return; 1351556Srgrimes if (on) { 1361556Srgrimes do { /* while we are in the background */ 13720425Ssteve#ifdef OLD_TTY_DRIVER 1381556Srgrimes if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) { 13920425Ssteve#else 14020425Ssteve initialpgrp = tcgetpgrp(2); 14120425Ssteve if (initialpgrp < 0) { 14220425Ssteve#endif 1431556Srgrimes out2str("sh: can't access tty; job control turned off\n"); 1441556Srgrimes mflag = 0; 1451556Srgrimes return; 1461556Srgrimes } 1471556Srgrimes if (initialpgrp == -1) 14817987Speter initialpgrp = getpgrp(); 14917987Speter else if (initialpgrp != getpgrp()) { 1501556Srgrimes killpg(initialpgrp, SIGTTIN); 1511556Srgrimes continue; 1521556Srgrimes } 1531556Srgrimes } while (0); 1541556Srgrimes#ifdef OLD_TTY_DRIVER 1551556Srgrimes if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) { 1561556Srgrimes out2str("sh: need new tty driver to run job control; job control turned off\n"); 1571556Srgrimes mflag = 0; 1581556Srgrimes return; 1591556Srgrimes } 1601556Srgrimes#endif 1611556Srgrimes setsignal(SIGTSTP); 1621556Srgrimes setsignal(SIGTTOU); 1631556Srgrimes setsignal(SIGTTIN); 16417987Speter setpgid(0, rootpid); 16520425Ssteve#ifdef OLD_TTY_DRIVER 1661556Srgrimes ioctl(2, TIOCSPGRP, (char *)&rootpid); 16720425Ssteve#else 16820425Ssteve tcsetpgrp(2, rootpid); 16920425Ssteve#endif 1701556Srgrimes } else { /* turning job control off */ 17117987Speter setpgid(0, initialpgrp); 17220425Ssteve#ifdef OLD_TTY_DRIVER 1731556Srgrimes ioctl(2, TIOCSPGRP, (char *)&initialpgrp); 17420425Ssteve#else 17520425Ssteve tcsetpgrp(2, initialpgrp); 17620425Ssteve#endif 1771556Srgrimes setsignal(SIGTSTP); 1781556Srgrimes setsignal(SIGTTOU); 1791556Srgrimes setsignal(SIGTTIN); 1801556Srgrimes } 1811556Srgrimes jobctl = on; 1821556Srgrimes} 18320425Ssteve#endif 1841556Srgrimes 1851556Srgrimes 1861556Srgrimes#ifdef mkinit 18728346SsteveINCLUDE <sys/types.h> 18817987SpeterINCLUDE <stdlib.h> 1891556Srgrimes 1901556SrgrimesSHELLPROC { 1911556Srgrimes backgndpid = -1; 1921556Srgrimes#if JOBS 1931556Srgrimes jobctl = 0; 1941556Srgrimes#endif 1951556Srgrimes} 1961556Srgrimes 1971556Srgrimes#endif 1981556Srgrimes 1991556Srgrimes 2001556Srgrimes 2011556Srgrimes#if JOBS 20217987Speterint 20390111Simpfgcmd(int argc __unused, char **argv) 20417987Speter{ 2051556Srgrimes struct job *jp; 2061556Srgrimes int pgrp; 2071556Srgrimes int status; 2081556Srgrimes 2091556Srgrimes jp = getjob(argv[1]); 2101556Srgrimes if (jp->jobctl == 0) 2111556Srgrimes error("job not created under job control"); 21296933Stjr out1str(jp->ps[0].cmd); 21396933Stjr out1c('\n'); 21496933Stjr flushout(&output); 2151556Srgrimes pgrp = jp->ps[0].pid; 21620425Ssteve#ifdef OLD_TTY_DRIVER 2171556Srgrimes ioctl(2, TIOCSPGRP, (char *)&pgrp); 21820425Ssteve#else 21920425Ssteve tcsetpgrp(2, pgrp); 22020425Ssteve#endif 2211556Srgrimes restartjob(jp); 2221556Srgrimes INTOFF; 22345916Scracauer status = waitforjob(jp, (int *)NULL); 2241556Srgrimes INTON; 2251556Srgrimes return status; 2261556Srgrimes} 2271556Srgrimes 2281556Srgrimes 22917987Speterint 23090111Simpbgcmd(int argc, char **argv) 23117987Speter{ 23296933Stjr char s[64]; 2331556Srgrimes struct job *jp; 2341556Srgrimes 2351556Srgrimes do { 2361556Srgrimes jp = getjob(*++argv); 2371556Srgrimes if (jp->jobctl == 0) 2381556Srgrimes error("job not created under job control"); 23996933Stjr if (jp->state == JOBDONE) 24096933Stjr continue; 2411556Srgrimes restartjob(jp); 24296933Stjr fmtstr(s, 64, "[%d] ", jp - jobtab + 1); 24396933Stjr out1str(s); 24496933Stjr out1str(jp->ps[0].cmd); 24596933Stjr out1c('\n'); 2461556Srgrimes } while (--argc > 1); 2471556Srgrimes return 0; 2481556Srgrimes} 2491556Srgrimes 2501556Srgrimes 2511556SrgrimesSTATIC void 25290111Simprestartjob(struct job *jp) 25317987Speter{ 2541556Srgrimes struct procstat *ps; 2551556Srgrimes int i; 2561556Srgrimes 2571556Srgrimes if (jp->state == JOBDONE) 2581556Srgrimes return; 25997660Stjr setcurjob(jp); 2601556Srgrimes INTOFF; 2611556Srgrimes killpg(jp->ps[0].pid, SIGCONT); 2621556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 26326104Ssteve if (WIFSTOPPED(ps->status)) { 2641556Srgrimes ps->status = -1; 2651556Srgrimes jp->state = 0; 2661556Srgrimes } 2671556Srgrimes } 2681556Srgrimes INTON; 2691556Srgrimes} 2701556Srgrimes#endif 2711556Srgrimes 2721556Srgrimes 2731556Srgrimesint 27497669Stjrjobscmd(int argc, char *argv[]) 27517987Speter{ 27697669Stjr char *id; 27797669Stjr int ch, sformat, lformat; 27897669Stjr 27997669Stjr optind = optreset = 1; 28097669Stjr sformat = lformat = 0; 28197669Stjr while ((ch = getopt(argc, argv, "ls")) != -1) { 28297669Stjr switch (ch) { 28397669Stjr case 'l': 28497669Stjr lformat = 1; 28597669Stjr break; 28697669Stjr case 's': 28797669Stjr sformat = 1; 28897669Stjr break; 28997669Stjr case '?': 29097669Stjr default: 29197669Stjr error("unknown option: -%c", optopt); 29297669Stjr } 29397669Stjr } 29497669Stjr argc -= optind; 29597669Stjr argv += optind; 29697669Stjr 29797669Stjr if (argc == 0) 29897669Stjr showjobs(0, sformat, lformat); 29997669Stjr else 30097669Stjr while ((id = *argv++) != NULL) 30197822Stjr showjob(getjob(id), 0, sformat, lformat); 30297669Stjr 30397669Stjr return (0); 3041556Srgrimes} 3051556Srgrimes 30697663StjrSTATIC void 30797822Stjrshowjob(struct job *jp, pid_t pid, int sformat, int lformat) 30897663Stjr{ 30997663Stjr char s[64]; 31097663Stjr struct procstat *ps; 31197669Stjr struct job *j; 31297669Stjr int col, curr, i, jobno, prev, procno; 31397669Stjr char c; 3141556Srgrimes 31597663Stjr procno = jp->nprocs; 31697663Stjr jobno = jp - jobtab + 1; 31797669Stjr curr = prev = 0; 31897669Stjr#if JOBS 31997669Stjr if ((j = getcurjob(NULL)) != NULL) { 32097669Stjr curr = j - jobtab + 1; 32197669Stjr if ((j = getcurjob(j)) != NULL) 32297669Stjr prev = j - jobtab + 1; 32397669Stjr } 32497669Stjr#endif 32597663Stjr for (ps = jp->ps ; ; ps++) { /* for each process */ 32697669Stjr if (sformat) { 32797669Stjr out1fmt("%d\n", ps->pid); 32897669Stjr goto skip; 32997669Stjr } 33097822Stjr if (!lformat && ps != jp->ps && pid == 0) 33197669Stjr goto skip; 33297822Stjr if (pid != 0 && pid != ps->pid) 33397822Stjr goto skip; 33497819Stjr if (jobno == curr && ps == jp->ps) 33597669Stjr c = '+'; 33697819Stjr else if (jobno == prev && ps == jp->ps) 33797669Stjr c = '-'; 33897669Stjr else 33997669Stjr c = ' '; 34097663Stjr if (ps == jp->ps) 34197669Stjr fmtstr(s, 64, "[%d] %c ", jobno, c); 34297663Stjr else 34397816Stjr fmtstr(s, 64, " %c ", c); 34497663Stjr out1str(s); 34597663Stjr col = strlen(s); 34697669Stjr if (lformat) { 34797669Stjr fmtstr(s, 64, "%d ", ps->pid); 34897669Stjr out1str(s); 34997669Stjr col += strlen(s); 35097669Stjr } 35197663Stjr s[0] = '\0'; 35297819Stjr if (ps != jp->ps) { 35397819Stjr *s = '\0'; 35497819Stjr } else if (ps->status == -1) { 35597669Stjr strcpy(s, "Running"); 35697663Stjr } else if (WIFEXITED(ps->status)) { 35797820Stjr if (WEXITSTATUS(ps->status) == 0) 35897820Stjr strcpy(s, "Done"); 35997820Stjr else 36097820Stjr fmtstr(s, 64, "Done (%d)", 36197820Stjr WEXITSTATUS(ps->status)); 36297663Stjr } else { 36397663Stjr#if JOBS 36497663Stjr if (WIFSTOPPED(ps->status)) 36597663Stjr i = WSTOPSIG(ps->status); 36697663Stjr else 36797663Stjr#endif 36897663Stjr i = WTERMSIG(ps->status); 36997663Stjr if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F]) 37097663Stjr scopy(sys_siglist[i & 0x7F], s); 37197663Stjr else 37297663Stjr fmtstr(s, 64, "Signal %d", i & 0x7F); 37397663Stjr if (WCOREDUMP(ps->status)) 37497663Stjr strcat(s, " (core dumped)"); 37597663Stjr } 37697663Stjr out1str(s); 37797663Stjr col += strlen(s); 37897663Stjr do { 37997663Stjr out1c(' '); 38097663Stjr col++; 38197663Stjr } while (col < 30); 38297663Stjr out1str(ps->cmd); 38397663Stjr out1c('\n'); 38497669Stjrskip: if (--procno <= 0) 38597663Stjr break; 38697663Stjr } 38797663Stjr} 38897663Stjr 3891556Srgrimes/* 3901556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 3911556Srgrimes * statuses have changed since the last call to showjobs. 3921556Srgrimes * 3931556Srgrimes * If the shell is interrupted in the process of creating a job, the 3941556Srgrimes * result may be a job structure containing zero processes. Such structures 3951556Srgrimes * will be freed here. 3961556Srgrimes */ 3971556Srgrimes 3981556Srgrimesvoid 39997669Stjrshowjobs(int change, int sformat, int lformat) 40017987Speter{ 4011556Srgrimes int jobno; 4021556Srgrimes struct job *jp; 4031556Srgrimes 4041556Srgrimes TRACE(("showjobs(%d) called\n", change)); 4051556Srgrimes while (dowait(0, (struct job *)NULL) > 0); 4061556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 4071556Srgrimes if (! jp->used) 4081556Srgrimes continue; 4091556Srgrimes if (jp->nprocs == 0) { 4101556Srgrimes freejob(jp); 4111556Srgrimes continue; 4121556Srgrimes } 4131556Srgrimes if (change && ! jp->changed) 4141556Srgrimes continue; 41597822Stjr showjob(jp, 0, sformat, lformat); 4161556Srgrimes jp->changed = 0; 4171556Srgrimes if (jp->state == JOBDONE) { 4181556Srgrimes freejob(jp); 4191556Srgrimes } 4201556Srgrimes } 4211556Srgrimes} 4221556Srgrimes 4231556Srgrimes 4241556Srgrimes/* 4251556Srgrimes * Mark a job structure as unused. 4261556Srgrimes */ 4271556Srgrimes 4281556SrgrimesSTATIC void 42990111Simpfreejob(struct job *jp) 43090111Simp{ 4311556Srgrimes struct procstat *ps; 4321556Srgrimes int i; 4331556Srgrimes 4341556Srgrimes INTOFF; 4351556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 4361556Srgrimes if (ps->cmd != nullstr) 4371556Srgrimes ckfree(ps->cmd); 4381556Srgrimes } 4391556Srgrimes if (jp->ps != &jp->ps0) 4401556Srgrimes ckfree(jp->ps); 4411556Srgrimes jp->used = 0; 4421556Srgrimes#if JOBS 44397659Stjr deljob(jp); 4441556Srgrimes#endif 4451556Srgrimes INTON; 4461556Srgrimes} 4471556Srgrimes 4481556Srgrimes 4491556Srgrimes 4501556Srgrimesint 45190111Simpwaitcmd(int argc, char **argv) 45217987Speter{ 4531556Srgrimes struct job *job; 45426104Ssteve int status, retval; 4551556Srgrimes struct job *jp; 4561556Srgrimes 4571556Srgrimes if (argc > 1) { 4581556Srgrimes job = getjob(argv[1]); 4591556Srgrimes } else { 4601556Srgrimes job = NULL; 4611556Srgrimes } 46238536Scracauer 46338536Scracauer /* 46438536Scracauer * Loop until a process is terminated or stopped, or a SIGINT is 46538536Scracauer * received. 46638536Scracauer */ 46738536Scracauer 46838521Scracauer in_waitcmd++; 46938536Scracauer do { 4701556Srgrimes if (job != NULL) { 4711556Srgrimes if (job->state) { 4721556Srgrimes status = job->ps[job->nprocs - 1].status; 47326104Ssteve if (WIFEXITED(status)) 47426104Ssteve retval = WEXITSTATUS(status); 4751556Srgrimes#if JOBS 47626104Ssteve else if (WIFSTOPPED(status)) 47726104Ssteve retval = WSTOPSIG(status) + 128; 4781556Srgrimes#endif 4791556Srgrimes else 48026104Ssteve retval = WTERMSIG(status) + 128; 4811556Srgrimes if (! iflag) 4821556Srgrimes freejob(job); 48338536Scracauer in_waitcmd--; 48426104Ssteve return retval; 4851556Srgrimes } 4861556Srgrimes } else { 4871556Srgrimes for (jp = jobtab ; ; jp++) { 4881556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 48938536Scracauer in_waitcmd--; 4901556Srgrimes return 0; 4911556Srgrimes } 4921556Srgrimes if (jp->used && jp->state == 0) 4931556Srgrimes break; 4941556Srgrimes } 4951556Srgrimes } 49638521Scracauer } while (dowait(1, (struct job *)NULL) != -1); 49738521Scracauer in_waitcmd--; 49838521Scracauer 49938521Scracauer return 0; 5001556Srgrimes} 5011556Srgrimes 5021556Srgrimes 5031556Srgrimes 50417987Speterint 50590111Simpjobidcmd(int argc __unused, char **argv) 50617987Speter{ 5071556Srgrimes struct job *jp; 5081556Srgrimes int i; 5091556Srgrimes 5101556Srgrimes jp = getjob(argv[1]); 5111556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 5121556Srgrimes out1fmt("%d", jp->ps[i].pid); 5131556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 5141556Srgrimes } 5151556Srgrimes return 0; 5161556Srgrimes} 5171556Srgrimes 5181556Srgrimes 5191556Srgrimes 5201556Srgrimes/* 5211556Srgrimes * Convert a job name to a job structure. 5221556Srgrimes */ 5231556Srgrimes 5241556SrgrimesSTATIC struct job * 52590111Simpgetjob(char *name) 52690111Simp{ 5271556Srgrimes int jobno; 52897688Stjr struct job *found, *jp; 5291556Srgrimes int pid; 5301556Srgrimes int i; 5311556Srgrimes 5321556Srgrimes if (name == NULL) { 5331556Srgrimes#if JOBS 53497659Stjrcurrentjob: if ((jp = getcurjob(NULL)) == NULL) 5351556Srgrimes error("No current job"); 53697659Stjr return (jp); 5371556Srgrimes#else 5381556Srgrimes error("No current job"); 5391556Srgrimes#endif 5401556Srgrimes } else if (name[0] == '%') { 5411556Srgrimes if (is_digit(name[1])) { 5421556Srgrimes jobno = number(name + 1); 5431556Srgrimes if (jobno > 0 && jobno <= njobs 5441556Srgrimes && jobtab[jobno - 1].used != 0) 5451556Srgrimes return &jobtab[jobno - 1]; 5461556Srgrimes#if JOBS 5471556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 5481556Srgrimes goto currentjob; 54997688Stjr } else if (name[1] == '+' && name[2] == '\0') { 55097688Stjr goto currentjob; 55197688Stjr } else if (name[1] == '-' && name[2] == '\0') { 55297688Stjr if ((jp = getcurjob(NULL)) == NULL || 55397688Stjr (jp = getcurjob(jp)) == NULL) 55497688Stjr error("No previous job"); 55597688Stjr return (jp); 5561556Srgrimes#endif 55797688Stjr } else if (name[1] == '?') { 55897688Stjr found = NULL; 55997688Stjr for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 56097688Stjr if (jp->used && jp->nprocs > 0 56197688Stjr && strstr(jp->ps[0].cmd, name + 2) != NULL) { 56297688Stjr if (found) 56397688Stjr error("%s: ambiguous", name); 56497688Stjr found = jp; 56597688Stjr } 56697688Stjr } 56797688Stjr if (found != NULL) 56897688Stjr return (found); 5691556Srgrimes } else { 57097688Stjr found = NULL; 5711556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5721556Srgrimes if (jp->used && jp->nprocs > 0 5731556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 5741556Srgrimes if (found) 5751556Srgrimes error("%s: ambiguous", name); 5761556Srgrimes found = jp; 5771556Srgrimes } 5781556Srgrimes } 5791556Srgrimes if (found) 5801556Srgrimes return found; 5811556Srgrimes } 5821556Srgrimes } else if (is_number(name)) { 5831556Srgrimes pid = number(name); 5841556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5851556Srgrimes if (jp->used && jp->nprocs > 0 5861556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 5871556Srgrimes return jp; 5881556Srgrimes } 5891556Srgrimes } 5901556Srgrimes error("No such job: %s", name); 59117987Speter /*NOTREACHED*/ 59217987Speter return NULL; 5931556Srgrimes} 5941556Srgrimes 5951556Srgrimes 5961556Srgrimes 5971556Srgrimes/* 5981556Srgrimes * Return a new job structure, 5991556Srgrimes */ 6001556Srgrimes 6011556Srgrimesstruct job * 60290111Simpmakejob(union node *node __unused, int nprocs) 60317987Speter{ 6041556Srgrimes int i; 6051556Srgrimes struct job *jp; 6061556Srgrimes 6071556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 6081556Srgrimes if (--i < 0) { 6091556Srgrimes INTOFF; 6101556Srgrimes if (njobs == 0) { 6111556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 61297664Stjr#if JOBS 61397659Stjr jobmru = NULL; 61497664Stjr#endif 6151556Srgrimes } else { 6161556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 61717987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 61897659Stjr#if JOBS 61997659Stjr /* Relocate `next' pointers and list head */ 62099760Stjr if (jobmru != NULL) 62199760Stjr jobmru = &jp[jobmru - jobtab]; 62297659Stjr for (i = 0; i < njobs; i++) 62397659Stjr if (jp[i].next != NULL) 62497659Stjr jp[i].next = &jp[jp[i].next - 62597659Stjr jobtab]; 62697659Stjr#endif 62720425Ssteve /* Relocate `ps' pointers */ 62820425Ssteve for (i = 0; i < njobs; i++) 62920425Ssteve if (jp[i].ps == &jobtab[i].ps0) 63020425Ssteve jp[i].ps = &jp[i].ps0; 6311556Srgrimes ckfree(jobtab); 6321556Srgrimes jobtab = jp; 6331556Srgrimes } 6341556Srgrimes jp = jobtab + njobs; 6351556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 6361556Srgrimes INTON; 6371556Srgrimes break; 6381556Srgrimes } 6391556Srgrimes if (jp->used == 0) 6401556Srgrimes break; 6411556Srgrimes } 6421556Srgrimes INTOFF; 6431556Srgrimes jp->state = 0; 6441556Srgrimes jp->used = 1; 6451556Srgrimes jp->changed = 0; 6461556Srgrimes jp->nprocs = 0; 6471556Srgrimes#if JOBS 6481556Srgrimes jp->jobctl = jobctl; 64997659Stjr jp->next = NULL; 6501556Srgrimes#endif 6511556Srgrimes if (nprocs > 1) { 6521556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 6531556Srgrimes } else { 6541556Srgrimes jp->ps = &jp->ps0; 6551556Srgrimes } 6561556Srgrimes INTON; 65717987Speter TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, 65817987Speter jp - jobtab + 1)); 6591556Srgrimes return jp; 6608855Srgrimes} 6611556Srgrimes 66297659Stjr#if JOBS 66397659StjrSTATIC void 66497659Stjrsetcurjob(struct job *cj) 66597659Stjr{ 66697659Stjr struct job *jp, *prev; 6671556Srgrimes 66897659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 66997659Stjr if (jp == cj) { 67097659Stjr if (prev != NULL) 67197659Stjr prev->next = jp->next; 67297659Stjr else 67397659Stjr jobmru = jp->next; 67497659Stjr jp->next = jobmru; 67597659Stjr jobmru = cj; 67697659Stjr return; 67797659Stjr } 67897659Stjr } 67997659Stjr cj->next = jobmru; 68097659Stjr jobmru = cj; 68197659Stjr} 68297659Stjr 68397659StjrSTATIC void 68497659Stjrdeljob(struct job *j) 68597659Stjr{ 68697659Stjr struct job *jp, *prev; 68797659Stjr 68897659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 68997659Stjr if (jp == j) { 69097659Stjr if (prev != NULL) 69197659Stjr prev->next = jp->next; 69297659Stjr else 69397659Stjr jobmru = jp->next; 69497659Stjr return; 69597659Stjr } 69697659Stjr } 69797659Stjr} 69897659Stjr 6991556Srgrimes/* 70097659Stjr * Return the most recently used job that isn't `nj', and preferably one 70197659Stjr * that is stopped. 70297659Stjr */ 70397659StjrSTATIC struct job * 70497659Stjrgetcurjob(struct job *nj) 70597659Stjr{ 70697659Stjr struct job *jp; 70797659Stjr 70897659Stjr /* Try to find a stopped one.. */ 70997659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 71097659Stjr if (jp->used && jp != nj && jp->state == JOBSTOPPED) 71197659Stjr return (jp); 71297659Stjr /* Otherwise the most recently used job that isn't `nj' */ 71397659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 71497659Stjr if (jp->used && jp != nj) 71597659Stjr return (jp); 71697659Stjr 71797659Stjr return (NULL); 71897659Stjr} 71997659Stjr 72097659Stjr#endif 72197659Stjr 72297659Stjr/* 7231556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 7241556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 7251556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 7261556Srgrimes * be NULL. The mode parameter can be one of the following: 7271556Srgrimes * FORK_FG - Fork off a foreground process. 7281556Srgrimes * FORK_BG - Fork off a background process. 7291556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 7301556Srgrimes * process group even if job control is on. 7311556Srgrimes * 7321556Srgrimes * When job control is turned off, background processes have their standard 7331556Srgrimes * input redirected to /dev/null (except for the second and later processes 7341556Srgrimes * in a pipeline). 7351556Srgrimes */ 7361556Srgrimes 7371556Srgrimesint 73890111Simpforkshell(struct job *jp, union node *n, int mode) 73917987Speter{ 7401556Srgrimes int pid; 7411556Srgrimes int pgrp; 7421556Srgrimes 74317987Speter TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n, 74417987Speter mode)); 7451556Srgrimes INTOFF; 7461556Srgrimes pid = fork(); 7471556Srgrimes if (pid == -1) { 7481556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 7491556Srgrimes INTON; 75053891Scracauer error("Cannot fork: %s", strerror(errno)); 7511556Srgrimes } 7521556Srgrimes if (pid == 0) { 7531556Srgrimes struct job *p; 7541556Srgrimes int wasroot; 7551556Srgrimes int i; 7561556Srgrimes 7571556Srgrimes TRACE(("Child shell %d\n", getpid())); 7581556Srgrimes wasroot = rootshell; 7591556Srgrimes rootshell = 0; 7601556Srgrimes for (i = njobs, p = jobtab ; --i >= 0 ; p++) 7611556Srgrimes if (p->used) 7621556Srgrimes freejob(p); 7631556Srgrimes closescript(); 7641556Srgrimes INTON; 7651556Srgrimes clear_traps(); 7661556Srgrimes#if JOBS 7671556Srgrimes jobctl = 0; /* do job control only in root shell */ 7681556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 7691556Srgrimes if (jp == NULL || jp->nprocs == 0) 7701556Srgrimes pgrp = getpid(); 7711556Srgrimes else 7721556Srgrimes pgrp = jp->ps[0].pid; 77321352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 7741556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 77520425Ssteve#ifdef OLD_TTY_DRIVER 7761556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0) 77718016Speter error("TIOCSPGRP failed, errno=%d", errno); 77820425Ssteve#else 77920425Ssteve if (tcsetpgrp(2, pgrp) < 0) 78020425Ssteve error("tcsetpgrp failed, errno=%d", errno); 78120425Ssteve#endif 7821556Srgrimes } 7831556Srgrimes setsignal(SIGTSTP); 7841556Srgrimes setsignal(SIGTTOU); 7851556Srgrimes } else if (mode == FORK_BG) { 7861556Srgrimes ignoresig(SIGINT); 7871556Srgrimes ignoresig(SIGQUIT); 7881556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7891556Srgrimes ! fd0_redirected_p ()) { 7901556Srgrimes close(0); 79169793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 79269793Sobrien error("Can't open %s: %s", 79369793Sobrien _PATH_DEVNULL, strerror(errno)); 7941556Srgrimes } 7951556Srgrimes } 7961556Srgrimes#else 7971556Srgrimes if (mode == FORK_BG) { 7981556Srgrimes ignoresig(SIGINT); 7991556Srgrimes ignoresig(SIGQUIT); 8001556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 8011556Srgrimes ! fd0_redirected_p ()) { 8021556Srgrimes close(0); 80369793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 80469793Sobrien error("Can't open %s: %s", 80569793Sobrien _PATH_DEVNULL, strerror(errno)); 8061556Srgrimes } 8071556Srgrimes } 8081556Srgrimes#endif 8091556Srgrimes if (wasroot && iflag) { 8101556Srgrimes setsignal(SIGINT); 8111556Srgrimes setsignal(SIGQUIT); 8121556Srgrimes setsignal(SIGTERM); 8131556Srgrimes } 8141556Srgrimes return pid; 8151556Srgrimes } 8161556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 8171556Srgrimes if (jp == NULL || jp->nprocs == 0) 8181556Srgrimes pgrp = pid; 8191556Srgrimes else 8201556Srgrimes pgrp = jp->ps[0].pid; 82117987Speter setpgid(pid, pgrp); 8221556Srgrimes } 8231556Srgrimes if (mode == FORK_BG) 8241556Srgrimes backgndpid = pid; /* set $! */ 8251556Srgrimes if (jp) { 8261556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 8271556Srgrimes ps->pid = pid; 8281556Srgrimes ps->status = -1; 8291556Srgrimes ps->cmd = nullstr; 8301556Srgrimes if (iflag && rootshell && n) 8311556Srgrimes ps->cmd = commandtext(n); 83297659Stjr#if JOBS 83397659Stjr setcurjob(jp); 83497659Stjr#endif 8351556Srgrimes } 8361556Srgrimes INTON; 8371556Srgrimes TRACE(("In parent shell: child = %d\n", pid)); 8381556Srgrimes return pid; 8391556Srgrimes} 8401556Srgrimes 8411556Srgrimes 8421556Srgrimes 8431556Srgrimes/* 8441556Srgrimes * Wait for job to finish. 8451556Srgrimes * 8461556Srgrimes * Under job control we have the problem that while a child process is 8471556Srgrimes * running interrupts generated by the user are sent to the child but not 8481556Srgrimes * to the shell. This means that an infinite loop started by an inter- 8491556Srgrimes * active user may be hard to kill. With job control turned off, an 8501556Srgrimes * interactive user may place an interactive program inside a loop. If 8511556Srgrimes * the interactive program catches interrupts, the user doesn't want 8521556Srgrimes * these interrupts to also abort the loop. The approach we take here 8531556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 85446684Skris * foreground process to terminate, and then send itself an interrupt 8551556Srgrimes * signal if the child process was terminated by an interrupt signal. 8561556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 8571556Srgrimes * exit on interrupt; unless these processes terminate themselves by 8581556Srgrimes * sending a signal to themselves (instead of calling exit) they will 8591556Srgrimes * confuse this approach. 8601556Srgrimes */ 8611556Srgrimes 8621556Srgrimesint 86390111Simpwaitforjob(struct job *jp, int *origstatus) 86445916Scracauer{ 8651556Srgrimes#if JOBS 86617987Speter int mypgrp = getpgrp(); 8671556Srgrimes#endif 8681556Srgrimes int status; 8691556Srgrimes int st; 8701556Srgrimes 8711556Srgrimes INTOFF; 8721556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 87338950Scracauer while (jp->state == 0) 87438950Scracauer if (dowait(1, jp) == -1) 87538950Scracauer dotrap(); 8761556Srgrimes#if JOBS 8771556Srgrimes if (jp->jobctl) { 87820425Ssteve#ifdef OLD_TTY_DRIVER 8791556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0) 88020425Ssteve error("TIOCSPGRP failed, errno=%d\n", errno); 88120425Ssteve#else 88220425Ssteve if (tcsetpgrp(2, mypgrp) < 0) 88320425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 88420425Ssteve#endif 8851556Srgrimes } 8861556Srgrimes if (jp->state == JOBSTOPPED) 88797659Stjr setcurjob(jp); 8881556Srgrimes#endif 8891556Srgrimes status = jp->ps[jp->nprocs - 1].status; 89045916Scracauer if (origstatus != NULL) 89145916Scracauer *origstatus = status; 8921556Srgrimes /* convert to 8 bits */ 89326104Ssteve if (WIFEXITED(status)) 89426104Ssteve st = WEXITSTATUS(status); 8951556Srgrimes#if JOBS 89626104Ssteve else if (WIFSTOPPED(status)) 89726104Ssteve st = WSTOPSIG(status) + 128; 8981556Srgrimes#endif 8991556Srgrimes else 90026104Ssteve st = WTERMSIG(status) + 128; 9011556Srgrimes if (! JOBS || jp->state == JOBDONE) 9021556Srgrimes freejob(jp); 90338521Scracauer if (int_pending()) { 90438521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 90538521Scracauer kill(getpid(), SIGINT); 90638521Scracauer else 90738521Scracauer CLEAR_PENDING_INT; 90838521Scracauer } 9091556Srgrimes INTON; 9101556Srgrimes return st; 9111556Srgrimes} 9121556Srgrimes 9131556Srgrimes 9141556Srgrimes 9151556Srgrimes/* 9161556Srgrimes * Wait for a process to terminate. 9171556Srgrimes */ 9181556Srgrimes 9191556SrgrimesSTATIC int 92090111Simpdowait(int block, struct job *job) 92117987Speter{ 9221556Srgrimes int pid; 9231556Srgrimes int status; 9241556Srgrimes struct procstat *sp; 9251556Srgrimes struct job *jp; 9261556Srgrimes struct job *thisjob; 9271556Srgrimes int done; 9281556Srgrimes int stopped; 92926104Ssteve int sig; 9301556Srgrimes 93138950Scracauer in_dowait++; 9321556Srgrimes TRACE(("dowait(%d) called\n", block)); 9331556Srgrimes do { 9341556Srgrimes pid = waitproc(block, &status); 9351556Srgrimes TRACE(("wait returns %d, status=%d\n", pid, status)); 93672086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 93772086Scracauer (WIFSTOPPED(status) && !iflag)); 93838950Scracauer in_dowait--; 93938521Scracauer if (breakwaitcmd != 0) { 94038521Scracauer breakwaitcmd = 0; 94138521Scracauer return -1; 94238521Scracauer } 9431556Srgrimes if (pid <= 0) 9441556Srgrimes return pid; 9451556Srgrimes INTOFF; 9461556Srgrimes thisjob = NULL; 9471556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 9481556Srgrimes if (jp->used) { 9491556Srgrimes done = 1; 9501556Srgrimes stopped = 1; 9511556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 9521556Srgrimes if (sp->pid == -1) 9531556Srgrimes continue; 9541556Srgrimes if (sp->pid == pid) { 95526104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 95626104Ssteve pid, sp->status, status)); 9571556Srgrimes sp->status = status; 9581556Srgrimes thisjob = jp; 9591556Srgrimes } 9601556Srgrimes if (sp->status == -1) 9611556Srgrimes stopped = 0; 96226104Ssteve else if (WIFSTOPPED(sp->status)) 9631556Srgrimes done = 0; 9641556Srgrimes } 9651556Srgrimes if (stopped) { /* stopped or done */ 9661556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 9671556Srgrimes if (jp->state != state) { 9681556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 9691556Srgrimes jp->state = state; 9701556Srgrimes#if JOBS 97197659Stjr if (done) 97297659Stjr deljob(jp); 9731556Srgrimes#endif 9741556Srgrimes } 9751556Srgrimes } 9761556Srgrimes } 9771556Srgrimes } 9781556Srgrimes INTON; 9791556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 9801556Srgrimes#if JOBS 98126104Ssteve if (WIFSTOPPED(status)) 98226104Ssteve sig = WSTOPSIG(status); 98326104Ssteve else 9841556Srgrimes#endif 98597663Stjr { 98626104Ssteve if (WIFEXITED(status)) 98726104Ssteve sig = 0; 98826104Ssteve else 98926104Ssteve sig = WTERMSIG(status); 9901556Srgrimes } 99197663Stjr if (sig != 0 && sig != SIGINT && sig != SIGPIPE) 99297822Stjr showjob(thisjob, pid, 0, 1); 9931556Srgrimes } else { 9941556Srgrimes TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job)); 9951556Srgrimes if (thisjob) 9961556Srgrimes thisjob->changed = 1; 9971556Srgrimes } 9981556Srgrimes return pid; 9991556Srgrimes} 10001556Srgrimes 10011556Srgrimes 10021556Srgrimes 10031556Srgrimes/* 10041556Srgrimes * Do a wait system call. If job control is compiled in, we accept 10051556Srgrimes * stopped processes. If block is zero, we return a value of zero 10061556Srgrimes * rather than blocking. 10071556Srgrimes * 10081556Srgrimes * System V doesn't have a non-blocking wait system call. It does 10091556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's 10101556Srgrimes * children dies. The obvious way to use SIGCLD would be to install 10111556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD 10121556Srgrimes * was received, and have waitproc bump another counter when it got 10131556Srgrimes * the status of a process. Waitproc would then know that a wait 10141556Srgrimes * system call would not block if the two counters were different. 10151556Srgrimes * This approach doesn't work because if a process has children that 10161556Srgrimes * have not been waited for, System V will send it a SIGCLD when it 10171556Srgrimes * installs a signal handler for SIGCLD. What this means is that when 10181556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously 10191556Srgrimes * until is runs out of stack space, unless it does a wait call before 10201556Srgrimes * restoring the signal handler. The code below takes advantage of 10211556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and 10221556Srgrimes * then checking to see whether it was called. If there are any 10231556Srgrimes * children to be waited for, it will be. 10241556Srgrimes * 10251556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking 10261556Srgrimes * waits at all. In this case, the user will not be informed when 10271556Srgrimes * a background process until the next time she runs a real program 10281556Srgrimes * (as opposed to running a builtin command or just typing return), 10291556Srgrimes * and the jobs command may give out of date information. 10301556Srgrimes */ 10311556Srgrimes 10321556Srgrimes#ifdef SYSV 103338521ScracauerSTATIC sig_atomic_t gotsigchild; 10341556Srgrimes 10351556SrgrimesSTATIC int onsigchild() { 10361556Srgrimes gotsigchild = 1; 10371556Srgrimes} 10381556Srgrimes#endif 10391556Srgrimes 10401556Srgrimes 10411556SrgrimesSTATIC int 104290111Simpwaitproc(int block, int *status) 104317987Speter{ 10441556Srgrimes#ifdef BSD 10451556Srgrimes int flags; 10461556Srgrimes 10471556Srgrimes#if JOBS 10481556Srgrimes flags = WUNTRACED; 10491556Srgrimes#else 10501556Srgrimes flags = 0; 10511556Srgrimes#endif 10521556Srgrimes if (block == 0) 10531556Srgrimes flags |= WNOHANG; 10541556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 10551556Srgrimes#else 10561556Srgrimes#ifdef SYSV 10571556Srgrimes int (*save)(); 10581556Srgrimes 10591556Srgrimes if (block == 0) { 10601556Srgrimes gotsigchild = 0; 10611556Srgrimes save = signal(SIGCLD, onsigchild); 10621556Srgrimes signal(SIGCLD, save); 10631556Srgrimes if (gotsigchild == 0) 10641556Srgrimes return 0; 10651556Srgrimes } 10661556Srgrimes return wait(status); 10671556Srgrimes#else 10681556Srgrimes if (block == 0) 10691556Srgrimes return 0; 10701556Srgrimes return wait(status); 10711556Srgrimes#endif 10721556Srgrimes#endif 10731556Srgrimes} 10741556Srgrimes 10751556Srgrimes/* 10761556Srgrimes * return 1 if there are stopped jobs, otherwise 0 10771556Srgrimes */ 10781556Srgrimesint job_warning = 0; 10791556Srgrimesint 108090111Simpstoppedjobs(void) 10811556Srgrimes{ 108225222Ssteve int jobno; 108325222Ssteve struct job *jp; 10841556Srgrimes 10851556Srgrimes if (job_warning) 10861556Srgrimes return (0); 10871556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 10881556Srgrimes if (jp->used == 0) 10891556Srgrimes continue; 10901556Srgrimes if (jp->state == JOBSTOPPED) { 10911556Srgrimes out2str("You have stopped jobs.\n"); 10921556Srgrimes job_warning = 2; 10931556Srgrimes return (1); 10941556Srgrimes } 10951556Srgrimes } 10961556Srgrimes 10971556Srgrimes return (0); 10981556Srgrimes} 10991556Srgrimes 11001556Srgrimes/* 11011556Srgrimes * Return a string identifying a command (to be printed by the 11021556Srgrimes * jobs command. 11031556Srgrimes */ 11041556Srgrimes 11051556SrgrimesSTATIC char *cmdnextc; 11061556SrgrimesSTATIC int cmdnleft; 11071556Srgrimes#define MAXCMDTEXT 200 11081556Srgrimes 11091556Srgrimeschar * 111090111Simpcommandtext(union node *n) 111190111Simp{ 11121556Srgrimes char *name; 11131556Srgrimes 11141556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 11151556Srgrimes cmdnleft = MAXCMDTEXT - 4; 11161556Srgrimes cmdtxt(n); 11171556Srgrimes *cmdnextc = '\0'; 11181556Srgrimes return name; 11191556Srgrimes} 11201556Srgrimes 11211556Srgrimes 11221556SrgrimesSTATIC void 112390111Simpcmdtxt(union node *n) 112490111Simp{ 11251556Srgrimes union node *np; 11261556Srgrimes struct nodelist *lp; 11271556Srgrimes char *p; 11281556Srgrimes int i; 11291556Srgrimes char s[2]; 11301556Srgrimes 11311556Srgrimes if (n == NULL) 11321556Srgrimes return; 11331556Srgrimes switch (n->type) { 11341556Srgrimes case NSEMI: 11351556Srgrimes cmdtxt(n->nbinary.ch1); 11361556Srgrimes cmdputs("; "); 11371556Srgrimes cmdtxt(n->nbinary.ch2); 11381556Srgrimes break; 11391556Srgrimes case NAND: 11401556Srgrimes cmdtxt(n->nbinary.ch1); 11411556Srgrimes cmdputs(" && "); 11421556Srgrimes cmdtxt(n->nbinary.ch2); 11431556Srgrimes break; 11441556Srgrimes case NOR: 11451556Srgrimes cmdtxt(n->nbinary.ch1); 11461556Srgrimes cmdputs(" || "); 11471556Srgrimes cmdtxt(n->nbinary.ch2); 11481556Srgrimes break; 11491556Srgrimes case NPIPE: 11501556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 11511556Srgrimes cmdtxt(lp->n); 11521556Srgrimes if (lp->next) 11531556Srgrimes cmdputs(" | "); 11541556Srgrimes } 11551556Srgrimes break; 11561556Srgrimes case NSUBSHELL: 11571556Srgrimes cmdputs("("); 11581556Srgrimes cmdtxt(n->nredir.n); 11591556Srgrimes cmdputs(")"); 11601556Srgrimes break; 11611556Srgrimes case NREDIR: 11621556Srgrimes case NBACKGND: 11631556Srgrimes cmdtxt(n->nredir.n); 11641556Srgrimes break; 11651556Srgrimes case NIF: 11661556Srgrimes cmdputs("if "); 11671556Srgrimes cmdtxt(n->nif.test); 11681556Srgrimes cmdputs("; then "); 11691556Srgrimes cmdtxt(n->nif.ifpart); 11701556Srgrimes cmdputs("..."); 11711556Srgrimes break; 11721556Srgrimes case NWHILE: 11731556Srgrimes cmdputs("while "); 11741556Srgrimes goto until; 11751556Srgrimes case NUNTIL: 11761556Srgrimes cmdputs("until "); 11771556Srgrimesuntil: 11781556Srgrimes cmdtxt(n->nbinary.ch1); 11791556Srgrimes cmdputs("; do "); 11801556Srgrimes cmdtxt(n->nbinary.ch2); 11811556Srgrimes cmdputs("; done"); 11821556Srgrimes break; 11831556Srgrimes case NFOR: 11841556Srgrimes cmdputs("for "); 11851556Srgrimes cmdputs(n->nfor.var); 11861556Srgrimes cmdputs(" in ..."); 11871556Srgrimes break; 11881556Srgrimes case NCASE: 11891556Srgrimes cmdputs("case "); 11901556Srgrimes cmdputs(n->ncase.expr->narg.text); 11911556Srgrimes cmdputs(" in ..."); 11921556Srgrimes break; 11931556Srgrimes case NDEFUN: 11941556Srgrimes cmdputs(n->narg.text); 11951556Srgrimes cmdputs("() ..."); 11961556Srgrimes break; 11971556Srgrimes case NCMD: 11981556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 11991556Srgrimes cmdtxt(np); 12001556Srgrimes if (np->narg.next) 12011556Srgrimes cmdputs(" "); 12021556Srgrimes } 12031556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 12041556Srgrimes cmdputs(" "); 12051556Srgrimes cmdtxt(np); 12061556Srgrimes } 12071556Srgrimes break; 12081556Srgrimes case NARG: 12091556Srgrimes cmdputs(n->narg.text); 12101556Srgrimes break; 12111556Srgrimes case NTO: 12121556Srgrimes p = ">"; i = 1; goto redir; 12131556Srgrimes case NAPPEND: 12141556Srgrimes p = ">>"; i = 1; goto redir; 12151556Srgrimes case NTOFD: 12161556Srgrimes p = ">&"; i = 1; goto redir; 121796922Stjr case NCLOBBER: 121896922Stjr p = ">|"; i = 1; goto redir; 12191556Srgrimes case NFROM: 12201556Srgrimes p = "<"; i = 0; goto redir; 122166612Sbrian case NFROMTO: 122266612Sbrian p = "<>"; i = 0; goto redir; 12231556Srgrimes case NFROMFD: 12241556Srgrimes p = "<&"; i = 0; goto redir; 12251556Srgrimesredir: 12261556Srgrimes if (n->nfile.fd != i) { 12271556Srgrimes s[0] = n->nfile.fd + '0'; 12281556Srgrimes s[1] = '\0'; 12291556Srgrimes cmdputs(s); 12301556Srgrimes } 12311556Srgrimes cmdputs(p); 12321556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 123399634Stjr if (n->ndup.dupfd >= 0) 123499634Stjr s[0] = n->ndup.dupfd + '0'; 123599634Stjr else 123699634Stjr s[0] = '-'; 12371556Srgrimes s[1] = '\0'; 12381556Srgrimes cmdputs(s); 12391556Srgrimes } else { 12401556Srgrimes cmdtxt(n->nfile.fname); 12411556Srgrimes } 12421556Srgrimes break; 12431556Srgrimes case NHERE: 12441556Srgrimes case NXHERE: 12451556Srgrimes cmdputs("<<..."); 12461556Srgrimes break; 12471556Srgrimes default: 12481556Srgrimes cmdputs("???"); 12491556Srgrimes break; 12501556Srgrimes } 12511556Srgrimes} 12521556Srgrimes 12531556Srgrimes 12541556Srgrimes 12551556SrgrimesSTATIC void 125690111Simpcmdputs(char *s) 125790111Simp{ 125825222Ssteve char *p, *q; 125925222Ssteve char c; 12601556Srgrimes int subtype = 0; 12611556Srgrimes 12621556Srgrimes if (cmdnleft <= 0) 12631556Srgrimes return; 12641556Srgrimes p = s; 12651556Srgrimes q = cmdnextc; 12661556Srgrimes while ((c = *p++) != '\0') { 12671556Srgrimes if (c == CTLESC) 12681556Srgrimes *q++ = *p++; 12691556Srgrimes else if (c == CTLVAR) { 12701556Srgrimes *q++ = '$'; 12711556Srgrimes if (--cmdnleft > 0) 12721556Srgrimes *q++ = '{'; 12731556Srgrimes subtype = *p++; 12741556Srgrimes } else if (c == '=' && subtype != 0) { 12751556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 12761556Srgrimes subtype = 0; 12771556Srgrimes } else if (c == CTLENDVAR) { 12781556Srgrimes *q++ = '}'; 127918954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 12801556Srgrimes cmdnleft++; /* ignore it */ 12811556Srgrimes else 12821556Srgrimes *q++ = c; 12831556Srgrimes if (--cmdnleft <= 0) { 12841556Srgrimes *q++ = '.'; 12851556Srgrimes *q++ = '.'; 12861556Srgrimes *q++ = '.'; 12871556Srgrimes break; 12881556Srgrimes } 12891556Srgrimes } 12901556Srgrimes cmdnextc = q; 12911556Srgrimes} 1292