17c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27c478bd9Sstevel@tonic-gate /* All Rights Reserved */
37c478bd9Sstevel@tonic-gate
47c478bd9Sstevel@tonic-gate
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
77c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
87c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
97c478bd9Sstevel@tonic-gate */
107c478bd9Sstevel@tonic-gate
117c478bd9Sstevel@tonic-gate /*
127c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
137c478bd9Sstevel@tonic-gate * Use is subject to license terms.
147c478bd9Sstevel@tonic-gate */
157c478bd9Sstevel@tonic-gate
16*1b58875aSJohn Levon /*
17*1b58875aSJohn Levon * Copyright (c) 2018, Joyent, Inc.
18*1b58875aSJohn Levon */
197c478bd9Sstevel@tonic-gate
207c478bd9Sstevel@tonic-gate /*
217c478bd9Sstevel@tonic-gate * checknr: check an nroff/troff input file for matching macro calls.
227c478bd9Sstevel@tonic-gate * we also attempt to match size and font changes, but only the embedded
237c478bd9Sstevel@tonic-gate * kind. These must end in \s0 and \fP resp. Maybe more sophistication
247c478bd9Sstevel@tonic-gate * later but for now think of these restrictions as contributions to
257c478bd9Sstevel@tonic-gate * structured typesetting.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <locale.h>
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #define MAXSTK 100 /* Stack size */
357c478bd9Sstevel@tonic-gate static int maxstk;
367c478bd9Sstevel@tonic-gate #define MAXBR 100 /* Max number of bracket pairs known */
377c478bd9Sstevel@tonic-gate #define MAXCMDS 500 /* Max number of commands known */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate * The stack on which we remember what we've seen so far.
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate static struct stkstr {
437c478bd9Sstevel@tonic-gate int opno; /* number of opening bracket */
447c478bd9Sstevel@tonic-gate int pl; /* '+', '-', ' ' for \s, 1 for \f, 0 for .ft */
457c478bd9Sstevel@tonic-gate int parm; /* parm to size, font, etc */
467c478bd9Sstevel@tonic-gate int lno; /* line number the thing came in in */
477c478bd9Sstevel@tonic-gate } *stk;
487c478bd9Sstevel@tonic-gate static int stktop;
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * The kinds of opening and closing brackets.
527c478bd9Sstevel@tonic-gate */
537c478bd9Sstevel@tonic-gate static struct brstr {
547c478bd9Sstevel@tonic-gate char *opbr;
557c478bd9Sstevel@tonic-gate char *clbr;
567c478bd9Sstevel@tonic-gate } br[MAXBR] = {
577c478bd9Sstevel@tonic-gate /* A few bare bones troff commands */
587c478bd9Sstevel@tonic-gate #define SZ 0
597c478bd9Sstevel@tonic-gate "sz", "sz", /* also \s */
607c478bd9Sstevel@tonic-gate #define FT 1
617c478bd9Sstevel@tonic-gate "ft", "ft", /* also \f */
627c478bd9Sstevel@tonic-gate /* the -mm package */
637c478bd9Sstevel@tonic-gate "AL", "LE",
647c478bd9Sstevel@tonic-gate "AS", "AE",
657c478bd9Sstevel@tonic-gate "BL", "LE",
667c478bd9Sstevel@tonic-gate "BS", "BE",
677c478bd9Sstevel@tonic-gate "DF", "DE",
687c478bd9Sstevel@tonic-gate "DL", "LE",
697c478bd9Sstevel@tonic-gate "DS", "DE",
707c478bd9Sstevel@tonic-gate "FS", "FE",
717c478bd9Sstevel@tonic-gate "ML", "LE",
727c478bd9Sstevel@tonic-gate "NS", "NE",
737c478bd9Sstevel@tonic-gate "RL", "LE",
747c478bd9Sstevel@tonic-gate "VL", "LE",
757c478bd9Sstevel@tonic-gate /* the -ms package */
767c478bd9Sstevel@tonic-gate "AB", "AE",
777c478bd9Sstevel@tonic-gate "BD", "DE",
787c478bd9Sstevel@tonic-gate "CD", "DE",
797c478bd9Sstevel@tonic-gate "DS", "DE",
807c478bd9Sstevel@tonic-gate "FS", "FE",
817c478bd9Sstevel@tonic-gate "ID", "DE",
827c478bd9Sstevel@tonic-gate "KF", "KE",
837c478bd9Sstevel@tonic-gate "KS", "KE",
847c478bd9Sstevel@tonic-gate "LD", "DE",
857c478bd9Sstevel@tonic-gate "LG", "NL",
867c478bd9Sstevel@tonic-gate "QS", "QE",
877c478bd9Sstevel@tonic-gate "RS", "RE",
887c478bd9Sstevel@tonic-gate "SM", "NL",
897c478bd9Sstevel@tonic-gate "XA", "XE",
907c478bd9Sstevel@tonic-gate "XS", "XE",
917c478bd9Sstevel@tonic-gate /* The -me package */
927c478bd9Sstevel@tonic-gate "(b", ")b",
937c478bd9Sstevel@tonic-gate "(c", ")c",
947c478bd9Sstevel@tonic-gate "(d", ")d",
957c478bd9Sstevel@tonic-gate "(f", ")f",
967c478bd9Sstevel@tonic-gate "(l", ")l",
977c478bd9Sstevel@tonic-gate "(q", ")q",
987c478bd9Sstevel@tonic-gate "(x", ")x",
997c478bd9Sstevel@tonic-gate "(z", ")z",
1007c478bd9Sstevel@tonic-gate /* Things needed by preprocessors */
1017c478bd9Sstevel@tonic-gate "EQ", "EN",
1027c478bd9Sstevel@tonic-gate "TS", "TE",
1037c478bd9Sstevel@tonic-gate /* Refer */
1047c478bd9Sstevel@tonic-gate "[", "]",
1057c478bd9Sstevel@tonic-gate 0, 0
1067c478bd9Sstevel@tonic-gate };
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate * All commands known to nroff, plus macro packages.
1107c478bd9Sstevel@tonic-gate * Used so we can complain about unrecognized commands.
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate static char *knowncmds[MAXCMDS] = {
1137c478bd9Sstevel@tonic-gate "$c", "$f", "$h", "$p", "$s", "(b", "(c", "(d", "(f", "(l", "(q", "(t",
1147c478bd9Sstevel@tonic-gate "(x", "(z", ")b", ")c", ")d", ")f", ")l", ")q", ")t", ")x", ")z", "++",
1157c478bd9Sstevel@tonic-gate "+c", "1C", "1c", "2C", "2c", "@(", "@)", "@C", "@D", "@F", "@I", "@M",
1167c478bd9Sstevel@tonic-gate "@c", "@e", "@f", "@h", "@m", "@n", "@o", "@p", "@r", "@t", "@z", "AB",
1177c478bd9Sstevel@tonic-gate "AE", "AF", "AI", "AL", "AM", "AS", "AT", "AU", "AX", "B", "B1", "B2",
1187c478bd9Sstevel@tonic-gate "BD", "BE", "BG", "BL", "BS", "BT", "BX", "C1", "C2", "CD", "CM", "CT",
1197c478bd9Sstevel@tonic-gate "D", "DA", "DE", "DF", "DL", "DS", "DT", "EC", "EF", "EG", "EH", "EM",
1207c478bd9Sstevel@tonic-gate "EN", "EQ", "EX", "FA", "FD", "FE", "FG", "FJ", "FK", "FL", "FN", "FO",
1217c478bd9Sstevel@tonic-gate "FQ", "FS", "FV", "FX", "H", "HC", "HD", "HM", "HO", "HU", "I", "ID",
1227c478bd9Sstevel@tonic-gate "IE", "IH", "IM", "IP", "IX", "IZ", "KD", "KE", "KF", "KQ", "KS", "LB",
1237c478bd9Sstevel@tonic-gate "LC", "LD", "LE", "LG", "LI", "LP", "MC", "ME", "MF", "MH", "ML", "MR",
1247c478bd9Sstevel@tonic-gate "MT", "ND", "NE", "NH", "NL", "NP", "NS", "OF", "OH", "OK", "OP", "P",
1257c478bd9Sstevel@tonic-gate "P1", "PF", "PH", "PP", "PT", "PX", "PY", "QE", "QP", "QS", "R", "RA",
1267c478bd9Sstevel@tonic-gate "RC", "RE", "RL", "RP", "RQ", "RS", "RT", "S", "S0", "S2", "S3", "SA",
1277c478bd9Sstevel@tonic-gate "SG", "SH", "SK", "SM", "SP", "SY", "T&", "TA", "TB", "TC", "TD", "TE",
1287c478bd9Sstevel@tonic-gate "TH", "TL", "TM", "TP", "TQ", "TR", "TS", "TX", "UL", "US", "UX", "VL",
1297c478bd9Sstevel@tonic-gate "WC", "WH", "XA", "XD", "XE", "XF", "XK", "XP", "XS", "[", "[-", "[0",
1307c478bd9Sstevel@tonic-gate "[1", "[2", "[3", "[4", "[5", "[<", "[>", "[]", "]", "]-", "]<", "]>",
1317c478bd9Sstevel@tonic-gate "][", "ab", "ac", "ad", "af", "am", "ar", "as", "b", "ba", "bc", "bd",
1327c478bd9Sstevel@tonic-gate "bi", "bl", "bp", "br", "bx", "c.", "c2", "cc", "ce", "cf", "ch", "cs",
1337c478bd9Sstevel@tonic-gate "ct", "cu", "da", "de", "di", "dl", "dn", "ds", "dt", "dw", "dy", "ec",
1347c478bd9Sstevel@tonic-gate "ef", "eh", "el", "em", "eo", "ep", "ev", "ex", "fc", "fi", "fl", "fo",
1357c478bd9Sstevel@tonic-gate "fp", "ft", "fz", "hc", "he", "hl", "hp", "ht", "hw", "hx", "hy", "i",
1367c478bd9Sstevel@tonic-gate "ie", "if", "ig", "in", "ip", "it", "ix", "lc", "lg", "li", "ll", "ln",
1377c478bd9Sstevel@tonic-gate "lo", "lp", "ls", "lt", "m1", "m2", "m3", "m4", "mc", "mk", "mo", "n1",
1387c478bd9Sstevel@tonic-gate "n2", "na", "ne", "nf", "nh", "nl", "nm", "nn", "np", "nr", "ns", "nx",
1397c478bd9Sstevel@tonic-gate "of", "oh", "os", "pa", "pc", "pi", "pl", "pm", "pn", "po", "pp", "ps",
1407c478bd9Sstevel@tonic-gate "q", "r", "rb", "rd", "re", "rm", "rn", "ro", "rr", "rs", "rt", "sb",
1417c478bd9Sstevel@tonic-gate "sc", "sh", "sk", "so", "sp", "ss", "st", "sv", "sz", "ta", "tc", "th",
1427c478bd9Sstevel@tonic-gate "ti", "tl", "tm", "tp", "tr", "u", "uf", "uh", "ul", "vs", "wh", "xp",
1437c478bd9Sstevel@tonic-gate "yr", 0
1447c478bd9Sstevel@tonic-gate };
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate static int lineno; /* current line number in input file */
1477c478bd9Sstevel@tonic-gate static char line[256]; /* the current line */
1487c478bd9Sstevel@tonic-gate static char *cfilename; /* name of current file */
1497c478bd9Sstevel@tonic-gate static int nfiles; /* number of files to process */
1507c478bd9Sstevel@tonic-gate static int fflag; /* -f: ignore \f */
1517c478bd9Sstevel@tonic-gate static int sflag; /* -s: ignore \s */
1527c478bd9Sstevel@tonic-gate static int ncmds; /* size of knowncmds */
1537c478bd9Sstevel@tonic-gate static int slot; /* slot in knowncmds found by binsrch */
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate static void growstk();
1567c478bd9Sstevel@tonic-gate static void usage();
1577c478bd9Sstevel@tonic-gate static void process(FILE *f);
1587c478bd9Sstevel@tonic-gate static void complain(int i);
1597c478bd9Sstevel@tonic-gate static void prop(int i);
1607c478bd9Sstevel@tonic-gate static void chkcmd(char *line, char *mac);
1617c478bd9Sstevel@tonic-gate static void nomatch(char *mac);
1627c478bd9Sstevel@tonic-gate static int eq(char *s1, char *s2);
1637c478bd9Sstevel@tonic-gate static void pe(int lineno);
1647c478bd9Sstevel@tonic-gate static void checkknown(char *mac);
1657c478bd9Sstevel@tonic-gate static void addcmd(char *line);
1667c478bd9Sstevel@tonic-gate static void addmac(char *mac);
1677c478bd9Sstevel@tonic-gate static int binsrch(char *mac);
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate static void
growstk()1707c478bd9Sstevel@tonic-gate growstk()
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate stktop++;
1737c478bd9Sstevel@tonic-gate if (stktop >= maxstk) {
1747c478bd9Sstevel@tonic-gate maxstk *= 2;
1757c478bd9Sstevel@tonic-gate stk = (struct stkstr *)realloc(stk,
1767c478bd9Sstevel@tonic-gate sizeof (struct stkstr) * maxstk);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate int
main(argc,argv)1817c478bd9Sstevel@tonic-gate main(argc, argv)
1827c478bd9Sstevel@tonic-gate int argc;
1837c478bd9Sstevel@tonic-gate char **argv;
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate FILE *f;
1867c478bd9Sstevel@tonic-gate int i;
1877c478bd9Sstevel@tonic-gate char *cp;
1887c478bd9Sstevel@tonic-gate char b1[4];
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1917c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1927c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
1937c478bd9Sstevel@tonic-gate #endif
1947c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
195*1b58875aSJohn Levon stk = (struct stkstr *)calloc(100, sizeof (struct stkstr));
1967c478bd9Sstevel@tonic-gate maxstk = 100;
1977c478bd9Sstevel@tonic-gate /* Figure out how many known commands there are */
1987c478bd9Sstevel@tonic-gate while (knowncmds[ncmds])
1997c478bd9Sstevel@tonic-gate ncmds++;
2007c478bd9Sstevel@tonic-gate while (argc > 1 && argv[1][0] == '-') {
2017c478bd9Sstevel@tonic-gate switch (argv[1][1]) {
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /* -a: add pairs of macros */
2047c478bd9Sstevel@tonic-gate case 'a':
2057c478bd9Sstevel@tonic-gate i = strlen(argv[1]) - 2;
2067c478bd9Sstevel@tonic-gate if (i % 6 != 0)
2077c478bd9Sstevel@tonic-gate usage();
2087c478bd9Sstevel@tonic-gate /* look for empty macro slots */
2097c478bd9Sstevel@tonic-gate for (i = 0; br[i].opbr; i++)
2107c478bd9Sstevel@tonic-gate ;
2117c478bd9Sstevel@tonic-gate for (cp = argv[1]+3; cp[-1]; cp += 6) {
2127c478bd9Sstevel@tonic-gate br[i].opbr = malloc(3);
2137c478bd9Sstevel@tonic-gate (void) strncpy(br[i].opbr, cp, 2);
2147c478bd9Sstevel@tonic-gate br[i].clbr = malloc(3);
2157c478bd9Sstevel@tonic-gate (void) strncpy(br[i].clbr, cp+3, 2);
2167c478bd9Sstevel@tonic-gate /* knows pairs are also known cmds */
2177c478bd9Sstevel@tonic-gate addmac(br[i].opbr);
2187c478bd9Sstevel@tonic-gate addmac(br[i].clbr);
2197c478bd9Sstevel@tonic-gate i++;
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate break;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate /* -c: add known commands */
2247c478bd9Sstevel@tonic-gate case 'c':
2257c478bd9Sstevel@tonic-gate i = strlen(argv[1]) - 2;
2267c478bd9Sstevel@tonic-gate if (i % 3 != 0)
2277c478bd9Sstevel@tonic-gate usage();
2287c478bd9Sstevel@tonic-gate for (cp = argv[1]+3; cp[-1]; cp += 3) {
2297c478bd9Sstevel@tonic-gate if (cp[2] && cp[2] != '.')
2307c478bd9Sstevel@tonic-gate usage();
2317c478bd9Sstevel@tonic-gate (void) strncpy(b1, cp, 2);
2327c478bd9Sstevel@tonic-gate addmac(b1);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate break;
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate /* -f: ignore font changes */
2377c478bd9Sstevel@tonic-gate case 'f':
2387c478bd9Sstevel@tonic-gate fflag = 1;
2397c478bd9Sstevel@tonic-gate break;
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate /* -s: ignore size changes */
2427c478bd9Sstevel@tonic-gate case 's':
2437c478bd9Sstevel@tonic-gate sflag = 1;
2447c478bd9Sstevel@tonic-gate break;
2457c478bd9Sstevel@tonic-gate default:
2467c478bd9Sstevel@tonic-gate usage();
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate argc--; argv++;
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate nfiles = argc - 1;
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate if (nfiles > 0) {
2547c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) {
2557c478bd9Sstevel@tonic-gate cfilename = argv[i];
2567c478bd9Sstevel@tonic-gate f = fopen(cfilename, "r");
2577c478bd9Sstevel@tonic-gate if (f == NULL) {
2587c478bd9Sstevel@tonic-gate perror(cfilename);
2597c478bd9Sstevel@tonic-gate exit(1);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate else
2627c478bd9Sstevel@tonic-gate process(f);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate } else {
2657c478bd9Sstevel@tonic-gate cfilename = "stdin";
2667c478bd9Sstevel@tonic-gate process(stdin);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate return (0);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate static void
usage()2727c478bd9Sstevel@tonic-gate usage()
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate (void) printf(gettext("Usage: \
2757c478bd9Sstevel@tonic-gate checknr [ -fs ] [ -a.xx.yy.xx.yy...] [-c.xx.xx.xx...] [ filename .. ]\n"));
2767c478bd9Sstevel@tonic-gate exit(1);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate static void
process(FILE * f)2807c478bd9Sstevel@tonic-gate process(FILE *f)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate int i, n;
2837c478bd9Sstevel@tonic-gate char mac[5]; /* The current macro or nroff command */
2847c478bd9Sstevel@tonic-gate int pl;
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate stktop = -1;
2877c478bd9Sstevel@tonic-gate for (lineno = 1; fgets(line, sizeof (line), f); lineno++) {
2887c478bd9Sstevel@tonic-gate if (line[0] == '.') {
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate * find and isolate the macro/command name.
2917c478bd9Sstevel@tonic-gate */
2927c478bd9Sstevel@tonic-gate (void) strncpy(mac, line+1, 4);
2937c478bd9Sstevel@tonic-gate if (isspace(mac[0])) {
2947c478bd9Sstevel@tonic-gate pe(lineno);
2957c478bd9Sstevel@tonic-gate (void) printf(gettext("Empty command\n"));
2967c478bd9Sstevel@tonic-gate } else if (isspace(mac[1])) {
2977c478bd9Sstevel@tonic-gate mac[1] = 0;
2987c478bd9Sstevel@tonic-gate } else if (isspace(mac[2])) {
2997c478bd9Sstevel@tonic-gate mac[2] = 0;
3007c478bd9Sstevel@tonic-gate } else if (mac[0] != '\\' || mac[1] != '\"') {
3017c478bd9Sstevel@tonic-gate pe(lineno);
3027c478bd9Sstevel@tonic-gate (void) printf(gettext("Command too long\n"));
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate * Is it a known command?
3077c478bd9Sstevel@tonic-gate */
3087c478bd9Sstevel@tonic-gate checkknown(mac);
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate * Should we add it?
3127c478bd9Sstevel@tonic-gate */
3137c478bd9Sstevel@tonic-gate if (eq(mac, "de"))
3147c478bd9Sstevel@tonic-gate addcmd(line);
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate chkcmd(line, mac);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate * At this point we process the line looking
3217c478bd9Sstevel@tonic-gate * for \s and \f.
3227c478bd9Sstevel@tonic-gate */
3237c478bd9Sstevel@tonic-gate for (i = 0; line[i]; i++)
3247c478bd9Sstevel@tonic-gate if (line[i] == '\\' && (i == 0 || line[i-1] != '\\')) {
3257c478bd9Sstevel@tonic-gate if (!sflag && line[++i] == 's') {
3267c478bd9Sstevel@tonic-gate pl = line[++i];
3277c478bd9Sstevel@tonic-gate if (isdigit(pl)) {
3287c478bd9Sstevel@tonic-gate n = pl - '0';
3297c478bd9Sstevel@tonic-gate pl = ' ';
3307c478bd9Sstevel@tonic-gate } else
3317c478bd9Sstevel@tonic-gate n = 0;
3327c478bd9Sstevel@tonic-gate while (isdigit(line[++i]))
3337c478bd9Sstevel@tonic-gate n = 10 * n + line[i] - '0';
3347c478bd9Sstevel@tonic-gate i--;
3357c478bd9Sstevel@tonic-gate if (n == 0) {
3367c478bd9Sstevel@tonic-gate if (stk[stktop].opno == SZ) {
3377c478bd9Sstevel@tonic-gate stktop--;
3387c478bd9Sstevel@tonic-gate } else {
3397c478bd9Sstevel@tonic-gate pe(lineno);
3407c478bd9Sstevel@tonic-gate (void) printf(
3417c478bd9Sstevel@tonic-gate gettext("unmatched \\s0\n"));
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate } else {
3447c478bd9Sstevel@tonic-gate growstk();
3457c478bd9Sstevel@tonic-gate stk[stktop].opno = SZ;
3467c478bd9Sstevel@tonic-gate stk[stktop].pl = pl;
3477c478bd9Sstevel@tonic-gate stk[stktop].parm = n;
3487c478bd9Sstevel@tonic-gate stk[stktop].lno = lineno;
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate } else if (!fflag && line[i] == 'f') {
3517c478bd9Sstevel@tonic-gate n = line[++i];
3527c478bd9Sstevel@tonic-gate if (n == 'P') {
3537c478bd9Sstevel@tonic-gate if (stk[stktop].opno == FT) {
3547c478bd9Sstevel@tonic-gate stktop--;
3557c478bd9Sstevel@tonic-gate } else {
3567c478bd9Sstevel@tonic-gate pe(lineno);
3577c478bd9Sstevel@tonic-gate (void) printf(
3587c478bd9Sstevel@tonic-gate gettext("unmatched \\fP\n"));
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate } else {
3617c478bd9Sstevel@tonic-gate growstk();
3627c478bd9Sstevel@tonic-gate stk[stktop].opno = FT;
3637c478bd9Sstevel@tonic-gate stk[stktop].pl = 1;
3647c478bd9Sstevel@tonic-gate stk[stktop].parm = n;
3657c478bd9Sstevel@tonic-gate stk[stktop].lno = lineno;
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate * We've hit the end and look at all this stuff that hasn't been
3727c478bd9Sstevel@tonic-gate * matched yet! Complain, complain.
3737c478bd9Sstevel@tonic-gate */
3747c478bd9Sstevel@tonic-gate for (i = stktop; i >= 0; i--) {
3757c478bd9Sstevel@tonic-gate complain(i);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate static void
complain(int i)3807c478bd9Sstevel@tonic-gate complain(int i)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate pe(stk[i].lno);
3837c478bd9Sstevel@tonic-gate (void) printf(gettext("Unmatched "));
3847c478bd9Sstevel@tonic-gate prop(i);
3857c478bd9Sstevel@tonic-gate (void) printf("\n");
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate static void
prop(int i)3897c478bd9Sstevel@tonic-gate prop(int i)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate if (stk[i].pl == 0)
3927c478bd9Sstevel@tonic-gate (void) printf(".%s", br[stk[i].opno].opbr);
3937c478bd9Sstevel@tonic-gate else switch (stk[i].opno) {
3947c478bd9Sstevel@tonic-gate case SZ:
3957c478bd9Sstevel@tonic-gate (void) printf("\\s%c%d", stk[i].pl, stk[i].parm);
3967c478bd9Sstevel@tonic-gate break;
3977c478bd9Sstevel@tonic-gate case FT:
3987c478bd9Sstevel@tonic-gate (void) printf("\\f%c", stk[i].parm);
3997c478bd9Sstevel@tonic-gate break;
4007c478bd9Sstevel@tonic-gate default:
4017c478bd9Sstevel@tonic-gate (void) printf(gettext("Bug: stk[%d].opno = %d = .%s, .%s"),
4027c478bd9Sstevel@tonic-gate i, stk[i].opno, br[stk[i].opno].opbr,
4037c478bd9Sstevel@tonic-gate br[stk[i].opno].clbr);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate /* ARGSUSED */
4087c478bd9Sstevel@tonic-gate static void
chkcmd(char * line,char * mac)4097c478bd9Sstevel@tonic-gate chkcmd(char *line, char *mac)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate int i;
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate * Check to see if it matches top of stack.
4157c478bd9Sstevel@tonic-gate */
4167c478bd9Sstevel@tonic-gate if (stktop >= 0 && eq(mac, br[stk[stktop].opno].clbr))
4177c478bd9Sstevel@tonic-gate stktop--; /* OK. Pop & forget */
4187c478bd9Sstevel@tonic-gate else {
4197c478bd9Sstevel@tonic-gate /* No. Maybe it's an opener */
4207c478bd9Sstevel@tonic-gate for (i = 0; br[i].opbr; i++) {
4217c478bd9Sstevel@tonic-gate if (eq(mac, br[i].opbr)) {
4227c478bd9Sstevel@tonic-gate /* Found. Push it. */
4237c478bd9Sstevel@tonic-gate growstk();
4247c478bd9Sstevel@tonic-gate stk[stktop].opno = i;
4257c478bd9Sstevel@tonic-gate stk[stktop].pl = 0;
4267c478bd9Sstevel@tonic-gate stk[stktop].parm = 0;
4277c478bd9Sstevel@tonic-gate stk[stktop].lno = lineno;
4287c478bd9Sstevel@tonic-gate break;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate * Maybe it's an unmatched closer.
4327c478bd9Sstevel@tonic-gate * NOTE: this depends on the fact
4337c478bd9Sstevel@tonic-gate * that none of the closers can be
4347c478bd9Sstevel@tonic-gate * openers too.
4357c478bd9Sstevel@tonic-gate */
4367c478bd9Sstevel@tonic-gate if (eq(mac, br[i].clbr)) {
4377c478bd9Sstevel@tonic-gate nomatch(mac);
4387c478bd9Sstevel@tonic-gate break;
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate static void
nomatch(char * mac)4457c478bd9Sstevel@tonic-gate nomatch(char *mac)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate int i, j;
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate * Look for a match further down on stack
4517c478bd9Sstevel@tonic-gate * If we find one, it suggests that the stuff in
4527c478bd9Sstevel@tonic-gate * between is supposed to match itself.
4537c478bd9Sstevel@tonic-gate */
4547c478bd9Sstevel@tonic-gate for (j = stktop; j >= 0; j--)
4557c478bd9Sstevel@tonic-gate if (eq(mac, br[stk[j].opno].clbr)) {
4567c478bd9Sstevel@tonic-gate /* Found. Make a good diagnostic. */
4577c478bd9Sstevel@tonic-gate if (j == stktop-2) {
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate * Check for special case \fx..\fR and don't
4607c478bd9Sstevel@tonic-gate * complain.
4617c478bd9Sstevel@tonic-gate */
4627c478bd9Sstevel@tonic-gate if (stk[j+1].opno == FT &&
4637c478bd9Sstevel@tonic-gate stk[j+1].parm != 'R' &&
4647c478bd9Sstevel@tonic-gate stk[j+2].opno == FT &&
4657c478bd9Sstevel@tonic-gate stk[j+2].parm == 'R') {
4667c478bd9Sstevel@tonic-gate stktop = j -1;
4677c478bd9Sstevel@tonic-gate return;
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate * We have two unmatched frobs. Chances are
4717c478bd9Sstevel@tonic-gate * they were intended to match, so we mention
4727c478bd9Sstevel@tonic-gate * them together.
4737c478bd9Sstevel@tonic-gate */
4747c478bd9Sstevel@tonic-gate pe(stk[j+1].lno);
4757c478bd9Sstevel@tonic-gate prop(j+1);
4767c478bd9Sstevel@tonic-gate (void) printf(gettext(" does not match %d: "),
4777c478bd9Sstevel@tonic-gate stk[j+2].lno);
4787c478bd9Sstevel@tonic-gate prop(j+2);
4797c478bd9Sstevel@tonic-gate (void) printf("\n");
4807c478bd9Sstevel@tonic-gate } else for (i = j+1; i <= stktop; i++) {
4817c478bd9Sstevel@tonic-gate complain(i);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate stktop = j-1;
4847c478bd9Sstevel@tonic-gate return;
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate /* Didn't find one. Throw this away. */
4877c478bd9Sstevel@tonic-gate pe(lineno);
4887c478bd9Sstevel@tonic-gate (void) printf(gettext("Unmatched .%s\n"), mac);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate /* eq: are two strings equal? */
4927c478bd9Sstevel@tonic-gate static int
eq(char * s1,char * s2)4937c478bd9Sstevel@tonic-gate eq(char *s1, char *s2)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate return (strcmp(s1, s2) == 0);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate /* print the first part of an error message, given the line number */
4997c478bd9Sstevel@tonic-gate static void
pe(int lineno)5007c478bd9Sstevel@tonic-gate pe(int lineno)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate if (nfiles > 1)
5037c478bd9Sstevel@tonic-gate (void) printf("%s: ", cfilename);
5047c478bd9Sstevel@tonic-gate (void) printf("%d: ", lineno);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate static void
checkknown(char * mac)5087c478bd9Sstevel@tonic-gate checkknown(char *mac)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate if (eq(mac, "."))
5127c478bd9Sstevel@tonic-gate return;
5137c478bd9Sstevel@tonic-gate if (binsrch(mac) >= 0)
5147c478bd9Sstevel@tonic-gate return;
5157c478bd9Sstevel@tonic-gate if (mac[0] == '\\' && mac[1] == '"') /* comments */
5167c478bd9Sstevel@tonic-gate return;
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate pe(lineno);
5197c478bd9Sstevel@tonic-gate (void) printf(gettext("Unknown command: .%s\n"), mac);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate * We have a .de xx line in "line". Add xx to the list of known commands.
5247c478bd9Sstevel@tonic-gate */
5257c478bd9Sstevel@tonic-gate static void
addcmd(char * line)5267c478bd9Sstevel@tonic-gate addcmd(char *line)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate char *mac;
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate /* grab the macro being defined */
5317c478bd9Sstevel@tonic-gate mac = line+4;
5327c478bd9Sstevel@tonic-gate while (isspace(*mac))
5337c478bd9Sstevel@tonic-gate mac++;
5347c478bd9Sstevel@tonic-gate if (*mac == 0) {
5357c478bd9Sstevel@tonic-gate pe(lineno);
5367c478bd9Sstevel@tonic-gate (void) printf(gettext("illegal define: %s\n"), line);
5377c478bd9Sstevel@tonic-gate return;
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate mac[2] = 0;
5407c478bd9Sstevel@tonic-gate if (isspace(mac[1]) || mac[1] == '\\')
5417c478bd9Sstevel@tonic-gate mac[1] = 0;
5427c478bd9Sstevel@tonic-gate if (ncmds >= MAXCMDS) {
5437c478bd9Sstevel@tonic-gate (void) printf(gettext("Only %d known commands allowed\n"),
5447c478bd9Sstevel@tonic-gate MAXCMDS);
5457c478bd9Sstevel@tonic-gate exit(1);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate addmac(mac);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate /*
5517c478bd9Sstevel@tonic-gate * Add mac to the list. We should really have some kind of tree
5527c478bd9Sstevel@tonic-gate * structure here but this is a quick-and-dirty job and I just don't
5537c478bd9Sstevel@tonic-gate * have time to mess with it. (I wonder if this will come back to haunt
5547c478bd9Sstevel@tonic-gate * me someday?) Anyway, I claim that .de is fairly rare in user
5557c478bd9Sstevel@tonic-gate * nroff programs, and the loop below is pretty fast.
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate static void
addmac(char * mac)5587c478bd9Sstevel@tonic-gate addmac(char *mac)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate char **src, **dest, **loc;
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate if (binsrch(mac) >= 0) { /* it's OK to redefine something */
5637c478bd9Sstevel@tonic-gate #ifdef DEBUG
5647c478bd9Sstevel@tonic-gate (void) printf("binsrch(%s) -> already in table\n", mac);
5657c478bd9Sstevel@tonic-gate #endif
5667c478bd9Sstevel@tonic-gate return;
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate /* binsrch sets slot as a side effect */
5697c478bd9Sstevel@tonic-gate #ifdef DEBUG
5707c478bd9Sstevel@tonic-gate printf("binsrch(%s) -> %d\n", mac, slot);
5717c478bd9Sstevel@tonic-gate #endif
5727c478bd9Sstevel@tonic-gate loc = &knowncmds[slot];
5737c478bd9Sstevel@tonic-gate src = &knowncmds[ncmds-1];
5747c478bd9Sstevel@tonic-gate dest = src+1;
5757c478bd9Sstevel@tonic-gate while (dest > loc)
5767c478bd9Sstevel@tonic-gate *dest-- = *src--;
5777c478bd9Sstevel@tonic-gate *loc = malloc(3);
5787c478bd9Sstevel@tonic-gate (void) strcpy(*loc, mac);
5797c478bd9Sstevel@tonic-gate ncmds++;
5807c478bd9Sstevel@tonic-gate #ifdef DEBUG
5817c478bd9Sstevel@tonic-gate (void) printf("after: %s %s %s %s %s, %d cmds\n",
5827c478bd9Sstevel@tonic-gate knowncmds[slot-2], knowncmds[slot-1], knowncmds[slot],
5837c478bd9Sstevel@tonic-gate knowncmds[slot+1], knowncmds[slot+2], ncmds);
5847c478bd9Sstevel@tonic-gate #endif
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate * Do a binary search in knowncmds for mac.
5897c478bd9Sstevel@tonic-gate * If found, return the index. If not, return -1.
5907c478bd9Sstevel@tonic-gate */
5917c478bd9Sstevel@tonic-gate static int
binsrch(char * mac)5927c478bd9Sstevel@tonic-gate binsrch(char *mac)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate char *p; /* pointer to current cmd in list */
5957c478bd9Sstevel@tonic-gate int d; /* difference if any */
5967c478bd9Sstevel@tonic-gate int mid; /* mid point in binary search */
5977c478bd9Sstevel@tonic-gate int top, bot; /* boundaries of bin search, inclusive */
5987c478bd9Sstevel@tonic-gate
5997c478bd9Sstevel@tonic-gate top = ncmds-1;
6007c478bd9Sstevel@tonic-gate bot = 0;
6017c478bd9Sstevel@tonic-gate while (top >= bot) {
6027c478bd9Sstevel@tonic-gate mid = (top+bot)/2;
6037c478bd9Sstevel@tonic-gate p = knowncmds[mid];
6047c478bd9Sstevel@tonic-gate d = p[0] - mac[0];
6057c478bd9Sstevel@tonic-gate if (d == 0)
6067c478bd9Sstevel@tonic-gate d = p[1] - mac[1];
6077c478bd9Sstevel@tonic-gate if (d == 0)
6087c478bd9Sstevel@tonic-gate return (mid);
6097c478bd9Sstevel@tonic-gate if (d < 0)
6107c478bd9Sstevel@tonic-gate bot = mid + 1;
6117c478bd9Sstevel@tonic-gate else
6127c478bd9Sstevel@tonic-gate top = mid - 1;
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate slot = bot; /* place it would have gone */
6157c478bd9Sstevel@tonic-gate return (-1);
6167c478bd9Sstevel@tonic-gate }
617