[BACK]Return to makewhatis.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/makewhatis.c, Revision 1.10

1.10    ! kristaps    1: /*     $Id: makewhatis.c,v 1.9 2011/06/22 09:10:36 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
                     21: #include <sys/param.h>
                     22:
                     23: #include <assert.h>
                     24: #include <fcntl.h>
                     25: #include <getopt.h>
                     26: #include <stdio.h>
                     27: #include <stdint.h>
                     28: #include <stdlib.h>
                     29: #include <string.h>
                     30:
1.10    ! kristaps   31: #ifdef __linux__
        !            32: # include <db_185.h>
        !            33: #else
        !            34: # include <db.h>
        !            35: #endif
        !            36:
1.1       kristaps   37: #include "man.h"
                     38: #include "mdoc.h"
                     39: #include "mandoc.h"
                     40:
                     41: #define        MANDOC_DB        "mandoc.db"
                     42: #define        MANDOC_IDX       "mandoc.index"
                     43: #define        MANDOC_BUFSZ      BUFSIZ
                     44: #define        MANDOC_FLAGS      O_CREAT|O_TRUNC|O_RDWR
                     45:
1.5       kristaps   46: /* Bit-fields.  See makewhatis.1. */
                     47:
1.10    ! kristaps   48: #define TYPE_NAME        0x01
        !            49: #define TYPE_FUNCTION    0x02
        !            50: #define TYPE_UTILITY     0x04
        !            51: #define TYPE_INCLUDES    0x08
        !            52: #define TYPE_VARIABLE    0x10
        !            53: #define TYPE_STANDARD    0x20
        !            54: #define TYPE_AUTHOR      0x40
        !            55: #define TYPE_CONFIG      0x80
        !            56: #define TYPE_DESC        0x100
1.3       kristaps   57:
1.5       kristaps   58: /* Buffer for storing growable data. */
                     59:
1.3       kristaps   60: struct buf {
                     61:        char             *cp;
                     62:        size_t            len;
                     63:        size_t            size;
1.1       kristaps   64: };
                     65:
1.3       kristaps   66: #define        MAN_ARGS          DB *hash, \
                     67:                          struct buf *buf, \
1.5       kristaps   68:                          struct buf *dbuf, \
1.1       kristaps   69:                          const struct man_node *n
1.3       kristaps   70: #define        MDOC_ARGS         DB *hash, \
                     71:                          struct buf *buf, \
1.5       kristaps   72:                          struct buf *dbuf, \
1.3       kristaps   73:                          const struct mdoc_node *n, \
                     74:                          const struct mdoc_meta *m
1.1       kristaps   75:
1.5       kristaps   76: static void              buf_append(struct buf *, const char *);
                     77: static void              buf_appendb(struct buf *,
1.1       kristaps   78:                                const void *, size_t);
                     79: static void              dbt_put(DB *, const char *, DBT *, DBT *);
1.3       kristaps   80: static void              hash_put(DB *, const struct buf *, int);
1.1       kristaps   81: static int               pman_node(MAN_ARGS);
                     82: static void              pmdoc_node(MDOC_ARGS);
                     83: static void              pmdoc_An(MDOC_ARGS);
                     84: static void              pmdoc_Cd(MDOC_ARGS);
                     85: static void              pmdoc_Fd(MDOC_ARGS);
                     86: static void              pmdoc_In(MDOC_ARGS);
                     87: static void              pmdoc_Fn(MDOC_ARGS);
                     88: static void              pmdoc_Fo(MDOC_ARGS);
                     89: static void              pmdoc_Nd(MDOC_ARGS);
                     90: static void              pmdoc_Nm(MDOC_ARGS);
                     91: static void              pmdoc_St(MDOC_ARGS);
                     92: static void              pmdoc_Vt(MDOC_ARGS);
1.5       kristaps   93: static void              usage(void);
1.1       kristaps   94:
                     95: typedef        void            (*pmdoc_nf)(MDOC_ARGS);
                     96:
                     97: static const pmdoc_nf    mdocs[MDOC_MAX] = {
                     98:        NULL, /* Ap */
                     99:        NULL, /* Dd */
                    100:        NULL, /* Dt */
                    101:        NULL, /* Os */
                    102:        NULL, /* Sh */
                    103:        NULL, /* Ss */
                    104:        NULL, /* Pp */
                    105:        NULL, /* D1 */
                    106:        NULL, /* Dl */
                    107:        NULL, /* Bd */
                    108:        NULL, /* Ed */
                    109:        NULL, /* Bl */
                    110:        NULL, /* El */
                    111:        NULL, /* It */
                    112:        NULL, /* Ad */
                    113:        pmdoc_An, /* An */
                    114:        NULL, /* Ar */
                    115:        pmdoc_Cd, /* Cd */
                    116:        NULL, /* Cm */
                    117:        NULL, /* Dv */
                    118:        NULL, /* Er */
                    119:        NULL, /* Ev */
                    120:        NULL, /* Ex */
                    121:        NULL, /* Fa */
                    122:        pmdoc_Fd, /* Fd */
                    123:        NULL, /* Fl */
                    124:        pmdoc_Fn, /* Fn */
                    125:        NULL, /* Ft */
                    126:        NULL, /* Ic */
                    127:        pmdoc_In, /* In */
                    128:        NULL, /* Li */
                    129:        pmdoc_Nd, /* Nd */
                    130:        pmdoc_Nm, /* Nm */
                    131:        NULL, /* Op */
                    132:        NULL, /* Ot */
                    133:        NULL, /* Pa */
                    134:        NULL, /* Rv */
                    135:        pmdoc_St, /* St */
                    136:        pmdoc_Vt, /* Va */
                    137:        pmdoc_Vt, /* Vt */
                    138:        NULL, /* Xr */
                    139:        NULL, /* %A */
                    140:        NULL, /* %B */
                    141:        NULL, /* %D */
                    142:        NULL, /* %I */
                    143:        NULL, /* %J */
                    144:        NULL, /* %N */
                    145:        NULL, /* %O */
                    146:        NULL, /* %P */
                    147:        NULL, /* %R */
                    148:        NULL, /* %T */
                    149:        NULL, /* %V */
                    150:        NULL, /* Ac */
                    151:        NULL, /* Ao */
                    152:        NULL, /* Aq */
                    153:        NULL, /* At */
                    154:        NULL, /* Bc */
                    155:        NULL, /* Bf */
                    156:        NULL, /* Bo */
                    157:        NULL, /* Bq */
                    158:        NULL, /* Bsx */
                    159:        NULL, /* Bx */
                    160:        NULL, /* Db */
                    161:        NULL, /* Dc */
                    162:        NULL, /* Do */
                    163:        NULL, /* Dq */
                    164:        NULL, /* Ec */
                    165:        NULL, /* Ef */
                    166:        NULL, /* Em */
                    167:        NULL, /* Eo */
                    168:        NULL, /* Fx */
                    169:        NULL, /* Ms */
                    170:        NULL, /* No */
                    171:        NULL, /* Ns */
                    172:        NULL, /* Nx */
                    173:        NULL, /* Ox */
                    174:        NULL, /* Pc */
                    175:        NULL, /* Pf */
                    176:        NULL, /* Po */
                    177:        NULL, /* Pq */
                    178:        NULL, /* Qc */
                    179:        NULL, /* Ql */
                    180:        NULL, /* Qo */
                    181:        NULL, /* Qq */
                    182:        NULL, /* Re */
                    183:        NULL, /* Rs */
                    184:        NULL, /* Sc */
                    185:        NULL, /* So */
                    186:        NULL, /* Sq */
                    187:        NULL, /* Sm */
                    188:        NULL, /* Sx */
                    189:        NULL, /* Sy */
                    190:        NULL, /* Tn */
                    191:        NULL, /* Ux */
                    192:        NULL, /* Xc */
                    193:        NULL, /* Xo */
                    194:        pmdoc_Fo, /* Fo */
                    195:        NULL, /* Fc */
                    196:        NULL, /* Oo */
                    197:        NULL, /* Oc */
                    198:        NULL, /* Bk */
                    199:        NULL, /* Ek */
                    200:        NULL, /* Bt */
                    201:        NULL, /* Hf */
                    202:        NULL, /* Fr */
                    203:        NULL, /* Ud */
                    204:        NULL, /* Lb */
                    205:        NULL, /* Lp */
                    206:        NULL, /* Lk */
                    207:        NULL, /* Mt */
                    208:        NULL, /* Brq */
                    209:        NULL, /* Bro */
                    210:        NULL, /* Brc */
                    211:        NULL, /* %C */
                    212:        NULL, /* Es */
                    213:        NULL, /* En */
                    214:        NULL, /* Dx */
                    215:        NULL, /* %Q */
                    216:        NULL, /* br */
                    217:        NULL, /* sp */
                    218:        NULL, /* %U */
                    219:        NULL, /* Ta */
                    220: };
                    221:
1.5       kristaps  222: static const char       *progname;
                    223:
1.1       kristaps  224: int
                    225: main(int argc, char *argv[])
                    226: {
                    227:        struct mparse   *mp; /* parse sequence */
                    228:        struct mdoc     *mdoc; /* resulting mdoc */
                    229:        struct man      *man; /* resulting man */
                    230:        char            *fn; /* current file being parsed */
                    231:        const char      *msec, /* manual section */
                    232:                        *mtitle, /* manual title */
                    233:                        *arch, /* manual architecture */
                    234:                        *dir; /* result dir (default: cwd) */
                    235:        char             ibuf[MAXPATHLEN], /* index fname */
                    236:                         ibbuf[MAXPATHLEN], /* index backup fname */
                    237:                         fbuf[MAXPATHLEN],  /* btree fname */
1.3       kristaps  238:                         fbbuf[MAXPATHLEN], /* btree backup fname */
                    239:                         vbuf[8]; /* stringified record number */
1.10    ! kristaps  240:        int              ch, seq, verb;
1.1       kristaps  241:        DB              *idx, /* index database */
1.3       kristaps  242:                        *db, /* keyword database */
                    243:                        *hash; /* temporary keyword hashtable */
1.5       kristaps  244:        DBT              key, val;
1.9       kristaps  245:        size_t           sv;
1.1       kristaps  246:        BTREEINFO        info; /* btree configuration */
                    247:        recno_t          rec; /* current record number */
1.5       kristaps  248:        struct buf       buf, /* keyword buffer */
                    249:                         dbuf; /* description buffer */
1.1       kristaps  250:        extern int       optind;
                    251:        extern char     *optarg;
                    252:
                    253:        progname = strrchr(argv[0], '/');
                    254:        if (progname == NULL)
                    255:                progname = argv[0];
                    256:        else
                    257:                ++progname;
                    258:
                    259:        dir = "";
1.10    ! kristaps  260:        verb = 0;
1.1       kristaps  261:
1.10    ! kristaps  262:        while (-1 != (ch = getopt(argc, argv, "d:v")))
1.1       kristaps  263:                switch (ch) {
                    264:                case ('d'):
                    265:                        dir = optarg;
                    266:                        break;
1.10    ! kristaps  267:                case ('v'):
        !           268:                        verb++;
        !           269:                        break;
1.1       kristaps  270:                default:
                    271:                        usage();
                    272:                        return((int)MANDOCLEVEL_BADARG);
                    273:                }
                    274:
                    275:        argc -= optind;
                    276:        argv += optind;
                    277:
                    278:        /*
                    279:         * Set up temporary file-names into which we're going to write
                    280:         * all of our data (both for the index and database).  These
                    281:         * will be securely renamed to the real file-names after we've
                    282:         * written all of our data.
                    283:         */
                    284:
                    285:        ibuf[0] = ibuf[MAXPATHLEN - 2] =
                    286:                ibbuf[0] = ibbuf[MAXPATHLEN - 2] =
                    287:                fbuf[0] = fbuf[MAXPATHLEN - 2] =
                    288:                fbbuf[0] = fbbuf[MAXPATHLEN - 2] = '\0';
                    289:
                    290:        strlcat(fbuf, dir, MAXPATHLEN);
                    291:        strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
                    292:
                    293:        strlcat(fbbuf, fbuf, MAXPATHLEN);
                    294:        strlcat(fbbuf, "~", MAXPATHLEN);
                    295:
                    296:        strlcat(ibuf, dir, MAXPATHLEN);
                    297:        strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
                    298:
                    299:        strlcat(ibbuf, ibuf, MAXPATHLEN);
                    300:        strlcat(ibbuf, "~", MAXPATHLEN);
                    301:
                    302:        if ('\0' != fbuf[MAXPATHLEN - 2] ||
                    303:                        '\0' != fbbuf[MAXPATHLEN - 2] ||
                    304:                        '\0' != ibuf[MAXPATHLEN - 2] ||
                    305:                        '\0' != ibbuf[MAXPATHLEN - 2]) {
1.10    ! kristaps  306:                fprintf(stderr, "%s: Path too long\n", dir);
1.1       kristaps  307:                exit((int)MANDOCLEVEL_SYSERR);
                    308:        }
                    309:
                    310:        /*
                    311:         * For the keyword database, open a BTREE database that allows
1.3       kristaps  312:         * duplicates.
                    313:         * For the index database, use a standard RECNO database type.
1.1       kristaps  314:         */
                    315:
                    316:        memset(&info, 0, sizeof(BTREEINFO));
                    317:        info.flags = R_DUP;
                    318:        db = dbopen(fbbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info);
                    319:
                    320:        if (NULL == db) {
                    321:                perror(fbbuf);
                    322:                exit((int)MANDOCLEVEL_SYSERR);
                    323:        }
                    324:
                    325:        idx = dbopen(ibbuf, MANDOC_FLAGS, 0644, DB_RECNO, NULL);
                    326:
                    327:        if (NULL == db) {
                    328:                perror(ibbuf);
                    329:                (*db->close)(db);
                    330:                exit((int)MANDOCLEVEL_SYSERR);
                    331:        }
                    332:
                    333:        /*
1.10    ! kristaps  334:         * Try parsing each manual given on the command line.
        !           335:         * If we fail, then emit an error and keep on going.
        !           336:         * Take resulting trees and push them down into the database code.
1.1       kristaps  337:         * Use the auto-parser and don't report any errors.
                    338:         */
                    339:
                    340:        mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
                    341:
                    342:        rec = 1;
1.9       kristaps  343:        hash = NULL;
1.3       kristaps  344:
                    345:        memset(&buf, 0, sizeof(struct buf));
1.5       kristaps  346:        memset(&dbuf, 0, sizeof(struct buf));
                    347:
                    348:        buf.size = dbuf.size = MANDOC_BUFSZ;
1.3       kristaps  349:
                    350:        buf.cp = mandoc_malloc(buf.size);
1.5       kristaps  351:        dbuf.cp = mandoc_malloc(dbuf.size);
1.1       kristaps  352:
                    353:        while (NULL != (fn = *argv++)) {
                    354:                mparse_reset(mp);
                    355:
1.10    ! kristaps  356:                /* Initialise the in-memory hash of keywords. */
        !           357:
1.8       kristaps  358:                if (hash)
                    359:                        (*hash->close)(hash);
                    360:
                    361:                hash = dbopen(NULL, MANDOC_FLAGS, 0644, DB_HASH, NULL);
                    362:
                    363:                if (NULL == hash) {
                    364:                        perror("hash");
                    365:                        exit((int)MANDOCLEVEL_SYSERR);
                    366:                }
                    367:
1.1       kristaps  368:                /* Parse and get (non-empty) AST. */
                    369:
                    370:                if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) {
                    371:                        fprintf(stderr, "%s: Parse failure\n", fn);
                    372:                        continue;
                    373:                }
1.10    ! kristaps  374:
1.1       kristaps  375:                mparse_result(mp, &mdoc, &man);
1.10    ! kristaps  376:
1.1       kristaps  377:                if (NULL == mdoc && NULL == man)
                    378:                        continue;
                    379:
                    380:                msec = NULL != mdoc ?
                    381:                        mdoc_meta(mdoc)->msec :
                    382:                        man_meta(man)->msec;
                    383:                mtitle = NULL != mdoc ?
                    384:                        mdoc_meta(mdoc)->title :
                    385:                        man_meta(man)->title;
                    386:                arch = NULL != mdoc ? mdoc_meta(mdoc)->arch : NULL;
                    387:
                    388:                /*
                    389:                 * The index record value consists of a nil-terminated
                    390:                 * filename, a nil-terminated manual section, and a
                    391:                 * nil-terminated description.  Since the description
                    392:                 * may not be set, we set a sentinel to see if we're
                    393:                 * going to write a nil byte in its place.
                    394:                 */
                    395:
1.5       kristaps  396:                dbuf.len = 0;
                    397:                buf_appendb(&dbuf, fn, strlen(fn) + 1);
                    398:                buf_appendb(&dbuf, msec, strlen(msec) + 1);
                    399:                buf_appendb(&dbuf, mtitle, strlen(mtitle) + 1);
                    400:                buf_appendb(&dbuf, arch ? arch : "",
1.1       kristaps  401:                                arch ? strlen(arch) + 1 : 1);
                    402:
1.5       kristaps  403:                sv = dbuf.len;
1.1       kristaps  404:
                    405:                /* Fix the record number in the btree value. */
                    406:
                    407:                if (mdoc)
1.5       kristaps  408:                        pmdoc_node(hash, &buf, &dbuf,
                    409:                                mdoc_node(mdoc), mdoc_meta(mdoc));
1.1       kristaps  410:                else
1.5       kristaps  411:                        pman_node(hash, &buf, &dbuf, man_node(man));
1.3       kristaps  412:
                    413:                /*
                    414:                 * Copy from the in-memory hashtable of pending keywords
                    415:                 * into the database.
                    416:                 */
                    417:
                    418:                memset(vbuf, 0, sizeof(uint32_t));
                    419:                memcpy(vbuf + 4, &rec, sizeof(uint32_t));
                    420:
                    421:                seq = R_FIRST;
                    422:                while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
1.7       kristaps  423:                        seq = R_NEXT;
                    424:
1.3       kristaps  425:                        memcpy(vbuf, val.data, sizeof(uint32_t));
                    426:                        val.size = sizeof(vbuf);
                    427:                        val.data = vbuf;
1.7       kristaps  428:
1.10    ! kristaps  429:                        if (verb > 1)
        !           430:                                printf("%s: Keyword %s (%zu): 0x%x\n",
        !           431:                                        fn, (char *)key.data, key.size,
1.8       kristaps  432:                                        *(int *)val.data);
                    433:
1.3       kristaps  434:                        dbt_put(db, fbbuf, &key, &val);
1.5       kristaps  435:
1.3       kristaps  436:                }
                    437:
                    438:                if (ch < 0) {
                    439:                        perror("hash");
                    440:                        exit((int)MANDOCLEVEL_SYSERR);
                    441:                }
1.1       kristaps  442:
                    443:                /*
1.3       kristaps  444:                 * Apply to the index.  If we haven't had a description
                    445:                 * set, put an empty one in now.
1.1       kristaps  446:                 */
                    447:
1.5       kristaps  448:                if (dbuf.len == sv)
                    449:                        buf_appendb(&dbuf, "", 1);
                    450:
                    451:                key.data = &rec;
                    452:                key.size = sizeof(recno_t);
1.1       kristaps  453:
1.5       kristaps  454:                val.data = dbuf.cp;
                    455:                val.size = dbuf.len;
1.1       kristaps  456:
1.10    ! kristaps  457:                if (verb > 0)
        !           458:                        printf("%s: Indexed\n", fn);
1.8       kristaps  459:
1.5       kristaps  460:                dbt_put(idx, ibbuf, &key, &val);
1.1       kristaps  461:                rec++;
                    462:        }
                    463:
                    464:        (*db->close)(db);
                    465:        (*idx->close)(idx);
1.10    ! kristaps  466:
1.8       kristaps  467:        if (hash)
                    468:                (*hash->close)(hash);
1.1       kristaps  469:
                    470:        mparse_free(mp);
                    471:
1.3       kristaps  472:        free(buf.cp);
1.5       kristaps  473:        free(dbuf.cp);
1.1       kristaps  474:
                    475:        /* Atomically replace the file with our temporary one. */
                    476:
                    477:        if (-1 == rename(fbbuf, fbuf))
                    478:                perror(fbuf);
                    479:        if (-1 == rename(ibbuf, ibuf))
                    480:                perror(fbuf);
                    481:
                    482:        return((int)MANDOCLEVEL_OK);
                    483: }
                    484:
                    485: /*
1.5       kristaps  486:  * Grow the buffer (if necessary) and copy in a binary string.
1.1       kristaps  487:  */
                    488: static void
1.3       kristaps  489: buf_appendb(struct buf *buf, const void *cp, size_t sz)
                    490: {
                    491:
                    492:        /* Overshoot by MANDOC_BUFSZ. */
                    493:
                    494:        while (buf->len + sz >= buf->size) {
                    495:                buf->size = buf->len + sz + MANDOC_BUFSZ;
                    496:                buf->cp = mandoc_realloc(buf->cp, buf->size);
                    497:        }
                    498:
                    499:        memcpy(buf->cp + (int)buf->len, cp, sz);
                    500:        buf->len += sz;
                    501: }
                    502:
1.1       kristaps  503: /*
1.5       kristaps  504:  * Append a nil-terminated string to the buffer.
                    505:  * This can be invoked multiple times.
                    506:  * The buffer string will be nil-terminated.
                    507:  * If invoked multiple times, a space is put between strings.
1.1       kristaps  508:  */
                    509: static void
1.3       kristaps  510: buf_append(struct buf *buf, const char *cp)
                    511: {
                    512:        size_t           sz;
                    513:
                    514:        if (0 == (sz = strlen(cp)))
                    515:                return;
                    516:
                    517:        if (buf->len)
                    518:                buf->cp[(int)buf->len - 1] = ' ';
                    519:
                    520:        buf_appendb(buf, cp, sz + 1);
                    521: }
                    522:
1.1       kristaps  523: /* ARGSUSED */
                    524: static void
                    525: pmdoc_An(MDOC_ARGS)
                    526: {
                    527:
                    528:        if (SEC_AUTHORS != n->sec)
                    529:                return;
                    530:
                    531:        for (n = n->child; n; n = n->next)
                    532:                if (MDOC_TEXT == n->type)
1.3       kristaps  533:                        buf_append(buf, n->string);
1.1       kristaps  534:
1.3       kristaps  535:        hash_put(hash, buf, TYPE_AUTHOR);
1.1       kristaps  536: }
                    537:
                    538: /* ARGSUSED */
                    539: static void
                    540: pmdoc_Fd(MDOC_ARGS)
                    541: {
                    542:        const char      *start, *end;
                    543:        size_t           sz;
                    544:
                    545:        if (SEC_SYNOPSIS != n->sec)
                    546:                return;
                    547:        if (NULL == (n = n->child) || MDOC_TEXT != n->type)
                    548:                return;
                    549:
                    550:        /*
                    551:         * Only consider those `Fd' macro fields that begin with an
                    552:         * "inclusion" token (versus, e.g., #define).
                    553:         */
                    554:        if (strcmp("#include", n->string))
                    555:                return;
                    556:
                    557:        if (NULL == (n = n->next) || MDOC_TEXT != n->type)
                    558:                return;
                    559:
                    560:        /*
                    561:         * Strip away the enclosing angle brackets and make sure we're
                    562:         * not zero-length.
                    563:         */
                    564:
                    565:        start = n->string;
                    566:        if ('<' == *start || '"' == *start)
                    567:                start++;
                    568:
                    569:        if (0 == (sz = strlen(start)))
                    570:                return;
                    571:
                    572:        end = &start[(int)sz - 1];
                    573:        if ('>' == *end || '"' == *end)
                    574:                end--;
                    575:
                    576:        assert(end >= start);
                    577:
1.3       kristaps  578:        buf_appendb(buf, start, (size_t)(end - start + 1));
                    579:        buf_appendb(buf, "", 1);
                    580:
                    581:        hash_put(hash, buf, TYPE_INCLUDES);
1.1       kristaps  582: }
                    583:
                    584: /* ARGSUSED */
                    585: static void
                    586: pmdoc_Cd(MDOC_ARGS)
                    587: {
                    588:
                    589:        if (SEC_SYNOPSIS != n->sec)
                    590:                return;
                    591:
                    592:        for (n = n->child; n; n = n->next)
                    593:                if (MDOC_TEXT == n->type)
1.3       kristaps  594:                        buf_append(buf, n->string);
1.1       kristaps  595:
1.3       kristaps  596:        hash_put(hash, buf, TYPE_CONFIG);
1.1       kristaps  597: }
                    598:
                    599: /* ARGSUSED */
                    600: static void
                    601: pmdoc_In(MDOC_ARGS)
                    602: {
                    603:
                    604:        if (SEC_SYNOPSIS != n->sec)
                    605:                return;
                    606:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    607:                return;
                    608:
1.3       kristaps  609:        buf_append(buf, n->child->string);
                    610:        hash_put(hash, buf, TYPE_INCLUDES);
1.1       kristaps  611: }
                    612:
                    613: /* ARGSUSED */
                    614: static void
                    615: pmdoc_Fn(MDOC_ARGS)
                    616: {
                    617:        const char      *cp;
                    618:
                    619:        if (SEC_SYNOPSIS != n->sec)
                    620:                return;
                    621:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    622:                return;
                    623:
                    624:        /* .Fn "struct type *arg" "foo" */
                    625:
                    626:        cp = strrchr(n->child->string, ' ');
                    627:        if (NULL == cp)
                    628:                cp = n->child->string;
                    629:
                    630:        /* Strip away pointer symbol. */
                    631:
                    632:        while ('*' == *cp)
                    633:                cp++;
                    634:
1.3       kristaps  635:        buf_append(buf, cp);
                    636:        hash_put(hash, buf, TYPE_FUNCTION);
1.1       kristaps  637: }
                    638:
                    639: /* ARGSUSED */
                    640: static void
                    641: pmdoc_St(MDOC_ARGS)
                    642: {
                    643:
                    644:        if (SEC_STANDARDS != n->sec)
                    645:                return;
                    646:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    647:                return;
                    648:
1.3       kristaps  649:        buf_append(buf, n->child->string);
                    650:        hash_put(hash, buf, TYPE_STANDARD);
1.1       kristaps  651: }
                    652:
                    653: /* ARGSUSED */
                    654: static void
                    655: pmdoc_Vt(MDOC_ARGS)
                    656: {
                    657:        const char      *start;
                    658:        size_t           sz;
                    659:
                    660:        if (SEC_SYNOPSIS != n->sec)
                    661:                return;
                    662:        if (MDOC_Vt == n->tok && MDOC_BODY != n->type)
                    663:                return;
                    664:        if (NULL == n->last || MDOC_TEXT != n->last->type)
                    665:                return;
                    666:
                    667:        /*
                    668:         * Strip away leading pointer symbol '*' and trailing ';'.
                    669:         */
                    670:
                    671:        start = n->last->string;
                    672:
                    673:        while ('*' == *start)
                    674:                start++;
                    675:
                    676:        if (0 == (sz = strlen(start)))
                    677:                return;
                    678:
                    679:        if (';' == start[(int)sz - 1])
                    680:                sz--;
                    681:
                    682:        if (0 == sz)
                    683:                return;
                    684:
1.3       kristaps  685:        buf_appendb(buf, start, sz);
                    686:        buf_appendb(buf, "", 1);
                    687:        hash_put(hash, buf, TYPE_VARIABLE);
1.1       kristaps  688: }
                    689:
                    690: /* ARGSUSED */
                    691: static void
                    692: pmdoc_Fo(MDOC_ARGS)
                    693: {
                    694:
                    695:        if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                    696:                return;
                    697:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    698:                return;
                    699:
1.3       kristaps  700:        buf_append(buf, n->child->string);
                    701:        hash_put(hash, buf, TYPE_FUNCTION);
1.1       kristaps  702: }
                    703:
                    704:
                    705: /* ARGSUSED */
                    706: static void
                    707: pmdoc_Nd(MDOC_ARGS)
                    708: {
                    709:        int              first;
1.6       kristaps  710:        size_t           sz;
1.1       kristaps  711:
                    712:        for (first = 1, n = n->child; n; n = n->next) {
                    713:                if (MDOC_TEXT != n->type)
                    714:                        continue;
1.6       kristaps  715:
                    716:                if (first) {
                    717:                        sz = strlen(n->string) + 1;
                    718:                        buf_appendb(dbuf, n->string, sz);
                    719:                        buf_appendb(buf, n->string, sz);
                    720:                } else {
1.5       kristaps  721:                        buf_append(dbuf, n->string);
1.6       kristaps  722:                        buf_append(buf, n->string);
                    723:                }
                    724:
1.1       kristaps  725:                first = 0;
                    726:        }
1.6       kristaps  727:
                    728:        hash_put(hash, buf, TYPE_DESC);
1.1       kristaps  729: }
                    730:
                    731: /* ARGSUSED */
                    732: static void
                    733: pmdoc_Nm(MDOC_ARGS)
                    734: {
                    735:
                    736:        if (SEC_NAME == n->sec) {
1.3       kristaps  737:                for (n = n->child; n; n = n->next)
                    738:                        if (MDOC_TEXT == n->type)
                    739:                                buf_append(buf, n->string);
                    740:                hash_put(hash, buf, TYPE_NAME);
1.1       kristaps  741:                return;
                    742:        } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                    743:                return;
                    744:
1.3       kristaps  745:        if (NULL == n->child)
                    746:                buf_append(buf, m->name);
                    747:
                    748:        for (n = n->child; n; n = n->next)
                    749:                if (MDOC_TEXT == n->type)
                    750:                        buf_append(buf, n->string);
                    751:
                    752:        hash_put(hash, buf, TYPE_UTILITY);
                    753: }
                    754:
                    755: static void
                    756: hash_put(DB *db, const struct buf *buf, int mask)
                    757: {
                    758:        DBT              key, val;
                    759:        int              rc;
                    760:
1.7       kristaps  761:        if (buf->len < 2)
                    762:                return;
                    763:
1.3       kristaps  764:        key.data = buf->cp;
1.7       kristaps  765:        key.size = buf->len;
1.3       kristaps  766:
                    767:        if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
                    768:                perror("hash");
                    769:                exit((int)MANDOCLEVEL_SYSERR);
                    770:        } else if (0 == rc)
                    771:                mask |= *(int *)val.data;
                    772:
                    773:        val.data = &mask;
                    774:        val.size = sizeof(int);
1.1       kristaps  775:
1.3       kristaps  776:        if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
                    777:                perror("hash");
                    778:                exit((int)MANDOCLEVEL_SYSERR);
                    779:        }
1.1       kristaps  780: }
                    781:
                    782: static void
                    783: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
                    784: {
                    785:
1.5       kristaps  786:        assert(key->size);
1.1       kristaps  787:        assert(val->size);
                    788:
                    789:        if (0 == (*db->put)(db, key, val, 0))
                    790:                return;
                    791:
                    792:        perror(dbn);
                    793:        exit((int)MANDOCLEVEL_SYSERR);
                    794:        /* NOTREACHED */
                    795: }
                    796:
                    797: /*
                    798:  * Call out to per-macro handlers after clearing the persistent database
                    799:  * key.  If the macro sets the database key, flush it to the database.
                    800:  */
                    801: static void
                    802: pmdoc_node(MDOC_ARGS)
                    803: {
                    804:
                    805:        if (NULL == n)
                    806:                return;
                    807:
                    808:        switch (n->type) {
                    809:        case (MDOC_HEAD):
                    810:                /* FALLTHROUGH */
                    811:        case (MDOC_BODY):
                    812:                /* FALLTHROUGH */
                    813:        case (MDOC_TAIL):
                    814:                /* FALLTHROUGH */
                    815:        case (MDOC_BLOCK):
                    816:                /* FALLTHROUGH */
                    817:        case (MDOC_ELEM):
                    818:                if (NULL == mdocs[n->tok])
                    819:                        break;
                    820:
1.3       kristaps  821:                buf->len = 0;
1.5       kristaps  822:                (*mdocs[n->tok])(hash, buf, dbuf, n, m);
1.1       kristaps  823:                break;
                    824:        default:
                    825:                break;
                    826:        }
                    827:
1.5       kristaps  828:        pmdoc_node(hash, buf, dbuf, n->child, m);
                    829:        pmdoc_node(hash, buf, dbuf, n->next, m);
1.1       kristaps  830: }
                    831:
                    832: static int
                    833: pman_node(MAN_ARGS)
                    834: {
                    835:        const struct man_node *head, *body;
                    836:        const char      *start, *sv;
                    837:        size_t           sz;
                    838:
                    839:        if (NULL == n)
                    840:                return(0);
                    841:
                    842:        /*
                    843:         * We're only searching for one thing: the first text child in
                    844:         * the BODY of a NAME section.  Since we don't keep track of
                    845:         * sections in -man, run some hoops to find out whether we're in
                    846:         * the correct section or not.
                    847:         */
                    848:
                    849:        if (MAN_BODY == n->type && MAN_SH == n->tok) {
                    850:                body = n;
                    851:                assert(body->parent);
                    852:                if (NULL != (head = body->parent->head) &&
                    853:                                1 == head->nchild &&
                    854:                                NULL != (head = (head->child)) &&
                    855:                                MAN_TEXT == head->type &&
                    856:                                0 == strcmp(head->string, "NAME") &&
                    857:                                NULL != (body = body->child) &&
                    858:                                MAN_TEXT == body->type) {
                    859:
                    860:                        assert(body->string);
                    861:                        start = sv = body->string;
                    862:
                    863:                        /*
                    864:                         * Go through a special heuristic dance here.
                    865:                         * This is why -man manuals are great!
                    866:                         * (I'm being sarcastic: my eyes are bleeding.)
                    867:                         * Conventionally, one or more manual names are
                    868:                         * comma-specified prior to a whitespace, then a
                    869:                         * dash, then a description.  Try to puzzle out
                    870:                         * the name parts here.
                    871:                         */
                    872:
                    873:                        for ( ;; ) {
                    874:                                sz = strcspn(start, " ,");
                    875:                                if ('\0' == start[(int)sz])
                    876:                                        break;
                    877:
1.3       kristaps  878:                                buf->len = 0;
                    879:                                buf_appendb(buf, start, sz);
                    880:                                buf_appendb(buf, "", 1);
1.1       kristaps  881:
1.3       kristaps  882:                                hash_put(hash, buf, TYPE_NAME);
1.1       kristaps  883:
                    884:                                if (' ' == start[(int)sz]) {
                    885:                                        start += (int)sz + 1;
                    886:                                        break;
                    887:                                }
                    888:
                    889:                                assert(',' == start[(int)sz]);
                    890:                                start += (int)sz + 1;
                    891:                                while (' ' == *start)
                    892:                                        start++;
                    893:                        }
                    894:
                    895:                        if (sv == start) {
1.3       kristaps  896:                                buf->len = 0;
                    897:                                buf_append(buf, start);
1.1       kristaps  898:                                return(1);
                    899:                        }
                    900:
                    901:                        while (' ' == *start)
                    902:                                start++;
                    903:
                    904:                        if (0 == strncmp(start, "-", 1))
                    905:                                start += 1;
                    906:                        else if (0 == strncmp(start, "\\-", 2))
                    907:                                start += 2;
                    908:                        else if (0 == strncmp(start, "\\(en", 4))
                    909:                                start += 4;
                    910:                        else if (0 == strncmp(start, "\\(em", 4))
                    911:                                start += 4;
                    912:
                    913:                        while (' ' == *start)
                    914:                                start++;
                    915:
1.6       kristaps  916:                        sz = strlen(start) + 1;
                    917:                        buf_appendb(dbuf, start, sz);
                    918:                        buf_appendb(buf, start, sz);
1.1       kristaps  919:                }
                    920:        }
                    921:
1.5       kristaps  922:        if (pman_node(hash, buf, dbuf, n->child))
1.1       kristaps  923:                return(1);
1.5       kristaps  924:        if (pman_node(hash, buf, dbuf, n->next))
1.1       kristaps  925:                return(1);
                    926:
                    927:        return(0);
                    928: }
                    929:
                    930: static void
                    931: usage(void)
                    932: {
                    933:
1.10    ! kristaps  934:        fprintf(stderr, "usage: %s [-v] [-d path] [file...]\n",
        !           935:                        progname);
1.1       kristaps  936: }

CVSweb