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

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