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

Annotation of mandoc/mandocdb.c, Revision 1.94

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

CVSweb