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 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)) < 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 __unused, char **argv __unused)
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 ; procno > 0 ; ps++, procno--) { /* 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) {
417 * Print a list of jobs. If "change" is nonzero, only print jobs whose
418 * statuses have changed since the last call to showjobs.
420 * If the shell is interrupted in the process of creating a job, the
421 * result may be a job structure containing zero processes. Such structures
422 * will be freed here.
426 showjobs(int change, int mode)
431 TRACE(("showjobs(%d) called\n", change));
433 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
436 if (jp->nprocs == 0) {
440 if (change && ! jp->changed)
443 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
445 /* Hack: discard jobs for which $! has not been
446 * referenced in interactive mode when they terminate.
448 if (jp->state == JOBDONE && !jp->remembered &&
449 (iflag || jp != bgjob)) {
458 * Mark a job structure as unused.
462 freejob(struct job *jp)
470 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
471 if (ps->cmd != nullstr)
474 if (jp->ps != &jp->ps0)
486 waitcmd(int argc __unused, char **argv __unused)
493 return (waitcmdloop(NULL));
496 job = getjob_nonotfound(*argptr);
500 retval = waitcmdloop(job);
502 } while (*argptr != NULL);
508 waitcmdloop(struct job *job)
510 int status, retval, sig;
514 * Loop until a process is terminated or stopped, or a SIGINT is
520 if (job->state == JOBDONE) {
521 status = job->ps[job->nprocs - 1].status;
522 if (WIFEXITED(status))
523 retval = WEXITSTATUS(status);
525 retval = WTERMSIG(status) + 128;
526 if (! iflag || ! job->changed)
536 for (jp = jobtab ; jp < jobtab + njobs; jp++)
537 if (jp->used && jp->state == JOBDONE) {
538 if (! iflag || ! jp->changed)
546 for (jp = jobtab ; ; jp++) {
547 if (jp >= jobtab + njobs) { /* no running procs */
550 if (jp->used && jp->state == 0)
554 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
556 sig = pendingsig_waitcmd;
557 pendingsig_waitcmd = 0;
564 jobidcmd(int argc __unused, char **argv __unused)
570 jp = getjob(*argptr);
571 for (i = 0 ; i < jp->nprocs ; ) {
572 out1fmt("%d", (int)jp->ps[i].pid);
573 out1c(++i < jp->nprocs? ' ' : '\n');
581 * Convert a job name to a job structure.
585 getjob_nonotfound(const char *name)
588 struct job *found, *jp;
597 error("No current job");
600 if (name[0] == '%') {
601 if (is_digit(name[1])) {
602 jobno = number(name + 1);
603 if (jobno > 0 && jobno <= njobs
604 && jobtab[jobno - 1].used != 0)
605 return &jobtab[jobno - 1];
607 } else if ((name[1] == '%' || name[1] == '+') &&
609 if ((jp = getcurjob(NULL)) == NULL)
610 error("No current job");
612 } else if (name[1] == '-' && name[2] == '\0') {
613 if ((jp = getcurjob(NULL)) == NULL ||
614 (jp = getcurjob(jp)) == NULL)
615 error("No previous job");
618 } else if (name[1] == '?') {
620 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
621 if (jp->used && jp->nprocs > 0
622 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
624 error("%s: ambiguous", name);
631 namelen = strlen(name);
633 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
634 if (jp->used && jp->nprocs > 0
635 && strncmp(jp->ps[0].cmd, name + 1,
638 error("%s: ambiguous", name);
645 } else if (is_number(name)) {
646 pid = (pid_t)number(name);
647 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
648 if (jp->used && jp->nprocs > 0
649 && jp->ps[jp->nprocs - 1].pid == pid)
658 getjob(const char *name)
662 jp = getjob_nonotfound(name);
664 error("No such job: %s", name);
670 killjob(const char *name, int sig)
676 if (jp->state == JOBDONE)
679 return kill(-jp->ps[0].pid, sig);
682 for (i = 0; i < jp->nprocs; i++)
683 if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
684 if (kill(jp->ps[i].pid, sig) == 0)
692 * Return a new job structure,
696 makejob(union node *node __unused, int nprocs)
701 for (i = njobs, jp = jobtab ; ; jp++) {
705 jobtab = ckmalloc(4 * sizeof jobtab[0]);
710 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
711 memcpy(jp, jobtab, njobs * sizeof jp[0]);
713 /* Relocate `next' pointers and list head */
715 jobmru = &jp[jobmru - jobtab];
716 for (i = 0; i < njobs; i++)
717 if (jp[i].next != NULL)
718 jp[i].next = &jp[jp[i].next -
722 bgjob = &jp[bgjob - jobtab];
723 /* Relocate `ps' pointers */
724 for (i = 0; i < njobs; i++)
725 if (jp[i].ps == &jobtab[i].ps0)
726 jp[i].ps = &jp[i].ps0;
731 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
751 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
756 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
763 setcurjob(struct job *cj)
765 struct job *jp, *prev;
767 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
770 prev->next = jp->next;
783 deljob(struct job *j)
785 struct job *jp, *prev;
787 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
790 prev->next = jp->next;
799 * Return the most recently used job that isn't `nj', and preferably one
803 getcurjob(struct job *nj)
807 /* Try to find a stopped one.. */
808 for (jp = jobmru; jp != NULL; jp = jp->next)
809 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
811 /* Otherwise the most recently used job that isn't `nj' */
812 for (jp = jobmru; jp != NULL; jp = jp->next)
813 if (jp->used && jp != nj)
822 * Fork of a subshell. If we are doing job control, give the subshell its
823 * own process group. Jp is a job structure that the job is to be added to.
824 * N is the command that will be evaluated by the child. Both jp and n may
825 * be NULL. The mode parameter can be one of the following:
826 * FORK_FG - Fork off a foreground process.
827 * FORK_BG - Fork off a background process.
828 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
829 * process group even if job control is on.
831 * When job control is turned off, background processes have their standard
832 * input redirected to /dev/null (except for the second and later processes
837 forkshell(struct job *jp, union node *n, int mode)
842 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
845 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
850 TRACE(("Fork failed, errno=%d\n", errno));
852 error("Cannot fork: %s", strerror(errno));
859 TRACE(("Child shell %d\n", (int)getpid()));
862 handler = &main_handler;
868 jobctl = 0; /* do job control only in root shell */
869 if (wasroot && mode != FORK_NOJOB && mflag) {
870 if (jp == NULL || jp->nprocs == 0)
873 pgrp = jp->ps[0].pid;
874 if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
876 /*** this causes superfluous TIOCSPGRPS ***/
877 if (tcsetpgrp(ttyfd, pgrp) < 0)
878 error("tcsetpgrp failed, errno=%d", errno);
882 } else if (mode == FORK_BG) {
885 if ((jp == NULL || jp->nprocs == 0) &&
886 ! fd0_redirected_p ()) {
888 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
889 error("cannot open %s: %s",
890 _PATH_DEVNULL, strerror(errno));
894 if (mode == FORK_BG) {
897 if ((jp == NULL || jp->nprocs == 0) &&
898 ! fd0_redirected_p ()) {
900 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
901 error("cannot open %s: %s",
902 _PATH_DEVNULL, strerror(errno));
907 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
911 if (wasroot && iflag) {
918 if (rootshell && mode != FORK_NOJOB && mflag) {
919 if (jp == NULL || jp->nprocs == 0)
922 pgrp = jp->ps[0].pid;
925 if (mode == FORK_BG) {
926 if (bgjob != NULL && bgjob->state == JOBDONE &&
927 !bgjob->remembered && !iflag)
929 backgndpid = pid; /* set $! */
933 struct procstat *ps = &jp->ps[jp->nprocs++];
937 if (iflag && rootshell && n)
938 ps->cmd = commandtext(n);
939 jp->foreground = mode == FORK_FG;
945 TRACE(("In parent shell: child = %d\n", (int)pid));
951 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
954 struct jmploc jmploc;
955 struct jmploc *savehandler;
957 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
961 savehandler = handler;
964 TRACE(("Vfork failed, errno=%d\n", errno));
966 error("Cannot fork: %s", strerror(errno));
969 TRACE(("Child shell %d\n", (int)getpid()));
970 if (setjmp(jmploc.loc))
971 _exit(exception == EXEXEC ? exerrno : 2);
980 shellexec(argv, envp, path, idx);
982 handler = savehandler;
984 struct procstat *ps = &jp->ps[jp->nprocs++];
994 TRACE(("In parent shell: child = %d\n", (int)pid));
1000 * Wait for job to finish.
1002 * Under job control we have the problem that while a child process is
1003 * running interrupts generated by the user are sent to the child but not
1004 * to the shell. This means that an infinite loop started by an inter-
1005 * active user may be hard to kill. With job control turned off, an
1006 * interactive user may place an interactive program inside a loop. If
1007 * the interactive program catches interrupts, the user doesn't want
1008 * these interrupts to also abort the loop. The approach we take here
1009 * is to have the shell ignore interrupt signals while waiting for a
1010 * foreground process to terminate, and then send itself an interrupt
1011 * signal if the child process was terminated by an interrupt signal.
1012 * Unfortunately, some programs want to do a bit of cleanup and then
1013 * exit on interrupt; unless these processes terminate themselves by
1014 * sending a signal to themselves (instead of calling exit) they will
1015 * confuse this approach.
1019 waitforjob(struct job *jp, int *origstatus)
1022 int propagate_int = jp->jobctl && jp->foreground;
1028 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1029 while (jp->state == 0)
1030 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
1031 DOWAIT_SIG_TRAP : 0), jp) == -1)
1035 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
1036 error("tcsetpgrp failed, errno=%d\n", errno);
1038 if (jp->state == JOBSTOPPED)
1041 status = jp->ps[jp->nprocs - 1].status;
1042 if (origstatus != NULL)
1043 *origstatus = status;
1044 /* convert to 8 bits */
1045 if (WIFEXITED(status))
1046 st = WEXITSTATUS(status);
1048 else if (WIFSTOPPED(status))
1049 st = WSTOPSIG(status) + 128;
1052 st = WTERMSIG(status) + 128;
1053 if (! JOBS || jp->state == JOBDONE)
1055 if (int_pending()) {
1056 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1060 else if (rootshell && propagate_int &&
1061 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1062 kill(getpid(), SIGINT);
1070 dummy_handler(int sig __unused)
1075 * Wait for a process to terminate.
1079 dowait(int mode, struct job *job)
1081 struct sigaction sa, osa;
1082 sigset_t mask, omask;
1085 struct procstat *sp;
1087 struct job *thisjob;
1094 int restore_sigchld;
1096 TRACE(("dowait(%d, %p) called\n", mode, job));
1097 restore_sigchld = 0;
1098 if ((mode & DOWAIT_SIG) != 0) {
1100 sigprocmask(SIG_BLOCK, &mask, &omask);
1102 if (!issigchldtrapped()) {
1103 restore_sigchld = 1;
1104 sa.sa_handler = dummy_handler;
1106 sigemptyset(&sa.sa_mask);
1107 sigaction(SIGCHLD, &sa, &osa);
1113 wflags = WUNTRACED | WCONTINUED;
1117 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1119 pid = wait3(&status, wflags, (struct rusage *)NULL);
1120 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1121 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1123 if (((mode & DOWAIT_SIG_TRAP) != 0 ?
1124 pendingsig : pendingsig_waitcmd) != 0) {
1132 } while (pid == -1 && errno == EINTR);
1133 if (pid == -1 && errno == ECHILD && job != NULL)
1134 job->state = JOBDONE;
1135 if ((mode & DOWAIT_SIG) != 0) {
1136 if (restore_sigchld)
1137 sigaction(SIGCHLD, &osa, NULL);
1138 sigprocmask(SIG_SETMASK, &omask, NULL);
1145 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1146 if (jp->used && jp->nprocs > 0) {
1149 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1152 if (sp->pid == pid && (sp->status == -1 ||
1153 WIFSTOPPED(sp->status))) {
1154 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1155 (int)pid, sp->status,
1157 if (WIFCONTINUED(status)) {
1161 sp->status = status;
1164 if (sp->status == -1)
1166 else if (WIFSTOPPED(sp->status))
1169 if (stopped) { /* stopped or done */
1170 int state = done? JOBDONE : JOBSTOPPED;
1171 if (jp->state != state) {
1172 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1175 if (done && !jp->remembered &&
1176 !iflag && jp != bgjob)
1188 if (!thisjob || thisjob->state == 0)
1190 else if ((!rootshell || !iflag || thisjob == job) &&
1191 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1194 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1195 if (WIFSIGNALED(sp->status)) {
1196 sig = WTERMSIG(sp->status);
1197 coredump = WCOREDUMP(sp->status);
1199 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1200 sigstr = strsignal(sig);
1204 out2str("Unknown signal");
1206 out2str(" (core dumped)");
1211 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1212 thisjob->changed = 1;
1220 * return 1 if there are stopped jobs, otherwise 0
1222 int job_warning = 0;
1231 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1234 if (jp->state == JOBSTOPPED) {
1235 out2fmt_flush("You have stopped jobs.\n");
1248 while (njobs > 0 && dowait(0, NULL) > 0)
1256 return backgndpid != -1;
1263 if (bgjob != NULL && !forcelocal)
1264 bgjob->remembered = 1;
1269 * Return a string identifying a command (to be printed by the
1273 static char *cmdnextc;
1274 static int cmdnleft;
1275 #define MAXCMDTEXT 200
1278 commandtext(union node *n)
1282 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1283 cmdnleft = MAXCMDTEXT - 4;
1291 cmdtxtdogroup(union node *n)
1300 cmdtxtredir(union node *n, const char *op, int deffd)
1304 if (n->nfile.fd != deffd) {
1305 s[0] = n->nfile.fd + '0';
1310 if (n->type == NTOFD || n->type == NFROMFD) {
1311 if (n->ndup.dupfd >= 0)
1312 s[0] = n->ndup.dupfd + '0';
1318 cmdtxt(n->nfile.fname);
1324 cmdtxt(union node *n)
1327 struct nodelist *lp;
1333 cmdtxt(n->nbinary.ch1);
1335 cmdtxt(n->nbinary.ch2);
1338 cmdtxt(n->nbinary.ch1);
1340 cmdtxt(n->nbinary.ch2);
1343 cmdtxt(n->nbinary.ch1);
1345 cmdtxt(n->nbinary.ch2);
1348 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1356 cmdtxt(n->nredir.n);
1361 cmdtxt(n->nredir.n);
1365 cmdtxt(n->nif.test);
1367 cmdtxt(n->nif.ifpart);
1372 cmdtxt(n->nbinary.ch1);
1373 cmdtxtdogroup(n->nbinary.ch2);
1377 cmdtxt(n->nbinary.ch1);
1378 cmdtxtdogroup(n->nbinary.ch2);
1382 cmdputs(n->nfor.var);
1387 cmdputs(n->ncase.expr->narg.text);
1391 cmdputs(n->narg.text);
1396 cmdtxt(n->nnot.com);
1399 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1404 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1410 cmdputs(n->narg.text);
1413 cmdtxtredir(n, ">", 1);
1416 cmdtxtredir(n, ">>", 1);
1419 cmdtxtredir(n, ">&", 1);
1422 cmdtxtredir(n, ">|", 1);
1425 cmdtxtredir(n, "<", 0);
1428 cmdtxtredir(n, "<>", 0);
1431 cmdtxtredir(n, "<&", 0);
1446 cmdputs(const char *s)
1457 while ((c = *p++) != '\0') {
1460 else if (c == CTLVAR) {
1465 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1467 } else if (c == '=' && subtype != 0) {
1468 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1473 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1474 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1478 } else if (c == CTLENDVAR) {
1480 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1490 } else if (c == CTLARI) {
1498 } else if (c == CTLENDARI) {
1499 if (--cmdnleft > 0) {
1503 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1504 cmdnleft++; /* ignore */
1507 if (--cmdnleft <= 0) {