[BACK]Return to checknr.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / checknr

Annotation of src/usr.bin/checknr/checknr.c, Revision 1.11

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