jobs.c revision 72086
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[] = 4250471Speter "$FreeBSD: head/bin/sh/jobs.c 72086 2001-02-06 10:29:34Z cracauer $"; 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> 5569793Sobrien#include <paths.h> 5617987Speter#endif 5718018Speter#include <sys/ioctl.h> 5817987Speter 591556Srgrimes#include "shell.h" 601556Srgrimes#if JOBS 6125222Ssteve#if OLD_TTY_DRIVER 621556Srgrimes#include "sgtty.h" 6317987Speter#else 6417987Speter#include <termios.h> 6517987Speter#endif 661556Srgrimes#undef CEOF /* syntax.h redefines this */ 671556Srgrimes#endif 6817987Speter#include "redir.h" 6917987Speter#include "show.h" 701556Srgrimes#include "main.h" 711556Srgrimes#include "parser.h" 721556Srgrimes#include "nodes.h" 731556Srgrimes#include "jobs.h" 741556Srgrimes#include "options.h" 751556Srgrimes#include "trap.h" 761556Srgrimes#include "syntax.h" 771556Srgrimes#include "input.h" 781556Srgrimes#include "output.h" 791556Srgrimes#include "memalloc.h" 801556Srgrimes#include "error.h" 811556Srgrimes#include "mystring.h" 821556Srgrimes 831556Srgrimes 841556Srgrimesstruct job *jobtab; /* array of jobs */ 851556Srgrimesint njobs; /* size of array */ 8628346SsteveMKINIT pid_t backgndpid = -1; /* pid of last background process */ 871556Srgrimes#if JOBS 881556Srgrimesint initialpgrp; /* pgrp of shell on invocation */ 8928346Ssteveint curjob; /* current job */ 901556Srgrimes#endif 9138536Scracauerint in_waitcmd = 0; /* are we in waitcmd()? */ 9238950Scracauerint in_dowait = 0; /* are we in dowait()? */ 9338536Scracauervolatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */ 941556Srgrimes 9520425Ssteve#if JOBS 9617987SpeterSTATIC void restartjob __P((struct job *)); 9720425Ssteve#endif 9817987SpeterSTATIC void freejob __P((struct job *)); 9917987SpeterSTATIC struct job *getjob __P((char *)); 10017987SpeterSTATIC int dowait __P((int, struct job *)); 10120425Ssteve#if SYSV 10238536ScracauerSTATIC int onsigchild __P((void)); 10320425Ssteve#endif 10417987SpeterSTATIC int waitproc __P((int, int *)); 10517987SpeterSTATIC void cmdtxt __P((union node *)); 10617987SpeterSTATIC void cmdputs __P((char *)); 1071556Srgrimes 1081556Srgrimes 1091556Srgrimes/* 1101556Srgrimes * Turn job control on and off. 1111556Srgrimes * 1121556Srgrimes * Note: This code assumes that the third arg to ioctl is a character 1131556Srgrimes * pointer, which is true on Berkeley systems but not System V. Since 1141556Srgrimes * System V doesn't have job control yet, this isn't a problem now. 1151556Srgrimes */ 1161556Srgrimes 1171556SrgrimesMKINIT int jobctl; 1181556Srgrimes 11920425Ssteve#if JOBS 1201556Srgrimesvoid 12120425Sstevesetjobctl(on) 12217987Speter int on; 12317987Speter{ 1241556Srgrimes#ifdef OLD_TTY_DRIVER 1251556Srgrimes int ldisc; 1261556Srgrimes#endif 1271556Srgrimes 1281556Srgrimes if (on == jobctl || rootshell == 0) 1291556Srgrimes return; 1301556Srgrimes if (on) { 1311556Srgrimes do { /* while we are in the background */ 13220425Ssteve#ifdef OLD_TTY_DRIVER 1331556Srgrimes if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) { 13420425Ssteve#else 13520425Ssteve initialpgrp = tcgetpgrp(2); 13620425Ssteve if (initialpgrp < 0) { 13720425Ssteve#endif 1381556Srgrimes out2str("sh: can't access tty; job control turned off\n"); 1391556Srgrimes mflag = 0; 1401556Srgrimes return; 1411556Srgrimes } 1421556Srgrimes if (initialpgrp == -1) 14317987Speter initialpgrp = getpgrp(); 14417987Speter else if (initialpgrp != getpgrp()) { 1451556Srgrimes killpg(initialpgrp, SIGTTIN); 1461556Srgrimes continue; 1471556Srgrimes } 1481556Srgrimes } while (0); 1491556Srgrimes#ifdef OLD_TTY_DRIVER 1501556Srgrimes if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) { 1511556Srgrimes out2str("sh: need new tty driver to run job control; job control turned off\n"); 1521556Srgrimes mflag = 0; 1531556Srgrimes return; 1541556Srgrimes } 1551556Srgrimes#endif 1561556Srgrimes setsignal(SIGTSTP); 1571556Srgrimes setsignal(SIGTTOU); 1581556Srgrimes setsignal(SIGTTIN); 15917987Speter setpgid(0, rootpid); 16020425Ssteve#ifdef OLD_TTY_DRIVER 1611556Srgrimes ioctl(2, TIOCSPGRP, (char *)&rootpid); 16220425Ssteve#else 16320425Ssteve tcsetpgrp(2, rootpid); 16420425Ssteve#endif 1651556Srgrimes } else { /* turning job control off */ 16617987Speter setpgid(0, initialpgrp); 16720425Ssteve#ifdef OLD_TTY_DRIVER 1681556Srgrimes ioctl(2, TIOCSPGRP, (char *)&initialpgrp); 16920425Ssteve#else 17020425Ssteve tcsetpgrp(2, initialpgrp); 17120425Ssteve#endif 1721556Srgrimes setsignal(SIGTSTP); 1731556Srgrimes setsignal(SIGTTOU); 1741556Srgrimes setsignal(SIGTTIN); 1751556Srgrimes } 1761556Srgrimes jobctl = on; 1771556Srgrimes} 17820425Ssteve#endif 1791556Srgrimes 1801556Srgrimes 1811556Srgrimes#ifdef mkinit 18228346SsteveINCLUDE <sys/types.h> 18317987SpeterINCLUDE <stdlib.h> 1841556Srgrimes 1851556SrgrimesSHELLPROC { 1861556Srgrimes backgndpid = -1; 1871556Srgrimes#if JOBS 1881556Srgrimes jobctl = 0; 1891556Srgrimes#endif 1901556Srgrimes} 1911556Srgrimes 1921556Srgrimes#endif 1931556Srgrimes 1941556Srgrimes 1951556Srgrimes 1961556Srgrimes#if JOBS 19717987Speterint 19817987Speterfgcmd(argc, argv) 19925905Ssteve int argc __unused; 20020425Ssteve char **argv; 20117987Speter{ 2021556Srgrimes struct job *jp; 2031556Srgrimes int pgrp; 2041556Srgrimes int status; 2051556Srgrimes 2061556Srgrimes jp = getjob(argv[1]); 2071556Srgrimes if (jp->jobctl == 0) 2081556Srgrimes error("job not created under job control"); 2091556Srgrimes pgrp = jp->ps[0].pid; 21020425Ssteve#ifdef OLD_TTY_DRIVER 2111556Srgrimes ioctl(2, TIOCSPGRP, (char *)&pgrp); 21220425Ssteve#else 21320425Ssteve tcsetpgrp(2, pgrp); 21420425Ssteve#endif 2151556Srgrimes restartjob(jp); 2161556Srgrimes INTOFF; 21745916Scracauer status = waitforjob(jp, (int *)NULL); 2181556Srgrimes INTON; 2191556Srgrimes return status; 2201556Srgrimes} 2211556Srgrimes 2221556Srgrimes 22317987Speterint 22417987Speterbgcmd(argc, argv) 22517987Speter int argc; 22620425Ssteve char **argv; 22717987Speter{ 2281556Srgrimes struct job *jp; 2291556Srgrimes 2301556Srgrimes do { 2311556Srgrimes jp = getjob(*++argv); 2321556Srgrimes if (jp->jobctl == 0) 2331556Srgrimes error("job not created under job control"); 2341556Srgrimes restartjob(jp); 2351556Srgrimes } while (--argc > 1); 2361556Srgrimes return 0; 2371556Srgrimes} 2381556Srgrimes 2391556Srgrimes 2401556SrgrimesSTATIC void 2411556Srgrimesrestartjob(jp) 2421556Srgrimes struct job *jp; 24317987Speter{ 2441556Srgrimes struct procstat *ps; 2451556Srgrimes int i; 2461556Srgrimes 2471556Srgrimes if (jp->state == JOBDONE) 2481556Srgrimes return; 2491556Srgrimes INTOFF; 2501556Srgrimes killpg(jp->ps[0].pid, SIGCONT); 2511556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 25226104Ssteve if (WIFSTOPPED(ps->status)) { 2531556Srgrimes ps->status = -1; 2541556Srgrimes jp->state = 0; 2551556Srgrimes } 2561556Srgrimes } 2571556Srgrimes INTON; 2581556Srgrimes} 2591556Srgrimes#endif 2601556Srgrimes 2611556Srgrimes 2621556Srgrimesint 26317987Speterjobscmd(argc, argv) 26425905Ssteve int argc __unused; 26525905Ssteve char **argv __unused; 26617987Speter{ 2671556Srgrimes showjobs(0); 2681556Srgrimes return 0; 2691556Srgrimes} 2701556Srgrimes 2711556Srgrimes 2721556Srgrimes/* 2731556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 2741556Srgrimes * statuses have changed since the last call to showjobs. 2751556Srgrimes * 2761556Srgrimes * If the shell is interrupted in the process of creating a job, the 2771556Srgrimes * result may be a job structure containing zero processes. Such structures 2781556Srgrimes * will be freed here. 2791556Srgrimes */ 2801556Srgrimes 2811556Srgrimesvoid 28220425Ssteveshowjobs(change) 28317987Speter int change; 28417987Speter{ 2851556Srgrimes int jobno; 2861556Srgrimes int procno; 2871556Srgrimes int i; 2881556Srgrimes struct job *jp; 2891556Srgrimes struct procstat *ps; 2901556Srgrimes int col; 2911556Srgrimes char s[64]; 2921556Srgrimes 2931556Srgrimes TRACE(("showjobs(%d) called\n", change)); 2941556Srgrimes while (dowait(0, (struct job *)NULL) > 0); 2951556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 2961556Srgrimes if (! jp->used) 2971556Srgrimes continue; 2981556Srgrimes if (jp->nprocs == 0) { 2991556Srgrimes freejob(jp); 3001556Srgrimes continue; 3011556Srgrimes } 3021556Srgrimes if (change && ! jp->changed) 3031556Srgrimes continue; 3041556Srgrimes procno = jp->nprocs; 3051556Srgrimes for (ps = jp->ps ; ; ps++) { /* for each process */ 3061556Srgrimes if (ps == jp->ps) 3071556Srgrimes fmtstr(s, 64, "[%d] %d ", jobno, ps->pid); 3081556Srgrimes else 3091556Srgrimes fmtstr(s, 64, " %d ", ps->pid); 3101556Srgrimes out1str(s); 3111556Srgrimes col = strlen(s); 3121556Srgrimes s[0] = '\0'; 3131556Srgrimes if (ps->status == -1) { 3141556Srgrimes /* don't print anything */ 31526104Ssteve } else if (WIFEXITED(ps->status)) { 31626104Ssteve fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status)); 3171556Srgrimes } else { 3181556Srgrimes#if JOBS 31926104Ssteve if (WIFSTOPPED(ps->status)) 32026104Ssteve i = WSTOPSIG(ps->status); 32126104Ssteve else 3221556Srgrimes#endif 32326104Ssteve i = WTERMSIG(ps->status); 32417987Speter if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F]) 32517987Speter scopy(sys_siglist[i & 0x7F], s); 3261556Srgrimes else 3271556Srgrimes fmtstr(s, 64, "Signal %d", i & 0x7F); 32826104Ssteve if (WCOREDUMP(ps->status)) 3291556Srgrimes strcat(s, " (core dumped)"); 3301556Srgrimes } 3311556Srgrimes out1str(s); 3321556Srgrimes col += strlen(s); 3331556Srgrimes do { 3341556Srgrimes out1c(' '); 3351556Srgrimes col++; 3361556Srgrimes } while (col < 30); 3371556Srgrimes out1str(ps->cmd); 3381556Srgrimes out1c('\n'); 3391556Srgrimes if (--procno <= 0) 3401556Srgrimes break; 3411556Srgrimes } 3421556Srgrimes jp->changed = 0; 3431556Srgrimes if (jp->state == JOBDONE) { 3441556Srgrimes freejob(jp); 3451556Srgrimes } 3461556Srgrimes } 3471556Srgrimes} 3481556Srgrimes 3491556Srgrimes 3501556Srgrimes/* 3511556Srgrimes * Mark a job structure as unused. 3521556Srgrimes */ 3531556Srgrimes 3541556SrgrimesSTATIC void 3551556Srgrimesfreejob(jp) 3561556Srgrimes struct job *jp; 3571556Srgrimes { 3581556Srgrimes struct procstat *ps; 3591556Srgrimes int i; 3601556Srgrimes 3611556Srgrimes INTOFF; 3621556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 3631556Srgrimes if (ps->cmd != nullstr) 3641556Srgrimes ckfree(ps->cmd); 3651556Srgrimes } 3661556Srgrimes if (jp->ps != &jp->ps0) 3671556Srgrimes ckfree(jp->ps); 3681556Srgrimes jp->used = 0; 3691556Srgrimes#if JOBS 3701556Srgrimes if (curjob == jp - jobtab + 1) 3711556Srgrimes curjob = 0; 3721556Srgrimes#endif 3731556Srgrimes INTON; 3741556Srgrimes} 3751556Srgrimes 3761556Srgrimes 3771556Srgrimes 3781556Srgrimesint 37920425Sstevewaitcmd(argc, argv) 38017987Speter int argc; 38120425Ssteve char **argv; 38217987Speter{ 3831556Srgrimes struct job *job; 38426104Ssteve int status, retval; 3851556Srgrimes struct job *jp; 3861556Srgrimes 3871556Srgrimes if (argc > 1) { 3881556Srgrimes job = getjob(argv[1]); 3891556Srgrimes } else { 3901556Srgrimes job = NULL; 3911556Srgrimes } 39238536Scracauer 39338536Scracauer /* 39438536Scracauer * Loop until a process is terminated or stopped, or a SIGINT is 39538536Scracauer * received. 39638536Scracauer */ 39738536Scracauer 39838521Scracauer in_waitcmd++; 39938536Scracauer do { 4001556Srgrimes if (job != NULL) { 4011556Srgrimes if (job->state) { 4021556Srgrimes status = job->ps[job->nprocs - 1].status; 40326104Ssteve if (WIFEXITED(status)) 40426104Ssteve retval = WEXITSTATUS(status); 4051556Srgrimes#if JOBS 40626104Ssteve else if (WIFSTOPPED(status)) 40726104Ssteve retval = WSTOPSIG(status) + 128; 4081556Srgrimes#endif 4091556Srgrimes else 41026104Ssteve retval = WTERMSIG(status) + 128; 4111556Srgrimes if (! iflag) 4121556Srgrimes freejob(job); 41338536Scracauer in_waitcmd--; 41426104Ssteve return retval; 4151556Srgrimes } 4161556Srgrimes } else { 4171556Srgrimes for (jp = jobtab ; ; jp++) { 4181556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 41938536Scracauer in_waitcmd--; 4201556Srgrimes return 0; 4211556Srgrimes } 4221556Srgrimes if (jp->used && jp->state == 0) 4231556Srgrimes break; 4241556Srgrimes } 4251556Srgrimes } 42638521Scracauer } while (dowait(1, (struct job *)NULL) != -1); 42738521Scracauer in_waitcmd--; 42838521Scracauer 42938521Scracauer return 0; 4301556Srgrimes} 4311556Srgrimes 4321556Srgrimes 4331556Srgrimes 43417987Speterint 43520425Sstevejobidcmd(argc, argv) 43625905Ssteve int argc __unused; 43720425Ssteve char **argv; 43817987Speter{ 4391556Srgrimes struct job *jp; 4401556Srgrimes int i; 4411556Srgrimes 4421556Srgrimes jp = getjob(argv[1]); 4431556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 4441556Srgrimes out1fmt("%d", jp->ps[i].pid); 4451556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 4461556Srgrimes } 4471556Srgrimes return 0; 4481556Srgrimes} 4491556Srgrimes 4501556Srgrimes 4511556Srgrimes 4521556Srgrimes/* 4531556Srgrimes * Convert a job name to a job structure. 4541556Srgrimes */ 4551556Srgrimes 4561556SrgrimesSTATIC struct job * 4571556Srgrimesgetjob(name) 4581556Srgrimes char *name; 4591556Srgrimes { 4601556Srgrimes int jobno; 46125222Ssteve struct job *jp; 4621556Srgrimes int pid; 4631556Srgrimes int i; 4641556Srgrimes 4651556Srgrimes if (name == NULL) { 4661556Srgrimes#if JOBS 4671556Srgrimescurrentjob: 4681556Srgrimes if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0) 4691556Srgrimes error("No current job"); 4701556Srgrimes return &jobtab[jobno - 1]; 4711556Srgrimes#else 4721556Srgrimes error("No current job"); 4731556Srgrimes#endif 4741556Srgrimes } else if (name[0] == '%') { 4751556Srgrimes if (is_digit(name[1])) { 4761556Srgrimes jobno = number(name + 1); 4771556Srgrimes if (jobno > 0 && jobno <= njobs 4781556Srgrimes && jobtab[jobno - 1].used != 0) 4791556Srgrimes return &jobtab[jobno - 1]; 4801556Srgrimes#if JOBS 4811556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 4821556Srgrimes goto currentjob; 4831556Srgrimes#endif 4841556Srgrimes } else { 48525222Ssteve struct job *found = NULL; 4861556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 4871556Srgrimes if (jp->used && jp->nprocs > 0 4881556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 4891556Srgrimes if (found) 4901556Srgrimes error("%s: ambiguous", name); 4911556Srgrimes found = jp; 4921556Srgrimes } 4931556Srgrimes } 4941556Srgrimes if (found) 4951556Srgrimes return found; 4961556Srgrimes } 4971556Srgrimes } else if (is_number(name)) { 4981556Srgrimes pid = number(name); 4991556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5001556Srgrimes if (jp->used && jp->nprocs > 0 5011556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 5021556Srgrimes return jp; 5031556Srgrimes } 5041556Srgrimes } 5051556Srgrimes error("No such job: %s", name); 50617987Speter /*NOTREACHED*/ 50717987Speter return NULL; 5081556Srgrimes} 5091556Srgrimes 5101556Srgrimes 5111556Srgrimes 5121556Srgrimes/* 5131556Srgrimes * Return a new job structure, 5141556Srgrimes */ 5151556Srgrimes 5161556Srgrimesstruct job * 5171556Srgrimesmakejob(node, nprocs) 51825905Ssteve union node *node __unused; 51917987Speter int nprocs; 52017987Speter{ 5211556Srgrimes int i; 5221556Srgrimes struct job *jp; 5231556Srgrimes 5241556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 5251556Srgrimes if (--i < 0) { 5261556Srgrimes INTOFF; 5271556Srgrimes if (njobs == 0) { 5281556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 5291556Srgrimes } else { 5301556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 53117987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 53220425Ssteve /* Relocate `ps' pointers */ 53320425Ssteve for (i = 0; i < njobs; i++) 53420425Ssteve if (jp[i].ps == &jobtab[i].ps0) 53520425Ssteve jp[i].ps = &jp[i].ps0; 5361556Srgrimes ckfree(jobtab); 5371556Srgrimes jobtab = jp; 5381556Srgrimes } 5391556Srgrimes jp = jobtab + njobs; 5401556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 5411556Srgrimes INTON; 5421556Srgrimes break; 5431556Srgrimes } 5441556Srgrimes if (jp->used == 0) 5451556Srgrimes break; 5461556Srgrimes } 5471556Srgrimes INTOFF; 5481556Srgrimes jp->state = 0; 5491556Srgrimes jp->used = 1; 5501556Srgrimes jp->changed = 0; 5511556Srgrimes jp->nprocs = 0; 5521556Srgrimes#if JOBS 5531556Srgrimes jp->jobctl = jobctl; 5541556Srgrimes#endif 5551556Srgrimes if (nprocs > 1) { 5561556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 5571556Srgrimes } else { 5581556Srgrimes jp->ps = &jp->ps0; 5591556Srgrimes } 5601556Srgrimes INTON; 56117987Speter TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, 56217987Speter jp - jobtab + 1)); 5631556Srgrimes return jp; 5648855Srgrimes} 5651556Srgrimes 5661556Srgrimes 5671556Srgrimes/* 5681556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 5691556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 5701556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 5711556Srgrimes * be NULL. The mode parameter can be one of the following: 5721556Srgrimes * FORK_FG - Fork off a foreground process. 5731556Srgrimes * FORK_BG - Fork off a background process. 5741556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 5751556Srgrimes * process group even if job control is on. 5761556Srgrimes * 5771556Srgrimes * When job control is turned off, background processes have their standard 5781556Srgrimes * input redirected to /dev/null (except for the second and later processes 5791556Srgrimes * in a pipeline). 5801556Srgrimes */ 5811556Srgrimes 5821556Srgrimesint 5831556Srgrimesforkshell(jp, n, mode) 5841556Srgrimes union node *n; 5851556Srgrimes struct job *jp; 58617987Speter int mode; 58717987Speter{ 5881556Srgrimes int pid; 5891556Srgrimes int pgrp; 5901556Srgrimes 59117987Speter TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n, 59217987Speter mode)); 5931556Srgrimes INTOFF; 5941556Srgrimes pid = fork(); 5951556Srgrimes if (pid == -1) { 5961556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 5971556Srgrimes INTON; 59853891Scracauer error("Cannot fork: %s", strerror(errno)); 5991556Srgrimes } 6001556Srgrimes if (pid == 0) { 6011556Srgrimes struct job *p; 6021556Srgrimes int wasroot; 6031556Srgrimes int i; 6041556Srgrimes 6051556Srgrimes TRACE(("Child shell %d\n", getpid())); 6061556Srgrimes wasroot = rootshell; 6071556Srgrimes rootshell = 0; 6081556Srgrimes for (i = njobs, p = jobtab ; --i >= 0 ; p++) 6091556Srgrimes if (p->used) 6101556Srgrimes freejob(p); 6111556Srgrimes closescript(); 6121556Srgrimes INTON; 6131556Srgrimes clear_traps(); 6141556Srgrimes#if JOBS 6151556Srgrimes jobctl = 0; /* do job control only in root shell */ 6161556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 6171556Srgrimes if (jp == NULL || jp->nprocs == 0) 6181556Srgrimes pgrp = getpid(); 6191556Srgrimes else 6201556Srgrimes pgrp = jp->ps[0].pid; 62121352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 6221556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 62320425Ssteve#ifdef OLD_TTY_DRIVER 6241556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0) 62518016Speter error("TIOCSPGRP failed, errno=%d", errno); 62620425Ssteve#else 62720425Ssteve if (tcsetpgrp(2, pgrp) < 0) 62820425Ssteve error("tcsetpgrp failed, errno=%d", errno); 62920425Ssteve#endif 6301556Srgrimes } 6311556Srgrimes setsignal(SIGTSTP); 6321556Srgrimes setsignal(SIGTTOU); 6331556Srgrimes } else if (mode == FORK_BG) { 6341556Srgrimes ignoresig(SIGINT); 6351556Srgrimes ignoresig(SIGQUIT); 6361556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 6371556Srgrimes ! fd0_redirected_p ()) { 6381556Srgrimes close(0); 63969793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 64069793Sobrien error("Can't open %s: %s", 64169793Sobrien _PATH_DEVNULL, strerror(errno)); 6421556Srgrimes } 6431556Srgrimes } 6441556Srgrimes#else 6451556Srgrimes if (mode == FORK_BG) { 6461556Srgrimes ignoresig(SIGINT); 6471556Srgrimes ignoresig(SIGQUIT); 6481556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 6491556Srgrimes ! fd0_redirected_p ()) { 6501556Srgrimes close(0); 65169793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 65269793Sobrien error("Can't open %s: %s", 65369793Sobrien _PATH_DEVNULL, strerror(errno)); 6541556Srgrimes } 6551556Srgrimes } 6561556Srgrimes#endif 6571556Srgrimes if (wasroot && iflag) { 6581556Srgrimes setsignal(SIGINT); 6591556Srgrimes setsignal(SIGQUIT); 6601556Srgrimes setsignal(SIGTERM); 6611556Srgrimes } 6621556Srgrimes return pid; 6631556Srgrimes } 6641556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 6651556Srgrimes if (jp == NULL || jp->nprocs == 0) 6661556Srgrimes pgrp = pid; 6671556Srgrimes else 6681556Srgrimes pgrp = jp->ps[0].pid; 66917987Speter setpgid(pid, pgrp); 6701556Srgrimes } 6711556Srgrimes if (mode == FORK_BG) 6721556Srgrimes backgndpid = pid; /* set $! */ 6731556Srgrimes if (jp) { 6741556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 6751556Srgrimes ps->pid = pid; 6761556Srgrimes ps->status = -1; 6771556Srgrimes ps->cmd = nullstr; 6781556Srgrimes if (iflag && rootshell && n) 6791556Srgrimes ps->cmd = commandtext(n); 6801556Srgrimes } 6811556Srgrimes INTON; 6821556Srgrimes TRACE(("In parent shell: child = %d\n", pid)); 6831556Srgrimes return pid; 6841556Srgrimes} 6851556Srgrimes 6861556Srgrimes 6871556Srgrimes 6881556Srgrimes/* 6891556Srgrimes * Wait for job to finish. 6901556Srgrimes * 6911556Srgrimes * Under job control we have the problem that while a child process is 6921556Srgrimes * running interrupts generated by the user are sent to the child but not 6931556Srgrimes * to the shell. This means that an infinite loop started by an inter- 6941556Srgrimes * active user may be hard to kill. With job control turned off, an 6951556Srgrimes * interactive user may place an interactive program inside a loop. If 6961556Srgrimes * the interactive program catches interrupts, the user doesn't want 6971556Srgrimes * these interrupts to also abort the loop. The approach we take here 6981556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 69946684Skris * foreground process to terminate, and then send itself an interrupt 7001556Srgrimes * signal if the child process was terminated by an interrupt signal. 7011556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 7021556Srgrimes * exit on interrupt; unless these processes terminate themselves by 7031556Srgrimes * sending a signal to themselves (instead of calling exit) they will 7041556Srgrimes * confuse this approach. 7051556Srgrimes */ 7061556Srgrimes 7071556Srgrimesint 70845916Scracauerwaitforjob(jp, origstatus) 70925222Ssteve struct job *jp; 71045916Scracauer int *origstatus; 71145916Scracauer{ 7121556Srgrimes#if JOBS 71317987Speter int mypgrp = getpgrp(); 7141556Srgrimes#endif 7151556Srgrimes int status; 7161556Srgrimes int st; 7171556Srgrimes 7181556Srgrimes INTOFF; 7191556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 72038950Scracauer while (jp->state == 0) 72138950Scracauer if (dowait(1, jp) == -1) 72238950Scracauer dotrap(); 7231556Srgrimes#if JOBS 7241556Srgrimes if (jp->jobctl) { 72520425Ssteve#ifdef OLD_TTY_DRIVER 7261556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0) 72720425Ssteve error("TIOCSPGRP failed, errno=%d\n", errno); 72820425Ssteve#else 72920425Ssteve if (tcsetpgrp(2, mypgrp) < 0) 73020425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 73120425Ssteve#endif 7321556Srgrimes } 7331556Srgrimes if (jp->state == JOBSTOPPED) 7341556Srgrimes curjob = jp - jobtab + 1; 7351556Srgrimes#endif 7361556Srgrimes status = jp->ps[jp->nprocs - 1].status; 73745916Scracauer if (origstatus != NULL) 73845916Scracauer *origstatus = status; 7391556Srgrimes /* convert to 8 bits */ 74026104Ssteve if (WIFEXITED(status)) 74126104Ssteve st = WEXITSTATUS(status); 7421556Srgrimes#if JOBS 74326104Ssteve else if (WIFSTOPPED(status)) 74426104Ssteve st = WSTOPSIG(status) + 128; 7451556Srgrimes#endif 7461556Srgrimes else 74726104Ssteve st = WTERMSIG(status) + 128; 7481556Srgrimes if (! JOBS || jp->state == JOBDONE) 7491556Srgrimes freejob(jp); 75038521Scracauer if (int_pending()) { 75138521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 75238521Scracauer kill(getpid(), SIGINT); 75338521Scracauer else 75438521Scracauer CLEAR_PENDING_INT; 75538521Scracauer } 7561556Srgrimes INTON; 7571556Srgrimes return st; 7581556Srgrimes} 7591556Srgrimes 7601556Srgrimes 7611556Srgrimes 7621556Srgrimes/* 7631556Srgrimes * Wait for a process to terminate. 7641556Srgrimes */ 7651556Srgrimes 7661556SrgrimesSTATIC int 7671556Srgrimesdowait(block, job) 76817987Speter int block; 7691556Srgrimes struct job *job; 77017987Speter{ 7711556Srgrimes int pid; 7721556Srgrimes int status; 7731556Srgrimes struct procstat *sp; 7741556Srgrimes struct job *jp; 7751556Srgrimes struct job *thisjob; 7761556Srgrimes int done; 7771556Srgrimes int stopped; 7781556Srgrimes int core; 77926104Ssteve int sig; 7801556Srgrimes 78138950Scracauer in_dowait++; 7821556Srgrimes TRACE(("dowait(%d) called\n", block)); 7831556Srgrimes do { 7841556Srgrimes pid = waitproc(block, &status); 7851556Srgrimes TRACE(("wait returns %d, status=%d\n", pid, status)); 78672086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 78772086Scracauer (WIFSTOPPED(status) && !iflag)); 78838950Scracauer in_dowait--; 78938521Scracauer if (breakwaitcmd != 0) { 79038521Scracauer breakwaitcmd = 0; 79138521Scracauer return -1; 79238521Scracauer } 7931556Srgrimes if (pid <= 0) 7941556Srgrimes return pid; 7951556Srgrimes INTOFF; 7961556Srgrimes thisjob = NULL; 7971556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 7981556Srgrimes if (jp->used) { 7991556Srgrimes done = 1; 8001556Srgrimes stopped = 1; 8011556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 8021556Srgrimes if (sp->pid == -1) 8031556Srgrimes continue; 8041556Srgrimes if (sp->pid == pid) { 80526104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 80626104Ssteve pid, sp->status, status)); 8071556Srgrimes sp->status = status; 8081556Srgrimes thisjob = jp; 8091556Srgrimes } 8101556Srgrimes if (sp->status == -1) 8111556Srgrimes stopped = 0; 81226104Ssteve else if (WIFSTOPPED(sp->status)) 8131556Srgrimes done = 0; 8141556Srgrimes } 8151556Srgrimes if (stopped) { /* stopped or done */ 8161556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 8171556Srgrimes if (jp->state != state) { 8181556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 8191556Srgrimes jp->state = state; 8201556Srgrimes#if JOBS 8211556Srgrimes if (done && curjob == jp - jobtab + 1) 8221556Srgrimes curjob = 0; /* no current job */ 8231556Srgrimes#endif 8241556Srgrimes } 8251556Srgrimes } 8261556Srgrimes } 8271556Srgrimes } 8281556Srgrimes INTON; 8291556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 83026104Ssteve core = WCOREDUMP(status); 8311556Srgrimes#if JOBS 83226104Ssteve if (WIFSTOPPED(status)) 83326104Ssteve sig = WSTOPSIG(status); 83426104Ssteve else 8351556Srgrimes#endif 83626104Ssteve if (WIFEXITED(status)) 83726104Ssteve sig = 0; 83826104Ssteve else 83926104Ssteve sig = WTERMSIG(status); 84026104Ssteve 84126104Ssteve if (sig != 0 && sig != SIGINT && sig != SIGPIPE) { 8421556Srgrimes if (thisjob != job) 8431556Srgrimes outfmt(out2, "%d: ", pid); 8441556Srgrimes#if JOBS 84526104Ssteve if (sig == SIGTSTP && rootshell && iflag) 8461556Srgrimes outfmt(out2, "%%%d ", job - jobtab + 1); 8471556Srgrimes#endif 84826104Ssteve if (sig < NSIG && sys_siglist[sig]) 84926104Ssteve out2str(sys_siglist[sig]); 8501556Srgrimes else 85126104Ssteve outfmt(out2, "Signal %d", sig); 8521556Srgrimes if (core) 8531556Srgrimes out2str(" - core dumped"); 8541556Srgrimes out2c('\n'); 8551556Srgrimes flushout(&errout); 8561556Srgrimes } else { 85726104Ssteve TRACE(("Not printing status: status=%d, sig=%d\n", 85826104Ssteve status, sig)); 8591556Srgrimes } 8601556Srgrimes } else { 8611556Srgrimes TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job)); 8621556Srgrimes if (thisjob) 8631556Srgrimes thisjob->changed = 1; 8641556Srgrimes } 8651556Srgrimes return pid; 8661556Srgrimes} 8671556Srgrimes 8681556Srgrimes 8691556Srgrimes 8701556Srgrimes/* 8711556Srgrimes * Do a wait system call. If job control is compiled in, we accept 8721556Srgrimes * stopped processes. If block is zero, we return a value of zero 8731556Srgrimes * rather than blocking. 8741556Srgrimes * 8751556Srgrimes * System V doesn't have a non-blocking wait system call. It does 8761556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's 8771556Srgrimes * children dies. The obvious way to use SIGCLD would be to install 8781556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD 8791556Srgrimes * was received, and have waitproc bump another counter when it got 8801556Srgrimes * the status of a process. Waitproc would then know that a wait 8811556Srgrimes * system call would not block if the two counters were different. 8821556Srgrimes * This approach doesn't work because if a process has children that 8831556Srgrimes * have not been waited for, System V will send it a SIGCLD when it 8841556Srgrimes * installs a signal handler for SIGCLD. What this means is that when 8851556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously 8861556Srgrimes * until is runs out of stack space, unless it does a wait call before 8871556Srgrimes * restoring the signal handler. The code below takes advantage of 8881556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and 8891556Srgrimes * then checking to see whether it was called. If there are any 8901556Srgrimes * children to be waited for, it will be. 8911556Srgrimes * 8921556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking 8931556Srgrimes * waits at all. In this case, the user will not be informed when 8941556Srgrimes * a background process until the next time she runs a real program 8951556Srgrimes * (as opposed to running a builtin command or just typing return), 8961556Srgrimes * and the jobs command may give out of date information. 8971556Srgrimes */ 8981556Srgrimes 8991556Srgrimes#ifdef SYSV 90038521ScracauerSTATIC sig_atomic_t gotsigchild; 9011556Srgrimes 9021556SrgrimesSTATIC int onsigchild() { 9031556Srgrimes gotsigchild = 1; 9041556Srgrimes} 9051556Srgrimes#endif 9061556Srgrimes 9071556Srgrimes 9081556SrgrimesSTATIC int 9091556Srgrimeswaitproc(block, status) 91017987Speter int block; 9111556Srgrimes int *status; 91217987Speter{ 9131556Srgrimes#ifdef BSD 9141556Srgrimes int flags; 9151556Srgrimes 9161556Srgrimes#if JOBS 9171556Srgrimes flags = WUNTRACED; 9181556Srgrimes#else 9191556Srgrimes flags = 0; 9201556Srgrimes#endif 9211556Srgrimes if (block == 0) 9221556Srgrimes flags |= WNOHANG; 9231556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 9241556Srgrimes#else 9251556Srgrimes#ifdef SYSV 9261556Srgrimes int (*save)(); 9271556Srgrimes 9281556Srgrimes if (block == 0) { 9291556Srgrimes gotsigchild = 0; 9301556Srgrimes save = signal(SIGCLD, onsigchild); 9311556Srgrimes signal(SIGCLD, save); 9321556Srgrimes if (gotsigchild == 0) 9331556Srgrimes return 0; 9341556Srgrimes } 9351556Srgrimes return wait(status); 9361556Srgrimes#else 9371556Srgrimes if (block == 0) 9381556Srgrimes return 0; 9391556Srgrimes return wait(status); 9401556Srgrimes#endif 9411556Srgrimes#endif 9421556Srgrimes} 9431556Srgrimes 9441556Srgrimes/* 9451556Srgrimes * return 1 if there are stopped jobs, otherwise 0 9461556Srgrimes */ 9471556Srgrimesint job_warning = 0; 9481556Srgrimesint 9491556Srgrimesstoppedjobs() 9501556Srgrimes{ 95125222Ssteve int jobno; 95225222Ssteve struct job *jp; 9531556Srgrimes 9541556Srgrimes if (job_warning) 9551556Srgrimes return (0); 9561556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 9571556Srgrimes if (jp->used == 0) 9581556Srgrimes continue; 9591556Srgrimes if (jp->state == JOBSTOPPED) { 9601556Srgrimes out2str("You have stopped jobs.\n"); 9611556Srgrimes job_warning = 2; 9621556Srgrimes return (1); 9631556Srgrimes } 9641556Srgrimes } 9651556Srgrimes 9661556Srgrimes return (0); 9671556Srgrimes} 9681556Srgrimes 9691556Srgrimes/* 9701556Srgrimes * Return a string identifying a command (to be printed by the 9711556Srgrimes * jobs command. 9721556Srgrimes */ 9731556Srgrimes 9741556SrgrimesSTATIC char *cmdnextc; 9751556SrgrimesSTATIC int cmdnleft; 9761556Srgrimes#define MAXCMDTEXT 200 9771556Srgrimes 9781556Srgrimeschar * 9791556Srgrimescommandtext(n) 9801556Srgrimes union node *n; 9811556Srgrimes { 9821556Srgrimes char *name; 9831556Srgrimes 9841556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 9851556Srgrimes cmdnleft = MAXCMDTEXT - 4; 9861556Srgrimes cmdtxt(n); 9871556Srgrimes *cmdnextc = '\0'; 9881556Srgrimes return name; 9891556Srgrimes} 9901556Srgrimes 9911556Srgrimes 9921556SrgrimesSTATIC void 9931556Srgrimescmdtxt(n) 9941556Srgrimes union node *n; 9951556Srgrimes { 9961556Srgrimes union node *np; 9971556Srgrimes struct nodelist *lp; 9981556Srgrimes char *p; 9991556Srgrimes int i; 10001556Srgrimes char s[2]; 10011556Srgrimes 10021556Srgrimes if (n == NULL) 10031556Srgrimes return; 10041556Srgrimes switch (n->type) { 10051556Srgrimes case NSEMI: 10061556Srgrimes cmdtxt(n->nbinary.ch1); 10071556Srgrimes cmdputs("; "); 10081556Srgrimes cmdtxt(n->nbinary.ch2); 10091556Srgrimes break; 10101556Srgrimes case NAND: 10111556Srgrimes cmdtxt(n->nbinary.ch1); 10121556Srgrimes cmdputs(" && "); 10131556Srgrimes cmdtxt(n->nbinary.ch2); 10141556Srgrimes break; 10151556Srgrimes case NOR: 10161556Srgrimes cmdtxt(n->nbinary.ch1); 10171556Srgrimes cmdputs(" || "); 10181556Srgrimes cmdtxt(n->nbinary.ch2); 10191556Srgrimes break; 10201556Srgrimes case NPIPE: 10211556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 10221556Srgrimes cmdtxt(lp->n); 10231556Srgrimes if (lp->next) 10241556Srgrimes cmdputs(" | "); 10251556Srgrimes } 10261556Srgrimes break; 10271556Srgrimes case NSUBSHELL: 10281556Srgrimes cmdputs("("); 10291556Srgrimes cmdtxt(n->nredir.n); 10301556Srgrimes cmdputs(")"); 10311556Srgrimes break; 10321556Srgrimes case NREDIR: 10331556Srgrimes case NBACKGND: 10341556Srgrimes cmdtxt(n->nredir.n); 10351556Srgrimes break; 10361556Srgrimes case NIF: 10371556Srgrimes cmdputs("if "); 10381556Srgrimes cmdtxt(n->nif.test); 10391556Srgrimes cmdputs("; then "); 10401556Srgrimes cmdtxt(n->nif.ifpart); 10411556Srgrimes cmdputs("..."); 10421556Srgrimes break; 10431556Srgrimes case NWHILE: 10441556Srgrimes cmdputs("while "); 10451556Srgrimes goto until; 10461556Srgrimes case NUNTIL: 10471556Srgrimes cmdputs("until "); 10481556Srgrimesuntil: 10491556Srgrimes cmdtxt(n->nbinary.ch1); 10501556Srgrimes cmdputs("; do "); 10511556Srgrimes cmdtxt(n->nbinary.ch2); 10521556Srgrimes cmdputs("; done"); 10531556Srgrimes break; 10541556Srgrimes case NFOR: 10551556Srgrimes cmdputs("for "); 10561556Srgrimes cmdputs(n->nfor.var); 10571556Srgrimes cmdputs(" in ..."); 10581556Srgrimes break; 10591556Srgrimes case NCASE: 10601556Srgrimes cmdputs("case "); 10611556Srgrimes cmdputs(n->ncase.expr->narg.text); 10621556Srgrimes cmdputs(" in ..."); 10631556Srgrimes break; 10641556Srgrimes case NDEFUN: 10651556Srgrimes cmdputs(n->narg.text); 10661556Srgrimes cmdputs("() ..."); 10671556Srgrimes break; 10681556Srgrimes case NCMD: 10691556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 10701556Srgrimes cmdtxt(np); 10711556Srgrimes if (np->narg.next) 10721556Srgrimes cmdputs(" "); 10731556Srgrimes } 10741556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 10751556Srgrimes cmdputs(" "); 10761556Srgrimes cmdtxt(np); 10771556Srgrimes } 10781556Srgrimes break; 10791556Srgrimes case NARG: 10801556Srgrimes cmdputs(n->narg.text); 10811556Srgrimes break; 10821556Srgrimes case NTO: 10831556Srgrimes p = ">"; i = 1; goto redir; 10841556Srgrimes case NAPPEND: 10851556Srgrimes p = ">>"; i = 1; goto redir; 10861556Srgrimes case NTOFD: 10871556Srgrimes p = ">&"; i = 1; goto redir; 10881556Srgrimes case NFROM: 10891556Srgrimes p = "<"; i = 0; goto redir; 109066612Sbrian case NFROMTO: 109166612Sbrian p = "<>"; i = 0; goto redir; 10921556Srgrimes case NFROMFD: 10931556Srgrimes p = "<&"; i = 0; goto redir; 10941556Srgrimesredir: 10951556Srgrimes if (n->nfile.fd != i) { 10961556Srgrimes s[0] = n->nfile.fd + '0'; 10971556Srgrimes s[1] = '\0'; 10981556Srgrimes cmdputs(s); 10991556Srgrimes } 11001556Srgrimes cmdputs(p); 11011556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 11021556Srgrimes s[0] = n->ndup.dupfd + '0'; 11031556Srgrimes s[1] = '\0'; 11041556Srgrimes cmdputs(s); 11051556Srgrimes } else { 11061556Srgrimes cmdtxt(n->nfile.fname); 11071556Srgrimes } 11081556Srgrimes break; 11091556Srgrimes case NHERE: 11101556Srgrimes case NXHERE: 11111556Srgrimes cmdputs("<<..."); 11121556Srgrimes break; 11131556Srgrimes default: 11141556Srgrimes cmdputs("???"); 11151556Srgrimes break; 11161556Srgrimes } 11171556Srgrimes} 11181556Srgrimes 11191556Srgrimes 11201556Srgrimes 11211556SrgrimesSTATIC void 11221556Srgrimescmdputs(s) 11231556Srgrimes char *s; 11241556Srgrimes { 112525222Ssteve char *p, *q; 112625222Ssteve char c; 11271556Srgrimes int subtype = 0; 11281556Srgrimes 11291556Srgrimes if (cmdnleft <= 0) 11301556Srgrimes return; 11311556Srgrimes p = s; 11321556Srgrimes q = cmdnextc; 11331556Srgrimes while ((c = *p++) != '\0') { 11341556Srgrimes if (c == CTLESC) 11351556Srgrimes *q++ = *p++; 11361556Srgrimes else if (c == CTLVAR) { 11371556Srgrimes *q++ = '$'; 11381556Srgrimes if (--cmdnleft > 0) 11391556Srgrimes *q++ = '{'; 11401556Srgrimes subtype = *p++; 11411556Srgrimes } else if (c == '=' && subtype != 0) { 11421556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 11431556Srgrimes subtype = 0; 11441556Srgrimes } else if (c == CTLENDVAR) { 11451556Srgrimes *q++ = '}'; 114618954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 11471556Srgrimes cmdnleft++; /* ignore it */ 11481556Srgrimes else 11491556Srgrimes *q++ = c; 11501556Srgrimes if (--cmdnleft <= 0) { 11511556Srgrimes *q++ = '.'; 11521556Srgrimes *q++ = '.'; 11531556Srgrimes *q++ = '.'; 11541556Srgrimes break; 11551556Srgrimes } 11561556Srgrimes } 11571556Srgrimes cmdnextc = q; 11581556Srgrimes} 1159