4 * consume options, initialization, main loop,
5 * input routines, escape function calling
16 char *Version = "March 11, 1994";
19 #define DWBVERSION "???"
22 char *DWBfontdir = FONTDIR;
23 char *DWBntermdir = NTERMDIR;
24 char *DWBalthyphens = ALTHYPHENS;
25 char *DWBhomedir = "";
27 dwbinit dwbpaths[] = {
29 &DWBntermdir, NULL, 0,
30 &DWBalthyphens, NULL, 0,
36 int TROFF = 1; /* assume we started in troff... */
41 static FILE *ifile = stdin;
42 static FILE *ifl[NSO]; /* open input file pointers */
43 char cfname[NSO+1][NS] = { "stdin" }; /* file name stack */
44 int cfline[NSO]; /* input line count stack */
45 char *progname; /* program name (troff or nroff) */
47 int trace = 0; /* tracing mode: default off */
50 main(int argc, char *argv[])
57 buf[0] = '\0'; /* make sure it's empty (silly 3b2) */
59 if ((p = strrchr(progname, '/')) == NULL)
63 DWBinit(progname, dwbpaths);
64 if (strcmp(p, "nroff") == 0)
67 alphabet = 128; /* unicode for plan 9 */
75 while (--argc > 0 && (++argv)[0][0] == '-')
78 case 'N': /* ought to be used first... */
82 fprintf(stderr, "troff/nroff version %s\n", Version);
84 case 'F': /* switch font tables from default */
85 if (argv[0][2] != '\0') {
86 strcpy(termtab, &argv[0][2]);
87 strcpy(fontdir, &argv[0][2]);
90 strcpy(termtab, argv[0]);
91 strcpy(fontdir, argv[0]);
100 npn = atoi(&argv[0][2]);
102 case 'u': /* set emboldening amount */
103 bdtab[3] = atoi(&argv[0][2]);
104 if (bdtab[3] < 0 || bdtab[3] > 50)
108 if (!(stop = atoi(&argv[0][2])))
112 sprintf(buf + strlen(buf), ".nr %c %s\n",
113 argv[0][2], &argv[0][3]);
114 /* not yet cpushback(buf);*/
115 /* dotnr(&argv[0][2], &argv[0][3]); */
119 ERROR "Too many macro packages: %s", argv[0] WARN;
122 strcpy(mfiles[nmfi], nextf);
123 strcat(mfiles[nmfi++], &argv[0][2]);
129 strcpy(devname, &argv[0][2]);
146 fprintf(stdout, "%croff: DWB %s\n",
147 TROFF ? 't' : 'n', DWBVERSION);
150 if (argv[0][2] != '\0')
151 trace = trace1 = argv[0][2];
152 break; /* for the sake of compatibility */
154 ERROR "unknown option %s", argv[0] WARN;
160 * cpushback maintains a LIFO, so push pack the -r arguments
161 * in reverse order to maintain a FIFO in case someone did -rC1 -rC3
168 while(strncmp(p, ".nr", 3) != 0)
180 copyf = lgf = nb = nflush = nlflg = 0;
181 if (ip && rbf0(ip) == 0 && ejf && frame->pframe <= ejl && dip == d) {
190 if ((j = cbits(i)) == XPAR) {
193 while (cbits(i) != '\n')
199 if (j == cc || j == c2) {
203 while ((j = cbits(i = getch())) == ' ' || j == '\t')
226 for (i = NTRTAB; --i; )
237 numtabp[PID].val = getpid();
238 numtabp[HP].val = init = 0;
239 numtabp[NL].val = -1;
242 sprintf(buf, ".ds .T %s\n", devname);
244 sprintf(buf, ".ds .P %s\n", DWBhomedir);
246 numtabp[CD].val = -1; /* compensation */
248 frame = stk = (Stack *)setbrk(STACKSIZE);
251 for (i = 1; i < NEV; i++) /* propagate the environment */
252 envcopy(&env[i], &env[0]);
253 for (i = 0; i < NEV; i++) {
254 if ((env[i]._word._bufp = (Tchar *)calloc(WDSIZE, sizeof(Tchar))) == NULL) {
255 ERROR "not enough room for word buffers" WARN;
258 env[i]._word._size = WDSIZE;
259 if ((env[i]._line._bufp = (Tchar *)calloc(LNSIZE, sizeof(Tchar))) == NULL) {
260 ERROR "not enough room for line buffers" WARN;
263 env[i]._line._size = LNSIZE;
265 if ((oline = (Tchar *)calloc(OLNSIZE, sizeof(Tchar))) == NULL) {
266 ERROR "not enough room for line buffers" WARN;
280 ltime = localtime(&tt);
281 numtabp[YR].val = ltime->tm_year % 100;
283 numtabp[MO].val = ltime->tm_mon + 1; /* troff uses 1..12 */
284 numtabp[DY].val = ltime->tm_mday;
285 numtabp[DW].val = ltime->tm_wday + 1; /* troff uses 1..7 */
292 void errprint(void) /* error message printer */
294 int savecd = numtabp[CD].val;
299 fprintf(stderr, "%s: ", progname);
300 fputs(errbuf, stderr);
302 fprintf(stderr, "; %s:%d", cfname[ifi], numtabp[CD].val);
306 numtabp[CD].val = savecd;
310 int control(int a, int b)
313 extern Contab *contabp;
317 if (a == 0 || (j = findmn(a)) == -1)
319 if (contabp[j].f == 0) {
321 fprintf(stderr, "invoke macro %s\n", unpair(a));
323 for (k = dilev; k; k--)
324 if (d[k].curd == a) {
325 ERROR "diversion %s invokes itself during diversion",
333 return pushi(contabp[j].mx, a); /* BUG??? all that matters is 0/!0 */
337 fprintf(stderr, "invoke request %s\n", unpair(a));
351 i = max(inumb(&trace), 0);
365 if ((i = getach()) == 0 || (j = getach()) == 0)
373 * table encodes some special characters, to speed up tests
374 * in getch, viz FLSS, RPT, f, \b, \n, fc, tabch, ldrch
377 char gchtab[NCHARS] = {
378 000,004,000,000,010,000,000,000, /* fc, ldr */
379 001,002,001,000,001,000,000,000, /* \b, tab, nl, RPT */
380 000,000,000,000,000,000,000,000,
381 000,001,000,001,000,000,000,000, /* FLSS, ESC */
382 000,000,000,000,000,000,000,000,
383 000,000,000,000,000,000,000,000,
384 000,000,000,000,000,000,000,000,
385 000,000,000,000,000,000,000,000,
386 000,000,000,000,000,000,000,000,
387 000,000,000,000,000,000,000,000,
388 000,000,000,000,000,000,000,000,
389 000,000,000,000,000,000,000,000,
390 000,000,000,000,000,000,001,000, /* f */
391 000,000,000,000,000,000,000,000,
392 000,000,000,000,000,000,000,000,
393 000,000,000,000,000,000,000,000,
396 int realcbits(Tchar c) /* return character bits, or MOTCH if motion */
412 if (cbits(i) == '\n')
424 if (k >= sizeof(gchtab)/sizeof(gchtab[0]) || gchtab[k] == 0) /* nothing special */
430 numtabp[CD].val++; /* line number */
448 if (k == 'f' && lg && !lgf) {
452 if (k == fc || k == tabch || k == ldrch) {
453 if ((i = setfield(k)) == 0)
459 i = makem(-width(' ' | chbits));
466 k = cbits(j = getch0());
471 case 'n': /* number register */
474 case '$': /* argument indicator */
477 case '*': /* string indicator */
483 case '}': /* RIGHT */
486 case '"': /* comment */
487 while (cbits(i = getch0()) != '\n')
490 numtabp[CD].val++; /* line number */
494 /* experiment: put it here instead of copy mode */
495 case '(': /* special char name \(xx */
496 case 'C': /* \C'...' */
497 if ((i = setch(k)) == 0)
501 case ESC: /* double backslash */
504 case 'e': /* printable version of current eschar */
507 case '\n': /* concealed newline */
510 case ' ': /* unpaddable space */
513 case '\'': /* \(aa */
522 case '-': /* current font minus */
525 case '&': /* filler */
528 case 'c': /* to be continued */
531 case '!': /* transparent indicator */
537 case 'a': /* leader (SOH) */
538 /* old: *pbp++ = LEADER; goto g0; */
544 case 'g': /* return format of a number register */
545 setaf(); /* should this really be in copy mode??? */
550 setsfbits(i, sfbits(j));
559 case 'f': /* font indicator */
562 case 's': /* size indicator */
565 case 'v': /* vert mot */
566 numerr.type = numerr.escarg = 0; numerr.esc = k;
571 case 'h': /* horiz mot */
572 numerr.type = numerr.escarg = 0; numerr.esc = k;
576 case '|': /* narrow space */
579 return(makem((int)(EM)/6));
580 case '^': /* half narrow space */
583 return(makem((int)(EM)/12));
584 case 'w': /* width function */
587 case 'p': /* spread */
590 case 'N': /* absolute character number */
591 numerr.type = numerr.escarg = 0; numerr.esc = k;
592 if ((i = setabs()) == 0)
595 case 'H': /* character height */
596 numerr.type = numerr.escarg = 0; numerr.esc = k;
598 case 'S': /* slant */
599 numerr.type = numerr.escarg = 0; numerr.esc = k;
601 case 'z': /* zero with char */
603 case 'l': /* hor line */
604 numerr.type = numerr.escarg = 0; numerr.esc = k;
607 case 'L': /* vert line */
608 numerr.type = numerr.escarg = 0; numerr.esc = k;
611 case 'D': /* drawing function */
612 numerr.type = numerr.escarg = 0; numerr.esc = k;
615 case 'X': /* \X'...' for copy through */
618 case 'b': /* bracket */
621 case 'o': /* overstrike */
624 case 'k': /* mark hor place */
625 if ((k = findr(getsn())) != -1) {
626 numtabp[k].val = numtabp[HP].val;
629 case '0': /* number space */
630 return(makem(width('0' | chbits)));
631 case 'x': /* extra line space */
632 numerr.type = numerr.escarg = 0; numerr.esc = k;
636 case 'u': /* half em up */
637 case 'r': /* full em up */
638 case 'd': /* half em down */
646 void setxon(void) /* \X'...' for copy through */
653 if (ismot(c = getch()))
658 while ((k = cbits(c = getch())) != delim && k != '\n' && i < xbuf+NC-1) {
663 *i++ = XOFF | chbits;
669 char ifilt[32] = { 0, 001, 002, 003, 0, 005, 006, 007, 010, 011, 012 };
694 if (nx || 1) { /* BUG: was ibufp >= eibuf, so EOF test is wrong */
696 ERROR "in getch0, nfo = %d", nfo WARN;
717 if (i >= 040) /* zapped: && i < 0177 */
721 if (cbits(i) == IMP && !raw)
723 if (i == 0 && !init && !raw) { /* zapped: || i == 0177 */
729 if (copyf == 0 && sfbits(i) == 0)
731 if (cbits(i) == eschar && !raw)
738 Tchar get1ch(FILE *fp) /* get one "character" from input, figure out what alphabet */
744 for (i = 0, p = buf; i < MB_CUR_MAX; i++) {
745 if ((c = getc(fp)) == EOF)
748 if ((n = mbtowc(&wc, buf, p-buf)) >= 0)
751 if (n == 1) /* real ascii, presumably */
754 return p[-1]; /* illegal, but what else to do? */
758 return chadd(buf, MBchar, Install); /* add name even if haven't seen it */
762 void pushback(Tchar *b)
769 while (b > ob && pbp < &pbbuf[NC-3])
771 if (pbp >= &pbbuf[NC-3]) {
772 ERROR "pushback overflow" WARN;
777 void cpushback(char *b)
784 while (b > ob && pbp < &pbbuf[NC-3])
786 if (pbp >= &pbbuf[NC-3]) {
787 ERROR "cpushback overflow" WARN;
799 if (ifi > 0 && !nx) {
801 goto n0; /* popf error */
802 return(1); /* popf ok */
804 if (nx || nmfi < mflg) {
810 if ((nfo -= mflg) && !stdi) {
814 numtabp[CD].val = stdi = mflg = 0;
816 strcpy(cfname[ifi], "stdin");
824 if (p[0] == '-' && p[1] == 0) {
826 strcpy(cfname[ifi], "stdin");
827 } else if ((ifile = fopen(p, "r")) == NULL) {
828 ERROR "cannot open file %s", p WARN;
832 strcpy(cfname[ifi],p);
842 ERROR "popf went negative" WARN;
845 numtabp[CD].val = cfline[ifi]; /* restore line counter */
846 ip = ipl[ifi]; /* input pointer */
847 ifile = ifl[ifi]; /* input FILE * */
859 if (donef && frame == stk)
867 * return 16-bit, ascii/alphabetic character, ignore chars with more bits,
868 * (internal names), spaces and special cookies (below 040).
869 * Leave STX ETX ENQ ACK and BELL in to maintain compatibility with v7 troff.
877 j = cbits(i = getch());
880 || (j <= 040 && j != 002 /*STX*/
884 && j != 007)) { /*BELL*/
901 strcpy(mfiles[nmfi], nextf);
917 for (k = 0; k < NS - 1; k++) {
936 if (skip() || !getname() || (fp = fopen(nextf, "r")) == NULL || ifi >= NSO) {
937 ERROR "can't open file %s", nextf WARN;
940 strcpy(cfname[ifi+1], nextf);
941 cfline[ifi] = numtabp[CD].val; /*hold line counter*/
953 void caself(void) /* set line number and file */
961 cfline[ifi] = numtabp[CD].val = n - 1;
963 if (getname()) { /* eats '\n' ? */
964 strcpy(cfname[ifi], nextf);
970 void cpout(FILE *fin, char *token)
975 if (token) { /* BUG: There should be no NULL bytes in input */
977 while ((fgets(buf, sizeof buf, fin)) != NULL) {
979 numtabp[CD].val++; /* line number */
980 if (strcmp(token, buf) == 0)
983 newl = strchr(buf, '\n');
987 while ((n = fread(buf, sizeof *buf, sizeof buf, fin)) > 0)
988 fwrite(buf, n, 1, ptid);
994 { /* copy file without change */
997 extern int hpos, esc, po;
999 /* this may not make much sense in nroff... */
1003 if (!skip() && getname()) {
1004 if (strncmp("<<", nextf, 2) != 0) {
1005 if ((fd = fopen(nextf, "r")) == NULL) {
1006 ERROR "can't open file %s", nextf WARN;
1009 eof = (char *) NULL;
1010 } else { /* current file */
1011 if (pbp > lastpbp || ip) {
1012 ERROR "casecf: not reading from file" WARN;
1017 ERROR "casecf: missing end of input token" WARN;
1028 ERROR "casecf: no argument" WARN;
1034 /* make it into a clean state, be sure that everything is out */
1038 ptesc(); /* to left margin */
1050 void getline(char *s, int n) /* get rest of input line into s */
1057 for (i = 0; i < n-1; i++)
1058 if ((s[i] = cbits(getch())) == '\n' || s[i] == RIGHT)
1065 void casesy(void) /* call system */
1069 getline(sybuf, NTM);
1093 n = 10 * n + *a++ - '0';
1094 while (isdigit(*a));
1098 *pnp++ = neg ? -n : n;
1100 if (pnp >= &pnlist[NPN-2]) {
1101 ERROR "too many page numbers" WARN;
1110 if (*pnp != -INT_MAX)
1124 if ((long) i < 0 || cbits(j = getch0()) == RPT)
1126 while (i > 0 && pbp < &pbbuf[NC-3]) {