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

Annotation of src/usr.bin/mandoc/mandocdb.c, Revision 1.14

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