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

Annotation of mandoc/mandocdb.c, Revision 1.13

1.13    ! schwarze    1: /*     $Id: mandocdb.c,v 1.12 2011/11/26 11:23:56 schwarze 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)
1.13    ! schwarze  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.13    ! schwarze  414:                        fprintf(stderr, "%s: Path too long\n",
1.10      kristaps  415:                                        dirs.paths[i]);
1.5       kristaps  416:                        exit((int)MANDOCLEVEL_BADARG);
1.4       kristaps  417:                }
1.3       kristaps  418:
1.13    ! schwarze  419:                if (db)
        !           420:                        (*db->close)(db);
        !           421:                if (idx)
        !           422:                        (*idx->close)(idx);
        !           423:
1.4       kristaps  424:                db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
                    425:                idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
1.3       kristaps  426:
1.4       kristaps  427:                if (NULL == db) {
                    428:                        perror(fbuf);
1.5       kristaps  429:                        exit((int)MANDOCLEVEL_SYSERR);
1.12      schwarze  430:                } else if (NULL == idx) {
1.4       kristaps  431:                        perror(ibuf);
1.5       kristaps  432:                        exit((int)MANDOCLEVEL_SYSERR);
                    433:                }
                    434:
                    435:                if (verb > 2) {
                    436:                        printf("%s: Truncated\n", fbuf);
                    437:                        printf("%s: Truncated\n", ibuf);
1.4       kristaps  438:                }
1.1       kristaps  439:
1.4       kristaps  440:                ofile_free(of);
                    441:                of = NULL;
1.1       kristaps  442:
1.12      schwarze  443:                if ( ! ofile_dirbuild(dirs.paths[i], NULL, NULL,
                    444:                                use_all, verb, &of))
1.5       kristaps  445:                        exit((int)MANDOCLEVEL_SYSERR);
1.1       kristaps  446:
1.5       kristaps  447:                if (NULL == of)
                    448:                        continue;
1.1       kristaps  449:
1.5       kristaps  450:                of = of->first;
1.1       kristaps  451:
1.13    ! schwarze  452:                index_merge(of, mp, &dbuf, &buf, hash, db, fbuf,
1.12      schwarze  453:                                idx, ibuf, use_all, verb,
                    454:                                maxrec, recs, reccur);
1.4       kristaps  455:        }
1.3       kristaps  456:
1.5       kristaps  457: out:
1.3       kristaps  458:        if (db)
                    459:                (*db->close)(db);
                    460:        if (idx)
                    461:                (*idx->close)(idx);
                    462:        if (hash)
                    463:                (*hash->close)(hash);
                    464:        if (mp)
                    465:                mparse_free(mp);
                    466:
1.10      kristaps  467:        manpath_free(&dirs);
1.4       kristaps  468:        ofile_free(of);
1.3       kristaps  469:        free(buf.cp);
                    470:        free(dbuf.cp);
                    471:        free(recs);
                    472:
1.5       kristaps  473:        return(MANDOCLEVEL_OK);
1.3       kristaps  474: }
                    475:
                    476: void
                    477: index_merge(const struct of *of, struct mparse *mp,
                    478:                struct buf *dbuf, struct buf *buf,
1.13    ! schwarze  479:                DB *hash, DB *db, const char *dbf,
1.12      schwarze  480:                DB *idx, const char *idxf, int use_all, int verb,
1.3       kristaps  481:                recno_t maxrec, const recno_t *recs, size_t reccur)
                    482: {
                    483:        recno_t          rec;
                    484:        int              ch;
                    485:        DBT              key, val;
                    486:        struct mdoc     *mdoc;
                    487:        struct man      *man;
                    488:        const char      *fn, *msec, *mtitle, *arch;
                    489:        size_t           sv;
                    490:        unsigned         seq;
1.9       kristaps  491:        struct db_val    vbuf;
1.3       kristaps  492:
                    493:        for (rec = 0; of; of = of->next) {
                    494:                fn = of->fname;
                    495:                if (reccur > 0) {
                    496:                        --reccur;
                    497:                        rec = recs[(int)reccur];
                    498:                } else if (maxrec > 0) {
                    499:                        rec = maxrec;
                    500:                        maxrec = 0;
1.1       kristaps  501:                } else
                    502:                        rec++;
                    503:
                    504:                mparse_reset(mp);
                    505:                hash_reset(&hash);
                    506:
                    507:                if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) {
                    508:                        fprintf(stderr, "%s: Parse failure\n", fn);
                    509:                        continue;
                    510:                }
                    511:
                    512:                mparse_result(mp, &mdoc, &man);
                    513:                if (NULL == mdoc && NULL == man)
                    514:                        continue;
                    515:
1.12      schwarze  516:                /*
                    517:                 * By default, skip a file if the manual section
                    518:                 * and architecture given in the file disagree
                    519:                 * with the directory where the file is located.
                    520:                 */
                    521:
1.1       kristaps  522:                msec = NULL != mdoc ?
                    523:                        mdoc_meta(mdoc)->msec : man_meta(man)->msec;
1.3       kristaps  524:                arch = NULL != mdoc ?
                    525:                        mdoc_meta(mdoc)->arch : NULL;
1.1       kristaps  526:
1.12      schwarze  527:                if (0 == use_all) {
                    528:                        assert(of->sec);
                    529:                        assert(msec);
                    530:                        if (strcmp(msec, of->sec))
                    531:                                continue;
                    532:
                    533:                        if (NULL == arch) {
                    534:                                if (NULL != of->arch)
                    535:                                        continue;
                    536:                        } else if (NULL == of->arch ||
                    537:                                        strcmp(arch, of->arch))
                    538:                                continue;
                    539:                }
                    540:
1.1       kristaps  541:                if (NULL == arch)
                    542:                        arch = "";
                    543:
                    544:                /*
1.12      schwarze  545:                 * By default, skip a file if the title given
                    546:                 * in the file disagrees with the file name.
                    547:                 * If both agree, use the file name as the title,
                    548:                 * because the one in the file usually is all caps.
                    549:                 */
                    550:
                    551:                mtitle = NULL != mdoc ?
                    552:                        mdoc_meta(mdoc)->title : man_meta(man)->title;
                    553:
                    554:                assert(of->title);
                    555:                assert(mtitle);
                    556:
                    557:                if (0 == strcasecmp(mtitle, of->title))
                    558:                        mtitle = of->title;
                    559:                else if (0 == use_all)
                    560:                        continue;
                    561:
                    562:                /*
1.1       kristaps  563:                 * The index record value consists of a nil-terminated
                    564:                 * filename, a nil-terminated manual section, and a
                    565:                 * nil-terminated description.  Since the description
                    566:                 * may not be set, we set a sentinel to see if we're
                    567:                 * going to write a nil byte in its place.
                    568:                 */
                    569:
1.3       kristaps  570:                dbuf->len = 0;
                    571:                buf_appendb(dbuf, fn, strlen(fn) + 1);
                    572:                buf_appendb(dbuf, msec, strlen(msec) + 1);
                    573:                buf_appendb(dbuf, mtitle, strlen(mtitle) + 1);
                    574:                buf_appendb(dbuf, arch, strlen(arch) + 1);
1.1       kristaps  575:
1.3       kristaps  576:                sv = dbuf->len;
1.1       kristaps  577:
                    578:                /* Fix the record number in the btree value. */
                    579:
                    580:                if (mdoc)
1.3       kristaps  581:                        pmdoc_node(hash, buf, dbuf,
1.1       kristaps  582:                                mdoc_node(mdoc), mdoc_meta(mdoc));
                    583:                else
1.3       kristaps  584:                        pman_node(hash, buf, dbuf, man_node(man));
1.1       kristaps  585:
                    586:                /*
                    587:                 * Copy from the in-memory hashtable of pending keywords
                    588:                 * into the database.
                    589:                 */
                    590:
1.9       kristaps  591:                vbuf.rec = rec;
1.1       kristaps  592:                seq = R_FIRST;
                    593:                while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
                    594:                        seq = R_NEXT;
                    595:
1.9       kristaps  596:                        vbuf.mask = *(uint64_t *)val.data;
                    597:                        val.size = sizeof(struct db_val);
                    598:                        val.data = &vbuf;
1.1       kristaps  599:
1.5       kristaps  600:                        if (verb > 1)
                    601:                                printf("%s: Added keyword: %s\n",
                    602:                                                fn, (char *)key.data);
1.3       kristaps  603:                        dbt_put(db, dbf, &key, &val);
1.1       kristaps  604:                }
                    605:                if (ch < 0) {
                    606:                        perror("hash");
                    607:                        exit((int)MANDOCLEVEL_SYSERR);
                    608:                }
                    609:
                    610:                /*
                    611:                 * Apply to the index.  If we haven't had a description
                    612:                 * set, put an empty one in now.
                    613:                 */
                    614:
1.3       kristaps  615:                if (dbuf->len == sv)
                    616:                        buf_appendb(dbuf, "", 1);
1.1       kristaps  617:
                    618:                key.data = &rec;
                    619:                key.size = sizeof(recno_t);
                    620:
1.3       kristaps  621:                val.data = dbuf->cp;
                    622:                val.size = dbuf->len;
1.1       kristaps  623:
1.5       kristaps  624:                if (verb)
                    625:                        printf("%s: Added index\n", fn);
1.3       kristaps  626:                dbt_put(idx, idxf, &key, &val);
                    627:        }
                    628: }
                    629:
                    630: /*
                    631:  * Scan through all entries in the index file `idx' and prune those
                    632:  * entries in `ofile'.
                    633:  * Pruning consists of removing from `db', then invalidating the entry
                    634:  * in `idx' (zeroing its value size).
                    635:  */
                    636: static void
                    637: index_prune(const struct of *ofile, DB *db, const char *dbf,
1.5       kristaps  638:                DB *idx, const char *idxf, int verb,
1.3       kristaps  639:                recno_t *maxrec, recno_t **recs, size_t *recsz)
                    640: {
                    641:        const struct of *of;
                    642:        const char      *fn;
1.9       kristaps  643:        struct db_val   *vbuf;
1.3       kristaps  644:        unsigned         seq, sseq;
                    645:        DBT              key, val;
                    646:        size_t           reccur;
                    647:        int              ch;
                    648:
                    649:        reccur = 0;
                    650:        seq = R_FIRST;
                    651:        while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
                    652:                seq = R_NEXT;
                    653:                *maxrec = *(recno_t *)key.data;
                    654:                if (0 == val.size) {
                    655:                        if (reccur >= *recsz) {
                    656:                                *recsz += MANDOC_SLOP;
                    657:                                *recs = mandoc_realloc(*recs,
                    658:                                        *recsz * sizeof(recno_t));
                    659:                        }
                    660:                        (*recs)[(int)reccur] = *maxrec;
                    661:                        reccur++;
                    662:                        continue;
                    663:                }
                    664:
                    665:                fn = (char *)val.data;
                    666:                for (of = ofile; of; of = of->next)
                    667:                        if (0 == strcmp(fn, of->fname))
                    668:                                break;
                    669:
                    670:                if (NULL == of)
                    671:                        continue;
                    672:
                    673:                sseq = R_FIRST;
                    674:                while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) {
                    675:                        sseq = R_NEXT;
1.9       kristaps  676:                        assert(sizeof(struct db_val) == val.size);
                    677:                        vbuf = val.data;
                    678:                        if (*maxrec != vbuf->rec)
1.3       kristaps  679:                                continue;
1.5       kristaps  680:                        if (verb)
                    681:                                printf("%s: Deleted keyword: %s\n",
                    682:                                                fn, (char *)key.data);
1.3       kristaps  683:                        ch = (*db->del)(db, &key, R_CURSOR);
                    684:                        if (ch < 0)
                    685:                                break;
                    686:                }
                    687:                if (ch < 0) {
                    688:                        perror(dbf);
                    689:                        exit((int)MANDOCLEVEL_SYSERR);
                    690:                }
1.1       kristaps  691:
1.5       kristaps  692:                if (verb)
                    693:                        printf("%s: Deleted index\n", fn);
1.1       kristaps  694:
1.3       kristaps  695:                val.size = 0;
                    696:                ch = (*idx->put)(idx, &key, &val, R_CURSOR);
                    697:                if (ch < 0) {
                    698:                        perror(idxf);
                    699:                        exit((int)MANDOCLEVEL_SYSERR);
                    700:                }
1.1       kristaps  701:
1.3       kristaps  702:                if (reccur >= *recsz) {
                    703:                        *recsz += MANDOC_SLOP;
                    704:                        *recs = mandoc_realloc
                    705:                                (*recs, *recsz * sizeof(recno_t));
                    706:                }
1.1       kristaps  707:
1.3       kristaps  708:                (*recs)[(int)reccur] = *maxrec;
                    709:                reccur++;
                    710:        }
                    711:        (*maxrec)++;
1.1       kristaps  712: }
                    713:
                    714: /*
                    715:  * Grow the buffer (if necessary) and copy in a binary string.
                    716:  */
                    717: static void
                    718: buf_appendb(struct buf *buf, const void *cp, size_t sz)
                    719: {
                    720:
                    721:        /* Overshoot by MANDOC_BUFSZ. */
                    722:
                    723:        while (buf->len + sz >= buf->size) {
                    724:                buf->size = buf->len + sz + MANDOC_BUFSZ;
                    725:                buf->cp = mandoc_realloc(buf->cp, buf->size);
                    726:        }
                    727:
                    728:        memcpy(buf->cp + (int)buf->len, cp, sz);
                    729:        buf->len += sz;
                    730: }
                    731:
                    732: /*
                    733:  * Append a nil-terminated string to the buffer.
                    734:  * This can be invoked multiple times.
                    735:  * The buffer string will be nil-terminated.
                    736:  * If invoked multiple times, a space is put between strings.
                    737:  */
                    738: static void
                    739: buf_append(struct buf *buf, const char *cp)
                    740: {
                    741:        size_t           sz;
                    742:
                    743:        if (0 == (sz = strlen(cp)))
                    744:                return;
                    745:
                    746:        if (buf->len)
                    747:                buf->cp[(int)buf->len - 1] = ' ';
                    748:
                    749:        buf_appendb(buf, cp, sz + 1);
                    750: }
                    751:
                    752: /*
                    753:  * Recursively add all text from a given node.
                    754:  * This is optimised for general mdoc nodes in this context, which do
                    755:  * not consist of subexpressions and having a recursive call for n->next
                    756:  * would be wasteful.
                    757:  * The "f" variable should be 0 unless called from pmdoc_Nd for the
                    758:  * description buffer, which does not start at the beginning of the
                    759:  * buffer.
                    760:  */
                    761: static void
                    762: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
                    763: {
                    764:
                    765:        for ( ; n; n = n->next) {
                    766:                if (n->child)
                    767:                        buf_appendmdoc(buf, n->child, f);
                    768:
                    769:                if (MDOC_TEXT == n->type && f) {
                    770:                        f = 0;
                    771:                        buf_appendb(buf, n->string,
                    772:                                        strlen(n->string) + 1);
                    773:                } else if (MDOC_TEXT == n->type)
                    774:                        buf_append(buf, n->string);
                    775:
                    776:        }
                    777: }
                    778:
                    779: /* ARGSUSED */
                    780: static void
                    781: pmdoc_An(MDOC_ARGS)
                    782: {
                    783:
                    784:        if (SEC_AUTHORS != n->sec)
                    785:                return;
                    786:
                    787:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze  788:        hash_put(hash, buf, TYPE_An);
1.1       kristaps  789: }
                    790:
                    791: static void
                    792: hash_reset(DB **db)
                    793: {
                    794:        DB              *hash;
                    795:
                    796:        if (NULL != (hash = *db))
                    797:                (*hash->close)(hash);
                    798:
1.5       kristaps  799:        *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1.1       kristaps  800:        if (NULL == *db) {
                    801:                perror("hash");
                    802:                exit((int)MANDOCLEVEL_SYSERR);
                    803:        }
                    804: }
                    805:
                    806: /* ARGSUSED */
                    807: static void
                    808: pmdoc_Fd(MDOC_ARGS)
                    809: {
                    810:        const char      *start, *end;
                    811:        size_t           sz;
                    812:
                    813:        if (SEC_SYNOPSIS != n->sec)
                    814:                return;
                    815:        if (NULL == (n = n->child) || MDOC_TEXT != n->type)
                    816:                return;
                    817:
                    818:        /*
                    819:         * Only consider those `Fd' macro fields that begin with an
                    820:         * "inclusion" token (versus, e.g., #define).
                    821:         */
                    822:        if (strcmp("#include", n->string))
                    823:                return;
                    824:
                    825:        if (NULL == (n = n->next) || MDOC_TEXT != n->type)
                    826:                return;
                    827:
                    828:        /*
                    829:         * Strip away the enclosing angle brackets and make sure we're
                    830:         * not zero-length.
                    831:         */
                    832:
                    833:        start = n->string;
                    834:        if ('<' == *start || '"' == *start)
                    835:                start++;
                    836:
                    837:        if (0 == (sz = strlen(start)))
                    838:                return;
                    839:
                    840:        end = &start[(int)sz - 1];
                    841:        if ('>' == *end || '"' == *end)
                    842:                end--;
                    843:
                    844:        assert(end >= start);
                    845:
                    846:        buf_appendb(buf, start, (size_t)(end - start + 1));
                    847:        buf_appendb(buf, "", 1);
                    848:
1.8       schwarze  849:        hash_put(hash, buf, TYPE_In);
1.1       kristaps  850: }
                    851:
                    852: /* ARGSUSED */
                    853: static void
                    854: pmdoc_Cd(MDOC_ARGS)
                    855: {
                    856:
                    857:        if (SEC_SYNOPSIS != n->sec)
                    858:                return;
                    859:
                    860:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze  861:        hash_put(hash, buf, TYPE_Cd);
1.1       kristaps  862: }
                    863:
                    864: /* ARGSUSED */
                    865: static void
                    866: pmdoc_In(MDOC_ARGS)
                    867: {
                    868:
                    869:        if (SEC_SYNOPSIS != n->sec)
                    870:                return;
                    871:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    872:                return;
                    873:
                    874:        buf_append(buf, n->child->string);
1.8       schwarze  875:        hash_put(hash, buf, TYPE_In);
1.1       kristaps  876: }
                    877:
                    878: /* ARGSUSED */
                    879: static void
                    880: pmdoc_Fn(MDOC_ARGS)
                    881: {
                    882:        const char      *cp;
                    883:
                    884:        if (SEC_SYNOPSIS != n->sec)
                    885:                return;
                    886:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    887:                return;
                    888:
                    889:        /* .Fn "struct type *arg" "foo" */
                    890:
                    891:        cp = strrchr(n->child->string, ' ');
                    892:        if (NULL == cp)
                    893:                cp = n->child->string;
                    894:
                    895:        /* Strip away pointer symbol. */
                    896:
                    897:        while ('*' == *cp)
                    898:                cp++;
                    899:
                    900:        buf_append(buf, cp);
1.8       schwarze  901:        hash_put(hash, buf, TYPE_Fn);
1.1       kristaps  902: }
                    903:
                    904: /* ARGSUSED */
                    905: static void
                    906: pmdoc_St(MDOC_ARGS)
                    907: {
                    908:
                    909:        if (SEC_STANDARDS != n->sec)
                    910:                return;
                    911:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    912:                return;
                    913:
                    914:        buf_append(buf, n->child->string);
1.8       schwarze  915:        hash_put(hash, buf, TYPE_St);
1.1       kristaps  916: }
                    917:
                    918: /* ARGSUSED */
                    919: static void
                    920: pmdoc_Xr(MDOC_ARGS)
                    921: {
                    922:
                    923:        if (NULL == (n = n->child))
                    924:                return;
                    925:
                    926:        buf_appendb(buf, n->string, strlen(n->string));
                    927:
                    928:        if (NULL != (n = n->next)) {
                    929:                buf_appendb(buf, ".", 1);
                    930:                buf_appendb(buf, n->string, strlen(n->string) + 1);
                    931:        } else
                    932:                buf_appendb(buf, ".", 2);
                    933:
1.8       schwarze  934:        hash_put(hash, buf, TYPE_Xr);
1.1       kristaps  935: }
                    936:
                    937: /* ARGSUSED */
                    938: static void
                    939: pmdoc_Vt(MDOC_ARGS)
                    940: {
                    941:        const char      *start;
                    942:        size_t           sz;
                    943:
                    944:        if (SEC_SYNOPSIS != n->sec)
                    945:                return;
                    946:        if (MDOC_Vt == n->tok && MDOC_BODY != n->type)
                    947:                return;
                    948:        if (NULL == n->last || MDOC_TEXT != n->last->type)
                    949:                return;
                    950:
                    951:        /*
                    952:         * Strip away leading pointer symbol '*' and trailing ';'.
                    953:         */
                    954:
                    955:        start = n->last->string;
                    956:
                    957:        while ('*' == *start)
                    958:                start++;
                    959:
                    960:        if (0 == (sz = strlen(start)))
                    961:                return;
                    962:
                    963:        if (';' == start[(int)sz - 1])
                    964:                sz--;
                    965:
                    966:        if (0 == sz)
                    967:                return;
                    968:
                    969:        buf_appendb(buf, start, sz);
                    970:        buf_appendb(buf, "", 1);
1.8       schwarze  971:        hash_put(hash, buf, TYPE_Va);
1.1       kristaps  972: }
                    973:
                    974: /* ARGSUSED */
                    975: static void
                    976: pmdoc_Fo(MDOC_ARGS)
                    977: {
                    978:
                    979:        if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                    980:                return;
                    981:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    982:                return;
                    983:
                    984:        buf_append(buf, n->child->string);
1.8       schwarze  985:        hash_put(hash, buf, TYPE_Fn);
1.1       kristaps  986: }
                    987:
                    988:
                    989: /* ARGSUSED */
                    990: static void
                    991: pmdoc_Nd(MDOC_ARGS)
                    992: {
                    993:
                    994:        if (MDOC_BODY != n->type)
                    995:                return;
                    996:
                    997:        buf_appendmdoc(dbuf, n->child, 1);
                    998:        buf_appendmdoc(buf, n->child, 0);
                    999:
1.8       schwarze 1000:        hash_put(hash, buf, TYPE_Nd);
1.1       kristaps 1001: }
                   1002:
                   1003: /* ARGSUSED */
                   1004: static void
                   1005: pmdoc_Er(MDOC_ARGS)
                   1006: {
                   1007:
                   1008:        if (SEC_ERRORS != n->sec)
                   1009:                return;
                   1010:
                   1011:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1012:        hash_put(hash, buf, TYPE_Er);
1.1       kristaps 1013: }
                   1014:
                   1015: /* ARGSUSED */
                   1016: static void
                   1017: pmdoc_Ev(MDOC_ARGS)
                   1018: {
                   1019:
                   1020:        if (SEC_ENVIRONMENT != n->sec)
                   1021:                return;
                   1022:
                   1023:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1024:        hash_put(hash, buf, TYPE_Ev);
1.1       kristaps 1025: }
                   1026:
                   1027: /* ARGSUSED */
                   1028: static void
                   1029: pmdoc_Pa(MDOC_ARGS)
                   1030: {
                   1031:
                   1032:        if (SEC_FILES != n->sec)
                   1033:                return;
                   1034:
                   1035:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1036:        hash_put(hash, buf, TYPE_Pa);
1.1       kristaps 1037: }
                   1038:
                   1039: /* ARGSUSED */
                   1040: static void
                   1041: pmdoc_Nm(MDOC_ARGS)
                   1042: {
                   1043:
                   1044:        if (SEC_NAME == n->sec) {
                   1045:                buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1046:                hash_put(hash, buf, TYPE_Nm);
1.1       kristaps 1047:                return;
                   1048:        } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1049:                return;
                   1050:
                   1051:        if (NULL == n->child)
                   1052:                buf_append(buf, m->name);
                   1053:
                   1054:        buf_appendmdoc(buf, n->child, 0);
1.8       schwarze 1055:        hash_put(hash, buf, TYPE_Nm);
1.1       kristaps 1056: }
                   1057:
                   1058: static void
1.9       kristaps 1059: hash_put(DB *db, const struct buf *buf, uint64_t mask)
1.1       kristaps 1060: {
                   1061:        DBT              key, val;
                   1062:        int              rc;
                   1063:
                   1064:        if (buf->len < 2)
                   1065:                return;
                   1066:
                   1067:        key.data = buf->cp;
                   1068:        key.size = buf->len;
                   1069:
                   1070:        if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
                   1071:                perror("hash");
                   1072:                exit((int)MANDOCLEVEL_SYSERR);
                   1073:        } else if (0 == rc)
1.9       kristaps 1074:                mask |= *(uint64_t *)val.data;
1.1       kristaps 1075:
                   1076:        val.data = &mask;
1.9       kristaps 1077:        val.size = sizeof(uint64_t);
1.1       kristaps 1078:
                   1079:        if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
                   1080:                perror("hash");
                   1081:                exit((int)MANDOCLEVEL_SYSERR);
                   1082:        }
                   1083: }
                   1084:
                   1085: static void
                   1086: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
                   1087: {
                   1088:
                   1089:        assert(key->size);
                   1090:        assert(val->size);
                   1091:
                   1092:        if (0 == (*db->put)(db, key, val, 0))
                   1093:                return;
                   1094:
                   1095:        perror(dbn);
                   1096:        exit((int)MANDOCLEVEL_SYSERR);
                   1097:        /* NOTREACHED */
                   1098: }
                   1099:
                   1100: /*
                   1101:  * Call out to per-macro handlers after clearing the persistent database
                   1102:  * key.  If the macro sets the database key, flush it to the database.
                   1103:  */
                   1104: static void
                   1105: pmdoc_node(MDOC_ARGS)
                   1106: {
                   1107:
                   1108:        if (NULL == n)
                   1109:                return;
                   1110:
                   1111:        switch (n->type) {
                   1112:        case (MDOC_HEAD):
                   1113:                /* FALLTHROUGH */
                   1114:        case (MDOC_BODY):
                   1115:                /* FALLTHROUGH */
                   1116:        case (MDOC_TAIL):
                   1117:                /* FALLTHROUGH */
                   1118:        case (MDOC_BLOCK):
                   1119:                /* FALLTHROUGH */
                   1120:        case (MDOC_ELEM):
                   1121:                if (NULL == mdocs[n->tok])
                   1122:                        break;
                   1123:
                   1124:                buf->len = 0;
                   1125:                (*mdocs[n->tok])(hash, buf, dbuf, n, m);
                   1126:                break;
                   1127:        default:
                   1128:                break;
                   1129:        }
                   1130:
                   1131:        pmdoc_node(hash, buf, dbuf, n->child, m);
                   1132:        pmdoc_node(hash, buf, dbuf, n->next, m);
                   1133: }
                   1134:
                   1135: static int
                   1136: pman_node(MAN_ARGS)
                   1137: {
                   1138:        const struct man_node *head, *body;
                   1139:        const char      *start, *sv;
                   1140:        size_t           sz;
                   1141:
                   1142:        if (NULL == n)
                   1143:                return(0);
                   1144:
                   1145:        /*
                   1146:         * We're only searching for one thing: the first text child in
                   1147:         * the BODY of a NAME section.  Since we don't keep track of
                   1148:         * sections in -man, run some hoops to find out whether we're in
                   1149:         * the correct section or not.
                   1150:         */
                   1151:
                   1152:        if (MAN_BODY == n->type && MAN_SH == n->tok) {
                   1153:                body = n;
                   1154:                assert(body->parent);
                   1155:                if (NULL != (head = body->parent->head) &&
                   1156:                                1 == head->nchild &&
                   1157:                                NULL != (head = (head->child)) &&
                   1158:                                MAN_TEXT == head->type &&
                   1159:                                0 == strcmp(head->string, "NAME") &&
                   1160:                                NULL != (body = body->child) &&
                   1161:                                MAN_TEXT == body->type) {
                   1162:
                   1163:                        assert(body->string);
                   1164:                        start = sv = body->string;
                   1165:
                   1166:                        /*
                   1167:                         * Go through a special heuristic dance here.
                   1168:                         * This is why -man manuals are great!
                   1169:                         * (I'm being sarcastic: my eyes are bleeding.)
                   1170:                         * Conventionally, one or more manual names are
                   1171:                         * comma-specified prior to a whitespace, then a
                   1172:                         * dash, then a description.  Try to puzzle out
                   1173:                         * the name parts here.
                   1174:                         */
                   1175:
                   1176:                        for ( ;; ) {
                   1177:                                sz = strcspn(start, " ,");
                   1178:                                if ('\0' == start[(int)sz])
                   1179:                                        break;
                   1180:
                   1181:                                buf->len = 0;
                   1182:                                buf_appendb(buf, start, sz);
                   1183:                                buf_appendb(buf, "", 1);
                   1184:
1.8       schwarze 1185:                                hash_put(hash, buf, TYPE_Nm);
1.1       kristaps 1186:
                   1187:                                if (' ' == start[(int)sz]) {
                   1188:                                        start += (int)sz + 1;
                   1189:                                        break;
                   1190:                                }
                   1191:
                   1192:                                assert(',' == start[(int)sz]);
                   1193:                                start += (int)sz + 1;
                   1194:                                while (' ' == *start)
                   1195:                                        start++;
                   1196:                        }
                   1197:
                   1198:                        buf->len = 0;
                   1199:
                   1200:                        if (sv == start) {
                   1201:                                buf_append(buf, start);
                   1202:                                return(1);
                   1203:                        }
                   1204:
                   1205:                        while (' ' == *start)
                   1206:                                start++;
                   1207:
                   1208:                        if (0 == strncmp(start, "-", 1))
                   1209:                                start += 1;
                   1210:                        else if (0 == strncmp(start, "\\-", 2))
                   1211:                                start += 2;
                   1212:                        else if (0 == strncmp(start, "\\(en", 4))
                   1213:                                start += 4;
                   1214:                        else if (0 == strncmp(start, "\\(em", 4))
                   1215:                                start += 4;
                   1216:
                   1217:                        while (' ' == *start)
                   1218:                                start++;
                   1219:
                   1220:                        sz = strlen(start) + 1;
                   1221:                        buf_appendb(dbuf, start, sz);
                   1222:                        buf_appendb(buf, start, sz);
                   1223:
1.8       schwarze 1224:                        hash_put(hash, buf, TYPE_Nd);
1.1       kristaps 1225:                }
                   1226:        }
                   1227:
1.7       schwarze 1228:        for (n = n->child; n; n = n->next)
                   1229:                if (pman_node(hash, buf, dbuf, n))
                   1230:                        return(1);
1.1       kristaps 1231:
                   1232:        return(0);
                   1233: }
                   1234:
1.5       kristaps 1235: static void
1.12      schwarze 1236: ofile_argbuild(char *argv[], int argc, int use_all, int verb,
                   1237:                struct of **of)
1.5       kristaps 1238: {
1.12      schwarze 1239:        char             buf[MAXPATHLEN];
                   1240:        char            *sec, *arch, *title, *p;
1.5       kristaps 1241:        int              i;
                   1242:        struct of       *nof;
                   1243:
                   1244:        for (i = 0; i < argc; i++) {
1.12      schwarze 1245:
                   1246:                /*
                   1247:                 * Try to infer the manual section, architecture and
                   1248:                 * page title from the path, assuming it looks like
                   1249:                 *   man*[/<arch>]/<title>.<section>
                   1250:                 */
                   1251:
                   1252:                if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {
                   1253:                        fprintf(stderr, "%s: Path too long\n", argv[i]);
                   1254:                        continue;
                   1255:                }
                   1256:                sec = arch = title = NULL;
                   1257:                p = strrchr(buf, '\0');
                   1258:                while (p-- > buf) {
                   1259:                        if (NULL == sec && '.' == *p) {
                   1260:                                sec = p + 1;
                   1261:                                *p = '\0';
                   1262:                                continue;
                   1263:                        }
                   1264:                        if ('/' != *p)
                   1265:                                continue;
                   1266:                        if (NULL == title) {
                   1267:                                title = p + 1;
                   1268:                                *p = '\0';
                   1269:                                continue;
                   1270:                        }
                   1271:                        if (strncmp("man", p + 1, 3))
                   1272:                                arch = p + 1;
                   1273:                        break;
                   1274:                }
                   1275:                if (NULL == title)
                   1276:                        title = buf;
                   1277:
                   1278:                /*
                   1279:                 * Build the file structure.
                   1280:                 */
                   1281:
1.5       kristaps 1282:                nof = mandoc_calloc(1, sizeof(struct of));
1.12      schwarze 1283:                nof->fname = mandoc_strdup(argv[i]);
                   1284:                if (NULL != sec)
                   1285:                        nof->sec = mandoc_strdup(sec);
                   1286:                if (NULL != arch)
                   1287:                        nof->arch = mandoc_strdup(arch);
                   1288:                nof->title = mandoc_strdup(title);
                   1289:
                   1290:                /*
                   1291:                 * Add the structure to the list.
                   1292:                 */
                   1293:
1.5       kristaps 1294:                if (verb > 2)
                   1295:                        printf("%s: Scheduling\n", argv[i]);
                   1296:                if (NULL == *of) {
                   1297:                        *of = nof;
                   1298:                        (*of)->first = nof;
                   1299:                } else {
                   1300:                        nof->first = (*of)->first;
                   1301:                        (*of)->next = nof;
                   1302:                        *of = nof;
                   1303:                }
                   1304:        }
                   1305: }
                   1306:
1.4       kristaps 1307: /*
                   1308:  * Recursively build up a list of files to parse.
                   1309:  * We use this instead of ftw() and so on because I don't want global
                   1310:  * variables hanging around.
                   1311:  * This ignores the mandoc.db and mandoc.index files, but assumes that
                   1312:  * everything else is a manual.
                   1313:  * Pass in a pointer to a NULL structure for the first invocation.
                   1314:  */
                   1315: static int
1.12      schwarze 1316: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
                   1317:                int use_all, int verb, struct of **of)
1.4       kristaps 1318: {
1.5       kristaps 1319:        char             buf[MAXPATHLEN];
                   1320:        size_t           sz;
1.4       kristaps 1321:        DIR             *d;
1.12      schwarze 1322:        const char      *fn, *sec, *arch;
                   1323:        char            *suffix;
1.4       kristaps 1324:        struct of       *nof;
                   1325:        struct dirent   *dp;
                   1326:
                   1327:        if (NULL == (d = opendir(dir))) {
                   1328:                perror(dir);
                   1329:                return(0);
                   1330:        }
                   1331:
                   1332:        while (NULL != (dp = readdir(d))) {
                   1333:                fn = dp->d_name;
1.12      schwarze 1334:
                   1335:                if ('.' == *fn)
                   1336:                        continue;
                   1337:
1.4       kristaps 1338:                if (DT_DIR == dp->d_type) {
1.12      schwarze 1339:                        sec = psec;
                   1340:                        arch = parch;
                   1341:
                   1342:                        /*
                   1343:                         * By default, only use directories called:
                   1344:                         *   man<section>/[<arch>/]
                   1345:                         */
                   1346:
                   1347:                        if (NULL == sec) {
                   1348:                                if(0 == strncmp("man", fn, 3))
                   1349:                                        sec = fn + 3;
                   1350:                                else if (use_all)
                   1351:                                        sec = fn;
                   1352:                                else
                   1353:                                        continue;
                   1354:                        } else if (NULL == arch && (use_all ||
                   1355:                                        NULL == strchr(fn, '.')))
                   1356:                                arch = fn;
                   1357:                        else if (0 == use_all)
1.5       kristaps 1358:                                continue;
                   1359:
                   1360:                        buf[0] = '\0';
                   1361:                        strlcat(buf, dir, MAXPATHLEN);
                   1362:                        strlcat(buf, "/", MAXPATHLEN);
                   1363:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1364:
1.12      schwarze 1365:                        if (MAXPATHLEN <= sz) {
                   1366:                                fprintf(stderr, "%s: Path too long\n", dir);
                   1367:                                return(0);
                   1368:                        }
                   1369:
                   1370:                        if (verb > 2)
                   1371:                                printf("%s: Scanning\n", buf);
                   1372:
                   1373:                        if ( ! ofile_dirbuild(buf, sec, arch,
                   1374:                                        use_all, verb, of))
                   1375:                                return(0);
                   1376:                }
                   1377:                if (DT_REG != dp->d_type ||
                   1378:                    (NULL == psec && !use_all) ||
                   1379:                    !strcmp(MANDOC_DB, fn) ||
                   1380:                    !strcmp(MANDOC_IDX, fn))
                   1381:                        continue;
                   1382:
                   1383:                /*
                   1384:                 * By default, skip files where the file name suffix
                   1385:                 * does not agree with the section directory
                   1386:                 * they are located in.
                   1387:                 */
                   1388:
                   1389:                suffix = strrchr(fn, '.');
                   1390:                if (0 == use_all) {
                   1391:                        if (NULL == suffix)
1.5       kristaps 1392:                                continue;
1.12      schwarze 1393:                        if (strcmp(suffix + 1, psec))
1.5       kristaps 1394:                                continue;
                   1395:                }
1.4       kristaps 1396:
1.5       kristaps 1397:                buf[0] = '\0';
                   1398:                strlcat(buf, dir, MAXPATHLEN);
                   1399:                strlcat(buf, "/", MAXPATHLEN);
1.6       schwarze 1400:                sz = strlcat(buf, fn, MAXPATHLEN);
1.5       kristaps 1401:                if (sz >= MAXPATHLEN) {
                   1402:                        fprintf(stderr, "%s: Path too long\n", dir);
                   1403:                        return(0);
                   1404:                }
                   1405:
1.4       kristaps 1406:                nof = mandoc_calloc(1, sizeof(struct of));
1.5       kristaps 1407:                nof->fname = mandoc_strdup(buf);
1.12      schwarze 1408:                if (NULL != psec)
                   1409:                        nof->sec = mandoc_strdup(psec);
                   1410:                if (NULL != parch)
                   1411:                        nof->arch = mandoc_strdup(parch);
                   1412:
                   1413:                /*
                   1414:                 * Remember the file name without the extension,
                   1415:                 * to be used as the page title in the database.
                   1416:                 */
                   1417:
                   1418:                if (NULL != suffix)
                   1419:                        *suffix = '\0';
                   1420:                nof->title = mandoc_strdup(fn);
1.5       kristaps 1421:
                   1422:                if (verb > 2)
                   1423:                        printf("%s: Scheduling\n", buf);
1.4       kristaps 1424:
                   1425:                if (NULL == *of) {
                   1426:                        *of = nof;
                   1427:                        (*of)->first = nof;
                   1428:                } else {
1.5       kristaps 1429:                        nof->first = (*of)->first;
1.4       kristaps 1430:                        (*of)->next = nof;
                   1431:                        *of = nof;
                   1432:                }
                   1433:        }
                   1434:
1.7       schwarze 1435:        closedir(d);
1.4       kristaps 1436:        return(1);
                   1437: }
                   1438:
                   1439: static void
                   1440: ofile_free(struct of *of)
                   1441: {
                   1442:        struct of       *nof;
                   1443:
                   1444:        while (of) {
                   1445:                nof = of->next;
                   1446:                free(of->fname);
1.12      schwarze 1447:                free(of->sec);
                   1448:                free(of->arch);
                   1449:                free(of->title);
1.4       kristaps 1450:                free(of);
                   1451:                of = nof;
                   1452:        }
                   1453: }
                   1454:
1.1       kristaps 1455: static void
                   1456: usage(void)
                   1457: {
                   1458:
1.5       kristaps 1459:        fprintf(stderr, "usage: %s [-v] "
                   1460:                        "[-d dir [files...] |"
                   1461:                        " -u dir [files...] |"
                   1462:                        " dir...]\n", progname);
1.1       kristaps 1463: }

CVSweb