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

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

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