jobs.c revision 208881
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 * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/jobs.c 208881 2010-06-06 22:27:32Z jilles $"); 401556Srgrimes 4117987Speter#include <fcntl.h> 4217987Speter#include <signal.h> 4317987Speter#include <errno.h> 4499762Stjr#include <paths.h> 4517987Speter#include <unistd.h> 4617987Speter#include <stdlib.h> 4717987Speter#include <sys/param.h> 4817987Speter#include <sys/wait.h> 4917987Speter#include <sys/time.h> 5017987Speter#include <sys/resource.h> 5169793Sobrien#include <paths.h> 5218018Speter#include <sys/ioctl.h> 5317987Speter 541556Srgrimes#include "shell.h" 551556Srgrimes#if JOBS 5617987Speter#include <termios.h> 571556Srgrimes#undef CEOF /* syntax.h redefines this */ 581556Srgrimes#endif 5917987Speter#include "redir.h" 6017987Speter#include "show.h" 611556Srgrimes#include "main.h" 621556Srgrimes#include "parser.h" 631556Srgrimes#include "nodes.h" 641556Srgrimes#include "jobs.h" 651556Srgrimes#include "options.h" 661556Srgrimes#include "trap.h" 671556Srgrimes#include "syntax.h" 681556Srgrimes#include "input.h" 691556Srgrimes#include "output.h" 701556Srgrimes#include "memalloc.h" 711556Srgrimes#include "error.h" 721556Srgrimes#include "mystring.h" 731556Srgrimes 741556Srgrimes 75117261SddsSTATIC struct job *jobtab; /* array of jobs */ 76117261SddsSTATIC int njobs; /* size of array */ 7728346SsteveMKINIT pid_t backgndpid = -1; /* pid of last background process */ 781556Srgrimes#if JOBS 79117261SddsSTATIC struct job *jobmru; /* most recently used job list */ 80117261SddsSTATIC pid_t initialpgrp; /* pgrp of shell on invocation */ 811556Srgrimes#endif 8238536Scracauerint in_waitcmd = 0; /* are we in waitcmd()? */ 8338950Scracauerint in_dowait = 0; /* are we in dowait()? */ 8438536Scracauervolatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */ 8599762Stjrstatic int ttyfd = -1; 861556Srgrimes 8720425Ssteve#if JOBS 8890111SimpSTATIC void restartjob(struct job *); 8920425Ssteve#endif 9090111SimpSTATIC void freejob(struct job *); 9190111SimpSTATIC struct job *getjob(char *); 92100308StjrSTATIC pid_t dowait(int, struct job *); 93100308StjrSTATIC pid_t waitproc(int, int *); 94208489SjillesSTATIC void checkzombies(void); 9590111SimpSTATIC void cmdtxt(union node *); 96201053SjillesSTATIC void cmdputs(const char *); 9797659Stjr#if JOBS 9897659StjrSTATIC void setcurjob(struct job *); 9997659StjrSTATIC void deljob(struct job *); 10097659StjrSTATIC struct job *getcurjob(struct job *); 10197659Stjr#endif 102163085SstefanfSTATIC void showjob(struct job *, pid_t, int); 1031556Srgrimes 1041556Srgrimes 1051556Srgrimes/* 1061556Srgrimes * Turn job control on and off. 1071556Srgrimes */ 1081556Srgrimes 1091556SrgrimesMKINIT int jobctl; 1101556Srgrimes 11120425Ssteve#if JOBS 1121556Srgrimesvoid 11390111Simpsetjobctl(int on) 11417987Speter{ 11599762Stjr int i; 1161556Srgrimes 1171556Srgrimes if (on == jobctl || rootshell == 0) 1181556Srgrimes return; 1191556Srgrimes if (on) { 12099762Stjr if (ttyfd != -1) 12199762Stjr close(ttyfd); 12299762Stjr if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) { 12399762Stjr i = 0; 12499762Stjr while (i <= 2 && !isatty(i)) 12599762Stjr i++; 126109927Stjr if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0) 12799762Stjr goto out; 12899762Stjr } 129109927Stjr if (ttyfd < 10) { 130109927Stjr /* 131109927Stjr * Keep our TTY file descriptor out of the way of 132109927Stjr * the user's redirections. 133109927Stjr */ 134109927Stjr if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) { 135109927Stjr close(ttyfd); 136109927Stjr ttyfd = -1; 137109927Stjr goto out; 138109927Stjr } 139109927Stjr close(ttyfd); 140109927Stjr ttyfd = i; 141109927Stjr } 142103223Snectar if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) { 14399762Stjr close(ttyfd); 14499762Stjr ttyfd = -1; 14599762Stjr goto out; 14699762Stjr } 1471556Srgrimes do { /* while we are in the background */ 14899762Stjr initialpgrp = tcgetpgrp(ttyfd); 14920425Ssteve if (initialpgrp < 0) { 150199629Sjillesout: out2fmt_flush("sh: can't access tty; job control turned off\n"); 1511556Srgrimes mflag = 0; 1521556Srgrimes return; 1531556Srgrimes } 1541556Srgrimes if (initialpgrp == -1) 15517987Speter initialpgrp = getpgrp(); 15617987Speter else if (initialpgrp != getpgrp()) { 15799762Stjr killpg(0, SIGTTIN); 1581556Srgrimes continue; 1591556Srgrimes } 1601556Srgrimes } while (0); 1611556Srgrimes setsignal(SIGTSTP); 1621556Srgrimes setsignal(SIGTTOU); 1631556Srgrimes setsignal(SIGTTIN); 16417987Speter setpgid(0, rootpid); 16599762Stjr tcsetpgrp(ttyfd, rootpid); 1661556Srgrimes } else { /* turning job control off */ 16717987Speter setpgid(0, initialpgrp); 16899762Stjr tcsetpgrp(ttyfd, initialpgrp); 16999762Stjr close(ttyfd); 17099762Stjr ttyfd = -1; 1711556Srgrimes setsignal(SIGTSTP); 1721556Srgrimes setsignal(SIGTTOU); 1731556Srgrimes setsignal(SIGTTIN); 1741556Srgrimes } 1751556Srgrimes jobctl = on; 1761556Srgrimes} 17720425Ssteve#endif 1781556Srgrimes 1791556Srgrimes 1801556Srgrimes#ifdef mkinit 18128346SsteveINCLUDE <sys/types.h> 18217987SpeterINCLUDE <stdlib.h> 1831556Srgrimes 1841556SrgrimesSHELLPROC { 1851556Srgrimes backgndpid = -1; 1861556Srgrimes#if JOBS 1871556Srgrimes jobctl = 0; 1881556Srgrimes#endif 1891556Srgrimes} 1901556Srgrimes 1911556Srgrimes#endif 1921556Srgrimes 1931556Srgrimes 1941556Srgrimes 1951556Srgrimes#if JOBS 19617987Speterint 19790111Simpfgcmd(int argc __unused, char **argv) 19817987Speter{ 1991556Srgrimes struct job *jp; 200100308Stjr pid_t pgrp; 2011556Srgrimes int status; 2021556Srgrimes 2031556Srgrimes jp = getjob(argv[1]); 2041556Srgrimes if (jp->jobctl == 0) 2051556Srgrimes error("job not created under job control"); 20696933Stjr out1str(jp->ps[0].cmd); 20796933Stjr out1c('\n'); 20896933Stjr flushout(&output); 2091556Srgrimes pgrp = jp->ps[0].pid; 21099762Stjr tcsetpgrp(ttyfd, pgrp); 2111556Srgrimes restartjob(jp); 212100305Stjr jp->foreground = 1; 2131556Srgrimes INTOFF; 21445916Scracauer status = waitforjob(jp, (int *)NULL); 2151556Srgrimes INTON; 2161556Srgrimes return status; 2171556Srgrimes} 2181556Srgrimes 2191556Srgrimes 22017987Speterint 22190111Simpbgcmd(int argc, char **argv) 22217987Speter{ 22396933Stjr char s[64]; 2241556Srgrimes struct job *jp; 2251556Srgrimes 2261556Srgrimes do { 2271556Srgrimes jp = getjob(*++argv); 2281556Srgrimes if (jp->jobctl == 0) 2291556Srgrimes error("job not created under job control"); 23096933Stjr if (jp->state == JOBDONE) 23196933Stjr continue; 2321556Srgrimes restartjob(jp); 233100305Stjr jp->foreground = 0; 234104275Smux fmtstr(s, 64, "[%td] ", jp - jobtab + 1); 23596933Stjr out1str(s); 23696933Stjr out1str(jp->ps[0].cmd); 23796933Stjr out1c('\n'); 2381556Srgrimes } while (--argc > 1); 2391556Srgrimes return 0; 2401556Srgrimes} 2411556Srgrimes 2421556Srgrimes 2431556SrgrimesSTATIC void 24490111Simprestartjob(struct job *jp) 24517987Speter{ 2461556Srgrimes struct procstat *ps; 2471556Srgrimes int i; 2481556Srgrimes 2491556Srgrimes if (jp->state == JOBDONE) 2501556Srgrimes return; 25197660Stjr setcurjob(jp); 2521556Srgrimes INTOFF; 2531556Srgrimes killpg(jp->ps[0].pid, SIGCONT); 2541556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 25526104Ssteve if (WIFSTOPPED(ps->status)) { 2561556Srgrimes ps->status = -1; 2571556Srgrimes jp->state = 0; 2581556Srgrimes } 2591556Srgrimes } 2601556Srgrimes INTON; 2611556Srgrimes} 2621556Srgrimes#endif 2631556Srgrimes 2641556Srgrimes 2651556Srgrimesint 26697669Stjrjobscmd(int argc, char *argv[]) 26717987Speter{ 26897669Stjr char *id; 269163085Sstefanf int ch, mode; 27097669Stjr 27197669Stjr optind = optreset = 1; 272100663Stjr opterr = 0; 273163085Sstefanf mode = SHOWJOBS_DEFAULT; 274163085Sstefanf while ((ch = getopt(argc, argv, "lps")) != -1) { 27597669Stjr switch (ch) { 27697669Stjr case 'l': 277163085Sstefanf mode = SHOWJOBS_VERBOSE; 27897669Stjr break; 279163085Sstefanf case 'p': 280163085Sstefanf mode = SHOWJOBS_PGIDS; 281163085Sstefanf break; 28297669Stjr case 's': 283163085Sstefanf mode = SHOWJOBS_PIDS; 28497669Stjr break; 28597669Stjr case '?': 28697669Stjr default: 28797669Stjr error("unknown option: -%c", optopt); 28897669Stjr } 28997669Stjr } 29097669Stjr argc -= optind; 29197669Stjr argv += optind; 29297669Stjr 29397669Stjr if (argc == 0) 294163085Sstefanf showjobs(0, mode); 29597669Stjr else 29697669Stjr while ((id = *argv++) != NULL) 297163085Sstefanf showjob(getjob(id), 0, mode); 29897669Stjr 29997669Stjr return (0); 3001556Srgrimes} 3011556Srgrimes 30297663StjrSTATIC void 303163085Sstefanfshowjob(struct job *jp, pid_t pid, int mode) 30497663Stjr{ 30597663Stjr char s[64]; 30697663Stjr struct procstat *ps; 30797669Stjr struct job *j; 30897669Stjr int col, curr, i, jobno, prev, procno; 309163085Sstefanf pid_t ppid; 31097669Stjr char c; 3111556Srgrimes 312163085Sstefanf procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs; 31397663Stjr jobno = jp - jobtab + 1; 31497669Stjr curr = prev = 0; 31597669Stjr#if JOBS 31697669Stjr if ((j = getcurjob(NULL)) != NULL) { 31797669Stjr curr = j - jobtab + 1; 31897669Stjr if ((j = getcurjob(j)) != NULL) 31997669Stjr prev = j - jobtab + 1; 32097669Stjr } 32197669Stjr#endif 32297663Stjr for (ps = jp->ps ; ; ps++) { /* for each process */ 323163085Sstefanf if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) { 324163085Sstefanf ppid = (mode == SHOWJOBS_PIDS) ? ps->pid : 325163085Sstefanf getpgid(ps->pid); 326163085Sstefanf out1fmt("%d\n", (int)ppid); 32797669Stjr goto skip; 32897669Stjr } 329163085Sstefanf if (mode != SHOWJOBS_VERBOSE && ps != jp->ps && pid == 0) 33097669Stjr goto skip; 33197822Stjr if (pid != 0 && pid != ps->pid) 33297822Stjr goto skip; 33397819Stjr if (jobno == curr && ps == jp->ps) 33497669Stjr c = '+'; 33597819Stjr else if (jobno == prev && ps == jp->ps) 33697669Stjr c = '-'; 33797669Stjr else 33897669Stjr c = ' '; 33997663Stjr if (ps == jp->ps) 34097669Stjr fmtstr(s, 64, "[%d] %c ", jobno, c); 34197663Stjr else 34297816Stjr fmtstr(s, 64, " %c ", c); 34397663Stjr out1str(s); 34497663Stjr col = strlen(s); 345163085Sstefanf if (mode == SHOWJOBS_VERBOSE) { 346100308Stjr fmtstr(s, 64, "%d ", (int)ps->pid); 34797669Stjr out1str(s); 34897669Stjr col += strlen(s); 34997669Stjr } 35097663Stjr s[0] = '\0'; 35197819Stjr if (ps != jp->ps) { 35297819Stjr *s = '\0'; 35397819Stjr } else if (ps->status == -1) { 35497669Stjr strcpy(s, "Running"); 35597663Stjr } else if (WIFEXITED(ps->status)) { 35697820Stjr if (WEXITSTATUS(ps->status) == 0) 35797820Stjr strcpy(s, "Done"); 35897820Stjr else 35997820Stjr fmtstr(s, 64, "Done (%d)", 36097820Stjr WEXITSTATUS(ps->status)); 36197663Stjr } else { 36297663Stjr#if JOBS 363155301Sschweikh if (WIFSTOPPED(ps->status)) 36497663Stjr i = WSTOPSIG(ps->status); 36597663Stjr else 36697663Stjr#endif 36797663Stjr i = WTERMSIG(ps->status); 368125155Snjl if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F]) 36997663Stjr scopy(sys_siglist[i & 0x7F], s); 37097663Stjr else 37197663Stjr fmtstr(s, 64, "Signal %d", i & 0x7F); 37297663Stjr if (WCOREDUMP(ps->status)) 37397663Stjr strcat(s, " (core dumped)"); 37497663Stjr } 37597663Stjr out1str(s); 37697663Stjr col += strlen(s); 37797663Stjr do { 37897663Stjr out1c(' '); 37997663Stjr col++; 38097663Stjr } while (col < 30); 38197663Stjr out1str(ps->cmd); 38297663Stjr out1c('\n'); 38397669Stjrskip: if (--procno <= 0) 38497663Stjr break; 38597663Stjr } 38697663Stjr} 38797663Stjr 3881556Srgrimes/* 3891556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 3901556Srgrimes * statuses have changed since the last call to showjobs. 3911556Srgrimes * 3921556Srgrimes * If the shell is interrupted in the process of creating a job, the 3931556Srgrimes * result may be a job structure containing zero processes. Such structures 3941556Srgrimes * will be freed here. 3951556Srgrimes */ 3961556Srgrimes 3971556Srgrimesvoid 398163085Sstefanfshowjobs(int change, int mode) 39917987Speter{ 4001556Srgrimes int jobno; 4011556Srgrimes struct job *jp; 4021556Srgrimes 4031556Srgrimes TRACE(("showjobs(%d) called\n", change)); 404208489Sjilles checkzombies(); 4051556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 4061556Srgrimes if (! jp->used) 4071556Srgrimes continue; 4081556Srgrimes if (jp->nprocs == 0) { 4091556Srgrimes freejob(jp); 4101556Srgrimes continue; 4111556Srgrimes } 4121556Srgrimes if (change && ! jp->changed) 4131556Srgrimes continue; 414163085Sstefanf showjob(jp, 0, mode); 4151556Srgrimes jp->changed = 0; 4161556Srgrimes if (jp->state == JOBDONE) { 4171556Srgrimes freejob(jp); 4181556Srgrimes } 4191556Srgrimes } 4201556Srgrimes} 4211556Srgrimes 4221556Srgrimes 4231556Srgrimes/* 4241556Srgrimes * Mark a job structure as unused. 4251556Srgrimes */ 4261556Srgrimes 4271556SrgrimesSTATIC void 42890111Simpfreejob(struct job *jp) 42990111Simp{ 4301556Srgrimes struct procstat *ps; 4311556Srgrimes int i; 4321556Srgrimes 4331556Srgrimes INTOFF; 4341556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 4351556Srgrimes if (ps->cmd != nullstr) 4361556Srgrimes ckfree(ps->cmd); 4371556Srgrimes } 4381556Srgrimes if (jp->ps != &jp->ps0) 4391556Srgrimes ckfree(jp->ps); 4401556Srgrimes jp->used = 0; 4411556Srgrimes#if JOBS 44297659Stjr deljob(jp); 4431556Srgrimes#endif 4441556Srgrimes INTON; 4451556Srgrimes} 4461556Srgrimes 4471556Srgrimes 4481556Srgrimes 4491556Srgrimesint 45090111Simpwaitcmd(int argc, char **argv) 45117987Speter{ 4521556Srgrimes struct job *job; 45326104Ssteve int status, retval; 4541556Srgrimes struct job *jp; 4551556Srgrimes 4561556Srgrimes if (argc > 1) { 4571556Srgrimes job = getjob(argv[1]); 4581556Srgrimes } else { 4591556Srgrimes job = NULL; 4601556Srgrimes } 46138536Scracauer 46238536Scracauer /* 46338536Scracauer * Loop until a process is terminated or stopped, or a SIGINT is 46438536Scracauer * received. 46538536Scracauer */ 46638536Scracauer 46738521Scracauer in_waitcmd++; 46838536Scracauer do { 4691556Srgrimes if (job != NULL) { 4701556Srgrimes if (job->state) { 4711556Srgrimes status = job->ps[job->nprocs - 1].status; 47226104Ssteve if (WIFEXITED(status)) 47326104Ssteve retval = WEXITSTATUS(status); 4741556Srgrimes#if JOBS 47526104Ssteve else if (WIFSTOPPED(status)) 47626104Ssteve retval = WSTOPSIG(status) + 128; 4771556Srgrimes#endif 4781556Srgrimes else 47926104Ssteve retval = WTERMSIG(status) + 128; 4801556Srgrimes if (! iflag) 4811556Srgrimes freejob(job); 48238536Scracauer in_waitcmd--; 48326104Ssteve return retval; 4841556Srgrimes } 4851556Srgrimes } else { 4861556Srgrimes for (jp = jobtab ; ; jp++) { 4871556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 48838536Scracauer in_waitcmd--; 4891556Srgrimes return 0; 4901556Srgrimes } 4911556Srgrimes if (jp->used && jp->state == 0) 4921556Srgrimes break; 4931556Srgrimes } 4941556Srgrimes } 49538521Scracauer } while (dowait(1, (struct job *)NULL) != -1); 49638521Scracauer in_waitcmd--; 49738521Scracauer 49838521Scracauer return 0; 4991556Srgrimes} 5001556Srgrimes 5011556Srgrimes 5021556Srgrimes 50317987Speterint 50490111Simpjobidcmd(int argc __unused, char **argv) 50517987Speter{ 5061556Srgrimes struct job *jp; 5071556Srgrimes int i; 5081556Srgrimes 5091556Srgrimes jp = getjob(argv[1]); 5101556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 511100308Stjr out1fmt("%d", (int)jp->ps[i].pid); 5121556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 5131556Srgrimes } 5141556Srgrimes return 0; 5151556Srgrimes} 5161556Srgrimes 5171556Srgrimes 5181556Srgrimes 5191556Srgrimes/* 5201556Srgrimes * Convert a job name to a job structure. 5211556Srgrimes */ 5221556Srgrimes 5231556SrgrimesSTATIC struct job * 52490111Simpgetjob(char *name) 52590111Simp{ 5261556Srgrimes int jobno; 52797688Stjr struct job *found, *jp; 528100308Stjr pid_t pid; 5291556Srgrimes int i; 5301556Srgrimes 5311556Srgrimes if (name == NULL) { 5321556Srgrimes#if JOBS 53397659Stjrcurrentjob: if ((jp = getcurjob(NULL)) == NULL) 5341556Srgrimes error("No current job"); 53597659Stjr return (jp); 5361556Srgrimes#else 5371556Srgrimes error("No current job"); 5381556Srgrimes#endif 5391556Srgrimes } else if (name[0] == '%') { 5401556Srgrimes if (is_digit(name[1])) { 5411556Srgrimes jobno = number(name + 1); 5421556Srgrimes if (jobno > 0 && jobno <= njobs 5431556Srgrimes && jobtab[jobno - 1].used != 0) 5441556Srgrimes return &jobtab[jobno - 1]; 5451556Srgrimes#if JOBS 5461556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 5471556Srgrimes goto currentjob; 54897688Stjr } else if (name[1] == '+' && name[2] == '\0') { 54997688Stjr goto currentjob; 55097688Stjr } else if (name[1] == '-' && name[2] == '\0') { 55197688Stjr if ((jp = getcurjob(NULL)) == NULL || 55297688Stjr (jp = getcurjob(jp)) == NULL) 55397688Stjr error("No previous job"); 55497688Stjr return (jp); 5551556Srgrimes#endif 55697688Stjr } else if (name[1] == '?') { 55797688Stjr found = NULL; 55897688Stjr for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 55997688Stjr if (jp->used && jp->nprocs > 0 56097688Stjr && strstr(jp->ps[0].cmd, name + 2) != NULL) { 56197688Stjr if (found) 56297688Stjr error("%s: ambiguous", name); 56397688Stjr found = jp; 56497688Stjr } 56597688Stjr } 56697688Stjr if (found != NULL) 56797688Stjr return (found); 5681556Srgrimes } else { 56997688Stjr found = NULL; 5701556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5711556Srgrimes if (jp->used && jp->nprocs > 0 5721556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 5731556Srgrimes if (found) 5741556Srgrimes error("%s: ambiguous", name); 5751556Srgrimes found = jp; 5761556Srgrimes } 5771556Srgrimes } 5781556Srgrimes if (found) 5791556Srgrimes return found; 5801556Srgrimes } 5811556Srgrimes } else if (is_number(name)) { 582100308Stjr pid = (pid_t)number(name); 5831556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5841556Srgrimes if (jp->used && jp->nprocs > 0 5851556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 5861556Srgrimes return jp; 5871556Srgrimes } 5881556Srgrimes } 5891556Srgrimes error("No such job: %s", name); 59017987Speter /*NOTREACHED*/ 59117987Speter return NULL; 5921556Srgrimes} 5931556Srgrimes 5941556Srgrimes 5951556Srgrimes 5961556Srgrimes/* 5971556Srgrimes * Return a new job structure, 5981556Srgrimes */ 5991556Srgrimes 6001556Srgrimesstruct job * 60190111Simpmakejob(union node *node __unused, int nprocs) 60217987Speter{ 6031556Srgrimes int i; 6041556Srgrimes struct job *jp; 6051556Srgrimes 6061556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 6071556Srgrimes if (--i < 0) { 6081556Srgrimes INTOFF; 6091556Srgrimes if (njobs == 0) { 6101556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 61197664Stjr#if JOBS 61297659Stjr jobmru = NULL; 61397664Stjr#endif 6141556Srgrimes } else { 6151556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 61617987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 61797659Stjr#if JOBS 61897659Stjr /* Relocate `next' pointers and list head */ 61999760Stjr if (jobmru != NULL) 62099760Stjr 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; 646100305Stjr jp->foreground = 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; 657149802Sstefanf TRACE(("makejob(%p, %d) returns %%%d\n", (void *)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 737100308Stjrpid_t 73890111Simpforkshell(struct job *jp, union node *n, int mode) 73917987Speter{ 740100308Stjr pid_t pid; 741100308Stjr pid_t pgrp; 7421556Srgrimes 743149802Sstefanf TRACE(("forkshell(%%%d, %p, %d) called\n", jp - jobtab, (void *)n, 74417987Speter mode)); 7451556Srgrimes INTOFF; 746208489Sjilles if (mode == FORK_BG) 747208489Sjilles checkzombies(); 748112341Stjr flushall(); 7491556Srgrimes pid = fork(); 7501556Srgrimes if (pid == -1) { 7511556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 7521556Srgrimes INTON; 75353891Scracauer error("Cannot fork: %s", strerror(errno)); 7541556Srgrimes } 7551556Srgrimes if (pid == 0) { 7561556Srgrimes struct job *p; 7571556Srgrimes int wasroot; 7581556Srgrimes int i; 7591556Srgrimes 760100308Stjr TRACE(("Child shell %d\n", (int)getpid())); 7611556Srgrimes wasroot = rootshell; 7621556Srgrimes rootshell = 0; 763200998Sjilles handler = &main_handler; 7641556Srgrimes closescript(); 7651556Srgrimes INTON; 7661556Srgrimes clear_traps(); 7671556Srgrimes#if JOBS 7681556Srgrimes jobctl = 0; /* do job control only in root shell */ 7691556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 7701556Srgrimes if (jp == NULL || jp->nprocs == 0) 7711556Srgrimes pgrp = getpid(); 7721556Srgrimes else 7731556Srgrimes pgrp = jp->ps[0].pid; 77421352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 7751556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 77699762Stjr if (tcsetpgrp(ttyfd, pgrp) < 0) 77720425Ssteve error("tcsetpgrp failed, errno=%d", errno); 7781556Srgrimes } 7791556Srgrimes setsignal(SIGTSTP); 7801556Srgrimes setsignal(SIGTTOU); 7811556Srgrimes } else if (mode == FORK_BG) { 7821556Srgrimes ignoresig(SIGINT); 7831556Srgrimes ignoresig(SIGQUIT); 7841556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7851556Srgrimes ! fd0_redirected_p ()) { 7861556Srgrimes close(0); 78769793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 78869793Sobrien error("Can't open %s: %s", 78969793Sobrien _PATH_DEVNULL, strerror(errno)); 7901556Srgrimes } 7911556Srgrimes } 7921556Srgrimes#else 7931556Srgrimes if (mode == FORK_BG) { 7941556Srgrimes ignoresig(SIGINT); 7951556Srgrimes ignoresig(SIGQUIT); 7961556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7971556Srgrimes ! fd0_redirected_p ()) { 7981556Srgrimes close(0); 79969793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 800155301Sschweikh error("Can't open %s: %s", 80169793Sobrien _PATH_DEVNULL, strerror(errno)); 8021556Srgrimes } 8031556Srgrimes } 8041556Srgrimes#endif 805102051Stjr INTOFF; 806102051Stjr for (i = njobs, p = jobtab ; --i >= 0 ; p++) 807102051Stjr if (p->used) 808102051Stjr freejob(p); 809102051Stjr INTON; 8101556Srgrimes if (wasroot && iflag) { 8111556Srgrimes setsignal(SIGINT); 8121556Srgrimes setsignal(SIGQUIT); 8131556Srgrimes setsignal(SIGTERM); 8141556Srgrimes } 8151556Srgrimes return pid; 8161556Srgrimes } 8171556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 8181556Srgrimes if (jp == NULL || jp->nprocs == 0) 8191556Srgrimes pgrp = pid; 8201556Srgrimes else 8211556Srgrimes pgrp = jp->ps[0].pid; 82217987Speter setpgid(pid, pgrp); 8231556Srgrimes } 8241556Srgrimes if (mode == FORK_BG) 8251556Srgrimes backgndpid = pid; /* set $! */ 8261556Srgrimes if (jp) { 8271556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 8281556Srgrimes ps->pid = pid; 8291556Srgrimes ps->status = -1; 8301556Srgrimes ps->cmd = nullstr; 8311556Srgrimes if (iflag && rootshell && n) 8321556Srgrimes ps->cmd = commandtext(n); 833100305Stjr jp->foreground = mode == FORK_FG; 83497659Stjr#if JOBS 83597659Stjr setcurjob(jp); 83697659Stjr#endif 8371556Srgrimes } 8381556Srgrimes INTON; 839100308Stjr TRACE(("In parent shell: child = %d\n", (int)pid)); 8401556Srgrimes return pid; 8411556Srgrimes} 8421556Srgrimes 8431556Srgrimes 8441556Srgrimes 8451556Srgrimes/* 8461556Srgrimes * Wait for job to finish. 8471556Srgrimes * 8481556Srgrimes * Under job control we have the problem that while a child process is 8491556Srgrimes * running interrupts generated by the user are sent to the child but not 8501556Srgrimes * to the shell. This means that an infinite loop started by an inter- 8511556Srgrimes * active user may be hard to kill. With job control turned off, an 8521556Srgrimes * interactive user may place an interactive program inside a loop. If 8531556Srgrimes * the interactive program catches interrupts, the user doesn't want 8541556Srgrimes * these interrupts to also abort the loop. The approach we take here 8551556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 85646684Skris * foreground process to terminate, and then send itself an interrupt 8571556Srgrimes * signal if the child process was terminated by an interrupt signal. 8581556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 8591556Srgrimes * exit on interrupt; unless these processes terminate themselves by 8601556Srgrimes * sending a signal to themselves (instead of calling exit) they will 8611556Srgrimes * confuse this approach. 8621556Srgrimes */ 8631556Srgrimes 8641556Srgrimesint 86590111Simpwaitforjob(struct job *jp, int *origstatus) 86645916Scracauer{ 8671556Srgrimes#if JOBS 868100308Stjr pid_t mypgrp = getpgrp(); 869208881Sjilles int propagate_int = jp->jobctl && jp->foreground; 8701556Srgrimes#endif 8711556Srgrimes int status; 8721556Srgrimes int st; 8731556Srgrimes 8741556Srgrimes INTOFF; 8751556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 87638950Scracauer while (jp->state == 0) 87738950Scracauer if (dowait(1, jp) == -1) 87838950Scracauer dotrap(); 8791556Srgrimes#if JOBS 8801556Srgrimes if (jp->jobctl) { 88199762Stjr if (tcsetpgrp(ttyfd, mypgrp) < 0) 88220425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 8831556Srgrimes } 8841556Srgrimes if (jp->state == JOBSTOPPED) 88597659Stjr setcurjob(jp); 8861556Srgrimes#endif 8871556Srgrimes status = jp->ps[jp->nprocs - 1].status; 88845916Scracauer if (origstatus != NULL) 88945916Scracauer *origstatus = status; 8901556Srgrimes /* convert to 8 bits */ 89126104Ssteve if (WIFEXITED(status)) 89226104Ssteve st = WEXITSTATUS(status); 8931556Srgrimes#if JOBS 89426104Ssteve else if (WIFSTOPPED(status)) 89526104Ssteve st = WSTOPSIG(status) + 128; 8961556Srgrimes#endif 8971556Srgrimes else 89826104Ssteve st = WTERMSIG(status) + 128; 8991556Srgrimes if (! JOBS || jp->state == JOBDONE) 9001556Srgrimes freejob(jp); 90138521Scracauer if (int_pending()) { 90238521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 90338521Scracauer kill(getpid(), SIGINT); 90438521Scracauer else 90538521Scracauer CLEAR_PENDING_INT; 90638521Scracauer } 907208881Sjilles#if JOBS 908208881Sjilles else if (rootshell && iflag && propagate_int && 909208881Sjilles WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 910208881Sjilles kill(getpid(), SIGINT); 911208881Sjilles#endif 9121556Srgrimes INTON; 9131556Srgrimes return st; 9141556Srgrimes} 9151556Srgrimes 9161556Srgrimes 9171556Srgrimes 9181556Srgrimes/* 9191556Srgrimes * Wait for a process to terminate. 9201556Srgrimes */ 9211556Srgrimes 922100308StjrSTATIC pid_t 92390111Simpdowait(int block, struct job *job) 92417987Speter{ 925100308Stjr pid_t pid; 9261556Srgrimes int status; 9271556Srgrimes struct procstat *sp; 9281556Srgrimes struct job *jp; 9291556Srgrimes struct job *thisjob; 9301556Srgrimes int done; 9311556Srgrimes int stopped; 93226104Ssteve int sig; 933100305Stjr int i; 9341556Srgrimes 93538950Scracauer in_dowait++; 9361556Srgrimes TRACE(("dowait(%d) called\n", block)); 9371556Srgrimes do { 9381556Srgrimes pid = waitproc(block, &status); 939100308Stjr TRACE(("wait returns %d, status=%d\n", (int)pid, status)); 94072086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 941125501Scracauer (pid > 0 && WIFSTOPPED(status) && !iflag)); 94238950Scracauer in_dowait--; 943153417Smaxim if (pid == -1 && errno == ECHILD && job != NULL) 944153417Smaxim job->state = JOBDONE; 94538521Scracauer if (breakwaitcmd != 0) { 94638521Scracauer breakwaitcmd = 0; 947138312Smaxim if (pid <= 0) 948138312Smaxim return -1; 94938521Scracauer } 9501556Srgrimes if (pid <= 0) 9511556Srgrimes return pid; 9521556Srgrimes INTOFF; 9531556Srgrimes thisjob = NULL; 9541556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 9551556Srgrimes if (jp->used) { 9561556Srgrimes done = 1; 9571556Srgrimes stopped = 1; 9581556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 9591556Srgrimes if (sp->pid == -1) 9601556Srgrimes continue; 9611556Srgrimes if (sp->pid == pid) { 96226104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 963100308Stjr (int)pid, sp->status, 964100308Stjr status)); 9651556Srgrimes sp->status = status; 9661556Srgrimes thisjob = jp; 9671556Srgrimes } 9681556Srgrimes if (sp->status == -1) 9691556Srgrimes stopped = 0; 97026104Ssteve else if (WIFSTOPPED(sp->status)) 9711556Srgrimes done = 0; 9721556Srgrimes } 9731556Srgrimes if (stopped) { /* stopped or done */ 9741556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 9751556Srgrimes if (jp->state != state) { 9761556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 9771556Srgrimes jp->state = state; 9781556Srgrimes#if JOBS 97997659Stjr if (done) 98097659Stjr deljob(jp); 9811556Srgrimes#endif 9821556Srgrimes } 9831556Srgrimes } 9841556Srgrimes } 9851556Srgrimes } 9861556Srgrimes INTON; 9871556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 9881556Srgrimes#if JOBS 98926104Ssteve if (WIFSTOPPED(status)) 99026104Ssteve sig = WSTOPSIG(status); 99126104Ssteve else 9921556Srgrimes#endif 99397663Stjr { 99426104Ssteve if (WIFEXITED(status)) 99526104Ssteve sig = 0; 99626104Ssteve else 99726104Ssteve sig = WTERMSIG(status); 9981556Srgrimes } 999100305Stjr if (sig != 0 && sig != SIGINT && sig != SIGPIPE) { 1000107846Stjr if (!mflag || 1001107846Stjr (thisjob->foreground && !WIFSTOPPED(status))) { 1002102007Stjr i = WTERMSIG(status); 1003125155Snjl if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F]) 1004100305Stjr out1str(sys_siglist[i & 0x7F]); 1005100305Stjr else 1006100305Stjr out1fmt("Signal %d", i & 0x7F); 1007100305Stjr if (WCOREDUMP(status)) 1008100305Stjr out1str(" (core dumped)"); 1009100305Stjr out1c('\n'); 1010100305Stjr } else 1011163085Sstefanf showjob(thisjob, pid, SHOWJOBS_DEFAULT); 1012100305Stjr } 10131556Srgrimes } else { 1014109627Stjr TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job)); 10151556Srgrimes if (thisjob) 10161556Srgrimes thisjob->changed = 1; 10171556Srgrimes } 10181556Srgrimes return pid; 10191556Srgrimes} 10201556Srgrimes 10211556Srgrimes 10221556Srgrimes 10231556Srgrimes/* 10241556Srgrimes * Do a wait system call. If job control is compiled in, we accept 10251556Srgrimes * stopped processes. If block is zero, we return a value of zero 10261556Srgrimes * rather than blocking. 10271556Srgrimes */ 1028100308StjrSTATIC pid_t 102990111Simpwaitproc(int block, int *status) 103017987Speter{ 10311556Srgrimes int flags; 10321556Srgrimes 10331556Srgrimes#if JOBS 10341556Srgrimes flags = WUNTRACED; 10351556Srgrimes#else 10361556Srgrimes flags = 0; 10371556Srgrimes#endif 10381556Srgrimes if (block == 0) 10391556Srgrimes flags |= WNOHANG; 10401556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 10411556Srgrimes} 10421556Srgrimes 10431556Srgrimes/* 10441556Srgrimes * return 1 if there are stopped jobs, otherwise 0 10451556Srgrimes */ 10461556Srgrimesint job_warning = 0; 10471556Srgrimesint 104890111Simpstoppedjobs(void) 10491556Srgrimes{ 105025222Ssteve int jobno; 105125222Ssteve struct job *jp; 10521556Srgrimes 10531556Srgrimes if (job_warning) 10541556Srgrimes return (0); 10551556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 10561556Srgrimes if (jp->used == 0) 10571556Srgrimes continue; 10581556Srgrimes if (jp->state == JOBSTOPPED) { 1059199629Sjilles out2fmt_flush("You have stopped jobs.\n"); 10601556Srgrimes job_warning = 2; 10611556Srgrimes return (1); 10621556Srgrimes } 10631556Srgrimes } 10641556Srgrimes 10651556Srgrimes return (0); 10661556Srgrimes} 10671556Srgrimes 1068208489Sjilles 1069208489SjillesSTATIC void 1070208489Sjillescheckzombies(void) 1071208489Sjilles{ 1072208489Sjilles while (njobs > 0 && dowait(0, NULL) > 0) 1073208489Sjilles ; 1074208489Sjilles} 1075208489Sjilles 1076208489Sjilles 10771556Srgrimes/* 10781556Srgrimes * Return a string identifying a command (to be printed by the 10791556Srgrimes * jobs command. 10801556Srgrimes */ 10811556Srgrimes 10821556SrgrimesSTATIC char *cmdnextc; 10831556SrgrimesSTATIC int cmdnleft; 10841556Srgrimes#define MAXCMDTEXT 200 10851556Srgrimes 10861556Srgrimeschar * 108790111Simpcommandtext(union node *n) 108890111Simp{ 10891556Srgrimes char *name; 10901556Srgrimes 10911556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 10921556Srgrimes cmdnleft = MAXCMDTEXT - 4; 10931556Srgrimes cmdtxt(n); 10941556Srgrimes *cmdnextc = '\0'; 10951556Srgrimes return name; 10961556Srgrimes} 10971556Srgrimes 10981556Srgrimes 10991556SrgrimesSTATIC void 110090111Simpcmdtxt(union node *n) 110190111Simp{ 11021556Srgrimes union node *np; 11031556Srgrimes struct nodelist *lp; 1104201053Sjilles const char *p; 11051556Srgrimes int i; 11061556Srgrimes char s[2]; 11071556Srgrimes 11081556Srgrimes if (n == NULL) 11091556Srgrimes return; 11101556Srgrimes switch (n->type) { 11111556Srgrimes case NSEMI: 11121556Srgrimes cmdtxt(n->nbinary.ch1); 11131556Srgrimes cmdputs("; "); 11141556Srgrimes cmdtxt(n->nbinary.ch2); 11151556Srgrimes break; 11161556Srgrimes case NAND: 11171556Srgrimes cmdtxt(n->nbinary.ch1); 11181556Srgrimes cmdputs(" && "); 11191556Srgrimes cmdtxt(n->nbinary.ch2); 11201556Srgrimes break; 11211556Srgrimes case NOR: 11221556Srgrimes cmdtxt(n->nbinary.ch1); 11231556Srgrimes cmdputs(" || "); 11241556Srgrimes cmdtxt(n->nbinary.ch2); 11251556Srgrimes break; 11261556Srgrimes case NPIPE: 11271556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 11281556Srgrimes cmdtxt(lp->n); 11291556Srgrimes if (lp->next) 11301556Srgrimes cmdputs(" | "); 11311556Srgrimes } 11321556Srgrimes break; 11331556Srgrimes case NSUBSHELL: 11341556Srgrimes cmdputs("("); 11351556Srgrimes cmdtxt(n->nredir.n); 11361556Srgrimes cmdputs(")"); 11371556Srgrimes break; 11381556Srgrimes case NREDIR: 11391556Srgrimes case NBACKGND: 11401556Srgrimes cmdtxt(n->nredir.n); 11411556Srgrimes break; 11421556Srgrimes case NIF: 11431556Srgrimes cmdputs("if "); 11441556Srgrimes cmdtxt(n->nif.test); 11451556Srgrimes cmdputs("; then "); 11461556Srgrimes cmdtxt(n->nif.ifpart); 11471556Srgrimes cmdputs("..."); 11481556Srgrimes break; 11491556Srgrimes case NWHILE: 11501556Srgrimes cmdputs("while "); 11511556Srgrimes goto until; 11521556Srgrimes case NUNTIL: 11531556Srgrimes cmdputs("until "); 11541556Srgrimesuntil: 11551556Srgrimes cmdtxt(n->nbinary.ch1); 11561556Srgrimes cmdputs("; do "); 11571556Srgrimes cmdtxt(n->nbinary.ch2); 11581556Srgrimes cmdputs("; done"); 11591556Srgrimes break; 11601556Srgrimes case NFOR: 11611556Srgrimes cmdputs("for "); 11621556Srgrimes cmdputs(n->nfor.var); 11631556Srgrimes cmdputs(" in ..."); 11641556Srgrimes break; 11651556Srgrimes case NCASE: 11661556Srgrimes cmdputs("case "); 11671556Srgrimes cmdputs(n->ncase.expr->narg.text); 11681556Srgrimes cmdputs(" in ..."); 11691556Srgrimes break; 11701556Srgrimes case NDEFUN: 11711556Srgrimes cmdputs(n->narg.text); 11721556Srgrimes cmdputs("() ..."); 11731556Srgrimes break; 11741556Srgrimes case NCMD: 11751556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 11761556Srgrimes cmdtxt(np); 11771556Srgrimes if (np->narg.next) 11781556Srgrimes cmdputs(" "); 11791556Srgrimes } 11801556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 11811556Srgrimes cmdputs(" "); 11821556Srgrimes cmdtxt(np); 11831556Srgrimes } 11841556Srgrimes break; 11851556Srgrimes case NARG: 11861556Srgrimes cmdputs(n->narg.text); 11871556Srgrimes break; 11881556Srgrimes case NTO: 11891556Srgrimes p = ">"; i = 1; goto redir; 11901556Srgrimes case NAPPEND: 11911556Srgrimes p = ">>"; i = 1; goto redir; 11921556Srgrimes case NTOFD: 11931556Srgrimes p = ">&"; i = 1; goto redir; 119496922Stjr case NCLOBBER: 119596922Stjr p = ">|"; i = 1; goto redir; 11961556Srgrimes case NFROM: 11971556Srgrimes p = "<"; i = 0; goto redir; 119866612Sbrian case NFROMTO: 119966612Sbrian p = "<>"; i = 0; goto redir; 12001556Srgrimes case NFROMFD: 12011556Srgrimes p = "<&"; i = 0; goto redir; 12021556Srgrimesredir: 12031556Srgrimes if (n->nfile.fd != i) { 12041556Srgrimes s[0] = n->nfile.fd + '0'; 12051556Srgrimes s[1] = '\0'; 12061556Srgrimes cmdputs(s); 12071556Srgrimes } 12081556Srgrimes cmdputs(p); 12091556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 121099634Stjr if (n->ndup.dupfd >= 0) 121199634Stjr s[0] = n->ndup.dupfd + '0'; 121299634Stjr else 121399634Stjr s[0] = '-'; 12141556Srgrimes s[1] = '\0'; 12151556Srgrimes cmdputs(s); 12161556Srgrimes } else { 12171556Srgrimes cmdtxt(n->nfile.fname); 12181556Srgrimes } 12191556Srgrimes break; 12201556Srgrimes case NHERE: 12211556Srgrimes case NXHERE: 12221556Srgrimes cmdputs("<<..."); 12231556Srgrimes break; 12241556Srgrimes default: 12251556Srgrimes cmdputs("???"); 12261556Srgrimes break; 12271556Srgrimes } 12281556Srgrimes} 12291556Srgrimes 12301556Srgrimes 12311556Srgrimes 12321556SrgrimesSTATIC void 1233201053Sjillescmdputs(const char *s) 123490111Simp{ 1235201053Sjilles const char *p; 1236201053Sjilles char *q; 123725222Ssteve char c; 12381556Srgrimes int subtype = 0; 12391556Srgrimes 12401556Srgrimes if (cmdnleft <= 0) 12411556Srgrimes return; 12421556Srgrimes p = s; 12431556Srgrimes q = cmdnextc; 12441556Srgrimes while ((c = *p++) != '\0') { 12451556Srgrimes if (c == CTLESC) 12461556Srgrimes *q++ = *p++; 12471556Srgrimes else if (c == CTLVAR) { 12481556Srgrimes *q++ = '$'; 12491556Srgrimes if (--cmdnleft > 0) 12501556Srgrimes *q++ = '{'; 12511556Srgrimes subtype = *p++; 12521556Srgrimes } else if (c == '=' && subtype != 0) { 12531556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 12541556Srgrimes subtype = 0; 12551556Srgrimes } else if (c == CTLENDVAR) { 12561556Srgrimes *q++ = '}'; 125718954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 12581556Srgrimes cmdnleft++; /* ignore it */ 12591556Srgrimes else 12601556Srgrimes *q++ = c; 12611556Srgrimes if (--cmdnleft <= 0) { 12621556Srgrimes *q++ = '.'; 12631556Srgrimes *q++ = '.'; 12641556Srgrimes *q++ = '.'; 12651556Srgrimes break; 12661556Srgrimes } 12671556Srgrimes } 12681556Srgrimes cmdnextc = q; 12691556Srgrimes} 1270