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.94 2011/06/12 23:06:04 jilles Exp $
40 #include <sys/ioctl.h>
41 #include <sys/param.h>
42 #include <sys/resource.h>
56 #undef CEOF /* syntax.h redefines this */
75 static struct job *jobtab; /* array of jobs */
76 static int njobs; /* size of array */
77 MKINIT pid_t backgndpid = -1; /* pid of last background process */
78 MKINIT struct job *bgjob = NULL; /* last background process */
80 static struct job *jobmru; /* most recently used job list */
81 static pid_t initialpgrp; /* pgrp of shell on invocation */
83 int in_waitcmd = 0; /* are we in waitcmd()? */
84 int in_dowait = 0; /* are we in dowait()? */
85 volatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */
86 static int ttyfd = -1;
89 static void restartjob(struct job *);
91 static void freejob(struct job *);
92 static struct job *getjob(char *);
93 static pid_t dowait(int, struct job *);
94 static pid_t waitproc(int, int *);
95 static void checkzombies(void);
96 static void cmdtxt(union node *);
97 static void cmdputs(const char *);
99 static void setcurjob(struct job *);
100 static void deljob(struct job *);
101 static struct job *getcurjob(struct job *);
103 static void printjobcmd(struct job *);
104 static void showjob(struct job *, int);
108 * Turn job control on and off.
119 if (on == jobctl || rootshell == 0)
124 if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) {
126 while (i <= 2 && !isatty(i))
128 if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0)
133 * Keep our TTY file descriptor out of the way of
134 * the user's redirections.
136 if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) {
144 if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) {
149 do { /* while we are in the background */
150 initialpgrp = tcgetpgrp(ttyfd);
151 if (initialpgrp < 0) {
152 out: out2fmt_flush("sh: can't access tty; job control turned off\n");
156 if (initialpgrp != getpgrp()) {
165 tcsetpgrp(ttyfd, rootpid);
166 } else { /* turning job control off */
167 setpgid(0, initialpgrp);
168 tcsetpgrp(ttyfd, initialpgrp);
182 fgcmd(int argc __unused, char **argv)
188 jp = getjob(argv[1]);
190 error("job not created under job control");
193 pgrp = jp->ps[0].pid;
194 tcsetpgrp(ttyfd, pgrp);
198 status = waitforjob(jp, NULL);
205 bgcmd(int argc, char **argv)
210 jp = getjob(*++argv);
212 error("job not created under job control");
213 if (jp->state == JOBDONE)
217 out1fmt("[%td] ", jp - jobtab + 1);
219 } while (--argc > 1);
225 restartjob(struct job *jp)
230 if (jp->state == JOBDONE)
234 kill(-jp->ps[0].pid, SIGCONT);
235 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
236 if (WIFSTOPPED(ps->status)) {
247 jobscmd(int argc, char *argv[])
252 optind = optreset = 1;
254 mode = SHOWJOBS_DEFAULT;
255 while ((ch = getopt(argc, argv, "lps")) != -1) {
258 mode = SHOWJOBS_VERBOSE;
261 mode = SHOWJOBS_PGIDS;
264 mode = SHOWJOBS_PIDS;
268 error("unknown option: -%c", optopt);
277 while ((id = *argv++) != NULL)
278 showjob(getjob(id), mode);
284 printjobcmd(struct job *jp)
289 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
298 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 if (i > 0 && i < sys_nsig && sys_siglist[i])
329 strcpy(statestr, sys_siglist[i]);
331 strcpy(statestr, "Suspended");
333 } else if (WIFEXITED(ps->status)) {
334 if (WEXITSTATUS(ps->status) == 0)
335 strcpy(statestr, "Done");
337 fmtstr(statestr, 64, "Done(%d)",
338 WEXITSTATUS(ps->status));
340 i = WTERMSIG(ps->status);
341 if (i > 0 && i < sys_nsig && sys_siglist[i])
342 strcpy(statestr, sys_siglist[i]);
344 fmtstr(statestr, 64, "Signal %d", i);
345 if (WCOREDUMP(ps->status))
346 strcat(statestr, " (core dumped)");
349 for (ps = jp->ps ; ; ps++) { /* for each process */
350 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
351 out1fmt("%d\n", (int)ps->pid);
354 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
356 if (jobno == curr && ps == jp->ps)
358 else if (jobno == prev && ps == jp->ps)
363 fmtstr(s, 64, "[%d] %c ", jobno, c);
365 fmtstr(s, 64, " %c ", c);
368 if (mode == SHOWJOBS_VERBOSE) {
369 fmtstr(s, 64, "%d ", (int)ps->pid);
375 col += strlen(statestr);
381 if (mode == SHOWJOBS_VERBOSE) {
386 skip: if (--procno <= 0)
392 * Print a list of jobs. If "change" is nonzero, only print jobs whose
393 * statuses have changed since the last call to showjobs.
395 * If the shell is interrupted in the process of creating a job, the
396 * result may be a job structure containing zero processes. Such structures
397 * will be freed here.
401 showjobs(int change, int mode)
406 TRACE(("showjobs(%d) called\n", change));
408 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
411 if (jp->nprocs == 0) {
415 if (change && ! jp->changed)
419 /* Hack: discard jobs for which $! has not been referenced
420 * in interactive mode when they terminate.
422 if (jp->state == JOBDONE && !jp->remembered &&
423 (iflag || jp != bgjob)) {
431 * Mark a job structure as unused.
435 freejob(struct job *jp)
443 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
444 if (ps->cmd != nullstr)
447 if (jp->ps != &jp->ps0)
459 waitcmd(int argc, char **argv)
466 job = getjob(argv[1]);
472 * Loop until a process is terminated or stopped, or a SIGINT is
480 status = job->ps[job->nprocs - 1].status;
481 if (WIFEXITED(status))
482 retval = WEXITSTATUS(status);
484 else if (WIFSTOPPED(status))
485 retval = WSTOPSIG(status) + 128;
488 retval = WTERMSIG(status) + 128;
489 if (! iflag || ! job->changed)
500 for (jp = jobtab ; jp < jobtab + njobs; jp++)
501 if (jp->used && jp->state == JOBDONE) {
502 if (! iflag || ! jp->changed)
510 for (jp = jobtab ; ; jp++) {
511 if (jp >= jobtab + njobs) { /* no running procs */
515 if (jp->used && jp->state == 0)
519 } while (dowait(1, NULL) != -1);
528 jobidcmd(int argc __unused, char **argv)
533 jp = getjob(argv[1]);
534 for (i = 0 ; i < jp->nprocs ; ) {
535 out1fmt("%d", (int)jp->ps[i].pid);
536 out1c(++i < jp->nprocs? ' ' : '\n');
544 * Convert a job name to a job structure.
551 struct job *found, *jp;
557 currentjob: if ((jp = getcurjob(NULL)) == NULL)
558 error("No current job");
561 error("No current job");
563 } else if (name[0] == '%') {
564 if (is_digit(name[1])) {
565 jobno = number(name + 1);
566 if (jobno > 0 && jobno <= njobs
567 && jobtab[jobno - 1].used != 0)
568 return &jobtab[jobno - 1];
570 } else if (name[1] == '%' && name[2] == '\0') {
572 } else if (name[1] == '+' && name[2] == '\0') {
574 } else if (name[1] == '-' && name[2] == '\0') {
575 if ((jp = getcurjob(NULL)) == NULL ||
576 (jp = getcurjob(jp)) == NULL)
577 error("No previous job");
580 } else if (name[1] == '?') {
582 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
583 if (jp->used && jp->nprocs > 0
584 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
586 error("%s: ambiguous", name);
594 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
595 if (jp->used && jp->nprocs > 0
596 && prefix(name + 1, jp->ps[0].cmd)) {
598 error("%s: ambiguous", name);
605 } else if (is_number(name)) {
606 pid = (pid_t)number(name);
607 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
608 if (jp->used && jp->nprocs > 0
609 && jp->ps[jp->nprocs - 1].pid == pid)
613 error("No such job: %s", name);
620 getjobpgrp(char *name)
625 return -jp->ps[0].pid;
629 * Return a new job structure,
633 makejob(union node *node __unused, int nprocs)
638 for (i = njobs, jp = jobtab ; ; jp++) {
642 jobtab = ckmalloc(4 * sizeof jobtab[0]);
647 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
648 memcpy(jp, jobtab, njobs * sizeof jp[0]);
650 /* Relocate `next' pointers and list head */
652 jobmru = &jp[jobmru - jobtab];
653 for (i = 0; i < njobs; i++)
654 if (jp[i].next != NULL)
655 jp[i].next = &jp[jp[i].next -
659 bgjob = &jp[bgjob - jobtab];
660 /* Relocate `ps' pointers */
661 for (i = 0; i < njobs; i++)
662 if (jp[i].ps == &jobtab[i].ps0)
663 jp[i].ps = &jp[i].ps0;
668 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
687 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
692 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
699 setcurjob(struct job *cj)
701 struct job *jp, *prev;
703 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
706 prev->next = jp->next;
719 deljob(struct job *j)
721 struct job *jp, *prev;
723 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
726 prev->next = jp->next;
735 * Return the most recently used job that isn't `nj', and preferably one
739 getcurjob(struct job *nj)
743 /* Try to find a stopped one.. */
744 for (jp = jobmru; jp != NULL; jp = jp->next)
745 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
747 /* Otherwise the most recently used job that isn't `nj' */
748 for (jp = jobmru; jp != NULL; jp = jp->next)
749 if (jp->used && jp != nj)
758 * Fork of a subshell. If we are doing job control, give the subshell its
759 * own process group. Jp is a job structure that the job is to be added to.
760 * N is the command that will be evaluated by the child. Both jp and n may
761 * be NULL. The mode parameter can be one of the following:
762 * FORK_FG - Fork off a foreground process.
763 * FORK_BG - Fork off a background process.
764 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
765 * process group even if job control is on.
767 * When job control is turned off, background processes have their standard
768 * input redirected to /dev/null (except for the second and later processes
773 forkshell(struct job *jp, union node *n, int mode)
778 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
781 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
786 TRACE(("Fork failed, errno=%d\n", errno));
788 error("Cannot fork: %s", strerror(errno));
795 TRACE(("Child shell %d\n", (int)getpid()));
798 handler = &main_handler;
804 jobctl = 0; /* do job control only in root shell */
805 if (wasroot && mode != FORK_NOJOB && mflag) {
806 if (jp == NULL || jp->nprocs == 0)
809 pgrp = jp->ps[0].pid;
810 if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
811 /*** this causes superfluous TIOCSPGRPS ***/
812 if (tcsetpgrp(ttyfd, pgrp) < 0)
813 error("tcsetpgrp failed, errno=%d", errno);
817 } else if (mode == FORK_BG) {
820 if ((jp == NULL || jp->nprocs == 0) &&
821 ! fd0_redirected_p ()) {
823 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
824 error("cannot open %s: %s",
825 _PATH_DEVNULL, strerror(errno));
829 if (mode == FORK_BG) {
832 if ((jp == NULL || jp->nprocs == 0) &&
833 ! fd0_redirected_p ()) {
835 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
836 error("cannot open %s: %s",
837 _PATH_DEVNULL, strerror(errno));
842 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
846 if (wasroot && iflag) {
853 if (rootshell && mode != FORK_NOJOB && mflag) {
854 if (jp == NULL || jp->nprocs == 0)
857 pgrp = jp->ps[0].pid;
860 if (mode == FORK_BG) {
861 if (bgjob != NULL && bgjob->state == JOBDONE &&
862 !bgjob->remembered && !iflag)
864 backgndpid = pid; /* set $! */
868 struct procstat *ps = &jp->ps[jp->nprocs++];
872 if (iflag && rootshell && n)
873 ps->cmd = commandtext(n);
874 jp->foreground = mode == FORK_FG;
880 TRACE(("In parent shell: child = %d\n", (int)pid));
887 * Wait for job to finish.
889 * Under job control we have the problem that while a child process is
890 * running interrupts generated by the user are sent to the child but not
891 * to the shell. This means that an infinite loop started by an inter-
892 * active user may be hard to kill. With job control turned off, an
893 * interactive user may place an interactive program inside a loop. If
894 * the interactive program catches interrupts, the user doesn't want
895 * these interrupts to also abort the loop. The approach we take here
896 * is to have the shell ignore interrupt signals while waiting for a
897 * foreground process to terminate, and then send itself an interrupt
898 * signal if the child process was terminated by an interrupt signal.
899 * Unfortunately, some programs want to do a bit of cleanup and then
900 * exit on interrupt; unless these processes terminate themselves by
901 * sending a signal to themselves (instead of calling exit) they will
902 * confuse this approach.
906 waitforjob(struct job *jp, int *origstatus)
909 pid_t mypgrp = getpgrp();
910 int propagate_int = jp->jobctl && jp->foreground;
916 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
917 while (jp->state == 0)
918 if (dowait(1, jp) == -1)
922 if (tcsetpgrp(ttyfd, mypgrp) < 0)
923 error("tcsetpgrp failed, errno=%d\n", errno);
925 if (jp->state == JOBSTOPPED)
928 status = jp->ps[jp->nprocs - 1].status;
929 if (origstatus != NULL)
930 *origstatus = status;
931 /* convert to 8 bits */
932 if (WIFEXITED(status))
933 st = WEXITSTATUS(status);
935 else if (WIFSTOPPED(status))
936 st = WSTOPSIG(status) + 128;
939 st = WTERMSIG(status) + 128;
940 if (! JOBS || jp->state == JOBDONE)
943 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
947 else if (rootshell && iflag && propagate_int &&
948 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
949 kill(getpid(), SIGINT);
958 * Wait for a process to terminate.
962 dowait(int block, struct job *job)
975 TRACE(("dowait(%d) called\n", block));
977 pid = waitproc(block, &status);
978 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
979 } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
980 (pid > 0 && WIFSTOPPED(status) && !iflag));
982 if (pid == -1 && errno == ECHILD && job != NULL)
983 job->state = JOBDONE;
984 if (breakwaitcmd != 0) {
987 * Do not early terminate if the pid is positive, else the
988 * job will not be properly recorded.
997 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
998 if (jp->used && jp->nprocs > 0) {
1001 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1004 if (sp->pid == pid) {
1005 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1006 (int)pid, sp->status,
1008 sp->status = status;
1011 if (sp->status == -1)
1013 else if (WIFSTOPPED(sp->status))
1016 if (stopped) { /* stopped or done */
1017 int state = done? JOBDONE : JOBSTOPPED;
1018 if (jp->state != state) {
1019 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1022 if (done && !jp->remembered &&
1023 !iflag && jp != bgjob)
1035 if (!thisjob || thisjob->state == 0)
1037 else if ((!rootshell || !iflag || thisjob == job) &&
1038 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1041 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1042 if (WIFSIGNALED(sp->status)) {
1043 sig = WTERMSIG(sp->status);
1044 coredump = WCOREDUMP(sp->status);
1046 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1047 if (sig < sys_nsig && sys_siglist[sig])
1048 out2str(sys_siglist[sig]);
1050 outfmt(out2, "Signal %d", sig);
1052 out2str(" (core dumped)");
1057 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1058 thisjob->changed = 1;
1066 * Do a wait system call. If job control is compiled in, we accept
1067 * stopped processes. If block is zero, we return a value of zero
1068 * rather than blocking.
1071 waitproc(int block, int *status)
1082 return wait3(status, flags, NULL);
1086 * return 1 if there are stopped jobs, otherwise 0
1088 int job_warning = 0;
1097 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1100 if (jp->state == JOBSTOPPED) {
1101 out2fmt_flush("You have stopped jobs.\n");
1114 while (njobs > 0 && dowait(0, NULL) > 0)
1122 return backgndpid != -1;
1129 if (bgjob != NULL && !forcelocal)
1130 bgjob->remembered = 1;
1135 * Return a string identifying a command (to be printed by the
1139 static char *cmdnextc;
1140 static int cmdnleft;
1141 #define MAXCMDTEXT 200
1144 commandtext(union node *n)
1148 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1149 cmdnleft = MAXCMDTEXT - 4;
1157 cmdtxt(union node *n)
1160 struct nodelist *lp;
1169 cmdtxt(n->nbinary.ch1);
1171 cmdtxt(n->nbinary.ch2);
1174 cmdtxt(n->nbinary.ch1);
1176 cmdtxt(n->nbinary.ch2);
1179 cmdtxt(n->nbinary.ch1);
1181 cmdtxt(n->nbinary.ch2);
1184 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1192 cmdtxt(n->nredir.n);
1197 cmdtxt(n->nredir.n);
1201 cmdtxt(n->nif.test);
1203 cmdtxt(n->nif.ifpart);
1212 cmdtxt(n->nbinary.ch1);
1214 cmdtxt(n->nbinary.ch2);
1219 cmdputs(n->nfor.var);
1224 cmdputs(n->ncase.expr->narg.text);
1228 cmdputs(n->narg.text);
1232 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1237 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1243 cmdputs(n->narg.text);
1246 p = ">"; i = 1; goto redir;
1248 p = ">>"; i = 1; goto redir;
1250 p = ">&"; i = 1; goto redir;
1252 p = ">|"; i = 1; goto redir;
1254 p = "<"; i = 0; goto redir;
1256 p = "<>"; i = 0; goto redir;
1258 p = "<&"; i = 0; goto redir;
1260 if (n->nfile.fd != i) {
1261 s[0] = n->nfile.fd + '0';
1266 if (n->type == NTOFD || n->type == NFROMFD) {
1267 if (n->ndup.dupfd >= 0)
1268 s[0] = n->ndup.dupfd + '0';
1274 cmdtxt(n->nfile.fname);
1290 cmdputs(const char *s)
1301 while ((c = *p++) != '\0') {
1304 else if (c == CTLVAR) {
1309 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1311 } else if (c == '=' && subtype != 0) {
1312 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1317 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1318 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1322 } else if (c == CTLENDVAR) {
1324 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1334 } else if (c == CTLARI) {
1342 } else if (c == CTLENDARI) {
1343 if (--cmdnleft > 0) {
1347 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1348 cmdnleft++; /* ignore */
1351 if (--cmdnleft <= 0) {