static char *sccsid
= "@(#)checknr.c 4.1 (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 ms and me.
* 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", "AB", "AE",
"AE", "AI", "AI", "AT", "AU", "AU", "AX", "B", "B1", "B2",
"BD", "BG", "BT", "BX", "C1", "C2", "CD", "CM", "CT", "D",
"DA", "DE", "DF", "DS", "EG", "EM", "EN", "EQ", "EQ", "FA",
"FE", "FJ", "FK", "FL", "FN", "FO", "FQ", "FS", "FV", "FX",
"HO", "I", "ID", "IE", "IH", "IM", "IP", "IZ", "KD", "KE",
"KF", "KQ", "KS", "LB", "LD", "LG", "LP", "MC", "ME", "MF",
"MH", "MR", "ND", "NH", "NL", "NP", "OK", "PP", "PT", "PY",
"QE", "QP", "QS", "R", "RA", "RC", "RE", "RP", "RQ", "RS",
"RT", "S0", "S2", "S3", "SG", "SH", "SM", "SY", "TA", "TC",
"TD", "TE", "TH", "TL", "TL", "TM", "TQ", "TR", "TS", "TS",
"TX", "UL", "US", "UX", "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", "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", "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",
"po", "pp", "ps", "q", "r", "rb", "rd", "re", "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", "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 */
while (argc
> 1 && argv
[1][0] == '-') {
/* -a: add pairs of macros */
printf("Usage: nrc -s -f -a.xx.yy.xx.yy... (.xx, .yy)\n");
/* look for empty macro slots */
for (i
=0; br
[i
].opbr
; i
++)
for (cp
=argv
[1]+3; cp
[-1]; cp
+= 6) {
printf("Illegal flag: %s\n", argv
[1]);
f
= fopen(cfilename
, "r");
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
[2] != '\\' || mac
[3] != '\"') {
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
);
/* First time figure out ncmds. */
printf("Unknown command: .%s\n", mac
);
* We have a .de xx line in "line". Add xx to the list of known commands.
register char **src
, **dest
, **loc
;
/* 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.
binsrch(mac
); /* it's OK to redefine something */
/* binsrch sets slot as a side effect */
src
= &knowncmds
[ncmds
-1];
* 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 */