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 __unused, char **argv __unused)
469 if (*argptr != NULL) {
470 job = getjob(*argptr);
476 * Loop until a process is terminated or stopped, or a SIGINT is
484 status = job->ps[job->nprocs - 1].status;
485 if (WIFEXITED(status))
486 retval = WEXITSTATUS(status);
488 else if (WIFSTOPPED(status))
489 retval = WSTOPSIG(status) + 128;
492 retval = WTERMSIG(status) + 128;
493 if (! iflag || ! job->changed)
504 for (jp = jobtab ; jp < jobtab + njobs; jp++)
505 if (jp->used && jp->state == JOBDONE) {
506 if (! iflag || ! jp->changed)
514 for (jp = jobtab ; ; jp++) {
515 if (jp >= jobtab + njobs) { /* no running procs */
519 if (jp->used && jp->state == 0)
523 } while (dowait(1, (struct job *)NULL) != -1);
532 jobidcmd(int argc __unused, char **argv)
537 jp = getjob(argv[1]);
538 for (i = 0 ; i < jp->nprocs ; ) {
539 out1fmt("%d", (int)jp->ps[i].pid);
540 out1c(++i < jp->nprocs? ' ' : '\n');
548 * Convert a job name to a job structure.
555 struct job *found, *jp;
561 currentjob: if ((jp = getcurjob(NULL)) == NULL)
562 error("No current job");
565 error("No current job");
567 } else if (name[0] == '%') {
568 if (is_digit(name[1])) {
569 jobno = number(name + 1);
570 if (jobno > 0 && jobno <= njobs
571 && jobtab[jobno - 1].used != 0)
572 return &jobtab[jobno - 1];
574 } else if (name[1] == '%' && name[2] == '\0') {
576 } else if (name[1] == '+' && name[2] == '\0') {
578 } else if (name[1] == '-' && name[2] == '\0') {
579 if ((jp = getcurjob(NULL)) == NULL ||
580 (jp = getcurjob(jp)) == NULL)
581 error("No previous job");
584 } else if (name[1] == '?') {
586 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
587 if (jp->used && jp->nprocs > 0
588 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
590 error("%s: ambiguous", name);
598 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
599 if (jp->used && jp->nprocs > 0
600 && prefix(name + 1, jp->ps[0].cmd)) {
602 error("%s: ambiguous", name);
609 } else if (is_number(name)) {
610 pid = (pid_t)number(name);
611 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
612 if (jp->used && jp->nprocs > 0
613 && jp->ps[jp->nprocs - 1].pid == pid)
617 error("No such job: %s", name);
624 getjobpgrp(char *name)
629 return -jp->ps[0].pid;
633 * Return a new job structure,
637 makejob(union node *node __unused, int nprocs)
642 for (i = njobs, jp = jobtab ; ; jp++) {
646 jobtab = ckmalloc(4 * sizeof jobtab[0]);
651 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
652 memcpy(jp, jobtab, njobs * sizeof jp[0]);
654 /* Relocate `next' pointers and list head */
656 jobmru = &jp[jobmru - jobtab];
657 for (i = 0; i < njobs; i++)
658 if (jp[i].next != NULL)
659 jp[i].next = &jp[jp[i].next -
663 bgjob = &jp[bgjob - jobtab];
664 /* Relocate `ps' pointers */
665 for (i = 0; i < njobs; i++)
666 if (jp[i].ps == &jobtab[i].ps0)
667 jp[i].ps = &jp[i].ps0;
672 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
691 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
696 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
703 setcurjob(struct job *cj)
705 struct job *jp, *prev;
707 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
710 prev->next = jp->next;
723 deljob(struct job *j)
725 struct job *jp, *prev;
727 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
730 prev->next = jp->next;
739 * Return the most recently used job that isn't `nj', and preferably one
743 getcurjob(struct job *nj)
747 /* Try to find a stopped one.. */
748 for (jp = jobmru; jp != NULL; jp = jp->next)
749 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
751 /* Otherwise the most recently used job that isn't `nj' */
752 for (jp = jobmru; jp != NULL; jp = jp->next)
753 if (jp->used && jp != nj)
762 * Fork of a subshell. If we are doing job control, give the subshell its
763 * own process group. Jp is a job structure that the job is to be added to.
764 * N is the command that will be evaluated by the child. Both jp and n may
765 * be NULL. The mode parameter can be one of the following:
766 * FORK_FG - Fork off a foreground process.
767 * FORK_BG - Fork off a background process.
768 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
769 * process group even if job control is on.
771 * When job control is turned off, background processes have their standard
772 * input redirected to /dev/null (except for the second and later processes
777 forkshell(struct job *jp, union node *n, int mode)
782 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
785 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
790 TRACE(("Fork failed, errno=%d\n", errno));
792 error("Cannot fork: %s", strerror(errno));
799 TRACE(("Child shell %d\n", (int)getpid()));
802 handler = &main_handler;
808 jobctl = 0; /* do job control only in root shell */
809 if (wasroot && mode != FORK_NOJOB && mflag) {
810 if (jp == NULL || jp->nprocs == 0)
813 pgrp = jp->ps[0].pid;
814 if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
815 /*** this causes superfluous TIOCSPGRPS ***/
816 if (tcsetpgrp(ttyfd, pgrp) < 0)
817 error("tcsetpgrp failed, errno=%d", errno);
821 } else if (mode == FORK_BG) {
824 if ((jp == NULL || jp->nprocs == 0) &&
825 ! fd0_redirected_p ()) {
827 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
828 error("cannot open %s: %s",
829 _PATH_DEVNULL, strerror(errno));
833 if (mode == FORK_BG) {
836 if ((jp == NULL || jp->nprocs == 0) &&
837 ! fd0_redirected_p ()) {
839 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
840 error("cannot open %s: %s",
841 _PATH_DEVNULL, strerror(errno));
846 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
850 if (wasroot && iflag) {
857 if (rootshell && mode != FORK_NOJOB && mflag) {
858 if (jp == NULL || jp->nprocs == 0)
861 pgrp = jp->ps[0].pid;
864 if (mode == FORK_BG) {
865 if (bgjob != NULL && bgjob->state == JOBDONE &&
866 !bgjob->remembered && !iflag)
868 backgndpid = pid; /* set $! */
872 struct procstat *ps = &jp->ps[jp->nprocs++];
876 if (iflag && rootshell && n)
877 ps->cmd = commandtext(n);
878 jp->foreground = mode == FORK_FG;
884 TRACE(("In parent shell: child = %d\n", (int)pid));
890 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
893 struct jmploc jmploc;
894 struct jmploc *savehandler;
896 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
900 savehandler = handler;
903 TRACE(("Vfork failed, errno=%d\n", errno));
905 error("Cannot fork: %s", strerror(errno));
908 TRACE(("Child shell %d\n", (int)getpid()));
909 if (setjmp(jmploc.loc))
910 _exit(exception == EXEXEC ? exerrno : 2);
919 shellexec(argv, envp, path, idx);
921 handler = savehandler;
923 struct procstat *ps = &jp->ps[jp->nprocs++];
933 TRACE(("In parent shell: child = %d\n", (int)pid));
939 * Wait for job to finish.
941 * Under job control we have the problem that while a child process is
942 * running interrupts generated by the user are sent to the child but not
943 * to the shell. This means that an infinite loop started by an inter-
944 * active user may be hard to kill. With job control turned off, an
945 * interactive user may place an interactive program inside a loop. If
946 * the interactive program catches interrupts, the user doesn't want
947 * these interrupts to also abort the loop. The approach we take here
948 * is to have the shell ignore interrupt signals while waiting for a
949 * foreground process to terminate, and then send itself an interrupt
950 * signal if the child process was terminated by an interrupt signal.
951 * Unfortunately, some programs want to do a bit of cleanup and then
952 * exit on interrupt; unless these processes terminate themselves by
953 * sending a signal to themselves (instead of calling exit) they will
954 * confuse this approach.
958 waitforjob(struct job *jp, int *origstatus)
961 pid_t mypgrp = getpgrp();
962 int propagate_int = jp->jobctl && jp->foreground;
968 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
969 while (jp->state == 0)
970 if (dowait(1, jp) == -1)
974 if (tcsetpgrp(ttyfd, mypgrp) < 0)
975 error("tcsetpgrp failed, errno=%d\n", errno);
977 if (jp->state == JOBSTOPPED)
980 status = jp->ps[jp->nprocs - 1].status;
981 if (origstatus != NULL)
982 *origstatus = status;
983 /* convert to 8 bits */
984 if (WIFEXITED(status))
985 st = WEXITSTATUS(status);
987 else if (WIFSTOPPED(status))
988 st = WSTOPSIG(status) + 128;
991 st = WTERMSIG(status) + 128;
992 if (! JOBS || jp->state == JOBDONE)
995 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
999 else if (rootshell && iflag && propagate_int &&
1000 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1001 kill(getpid(), SIGINT);
1010 * Wait for a process to terminate.
1014 dowait(int block, struct job *job)
1018 struct procstat *sp;
1020 struct job *thisjob;
1027 TRACE(("dowait(%d) called\n", block));
1029 pid = waitproc(block, &status);
1030 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1031 } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
1032 (pid > 0 && WIFSTOPPED(status) && !iflag));
1034 if (pid == -1 && errno == ECHILD && job != NULL)
1035 job->state = JOBDONE;
1036 if (breakwaitcmd != 0) {
1045 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1046 if (jp->used && jp->nprocs > 0) {
1049 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1052 if (sp->pid == pid) {
1053 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1054 (int)pid, sp->status,
1056 sp->status = status;
1059 if (sp->status == -1)
1061 else if (WIFSTOPPED(sp->status))
1064 if (stopped) { /* stopped or done */
1065 int state = done? JOBDONE : JOBSTOPPED;
1066 if (jp->state != state) {
1067 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1070 if (done && !jp->remembered &&
1071 !iflag && jp != bgjob)
1083 if (!thisjob || thisjob->state == 0)
1085 else if ((!rootshell || !iflag || thisjob == job) &&
1086 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1089 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1090 if (WIFSIGNALED(sp->status)) {
1091 sig = WTERMSIG(sp->status);
1092 coredump = WCOREDUMP(sp->status);
1094 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1095 if (sig < sys_nsig && sys_siglist[sig])
1096 out2str(sys_siglist[sig]);
1098 outfmt(out2, "Signal %d", sig);
1100 out2str(" (core dumped)");
1105 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1106 thisjob->changed = 1;
1114 * Do a wait system call. If job control is compiled in, we accept
1115 * stopped processes. If block is zero, we return a value of zero
1116 * rather than blocking.
1119 waitproc(int block, int *status)
1130 return wait3(status, flags, (struct rusage *)NULL);
1134 * return 1 if there are stopped jobs, otherwise 0
1136 int job_warning = 0;
1145 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1148 if (jp->state == JOBSTOPPED) {
1149 out2fmt_flush("You have stopped jobs.\n");
1162 while (njobs > 0 && dowait(0, NULL) > 0)
1170 return backgndpid != -1;
1177 if (bgjob != NULL && !forcelocal)
1178 bgjob->remembered = 1;
1183 * Return a string identifying a command (to be printed by the
1187 static char *cmdnextc;
1188 static int cmdnleft;
1189 #define MAXCMDTEXT 200
1192 commandtext(union node *n)
1196 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1197 cmdnleft = MAXCMDTEXT - 4;
1205 cmdtxt(union node *n)
1208 struct nodelist *lp;
1217 cmdtxt(n->nbinary.ch1);
1219 cmdtxt(n->nbinary.ch2);
1222 cmdtxt(n->nbinary.ch1);
1224 cmdtxt(n->nbinary.ch2);
1227 cmdtxt(n->nbinary.ch1);
1229 cmdtxt(n->nbinary.ch2);
1232 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1240 cmdtxt(n->nredir.n);
1245 cmdtxt(n->nredir.n);
1249 cmdtxt(n->nif.test);
1251 cmdtxt(n->nif.ifpart);
1260 cmdtxt(n->nbinary.ch1);
1262 cmdtxt(n->nbinary.ch2);
1267 cmdputs(n->nfor.var);
1272 cmdputs(n->ncase.expr->narg.text);
1276 cmdputs(n->narg.text);
1280 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1285 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1291 cmdputs(n->narg.text);
1294 p = ">"; i = 1; goto redir;
1296 p = ">>"; i = 1; goto redir;
1298 p = ">&"; i = 1; goto redir;
1300 p = ">|"; i = 1; goto redir;
1302 p = "<"; i = 0; goto redir;
1304 p = "<>"; i = 0; goto redir;
1306 p = "<&"; i = 0; goto redir;
1308 if (n->nfile.fd != i) {
1309 s[0] = n->nfile.fd + '0';
1314 if (n->type == NTOFD || n->type == NFROMFD) {
1315 if (n->ndup.dupfd >= 0)
1316 s[0] = n->ndup.dupfd + '0';
1322 cmdtxt(n->nfile.fname);
1338 cmdputs(const char *s)
1349 while ((c = *p++) != '\0') {
1352 else if (c == CTLVAR) {
1357 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1359 } else if (c == '=' && subtype != 0) {
1360 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1365 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1366 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1370 } else if (c == CTLENDVAR) {
1372 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1382 } else if (c == CTLARI) {
1390 } else if (c == CTLENDARI) {
1391 if (--cmdnleft > 0) {
1395 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1396 cmdnleft++; /* ignore */
1399 if (--cmdnleft <= 0) {