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
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 signal */
91 #define DOWAIT_SIG_TRAP 0x4 /* if DOWAIT_SIG, abort on trapped signal only */
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_MAYBE)) < 0) {
151 while (i <= 2 && !isatty(i))
154 (ttyfd = fcntl(i, F_DUPFD_CLOEXEC_MAYBE, 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_MAYBE, 10)) < 0) {
171 #if !defined(O_CLOEXEC) || !defined(F_DUPFD_CLOEXEC)
172 if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) {
178 do { /* while we are in the background */
179 initialpgrp = tcgetpgrp(ttyfd);
180 if (initialpgrp < 0) {
184 if (initialpgrp != getpgrp()) {
198 tcsetpgrp(ttyfd, rootpid);
199 } else { /* turning job control off */
200 setpgid(0, initialpgrp);
202 tcsetpgrp(ttyfd, initialpgrp);
217 fgcmd(int argc __unused, char **argv __unused)
224 jp = getjob(*argptr);
226 error("job not created under job control");
229 pgrp = jp->ps[0].pid;
231 tcsetpgrp(ttyfd, pgrp);
235 status = waitforjob(jp, (int *)NULL);
242 bgcmd(int argc __unused, char **argv __unused)
248 jp = getjob(*argptr);
250 error("job not created under job control");
251 if (jp->state == JOBDONE)
255 out1fmt("[%td] ", jp - jobtab + 1);
257 } while (*argptr != NULL && *++argptr != NULL);
263 restartjob(struct job *jp)
268 if (jp->state == JOBDONE)
272 kill(-jp->ps[0].pid, SIGCONT);
273 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
274 if (WIFSTOPPED(ps->status)) {
285 jobscmd(int argc __unused, char *argv[] __unused)
290 mode = SHOWJOBS_DEFAULT;
291 while ((ch = nextopt("lps")) != '\0') {
294 mode = SHOWJOBS_VERBOSE;
297 mode = SHOWJOBS_PGIDS;
300 mode = SHOWJOBS_PIDS;
308 while ((id = *argptr++) != NULL)
309 showjob(getjob(id), mode);
315 printjobcmd(struct job *jp)
320 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
329 showjob(struct job *jp, int mode)
336 int col, curr, i, jobno, prev, procno;
339 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
340 jobno = jp - jobtab + 1;
343 if ((j = getcurjob(NULL)) != NULL) {
344 curr = j - jobtab + 1;
345 if ((j = getcurjob(j)) != NULL)
346 prev = j - jobtab + 1;
349 ps = jp->ps + jp->nprocs - 1;
350 if (jp->state == 0) {
351 strcpy(statestr, "Running");
353 } else if (jp->state == JOBSTOPPED) {
354 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
356 if (WIFSTOPPED(ps->status))
357 i = WSTOPSIG(ps->status);
360 sigstr = strsignal(i);
362 strcpy(statestr, sigstr);
364 strcpy(statestr, "Suspended");
366 } else if (WIFEXITED(ps->status)) {
367 if (WEXITSTATUS(ps->status) == 0)
368 strcpy(statestr, "Done");
370 fmtstr(statestr, 64, "Done(%d)",
371 WEXITSTATUS(ps->status));
373 i = WTERMSIG(ps->status);
374 sigstr = strsignal(i);
376 strcpy(statestr, sigstr);
378 strcpy(statestr, "Unknown signal");
379 if (WCOREDUMP(ps->status))
380 strcat(statestr, " (core dumped)");
383 for (ps = jp->ps ; procno > 0 ; ps++, procno--) { /* for each process */
384 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
385 out1fmt("%d\n", (int)ps->pid);
388 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
390 if (jobno == curr && ps == jp->ps)
392 else if (jobno == prev && ps == jp->ps)
397 fmtstr(s, 64, "[%d] %c ", jobno, c);
399 fmtstr(s, 64, " %c ", c);
402 if (mode == SHOWJOBS_VERBOSE) {
403 fmtstr(s, 64, "%d ", (int)ps->pid);
409 col += strlen(statestr);
415 if (mode == SHOWJOBS_VERBOSE) {
424 * Print a list of jobs. If "change" is nonzero, only print jobs whose
425 * statuses have changed since the last call to showjobs.
427 * If the shell is interrupted in the process of creating a job, the
428 * result may be a job structure containing zero processes. Such structures
429 * will be freed here.
433 showjobs(int change, int mode)
438 TRACE(("showjobs(%d) called\n", change));
440 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
443 if (jp->nprocs == 0) {
447 if (change && ! jp->changed)
450 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
452 /* Hack: discard jobs for which $! has not been
453 * referenced in interactive mode when they terminate.
455 if (jp->state == JOBDONE && !jp->remembered &&
456 (iflag || jp != bgjob)) {
465 * Mark a job structure as unused.
469 freejob(struct job *jp)
477 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
478 if (ps->cmd != nullstr)
481 if (jp->ps != &jp->ps0)
493 waitcmd(int argc __unused, char **argv __unused)
500 return (waitcmdloop(NULL));
503 job = getjob_nonotfound(*argptr);
507 retval = waitcmdloop(job);
509 } while (*argptr != NULL);
515 waitcmdloop(struct job *job)
517 int status, retval, sig;
521 * Loop until a process is terminated or stopped, or a SIGINT is
527 if (job->state == JOBDONE) {
528 status = job->ps[job->nprocs - 1].status;
529 if (WIFEXITED(status))
530 retval = WEXITSTATUS(status);
532 retval = WTERMSIG(status) + 128;
533 if (! iflag || ! job->changed)
543 for (jp = jobtab ; jp < jobtab + njobs; jp++)
544 if (jp->used && jp->state == JOBDONE) {
545 if (! iflag || ! jp->changed)
553 for (jp = jobtab ; ; jp++) {
554 if (jp >= jobtab + njobs) { /* no running procs */
557 if (jp->used && jp->state == 0)
561 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
563 sig = pendingsig_waitcmd;
564 pendingsig_waitcmd = 0;
571 jobidcmd(int argc __unused, char **argv __unused)
577 jp = getjob(*argptr);
578 for (i = 0 ; i < jp->nprocs ; ) {
579 out1fmt("%d", (int)jp->ps[i].pid);
580 out1c(++i < jp->nprocs? ' ' : '\n');
588 * Convert a job name to a job structure.
592 getjob_nonotfound(const char *name)
595 struct job *found, *jp;
604 error("No current job");
607 if (name[0] == '%') {
608 if (is_digit(name[1])) {
609 jobno = number(name + 1);
610 if (jobno > 0 && jobno <= njobs
611 && jobtab[jobno - 1].used != 0)
612 return &jobtab[jobno - 1];
614 } else if ((name[1] == '%' || name[1] == '+') &&
616 if ((jp = getcurjob(NULL)) == NULL)
617 error("No current job");
619 } else if (name[1] == '-' && name[2] == '\0') {
620 if ((jp = getcurjob(NULL)) == NULL ||
621 (jp = getcurjob(jp)) == NULL)
622 error("No previous job");
625 } else if (name[1] == '?') {
627 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
628 if (jp->used && jp->nprocs > 0
629 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
631 error("%s: ambiguous", name);
638 namelen = strlen(name);
640 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
641 if (jp->used && jp->nprocs > 0
642 && strncmp(jp->ps[0].cmd, name + 1,
645 error("%s: ambiguous", name);
652 } else if (is_number(name)) {
653 pid = (pid_t)number(name);
654 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
655 if (jp->used && jp->nprocs > 0
656 && jp->ps[jp->nprocs - 1].pid == pid)
665 getjob(const char *name)
669 jp = getjob_nonotfound(name);
671 error("No such job: %s", name);
677 killjob(const char *name, int sig)
683 if (jp->state == JOBDONE)
686 return kill(-jp->ps[0].pid, sig);
689 for (i = 0; i < jp->nprocs; i++)
690 if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
691 if (kill(jp->ps[i].pid, sig) == 0)
699 * Return a new job structure,
703 makejob(union node *node __unused, int nprocs)
708 for (i = njobs, jp = jobtab ; ; jp++) {
712 jobtab = ckmalloc(4 * sizeof jobtab[0]);
717 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
718 memcpy(jp, jobtab, njobs * sizeof jp[0]);
720 /* Relocate `next' pointers and list head */
722 jobmru = &jp[jobmru - jobtab];
723 for (i = 0; i < njobs; i++)
724 if (jp[i].next != NULL)
725 jp[i].next = &jp[jp[i].next -
729 bgjob = &jp[bgjob - jobtab];
730 /* Relocate `ps' pointers */
731 for (i = 0; i < njobs; i++)
732 if (jp[i].ps == &jobtab[i].ps0)
733 jp[i].ps = &jp[i].ps0;
738 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
758 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
763 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
770 setcurjob(struct job *cj)
772 struct job *jp, *prev;
774 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
777 prev->next = jp->next;
790 deljob(struct job *j)
792 struct job *jp, *prev;
794 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
797 prev->next = jp->next;
806 * Return the most recently used job that isn't `nj', and preferably one
810 getcurjob(struct job *nj)
814 /* Try to find a stopped one.. */
815 for (jp = jobmru; jp != NULL; jp = jp->next)
816 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
818 /* Otherwise the most recently used job that isn't `nj' */
819 for (jp = jobmru; jp != NULL; jp = jp->next)
820 if (jp->used && jp != nj)
829 * Fork of a subshell. If we are doing job control, give the subshell its
830 * own process group. Jp is a job structure that the job is to be added to.
831 * N is the command that will be evaluated by the child. Both jp and n may
832 * be NULL. The mode parameter can be one of the following:
833 * FORK_FG - Fork off a foreground process.
834 * FORK_BG - Fork off a background process.
835 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
836 * process group even if job control is on.
838 * When job control is turned off, background processes have their standard
839 * input redirected to /dev/null (except for the second and later processes
844 forkshell(struct job *jp, union node *n, int mode)
849 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
852 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
857 TRACE(("Fork failed, errno=%d\n", errno));
859 error("Cannot fork: %s", strerror(errno));
866 TRACE(("Child shell %d\n", (int)getpid()));
869 handler = &main_handler;
875 jobctl = 0; /* do job control only in root shell */
876 if (wasroot && mode != FORK_NOJOB && mflag) {
877 if (jp == NULL || jp->nprocs == 0)
880 pgrp = jp->ps[0].pid;
881 if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
883 /*** this causes superfluous TIOCSPGRPS ***/
884 if (tcsetpgrp(ttyfd, pgrp) < 0)
885 error("tcsetpgrp failed, errno=%d", errno);
889 } else if (mode == FORK_BG) {
892 if ((jp == NULL || jp->nprocs == 0) &&
893 ! fd0_redirected_p ()) {
895 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
896 error("cannot open %s: %s",
897 _PATH_DEVNULL, strerror(errno));
901 if (mode == FORK_BG) {
904 if ((jp == NULL || jp->nprocs == 0) &&
905 ! fd0_redirected_p ()) {
907 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
908 error("cannot open %s: %s",
909 _PATH_DEVNULL, strerror(errno));
914 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
918 if (wasroot && iflag) {
925 if (rootshell && mode != FORK_NOJOB && mflag) {
926 if (jp == NULL || jp->nprocs == 0)
929 pgrp = jp->ps[0].pid;
932 if (mode == FORK_BG) {
933 if (bgjob != NULL && bgjob->state == JOBDONE &&
934 !bgjob->remembered && !iflag)
936 backgndpid = pid; /* set $! */
940 struct procstat *ps = &jp->ps[jp->nprocs++];
944 if (iflag && rootshell && n)
945 ps->cmd = commandtext(n);
946 jp->foreground = mode == FORK_FG;
952 TRACE(("In parent shell: child = %d\n", (int)pid));
958 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
961 struct jmploc jmploc;
962 struct jmploc *savehandler;
964 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
968 savehandler = handler;
971 TRACE(("Vfork failed, errno=%d\n", errno));
973 error("Cannot fork: %s", strerror(errno));
976 TRACE(("Child shell %d\n", (int)getpid()));
977 if (setjmp(jmploc.loc))
978 _exit(exception == EXEXEC ? exerrno : 2);
987 shellexec(argv, envp, path, idx);
989 handler = savehandler;
991 struct procstat *ps = &jp->ps[jp->nprocs++];
1001 TRACE(("In parent shell: child = %d\n", (int)pid));
1007 * Wait for job to finish.
1009 * Under job control we have the problem that while a child process is
1010 * running interrupts generated by the user are sent to the child but not
1011 * to the shell. This means that an infinite loop started by an inter-
1012 * active user may be hard to kill. With job control turned off, an
1013 * interactive user may place an interactive program inside a loop. If
1014 * the interactive program catches interrupts, the user doesn't want
1015 * these interrupts to also abort the loop. The approach we take here
1016 * is to have the shell ignore interrupt signals while waiting for a
1017 * foreground process to terminate, and then send itself an interrupt
1018 * signal if the child process was terminated by an interrupt signal.
1019 * Unfortunately, some programs want to do a bit of cleanup and then
1020 * exit on interrupt; unless these processes terminate themselves by
1021 * sending a signal to themselves (instead of calling exit) they will
1022 * confuse this approach.
1026 waitforjob(struct job *jp, int *origstatus)
1029 int propagate_int = jp->jobctl && jp->foreground;
1035 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1036 while (jp->state == 0)
1037 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
1038 DOWAIT_SIG_TRAP : 0), jp) == -1)
1042 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
1043 error("tcsetpgrp failed, errno=%d\n", errno);
1045 if (jp->state == JOBSTOPPED)
1048 status = jp->ps[jp->nprocs - 1].status;
1049 if (origstatus != NULL)
1050 *origstatus = status;
1051 /* convert to 8 bits */
1052 if (WIFEXITED(status))
1053 st = WEXITSTATUS(status);
1055 else if (WIFSTOPPED(status))
1056 st = WSTOPSIG(status) + 128;
1059 st = WTERMSIG(status) + 128;
1060 if (! JOBS || jp->state == JOBDONE)
1062 if (int_pending()) {
1063 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1067 else if (rootshell && propagate_int &&
1068 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1069 kill(getpid(), SIGINT);
1077 dummy_handler(int sig __unused)
1082 * Wait for a process to terminate.
1086 dowait(int mode, struct job *job)
1088 struct sigaction sa, osa;
1089 sigset_t mask, omask;
1092 struct procstat *sp;
1094 struct job *thisjob;
1101 int restore_sigchld;
1103 TRACE(("dowait(%d, %p) called\n", mode, job));
1104 restore_sigchld = 0;
1105 if ((mode & DOWAIT_SIG) != 0) {
1107 sigprocmask(SIG_BLOCK, &mask, &omask);
1109 if (!issigchldtrapped()) {
1110 restore_sigchld = 1;
1111 sa.sa_handler = dummy_handler;
1113 sigemptyset(&sa.sa_mask);
1114 sigaction(SIGCHLD, &sa, &osa);
1120 wflags = WUNTRACED | WCONTINUED;
1124 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1126 pid = wait3(&status, wflags, (struct rusage *)NULL);
1127 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1128 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1130 if (((mode & DOWAIT_SIG_TRAP) != 0 ?
1131 pendingsig : pendingsig_waitcmd) != 0) {
1139 } while (pid == -1 && errno == EINTR);
1140 if (pid == -1 && errno == ECHILD && job != NULL)
1141 job->state = JOBDONE;
1142 if ((mode & DOWAIT_SIG) != 0) {
1143 if (restore_sigchld)
1144 sigaction(SIGCHLD, &osa, NULL);
1145 sigprocmask(SIG_SETMASK, &omask, NULL);
1152 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1153 if (jp->used && jp->nprocs > 0) {
1156 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1159 if (sp->pid == pid && (sp->status == -1 ||
1160 WIFSTOPPED(sp->status))) {
1161 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1162 (int)pid, sp->status,
1164 if (WIFCONTINUED(status)) {
1168 sp->status = status;
1171 if (sp->status == -1)
1173 else if (WIFSTOPPED(sp->status))
1176 if (stopped) { /* stopped or done */
1177 int state = done? JOBDONE : JOBSTOPPED;
1178 if (jp->state != state) {
1179 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1182 if (done && !jp->remembered &&
1183 !iflag && jp != bgjob)
1195 if (!thisjob || thisjob->state == 0)
1197 else if ((!rootshell || !iflag || thisjob == job) &&
1198 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1201 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1202 if (WIFSIGNALED(sp->status)) {
1203 sig = WTERMSIG(sp->status);
1204 coredump = WCOREDUMP(sp->status);
1206 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1207 sigstr = strsignal(sig);
1211 out2str("Unknown signal");
1213 out2str(" (core dumped)");
1218 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1219 thisjob->changed = 1;
1227 * return 1 if there are stopped jobs, otherwise 0
1229 int job_warning = 0;
1238 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1241 if (jp->state == JOBSTOPPED) {
1242 out2fmt_flush("You have stopped jobs.\n");
1255 while (njobs > 0 && dowait(0, NULL) > 0)
1263 return backgndpid != -1;
1270 if (bgjob != NULL && !forcelocal)
1271 bgjob->remembered = 1;
1276 * Return a string identifying a command (to be printed by the
1280 static char *cmdnextc;
1281 static int cmdnleft;
1282 #define MAXCMDTEXT 200
1285 commandtext(union node *n)
1289 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1290 cmdnleft = MAXCMDTEXT - 4;
1298 cmdtxtdogroup(union node *n)
1307 cmdtxtredir(union node *n, const char *op, int deffd)
1311 if (n->nfile.fd != deffd) {
1312 s[0] = n->nfile.fd + '0';
1317 if (n->type == NTOFD || n->type == NFROMFD) {
1318 if (n->ndup.dupfd >= 0)
1319 s[0] = n->ndup.dupfd + '0';
1325 cmdtxt(n->nfile.fname);
1331 cmdtxt(union node *n)
1334 struct nodelist *lp;
1340 cmdtxt(n->nbinary.ch1);
1342 cmdtxt(n->nbinary.ch2);
1345 cmdtxt(n->nbinary.ch1);
1347 cmdtxt(n->nbinary.ch2);
1350 cmdtxt(n->nbinary.ch1);
1352 cmdtxt(n->nbinary.ch2);
1355 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1363 cmdtxt(n->nredir.n);
1368 cmdtxt(n->nredir.n);
1372 cmdtxt(n->nif.test);
1374 cmdtxt(n->nif.ifpart);
1379 cmdtxt(n->nbinary.ch1);
1380 cmdtxtdogroup(n->nbinary.ch2);
1384 cmdtxt(n->nbinary.ch1);
1385 cmdtxtdogroup(n->nbinary.ch2);
1389 cmdputs(n->nfor.var);
1394 cmdputs(n->ncase.expr->narg.text);
1398 cmdputs(n->narg.text);
1403 cmdtxt(n->nnot.com);
1406 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1411 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1417 cmdputs(n->narg.text);
1420 cmdtxtredir(n, ">", 1);
1423 cmdtxtredir(n, ">>", 1);
1426 cmdtxtredir(n, ">&", 1);
1429 cmdtxtredir(n, ">|", 1);
1432 cmdtxtredir(n, "<", 0);
1435 cmdtxtredir(n, "<>", 0);
1438 cmdtxtredir(n, "<&", 0);
1453 cmdputs(const char *s)
1464 while ((c = *p++) != '\0') {
1467 else if (c == CTLVAR) {
1472 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1474 } else if (c == '=' && subtype != 0) {
1475 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1480 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1481 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1485 } else if (c == CTLENDVAR) {
1487 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1497 } else if (c == CTLARI) {
1505 } else if (c == CTLENDARI) {
1506 if (--cmdnleft > 0) {
1510 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1511 cmdnleft++; /* ignore */
1514 if (--cmdnleft <= 0) {