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

Annotation of mandoc/mandocdb.c, Revision 1.207

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

CVSweb