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

Annotation of mandoc/mandocdb.c, Revision 1.85

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

CVSweb