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

Annotation of mandoc/mandocdb.c, Revision 1.82

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

CVSweb