jobs.c revision 109927
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 109927 2003-01-27 07:41:12Z tjr $"); 441556Srgrimes 4517987Speter#include <fcntl.h> 4617987Speter#include <signal.h> 4717987Speter#include <errno.h> 4899762Stjr#include <paths.h> 4917987Speter#include <unistd.h> 5017987Speter#include <stdlib.h> 5117987Speter#include <sys/param.h> 5217987Speter#include <sys/wait.h> 5317987Speter#include <sys/time.h> 5417987Speter#include <sys/resource.h> 5569793Sobrien#include <paths.h> 5618018Speter#include <sys/ioctl.h> 5717987Speter 581556Srgrimes#include "shell.h" 591556Srgrimes#if JOBS 6017987Speter#include <termios.h> 611556Srgrimes#undef CEOF /* syntax.h redefines this */ 621556Srgrimes#endif 6317987Speter#include "redir.h" 6417987Speter#include "show.h" 651556Srgrimes#include "main.h" 661556Srgrimes#include "parser.h" 671556Srgrimes#include "nodes.h" 681556Srgrimes#include "jobs.h" 691556Srgrimes#include "options.h" 701556Srgrimes#include "trap.h" 711556Srgrimes#include "syntax.h" 721556Srgrimes#include "input.h" 731556Srgrimes#include "output.h" 741556Srgrimes#include "memalloc.h" 751556Srgrimes#include "error.h" 761556Srgrimes#include "mystring.h" 771556Srgrimes 781556Srgrimes 791556Srgrimesstruct job *jobtab; /* array of jobs */ 801556Srgrimesint njobs; /* size of array */ 8128346SsteveMKINIT pid_t backgndpid = -1; /* pid of last background process */ 821556Srgrimes#if JOBS 8397659Stjrstruct job *jobmru; /* most recently used job list */ 84100308Stjrpid_t initialpgrp; /* pgrp of shell on invocation */ 851556Srgrimes#endif 8638536Scracauerint in_waitcmd = 0; /* are we in waitcmd()? */ 8738950Scracauerint in_dowait = 0; /* are we in dowait()? */ 8838536Scracauervolatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */ 8999762Stjrstatic int ttyfd = -1; 901556Srgrimes 9120425Ssteve#if JOBS 9290111SimpSTATIC void restartjob(struct job *); 9320425Ssteve#endif 9490111SimpSTATIC void freejob(struct job *); 9590111SimpSTATIC struct job *getjob(char *); 96100308StjrSTATIC pid_t dowait(int, struct job *); 97100308StjrSTATIC pid_t waitproc(int, int *); 9890111SimpSTATIC void cmdtxt(union node *); 9990111SimpSTATIC void cmdputs(char *); 10097659Stjr#if JOBS 10197659StjrSTATIC void setcurjob(struct job *); 10297659StjrSTATIC void deljob(struct job *); 10397659StjrSTATIC struct job *getcurjob(struct job *); 10497659Stjr#endif 10597822StjrSTATIC void showjob(struct job *, pid_t, int, int); 1061556Srgrimes 1071556Srgrimes 1081556Srgrimes/* 1091556Srgrimes * Turn job control on and off. 1101556Srgrimes */ 1111556Srgrimes 1121556SrgrimesMKINIT int jobctl; 1131556Srgrimes 11420425Ssteve#if JOBS 1151556Srgrimesvoid 11690111Simpsetjobctl(int on) 11717987Speter{ 11899762Stjr int i; 1191556Srgrimes 1201556Srgrimes if (on == jobctl || rootshell == 0) 1211556Srgrimes return; 1221556Srgrimes if (on) { 12399762Stjr if (ttyfd != -1) 12499762Stjr close(ttyfd); 12599762Stjr if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) { 12699762Stjr i = 0; 12799762Stjr while (i <= 2 && !isatty(i)) 12899762Stjr i++; 129109927Stjr if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0) 13099762Stjr goto out; 13199762Stjr } 132109927Stjr if (ttyfd < 10) { 133109927Stjr /* 134109927Stjr * Keep our TTY file descriptor out of the way of 135109927Stjr * the user's redirections. 136109927Stjr */ 137109927Stjr if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) { 138109927Stjr close(ttyfd); 139109927Stjr ttyfd = -1; 140109927Stjr goto out; 141109927Stjr } 142109927Stjr close(ttyfd); 143109927Stjr ttyfd = i; 144109927Stjr } 145103223Snectar if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) { 14699762Stjr close(ttyfd); 14799762Stjr ttyfd = -1; 14899762Stjr goto out; 14999762Stjr } 1501556Srgrimes do { /* while we are in the background */ 15199762Stjr initialpgrp = tcgetpgrp(ttyfd); 15220425Ssteve if (initialpgrp < 0) { 15399762Stjrout: out2str("sh: can't access tty; job control turned off\n"); 1541556Srgrimes mflag = 0; 1551556Srgrimes return; 1561556Srgrimes } 1571556Srgrimes if (initialpgrp == -1) 15817987Speter initialpgrp = getpgrp(); 15917987Speter else if (initialpgrp != getpgrp()) { 16099762Stjr killpg(0, SIGTTIN); 1611556Srgrimes continue; 1621556Srgrimes } 1631556Srgrimes } while (0); 1641556Srgrimes setsignal(SIGTSTP); 1651556Srgrimes setsignal(SIGTTOU); 1661556Srgrimes setsignal(SIGTTIN); 16717987Speter setpgid(0, rootpid); 16899762Stjr tcsetpgrp(ttyfd, rootpid); 1691556Srgrimes } else { /* turning job control off */ 17017987Speter setpgid(0, initialpgrp); 17199762Stjr tcsetpgrp(ttyfd, initialpgrp); 17299762Stjr close(ttyfd); 17399762Stjr ttyfd = -1; 1741556Srgrimes setsignal(SIGTSTP); 1751556Srgrimes setsignal(SIGTTOU); 1761556Srgrimes setsignal(SIGTTIN); 1771556Srgrimes } 1781556Srgrimes jobctl = on; 1791556Srgrimes} 18020425Ssteve#endif 1811556Srgrimes 1821556Srgrimes 1831556Srgrimes#ifdef mkinit 18428346SsteveINCLUDE <sys/types.h> 18517987SpeterINCLUDE <stdlib.h> 1861556Srgrimes 1871556SrgrimesSHELLPROC { 1881556Srgrimes backgndpid = -1; 1891556Srgrimes#if JOBS 1901556Srgrimes jobctl = 0; 1911556Srgrimes#endif 1921556Srgrimes} 1931556Srgrimes 1941556Srgrimes#endif 1951556Srgrimes 1961556Srgrimes 1971556Srgrimes 1981556Srgrimes#if JOBS 19917987Speterint 20090111Simpfgcmd(int argc __unused, char **argv) 20117987Speter{ 2021556Srgrimes struct job *jp; 203100308Stjr pid_t pgrp; 2041556Srgrimes int status; 2051556Srgrimes 2061556Srgrimes jp = getjob(argv[1]); 2071556Srgrimes if (jp->jobctl == 0) 2081556Srgrimes error("job not created under job control"); 20996933Stjr out1str(jp->ps[0].cmd); 21096933Stjr out1c('\n'); 21196933Stjr flushout(&output); 2121556Srgrimes pgrp = jp->ps[0].pid; 21399762Stjr tcsetpgrp(ttyfd, pgrp); 2141556Srgrimes restartjob(jp); 215100305Stjr jp->foreground = 1; 2161556Srgrimes INTOFF; 21745916Scracauer status = waitforjob(jp, (int *)NULL); 2181556Srgrimes INTON; 2191556Srgrimes return status; 2201556Srgrimes} 2211556Srgrimes 2221556Srgrimes 22317987Speterint 22490111Simpbgcmd(int argc, char **argv) 22517987Speter{ 22696933Stjr char s[64]; 2271556Srgrimes struct job *jp; 2281556Srgrimes 2291556Srgrimes do { 2301556Srgrimes jp = getjob(*++argv); 2311556Srgrimes if (jp->jobctl == 0) 2321556Srgrimes error("job not created under job control"); 23396933Stjr if (jp->state == JOBDONE) 23496933Stjr continue; 2351556Srgrimes restartjob(jp); 236100305Stjr jp->foreground = 0; 237104275Smux fmtstr(s, 64, "[%td] ", jp - jobtab + 1); 23896933Stjr out1str(s); 23996933Stjr out1str(jp->ps[0].cmd); 24096933Stjr out1c('\n'); 2411556Srgrimes } while (--argc > 1); 2421556Srgrimes return 0; 2431556Srgrimes} 2441556Srgrimes 2451556Srgrimes 2461556SrgrimesSTATIC void 24790111Simprestartjob(struct job *jp) 24817987Speter{ 2491556Srgrimes struct procstat *ps; 2501556Srgrimes int i; 2511556Srgrimes 2521556Srgrimes if (jp->state == JOBDONE) 2531556Srgrimes return; 25497660Stjr setcurjob(jp); 2551556Srgrimes INTOFF; 2561556Srgrimes killpg(jp->ps[0].pid, SIGCONT); 2571556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 25826104Ssteve if (WIFSTOPPED(ps->status)) { 2591556Srgrimes ps->status = -1; 2601556Srgrimes jp->state = 0; 2611556Srgrimes } 2621556Srgrimes } 2631556Srgrimes INTON; 2641556Srgrimes} 2651556Srgrimes#endif 2661556Srgrimes 2671556Srgrimes 2681556Srgrimesint 26997669Stjrjobscmd(int argc, char *argv[]) 27017987Speter{ 27197669Stjr char *id; 27297669Stjr int ch, sformat, lformat; 27397669Stjr 27497669Stjr optind = optreset = 1; 275100663Stjr opterr = 0; 27697669Stjr sformat = lformat = 0; 27797669Stjr while ((ch = getopt(argc, argv, "ls")) != -1) { 27897669Stjr switch (ch) { 27997669Stjr case 'l': 28097669Stjr lformat = 1; 28197669Stjr break; 28297669Stjr case 's': 28397669Stjr sformat = 1; 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) 29497669Stjr showjobs(0, sformat, lformat); 29597669Stjr else 29697669Stjr while ((id = *argv++) != NULL) 29797822Stjr showjob(getjob(id), 0, sformat, lformat); 29897669Stjr 29997669Stjr return (0); 3001556Srgrimes} 3011556Srgrimes 30297663StjrSTATIC void 30397822Stjrshowjob(struct job *jp, pid_t pid, int sformat, int lformat) 30497663Stjr{ 30597663Stjr char s[64]; 30697663Stjr struct procstat *ps; 30797669Stjr struct job *j; 30897669Stjr int col, curr, i, jobno, prev, procno; 30997669Stjr char c; 3101556Srgrimes 31197663Stjr procno = jp->nprocs; 31297663Stjr jobno = jp - jobtab + 1; 31397669Stjr curr = prev = 0; 31497669Stjr#if JOBS 31597669Stjr if ((j = getcurjob(NULL)) != NULL) { 31697669Stjr curr = j - jobtab + 1; 31797669Stjr if ((j = getcurjob(j)) != NULL) 31897669Stjr prev = j - jobtab + 1; 31997669Stjr } 32097669Stjr#endif 32197663Stjr for (ps = jp->ps ; ; ps++) { /* for each process */ 32297669Stjr if (sformat) { 323100308Stjr out1fmt("%d\n", (int)ps->pid); 32497669Stjr goto skip; 32597669Stjr } 32697822Stjr if (!lformat && ps != jp->ps && pid == 0) 32797669Stjr goto skip; 32897822Stjr if (pid != 0 && pid != ps->pid) 32997822Stjr goto skip; 33097819Stjr if (jobno == curr && ps == jp->ps) 33197669Stjr c = '+'; 33297819Stjr else if (jobno == prev && ps == jp->ps) 33397669Stjr c = '-'; 33497669Stjr else 33597669Stjr c = ' '; 33697663Stjr if (ps == jp->ps) 33797669Stjr fmtstr(s, 64, "[%d] %c ", jobno, c); 33897663Stjr else 33997816Stjr fmtstr(s, 64, " %c ", c); 34097663Stjr out1str(s); 34197663Stjr col = strlen(s); 34297669Stjr if (lformat) { 343100308Stjr fmtstr(s, 64, "%d ", (int)ps->pid); 34497669Stjr out1str(s); 34597669Stjr col += strlen(s); 34697669Stjr } 34797663Stjr s[0] = '\0'; 34897819Stjr if (ps != jp->ps) { 34997819Stjr *s = '\0'; 35097819Stjr } else if (ps->status == -1) { 35197669Stjr strcpy(s, "Running"); 35297663Stjr } else if (WIFEXITED(ps->status)) { 35397820Stjr if (WEXITSTATUS(ps->status) == 0) 35497820Stjr strcpy(s, "Done"); 35597820Stjr else 35697820Stjr fmtstr(s, 64, "Done (%d)", 35797820Stjr WEXITSTATUS(ps->status)); 35897663Stjr } else { 35997663Stjr#if JOBS 36097663Stjr if (WIFSTOPPED(ps->status)) 36197663Stjr i = WSTOPSIG(ps->status); 36297663Stjr else 36397663Stjr#endif 36497663Stjr i = WTERMSIG(ps->status); 36597663Stjr if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F]) 36697663Stjr scopy(sys_siglist[i & 0x7F], s); 36797663Stjr else 36897663Stjr fmtstr(s, 64, "Signal %d", i & 0x7F); 36997663Stjr if (WCOREDUMP(ps->status)) 37097663Stjr strcat(s, " (core dumped)"); 37197663Stjr } 37297663Stjr out1str(s); 37397663Stjr col += strlen(s); 37497663Stjr do { 37597663Stjr out1c(' '); 37697663Stjr col++; 37797663Stjr } while (col < 30); 37897663Stjr out1str(ps->cmd); 37997663Stjr out1c('\n'); 38097669Stjrskip: if (--procno <= 0) 38197663Stjr break; 38297663Stjr } 38397663Stjr} 38497663Stjr 3851556Srgrimes/* 3861556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 3871556Srgrimes * statuses have changed since the last call to showjobs. 3881556Srgrimes * 3891556Srgrimes * If the shell is interrupted in the process of creating a job, the 3901556Srgrimes * result may be a job structure containing zero processes. Such structures 3911556Srgrimes * will be freed here. 3921556Srgrimes */ 3931556Srgrimes 3941556Srgrimesvoid 39597669Stjrshowjobs(int change, int sformat, int lformat) 39617987Speter{ 3971556Srgrimes int jobno; 3981556Srgrimes struct job *jp; 3991556Srgrimes 4001556Srgrimes TRACE(("showjobs(%d) called\n", change)); 4011556Srgrimes while (dowait(0, (struct job *)NULL) > 0); 4021556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 4031556Srgrimes if (! jp->used) 4041556Srgrimes continue; 4051556Srgrimes if (jp->nprocs == 0) { 4061556Srgrimes freejob(jp); 4071556Srgrimes continue; 4081556Srgrimes } 4091556Srgrimes if (change && ! jp->changed) 4101556Srgrimes continue; 41197822Stjr showjob(jp, 0, sformat, lformat); 4121556Srgrimes jp->changed = 0; 4131556Srgrimes if (jp->state == JOBDONE) { 4141556Srgrimes freejob(jp); 4151556Srgrimes } 4161556Srgrimes } 4171556Srgrimes} 4181556Srgrimes 4191556Srgrimes 4201556Srgrimes/* 4211556Srgrimes * Mark a job structure as unused. 4221556Srgrimes */ 4231556Srgrimes 4241556SrgrimesSTATIC void 42590111Simpfreejob(struct job *jp) 42690111Simp{ 4271556Srgrimes struct procstat *ps; 4281556Srgrimes int i; 4291556Srgrimes 4301556Srgrimes INTOFF; 4311556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 4321556Srgrimes if (ps->cmd != nullstr) 4331556Srgrimes ckfree(ps->cmd); 4341556Srgrimes } 4351556Srgrimes if (jp->ps != &jp->ps0) 4361556Srgrimes ckfree(jp->ps); 4371556Srgrimes jp->used = 0; 4381556Srgrimes#if JOBS 43997659Stjr deljob(jp); 4401556Srgrimes#endif 4411556Srgrimes INTON; 4421556Srgrimes} 4431556Srgrimes 4441556Srgrimes 4451556Srgrimes 4461556Srgrimesint 44790111Simpwaitcmd(int argc, char **argv) 44817987Speter{ 4491556Srgrimes struct job *job; 45026104Ssteve int status, retval; 4511556Srgrimes struct job *jp; 4521556Srgrimes 4531556Srgrimes if (argc > 1) { 4541556Srgrimes job = getjob(argv[1]); 4551556Srgrimes } else { 4561556Srgrimes job = NULL; 4571556Srgrimes } 45838536Scracauer 45938536Scracauer /* 46038536Scracauer * Loop until a process is terminated or stopped, or a SIGINT is 46138536Scracauer * received. 46238536Scracauer */ 46338536Scracauer 46438521Scracauer in_waitcmd++; 46538536Scracauer do { 4661556Srgrimes if (job != NULL) { 4671556Srgrimes if (job->state) { 4681556Srgrimes status = job->ps[job->nprocs - 1].status; 46926104Ssteve if (WIFEXITED(status)) 47026104Ssteve retval = WEXITSTATUS(status); 4711556Srgrimes#if JOBS 47226104Ssteve else if (WIFSTOPPED(status)) 47326104Ssteve retval = WSTOPSIG(status) + 128; 4741556Srgrimes#endif 4751556Srgrimes else 47626104Ssteve retval = WTERMSIG(status) + 128; 4771556Srgrimes if (! iflag) 4781556Srgrimes freejob(job); 47938536Scracauer in_waitcmd--; 48026104Ssteve return retval; 4811556Srgrimes } 4821556Srgrimes } else { 4831556Srgrimes for (jp = jobtab ; ; jp++) { 4841556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 48538536Scracauer in_waitcmd--; 4861556Srgrimes return 0; 4871556Srgrimes } 4881556Srgrimes if (jp->used && jp->state == 0) 4891556Srgrimes break; 4901556Srgrimes } 4911556Srgrimes } 49238521Scracauer } while (dowait(1, (struct job *)NULL) != -1); 49338521Scracauer in_waitcmd--; 49438521Scracauer 49538521Scracauer return 0; 4961556Srgrimes} 4971556Srgrimes 4981556Srgrimes 4991556Srgrimes 50017987Speterint 50190111Simpjobidcmd(int argc __unused, char **argv) 50217987Speter{ 5031556Srgrimes struct job *jp; 5041556Srgrimes int i; 5051556Srgrimes 5061556Srgrimes jp = getjob(argv[1]); 5071556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 508100308Stjr out1fmt("%d", (int)jp->ps[i].pid); 5091556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 5101556Srgrimes } 5111556Srgrimes return 0; 5121556Srgrimes} 5131556Srgrimes 5141556Srgrimes 5151556Srgrimes 5161556Srgrimes/* 5171556Srgrimes * Convert a job name to a job structure. 5181556Srgrimes */ 5191556Srgrimes 5201556SrgrimesSTATIC struct job * 52190111Simpgetjob(char *name) 52290111Simp{ 5231556Srgrimes int jobno; 52497688Stjr struct job *found, *jp; 525100308Stjr pid_t pid; 5261556Srgrimes int i; 5271556Srgrimes 5281556Srgrimes if (name == NULL) { 5291556Srgrimes#if JOBS 53097659Stjrcurrentjob: if ((jp = getcurjob(NULL)) == NULL) 5311556Srgrimes error("No current job"); 53297659Stjr return (jp); 5331556Srgrimes#else 5341556Srgrimes error("No current job"); 5351556Srgrimes#endif 5361556Srgrimes } else if (name[0] == '%') { 5371556Srgrimes if (is_digit(name[1])) { 5381556Srgrimes jobno = number(name + 1); 5391556Srgrimes if (jobno > 0 && jobno <= njobs 5401556Srgrimes && jobtab[jobno - 1].used != 0) 5411556Srgrimes return &jobtab[jobno - 1]; 5421556Srgrimes#if JOBS 5431556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 5441556Srgrimes goto currentjob; 54597688Stjr } else if (name[1] == '+' && name[2] == '\0') { 54697688Stjr goto currentjob; 54797688Stjr } else if (name[1] == '-' && name[2] == '\0') { 54897688Stjr if ((jp = getcurjob(NULL)) == NULL || 54997688Stjr (jp = getcurjob(jp)) == NULL) 55097688Stjr error("No previous job"); 55197688Stjr return (jp); 5521556Srgrimes#endif 55397688Stjr } else if (name[1] == '?') { 55497688Stjr found = NULL; 55597688Stjr for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 55697688Stjr if (jp->used && jp->nprocs > 0 55797688Stjr && strstr(jp->ps[0].cmd, name + 2) != NULL) { 55897688Stjr if (found) 55997688Stjr error("%s: ambiguous", name); 56097688Stjr found = jp; 56197688Stjr } 56297688Stjr } 56397688Stjr if (found != NULL) 56497688Stjr return (found); 5651556Srgrimes } else { 56697688Stjr found = NULL; 5671556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5681556Srgrimes if (jp->used && jp->nprocs > 0 5691556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 5701556Srgrimes if (found) 5711556Srgrimes error("%s: ambiguous", name); 5721556Srgrimes found = jp; 5731556Srgrimes } 5741556Srgrimes } 5751556Srgrimes if (found) 5761556Srgrimes return found; 5771556Srgrimes } 5781556Srgrimes } else if (is_number(name)) { 579100308Stjr pid = (pid_t)number(name); 5801556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5811556Srgrimes if (jp->used && jp->nprocs > 0 5821556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 5831556Srgrimes return jp; 5841556Srgrimes } 5851556Srgrimes } 5861556Srgrimes error("No such job: %s", name); 58717987Speter /*NOTREACHED*/ 58817987Speter return NULL; 5891556Srgrimes} 5901556Srgrimes 5911556Srgrimes 5921556Srgrimes 5931556Srgrimes/* 5941556Srgrimes * Return a new job structure, 5951556Srgrimes */ 5961556Srgrimes 5971556Srgrimesstruct job * 59890111Simpmakejob(union node *node __unused, int nprocs) 59917987Speter{ 6001556Srgrimes int i; 6011556Srgrimes struct job *jp; 6021556Srgrimes 6031556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 6041556Srgrimes if (--i < 0) { 6051556Srgrimes INTOFF; 6061556Srgrimes if (njobs == 0) { 6071556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 60897664Stjr#if JOBS 60997659Stjr jobmru = NULL; 61097664Stjr#endif 6111556Srgrimes } else { 6121556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 61317987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 61497659Stjr#if JOBS 61597659Stjr /* Relocate `next' pointers and list head */ 61699760Stjr if (jobmru != NULL) 61799760Stjr jobmru = &jp[jobmru - jobtab]; 61897659Stjr for (i = 0; i < njobs; i++) 61997659Stjr if (jp[i].next != NULL) 62097659Stjr jp[i].next = &jp[jp[i].next - 62197659Stjr jobtab]; 62297659Stjr#endif 62320425Ssteve /* Relocate `ps' pointers */ 62420425Ssteve for (i = 0; i < njobs; i++) 62520425Ssteve if (jp[i].ps == &jobtab[i].ps0) 62620425Ssteve jp[i].ps = &jp[i].ps0; 6271556Srgrimes ckfree(jobtab); 6281556Srgrimes jobtab = jp; 6291556Srgrimes } 6301556Srgrimes jp = jobtab + njobs; 6311556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 6321556Srgrimes INTON; 6331556Srgrimes break; 6341556Srgrimes } 6351556Srgrimes if (jp->used == 0) 6361556Srgrimes break; 6371556Srgrimes } 6381556Srgrimes INTOFF; 6391556Srgrimes jp->state = 0; 6401556Srgrimes jp->used = 1; 6411556Srgrimes jp->changed = 0; 6421556Srgrimes jp->nprocs = 0; 643100305Stjr jp->foreground = 0; 6441556Srgrimes#if JOBS 6451556Srgrimes jp->jobctl = jobctl; 64697659Stjr jp->next = NULL; 6471556Srgrimes#endif 6481556Srgrimes if (nprocs > 1) { 6491556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 6501556Srgrimes } else { 6511556Srgrimes jp->ps = &jp->ps0; 6521556Srgrimes } 6531556Srgrimes INTON; 65417987Speter TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, 65517987Speter jp - jobtab + 1)); 6561556Srgrimes return jp; 6578855Srgrimes} 6581556Srgrimes 65997659Stjr#if JOBS 66097659StjrSTATIC void 66197659Stjrsetcurjob(struct job *cj) 66297659Stjr{ 66397659Stjr struct job *jp, *prev; 6641556Srgrimes 66597659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 66697659Stjr if (jp == cj) { 66797659Stjr if (prev != NULL) 66897659Stjr prev->next = jp->next; 66997659Stjr else 67097659Stjr jobmru = jp->next; 67197659Stjr jp->next = jobmru; 67297659Stjr jobmru = cj; 67397659Stjr return; 67497659Stjr } 67597659Stjr } 67697659Stjr cj->next = jobmru; 67797659Stjr jobmru = cj; 67897659Stjr} 67997659Stjr 68097659StjrSTATIC void 68197659Stjrdeljob(struct job *j) 68297659Stjr{ 68397659Stjr struct job *jp, *prev; 68497659Stjr 68597659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 68697659Stjr if (jp == j) { 68797659Stjr if (prev != NULL) 68897659Stjr prev->next = jp->next; 68997659Stjr else 69097659Stjr jobmru = jp->next; 69197659Stjr return; 69297659Stjr } 69397659Stjr } 69497659Stjr} 69597659Stjr 6961556Srgrimes/* 69797659Stjr * Return the most recently used job that isn't `nj', and preferably one 69897659Stjr * that is stopped. 69997659Stjr */ 70097659StjrSTATIC struct job * 70197659Stjrgetcurjob(struct job *nj) 70297659Stjr{ 70397659Stjr struct job *jp; 70497659Stjr 70597659Stjr /* Try to find a stopped one.. */ 70697659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 70797659Stjr if (jp->used && jp != nj && jp->state == JOBSTOPPED) 70897659Stjr return (jp); 70997659Stjr /* Otherwise the most recently used job that isn't `nj' */ 71097659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 71197659Stjr if (jp->used && jp != nj) 71297659Stjr return (jp); 71397659Stjr 71497659Stjr return (NULL); 71597659Stjr} 71697659Stjr 71797659Stjr#endif 71897659Stjr 71997659Stjr/* 7201556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 7211556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 7221556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 7231556Srgrimes * be NULL. The mode parameter can be one of the following: 7241556Srgrimes * FORK_FG - Fork off a foreground process. 7251556Srgrimes * FORK_BG - Fork off a background process. 7261556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 7271556Srgrimes * process group even if job control is on. 7281556Srgrimes * 7291556Srgrimes * When job control is turned off, background processes have their standard 7301556Srgrimes * input redirected to /dev/null (except for the second and later processes 7311556Srgrimes * in a pipeline). 7321556Srgrimes */ 7331556Srgrimes 734100308Stjrpid_t 73590111Simpforkshell(struct job *jp, union node *n, int mode) 73617987Speter{ 737100308Stjr pid_t pid; 738100308Stjr pid_t pgrp; 7391556Srgrimes 74017987Speter TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n, 74117987Speter mode)); 7421556Srgrimes INTOFF; 7431556Srgrimes pid = fork(); 7441556Srgrimes if (pid == -1) { 7451556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 7461556Srgrimes INTON; 74753891Scracauer error("Cannot fork: %s", strerror(errno)); 7481556Srgrimes } 7491556Srgrimes if (pid == 0) { 7501556Srgrimes struct job *p; 7511556Srgrimes int wasroot; 7521556Srgrimes int i; 7531556Srgrimes 754100308Stjr TRACE(("Child shell %d\n", (int)getpid())); 7551556Srgrimes wasroot = rootshell; 7561556Srgrimes rootshell = 0; 7571556Srgrimes closescript(); 7581556Srgrimes INTON; 7591556Srgrimes clear_traps(); 7601556Srgrimes#if JOBS 7611556Srgrimes jobctl = 0; /* do job control only in root shell */ 7621556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 7631556Srgrimes if (jp == NULL || jp->nprocs == 0) 7641556Srgrimes pgrp = getpid(); 7651556Srgrimes else 7661556Srgrimes pgrp = jp->ps[0].pid; 76721352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 7681556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 76999762Stjr if (tcsetpgrp(ttyfd, pgrp) < 0) 77020425Ssteve error("tcsetpgrp failed, errno=%d", errno); 7711556Srgrimes } 7721556Srgrimes setsignal(SIGTSTP); 7731556Srgrimes setsignal(SIGTTOU); 7741556Srgrimes } else if (mode == FORK_BG) { 7751556Srgrimes ignoresig(SIGINT); 7761556Srgrimes ignoresig(SIGQUIT); 7771556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7781556Srgrimes ! fd0_redirected_p ()) { 7791556Srgrimes close(0); 78069793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 78169793Sobrien error("Can't open %s: %s", 78269793Sobrien _PATH_DEVNULL, strerror(errno)); 7831556Srgrimes } 7841556Srgrimes } 7851556Srgrimes#else 7861556Srgrimes if (mode == FORK_BG) { 7871556Srgrimes ignoresig(SIGINT); 7881556Srgrimes ignoresig(SIGQUIT); 7891556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7901556Srgrimes ! fd0_redirected_p ()) { 7911556Srgrimes close(0); 79269793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 79369793Sobrien error("Can't open %s: %s", 79469793Sobrien _PATH_DEVNULL, strerror(errno)); 7951556Srgrimes } 7961556Srgrimes } 7971556Srgrimes#endif 798102051Stjr INTOFF; 799102051Stjr for (i = njobs, p = jobtab ; --i >= 0 ; p++) 800102051Stjr if (p->used) 801102051Stjr freejob(p); 802102051Stjr INTON; 8031556Srgrimes if (wasroot && iflag) { 8041556Srgrimes setsignal(SIGINT); 8051556Srgrimes setsignal(SIGQUIT); 8061556Srgrimes setsignal(SIGTERM); 8071556Srgrimes } 8081556Srgrimes return pid; 8091556Srgrimes } 8101556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 8111556Srgrimes if (jp == NULL || jp->nprocs == 0) 8121556Srgrimes pgrp = pid; 8131556Srgrimes else 8141556Srgrimes pgrp = jp->ps[0].pid; 81517987Speter setpgid(pid, pgrp); 8161556Srgrimes } 8171556Srgrimes if (mode == FORK_BG) 8181556Srgrimes backgndpid = pid; /* set $! */ 8191556Srgrimes if (jp) { 8201556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 8211556Srgrimes ps->pid = pid; 8221556Srgrimes ps->status = -1; 8231556Srgrimes ps->cmd = nullstr; 8241556Srgrimes if (iflag && rootshell && n) 8251556Srgrimes ps->cmd = commandtext(n); 826100305Stjr jp->foreground = mode == FORK_FG; 82797659Stjr#if JOBS 82897659Stjr setcurjob(jp); 82997659Stjr#endif 8301556Srgrimes } 8311556Srgrimes INTON; 832100308Stjr TRACE(("In parent shell: child = %d\n", (int)pid)); 8331556Srgrimes return pid; 8341556Srgrimes} 8351556Srgrimes 8361556Srgrimes 8371556Srgrimes 8381556Srgrimes/* 8391556Srgrimes * Wait for job to finish. 8401556Srgrimes * 8411556Srgrimes * Under job control we have the problem that while a child process is 8421556Srgrimes * running interrupts generated by the user are sent to the child but not 8431556Srgrimes * to the shell. This means that an infinite loop started by an inter- 8441556Srgrimes * active user may be hard to kill. With job control turned off, an 8451556Srgrimes * interactive user may place an interactive program inside a loop. If 8461556Srgrimes * the interactive program catches interrupts, the user doesn't want 8471556Srgrimes * these interrupts to also abort the loop. The approach we take here 8481556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 84946684Skris * foreground process to terminate, and then send itself an interrupt 8501556Srgrimes * signal if the child process was terminated by an interrupt signal. 8511556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 8521556Srgrimes * exit on interrupt; unless these processes terminate themselves by 8531556Srgrimes * sending a signal to themselves (instead of calling exit) they will 8541556Srgrimes * confuse this approach. 8551556Srgrimes */ 8561556Srgrimes 8571556Srgrimesint 85890111Simpwaitforjob(struct job *jp, int *origstatus) 85945916Scracauer{ 8601556Srgrimes#if JOBS 861100308Stjr pid_t mypgrp = getpgrp(); 8621556Srgrimes#endif 8631556Srgrimes int status; 8641556Srgrimes int st; 8651556Srgrimes 8661556Srgrimes INTOFF; 8671556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 86838950Scracauer while (jp->state == 0) 86938950Scracauer if (dowait(1, jp) == -1) 87038950Scracauer dotrap(); 8711556Srgrimes#if JOBS 8721556Srgrimes if (jp->jobctl) { 87399762Stjr if (tcsetpgrp(ttyfd, mypgrp) < 0) 87420425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 8751556Srgrimes } 8761556Srgrimes if (jp->state == JOBSTOPPED) 87797659Stjr setcurjob(jp); 8781556Srgrimes#endif 8791556Srgrimes status = jp->ps[jp->nprocs - 1].status; 88045916Scracauer if (origstatus != NULL) 88145916Scracauer *origstatus = status; 8821556Srgrimes /* convert to 8 bits */ 88326104Ssteve if (WIFEXITED(status)) 88426104Ssteve st = WEXITSTATUS(status); 8851556Srgrimes#if JOBS 88626104Ssteve else if (WIFSTOPPED(status)) 88726104Ssteve st = WSTOPSIG(status) + 128; 8881556Srgrimes#endif 8891556Srgrimes else 89026104Ssteve st = WTERMSIG(status) + 128; 8911556Srgrimes if (! JOBS || jp->state == JOBDONE) 8921556Srgrimes freejob(jp); 89338521Scracauer if (int_pending()) { 89438521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 89538521Scracauer kill(getpid(), SIGINT); 89638521Scracauer else 89738521Scracauer CLEAR_PENDING_INT; 89838521Scracauer } 8991556Srgrimes INTON; 9001556Srgrimes return st; 9011556Srgrimes} 9021556Srgrimes 9031556Srgrimes 9041556Srgrimes 9051556Srgrimes/* 9061556Srgrimes * Wait for a process to terminate. 9071556Srgrimes */ 9081556Srgrimes 909100308StjrSTATIC pid_t 91090111Simpdowait(int block, struct job *job) 91117987Speter{ 912100308Stjr pid_t pid; 9131556Srgrimes int status; 9141556Srgrimes struct procstat *sp; 9151556Srgrimes struct job *jp; 9161556Srgrimes struct job *thisjob; 9171556Srgrimes int done; 9181556Srgrimes int stopped; 91926104Ssteve int sig; 920100305Stjr int i; 9211556Srgrimes 92238950Scracauer in_dowait++; 9231556Srgrimes TRACE(("dowait(%d) called\n", block)); 9241556Srgrimes do { 9251556Srgrimes pid = waitproc(block, &status); 926100308Stjr TRACE(("wait returns %d, status=%d\n", (int)pid, status)); 92772086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 92872086Scracauer (WIFSTOPPED(status) && !iflag)); 92938950Scracauer in_dowait--; 93038521Scracauer if (breakwaitcmd != 0) { 93138521Scracauer breakwaitcmd = 0; 93238521Scracauer 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); 987100305Stjr if ((i & 0x7F) < 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