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 * 3. 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
32 * @(#)jobs.c 8.5 (Berkeley) 5/4/95
33 * $FreeBSD: head/bin/sh/jobs.c 253658 2013-07-25 19:48:15Z jilles $
36 #include <sys/ioctl.h>
37 #include <sys/param.h>
38 #include <sys/resource.h>
52 #undef CEOF /* syntax.h redefines this */
73 static struct job *jobtab; /* array of jobs */
74 static int njobs; /* size of array */
75 static pid_t backgndpid = -1; /* pid of last background process */
76 static struct job *bgjob = NULL; /* last background process */
78 static struct job *jobmru; /* most recently used job list */
79 static pid_t initialpgrp; /* pgrp of shell on invocation */
81 int in_waitcmd = 0; /* are we in waitcmd()? */
82 volatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */
83 static int ttyfd = -1;
85 /* mode flags for dowait */
86 #define DOWAIT_BLOCK 0x1 /* wait until a child exits */
87 #define DOWAIT_SIG 0x2 /* if DOWAIT_BLOCK, abort on signals */
90 static void restartjob(struct job *);
92 static void freejob(struct job *);
93 static int waitcmdloop(struct job *);
94 static struct job *getjob_nonotfound(char *);
95 static struct job *getjob(char *);
96 static pid_t dowait(int, struct job *);
97 static void checkzombies(void);
98 static void cmdtxt(union node *);
99 static void cmdputs(const char *);
101 static void setcurjob(struct job *);
102 static void deljob(struct job *);
103 static struct job *getcurjob(struct job *);
105 static void printjobcmd(struct job *);
106 static void showjob(struct job *, int);
110 * Turn job control on and off.
121 if (on == jobctl || rootshell == 0)
127 if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) {
129 if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
132 while (i <= 2 && !isatty(i))
135 #ifndef F_DUPFD_CLOEXEC
136 (ttyfd = fcntl(i, F_DUPFD, 10)) < 0)
138 (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0)
144 * Keep our TTY file descriptor out of the way of
145 * the user's redirections.
147 #ifndef F_DUPFD_CLOEXEC
148 if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) {
150 if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
159 #if !defined(O_CLOEXEC) || !defined(F_DUPFD_CLOEXEC)
160 if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) {
166 do { /* while we are in the background */
167 initialpgrp = tcgetpgrp(ttyfd);
168 if (initialpgrp < 0) {
169 out: out2fmt_flush("sh: can't access tty; job control turned off\n");
173 if (initialpgrp != getpgrp()) {
182 tcsetpgrp(ttyfd, rootpid);
183 } else { /* turning job control off */
184 setpgid(0, initialpgrp);
185 tcsetpgrp(ttyfd, initialpgrp);
199 fgcmd(int argc __unused, char **argv)
205 jp = getjob(argv[1]);
207 error("job not created under job control");
210 pgrp = jp->ps[0].pid;
211 tcsetpgrp(ttyfd, pgrp);
215 status = waitforjob(jp, NULL);
222 bgcmd(int argc, char **argv)
227 jp = getjob(*++argv);
229 error("job not created under job control");
230 if (jp->state == JOBDONE)
234 out1fmt("[%td] ", jp - jobtab + 1);
236 } while (--argc > 1);
242 restartjob(struct job *jp)
247 if (jp->state == JOBDONE)
251 kill(-jp->ps[0].pid, SIGCONT);
252 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
253 if (WIFSTOPPED(ps->status)) {
264 jobscmd(int argc __unused, char *argv[] __unused)
269 mode = SHOWJOBS_DEFAULT;
270 while ((ch = nextopt("lps")) != '\0') {
273 mode = SHOWJOBS_VERBOSE;
276 mode = SHOWJOBS_PGIDS;
279 mode = SHOWJOBS_PIDS;
287 while ((id = *argptr++) != NULL)
288 showjob(getjob(id), mode);
294 printjobcmd(struct job *jp)
299 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
308 showjob(struct job *jp, int mode)
315 int col, curr, i, jobno, prev, procno;
318 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
319 jobno = jp - jobtab + 1;
322 if ((j = getcurjob(NULL)) != NULL) {
323 curr = j - jobtab + 1;
324 if ((j = getcurjob(j)) != NULL)
325 prev = j - jobtab + 1;
328 ps = jp->ps + jp->nprocs - 1;
329 if (jp->state == 0) {
330 strcpy(statestr, "Running");
332 } else if (jp->state == JOBSTOPPED) {
333 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
335 if (WIFSTOPPED(ps->status))
336 i = WSTOPSIG(ps->status);
339 sigstr = strsignal(i);
341 strcpy(statestr, sigstr);
343 strcpy(statestr, "Suspended");
345 } else if (WIFEXITED(ps->status)) {
346 if (WEXITSTATUS(ps->status) == 0)
347 strcpy(statestr, "Done");
349 fmtstr(statestr, 64, "Done(%d)",
350 WEXITSTATUS(ps->status));
352 i = WTERMSIG(ps->status);
353 sigstr = strsignal(i);
355 strcpy(statestr, sigstr);
357 strcpy(statestr, "Unknown signal");
358 if (WCOREDUMP(ps->status))
359 strcat(statestr, " (core dumped)");
362 for (ps = jp->ps ; ; ps++) { /* for each process */
363 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
364 out1fmt("%d\n", (int)ps->pid);
367 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
369 if (jobno == curr && ps == jp->ps)
371 else if (jobno == prev && ps == jp->ps)
376 fmtstr(s, 64, "[%d] %c ", jobno, c);
378 fmtstr(s, 64, " %c ", c);
381 if (mode == SHOWJOBS_VERBOSE) {
382 fmtstr(s, 64, "%d ", (int)ps->pid);
388 col += strlen(statestr);
394 if (mode == SHOWJOBS_VERBOSE) {
399 skip: if (--procno <= 0)
405 * Print a list of jobs. If "change" is nonzero, only print jobs whose
406 * statuses have changed since the last call to showjobs.
408 * If the shell is interrupted in the process of creating a job, the
409 * result may be a job structure containing zero processes. Such structures
410 * will be freed here.
414 showjobs(int change, int mode)
419 TRACE(("showjobs(%d) called\n", change));
421 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
424 if (jp->nprocs == 0) {
428 if (change && ! jp->changed)
431 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
433 /* Hack: discard jobs for which $! has not been
434 * referenced in interactive mode when they terminate.
436 if (jp->state == JOBDONE && !jp->remembered &&
437 (iflag || jp != bgjob)) {
446 * Mark a job structure as unused.
450 freejob(struct job *jp)
458 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
459 if (ps->cmd != nullstr)
462 if (jp->ps != &jp->ps0)
474 waitcmd(int argc __unused, char **argv __unused)
481 return (waitcmdloop(NULL));
484 job = getjob_nonotfound(*argptr);
488 retval = waitcmdloop(job);
490 } while (*argptr != NULL);
496 waitcmdloop(struct job *job)
502 * Loop until a process is terminated or stopped, or a SIGINT is
510 status = job->ps[job->nprocs - 1].status;
511 if (WIFEXITED(status))
512 retval = WEXITSTATUS(status);
514 else if (WIFSTOPPED(status))
515 retval = WSTOPSIG(status) + 128;
518 retval = WTERMSIG(status) + 128;
519 if (! iflag || ! job->changed)
530 for (jp = jobtab ; jp < jobtab + njobs; jp++)
531 if (jp->used && jp->state == JOBDONE) {
532 if (! iflag || ! jp->changed)
540 for (jp = jobtab ; ; jp++) {
541 if (jp >= jobtab + njobs) { /* no running procs */
545 if (jp->used && jp->state == 0)
549 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, NULL) != -1);
552 return pendingsig + 128;
558 jobidcmd(int argc __unused, char **argv)
563 jp = getjob(argv[1]);
564 for (i = 0 ; i < jp->nprocs ; ) {
565 out1fmt("%d", (int)jp->ps[i].pid);
566 out1c(++i < jp->nprocs? ' ' : '\n');
574 * Convert a job name to a job structure.
578 getjob_nonotfound(char *name)
581 struct job *found, *jp;
587 currentjob: if ((jp = getcurjob(NULL)) == NULL)
588 error("No current job");
591 error("No current job");
593 } else if (name[0] == '%') {
594 if (is_digit(name[1])) {
595 jobno = number(name + 1);
596 if (jobno > 0 && jobno <= njobs
597 && jobtab[jobno - 1].used != 0)
598 return &jobtab[jobno - 1];
600 } else if (name[1] == '%' && name[2] == '\0') {
602 } else if (name[1] == '+' && name[2] == '\0') {
604 } else if (name[1] == '-' && name[2] == '\0') {
605 if ((jp = getcurjob(NULL)) == NULL ||
606 (jp = getcurjob(jp)) == NULL)
607 error("No previous job");
610 } else if (name[1] == '?') {
612 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
613 if (jp->used && jp->nprocs > 0
614 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
616 error("%s: ambiguous", name);
624 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
625 if (jp->used && jp->nprocs > 0
626 && prefix(name + 1, jp->ps[0].cmd)) {
628 error("%s: ambiguous", name);
635 } else if (is_number(name)) {
636 pid = (pid_t)number(name);
637 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
638 if (jp->used && jp->nprocs > 0
639 && jp->ps[jp->nprocs - 1].pid == pid)
652 jp = getjob_nonotfound(name);
654 error("No such job: %s", name);
660 getjobpgrp(char *name)
665 return -jp->ps[0].pid;
669 * Return a new job structure,
673 makejob(union node *node __unused, int nprocs)
678 for (i = njobs, jp = jobtab ; ; jp++) {
682 jobtab = ckmalloc(4 * sizeof jobtab[0]);
687 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
688 memcpy(jp, jobtab, njobs * sizeof jp[0]);
690 /* Relocate `next' pointers and list head */
692 jobmru = &jp[jobmru - jobtab];
693 for (i = 0; i < njobs; i++)
694 if (jp[i].next != NULL)
695 jp[i].next = &jp[jp[i].next -
699 bgjob = &jp[bgjob - jobtab];
700 /* Relocate `ps' pointers */
701 for (i = 0; i < njobs; i++)
702 if (jp[i].ps == &jobtab[i].ps0)
703 jp[i].ps = &jp[i].ps0;
708 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
728 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
733 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
740 setcurjob(struct job *cj)
742 struct job *jp, *prev;
744 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
747 prev->next = jp->next;
760 deljob(struct job *j)
762 struct job *jp, *prev;
764 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
767 prev->next = jp->next;
776 * Return the most recently used job that isn't `nj', and preferably one
780 getcurjob(struct job *nj)
784 /* Try to find a stopped one.. */
785 for (jp = jobmru; jp != NULL; jp = jp->next)
786 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
788 /* Otherwise the most recently used job that isn't `nj' */
789 for (jp = jobmru; jp != NULL; jp = jp->next)
790 if (jp->used && jp != nj)
799 * Fork of a subshell. If we are doing job control, give the subshell its
800 * own process group. Jp is a job structure that the job is to be added to.
801 * N is the command that will be evaluated by the child. Both jp and n may
802 * be NULL. The mode parameter can be one of the following:
803 * FORK_FG - Fork off a foreground process.
804 * FORK_BG - Fork off a background process.
805 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
806 * process group even if job control is on.
808 * When job control is turned off, background processes have their standard
809 * input redirected to /dev/null (except for the second and later processes
814 forkshell(struct job *jp, union node *n, int mode)
819 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
822 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
827 TRACE(("Fork failed, errno=%d\n", errno));
829 error("Cannot fork: %s", strerror(errno));
836 TRACE(("Child shell %d\n", (int)getpid()));
839 handler = &main_handler;
845 jobctl = 0; /* do job control only in root shell */
846 if (wasroot && mode != FORK_NOJOB && mflag) {
847 if (jp == NULL || jp->nprocs == 0)
850 pgrp = jp->ps[0].pid;
851 if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
852 /*** this causes superfluous TIOCSPGRPS ***/
853 if (tcsetpgrp(ttyfd, pgrp) < 0)
854 error("tcsetpgrp failed, errno=%d", errno);
858 } else if (mode == FORK_BG) {
861 if ((jp == NULL || jp->nprocs == 0) &&
862 ! fd0_redirected_p ()) {
864 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
865 error("cannot open %s: %s",
866 _PATH_DEVNULL, strerror(errno));
870 if (mode == FORK_BG) {
873 if ((jp == NULL || jp->nprocs == 0) &&
874 ! fd0_redirected_p ()) {
876 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
877 error("cannot open %s: %s",
878 _PATH_DEVNULL, strerror(errno));
883 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
887 if (wasroot && iflag) {
894 if (rootshell && mode != FORK_NOJOB && mflag) {
895 if (jp == NULL || jp->nprocs == 0)
898 pgrp = jp->ps[0].pid;
901 if (mode == FORK_BG) {
902 if (bgjob != NULL && bgjob->state == JOBDONE &&
903 !bgjob->remembered && !iflag)
905 backgndpid = pid; /* set $! */
909 struct procstat *ps = &jp->ps[jp->nprocs++];
913 if (iflag && rootshell && n)
914 ps->cmd = commandtext(n);
915 jp->foreground = mode == FORK_FG;
921 TRACE(("In parent shell: child = %d\n", (int)pid));
927 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
930 struct jmploc jmploc;
931 struct jmploc *savehandler;
933 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
937 savehandler = handler;
940 TRACE(("Vfork failed, errno=%d\n", errno));
942 error("Cannot fork: %s", strerror(errno));
945 TRACE(("Child shell %d\n", (int)getpid()));
946 if (setjmp(jmploc.loc))
947 _exit(exception == EXEXEC ? exerrno : 2);
956 shellexec(argv, envp, path, idx);
958 handler = savehandler;
960 struct procstat *ps = &jp->ps[jp->nprocs++];
970 TRACE(("In parent shell: child = %d\n", (int)pid));
976 * Wait for job to finish.
978 * Under job control we have the problem that while a child process is
979 * running interrupts generated by the user are sent to the child but not
980 * to the shell. This means that an infinite loop started by an inter-
981 * active user may be hard to kill. With job control turned off, an
982 * interactive user may place an interactive program inside a loop. If
983 * the interactive program catches interrupts, the user doesn't want
984 * these interrupts to also abort the loop. The approach we take here
985 * is to have the shell ignore interrupt signals while waiting for a
986 * foreground process to terminate, and then send itself an interrupt
987 * signal if the child process was terminated by an interrupt signal.
988 * Unfortunately, some programs want to do a bit of cleanup and then
989 * exit on interrupt; unless these processes terminate themselves by
990 * sending a signal to themselves (instead of calling exit) they will
991 * confuse this approach.
995 waitforjob(struct job *jp, int *origstatus)
998 pid_t mypgrp = getpgrp();
999 int propagate_int = jp->jobctl && jp->foreground;
1005 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1006 while (jp->state == 0)
1007 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG : 0), jp) == -1)
1011 if (tcsetpgrp(ttyfd, mypgrp) < 0)
1012 error("tcsetpgrp failed, errno=%d\n", errno);
1014 if (jp->state == JOBSTOPPED)
1017 status = jp->ps[jp->nprocs - 1].status;
1018 if (origstatus != NULL)
1019 *origstatus = status;
1020 /* convert to 8 bits */
1021 if (WIFEXITED(status))
1022 st = WEXITSTATUS(status);
1024 else if (WIFSTOPPED(status))
1025 st = WSTOPSIG(status) + 128;
1028 st = WTERMSIG(status) + 128;
1029 if (! JOBS || jp->state == JOBDONE)
1031 if (int_pending()) {
1032 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1036 else if (rootshell && iflag && propagate_int &&
1037 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1038 kill(getpid(), SIGINT);
1046 dummy_handler(int sig __unused)
1051 * Wait for a process to terminate.
1055 dowait(int mode, struct job *job)
1057 struct sigaction sa, osa;
1058 sigset_t mask, omask;
1061 struct procstat *sp;
1063 struct job *thisjob;
1070 int restore_sigchld;
1072 TRACE(("dowait(%d, %p) called\n", mode, job));
1073 restore_sigchld = 0;
1074 if ((mode & DOWAIT_SIG) != 0) {
1076 sigprocmask(SIG_BLOCK, &mask, &omask);
1078 if (!issigchldtrapped()) {
1079 restore_sigchld = 1;
1080 sa.sa_handler = dummy_handler;
1082 sigemptyset(&sa.sa_mask);
1083 sigaction(SIGCHLD, &sa, &osa);
1089 wflags = WUNTRACED | WCONTINUED;
1093 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1095 pid = wait3(&status, wflags, NULL);
1096 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1097 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1103 } while (pid == -1 && errno == EINTR && breakwaitcmd == 0);
1104 if (pid == -1 && errno == ECHILD && job != NULL)
1105 job->state = JOBDONE;
1106 if ((mode & DOWAIT_SIG) != 0) {
1107 if (restore_sigchld)
1108 sigaction(SIGCHLD, &osa, NULL);
1109 sigprocmask(SIG_SETMASK, &omask, NULL);
1112 if (breakwaitcmd != 0) {
1115 * Do not early terminate if the pid is positive, else the
1116 * job will not be properly recorded.
1125 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1126 if (jp->used && jp->nprocs > 0) {
1129 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1132 if (sp->pid == pid) {
1133 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1134 (int)pid, sp->status,
1136 if (WIFCONTINUED(status)) {
1140 sp->status = status;
1143 if (sp->status == -1)
1145 else if (WIFSTOPPED(sp->status))
1148 if (stopped) { /* stopped or done */
1149 int state = done? JOBDONE : JOBSTOPPED;
1150 if (jp->state != state) {
1151 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1154 if (done && !jp->remembered &&
1155 !iflag && jp != bgjob)
1167 if (!thisjob || thisjob->state == 0)
1169 else if ((!rootshell || !iflag || thisjob == job) &&
1170 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1173 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1174 if (WIFSIGNALED(sp->status)) {
1175 sig = WTERMSIG(sp->status);
1176 coredump = WCOREDUMP(sp->status);
1178 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1179 sigstr = strsignal(sig);
1183 out2str("Unknown signal");
1185 out2str(" (core dumped)");
1190 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1191 thisjob->changed = 1;
1199 * return 1 if there are stopped jobs, otherwise 0
1201 int job_warning = 0;
1210 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1213 if (jp->state == JOBSTOPPED) {
1214 out2fmt_flush("You have stopped jobs.\n");
1227 while (njobs > 0 && dowait(0, NULL) > 0)
1235 return backgndpid != -1;
1242 if (bgjob != NULL && !forcelocal)
1243 bgjob->remembered = 1;
1248 * Return a string identifying a command (to be printed by the
1252 static char *cmdnextc;
1253 static int cmdnleft;
1254 #define MAXCMDTEXT 200
1257 commandtext(union node *n)
1261 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1262 cmdnleft = MAXCMDTEXT - 4;
1270 cmdtxt(union node *n)
1273 struct nodelist *lp;
1282 cmdtxt(n->nbinary.ch1);
1284 cmdtxt(n->nbinary.ch2);
1287 cmdtxt(n->nbinary.ch1);
1289 cmdtxt(n->nbinary.ch2);
1292 cmdtxt(n->nbinary.ch1);
1294 cmdtxt(n->nbinary.ch2);
1297 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1305 cmdtxt(n->nredir.n);
1310 cmdtxt(n->nredir.n);
1314 cmdtxt(n->nif.test);
1316 cmdtxt(n->nif.ifpart);
1325 cmdtxt(n->nbinary.ch1);
1327 cmdtxt(n->nbinary.ch2);
1332 cmdputs(n->nfor.var);
1337 cmdputs(n->ncase.expr->narg.text);
1341 cmdputs(n->narg.text);
1346 cmdtxt(n->nnot.com);
1349 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1354 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1360 cmdputs(n->narg.text);
1363 p = ">"; i = 1; goto redir;
1365 p = ">>"; i = 1; goto redir;
1367 p = ">&"; i = 1; goto redir;
1369 p = ">|"; i = 1; goto redir;
1371 p = "<"; i = 0; goto redir;
1373 p = "<>"; i = 0; goto redir;
1375 p = "<&"; i = 0; goto redir;
1377 if (n->nfile.fd != i) {
1378 s[0] = n->nfile.fd + '0';
1383 if (n->type == NTOFD || n->type == NFROMFD) {
1384 if (n->ndup.dupfd >= 0)
1385 s[0] = n->ndup.dupfd + '0';
1391 cmdtxt(n->nfile.fname);
1407 cmdputs(const char *s)
1418 while ((c = *p++) != '\0') {
1421 else if (c == CTLVAR) {
1426 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1428 } else if (c == '=' && subtype != 0) {
1429 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1434 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1435 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1439 } else if (c == CTLENDVAR) {
1441 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1451 } else if (c == CTLARI) {
1459 } else if (c == CTLENDARI) {
1460 if (--cmdnleft > 0) {
1464 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1465 cmdnleft++; /* ignore */
1468 if (--cmdnleft <= 0) {