jobs.c revision 25222
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 * 3625222Ssteve * $Id: jobs.c,v 1.12 1997/02/22 13:58:29 peter 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) 19317987Speter int argc; 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++) { 2461556Srgrimes if ((ps->status & 0377) == 0177) { 2471556Srgrimes ps->status = -1; 2481556Srgrimes jp->state = 0; 2491556Srgrimes } 2501556Srgrimes } 2511556Srgrimes INTON; 2521556Srgrimes} 2531556Srgrimes#endif 2541556Srgrimes 2551556Srgrimes 2561556Srgrimesint 25717987Speterjobscmd(argc, argv) 25817987Speter int argc; 25920425Ssteve char **argv; 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 */ 3091556Srgrimes } else if ((ps->status & 0xFF) == 0) { 3101556Srgrimes fmtstr(s, 64, "Exit %d", ps->status >> 8); 3111556Srgrimes } else { 3121556Srgrimes i = ps->status; 3131556Srgrimes#if JOBS 3141556Srgrimes if ((i & 0xFF) == 0177) 3151556Srgrimes i >>= 8; 3161556Srgrimes#endif 31717987Speter if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F]) 31817987Speter scopy(sys_siglist[i & 0x7F], s); 3191556Srgrimes else 3201556Srgrimes fmtstr(s, 64, "Signal %d", i & 0x7F); 3211556Srgrimes if (i & 0x80) 3221556Srgrimes strcat(s, " (core dumped)"); 3231556Srgrimes } 3241556Srgrimes out1str(s); 3251556Srgrimes col += strlen(s); 3261556Srgrimes do { 3271556Srgrimes out1c(' '); 3281556Srgrimes col++; 3291556Srgrimes } while (col < 30); 3301556Srgrimes out1str(ps->cmd); 3311556Srgrimes out1c('\n'); 3321556Srgrimes if (--procno <= 0) 3331556Srgrimes break; 3341556Srgrimes } 3351556Srgrimes jp->changed = 0; 3361556Srgrimes if (jp->state == JOBDONE) { 3371556Srgrimes freejob(jp); 3381556Srgrimes } 3391556Srgrimes } 3401556Srgrimes} 3411556Srgrimes 3421556Srgrimes 3431556Srgrimes/* 3441556Srgrimes * Mark a job structure as unused. 3451556Srgrimes */ 3461556Srgrimes 3471556SrgrimesSTATIC void 3481556Srgrimesfreejob(jp) 3491556Srgrimes struct job *jp; 3501556Srgrimes { 3511556Srgrimes struct procstat *ps; 3521556Srgrimes int i; 3531556Srgrimes 3541556Srgrimes INTOFF; 3551556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 3561556Srgrimes if (ps->cmd != nullstr) 3571556Srgrimes ckfree(ps->cmd); 3581556Srgrimes } 3591556Srgrimes if (jp->ps != &jp->ps0) 3601556Srgrimes ckfree(jp->ps); 3611556Srgrimes jp->used = 0; 3621556Srgrimes#if JOBS 3631556Srgrimes if (curjob == jp - jobtab + 1) 3641556Srgrimes curjob = 0; 3651556Srgrimes#endif 3661556Srgrimes INTON; 3671556Srgrimes} 3681556Srgrimes 3691556Srgrimes 3701556Srgrimes 3711556Srgrimesint 37220425Sstevewaitcmd(argc, argv) 37317987Speter int argc; 37420425Ssteve char **argv; 37517987Speter{ 3761556Srgrimes struct job *job; 3771556Srgrimes int status; 3781556Srgrimes struct job *jp; 3791556Srgrimes 3801556Srgrimes if (argc > 1) { 3811556Srgrimes job = getjob(argv[1]); 3821556Srgrimes } else { 3831556Srgrimes job = NULL; 3841556Srgrimes } 3851556Srgrimes for (;;) { /* loop until process terminated or stopped */ 3861556Srgrimes if (job != NULL) { 3871556Srgrimes if (job->state) { 3881556Srgrimes status = job->ps[job->nprocs - 1].status; 3891556Srgrimes if ((status & 0xFF) == 0) 3901556Srgrimes status = status >> 8 & 0xFF; 3911556Srgrimes#if JOBS 3921556Srgrimes else if ((status & 0xFF) == 0177) 3931556Srgrimes status = (status >> 8 & 0x7F) + 128; 3941556Srgrimes#endif 3951556Srgrimes else 3961556Srgrimes status = (status & 0x7F) + 128; 3971556Srgrimes if (! iflag) 3981556Srgrimes freejob(job); 3991556Srgrimes return status; 4001556Srgrimes } 4011556Srgrimes } else { 4021556Srgrimes for (jp = jobtab ; ; jp++) { 4031556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 4041556Srgrimes return 0; 4051556Srgrimes } 4061556Srgrimes if (jp->used && jp->state == 0) 4071556Srgrimes break; 4081556Srgrimes } 4091556Srgrimes } 4101556Srgrimes dowait(1, (struct job *)NULL); 4111556Srgrimes } 4121556Srgrimes} 4131556Srgrimes 4141556Srgrimes 4151556Srgrimes 41617987Speterint 41720425Sstevejobidcmd(argc, argv) 41817987Speter int argc; 41920425Ssteve char **argv; 42017987Speter{ 4211556Srgrimes struct job *jp; 4221556Srgrimes int i; 4231556Srgrimes 4241556Srgrimes jp = getjob(argv[1]); 4251556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 4261556Srgrimes out1fmt("%d", jp->ps[i].pid); 4271556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 4281556Srgrimes } 4291556Srgrimes return 0; 4301556Srgrimes} 4311556Srgrimes 4321556Srgrimes 4331556Srgrimes 4341556Srgrimes/* 4351556Srgrimes * Convert a job name to a job structure. 4361556Srgrimes */ 4371556Srgrimes 4381556SrgrimesSTATIC struct job * 4391556Srgrimesgetjob(name) 4401556Srgrimes char *name; 4411556Srgrimes { 4421556Srgrimes int jobno; 44325222Ssteve struct job *jp; 4441556Srgrimes int pid; 4451556Srgrimes int i; 4461556Srgrimes 4471556Srgrimes if (name == NULL) { 4481556Srgrimes#if JOBS 4491556Srgrimescurrentjob: 4501556Srgrimes if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0) 4511556Srgrimes error("No current job"); 4521556Srgrimes return &jobtab[jobno - 1]; 4531556Srgrimes#else 4541556Srgrimes error("No current job"); 4551556Srgrimes#endif 4561556Srgrimes } else if (name[0] == '%') { 4571556Srgrimes if (is_digit(name[1])) { 4581556Srgrimes jobno = number(name + 1); 4591556Srgrimes if (jobno > 0 && jobno <= njobs 4601556Srgrimes && jobtab[jobno - 1].used != 0) 4611556Srgrimes return &jobtab[jobno - 1]; 4621556Srgrimes#if JOBS 4631556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 4641556Srgrimes goto currentjob; 4651556Srgrimes#endif 4661556Srgrimes } else { 46725222Ssteve struct job *found = NULL; 4681556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 4691556Srgrimes if (jp->used && jp->nprocs > 0 4701556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 4711556Srgrimes if (found) 4721556Srgrimes error("%s: ambiguous", name); 4731556Srgrimes found = jp; 4741556Srgrimes } 4751556Srgrimes } 4761556Srgrimes if (found) 4771556Srgrimes return found; 4781556Srgrimes } 4791556Srgrimes } else if (is_number(name)) { 4801556Srgrimes pid = number(name); 4811556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 4821556Srgrimes if (jp->used && jp->nprocs > 0 4831556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 4841556Srgrimes return jp; 4851556Srgrimes } 4861556Srgrimes } 4871556Srgrimes error("No such job: %s", name); 48817987Speter /*NOTREACHED*/ 48917987Speter return NULL; 4901556Srgrimes} 4911556Srgrimes 4921556Srgrimes 4931556Srgrimes 4941556Srgrimes/* 4951556Srgrimes * Return a new job structure, 4961556Srgrimes */ 4971556Srgrimes 4981556Srgrimesstruct job * 4991556Srgrimesmakejob(node, nprocs) 5001556Srgrimes union node *node; 50117987Speter int nprocs; 50217987Speter{ 5031556Srgrimes int i; 5041556Srgrimes struct job *jp; 5051556Srgrimes 5061556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 5071556Srgrimes if (--i < 0) { 5081556Srgrimes INTOFF; 5091556Srgrimes if (njobs == 0) { 5101556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 5111556Srgrimes } else { 5121556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 51317987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 51420425Ssteve /* Relocate `ps' pointers */ 51520425Ssteve for (i = 0; i < njobs; i++) 51620425Ssteve if (jp[i].ps == &jobtab[i].ps0) 51720425Ssteve jp[i].ps = &jp[i].ps0; 5181556Srgrimes ckfree(jobtab); 5191556Srgrimes jobtab = jp; 5201556Srgrimes } 5211556Srgrimes jp = jobtab + njobs; 5221556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 5231556Srgrimes INTON; 5241556Srgrimes break; 5251556Srgrimes } 5261556Srgrimes if (jp->used == 0) 5271556Srgrimes break; 5281556Srgrimes } 5291556Srgrimes INTOFF; 5301556Srgrimes jp->state = 0; 5311556Srgrimes jp->used = 1; 5321556Srgrimes jp->changed = 0; 5331556Srgrimes jp->nprocs = 0; 5341556Srgrimes#if JOBS 5351556Srgrimes jp->jobctl = jobctl; 5361556Srgrimes#endif 5371556Srgrimes if (nprocs > 1) { 5381556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 5391556Srgrimes } else { 5401556Srgrimes jp->ps = &jp->ps0; 5411556Srgrimes } 5421556Srgrimes INTON; 54317987Speter TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs, 54417987Speter jp - jobtab + 1)); 5451556Srgrimes return jp; 5468855Srgrimes} 5471556Srgrimes 5481556Srgrimes 5491556Srgrimes/* 5501556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 5511556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 5521556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 5531556Srgrimes * be NULL. The mode parameter can be one of the following: 5541556Srgrimes * FORK_FG - Fork off a foreground process. 5551556Srgrimes * FORK_BG - Fork off a background process. 5561556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 5571556Srgrimes * process group even if job control is on. 5581556Srgrimes * 5591556Srgrimes * When job control is turned off, background processes have their standard 5601556Srgrimes * input redirected to /dev/null (except for the second and later processes 5611556Srgrimes * in a pipeline). 5621556Srgrimes */ 5631556Srgrimes 5641556Srgrimesint 5651556Srgrimesforkshell(jp, n, mode) 5661556Srgrimes union node *n; 5671556Srgrimes struct job *jp; 56817987Speter int mode; 56917987Speter{ 5701556Srgrimes int pid; 5711556Srgrimes int pgrp; 5721556Srgrimes 57317987Speter TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n, 57417987Speter mode)); 5751556Srgrimes INTOFF; 5761556Srgrimes pid = fork(); 5771556Srgrimes if (pid == -1) { 5781556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 5791556Srgrimes INTON; 5801556Srgrimes error("Cannot fork"); 5811556Srgrimes } 5821556Srgrimes if (pid == 0) { 5831556Srgrimes struct job *p; 5841556Srgrimes int wasroot; 5851556Srgrimes int i; 5861556Srgrimes 5871556Srgrimes TRACE(("Child shell %d\n", getpid())); 5881556Srgrimes wasroot = rootshell; 5891556Srgrimes rootshell = 0; 5901556Srgrimes for (i = njobs, p = jobtab ; --i >= 0 ; p++) 5911556Srgrimes if (p->used) 5921556Srgrimes freejob(p); 5931556Srgrimes closescript(); 5941556Srgrimes INTON; 5951556Srgrimes clear_traps(); 5961556Srgrimes#if JOBS 5971556Srgrimes jobctl = 0; /* do job control only in root shell */ 5981556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 5991556Srgrimes if (jp == NULL || jp->nprocs == 0) 6001556Srgrimes pgrp = getpid(); 6011556Srgrimes else 6021556Srgrimes pgrp = jp->ps[0].pid; 60321352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 6041556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 60520425Ssteve#ifdef OLD_TTY_DRIVER 6061556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0) 60718016Speter error("TIOCSPGRP failed, errno=%d", errno); 60820425Ssteve#else 60920425Ssteve if (tcsetpgrp(2, pgrp) < 0) 61020425Ssteve error("tcsetpgrp failed, errno=%d", errno); 61120425Ssteve#endif 6121556Srgrimes } 6131556Srgrimes setsignal(SIGTSTP); 6141556Srgrimes setsignal(SIGTTOU); 6151556Srgrimes } else if (mode == FORK_BG) { 6161556Srgrimes ignoresig(SIGINT); 6171556Srgrimes ignoresig(SIGQUIT); 6181556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 6191556Srgrimes ! fd0_redirected_p ()) { 6201556Srgrimes close(0); 6211556Srgrimes if (open("/dev/null", O_RDONLY) != 0) 6221556Srgrimes error("Can't open /dev/null"); 6231556Srgrimes } 6241556Srgrimes } 6251556Srgrimes#else 6261556Srgrimes if (mode == FORK_BG) { 6271556Srgrimes ignoresig(SIGINT); 6281556Srgrimes ignoresig(SIGQUIT); 6291556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 6301556Srgrimes ! fd0_redirected_p ()) { 6311556Srgrimes close(0); 6321556Srgrimes if (open("/dev/null", O_RDONLY) != 0) 6331556Srgrimes error("Can't open /dev/null"); 6341556Srgrimes } 6351556Srgrimes } 6361556Srgrimes#endif 6371556Srgrimes if (wasroot && iflag) { 6381556Srgrimes setsignal(SIGINT); 6391556Srgrimes setsignal(SIGQUIT); 6401556Srgrimes setsignal(SIGTERM); 6411556Srgrimes } 6421556Srgrimes return pid; 6431556Srgrimes } 6441556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 6451556Srgrimes if (jp == NULL || jp->nprocs == 0) 6461556Srgrimes pgrp = pid; 6471556Srgrimes else 6481556Srgrimes pgrp = jp->ps[0].pid; 64917987Speter setpgid(pid, pgrp); 6501556Srgrimes } 6511556Srgrimes if (mode == FORK_BG) 6521556Srgrimes backgndpid = pid; /* set $! */ 6531556Srgrimes if (jp) { 6541556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 6551556Srgrimes ps->pid = pid; 6561556Srgrimes ps->status = -1; 6571556Srgrimes ps->cmd = nullstr; 6581556Srgrimes if (iflag && rootshell && n) 6591556Srgrimes ps->cmd = commandtext(n); 6601556Srgrimes } 6611556Srgrimes INTON; 6621556Srgrimes TRACE(("In parent shell: child = %d\n", pid)); 6631556Srgrimes return pid; 6641556Srgrimes} 6651556Srgrimes 6661556Srgrimes 6671556Srgrimes 6681556Srgrimes/* 6691556Srgrimes * Wait for job to finish. 6701556Srgrimes * 6711556Srgrimes * Under job control we have the problem that while a child process is 6721556Srgrimes * running interrupts generated by the user are sent to the child but not 6731556Srgrimes * to the shell. This means that an infinite loop started by an inter- 6741556Srgrimes * active user may be hard to kill. With job control turned off, an 6751556Srgrimes * interactive user may place an interactive program inside a loop. If 6761556Srgrimes * the interactive program catches interrupts, the user doesn't want 6771556Srgrimes * these interrupts to also abort the loop. The approach we take here 6781556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 6791556Srgrimes * forground process to terminate, and then send itself an interrupt 6801556Srgrimes * signal if the child process was terminated by an interrupt signal. 6811556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 6821556Srgrimes * exit on interrupt; unless these processes terminate themselves by 6831556Srgrimes * sending a signal to themselves (instead of calling exit) they will 6841556Srgrimes * confuse this approach. 6851556Srgrimes */ 6861556Srgrimes 6871556Srgrimesint 6881556Srgrimeswaitforjob(jp) 68925222Ssteve struct job *jp; 6901556Srgrimes { 6911556Srgrimes#if JOBS 69217987Speter int mypgrp = getpgrp(); 6931556Srgrimes#endif 6941556Srgrimes int status; 6951556Srgrimes int st; 6961556Srgrimes 6971556Srgrimes INTOFF; 6981556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 6991556Srgrimes while (jp->state == 0) { 7001556Srgrimes dowait(1, jp); 7011556Srgrimes } 7021556Srgrimes#if JOBS 7031556Srgrimes if (jp->jobctl) { 70420425Ssteve#ifdef OLD_TTY_DRIVER 7051556Srgrimes if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0) 70620425Ssteve error("TIOCSPGRP failed, errno=%d\n", errno); 70720425Ssteve#else 70820425Ssteve if (tcsetpgrp(2, mypgrp) < 0) 70920425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 71020425Ssteve#endif 7111556Srgrimes } 7121556Srgrimes if (jp->state == JOBSTOPPED) 7131556Srgrimes curjob = jp - jobtab + 1; 7141556Srgrimes#endif 7151556Srgrimes status = jp->ps[jp->nprocs - 1].status; 7161556Srgrimes /* convert to 8 bits */ 7171556Srgrimes if ((status & 0xFF) == 0) 7181556Srgrimes st = status >> 8 & 0xFF; 7191556Srgrimes#if JOBS 7201556Srgrimes else if ((status & 0xFF) == 0177) 7211556Srgrimes st = (status >> 8 & 0x7F) + 128; 7221556Srgrimes#endif 7231556Srgrimes else 7241556Srgrimes st = (status & 0x7F) + 128; 7251556Srgrimes if (! JOBS || jp->state == JOBDONE) 7261556Srgrimes freejob(jp); 7271556Srgrimes CLEAR_PENDING_INT; 7281556Srgrimes if ((status & 0x7F) == SIGINT) 7291556Srgrimes kill(getpid(), SIGINT); 7301556Srgrimes INTON; 7311556Srgrimes return st; 7321556Srgrimes} 7331556Srgrimes 7341556Srgrimes 7351556Srgrimes 7361556Srgrimes/* 7371556Srgrimes * Wait for a process to terminate. 7381556Srgrimes */ 7391556Srgrimes 7401556SrgrimesSTATIC int 7411556Srgrimesdowait(block, job) 74217987Speter int block; 7431556Srgrimes struct job *job; 74417987Speter{ 7451556Srgrimes int pid; 7461556Srgrimes int status; 7471556Srgrimes struct procstat *sp; 7481556Srgrimes struct job *jp; 7491556Srgrimes struct job *thisjob; 7501556Srgrimes int done; 7511556Srgrimes int stopped; 7521556Srgrimes int core; 7531556Srgrimes 7541556Srgrimes TRACE(("dowait(%d) called\n", block)); 7551556Srgrimes do { 7561556Srgrimes pid = waitproc(block, &status); 7571556Srgrimes TRACE(("wait returns %d, status=%d\n", pid, status)); 7581556Srgrimes } while (pid == -1 && errno == EINTR); 7591556Srgrimes if (pid <= 0) 7601556Srgrimes return pid; 7611556Srgrimes INTOFF; 7621556Srgrimes thisjob = NULL; 7631556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 7641556Srgrimes if (jp->used) { 7651556Srgrimes done = 1; 7661556Srgrimes stopped = 1; 7671556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 7681556Srgrimes if (sp->pid == -1) 7691556Srgrimes continue; 7701556Srgrimes if (sp->pid == pid) { 7711556Srgrimes TRACE(("Changin status of proc %d from 0x%x to 0x%x\n", pid, sp->status, status)); 7721556Srgrimes sp->status = status; 7731556Srgrimes thisjob = jp; 7741556Srgrimes } 7751556Srgrimes if (sp->status == -1) 7761556Srgrimes stopped = 0; 7771556Srgrimes else if ((sp->status & 0377) == 0177) 7781556Srgrimes done = 0; 7791556Srgrimes } 7801556Srgrimes if (stopped) { /* stopped or done */ 7811556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 7821556Srgrimes if (jp->state != state) { 7831556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 7841556Srgrimes jp->state = state; 7851556Srgrimes#if JOBS 7861556Srgrimes if (done && curjob == jp - jobtab + 1) 7871556Srgrimes curjob = 0; /* no current job */ 7881556Srgrimes#endif 7891556Srgrimes } 7901556Srgrimes } 7911556Srgrimes } 7921556Srgrimes } 7931556Srgrimes INTON; 7941556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 7951556Srgrimes#if JOBS 7961556Srgrimes if ((status & 0xFF) == 0177) 7971556Srgrimes status >>= 8; 7981556Srgrimes#endif 7991556Srgrimes core = status & 0x80; 8001556Srgrimes status &= 0x7F; 8011556Srgrimes if (status != 0 && status != SIGINT && status != SIGPIPE) { 8021556Srgrimes if (thisjob != job) 8031556Srgrimes outfmt(out2, "%d: ", pid); 8041556Srgrimes#if JOBS 8051556Srgrimes if (status == SIGTSTP && rootshell && iflag) 8061556Srgrimes outfmt(out2, "%%%d ", job - jobtab + 1); 8071556Srgrimes#endif 80817987Speter if (status < NSIG && sys_siglist[status]) 80917987Speter out2str(sys_siglist[status]); 8101556Srgrimes else 8111556Srgrimes outfmt(out2, "Signal %d", status); 8121556Srgrimes if (core) 8131556Srgrimes out2str(" - core dumped"); 8141556Srgrimes out2c('\n'); 8151556Srgrimes flushout(&errout); 8161556Srgrimes } else { 8171556Srgrimes TRACE(("Not printing status: status=%d\n", status)); 8181556Srgrimes } 8191556Srgrimes } else { 8201556Srgrimes TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job)); 8211556Srgrimes if (thisjob) 8221556Srgrimes thisjob->changed = 1; 8231556Srgrimes } 8241556Srgrimes return pid; 8251556Srgrimes} 8261556Srgrimes 8271556Srgrimes 8281556Srgrimes 8291556Srgrimes/* 8301556Srgrimes * Do a wait system call. If job control is compiled in, we accept 8311556Srgrimes * stopped processes. If block is zero, we return a value of zero 8321556Srgrimes * rather than blocking. 8331556Srgrimes * 8341556Srgrimes * System V doesn't have a non-blocking wait system call. It does 8351556Srgrimes * have a SIGCLD signal that is sent to a process when one of it's 8361556Srgrimes * children dies. The obvious way to use SIGCLD would be to install 8371556Srgrimes * a handler for SIGCLD which simply bumped a counter when a SIGCLD 8381556Srgrimes * was received, and have waitproc bump another counter when it got 8391556Srgrimes * the status of a process. Waitproc would then know that a wait 8401556Srgrimes * system call would not block if the two counters were different. 8411556Srgrimes * This approach doesn't work because if a process has children that 8421556Srgrimes * have not been waited for, System V will send it a SIGCLD when it 8431556Srgrimes * installs a signal handler for SIGCLD. What this means is that when 8441556Srgrimes * a child exits, the shell will be sent SIGCLD signals continuously 8451556Srgrimes * until is runs out of stack space, unless it does a wait call before 8461556Srgrimes * restoring the signal handler. The code below takes advantage of 8471556Srgrimes * this (mis)feature by installing a signal handler for SIGCLD and 8481556Srgrimes * then checking to see whether it was called. If there are any 8491556Srgrimes * children to be waited for, it will be. 8501556Srgrimes * 8511556Srgrimes * If neither SYSV nor BSD is defined, we don't implement nonblocking 8521556Srgrimes * waits at all. In this case, the user will not be informed when 8531556Srgrimes * a background process until the next time she runs a real program 8541556Srgrimes * (as opposed to running a builtin command or just typing return), 8551556Srgrimes * and the jobs command may give out of date information. 8561556Srgrimes */ 8571556Srgrimes 8581556Srgrimes#ifdef SYSV 8591556SrgrimesSTATIC int gotsigchild; 8601556Srgrimes 8611556SrgrimesSTATIC int onsigchild() { 8621556Srgrimes gotsigchild = 1; 8631556Srgrimes} 8641556Srgrimes#endif 8651556Srgrimes 8661556Srgrimes 8671556SrgrimesSTATIC int 8681556Srgrimeswaitproc(block, status) 86917987Speter int block; 8701556Srgrimes int *status; 87117987Speter{ 8721556Srgrimes#ifdef BSD 8731556Srgrimes int flags; 8741556Srgrimes 8751556Srgrimes#if JOBS 8761556Srgrimes flags = WUNTRACED; 8771556Srgrimes#else 8781556Srgrimes flags = 0; 8791556Srgrimes#endif 8801556Srgrimes if (block == 0) 8811556Srgrimes flags |= WNOHANG; 8821556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 8831556Srgrimes#else 8841556Srgrimes#ifdef SYSV 8851556Srgrimes int (*save)(); 8861556Srgrimes 8871556Srgrimes if (block == 0) { 8881556Srgrimes gotsigchild = 0; 8891556Srgrimes save = signal(SIGCLD, onsigchild); 8901556Srgrimes signal(SIGCLD, save); 8911556Srgrimes if (gotsigchild == 0) 8921556Srgrimes return 0; 8931556Srgrimes } 8941556Srgrimes return wait(status); 8951556Srgrimes#else 8961556Srgrimes if (block == 0) 8971556Srgrimes return 0; 8981556Srgrimes return wait(status); 8991556Srgrimes#endif 9001556Srgrimes#endif 9011556Srgrimes} 9021556Srgrimes 9031556Srgrimes/* 9041556Srgrimes * return 1 if there are stopped jobs, otherwise 0 9051556Srgrimes */ 9061556Srgrimesint job_warning = 0; 9071556Srgrimesint 9081556Srgrimesstoppedjobs() 9091556Srgrimes{ 91025222Ssteve int jobno; 91125222Ssteve struct job *jp; 9121556Srgrimes 9131556Srgrimes if (job_warning) 9141556Srgrimes return (0); 9151556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 9161556Srgrimes if (jp->used == 0) 9171556Srgrimes continue; 9181556Srgrimes if (jp->state == JOBSTOPPED) { 9191556Srgrimes out2str("You have stopped jobs.\n"); 9201556Srgrimes job_warning = 2; 9211556Srgrimes return (1); 9221556Srgrimes } 9231556Srgrimes } 9241556Srgrimes 9251556Srgrimes return (0); 9261556Srgrimes} 9271556Srgrimes 9281556Srgrimes/* 9291556Srgrimes * Return a string identifying a command (to be printed by the 9301556Srgrimes * jobs command. 9311556Srgrimes */ 9321556Srgrimes 9331556SrgrimesSTATIC char *cmdnextc; 9341556SrgrimesSTATIC int cmdnleft; 9351556SrgrimesSTATIC void cmdtxt(), cmdputs(); 9361556Srgrimes#define MAXCMDTEXT 200 9371556Srgrimes 9381556Srgrimeschar * 9391556Srgrimescommandtext(n) 9401556Srgrimes union node *n; 9411556Srgrimes { 9421556Srgrimes char *name; 9431556Srgrimes 9441556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 9451556Srgrimes cmdnleft = MAXCMDTEXT - 4; 9461556Srgrimes cmdtxt(n); 9471556Srgrimes *cmdnextc = '\0'; 9481556Srgrimes return name; 9491556Srgrimes} 9501556Srgrimes 9511556Srgrimes 9521556SrgrimesSTATIC void 9531556Srgrimescmdtxt(n) 9541556Srgrimes union node *n; 9551556Srgrimes { 9561556Srgrimes union node *np; 9571556Srgrimes struct nodelist *lp; 9581556Srgrimes char *p; 9591556Srgrimes int i; 9601556Srgrimes char s[2]; 9611556Srgrimes 9621556Srgrimes if (n == NULL) 9631556Srgrimes return; 9641556Srgrimes switch (n->type) { 9651556Srgrimes case NSEMI: 9661556Srgrimes cmdtxt(n->nbinary.ch1); 9671556Srgrimes cmdputs("; "); 9681556Srgrimes cmdtxt(n->nbinary.ch2); 9691556Srgrimes break; 9701556Srgrimes case NAND: 9711556Srgrimes cmdtxt(n->nbinary.ch1); 9721556Srgrimes cmdputs(" && "); 9731556Srgrimes cmdtxt(n->nbinary.ch2); 9741556Srgrimes break; 9751556Srgrimes case NOR: 9761556Srgrimes cmdtxt(n->nbinary.ch1); 9771556Srgrimes cmdputs(" || "); 9781556Srgrimes cmdtxt(n->nbinary.ch2); 9791556Srgrimes break; 9801556Srgrimes case NPIPE: 9811556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 9821556Srgrimes cmdtxt(lp->n); 9831556Srgrimes if (lp->next) 9841556Srgrimes cmdputs(" | "); 9851556Srgrimes } 9861556Srgrimes break; 9871556Srgrimes case NSUBSHELL: 9881556Srgrimes cmdputs("("); 9891556Srgrimes cmdtxt(n->nredir.n); 9901556Srgrimes cmdputs(")"); 9911556Srgrimes break; 9921556Srgrimes case NREDIR: 9931556Srgrimes case NBACKGND: 9941556Srgrimes cmdtxt(n->nredir.n); 9951556Srgrimes break; 9961556Srgrimes case NIF: 9971556Srgrimes cmdputs("if "); 9981556Srgrimes cmdtxt(n->nif.test); 9991556Srgrimes cmdputs("; then "); 10001556Srgrimes cmdtxt(n->nif.ifpart); 10011556Srgrimes cmdputs("..."); 10021556Srgrimes break; 10031556Srgrimes case NWHILE: 10041556Srgrimes cmdputs("while "); 10051556Srgrimes goto until; 10061556Srgrimes case NUNTIL: 10071556Srgrimes cmdputs("until "); 10081556Srgrimesuntil: 10091556Srgrimes cmdtxt(n->nbinary.ch1); 10101556Srgrimes cmdputs("; do "); 10111556Srgrimes cmdtxt(n->nbinary.ch2); 10121556Srgrimes cmdputs("; done"); 10131556Srgrimes break; 10141556Srgrimes case NFOR: 10151556Srgrimes cmdputs("for "); 10161556Srgrimes cmdputs(n->nfor.var); 10171556Srgrimes cmdputs(" in ..."); 10181556Srgrimes break; 10191556Srgrimes case NCASE: 10201556Srgrimes cmdputs("case "); 10211556Srgrimes cmdputs(n->ncase.expr->narg.text); 10221556Srgrimes cmdputs(" in ..."); 10231556Srgrimes break; 10241556Srgrimes case NDEFUN: 10251556Srgrimes cmdputs(n->narg.text); 10261556Srgrimes cmdputs("() ..."); 10271556Srgrimes break; 10281556Srgrimes case NCMD: 10291556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 10301556Srgrimes cmdtxt(np); 10311556Srgrimes if (np->narg.next) 10321556Srgrimes cmdputs(" "); 10331556Srgrimes } 10341556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 10351556Srgrimes cmdputs(" "); 10361556Srgrimes cmdtxt(np); 10371556Srgrimes } 10381556Srgrimes break; 10391556Srgrimes case NARG: 10401556Srgrimes cmdputs(n->narg.text); 10411556Srgrimes break; 10421556Srgrimes case NTO: 10431556Srgrimes p = ">"; i = 1; goto redir; 10441556Srgrimes case NAPPEND: 10451556Srgrimes p = ">>"; i = 1; goto redir; 10461556Srgrimes case NTOFD: 10471556Srgrimes p = ">&"; i = 1; goto redir; 10481556Srgrimes case NFROM: 10491556Srgrimes p = "<"; i = 0; goto redir; 10501556Srgrimes case NFROMFD: 10511556Srgrimes p = "<&"; i = 0; goto redir; 10521556Srgrimesredir: 10531556Srgrimes if (n->nfile.fd != i) { 10541556Srgrimes s[0] = n->nfile.fd + '0'; 10551556Srgrimes s[1] = '\0'; 10561556Srgrimes cmdputs(s); 10571556Srgrimes } 10581556Srgrimes cmdputs(p); 10591556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 10601556Srgrimes s[0] = n->ndup.dupfd + '0'; 10611556Srgrimes s[1] = '\0'; 10621556Srgrimes cmdputs(s); 10631556Srgrimes } else { 10641556Srgrimes cmdtxt(n->nfile.fname); 10651556Srgrimes } 10661556Srgrimes break; 10671556Srgrimes case NHERE: 10681556Srgrimes case NXHERE: 10691556Srgrimes cmdputs("<<..."); 10701556Srgrimes break; 10711556Srgrimes default: 10721556Srgrimes cmdputs("???"); 10731556Srgrimes break; 10741556Srgrimes } 10751556Srgrimes} 10761556Srgrimes 10771556Srgrimes 10781556Srgrimes 10791556SrgrimesSTATIC void 10801556Srgrimescmdputs(s) 10811556Srgrimes char *s; 10821556Srgrimes { 108325222Ssteve char *p, *q; 108425222Ssteve char c; 10851556Srgrimes int subtype = 0; 10861556Srgrimes 10871556Srgrimes if (cmdnleft <= 0) 10881556Srgrimes return; 10891556Srgrimes p = s; 10901556Srgrimes q = cmdnextc; 10911556Srgrimes while ((c = *p++) != '\0') { 10921556Srgrimes if (c == CTLESC) 10931556Srgrimes *q++ = *p++; 10941556Srgrimes else if (c == CTLVAR) { 10951556Srgrimes *q++ = '$'; 10961556Srgrimes if (--cmdnleft > 0) 10971556Srgrimes *q++ = '{'; 10981556Srgrimes subtype = *p++; 10991556Srgrimes } else if (c == '=' && subtype != 0) { 11001556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 11011556Srgrimes subtype = 0; 11021556Srgrimes } else if (c == CTLENDVAR) { 11031556Srgrimes *q++ = '}'; 110418954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 11051556Srgrimes cmdnleft++; /* ignore it */ 11061556Srgrimes else 11071556Srgrimes *q++ = c; 11081556Srgrimes if (--cmdnleft <= 0) { 11091556Srgrimes *q++ = '.'; 11101556Srgrimes *q++ = '.'; 11111556Srgrimes *q++ = '.'; 11121556Srgrimes break; 11131556Srgrimes } 11141556Srgrimes } 11151556Srgrimes cmdnextc = q; 11161556Srgrimes} 1117