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

1.38    ! schwarze    1: /*     $Id: mandocdb.c,v 1.37 2012/01/09 23:21:47 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.38    ! schwarze  646:                 * Check whether the manual section given in a file
        !           647:                 * agrees with the directory where the file is located.
        !           648:                 * Some manuals have suffixes like (3p) on their
        !           649:                 * section number either inside the file or in the
        !           650:                 * directory name, some are linked into more than one
        !           651:                 * section, like encrypt(1) = makekey(8).  Do not skip
        !           652:                 * manuals for such reasons.
1.6       schwarze  653:                 */
                    654:
1.28      schwarze  655:                skip = 0;
                    656:                assert(of->sec);
                    657:                assert(msec);
1.38    ! schwarze  658:                if (warnings)
        !           659:                        if (strcasecmp(msec, of->sec))
1.28      schwarze  660:                                fprintf(stderr, "%s: "
                    661:                                        "section \"%s\" manual "
                    662:                                        "in \"%s\" directory\n",
                    663:                                        fn, msec, of->sec);
                    664:
1.32      schwarze  665:                /*
                    666:                 * Manual page directories exist for each kernel
                    667:                 * architecture as returned by machine(1).
                    668:                 * However, many manuals only depend on the
                    669:                 * application architecture as returned by arch(1).
                    670:                 * For example, some (2/ARM) manuals are shared
                    671:                 * across the "armish" and "zaurus" kernel
                    672:                 * architectures.
                    673:                 * A few manuals are even shared across completely
                    674:                 * different architectures, for example fdformat(1)
                    675:                 * on amd64, i386, sparc, and sparc64.
                    676:                 * Thus, warn about architecture mismatches,
                    677:                 * but don't skip manuals for this reason.
                    678:                 */
                    679:
1.28      schwarze  680:                assert(of->arch);
                    681:                assert(march);
1.38    ! schwarze  682:                if (warnings)
        !           683:                        if (strcasecmp(march, of->arch))
1.28      schwarze  684:                                fprintf(stderr, "%s: "
                    685:                                        "architecture \"%s\" manual "
                    686:                                        "in \"%s\" directory\n",
                    687:                                        fn, march, of->arch);
1.6       schwarze  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.6       schwarze  693:                 */
                    694:
                    695:                assert(of->title);
                    696:                assert(mtitle);
1.37      schwarze  697:                if (strcasecmp(mtitle, of->title))
1.28      schwarze  698:                        skip = 1;
1.6       schwarze  699:
1.37      schwarze  700:                /*
                    701:                 * Build a title string for the file.  If it matches
                    702:                 * the location of the file, remember the title as
                    703:                 * found; else, remember it as missing.
                    704:                 */
                    705:
                    706:                if (warnings) {
                    707:                        buf->len = 0;
                    708:                        buf_appendb(buf, mtitle, strlen(mtitle));
                    709:                        buf_appendb(buf, "(", 1);
                    710:                        buf_appendb(buf, msec, strlen(msec));
                    711:                        if ('\0' != *march) {
                    712:                                buf_appendb(buf, "/", 1);
                    713:                                buf_appendb(buf, march, strlen(march));
                    714:                        }
                    715:                        buf_appendb(buf, ")", 2);
                    716:                        for (p = buf->cp; '\0' != *p; p++)
                    717:                                *p = tolower(*p);
                    718:                        key.data = buf->cp;
                    719:                        key.size = buf->len;
                    720:                        val.data = NULL;
                    721:                        val.size = 0;
                    722:                        if (0 == skip)
                    723:                                val.data = "";
                    724:                        else {
                    725:                                ch = (*files->get)(files, &key, &val, 0);
                    726:                                if (ch < 0) {
                    727:                                        perror("hash");
                    728:                                        exit((int)MANDOCLEVEL_SYSERR);
                    729:                                } else if (ch > 0) {
                    730:                                        val.data = (void *)fn;
                    731:                                        val.size = strlen(fn) + 1;
                    732:                                } else
                    733:                                        val.data = NULL;
                    734:                        }
                    735:                        if (NULL != val.data &&
                    736:                            (*files->put)(files, &key, &val, 0) < 0) {
                    737:                                perror("hash");
                    738:                                exit((int)MANDOCLEVEL_SYSERR);
                    739:                        }
                    740:                }
                    741:
1.28      schwarze  742:                if (skip && !use_all)
1.6       schwarze  743:                        continue;
                    744:
1.28      schwarze  745:                /*
1.1       schwarze  746:                 * The index record value consists of a nil-terminated
                    747:                 * filename, a nil-terminated manual section, and a
1.38    ! schwarze  748:                 * nil-terminated description.  Use the actual
        !           749:                 * location of the file, such that the user can find
        !           750:                 * it with man(1).  Since the description may not be
        !           751:                 * set, we set a sentinel to see if we're going to
        !           752:                 * write a nil byte in its place.
1.1       schwarze  753:                 */
                    754:
1.2       schwarze  755:                dbuf->len = 0;
1.26      schwarze  756:                type = mdoc ? 'd' : (man ? 'a' : 'c');
                    757:                buf_appendb(dbuf, &type, 1);
1.2       schwarze  758:                buf_appendb(dbuf, fn, strlen(fn) + 1);
1.38    ! schwarze  759:                buf_appendb(dbuf, of->sec, strlen(of->sec) + 1);
        !           760:                buf_appendb(dbuf, of->title, strlen(of->title) + 1);
        !           761:                buf_appendb(dbuf, of->arch, strlen(of->arch) + 1);
1.1       schwarze  762:
1.2       schwarze  763:                sv = dbuf->len;
1.1       schwarze  764:
1.24      schwarze  765:                /*
                    766:                 * Collect keyword/mask pairs.
                    767:                 * Each pair will become a new btree node.
                    768:                 */
1.1       schwarze  769:
1.24      schwarze  770:                hash_reset(&hash);
1.1       schwarze  771:                if (mdoc)
1.2       schwarze  772:                        pmdoc_node(hash, buf, dbuf,
1.1       schwarze  773:                                mdoc_node(mdoc), mdoc_meta(mdoc));
1.11      schwarze  774:                else if (man)
1.2       schwarze  775:                        pman_node(hash, buf, dbuf, man_node(man));
1.11      schwarze  776:                else
                    777:                        pformatted(hash, buf, dbuf, of);
1.1       schwarze  778:
1.28      schwarze  779:                /* Test mode, do not access any database. */
                    780:
                    781:                if (NULL == mdb->db || NULL == mdb->idx)
                    782:                        continue;
1.38    ! schwarze  783:
        !           784:                /*
        !           785:                 * Make sure the file name is always registered
        !           786:                 * as an .Nm search key.
        !           787:                 */
        !           788:                buf->len = 0;
        !           789:                buf_append(buf, of->title);
        !           790:                hash_put(hash, buf, TYPE_Nm);
1.28      schwarze  791:
1.1       schwarze  792:                /*
1.24      schwarze  793:                 * Reclaim an empty index record, if available.
                    794:                 * Use its record number for all new btree nodes.
1.1       schwarze  795:                 */
                    796:
1.28      schwarze  797:                if (recs->cur > 0) {
                    798:                        recs->cur--;
                    799:                        rec = recs->stack[(int)recs->cur];
                    800:                } else if (recs->last > 0) {
                    801:                        rec = recs->last;
                    802:                        recs->last = 0;
1.24      schwarze  803:                } else
                    804:                        rec++;
1.29      schwarze  805:                vbuf[1] = htobe64(rec);
1.24      schwarze  806:
                    807:                /*
                    808:                 * Copy from the in-memory hashtable of pending
                    809:                 * keyword/mask pairs into the database.
                    810:                 */
                    811:
1.1       schwarze  812:                seq = R_FIRST;
                    813:                while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
                    814:                        seq = R_NEXT;
1.27      schwarze  815:                        assert(sizeof(uint64_t) == val.size);
                    816:                        memcpy(&mask, val.data, val.size);
1.29      schwarze  817:                        vbuf[0] = htobe64(mask);
                    818:                        val.size = sizeof(vbuf);
1.9       schwarze  819:                        val.data = &vbuf;
1.28      schwarze  820:                        dbt_put(mdb->db, mdb->dbn, &key, &val);
1.1       schwarze  821:                }
                    822:                if (ch < 0) {
                    823:                        perror("hash");
1.34      schwarze  824:                        unlink(mdb->dbn);
                    825:                        unlink(mdb->idxn);
1.1       schwarze  826:                        exit((int)MANDOCLEVEL_SYSERR);
                    827:                }
1.28      schwarze  828:
1.1       schwarze  829:                /*
                    830:                 * Apply to the index.  If we haven't had a description
                    831:                 * set, put an empty one in now.
                    832:                 */
                    833:
1.2       schwarze  834:                if (dbuf->len == sv)
                    835:                        buf_appendb(dbuf, "", 1);
1.1       schwarze  836:
                    837:                key.data = &rec;
                    838:                key.size = sizeof(recno_t);
                    839:
1.2       schwarze  840:                val.data = dbuf->cp;
                    841:                val.size = dbuf->len;
1.1       schwarze  842:
1.2       schwarze  843:                if (verb)
1.28      schwarze  844:                        printf("%s: adding to index\n", fn);
1.16      schwarze  845:
1.28      schwarze  846:                dbt_put(mdb->idx, mdb->idxn, &key, &val);
1.37      schwarze  847:        }
                    848:
                    849:        /*
                    850:         * Iterate the remembered file titles and check that
                    851:         * all files can be found by their main title.
                    852:         */
                    853:
                    854:        if (warnings) {
                    855:                seq = R_FIRST;
                    856:                while (0 == (*files->seq)(files, &key, &val, seq)) {
                    857:                        seq = R_NEXT;
                    858:                        if (val.size)
                    859:                                fprintf(stderr, "%s: probably "
                    860:                                    "unreachable, title is %s\n",
                    861:                                    (char *)val.data, (char *)key.data);
                    862:                }
                    863:                (*files->close)(files);
1.2       schwarze  864:        }
                    865: }
                    866:
                    867: /*
                    868:  * Scan through all entries in the index file `idx' and prune those
                    869:  * entries in `ofile'.
                    870:  * Pruning consists of removing from `db', then invalidating the entry
                    871:  * in `idx' (zeroing its value size).
                    872:  */
                    873: static void
1.28      schwarze  874: index_prune(const struct of *ofile, struct mdb *mdb, struct recs *recs)
1.2       schwarze  875: {
                    876:        const struct of *of;
1.26      schwarze  877:        const char      *fn;
1.29      schwarze  878:        uint64_t         vbuf[2];
1.2       schwarze  879:        unsigned         seq, sseq;
                    880:        DBT              key, val;
                    881:        int              ch;
                    882:
1.28      schwarze  883:        recs->cur = 0;
1.2       schwarze  884:        seq = R_FIRST;
1.28      schwarze  885:        while (0 == (ch = (*mdb->idx->seq)(mdb->idx, &key, &val, seq))) {
1.2       schwarze  886:                seq = R_NEXT;
1.27      schwarze  887:                assert(sizeof(recno_t) == key.size);
1.28      schwarze  888:                memcpy(&recs->last, key.data, key.size);
1.16      schwarze  889:
                    890:                /* Deleted records are zero-sized.  Skip them. */
                    891:
                    892:                if (0 == val.size)
                    893:                        goto cont;
                    894:
                    895:                /*
                    896:                 * Make sure we're sane.
                    897:                 * Read past our mdoc/man/cat type to the next string,
                    898:                 * then make sure it's bounded by a NUL.
                    899:                 * Failing any of these, we go into our error handler.
                    900:                 */
                    901:
1.26      schwarze  902:                fn = (char *)val.data + 1;
                    903:                if (NULL == memchr(fn, '\0', val.size - 1))
1.16      schwarze  904:                        break;
                    905:
1.28      schwarze  906:                /*
1.16      schwarze  907:                 * Search for the file in those we care about.
                    908:                 * XXX: build this into a tree.  Too slow.
                    909:                 */
1.2       schwarze  910:
1.28      schwarze  911:                for (of = ofile->first; of; of = of->next)
1.2       schwarze  912:                        if (0 == strcmp(fn, of->fname))
                    913:                                break;
                    914:
                    915:                if (NULL == of)
                    916:                        continue;
                    917:
1.16      schwarze  918:                /*
                    919:                 * Search through the keyword database, throwing out all
                    920:                 * references to our file.
                    921:                 */
                    922:
1.2       schwarze  923:                sseq = R_FIRST;
1.28      schwarze  924:                while (0 == (ch = (*mdb->db->seq)(mdb->db,
                    925:                                        &key, &val, sseq))) {
1.2       schwarze  926:                        sseq = R_NEXT;
1.29      schwarze  927:                        if (sizeof(vbuf) != val.size)
1.16      schwarze  928:                                break;
                    929:
1.29      schwarze  930:                        memcpy(vbuf, val.data, val.size);
                    931:                        if (recs->last != betoh64(vbuf[1]))
1.2       schwarze  932:                                continue;
1.16      schwarze  933:
1.28      schwarze  934:                        if ((ch = (*mdb->db->del)(mdb->db,
                    935:                                        &key, R_CURSOR)) < 0)
1.2       schwarze  936:                                break;
                    937:                }
1.16      schwarze  938:
1.2       schwarze  939:                if (ch < 0) {
1.28      schwarze  940:                        perror(mdb->dbn);
1.2       schwarze  941:                        exit((int)MANDOCLEVEL_SYSERR);
1.16      schwarze  942:                } else if (1 != ch) {
1.28      schwarze  943:                        fprintf(stderr, "%s: corrupt database\n",
                    944:                                        mdb->dbn);
1.16      schwarze  945:                        exit((int)MANDOCLEVEL_SYSERR);
1.2       schwarze  946:                }
1.1       schwarze  947:
1.2       schwarze  948:                if (verb)
1.28      schwarze  949:                        printf("%s: deleting from index\n", fn);
1.1       schwarze  950:
1.2       schwarze  951:                val.size = 0;
1.28      schwarze  952:                ch = (*mdb->idx->put)(mdb->idx, &key, &val, R_CURSOR);
1.1       schwarze  953:
1.16      schwarze  954:                if (ch < 0)
                    955:                        break;
                    956: cont:
1.28      schwarze  957:                if (recs->cur >= recs->size) {
                    958:                        recs->size += MANDOC_SLOP;
                    959:                        recs->stack = mandoc_realloc(recs->stack,
                    960:                                        recs->size * sizeof(recno_t));
1.2       schwarze  961:                }
1.1       schwarze  962:
1.28      schwarze  963:                recs->stack[(int)recs->cur] = recs->last;
                    964:                recs->cur++;
1.2       schwarze  965:        }
1.16      schwarze  966:
                    967:        if (ch < 0) {
1.28      schwarze  968:                perror(mdb->idxn);
1.16      schwarze  969:                exit((int)MANDOCLEVEL_SYSERR);
                    970:        } else if (1 != ch) {
1.28      schwarze  971:                fprintf(stderr, "%s: corrupt index\n", mdb->idxn);
1.16      schwarze  972:                exit((int)MANDOCLEVEL_SYSERR);
                    973:        }
                    974:
1.28      schwarze  975:        recs->last++;
1.1       schwarze  976: }
                    977:
                    978: /*
                    979:  * Grow the buffer (if necessary) and copy in a binary string.
                    980:  */
                    981: static void
                    982: buf_appendb(struct buf *buf, const void *cp, size_t sz)
                    983: {
                    984:
                    985:        /* Overshoot by MANDOC_BUFSZ. */
                    986:
                    987:        while (buf->len + sz >= buf->size) {
                    988:                buf->size = buf->len + sz + MANDOC_BUFSZ;
                    989:                buf->cp = mandoc_realloc(buf->cp, buf->size);
                    990:        }
                    991:
                    992:        memcpy(buf->cp + (int)buf->len, cp, sz);
                    993:        buf->len += sz;
                    994: }
                    995:
                    996: /*
                    997:  * Append a nil-terminated string to the buffer.
                    998:  * This can be invoked multiple times.
                    999:  * The buffer string will be nil-terminated.
                   1000:  * If invoked multiple times, a space is put between strings.
                   1001:  */
                   1002: static void
                   1003: buf_append(struct buf *buf, const char *cp)
                   1004: {
                   1005:        size_t           sz;
                   1006:
                   1007:        if (0 == (sz = strlen(cp)))
                   1008:                return;
                   1009:
                   1010:        if (buf->len)
                   1011:                buf->cp[(int)buf->len - 1] = ' ';
                   1012:
                   1013:        buf_appendb(buf, cp, sz + 1);
                   1014: }
                   1015:
                   1016: /*
                   1017:  * Recursively add all text from a given node.
                   1018:  * This is optimised for general mdoc nodes in this context, which do
                   1019:  * not consist of subexpressions and having a recursive call for n->next
                   1020:  * would be wasteful.
                   1021:  * The "f" variable should be 0 unless called from pmdoc_Nd for the
                   1022:  * description buffer, which does not start at the beginning of the
                   1023:  * buffer.
                   1024:  */
                   1025: static void
                   1026: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
                   1027: {
                   1028:
                   1029:        for ( ; n; n = n->next) {
                   1030:                if (n->child)
                   1031:                        buf_appendmdoc(buf, n->child, f);
                   1032:
                   1033:                if (MDOC_TEXT == n->type && f) {
                   1034:                        f = 0;
                   1035:                        buf_appendb(buf, n->string,
                   1036:                                        strlen(n->string) + 1);
                   1037:                } else if (MDOC_TEXT == n->type)
                   1038:                        buf_append(buf, n->string);
                   1039:
                   1040:        }
                   1041: }
                   1042:
                   1043: static void
                   1044: hash_reset(DB **db)
                   1045: {
                   1046:        DB              *hash;
                   1047:
                   1048:        if (NULL != (hash = *db))
                   1049:                (*hash->close)(hash);
                   1050:
1.2       schwarze 1051:        *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1.1       schwarze 1052:        if (NULL == *db) {
                   1053:                perror("hash");
                   1054:                exit((int)MANDOCLEVEL_SYSERR);
                   1055:        }
                   1056: }
                   1057:
                   1058: /* ARGSUSED */
1.19      schwarze 1059: static int
                   1060: pmdoc_head(MDOC_ARGS)
                   1061: {
                   1062:
                   1063:        return(MDOC_HEAD == n->type);
                   1064: }
                   1065:
                   1066: /* ARGSUSED */
                   1067: static int
                   1068: pmdoc_body(MDOC_ARGS)
                   1069: {
                   1070:
                   1071:        return(MDOC_BODY == n->type);
                   1072: }
                   1073:
                   1074: /* ARGSUSED */
                   1075: static int
1.1       schwarze 1076: pmdoc_Fd(MDOC_ARGS)
                   1077: {
                   1078:        const char      *start, *end;
                   1079:        size_t           sz;
1.19      schwarze 1080:
1.1       schwarze 1081:        if (SEC_SYNOPSIS != n->sec)
1.19      schwarze 1082:                return(0);
1.1       schwarze 1083:        if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.19      schwarze 1084:                return(0);
1.1       schwarze 1085:
                   1086:        /*
                   1087:         * Only consider those `Fd' macro fields that begin with an
                   1088:         * "inclusion" token (versus, e.g., #define).
                   1089:         */
                   1090:        if (strcmp("#include", n->string))
1.19      schwarze 1091:                return(0);
1.1       schwarze 1092:
                   1093:        if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.19      schwarze 1094:                return(0);
1.1       schwarze 1095:
                   1096:        /*
                   1097:         * Strip away the enclosing angle brackets and make sure we're
                   1098:         * not zero-length.
                   1099:         */
                   1100:
                   1101:        start = n->string;
                   1102:        if ('<' == *start || '"' == *start)
                   1103:                start++;
                   1104:
                   1105:        if (0 == (sz = strlen(start)))
1.19      schwarze 1106:                return(0);
1.1       schwarze 1107:
                   1108:        end = &start[(int)sz - 1];
                   1109:        if ('>' == *end || '"' == *end)
                   1110:                end--;
                   1111:
                   1112:        assert(end >= start);
                   1113:
                   1114:        buf_appendb(buf, start, (size_t)(end - start + 1));
                   1115:        buf_appendb(buf, "", 1);
1.19      schwarze 1116:        return(1);
1.1       schwarze 1117: }
                   1118:
                   1119: /* ARGSUSED */
1.19      schwarze 1120: static int
                   1121: pmdoc_In(MDOC_ARGS)
1.1       schwarze 1122: {
                   1123:
                   1124:        if (NULL == n->child || MDOC_TEXT != n->child->type)
1.19      schwarze 1125:                return(0);
1.1       schwarze 1126:
                   1127:        buf_append(buf, n->child->string);
1.19      schwarze 1128:        return(1);
1.1       schwarze 1129: }
                   1130:
                   1131: /* ARGSUSED */
1.19      schwarze 1132: static int
1.1       schwarze 1133: pmdoc_Fn(MDOC_ARGS)
                   1134: {
1.19      schwarze 1135:        struct mdoc_node *nn;
1.1       schwarze 1136:        const char      *cp;
                   1137:
1.19      schwarze 1138:        nn = n->child;
                   1139:
                   1140:        if (NULL == nn || MDOC_TEXT != nn->type)
                   1141:                return(0);
                   1142:
                   1143:        /* .Fn "struct type *name" "char *arg" */
1.1       schwarze 1144:
1.19      schwarze 1145:        cp = strrchr(nn->string, ' ');
1.1       schwarze 1146:        if (NULL == cp)
1.19      schwarze 1147:                cp = nn->string;
1.1       schwarze 1148:
                   1149:        /* Strip away pointer symbol. */
                   1150:
                   1151:        while ('*' == *cp)
                   1152:                cp++;
                   1153:
1.19      schwarze 1154:        /* Store the function name. */
                   1155:
1.1       schwarze 1156:        buf_append(buf, cp);
1.5       schwarze 1157:        hash_put(hash, buf, TYPE_Fn);
1.19      schwarze 1158:
                   1159:        /* Store the function type. */
                   1160:
                   1161:        if (nn->string < cp) {
                   1162:                buf->len = 0;
                   1163:                buf_appendb(buf, nn->string, cp - nn->string);
                   1164:                buf_appendb(buf, "", 1);
                   1165:                hash_put(hash, buf, TYPE_Ft);
                   1166:        }
                   1167:
                   1168:        /* Store the arguments. */
                   1169:
                   1170:        for (nn = nn->next; nn; nn = nn->next) {
                   1171:                if (MDOC_TEXT != nn->type)
                   1172:                        continue;
                   1173:                buf->len = 0;
                   1174:                buf_append(buf, nn->string);
                   1175:                hash_put(hash, buf, TYPE_Fa);
                   1176:        }
                   1177:
                   1178:        return(0);
1.1       schwarze 1179: }
                   1180:
                   1181: /* ARGSUSED */
1.19      schwarze 1182: static int
1.1       schwarze 1183: pmdoc_St(MDOC_ARGS)
                   1184: {
1.19      schwarze 1185:
1.1       schwarze 1186:        if (NULL == n->child || MDOC_TEXT != n->child->type)
1.19      schwarze 1187:                return(0);
1.1       schwarze 1188:
                   1189:        buf_append(buf, n->child->string);
1.19      schwarze 1190:        return(1);
1.1       schwarze 1191: }
                   1192:
                   1193: /* ARGSUSED */
1.19      schwarze 1194: static int
1.1       schwarze 1195: pmdoc_Xr(MDOC_ARGS)
                   1196: {
                   1197:
                   1198:        if (NULL == (n = n->child))
1.19      schwarze 1199:                return(0);
1.1       schwarze 1200:
                   1201:        buf_appendb(buf, n->string, strlen(n->string));
                   1202:
                   1203:        if (NULL != (n = n->next)) {
                   1204:                buf_appendb(buf, ".", 1);
                   1205:                buf_appendb(buf, n->string, strlen(n->string) + 1);
                   1206:        } else
                   1207:                buf_appendb(buf, ".", 2);
                   1208:
1.19      schwarze 1209:        return(1);
1.1       schwarze 1210: }
                   1211:
                   1212: /* ARGSUSED */
1.19      schwarze 1213: static int
1.1       schwarze 1214: pmdoc_Nd(MDOC_ARGS)
                   1215: {
                   1216:
                   1217:        if (MDOC_BODY != n->type)
1.19      schwarze 1218:                return(0);
1.1       schwarze 1219:
                   1220:        buf_appendmdoc(dbuf, n->child, 1);
1.19      schwarze 1221:        return(1);
1.1       schwarze 1222: }
                   1223:
                   1224: /* ARGSUSED */
1.19      schwarze 1225: static int
                   1226: pmdoc_Nm(MDOC_ARGS)
1.1       schwarze 1227: {
                   1228:
1.19      schwarze 1229:        if (SEC_NAME == n->sec)
                   1230:                return(1);
                   1231:        else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1232:                return(0);
1.1       schwarze 1233:
1.19      schwarze 1234:        if (NULL == n->child)
                   1235:                buf_append(buf, m->name);
1.1       schwarze 1236:
1.19      schwarze 1237:        return(1);
1.1       schwarze 1238: }
                   1239:
                   1240: /* ARGSUSED */
1.19      schwarze 1241: static int
                   1242: pmdoc_Sh(MDOC_ARGS)
1.1       schwarze 1243: {
                   1244:
1.19      schwarze 1245:        return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1       schwarze 1246: }
                   1247:
                   1248: static void
1.9       schwarze 1249: hash_put(DB *db, const struct buf *buf, uint64_t mask)
1.1       schwarze 1250: {
1.27      schwarze 1251:        uint64_t         oldmask;
1.1       schwarze 1252:        DBT              key, val;
                   1253:        int              rc;
                   1254:
                   1255:        if (buf->len < 2)
                   1256:                return;
                   1257:
                   1258:        key.data = buf->cp;
                   1259:        key.size = buf->len;
                   1260:
                   1261:        if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
                   1262:                perror("hash");
                   1263:                exit((int)MANDOCLEVEL_SYSERR);
1.27      schwarze 1264:        } else if (0 == rc) {
                   1265:                assert(sizeof(uint64_t) == val.size);
                   1266:                memcpy(&oldmask, val.data, val.size);
                   1267:                mask |= oldmask;
                   1268:        }
1.1       schwarze 1269:
                   1270:        val.data = &mask;
1.9       schwarze 1271:        val.size = sizeof(uint64_t);
1.1       schwarze 1272:
                   1273:        if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
                   1274:                perror("hash");
                   1275:                exit((int)MANDOCLEVEL_SYSERR);
                   1276:        }
                   1277: }
                   1278:
                   1279: static void
                   1280: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
                   1281: {
                   1282:
                   1283:        assert(key->size);
                   1284:        assert(val->size);
                   1285:
                   1286:        if (0 == (*db->put)(db, key, val, 0))
                   1287:                return;
                   1288:
                   1289:        perror(dbn);
                   1290:        exit((int)MANDOCLEVEL_SYSERR);
                   1291:        /* NOTREACHED */
                   1292: }
                   1293:
                   1294: /*
                   1295:  * Call out to per-macro handlers after clearing the persistent database
                   1296:  * key.  If the macro sets the database key, flush it to the database.
                   1297:  */
                   1298: static void
                   1299: pmdoc_node(MDOC_ARGS)
                   1300: {
                   1301:
                   1302:        if (NULL == n)
                   1303:                return;
                   1304:
                   1305:        switch (n->type) {
                   1306:        case (MDOC_HEAD):
                   1307:                /* FALLTHROUGH */
                   1308:        case (MDOC_BODY):
                   1309:                /* FALLTHROUGH */
                   1310:        case (MDOC_TAIL):
                   1311:                /* FALLTHROUGH */
                   1312:        case (MDOC_BLOCK):
                   1313:                /* FALLTHROUGH */
                   1314:        case (MDOC_ELEM):
1.19      schwarze 1315:                buf->len = 0;
                   1316:
                   1317:                /*
                   1318:                 * Both NULL handlers and handlers returning true
                   1319:                 * request using the data.  Only skip the element
                   1320:                 * when the handler returns false.
                   1321:                 */
                   1322:
                   1323:                if (NULL != mdocs[n->tok].fp &&
                   1324:                    0 == (*mdocs[n->tok].fp)(hash, buf, dbuf, n, m))
1.1       schwarze 1325:                        break;
                   1326:
1.19      schwarze 1327:                /*
                   1328:                 * For many macros, use the text from all children.
                   1329:                 * Set zero flags for macros not needing this.
                   1330:                 * In that case, the handler must fill the buffer.
                   1331:                 */
                   1332:
                   1333:                if (MDOCF_CHILD & mdocs[n->tok].flags)
                   1334:                        buf_appendmdoc(buf, n->child, 0);
                   1335:
                   1336:                /*
                   1337:                 * Cover the most common case:
                   1338:                 * Automatically stage one string per element.
                   1339:                 * Set a zero mask for macros not needing this.
                   1340:                 * Additional staging can be done in the handler.
                   1341:                 */
                   1342:
                   1343:                if (mdocs[n->tok].mask)
                   1344:                        hash_put(hash, buf, mdocs[n->tok].mask);
1.1       schwarze 1345:                break;
                   1346:        default:
                   1347:                break;
                   1348:        }
                   1349:
                   1350:        pmdoc_node(hash, buf, dbuf, n->child, m);
                   1351:        pmdoc_node(hash, buf, dbuf, n->next, m);
                   1352: }
                   1353:
                   1354: static int
                   1355: pman_node(MAN_ARGS)
                   1356: {
                   1357:        const struct man_node *head, *body;
                   1358:        const char      *start, *sv;
                   1359:        size_t           sz;
                   1360:
                   1361:        if (NULL == n)
                   1362:                return(0);
                   1363:
                   1364:        /*
                   1365:         * We're only searching for one thing: the first text child in
                   1366:         * the BODY of a NAME section.  Since we don't keep track of
                   1367:         * sections in -man, run some hoops to find out whether we're in
                   1368:         * the correct section or not.
                   1369:         */
                   1370:
                   1371:        if (MAN_BODY == n->type && MAN_SH == n->tok) {
                   1372:                body = n;
                   1373:                assert(body->parent);
                   1374:                if (NULL != (head = body->parent->head) &&
                   1375:                                1 == head->nchild &&
                   1376:                                NULL != (head = (head->child)) &&
                   1377:                                MAN_TEXT == head->type &&
                   1378:                                0 == strcmp(head->string, "NAME") &&
                   1379:                                NULL != (body = body->child) &&
                   1380:                                MAN_TEXT == body->type) {
                   1381:
                   1382:                        assert(body->string);
                   1383:                        start = sv = body->string;
                   1384:
                   1385:                        /*
                   1386:                         * Go through a special heuristic dance here.
                   1387:                         * This is why -man manuals are great!
                   1388:                         * (I'm being sarcastic: my eyes are bleeding.)
                   1389:                         * Conventionally, one or more manual names are
                   1390:                         * comma-specified prior to a whitespace, then a
                   1391:                         * dash, then a description.  Try to puzzle out
                   1392:                         * the name parts here.
                   1393:                         */
                   1394:
                   1395:                        for ( ;; ) {
                   1396:                                sz = strcspn(start, " ,");
                   1397:                                if ('\0' == start[(int)sz])
                   1398:                                        break;
                   1399:
                   1400:                                buf->len = 0;
                   1401:                                buf_appendb(buf, start, sz);
                   1402:                                buf_appendb(buf, "", 1);
                   1403:
1.5       schwarze 1404:                                hash_put(hash, buf, TYPE_Nm);
1.1       schwarze 1405:
                   1406:                                if (' ' == start[(int)sz]) {
                   1407:                                        start += (int)sz + 1;
                   1408:                                        break;
                   1409:                                }
                   1410:
                   1411:                                assert(',' == start[(int)sz]);
                   1412:                                start += (int)sz + 1;
                   1413:                                while (' ' == *start)
                   1414:                                        start++;
                   1415:                        }
                   1416:
                   1417:                        buf->len = 0;
                   1418:
                   1419:                        if (sv == start) {
                   1420:                                buf_append(buf, start);
                   1421:                                return(1);
                   1422:                        }
                   1423:
                   1424:                        while (' ' == *start)
                   1425:                                start++;
                   1426:
                   1427:                        if (0 == strncmp(start, "-", 1))
                   1428:                                start += 1;
1.33      schwarze 1429:                        else if (0 == strncmp(start, "\\-\\-", 4))
                   1430:                                start += 4;
1.1       schwarze 1431:                        else if (0 == strncmp(start, "\\-", 2))
                   1432:                                start += 2;
                   1433:                        else if (0 == strncmp(start, "\\(en", 4))
                   1434:                                start += 4;
                   1435:                        else if (0 == strncmp(start, "\\(em", 4))
                   1436:                                start += 4;
                   1437:
                   1438:                        while (' ' == *start)
                   1439:                                start++;
                   1440:
                   1441:                        sz = strlen(start) + 1;
                   1442:                        buf_appendb(dbuf, start, sz);
                   1443:                        buf_appendb(buf, start, sz);
                   1444:
1.5       schwarze 1445:                        hash_put(hash, buf, TYPE_Nd);
1.1       schwarze 1446:                }
                   1447:        }
                   1448:
1.4       schwarze 1449:        for (n = n->child; n; n = n->next)
                   1450:                if (pman_node(hash, buf, dbuf, n))
                   1451:                        return(1);
1.1       schwarze 1452:
                   1453:        return(0);
                   1454: }
                   1455:
1.11      schwarze 1456: /*
                   1457:  * Parse a formatted manual page.
                   1458:  * By necessity, this involves rather crude guesswork.
                   1459:  */
                   1460: static void
1.33      schwarze 1461: pformatted(DB *hash, struct buf *buf,
                   1462:                struct buf *dbuf, const struct of *of)
1.11      schwarze 1463: {
                   1464:        FILE            *stream;
1.33      schwarze 1465:        char            *line, *p, *title;
                   1466:        size_t           len, plen, titlesz;
1.11      schwarze 1467:
                   1468:        if (NULL == (stream = fopen(of->fname, "r"))) {
1.28      schwarze 1469:                if (warnings)
                   1470:                        perror(of->fname);
1.11      schwarze 1471:                return;
                   1472:        }
                   1473:
                   1474:        /*
                   1475:         * Always use the title derived from the filename up front,
                   1476:         * do not even try to find it in the file.  This also makes
                   1477:         * sure we don't end up with an orphan index record, even if
                   1478:         * the file content turns out to be completely unintelligible.
                   1479:         */
                   1480:
                   1481:        buf->len = 0;
                   1482:        buf_append(buf, of->title);
                   1483:        hash_put(hash, buf, TYPE_Nm);
                   1484:
1.22      schwarze 1485:        /* Skip to first blank line. */
1.11      schwarze 1486:
1.22      schwarze 1487:        while (NULL != (line = fgetln(stream, &len)))
                   1488:                if ('\n' == *line)
                   1489:                        break;
                   1490:
                   1491:        /*
                   1492:         * Assume the first line that is not indented
                   1493:         * is the first section header.  Skip to it.
                   1494:         */
                   1495:
                   1496:        while (NULL != (line = fgetln(stream, &len)))
                   1497:                if ('\n' != *line && ' ' != *line)
                   1498:                        break;
1.33      schwarze 1499:
                   1500:        /*
                   1501:         * Read up until the next section into a buffer.
                   1502:         * Strip the leading and trailing newline from each read line,
                   1503:         * appending a trailing space.
                   1504:         * Ignore empty (whitespace-only) lines.
                   1505:         */
                   1506:
                   1507:        titlesz = 0;
                   1508:        title = NULL;
                   1509:
                   1510:        while (NULL != (line = fgetln(stream, &len))) {
                   1511:                if (' ' != *line || '\n' != line[(int)len - 1])
                   1512:                        break;
                   1513:                while (len > 0 && isspace((unsigned char)*line)) {
                   1514:                        line++;
                   1515:                        len--;
                   1516:                }
                   1517:                if (1 == len)
                   1518:                        continue;
                   1519:                title = mandoc_realloc(title, titlesz + len);
                   1520:                memcpy(title + titlesz, line, len);
                   1521:                titlesz += len;
                   1522:                title[(int)titlesz - 1] = ' ';
                   1523:        }
                   1524:
1.11      schwarze 1525:
                   1526:        /*
1.22      schwarze 1527:         * If no page content can be found, or the input line
                   1528:         * is already the next section header, or there is no
                   1529:         * trailing newline, reuse the page title as the page
                   1530:         * description.
1.11      schwarze 1531:         */
                   1532:
1.33      schwarze 1533:        if (NULL == title || '\0' == *title) {
1.28      schwarze 1534:                if (warnings)
                   1535:                        fprintf(stderr, "%s: cannot find NAME section\n",
                   1536:                                        of->fname);
1.11      schwarze 1537:                buf_appendb(dbuf, buf->cp, buf->size);
                   1538:                hash_put(hash, buf, TYPE_Nd);
                   1539:                fclose(stream);
1.33      schwarze 1540:                free(title);
1.11      schwarze 1541:                return;
                   1542:        }
1.22      schwarze 1543:
1.33      schwarze 1544:        title = mandoc_realloc(title, titlesz + 1);
                   1545:        title[(int)titlesz] = '\0';
1.11      schwarze 1546:
                   1547:        /*
1.22      schwarze 1548:         * Skip to the first dash.
                   1549:         * Use the remaining line as the description (no more than 70
                   1550:         * bytes).
1.11      schwarze 1551:         */
                   1552:
1.33      schwarze 1553:        if (NULL != (p = strstr(title, "- "))) {
1.22      schwarze 1554:                for (p += 2; ' ' == *p || '\b' == *p; p++)
                   1555:                        /* Skip to next word. */ ;
1.28      schwarze 1556:        } else {
                   1557:                if (warnings)
                   1558:                        fprintf(stderr, "%s: no dash in title line\n",
                   1559:                                        of->fname);
1.33      schwarze 1560:                p = title;
1.28      schwarze 1561:        }
1.22      schwarze 1562:
1.33      schwarze 1563:        plen = strlen(p);
1.11      schwarze 1564:
1.22      schwarze 1565:        /* Strip backspace-encoding from line. */
                   1566:
                   1567:        while (NULL != (line = memchr(p, '\b', plen))) {
                   1568:                len = line - p;
                   1569:                if (0 == len) {
                   1570:                        memmove(line, line + 1, plen--);
                   1571:                        continue;
                   1572:                }
                   1573:                memmove(line - 1, line + 1, plen - len);
                   1574:                plen -= 2;
                   1575:        }
1.11      schwarze 1576:
1.22      schwarze 1577:        buf_appendb(dbuf, p, plen + 1);
1.11      schwarze 1578:        buf->len = 0;
1.22      schwarze 1579:        buf_appendb(buf, p, plen + 1);
1.11      schwarze 1580:        hash_put(hash, buf, TYPE_Nd);
1.22      schwarze 1581:        fclose(stream);
1.33      schwarze 1582:        free(title);
1.11      schwarze 1583: }
                   1584:
1.1       schwarze 1585: static void
1.35      schwarze 1586: ofile_argbuild(int argc, char *argv[], struct of **of,
                   1587:                const char *basedir)
1.2       schwarze 1588: {
1.6       schwarze 1589:        char             buf[MAXPATHLEN];
1.35      schwarze 1590:        char             pbuf[PATH_MAX];
1.31      schwarze 1591:        const char      *sec, *arch, *title;
1.35      schwarze 1592:        char            *relpath, *p;
1.11      schwarze 1593:        int              i, src_form;
1.2       schwarze 1594:        struct of       *nof;
                   1595:
                   1596:        for (i = 0; i < argc; i++) {
1.35      schwarze 1597:                if (NULL == (relpath = realpath(argv[i], pbuf))) {
                   1598:                        perror(argv[i]);
                   1599:                        continue;
                   1600:                }
                   1601:                if (NULL != basedir) {
                   1602:                        if (strstr(pbuf, basedir) != pbuf) {
                   1603:                                fprintf(stderr, "%s: file outside "
                   1604:                                    "base directory %s\n",
                   1605:                                    pbuf, basedir);
                   1606:                                continue;
                   1607:                        }
                   1608:                        relpath = pbuf + strlen(basedir);
                   1609:                }
1.6       schwarze 1610:
                   1611:                /*
1.8       schwarze 1612:                 * Try to infer the manual section, architecture and
                   1613:                 * page title from the path, assuming it looks like
1.11      schwarze 1614:                 *   man*[/<arch>]/<title>.<section>   or
                   1615:                 *   cat<section>[/<arch>]/<title>.0
1.6       schwarze 1616:                 */
                   1617:
1.35      schwarze 1618:                if (strlcpy(buf, relpath, sizeof(buf)) >= sizeof(buf)) {
                   1619:                        fprintf(stderr, "%s: path too long\n", relpath);
1.6       schwarze 1620:                        continue;
                   1621:                }
1.28      schwarze 1622:                sec = arch = title = "";
1.11      schwarze 1623:                src_form = 0;
1.6       schwarze 1624:                p = strrchr(buf, '\0');
                   1625:                while (p-- > buf) {
1.28      schwarze 1626:                        if ('\0' == *sec && '.' == *p) {
1.6       schwarze 1627:                                sec = p + 1;
                   1628:                                *p = '\0';
1.11      schwarze 1629:                                if ('0' == *sec)
                   1630:                                        src_form |= MANDOC_FORM;
                   1631:                                else if ('1' <= *sec && '9' >= *sec)
                   1632:                                        src_form |= MANDOC_SRC;
1.6       schwarze 1633:                                continue;
                   1634:                        }
                   1635:                        if ('/' != *p)
                   1636:                                continue;
1.28      schwarze 1637:                        if ('\0' == *title) {
1.6       schwarze 1638:                                title = p + 1;
                   1639:                                *p = '\0';
                   1640:                                continue;
                   1641:                        }
1.18      schwarze 1642:                        if (0 == strncmp("man", p + 1, 3))
1.11      schwarze 1643:                                src_form |= MANDOC_SRC;
1.18      schwarze 1644:                        else if (0 == strncmp("cat", p + 1, 3))
1.11      schwarze 1645:                                src_form |= MANDOC_FORM;
1.18      schwarze 1646:                        else
1.6       schwarze 1647:                                arch = p + 1;
                   1648:                        break;
                   1649:                }
1.28      schwarze 1650:                if ('\0' == *title) {
                   1651:                        if (warnings)
                   1652:                                fprintf(stderr,
                   1653:                                    "%s: cannot deduce title "
                   1654:                                    "from filename\n",
1.35      schwarze 1655:                                    relpath);
1.6       schwarze 1656:                        title = buf;
1.28      schwarze 1657:                }
1.6       schwarze 1658:
                   1659:                /*
                   1660:                 * Build the file structure.
                   1661:                 */
                   1662:
1.2       schwarze 1663:                nof = mandoc_calloc(1, sizeof(struct of));
1.35      schwarze 1664:                nof->fname = mandoc_strdup(relpath);
1.28      schwarze 1665:                nof->sec = mandoc_strdup(sec);
                   1666:                nof->arch = mandoc_strdup(arch);
1.6       schwarze 1667:                nof->title = mandoc_strdup(title);
1.11      schwarze 1668:                nof->src_form = src_form;
1.6       schwarze 1669:
                   1670:                /*
                   1671:                 * Add the structure to the list.
                   1672:                 */
                   1673:
1.28      schwarze 1674:                if (verb > 1)
1.35      schwarze 1675:                        printf("%s: scheduling\n", relpath);
1.2       schwarze 1676:                if (NULL == *of) {
                   1677:                        *of = nof;
                   1678:                        (*of)->first = nof;
                   1679:                } else {
                   1680:                        nof->first = (*of)->first;
                   1681:                        (*of)->next = nof;
                   1682:                        *of = nof;
                   1683:                }
                   1684:        }
                   1685: }
                   1686:
                   1687: /*
                   1688:  * Recursively build up a list of files to parse.
                   1689:  * We use this instead of ftw() and so on because I don't want global
                   1690:  * variables hanging around.
1.36      schwarze 1691:  * This ignores the mandoc.db and mandoc.index files, but assumes that
1.2       schwarze 1692:  * everything else is a manual.
                   1693:  * Pass in a pointer to a NULL structure for the first invocation.
                   1694:  */
1.26      schwarze 1695: static void
1.6       schwarze 1696: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1.13      schwarze 1697:                int p_src_form, struct of **of)
1.2       schwarze 1698: {
                   1699:        char             buf[MAXPATHLEN];
                   1700:        size_t           sz;
                   1701:        DIR             *d;
1.6       schwarze 1702:        const char      *fn, *sec, *arch;
1.11      schwarze 1703:        char            *p, *q, *suffix;
1.2       schwarze 1704:        struct of       *nof;
                   1705:        struct dirent   *dp;
1.11      schwarze 1706:        int              src_form;
1.2       schwarze 1707:
                   1708:        if (NULL == (d = opendir(dir))) {
1.28      schwarze 1709:                if (warnings)
                   1710:                        perror(dir);
                   1711:                return;
1.2       schwarze 1712:        }
                   1713:
                   1714:        while (NULL != (dp = readdir(d))) {
                   1715:                fn = dp->d_name;
1.6       schwarze 1716:
                   1717:                if ('.' == *fn)
                   1718:                        continue;
                   1719:
1.11      schwarze 1720:                src_form = p_src_form;
                   1721:
1.2       schwarze 1722:                if (DT_DIR == dp->d_type) {
1.6       schwarze 1723:                        sec = psec;
                   1724:                        arch = parch;
                   1725:
                   1726:                        /*
1.8       schwarze 1727:                         * By default, only use directories called:
1.11      schwarze 1728:                         *   man<section>/[<arch>/]   or
                   1729:                         *   cat<section>/[<arch>/]
1.6       schwarze 1730:                         */
                   1731:
1.28      schwarze 1732:                        if ('\0' == *sec) {
1.11      schwarze 1733:                                if(0 == strncmp("man", fn, 3)) {
                   1734:                                        src_form |= MANDOC_SRC;
1.6       schwarze 1735:                                        sec = fn + 3;
1.11      schwarze 1736:                                } else if (0 == strncmp("cat", fn, 3)) {
                   1737:                                        src_form |= MANDOC_FORM;
                   1738:                                        sec = fn + 3;
1.28      schwarze 1739:                                } else {
                   1740:                                        if (warnings) fprintf(stderr,
                   1741:                                            "%s/%s: bad section\n",
                   1742:                                            dir, fn);
                   1743:                                        if (use_all)
                   1744:                                                sec = fn;
                   1745:                                        else
                   1746:                                                continue;
                   1747:                                }
                   1748:                        } else if ('\0' == *arch) {
                   1749:                                if (NULL != strchr(fn, '.')) {
                   1750:                                        if (warnings) fprintf(stderr,
                   1751:                                            "%s/%s: bad architecture\n",
                   1752:                                            dir, fn);
                   1753:                                        if (0 == use_all)
                   1754:                                                continue;
                   1755:                                }
                   1756:                                arch = fn;
                   1757:                        } else {
                   1758:                                if (warnings) fprintf(stderr, "%s/%s: "
                   1759:                                    "excessive subdirectory\n", dir, fn);
                   1760:                                if (0 == use_all)
1.6       schwarze 1761:                                        continue;
1.28      schwarze 1762:                        }
1.2       schwarze 1763:
                   1764:                        buf[0] = '\0';
                   1765:                        strlcat(buf, dir, MAXPATHLEN);
                   1766:                        strlcat(buf, "/", MAXPATHLEN);
                   1767:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1768:
1.6       schwarze 1769:                        if (MAXPATHLEN <= sz) {
1.28      schwarze 1770:                                if (warnings) fprintf(stderr, "%s/%s: "
                   1771:                                    "path too long\n", dir, fn);
                   1772:                                continue;
1.6       schwarze 1773:                        }
1.28      schwarze 1774:
                   1775:                        if (verb > 1)
                   1776:                                printf("%s: scanning\n", buf);
1.6       schwarze 1777:
1.26      schwarze 1778:                        ofile_dirbuild(buf, sec, arch, src_form, of);
1.28      schwarze 1779:                        continue;
1.6       schwarze 1780:                }
1.26      schwarze 1781:
1.28      schwarze 1782:                if (DT_REG != dp->d_type) {
                   1783:                        if (warnings)
                   1784:                                fprintf(stderr,
                   1785:                                    "%s/%s: not a regular file\n",
                   1786:                                    dir, fn);
                   1787:                        continue;
                   1788:                }
                   1789:                if (!strcmp(MANDOC_DB, fn) || !strcmp(MANDOC_IDX, fn))
1.6       schwarze 1790:                        continue;
1.28      schwarze 1791:                if ('\0' == *psec) {
                   1792:                        if (warnings)
                   1793:                                fprintf(stderr,
                   1794:                                    "%s/%s: file outside section\n",
                   1795:                                    dir, fn);
                   1796:                        if (0 == use_all)
                   1797:                                continue;
                   1798:                }
1.6       schwarze 1799:
                   1800:                /*
1.8       schwarze 1801:                 * By default, skip files where the file name suffix
                   1802:                 * does not agree with the section directory
                   1803:                 * they are located in.
1.6       schwarze 1804:                 */
                   1805:
                   1806:                suffix = strrchr(fn, '.');
1.28      schwarze 1807:                if (NULL == suffix) {
                   1808:                        if (warnings)
                   1809:                                fprintf(stderr,
                   1810:                                    "%s/%s: no filename suffix\n",
                   1811:                                    dir, fn);
                   1812:                        if (0 == use_all)
1.2       schwarze 1813:                                continue;
1.28      schwarze 1814:                } else if ((MANDOC_SRC & src_form &&
                   1815:                                strcmp(suffix + 1, psec)) ||
1.11      schwarze 1816:                            (MANDOC_FORM & src_form &&
1.28      schwarze 1817:                                strcmp(suffix + 1, "0"))) {
                   1818:                        if (warnings)
                   1819:                                fprintf(stderr,
                   1820:                                    "%s/%s: wrong filename suffix\n",
                   1821:                                    dir, fn);
                   1822:                        if (0 == use_all)
                   1823:                                continue;
1.11      schwarze 1824:                        if ('0' == suffix[1])
                   1825:                                src_form |= MANDOC_FORM;
                   1826:                        else if ('1' <= suffix[1] && '9' >= suffix[1])
                   1827:                                src_form |= MANDOC_SRC;
                   1828:                }
                   1829:
                   1830:                /*
                   1831:                 * Skip formatted manuals if a source version is
                   1832:                 * available.  Ignore the age: it is very unlikely
                   1833:                 * that people install newer formatted base manuals
                   1834:                 * when they used to have source manuals before,
                   1835:                 * and in ports, old manuals get removed on update.
                   1836:                 */
                   1837:                if (0 == use_all && MANDOC_FORM & src_form &&
1.28      schwarze 1838:                                '\0' != *psec) {
1.11      schwarze 1839:                        buf[0] = '\0';
                   1840:                        strlcat(buf, dir, MAXPATHLEN);
                   1841:                        p = strrchr(buf, '/');
1.28      schwarze 1842:                        if ('\0' != *parch && NULL != p)
1.23      schwarze 1843:                                for (p--; p > buf; p--)
                   1844:                                        if ('/' == *p)
                   1845:                                                break;
1.11      schwarze 1846:                        if (NULL == p)
                   1847:                                p = buf;
                   1848:                        else
                   1849:                                p++;
                   1850:                        if (0 == strncmp("cat", p, 3))
                   1851:                                memcpy(p, "man", 3);
                   1852:                        strlcat(buf, "/", MAXPATHLEN);
                   1853:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1854:                        if (sz >= MAXPATHLEN) {
1.28      schwarze 1855:                                if (warnings) fprintf(stderr,
                   1856:                                    "%s/%s: path too long\n",
                   1857:                                    dir, fn);
1.2       schwarze 1858:                                continue;
1.11      schwarze 1859:                        }
                   1860:                        q = strrchr(buf, '.');
                   1861:                        if (NULL != q && p < q++) {
                   1862:                                *q = '\0';
                   1863:                                sz = strlcat(buf, psec, MAXPATHLEN);
                   1864:                                if (sz >= MAXPATHLEN) {
1.28      schwarze 1865:                                        if (warnings) fprintf(stderr,
                   1866:                                            "%s/%s: path too long\n",
                   1867:                                            dir, fn);
1.11      schwarze 1868:                                        continue;
                   1869:                                }
1.26      schwarze 1870:                                if (0 == access(buf, R_OK))
1.11      schwarze 1871:                                        continue;
                   1872:                        }
1.2       schwarze 1873:                }
                   1874:
1.28      schwarze 1875:                buf[0] = '\0';
1.26      schwarze 1876:                assert('.' == dir[0]);
1.28      schwarze 1877:                if ('/' == dir[1]) {
                   1878:                        strlcat(buf, dir + 2, MAXPATHLEN);
                   1879:                        strlcat(buf, "/", MAXPATHLEN);
                   1880:                }
1.2       schwarze 1881:                sz = strlcat(buf, fn, MAXPATHLEN);
                   1882:                if (sz >= MAXPATHLEN) {
1.28      schwarze 1883:                        if (warnings) fprintf(stderr,
                   1884:                            "%s/%s: path too long\n", dir, fn);
1.11      schwarze 1885:                        continue;
1.2       schwarze 1886:                }
                   1887:
                   1888:                nof = mandoc_calloc(1, sizeof(struct of));
                   1889:                nof->fname = mandoc_strdup(buf);
1.28      schwarze 1890:                nof->sec = mandoc_strdup(psec);
                   1891:                nof->arch = mandoc_strdup(parch);
1.11      schwarze 1892:                nof->src_form = src_form;
1.8       schwarze 1893:
                   1894:                /*
                   1895:                 * Remember the file name without the extension,
                   1896:                 * to be used as the page title in the database.
                   1897:                 */
                   1898:
1.6       schwarze 1899:                if (NULL != suffix)
                   1900:                        *suffix = '\0';
                   1901:                nof->title = mandoc_strdup(fn);
1.2       schwarze 1902:
1.11      schwarze 1903:                /*
                   1904:                 * Add the structure to the list.
                   1905:                 */
                   1906:
1.28      schwarze 1907:                if (verb > 1)
                   1908:                        printf("%s: scheduling\n", buf);
1.31      schwarze 1909:
1.2       schwarze 1910:                if (NULL == *of) {
                   1911:                        *of = nof;
                   1912:                        (*of)->first = nof;
                   1913:                } else {
                   1914:                        nof->first = (*of)->first;
                   1915:                        (*of)->next = nof;
                   1916:                        *of = nof;
                   1917:                }
                   1918:        }
                   1919:
1.4       schwarze 1920:        closedir(d);
1.2       schwarze 1921: }
                   1922:
                   1923: static void
                   1924: ofile_free(struct of *of)
                   1925: {
                   1926:        struct of       *nof;
                   1927:
1.31      schwarze 1928:        if (NULL != of)
                   1929:                of = of->first;
                   1930:
                   1931:        while (NULL != of) {
1.2       schwarze 1932:                nof = of->next;
                   1933:                free(of->fname);
1.6       schwarze 1934:                free(of->sec);
                   1935:                free(of->arch);
                   1936:                free(of->title);
1.2       schwarze 1937:                free(of);
                   1938:                of = nof;
                   1939:        }
1.1       schwarze 1940: }