jobs.c revision 38536
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 4136150Scharnierstatic const char rcsid[] = 4238536Scracauer "$Id: jobs.c,v 1.21 1998/08/24 10:20:36 cracauer Exp $"; 431556Srgrimes#endif /* not lint */ 441556Srgrimes 4517987Speter#include <fcntl.h> 4617987Speter#include <signal.h> 4717987Speter#include <errno.h> 4817987Speter#include <unistd.h> 4917987Speter#include <stdlib.h> 5017987Speter#include <sys/param.h> 5117987Speter#ifdef BSD 5217987Speter#include <sys/wait.h> 5317987Speter#include <sys/time.h> 5417987Speter#include <sys/resource.h> 5517987Speter#endif 5618018Speter#include <sys/ioctl.h> 5717987Speter 581556Srgrimes#include "shell.h" 591556Srgrimes#if JOBS 6025222Ssteve#if OLD_TTY_DRIVER 611556Srgrimes#include "sgtty.h" 6217987Speter#else 6317987Speter#include <termios.h> 6417987Speter#endif 651556Srgrimes#undef CEOF /* syntax.h redefines this */ 661556Srgrimes#endif 6717987Speter#include "redir.h" 6817987Speter#include "show.h" 691556Srgrimes#include "main.h" 701556Srgrimes#include "parser.h" 711556Srgrimes#include "nodes.h" 721556Srgrimes#include "jobs.h" 731556Srgrimes#include "options.h" 741556Srgrimes#include "trap.h" 751556Srgrimes#include "syntax.h" 761556Srgrimes#include "input.h" 771556Srgrimes#include "output.h" 781556Srgrimes#include "memalloc.h" 791556Srgrimes#include "error.h" 801556Srgrimes#include "mystring.h" 811556Srgrimes 821556Srgrimes 831556Srgrimesstruct job *jobtab; /* array of jobs */ 841556Srgrimesint njobs; /* size of array */ 8528346SsteveMKINIT pid_t backgndpid = -1; /* pid of last background process */ 861556Srgrimes#if JOBS 871556Srgrimesint initialpgrp; /* pgrp of shell on invocation */ 8828346Ssteveint curjob; /* current job */ 891556Srgrimes#endif 9038536Scracauerint in_waitcmd = 0; /* are we in waitcmd()? */ 9138536Scracauervolatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */ 921556Srgrimes 9320425Ssteve#if JOBS 9417987SpeterSTATIC void restartjob __P((struct job *)); 9520425Ssteve#endif 9617987SpeterSTATIC void freejob __P((struct job *)); 9717987SpeterSTATIC struct job *getjob __P((char *)); 9817987SpeterSTATIC int dowait __P((int, struct job *)); 9920425Ssteve#if SYSV 10038536ScracauerSTATIC int onsigchild __P((void)); 10120425Ssteve#endif 10217987SpeterSTATIC int waitproc __P((int, int *)); 10317987SpeterSTATIC void cmdtxt __P((union node *)); 10417987SpeterSTATIC void cmdputs __P((char *)); 1051556Srgrimes 1061556Srgrimes 1071556Srgrimes/* 1081556Srgrimes * Turn job control on and off. 1091556Srgrimes * 1101556Srgrimes * Note: This code assumes that the third arg to ioctl is a character 1111556Srgrimes * pointer, which is true on Berkeley systems but not System V. Since 1121556Srgrimes * System V doesn't have job control yet, this isn't a problem now. 1131556Srgrimes */ 1141556Srgrimes 1151556SrgrimesMKINIT int jobctl; 1161556Srgrimes 11720425Ssteve#if JOBS 1181556Srgrimesvoid 11920425Sstevesetjobctl(on) 12017987Speter int on; 12117987Speter{ 1221556Srgrimes#ifdef OLD_TTY_DRIVER 1231556Srgrimes int ldisc; 1241556Srgrimes#endif 1251556Srgrimes 1261556Srgrimes if (on == jobctl || rootshell == 0) 1271556Srgrimes return; 1281556Srgrimes if (on) { 1291556Srgrimes do { /* while we are in the background */ 13020425Ssteve#ifdef OLD_TTY_DRIVER 1311556Srgrimes if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) { 13220425Ssteve#else 13320425Ssteve initialpgrp = tcgetpgrp(2); 13420425Ssteve if (initialpgrp < 0) { 13520425Ssteve#endif 1361556Srgrimes out2str("sh: can't access tty; job control turned off\n"); 1371556Srgrimes mflag = 0; 1381556Srgrimes return; 1391556Srgrimes } 1401556Srgrimes if (initialpgrp == -1) 14117987Speter initialpgrp = getpgrp(); 14217987Speter else if (initialpgrp != getpgrp()) { 1431556Srgrimes killpg(initialpgrp, SIGTTIN); 1441556Srgrimes continue; 1451556Srgrimes } 1461556Srgrimes } while (0); 1471556Srgrimes#ifdef OLD_TTY_DRIVER 1481556Srgrimes if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) { 1491556Srgrimes out2str("sh: need new tty driver to run job control; job control turned off\n"); 1501556Srgrimes mflag = 0; 1511556Srgrimes return; 1521556Srgrimes } 1531556Srgrimes#endif 1541556Srgrimes setsignal(SIGTSTP); 1551556Srgrimes setsignal(SIGTTOU); 1561556Srgrimes setsignal(SIGTTIN); 15717987Speter setpgid(0, rootpid); 15820425Ssteve#ifdef OLD_TTY_DRIVER 1591556Srgrimes ioctl(2, TIOCSPGRP, (char *)&rootpid); 16020425Ssteve#else 16120425Ssteve tcsetpgrp(2, rootpid); 16220425Ssteve#endif 1631556Srgrimes } else { /* turning job control off */ 16417987Speter setpgid(0, initialpgrp); 16520425Ssteve#ifdef OLD_TTY_DRIVER 1661556Srgrimes ioctl(2, TIOCSPGRP, (char *)&initialpgrp); 16720425Ssteve#else 16820425Ssteve tcsetpgrp(2, initialpgrp); 16920425Ssteve#endif 1701556Srgrimes setsignal(SIGTSTP); 1711556Srgrimes setsignal(SIGTTOU); 1721556Srgrimes setsignal(SIGTTIN); 1731556Srgrimes } 1741556Srgrimes jobctl = on; 1751556Srgrimes} 17620425Ssteve#endif 1771556Srgrimes 1781556Srgrimes 1791556Srgrimes#ifdef mkinit 18028346SsteveINCLUDE <sys/types.h> 18117987SpeterINCLUDE <stdlib.h> 1821556Srgrimes 1831556SrgrimesSHELLPROC { 1841556Srgrimes backgndpid = -1; 1851556Srgrimes#if JOBS 1861556Srgrimes jobctl = 0; 1871556Srgrimes#endif 1881556Srgrimes} 1891556Srgrimes 1901556Srgrimes#endif 1911556Srgrimes 1921556Srgrimes 1931556Srgrimes 1941556Srgrimes#if JOBS 19517987Speterint 19617987Speterfgcmd(argc, argv) 19725905Ssteve int argc __unused; 19820425Ssteve char **argv; 19917987Speter{ 2001556Srgrimes struct job *jp; 2011556Srgrimes int pgrp; 2021556Srgrimes int status; 2031556Srgrimes 2041556Srgrimes jp = getjob(argv[1]); 2051556Srgrimes if (jp->jobctl == 0) 2061556Srgrimes error("job not created under job control"); 2071556Srgrimes pgrp = jp->ps[0].pid; 20820425Ssteve#ifdef OLD_TTY_DRIVER 2091556Srgrimes ioctl(2, TIOCSPGRP, (char *)&pgrp); 21020425Ssteve#else 21120425Ssteve tcsetpgrp(2, pgrp); 21220425Ssteve#endif 2131556Srgrimes restartjob(jp); 2141556Srgrimes INTOFF; 2151556Srgrimes status = waitforjob(jp); 2161556Srgrimes INTON; 2171556Srgrimes return status; 2181556Srgrimes} 2191556Srgrimes 2201556Srgrimes 22117987Speterint 22217987Speterbgcmd(argc, argv) 22317987Speter int argc; 22420425Ssteve char **argv; 22517987Speter{ 2261556Srgrimes struct job *jp; 2271556Srgrimes 2281556Srgrimes do { 2291556Srgrimes jp = getjob(*++argv); 2301556Srgrimes if (jp->jobctl == 0) 2311556Srgrimes error("job not created under job control"); 2321556Srgrimes restartjob(jp); 2331556Srgrimes } while (--argc > 1); 2341556Srgrimes return 0; 2351556Srgrimes} 2361556Srgrimes 2371556Srgrimes 2381556SrgrimesSTATIC void 2391556Srgrimesrestartjob(jp) 2401556Srgrimes struct job *jp; 24117987Speter{ 2421556Srgrimes struct procstat *ps; 2431556Srgrimes int i; 2441556Srgrimes 2451556Srgrimes if (jp->state == JOBDONE) 2461556Srgrimes return; 2471556Srgrimes INTOFF; 2481556Srgrimes killpg(jp->ps[0].pid, SIGCONT); 2491556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 25026104Ssteve if (WIFSTOPPED(ps->status)) { 2511556Srgrimes ps->status = -1; 2521556Srgrimes jp->state = 0; 2531556Srgrimes } 2541556Srgrimes } 2551556Srgrimes INTON; 2561556Srgrimes} 2571556Srgrimes#endif 2581556Srgrimes 2591556Srgrimes 2601556Srgrimesint 26117987Speterjobscmd(argc, argv) 26225905Ssteve int argc __unused; 26325905Ssteve char **argv __unused; 26417987Speter{ 2651556Srgrimes showjobs(0); 2661556Srgrimes return 0; 2671556Srgrimes} 2681556Srgrimes 2691556Srgrimes 2701556Srgrimes/* 2711556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 2721556Srgrimes * statuses have changed since the last call to showjobs. 2731556Srgrimes * 2741556Srgrimes * If the shell is interrupted in the process of creating a job, the 2751556Srgrimes * result may be a job structure containing zero processes. Such structures 2761556Srgrimes * will be freed here. 2771556Srgrimes */ 2781556Srgrimes 2791556Srgrimesvoid 28020425Ssteveshowjobs(change) 28117987Speter int change; 28217987Speter{ 2831556Srgrimes int jobno; 2841556Srgrimes int procno; 2851556Srgrimes int i; 2861556Srgrimes struct job *jp; 2871556Srgrimes struct procstat *ps; 2881556Srgrimes int col; 2891556Srgrimes char s[64]; 2901556Srgrimes 2911556Srgrimes TRACE(("showjobs(%d) called\n", change)); 2921556Srgrimes while (dowait(0, (struct job *)NULL) > 0); 2931556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 2941556Srgrimes if (! jp->used) 2951556Srgrimes continue; 2961556Srgrimes if (jp->nprocs == 0) { 2971556Srgrimes freejob(jp); 2981556Srgrimes continue; 2991556Srgrimes } 3001556Srgrimes if (change && ! jp->changed) 3011556Srgrimes continue; 3021556Srgrimes procno = jp->nprocs; 3031556Srgrimes for (ps = jp->ps ; ; ps++) { /* for each process */ 3041556Srgrimes if (ps == jp->ps) 3051556Srgrimes fmtstr(s, 64, "[%d] %d ", jobno, ps->pid); 3061556Srgrimes else 3071556Srgrimes fmtstr(s, 64, " %d ", ps->pid); 3081556Srgrimes out1str(s); 3091556Srgrimes col = strlen(s); 3101556Srgrimes s[0] = '\0'; 3111556Srgrimes if (ps->status == -1) { 3121556Srgrimes /* don't print anything */ 31326104Ssteve } else if (WIFEXITED(ps->status)) { 31426104Ssteve fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status)); 3151556Srgrimes } else { 3161556Srgrimes#if JOBS 31726104Ssteve if (WIFSTOPPED(ps->status)) 31826104Ssteve i = WSTOPSIG(ps->status); 31926104Ssteve else 3201556Srgrimes#endif 32126104Ssteve i = WTERMSIG(ps->status); 32217987Speter if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F]) 32317987Speter scopy(sys_siglist[i & 0x7F], s); 3241556Srgrimes else 3251556Srgrimes fmtstr(s, 64, "Signal %d", i & 0x7F); 32626104Ssteve if (WCOREDUMP(ps->status)) 3271556Srgrimes strcat(s, " (core dumped)"); 3281556Srgrimes } 3291556Srgrimes out1str(s); 3301556Srgrimes col += strlen(s); 3311556Srgrimes do { 3321556Srgrimes out1c(' '); 3331556Srgrimes col++; 3341556Srgrimes } while (col < 30); 3351556Srgrimes out1str(ps->cmd); 3361556Srgrimes out1c('\n'); 3371556Srgrimes if (--procno <= 0) 3381556Srgrimes break; 3391556Srgrimes } 3401556Srgrimes jp->changed = 0; 3411556Srgrimes if (jp->state == JOBDONE) { 3421556Srgrimes freejob(jp); 3431556Srgrimes } 3441556Srgrimes } 3451556Srgrimes} 3461556Srgrimes 3471556Srgrimes 3481556Srgrimes/* 3491556Srgrimes * Mark a job structure as unused. 3501556Srgrimes */ 3511556Srgrimes 3521556SrgrimesSTATIC void 3531556Srgrimesfreejob(jp) 3541556Srgrimes struct job *jp; 3551556Srgrimes { 3561556Srgrimes struct procstat *ps; 3571556Srgrimes int i; 3581556Srgrimes 3591556Srgrimes INTOFF; 3601556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 3611556Srgrimes if (ps->cmd != nullstr) 3621556Srgrimes ckfree(ps->cmd); 3631556Srgrimes } 3641556Srgrimes if (jp->ps != &jp->ps0) 3651556Srgrimes ckfree(jp->ps); 3661556Srgrimes jp->used = 0; 3671556Srgrimes#if JOBS 3681556Srgrimes if (curjob == jp - jobtab + 1) 3691556Srgrimes curjob = 0; 3701556Srgrimes#endif 3711556Srgrimes INTON; 3721556Srgrimes} 3731556Srgrimes 3741556Srgrimes 3751556Srgrimes 3761556Srgrimesint 37720425Sstevewaitcmd(argc, argv) 37817987Speter int argc; 37920425Ssteve char **argv; 38017987Speter{ 3811556Srgrimes struct job *job; 38226104Ssteve int status, retval; 3831556Srgrimes struct job *jp; 3841556Srgrimes 3851556Srgrimes if (argc > 1) { 3861556Srgrimes job = getjob(argv[1]); 3871556Srgrimes } else { 3881556Srgrimes job = NULL; 3891556Srgrimes } 39038536Scracauer 39138536Scracauer /* 39238536Scracauer * Loop until a process is terminated or stopped, or a SIGINT is 39338536Scracauer * received. 39438536Scracauer */ 39538536Scracauer 39638521Scracauer in_waitcmd++; 39738536Scracauer do { 3981556Srgrimes if (job != NULL) { 3991556Srgrimes if (job->state) { 4001556Srgrimes status = job->ps[job->nprocs - 1].status; 40126104Ssteve if (WIFEXITED(status)) 40226104Ssteve retval = WEXITSTATUS(status); 4031556Srgrimes#if JOBS 40426104Ssteve else if (WIFSTOPPED(status)) 40526104Ssteve retval = WSTOPSIG(status) + 128; 4061556Srgrimes#endif 4071556Srgrimes else 40826104Ssteve retval = WTERMSIG(status) + 128; 4091556Srgrimes if (! iflag) 4101556Srgrimes freejob(job); 41138536Scracauer in_waitcmd--; 41226104Ssteve return retval; 4131556Srgrimes } 4141556Srgrimes } else { 4151556Srgrimes for (jp = jobtab ; ; jp++) { 4161556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 41738536Scracauer in_waitcmd--; 4181556Srgrimes return 0; 4191556Srgrimes } 4201556Srgrimes if (jp->used && jp->state == 0) 4211556Srgrimes break; 4221556Srgrimes } 4231556Srgrimes } 42438521Scracauer } while (dowait(1, (struct job *)NULL) != -1); 42538521Scracauer in_waitcmd--; 42638521Scracauer 42738521Scracauer return 0; 4281556Srgrimes} 4291556Srgrimes 4301556Srgrimes 4311556Srgrimes 43217987Speterint 43320425Sstevejobidcmd(argc, argv) 43425905Ssteve int argc __unused; 43520425Ssteve char **argv; 43617987Speter{ 4371556Srgrimes struct job *jp; 4381556Srgrimes int i; 4391556Srgrimes 4401556Srgrimes jp = getjob(argv[1]); 4411556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 4421556Srgrimes out1fmt("%d", jp->ps[i].pid); 4431556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 4441556Srgrimes } 4451556Srgrimes return 0; 4461556Srgrimes} 4471556Srgrimes 4481556Srgrimes 4491556Srgrimes 4501556Srgrimes/* 4511556Srgrimes * Convert a job name to a job structure. 4521556Srgrimes */ 4531556Srgrimes 4541556SrgrimesSTATIC struct job * 4551556Srgrimesgetjob(name) 4561556Srgrimes char *name; 4571556Srgrimes { 4581556Srgrimes int jobno; 45925222Ssteve struct job *jp; 4601556Srgrimes int pid; 4611556Srgrimes int i; 4621556Srgrimes 4631556Srgrimes if (name == NULL) { 4641556Srgrimes#if JOBS 4651556Srgrimescurrentjob: 4661556Srgrimes if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0) 4671556Srgrimes error("No current job"); 4681556Srgrimes return &jobtab[jobno - 1]; 4691556Srgrimes#else 4701556Srgrimes error("No current job"); 4711556Srgrimes#endif 4721556Srgrimes } else if (name[0] == '%') { 4731556Srgrimes if (is_digit(name[1])) { 4741556Srgrimes jobno = number(name + 1); 4751556Srgrimes if (jobno > 0 && jobno <= njobs 4761556Srgrimes && jobtab[jobno - 1].used != 0) 4771556Srgrimes return &jobtab[jobno - 1]; 4781556Srgrimes#if JOBS 4791556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 4801556Srgrimes goto currentjob; 4811556Srgrimes#endif 4821556Srgrimes } else { 48325222Ssteve struct job *found = NULL; 4841556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 4851556Srgrimes if (jp->used && jp->nprocs > 0 4861556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 4871556Srgrimes if (found) 4881556Srgrimes error("%s: ambiguous", name); 4891556Srgrimes found = jp; 4901556Srgrimes } 4911556Srgrimes } 4921556Srgrimes if (found) 4931556Srgrimes return found; 4941556Srgrimes } 4951556Srgrimes } else if (is_number(name)) { 4961556Srgrimes pid = number(name); 4971556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 4981556Srgrimes if (jp->used && jp->nprocs > 0 4991556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 5001556Srgrimes return jp; 5011556Srgrimes } 5021556Srgrimes } 5031556Srgrimes error("No such job: %s", name); 50417987Speter /*NOTREACHED*/ 50517987Speter return NULL; 5061556Srgrimes} 5071556Srgrimes 5081556Srgrimes 5091556Srgrimes 5101556Srgrimes/* 5111556Srgrimes * Return a new job structure, 5121556Srgrimes */ 5131556Srgrimes 5141556Srgrimesstruct job * 5151556Srgrimesmakejob(node, nprocs) 51625905Ssteve union node *node __unused; 51717987Speter int nprocs; 51817987Speter{ 5191556Srgrimes int i; 5201556Srgrimes struct job *jp; 5211556Srgrimes 5221556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 5231556Srgrimes if (--i < 0) { 5241556Srgrimes INTOFF; 5251556Srgrimes if (njobs == 0) { 5261556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 5271556Srgrimes } else { 5281556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 52917987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 53020425Ssteve /* Relocate `ps' pointers */ 53120425Ssteve for (i = 0; i < njobs; i++) 53220425Ssteve if (jp[i].ps == &jobtab[i].ps0) 53320425Ssteve jp[i].ps = &jp[i].ps0; 5341556Srgrimes ckfree(jobtab); 5351556Srgrimes jobtab = jp; 5361556Srgrimes } 5371556Srgrimes jp = jobtab + njobs; 5381556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 5391556Srgrimes INTON; 5401556Srgrimes break; 5411556Srgrimes } 5421556Srgrimes if (jp->used == 0) 5431556Srgrimes break; 5441556Srgrimes } 5451556Srgrimes INTOFF; 5461556Srgrimes jp->state = 0; 5471556Srgrimes jp->used = 1; 5481556Srgrimes jp->changed = 0; 5491556Srgrimes jp->nprocs = 0; 5501556Srgrimes#if JOBS 5511556Srgrimes jp->jobctl = jobctl; 5521556Srgrimes#endif 5531556Srgrimes if (nprocs > 1) { 5541556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 5551556Srgrimes } else { 5561556Srgrimes jp->ps = &jp->ps0; 5571556Srgrimes } 5581556Srgrimes INTON; 55917987Speter TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, 56017987Speter jp - jobtab + 1)); 5611556Srgrimes return jp; 5628855Srgrimes} 5631556Srgrimes 5641556Srgrimes 5651556Srgrimes/* 5661556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 5671556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 5681556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 5691556Srgrimes * be NULL. The mode parameter can be one of the following: 5701556Srgrimes * FORK_FG - Fork off a foreground process. 5711556Srgrimes * FORK_BG - Fork off a background process. 5721556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 5731556Srgrimes * process group even if job control is on. 5741556Srgrimes * 5751556Srgrimes * When job control is turned off, background processes have their standard 5761556Srgrimes * input redirected to /dev/null (except for the second and later processes 5771556Srgrimes * in a pipeline). 5781556Srgrimes */ 5791556Srgrimes 5801556Srgrimesint 5811556Srgrimesforkshell(jp, n, mode) 5821556Srgrimes union node *n; 5831556Srgrimes struct job *jp; 58417987Speter int mode; 58517987Speter{ 5861556Srgrimes int pid; 5871556Srgrimes int pgrp; 5881556Srgrimes 58917987Speter TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n, 59017987Speter mode)); 5911556Srgrimes INTOFF; 5921556Srgrimes pid = fork(); 5931556Srgrimes if (pid == -1) { 5941556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 5951556Srgrimes INTON; 5961556Srgrimes error("Cannot fork"); 5971556Srgrimes } 5981556Srgrimes if (pid == 0) { 5991556Srgrimes struct job *p; 6001556Srgrimes int wasroot; 6011556Srgrimes int i; 6021556Srgrimes 6031556Srgrimes TRACE(("Child shell %d\n", getpid())); 6041556Srgrimes wasroot = rootshell; 6051556Srgrimes rootshell = 0; 6061556Srgrimes for (i = njobs, p = jobtab ; --i >= 0 ; p++) 6071556Srgrimes if (p->used) 6081556Srgrimes freejob(p); 6091556Srgrimes closescript(); 6101556Srgrimes INTON; 6111556Srgrimes clear_traps(); 6121556Srgrimes#if JOBS 6131556Srgrimes jobctl = 0; /* do job control only in root shell */ 6141556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 6151556Srgrimes if (jp == NULL || jp->nprocs == 0) 6161556Srgrimes pgrp = getpid(); 6171556Srgrimes else 6181556Srgrimes pgrp = jp->ps[0].pid; 61921352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 6201556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 62120425Ssteve#ifdef OLD_TTY_DRIVER 6221556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0) 62318016Speter error("TIOCSPGRP failed, errno=%d", errno); 62420425Ssteve#else 62520425Ssteve if (tcsetpgrp(2, pgrp) < 0) 62620425Ssteve error("tcsetpgrp failed, errno=%d", errno); 62720425Ssteve#endif 6281556Srgrimes } 6291556Srgrimes setsignal(SIGTSTP); 6301556Srgrimes setsignal(SIGTTOU); 6311556Srgrimes } else if (mode == FORK_BG) { 6321556Srgrimes ignoresig(SIGINT); 6331556Srgrimes ignoresig(SIGQUIT); 6341556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 6351556Srgrimes ! fd0_redirected_p ()) { 6361556Srgrimes close(0); 6371556Srgrimes if (open("/dev/null", O_RDONLY) != 0) 6381556Srgrimes error("Can't open /dev/null"); 6391556Srgrimes } 6401556Srgrimes } 6411556Srgrimes#else 6421556Srgrimes if (mode == FORK_BG) { 6431556Srgrimes ignoresig(SIGINT); 6441556Srgrimes ignoresig(SIGQUIT); 6451556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 6461556Srgrimes ! fd0_redirected_p ()) { 6471556Srgrimes close(0); 6481556Srgrimes if (open("/dev/null", O_RDONLY) != 0) 6491556Srgrimes error("Can't open /dev/null"); 6501556Srgrimes } 6511556Srgrimes } 6521556Srgrimes#endif 6531556Srgrimes if (wasroot && iflag) { 6541556Srgrimes setsignal(SIGINT); 6551556Srgrimes setsignal(SIGQUIT); 6561556Srgrimes setsignal(SIGTERM); 6571556Srgrimes } 6581556Srgrimes return pid; 6591556Srgrimes } 6601556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 6611556Srgrimes if (jp == NULL || jp->nprocs == 0) 6621556Srgrimes pgrp = pid; 6631556Srgrimes else 6641556Srgrimes pgrp = jp->ps[0].pid; 66517987Speter setpgid(pid, pgrp); 6661556Srgrimes } 6671556Srgrimes if (mode == FORK_BG) 6681556Srgrimes backgndpid = pid; /* set $! */ 6691556Srgrimes if (jp) { 6701556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 6711556Srgrimes ps->pid = pid; 6721556Srgrimes ps->status = -1; 6731556Srgrimes ps->cmd = nullstr; 6741556Srgrimes if (iflag && rootshell && n) 6751556Srgrimes ps->cmd = commandtext(n); 6761556Srgrimes } 6771556Srgrimes INTON; 6781556Srgrimes TRACE(("In parent shell: child = %d\n", pid)); 6791556Srgrimes return pid; 6801556Srgrimes} 6811556Srgrimes 6821556Srgrimes 6831556Srgrimes 6841556Srgrimes/* 6851556Srgrimes * Wait for job to finish. 6861556Srgrimes * 6871556Srgrimes * Under job control we have the problem that while a child process is 6881556Srgrimes * running interrupts generated by the user are sent to the child but not 6891556Srgrimes * to the shell. This means that an infinite loop started by an inter- 6901556Srgrimes * active user may be hard to kill. With job control turned off, an 6911556Srgrimes * interactive user may place an interactive program inside a loop. If 6921556Srgrimes * the interactive program catches interrupts, the user doesn't want 6931556Srgrimes * these interrupts to also abort the loop. The approach we take here 6941556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 6951556Srgrimes * forground process to terminate, and then send itself an interrupt 6961556Srgrimes * signal if the child process was terminated by an interrupt signal. 6971556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 6981556Srgrimes * exit on interrupt; unless these processes terminate themselves by 6991556Srgrimes * sending a signal to themselves (instead of calling exit) they will 7001556Srgrimes * confuse this approach. 7011556Srgrimes */ 7021556Srgrimes 7031556Srgrimesint 7041556Srgrimeswaitforjob(jp) 70525222Ssteve struct job *jp; 7061556Srgrimes { 7071556Srgrimes#if JOBS 70817987Speter int mypgrp = getpgrp(); 7091556Srgrimes#endif 7101556Srgrimes int status; 7111556Srgrimes int st; 7121556Srgrimes 7131556Srgrimes INTOFF; 7141556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 7151556Srgrimes while (jp->state == 0) { 7161556Srgrimes dowait(1, jp); 7171556Srgrimes } 7181556Srgrimes#if JOBS 7191556Srgrimes if (jp->jobctl) { 72020425Ssteve#ifdef OLD_TTY_DRIVER 7211556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0) 72220425Ssteve error("TIOCSPGRP failed, errno=%d\n", errno); 72320425Ssteve#else 72420425Ssteve if (tcsetpgrp(2, mypgrp) < 0) 72520425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 72620425Ssteve#endif 7271556Srgrimes } 7281556Srgrimes if (jp->state == JOBSTOPPED) 7291556Srgrimes curjob = jp - jobtab + 1; 7301556Srgrimes#endif 7311556Srgrimes status = jp->ps[jp->nprocs - 1].status; 7321556Srgrimes /* convert to 8 bits */ 73326104Ssteve if (WIFEXITED(status)) 73426104Ssteve st = WEXITSTATUS(status); 7351556Srgrimes#if JOBS 73626104Ssteve else if (WIFSTOPPED(status)) 73726104Ssteve st = WSTOPSIG(status) + 128; 7381556Srgrimes#endif 7391556Srgrimes else 74026104Ssteve st = WTERMSIG(status) + 128; 7411556Srgrimes if (! JOBS || jp->state == JOBDONE) 7421556Srgrimes freejob(jp); 74338521Scracauer if (int_pending()) { 74438521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 74538521Scracauer kill(getpid(), SIGINT); 74638521Scracauer else 74738521Scracauer CLEAR_PENDING_INT; 74838521Scracauer } 7491556Srgrimes INTON; 7501556Srgrimes return st; 7511556Srgrimes} 7521556Srgrimes 7531556Srgrimes 7541556Srgrimes 7551556Srgrimes/* 7561556Srgrimes * Wait for a process to terminate. 7571556Srgrimes */ 7581556Srgrimes 7591556SrgrimesSTATIC int 7601556Srgrimesdowait(block, job) 76117987Speter int block; 7621556Srgrimes struct job *job; 76317987Speter{ 7641556Srgrimes int pid; 7651556Srgrimes int status; 7661556Srgrimes struct procstat *sp; 7671556Srgrimes struct job *jp; 7681556Srgrimes struct job *thisjob; 7691556Srgrimes int done; 7701556Srgrimes int stopped; 7711556Srgrimes int core; 77226104Ssteve int sig; 7731556Srgrimes 7741556Srgrimes TRACE(("dowait(%d) called\n", block)); 7751556Srgrimes do { 7761556Srgrimes pid = waitproc(block, &status); 7771556Srgrimes TRACE(("wait returns %d, status=%d\n", pid, status)); 77838521Scracauer } while (pid == -1 && errno == EINTR && breakwaitcmd == 0); 77938521Scracauer if (breakwaitcmd != 0) { 78038521Scracauer breakwaitcmd = 0; 78138521Scracauer return -1; 78238521Scracauer } 7831556Srgrimes if (pid <= 0) 7841556Srgrimes return pid; 7851556Srgrimes INTOFF; 7861556Srgrimes thisjob = NULL; 7871556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 7881556Srgrimes if (jp->used) { 7891556Srgrimes done = 1; 7901556Srgrimes stopped = 1; 7911556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 7921556Srgrimes if (sp->pid == -1) 7931556Srgrimes continue; 7941556Srgrimes if (sp->pid == pid) { 79526104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 79626104Ssteve pid, sp->status, status)); 7971556Srgrimes sp->status = status; 7981556Srgrimes thisjob = jp; 7991556Srgrimes } 8001556Srgrimes if (sp->status == -1) 8011556Srgrimes stopped = 0; 80226104Ssteve else if (WIFSTOPPED(sp->status)) 8031556Srgrimes done = 0; 8041556Srgrimes } 8051556Srgrimes if (stopped) { /* stopped or done */ 8061556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 8071556Srgrimes if (jp->state != state) { 8081556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 8091556Srgrimes jp->state = state; 8101556Srgrimes#if JOBS 8111556Srgrimes if (done && curjob == jp - jobtab + 1) 8121556Srgrimes curjob = 0; /* no current job */ 8131556Srgrimes#endif 8141556Srgrimes } 8151556Srgrimes } 8161556Srgrimes } 8171556Srgrimes } 8181556Srgrimes INTON; 8191556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 82026104Ssteve core = WCOREDUMP(status); 8211556Srgrimes#if JOBS 82226104Ssteve if (WIFSTOPPED(status)) 82326104Ssteve sig = WSTOPSIG(status); 82426104Ssteve else 8251556Srgrimes#endif 82626104Ssteve if (WIFEXITED(status)) 82726104Ssteve sig = 0; 82826104Ssteve else 82926104Ssteve sig = WTERMSIG(status); 83026104Ssteve 83126104Ssteve if (sig != 0 && sig != SIGINT && sig != SIGPIPE) { 8321556Srgrimes if (thisjob != job) 8331556Srgrimes outfmt(out2, "%d: ", pid); 8341556Srgrimes#if JOBS 83526104Ssteve if (sig == SIGTSTP && rootshell && iflag) 8361556Srgrimes outfmt(out2, "%%%d ", job - jobtab + 1); 8371556Srgrimes#endif 83826104Ssteve if (sig < NSIG && sys_siglist[sig]) 83926104Ssteve out2str(sys_siglist[sig]); 8401556Srgrimes else 84126104Ssteve outfmt(out2, "Signal %d", sig); 8421556Srgrimes if (core) 8431556Srgrimes out2str(" - core dumped"); 8441556Srgrimes out2c('\n'); 8451556Srgrimes flushout(&errout); 8461556Srgrimes } else { 84726104Ssteve TRACE(("Not printing status: status=%d, sig=%d\n", 84826104Ssteve status, sig)); 8491556Srgrimes } 8501556Srgrimes } else { 8511556Srgrimes TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job)); 8521556Srgrimes if (thisjob) 8531556Srgrimes thisjob->changed = 1; 8541556Srgrimes } 8551556Srgrimes return pid; 8561556Srgrimes} 8571556Srgrimes 8581556Srgrimes 8591556Srgrimes 8601556Srgrimes/* 8611556Srgrimes * Do a wait system call. If job control is compiled in, we accept 8621556Srgrimes * stopped processes. If block is zero, we return a value of zero 8631556Srgrimes * rather than blocking. 8641556Srgrimes * 8651556Srgrimes * System V doesn't have a non-blocking wait system call. It does 8661556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's 8671556Srgrimes * children dies. The obvious way to use SIGCLD would be to install 8681556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD 8691556Srgrimes * was received, and have waitproc bump another counter when it got 8701556Srgrimes * the status of a process. Waitproc would then know that a wait 8711556Srgrimes * system call would not block if the two counters were different. 8721556Srgrimes * This approach doesn't work because if a process has children that 8731556Srgrimes * have not been waited for, System V will send it a SIGCLD when it 8741556Srgrimes * installs a signal handler for SIGCLD. What this means is that when 8751556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously 8761556Srgrimes * until is runs out of stack space, unless it does a wait call before 8771556Srgrimes * restoring the signal handler. The code below takes advantage of 8781556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and 8791556Srgrimes * then checking to see whether it was called. If there are any 8801556Srgrimes * children to be waited for, it will be. 8811556Srgrimes * 8821556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking 8831556Srgrimes * waits at all. In this case, the user will not be informed when 8841556Srgrimes * a background process until the next time she runs a real program 8851556Srgrimes * (as opposed to running a builtin command or just typing return), 8861556Srgrimes * and the jobs command may give out of date information. 8871556Srgrimes */ 8881556Srgrimes 8891556Srgrimes#ifdef SYSV 89038521ScracauerSTATIC sig_atomic_t gotsigchild; 8911556Srgrimes 8921556SrgrimesSTATIC int onsigchild() { 8931556Srgrimes gotsigchild = 1; 8941556Srgrimes} 8951556Srgrimes#endif 8961556Srgrimes 8971556Srgrimes 8981556SrgrimesSTATIC int 8991556Srgrimeswaitproc(block, status) 90017987Speter int block; 9011556Srgrimes int *status; 90217987Speter{ 9031556Srgrimes#ifdef BSD 9041556Srgrimes int flags; 9051556Srgrimes 9061556Srgrimes#if JOBS 9071556Srgrimes flags = WUNTRACED; 9081556Srgrimes#else 9091556Srgrimes flags = 0; 9101556Srgrimes#endif 9111556Srgrimes if (block == 0) 9121556Srgrimes flags |= WNOHANG; 9131556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 9141556Srgrimes#else 9151556Srgrimes#ifdef SYSV 9161556Srgrimes int (*save)(); 9171556Srgrimes 9181556Srgrimes if (block == 0) { 9191556Srgrimes gotsigchild = 0; 9201556Srgrimes save = signal(SIGCLD, onsigchild); 9211556Srgrimes signal(SIGCLD, save); 9221556Srgrimes if (gotsigchild == 0) 9231556Srgrimes return 0; 9241556Srgrimes } 9251556Srgrimes return wait(status); 9261556Srgrimes#else 9271556Srgrimes if (block == 0) 9281556Srgrimes return 0; 9291556Srgrimes return wait(status); 9301556Srgrimes#endif 9311556Srgrimes#endif 9321556Srgrimes} 9331556Srgrimes 9341556Srgrimes/* 9351556Srgrimes * return 1 if there are stopped jobs, otherwise 0 9361556Srgrimes */ 9371556Srgrimesint job_warning = 0; 9381556Srgrimesint 9391556Srgrimesstoppedjobs() 9401556Srgrimes{ 94125222Ssteve int jobno; 94225222Ssteve struct job *jp; 9431556Srgrimes 9441556Srgrimes if (job_warning) 9451556Srgrimes return (0); 9461556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 9471556Srgrimes if (jp->used == 0) 9481556Srgrimes continue; 9491556Srgrimes if (jp->state == JOBSTOPPED) { 9501556Srgrimes out2str("You have stopped jobs.\n"); 9511556Srgrimes job_warning = 2; 9521556Srgrimes return (1); 9531556Srgrimes } 9541556Srgrimes } 9551556Srgrimes 9561556Srgrimes return (0); 9571556Srgrimes} 9581556Srgrimes 9591556Srgrimes/* 9601556Srgrimes * Return a string identifying a command (to be printed by the 9611556Srgrimes * jobs command. 9621556Srgrimes */ 9631556Srgrimes 9641556SrgrimesSTATIC char *cmdnextc; 9651556SrgrimesSTATIC int cmdnleft; 9661556SrgrimesSTATIC void cmdtxt(), cmdputs(); 9671556Srgrimes#define MAXCMDTEXT 200 9681556Srgrimes 9691556Srgrimeschar * 9701556Srgrimescommandtext(n) 9711556Srgrimes union node *n; 9721556Srgrimes { 9731556Srgrimes char *name; 9741556Srgrimes 9751556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 9761556Srgrimes cmdnleft = MAXCMDTEXT - 4; 9771556Srgrimes cmdtxt(n); 9781556Srgrimes *cmdnextc = '\0'; 9791556Srgrimes return name; 9801556Srgrimes} 9811556Srgrimes 9821556Srgrimes 9831556SrgrimesSTATIC void 9841556Srgrimescmdtxt(n) 9851556Srgrimes union node *n; 9861556Srgrimes { 9871556Srgrimes union node *np; 9881556Srgrimes struct nodelist *lp; 9891556Srgrimes char *p; 9901556Srgrimes int i; 9911556Srgrimes char s[2]; 9921556Srgrimes 9931556Srgrimes if (n == NULL) 9941556Srgrimes return; 9951556Srgrimes switch (n->type) { 9961556Srgrimes case NSEMI: 9971556Srgrimes cmdtxt(n->nbinary.ch1); 9981556Srgrimes cmdputs("; "); 9991556Srgrimes cmdtxt(n->nbinary.ch2); 10001556Srgrimes break; 10011556Srgrimes case NAND: 10021556Srgrimes cmdtxt(n->nbinary.ch1); 10031556Srgrimes cmdputs(" && "); 10041556Srgrimes cmdtxt(n->nbinary.ch2); 10051556Srgrimes break; 10061556Srgrimes case NOR: 10071556Srgrimes cmdtxt(n->nbinary.ch1); 10081556Srgrimes cmdputs(" || "); 10091556Srgrimes cmdtxt(n->nbinary.ch2); 10101556Srgrimes break; 10111556Srgrimes case NPIPE: 10121556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 10131556Srgrimes cmdtxt(lp->n); 10141556Srgrimes if (lp->next) 10151556Srgrimes cmdputs(" | "); 10161556Srgrimes } 10171556Srgrimes break; 10181556Srgrimes case NSUBSHELL: 10191556Srgrimes cmdputs("("); 10201556Srgrimes cmdtxt(n->nredir.n); 10211556Srgrimes cmdputs(")"); 10221556Srgrimes break; 10231556Srgrimes case NREDIR: 10241556Srgrimes case NBACKGND: 10251556Srgrimes cmdtxt(n->nredir.n); 10261556Srgrimes break; 10271556Srgrimes case NIF: 10281556Srgrimes cmdputs("if "); 10291556Srgrimes cmdtxt(n->nif.test); 10301556Srgrimes cmdputs("; then "); 10311556Srgrimes cmdtxt(n->nif.ifpart); 10321556Srgrimes cmdputs("..."); 10331556Srgrimes break; 10341556Srgrimes case NWHILE: 10351556Srgrimes cmdputs("while "); 10361556Srgrimes goto until; 10371556Srgrimes case NUNTIL: 10381556Srgrimes cmdputs("until "); 10391556Srgrimesuntil: 10401556Srgrimes cmdtxt(n->nbinary.ch1); 10411556Srgrimes cmdputs("; do "); 10421556Srgrimes cmdtxt(n->nbinary.ch2); 10431556Srgrimes cmdputs("; done"); 10441556Srgrimes break; 10451556Srgrimes case NFOR: 10461556Srgrimes cmdputs("for "); 10471556Srgrimes cmdputs(n->nfor.var); 10481556Srgrimes cmdputs(" in ..."); 10491556Srgrimes break; 10501556Srgrimes case NCASE: 10511556Srgrimes cmdputs("case "); 10521556Srgrimes cmdputs(n->ncase.expr->narg.text); 10531556Srgrimes cmdputs(" in ..."); 10541556Srgrimes break; 10551556Srgrimes case NDEFUN: 10561556Srgrimes cmdputs(n->narg.text); 10571556Srgrimes cmdputs("() ..."); 10581556Srgrimes break; 10591556Srgrimes case NCMD: 10601556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 10611556Srgrimes cmdtxt(np); 10621556Srgrimes if (np->narg.next) 10631556Srgrimes cmdputs(" "); 10641556Srgrimes } 10651556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 10661556Srgrimes cmdputs(" "); 10671556Srgrimes cmdtxt(np); 10681556Srgrimes } 10691556Srgrimes break; 10701556Srgrimes case NARG: 10711556Srgrimes cmdputs(n->narg.text); 10721556Srgrimes break; 10731556Srgrimes case NTO: 10741556Srgrimes p = ">"; i = 1; goto redir; 10751556Srgrimes case NAPPEND: 10761556Srgrimes p = ">>"; i = 1; goto redir; 10771556Srgrimes case NTOFD: 10781556Srgrimes p = ">&"; i = 1; goto redir; 10791556Srgrimes case NFROM: 10801556Srgrimes p = "<"; i = 0; goto redir; 10811556Srgrimes case NFROMFD: 10821556Srgrimes p = "<&"; i = 0; goto redir; 10831556Srgrimesredir: 10841556Srgrimes if (n->nfile.fd != i) { 10851556Srgrimes s[0] = n->nfile.fd + '0'; 10861556Srgrimes s[1] = '\0'; 10871556Srgrimes cmdputs(s); 10881556Srgrimes } 10891556Srgrimes cmdputs(p); 10901556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 10911556Srgrimes s[0] = n->ndup.dupfd + '0'; 10921556Srgrimes s[1] = '\0'; 10931556Srgrimes cmdputs(s); 10941556Srgrimes } else { 10951556Srgrimes cmdtxt(n->nfile.fname); 10961556Srgrimes } 10971556Srgrimes break; 10981556Srgrimes case NHERE: 10991556Srgrimes case NXHERE: 11001556Srgrimes cmdputs("<<..."); 11011556Srgrimes break; 11021556Srgrimes default: 11031556Srgrimes cmdputs("???"); 11041556Srgrimes break; 11051556Srgrimes } 11061556Srgrimes} 11071556Srgrimes 11081556Srgrimes 11091556Srgrimes 11101556SrgrimesSTATIC void 11111556Srgrimescmdputs(s) 11121556Srgrimes char *s; 11131556Srgrimes { 111425222Ssteve char *p, *q; 111525222Ssteve char c; 11161556Srgrimes int subtype = 0; 11171556Srgrimes 11181556Srgrimes if (cmdnleft <= 0) 11191556Srgrimes return; 11201556Srgrimes p = s; 11211556Srgrimes q = cmdnextc; 11221556Srgrimes while ((c = *p++) != '\0') { 11231556Srgrimes if (c == CTLESC) 11241556Srgrimes *q++ = *p++; 11251556Srgrimes else if (c == CTLVAR) { 11261556Srgrimes *q++ = '$'; 11271556Srgrimes if (--cmdnleft > 0) 11281556Srgrimes *q++ = '{'; 11291556Srgrimes subtype = *p++; 11301556Srgrimes } else if (c == '=' && subtype != 0) { 11311556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 11321556Srgrimes subtype = 0; 11331556Srgrimes } else if (c == CTLENDVAR) { 11341556Srgrimes *q++ = '}'; 113518954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 11361556Srgrimes cmdnleft++; /* ignore it */ 11371556Srgrimes else 11381556Srgrimes *q++ = c; 11391556Srgrimes if (--cmdnleft <= 0) { 11401556Srgrimes *q++ = '.'; 11411556Srgrimes *q++ = '.'; 11421556Srgrimes *q++ = '.'; 11431556Srgrimes break; 11441556Srgrimes } 11451556Srgrimes } 11461556Srgrimes cmdnextc = q; 11471556Srgrimes} 1148