include aculib in cleanup
[unix-history] / usr / src / usr.bin / checknr / checknr.c
index 3f8a703..87faf93 100644 (file)
@@ -1,4 +1,4 @@
-static char *sccsid = "@(#)checknr.c   4.1 (Berkeley) %G%";
+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
 /*
  * checknr: check an nroff/troff input file for matching macro calls.
  * we also attempt to match size and font changes, but only the embedded
@@ -36,71 +36,83 @@ struct brstr {
        "sz",   "sz",   /* also \s */
 #define FT     1
        "ft",   "ft",   /* also \f */
        "sz",   "sz",   /* also \s */
 #define FT     1
        "ft",   "ft",   /* also \f */
+       /* the -mm package */
+       "AL",   "LE",
+       "AS",   "AE",
+       "BL",   "LE",
+       "BS",   "BE",
+       "DF",   "DE",
+       "DL",   "LE",
+       "DS",   "DE",
+       "FS",   "FE",
+       "ML",   "LE",
+       "NS",   "NE",
+       "RL",   "LE",
+       "VL",   "LE",
        /* the -ms package */
        "AB",   "AE",
        /* the -ms package */
        "AB",   "AE",
-       "RS",   "RE",
-       "LG",   "NL",
-       "SM",   "NL",
-       "FS",   "FE",
-       "DS",   "DE",
        "CD",   "DE",
        "CD",   "DE",
-       "LD",   "DE",
+       "DS",   "DE",
+       "FS",   "FE",
        "ID",   "DE",
        "ID",   "DE",
-       "KS",   "KE",
        "KF",   "KE",
        "KF",   "KE",
+       "KS",   "KE",
+       "LD",   "DE",
+       "LG",   "NL",
        "QS",   "QE",
        "QS",   "QE",
-       /* Things needed by preprocessors */
-       "TS",   "TE",
-       "EQ",   "EN",
+       "RS",   "RE",
+       "SM",   "NL",
        /* The -me package */
        /* The -me package */
-       "(l",   ")l",
-       "(q",   ")q",
        "(b",   ")b",
        "(b",   ")b",
-       "(z",   ")z",
        "(c",   ")c",
        "(d",   ")d",
        "(f",   ")f",
        "(c",   ")c",
        "(d",   ")d",
        "(f",   ")f",
+       "(l",   ")l",
+       "(q",   ")q",
        "(x",   ")x",
        "(x",   ")x",
+       "(z",   ")z",
+       /* Things needed by preprocessors */
+       "EQ",   "EN",
+       "TS",   "TE",
+       /* Refer */
+       "[",    "]",
        0,      0
 };
 
 /*
        0,      0
 };
 
 /*
- * All commands known to nroff, plus ms and me.
+ * All commands known to nroff, plus macro packages.
  * Used so we can complain about unrecognized commands.
  */
 char *knowncmds[MAXCMDS] = {
  * 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",
+"$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",
 0
 };
 
 0
 };
 
@@ -122,38 +134,57 @@ char **argv;
        FILE *f;
        int i;
        char *cp;
        FILE *f;
        int i;
        char *cp;
+       char b1[4];
 
 
-       if (argc <= 1)
-               goto usage;
+       /* Figure out how many known commands there are */
+       while (knowncmds[ncmds])
+               ncmds++;
        while (argc > 1 && argv[1][0] == '-') {
                switch(argv[1][1]) {
        while (argc > 1 && argv[1][0] == '-') {
                switch(argv[1][1]) {
+
+               /* -a: add pairs of macros */
                case 'a':
                case 'a':
-                       /* -a: add pairs of macros */
                        i = strlen(argv[1]) - 2;
                        i = strlen(argv[1]) - 2;
-                       if (i % 6 != 0) {
-usage:
-                               printf("Usage: nrc -s -f -a.xx.yy.xx.yy... (.xx, .yy)\n");
-                               break;
-                       }
+                       if (i % 6 != 0)
+                               usage();
                        /* look for empty macro slots */
                        for (i=0; br[i].opbr; i++)
                                ;
                        for (cp=argv[1]+3; cp[-1]; cp += 6) {
                        /* look for empty macro slots */
                        for (i=0; br[i].opbr; i++)
                                ;
                        for (cp=argv[1]+3; cp[-1]; cp += 6) {
-                               br[i].opbr = cp;
-                               br[i].clbr = cp+3;
-                               cp[2] = cp[5] = 0;
+                               br[i].opbr = malloc(3);
+                               strncpy(br[i].opbr, cp, 2);
+                               br[i].clbr = malloc(3);
+                               strncpy(br[i].clbr, cp+3, 2);
+                               addmac(br[i].opbr);     /* knows pairs are also known cmds */
+                               addmac(br[i].clbr);
                                i++;
                        }
                        break;
                                i++;
                        }
                        break;
+
+               /* -c: add known commands */
+               case 'c':
+                       i = strlen(argv[1]) - 2;
+                       if (i % 3 != 0)
+                               usage();
+                       for (cp=argv[1]+3; cp[-1]; cp += 3) {
+                               if (cp[2] && cp[2] != '.')
+                                       usage();
+                               strncpy(b1, cp, 2);
+                               addmac(b1);
+                       }
+                       break;
+
+               /* -f: ignore font changes */
                case 'f':
                        fflag = 1;
                        break;
                case 'f':
                        fflag = 1;
                        break;
+
+               /* -s: ignore size changes */
                case 's':
                        sflag = 1;
                        break;
                default:
                case 's':
                        sflag = 1;
                        break;
                default:
-                       printf("Illegal flag: %s\n", argv[1]);
-                       break;
+                       usage();
                }
                argc--; argv++;
        }
                }
                argc--; argv++;
        }
@@ -176,6 +207,12 @@ usage:
        exit(0);
 }
 
        exit(0);
 }
 
+usage()
+{
+       printf("Usage: checknr -s -f -a.xx.yy.xx.yy... -c.xx.xx.xx...\n");
+       exit(1);
+}
+
 process(f)
 FILE *f;
 {
 process(f)
 FILE *f;
 {
@@ -197,7 +234,7 @@ FILE *f;
                                mac[1] = 0;
                        } else if (isspace(mac[2])) {
                                mac[2] = 0;
                                mac[1] = 0;
                        } else if (isspace(mac[2])) {
                                mac[2] = 0;
-                       } else if (mac[2] != '\\' || mac[3] != '\"') {
+                       } else if (mac[0] != '\\' || mac[1] != '\"') {
                                pe(lineno);
                                printf("Command too long\n");
                        }
                                pe(lineno);
                                printf("Command too long\n");
                        }
@@ -397,16 +434,13 @@ int lineno;
 checkknown(mac)
 char *mac;
 {
 checkknown(mac)
 char *mac;
 {
-       /* First time figure out ncmds. */
-       if (ncmds == 0) {
-               while (knowncmds[ncmds])
-                       ncmds++;
-       }
 
        if (eq(mac, "."))
                return;
        if (binsrch(mac) >= 0)
                return;
 
        if (eq(mac, "."))
                return;
        if (binsrch(mac) >= 0)
                return;
+       if (mac[0] == '\\' && mac[1] == '"')    /* comments */
+               return;
 
        pe(lineno);
        printf("Unknown command: .%s\n", mac);
 
        pe(lineno);
        printf("Unknown command: .%s\n", mac);
@@ -419,7 +453,6 @@ addcmd(line)
 char *line;
 {
        char *mac;
 char *line;
 {
        char *mac;
-       register char **src, **dest, **loc;
 
        /* grab the macro being defined */
        mac = line+4;
 
        /* grab the macro being defined */
        mac = line+4;
@@ -437,16 +470,26 @@ char *line;
                printf("Only %d known commands allowed\n", MAXCMDS);
                exit(1);
        }
                printf("Only %d known commands allowed\n", MAXCMDS);
                exit(1);
        }
-       
-       /*
-        * 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.
-        */
+       addmac(mac);
+}
+
+/*
+ * 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.
+ */
+addmac(mac)
+char *mac;
+{
+       register char **src, **dest, **loc;
+
        binsrch(mac);   /* it's OK to redefine something */
        /* binsrch sets slot as a side effect */
        binsrch(mac);   /* it's OK to redefine something */
        /* binsrch sets slot as a side effect */
+#ifdef DEBUG
+printf("binsrch(%s) -> %d\n", mac, slot);
+#endif
        loc = &knowncmds[slot];
        src = &knowncmds[ncmds-1];
        dest = src+1;
        loc = &knowncmds[slot];
        src = &knowncmds[ncmds-1];
        dest = src+1;
@@ -455,6 +498,9 @@ char *line;
        *loc = malloc(3);
        strcpy(*loc, mac);
        ncmds++;
        *loc = malloc(3);
        strcpy(*loc, mac);
        ncmds++;
+#ifdef DEBUG
+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);
+#endif
 }
 
 /*
 }
 
 /*
@@ -487,3 +533,5 @@ char *mac;
        slot = bot;     /* place it would have gone */
        return -1;
 }
        slot = bot;     /* place it would have gone */
        return -1;
 }
+
+