jobs.c revision 26104
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. 353044Sdg * 3626104Ssteve * $Id: jobs.c,v 1.14 1997/05/19 00:18:42 steve Exp $ 371556Srgrimes */ 381556Srgrimes 391556Srgrimes#ifndef lint 4020425Sstevestatic char const sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95"; 411556Srgrimes#endif /* not lint */ 421556Srgrimes 4317987Speter#include <fcntl.h> 4417987Speter#include <signal.h> 4517987Speter#include <errno.h> 4617987Speter#include <unistd.h> 4717987Speter#include <stdlib.h> 4817987Speter#include <sys/types.h> 4917987Speter#include <sys/param.h> 5017987Speter#ifdef BSD 5117987Speter#include <sys/wait.h> 5217987Speter#include <sys/time.h> 5317987Speter#include <sys/resource.h> 5417987Speter#endif 5518018Speter#include <sys/ioctl.h> 5617987Speter 571556Srgrimes#include "shell.h" 581556Srgrimes#if JOBS 5925222Ssteve#if OLD_TTY_DRIVER 601556Srgrimes#include "sgtty.h" 6117987Speter#else 6217987Speter#include <termios.h> 6317987Speter#endif 641556Srgrimes#undef CEOF /* syntax.h redefines this */ 651556Srgrimes#endif 6617987Speter#include "redir.h" 6717987Speter#include "show.h" 681556Srgrimes#include "main.h" 691556Srgrimes#include "parser.h" 701556Srgrimes#include "nodes.h" 711556Srgrimes#include "jobs.h" 721556Srgrimes#include "options.h" 731556Srgrimes#include "trap.h" 741556Srgrimes#include "syntax.h" 751556Srgrimes#include "input.h" 761556Srgrimes#include "output.h" 771556Srgrimes#include "memalloc.h" 781556Srgrimes#include "error.h" 791556Srgrimes#include "mystring.h" 801556Srgrimes 811556Srgrimes 821556Srgrimesstruct job *jobtab; /* array of jobs */ 831556Srgrimesint njobs; /* size of array */ 841556SrgrimesMKINIT short backgndpid = -1; /* pid of last background process */ 851556Srgrimes#if JOBS 861556Srgrimesint initialpgrp; /* pgrp of shell on invocation */ 871556Srgrimesshort curjob; /* current job */ 881556Srgrimes#endif 891556Srgrimes 9020425Ssteve#if JOBS 9117987SpeterSTATIC void restartjob __P((struct job *)); 9220425Ssteve#endif 9317987SpeterSTATIC void freejob __P((struct job *)); 9417987SpeterSTATIC struct job *getjob __P((char *)); 9517987SpeterSTATIC int dowait __P((int, struct job *)); 9620425Ssteve#if SYSV 9717987SpeterSTATIC int onsigchild __P((void)); 9820425Ssteve#endif 9917987SpeterSTATIC int waitproc __P((int, int *)); 10017987SpeterSTATIC void cmdtxt __P((union node *)); 10117987SpeterSTATIC void cmdputs __P((char *)); 1021556Srgrimes 1031556Srgrimes 1041556Srgrimes/* 1051556Srgrimes * Turn job control on and off. 1061556Srgrimes * 1071556Srgrimes * Note: This code assumes that the third arg to ioctl is a character 1081556Srgrimes * pointer, which is true on Berkeley systems but not System V. Since 1091556Srgrimes * System V doesn't have job control yet, this isn't a problem now. 1101556Srgrimes */ 1111556Srgrimes 1121556SrgrimesMKINIT int jobctl; 1131556Srgrimes 11420425Ssteve#if JOBS 1151556Srgrimesvoid 11620425Sstevesetjobctl(on) 11717987Speter int on; 11817987Speter{ 1191556Srgrimes#ifdef OLD_TTY_DRIVER 1201556Srgrimes int ldisc; 1211556Srgrimes#endif 1221556Srgrimes 1231556Srgrimes if (on == jobctl || rootshell == 0) 1241556Srgrimes return; 1251556Srgrimes if (on) { 1261556Srgrimes do { /* while we are in the background */ 12720425Ssteve#ifdef OLD_TTY_DRIVER 1281556Srgrimes if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) { 12920425Ssteve#else 13020425Ssteve initialpgrp = tcgetpgrp(2); 13120425Ssteve if (initialpgrp < 0) { 13220425Ssteve#endif 1331556Srgrimes out2str("sh: can't access tty; job control turned off\n"); 1341556Srgrimes mflag = 0; 1351556Srgrimes return; 1361556Srgrimes } 1371556Srgrimes if (initialpgrp == -1) 13817987Speter initialpgrp = getpgrp(); 13917987Speter else if (initialpgrp != getpgrp()) { 1401556Srgrimes killpg(initialpgrp, SIGTTIN); 1411556Srgrimes continue; 1421556Srgrimes } 1431556Srgrimes } while (0); 1441556Srgrimes#ifdef OLD_TTY_DRIVER 1451556Srgrimes if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) { 1461556Srgrimes out2str("sh: need new tty driver to run job control; job control turned off\n"); 1471556Srgrimes mflag = 0; 1481556Srgrimes return; 1491556Srgrimes } 1501556Srgrimes#endif 1511556Srgrimes setsignal(SIGTSTP); 1521556Srgrimes setsignal(SIGTTOU); 1531556Srgrimes setsignal(SIGTTIN); 15417987Speter setpgid(0, rootpid); 15520425Ssteve#ifdef OLD_TTY_DRIVER 1561556Srgrimes ioctl(2, TIOCSPGRP, (char *)&rootpid); 15720425Ssteve#else 15820425Ssteve tcsetpgrp(2, rootpid); 15920425Ssteve#endif 1601556Srgrimes } else { /* turning job control off */ 16117987Speter setpgid(0, initialpgrp); 16220425Ssteve#ifdef OLD_TTY_DRIVER 1631556Srgrimes ioctl(2, TIOCSPGRP, (char *)&initialpgrp); 16420425Ssteve#else 16520425Ssteve tcsetpgrp(2, initialpgrp); 16620425Ssteve#endif 1671556Srgrimes setsignal(SIGTSTP); 1681556Srgrimes setsignal(SIGTTOU); 1691556Srgrimes setsignal(SIGTTIN); 1701556Srgrimes } 1711556Srgrimes jobctl = on; 1721556Srgrimes} 17320425Ssteve#endif 1741556Srgrimes 1751556Srgrimes 1761556Srgrimes#ifdef mkinit 17717987SpeterINCLUDE <stdlib.h> 1781556Srgrimes 1791556SrgrimesSHELLPROC { 1801556Srgrimes backgndpid = -1; 1811556Srgrimes#if JOBS 1821556Srgrimes jobctl = 0; 1831556Srgrimes#endif 1841556Srgrimes} 1851556Srgrimes 1861556Srgrimes#endif 1871556Srgrimes 1881556Srgrimes 1891556Srgrimes 1901556Srgrimes#if JOBS 19117987Speterint 19217987Speterfgcmd(argc, argv) 19325905Ssteve int argc __unused; 19420425Ssteve char **argv; 19517987Speter{ 1961556Srgrimes struct job *jp; 1971556Srgrimes int pgrp; 1981556Srgrimes int status; 1991556Srgrimes 2001556Srgrimes jp = getjob(argv[1]); 2011556Srgrimes if (jp->jobctl == 0) 2021556Srgrimes error("job not created under job control"); 2031556Srgrimes pgrp = jp->ps[0].pid; 20420425Ssteve#ifdef OLD_TTY_DRIVER 2051556Srgrimes ioctl(2, TIOCSPGRP, (char *)&pgrp); 20620425Ssteve#else 20720425Ssteve tcsetpgrp(2, pgrp); 20820425Ssteve#endif 2091556Srgrimes restartjob(jp); 2101556Srgrimes INTOFF; 2111556Srgrimes status = waitforjob(jp); 2121556Srgrimes INTON; 2131556Srgrimes return status; 2141556Srgrimes} 2151556Srgrimes 2161556Srgrimes 21717987Speterint 21817987Speterbgcmd(argc, argv) 21917987Speter int argc; 22020425Ssteve char **argv; 22117987Speter{ 2221556Srgrimes struct job *jp; 2231556Srgrimes 2241556Srgrimes do { 2251556Srgrimes jp = getjob(*++argv); 2261556Srgrimes if (jp->jobctl == 0) 2271556Srgrimes error("job not created under job control"); 2281556Srgrimes restartjob(jp); 2291556Srgrimes } while (--argc > 1); 2301556Srgrimes return 0; 2311556Srgrimes} 2321556Srgrimes 2331556Srgrimes 2341556SrgrimesSTATIC void 2351556Srgrimesrestartjob(jp) 2361556Srgrimes struct job *jp; 23717987Speter{ 2381556Srgrimes struct procstat *ps; 2391556Srgrimes int i; 2401556Srgrimes 2411556Srgrimes if (jp->state == JOBDONE) 2421556Srgrimes return; 2431556Srgrimes INTOFF; 2441556Srgrimes killpg(jp->ps[0].pid, SIGCONT); 2451556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 24626104Ssteve if (WIFSTOPPED(ps->status)) { 2471556Srgrimes ps->status = -1; 2481556Srgrimes jp->state = 0; 2491556Srgrimes } 2501556Srgrimes } 2511556Srgrimes INTON; 2521556Srgrimes} 2531556Srgrimes#endif 2541556Srgrimes 2551556Srgrimes 2561556Srgrimesint 25717987Speterjobscmd(argc, argv) 25825905Ssteve int argc __unused; 25925905Ssteve char **argv __unused; 26017987Speter{ 2611556Srgrimes showjobs(0); 2621556Srgrimes return 0; 2631556Srgrimes} 2641556Srgrimes 2651556Srgrimes 2661556Srgrimes/* 2671556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 2681556Srgrimes * statuses have changed since the last call to showjobs. 2691556Srgrimes * 2701556Srgrimes * If the shell is interrupted in the process of creating a job, the 2711556Srgrimes * result may be a job structure containing zero processes. Such structures 2721556Srgrimes * will be freed here. 2731556Srgrimes */ 2741556Srgrimes 2751556Srgrimesvoid 27620425Ssteveshowjobs(change) 27717987Speter int change; 27817987Speter{ 2791556Srgrimes int jobno; 2801556Srgrimes int procno; 2811556Srgrimes int i; 2821556Srgrimes struct job *jp; 2831556Srgrimes struct procstat *ps; 2841556Srgrimes int col; 2851556Srgrimes char s[64]; 2861556Srgrimes 2871556Srgrimes TRACE(("showjobs(%d) called\n", change)); 2881556Srgrimes while (dowait(0, (struct job *)NULL) > 0); 2891556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 2901556Srgrimes if (! jp->used) 2911556Srgrimes continue; 2921556Srgrimes if (jp->nprocs == 0) { 2931556Srgrimes freejob(jp); 2941556Srgrimes continue; 2951556Srgrimes } 2961556Srgrimes if (change && ! jp->changed) 2971556Srgrimes continue; 2981556Srgrimes procno = jp->nprocs; 2991556Srgrimes for (ps = jp->ps ; ; ps++) { /* for each process */ 3001556Srgrimes if (ps == jp->ps) 3011556Srgrimes fmtstr(s, 64, "[%d] %d ", jobno, ps->pid); 3021556Srgrimes else 3031556Srgrimes fmtstr(s, 64, " %d ", ps->pid); 3041556Srgrimes out1str(s); 3051556Srgrimes col = strlen(s); 3061556Srgrimes s[0] = '\0'; 3071556Srgrimes if (ps->status == -1) { 3081556Srgrimes /* don't print anything */ 30926104Ssteve } else if (WIFEXITED(ps->status)) { 31026104Ssteve fmtstr(s, 64, "Exit %d", WEXITSTATUS(ps->status)); 3111556Srgrimes } else { 3121556Srgrimes#if JOBS 31326104Ssteve if (WIFSTOPPED(ps->status)) 31426104Ssteve i = WSTOPSIG(ps->status); 31526104Ssteve else 3161556Srgrimes#endif 31726104Ssteve i = WTERMSIG(ps->status); 31817987Speter if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F]) 31917987Speter scopy(sys_siglist[i & 0x7F], s); 3201556Srgrimes else 3211556Srgrimes fmtstr(s, 64, "Signal %d", i & 0x7F); 32226104Ssteve if (WCOREDUMP(ps->status)) 3231556Srgrimes strcat(s, " (core dumped)"); 3241556Srgrimes } 3251556Srgrimes out1str(s); 3261556Srgrimes col += strlen(s); 3271556Srgrimes do { 3281556Srgrimes out1c(' '); 3291556Srgrimes col++; 3301556Srgrimes } while (col < 30); 3311556Srgrimes out1str(ps->cmd); 3321556Srgrimes out1c('\n'); 3331556Srgrimes if (--procno <= 0) 3341556Srgrimes break; 3351556Srgrimes } 3361556Srgrimes jp->changed = 0; 3371556Srgrimes if (jp->state == JOBDONE) { 3381556Srgrimes freejob(jp); 3391556Srgrimes } 3401556Srgrimes } 3411556Srgrimes} 3421556Srgrimes 3431556Srgrimes 3441556Srgrimes/* 3451556Srgrimes * Mark a job structure as unused. 3461556Srgrimes */ 3471556Srgrimes 3481556SrgrimesSTATIC void 3491556Srgrimesfreejob(jp) 3501556Srgrimes struct job *jp; 3511556Srgrimes { 3521556Srgrimes struct procstat *ps; 3531556Srgrimes int i; 3541556Srgrimes 3551556Srgrimes INTOFF; 3561556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 3571556Srgrimes if (ps->cmd != nullstr) 3581556Srgrimes ckfree(ps->cmd); 3591556Srgrimes } 3601556Srgrimes if (jp->ps != &jp->ps0) 3611556Srgrimes ckfree(jp->ps); 3621556Srgrimes jp->used = 0; 3631556Srgrimes#if JOBS 3641556Srgrimes if (curjob == jp - jobtab + 1) 3651556Srgrimes curjob = 0; 3661556Srgrimes#endif 3671556Srgrimes INTON; 3681556Srgrimes} 3691556Srgrimes 3701556Srgrimes 3711556Srgrimes 3721556Srgrimesint 37320425Sstevewaitcmd(argc, argv) 37417987Speter int argc; 37520425Ssteve char **argv; 37617987Speter{ 3771556Srgrimes struct job *job; 37826104Ssteve int status, retval; 3791556Srgrimes struct job *jp; 3801556Srgrimes 3811556Srgrimes if (argc > 1) { 3821556Srgrimes job = getjob(argv[1]); 3831556Srgrimes } else { 3841556Srgrimes job = NULL; 3851556Srgrimes } 3861556Srgrimes for (;;) { /* loop until process terminated or stopped */ 3871556Srgrimes if (job != NULL) { 3881556Srgrimes if (job->state) { 3891556Srgrimes status = job->ps[job->nprocs - 1].status; 39026104Ssteve if (WIFEXITED(status)) 39126104Ssteve retval = WEXITSTATUS(status); 3921556Srgrimes#if JOBS 39326104Ssteve else if (WIFSTOPPED(status)) 39426104Ssteve retval = WSTOPSIG(status) + 128; 3951556Srgrimes#endif 3961556Srgrimes else 39726104Ssteve retval = WTERMSIG(status) + 128; 3981556Srgrimes if (! iflag) 3991556Srgrimes freejob(job); 40026104Ssteve return retval; 4011556Srgrimes } 4021556Srgrimes } else { 4031556Srgrimes for (jp = jobtab ; ; jp++) { 4041556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 4051556Srgrimes return 0; 4061556Srgrimes } 4071556Srgrimes if (jp->used && jp->state == 0) 4081556Srgrimes break; 4091556Srgrimes } 4101556Srgrimes } 4111556Srgrimes dowait(1, (struct job *)NULL); 4121556Srgrimes } 4131556Srgrimes} 4141556Srgrimes 4151556Srgrimes 4161556Srgrimes 41717987Speterint 41820425Sstevejobidcmd(argc, argv) 41925905Ssteve int argc __unused; 42020425Ssteve char **argv; 42117987Speter{ 4221556Srgrimes struct job *jp; 4231556Srgrimes int i; 4241556Srgrimes 4251556Srgrimes jp = getjob(argv[1]); 4261556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 4271556Srgrimes out1fmt("%d", jp->ps[i].pid); 4281556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 4291556Srgrimes } 4301556Srgrimes return 0; 4311556Srgrimes} 4321556Srgrimes 4331556Srgrimes 4341556Srgrimes 4351556Srgrimes/* 4361556Srgrimes * Convert a job name to a job structure. 4371556Srgrimes */ 4381556Srgrimes 4391556SrgrimesSTATIC struct job * 4401556Srgrimesgetjob(name) 4411556Srgrimes char *name; 4421556Srgrimes { 4431556Srgrimes int jobno; 44425222Ssteve struct job *jp; 4451556Srgrimes int pid; 4461556Srgrimes int i; 4471556Srgrimes 4481556Srgrimes if (name == NULL) { 4491556Srgrimes#if JOBS 4501556Srgrimescurrentjob: 4511556Srgrimes if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0) 4521556Srgrimes error("No current job"); 4531556Srgrimes return &jobtab[jobno - 1]; 4541556Srgrimes#else 4551556Srgrimes error("No current job"); 4561556Srgrimes#endif 4571556Srgrimes } else if (name[0] == '%') { 4581556Srgrimes if (is_digit(name[1])) { 4591556Srgrimes jobno = number(name + 1); 4601556Srgrimes if (jobno > 0 && jobno <= njobs 4611556Srgrimes && jobtab[jobno - 1].used != 0) 4621556Srgrimes return &jobtab[jobno - 1]; 4631556Srgrimes#if JOBS 4641556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 4651556Srgrimes goto currentjob; 4661556Srgrimes#endif 4671556Srgrimes } else { 46825222Ssteve struct job *found = NULL; 4691556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 4701556Srgrimes if (jp->used && jp->nprocs > 0 4711556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 4721556Srgrimes if (found) 4731556Srgrimes error("%s: ambiguous", name); 4741556Srgrimes found = jp; 4751556Srgrimes } 4761556Srgrimes } 4771556Srgrimes if (found) 4781556Srgrimes return found; 4791556Srgrimes } 4801556Srgrimes } else if (is_number(name)) { 4811556Srgrimes pid = number(name); 4821556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 4831556Srgrimes if (jp->used && jp->nprocs > 0 4841556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 4851556Srgrimes return jp; 4861556Srgrimes } 4871556Srgrimes } 4881556Srgrimes error("No such job: %s", name); 48917987Speter /*NOTREACHED*/ 49017987Speter return NULL; 4911556Srgrimes} 4921556Srgrimes 4931556Srgrimes 4941556Srgrimes 4951556Srgrimes/* 4961556Srgrimes * Return a new job structure, 4971556Srgrimes */ 4981556Srgrimes 4991556Srgrimesstruct job * 5001556Srgrimesmakejob(node, nprocs) 50125905Ssteve union node *node __unused; 50217987Speter int nprocs; 50317987Speter{ 5041556Srgrimes int i; 5051556Srgrimes struct job *jp; 5061556Srgrimes 5071556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 5081556Srgrimes if (--i < 0) { 5091556Srgrimes INTOFF; 5101556Srgrimes if (njobs == 0) { 5111556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 5121556Srgrimes } else { 5131556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 51417987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 51520425Ssteve /* Relocate `ps' pointers */ 51620425Ssteve for (i = 0; i < njobs; i++) 51720425Ssteve if (jp[i].ps == &jobtab[i].ps0) 51820425Ssteve jp[i].ps = &jp[i].ps0; 5191556Srgrimes ckfree(jobtab); 5201556Srgrimes jobtab = jp; 5211556Srgrimes } 5221556Srgrimes jp = jobtab + njobs; 5231556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 5241556Srgrimes INTON; 5251556Srgrimes break; 5261556Srgrimes } 5271556Srgrimes if (jp->used == 0) 5281556Srgrimes break; 5291556Srgrimes } 5301556Srgrimes INTOFF; 5311556Srgrimes jp->state = 0; 5321556Srgrimes jp->used = 1; 5331556Srgrimes jp->changed = 0; 5341556Srgrimes jp->nprocs = 0; 5351556Srgrimes#if JOBS 5361556Srgrimes jp->jobctl = jobctl; 5371556Srgrimes#endif 5381556Srgrimes if (nprocs > 1) { 5391556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 5401556Srgrimes } else { 5411556Srgrimes jp->ps = &jp->ps0; 5421556Srgrimes } 5431556Srgrimes INTON; 54417987Speter TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, 54517987Speter jp - jobtab + 1)); 5461556Srgrimes return jp; 5478855Srgrimes} 5481556Srgrimes 5491556Srgrimes 5501556Srgrimes/* 5511556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 5521556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 5531556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 5541556Srgrimes * be NULL. The mode parameter can be one of the following: 5551556Srgrimes * FORK_FG - Fork off a foreground process. 5561556Srgrimes * FORK_BG - Fork off a background process. 5571556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 5581556Srgrimes * process group even if job control is on. 5591556Srgrimes * 5601556Srgrimes * When job control is turned off, background processes have their standard 5611556Srgrimes * input redirected to /dev/null (except for the second and later processes 5621556Srgrimes * in a pipeline). 5631556Srgrimes */ 5641556Srgrimes 5651556Srgrimesint 5661556Srgrimesforkshell(jp, n, mode) 5671556Srgrimes union node *n; 5681556Srgrimes struct job *jp; 56917987Speter int mode; 57017987Speter{ 5711556Srgrimes int pid; 5721556Srgrimes int pgrp; 5731556Srgrimes 57417987Speter TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n, 57517987Speter mode)); 5761556Srgrimes INTOFF; 5771556Srgrimes pid = fork(); 5781556Srgrimes if (pid == -1) { 5791556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 5801556Srgrimes INTON; 5811556Srgrimes error("Cannot fork"); 5821556Srgrimes } 5831556Srgrimes if (pid == 0) { 5841556Srgrimes struct job *p; 5851556Srgrimes int wasroot; 5861556Srgrimes int i; 5871556Srgrimes 5881556Srgrimes TRACE(("Child shell %d\n", getpid())); 5891556Srgrimes wasroot = rootshell; 5901556Srgrimes rootshell = 0; 5911556Srgrimes for (i = njobs, p = jobtab ; --i >= 0 ; p++) 5921556Srgrimes if (p->used) 5931556Srgrimes freejob(p); 5941556Srgrimes closescript(); 5951556Srgrimes INTON; 5961556Srgrimes clear_traps(); 5971556Srgrimes#if JOBS 5981556Srgrimes jobctl = 0; /* do job control only in root shell */ 5991556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 6001556Srgrimes if (jp == NULL || jp->nprocs == 0) 6011556Srgrimes pgrp = getpid(); 6021556Srgrimes else 6031556Srgrimes pgrp = jp->ps[0].pid; 60421352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 6051556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 60620425Ssteve#ifdef OLD_TTY_DRIVER 6071556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0) 60818016Speter error("TIOCSPGRP failed, errno=%d", errno); 60920425Ssteve#else 61020425Ssteve if (tcsetpgrp(2, pgrp) < 0) 61120425Ssteve error("tcsetpgrp failed, errno=%d", errno); 61220425Ssteve#endif 6131556Srgrimes } 6141556Srgrimes setsignal(SIGTSTP); 6151556Srgrimes setsignal(SIGTTOU); 6161556Srgrimes } else if (mode == FORK_BG) { 6171556Srgrimes ignoresig(SIGINT); 6181556Srgrimes ignoresig(SIGQUIT); 6191556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 6201556Srgrimes ! fd0_redirected_p ()) { 6211556Srgrimes close(0); 6221556Srgrimes if (open("/dev/null", O_RDONLY) != 0) 6231556Srgrimes error("Can't open /dev/null"); 6241556Srgrimes } 6251556Srgrimes } 6261556Srgrimes#else 6271556Srgrimes if (mode == FORK_BG) { 6281556Srgrimes ignoresig(SIGINT); 6291556Srgrimes ignoresig(SIGQUIT); 6301556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 6311556Srgrimes ! fd0_redirected_p ()) { 6321556Srgrimes close(0); 6331556Srgrimes if (open("/dev/null", O_RDONLY) != 0) 6341556Srgrimes error("Can't open /dev/null"); 6351556Srgrimes } 6361556Srgrimes } 6371556Srgrimes#endif 6381556Srgrimes if (wasroot && iflag) { 6391556Srgrimes setsignal(SIGINT); 6401556Srgrimes setsignal(SIGQUIT); 6411556Srgrimes setsignal(SIGTERM); 6421556Srgrimes } 6431556Srgrimes return pid; 6441556Srgrimes } 6451556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 6461556Srgrimes if (jp == NULL || jp->nprocs == 0) 6471556Srgrimes pgrp = pid; 6481556Srgrimes else 6491556Srgrimes pgrp = jp->ps[0].pid; 65017987Speter setpgid(pid, pgrp); 6511556Srgrimes } 6521556Srgrimes if (mode == FORK_BG) 6531556Srgrimes backgndpid = pid; /* set $! */ 6541556Srgrimes if (jp) { 6551556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 6561556Srgrimes ps->pid = pid; 6571556Srgrimes ps->status = -1; 6581556Srgrimes ps->cmd = nullstr; 6591556Srgrimes if (iflag && rootshell && n) 6601556Srgrimes ps->cmd = commandtext(n); 6611556Srgrimes } 6621556Srgrimes INTON; 6631556Srgrimes TRACE(("In parent shell: child = %d\n", pid)); 6641556Srgrimes return pid; 6651556Srgrimes} 6661556Srgrimes 6671556Srgrimes 6681556Srgrimes 6691556Srgrimes/* 6701556Srgrimes * Wait for job to finish. 6711556Srgrimes * 6721556Srgrimes * Under job control we have the problem that while a child process is 6731556Srgrimes * running interrupts generated by the user are sent to the child but not 6741556Srgrimes * to the shell. This means that an infinite loop started by an inter- 6751556Srgrimes * active user may be hard to kill. With job control turned off, an 6761556Srgrimes * interactive user may place an interactive program inside a loop. If 6771556Srgrimes * the interactive program catches interrupts, the user doesn't want 6781556Srgrimes * these interrupts to also abort the loop. The approach we take here 6791556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 6801556Srgrimes * forground process to terminate, and then send itself an interrupt 6811556Srgrimes * signal if the child process was terminated by an interrupt signal. 6821556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 6831556Srgrimes * exit on interrupt; unless these processes terminate themselves by 6841556Srgrimes * sending a signal to themselves (instead of calling exit) they will 6851556Srgrimes * confuse this approach. 6861556Srgrimes */ 6871556Srgrimes 6881556Srgrimesint 6891556Srgrimeswaitforjob(jp) 69025222Ssteve struct job *jp; 6911556Srgrimes { 6921556Srgrimes#if JOBS 69317987Speter int mypgrp = getpgrp(); 6941556Srgrimes#endif 6951556Srgrimes int status; 6961556Srgrimes int st; 6971556Srgrimes 6981556Srgrimes INTOFF; 6991556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 7001556Srgrimes while (jp->state == 0) { 7011556Srgrimes dowait(1, jp); 7021556Srgrimes } 7031556Srgrimes#if JOBS 7041556Srgrimes if (jp->jobctl) { 70520425Ssteve#ifdef OLD_TTY_DRIVER 7061556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0) 70720425Ssteve error("TIOCSPGRP failed, errno=%d\n", errno); 70820425Ssteve#else 70920425Ssteve if (tcsetpgrp(2, mypgrp) < 0) 71020425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 71120425Ssteve#endif 7121556Srgrimes } 7131556Srgrimes if (jp->state == JOBSTOPPED) 7141556Srgrimes curjob = jp - jobtab + 1; 7151556Srgrimes#endif 7161556Srgrimes status = jp->ps[jp->nprocs - 1].status; 7171556Srgrimes /* convert to 8 bits */ 71826104Ssteve if (WIFEXITED(status)) 71926104Ssteve st = WEXITSTATUS(status); 7201556Srgrimes#if JOBS 72126104Ssteve else if (WIFSTOPPED(status)) 72226104Ssteve st = WSTOPSIG(status) + 128; 7231556Srgrimes#endif 7241556Srgrimes else 72526104Ssteve st = WTERMSIG(status) + 128; 7261556Srgrimes if (! JOBS || jp->state == JOBDONE) 7271556Srgrimes freejob(jp); 7281556Srgrimes CLEAR_PENDING_INT; 72926104Ssteve if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 7301556Srgrimes kill(getpid(), SIGINT); 7311556Srgrimes INTON; 7321556Srgrimes return st; 7331556Srgrimes} 7341556Srgrimes 7351556Srgrimes 7361556Srgrimes 7371556Srgrimes/* 7381556Srgrimes * Wait for a process to terminate. 7391556Srgrimes */ 7401556Srgrimes 7411556SrgrimesSTATIC int 7421556Srgrimesdowait(block, job) 74317987Speter int block; 7441556Srgrimes struct job *job; 74517987Speter{ 7461556Srgrimes int pid; 7471556Srgrimes int status; 7481556Srgrimes struct procstat *sp; 7491556Srgrimes struct job *jp; 7501556Srgrimes struct job *thisjob; 7511556Srgrimes int done; 7521556Srgrimes int stopped; 7531556Srgrimes int core; 75426104Ssteve int sig; 7551556Srgrimes 7561556Srgrimes TRACE(("dowait(%d) called\n", block)); 7571556Srgrimes do { 7581556Srgrimes pid = waitproc(block, &status); 7591556Srgrimes TRACE(("wait returns %d, status=%d\n", pid, status)); 7601556Srgrimes } while (pid == -1 && errno == EINTR); 7611556Srgrimes if (pid <= 0) 7621556Srgrimes return pid; 7631556Srgrimes INTOFF; 7641556Srgrimes thisjob = NULL; 7651556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 7661556Srgrimes if (jp->used) { 7671556Srgrimes done = 1; 7681556Srgrimes stopped = 1; 7691556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 7701556Srgrimes if (sp->pid == -1) 7711556Srgrimes continue; 7721556Srgrimes if (sp->pid == pid) { 77326104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 77426104Ssteve pid, sp->status, status)); 7751556Srgrimes sp->status = status; 7761556Srgrimes thisjob = jp; 7771556Srgrimes } 7781556Srgrimes if (sp->status == -1) 7791556Srgrimes stopped = 0; 78026104Ssteve else if (WIFSTOPPED(sp->status)) 7811556Srgrimes done = 0; 7821556Srgrimes } 7831556Srgrimes if (stopped) { /* stopped or done */ 7841556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 7851556Srgrimes if (jp->state != state) { 7861556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 7871556Srgrimes jp->state = state; 7881556Srgrimes#if JOBS 7891556Srgrimes if (done && curjob == jp - jobtab + 1) 7901556Srgrimes curjob = 0; /* no current job */ 7911556Srgrimes#endif 7921556Srgrimes } 7931556Srgrimes } 7941556Srgrimes } 7951556Srgrimes } 7961556Srgrimes INTON; 7971556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 79826104Ssteve core = WCOREDUMP(status); 7991556Srgrimes#if JOBS 80026104Ssteve if (WIFSTOPPED(status)) 80126104Ssteve sig = WSTOPSIG(status); 80226104Ssteve else 8031556Srgrimes#endif 80426104Ssteve if (WIFEXITED(status)) 80526104Ssteve sig = 0; 80626104Ssteve else 80726104Ssteve sig = WTERMSIG(status); 80826104Ssteve 80926104Ssteve if (sig != 0 && sig != SIGINT && sig != SIGPIPE) { 8101556Srgrimes if (thisjob != job) 8111556Srgrimes outfmt(out2, "%d: ", pid); 8121556Srgrimes#if JOBS 81326104Ssteve if (sig == SIGTSTP && rootshell && iflag) 8141556Srgrimes outfmt(out2, "%%%d ", job - jobtab + 1); 8151556Srgrimes#endif 81626104Ssteve if (sig < NSIG && sys_siglist[sig]) 81726104Ssteve out2str(sys_siglist[sig]); 8181556Srgrimes else 81926104Ssteve outfmt(out2, "Signal %d", sig); 8201556Srgrimes if (core) 8211556Srgrimes out2str(" - core dumped"); 8221556Srgrimes out2c('\n'); 8231556Srgrimes flushout(&errout); 8241556Srgrimes } else { 82526104Ssteve TRACE(("Not printing status: status=%d, sig=%d\n", 82626104Ssteve status, sig)); 8271556Srgrimes } 8281556Srgrimes } else { 8291556Srgrimes TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job)); 8301556Srgrimes if (thisjob) 8311556Srgrimes thisjob->changed = 1; 8321556Srgrimes } 8331556Srgrimes return pid; 8341556Srgrimes} 8351556Srgrimes 8361556Srgrimes 8371556Srgrimes 8381556Srgrimes/* 8391556Srgrimes * Do a wait system call. If job control is compiled in, we accept 8401556Srgrimes * stopped processes. If block is zero, we return a value of zero 8411556Srgrimes * rather than blocking. 8421556Srgrimes * 8431556Srgrimes * System V doesn't have a non-blocking wait system call. It does 8441556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's 8451556Srgrimes * children dies. The obvious way to use SIGCLD would be to install 8461556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD 8471556Srgrimes * was received, and have waitproc bump another counter when it got 8481556Srgrimes * the status of a process. Waitproc would then know that a wait 8491556Srgrimes * system call would not block if the two counters were different. 8501556Srgrimes * This approach doesn't work because if a process has children that 8511556Srgrimes * have not been waited for, System V will send it a SIGCLD when it 8521556Srgrimes * installs a signal handler for SIGCLD. What this means is that when 8531556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously 8541556Srgrimes * until is runs out of stack space, unless it does a wait call before 8551556Srgrimes * restoring the signal handler. The code below takes advantage of 8561556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and 8571556Srgrimes * then checking to see whether it was called. If there are any 8581556Srgrimes * children to be waited for, it will be. 8591556Srgrimes * 8601556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking 8611556Srgrimes * waits at all. In this case, the user will not be informed when 8621556Srgrimes * a background process until the next time she runs a real program 8631556Srgrimes * (as opposed to running a builtin command or just typing return), 8641556Srgrimes * and the jobs command may give out of date information. 8651556Srgrimes */ 8661556Srgrimes 8671556Srgrimes#ifdef SYSV 8681556SrgrimesSTATIC int gotsigchild; 8691556Srgrimes 8701556SrgrimesSTATIC int onsigchild() { 8711556Srgrimes gotsigchild = 1; 8721556Srgrimes} 8731556Srgrimes#endif 8741556Srgrimes 8751556Srgrimes 8761556SrgrimesSTATIC int 8771556Srgrimeswaitproc(block, status) 87817987Speter int block; 8791556Srgrimes int *status; 88017987Speter{ 8811556Srgrimes#ifdef BSD 8821556Srgrimes int flags; 8831556Srgrimes 8841556Srgrimes#if JOBS 8851556Srgrimes flags = WUNTRACED; 8861556Srgrimes#else 8871556Srgrimes flags = 0; 8881556Srgrimes#endif 8891556Srgrimes if (block == 0) 8901556Srgrimes flags |= WNOHANG; 8911556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 8921556Srgrimes#else 8931556Srgrimes#ifdef SYSV 8941556Srgrimes int (*save)(); 8951556Srgrimes 8961556Srgrimes if (block == 0) { 8971556Srgrimes gotsigchild = 0; 8981556Srgrimes save = signal(SIGCLD, onsigchild); 8991556Srgrimes signal(SIGCLD, save); 9001556Srgrimes if (gotsigchild == 0) 9011556Srgrimes return 0; 9021556Srgrimes } 9031556Srgrimes return wait(status); 9041556Srgrimes#else 9051556Srgrimes if (block == 0) 9061556Srgrimes return 0; 9071556Srgrimes return wait(status); 9081556Srgrimes#endif 9091556Srgrimes#endif 9101556Srgrimes} 9111556Srgrimes 9121556Srgrimes/* 9131556Srgrimes * return 1 if there are stopped jobs, otherwise 0 9141556Srgrimes */ 9151556Srgrimesint job_warning = 0; 9161556Srgrimesint 9171556Srgrimesstoppedjobs() 9181556Srgrimes{ 91925222Ssteve int jobno; 92025222Ssteve struct job *jp; 9211556Srgrimes 9221556Srgrimes if (job_warning) 9231556Srgrimes return (0); 9241556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 9251556Srgrimes if (jp->used == 0) 9261556Srgrimes continue; 9271556Srgrimes if (jp->state == JOBSTOPPED) { 9281556Srgrimes out2str("You have stopped jobs.\n"); 9291556Srgrimes job_warning = 2; 9301556Srgrimes return (1); 9311556Srgrimes } 9321556Srgrimes } 9331556Srgrimes 9341556Srgrimes return (0); 9351556Srgrimes} 9361556Srgrimes 9371556Srgrimes/* 9381556Srgrimes * Return a string identifying a command (to be printed by the 9391556Srgrimes * jobs command. 9401556Srgrimes */ 9411556Srgrimes 9421556SrgrimesSTATIC char *cmdnextc; 9431556SrgrimesSTATIC int cmdnleft; 9441556SrgrimesSTATIC void cmdtxt(), cmdputs(); 9451556Srgrimes#define MAXCMDTEXT 200 9461556Srgrimes 9471556Srgrimeschar * 9481556Srgrimescommandtext(n) 9491556Srgrimes union node *n; 9501556Srgrimes { 9511556Srgrimes char *name; 9521556Srgrimes 9531556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 9541556Srgrimes cmdnleft = MAXCMDTEXT - 4; 9551556Srgrimes cmdtxt(n); 9561556Srgrimes *cmdnextc = '\0'; 9571556Srgrimes return name; 9581556Srgrimes} 9591556Srgrimes 9601556Srgrimes 9611556SrgrimesSTATIC void 9621556Srgrimescmdtxt(n) 9631556Srgrimes union node *n; 9641556Srgrimes { 9651556Srgrimes union node *np; 9661556Srgrimes struct nodelist *lp; 9671556Srgrimes char *p; 9681556Srgrimes int i; 9691556Srgrimes char s[2]; 9701556Srgrimes 9711556Srgrimes if (n == NULL) 9721556Srgrimes return; 9731556Srgrimes switch (n->type) { 9741556Srgrimes case NSEMI: 9751556Srgrimes cmdtxt(n->nbinary.ch1); 9761556Srgrimes cmdputs("; "); 9771556Srgrimes cmdtxt(n->nbinary.ch2); 9781556Srgrimes break; 9791556Srgrimes case NAND: 9801556Srgrimes cmdtxt(n->nbinary.ch1); 9811556Srgrimes cmdputs(" && "); 9821556Srgrimes cmdtxt(n->nbinary.ch2); 9831556Srgrimes break; 9841556Srgrimes case NOR: 9851556Srgrimes cmdtxt(n->nbinary.ch1); 9861556Srgrimes cmdputs(" || "); 9871556Srgrimes cmdtxt(n->nbinary.ch2); 9881556Srgrimes break; 9891556Srgrimes case NPIPE: 9901556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 9911556Srgrimes cmdtxt(lp->n); 9921556Srgrimes if (lp->next) 9931556Srgrimes cmdputs(" | "); 9941556Srgrimes } 9951556Srgrimes break; 9961556Srgrimes case NSUBSHELL: 9971556Srgrimes cmdputs("("); 9981556Srgrimes cmdtxt(n->nredir.n); 9991556Srgrimes cmdputs(")"); 10001556Srgrimes break; 10011556Srgrimes case NREDIR: 10021556Srgrimes case NBACKGND: 10031556Srgrimes cmdtxt(n->nredir.n); 10041556Srgrimes break; 10051556Srgrimes case NIF: 10061556Srgrimes cmdputs("if "); 10071556Srgrimes cmdtxt(n->nif.test); 10081556Srgrimes cmdputs("; then "); 10091556Srgrimes cmdtxt(n->nif.ifpart); 10101556Srgrimes cmdputs("..."); 10111556Srgrimes break; 10121556Srgrimes case NWHILE: 10131556Srgrimes cmdputs("while "); 10141556Srgrimes goto until; 10151556Srgrimes case NUNTIL: 10161556Srgrimes cmdputs("until "); 10171556Srgrimesuntil: 10181556Srgrimes cmdtxt(n->nbinary.ch1); 10191556Srgrimes cmdputs("; do "); 10201556Srgrimes cmdtxt(n->nbinary.ch2); 10211556Srgrimes cmdputs("; done"); 10221556Srgrimes break; 10231556Srgrimes case NFOR: 10241556Srgrimes cmdputs("for "); 10251556Srgrimes cmdputs(n->nfor.var); 10261556Srgrimes cmdputs(" in ..."); 10271556Srgrimes break; 10281556Srgrimes case NCASE: 10291556Srgrimes cmdputs("case "); 10301556Srgrimes cmdputs(n->ncase.expr->narg.text); 10311556Srgrimes cmdputs(" in ..."); 10321556Srgrimes break; 10331556Srgrimes case NDEFUN: 10341556Srgrimes cmdputs(n->narg.text); 10351556Srgrimes cmdputs("() ..."); 10361556Srgrimes break; 10371556Srgrimes case NCMD: 10381556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 10391556Srgrimes cmdtxt(np); 10401556Srgrimes if (np->narg.next) 10411556Srgrimes cmdputs(" "); 10421556Srgrimes } 10431556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 10441556Srgrimes cmdputs(" "); 10451556Srgrimes cmdtxt(np); 10461556Srgrimes } 10471556Srgrimes break; 10481556Srgrimes case NARG: 10491556Srgrimes cmdputs(n->narg.text); 10501556Srgrimes break; 10511556Srgrimes case NTO: 10521556Srgrimes p = ">"; i = 1; goto redir; 10531556Srgrimes case NAPPEND: 10541556Srgrimes p = ">>"; i = 1; goto redir; 10551556Srgrimes case NTOFD: 10561556Srgrimes p = ">&"; i = 1; goto redir; 10571556Srgrimes case NFROM: 10581556Srgrimes p = "<"; i = 0; goto redir; 10591556Srgrimes case NFROMFD: 10601556Srgrimes p = "<&"; i = 0; goto redir; 10611556Srgrimesredir: 10621556Srgrimes if (n->nfile.fd != i) { 10631556Srgrimes s[0] = n->nfile.fd + '0'; 10641556Srgrimes s[1] = '\0'; 10651556Srgrimes cmdputs(s); 10661556Srgrimes } 10671556Srgrimes cmdputs(p); 10681556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 10691556Srgrimes s[0] = n->ndup.dupfd + '0'; 10701556Srgrimes s[1] = '\0'; 10711556Srgrimes cmdputs(s); 10721556Srgrimes } else { 10731556Srgrimes cmdtxt(n->nfile.fname); 10741556Srgrimes } 10751556Srgrimes break; 10761556Srgrimes case NHERE: 10771556Srgrimes case NXHERE: 10781556Srgrimes cmdputs("<<..."); 10791556Srgrimes break; 10801556Srgrimes default: 10811556Srgrimes cmdputs("???"); 10821556Srgrimes break; 10831556Srgrimes } 10841556Srgrimes} 10851556Srgrimes 10861556Srgrimes 10871556Srgrimes 10881556SrgrimesSTATIC void 10891556Srgrimescmdputs(s) 10901556Srgrimes char *s; 10911556Srgrimes { 109225222Ssteve char *p, *q; 109325222Ssteve char c; 10941556Srgrimes int subtype = 0; 10951556Srgrimes 10961556Srgrimes if (cmdnleft <= 0) 10971556Srgrimes return; 10981556Srgrimes p = s; 10991556Srgrimes q = cmdnextc; 11001556Srgrimes while ((c = *p++) != '\0') { 11011556Srgrimes if (c == CTLESC) 11021556Srgrimes *q++ = *p++; 11031556Srgrimes else if (c == CTLVAR) { 11041556Srgrimes *q++ = '$'; 11051556Srgrimes if (--cmdnleft > 0) 11061556Srgrimes *q++ = '{'; 11071556Srgrimes subtype = *p++; 11081556Srgrimes } else if (c == '=' && subtype != 0) { 11091556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 11101556Srgrimes subtype = 0; 11111556Srgrimes } else if (c == CTLENDVAR) { 11121556Srgrimes *q++ = '}'; 111318954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 11141556Srgrimes cmdnleft++; /* ignore it */ 11151556Srgrimes else 11161556Srgrimes *q++ = c; 11171556Srgrimes if (--cmdnleft <= 0) { 11181556Srgrimes *q++ = '.'; 11191556Srgrimes *q++ = '.'; 11201556Srgrimes *q++ = '.'; 11211556Srgrimes break; 11221556Srgrimes } 11231556Srgrimes } 11241556Srgrimes cmdnextc = q; 11251556Srgrimes} 1126