checknr.c revision 100813
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1980, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 3541568Sarchiestatic const char copyright[] = 361590Srgrimes"@(#) Copyright (c) 1980, 1993\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 4091389Sdwmalone#if 0 411590Srgrimes#ifndef lint 4291389Sdwmalonestatic char sccsid[] = "@(#)checknr.c 8.1 (Berkeley) 6/6/93"; 431590Srgrimes#endif /* not lint */ 4491389Sdwmalone#endif 451590Srgrimes 4691389Sdwmalone#include <sys/cdefs.h> 4791389Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/checknr/checknr.c 100813 2002-07-28 14:55:59Z dwmalone $"); 4891389Sdwmalone 491590Srgrimes/* 501590Srgrimes * checknr: check an nroff/troff input file for matching macro calls. 511590Srgrimes * we also attempt to match size and font changes, but only the embedded 521590Srgrimes * kind. These must end in \s0 and \fP resp. Maybe more sophistication 531590Srgrimes * later but for now think of these restrictions as contributions to 541590Srgrimes * structured typesetting. 551590Srgrimes */ 561590Srgrimes#include <stdio.h> 5726881Scharnier#include <stdlib.h> 5826881Scharnier#include <string.h> 591590Srgrimes#include <ctype.h> 601590Srgrimes 611590Srgrimes#define MAXSTK 100 /* Stack size */ 621590Srgrimes#define MAXBR 100 /* Max number of bracket pairs known */ 631590Srgrimes#define MAXCMDS 500 /* Max number of commands known */ 641590Srgrimes 6592920Simpvoid addcmd(char *); 6692920Simpvoid addmac(const char *); 6792920Simpint binsrch(const char *); 68100813Sdwmalonevoid checkknown(const char *); 69100813Sdwmalonevoid chkcmd(const char *, const char *); 7092920Simpvoid complain(int); 7192920Simpint eq(const char *, const char *); 72100813Sdwmalonevoid nomatch(const char *); 7392920Simpvoid pe(int); 7492920Simpvoid process(FILE *); 7592920Simpvoid prop(int); 7692920Simpstatic void usage(void); 7726881Scharnier 781590Srgrimes/* 791590Srgrimes * The stack on which we remember what we've seen so far. 801590Srgrimes */ 811590Srgrimesstruct stkstr { 821590Srgrimes int opno; /* number of opening bracket */ 831590Srgrimes int pl; /* '+', '-', ' ' for \s, 1 for \f, 0 for .ft */ 841590Srgrimes int parm; /* parm to size, font, etc */ 851590Srgrimes int lno; /* line number the thing came in in */ 861590Srgrimes} stk[MAXSTK]; 871590Srgrimesint stktop; 881590Srgrimes 891590Srgrimes/* 901590Srgrimes * The kinds of opening and closing brackets. 911590Srgrimes */ 921590Srgrimesstruct brstr { 9391389Sdwmalone const char *opbr; 9491389Sdwmalone const char *clbr; 951590Srgrimes} br[MAXBR] = { 961590Srgrimes /* A few bare bones troff commands */ 971590Srgrimes#define SZ 0 9826881Scharnier {"sz", "sz"}, /* also \s */ 991590Srgrimes#define FT 1 10026881Scharnier {"ft", "ft"}, /* also \f */ 1011590Srgrimes /* the -mm package */ 10226881Scharnier {"AL", "LE"}, 10326881Scharnier {"AS", "AE"}, 10426881Scharnier {"BL", "LE"}, 10526881Scharnier {"BS", "BE"}, 10626881Scharnier {"DF", "DE"}, 10726881Scharnier {"DL", "LE"}, 10826881Scharnier {"DS", "DE"}, 10926881Scharnier {"FS", "FE"}, 11026881Scharnier {"ML", "LE"}, 11126881Scharnier {"NS", "NE"}, 11226881Scharnier {"RL", "LE"}, 11326881Scharnier {"VL", "LE"}, 1141590Srgrimes /* the -ms package */ 11526881Scharnier {"AB", "AE"}, 11626881Scharnier {"BD", "DE"}, 11726881Scharnier {"CD", "DE"}, 11826881Scharnier {"DS", "DE"}, 11926881Scharnier {"FS", "FE"}, 12026881Scharnier {"ID", "DE"}, 12126881Scharnier {"KF", "KE"}, 12226881Scharnier {"KS", "KE"}, 12326881Scharnier {"LD", "DE"}, 12426881Scharnier {"LG", "NL"}, 12526881Scharnier {"QS", "QE"}, 12626881Scharnier {"RS", "RE"}, 12726881Scharnier {"SM", "NL"}, 12826881Scharnier {"XA", "XE"}, 12926881Scharnier {"XS", "XE"}, 1301590Srgrimes /* The -me package */ 13126881Scharnier {"(b", ")b"}, 13226881Scharnier {"(c", ")c"}, 13326881Scharnier {"(d", ")d"}, 13426881Scharnier {"(f", ")f"}, 13526881Scharnier {"(l", ")l"}, 13626881Scharnier {"(q", ")q"}, 13726881Scharnier {"(x", ")x"}, 13826881Scharnier {"(z", ")z"}, 1391590Srgrimes /* Things needed by preprocessors */ 14026881Scharnier {"EQ", "EN"}, 14126881Scharnier {"TS", "TE"}, 1421590Srgrimes /* Refer */ 14326881Scharnier {"[", "]"}, 14426881Scharnier {0, 0} 1451590Srgrimes}; 1461590Srgrimes 1471590Srgrimes/* 1481590Srgrimes * All commands known to nroff, plus macro packages. 1491590Srgrimes * Used so we can complain about unrecognized commands. 1501590Srgrimes */ 15191389Sdwmaloneconst char *knowncmds[MAXCMDS] = { 1521590Srgrimes"$c", "$f", "$h", "$p", "$s", "(b", "(c", "(d", "(f", "(l", "(q", "(t", 1531590Srgrimes"(x", "(z", ")b", ")c", ")d", ")f", ")l", ")q", ")t", ")x", ")z", "++", 1541590Srgrimes"+c", "1C", "1c", "2C", "2c", "@(", "@)", "@C", "@D", "@F", "@I", "@M", 1551590Srgrimes"@c", "@e", "@f", "@h", "@m", "@n", "@o", "@p", "@r", "@t", "@z", "AB", 1561590Srgrimes"AE", "AF", "AI", "AL", "AM", "AS", "AT", "AU", "AX", "B", "B1", "B2", 1571590Srgrimes"BD", "BE", "BG", "BL", "BS", "BT", "BX", "C1", "C2", "CD", "CM", "CT", 1581590Srgrimes"D", "DA", "DE", "DF", "DL", "DS", "DT", "EC", "EF", "EG", "EH", "EM", 1591590Srgrimes"EN", "EQ", "EX", "FA", "FD", "FE", "FG", "FJ", "FK", "FL", "FN", "FO", 1601590Srgrimes"FQ", "FS", "FV", "FX", "H", "HC", "HD", "HM", "HO", "HU", "I", "ID", 1611590Srgrimes"IE", "IH", "IM", "IP", "IX", "IZ", "KD", "KE", "KF", "KQ", "KS", "LB", 1621590Srgrimes"LC", "LD", "LE", "LG", "LI", "LP", "MC", "ME", "MF", "MH", "ML", "MR", 1631590Srgrimes"MT", "ND", "NE", "NH", "NL", "NP", "NS", "OF", "OH", "OK", "OP", "P", 1641590Srgrimes"P1", "PF", "PH", "PP", "PT", "PX", "PY", "QE", "QP", "QS", "R", "RA", 1651590Srgrimes"RC", "RE", "RL", "RP", "RQ", "RS", "RT", "S", "S0", "S2", "S3", "SA", 1661590Srgrimes"SG", "SH", "SK", "SM", "SP", "SY", "T&", "TA", "TB", "TC", "TD", "TE", 1671590Srgrimes"TH", "TL", "TM", "TP", "TQ", "TR", "TS", "TX", "UL", "US", "UX", "VL", 1681590Srgrimes"WC", "WH", "XA", "XD", "XE", "XF", "XK", "XP", "XS", "[", "[-", "[0", 1691590Srgrimes"[1", "[2", "[3", "[4", "[5", "[<", "[>", "[]", "]", "]-", "]<", "]>", 1701590Srgrimes"][", "ab", "ac", "ad", "af", "am", "ar", "as", "b", "ba", "bc", "bd", 1711590Srgrimes"bi", "bl", "bp", "br", "bx", "c.", "c2", "cc", "ce", "cf", "ch", "cs", 1721590Srgrimes"ct", "cu", "da", "de", "di", "dl", "dn", "ds", "dt", "dw", "dy", "ec", 1731590Srgrimes"ef", "eh", "el", "em", "eo", "ep", "ev", "ex", "fc", "fi", "fl", "fo", 1741590Srgrimes"fp", "ft", "fz", "hc", "he", "hl", "hp", "ht", "hw", "hx", "hy", "i", 1751590Srgrimes"ie", "if", "ig", "in", "ip", "it", "ix", "lc", "lg", "li", "ll", "ln", 1761590Srgrimes"lo", "lp", "ls", "lt", "m1", "m2", "m3", "m4", "mc", "mk", "mo", "n1", 1771590Srgrimes"n2", "na", "ne", "nf", "nh", "nl", "nm", "nn", "np", "nr", "ns", "nx", 1781590Srgrimes"of", "oh", "os", "pa", "pc", "pi", "pl", "pm", "pn", "po", "pp", "ps", 1791590Srgrimes"q", "r", "rb", "rd", "re", "rm", "rn", "ro", "rr", "rs", "rt", "sb", 1801590Srgrimes"sc", "sh", "sk", "so", "sp", "ss", "st", "sv", "sz", "ta", "tc", "th", 1811590Srgrimes"ti", "tl", "tm", "tp", "tr", "u", "uf", "uh", "ul", "vs", "wh", "xp", 1821590Srgrimes"yr", 0 1831590Srgrimes}; 1841590Srgrimes 1851590Srgrimesint lineno; /* current line number in input file */ 18691389Sdwmaloneconst char *cfilename; /* name of current file */ 1871590Srgrimesint nfiles; /* number of files to process */ 1881590Srgrimesint fflag; /* -f: ignore \f */ 1891590Srgrimesint sflag; /* -s: ignore \s */ 1901590Srgrimesint ncmds; /* size of knowncmds */ 1911590Srgrimesint slot; /* slot in knowncmds found by binsrch */ 1921590Srgrimes 19326881Scharnierint 194100813Sdwmalonemain(int argc, char **argv) 1951590Srgrimes{ 1961590Srgrimes FILE *f; 1971590Srgrimes int i; 1981590Srgrimes char *cp; 1991590Srgrimes char b1[4]; 2001590Srgrimes 2011590Srgrimes /* Figure out how many known commands there are */ 2021590Srgrimes while (knowncmds[ncmds]) 2031590Srgrimes ncmds++; 2041590Srgrimes while (argc > 1 && argv[1][0] == '-') { 2051590Srgrimes switch(argv[1][1]) { 2061590Srgrimes 2071590Srgrimes /* -a: add pairs of macros */ 2081590Srgrimes case 'a': 2091590Srgrimes i = strlen(argv[1]) - 2; 2101590Srgrimes if (i % 6 != 0) 2111590Srgrimes usage(); 2121590Srgrimes /* look for empty macro slots */ 2131590Srgrimes for (i=0; br[i].opbr; i++) 2141590Srgrimes ; 2151590Srgrimes for (cp=argv[1]+3; cp[-1]; cp += 6) { 21691389Sdwmalone br[i].opbr = strncpy(malloc(3), cp, 2); 21791389Sdwmalone br[i].clbr = strncpy(malloc(3), cp+3, 2); 2181590Srgrimes addmac(br[i].opbr); /* knows pairs are also known cmds */ 2191590Srgrimes addmac(br[i].clbr); 2201590Srgrimes i++; 2211590Srgrimes } 2221590Srgrimes break; 2231590Srgrimes 2241590Srgrimes /* -c: add known commands */ 2251590Srgrimes case 'c': 2261590Srgrimes i = strlen(argv[1]) - 2; 2271590Srgrimes if (i % 3 != 0) 2281590Srgrimes usage(); 2291590Srgrimes for (cp=argv[1]+3; cp[-1]; cp += 3) { 2301590Srgrimes if (cp[2] && cp[2] != '.') 2311590Srgrimes usage(); 2321590Srgrimes strncpy(b1, cp, 2); 2339376Sasami b1[2] = '\0'; 2341590Srgrimes addmac(b1); 2351590Srgrimes } 2361590Srgrimes break; 2371590Srgrimes 2381590Srgrimes /* -f: ignore font changes */ 2391590Srgrimes case 'f': 2401590Srgrimes fflag = 1; 2411590Srgrimes break; 2421590Srgrimes 2431590Srgrimes /* -s: ignore size changes */ 2441590Srgrimes case 's': 2451590Srgrimes sflag = 1; 2461590Srgrimes break; 2471590Srgrimes default: 2481590Srgrimes usage(); 2491590Srgrimes } 2501590Srgrimes argc--; argv++; 2511590Srgrimes } 2521590Srgrimes 2531590Srgrimes nfiles = argc - 1; 2541590Srgrimes 2551590Srgrimes if (nfiles > 0) { 2561590Srgrimes for (i=1; i<argc; i++) { 2571590Srgrimes cfilename = argv[i]; 2581590Srgrimes f = fopen(cfilename, "r"); 2591590Srgrimes if (f == NULL) 2601590Srgrimes perror(cfilename); 2611590Srgrimes else 2621590Srgrimes process(f); 2631590Srgrimes } 2641590Srgrimes } else { 2651590Srgrimes cfilename = "stdin"; 2661590Srgrimes process(stdin); 2671590Srgrimes } 2681590Srgrimes exit(0); 2691590Srgrimes} 2701590Srgrimes 27126881Scharnierstatic void 272100813Sdwmaloneusage(void) 2731590Srgrimes{ 27426881Scharnier fprintf(stderr, 27526881Scharnier "usage: checknr [-a.xx.yy.xx.yy...] [-c.xx.xx.xx...] [-s] [-f] file\n"); 2761590Srgrimes exit(1); 2771590Srgrimes} 2781590Srgrimes 27926881Scharniervoid 280100813Sdwmaloneprocess(FILE *f) 2811590Srgrimes{ 28291389Sdwmalone int i, n; 2831590Srgrimes char mac[5]; /* The current macro or nroff command */ 2841590Srgrimes int pl; 28591389Sdwmalone static char line[256]; /* the current line */ 2861590Srgrimes 2871590Srgrimes stktop = -1; 2881590Srgrimes for (lineno = 1; fgets(line, sizeof line, f); lineno++) { 2891590Srgrimes if (line[0] == '.') { 2901590Srgrimes /* 2911590Srgrimes * find and isolate the macro/command name. 2921590Srgrimes */ 2931590Srgrimes strncpy(mac, line+1, 4); 2941590Srgrimes if (isspace(mac[0])) { 2951590Srgrimes pe(lineno); 2961590Srgrimes printf("Empty command\n"); 2971590Srgrimes } else if (isspace(mac[1])) { 2981590Srgrimes mac[1] = 0; 2991590Srgrimes } else if (isspace(mac[2])) { 3001590Srgrimes mac[2] = 0; 3011590Srgrimes } else if (mac[0] != '\\' || mac[1] != '\"') { 3021590Srgrimes pe(lineno); 3031590Srgrimes printf("Command too long\n"); 3041590Srgrimes } 3051590Srgrimes 3061590Srgrimes /* 3071590Srgrimes * Is it a known command? 3081590Srgrimes */ 3091590Srgrimes checkknown(mac); 3101590Srgrimes 3111590Srgrimes /* 3121590Srgrimes * Should we add it? 3131590Srgrimes */ 3141590Srgrimes if (eq(mac, "de")) 3151590Srgrimes addcmd(line); 3161590Srgrimes 3171590Srgrimes chkcmd(line, mac); 3181590Srgrimes } 3191590Srgrimes 3201590Srgrimes /* 3211590Srgrimes * At this point we process the line looking 3221590Srgrimes * for \s and \f. 3231590Srgrimes */ 3241590Srgrimes for (i=0; line[i]; i++) 3251590Srgrimes if (line[i]=='\\' && (i==0 || line[i-1]!='\\')) { 3261590Srgrimes if (!sflag && line[++i]=='s') { 3271590Srgrimes pl = line[++i]; 3281590Srgrimes if (isdigit(pl)) { 3291590Srgrimes n = pl - '0'; 3301590Srgrimes pl = ' '; 3311590Srgrimes } else 3321590Srgrimes n = 0; 3331590Srgrimes while (isdigit(line[++i])) 3341590Srgrimes n = 10 * n + line[i] - '0'; 3351590Srgrimes i--; 3361590Srgrimes if (n == 0) { 3371590Srgrimes if (stk[stktop].opno == SZ) { 3381590Srgrimes stktop--; 3391590Srgrimes } else { 3401590Srgrimes pe(lineno); 3411590Srgrimes printf("unmatched \\s0\n"); 3421590Srgrimes } 3431590Srgrimes } else { 3441590Srgrimes stk[++stktop].opno = SZ; 3451590Srgrimes stk[stktop].pl = pl; 3461590Srgrimes stk[stktop].parm = n; 3471590Srgrimes stk[stktop].lno = lineno; 3481590Srgrimes } 3491590Srgrimes } else if (!fflag && line[i]=='f') { 3501590Srgrimes n = line[++i]; 3511590Srgrimes if (n == 'P') { 3521590Srgrimes if (stk[stktop].opno == FT) { 3531590Srgrimes stktop--; 3541590Srgrimes } else { 3551590Srgrimes pe(lineno); 3561590Srgrimes printf("unmatched \\fP\n"); 3571590Srgrimes } 3581590Srgrimes } else { 3591590Srgrimes stk[++stktop].opno = FT; 3601590Srgrimes stk[stktop].pl = 1; 3611590Srgrimes stk[stktop].parm = n; 3621590Srgrimes stk[stktop].lno = lineno; 3631590Srgrimes } 3641590Srgrimes } 3651590Srgrimes } 3661590Srgrimes } 3671590Srgrimes /* 3681590Srgrimes * We've hit the end and look at all this stuff that hasn't been 3691590Srgrimes * matched yet! Complain, complain. 3701590Srgrimes */ 3711590Srgrimes for (i=stktop; i>=0; i--) { 3721590Srgrimes complain(i); 3731590Srgrimes } 3741590Srgrimes} 3751590Srgrimes 37626881Scharniervoid 377100813Sdwmalonecomplain(int i) 3781590Srgrimes{ 3791590Srgrimes pe(stk[i].lno); 3801590Srgrimes printf("Unmatched "); 3811590Srgrimes prop(i); 3821590Srgrimes printf("\n"); 3831590Srgrimes} 3841590Srgrimes 38526881Scharniervoid 386100813Sdwmaloneprop(int i) 3871590Srgrimes{ 3881590Srgrimes if (stk[i].pl == 0) 3891590Srgrimes printf(".%s", br[stk[i].opno].opbr); 3901590Srgrimes else switch(stk[i].opno) { 3911590Srgrimes case SZ: 3921590Srgrimes printf("\\s%c%d", stk[i].pl, stk[i].parm); 3931590Srgrimes break; 3941590Srgrimes case FT: 3951590Srgrimes printf("\\f%c", stk[i].parm); 3961590Srgrimes break; 3971590Srgrimes default: 3981590Srgrimes printf("Bug: stk[%d].opno = %d = .%s, .%s", 3991590Srgrimes i, stk[i].opno, br[stk[i].opno].opbr, br[stk[i].opno].clbr); 4001590Srgrimes } 4011590Srgrimes} 4021590Srgrimes 40326881Scharniervoid 404100813Sdwmalonechkcmd(const char *line __unused, const char *mac) 4051590Srgrimes{ 40691389Sdwmalone int i; 4071590Srgrimes 4081590Srgrimes /* 4091590Srgrimes * Check to see if it matches top of stack. 4101590Srgrimes */ 4111590Srgrimes if (stktop >= 0 && eq(mac, br[stk[stktop].opno].clbr)) 4121590Srgrimes stktop--; /* OK. Pop & forget */ 4131590Srgrimes else { 4141590Srgrimes /* No. Maybe it's an opener */ 4151590Srgrimes for (i=0; br[i].opbr; i++) { 4161590Srgrimes if (eq(mac, br[i].opbr)) { 4171590Srgrimes /* Found. Push it. */ 4181590Srgrimes stktop++; 4191590Srgrimes stk[stktop].opno = i; 4201590Srgrimes stk[stktop].pl = 0; 4211590Srgrimes stk[stktop].parm = 0; 4221590Srgrimes stk[stktop].lno = lineno; 4231590Srgrimes break; 4241590Srgrimes } 4251590Srgrimes /* 4261590Srgrimes * Maybe it's an unmatched closer. 4271590Srgrimes * NOTE: this depends on the fact 4281590Srgrimes * that none of the closers can be 4291590Srgrimes * openers too. 4301590Srgrimes */ 4311590Srgrimes if (eq(mac, br[i].clbr)) { 4321590Srgrimes nomatch(mac); 4331590Srgrimes break; 4341590Srgrimes } 4351590Srgrimes } 4361590Srgrimes } 4371590Srgrimes} 4381590Srgrimes 43926881Scharniervoid 440100813Sdwmalonenomatch(const char *mac) 4411590Srgrimes{ 44291389Sdwmalone int i, j; 4431590Srgrimes 4441590Srgrimes /* 4451590Srgrimes * Look for a match further down on stack 4461590Srgrimes * If we find one, it suggests that the stuff in 4471590Srgrimes * between is supposed to match itself. 4481590Srgrimes */ 4491590Srgrimes for (j=stktop; j>=0; j--) 4501590Srgrimes if (eq(mac,br[stk[j].opno].clbr)) { 4511590Srgrimes /* Found. Make a good diagnostic. */ 4521590Srgrimes if (j == stktop-2) { 4531590Srgrimes /* 4541590Srgrimes * Check for special case \fx..\fR and don't 4551590Srgrimes * complain. 4561590Srgrimes */ 4571590Srgrimes if (stk[j+1].opno==FT && stk[j+1].parm!='R' 4581590Srgrimes && stk[j+2].opno==FT && stk[j+2].parm=='R') { 4591590Srgrimes stktop = j -1; 4601590Srgrimes return; 4611590Srgrimes } 4621590Srgrimes /* 4631590Srgrimes * We have two unmatched frobs. Chances are 4641590Srgrimes * they were intended to match, so we mention 4651590Srgrimes * them together. 4661590Srgrimes */ 4671590Srgrimes pe(stk[j+1].lno); 4681590Srgrimes prop(j+1); 4691590Srgrimes printf(" does not match %d: ", stk[j+2].lno); 4701590Srgrimes prop(j+2); 4711590Srgrimes printf("\n"); 4721590Srgrimes } else for (i=j+1; i <= stktop; i++) { 4731590Srgrimes complain(i); 4741590Srgrimes } 4751590Srgrimes stktop = j-1; 4761590Srgrimes return; 4771590Srgrimes } 4781590Srgrimes /* Didn't find one. Throw this away. */ 4791590Srgrimes pe(lineno); 4801590Srgrimes printf("Unmatched .%s\n", mac); 4811590Srgrimes} 4821590Srgrimes 4831590Srgrimes/* eq: are two strings equal? */ 48426881Scharnierint 485100813Sdwmaloneeq(const char *s1, const char *s2) 4861590Srgrimes{ 4871590Srgrimes return (strcmp(s1, s2) == 0); 4881590Srgrimes} 4891590Srgrimes 4901590Srgrimes/* print the first part of an error message, given the line number */ 49126881Scharniervoid 492100813Sdwmalonepe(int linen) 4931590Srgrimes{ 4941590Srgrimes if (nfiles > 1) 4951590Srgrimes printf("%s: ", cfilename); 49691389Sdwmalone printf("%d: ", linen); 4971590Srgrimes} 4981590Srgrimes 49926881Scharniervoid 500100813Sdwmalonecheckknown(const char *mac) 5011590Srgrimes{ 5021590Srgrimes 5031590Srgrimes if (eq(mac, ".")) 5041590Srgrimes return; 5051590Srgrimes if (binsrch(mac) >= 0) 5061590Srgrimes return; 5071590Srgrimes if (mac[0] == '\\' && mac[1] == '"') /* comments */ 5081590Srgrimes return; 5091590Srgrimes 5101590Srgrimes pe(lineno); 5111590Srgrimes printf("Unknown command: .%s\n", mac); 5121590Srgrimes} 5131590Srgrimes 5141590Srgrimes/* 5151590Srgrimes * We have a .de xx line in "line". Add xx to the list of known commands. 5161590Srgrimes */ 51726881Scharniervoid 518100813Sdwmaloneaddcmd(char *line) 5191590Srgrimes{ 5201590Srgrimes char *mac; 5211590Srgrimes 5221590Srgrimes /* grab the macro being defined */ 5231590Srgrimes mac = line+4; 5241590Srgrimes while (isspace(*mac)) 5251590Srgrimes mac++; 5261590Srgrimes if (*mac == 0) { 5271590Srgrimes pe(lineno); 5281590Srgrimes printf("illegal define: %s\n", line); 5291590Srgrimes return; 5301590Srgrimes } 5311590Srgrimes mac[2] = 0; 5321590Srgrimes if (isspace(mac[1]) || mac[1] == '\\') 5331590Srgrimes mac[1] = 0; 5341590Srgrimes if (ncmds >= MAXCMDS) { 5351590Srgrimes printf("Only %d known commands allowed\n", MAXCMDS); 5361590Srgrimes exit(1); 5371590Srgrimes } 5381590Srgrimes addmac(mac); 5391590Srgrimes} 5401590Srgrimes 5411590Srgrimes/* 5421590Srgrimes * Add mac to the list. We should really have some kind of tree 5431590Srgrimes * structure here but this is a quick-and-dirty job and I just don't 5441590Srgrimes * have time to mess with it. (I wonder if this will come back to haunt 5451590Srgrimes * me someday?) Anyway, I claim that .de is fairly rare in user 5461590Srgrimes * nroff programs, and the register loop below is pretty fast. 5471590Srgrimes */ 54826881Scharniervoid 549100813Sdwmaloneaddmac(const char *mac) 5501590Srgrimes{ 55191389Sdwmalone const char **src, **dest, **loc; 5521590Srgrimes 5531590Srgrimes if (binsrch(mac) >= 0){ /* it's OK to redefine something */ 5541590Srgrimes#ifdef DEBUG 5551590Srgrimes printf("binsrch(%s) -> already in table\n", mac); 55691389Sdwmalone#endif 5571590Srgrimes return; 5581590Srgrimes } 5591590Srgrimes /* binsrch sets slot as a side effect */ 5601590Srgrimes#ifdef DEBUG 5611590Srgrimesprintf("binsrch(%s) -> %d\n", mac, slot); 5621590Srgrimes#endif 5631590Srgrimes loc = &knowncmds[slot]; 5641590Srgrimes src = &knowncmds[ncmds-1]; 5651590Srgrimes dest = src+1; 5661590Srgrimes while (dest > loc) 5671590Srgrimes *dest-- = *src--; 56891389Sdwmalone *loc = strcpy(malloc(3), mac); 5691590Srgrimes ncmds++; 5701590Srgrimes#ifdef DEBUG 5711590Srgrimesprintf("after: %s %s %s %s %s, %d cmds\n", knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot], knowncmds[slot+1], knowncmds[slot+2], ncmds); 5721590Srgrimes#endif 5731590Srgrimes} 5741590Srgrimes 5751590Srgrimes/* 5761590Srgrimes * Do a binary search in knowncmds for mac. 5771590Srgrimes * If found, return the index. If not, return -1. 5781590Srgrimes */ 57926881Scharnierint 580100813Sdwmalonebinsrch(const char *mac) 5811590Srgrimes{ 58291389Sdwmalone const char *p; /* pointer to current cmd in list */ 58391389Sdwmalone int d; /* difference if any */ 58491389Sdwmalone int mid; /* mid point in binary search */ 58591389Sdwmalone int top, bot; /* boundaries of bin search, inclusive */ 5861590Srgrimes 5871590Srgrimes top = ncmds-1; 5881590Srgrimes bot = 0; 5891590Srgrimes while (top >= bot) { 5901590Srgrimes mid = (top+bot)/2; 5911590Srgrimes p = knowncmds[mid]; 5921590Srgrimes d = p[0] - mac[0]; 5931590Srgrimes if (d == 0) 5941590Srgrimes d = p[1] - mac[1]; 5951590Srgrimes if (d == 0) 5961590Srgrimes return mid; 5971590Srgrimes if (d < 0) 5981590Srgrimes bot = mid + 1; 5991590Srgrimes else 6001590Srgrimes top = mid - 1; 6011590Srgrimes } 6021590Srgrimes slot = bot; /* place it would have gone */ 6031590Srgrimes return -1; 6041590Srgrimes} 605