2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)checknr.c 8.1 (Berkeley) 6/6/93
38 * checknr: check an nroff/troff input file for matching macro calls.
39 * we also attempt to match size and font changes, but only the embedded
40 * kind. These must end in \s0 and \fP resp. Maybe more sophistication
41 * later but for now think of these restrictions as contributions to
42 * structured typesetting.
49 #define MAXSTK 100 /* Stack size */
50 #define MAXBR 100 /* Max number of bracket pairs known */
51 #define MAXCMDS 500 /* Max number of commands known */
53 void addcmd __P((char *));
54 void addmac __P((char *));
55 int binsrch __P((char *));
56 void checkknown __P((char *));
57 void chkcmd __P((char *, char *));
58 void complain __P((int));
59 int eq __P((char *, char *));
60 void nomatch __P((char *));
62 void process __P((FILE *));
64 static void usage __P((void));
67 * The stack on which we remember what we've seen so far.
70 int opno; /* number of opening bracket */
71 int pl; /* '+', '-', ' ' for \s, 1 for \f, 0 for .ft */
72 int parm; /* parm to size, font, etc */
73 int lno; /* line number the thing came in in */
78 * The kinds of opening and closing brackets.
84 /* A few bare bones troff commands */
86 {"sz", "sz"}, /* also \s */
88 {"ft", "ft"}, /* also \f */
102 /* the -ms package */
118 /* The -me package */
127 /* Things needed by preprocessors */
136 * All commands known to nroff, plus macro packages.
137 * Used so we can complain about unrecognized commands.
139 char *knowncmds[MAXCMDS] = {
140 "$c", "$f", "$h", "$p", "$s", "(b", "(c", "(d", "(f", "(l", "(q", "(t",
141 "(x", "(z", ")b", ")c", ")d", ")f", ")l", ")q", ")t", ")x", ")z", "++",
142 "+c", "1C", "1c", "2C", "2c", "@(", "@)", "@C", "@D", "@F", "@I", "@M",
143 "@c", "@e", "@f", "@h", "@m", "@n", "@o", "@p", "@r", "@t", "@z", "AB",
144 "AE", "AF", "AI", "AL", "AM", "AS", "AT", "AU", "AX", "B", "B1", "B2",
145 "BD", "BE", "BG", "BL", "BS", "BT", "BX", "C1", "C2", "CD", "CM", "CT",
146 "D", "DA", "DE", "DF", "DL", "DS", "DT", "EC", "EF", "EG", "EH", "EM",
147 "EN", "EQ", "EX", "FA", "FD", "FE", "FG", "FJ", "FK", "FL", "FN", "FO",
148 "FQ", "FS", "FV", "FX", "H", "HC", "HD", "HM", "HO", "HU", "I", "ID",
149 "IE", "IH", "IM", "IP", "IX", "IZ", "KD", "KE", "KF", "KQ", "KS", "LB",
150 "LC", "LD", "LE", "LG", "LI", "LP", "MC", "ME", "MF", "MH", "ML", "MR",
151 "MT", "ND", "NE", "NH", "NL", "NP", "NS", "OF", "OH", "OK", "OP", "P",
152 "P1", "PF", "PH", "PP", "PT", "PX", "PY", "QE", "QP", "QS", "R", "RA",
153 "RC", "RE", "RL", "RP", "RQ", "RS", "RT", "S", "S0", "S2", "S3", "SA",
154 "SG", "SH", "SK", "SM", "SP", "SY", "T&", "TA", "TB", "TC", "TD", "TE",
155 "TH", "TL", "TM", "TP", "TQ", "TR", "TS", "TX", "UL", "US", "UX", "VL",
156 "WC", "WH", "XA", "XD", "XE", "XF", "XK", "XP", "XS", "[", "[-", "[0",
157 "[1", "[2", "[3", "[4", "[5", "[<", "[>", "[]", "]", "]-", "]<", "]>",
158 "][", "ab", "ac", "ad", "af", "am", "ar", "as", "b", "ba", "bc", "bd",
159 "bi", "bl", "bp", "br", "bx", "c.", "c2", "cc", "ce", "cf", "ch", "cs",
160 "ct", "cu", "da", "de", "di", "dl", "dn", "ds", "dt", "dw", "dy", "ec",
161 "ef", "eh", "el", "em", "eo", "ep", "ev", "ex", "fc", "fi", "fl", "fo",
162 "fp", "ft", "fz", "hc", "he", "hl", "hp", "ht", "hw", "hx", "hy", "i",
163 "ie", "if", "ig", "in", "ip", "it", "ix", "lc", "lg", "li", "ll", "ln",
164 "lo", "lp", "ls", "lt", "m1", "m2", "m3", "m4", "mc", "mk", "mo", "n1",
165 "n2", "na", "ne", "nf", "nh", "nl", "nm", "nn", "np", "nr", "ns", "nx",
166 "of", "oh", "os", "pa", "pc", "pi", "pl", "pm", "pn", "po", "pp", "ps",
167 "q", "r", "rb", "rd", "re", "rm", "rn", "ro", "rr", "rs", "rt", "sb",
168 "sc", "sh", "sk", "so", "sp", "ss", "st", "sv", "sz", "ta", "tc", "th",
169 "ti", "tl", "tm", "tp", "tr", "u", "uf", "uh", "ul", "vs", "wh", "xp",
173 int lineno; /* current line number in input file */
174 char line[256]; /* the current line */
175 char *cfilename; /* name of current file */
176 int nfiles; /* number of files to process */
177 int fflag; /* -f: ignore \f */
178 int sflag; /* -s: ignore \s */
179 int ncmds; /* size of knowncmds */
180 int slot; /* slot in knowncmds found by binsrch */
192 /* Figure out how many known commands there are */
193 while (knowncmds[ncmds])
195 while (argc > 1 && argv[1][0] == '-') {
198 /* -a: add pairs of macros */
200 i = strlen(argv[1]) - 2;
203 /* look for empty macro slots */
204 for (i=0; br[i].opbr; i++)
206 for (cp=argv[1]+3; cp[-1]; cp += 6) {
207 br[i].opbr = malloc(3);
208 strncpy(br[i].opbr, cp, 2);
209 br[i].clbr = malloc(3);
210 strncpy(br[i].clbr, cp+3, 2);
211 addmac(br[i].opbr); /* knows pairs are also known cmds */
217 /* -c: add known commands */
219 i = strlen(argv[1]) - 2;
222 for (cp=argv[1]+3; cp[-1]; cp += 3) {
223 if (cp[2] && cp[2] != '.')
231 /* -f: ignore font changes */
236 /* -s: ignore size changes */
249 for (i=1; i<argc; i++) {
251 f = fopen(cfilename, "r");
268 "usage: checknr [-a.xx.yy.xx.yy...] [-c.xx.xx.xx...] [-s] [-f] file\n");
277 char mac[5]; /* The current macro or nroff command */
281 for (lineno = 1; fgets(line, sizeof line, f); lineno++) {
282 if (line[0] == '.') {
284 * find and isolate the macro/command name.
286 strncpy(mac, line+1, 4);
287 if (isspace(mac[0])) {
289 printf("Empty command\n");
290 } else if (isspace(mac[1])) {
292 } else if (isspace(mac[2])) {
294 } else if (mac[0] != '\\' || mac[1] != '\"') {
296 printf("Command too long\n");
300 * Is it a known command?
314 * At this point we process the line looking
317 for (i=0; line[i]; i++)
318 if (line[i]=='\\' && (i==0 || line[i-1]!='\\')) {
319 if (!sflag && line[++i]=='s') {
326 while (isdigit(line[++i]))
327 n = 10 * n + line[i] - '0';
330 if (stk[stktop].opno == SZ) {
334 printf("unmatched \\s0\n");
337 stk[++stktop].opno = SZ;
339 stk[stktop].parm = n;
340 stk[stktop].lno = lineno;
342 } else if (!fflag && line[i]=='f') {
345 if (stk[stktop].opno == FT) {
349 printf("unmatched \\fP\n");
352 stk[++stktop].opno = FT;
354 stk[stktop].parm = n;
355 stk[stktop].lno = lineno;
361 * We've hit the end and look at all this stuff that hasn't been
362 * matched yet! Complain, complain.
364 for (i=stktop; i>=0; i--) {
374 printf("Unmatched ");
384 printf(".%s", br[stk[i].opno].opbr);
385 else switch(stk[i].opno) {
387 printf("\\s%c%d", stk[i].pl, stk[i].parm);
390 printf("\\f%c", stk[i].parm);
393 printf("Bug: stk[%d].opno = %d = .%s, .%s",
394 i, stk[i].opno, br[stk[i].opno].opbr, br[stk[i].opno].clbr);
406 * Check to see if it matches top of stack.
408 if (stktop >= 0 && eq(mac, br[stk[stktop].opno].clbr))
409 stktop--; /* OK. Pop & forget */
411 /* No. Maybe it's an opener */
412 for (i=0; br[i].opbr; i++) {
413 if (eq(mac, br[i].opbr)) {
414 /* Found. Push it. */
416 stk[stktop].opno = i;
418 stk[stktop].parm = 0;
419 stk[stktop].lno = lineno;
423 * Maybe it's an unmatched closer.
424 * NOTE: this depends on the fact
425 * that none of the closers can be
428 if (eq(mac, br[i].clbr)) {
443 * Look for a match further down on stack
444 * If we find one, it suggests that the stuff in
445 * between is supposed to match itself.
447 for (j=stktop; j>=0; j--)
448 if (eq(mac,br[stk[j].opno].clbr)) {
449 /* Found. Make a good diagnostic. */
452 * Check for special case \fx..\fR and don't
455 if (stk[j+1].opno==FT && stk[j+1].parm!='R'
456 && stk[j+2].opno==FT && stk[j+2].parm=='R') {
461 * We have two unmatched frobs. Chances are
462 * they were intended to match, so we mention
467 printf(" does not match %d: ", stk[j+2].lno);
470 } else for (i=j+1; i <= stktop; i++) {
476 /* Didn't find one. Throw this away. */
478 printf("Unmatched .%s\n", mac);
481 /* eq: are two strings equal? */
486 return (strcmp(s1, s2) == 0);
489 /* print the first part of an error message, given the line number */
495 printf("%s: ", cfilename);
496 printf("%d: ", lineno);
506 if (binsrch(mac) >= 0)
508 if (mac[0] == '\\' && mac[1] == '"') /* comments */
512 printf("Unknown command: .%s\n", mac);
516 * We have a .de xx line in "line". Add xx to the list of known commands.
524 /* grab the macro being defined */
526 while (isspace(*mac))
530 printf("illegal define: %s\n", line);
534 if (isspace(mac[1]) || mac[1] == '\\')
536 if (ncmds >= MAXCMDS) {
537 printf("Only %d known commands allowed\n", MAXCMDS);
544 * Add mac to the list. We should really have some kind of tree
545 * structure here but this is a quick-and-dirty job and I just don't
546 * have time to mess with it. (I wonder if this will come back to haunt
547 * me someday?) Anyway, I claim that .de is fairly rare in user
548 * nroff programs, and the register loop below is pretty fast.
554 register char **src, **dest, **loc;
556 if (binsrch(mac) >= 0){ /* it's OK to redefine something */
558 printf("binsrch(%s) -> already in table\n", mac);
562 /* binsrch sets slot as a side effect */
564 printf("binsrch(%s) -> %d\n", mac, slot);
566 loc = &knowncmds[slot];
567 src = &knowncmds[ncmds-1];
575 printf("after: %s %s %s %s %s, %d cmds\n", knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot], knowncmds[slot+1], knowncmds[slot+2], ncmds);
580 * Do a binary search in knowncmds for mac.
581 * If found, return the index. If not, return -1.
587 register char *p; /* pointer to current cmd in list */
588 register int d; /* difference if any */
589 register int mid; /* mid point in binary search */
590 register int top, bot; /* boundaries of bin search, inclusive */
607 slot = bot; /* place it would have gone */