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 int in_dowait = 0; /* are we in dowait()? */
88 volatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */
89 static int ttyfd = -1;
92 static void restartjob(struct job *);
94 static void freejob(struct job *);
95 static struct job *getjob(char *);
96 static pid_t dowait(int, struct job *);
97 static pid_t waitproc(int, int *);
98 static void checkzombies(void);
99 static void cmdtxt(union node *);
100 static void cmdputs(const char *);
102 static void setcurjob(struct job *);
103 static void deljob(struct job *);
104 static struct job *getcurjob(struct job *);
106 static void printjobcmd(struct job *);
107 static void showjob(struct job *, int);
111 * Turn job control on and off.
122 if (on == jobctl || rootshell == 0)
127 if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) {
129 while (i <= 2 && !isatty(i))
131 if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0)
136 * Keep our TTY file descriptor out of the way of
137 * the user's redirections.
139 if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) {
147 if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) {
152 do { /* while we are in the background */
153 initialpgrp = tcgetpgrp(ttyfd);
154 if (initialpgrp < 0) {
155 out: out2fmt_flush("sh: can't access tty; job control turned off\n");
159 if (initialpgrp != getpgrp()) {
168 tcsetpgrp(ttyfd, rootpid);
169 } else { /* turning job control off */
170 setpgid(0, initialpgrp);
171 tcsetpgrp(ttyfd, initialpgrp);
185 fgcmd(int argc __unused, char **argv)
191 jp = getjob(argv[1]);
193 error("job not created under job control");
196 pgrp = jp->ps[0].pid;
197 tcsetpgrp(ttyfd, pgrp);
201 status = waitforjob(jp, (int *)NULL);
208 bgcmd(int argc, char **argv)
213 jp = getjob(*++argv);
215 error("job not created under job control");
216 if (jp->state == JOBDONE)
220 out1fmt("[%td] ", jp - jobtab + 1);
222 } while (--argc > 1);
228 restartjob(struct job *jp)
233 if (jp->state == JOBDONE)
237 kill(-jp->ps[0].pid, SIGCONT);
238 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
239 if (WIFSTOPPED(ps->status)) {
250 jobscmd(int argc, char *argv[])
255 optind = optreset = 1;
257 mode = SHOWJOBS_DEFAULT;
258 while ((ch = getopt(argc, argv, "lps")) != -1) {
261 mode = SHOWJOBS_VERBOSE;
264 mode = SHOWJOBS_PGIDS;
267 mode = SHOWJOBS_PIDS;
271 error("unknown option: -%c", optopt);
280 while ((id = *argv++) != NULL)
281 showjob(getjob(id), mode);
287 printjobcmd(struct job *jp)
292 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
301 showjob(struct job *jp, int mode)
307 int col, curr, i, jobno, prev, procno;
310 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
311 jobno = jp - jobtab + 1;
314 if ((j = getcurjob(NULL)) != NULL) {
315 curr = j - jobtab + 1;
316 if ((j = getcurjob(j)) != NULL)
317 prev = j - jobtab + 1;
320 ps = jp->ps + jp->nprocs - 1;
321 if (jp->state == 0) {
322 strcpy(statestr, "Running");
324 } else if (jp->state == JOBSTOPPED) {
325 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
327 if (WIFSTOPPED(ps->status))
328 i = WSTOPSIG(ps->status);
331 if (i > 0 && i < sys_nsig && sys_siglist[i])
332 strcpy(statestr, sys_siglist[i]);
334 strcpy(statestr, "Suspended");
336 } else if (WIFEXITED(ps->status)) {
337 if (WEXITSTATUS(ps->status) == 0)
338 strcpy(statestr, "Done");
340 fmtstr(statestr, 64, "Done(%d)",
341 WEXITSTATUS(ps->status));
343 i = WTERMSIG(ps->status);
344 if (i > 0 && i < sys_nsig && sys_siglist[i])
345 strcpy(statestr, sys_siglist[i]);
347 fmtstr(statestr, 64, "Signal %d", i);
348 if (WCOREDUMP(ps->status))
349 strcat(statestr, " (core dumped)");
352 for (ps = jp->ps ; ; ps++) { /* for each process */
353 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
354 out1fmt("%d\n", (int)ps->pid);
357 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
359 if (jobno == curr && ps == jp->ps)
361 else if (jobno == prev && ps == jp->ps)
366 fmtstr(s, 64, "[%d] %c ", jobno, c);
368 fmtstr(s, 64, " %c ", c);
371 if (mode == SHOWJOBS_VERBOSE) {
372 fmtstr(s, 64, "%d ", (int)ps->pid);
378 col += strlen(statestr);
384 if (mode == SHOWJOBS_VERBOSE) {
389 skip: if (--procno <= 0)
395 * Print a list of jobs. If "change" is nonzero, only print jobs whose
396 * statuses have changed since the last call to showjobs.
398 * If the shell is interrupted in the process of creating a job, the
399 * result may be a job structure containing zero processes. Such structures
400 * will be freed here.
404 showjobs(int change, int mode)
409 TRACE(("showjobs(%d) called\n", change));
411 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
414 if (jp->nprocs == 0) {
418 if (change && ! jp->changed)
422 /* Hack: discard jobs for which $! has not been referenced
423 * in interactive mode when they terminate.
425 if (jp->state == JOBDONE && !jp->remembered &&
426 (iflag || jp != bgjob)) {
434 * Mark a job structure as unused.
438 freejob(struct job *jp)
446 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
447 if (ps->cmd != nullstr)
450 if (jp->ps != &jp->ps0)
462 waitcmd(int argc, char **argv)
469 job = getjob(argv[1]);
475 * Loop until a process is terminated or stopped, or a SIGINT is
483 status = job->ps[job->nprocs - 1].status;
484 if (WIFEXITED(status))
485 retval = WEXITSTATUS(status);
487 else if (WIFSTOPPED(status))
488 retval = WSTOPSIG(status) + 128;
491 retval = WTERMSIG(status) + 128;
492 if (! iflag || ! job->changed)
503 for (jp = jobtab ; jp < jobtab + njobs; jp++)
504 if (jp->used && jp->state == JOBDONE) {
505 if (! iflag || ! jp->changed)
513 for (jp = jobtab ; ; jp++) {
514 if (jp >= jobtab + njobs) { /* no running procs */
518 if (jp->used && jp->state == 0)
522 } while (dowait(1, (struct job *)NULL) != -1);
531 jobidcmd(int argc __unused, char **argv)
536 jp = getjob(argv[1]);
537 for (i = 0 ; i < jp->nprocs ; ) {
538 out1fmt("%d", (int)jp->ps[i].pid);
539 out1c(++i < jp->nprocs? ' ' : '\n');
547 * Convert a job name to a job structure.
554 struct job *found, *jp;
560 currentjob: if ((jp = getcurjob(NULL)) == NULL)
561 error("No current job");
564 error("No current job");
566 } else if (name[0] == '%') {
567 if (is_digit(name[1])) {
568 jobno = number(name + 1);
569 if (jobno > 0 && jobno <= njobs
570 && jobtab[jobno - 1].used != 0)
571 return &jobtab[jobno - 1];
573 } else if (name[1] == '%' && name[2] == '\0') {
575 } else if (name[1] == '+' && name[2] == '\0') {
577 } else if (name[1] == '-' && name[2] == '\0') {
578 if ((jp = getcurjob(NULL)) == NULL ||
579 (jp = getcurjob(jp)) == NULL)
580 error("No previous job");
583 } else if (name[1] == '?') {
585 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
586 if (jp->used && jp->nprocs > 0
587 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
589 error("%s: ambiguous", name);
597 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
598 if (jp->used && jp->nprocs > 0
599 && prefix(name + 1, jp->ps[0].cmd)) {
601 error("%s: ambiguous", name);
608 } else if (is_number(name)) {
609 pid = (pid_t)number(name);
610 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
611 if (jp->used && jp->nprocs > 0
612 && jp->ps[jp->nprocs - 1].pid == pid)
616 error("No such job: %s", name);
623 getjobpgrp(char *name)
628 return -jp->ps[0].pid;
632 * Return a new job structure,
636 makejob(union node *node __unused, int nprocs)
641 for (i = njobs, jp = jobtab ; ; jp++) {
645 jobtab = ckmalloc(4 * sizeof jobtab[0]);
650 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
651 memcpy(jp, jobtab, njobs * sizeof jp[0]);
653 /* Relocate `next' pointers and list head */
655 jobmru = &jp[jobmru - jobtab];
656 for (i = 0; i < njobs; i++)
657 if (jp[i].next != NULL)
658 jp[i].next = &jp[jp[i].next -
662 bgjob = &jp[bgjob - jobtab];
663 /* Relocate `ps' pointers */
664 for (i = 0; i < njobs; i++)
665 if (jp[i].ps == &jobtab[i].ps0)
666 jp[i].ps = &jp[i].ps0;
671 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
690 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
695 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
702 setcurjob(struct job *cj)
704 struct job *jp, *prev;
706 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
709 prev->next = jp->next;
722 deljob(struct job *j)
724 struct job *jp, *prev;
726 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
729 prev->next = jp->next;
738 * Return the most recently used job that isn't `nj', and preferably one
742 getcurjob(struct job *nj)
746 /* Try to find a stopped one.. */
747 for (jp = jobmru; jp != NULL; jp = jp->next)
748 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
750 /* Otherwise the most recently used job that isn't `nj' */
751 for (jp = jobmru; jp != NULL; jp = jp->next)
752 if (jp->used && jp != nj)
761 * Fork of a subshell. If we are doing job control, give the subshell its
762 * own process group. Jp is a job structure that the job is to be added to.
763 * N is the command that will be evaluated by the child. Both jp and n may
764 * be NULL. The mode parameter can be one of the following:
765 * FORK_FG - Fork off a foreground process.
766 * FORK_BG - Fork off a background process.
767 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
768 * process group even if job control is on.
770 * When job control is turned off, background processes have their standard
771 * input redirected to /dev/null (except for the second and later processes
776 forkshell(struct job *jp, union node *n, int mode)
781 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
784 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
789 TRACE(("Fork failed, errno=%d\n", errno));
791 error("Cannot fork: %s", strerror(errno));
798 TRACE(("Child shell %d\n", (int)getpid()));
801 handler = &main_handler;
807 jobctl = 0; /* do job control only in root shell */
808 if (wasroot && mode != FORK_NOJOB && mflag) {
809 if (jp == NULL || jp->nprocs == 0)
812 pgrp = jp->ps[0].pid;
813 if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
814 /*** this causes superfluous TIOCSPGRPS ***/
815 if (tcsetpgrp(ttyfd, pgrp) < 0)
816 error("tcsetpgrp failed, errno=%d", errno);
820 } else if (mode == FORK_BG) {
823 if ((jp == NULL || jp->nprocs == 0) &&
824 ! fd0_redirected_p ()) {
826 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
827 error("cannot open %s: %s",
828 _PATH_DEVNULL, strerror(errno));
832 if (mode == FORK_BG) {
835 if ((jp == NULL || jp->nprocs == 0) &&
836 ! fd0_redirected_p ()) {
838 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
839 error("cannot open %s: %s",
840 _PATH_DEVNULL, strerror(errno));
845 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
849 if (wasroot && iflag) {
856 if (rootshell && mode != FORK_NOJOB && mflag) {
857 if (jp == NULL || jp->nprocs == 0)
860 pgrp = jp->ps[0].pid;
863 if (mode == FORK_BG) {
864 if (bgjob != NULL && bgjob->state == JOBDONE &&
865 !bgjob->remembered && !iflag)
867 backgndpid = pid; /* set $! */
871 struct procstat *ps = &jp->ps[jp->nprocs++];
875 if (iflag && rootshell && n)
876 ps->cmd = commandtext(n);
877 jp->foreground = mode == FORK_FG;
883 TRACE(("In parent shell: child = %d\n", (int)pid));
889 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
892 struct jmploc jmploc;
893 struct jmploc *savehandler;
895 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
899 savehandler = handler;
902 TRACE(("Vfork failed, errno=%d\n", errno));
904 error("Cannot fork: %s", strerror(errno));
907 TRACE(("Child shell %d\n", (int)getpid()));
908 if (setjmp(jmploc.loc))
909 _exit(exception == EXEXEC ? exerrno : 2);
918 shellexec(argv, envp, path, idx);
920 handler = savehandler;
922 struct procstat *ps = &jp->ps[jp->nprocs++];
932 TRACE(("In parent shell: child = %d\n", (int)pid));
938 * Wait for job to finish.
940 * Under job control we have the problem that while a child process is
941 * running interrupts generated by the user are sent to the child but not
942 * to the shell. This means that an infinite loop started by an inter-
943 * active user may be hard to kill. With job control turned off, an
944 * interactive user may place an interactive program inside a loop. If
945 * the interactive program catches interrupts, the user doesn't want
946 * these interrupts to also abort the loop. The approach we take here
947 * is to have the shell ignore interrupt signals while waiting for a
948 * foreground process to terminate, and then send itself an interrupt
949 * signal if the child process was terminated by an interrupt signal.
950 * Unfortunately, some programs want to do a bit of cleanup and then
951 * exit on interrupt; unless these processes terminate themselves by
952 * sending a signal to themselves (instead of calling exit) they will
953 * confuse this approach.
957 waitforjob(struct job *jp, int *origstatus)
960 pid_t mypgrp = getpgrp();
961 int propagate_int = jp->jobctl && jp->foreground;
967 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
968 while (jp->state == 0)
969 if (dowait(1, jp) == -1)
973 if (tcsetpgrp(ttyfd, mypgrp) < 0)
974 error("tcsetpgrp failed, errno=%d\n", errno);
976 if (jp->state == JOBSTOPPED)
979 status = jp->ps[jp->nprocs - 1].status;
980 if (origstatus != NULL)
981 *origstatus = status;
982 /* convert to 8 bits */
983 if (WIFEXITED(status))
984 st = WEXITSTATUS(status);
986 else if (WIFSTOPPED(status))
987 st = WSTOPSIG(status) + 128;
990 st = WTERMSIG(status) + 128;
991 if (! JOBS || jp->state == JOBDONE)
994 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
998 else if (rootshell && iflag && propagate_int &&
999 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1000 kill(getpid(), SIGINT);
1009 * Wait for a process to terminate.
1013 dowait(int block, struct job *job)
1017 struct procstat *sp;
1019 struct job *thisjob;
1026 TRACE(("dowait(%d) called\n", block));
1028 pid = waitproc(block, &status);
1029 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1030 } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
1031 (pid > 0 && WIFSTOPPED(status) && !iflag));
1033 if (pid == -1 && errno == ECHILD && job != NULL)
1034 job->state = JOBDONE;
1035 if (breakwaitcmd != 0) {
1044 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1045 if (jp->used && jp->nprocs > 0) {
1048 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1051 if (sp->pid == pid) {
1052 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1053 (int)pid, sp->status,
1055 sp->status = status;
1058 if (sp->status == -1)
1060 else if (WIFSTOPPED(sp->status))
1063 if (stopped) { /* stopped or done */
1064 int state = done? JOBDONE : JOBSTOPPED;
1065 if (jp->state != state) {
1066 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1069 if (done && !jp->remembered &&
1070 !iflag && jp != bgjob)
1082 if (!thisjob || thisjob->state == 0)
1084 else if ((!rootshell || !iflag || thisjob == job) &&
1085 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1088 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1089 if (WIFSIGNALED(sp->status)) {
1090 sig = WTERMSIG(sp->status);
1091 coredump = WCOREDUMP(sp->status);
1093 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1094 if (sig < sys_nsig && sys_siglist[sig])
1095 out2str(sys_siglist[sig]);
1097 outfmt(out2, "Signal %d", sig);
1099 out2str(" (core dumped)");
1104 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1105 thisjob->changed = 1;
1113 * Do a wait system call. If job control is compiled in, we accept
1114 * stopped processes. If block is zero, we return a value of zero
1115 * rather than blocking.
1118 waitproc(int block, int *status)
1129 return wait3(status, flags, (struct rusage *)NULL);
1133 * return 1 if there are stopped jobs, otherwise 0
1135 int job_warning = 0;
1144 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1147 if (jp->state == JOBSTOPPED) {
1148 out2fmt_flush("You have stopped jobs.\n");
1161 while (njobs > 0 && dowait(0, NULL) > 0)
1169 return backgndpid != -1;
1176 if (bgjob != NULL && !forcelocal)
1177 bgjob->remembered = 1;
1182 * Return a string identifying a command (to be printed by the
1186 static char *cmdnextc;
1187 static int cmdnleft;
1188 #define MAXCMDTEXT 200
1191 commandtext(union node *n)
1195 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1196 cmdnleft = MAXCMDTEXT - 4;
1204 cmdtxt(union node *n)
1207 struct nodelist *lp;
1216 cmdtxt(n->nbinary.ch1);
1218 cmdtxt(n->nbinary.ch2);
1221 cmdtxt(n->nbinary.ch1);
1223 cmdtxt(n->nbinary.ch2);
1226 cmdtxt(n->nbinary.ch1);
1228 cmdtxt(n->nbinary.ch2);
1231 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1239 cmdtxt(n->nredir.n);
1244 cmdtxt(n->nredir.n);
1248 cmdtxt(n->nif.test);
1250 cmdtxt(n->nif.ifpart);
1259 cmdtxt(n->nbinary.ch1);
1261 cmdtxt(n->nbinary.ch2);
1266 cmdputs(n->nfor.var);
1271 cmdputs(n->ncase.expr->narg.text);
1275 cmdputs(n->narg.text);
1279 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1284 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1290 cmdputs(n->narg.text);
1293 p = ">"; i = 1; goto redir;
1295 p = ">>"; i = 1; goto redir;
1297 p = ">&"; i = 1; goto redir;
1299 p = ">|"; i = 1; goto redir;
1301 p = "<"; i = 0; goto redir;
1303 p = "<>"; i = 0; goto redir;
1305 p = "<&"; i = 0; goto redir;
1307 if (n->nfile.fd != i) {
1308 s[0] = n->nfile.fd + '0';
1313 if (n->type == NTOFD || n->type == NFROMFD) {
1314 if (n->ndup.dupfd >= 0)
1315 s[0] = n->ndup.dupfd + '0';
1321 cmdtxt(n->nfile.fname);
1337 cmdputs(const char *s)
1348 while ((c = *p++) != '\0') {
1351 else if (c == CTLVAR) {
1356 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1358 } else if (c == '=' && subtype != 0) {
1359 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1364 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1365 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1369 } else if (c == CTLENDVAR) {
1371 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1381 } else if (c == CTLARI) {
1389 } else if (c == CTLENDARI) {
1390 if (--cmdnleft > 0) {
1394 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1395 cmdnleft++; /* ignore */
1398 if (--cmdnleft <= 0) {