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

Annotation of mandoc/mandocdb.c, Revision 1.16

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

CVSweb