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

Annotation of mandoc/mandocdb.c, Revision 1.15

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

CVSweb