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 * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
301590Srgrimes#ifndef lint
3141568Sarchiestatic const char copyright[] =
321590Srgrimes"@(#) Copyright (c) 1980, 1993\n\
331590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
341590Srgrimes#endif /* not lint */
351590Srgrimes
3691389Sdwmalone#if 0
371590Srgrimes#ifndef lint
3891389Sdwmalonestatic char sccsid[] = "@(#)checknr.c	8.1 (Berkeley) 6/6/93";
391590Srgrimes#endif /* not lint */
4091389Sdwmalone#endif
411590Srgrimes
4291389Sdwmalone#include <sys/cdefs.h>
4391389Sdwmalone__FBSDID("$FreeBSD$");
4491389Sdwmalone
451590Srgrimes/*
461590Srgrimes * checknr: check an nroff/troff input file for matching macro calls.
471590Srgrimes * we also attempt to match size and font changes, but only the embedded
481590Srgrimes * kind.  These must end in \s0 and \fP resp.  Maybe more sophistication
491590Srgrimes * later but for now think of these restrictions as contributions to
501590Srgrimes * structured typesetting.
511590Srgrimes */
52132178Stjr#include <err.h>
531590Srgrimes#include <stdio.h>
5426881Scharnier#include <stdlib.h>
5526881Scharnier#include <string.h>
561590Srgrimes#include <ctype.h>
571590Srgrimes
581590Srgrimes#define MAXSTK	100	/* Stack size */
591590Srgrimes#define MAXBR	100	/* Max number of bracket pairs known */
601590Srgrimes#define MAXCMDS	500	/* Max number of commands known */
611590Srgrimes
62227234Sedstatic void addcmd(char *);
63227234Sedstatic void addmac(const char *);
64227234Sedstatic int binsrch(const char *);
65227234Sedstatic void checkknown(const char *);
66227234Sedstatic void chkcmd(const char *, const char *);
67227234Sedstatic void complain(int);
68227234Sedstatic int eq(const char *, const char *);
69227234Sedstatic void nomatch(const char *);
70227234Sedstatic void pe(int);
71227234Sedstatic void process(FILE *);
72227234Sedstatic void prop(int);
7392920Simpstatic void usage(void);
7426881Scharnier
751590Srgrimes/*
761590Srgrimes * The stack on which we remember what we've seen so far.
771590Srgrimes */
78227234Sedstatic struct stkstr {
791590Srgrimes	int opno;	/* number of opening bracket */
801590Srgrimes	int pl;		/* '+', '-', ' ' for \s, 1 for \f, 0 for .ft */
811590Srgrimes	int parm;	/* parm to size, font, etc */
82219126Sbrucec	int lno;	/* line number */
831590Srgrimes} stk[MAXSTK];
84227234Sedstatic int stktop;
851590Srgrimes
861590Srgrimes/*
871590Srgrimes * The kinds of opening and closing brackets.
881590Srgrimes */
89227234Sedstatic struct brstr {
9091389Sdwmalone	const char *opbr;
9191389Sdwmalone	const char *clbr;
921590Srgrimes} br[MAXBR] = {
931590Srgrimes	/* A few bare bones troff commands */
941590Srgrimes#define SZ	0
9526881Scharnier	{"sz",	"sz"},	/* also \s */
961590Srgrimes#define FT	1
9726881Scharnier	{"ft",	"ft"},	/* also \f */
981590Srgrimes	/* the -mm package */
9926881Scharnier	{"AL",	"LE"},
10026881Scharnier	{"AS",	"AE"},
10126881Scharnier	{"BL",	"LE"},
10226881Scharnier	{"BS",	"BE"},
10326881Scharnier	{"DF",	"DE"},
10426881Scharnier	{"DL",	"LE"},
10526881Scharnier	{"DS",	"DE"},
10626881Scharnier	{"FS",	"FE"},
10726881Scharnier	{"ML",	"LE"},
10826881Scharnier	{"NS",	"NE"},
10926881Scharnier	{"RL",	"LE"},
11026881Scharnier	{"VL",	"LE"},
1111590Srgrimes	/* the -ms package */
11226881Scharnier	{"AB",	"AE"},
11326881Scharnier	{"BD",	"DE"},
11426881Scharnier	{"CD",	"DE"},
11526881Scharnier	{"DS",	"DE"},
11626881Scharnier	{"FS",	"FE"},
11726881Scharnier	{"ID",	"DE"},
11826881Scharnier	{"KF",	"KE"},
11926881Scharnier	{"KS",	"KE"},
12026881Scharnier	{"LD",	"DE"},
12126881Scharnier	{"LG",	"NL"},
12226881Scharnier	{"QS",	"QE"},
12326881Scharnier	{"RS",	"RE"},
12426881Scharnier	{"SM",	"NL"},
12526881Scharnier	{"XA",	"XE"},
12626881Scharnier	{"XS",	"XE"},
1271590Srgrimes	/* The -me package */
12826881Scharnier	{"(b",	")b"},
12926881Scharnier	{"(c",	")c"},
13026881Scharnier	{"(d",	")d"},
13126881Scharnier	{"(f",	")f"},
13226881Scharnier	{"(l",	")l"},
13326881Scharnier	{"(q",	")q"},
13426881Scharnier	{"(x",	")x"},
13526881Scharnier	{"(z",	")z"},
1361590Srgrimes	/* Things needed by preprocessors */
13726881Scharnier	{"EQ",	"EN"},
13826881Scharnier	{"TS",	"TE"},
1391590Srgrimes	/* Refer */
14026881Scharnier	{"[",	"]"},
14126881Scharnier	{0,	0}
1421590Srgrimes};
1431590Srgrimes
1441590Srgrimes/*
1451590Srgrimes * All commands known to nroff, plus macro packages.
1461590Srgrimes * Used so we can complain about unrecognized commands.
1471590Srgrimes */
148227234Sedstatic const char *knowncmds[MAXCMDS] = {
1491590Srgrimes"$c", "$f", "$h", "$p", "$s", "(b", "(c", "(d", "(f", "(l", "(q", "(t",
1501590Srgrimes"(x", "(z", ")b", ")c", ")d", ")f", ")l", ")q", ")t", ")x", ")z", "++",
1511590Srgrimes"+c", "1C", "1c", "2C", "2c", "@(", "@)", "@C", "@D", "@F", "@I", "@M",
1521590Srgrimes"@c", "@e", "@f", "@h", "@m", "@n", "@o", "@p", "@r", "@t", "@z", "AB",
1531590Srgrimes"AE", "AF", "AI", "AL", "AM", "AS", "AT", "AU", "AX", "B",  "B1", "B2",
1541590Srgrimes"BD", "BE", "BG", "BL", "BS", "BT", "BX", "C1", "C2", "CD", "CM", "CT",
1551590Srgrimes"D",  "DA", "DE", "DF", "DL", "DS", "DT", "EC", "EF", "EG", "EH", "EM",
1561590Srgrimes"EN", "EQ", "EX", "FA", "FD", "FE", "FG", "FJ", "FK", "FL", "FN", "FO",
1571590Srgrimes"FQ", "FS", "FV", "FX", "H",  "HC", "HD", "HM", "HO", "HU", "I",  "ID",
1581590Srgrimes"IE", "IH", "IM", "IP", "IX", "IZ", "KD", "KE", "KF", "KQ", "KS", "LB",
1591590Srgrimes"LC", "LD", "LE", "LG", "LI", "LP", "MC", "ME", "MF", "MH", "ML", "MR",
1601590Srgrimes"MT", "ND", "NE", "NH", "NL", "NP", "NS", "OF", "OH", "OK", "OP", "P",
1611590Srgrimes"P1", "PF", "PH", "PP", "PT", "PX", "PY", "QE", "QP", "QS", "R",  "RA",
1621590Srgrimes"RC", "RE", "RL", "RP", "RQ", "RS", "RT", "S",  "S0", "S2", "S3", "SA",
1631590Srgrimes"SG", "SH", "SK", "SM", "SP", "SY", "T&", "TA", "TB", "TC", "TD", "TE",
1641590Srgrimes"TH", "TL", "TM", "TP", "TQ", "TR", "TS", "TX", "UL", "US", "UX", "VL",
1651590Srgrimes"WC", "WH", "XA", "XD", "XE", "XF", "XK", "XP", "XS", "[",  "[-", "[0",
1661590Srgrimes"[1", "[2", "[3", "[4", "[5", "[<", "[>", "[]", "]",  "]-", "]<", "]>",
1671590Srgrimes"][", "ab", "ac", "ad", "af", "am", "ar", "as", "b",  "ba", "bc", "bd",
1681590Srgrimes"bi", "bl", "bp", "br", "bx", "c.", "c2", "cc", "ce", "cf", "ch", "cs",
1691590Srgrimes"ct", "cu", "da", "de", "di", "dl", "dn", "ds", "dt", "dw", "dy", "ec",
1701590Srgrimes"ef", "eh", "el", "em", "eo", "ep", "ev", "ex", "fc", "fi", "fl", "fo",
1711590Srgrimes"fp", "ft", "fz", "hc", "he", "hl", "hp", "ht", "hw", "hx", "hy", "i",
1721590Srgrimes"ie", "if", "ig", "in", "ip", "it", "ix", "lc", "lg", "li", "ll", "ln",
1731590Srgrimes"lo", "lp", "ls", "lt", "m1", "m2", "m3", "m4", "mc", "mk", "mo", "n1",
1741590Srgrimes"n2", "na", "ne", "nf", "nh", "nl", "nm", "nn", "np", "nr", "ns", "nx",
1751590Srgrimes"of", "oh", "os", "pa", "pc", "pi", "pl", "pm", "pn", "po", "pp", "ps",
1761590Srgrimes"q",  "r",  "rb", "rd", "re", "rm", "rn", "ro", "rr", "rs", "rt", "sb",
1771590Srgrimes"sc", "sh", "sk", "so", "sp", "ss", "st", "sv", "sz", "ta", "tc", "th",
1781590Srgrimes"ti", "tl", "tm", "tp", "tr", "u",  "uf", "uh", "ul", "vs", "wh", "xp",
1791590Srgrimes"yr", 0
1801590Srgrimes};
1811590Srgrimes
182227234Sedstatic int	lineno;		/* current line number in input file */
183227234Sedstatic const char *cfilename;	/* name of current file */
184227234Sedstatic int	nfiles;		/* number of files to process */
185227234Sedstatic int	fflag;		/* -f: ignore \f */
186227234Sedstatic int	sflag;		/* -s: ignore \s */
187227234Sedstatic int	ncmds;		/* size of knowncmds */
188227234Sedstatic int	slot;		/* slot in knowncmds found by binsrch */
1891590Srgrimes
19026881Scharnierint
191100813Sdwmalonemain(int argc, char **argv)
1921590Srgrimes{
1931590Srgrimes	FILE *f;
1941590Srgrimes	int i;
1951590Srgrimes	char *cp;
1961590Srgrimes	char b1[4];
1971590Srgrimes
1981590Srgrimes	/* Figure out how many known commands there are */
1991590Srgrimes	while (knowncmds[ncmds])
2001590Srgrimes		ncmds++;
2011590Srgrimes	while (argc > 1 && argv[1][0] == '-') {
2021590Srgrimes		switch(argv[1][1]) {
2031590Srgrimes
2041590Srgrimes		/* -a: add pairs of macros */
2051590Srgrimes		case 'a':
2061590Srgrimes			i = strlen(argv[1]) - 2;
2071590Srgrimes			if (i % 6 != 0)
2081590Srgrimes				usage();
2091590Srgrimes			/* look for empty macro slots */
2101590Srgrimes			for (i=0; br[i].opbr; i++)
2111590Srgrimes				;
2121590Srgrimes			for (cp=argv[1]+3; cp[-1]; cp += 6) {
21391389Sdwmalone				br[i].opbr = strncpy(malloc(3), cp, 2);
21491389Sdwmalone				br[i].clbr = strncpy(malloc(3), cp+3, 2);
2151590Srgrimes				addmac(br[i].opbr);	/* knows pairs are also known cmds */
2161590Srgrimes				addmac(br[i].clbr);
2171590Srgrimes				i++;
2181590Srgrimes			}
2191590Srgrimes			break;
2201590Srgrimes
2211590Srgrimes		/* -c: add known commands */
2221590Srgrimes		case 'c':
2231590Srgrimes			i = strlen(argv[1]) - 2;
2241590Srgrimes			if (i % 3 != 0)
2251590Srgrimes				usage();
2261590Srgrimes			for (cp=argv[1]+3; cp[-1]; cp += 3) {
2271590Srgrimes				if (cp[2] && cp[2] != '.')
2281590Srgrimes					usage();
2291590Srgrimes				strncpy(b1, cp, 2);
2309376Sasami				b1[2] = '\0';
2311590Srgrimes				addmac(b1);
2321590Srgrimes			}
2331590Srgrimes			break;
2341590Srgrimes
2351590Srgrimes		/* -f: ignore font changes */
2361590Srgrimes		case 'f':
2371590Srgrimes			fflag = 1;
2381590Srgrimes			break;
2391590Srgrimes
2401590Srgrimes		/* -s: ignore size changes */
2411590Srgrimes		case 's':
2421590Srgrimes			sflag = 1;
2431590Srgrimes			break;
2441590Srgrimes		default:
2451590Srgrimes			usage();
2461590Srgrimes		}
2471590Srgrimes		argc--; argv++;
2481590Srgrimes	}
2491590Srgrimes
2501590Srgrimes	nfiles = argc - 1;
2511590Srgrimes
2521590Srgrimes	if (nfiles > 0) {
2531590Srgrimes		for (i=1; i<argc; i++) {
2541590Srgrimes			cfilename = argv[i];
2551590Srgrimes			f = fopen(cfilename, "r");
2561590Srgrimes			if (f == NULL)
257132178Stjr				warn("%s", cfilename);
258115601Stjr			else {
2591590Srgrimes				process(f);
260115601Stjr				fclose(f);
261115601Stjr			}
2621590Srgrimes		}
2631590Srgrimes	} else {
2641590Srgrimes		cfilename = "stdin";
2651590Srgrimes		process(stdin);
2661590Srgrimes	}
2671590Srgrimes	exit(0);
2681590Srgrimes}
2691590Srgrimes
27026881Scharnierstatic void
271100813Sdwmaloneusage(void)
2721590Srgrimes{
27326881Scharnier	fprintf(stderr,
27426881Scharnier	"usage: checknr [-a.xx.yy.xx.yy...] [-c.xx.xx.xx...] [-s] [-f] file\n");
2751590Srgrimes	exit(1);
2761590Srgrimes}
2771590Srgrimes
278227234Sedstatic void
279100813Sdwmaloneprocess(FILE *f)
2801590Srgrimes{
28191389Sdwmalone	int i, n;
2821590Srgrimes	char mac[5];	/* The current macro or nroff command */
2831590Srgrimes	int pl;
28491389Sdwmalone	static char line[256];	/* the current line */
2851590Srgrimes
2861590Srgrimes	stktop = -1;
2871590Srgrimes	for (lineno = 1; fgets(line, sizeof line, f); lineno++) {
2881590Srgrimes		if (line[0] == '.') {
2891590Srgrimes			/*
2901590Srgrimes			 * find and isolate the macro/command name.
2911590Srgrimes			 */
2921590Srgrimes			strncpy(mac, line+1, 4);
2931590Srgrimes			if (isspace(mac[0])) {
2941590Srgrimes				pe(lineno);
2951590Srgrimes				printf("Empty command\n");
2961590Srgrimes			} else if (isspace(mac[1])) {
2971590Srgrimes				mac[1] = 0;
2981590Srgrimes			} else if (isspace(mac[2])) {
2991590Srgrimes				mac[2] = 0;
3001590Srgrimes			} else if (mac[0] != '\\' || mac[1] != '\"') {
3011590Srgrimes				pe(lineno);
3021590Srgrimes				printf("Command too long\n");
3031590Srgrimes			}
3041590Srgrimes
3051590Srgrimes			/*
3061590Srgrimes			 * Is it a known command?
3071590Srgrimes			 */
3081590Srgrimes			checkknown(mac);
3091590Srgrimes
3101590Srgrimes			/*
3111590Srgrimes			 * Should we add it?
3121590Srgrimes			 */
3131590Srgrimes			if (eq(mac, "de"))
3141590Srgrimes				addcmd(line);
3151590Srgrimes
3161590Srgrimes			chkcmd(line, mac);
3171590Srgrimes		}
3181590Srgrimes
3191590Srgrimes		/*
3201590Srgrimes		 * At this point we process the line looking
3211590Srgrimes		 * for \s and \f.
3221590Srgrimes		 */
3231590Srgrimes		for (i=0; line[i]; i++)
3241590Srgrimes			if (line[i]=='\\' && (i==0 || line[i-1]!='\\')) {
3251590Srgrimes				if (!sflag && line[++i]=='s') {
3261590Srgrimes					pl = line[++i];
3271590Srgrimes					if (isdigit(pl)) {
3281590Srgrimes						n = pl - '0';
3291590Srgrimes						pl = ' ';
3301590Srgrimes					} else
3311590Srgrimes						n = 0;
3321590Srgrimes					while (isdigit(line[++i]))
3331590Srgrimes						n = 10 * n + line[i] - '0';
3341590Srgrimes					i--;
3351590Srgrimes					if (n == 0) {
3361590Srgrimes						if (stk[stktop].opno == SZ) {
3371590Srgrimes							stktop--;
3381590Srgrimes						} else {
3391590Srgrimes							pe(lineno);
3401590Srgrimes							printf("unmatched \\s0\n");
3411590Srgrimes						}
3421590Srgrimes					} else {
3431590Srgrimes						stk[++stktop].opno = SZ;
3441590Srgrimes						stk[stktop].pl = pl;
3451590Srgrimes						stk[stktop].parm = n;
3461590Srgrimes						stk[stktop].lno = lineno;
3471590Srgrimes					}
3481590Srgrimes				} else if (!fflag && line[i]=='f') {
3491590Srgrimes					n = line[++i];
3501590Srgrimes					if (n == 'P') {
3511590Srgrimes						if (stk[stktop].opno == FT) {
3521590Srgrimes							stktop--;
3531590Srgrimes						} else {
3541590Srgrimes							pe(lineno);
3551590Srgrimes							printf("unmatched \\fP\n");
3561590Srgrimes						}
3571590Srgrimes					} else {
3581590Srgrimes						stk[++stktop].opno = FT;
3591590Srgrimes						stk[stktop].pl = 1;
3601590Srgrimes						stk[stktop].parm = n;
3611590Srgrimes						stk[stktop].lno = lineno;
3621590Srgrimes					}
3631590Srgrimes				}
3641590Srgrimes			}
3651590Srgrimes	}
3661590Srgrimes	/*
3671590Srgrimes	 * We've hit the end and look at all this stuff that hasn't been
3681590Srgrimes	 * matched yet!  Complain, complain.
3691590Srgrimes	 */
3701590Srgrimes	for (i=stktop; i>=0; i--) {
3711590Srgrimes		complain(i);
3721590Srgrimes	}
3731590Srgrimes}
3741590Srgrimes
375227234Sedstatic void
376100813Sdwmalonecomplain(int i)
3771590Srgrimes{
3781590Srgrimes	pe(stk[i].lno);
3791590Srgrimes	printf("Unmatched ");
3801590Srgrimes	prop(i);
3811590Srgrimes	printf("\n");
3821590Srgrimes}
3831590Srgrimes
384227234Sedstatic void
385100813Sdwmaloneprop(int i)
3861590Srgrimes{
3871590Srgrimes	if (stk[i].pl == 0)
3881590Srgrimes		printf(".%s", br[stk[i].opno].opbr);
3891590Srgrimes	else switch(stk[i].opno) {
3901590Srgrimes	case SZ:
3911590Srgrimes		printf("\\s%c%d", stk[i].pl, stk[i].parm);
3921590Srgrimes		break;
3931590Srgrimes	case FT:
3941590Srgrimes		printf("\\f%c", stk[i].parm);
3951590Srgrimes		break;
3961590Srgrimes	default:
3971590Srgrimes		printf("Bug: stk[%d].opno = %d = .%s, .%s",
3981590Srgrimes			i, stk[i].opno, br[stk[i].opno].opbr, br[stk[i].opno].clbr);
3991590Srgrimes	}
4001590Srgrimes}
4011590Srgrimes
402227234Sedstatic void
403100813Sdwmalonechkcmd(const char *line __unused, const char *mac)
4041590Srgrimes{
40591389Sdwmalone	int i;
4061590Srgrimes
4071590Srgrimes	/*
4081590Srgrimes	 * Check to see if it matches top of stack.
4091590Srgrimes	 */
4101590Srgrimes	if (stktop >= 0 && eq(mac, br[stk[stktop].opno].clbr))
4111590Srgrimes		stktop--;	/* OK. Pop & forget */
4121590Srgrimes	else {
4131590Srgrimes		/* No. Maybe it's an opener */
4141590Srgrimes		for (i=0; br[i].opbr; i++) {
4151590Srgrimes			if (eq(mac, br[i].opbr)) {
4161590Srgrimes				/* Found. Push it. */
4171590Srgrimes				stktop++;
4181590Srgrimes				stk[stktop].opno = i;
4191590Srgrimes				stk[stktop].pl = 0;
4201590Srgrimes				stk[stktop].parm = 0;
4211590Srgrimes				stk[stktop].lno = lineno;
4221590Srgrimes				break;
4231590Srgrimes			}
4241590Srgrimes			/*
4251590Srgrimes			 * Maybe it's an unmatched closer.
4261590Srgrimes			 * NOTE: this depends on the fact
4271590Srgrimes			 * that none of the closers can be
4281590Srgrimes			 * openers too.
4291590Srgrimes			 */
4301590Srgrimes			if (eq(mac, br[i].clbr)) {
4311590Srgrimes				nomatch(mac);
4321590Srgrimes				break;
4331590Srgrimes			}
4341590Srgrimes		}
4351590Srgrimes	}
4361590Srgrimes}
4371590Srgrimes
438227234Sedstatic void
439100813Sdwmalonenomatch(const char *mac)
4401590Srgrimes{
44191389Sdwmalone	int i, j;
4421590Srgrimes
4431590Srgrimes	/*
4441590Srgrimes	 * Look for a match further down on stack
4451590Srgrimes	 * If we find one, it suggests that the stuff in
4461590Srgrimes	 * between is supposed to match itself.
4471590Srgrimes	 */
4481590Srgrimes	for (j=stktop; j>=0; j--)
4491590Srgrimes		if (eq(mac,br[stk[j].opno].clbr)) {
4501590Srgrimes			/* Found.  Make a good diagnostic. */
4511590Srgrimes			if (j == stktop-2) {
4521590Srgrimes				/*
4531590Srgrimes				 * Check for special case \fx..\fR and don't
4541590Srgrimes				 * complain.
4551590Srgrimes				 */
4561590Srgrimes				if (stk[j+1].opno==FT && stk[j+1].parm!='R'
4571590Srgrimes				 && stk[j+2].opno==FT && stk[j+2].parm=='R') {
4581590Srgrimes					stktop = j -1;
4591590Srgrimes					return;
4601590Srgrimes				}
4611590Srgrimes				/*
4621590Srgrimes				 * We have two unmatched frobs.  Chances are
4631590Srgrimes				 * they were intended to match, so we mention
4641590Srgrimes				 * them together.
4651590Srgrimes				 */
4661590Srgrimes				pe(stk[j+1].lno);
4671590Srgrimes				prop(j+1);
4681590Srgrimes				printf(" does not match %d: ", stk[j+2].lno);
4691590Srgrimes				prop(j+2);
4701590Srgrimes				printf("\n");
4711590Srgrimes			} else for (i=j+1; i <= stktop; i++) {
4721590Srgrimes				complain(i);
4731590Srgrimes			}
4741590Srgrimes			stktop = j-1;
4751590Srgrimes			return;
4761590Srgrimes		}
4771590Srgrimes	/* Didn't find one.  Throw this away. */
4781590Srgrimes	pe(lineno);
4791590Srgrimes	printf("Unmatched .%s\n", mac);
4801590Srgrimes}
4811590Srgrimes
4821590Srgrimes/* eq: are two strings equal? */
483227234Sedstatic int
484100813Sdwmaloneeq(const char *s1, const char *s2)
4851590Srgrimes{
4861590Srgrimes	return (strcmp(s1, s2) == 0);
4871590Srgrimes}
4881590Srgrimes
4891590Srgrimes/* print the first part of an error message, given the line number */
490227234Sedstatic void
491100813Sdwmalonepe(int linen)
4921590Srgrimes{
4931590Srgrimes	if (nfiles > 1)
4941590Srgrimes		printf("%s: ", cfilename);
49591389Sdwmalone	printf("%d: ", linen);
4961590Srgrimes}
4971590Srgrimes
498227234Sedstatic void
499100813Sdwmalonecheckknown(const char *mac)
5001590Srgrimes{
5011590Srgrimes
5021590Srgrimes	if (eq(mac, "."))
5031590Srgrimes		return;
5041590Srgrimes	if (binsrch(mac) >= 0)
5051590Srgrimes		return;
5061590Srgrimes	if (mac[0] == '\\' && mac[1] == '"')	/* comments */
5071590Srgrimes		return;
5081590Srgrimes
5091590Srgrimes	pe(lineno);
5101590Srgrimes	printf("Unknown command: .%s\n", mac);
5111590Srgrimes}
5121590Srgrimes
5131590Srgrimes/*
5141590Srgrimes * We have a .de xx line in "line".  Add xx to the list of known commands.
5151590Srgrimes */
516227234Sedstatic void
517100813Sdwmaloneaddcmd(char *line)
5181590Srgrimes{
5191590Srgrimes	char *mac;
5201590Srgrimes
5211590Srgrimes	/* grab the macro being defined */
5221590Srgrimes	mac = line+4;
5231590Srgrimes	while (isspace(*mac))
5241590Srgrimes		mac++;
5251590Srgrimes	if (*mac == 0) {
5261590Srgrimes		pe(lineno);
5271590Srgrimes		printf("illegal define: %s\n", line);
5281590Srgrimes		return;
5291590Srgrimes	}
5301590Srgrimes	mac[2] = 0;
5311590Srgrimes	if (isspace(mac[1]) || mac[1] == '\\')
5321590Srgrimes		mac[1] = 0;
5331590Srgrimes	if (ncmds >= MAXCMDS) {
5341590Srgrimes		printf("Only %d known commands allowed\n", MAXCMDS);
5351590Srgrimes		exit(1);
5361590Srgrimes	}
5371590Srgrimes	addmac(mac);
5381590Srgrimes}
5391590Srgrimes
5401590Srgrimes/*
5411590Srgrimes * Add mac to the list.  We should really have some kind of tree
5421590Srgrimes * structure here but this is a quick-and-dirty job and I just don't
5431590Srgrimes * have time to mess with it.  (I wonder if this will come back to haunt
5441590Srgrimes * me someday?)  Anyway, I claim that .de is fairly rare in user
5451590Srgrimes * nroff programs, and the register loop below is pretty fast.
5461590Srgrimes */
547227234Sedstatic void
548100813Sdwmaloneaddmac(const char *mac)
5491590Srgrimes{
55091389Sdwmalone	const char **src, **dest, **loc;
5511590Srgrimes
5521590Srgrimes	if (binsrch(mac) >= 0){	/* it's OK to redefine something */
5531590Srgrimes#ifdef DEBUG
5541590Srgrimes		printf("binsrch(%s) -> already in table\n", mac);
55591389Sdwmalone#endif
5561590Srgrimes		return;
5571590Srgrimes	}
5581590Srgrimes	/* binsrch sets slot as a side effect */
5591590Srgrimes#ifdef DEBUG
5601590Srgrimesprintf("binsrch(%s) -> %d\n", mac, slot);
5611590Srgrimes#endif
5621590Srgrimes	loc = &knowncmds[slot];
5631590Srgrimes	src = &knowncmds[ncmds-1];
5641590Srgrimes	dest = src+1;
5651590Srgrimes	while (dest > loc)
5661590Srgrimes		*dest-- = *src--;
56791389Sdwmalone	*loc = strcpy(malloc(3), mac);
5681590Srgrimes	ncmds++;
5691590Srgrimes#ifdef DEBUG
5701590Srgrimesprintf("after: %s %s %s %s %s, %d cmds\n", knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot], knowncmds[slot+1], knowncmds[slot+2], ncmds);
5711590Srgrimes#endif
5721590Srgrimes}
5731590Srgrimes
5741590Srgrimes/*
5751590Srgrimes * Do a binary search in knowncmds for mac.
5761590Srgrimes * If found, return the index.  If not, return -1.
5771590Srgrimes */
578227234Sedstatic int
579100813Sdwmalonebinsrch(const char *mac)
5801590Srgrimes{
58191389Sdwmalone	const char *p;	/* pointer to current cmd in list */
58291389Sdwmalone	int d;		/* difference if any */
58391389Sdwmalone	int mid;	/* mid point in binary search */
58491389Sdwmalone	int top, bot;	/* boundaries of bin search, inclusive */
5851590Srgrimes
5861590Srgrimes	top = ncmds-1;
5871590Srgrimes	bot = 0;
5881590Srgrimes	while (top >= bot) {
5891590Srgrimes		mid = (top+bot)/2;
5901590Srgrimes		p = knowncmds[mid];
5911590Srgrimes		d = p[0] - mac[0];
5921590Srgrimes		if (d == 0)
5931590Srgrimes			d = p[1] - mac[1];
5941590Srgrimes		if (d == 0)
5951590Srgrimes			return mid;
5961590Srgrimes		if (d < 0)
5971590Srgrimes			bot = mid + 1;
5981590Srgrimes		else
5991590Srgrimes			top = mid - 1;
6001590Srgrimes	}
6011590Srgrimes	slot = bot;	/* place it would have gone */
6021590Srgrimes	return -1;
6031590Srgrimes}
604