jobs.c revision 153417
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 153417 2005-12-14 17:26:29Z maxim $"); 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 *); 9490111SimpSTATIC void cmdtxt(union node *); 9590111SimpSTATIC void cmdputs(char *); 9697659Stjr#if JOBS 9797659StjrSTATIC void setcurjob(struct job *); 9897659StjrSTATIC void deljob(struct job *); 9997659StjrSTATIC struct job *getcurjob(struct job *); 10097659Stjr#endif 10197822StjrSTATIC void showjob(struct job *, pid_t, int, int); 1021556Srgrimes 1031556Srgrimes 1041556Srgrimes/* 1051556Srgrimes * Turn job control on and off. 1061556Srgrimes */ 1071556Srgrimes 1081556SrgrimesMKINIT int jobctl; 1091556Srgrimes 11020425Ssteve#if JOBS 1111556Srgrimesvoid 11290111Simpsetjobctl(int on) 11317987Speter{ 11499762Stjr int i; 1151556Srgrimes 1161556Srgrimes if (on == jobctl || rootshell == 0) 1171556Srgrimes return; 1181556Srgrimes if (on) { 11999762Stjr if (ttyfd != -1) 12099762Stjr close(ttyfd); 12199762Stjr if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) { 12299762Stjr i = 0; 12399762Stjr while (i <= 2 && !isatty(i)) 12499762Stjr i++; 125109927Stjr if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0) 12699762Stjr goto out; 12799762Stjr } 128109927Stjr if (ttyfd < 10) { 129109927Stjr /* 130109927Stjr * Keep our TTY file descriptor out of the way of 131109927Stjr * the user's redirections. 132109927Stjr */ 133109927Stjr if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) { 134109927Stjr close(ttyfd); 135109927Stjr ttyfd = -1; 136109927Stjr goto out; 137109927Stjr } 138109927Stjr close(ttyfd); 139109927Stjr ttyfd = i; 140109927Stjr } 141103223Snectar if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) { 14299762Stjr close(ttyfd); 14399762Stjr ttyfd = -1; 14499762Stjr goto out; 14599762Stjr } 1461556Srgrimes do { /* while we are in the background */ 14799762Stjr initialpgrp = tcgetpgrp(ttyfd); 14820425Ssteve if (initialpgrp < 0) { 14999762Stjrout: out2str("sh: can't access tty; job control turned off\n"); 1501556Srgrimes mflag = 0; 1511556Srgrimes return; 1521556Srgrimes } 1531556Srgrimes if (initialpgrp == -1) 15417987Speter initialpgrp = getpgrp(); 15517987Speter else if (initialpgrp != getpgrp()) { 15699762Stjr killpg(0, SIGTTIN); 1571556Srgrimes continue; 1581556Srgrimes } 1591556Srgrimes } while (0); 1601556Srgrimes setsignal(SIGTSTP); 1611556Srgrimes setsignal(SIGTTOU); 1621556Srgrimes setsignal(SIGTTIN); 16317987Speter setpgid(0, rootpid); 16499762Stjr tcsetpgrp(ttyfd, rootpid); 1651556Srgrimes } else { /* turning job control off */ 16617987Speter setpgid(0, initialpgrp); 16799762Stjr tcsetpgrp(ttyfd, initialpgrp); 16899762Stjr close(ttyfd); 16999762Stjr ttyfd = -1; 1701556Srgrimes setsignal(SIGTSTP); 1711556Srgrimes setsignal(SIGTTOU); 1721556Srgrimes setsignal(SIGTTIN); 1731556Srgrimes } 1741556Srgrimes jobctl = on; 1751556Srgrimes} 17620425Ssteve#endif 1771556Srgrimes 1781556Srgrimes 1791556Srgrimes#ifdef mkinit 18028346SsteveINCLUDE <sys/types.h> 18117987SpeterINCLUDE <stdlib.h> 1821556Srgrimes 1831556SrgrimesSHELLPROC { 1841556Srgrimes backgndpid = -1; 1851556Srgrimes#if JOBS 1861556Srgrimes jobctl = 0; 1871556Srgrimes#endif 1881556Srgrimes} 1891556Srgrimes 1901556Srgrimes#endif 1911556Srgrimes 1921556Srgrimes 1931556Srgrimes 1941556Srgrimes#if JOBS 19517987Speterint 19690111Simpfgcmd(int argc __unused, char **argv) 19717987Speter{ 1981556Srgrimes struct job *jp; 199100308Stjr pid_t pgrp; 2001556Srgrimes int status; 2011556Srgrimes 2021556Srgrimes jp = getjob(argv[1]); 2031556Srgrimes if (jp->jobctl == 0) 2041556Srgrimes error("job not created under job control"); 20596933Stjr out1str(jp->ps[0].cmd); 20696933Stjr out1c('\n'); 20796933Stjr flushout(&output); 2081556Srgrimes pgrp = jp->ps[0].pid; 20999762Stjr tcsetpgrp(ttyfd, pgrp); 2101556Srgrimes restartjob(jp); 211100305Stjr jp->foreground = 1; 2121556Srgrimes INTOFF; 21345916Scracauer status = waitforjob(jp, (int *)NULL); 2141556Srgrimes INTON; 2151556Srgrimes return status; 2161556Srgrimes} 2171556Srgrimes 2181556Srgrimes 21917987Speterint 22090111Simpbgcmd(int argc, char **argv) 22117987Speter{ 22296933Stjr char s[64]; 2231556Srgrimes struct job *jp; 2241556Srgrimes 2251556Srgrimes do { 2261556Srgrimes jp = getjob(*++argv); 2271556Srgrimes if (jp->jobctl == 0) 2281556Srgrimes error("job not created under job control"); 22996933Stjr if (jp->state == JOBDONE) 23096933Stjr continue; 2311556Srgrimes restartjob(jp); 232100305Stjr jp->foreground = 0; 233104275Smux fmtstr(s, 64, "[%td] ", jp - jobtab + 1); 23496933Stjr out1str(s); 23596933Stjr out1str(jp->ps[0].cmd); 23696933Stjr out1c('\n'); 2371556Srgrimes } while (--argc > 1); 2381556Srgrimes return 0; 2391556Srgrimes} 2401556Srgrimes 2411556Srgrimes 2421556SrgrimesSTATIC void 24390111Simprestartjob(struct job *jp) 24417987Speter{ 2451556Srgrimes struct procstat *ps; 2461556Srgrimes int i; 2471556Srgrimes 2481556Srgrimes if (jp->state == JOBDONE) 2491556Srgrimes return; 25097660Stjr setcurjob(jp); 2511556Srgrimes INTOFF; 2521556Srgrimes killpg(jp->ps[0].pid, SIGCONT); 2531556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 25426104Ssteve if (WIFSTOPPED(ps->status)) { 2551556Srgrimes ps->status = -1; 2561556Srgrimes jp->state = 0; 2571556Srgrimes } 2581556Srgrimes } 2591556Srgrimes INTON; 2601556Srgrimes} 2611556Srgrimes#endif 2621556Srgrimes 2631556Srgrimes 2641556Srgrimesint 26597669Stjrjobscmd(int argc, char *argv[]) 26617987Speter{ 26797669Stjr char *id; 26897669Stjr int ch, sformat, lformat; 26997669Stjr 27097669Stjr optind = optreset = 1; 271100663Stjr opterr = 0; 27297669Stjr sformat = lformat = 0; 27397669Stjr while ((ch = getopt(argc, argv, "ls")) != -1) { 27497669Stjr switch (ch) { 27597669Stjr case 'l': 27697669Stjr lformat = 1; 27797669Stjr break; 27897669Stjr case 's': 27997669Stjr sformat = 1; 28097669Stjr break; 28197669Stjr case '?': 28297669Stjr default: 28397669Stjr error("unknown option: -%c", optopt); 28497669Stjr } 28597669Stjr } 28697669Stjr argc -= optind; 28797669Stjr argv += optind; 28897669Stjr 28997669Stjr if (argc == 0) 29097669Stjr showjobs(0, sformat, lformat); 29197669Stjr else 29297669Stjr while ((id = *argv++) != NULL) 29397822Stjr showjob(getjob(id), 0, sformat, lformat); 29497669Stjr 29597669Stjr return (0); 2961556Srgrimes} 2971556Srgrimes 29897663StjrSTATIC void 29997822Stjrshowjob(struct job *jp, pid_t pid, int sformat, int lformat) 30097663Stjr{ 30197663Stjr char s[64]; 30297663Stjr struct procstat *ps; 30397669Stjr struct job *j; 30497669Stjr int col, curr, i, jobno, prev, procno; 30597669Stjr char c; 3061556Srgrimes 30797663Stjr procno = jp->nprocs; 30897663Stjr jobno = jp - jobtab + 1; 30997669Stjr curr = prev = 0; 31097669Stjr#if JOBS 31197669Stjr if ((j = getcurjob(NULL)) != NULL) { 31297669Stjr curr = j - jobtab + 1; 31397669Stjr if ((j = getcurjob(j)) != NULL) 31497669Stjr prev = j - jobtab + 1; 31597669Stjr } 31697669Stjr#endif 31797663Stjr for (ps = jp->ps ; ; ps++) { /* for each process */ 31897669Stjr if (sformat) { 319100308Stjr out1fmt("%d\n", (int)ps->pid); 32097669Stjr goto skip; 32197669Stjr } 32297822Stjr if (!lformat && ps != jp->ps && pid == 0) 32397669Stjr goto skip; 32497822Stjr if (pid != 0 && pid != ps->pid) 32597822Stjr goto skip; 32697819Stjr if (jobno == curr && ps == jp->ps) 32797669Stjr c = '+'; 32897819Stjr else if (jobno == prev && ps == jp->ps) 32997669Stjr c = '-'; 33097669Stjr else 33197669Stjr c = ' '; 33297663Stjr if (ps == jp->ps) 33397669Stjr fmtstr(s, 64, "[%d] %c ", jobno, c); 33497663Stjr else 33597816Stjr fmtstr(s, 64, " %c ", c); 33697663Stjr out1str(s); 33797663Stjr col = strlen(s); 33897669Stjr if (lformat) { 339100308Stjr fmtstr(s, 64, "%d ", (int)ps->pid); 34097669Stjr out1str(s); 34197669Stjr col += strlen(s); 34297669Stjr } 34397663Stjr s[0] = '\0'; 34497819Stjr if (ps != jp->ps) { 34597819Stjr *s = '\0'; 34697819Stjr } else if (ps->status == -1) { 34797669Stjr strcpy(s, "Running"); 34897663Stjr } else if (WIFEXITED(ps->status)) { 34997820Stjr if (WEXITSTATUS(ps->status) == 0) 35097820Stjr strcpy(s, "Done"); 35197820Stjr else 35297820Stjr fmtstr(s, 64, "Done (%d)", 35397820Stjr WEXITSTATUS(ps->status)); 35497663Stjr } else { 35597663Stjr#if JOBS 35697663Stjr if (WIFSTOPPED(ps->status)) 35797663Stjr i = WSTOPSIG(ps->status); 35897663Stjr else 35997663Stjr#endif 36097663Stjr i = WTERMSIG(ps->status); 361125155Snjl if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F]) 36297663Stjr scopy(sys_siglist[i & 0x7F], s); 36397663Stjr else 36497663Stjr fmtstr(s, 64, "Signal %d", i & 0x7F); 36597663Stjr if (WCOREDUMP(ps->status)) 36697663Stjr strcat(s, " (core dumped)"); 36797663Stjr } 36897663Stjr out1str(s); 36997663Stjr col += strlen(s); 37097663Stjr do { 37197663Stjr out1c(' '); 37297663Stjr col++; 37397663Stjr } while (col < 30); 37497663Stjr out1str(ps->cmd); 37597663Stjr out1c('\n'); 37697669Stjrskip: if (--procno <= 0) 37797663Stjr break; 37897663Stjr } 37997663Stjr} 38097663Stjr 3811556Srgrimes/* 3821556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 3831556Srgrimes * statuses have changed since the last call to showjobs. 3841556Srgrimes * 3851556Srgrimes * If the shell is interrupted in the process of creating a job, the 3861556Srgrimes * result may be a job structure containing zero processes. Such structures 3871556Srgrimes * will be freed here. 3881556Srgrimes */ 3891556Srgrimes 3901556Srgrimesvoid 39197669Stjrshowjobs(int change, int sformat, int lformat) 39217987Speter{ 3931556Srgrimes int jobno; 3941556Srgrimes struct job *jp; 3951556Srgrimes 3961556Srgrimes TRACE(("showjobs(%d) called\n", change)); 3971556Srgrimes while (dowait(0, (struct job *)NULL) > 0); 3981556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 3991556Srgrimes if (! jp->used) 4001556Srgrimes continue; 4011556Srgrimes if (jp->nprocs == 0) { 4021556Srgrimes freejob(jp); 4031556Srgrimes continue; 4041556Srgrimes } 4051556Srgrimes if (change && ! jp->changed) 4061556Srgrimes continue; 40797822Stjr showjob(jp, 0, sformat, lformat); 4081556Srgrimes jp->changed = 0; 4091556Srgrimes if (jp->state == JOBDONE) { 4101556Srgrimes freejob(jp); 4111556Srgrimes } 4121556Srgrimes } 4131556Srgrimes} 4141556Srgrimes 4151556Srgrimes 4161556Srgrimes/* 4171556Srgrimes * Mark a job structure as unused. 4181556Srgrimes */ 4191556Srgrimes 4201556SrgrimesSTATIC void 42190111Simpfreejob(struct job *jp) 42290111Simp{ 4231556Srgrimes struct procstat *ps; 4241556Srgrimes int i; 4251556Srgrimes 4261556Srgrimes INTOFF; 4271556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 4281556Srgrimes if (ps->cmd != nullstr) 4291556Srgrimes ckfree(ps->cmd); 4301556Srgrimes } 4311556Srgrimes if (jp->ps != &jp->ps0) 4321556Srgrimes ckfree(jp->ps); 4331556Srgrimes jp->used = 0; 4341556Srgrimes#if JOBS 43597659Stjr deljob(jp); 4361556Srgrimes#endif 4371556Srgrimes INTON; 4381556Srgrimes} 4391556Srgrimes 4401556Srgrimes 4411556Srgrimes 4421556Srgrimesint 44390111Simpwaitcmd(int argc, char **argv) 44417987Speter{ 4451556Srgrimes struct job *job; 44626104Ssteve int status, retval; 4471556Srgrimes struct job *jp; 4481556Srgrimes 4491556Srgrimes if (argc > 1) { 4501556Srgrimes job = getjob(argv[1]); 4511556Srgrimes } else { 4521556Srgrimes job = NULL; 4531556Srgrimes } 45438536Scracauer 45538536Scracauer /* 45638536Scracauer * Loop until a process is terminated or stopped, or a SIGINT is 45738536Scracauer * received. 45838536Scracauer */ 45938536Scracauer 46038521Scracauer in_waitcmd++; 46138536Scracauer do { 4621556Srgrimes if (job != NULL) { 4631556Srgrimes if (job->state) { 4641556Srgrimes status = job->ps[job->nprocs - 1].status; 46526104Ssteve if (WIFEXITED(status)) 46626104Ssteve retval = WEXITSTATUS(status); 4671556Srgrimes#if JOBS 46826104Ssteve else if (WIFSTOPPED(status)) 46926104Ssteve retval = WSTOPSIG(status) + 128; 4701556Srgrimes#endif 4711556Srgrimes else 47226104Ssteve retval = WTERMSIG(status) + 128; 4731556Srgrimes if (! iflag) 4741556Srgrimes freejob(job); 47538536Scracauer in_waitcmd--; 47626104Ssteve return retval; 4771556Srgrimes } 4781556Srgrimes } else { 4791556Srgrimes for (jp = jobtab ; ; jp++) { 4801556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 48138536Scracauer in_waitcmd--; 4821556Srgrimes return 0; 4831556Srgrimes } 4841556Srgrimes if (jp->used && jp->state == 0) 4851556Srgrimes break; 4861556Srgrimes } 4871556Srgrimes } 48838521Scracauer } while (dowait(1, (struct job *)NULL) != -1); 48938521Scracauer in_waitcmd--; 49038521Scracauer 49138521Scracauer return 0; 4921556Srgrimes} 4931556Srgrimes 4941556Srgrimes 4951556Srgrimes 49617987Speterint 49790111Simpjobidcmd(int argc __unused, char **argv) 49817987Speter{ 4991556Srgrimes struct job *jp; 5001556Srgrimes int i; 5011556Srgrimes 5021556Srgrimes jp = getjob(argv[1]); 5031556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 504100308Stjr out1fmt("%d", (int)jp->ps[i].pid); 5051556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 5061556Srgrimes } 5071556Srgrimes return 0; 5081556Srgrimes} 5091556Srgrimes 5101556Srgrimes 5111556Srgrimes 5121556Srgrimes/* 5131556Srgrimes * Convert a job name to a job structure. 5141556Srgrimes */ 5151556Srgrimes 5161556SrgrimesSTATIC struct job * 51790111Simpgetjob(char *name) 51890111Simp{ 5191556Srgrimes int jobno; 52097688Stjr struct job *found, *jp; 521100308Stjr pid_t pid; 5221556Srgrimes int i; 5231556Srgrimes 5241556Srgrimes if (name == NULL) { 5251556Srgrimes#if JOBS 52697659Stjrcurrentjob: if ((jp = getcurjob(NULL)) == NULL) 5271556Srgrimes error("No current job"); 52897659Stjr return (jp); 5291556Srgrimes#else 5301556Srgrimes error("No current job"); 5311556Srgrimes#endif 5321556Srgrimes } else if (name[0] == '%') { 5331556Srgrimes if (is_digit(name[1])) { 5341556Srgrimes jobno = number(name + 1); 5351556Srgrimes if (jobno > 0 && jobno <= njobs 5361556Srgrimes && jobtab[jobno - 1].used != 0) 5371556Srgrimes return &jobtab[jobno - 1]; 5381556Srgrimes#if JOBS 5391556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 5401556Srgrimes goto currentjob; 54197688Stjr } else if (name[1] == '+' && name[2] == '\0') { 54297688Stjr goto currentjob; 54397688Stjr } else if (name[1] == '-' && name[2] == '\0') { 54497688Stjr if ((jp = getcurjob(NULL)) == NULL || 54597688Stjr (jp = getcurjob(jp)) == NULL) 54697688Stjr error("No previous job"); 54797688Stjr return (jp); 5481556Srgrimes#endif 54997688Stjr } else if (name[1] == '?') { 55097688Stjr found = NULL; 55197688Stjr for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 55297688Stjr if (jp->used && jp->nprocs > 0 55397688Stjr && strstr(jp->ps[0].cmd, name + 2) != NULL) { 55497688Stjr if (found) 55597688Stjr error("%s: ambiguous", name); 55697688Stjr found = jp; 55797688Stjr } 55897688Stjr } 55997688Stjr if (found != NULL) 56097688Stjr return (found); 5611556Srgrimes } else { 56297688Stjr found = NULL; 5631556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5641556Srgrimes if (jp->used && jp->nprocs > 0 5651556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 5661556Srgrimes if (found) 5671556Srgrimes error("%s: ambiguous", name); 5681556Srgrimes found = jp; 5691556Srgrimes } 5701556Srgrimes } 5711556Srgrimes if (found) 5721556Srgrimes return found; 5731556Srgrimes } 5741556Srgrimes } else if (is_number(name)) { 575100308Stjr pid = (pid_t)number(name); 5761556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5771556Srgrimes if (jp->used && jp->nprocs > 0 5781556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 5791556Srgrimes return jp; 5801556Srgrimes } 5811556Srgrimes } 5821556Srgrimes error("No such job: %s", name); 58317987Speter /*NOTREACHED*/ 58417987Speter return NULL; 5851556Srgrimes} 5861556Srgrimes 5871556Srgrimes 5881556Srgrimes 5891556Srgrimes/* 5901556Srgrimes * Return a new job structure, 5911556Srgrimes */ 5921556Srgrimes 5931556Srgrimesstruct job * 59490111Simpmakejob(union node *node __unused, int nprocs) 59517987Speter{ 5961556Srgrimes int i; 5971556Srgrimes struct job *jp; 5981556Srgrimes 5991556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 6001556Srgrimes if (--i < 0) { 6011556Srgrimes INTOFF; 6021556Srgrimes if (njobs == 0) { 6031556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 60497664Stjr#if JOBS 60597659Stjr jobmru = NULL; 60697664Stjr#endif 6071556Srgrimes } else { 6081556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 60917987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 61097659Stjr#if JOBS 61197659Stjr /* Relocate `next' pointers and list head */ 61299760Stjr if (jobmru != NULL) 61399760Stjr jobmru = &jp[jobmru - jobtab]; 61497659Stjr for (i = 0; i < njobs; i++) 61597659Stjr if (jp[i].next != NULL) 61697659Stjr jp[i].next = &jp[jp[i].next - 61797659Stjr jobtab]; 61897659Stjr#endif 61920425Ssteve /* Relocate `ps' pointers */ 62020425Ssteve for (i = 0; i < njobs; i++) 62120425Ssteve if (jp[i].ps == &jobtab[i].ps0) 62220425Ssteve jp[i].ps = &jp[i].ps0; 6231556Srgrimes ckfree(jobtab); 6241556Srgrimes jobtab = jp; 6251556Srgrimes } 6261556Srgrimes jp = jobtab + njobs; 6271556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 6281556Srgrimes INTON; 6291556Srgrimes break; 6301556Srgrimes } 6311556Srgrimes if (jp->used == 0) 6321556Srgrimes break; 6331556Srgrimes } 6341556Srgrimes INTOFF; 6351556Srgrimes jp->state = 0; 6361556Srgrimes jp->used = 1; 6371556Srgrimes jp->changed = 0; 6381556Srgrimes jp->nprocs = 0; 639100305Stjr jp->foreground = 0; 6401556Srgrimes#if JOBS 6411556Srgrimes jp->jobctl = jobctl; 64297659Stjr jp->next = NULL; 6431556Srgrimes#endif 6441556Srgrimes if (nprocs > 1) { 6451556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 6461556Srgrimes } else { 6471556Srgrimes jp->ps = &jp->ps0; 6481556Srgrimes } 6491556Srgrimes INTON; 650149802Sstefanf TRACE(("makejob(%p, %d) returns %%%d\n", (void *)node, nprocs, 65117987Speter jp - jobtab + 1)); 6521556Srgrimes return jp; 6538855Srgrimes} 6541556Srgrimes 65597659Stjr#if JOBS 65697659StjrSTATIC void 65797659Stjrsetcurjob(struct job *cj) 65897659Stjr{ 65997659Stjr struct job *jp, *prev; 6601556Srgrimes 66197659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 66297659Stjr if (jp == cj) { 66397659Stjr if (prev != NULL) 66497659Stjr prev->next = jp->next; 66597659Stjr else 66697659Stjr jobmru = jp->next; 66797659Stjr jp->next = jobmru; 66897659Stjr jobmru = cj; 66997659Stjr return; 67097659Stjr } 67197659Stjr } 67297659Stjr cj->next = jobmru; 67397659Stjr jobmru = cj; 67497659Stjr} 67597659Stjr 67697659StjrSTATIC void 67797659Stjrdeljob(struct job *j) 67897659Stjr{ 67997659Stjr struct job *jp, *prev; 68097659Stjr 68197659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 68297659Stjr if (jp == j) { 68397659Stjr if (prev != NULL) 68497659Stjr prev->next = jp->next; 68597659Stjr else 68697659Stjr jobmru = jp->next; 68797659Stjr return; 68897659Stjr } 68997659Stjr } 69097659Stjr} 69197659Stjr 6921556Srgrimes/* 69397659Stjr * Return the most recently used job that isn't `nj', and preferably one 69497659Stjr * that is stopped. 69597659Stjr */ 69697659StjrSTATIC struct job * 69797659Stjrgetcurjob(struct job *nj) 69897659Stjr{ 69997659Stjr struct job *jp; 70097659Stjr 70197659Stjr /* Try to find a stopped one.. */ 70297659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 70397659Stjr if (jp->used && jp != nj && jp->state == JOBSTOPPED) 70497659Stjr return (jp); 70597659Stjr /* Otherwise the most recently used job that isn't `nj' */ 70697659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 70797659Stjr if (jp->used && jp != nj) 70897659Stjr return (jp); 70997659Stjr 71097659Stjr return (NULL); 71197659Stjr} 71297659Stjr 71397659Stjr#endif 71497659Stjr 71597659Stjr/* 7161556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 7171556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 7181556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 7191556Srgrimes * be NULL. The mode parameter can be one of the following: 7201556Srgrimes * FORK_FG - Fork off a foreground process. 7211556Srgrimes * FORK_BG - Fork off a background process. 7221556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 7231556Srgrimes * process group even if job control is on. 7241556Srgrimes * 7251556Srgrimes * When job control is turned off, background processes have their standard 7261556Srgrimes * input redirected to /dev/null (except for the second and later processes 7271556Srgrimes * in a pipeline). 7281556Srgrimes */ 7291556Srgrimes 730100308Stjrpid_t 73190111Simpforkshell(struct job *jp, union node *n, int mode) 73217987Speter{ 733100308Stjr pid_t pid; 734100308Stjr pid_t pgrp; 7351556Srgrimes 736149802Sstefanf TRACE(("forkshell(%%%d, %p, %d) called\n", jp - jobtab, (void *)n, 73717987Speter mode)); 7381556Srgrimes INTOFF; 739112341Stjr flushall(); 7401556Srgrimes pid = fork(); 7411556Srgrimes if (pid == -1) { 7421556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 7431556Srgrimes INTON; 74453891Scracauer error("Cannot fork: %s", strerror(errno)); 7451556Srgrimes } 7461556Srgrimes if (pid == 0) { 7471556Srgrimes struct job *p; 7481556Srgrimes int wasroot; 7491556Srgrimes int i; 7501556Srgrimes 751100308Stjr TRACE(("Child shell %d\n", (int)getpid())); 7521556Srgrimes wasroot = rootshell; 7531556Srgrimes rootshell = 0; 7541556Srgrimes closescript(); 7551556Srgrimes INTON; 7561556Srgrimes clear_traps(); 7571556Srgrimes#if JOBS 7581556Srgrimes jobctl = 0; /* do job control only in root shell */ 7591556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 7601556Srgrimes if (jp == NULL || jp->nprocs == 0) 7611556Srgrimes pgrp = getpid(); 7621556Srgrimes else 7631556Srgrimes pgrp = jp->ps[0].pid; 76421352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 7651556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 76699762Stjr if (tcsetpgrp(ttyfd, pgrp) < 0) 76720425Ssteve error("tcsetpgrp failed, errno=%d", errno); 7681556Srgrimes } 7691556Srgrimes setsignal(SIGTSTP); 7701556Srgrimes setsignal(SIGTTOU); 7711556Srgrimes } else if (mode == FORK_BG) { 7721556Srgrimes ignoresig(SIGINT); 7731556Srgrimes ignoresig(SIGQUIT); 7741556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7751556Srgrimes ! fd0_redirected_p ()) { 7761556Srgrimes close(0); 77769793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 77869793Sobrien error("Can't open %s: %s", 77969793Sobrien _PATH_DEVNULL, strerror(errno)); 7801556Srgrimes } 7811556Srgrimes } 7821556Srgrimes#else 7831556Srgrimes if (mode == FORK_BG) { 7841556Srgrimes ignoresig(SIGINT); 7851556Srgrimes ignoresig(SIGQUIT); 7861556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7871556Srgrimes ! fd0_redirected_p ()) { 7881556Srgrimes close(0); 78969793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 79069793Sobrien error("Can't open %s: %s", 79169793Sobrien _PATH_DEVNULL, strerror(errno)); 7921556Srgrimes } 7931556Srgrimes } 7941556Srgrimes#endif 795102051Stjr INTOFF; 796102051Stjr for (i = njobs, p = jobtab ; --i >= 0 ; p++) 797102051Stjr if (p->used) 798102051Stjr freejob(p); 799102051Stjr INTON; 8001556Srgrimes if (wasroot && iflag) { 8011556Srgrimes setsignal(SIGINT); 8021556Srgrimes setsignal(SIGQUIT); 8031556Srgrimes setsignal(SIGTERM); 8041556Srgrimes } 8051556Srgrimes return pid; 8061556Srgrimes } 8071556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 8081556Srgrimes if (jp == NULL || jp->nprocs == 0) 8091556Srgrimes pgrp = pid; 8101556Srgrimes else 8111556Srgrimes pgrp = jp->ps[0].pid; 81217987Speter setpgid(pid, pgrp); 8131556Srgrimes } 8141556Srgrimes if (mode == FORK_BG) 8151556Srgrimes backgndpid = pid; /* set $! */ 8161556Srgrimes if (jp) { 8171556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 8181556Srgrimes ps->pid = pid; 8191556Srgrimes ps->status = -1; 8201556Srgrimes ps->cmd = nullstr; 8211556Srgrimes if (iflag && rootshell && n) 8221556Srgrimes ps->cmd = commandtext(n); 823100305Stjr jp->foreground = mode == FORK_FG; 82497659Stjr#if JOBS 82597659Stjr setcurjob(jp); 82697659Stjr#endif 8271556Srgrimes } 8281556Srgrimes INTON; 829100308Stjr TRACE(("In parent shell: child = %d\n", (int)pid)); 8301556Srgrimes return pid; 8311556Srgrimes} 8321556Srgrimes 8331556Srgrimes 8341556Srgrimes 8351556Srgrimes/* 8361556Srgrimes * Wait for job to finish. 8371556Srgrimes * 8381556Srgrimes * Under job control we have the problem that while a child process is 8391556Srgrimes * running interrupts generated by the user are sent to the child but not 8401556Srgrimes * to the shell. This means that an infinite loop started by an inter- 8411556Srgrimes * active user may be hard to kill. With job control turned off, an 8421556Srgrimes * interactive user may place an interactive program inside a loop. If 8431556Srgrimes * the interactive program catches interrupts, the user doesn't want 8441556Srgrimes * these interrupts to also abort the loop. The approach we take here 8451556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 84646684Skris * foreground process to terminate, and then send itself an interrupt 8471556Srgrimes * signal if the child process was terminated by an interrupt signal. 8481556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 8491556Srgrimes * exit on interrupt; unless these processes terminate themselves by 8501556Srgrimes * sending a signal to themselves (instead of calling exit) they will 8511556Srgrimes * confuse this approach. 8521556Srgrimes */ 8531556Srgrimes 8541556Srgrimesint 85590111Simpwaitforjob(struct job *jp, int *origstatus) 85645916Scracauer{ 8571556Srgrimes#if JOBS 858100308Stjr pid_t mypgrp = getpgrp(); 8591556Srgrimes#endif 8601556Srgrimes int status; 8611556Srgrimes int st; 8621556Srgrimes 8631556Srgrimes INTOFF; 8641556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 86538950Scracauer while (jp->state == 0) 86638950Scracauer if (dowait(1, jp) == -1) 86738950Scracauer dotrap(); 8681556Srgrimes#if JOBS 8691556Srgrimes if (jp->jobctl) { 87099762Stjr if (tcsetpgrp(ttyfd, mypgrp) < 0) 87120425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 8721556Srgrimes } 8731556Srgrimes if (jp->state == JOBSTOPPED) 87497659Stjr setcurjob(jp); 8751556Srgrimes#endif 8761556Srgrimes status = jp->ps[jp->nprocs - 1].status; 87745916Scracauer if (origstatus != NULL) 87845916Scracauer *origstatus = status; 8791556Srgrimes /* convert to 8 bits */ 88026104Ssteve if (WIFEXITED(status)) 88126104Ssteve st = WEXITSTATUS(status); 8821556Srgrimes#if JOBS 88326104Ssteve else if (WIFSTOPPED(status)) 88426104Ssteve st = WSTOPSIG(status) + 128; 8851556Srgrimes#endif 8861556Srgrimes else 88726104Ssteve st = WTERMSIG(status) + 128; 8881556Srgrimes if (! JOBS || jp->state == JOBDONE) 8891556Srgrimes freejob(jp); 89038521Scracauer if (int_pending()) { 89138521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 89238521Scracauer kill(getpid(), SIGINT); 89338521Scracauer else 89438521Scracauer CLEAR_PENDING_INT; 89538521Scracauer } 8961556Srgrimes INTON; 8971556Srgrimes return st; 8981556Srgrimes} 8991556Srgrimes 9001556Srgrimes 9011556Srgrimes 9021556Srgrimes/* 9031556Srgrimes * Wait for a process to terminate. 9041556Srgrimes */ 9051556Srgrimes 906100308StjrSTATIC pid_t 90790111Simpdowait(int block, struct job *job) 90817987Speter{ 909100308Stjr pid_t pid; 9101556Srgrimes int status; 9111556Srgrimes struct procstat *sp; 9121556Srgrimes struct job *jp; 9131556Srgrimes struct job *thisjob; 9141556Srgrimes int done; 9151556Srgrimes int stopped; 91626104Ssteve int sig; 917100305Stjr int i; 9181556Srgrimes 91938950Scracauer in_dowait++; 9201556Srgrimes TRACE(("dowait(%d) called\n", block)); 9211556Srgrimes do { 9221556Srgrimes pid = waitproc(block, &status); 923100308Stjr TRACE(("wait returns %d, status=%d\n", (int)pid, status)); 92472086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 925125501Scracauer (pid > 0 && WIFSTOPPED(status) && !iflag)); 92638950Scracauer in_dowait--; 927153417Smaxim if (pid == -1 && errno == ECHILD && job != NULL) 928153417Smaxim job->state = JOBDONE; 92938521Scracauer if (breakwaitcmd != 0) { 93038521Scracauer breakwaitcmd = 0; 931138312Smaxim if (pid <= 0) 932138312Smaxim return -1; 93338521Scracauer } 9341556Srgrimes if (pid <= 0) 9351556Srgrimes return pid; 9361556Srgrimes INTOFF; 9371556Srgrimes thisjob = NULL; 9381556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 9391556Srgrimes if (jp->used) { 9401556Srgrimes done = 1; 9411556Srgrimes stopped = 1; 9421556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 9431556Srgrimes if (sp->pid == -1) 9441556Srgrimes continue; 9451556Srgrimes if (sp->pid == pid) { 94626104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 947100308Stjr (int)pid, sp->status, 948100308Stjr status)); 9491556Srgrimes sp->status = status; 9501556Srgrimes thisjob = jp; 9511556Srgrimes } 9521556Srgrimes if (sp->status == -1) 9531556Srgrimes stopped = 0; 95426104Ssteve else if (WIFSTOPPED(sp->status)) 9551556Srgrimes done = 0; 9561556Srgrimes } 9571556Srgrimes if (stopped) { /* stopped or done */ 9581556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 9591556Srgrimes if (jp->state != state) { 9601556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 9611556Srgrimes jp->state = state; 9621556Srgrimes#if JOBS 96397659Stjr if (done) 96497659Stjr deljob(jp); 9651556Srgrimes#endif 9661556Srgrimes } 9671556Srgrimes } 9681556Srgrimes } 9691556Srgrimes } 9701556Srgrimes INTON; 9711556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 9721556Srgrimes#if JOBS 97326104Ssteve if (WIFSTOPPED(status)) 97426104Ssteve sig = WSTOPSIG(status); 97526104Ssteve else 9761556Srgrimes#endif 97797663Stjr { 97826104Ssteve if (WIFEXITED(status)) 97926104Ssteve sig = 0; 98026104Ssteve else 98126104Ssteve sig = WTERMSIG(status); 9821556Srgrimes } 983100305Stjr if (sig != 0 && sig != SIGINT && sig != SIGPIPE) { 984107846Stjr if (!mflag || 985107846Stjr (thisjob->foreground && !WIFSTOPPED(status))) { 986102007Stjr i = WTERMSIG(status); 987125155Snjl if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F]) 988100305Stjr out1str(sys_siglist[i & 0x7F]); 989100305Stjr else 990100305Stjr out1fmt("Signal %d", i & 0x7F); 991100305Stjr if (WCOREDUMP(status)) 992100305Stjr out1str(" (core dumped)"); 993100305Stjr out1c('\n'); 994100305Stjr } else 995102351Stjr showjob(thisjob, pid, 0, 0); 996100305Stjr } 9971556Srgrimes } else { 998109627Stjr TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job)); 9991556Srgrimes if (thisjob) 10001556Srgrimes thisjob->changed = 1; 10011556Srgrimes } 10021556Srgrimes return pid; 10031556Srgrimes} 10041556Srgrimes 10051556Srgrimes 10061556Srgrimes 10071556Srgrimes/* 10081556Srgrimes * Do a wait system call. If job control is compiled in, we accept 10091556Srgrimes * stopped processes. If block is zero, we return a value of zero 10101556Srgrimes * rather than blocking. 10111556Srgrimes */ 1012100308StjrSTATIC pid_t 101390111Simpwaitproc(int block, int *status) 101417987Speter{ 10151556Srgrimes int flags; 10161556Srgrimes 10171556Srgrimes#if JOBS 10181556Srgrimes flags = WUNTRACED; 10191556Srgrimes#else 10201556Srgrimes flags = 0; 10211556Srgrimes#endif 10221556Srgrimes if (block == 0) 10231556Srgrimes flags |= WNOHANG; 10241556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 10251556Srgrimes} 10261556Srgrimes 10271556Srgrimes/* 10281556Srgrimes * return 1 if there are stopped jobs, otherwise 0 10291556Srgrimes */ 10301556Srgrimesint job_warning = 0; 10311556Srgrimesint 103290111Simpstoppedjobs(void) 10331556Srgrimes{ 103425222Ssteve int jobno; 103525222Ssteve struct job *jp; 10361556Srgrimes 10371556Srgrimes if (job_warning) 10381556Srgrimes return (0); 10391556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 10401556Srgrimes if (jp->used == 0) 10411556Srgrimes continue; 10421556Srgrimes if (jp->state == JOBSTOPPED) { 10431556Srgrimes out2str("You have stopped jobs.\n"); 10441556Srgrimes job_warning = 2; 10451556Srgrimes return (1); 10461556Srgrimes } 10471556Srgrimes } 10481556Srgrimes 10491556Srgrimes return (0); 10501556Srgrimes} 10511556Srgrimes 10521556Srgrimes/* 10531556Srgrimes * Return a string identifying a command (to be printed by the 10541556Srgrimes * jobs command. 10551556Srgrimes */ 10561556Srgrimes 10571556SrgrimesSTATIC char *cmdnextc; 10581556SrgrimesSTATIC int cmdnleft; 10591556Srgrimes#define MAXCMDTEXT 200 10601556Srgrimes 10611556Srgrimeschar * 106290111Simpcommandtext(union node *n) 106390111Simp{ 10641556Srgrimes char *name; 10651556Srgrimes 10661556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 10671556Srgrimes cmdnleft = MAXCMDTEXT - 4; 10681556Srgrimes cmdtxt(n); 10691556Srgrimes *cmdnextc = '\0'; 10701556Srgrimes return name; 10711556Srgrimes} 10721556Srgrimes 10731556Srgrimes 10741556SrgrimesSTATIC void 107590111Simpcmdtxt(union node *n) 107690111Simp{ 10771556Srgrimes union node *np; 10781556Srgrimes struct nodelist *lp; 10791556Srgrimes char *p; 10801556Srgrimes int i; 10811556Srgrimes char s[2]; 10821556Srgrimes 10831556Srgrimes if (n == NULL) 10841556Srgrimes return; 10851556Srgrimes switch (n->type) { 10861556Srgrimes case NSEMI: 10871556Srgrimes cmdtxt(n->nbinary.ch1); 10881556Srgrimes cmdputs("; "); 10891556Srgrimes cmdtxt(n->nbinary.ch2); 10901556Srgrimes break; 10911556Srgrimes case NAND: 10921556Srgrimes cmdtxt(n->nbinary.ch1); 10931556Srgrimes cmdputs(" && "); 10941556Srgrimes cmdtxt(n->nbinary.ch2); 10951556Srgrimes break; 10961556Srgrimes case NOR: 10971556Srgrimes cmdtxt(n->nbinary.ch1); 10981556Srgrimes cmdputs(" || "); 10991556Srgrimes cmdtxt(n->nbinary.ch2); 11001556Srgrimes break; 11011556Srgrimes case NPIPE: 11021556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 11031556Srgrimes cmdtxt(lp->n); 11041556Srgrimes if (lp->next) 11051556Srgrimes cmdputs(" | "); 11061556Srgrimes } 11071556Srgrimes break; 11081556Srgrimes case NSUBSHELL: 11091556Srgrimes cmdputs("("); 11101556Srgrimes cmdtxt(n->nredir.n); 11111556Srgrimes cmdputs(")"); 11121556Srgrimes break; 11131556Srgrimes case NREDIR: 11141556Srgrimes case NBACKGND: 11151556Srgrimes cmdtxt(n->nredir.n); 11161556Srgrimes break; 11171556Srgrimes case NIF: 11181556Srgrimes cmdputs("if "); 11191556Srgrimes cmdtxt(n->nif.test); 11201556Srgrimes cmdputs("; then "); 11211556Srgrimes cmdtxt(n->nif.ifpart); 11221556Srgrimes cmdputs("..."); 11231556Srgrimes break; 11241556Srgrimes case NWHILE: 11251556Srgrimes cmdputs("while "); 11261556Srgrimes goto until; 11271556Srgrimes case NUNTIL: 11281556Srgrimes cmdputs("until "); 11291556Srgrimesuntil: 11301556Srgrimes cmdtxt(n->nbinary.ch1); 11311556Srgrimes cmdputs("; do "); 11321556Srgrimes cmdtxt(n->nbinary.ch2); 11331556Srgrimes cmdputs("; done"); 11341556Srgrimes break; 11351556Srgrimes case NFOR: 11361556Srgrimes cmdputs("for "); 11371556Srgrimes cmdputs(n->nfor.var); 11381556Srgrimes cmdputs(" in ..."); 11391556Srgrimes break; 11401556Srgrimes case NCASE: 11411556Srgrimes cmdputs("case "); 11421556Srgrimes cmdputs(n->ncase.expr->narg.text); 11431556Srgrimes cmdputs(" in ..."); 11441556Srgrimes break; 11451556Srgrimes case NDEFUN: 11461556Srgrimes cmdputs(n->narg.text); 11471556Srgrimes cmdputs("() ..."); 11481556Srgrimes break; 11491556Srgrimes case NCMD: 11501556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 11511556Srgrimes cmdtxt(np); 11521556Srgrimes if (np->narg.next) 11531556Srgrimes cmdputs(" "); 11541556Srgrimes } 11551556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 11561556Srgrimes cmdputs(" "); 11571556Srgrimes cmdtxt(np); 11581556Srgrimes } 11591556Srgrimes break; 11601556Srgrimes case NARG: 11611556Srgrimes cmdputs(n->narg.text); 11621556Srgrimes break; 11631556Srgrimes case NTO: 11641556Srgrimes p = ">"; i = 1; goto redir; 11651556Srgrimes case NAPPEND: 11661556Srgrimes p = ">>"; i = 1; goto redir; 11671556Srgrimes case NTOFD: 11681556Srgrimes p = ">&"; i = 1; goto redir; 116996922Stjr case NCLOBBER: 117096922Stjr p = ">|"; i = 1; goto redir; 11711556Srgrimes case NFROM: 11721556Srgrimes p = "<"; i = 0; goto redir; 117366612Sbrian case NFROMTO: 117466612Sbrian p = "<>"; i = 0; goto redir; 11751556Srgrimes case NFROMFD: 11761556Srgrimes p = "<&"; i = 0; goto redir; 11771556Srgrimesredir: 11781556Srgrimes if (n->nfile.fd != i) { 11791556Srgrimes s[0] = n->nfile.fd + '0'; 11801556Srgrimes s[1] = '\0'; 11811556Srgrimes cmdputs(s); 11821556Srgrimes } 11831556Srgrimes cmdputs(p); 11841556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 118599634Stjr if (n->ndup.dupfd >= 0) 118699634Stjr s[0] = n->ndup.dupfd + '0'; 118799634Stjr else 118899634Stjr s[0] = '-'; 11891556Srgrimes s[1] = '\0'; 11901556Srgrimes cmdputs(s); 11911556Srgrimes } else { 11921556Srgrimes cmdtxt(n->nfile.fname); 11931556Srgrimes } 11941556Srgrimes break; 11951556Srgrimes case NHERE: 11961556Srgrimes case NXHERE: 11971556Srgrimes cmdputs("<<..."); 11981556Srgrimes break; 11991556Srgrimes default: 12001556Srgrimes cmdputs("???"); 12011556Srgrimes break; 12021556Srgrimes } 12031556Srgrimes} 12041556Srgrimes 12051556Srgrimes 12061556Srgrimes 12071556SrgrimesSTATIC void 120890111Simpcmdputs(char *s) 120990111Simp{ 121025222Ssteve char *p, *q; 121125222Ssteve char c; 12121556Srgrimes int subtype = 0; 12131556Srgrimes 12141556Srgrimes if (cmdnleft <= 0) 12151556Srgrimes return; 12161556Srgrimes p = s; 12171556Srgrimes q = cmdnextc; 12181556Srgrimes while ((c = *p++) != '\0') { 12191556Srgrimes if (c == CTLESC) 12201556Srgrimes *q++ = *p++; 12211556Srgrimes else if (c == CTLVAR) { 12221556Srgrimes *q++ = '$'; 12231556Srgrimes if (--cmdnleft > 0) 12241556Srgrimes *q++ = '{'; 12251556Srgrimes subtype = *p++; 12261556Srgrimes } else if (c == '=' && subtype != 0) { 12271556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 12281556Srgrimes subtype = 0; 12291556Srgrimes } else if (c == CTLENDVAR) { 12301556Srgrimes *q++ = '}'; 123118954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 12321556Srgrimes cmdnleft++; /* ignore it */ 12331556Srgrimes else 12341556Srgrimes *q++ = c; 12351556Srgrimes if (--cmdnleft <= 0) { 12361556Srgrimes *q++ = '.'; 12371556Srgrimes *q++ = '.'; 12381556Srgrimes *q++ = '.'; 12391556Srgrimes break; 12401556Srgrimes } 12411556Srgrimes } 12421556Srgrimes cmdnextc = q; 12431556Srgrimes} 1244