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

Annotation of mandoc/mandocdb.c, Revision 1.12

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

CVSweb