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

Annotation of mandoc/mandocdb.c, Revision 1.35

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

CVSweb