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

Annotation of mandoc/mandocdb.c, Revision 1.18

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

CVSweb