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 MKINIT pid_t backgndpid = -1; /* pid of last background process */
81 MKINIT 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 int in_waitcmd = 0; /* are we in waitcmd()? */
87 volatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */
88 static int ttyfd = -1;
90 /* mode flags for dowait */
91 #define DOWAIT_BLOCK 0x1 /* wait until a child exits */
92 #define DOWAIT_SIG 0x2 /* if DOWAIT_BLOCK, abort on signals */
95 static void restartjob(struct job *);
97 static void freejob(struct job *);
98 static struct job *getjob(char *);
99 pid_t getjobpgrp(char *);
100 static pid_t dowait(int, struct job *);
101 static void checkzombies(void);
102 static void cmdtxt(union node *);
103 static void cmdputs(const char *);
105 static void setcurjob(struct job *);
106 static void deljob(struct job *);
107 static struct job *getcurjob(struct job *);
109 static void printjobcmd(struct job *);
110 static void showjob(struct job *, int);
114 * Turn job control on and off.
125 if (on == jobctl || rootshell == 0)
130 if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) {
132 while (i <= 2 && !isatty(i))
134 if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0)
139 * Keep our TTY file descriptor out of the way of
140 * the user's redirections.
142 if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) {
150 if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) {
155 do { /* while we are in the background */
156 initialpgrp = tcgetpgrp(ttyfd);
157 if (initialpgrp < 0) {
158 out: out2fmt_flush("sh: can't access tty; job control turned off\n");
162 if (initialpgrp != getpgrp()) {
171 tcsetpgrp(ttyfd, rootpid);
172 } else { /* turning job control off */
173 setpgid(0, initialpgrp);
174 tcsetpgrp(ttyfd, initialpgrp);
188 fgcmd(int argc __unused, char **argv)
194 jp = getjob(argv[1]);
196 error("job not created under job control");
199 pgrp = jp->ps[0].pid;
200 tcsetpgrp(ttyfd, pgrp);
204 status = waitforjob(jp, (int *)NULL);
211 bgcmd(int argc, char **argv)
216 jp = getjob(*++argv);
218 error("job not created under job control");
219 if (jp->state == JOBDONE)
223 out1fmt("[%td] ", jp - jobtab + 1);
225 } while (--argc > 1);
231 restartjob(struct job *jp)
236 if (jp->state == JOBDONE)
240 kill(-jp->ps[0].pid, SIGCONT);
241 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
242 if (WIFSTOPPED(ps->status)) {
253 jobscmd(int argc, char *argv[])
258 optind = optreset = 1;
260 mode = SHOWJOBS_DEFAULT;
261 while ((ch = getopt(argc, argv, "lps")) != -1) {
264 mode = SHOWJOBS_VERBOSE;
267 mode = SHOWJOBS_PGIDS;
270 mode = SHOWJOBS_PIDS;
274 error("unknown option: -%c", optopt);
283 while ((id = *argv++) != NULL)
284 showjob(getjob(id), mode);
290 printjobcmd(struct job *jp)
295 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
304 showjob(struct job *jp, int mode)
310 int col, curr, i, jobno, prev, procno;
313 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
314 jobno = jp - jobtab + 1;
317 if ((j = getcurjob(NULL)) != NULL) {
318 curr = j - jobtab + 1;
319 if ((j = getcurjob(j)) != NULL)
320 prev = j - jobtab + 1;
323 ps = jp->ps + jp->nprocs - 1;
324 if (jp->state == 0) {
325 strcpy(statestr, "Running");
327 } else if (jp->state == JOBSTOPPED) {
328 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
330 if (WIFSTOPPED(ps->status))
331 i = WSTOPSIG(ps->status);
334 if (i > 0 && i < sys_nsig && sys_siglist[i])
335 strcpy(statestr, sys_siglist[i]);
337 strcpy(statestr, "Suspended");
339 } else if (WIFEXITED(ps->status)) {
340 if (WEXITSTATUS(ps->status) == 0)
341 strcpy(statestr, "Done");
343 fmtstr(statestr, 64, "Done(%d)",
344 WEXITSTATUS(ps->status));
346 i = WTERMSIG(ps->status);
347 if (i > 0 && i < sys_nsig && sys_siglist[i])
348 strcpy(statestr, sys_siglist[i]);
350 fmtstr(statestr, 64, "Signal %d", i);
351 if (WCOREDUMP(ps->status))
352 strcat(statestr, " (core dumped)");
355 for (ps = jp->ps ; ; ps++) { /* for each process */
356 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
357 out1fmt("%d\n", (int)ps->pid);
360 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
362 if (jobno == curr && ps == jp->ps)
364 else if (jobno == prev && ps == jp->ps)
369 fmtstr(s, 64, "[%d] %c ", jobno, c);
371 fmtstr(s, 64, " %c ", c);
374 if (mode == SHOWJOBS_VERBOSE) {
375 fmtstr(s, 64, "%d ", (int)ps->pid);
381 col += strlen(statestr);
387 if (mode == SHOWJOBS_VERBOSE) {
392 skip: if (--procno <= 0)
398 * Print a list of jobs. If "change" is nonzero, only print jobs whose
399 * statuses have changed since the last call to showjobs.
401 * If the shell is interrupted in the process of creating a job, the
402 * result may be a job structure containing zero processes. Such structures
403 * will be freed here.
407 showjobs(int change, int mode)
412 TRACE(("showjobs(%d) called\n", change));
414 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
417 if (jp->nprocs == 0) {
421 if (change && ! jp->changed)
425 /* Hack: discard jobs for which $! has not been referenced
426 * in interactive mode when they terminate.
428 if (jp->state == JOBDONE && !jp->remembered &&
429 (iflag || jp != bgjob)) {
437 * Mark a job structure as unused.
441 freejob(struct job *jp)
449 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
450 if (ps->cmd != nullstr)
453 if (jp->ps != &jp->ps0)
465 waitcmd(int argc, char **argv)
472 job = getjob(argv[1]);
478 * Loop until a process is terminated or stopped, or a SIGINT is
486 status = job->ps[job->nprocs - 1].status;
487 if (WIFEXITED(status))
488 retval = WEXITSTATUS(status);
490 else if (WIFSTOPPED(status))
491 retval = WSTOPSIG(status) + 128;
494 retval = WTERMSIG(status) + 128;
495 if (! iflag || ! job->changed)
506 for (jp = jobtab ; jp < jobtab + njobs; jp++)
507 if (jp->used && jp->state == JOBDONE) {
508 if (! iflag || ! jp->changed)
516 for (jp = jobtab ; ; jp++) {
517 if (jp >= jobtab + njobs) { /* no running procs */
521 if (jp->used && jp->state == 0)
525 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
534 jobidcmd(int argc __unused, char **argv)
539 jp = getjob(argv[1]);
540 for (i = 0 ; i < jp->nprocs ; ) {
541 out1fmt("%d", (int)jp->ps[i].pid);
542 out1c(++i < jp->nprocs? ' ' : '\n');
550 * Convert a job name to a job structure.
557 struct job *found, *jp;
563 currentjob: if ((jp = getcurjob(NULL)) == NULL)
564 error("No current job");
567 error("No current job");
569 } else if (name[0] == '%') {
570 if (is_digit(name[1])) {
571 jobno = number(name + 1);
572 if (jobno > 0 && jobno <= njobs
573 && jobtab[jobno - 1].used != 0)
574 return &jobtab[jobno - 1];
576 } else if (name[1] == '%' && name[2] == '\0') {
578 } else if (name[1] == '+' && name[2] == '\0') {
580 } else if (name[1] == '-' && name[2] == '\0') {
581 if ((jp = getcurjob(NULL)) == NULL ||
582 (jp = getcurjob(jp)) == NULL)
583 error("No previous job");
586 } else if (name[1] == '?') {
588 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
589 if (jp->used && jp->nprocs > 0
590 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
592 error("%s: ambiguous", name);
600 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
601 if (jp->used && jp->nprocs > 0
602 && prefix(name + 1, jp->ps[0].cmd)) {
604 error("%s: ambiguous", name);
611 } else if (is_number(name)) {
612 pid = (pid_t)number(name);
613 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
614 if (jp->used && jp->nprocs > 0
615 && jp->ps[jp->nprocs - 1].pid == pid)
619 error("No such job: %s", name);
626 getjobpgrp(char *name)
631 return -jp->ps[0].pid;
635 * Return a new job structure,
639 makejob(union node *node __unused, int nprocs)
644 for (i = njobs, jp = jobtab ; ; jp++) {
648 jobtab = ckmalloc(4 * sizeof jobtab[0]);
653 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
654 memcpy(jp, jobtab, njobs * sizeof jp[0]);
656 /* Relocate `next' pointers and list head */
658 jobmru = &jp[jobmru - jobtab];
659 for (i = 0; i < njobs; i++)
660 if (jp[i].next != NULL)
661 jp[i].next = &jp[jp[i].next -
665 bgjob = &jp[bgjob - jobtab];
666 /* Relocate `ps' pointers */
667 for (i = 0; i < njobs; i++)
668 if (jp[i].ps == &jobtab[i].ps0)
669 jp[i].ps = &jp[i].ps0;
674 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
693 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
698 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
705 setcurjob(struct job *cj)
707 struct job *jp, *prev;
709 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
712 prev->next = jp->next;
725 deljob(struct job *j)
727 struct job *jp, *prev;
729 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
732 prev->next = jp->next;
741 * Return the most recently used job that isn't `nj', and preferably one
745 getcurjob(struct job *nj)
749 /* Try to find a stopped one.. */
750 for (jp = jobmru; jp != NULL; jp = jp->next)
751 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
753 /* Otherwise the most recently used job that isn't `nj' */
754 for (jp = jobmru; jp != NULL; jp = jp->next)
755 if (jp->used && jp != nj)
764 * Fork of a subshell. If we are doing job control, give the subshell its
765 * own process group. Jp is a job structure that the job is to be added to.
766 * N is the command that will be evaluated by the child. Both jp and n may
767 * be NULL. The mode parameter can be one of the following:
768 * FORK_FG - Fork off a foreground process.
769 * FORK_BG - Fork off a background process.
770 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
771 * process group even if job control is on.
773 * When job control is turned off, background processes have their standard
774 * input redirected to /dev/null (except for the second and later processes
779 forkshell(struct job *jp, union node *n, int mode)
784 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
787 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
792 TRACE(("Fork failed, errno=%d\n", errno));
794 error("Cannot fork: %s", strerror(errno));
801 TRACE(("Child shell %d\n", (int)getpid()));
804 handler = &main_handler;
810 jobctl = 0; /* do job control only in root shell */
811 if (wasroot && mode != FORK_NOJOB && mflag) {
812 if (jp == NULL || jp->nprocs == 0)
815 pgrp = jp->ps[0].pid;
816 if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
817 /*** this causes superfluous TIOCSPGRPS ***/
818 if (tcsetpgrp(ttyfd, pgrp) < 0)
819 error("tcsetpgrp failed, errno=%d", errno);
823 } else if (mode == FORK_BG) {
826 if ((jp == NULL || jp->nprocs == 0) &&
827 ! fd0_redirected_p ()) {
829 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
830 error("cannot open %s: %s",
831 _PATH_DEVNULL, strerror(errno));
835 if (mode == FORK_BG) {
838 if ((jp == NULL || jp->nprocs == 0) &&
839 ! fd0_redirected_p ()) {
841 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
842 error("cannot open %s: %s",
843 _PATH_DEVNULL, strerror(errno));
848 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
852 if (wasroot && iflag) {
859 if (rootshell && mode != FORK_NOJOB && mflag) {
860 if (jp == NULL || jp->nprocs == 0)
863 pgrp = jp->ps[0].pid;
866 if (mode == FORK_BG) {
867 if (bgjob != NULL && bgjob->state == JOBDONE &&
868 !bgjob->remembered && !iflag)
870 backgndpid = pid; /* set $! */
874 struct procstat *ps = &jp->ps[jp->nprocs++];
878 if (iflag && rootshell && n)
879 ps->cmd = commandtext(n);
880 jp->foreground = mode == FORK_FG;
886 TRACE(("In parent shell: child = %d\n", (int)pid));
892 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
895 struct jmploc jmploc;
896 struct jmploc *savehandler;
898 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
902 savehandler = handler;
905 TRACE(("Vfork failed, errno=%d\n", errno));
907 error("Cannot fork: %s", strerror(errno));
910 TRACE(("Child shell %d\n", (int)getpid()));
911 if (setjmp(jmploc.loc))
912 _exit(exception == EXEXEC ? exerrno : 2);
921 shellexec(argv, envp, path, idx);
923 handler = savehandler;
925 struct procstat *ps = &jp->ps[jp->nprocs++];
935 TRACE(("In parent shell: child = %d\n", (int)pid));
941 * Wait for job to finish.
943 * Under job control we have the problem that while a child process is
944 * running interrupts generated by the user are sent to the child but not
945 * to the shell. This means that an infinite loop started by an inter-
946 * active user may be hard to kill. With job control turned off, an
947 * interactive user may place an interactive program inside a loop. If
948 * the interactive program catches interrupts, the user doesn't want
949 * these interrupts to also abort the loop. The approach we take here
950 * is to have the shell ignore interrupt signals while waiting for a
951 * foreground process to terminate, and then send itself an interrupt
952 * signal if the child process was terminated by an interrupt signal.
953 * Unfortunately, some programs want to do a bit of cleanup and then
954 * exit on interrupt; unless these processes terminate themselves by
955 * sending a signal to themselves (instead of calling exit) they will
956 * confuse this approach.
960 waitforjob(struct job *jp, int *origstatus)
963 pid_t mypgrp = getpgrp();
964 int propagate_int = jp->jobctl && jp->foreground;
970 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
971 while (jp->state == 0)
972 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG : 0), jp) == -1)
976 if (tcsetpgrp(ttyfd, mypgrp) < 0)
977 error("tcsetpgrp failed, errno=%d\n", errno);
979 if (jp->state == JOBSTOPPED)
982 status = jp->ps[jp->nprocs - 1].status;
983 if (origstatus != NULL)
984 *origstatus = status;
985 /* convert to 8 bits */
986 if (WIFEXITED(status))
987 st = WEXITSTATUS(status);
989 else if (WIFSTOPPED(status))
990 st = WSTOPSIG(status) + 128;
993 st = WTERMSIG(status) + 128;
994 if (! JOBS || jp->state == JOBDONE)
997 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1001 else if (rootshell && iflag && propagate_int &&
1002 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1003 kill(getpid(), SIGINT);
1011 dummy_handler(int sig)
1016 * Wait for a process to terminate.
1020 dowait(int mode, struct job *job)
1022 struct sigaction sa, osa;
1023 sigset_t mask, omask;
1026 struct procstat *sp;
1028 struct job *thisjob;
1034 int restore_sigchld;
1036 TRACE(("dowait(%d) called\n", block));
1037 restore_sigchld = 0;
1038 if ((mode & DOWAIT_SIG) != 0) {
1040 sigprocmask(SIG_BLOCK, &mask, &omask);
1042 if (!issigchldtrapped()) {
1043 restore_sigchld = 1;
1044 sa.sa_handler = dummy_handler;
1046 sigemptyset(&sa.sa_mask);
1047 sigaction(SIGCHLD, &sa, &osa);
1053 wflags = WUNTRACED | WCONTINUED;
1057 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1059 pid = wait3(&status, wflags, (struct rusage *)NULL);
1060 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1061 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1067 } while (pid == -1 && errno == EINTR && breakwaitcmd == 0);
1068 if (pid == -1 && errno == ECHILD && job != NULL)
1069 job->state = JOBDONE;
1070 if ((mode & DOWAIT_SIG) != 0) {
1071 if (restore_sigchld)
1072 sigaction(SIGCHLD, &osa, NULL);
1073 sigprocmask(SIG_SETMASK, &omask, NULL);
1076 if (breakwaitcmd != 0) {
1085 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1086 if (jp->used && jp->nprocs > 0) {
1089 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1092 if (sp->pid == pid) {
1093 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1094 (int)pid, sp->status,
1096 if (WIFCONTINUED(status)) {
1100 sp->status = status;
1103 if (sp->status == -1)
1105 else if (WIFSTOPPED(sp->status))
1108 if (stopped) { /* stopped or done */
1109 int state = done? JOBDONE : JOBSTOPPED;
1110 if (jp->state != state) {
1111 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1114 if (done && !jp->remembered &&
1115 !iflag && jp != bgjob)
1127 if (!thisjob || thisjob->state == 0)
1129 else if ((!rootshell || !iflag || thisjob == job) &&
1130 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1133 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1134 if (WIFSIGNALED(sp->status)) {
1135 sig = WTERMSIG(sp->status);
1136 coredump = WCOREDUMP(sp->status);
1138 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1139 if (sig < sys_nsig && sys_siglist[sig])
1140 out2str(sys_siglist[sig]);
1142 outfmt(out2, "Signal %d", sig);
1144 out2str(" (core dumped)");
1149 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1150 thisjob->changed = 1;
1158 * return 1 if there are stopped jobs, otherwise 0
1160 int job_warning = 0;
1169 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1172 if (jp->state == JOBSTOPPED) {
1173 out2fmt_flush("You have stopped jobs.\n");
1186 while (njobs > 0 && dowait(0, NULL) > 0)
1194 return backgndpid != -1;
1201 if (bgjob != NULL && !forcelocal)
1202 bgjob->remembered = 1;
1207 * Return a string identifying a command (to be printed by the
1211 static char *cmdnextc;
1212 static int cmdnleft;
1213 #define MAXCMDTEXT 200
1216 commandtext(union node *n)
1220 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1221 cmdnleft = MAXCMDTEXT - 4;
1229 cmdtxt(union node *n)
1232 struct nodelist *lp;
1241 cmdtxt(n->nbinary.ch1);
1243 cmdtxt(n->nbinary.ch2);
1246 cmdtxt(n->nbinary.ch1);
1248 cmdtxt(n->nbinary.ch2);
1251 cmdtxt(n->nbinary.ch1);
1253 cmdtxt(n->nbinary.ch2);
1256 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1264 cmdtxt(n->nredir.n);
1269 cmdtxt(n->nredir.n);
1273 cmdtxt(n->nif.test);
1275 cmdtxt(n->nif.ifpart);
1284 cmdtxt(n->nbinary.ch1);
1286 cmdtxt(n->nbinary.ch2);
1291 cmdputs(n->nfor.var);
1296 cmdputs(n->ncase.expr->narg.text);
1300 cmdputs(n->narg.text);
1304 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1309 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1315 cmdputs(n->narg.text);
1318 p = ">"; i = 1; goto redir;
1320 p = ">>"; i = 1; goto redir;
1322 p = ">&"; i = 1; goto redir;
1324 p = ">|"; i = 1; goto redir;
1326 p = "<"; i = 0; goto redir;
1328 p = "<>"; i = 0; goto redir;
1330 p = "<&"; i = 0; goto redir;
1332 if (n->nfile.fd != i) {
1333 s[0] = n->nfile.fd + '0';
1338 if (n->type == NTOFD || n->type == NFROMFD) {
1339 if (n->ndup.dupfd >= 0)
1340 s[0] = n->ndup.dupfd + '0';
1346 cmdtxt(n->nfile.fname);
1362 cmdputs(const char *s)
1373 while ((c = *p++) != '\0') {
1376 else if (c == CTLVAR) {
1381 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1383 } else if (c == '=' && subtype != 0) {
1384 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1389 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1390 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1394 } else if (c == CTLENDVAR) {
1396 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1406 } else if (c == CTLARI) {
1414 } else if (c == CTLENDARI) {
1415 if (--cmdnleft > 0) {
1419 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1420 cmdnleft++; /* ignore */
1423 if (--cmdnleft <= 0) {