checknr.c revision 282439
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: head/usr.bin/checknr/checknr.c 282439 2015-05-04 22:27:55Z bapt $");
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>
53282439Sbapt#define _WITH_GETLINE
541590Srgrimes#include <stdio.h>
5526881Scharnier#include <stdlib.h>
5626881Scharnier#include <string.h>
571590Srgrimes#include <ctype.h>
581590Srgrimes
591590Srgrimes#define MAXSTK	100	/* Stack size */
601590Srgrimes#define MAXBR	100	/* Max number of bracket pairs known */
611590Srgrimes#define MAXCMDS	500	/* Max number of commands known */
621590Srgrimes
63227234Sedstatic void addcmd(char *);
64227234Sedstatic void addmac(const char *);
65227234Sedstatic int binsrch(const char *);
66227234Sedstatic void checkknown(const char *);
67227234Sedstatic void chkcmd(const char *, const char *);
68227234Sedstatic void complain(int);
69227234Sedstatic int eq(const char *, const char *);
70227234Sedstatic void nomatch(const char *);
71227234Sedstatic void pe(int);
72227234Sedstatic void process(FILE *);
73227234Sedstatic void prop(int);
7492920Simpstatic void usage(void);
7526881Scharnier
761590Srgrimes/*
771590Srgrimes * The stack on which we remember what we've seen so far.
781590Srgrimes */
79227234Sedstatic struct stkstr {
801590Srgrimes	int opno;	/* number of opening bracket */
811590Srgrimes	int pl;		/* '+', '-', ' ' for \s, 1 for \f, 0 for .ft */
821590Srgrimes	int parm;	/* parm to size, font, etc */
83219126Sbrucec	int lno;	/* line number */
841590Srgrimes} stk[MAXSTK];
85227234Sedstatic int stktop;
861590Srgrimes
871590Srgrimes/*
881590Srgrimes * The kinds of opening and closing brackets.
891590Srgrimes */
90227234Sedstatic struct brstr {
9191389Sdwmalone	const char *opbr;
9291389Sdwmalone	const char *clbr;
931590Srgrimes} br[MAXBR] = {
941590Srgrimes	/* A few bare bones troff commands */
951590Srgrimes#define SZ	0
9626881Scharnier	{"sz",	"sz"},	/* also \s */
971590Srgrimes#define FT	1
9826881Scharnier	{"ft",	"ft"},	/* also \f */
991590Srgrimes	/* the -mm package */
10026881Scharnier	{"AL",	"LE"},
10126881Scharnier	{"AS",	"AE"},
10226881Scharnier	{"BL",	"LE"},
10326881Scharnier	{"BS",	"BE"},
10426881Scharnier	{"DF",	"DE"},
10526881Scharnier	{"DL",	"LE"},
10626881Scharnier	{"DS",	"DE"},
10726881Scharnier	{"FS",	"FE"},
10826881Scharnier	{"ML",	"LE"},
10926881Scharnier	{"NS",	"NE"},
11026881Scharnier	{"RL",	"LE"},
11126881Scharnier	{"VL",	"LE"},
1121590Srgrimes	/* the -ms package */
11326881Scharnier	{"AB",	"AE"},
11426881Scharnier	{"BD",	"DE"},
11526881Scharnier	{"CD",	"DE"},
11626881Scharnier	{"DS",	"DE"},
11726881Scharnier	{"FS",	"FE"},
11826881Scharnier	{"ID",	"DE"},
11926881Scharnier	{"KF",	"KE"},
12026881Scharnier	{"KS",	"KE"},
12126881Scharnier	{"LD",	"DE"},
12226881Scharnier	{"LG",	"NL"},
12326881Scharnier	{"QS",	"QE"},
12426881Scharnier	{"RS",	"RE"},
12526881Scharnier	{"SM",	"NL"},
12626881Scharnier	{"XA",	"XE"},
12726881Scharnier	{"XS",	"XE"},
1281590Srgrimes	/* The -me package */
12926881Scharnier	{"(b",	")b"},
13026881Scharnier	{"(c",	")c"},
13126881Scharnier	{"(d",	")d"},
13226881Scharnier	{"(f",	")f"},
13326881Scharnier	{"(l",	")l"},
13426881Scharnier	{"(q",	")q"},
13526881Scharnier	{"(x",	")x"},
13626881Scharnier	{"(z",	")z"},
1371590Srgrimes	/* Things needed by preprocessors */
13826881Scharnier	{"EQ",	"EN"},
13926881Scharnier	{"TS",	"TE"},
1401590Srgrimes	/* Refer */
14126881Scharnier	{"[",	"]"},
14226881Scharnier	{0,	0}
1431590Srgrimes};
1441590Srgrimes
1451590Srgrimes/*
1461590Srgrimes * All commands known to nroff, plus macro packages.
1471590Srgrimes * Used so we can complain about unrecognized commands.
1481590Srgrimes */
149227234Sedstatic const char *knowncmds[MAXCMDS] = {
1501590Srgrimes"$c", "$f", "$h", "$p", "$s", "(b", "(c", "(d", "(f", "(l", "(q", "(t",
1511590Srgrimes"(x", "(z", ")b", ")c", ")d", ")f", ")l", ")q", ")t", ")x", ")z", "++",
1521590Srgrimes"+c", "1C", "1c", "2C", "2c", "@(", "@)", "@C", "@D", "@F", "@I", "@M",
1531590Srgrimes"@c", "@e", "@f", "@h", "@m", "@n", "@o", "@p", "@r", "@t", "@z", "AB",
1541590Srgrimes"AE", "AF", "AI", "AL", "AM", "AS", "AT", "AU", "AX", "B",  "B1", "B2",
1551590Srgrimes"BD", "BE", "BG", "BL", "BS", "BT", "BX", "C1", "C2", "CD", "CM", "CT",
1561590Srgrimes"D",  "DA", "DE", "DF", "DL", "DS", "DT", "EC", "EF", "EG", "EH", "EM",
1571590Srgrimes"EN", "EQ", "EX", "FA", "FD", "FE", "FG", "FJ", "FK", "FL", "FN", "FO",
1581590Srgrimes"FQ", "FS", "FV", "FX", "H",  "HC", "HD", "HM", "HO", "HU", "I",  "ID",
1591590Srgrimes"IE", "IH", "IM", "IP", "IX", "IZ", "KD", "KE", "KF", "KQ", "KS", "LB",
1601590Srgrimes"LC", "LD", "LE", "LG", "LI", "LP", "MC", "ME", "MF", "MH", "ML", "MR",
1611590Srgrimes"MT", "ND", "NE", "NH", "NL", "NP", "NS", "OF", "OH", "OK", "OP", "P",
1621590Srgrimes"P1", "PF", "PH", "PP", "PT", "PX", "PY", "QE", "QP", "QS", "R",  "RA",
1631590Srgrimes"RC", "RE", "RL", "RP", "RQ", "RS", "RT", "S",  "S0", "S2", "S3", "SA",
1641590Srgrimes"SG", "SH", "SK", "SM", "SP", "SY", "T&", "TA", "TB", "TC", "TD", "TE",
1651590Srgrimes"TH", "TL", "TM", "TP", "TQ", "TR", "TS", "TX", "UL", "US", "UX", "VL",
1661590Srgrimes"WC", "WH", "XA", "XD", "XE", "XF", "XK", "XP", "XS", "[",  "[-", "[0",
1671590Srgrimes"[1", "[2", "[3", "[4", "[5", "[<", "[>", "[]", "]",  "]-", "]<", "]>",
1681590Srgrimes"][", "ab", "ac", "ad", "af", "am", "ar", "as", "b",  "ba", "bc", "bd",
169282438Sbapt"bi", "bl", "bp", "br", "bx", "c.", "c2", "cc", "ce", "cf", "ch",
170282438Sbapt"chop", "cs", "ct", "cu", "da", "de", "di", "dl", "dn", "do", "ds",
171282438Sbapt"dt", "dw", "dy", "ec", "ef", "eh", "el", "em", "eo", "ep", "ev",
172282438Sbapt"evc", "ex", "fallback", "fc", "feature", "fi", "fl", "flig", "fo",
173282438Sbapt"fp", "ft", "ftr", "fz", "fzoom", "hc", "he", "hidechar", "hl", "hp",
174282438Sbapt"ht", "hw", "hx", "hy", "hylang", "i", "ie", "if", "ig", "in", "ip",
175282438Sbapt"it", "ix", "kern", "kernafter", "kernbefore", "kernpair", "lc", "lg",
176282438Sbapt"lhang", "lc_ctype", "li", "ll", "ln", "lo", "lp", "ls", "lt", "m1",
177282438Sbapt"m2", "m3", "m4", "mc", "mk", "mo", "n1", "n2", "na", "ne", "nf", "nh",
178282438Sbapt"nl", "nm", "nn", "np", "nr", "ns", "nx", "of", "oh", "os", "pa",
179282438Sbapt"papersize", "pc", "pi", "pl", "pm", "pn", "po", "pp", "ps", "q",
180282438Sbapt"r",  "rb", "rd", "re", "recursionlimit", "return", "rhang", "rm",
181282438Sbapt"rn", "ro", "rr", "rs", "rt", "sb", "sc", "sh", "shift", "sk", "so",
182282438Sbapt"sp", "ss", "st", "sv", "sz", "ta", "tc", "th", "ti", "tl", "tm", "tp",
183282438Sbapt"tr", "track", "u",  "uf", "uh", "ul", "vs", "wh", "xflag", "xp", "yr",
184282438Sbapt0
1851590Srgrimes};
1861590Srgrimes
187227234Sedstatic int	lineno;		/* current line number in input file */
188227234Sedstatic const char *cfilename;	/* name of current file */
189227234Sedstatic int	nfiles;		/* number of files to process */
190227234Sedstatic int	fflag;		/* -f: ignore \f */
191227234Sedstatic int	sflag;		/* -s: ignore \s */
192227234Sedstatic int	ncmds;		/* size of knowncmds */
193227234Sedstatic int	slot;		/* slot in knowncmds found by binsrch */
1941590Srgrimes
19526881Scharnierint
196100813Sdwmalonemain(int argc, char **argv)
1971590Srgrimes{
1981590Srgrimes	FILE *f;
1991590Srgrimes	int i;
2001590Srgrimes	char *cp;
2011590Srgrimes	char b1[4];
2021590Srgrimes
2031590Srgrimes	/* Figure out how many known commands there are */
2041590Srgrimes	while (knowncmds[ncmds])
2051590Srgrimes		ncmds++;
2061590Srgrimes	while (argc > 1 && argv[1][0] == '-') {
2071590Srgrimes		switch(argv[1][1]) {
2081590Srgrimes
2091590Srgrimes		/* -a: add pairs of macros */
2101590Srgrimes		case 'a':
2111590Srgrimes			i = strlen(argv[1]) - 2;
2121590Srgrimes			if (i % 6 != 0)
2131590Srgrimes				usage();
2141590Srgrimes			/* look for empty macro slots */
2151590Srgrimes			for (i=0; br[i].opbr; i++)
2161590Srgrimes				;
2171590Srgrimes			for (cp=argv[1]+3; cp[-1]; cp += 6) {
21891389Sdwmalone				br[i].opbr = strncpy(malloc(3), cp, 2);
21991389Sdwmalone				br[i].clbr = strncpy(malloc(3), cp+3, 2);
2201590Srgrimes				addmac(br[i].opbr);	/* knows pairs are also known cmds */
2211590Srgrimes				addmac(br[i].clbr);
2221590Srgrimes				i++;
2231590Srgrimes			}
2241590Srgrimes			break;
2251590Srgrimes
2261590Srgrimes		/* -c: add known commands */
2271590Srgrimes		case 'c':
2281590Srgrimes			i = strlen(argv[1]) - 2;
2291590Srgrimes			if (i % 3 != 0)
2301590Srgrimes				usage();
2311590Srgrimes			for (cp=argv[1]+3; cp[-1]; cp += 3) {
2321590Srgrimes				if (cp[2] && cp[2] != '.')
2331590Srgrimes					usage();
2341590Srgrimes				strncpy(b1, cp, 2);
2359376Sasami				b1[2] = '\0';
2361590Srgrimes				addmac(b1);
2371590Srgrimes			}
2381590Srgrimes			break;
2391590Srgrimes
2401590Srgrimes		/* -f: ignore font changes */
2411590Srgrimes		case 'f':
2421590Srgrimes			fflag = 1;
2431590Srgrimes			break;
2441590Srgrimes
2451590Srgrimes		/* -s: ignore size changes */
2461590Srgrimes		case 's':
2471590Srgrimes			sflag = 1;
2481590Srgrimes			break;
2491590Srgrimes		default:
2501590Srgrimes			usage();
2511590Srgrimes		}
2521590Srgrimes		argc--; argv++;
2531590Srgrimes	}
2541590Srgrimes
2551590Srgrimes	nfiles = argc - 1;
2561590Srgrimes
2571590Srgrimes	if (nfiles > 0) {
258282437Sbapt		for (i = 1; i < argc; i++) {
2591590Srgrimes			cfilename = argv[i];
2601590Srgrimes			f = fopen(cfilename, "r");
2611590Srgrimes			if (f == NULL)
262132178Stjr				warn("%s", cfilename);
263115601Stjr			else {
2641590Srgrimes				process(f);
265115601Stjr				fclose(f);
266115601Stjr			}
2671590Srgrimes		}
2681590Srgrimes	} else {
2691590Srgrimes		cfilename = "stdin";
2701590Srgrimes		process(stdin);
2711590Srgrimes	}
2721590Srgrimes	exit(0);
2731590Srgrimes}
2741590Srgrimes
27526881Scharnierstatic void
276100813Sdwmaloneusage(void)
2771590Srgrimes{
27826881Scharnier	fprintf(stderr,
27926881Scharnier	"usage: checknr [-a.xx.yy.xx.yy...] [-c.xx.xx.xx...] [-s] [-f] file\n");
2801590Srgrimes	exit(1);
2811590Srgrimes}
2821590Srgrimes
283227234Sedstatic void
284100813Sdwmaloneprocess(FILE *f)
2851590Srgrimes{
28691389Sdwmalone	int i, n;
2871590Srgrimes	char mac[5];	/* The current macro or nroff command */
288282439Sbapt	char *line;
289282439Sbapt	size_t linecap;
2901590Srgrimes	int pl;
2911590Srgrimes
292282439Sbapt	line = NULL;
293282439Sbapt	linecap = 0;
2941590Srgrimes	stktop = -1;
295282439Sbapt	for (lineno = 1; getline(&line, &linecap, f) > 0; lineno++) {
2961590Srgrimes		if (line[0] == '.') {
2971590Srgrimes			/*
2981590Srgrimes			 * find and isolate the macro/command name.
2991590Srgrimes			 */
3001590Srgrimes			strncpy(mac, line+1, 4);
3011590Srgrimes			if (isspace(mac[0])) {
3021590Srgrimes				pe(lineno);
3031590Srgrimes				printf("Empty command\n");
3041590Srgrimes			} else if (isspace(mac[1])) {
3051590Srgrimes				mac[1] = 0;
3061590Srgrimes			} else if (isspace(mac[2])) {
3071590Srgrimes				mac[2] = 0;
3081590Srgrimes			} else if (mac[0] != '\\' || mac[1] != '\"') {
3091590Srgrimes				pe(lineno);
3101590Srgrimes				printf("Command too long\n");
3111590Srgrimes			}
3121590Srgrimes
3131590Srgrimes			/*
3141590Srgrimes			 * Is it a known command?
3151590Srgrimes			 */
3161590Srgrimes			checkknown(mac);
3171590Srgrimes
3181590Srgrimes			/*
3191590Srgrimes			 * Should we add it?
3201590Srgrimes			 */
3211590Srgrimes			if (eq(mac, "de"))
3221590Srgrimes				addcmd(line);
3231590Srgrimes
3241590Srgrimes			chkcmd(line, mac);
3251590Srgrimes		}
3261590Srgrimes
3271590Srgrimes		/*
3281590Srgrimes		 * At this point we process the line looking
3291590Srgrimes		 * for \s and \f.
3301590Srgrimes		 */
331282437Sbapt		for (i = 0; line[i]; i++)
332282437Sbapt			if (line[i] == '\\' && (i == 0 || line[i-1] != '\\')) {
333282437Sbapt				if (!sflag && line[++i] == 's') {
3341590Srgrimes					pl = line[++i];
3351590Srgrimes					if (isdigit(pl)) {
3361590Srgrimes						n = pl - '0';
3371590Srgrimes						pl = ' ';
3381590Srgrimes					} else
3391590Srgrimes						n = 0;
3401590Srgrimes					while (isdigit(line[++i]))
3411590Srgrimes						n = 10 * n + line[i] - '0';
3421590Srgrimes					i--;
3431590Srgrimes					if (n == 0) {
3441590Srgrimes						if (stk[stktop].opno == SZ) {
3451590Srgrimes							stktop--;
3461590Srgrimes						} else {
3471590Srgrimes							pe(lineno);
3481590Srgrimes							printf("unmatched \\s0\n");
3491590Srgrimes						}
3501590Srgrimes					} else {
3511590Srgrimes						stk[++stktop].opno = SZ;
3521590Srgrimes						stk[stktop].pl = pl;
3531590Srgrimes						stk[stktop].parm = n;
3541590Srgrimes						stk[stktop].lno = lineno;
3551590Srgrimes					}
356282437Sbapt				} else if (!fflag && line[i] == 'f') {
3571590Srgrimes					n = line[++i];
3581590Srgrimes					if (n == 'P') {
3591590Srgrimes						if (stk[stktop].opno == FT) {
3601590Srgrimes							stktop--;
3611590Srgrimes						} else {
3621590Srgrimes							pe(lineno);
3631590Srgrimes							printf("unmatched \\fP\n");
3641590Srgrimes						}
3651590Srgrimes					} else {
3661590Srgrimes						stk[++stktop].opno = FT;
3671590Srgrimes						stk[stktop].pl = 1;
3681590Srgrimes						stk[stktop].parm = n;
3691590Srgrimes						stk[stktop].lno = lineno;
3701590Srgrimes					}
3711590Srgrimes				}
3721590Srgrimes			}
3731590Srgrimes	}
374282439Sbapt	free(line);
3751590Srgrimes	/*
3761590Srgrimes	 * We've hit the end and look at all this stuff that hasn't been
3771590Srgrimes	 * matched yet!  Complain, complain.
3781590Srgrimes	 */
379282437Sbapt	for (i = stktop; i >= 0; i--) {
3801590Srgrimes		complain(i);
3811590Srgrimes	}
3821590Srgrimes}
3831590Srgrimes
384227234Sedstatic void
385100813Sdwmalonecomplain(int i)
3861590Srgrimes{
3871590Srgrimes	pe(stk[i].lno);
3881590Srgrimes	printf("Unmatched ");
3891590Srgrimes	prop(i);
3901590Srgrimes	printf("\n");
3911590Srgrimes}
3921590Srgrimes
393227234Sedstatic void
394100813Sdwmaloneprop(int i)
3951590Srgrimes{
3961590Srgrimes	if (stk[i].pl == 0)
3971590Srgrimes		printf(".%s", br[stk[i].opno].opbr);
398282437Sbapt	else switch (stk[i].opno) {
3991590Srgrimes	case SZ:
4001590Srgrimes		printf("\\s%c%d", stk[i].pl, stk[i].parm);
4011590Srgrimes		break;
4021590Srgrimes	case FT:
4031590Srgrimes		printf("\\f%c", stk[i].parm);
4041590Srgrimes		break;
4051590Srgrimes	default:
4061590Srgrimes		printf("Bug: stk[%d].opno = %d = .%s, .%s",
407282437Sbapt			i, stk[i].opno, br[stk[i].opno].opbr,
408282437Sbapt			br[stk[i].opno].clbr);
4091590Srgrimes	}
4101590Srgrimes}
4111590Srgrimes
412227234Sedstatic void
413100813Sdwmalonechkcmd(const char *line __unused, const char *mac)
4141590Srgrimes{
41591389Sdwmalone	int i;
4161590Srgrimes
4171590Srgrimes	/*
4181590Srgrimes	 * Check to see if it matches top of stack.
4191590Srgrimes	 */
4201590Srgrimes	if (stktop >= 0 && eq(mac, br[stk[stktop].opno].clbr))
4211590Srgrimes		stktop--;	/* OK. Pop & forget */
4221590Srgrimes	else {
4231590Srgrimes		/* No. Maybe it's an opener */
4241590Srgrimes		for (i=0; br[i].opbr; i++) {
4251590Srgrimes			if (eq(mac, br[i].opbr)) {
4261590Srgrimes				/* Found. Push it. */
4271590Srgrimes				stktop++;
4281590Srgrimes				stk[stktop].opno = i;
4291590Srgrimes				stk[stktop].pl = 0;
4301590Srgrimes				stk[stktop].parm = 0;
4311590Srgrimes				stk[stktop].lno = lineno;
4321590Srgrimes				break;
4331590Srgrimes			}
4341590Srgrimes			/*
4351590Srgrimes			 * Maybe it's an unmatched closer.
4361590Srgrimes			 * NOTE: this depends on the fact
4371590Srgrimes			 * that none of the closers can be
4381590Srgrimes			 * openers too.
4391590Srgrimes			 */
4401590Srgrimes			if (eq(mac, br[i].clbr)) {
4411590Srgrimes				nomatch(mac);
4421590Srgrimes				break;
4431590Srgrimes			}
4441590Srgrimes		}
4451590Srgrimes	}
4461590Srgrimes}
4471590Srgrimes
448227234Sedstatic void
449100813Sdwmalonenomatch(const char *mac)
4501590Srgrimes{
45191389Sdwmalone	int i, j;
4521590Srgrimes
4531590Srgrimes	/*
4541590Srgrimes	 * Look for a match further down on stack
4551590Srgrimes	 * If we find one, it suggests that the stuff in
4561590Srgrimes	 * between is supposed to match itself.
4571590Srgrimes	 */
4581590Srgrimes	for (j=stktop; j>=0; j--)
4591590Srgrimes		if (eq(mac,br[stk[j].opno].clbr)) {
4601590Srgrimes			/* Found.  Make a good diagnostic. */
4611590Srgrimes			if (j == stktop-2) {
4621590Srgrimes				/*
4631590Srgrimes				 * Check for special case \fx..\fR and don't
4641590Srgrimes				 * complain.
4651590Srgrimes				 */
4661590Srgrimes				if (stk[j+1].opno==FT && stk[j+1].parm!='R'
4671590Srgrimes				 && stk[j+2].opno==FT && stk[j+2].parm=='R') {
4681590Srgrimes					stktop = j -1;
4691590Srgrimes					return;
4701590Srgrimes				}
4711590Srgrimes				/*
4721590Srgrimes				 * We have two unmatched frobs.  Chances are
4731590Srgrimes				 * they were intended to match, so we mention
4741590Srgrimes				 * them together.
4751590Srgrimes				 */
4761590Srgrimes				pe(stk[j+1].lno);
4771590Srgrimes				prop(j+1);
4781590Srgrimes				printf(" does not match %d: ", stk[j+2].lno);
4791590Srgrimes				prop(j+2);
4801590Srgrimes				printf("\n");
4811590Srgrimes			} else for (i=j+1; i <= stktop; i++) {
4821590Srgrimes				complain(i);
4831590Srgrimes			}
4841590Srgrimes			stktop = j-1;
4851590Srgrimes			return;
4861590Srgrimes		}
4871590Srgrimes	/* Didn't find one.  Throw this away. */
4881590Srgrimes	pe(lineno);
4891590Srgrimes	printf("Unmatched .%s\n", mac);
4901590Srgrimes}
4911590Srgrimes
4921590Srgrimes/* eq: are two strings equal? */
493227234Sedstatic int
494100813Sdwmaloneeq(const char *s1, const char *s2)
4951590Srgrimes{
4961590Srgrimes	return (strcmp(s1, s2) == 0);
4971590Srgrimes}
4981590Srgrimes
4991590Srgrimes/* print the first part of an error message, given the line number */
500227234Sedstatic void
501100813Sdwmalonepe(int linen)
5021590Srgrimes{
5031590Srgrimes	if (nfiles > 1)
5041590Srgrimes		printf("%s: ", cfilename);
50591389Sdwmalone	printf("%d: ", linen);
5061590Srgrimes}
5071590Srgrimes
508227234Sedstatic void
509100813Sdwmalonecheckknown(const char *mac)
5101590Srgrimes{
5111590Srgrimes
5121590Srgrimes	if (eq(mac, "."))
5131590Srgrimes		return;
5141590Srgrimes	if (binsrch(mac) >= 0)
5151590Srgrimes		return;
5161590Srgrimes	if (mac[0] == '\\' && mac[1] == '"')	/* comments */
5171590Srgrimes		return;
5181590Srgrimes
5191590Srgrimes	pe(lineno);
5201590Srgrimes	printf("Unknown command: .%s\n", mac);
5211590Srgrimes}
5221590Srgrimes
5231590Srgrimes/*
5241590Srgrimes * We have a .de xx line in "line".  Add xx to the list of known commands.
5251590Srgrimes */
526227234Sedstatic void
527100813Sdwmaloneaddcmd(char *line)
5281590Srgrimes{
5291590Srgrimes	char *mac;
5301590Srgrimes
5311590Srgrimes	/* grab the macro being defined */
5321590Srgrimes	mac = line+4;
5331590Srgrimes	while (isspace(*mac))
5341590Srgrimes		mac++;
5351590Srgrimes	if (*mac == 0) {
5361590Srgrimes		pe(lineno);
5371590Srgrimes		printf("illegal define: %s\n", line);
5381590Srgrimes		return;
5391590Srgrimes	}
5401590Srgrimes	mac[2] = 0;
5411590Srgrimes	if (isspace(mac[1]) || mac[1] == '\\')
5421590Srgrimes		mac[1] = 0;
5431590Srgrimes	if (ncmds >= MAXCMDS) {
5441590Srgrimes		printf("Only %d known commands allowed\n", MAXCMDS);
5451590Srgrimes		exit(1);
5461590Srgrimes	}
5471590Srgrimes	addmac(mac);
5481590Srgrimes}
5491590Srgrimes
5501590Srgrimes/*
5511590Srgrimes * Add mac to the list.  We should really have some kind of tree
5521590Srgrimes * structure here but this is a quick-and-dirty job and I just don't
5531590Srgrimes * have time to mess with it.  (I wonder if this will come back to haunt
5541590Srgrimes * me someday?)  Anyway, I claim that .de is fairly rare in user
5551590Srgrimes * nroff programs, and the register loop below is pretty fast.
5561590Srgrimes */
557227234Sedstatic void
558100813Sdwmaloneaddmac(const char *mac)
5591590Srgrimes{
56091389Sdwmalone	const char **src, **dest, **loc;
5611590Srgrimes
5621590Srgrimes	if (binsrch(mac) >= 0){	/* it's OK to redefine something */
5631590Srgrimes#ifdef DEBUG
5641590Srgrimes		printf("binsrch(%s) -> already in table\n", mac);
56591389Sdwmalone#endif
5661590Srgrimes		return;
5671590Srgrimes	}
5681590Srgrimes	/* binsrch sets slot as a side effect */
5691590Srgrimes#ifdef DEBUG
5701590Srgrimesprintf("binsrch(%s) -> %d\n", mac, slot);
5711590Srgrimes#endif
5721590Srgrimes	loc = &knowncmds[slot];
5731590Srgrimes	src = &knowncmds[ncmds-1];
5741590Srgrimes	dest = src+1;
5751590Srgrimes	while (dest > loc)
5761590Srgrimes		*dest-- = *src--;
57791389Sdwmalone	*loc = strcpy(malloc(3), mac);
5781590Srgrimes	ncmds++;
5791590Srgrimes#ifdef DEBUG
580282437Sbapt	printf("after: %s %s %s %s %s, %d cmds\n",
581282437Sbapt	    knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot],
582282437Sbapt	    knowncmds[slot+1], knowncmds[slot+2], ncmds);
5831590Srgrimes#endif
5841590Srgrimes}
5851590Srgrimes
5861590Srgrimes/*
5871590Srgrimes * Do a binary search in knowncmds for mac.
5881590Srgrimes * If found, return the index.  If not, return -1.
5891590Srgrimes */
590227234Sedstatic int
591100813Sdwmalonebinsrch(const char *mac)
5921590Srgrimes{
59391389Sdwmalone	const char *p;	/* pointer to current cmd in list */
59491389Sdwmalone	int d;		/* difference if any */
59591389Sdwmalone	int mid;	/* mid point in binary search */
59691389Sdwmalone	int top, bot;	/* boundaries of bin search, inclusive */
5971590Srgrimes
5981590Srgrimes	top = ncmds-1;
5991590Srgrimes	bot = 0;
6001590Srgrimes	while (top >= bot) {
6011590Srgrimes		mid = (top+bot)/2;
6021590Srgrimes		p = knowncmds[mid];
6031590Srgrimes		d = p[0] - mac[0];
6041590Srgrimes		if (d == 0)
6051590Srgrimes			d = p[1] - mac[1];
6061590Srgrimes		if (d == 0)
607282437Sbapt			return (mid);
6081590Srgrimes		if (d < 0)
6091590Srgrimes			bot = mid + 1;
6101590Srgrimes		else
6111590Srgrimes			top = mid - 1;
6121590Srgrimes	}
6131590Srgrimes	slot = bot;	/* place it would have gone */
614282437Sbapt	return (-1);
6151590Srgrimes}
616