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.95 2011/06/13 21:03:27 jilles Exp $
40 #include <sys/ioctl.h>
41 #include <sys/param.h>
42 #include <sys/resource.h>
56 #undef CEOF /* syntax.h redefines this */
76 static struct job *jobtab; /* array of jobs */
77 static int njobs; /* size of array */
78 MKINIT pid_t backgndpid = -1; /* pid of last background process */
79 MKINIT struct job *bgjob = NULL; /* last background process */
81 static struct job *jobmru; /* most recently used job list */
82 static pid_t initialpgrp; /* pgrp of shell on invocation */
84 int in_waitcmd = 0; /* are we in waitcmd()? */
85 int in_dowait = 0; /* are we in dowait()? */
86 volatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */
87 static int ttyfd = -1;
90 static void restartjob(struct job *);
92 static void freejob(struct job *);
93 static struct job *getjob(char *);
94 static pid_t dowait(int, struct job *);
95 static pid_t waitproc(int, int *);
96 static void checkzombies(void);
97 static void cmdtxt(union node *);
98 static void cmdputs(const char *);
100 static void setcurjob(struct job *);
101 static void deljob(struct job *);
102 static struct job *getcurjob(struct job *);
104 static void printjobcmd(struct job *);
105 static void showjob(struct job *, int);
109 * Turn job control on and off.
120 if (on == jobctl || rootshell == 0)
125 if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) {
127 while (i <= 2 && !isatty(i))
129 if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0)
134 * Keep our TTY file descriptor out of the way of
135 * the user's redirections.
137 if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) {
145 if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) {
150 do { /* while we are in the background */
151 initialpgrp = tcgetpgrp(ttyfd);
152 if (initialpgrp < 0) {
153 out: out2fmt_flush("sh: can't access tty; job control turned off\n");
157 if (initialpgrp != getpgrp()) {
166 tcsetpgrp(ttyfd, rootpid);
167 } else { /* turning job control off */
168 setpgid(0, initialpgrp);
169 tcsetpgrp(ttyfd, initialpgrp);
183 fgcmd(int argc __unused, char **argv)
189 jp = getjob(argv[1]);
191 error("job not created under job control");
194 pgrp = jp->ps[0].pid;
195 tcsetpgrp(ttyfd, pgrp);
199 status = waitforjob(jp, NULL);
206 bgcmd(int argc, char **argv)
211 jp = getjob(*++argv);
213 error("job not created under job control");
214 if (jp->state == JOBDONE)
218 out1fmt("[%td] ", jp - jobtab + 1);
220 } while (--argc > 1);
226 restartjob(struct job *jp)
231 if (jp->state == JOBDONE)
235 kill(-jp->ps[0].pid, SIGCONT);
236 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
237 if (WIFSTOPPED(ps->status)) {
248 jobscmd(int argc, char *argv[])
253 optind = optreset = 1;
255 mode = SHOWJOBS_DEFAULT;
256 while ((ch = getopt(argc, argv, "lps")) != -1) {
259 mode = SHOWJOBS_VERBOSE;
262 mode = SHOWJOBS_PGIDS;
265 mode = SHOWJOBS_PIDS;
269 error("unknown option: -%c", optopt);
278 while ((id = *argv++) != NULL)
279 showjob(getjob(id), mode);
285 printjobcmd(struct job *jp)
290 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
299 showjob(struct job *jp, int mode)
305 int col, curr, i, jobno, prev, procno;
308 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
309 jobno = jp - jobtab + 1;
312 if ((j = getcurjob(NULL)) != NULL) {
313 curr = j - jobtab + 1;
314 if ((j = getcurjob(j)) != NULL)
315 prev = j - jobtab + 1;
318 ps = jp->ps + jp->nprocs - 1;
319 if (jp->state == 0) {
320 strcpy(statestr, "Running");
322 } else if (jp->state == JOBSTOPPED) {
323 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
325 if (WIFSTOPPED(ps->status))
326 i = WSTOPSIG(ps->status);
329 if (i > 0 && i < sys_nsig && sys_siglist[i])
330 strcpy(statestr, sys_siglist[i]);
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 if (i > 0 && i < sys_nsig && sys_siglist[i])
343 strcpy(statestr, sys_siglist[i]);
345 fmtstr(statestr, 64, "Signal %d", i);
346 if (WCOREDUMP(ps->status))
347 strcat(statestr, " (core dumped)");
350 for (ps = jp->ps ; ; ps++) { /* for each process */
351 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
352 out1fmt("%d\n", (int)ps->pid);
355 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
357 if (jobno == curr && ps == jp->ps)
359 else if (jobno == prev && ps == jp->ps)
364 fmtstr(s, 64, "[%d] %c ", jobno, c);
366 fmtstr(s, 64, " %c ", c);
369 if (mode == SHOWJOBS_VERBOSE) {
370 fmtstr(s, 64, "%d ", (int)ps->pid);
376 col += strlen(statestr);
382 if (mode == SHOWJOBS_VERBOSE) {
387 skip: if (--procno <= 0)
393 * Print a list of jobs. If "change" is nonzero, only print jobs whose
394 * statuses have changed since the last call to showjobs.
396 * If the shell is interrupted in the process of creating a job, the
397 * result may be a job structure containing zero processes. Such structures
398 * will be freed here.
402 showjobs(int change, int mode)
407 TRACE(("showjobs(%d) called\n", change));
409 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
412 if (jp->nprocs == 0) {
416 if (change && ! jp->changed)
420 /* Hack: discard jobs for which $! has not been referenced
421 * in interactive mode when they terminate.
423 if (jp->state == JOBDONE && !jp->remembered &&
424 (iflag || jp != bgjob)) {
432 * Mark a job structure as unused.
436 freejob(struct job *jp)
444 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
445 if (ps->cmd != nullstr)
448 if (jp->ps != &jp->ps0)
460 waitcmd(int argc, char **argv)
467 job = getjob(argv[1]);
473 * Loop until a process is terminated or stopped, or a SIGINT is
481 status = job->ps[job->nprocs - 1].status;
482 if (WIFEXITED(status))
483 retval = WEXITSTATUS(status);
485 else if (WIFSTOPPED(status))
486 retval = WSTOPSIG(status) + 128;
489 retval = WTERMSIG(status) + 128;
490 if (! iflag || ! job->changed)
501 for (jp = jobtab ; jp < jobtab + njobs; jp++)
502 if (jp->used && jp->state == JOBDONE) {
503 if (! iflag || ! jp->changed)
511 for (jp = jobtab ; ; jp++) {
512 if (jp >= jobtab + njobs) { /* no running procs */
516 if (jp->used && jp->state == 0)
520 } while (dowait(1, NULL) != -1);
529 jobidcmd(int argc __unused, char **argv)
534 jp = getjob(argv[1]);
535 for (i = 0 ; i < jp->nprocs ; ) {
536 out1fmt("%d", (int)jp->ps[i].pid);
537 out1c(++i < jp->nprocs? ' ' : '\n');
545 * Convert a job name to a job structure.
552 struct job *found, *jp;
558 currentjob: if ((jp = getcurjob(NULL)) == NULL)
559 error("No current job");
562 error("No current job");
564 } else if (name[0] == '%') {
565 if (is_digit(name[1])) {
566 jobno = number(name + 1);
567 if (jobno > 0 && jobno <= njobs
568 && jobtab[jobno - 1].used != 0)
569 return &jobtab[jobno - 1];
571 } else if (name[1] == '%' && name[2] == '\0') {
573 } else if (name[1] == '+' && name[2] == '\0') {
575 } else if (name[1] == '-' && name[2] == '\0') {
576 if ((jp = getcurjob(NULL)) == NULL ||
577 (jp = getcurjob(jp)) == NULL)
578 error("No previous job");
581 } else if (name[1] == '?') {
583 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
584 if (jp->used && jp->nprocs > 0
585 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
587 error("%s: ambiguous", name);
595 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
596 if (jp->used && jp->nprocs > 0
597 && prefix(name + 1, jp->ps[0].cmd)) {
599 error("%s: ambiguous", name);
606 } else if (is_number(name)) {
607 pid = (pid_t)number(name);
608 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
609 if (jp->used && jp->nprocs > 0
610 && jp->ps[jp->nprocs - 1].pid == pid)
614 error("No such job: %s", name);
621 getjobpgrp(char *name)
626 return -jp->ps[0].pid;
630 * Return a new job structure,
634 makejob(union node *node __unused, int nprocs)
639 for (i = njobs, jp = jobtab ; ; jp++) {
643 jobtab = ckmalloc(4 * sizeof jobtab[0]);
648 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
649 memcpy(jp, jobtab, njobs * sizeof jp[0]);
651 /* Relocate `next' pointers and list head */
653 jobmru = &jp[jobmru - jobtab];
654 for (i = 0; i < njobs; i++)
655 if (jp[i].next != NULL)
656 jp[i].next = &jp[jp[i].next -
660 bgjob = &jp[bgjob - jobtab];
661 /* Relocate `ps' pointers */
662 for (i = 0; i < njobs; i++)
663 if (jp[i].ps == &jobtab[i].ps0)
664 jp[i].ps = &jp[i].ps0;
669 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
688 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
693 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
700 setcurjob(struct job *cj)
702 struct job *jp, *prev;
704 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
707 prev->next = jp->next;
720 deljob(struct job *j)
722 struct job *jp, *prev;
724 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
727 prev->next = jp->next;
736 * Return the most recently used job that isn't `nj', and preferably one
740 getcurjob(struct job *nj)
744 /* Try to find a stopped one.. */
745 for (jp = jobmru; jp != NULL; jp = jp->next)
746 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
748 /* Otherwise the most recently used job that isn't `nj' */
749 for (jp = jobmru; jp != NULL; jp = jp->next)
750 if (jp->used && jp != nj)
759 * Fork of a subshell. If we are doing job control, give the subshell its
760 * own process group. Jp is a job structure that the job is to be added to.
761 * N is the command that will be evaluated by the child. Both jp and n may
762 * be NULL. The mode parameter can be one of the following:
763 * FORK_FG - Fork off a foreground process.
764 * FORK_BG - Fork off a background process.
765 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
766 * process group even if job control is on.
768 * When job control is turned off, background processes have their standard
769 * input redirected to /dev/null (except for the second and later processes
774 forkshell(struct job *jp, union node *n, int mode)
779 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
782 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
787 TRACE(("Fork failed, errno=%d\n", errno));
789 error("Cannot fork: %s", strerror(errno));
796 TRACE(("Child shell %d\n", (int)getpid()));
799 handler = &main_handler;
805 jobctl = 0; /* do job control only in root shell */
806 if (wasroot && mode != FORK_NOJOB && mflag) {
807 if (jp == NULL || jp->nprocs == 0)
810 pgrp = jp->ps[0].pid;
811 if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
812 /*** this causes superfluous TIOCSPGRPS ***/
813 if (tcsetpgrp(ttyfd, pgrp) < 0)
814 error("tcsetpgrp failed, errno=%d", errno);
818 } else if (mode == FORK_BG) {
821 if ((jp == NULL || jp->nprocs == 0) &&
822 ! fd0_redirected_p ()) {
824 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
825 error("cannot open %s: %s",
826 _PATH_DEVNULL, strerror(errno));
830 if (mode == FORK_BG) {
833 if ((jp == NULL || jp->nprocs == 0) &&
834 ! fd0_redirected_p ()) {
836 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
837 error("cannot open %s: %s",
838 _PATH_DEVNULL, strerror(errno));
843 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
847 if (wasroot && iflag) {
854 if (rootshell && mode != FORK_NOJOB && mflag) {
855 if (jp == NULL || jp->nprocs == 0)
858 pgrp = jp->ps[0].pid;
861 if (mode == FORK_BG) {
862 if (bgjob != NULL && bgjob->state == JOBDONE &&
863 !bgjob->remembered && !iflag)
865 backgndpid = pid; /* set $! */
869 struct procstat *ps = &jp->ps[jp->nprocs++];
873 if (iflag && rootshell && n)
874 ps->cmd = commandtext(n);
875 jp->foreground = mode == FORK_FG;
881 TRACE(("In parent shell: child = %d\n", (int)pid));
888 * Wait for job to finish.
890 * Under job control we have the problem that while a child process is
891 * running interrupts generated by the user are sent to the child but not
892 * to the shell. This means that an infinite loop started by an inter-
893 * active user may be hard to kill. With job control turned off, an
894 * interactive user may place an interactive program inside a loop. If
895 * the interactive program catches interrupts, the user doesn't want
896 * these interrupts to also abort the loop. The approach we take here
897 * is to have the shell ignore interrupt signals while waiting for a
898 * foreground process to terminate, and then send itself an interrupt
899 * signal if the child process was terminated by an interrupt signal.
900 * Unfortunately, some programs want to do a bit of cleanup and then
901 * exit on interrupt; unless these processes terminate themselves by
902 * sending a signal to themselves (instead of calling exit) they will
903 * confuse this approach.
907 waitforjob(struct job *jp, int *origstatus)
910 pid_t mypgrp = getpgrp();
911 int propagate_int = jp->jobctl && jp->foreground;
917 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
918 while (jp->state == 0)
919 if (dowait(1, jp) == -1)
923 if (tcsetpgrp(ttyfd, mypgrp) < 0)
924 error("tcsetpgrp failed, errno=%d\n", errno);
926 if (jp->state == JOBSTOPPED)
929 status = jp->ps[jp->nprocs - 1].status;
930 if (origstatus != NULL)
931 *origstatus = status;
932 /* convert to 8 bits */
933 if (WIFEXITED(status))
934 st = WEXITSTATUS(status);
936 else if (WIFSTOPPED(status))
937 st = WSTOPSIG(status) + 128;
940 st = WTERMSIG(status) + 128;
941 if (! JOBS || jp->state == JOBDONE)
944 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
948 else if (rootshell && iflag && propagate_int &&
949 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
950 kill(getpid(), SIGINT);
959 * Wait for a process to terminate.
963 dowait(int block, struct job *job)
976 TRACE(("dowait(%d) called\n", block));
978 pid = waitproc(block, &status);
979 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
980 } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
981 (pid > 0 && WIFSTOPPED(status) && !iflag));
983 if (pid == -1 && errno == ECHILD && job != NULL)
984 job->state = JOBDONE;
985 if (breakwaitcmd != 0) {
988 * Do not early terminate if the pid is positive, else the
989 * job will not be properly recorded.
998 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
999 if (jp->used && jp->nprocs > 0) {
1002 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1005 if (sp->pid == pid) {
1006 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1007 (int)pid, sp->status,
1009 sp->status = status;
1012 if (sp->status == -1)
1014 else if (WIFSTOPPED(sp->status))
1017 if (stopped) { /* stopped or done */
1018 int state = done? JOBDONE : JOBSTOPPED;
1019 if (jp->state != state) {
1020 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1023 if (done && !jp->remembered &&
1024 !iflag && jp != bgjob)
1036 if (!thisjob || thisjob->state == 0)
1038 else if ((!rootshell || !iflag || thisjob == job) &&
1039 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1042 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1043 if (WIFSIGNALED(sp->status)) {
1044 sig = WTERMSIG(sp->status);
1045 coredump = WCOREDUMP(sp->status);
1047 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1048 if (sig < sys_nsig && sys_siglist[sig])
1049 out2str(sys_siglist[sig]);
1051 outfmt(out2, "Signal %d", sig);
1053 out2str(" (core dumped)");
1058 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1059 thisjob->changed = 1;
1067 * Do a wait system call. If job control is compiled in, we accept
1068 * stopped processes. If block is zero, we return a value of zero
1069 * rather than blocking.
1072 waitproc(int block, int *status)
1083 return wait3(status, flags, NULL);
1087 * return 1 if there are stopped jobs, otherwise 0
1089 int job_warning = 0;
1098 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1101 if (jp->state == JOBSTOPPED) {
1102 out2fmt_flush("You have stopped jobs.\n");
1115 while (njobs > 0 && dowait(0, NULL) > 0)
1123 return backgndpid != -1;
1130 if (bgjob != NULL && !forcelocal)
1131 bgjob->remembered = 1;
1136 * Return a string identifying a command (to be printed by the
1140 static char *cmdnextc;
1141 static int cmdnleft;
1142 #define MAXCMDTEXT 200
1145 commandtext(union node *n)
1149 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1150 cmdnleft = MAXCMDTEXT - 4;
1158 cmdtxt(union node *n)
1161 struct nodelist *lp;
1170 cmdtxt(n->nbinary.ch1);
1172 cmdtxt(n->nbinary.ch2);
1175 cmdtxt(n->nbinary.ch1);
1177 cmdtxt(n->nbinary.ch2);
1180 cmdtxt(n->nbinary.ch1);
1182 cmdtxt(n->nbinary.ch2);
1185 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1193 cmdtxt(n->nredir.n);
1198 cmdtxt(n->nredir.n);
1202 cmdtxt(n->nif.test);
1204 cmdtxt(n->nif.ifpart);
1213 cmdtxt(n->nbinary.ch1);
1215 cmdtxt(n->nbinary.ch2);
1220 cmdputs(n->nfor.var);
1225 cmdputs(n->ncase.expr->narg.text);
1229 cmdputs(n->narg.text);
1233 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1238 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1244 cmdputs(n->narg.text);
1247 p = ">"; i = 1; goto redir;
1249 p = ">>"; i = 1; goto redir;
1251 p = ">&"; i = 1; goto redir;
1253 p = ">|"; i = 1; goto redir;
1255 p = "<"; i = 0; goto redir;
1257 p = "<>"; i = 0; goto redir;
1259 p = "<&"; i = 0; goto redir;
1261 if (n->nfile.fd != i) {
1262 s[0] = n->nfile.fd + '0';
1267 if (n->type == NTOFD || n->type == NFROMFD) {
1268 if (n->ndup.dupfd >= 0)
1269 s[0] = n->ndup.dupfd + '0';
1275 cmdtxt(n->nfile.fname);
1291 cmdputs(const char *s)
1302 while ((c = *p++) != '\0') {
1305 else if (c == CTLVAR) {
1310 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1312 } else if (c == '=' && subtype != 0) {
1313 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1318 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1319 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1323 } else if (c == CTLENDVAR) {
1325 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1335 } else if (c == CTLARI) {
1343 } else if (c == CTLENDARI) {
1344 if (--cmdnleft > 0) {
1348 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1349 cmdnleft++; /* ignore */
1352 if (--cmdnleft <= 0) {