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

Annotation of mandoc/mandocdb.c, Revision 1.220.2.2

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

CVSweb