jobs.c revision 125501
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 125501 2004-02-05 22:34:05Z cracauer $"); 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 79117261SddsSTATIC struct job *jobtab; /* array of jobs */ 80117261SddsSTATIC int njobs; /* size of array */ 8128346SsteveMKINIT pid_t backgndpid = -1; /* pid of last background process */ 821556Srgrimes#if JOBS 83117261SddsSTATIC struct job *jobmru; /* most recently used job list */ 84117261SddsSTATIC pid_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); 365125155Snjl if ((i & 0x7F) < sys_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; 743112341Stjr flushall(); 7441556Srgrimes pid = fork(); 7451556Srgrimes if (pid == -1) { 7461556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 7471556Srgrimes INTON; 74853891Scracauer error("Cannot fork: %s", strerror(errno)); 7491556Srgrimes } 7501556Srgrimes if (pid == 0) { 7511556Srgrimes struct job *p; 7521556Srgrimes int wasroot; 7531556Srgrimes int i; 7541556Srgrimes 755100308Stjr TRACE(("Child shell %d\n", (int)getpid())); 7561556Srgrimes wasroot = rootshell; 7571556Srgrimes rootshell = 0; 7581556Srgrimes closescript(); 7591556Srgrimes INTON; 7601556Srgrimes clear_traps(); 7611556Srgrimes#if JOBS 7621556Srgrimes jobctl = 0; /* do job control only in root shell */ 7631556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 7641556Srgrimes if (jp == NULL || jp->nprocs == 0) 7651556Srgrimes pgrp = getpid(); 7661556Srgrimes else 7671556Srgrimes pgrp = jp->ps[0].pid; 76821352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 7691556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 77099762Stjr if (tcsetpgrp(ttyfd, pgrp) < 0) 77120425Ssteve error("tcsetpgrp failed, errno=%d", errno); 7721556Srgrimes } 7731556Srgrimes setsignal(SIGTSTP); 7741556Srgrimes setsignal(SIGTTOU); 7751556Srgrimes } else if (mode == FORK_BG) { 7761556Srgrimes ignoresig(SIGINT); 7771556Srgrimes ignoresig(SIGQUIT); 7781556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7791556Srgrimes ! fd0_redirected_p ()) { 7801556Srgrimes close(0); 78169793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 78269793Sobrien error("Can't open %s: %s", 78369793Sobrien _PATH_DEVNULL, strerror(errno)); 7841556Srgrimes } 7851556Srgrimes } 7861556Srgrimes#else 7871556Srgrimes if (mode == FORK_BG) { 7881556Srgrimes ignoresig(SIGINT); 7891556Srgrimes ignoresig(SIGQUIT); 7901556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7911556Srgrimes ! fd0_redirected_p ()) { 7921556Srgrimes close(0); 79369793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 79469793Sobrien error("Can't open %s: %s", 79569793Sobrien _PATH_DEVNULL, strerror(errno)); 7961556Srgrimes } 7971556Srgrimes } 7981556Srgrimes#endif 799102051Stjr INTOFF; 800102051Stjr for (i = njobs, p = jobtab ; --i >= 0 ; p++) 801102051Stjr if (p->used) 802102051Stjr freejob(p); 803102051Stjr INTON; 8041556Srgrimes if (wasroot && iflag) { 8051556Srgrimes setsignal(SIGINT); 8061556Srgrimes setsignal(SIGQUIT); 8071556Srgrimes setsignal(SIGTERM); 8081556Srgrimes } 8091556Srgrimes return pid; 8101556Srgrimes } 8111556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 8121556Srgrimes if (jp == NULL || jp->nprocs == 0) 8131556Srgrimes pgrp = pid; 8141556Srgrimes else 8151556Srgrimes pgrp = jp->ps[0].pid; 81617987Speter setpgid(pid, pgrp); 8171556Srgrimes } 8181556Srgrimes if (mode == FORK_BG) 8191556Srgrimes backgndpid = pid; /* set $! */ 8201556Srgrimes if (jp) { 8211556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 8221556Srgrimes ps->pid = pid; 8231556Srgrimes ps->status = -1; 8241556Srgrimes ps->cmd = nullstr; 8251556Srgrimes if (iflag && rootshell && n) 8261556Srgrimes ps->cmd = commandtext(n); 827100305Stjr jp->foreground = mode == FORK_FG; 82897659Stjr#if JOBS 82997659Stjr setcurjob(jp); 83097659Stjr#endif 8311556Srgrimes } 8321556Srgrimes INTON; 833100308Stjr TRACE(("In parent shell: child = %d\n", (int)pid)); 8341556Srgrimes return pid; 8351556Srgrimes} 8361556Srgrimes 8371556Srgrimes 8381556Srgrimes 8391556Srgrimes/* 8401556Srgrimes * Wait for job to finish. 8411556Srgrimes * 8421556Srgrimes * Under job control we have the problem that while a child process is 8431556Srgrimes * running interrupts generated by the user are sent to the child but not 8441556Srgrimes * to the shell. This means that an infinite loop started by an inter- 8451556Srgrimes * active user may be hard to kill. With job control turned off, an 8461556Srgrimes * interactive user may place an interactive program inside a loop. If 8471556Srgrimes * the interactive program catches interrupts, the user doesn't want 8481556Srgrimes * these interrupts to also abort the loop. The approach we take here 8491556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 85046684Skris * foreground process to terminate, and then send itself an interrupt 8511556Srgrimes * signal if the child process was terminated by an interrupt signal. 8521556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 8531556Srgrimes * exit on interrupt; unless these processes terminate themselves by 8541556Srgrimes * sending a signal to themselves (instead of calling exit) they will 8551556Srgrimes * confuse this approach. 8561556Srgrimes */ 8571556Srgrimes 8581556Srgrimesint 85990111Simpwaitforjob(struct job *jp, int *origstatus) 86045916Scracauer{ 8611556Srgrimes#if JOBS 862100308Stjr pid_t mypgrp = getpgrp(); 8631556Srgrimes#endif 8641556Srgrimes int status; 8651556Srgrimes int st; 8661556Srgrimes 8671556Srgrimes INTOFF; 8681556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 86938950Scracauer while (jp->state == 0) 87038950Scracauer if (dowait(1, jp) == -1) 87138950Scracauer dotrap(); 8721556Srgrimes#if JOBS 8731556Srgrimes if (jp->jobctl) { 87499762Stjr if (tcsetpgrp(ttyfd, mypgrp) < 0) 87520425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 8761556Srgrimes } 8771556Srgrimes if (jp->state == JOBSTOPPED) 87897659Stjr setcurjob(jp); 8791556Srgrimes#endif 8801556Srgrimes status = jp->ps[jp->nprocs - 1].status; 88145916Scracauer if (origstatus != NULL) 88245916Scracauer *origstatus = status; 8831556Srgrimes /* convert to 8 bits */ 88426104Ssteve if (WIFEXITED(status)) 88526104Ssteve st = WEXITSTATUS(status); 8861556Srgrimes#if JOBS 88726104Ssteve else if (WIFSTOPPED(status)) 88826104Ssteve st = WSTOPSIG(status) + 128; 8891556Srgrimes#endif 8901556Srgrimes else 89126104Ssteve st = WTERMSIG(status) + 128; 8921556Srgrimes if (! JOBS || jp->state == JOBDONE) 8931556Srgrimes freejob(jp); 89438521Scracauer if (int_pending()) { 89538521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 89638521Scracauer kill(getpid(), SIGINT); 89738521Scracauer else 89838521Scracauer CLEAR_PENDING_INT; 89938521Scracauer } 9001556Srgrimes INTON; 9011556Srgrimes return st; 9021556Srgrimes} 9031556Srgrimes 9041556Srgrimes 9051556Srgrimes 9061556Srgrimes/* 9071556Srgrimes * Wait for a process to terminate. 9081556Srgrimes */ 9091556Srgrimes 910100308StjrSTATIC pid_t 91190111Simpdowait(int block, struct job *job) 91217987Speter{ 913100308Stjr pid_t pid; 9141556Srgrimes int status; 9151556Srgrimes struct procstat *sp; 9161556Srgrimes struct job *jp; 9171556Srgrimes struct job *thisjob; 9181556Srgrimes int done; 9191556Srgrimes int stopped; 92026104Ssteve int sig; 921100305Stjr int i; 9221556Srgrimes 92338950Scracauer in_dowait++; 9241556Srgrimes TRACE(("dowait(%d) called\n", block)); 9251556Srgrimes do { 9261556Srgrimes pid = waitproc(block, &status); 927100308Stjr TRACE(("wait returns %d, status=%d\n", (int)pid, status)); 92872086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 929125501Scracauer (pid > 0 && WIFSTOPPED(status) && !iflag)); 93038950Scracauer in_dowait--; 93138521Scracauer if (breakwaitcmd != 0) { 93238521Scracauer breakwaitcmd = 0; 93338521Scracauer return -1; 93438521Scracauer } 9351556Srgrimes if (pid <= 0) 9361556Srgrimes return pid; 9371556Srgrimes INTOFF; 9381556Srgrimes thisjob = NULL; 9391556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 9401556Srgrimes if (jp->used) { 9411556Srgrimes done = 1; 9421556Srgrimes stopped = 1; 9431556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 9441556Srgrimes if (sp->pid == -1) 9451556Srgrimes continue; 9461556Srgrimes if (sp->pid == pid) { 94726104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 948100308Stjr (int)pid, sp->status, 949100308Stjr status)); 9501556Srgrimes sp->status = status; 9511556Srgrimes thisjob = jp; 9521556Srgrimes } 9531556Srgrimes if (sp->status == -1) 9541556Srgrimes stopped = 0; 95526104Ssteve else if (WIFSTOPPED(sp->status)) 9561556Srgrimes done = 0; 9571556Srgrimes } 9581556Srgrimes if (stopped) { /* stopped or done */ 9591556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 9601556Srgrimes if (jp->state != state) { 9611556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 9621556Srgrimes jp->state = state; 9631556Srgrimes#if JOBS 96497659Stjr if (done) 96597659Stjr deljob(jp); 9661556Srgrimes#endif 9671556Srgrimes } 9681556Srgrimes } 9691556Srgrimes } 9701556Srgrimes } 9711556Srgrimes INTON; 9721556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 9731556Srgrimes#if JOBS 97426104Ssteve if (WIFSTOPPED(status)) 97526104Ssteve sig = WSTOPSIG(status); 97626104Ssteve else 9771556Srgrimes#endif 97897663Stjr { 97926104Ssteve if (WIFEXITED(status)) 98026104Ssteve sig = 0; 98126104Ssteve else 98226104Ssteve sig = WTERMSIG(status); 9831556Srgrimes } 984100305Stjr if (sig != 0 && sig != SIGINT && sig != SIGPIPE) { 985107846Stjr if (!mflag || 986107846Stjr (thisjob->foreground && !WIFSTOPPED(status))) { 987102007Stjr i = WTERMSIG(status); 988125155Snjl if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F]) 989100305Stjr out1str(sys_siglist[i & 0x7F]); 990100305Stjr else 991100305Stjr out1fmt("Signal %d", i & 0x7F); 992100305Stjr if (WCOREDUMP(status)) 993100305Stjr out1str(" (core dumped)"); 994100305Stjr out1c('\n'); 995100305Stjr } else 996102351Stjr showjob(thisjob, pid, 0, 0); 997100305Stjr } 9981556Srgrimes } else { 999109627Stjr TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job)); 10001556Srgrimes if (thisjob) 10011556Srgrimes thisjob->changed = 1; 10021556Srgrimes } 10031556Srgrimes return pid; 10041556Srgrimes} 10051556Srgrimes 10061556Srgrimes 10071556Srgrimes 10081556Srgrimes/* 10091556Srgrimes * Do a wait system call. If job control is compiled in, we accept 10101556Srgrimes * stopped processes. If block is zero, we return a value of zero 10111556Srgrimes * rather than blocking. 10121556Srgrimes */ 1013100308StjrSTATIC pid_t 101490111Simpwaitproc(int block, int *status) 101517987Speter{ 10161556Srgrimes int flags; 10171556Srgrimes 10181556Srgrimes#if JOBS 10191556Srgrimes flags = WUNTRACED; 10201556Srgrimes#else 10211556Srgrimes flags = 0; 10221556Srgrimes#endif 10231556Srgrimes if (block == 0) 10241556Srgrimes flags |= WNOHANG; 10251556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 10261556Srgrimes} 10271556Srgrimes 10281556Srgrimes/* 10291556Srgrimes * return 1 if there are stopped jobs, otherwise 0 10301556Srgrimes */ 10311556Srgrimesint job_warning = 0; 10321556Srgrimesint 103390111Simpstoppedjobs(void) 10341556Srgrimes{ 103525222Ssteve int jobno; 103625222Ssteve struct job *jp; 10371556Srgrimes 10381556Srgrimes if (job_warning) 10391556Srgrimes return (0); 10401556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 10411556Srgrimes if (jp->used == 0) 10421556Srgrimes continue; 10431556Srgrimes if (jp->state == JOBSTOPPED) { 10441556Srgrimes out2str("You have stopped jobs.\n"); 10451556Srgrimes job_warning = 2; 10461556Srgrimes return (1); 10471556Srgrimes } 10481556Srgrimes } 10491556Srgrimes 10501556Srgrimes return (0); 10511556Srgrimes} 10521556Srgrimes 10531556Srgrimes/* 10541556Srgrimes * Return a string identifying a command (to be printed by the 10551556Srgrimes * jobs command. 10561556Srgrimes */ 10571556Srgrimes 10581556SrgrimesSTATIC char *cmdnextc; 10591556SrgrimesSTATIC int cmdnleft; 10601556Srgrimes#define MAXCMDTEXT 200 10611556Srgrimes 10621556Srgrimeschar * 106390111Simpcommandtext(union node *n) 106490111Simp{ 10651556Srgrimes char *name; 10661556Srgrimes 10671556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 10681556Srgrimes cmdnleft = MAXCMDTEXT - 4; 10691556Srgrimes cmdtxt(n); 10701556Srgrimes *cmdnextc = '\0'; 10711556Srgrimes return name; 10721556Srgrimes} 10731556Srgrimes 10741556Srgrimes 10751556SrgrimesSTATIC void 107690111Simpcmdtxt(union node *n) 107790111Simp{ 10781556Srgrimes union node *np; 10791556Srgrimes struct nodelist *lp; 10801556Srgrimes char *p; 10811556Srgrimes int i; 10821556Srgrimes char s[2]; 10831556Srgrimes 10841556Srgrimes if (n == NULL) 10851556Srgrimes return; 10861556Srgrimes switch (n->type) { 10871556Srgrimes case NSEMI: 10881556Srgrimes cmdtxt(n->nbinary.ch1); 10891556Srgrimes cmdputs("; "); 10901556Srgrimes cmdtxt(n->nbinary.ch2); 10911556Srgrimes break; 10921556Srgrimes case NAND: 10931556Srgrimes cmdtxt(n->nbinary.ch1); 10941556Srgrimes cmdputs(" && "); 10951556Srgrimes cmdtxt(n->nbinary.ch2); 10961556Srgrimes break; 10971556Srgrimes case NOR: 10981556Srgrimes cmdtxt(n->nbinary.ch1); 10991556Srgrimes cmdputs(" || "); 11001556Srgrimes cmdtxt(n->nbinary.ch2); 11011556Srgrimes break; 11021556Srgrimes case NPIPE: 11031556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 11041556Srgrimes cmdtxt(lp->n); 11051556Srgrimes if (lp->next) 11061556Srgrimes cmdputs(" | "); 11071556Srgrimes } 11081556Srgrimes break; 11091556Srgrimes case NSUBSHELL: 11101556Srgrimes cmdputs("("); 11111556Srgrimes cmdtxt(n->nredir.n); 11121556Srgrimes cmdputs(")"); 11131556Srgrimes break; 11141556Srgrimes case NREDIR: 11151556Srgrimes case NBACKGND: 11161556Srgrimes cmdtxt(n->nredir.n); 11171556Srgrimes break; 11181556Srgrimes case NIF: 11191556Srgrimes cmdputs("if "); 11201556Srgrimes cmdtxt(n->nif.test); 11211556Srgrimes cmdputs("; then "); 11221556Srgrimes cmdtxt(n->nif.ifpart); 11231556Srgrimes cmdputs("..."); 11241556Srgrimes break; 11251556Srgrimes case NWHILE: 11261556Srgrimes cmdputs("while "); 11271556Srgrimes goto until; 11281556Srgrimes case NUNTIL: 11291556Srgrimes cmdputs("until "); 11301556Srgrimesuntil: 11311556Srgrimes cmdtxt(n->nbinary.ch1); 11321556Srgrimes cmdputs("; do "); 11331556Srgrimes cmdtxt(n->nbinary.ch2); 11341556Srgrimes cmdputs("; done"); 11351556Srgrimes break; 11361556Srgrimes case NFOR: 11371556Srgrimes cmdputs("for "); 11381556Srgrimes cmdputs(n->nfor.var); 11391556Srgrimes cmdputs(" in ..."); 11401556Srgrimes break; 11411556Srgrimes case NCASE: 11421556Srgrimes cmdputs("case "); 11431556Srgrimes cmdputs(n->ncase.expr->narg.text); 11441556Srgrimes cmdputs(" in ..."); 11451556Srgrimes break; 11461556Srgrimes case NDEFUN: 11471556Srgrimes cmdputs(n->narg.text); 11481556Srgrimes cmdputs("() ..."); 11491556Srgrimes break; 11501556Srgrimes case NCMD: 11511556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 11521556Srgrimes cmdtxt(np); 11531556Srgrimes if (np->narg.next) 11541556Srgrimes cmdputs(" "); 11551556Srgrimes } 11561556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 11571556Srgrimes cmdputs(" "); 11581556Srgrimes cmdtxt(np); 11591556Srgrimes } 11601556Srgrimes break; 11611556Srgrimes case NARG: 11621556Srgrimes cmdputs(n->narg.text); 11631556Srgrimes break; 11641556Srgrimes case NTO: 11651556Srgrimes p = ">"; i = 1; goto redir; 11661556Srgrimes case NAPPEND: 11671556Srgrimes p = ">>"; i = 1; goto redir; 11681556Srgrimes case NTOFD: 11691556Srgrimes p = ">&"; i = 1; goto redir; 117096922Stjr case NCLOBBER: 117196922Stjr p = ">|"; i = 1; goto redir; 11721556Srgrimes case NFROM: 11731556Srgrimes p = "<"; i = 0; goto redir; 117466612Sbrian case NFROMTO: 117566612Sbrian p = "<>"; i = 0; goto redir; 11761556Srgrimes case NFROMFD: 11771556Srgrimes p = "<&"; i = 0; goto redir; 11781556Srgrimesredir: 11791556Srgrimes if (n->nfile.fd != i) { 11801556Srgrimes s[0] = n->nfile.fd + '0'; 11811556Srgrimes s[1] = '\0'; 11821556Srgrimes cmdputs(s); 11831556Srgrimes } 11841556Srgrimes cmdputs(p); 11851556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 118699634Stjr if (n->ndup.dupfd >= 0) 118799634Stjr s[0] = n->ndup.dupfd + '0'; 118899634Stjr else 118999634Stjr s[0] = '-'; 11901556Srgrimes s[1] = '\0'; 11911556Srgrimes cmdputs(s); 11921556Srgrimes } else { 11931556Srgrimes cmdtxt(n->nfile.fname); 11941556Srgrimes } 11951556Srgrimes break; 11961556Srgrimes case NHERE: 11971556Srgrimes case NXHERE: 11981556Srgrimes cmdputs("<<..."); 11991556Srgrimes break; 12001556Srgrimes default: 12011556Srgrimes cmdputs("???"); 12021556Srgrimes break; 12031556Srgrimes } 12041556Srgrimes} 12051556Srgrimes 12061556Srgrimes 12071556Srgrimes 12081556SrgrimesSTATIC void 120990111Simpcmdputs(char *s) 121090111Simp{ 121125222Ssteve char *p, *q; 121225222Ssteve char c; 12131556Srgrimes int subtype = 0; 12141556Srgrimes 12151556Srgrimes if (cmdnleft <= 0) 12161556Srgrimes return; 12171556Srgrimes p = s; 12181556Srgrimes q = cmdnextc; 12191556Srgrimes while ((c = *p++) != '\0') { 12201556Srgrimes if (c == CTLESC) 12211556Srgrimes *q++ = *p++; 12221556Srgrimes else if (c == CTLVAR) { 12231556Srgrimes *q++ = '$'; 12241556Srgrimes if (--cmdnleft > 0) 12251556Srgrimes *q++ = '{'; 12261556Srgrimes subtype = *p++; 12271556Srgrimes } else if (c == '=' && subtype != 0) { 12281556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 12291556Srgrimes subtype = 0; 12301556Srgrimes } else if (c == CTLENDVAR) { 12311556Srgrimes *q++ = '}'; 123218954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 12331556Srgrimes cmdnleft++; /* ignore it */ 12341556Srgrimes else 12351556Srgrimes *q++ = c; 12361556Srgrimes if (--cmdnleft <= 0) { 12371556Srgrimes *q++ = '.'; 12381556Srgrimes *q++ = '.'; 12391556Srgrimes *q++ = '.'; 12401556Srgrimes break; 12411556Srgrimes } 12421556Srgrimes } 12431556Srgrimes cmdnextc = q; 12441556Srgrimes} 1245