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

Annotation of mandoc/mandocdb.c, Revision 1.37

1.37    ! schwarze    1: /*     $Id: mandocdb.c,v 1.36 2011/12/16 12:06:35 kristaps 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;
1.37    ! schwarze  528:        uint64_t         mask;
1.3       kristaps  529:        size_t           sv;
                    530:        unsigned         seq;
1.9       kristaps  531:        struct db_val    vbuf;
1.36      kristaps  532:        char             type;
1.3       kristaps  533:
                    534:        for (rec = 0; of; of = of->next) {
                    535:                fn = of->fname;
1.14      schwarze  536:
                    537:                /*
1.33      schwarze  538:                 * Try interpreting the file as mdoc(7) or man(7)
                    539:                 * source code, unless it is already known to be
                    540:                 * formatted.  Fall back to formatted mode.
1.14      schwarze  541:                 */
                    542:
1.1       kristaps  543:                mparse_reset(mp);
1.14      schwarze  544:                mdoc = NULL;
                    545:                man = NULL;
1.1       kristaps  546:
1.14      schwarze  547:                if ((MANDOC_SRC & of->src_form ||
                    548:                    ! (MANDOC_FORM & of->src_form)) &&
                    549:                    MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn))
                    550:                        mparse_result(mp, &mdoc, &man);
                    551:
                    552:                if (NULL != mdoc) {
                    553:                        msec = mdoc_meta(mdoc)->msec;
                    554:                        arch = mdoc_meta(mdoc)->arch;
                    555:                        mtitle = mdoc_meta(mdoc)->title;
                    556:                } else if (NULL != man) {
                    557:                        msec = man_meta(man)->msec;
                    558:                        arch = NULL;
                    559:                        mtitle = man_meta(man)->title;
                    560:                } else {
                    561:                        msec = of->sec;
                    562:                        arch = of->arch;
                    563:                        mtitle = of->title;
1.1       kristaps  564:                }
                    565:
1.12      schwarze  566:                /*
                    567:                 * By default, skip a file if the manual section
                    568:                 * and architecture given in the file disagree
                    569:                 * with the directory where the file is located.
                    570:                 */
                    571:
                    572:                if (0 == use_all) {
                    573:                        assert(of->sec);
                    574:                        assert(msec);
1.32      schwarze  575:                        if (strcasecmp(msec, of->sec))
1.12      schwarze  576:                                continue;
                    577:
                    578:                        if (NULL == arch) {
                    579:                                if (NULL != of->arch)
                    580:                                        continue;
                    581:                        } else if (NULL == of->arch ||
1.32      schwarze  582:                                        strcasecmp(arch, of->arch))
1.12      schwarze  583:                                continue;
                    584:                }
                    585:
1.1       kristaps  586:                if (NULL == arch)
                    587:                        arch = "";
                    588:
                    589:                /*
1.12      schwarze  590:                 * By default, skip a file if the title given
                    591:                 * in the file disagrees with the file name.
                    592:                 * If both agree, use the file name as the title,
                    593:                 * because the one in the file usually is all caps.
                    594:                 */
                    595:
                    596:                assert(of->title);
                    597:                assert(mtitle);
                    598:
                    599:                if (0 == strcasecmp(mtitle, of->title))
                    600:                        mtitle = of->title;
                    601:                else if (0 == use_all)
                    602:                        continue;
                    603:
                    604:                /*
1.1       kristaps  605:                 * The index record value consists of a nil-terminated
                    606:                 * filename, a nil-terminated manual section, and a
                    607:                 * nil-terminated description.  Since the description
                    608:                 * may not be set, we set a sentinel to see if we're
                    609:                 * going to write a nil byte in its place.
                    610:                 */
                    611:
1.3       kristaps  612:                dbuf->len = 0;
1.36      kristaps  613:                type = mdoc ? 'd' : (man ? 'a' : 'c');
                    614:                buf_appendb(dbuf, &type, 1);
1.3       kristaps  615:                buf_appendb(dbuf, fn, strlen(fn) + 1);
                    616:                buf_appendb(dbuf, msec, strlen(msec) + 1);
                    617:                buf_appendb(dbuf, mtitle, strlen(mtitle) + 1);
                    618:                buf_appendb(dbuf, arch, strlen(arch) + 1);
1.1       kristaps  619:
1.3       kristaps  620:                sv = dbuf->len;
1.1       kristaps  621:
1.33      schwarze  622:                /*
                    623:                 * Collect keyword/mask pairs.
                    624:                 * Each pair will become a new btree node.
                    625:                 */
1.1       kristaps  626:
1.33      schwarze  627:                hash_reset(&hash);
1.1       kristaps  628:                if (mdoc)
1.3       kristaps  629:                        pmdoc_node(hash, buf, dbuf,
1.1       kristaps  630:                                mdoc_node(mdoc), mdoc_meta(mdoc));
1.14      schwarze  631:                else if (man)
1.3       kristaps  632:                        pman_node(hash, buf, dbuf, man_node(man));
1.14      schwarze  633:                else
                    634:                        pformatted(hash, buf, dbuf, of);
1.1       kristaps  635:
                    636:                /*
1.33      schwarze  637:                 * Reclaim an empty index record, if available.
                    638:                 * Use its record number for all new btree nodes.
1.1       kristaps  639:                 */
                    640:
1.33      schwarze  641:                if (reccur > 0) {
                    642:                        --reccur;
                    643:                        rec = recs[(int)reccur];
                    644:                } else if (maxrec > 0) {
                    645:                        rec = maxrec;
                    646:                        maxrec = 0;
                    647:                } else
                    648:                        rec++;
1.20      kristaps  649:                vbuf.rec = htobe32(rec);
1.33      schwarze  650:
                    651:                /*
                    652:                 * Copy from the in-memory hashtable of pending
                    653:                 * keyword/mask pairs into the database.
                    654:                 */
                    655:
1.1       kristaps  656:                seq = R_FIRST;
                    657:                while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
                    658:                        seq = R_NEXT;
1.37    ! schwarze  659:                        assert(sizeof(uint64_t) == val.size);
        !           660:                        memcpy(&mask, val.data, val.size);
        !           661:                        vbuf.mask = htobe64(mask);
1.9       kristaps  662:                        val.size = sizeof(struct db_val);
                    663:                        val.data = &vbuf;
1.3       kristaps  664:                        dbt_put(db, dbf, &key, &val);
1.1       kristaps  665:                }
                    666:                if (ch < 0) {
                    667:                        perror("hash");
                    668:                        exit((int)MANDOCLEVEL_SYSERR);
                    669:                }
                    670:
                    671:                /*
                    672:                 * Apply to the index.  If we haven't had a description
                    673:                 * set, put an empty one in now.
                    674:                 */
                    675:
1.3       kristaps  676:                if (dbuf->len == sv)
                    677:                        buf_appendb(dbuf, "", 1);
1.1       kristaps  678:
                    679:                key.data = &rec;
                    680:                key.size = sizeof(recno_t);
                    681:
1.3       kristaps  682:                val.data = dbuf->cp;
                    683:                val.size = dbuf->len;
1.1       kristaps  684:
1.5       kristaps  685:                if (verb)
                    686:                        printf("%s: Added index\n", fn);
1.18      kristaps  687:
1.3       kristaps  688:                dbt_put(idx, idxf, &key, &val);
                    689:        }
                    690: }
                    691:
                    692: /*
                    693:  * Scan through all entries in the index file `idx' and prune those
                    694:  * entries in `ofile'.
                    695:  * Pruning consists of removing from `db', then invalidating the entry
                    696:  * in `idx' (zeroing its value size).
                    697:  */
                    698: static void
                    699: index_prune(const struct of *ofile, DB *db, const char *dbf,
1.27      schwarze  700:                DB *idx, const char *idxf, recno_t *maxrec,
                    701:                recno_t **recs, size_t *recsz, size_t *reccur)
1.3       kristaps  702: {
                    703:        const struct of *of;
1.36      kristaps  704:        const char      *fn;
1.9       kristaps  705:        struct db_val   *vbuf;
1.3       kristaps  706:        unsigned         seq, sseq;
                    707:        DBT              key, val;
                    708:        int              ch;
                    709:
1.27      schwarze  710:        *reccur = 0;
1.3       kristaps  711:        seq = R_FIRST;
                    712:        while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
                    713:                seq = R_NEXT;
1.37    ! schwarze  714:                assert(sizeof(recno_t) == key.size);
        !           715:                memcpy(maxrec, key.data, key.size);
1.18      kristaps  716:
                    717:                /* Deleted records are zero-sized.  Skip them. */
                    718:
                    719:                if (0 == val.size)
                    720:                        goto cont;
                    721:
                    722:                /*
                    723:                 * Make sure we're sane.
                    724:                 * Read past our mdoc/man/cat type to the next string,
                    725:                 * then make sure it's bounded by a NUL.
                    726:                 * Failing any of these, we go into our error handler.
                    727:                 */
                    728:
1.36      kristaps  729:                fn = (char *)val.data + 1;
                    730:                if (NULL == memchr(fn, '\0', val.size - 1))
1.18      kristaps  731:                        break;
                    732:
                    733:                /*
                    734:                 * Search for the file in those we care about.
                    735:                 * XXX: build this into a tree.  Too slow.
                    736:                 */
1.3       kristaps  737:
                    738:                for (of = ofile; of; of = of->next)
                    739:                        if (0 == strcmp(fn, of->fname))
                    740:                                break;
                    741:
                    742:                if (NULL == of)
                    743:                        continue;
                    744:
1.18      kristaps  745:                /*
                    746:                 * Search through the keyword database, throwing out all
                    747:                 * references to our file.
                    748:                 */
                    749:
1.3       kristaps  750:                sseq = R_FIRST;
                    751:                while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) {
                    752:                        sseq = R_NEXT;
1.18      kristaps  753:                        if (sizeof(struct db_val) != val.size)
                    754:                                break;
                    755:
1.9       kristaps  756:                        vbuf = val.data;
1.20      kristaps  757:                        if (*maxrec != betoh32(vbuf->rec))
1.3       kristaps  758:                                continue;
1.18      kristaps  759:
                    760:                        if ((ch = (*db->del)(db, &key, R_CURSOR)) < 0)
1.3       kristaps  761:                                break;
                    762:                }
1.18      kristaps  763:
1.3       kristaps  764:                if (ch < 0) {
                    765:                        perror(dbf);
                    766:                        exit((int)MANDOCLEVEL_SYSERR);
1.18      kristaps  767:                } else if (1 != ch) {
                    768:                        fprintf(stderr, "%s: Corrupt database\n", dbf);
                    769:                        exit((int)MANDOCLEVEL_SYSERR);
1.3       kristaps  770:                }
1.1       kristaps  771:
1.5       kristaps  772:                if (verb)
                    773:                        printf("%s: Deleted index\n", fn);
1.1       kristaps  774:
1.3       kristaps  775:                val.size = 0;
                    776:                ch = (*idx->put)(idx, &key, &val, R_CURSOR);
1.1       kristaps  777:
1.18      kristaps  778:                if (ch < 0)
                    779:                        break;
                    780: cont:
1.27      schwarze  781:                if (*reccur >= *recsz) {
1.3       kristaps  782:                        *recsz += MANDOC_SLOP;
                    783:                        *recs = mandoc_realloc
                    784:                                (*recs, *recsz * sizeof(recno_t));
                    785:                }
1.1       kristaps  786:
1.27      schwarze  787:                (*recs)[(int)*reccur] = *maxrec;
                    788:                (*reccur)++;
1.3       kristaps  789:        }
1.18      kristaps  790:
                    791:        if (ch < 0) {
                    792:                perror(idxf);
                    793:                exit((int)MANDOCLEVEL_SYSERR);
                    794:        } else if (1 != ch) {
                    795:                fprintf(stderr, "%s: Corrupt index\n", idxf);
                    796:                exit((int)MANDOCLEVEL_SYSERR);
                    797:        }
                    798:
1.3       kristaps  799:        (*maxrec)++;
1.1       kristaps  800: }
                    801:
                    802: /*
                    803:  * Grow the buffer (if necessary) and copy in a binary string.
                    804:  */
                    805: static void
                    806: buf_appendb(struct buf *buf, const void *cp, size_t sz)
                    807: {
                    808:
                    809:        /* Overshoot by MANDOC_BUFSZ. */
                    810:
                    811:        while (buf->len + sz >= buf->size) {
                    812:                buf->size = buf->len + sz + MANDOC_BUFSZ;
                    813:                buf->cp = mandoc_realloc(buf->cp, buf->size);
                    814:        }
                    815:
                    816:        memcpy(buf->cp + (int)buf->len, cp, sz);
                    817:        buf->len += sz;
                    818: }
                    819:
                    820: /*
                    821:  * Append a nil-terminated string to the buffer.
                    822:  * This can be invoked multiple times.
                    823:  * The buffer string will be nil-terminated.
                    824:  * If invoked multiple times, a space is put between strings.
                    825:  */
                    826: static void
                    827: buf_append(struct buf *buf, const char *cp)
                    828: {
                    829:        size_t           sz;
                    830:
                    831:        if (0 == (sz = strlen(cp)))
                    832:                return;
                    833:
                    834:        if (buf->len)
                    835:                buf->cp[(int)buf->len - 1] = ' ';
                    836:
                    837:        buf_appendb(buf, cp, sz + 1);
                    838: }
                    839:
                    840: /*
                    841:  * Recursively add all text from a given node.
                    842:  * This is optimised for general mdoc nodes in this context, which do
                    843:  * not consist of subexpressions and having a recursive call for n->next
                    844:  * would be wasteful.
                    845:  * The "f" variable should be 0 unless called from pmdoc_Nd for the
                    846:  * description buffer, which does not start at the beginning of the
                    847:  * buffer.
                    848:  */
                    849: static void
                    850: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
                    851: {
                    852:
                    853:        for ( ; n; n = n->next) {
                    854:                if (n->child)
                    855:                        buf_appendmdoc(buf, n->child, f);
                    856:
                    857:                if (MDOC_TEXT == n->type && f) {
                    858:                        f = 0;
                    859:                        buf_appendb(buf, n->string,
                    860:                                        strlen(n->string) + 1);
                    861:                } else if (MDOC_TEXT == n->type)
                    862:                        buf_append(buf, n->string);
                    863:
                    864:        }
                    865: }
                    866:
                    867: static void
                    868: hash_reset(DB **db)
                    869: {
                    870:        DB              *hash;
                    871:
                    872:        if (NULL != (hash = *db))
                    873:                (*hash->close)(hash);
                    874:
1.5       kristaps  875:        *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1.1       kristaps  876:        if (NULL == *db) {
                    877:                perror("hash");
                    878:                exit((int)MANDOCLEVEL_SYSERR);
                    879:        }
                    880: }
                    881:
                    882: /* ARGSUSED */
1.25      schwarze  883: static int
                    884: pmdoc_head(MDOC_ARGS)
                    885: {
                    886:
                    887:        return(MDOC_HEAD == n->type);
                    888: }
                    889:
                    890: /* ARGSUSED */
                    891: static int
                    892: pmdoc_body(MDOC_ARGS)
                    893: {
                    894:
                    895:        return(MDOC_BODY == n->type);
                    896: }
                    897:
                    898: /* ARGSUSED */
                    899: static int
1.1       kristaps  900: pmdoc_Fd(MDOC_ARGS)
                    901: {
                    902:        const char      *start, *end;
                    903:        size_t           sz;
1.25      schwarze  904:
1.1       kristaps  905:        if (SEC_SYNOPSIS != n->sec)
1.25      schwarze  906:                return(0);
1.1       kristaps  907:        if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.25      schwarze  908:                return(0);
1.1       kristaps  909:
                    910:        /*
                    911:         * Only consider those `Fd' macro fields that begin with an
                    912:         * "inclusion" token (versus, e.g., #define).
                    913:         */
                    914:        if (strcmp("#include", n->string))
1.25      schwarze  915:                return(0);
1.1       kristaps  916:
                    917:        if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.25      schwarze  918:                return(0);
1.1       kristaps  919:
                    920:        /*
                    921:         * Strip away the enclosing angle brackets and make sure we're
                    922:         * not zero-length.
                    923:         */
                    924:
                    925:        start = n->string;
                    926:        if ('<' == *start || '"' == *start)
                    927:                start++;
                    928:
                    929:        if (0 == (sz = strlen(start)))
1.25      schwarze  930:                return(0);
1.1       kristaps  931:
                    932:        end = &start[(int)sz - 1];
                    933:        if ('>' == *end || '"' == *end)
                    934:                end--;
                    935:
                    936:        assert(end >= start);
                    937:
                    938:        buf_appendb(buf, start, (size_t)(end - start + 1));
                    939:        buf_appendb(buf, "", 1);
1.25      schwarze  940:        return(1);
1.1       kristaps  941: }
                    942:
                    943: /* ARGSUSED */
1.25      schwarze  944: static int
                    945: pmdoc_In(MDOC_ARGS)
1.1       kristaps  946: {
                    947:
                    948:        if (NULL == n->child || MDOC_TEXT != n->child->type)
1.25      schwarze  949:                return(0);
1.1       kristaps  950:
                    951:        buf_append(buf, n->child->string);
1.25      schwarze  952:        return(1);
1.1       kristaps  953: }
                    954:
                    955: /* ARGSUSED */
1.25      schwarze  956: static int
1.1       kristaps  957: pmdoc_Fn(MDOC_ARGS)
                    958: {
1.25      schwarze  959:        struct mdoc_node *nn;
1.1       kristaps  960:        const char      *cp;
                    961:
1.25      schwarze  962:        nn = n->child;
                    963:
                    964:        if (NULL == nn || MDOC_TEXT != nn->type)
                    965:                return(0);
                    966:
                    967:        /* .Fn "struct type *name" "char *arg" */
1.1       kristaps  968:
1.25      schwarze  969:        cp = strrchr(nn->string, ' ');
1.1       kristaps  970:        if (NULL == cp)
1.25      schwarze  971:                cp = nn->string;
1.1       kristaps  972:
                    973:        /* Strip away pointer symbol. */
                    974:
                    975:        while ('*' == *cp)
                    976:                cp++;
                    977:
1.25      schwarze  978:        /* Store the function name. */
                    979:
1.1       kristaps  980:        buf_append(buf, cp);
1.8       schwarze  981:        hash_put(hash, buf, TYPE_Fn);
1.25      schwarze  982:
                    983:        /* Store the function type. */
                    984:
                    985:        if (nn->string < cp) {
                    986:                buf->len = 0;
                    987:                buf_appendb(buf, nn->string, cp - nn->string);
                    988:                buf_appendb(buf, "", 1);
                    989:                hash_put(hash, buf, TYPE_Ft);
                    990:        }
                    991:
                    992:        /* Store the arguments. */
                    993:
                    994:        for (nn = nn->next; nn; nn = nn->next) {
                    995:                if (MDOC_TEXT != nn->type)
                    996:                        continue;
                    997:                buf->len = 0;
                    998:                buf_append(buf, nn->string);
                    999:                hash_put(hash, buf, TYPE_Fa);
                   1000:        }
                   1001:
                   1002:        return(0);
1.1       kristaps 1003: }
                   1004:
                   1005: /* ARGSUSED */
1.25      schwarze 1006: static int
1.1       kristaps 1007: pmdoc_St(MDOC_ARGS)
                   1008: {
1.25      schwarze 1009:
1.1       kristaps 1010:        if (NULL == n->child || MDOC_TEXT != n->child->type)
1.25      schwarze 1011:                return(0);
1.1       kristaps 1012:
                   1013:        buf_append(buf, n->child->string);
1.25      schwarze 1014:        return(1);
1.1       kristaps 1015: }
                   1016:
                   1017: /* ARGSUSED */
1.25      schwarze 1018: static int
1.1       kristaps 1019: pmdoc_Xr(MDOC_ARGS)
                   1020: {
                   1021:
                   1022:        if (NULL == (n = n->child))
1.25      schwarze 1023:                return(0);
1.1       kristaps 1024:
                   1025:        buf_appendb(buf, n->string, strlen(n->string));
                   1026:
                   1027:        if (NULL != (n = n->next)) {
                   1028:                buf_appendb(buf, ".", 1);
                   1029:                buf_appendb(buf, n->string, strlen(n->string) + 1);
                   1030:        } else
                   1031:                buf_appendb(buf, ".", 2);
                   1032:
1.25      schwarze 1033:        return(1);
1.1       kristaps 1034: }
                   1035:
                   1036: /* ARGSUSED */
1.25      schwarze 1037: static int
1.1       kristaps 1038: pmdoc_Nd(MDOC_ARGS)
                   1039: {
                   1040:
                   1041:        if (MDOC_BODY != n->type)
1.25      schwarze 1042:                return(0);
1.1       kristaps 1043:
                   1044:        buf_appendmdoc(dbuf, n->child, 1);
1.25      schwarze 1045:        return(1);
1.1       kristaps 1046: }
                   1047:
                   1048: /* ARGSUSED */
1.25      schwarze 1049: static int
                   1050: pmdoc_Nm(MDOC_ARGS)
1.1       kristaps 1051: {
                   1052:
1.25      schwarze 1053:        if (SEC_NAME == n->sec)
                   1054:                return(1);
                   1055:        else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1056:                return(0);
1.1       kristaps 1057:
1.25      schwarze 1058:        if (NULL == n->child)
                   1059:                buf_append(buf, m->name);
1.1       kristaps 1060:
1.25      schwarze 1061:        return(1);
1.1       kristaps 1062: }
                   1063:
                   1064: /* ARGSUSED */
1.25      schwarze 1065: static int
                   1066: pmdoc_Sh(MDOC_ARGS)
1.1       kristaps 1067: {
                   1068:
1.25      schwarze 1069:        return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1       kristaps 1070: }
                   1071:
                   1072: static void
1.9       kristaps 1073: hash_put(DB *db, const struct buf *buf, uint64_t mask)
1.1       kristaps 1074: {
1.37    ! schwarze 1075:        uint64_t         oldmask;
1.1       kristaps 1076:        DBT              key, val;
                   1077:        int              rc;
                   1078:
                   1079:        if (buf->len < 2)
                   1080:                return;
                   1081:
                   1082:        key.data = buf->cp;
                   1083:        key.size = buf->len;
                   1084:
                   1085:        if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
                   1086:                perror("hash");
                   1087:                exit((int)MANDOCLEVEL_SYSERR);
1.37    ! schwarze 1088:        } else if (0 == rc) {
        !          1089:                assert(sizeof(uint64_t) == val.size);
        !          1090:                memcpy(&oldmask, val.data, val.size);
        !          1091:                mask |= oldmask;
        !          1092:        }
1.1       kristaps 1093:
                   1094:        val.data = &mask;
1.9       kristaps 1095:        val.size = sizeof(uint64_t);
1.1       kristaps 1096:
                   1097:        if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
                   1098:                perror("hash");
                   1099:                exit((int)MANDOCLEVEL_SYSERR);
                   1100:        }
                   1101: }
                   1102:
                   1103: static void
                   1104: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
                   1105: {
                   1106:
                   1107:        assert(key->size);
                   1108:        assert(val->size);
                   1109:
                   1110:        if (0 == (*db->put)(db, key, val, 0))
                   1111:                return;
                   1112:
                   1113:        perror(dbn);
                   1114:        exit((int)MANDOCLEVEL_SYSERR);
                   1115:        /* NOTREACHED */
                   1116: }
                   1117:
                   1118: /*
                   1119:  * Call out to per-macro handlers after clearing the persistent database
                   1120:  * key.  If the macro sets the database key, flush it to the database.
                   1121:  */
                   1122: static void
                   1123: pmdoc_node(MDOC_ARGS)
                   1124: {
                   1125:
                   1126:        if (NULL == n)
                   1127:                return;
                   1128:
                   1129:        switch (n->type) {
                   1130:        case (MDOC_HEAD):
                   1131:                /* FALLTHROUGH */
                   1132:        case (MDOC_BODY):
                   1133:                /* FALLTHROUGH */
                   1134:        case (MDOC_TAIL):
                   1135:                /* FALLTHROUGH */
                   1136:        case (MDOC_BLOCK):
                   1137:                /* FALLTHROUGH */
                   1138:        case (MDOC_ELEM):
1.25      schwarze 1139:                buf->len = 0;
                   1140:
                   1141:                /*
                   1142:                 * Both NULL handlers and handlers returning true
                   1143:                 * request using the data.  Only skip the element
                   1144:                 * when the handler returns false.
                   1145:                 */
                   1146:
                   1147:                if (NULL != mdocs[n->tok].fp &&
                   1148:                    0 == (*mdocs[n->tok].fp)(hash, buf, dbuf, n, m))
1.1       kristaps 1149:                        break;
                   1150:
1.25      schwarze 1151:                /*
                   1152:                 * For many macros, use the text from all children.
                   1153:                 * Set zero flags for macros not needing this.
                   1154:                 * In that case, the handler must fill the buffer.
                   1155:                 */
                   1156:
                   1157:                if (MDOCF_CHILD & mdocs[n->tok].flags)
                   1158:                        buf_appendmdoc(buf, n->child, 0);
                   1159:
                   1160:                /*
                   1161:                 * Cover the most common case:
                   1162:                 * Automatically stage one string per element.
                   1163:                 * Set a zero mask for macros not needing this.
                   1164:                 * Additional staging can be done in the handler.
                   1165:                 */
                   1166:
                   1167:                if (mdocs[n->tok].mask)
                   1168:                        hash_put(hash, buf, mdocs[n->tok].mask);
1.1       kristaps 1169:                break;
                   1170:        default:
                   1171:                break;
                   1172:        }
                   1173:
                   1174:        pmdoc_node(hash, buf, dbuf, n->child, m);
                   1175:        pmdoc_node(hash, buf, dbuf, n->next, m);
                   1176: }
                   1177:
                   1178: static int
                   1179: pman_node(MAN_ARGS)
                   1180: {
                   1181:        const struct man_node *head, *body;
                   1182:        const char      *start, *sv;
                   1183:        size_t           sz;
                   1184:
                   1185:        if (NULL == n)
                   1186:                return(0);
                   1187:
                   1188:        /*
                   1189:         * We're only searching for one thing: the first text child in
                   1190:         * the BODY of a NAME section.  Since we don't keep track of
                   1191:         * sections in -man, run some hoops to find out whether we're in
                   1192:         * the correct section or not.
                   1193:         */
                   1194:
                   1195:        if (MAN_BODY == n->type && MAN_SH == n->tok) {
                   1196:                body = n;
                   1197:                assert(body->parent);
                   1198:                if (NULL != (head = body->parent->head) &&
                   1199:                                1 == head->nchild &&
                   1200:                                NULL != (head = (head->child)) &&
                   1201:                                MAN_TEXT == head->type &&
                   1202:                                0 == strcmp(head->string, "NAME") &&
                   1203:                                NULL != (body = body->child) &&
                   1204:                                MAN_TEXT == body->type) {
                   1205:
                   1206:                        assert(body->string);
                   1207:                        start = sv = body->string;
                   1208:
                   1209:                        /*
                   1210:                         * Go through a special heuristic dance here.
                   1211:                         * This is why -man manuals are great!
                   1212:                         * (I'm being sarcastic: my eyes are bleeding.)
                   1213:                         * Conventionally, one or more manual names are
                   1214:                         * comma-specified prior to a whitespace, then a
                   1215:                         * dash, then a description.  Try to puzzle out
                   1216:                         * the name parts here.
                   1217:                         */
                   1218:
                   1219:                        for ( ;; ) {
                   1220:                                sz = strcspn(start, " ,");
                   1221:                                if ('\0' == start[(int)sz])
                   1222:                                        break;
                   1223:
                   1224:                                buf->len = 0;
                   1225:                                buf_appendb(buf, start, sz);
                   1226:                                buf_appendb(buf, "", 1);
                   1227:
1.8       schwarze 1228:                                hash_put(hash, buf, TYPE_Nm);
1.1       kristaps 1229:
                   1230:                                if (' ' == start[(int)sz]) {
                   1231:                                        start += (int)sz + 1;
                   1232:                                        break;
                   1233:                                }
                   1234:
                   1235:                                assert(',' == start[(int)sz]);
                   1236:                                start += (int)sz + 1;
                   1237:                                while (' ' == *start)
                   1238:                                        start++;
                   1239:                        }
                   1240:
                   1241:                        buf->len = 0;
                   1242:
                   1243:                        if (sv == start) {
                   1244:                                buf_append(buf, start);
                   1245:                                return(1);
                   1246:                        }
                   1247:
                   1248:                        while (' ' == *start)
                   1249:                                start++;
                   1250:
                   1251:                        if (0 == strncmp(start, "-", 1))
                   1252:                                start += 1;
                   1253:                        else if (0 == strncmp(start, "\\-", 2))
                   1254:                                start += 2;
                   1255:                        else if (0 == strncmp(start, "\\(en", 4))
                   1256:                                start += 4;
                   1257:                        else if (0 == strncmp(start, "\\(em", 4))
                   1258:                                start += 4;
                   1259:
                   1260:                        while (' ' == *start)
                   1261:                                start++;
                   1262:
                   1263:                        sz = strlen(start) + 1;
                   1264:                        buf_appendb(dbuf, start, sz);
                   1265:                        buf_appendb(buf, start, sz);
                   1266:
1.8       schwarze 1267:                        hash_put(hash, buf, TYPE_Nd);
1.1       kristaps 1268:                }
                   1269:        }
                   1270:
1.7       schwarze 1271:        for (n = n->child; n; n = n->next)
                   1272:                if (pman_node(hash, buf, dbuf, n))
                   1273:                        return(1);
1.1       kristaps 1274:
                   1275:        return(0);
                   1276: }
                   1277:
1.14      schwarze 1278: /*
                   1279:  * Parse a formatted manual page.
                   1280:  * By necessity, this involves rather crude guesswork.
                   1281:  */
                   1282: static void
                   1283: pformatted(DB *hash, struct buf *buf, struct buf *dbuf,
                   1284:                 const struct of *of)
                   1285: {
                   1286:        FILE            *stream;
                   1287:        char            *line, *p;
                   1288:        size_t           len, plen;
                   1289:
                   1290:        if (NULL == (stream = fopen(of->fname, "r"))) {
                   1291:                perror(of->fname);
                   1292:                return;
                   1293:        }
                   1294:
                   1295:        /*
                   1296:         * Always use the title derived from the filename up front,
                   1297:         * do not even try to find it in the file.  This also makes
                   1298:         * sure we don't end up with an orphan index record, even if
                   1299:         * the file content turns out to be completely unintelligible.
                   1300:         */
                   1301:
                   1302:        buf->len = 0;
                   1303:        buf_append(buf, of->title);
                   1304:        hash_put(hash, buf, TYPE_Nm);
                   1305:
1.31      schwarze 1306:        /* Skip to first blank line. */
1.14      schwarze 1307:
1.28      kristaps 1308:        while (NULL != (line = fgetln(stream, &len)))
1.31      schwarze 1309:                if ('\n' == *line)
1.28      kristaps 1310:                        break;
                   1311:
1.31      schwarze 1312:        /*
                   1313:         * Assume the first line that is not indented
                   1314:         * is the first section header.  Skip to it.
1.28      kristaps 1315:         */
                   1316:
                   1317:        while (NULL != (line = fgetln(stream, &len)))
1.31      schwarze 1318:                if ('\n' != *line && ' ' != *line)
1.28      kristaps 1319:                        break;
1.14      schwarze 1320:
                   1321:        /*
1.31      schwarze 1322:         * If no page content can be found, or the input line
                   1323:         * is already the next section header, or there is no
                   1324:         * trailing newline, reuse the page title as the page
                   1325:         * description.
1.14      schwarze 1326:         */
                   1327:
1.28      kristaps 1328:        line = fgetln(stream, &len);
1.31      schwarze 1329:        if (NULL == line || ' ' != *line || '\n' != line[(int)len - 1]) {
1.14      schwarze 1330:                buf_appendb(dbuf, buf->cp, buf->size);
                   1331:                hash_put(hash, buf, TYPE_Nd);
                   1332:                fclose(stream);
                   1333:                return;
                   1334:        }
                   1335:
1.28      kristaps 1336:        line[(int)--len] = '\0';
                   1337:
1.31      schwarze 1338:        /*
                   1339:         * Skip to the first dash.
1.28      kristaps 1340:         * Use the remaining line as the description (no more than 70
                   1341:         * bytes).
1.14      schwarze 1342:         */
                   1343:
1.30      kristaps 1344:        if (NULL != (p = strstr(line, "- "))) {
                   1345:                for (p += 2; ' ' == *p || '\b' == *p; p++)
1.28      kristaps 1346:                        /* Skip to next word. */ ;
                   1347:        } else
1.14      schwarze 1348:                p = line;
1.28      kristaps 1349:
                   1350:        if ((plen = strlen(p)) > 70) {
                   1351:                plen = 70;
                   1352:                p[plen] = '\0';
1.29      kristaps 1353:        }
                   1354:
                   1355:        /* Strip backspace-encoding from line. */
                   1356:
                   1357:        while (NULL != (line = memchr(p, '\b', plen))) {
                   1358:                len = line - p;
                   1359:                if (0 == len) {
                   1360:                        memmove(line, line + 1, plen--);
                   1361:                        continue;
                   1362:                }
                   1363:                memmove(line - 1, line + 1, plen - len);
                   1364:                plen -= 2;
1.14      schwarze 1365:        }
                   1366:
1.28      kristaps 1367:        buf_appendb(dbuf, p, plen + 1);
1.14      schwarze 1368:        buf->len = 0;
1.28      kristaps 1369:        buf_appendb(buf, p, plen + 1);
1.14      schwarze 1370:        hash_put(hash, buf, TYPE_Nd);
1.28      kristaps 1371:        fclose(stream);
1.14      schwarze 1372: }
                   1373:
1.5       kristaps 1374: static void
1.16      schwarze 1375: ofile_argbuild(int argc, char *argv[], struct of **of)
1.5       kristaps 1376: {
1.12      schwarze 1377:        char             buf[MAXPATHLEN];
                   1378:        char            *sec, *arch, *title, *p;
1.14      schwarze 1379:        int              i, src_form;
1.5       kristaps 1380:        struct of       *nof;
                   1381:
                   1382:        for (i = 0; i < argc; i++) {
1.12      schwarze 1383:
                   1384:                /*
                   1385:                 * Try to infer the manual section, architecture and
                   1386:                 * page title from the path, assuming it looks like
1.14      schwarze 1387:                 *   man*[/<arch>]/<title>.<section>   or
                   1388:                 *   cat<section>[/<arch>]/<title>.0
1.12      schwarze 1389:                 */
                   1390:
                   1391:                if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {
                   1392:                        fprintf(stderr, "%s: Path too long\n", argv[i]);
                   1393:                        continue;
                   1394:                }
                   1395:                sec = arch = title = NULL;
1.14      schwarze 1396:                src_form = 0;
1.12      schwarze 1397:                p = strrchr(buf, '\0');
                   1398:                while (p-- > buf) {
                   1399:                        if (NULL == sec && '.' == *p) {
                   1400:                                sec = p + 1;
                   1401:                                *p = '\0';
1.14      schwarze 1402:                                if ('0' == *sec)
                   1403:                                        src_form |= MANDOC_FORM;
                   1404:                                else if ('1' <= *sec && '9' >= *sec)
                   1405:                                        src_form |= MANDOC_SRC;
1.12      schwarze 1406:                                continue;
                   1407:                        }
                   1408:                        if ('/' != *p)
                   1409:                                continue;
                   1410:                        if (NULL == title) {
                   1411:                                title = p + 1;
                   1412:                                *p = '\0';
                   1413:                                continue;
                   1414:                        }
1.24      schwarze 1415:                        if (0 == strncmp("man", p + 1, 3))
1.14      schwarze 1416:                                src_form |= MANDOC_SRC;
1.24      schwarze 1417:                        else if (0 == strncmp("cat", p + 1, 3))
1.14      schwarze 1418:                                src_form |= MANDOC_FORM;
1.24      schwarze 1419:                        else
1.12      schwarze 1420:                                arch = p + 1;
                   1421:                        break;
                   1422:                }
                   1423:                if (NULL == title)
                   1424:                        title = buf;
                   1425:
                   1426:                /*
                   1427:                 * Build the file structure.
                   1428:                 */
                   1429:
1.5       kristaps 1430:                nof = mandoc_calloc(1, sizeof(struct of));
1.12      schwarze 1431:                nof->fname = mandoc_strdup(argv[i]);
                   1432:                if (NULL != sec)
                   1433:                        nof->sec = mandoc_strdup(sec);
                   1434:                if (NULL != arch)
                   1435:                        nof->arch = mandoc_strdup(arch);
                   1436:                nof->title = mandoc_strdup(title);
1.14      schwarze 1437:                nof->src_form = src_form;
1.12      schwarze 1438:
                   1439:                /*
                   1440:                 * Add the structure to the list.
                   1441:                 */
                   1442:
1.5       kristaps 1443:                if (NULL == *of) {
                   1444:                        *of = nof;
                   1445:                        (*of)->first = nof;
                   1446:                } else {
                   1447:                        nof->first = (*of)->first;
                   1448:                        (*of)->next = nof;
                   1449:                        *of = nof;
                   1450:                }
                   1451:        }
                   1452: }
                   1453:
1.4       kristaps 1454: /*
                   1455:  * Recursively build up a list of files to parse.
                   1456:  * We use this instead of ftw() and so on because I don't want global
                   1457:  * variables hanging around.
                   1458:  * This ignores the mandoc.db and mandoc.index files, but assumes that
                   1459:  * everything else is a manual.
                   1460:  * Pass in a pointer to a NULL structure for the first invocation.
                   1461:  */
1.35      kristaps 1462: static void
1.12      schwarze 1463: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1.16      schwarze 1464:                int p_src_form, struct of **of)
1.4       kristaps 1465: {
1.5       kristaps 1466:        char             buf[MAXPATHLEN];
                   1467:        size_t           sz;
1.4       kristaps 1468:        DIR             *d;
1.12      schwarze 1469:        const char      *fn, *sec, *arch;
1.14      schwarze 1470:        char            *p, *q, *suffix;
1.4       kristaps 1471:        struct of       *nof;
                   1472:        struct dirent   *dp;
1.14      schwarze 1473:        int              src_form;
1.4       kristaps 1474:
                   1475:        if (NULL == (d = opendir(dir))) {
                   1476:                perror(dir);
1.35      kristaps 1477:                exit((int)MANDOCLEVEL_SYSERR);
1.4       kristaps 1478:        }
                   1479:
                   1480:        while (NULL != (dp = readdir(d))) {
                   1481:                fn = dp->d_name;
1.12      schwarze 1482:
                   1483:                if ('.' == *fn)
                   1484:                        continue;
                   1485:
1.14      schwarze 1486:                src_form = p_src_form;
                   1487:
1.4       kristaps 1488:                if (DT_DIR == dp->d_type) {
1.12      schwarze 1489:                        sec = psec;
                   1490:                        arch = parch;
                   1491:
                   1492:                        /*
                   1493:                         * By default, only use directories called:
1.14      schwarze 1494:                         *   man<section>/[<arch>/]   or
                   1495:                         *   cat<section>/[<arch>/]
1.12      schwarze 1496:                         */
                   1497:
                   1498:                        if (NULL == sec) {
1.14      schwarze 1499:                                if(0 == strncmp("man", fn, 3)) {
                   1500:                                        src_form |= MANDOC_SRC;
1.12      schwarze 1501:                                        sec = fn + 3;
1.14      schwarze 1502:                                } else if (0 == strncmp("cat", fn, 3)) {
                   1503:                                        src_form |= MANDOC_FORM;
                   1504:                                        sec = fn + 3;
                   1505:                                } else if (use_all)
1.12      schwarze 1506:                                        sec = fn;
                   1507:                                else
                   1508:                                        continue;
                   1509:                        } else if (NULL == arch && (use_all ||
                   1510:                                        NULL == strchr(fn, '.')))
                   1511:                                arch = fn;
                   1512:                        else if (0 == use_all)
1.5       kristaps 1513:                                continue;
                   1514:
                   1515:                        buf[0] = '\0';
                   1516:                        strlcat(buf, dir, MAXPATHLEN);
                   1517:                        strlcat(buf, "/", MAXPATHLEN);
                   1518:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1519:
1.12      schwarze 1520:                        if (MAXPATHLEN <= sz) {
                   1521:                                fprintf(stderr, "%s: Path too long\n", dir);
1.35      kristaps 1522:                                exit((int)MANDOCLEVEL_SYSERR);
1.12      schwarze 1523:                        }
                   1524:
1.35      kristaps 1525:                        ofile_dirbuild(buf, sec, arch, src_form, of);
                   1526:                }
1.12      schwarze 1527:
                   1528:                if (DT_REG != dp->d_type ||
1.35      kristaps 1529:                                (NULL == psec && !use_all) ||
                   1530:                                ! strcmp(MANDOC_DB, fn) ||
                   1531:                                ! strcmp(MANDOC_IDX, fn))
1.12      schwarze 1532:                        continue;
                   1533:
                   1534:                /*
                   1535:                 * By default, skip files where the file name suffix
                   1536:                 * does not agree with the section directory
                   1537:                 * they are located in.
                   1538:                 */
                   1539:
                   1540:                suffix = strrchr(fn, '.');
                   1541:                if (0 == use_all) {
                   1542:                        if (NULL == suffix)
1.5       kristaps 1543:                                continue;
1.14      schwarze 1544:                        if ((MANDOC_SRC & src_form &&
                   1545:                                         strcmp(suffix + 1, psec)) ||
                   1546:                            (MANDOC_FORM & src_form &&
                   1547:                                         strcmp(suffix + 1, "0")))
                   1548:                                        continue;
                   1549:                }
                   1550:                if (NULL != suffix) {
                   1551:                        if ('0' == suffix[1])
                   1552:                                src_form |= MANDOC_FORM;
                   1553:                        else if ('1' <= suffix[1] && '9' >= suffix[1])
                   1554:                                src_form |= MANDOC_SRC;
                   1555:                }
                   1556:
                   1557:
                   1558:                /*
                   1559:                 * Skip formatted manuals if a source version is
                   1560:                 * available.  Ignore the age: it is very unlikely
                   1561:                 * that people install newer formatted base manuals
                   1562:                 * when they used to have source manuals before,
                   1563:                 * and in ports, old manuals get removed on update.
                   1564:                 */
                   1565:                if (0 == use_all && MANDOC_FORM & src_form &&
                   1566:                                NULL != psec) {
                   1567:                        buf[0] = '\0';
                   1568:                        strlcat(buf, dir, MAXPATHLEN);
                   1569:                        p = strrchr(buf, '/');
1.32      schwarze 1570:                        if (NULL != parch && NULL != p)
                   1571:                                for (p--; p > buf; p--)
                   1572:                                        if ('/' == *p)
                   1573:                                                break;
1.14      schwarze 1574:                        if (NULL == p)
                   1575:                                p = buf;
                   1576:                        else
                   1577:                                p++;
                   1578:                        if (0 == strncmp("cat", p, 3))
                   1579:                                memcpy(p, "man", 3);
                   1580:                        strlcat(buf, "/", MAXPATHLEN);
                   1581:                        sz = strlcat(buf, fn, MAXPATHLEN);
                   1582:                        if (sz >= MAXPATHLEN) {
                   1583:                                fprintf(stderr, "%s: Path too long\n", buf);
1.5       kristaps 1584:                                continue;
1.14      schwarze 1585:                        }
                   1586:                        q = strrchr(buf, '.');
                   1587:                        if (NULL != q && p < q++) {
                   1588:                                *q = '\0';
                   1589:                                sz = strlcat(buf, psec, MAXPATHLEN);
                   1590:                                if (sz >= MAXPATHLEN) {
                   1591:                                        fprintf(stderr,
                   1592:                                            "%s: Path too long\n", buf);
                   1593:                                        continue;
                   1594:                                }
1.35      kristaps 1595:                                if (0 == access(buf, R_OK))
1.14      schwarze 1596:                                        continue;
                   1597:                        }
1.5       kristaps 1598:                }
1.4       kristaps 1599:
1.35      kristaps 1600:                assert('.' == dir[0]);
                   1601:                assert('/' == dir[1]);
1.5       kristaps 1602:                buf[0] = '\0';
1.35      kristaps 1603:                strlcat(buf, dir + 2, MAXPATHLEN);
1.5       kristaps 1604:                strlcat(buf, "/", MAXPATHLEN);
1.6       schwarze 1605:                sz = strlcat(buf, fn, MAXPATHLEN);
1.5       kristaps 1606:                if (sz >= MAXPATHLEN) {
                   1607:                        fprintf(stderr, "%s: Path too long\n", dir);
1.14      schwarze 1608:                        continue;
1.5       kristaps 1609:                }
                   1610:
1.4       kristaps 1611:                nof = mandoc_calloc(1, sizeof(struct of));
1.5       kristaps 1612:                nof->fname = mandoc_strdup(buf);
1.12      schwarze 1613:                if (NULL != psec)
                   1614:                        nof->sec = mandoc_strdup(psec);
                   1615:                if (NULL != parch)
                   1616:                        nof->arch = mandoc_strdup(parch);
1.14      schwarze 1617:                nof->src_form = src_form;
1.12      schwarze 1618:
                   1619:                /*
                   1620:                 * Remember the file name without the extension,
                   1621:                 * to be used as the page title in the database.
                   1622:                 */
                   1623:
                   1624:                if (NULL != suffix)
                   1625:                        *suffix = '\0';
                   1626:                nof->title = mandoc_strdup(fn);
1.5       kristaps 1627:
1.14      schwarze 1628:                /*
                   1629:                 * Add the structure to the list.
                   1630:                 */
                   1631:
1.4       kristaps 1632:                if (NULL == *of) {
                   1633:                        *of = nof;
                   1634:                        (*of)->first = nof;
                   1635:                } else {
1.5       kristaps 1636:                        nof->first = (*of)->first;
1.4       kristaps 1637:                        (*of)->next = nof;
                   1638:                        *of = nof;
                   1639:                }
                   1640:        }
                   1641:
1.7       schwarze 1642:        closedir(d);
1.4       kristaps 1643: }
                   1644:
                   1645: static void
                   1646: ofile_free(struct of *of)
                   1647: {
                   1648:        struct of       *nof;
                   1649:
                   1650:        while (of) {
                   1651:                nof = of->next;
                   1652:                free(of->fname);
1.12      schwarze 1653:                free(of->sec);
                   1654:                free(of->arch);
                   1655:                free(of->title);
1.4       kristaps 1656:                free(of);
                   1657:                of = nof;
                   1658:        }
                   1659: }
                   1660:
1.1       kristaps 1661: static void
                   1662: usage(void)
                   1663: {
                   1664:
1.5       kristaps 1665:        fprintf(stderr, "usage: %s [-v] "
1.34      schwarze 1666:                        "[-C file] |"
                   1667:                        " dir ... |"
                   1668:                        " -d dir [file ...] |"
                   1669:                        " -u dir [file ...]\n", progname);
1.1       kristaps 1670: }

CVSweb