2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/ioctl.h>
42 #include <sys/param.h>
43 #include <sys/resource.h>
57 #undef CEOF /* syntax.h redefines this */
78 static struct job *jobtab; /* array of jobs */
79 static int njobs; /* size of array */
80 static pid_t backgndpid = -1; /* pid of last background process */
81 static struct job *bgjob = NULL; /* last background process */
83 static struct job *jobmru; /* most recently used job list */
84 static pid_t initialpgrp; /* pgrp of shell on invocation */
86 static int ttyfd = -1;
88 /* mode flags for dowait */
89 #define DOWAIT_BLOCK 0x1 /* wait until a child exits */
90 #define DOWAIT_SIG 0x2 /* if DOWAIT_BLOCK, abort on SIGINT/SIGQUIT */
91 #define DOWAIT_SIG_ANY 0x4 /* if DOWAIT_SIG, abort on any signal */
94 static void restartjob(struct job *);
96 static void freejob(struct job *);
97 static int waitcmdloop(struct job *);
98 static struct job *getjob_nonotfound(char *);
99 static struct job *getjob(char *);
100 pid_t getjobpgrp(char *);
101 static pid_t dowait(int, struct job *);
102 static void checkzombies(void);
103 static void cmdtxt(union node *);
104 static void cmdputs(const char *);
106 static void setcurjob(struct job *);
107 static void deljob(struct job *);
108 static struct job *getcurjob(struct job *);
110 static void printjobcmd(struct job *);
111 static void showjob(struct job *, int);
115 * Turn job control on and off.
126 if (on == jobctl || rootshell == 0)
131 if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
133 while (i <= 2 && !isatty(i))
136 (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0)
141 * Keep our TTY file descriptor out of the way of
142 * the user's redirections.
144 if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
152 do { /* while we are in the background */
153 initialpgrp = tcgetpgrp(ttyfd);
154 if (initialpgrp < 0) {
155 out: out2fmt_flush("sh: can't access tty; job control turned off\n");
159 if (initialpgrp != getpgrp()) {
168 tcsetpgrp(ttyfd, rootpid);
169 } else { /* turning job control off */
170 setpgid(0, initialpgrp);
171 tcsetpgrp(ttyfd, initialpgrp);
185 fgcmd(int argc __unused, char **argv __unused)
192 jp = getjob(*argptr);
194 error("job not created under job control");
197 pgrp = jp->ps[0].pid;
198 tcsetpgrp(ttyfd, pgrp);
202 status = waitforjob(jp, (int *)NULL);
209 bgcmd(int argc, char **argv)
215 jp = getjob(*argptr);
217 error("job not created under job control");
218 if (jp->state == JOBDONE)
222 out1fmt("[%td] ", jp - jobtab + 1);
224 } while (*argptr != NULL && *++argptr != NULL);
230 restartjob(struct job *jp)
235 if (jp->state == JOBDONE)
239 kill(-jp->ps[0].pid, SIGCONT);
240 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
241 if (WIFSTOPPED(ps->status)) {
252 jobscmd(int argc __unused, char *argv[] __unused)
257 mode = SHOWJOBS_DEFAULT;
258 while ((ch = nextopt("lps")) != '\0') {
261 mode = SHOWJOBS_VERBOSE;
264 mode = SHOWJOBS_PGIDS;
267 mode = SHOWJOBS_PIDS;
275 while ((id = *argptr++) != NULL)
276 showjob(getjob(id), mode);
282 printjobcmd(struct job *jp)
287 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
296 showjob(struct job *jp, int mode)
303 int col, curr, i, jobno, prev, procno;
306 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
307 jobno = jp - jobtab + 1;
310 if ((j = getcurjob(NULL)) != NULL) {
311 curr = j - jobtab + 1;
312 if ((j = getcurjob(j)) != NULL)
313 prev = j - jobtab + 1;
316 ps = jp->ps + jp->nprocs - 1;
317 if (jp->state == 0) {
318 strcpy(statestr, "Running");
320 } else if (jp->state == JOBSTOPPED) {
321 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
323 if (WIFSTOPPED(ps->status))
324 i = WSTOPSIG(ps->status);
327 sigstr = strsignal(i);
329 strcpy(statestr, sigstr);
331 strcpy(statestr, "Suspended");
333 } else if (WIFEXITED(ps->status)) {
334 if (WEXITSTATUS(ps->status) == 0)
335 strcpy(statestr, "Done");
337 fmtstr(statestr, 64, "Done(%d)",
338 WEXITSTATUS(ps->status));
340 i = WTERMSIG(ps->status);
341 sigstr = strsignal(i);
343 strcpy(statestr, sigstr);
345 strcpy(statestr, "Unknown signal");
346 if (WCOREDUMP(ps->status))
347 strcat(statestr, " (core dumped)");
350 for (ps = jp->ps ; ; ps++) { /* for each process */
351 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
352 out1fmt("%d\n", (int)ps->pid);
355 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
357 if (jobno == curr && ps == jp->ps)
359 else if (jobno == prev && ps == jp->ps)
364 fmtstr(s, 64, "[%d] %c ", jobno, c);
366 fmtstr(s, 64, " %c ", c);
369 if (mode == SHOWJOBS_VERBOSE) {
370 fmtstr(s, 64, "%d ", (int)ps->pid);
376 col += strlen(statestr);
382 if (mode == SHOWJOBS_VERBOSE) {
387 skip: if (--procno <= 0)
393 * Print a list of jobs. If "change" is nonzero, only print jobs whose
394 * statuses have changed since the last call to showjobs.
396 * If the shell is interrupted in the process of creating a job, the
397 * result may be a job structure containing zero processes. Such structures
398 * will be freed here.
402 showjobs(int change, int mode)
407 TRACE(("showjobs(%d) called\n", change));
409 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
412 if (jp->nprocs == 0) {
416 if (change && ! jp->changed)
419 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
421 /* Hack: discard jobs for which $! has not been
422 * referenced in interactive mode when they terminate.
424 if (jp->state == JOBDONE && !jp->remembered &&
425 (iflag || jp != bgjob)) {
434 * Mark a job structure as unused.
438 freejob(struct job *jp)
446 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
447 if (ps->cmd != nullstr)
450 if (jp->ps != &jp->ps0)
462 waitcmd(int argc __unused, char **argv __unused)
469 return (waitcmdloop(NULL));
472 job = getjob_nonotfound(*argptr);
476 retval = waitcmdloop(job);
478 } while (*argptr != NULL);
484 waitcmdloop(struct job *job)
486 int status, retval, sig;
490 * Loop until a process is terminated or stopped, or a SIGINT is
496 if (job->state == JOBDONE) {
497 status = job->ps[job->nprocs - 1].status;
498 if (WIFEXITED(status))
499 retval = WEXITSTATUS(status);
501 retval = WTERMSIG(status) + 128;
502 if (! iflag || ! job->changed)
512 for (jp = jobtab ; jp < jobtab + njobs; jp++)
513 if (jp->used && jp->state == JOBDONE) {
514 if (! iflag || ! jp->changed)
522 for (jp = jobtab ; ; jp++) {
523 if (jp >= jobtab + njobs) { /* no running procs */
526 if (jp->used && jp->state == 0)
530 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
532 sig = pendingsig_waitcmd;
533 pendingsig_waitcmd = 0;
540 jobidcmd(int argc __unused, char **argv __unused)
546 jp = getjob(*argptr);
547 for (i = 0 ; i < jp->nprocs ; ) {
548 out1fmt("%d", (int)jp->ps[i].pid);
549 out1c(++i < jp->nprocs? ' ' : '\n');
557 * Convert a job name to a job structure.
561 getjob_nonotfound(char *name)
564 struct job *found, *jp;
570 currentjob: if ((jp = getcurjob(NULL)) == NULL)
571 error("No current job");
574 error("No current job");
576 } else if (name[0] == '%') {
577 if (is_digit(name[1])) {
578 jobno = number(name + 1);
579 if (jobno > 0 && jobno <= njobs
580 && jobtab[jobno - 1].used != 0)
581 return &jobtab[jobno - 1];
583 } else if (name[1] == '%' && name[2] == '\0') {
585 } else if (name[1] == '+' && name[2] == '\0') {
587 } else if (name[1] == '-' && name[2] == '\0') {
588 if ((jp = getcurjob(NULL)) == NULL ||
589 (jp = getcurjob(jp)) == NULL)
590 error("No previous job");
593 } else if (name[1] == '?') {
595 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
596 if (jp->used && jp->nprocs > 0
597 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
599 error("%s: ambiguous", name);
607 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
608 if (jp->used && jp->nprocs > 0
609 && prefix(name + 1, jp->ps[0].cmd)) {
611 error("%s: ambiguous", name);
618 } else if (is_number(name)) {
619 pid = (pid_t)number(name);
620 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
621 if (jp->used && jp->nprocs > 0
622 && jp->ps[jp->nprocs - 1].pid == pid)
635 jp = getjob_nonotfound(name);
637 error("No such job: %s", name);
643 getjobpgrp(char *name)
648 return -jp->ps[0].pid;
652 * Return a new job structure,
656 makejob(union node *node __unused, int nprocs)
661 for (i = njobs, jp = jobtab ; ; jp++) {
665 jobtab = ckmalloc(4 * sizeof jobtab[0]);
670 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
671 memcpy(jp, jobtab, njobs * sizeof jp[0]);
673 /* Relocate `next' pointers and list head */
675 jobmru = &jp[jobmru - jobtab];
676 for (i = 0; i < njobs; i++)
677 if (jp[i].next != NULL)
678 jp[i].next = &jp[jp[i].next -
682 bgjob = &jp[bgjob - jobtab];
683 /* Relocate `ps' pointers */
684 for (i = 0; i < njobs; i++)
685 if (jp[i].ps == &jobtab[i].ps0)
686 jp[i].ps = &jp[i].ps0;
691 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
711 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
716 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
723 setcurjob(struct job *cj)
725 struct job *jp, *prev;
727 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
730 prev->next = jp->next;
743 deljob(struct job *j)
745 struct job *jp, *prev;
747 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
750 prev->next = jp->next;
759 * Return the most recently used job that isn't `nj', and preferably one
763 getcurjob(struct job *nj)
767 /* Try to find a stopped one.. */
768 for (jp = jobmru; jp != NULL; jp = jp->next)
769 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
771 /* Otherwise the most recently used job that isn't `nj' */
772 for (jp = jobmru; jp != NULL; jp = jp->next)
773 if (jp->used && jp != nj)
782 * Fork of a subshell. If we are doing job control, give the subshell its
783 * own process group. Jp is a job structure that the job is to be added to.
784 * N is the command that will be evaluated by the child. Both jp and n may
785 * be NULL. The mode parameter can be one of the following:
786 * FORK_FG - Fork off a foreground process.
787 * FORK_BG - Fork off a background process.
788 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
789 * process group even if job control is on.
791 * When job control is turned off, background processes have their standard
792 * input redirected to /dev/null (except for the second and later processes
797 forkshell(struct job *jp, union node *n, int mode)
802 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
805 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
810 TRACE(("Fork failed, errno=%d\n", errno));
812 error("Cannot fork: %s", strerror(errno));
819 TRACE(("Child shell %d\n", (int)getpid()));
822 handler = &main_handler;
828 jobctl = 0; /* do job control only in root shell */
829 if (wasroot && mode != FORK_NOJOB && mflag) {
830 if (jp == NULL || jp->nprocs == 0)
833 pgrp = jp->ps[0].pid;
834 if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
835 /*** this causes superfluous TIOCSPGRPS ***/
836 if (tcsetpgrp(ttyfd, pgrp) < 0)
837 error("tcsetpgrp failed, errno=%d", errno);
841 } else if (mode == FORK_BG) {
844 if ((jp == NULL || jp->nprocs == 0) &&
845 ! fd0_redirected_p ()) {
847 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
848 error("cannot open %s: %s",
849 _PATH_DEVNULL, strerror(errno));
853 if (mode == FORK_BG) {
856 if ((jp == NULL || jp->nprocs == 0) &&
857 ! fd0_redirected_p ()) {
859 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
860 error("cannot open %s: %s",
861 _PATH_DEVNULL, strerror(errno));
866 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
870 if (wasroot && iflag) {
877 if (rootshell && mode != FORK_NOJOB && mflag) {
878 if (jp == NULL || jp->nprocs == 0)
881 pgrp = jp->ps[0].pid;
884 if (mode == FORK_BG) {
885 if (bgjob != NULL && bgjob->state == JOBDONE &&
886 !bgjob->remembered && !iflag)
888 backgndpid = pid; /* set $! */
892 struct procstat *ps = &jp->ps[jp->nprocs++];
896 if (iflag && rootshell && n)
897 ps->cmd = commandtext(n);
898 jp->foreground = mode == FORK_FG;
904 TRACE(("In parent shell: child = %d\n", (int)pid));
910 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
913 struct jmploc jmploc;
914 struct jmploc *savehandler;
916 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
920 savehandler = handler;
923 TRACE(("Vfork failed, errno=%d\n", errno));
925 error("Cannot fork: %s", strerror(errno));
928 TRACE(("Child shell %d\n", (int)getpid()));
929 if (setjmp(jmploc.loc))
930 _exit(exception == EXEXEC ? exerrno : 2);
939 shellexec(argv, envp, path, idx);
941 handler = savehandler;
943 struct procstat *ps = &jp->ps[jp->nprocs++];
953 TRACE(("In parent shell: child = %d\n", (int)pid));
959 * Wait for job to finish.
961 * Under job control we have the problem that while a child process is
962 * running interrupts generated by the user are sent to the child but not
963 * to the shell. This means that an infinite loop started by an inter-
964 * active user may be hard to kill. With job control turned off, an
965 * interactive user may place an interactive program inside a loop. If
966 * the interactive program catches interrupts, the user doesn't want
967 * these interrupts to also abort the loop. The approach we take here
968 * is to have the shell ignore interrupt signals while waiting for a
969 * foreground process to terminate, and then send itself an interrupt
970 * signal if the child process was terminated by an interrupt signal.
971 * Unfortunately, some programs want to do a bit of cleanup and then
972 * exit on interrupt; unless these processes terminate themselves by
973 * sending a signal to themselves (instead of calling exit) they will
974 * confuse this approach.
978 waitforjob(struct job *jp, int *origstatus)
981 pid_t mypgrp = getpgrp();
982 int propagate_int = jp->jobctl && jp->foreground;
988 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
989 while (jp->state == 0)
990 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
991 DOWAIT_SIG_ANY : 0), jp) == -1)
995 if (tcsetpgrp(ttyfd, mypgrp) < 0)
996 error("tcsetpgrp failed, errno=%d\n", errno);
998 if (jp->state == JOBSTOPPED)
1001 status = jp->ps[jp->nprocs - 1].status;
1002 if (origstatus != NULL)
1003 *origstatus = status;
1004 /* convert to 8 bits */
1005 if (WIFEXITED(status))
1006 st = WEXITSTATUS(status);
1008 else if (WIFSTOPPED(status))
1009 st = WSTOPSIG(status) + 128;
1012 st = WTERMSIG(status) + 128;
1013 if (! JOBS || jp->state == JOBDONE)
1015 if (int_pending()) {
1016 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1020 else if (rootshell && iflag && propagate_int &&
1021 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1022 kill(getpid(), SIGINT);
1030 dummy_handler(int sig __unused)
1035 * Wait for a process to terminate.
1039 dowait(int mode, struct job *job)
1041 struct sigaction sa, osa;
1042 sigset_t mask, omask;
1045 struct procstat *sp;
1047 struct job *thisjob;
1054 int restore_sigchld;
1056 TRACE(("dowait(%d, %p) called\n", mode, job));
1057 restore_sigchld = 0;
1058 if ((mode & DOWAIT_SIG) != 0) {
1060 sigprocmask(SIG_BLOCK, &mask, &omask);
1062 if (!issigchldtrapped()) {
1063 restore_sigchld = 1;
1064 sa.sa_handler = dummy_handler;
1066 sigemptyset(&sa.sa_mask);
1067 sigaction(SIGCHLD, &sa, &osa);
1073 wflags = WUNTRACED | WCONTINUED;
1077 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1079 pid = wait3(&status, wflags, (struct rusage *)NULL);
1080 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1081 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1083 if (((mode & DOWAIT_SIG_ANY) != 0 ?
1084 pendingsig : pendingsig_waitcmd) != 0) {
1092 } while (pid == -1 && errno == EINTR);
1093 if (pid == -1 && errno == ECHILD && job != NULL)
1094 job->state = JOBDONE;
1095 if ((mode & DOWAIT_SIG) != 0) {
1096 if (restore_sigchld)
1097 sigaction(SIGCHLD, &osa, NULL);
1098 sigprocmask(SIG_SETMASK, &omask, NULL);
1105 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1106 if (jp->used && jp->nprocs > 0) {
1109 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1112 if (sp->pid == pid) {
1113 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1114 (int)pid, sp->status,
1116 if (WIFCONTINUED(status)) {
1120 sp->status = status;
1123 if (sp->status == -1)
1125 else if (WIFSTOPPED(sp->status))
1128 if (stopped) { /* stopped or done */
1129 int state = done? JOBDONE : JOBSTOPPED;
1130 if (jp->state != state) {
1131 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1134 if (done && !jp->remembered &&
1135 !iflag && jp != bgjob)
1147 if (!thisjob || thisjob->state == 0)
1149 else if ((!rootshell || !iflag || thisjob == job) &&
1150 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1153 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1154 if (WIFSIGNALED(sp->status)) {
1155 sig = WTERMSIG(sp->status);
1156 coredump = WCOREDUMP(sp->status);
1158 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1159 sigstr = strsignal(sig);
1163 out2str("Unknown signal");
1165 out2str(" (core dumped)");
1170 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1171 thisjob->changed = 1;
1179 * return 1 if there are stopped jobs, otherwise 0
1181 int job_warning = 0;
1190 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1193 if (jp->state == JOBSTOPPED) {
1194 out2fmt_flush("You have stopped jobs.\n");
1207 while (njobs > 0 && dowait(0, NULL) > 0)
1215 return backgndpid != -1;
1222 if (bgjob != NULL && !forcelocal)
1223 bgjob->remembered = 1;
1228 * Return a string identifying a command (to be printed by the
1232 static char *cmdnextc;
1233 static int cmdnleft;
1234 #define MAXCMDTEXT 200
1237 commandtext(union node *n)
1241 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1242 cmdnleft = MAXCMDTEXT - 4;
1250 cmdtxt(union node *n)
1253 struct nodelist *lp;
1262 cmdtxt(n->nbinary.ch1);
1264 cmdtxt(n->nbinary.ch2);
1267 cmdtxt(n->nbinary.ch1);
1269 cmdtxt(n->nbinary.ch2);
1272 cmdtxt(n->nbinary.ch1);
1274 cmdtxt(n->nbinary.ch2);
1277 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1285 cmdtxt(n->nredir.n);
1290 cmdtxt(n->nredir.n);
1294 cmdtxt(n->nif.test);
1296 cmdtxt(n->nif.ifpart);
1305 cmdtxt(n->nbinary.ch1);
1307 cmdtxt(n->nbinary.ch2);
1312 cmdputs(n->nfor.var);
1317 cmdputs(n->ncase.expr->narg.text);
1321 cmdputs(n->narg.text);
1326 cmdtxt(n->nnot.com);
1329 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1334 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1340 cmdputs(n->narg.text);
1343 p = ">"; i = 1; goto redir;
1345 p = ">>"; i = 1; goto redir;
1347 p = ">&"; i = 1; goto redir;
1349 p = ">|"; i = 1; goto redir;
1351 p = "<"; i = 0; goto redir;
1353 p = "<>"; i = 0; goto redir;
1355 p = "<&"; i = 0; goto redir;
1357 if (n->nfile.fd != i) {
1358 s[0] = n->nfile.fd + '0';
1363 if (n->type == NTOFD || n->type == NFROMFD) {
1364 if (n->ndup.dupfd >= 0)
1365 s[0] = n->ndup.dupfd + '0';
1371 cmdtxt(n->nfile.fname);
1387 cmdputs(const char *s)
1398 while ((c = *p++) != '\0') {
1401 else if (c == CTLVAR) {
1406 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1408 } else if (c == '=' && subtype != 0) {
1409 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1414 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1415 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1419 } else if (c == CTLENDVAR) {
1421 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1431 } else if (c == CTLARI) {
1439 } else if (c == CTLENDARI) {
1440 if (--cmdnleft > 0) {
1444 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1445 cmdnleft++; /* ignore */
1448 if (--cmdnleft <= 0) {