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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)jobs.c 8.5 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/jobs.c,v 1.98 2012/04/02 17:16:24 jilles Exp $
40 #include <sys/ioctl.h>
41 #include <sys/param.h>
42 #include <sys/resource.h>
56 #undef CEOF /* syntax.h redefines this */
77 static struct job *jobtab; /* array of jobs */
78 static int njobs; /* size of array */
79 MKINIT pid_t backgndpid = -1; /* pid of last background process */
80 MKINIT struct job *bgjob = NULL; /* last background process */
82 static struct job *jobmru; /* most recently used job list */
83 static pid_t initialpgrp; /* pgrp of shell on invocation */
85 int in_waitcmd = 0; /* are we in waitcmd()? */
86 int in_dowait = 0; /* are we in dowait()? */
87 volatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */
88 static int ttyfd = -1;
91 static void restartjob(struct job *);
93 static void freejob(struct job *);
94 static struct job *getjob(char *);
95 static pid_t dowait(int, struct job *);
96 static pid_t waitproc(int, int *);
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)
126 if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) {
128 while (i <= 2 && !isatty(i))
130 if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0)
135 * Keep our TTY file descriptor out of the way of
136 * the user's redirections.
138 if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) {
146 if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) {
151 do { /* while we are in the background */
152 initialpgrp = tcgetpgrp(ttyfd);
153 if (initialpgrp < 0) {
154 out: out2fmt_flush("sh: can't access tty; job control turned off\n");
158 if (initialpgrp != getpgrp()) {
167 tcsetpgrp(ttyfd, rootpid);
168 } else { /* turning job control off */
169 setpgid(0, initialpgrp);
170 tcsetpgrp(ttyfd, initialpgrp);
184 fgcmd(int argc __unused, char **argv)
190 jp = getjob(argv[1]);
192 error("job not created under job control");
195 pgrp = jp->ps[0].pid;
196 tcsetpgrp(ttyfd, pgrp);
200 status = waitforjob(jp, NULL);
207 bgcmd(int argc, char **argv)
212 jp = getjob(*++argv);
214 error("job not created under job control");
215 if (jp->state == JOBDONE)
219 out1fmt("[%td] ", jp - jobtab + 1);
221 } while (--argc > 1);
227 restartjob(struct job *jp)
232 if (jp->state == JOBDONE)
236 kill(-jp->ps[0].pid, SIGCONT);
237 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
238 if (WIFSTOPPED(ps->status)) {
249 jobscmd(int argc, char *argv[])
254 optind = optreset = 1;
256 mode = SHOWJOBS_DEFAULT;
257 while ((ch = getopt(argc, argv, "lps")) != -1) {
260 mode = SHOWJOBS_VERBOSE;
263 mode = SHOWJOBS_PGIDS;
266 mode = SHOWJOBS_PIDS;
270 error("unknown option: -%c", optopt);
279 while ((id = *argv++) != NULL)
280 showjob(getjob(id), mode);
286 printjobcmd(struct job *jp)
291 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
300 showjob(struct job *jp, int mode)
306 int col, curr, i, jobno, prev, procno;
309 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
310 jobno = jp - jobtab + 1;
313 if ((j = getcurjob(NULL)) != NULL) {
314 curr = j - jobtab + 1;
315 if ((j = getcurjob(j)) != NULL)
316 prev = j - jobtab + 1;
319 ps = jp->ps + jp->nprocs - 1;
320 if (jp->state == 0) {
321 strcpy(statestr, "Running");
323 } else if (jp->state == JOBSTOPPED) {
324 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
326 if (WIFSTOPPED(ps->status))
327 i = WSTOPSIG(ps->status);
330 if (i > 0 && i < sys_nsig && sys_siglist[i])
331 strcpy(statestr, sys_siglist[i]);
333 strcpy(statestr, "Suspended");
335 } else if (WIFEXITED(ps->status)) {
336 if (WEXITSTATUS(ps->status) == 0)
337 strcpy(statestr, "Done");
339 fmtstr(statestr, 64, "Done(%d)",
340 WEXITSTATUS(ps->status));
342 i = WTERMSIG(ps->status);
343 if (i > 0 && i < sys_nsig && sys_siglist[i])
344 strcpy(statestr, sys_siglist[i]);
346 fmtstr(statestr, 64, "Signal %d", i);
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(1, NULL) != -1);
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(1, 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);
1008 * Wait for a process to terminate.
1012 dowait(int block, struct job *job)
1016 struct procstat *sp;
1018 struct job *thisjob;
1025 TRACE(("dowait(%d) called\n", block));
1027 pid = waitproc(block, &status);
1028 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1029 } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
1030 (pid > 0 && WIFSTOPPED(status) && !iflag));
1032 if (pid == -1 && errno == ECHILD && job != NULL)
1033 job->state = JOBDONE;
1034 if (breakwaitcmd != 0) {
1037 * Do not early terminate if the pid is positive, else the
1038 * job will not be properly recorded.
1047 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1048 if (jp->used && jp->nprocs > 0) {
1051 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1054 if (sp->pid == pid) {
1055 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1056 (int)pid, sp->status,
1058 sp->status = status;
1061 if (sp->status == -1)
1063 else if (WIFSTOPPED(sp->status))
1066 if (stopped) { /* stopped or done */
1067 int state = done? JOBDONE : JOBSTOPPED;
1068 if (jp->state != state) {
1069 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1072 if (done && !jp->remembered &&
1073 !iflag && jp != bgjob)
1085 if (!thisjob || thisjob->state == 0)
1087 else if ((!rootshell || !iflag || thisjob == job) &&
1088 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1091 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1092 if (WIFSIGNALED(sp->status)) {
1093 sig = WTERMSIG(sp->status);
1094 coredump = WCOREDUMP(sp->status);
1096 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1097 if (sig < sys_nsig && sys_siglist[sig])
1098 out2str(sys_siglist[sig]);
1100 outfmt(out2, "Signal %d", sig);
1102 out2str(" (core dumped)");
1107 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1108 thisjob->changed = 1;
1116 * Do a wait system call. If job control is compiled in, we accept
1117 * stopped processes. If block is zero, we return a value of zero
1118 * rather than blocking.
1121 waitproc(int block, int *status)
1132 return wait3(status, flags, NULL);
1136 * return 1 if there are stopped jobs, otherwise 0
1138 int job_warning = 0;
1147 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1150 if (jp->state == JOBSTOPPED) {
1151 out2fmt_flush("You have stopped jobs.\n");
1164 while (njobs > 0 && dowait(0, NULL) > 0)
1172 return backgndpid != -1;
1179 if (bgjob != NULL && !forcelocal)
1180 bgjob->remembered = 1;
1185 * Return a string identifying a command (to be printed by the
1189 static char *cmdnextc;
1190 static int cmdnleft;
1191 #define MAXCMDTEXT 200
1194 commandtext(union node *n)
1198 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1199 cmdnleft = MAXCMDTEXT - 4;
1207 cmdtxt(union node *n)
1210 struct nodelist *lp;
1219 cmdtxt(n->nbinary.ch1);
1221 cmdtxt(n->nbinary.ch2);
1224 cmdtxt(n->nbinary.ch1);
1226 cmdtxt(n->nbinary.ch2);
1229 cmdtxt(n->nbinary.ch1);
1231 cmdtxt(n->nbinary.ch2);
1234 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1242 cmdtxt(n->nredir.n);
1247 cmdtxt(n->nredir.n);
1251 cmdtxt(n->nif.test);
1253 cmdtxt(n->nif.ifpart);
1262 cmdtxt(n->nbinary.ch1);
1264 cmdtxt(n->nbinary.ch2);
1269 cmdputs(n->nfor.var);
1274 cmdputs(n->ncase.expr->narg.text);
1278 cmdputs(n->narg.text);
1282 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1287 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1293 cmdputs(n->narg.text);
1296 p = ">"; i = 1; goto redir;
1298 p = ">>"; i = 1; goto redir;
1300 p = ">&"; i = 1; goto redir;
1302 p = ">|"; i = 1; goto redir;
1304 p = "<"; i = 0; goto redir;
1306 p = "<>"; i = 0; goto redir;
1308 p = "<&"; i = 0; goto redir;
1310 if (n->nfile.fd != i) {
1311 s[0] = n->nfile.fd + '0';
1316 if (n->type == NTOFD || n->type == NFROMFD) {
1317 if (n->ndup.dupfd >= 0)
1318 s[0] = n->ndup.dupfd + '0';
1324 cmdtxt(n->nfile.fname);
1340 cmdputs(const char *s)
1351 while ((c = *p++) != '\0') {
1354 else if (c == CTLVAR) {
1359 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1361 } else if (c == '=' && subtype != 0) {
1362 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1367 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1368 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1372 } else if (c == CTLENDVAR) {
1374 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1384 } else if (c == CTLARI) {
1392 } else if (c == CTLENDARI) {
1393 if (--cmdnleft > 0) {
1397 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1398 cmdnleft++; /* ignore */
1401 if (--cmdnleft <= 0) {