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

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

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