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 __unused, char *argv[] __unused)
258 mode = SHOWJOBS_DEFAULT;
259 while ((ch = nextopt("lps")) != '\0') {
262 mode = SHOWJOBS_VERBOSE;
265 mode = SHOWJOBS_PGIDS;
268 mode = SHOWJOBS_PIDS;
276 while ((id = *argptr++) != NULL)
277 showjob(getjob(id), mode);
283 printjobcmd(struct job *jp)
288 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
297 showjob(struct job *jp, int mode)
304 int col, curr, i, jobno, prev, procno;
307 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
308 jobno = jp - jobtab + 1;
311 if ((j = getcurjob(NULL)) != NULL) {
312 curr = j - jobtab + 1;
313 if ((j = getcurjob(j)) != NULL)
314 prev = j - jobtab + 1;
317 ps = jp->ps + jp->nprocs - 1;
318 if (jp->state == 0) {
319 strcpy(statestr, "Running");
321 } else if (jp->state == JOBSTOPPED) {
322 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
324 if (WIFSTOPPED(ps->status))
325 i = WSTOPSIG(ps->status);
328 sigstr = strsignal(i);
330 strcpy(statestr, sigstr);
332 strcpy(statestr, "Suspended");
334 } else if (WIFEXITED(ps->status)) {
335 if (WEXITSTATUS(ps->status) == 0)
336 strcpy(statestr, "Done");
338 fmtstr(statestr, 64, "Done(%d)",
339 WEXITSTATUS(ps->status));
341 i = WTERMSIG(ps->status);
342 sigstr = strsignal(i);
344 strcpy(statestr, sigstr);
346 strcpy(statestr, "Unknown signal");
347 if (WCOREDUMP(ps->status))
348 strcat(statestr, " (core dumped)");
351 for (ps = jp->ps ; ; ps++) { /* for each process */
352 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
353 out1fmt("%d\n", (int)ps->pid);
356 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
358 if (jobno == curr && ps == jp->ps)
360 else if (jobno == prev && ps == jp->ps)
365 fmtstr(s, 64, "[%d] %c ", jobno, c);
367 fmtstr(s, 64, " %c ", c);
370 if (mode == SHOWJOBS_VERBOSE) {
371 fmtstr(s, 64, "%d ", (int)ps->pid);
377 col += strlen(statestr);
383 if (mode == SHOWJOBS_VERBOSE) {
388 skip: if (--procno <= 0)
394 * Print a list of jobs. If "change" is nonzero, only print jobs whose
395 * statuses have changed since the last call to showjobs.
397 * If the shell is interrupted in the process of creating a job, the
398 * result may be a job structure containing zero processes. Such structures
399 * will be freed here.
403 showjobs(int change, int mode)
408 TRACE(("showjobs(%d) called\n", change));
410 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
413 if (jp->nprocs == 0) {
417 if (change && ! jp->changed)
421 /* Hack: discard jobs for which $! has not been referenced
422 * in interactive mode when they terminate.
424 if (jp->state == JOBDONE && !jp->remembered &&
425 (iflag || jp != bgjob)) {
433 * Mark a job structure as unused.
437 freejob(struct job *jp)
445 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
446 if (ps->cmd != nullstr)
449 if (jp->ps != &jp->ps0)
461 waitcmd(int argc, char **argv)
468 job = getjob(argv[1]);
474 * Loop until a process is terminated or stopped, or a SIGINT is
482 status = job->ps[job->nprocs - 1].status;
483 if (WIFEXITED(status))
484 retval = WEXITSTATUS(status);
486 else if (WIFSTOPPED(status))
487 retval = WSTOPSIG(status) + 128;
490 retval = WTERMSIG(status) + 128;
491 if (! iflag || ! job->changed)
502 for (jp = jobtab ; jp < jobtab + njobs; jp++)
503 if (jp->used && jp->state == JOBDONE) {
504 if (! iflag || ! jp->changed)
512 for (jp = jobtab ; ; jp++) {
513 if (jp >= jobtab + njobs) { /* no running procs */
517 if (jp->used && jp->state == 0)
521 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
524 return pendingsig + 128;
530 jobidcmd(int argc __unused, char **argv)
535 jp = getjob(argv[1]);
536 for (i = 0 ; i < jp->nprocs ; ) {
537 out1fmt("%d", (int)jp->ps[i].pid);
538 out1c(++i < jp->nprocs? ' ' : '\n');
546 * Convert a job name to a job structure.
553 struct job *found, *jp;
559 currentjob: if ((jp = getcurjob(NULL)) == NULL)
560 error("No current job");
563 error("No current job");
565 } else if (name[0] == '%') {
566 if (is_digit(name[1])) {
567 jobno = number(name + 1);
568 if (jobno > 0 && jobno <= njobs
569 && jobtab[jobno - 1].used != 0)
570 return &jobtab[jobno - 1];
572 } else if (name[1] == '%' && name[2] == '\0') {
574 } else if (name[1] == '+' && name[2] == '\0') {
576 } else if (name[1] == '-' && name[2] == '\0') {
577 if ((jp = getcurjob(NULL)) == NULL ||
578 (jp = getcurjob(jp)) == NULL)
579 error("No previous job");
582 } else if (name[1] == '?') {
584 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
585 if (jp->used && jp->nprocs > 0
586 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
588 error("%s: ambiguous", name);
596 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
597 if (jp->used && jp->nprocs > 0
598 && prefix(name + 1, jp->ps[0].cmd)) {
600 error("%s: ambiguous", name);
607 } else if (is_number(name)) {
608 pid = (pid_t)number(name);
609 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
610 if (jp->used && jp->nprocs > 0
611 && jp->ps[jp->nprocs - 1].pid == pid)
615 error("No such job: %s", name);
622 getjobpgrp(char *name)
627 return -jp->ps[0].pid;
631 * Return a new job structure,
635 makejob(union node *node __unused, int nprocs)
640 for (i = njobs, jp = jobtab ; ; jp++) {
644 jobtab = ckmalloc(4 * sizeof jobtab[0]);
649 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
650 memcpy(jp, jobtab, njobs * sizeof jp[0]);
652 /* Relocate `next' pointers and list head */
654 jobmru = &jp[jobmru - jobtab];
655 for (i = 0; i < njobs; i++)
656 if (jp[i].next != NULL)
657 jp[i].next = &jp[jp[i].next -
661 bgjob = &jp[bgjob - jobtab];
662 /* Relocate `ps' pointers */
663 for (i = 0; i < njobs; i++)
664 if (jp[i].ps == &jobtab[i].ps0)
665 jp[i].ps = &jp[i].ps0;
670 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
689 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
694 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
701 setcurjob(struct job *cj)
703 struct job *jp, *prev;
705 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
708 prev->next = jp->next;
721 deljob(struct job *j)
723 struct job *jp, *prev;
725 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
728 prev->next = jp->next;
737 * Return the most recently used job that isn't `nj', and preferably one
741 getcurjob(struct job *nj)
745 /* Try to find a stopped one.. */
746 for (jp = jobmru; jp != NULL; jp = jp->next)
747 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
749 /* Otherwise the most recently used job that isn't `nj' */
750 for (jp = jobmru; jp != NULL; jp = jp->next)
751 if (jp->used && jp != nj)
760 * Fork of a subshell. If we are doing job control, give the subshell its
761 * own process group. Jp is a job structure that the job is to be added to.
762 * N is the command that will be evaluated by the child. Both jp and n may
763 * be NULL. The mode parameter can be one of the following:
764 * FORK_FG - Fork off a foreground process.
765 * FORK_BG - Fork off a background process.
766 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
767 * process group even if job control is on.
769 * When job control is turned off, background processes have their standard
770 * input redirected to /dev/null (except for the second and later processes
775 forkshell(struct job *jp, union node *n, int mode)
780 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
783 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
788 TRACE(("Fork failed, errno=%d\n", errno));
790 error("Cannot fork: %s", strerror(errno));
797 TRACE(("Child shell %d\n", (int)getpid()));
800 handler = &main_handler;
806 jobctl = 0; /* do job control only in root shell */
807 if (wasroot && mode != FORK_NOJOB && mflag) {
808 if (jp == NULL || jp->nprocs == 0)
811 pgrp = jp->ps[0].pid;
812 if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
813 /*** this causes superfluous TIOCSPGRPS ***/
814 if (tcsetpgrp(ttyfd, pgrp) < 0)
815 error("tcsetpgrp failed, errno=%d", errno);
819 } else if (mode == FORK_BG) {
822 if ((jp == NULL || jp->nprocs == 0) &&
823 ! fd0_redirected_p ()) {
825 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
826 error("cannot open %s: %s",
827 _PATH_DEVNULL, strerror(errno));
831 if (mode == FORK_BG) {
834 if ((jp == NULL || jp->nprocs == 0) &&
835 ! fd0_redirected_p ()) {
837 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
838 error("cannot open %s: %s",
839 _PATH_DEVNULL, strerror(errno));
844 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
848 if (wasroot && iflag) {
855 if (rootshell && mode != FORK_NOJOB && mflag) {
856 if (jp == NULL || jp->nprocs == 0)
859 pgrp = jp->ps[0].pid;
862 if (mode == FORK_BG) {
863 if (bgjob != NULL && bgjob->state == JOBDONE &&
864 !bgjob->remembered && !iflag)
866 backgndpid = pid; /* set $! */
870 struct procstat *ps = &jp->ps[jp->nprocs++];
874 if (iflag && rootshell && n)
875 ps->cmd = commandtext(n);
876 jp->foreground = mode == FORK_FG;
882 TRACE(("In parent shell: child = %d\n", (int)pid));
888 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
891 struct jmploc jmploc;
892 struct jmploc *savehandler;
894 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
898 savehandler = handler;
901 TRACE(("Vfork failed, errno=%d\n", errno));
903 error("Cannot fork: %s", strerror(errno));
906 TRACE(("Child shell %d\n", (int)getpid()));
907 if (setjmp(jmploc.loc))
908 _exit(exception == EXEXEC ? exerrno : 2);
917 shellexec(argv, envp, path, idx);
919 handler = savehandler;
921 struct procstat *ps = &jp->ps[jp->nprocs++];
931 TRACE(("In parent shell: child = %d\n", (int)pid));
937 * Wait for job to finish.
939 * Under job control we have the problem that while a child process is
940 * running interrupts generated by the user are sent to the child but not
941 * to the shell. This means that an infinite loop started by an inter-
942 * active user may be hard to kill. With job control turned off, an
943 * interactive user may place an interactive program inside a loop. If
944 * the interactive program catches interrupts, the user doesn't want
945 * these interrupts to also abort the loop. The approach we take here
946 * is to have the shell ignore interrupt signals while waiting for a
947 * foreground process to terminate, and then send itself an interrupt
948 * signal if the child process was terminated by an interrupt signal.
949 * Unfortunately, some programs want to do a bit of cleanup and then
950 * exit on interrupt; unless these processes terminate themselves by
951 * sending a signal to themselves (instead of calling exit) they will
952 * confuse this approach.
956 waitforjob(struct job *jp, int *origstatus)
959 pid_t mypgrp = getpgrp();
960 int propagate_int = jp->jobctl && jp->foreground;
966 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
967 while (jp->state == 0)
968 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG : 0), jp) == -1)
972 if (tcsetpgrp(ttyfd, mypgrp) < 0)
973 error("tcsetpgrp failed, errno=%d\n", errno);
975 if (jp->state == JOBSTOPPED)
978 status = jp->ps[jp->nprocs - 1].status;
979 if (origstatus != NULL)
980 *origstatus = status;
981 /* convert to 8 bits */
982 if (WIFEXITED(status))
983 st = WEXITSTATUS(status);
985 else if (WIFSTOPPED(status))
986 st = WSTOPSIG(status) + 128;
989 st = WTERMSIG(status) + 128;
990 if (! JOBS || jp->state == JOBDONE)
993 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
997 else if (rootshell && iflag && propagate_int &&
998 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
999 kill(getpid(), SIGINT);
1007 dummy_handler(int sig)
1012 * Wait for a process to terminate.
1016 dowait(int mode, struct job *job)
1018 struct sigaction sa, osa;
1019 sigset_t mask, omask;
1022 struct procstat *sp;
1024 struct job *thisjob;
1031 int restore_sigchld;
1033 TRACE(("dowait(%d, %p) called\n", mode, job));
1034 restore_sigchld = 0;
1035 if ((mode & DOWAIT_SIG) != 0) {
1037 sigprocmask(SIG_BLOCK, &mask, &omask);
1039 if (!issigchldtrapped()) {
1040 restore_sigchld = 1;
1041 sa.sa_handler = dummy_handler;
1043 sigemptyset(&sa.sa_mask);
1044 sigaction(SIGCHLD, &sa, &osa);
1050 wflags = WUNTRACED | WCONTINUED;
1054 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1056 pid = wait3(&status, wflags, (struct rusage *)NULL);
1057 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1058 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1064 } while (pid == -1 && errno == EINTR && breakwaitcmd == 0);
1065 if (pid == -1 && errno == ECHILD && job != NULL)
1066 job->state = JOBDONE;
1067 if ((mode & DOWAIT_SIG) != 0) {
1068 if (restore_sigchld)
1069 sigaction(SIGCHLD, &osa, NULL);
1070 sigprocmask(SIG_SETMASK, &omask, NULL);
1073 if (breakwaitcmd != 0) {
1082 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1083 if (jp->used && jp->nprocs > 0) {
1086 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1089 if (sp->pid == pid) {
1090 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1091 (int)pid, sp->status,
1093 if (WIFCONTINUED(status)) {
1097 sp->status = status;
1100 if (sp->status == -1)
1102 else if (WIFSTOPPED(sp->status))
1105 if (stopped) { /* stopped or done */
1106 int state = done? JOBDONE : JOBSTOPPED;
1107 if (jp->state != state) {
1108 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1111 if (done && !jp->remembered &&
1112 !iflag && jp != bgjob)
1124 if (!thisjob || thisjob->state == 0)
1126 else if ((!rootshell || !iflag || thisjob == job) &&
1127 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1130 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1131 if (WIFSIGNALED(sp->status)) {
1132 sig = WTERMSIG(sp->status);
1133 coredump = WCOREDUMP(sp->status);
1135 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1136 sigstr = strsignal(sig);
1140 out2str("Unknown signal");
1142 out2str(" (core dumped)");
1147 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1148 thisjob->changed = 1;
1156 * return 1 if there are stopped jobs, otherwise 0
1158 int job_warning = 0;
1167 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1170 if (jp->state == JOBSTOPPED) {
1171 out2fmt_flush("You have stopped jobs.\n");
1184 while (njobs > 0 && dowait(0, NULL) > 0)
1192 return backgndpid != -1;
1199 if (bgjob != NULL && !forcelocal)
1200 bgjob->remembered = 1;
1205 * Return a string identifying a command (to be printed by the
1209 static char *cmdnextc;
1210 static int cmdnleft;
1211 #define MAXCMDTEXT 200
1214 commandtext(union node *n)
1218 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1219 cmdnleft = MAXCMDTEXT - 4;
1227 cmdtxt(union node *n)
1230 struct nodelist *lp;
1239 cmdtxt(n->nbinary.ch1);
1241 cmdtxt(n->nbinary.ch2);
1244 cmdtxt(n->nbinary.ch1);
1246 cmdtxt(n->nbinary.ch2);
1249 cmdtxt(n->nbinary.ch1);
1251 cmdtxt(n->nbinary.ch2);
1254 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1262 cmdtxt(n->nredir.n);
1267 cmdtxt(n->nredir.n);
1271 cmdtxt(n->nif.test);
1273 cmdtxt(n->nif.ifpart);
1282 cmdtxt(n->nbinary.ch1);
1284 cmdtxt(n->nbinary.ch2);
1289 cmdputs(n->nfor.var);
1294 cmdputs(n->ncase.expr->narg.text);
1298 cmdputs(n->narg.text);
1303 cmdtxt(n->nnot.com);
1306 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1311 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1317 cmdputs(n->narg.text);
1320 p = ">"; i = 1; goto redir;
1322 p = ">>"; i = 1; goto redir;
1324 p = ">&"; i = 1; goto redir;
1326 p = ">|"; i = 1; goto redir;
1328 p = "<"; i = 0; goto redir;
1330 p = "<>"; i = 0; goto redir;
1332 p = "<&"; i = 0; goto redir;
1334 if (n->nfile.fd != i) {
1335 s[0] = n->nfile.fd + '0';
1340 if (n->type == NTOFD || n->type == NFROMFD) {
1341 if (n->ndup.dupfd >= 0)
1342 s[0] = n->ndup.dupfd + '0';
1348 cmdtxt(n->nfile.fname);
1364 cmdputs(const char *s)
1375 while ((c = *p++) != '\0') {
1378 else if (c == CTLVAR) {
1383 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1385 } else if (c == '=' && subtype != 0) {
1386 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1391 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1392 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1396 } else if (c == CTLENDVAR) {
1398 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1408 } else if (c == CTLARI) {
1416 } else if (c == CTLENDARI) {
1417 if (--cmdnleft > 0) {
1421 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1422 cmdnleft++; /* ignore */
1425 if (--cmdnleft <= 0) {