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

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