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

Annotation of mandoc/mandocdb.c, Revision 1.70

1.70    ! schwarze    1: /*     $Id: mandocdb.c,v 1.69 2013/07/02 11:40:40 schwarze Exp $ */
1.1       kristaps    2: /*
1.48      schwarze    3:  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.56      schwarze    4:  * Copyright (c) 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.50      kristaps   22: #include <sys/stat.h>
1.1       kristaps   23:
                     24: #include <assert.h>
1.43      kristaps   25: #include <ctype.h>
1.63      schwarze   26: #include <errno.h>
1.1       kristaps   27: #include <fcntl.h>
1.50      kristaps   28: #include <fts.h>
1.1       kristaps   29: #include <getopt.h>
1.58      schwarze   30: #include <limits.h>
1.50      kristaps   31: #include <stddef.h>
1.59      schwarze   32: #include <stdio.h>
1.1       kristaps   33: #include <stdint.h>
                     34: #include <stdlib.h>
                     35: #include <string.h>
1.17      schwarze   36: #include <unistd.h>
1.1       kristaps   37:
1.53      kristaps   38: #ifdef HAVE_OHASH
1.50      kristaps   39: #include <ohash.h>
1.53      kristaps   40: #else
                     41: #include "compat_ohash.h"
                     42: #endif
1.50      kristaps   43: #include <sqlite3.h>
1.1       kristaps   44:
1.50      kristaps   45: #include "mdoc.h"
1.1       kristaps   46: #include "man.h"
                     47: #include "mandoc.h"
1.10      kristaps   48: #include "manpath.h"
1.55      kristaps   49: #include "mansearch.h"
1.1       kristaps   50:
1.52      kristaps   51: #define        SQL_EXEC(_v) \
                     52:        if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
                     53:                fprintf(stderr, "%s\n", sqlite3_errmsg(db))
                     54: #define        SQL_BIND_TEXT(_s, _i, _v) \
                     55:        if (SQLITE_OK != sqlite3_bind_text \
                     56:                ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
                     57:                fprintf(stderr, "%s\n", sqlite3_errmsg(db))
                     58: #define        SQL_BIND_INT(_s, _i, _v) \
                     59:        if (SQLITE_OK != sqlite3_bind_int \
                     60:                ((_s), (_i)++, (_v))) \
                     61:                fprintf(stderr, "%s\n", sqlite3_errmsg(db))
                     62: #define        SQL_BIND_INT64(_s, _i, _v) \
                     63:        if (SQLITE_OK != sqlite3_bind_int64 \
                     64:                ((_s), (_i)++, (_v))) \
                     65:                fprintf(stderr, "%s\n", sqlite3_errmsg(db))
                     66: #define SQL_STEP(_s) \
                     67:        if (SQLITE_DONE != sqlite3_step((_s))) \
                     68:                fprintf(stderr, "%s\n", sqlite3_errmsg(db))
                     69:
1.50      kristaps   70: enum   op {
                     71:        OP_DEFAULT = 0, /* new dbs from dir list or default config */
                     72:        OP_CONFFILE, /* new databases from custom config file */
                     73:        OP_UPDATE, /* delete/add entries in existing database */
                     74:        OP_DELETE, /* delete entries from existing database */
                     75:        OP_TEST /* change no databases, report potential problems */
1.38      schwarze   76: };
                     77:
1.50      kristaps   78: enum   form {
                     79:        FORM_SRC, /* format is -man or -mdoc */
                     80:        FORM_CAT, /* format is cat */
                     81:        FORM_NONE /* format is unknown */
                     82: };
1.38      schwarze   83:
1.50      kristaps   84: struct str {
                     85:        char            *utf8; /* key in UTF-8 form */
                     86:        const struct of *of; /* if set, the owning parse */
                     87:        uint64_t         mask; /* bitmask in sequence */
1.51      kristaps   88:        char             key[]; /* the string itself */
1.38      schwarze   89: };
                     90:
1.50      kristaps   91: struct id {
                     92:        ino_t            ino;
                     93:        dev_t            dev;
                     94: };
1.5       kristaps   95:
1.3       kristaps   96: struct of {
1.50      kristaps   97:        struct id        id; /* used for hashing routine */
                     98:        struct of       *next; /* next in ofs */
                     99:        enum form        dform; /* path-cued form */
                    100:        enum form        sform; /* suffix-cued form */
1.58      schwarze  101:        char             file[PATH_MAX]; /* filename rel. to manpath */
1.66      schwarze  102:        char            *desc; /* parsed description */
                    103:        char            *name; /* name (from filename) (not empty) */
                    104:        char            *sec; /* suffix-cued section (or empty) */
                    105:        char            *dsec; /* path-cued section (or empty) */
                    106:        char            *arch; /* path-cued arch. (or empty) */
1.3       kristaps  107: };
                    108:
1.69      schwarze  109: struct title {
                    110:        char            *title; /* name(sec/arch) given inside the file */
                    111:        char            *file; /* file name in case of mismatch */
                    112: };
                    113:
1.50      kristaps  114: enum   stmt {
                    115:        STMT_DELETE = 0, /* delete manpage */
                    116:        STMT_INSERT_DOC, /* insert manpage */
                    117:        STMT_INSERT_KEY, /* insert parsed key */
                    118:        STMT__MAX
1.1       kristaps  119: };
                    120:
1.50      kristaps  121: typedef        int (*mdoc_fp)(struct of *, const struct mdoc_node *);
1.1       kristaps  122:
1.50      kristaps  123: struct mdoc_handler {
                    124:        mdoc_fp          fp; /* optional handler */
                    125:        uint64_t         mask;  /* set unless handler returns 0 */
1.1       kristaps  126: };
                    127:
1.59      schwarze  128: static void     dbclose(int);
                    129: static void     dbindex(struct mchars *, int, const struct of *);
                    130: static int      dbopen(int);
                    131: static void     dbprune(void);
1.50      kristaps  132: static void     fileadd(struct of *);
                    133: static int      filecheck(const char *);
1.59      schwarze  134: static void     filescan(const char *);
1.50      kristaps  135: static void    *hash_alloc(size_t, void *);
                    136: static void     hash_free(void *, size_t, void *);
                    137: static void    *hash_halloc(size_t, void *);
                    138: static void     inoadd(const struct stat *, struct of *);
                    139: static int      inocheck(const struct stat *);
1.59      schwarze  140: static void     ofadd(int, const char *, const char *, const char *,
                    141:                        const char *, const char *, const struct stat *);
1.50      kristaps  142: static void     offree(void);
1.66      schwarze  143: static void     ofmerge(struct mchars *, struct mparse *,
1.69      schwarze  144:                        struct ohash_info*, int);
1.59      schwarze  145: static void     parse_catpage(struct of *);
1.61      schwarze  146: static void     parse_man(struct of *, const struct man_node *);
1.50      kristaps  147: static void     parse_mdoc(struct of *, const struct mdoc_node *);
                    148: static int      parse_mdoc_body(struct of *, const struct mdoc_node *);
                    149: static int      parse_mdoc_head(struct of *, const struct mdoc_node *);
                    150: static int      parse_mdoc_Fd(struct of *, const struct mdoc_node *);
                    151: static int      parse_mdoc_Fn(struct of *, const struct mdoc_node *);
                    152: static int      parse_mdoc_In(struct of *, const struct mdoc_node *);
                    153: static int      parse_mdoc_Nd(struct of *, const struct mdoc_node *);
                    154: static int      parse_mdoc_Nm(struct of *, const struct mdoc_node *);
                    155: static int      parse_mdoc_Sh(struct of *, const struct mdoc_node *);
                    156: static int      parse_mdoc_St(struct of *, const struct mdoc_node *);
                    157: static int      parse_mdoc_Xr(struct of *, const struct mdoc_node *);
1.59      schwarze  158: static int      set_basedir(const char *);
1.50      kristaps  159: static void     putkey(const struct of *,
                    160:                        const char *, uint64_t);
                    161: static void     putkeys(const struct of *,
1.64      schwarze  162:                        const char *, size_t, uint64_t);
1.50      kristaps  163: static void     putmdockey(const struct of *,
                    164:                        const struct mdoc_node *, uint64_t);
1.59      schwarze  165: static void     say(const char *, const char *, ...);
                    166: static int      treescan(void);
1.50      kristaps  167: static size_t   utf8(unsigned int, char [7]);
                    168: static void     utf8key(struct mchars *, struct str *);
                    169:
                    170: static char            *progname;
                    171: static int              use_all; /* use all found files */
                    172: static int              nodb; /* no database changes */
                    173: static int              verb; /* print what we're doing */
                    174: static int              warnings; /* warn about crap */
1.59      schwarze  175: static int              exitcode; /* to be returned by main */
1.50      kristaps  176: static enum op          op; /* operational mode */
1.59      schwarze  177: static char             basedir[PATH_MAX]; /* current base directory */
1.50      kristaps  178: static struct ohash     inos; /* table of inodes/devices */
                    179: static struct ohash     filenames; /* table of filenames */
                    180: static struct ohash     strings; /* table of all strings */
                    181: static struct of       *ofs = NULL; /* vector of files to parse */
                    182: static sqlite3         *db = NULL; /* current database */
                    183: static sqlite3_stmt    *stmts[STMT__MAX]; /* current statements */
1.25      schwarze  184:
                    185: static const struct mdoc_handler mdocs[MDOC_MAX] = {
1.68      schwarze  186:        { NULL, 0 },  /* Ap */
                    187:        { NULL, 0 },  /* Dd */
                    188:        { NULL, 0 },  /* Dt */
                    189:        { NULL, 0 },  /* Os */
                    190:        { parse_mdoc_Sh, TYPE_Sh }, /* Sh */
                    191:        { parse_mdoc_head, TYPE_Ss }, /* Ss */
                    192:        { NULL, 0 },  /* Pp */
                    193:        { NULL, 0 },  /* D1 */
                    194:        { NULL, 0 },  /* Dl */
                    195:        { NULL, 0 },  /* Bd */
                    196:        { NULL, 0 },  /* Ed */
                    197:        { NULL, 0 },  /* Bl */
                    198:        { NULL, 0 },  /* El */
                    199:        { NULL, 0 },  /* It */
                    200:        { NULL, 0 },  /* Ad */
                    201:        { NULL, TYPE_An },  /* An */
                    202:        { NULL, TYPE_Ar },  /* Ar */
                    203:        { NULL, TYPE_Cd },  /* Cd */
                    204:        { NULL, TYPE_Cm },  /* Cm */
                    205:        { NULL, TYPE_Dv },  /* Dv */
                    206:        { NULL, TYPE_Er },  /* Er */
                    207:        { NULL, TYPE_Ev },  /* Ev */
                    208:        { NULL, 0 },  /* Ex */
                    209:        { NULL, TYPE_Fa },  /* Fa */
                    210:        { parse_mdoc_Fd, 0 },  /* Fd */
                    211:        { NULL, TYPE_Fl },  /* Fl */
                    212:        { parse_mdoc_Fn, 0 },  /* Fn */
                    213:        { NULL, TYPE_Ft },  /* Ft */
                    214:        { NULL, TYPE_Ic },  /* Ic */
                    215:        { parse_mdoc_In, TYPE_In },  /* In */
                    216:        { NULL, TYPE_Li },  /* Li */
                    217:        { parse_mdoc_Nd, TYPE_Nd },  /* Nd */
                    218:        { parse_mdoc_Nm, TYPE_Nm },  /* Nm */
                    219:        { NULL, 0 },  /* Op */
                    220:        { NULL, 0 },  /* Ot */
                    221:        { NULL, TYPE_Pa },  /* Pa */
                    222:        { NULL, 0 },  /* Rv */
                    223:        { parse_mdoc_St, 0 },  /* St */
                    224:        { NULL, TYPE_Va },  /* Va */
                    225:        { parse_mdoc_body, TYPE_Va },  /* Vt */
                    226:        { parse_mdoc_Xr, 0 },  /* Xr */
                    227:        { NULL, 0 },  /* %A */
                    228:        { NULL, 0 },  /* %B */
                    229:        { NULL, 0 },  /* %D */
                    230:        { NULL, 0 },  /* %I */
                    231:        { NULL, 0 },  /* %J */
                    232:        { NULL, 0 },  /* %N */
                    233:        { NULL, 0 },  /* %O */
                    234:        { NULL, 0 },  /* %P */
                    235:        { NULL, 0 },  /* %R */
                    236:        { NULL, 0 },  /* %T */
                    237:        { NULL, 0 },  /* %V */
                    238:        { NULL, 0 },  /* Ac */
                    239:        { NULL, 0 },  /* Ao */
                    240:        { NULL, 0 },  /* Aq */
                    241:        { NULL, TYPE_At },  /* At */
                    242:        { NULL, 0 },  /* Bc */
                    243:        { NULL, 0 },  /* Bf */
                    244:        { NULL, 0 },  /* Bo */
                    245:        { NULL, 0 },  /* Bq */
                    246:        { NULL, TYPE_Bsx },  /* Bsx */
                    247:        { NULL, TYPE_Bx },  /* Bx */
                    248:        { NULL, 0 },  /* Db */
                    249:        { NULL, 0 },  /* Dc */
                    250:        { NULL, 0 },  /* Do */
                    251:        { NULL, 0 },  /* Dq */
                    252:        { NULL, 0 },  /* Ec */
                    253:        { NULL, 0 },  /* Ef */
                    254:        { NULL, TYPE_Em },  /* Em */
                    255:        { NULL, 0 },  /* Eo */
                    256:        { NULL, TYPE_Fx },  /* Fx */
                    257:        { NULL, TYPE_Ms },  /* Ms */
                    258:        { NULL, 0 },  /* No */
                    259:        { NULL, 0 },  /* Ns */
                    260:        { NULL, TYPE_Nx },  /* Nx */
                    261:        { NULL, TYPE_Ox },  /* Ox */
                    262:        { NULL, 0 },  /* Pc */
                    263:        { NULL, 0 },  /* Pf */
                    264:        { NULL, 0 },  /* Po */
                    265:        { NULL, 0 },  /* Pq */
                    266:        { NULL, 0 },  /* Qc */
                    267:        { NULL, 0 },  /* Ql */
                    268:        { NULL, 0 },  /* Qo */
                    269:        { NULL, 0 },  /* Qq */
                    270:        { NULL, 0 },  /* Re */
                    271:        { NULL, 0 },  /* Rs */
                    272:        { NULL, 0 },  /* Sc */
                    273:        { NULL, 0 },  /* So */
                    274:        { NULL, 0 },  /* Sq */
                    275:        { NULL, 0 },  /* Sm */
                    276:        { NULL, 0 },  /* Sx */
                    277:        { NULL, TYPE_Sy },  /* Sy */
                    278:        { NULL, TYPE_Tn },  /* Tn */
                    279:        { NULL, 0 },  /* Ux */
                    280:        { NULL, 0 },  /* Xc */
                    281:        { NULL, 0 },  /* Xo */
                    282:        { parse_mdoc_head, 0 },  /* Fo */
                    283:        { NULL, 0 },  /* Fc */
                    284:        { NULL, 0 },  /* Oo */
                    285:        { NULL, 0 },  /* Oc */
                    286:        { NULL, 0 },  /* Bk */
                    287:        { NULL, 0 },  /* Ek */
                    288:        { NULL, 0 },  /* Bt */
                    289:        { NULL, 0 },  /* Hf */
                    290:        { NULL, 0 },  /* Fr */
                    291:        { NULL, 0 },  /* Ud */
                    292:        { NULL, TYPE_Lb },  /* Lb */
                    293:        { NULL, 0 },  /* Lp */
                    294:        { NULL, TYPE_Lk },  /* Lk */
                    295:        { NULL, TYPE_Mt },  /* Mt */
                    296:        { NULL, 0 },  /* Brq */
                    297:        { NULL, 0 },  /* Bro */
                    298:        { NULL, 0 },  /* Brc */
                    299:        { NULL, 0 },  /* %C */
                    300:        { NULL, 0 },  /* Es */
                    301:        { NULL, 0 },  /* En */
                    302:        { NULL, TYPE_Dx },  /* Dx */
                    303:        { NULL, 0 },  /* %Q */
                    304:        { NULL, 0 },  /* br */
                    305:        { NULL, 0 },  /* sp */
                    306:        { NULL, 0 },  /* %U */
                    307:        { NULL, 0 },  /* Ta */
1.1       kristaps  308: };
                    309:
                    310: int
                    311: main(int argc, char *argv[])
                    312: {
1.59      schwarze  313:        int               ch, i;
1.50      kristaps  314:        size_t            j, sz;
1.59      schwarze  315:        const char       *path_arg;
1.50      kristaps  316:        struct mchars    *mc;
                    317:        struct manpaths   dirs;
                    318:        struct mparse    *mp;
                    319:        struct ohash_info ino_info, filename_info, str_info;
                    320:
                    321:        memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *));
                    322:        memset(&dirs, 0, sizeof(struct manpaths));
                    323:
                    324:        ino_info.halloc = filename_info.halloc =
                    325:                str_info.halloc = hash_halloc;
                    326:        ino_info.hfree = filename_info.hfree =
                    327:                str_info.hfree = hash_free;
                    328:        ino_info.alloc = filename_info.alloc =
                    329:                str_info.alloc = hash_alloc;
                    330:
                    331:        ino_info.key_offset = offsetof(struct of, id);
                    332:        filename_info.key_offset = offsetof(struct of, file);
                    333:        str_info.key_offset = offsetof(struct str, key);
1.1       kristaps  334:
                    335:        progname = strrchr(argv[0], '/');
                    336:        if (progname == NULL)
                    337:                progname = argv[0];
                    338:        else
                    339:                ++progname;
                    340:
1.50      kristaps  341:        /*
                    342:         * We accept a few different invocations.
                    343:         * The CHECKOP macro makes sure that invocation styles don't
                    344:         * clobber each other.
                    345:         */
                    346: #define        CHECKOP(_op, _ch) do \
                    347:        if (OP_DEFAULT != (_op)) { \
                    348:                fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
                    349:                goto usage; \
                    350:        } while (/*CONSTCOND*/0)
1.10      kristaps  351:
1.59      schwarze  352:        path_arg = NULL;
1.38      schwarze  353:        op = OP_DEFAULT;
1.1       kristaps  354:
1.50      kristaps  355:        while (-1 != (ch = getopt(argc, argv, "aC:d:ntu:vW")))
1.1       kristaps  356:                switch (ch) {
1.12      schwarze  357:                case ('a'):
                    358:                        use_all = 1;
                    359:                        break;
1.34      schwarze  360:                case ('C'):
1.50      kristaps  361:                        CHECKOP(op, ch);
1.59      schwarze  362:                        path_arg = optarg;
1.38      schwarze  363:                        op = OP_CONFFILE;
1.34      schwarze  364:                        break;
1.5       kristaps  365:                case ('d'):
1.50      kristaps  366:                        CHECKOP(op, ch);
1.59      schwarze  367:                        path_arg = optarg;
1.5       kristaps  368:                        op = OP_UPDATE;
                    369:                        break;
1.50      kristaps  370:                case ('n'):
                    371:                        nodb = 1;
                    372:                        break;
1.38      schwarze  373:                case ('t'):
1.50      kristaps  374:                        CHECKOP(op, ch);
1.38      schwarze  375:                        dup2(STDOUT_FILENO, STDERR_FILENO);
                    376:                        op = OP_TEST;
1.50      kristaps  377:                        nodb = warnings = 1;
1.38      schwarze  378:                        break;
1.5       kristaps  379:                case ('u'):
1.50      kristaps  380:                        CHECKOP(op, ch);
1.59      schwarze  381:                        path_arg = optarg;
1.5       kristaps  382:                        op = OP_DELETE;
                    383:                        break;
                    384:                case ('v'):
                    385:                        verb++;
                    386:                        break;
1.38      schwarze  387:                case ('W'):
                    388:                        warnings = 1;
                    389:                        break;
1.1       kristaps  390:                default:
1.38      schwarze  391:                        goto usage;
1.1       kristaps  392:                }
                    393:
                    394:        argc -= optind;
                    395:        argv += optind;
                    396:
1.38      schwarze  397:        if (OP_CONFFILE == op && argc > 0) {
1.50      kristaps  398:                fprintf(stderr, "-C: Too many arguments\n");
1.38      schwarze  399:                goto usage;
                    400:        }
                    401:
1.59      schwarze  402:        exitcode = (int)MANDOCLEVEL_OK;
1.50      kristaps  403:        mp = mparse_alloc(MPARSE_AUTO,
                    404:                MANDOCLEVEL_FATAL, NULL, NULL, NULL);
                    405:        mc = mchars_alloc();
                    406:
                    407:        ohash_init(&inos, 6, &ino_info);
                    408:        ohash_init(&filenames, 6, &filename_info);
                    409:
                    410:        if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
                    411:                /*
                    412:                 * Force processing all files.
                    413:                 */
                    414:                use_all = 1;
1.59      schwarze  415:
1.50      kristaps  416:                /*
                    417:                 * All of these deal with a specific directory.
                    418:                 * Jump into that directory then collect files specified
                    419:                 * on the command-line.
                    420:                 */
1.59      schwarze  421:                if (0 == set_basedir(path_arg))
1.50      kristaps  422:                        goto out;
                    423:                for (i = 0; i < argc; i++)
1.59      schwarze  424:                        filescan(argv[i]);
                    425:                if (0 == dbopen(1))
1.50      kristaps  426:                        goto out;
                    427:                if (OP_TEST != op)
1.59      schwarze  428:                        dbprune();
1.50      kristaps  429:                if (OP_DELETE != op)
1.69      schwarze  430:                        ofmerge(mc, mp, &str_info, 0);
1.59      schwarze  431:                dbclose(1);
1.50      kristaps  432:        } else {
                    433:                /*
                    434:                 * If we have arguments, use them as our manpaths.
                    435:                 * If we don't, grok from manpath(1) or however else
                    436:                 * manpath_parse() wants to do it.
                    437:                 */
                    438:                if (argc > 0) {
                    439:                        dirs.paths = mandoc_calloc
                    440:                                (argc, sizeof(char *));
                    441:                        dirs.sz = (size_t)argc;
                    442:                        for (i = 0; i < argc; i++)
                    443:                                dirs.paths[i] = mandoc_strdup(argv[i]);
                    444:                } else
1.59      schwarze  445:                        manpath_parse(&dirs, path_arg, NULL, NULL);
1.50      kristaps  446:
                    447:                /*
1.69      schwarze  448:                 * First scan the tree rooted at a base directory, then
                    449:                 * build a new database and finally move it into place.
1.50      kristaps  450:                 * Ignore zero-length directories and strip trailing
                    451:                 * slashes.
                    452:                 */
                    453:                for (j = 0; j < dirs.sz; j++) {
                    454:                        sz = strlen(dirs.paths[j]);
                    455:                        if (sz && '/' == dirs.paths[j][sz - 1])
                    456:                                dirs.paths[j][--sz] = '\0';
                    457:                        if (0 == sz)
                    458:                                continue;
1.66      schwarze  459:
                    460:                        if (j) {
                    461:                                ohash_init(&inos, 6, &ino_info);
                    462:                                ohash_init(&filenames, 6, &filename_info);
                    463:                        }
                    464:
1.59      schwarze  465:                        if (0 == set_basedir(dirs.paths[j]))
1.50      kristaps  466:                                goto out;
1.59      schwarze  467:                        if (0 == treescan())
1.50      kristaps  468:                                goto out;
1.59      schwarze  469:                        if (0 == set_basedir(dirs.paths[j]))
1.50      kristaps  470:                                goto out;
1.59      schwarze  471:                        if (0 == dbopen(0))
1.50      kristaps  472:                                goto out;
1.52      kristaps  473:
1.69      schwarze  474:                        ofmerge(mc, mp, &str_info, warnings && !use_all);
1.59      schwarze  475:                        dbclose(0);
1.66      schwarze  476:
                    477:                        if (j + 1 < dirs.sz) {
                    478:                                ohash_delete(&inos);
                    479:                                ohash_delete(&filenames);
                    480:                                offree();
                    481:                        }
1.50      kristaps  482:                }
                    483:        }
                    484: out:
1.59      schwarze  485:        set_basedir(NULL);
1.50      kristaps  486:        manpath_free(&dirs);
                    487:        mchars_free(mc);
                    488:        mparse_free(mp);
                    489:        ohash_delete(&inos);
                    490:        ohash_delete(&filenames);
                    491:        offree();
1.59      schwarze  492:        return(exitcode);
1.50      kristaps  493: usage:
                    494:        fprintf(stderr, "usage: %s [-anvW] [-C file]\n"
                    495:                        "       %s [-anvW] dir ...\n"
                    496:                        "       %s [-nvW] -d dir [file ...]\n"
                    497:                        "       %s [-nvW] -u dir [file ...]\n"
                    498:                        "       %s -t file ...\n",
                    499:                       progname, progname, progname,
                    500:                       progname, progname);
                    501:
1.59      schwarze  502:        return((int)MANDOCLEVEL_BADARG);
1.50      kristaps  503: }
                    504:
                    505: /*
1.59      schwarze  506:  * Scan a directory tree rooted at "basedir" for manpages.
1.50      kristaps  507:  * We use fts(), scanning directory parts along the way for clues to our
                    508:  * section and architecture.
                    509:  *
                    510:  * If use_all has been specified, grok all files.
                    511:  * If not, sanitise paths to the following:
                    512:  *
                    513:  *   [./]man*[/<arch>]/<name>.<section>
                    514:  *   or
                    515:  *   [./]cat<section>[/<arch>]/<name>.0
                    516:  *
                    517:  * TODO: accomodate for multi-language directories.
                    518:  */
                    519: static int
1.59      schwarze  520: treescan(void)
1.50      kristaps  521: {
                    522:        FTS             *f;
                    523:        FTSENT          *ff;
                    524:        int              dform;
                    525:        char            *sec;
1.66      schwarze  526:        const char      *dsec, *arch, *cp, *path;
1.50      kristaps  527:        const char      *argv[2];
                    528:
                    529:        argv[0] = ".";
                    530:        argv[1] = (char *)NULL;
                    531:
                    532:        /*
                    533:         * Walk through all components under the directory, using the
                    534:         * logical descent of files.
                    535:         */
                    536:        f = fts_open((char * const *)argv, FTS_LOGICAL, NULL);
                    537:        if (NULL == f) {
1.59      schwarze  538:                exitcode = (int)MANDOCLEVEL_SYSERR;
                    539:                say("", NULL);
1.50      kristaps  540:                return(0);
                    541:        }
                    542:
                    543:        dsec = arch = NULL;
                    544:        dform = FORM_NONE;
                    545:
                    546:        while (NULL != (ff = fts_read(f))) {
                    547:                path = ff->fts_path + 2;
                    548:                /*
                    549:                 * If we're a regular file, add an "of" by using the
                    550:                 * stored directory data and handling the filename.
                    551:                 * Disallow duplicate (hard-linked) files.
                    552:                 */
                    553:                if (FTS_F == ff->fts_info) {
1.60      schwarze  554:                        if (0 == strcmp(path, MANDOC_DB))
                    555:                                continue;
1.50      kristaps  556:                        if ( ! use_all && ff->fts_level < 2) {
1.56      schwarze  557:                                if (warnings)
1.59      schwarze  558:                                        say(path, "Extraneous file");
1.50      kristaps  559:                                continue;
                    560:                        } else if (inocheck(ff->fts_statp)) {
1.56      schwarze  561:                                if (warnings)
1.59      schwarze  562:                                        say(path, "Duplicate file");
1.50      kristaps  563:                                continue;
1.60      schwarze  564:                        } else if (NULL == (sec =
                    565:                                        strrchr(ff->fts_name, '.'))) {
                    566:                                if ( ! use_all) {
1.56      schwarze  567:                                        if (warnings)
1.60      schwarze  568:                                                say(path,
                    569:                                                    "No filename suffix");
1.50      kristaps  570:                                        continue;
                    571:                                }
1.60      schwarze  572:                        } else if (0 == strcmp(++sec, "html")) {
                    573:                                if (warnings)
                    574:                                        say(path, "Skip html");
                    575:                                continue;
                    576:                        } else if (0 == strcmp(sec, "gz")) {
                    577:                                if (warnings)
                    578:                                        say(path, "Skip gz");
                    579:                                continue;
                    580:                        } else if (0 == strcmp(sec, "ps")) {
                    581:                                if (warnings)
                    582:                                        say(path, "Skip ps");
                    583:                                continue;
                    584:                        } else if (0 == strcmp(sec, "pdf")) {
                    585:                                if (warnings)
                    586:                                        say(path, "Skip pdf");
                    587:                                continue;
                    588:                        } else if ( ! use_all &&
                    589:                            ((FORM_SRC == dform && strcmp(sec, dsec)) ||
                    590:                             (FORM_CAT == dform && strcmp(sec, "0")))) {
                    591:                                if (warnings)
                    592:                                        say(path, "Wrong filename suffix");
                    593:                                continue;
1.66      schwarze  594:                        } else
1.60      schwarze  595:                                sec[-1] = '\0';
1.66      schwarze  596:                        ofadd(dform, path, ff->fts_name, dsec, sec,
                    597:                                        arch, ff->fts_statp);
1.50      kristaps  598:                        continue;
                    599:                } else if (FTS_D != ff->fts_info &&
1.60      schwarze  600:                                FTS_DP != ff->fts_info) {
                    601:                        if (warnings)
                    602:                                say(path, "Not a regular file");
1.50      kristaps  603:                        continue;
1.60      schwarze  604:                }
1.1       kristaps  605:
1.50      kristaps  606:                switch (ff->fts_level) {
                    607:                case (0):
                    608:                        /* Ignore the root directory. */
                    609:                        break;
                    610:                case (1):
                    611:                        /*
                    612:                         * This might contain manX/ or catX/.
                    613:                         * Try to infer this from the name.
                    614:                         * If we're not in use_all, enforce it.
                    615:                         */
                    616:                        dsec = NULL;
                    617:                        dform = FORM_NONE;
                    618:                        cp = ff->fts_name;
                    619:                        if (FTS_DP == ff->fts_info)
                    620:                                break;
1.1       kristaps  621:
1.50      kristaps  622:                        if (0 == strncmp(cp, "man", 3)) {
                    623:                                dform = FORM_SRC;
1.66      schwarze  624:                                dsec = cp + 3;
1.50      kristaps  625:                        } else if (0 == strncmp(cp, "cat", 3)) {
                    626:                                dform = FORM_CAT;
1.66      schwarze  627:                                dsec = cp + 3;
1.50      kristaps  628:                        }
1.1       kristaps  629:
1.50      kristaps  630:                        if (NULL != dsec || use_all)
                    631:                                break;
1.1       kristaps  632:
1.56      schwarze  633:                        if (warnings)
1.59      schwarze  634:                                say(path, "Unknown directory part");
1.50      kristaps  635:                        fts_set(f, ff, FTS_SKIP);
                    636:                        break;
                    637:                case (2):
                    638:                        /*
                    639:                         * Possibly our architecture.
                    640:                         * If we're descending, keep tabs on it.
                    641:                         */
                    642:                        arch = NULL;
                    643:                        if (FTS_DP != ff->fts_info && NULL != dsec)
1.66      schwarze  644:                                arch = ff->fts_name;
1.50      kristaps  645:                        break;
                    646:                default:
                    647:                        if (FTS_DP == ff->fts_info || use_all)
                    648:                                break;
1.56      schwarze  649:                        if (warnings)
1.59      schwarze  650:                                say(path, "Extraneous directory part");
1.50      kristaps  651:                        fts_set(f, ff, FTS_SKIP);
                    652:                        break;
1.5       kristaps  653:                }
1.50      kristaps  654:        }
                    655:
                    656:        fts_close(f);
                    657:        return(1);
                    658: }
1.5       kristaps  659:
1.50      kristaps  660: /*
                    661:  * Add a file to the file vector.
                    662:  * Do not verify that it's a "valid" looking manpage (we'll do that
                    663:  * later).
                    664:  *
                    665:  * Try to infer the manual section, architecture, and page name from the
                    666:  * path, assuming it looks like
                    667:  *
                    668:  *   [./]man*[/<arch>]/<name>.<section>
                    669:  *   or
                    670:  *   [./]cat<section>[/<arch>]/<name>.0
                    671:  *
                    672:  * Stuff this information directly into the "of" vector.
                    673:  * See treescan() for the fts(3) version of this.
                    674:  */
                    675: static void
1.59      schwarze  676: filescan(const char *file)
1.50      kristaps  677: {
1.59      schwarze  678:        char             buf[PATH_MAX];
1.50      kristaps  679:        const char      *sec, *arch, *name, *dsec;
1.59      schwarze  680:        char            *p, *start;
1.50      kristaps  681:        int              dform;
                    682:        struct stat      st;
1.5       kristaps  683:
1.50      kristaps  684:        assert(use_all);
1.5       kristaps  685:
1.50      kristaps  686:        if (0 == strncmp(file, "./", 2))
                    687:                file += 2;
1.5       kristaps  688:
1.59      schwarze  689:        if (NULL == realpath(file, buf)) {
                    690:                exitcode = (int)MANDOCLEVEL_BADARG;
                    691:                say(file, NULL);
                    692:                return;
                    693:        } else if (strstr(buf, basedir) != buf) {
                    694:                exitcode = (int)MANDOCLEVEL_BADARG;
                    695:                say("", "%s: outside base directory", buf);
                    696:                return;
                    697:        } else if (-1 == stat(buf, &st)) {
                    698:                exitcode = (int)MANDOCLEVEL_BADARG;
                    699:                say(file, NULL);
1.50      kristaps  700:                return;
                    701:        } else if ( ! (S_IFREG & st.st_mode)) {
1.59      schwarze  702:                exitcode = (int)MANDOCLEVEL_BADARG;
                    703:                say(file, "Not a regular file");
1.50      kristaps  704:                return;
                    705:        } else if (inocheck(&st)) {
1.56      schwarze  706:                if (warnings)
1.59      schwarze  707:                        say(file, "Duplicate file");
1.50      kristaps  708:                return;
                    709:        }
1.59      schwarze  710:        start = buf + strlen(basedir);
1.50      kristaps  711:        sec = arch = name = dsec = NULL;
                    712:        dform = FORM_NONE;
1.17      schwarze  713:
1.50      kristaps  714:        /*
                    715:         * First try to guess our directory structure.
                    716:         * If we find a separator, try to look for man* or cat*.
                    717:         * If we find one of these and what's underneath is a directory,
                    718:         * assume it's an architecture.
                    719:         */
                    720:        if (NULL != (p = strchr(start, '/'))) {
                    721:                *p++ = '\0';
                    722:                if (0 == strncmp(start, "man", 3)) {
                    723:                        dform = FORM_SRC;
                    724:                        dsec = start + 3;
                    725:                } else if (0 == strncmp(start, "cat", 3)) {
                    726:                        dform = FORM_CAT;
                    727:                        dsec = start + 3;
1.17      schwarze  728:                }
1.5       kristaps  729:
1.50      kristaps  730:                start = p;
                    731:                if (NULL != dsec && NULL != (p = strchr(start, '/'))) {
                    732:                        *p++ = '\0';
                    733:                        arch = start;
                    734:                        start = p;
                    735:                }
                    736:        }
                    737:
                    738:        /*
                    739:         * Now check the file suffix.
                    740:         * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
                    741:         */
                    742:        p = strrchr(start, '\0');
                    743:        while (p-- > start && '/' != *p && '.' != *p)
                    744:                /* Loop. */ ;
                    745:
                    746:        if ('.' == *p) {
                    747:                *p++ = '\0';
                    748:                sec = p;
1.5       kristaps  749:        }
                    750:
1.10      kristaps  751:        /*
1.50      kristaps  752:         * Now try to parse the name.
                    753:         * Use the filename portion of the path.
1.10      kristaps  754:         */
1.50      kristaps  755:        name = start;
                    756:        if (NULL != (p = strrchr(start, '/'))) {
                    757:                name = p + 1;
                    758:                *p = '\0';
                    759:        }
                    760:
1.59      schwarze  761:        ofadd(dform, file, name, dsec, sec, arch, &st);
1.50      kristaps  762: }
                    763:
                    764: /*
                    765:  * See fileadd().
                    766:  */
                    767: static int
                    768: filecheck(const char *name)
                    769: {
1.10      kristaps  770:
1.65      schwarze  771:        return(NULL != ohash_find(&filenames,
                    772:                        ohash_qlookup(&filenames, name)));
1.50      kristaps  773: }
1.10      kristaps  774:
1.50      kristaps  775: /*
                    776:  * Use the standard hashing mechanism (K&R) to see if the given filename
                    777:  * already exists.
                    778:  */
                    779: static void
                    780: fileadd(struct of *of)
                    781: {
1.65      schwarze  782:        unsigned int     slot;
1.1       kristaps  783:
1.65      schwarze  784:        slot = ohash_qlookup(&filenames, of->file);
                    785:        assert(NULL == ohash_find(&filenames, slot));
                    786:        ohash_insert(&filenames, slot, of);
1.50      kristaps  787: }
1.3       kristaps  788:
1.50      kristaps  789: /*
                    790:  * See inoadd().
                    791:  */
                    792: static int
                    793: inocheck(const struct stat *st)
                    794: {
                    795:        struct id        id;
                    796:        uint32_t         hash;
                    797:
                    798:        memset(&id, 0, sizeof(id));
                    799:        id.ino = hash = st->st_ino;
                    800:        id.dev = st->st_dev;
1.13      schwarze  801:
1.65      schwarze  802:        return(NULL != ohash_find(&inos, ohash_lookup_memory(
                    803:                        &inos, (char *)&id, sizeof(id), hash)));
1.50      kristaps  804: }
1.5       kristaps  805:
1.50      kristaps  806: /*
                    807:  * The hashing function used here is quite simple: simply take the inode
                    808:  * and use uint32_t of its bits.
                    809:  * Then when we do the lookup, use both the inode and device identifier.
                    810:  */
                    811: static void
                    812: inoadd(const struct stat *st, struct of *of)
                    813: {
                    814:        uint32_t         hash;
1.65      schwarze  815:        unsigned int     slot;
1.1       kristaps  816:
1.50      kristaps  817:        of->id.ino = hash = st->st_ino;
                    818:        of->id.dev = st->st_dev;
1.65      schwarze  819:        slot = ohash_lookup_memory
1.50      kristaps  820:                (&inos, (char *)&of->id, sizeof(of->id), hash);
1.1       kristaps  821:
1.65      schwarze  822:        assert(NULL == ohash_find(&inos, slot));
                    823:        ohash_insert(&inos, slot, of);
1.50      kristaps  824: }
1.35      kristaps  825:
1.50      kristaps  826: static void
1.59      schwarze  827: ofadd(int dform, const char *file, const char *name, const char *dsec,
                    828:        const char *sec, const char *arch, const struct stat *st)
1.50      kristaps  829: {
                    830:        struct of       *of;
                    831:        int              sform;
                    832:
                    833:        assert(NULL != file);
                    834:
                    835:        if (NULL == name)
                    836:                name = "";
                    837:        if (NULL == sec)
                    838:                sec = "";
                    839:        if (NULL == dsec)
                    840:                dsec = "";
                    841:        if (NULL == arch)
                    842:                arch = "";
                    843:
                    844:        sform = FORM_NONE;
                    845:        if (NULL != sec && *sec <= '9' && *sec >= '1')
                    846:                sform = FORM_SRC;
                    847:        else if (NULL != sec && *sec == '0') {
                    848:                sec = dsec;
                    849:                sform = FORM_CAT;
                    850:        }
                    851:
                    852:        of = mandoc_calloc(1, sizeof(struct of));
1.58      schwarze  853:        strlcpy(of->file, file, PATH_MAX);
1.66      schwarze  854:        of->name = mandoc_strdup(name);
                    855:        of->sec = mandoc_strdup(sec);
                    856:        of->dsec = mandoc_strdup(dsec);
                    857:        of->arch = mandoc_strdup(arch);
1.50      kristaps  858:        of->sform = sform;
                    859:        of->dform = dform;
                    860:        of->next = ofs;
                    861:        ofs = of;
1.3       kristaps  862:
1.50      kristaps  863:        /*
                    864:         * Add to unique identifier hash.
                    865:         * Then if it's a source manual and we're going to use source in
                    866:         * favour of catpages, add it to that hash.
                    867:         */
                    868:        inoadd(st, of);
                    869:        fileadd(of);
                    870: }
1.3       kristaps  871:
1.50      kristaps  872: static void
                    873: offree(void)
                    874: {
                    875:        struct of       *of;
1.3       kristaps  876:
1.50      kristaps  877:        while (NULL != (of = ofs)) {
                    878:                ofs = of->next;
1.66      schwarze  879:                free(of->name);
                    880:                free(of->sec);
                    881:                free(of->dsec);
                    882:                free(of->arch);
1.50      kristaps  883:                free(of);
                    884:        }
                    885: }
1.38      schwarze  886:
1.50      kristaps  887: /*
                    888:  * Run through the files in the global vector "ofs" and add them to the
1.59      schwarze  889:  * database specified in "basedir".
1.50      kristaps  890:  *
                    891:  * This handles the parsing scheme itself, using the cues of directory
                    892:  * and filename to determine whether the file is parsable or not.
                    893:  */
1.59      schwarze  894: static void
1.66      schwarze  895: ofmerge(struct mchars *mc, struct mparse *mp,
1.69      schwarze  896:                struct ohash_info *infop, int check_reachable)
1.50      kristaps  897: {
1.69      schwarze  898:        struct ohash             title_table;
                    899:        struct ohash_info        title_info;
                    900:        char                     buf[PATH_MAX];
                    901:        struct of               *of;
                    902:        struct mdoc             *mdoc;
                    903:        struct man              *man;
                    904:        struct title            *title_entry;
                    905:        char                    *bufp, *title_str;
                    906:        const char              *msec, *march, *mtitle, *cp;
                    907:        size_t                   sz;
                    908:        int                      form;
                    909:        int                      match;
                    910:        unsigned int             slot;
                    911:        enum mandoclevel         lvl;
                    912:
                    913:        if (check_reachable) {
                    914:                title_info.alloc = hash_alloc;
                    915:                title_info.halloc = hash_halloc;
                    916:                title_info.hfree = hash_free;
                    917:                title_info.key_offset = offsetof(struct title, title);
                    918:                ohash_init(&title_table, 6, &title_info);
                    919:        }
1.50      kristaps  920:
                    921:        for (of = ofs; NULL != of; of = of->next) {
                    922:                /*
                    923:                 * If we're a catpage (as defined by our path), then see
                    924:                 * if a manpage exists by the same name (ignoring the
                    925:                 * suffix).
                    926:                 * If it does, then we want to use it instead of our
                    927:                 * own.
                    928:                 */
                    929:                if ( ! use_all && FORM_CAT == of->dform) {
1.58      schwarze  930:                        sz = strlcpy(buf, of->file, PATH_MAX);
                    931:                        if (sz >= PATH_MAX) {
1.56      schwarze  932:                                if (warnings)
1.59      schwarze  933:                                        say(of->file, "Filename too long");
1.50      kristaps  934:                                continue;
                    935:                        }
                    936:                        bufp = strstr(buf, "cat");
                    937:                        assert(NULL != bufp);
                    938:                        memcpy(bufp, "man", 3);
                    939:                        if (NULL != (bufp = strrchr(buf, '.')))
                    940:                                *++bufp = '\0';
1.58      schwarze  941:                        strlcat(buf, of->dsec, PATH_MAX);
1.50      kristaps  942:                        if (filecheck(buf)) {
1.56      schwarze  943:                                if (warnings)
1.59      schwarze  944:                                        say(of->file, "Man "
1.56      schwarze  945:                                            "source exists: %s", buf);
1.50      kristaps  946:                                continue;
                    947:                        }
                    948:                }
                    949:
1.66      schwarze  950:                ohash_init(&strings, 6, infop);
1.50      kristaps  951:                mparse_reset(mp);
                    952:                mdoc = NULL;
                    953:                man = NULL;
                    954:                form = 0;
                    955:                msec = of->dsec;
                    956:                march = of->arch;
                    957:                mtitle = of->name;
1.69      schwarze  958:                match = 1;
1.14      schwarze  959:
                    960:                /*
1.33      schwarze  961:                 * Try interpreting the file as mdoc(7) or man(7)
                    962:                 * source code, unless it is already known to be
                    963:                 * formatted.  Fall back to formatted mode.
1.14      schwarze  964:                 */
1.50      kristaps  965:                if (FORM_SRC == of->dform || FORM_SRC == of->sform) {
                    966:                        lvl = mparse_readfd(mp, -1, of->file);
                    967:                        if (lvl < MANDOCLEVEL_FATAL)
                    968:                                mparse_result(mp, &mdoc, &man);
                    969:                }
1.14      schwarze  970:
                    971:                if (NULL != mdoc) {
1.50      kristaps  972:                        form = 1;
1.14      schwarze  973:                        msec = mdoc_meta(mdoc)->msec;
1.38      schwarze  974:                        march = mdoc_meta(mdoc)->arch;
1.14      schwarze  975:                        mtitle = mdoc_meta(mdoc)->title;
                    976:                } else if (NULL != man) {
1.50      kristaps  977:                        form = 1;
1.14      schwarze  978:                        msec = man_meta(man)->msec;
1.38      schwarze  979:                        march = "";
1.14      schwarze  980:                        mtitle = man_meta(man)->title;
1.50      kristaps  981:                }
                    982:
                    983:                if (NULL == msec)
                    984:                        msec = "";
                    985:                if (NULL == march)
                    986:                        march = "";
                    987:                if (NULL == mtitle)
                    988:                        mtitle = "";
1.1       kristaps  989:
1.12      schwarze  990:                /*
1.44      kristaps  991:                 * Check whether the manual section given in a file
                    992:                 * agrees with the directory where the file is located.
                    993:                 * Some manuals have suffixes like (3p) on their
                    994:                 * section number either inside the file or in the
                    995:                 * directory name, some are linked into more than one
                    996:                 * section, like encrypt(1) = makekey(8).  Do not skip
                    997:                 * manuals for such reasons.
1.12      schwarze  998:                 */
1.56      schwarze  999:                if (warnings && !use_all && form &&
1.69      schwarze 1000:                                strcasecmp(msec, of->dsec)) {
                   1001:                        match = 0;
1.59      schwarze 1002:                        say(of->file, "Section \"%s\" "
1.50      kristaps 1003:                                "manual in %s directory",
                   1004:                                msec, of->dsec);
1.69      schwarze 1005:                }
1.12      schwarze 1006:
1.42      schwarze 1007:                /*
                   1008:                 * Manual page directories exist for each kernel
                   1009:                 * architecture as returned by machine(1).
                   1010:                 * However, many manuals only depend on the
                   1011:                 * application architecture as returned by arch(1).
                   1012:                 * For example, some (2/ARM) manuals are shared
                   1013:                 * across the "armish" and "zaurus" kernel
                   1014:                 * architectures.
                   1015:                 * A few manuals are even shared across completely
                   1016:                 * different architectures, for example fdformat(1)
                   1017:                 * on amd64, i386, sparc, and sparc64.
                   1018:                 * Thus, warn about architecture mismatches,
                   1019:                 * but don't skip manuals for this reason.
                   1020:                 */
1.69      schwarze 1021:                if (warnings && !use_all && strcasecmp(march, of->arch)) {
                   1022:                        match = 0;
1.59      schwarze 1023:                        say(of->file, "Architecture \"%s\" "
1.47      schwarze 1024:                                "manual in \"%s\" directory",
1.45      kristaps 1025:                                march, of->arch);
1.69      schwarze 1026:                }
                   1027:                if (warnings && !use_all && strcasecmp(mtitle, of->name))
                   1028:                        match = 0;
1.12      schwarze 1029:
1.50      kristaps 1030:                putkey(of, of->name, TYPE_Nm);
1.12      schwarze 1031:
1.50      kristaps 1032:                if (NULL != mdoc) {
                   1033:                        if (NULL != (cp = mdoc_meta(mdoc)->name))
                   1034:                                putkey(of, cp, TYPE_Nm);
1.66      schwarze 1035:                        assert(NULL == of->desc);
1.50      kristaps 1036:                        parse_mdoc(of, mdoc_node(mdoc));
1.66      schwarze 1037:                        putkey(of, NULL != of->desc ?
                   1038:                                of->desc : of->name, TYPE_Nd);
1.50      kristaps 1039:                } else if (NULL != man)
                   1040:                        parse_man(of, man_node(man));
                   1041:                else
1.59      schwarze 1042:                        parse_catpage(of);
1.44      kristaps 1043:
1.69      schwarze 1044:                /*
                   1045:                 * Build a title string for the file.  If it matches
                   1046:                 * the location of the file, remember the title as
                   1047:                 * found; else, remember it as missing.
                   1048:                 */
                   1049:
                   1050:                if (check_reachable) {
                   1051:                        if (-1 == asprintf(&title_str, "%s(%s%s%s)", mtitle,
                   1052:                            msec, '\0' == *march ? "" : "/", march)) {
                   1053:                                perror(NULL);
                   1054:                                exit((int)MANDOCLEVEL_SYSERR);
                   1055:                        }
                   1056:                        slot = ohash_qlookup(&title_table, title_str);
                   1057:                        title_entry = ohash_find(&title_table, slot);
                   1058:                        if (NULL == title_entry) {
                   1059:                                title_entry = mandoc_malloc(
                   1060:                                                sizeof(struct title));
                   1061:                                title_entry->title = title_str;
                   1062:                                title_entry->file = mandoc_strdup(
                   1063:                                    match ? "" : of->file);
                   1064:                                ohash_insert(&title_table, slot,
                   1065:                                                title_entry);
                   1066:                        } else {
                   1067:                                if (match)
                   1068:                                        *title_entry->file = '\0';
                   1069:                                free(title_str);
                   1070:                        }
                   1071:                }
                   1072:
1.59      schwarze 1073:                dbindex(mc, form, of);
1.66      schwarze 1074:                ohash_delete(&strings);
1.69      schwarze 1075:        }
                   1076:
                   1077:        if (check_reachable) {
                   1078:                title_entry = ohash_first(&title_table, &slot);
                   1079:                while (NULL != title_entry) {
                   1080:                        if ('\0' != *title_entry->file)
                   1081:                                say(title_entry->file,
                   1082:                                    "Probably unreachable, title is %s",
                   1083:                                    title_entry->title);
                   1084:                        free(title_entry->title);
                   1085:                        free(title_entry->file);
                   1086:                        free(title_entry);
                   1087:                        title_entry = ohash_next(&title_table, &slot);
                   1088:                }
                   1089:                ohash_delete(&title_table);
1.50      kristaps 1090:        }
                   1091: }
1.12      schwarze 1092:
1.50      kristaps 1093: static void
1.59      schwarze 1094: parse_catpage(struct of *of)
1.50      kristaps 1095: {
                   1096:        FILE            *stream;
                   1097:        char            *line, *p, *title;
                   1098:        size_t           len, plen, titlesz;
1.12      schwarze 1099:
1.50      kristaps 1100:        if (NULL == (stream = fopen(of->file, "r"))) {
1.56      schwarze 1101:                if (warnings)
1.59      schwarze 1102:                        say(of->file, NULL);
1.50      kristaps 1103:                return;
                   1104:        }
1.1       kristaps 1105:
1.50      kristaps 1106:        /* Skip to first blank line. */
1.1       kristaps 1107:
1.50      kristaps 1108:        while (NULL != (line = fgetln(stream, &len)))
                   1109:                if ('\n' == *line)
                   1110:                        break;
1.1       kristaps 1111:
1.50      kristaps 1112:        /*
                   1113:         * Assume the first line that is not indented
                   1114:         * is the first section header.  Skip to it.
                   1115:         */
1.1       kristaps 1116:
1.50      kristaps 1117:        while (NULL != (line = fgetln(stream, &len)))
                   1118:                if ('\n' != *line && ' ' != *line)
                   1119:                        break;
                   1120:
                   1121:        /*
                   1122:         * Read up until the next section into a buffer.
                   1123:         * Strip the leading and trailing newline from each read line,
                   1124:         * appending a trailing space.
                   1125:         * Ignore empty (whitespace-only) lines.
                   1126:         */
1.1       kristaps 1127:
1.50      kristaps 1128:        titlesz = 0;
                   1129:        title = NULL;
1.38      schwarze 1130:
1.50      kristaps 1131:        while (NULL != (line = fgetln(stream, &len))) {
                   1132:                if (' ' != *line || '\n' != line[len - 1])
                   1133:                        break;
                   1134:                while (len > 0 && isspace((unsigned char)*line)) {
                   1135:                        line++;
                   1136:                        len--;
                   1137:                }
                   1138:                if (1 == len)
1.38      schwarze 1139:                        continue;
1.50      kristaps 1140:                title = mandoc_realloc(title, titlesz + len);
                   1141:                memcpy(title + titlesz, line, len);
                   1142:                titlesz += len;
                   1143:                title[titlesz - 1] = ' ';
                   1144:        }
1.38      schwarze 1145:
1.50      kristaps 1146:        /*
                   1147:         * If no page content can be found, or the input line
                   1148:         * is already the next section header, or there is no
                   1149:         * trailing newline, reuse the page title as the page
                   1150:         * description.
                   1151:         */
1.44      kristaps 1152:
1.50      kristaps 1153:        if (NULL == title || '\0' == *title) {
1.56      schwarze 1154:                if (warnings)
1.59      schwarze 1155:                        say(of->file, "Cannot find NAME section");
1.66      schwarze 1156:                assert(NULL == of->desc);
                   1157:                of->desc = mandoc_strdup(of->name);
1.62      schwarze 1158:                putkey(of, of->name, TYPE_Nd);
1.50      kristaps 1159:                fclose(stream);
                   1160:                free(title);
                   1161:                return;
                   1162:        }
1.1       kristaps 1163:
1.50      kristaps 1164:        title = mandoc_realloc(title, titlesz + 1);
                   1165:        title[titlesz] = '\0';
1.33      schwarze 1166:
1.50      kristaps 1167:        /*
                   1168:         * Skip to the first dash.
                   1169:         * Use the remaining line as the description (no more than 70
                   1170:         * bytes).
                   1171:         */
1.33      schwarze 1172:
1.50      kristaps 1173:        if (NULL != (p = strstr(title, "- "))) {
                   1174:                for (p += 2; ' ' == *p || '\b' == *p; p++)
                   1175:                        /* Skip to next word. */ ;
                   1176:        } else {
1.56      schwarze 1177:                if (warnings)
1.59      schwarze 1178:                        say(of->file, "No dash in title line");
1.50      kristaps 1179:                p = title;
                   1180:        }
1.38      schwarze 1181:
1.50      kristaps 1182:        plen = strlen(p);
1.1       kristaps 1183:
1.50      kristaps 1184:        /* Strip backspace-encoding from line. */
1.1       kristaps 1185:
1.50      kristaps 1186:        while (NULL != (line = memchr(p, '\b', plen))) {
                   1187:                len = line - p;
                   1188:                if (0 == len) {
                   1189:                        memmove(line, line + 1, plen--);
                   1190:                        continue;
                   1191:                }
                   1192:                memmove(line - 1, line + 1, plen - len);
                   1193:                plen -= 2;
                   1194:        }
1.1       kristaps 1195:
1.66      schwarze 1196:        assert(NULL == of->desc);
                   1197:        of->desc = mandoc_strdup(p);
                   1198:        putkey(of, of->desc, TYPE_Nd);
1.50      kristaps 1199:        fclose(stream);
                   1200:        free(title);
                   1201: }
1.1       kristaps 1202:
1.50      kristaps 1203: /*
                   1204:  * Put a type/word pair into the word database for this particular file.
                   1205:  */
                   1206: static void
                   1207: putkey(const struct of *of, const char *value, uint64_t type)
                   1208: {
1.18      kristaps 1209:
1.50      kristaps 1210:        assert(NULL != value);
1.64      schwarze 1211:        putkeys(of, value, strlen(value), type);
1.3       kristaps 1212: }
                   1213:
                   1214: /*
1.50      kristaps 1215:  * Grok all nodes at or below a certain mdoc node into putkey().
1.3       kristaps 1216:  */
                   1217: static void
1.50      kristaps 1218: putmdockey(const struct of *of, const struct mdoc_node *n, uint64_t m)
1.3       kristaps 1219: {
1.18      kristaps 1220:
1.50      kristaps 1221:        for ( ; NULL != n; n = n->next) {
                   1222:                if (NULL != n->child)
                   1223:                        putmdockey(of, n->child, m);
                   1224:                if (MDOC_TEXT == n->type)
                   1225:                        putkey(of, n->string, m);
                   1226:        }
                   1227: }
1.18      kristaps 1228:
1.61      schwarze 1229: static void
1.50      kristaps 1230: parse_man(struct of *of, const struct man_node *n)
                   1231: {
                   1232:        const struct man_node *head, *body;
                   1233:        char            *start, *sv, *title;
                   1234:        char             byte;
                   1235:        size_t           sz, titlesz;
1.18      kristaps 1236:
1.50      kristaps 1237:        if (NULL == n)
1.61      schwarze 1238:                return;
1.18      kristaps 1239:
1.50      kristaps 1240:        /*
                   1241:         * We're only searching for one thing: the first text child in
                   1242:         * the BODY of a NAME section.  Since we don't keep track of
                   1243:         * sections in -man, run some hoops to find out whether we're in
                   1244:         * the correct section or not.
                   1245:         */
1.18      kristaps 1246:
1.50      kristaps 1247:        if (MAN_BODY == n->type && MAN_SH == n->tok) {
                   1248:                body = n;
                   1249:                assert(body->parent);
                   1250:                if (NULL != (head = body->parent->head) &&
                   1251:                                1 == head->nchild &&
                   1252:                                NULL != (head = (head->child)) &&
                   1253:                                MAN_TEXT == head->type &&
                   1254:                                0 == strcmp(head->string, "NAME") &&
                   1255:                                NULL != (body = body->child) &&
                   1256:                                MAN_TEXT == body->type) {
1.3       kristaps 1257:
1.50      kristaps 1258:                        title = NULL;
                   1259:                        titlesz = 0;
1.3       kristaps 1260:
1.50      kristaps 1261:                        /*
                   1262:                         * Suck the entire NAME section into memory.
                   1263:                         * Yes, we might run away.
                   1264:                         * But too many manuals have big, spread-out
                   1265:                         * NAME sections over many lines.
                   1266:                         */
1.3       kristaps 1267:
1.50      kristaps 1268:                        for ( ; NULL != body; body = body->next) {
                   1269:                                if (MAN_TEXT != body->type)
                   1270:                                        break;
                   1271:                                if (0 == (sz = strlen(body->string)))
                   1272:                                        continue;
                   1273:                                title = mandoc_realloc
                   1274:                                        (title, titlesz + sz + 1);
                   1275:                                memcpy(title + titlesz, body->string, sz);
                   1276:                                titlesz += sz + 1;
                   1277:                                title[titlesz - 1] = ' ';
                   1278:                        }
                   1279:                        if (NULL == title)
1.61      schwarze 1280:                                return;
1.18      kristaps 1281:
1.50      kristaps 1282:                        title = mandoc_realloc(title, titlesz + 1);
                   1283:                        title[titlesz] = '\0';
1.18      kristaps 1284:
1.50      kristaps 1285:                        /* Skip leading space.  */
1.18      kristaps 1286:
1.50      kristaps 1287:                        sv = title;
                   1288:                        while (isspace((unsigned char)*sv))
                   1289:                                sv++;
1.18      kristaps 1290:
1.50      kristaps 1291:                        if (0 == (sz = strlen(sv))) {
                   1292:                                free(title);
1.61      schwarze 1293:                                return;
1.50      kristaps 1294:                        }
1.1       kristaps 1295:
1.50      kristaps 1296:                        /* Erase trailing space. */
1.1       kristaps 1297:
1.50      kristaps 1298:                        start = &sv[sz - 1];
                   1299:                        while (start > sv && isspace((unsigned char)*start))
                   1300:                                *start-- = '\0';
1.1       kristaps 1301:
1.50      kristaps 1302:                        if (start == sv) {
                   1303:                                free(title);
1.61      schwarze 1304:                                return;
1.50      kristaps 1305:                        }
1.1       kristaps 1306:
1.50      kristaps 1307:                        start = sv;
1.18      kristaps 1308:
1.50      kristaps 1309:                        /*
                   1310:                         * Go through a special heuristic dance here.
                   1311:                         * Conventionally, one or more manual names are
                   1312:                         * comma-specified prior to a whitespace, then a
                   1313:                         * dash, then a description.  Try to puzzle out
                   1314:                         * the name parts here.
                   1315:                         */
1.18      kristaps 1316:
1.50      kristaps 1317:                        for ( ;; ) {
                   1318:                                sz = strcspn(start, " ,");
                   1319:                                if ('\0' == start[sz])
                   1320:                                        break;
1.1       kristaps 1321:
1.50      kristaps 1322:                                byte = start[sz];
                   1323:                                start[sz] = '\0';
1.1       kristaps 1324:
1.50      kristaps 1325:                                putkey(of, start, TYPE_Nm);
1.1       kristaps 1326:
1.50      kristaps 1327:                                if (' ' == byte) {
                   1328:                                        start += sz + 1;
                   1329:                                        break;
                   1330:                                }
1.1       kristaps 1331:
1.50      kristaps 1332:                                assert(',' == byte);
                   1333:                                start += sz + 1;
                   1334:                                while (' ' == *start)
                   1335:                                        start++;
                   1336:                        }
1.1       kristaps 1337:
1.50      kristaps 1338:                        if (sv == start) {
                   1339:                                putkey(of, start, TYPE_Nm);
                   1340:                                free(title);
1.61      schwarze 1341:                                return;
1.50      kristaps 1342:                        }
1.1       kristaps 1343:
1.50      kristaps 1344:                        while (isspace((unsigned char)*start))
                   1345:                                start++;
1.1       kristaps 1346:
1.50      kristaps 1347:                        if (0 == strncmp(start, "-", 1))
                   1348:                                start += 1;
                   1349:                        else if (0 == strncmp(start, "\\-\\-", 4))
                   1350:                                start += 4;
                   1351:                        else if (0 == strncmp(start, "\\-", 2))
                   1352:                                start += 2;
                   1353:                        else if (0 == strncmp(start, "\\(en", 4))
                   1354:                                start += 4;
                   1355:                        else if (0 == strncmp(start, "\\(em", 4))
                   1356:                                start += 4;
1.1       kristaps 1357:
1.50      kristaps 1358:                        while (' ' == *start)
                   1359:                                start++;
1.1       kristaps 1360:
1.50      kristaps 1361:                        assert(NULL == of->desc);
1.66      schwarze 1362:                        of->desc = mandoc_strdup(start);
                   1363:                        putkey(of, of->desc, TYPE_Nd);
1.50      kristaps 1364:                        free(title);
1.61      schwarze 1365:                        return;
1.50      kristaps 1366:                }
                   1367:        }
1.1       kristaps 1368:
1.50      kristaps 1369:        for (n = n->child; n; n = n->next)
1.61      schwarze 1370:                parse_man(of, n);
1.1       kristaps 1371: }
                   1372:
                   1373: static void
1.50      kristaps 1374: parse_mdoc(struct of *of, const struct mdoc_node *n)
1.1       kristaps 1375: {
                   1376:
1.50      kristaps 1377:        assert(NULL != n);
                   1378:        for (n = n->child; NULL != n; n = n->next) {
                   1379:                switch (n->type) {
                   1380:                case (MDOC_ELEM):
                   1381:                        /* FALLTHROUGH */
                   1382:                case (MDOC_BLOCK):
                   1383:                        /* FALLTHROUGH */
                   1384:                case (MDOC_HEAD):
                   1385:                        /* FALLTHROUGH */
                   1386:                case (MDOC_BODY):
                   1387:                        /* FALLTHROUGH */
                   1388:                case (MDOC_TAIL):
                   1389:                        if (NULL != mdocs[n->tok].fp)
                   1390:                               if (0 == (*mdocs[n->tok].fp)(of, n))
                   1391:                                       break;
1.68      schwarze 1392:                        if (mdocs[n->tok].mask)
1.50      kristaps 1393:                                putmdockey(of, n->child, mdocs[n->tok].mask);
                   1394:                        break;
                   1395:                default:
                   1396:                        assert(MDOC_ROOT != n->type);
                   1397:                        continue;
                   1398:                }
                   1399:                if (NULL != n->child)
                   1400:                        parse_mdoc(of, n);
1.1       kristaps 1401:        }
                   1402: }
                   1403:
1.25      schwarze 1404: static int
1.50      kristaps 1405: parse_mdoc_Fd(struct of *of, const struct mdoc_node *n)
1.1       kristaps 1406: {
                   1407:        const char      *start, *end;
                   1408:        size_t           sz;
1.25      schwarze 1409:
1.50      kristaps 1410:        if (SEC_SYNOPSIS != n->sec ||
                   1411:                        NULL == (n = n->child) ||
                   1412:                        MDOC_TEXT != n->type)
1.25      schwarze 1413:                return(0);
1.1       kristaps 1414:
                   1415:        /*
                   1416:         * Only consider those `Fd' macro fields that begin with an
                   1417:         * "inclusion" token (versus, e.g., #define).
                   1418:         */
1.50      kristaps 1419:
1.1       kristaps 1420:        if (strcmp("#include", n->string))
1.25      schwarze 1421:                return(0);
1.1       kristaps 1422:
                   1423:        if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.25      schwarze 1424:                return(0);
1.1       kristaps 1425:
                   1426:        /*
                   1427:         * Strip away the enclosing angle brackets and make sure we're
                   1428:         * not zero-length.
                   1429:         */
                   1430:
                   1431:        start = n->string;
                   1432:        if ('<' == *start || '"' == *start)
                   1433:                start++;
                   1434:
                   1435:        if (0 == (sz = strlen(start)))
1.25      schwarze 1436:                return(0);
1.1       kristaps 1437:
                   1438:        end = &start[(int)sz - 1];
                   1439:        if ('>' == *end || '"' == *end)
                   1440:                end--;
                   1441:
1.50      kristaps 1442:        if (end > start)
                   1443:                putkeys(of, start, end - start + 1, TYPE_In);
1.25      schwarze 1444:        return(1);
1.1       kristaps 1445: }
                   1446:
1.25      schwarze 1447: static int
1.50      kristaps 1448: parse_mdoc_In(struct of *of, const struct mdoc_node *n)
1.1       kristaps 1449: {
                   1450:
1.50      kristaps 1451:        if (NULL != n->child && MDOC_TEXT == n->child->type)
1.25      schwarze 1452:                return(0);
1.1       kristaps 1453:
1.50      kristaps 1454:        putkey(of, n->child->string, TYPE_In);
1.25      schwarze 1455:        return(1);
1.1       kristaps 1456: }
                   1457:
1.25      schwarze 1458: static int
1.50      kristaps 1459: parse_mdoc_Fn(struct of *of, const struct mdoc_node *n)
1.1       kristaps 1460: {
                   1461:        const char      *cp;
                   1462:
1.50      kristaps 1463:        if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.25      schwarze 1464:                return(0);
                   1465:
1.50      kristaps 1466:        /*
                   1467:         * Parse: .Fn "struct type *name" "char *arg".
                   1468:         * First strip away pointer symbol.
                   1469:         * Then store the function name, then type.
                   1470:         * Finally, store the arguments.
                   1471:         */
1.1       kristaps 1472:
1.50      kristaps 1473:        if (NULL == (cp = strrchr(n->string, ' ')))
                   1474:                cp = n->string;
1.1       kristaps 1475:
                   1476:        while ('*' == *cp)
                   1477:                cp++;
                   1478:
1.50      kristaps 1479:        putkey(of, cp, TYPE_Fn);
1.25      schwarze 1480:
1.50      kristaps 1481:        if (n->string < cp)
                   1482:                putkeys(of, n->string, cp - n->string, TYPE_Ft);
1.25      schwarze 1483:
1.50      kristaps 1484:        for (n = n->next; NULL != n; n = n->next)
                   1485:                if (MDOC_TEXT == n->type)
                   1486:                        putkey(of, n->string, TYPE_Fa);
1.25      schwarze 1487:
                   1488:        return(0);
1.1       kristaps 1489: }
                   1490:
1.25      schwarze 1491: static int
1.50      kristaps 1492: parse_mdoc_St(struct of *of, const struct mdoc_node *n)
1.1       kristaps 1493: {
1.25      schwarze 1494:
1.1       kristaps 1495:        if (NULL == n->child || MDOC_TEXT != n->child->type)
1.25      schwarze 1496:                return(0);
1.1       kristaps 1497:
1.50      kristaps 1498:        putkey(of, n->child->string, TYPE_St);
1.25      schwarze 1499:        return(1);
1.1       kristaps 1500: }
                   1501:
1.25      schwarze 1502: static int
1.50      kristaps 1503: parse_mdoc_Xr(struct of *of, const struct mdoc_node *n)
1.1       kristaps 1504: {
1.67      schwarze 1505:        char    *cp;
1.1       kristaps 1506:
                   1507:        if (NULL == (n = n->child))
1.25      schwarze 1508:                return(0);
1.1       kristaps 1509:
1.67      schwarze 1510:        if (NULL == n->next) {
                   1511:                putkey(of, n->string, TYPE_Xr);
                   1512:                return(0);
                   1513:        }
                   1514:
                   1515:        if (-1 == asprintf(&cp, "%s(%s)", n->string, n->next->string)) {
                   1516:                perror(NULL);
                   1517:                exit((int)MANDOCLEVEL_SYSERR);
                   1518:        }
                   1519:        putkey(of, cp, TYPE_Xr);
                   1520:        free(cp);
                   1521:        return(0);
1.1       kristaps 1522: }
                   1523:
1.25      schwarze 1524: static int
1.50      kristaps 1525: parse_mdoc_Nd(struct of *of, const struct mdoc_node *n)
1.1       kristaps 1526: {
1.50      kristaps 1527:        size_t           sz;
1.1       kristaps 1528:
                   1529:        if (MDOC_BODY != n->type)
1.25      schwarze 1530:                return(0);
1.1       kristaps 1531:
1.50      kristaps 1532:        /*
                   1533:         * Special-case the `Nd' because we need to put the description
                   1534:         * into the document table.
                   1535:         */
                   1536:
                   1537:        for (n = n->child; NULL != n; n = n->next) {
                   1538:                if (MDOC_TEXT == n->type) {
1.66      schwarze 1539:                        if (NULL != of->desc) {
                   1540:                                sz = strlen(of->desc) +
                   1541:                                     strlen(n->string) + 2;
                   1542:                                of->desc = mandoc_realloc(of->desc, sz);
                   1543:                                strlcat(of->desc, " ", sz);
                   1544:                                strlcat(of->desc, n->string, sz);
                   1545:                        } else
                   1546:                                of->desc = mandoc_strdup(n->string);
1.50      kristaps 1547:                }
                   1548:                if (NULL != n->child)
                   1549:                        parse_mdoc_Nd(of, n);
                   1550:        }
1.25      schwarze 1551:        return(1);
1.1       kristaps 1552: }
                   1553:
1.25      schwarze 1554: static int
1.50      kristaps 1555: parse_mdoc_Nm(struct of *of, const struct mdoc_node *n)
1.1       kristaps 1556: {
                   1557:
1.25      schwarze 1558:        if (SEC_NAME == n->sec)
                   1559:                return(1);
                   1560:        else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1561:                return(0);
1.1       kristaps 1562:
1.25      schwarze 1563:        return(1);
1.1       kristaps 1564: }
                   1565:
1.25      schwarze 1566: static int
1.50      kristaps 1567: parse_mdoc_Sh(struct of *of, const struct mdoc_node *n)
1.1       kristaps 1568: {
                   1569:
1.25      schwarze 1570:        return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1       kristaps 1571: }
                   1572:
1.50      kristaps 1573: static int
                   1574: parse_mdoc_head(struct of *of, const struct mdoc_node *n)
1.1       kristaps 1575: {
                   1576:
1.50      kristaps 1577:        return(MDOC_HEAD == n->type);
                   1578: }
1.1       kristaps 1579:
1.50      kristaps 1580: static int
                   1581: parse_mdoc_body(struct of *of, const struct mdoc_node *n)
                   1582: {
1.1       kristaps 1583:
1.50      kristaps 1584:        return(MDOC_BODY == n->type);
1.1       kristaps 1585: }
                   1586:
1.50      kristaps 1587: /*
1.66      schwarze 1588:  * Add a string to the hash table for the current manual.
                   1589:  * Each string has a bitmask telling which macros it belongs to.
                   1590:  * When we finish the manual, we'll dump the table.
1.50      kristaps 1591:  */
                   1592: static void
1.64      schwarze 1593: putkeys(const struct of *of, const char *cp, size_t sz, uint64_t v)
1.50      kristaps 1594: {
                   1595:        struct str      *s;
1.65      schwarze 1596:        unsigned int     slot;
1.50      kristaps 1597:        const char      *end;
1.25      schwarze 1598:
1.50      kristaps 1599:        if (0 == sz)
                   1600:                return;
1.25      schwarze 1601:
1.65      schwarze 1602:        end = cp + sz;
                   1603:        slot = ohash_qlookupi(&strings, cp, &end);
                   1604:        s = ohash_find(&strings, slot);
1.25      schwarze 1605:
1.50      kristaps 1606:        if (NULL != s && of == s->of) {
                   1607:                s->mask |= v;
                   1608:                return;
                   1609:        } else if (NULL == s) {
1.51      kristaps 1610:                s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
1.50      kristaps 1611:                memcpy(s->key, cp, sz);
1.65      schwarze 1612:                ohash_insert(&strings, slot, s);
1.1       kristaps 1613:        }
1.50      kristaps 1614:        s->of = of;
                   1615:        s->mask = v;
1.1       kristaps 1616: }
                   1617:
1.50      kristaps 1618: /*
                   1619:  * Take a Unicode codepoint and produce its UTF-8 encoding.
                   1620:  * This isn't the best way to do this, but it works.
                   1621:  * The magic numbers are from the UTF-8 packaging.
                   1622:  * They're not as scary as they seem: read the UTF-8 spec for details.
                   1623:  */
                   1624: static size_t
                   1625: utf8(unsigned int cp, char out[7])
1.1       kristaps 1626: {
1.50      kristaps 1627:        size_t           rc;
1.1       kristaps 1628:
1.50      kristaps 1629:        rc = 0;
                   1630:        if (cp <= 0x0000007F) {
                   1631:                rc = 1;
                   1632:                out[0] = (char)cp;
                   1633:        } else if (cp <= 0x000007FF) {
                   1634:                rc = 2;
                   1635:                out[0] = (cp >> 6  & 31) | 192;
                   1636:                out[1] = (cp       & 63) | 128;
                   1637:        } else if (cp <= 0x0000FFFF) {
                   1638:                rc = 3;
                   1639:                out[0] = (cp >> 12 & 15) | 224;
                   1640:                out[1] = (cp >> 6  & 63) | 128;
                   1641:                out[2] = (cp       & 63) | 128;
                   1642:        } else if (cp <= 0x001FFFFF) {
                   1643:                rc = 4;
                   1644:                out[0] = (cp >> 18 &  7) | 240;
                   1645:                out[1] = (cp >> 12 & 63) | 128;
                   1646:                out[2] = (cp >> 6  & 63) | 128;
                   1647:                out[3] = (cp       & 63) | 128;
                   1648:        } else if (cp <= 0x03FFFFFF) {
                   1649:                rc = 5;
                   1650:                out[0] = (cp >> 24 &  3) | 248;
                   1651:                out[1] = (cp >> 18 & 63) | 128;
                   1652:                out[2] = (cp >> 12 & 63) | 128;
                   1653:                out[3] = (cp >> 6  & 63) | 128;
                   1654:                out[4] = (cp       & 63) | 128;
                   1655:        } else if (cp <= 0x7FFFFFFF) {
                   1656:                rc = 6;
                   1657:                out[0] = (cp >> 30 &  1) | 252;
                   1658:                out[1] = (cp >> 24 & 63) | 128;
                   1659:                out[2] = (cp >> 18 & 63) | 128;
                   1660:                out[3] = (cp >> 12 & 63) | 128;
                   1661:                out[4] = (cp >> 6  & 63) | 128;
                   1662:                out[5] = (cp       & 63) | 128;
                   1663:        } else
1.1       kristaps 1664:                return(0);
                   1665:
1.50      kristaps 1666:        out[rc] = '\0';
                   1667:        return(rc);
                   1668: }
1.1       kristaps 1669:
1.50      kristaps 1670: /*
                   1671:  * Store the UTF-8 version of a key, or alias the pointer if the key has
                   1672:  * no UTF-8 transcription marks in it.
                   1673:  */
                   1674: static void
                   1675: utf8key(struct mchars *mc, struct str *key)
                   1676: {
                   1677:        size_t           sz, bsz, pos;
                   1678:        char             utfbuf[7], res[5];
                   1679:        char            *buf;
                   1680:        const char      *seq, *cpp, *val;
                   1681:        int              len, u;
                   1682:        enum mandoc_esc  esc;
                   1683:
                   1684:        assert(NULL == key->utf8);
                   1685:
                   1686:        res[0] = '\\';
                   1687:        res[1] = '\t';
                   1688:        res[2] = ASCII_NBRSP;
                   1689:        res[3] = ASCII_HYPH;
                   1690:        res[4] = '\0';
1.1       kristaps 1691:
1.50      kristaps 1692:        val = key->key;
                   1693:        bsz = strlen(val);
1.46      kristaps 1694:
1.50      kristaps 1695:        /*
                   1696:         * Pre-check: if we have no stop-characters, then set the
                   1697:         * pointer as ourselvse and get out of here.
                   1698:         */
                   1699:        if (strcspn(val, res) == bsz) {
                   1700:                key->utf8 = key->key;
                   1701:                return;
                   1702:        }
1.46      kristaps 1703:
1.50      kristaps 1704:        /* Pre-allocate by the length of the input */
1.46      kristaps 1705:
1.50      kristaps 1706:        buf = mandoc_malloc(++bsz);
                   1707:        pos = 0;
1.46      kristaps 1708:
1.50      kristaps 1709:        while ('\0' != *val) {
                   1710:                /*
                   1711:                 * Halt on the first escape sequence.
                   1712:                 * This also halts on the end of string, in which case
                   1713:                 * we just copy, fallthrough, and exit the loop.
                   1714:                 */
                   1715:                if ((sz = strcspn(val, res)) > 0) {
                   1716:                        memcpy(&buf[pos], val, sz);
                   1717:                        pos += sz;
                   1718:                        val += sz;
                   1719:                }
1.46      kristaps 1720:
1.50      kristaps 1721:                if (ASCII_HYPH == *val) {
                   1722:                        buf[pos++] = '-';
                   1723:                        val++;
                   1724:                        continue;
                   1725:                } else if ('\t' == *val || ASCII_NBRSP == *val) {
                   1726:                        buf[pos++] = ' ';
                   1727:                        val++;
                   1728:                        continue;
                   1729:                } else if ('\\' != *val)
                   1730:                        break;
1.46      kristaps 1731:
1.50      kristaps 1732:                /* Read past the slash. */
1.46      kristaps 1733:
1.50      kristaps 1734:                val++;
                   1735:                u = 0;
1.46      kristaps 1736:
1.50      kristaps 1737:                /*
                   1738:                 * Parse the escape sequence and see if it's a
                   1739:                 * predefined character or special character.
                   1740:                 */
                   1741:                esc = mandoc_escape
                   1742:                        ((const char **)&val, &seq, &len);
                   1743:                if (ESCAPE_ERROR == esc)
                   1744:                        break;
1.1       kristaps 1745:
1.50      kristaps 1746:                if (ESCAPE_SPECIAL != esc)
                   1747:                        continue;
                   1748:                if (0 == (u = mchars_spec2cp(mc, seq, len)))
                   1749:                        continue;
1.1       kristaps 1750:
1.50      kristaps 1751:                /*
                   1752:                 * If we have a Unicode codepoint, try to convert that
                   1753:                 * to a UTF-8 byte string.
                   1754:                 */
                   1755:                cpp = utfbuf;
                   1756:                if (0 == (sz = utf8(u, utfbuf)))
                   1757:                        continue;
1.1       kristaps 1758:
1.50      kristaps 1759:                /* Copy the rendered glyph into the stream. */
1.1       kristaps 1760:
1.50      kristaps 1761:                sz = strlen(cpp);
                   1762:                bsz += sz;
1.1       kristaps 1763:
1.50      kristaps 1764:                buf = mandoc_realloc(buf, bsz);
1.1       kristaps 1765:
1.50      kristaps 1766:                memcpy(&buf[pos], cpp, sz);
                   1767:                pos += sz;
1.1       kristaps 1768:        }
                   1769:
1.50      kristaps 1770:        buf[pos] = '\0';
                   1771:        key->utf8 = buf;
1.1       kristaps 1772: }
                   1773:
1.14      schwarze 1774: /*
1.50      kristaps 1775:  * Flush the current page's terms (and their bits) into the database.
                   1776:  * Wrap the entire set of additions in a transaction to make sqlite be a
                   1777:  * little faster.
                   1778:  * Also, UTF-8-encode the description at the last possible moment.
1.14      schwarze 1779:  */
                   1780: static void
1.59      schwarze 1781: dbindex(struct mchars *mc, int form, const struct of *of)
1.14      schwarze 1782: {
1.50      kristaps 1783:        struct str      *key;
                   1784:        const char      *desc;
                   1785:        int64_t          recno;
1.52      kristaps 1786:        size_t           i;
1.66      schwarze 1787:        unsigned int     slot;
1.14      schwarze 1788:
1.56      schwarze 1789:        if (verb)
1.59      schwarze 1790:                say(of->file, "Adding to index");
1.43      kristaps 1791:
1.50      kristaps 1792:        if (nodb)
1.14      schwarze 1793:                return;
1.28      kristaps 1794:
1.50      kristaps 1795:        desc = "";
                   1796:        if (NULL != of->desc) {
1.65      schwarze 1797:                key = ohash_find(&strings,
                   1798:                        ohash_qlookup(&strings, of->desc));
1.50      kristaps 1799:                assert(NULL != key);
                   1800:                if (NULL == key->utf8)
                   1801:                        utf8key(mc, key);
                   1802:                desc = key->utf8;
                   1803:        }
                   1804:
1.52      kristaps 1805:        SQL_EXEC("BEGIN TRANSACTION");
1.50      kristaps 1806:
1.52      kristaps 1807:        i = 1;
                   1808:        SQL_BIND_TEXT(stmts[STMT_INSERT_DOC], i, of->file);
                   1809:        SQL_BIND_TEXT(stmts[STMT_INSERT_DOC], i, of->sec);
                   1810:        SQL_BIND_TEXT(stmts[STMT_INSERT_DOC], i, of->arch);
                   1811:        SQL_BIND_TEXT(stmts[STMT_INSERT_DOC], i, desc);
                   1812:        SQL_BIND_INT(stmts[STMT_INSERT_DOC], i, form);
                   1813:        SQL_STEP(stmts[STMT_INSERT_DOC]);
1.50      kristaps 1814:        recno = sqlite3_last_insert_rowid(db);
                   1815:        sqlite3_reset(stmts[STMT_INSERT_DOC]);
                   1816:
1.66      schwarze 1817:        for (key = ohash_first(&strings, &slot); NULL != key;
                   1818:             key = ohash_next(&strings, &slot)) {
1.50      kristaps 1819:                assert(key->of == of);
                   1820:                if (NULL == key->utf8)
                   1821:                        utf8key(mc, key);
1.52      kristaps 1822:                i = 1;
                   1823:                SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);
                   1824:                SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->utf8);
                   1825:                SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, recno);
                   1826:                SQL_STEP(stmts[STMT_INSERT_KEY]);
1.50      kristaps 1827:                sqlite3_reset(stmts[STMT_INSERT_KEY]);
1.66      schwarze 1828:                if (key->utf8 != key->key)
                   1829:                        free(key->utf8);
                   1830:                free(key);
1.38      schwarze 1831:        }
1.28      kristaps 1832:
1.52      kristaps 1833:        SQL_EXEC("END TRANSACTION");
1.14      schwarze 1834: }
                   1835:
1.5       kristaps 1836: static void
1.59      schwarze 1837: dbprune(void)
1.5       kristaps 1838: {
1.50      kristaps 1839:        struct of       *of;
1.52      kristaps 1840:        size_t           i;
1.5       kristaps 1841:
1.50      kristaps 1842:        if (nodb)
                   1843:                return;
1.12      schwarze 1844:
1.50      kristaps 1845:        for (of = ofs; NULL != of; of = of->next) {
1.52      kristaps 1846:                i = 1;
                   1847:                SQL_BIND_TEXT(stmts[STMT_DELETE], i, of->file);
                   1848:                SQL_STEP(stmts[STMT_DELETE]);
1.50      kristaps 1849:                sqlite3_reset(stmts[STMT_DELETE]);
1.56      schwarze 1850:                if (verb)
1.59      schwarze 1851:                        say(of->file, "Deleted from index");
1.5       kristaps 1852:        }
                   1853: }
                   1854:
1.4       kristaps 1855: /*
1.50      kristaps 1856:  * Close an existing database and its prepared statements.
                   1857:  * If "real" is not set, rename the temporary file into the real one.
1.4       kristaps 1858:  */
1.35      kristaps 1859: static void
1.59      schwarze 1860: dbclose(int real)
1.4       kristaps 1861: {
1.50      kristaps 1862:        size_t           i;
1.4       kristaps 1863:
1.50      kristaps 1864:        if (nodb)
1.38      schwarze 1865:                return;
1.50      kristaps 1866:
                   1867:        for (i = 0; i < STMT__MAX; i++) {
                   1868:                sqlite3_finalize(stmts[i]);
                   1869:                stmts[i] = NULL;
1.4       kristaps 1870:        }
                   1871:
1.50      kristaps 1872:        sqlite3_close(db);
                   1873:        db = NULL;
1.12      schwarze 1874:
1.50      kristaps 1875:        if (real)
                   1876:                return;
1.12      schwarze 1877:
1.63      schwarze 1878:        if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {
1.59      schwarze 1879:                exitcode = (int)MANDOCLEVEL_SYSERR;
                   1880:                say(MANDOC_DB, NULL);
                   1881:        }
1.50      kristaps 1882: }
1.14      schwarze 1883:
1.50      kristaps 1884: /*
                   1885:  * This is straightforward stuff.
                   1886:  * Open a database connection to a "temporary" database, then open a set
                   1887:  * of prepared statements we'll use over and over again.
                   1888:  * If "real" is set, we use the existing database; if not, we truncate a
                   1889:  * temporary one.
                   1890:  * Must be matched by dbclose().
                   1891:  */
                   1892: static int
1.59      schwarze 1893: dbopen(int real)
1.50      kristaps 1894: {
1.63      schwarze 1895:        const char      *file, *sql;
1.50      kristaps 1896:        int              rc, ofl;
1.12      schwarze 1897:
1.50      kristaps 1898:        if (nodb)
                   1899:                return(1);
1.12      schwarze 1900:
1.63      schwarze 1901:        ofl = SQLITE_OPEN_READWRITE;
                   1902:        if (0 == real) {
                   1903:                file = MANDOC_DB "~";
                   1904:                if (-1 == remove(file) && ENOENT != errno) {
                   1905:                        exitcode = (int)MANDOCLEVEL_SYSERR;
                   1906:                        say(file, NULL);
                   1907:                        return(0);
                   1908:                }
                   1909:                ofl |= SQLITE_OPEN_EXCLUSIVE;
                   1910:        } else
                   1911:                file = MANDOC_DB;
1.45      kristaps 1912:
1.50      kristaps 1913:        rc = sqlite3_open_v2(file, &db, ofl, NULL);
                   1914:        if (SQLITE_OK == rc)
1.57      schwarze 1915:                goto prepare_statements;
1.50      kristaps 1916:        if (SQLITE_CANTOPEN != rc) {
1.59      schwarze 1917:                exitcode = (int)MANDOCLEVEL_SYSERR;
                   1918:                say(file, NULL);
1.50      kristaps 1919:                return(0);
                   1920:        }
1.12      schwarze 1921:
1.50      kristaps 1922:        sqlite3_close(db);
                   1923:        db = NULL;
1.12      schwarze 1924:
1.50      kristaps 1925:        if (SQLITE_OK != (rc = sqlite3_open(file, &db))) {
1.59      schwarze 1926:                exitcode = (int)MANDOCLEVEL_SYSERR;
                   1927:                say(file, NULL);
1.50      kristaps 1928:                return(0);
                   1929:        }
1.12      schwarze 1930:
1.50      kristaps 1931:        sql = "CREATE TABLE \"docs\" (\n"
                   1932:              " \"file\" TEXT NOT NULL,\n"
                   1933:              " \"sec\" TEXT NOT NULL,\n"
                   1934:              " \"arch\" TEXT NOT NULL,\n"
                   1935:              " \"desc\" TEXT NOT NULL,\n"
                   1936:              " \"form\" INTEGER NOT NULL,\n"
                   1937:              " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
                   1938:              ");\n"
                   1939:              "\n"
                   1940:              "CREATE TABLE \"keys\" (\n"
                   1941:              " \"bits\" INTEGER NOT NULL,\n"
                   1942:              " \"key\" TEXT NOT NULL,\n"
                   1943:              " \"docid\" INTEGER NOT NULL REFERENCES docs(id) "
                   1944:                "ON DELETE CASCADE,\n"
                   1945:              " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
                   1946:              ");\n"
                   1947:              "\n"
                   1948:              "CREATE INDEX \"key_index\" ON keys (key);\n";
1.14      schwarze 1949:
1.50      kristaps 1950:        if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
1.59      schwarze 1951:                exitcode = (int)MANDOCLEVEL_SYSERR;
                   1952:                say(file, "%s", sqlite3_errmsg(db));
1.50      kristaps 1953:                return(0);
                   1954:        }
1.4       kristaps 1955:
1.57      schwarze 1956: prepare_statements:
                   1957:        SQL_EXEC("PRAGMA foreign_keys = ON");
1.50      kristaps 1958:        sql = "DELETE FROM docs where file=?";
                   1959:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE], NULL);
                   1960:        sql = "INSERT INTO docs "
                   1961:                "(file,sec,arch,desc,form) VALUES (?,?,?,?,?)";
                   1962:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_DOC], NULL);
                   1963:        sql = "INSERT INTO keys "
                   1964:                "(bits,key,docid) VALUES (?,?,?)";
                   1965:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL);
1.70    ! schwarze 1966:
        !          1967: #ifndef __APPLE__
        !          1968:        /*
        !          1969:         * When opening a new database, we can turn off
        !          1970:         * synchronous mode for much better performance.
        !          1971:         */
        !          1972:
        !          1973:        if (real)
        !          1974:                SQL_EXEC("PRAGMA synchronous = OFF");
        !          1975: #endif
        !          1976:
1.50      kristaps 1977:        return(1);
                   1978: }
1.5       kristaps 1979:
1.50      kristaps 1980: static void *
                   1981: hash_halloc(size_t sz, void *arg)
                   1982: {
1.12      schwarze 1983:
1.50      kristaps 1984:        return(mandoc_calloc(sz, 1));
                   1985: }
1.12      schwarze 1986:
1.50      kristaps 1987: static void *
                   1988: hash_alloc(size_t sz, void *arg)
                   1989: {
1.5       kristaps 1990:
1.50      kristaps 1991:        return(mandoc_malloc(sz));
                   1992: }
1.41      kristaps 1993:
1.50      kristaps 1994: static void
                   1995: hash_free(void *p, size_t sz, void *arg)
                   1996: {
1.4       kristaps 1997:
1.50      kristaps 1998:        free(p);
1.4       kristaps 1999: }
                   2000:
1.50      kristaps 2001: static int
1.59      schwarze 2002: set_basedir(const char *targetdir)
1.4       kristaps 2003: {
1.59      schwarze 2004:        static char      startdir[PATH_MAX];
                   2005:        static int       fd;
1.4       kristaps 2006:
1.59      schwarze 2007:        /*
                   2008:         * Remember where we started by keeping a fd open to the origin
                   2009:         * path component: throughout this utility, we chdir() a lot to
                   2010:         * handle relative paths, and by doing this, we can return to
                   2011:         * the starting point.
                   2012:         */
                   2013:        if ('\0' == *startdir) {
                   2014:                if (NULL == getcwd(startdir, PATH_MAX)) {
                   2015:                        exitcode = (int)MANDOCLEVEL_SYSERR;
                   2016:                        if (NULL != targetdir)
                   2017:                                say(".", NULL);
                   2018:                        return(0);
                   2019:                }
                   2020:                if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
                   2021:                        exitcode = (int)MANDOCLEVEL_SYSERR;
                   2022:                        say(startdir, NULL);
                   2023:                        return(0);
                   2024:                }
                   2025:                if (NULL == targetdir)
                   2026:                        targetdir = startdir;
                   2027:        } else {
                   2028:                if (-1 == fd)
                   2029:                        return(0);
                   2030:                if (-1 == fchdir(fd)) {
                   2031:                        close(fd);
                   2032:                        basedir[0] = '\0';
                   2033:                        exitcode = (int)MANDOCLEVEL_SYSERR;
                   2034:                        say(startdir, NULL);
                   2035:                        return(0);
                   2036:                }
                   2037:                if (NULL == targetdir) {
                   2038:                        close(fd);
                   2039:                        return(1);
                   2040:                }
                   2041:        }
                   2042:        if (NULL == realpath(targetdir, basedir)) {
                   2043:                basedir[0] = '\0';
                   2044:                exitcode = (int)MANDOCLEVEL_BADARG;
                   2045:                say(targetdir, NULL);
1.50      kristaps 2046:                return(0);
1.59      schwarze 2047:        } else if (-1 == chdir(basedir)) {
                   2048:                exitcode = (int)MANDOCLEVEL_BADARG;
                   2049:                say("", NULL);
1.50      kristaps 2050:                return(0);
1.4       kristaps 2051:        }
1.50      kristaps 2052:        return(1);
1.56      schwarze 2053: }
                   2054:
                   2055: static void
1.59      schwarze 2056: say(const char *file, const char *format, ...)
1.56      schwarze 2057: {
                   2058:        va_list          ap;
                   2059:
1.59      schwarze 2060:        if ('\0' != *basedir)
                   2061:                fprintf(stderr, "%s", basedir);
                   2062:        if ('\0' != *basedir && '\0' != *file)
                   2063:                fputs("//", stderr);
1.56      schwarze 2064:        if ('\0' != *file)
1.59      schwarze 2065:                fprintf(stderr, "%s", file);
1.56      schwarze 2066:        fputs(": ", stderr);
1.59      schwarze 2067:
                   2068:        if (NULL == format) {
                   2069:                perror(NULL);
                   2070:                return;
                   2071:        }
1.56      schwarze 2072:
                   2073:        va_start(ap, format);
                   2074:        vfprintf(stderr, format, ap);
                   2075:        va_end(ap);
                   2076:
                   2077:        fputc('\n', stderr);
1.1       kristaps 2078: }

CVSweb