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

Annotation of mandoc/mandocdb.c, Revision 1.215

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

CVSweb