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

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