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