[BACK]Return to mandocdb.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mandocdb.c, Revision 1.129

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