[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.10

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