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

Annotation of mandoc/mandocdb.c, Revision 1.217

1.217   ! schwarze    1: /*     $Id: mandocdb.c,v 1.216 2016/03/17 21:26:26 schwarze Exp $ */
1.1       kristaps    2: /*
1.48      schwarze    3:  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.211     schwarze    4:  * Copyright (c) 2011-2016 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:  *
1.187     schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.187     schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       kristaps   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: #include "config.h"
                     19:
1.162     schwarze   20: #include <sys/types.h>
1.50      kristaps   21: #include <sys/stat.h>
1.115     schwarze   22: #include <sys/wait.h>
1.1       kristaps   23:
                     24: #include <assert.h>
1.43      kristaps   25: #include <ctype.h>
1.208     schwarze   26: #if HAVE_ERR
1.198     schwarze   27: #include <err.h>
1.208     schwarze   28: #endif
1.63      schwarze   29: #include <errno.h>
1.1       kristaps   30: #include <fcntl.h>
1.158     schwarze   31: #if HAVE_FTS
1.50      kristaps   32: #include <fts.h>
1.157     schwarze   33: #else
                     34: #include "compat_fts.h"
                     35: #endif
1.58      schwarze   36: #include <limits.h>
1.50      kristaps   37: #include <stddef.h>
1.59      schwarze   38: #include <stdio.h>
1.1       kristaps   39: #include <stdint.h>
                     40: #include <stdlib.h>
                     41: #include <string.h>
1.17      schwarze   42: #include <unistd.h>
1.1       kristaps   43:
1.50      kristaps   44: #include <sqlite3.h>
1.1       kristaps   45:
1.188     schwarze   46: #include "mandoc_aux.h"
1.202     schwarze   47: #include "mandoc_ohash.h"
1.188     schwarze   48: #include "mandoc.h"
                     49: #include "roff.h"
1.50      kristaps   50: #include "mdoc.h"
1.1       kristaps   51: #include "man.h"
1.187     schwarze   52: #include "manconf.h"
1.55      kristaps   53: #include "mansearch.h"
1.1       kristaps   54:
1.111     schwarze   55: extern int mansearch_keymax;
                     56: extern const char *const mansearch_keynames[];
                     57:
1.52      kristaps   58: #define        SQL_EXEC(_v) \
                     59:        if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
1.134     schwarze   60:                say("", "%s: %s", (_v), sqlite3_errmsg(db))
1.52      kristaps   61: #define        SQL_BIND_TEXT(_s, _i, _v) \
                     62:        if (SQLITE_OK != sqlite3_bind_text \
                     63:                ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
1.134     schwarze   64:                say(mlink->file, "%s", sqlite3_errmsg(db))
1.52      kristaps   65: #define        SQL_BIND_INT(_s, _i, _v) \
                     66:        if (SQLITE_OK != sqlite3_bind_int \
                     67:                ((_s), (_i)++, (_v))) \
1.134     schwarze   68:                say(mlink->file, "%s", sqlite3_errmsg(db))
1.52      kristaps   69: #define        SQL_BIND_INT64(_s, _i, _v) \
                     70:        if (SQLITE_OK != sqlite3_bind_int64 \
                     71:                ((_s), (_i)++, (_v))) \
1.134     schwarze   72:                say(mlink->file, "%s", sqlite3_errmsg(db))
1.52      kristaps   73: #define SQL_STEP(_s) \
                     74:        if (SQLITE_DONE != sqlite3_step((_s))) \
1.134     schwarze   75:                say(mlink->file, "%s", sqlite3_errmsg(db))
1.52      kristaps   76:
1.50      kristaps   77: enum   op {
                     78:        OP_DEFAULT = 0, /* new dbs from dir list or default config */
                     79:        OP_CONFFILE, /* new databases from custom config file */
                     80:        OP_UPDATE, /* delete/add entries in existing database */
                     81:        OP_DELETE, /* delete entries from existing database */
                     82:        OP_TEST /* change no databases, report potential problems */
1.38      schwarze   83: };
                     84:
1.50      kristaps   85: struct str {
1.78      schwarze   86:        const struct mpage *mpage; /* if set, the owning parse */
1.50      kristaps   87:        uint64_t         mask; /* bitmask in sequence */
1.176     schwarze   88:        char             key[]; /* rendered text */
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.137     schwarze   98:        int64_t          pageid;  /* pageid in mpages SQL table */
1.82      schwarze   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 */
1.161     schwarze  104:        int              form;    /* format from file content */
1.174     schwarze  105:        int              name_head_done;
1.82      schwarze  106: };
                    107:
                    108: struct mlink {
1.58      schwarze  109:        char             file[PATH_MAX]; /* filename rel. to manpath */
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.118     schwarze  115:        struct mpage    *mpage;   /* parent */
1.161     schwarze  116:        int              dform;   /* format from directory */
                    117:        int              fform;   /* format from file name suffix */
1.124     schwarze  118:        int              gzip;    /* filename has a .gz suffix */
1.3       kristaps  119: };
                    120:
1.50      kristaps  121: enum   stmt {
1.81      schwarze  122:        STMT_DELETE_PAGE = 0,   /* delete mpage */
                    123:        STMT_INSERT_PAGE,       /* insert mpage */
                    124:        STMT_INSERT_LINK,       /* insert mlink */
1.133     schwarze  125:        STMT_INSERT_NAME,       /* insert name */
1.175     schwarze  126:        STMT_SELECT_NAME,       /* retrieve existing name flags */
1.81      schwarze  127:        STMT_INSERT_KEY,        /* insert parsed key */
1.50      kristaps  128:        STMT__MAX
1.1       kristaps  129: };
                    130:
1.190     schwarze  131: typedef        int (*mdoc_fp)(struct mpage *, const struct roff_meta *,
1.189     schwarze  132:                        const struct roff_node *);
1.1       kristaps  133:
1.50      kristaps  134: struct mdoc_handler {
                    135:        mdoc_fp          fp; /* optional handler */
                    136:        uint64_t         mask;  /* set unless handler returns 0 */
1.1       kristaps  137: };
                    138:
1.59      schwarze  139: static void     dbclose(int);
1.176     schwarze  140: static void     dbadd(struct mpage *);
1.118     schwarze  141: static void     dbadd_mlink(const struct mlink *mlink);
1.169     schwarze  142: static void     dbadd_mlink_name(const struct mlink *mlink);
1.59      schwarze  143: static int      dbopen(int);
                    144: static void     dbprune(void);
                    145: static void     filescan(const char *);
1.84      schwarze  146: static void     mlink_add(struct mlink *, const struct stat *);
1.131     schwarze  147: static void     mlink_check(struct mpage *, struct mlink *);
1.82      schwarze  148: static void     mlink_free(struct mlink *);
1.89      schwarze  149: static void     mlinks_undupe(struct mpage *);
1.78      schwarze  150: static void     mpages_free(void);
1.176     schwarze  151: static void     mpages_merge(struct mparse *);
1.130     schwarze  152: static void     names_check(void);
1.124     schwarze  153: static void     parse_cat(struct mpage *, int);
1.190     schwarze  154: static void     parse_man(struct mpage *, const struct roff_meta *,
1.189     schwarze  155:                        const struct roff_node *);
1.190     schwarze  156: static void     parse_mdoc(struct mpage *, const struct roff_meta *,
1.189     schwarze  157:                        const struct roff_node *);
1.190     schwarze  158: static int      parse_mdoc_head(struct mpage *, const struct roff_meta *,
1.189     schwarze  159:                        const struct roff_node *);
1.190     schwarze  160: static int      parse_mdoc_Fd(struct mpage *, const struct roff_meta *,
1.189     schwarze  161:                        const struct roff_node *);
                    162: static void     parse_mdoc_fname(struct mpage *, const struct roff_node *);
1.190     schwarze  163: static int      parse_mdoc_Fn(struct mpage *, const struct roff_meta *,
1.189     schwarze  164:                        const struct roff_node *);
1.190     schwarze  165: static int      parse_mdoc_Fo(struct mpage *, const struct roff_meta *,
1.189     schwarze  166:                        const struct roff_node *);
1.190     schwarze  167: static int      parse_mdoc_Nd(struct mpage *, const struct roff_meta *,
1.189     schwarze  168:                        const struct roff_node *);
1.190     schwarze  169: static int      parse_mdoc_Nm(struct mpage *, const struct roff_meta *,
1.189     schwarze  170:                        const struct roff_node *);
1.190     schwarze  171: static int      parse_mdoc_Sh(struct mpage *, const struct roff_meta *,
1.189     schwarze  172:                        const struct roff_node *);
1.211     schwarze  173: static int      parse_mdoc_Va(struct mpage *, const struct roff_meta *,
                    174:                        const struct roff_node *);
1.190     schwarze  175: static int      parse_mdoc_Xr(struct mpage *, const struct roff_meta *,
1.189     schwarze  176:                        const struct roff_node *);
1.112     schwarze  177: static void     putkey(const struct mpage *, char *, uint64_t);
1.176     schwarze  178: static void     putkeys(const struct mpage *, char *, size_t, uint64_t);
1.78      schwarze  179: static void     putmdockey(const struct mpage *,
1.189     schwarze  180:                        const struct roff_node *, uint64_t);
1.176     schwarze  181: static int      render_string(char **, size_t *);
1.59      schwarze  182: static void     say(const char *, const char *, ...);
1.165     schwarze  183: static int      set_basedir(const char *, int);
1.59      schwarze  184: static int      treescan(void);
1.50      kristaps  185: static size_t   utf8(unsigned int, char [7]);
                    186:
1.115     schwarze  187: static char             tempfilename[32];
1.102     schwarze  188: static int              nodb; /* no database changes */
1.116     schwarze  189: static int              mparse_options; /* abort the parse early */
1.141     schwarze  190: static int              use_all; /* use all found files */
                    191: static int              debug; /* print what we're doing */
                    192: static int              warnings; /* warn about crap */
1.95      schwarze  193: static int              write_utf8; /* write UTF-8 output; else ASCII */
1.59      schwarze  194: static int              exitcode; /* to be returned by main */
1.141     schwarze  195: static enum op          op; /* operational mode */
1.59      schwarze  196: static char             basedir[PATH_MAX]; /* current base directory */
1.78      schwarze  197: static struct ohash     mpages; /* table of distinct manual pages */
1.83      schwarze  198: static struct ohash     mlinks; /* table of directory entries */
1.133     schwarze  199: static struct ohash     names; /* table of all names */
1.50      kristaps  200: static struct ohash     strings; /* table of all strings */
                    201: static sqlite3         *db = NULL; /* current database */
                    202: static sqlite3_stmt    *stmts[STMT__MAX]; /* current statements */
1.133     schwarze  203: static uint64_t         name_mask;
1.25      schwarze  204:
                    205: static const struct mdoc_handler mdocs[MDOC_MAX] = {
1.68      schwarze  206:        { NULL, 0 },  /* Ap */
                    207:        { NULL, 0 },  /* Dd */
                    208:        { NULL, 0 },  /* Dt */
                    209:        { NULL, 0 },  /* Os */
                    210:        { parse_mdoc_Sh, TYPE_Sh }, /* Sh */
                    211:        { parse_mdoc_head, TYPE_Ss }, /* Ss */
                    212:        { NULL, 0 },  /* Pp */
                    213:        { NULL, 0 },  /* D1 */
                    214:        { NULL, 0 },  /* Dl */
                    215:        { NULL, 0 },  /* Bd */
                    216:        { NULL, 0 },  /* Ed */
                    217:        { NULL, 0 },  /* Bl */
                    218:        { NULL, 0 },  /* El */
                    219:        { NULL, 0 },  /* It */
                    220:        { NULL, 0 },  /* Ad */
                    221:        { NULL, TYPE_An },  /* An */
                    222:        { NULL, TYPE_Ar },  /* Ar */
                    223:        { NULL, TYPE_Cd },  /* Cd */
                    224:        { NULL, TYPE_Cm },  /* Cm */
                    225:        { NULL, TYPE_Dv },  /* Dv */
                    226:        { NULL, TYPE_Er },  /* Er */
                    227:        { NULL, TYPE_Ev },  /* Ev */
                    228:        { NULL, 0 },  /* Ex */
                    229:        { NULL, TYPE_Fa },  /* Fa */
                    230:        { parse_mdoc_Fd, 0 },  /* Fd */
                    231:        { NULL, TYPE_Fl },  /* Fl */
                    232:        { parse_mdoc_Fn, 0 },  /* Fn */
                    233:        { NULL, TYPE_Ft },  /* Ft */
                    234:        { NULL, TYPE_Ic },  /* Ic */
1.92      schwarze  235:        { NULL, TYPE_In },  /* In */
1.68      schwarze  236:        { NULL, TYPE_Li },  /* Li */
1.132     schwarze  237:        { parse_mdoc_Nd, 0 },  /* Nd */
1.129     schwarze  238:        { parse_mdoc_Nm, 0 },  /* Nm */
1.68      schwarze  239:        { NULL, 0 },  /* Op */
                    240:        { NULL, 0 },  /* Ot */
                    241:        { NULL, TYPE_Pa },  /* Pa */
                    242:        { NULL, 0 },  /* Rv */
1.92      schwarze  243:        { NULL, TYPE_St },  /* St */
1.211     schwarze  244:        { parse_mdoc_Va, TYPE_Va },  /* Va */
                    245:        { parse_mdoc_Va, TYPE_Vt },  /* Vt */
1.68      schwarze  246:        { parse_mdoc_Xr, 0 },  /* Xr */
                    247:        { NULL, 0 },  /* %A */
                    248:        { NULL, 0 },  /* %B */
                    249:        { NULL, 0 },  /* %D */
                    250:        { NULL, 0 },  /* %I */
                    251:        { NULL, 0 },  /* %J */
                    252:        { NULL, 0 },  /* %N */
                    253:        { NULL, 0 },  /* %O */
                    254:        { NULL, 0 },  /* %P */
                    255:        { NULL, 0 },  /* %R */
                    256:        { NULL, 0 },  /* %T */
                    257:        { NULL, 0 },  /* %V */
                    258:        { NULL, 0 },  /* Ac */
                    259:        { NULL, 0 },  /* Ao */
                    260:        { NULL, 0 },  /* Aq */
                    261:        { NULL, TYPE_At },  /* At */
                    262:        { NULL, 0 },  /* Bc */
                    263:        { NULL, 0 },  /* Bf */
                    264:        { NULL, 0 },  /* Bo */
                    265:        { NULL, 0 },  /* Bq */
                    266:        { NULL, TYPE_Bsx },  /* Bsx */
                    267:        { NULL, TYPE_Bx },  /* Bx */
                    268:        { NULL, 0 },  /* Db */
                    269:        { NULL, 0 },  /* Dc */
                    270:        { NULL, 0 },  /* Do */
                    271:        { NULL, 0 },  /* Dq */
                    272:        { NULL, 0 },  /* Ec */
                    273:        { NULL, 0 },  /* Ef */
                    274:        { NULL, TYPE_Em },  /* Em */
                    275:        { NULL, 0 },  /* Eo */
                    276:        { NULL, TYPE_Fx },  /* Fx */
                    277:        { NULL, TYPE_Ms },  /* Ms */
                    278:        { NULL, 0 },  /* No */
                    279:        { NULL, 0 },  /* Ns */
                    280:        { NULL, TYPE_Nx },  /* Nx */
                    281:        { NULL, TYPE_Ox },  /* Ox */
                    282:        { NULL, 0 },  /* Pc */
                    283:        { NULL, 0 },  /* Pf */
                    284:        { NULL, 0 },  /* Po */
                    285:        { NULL, 0 },  /* Pq */
                    286:        { NULL, 0 },  /* Qc */
                    287:        { NULL, 0 },  /* Ql */
                    288:        { NULL, 0 },  /* Qo */
                    289:        { NULL, 0 },  /* Qq */
                    290:        { NULL, 0 },  /* Re */
                    291:        { NULL, 0 },  /* Rs */
                    292:        { NULL, 0 },  /* Sc */
                    293:        { NULL, 0 },  /* So */
                    294:        { NULL, 0 },  /* Sq */
                    295:        { NULL, 0 },  /* Sm */
                    296:        { NULL, 0 },  /* Sx */
                    297:        { NULL, TYPE_Sy },  /* Sy */
                    298:        { NULL, TYPE_Tn },  /* Tn */
                    299:        { NULL, 0 },  /* Ux */
                    300:        { NULL, 0 },  /* Xc */
                    301:        { NULL, 0 },  /* Xo */
1.173     schwarze  302:        { parse_mdoc_Fo, 0 },  /* Fo */
1.68      schwarze  303:        { NULL, 0 },  /* Fc */
                    304:        { NULL, 0 },  /* Oo */
                    305:        { NULL, 0 },  /* Oc */
                    306:        { NULL, 0 },  /* Bk */
                    307:        { NULL, 0 },  /* Ek */
                    308:        { NULL, 0 },  /* Bt */
                    309:        { NULL, 0 },  /* Hf */
                    310:        { NULL, 0 },  /* Fr */
                    311:        { NULL, 0 },  /* Ud */
                    312:        { NULL, TYPE_Lb },  /* Lb */
                    313:        { NULL, 0 },  /* Lp */
                    314:        { NULL, TYPE_Lk },  /* Lk */
                    315:        { NULL, TYPE_Mt },  /* Mt */
                    316:        { NULL, 0 },  /* Brq */
                    317:        { NULL, 0 },  /* Bro */
                    318:        { NULL, 0 },  /* Brc */
                    319:        { NULL, 0 },  /* %C */
                    320:        { NULL, 0 },  /* Es */
                    321:        { NULL, 0 },  /* En */
                    322:        { NULL, TYPE_Dx },  /* Dx */
                    323:        { NULL, 0 },  /* %Q */
                    324:        { NULL, 0 },  /* br */
                    325:        { NULL, 0 },  /* sp */
                    326:        { NULL, 0 },  /* %U */
                    327:        { NULL, 0 },  /* Ta */
1.166     schwarze  328:        { NULL, 0 },  /* ll */
1.1       kristaps  329: };
                    330:
1.141     schwarze  331:
1.1       kristaps  332: int
1.179     schwarze  333: mandocdb(int argc, char *argv[])
1.1       kristaps  334: {
1.187     schwarze  335:        struct manconf    conf;
                    336:        struct mparse    *mp;
1.206     schwarze  337:        const char       *path_arg, *progname;
1.187     schwarze  338:        size_t            j, sz;
1.59      schwarze  339:        int               ch, i;
1.50      kristaps  340:
1.207     schwarze  341: #if HAVE_PLEDGE
                    342:        if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL) == -1) {
1.214     schwarze  343:                warn("pledge");
1.207     schwarze  344:                return (int)MANDOCLEVEL_SYSERR;
                    345:        }
                    346: #endif
                    347:
1.187     schwarze  348:        memset(&conf, 0, sizeof(conf));
1.50      kristaps  349:        memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *));
                    350:
                    351:        /*
1.141     schwarze  352:         * We accept a few different invocations.
1.50      kristaps  353:         * The CHECKOP macro makes sure that invocation styles don't
                    354:         * clobber each other.
                    355:         */
                    356: #define        CHECKOP(_op, _ch) do \
                    357:        if (OP_DEFAULT != (_op)) { \
1.198     schwarze  358:                warnx("-%c: Conflicting option", (_ch)); \
1.50      kristaps  359:                goto usage; \
                    360:        } while (/*CONSTCOND*/0)
1.10      kristaps  361:
1.59      schwarze  362:        path_arg = NULL;
1.38      schwarze  363:        op = OP_DEFAULT;
1.1       kristaps  364:
1.126     schwarze  365:        while (-1 != (ch = getopt(argc, argv, "aC:Dd:npQT:tu:v")))
1.1       kristaps  366:                switch (ch) {
1.141     schwarze  367:                case 'a':
1.12      schwarze  368:                        use_all = 1;
                    369:                        break;
1.141     schwarze  370:                case 'C':
1.50      kristaps  371:                        CHECKOP(op, ch);
1.59      schwarze  372:                        path_arg = optarg;
1.38      schwarze  373:                        op = OP_CONFFILE;
1.34      schwarze  374:                        break;
1.141     schwarze  375:                case 'D':
1.125     schwarze  376:                        debug++;
                    377:                        break;
1.141     schwarze  378:                case 'd':
1.50      kristaps  379:                        CHECKOP(op, ch);
1.59      schwarze  380:                        path_arg = optarg;
1.5       kristaps  381:                        op = OP_UPDATE;
                    382:                        break;
1.141     schwarze  383:                case 'n':
1.50      kristaps  384:                        nodb = 1;
                    385:                        break;
1.141     schwarze  386:                case 'p':
1.126     schwarze  387:                        warnings = 1;
                    388:                        break;
1.141     schwarze  389:                case 'Q':
1.116     schwarze  390:                        mparse_options |= MPARSE_QUICK;
1.102     schwarze  391:                        break;
1.141     schwarze  392:                case 'T':
1.95      schwarze  393:                        if (strcmp(optarg, "utf8")) {
1.198     schwarze  394:                                warnx("-T%s: Unsupported output format",
                    395:                                    optarg);
1.95      schwarze  396:                                goto usage;
                    397:                        }
                    398:                        write_utf8 = 1;
                    399:                        break;
1.141     schwarze  400:                case 't':
1.50      kristaps  401:                        CHECKOP(op, ch);
1.38      schwarze  402:                        dup2(STDOUT_FILENO, STDERR_FILENO);
                    403:                        op = OP_TEST;
1.50      kristaps  404:                        nodb = warnings = 1;
1.38      schwarze  405:                        break;
1.141     schwarze  406:                case 'u':
1.50      kristaps  407:                        CHECKOP(op, ch);
1.59      schwarze  408:                        path_arg = optarg;
1.5       kristaps  409:                        op = OP_DELETE;
                    410:                        break;
1.141     schwarze  411:                case 'v':
1.126     schwarze  412:                        /* Compatibility with espie@'s makewhatis. */
1.38      schwarze  413:                        break;
1.1       kristaps  414:                default:
1.38      schwarze  415:                        goto usage;
1.1       kristaps  416:                }
                    417:
                    418:        argc -= optind;
                    419:        argv += optind;
                    420:
1.207     schwarze  421: #if HAVE_PLEDGE
1.210     schwarze  422:        if (nodb) {
                    423:                if (pledge("stdio rpath", NULL) == -1) {
1.214     schwarze  424:                        warn("pledge");
1.210     schwarze  425:                        return (int)MANDOCLEVEL_SYSERR;
                    426:                }
1.207     schwarze  427:        }
                    428: #endif
                    429:
1.38      schwarze  430:        if (OP_CONFFILE == op && argc > 0) {
1.198     schwarze  431:                warnx("-C: Too many arguments");
1.38      schwarze  432:                goto usage;
                    433:        }
                    434:
1.59      schwarze  435:        exitcode = (int)MANDOCLEVEL_OK;
1.203     schwarze  436:        mchars_alloc();
                    437:        mp = mparse_alloc(mparse_options, MANDOCLEVEL_BADARG, NULL, NULL);
1.202     schwarze  438:        mandoc_ohash_init(&mpages, 6, offsetof(struct mpage, inodev));
                    439:        mandoc_ohash_init(&mlinks, 6, offsetof(struct mlink, file));
1.50      kristaps  440:
                    441:        if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
1.59      schwarze  442:
1.50      kristaps  443:                /*
1.150     schwarze  444:                 * Most of these deal with a specific directory.
1.140     schwarze  445:                 * Jump into that directory first.
1.50      kristaps  446:                 */
1.165     schwarze  447:                if (OP_TEST != op && 0 == set_basedir(path_arg, 1))
1.50      kristaps  448:                        goto out;
1.140     schwarze  449:
1.138     schwarze  450:                if (dbopen(1)) {
1.140     schwarze  451:                        /*
                    452:                         * The existing database is usable.  Process
                    453:                         * all files specified on the command-line.
                    454:                         */
1.207     schwarze  455: #if HAVE_PLEDGE
1.210     schwarze  456:                        if (!nodb) {
                    457:                                if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1) {
1.214     schwarze  458:                                        warn("pledge");
1.210     schwarze  459:                                        exitcode = (int)MANDOCLEVEL_SYSERR;
                    460:                                        goto out;
                    461:                                }
1.207     schwarze  462:                        }
                    463: #endif
1.140     schwarze  464:                        use_all = 1;
1.138     schwarze  465:                        for (i = 0; i < argc; i++)
                    466:                                filescan(argv[i]);
                    467:                        if (OP_TEST != op)
                    468:                                dbprune();
                    469:                } else {
                    470:                        /*
                    471:                         * Database missing or corrupt.
                    472:                         * Recreate from scratch.
                    473:                         */
1.140     schwarze  474:                        exitcode = (int)MANDOCLEVEL_OK;
1.138     schwarze  475:                        op = OP_DEFAULT;
                    476:                        if (0 == treescan())
                    477:                                goto out;
                    478:                        if (0 == dbopen(0))
                    479:                                goto out;
                    480:                }
1.50      kristaps  481:                if (OP_DELETE != op)
1.176     schwarze  482:                        mpages_merge(mp);
1.138     schwarze  483:                dbclose(OP_DEFAULT == op ? 0 : 1);
1.50      kristaps  484:        } else {
                    485:                /*
                    486:                 * If we have arguments, use them as our manpaths.
                    487:                 * If we don't, grok from manpath(1) or however else
1.187     schwarze  488:                 * manconf_parse() wants to do it.
1.50      kristaps  489:                 */
                    490:                if (argc > 0) {
1.187     schwarze  491:                        conf.manpath.paths = mandoc_reallocarray(NULL,
1.144     schwarze  492:                            argc, sizeof(char *));
1.187     schwarze  493:                        conf.manpath.sz = (size_t)argc;
1.50      kristaps  494:                        for (i = 0; i < argc; i++)
1.187     schwarze  495:                                conf.manpath.paths[i] = mandoc_strdup(argv[i]);
1.50      kristaps  496:                } else
1.187     schwarze  497:                        manconf_parse(&conf, path_arg, NULL, NULL);
1.127     schwarze  498:
1.187     schwarze  499:                if (conf.manpath.sz == 0) {
1.127     schwarze  500:                        exitcode = (int)MANDOCLEVEL_BADARG;
                    501:                        say("", "Empty manpath");
                    502:                }
1.50      kristaps  503:
                    504:                /*
1.69      schwarze  505:                 * First scan the tree rooted at a base directory, then
                    506:                 * build a new database and finally move it into place.
1.50      kristaps  507:                 * Ignore zero-length directories and strip trailing
                    508:                 * slashes.
                    509:                 */
1.187     schwarze  510:                for (j = 0; j < conf.manpath.sz; j++) {
                    511:                        sz = strlen(conf.manpath.paths[j]);
                    512:                        if (sz && conf.manpath.paths[j][sz - 1] == '/')
                    513:                                conf.manpath.paths[j][--sz] = '\0';
1.50      kristaps  514:                        if (0 == sz)
                    515:                                continue;
1.66      schwarze  516:
                    517:                        if (j) {
1.202     schwarze  518:                                mandoc_ohash_init(&mpages, 6,
                    519:                                    offsetof(struct mpage, inodev));
                    520:                                mandoc_ohash_init(&mlinks, 6,
                    521:                                    offsetof(struct mlink, file));
1.66      schwarze  522:                        }
                    523:
1.187     schwarze  524:                        if ( ! set_basedir(conf.manpath.paths[j], argc > 0))
1.165     schwarze  525:                                continue;
1.59      schwarze  526:                        if (0 == treescan())
1.165     schwarze  527:                                continue;
1.59      schwarze  528:                        if (0 == dbopen(0))
1.165     schwarze  529:                                continue;
1.52      kristaps  530:
1.176     schwarze  531:                        mpages_merge(mp);
1.147     schwarze  532:                        if (warnings && !nodb &&
1.130     schwarze  533:                            ! (MPARSE_QUICK & mparse_options))
                    534:                                names_check();
1.59      schwarze  535:                        dbclose(0);
1.66      schwarze  536:
1.187     schwarze  537:                        if (j + 1 < conf.manpath.sz) {
1.80      schwarze  538:                                mpages_free();
1.78      schwarze  539:                                ohash_delete(&mpages);
1.83      schwarze  540:                                ohash_delete(&mlinks);
1.66      schwarze  541:                        }
1.50      kristaps  542:                }
                    543:        }
                    544: out:
1.187     schwarze  545:        manconf_free(&conf);
1.168     schwarze  546:        mparse_free(mp);
1.203     schwarze  547:        mchars_free();
1.80      schwarze  548:        mpages_free();
1.78      schwarze  549:        ohash_delete(&mpages);
1.83      schwarze  550:        ohash_delete(&mlinks);
1.197     schwarze  551:        return exitcode;
1.50      kristaps  552: usage:
1.206     schwarze  553:        progname = getprogname();
1.126     schwarze  554:        fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
                    555:                        "       %s [-aDnpQ] [-Tutf8] dir ...\n"
                    556:                        "       %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
                    557:                        "       %s [-Dnp] -u dir [file ...]\n"
1.102     schwarze  558:                        "       %s [-Q] -t file ...\n",
1.206     schwarze  559:                        progname, progname, progname, progname, progname);
1.50      kristaps  560:
1.197     schwarze  561:        return (int)MANDOCLEVEL_BADARG;
1.50      kristaps  562: }
                    563:
                    564: /*
1.59      schwarze  565:  * Scan a directory tree rooted at "basedir" for manpages.
1.50      kristaps  566:  * We use fts(), scanning directory parts along the way for clues to our
                    567:  * section and architecture.
                    568:  *
                    569:  * If use_all has been specified, grok all files.
                    570:  * If not, sanitise paths to the following:
                    571:  *
1.141     schwarze  572:  *   [./]man*[/<arch>]/<name>.<section>
1.50      kristaps  573:  *   or
                    574:  *   [./]cat<section>[/<arch>]/<name>.0
                    575:  *
1.216     schwarze  576:  * TODO: accommodate for multi-language directories.
1.50      kristaps  577:  */
                    578: static int
1.59      schwarze  579: treescan(void)
1.50      kristaps  580: {
1.139     schwarze  581:        char             buf[PATH_MAX];
1.50      kristaps  582:        FTS             *f;
                    583:        FTSENT          *ff;
1.84      schwarze  584:        struct mlink    *mlink;
1.124     schwarze  585:        int              dform, gzip;
1.94      schwarze  586:        char            *dsec, *arch, *fsec, *cp;
                    587:        const char      *path;
1.50      kristaps  588:        const char      *argv[2];
                    589:
                    590:        argv[0] = ".";
                    591:        argv[1] = (char *)NULL;
                    592:
1.139     schwarze  593:        f = fts_open((char * const *)argv,
                    594:            FTS_PHYSICAL | FTS_NOCHDIR, NULL);
1.200     schwarze  595:        if (f == NULL) {
1.59      schwarze  596:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.123     schwarze  597:                say("", "&fts_open");
1.197     schwarze  598:                return 0;
1.50      kristaps  599:        }
                    600:
                    601:        dsec = arch = NULL;
                    602:        dform = FORM_NONE;
                    603:
1.200     schwarze  604:        while ((ff = fts_read(f)) != NULL) {
1.50      kristaps  605:                path = ff->fts_path + 2;
1.139     schwarze  606:                switch (ff->fts_info) {
                    607:
                    608:                /*
                    609:                 * Symbolic links require various sanity checks,
                    610:                 * then get handled just like regular files.
                    611:                 */
1.141     schwarze  612:                case FTS_SL:
1.200     schwarze  613:                        if (realpath(path, buf) == NULL) {
1.139     schwarze  614:                                if (warnings)
                    615:                                        say(path, "&realpath");
                    616:                                continue;
                    617:                        }
1.184     schwarze  618:                        if (strstr(buf, basedir) != buf
                    619: #ifdef HOMEBREWDIR
                    620:                            && strstr(buf, HOMEBREWDIR) != buf
                    621: #endif
                    622:                        ) {
1.139     schwarze  623:                                if (warnings) say("",
                    624:                                    "%s: outside base directory", buf);
                    625:                                continue;
                    626:                        }
                    627:                        /* Use logical inode to avoid mpages dupe. */
1.200     schwarze  628:                        if (stat(path, ff->fts_statp) == -1) {
1.139     schwarze  629:                                if (warnings)
                    630:                                        say(path, "&stat");
                    631:                                continue;
                    632:                        }
                    633:                        /* FALLTHROUGH */
                    634:
1.50      kristaps  635:                /*
1.83      schwarze  636:                 * If we're a regular file, add an mlink by using the
1.50      kristaps  637:                 * stored directory data and handling the filename.
                    638:                 */
1.141     schwarze  639:                case FTS_F:
1.200     schwarze  640:                        if ( ! strcmp(path, MANDOC_DB))
1.60      schwarze  641:                                continue;
1.50      kristaps  642:                        if ( ! use_all && ff->fts_level < 2) {
1.56      schwarze  643:                                if (warnings)
1.59      schwarze  644:                                        say(path, "Extraneous file");
1.50      kristaps  645:                                continue;
1.124     schwarze  646:                        }
                    647:                        gzip = 0;
                    648:                        fsec = NULL;
1.200     schwarze  649:                        while (fsec == NULL) {
1.124     schwarze  650:                                fsec = strrchr(ff->fts_name, '.');
1.200     schwarze  651:                                if (fsec == NULL || strcmp(fsec+1, "gz"))
1.124     schwarze  652:                                        break;
                    653:                                gzip = 1;
                    654:                                *fsec = '\0';
                    655:                                fsec = NULL;
                    656:                        }
1.200     schwarze  657:                        if (fsec == NULL) {
1.60      schwarze  658:                                if ( ! use_all) {
1.56      schwarze  659:                                        if (warnings)
1.60      schwarze  660:                                                say(path,
                    661:                                                    "No filename suffix");
1.50      kristaps  662:                                        continue;
                    663:                                }
1.200     schwarze  664:                        } else if ( ! strcmp(++fsec, "html")) {
1.60      schwarze  665:                                if (warnings)
                    666:                                        say(path, "Skip html");
                    667:                                continue;
1.200     schwarze  668:                        } else if ( ! strcmp(fsec, "ps")) {
1.60      schwarze  669:                                if (warnings)
                    670:                                        say(path, "Skip ps");
                    671:                                continue;
1.200     schwarze  672:                        } else if ( ! strcmp(fsec, "pdf")) {
1.60      schwarze  673:                                if (warnings)
                    674:                                        say(path, "Skip pdf");
                    675:                                continue;
                    676:                        } else if ( ! use_all &&
1.200     schwarze  677:                            ((dform == FORM_SRC &&
1.185     schwarze  678:                              strncmp(fsec, dsec, strlen(dsec))) ||
1.200     schwarze  679:                             (dform == FORM_CAT && strcmp(fsec, "0")))) {
1.60      schwarze  680:                                if (warnings)
                    681:                                        say(path, "Wrong filename suffix");
                    682:                                continue;
1.66      schwarze  683:                        } else
1.84      schwarze  684:                                fsec[-1] = '\0';
1.94      schwarze  685:
1.84      schwarze  686:                        mlink = mandoc_calloc(1, sizeof(struct mlink));
1.143     schwarze  687:                        if (strlcpy(mlink->file, path,
                    688:                            sizeof(mlink->file)) >=
                    689:                            sizeof(mlink->file)) {
                    690:                                say(path, "Filename too long");
                    691:                                free(mlink);
                    692:                                continue;
                    693:                        }
1.84      schwarze  694:                        mlink->dform = dform;
1.94      schwarze  695:                        mlink->dsec = dsec;
                    696:                        mlink->arch = arch;
                    697:                        mlink->name = ff->fts_name;
                    698:                        mlink->fsec = fsec;
1.124     schwarze  699:                        mlink->gzip = gzip;
1.84      schwarze  700:                        mlink_add(mlink, ff->fts_statp);
1.50      kristaps  701:                        continue;
1.139     schwarze  702:
1.141     schwarze  703:                case FTS_D:
                    704:                case FTS_DP:
1.139     schwarze  705:                        break;
                    706:
                    707:                default:
1.60      schwarze  708:                        if (warnings)
                    709:                                say(path, "Not a regular file");
1.50      kristaps  710:                        continue;
1.60      schwarze  711:                }
1.1       kristaps  712:
1.50      kristaps  713:                switch (ff->fts_level) {
1.141     schwarze  714:                case 0:
1.50      kristaps  715:                        /* Ignore the root directory. */
                    716:                        break;
1.141     schwarze  717:                case 1:
1.50      kristaps  718:                        /*
                    719:                         * This might contain manX/ or catX/.
                    720:                         * Try to infer this from the name.
                    721:                         * If we're not in use_all, enforce it.
                    722:                         */
                    723:                        cp = ff->fts_name;
1.200     schwarze  724:                        if (ff->fts_info == FTS_DP) {
                    725:                                dform = FORM_NONE;
                    726:                                dsec = NULL;
1.50      kristaps  727:                                break;
1.200     schwarze  728:                        }
1.1       kristaps  729:
1.200     schwarze  730:                        if ( ! strncmp(cp, "man", 3)) {
1.50      kristaps  731:                                dform = FORM_SRC;
1.66      schwarze  732:                                dsec = cp + 3;
1.200     schwarze  733:                        } else if ( ! strncmp(cp, "cat", 3)) {
1.50      kristaps  734:                                dform = FORM_CAT;
1.66      schwarze  735:                                dsec = cp + 3;
1.94      schwarze  736:                        } else {
                    737:                                dform = FORM_NONE;
                    738:                                dsec = NULL;
1.50      kristaps  739:                        }
1.1       kristaps  740:
1.200     schwarze  741:                        if (dsec != NULL || use_all)
1.50      kristaps  742:                                break;
1.1       kristaps  743:
1.56      schwarze  744:                        if (warnings)
1.59      schwarze  745:                                say(path, "Unknown directory part");
1.50      kristaps  746:                        fts_set(f, ff, FTS_SKIP);
                    747:                        break;
1.141     schwarze  748:                case 2:
1.50      kristaps  749:                        /*
                    750:                         * Possibly our architecture.
                    751:                         * If we're descending, keep tabs on it.
                    752:                         */
1.200     schwarze  753:                        if (ff->fts_info != FTS_DP && dsec != NULL)
1.66      schwarze  754:                                arch = ff->fts_name;
1.94      schwarze  755:                        else
                    756:                                arch = NULL;
1.50      kristaps  757:                        break;
                    758:                default:
1.200     schwarze  759:                        if (ff->fts_info == FTS_DP || use_all)
1.50      kristaps  760:                                break;
1.56      schwarze  761:                        if (warnings)
1.59      schwarze  762:                                say(path, "Extraneous directory part");
1.50      kristaps  763:                        fts_set(f, ff, FTS_SKIP);
                    764:                        break;
1.5       kristaps  765:                }
1.50      kristaps  766:        }
                    767:
                    768:        fts_close(f);
1.197     schwarze  769:        return 1;
1.50      kristaps  770: }
1.5       kristaps  771:
1.50      kristaps  772: /*
1.89      schwarze  773:  * Add a file to the mlinks table.
1.50      kristaps  774:  * Do not verify that it's a "valid" looking manpage (we'll do that
                    775:  * later).
                    776:  *
                    777:  * Try to infer the manual section, architecture, and page name from the
                    778:  * path, assuming it looks like
                    779:  *
1.141     schwarze  780:  *   [./]man*[/<arch>]/<name>.<section>
1.50      kristaps  781:  *   or
                    782:  *   [./]cat<section>[/<arch>]/<name>.0
                    783:  *
                    784:  * See treescan() for the fts(3) version of this.
                    785:  */
                    786: static void
1.59      schwarze  787: filescan(const char *file)
1.50      kristaps  788: {
1.59      schwarze  789:        char             buf[PATH_MAX];
1.84      schwarze  790:        struct stat      st;
                    791:        struct mlink    *mlink;
1.59      schwarze  792:        char            *p, *start;
1.5       kristaps  793:
1.50      kristaps  794:        assert(use_all);
1.5       kristaps  795:
1.50      kristaps  796:        if (0 == strncmp(file, "./", 2))
                    797:                file += 2;
1.5       kristaps  798:
1.139     schwarze  799:        /*
                    800:         * We have to do lstat(2) before realpath(3) loses
                    801:         * the information whether this is a symbolic link.
                    802:         * We need to know that because for symbolic links,
                    803:         * we want to use the orginal file name, while for
                    804:         * regular files, we want to use the real path.
                    805:         */
                    806:        if (-1 == lstat(file, &st)) {
                    807:                exitcode = (int)MANDOCLEVEL_BADARG;
                    808:                say(file, "&lstat");
                    809:                return;
                    810:        } else if (0 == ((S_IFREG | S_IFLNK) & st.st_mode)) {
                    811:                exitcode = (int)MANDOCLEVEL_BADARG;
                    812:                say(file, "Not a regular file");
                    813:                return;
                    814:        }
                    815:
                    816:        /*
                    817:         * We have to resolve the file name to the real path
                    818:         * in any case for the base directory check.
                    819:         */
1.59      schwarze  820:        if (NULL == realpath(file, buf)) {
                    821:                exitcode = (int)MANDOCLEVEL_BADARG;
1.123     schwarze  822:                say(file, "&realpath");
1.59      schwarze  823:                return;
1.106     schwarze  824:        }
                    825:
1.150     schwarze  826:        if (OP_TEST == op)
1.106     schwarze  827:                start = buf;
1.150     schwarze  828:        else if (strstr(buf, basedir) == buf)
                    829:                start = buf + strlen(basedir);
1.184     schwarze  830: #ifdef HOMEBREWDIR
                    831:        else if (strstr(buf, HOMEBREWDIR) == buf)
                    832:                start = buf;
                    833: #endif
1.106     schwarze  834:        else {
1.59      schwarze  835:                exitcode = (int)MANDOCLEVEL_BADARG;
                    836:                say("", "%s: outside base directory", buf);
                    837:                return;
1.106     schwarze  838:        }
                    839:
1.139     schwarze  840:        /*
                    841:         * Now we are sure the file is inside our tree.
                    842:         * If it is a symbolic link, ignore the real path
                    843:         * and use the original name.
                    844:         * This implies passing stuff like "cat1/../man1/foo.1"
                    845:         * on the command line won't work.  So don't do that.
                    846:         * Note the stat(2) can still fail if the link target
                    847:         * doesn't exist.
                    848:         */
                    849:        if (S_IFLNK & st.st_mode) {
                    850:                if (-1 == stat(buf, &st)) {
                    851:                        exitcode = (int)MANDOCLEVEL_BADARG;
                    852:                        say(file, "&stat");
                    853:                        return;
                    854:                }
1.143     schwarze  855:                if (strlcpy(buf, file, sizeof(buf)) >= sizeof(buf)) {
                    856:                        say(file, "Filename too long");
                    857:                        return;
                    858:                }
1.150     schwarze  859:                start = buf;
                    860:                if (OP_TEST != op && strstr(buf, basedir) == buf)
                    861:                        start += strlen(basedir);
1.50      kristaps  862:        }
1.106     schwarze  863:
1.84      schwarze  864:        mlink = mandoc_calloc(1, sizeof(struct mlink));
1.161     schwarze  865:        mlink->dform = FORM_NONE;
1.143     schwarze  866:        if (strlcpy(mlink->file, start, sizeof(mlink->file)) >=
                    867:            sizeof(mlink->file)) {
                    868:                say(start, "Filename too long");
1.180     schwarze  869:                free(mlink);
1.143     schwarze  870:                return;
                    871:        }
1.17      schwarze  872:
1.50      kristaps  873:        /*
                    874:         * First try to guess our directory structure.
                    875:         * If we find a separator, try to look for man* or cat*.
                    876:         * If we find one of these and what's underneath is a directory,
                    877:         * assume it's an architecture.
                    878:         */
                    879:        if (NULL != (p = strchr(start, '/'))) {
                    880:                *p++ = '\0';
                    881:                if (0 == strncmp(start, "man", 3)) {
1.84      schwarze  882:                        mlink->dform = FORM_SRC;
1.94      schwarze  883:                        mlink->dsec = start + 3;
1.50      kristaps  884:                } else if (0 == strncmp(start, "cat", 3)) {
1.84      schwarze  885:                        mlink->dform = FORM_CAT;
1.94      schwarze  886:                        mlink->dsec = start + 3;
1.17      schwarze  887:                }
1.5       kristaps  888:
1.50      kristaps  889:                start = p;
1.84      schwarze  890:                if (NULL != mlink->dsec && NULL != (p = strchr(start, '/'))) {
1.50      kristaps  891:                        *p++ = '\0';
1.94      schwarze  892:                        mlink->arch = start;
1.50      kristaps  893:                        start = p;
1.84      schwarze  894:                }
1.50      kristaps  895:        }
                    896:
                    897:        /*
                    898:         * Now check the file suffix.
                    899:         * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
                    900:         */
                    901:        p = strrchr(start, '\0');
                    902:        while (p-- > start && '/' != *p && '.' != *p)
                    903:                /* Loop. */ ;
                    904:
                    905:        if ('.' == *p) {
                    906:                *p++ = '\0';
1.94      schwarze  907:                mlink->fsec = p;
1.5       kristaps  908:        }
                    909:
1.10      kristaps  910:        /*
1.50      kristaps  911:         * Now try to parse the name.
                    912:         * Use the filename portion of the path.
1.10      kristaps  913:         */
1.84      schwarze  914:        mlink->name = start;
1.50      kristaps  915:        if (NULL != (p = strrchr(start, '/'))) {
1.84      schwarze  916:                mlink->name = p + 1;
1.50      kristaps  917:                *p = '\0';
1.84      schwarze  918:        }
                    919:        mlink_add(mlink, &st);
1.50      kristaps  920: }
1.5       kristaps  921:
1.50      kristaps  922: static void
1.84      schwarze  923: mlink_add(struct mlink *mlink, const struct stat *st)
1.50      kristaps  924: {
1.83      schwarze  925:        struct inodev    inodev;
1.78      schwarze  926:        struct mpage    *mpage;
1.83      schwarze  927:        unsigned int     slot;
1.50      kristaps  928:
1.84      schwarze  929:        assert(NULL != mlink->file);
1.50      kristaps  930:
1.94      schwarze  931:        mlink->dsec = mandoc_strdup(mlink->dsec ? mlink->dsec : "");
                    932:        mlink->arch = mandoc_strdup(mlink->arch ? mlink->arch : "");
                    933:        mlink->name = mandoc_strdup(mlink->name ? mlink->name : "");
                    934:        mlink->fsec = mandoc_strdup(mlink->fsec ? mlink->fsec : "");
1.84      schwarze  935:
                    936:        if ('0' == *mlink->fsec) {
                    937:                free(mlink->fsec);
                    938:                mlink->fsec = mandoc_strdup(mlink->dsec);
                    939:                mlink->fform = FORM_CAT;
                    940:        } else if ('1' <= *mlink->fsec && '9' >= *mlink->fsec)
                    941:                mlink->fform = FORM_SRC;
1.74      schwarze  942:        else
1.84      schwarze  943:                mlink->fform = FORM_NONE;
1.82      schwarze  944:
1.83      schwarze  945:        slot = ohash_qlookup(&mlinks, mlink->file);
                    946:        assert(NULL == ohash_find(&mlinks, slot));
                    947:        ohash_insert(&mlinks, slot, mlink);
                    948:
1.186     schwarze  949:        memset(&inodev, 0, sizeof(inodev));  /* Clear padding. */
1.83      schwarze  950:        inodev.st_ino = st->st_ino;
                    951:        inodev.st_dev = st->st_dev;
                    952:        slot = ohash_lookup_memory(&mpages, (char *)&inodev,
                    953:            sizeof(struct inodev), inodev.st_ino);
                    954:        mpage = ohash_find(&mpages, slot);
                    955:        if (NULL == mpage) {
                    956:                mpage = mandoc_calloc(1, sizeof(struct mpage));
                    957:                mpage->inodev.st_ino = inodev.st_ino;
                    958:                mpage->inodev.st_dev = inodev.st_dev;
                    959:                ohash_insert(&mpages, slot, mpage);
                    960:        } else
1.85      schwarze  961:                mlink->next = mpage->mlinks;
1.82      schwarze  962:        mpage->mlinks = mlink;
1.118     schwarze  963:        mlink->mpage = mpage;
1.82      schwarze  964: }
                    965:
                    966: static void
                    967: mlink_free(struct mlink *mlink)
                    968: {
                    969:
                    970:        free(mlink->dsec);
                    971:        free(mlink->arch);
                    972:        free(mlink->name);
                    973:        free(mlink->fsec);
                    974:        free(mlink);
1.50      kristaps  975: }
1.3       kristaps  976:
1.50      kristaps  977: static void
1.78      schwarze  978: mpages_free(void)
1.50      kristaps  979: {
1.78      schwarze  980:        struct mpage    *mpage;
1.82      schwarze  981:        struct mlink    *mlink;
1.80      schwarze  982:        unsigned int     slot;
1.3       kristaps  983:
1.80      schwarze  984:        mpage = ohash_first(&mpages, &slot);
                    985:        while (NULL != mpage) {
1.82      schwarze  986:                while (NULL != (mlink = mpage->mlinks)) {
1.85      schwarze  987:                        mpage->mlinks = mlink->next;
1.82      schwarze  988:                        mlink_free(mlink);
                    989:                }
1.78      schwarze  990:                free(mpage->sec);
                    991:                free(mpage->arch);
1.82      schwarze  992:                free(mpage->title);
                    993:                free(mpage->desc);
1.78      schwarze  994:                free(mpage);
1.80      schwarze  995:                mpage = ohash_next(&mpages, &slot);
1.50      kristaps  996:        }
                    997: }
1.38      schwarze  998:
1.50      kristaps  999: /*
1.89      schwarze 1000:  * For each mlink to the mpage, check whether the path looks like
                   1001:  * it is formatted, and if it does, check whether a source manual
                   1002:  * exists by the same name, ignoring the suffix.
                   1003:  * If both conditions hold, drop the mlink.
                   1004:  */
                   1005: static void
                   1006: mlinks_undupe(struct mpage *mpage)
                   1007: {
                   1008:        char              buf[PATH_MAX];
                   1009:        struct mlink    **prev;
                   1010:        struct mlink     *mlink;
                   1011:        char             *bufp;
                   1012:
                   1013:        mpage->form = FORM_CAT;
1.90      schwarze 1014:        prev = &mpage->mlinks;
                   1015:        while (NULL != (mlink = *prev)) {
1.89      schwarze 1016:                if (FORM_CAT != mlink->dform) {
                   1017:                        mpage->form = FORM_NONE;
1.90      schwarze 1018:                        goto nextlink;
1.89      schwarze 1019:                }
1.143     schwarze 1020:                (void)strlcpy(buf, mlink->file, sizeof(buf));
1.89      schwarze 1021:                bufp = strstr(buf, "cat");
                   1022:                assert(NULL != bufp);
                   1023:                memcpy(bufp, "man", 3);
                   1024:                if (NULL != (bufp = strrchr(buf, '.')))
                   1025:                        *++bufp = '\0';
1.143     schwarze 1026:                (void)strlcat(buf, mlink->dsec, sizeof(buf));
1.89      schwarze 1027:                if (NULL == ohash_find(&mlinks,
1.141     schwarze 1028:                    ohash_qlookup(&mlinks, buf)))
1.90      schwarze 1029:                        goto nextlink;
1.89      schwarze 1030:                if (warnings)
                   1031:                        say(mlink->file, "Man source exists: %s", buf);
                   1032:                if (use_all)
1.90      schwarze 1033:                        goto nextlink;
1.89      schwarze 1034:                *prev = mlink->next;
                   1035:                mlink_free(mlink);
1.90      schwarze 1036:                continue;
                   1037: nextlink:
                   1038:                prev = &(*prev)->next;
1.89      schwarze 1039:        }
                   1040: }
                   1041:
1.131     schwarze 1042: static void
1.93      schwarze 1043: mlink_check(struct mpage *mpage, struct mlink *mlink)
                   1044: {
1.131     schwarze 1045:        struct str      *str;
                   1046:        unsigned int     slot;
1.93      schwarze 1047:
                   1048:        /*
                   1049:         * Check whether the manual section given in a file
                   1050:         * agrees with the directory where the file is located.
                   1051:         * Some manuals have suffixes like (3p) on their
                   1052:         * section number either inside the file or in the
                   1053:         * directory name, some are linked into more than one
                   1054:         * section, like encrypt(1) = makekey(8).
                   1055:         */
                   1056:
                   1057:        if (FORM_SRC == mpage->form &&
1.131     schwarze 1058:            strcasecmp(mpage->sec, mlink->dsec))
1.93      schwarze 1059:                say(mlink->file, "Section \"%s\" manual in %s directory",
                   1060:                    mpage->sec, mlink->dsec);
                   1061:
                   1062:        /*
                   1063:         * Manual page directories exist for each kernel
                   1064:         * architecture as returned by machine(1).
                   1065:         * However, many manuals only depend on the
                   1066:         * application architecture as returned by arch(1).
                   1067:         * For example, some (2/ARM) manuals are shared
                   1068:         * across the "armish" and "zaurus" kernel
                   1069:         * architectures.
                   1070:         * A few manuals are even shared across completely
                   1071:         * different architectures, for example fdformat(1)
                   1072:         * on amd64, i386, sparc, and sparc64.
                   1073:         */
                   1074:
1.131     schwarze 1075:        if (strcasecmp(mpage->arch, mlink->arch))
1.93      schwarze 1076:                say(mlink->file, "Architecture \"%s\" manual in "
                   1077:                    "\"%s\" directory", mpage->arch, mlink->arch);
                   1078:
1.131     schwarze 1079:        /*
                   1080:         * XXX
1.133     schwarze 1081:         * parse_cat() doesn't set NAME_TITLE yet.
1.131     schwarze 1082:         */
                   1083:
                   1084:        if (FORM_CAT == mpage->form)
                   1085:                return;
                   1086:
                   1087:        /*
                   1088:         * Check whether this mlink
                   1089:         * appears as a name in the NAME section.
                   1090:         */
1.93      schwarze 1091:
1.133     schwarze 1092:        slot = ohash_qlookup(&names, mlink->name);
                   1093:        str = ohash_find(&names, slot);
1.131     schwarze 1094:        assert(NULL != str);
1.133     schwarze 1095:        if ( ! (NAME_TITLE & str->mask))
1.131     schwarze 1096:                say(mlink->file, "Name missing in NAME section");
1.93      schwarze 1097: }
                   1098:
1.89      schwarze 1099: /*
1.80      schwarze 1100:  * Run through the files in the global vector "mpages"
                   1101:  * and add them to the database specified in "basedir".
1.50      kristaps 1102:  *
                   1103:  * This handles the parsing scheme itself, using the cues of directory
                   1104:  * and filename to determine whether the file is parsable or not.
                   1105:  */
1.59      schwarze 1106: static void
1.176     schwarze 1107: mpages_merge(struct mparse *mp)
1.50      kristaps 1108: {
1.113     schwarze 1109:        char                     any[] = "any";
1.118     schwarze 1110:        struct mpage            *mpage, *mpage_dest;
                   1111:        struct mlink            *mlink, *mlink_dest;
1.191     schwarze 1112:        struct roff_man         *man;
1.118     schwarze 1113:        char                    *sodest;
1.112     schwarze 1114:        char                    *cp;
1.162     schwarze 1115:        int                      fd;
1.101     schwarze 1116:        unsigned int             pslot;
1.69      schwarze 1117:
1.171     schwarze 1118:        if ( ! nodb)
1.107     schwarze 1119:                SQL_EXEC("BEGIN TRANSACTION");
                   1120:
1.80      schwarze 1121:        mpage = ohash_first(&mpages, &pslot);
1.171     schwarze 1122:        while (mpage != NULL) {
1.89      schwarze 1123:                mlinks_undupe(mpage);
1.182     schwarze 1124:                if ((mlink = mpage->mlinks) == NULL) {
1.89      schwarze 1125:                        mpage = ohash_next(&mpages, &pslot);
                   1126:                        continue;
1.50      kristaps 1127:                }
                   1128:
1.133     schwarze 1129:                name_mask = NAME_MASK;
1.202     schwarze 1130:                mandoc_ohash_init(&names, 4, offsetof(struct str, key));
                   1131:                mandoc_ohash_init(&strings, 6, offsetof(struct str, key));
1.50      kristaps 1132:                mparse_reset(mp);
                   1133:                man = NULL;
1.124     schwarze 1134:                sodest = NULL;
                   1135:
1.213     schwarze 1136:                if ((fd = mparse_open(mp, mlink->file)) == -1) {
1.182     schwarze 1137:                        say(mlink->file, "&open");
1.162     schwarze 1138:                        goto nextpage;
1.124     schwarze 1139:                }
1.14      schwarze 1140:
                   1141:                /*
1.183     schwarze 1142:                 * Interpret the file as mdoc(7) or man(7) source
                   1143:                 * code, unless it is known to be formatted.
1.14      schwarze 1144:                 */
1.182     schwarze 1145:                if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) {
1.183     schwarze 1146:                        mparse_readfd(mp, fd, mlink->file);
1.212     schwarze 1147:                        close(fd);
1.192     schwarze 1148:                        mparse_result(mp, &man, &sodest);
1.76      schwarze 1149:                }
1.14      schwarze 1150:
1.171     schwarze 1151:                if (sodest != NULL) {
1.118     schwarze 1152:                        mlink_dest = ohash_find(&mlinks,
                   1153:                            ohash_qlookup(&mlinks, sodest));
1.171     schwarze 1154:                        if (mlink_dest == NULL) {
                   1155:                                mandoc_asprintf(&cp, "%s.gz", sodest);
                   1156:                                mlink_dest = ohash_find(&mlinks,
                   1157:                                    ohash_qlookup(&mlinks, cp));
                   1158:                                free(cp);
                   1159:                        }
                   1160:                        if (mlink_dest != NULL) {
1.118     schwarze 1161:
                   1162:                                /* The .so target exists. */
                   1163:
                   1164:                                mpage_dest = mlink_dest->mpage;
                   1165:                                while (1) {
                   1166:                                        mlink->mpage = mpage_dest;
                   1167:
                   1168:                                        /*
                   1169:                                         * If the target was already
                   1170:                                         * processed, add the links
                   1171:                                         * to the database now.
                   1172:                                         * Otherwise, this will
                   1173:                                         * happen when we come
                   1174:                                         * to the target.
                   1175:                                         */
                   1176:
1.137     schwarze 1177:                                        if (mpage_dest->pageid)
1.169     schwarze 1178:                                                dbadd_mlink_name(mlink);
1.118     schwarze 1179:
1.171     schwarze 1180:                                        if (mlink->next == NULL)
1.118     schwarze 1181:                                                break;
                   1182:                                        mlink = mlink->next;
                   1183:                                }
                   1184:
                   1185:                                /* Move all links to the target. */
                   1186:
                   1187:                                mlink->next = mlink_dest->next;
                   1188:                                mlink_dest->next = mpage->mlinks;
                   1189:                                mpage->mlinks = NULL;
                   1190:                        }
1.124     schwarze 1191:                        goto nextpage;
1.192     schwarze 1192:                } else if (man != NULL && man->macroset == MACROSET_MDOC) {
1.204     schwarze 1193:                        mdoc_validate(man);
1.82      schwarze 1194:                        mpage->form = FORM_SRC;
1.193     schwarze 1195:                        mpage->sec = man->meta.msec;
1.155     schwarze 1196:                        mpage->sec = mandoc_strdup(
1.171     schwarze 1197:                            mpage->sec == NULL ? "" : mpage->sec);
1.193     schwarze 1198:                        mpage->arch = man->meta.arch;
1.84      schwarze 1199:                        mpage->arch = mandoc_strdup(
1.171     schwarze 1200:                            mpage->arch == NULL ? "" : mpage->arch);
1.193     schwarze 1201:                        mpage->title = mandoc_strdup(man->meta.title);
1.192     schwarze 1202:                } else if (man != NULL && man->macroset == MACROSET_MAN) {
1.205     schwarze 1203:                        man_validate(man);
1.82      schwarze 1204:                        mpage->form = FORM_SRC;
1.193     schwarze 1205:                        mpage->sec = mandoc_strdup(man->meta.msec);
1.182     schwarze 1206:                        mpage->arch = mandoc_strdup(mlink->arch);
1.193     schwarze 1207:                        mpage->title = mandoc_strdup(man->meta.title);
1.76      schwarze 1208:                } else {
1.82      schwarze 1209:                        mpage->form = FORM_CAT;
1.182     schwarze 1210:                        mpage->sec = mandoc_strdup(mlink->dsec);
                   1211:                        mpage->arch = mandoc_strdup(mlink->arch);
                   1212:                        mpage->title = mandoc_strdup(mlink->name);
1.76      schwarze 1213:                }
1.97      schwarze 1214:                putkey(mpage, mpage->sec, TYPE_sec);
1.164     schwarze 1215:                if (*mpage->arch != '\0')
                   1216:                        putkey(mpage, mpage->arch, TYPE_arch);
1.50      kristaps 1217:
1.182     schwarze 1218:                for ( ; mlink != NULL; mlink = mlink->next) {
1.97      schwarze 1219:                        if ('\0' != *mlink->dsec)
                   1220:                                putkey(mpage, mlink->dsec, TYPE_sec);
                   1221:                        if ('\0' != *mlink->fsec)
                   1222:                                putkey(mpage, mlink->fsec, TYPE_sec);
1.98      schwarze 1223:                        putkey(mpage, '\0' == *mlink->arch ?
1.113     schwarze 1224:                            any : mlink->arch, TYPE_arch);
1.133     schwarze 1225:                        putkey(mpage, mlink->name, NAME_FILE);
1.97      schwarze 1226:                }
1.12      schwarze 1227:
1.174     schwarze 1228:                assert(mpage->desc == NULL);
1.192     schwarze 1229:                if (man != NULL && man->macroset == MACROSET_MDOC)
1.193     schwarze 1230:                        parse_mdoc(mpage, &man->meta, man->first);
1.174     schwarze 1231:                else if (man != NULL)
1.193     schwarze 1232:                        parse_man(mpage, &man->meta, man->first);
1.50      kristaps 1233:                else
1.162     schwarze 1234:                        parse_cat(mpage, fd);
1.174     schwarze 1235:                if (mpage->desc == NULL)
1.135     schwarze 1236:                        mpage->desc = mandoc_strdup(mpage->mlinks->name);
1.131     schwarze 1237:
                   1238:                if (warnings && !use_all)
                   1239:                        for (mlink = mpage->mlinks; mlink;
                   1240:                             mlink = mlink->next)
                   1241:                                mlink_check(mpage, mlink);
1.44      kristaps 1242:
1.176     schwarze 1243:                dbadd(mpage);
1.182     schwarze 1244:                mlink = mpage->mlinks;
1.124     schwarze 1245:
                   1246: nextpage:
1.66      schwarze 1247:                ohash_delete(&strings);
1.133     schwarze 1248:                ohash_delete(&names);
1.80      schwarze 1249:                mpage = ohash_next(&mpages, &pslot);
1.50      kristaps 1250:        }
1.107     schwarze 1251:
                   1252:        if (0 == nodb)
                   1253:                SQL_EXEC("END TRANSACTION");
1.130     schwarze 1254: }
                   1255:
                   1256: static void
                   1257: names_check(void)
                   1258: {
                   1259:        sqlite3_stmt    *stmt;
                   1260:        const char      *name, *sec, *arch, *key;
                   1261:
                   1262:        sqlite3_prepare_v2(db,
                   1263:          "SELECT name, sec, arch, key FROM ("
1.133     schwarze 1264:            "SELECT name AS key, pageid FROM names "
1.130     schwarze 1265:            "WHERE bits & ? AND NOT EXISTS ("
                   1266:              "SELECT pageid FROM mlinks "
1.133     schwarze 1267:              "WHERE mlinks.pageid == names.pageid "
                   1268:              "AND mlinks.name == names.name"
1.130     schwarze 1269:            ")"
                   1270:          ") JOIN ("
1.142     schwarze 1271:            "SELECT sec, arch, name, pageid FROM mlinks "
                   1272:            "GROUP BY pageid"
1.130     schwarze 1273:          ") USING (pageid);",
                   1274:          -1, &stmt, NULL);
                   1275:
1.201     schwarze 1276:        if (sqlite3_bind_int64(stmt, 1, NAME_TITLE) != SQLITE_OK)
1.134     schwarze 1277:                say("", "%s", sqlite3_errmsg(db));
1.130     schwarze 1278:
1.201     schwarze 1279:        while (sqlite3_step(stmt) == SQLITE_ROW) {
1.154     schwarze 1280:                name = (const char *)sqlite3_column_text(stmt, 0);
                   1281:                sec  = (const char *)sqlite3_column_text(stmt, 1);
                   1282:                arch = (const char *)sqlite3_column_text(stmt, 2);
                   1283:                key  = (const char *)sqlite3_column_text(stmt, 3);
1.130     schwarze 1284:                say("", "%s(%s%s%s) lacks mlink \"%s\"", name, sec,
                   1285:                    '\0' == *arch ? "" : "/",
                   1286:                    '\0' == *arch ? "" : arch, key);
                   1287:        }
                   1288:        sqlite3_finalize(stmt);
1.50      kristaps 1289: }
1.12      schwarze 1290:
1.50      kristaps 1291: static void
1.124     schwarze 1292: parse_cat(struct mpage *mpage, int fd)
1.50      kristaps 1293: {
                   1294:        FILE            *stream;
                   1295:        char            *line, *p, *title;
1.209     schwarze 1296:        size_t           linesz, plen, titlesz;
                   1297:        ssize_t          len;
                   1298:        int              offs;
1.12      schwarze 1299:
1.124     schwarze 1300:        stream = (-1 == fd) ?
                   1301:            fopen(mpage->mlinks->file, "r") :
                   1302:            fdopen(fd, "r");
                   1303:        if (NULL == stream) {
1.159     schwarze 1304:                if (-1 != fd)
                   1305:                        close(fd);
1.56      schwarze 1306:                if (warnings)
1.123     schwarze 1307:                        say(mpage->mlinks->file, "&fopen");
1.50      kristaps 1308:                return;
                   1309:        }
1.1       kristaps 1310:
1.209     schwarze 1311:        line = NULL;
                   1312:        linesz = 0;
                   1313:
1.50      kristaps 1314:        /* Skip to first blank line. */
1.1       kristaps 1315:
1.209     schwarze 1316:        while (getline(&line, &linesz, stream) != -1)
                   1317:                if (*line == '\n')
1.50      kristaps 1318:                        break;
1.1       kristaps 1319:
1.50      kristaps 1320:        /*
                   1321:         * Assume the first line that is not indented
                   1322:         * is the first section header.  Skip to it.
                   1323:         */
1.1       kristaps 1324:
1.209     schwarze 1325:        while (getline(&line, &linesz, stream) != -1)
                   1326:                if (*line != '\n' && *line != ' ')
1.50      kristaps 1327:                        break;
1.141     schwarze 1328:
1.50      kristaps 1329:        /*
                   1330:         * Read up until the next section into a buffer.
                   1331:         * Strip the leading and trailing newline from each read line,
                   1332:         * appending a trailing space.
                   1333:         * Ignore empty (whitespace-only) lines.
                   1334:         */
1.1       kristaps 1335:
1.50      kristaps 1336:        titlesz = 0;
                   1337:        title = NULL;
1.38      schwarze 1338:
1.209     schwarze 1339:        while ((len = getline(&line, &linesz, stream)) != -1) {
                   1340:                if (*line != ' ')
1.50      kristaps 1341:                        break;
1.209     schwarze 1342:                offs = 0;
                   1343:                while (isspace((unsigned char)line[offs]))
                   1344:                        offs++;
                   1345:                if (line[offs] == '\0')
1.38      schwarze 1346:                        continue;
1.209     schwarze 1347:                title = mandoc_realloc(title, titlesz + len - offs);
                   1348:                memcpy(title + titlesz, line + offs, len - offs);
                   1349:                titlesz += len - offs;
1.50      kristaps 1350:                title[titlesz - 1] = ' ';
                   1351:        }
1.209     schwarze 1352:        free(line);
1.38      schwarze 1353:
1.50      kristaps 1354:        /*
                   1355:         * If no page content can be found, or the input line
                   1356:         * is already the next section header, or there is no
                   1357:         * trailing newline, reuse the page title as the page
                   1358:         * description.
                   1359:         */
1.44      kristaps 1360:
1.50      kristaps 1361:        if (NULL == title || '\0' == *title) {
1.56      schwarze 1362:                if (warnings)
1.82      schwarze 1363:                        say(mpage->mlinks->file,
                   1364:                            "Cannot find NAME section");
1.50      kristaps 1365:                fclose(stream);
                   1366:                free(title);
                   1367:                return;
                   1368:        }
1.1       kristaps 1369:
1.209     schwarze 1370:        title[titlesz - 1] = '\0';
1.33      schwarze 1371:
1.50      kristaps 1372:        /*
                   1373:         * Skip to the first dash.
                   1374:         * Use the remaining line as the description (no more than 70
                   1375:         * bytes).
                   1376:         */
1.33      schwarze 1377:
1.50      kristaps 1378:        if (NULL != (p = strstr(title, "- "))) {
                   1379:                for (p += 2; ' ' == *p || '\b' == *p; p++)
                   1380:                        /* Skip to next word. */ ;
                   1381:        } else {
1.56      schwarze 1382:                if (warnings)
1.82      schwarze 1383:                        say(mpage->mlinks->file,
                   1384:                            "No dash in title line");
1.50      kristaps 1385:                p = title;
                   1386:        }
1.38      schwarze 1387:
1.50      kristaps 1388:        plen = strlen(p);
1.1       kristaps 1389:
1.50      kristaps 1390:        /* Strip backspace-encoding from line. */
1.1       kristaps 1391:
1.50      kristaps 1392:        while (NULL != (line = memchr(p, '\b', plen))) {
                   1393:                len = line - p;
                   1394:                if (0 == len) {
                   1395:                        memmove(line, line + 1, plen--);
                   1396:                        continue;
1.141     schwarze 1397:                }
1.50      kristaps 1398:                memmove(line - 1, line + 1, plen - len);
                   1399:                plen -= 2;
                   1400:        }
1.1       kristaps 1401:
1.78      schwarze 1402:        mpage->desc = mandoc_strdup(p);
1.50      kristaps 1403:        fclose(stream);
                   1404:        free(title);
                   1405: }
1.1       kristaps 1406:
1.50      kristaps 1407: /*
                   1408:  * Put a type/word pair into the word database for this particular file.
                   1409:  */
                   1410: static void
1.112     schwarze 1411: putkey(const struct mpage *mpage, char *value, uint64_t type)
1.50      kristaps 1412: {
1.112     schwarze 1413:        char     *cp;
1.18      kristaps 1414:
1.50      kristaps 1415:        assert(NULL != value);
1.112     schwarze 1416:        if (TYPE_arch == type)
                   1417:                for (cp = value; *cp; cp++)
                   1418:                        if (isupper((unsigned char)*cp))
                   1419:                                *cp = _tolower((unsigned char)*cp);
1.78      schwarze 1420:        putkeys(mpage, value, strlen(value), type);
1.3       kristaps 1421: }
                   1422:
                   1423: /*
1.50      kristaps 1424:  * Grok all nodes at or below a certain mdoc node into putkey().
1.3       kristaps 1425:  */
                   1426: static void
1.78      schwarze 1427: putmdockey(const struct mpage *mpage,
1.189     schwarze 1428:        const struct roff_node *n, uint64_t m)
1.3       kristaps 1429: {
1.18      kristaps 1430:
1.50      kristaps 1431:        for ( ; NULL != n; n = n->next) {
                   1432:                if (NULL != n->child)
1.78      schwarze 1433:                        putmdockey(mpage, n->child, m);
1.188     schwarze 1434:                if (n->type == ROFFT_TEXT)
1.78      schwarze 1435:                        putkey(mpage, n->string, m);
1.50      kristaps 1436:        }
                   1437: }
1.18      kristaps 1438:
1.61      schwarze 1439: static void
1.190     schwarze 1440: parse_man(struct mpage *mpage, const struct roff_meta *meta,
1.189     schwarze 1441:        const struct roff_node *n)
1.50      kristaps 1442: {
1.189     schwarze 1443:        const struct roff_node *head, *body;
1.121     schwarze 1444:        char            *start, *title;
1.50      kristaps 1445:        char             byte;
1.121     schwarze 1446:        size_t           sz;
1.18      kristaps 1447:
1.215     schwarze 1448:        if (n == NULL)
1.61      schwarze 1449:                return;
1.18      kristaps 1450:
1.50      kristaps 1451:        /*
                   1452:         * We're only searching for one thing: the first text child in
                   1453:         * the BODY of a NAME section.  Since we don't keep track of
                   1454:         * sections in -man, run some hoops to find out whether we're in
                   1455:         * the correct section or not.
                   1456:         */
1.18      kristaps 1457:
1.188     schwarze 1458:        if (n->type == ROFFT_BODY && n->tok == MAN_SH) {
1.50      kristaps 1459:                body = n;
1.215     schwarze 1460:                if ((head = body->parent->head) != NULL &&
                   1461:                    (head = head->child) != NULL &&
                   1462:                    head->next == NULL &&
1.188     schwarze 1463:                    head->type == ROFFT_TEXT &&
1.215     schwarze 1464:                    strcmp(head->string, "NAME") == 0 &&
                   1465:                    body->child != NULL) {
1.3       kristaps 1466:
1.50      kristaps 1467:                        /*
                   1468:                         * Suck the entire NAME section into memory.
                   1469:                         * Yes, we might run away.
                   1470:                         * But too many manuals have big, spread-out
                   1471:                         * NAME sections over many lines.
                   1472:                         */
1.3       kristaps 1473:
1.121     schwarze 1474:                        title = NULL;
1.194     schwarze 1475:                        deroff(&title, body);
1.50      kristaps 1476:                        if (NULL == title)
1.61      schwarze 1477:                                return;
1.18      kristaps 1478:
1.141     schwarze 1479:                        /*
1.50      kristaps 1480:                         * Go through a special heuristic dance here.
                   1481:                         * Conventionally, one or more manual names are
                   1482:                         * comma-specified prior to a whitespace, then a
                   1483:                         * dash, then a description.  Try to puzzle out
                   1484:                         * the name parts here.
                   1485:                         */
1.18      kristaps 1486:
1.121     schwarze 1487:                        start = title;
1.50      kristaps 1488:                        for ( ;; ) {
                   1489:                                sz = strcspn(start, " ,");
                   1490:                                if ('\0' == start[sz])
                   1491:                                        break;
1.1       kristaps 1492:
1.50      kristaps 1493:                                byte = start[sz];
                   1494:                                start[sz] = '\0';
1.110     schwarze 1495:
                   1496:                                /*
                   1497:                                 * Assume a stray trailing comma in the
                   1498:                                 * name list if a name begins with a dash.
                   1499:                                 */
                   1500:
                   1501:                                if ('-' == start[0] ||
                   1502:                                    ('\\' == start[0] && '-' == start[1]))
                   1503:                                        break;
1.1       kristaps 1504:
1.133     schwarze 1505:                                putkey(mpage, start, NAME_TITLE);
1.174     schwarze 1506:                                if ( ! (mpage->name_head_done ||
                   1507:                                    strcasecmp(start, meta->title))) {
                   1508:                                        putkey(mpage, start, NAME_HEAD);
                   1509:                                        mpage->name_head_done = 1;
                   1510:                                }
1.1       kristaps 1511:
1.50      kristaps 1512:                                if (' ' == byte) {
                   1513:                                        start += sz + 1;
                   1514:                                        break;
                   1515:                                }
1.1       kristaps 1516:
1.50      kristaps 1517:                                assert(',' == byte);
                   1518:                                start += sz + 1;
                   1519:                                while (' ' == *start)
                   1520:                                        start++;
                   1521:                        }
1.1       kristaps 1522:
1.121     schwarze 1523:                        if (start == title) {
1.133     schwarze 1524:                                putkey(mpage, start, NAME_TITLE);
1.174     schwarze 1525:                                if ( ! (mpage->name_head_done ||
                   1526:                                    strcasecmp(start, meta->title))) {
                   1527:                                        putkey(mpage, start, NAME_HEAD);
                   1528:                                        mpage->name_head_done = 1;
                   1529:                                }
1.50      kristaps 1530:                                free(title);
1.61      schwarze 1531:                                return;
1.50      kristaps 1532:                        }
1.1       kristaps 1533:
1.50      kristaps 1534:                        while (isspace((unsigned char)*start))
                   1535:                                start++;
1.1       kristaps 1536:
1.50      kristaps 1537:                        if (0 == strncmp(start, "-", 1))
                   1538:                                start += 1;
                   1539:                        else if (0 == strncmp(start, "\\-\\-", 4))
                   1540:                                start += 4;
                   1541:                        else if (0 == strncmp(start, "\\-", 2))
                   1542:                                start += 2;
                   1543:                        else if (0 == strncmp(start, "\\(en", 4))
                   1544:                                start += 4;
                   1545:                        else if (0 == strncmp(start, "\\(em", 4))
                   1546:                                start += 4;
1.1       kristaps 1547:
1.50      kristaps 1548:                        while (' ' == *start)
                   1549:                                start++;
1.1       kristaps 1550:
1.78      schwarze 1551:                        mpage->desc = mandoc_strdup(start);
1.50      kristaps 1552:                        free(title);
1.61      schwarze 1553:                        return;
1.50      kristaps 1554:                }
                   1555:        }
1.1       kristaps 1556:
1.77      schwarze 1557:        for (n = n->child; n; n = n->next) {
1.78      schwarze 1558:                if (NULL != mpage->desc)
1.77      schwarze 1559:                        break;
1.174     schwarze 1560:                parse_man(mpage, meta, n);
1.77      schwarze 1561:        }
1.1       kristaps 1562: }
                   1563:
                   1564: static void
1.190     schwarze 1565: parse_mdoc(struct mpage *mpage, const struct roff_meta *meta,
1.189     schwarze 1566:        const struct roff_node *n)
1.1       kristaps 1567: {
                   1568:
1.50      kristaps 1569:        assert(NULL != n);
                   1570:        for (n = n->child; NULL != n; n = n->next) {
                   1571:                switch (n->type) {
1.188     schwarze 1572:                case ROFFT_ELEM:
                   1573:                case ROFFT_BLOCK:
                   1574:                case ROFFT_HEAD:
                   1575:                case ROFFT_BODY:
                   1576:                case ROFFT_TAIL:
1.50      kristaps 1577:                        if (NULL != mdocs[n->tok].fp)
1.172     schwarze 1578:                               if (0 == (*mdocs[n->tok].fp)(mpage, meta, n))
1.50      kristaps 1579:                                       break;
1.68      schwarze 1580:                        if (mdocs[n->tok].mask)
1.78      schwarze 1581:                                putmdockey(mpage, n->child,
                   1582:                                    mdocs[n->tok].mask);
1.50      kristaps 1583:                        break;
                   1584:                default:
1.188     schwarze 1585:                        assert(n->type != ROFFT_ROOT);
1.50      kristaps 1586:                        continue;
                   1587:                }
                   1588:                if (NULL != n->child)
1.172     schwarze 1589:                        parse_mdoc(mpage, meta, n);
1.1       kristaps 1590:        }
                   1591: }
                   1592:
1.25      schwarze 1593: static int
1.190     schwarze 1594: parse_mdoc_Fd(struct mpage *mpage, const struct roff_meta *meta,
1.189     schwarze 1595:        const struct roff_node *n)
1.1       kristaps 1596: {
1.176     schwarze 1597:        char            *start, *end;
1.1       kristaps 1598:        size_t           sz;
1.25      schwarze 1599:
1.50      kristaps 1600:        if (SEC_SYNOPSIS != n->sec ||
1.141     schwarze 1601:            NULL == (n = n->child) ||
1.188     schwarze 1602:            n->type != ROFFT_TEXT)
1.197     schwarze 1603:                return 0;
1.1       kristaps 1604:
                   1605:        /*
                   1606:         * Only consider those `Fd' macro fields that begin with an
                   1607:         * "inclusion" token (versus, e.g., #define).
                   1608:         */
1.50      kristaps 1609:
1.1       kristaps 1610:        if (strcmp("#include", n->string))
1.197     schwarze 1611:                return 0;
1.1       kristaps 1612:
1.188     schwarze 1613:        if ((n = n->next) == NULL || n->type != ROFFT_TEXT)
1.197     schwarze 1614:                return 0;
1.1       kristaps 1615:
                   1616:        /*
                   1617:         * Strip away the enclosing angle brackets and make sure we're
                   1618:         * not zero-length.
                   1619:         */
                   1620:
                   1621:        start = n->string;
                   1622:        if ('<' == *start || '"' == *start)
                   1623:                start++;
                   1624:
                   1625:        if (0 == (sz = strlen(start)))
1.197     schwarze 1626:                return 0;
1.1       kristaps 1627:
                   1628:        end = &start[(int)sz - 1];
                   1629:        if ('>' == *end || '"' == *end)
                   1630:                end--;
                   1631:
1.50      kristaps 1632:        if (end > start)
1.78      schwarze 1633:                putkeys(mpage, start, end - start + 1, TYPE_In);
1.197     schwarze 1634:        return 0;
1.1       kristaps 1635: }
                   1636:
1.178     schwarze 1637: static void
1.189     schwarze 1638: parse_mdoc_fname(struct mpage *mpage, const struct roff_node *n)
1.1       kristaps 1639: {
1.112     schwarze 1640:        char    *cp;
1.178     schwarze 1641:        size_t   sz;
1.1       kristaps 1642:
1.188     schwarze 1643:        if (n->type != ROFFT_TEXT)
1.178     schwarze 1644:                return;
1.25      schwarze 1645:
1.178     schwarze 1646:        /* Skip function pointer punctuation. */
1.1       kristaps 1647:
1.178     schwarze 1648:        cp = n->string;
                   1649:        while (*cp == '(' || *cp == '*')
1.1       kristaps 1650:                cp++;
1.178     schwarze 1651:        sz = strcspn(cp, "()");
1.1       kristaps 1652:
1.178     schwarze 1653:        putkeys(mpage, cp, sz, TYPE_Fn);
1.173     schwarze 1654:        if (n->sec == SEC_SYNOPSIS)
1.178     schwarze 1655:                putkeys(mpage, cp, sz, NAME_SYN);
                   1656: }
1.25      schwarze 1657:
1.178     schwarze 1658: static int
1.190     schwarze 1659: parse_mdoc_Fn(struct mpage *mpage, const struct roff_meta *meta,
1.189     schwarze 1660:        const struct roff_node *n)
1.178     schwarze 1661: {
                   1662:
                   1663:        if (n->child == NULL)
1.197     schwarze 1664:                return 0;
1.178     schwarze 1665:
                   1666:        parse_mdoc_fname(mpage, n->child);
1.25      schwarze 1667:
1.178     schwarze 1668:        for (n = n->child->next; n != NULL; n = n->next)
1.188     schwarze 1669:                if (n->type == ROFFT_TEXT)
1.78      schwarze 1670:                        putkey(mpage, n->string, TYPE_Fa);
1.25      schwarze 1671:
1.197     schwarze 1672:        return 0;
1.173     schwarze 1673: }
                   1674:
                   1675: static int
1.190     schwarze 1676: parse_mdoc_Fo(struct mpage *mpage, const struct roff_meta *meta,
1.189     schwarze 1677:        const struct roff_node *n)
1.173     schwarze 1678: {
1.177     schwarze 1679:
1.188     schwarze 1680:        if (n->type != ROFFT_HEAD)
1.197     schwarze 1681:                return 1;
1.173     schwarze 1682:
1.178     schwarze 1683:        if (n->child != NULL)
                   1684:                parse_mdoc_fname(mpage, n->child);
                   1685:
1.197     schwarze 1686:        return 0;
1.1       kristaps 1687: }
                   1688:
1.25      schwarze 1689: static int
1.211     schwarze 1690: parse_mdoc_Va(struct mpage *mpage, const struct roff_meta *meta,
                   1691:        const struct roff_node *n)
                   1692: {
                   1693:        char *cp;
                   1694:
                   1695:        if (n->type != ROFFT_ELEM && n->type != ROFFT_BODY)
                   1696:                return 0;
                   1697:
1.215     schwarze 1698:        if (n->child != NULL &&
                   1699:            n->child->next == NULL &&
                   1700:            n->child->type == ROFFT_TEXT)
1.211     schwarze 1701:                return 1;
                   1702:
                   1703:        cp = NULL;
                   1704:        deroff(&cp, n);
                   1705:        if (cp != NULL) {
                   1706:                putkey(mpage, cp, TYPE_Vt | (n->tok == MDOC_Va ||
                   1707:                    n->type == ROFFT_BODY ? TYPE_Va : 0));
                   1708:                free(cp);
                   1709:        }
                   1710:
                   1711:        return 0;
                   1712: }
                   1713:
                   1714: static int
1.190     schwarze 1715: parse_mdoc_Xr(struct mpage *mpage, const struct roff_meta *meta,
1.189     schwarze 1716:        const struct roff_node *n)
1.1       kristaps 1717: {
1.67      schwarze 1718:        char    *cp;
1.1       kristaps 1719:
                   1720:        if (NULL == (n = n->child))
1.197     schwarze 1721:                return 0;
1.1       kristaps 1722:
1.67      schwarze 1723:        if (NULL == n->next) {
1.78      schwarze 1724:                putkey(mpage, n->string, TYPE_Xr);
1.197     schwarze 1725:                return 0;
1.67      schwarze 1726:        }
                   1727:
1.120     schwarze 1728:        mandoc_asprintf(&cp, "%s(%s)", n->string, n->next->string);
1.78      schwarze 1729:        putkey(mpage, cp, TYPE_Xr);
1.67      schwarze 1730:        free(cp);
1.197     schwarze 1731:        return 0;
1.1       kristaps 1732: }
                   1733:
1.25      schwarze 1734: static int
1.190     schwarze 1735: parse_mdoc_Nd(struct mpage *mpage, const struct roff_meta *meta,
1.189     schwarze 1736:        const struct roff_node *n)
1.1       kristaps 1737: {
                   1738:
1.188     schwarze 1739:        if (n->type == ROFFT_BODY)
1.194     schwarze 1740:                deroff(&mpage->desc, n);
1.197     schwarze 1741:        return 0;
1.1       kristaps 1742: }
                   1743:
1.25      schwarze 1744: static int
1.190     schwarze 1745: parse_mdoc_Nm(struct mpage *mpage, const struct roff_meta *meta,
1.189     schwarze 1746:        const struct roff_node *n)
1.1       kristaps 1747: {
                   1748:
1.129     schwarze 1749:        if (SEC_NAME == n->sec)
1.133     schwarze 1750:                putmdockey(mpage, n->child, NAME_TITLE);
1.188     schwarze 1751:        else if (n->sec == SEC_SYNOPSIS && n->type == ROFFT_HEAD) {
1.172     schwarze 1752:                if (n->child == NULL)
                   1753:                        putkey(mpage, meta->name, NAME_SYN);
                   1754:                else
                   1755:                        putmdockey(mpage, n->child, NAME_SYN);
1.174     schwarze 1756:        }
                   1757:        if ( ! (mpage->name_head_done ||
                   1758:            n->child == NULL || n->child->string == NULL ||
                   1759:            strcasecmp(n->child->string, meta->title))) {
1.188     schwarze 1760:                putkey(mpage, n->child->string, ROFFT_HEAD);
1.174     schwarze 1761:                mpage->name_head_done = 1;
1.172     schwarze 1762:        }
1.197     schwarze 1763:        return 0;
1.1       kristaps 1764: }
                   1765:
1.25      schwarze 1766: static int
1.190     schwarze 1767: parse_mdoc_Sh(struct mpage *mpage, const struct roff_meta *meta,
1.189     schwarze 1768:        const struct roff_node *n)
1.1       kristaps 1769: {
                   1770:
1.197     schwarze 1771:        return n->sec == SEC_CUSTOM && n->type == ROFFT_HEAD;
1.1       kristaps 1772: }
                   1773:
1.50      kristaps 1774: static int
1.190     schwarze 1775: parse_mdoc_head(struct mpage *mpage, const struct roff_meta *meta,
1.189     schwarze 1776:        const struct roff_node *n)
1.1       kristaps 1777: {
                   1778:
1.197     schwarze 1779:        return n->type == ROFFT_HEAD;
1.1       kristaps 1780: }
                   1781:
1.50      kristaps 1782: /*
1.66      schwarze 1783:  * Add a string to the hash table for the current manual.
                   1784:  * Each string has a bitmask telling which macros it belongs to.
                   1785:  * When we finish the manual, we'll dump the table.
1.50      kristaps 1786:  */
                   1787: static void
1.176     schwarze 1788: putkeys(const struct mpage *mpage, char *cp, size_t sz, uint64_t v)
1.50      kristaps 1789: {
1.133     schwarze 1790:        struct ohash    *htab;
1.50      kristaps 1791:        struct str      *s;
1.111     schwarze 1792:        const char      *end;
1.65      schwarze 1793:        unsigned int     slot;
1.176     schwarze 1794:        int              i, mustfree;
1.25      schwarze 1795:
1.50      kristaps 1796:        if (0 == sz)
                   1797:                return;
1.111     schwarze 1798:
1.176     schwarze 1799:        mustfree = render_string(&cp, &sz);
                   1800:
1.133     schwarze 1801:        if (TYPE_Nm & v) {
                   1802:                htab = &names;
                   1803:                v &= name_mask;
1.169     schwarze 1804:                if (v & NAME_FIRST)
                   1805:                        name_mask &= ~NAME_FIRST;
1.133     schwarze 1806:                if (debug > 1)
                   1807:                        say(mpage->mlinks->file,
1.175     schwarze 1808:                            "Adding name %*s, bits=%d", sz, cp, v);
1.133     schwarze 1809:        } else {
                   1810:                htab = &strings;
                   1811:                if (debug > 1)
                   1812:                    for (i = 0; i < mansearch_keymax; i++)
1.163     schwarze 1813:                        if ((uint64_t)1 << i & v)
1.133     schwarze 1814:                            say(mpage->mlinks->file,
                   1815:                                "Adding key %s=%*s",
                   1816:                                mansearch_keynames[i], sz, cp);
1.111     schwarze 1817:        }
1.25      schwarze 1818:
1.65      schwarze 1819:        end = cp + sz;
1.133     schwarze 1820:        slot = ohash_qlookupi(htab, cp, &end);
                   1821:        s = ohash_find(htab, slot);
1.25      schwarze 1822:
1.78      schwarze 1823:        if (NULL != s && mpage == s->mpage) {
1.50      kristaps 1824:                s->mask |= v;
                   1825:                return;
                   1826:        } else if (NULL == s) {
1.144     schwarze 1827:                s = mandoc_calloc(1, sizeof(struct str) + sz + 1);
1.50      kristaps 1828:                memcpy(s->key, cp, sz);
1.133     schwarze 1829:                ohash_insert(htab, slot, s);
1.1       kristaps 1830:        }
1.78      schwarze 1831:        s->mpage = mpage;
1.50      kristaps 1832:        s->mask = v;
1.176     schwarze 1833:
                   1834:        if (mustfree)
                   1835:                free(cp);
1.1       kristaps 1836: }
                   1837:
1.50      kristaps 1838: /*
                   1839:  * Take a Unicode codepoint and produce its UTF-8 encoding.
                   1840:  * This isn't the best way to do this, but it works.
                   1841:  * The magic numbers are from the UTF-8 packaging.
                   1842:  * They're not as scary as they seem: read the UTF-8 spec for details.
                   1843:  */
                   1844: static size_t
                   1845: utf8(unsigned int cp, char out[7])
1.1       kristaps 1846: {
1.50      kristaps 1847:        size_t           rc;
1.1       kristaps 1848:
1.50      kristaps 1849:        rc = 0;
                   1850:        if (cp <= 0x0000007F) {
                   1851:                rc = 1;
                   1852:                out[0] = (char)cp;
                   1853:        } else if (cp <= 0x000007FF) {
                   1854:                rc = 2;
                   1855:                out[0] = (cp >> 6  & 31) | 192;
                   1856:                out[1] = (cp       & 63) | 128;
                   1857:        } else if (cp <= 0x0000FFFF) {
                   1858:                rc = 3;
                   1859:                out[0] = (cp >> 12 & 15) | 224;
                   1860:                out[1] = (cp >> 6  & 63) | 128;
                   1861:                out[2] = (cp       & 63) | 128;
                   1862:        } else if (cp <= 0x001FFFFF) {
                   1863:                rc = 4;
                   1864:                out[0] = (cp >> 18 &  7) | 240;
                   1865:                out[1] = (cp >> 12 & 63) | 128;
                   1866:                out[2] = (cp >> 6  & 63) | 128;
                   1867:                out[3] = (cp       & 63) | 128;
                   1868:        } else if (cp <= 0x03FFFFFF) {
                   1869:                rc = 5;
                   1870:                out[0] = (cp >> 24 &  3) | 248;
                   1871:                out[1] = (cp >> 18 & 63) | 128;
                   1872:                out[2] = (cp >> 12 & 63) | 128;
                   1873:                out[3] = (cp >> 6  & 63) | 128;
                   1874:                out[4] = (cp       & 63) | 128;
                   1875:        } else if (cp <= 0x7FFFFFFF) {
                   1876:                rc = 6;
                   1877:                out[0] = (cp >> 30 &  1) | 252;
                   1878:                out[1] = (cp >> 24 & 63) | 128;
                   1879:                out[2] = (cp >> 18 & 63) | 128;
                   1880:                out[3] = (cp >> 12 & 63) | 128;
                   1881:                out[4] = (cp >> 6  & 63) | 128;
                   1882:                out[5] = (cp       & 63) | 128;
                   1883:        } else
1.197     schwarze 1884:                return 0;
1.1       kristaps 1885:
1.50      kristaps 1886:        out[rc] = '\0';
1.197     schwarze 1887:        return rc;
1.50      kristaps 1888: }
1.1       kristaps 1889:
1.50      kristaps 1890: /*
1.176     schwarze 1891:  * If the string contains escape sequences,
                   1892:  * replace it with an allocated rendering and return 1,
                   1893:  * such that the caller can free it after use.
                   1894:  * Otherwise, do nothing and return 0.
1.50      kristaps 1895:  */
1.176     schwarze 1896: static int
                   1897: render_string(char **public, size_t *psz)
1.50      kristaps 1898: {
1.176     schwarze 1899:        const char      *src, *scp, *addcp, *seq;
                   1900:        char            *dst;
                   1901:        size_t           ssz, dsz, addsz;
1.114     schwarze 1902:        char             utfbuf[7], res[6];
1.176     schwarze 1903:        int              seqlen, unicode;
1.50      kristaps 1904:
                   1905:        res[0] = '\\';
                   1906:        res[1] = '\t';
                   1907:        res[2] = ASCII_NBRSP;
                   1908:        res[3] = ASCII_HYPH;
1.114     schwarze 1909:        res[4] = ASCII_BREAK;
                   1910:        res[5] = '\0';
1.1       kristaps 1911:
1.176     schwarze 1912:        src = scp = *public;
                   1913:        ssz = *psz;
                   1914:        dst = NULL;
                   1915:        dsz = 0;
                   1916:
                   1917:        while (scp < src + *psz) {
                   1918:
                   1919:                /* Leave normal characters unchanged. */
                   1920:
                   1921:                if (strchr(res, *scp) == NULL) {
                   1922:                        if (dst != NULL)
                   1923:                                dst[dsz++] = *scp;
                   1924:                        scp++;
                   1925:                        continue;
                   1926:                }
1.46      kristaps 1927:
1.50      kristaps 1928:                /*
1.176     schwarze 1929:                 * Found something that requires replacing,
                   1930:                 * make sure we have a destination buffer.
1.50      kristaps 1931:                 */
1.176     schwarze 1932:
                   1933:                if (dst == NULL) {
                   1934:                        dst = mandoc_malloc(ssz + 1);
                   1935:                        dsz = scp - src;
                   1936:                        memcpy(dst, src, dsz);
1.50      kristaps 1937:                }
1.46      kristaps 1938:
1.176     schwarze 1939:                /* Handle single-char special characters. */
                   1940:
                   1941:                switch (*scp) {
                   1942:                case '\\':
                   1943:                        break;
1.141     schwarze 1944:                case '\t':
                   1945:                case ASCII_NBRSP:
1.176     schwarze 1946:                        dst[dsz++] = ' ';
                   1947:                        scp++;
                   1948:                        continue;
                   1949:                case ASCII_HYPH:
                   1950:                        dst[dsz++] = '-';
1.114     schwarze 1951:                        /* FALLTHROUGH */
1.141     schwarze 1952:                case ASCII_BREAK:
1.176     schwarze 1953:                        scp++;
1.50      kristaps 1954:                        continue;
1.114     schwarze 1955:                default:
1.176     schwarze 1956:                        abort();
1.114     schwarze 1957:                }
1.46      kristaps 1958:
1.50      kristaps 1959:                /*
1.176     schwarze 1960:                 * Found an escape sequence.
                   1961:                 * Read past the slash, then parse it.
                   1962:                 * Ignore everything except characters.
1.50      kristaps 1963:                 */
1.95      schwarze 1964:
1.176     schwarze 1965:                scp++;
                   1966:                if (mandoc_escape(&scp, &seq, &seqlen) != ESCAPE_SPECIAL)
1.50      kristaps 1967:                        continue;
1.1       kristaps 1968:
1.50      kristaps 1969:                /*
1.95      schwarze 1970:                 * Render the special character
                   1971:                 * as either UTF-8 or ASCII.
1.50      kristaps 1972:                 */
1.95      schwarze 1973:
                   1974:                if (write_utf8) {
1.203     schwarze 1975:                        unicode = mchars_spec2cp(seq, seqlen);
1.176     schwarze 1976:                        if (unicode <= 0)
1.95      schwarze 1977:                                continue;
1.176     schwarze 1978:                        addsz = utf8(unicode, utfbuf);
                   1979:                        if (addsz == 0)
1.95      schwarze 1980:                                continue;
1.176     schwarze 1981:                        addcp = utfbuf;
1.95      schwarze 1982:                } else {
1.203     schwarze 1983:                        addcp = mchars_spec2str(seq, seqlen, &addsz);
1.176     schwarze 1984:                        if (addcp == NULL)
1.95      schwarze 1985:                                continue;
1.176     schwarze 1986:                        if (*addcp == ASCII_NBRSP) {
                   1987:                                addcp = " ";
                   1988:                                addsz = 1;
1.95      schwarze 1989:                        }
                   1990:                }
1.1       kristaps 1991:
1.50      kristaps 1992:                /* Copy the rendered glyph into the stream. */
1.1       kristaps 1993:
1.176     schwarze 1994:                ssz += addsz;
                   1995:                dst = mandoc_realloc(dst, ssz + 1);
                   1996:                memcpy(dst + dsz, addcp, addsz);
                   1997:                dsz += addsz;
                   1998:        }
                   1999:        if (dst != NULL) {
                   2000:                *public = dst;
                   2001:                *psz = dsz;
                   2002:        }
                   2003:
                   2004:        /* Trim trailing whitespace and NUL-terminate. */
                   2005:
                   2006:        while (*psz > 0 && (*public)[*psz - 1] == ' ')
                   2007:                --*psz;
                   2008:        if (dst != NULL) {
                   2009:                (*public)[*psz] = '\0';
1.197     schwarze 2010:                return 1;
1.176     schwarze 2011:        } else
1.197     schwarze 2012:                return 0;
1.1       kristaps 2013: }
                   2014:
1.118     schwarze 2015: static void
                   2016: dbadd_mlink(const struct mlink *mlink)
                   2017: {
                   2018:        size_t           i;
                   2019:
                   2020:        i = 1;
                   2021:        SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->dsec);
                   2022:        SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->arch);
                   2023:        SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->name);
1.137     schwarze 2024:        SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, mlink->mpage->pageid);
1.118     schwarze 2025:        SQL_STEP(stmts[STMT_INSERT_LINK]);
                   2026:        sqlite3_reset(stmts[STMT_INSERT_LINK]);
1.169     schwarze 2027: }
                   2028:
                   2029: static void
                   2030: dbadd_mlink_name(const struct mlink *mlink)
                   2031: {
1.175     schwarze 2032:        uint64_t         bits;
1.169     schwarze 2033:        size_t           i;
                   2034:
                   2035:        dbadd_mlink(mlink);
1.160     schwarze 2036:
                   2037:        i = 1;
1.175     schwarze 2038:        SQL_BIND_INT64(stmts[STMT_SELECT_NAME], i, mlink->mpage->pageid);
                   2039:        bits = NAME_FILE & NAME_MASK;
                   2040:        if (sqlite3_step(stmts[STMT_SELECT_NAME]) == SQLITE_ROW) {
                   2041:                bits |= sqlite3_column_int64(stmts[STMT_SELECT_NAME], 0);
                   2042:                sqlite3_reset(stmts[STMT_SELECT_NAME]);
                   2043:        }
                   2044:
                   2045:        i = 1;
                   2046:        SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, bits);
1.160     schwarze 2047:        SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, mlink->name);
                   2048:        SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mlink->mpage->pageid);
                   2049:        SQL_STEP(stmts[STMT_INSERT_NAME]);
                   2050:        sqlite3_reset(stmts[STMT_INSERT_NAME]);
1.118     schwarze 2051: }
                   2052:
1.14      schwarze 2053: /*
1.50      kristaps 2054:  * Flush the current page's terms (and their bits) into the database.
                   2055:  * Wrap the entire set of additions in a transaction to make sqlite be a
                   2056:  * little faster.
1.96      schwarze 2057:  * Also, handle escape sequences at the last possible moment.
1.14      schwarze 2058:  */
                   2059: static void
1.176     schwarze 2060: dbadd(struct mpage *mpage)
1.14      schwarze 2061: {
1.87      schwarze 2062:        struct mlink    *mlink;
1.50      kristaps 2063:        struct str      *key;
1.176     schwarze 2064:        char            *cp;
1.52      kristaps 2065:        size_t           i;
1.66      schwarze 2066:        unsigned int     slot;
1.176     schwarze 2067:        int              mustfree;
1.14      schwarze 2068:
1.128     schwarze 2069:        mlink = mpage->mlinks;
1.43      kristaps 2070:
1.128     schwarze 2071:        if (nodb) {
1.147     schwarze 2072:                for (key = ohash_first(&names, &slot); NULL != key;
1.176     schwarze 2073:                     key = ohash_next(&names, &slot))
1.147     schwarze 2074:                        free(key);
                   2075:                for (key = ohash_first(&strings, &slot); NULL != key;
1.176     schwarze 2076:                     key = ohash_next(&strings, &slot))
1.147     schwarze 2077:                        free(key);
1.145     schwarze 2078:                if (0 == debug)
                   2079:                        return;
1.128     schwarze 2080:                while (NULL != mlink) {
                   2081:                        fputs(mlink->name, stdout);
                   2082:                        if (NULL == mlink->next ||
                   2083:                            strcmp(mlink->dsec, mlink->next->dsec) ||
                   2084:                            strcmp(mlink->fsec, mlink->next->fsec) ||
                   2085:                            strcmp(mlink->arch, mlink->next->arch)) {
                   2086:                                putchar('(');
                   2087:                                if ('\0' == *mlink->dsec)
                   2088:                                        fputs(mlink->fsec, stdout);
                   2089:                                else
                   2090:                                        fputs(mlink->dsec, stdout);
                   2091:                                if ('\0' != *mlink->arch)
                   2092:                                        printf("/%s", mlink->arch);
                   2093:                                putchar(')');
                   2094:                        }
                   2095:                        mlink = mlink->next;
                   2096:                        if (NULL != mlink)
                   2097:                                fputs(", ", stdout);
                   2098:                }
1.132     schwarze 2099:                printf(" - %s\n", mpage->desc);
1.14      schwarze 2100:                return;
1.128     schwarze 2101:        }
                   2102:
                   2103:        if (debug)
                   2104:                say(mlink->file, "Adding to database");
1.28      kristaps 2105:
1.176     schwarze 2106:        cp = mpage->desc;
                   2107:        i = strlen(cp);
                   2108:        mustfree = render_string(&cp, &i);
1.52      kristaps 2109:        i = 1;
1.176     schwarze 2110:        SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, cp);
1.161     schwarze 2111:        SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, mpage->form);
1.81      schwarze 2112:        SQL_STEP(stmts[STMT_INSERT_PAGE]);
1.137     schwarze 2113:        mpage->pageid = sqlite3_last_insert_rowid(db);
1.81      schwarze 2114:        sqlite3_reset(stmts[STMT_INSERT_PAGE]);
1.176     schwarze 2115:        if (mustfree)
                   2116:                free(cp);
1.81      schwarze 2117:
1.128     schwarze 2118:        while (NULL != mlink) {
1.118     schwarze 2119:                dbadd_mlink(mlink);
1.128     schwarze 2120:                mlink = mlink->next;
                   2121:        }
1.134     schwarze 2122:        mlink = mpage->mlinks;
1.50      kristaps 2123:
1.133     schwarze 2124:        for (key = ohash_first(&names, &slot); NULL != key;
                   2125:             key = ohash_next(&names, &slot)) {
                   2126:                assert(key->mpage == mpage);
                   2127:                i = 1;
                   2128:                SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, key->mask);
1.176     schwarze 2129:                SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, key->key);
1.137     schwarze 2130:                SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mpage->pageid);
1.133     schwarze 2131:                SQL_STEP(stmts[STMT_INSERT_NAME]);
                   2132:                sqlite3_reset(stmts[STMT_INSERT_NAME]);
                   2133:                free(key);
                   2134:        }
1.66      schwarze 2135:        for (key = ohash_first(&strings, &slot); NULL != key;
                   2136:             key = ohash_next(&strings, &slot)) {
1.78      schwarze 2137:                assert(key->mpage == mpage);
1.52      kristaps 2138:                i = 1;
                   2139:                SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);
1.176     schwarze 2140:                SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->key);
1.137     schwarze 2141:                SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, mpage->pageid);
1.52      kristaps 2142:                SQL_STEP(stmts[STMT_INSERT_KEY]);
1.50      kristaps 2143:                sqlite3_reset(stmts[STMT_INSERT_KEY]);
1.66      schwarze 2144:                free(key);
1.38      schwarze 2145:        }
1.14      schwarze 2146: }
                   2147:
1.5       kristaps 2148: static void
1.59      schwarze 2149: dbprune(void)
1.5       kristaps 2150: {
1.78      schwarze 2151:        struct mpage    *mpage;
1.82      schwarze 2152:        struct mlink    *mlink;
1.52      kristaps 2153:        size_t           i;
1.80      schwarze 2154:        unsigned int     slot;
1.5       kristaps 2155:
1.106     schwarze 2156:        if (0 == nodb)
                   2157:                SQL_EXEC("BEGIN TRANSACTION");
1.12      schwarze 2158:
1.106     schwarze 2159:        for (mpage = ohash_first(&mpages, &slot); NULL != mpage;
                   2160:             mpage = ohash_next(&mpages, &slot)) {
1.82      schwarze 2161:                mlink = mpage->mlinks;
1.125     schwarze 2162:                if (debug)
1.106     schwarze 2163:                        say(mlink->file, "Deleting from database");
                   2164:                if (nodb)
                   2165:                        continue;
                   2166:                for ( ; NULL != mlink; mlink = mlink->next) {
                   2167:                        i = 1;
                   2168:                        SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
                   2169:                            i, mlink->dsec);
                   2170:                        SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
                   2171:                            i, mlink->arch);
                   2172:                        SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
                   2173:                            i, mlink->name);
                   2174:                        SQL_STEP(stmts[STMT_DELETE_PAGE]);
                   2175:                        sqlite3_reset(stmts[STMT_DELETE_PAGE]);
                   2176:                }
1.5       kristaps 2177:        }
1.106     schwarze 2178:
                   2179:        if (0 == nodb)
                   2180:                SQL_EXEC("END TRANSACTION");
1.5       kristaps 2181: }
                   2182:
1.4       kristaps 2183: /*
1.50      kristaps 2184:  * Close an existing database and its prepared statements.
                   2185:  * If "real" is not set, rename the temporary file into the real one.
1.4       kristaps 2186:  */
1.35      kristaps 2187: static void
1.59      schwarze 2188: dbclose(int real)
1.4       kristaps 2189: {
1.50      kristaps 2190:        size_t           i;
1.115     schwarze 2191:        int              status;
                   2192:        pid_t            child;
1.4       kristaps 2193:
1.50      kristaps 2194:        if (nodb)
1.38      schwarze 2195:                return;
1.50      kristaps 2196:
                   2197:        for (i = 0; i < STMT__MAX; i++) {
                   2198:                sqlite3_finalize(stmts[i]);
                   2199:                stmts[i] = NULL;
1.4       kristaps 2200:        }
                   2201:
1.50      kristaps 2202:        sqlite3_close(db);
                   2203:        db = NULL;
1.12      schwarze 2204:
1.50      kristaps 2205:        if (real)
                   2206:                return;
1.12      schwarze 2207:
1.115     schwarze 2208:        if ('\0' == *tempfilename) {
                   2209:                if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {
                   2210:                        exitcode = (int)MANDOCLEVEL_SYSERR;
1.123     schwarze 2211:                        say(MANDOC_DB, "&rename");
1.115     schwarze 2212:                }
                   2213:                return;
                   2214:        }
                   2215:
                   2216:        switch (child = fork()) {
1.141     schwarze 2217:        case -1:
1.115     schwarze 2218:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.123     schwarze 2219:                say("", "&fork cmp");
1.115     schwarze 2220:                return;
1.141     schwarze 2221:        case 0:
1.115     schwarze 2222:                execlp("cmp", "cmp", "-s",
1.196     schwarze 2223:                    tempfilename, MANDOC_DB, (char *)NULL);
1.123     schwarze 2224:                say("", "&exec cmp");
1.115     schwarze 2225:                exit(0);
                   2226:        default:
                   2227:                break;
                   2228:        }
                   2229:        if (-1 == waitpid(child, &status, 0)) {
                   2230:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.123     schwarze 2231:                say("", "&wait cmp");
1.115     schwarze 2232:        } else if (WIFSIGNALED(status)) {
                   2233:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.123     schwarze 2234:                say("", "cmp died from signal %d", WTERMSIG(status));
1.115     schwarze 2235:        } else if (WEXITSTATUS(status)) {
1.59      schwarze 2236:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.115     schwarze 2237:                say(MANDOC_DB,
                   2238:                    "Data changed, but cannot replace database");
                   2239:        }
                   2240:
                   2241:        *strrchr(tempfilename, '/') = '\0';
                   2242:        switch (child = fork()) {
1.141     schwarze 2243:        case -1:
1.115     schwarze 2244:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.123     schwarze 2245:                say("", "&fork rm");
1.115     schwarze 2246:                return;
1.141     schwarze 2247:        case 0:
1.196     schwarze 2248:                execlp("rm", "rm", "-rf", tempfilename, (char *)NULL);
1.123     schwarze 2249:                say("", "&exec rm");
1.115     schwarze 2250:                exit((int)MANDOCLEVEL_SYSERR);
                   2251:        default:
                   2252:                break;
                   2253:        }
                   2254:        if (-1 == waitpid(child, &status, 0)) {
                   2255:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.123     schwarze 2256:                say("", "&wait rm");
1.115     schwarze 2257:        } else if (WIFSIGNALED(status) || WEXITSTATUS(status)) {
                   2258:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.123     schwarze 2259:                say("", "%s: Cannot remove temporary directory",
                   2260:                    tempfilename);
1.59      schwarze 2261:        }
1.50      kristaps 2262: }
1.14      schwarze 2263:
1.50      kristaps 2264: /*
                   2265:  * This is straightforward stuff.
                   2266:  * Open a database connection to a "temporary" database, then open a set
                   2267:  * of prepared statements we'll use over and over again.
                   2268:  * If "real" is set, we use the existing database; if not, we truncate a
                   2269:  * temporary one.
                   2270:  * Must be matched by dbclose().
                   2271:  */
                   2272: static int
1.59      schwarze 2273: dbopen(int real)
1.50      kristaps 2274: {
1.115     schwarze 2275:        const char      *sql;
1.50      kristaps 2276:        int              rc, ofl;
1.12      schwarze 2277:
1.141     schwarze 2278:        if (nodb)
1.197     schwarze 2279:                return 1;
1.12      schwarze 2280:
1.115     schwarze 2281:        *tempfilename = '\0';
1.63      schwarze 2282:        ofl = SQLITE_OPEN_READWRITE;
1.115     schwarze 2283:
                   2284:        if (real) {
                   2285:                rc = sqlite3_open_v2(MANDOC_DB, &db, ofl, NULL);
                   2286:                if (SQLITE_OK != rc) {
1.63      schwarze 2287:                        exitcode = (int)MANDOCLEVEL_SYSERR;
1.149     schwarze 2288:                        if (SQLITE_CANTOPEN != rc)
                   2289:                                say(MANDOC_DB, "%s", sqlite3_errstr(rc));
1.197     schwarze 2290:                        return 0;
1.63      schwarze 2291:                }
1.115     schwarze 2292:                goto prepare_statements;
                   2293:        }
                   2294:
                   2295:        ofl |= SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE;
1.45      kristaps 2296:
1.115     schwarze 2297:        remove(MANDOC_DB "~");
                   2298:        rc = sqlite3_open_v2(MANDOC_DB "~", &db, ofl, NULL);
1.141     schwarze 2299:        if (SQLITE_OK == rc)
1.115     schwarze 2300:                goto create_tables;
1.116     schwarze 2301:        if (MPARSE_QUICK & mparse_options) {
1.59      schwarze 2302:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.146     schwarze 2303:                say(MANDOC_DB "~", "%s", sqlite3_errstr(rc));
1.197     schwarze 2304:                return 0;
1.50      kristaps 2305:        }
1.12      schwarze 2306:
1.143     schwarze 2307:        (void)strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX",
                   2308:            sizeof(tempfilename));
1.115     schwarze 2309:        if (NULL == mkdtemp(tempfilename)) {
                   2310:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.123     schwarze 2311:                say("", "&%s", tempfilename);
1.197     schwarze 2312:                return 0;
1.115     schwarze 2313:        }
1.143     schwarze 2314:        (void)strlcat(tempfilename, "/" MANDOC_DB,
                   2315:            sizeof(tempfilename));
1.115     schwarze 2316:        rc = sqlite3_open_v2(tempfilename, &db, ofl, NULL);
                   2317:        if (SQLITE_OK != rc) {
1.59      schwarze 2318:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.146     schwarze 2319:                say("", "%s: %s", tempfilename, sqlite3_errstr(rc));
1.197     schwarze 2320:                return 0;
1.50      kristaps 2321:        }
1.12      schwarze 2322:
1.115     schwarze 2323: create_tables:
1.81      schwarze 2324:        sql = "CREATE TABLE \"mpages\" (\n"
1.132     schwarze 2325:              " \"desc\" TEXT NOT NULL,\n"
1.50      kristaps 2326:              " \"form\" INTEGER NOT NULL,\n"
1.137     schwarze 2327:              " \"pageid\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1.50      kristaps 2328:              ");\n"
                   2329:              "\n"
1.81      schwarze 2330:              "CREATE TABLE \"mlinks\" (\n"
                   2331:              " \"sec\" TEXT NOT NULL,\n"
                   2332:              " \"arch\" TEXT NOT NULL,\n"
                   2333:              " \"name\" TEXT NOT NULL,\n"
1.137     schwarze 2334:              " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
1.109     schwarze 2335:                "ON DELETE CASCADE\n"
1.81      schwarze 2336:              ");\n"
1.136     schwarze 2337:              "CREATE INDEX mlinks_pageid_idx ON mlinks (pageid);\n"
1.81      schwarze 2338:              "\n"
1.133     schwarze 2339:              "CREATE TABLE \"names\" (\n"
                   2340:              " \"bits\" INTEGER NOT NULL,\n"
                   2341:              " \"name\" TEXT NOT NULL,\n"
1.137     schwarze 2342:              " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
1.175     schwarze 2343:                "ON DELETE CASCADE,\n"
                   2344:              " UNIQUE (\"name\", \"pageid\") ON CONFLICT REPLACE\n"
1.133     schwarze 2345:              ");\n"
                   2346:              "\n"
1.50      kristaps 2347:              "CREATE TABLE \"keys\" (\n"
                   2348:              " \"bits\" INTEGER NOT NULL,\n"
                   2349:              " \"key\" TEXT NOT NULL,\n"
1.137     schwarze 2350:              " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
1.109     schwarze 2351:                "ON DELETE CASCADE\n"
1.136     schwarze 2352:              ");\n"
                   2353:              "CREATE INDEX keys_pageid_idx ON keys (pageid);\n";
1.14      schwarze 2354:
1.50      kristaps 2355:        if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
1.59      schwarze 2356:                exitcode = (int)MANDOCLEVEL_SYSERR;
1.115     schwarze 2357:                say(MANDOC_DB, "%s", sqlite3_errmsg(db));
1.146     schwarze 2358:                sqlite3_close(db);
1.197     schwarze 2359:                return 0;
1.50      kristaps 2360:        }
1.4       kristaps 2361:
1.57      schwarze 2362: prepare_statements:
1.146     schwarze 2363:        if (SQLITE_OK != sqlite3_exec(db,
                   2364:            "PRAGMA foreign_keys = ON", NULL, NULL, NULL)) {
                   2365:                exitcode = (int)MANDOCLEVEL_SYSERR;
                   2366:                say(MANDOC_DB, "PRAGMA foreign_keys: %s",
                   2367:                    sqlite3_errmsg(db));
                   2368:                sqlite3_close(db);
1.197     schwarze 2369:                return 0;
1.146     schwarze 2370:        }
                   2371:
1.137     schwarze 2372:        sql = "DELETE FROM mpages WHERE pageid IN "
1.106     schwarze 2373:                "(SELECT pageid FROM mlinks WHERE "
                   2374:                "sec=? AND arch=? AND name=?)";
1.81      schwarze 2375:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL);
                   2376:        sql = "INSERT INTO mpages "
1.132     schwarze 2377:                "(desc,form) VALUES (?,?)";
1.81      schwarze 2378:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL);
                   2379:        sql = "INSERT INTO mlinks "
1.104     schwarze 2380:                "(sec,arch,name,pageid) VALUES (?,?,?,?)";
1.81      schwarze 2381:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL);
1.175     schwarze 2382:        sql = "SELECT bits FROM names where pageid = ?";
                   2383:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_SELECT_NAME], NULL);
1.133     schwarze 2384:        sql = "INSERT INTO names "
                   2385:                "(bits,name,pageid) VALUES (?,?,?)";
                   2386:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_NAME], NULL);
1.50      kristaps 2387:        sql = "INSERT INTO keys "
1.81      schwarze 2388:                "(bits,key,pageid) VALUES (?,?,?)";
1.50      kristaps 2389:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL);
1.70      schwarze 2390:
                   2391: #ifndef __APPLE__
                   2392:        /*
                   2393:         * When opening a new database, we can turn off
                   2394:         * synchronous mode for much better performance.
                   2395:         */
                   2396:
1.146     schwarze 2397:        if (real && SQLITE_OK != sqlite3_exec(db,
                   2398:            "PRAGMA synchronous = OFF", NULL, NULL, NULL)) {
                   2399:                exitcode = (int)MANDOCLEVEL_SYSERR;
                   2400:                say(MANDOC_DB, "PRAGMA synchronous: %s",
1.162     schwarze 2401:                    sqlite3_errmsg(db));
1.146     schwarze 2402:                sqlite3_close(db);
1.197     schwarze 2403:                return 0;
1.146     schwarze 2404:        }
1.70      schwarze 2405: #endif
                   2406:
1.197     schwarze 2407:        return 1;
1.4       kristaps 2408: }
                   2409:
1.50      kristaps 2410: static int
1.165     schwarze 2411: set_basedir(const char *targetdir, int report_baddir)
1.4       kristaps 2412: {
1.59      schwarze 2413:        static char      startdir[PATH_MAX];
1.151     schwarze 2414:        static int       getcwd_status;  /* 1 = ok, 2 = failure */
                   2415:        static int       chdir_status;  /* 1 = changed directory */
1.150     schwarze 2416:        char            *cp;
1.4       kristaps 2417:
1.59      schwarze 2418:        /*
1.151     schwarze 2419:         * Remember the original working directory, if possible.
                   2420:         * This will be needed if the second or a later directory
                   2421:         * on the command line is given as a relative path.
                   2422:         * Do not error out if the current directory is not
                   2423:         * searchable: Maybe it won't be needed after all.
                   2424:         */
                   2425:        if (0 == getcwd_status) {
                   2426:                if (NULL == getcwd(startdir, sizeof(startdir))) {
                   2427:                        getcwd_status = 2;
                   2428:                        (void)strlcpy(startdir, strerror(errno),
                   2429:                            sizeof(startdir));
                   2430:                } else
                   2431:                        getcwd_status = 1;
                   2432:        }
                   2433:
                   2434:        /*
                   2435:         * We are leaving the old base directory.
                   2436:         * Do not use it any longer, not even for messages.
                   2437:         */
                   2438:        *basedir = '\0';
                   2439:
                   2440:        /*
                   2441:         * If and only if the directory was changed earlier and
                   2442:         * the next directory to process is given as a relative path,
                   2443:         * first go back, or bail out if that is impossible.
1.59      schwarze 2444:         */
1.151     schwarze 2445:        if (chdir_status && '/' != *targetdir) {
                   2446:                if (2 == getcwd_status) {
1.59      schwarze 2447:                        exitcode = (int)MANDOCLEVEL_SYSERR;
1.151     schwarze 2448:                        say("", "getcwd: %s", startdir);
1.197     schwarze 2449:                        return 0;
1.59      schwarze 2450:                }
1.151     schwarze 2451:                if (-1 == chdir(startdir)) {
1.59      schwarze 2452:                        exitcode = (int)MANDOCLEVEL_SYSERR;
1.123     schwarze 2453:                        say("", "&chdir %s", startdir);
1.197     schwarze 2454:                        return 0;
1.59      schwarze 2455:                }
                   2456:        }
1.151     schwarze 2457:
                   2458:        /*
                   2459:         * Always resolve basedir to the canonicalized absolute
                   2460:         * pathname and append a trailing slash, such that
                   2461:         * we can reliably check whether files are inside.
                   2462:         */
1.59      schwarze 2463:        if (NULL == realpath(targetdir, basedir)) {
1.165     schwarze 2464:                if (report_baddir || errno != ENOENT) {
                   2465:                        exitcode = (int)MANDOCLEVEL_BADARG;
                   2466:                        say("", "&%s: realpath", targetdir);
                   2467:                }
1.197     schwarze 2468:                return 0;
1.59      schwarze 2469:        } else if (-1 == chdir(basedir)) {
1.165     schwarze 2470:                if (report_baddir || errno != ENOENT) {
                   2471:                        exitcode = (int)MANDOCLEVEL_BADARG;
                   2472:                        say("", "&chdir");
                   2473:                }
1.197     schwarze 2474:                return 0;
1.150     schwarze 2475:        }
1.151     schwarze 2476:        chdir_status = 1;
1.150     schwarze 2477:        cp = strchr(basedir, '\0');
                   2478:        if ('/' != cp[-1]) {
                   2479:                if (cp - basedir >= PATH_MAX - 1) {
                   2480:                        exitcode = (int)MANDOCLEVEL_SYSERR;
                   2481:                        say("", "Filename too long");
1.197     schwarze 2482:                        return 0;
1.150     schwarze 2483:                }
                   2484:                *cp++ = '/';
                   2485:                *cp = '\0';
1.4       kristaps 2486:        }
1.197     schwarze 2487:        return 1;
1.56      schwarze 2488: }
                   2489:
                   2490: static void
1.59      schwarze 2491: say(const char *file, const char *format, ...)
1.56      schwarze 2492: {
                   2493:        va_list          ap;
1.123     schwarze 2494:        int              use_errno;
1.56      schwarze 2495:
1.59      schwarze 2496:        if ('\0' != *basedir)
                   2497:                fprintf(stderr, "%s", basedir);
                   2498:        if ('\0' != *basedir && '\0' != *file)
1.151     schwarze 2499:                fputc('/', stderr);
1.56      schwarze 2500:        if ('\0' != *file)
1.59      schwarze 2501:                fprintf(stderr, "%s", file);
                   2502:
1.123     schwarze 2503:        use_errno = 1;
                   2504:        if (NULL != format) {
                   2505:                switch (*format) {
1.141     schwarze 2506:                case '&':
1.123     schwarze 2507:                        format++;
                   2508:                        break;
1.141     schwarze 2509:                case '\0':
1.123     schwarze 2510:                        format = NULL;
                   2511:                        break;
                   2512:                default:
                   2513:                        use_errno = 0;
                   2514:                        break;
                   2515:                }
                   2516:        }
                   2517:        if (NULL != format) {
                   2518:                if ('\0' != *basedir || '\0' != *file)
                   2519:                        fputs(": ", stderr);
                   2520:                va_start(ap, format);
                   2521:                vfprintf(stderr, format, ap);
                   2522:                va_end(ap);
                   2523:        }
                   2524:        if (use_errno) {
                   2525:                if ('\0' != *basedir || '\0' != *file || NULL != format)
                   2526:                        fputs(": ", stderr);
1.59      schwarze 2527:                perror(NULL);
1.123     schwarze 2528:        } else
                   2529:                fputc('\n', stderr);
1.1       kristaps 2530: }

CVSweb