1*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2*0Sstevel@tonic-gate /* All Rights Reserved */
3*0Sstevel@tonic-gate
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate /*
6*0Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
7*0Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
8*0Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
9*0Sstevel@tonic-gate */
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gate /*
12*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
13*0Sstevel@tonic-gate * Use is subject to license terms.
14*0Sstevel@tonic-gate */
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gate /*
19*0Sstevel@tonic-gate * checknr: check an nroff/troff input file for matching macro calls.
20*0Sstevel@tonic-gate * we also attempt to match size and font changes, but only the embedded
21*0Sstevel@tonic-gate * kind. These must end in \s0 and \fP resp. Maybe more sophistication
22*0Sstevel@tonic-gate * later but for now think of these restrictions as contributions to
23*0Sstevel@tonic-gate * structured typesetting.
24*0Sstevel@tonic-gate */
25*0Sstevel@tonic-gate #include <stdio.h>
26*0Sstevel@tonic-gate #include <stdlib.h>
27*0Sstevel@tonic-gate #include <unistd.h>
28*0Sstevel@tonic-gate #include <string.h>
29*0Sstevel@tonic-gate #include <ctype.h>
30*0Sstevel@tonic-gate #include <locale.h>
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate #define MAXSTK 100 /* Stack size */
33*0Sstevel@tonic-gate static int maxstk;
34*0Sstevel@tonic-gate #define MAXBR 100 /* Max number of bracket pairs known */
35*0Sstevel@tonic-gate #define MAXCMDS 500 /* Max number of commands known */
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate * The stack on which we remember what we've seen so far.
39*0Sstevel@tonic-gate */
40*0Sstevel@tonic-gate static struct stkstr {
41*0Sstevel@tonic-gate int opno; /* number of opening bracket */
42*0Sstevel@tonic-gate int pl; /* '+', '-', ' ' for \s, 1 for \f, 0 for .ft */
43*0Sstevel@tonic-gate int parm; /* parm to size, font, etc */
44*0Sstevel@tonic-gate int lno; /* line number the thing came in in */
45*0Sstevel@tonic-gate } *stk;
46*0Sstevel@tonic-gate static int stktop;
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate * The kinds of opening and closing brackets.
50*0Sstevel@tonic-gate */
51*0Sstevel@tonic-gate static struct brstr {
52*0Sstevel@tonic-gate char *opbr;
53*0Sstevel@tonic-gate char *clbr;
54*0Sstevel@tonic-gate } br[MAXBR] = {
55*0Sstevel@tonic-gate /* A few bare bones troff commands */
56*0Sstevel@tonic-gate #define SZ 0
57*0Sstevel@tonic-gate "sz", "sz", /* also \s */
58*0Sstevel@tonic-gate #define FT 1
59*0Sstevel@tonic-gate "ft", "ft", /* also \f */
60*0Sstevel@tonic-gate /* the -mm package */
61*0Sstevel@tonic-gate "AL", "LE",
62*0Sstevel@tonic-gate "AS", "AE",
63*0Sstevel@tonic-gate "BL", "LE",
64*0Sstevel@tonic-gate "BS", "BE",
65*0Sstevel@tonic-gate "DF", "DE",
66*0Sstevel@tonic-gate "DL", "LE",
67*0Sstevel@tonic-gate "DS", "DE",
68*0Sstevel@tonic-gate "FS", "FE",
69*0Sstevel@tonic-gate "ML", "LE",
70*0Sstevel@tonic-gate "NS", "NE",
71*0Sstevel@tonic-gate "RL", "LE",
72*0Sstevel@tonic-gate "VL", "LE",
73*0Sstevel@tonic-gate /* the -ms package */
74*0Sstevel@tonic-gate "AB", "AE",
75*0Sstevel@tonic-gate "BD", "DE",
76*0Sstevel@tonic-gate "CD", "DE",
77*0Sstevel@tonic-gate "DS", "DE",
78*0Sstevel@tonic-gate "FS", "FE",
79*0Sstevel@tonic-gate "ID", "DE",
80*0Sstevel@tonic-gate "KF", "KE",
81*0Sstevel@tonic-gate "KS", "KE",
82*0Sstevel@tonic-gate "LD", "DE",
83*0Sstevel@tonic-gate "LG", "NL",
84*0Sstevel@tonic-gate "QS", "QE",
85*0Sstevel@tonic-gate "RS", "RE",
86*0Sstevel@tonic-gate "SM", "NL",
87*0Sstevel@tonic-gate "XA", "XE",
88*0Sstevel@tonic-gate "XS", "XE",
89*0Sstevel@tonic-gate /* The -me package */
90*0Sstevel@tonic-gate "(b", ")b",
91*0Sstevel@tonic-gate "(c", ")c",
92*0Sstevel@tonic-gate "(d", ")d",
93*0Sstevel@tonic-gate "(f", ")f",
94*0Sstevel@tonic-gate "(l", ")l",
95*0Sstevel@tonic-gate "(q", ")q",
96*0Sstevel@tonic-gate "(x", ")x",
97*0Sstevel@tonic-gate "(z", ")z",
98*0Sstevel@tonic-gate /* Things needed by preprocessors */
99*0Sstevel@tonic-gate "EQ", "EN",
100*0Sstevel@tonic-gate "TS", "TE",
101*0Sstevel@tonic-gate /* Refer */
102*0Sstevel@tonic-gate "[", "]",
103*0Sstevel@tonic-gate 0, 0
104*0Sstevel@tonic-gate };
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate /*
107*0Sstevel@tonic-gate * All commands known to nroff, plus macro packages.
108*0Sstevel@tonic-gate * Used so we can complain about unrecognized commands.
109*0Sstevel@tonic-gate */
110*0Sstevel@tonic-gate static char *knowncmds[MAXCMDS] = {
111*0Sstevel@tonic-gate "$c", "$f", "$h", "$p", "$s", "(b", "(c", "(d", "(f", "(l", "(q", "(t",
112*0Sstevel@tonic-gate "(x", "(z", ")b", ")c", ")d", ")f", ")l", ")q", ")t", ")x", ")z", "++",
113*0Sstevel@tonic-gate "+c", "1C", "1c", "2C", "2c", "@(", "@)", "@C", "@D", "@F", "@I", "@M",
114*0Sstevel@tonic-gate "@c", "@e", "@f", "@h", "@m", "@n", "@o", "@p", "@r", "@t", "@z", "AB",
115*0Sstevel@tonic-gate "AE", "AF", "AI", "AL", "AM", "AS", "AT", "AU", "AX", "B", "B1", "B2",
116*0Sstevel@tonic-gate "BD", "BE", "BG", "BL", "BS", "BT", "BX", "C1", "C2", "CD", "CM", "CT",
117*0Sstevel@tonic-gate "D", "DA", "DE", "DF", "DL", "DS", "DT", "EC", "EF", "EG", "EH", "EM",
118*0Sstevel@tonic-gate "EN", "EQ", "EX", "FA", "FD", "FE", "FG", "FJ", "FK", "FL", "FN", "FO",
119*0Sstevel@tonic-gate "FQ", "FS", "FV", "FX", "H", "HC", "HD", "HM", "HO", "HU", "I", "ID",
120*0Sstevel@tonic-gate "IE", "IH", "IM", "IP", "IX", "IZ", "KD", "KE", "KF", "KQ", "KS", "LB",
121*0Sstevel@tonic-gate "LC", "LD", "LE", "LG", "LI", "LP", "MC", "ME", "MF", "MH", "ML", "MR",
122*0Sstevel@tonic-gate "MT", "ND", "NE", "NH", "NL", "NP", "NS", "OF", "OH", "OK", "OP", "P",
123*0Sstevel@tonic-gate "P1", "PF", "PH", "PP", "PT", "PX", "PY", "QE", "QP", "QS", "R", "RA",
124*0Sstevel@tonic-gate "RC", "RE", "RL", "RP", "RQ", "RS", "RT", "S", "S0", "S2", "S3", "SA",
125*0Sstevel@tonic-gate "SG", "SH", "SK", "SM", "SP", "SY", "T&", "TA", "TB", "TC", "TD", "TE",
126*0Sstevel@tonic-gate "TH", "TL", "TM", "TP", "TQ", "TR", "TS", "TX", "UL", "US", "UX", "VL",
127*0Sstevel@tonic-gate "WC", "WH", "XA", "XD", "XE", "XF", "XK", "XP", "XS", "[", "[-", "[0",
128*0Sstevel@tonic-gate "[1", "[2", "[3", "[4", "[5", "[<", "[>", "[]", "]", "]-", "]<", "]>",
129*0Sstevel@tonic-gate "][", "ab", "ac", "ad", "af", "am", "ar", "as", "b", "ba", "bc", "bd",
130*0Sstevel@tonic-gate "bi", "bl", "bp", "br", "bx", "c.", "c2", "cc", "ce", "cf", "ch", "cs",
131*0Sstevel@tonic-gate "ct", "cu", "da", "de", "di", "dl", "dn", "ds", "dt", "dw", "dy", "ec",
132*0Sstevel@tonic-gate "ef", "eh", "el", "em", "eo", "ep", "ev", "ex", "fc", "fi", "fl", "fo",
133*0Sstevel@tonic-gate "fp", "ft", "fz", "hc", "he", "hl", "hp", "ht", "hw", "hx", "hy", "i",
134*0Sstevel@tonic-gate "ie", "if", "ig", "in", "ip", "it", "ix", "lc", "lg", "li", "ll", "ln",
135*0Sstevel@tonic-gate "lo", "lp", "ls", "lt", "m1", "m2", "m3", "m4", "mc", "mk", "mo", "n1",
136*0Sstevel@tonic-gate "n2", "na", "ne", "nf", "nh", "nl", "nm", "nn", "np", "nr", "ns", "nx",
137*0Sstevel@tonic-gate "of", "oh", "os", "pa", "pc", "pi", "pl", "pm", "pn", "po", "pp", "ps",
138*0Sstevel@tonic-gate "q", "r", "rb", "rd", "re", "rm", "rn", "ro", "rr", "rs", "rt", "sb",
139*0Sstevel@tonic-gate "sc", "sh", "sk", "so", "sp", "ss", "st", "sv", "sz", "ta", "tc", "th",
140*0Sstevel@tonic-gate "ti", "tl", "tm", "tp", "tr", "u", "uf", "uh", "ul", "vs", "wh", "xp",
141*0Sstevel@tonic-gate "yr", 0
142*0Sstevel@tonic-gate };
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate static int lineno; /* current line number in input file */
145*0Sstevel@tonic-gate static char line[256]; /* the current line */
146*0Sstevel@tonic-gate static char *cfilename; /* name of current file */
147*0Sstevel@tonic-gate static int nfiles; /* number of files to process */
148*0Sstevel@tonic-gate static int fflag; /* -f: ignore \f */
149*0Sstevel@tonic-gate static int sflag; /* -s: ignore \s */
150*0Sstevel@tonic-gate static int ncmds; /* size of knowncmds */
151*0Sstevel@tonic-gate static int slot; /* slot in knowncmds found by binsrch */
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate static void growstk();
154*0Sstevel@tonic-gate static void usage();
155*0Sstevel@tonic-gate static void process(FILE *f);
156*0Sstevel@tonic-gate static void complain(int i);
157*0Sstevel@tonic-gate static void prop(int i);
158*0Sstevel@tonic-gate static void chkcmd(char *line, char *mac);
159*0Sstevel@tonic-gate static void nomatch(char *mac);
160*0Sstevel@tonic-gate static int eq(char *s1, char *s2);
161*0Sstevel@tonic-gate static void pe(int lineno);
162*0Sstevel@tonic-gate static void checkknown(char *mac);
163*0Sstevel@tonic-gate static void addcmd(char *line);
164*0Sstevel@tonic-gate static void addmac(char *mac);
165*0Sstevel@tonic-gate static int binsrch(char *mac);
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate static void
growstk()168*0Sstevel@tonic-gate growstk()
169*0Sstevel@tonic-gate {
170*0Sstevel@tonic-gate stktop++;
171*0Sstevel@tonic-gate if (stktop >= maxstk) {
172*0Sstevel@tonic-gate maxstk *= 2;
173*0Sstevel@tonic-gate stk = (struct stkstr *)realloc(stk,
174*0Sstevel@tonic-gate sizeof (struct stkstr) * maxstk);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate int
main(argc,argv)179*0Sstevel@tonic-gate main(argc, argv)
180*0Sstevel@tonic-gate int argc;
181*0Sstevel@tonic-gate char **argv;
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate FILE *f;
184*0Sstevel@tonic-gate int i;
185*0Sstevel@tonic-gate char *cp;
186*0Sstevel@tonic-gate char b1[4];
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
189*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
190*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
191*0Sstevel@tonic-gate #endif
192*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
193*0Sstevel@tonic-gate stk = (struct stkstr *)calloc(sizeof (struct stkstr), 100);
194*0Sstevel@tonic-gate maxstk = 100;
195*0Sstevel@tonic-gate /* Figure out how many known commands there are */
196*0Sstevel@tonic-gate while (knowncmds[ncmds])
197*0Sstevel@tonic-gate ncmds++;
198*0Sstevel@tonic-gate while (argc > 1 && argv[1][0] == '-') {
199*0Sstevel@tonic-gate switch (argv[1][1]) {
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate /* -a: add pairs of macros */
202*0Sstevel@tonic-gate case 'a':
203*0Sstevel@tonic-gate i = strlen(argv[1]) - 2;
204*0Sstevel@tonic-gate if (i % 6 != 0)
205*0Sstevel@tonic-gate usage();
206*0Sstevel@tonic-gate /* look for empty macro slots */
207*0Sstevel@tonic-gate for (i = 0; br[i].opbr; i++)
208*0Sstevel@tonic-gate ;
209*0Sstevel@tonic-gate for (cp = argv[1]+3; cp[-1]; cp += 6) {
210*0Sstevel@tonic-gate br[i].opbr = malloc(3);
211*0Sstevel@tonic-gate (void) strncpy(br[i].opbr, cp, 2);
212*0Sstevel@tonic-gate br[i].clbr = malloc(3);
213*0Sstevel@tonic-gate (void) strncpy(br[i].clbr, cp+3, 2);
214*0Sstevel@tonic-gate /* knows pairs are also known cmds */
215*0Sstevel@tonic-gate addmac(br[i].opbr);
216*0Sstevel@tonic-gate addmac(br[i].clbr);
217*0Sstevel@tonic-gate i++;
218*0Sstevel@tonic-gate }
219*0Sstevel@tonic-gate break;
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate /* -c: add known commands */
222*0Sstevel@tonic-gate case 'c':
223*0Sstevel@tonic-gate i = strlen(argv[1]) - 2;
224*0Sstevel@tonic-gate if (i % 3 != 0)
225*0Sstevel@tonic-gate usage();
226*0Sstevel@tonic-gate for (cp = argv[1]+3; cp[-1]; cp += 3) {
227*0Sstevel@tonic-gate if (cp[2] && cp[2] != '.')
228*0Sstevel@tonic-gate usage();
229*0Sstevel@tonic-gate (void) strncpy(b1, cp, 2);
230*0Sstevel@tonic-gate addmac(b1);
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate break;
233*0Sstevel@tonic-gate
234*0Sstevel@tonic-gate /* -f: ignore font changes */
235*0Sstevel@tonic-gate case 'f':
236*0Sstevel@tonic-gate fflag = 1;
237*0Sstevel@tonic-gate break;
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate /* -s: ignore size changes */
240*0Sstevel@tonic-gate case 's':
241*0Sstevel@tonic-gate sflag = 1;
242*0Sstevel@tonic-gate break;
243*0Sstevel@tonic-gate default:
244*0Sstevel@tonic-gate usage();
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate argc--; argv++;
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate nfiles = argc - 1;
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gate if (nfiles > 0) {
252*0Sstevel@tonic-gate for (i = 1; i < argc; i++) {
253*0Sstevel@tonic-gate cfilename = argv[i];
254*0Sstevel@tonic-gate f = fopen(cfilename, "r");
255*0Sstevel@tonic-gate if (f == NULL) {
256*0Sstevel@tonic-gate perror(cfilename);
257*0Sstevel@tonic-gate exit(1);
258*0Sstevel@tonic-gate }
259*0Sstevel@tonic-gate else
260*0Sstevel@tonic-gate process(f);
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate } else {
263*0Sstevel@tonic-gate cfilename = "stdin";
264*0Sstevel@tonic-gate process(stdin);
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate return (0);
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate
269*0Sstevel@tonic-gate static void
usage()270*0Sstevel@tonic-gate usage()
271*0Sstevel@tonic-gate {
272*0Sstevel@tonic-gate (void) printf(gettext("Usage: \
273*0Sstevel@tonic-gate checknr [ -fs ] [ -a.xx.yy.xx.yy...] [-c.xx.xx.xx...] [ filename .. ]\n"));
274*0Sstevel@tonic-gate exit(1);
275*0Sstevel@tonic-gate }
276*0Sstevel@tonic-gate
277*0Sstevel@tonic-gate static void
process(FILE * f)278*0Sstevel@tonic-gate process(FILE *f)
279*0Sstevel@tonic-gate {
280*0Sstevel@tonic-gate int i, n;
281*0Sstevel@tonic-gate char mac[5]; /* The current macro or nroff command */
282*0Sstevel@tonic-gate int pl;
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gate stktop = -1;
285*0Sstevel@tonic-gate for (lineno = 1; fgets(line, sizeof (line), f); lineno++) {
286*0Sstevel@tonic-gate if (line[0] == '.') {
287*0Sstevel@tonic-gate /*
288*0Sstevel@tonic-gate * find and isolate the macro/command name.
289*0Sstevel@tonic-gate */
290*0Sstevel@tonic-gate (void) strncpy(mac, line+1, 4);
291*0Sstevel@tonic-gate if (isspace(mac[0])) {
292*0Sstevel@tonic-gate pe(lineno);
293*0Sstevel@tonic-gate (void) printf(gettext("Empty command\n"));
294*0Sstevel@tonic-gate } else if (isspace(mac[1])) {
295*0Sstevel@tonic-gate mac[1] = 0;
296*0Sstevel@tonic-gate } else if (isspace(mac[2])) {
297*0Sstevel@tonic-gate mac[2] = 0;
298*0Sstevel@tonic-gate } else if (mac[0] != '\\' || mac[1] != '\"') {
299*0Sstevel@tonic-gate pe(lineno);
300*0Sstevel@tonic-gate (void) printf(gettext("Command too long\n"));
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate /*
304*0Sstevel@tonic-gate * Is it a known command?
305*0Sstevel@tonic-gate */
306*0Sstevel@tonic-gate checkknown(mac);
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate /*
309*0Sstevel@tonic-gate * Should we add it?
310*0Sstevel@tonic-gate */
311*0Sstevel@tonic-gate if (eq(mac, "de"))
312*0Sstevel@tonic-gate addcmd(line);
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate chkcmd(line, mac);
315*0Sstevel@tonic-gate }
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate /*
318*0Sstevel@tonic-gate * At this point we process the line looking
319*0Sstevel@tonic-gate * for \s and \f.
320*0Sstevel@tonic-gate */
321*0Sstevel@tonic-gate for (i = 0; line[i]; i++)
322*0Sstevel@tonic-gate if (line[i] == '\\' && (i == 0 || line[i-1] != '\\')) {
323*0Sstevel@tonic-gate if (!sflag && line[++i] == 's') {
324*0Sstevel@tonic-gate pl = line[++i];
325*0Sstevel@tonic-gate if (isdigit(pl)) {
326*0Sstevel@tonic-gate n = pl - '0';
327*0Sstevel@tonic-gate pl = ' ';
328*0Sstevel@tonic-gate } else
329*0Sstevel@tonic-gate n = 0;
330*0Sstevel@tonic-gate while (isdigit(line[++i]))
331*0Sstevel@tonic-gate n = 10 * n + line[i] - '0';
332*0Sstevel@tonic-gate i--;
333*0Sstevel@tonic-gate if (n == 0) {
334*0Sstevel@tonic-gate if (stk[stktop].opno == SZ) {
335*0Sstevel@tonic-gate stktop--;
336*0Sstevel@tonic-gate } else {
337*0Sstevel@tonic-gate pe(lineno);
338*0Sstevel@tonic-gate (void) printf(
339*0Sstevel@tonic-gate gettext("unmatched \\s0\n"));
340*0Sstevel@tonic-gate }
341*0Sstevel@tonic-gate } else {
342*0Sstevel@tonic-gate growstk();
343*0Sstevel@tonic-gate stk[stktop].opno = SZ;
344*0Sstevel@tonic-gate stk[stktop].pl = pl;
345*0Sstevel@tonic-gate stk[stktop].parm = n;
346*0Sstevel@tonic-gate stk[stktop].lno = lineno;
347*0Sstevel@tonic-gate }
348*0Sstevel@tonic-gate } else if (!fflag && line[i] == 'f') {
349*0Sstevel@tonic-gate n = line[++i];
350*0Sstevel@tonic-gate if (n == 'P') {
351*0Sstevel@tonic-gate if (stk[stktop].opno == FT) {
352*0Sstevel@tonic-gate stktop--;
353*0Sstevel@tonic-gate } else {
354*0Sstevel@tonic-gate pe(lineno);
355*0Sstevel@tonic-gate (void) printf(
356*0Sstevel@tonic-gate gettext("unmatched \\fP\n"));
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate } else {
359*0Sstevel@tonic-gate growstk();
360*0Sstevel@tonic-gate stk[stktop].opno = FT;
361*0Sstevel@tonic-gate stk[stktop].pl = 1;
362*0Sstevel@tonic-gate stk[stktop].parm = n;
363*0Sstevel@tonic-gate stk[stktop].lno = lineno;
364*0Sstevel@tonic-gate }
365*0Sstevel@tonic-gate }
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate /*
369*0Sstevel@tonic-gate * We've hit the end and look at all this stuff that hasn't been
370*0Sstevel@tonic-gate * matched yet! Complain, complain.
371*0Sstevel@tonic-gate */
372*0Sstevel@tonic-gate for (i = stktop; i >= 0; i--) {
373*0Sstevel@tonic-gate complain(i);
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate
377*0Sstevel@tonic-gate static void
complain(int i)378*0Sstevel@tonic-gate complain(int i)
379*0Sstevel@tonic-gate {
380*0Sstevel@tonic-gate pe(stk[i].lno);
381*0Sstevel@tonic-gate (void) printf(gettext("Unmatched "));
382*0Sstevel@tonic-gate prop(i);
383*0Sstevel@tonic-gate (void) printf("\n");
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gate static void
prop(int i)387*0Sstevel@tonic-gate prop(int i)
388*0Sstevel@tonic-gate {
389*0Sstevel@tonic-gate if (stk[i].pl == 0)
390*0Sstevel@tonic-gate (void) printf(".%s", br[stk[i].opno].opbr);
391*0Sstevel@tonic-gate else switch (stk[i].opno) {
392*0Sstevel@tonic-gate case SZ:
393*0Sstevel@tonic-gate (void) printf("\\s%c%d", stk[i].pl, stk[i].parm);
394*0Sstevel@tonic-gate break;
395*0Sstevel@tonic-gate case FT:
396*0Sstevel@tonic-gate (void) printf("\\f%c", stk[i].parm);
397*0Sstevel@tonic-gate break;
398*0Sstevel@tonic-gate default:
399*0Sstevel@tonic-gate (void) printf(gettext("Bug: stk[%d].opno = %d = .%s, .%s"),
400*0Sstevel@tonic-gate i, stk[i].opno, br[stk[i].opno].opbr,
401*0Sstevel@tonic-gate br[stk[i].opno].clbr);
402*0Sstevel@tonic-gate }
403*0Sstevel@tonic-gate }
404*0Sstevel@tonic-gate
405*0Sstevel@tonic-gate /* ARGSUSED */
406*0Sstevel@tonic-gate static void
chkcmd(char * line,char * mac)407*0Sstevel@tonic-gate chkcmd(char *line, char *mac)
408*0Sstevel@tonic-gate {
409*0Sstevel@tonic-gate int i;
410*0Sstevel@tonic-gate
411*0Sstevel@tonic-gate /*
412*0Sstevel@tonic-gate * Check to see if it matches top of stack.
413*0Sstevel@tonic-gate */
414*0Sstevel@tonic-gate if (stktop >= 0 && eq(mac, br[stk[stktop].opno].clbr))
415*0Sstevel@tonic-gate stktop--; /* OK. Pop & forget */
416*0Sstevel@tonic-gate else {
417*0Sstevel@tonic-gate /* No. Maybe it's an opener */
418*0Sstevel@tonic-gate for (i = 0; br[i].opbr; i++) {
419*0Sstevel@tonic-gate if (eq(mac, br[i].opbr)) {
420*0Sstevel@tonic-gate /* Found. Push it. */
421*0Sstevel@tonic-gate growstk();
422*0Sstevel@tonic-gate stk[stktop].opno = i;
423*0Sstevel@tonic-gate stk[stktop].pl = 0;
424*0Sstevel@tonic-gate stk[stktop].parm = 0;
425*0Sstevel@tonic-gate stk[stktop].lno = lineno;
426*0Sstevel@tonic-gate break;
427*0Sstevel@tonic-gate }
428*0Sstevel@tonic-gate /*
429*0Sstevel@tonic-gate * Maybe it's an unmatched closer.
430*0Sstevel@tonic-gate * NOTE: this depends on the fact
431*0Sstevel@tonic-gate * that none of the closers can be
432*0Sstevel@tonic-gate * openers too.
433*0Sstevel@tonic-gate */
434*0Sstevel@tonic-gate if (eq(mac, br[i].clbr)) {
435*0Sstevel@tonic-gate nomatch(mac);
436*0Sstevel@tonic-gate break;
437*0Sstevel@tonic-gate }
438*0Sstevel@tonic-gate }
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate }
441*0Sstevel@tonic-gate
442*0Sstevel@tonic-gate static void
nomatch(char * mac)443*0Sstevel@tonic-gate nomatch(char *mac)
444*0Sstevel@tonic-gate {
445*0Sstevel@tonic-gate int i, j;
446*0Sstevel@tonic-gate
447*0Sstevel@tonic-gate /*
448*0Sstevel@tonic-gate * Look for a match further down on stack
449*0Sstevel@tonic-gate * If we find one, it suggests that the stuff in
450*0Sstevel@tonic-gate * between is supposed to match itself.
451*0Sstevel@tonic-gate */
452*0Sstevel@tonic-gate for (j = stktop; j >= 0; j--)
453*0Sstevel@tonic-gate if (eq(mac, br[stk[j].opno].clbr)) {
454*0Sstevel@tonic-gate /* Found. Make a good diagnostic. */
455*0Sstevel@tonic-gate if (j == stktop-2) {
456*0Sstevel@tonic-gate /*
457*0Sstevel@tonic-gate * Check for special case \fx..\fR and don't
458*0Sstevel@tonic-gate * complain.
459*0Sstevel@tonic-gate */
460*0Sstevel@tonic-gate if (stk[j+1].opno == FT &&
461*0Sstevel@tonic-gate stk[j+1].parm != 'R' &&
462*0Sstevel@tonic-gate stk[j+2].opno == FT &&
463*0Sstevel@tonic-gate stk[j+2].parm == 'R') {
464*0Sstevel@tonic-gate stktop = j -1;
465*0Sstevel@tonic-gate return;
466*0Sstevel@tonic-gate }
467*0Sstevel@tonic-gate /*
468*0Sstevel@tonic-gate * We have two unmatched frobs. Chances are
469*0Sstevel@tonic-gate * they were intended to match, so we mention
470*0Sstevel@tonic-gate * them together.
471*0Sstevel@tonic-gate */
472*0Sstevel@tonic-gate pe(stk[j+1].lno);
473*0Sstevel@tonic-gate prop(j+1);
474*0Sstevel@tonic-gate (void) printf(gettext(" does not match %d: "),
475*0Sstevel@tonic-gate stk[j+2].lno);
476*0Sstevel@tonic-gate prop(j+2);
477*0Sstevel@tonic-gate (void) printf("\n");
478*0Sstevel@tonic-gate } else for (i = j+1; i <= stktop; i++) {
479*0Sstevel@tonic-gate complain(i);
480*0Sstevel@tonic-gate }
481*0Sstevel@tonic-gate stktop = j-1;
482*0Sstevel@tonic-gate return;
483*0Sstevel@tonic-gate }
484*0Sstevel@tonic-gate /* Didn't find one. Throw this away. */
485*0Sstevel@tonic-gate pe(lineno);
486*0Sstevel@tonic-gate (void) printf(gettext("Unmatched .%s\n"), mac);
487*0Sstevel@tonic-gate }
488*0Sstevel@tonic-gate
489*0Sstevel@tonic-gate /* eq: are two strings equal? */
490*0Sstevel@tonic-gate static int
eq(char * s1,char * s2)491*0Sstevel@tonic-gate eq(char *s1, char *s2)
492*0Sstevel@tonic-gate {
493*0Sstevel@tonic-gate return (strcmp(s1, s2) == 0);
494*0Sstevel@tonic-gate }
495*0Sstevel@tonic-gate
496*0Sstevel@tonic-gate /* print the first part of an error message, given the line number */
497*0Sstevel@tonic-gate static void
pe(int lineno)498*0Sstevel@tonic-gate pe(int lineno)
499*0Sstevel@tonic-gate {
500*0Sstevel@tonic-gate if (nfiles > 1)
501*0Sstevel@tonic-gate (void) printf("%s: ", cfilename);
502*0Sstevel@tonic-gate (void) printf("%d: ", lineno);
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate
505*0Sstevel@tonic-gate static void
checkknown(char * mac)506*0Sstevel@tonic-gate checkknown(char *mac)
507*0Sstevel@tonic-gate {
508*0Sstevel@tonic-gate
509*0Sstevel@tonic-gate if (eq(mac, "."))
510*0Sstevel@tonic-gate return;
511*0Sstevel@tonic-gate if (binsrch(mac) >= 0)
512*0Sstevel@tonic-gate return;
513*0Sstevel@tonic-gate if (mac[0] == '\\' && mac[1] == '"') /* comments */
514*0Sstevel@tonic-gate return;
515*0Sstevel@tonic-gate
516*0Sstevel@tonic-gate pe(lineno);
517*0Sstevel@tonic-gate (void) printf(gettext("Unknown command: .%s\n"), mac);
518*0Sstevel@tonic-gate }
519*0Sstevel@tonic-gate
520*0Sstevel@tonic-gate /*
521*0Sstevel@tonic-gate * We have a .de xx line in "line". Add xx to the list of known commands.
522*0Sstevel@tonic-gate */
523*0Sstevel@tonic-gate static void
addcmd(char * line)524*0Sstevel@tonic-gate addcmd(char *line)
525*0Sstevel@tonic-gate {
526*0Sstevel@tonic-gate char *mac;
527*0Sstevel@tonic-gate
528*0Sstevel@tonic-gate /* grab the macro being defined */
529*0Sstevel@tonic-gate mac = line+4;
530*0Sstevel@tonic-gate while (isspace(*mac))
531*0Sstevel@tonic-gate mac++;
532*0Sstevel@tonic-gate if (*mac == 0) {
533*0Sstevel@tonic-gate pe(lineno);
534*0Sstevel@tonic-gate (void) printf(gettext("illegal define: %s\n"), line);
535*0Sstevel@tonic-gate return;
536*0Sstevel@tonic-gate }
537*0Sstevel@tonic-gate mac[2] = 0;
538*0Sstevel@tonic-gate if (isspace(mac[1]) || mac[1] == '\\')
539*0Sstevel@tonic-gate mac[1] = 0;
540*0Sstevel@tonic-gate if (ncmds >= MAXCMDS) {
541*0Sstevel@tonic-gate (void) printf(gettext("Only %d known commands allowed\n"),
542*0Sstevel@tonic-gate MAXCMDS);
543*0Sstevel@tonic-gate exit(1);
544*0Sstevel@tonic-gate }
545*0Sstevel@tonic-gate addmac(mac);
546*0Sstevel@tonic-gate }
547*0Sstevel@tonic-gate
548*0Sstevel@tonic-gate /*
549*0Sstevel@tonic-gate * Add mac to the list. We should really have some kind of tree
550*0Sstevel@tonic-gate * structure here but this is a quick-and-dirty job and I just don't
551*0Sstevel@tonic-gate * have time to mess with it. (I wonder if this will come back to haunt
552*0Sstevel@tonic-gate * me someday?) Anyway, I claim that .de is fairly rare in user
553*0Sstevel@tonic-gate * nroff programs, and the loop below is pretty fast.
554*0Sstevel@tonic-gate */
555*0Sstevel@tonic-gate static void
addmac(char * mac)556*0Sstevel@tonic-gate addmac(char *mac)
557*0Sstevel@tonic-gate {
558*0Sstevel@tonic-gate char **src, **dest, **loc;
559*0Sstevel@tonic-gate
560*0Sstevel@tonic-gate if (binsrch(mac) >= 0) { /* it's OK to redefine something */
561*0Sstevel@tonic-gate #ifdef DEBUG
562*0Sstevel@tonic-gate (void) printf("binsrch(%s) -> already in table\n", mac);
563*0Sstevel@tonic-gate #endif
564*0Sstevel@tonic-gate return;
565*0Sstevel@tonic-gate }
566*0Sstevel@tonic-gate /* binsrch sets slot as a side effect */
567*0Sstevel@tonic-gate #ifdef DEBUG
568*0Sstevel@tonic-gate printf("binsrch(%s) -> %d\n", mac, slot);
569*0Sstevel@tonic-gate #endif
570*0Sstevel@tonic-gate loc = &knowncmds[slot];
571*0Sstevel@tonic-gate src = &knowncmds[ncmds-1];
572*0Sstevel@tonic-gate dest = src+1;
573*0Sstevel@tonic-gate while (dest > loc)
574*0Sstevel@tonic-gate *dest-- = *src--;
575*0Sstevel@tonic-gate *loc = malloc(3);
576*0Sstevel@tonic-gate (void) strcpy(*loc, mac);
577*0Sstevel@tonic-gate ncmds++;
578*0Sstevel@tonic-gate #ifdef DEBUG
579*0Sstevel@tonic-gate (void) printf("after: %s %s %s %s %s, %d cmds\n",
580*0Sstevel@tonic-gate knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot],
581*0Sstevel@tonic-gate knowncmds[slot+1], knowncmds[slot+2], ncmds);
582*0Sstevel@tonic-gate #endif
583*0Sstevel@tonic-gate }
584*0Sstevel@tonic-gate
585*0Sstevel@tonic-gate /*
586*0Sstevel@tonic-gate * Do a binary search in knowncmds for mac.
587*0Sstevel@tonic-gate * If found, return the index. If not, return -1.
588*0Sstevel@tonic-gate */
589*0Sstevel@tonic-gate static int
binsrch(char * mac)590*0Sstevel@tonic-gate binsrch(char *mac)
591*0Sstevel@tonic-gate {
592*0Sstevel@tonic-gate char *p; /* pointer to current cmd in list */
593*0Sstevel@tonic-gate int d; /* difference if any */
594*0Sstevel@tonic-gate int mid; /* mid point in binary search */
595*0Sstevel@tonic-gate int top, bot; /* boundaries of bin search, inclusive */
596*0Sstevel@tonic-gate
597*0Sstevel@tonic-gate top = ncmds-1;
598*0Sstevel@tonic-gate bot = 0;
599*0Sstevel@tonic-gate while (top >= bot) {
600*0Sstevel@tonic-gate mid = (top+bot)/2;
601*0Sstevel@tonic-gate p = knowncmds[mid];
602*0Sstevel@tonic-gate d = p[0] - mac[0];
603*0Sstevel@tonic-gate if (d == 0)
604*0Sstevel@tonic-gate d = p[1] - mac[1];
605*0Sstevel@tonic-gate if (d == 0)
606*0Sstevel@tonic-gate return (mid);
607*0Sstevel@tonic-gate if (d < 0)
608*0Sstevel@tonic-gate bot = mid + 1;
609*0Sstevel@tonic-gate else
610*0Sstevel@tonic-gate top = mid - 1;
611*0Sstevel@tonic-gate }
612*0Sstevel@tonic-gate slot = bot; /* place it would have gone */
613*0Sstevel@tonic-gate return (-1);
614*0Sstevel@tonic-gate }
615