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

Annotation of mandoc/mandocdb.c, Revision 1.84

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

CVSweb