jobs.c revision 200998
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 * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/jobs.c 200998 2009-12-25 20:21:35Z jilles $"); 401556Srgrimes 4117987Speter#include <fcntl.h> 4217987Speter#include <signal.h> 4317987Speter#include <errno.h> 4499762Stjr#include <paths.h> 4517987Speter#include <unistd.h> 4617987Speter#include <stdlib.h> 4717987Speter#include <sys/param.h> 4817987Speter#include <sys/wait.h> 4917987Speter#include <sys/time.h> 5017987Speter#include <sys/resource.h> 5169793Sobrien#include <paths.h> 5218018Speter#include <sys/ioctl.h> 5317987Speter 541556Srgrimes#include "shell.h" 551556Srgrimes#if JOBS 5617987Speter#include <termios.h> 571556Srgrimes#undef CEOF /* syntax.h redefines this */ 581556Srgrimes#endif 5917987Speter#include "redir.h" 6017987Speter#include "show.h" 611556Srgrimes#include "main.h" 621556Srgrimes#include "parser.h" 631556Srgrimes#include "nodes.h" 641556Srgrimes#include "jobs.h" 651556Srgrimes#include "options.h" 661556Srgrimes#include "trap.h" 671556Srgrimes#include "syntax.h" 681556Srgrimes#include "input.h" 691556Srgrimes#include "output.h" 701556Srgrimes#include "memalloc.h" 711556Srgrimes#include "error.h" 721556Srgrimes#include "mystring.h" 731556Srgrimes 741556Srgrimes 75117261SddsSTATIC struct job *jobtab; /* array of jobs */ 76117261SddsSTATIC int njobs; /* size of array */ 7728346SsteveMKINIT pid_t backgndpid = -1; /* pid of last background process */ 781556Srgrimes#if JOBS 79117261SddsSTATIC struct job *jobmru; /* most recently used job list */ 80117261SddsSTATIC pid_t initialpgrp; /* pgrp of shell on invocation */ 811556Srgrimes#endif 8238536Scracauerint in_waitcmd = 0; /* are we in waitcmd()? */ 8338950Scracauerint in_dowait = 0; /* are we in dowait()? */ 8438536Scracauervolatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */ 8599762Stjrstatic int ttyfd = -1; 861556Srgrimes 8720425Ssteve#if JOBS 8890111SimpSTATIC void restartjob(struct job *); 8920425Ssteve#endif 9090111SimpSTATIC void freejob(struct job *); 9190111SimpSTATIC struct job *getjob(char *); 92100308StjrSTATIC pid_t dowait(int, struct job *); 93100308StjrSTATIC pid_t waitproc(int, int *); 9490111SimpSTATIC void cmdtxt(union node *); 9590111SimpSTATIC void cmdputs(char *); 9697659Stjr#if JOBS 9797659StjrSTATIC void setcurjob(struct job *); 9897659StjrSTATIC void deljob(struct job *); 9997659StjrSTATIC struct job *getcurjob(struct job *); 10097659Stjr#endif 101163085SstefanfSTATIC void showjob(struct job *, pid_t, int); 1021556Srgrimes 1031556Srgrimes 1041556Srgrimes/* 1051556Srgrimes * Turn job control on and off. 1061556Srgrimes */ 1071556Srgrimes 1081556SrgrimesMKINIT int jobctl; 1091556Srgrimes 11020425Ssteve#if JOBS 1111556Srgrimesvoid 11290111Simpsetjobctl(int on) 11317987Speter{ 11499762Stjr int i; 1151556Srgrimes 1161556Srgrimes if (on == jobctl || rootshell == 0) 1171556Srgrimes return; 1181556Srgrimes if (on) { 11999762Stjr if (ttyfd != -1) 12099762Stjr close(ttyfd); 12199762Stjr if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) { 12299762Stjr i = 0; 12399762Stjr while (i <= 2 && !isatty(i)) 12499762Stjr i++; 125109927Stjr if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0) 12699762Stjr goto out; 12799762Stjr } 128109927Stjr if (ttyfd < 10) { 129109927Stjr /* 130109927Stjr * Keep our TTY file descriptor out of the way of 131109927Stjr * the user's redirections. 132109927Stjr */ 133109927Stjr if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) { 134109927Stjr close(ttyfd); 135109927Stjr ttyfd = -1; 136109927Stjr goto out; 137109927Stjr } 138109927Stjr close(ttyfd); 139109927Stjr ttyfd = i; 140109927Stjr } 141103223Snectar if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) { 14299762Stjr close(ttyfd); 14399762Stjr ttyfd = -1; 14499762Stjr goto out; 14599762Stjr } 1461556Srgrimes do { /* while we are in the background */ 14799762Stjr initialpgrp = tcgetpgrp(ttyfd); 14820425Ssteve if (initialpgrp < 0) { 149199629Sjillesout: out2fmt_flush("sh: can't access tty; job control turned off\n"); 1501556Srgrimes mflag = 0; 1511556Srgrimes return; 1521556Srgrimes } 1531556Srgrimes if (initialpgrp == -1) 15417987Speter initialpgrp = getpgrp(); 15517987Speter else if (initialpgrp != getpgrp()) { 15699762Stjr killpg(0, SIGTTIN); 1571556Srgrimes continue; 1581556Srgrimes } 1591556Srgrimes } while (0); 1601556Srgrimes setsignal(SIGTSTP); 1611556Srgrimes setsignal(SIGTTOU); 1621556Srgrimes setsignal(SIGTTIN); 16317987Speter setpgid(0, rootpid); 16499762Stjr tcsetpgrp(ttyfd, rootpid); 1651556Srgrimes } else { /* turning job control off */ 16617987Speter setpgid(0, initialpgrp); 16799762Stjr tcsetpgrp(ttyfd, initialpgrp); 16899762Stjr close(ttyfd); 16999762Stjr ttyfd = -1; 1701556Srgrimes setsignal(SIGTSTP); 1711556Srgrimes setsignal(SIGTTOU); 1721556Srgrimes setsignal(SIGTTIN); 1731556Srgrimes } 1741556Srgrimes jobctl = on; 1751556Srgrimes} 17620425Ssteve#endif 1771556Srgrimes 1781556Srgrimes 1791556Srgrimes#ifdef mkinit 18028346SsteveINCLUDE <sys/types.h> 18117987SpeterINCLUDE <stdlib.h> 1821556Srgrimes 1831556SrgrimesSHELLPROC { 1841556Srgrimes backgndpid = -1; 1851556Srgrimes#if JOBS 1861556Srgrimes jobctl = 0; 1871556Srgrimes#endif 1881556Srgrimes} 1891556Srgrimes 1901556Srgrimes#endif 1911556Srgrimes 1921556Srgrimes 1931556Srgrimes 1941556Srgrimes#if JOBS 19517987Speterint 19690111Simpfgcmd(int argc __unused, char **argv) 19717987Speter{ 1981556Srgrimes struct job *jp; 199100308Stjr pid_t pgrp; 2001556Srgrimes int status; 2011556Srgrimes 2021556Srgrimes jp = getjob(argv[1]); 2031556Srgrimes if (jp->jobctl == 0) 2041556Srgrimes error("job not created under job control"); 20596933Stjr out1str(jp->ps[0].cmd); 20696933Stjr out1c('\n'); 20796933Stjr flushout(&output); 2081556Srgrimes pgrp = jp->ps[0].pid; 20999762Stjr tcsetpgrp(ttyfd, pgrp); 2101556Srgrimes restartjob(jp); 211100305Stjr jp->foreground = 1; 2121556Srgrimes INTOFF; 21345916Scracauer status = waitforjob(jp, (int *)NULL); 2141556Srgrimes INTON; 2151556Srgrimes return status; 2161556Srgrimes} 2171556Srgrimes 2181556Srgrimes 21917987Speterint 22090111Simpbgcmd(int argc, char **argv) 22117987Speter{ 22296933Stjr char s[64]; 2231556Srgrimes struct job *jp; 2241556Srgrimes 2251556Srgrimes do { 2261556Srgrimes jp = getjob(*++argv); 2271556Srgrimes if (jp->jobctl == 0) 2281556Srgrimes error("job not created under job control"); 22996933Stjr if (jp->state == JOBDONE) 23096933Stjr continue; 2311556Srgrimes restartjob(jp); 232100305Stjr jp->foreground = 0; 233104275Smux fmtstr(s, 64, "[%td] ", jp - jobtab + 1); 23496933Stjr out1str(s); 23596933Stjr out1str(jp->ps[0].cmd); 23696933Stjr out1c('\n'); 2371556Srgrimes } while (--argc > 1); 2381556Srgrimes return 0; 2391556Srgrimes} 2401556Srgrimes 2411556Srgrimes 2421556SrgrimesSTATIC void 24390111Simprestartjob(struct job *jp) 24417987Speter{ 2451556Srgrimes struct procstat *ps; 2461556Srgrimes int i; 2471556Srgrimes 2481556Srgrimes if (jp->state == JOBDONE) 2491556Srgrimes return; 25097660Stjr setcurjob(jp); 2511556Srgrimes INTOFF; 2521556Srgrimes killpg(jp->ps[0].pid, SIGCONT); 2531556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 25426104Ssteve if (WIFSTOPPED(ps->status)) { 2551556Srgrimes ps->status = -1; 2561556Srgrimes jp->state = 0; 2571556Srgrimes } 2581556Srgrimes } 2591556Srgrimes INTON; 2601556Srgrimes} 2611556Srgrimes#endif 2621556Srgrimes 2631556Srgrimes 2641556Srgrimesint 26597669Stjrjobscmd(int argc, char *argv[]) 26617987Speter{ 26797669Stjr char *id; 268163085Sstefanf int ch, mode; 26997669Stjr 27097669Stjr optind = optreset = 1; 271100663Stjr opterr = 0; 272163085Sstefanf mode = SHOWJOBS_DEFAULT; 273163085Sstefanf while ((ch = getopt(argc, argv, "lps")) != -1) { 27497669Stjr switch (ch) { 27597669Stjr case 'l': 276163085Sstefanf mode = SHOWJOBS_VERBOSE; 27797669Stjr break; 278163085Sstefanf case 'p': 279163085Sstefanf mode = SHOWJOBS_PGIDS; 280163085Sstefanf break; 28197669Stjr case 's': 282163085Sstefanf mode = SHOWJOBS_PIDS; 28397669Stjr break; 28497669Stjr case '?': 28597669Stjr default: 28697669Stjr error("unknown option: -%c", optopt); 28797669Stjr } 28897669Stjr } 28997669Stjr argc -= optind; 29097669Stjr argv += optind; 29197669Stjr 29297669Stjr if (argc == 0) 293163085Sstefanf showjobs(0, mode); 29497669Stjr else 29597669Stjr while ((id = *argv++) != NULL) 296163085Sstefanf showjob(getjob(id), 0, mode); 29797669Stjr 29897669Stjr return (0); 2991556Srgrimes} 3001556Srgrimes 30197663StjrSTATIC void 302163085Sstefanfshowjob(struct job *jp, pid_t pid, int mode) 30397663Stjr{ 30497663Stjr char s[64]; 30597663Stjr struct procstat *ps; 30697669Stjr struct job *j; 30797669Stjr int col, curr, i, jobno, prev, procno; 308163085Sstefanf pid_t ppid; 30997669Stjr char c; 3101556Srgrimes 311163085Sstefanf procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs; 31297663Stjr jobno = jp - jobtab + 1; 31397669Stjr curr = prev = 0; 31497669Stjr#if JOBS 31597669Stjr if ((j = getcurjob(NULL)) != NULL) { 31697669Stjr curr = j - jobtab + 1; 31797669Stjr if ((j = getcurjob(j)) != NULL) 31897669Stjr prev = j - jobtab + 1; 31997669Stjr } 32097669Stjr#endif 32197663Stjr for (ps = jp->ps ; ; ps++) { /* for each process */ 322163085Sstefanf if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) { 323163085Sstefanf ppid = (mode == SHOWJOBS_PIDS) ? ps->pid : 324163085Sstefanf getpgid(ps->pid); 325163085Sstefanf out1fmt("%d\n", (int)ppid); 32697669Stjr goto skip; 32797669Stjr } 328163085Sstefanf if (mode != SHOWJOBS_VERBOSE && ps != jp->ps && pid == 0) 32997669Stjr goto skip; 33097822Stjr if (pid != 0 && pid != ps->pid) 33197822Stjr goto skip; 33297819Stjr if (jobno == curr && ps == jp->ps) 33397669Stjr c = '+'; 33497819Stjr else if (jobno == prev && ps == jp->ps) 33597669Stjr c = '-'; 33697669Stjr else 33797669Stjr c = ' '; 33897663Stjr if (ps == jp->ps) 33997669Stjr fmtstr(s, 64, "[%d] %c ", jobno, c); 34097663Stjr else 34197816Stjr fmtstr(s, 64, " %c ", c); 34297663Stjr out1str(s); 34397663Stjr col = strlen(s); 344163085Sstefanf if (mode == SHOWJOBS_VERBOSE) { 345100308Stjr fmtstr(s, 64, "%d ", (int)ps->pid); 34697669Stjr out1str(s); 34797669Stjr col += strlen(s); 34897669Stjr } 34997663Stjr s[0] = '\0'; 35097819Stjr if (ps != jp->ps) { 35197819Stjr *s = '\0'; 35297819Stjr } else if (ps->status == -1) { 35397669Stjr strcpy(s, "Running"); 35497663Stjr } else if (WIFEXITED(ps->status)) { 35597820Stjr if (WEXITSTATUS(ps->status) == 0) 35697820Stjr strcpy(s, "Done"); 35797820Stjr else 35897820Stjr fmtstr(s, 64, "Done (%d)", 35997820Stjr WEXITSTATUS(ps->status)); 36097663Stjr } else { 36197663Stjr#if JOBS 362155301Sschweikh if (WIFSTOPPED(ps->status)) 36397663Stjr i = WSTOPSIG(ps->status); 36497663Stjr else 36597663Stjr#endif 36697663Stjr i = WTERMSIG(ps->status); 367125155Snjl if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F]) 36897663Stjr scopy(sys_siglist[i & 0x7F], s); 36997663Stjr else 37097663Stjr fmtstr(s, 64, "Signal %d", i & 0x7F); 37197663Stjr if (WCOREDUMP(ps->status)) 37297663Stjr strcat(s, " (core dumped)"); 37397663Stjr } 37497663Stjr out1str(s); 37597663Stjr col += strlen(s); 37697663Stjr do { 37797663Stjr out1c(' '); 37897663Stjr col++; 37997663Stjr } while (col < 30); 38097663Stjr out1str(ps->cmd); 38197663Stjr out1c('\n'); 38297669Stjrskip: if (--procno <= 0) 38397663Stjr break; 38497663Stjr } 38597663Stjr} 38697663Stjr 3871556Srgrimes/* 3881556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 3891556Srgrimes * statuses have changed since the last call to showjobs. 3901556Srgrimes * 3911556Srgrimes * If the shell is interrupted in the process of creating a job, the 3921556Srgrimes * result may be a job structure containing zero processes. Such structures 3931556Srgrimes * will be freed here. 3941556Srgrimes */ 3951556Srgrimes 3961556Srgrimesvoid 397163085Sstefanfshowjobs(int change, int mode) 39817987Speter{ 3991556Srgrimes int jobno; 4001556Srgrimes struct job *jp; 4011556Srgrimes 4021556Srgrimes TRACE(("showjobs(%d) called\n", change)); 4031556Srgrimes while (dowait(0, (struct job *)NULL) > 0); 4041556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 4051556Srgrimes if (! jp->used) 4061556Srgrimes continue; 4071556Srgrimes if (jp->nprocs == 0) { 4081556Srgrimes freejob(jp); 4091556Srgrimes continue; 4101556Srgrimes } 4111556Srgrimes if (change && ! jp->changed) 4121556Srgrimes continue; 413163085Sstefanf showjob(jp, 0, mode); 4141556Srgrimes jp->changed = 0; 4151556Srgrimes if (jp->state == JOBDONE) { 4161556Srgrimes freejob(jp); 4171556Srgrimes } 4181556Srgrimes } 4191556Srgrimes} 4201556Srgrimes 4211556Srgrimes 4221556Srgrimes/* 4231556Srgrimes * Mark a job structure as unused. 4241556Srgrimes */ 4251556Srgrimes 4261556SrgrimesSTATIC void 42790111Simpfreejob(struct job *jp) 42890111Simp{ 4291556Srgrimes struct procstat *ps; 4301556Srgrimes int i; 4311556Srgrimes 4321556Srgrimes INTOFF; 4331556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 4341556Srgrimes if (ps->cmd != nullstr) 4351556Srgrimes ckfree(ps->cmd); 4361556Srgrimes } 4371556Srgrimes if (jp->ps != &jp->ps0) 4381556Srgrimes ckfree(jp->ps); 4391556Srgrimes jp->used = 0; 4401556Srgrimes#if JOBS 44197659Stjr deljob(jp); 4421556Srgrimes#endif 4431556Srgrimes INTON; 4441556Srgrimes} 4451556Srgrimes 4461556Srgrimes 4471556Srgrimes 4481556Srgrimesint 44990111Simpwaitcmd(int argc, char **argv) 45017987Speter{ 4511556Srgrimes struct job *job; 45226104Ssteve int status, retval; 4531556Srgrimes struct job *jp; 4541556Srgrimes 4551556Srgrimes if (argc > 1) { 4561556Srgrimes job = getjob(argv[1]); 4571556Srgrimes } else { 4581556Srgrimes job = NULL; 4591556Srgrimes } 46038536Scracauer 46138536Scracauer /* 46238536Scracauer * Loop until a process is terminated or stopped, or a SIGINT is 46338536Scracauer * received. 46438536Scracauer */ 46538536Scracauer 46638521Scracauer in_waitcmd++; 46738536Scracauer do { 4681556Srgrimes if (job != NULL) { 4691556Srgrimes if (job->state) { 4701556Srgrimes status = job->ps[job->nprocs - 1].status; 47126104Ssteve if (WIFEXITED(status)) 47226104Ssteve retval = WEXITSTATUS(status); 4731556Srgrimes#if JOBS 47426104Ssteve else if (WIFSTOPPED(status)) 47526104Ssteve retval = WSTOPSIG(status) + 128; 4761556Srgrimes#endif 4771556Srgrimes else 47826104Ssteve retval = WTERMSIG(status) + 128; 4791556Srgrimes if (! iflag) 4801556Srgrimes freejob(job); 48138536Scracauer in_waitcmd--; 48226104Ssteve return retval; 4831556Srgrimes } 4841556Srgrimes } else { 4851556Srgrimes for (jp = jobtab ; ; jp++) { 4861556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 48738536Scracauer in_waitcmd--; 4881556Srgrimes return 0; 4891556Srgrimes } 4901556Srgrimes if (jp->used && jp->state == 0) 4911556Srgrimes break; 4921556Srgrimes } 4931556Srgrimes } 49438521Scracauer } while (dowait(1, (struct job *)NULL) != -1); 49538521Scracauer in_waitcmd--; 49638521Scracauer 49738521Scracauer return 0; 4981556Srgrimes} 4991556Srgrimes 5001556Srgrimes 5011556Srgrimes 50217987Speterint 50390111Simpjobidcmd(int argc __unused, char **argv) 50417987Speter{ 5051556Srgrimes struct job *jp; 5061556Srgrimes int i; 5071556Srgrimes 5081556Srgrimes jp = getjob(argv[1]); 5091556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 510100308Stjr out1fmt("%d", (int)jp->ps[i].pid); 5111556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 5121556Srgrimes } 5131556Srgrimes return 0; 5141556Srgrimes} 5151556Srgrimes 5161556Srgrimes 5171556Srgrimes 5181556Srgrimes/* 5191556Srgrimes * Convert a job name to a job structure. 5201556Srgrimes */ 5211556Srgrimes 5221556SrgrimesSTATIC struct job * 52390111Simpgetjob(char *name) 52490111Simp{ 5251556Srgrimes int jobno; 52697688Stjr struct job *found, *jp; 527100308Stjr pid_t pid; 5281556Srgrimes int i; 5291556Srgrimes 5301556Srgrimes if (name == NULL) { 5311556Srgrimes#if JOBS 53297659Stjrcurrentjob: if ((jp = getcurjob(NULL)) == NULL) 5331556Srgrimes error("No current job"); 53497659Stjr return (jp); 5351556Srgrimes#else 5361556Srgrimes error("No current job"); 5371556Srgrimes#endif 5381556Srgrimes } else if (name[0] == '%') { 5391556Srgrimes if (is_digit(name[1])) { 5401556Srgrimes jobno = number(name + 1); 5411556Srgrimes if (jobno > 0 && jobno <= njobs 5421556Srgrimes && jobtab[jobno - 1].used != 0) 5431556Srgrimes return &jobtab[jobno - 1]; 5441556Srgrimes#if JOBS 5451556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 5461556Srgrimes goto currentjob; 54797688Stjr } else if (name[1] == '+' && name[2] == '\0') { 54897688Stjr goto currentjob; 54997688Stjr } else if (name[1] == '-' && name[2] == '\0') { 55097688Stjr if ((jp = getcurjob(NULL)) == NULL || 55197688Stjr (jp = getcurjob(jp)) == NULL) 55297688Stjr error("No previous job"); 55397688Stjr return (jp); 5541556Srgrimes#endif 55597688Stjr } else if (name[1] == '?') { 55697688Stjr found = NULL; 55797688Stjr for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 55897688Stjr if (jp->used && jp->nprocs > 0 55997688Stjr && strstr(jp->ps[0].cmd, name + 2) != NULL) { 56097688Stjr if (found) 56197688Stjr error("%s: ambiguous", name); 56297688Stjr found = jp; 56397688Stjr } 56497688Stjr } 56597688Stjr if (found != NULL) 56697688Stjr return (found); 5671556Srgrimes } else { 56897688Stjr found = NULL; 5691556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5701556Srgrimes if (jp->used && jp->nprocs > 0 5711556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 5721556Srgrimes if (found) 5731556Srgrimes error("%s: ambiguous", name); 5741556Srgrimes found = jp; 5751556Srgrimes } 5761556Srgrimes } 5771556Srgrimes if (found) 5781556Srgrimes return found; 5791556Srgrimes } 5801556Srgrimes } else if (is_number(name)) { 581100308Stjr pid = (pid_t)number(name); 5821556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5831556Srgrimes if (jp->used && jp->nprocs > 0 5841556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 5851556Srgrimes return jp; 5861556Srgrimes } 5871556Srgrimes } 5881556Srgrimes error("No such job: %s", name); 58917987Speter /*NOTREACHED*/ 59017987Speter return NULL; 5911556Srgrimes} 5921556Srgrimes 5931556Srgrimes 5941556Srgrimes 5951556Srgrimes/* 5961556Srgrimes * Return a new job structure, 5971556Srgrimes */ 5981556Srgrimes 5991556Srgrimesstruct job * 60090111Simpmakejob(union node *node __unused, int nprocs) 60117987Speter{ 6021556Srgrimes int i; 6031556Srgrimes struct job *jp; 6041556Srgrimes 6051556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 6061556Srgrimes if (--i < 0) { 6071556Srgrimes INTOFF; 6081556Srgrimes if (njobs == 0) { 6091556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 61097664Stjr#if JOBS 61197659Stjr jobmru = NULL; 61297664Stjr#endif 6131556Srgrimes } else { 6141556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 61517987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 61697659Stjr#if JOBS 61797659Stjr /* Relocate `next' pointers and list head */ 61899760Stjr if (jobmru != NULL) 61999760Stjr jobmru = &jp[jobmru - jobtab]; 62097659Stjr for (i = 0; i < njobs; i++) 62197659Stjr if (jp[i].next != NULL) 62297659Stjr jp[i].next = &jp[jp[i].next - 62397659Stjr jobtab]; 62497659Stjr#endif 62520425Ssteve /* Relocate `ps' pointers */ 62620425Ssteve for (i = 0; i < njobs; i++) 62720425Ssteve if (jp[i].ps == &jobtab[i].ps0) 62820425Ssteve jp[i].ps = &jp[i].ps0; 6291556Srgrimes ckfree(jobtab); 6301556Srgrimes jobtab = jp; 6311556Srgrimes } 6321556Srgrimes jp = jobtab + njobs; 6331556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 6341556Srgrimes INTON; 6351556Srgrimes break; 6361556Srgrimes } 6371556Srgrimes if (jp->used == 0) 6381556Srgrimes break; 6391556Srgrimes } 6401556Srgrimes INTOFF; 6411556Srgrimes jp->state = 0; 6421556Srgrimes jp->used = 1; 6431556Srgrimes jp->changed = 0; 6441556Srgrimes jp->nprocs = 0; 645100305Stjr jp->foreground = 0; 6461556Srgrimes#if JOBS 6471556Srgrimes jp->jobctl = jobctl; 64897659Stjr jp->next = NULL; 6491556Srgrimes#endif 6501556Srgrimes if (nprocs > 1) { 6511556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 6521556Srgrimes } else { 6531556Srgrimes jp->ps = &jp->ps0; 6541556Srgrimes } 6551556Srgrimes INTON; 656149802Sstefanf TRACE(("makejob(%p, %d) returns %%%d\n", (void *)node, nprocs, 65717987Speter jp - jobtab + 1)); 6581556Srgrimes return jp; 6598855Srgrimes} 6601556Srgrimes 66197659Stjr#if JOBS 66297659StjrSTATIC void 66397659Stjrsetcurjob(struct job *cj) 66497659Stjr{ 66597659Stjr struct job *jp, *prev; 6661556Srgrimes 66797659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 66897659Stjr if (jp == cj) { 66997659Stjr if (prev != NULL) 67097659Stjr prev->next = jp->next; 67197659Stjr else 67297659Stjr jobmru = jp->next; 67397659Stjr jp->next = jobmru; 67497659Stjr jobmru = cj; 67597659Stjr return; 67697659Stjr } 67797659Stjr } 67897659Stjr cj->next = jobmru; 67997659Stjr jobmru = cj; 68097659Stjr} 68197659Stjr 68297659StjrSTATIC void 68397659Stjrdeljob(struct job *j) 68497659Stjr{ 68597659Stjr struct job *jp, *prev; 68697659Stjr 68797659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 68897659Stjr if (jp == j) { 68997659Stjr if (prev != NULL) 69097659Stjr prev->next = jp->next; 69197659Stjr else 69297659Stjr jobmru = jp->next; 69397659Stjr return; 69497659Stjr } 69597659Stjr } 69697659Stjr} 69797659Stjr 6981556Srgrimes/* 69997659Stjr * Return the most recently used job that isn't `nj', and preferably one 70097659Stjr * that is stopped. 70197659Stjr */ 70297659StjrSTATIC struct job * 70397659Stjrgetcurjob(struct job *nj) 70497659Stjr{ 70597659Stjr struct job *jp; 70697659Stjr 70797659Stjr /* Try to find a stopped one.. */ 70897659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 70997659Stjr if (jp->used && jp != nj && jp->state == JOBSTOPPED) 71097659Stjr return (jp); 71197659Stjr /* Otherwise the most recently used job that isn't `nj' */ 71297659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 71397659Stjr if (jp->used && jp != nj) 71497659Stjr return (jp); 71597659Stjr 71697659Stjr return (NULL); 71797659Stjr} 71897659Stjr 71997659Stjr#endif 72097659Stjr 72197659Stjr/* 7221556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 7231556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 7241556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 7251556Srgrimes * be NULL. The mode parameter can be one of the following: 7261556Srgrimes * FORK_FG - Fork off a foreground process. 7271556Srgrimes * FORK_BG - Fork off a background process. 7281556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 7291556Srgrimes * process group even if job control is on. 7301556Srgrimes * 7311556Srgrimes * When job control is turned off, background processes have their standard 7321556Srgrimes * input redirected to /dev/null (except for the second and later processes 7331556Srgrimes * in a pipeline). 7341556Srgrimes */ 7351556Srgrimes 736100308Stjrpid_t 73790111Simpforkshell(struct job *jp, union node *n, int mode) 73817987Speter{ 739100308Stjr pid_t pid; 740100308Stjr pid_t pgrp; 7411556Srgrimes 742149802Sstefanf TRACE(("forkshell(%%%d, %p, %d) called\n", jp - jobtab, (void *)n, 74317987Speter mode)); 7441556Srgrimes INTOFF; 745112341Stjr flushall(); 7461556Srgrimes pid = fork(); 7471556Srgrimes if (pid == -1) { 7481556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 7491556Srgrimes INTON; 75053891Scracauer error("Cannot fork: %s", strerror(errno)); 7511556Srgrimes } 7521556Srgrimes if (pid == 0) { 7531556Srgrimes struct job *p; 7541556Srgrimes int wasroot; 7551556Srgrimes int i; 7561556Srgrimes 757100308Stjr TRACE(("Child shell %d\n", (int)getpid())); 7581556Srgrimes wasroot = rootshell; 7591556Srgrimes rootshell = 0; 760200998Sjilles handler = &main_handler; 7611556Srgrimes closescript(); 7621556Srgrimes INTON; 7631556Srgrimes clear_traps(); 7641556Srgrimes#if JOBS 7651556Srgrimes jobctl = 0; /* do job control only in root shell */ 7661556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 7671556Srgrimes if (jp == NULL || jp->nprocs == 0) 7681556Srgrimes pgrp = getpid(); 7691556Srgrimes else 7701556Srgrimes pgrp = jp->ps[0].pid; 77121352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 7721556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 77399762Stjr if (tcsetpgrp(ttyfd, pgrp) < 0) 77420425Ssteve error("tcsetpgrp failed, errno=%d", errno); 7751556Srgrimes } 7761556Srgrimes setsignal(SIGTSTP); 7771556Srgrimes setsignal(SIGTTOU); 7781556Srgrimes } else if (mode == FORK_BG) { 7791556Srgrimes ignoresig(SIGINT); 7801556Srgrimes ignoresig(SIGQUIT); 7811556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7821556Srgrimes ! fd0_redirected_p ()) { 7831556Srgrimes close(0); 78469793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 78569793Sobrien error("Can't open %s: %s", 78669793Sobrien _PATH_DEVNULL, strerror(errno)); 7871556Srgrimes } 7881556Srgrimes } 7891556Srgrimes#else 7901556Srgrimes if (mode == FORK_BG) { 7911556Srgrimes ignoresig(SIGINT); 7921556Srgrimes ignoresig(SIGQUIT); 7931556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 7941556Srgrimes ! fd0_redirected_p ()) { 7951556Srgrimes close(0); 79669793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 797155301Sschweikh error("Can't open %s: %s", 79869793Sobrien _PATH_DEVNULL, strerror(errno)); 7991556Srgrimes } 8001556Srgrimes } 8011556Srgrimes#endif 802102051Stjr INTOFF; 803102051Stjr for (i = njobs, p = jobtab ; --i >= 0 ; p++) 804102051Stjr if (p->used) 805102051Stjr freejob(p); 806102051Stjr INTON; 8071556Srgrimes if (wasroot && iflag) { 8081556Srgrimes setsignal(SIGINT); 8091556Srgrimes setsignal(SIGQUIT); 8101556Srgrimes setsignal(SIGTERM); 8111556Srgrimes } 8121556Srgrimes return pid; 8131556Srgrimes } 8141556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 8151556Srgrimes if (jp == NULL || jp->nprocs == 0) 8161556Srgrimes pgrp = pid; 8171556Srgrimes else 8181556Srgrimes pgrp = jp->ps[0].pid; 81917987Speter setpgid(pid, pgrp); 8201556Srgrimes } 8211556Srgrimes if (mode == FORK_BG) 8221556Srgrimes backgndpid = pid; /* set $! */ 8231556Srgrimes if (jp) { 8241556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 8251556Srgrimes ps->pid = pid; 8261556Srgrimes ps->status = -1; 8271556Srgrimes ps->cmd = nullstr; 8281556Srgrimes if (iflag && rootshell && n) 8291556Srgrimes ps->cmd = commandtext(n); 830100305Stjr jp->foreground = mode == FORK_FG; 83197659Stjr#if JOBS 83297659Stjr setcurjob(jp); 83397659Stjr#endif 8341556Srgrimes } 8351556Srgrimes INTON; 836100308Stjr TRACE(("In parent shell: child = %d\n", (int)pid)); 8371556Srgrimes return pid; 8381556Srgrimes} 8391556Srgrimes 8401556Srgrimes 8411556Srgrimes 8421556Srgrimes/* 8431556Srgrimes * Wait for job to finish. 8441556Srgrimes * 8451556Srgrimes * Under job control we have the problem that while a child process is 8461556Srgrimes * running interrupts generated by the user are sent to the child but not 8471556Srgrimes * to the shell. This means that an infinite loop started by an inter- 8481556Srgrimes * active user may be hard to kill. With job control turned off, an 8491556Srgrimes * interactive user may place an interactive program inside a loop. If 8501556Srgrimes * the interactive program catches interrupts, the user doesn't want 8511556Srgrimes * these interrupts to also abort the loop. The approach we take here 8521556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 85346684Skris * foreground process to terminate, and then send itself an interrupt 8541556Srgrimes * signal if the child process was terminated by an interrupt signal. 8551556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 8561556Srgrimes * exit on interrupt; unless these processes terminate themselves by 8571556Srgrimes * sending a signal to themselves (instead of calling exit) they will 8581556Srgrimes * confuse this approach. 8591556Srgrimes */ 8601556Srgrimes 8611556Srgrimesint 86290111Simpwaitforjob(struct job *jp, int *origstatus) 86345916Scracauer{ 8641556Srgrimes#if JOBS 865100308Stjr pid_t mypgrp = getpgrp(); 8661556Srgrimes#endif 8671556Srgrimes int status; 8681556Srgrimes int st; 8691556Srgrimes 8701556Srgrimes INTOFF; 8711556Srgrimes TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 87238950Scracauer while (jp->state == 0) 87338950Scracauer if (dowait(1, jp) == -1) 87438950Scracauer dotrap(); 8751556Srgrimes#if JOBS 8761556Srgrimes if (jp->jobctl) { 87799762Stjr if (tcsetpgrp(ttyfd, mypgrp) < 0) 87820425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 8791556Srgrimes } 8801556Srgrimes if (jp->state == JOBSTOPPED) 88197659Stjr setcurjob(jp); 8821556Srgrimes#endif 8831556Srgrimes status = jp->ps[jp->nprocs - 1].status; 88445916Scracauer if (origstatus != NULL) 88545916Scracauer *origstatus = status; 8861556Srgrimes /* convert to 8 bits */ 88726104Ssteve if (WIFEXITED(status)) 88826104Ssteve st = WEXITSTATUS(status); 8891556Srgrimes#if JOBS 89026104Ssteve else if (WIFSTOPPED(status)) 89126104Ssteve st = WSTOPSIG(status) + 128; 8921556Srgrimes#endif 8931556Srgrimes else 89426104Ssteve st = WTERMSIG(status) + 128; 8951556Srgrimes if (! JOBS || jp->state == JOBDONE) 8961556Srgrimes freejob(jp); 89738521Scracauer if (int_pending()) { 89838521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 89938521Scracauer kill(getpid(), SIGINT); 90038521Scracauer else 90138521Scracauer CLEAR_PENDING_INT; 90238521Scracauer } 9031556Srgrimes INTON; 9041556Srgrimes return st; 9051556Srgrimes} 9061556Srgrimes 9071556Srgrimes 9081556Srgrimes 9091556Srgrimes/* 9101556Srgrimes * Wait for a process to terminate. 9111556Srgrimes */ 9121556Srgrimes 913100308StjrSTATIC pid_t 91490111Simpdowait(int block, struct job *job) 91517987Speter{ 916100308Stjr pid_t pid; 9171556Srgrimes int status; 9181556Srgrimes struct procstat *sp; 9191556Srgrimes struct job *jp; 9201556Srgrimes struct job *thisjob; 9211556Srgrimes int done; 9221556Srgrimes int stopped; 92326104Ssteve int sig; 924100305Stjr int i; 9251556Srgrimes 92638950Scracauer in_dowait++; 9271556Srgrimes TRACE(("dowait(%d) called\n", block)); 9281556Srgrimes do { 9291556Srgrimes pid = waitproc(block, &status); 930100308Stjr TRACE(("wait returns %d, status=%d\n", (int)pid, status)); 93172086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 932125501Scracauer (pid > 0 && WIFSTOPPED(status) && !iflag)); 93338950Scracauer in_dowait--; 934153417Smaxim if (pid == -1 && errno == ECHILD && job != NULL) 935153417Smaxim job->state = JOBDONE; 93638521Scracauer if (breakwaitcmd != 0) { 93738521Scracauer breakwaitcmd = 0; 938138312Smaxim if (pid <= 0) 939138312Smaxim return -1; 94038521Scracauer } 9411556Srgrimes if (pid <= 0) 9421556Srgrimes return pid; 9431556Srgrimes INTOFF; 9441556Srgrimes thisjob = NULL; 9451556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 9461556Srgrimes if (jp->used) { 9471556Srgrimes done = 1; 9481556Srgrimes stopped = 1; 9491556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 9501556Srgrimes if (sp->pid == -1) 9511556Srgrimes continue; 9521556Srgrimes if (sp->pid == pid) { 95326104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 954100308Stjr (int)pid, sp->status, 955100308Stjr status)); 9561556Srgrimes sp->status = status; 9571556Srgrimes thisjob = jp; 9581556Srgrimes } 9591556Srgrimes if (sp->status == -1) 9601556Srgrimes stopped = 0; 96126104Ssteve else if (WIFSTOPPED(sp->status)) 9621556Srgrimes done = 0; 9631556Srgrimes } 9641556Srgrimes if (stopped) { /* stopped or done */ 9651556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 9661556Srgrimes if (jp->state != state) { 9671556Srgrimes TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 9681556Srgrimes jp->state = state; 9691556Srgrimes#if JOBS 97097659Stjr if (done) 97197659Stjr deljob(jp); 9721556Srgrimes#endif 9731556Srgrimes } 9741556Srgrimes } 9751556Srgrimes } 9761556Srgrimes } 9771556Srgrimes INTON; 9781556Srgrimes if (! rootshell || ! iflag || (job && thisjob == job)) { 9791556Srgrimes#if JOBS 98026104Ssteve if (WIFSTOPPED(status)) 98126104Ssteve sig = WSTOPSIG(status); 98226104Ssteve else 9831556Srgrimes#endif 98497663Stjr { 98526104Ssteve if (WIFEXITED(status)) 98626104Ssteve sig = 0; 98726104Ssteve else 98826104Ssteve sig = WTERMSIG(status); 9891556Srgrimes } 990100305Stjr if (sig != 0 && sig != SIGINT && sig != SIGPIPE) { 991107846Stjr if (!mflag || 992107846Stjr (thisjob->foreground && !WIFSTOPPED(status))) { 993102007Stjr i = WTERMSIG(status); 994125155Snjl if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F]) 995100305Stjr out1str(sys_siglist[i & 0x7F]); 996100305Stjr else 997100305Stjr out1fmt("Signal %d", i & 0x7F); 998100305Stjr if (WCOREDUMP(status)) 999100305Stjr out1str(" (core dumped)"); 1000100305Stjr out1c('\n'); 1001100305Stjr } else 1002163085Sstefanf showjob(thisjob, pid, SHOWJOBS_DEFAULT); 1003100305Stjr } 10041556Srgrimes } else { 1005109627Stjr TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job)); 10061556Srgrimes if (thisjob) 10071556Srgrimes thisjob->changed = 1; 10081556Srgrimes } 10091556Srgrimes return pid; 10101556Srgrimes} 10111556Srgrimes 10121556Srgrimes 10131556Srgrimes 10141556Srgrimes/* 10151556Srgrimes * Do a wait system call. If job control is compiled in, we accept 10161556Srgrimes * stopped processes. If block is zero, we return a value of zero 10171556Srgrimes * rather than blocking. 10181556Srgrimes */ 1019100308StjrSTATIC pid_t 102090111Simpwaitproc(int block, int *status) 102117987Speter{ 10221556Srgrimes int flags; 10231556Srgrimes 10241556Srgrimes#if JOBS 10251556Srgrimes flags = WUNTRACED; 10261556Srgrimes#else 10271556Srgrimes flags = 0; 10281556Srgrimes#endif 10291556Srgrimes if (block == 0) 10301556Srgrimes flags |= WNOHANG; 10311556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 10321556Srgrimes} 10331556Srgrimes 10341556Srgrimes/* 10351556Srgrimes * return 1 if there are stopped jobs, otherwise 0 10361556Srgrimes */ 10371556Srgrimesint job_warning = 0; 10381556Srgrimesint 103990111Simpstoppedjobs(void) 10401556Srgrimes{ 104125222Ssteve int jobno; 104225222Ssteve struct job *jp; 10431556Srgrimes 10441556Srgrimes if (job_warning) 10451556Srgrimes return (0); 10461556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 10471556Srgrimes if (jp->used == 0) 10481556Srgrimes continue; 10491556Srgrimes if (jp->state == JOBSTOPPED) { 1050199629Sjilles out2fmt_flush("You have stopped jobs.\n"); 10511556Srgrimes job_warning = 2; 10521556Srgrimes return (1); 10531556Srgrimes } 10541556Srgrimes } 10551556Srgrimes 10561556Srgrimes return (0); 10571556Srgrimes} 10581556Srgrimes 10591556Srgrimes/* 10601556Srgrimes * Return a string identifying a command (to be printed by the 10611556Srgrimes * jobs command. 10621556Srgrimes */ 10631556Srgrimes 10641556SrgrimesSTATIC char *cmdnextc; 10651556SrgrimesSTATIC int cmdnleft; 10661556Srgrimes#define MAXCMDTEXT 200 10671556Srgrimes 10681556Srgrimeschar * 106990111Simpcommandtext(union node *n) 107090111Simp{ 10711556Srgrimes char *name; 10721556Srgrimes 10731556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 10741556Srgrimes cmdnleft = MAXCMDTEXT - 4; 10751556Srgrimes cmdtxt(n); 10761556Srgrimes *cmdnextc = '\0'; 10771556Srgrimes return name; 10781556Srgrimes} 10791556Srgrimes 10801556Srgrimes 10811556SrgrimesSTATIC void 108290111Simpcmdtxt(union node *n) 108390111Simp{ 10841556Srgrimes union node *np; 10851556Srgrimes struct nodelist *lp; 10861556Srgrimes char *p; 10871556Srgrimes int i; 10881556Srgrimes char s[2]; 10891556Srgrimes 10901556Srgrimes if (n == NULL) 10911556Srgrimes return; 10921556Srgrimes switch (n->type) { 10931556Srgrimes case NSEMI: 10941556Srgrimes cmdtxt(n->nbinary.ch1); 10951556Srgrimes cmdputs("; "); 10961556Srgrimes cmdtxt(n->nbinary.ch2); 10971556Srgrimes break; 10981556Srgrimes case NAND: 10991556Srgrimes cmdtxt(n->nbinary.ch1); 11001556Srgrimes cmdputs(" && "); 11011556Srgrimes cmdtxt(n->nbinary.ch2); 11021556Srgrimes break; 11031556Srgrimes case NOR: 11041556Srgrimes cmdtxt(n->nbinary.ch1); 11051556Srgrimes cmdputs(" || "); 11061556Srgrimes cmdtxt(n->nbinary.ch2); 11071556Srgrimes break; 11081556Srgrimes case NPIPE: 11091556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 11101556Srgrimes cmdtxt(lp->n); 11111556Srgrimes if (lp->next) 11121556Srgrimes cmdputs(" | "); 11131556Srgrimes } 11141556Srgrimes break; 11151556Srgrimes case NSUBSHELL: 11161556Srgrimes cmdputs("("); 11171556Srgrimes cmdtxt(n->nredir.n); 11181556Srgrimes cmdputs(")"); 11191556Srgrimes break; 11201556Srgrimes case NREDIR: 11211556Srgrimes case NBACKGND: 11221556Srgrimes cmdtxt(n->nredir.n); 11231556Srgrimes break; 11241556Srgrimes case NIF: 11251556Srgrimes cmdputs("if "); 11261556Srgrimes cmdtxt(n->nif.test); 11271556Srgrimes cmdputs("; then "); 11281556Srgrimes cmdtxt(n->nif.ifpart); 11291556Srgrimes cmdputs("..."); 11301556Srgrimes break; 11311556Srgrimes case NWHILE: 11321556Srgrimes cmdputs("while "); 11331556Srgrimes goto until; 11341556Srgrimes case NUNTIL: 11351556Srgrimes cmdputs("until "); 11361556Srgrimesuntil: 11371556Srgrimes cmdtxt(n->nbinary.ch1); 11381556Srgrimes cmdputs("; do "); 11391556Srgrimes cmdtxt(n->nbinary.ch2); 11401556Srgrimes cmdputs("; done"); 11411556Srgrimes break; 11421556Srgrimes case NFOR: 11431556Srgrimes cmdputs("for "); 11441556Srgrimes cmdputs(n->nfor.var); 11451556Srgrimes cmdputs(" in ..."); 11461556Srgrimes break; 11471556Srgrimes case NCASE: 11481556Srgrimes cmdputs("case "); 11491556Srgrimes cmdputs(n->ncase.expr->narg.text); 11501556Srgrimes cmdputs(" in ..."); 11511556Srgrimes break; 11521556Srgrimes case NDEFUN: 11531556Srgrimes cmdputs(n->narg.text); 11541556Srgrimes cmdputs("() ..."); 11551556Srgrimes break; 11561556Srgrimes case NCMD: 11571556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 11581556Srgrimes cmdtxt(np); 11591556Srgrimes if (np->narg.next) 11601556Srgrimes cmdputs(" "); 11611556Srgrimes } 11621556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 11631556Srgrimes cmdputs(" "); 11641556Srgrimes cmdtxt(np); 11651556Srgrimes } 11661556Srgrimes break; 11671556Srgrimes case NARG: 11681556Srgrimes cmdputs(n->narg.text); 11691556Srgrimes break; 11701556Srgrimes case NTO: 11711556Srgrimes p = ">"; i = 1; goto redir; 11721556Srgrimes case NAPPEND: 11731556Srgrimes p = ">>"; i = 1; goto redir; 11741556Srgrimes case NTOFD: 11751556Srgrimes p = ">&"; i = 1; goto redir; 117696922Stjr case NCLOBBER: 117796922Stjr p = ">|"; i = 1; goto redir; 11781556Srgrimes case NFROM: 11791556Srgrimes p = "<"; i = 0; goto redir; 118066612Sbrian case NFROMTO: 118166612Sbrian p = "<>"; i = 0; goto redir; 11821556Srgrimes case NFROMFD: 11831556Srgrimes p = "<&"; i = 0; goto redir; 11841556Srgrimesredir: 11851556Srgrimes if (n->nfile.fd != i) { 11861556Srgrimes s[0] = n->nfile.fd + '0'; 11871556Srgrimes s[1] = '\0'; 11881556Srgrimes cmdputs(s); 11891556Srgrimes } 11901556Srgrimes cmdputs(p); 11911556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 119299634Stjr if (n->ndup.dupfd >= 0) 119399634Stjr s[0] = n->ndup.dupfd + '0'; 119499634Stjr else 119599634Stjr s[0] = '-'; 11961556Srgrimes s[1] = '\0'; 11971556Srgrimes cmdputs(s); 11981556Srgrimes } else { 11991556Srgrimes cmdtxt(n->nfile.fname); 12001556Srgrimes } 12011556Srgrimes break; 12021556Srgrimes case NHERE: 12031556Srgrimes case NXHERE: 12041556Srgrimes cmdputs("<<..."); 12051556Srgrimes break; 12061556Srgrimes default: 12071556Srgrimes cmdputs("???"); 12081556Srgrimes break; 12091556Srgrimes } 12101556Srgrimes} 12111556Srgrimes 12121556Srgrimes 12131556Srgrimes 12141556SrgrimesSTATIC void 121590111Simpcmdputs(char *s) 121690111Simp{ 121725222Ssteve char *p, *q; 121825222Ssteve char c; 12191556Srgrimes int subtype = 0; 12201556Srgrimes 12211556Srgrimes if (cmdnleft <= 0) 12221556Srgrimes return; 12231556Srgrimes p = s; 12241556Srgrimes q = cmdnextc; 12251556Srgrimes while ((c = *p++) != '\0') { 12261556Srgrimes if (c == CTLESC) 12271556Srgrimes *q++ = *p++; 12281556Srgrimes else if (c == CTLVAR) { 12291556Srgrimes *q++ = '$'; 12301556Srgrimes if (--cmdnleft > 0) 12311556Srgrimes *q++ = '{'; 12321556Srgrimes subtype = *p++; 12331556Srgrimes } else if (c == '=' && subtype != 0) { 12341556Srgrimes *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 12351556Srgrimes subtype = 0; 12361556Srgrimes } else if (c == CTLENDVAR) { 12371556Srgrimes *q++ = '}'; 123818954Ssteve } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) 12391556Srgrimes cmdnleft++; /* ignore it */ 12401556Srgrimes else 12411556Srgrimes *q++ = c; 12421556Srgrimes if (--cmdnleft <= 0) { 12431556Srgrimes *q++ = '.'; 12441556Srgrimes *q++ = '.'; 12451556Srgrimes *q++ = '.'; 12461556Srgrimes break; 12471556Srgrimes } 12481556Srgrimes } 12491556Srgrimes cmdnextc = q; 12501556Srgrimes} 1251