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

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