jobs.c revision 109627
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 109627 2003-01-21 10:06:04Z 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++; 12999762Stjr if (i > 2 || (ttyfd = dup(i)) < 0) 13099762Stjr goto out; 13199762Stjr } 132103223Snectar if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) { 13399762Stjr close(ttyfd); 13499762Stjr ttyfd = -1; 13599762Stjr goto out; 13699762Stjr } 1371556Srgrimes do { /* while we are in the background */ 13899762Stjr initialpgrp = tcgetpgrp(ttyfd); 13920425Ssteve if (initialpgrp < 0) { 14099762Stjrout: out2str("sh: can't access tty; job control turned off\n"); 1411556Srgrimes mflag = 0; 1421556Srgrimes return; 1431556Srgrimes } 1441556Srgrimes if (initialpgrp == -1) 14517987Speter initialpgrp = getpgrp(); 14617987Speter else if (initialpgrp != getpgrp()) { 14799762Stjr killpg(0, SIGTTIN); 1481556Srgrimes continue; 1491556Srgrimes } 1501556Srgrimes } while (0); 1511556Srgrimes setsignal(SIGTSTP); 1521556Srgrimes setsignal(SIGTTOU); 1531556Srgrimes setsignal(SIGTTIN); 15417987Speter setpgid(0, rootpid); 15599762Stjr tcsetpgrp(ttyfd, rootpid); 1561556Srgrimes } else { /* turning job control off */ 15717987Speter setpgid(0, initialpgrp); 15899762Stjr tcsetpgrp(ttyfd, initialpgrp); 15999762Stjr close(ttyfd); 16099762Stjr ttyfd = -1; 1611556Srgrimes setsignal(SIGTSTP); 1621556Srgrimes setsignal(SIGTTOU); 1631556Srgrimes setsignal(SIGTTIN); 1641556Srgrimes } 1651556Srgrimes jobctl = on; 1661556Srgrimes} 16720425Ssteve#endif 1681556Srgrimes 1691556Srgrimes 1701556Srgrimes#ifdef mkinit 17128346SsteveINCLUDE <sys/types.h> 17217987SpeterINCLUDE <stdlib.h> 1731556Srgrimes 1741556SrgrimesSHELLPROC { 1751556Srgrimes backgndpid = -1; 1761556Srgrimes#if JOBS 1771556Srgrimes jobctl = 0; 1781556Srgrimes#endif 1791556Srgrimes} 1801556Srgrimes 1811556Srgrimes#endif 1821556Srgrimes 1831556Srgrimes 1841556Srgrimes 1851556Srgrimes#if JOBS 18617987Speterint 18790111Simpfgcmd(int argc __unused, char **argv) 18817987Speter{ 1891556Srgrimes struct job *jp; 190100308Stjr pid_t pgrp; 1911556Srgrimes int status; 1921556Srgrimes 1931556Srgrimes jp = getjob(argv[1]); 1941556Srgrimes if (jp->jobctl == 0) 1951556Srgrimes error("job not created under job control"); 19696933Stjr out1str(jp->ps[0].cmd); 19796933Stjr out1c('\n'); 19896933Stjr flushout(&output); 1991556Srgrimes pgrp = jp->ps[0].pid; 20099762Stjr tcsetpgrp(ttyfd, pgrp); 2011556Srgrimes restartjob(jp); 202100305Stjr jp->foreground = 1; 2031556Srgrimes INTOFF; 20445916Scracauer status = waitforjob(jp, (int *)NULL); 2051556Srgrimes INTON; 2061556Srgrimes return status; 2071556Srgrimes} 2081556Srgrimes 2091556Srgrimes 21017987Speterint 21190111Simpbgcmd(int argc, char **argv) 21217987Speter{ 21396933Stjr char s[64]; 2141556Srgrimes struct job *jp; 2151556Srgrimes 2161556Srgrimes do { 2171556Srgrimes jp = getjob(*++argv); 2181556Srgrimes if (jp->jobctl == 0) 2191556Srgrimes error("job not created under job control"); 22096933Stjr if (jp->state == JOBDONE) 22196933Stjr continue; 2221556Srgrimes restartjob(jp); 223100305Stjr jp->foreground = 0; 224104275Smux fmtstr(s, 64, "[%td] ", jp - jobtab + 1); 22596933Stjr out1str(s); 22696933Stjr out1str(jp->ps[0].cmd); 22796933Stjr out1c('\n'); 2281556Srgrimes } while (--argc > 1); 2291556Srgrimes return 0; 2301556Srgrimes} 2311556Srgrimes 2321556Srgrimes 2331556SrgrimesSTATIC void 23490111Simprestartjob(struct job *jp) 23517987Speter{ 2361556Srgrimes struct procstat *ps; 2371556Srgrimes int i; 2381556Srgrimes 2391556Srgrimes if (jp->state == JOBDONE) 2401556Srgrimes return; 24197660Stjr setcurjob(jp); 2421556Srgrimes INTOFF; 2431556Srgrimes killpg(jp->ps[0].pid, SIGCONT); 2441556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 24526104Ssteve if (WIFSTOPPED(ps->status)) { 2461556Srgrimes ps->status = -1; 2471556Srgrimes jp->state = 0; 2481556Srgrimes } 2491556Srgrimes } 2501556Srgrimes INTON; 2511556Srgrimes} 2521556Srgrimes#endif 2531556Srgrimes 2541556Srgrimes 2551556Srgrimesint 25697669Stjrjobscmd(int argc, char *argv[]) 25717987Speter{ 25897669Stjr char *id; 25997669Stjr int ch, sformat, lformat; 26097669Stjr 26197669Stjr optind = optreset = 1; 262100663Stjr opterr = 0; 26397669Stjr sformat = lformat = 0; 26497669Stjr while ((ch = getopt(argc, argv, "ls")) != -1) { 26597669Stjr switch (ch) { 26697669Stjr case 'l': 26797669Stjr lformat = 1; 26897669Stjr break; 26997669Stjr case 's': 27097669Stjr sformat = 1; 27197669Stjr break; 27297669Stjr case '?': 27397669Stjr default: 27497669Stjr error("unknown option: -%c", optopt); 27597669Stjr } 27697669Stjr } 27797669Stjr argc -= optind; 27897669Stjr argv += optind; 27997669Stjr 28097669Stjr if (argc == 0) 28197669Stjr showjobs(0, sformat, lformat); 28297669Stjr else 28397669Stjr while ((id = *argv++) != NULL) 28497822Stjr showjob(getjob(id), 0, sformat, lformat); 28597669Stjr 28697669Stjr return (0); 2871556Srgrimes} 2881556Srgrimes 28997663StjrSTATIC void 29097822Stjrshowjob(struct job *jp, pid_t pid, int sformat, int lformat) 29197663Stjr{ 29297663Stjr char s[64]; 29397663Stjr struct procstat *ps; 29497669Stjr struct job *j; 29597669Stjr int col, curr, i, jobno, prev, procno; 29697669Stjr char c; 2971556Srgrimes 29897663Stjr procno = jp->nprocs; 29997663Stjr jobno = jp - jobtab + 1; 30097669Stjr curr = prev = 0; 30197669Stjr#if JOBS 30297669Stjr if ((j = getcurjob(NULL)) != NULL) { 30397669Stjr curr = j - jobtab + 1; 30497669Stjr if ((j = getcurjob(j)) != NULL) 30597669Stjr prev = j - jobtab + 1; 30697669Stjr } 30797669Stjr#endif 30897663Stjr for (ps = jp->ps ; ; ps++) { /* for each process */ 30997669Stjr if (sformat) { 310100308Stjr out1fmt("%d\n", (int)ps->pid); 31197669Stjr goto skip; 31297669Stjr } 31397822Stjr if (!lformat && ps != jp->ps && pid == 0) 31497669Stjr goto skip; 31597822Stjr if (pid != 0 && pid != ps->pid) 31697822Stjr goto skip; 31797819Stjr if (jobno == curr && ps == jp->ps) 31897669Stjr c = '+'; 31997819Stjr else if (jobno == prev && ps == jp->ps) 32097669Stjr c = '-'; 32197669Stjr else 32297669Stjr c = ' '; 32397663Stjr if (ps == jp->ps) 32497669Stjr fmtstr(s, 64, "[%d] %c ", jobno, c); 32597663Stjr else 32697816Stjr fmtstr(s, 64, " %c ", c); 32797663Stjr out1str(s); 32897663Stjr col = strlen(s); 32997669Stjr if (lformat) { 330100308Stjr fmtstr(s, 64, "%d ", (int)ps->pid); 33197669Stjr out1str(s); 33297669Stjr col += strlen(s); 33397669Stjr } 33497663Stjr s[0] = '\0'; 33597819Stjr if (ps != jp->ps) { 33697819Stjr *s = '\0'; 33797819Stjr } else if (ps->status == -1) { 33897669Stjr strcpy(s, "Running"); 33997663Stjr } else if (WIFEXITED(ps->status)) { 34097820Stjr if (WEXITSTATUS(ps->status) == 0) 34197820Stjr strcpy(s, "Done"); 34297820Stjr else 34397820Stjr fmtstr(s, 64, "Done (%d)", 34497820Stjr WEXITSTATUS(ps->status)); 34597663Stjr } else { 34697663Stjr#if JOBS 34797663Stjr if (WIFSTOPPED(ps->status)) 34897663Stjr i = WSTOPSIG(ps->status); 34997663Stjr else 35097663Stjr#endif 35197663Stjr i = WTERMSIG(ps->status); 35297663Stjr if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F]) 35397663Stjr scopy(sys_siglist[i & 0x7F], s); 35497663Stjr else 35597663Stjr fmtstr(s, 64, "Signal %d", i & 0x7F); 35697663Stjr if (WCOREDUMP(ps->status)) 35797663Stjr strcat(s, " (core dumped)"); 35897663Stjr } 35997663Stjr out1str(s); 36097663Stjr col += strlen(s); 36197663Stjr do { 36297663Stjr out1c(' '); 36397663Stjr col++; 36497663Stjr } while (col < 30); 36597663Stjr out1str(ps->cmd); 36697663Stjr out1c('\n'); 36797669Stjrskip: if (--procno <= 0) 36897663Stjr break; 36997663Stjr } 37097663Stjr} 37197663Stjr 3721556Srgrimes/* 3731556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 3741556Srgrimes * statuses have changed since the last call to showjobs. 3751556Srgrimes * 3761556Srgrimes * If the shell is interrupted in the process of creating a job, the 3771556Srgrimes * result may be a job structure containing zero processes. Such structures 3781556Srgrimes * will be freed here. 3791556Srgrimes */ 3801556Srgrimes 3811556Srgrimesvoid 38297669Stjrshowjobs(int change, int sformat, int lformat) 38317987Speter{ 3841556Srgrimes int jobno; 3851556Srgrimes struct job *jp; 3861556Srgrimes 3871556Srgrimes TRACE(("showjobs(%d) called\n", change)); 3881556Srgrimes while (dowait(0, (struct job *)NULL) > 0); 3891556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 3901556Srgrimes if (! jp->used) 3911556Srgrimes continue; 3921556Srgrimes if (jp->nprocs == 0) { 3931556Srgrimes freejob(jp); 3941556Srgrimes continue; 3951556Srgrimes } 3961556Srgrimes if (change && ! jp->changed) 3971556Srgrimes continue; 39897822Stjr showjob(jp, 0, sformat, lformat); 3991556Srgrimes jp->changed = 0; 4001556Srgrimes if (jp->state == JOBDONE) { 4011556Srgrimes freejob(jp); 4021556Srgrimes } 4031556Srgrimes } 4041556Srgrimes} 4051556Srgrimes 4061556Srgrimes 4071556Srgrimes/* 4081556Srgrimes * Mark a job structure as unused. 4091556Srgrimes */ 4101556Srgrimes 4111556SrgrimesSTATIC void 41290111Simpfreejob(struct job *jp) 41390111Simp{ 4141556Srgrimes struct procstat *ps; 4151556Srgrimes int i; 4161556Srgrimes 4171556Srgrimes INTOFF; 4181556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 4191556Srgrimes if (ps->cmd != nullstr) 4201556Srgrimes ckfree(ps->cmd); 4211556Srgrimes } 4221556Srgrimes if (jp->ps != &jp->ps0) 4231556Srgrimes ckfree(jp->ps); 4241556Srgrimes jp->used = 0; 4251556Srgrimes#if JOBS 42697659Stjr deljob(jp); 4271556Srgrimes#endif 4281556Srgrimes INTON; 4291556Srgrimes} 4301556Srgrimes 4311556Srgrimes 4321556Srgrimes 4331556Srgrimesint 43490111Simpwaitcmd(int argc, char **argv) 43517987Speter{ 4361556Srgrimes struct job *job; 43726104Ssteve int status, retval; 4381556Srgrimes struct job *jp; 4391556Srgrimes 4401556Srgrimes if (argc > 1) { 4411556Srgrimes job = getjob(argv[1]); 4421556Srgrimes } else { 4431556Srgrimes job = NULL; 4441556Srgrimes } 44538536Scracauer 44638536Scracauer /* 44738536Scracauer * Loop until a process is terminated or stopped, or a SIGINT is 44838536Scracauer * received. 44938536Scracauer */ 45038536Scracauer 45138521Scracauer in_waitcmd++; 45238536Scracauer do { 4531556Srgrimes if (job != NULL) { 4541556Srgrimes if (job->state) { 4551556Srgrimes status = job->ps[job->nprocs - 1].status; 45626104Ssteve if (WIFEXITED(status)) 45726104Ssteve retval = WEXITSTATUS(status); 4581556Srgrimes#if JOBS 45926104Ssteve else if (WIFSTOPPED(status)) 46026104Ssteve retval = WSTOPSIG(status) + 128; 4611556Srgrimes#endif 4621556Srgrimes else 46326104Ssteve retval = WTERMSIG(status) + 128; 4641556Srgrimes if (! iflag) 4651556Srgrimes freejob(job); 46638536Scracauer in_waitcmd--; 46726104Ssteve return retval; 4681556Srgrimes } 4691556Srgrimes } else { 4701556Srgrimes for (jp = jobtab ; ; jp++) { 4711556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 47238536Scracauer in_waitcmd--; 4731556Srgrimes return 0; 4741556Srgrimes } 4751556Srgrimes if (jp->used && jp->state == 0) 4761556Srgrimes break; 4771556Srgrimes } 4781556Srgrimes } 47938521Scracauer } while (dowait(1, (struct job *)NULL) != -1); 48038521Scracauer in_waitcmd--; 48138521Scracauer 48238521Scracauer return 0; 4831556Srgrimes} 4841556Srgrimes 4851556Srgrimes 4861556Srgrimes 48717987Speterint 48890111Simpjobidcmd(int argc __unused, char **argv) 48917987Speter{ 4901556Srgrimes struct job *jp; 4911556Srgrimes int i; 4921556Srgrimes 4931556Srgrimes jp = getjob(argv[1]); 4941556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 495100308Stjr out1fmt("%d", (int)jp->ps[i].pid); 4961556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 4971556Srgrimes } 4981556Srgrimes return 0; 4991556Srgrimes} 5001556Srgrimes 5011556Srgrimes 5021556Srgrimes 5031556Srgrimes/* 5041556Srgrimes * Convert a job name to a job structure. 5051556Srgrimes */ 5061556Srgrimes 5071556SrgrimesSTATIC struct job * 50890111Simpgetjob(char *name) 50990111Simp{ 5101556Srgrimes int jobno; 51197688Stjr struct job *found, *jp; 512100308Stjr pid_t pid; 5131556Srgrimes int i; 5141556Srgrimes 5151556Srgrimes if (name == NULL) { 5161556Srgrimes#if JOBS 51797659Stjrcurrentjob: if ((jp = getcurjob(NULL)) == NULL) 5181556Srgrimes error("No current job"); 51997659Stjr return (jp); 5201556Srgrimes#else 5211556Srgrimes error("No current job"); 5221556Srgrimes#endif 5231556Srgrimes } else if (name[0] == '%') { 5241556Srgrimes if (is_digit(name[1])) { 5251556Srgrimes jobno = number(name + 1); 5261556Srgrimes if (jobno > 0 && jobno <= njobs 5271556Srgrimes && jobtab[jobno - 1].used != 0) 5281556Srgrimes return &jobtab[jobno - 1]; 5291556Srgrimes#if JOBS 5301556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 5311556Srgrimes goto currentjob; 53297688Stjr } else if (name[1] == '+' && name[2] == '\0') { 53397688Stjr goto currentjob; 53497688Stjr } else if (name[1] == '-' && name[2] == '\0') { 53597688Stjr if ((jp = getcurjob(NULL)) == NULL || 53697688Stjr (jp = getcurjob(jp)) == NULL) 53797688Stjr error("No previous job"); 53897688Stjr return (jp); 5391556Srgrimes#endif 54097688Stjr } else if (name[1] == '?') { 54197688Stjr found = NULL; 54297688Stjr for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 54397688Stjr if (jp->used && jp->nprocs > 0 54497688Stjr && strstr(jp->ps[0].cmd, name + 2) != NULL) { 54597688Stjr if (found) 54697688Stjr error("%s: ambiguous", name); 54797688Stjr found = jp; 54897688Stjr } 54997688Stjr } 55097688Stjr if (found != NULL) 55197688Stjr return (found); 5521556Srgrimes } else { 55397688Stjr found = NULL; 5541556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5551556Srgrimes if (jp->used && jp->nprocs > 0 5561556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 5571556Srgrimes if (found) 5581556Srgrimes error("%s: ambiguous", name); 5591556Srgrimes found = jp; 5601556Srgrimes } 5611556Srgrimes } 5621556Srgrimes if (found) 5631556Srgrimes return found; 5641556Srgrimes } 5651556Srgrimes } else if (is_number(name)) { 566100308Stjr pid = (pid_t)number(name); 5671556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5681556Srgrimes if (jp->used && jp->nprocs > 0 5691556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 5701556Srgrimes return jp; 5711556Srgrimes } 5721556Srgrimes } 5731556Srgrimes error("No such job: %s", name); 57417987Speter /*NOTREACHED*/ 57517987Speter return NULL; 5761556Srgrimes} 5771556Srgrimes 5781556Srgrimes 5791556Srgrimes 5801556Srgrimes/* 5811556Srgrimes * Return a new job structure, 5821556Srgrimes */ 5831556Srgrimes 5841556Srgrimesstruct job * 58590111Simpmakejob(union node *node __unused, int nprocs) 58617987Speter{ 5871556Srgrimes int i; 5881556Srgrimes struct job *jp; 5891556Srgrimes 5901556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 5911556Srgrimes if (--i < 0) { 5921556Srgrimes INTOFF; 5931556Srgrimes if (njobs == 0) { 5941556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 59597664Stjr#if JOBS 59697659Stjr jobmru = NULL; 59797664Stjr#endif 5981556Srgrimes } else { 5991556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 60017987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 60197659Stjr#if JOBS 60297659Stjr /* Relocate `next' pointers and list head */ 60399760Stjr if (jobmru != NULL) 60499760Stjr jobmru = &jp[jobmru - jobtab]; 60597659Stjr for (i = 0; i < njobs; i++) 60697659Stjr if (jp[i].next != NULL) 60797659Stjr jp[i].next = &jp[jp[i].next - 60897659Stjr jobtab]; 60997659Stjr#endif 61020425Ssteve /* Relocate `ps' pointers */ 61120425Ssteve for (i = 0; i < njobs; i++) 61220425Ssteve if (jp[i].ps == &jobtab[i].ps0) 61320425Ssteve jp[i].ps = &jp[i].ps0; 6141556Srgrimes ckfree(jobtab); 6151556Srgrimes jobtab = jp; 6161556Srgrimes } 6171556Srgrimes jp = jobtab + njobs; 6181556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 6191556Srgrimes INTON; 6201556Srgrimes break; 6211556Srgrimes } 6221556Srgrimes if (jp->used == 0) 6231556Srgrimes break; 6241556Srgrimes } 6251556Srgrimes INTOFF; 6261556Srgrimes jp->state = 0; 6271556Srgrimes jp->used = 1; 6281556Srgrimes jp->changed = 0; 6291556Srgrimes jp->nprocs = 0; 630100305Stjr jp->foreground = 0; 6311556Srgrimes#if JOBS 6321556Srgrimes jp->jobctl = jobctl; 63397659Stjr jp->next = NULL; 6341556Srgrimes#endif 6351556Srgrimes if (nprocs > 1) { 6361556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 6371556Srgrimes } else { 6381556Srgrimes jp->ps = &jp->ps0; 6391556Srgrimes } 6401556Srgrimes INTON; 64117987Speter TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, 64217987Speter jp - jobtab + 1)); 6431556Srgrimes return jp; 6448855Srgrimes} 6451556Srgrimes 64697659Stjr#if JOBS 64797659StjrSTATIC void 64897659Stjrsetcurjob(struct job *cj) 64997659Stjr{ 65097659Stjr struct job *jp, *prev; 6511556Srgrimes 65297659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 65397659Stjr if (jp == cj) { 65497659Stjr if (prev != NULL) 65597659Stjr prev->next = jp->next; 65697659Stjr else 65797659Stjr jobmru = jp->next; 65897659Stjr jp->next = jobmru; 65997659Stjr jobmru = cj; 66097659Stjr return; 66197659Stjr } 66297659Stjr } 66397659Stjr cj->next = jobmru; 66497659Stjr jobmru = cj; 66597659Stjr} 66697659Stjr 66797659StjrSTATIC void 66897659Stjrdeljob(struct job *j) 66997659Stjr{ 67097659Stjr struct job *jp, *prev; 67197659Stjr 67297659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 67397659Stjr if (jp == j) { 67497659Stjr if (prev != NULL) 67597659Stjr prev->next = jp->next; 67697659Stjr else 67797659Stjr jobmru = jp->next; 67897659Stjr return; 67997659Stjr } 68097659Stjr } 68197659Stjr} 68297659Stjr 6831556Srgrimes/* 68497659Stjr * Return the most recently used job that isn't `nj', and preferably one 68597659Stjr * that is stopped. 68697659Stjr */ 68797659StjrSTATIC struct job * 68897659Stjrgetcurjob(struct job *nj) 68997659Stjr{ 69097659Stjr struct job *jp; 69197659Stjr 69297659Stjr /* Try to find a stopped one.. */ 69397659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 69497659Stjr if (jp->used && jp != nj && jp->state == JOBSTOPPED) 69597659Stjr return (jp); 69697659Stjr /* Otherwise the most recently used job that isn't `nj' */ 69797659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 69897659Stjr if (jp->used && jp != nj) 69997659Stjr return (jp); 70097659Stjr 70197659Stjr return (NULL); 70297659Stjr} 70397659Stjr 70497659Stjr#endif 70597659Stjr 70697659Stjr/* 7071556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 7081556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 7091556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 7101556Srgrimes * be NULL. The mode parameter can be one of the following: 7111556Srgrimes * FORK_FG - Fork off a foreground process. 7121556Srgrimes * FORK_BG - Fork off a background process. 7131556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 7141556Srgrimes * process group even if job control is on. 7151556Srgrimes * 7161556Srgrimes * When job control is turned off, background processes have their standard 7171556Srgrimes * input redirected to /dev/null (except for the second and later processes 7181556Srgrimes * in a pipeline). 7191556Srgrimes */ 7201556Srgrimes 721100308Stjrpid_t 72290111Simpforkshell(struct job *jp, union node *n, int mode) 72317987Speter{ 724100308Stjr pid_t pid; 725100308Stjr pid_t pgrp; 7261556Srgrimes 72717987Speter TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n, 72817987Speter mode)); 7291556Srgrimes INTOFF; 7301556Srgrimes pid = fork(); 7311556Srgrimes if (pid == -1) { 7321556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 7331556Srgrimes INTON; 73453891Scracauer error("Cannot fork: %s", strerror(errno)); 7351556Srgrimes } 7361556Srgrimes if (pid == 0) { 7371556Srgrimes struct job *p; 7381556Srgrimes int wasroot; 7391556Srgrimes int i; 7401556Srgrimes 741100308Stjr TRACE(("Child shell %d\n", (int)getpid())); 7421556Srgrimes wasroot = rootshell; 7431556Srgrimes rootshell = 0; 7441556Srgrimes closescript(); 7451556Srgrimes INTON; 7461556Srgrimes clear_traps(); 7471556Srgrimes#if JOBS 7481556Srgrimes jobctl = 0; /* do job control only in root shell */ 7491556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 7501556Srgrimes if (jp == NULL || jp->nprocs == 0) 7511556Srgrimes pgrp = getpid(); 7521556Srgrimes else 7531556Srgrimes pgrp = jp->ps[0].pid; 75421352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 7551556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 75699762Stjr if (tcsetpgrp(ttyfd, pgrp) < 0) 75720425Ssteve error("tcsetpgrp failed, errno=%d", errno); 7581556Srgrimes } 7591556Srgrimes setsignal(SIGTSTP); 7601556Srgrimes setsignal(SIGTTOU); 7611556Srgrimes } else if (mode == FORK_BG) { 7621556Srgrimes ignoresig(SIGINT); 7631556Srgrimes ignoresig(SIGQUIT); 7641556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7651556Srgrimes ! fd0_redirected_p ()) { 7661556Srgrimes close(0); 76769793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 76869793Sobrien error("Can't open %s: %s", 76969793Sobrien _PATH_DEVNULL, strerror(errno)); 7701556Srgrimes } 7711556Srgrimes } 7721556Srgrimes#else 7731556Srgrimes if (mode == FORK_BG) { 7741556Srgrimes ignoresig(SIGINT); 7751556Srgrimes ignoresig(SIGQUIT); 7761556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7771556Srgrimes ! fd0_redirected_p ()) { 7781556Srgrimes close(0); 77969793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 78069793Sobrien error("Can't open %s: %s", 78169793Sobrien _PATH_DEVNULL, strerror(errno)); 7821556Srgrimes } 7831556Srgrimes } 7841556Srgrimes#endif 785102051Stjr INTOFF; 786102051Stjr for (i = njobs, p = jobtab ; --i >= 0 ; p++) 787102051Stjr if (p->used) 788102051Stjr freejob(p); 789102051Stjr INTON; 7901556Srgrimes if (wasroot && iflag) { 7911556Srgrimes setsignal(SIGINT); 7921556Srgrimes setsignal(SIGQUIT); 7931556Srgrimes setsignal(SIGTERM); 7941556Srgrimes } 7951556Srgrimes return pid; 7961556Srgrimes } 7971556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 7981556Srgrimes if (jp == NULL || jp->nprocs == 0) 7991556Srgrimes pgrp = pid; 8001556Srgrimes else 8011556Srgrimes pgrp = jp->ps[0].pid; 80217987Speter setpgid(pid, pgrp); 8031556Srgrimes } 8041556Srgrimes if (mode == FORK_BG) 8051556Srgrimes backgndpid = pid; /* set $! */ 8061556Srgrimes if (jp) { 8071556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 8081556Srgrimes ps->pid = pid; 8091556Srgrimes ps->status = -1; 8101556Srgrimes ps->cmd = nullstr; 8111556Srgrimes if (iflag && rootshell && n) 8121556Srgrimes ps->cmd = commandtext(n); 813100305Stjr jp->foreground = mode == FORK_FG; 81497659Stjr#if JOBS 81597659Stjr setcurjob(jp); 81697659Stjr#endif 8171556Srgrimes } 8181556Srgrimes INTON; 819100308Stjr TRACE(("In parent shell: child = %d\n", (int)pid)); 8201556Srgrimes return pid; 8211556Srgrimes} 8221556Srgrimes 8231556Srgrimes 8241556Srgrimes 8251556Srgrimes/* 8261556Srgrimes * Wait for job to finish. 8271556Srgrimes * 8281556Srgrimes * Under job control we have the problem that while a child process is 8291556Srgrimes * running interrupts generated by the user are sent to the child but not 8301556Srgrimes * to the shell. This means that an infinite loop started by an inter- 8311556Srgrimes * active user may be hard to kill. With job control turned off, an 8321556Srgrimes * interactive user may place an interactive program inside a loop. If 8331556Srgrimes * the interactive program catches interrupts, the user doesn't want 8341556Srgrimes * these interrupts to also abort the loop. The approach we take here 8351556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 83646684Skris * foreground process to terminate, and then send itself an interrupt 8371556Srgrimes * signal if the child process was terminated by an interrupt signal. 8381556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 8391556Srgrimes * exit on interrupt; unless these processes terminate themselves by 8401556Srgrimes * sending a signal to themselves (instead of calling exit) they will 8411556Srgrimes * confuse this approach. 8421556Srgrimes */ 8431556Srgrimes 8441556Srgrimesint 84590111Simpwaitforjob(struct job *jp, int *origstatus) 84645916Scracauer{ 8471556Srgrimes#if JOBS 848100308Stjr pid_t mypgrp = getpgrp(); 8491556Srgrimes#endif 8501556Srgrimes int status; 8511556Srgrimes int st; 8521556Srgrimes 8531556Srgrimes INTOFF; 8541556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 85538950Scracauer while (jp->state == 0) 85638950Scracauer if (dowait(1, jp) == -1) 85738950Scracauer dotrap(); 8581556Srgrimes#if JOBS 8591556Srgrimes if (jp->jobctl) { 86099762Stjr if (tcsetpgrp(ttyfd, mypgrp) < 0) 86120425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 8621556Srgrimes } 8631556Srgrimes if (jp->state == JOBSTOPPED) 86497659Stjr setcurjob(jp); 8651556Srgrimes#endif 8661556Srgrimes status = jp->ps[jp->nprocs - 1].status; 86745916Scracauer if (origstatus != NULL) 86845916Scracauer *origstatus = status; 8691556Srgrimes /* convert to 8 bits */ 87026104Ssteve if (WIFEXITED(status)) 87126104Ssteve st = WEXITSTATUS(status); 8721556Srgrimes#if JOBS 87326104Ssteve else if (WIFSTOPPED(status)) 87426104Ssteve st = WSTOPSIG(status) + 128; 8751556Srgrimes#endif 8761556Srgrimes else 87726104Ssteve st = WTERMSIG(status) + 128; 8781556Srgrimes if (! JOBS || jp->state == JOBDONE) 8791556Srgrimes freejob(jp); 88038521Scracauer if (int_pending()) { 88138521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 88238521Scracauer kill(getpid(), SIGINT); 88338521Scracauer else 88438521Scracauer CLEAR_PENDING_INT; 88538521Scracauer } 8861556Srgrimes INTON; 8871556Srgrimes return st; 8881556Srgrimes} 8891556Srgrimes 8901556Srgrimes 8911556Srgrimes 8921556Srgrimes/* 8931556Srgrimes * Wait for a process to terminate. 8941556Srgrimes */ 8951556Srgrimes 896100308StjrSTATIC pid_t 89790111Simpdowait(int block, struct job *job) 89817987Speter{ 899100308Stjr pid_t pid; 9001556Srgrimes int status; 9011556Srgrimes struct procstat *sp; 9021556Srgrimes struct job *jp; 9031556Srgrimes struct job *thisjob; 9041556Srgrimes int done; 9051556Srgrimes int stopped; 90626104Ssteve int sig; 907100305Stjr int i; 9081556Srgrimes 90938950Scracauer in_dowait++; 9101556Srgrimes TRACE(("dowait(%d) called\n", block)); 9111556Srgrimes do { 9121556Srgrimes pid = waitproc(block, &status); 913100308Stjr TRACE(("wait returns %d, status=%d\n", (int)pid, status)); 91472086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 91572086Scracauer (WIFSTOPPED(status) && !iflag)); 91638950Scracauer in_dowait--; 91738521Scracauer if (breakwaitcmd != 0) { 91838521Scracauer breakwaitcmd = 0; 91938521Scracauer return -1; 92038521Scracauer } 9211556Srgrimes if (pid <= 0) 9221556Srgrimes return pid; 9231556Srgrimes INTOFF; 9241556Srgrimes thisjob = NULL; 9251556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 9261556Srgrimes if (jp->used) { 9271556Srgrimes done = 1; 9281556Srgrimes stopped = 1; 9291556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 9301556Srgrimes if (sp->pid == -1) 9311556Srgrimes continue; 9321556Srgrimes if (sp->pid == pid) { 93326104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 934100308Stjr (int)pid, sp->status, 935100308Stjr status)); 9361556Srgrimes sp->status = status; 9371556Srgrimes thisjob = jp; 9381556Srgrimes } 9391556Srgrimes if (sp->status == -1) 9401556Srgrimes stopped = 0; 94126104Ssteve else if (WIFSTOPPED(sp->status)) 9421556Srgrimes done = 0; 9431556Srgrimes } 9441556Srgrimes if (stopped) { /* stopped or done */ 9451556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 9461556Srgrimes if (jp->state != state) { 9471556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 9481556Srgrimes jp->state = state; 9491556Srgrimes#if JOBS 95097659Stjr if (done) 95197659Stjr deljob(jp); 9521556Srgrimes#endif 9531556Srgrimes } 9541556Srgrimes } 9551556Srgrimes } 9561556Srgrimes } 9571556Srgrimes INTON; 9581556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 9591556Srgrimes#if JOBS 96026104Ssteve if (WIFSTOPPED(status)) 96126104Ssteve sig = WSTOPSIG(status); 96226104Ssteve else 9631556Srgrimes#endif 96497663Stjr { 96526104Ssteve if (WIFEXITED(status)) 96626104Ssteve sig = 0; 96726104Ssteve else 96826104Ssteve sig = WTERMSIG(status); 9691556Srgrimes } 970100305Stjr if (sig != 0 && sig != SIGINT && sig != SIGPIPE) { 971107846Stjr if (!mflag || 972107846Stjr (thisjob->foreground && !WIFSTOPPED(status))) { 973102007Stjr i = WTERMSIG(status); 974100305Stjr if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F]) 975100305Stjr out1str(sys_siglist[i & 0x7F]); 976100305Stjr else 977100305Stjr out1fmt("Signal %d", i & 0x7F); 978100305Stjr if (WCOREDUMP(status)) 979100305Stjr out1str(" (core dumped)"); 980100305Stjr out1c('\n'); 981100305Stjr } else 982102351Stjr showjob(thisjob, pid, 0, 0); 983100305Stjr } 9841556Srgrimes } else { 985109627Stjr TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job)); 9861556Srgrimes if (thisjob) 9871556Srgrimes thisjob->changed = 1; 9881556Srgrimes } 9891556Srgrimes return pid; 9901556Srgrimes} 9911556Srgrimes 9921556Srgrimes 9931556Srgrimes 9941556Srgrimes/* 9951556Srgrimes * Do a wait system call. If job control is compiled in, we accept 9961556Srgrimes * stopped processes. If block is zero, we return a value of zero 9971556Srgrimes * rather than blocking. 9981556Srgrimes */ 999100308StjrSTATIC pid_t 100090111Simpwaitproc(int block, int *status) 100117987Speter{ 10021556Srgrimes int flags; 10031556Srgrimes 10041556Srgrimes#if JOBS 10051556Srgrimes flags = WUNTRACED; 10061556Srgrimes#else 10071556Srgrimes flags = 0; 10081556Srgrimes#endif 10091556Srgrimes if (block == 0) 10101556Srgrimes flags |= WNOHANG; 10111556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 10121556Srgrimes} 10131556Srgrimes 10141556Srgrimes/* 10151556Srgrimes * return 1 if there are stopped jobs, otherwise 0 10161556Srgrimes */ 10171556Srgrimesint job_warning = 0; 10181556Srgrimesint 101990111Simpstoppedjobs(void) 10201556Srgrimes{ 102125222Ssteve int jobno; 102225222Ssteve struct job *jp; 10231556Srgrimes 10241556Srgrimes if (job_warning) 10251556Srgrimes return (0); 10261556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 10271556Srgrimes if (jp->used == 0) 10281556Srgrimes continue; 10291556Srgrimes if (jp->state == JOBSTOPPED) { 10301556Srgrimes out2str("You have stopped jobs.\n"); 10311556Srgrimes job_warning = 2; 10321556Srgrimes return (1); 10331556Srgrimes } 10341556Srgrimes } 10351556Srgrimes 10361556Srgrimes return (0); 10371556Srgrimes} 10381556Srgrimes 10391556Srgrimes/* 10401556Srgrimes * Return a string identifying a command (to be printed by the 10411556Srgrimes * jobs command. 10421556Srgrimes */ 10431556Srgrimes 10441556SrgrimesSTATIC char *cmdnextc; 10451556SrgrimesSTATIC int cmdnleft; 10461556Srgrimes#define MAXCMDTEXT 200 10471556Srgrimes 10481556Srgrimeschar * 104990111Simpcommandtext(union node *n) 105090111Simp{ 10511556Srgrimes char *name; 10521556Srgrimes 10531556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 10541556Srgrimes cmdnleft = MAXCMDTEXT - 4; 10551556Srgrimes cmdtxt(n); 10561556Srgrimes *cmdnextc = '\0'; 10571556Srgrimes return name; 10581556Srgrimes} 10591556Srgrimes 10601556Srgrimes 10611556SrgrimesSTATIC void 106290111Simpcmdtxt(union node *n) 106390111Simp{ 10641556Srgrimes union node *np; 10651556Srgrimes struct nodelist *lp; 10661556Srgrimes char *p; 10671556Srgrimes int i; 10681556Srgrimes char s[2]; 10691556Srgrimes 10701556Srgrimes if (n == NULL) 10711556Srgrimes return; 10721556Srgrimes switch (n->type) { 10731556Srgrimes case NSEMI: 10741556Srgrimes cmdtxt(n->nbinary.ch1); 10751556Srgrimes cmdputs("; "); 10761556Srgrimes cmdtxt(n->nbinary.ch2); 10771556Srgrimes break; 10781556Srgrimes case NAND: 10791556Srgrimes cmdtxt(n->nbinary.ch1); 10801556Srgrimes cmdputs(" && "); 10811556Srgrimes cmdtxt(n->nbinary.ch2); 10821556Srgrimes break; 10831556Srgrimes case NOR: 10841556Srgrimes cmdtxt(n->nbinary.ch1); 10851556Srgrimes cmdputs(" || "); 10861556Srgrimes cmdtxt(n->nbinary.ch2); 10871556Srgrimes break; 10881556Srgrimes case NPIPE: 10891556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 10901556Srgrimes cmdtxt(lp->n); 10911556Srgrimes if (lp->next) 10921556Srgrimes cmdputs(" | "); 10931556Srgrimes } 10941556Srgrimes break; 10951556Srgrimes case NSUBSHELL: 10961556Srgrimes cmdputs("("); 10971556Srgrimes cmdtxt(n->nredir.n); 10981556Srgrimes cmdputs(")"); 10991556Srgrimes break; 11001556Srgrimes case NREDIR: 11011556Srgrimes case NBACKGND: 11021556Srgrimes cmdtxt(n->nredir.n); 11031556Srgrimes break; 11041556Srgrimes case NIF: 11051556Srgrimes cmdputs("if "); 11061556Srgrimes cmdtxt(n->nif.test); 11071556Srgrimes cmdputs("; then "); 11081556Srgrimes cmdtxt(n->nif.ifpart); 11091556Srgrimes cmdputs("..."); 11101556Srgrimes break; 11111556Srgrimes case NWHILE: 11121556Srgrimes cmdputs("while "); 11131556Srgrimes goto until; 11141556Srgrimes case NUNTIL: 11151556Srgrimes cmdputs("until "); 11161556Srgrimesuntil: 11171556Srgrimes cmdtxt(n->nbinary.ch1); 11181556Srgrimes cmdputs("; do "); 11191556Srgrimes cmdtxt(n->nbinary.ch2); 11201556Srgrimes cmdputs("; done"); 11211556Srgrimes break; 11221556Srgrimes case NFOR: 11231556Srgrimes cmdputs("for "); 11241556Srgrimes cmdputs(n->nfor.var); 11251556Srgrimes cmdputs(" in ..."); 11261556Srgrimes break; 11271556Srgrimes case NCASE: 11281556Srgrimes cmdputs("case "); 11291556Srgrimes cmdputs(n->ncase.expr->narg.text); 11301556Srgrimes cmdputs(" in ..."); 11311556Srgrimes break; 11321556Srgrimes case NDEFUN: 11331556Srgrimes cmdputs(n->narg.text); 11341556Srgrimes cmdputs("() ..."); 11351556Srgrimes break; 11361556Srgrimes case NCMD: 11371556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 11381556Srgrimes cmdtxt(np); 11391556Srgrimes if (np->narg.next) 11401556Srgrimes cmdputs(" "); 11411556Srgrimes } 11421556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 11431556Srgrimes cmdputs(" "); 11441556Srgrimes cmdtxt(np); 11451556Srgrimes } 11461556Srgrimes break; 11471556Srgrimes case NARG: 11481556Srgrimes cmdputs(n->narg.text); 11491556Srgrimes break; 11501556Srgrimes case NTO: 11511556Srgrimes p = ">"; i = 1; goto redir; 11521556Srgrimes case NAPPEND: 11531556Srgrimes p = ">>"; i = 1; goto redir; 11541556Srgrimes case NTOFD: 11551556Srgrimes p = ">&"; i = 1; goto redir; 115696922Stjr case NCLOBBER: 115796922Stjr p = ">|"; i = 1; goto redir; 11581556Srgrimes case NFROM: 11591556Srgrimes p = "<"; i = 0; goto redir; 116066612Sbrian case NFROMTO: 116166612Sbrian p = "<>"; i = 0; goto redir; 11621556Srgrimes case NFROMFD: 11631556Srgrimes p = "<&"; i = 0; goto redir; 11641556Srgrimesredir: 11651556Srgrimes if (n->nfile.fd != i) { 11661556Srgrimes s[0] = n->nfile.fd + '0'; 11671556Srgrimes s[1] = '\0'; 11681556Srgrimes cmdputs(s); 11691556Srgrimes } 11701556Srgrimes cmdputs(p); 11711556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 117299634Stjr if (n->ndup.dupfd >= 0) 117399634Stjr s[0] = n->ndup.dupfd + '0'; 117499634Stjr else 117599634Stjr s[0] = '-'; 11761556Srgrimes s[1] = '\0'; 11771556Srgrimes cmdputs(s); 11781556Srgrimes } else { 11791556Srgrimes cmdtxt(n->nfile.fname); 11801556Srgrimes } 11811556Srgrimes break; 11821556Srgrimes case NHERE: 11831556Srgrimes case NXHERE: 11841556Srgrimes cmdputs("<<..."); 11851556Srgrimes break; 11861556Srgrimes default: 11871556Srgrimes cmdputs("???"); 11881556Srgrimes break; 11891556Srgrimes } 11901556Srgrimes} 11911556Srgrimes 11921556Srgrimes 11931556Srgrimes 11941556SrgrimesSTATIC void 119590111Simpcmdputs(char *s) 119690111Simp{ 119725222Ssteve char *p, *q; 119825222Ssteve char c; 11991556Srgrimes int subtype = 0; 12001556Srgrimes 12011556Srgrimes if (cmdnleft <= 0) 12021556Srgrimes return; 12031556Srgrimes p = s; 12041556Srgrimes q = cmdnextc; 12051556Srgrimes while ((c = *p++) != '\0') { 12061556Srgrimes if (c == CTLESC) 12071556Srgrimes *q++ = *p++; 12081556Srgrimes else if (c == CTLVAR) { 12091556Srgrimes *q++ = '$'; 12101556Srgrimes if (--cmdnleft > 0) 12111556Srgrimes *q++ = '{'; 12121556Srgrimes subtype = *p++; 12131556Srgrimes } else if (c == '=' && subtype != 0) { 12141556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 12151556Srgrimes subtype = 0; 12161556Srgrimes } else if (c == CTLENDVAR) { 12171556Srgrimes *q++ = '}'; 121818954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 12191556Srgrimes cmdnleft++; /* ignore it */ 12201556Srgrimes else 12211556Srgrimes *q++ = c; 12221556Srgrimes if (--cmdnleft <= 0) { 12231556Srgrimes *q++ = '.'; 12241556Srgrimes *q++ = '.'; 12251556Srgrimes *q++ = '.'; 12261556Srgrimes break; 12271556Srgrimes } 12281556Srgrimes } 12291556Srgrimes cmdnextc = q; 12301556Srgrimes} 1231