jobs.c revision 99110
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 99110 2002-06-30 05:15:05Z obrien $"); 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 */ 62097659Stjr jobmru = &jp[jobmru - jobtab]; 62197659Stjr for (i = 0; i < njobs; i++) 62297659Stjr if (jp[i].next != NULL) 62397659Stjr jp[i].next = &jp[jp[i].next - 62497659Stjr jobtab]; 62597659Stjr#endif 62620425Ssteve /* Relocate `ps' pointers */ 62720425Ssteve for (i = 0; i < njobs; i++) 62820425Ssteve if (jp[i].ps == &jobtab[i].ps0) 62920425Ssteve jp[i].ps = &jp[i].ps0; 6301556Srgrimes ckfree(jobtab); 6311556Srgrimes jobtab = jp; 6321556Srgrimes } 6331556Srgrimes jp = jobtab + njobs; 6341556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 6351556Srgrimes INTON; 6361556Srgrimes break; 6371556Srgrimes } 6381556Srgrimes if (jp->used == 0) 6391556Srgrimes break; 6401556Srgrimes } 6411556Srgrimes INTOFF; 6421556Srgrimes jp->state = 0; 6431556Srgrimes jp->used = 1; 6441556Srgrimes jp->changed = 0; 6451556Srgrimes jp->nprocs = 0; 6461556Srgrimes#if JOBS 6471556Srgrimes jp->jobctl = jobctl; 64897659Stjr jp->next = NULL; 6491556Srgrimes#endif 6501556Srgrimes if (nprocs > 1) { 6511556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 6521556Srgrimes } else { 6531556Srgrimes jp->ps = &jp->ps0; 6541556Srgrimes } 6551556Srgrimes INTON; 65617987Speter TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, 65717987Speter jp - jobtab + 1)); 6581556Srgrimes return jp; 6598855Srgrimes} 6601556Srgrimes 66197659Stjr#if JOBS 66297659StjrSTATIC void 66397659Stjrsetcurjob(struct job *cj) 66497659Stjr{ 66597659Stjr struct job *jp, *prev; 6661556Srgrimes 66797659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 66897659Stjr if (jp == cj) { 66997659Stjr if (prev != NULL) 67097659Stjr prev->next = jp->next; 67197659Stjr else 67297659Stjr jobmru = jp->next; 67397659Stjr jp->next = jobmru; 67497659Stjr jobmru = cj; 67597659Stjr return; 67697659Stjr } 67797659Stjr } 67897659Stjr cj->next = jobmru; 67997659Stjr jobmru = cj; 68097659Stjr} 68197659Stjr 68297659StjrSTATIC void 68397659Stjrdeljob(struct job *j) 68497659Stjr{ 68597659Stjr struct job *jp, *prev; 68697659Stjr 68797659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 68897659Stjr if (jp == j) { 68997659Stjr if (prev != NULL) 69097659Stjr prev->next = jp->next; 69197659Stjr else 69297659Stjr jobmru = jp->next; 69397659Stjr return; 69497659Stjr } 69597659Stjr } 69697659Stjr} 69797659Stjr 6981556Srgrimes/* 69997659Stjr * Return the most recently used job that isn't `nj', and preferably one 70097659Stjr * that is stopped. 70197659Stjr */ 70297659StjrSTATIC struct job * 70397659Stjrgetcurjob(struct job *nj) 70497659Stjr{ 70597659Stjr struct job *jp; 70697659Stjr 70797659Stjr /* Try to find a stopped one.. */ 70897659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 70997659Stjr if (jp->used && jp != nj && jp->state == JOBSTOPPED) 71097659Stjr return (jp); 71197659Stjr /* Otherwise the most recently used job that isn't `nj' */ 71297659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 71397659Stjr if (jp->used && jp != nj) 71497659Stjr return (jp); 71597659Stjr 71697659Stjr return (NULL); 71797659Stjr} 71897659Stjr 71997659Stjr#endif 72097659Stjr 72197659Stjr/* 7221556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 7231556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 7241556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 7251556Srgrimes * be NULL. The mode parameter can be one of the following: 7261556Srgrimes * FORK_FG - Fork off a foreground process. 7271556Srgrimes * FORK_BG - Fork off a background process. 7281556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 7291556Srgrimes * process group even if job control is on. 7301556Srgrimes * 7311556Srgrimes * When job control is turned off, background processes have their standard 7321556Srgrimes * input redirected to /dev/null (except for the second and later processes 7331556Srgrimes * in a pipeline). 7341556Srgrimes */ 7351556Srgrimes 7361556Srgrimesint 73790111Simpforkshell(struct job *jp, union node *n, int mode) 73817987Speter{ 7391556Srgrimes int pid; 7401556Srgrimes int pgrp; 7411556Srgrimes 74217987Speter TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n, 74317987Speter mode)); 7441556Srgrimes INTOFF; 7451556Srgrimes pid = fork(); 7461556Srgrimes if (pid == -1) { 7471556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 7481556Srgrimes INTON; 74953891Scracauer error("Cannot fork: %s", strerror(errno)); 7501556Srgrimes } 7511556Srgrimes if (pid == 0) { 7521556Srgrimes struct job *p; 7531556Srgrimes int wasroot; 7541556Srgrimes int i; 7551556Srgrimes 7561556Srgrimes TRACE(("Child shell %d\n", getpid())); 7571556Srgrimes wasroot = rootshell; 7581556Srgrimes rootshell = 0; 7591556Srgrimes for (i = njobs, p = jobtab ; --i >= 0 ; p++) 7601556Srgrimes if (p->used) 7611556Srgrimes freejob(p); 7621556Srgrimes closescript(); 7631556Srgrimes INTON; 7641556Srgrimes clear_traps(); 7651556Srgrimes#if JOBS 7661556Srgrimes jobctl = 0; /* do job control only in root shell */ 7671556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 7681556Srgrimes if (jp == NULL || jp->nprocs == 0) 7691556Srgrimes pgrp = getpid(); 7701556Srgrimes else 7711556Srgrimes pgrp = jp->ps[0].pid; 77221352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 7731556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 77420425Ssteve#ifdef OLD_TTY_DRIVER 7751556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0) 77618016Speter error("TIOCSPGRP failed, errno=%d", errno); 77720425Ssteve#else 77820425Ssteve if (tcsetpgrp(2, pgrp) < 0) 77920425Ssteve error("tcsetpgrp failed, errno=%d", errno); 78020425Ssteve#endif 7811556Srgrimes } 7821556Srgrimes setsignal(SIGTSTP); 7831556Srgrimes setsignal(SIGTTOU); 7841556Srgrimes } else if (mode == FORK_BG) { 7851556Srgrimes ignoresig(SIGINT); 7861556Srgrimes ignoresig(SIGQUIT); 7871556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7881556Srgrimes ! fd0_redirected_p ()) { 7891556Srgrimes close(0); 79069793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 79169793Sobrien error("Can't open %s: %s", 79269793Sobrien _PATH_DEVNULL, strerror(errno)); 7931556Srgrimes } 7941556Srgrimes } 7951556Srgrimes#else 7961556Srgrimes if (mode == FORK_BG) { 7971556Srgrimes ignoresig(SIGINT); 7981556Srgrimes ignoresig(SIGQUIT); 7991556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 8001556Srgrimes ! fd0_redirected_p ()) { 8011556Srgrimes close(0); 80269793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 80369793Sobrien error("Can't open %s: %s", 80469793Sobrien _PATH_DEVNULL, strerror(errno)); 8051556Srgrimes } 8061556Srgrimes } 8071556Srgrimes#endif 8081556Srgrimes if (wasroot && iflag) { 8091556Srgrimes setsignal(SIGINT); 8101556Srgrimes setsignal(SIGQUIT); 8111556Srgrimes setsignal(SIGTERM); 8121556Srgrimes } 8131556Srgrimes return pid; 8141556Srgrimes } 8151556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 8161556Srgrimes if (jp == NULL || jp->nprocs == 0) 8171556Srgrimes pgrp = pid; 8181556Srgrimes else 8191556Srgrimes pgrp = jp->ps[0].pid; 82017987Speter setpgid(pid, pgrp); 8211556Srgrimes } 8221556Srgrimes if (mode == FORK_BG) 8231556Srgrimes backgndpid = pid; /* set $! */ 8241556Srgrimes if (jp) { 8251556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 8261556Srgrimes ps->pid = pid; 8271556Srgrimes ps->status = -1; 8281556Srgrimes ps->cmd = nullstr; 8291556Srgrimes if (iflag && rootshell && n) 8301556Srgrimes ps->cmd = commandtext(n); 83197659Stjr#if JOBS 83297659Stjr setcurjob(jp); 83397659Stjr#endif 8341556Srgrimes } 8351556Srgrimes INTON; 8361556Srgrimes TRACE(("In parent shell: child = %d\n", pid)); 8371556Srgrimes return pid; 8381556Srgrimes} 8391556Srgrimes 8401556Srgrimes 8411556Srgrimes 8421556Srgrimes/* 8431556Srgrimes * Wait for job to finish. 8441556Srgrimes * 8451556Srgrimes * Under job control we have the problem that while a child process is 8461556Srgrimes * running interrupts generated by the user are sent to the child but not 8471556Srgrimes * to the shell. This means that an infinite loop started by an inter- 8481556Srgrimes * active user may be hard to kill. With job control turned off, an 8491556Srgrimes * interactive user may place an interactive program inside a loop. If 8501556Srgrimes * the interactive program catches interrupts, the user doesn't want 8511556Srgrimes * these interrupts to also abort the loop. The approach we take here 8521556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 85346684Skris * foreground process to terminate, and then send itself an interrupt 8541556Srgrimes * signal if the child process was terminated by an interrupt signal. 8551556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 8561556Srgrimes * exit on interrupt; unless these processes terminate themselves by 8571556Srgrimes * sending a signal to themselves (instead of calling exit) they will 8581556Srgrimes * confuse this approach. 8591556Srgrimes */ 8601556Srgrimes 8611556Srgrimesint 86290111Simpwaitforjob(struct job *jp, int *origstatus) 86345916Scracauer{ 8641556Srgrimes#if JOBS 86517987Speter int mypgrp = getpgrp(); 8661556Srgrimes#endif 8671556Srgrimes int status; 8681556Srgrimes int st; 8691556Srgrimes 8701556Srgrimes INTOFF; 8711556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 87238950Scracauer while (jp->state == 0) 87338950Scracauer if (dowait(1, jp) == -1) 87438950Scracauer dotrap(); 8751556Srgrimes#if JOBS 8761556Srgrimes if (jp->jobctl) { 87720425Ssteve#ifdef OLD_TTY_DRIVER 8781556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0) 87920425Ssteve error("TIOCSPGRP failed, errno=%d\n", errno); 88020425Ssteve#else 88120425Ssteve if (tcsetpgrp(2, mypgrp) < 0) 88220425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 88320425Ssteve#endif 8841556Srgrimes } 8851556Srgrimes if (jp->state == JOBSTOPPED) 88697659Stjr setcurjob(jp); 8871556Srgrimes#endif 8881556Srgrimes status = jp->ps[jp->nprocs - 1].status; 88945916Scracauer if (origstatus != NULL) 89045916Scracauer *origstatus = status; 8911556Srgrimes /* convert to 8 bits */ 89226104Ssteve if (WIFEXITED(status)) 89326104Ssteve st = WEXITSTATUS(status); 8941556Srgrimes#if JOBS 89526104Ssteve else if (WIFSTOPPED(status)) 89626104Ssteve st = WSTOPSIG(status) + 128; 8971556Srgrimes#endif 8981556Srgrimes else 89926104Ssteve st = WTERMSIG(status) + 128; 9001556Srgrimes if (! JOBS || jp->state == JOBDONE) 9011556Srgrimes freejob(jp); 90238521Scracauer if (int_pending()) { 90338521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 90438521Scracauer kill(getpid(), SIGINT); 90538521Scracauer else 90638521Scracauer CLEAR_PENDING_INT; 90738521Scracauer } 9081556Srgrimes INTON; 9091556Srgrimes return st; 9101556Srgrimes} 9111556Srgrimes 9121556Srgrimes 9131556Srgrimes 9141556Srgrimes/* 9151556Srgrimes * Wait for a process to terminate. 9161556Srgrimes */ 9171556Srgrimes 9181556SrgrimesSTATIC int 91990111Simpdowait(int block, struct job *job) 92017987Speter{ 9211556Srgrimes int pid; 9221556Srgrimes int status; 9231556Srgrimes struct procstat *sp; 9241556Srgrimes struct job *jp; 9251556Srgrimes struct job *thisjob; 9261556Srgrimes int done; 9271556Srgrimes int stopped; 92826104Ssteve int sig; 9291556Srgrimes 93038950Scracauer in_dowait++; 9311556Srgrimes TRACE(("dowait(%d) called\n", block)); 9321556Srgrimes do { 9331556Srgrimes pid = waitproc(block, &status); 9341556Srgrimes TRACE(("wait returns %d, status=%d\n", pid, status)); 93572086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 93672086Scracauer (WIFSTOPPED(status) && !iflag)); 93738950Scracauer in_dowait--; 93838521Scracauer if (breakwaitcmd != 0) { 93938521Scracauer breakwaitcmd = 0; 94038521Scracauer return -1; 94138521Scracauer } 9421556Srgrimes if (pid <= 0) 9431556Srgrimes return pid; 9441556Srgrimes INTOFF; 9451556Srgrimes thisjob = NULL; 9461556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 9471556Srgrimes if (jp->used) { 9481556Srgrimes done = 1; 9491556Srgrimes stopped = 1; 9501556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 9511556Srgrimes if (sp->pid == -1) 9521556Srgrimes continue; 9531556Srgrimes if (sp->pid == pid) { 95426104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 95526104Ssteve pid, sp->status, status)); 9561556Srgrimes sp->status = status; 9571556Srgrimes thisjob = jp; 9581556Srgrimes } 9591556Srgrimes if (sp->status == -1) 9601556Srgrimes stopped = 0; 96126104Ssteve else if (WIFSTOPPED(sp->status)) 9621556Srgrimes done = 0; 9631556Srgrimes } 9641556Srgrimes if (stopped) { /* stopped or done */ 9651556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 9661556Srgrimes if (jp->state != state) { 9671556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 9681556Srgrimes jp->state = state; 9691556Srgrimes#if JOBS 97097659Stjr if (done) 97197659Stjr deljob(jp); 9721556Srgrimes#endif 9731556Srgrimes } 9741556Srgrimes } 9751556Srgrimes } 9761556Srgrimes } 9771556Srgrimes INTON; 9781556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 9791556Srgrimes#if JOBS 98026104Ssteve if (WIFSTOPPED(status)) 98126104Ssteve sig = WSTOPSIG(status); 98226104Ssteve else 9831556Srgrimes#endif 98497663Stjr { 98526104Ssteve if (WIFEXITED(status)) 98626104Ssteve sig = 0; 98726104Ssteve else 98826104Ssteve sig = WTERMSIG(status); 9891556Srgrimes } 99097663Stjr if (sig != 0 && sig != SIGINT && sig != SIGPIPE) 99197822Stjr showjob(thisjob, pid, 0, 1); 9921556Srgrimes } else { 9931556Srgrimes TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job)); 9941556Srgrimes if (thisjob) 9951556Srgrimes thisjob->changed = 1; 9961556Srgrimes } 9971556Srgrimes return pid; 9981556Srgrimes} 9991556Srgrimes 10001556Srgrimes 10011556Srgrimes 10021556Srgrimes/* 10031556Srgrimes * Do a wait system call. If job control is compiled in, we accept 10041556Srgrimes * stopped processes. If block is zero, we return a value of zero 10051556Srgrimes * rather than blocking. 10061556Srgrimes * 10071556Srgrimes * System V doesn't have a non-blocking wait system call. It does 10081556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's 10091556Srgrimes * children dies. The obvious way to use SIGCLD would be to install 10101556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD 10111556Srgrimes * was received, and have waitproc bump another counter when it got 10121556Srgrimes * the status of a process. Waitproc would then know that a wait 10131556Srgrimes * system call would not block if the two counters were different. 10141556Srgrimes * This approach doesn't work because if a process has children that 10151556Srgrimes * have not been waited for, System V will send it a SIGCLD when it 10161556Srgrimes * installs a signal handler for SIGCLD. What this means is that when 10171556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously 10181556Srgrimes * until is runs out of stack space, unless it does a wait call before 10191556Srgrimes * restoring the signal handler. The code below takes advantage of 10201556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and 10211556Srgrimes * then checking to see whether it was called. If there are any 10221556Srgrimes * children to be waited for, it will be. 10231556Srgrimes * 10241556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking 10251556Srgrimes * waits at all. In this case, the user will not be informed when 10261556Srgrimes * a background process until the next time she runs a real program 10271556Srgrimes * (as opposed to running a builtin command or just typing return), 10281556Srgrimes * and the jobs command may give out of date information. 10291556Srgrimes */ 10301556Srgrimes 10311556Srgrimes#ifdef SYSV 103238521ScracauerSTATIC sig_atomic_t gotsigchild; 10331556Srgrimes 10341556SrgrimesSTATIC int onsigchild() { 10351556Srgrimes gotsigchild = 1; 10361556Srgrimes} 10371556Srgrimes#endif 10381556Srgrimes 10391556Srgrimes 10401556SrgrimesSTATIC int 104190111Simpwaitproc(int block, int *status) 104217987Speter{ 10431556Srgrimes#ifdef BSD 10441556Srgrimes int flags; 10451556Srgrimes 10461556Srgrimes#if JOBS 10471556Srgrimes flags = WUNTRACED; 10481556Srgrimes#else 10491556Srgrimes flags = 0; 10501556Srgrimes#endif 10511556Srgrimes if (block == 0) 10521556Srgrimes flags |= WNOHANG; 10531556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 10541556Srgrimes#else 10551556Srgrimes#ifdef SYSV 10561556Srgrimes int (*save)(); 10571556Srgrimes 10581556Srgrimes if (block == 0) { 10591556Srgrimes gotsigchild = 0; 10601556Srgrimes save = signal(SIGCLD, onsigchild); 10611556Srgrimes signal(SIGCLD, save); 10621556Srgrimes if (gotsigchild == 0) 10631556Srgrimes return 0; 10641556Srgrimes } 10651556Srgrimes return wait(status); 10661556Srgrimes#else 10671556Srgrimes if (block == 0) 10681556Srgrimes return 0; 10691556Srgrimes return wait(status); 10701556Srgrimes#endif 10711556Srgrimes#endif 10721556Srgrimes} 10731556Srgrimes 10741556Srgrimes/* 10751556Srgrimes * return 1 if there are stopped jobs, otherwise 0 10761556Srgrimes */ 10771556Srgrimesint job_warning = 0; 10781556Srgrimesint 107990111Simpstoppedjobs(void) 10801556Srgrimes{ 108125222Ssteve int jobno; 108225222Ssteve struct job *jp; 10831556Srgrimes 10841556Srgrimes if (job_warning) 10851556Srgrimes return (0); 10861556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 10871556Srgrimes if (jp->used == 0) 10881556Srgrimes continue; 10891556Srgrimes if (jp->state == JOBSTOPPED) { 10901556Srgrimes out2str("You have stopped jobs.\n"); 10911556Srgrimes job_warning = 2; 10921556Srgrimes return (1); 10931556Srgrimes } 10941556Srgrimes } 10951556Srgrimes 10961556Srgrimes return (0); 10971556Srgrimes} 10981556Srgrimes 10991556Srgrimes/* 11001556Srgrimes * Return a string identifying a command (to be printed by the 11011556Srgrimes * jobs command. 11021556Srgrimes */ 11031556Srgrimes 11041556SrgrimesSTATIC char *cmdnextc; 11051556SrgrimesSTATIC int cmdnleft; 11061556Srgrimes#define MAXCMDTEXT 200 11071556Srgrimes 11081556Srgrimeschar * 110990111Simpcommandtext(union node *n) 111090111Simp{ 11111556Srgrimes char *name; 11121556Srgrimes 11131556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 11141556Srgrimes cmdnleft = MAXCMDTEXT - 4; 11151556Srgrimes cmdtxt(n); 11161556Srgrimes *cmdnextc = '\0'; 11171556Srgrimes return name; 11181556Srgrimes} 11191556Srgrimes 11201556Srgrimes 11211556SrgrimesSTATIC void 112290111Simpcmdtxt(union node *n) 112390111Simp{ 11241556Srgrimes union node *np; 11251556Srgrimes struct nodelist *lp; 11261556Srgrimes char *p; 11271556Srgrimes int i; 11281556Srgrimes char s[2]; 11291556Srgrimes 11301556Srgrimes if (n == NULL) 11311556Srgrimes return; 11321556Srgrimes switch (n->type) { 11331556Srgrimes case NSEMI: 11341556Srgrimes cmdtxt(n->nbinary.ch1); 11351556Srgrimes cmdputs("; "); 11361556Srgrimes cmdtxt(n->nbinary.ch2); 11371556Srgrimes break; 11381556Srgrimes case NAND: 11391556Srgrimes cmdtxt(n->nbinary.ch1); 11401556Srgrimes cmdputs(" && "); 11411556Srgrimes cmdtxt(n->nbinary.ch2); 11421556Srgrimes break; 11431556Srgrimes case NOR: 11441556Srgrimes cmdtxt(n->nbinary.ch1); 11451556Srgrimes cmdputs(" || "); 11461556Srgrimes cmdtxt(n->nbinary.ch2); 11471556Srgrimes break; 11481556Srgrimes case NPIPE: 11491556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 11501556Srgrimes cmdtxt(lp->n); 11511556Srgrimes if (lp->next) 11521556Srgrimes cmdputs(" | "); 11531556Srgrimes } 11541556Srgrimes break; 11551556Srgrimes case NSUBSHELL: 11561556Srgrimes cmdputs("("); 11571556Srgrimes cmdtxt(n->nredir.n); 11581556Srgrimes cmdputs(")"); 11591556Srgrimes break; 11601556Srgrimes case NREDIR: 11611556Srgrimes case NBACKGND: 11621556Srgrimes cmdtxt(n->nredir.n); 11631556Srgrimes break; 11641556Srgrimes case NIF: 11651556Srgrimes cmdputs("if "); 11661556Srgrimes cmdtxt(n->nif.test); 11671556Srgrimes cmdputs("; then "); 11681556Srgrimes cmdtxt(n->nif.ifpart); 11691556Srgrimes cmdputs("..."); 11701556Srgrimes break; 11711556Srgrimes case NWHILE: 11721556Srgrimes cmdputs("while "); 11731556Srgrimes goto until; 11741556Srgrimes case NUNTIL: 11751556Srgrimes cmdputs("until "); 11761556Srgrimesuntil: 11771556Srgrimes cmdtxt(n->nbinary.ch1); 11781556Srgrimes cmdputs("; do "); 11791556Srgrimes cmdtxt(n->nbinary.ch2); 11801556Srgrimes cmdputs("; done"); 11811556Srgrimes break; 11821556Srgrimes case NFOR: 11831556Srgrimes cmdputs("for "); 11841556Srgrimes cmdputs(n->nfor.var); 11851556Srgrimes cmdputs(" in ..."); 11861556Srgrimes break; 11871556Srgrimes case NCASE: 11881556Srgrimes cmdputs("case "); 11891556Srgrimes cmdputs(n->ncase.expr->narg.text); 11901556Srgrimes cmdputs(" in ..."); 11911556Srgrimes break; 11921556Srgrimes case NDEFUN: 11931556Srgrimes cmdputs(n->narg.text); 11941556Srgrimes cmdputs("() ..."); 11951556Srgrimes break; 11961556Srgrimes case NCMD: 11971556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 11981556Srgrimes cmdtxt(np); 11991556Srgrimes if (np->narg.next) 12001556Srgrimes cmdputs(" "); 12011556Srgrimes } 12021556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 12031556Srgrimes cmdputs(" "); 12041556Srgrimes cmdtxt(np); 12051556Srgrimes } 12061556Srgrimes break; 12071556Srgrimes case NARG: 12081556Srgrimes cmdputs(n->narg.text); 12091556Srgrimes break; 12101556Srgrimes case NTO: 12111556Srgrimes p = ">"; i = 1; goto redir; 12121556Srgrimes case NAPPEND: 12131556Srgrimes p = ">>"; i = 1; goto redir; 12141556Srgrimes case NTOFD: 12151556Srgrimes p = ">&"; i = 1; goto redir; 121696922Stjr case NCLOBBER: 121796922Stjr p = ">|"; i = 1; goto redir; 12181556Srgrimes case NFROM: 12191556Srgrimes p = "<"; i = 0; goto redir; 122066612Sbrian case NFROMTO: 122166612Sbrian p = "<>"; i = 0; goto redir; 12221556Srgrimes case NFROMFD: 12231556Srgrimes p = "<&"; i = 0; goto redir; 12241556Srgrimesredir: 12251556Srgrimes if (n->nfile.fd != i) { 12261556Srgrimes s[0] = n->nfile.fd + '0'; 12271556Srgrimes s[1] = '\0'; 12281556Srgrimes cmdputs(s); 12291556Srgrimes } 12301556Srgrimes cmdputs(p); 12311556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 12321556Srgrimes s[0] = n->ndup.dupfd + '0'; 12331556Srgrimes s[1] = '\0'; 12341556Srgrimes cmdputs(s); 12351556Srgrimes } else { 12361556Srgrimes cmdtxt(n->nfile.fname); 12371556Srgrimes } 12381556Srgrimes break; 12391556Srgrimes case NHERE: 12401556Srgrimes case NXHERE: 12411556Srgrimes cmdputs("<<..."); 12421556Srgrimes break; 12431556Srgrimes default: 12441556Srgrimes cmdputs("???"); 12451556Srgrimes break; 12461556Srgrimes } 12471556Srgrimes} 12481556Srgrimes 12491556Srgrimes 12501556Srgrimes 12511556SrgrimesSTATIC void 125290111Simpcmdputs(char *s) 125390111Simp{ 125425222Ssteve char *p, *q; 125525222Ssteve char c; 12561556Srgrimes int subtype = 0; 12571556Srgrimes 12581556Srgrimes if (cmdnleft <= 0) 12591556Srgrimes return; 12601556Srgrimes p = s; 12611556Srgrimes q = cmdnextc; 12621556Srgrimes while ((c = *p++) != '\0') { 12631556Srgrimes if (c == CTLESC) 12641556Srgrimes *q++ = *p++; 12651556Srgrimes else if (c == CTLVAR) { 12661556Srgrimes *q++ = '$'; 12671556Srgrimes if (--cmdnleft > 0) 12681556Srgrimes *q++ = '{'; 12691556Srgrimes subtype = *p++; 12701556Srgrimes } else if (c == '=' && subtype != 0) { 12711556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 12721556Srgrimes subtype = 0; 12731556Srgrimes } else if (c == CTLENDVAR) { 12741556Srgrimes *q++ = '}'; 127518954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 12761556Srgrimes cmdnleft++; /* ignore it */ 12771556Srgrimes else 12781556Srgrimes *q++ = c; 12791556Srgrimes if (--cmdnleft <= 0) { 12801556Srgrimes *q++ = '.'; 12811556Srgrimes *q++ = '.'; 12821556Srgrimes *q++ = '.'; 12831556Srgrimes break; 12841556Srgrimes } 12851556Srgrimes } 12861556Srgrimes cmdnextc = q; 12871556Srgrimes} 1288