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

Annotation of mandoc/mandocdb.c, Revision 1.22

1.22    ! kristaps    1: /*     $Id: mandocdb.c,v 1.21 2011/12/02 00:21:56 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));
1.22    ! kristaps  336:        info.lorder = 4321;
1.4       kristaps  337:        info.flags = R_DUP;
1.1       kristaps  338:
1.4       kristaps  339:        mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
1.1       kristaps  340:
1.5       kristaps  341:        memset(&buf, 0, sizeof(struct buf));
                    342:        memset(&dbuf, 0, sizeof(struct buf));
1.1       kristaps  343:
1.4       kristaps  344:        buf.size = dbuf.size = MANDOC_BUFSZ;
1.1       kristaps  345:
1.4       kristaps  346:        buf.cp = mandoc_malloc(buf.size);
                    347:        dbuf.cp = mandoc_malloc(dbuf.size);
1.1       kristaps  348:
1.5       kristaps  349:        flags = OP_NEW == op ? O_CREAT|O_TRUNC|O_RDWR : O_CREAT|O_RDWR;
                    350:
                    351:        if (OP_UPDATE == op || OP_DELETE == op) {
                    352:                ibuf[0] = fbuf[0] = '\0';
                    353:
                    354:                strlcat(fbuf, dir, MAXPATHLEN);
                    355:                strlcat(fbuf, "/", MAXPATHLEN);
                    356:                sz1 = strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
                    357:
                    358:                strlcat(ibuf, dir, MAXPATHLEN);
                    359:                strlcat(ibuf, "/", MAXPATHLEN);
                    360:                sz2 = strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
                    361:
                    362:                if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
                    363:                        fprintf(stderr, "%s: Path too long\n", dir);
                    364:                        exit((int)MANDOCLEVEL_BADARG);
                    365:                }
                    366:
                    367:                db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
                    368:                idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
                    369:
                    370:                if (NULL == db) {
                    371:                        perror(fbuf);
                    372:                        exit((int)MANDOCLEVEL_SYSERR);
1.12      schwarze  373:                } else if (NULL == idx) {
1.5       kristaps  374:                        perror(ibuf);
                    375:                        exit((int)MANDOCLEVEL_SYSERR);
                    376:                }
                    377:
                    378:                if (verb > 2) {
                    379:                        printf("%s: Opened\n", fbuf);
                    380:                        printf("%s: Opened\n", ibuf);
                    381:                }
                    382:
1.16      schwarze  383:                ofile_argbuild(argc, argv, &of);
1.5       kristaps  384:                if (NULL == of)
                    385:                        goto out;
                    386:
                    387:                of = of->first;
                    388:
1.16      schwarze  389:                index_prune(of, db, fbuf, idx, ibuf,
1.5       kristaps  390:                                &maxrec, &recs, &recsz);
                    391:
1.17      schwarze  392:                /*
                    393:                 * Go to the root of the respective manual tree
                    394:                 * such that .so links work.  In case of failure,
                    395:                 * just prod on, even though .so links won't work.
                    396:                 */
                    397:
                    398:                if (OP_UPDATE == op) {
                    399:                        chdir(dir);
1.13      schwarze  400:                        index_merge(of, mp, &dbuf, &buf, hash,
1.16      schwarze  401:                                        db, fbuf, idx, ibuf,
                    402:                                        maxrec, recs, reccur);
1.17      schwarze  403:                }
1.5       kristaps  404:
                    405:                goto out;
                    406:        }
                    407:
1.10      kristaps  408:        /*
                    409:         * Configure the directories we're going to scan.
                    410:         * If we have command-line arguments, use them.
                    411:         * If not, we use man(1)'s method (see mandocdb.8).
                    412:         */
                    413:
                    414:        if (argc > 0) {
                    415:                dirs.paths = mandoc_malloc(argc * sizeof(char *));
                    416:                dirs.sz = argc;
                    417:                for (i = 0; i < argc; i++)
                    418:                        dirs.paths[i] = mandoc_strdup(argv[i]);
                    419:        } else
1.11      kristaps  420:                manpath_parse(&dirs, NULL, NULL);
1.10      kristaps  421:
                    422:        for (i = 0; i < dirs.sz; i++) {
1.5       kristaps  423:                ibuf[0] = fbuf[0] = '\0';
1.1       kristaps  424:
1.10      kristaps  425:                strlcat(fbuf, dirs.paths[i], MAXPATHLEN);
1.5       kristaps  426:                strlcat(fbuf, "/", MAXPATHLEN);
                    427:                sz1 = strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
1.1       kristaps  428:
1.10      kristaps  429:                strlcat(ibuf, dirs.paths[i], MAXPATHLEN);
1.5       kristaps  430:                strlcat(ibuf, "/", MAXPATHLEN);
                    431:                sz2 = strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
1.1       kristaps  432:
1.5       kristaps  433:                if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
1.13      schwarze  434:                        fprintf(stderr, "%s: Path too long\n",
1.10      kristaps  435:                                        dirs.paths[i]);
1.5       kristaps  436:                        exit((int)MANDOCLEVEL_BADARG);
1.4       kristaps  437:                }
1.3       kristaps  438:
1.13      schwarze  439:                if (db)
                    440:                        (*db->close)(db);
                    441:                if (idx)
                    442:                        (*idx->close)(idx);
                    443:
1.4       kristaps  444:                db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
                    445:                idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
1.3       kristaps  446:
1.4       kristaps  447:                if (NULL == db) {
                    448:                        perror(fbuf);
1.5       kristaps  449:                        exit((int)MANDOCLEVEL_SYSERR);
1.12      schwarze  450:                } else if (NULL == idx) {
1.4       kristaps  451:                        perror(ibuf);
1.5       kristaps  452:                        exit((int)MANDOCLEVEL_SYSERR);
                    453:                }
                    454:
                    455:                if (verb > 2) {
                    456:                        printf("%s: Truncated\n", fbuf);
                    457:                        printf("%s: Truncated\n", ibuf);
1.4       kristaps  458:                }
1.1       kristaps  459:
1.4       kristaps  460:                ofile_free(of);
                    461:                of = NULL;
1.1       kristaps  462:
1.12      schwarze  463:                if ( ! ofile_dirbuild(dirs.paths[i], NULL, NULL,
1.16      schwarze  464:                                        0, &of))
1.5       kristaps  465:                        exit((int)MANDOCLEVEL_SYSERR);
1.1       kristaps  466:
1.5       kristaps  467:                if (NULL == of)
                    468:                        continue;
1.1       kristaps  469:
1.5       kristaps  470:                of = of->first;
1.1       kristaps  471:
1.17      schwarze  472:                /*
                    473:                 * Go to the root of the respective manual tree
                    474:                 * such that .so links work.  In case of failure,
                    475:                 * just prod on, even though .so links won't work.
                    476:                 */
                    477:
                    478:                chdir(dirs.paths[i]);
1.13      schwarze  479:                index_merge(of, mp, &dbuf, &buf, hash, db, fbuf,
1.16      schwarze  480:                                idx, ibuf, maxrec, recs, reccur);
1.4       kristaps  481:        }
1.3       kristaps  482:
1.5       kristaps  483: out:
1.3       kristaps  484:        if (db)
                    485:                (*db->close)(db);
                    486:        if (idx)
                    487:                (*idx->close)(idx);
                    488:        if (hash)
                    489:                (*hash->close)(hash);
                    490:        if (mp)
                    491:                mparse_free(mp);
                    492:
1.10      kristaps  493:        manpath_free(&dirs);
1.4       kristaps  494:        ofile_free(of);
1.3       kristaps  495:        free(buf.cp);
                    496:        free(dbuf.cp);
                    497:        free(recs);
                    498:
1.5       kristaps  499:        return(MANDOCLEVEL_OK);
1.3       kristaps  500: }
                    501:
                    502: void
                    503: index_merge(const struct of *of, struct mparse *mp,
1.16      schwarze  504:                struct buf *dbuf, struct buf *buf, DB *hash,
                    505:                DB *db, const char *dbf, DB *idx, const char *idxf,
1.3       kristaps  506:                recno_t maxrec, const recno_t *recs, size_t reccur)
                    507: {
                    508:        recno_t          rec;
                    509:        int              ch;
                    510:        DBT              key, val;
                    511:        struct mdoc     *mdoc;
                    512:        struct man      *man;
                    513:        const char      *fn, *msec, *mtitle, *arch;
                    514:        size_t           sv;
                    515:        unsigned         seq;
1.9       kristaps  516:        struct db_val    vbuf;
1.3       kristaps  517:
                    518:        for (rec = 0; of; of = of->next) {
                    519:                fn = of->fname;
1.14      schwarze  520:
                    521:                /*
                    522:                 * Reclaim an empty index record, if available.
                    523:                 */
                    524:
1.3       kristaps  525:                if (reccur > 0) {
                    526:                        --reccur;
                    527:                        rec = recs[(int)reccur];
                    528:                } else if (maxrec > 0) {
                    529:                        rec = maxrec;
                    530:                        maxrec = 0;
1.1       kristaps  531:                } else
                    532:                        rec++;
                    533:
                    534:                mparse_reset(mp);
                    535:                hash_reset(&hash);
1.14      schwarze  536:                mdoc = NULL;
                    537:                man = NULL;
1.1       kristaps  538:
1.14      schwarze  539:                /*
                    540:                 * Try interpreting the file as mdoc(7) or man(7)
                    541:                 * source code, unless it is already known to be
                    542:                 * formatted.  Fall back to formatted mode.
                    543:                 */
                    544:
                    545:                if ((MANDOC_SRC & of->src_form ||
                    546:                    ! (MANDOC_FORM & of->src_form)) &&
                    547:                    MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn))
                    548:                        mparse_result(mp, &mdoc, &man);
                    549:
                    550:                if (NULL != mdoc) {
                    551:                        msec = mdoc_meta(mdoc)->msec;
                    552:                        arch = mdoc_meta(mdoc)->arch;
                    553:                        mtitle = mdoc_meta(mdoc)->title;
                    554:                } else if (NULL != man) {
                    555:                        msec = man_meta(man)->msec;
                    556:                        arch = NULL;
                    557:                        mtitle = man_meta(man)->title;
                    558:                } else {
                    559:                        msec = of->sec;
                    560:                        arch = of->arch;
                    561:                        mtitle = of->title;
1.1       kristaps  562:                }
                    563:
1.12      schwarze  564:                /*
                    565:                 * By default, skip a file if the manual section
                    566:                 * and architecture given in the file disagree
                    567:                 * with the directory where the file is located.
                    568:                 */
                    569:
                    570:                if (0 == use_all) {
                    571:                        assert(of->sec);
                    572:                        assert(msec);
                    573:                        if (strcmp(msec, of->sec))
                    574:                                continue;
                    575:
                    576:                        if (NULL == arch) {
                    577:                                if (NULL != of->arch)
                    578:                                        continue;
                    579:                        } else if (NULL == of->arch ||
                    580:                                        strcmp(arch, of->arch))
                    581:                                continue;
                    582:                }
                    583:
1.1       kristaps  584:                if (NULL == arch)
                    585:                        arch = "";
                    586:
                    587:                /*
1.12      schwarze  588:                 * By default, skip a file if the title given
                    589:                 * in the file disagrees with the file name.
                    590:                 * If both agree, use the file name as the title,
                    591:                 * because the one in the file usually is all caps.
                    592:                 */
                    593:
                    594:                assert(of->title);
                    595:                assert(mtitle);
                    596:
                    597:                if (0 == strcasecmp(mtitle, of->title))
                    598:                        mtitle = of->title;
                    599:                else if (0 == use_all)
                    600:                        continue;
                    601:
                    602:                /*
1.1       kristaps  603:                 * The index record value consists of a nil-terminated
                    604:                 * filename, a nil-terminated manual section, and a
                    605:                 * nil-terminated description.  Since the description
                    606:                 * may not be set, we set a sentinel to see if we're
                    607:                 * going to write a nil byte in its place.
                    608:                 */
                    609:
1.3       kristaps  610:                dbuf->len = 0;
1.15      schwarze  611:                buf_append(dbuf, mdoc ? "mdoc" : (man ? "man" : "cat"));
1.3       kristaps  612:                buf_appendb(dbuf, fn, strlen(fn) + 1);
                    613:                buf_appendb(dbuf, msec, strlen(msec) + 1);
                    614:                buf_appendb(dbuf, mtitle, strlen(mtitle) + 1);
                    615:                buf_appendb(dbuf, arch, strlen(arch) + 1);
1.1       kristaps  616:
1.3       kristaps  617:                sv = dbuf->len;
1.1       kristaps  618:
                    619:                /* Fix the record number in the btree value. */
                    620:
                    621:                if (mdoc)
1.3       kristaps  622:                        pmdoc_node(hash, buf, dbuf,
1.1       kristaps  623:                                mdoc_node(mdoc), mdoc_meta(mdoc));
1.14      schwarze  624:                else if (man)
1.3       kristaps  625:                        pman_node(hash, buf, dbuf, man_node(man));
1.14      schwarze  626:                else
                    627:                        pformatted(hash, buf, dbuf, of);
1.1       kristaps  628:
                    629:                /*
                    630:                 * Copy from the in-memory hashtable of pending keywords
                    631:                 * into the database.
                    632:                 */
                    633:
1.20      kristaps  634:                vbuf.rec = htobe32(rec);
1.1       kristaps  635:                seq = R_FIRST;
                    636:                while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
                    637:                        seq = R_NEXT;
1.20      kristaps  638:                        vbuf.mask = htobe64(*(uint64_t *)val.data);
1.9       kristaps  639:                        val.size = sizeof(struct db_val);
                    640:                        val.data = &vbuf;
1.3       kristaps  641:                        dbt_put(db, dbf, &key, &val);
1.1       kristaps  642:                }
                    643:                if (ch < 0) {
                    644:                        perror("hash");
                    645:                        exit((int)MANDOCLEVEL_SYSERR);
                    646:                }
                    647:
                    648:                /*
                    649:                 * Apply to the index.  If we haven't had a description
                    650:                 * set, put an empty one in now.
                    651:                 */
                    652:
1.3       kristaps  653:                if (dbuf->len == sv)
                    654:                        buf_appendb(dbuf, "", 1);
1.1       kristaps  655:
                    656:                key.data = &rec;
                    657:                key.size = sizeof(recno_t);
                    658:
1.3       kristaps  659:                val.data = dbuf->cp;
                    660:                val.size = dbuf->len;
1.1       kristaps  661:
1.5       kristaps  662:                if (verb)
                    663:                        printf("%s: Added index\n", fn);
1.18      kristaps  664:
1.3       kristaps  665:                dbt_put(idx, idxf, &key, &val);
                    666:        }
                    667: }
                    668:
                    669: /*
                    670:  * Scan through all entries in the index file `idx' and prune those
                    671:  * entries in `ofile'.
                    672:  * Pruning consists of removing from `db', then invalidating the entry
                    673:  * in `idx' (zeroing its value size).
                    674:  */
                    675: static void
                    676: index_prune(const struct of *ofile, DB *db, const char *dbf,
1.16      schwarze  677:                DB *idx, const char *idxf,
1.3       kristaps  678:                recno_t *maxrec, recno_t **recs, size_t *recsz)
                    679: {
                    680:        const struct of *of;
1.18      kristaps  681:        const char      *fn, *cp;
1.9       kristaps  682:        struct db_val   *vbuf;
1.3       kristaps  683:        unsigned         seq, sseq;
                    684:        DBT              key, val;
                    685:        size_t           reccur;
                    686:        int              ch;
                    687:
                    688:        reccur = 0;
                    689:        seq = R_FIRST;
                    690:        while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
                    691:                seq = R_NEXT;
                    692:                *maxrec = *(recno_t *)key.data;
1.18      kristaps  693:                cp = val.data;
                    694:
                    695:                /* Deleted records are zero-sized.  Skip them. */
                    696:
                    697:                if (0 == val.size)
                    698:                        goto cont;
                    699:
                    700:                /*
                    701:                 * Make sure we're sane.
                    702:                 * Read past our mdoc/man/cat type to the next string,
                    703:                 * then make sure it's bounded by a NUL.
                    704:                 * Failing any of these, we go into our error handler.
                    705:                 */
                    706:
                    707:                if (NULL == (fn = memchr(cp, '\0', val.size)))
                    708:                        break;
                    709:                if (++fn - cp >= (int)val.size)
                    710:                        break;
                    711:                if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
                    712:                        break;
                    713:
                    714:                /*
                    715:                 * Search for the file in those we care about.
                    716:                 * XXX: build this into a tree.  Too slow.
                    717:                 */
1.3       kristaps  718:
                    719:                for (of = ofile; of; of = of->next)
                    720:                        if (0 == strcmp(fn, of->fname))
                    721:                                break;
                    722:
                    723:                if (NULL == of)
                    724:                        continue;
                    725:
1.18      kristaps  726:                /*
                    727:                 * Search through the keyword database, throwing out all
                    728:                 * references to our file.
                    729:                 */
                    730:
1.3       kristaps  731:                sseq = R_FIRST;
                    732:                while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) {
                    733:                        sseq = R_NEXT;
1.18      kristaps  734:                        if (sizeof(struct db_val) != val.size)
                    735:                                break;
                    736:
1.9       kristaps  737:                        vbuf = val.data;
1.20      kristaps  738:                        if (*maxrec != betoh32(vbuf->rec))
1.3       kristaps  739:                                continue;
1.18      kristaps  740:
                    741:                        if ((ch = (*db->del)(db, &key, R_CURSOR)) < 0)
1.3       kristaps  742:                                break;
                    743:                }
1.18      kristaps  744:
1.3       kristaps  745:                if (ch < 0) {
                    746:                        perror(dbf);
                    747:                        exit((int)MANDOCLEVEL_SYSERR);
1.18      kristaps  748:                } else if (1 != ch) {
                    749:                        fprintf(stderr, "%s: Corrupt database\n", dbf);
                    750:                        exit((int)MANDOCLEVEL_SYSERR);
1.3       kristaps  751:                }
1.1       kristaps  752:
1.5       kristaps  753:                if (verb)
                    754:                        printf("%s: Deleted index\n", fn);
1.1       kristaps  755:
1.3       kristaps  756:                val.size = 0;
                    757:                ch = (*idx->put)(idx, &key, &val, R_CURSOR);
1.1       kristaps  758:
1.18      kristaps  759:                if (ch < 0)
                    760:                        break;
                    761: cont:
1.3       kristaps  762:                if (reccur >= *recsz) {
                    763:                        *recsz += MANDOC_SLOP;
                    764:                        *recs = mandoc_realloc
                    765:                                (*recs, *recsz * sizeof(recno_t));
                    766:                }
1.1       kristaps  767:
1.3       kristaps  768:                (*recs)[(int)reccur] = *maxrec;
                    769:                reccur++;
                    770:        }
1.18      kristaps  771:
                    772:        if (ch < 0) {
                    773:                perror(idxf);
                    774:                exit((int)MANDOCLEVEL_SYSERR);
                    775:        } else if (1 != ch) {
                    776:                fprintf(stderr, "%s: Corrupt index\n", idxf);
                    777:                exit((int)MANDOCLEVEL_SYSERR);
                    778:        }
                    779:
1.3       kristaps  780:        (*maxrec)++;
1.1       kristaps  781: }
                    782:
                    783: /*
                    784:  * Grow the buffer (if necessary) and copy in a binary string.
                    785:  */
                    786: static void
                    787: buf_appendb(struct buf *buf, const void *cp, size_t sz)
                    788: {
                    789:
                    790:        /* Overshoot by MANDOC_BUFSZ. */
                    791:
                    792:        while (buf->len + sz >= buf->size) {
                    793:                buf->size = buf->len + sz + MANDOC_BUFSZ;
                    794:                buf->cp = mandoc_realloc(buf->cp, buf->size);
                    795:        }
                    796:
                    797:        memcpy(buf->cp + (int)buf->len, cp, sz);
                    798:        buf->len += sz;
                    799: }
                    800:
                    801: /*
                    802:  * Append a nil-terminated string to the buffer.
                    803:  * This can be invoked multiple times.
                    804:  * The buffer string will be nil-terminated.
                    805:  * If invoked multiple times, a space is put between strings.
                    806:  */
                    807: static void
                    808: buf_append(struct buf *buf, const char *cp)
                    809: {
                    810:        size_t           sz;
                    811:
                    812:        if (0 == (sz = strlen(cp)))
                    813:                return;
                    814:
                    815:        if (buf->len)
                    816:                buf->cp[(int)buf->len - 1] = ' ';
                    817:
                    818:        buf_appendb(buf, cp, sz + 1);
                    819: }
                    820:
                    821: /*
                    822:  * Recursively add all text from a given node.
                    823:  * This is optimised for general mdoc nodes in this context, which do
                    824:  * not consist of subexpressions and having a recursive call for n->next
                    825:  * would be wasteful.
                    826:  * The "f" variable should be 0 unless called from pmdoc_Nd for the
                    827:  * description buffer, which does not start at the beginning of the
                    828:  * buffer.
                    829:  */
                    830: static void
                    831: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
                    832: {
                    833:
                    834:        for ( ; n; n = n->next) {
                    835:                if (n->child)
                    836:                        buf_appendmdoc(buf, n->child, f);
                    837:
                    838:                if (MDOC_TEXT == n->type && f) {
                    839:                        f = 0;
                    840:                        buf_appendb(buf, n->string,
                    841:                                        strlen(n->string) + 1);
                    842:                } else if (MDOC_TEXT == n->type)
                    843:                        buf_append(buf, n->string);
                    844:
                    845:        }
                    846: }
                    847:
                    848: /* ARGSUSED */
                    849: static void
                    850: pmdoc_An(MDOC_ARGS)
                    851: {
                    852:
                    853:        if (SEC_AUTHORS != n->sec)
                    854:                return;
                    855:
                    856:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze  857:        hash_put(hash, buf, TYPE_An);
1.1       kristaps  858: }
                    859:
                    860: static void
                    861: hash_reset(DB **db)
                    862: {
                    863:        DB              *hash;
                    864:
                    865:        if (NULL != (hash = *db))
                    866:                (*hash->close)(hash);
                    867:
1.5       kristaps  868:        *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1.1       kristaps  869:        if (NULL == *db) {
                    870:                perror("hash");
                    871:                exit((int)MANDOCLEVEL_SYSERR);
                    872:        }
                    873: }
                    874:
                    875: /* ARGSUSED */
                    876: static void
                    877: pmdoc_Fd(MDOC_ARGS)
                    878: {
                    879:        const char      *start, *end;
                    880:        size_t           sz;
                    881:
                    882:        if (SEC_SYNOPSIS != n->sec)
                    883:                return;
                    884:        if (NULL == (n = n->child) || MDOC_TEXT != n->type)
                    885:                return;
                    886:
                    887:        /*
                    888:         * Only consider those `Fd' macro fields that begin with an
                    889:         * "inclusion" token (versus, e.g., #define).
                    890:         */
                    891:        if (strcmp("#include", n->string))
                    892:                return;
                    893:
                    894:        if (NULL == (n = n->next) || MDOC_TEXT != n->type)
                    895:                return;
                    896:
                    897:        /*
                    898:         * Strip away the enclosing angle brackets and make sure we're
                    899:         * not zero-length.
                    900:         */
                    901:
                    902:        start = n->string;
                    903:        if ('<' == *start || '"' == *start)
                    904:                start++;
                    905:
                    906:        if (0 == (sz = strlen(start)))
                    907:                return;
                    908:
                    909:        end = &start[(int)sz - 1];
                    910:        if ('>' == *end || '"' == *end)
                    911:                end--;
                    912:
                    913:        assert(end >= start);
                    914:
                    915:        buf_appendb(buf, start, (size_t)(end - start + 1));
                    916:        buf_appendb(buf, "", 1);
                    917:
1.8       schwarze  918:        hash_put(hash, buf, TYPE_In);
1.1       kristaps  919: }
                    920:
                    921: /* ARGSUSED */
                    922: static void
                    923: pmdoc_Cd(MDOC_ARGS)
                    924: {
                    925:
                    926:        if (SEC_SYNOPSIS != n->sec)
                    927:                return;
                    928:
                    929:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze  930:        hash_put(hash, buf, TYPE_Cd);
1.1       kristaps  931: }
                    932:
                    933: /* ARGSUSED */
                    934: static void
                    935: pmdoc_In(MDOC_ARGS)
                    936: {
                    937:
                    938:        if (SEC_SYNOPSIS != n->sec)
                    939:                return;
                    940:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    941:                return;
                    942:
                    943:        buf_append(buf, n->child->string);
1.8       schwarze  944:        hash_put(hash, buf, TYPE_In);
1.1       kristaps  945: }
                    946:
                    947: /* ARGSUSED */
                    948: static void
                    949: pmdoc_Fn(MDOC_ARGS)
                    950: {
                    951:        const char      *cp;
                    952:
                    953:        if (SEC_SYNOPSIS != n->sec)
                    954:                return;
                    955:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    956:                return;
                    957:
                    958:        /* .Fn "struct type *arg" "foo" */
                    959:
                    960:        cp = strrchr(n->child->string, ' ');
                    961:        if (NULL == cp)
                    962:                cp = n->child->string;
                    963:
                    964:        /* Strip away pointer symbol. */
                    965:
                    966:        while ('*' == *cp)
                    967:                cp++;
                    968:
                    969:        buf_append(buf, cp);
1.8       schwarze  970:        hash_put(hash, buf, TYPE_Fn);
1.1       kristaps  971: }
                    972:
                    973: /* ARGSUSED */
                    974: static void
                    975: pmdoc_St(MDOC_ARGS)
                    976: {
                    977:
                    978:        if (SEC_STANDARDS != n->sec)
                    979:                return;
                    980:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    981:                return;
                    982:
                    983:        buf_append(buf, n->child->string);
1.8       schwarze  984:        hash_put(hash, buf, TYPE_St);
1.1       kristaps  985: }
                    986:
                    987: /* ARGSUSED */
                    988: static void
                    989: pmdoc_Xr(MDOC_ARGS)
                    990: {
                    991:
                    992:        if (NULL == (n = n->child))
                    993:                return;
                    994:
                    995:        buf_appendb(buf, n->string, strlen(n->string));
                    996:
                    997:        if (NULL != (n = n->next)) {
                    998:                buf_appendb(buf, ".", 1);
                    999:                buf_appendb(buf, n->string, strlen(n->string) + 1);
                   1000:        } else
                   1001:                buf_appendb(buf, ".", 2);
                   1002:
1.8       schwarze 1003:        hash_put(hash, buf, TYPE_Xr);
1.1       kristaps 1004: }
                   1005:
                   1006: /* ARGSUSED */
                   1007: static void
                   1008: pmdoc_Vt(MDOC_ARGS)
                   1009: {
                   1010:        const char      *start;
                   1011:        size_t           sz;
                   1012:
                   1013:        if (SEC_SYNOPSIS != n->sec)
                   1014:                return;
                   1015:        if (MDOC_Vt == n->tok && MDOC_BODY != n->type)
                   1016:                return;
                   1017:        if (NULL == n->last || MDOC_TEXT != n->last->type)
                   1018:                return;
                   1019:
                   1020:        /*
                   1021:         * Strip away leading pointer symbol '*' and trailing ';'.
                   1022:         */
                   1023:
                   1024:        start = n->last->string;
                   1025:
                   1026:        while ('*' == *start)
                   1027:                start++;
                   1028:
                   1029:        if (0 == (sz = strlen(start)))
                   1030:                return;
                   1031:
                   1032:        if (';' == start[(int)sz - 1])
                   1033:                sz--;
                   1034:
                   1035:        if (0 == sz)
                   1036:                return;
                   1037:
                   1038:        buf_appendb(buf, start, sz);
                   1039:        buf_appendb(buf, "", 1);
1.8       schwarze 1040:        hash_put(hash, buf, TYPE_Va);
1.1       kristaps 1041: }
                   1042:
                   1043: /* ARGSUSED */
                   1044: static void
                   1045: pmdoc_Fo(MDOC_ARGS)
                   1046: {
                   1047:
                   1048:        if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1049:                return;
                   1050:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                   1051:                return;
                   1052:
                   1053:        buf_append(buf, n->child->string);
1.8       schwarze 1054:        hash_put(hash, buf, TYPE_Fn);
1.1       kristaps 1055: }
                   1056:
                   1057:
                   1058: /* ARGSUSED */
                   1059: static void
                   1060: pmdoc_Nd(MDOC_ARGS)
                   1061: {
                   1062:
                   1063:        if (MDOC_BODY != n->type)
                   1064:                return;
                   1065:
                   1066:        buf_appendmdoc(dbuf, n->child, 1);
                   1067:        buf_appendmdoc(buf, n->child, 0);
                   1068:
1.8       schwarze 1069:        hash_put(hash, buf, TYPE_Nd);
1.1       kristaps 1070: }
                   1071:
                   1072: /* ARGSUSED */
                   1073: static void
                   1074: pmdoc_Er(MDOC_ARGS)
                   1075: {
                   1076:
                   1077:        if (SEC_ERRORS != n->sec)
                   1078:                return;
                   1079:
                   1080:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1081:        hash_put(hash, buf, TYPE_Er);
1.1       kristaps 1082: }
                   1083:
                   1084: /* ARGSUSED */
                   1085: static void
                   1086: pmdoc_Ev(MDOC_ARGS)
                   1087: {
                   1088:
                   1089:        if (SEC_ENVIRONMENT != n->sec)
                   1090:                return;
                   1091:
                   1092:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1093:        hash_put(hash, buf, TYPE_Ev);
1.1       kristaps 1094: }
                   1095:
                   1096: /* ARGSUSED */
                   1097: static void
                   1098: pmdoc_Pa(MDOC_ARGS)
                   1099: {
                   1100:
                   1101:        if (SEC_FILES != n->sec)
                   1102:                return;
                   1103:
                   1104:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1105:        hash_put(hash, buf, TYPE_Pa);
1.1       kristaps 1106: }
                   1107:
                   1108: /* ARGSUSED */
                   1109: static void
                   1110: pmdoc_Nm(MDOC_ARGS)
                   1111: {
                   1112:
                   1113:        if (SEC_NAME == n->sec) {
                   1114:                buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1115:                hash_put(hash, buf, TYPE_Nm);
1.1       kristaps 1116:                return;
                   1117:        } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1118:                return;
                   1119:
                   1120:        if (NULL == n->child)
                   1121:                buf_append(buf, m->name);
                   1122:
                   1123:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1124:        hash_put(hash, buf, TYPE_Nm);
1.1       kristaps 1125: }
                   1126:
                   1127: static void
1.9       kristaps 1128: hash_put(DB *db, const struct buf *buf, uint64_t mask)
1.1       kristaps 1129: {
                   1130:        DBT              key, val;
                   1131:        int              rc;
                   1132:
                   1133:        if (buf->len < 2)
                   1134:                return;
                   1135:
                   1136:        key.data = buf->cp;
                   1137:        key.size = buf->len;
                   1138:
                   1139:        if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
                   1140:                perror("hash");
                   1141:                exit((int)MANDOCLEVEL_SYSERR);
                   1142:        } else if (0 == rc)
1.9       kristaps 1143:                mask |= *(uint64_t *)val.data;
1.1       kristaps 1144:
                   1145:        val.data = &mask;
1.9       kristaps 1146:        val.size = sizeof(uint64_t);
1.1       kristaps 1147:
                   1148:        if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
                   1149:                perror("hash");
                   1150:                exit((int)MANDOCLEVEL_SYSERR);
                   1151:        }
                   1152: }
                   1153:
                   1154: static void
                   1155: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
                   1156: {
                   1157:
                   1158:        assert(key->size);
                   1159:        assert(val->size);
                   1160:
                   1161:        if (0 == (*db->put)(db, key, val, 0))
                   1162:                return;
                   1163:
                   1164:        perror(dbn);
                   1165:        exit((int)MANDOCLEVEL_SYSERR);
                   1166:        /* NOTREACHED */
                   1167: }
                   1168:
                   1169: /*
                   1170:  * Call out to per-macro handlers after clearing the persistent database
                   1171:  * key.  If the macro sets the database key, flush it to the database.
                   1172:  */
                   1173: static void
                   1174: pmdoc_node(MDOC_ARGS)
                   1175: {
                   1176:
                   1177:        if (NULL == n)
                   1178:                return;
                   1179:
                   1180:        switch (n->type) {
                   1181:        case (MDOC_HEAD):
                   1182:                /* FALLTHROUGH */
                   1183:        case (MDOC_BODY):
                   1184:                /* FALLTHROUGH */
                   1185:        case (MDOC_TAIL):
                   1186:                /* FALLTHROUGH */
                   1187:        case (MDOC_BLOCK):
                   1188:                /* FALLTHROUGH */
                   1189:        case (MDOC_ELEM):
                   1190:                if (NULL == mdocs[n->tok])
                   1191:                        break;
                   1192:
                   1193:                buf->len = 0;
                   1194:                (*mdocs[n->tok])(hash, buf, dbuf, n, m);
                   1195:                break;
                   1196:        default:
                   1197:                break;
                   1198:        }
                   1199:
                   1200:        pmdoc_node(hash, buf, dbuf, n->child, m);
                   1201:        pmdoc_node(hash, buf, dbuf, n->next, m);
                   1202: }
                   1203:
                   1204: static int
                   1205: pman_node(MAN_ARGS)
                   1206: {
                   1207:        const struct man_node *head, *body;
                   1208:        const char      *start, *sv;
                   1209:        size_t           sz;
                   1210:
                   1211:        if (NULL == n)
                   1212:                return(0);
                   1213:
                   1214:        /*
                   1215:         * We're only searching for one thing: the first text child in
                   1216:         * the BODY of a NAME section.  Since we don't keep track of
                   1217:         * sections in -man, run some hoops to find out whether we're in
                   1218:         * the correct section or not.
                   1219:         */
                   1220:
                   1221:        if (MAN_BODY == n->type && MAN_SH == n->tok) {
                   1222:                body = n;
                   1223:                assert(body->parent);
                   1224:                if (NULL != (head = body->parent->head) &&
                   1225:                                1 == head->nchild &&
                   1226:                                NULL != (head = (head->child)) &&
                   1227:                                MAN_TEXT == head->type &&
                   1228:                                0 == strcmp(head->string, "NAME") &&
                   1229:                                NULL != (body = body->child) &&
                   1230:                                MAN_TEXT == body->type) {
                   1231:
                   1232:                        assert(body->string);
                   1233:                        start = sv = body->string;
                   1234:
                   1235:                        /*
                   1236:                         * Go through a special heuristic dance here.
                   1237:                         * This is why -man manuals are great!
                   1238:                         * (I'm being sarcastic: my eyes are bleeding.)
                   1239:                         * Conventionally, one or more manual names are
                   1240:                         * comma-specified prior to a whitespace, then a
                   1241:                         * dash, then a description.  Try to puzzle out
                   1242:                         * the name parts here.
                   1243:                         */
                   1244:
                   1245:                        for ( ;; ) {
                   1246:                                sz = strcspn(start, " ,");
                   1247:                                if ('\0' == start[(int)sz])
                   1248:                                        break;
                   1249:
                   1250:                                buf->len = 0;
                   1251:                                buf_appendb(buf, start, sz);
                   1252:                                buf_appendb(buf, "", 1);
                   1253:
1.8       schwarze 1254:                                hash_put(hash, buf, TYPE_Nm);
1.1       kristaps 1255:
                   1256:                                if (' ' == start[(int)sz]) {
                   1257:                                        start += (int)sz + 1;
                   1258:                                        break;
                   1259:                                }
                   1260:
                   1261:                                assert(',' == start[(int)sz]);
                   1262:                                start += (int)sz + 1;
                   1263:                                while (' ' == *start)
                   1264:                                        start++;
                   1265:                        }
                   1266:
                   1267:                        buf->len = 0;
                   1268:
                   1269:                        if (sv == start) {
                   1270:                                buf_append(buf, start);
                   1271:                                return(1);
                   1272:                        }
                   1273:
                   1274:                        while (' ' == *start)
                   1275:                                start++;
                   1276:
                   1277:                        if (0 == strncmp(start, "-", 1))
                   1278:                                start += 1;
                   1279:                        else if (0 == strncmp(start, "\\-", 2))
                   1280:                                start += 2;
                   1281:                        else if (0 == strncmp(start, "\\(en", 4))
                   1282:                                start += 4;
                   1283:                        else if (0 == strncmp(start, "\\(em", 4))
                   1284:                                start += 4;
                   1285:
                   1286:                        while (' ' == *start)
                   1287:                                start++;
                   1288:
                   1289:                        sz = strlen(start) + 1;
                   1290:                        buf_appendb(dbuf, start, sz);
                   1291:                        buf_appendb(buf, start, sz);
                   1292:
1.8       schwarze 1293:                        hash_put(hash, buf, TYPE_Nd);
1.1       kristaps 1294:                }
                   1295:        }
                   1296:
1.7       schwarze 1297:        for (n = n->child; n; n = n->next)
                   1298:                if (pman_node(hash, buf, dbuf, n))
                   1299:                        return(1);
1.1       kristaps 1300:
                   1301:        return(0);
                   1302: }
                   1303:
1.14      schwarze 1304: /*
                   1305:  * Parse a formatted manual page.
                   1306:  * By necessity, this involves rather crude guesswork.
                   1307:  */
                   1308: static void
                   1309: pformatted(DB *hash, struct buf *buf, struct buf *dbuf,
                   1310:                 const struct of *of)
                   1311: {
                   1312:        FILE            *stream;
                   1313:        char            *line, *p;
                   1314:        size_t           len, plen;
                   1315:
                   1316:        if (NULL == (stream = fopen(of->fname, "r"))) {
                   1317:                perror(of->fname);
                   1318:                return;
                   1319:        }
                   1320:
                   1321:        /*
                   1322:         * Always use the title derived from the filename up front,
                   1323:         * do not even try to find it in the file.  This also makes
                   1324:         * sure we don't end up with an orphan index record, even if
                   1325:         * the file content turns out to be completely unintelligible.
                   1326:         */
                   1327:
                   1328:        buf->len = 0;
                   1329:        buf_append(buf, of->title);
                   1330:        hash_put(hash, buf, TYPE_Nm);
                   1331:
                   1332:        while (NULL != (line = fgetln(stream, &len)) && '\n' != *line)
                   1333:                /* Skip to first blank line. */ ;
                   1334:
                   1335:        while (NULL != (line = fgetln(stream, &len)) &&
                   1336:                        ('\n' == *line || ' ' == *line))
                   1337:                /* Skip to first section header. */ ;
                   1338:
                   1339:        /*
                   1340:         * If no page content can be found,
                   1341:         * reuse the page title as the page description.
                   1342:         */
                   1343:
                   1344:        if (NULL == (line = fgetln(stream, &len))) {
                   1345:                buf_appendb(dbuf, buf->cp, buf->size);
                   1346:                hash_put(hash, buf, TYPE_Nd);
                   1347:                fclose(stream);
                   1348:                return;
                   1349:        }
                   1350:        fclose(stream);
                   1351:
                   1352:        /*
                   1353:         * If there is a dash, skip to the text following it.
                   1354:         */
                   1355:
                   1356:        for (p = line, plen = len; plen; p++, plen--)
                   1357:                if ('-' == *p)
                   1358:                        break;
                   1359:        for ( ; plen; p++, plen--)
                   1360:                if ('-' != *p && ' ' != *p && 8 != *p)
                   1361:                        break;
                   1362:        if (0 == plen) {
                   1363:                p = line;
                   1364:                plen = len;
                   1365:        }
                   1366:
                   1367:        /*
                   1368:         * Copy the rest of the line, but no more than 70 bytes.
                   1369:         */
                   1370:
                   1371:        if (70 < plen)
                   1372:                plen = 70;
                   1373:        p[plen-1] = '\0';
                   1374:        buf_appendb(dbuf, p, plen);
                   1375:        buf->len = 0;
                   1376:        buf_appendb(buf, p, plen);
                   1377:        hash_put(hash, buf, TYPE_Nd);
                   1378: }
                   1379:
1.5       kristaps 1380: static void
1.16      schwarze 1381: ofile_argbuild(int argc, char *argv[], struct of **of)
1.5       kristaps 1382: {
1.12      schwarze 1383:        char             buf[MAXPATHLEN];
                   1384:        char            *sec, *arch, *title, *p;
1.14      schwarze 1385:        int              i, src_form;
1.5       kristaps 1386:        struct of       *nof;
                   1387:
                   1388:        for (i = 0; i < argc; i++) {
1.12      schwarze 1389:
                   1390:                /*
                   1391:                 * Try to infer the manual section, architecture and
                   1392:                 * page title from the path, assuming it looks like
1.14      schwarze 1393:                 *   man*[/<arch>]/<title>.<section>   or
                   1394:                 *   cat<section>[/<arch>]/<title>.0
1.12      schwarze 1395:                 */
                   1396:
                   1397:                if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {
                   1398:                        fprintf(stderr, "%s: Path too long\n", argv[i]);
                   1399:                        continue;
                   1400:                }
                   1401:                sec = arch = title = NULL;
1.14      schwarze 1402:                src_form = 0;
1.12      schwarze 1403:                p = strrchr(buf, '\0');
                   1404:                while (p-- > buf) {
                   1405:                        if (NULL == sec && '.' == *p) {
                   1406:                                sec = p + 1;
                   1407:                                *p = '\0';
1.14      schwarze 1408:                                if ('0' == *sec)
                   1409:                                        src_form |= MANDOC_FORM;
                   1410:                                else if ('1' <= *sec && '9' >= *sec)
                   1411:                                        src_form |= MANDOC_SRC;
1.12      schwarze 1412:                                continue;
                   1413:                        }
                   1414:                        if ('/' != *p)
                   1415:                                continue;
                   1416:                        if (NULL == title) {
                   1417:                                title = p + 1;
                   1418:                                *p = '\0';
                   1419:                                continue;
                   1420:                        }
1.14      schwarze 1421:                        if (strncmp("man", p + 1, 3)) {
                   1422:                                src_form |= MANDOC_SRC;
                   1423:                                arch = p + 1;
                   1424:                        } else if (strncmp("cat", p + 1, 3)) {
                   1425:                                src_form |= MANDOC_FORM;
1.12      schwarze 1426:                                arch = p + 1;
1.14      schwarze 1427:                        }
1.12      schwarze 1428:                        break;
                   1429:                }
                   1430:                if (NULL == title)
                   1431:                        title = buf;
                   1432:
                   1433:                /*
                   1434:                 * Build the file structure.
                   1435:                 */
                   1436:
1.5       kristaps 1437:                nof = mandoc_calloc(1, sizeof(struct of));
1.12      schwarze 1438:                nof->fname = mandoc_strdup(argv[i]);
                   1439:                if (NULL != sec)
                   1440:                        nof->sec = mandoc_strdup(sec);
                   1441:                if (NULL != arch)
                   1442:                        nof->arch = mandoc_strdup(arch);
                   1443:                nof->title = mandoc_strdup(title);
1.14      schwarze 1444:                nof->src_form = src_form;
1.12      schwarze 1445:
                   1446:                /*
                   1447:                 * Add the structure to the list.
                   1448:                 */
                   1449:
1.5       kristaps 1450:                if (verb > 2)
                   1451:                        printf("%s: Scheduling\n", argv[i]);
                   1452:                if (NULL == *of) {
                   1453:                        *of = nof;
                   1454:                        (*of)->first = nof;
                   1455:                } else {
                   1456:                        nof->first = (*of)->first;
                   1457:                        (*of)->next = nof;
                   1458:                        *of = nof;
                   1459:                }
                   1460:        }
                   1461: }
                   1462:
1.4       kristaps 1463: /*
                   1464:  * Recursively build up a list of files to parse.
                   1465:  * We use this instead of ftw() and so on because I don't want global
                   1466:  * variables hanging around.
                   1467:  * This ignores the mandoc.db and mandoc.index files, but assumes that
                   1468:  * everything else is a manual.
                   1469:  * Pass in a pointer to a NULL structure for the first invocation.
                   1470:  */
                   1471: static int
1.12      schwarze 1472: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1.16      schwarze 1473:                int p_src_form, struct of **of)
1.4       kristaps 1474: {
1.5       kristaps 1475:        char             buf[MAXPATHLEN];
1.14      schwarze 1476:        struct stat      sb;
1.5       kristaps 1477:        size_t           sz;
1.4       kristaps 1478:        DIR             *d;
1.12      schwarze 1479:        const char      *fn, *sec, *arch;
1.14      schwarze 1480:        char            *p, *q, *suffix;
1.4       kristaps 1481:        struct of       *nof;
                   1482:        struct dirent   *dp;
1.14      schwarze 1483:        int              src_form;
1.4       kristaps 1484:
                   1485:        if (NULL == (d = opendir(dir))) {
                   1486:                perror(dir);
                   1487:                return(0);
                   1488:        }
                   1489:
                   1490:        while (NULL != (dp = readdir(d))) {
                   1491:                fn = dp->d_name;
1.12      schwarze 1492:
                   1493:                if ('.' == *fn)
                   1494:                        continue;
                   1495:
1.14      schwarze 1496:                src_form = p_src_form;
                   1497:
1.4       kristaps 1498:                if (DT_DIR == dp->d_type) {
1.12      schwarze 1499:                        sec = psec;
                   1500:                        arch = parch;
                   1501:
                   1502:                        /*
                   1503:                         * By default, only use directories called:
1.14      schwarze 1504:                         *   man<section>/[<arch>/]   or
                   1505:                         *   cat<section>/[<arch>/]
1.12      schwarze 1506:                         */
                   1507:
                   1508:                        if (NULL == sec) {
1.14      schwarze 1509:                                if(0 == strncmp("man", fn, 3)) {
                   1510:                                        src_form |= MANDOC_SRC;
1.12      schwarze 1511:                                        sec = fn + 3;
1.14      schwarze 1512:                                } else if (0 == strncmp("cat", fn, 3)) {
                   1513:                                        src_form |= MANDOC_FORM;
                   1514:                                        sec = fn + 3;
                   1515:                                } else if (use_all)
1.12      schwarze 1516:                                        sec = fn;
                   1517:                                else
                   1518:                                        continue;
                   1519:                        } else if (NULL == arch && (use_all ||
                   1520:                                        NULL == strchr(fn, '.')))
                   1521:                                arch = fn;
                   1522:                        else if (0 == use_all)
1.5       kristaps 1523:                                continue;
                   1524:
                   1525:                        buf[0] = '\0';
                   1526:                        strlcat(buf, dir, MAXPATHLEN);
                   1527:                        strlcat(buf, "/", MAXPATHLEN);
                   1528:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1529:
1.12      schwarze 1530:                        if (MAXPATHLEN <= sz) {
                   1531:                                fprintf(stderr, "%s: Path too long\n", dir);
                   1532:                                return(0);
                   1533:                        }
                   1534:
                   1535:                        if (verb > 2)
                   1536:                                printf("%s: Scanning\n", buf);
                   1537:
                   1538:                        if ( ! ofile_dirbuild(buf, sec, arch,
1.16      schwarze 1539:                                        src_form, of))
1.12      schwarze 1540:                                return(0);
                   1541:                }
                   1542:                if (DT_REG != dp->d_type ||
                   1543:                    (NULL == psec && !use_all) ||
                   1544:                    !strcmp(MANDOC_DB, fn) ||
                   1545:                    !strcmp(MANDOC_IDX, fn))
                   1546:                        continue;
                   1547:
                   1548:                /*
                   1549:                 * By default, skip files where the file name suffix
                   1550:                 * does not agree with the section directory
                   1551:                 * they are located in.
                   1552:                 */
                   1553:
                   1554:                suffix = strrchr(fn, '.');
                   1555:                if (0 == use_all) {
                   1556:                        if (NULL == suffix)
1.5       kristaps 1557:                                continue;
1.14      schwarze 1558:                        if ((MANDOC_SRC & src_form &&
                   1559:                                         strcmp(suffix + 1, psec)) ||
                   1560:                            (MANDOC_FORM & src_form &&
                   1561:                                         strcmp(suffix + 1, "0")))
                   1562:                                        continue;
                   1563:                }
                   1564:                if (NULL != suffix) {
                   1565:                        if ('0' == suffix[1])
                   1566:                                src_form |= MANDOC_FORM;
                   1567:                        else if ('1' <= suffix[1] && '9' >= suffix[1])
                   1568:                                src_form |= MANDOC_SRC;
                   1569:                }
                   1570:
                   1571:
                   1572:                /*
                   1573:                 * Skip formatted manuals if a source version is
                   1574:                 * available.  Ignore the age: it is very unlikely
                   1575:                 * that people install newer formatted base manuals
                   1576:                 * when they used to have source manuals before,
                   1577:                 * and in ports, old manuals get removed on update.
                   1578:                 */
                   1579:                if (0 == use_all && MANDOC_FORM & src_form &&
                   1580:                                NULL != psec) {
                   1581:                        buf[0] = '\0';
                   1582:                        strlcat(buf, dir, MAXPATHLEN);
                   1583:                        p = strrchr(buf, '/');
                   1584:                        if (NULL == p)
                   1585:                                p = buf;
                   1586:                        else
                   1587:                                p++;
                   1588:                        if (0 == strncmp("cat", p, 3))
                   1589:                                memcpy(p, "man", 3);
                   1590:                        strlcat(buf, "/", MAXPATHLEN);
                   1591:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1592:                        if (sz >= MAXPATHLEN) {
                   1593:                                fprintf(stderr, "%s: Path too long\n", buf);
1.5       kristaps 1594:                                continue;
1.14      schwarze 1595:                        }
                   1596:                        q = strrchr(buf, '.');
                   1597:                        if (NULL != q && p < q++) {
                   1598:                                *q = '\0';
                   1599:                                sz = strlcat(buf, psec, MAXPATHLEN);
                   1600:                                if (sz >= MAXPATHLEN) {
                   1601:                                        fprintf(stderr,
                   1602:                                            "%s: Path too long\n", buf);
                   1603:                                        continue;
                   1604:                                }
                   1605:                                if (0 == stat(buf, &sb))
                   1606:                                        continue;
                   1607:                        }
1.5       kristaps 1608:                }
1.4       kristaps 1609:
1.5       kristaps 1610:                buf[0] = '\0';
                   1611:                strlcat(buf, dir, MAXPATHLEN);
                   1612:                strlcat(buf, "/", MAXPATHLEN);
1.6       schwarze 1613:                sz = strlcat(buf, fn, MAXPATHLEN);
1.5       kristaps 1614:                if (sz >= MAXPATHLEN) {
                   1615:                        fprintf(stderr, "%s: Path too long\n", dir);
1.14      schwarze 1616:                        continue;
1.5       kristaps 1617:                }
                   1618:
1.4       kristaps 1619:                nof = mandoc_calloc(1, sizeof(struct of));
1.5       kristaps 1620:                nof->fname = mandoc_strdup(buf);
1.12      schwarze 1621:                if (NULL != psec)
                   1622:                        nof->sec = mandoc_strdup(psec);
                   1623:                if (NULL != parch)
                   1624:                        nof->arch = mandoc_strdup(parch);
1.14      schwarze 1625:                nof->src_form = src_form;
1.12      schwarze 1626:
                   1627:                /*
                   1628:                 * Remember the file name without the extension,
                   1629:                 * to be used as the page title in the database.
                   1630:                 */
                   1631:
                   1632:                if (NULL != suffix)
                   1633:                        *suffix = '\0';
                   1634:                nof->title = mandoc_strdup(fn);
1.5       kristaps 1635:
1.14      schwarze 1636:                /*
                   1637:                 * Add the structure to the list.
                   1638:                 */
                   1639:
1.5       kristaps 1640:                if (verb > 2)
                   1641:                        printf("%s: Scheduling\n", buf);
1.4       kristaps 1642:                if (NULL == *of) {
                   1643:                        *of = nof;
                   1644:                        (*of)->first = nof;
                   1645:                } else {
1.5       kristaps 1646:                        nof->first = (*of)->first;
1.4       kristaps 1647:                        (*of)->next = nof;
                   1648:                        *of = nof;
                   1649:                }
                   1650:        }
                   1651:
1.7       schwarze 1652:        closedir(d);
1.4       kristaps 1653:        return(1);
                   1654: }
                   1655:
                   1656: static void
                   1657: ofile_free(struct of *of)
                   1658: {
                   1659:        struct of       *nof;
                   1660:
                   1661:        while (of) {
                   1662:                nof = of->next;
                   1663:                free(of->fname);
1.12      schwarze 1664:                free(of->sec);
                   1665:                free(of->arch);
                   1666:                free(of->title);
1.4       kristaps 1667:                free(of);
                   1668:                of = nof;
                   1669:        }
                   1670: }
                   1671:
1.1       kristaps 1672: static void
                   1673: usage(void)
                   1674: {
                   1675:
1.5       kristaps 1676:        fprintf(stderr, "usage: %s [-v] "
                   1677:                        "[-d dir [files...] |"
                   1678:                        " -u dir [files...] |"
                   1679:                        " dir...]\n", progname);
1.1       kristaps 1680: }

CVSweb