checknr.c revision 115601
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 115601 2003-06-01 06:15:30Z tjr $"); 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); 261115601Stjr else { 2621590Srgrimes process(f); 263115601Stjr fclose(f); 264115601Stjr } 2651590Srgrimes } 2661590Srgrimes } else { 2671590Srgrimes cfilename = "stdin"; 2681590Srgrimes process(stdin); 2691590Srgrimes } 2701590Srgrimes exit(0); 2711590Srgrimes} 2721590Srgrimes 27326881Scharnierstatic void 274100813Sdwmaloneusage(void) 2751590Srgrimes{ 27626881Scharnier fprintf(stderr, 27726881Scharnier "usage: checknr [-a.xx.yy.xx.yy...] [-c.xx.xx.xx...] [-s] [-f] file\n"); 2781590Srgrimes exit(1); 2791590Srgrimes} 2801590Srgrimes 28126881Scharniervoid 282100813Sdwmaloneprocess(FILE *f) 2831590Srgrimes{ 28491389Sdwmalone int i, n; 2851590Srgrimes char mac[5]; /* The current macro or nroff command */ 2861590Srgrimes int pl; 28791389Sdwmalone static char line[256]; /* the current line */ 2881590Srgrimes 2891590Srgrimes stktop = -1; 2901590Srgrimes for (lineno = 1; fgets(line, sizeof line, f); lineno++) { 2911590Srgrimes if (line[0] == '.') { 2921590Srgrimes /* 2931590Srgrimes * find and isolate the macro/command name. 2941590Srgrimes */ 2951590Srgrimes strncpy(mac, line+1, 4); 2961590Srgrimes if (isspace(mac[0])) { 2971590Srgrimes pe(lineno); 2981590Srgrimes printf("Empty command\n"); 2991590Srgrimes } else if (isspace(mac[1])) { 3001590Srgrimes mac[1] = 0; 3011590Srgrimes } else if (isspace(mac[2])) { 3021590Srgrimes mac[2] = 0; 3031590Srgrimes } else if (mac[0] != '\\' || mac[1] != '\"') { 3041590Srgrimes pe(lineno); 3051590Srgrimes printf("Command too long\n"); 3061590Srgrimes } 3071590Srgrimes 3081590Srgrimes /* 3091590Srgrimes * Is it a known command? 3101590Srgrimes */ 3111590Srgrimes checkknown(mac); 3121590Srgrimes 3131590Srgrimes /* 3141590Srgrimes * Should we add it? 3151590Srgrimes */ 3161590Srgrimes if (eq(mac, "de")) 3171590Srgrimes addcmd(line); 3181590Srgrimes 3191590Srgrimes chkcmd(line, mac); 3201590Srgrimes } 3211590Srgrimes 3221590Srgrimes /* 3231590Srgrimes * At this point we process the line looking 3241590Srgrimes * for \s and \f. 3251590Srgrimes */ 3261590Srgrimes for (i=0; line[i]; i++) 3271590Srgrimes if (line[i]=='\\' && (i==0 || line[i-1]!='\\')) { 3281590Srgrimes if (!sflag && line[++i]=='s') { 3291590Srgrimes pl = line[++i]; 3301590Srgrimes if (isdigit(pl)) { 3311590Srgrimes n = pl - '0'; 3321590Srgrimes pl = ' '; 3331590Srgrimes } else 3341590Srgrimes n = 0; 3351590Srgrimes while (isdigit(line[++i])) 3361590Srgrimes n = 10 * n + line[i] - '0'; 3371590Srgrimes i--; 3381590Srgrimes if (n == 0) { 3391590Srgrimes if (stk[stktop].opno == SZ) { 3401590Srgrimes stktop--; 3411590Srgrimes } else { 3421590Srgrimes pe(lineno); 3431590Srgrimes printf("unmatched \\s0\n"); 3441590Srgrimes } 3451590Srgrimes } else { 3461590Srgrimes stk[++stktop].opno = SZ; 3471590Srgrimes stk[stktop].pl = pl; 3481590Srgrimes stk[stktop].parm = n; 3491590Srgrimes stk[stktop].lno = lineno; 3501590Srgrimes } 3511590Srgrimes } else if (!fflag && line[i]=='f') { 3521590Srgrimes n = line[++i]; 3531590Srgrimes if (n == 'P') { 3541590Srgrimes if (stk[stktop].opno == FT) { 3551590Srgrimes stktop--; 3561590Srgrimes } else { 3571590Srgrimes pe(lineno); 3581590Srgrimes printf("unmatched \\fP\n"); 3591590Srgrimes } 3601590Srgrimes } else { 3611590Srgrimes stk[++stktop].opno = FT; 3621590Srgrimes stk[stktop].pl = 1; 3631590Srgrimes stk[stktop].parm = n; 3641590Srgrimes stk[stktop].lno = lineno; 3651590Srgrimes } 3661590Srgrimes } 3671590Srgrimes } 3681590Srgrimes } 3691590Srgrimes /* 3701590Srgrimes * We've hit the end and look at all this stuff that hasn't been 3711590Srgrimes * matched yet! Complain, complain. 3721590Srgrimes */ 3731590Srgrimes for (i=stktop; i>=0; i--) { 3741590Srgrimes complain(i); 3751590Srgrimes } 3761590Srgrimes} 3771590Srgrimes 37826881Scharniervoid 379100813Sdwmalonecomplain(int i) 3801590Srgrimes{ 3811590Srgrimes pe(stk[i].lno); 3821590Srgrimes printf("Unmatched "); 3831590Srgrimes prop(i); 3841590Srgrimes printf("\n"); 3851590Srgrimes} 3861590Srgrimes 38726881Scharniervoid 388100813Sdwmaloneprop(int i) 3891590Srgrimes{ 3901590Srgrimes if (stk[i].pl == 0) 3911590Srgrimes printf(".%s", br[stk[i].opno].opbr); 3921590Srgrimes else switch(stk[i].opno) { 3931590Srgrimes case SZ: 3941590Srgrimes printf("\\s%c%d", stk[i].pl, stk[i].parm); 3951590Srgrimes break; 3961590Srgrimes case FT: 3971590Srgrimes printf("\\f%c", stk[i].parm); 3981590Srgrimes break; 3991590Srgrimes default: 4001590Srgrimes printf("Bug: stk[%d].opno = %d = .%s, .%s", 4011590Srgrimes i, stk[i].opno, br[stk[i].opno].opbr, br[stk[i].opno].clbr); 4021590Srgrimes } 4031590Srgrimes} 4041590Srgrimes 40526881Scharniervoid 406100813Sdwmalonechkcmd(const char *line __unused, const char *mac) 4071590Srgrimes{ 40891389Sdwmalone int i; 4091590Srgrimes 4101590Srgrimes /* 4111590Srgrimes * Check to see if it matches top of stack. 4121590Srgrimes */ 4131590Srgrimes if (stktop >= 0 && eq(mac, br[stk[stktop].opno].clbr)) 4141590Srgrimes stktop--; /* OK. Pop & forget */ 4151590Srgrimes else { 4161590Srgrimes /* No. Maybe it's an opener */ 4171590Srgrimes for (i=0; br[i].opbr; i++) { 4181590Srgrimes if (eq(mac, br[i].opbr)) { 4191590Srgrimes /* Found. Push it. */ 4201590Srgrimes stktop++; 4211590Srgrimes stk[stktop].opno = i; 4221590Srgrimes stk[stktop].pl = 0; 4231590Srgrimes stk[stktop].parm = 0; 4241590Srgrimes stk[stktop].lno = lineno; 4251590Srgrimes break; 4261590Srgrimes } 4271590Srgrimes /* 4281590Srgrimes * Maybe it's an unmatched closer. 4291590Srgrimes * NOTE: this depends on the fact 4301590Srgrimes * that none of the closers can be 4311590Srgrimes * openers too. 4321590Srgrimes */ 4331590Srgrimes if (eq(mac, br[i].clbr)) { 4341590Srgrimes nomatch(mac); 4351590Srgrimes break; 4361590Srgrimes } 4371590Srgrimes } 4381590Srgrimes } 4391590Srgrimes} 4401590Srgrimes 44126881Scharniervoid 442100813Sdwmalonenomatch(const char *mac) 4431590Srgrimes{ 44491389Sdwmalone int i, j; 4451590Srgrimes 4461590Srgrimes /* 4471590Srgrimes * Look for a match further down on stack 4481590Srgrimes * If we find one, it suggests that the stuff in 4491590Srgrimes * between is supposed to match itself. 4501590Srgrimes */ 4511590Srgrimes for (j=stktop; j>=0; j--) 4521590Srgrimes if (eq(mac,br[stk[j].opno].clbr)) { 4531590Srgrimes /* Found. Make a good diagnostic. */ 4541590Srgrimes if (j == stktop-2) { 4551590Srgrimes /* 4561590Srgrimes * Check for special case \fx..\fR and don't 4571590Srgrimes * complain. 4581590Srgrimes */ 4591590Srgrimes if (stk[j+1].opno==FT && stk[j+1].parm!='R' 4601590Srgrimes && stk[j+2].opno==FT && stk[j+2].parm=='R') { 4611590Srgrimes stktop = j -1; 4621590Srgrimes return; 4631590Srgrimes } 4641590Srgrimes /* 4651590Srgrimes * We have two unmatched frobs. Chances are 4661590Srgrimes * they were intended to match, so we mention 4671590Srgrimes * them together. 4681590Srgrimes */ 4691590Srgrimes pe(stk[j+1].lno); 4701590Srgrimes prop(j+1); 4711590Srgrimes printf(" does not match %d: ", stk[j+2].lno); 4721590Srgrimes prop(j+2); 4731590Srgrimes printf("\n"); 4741590Srgrimes } else for (i=j+1; i <= stktop; i++) { 4751590Srgrimes complain(i); 4761590Srgrimes } 4771590Srgrimes stktop = j-1; 4781590Srgrimes return; 4791590Srgrimes } 4801590Srgrimes /* Didn't find one. Throw this away. */ 4811590Srgrimes pe(lineno); 4821590Srgrimes printf("Unmatched .%s\n", mac); 4831590Srgrimes} 4841590Srgrimes 4851590Srgrimes/* eq: are two strings equal? */ 48626881Scharnierint 487100813Sdwmaloneeq(const char *s1, const char *s2) 4881590Srgrimes{ 4891590Srgrimes return (strcmp(s1, s2) == 0); 4901590Srgrimes} 4911590Srgrimes 4921590Srgrimes/* print the first part of an error message, given the line number */ 49326881Scharniervoid 494100813Sdwmalonepe(int linen) 4951590Srgrimes{ 4961590Srgrimes if (nfiles > 1) 4971590Srgrimes printf("%s: ", cfilename); 49891389Sdwmalone printf("%d: ", linen); 4991590Srgrimes} 5001590Srgrimes 50126881Scharniervoid 502100813Sdwmalonecheckknown(const char *mac) 5031590Srgrimes{ 5041590Srgrimes 5051590Srgrimes if (eq(mac, ".")) 5061590Srgrimes return; 5071590Srgrimes if (binsrch(mac) >= 0) 5081590Srgrimes return; 5091590Srgrimes if (mac[0] == '\\' && mac[1] == '"') /* comments */ 5101590Srgrimes return; 5111590Srgrimes 5121590Srgrimes pe(lineno); 5131590Srgrimes printf("Unknown command: .%s\n", mac); 5141590Srgrimes} 5151590Srgrimes 5161590Srgrimes/* 5171590Srgrimes * We have a .de xx line in "line". Add xx to the list of known commands. 5181590Srgrimes */ 51926881Scharniervoid 520100813Sdwmaloneaddcmd(char *line) 5211590Srgrimes{ 5221590Srgrimes char *mac; 5231590Srgrimes 5241590Srgrimes /* grab the macro being defined */ 5251590Srgrimes mac = line+4; 5261590Srgrimes while (isspace(*mac)) 5271590Srgrimes mac++; 5281590Srgrimes if (*mac == 0) { 5291590Srgrimes pe(lineno); 5301590Srgrimes printf("illegal define: %s\n", line); 5311590Srgrimes return; 5321590Srgrimes } 5331590Srgrimes mac[2] = 0; 5341590Srgrimes if (isspace(mac[1]) || mac[1] == '\\') 5351590Srgrimes mac[1] = 0; 5361590Srgrimes if (ncmds >= MAXCMDS) { 5371590Srgrimes printf("Only %d known commands allowed\n", MAXCMDS); 5381590Srgrimes exit(1); 5391590Srgrimes } 5401590Srgrimes addmac(mac); 5411590Srgrimes} 5421590Srgrimes 5431590Srgrimes/* 5441590Srgrimes * Add mac to the list. We should really have some kind of tree 5451590Srgrimes * structure here but this is a quick-and-dirty job and I just don't 5461590Srgrimes * have time to mess with it. (I wonder if this will come back to haunt 5471590Srgrimes * me someday?) Anyway, I claim that .de is fairly rare in user 5481590Srgrimes * nroff programs, and the register loop below is pretty fast. 5491590Srgrimes */ 55026881Scharniervoid 551100813Sdwmaloneaddmac(const char *mac) 5521590Srgrimes{ 55391389Sdwmalone const char **src, **dest, **loc; 5541590Srgrimes 5551590Srgrimes if (binsrch(mac) >= 0){ /* it's OK to redefine something */ 5561590Srgrimes#ifdef DEBUG 5571590Srgrimes printf("binsrch(%s) -> already in table\n", mac); 55891389Sdwmalone#endif 5591590Srgrimes return; 5601590Srgrimes } 5611590Srgrimes /* binsrch sets slot as a side effect */ 5621590Srgrimes#ifdef DEBUG 5631590Srgrimesprintf("binsrch(%s) -> %d\n", mac, slot); 5641590Srgrimes#endif 5651590Srgrimes loc = &knowncmds[slot]; 5661590Srgrimes src = &knowncmds[ncmds-1]; 5671590Srgrimes dest = src+1; 5681590Srgrimes while (dest > loc) 5691590Srgrimes *dest-- = *src--; 57091389Sdwmalone *loc = strcpy(malloc(3), mac); 5711590Srgrimes ncmds++; 5721590Srgrimes#ifdef DEBUG 5731590Srgrimesprintf("after: %s %s %s %s %s, %d cmds\n", knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot], knowncmds[slot+1], knowncmds[slot+2], ncmds); 5741590Srgrimes#endif 5751590Srgrimes} 5761590Srgrimes 5771590Srgrimes/* 5781590Srgrimes * Do a binary search in knowncmds for mac. 5791590Srgrimes * If found, return the index. If not, return -1. 5801590Srgrimes */ 58126881Scharnierint 582100813Sdwmalonebinsrch(const char *mac) 5831590Srgrimes{ 58491389Sdwmalone const char *p; /* pointer to current cmd in list */ 58591389Sdwmalone int d; /* difference if any */ 58691389Sdwmalone int mid; /* mid point in binary search */ 58791389Sdwmalone int top, bot; /* boundaries of bin search, inclusive */ 5881590Srgrimes 5891590Srgrimes top = ncmds-1; 5901590Srgrimes bot = 0; 5911590Srgrimes while (top >= bot) { 5921590Srgrimes mid = (top+bot)/2; 5931590Srgrimes p = knowncmds[mid]; 5941590Srgrimes d = p[0] - mac[0]; 5951590Srgrimes if (d == 0) 5961590Srgrimes d = p[1] - mac[1]; 5971590Srgrimes if (d == 0) 5981590Srgrimes return mid; 5991590Srgrimes if (d < 0) 6001590Srgrimes bot = mid + 1; 6011590Srgrimes else 6021590Srgrimes top = mid - 1; 6031590Srgrimes } 6041590Srgrimes slot = bot; /* place it would have gone */ 6051590Srgrimes return -1; 6061590Srgrimes} 607