[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.16

1.16    ! schwarze    1: /*     $Id: mandocdb.c,v 1.15 2011/11/29 22:30:56 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:
1.2       schwarze  629:                        dbt_put(db, dbf, &key, &val);
1.1       schwarze  630:                }
                    631:                if (ch < 0) {
                    632:                        perror("hash");
                    633:                        exit((int)MANDOCLEVEL_SYSERR);
                    634:                }
                    635:
                    636:                /*
                    637:                 * Apply to the index.  If we haven't had a description
                    638:                 * set, put an empty one in now.
                    639:                 */
                    640:
1.2       schwarze  641:                if (dbuf->len == sv)
                    642:                        buf_appendb(dbuf, "", 1);
1.1       schwarze  643:
                    644:                key.data = &rec;
                    645:                key.size = sizeof(recno_t);
                    646:
1.2       schwarze  647:                val.data = dbuf->cp;
                    648:                val.size = dbuf->len;
1.1       schwarze  649:
1.2       schwarze  650:                if (verb)
1.1       schwarze  651:                        printf("%s: Added index\n", fn);
1.16    ! schwarze  652:
1.2       schwarze  653:                dbt_put(idx, idxf, &key, &val);
                    654:        }
                    655: }
                    656:
                    657: /*
                    658:  * Scan through all entries in the index file `idx' and prune those
                    659:  * entries in `ofile'.
                    660:  * Pruning consists of removing from `db', then invalidating the entry
                    661:  * in `idx' (zeroing its value size).
                    662:  */
                    663: static void
                    664: index_prune(const struct of *ofile, DB *db, const char *dbf,
1.13      schwarze  665:                DB *idx, const char *idxf,
1.2       schwarze  666:                recno_t *maxrec, recno_t **recs, size_t *recsz)
                    667: {
                    668:        const struct of *of;
1.16    ! schwarze  669:        const char      *fn, *cp;
1.9       schwarze  670:        struct db_val   *vbuf;
1.2       schwarze  671:        unsigned         seq, sseq;
                    672:        DBT              key, val;
                    673:        size_t           reccur;
                    674:        int              ch;
                    675:
                    676:        reccur = 0;
                    677:        seq = R_FIRST;
                    678:        while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
                    679:                seq = R_NEXT;
                    680:                *maxrec = *(recno_t *)key.data;
1.16    ! schwarze  681:                cp = val.data;
        !           682:
        !           683:                /* Deleted records are zero-sized.  Skip them. */
        !           684:
        !           685:                if (0 == val.size)
        !           686:                        goto cont;
        !           687:
        !           688:                /*
        !           689:                 * Make sure we're sane.
        !           690:                 * Read past our mdoc/man/cat type to the next string,
        !           691:                 * then make sure it's bounded by a NUL.
        !           692:                 * Failing any of these, we go into our error handler.
        !           693:                 */
        !           694:
        !           695:                if (NULL == (fn = memchr(cp, '\0', val.size)))
        !           696:                        break;
        !           697:                if (++fn - cp >= (int)val.size)
        !           698:                        break;
        !           699:                if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
        !           700:                        break;
        !           701:
        !           702:                /*
        !           703:                 * Search for the file in those we care about.
        !           704:                 * XXX: build this into a tree.  Too slow.
        !           705:                 */
1.2       schwarze  706:
                    707:                for (of = ofile; of; of = of->next)
                    708:                        if (0 == strcmp(fn, of->fname))
                    709:                                break;
                    710:
                    711:                if (NULL == of)
                    712:                        continue;
                    713:
1.16    ! schwarze  714:                /*
        !           715:                 * Search through the keyword database, throwing out all
        !           716:                 * references to our file.
        !           717:                 */
        !           718:
1.2       schwarze  719:                sseq = R_FIRST;
                    720:                while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) {
                    721:                        sseq = R_NEXT;
1.16    ! schwarze  722:                        if (sizeof(struct db_val) != val.size)
        !           723:                                break;
        !           724:
1.9       schwarze  725:                        vbuf = val.data;
                    726:                        if (*maxrec != vbuf->rec)
1.2       schwarze  727:                                continue;
1.16    ! schwarze  728:
        !           729:                        if ((ch = (*db->del)(db, &key, R_CURSOR)) < 0)
1.2       schwarze  730:                                break;
                    731:                }
1.16    ! schwarze  732:
1.2       schwarze  733:                if (ch < 0) {
                    734:                        perror(dbf);
                    735:                        exit((int)MANDOCLEVEL_SYSERR);
1.16    ! schwarze  736:                } else if (1 != ch) {
        !           737:                        fprintf(stderr, "%s: Corrupt database\n", dbf);
        !           738:                        exit((int)MANDOCLEVEL_SYSERR);
1.2       schwarze  739:                }
1.1       schwarze  740:
1.2       schwarze  741:                if (verb)
                    742:                        printf("%s: Deleted index\n", fn);
1.1       schwarze  743:
1.2       schwarze  744:                val.size = 0;
                    745:                ch = (*idx->put)(idx, &key, &val, R_CURSOR);
1.1       schwarze  746:
1.16    ! schwarze  747:                if (ch < 0)
        !           748:                        break;
        !           749: cont:
1.2       schwarze  750:                if (reccur >= *recsz) {
                    751:                        *recsz += MANDOC_SLOP;
                    752:                        *recs = mandoc_realloc
                    753:                                (*recs, *recsz * sizeof(recno_t));
                    754:                }
1.1       schwarze  755:
1.2       schwarze  756:                (*recs)[(int)reccur] = *maxrec;
                    757:                reccur++;
                    758:        }
1.16    ! schwarze  759:
        !           760:        if (ch < 0) {
        !           761:                perror(idxf);
        !           762:                exit((int)MANDOCLEVEL_SYSERR);
        !           763:        } else if (1 != ch) {
        !           764:                fprintf(stderr, "%s: Corrupt index\n", idxf);
        !           765:                exit((int)MANDOCLEVEL_SYSERR);
        !           766:        }
        !           767:
1.2       schwarze  768:        (*maxrec)++;
1.1       schwarze  769: }
                    770:
                    771: /*
                    772:  * Grow the buffer (if necessary) and copy in a binary string.
                    773:  */
                    774: static void
                    775: buf_appendb(struct buf *buf, const void *cp, size_t sz)
                    776: {
                    777:
                    778:        /* Overshoot by MANDOC_BUFSZ. */
                    779:
                    780:        while (buf->len + sz >= buf->size) {
                    781:                buf->size = buf->len + sz + MANDOC_BUFSZ;
                    782:                buf->cp = mandoc_realloc(buf->cp, buf->size);
                    783:        }
                    784:
                    785:        memcpy(buf->cp + (int)buf->len, cp, sz);
                    786:        buf->len += sz;
                    787: }
                    788:
                    789: /*
                    790:  * Append a nil-terminated string to the buffer.
                    791:  * This can be invoked multiple times.
                    792:  * The buffer string will be nil-terminated.
                    793:  * If invoked multiple times, a space is put between strings.
                    794:  */
                    795: static void
                    796: buf_append(struct buf *buf, const char *cp)
                    797: {
                    798:        size_t           sz;
                    799:
                    800:        if (0 == (sz = strlen(cp)))
                    801:                return;
                    802:
                    803:        if (buf->len)
                    804:                buf->cp[(int)buf->len - 1] = ' ';
                    805:
                    806:        buf_appendb(buf, cp, sz + 1);
                    807: }
                    808:
                    809: /*
                    810:  * Recursively add all text from a given node.
                    811:  * This is optimised for general mdoc nodes in this context, which do
                    812:  * not consist of subexpressions and having a recursive call for n->next
                    813:  * would be wasteful.
                    814:  * The "f" variable should be 0 unless called from pmdoc_Nd for the
                    815:  * description buffer, which does not start at the beginning of the
                    816:  * buffer.
                    817:  */
                    818: static void
                    819: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
                    820: {
                    821:
                    822:        for ( ; n; n = n->next) {
                    823:                if (n->child)
                    824:                        buf_appendmdoc(buf, n->child, f);
                    825:
                    826:                if (MDOC_TEXT == n->type && f) {
                    827:                        f = 0;
                    828:                        buf_appendb(buf, n->string,
                    829:                                        strlen(n->string) + 1);
                    830:                } else if (MDOC_TEXT == n->type)
                    831:                        buf_append(buf, n->string);
                    832:
                    833:        }
                    834: }
                    835:
                    836: /* ARGSUSED */
                    837: static void
                    838: pmdoc_An(MDOC_ARGS)
                    839: {
                    840:
                    841:        if (SEC_AUTHORS != n->sec)
                    842:                return;
                    843:
                    844:        buf_appendmdoc(buf, n->child, 0);
1.5       schwarze  845:        hash_put(hash, buf, TYPE_An);
1.1       schwarze  846: }
                    847:
                    848: static void
                    849: hash_reset(DB **db)
                    850: {
                    851:        DB              *hash;
                    852:
                    853:        if (NULL != (hash = *db))
                    854:                (*hash->close)(hash);
                    855:
1.2       schwarze  856:        *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1.1       schwarze  857:        if (NULL == *db) {
                    858:                perror("hash");
                    859:                exit((int)MANDOCLEVEL_SYSERR);
                    860:        }
                    861: }
                    862:
                    863: /* ARGSUSED */
                    864: static void
                    865: pmdoc_Fd(MDOC_ARGS)
                    866: {
                    867:        const char      *start, *end;
                    868:        size_t           sz;
                    869:
                    870:        if (SEC_SYNOPSIS != n->sec)
                    871:                return;
                    872:        if (NULL == (n = n->child) || MDOC_TEXT != n->type)
                    873:                return;
                    874:
                    875:        /*
                    876:         * Only consider those `Fd' macro fields that begin with an
                    877:         * "inclusion" token (versus, e.g., #define).
                    878:         */
                    879:        if (strcmp("#include", n->string))
                    880:                return;
                    881:
                    882:        if (NULL == (n = n->next) || MDOC_TEXT != n->type)
                    883:                return;
                    884:
                    885:        /*
                    886:         * Strip away the enclosing angle brackets and make sure we're
                    887:         * not zero-length.
                    888:         */
                    889:
                    890:        start = n->string;
                    891:        if ('<' == *start || '"' == *start)
                    892:                start++;
                    893:
                    894:        if (0 == (sz = strlen(start)))
                    895:                return;
                    896:
                    897:        end = &start[(int)sz - 1];
                    898:        if ('>' == *end || '"' == *end)
                    899:                end--;
                    900:
                    901:        assert(end >= start);
                    902:
                    903:        buf_appendb(buf, start, (size_t)(end - start + 1));
                    904:        buf_appendb(buf, "", 1);
                    905:
1.5       schwarze  906:        hash_put(hash, buf, TYPE_In);
1.1       schwarze  907: }
                    908:
                    909: /* ARGSUSED */
                    910: static void
                    911: pmdoc_Cd(MDOC_ARGS)
                    912: {
                    913:
                    914:        if (SEC_SYNOPSIS != n->sec)
                    915:                return;
                    916:
                    917:        buf_appendmdoc(buf, n->child, 0);
1.5       schwarze  918:        hash_put(hash, buf, TYPE_Cd);
1.1       schwarze  919: }
                    920:
                    921: /* ARGSUSED */
                    922: static void
                    923: pmdoc_In(MDOC_ARGS)
                    924: {
                    925:
                    926:        if (SEC_SYNOPSIS != n->sec)
                    927:                return;
                    928:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    929:                return;
                    930:
                    931:        buf_append(buf, n->child->string);
1.5       schwarze  932:        hash_put(hash, buf, TYPE_In);
1.1       schwarze  933: }
                    934:
                    935: /* ARGSUSED */
                    936: static void
                    937: pmdoc_Fn(MDOC_ARGS)
                    938: {
                    939:        const char      *cp;
                    940:
                    941:        if (SEC_SYNOPSIS != n->sec)
                    942:                return;
                    943:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    944:                return;
                    945:
                    946:        /* .Fn "struct type *arg" "foo" */
                    947:
                    948:        cp = strrchr(n->child->string, ' ');
                    949:        if (NULL == cp)
                    950:                cp = n->child->string;
                    951:
                    952:        /* Strip away pointer symbol. */
                    953:
                    954:        while ('*' == *cp)
                    955:                cp++;
                    956:
                    957:        buf_append(buf, cp);
1.5       schwarze  958:        hash_put(hash, buf, TYPE_Fn);
1.1       schwarze  959: }
                    960:
                    961: /* ARGSUSED */
                    962: static void
                    963: pmdoc_St(MDOC_ARGS)
                    964: {
                    965:
                    966:        if (SEC_STANDARDS != n->sec)
                    967:                return;
                    968:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    969:                return;
                    970:
                    971:        buf_append(buf, n->child->string);
1.5       schwarze  972:        hash_put(hash, buf, TYPE_St);
1.1       schwarze  973: }
                    974:
                    975: /* ARGSUSED */
                    976: static void
                    977: pmdoc_Xr(MDOC_ARGS)
                    978: {
                    979:
                    980:        if (NULL == (n = n->child))
                    981:                return;
                    982:
                    983:        buf_appendb(buf, n->string, strlen(n->string));
                    984:
                    985:        if (NULL != (n = n->next)) {
                    986:                buf_appendb(buf, ".", 1);
                    987:                buf_appendb(buf, n->string, strlen(n->string) + 1);
                    988:        } else
                    989:                buf_appendb(buf, ".", 2);
                    990:
1.5       schwarze  991:        hash_put(hash, buf, TYPE_Xr);
1.1       schwarze  992: }
                    993:
                    994: /* ARGSUSED */
                    995: static void
                    996: pmdoc_Vt(MDOC_ARGS)
                    997: {
                    998:        const char      *start;
                    999:        size_t           sz;
                   1000:
                   1001:        if (SEC_SYNOPSIS != n->sec)
                   1002:                return;
                   1003:        if (MDOC_Vt == n->tok && MDOC_BODY != n->type)
                   1004:                return;
                   1005:        if (NULL == n->last || MDOC_TEXT != n->last->type)
                   1006:                return;
                   1007:
                   1008:        /*
                   1009:         * Strip away leading pointer symbol '*' and trailing ';'.
                   1010:         */
                   1011:
                   1012:        start = n->last->string;
                   1013:
                   1014:        while ('*' == *start)
                   1015:                start++;
                   1016:
                   1017:        if (0 == (sz = strlen(start)))
                   1018:                return;
                   1019:
                   1020:        if (';' == start[(int)sz - 1])
                   1021:                sz--;
                   1022:
                   1023:        if (0 == sz)
                   1024:                return;
                   1025:
                   1026:        buf_appendb(buf, start, sz);
                   1027:        buf_appendb(buf, "", 1);
1.5       schwarze 1028:        hash_put(hash, buf, TYPE_Va);
1.1       schwarze 1029: }
                   1030:
                   1031: /* ARGSUSED */
                   1032: static void
                   1033: pmdoc_Fo(MDOC_ARGS)
                   1034: {
                   1035:
                   1036:        if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1037:                return;
                   1038:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                   1039:                return;
                   1040:
                   1041:        buf_append(buf, n->child->string);
1.5       schwarze 1042:        hash_put(hash, buf, TYPE_Fn);
1.1       schwarze 1043: }
                   1044:
                   1045:
                   1046: /* ARGSUSED */
                   1047: static void
                   1048: pmdoc_Nd(MDOC_ARGS)
                   1049: {
                   1050:
                   1051:        if (MDOC_BODY != n->type)
                   1052:                return;
                   1053:
                   1054:        buf_appendmdoc(dbuf, n->child, 1);
                   1055:        buf_appendmdoc(buf, n->child, 0);
                   1056:
1.5       schwarze 1057:        hash_put(hash, buf, TYPE_Nd);
1.1       schwarze 1058: }
                   1059:
                   1060: /* ARGSUSED */
                   1061: static void
                   1062: pmdoc_Er(MDOC_ARGS)
                   1063: {
                   1064:
                   1065:        if (SEC_ERRORS != n->sec)
                   1066:                return;
                   1067:
                   1068:        buf_appendmdoc(buf, n->child, 0);
1.5       schwarze 1069:        hash_put(hash, buf, TYPE_Er);
1.1       schwarze 1070: }
                   1071:
                   1072: /* ARGSUSED */
                   1073: static void
                   1074: pmdoc_Ev(MDOC_ARGS)
                   1075: {
                   1076:
                   1077:        if (SEC_ENVIRONMENT != n->sec)
                   1078:                return;
                   1079:
                   1080:        buf_appendmdoc(buf, n->child, 0);
1.5       schwarze 1081:        hash_put(hash, buf, TYPE_Ev);
1.1       schwarze 1082: }
                   1083:
                   1084: /* ARGSUSED */
                   1085: static void
                   1086: pmdoc_Pa(MDOC_ARGS)
                   1087: {
                   1088:
                   1089:        if (SEC_FILES != n->sec)
                   1090:                return;
                   1091:
                   1092:        buf_appendmdoc(buf, n->child, 0);
1.5       schwarze 1093:        hash_put(hash, buf, TYPE_Pa);
1.1       schwarze 1094: }
                   1095:
                   1096: /* ARGSUSED */
                   1097: static void
                   1098: pmdoc_Nm(MDOC_ARGS)
                   1099: {
                   1100:
                   1101:        if (SEC_NAME == n->sec) {
                   1102:                buf_appendmdoc(buf, n->child, 0);
1.5       schwarze 1103:                hash_put(hash, buf, TYPE_Nm);
1.1       schwarze 1104:                return;
                   1105:        } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1106:                return;
                   1107:
                   1108:        if (NULL == n->child)
                   1109:                buf_append(buf, m->name);
                   1110:
                   1111:        buf_appendmdoc(buf, n->child, 0);
1.5       schwarze 1112:        hash_put(hash, buf, TYPE_Nm);
1.1       schwarze 1113: }
                   1114:
                   1115: static void
1.9       schwarze 1116: hash_put(DB *db, const struct buf *buf, uint64_t mask)
1.1       schwarze 1117: {
                   1118:        DBT              key, val;
                   1119:        int              rc;
                   1120:
                   1121:        if (buf->len < 2)
                   1122:                return;
                   1123:
                   1124:        key.data = buf->cp;
                   1125:        key.size = buf->len;
                   1126:
                   1127:        if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
                   1128:                perror("hash");
                   1129:                exit((int)MANDOCLEVEL_SYSERR);
                   1130:        } else if (0 == rc)
1.9       schwarze 1131:                mask |= *(uint64_t *)val.data;
1.1       schwarze 1132:
                   1133:        val.data = &mask;
1.9       schwarze 1134:        val.size = sizeof(uint64_t);
1.1       schwarze 1135:
                   1136:        if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
                   1137:                perror("hash");
                   1138:                exit((int)MANDOCLEVEL_SYSERR);
                   1139:        }
                   1140: }
                   1141:
                   1142: static void
                   1143: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
                   1144: {
                   1145:
                   1146:        assert(key->size);
                   1147:        assert(val->size);
                   1148:
                   1149:        if (0 == (*db->put)(db, key, val, 0))
                   1150:                return;
                   1151:
                   1152:        perror(dbn);
                   1153:        exit((int)MANDOCLEVEL_SYSERR);
                   1154:        /* NOTREACHED */
                   1155: }
                   1156:
                   1157: /*
                   1158:  * Call out to per-macro handlers after clearing the persistent database
                   1159:  * key.  If the macro sets the database key, flush it to the database.
                   1160:  */
                   1161: static void
                   1162: pmdoc_node(MDOC_ARGS)
                   1163: {
                   1164:
                   1165:        if (NULL == n)
                   1166:                return;
                   1167:
                   1168:        switch (n->type) {
                   1169:        case (MDOC_HEAD):
                   1170:                /* FALLTHROUGH */
                   1171:        case (MDOC_BODY):
                   1172:                /* FALLTHROUGH */
                   1173:        case (MDOC_TAIL):
                   1174:                /* FALLTHROUGH */
                   1175:        case (MDOC_BLOCK):
                   1176:                /* FALLTHROUGH */
                   1177:        case (MDOC_ELEM):
                   1178:                if (NULL == mdocs[n->tok])
                   1179:                        break;
                   1180:
                   1181:                buf->len = 0;
                   1182:                (*mdocs[n->tok])(hash, buf, dbuf, n, m);
                   1183:                break;
                   1184:        default:
                   1185:                break;
                   1186:        }
                   1187:
                   1188:        pmdoc_node(hash, buf, dbuf, n->child, m);
                   1189:        pmdoc_node(hash, buf, dbuf, n->next, m);
                   1190: }
                   1191:
                   1192: static int
                   1193: pman_node(MAN_ARGS)
                   1194: {
                   1195:        const struct man_node *head, *body;
                   1196:        const char      *start, *sv;
                   1197:        size_t           sz;
                   1198:
                   1199:        if (NULL == n)
                   1200:                return(0);
                   1201:
                   1202:        /*
                   1203:         * We're only searching for one thing: the first text child in
                   1204:         * the BODY of a NAME section.  Since we don't keep track of
                   1205:         * sections in -man, run some hoops to find out whether we're in
                   1206:         * the correct section or not.
                   1207:         */
                   1208:
                   1209:        if (MAN_BODY == n->type && MAN_SH == n->tok) {
                   1210:                body = n;
                   1211:                assert(body->parent);
                   1212:                if (NULL != (head = body->parent->head) &&
                   1213:                                1 == head->nchild &&
                   1214:                                NULL != (head = (head->child)) &&
                   1215:                                MAN_TEXT == head->type &&
                   1216:                                0 == strcmp(head->string, "NAME") &&
                   1217:                                NULL != (body = body->child) &&
                   1218:                                MAN_TEXT == body->type) {
                   1219:
                   1220:                        assert(body->string);
                   1221:                        start = sv = body->string;
                   1222:
                   1223:                        /*
                   1224:                         * Go through a special heuristic dance here.
                   1225:                         * This is why -man manuals are great!
                   1226:                         * (I'm being sarcastic: my eyes are bleeding.)
                   1227:                         * Conventionally, one or more manual names are
                   1228:                         * comma-specified prior to a whitespace, then a
                   1229:                         * dash, then a description.  Try to puzzle out
                   1230:                         * the name parts here.
                   1231:                         */
                   1232:
                   1233:                        for ( ;; ) {
                   1234:                                sz = strcspn(start, " ,");
                   1235:                                if ('\0' == start[(int)sz])
                   1236:                                        break;
                   1237:
                   1238:                                buf->len = 0;
                   1239:                                buf_appendb(buf, start, sz);
                   1240:                                buf_appendb(buf, "", 1);
                   1241:
1.5       schwarze 1242:                                hash_put(hash, buf, TYPE_Nm);
1.1       schwarze 1243:
                   1244:                                if (' ' == start[(int)sz]) {
                   1245:                                        start += (int)sz + 1;
                   1246:                                        break;
                   1247:                                }
                   1248:
                   1249:                                assert(',' == start[(int)sz]);
                   1250:                                start += (int)sz + 1;
                   1251:                                while (' ' == *start)
                   1252:                                        start++;
                   1253:                        }
                   1254:
                   1255:                        buf->len = 0;
                   1256:
                   1257:                        if (sv == start) {
                   1258:                                buf_append(buf, start);
                   1259:                                return(1);
                   1260:                        }
                   1261:
                   1262:                        while (' ' == *start)
                   1263:                                start++;
                   1264:
                   1265:                        if (0 == strncmp(start, "-", 1))
                   1266:                                start += 1;
                   1267:                        else if (0 == strncmp(start, "\\-", 2))
                   1268:                                start += 2;
                   1269:                        else if (0 == strncmp(start, "\\(en", 4))
                   1270:                                start += 4;
                   1271:                        else if (0 == strncmp(start, "\\(em", 4))
                   1272:                                start += 4;
                   1273:
                   1274:                        while (' ' == *start)
                   1275:                                start++;
                   1276:
                   1277:                        sz = strlen(start) + 1;
                   1278:                        buf_appendb(dbuf, start, sz);
                   1279:                        buf_appendb(buf, start, sz);
                   1280:
1.5       schwarze 1281:                        hash_put(hash, buf, TYPE_Nd);
1.1       schwarze 1282:                }
                   1283:        }
                   1284:
1.4       schwarze 1285:        for (n = n->child; n; n = n->next)
                   1286:                if (pman_node(hash, buf, dbuf, n))
                   1287:                        return(1);
1.1       schwarze 1288:
                   1289:        return(0);
                   1290: }
                   1291:
1.11      schwarze 1292: /*
                   1293:  * Parse a formatted manual page.
                   1294:  * By necessity, this involves rather crude guesswork.
                   1295:  */
                   1296: static void
                   1297: pformatted(DB *hash, struct buf *buf, struct buf *dbuf,
                   1298:                 const struct of *of)
                   1299: {
                   1300:        FILE            *stream;
                   1301:        char            *line, *p;
                   1302:        size_t           len, plen;
                   1303:
                   1304:        if (NULL == (stream = fopen(of->fname, "r"))) {
                   1305:                perror(of->fname);
                   1306:                return;
                   1307:        }
                   1308:
                   1309:        /*
                   1310:         * Always use the title derived from the filename up front,
                   1311:         * do not even try to find it in the file.  This also makes
                   1312:         * sure we don't end up with an orphan index record, even if
                   1313:         * the file content turns out to be completely unintelligible.
                   1314:         */
                   1315:
                   1316:        buf->len = 0;
                   1317:        buf_append(buf, of->title);
                   1318:        hash_put(hash, buf, TYPE_Nm);
                   1319:
                   1320:        while (NULL != (line = fgetln(stream, &len)) && '\n' != *line)
                   1321:                /* Skip to first blank line. */ ;
                   1322:
                   1323:        while (NULL != (line = fgetln(stream, &len)) &&
                   1324:                        ('\n' == *line || ' ' == *line))
                   1325:                /* Skip to first section header. */ ;
                   1326:
                   1327:        /*
                   1328:         * If no page content can be found,
                   1329:         * reuse the page title as the page description.
                   1330:         */
                   1331:
                   1332:        if (NULL == (line = fgetln(stream, &len))) {
                   1333:                buf_appendb(dbuf, buf->cp, buf->size);
                   1334:                hash_put(hash, buf, TYPE_Nd);
                   1335:                fclose(stream);
                   1336:                return;
                   1337:        }
                   1338:        fclose(stream);
                   1339:
                   1340:        /*
                   1341:         * If there is a dash, skip to the text following it.
                   1342:         */
                   1343:
                   1344:        for (p = line, plen = len; plen; p++, plen--)
                   1345:                if ('-' == *p)
                   1346:                        break;
                   1347:        for ( ; plen; p++, plen--)
                   1348:                if ('-' != *p && ' ' != *p && 8 != *p)
                   1349:                        break;
                   1350:        if (0 == plen) {
                   1351:                p = line;
                   1352:                plen = len;
                   1353:        }
                   1354:
                   1355:        /*
                   1356:         * Copy the rest of the line, but no more than 70 bytes.
                   1357:         */
                   1358:
                   1359:        if (70 < plen)
                   1360:                plen = 70;
                   1361:        p[plen-1] = '\0';
                   1362:        buf_appendb(dbuf, p, plen);
                   1363:        buf->len = 0;
                   1364:        buf_appendb(buf, p, plen);
                   1365:        hash_put(hash, buf, TYPE_Nd);
                   1366: }
                   1367:
1.1       schwarze 1368: static void
1.13      schwarze 1369: ofile_argbuild(int argc, char *argv[], struct of **of)
1.2       schwarze 1370: {
1.6       schwarze 1371:        char             buf[MAXPATHLEN];
                   1372:        char            *sec, *arch, *title, *p;
1.11      schwarze 1373:        int              i, src_form;
1.2       schwarze 1374:        struct of       *nof;
                   1375:
                   1376:        for (i = 0; i < argc; i++) {
1.6       schwarze 1377:
                   1378:                /*
1.8       schwarze 1379:                 * Try to infer the manual section, architecture and
                   1380:                 * page title from the path, assuming it looks like
1.11      schwarze 1381:                 *   man*[/<arch>]/<title>.<section>   or
                   1382:                 *   cat<section>[/<arch>]/<title>.0
1.6       schwarze 1383:                 */
                   1384:
                   1385:                if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {
                   1386:                        fprintf(stderr, "%s: Path too long\n", argv[i]);
                   1387:                        continue;
                   1388:                }
                   1389:                sec = arch = title = NULL;
1.11      schwarze 1390:                src_form = 0;
1.6       schwarze 1391:                p = strrchr(buf, '\0');
                   1392:                while (p-- > buf) {
                   1393:                        if (NULL == sec && '.' == *p) {
                   1394:                                sec = p + 1;
                   1395:                                *p = '\0';
1.11      schwarze 1396:                                if ('0' == *sec)
                   1397:                                        src_form |= MANDOC_FORM;
                   1398:                                else if ('1' <= *sec && '9' >= *sec)
                   1399:                                        src_form |= MANDOC_SRC;
1.6       schwarze 1400:                                continue;
                   1401:                        }
                   1402:                        if ('/' != *p)
                   1403:                                continue;
                   1404:                        if (NULL == title) {
                   1405:                                title = p + 1;
                   1406:                                *p = '\0';
                   1407:                                continue;
                   1408:                        }
1.11      schwarze 1409:                        if (strncmp("man", p + 1, 3)) {
                   1410:                                src_form |= MANDOC_SRC;
                   1411:                                arch = p + 1;
                   1412:                        } else if (strncmp("cat", p + 1, 3)) {
                   1413:                                src_form |= MANDOC_FORM;
1.6       schwarze 1414:                                arch = p + 1;
1.11      schwarze 1415:                        }
1.6       schwarze 1416:                        break;
                   1417:                }
                   1418:                if (NULL == title)
                   1419:                        title = buf;
                   1420:
                   1421:                /*
                   1422:                 * Build the file structure.
                   1423:                 */
                   1424:
1.2       schwarze 1425:                nof = mandoc_calloc(1, sizeof(struct of));
1.6       schwarze 1426:                nof->fname = mandoc_strdup(argv[i]);
                   1427:                if (NULL != sec)
                   1428:                        nof->sec = mandoc_strdup(sec);
                   1429:                if (NULL != arch)
                   1430:                        nof->arch = mandoc_strdup(arch);
                   1431:                nof->title = mandoc_strdup(title);
1.11      schwarze 1432:                nof->src_form = src_form;
1.6       schwarze 1433:
                   1434:                /*
                   1435:                 * Add the structure to the list.
                   1436:                 */
                   1437:
1.2       schwarze 1438:                if (verb > 2)
                   1439:                        printf("%s: Scheduling\n", argv[i]);
                   1440:                if (NULL == *of) {
                   1441:                        *of = nof;
                   1442:                        (*of)->first = nof;
                   1443:                } else {
                   1444:                        nof->first = (*of)->first;
                   1445:                        (*of)->next = nof;
                   1446:                        *of = nof;
                   1447:                }
                   1448:        }
                   1449: }
                   1450:
                   1451: /*
                   1452:  * Recursively build up a list of files to parse.
                   1453:  * We use this instead of ftw() and so on because I don't want global
                   1454:  * variables hanging around.
                   1455:  * This ignores the mandoc.db and mandoc.index files, but assumes that
                   1456:  * everything else is a manual.
                   1457:  * Pass in a pointer to a NULL structure for the first invocation.
                   1458:  */
                   1459: static int
1.6       schwarze 1460: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1.13      schwarze 1461:                int p_src_form, struct of **of)
1.2       schwarze 1462: {
                   1463:        char             buf[MAXPATHLEN];
1.11      schwarze 1464:        struct stat      sb;
1.2       schwarze 1465:        size_t           sz;
                   1466:        DIR             *d;
1.6       schwarze 1467:        const char      *fn, *sec, *arch;
1.11      schwarze 1468:        char            *p, *q, *suffix;
1.2       schwarze 1469:        struct of       *nof;
                   1470:        struct dirent   *dp;
1.11      schwarze 1471:        int              src_form;
1.2       schwarze 1472:
                   1473:        if (NULL == (d = opendir(dir))) {
                   1474:                perror(dir);
                   1475:                return(0);
                   1476:        }
                   1477:
                   1478:        while (NULL != (dp = readdir(d))) {
                   1479:                fn = dp->d_name;
1.6       schwarze 1480:
                   1481:                if ('.' == *fn)
                   1482:                        continue;
                   1483:
1.11      schwarze 1484:                src_form = p_src_form;
                   1485:
1.2       schwarze 1486:                if (DT_DIR == dp->d_type) {
1.6       schwarze 1487:                        sec = psec;
                   1488:                        arch = parch;
                   1489:
                   1490:                        /*
1.8       schwarze 1491:                         * By default, only use directories called:
1.11      schwarze 1492:                         *   man<section>/[<arch>/]   or
                   1493:                         *   cat<section>/[<arch>/]
1.6       schwarze 1494:                         */
                   1495:
                   1496:                        if (NULL == sec) {
1.11      schwarze 1497:                                if(0 == strncmp("man", fn, 3)) {
                   1498:                                        src_form |= MANDOC_SRC;
1.6       schwarze 1499:                                        sec = fn + 3;
1.11      schwarze 1500:                                } else if (0 == strncmp("cat", fn, 3)) {
                   1501:                                        src_form |= MANDOC_FORM;
                   1502:                                        sec = fn + 3;
                   1503:                                } else if (use_all)
1.6       schwarze 1504:                                        sec = fn;
                   1505:                                else
                   1506:                                        continue;
                   1507:                        } else if (NULL == arch && (use_all ||
                   1508:                                        NULL == strchr(fn, '.')))
                   1509:                                arch = fn;
                   1510:                        else if (0 == use_all)
1.2       schwarze 1511:                                continue;
                   1512:
                   1513:                        buf[0] = '\0';
                   1514:                        strlcat(buf, dir, MAXPATHLEN);
                   1515:                        strlcat(buf, "/", MAXPATHLEN);
                   1516:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1517:
1.6       schwarze 1518:                        if (MAXPATHLEN <= sz) {
                   1519:                                fprintf(stderr, "%s: Path too long\n", dir);
                   1520:                                return(0);
                   1521:                        }
                   1522:
                   1523:                        if (verb > 2)
                   1524:                                printf("%s: Scanning\n", buf);
                   1525:
                   1526:                        if ( ! ofile_dirbuild(buf, sec, arch,
1.13      schwarze 1527:                                        src_form, of))
1.6       schwarze 1528:                                return(0);
                   1529:                }
                   1530:                if (DT_REG != dp->d_type ||
                   1531:                    (NULL == psec && !use_all) ||
                   1532:                    !strcmp(MANDOC_DB, fn) ||
                   1533:                    !strcmp(MANDOC_IDX, fn))
                   1534:                        continue;
                   1535:
                   1536:                /*
1.8       schwarze 1537:                 * By default, skip files where the file name suffix
                   1538:                 * does not agree with the section directory
                   1539:                 * they are located in.
1.6       schwarze 1540:                 */
                   1541:
                   1542:                suffix = strrchr(fn, '.');
                   1543:                if (0 == use_all) {
                   1544:                        if (NULL == suffix)
1.2       schwarze 1545:                                continue;
1.11      schwarze 1546:                        if ((MANDOC_SRC & src_form &&
                   1547:                                         strcmp(suffix + 1, psec)) ||
                   1548:                            (MANDOC_FORM & src_form &&
                   1549:                                         strcmp(suffix + 1, "0")))
                   1550:                                        continue;
                   1551:                }
                   1552:                if (NULL != suffix) {
                   1553:                        if ('0' == suffix[1])
                   1554:                                src_form |= MANDOC_FORM;
                   1555:                        else if ('1' <= suffix[1] && '9' >= suffix[1])
                   1556:                                src_form |= MANDOC_SRC;
                   1557:                }
                   1558:
                   1559:
                   1560:                /*
                   1561:                 * Skip formatted manuals if a source version is
                   1562:                 * available.  Ignore the age: it is very unlikely
                   1563:                 * that people install newer formatted base manuals
                   1564:                 * when they used to have source manuals before,
                   1565:                 * and in ports, old manuals get removed on update.
                   1566:                 */
                   1567:                if (0 == use_all && MANDOC_FORM & src_form &&
                   1568:                                NULL != psec) {
                   1569:                        buf[0] = '\0';
                   1570:                        strlcat(buf, dir, MAXPATHLEN);
                   1571:                        p = strrchr(buf, '/');
                   1572:                        if (NULL == p)
                   1573:                                p = buf;
                   1574:                        else
                   1575:                                p++;
                   1576:                        if (0 == strncmp("cat", p, 3))
                   1577:                                memcpy(p, "man", 3);
                   1578:                        strlcat(buf, "/", MAXPATHLEN);
                   1579:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1580:                        if (sz >= MAXPATHLEN) {
                   1581:                                fprintf(stderr, "%s: Path too long\n", buf);
1.2       schwarze 1582:                                continue;
1.11      schwarze 1583:                        }
                   1584:                        q = strrchr(buf, '.');
                   1585:                        if (NULL != q && p < q++) {
                   1586:                                *q = '\0';
                   1587:                                sz = strlcat(buf, psec, MAXPATHLEN);
                   1588:                                if (sz >= MAXPATHLEN) {
                   1589:                                        fprintf(stderr,
                   1590:                                            "%s: Path too long\n", buf);
                   1591:                                        continue;
                   1592:                                }
                   1593:                                if (0 == stat(buf, &sb))
                   1594:                                        continue;
                   1595:                        }
1.2       schwarze 1596:                }
                   1597:
                   1598:                buf[0] = '\0';
                   1599:                strlcat(buf, dir, MAXPATHLEN);
                   1600:                strlcat(buf, "/", MAXPATHLEN);
                   1601:                sz = strlcat(buf, fn, MAXPATHLEN);
                   1602:                if (sz >= MAXPATHLEN) {
                   1603:                        fprintf(stderr, "%s: Path too long\n", dir);
1.11      schwarze 1604:                        continue;
1.2       schwarze 1605:                }
                   1606:
                   1607:                nof = mandoc_calloc(1, sizeof(struct of));
                   1608:                nof->fname = mandoc_strdup(buf);
1.6       schwarze 1609:                if (NULL != psec)
                   1610:                        nof->sec = mandoc_strdup(psec);
                   1611:                if (NULL != parch)
                   1612:                        nof->arch = mandoc_strdup(parch);
1.11      schwarze 1613:                nof->src_form = src_form;
1.8       schwarze 1614:
                   1615:                /*
                   1616:                 * Remember the file name without the extension,
                   1617:                 * to be used as the page title in the database.
                   1618:                 */
                   1619:
1.6       schwarze 1620:                if (NULL != suffix)
                   1621:                        *suffix = '\0';
                   1622:                nof->title = mandoc_strdup(fn);
1.2       schwarze 1623:
1.11      schwarze 1624:                /*
                   1625:                 * Add the structure to the list.
                   1626:                 */
                   1627:
1.2       schwarze 1628:                if (verb > 2)
                   1629:                        printf("%s: Scheduling\n", buf);
                   1630:                if (NULL == *of) {
                   1631:                        *of = nof;
                   1632:                        (*of)->first = nof;
                   1633:                } else {
                   1634:                        nof->first = (*of)->first;
                   1635:                        (*of)->next = nof;
                   1636:                        *of = nof;
                   1637:                }
                   1638:        }
                   1639:
1.4       schwarze 1640:        closedir(d);
1.2       schwarze 1641:        return(1);
                   1642: }
                   1643:
                   1644: static void
                   1645: ofile_free(struct of *of)
                   1646: {
                   1647:        struct of       *nof;
                   1648:
                   1649:        while (of) {
                   1650:                nof = of->next;
                   1651:                free(of->fname);
1.6       schwarze 1652:                free(of->sec);
                   1653:                free(of->arch);
                   1654:                free(of->title);
1.2       schwarze 1655:                free(of);
                   1656:                of = nof;
                   1657:        }
                   1658: }
                   1659:
                   1660: static void
1.1       schwarze 1661: usage(void)
                   1662: {
                   1663:
1.2       schwarze 1664:        fprintf(stderr, "usage: %s [-v] "
                   1665:                        "[-d dir [files...] |"
                   1666:                        " -u dir [files...] |"
                   1667:                        " dir...]\n", progname);
1.1       schwarze 1668: }