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

Annotation of mandoc/mandocdb.c, Revision 1.23

1.23    ! kristaps    1: /*     $Id: mandocdb.c,v 1.22 2011/12/03 12:09:07 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.12      schwarze    4:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
                     22: #include <sys/param.h>
1.14      schwarze   23: #include <sys/types.h>
                     24: #include <sys/stat.h>
1.1       kristaps   25:
                     26: #include <assert.h>
1.4       kristaps   27: #include <dirent.h>
1.1       kristaps   28: #include <fcntl.h>
                     29: #include <getopt.h>
                     30: #include <stdio.h>
                     31: #include <stdint.h>
                     32: #include <stdlib.h>
                     33: #include <string.h>
1.17      schwarze   34: #include <unistd.h>
1.1       kristaps   35:
1.21      kristaps   36: #if defined(__linux__)
                     37: # include <endian.h>
1.1       kristaps   38: # include <db_185.h>
1.21      kristaps   39: #elif defined(__APPLE__)
                     40: # include <libkern/OSByteOrder.h>
                     41: # include <db.h>
1.1       kristaps   42: #else
                     43: # include <db.h>
                     44: #endif
                     45:
                     46: #include "man.h"
                     47: #include "mdoc.h"
                     48: #include "mandoc.h"
1.8       schwarze   49: #include "mandocdb.h"
1.10      kristaps   50: #include "manpath.h"
1.1       kristaps   51:
                     52: #define        MANDOC_BUFSZ      BUFSIZ
                     53: #define        MANDOC_SLOP       1024
                     54:
1.14      schwarze   55: #define        MANDOC_SRC        0x1
                     56: #define        MANDOC_FORM       0x2
                     57:
1.5       kristaps   58: /* Tiny list for files.  No need to bring in QUEUE. */
                     59:
1.3       kristaps   60: struct of {
1.5       kristaps   61:        char             *fname; /* heap-allocated */
1.12      schwarze   62:        char             *sec;
                     63:        char             *arch;
                     64:        char             *title;
1.14      schwarze   65:        int               src_form;
1.5       kristaps   66:        struct of        *next; /* NULL for last one */
                     67:        struct of        *first; /* first in list */
1.3       kristaps   68: };
                     69:
1.1       kristaps   70: /* Buffer for storing growable data. */
                     71:
                     72: struct buf {
                     73:        char             *cp;
1.5       kristaps   74:        size_t            len; /* current length */
                     75:        size_t            size; /* total buffer size */
1.1       kristaps   76: };
                     77:
                     78: /* Operation we're going to perform. */
                     79:
                     80: enum   op {
                     81:        OP_NEW = 0, /* new database */
1.5       kristaps   82:        OP_UPDATE, /* delete/add entries in existing database */
1.1       kristaps   83:        OP_DELETE /* delete entries from existing database */
                     84: };
                     85:
                     86: #define        MAN_ARGS          DB *hash, \
                     87:                          struct buf *buf, \
                     88:                          struct buf *dbuf, \
                     89:                          const struct man_node *n
                     90: #define        MDOC_ARGS         DB *hash, \
                     91:                          struct buf *buf, \
                     92:                          struct buf *dbuf, \
                     93:                          const struct mdoc_node *n, \
                     94:                          const struct mdoc_meta *m
                     95:
                     96: static void              buf_appendmdoc(struct buf *,
                     97:                                const struct mdoc_node *, int);
                     98: static void              buf_append(struct buf *, const char *);
                     99: static void              buf_appendb(struct buf *,
                    100:                                const void *, size_t);
                    101: static void              dbt_put(DB *, const char *, DBT *, DBT *);
1.9       kristaps  102: static void              hash_put(DB *, const struct buf *, uint64_t);
1.1       kristaps  103: static void              hash_reset(DB **);
1.3       kristaps  104: static void              index_merge(const struct of *, struct mparse *,
1.16      schwarze  105:                                struct buf *, struct buf *, DB *,
                    106:                                DB *, const char *, DB *, const char *,
1.3       kristaps  107:                                recno_t, const recno_t *, size_t);
                    108: static void              index_prune(const struct of *, DB *,
                    109:                                const char *, DB *, const char *,
1.16      schwarze  110:                                recno_t *, recno_t **, size_t *);
                    111: static void              ofile_argbuild(int, char *[], struct of **);
1.12      schwarze  112: static int               ofile_dirbuild(const char *, const char *,
1.16      schwarze  113:                                const char *, int, struct of **);
1.4       kristaps  114: static void              ofile_free(struct of *);
1.14      schwarze  115: static void              pformatted(DB *, struct buf *, struct buf *,
                    116:                                const struct of *);
1.1       kristaps  117: static int               pman_node(MAN_ARGS);
                    118: static void              pmdoc_node(MDOC_ARGS);
                    119: static void              pmdoc_An(MDOC_ARGS);
                    120: static void              pmdoc_Cd(MDOC_ARGS);
                    121: static void              pmdoc_Er(MDOC_ARGS);
                    122: static void              pmdoc_Ev(MDOC_ARGS);
                    123: static void              pmdoc_Fd(MDOC_ARGS);
                    124: static void              pmdoc_In(MDOC_ARGS);
                    125: static void              pmdoc_Fn(MDOC_ARGS);
                    126: static void              pmdoc_Fo(MDOC_ARGS);
                    127: static void              pmdoc_Nd(MDOC_ARGS);
                    128: static void              pmdoc_Nm(MDOC_ARGS);
                    129: static void              pmdoc_Pa(MDOC_ARGS);
                    130: static void              pmdoc_St(MDOC_ARGS);
                    131: static void              pmdoc_Vt(MDOC_ARGS);
                    132: static void              pmdoc_Xr(MDOC_ARGS);
                    133: static void              usage(void);
                    134:
                    135: typedef        void            (*pmdoc_nf)(MDOC_ARGS);
                    136:
                    137: static const pmdoc_nf    mdocs[MDOC_MAX] = {
                    138:        NULL, /* Ap */
                    139:        NULL, /* Dd */
                    140:        NULL, /* Dt */
                    141:        NULL, /* Os */
                    142:        NULL, /* Sh */
                    143:        NULL, /* Ss */
                    144:        NULL, /* Pp */
                    145:        NULL, /* D1 */
                    146:        NULL, /* Dl */
                    147:        NULL, /* Bd */
                    148:        NULL, /* Ed */
                    149:        NULL, /* Bl */
                    150:        NULL, /* El */
                    151:        NULL, /* It */
                    152:        NULL, /* Ad */
                    153:        pmdoc_An, /* An */
                    154:        NULL, /* Ar */
                    155:        pmdoc_Cd, /* Cd */
                    156:        NULL, /* Cm */
                    157:        NULL, /* Dv */
                    158:        pmdoc_Er, /* Er */
                    159:        pmdoc_Ev, /* Ev */
                    160:        NULL, /* Ex */
                    161:        NULL, /* Fa */
                    162:        pmdoc_Fd, /* Fd */
                    163:        NULL, /* Fl */
                    164:        pmdoc_Fn, /* Fn */
                    165:        NULL, /* Ft */
                    166:        NULL, /* Ic */
                    167:        pmdoc_In, /* In */
                    168:        NULL, /* Li */
                    169:        pmdoc_Nd, /* Nd */
                    170:        pmdoc_Nm, /* Nm */
                    171:        NULL, /* Op */
                    172:        NULL, /* Ot */
                    173:        pmdoc_Pa, /* Pa */
                    174:        NULL, /* Rv */
                    175:        pmdoc_St, /* St */
                    176:        pmdoc_Vt, /* Va */
                    177:        pmdoc_Vt, /* Vt */
                    178:        pmdoc_Xr, /* Xr */
                    179:        NULL, /* %A */
                    180:        NULL, /* %B */
                    181:        NULL, /* %D */
                    182:        NULL, /* %I */
                    183:        NULL, /* %J */
                    184:        NULL, /* %N */
                    185:        NULL, /* %O */
                    186:        NULL, /* %P */
                    187:        NULL, /* %R */
                    188:        NULL, /* %T */
                    189:        NULL, /* %V */
                    190:        NULL, /* Ac */
                    191:        NULL, /* Ao */
                    192:        NULL, /* Aq */
                    193:        NULL, /* At */
                    194:        NULL, /* Bc */
                    195:        NULL, /* Bf */
                    196:        NULL, /* Bo */
                    197:        NULL, /* Bq */
                    198:        NULL, /* Bsx */
                    199:        NULL, /* Bx */
                    200:        NULL, /* Db */
                    201:        NULL, /* Dc */
                    202:        NULL, /* Do */
                    203:        NULL, /* Dq */
                    204:        NULL, /* Ec */
                    205:        NULL, /* Ef */
                    206:        NULL, /* Em */
                    207:        NULL, /* Eo */
                    208:        NULL, /* Fx */
                    209:        NULL, /* Ms */
                    210:        NULL, /* No */
                    211:        NULL, /* Ns */
                    212:        NULL, /* Nx */
                    213:        NULL, /* Ox */
                    214:        NULL, /* Pc */
                    215:        NULL, /* Pf */
                    216:        NULL, /* Po */
                    217:        NULL, /* Pq */
                    218:        NULL, /* Qc */
                    219:        NULL, /* Ql */
                    220:        NULL, /* Qo */
                    221:        NULL, /* Qq */
                    222:        NULL, /* Re */
                    223:        NULL, /* Rs */
                    224:        NULL, /* Sc */
                    225:        NULL, /* So */
                    226:        NULL, /* Sq */
                    227:        NULL, /* Sm */
                    228:        NULL, /* Sx */
                    229:        NULL, /* Sy */
                    230:        NULL, /* Tn */
                    231:        NULL, /* Ux */
                    232:        NULL, /* Xc */
                    233:        NULL, /* Xo */
                    234:        pmdoc_Fo, /* Fo */
                    235:        NULL, /* Fc */
                    236:        NULL, /* Oo */
                    237:        NULL, /* Oc */
                    238:        NULL, /* Bk */
                    239:        NULL, /* Ek */
                    240:        NULL, /* Bt */
                    241:        NULL, /* Hf */
                    242:        NULL, /* Fr */
                    243:        NULL, /* Ud */
                    244:        NULL, /* Lb */
                    245:        NULL, /* Lp */
                    246:        NULL, /* Lk */
                    247:        NULL, /* Mt */
                    248:        NULL, /* Brq */
                    249:        NULL, /* Bro */
                    250:        NULL, /* Brc */
                    251:        NULL, /* %C */
                    252:        NULL, /* Es */
                    253:        NULL, /* En */
                    254:        NULL, /* Dx */
                    255:        NULL, /* %Q */
                    256:        NULL, /* br */
                    257:        NULL, /* sp */
                    258:        NULL, /* %U */
                    259:        NULL, /* Ta */
                    260: };
                    261:
                    262: static const char       *progname;
1.16      schwarze  263: static int               use_all;  /* Use all directories and files. */
                    264: static int               verb;  /* Output verbosity level. */
1.1       kristaps  265:
                    266: int
                    267: main(int argc, char *argv[])
                    268: {
                    269:        struct mparse   *mp; /* parse sequence */
1.10      kristaps  270:        struct manpaths  dirs;
1.1       kristaps  271:        enum op          op; /* current operation */
1.5       kristaps  272:        const char      *dir;
1.1       kristaps  273:        char             ibuf[MAXPATHLEN], /* index fname */
1.3       kristaps  274:                         fbuf[MAXPATHLEN];  /* btree fname */
1.16      schwarze  275:        int              ch, i, flags;
1.1       kristaps  276:        DB              *idx, /* index database */
                    277:                        *db, /* keyword database */
                    278:                        *hash; /* temporary keyword hashtable */
                    279:        BTREEINFO        info; /* btree configuration */
1.12      schwarze  280:        recno_t          maxrec; /* last record number in the index */
                    281:        recno_t         *recs; /* the numbers of all empty records */
1.5       kristaps  282:        size_t           sz1, sz2,
1.12      schwarze  283:                         recsz, /* number of allocated slots in recs */
                    284:                         reccur; /* current number of empty records */
1.1       kristaps  285:        struct buf       buf, /* keyword buffer */
                    286:                         dbuf; /* description buffer */
1.5       kristaps  287:        struct of       *of; /* list of files for processing */
1.1       kristaps  288:        extern int       optind;
                    289:        extern char     *optarg;
                    290:
                    291:        progname = strrchr(argv[0], '/');
                    292:        if (progname == NULL)
                    293:                progname = argv[0];
                    294:        else
                    295:                ++progname;
                    296:
1.10      kristaps  297:        memset(&dirs, 0, sizeof(struct manpaths));
                    298:
1.5       kristaps  299:        verb = 0;
1.12      schwarze  300:        use_all = 0;
1.4       kristaps  301:        of = NULL;
1.1       kristaps  302:        db = idx = NULL;
                    303:        mp = NULL;
                    304:        hash = NULL;
                    305:        recs = NULL;
                    306:        recsz = reccur = 0;
                    307:        maxrec = 0;
                    308:        op = OP_NEW;
1.5       kristaps  309:        dir = NULL;
1.1       kristaps  310:
1.12      schwarze  311:        while (-1 != (ch = getopt(argc, argv, "ad:u:v")))
1.1       kristaps  312:                switch (ch) {
1.12      schwarze  313:                case ('a'):
                    314:                        use_all = 1;
                    315:                        break;
1.5       kristaps  316:                case ('d'):
                    317:                        dir = optarg;
                    318:                        op = OP_UPDATE;
                    319:                        break;
                    320:                case ('u'):
                    321:                        dir = optarg;
                    322:                        op = OP_DELETE;
                    323:                        break;
                    324:                case ('v'):
                    325:                        verb++;
                    326:                        break;
1.1       kristaps  327:                default:
                    328:                        usage();
                    329:                        return((int)MANDOCLEVEL_BADARG);
                    330:                }
                    331:
                    332:        argc -= optind;
                    333:        argv += optind;
                    334:
1.4       kristaps  335:        memset(&info, 0, sizeof(BTREEINFO));
                    336:        info.flags = R_DUP;
1.1       kristaps  337:
1.4       kristaps  338:        mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
1.1       kristaps  339:
1.5       kristaps  340:        memset(&buf, 0, sizeof(struct buf));
                    341:        memset(&dbuf, 0, sizeof(struct buf));
1.1       kristaps  342:
1.4       kristaps  343:        buf.size = dbuf.size = MANDOC_BUFSZ;
1.1       kristaps  344:
1.4       kristaps  345:        buf.cp = mandoc_malloc(buf.size);
                    346:        dbuf.cp = mandoc_malloc(dbuf.size);
1.1       kristaps  347:
1.5       kristaps  348:        flags = OP_NEW == op ? O_CREAT|O_TRUNC|O_RDWR : O_CREAT|O_RDWR;
                    349:
                    350:        if (OP_UPDATE == op || OP_DELETE == op) {
                    351:                ibuf[0] = fbuf[0] = '\0';
                    352:
                    353:                strlcat(fbuf, dir, MAXPATHLEN);
                    354:                strlcat(fbuf, "/", MAXPATHLEN);
                    355:                sz1 = strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
                    356:
                    357:                strlcat(ibuf, dir, MAXPATHLEN);
                    358:                strlcat(ibuf, "/", MAXPATHLEN);
                    359:                sz2 = strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
                    360:
                    361:                if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
                    362:                        fprintf(stderr, "%s: Path too long\n", dir);
                    363:                        exit((int)MANDOCLEVEL_BADARG);
                    364:                }
                    365:
                    366:                db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
                    367:                idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
                    368:
                    369:                if (NULL == db) {
                    370:                        perror(fbuf);
                    371:                        exit((int)MANDOCLEVEL_SYSERR);
1.12      schwarze  372:                } else if (NULL == idx) {
1.5       kristaps  373:                        perror(ibuf);
                    374:                        exit((int)MANDOCLEVEL_SYSERR);
                    375:                }
                    376:
                    377:                if (verb > 2) {
                    378:                        printf("%s: Opened\n", fbuf);
                    379:                        printf("%s: Opened\n", ibuf);
                    380:                }
                    381:
1.16      schwarze  382:                ofile_argbuild(argc, argv, &of);
1.5       kristaps  383:                if (NULL == of)
                    384:                        goto out;
                    385:
                    386:                of = of->first;
                    387:
1.16      schwarze  388:                index_prune(of, db, fbuf, idx, ibuf,
1.5       kristaps  389:                                &maxrec, &recs, &recsz);
                    390:
1.17      schwarze  391:                /*
                    392:                 * Go to the root of the respective manual tree
                    393:                 * such that .so links work.  In case of failure,
                    394:                 * just prod on, even though .so links won't work.
                    395:                 */
                    396:
                    397:                if (OP_UPDATE == op) {
                    398:                        chdir(dir);
1.13      schwarze  399:                        index_merge(of, mp, &dbuf, &buf, hash,
1.16      schwarze  400:                                        db, fbuf, idx, ibuf,
                    401:                                        maxrec, recs, reccur);
1.17      schwarze  402:                }
1.5       kristaps  403:
                    404:                goto out;
                    405:        }
                    406:
1.10      kristaps  407:        /*
                    408:         * Configure the directories we're going to scan.
                    409:         * If we have command-line arguments, use them.
                    410:         * If not, we use man(1)'s method (see mandocdb.8).
                    411:         */
                    412:
                    413:        if (argc > 0) {
                    414:                dirs.paths = mandoc_malloc(argc * sizeof(char *));
                    415:                dirs.sz = argc;
                    416:                for (i = 0; i < argc; i++)
                    417:                        dirs.paths[i] = mandoc_strdup(argv[i]);
                    418:        } else
1.11      kristaps  419:                manpath_parse(&dirs, NULL, NULL);
1.10      kristaps  420:
                    421:        for (i = 0; i < dirs.sz; i++) {
1.5       kristaps  422:                ibuf[0] = fbuf[0] = '\0';
1.1       kristaps  423:
1.10      kristaps  424:                strlcat(fbuf, dirs.paths[i], MAXPATHLEN);
1.5       kristaps  425:                strlcat(fbuf, "/", MAXPATHLEN);
                    426:                sz1 = strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
1.1       kristaps  427:
1.10      kristaps  428:                strlcat(ibuf, dirs.paths[i], MAXPATHLEN);
1.5       kristaps  429:                strlcat(ibuf, "/", MAXPATHLEN);
                    430:                sz2 = strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
1.1       kristaps  431:
1.5       kristaps  432:                if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
1.13      schwarze  433:                        fprintf(stderr, "%s: Path too long\n",
1.10      kristaps  434:                                        dirs.paths[i]);
1.5       kristaps  435:                        exit((int)MANDOCLEVEL_BADARG);
1.4       kristaps  436:                }
1.3       kristaps  437:
1.13      schwarze  438:                if (db)
                    439:                        (*db->close)(db);
                    440:                if (idx)
                    441:                        (*idx->close)(idx);
                    442:
1.4       kristaps  443:                db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
                    444:                idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
1.3       kristaps  445:
1.4       kristaps  446:                if (NULL == db) {
                    447:                        perror(fbuf);
1.5       kristaps  448:                        exit((int)MANDOCLEVEL_SYSERR);
1.12      schwarze  449:                } else if (NULL == idx) {
1.4       kristaps  450:                        perror(ibuf);
1.5       kristaps  451:                        exit((int)MANDOCLEVEL_SYSERR);
                    452:                }
                    453:
                    454:                if (verb > 2) {
                    455:                        printf("%s: Truncated\n", fbuf);
                    456:                        printf("%s: Truncated\n", ibuf);
1.4       kristaps  457:                }
1.1       kristaps  458:
1.4       kristaps  459:                ofile_free(of);
                    460:                of = NULL;
1.1       kristaps  461:
1.12      schwarze  462:                if ( ! ofile_dirbuild(dirs.paths[i], NULL, NULL,
1.16      schwarze  463:                                        0, &of))
1.5       kristaps  464:                        exit((int)MANDOCLEVEL_SYSERR);
1.1       kristaps  465:
1.5       kristaps  466:                if (NULL == of)
                    467:                        continue;
1.1       kristaps  468:
1.5       kristaps  469:                of = of->first;
1.1       kristaps  470:
1.17      schwarze  471:                /*
                    472:                 * Go to the root of the respective manual tree
                    473:                 * such that .so links work.  In case of failure,
                    474:                 * just prod on, even though .so links won't work.
                    475:                 */
                    476:
                    477:                chdir(dirs.paths[i]);
1.13      schwarze  478:                index_merge(of, mp, &dbuf, &buf, hash, db, fbuf,
1.16      schwarze  479:                                idx, ibuf, maxrec, recs, reccur);
1.4       kristaps  480:        }
1.3       kristaps  481:
1.5       kristaps  482: out:
1.3       kristaps  483:        if (db)
                    484:                (*db->close)(db);
                    485:        if (idx)
                    486:                (*idx->close)(idx);
                    487:        if (hash)
                    488:                (*hash->close)(hash);
                    489:        if (mp)
                    490:                mparse_free(mp);
                    491:
1.10      kristaps  492:        manpath_free(&dirs);
1.4       kristaps  493:        ofile_free(of);
1.3       kristaps  494:        free(buf.cp);
                    495:        free(dbuf.cp);
                    496:        free(recs);
                    497:
1.5       kristaps  498:        return(MANDOCLEVEL_OK);
1.3       kristaps  499: }
                    500:
                    501: void
                    502: index_merge(const struct of *of, struct mparse *mp,
1.16      schwarze  503:                struct buf *dbuf, struct buf *buf, DB *hash,
                    504:                DB *db, const char *dbf, DB *idx, const char *idxf,
1.3       kristaps  505:                recno_t maxrec, const recno_t *recs, size_t reccur)
                    506: {
                    507:        recno_t          rec;
                    508:        int              ch;
                    509:        DBT              key, val;
                    510:        struct mdoc     *mdoc;
                    511:        struct man      *man;
                    512:        const char      *fn, *msec, *mtitle, *arch;
                    513:        size_t           sv;
                    514:        unsigned         seq;
1.9       kristaps  515:        struct db_val    vbuf;
1.3       kristaps  516:
                    517:        for (rec = 0; of; of = of->next) {
                    518:                fn = of->fname;
1.14      schwarze  519:
                    520:                /*
                    521:                 * Reclaim an empty index record, if available.
                    522:                 */
                    523:
1.3       kristaps  524:                if (reccur > 0) {
                    525:                        --reccur;
                    526:                        rec = recs[(int)reccur];
                    527:                } else if (maxrec > 0) {
                    528:                        rec = maxrec;
                    529:                        maxrec = 0;
1.1       kristaps  530:                } else
                    531:                        rec++;
                    532:
                    533:                mparse_reset(mp);
                    534:                hash_reset(&hash);
1.14      schwarze  535:                mdoc = NULL;
                    536:                man = NULL;
1.1       kristaps  537:
1.14      schwarze  538:                /*
                    539:                 * Try interpreting the file as mdoc(7) or man(7)
                    540:                 * source code, unless it is already known to be
                    541:                 * formatted.  Fall back to formatted mode.
                    542:                 */
                    543:
                    544:                if ((MANDOC_SRC & of->src_form ||
                    545:                    ! (MANDOC_FORM & of->src_form)) &&
                    546:                    MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn))
                    547:                        mparse_result(mp, &mdoc, &man);
                    548:
                    549:                if (NULL != mdoc) {
                    550:                        msec = mdoc_meta(mdoc)->msec;
                    551:                        arch = mdoc_meta(mdoc)->arch;
                    552:                        mtitle = mdoc_meta(mdoc)->title;
                    553:                } else if (NULL != man) {
                    554:                        msec = man_meta(man)->msec;
                    555:                        arch = NULL;
                    556:                        mtitle = man_meta(man)->title;
                    557:                } else {
                    558:                        msec = of->sec;
                    559:                        arch = of->arch;
                    560:                        mtitle = of->title;
1.1       kristaps  561:                }
                    562:
1.12      schwarze  563:                /*
                    564:                 * By default, skip a file if the manual section
                    565:                 * and architecture given in the file disagree
                    566:                 * with the directory where the file is located.
                    567:                 */
                    568:
                    569:                if (0 == use_all) {
                    570:                        assert(of->sec);
                    571:                        assert(msec);
                    572:                        if (strcmp(msec, of->sec))
                    573:                                continue;
                    574:
                    575:                        if (NULL == arch) {
                    576:                                if (NULL != of->arch)
                    577:                                        continue;
                    578:                        } else if (NULL == of->arch ||
                    579:                                        strcmp(arch, of->arch))
                    580:                                continue;
                    581:                }
                    582:
1.1       kristaps  583:                if (NULL == arch)
                    584:                        arch = "";
                    585:
                    586:                /*
1.12      schwarze  587:                 * By default, skip a file if the title given
                    588:                 * in the file disagrees with the file name.
                    589:                 * If both agree, use the file name as the title,
                    590:                 * because the one in the file usually is all caps.
                    591:                 */
                    592:
                    593:                assert(of->title);
                    594:                assert(mtitle);
                    595:
                    596:                if (0 == strcasecmp(mtitle, of->title))
                    597:                        mtitle = of->title;
                    598:                else if (0 == use_all)
                    599:                        continue;
                    600:
                    601:                /*
1.1       kristaps  602:                 * The index record value consists of a nil-terminated
                    603:                 * filename, a nil-terminated manual section, and a
                    604:                 * nil-terminated description.  Since the description
                    605:                 * may not be set, we set a sentinel to see if we're
                    606:                 * going to write a nil byte in its place.
                    607:                 */
                    608:
1.3       kristaps  609:                dbuf->len = 0;
1.15      schwarze  610:                buf_append(dbuf, mdoc ? "mdoc" : (man ? "man" : "cat"));
1.3       kristaps  611:                buf_appendb(dbuf, fn, strlen(fn) + 1);
                    612:                buf_appendb(dbuf, msec, strlen(msec) + 1);
                    613:                buf_appendb(dbuf, mtitle, strlen(mtitle) + 1);
                    614:                buf_appendb(dbuf, arch, strlen(arch) + 1);
1.1       kristaps  615:
1.3       kristaps  616:                sv = dbuf->len;
1.1       kristaps  617:
                    618:                /* Fix the record number in the btree value. */
                    619:
                    620:                if (mdoc)
1.3       kristaps  621:                        pmdoc_node(hash, buf, dbuf,
1.1       kristaps  622:                                mdoc_node(mdoc), mdoc_meta(mdoc));
1.14      schwarze  623:                else if (man)
1.3       kristaps  624:                        pman_node(hash, buf, dbuf, man_node(man));
1.14      schwarze  625:                else
                    626:                        pformatted(hash, buf, dbuf, of);
1.1       kristaps  627:
                    628:                /*
                    629:                 * Copy from the in-memory hashtable of pending keywords
                    630:                 * into the database.
                    631:                 */
                    632:
1.20      kristaps  633:                vbuf.rec = htobe32(rec);
1.1       kristaps  634:                seq = R_FIRST;
                    635:                while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
                    636:                        seq = R_NEXT;
1.20      kristaps  637:                        vbuf.mask = htobe64(*(uint64_t *)val.data);
1.9       kristaps  638:                        val.size = sizeof(struct db_val);
                    639:                        val.data = &vbuf;
1.3       kristaps  640:                        dbt_put(db, dbf, &key, &val);
1.1       kristaps  641:                }
                    642:                if (ch < 0) {
                    643:                        perror("hash");
                    644:                        exit((int)MANDOCLEVEL_SYSERR);
                    645:                }
                    646:
                    647:                /*
                    648:                 * Apply to the index.  If we haven't had a description
                    649:                 * set, put an empty one in now.
                    650:                 */
                    651:
1.3       kristaps  652:                if (dbuf->len == sv)
                    653:                        buf_appendb(dbuf, "", 1);
1.1       kristaps  654:
                    655:                key.data = &rec;
                    656:                key.size = sizeof(recno_t);
                    657:
1.3       kristaps  658:                val.data = dbuf->cp;
                    659:                val.size = dbuf->len;
1.1       kristaps  660:
1.5       kristaps  661:                if (verb)
                    662:                        printf("%s: Added index\n", fn);
1.18      kristaps  663:
1.3       kristaps  664:                dbt_put(idx, idxf, &key, &val);
                    665:        }
                    666: }
                    667:
                    668: /*
                    669:  * Scan through all entries in the index file `idx' and prune those
                    670:  * entries in `ofile'.
                    671:  * Pruning consists of removing from `db', then invalidating the entry
                    672:  * in `idx' (zeroing its value size).
                    673:  */
                    674: static void
                    675: index_prune(const struct of *ofile, DB *db, const char *dbf,
1.16      schwarze  676:                DB *idx, const char *idxf,
1.3       kristaps  677:                recno_t *maxrec, recno_t **recs, size_t *recsz)
                    678: {
                    679:        const struct of *of;
1.18      kristaps  680:        const char      *fn, *cp;
1.9       kristaps  681:        struct db_val   *vbuf;
1.3       kristaps  682:        unsigned         seq, sseq;
                    683:        DBT              key, val;
                    684:        size_t           reccur;
                    685:        int              ch;
                    686:
                    687:        reccur = 0;
                    688:        seq = R_FIRST;
                    689:        while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
                    690:                seq = R_NEXT;
                    691:                *maxrec = *(recno_t *)key.data;
1.18      kristaps  692:                cp = val.data;
                    693:
                    694:                /* Deleted records are zero-sized.  Skip them. */
                    695:
                    696:                if (0 == val.size)
                    697:                        goto cont;
                    698:
                    699:                /*
                    700:                 * Make sure we're sane.
                    701:                 * Read past our mdoc/man/cat type to the next string,
                    702:                 * then make sure it's bounded by a NUL.
                    703:                 * Failing any of these, we go into our error handler.
                    704:                 */
                    705:
                    706:                if (NULL == (fn = memchr(cp, '\0', val.size)))
                    707:                        break;
                    708:                if (++fn - cp >= (int)val.size)
                    709:                        break;
                    710:                if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
                    711:                        break;
                    712:
                    713:                /*
                    714:                 * Search for the file in those we care about.
                    715:                 * XXX: build this into a tree.  Too slow.
                    716:                 */
1.3       kristaps  717:
                    718:                for (of = ofile; of; of = of->next)
                    719:                        if (0 == strcmp(fn, of->fname))
                    720:                                break;
                    721:
                    722:                if (NULL == of)
                    723:                        continue;
                    724:
1.18      kristaps  725:                /*
                    726:                 * Search through the keyword database, throwing out all
                    727:                 * references to our file.
                    728:                 */
                    729:
1.3       kristaps  730:                sseq = R_FIRST;
                    731:                while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) {
                    732:                        sseq = R_NEXT;
1.18      kristaps  733:                        if (sizeof(struct db_val) != val.size)
                    734:                                break;
                    735:
1.9       kristaps  736:                        vbuf = val.data;
1.20      kristaps  737:                        if (*maxrec != betoh32(vbuf->rec))
1.3       kristaps  738:                                continue;
1.18      kristaps  739:
                    740:                        if ((ch = (*db->del)(db, &key, R_CURSOR)) < 0)
1.3       kristaps  741:                                break;
                    742:                }
1.18      kristaps  743:
1.3       kristaps  744:                if (ch < 0) {
                    745:                        perror(dbf);
                    746:                        exit((int)MANDOCLEVEL_SYSERR);
1.18      kristaps  747:                } else if (1 != ch) {
                    748:                        fprintf(stderr, "%s: Corrupt database\n", dbf);
                    749:                        exit((int)MANDOCLEVEL_SYSERR);
1.3       kristaps  750:                }
1.1       kristaps  751:
1.5       kristaps  752:                if (verb)
                    753:                        printf("%s: Deleted index\n", fn);
1.1       kristaps  754:
1.3       kristaps  755:                val.size = 0;
                    756:                ch = (*idx->put)(idx, &key, &val, R_CURSOR);
1.1       kristaps  757:
1.18      kristaps  758:                if (ch < 0)
                    759:                        break;
                    760: cont:
1.3       kristaps  761:                if (reccur >= *recsz) {
                    762:                        *recsz += MANDOC_SLOP;
                    763:                        *recs = mandoc_realloc
                    764:                                (*recs, *recsz * sizeof(recno_t));
                    765:                }
1.1       kristaps  766:
1.3       kristaps  767:                (*recs)[(int)reccur] = *maxrec;
                    768:                reccur++;
                    769:        }
1.18      kristaps  770:
                    771:        if (ch < 0) {
                    772:                perror(idxf);
                    773:                exit((int)MANDOCLEVEL_SYSERR);
                    774:        } else if (1 != ch) {
                    775:                fprintf(stderr, "%s: Corrupt index\n", idxf);
                    776:                exit((int)MANDOCLEVEL_SYSERR);
                    777:        }
                    778:
1.3       kristaps  779:        (*maxrec)++;
1.1       kristaps  780: }
                    781:
                    782: /*
                    783:  * Grow the buffer (if necessary) and copy in a binary string.
                    784:  */
                    785: static void
                    786: buf_appendb(struct buf *buf, const void *cp, size_t sz)
                    787: {
                    788:
                    789:        /* Overshoot by MANDOC_BUFSZ. */
                    790:
                    791:        while (buf->len + sz >= buf->size) {
                    792:                buf->size = buf->len + sz + MANDOC_BUFSZ;
                    793:                buf->cp = mandoc_realloc(buf->cp, buf->size);
                    794:        }
                    795:
                    796:        memcpy(buf->cp + (int)buf->len, cp, sz);
                    797:        buf->len += sz;
                    798: }
                    799:
                    800: /*
                    801:  * Append a nil-terminated string to the buffer.
                    802:  * This can be invoked multiple times.
                    803:  * The buffer string will be nil-terminated.
                    804:  * If invoked multiple times, a space is put between strings.
                    805:  */
                    806: static void
                    807: buf_append(struct buf *buf, const char *cp)
                    808: {
                    809:        size_t           sz;
                    810:
                    811:        if (0 == (sz = strlen(cp)))
                    812:                return;
                    813:
                    814:        if (buf->len)
                    815:                buf->cp[(int)buf->len - 1] = ' ';
                    816:
                    817:        buf_appendb(buf, cp, sz + 1);
                    818: }
                    819:
                    820: /*
                    821:  * Recursively add all text from a given node.
                    822:  * This is optimised for general mdoc nodes in this context, which do
                    823:  * not consist of subexpressions and having a recursive call for n->next
                    824:  * would be wasteful.
                    825:  * The "f" variable should be 0 unless called from pmdoc_Nd for the
                    826:  * description buffer, which does not start at the beginning of the
                    827:  * buffer.
                    828:  */
                    829: static void
                    830: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
                    831: {
                    832:
                    833:        for ( ; n; n = n->next) {
                    834:                if (n->child)
                    835:                        buf_appendmdoc(buf, n->child, f);
                    836:
                    837:                if (MDOC_TEXT == n->type && f) {
                    838:                        f = 0;
                    839:                        buf_appendb(buf, n->string,
                    840:                                        strlen(n->string) + 1);
                    841:                } else if (MDOC_TEXT == n->type)
                    842:                        buf_append(buf, n->string);
                    843:
                    844:        }
                    845: }
                    846:
                    847: /* ARGSUSED */
                    848: static void
                    849: pmdoc_An(MDOC_ARGS)
                    850: {
                    851:
                    852:        if (SEC_AUTHORS != n->sec)
                    853:                return;
                    854:
                    855:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze  856:        hash_put(hash, buf, TYPE_An);
1.1       kristaps  857: }
                    858:
                    859: static void
                    860: hash_reset(DB **db)
                    861: {
                    862:        DB              *hash;
                    863:
                    864:        if (NULL != (hash = *db))
                    865:                (*hash->close)(hash);
                    866:
1.5       kristaps  867:        *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1.1       kristaps  868:        if (NULL == *db) {
                    869:                perror("hash");
                    870:                exit((int)MANDOCLEVEL_SYSERR);
                    871:        }
                    872: }
                    873:
                    874: /* ARGSUSED */
                    875: static void
                    876: pmdoc_Fd(MDOC_ARGS)
                    877: {
                    878:        const char      *start, *end;
                    879:        size_t           sz;
                    880:
                    881:        if (SEC_SYNOPSIS != n->sec)
                    882:                return;
                    883:        if (NULL == (n = n->child) || MDOC_TEXT != n->type)
                    884:                return;
                    885:
                    886:        /*
                    887:         * Only consider those `Fd' macro fields that begin with an
                    888:         * "inclusion" token (versus, e.g., #define).
                    889:         */
                    890:        if (strcmp("#include", n->string))
                    891:                return;
                    892:
                    893:        if (NULL == (n = n->next) || MDOC_TEXT != n->type)
                    894:                return;
                    895:
                    896:        /*
                    897:         * Strip away the enclosing angle brackets and make sure we're
                    898:         * not zero-length.
                    899:         */
                    900:
                    901:        start = n->string;
                    902:        if ('<' == *start || '"' == *start)
                    903:                start++;
                    904:
                    905:        if (0 == (sz = strlen(start)))
                    906:                return;
                    907:
                    908:        end = &start[(int)sz - 1];
                    909:        if ('>' == *end || '"' == *end)
                    910:                end--;
                    911:
                    912:        assert(end >= start);
                    913:
                    914:        buf_appendb(buf, start, (size_t)(end - start + 1));
                    915:        buf_appendb(buf, "", 1);
                    916:
1.8       schwarze  917:        hash_put(hash, buf, TYPE_In);
1.1       kristaps  918: }
                    919:
                    920: /* ARGSUSED */
                    921: static void
                    922: pmdoc_Cd(MDOC_ARGS)
                    923: {
                    924:
                    925:        if (SEC_SYNOPSIS != n->sec)
                    926:                return;
                    927:
                    928:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze  929:        hash_put(hash, buf, TYPE_Cd);
1.1       kristaps  930: }
                    931:
                    932: /* ARGSUSED */
                    933: static void
                    934: pmdoc_In(MDOC_ARGS)
                    935: {
                    936:
                    937:        if (SEC_SYNOPSIS != n->sec)
                    938:                return;
                    939:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    940:                return;
                    941:
                    942:        buf_append(buf, n->child->string);
1.8       schwarze  943:        hash_put(hash, buf, TYPE_In);
1.1       kristaps  944: }
                    945:
                    946: /* ARGSUSED */
                    947: static void
                    948: pmdoc_Fn(MDOC_ARGS)
                    949: {
                    950:        const char      *cp;
                    951:
                    952:        if (SEC_SYNOPSIS != n->sec)
                    953:                return;
                    954:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    955:                return;
                    956:
                    957:        /* .Fn "struct type *arg" "foo" */
                    958:
                    959:        cp = strrchr(n->child->string, ' ');
                    960:        if (NULL == cp)
                    961:                cp = n->child->string;
                    962:
                    963:        /* Strip away pointer symbol. */
                    964:
                    965:        while ('*' == *cp)
                    966:                cp++;
                    967:
                    968:        buf_append(buf, cp);
1.8       schwarze  969:        hash_put(hash, buf, TYPE_Fn);
1.1       kristaps  970: }
                    971:
                    972: /* ARGSUSED */
                    973: static void
                    974: pmdoc_St(MDOC_ARGS)
                    975: {
                    976:
                    977:        if (SEC_STANDARDS != n->sec)
                    978:                return;
                    979:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    980:                return;
                    981:
                    982:        buf_append(buf, n->child->string);
1.8       schwarze  983:        hash_put(hash, buf, TYPE_St);
1.1       kristaps  984: }
                    985:
                    986: /* ARGSUSED */
                    987: static void
                    988: pmdoc_Xr(MDOC_ARGS)
                    989: {
                    990:
                    991:        if (NULL == (n = n->child))
                    992:                return;
                    993:
                    994:        buf_appendb(buf, n->string, strlen(n->string));
                    995:
                    996:        if (NULL != (n = n->next)) {
                    997:                buf_appendb(buf, ".", 1);
                    998:                buf_appendb(buf, n->string, strlen(n->string) + 1);
                    999:        } else
                   1000:                buf_appendb(buf, ".", 2);
                   1001:
1.8       schwarze 1002:        hash_put(hash, buf, TYPE_Xr);
1.1       kristaps 1003: }
                   1004:
                   1005: /* ARGSUSED */
                   1006: static void
                   1007: pmdoc_Vt(MDOC_ARGS)
                   1008: {
                   1009:        const char      *start;
                   1010:        size_t           sz;
                   1011:
                   1012:        if (SEC_SYNOPSIS != n->sec)
                   1013:                return;
                   1014:        if (MDOC_Vt == n->tok && MDOC_BODY != n->type)
                   1015:                return;
                   1016:        if (NULL == n->last || MDOC_TEXT != n->last->type)
                   1017:                return;
                   1018:
                   1019:        /*
                   1020:         * Strip away leading pointer symbol '*' and trailing ';'.
                   1021:         */
                   1022:
                   1023:        start = n->last->string;
                   1024:
                   1025:        while ('*' == *start)
                   1026:                start++;
                   1027:
                   1028:        if (0 == (sz = strlen(start)))
                   1029:                return;
                   1030:
                   1031:        if (';' == start[(int)sz - 1])
                   1032:                sz--;
                   1033:
                   1034:        if (0 == sz)
                   1035:                return;
                   1036:
                   1037:        buf_appendb(buf, start, sz);
                   1038:        buf_appendb(buf, "", 1);
1.8       schwarze 1039:        hash_put(hash, buf, TYPE_Va);
1.1       kristaps 1040: }
                   1041:
                   1042: /* ARGSUSED */
                   1043: static void
                   1044: pmdoc_Fo(MDOC_ARGS)
                   1045: {
                   1046:
                   1047:        if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1048:                return;
                   1049:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                   1050:                return;
                   1051:
                   1052:        buf_append(buf, n->child->string);
1.8       schwarze 1053:        hash_put(hash, buf, TYPE_Fn);
1.1       kristaps 1054: }
                   1055:
                   1056:
                   1057: /* ARGSUSED */
                   1058: static void
                   1059: pmdoc_Nd(MDOC_ARGS)
                   1060: {
                   1061:
                   1062:        if (MDOC_BODY != n->type)
                   1063:                return;
                   1064:
                   1065:        buf_appendmdoc(dbuf, n->child, 1);
                   1066:        buf_appendmdoc(buf, n->child, 0);
                   1067:
1.8       schwarze 1068:        hash_put(hash, buf, TYPE_Nd);
1.1       kristaps 1069: }
                   1070:
                   1071: /* ARGSUSED */
                   1072: static void
                   1073: pmdoc_Er(MDOC_ARGS)
                   1074: {
                   1075:
                   1076:        if (SEC_ERRORS != n->sec)
                   1077:                return;
                   1078:
                   1079:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1080:        hash_put(hash, buf, TYPE_Er);
1.1       kristaps 1081: }
                   1082:
                   1083: /* ARGSUSED */
                   1084: static void
                   1085: pmdoc_Ev(MDOC_ARGS)
                   1086: {
                   1087:
                   1088:        if (SEC_ENVIRONMENT != n->sec)
                   1089:                return;
                   1090:
                   1091:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1092:        hash_put(hash, buf, TYPE_Ev);
1.1       kristaps 1093: }
                   1094:
                   1095: /* ARGSUSED */
                   1096: static void
                   1097: pmdoc_Pa(MDOC_ARGS)
                   1098: {
                   1099:
                   1100:        if (SEC_FILES != n->sec)
                   1101:                return;
                   1102:
                   1103:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1104:        hash_put(hash, buf, TYPE_Pa);
1.1       kristaps 1105: }
                   1106:
                   1107: /* ARGSUSED */
                   1108: static void
                   1109: pmdoc_Nm(MDOC_ARGS)
                   1110: {
                   1111:
                   1112:        if (SEC_NAME == n->sec) {
                   1113:                buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1114:                hash_put(hash, buf, TYPE_Nm);
1.1       kristaps 1115:                return;
                   1116:        } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1117:                return;
                   1118:
                   1119:        if (NULL == n->child)
                   1120:                buf_append(buf, m->name);
                   1121:
                   1122:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1123:        hash_put(hash, buf, TYPE_Nm);
1.1       kristaps 1124: }
                   1125:
                   1126: static void
1.9       kristaps 1127: hash_put(DB *db, const struct buf *buf, uint64_t mask)
1.1       kristaps 1128: {
                   1129:        DBT              key, val;
                   1130:        int              rc;
                   1131:
                   1132:        if (buf->len < 2)
                   1133:                return;
                   1134:
                   1135:        key.data = buf->cp;
                   1136:        key.size = buf->len;
                   1137:
                   1138:        if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
                   1139:                perror("hash");
                   1140:                exit((int)MANDOCLEVEL_SYSERR);
                   1141:        } else if (0 == rc)
1.9       kristaps 1142:                mask |= *(uint64_t *)val.data;
1.1       kristaps 1143:
                   1144:        val.data = &mask;
1.9       kristaps 1145:        val.size = sizeof(uint64_t);
1.1       kristaps 1146:
                   1147:        if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
                   1148:                perror("hash");
                   1149:                exit((int)MANDOCLEVEL_SYSERR);
                   1150:        }
                   1151: }
                   1152:
                   1153: static void
                   1154: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
                   1155: {
                   1156:
                   1157:        assert(key->size);
                   1158:        assert(val->size);
                   1159:
                   1160:        if (0 == (*db->put)(db, key, val, 0))
                   1161:                return;
                   1162:
                   1163:        perror(dbn);
                   1164:        exit((int)MANDOCLEVEL_SYSERR);
                   1165:        /* NOTREACHED */
                   1166: }
                   1167:
                   1168: /*
                   1169:  * Call out to per-macro handlers after clearing the persistent database
                   1170:  * key.  If the macro sets the database key, flush it to the database.
                   1171:  */
                   1172: static void
                   1173: pmdoc_node(MDOC_ARGS)
                   1174: {
                   1175:
                   1176:        if (NULL == n)
                   1177:                return;
                   1178:
                   1179:        switch (n->type) {
                   1180:        case (MDOC_HEAD):
                   1181:                /* FALLTHROUGH */
                   1182:        case (MDOC_BODY):
                   1183:                /* FALLTHROUGH */
                   1184:        case (MDOC_TAIL):
                   1185:                /* FALLTHROUGH */
                   1186:        case (MDOC_BLOCK):
                   1187:                /* FALLTHROUGH */
                   1188:        case (MDOC_ELEM):
                   1189:                if (NULL == mdocs[n->tok])
                   1190:                        break;
                   1191:
                   1192:                buf->len = 0;
                   1193:                (*mdocs[n->tok])(hash, buf, dbuf, n, m);
                   1194:                break;
                   1195:        default:
                   1196:                break;
                   1197:        }
                   1198:
                   1199:        pmdoc_node(hash, buf, dbuf, n->child, m);
                   1200:        pmdoc_node(hash, buf, dbuf, n->next, m);
                   1201: }
                   1202:
                   1203: static int
                   1204: pman_node(MAN_ARGS)
                   1205: {
                   1206:        const struct man_node *head, *body;
                   1207:        const char      *start, *sv;
                   1208:        size_t           sz;
                   1209:
                   1210:        if (NULL == n)
                   1211:                return(0);
                   1212:
                   1213:        /*
                   1214:         * We're only searching for one thing: the first text child in
                   1215:         * the BODY of a NAME section.  Since we don't keep track of
                   1216:         * sections in -man, run some hoops to find out whether we're in
                   1217:         * the correct section or not.
                   1218:         */
                   1219:
                   1220:        if (MAN_BODY == n->type && MAN_SH == n->tok) {
                   1221:                body = n;
                   1222:                assert(body->parent);
                   1223:                if (NULL != (head = body->parent->head) &&
                   1224:                                1 == head->nchild &&
                   1225:                                NULL != (head = (head->child)) &&
                   1226:                                MAN_TEXT == head->type &&
                   1227:                                0 == strcmp(head->string, "NAME") &&
                   1228:                                NULL != (body = body->child) &&
                   1229:                                MAN_TEXT == body->type) {
                   1230:
                   1231:                        assert(body->string);
                   1232:                        start = sv = body->string;
                   1233:
                   1234:                        /*
                   1235:                         * Go through a special heuristic dance here.
                   1236:                         * This is why -man manuals are great!
                   1237:                         * (I'm being sarcastic: my eyes are bleeding.)
                   1238:                         * Conventionally, one or more manual names are
                   1239:                         * comma-specified prior to a whitespace, then a
                   1240:                         * dash, then a description.  Try to puzzle out
                   1241:                         * the name parts here.
                   1242:                         */
                   1243:
                   1244:                        for ( ;; ) {
                   1245:                                sz = strcspn(start, " ,");
                   1246:                                if ('\0' == start[(int)sz])
                   1247:                                        break;
                   1248:
                   1249:                                buf->len = 0;
                   1250:                                buf_appendb(buf, start, sz);
                   1251:                                buf_appendb(buf, "", 1);
                   1252:
1.8       schwarze 1253:                                hash_put(hash, buf, TYPE_Nm);
1.1       kristaps 1254:
                   1255:                                if (' ' == start[(int)sz]) {
                   1256:                                        start += (int)sz + 1;
                   1257:                                        break;
                   1258:                                }
                   1259:
                   1260:                                assert(',' == start[(int)sz]);
                   1261:                                start += (int)sz + 1;
                   1262:                                while (' ' == *start)
                   1263:                                        start++;
                   1264:                        }
                   1265:
                   1266:                        buf->len = 0;
                   1267:
                   1268:                        if (sv == start) {
                   1269:                                buf_append(buf, start);
                   1270:                                return(1);
                   1271:                        }
                   1272:
                   1273:                        while (' ' == *start)
                   1274:                                start++;
                   1275:
                   1276:                        if (0 == strncmp(start, "-", 1))
                   1277:                                start += 1;
                   1278:                        else if (0 == strncmp(start, "\\-", 2))
                   1279:                                start += 2;
                   1280:                        else if (0 == strncmp(start, "\\(en", 4))
                   1281:                                start += 4;
                   1282:                        else if (0 == strncmp(start, "\\(em", 4))
                   1283:                                start += 4;
                   1284:
                   1285:                        while (' ' == *start)
                   1286:                                start++;
                   1287:
                   1288:                        sz = strlen(start) + 1;
                   1289:                        buf_appendb(dbuf, start, sz);
                   1290:                        buf_appendb(buf, start, sz);
                   1291:
1.8       schwarze 1292:                        hash_put(hash, buf, TYPE_Nd);
1.1       kristaps 1293:                }
                   1294:        }
                   1295:
1.7       schwarze 1296:        for (n = n->child; n; n = n->next)
                   1297:                if (pman_node(hash, buf, dbuf, n))
                   1298:                        return(1);
1.1       kristaps 1299:
                   1300:        return(0);
                   1301: }
                   1302:
1.14      schwarze 1303: /*
                   1304:  * Parse a formatted manual page.
                   1305:  * By necessity, this involves rather crude guesswork.
                   1306:  */
                   1307: static void
                   1308: pformatted(DB *hash, struct buf *buf, struct buf *dbuf,
                   1309:                 const struct of *of)
                   1310: {
                   1311:        FILE            *stream;
                   1312:        char            *line, *p;
                   1313:        size_t           len, plen;
                   1314:
                   1315:        if (NULL == (stream = fopen(of->fname, "r"))) {
                   1316:                perror(of->fname);
                   1317:                return;
                   1318:        }
                   1319:
                   1320:        /*
                   1321:         * Always use the title derived from the filename up front,
                   1322:         * do not even try to find it in the file.  This also makes
                   1323:         * sure we don't end up with an orphan index record, even if
                   1324:         * the file content turns out to be completely unintelligible.
                   1325:         */
                   1326:
                   1327:        buf->len = 0;
                   1328:        buf_append(buf, of->title);
                   1329:        hash_put(hash, buf, TYPE_Nm);
                   1330:
                   1331:        while (NULL != (line = fgetln(stream, &len)) && '\n' != *line)
                   1332:                /* Skip to first blank line. */ ;
                   1333:
                   1334:        while (NULL != (line = fgetln(stream, &len)) &&
                   1335:                        ('\n' == *line || ' ' == *line))
                   1336:                /* Skip to first section header. */ ;
                   1337:
                   1338:        /*
                   1339:         * If no page content can be found,
                   1340:         * reuse the page title as the page description.
                   1341:         */
                   1342:
                   1343:        if (NULL == (line = fgetln(stream, &len))) {
                   1344:                buf_appendb(dbuf, buf->cp, buf->size);
                   1345:                hash_put(hash, buf, TYPE_Nd);
                   1346:                fclose(stream);
                   1347:                return;
                   1348:        }
                   1349:        fclose(stream);
                   1350:
                   1351:        /*
                   1352:         * If there is a dash, skip to the text following it.
                   1353:         */
                   1354:
                   1355:        for (p = line, plen = len; plen; p++, plen--)
                   1356:                if ('-' == *p)
                   1357:                        break;
                   1358:        for ( ; plen; p++, plen--)
                   1359:                if ('-' != *p && ' ' != *p && 8 != *p)
                   1360:                        break;
                   1361:        if (0 == plen) {
                   1362:                p = line;
                   1363:                plen = len;
                   1364:        }
                   1365:
                   1366:        /*
                   1367:         * Copy the rest of the line, but no more than 70 bytes.
                   1368:         */
                   1369:
                   1370:        if (70 < plen)
                   1371:                plen = 70;
                   1372:        p[plen-1] = '\0';
                   1373:        buf_appendb(dbuf, p, plen);
                   1374:        buf->len = 0;
                   1375:        buf_appendb(buf, p, plen);
                   1376:        hash_put(hash, buf, TYPE_Nd);
                   1377: }
                   1378:
1.5       kristaps 1379: static void
1.16      schwarze 1380: ofile_argbuild(int argc, char *argv[], struct of **of)
1.5       kristaps 1381: {
1.12      schwarze 1382:        char             buf[MAXPATHLEN];
                   1383:        char            *sec, *arch, *title, *p;
1.14      schwarze 1384:        int              i, src_form;
1.5       kristaps 1385:        struct of       *nof;
                   1386:
                   1387:        for (i = 0; i < argc; i++) {
1.12      schwarze 1388:
                   1389:                /*
                   1390:                 * Try to infer the manual section, architecture and
                   1391:                 * page title from the path, assuming it looks like
1.14      schwarze 1392:                 *   man*[/<arch>]/<title>.<section>   or
                   1393:                 *   cat<section>[/<arch>]/<title>.0
1.12      schwarze 1394:                 */
                   1395:
                   1396:                if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {
                   1397:                        fprintf(stderr, "%s: Path too long\n", argv[i]);
                   1398:                        continue;
                   1399:                }
                   1400:                sec = arch = title = NULL;
1.14      schwarze 1401:                src_form = 0;
1.12      schwarze 1402:                p = strrchr(buf, '\0');
                   1403:                while (p-- > buf) {
                   1404:                        if (NULL == sec && '.' == *p) {
                   1405:                                sec = p + 1;
                   1406:                                *p = '\0';
1.14      schwarze 1407:                                if ('0' == *sec)
                   1408:                                        src_form |= MANDOC_FORM;
                   1409:                                else if ('1' <= *sec && '9' >= *sec)
                   1410:                                        src_form |= MANDOC_SRC;
1.12      schwarze 1411:                                continue;
                   1412:                        }
                   1413:                        if ('/' != *p)
                   1414:                                continue;
                   1415:                        if (NULL == title) {
                   1416:                                title = p + 1;
                   1417:                                *p = '\0';
                   1418:                                continue;
                   1419:                        }
1.14      schwarze 1420:                        if (strncmp("man", p + 1, 3)) {
                   1421:                                src_form |= MANDOC_SRC;
                   1422:                                arch = p + 1;
                   1423:                        } else if (strncmp("cat", p + 1, 3)) {
                   1424:                                src_form |= MANDOC_FORM;
1.12      schwarze 1425:                                arch = p + 1;
1.14      schwarze 1426:                        }
1.12      schwarze 1427:                        break;
                   1428:                }
                   1429:                if (NULL == title)
                   1430:                        title = buf;
                   1431:
                   1432:                /*
                   1433:                 * Build the file structure.
                   1434:                 */
                   1435:
1.5       kristaps 1436:                nof = mandoc_calloc(1, sizeof(struct of));
1.12      schwarze 1437:                nof->fname = mandoc_strdup(argv[i]);
                   1438:                if (NULL != sec)
                   1439:                        nof->sec = mandoc_strdup(sec);
                   1440:                if (NULL != arch)
                   1441:                        nof->arch = mandoc_strdup(arch);
                   1442:                nof->title = mandoc_strdup(title);
1.14      schwarze 1443:                nof->src_form = src_form;
1.12      schwarze 1444:
                   1445:                /*
                   1446:                 * Add the structure to the list.
                   1447:                 */
                   1448:
1.5       kristaps 1449:                if (verb > 2)
                   1450:                        printf("%s: Scheduling\n", argv[i]);
                   1451:                if (NULL == *of) {
                   1452:                        *of = nof;
                   1453:                        (*of)->first = nof;
                   1454:                } else {
                   1455:                        nof->first = (*of)->first;
                   1456:                        (*of)->next = nof;
                   1457:                        *of = nof;
                   1458:                }
                   1459:        }
                   1460: }
                   1461:
1.4       kristaps 1462: /*
                   1463:  * Recursively build up a list of files to parse.
                   1464:  * We use this instead of ftw() and so on because I don't want global
                   1465:  * variables hanging around.
                   1466:  * This ignores the mandoc.db and mandoc.index files, but assumes that
                   1467:  * everything else is a manual.
                   1468:  * Pass in a pointer to a NULL structure for the first invocation.
                   1469:  */
                   1470: static int
1.12      schwarze 1471: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1.16      schwarze 1472:                int p_src_form, struct of **of)
1.4       kristaps 1473: {
1.5       kristaps 1474:        char             buf[MAXPATHLEN];
1.14      schwarze 1475:        struct stat      sb;
1.5       kristaps 1476:        size_t           sz;
1.4       kristaps 1477:        DIR             *d;
1.12      schwarze 1478:        const char      *fn, *sec, *arch;
1.14      schwarze 1479:        char            *p, *q, *suffix;
1.4       kristaps 1480:        struct of       *nof;
                   1481:        struct dirent   *dp;
1.14      schwarze 1482:        int              src_form;
1.4       kristaps 1483:
                   1484:        if (NULL == (d = opendir(dir))) {
                   1485:                perror(dir);
                   1486:                return(0);
                   1487:        }
                   1488:
                   1489:        while (NULL != (dp = readdir(d))) {
                   1490:                fn = dp->d_name;
1.12      schwarze 1491:
                   1492:                if ('.' == *fn)
                   1493:                        continue;
                   1494:
1.14      schwarze 1495:                src_form = p_src_form;
                   1496:
1.4       kristaps 1497:                if (DT_DIR == dp->d_type) {
1.12      schwarze 1498:                        sec = psec;
                   1499:                        arch = parch;
                   1500:
                   1501:                        /*
                   1502:                         * By default, only use directories called:
1.14      schwarze 1503:                         *   man<section>/[<arch>/]   or
                   1504:                         *   cat<section>/[<arch>/]
1.12      schwarze 1505:                         */
                   1506:
                   1507:                        if (NULL == sec) {
1.14      schwarze 1508:                                if(0 == strncmp("man", fn, 3)) {
                   1509:                                        src_form |= MANDOC_SRC;
1.12      schwarze 1510:                                        sec = fn + 3;
1.14      schwarze 1511:                                } else if (0 == strncmp("cat", fn, 3)) {
                   1512:                                        src_form |= MANDOC_FORM;
                   1513:                                        sec = fn + 3;
                   1514:                                } else if (use_all)
1.12      schwarze 1515:                                        sec = fn;
                   1516:                                else
                   1517:                                        continue;
                   1518:                        } else if (NULL == arch && (use_all ||
                   1519:                                        NULL == strchr(fn, '.')))
                   1520:                                arch = fn;
                   1521:                        else if (0 == use_all)
1.5       kristaps 1522:                                continue;
                   1523:
                   1524:                        buf[0] = '\0';
                   1525:                        strlcat(buf, dir, MAXPATHLEN);
                   1526:                        strlcat(buf, "/", MAXPATHLEN);
                   1527:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1528:
1.12      schwarze 1529:                        if (MAXPATHLEN <= sz) {
                   1530:                                fprintf(stderr, "%s: Path too long\n", dir);
                   1531:                                return(0);
                   1532:                        }
                   1533:
                   1534:                        if (verb > 2)
                   1535:                                printf("%s: Scanning\n", buf);
                   1536:
                   1537:                        if ( ! ofile_dirbuild(buf, sec, arch,
1.16      schwarze 1538:                                        src_form, of))
1.12      schwarze 1539:                                return(0);
                   1540:                }
                   1541:                if (DT_REG != dp->d_type ||
                   1542:                    (NULL == psec && !use_all) ||
                   1543:                    !strcmp(MANDOC_DB, fn) ||
                   1544:                    !strcmp(MANDOC_IDX, fn))
                   1545:                        continue;
                   1546:
                   1547:                /*
                   1548:                 * By default, skip files where the file name suffix
                   1549:                 * does not agree with the section directory
                   1550:                 * they are located in.
                   1551:                 */
                   1552:
                   1553:                suffix = strrchr(fn, '.');
                   1554:                if (0 == use_all) {
                   1555:                        if (NULL == suffix)
1.5       kristaps 1556:                                continue;
1.14      schwarze 1557:                        if ((MANDOC_SRC & src_form &&
                   1558:                                         strcmp(suffix + 1, psec)) ||
                   1559:                            (MANDOC_FORM & src_form &&
                   1560:                                         strcmp(suffix + 1, "0")))
                   1561:                                        continue;
                   1562:                }
                   1563:                if (NULL != suffix) {
                   1564:                        if ('0' == suffix[1])
                   1565:                                src_form |= MANDOC_FORM;
                   1566:                        else if ('1' <= suffix[1] && '9' >= suffix[1])
                   1567:                                src_form |= MANDOC_SRC;
                   1568:                }
                   1569:
                   1570:
                   1571:                /*
                   1572:                 * Skip formatted manuals if a source version is
                   1573:                 * available.  Ignore the age: it is very unlikely
                   1574:                 * that people install newer formatted base manuals
                   1575:                 * when they used to have source manuals before,
                   1576:                 * and in ports, old manuals get removed on update.
                   1577:                 */
                   1578:                if (0 == use_all && MANDOC_FORM & src_form &&
                   1579:                                NULL != psec) {
                   1580:                        buf[0] = '\0';
                   1581:                        strlcat(buf, dir, MAXPATHLEN);
                   1582:                        p = strrchr(buf, '/');
                   1583:                        if (NULL == p)
                   1584:                                p = buf;
                   1585:                        else
                   1586:                                p++;
                   1587:                        if (0 == strncmp("cat", p, 3))
                   1588:                                memcpy(p, "man", 3);
                   1589:                        strlcat(buf, "/", MAXPATHLEN);
                   1590:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1591:                        if (sz >= MAXPATHLEN) {
                   1592:                                fprintf(stderr, "%s: Path too long\n", buf);
1.5       kristaps 1593:                                continue;
1.14      schwarze 1594:                        }
                   1595:                        q = strrchr(buf, '.');
                   1596:                        if (NULL != q && p < q++) {
                   1597:                                *q = '\0';
                   1598:                                sz = strlcat(buf, psec, MAXPATHLEN);
                   1599:                                if (sz >= MAXPATHLEN) {
                   1600:                                        fprintf(stderr,
                   1601:                                            "%s: Path too long\n", buf);
                   1602:                                        continue;
                   1603:                                }
                   1604:                                if (0 == stat(buf, &sb))
                   1605:                                        continue;
                   1606:                        }
1.5       kristaps 1607:                }
1.4       kristaps 1608:
1.5       kristaps 1609:                buf[0] = '\0';
                   1610:                strlcat(buf, dir, MAXPATHLEN);
                   1611:                strlcat(buf, "/", MAXPATHLEN);
1.6       schwarze 1612:                sz = strlcat(buf, fn, MAXPATHLEN);
1.5       kristaps 1613:                if (sz >= MAXPATHLEN) {
                   1614:                        fprintf(stderr, "%s: Path too long\n", dir);
1.14      schwarze 1615:                        continue;
1.5       kristaps 1616:                }
                   1617:
1.4       kristaps 1618:                nof = mandoc_calloc(1, sizeof(struct of));
1.5       kristaps 1619:                nof->fname = mandoc_strdup(buf);
1.12      schwarze 1620:                if (NULL != psec)
                   1621:                        nof->sec = mandoc_strdup(psec);
                   1622:                if (NULL != parch)
                   1623:                        nof->arch = mandoc_strdup(parch);
1.14      schwarze 1624:                nof->src_form = src_form;
1.12      schwarze 1625:
                   1626:                /*
                   1627:                 * Remember the file name without the extension,
                   1628:                 * to be used as the page title in the database.
                   1629:                 */
                   1630:
                   1631:                if (NULL != suffix)
                   1632:                        *suffix = '\0';
                   1633:                nof->title = mandoc_strdup(fn);
1.5       kristaps 1634:
1.14      schwarze 1635:                /*
                   1636:                 * Add the structure to the list.
                   1637:                 */
                   1638:
1.5       kristaps 1639:                if (verb > 2)
                   1640:                        printf("%s: Scheduling\n", buf);
1.4       kristaps 1641:                if (NULL == *of) {
                   1642:                        *of = nof;
                   1643:                        (*of)->first = nof;
                   1644:                } else {
1.5       kristaps 1645:                        nof->first = (*of)->first;
1.4       kristaps 1646:                        (*of)->next = nof;
                   1647:                        *of = nof;
                   1648:                }
                   1649:        }
                   1650:
1.7       schwarze 1651:        closedir(d);
1.4       kristaps 1652:        return(1);
                   1653: }
                   1654:
                   1655: static void
                   1656: ofile_free(struct of *of)
                   1657: {
                   1658:        struct of       *nof;
                   1659:
                   1660:        while (of) {
                   1661:                nof = of->next;
                   1662:                free(of->fname);
1.12      schwarze 1663:                free(of->sec);
                   1664:                free(of->arch);
                   1665:                free(of->title);
1.4       kristaps 1666:                free(of);
                   1667:                of = nof;
                   1668:        }
                   1669: }
                   1670:
1.1       kristaps 1671: static void
                   1672: usage(void)
                   1673: {
                   1674:
1.5       kristaps 1675:        fprintf(stderr, "usage: %s [-v] "
                   1676:                        "[-d dir [files...] |"
                   1677:                        " -u dir [files...] |"
                   1678:                        " dir...]\n", progname);
1.1       kristaps 1679: }

CVSweb