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(const char *);
99 static struct job *getjob(const char *);
100 pid_t killjob(const char *, int);
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.
135 out2fmt_flush("sh: can't access tty; job control turned off\n");
144 if (on == jobctl || rootshell == 0)
149 if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
151 while (i <= 2 && !isatty(i))
154 (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) {
161 * Keep our TTY file descriptor out of the way of
162 * the user's redirections.
164 if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
171 do { /* while we are in the background */
172 initialpgrp = tcgetpgrp(ttyfd);
173 if (initialpgrp < 0) {
177 if (initialpgrp != getpgrp()) {
191 tcsetpgrp(ttyfd, rootpid);
192 } else { /* turning job control off */
193 setpgid(0, initialpgrp);
195 tcsetpgrp(ttyfd, initialpgrp);
210 fgcmd(int argc __unused, char **argv __unused)
217 jp = getjob(*argptr);
219 error("job not created under job control");
222 pgrp = jp->ps[0].pid;
224 tcsetpgrp(ttyfd, pgrp);
228 status = waitforjob(jp, (int *)NULL);
235 bgcmd(int argc, char **argv)
241 jp = getjob(*argptr);
243 error("job not created under job control");
244 if (jp->state == JOBDONE)
248 out1fmt("[%td] ", jp - jobtab + 1);
250 } while (*argptr != NULL && *++argptr != NULL);
256 restartjob(struct job *jp)
261 if (jp->state == JOBDONE)
265 kill(-jp->ps[0].pid, SIGCONT);
266 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
267 if (WIFSTOPPED(ps->status)) {
278 jobscmd(int argc __unused, char *argv[] __unused)
283 mode = SHOWJOBS_DEFAULT;
284 while ((ch = nextopt("lps")) != '\0') {
287 mode = SHOWJOBS_VERBOSE;
290 mode = SHOWJOBS_PGIDS;
293 mode = SHOWJOBS_PIDS;
301 while ((id = *argptr++) != NULL)
302 showjob(getjob(id), mode);
308 printjobcmd(struct job *jp)
313 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
322 showjob(struct job *jp, int mode)
329 int col, curr, i, jobno, prev, procno;
332 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
333 jobno = jp - jobtab + 1;
336 if ((j = getcurjob(NULL)) != NULL) {
337 curr = j - jobtab + 1;
338 if ((j = getcurjob(j)) != NULL)
339 prev = j - jobtab + 1;
342 ps = jp->ps + jp->nprocs - 1;
343 if (jp->state == 0) {
344 strcpy(statestr, "Running");
346 } else if (jp->state == JOBSTOPPED) {
347 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
349 if (WIFSTOPPED(ps->status))
350 i = WSTOPSIG(ps->status);
353 sigstr = strsignal(i);
355 strcpy(statestr, sigstr);
357 strcpy(statestr, "Suspended");
359 } else if (WIFEXITED(ps->status)) {
360 if (WEXITSTATUS(ps->status) == 0)
361 strcpy(statestr, "Done");
363 fmtstr(statestr, 64, "Done(%d)",
364 WEXITSTATUS(ps->status));
366 i = WTERMSIG(ps->status);
367 sigstr = strsignal(i);
369 strcpy(statestr, sigstr);
371 strcpy(statestr, "Unknown signal");
372 if (WCOREDUMP(ps->status))
373 strcat(statestr, " (core dumped)");
376 for (ps = jp->ps ; ; ps++) { /* for each process */
377 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
378 out1fmt("%d\n", (int)ps->pid);
381 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
383 if (jobno == curr && ps == jp->ps)
385 else if (jobno == prev && ps == jp->ps)
390 fmtstr(s, 64, "[%d] %c ", jobno, c);
392 fmtstr(s, 64, " %c ", c);
395 if (mode == SHOWJOBS_VERBOSE) {
396 fmtstr(s, 64, "%d ", (int)ps->pid);
402 col += strlen(statestr);
408 if (mode == SHOWJOBS_VERBOSE) {
413 skip: if (--procno <= 0)
419 * Print a list of jobs. If "change" is nonzero, only print jobs whose
420 * statuses have changed since the last call to showjobs.
422 * If the shell is interrupted in the process of creating a job, the
423 * result may be a job structure containing zero processes. Such structures
424 * will be freed here.
428 showjobs(int change, int mode)
433 TRACE(("showjobs(%d) called\n", change));
435 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
438 if (jp->nprocs == 0) {
442 if (change && ! jp->changed)
445 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
447 /* Hack: discard jobs for which $! has not been
448 * referenced in interactive mode when they terminate.
450 if (jp->state == JOBDONE && !jp->remembered &&
451 (iflag || jp != bgjob)) {
460 * Mark a job structure as unused.
464 freejob(struct job *jp)
472 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
473 if (ps->cmd != nullstr)
476 if (jp->ps != &jp->ps0)
488 waitcmd(int argc __unused, char **argv __unused)
495 return (waitcmdloop(NULL));
498 job = getjob_nonotfound(*argptr);
502 retval = waitcmdloop(job);
504 } while (*argptr != NULL);
510 waitcmdloop(struct job *job)
512 int status, retval, sig;
516 * Loop until a process is terminated or stopped, or a SIGINT is
522 if (job->state == JOBDONE) {
523 status = job->ps[job->nprocs - 1].status;
524 if (WIFEXITED(status))
525 retval = WEXITSTATUS(status);
527 retval = WTERMSIG(status) + 128;
528 if (! iflag || ! job->changed)
538 for (jp = jobtab ; jp < jobtab + njobs; jp++)
539 if (jp->used && jp->state == JOBDONE) {
540 if (! iflag || ! jp->changed)
548 for (jp = jobtab ; ; jp++) {
549 if (jp >= jobtab + njobs) { /* no running procs */
552 if (jp->used && jp->state == 0)
556 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
558 sig = pendingsig_waitcmd;
559 pendingsig_waitcmd = 0;
566 jobidcmd(int argc __unused, char **argv __unused)
572 jp = getjob(*argptr);
573 for (i = 0 ; i < jp->nprocs ; ) {
574 out1fmt("%d", (int)jp->ps[i].pid);
575 out1c(++i < jp->nprocs? ' ' : '\n');
583 * Convert a job name to a job structure.
587 getjob_nonotfound(const char *name)
590 struct job *found, *jp;
597 currentjob: if ((jp = getcurjob(NULL)) == NULL)
598 error("No current job");
601 error("No current job");
603 } else if (name[0] == '%') {
604 if (is_digit(name[1])) {
605 jobno = number(name + 1);
606 if (jobno > 0 && jobno <= njobs
607 && jobtab[jobno - 1].used != 0)
608 return &jobtab[jobno - 1];
610 } else if (name[1] == '%' && name[2] == '\0') {
612 } else if (name[1] == '+' && name[2] == '\0') {
614 } else if (name[1] == '-' && name[2] == '\0') {
615 if ((jp = getcurjob(NULL)) == NULL ||
616 (jp = getcurjob(jp)) == NULL)
617 error("No previous job");
620 } else if (name[1] == '?') {
622 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
623 if (jp->used && jp->nprocs > 0
624 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
626 error("%s: ambiguous", name);
633 namelen = strlen(name);
635 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
636 if (jp->used && jp->nprocs > 0
637 && strncmp(jp->ps[0].cmd, name + 1,
640 error("%s: ambiguous", name);
647 } else if (is_number(name)) {
648 pid = (pid_t)number(name);
649 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
650 if (jp->used && jp->nprocs > 0
651 && jp->ps[jp->nprocs - 1].pid == pid)
660 getjob(const char *name)
664 jp = getjob_nonotfound(name);
666 error("No such job: %s", name);
672 killjob(const char *name, int sig)
678 if (jp->state == JOBDONE)
681 return kill(-jp->ps[0].pid, sig);
684 for (i = 0; i < jp->nprocs; i++)
685 if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
686 if (kill(jp->ps[i].pid, sig) == 0)
694 * Return a new job structure,
698 makejob(union node *node __unused, int nprocs)
703 for (i = njobs, jp = jobtab ; ; jp++) {
707 jobtab = ckmalloc(4 * sizeof jobtab[0]);
712 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
713 memcpy(jp, jobtab, njobs * sizeof jp[0]);
715 /* Relocate `next' pointers and list head */
717 jobmru = &jp[jobmru - jobtab];
718 for (i = 0; i < njobs; i++)
719 if (jp[i].next != NULL)
720 jp[i].next = &jp[jp[i].next -
724 bgjob = &jp[bgjob - jobtab];
725 /* Relocate `ps' pointers */
726 for (i = 0; i < njobs; i++)
727 if (jp[i].ps == &jobtab[i].ps0)
728 jp[i].ps = &jp[i].ps0;
733 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
753 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
758 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
765 setcurjob(struct job *cj)
767 struct job *jp, *prev;
769 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
772 prev->next = jp->next;
785 deljob(struct job *j)
787 struct job *jp, *prev;
789 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
792 prev->next = jp->next;
801 * Return the most recently used job that isn't `nj', and preferably one
805 getcurjob(struct job *nj)
809 /* Try to find a stopped one.. */
810 for (jp = jobmru; jp != NULL; jp = jp->next)
811 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
813 /* Otherwise the most recently used job that isn't `nj' */
814 for (jp = jobmru; jp != NULL; jp = jp->next)
815 if (jp->used && jp != nj)
824 * Fork of a subshell. If we are doing job control, give the subshell its
825 * own process group. Jp is a job structure that the job is to be added to.
826 * N is the command that will be evaluated by the child. Both jp and n may
827 * be NULL. The mode parameter can be one of the following:
828 * FORK_FG - Fork off a foreground process.
829 * FORK_BG - Fork off a background process.
830 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
831 * process group even if job control is on.
833 * When job control is turned off, background processes have their standard
834 * input redirected to /dev/null (except for the second and later processes
839 forkshell(struct job *jp, union node *n, int mode)
844 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
847 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
852 TRACE(("Fork failed, errno=%d\n", errno));
854 error("Cannot fork: %s", strerror(errno));
861 TRACE(("Child shell %d\n", (int)getpid()));
864 handler = &main_handler;
870 jobctl = 0; /* do job control only in root shell */
871 if (wasroot && mode != FORK_NOJOB && mflag) {
872 if (jp == NULL || jp->nprocs == 0)
875 pgrp = jp->ps[0].pid;
876 if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
878 /*** this causes superfluous TIOCSPGRPS ***/
879 if (tcsetpgrp(ttyfd, pgrp) < 0)
880 error("tcsetpgrp failed, errno=%d", errno);
884 } else if (mode == FORK_BG) {
887 if ((jp == NULL || jp->nprocs == 0) &&
888 ! fd0_redirected_p ()) {
890 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
891 error("cannot open %s: %s",
892 _PATH_DEVNULL, strerror(errno));
896 if (mode == FORK_BG) {
899 if ((jp == NULL || jp->nprocs == 0) &&
900 ! fd0_redirected_p ()) {
902 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
903 error("cannot open %s: %s",
904 _PATH_DEVNULL, strerror(errno));
909 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
913 if (wasroot && iflag) {
920 if (rootshell && mode != FORK_NOJOB && mflag) {
921 if (jp == NULL || jp->nprocs == 0)
924 pgrp = jp->ps[0].pid;
927 if (mode == FORK_BG) {
928 if (bgjob != NULL && bgjob->state == JOBDONE &&
929 !bgjob->remembered && !iflag)
931 backgndpid = pid; /* set $! */
935 struct procstat *ps = &jp->ps[jp->nprocs++];
939 if (iflag && rootshell && n)
940 ps->cmd = commandtext(n);
941 jp->foreground = mode == FORK_FG;
947 TRACE(("In parent shell: child = %d\n", (int)pid));
953 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
956 struct jmploc jmploc;
957 struct jmploc *savehandler;
959 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
963 savehandler = handler;
966 TRACE(("Vfork failed, errno=%d\n", errno));
968 error("Cannot fork: %s", strerror(errno));
971 TRACE(("Child shell %d\n", (int)getpid()));
972 if (setjmp(jmploc.loc))
973 _exit(exception == EXEXEC ? exerrno : 2);
982 shellexec(argv, envp, path, idx);
984 handler = savehandler;
986 struct procstat *ps = &jp->ps[jp->nprocs++];
996 TRACE(("In parent shell: child = %d\n", (int)pid));
1002 * Wait for job to finish.
1004 * Under job control we have the problem that while a child process is
1005 * running interrupts generated by the user are sent to the child but not
1006 * to the shell. This means that an infinite loop started by an inter-
1007 * active user may be hard to kill. With job control turned off, an
1008 * interactive user may place an interactive program inside a loop. If
1009 * the interactive program catches interrupts, the user doesn't want
1010 * these interrupts to also abort the loop. The approach we take here
1011 * is to have the shell ignore interrupt signals while waiting for a
1012 * foreground process to terminate, and then send itself an interrupt
1013 * signal if the child process was terminated by an interrupt signal.
1014 * Unfortunately, some programs want to do a bit of cleanup and then
1015 * exit on interrupt; unless these processes terminate themselves by
1016 * sending a signal to themselves (instead of calling exit) they will
1017 * confuse this approach.
1021 waitforjob(struct job *jp, int *origstatus)
1024 int propagate_int = jp->jobctl && jp->foreground;
1030 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1031 while (jp->state == 0)
1032 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
1033 DOWAIT_SIG_ANY : 0), jp) == -1)
1037 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
1038 error("tcsetpgrp failed, errno=%d\n", errno);
1040 if (jp->state == JOBSTOPPED)
1043 status = jp->ps[jp->nprocs - 1].status;
1044 if (origstatus != NULL)
1045 *origstatus = status;
1046 /* convert to 8 bits */
1047 if (WIFEXITED(status))
1048 st = WEXITSTATUS(status);
1050 else if (WIFSTOPPED(status))
1051 st = WSTOPSIG(status) + 128;
1054 st = WTERMSIG(status) + 128;
1055 if (! JOBS || jp->state == JOBDONE)
1057 if (int_pending()) {
1058 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1062 else if (rootshell && iflag && propagate_int &&
1063 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1064 kill(getpid(), SIGINT);
1072 dummy_handler(int sig __unused)
1077 * Wait for a process to terminate.
1081 dowait(int mode, struct job *job)
1083 struct sigaction sa, osa;
1084 sigset_t mask, omask;
1087 struct procstat *sp;
1089 struct job *thisjob;
1096 int restore_sigchld;
1098 TRACE(("dowait(%d, %p) called\n", mode, job));
1099 restore_sigchld = 0;
1100 if ((mode & DOWAIT_SIG) != 0) {
1102 sigprocmask(SIG_BLOCK, &mask, &omask);
1104 if (!issigchldtrapped()) {
1105 restore_sigchld = 1;
1106 sa.sa_handler = dummy_handler;
1108 sigemptyset(&sa.sa_mask);
1109 sigaction(SIGCHLD, &sa, &osa);
1115 wflags = WUNTRACED | WCONTINUED;
1119 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1121 pid = wait3(&status, wflags, (struct rusage *)NULL);
1122 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1123 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1125 if (((mode & DOWAIT_SIG_ANY) != 0 ?
1126 pendingsig : pendingsig_waitcmd) != 0) {
1134 } while (pid == -1 && errno == EINTR);
1135 if (pid == -1 && errno == ECHILD && job != NULL)
1136 job->state = JOBDONE;
1137 if ((mode & DOWAIT_SIG) != 0) {
1138 if (restore_sigchld)
1139 sigaction(SIGCHLD, &osa, NULL);
1140 sigprocmask(SIG_SETMASK, &omask, NULL);
1147 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1148 if (jp->used && jp->nprocs > 0) {
1151 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1154 if (sp->pid == pid && (sp->status == -1 ||
1155 WIFSTOPPED(sp->status))) {
1156 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1157 (int)pid, sp->status,
1159 if (WIFCONTINUED(status)) {
1163 sp->status = status;
1166 if (sp->status == -1)
1168 else if (WIFSTOPPED(sp->status))
1171 if (stopped) { /* stopped or done */
1172 int state = done? JOBDONE : JOBSTOPPED;
1173 if (jp->state != state) {
1174 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1177 if (done && !jp->remembered &&
1178 !iflag && jp != bgjob)
1190 if (!thisjob || thisjob->state == 0)
1192 else if ((!rootshell || !iflag || thisjob == job) &&
1193 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1196 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1197 if (WIFSIGNALED(sp->status)) {
1198 sig = WTERMSIG(sp->status);
1199 coredump = WCOREDUMP(sp->status);
1201 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1202 sigstr = strsignal(sig);
1206 out2str("Unknown signal");
1208 out2str(" (core dumped)");
1213 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1214 thisjob->changed = 1;
1222 * return 1 if there are stopped jobs, otherwise 0
1224 int job_warning = 0;
1233 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1236 if (jp->state == JOBSTOPPED) {
1237 out2fmt_flush("You have stopped jobs.\n");
1250 while (njobs > 0 && dowait(0, NULL) > 0)
1258 return backgndpid != -1;
1265 if (bgjob != NULL && !forcelocal)
1266 bgjob->remembered = 1;
1271 * Return a string identifying a command (to be printed by the
1275 static char *cmdnextc;
1276 static int cmdnleft;
1277 #define MAXCMDTEXT 200
1280 commandtext(union node *n)
1284 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1285 cmdnleft = MAXCMDTEXT - 4;
1293 cmdtxt(union node *n)
1296 struct nodelist *lp;
1305 cmdtxt(n->nbinary.ch1);
1307 cmdtxt(n->nbinary.ch2);
1310 cmdtxt(n->nbinary.ch1);
1312 cmdtxt(n->nbinary.ch2);
1315 cmdtxt(n->nbinary.ch1);
1317 cmdtxt(n->nbinary.ch2);
1320 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1328 cmdtxt(n->nredir.n);
1333 cmdtxt(n->nredir.n);
1337 cmdtxt(n->nif.test);
1339 cmdtxt(n->nif.ifpart);
1348 cmdtxt(n->nbinary.ch1);
1350 cmdtxt(n->nbinary.ch2);
1355 cmdputs(n->nfor.var);
1360 cmdputs(n->ncase.expr->narg.text);
1364 cmdputs(n->narg.text);
1369 cmdtxt(n->nnot.com);
1372 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1377 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1383 cmdputs(n->narg.text);
1386 p = ">"; i = 1; goto redir;
1388 p = ">>"; i = 1; goto redir;
1390 p = ">&"; i = 1; goto redir;
1392 p = ">|"; i = 1; goto redir;
1394 p = "<"; i = 0; goto redir;
1396 p = "<>"; i = 0; goto redir;
1398 p = "<&"; i = 0; goto redir;
1400 if (n->nfile.fd != i) {
1401 s[0] = n->nfile.fd + '0';
1406 if (n->type == NTOFD || n->type == NFROMFD) {
1407 if (n->ndup.dupfd >= 0)
1408 s[0] = n->ndup.dupfd + '0';
1414 cmdtxt(n->nfile.fname);
1430 cmdputs(const char *s)
1441 while ((c = *p++) != '\0') {
1444 else if (c == CTLVAR) {
1449 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1451 } else if (c == '=' && subtype != 0) {
1452 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1457 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1458 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1462 } else if (c == CTLENDVAR) {
1464 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1474 } else if (c == CTLARI) {
1482 } else if (c == CTLENDARI) {
1483 if (--cmdnleft > 0) {
1487 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1488 cmdnleft++; /* ignore */
1491 if (--cmdnleft <= 0) {