[BACK]Return to mandocdb.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/mandocdb.c, Revision 1.49.2.6

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

CVSweb