static char *sccsid
= "@(#)checknr.c 4.5 (Berkeley) %G%";
* checknr: check an nroff/troff input file for matching macro calls.
* we also attempt to match size and font changes, but only the embedded
* kind. These must end in \s0 and \fP resp. Maybe more sophistication
* later but for now think of these restrictions as contributions to
* structured typesetting.
#define MAXSTK 100 /* Stack size */
#define MAXBR 100 /* Max number of bracket pairs known */
#define MAXCMDS 500 /* Max number of commands known */
* The stack on which we remember what we've seen so far.
int opno
; /* number of opening bracket */
int pl
; /* '+', '-', ' ' for \s, 1 for \f, 0 for .ft */
int parm
; /* parm to size, font, etc */
int lno
; /* line number the thing came in in */
* The kinds of opening and closing brackets.
/* A few bare bones troff commands */
"sz", "sz", /* also \s */
"ft", "ft", /* also \f */
/* Things needed by preprocessors */
* All commands known to nroff, plus macro packages.
* Used so we can complain about unrecognized commands.
char *knowncmds
[MAXCMDS
] = {
"$c", "$f", "$h", "$p", "$s", "(b", "(c", "(d", "(f", "(l", "(q", "(t",
"(x", "(z", ")b", ")c", ")d", ")f", ")l", ")q", ")t", ")x", ")z", "++",
"+c", "1C", "1c", "2C", "2c", "@(", "@)", "@C", "@D", "@F", "@I", "@M",
"@c", "@e", "@f", "@h", "@m", "@n", "@o", "@p", "@r", "@t", "@z", "AB",
"AE", "AF", "AI", "AL", "AS", "AT", "AU", "AX", "B", "B1", "B2", "BD",
"BE", "BG", "BL", "BS", "BT", "BX", "C1", "C2", "CD", "CM", "CT", "D",
"DA", "DE", "DF", "DL", "DS", "DT", "EC", "EF", "EG", "EH", "EM", "EN", "EQ",
"EX", "FA", "FD", "FE", "FG", "FJ", "FK", "FL", "FN", "FO", "FQ", "FS",
"FV", "FX", "H", "HC", "HM", "HO", "HU", "I", "ID", "IE", "IH", "IM",
"IP", "IZ", "KD", "KE", "KF", "KQ", "KS", "LB", "LC", "LD", "LE", "LG",
"LI", "LP", "MC", "ME", "MF", "MH", "ML", "MR", "MT", "ND", "NE", "NH",
"NL", "NP", "NS", "OF", "OH", "OK", "OP", "P", "PF", "PH", "PP", "PT",
"PY", "QE", "QP", "QS", "R", "RA", "RC", "RE", "RL", "RP", "RQ", "RS",
"RT", "S", "S0", "S2", "S3", "SA", "SG", "SH", "SK", "SM", "SP", "SY",
"TA", "TB", "TC", "TD", "TE", "TH", "TL", "TM", "TP", "TQ", "TR", "TS",
"TX", "UL", "US", "UX", "VL", "WC", "WH", "XD", "XF", "XK", "XP", "[", "[-",
"[0", "[1", "[2", "[3", "[4", "[5", "[<", "[>", "[]", "]", "]-", "]<", "]>",
"][", "ab", "ac", "ad", "af", "am", "ar", "as", "b", "ba", "bc", "bd",
"bi", "bl", "bp", "br", "bx", "c.", "c2", "cc", "ce", "cf", "ch", "cs",
"ct", "cu", "da", "de", "di", "dl", "dn", "ds", "dt", "dw", "dy", "ec",
"ef", "eh", "el", "em", "eo", "ep", "ev", "ex", "fc", "fi", "fl", "fo",
"fp", "ft", "fz", "hc", "he", "hl", "hp", "ht", "hw", "hx", "hy", "i",
"ie", "if", "ig", "in", "ip", "it", "ix", "lc", "lg", "li", "ll", "ln",
"lo", "lp", "ls", "lt", "m1", "m2", "m3", "m4", "mc", "mk", "mo", "n1",
"n2", "na", "ne", "nf", "nh", "nl", "nm", "nn", "np", "nr", "ns", "nx",
"of", "oh", "os", "pa", "pc", "pi", "pl", "pm", "pn", "po", "pp", "ps",
"q", "r", "rb", "rd", "re", "rm", "rn", "ro", "rr", "rs", "rt", "sb",
"sc", "sh", "sk", "so", "sp", "ss", "st", "sv", "sz", "ta", "tc", "th",
"ti", "tl", "tm", "tp", "tr", "u", "uf", "uh", "ul", "vs", "wh", "xp", "yr",
int lineno
; /* current line number in input file */
char line
[256]; /* the current line */
char *cfilename
; /* name of current file */
int nfiles
; /* number of files to process */
int fflag
; /* -f: ignore \f */
int sflag
; /* -s: ignore \s */
int ncmds
; /* size of knowncmds */
int slot
; /* slot in knowncmds found by binsrch */
/* Figure out how many known commands there are */
while (argc
> 1 && argv
[1][0] == '-') {
/* -a: add pairs of macros */
/* look for empty macro slots */
for (i
=0; br
[i
].opbr
; i
++)
for (cp
=argv
[1]+3; cp
[-1]; cp
+= 6) {
strncpy(br
[i
].opbr
, cp
, 2);
strncpy(br
[i
].clbr
, cp
+3, 2);
addmac(br
[i
].opbr
); /* knows pairs are also known cmds */
/* -c: add known commands */
for (cp
=argv
[1]+3; cp
[-1]; cp
+= 3) {
if (cp
[2] && cp
[2] != '.')
/* -f: ignore font changes */
/* -s: ignore size changes */
f
= fopen(cfilename
, "r");
printf("Usage: checknr -s -f -a.xx.yy.xx.yy... -c.xx.xx.xx...\n");
char mac
[5]; /* The current macro or nroff command */
for (lineno
= 1; fgets(line
, sizeof line
, f
); lineno
++) {
* find and isolate the macro/command name.
printf("Empty command\n");
} else if (isspace(mac
[1])) {
} else if (isspace(mac
[2])) {
} else if (mac
[0] != '\\' || mac
[1] != '\"') {
printf("Command too long\n");
* At this point we process the line looking
if (line
[i
]=='\\' && (i
==0 || line
[i
-1]!='\\')) {
if (!sflag
&& line
[++i
]=='s') {
while (isdigit(line
[++i
]))
n
= 10 * n
+ line
[i
] - '0';
if (stk
[stktop
].opno
== SZ
) {
printf("unmatched \\s0\n");
stk
[stktop
].lno
= lineno
;
} else if (!fflag
&& line
[i
]=='f') {
if (stk
[stktop
].opno
== FT
) {
printf("unmatched \\fP\n");
stk
[stktop
].lno
= lineno
;
* We've hit the end and look at all this stuff that hasn't been
* matched yet! Complain, complain.
for (i
=stktop
; i
>=0; i
--) {
printf(".%s", br
[stk
[i
].opno
].opbr
);
else switch(stk
[i
].opno
) {
printf("\\s%c%d", stk
[i
].pl
, stk
[i
].parm
);
printf("\\f%c", stk
[i
].parm
);
printf("Bug: stk[%d].opno = %d = .%s, .%s",
i
, stk
[i
].opno
, br
[stk
[i
].opno
].opbr
, br
[stk
[i
].opno
].clbr
);
* Check to see if it matches top of stack.
if (stktop
>= 0 && eq(mac
, br
[stk
[stktop
].opno
].clbr
))
stktop
--; /* OK. Pop & forget */
/* No. Maybe it's an opener */
for (i
=0; br
[i
].opbr
; i
++) {
if (eq(mac
, br
[i
].opbr
)) {
stk
[stktop
].lno
= lineno
;
* Maybe it's an unmatched closer.
* NOTE: this depends on the fact
* that none of the closers can be
if (eq(mac
, br
[i
].clbr
)) {
* Look for a match further down on stack
* If we find one, it suggests that the stuff in
* between is supposed to match itself.
for (j
=stktop
; j
>=0; j
--)
if (eq(mac
,br
[stk
[j
].opno
].clbr
)) {
/* Found. Make a good diagnostic. */
* Check for special case \fx..\fR and don't
if (stk
[j
+1].opno
==FT
&& stk
[j
+1].parm
!='R'
&& stk
[j
+2].opno
==FT
&& stk
[j
+2].parm
=='R') {
* We have two unmatched frobs. Chances are
* they were intended to match, so we mention
printf(" does not match %d: ", stk
[j
+2].lno
);
} else for (i
=j
+1; i
<= stktop
; i
++) {
/* Didn't find one. Throw this away. */
printf("Unmatched .%s\n", mac
);
/* eq: are two strings equal? */
return (strcmp(s1
, s2
) == 0);
/* print the first part of an error message, given the line number */
printf("%s: ", cfilename
);
if (mac
[0] == '\\' && mac
[1] == '"') /* comments */
printf("Unknown command: .%s\n", mac
);
* We have a .de xx line in "line". Add xx to the list of known commands.
/* grab the macro being defined */
printf("illegal define: %s\n", line
);
if (isspace(mac
[1]) || mac
[1] == '\\')
printf("Only %d known commands allowed\n", MAXCMDS
);
* Add mac to the list. We should really have some kind of tree
* structure here but this is a quick-and-dirty job and I just don't
* have time to mess with it. (I wonder if this will come back to haunt
* me someday?) Anyway, I claim that .de is fairly rare in user
* nroff programs, and the register loop below is pretty fast.
register char **src
, **dest
, **loc
;
binsrch(mac
); /* it's OK to redefine something */
/* binsrch sets slot as a side effect */
printf("binsrch(%s) -> %d\n", mac
, slot
);
src
= &knowncmds
[ncmds
-1];
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
);
* Do a binary search in knowncmds for mac.
* If found, return the index. If not, return -1.
register char *p
; /* pointer to current cmd in list */
register int d
; /* difference if any */
register int mid
; /* mid point in binary search */
register int top
, bot
; /* boundaries of bin search, inclusive */
slot
= bot
; /* place it would have gone */