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

Annotation of mandoc/makewhatis.c, Revision 1.7

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

CVSweb