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

Annotation of mandoc/mandocdb.c, Revision 1.86

1.86    ! schwarze    1: /*     $Id: mandocdb.c,v 1.85 2013/12/27 15:48:09 schwarze Exp $ */
1.1       kristaps    2: /*
1.48      schwarze    3:  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.56      schwarze    4:  * Copyright (c) 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     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:  */
                     18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.50      kristaps   22: #include <sys/stat.h>
1.1       kristaps   23:
                     24: #include <assert.h>
1.43      kristaps   25: #include <ctype.h>
1.63      schwarze   26: #include <errno.h>
1.1       kristaps   27: #include <fcntl.h>
1.50      kristaps   28: #include <fts.h>
1.1       kristaps   29: #include <getopt.h>
1.58      schwarze   30: #include <limits.h>
1.50      kristaps   31: #include <stddef.h>
1.59      schwarze   32: #include <stdio.h>
1.1       kristaps   33: #include <stdint.h>
                     34: #include <stdlib.h>
                     35: #include <string.h>
1.17      schwarze   36: #include <unistd.h>
1.1       kristaps   37:
1.53      kristaps   38: #ifdef HAVE_OHASH
1.50      kristaps   39: #include <ohash.h>
1.53      kristaps   40: #else
                     41: #include "compat_ohash.h"
                     42: #endif
1.50      kristaps   43: #include <sqlite3.h>
1.1       kristaps   44:
1.50      kristaps   45: #include "mdoc.h"
1.1       kristaps   46: #include "man.h"
                     47: #include "mandoc.h"
1.10      kristaps   48: #include "manpath.h"
1.55      kristaps   49: #include "mansearch.h"
1.1       kristaps   50:
1.52      kristaps   51: #define        SQL_EXEC(_v) \
                     52:        if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
                     53:                fprintf(stderr, "%s\n", sqlite3_errmsg(db))
                     54: #define        SQL_BIND_TEXT(_s, _i, _v) \
                     55:        if (SQLITE_OK != sqlite3_bind_text \
                     56:                ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
                     57:                fprintf(stderr, "%s\n", sqlite3_errmsg(db))
                     58: #define        SQL_BIND_INT(_s, _i, _v) \
                     59:        if (SQLITE_OK != sqlite3_bind_int \
                     60:                ((_s), (_i)++, (_v))) \
                     61:                fprintf(stderr, "%s\n", sqlite3_errmsg(db))
                     62: #define        SQL_BIND_INT64(_s, _i, _v) \
                     63:        if (SQLITE_OK != sqlite3_bind_int64 \
                     64:                ((_s), (_i)++, (_v))) \
                     65:                fprintf(stderr, "%s\n", sqlite3_errmsg(db))
                     66: #define SQL_STEP(_s) \
                     67:        if (SQLITE_DONE != sqlite3_step((_s))) \
                     68:                fprintf(stderr, "%s\n", sqlite3_errmsg(db))
                     69:
1.50      kristaps   70: enum   op {
                     71:        OP_DEFAULT = 0, /* new dbs from dir list or default config */
                     72:        OP_CONFFILE, /* new databases from custom config file */
                     73:        OP_UPDATE, /* delete/add entries in existing database */
                     74:        OP_DELETE, /* delete entries from existing database */
                     75:        OP_TEST /* change no databases, report potential problems */
1.38      schwarze   76: };
                     77:
1.50      kristaps   78: enum   form {
1.84      schwarze   79:        FORM_NONE,  /* format is unknown */
                     80:        FORM_SRC,   /* format is -man or -mdoc */
                     81:        FORM_CAT    /* format is cat */
1.50      kristaps   82: };
1.38      schwarze   83:
1.50      kristaps   84: struct str {
                     85:        char            *utf8; /* key in UTF-8 form */
1.78      schwarze   86:        const struct mpage *mpage; /* if set, the owning parse */
1.50      kristaps   87:        uint64_t         mask; /* bitmask in sequence */
1.51      kristaps   88:        char             key[]; /* the string itself */
1.38      schwarze   89: };
                     90:
1.79      schwarze   91: struct inodev {
                     92:        ino_t            st_ino;
                     93:        dev_t            st_dev;
1.50      kristaps   94: };
1.5       kristaps   95:
1.78      schwarze   96: struct mpage {
1.79      schwarze   97:        struct inodev    inodev;  /* used for hashing routine */
1.82      schwarze   98:        enum form        form;    /* format from file content */
                     99:        char            *sec;     /* section from file content */
                    100:        char            *arch;    /* architecture from file content */
                    101:        char            *title;   /* title from file content */
                    102:        char            *desc;    /* description from file content */
                    103:        struct mlink    *mlinks;  /* singly linked list */
                    104: };
                    105:
                    106: struct mlink {
1.58      schwarze  107:        char             file[PATH_MAX]; /* filename rel. to manpath */
1.82      schwarze  108:        enum form        dform;   /* format from directory */
1.84      schwarze  109:        enum form        fform;   /* format from file name suffix */
1.82      schwarze  110:        char            *dsec;    /* section from directory */
                    111:        char            *arch;    /* architecture from directory */
                    112:        char            *name;    /* name from file name (not empty) */
                    113:        char            *fsec;    /* section from file name suffix */
1.85      schwarze  114:        struct mlink    *next;    /* singly linked list */
1.3       kristaps  115: };
                    116:
1.69      schwarze  117: struct title {
                    118:        char            *title; /* name(sec/arch) given inside the file */
                    119:        char            *file; /* file name in case of mismatch */
                    120: };
                    121:
1.50      kristaps  122: enum   stmt {
1.81      schwarze  123:        STMT_DELETE_PAGE = 0,   /* delete mpage */
                    124:        STMT_INSERT_PAGE,       /* insert mpage */
                    125:        STMT_INSERT_LINK,       /* insert mlink */
                    126:        STMT_INSERT_KEY,        /* insert parsed key */
1.50      kristaps  127:        STMT__MAX
1.1       kristaps  128: };
                    129:
1.78      schwarze  130: typedef        int (*mdoc_fp)(struct mpage *, const struct mdoc_node *);
1.1       kristaps  131:
1.50      kristaps  132: struct mdoc_handler {
                    133:        mdoc_fp          fp; /* optional handler */
                    134:        uint64_t         mask;  /* set unless handler returns 0 */
1.1       kristaps  135: };
                    136:
1.59      schwarze  137: static void     dbclose(int);
1.82      schwarze  138: static void     dbindex(const struct mpage *, struct mchars *);
1.59      schwarze  139: static int      dbopen(int);
                    140: static void     dbprune(void);
                    141: static void     filescan(const char *);
1.50      kristaps  142: static void    *hash_alloc(size_t, void *);
                    143: static void     hash_free(void *, size_t, void *);
                    144: static void    *hash_halloc(size_t, void *);
1.84      schwarze  145: static void     mlink_add(struct mlink *, const struct stat *);
1.82      schwarze  146: static void     mlink_free(struct mlink *);
1.78      schwarze  147: static void     mpages_free(void);
                    148: static void     mpages_merge(struct mchars *, struct mparse *, int);
                    149: static void     parse_cat(struct mpage *);
                    150: static void     parse_man(struct mpage *, const struct man_node *);
                    151: static void     parse_mdoc(struct mpage *, const struct mdoc_node *);
                    152: static int      parse_mdoc_body(struct mpage *, const struct mdoc_node *);
                    153: static int      parse_mdoc_head(struct mpage *, const struct mdoc_node *);
                    154: static int      parse_mdoc_Fd(struct mpage *, const struct mdoc_node *);
                    155: static int      parse_mdoc_Fn(struct mpage *, const struct mdoc_node *);
                    156: static int      parse_mdoc_In(struct mpage *, const struct mdoc_node *);
                    157: static int      parse_mdoc_Nd(struct mpage *, const struct mdoc_node *);
                    158: static int      parse_mdoc_Nm(struct mpage *, const struct mdoc_node *);
                    159: static int      parse_mdoc_Sh(struct mpage *, const struct mdoc_node *);
                    160: static int      parse_mdoc_St(struct mpage *, const struct mdoc_node *);
                    161: static int      parse_mdoc_Xr(struct mpage *, const struct mdoc_node *);
                    162: static void     putkey(const struct mpage *,
1.50      kristaps  163:                        const char *, uint64_t);
1.78      schwarze  164: static void     putkeys(const struct mpage *,
1.64      schwarze  165:                        const char *, size_t, uint64_t);
1.78      schwarze  166: static void     putmdockey(const struct mpage *,
1.50      kristaps  167:                        const struct mdoc_node *, uint64_t);
1.59      schwarze  168: static void     say(const char *, const char *, ...);
1.80      schwarze  169: static int      set_basedir(const char *);
1.59      schwarze  170: static int      treescan(void);
1.50      kristaps  171: static size_t   utf8(unsigned int, char [7]);
                    172: static void     utf8key(struct mchars *, struct str *);
                    173:
                    174: static char            *progname;
                    175: static int              use_all; /* use all found files */
                    176: static int              nodb; /* no database changes */
                    177: static int              verb; /* print what we're doing */
                    178: static int              warnings; /* warn about crap */
1.59      schwarze  179: static int              exitcode; /* to be returned by main */
1.50      kristaps  180: static enum op          op; /* operational mode */
1.59      schwarze  181: static char             basedir[PATH_MAX]; /* current base directory */
1.78      schwarze  182: static struct ohash     mpages; /* table of distinct manual pages */
1.83      schwarze  183: static struct ohash     mlinks; /* table of directory entries */
1.50      kristaps  184: static struct ohash     strings; /* table of all strings */
                    185: static sqlite3         *db = NULL; /* current database */
                    186: static sqlite3_stmt    *stmts[STMT__MAX]; /* current statements */
1.25      schwarze  187:
                    188: static const struct mdoc_handler mdocs[MDOC_MAX] = {
1.68      schwarze  189:        { NULL, 0 },  /* Ap */
                    190:        { NULL, 0 },  /* Dd */
                    191:        { NULL, 0 },  /* Dt */
                    192:        { NULL, 0 },  /* Os */
                    193:        { parse_mdoc_Sh, TYPE_Sh }, /* Sh */
                    194:        { parse_mdoc_head, TYPE_Ss }, /* Ss */
                    195:        { NULL, 0 },  /* Pp */
                    196:        { NULL, 0 },  /* D1 */
                    197:        { NULL, 0 },  /* Dl */
                    198:        { NULL, 0 },  /* Bd */
                    199:        { NULL, 0 },  /* Ed */
                    200:        { NULL, 0 },  /* Bl */
                    201:        { NULL, 0 },  /* El */
                    202:        { NULL, 0 },  /* It */
                    203:        { NULL, 0 },  /* Ad */
                    204:        { NULL, TYPE_An },  /* An */
                    205:        { NULL, TYPE_Ar },  /* Ar */
                    206:        { NULL, TYPE_Cd },  /* Cd */
                    207:        { NULL, TYPE_Cm },  /* Cm */
                    208:        { NULL, TYPE_Dv },  /* Dv */
                    209:        { NULL, TYPE_Er },  /* Er */
                    210:        { NULL, TYPE_Ev },  /* Ev */
                    211:        { NULL, 0 },  /* Ex */
                    212:        { NULL, TYPE_Fa },  /* Fa */
                    213:        { parse_mdoc_Fd, 0 },  /* Fd */
                    214:        { NULL, TYPE_Fl },  /* Fl */
                    215:        { parse_mdoc_Fn, 0 },  /* Fn */
                    216:        { NULL, TYPE_Ft },  /* Ft */
                    217:        { NULL, TYPE_Ic },  /* Ic */
                    218:        { parse_mdoc_In, TYPE_In },  /* In */
                    219:        { NULL, TYPE_Li },  /* Li */
                    220:        { parse_mdoc_Nd, TYPE_Nd },  /* Nd */
                    221:        { parse_mdoc_Nm, TYPE_Nm },  /* Nm */
                    222:        { NULL, 0 },  /* Op */
                    223:        { NULL, 0 },  /* Ot */
                    224:        { NULL, TYPE_Pa },  /* Pa */
                    225:        { NULL, 0 },  /* Rv */
                    226:        { parse_mdoc_St, 0 },  /* St */
                    227:        { NULL, TYPE_Va },  /* Va */
                    228:        { parse_mdoc_body, TYPE_Va },  /* Vt */
                    229:        { parse_mdoc_Xr, 0 },  /* Xr */
                    230:        { NULL, 0 },  /* %A */
                    231:        { NULL, 0 },  /* %B */
                    232:        { NULL, 0 },  /* %D */
                    233:        { NULL, 0 },  /* %I */
                    234:        { NULL, 0 },  /* %J */
                    235:        { NULL, 0 },  /* %N */
                    236:        { NULL, 0 },  /* %O */
                    237:        { NULL, 0 },  /* %P */
                    238:        { NULL, 0 },  /* %R */
                    239:        { NULL, 0 },  /* %T */
                    240:        { NULL, 0 },  /* %V */
                    241:        { NULL, 0 },  /* Ac */
                    242:        { NULL, 0 },  /* Ao */
                    243:        { NULL, 0 },  /* Aq */
                    244:        { NULL, TYPE_At },  /* At */
                    245:        { NULL, 0 },  /* Bc */
                    246:        { NULL, 0 },  /* Bf */
                    247:        { NULL, 0 },  /* Bo */
                    248:        { NULL, 0 },  /* Bq */
                    249:        { NULL, TYPE_Bsx },  /* Bsx */
                    250:        { NULL, TYPE_Bx },  /* Bx */
                    251:        { NULL, 0 },  /* Db */
                    252:        { NULL, 0 },  /* Dc */
                    253:        { NULL, 0 },  /* Do */
                    254:        { NULL, 0 },  /* Dq */
                    255:        { NULL, 0 },  /* Ec */
                    256:        { NULL, 0 },  /* Ef */
                    257:        { NULL, TYPE_Em },  /* Em */
                    258:        { NULL, 0 },  /* Eo */
                    259:        { NULL, TYPE_Fx },  /* Fx */
                    260:        { NULL, TYPE_Ms },  /* Ms */
                    261:        { NULL, 0 },  /* No */
                    262:        { NULL, 0 },  /* Ns */
                    263:        { NULL, TYPE_Nx },  /* Nx */
                    264:        { NULL, TYPE_Ox },  /* Ox */
                    265:        { NULL, 0 },  /* Pc */
                    266:        { NULL, 0 },  /* Pf */
                    267:        { NULL, 0 },  /* Po */
                    268:        { NULL, 0 },  /* Pq */
                    269:        { NULL, 0 },  /* Qc */
                    270:        { NULL, 0 },  /* Ql */
                    271:        { NULL, 0 },  /* Qo */
                    272:        { NULL, 0 },  /* Qq */
                    273:        { NULL, 0 },  /* Re */
                    274:        { NULL, 0 },  /* Rs */
                    275:        { NULL, 0 },  /* Sc */
                    276:        { NULL, 0 },  /* So */
                    277:        { NULL, 0 },  /* Sq */
                    278:        { NULL, 0 },  /* Sm */
                    279:        { NULL, 0 },  /* Sx */
                    280:        { NULL, TYPE_Sy },  /* Sy */
                    281:        { NULL, TYPE_Tn },  /* Tn */
                    282:        { NULL, 0 },  /* Ux */
                    283:        { NULL, 0 },  /* Xc */
                    284:        { NULL, 0 },  /* Xo */
                    285:        { parse_mdoc_head, 0 },  /* Fo */
                    286:        { NULL, 0 },  /* Fc */
                    287:        { NULL, 0 },  /* Oo */
                    288:        { NULL, 0 },  /* Oc */
                    289:        { NULL, 0 },  /* Bk */
                    290:        { NULL, 0 },  /* Ek */
                    291:        { NULL, 0 },  /* Bt */
                    292:        { NULL, 0 },  /* Hf */
                    293:        { NULL, 0 },  /* Fr */
                    294:        { NULL, 0 },  /* Ud */
                    295:        { NULL, TYPE_Lb },  /* Lb */
                    296:        { NULL, 0 },  /* Lp */
                    297:        { NULL, TYPE_Lk },  /* Lk */
                    298:        { NULL, TYPE_Mt },  /* Mt */
                    299:        { NULL, 0 },  /* Brq */
                    300:        { NULL, 0 },  /* Bro */
                    301:        { NULL, 0 },  /* Brc */
                    302:        { NULL, 0 },  /* %C */
                    303:        { NULL, 0 },  /* Es */
                    304:        { NULL, 0 },  /* En */
                    305:        { NULL, TYPE_Dx },  /* Dx */
                    306:        { NULL, 0 },  /* %Q */
                    307:        { NULL, 0 },  /* br */
                    308:        { NULL, 0 },  /* sp */
                    309:        { NULL, 0 },  /* %U */
                    310:        { NULL, 0 },  /* Ta */
1.1       kristaps  311: };
                    312:
                    313: int
                    314: main(int argc, char *argv[])
                    315: {
1.59      schwarze  316:        int               ch, i;
1.50      kristaps  317:        size_t            j, sz;
1.59      schwarze  318:        const char       *path_arg;
1.50      kristaps  319:        struct mchars    *mc;
                    320:        struct manpaths   dirs;
                    321:        struct mparse    *mp;
1.83      schwarze  322:        struct ohash_info mpages_info, mlinks_info;
1.50      kristaps  323:
                    324:        memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *));
                    325:        memset(&dirs, 0, sizeof(struct manpaths));
                    326:
1.83      schwarze  327:        mpages_info.alloc  = mlinks_info.alloc  = hash_alloc;
                    328:        mpages_info.halloc = mlinks_info.halloc = hash_halloc;
                    329:        mpages_info.hfree  = mlinks_info.hfree  = hash_free;
1.50      kristaps  330:
1.79      schwarze  331:        mpages_info.key_offset = offsetof(struct mpage, inodev);
1.83      schwarze  332:        mlinks_info.key_offset = offsetof(struct mlink, file);
1.1       kristaps  333:
                    334:        progname = strrchr(argv[0], '/');
                    335:        if (progname == NULL)
                    336:                progname = argv[0];
                    337:        else
                    338:                ++progname;
                    339:
1.50      kristaps  340:        /*
                    341:         * We accept a few different invocations.
                    342:         * The CHECKOP macro makes sure that invocation styles don't
                    343:         * clobber each other.
                    344:         */
                    345: #define        CHECKOP(_op, _ch) do \
                    346:        if (OP_DEFAULT != (_op)) { \
                    347:                fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
                    348:                goto usage; \
                    349:        } while (/*CONSTCOND*/0)
1.10      kristaps  350:
1.59      schwarze  351:        path_arg = NULL;
1.38      schwarze  352:        op = OP_DEFAULT;
1.1       kristaps  353:
1.50      kristaps  354:        while (-1 != (ch = getopt(argc, argv, "aC:d:ntu:vW")))
1.1       kristaps  355:                switch (ch) {
1.12      schwarze  356:                case ('a'):
                    357:                        use_all = 1;
                    358:                        break;
1.34      schwarze  359:                case ('C'):
1.50      kristaps  360:                        CHECKOP(op, ch);
1.59      schwarze  361:                        path_arg = optarg;
1.38      schwarze  362:                        op = OP_CONFFILE;
1.34      schwarze  363:                        break;
1.5       kristaps  364:                case ('d'):
1.50      kristaps  365:                        CHECKOP(op, ch);
1.59      schwarze  366:                        path_arg = optarg;
1.5       kristaps  367:                        op = OP_UPDATE;
                    368:                        break;
1.50      kristaps  369:                case ('n'):
                    370:                        nodb = 1;
                    371:                        break;
1.38      schwarze  372:                case ('t'):
1.50      kristaps  373:                        CHECKOP(op, ch);
1.38      schwarze  374:                        dup2(STDOUT_FILENO, STDERR_FILENO);
                    375:                        op = OP_TEST;
1.50      kristaps  376:                        nodb = warnings = 1;
1.38      schwarze  377:                        break;
1.5       kristaps  378:                case ('u'):
1.50      kristaps  379:                        CHECKOP(op, ch);
1.59      schwarze  380:                        path_arg = optarg;
1.5       kristaps  381:                        op = OP_DELETE;
                    382:                        break;
                    383:                case ('v'):
                    384:                        verb++;
                    385:                        break;
1.38      schwarze  386:                case ('W'):
                    387:                        warnings = 1;
                    388:                        break;
1.1       kristaps  389:                default:
1.38      schwarze  390:                        goto usage;
1.1       kristaps  391:                }
                    392:
                    393:        argc -= optind;
                    394:        argv += optind;
                    395:
1.38      schwarze  396:        if (OP_CONFFILE == op && argc > 0) {
1.50      kristaps  397:                fprintf(stderr, "-C: Too many arguments\n");
1.38      schwarze  398:                goto usage;
                    399:        }
                    400:
1.59      schwarze  401:        exitcode = (int)MANDOCLEVEL_OK;
1.50      kristaps  402:        mp = mparse_alloc(MPARSE_AUTO,
                    403:                MANDOCLEVEL_FATAL, NULL, NULL, NULL);
                    404:        mc = mchars_alloc();
                    405:
1.78      schwarze  406:        ohash_init(&mpages, 6, &mpages_info);
1.83      schwarze  407:        ohash_init(&mlinks, 6, &mlinks_info);
1.50      kristaps  408:
                    409:        if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
                    410:                /*
                    411:                 * Force processing all files.
                    412:                 */
                    413:                use_all = 1;
1.59      schwarze  414:
1.50      kristaps  415:                /*
                    416:                 * All of these deal with a specific directory.
                    417:                 * Jump into that directory then collect files specified
                    418:                 * on the command-line.
                    419:                 */
1.59      schwarze  420:                if (0 == set_basedir(path_arg))
1.50      kristaps  421:                        goto out;
                    422:                for (i = 0; i < argc; i++)
1.59      schwarze  423:                        filescan(argv[i]);
                    424:                if (0 == dbopen(1))
1.50      kristaps  425:                        goto out;
                    426:                if (OP_TEST != op)
1.59      schwarze  427:                        dbprune();
1.50      kristaps  428:                if (OP_DELETE != op)
1.78      schwarze  429:                        mpages_merge(mc, mp, 0);
1.59      schwarze  430:                dbclose(1);
1.50      kristaps  431:        } else {
                    432:                /*
                    433:                 * If we have arguments, use them as our manpaths.
                    434:                 * If we don't, grok from manpath(1) or however else
                    435:                 * manpath_parse() wants to do it.
                    436:                 */
                    437:                if (argc > 0) {
                    438:                        dirs.paths = mandoc_calloc
                    439:                                (argc, sizeof(char *));
                    440:                        dirs.sz = (size_t)argc;
                    441:                        for (i = 0; i < argc; i++)
                    442:                                dirs.paths[i] = mandoc_strdup(argv[i]);
                    443:                } else
1.59      schwarze  444:                        manpath_parse(&dirs, path_arg, NULL, NULL);
1.50      kristaps  445:
                    446:                /*
1.69      schwarze  447:                 * First scan the tree rooted at a base directory, then
                    448:                 * build a new database and finally move it into place.
1.50      kristaps  449:                 * Ignore zero-length directories and strip trailing
                    450:                 * slashes.
                    451:                 */
                    452:                for (j = 0; j < dirs.sz; j++) {
                    453:                        sz = strlen(dirs.paths[j]);
                    454:                        if (sz && '/' == dirs.paths[j][sz - 1])
                    455:                                dirs.paths[j][--sz] = '\0';
                    456:                        if (0 == sz)
                    457:                                continue;
1.66      schwarze  458:
                    459:                        if (j) {
1.78      schwarze  460:                                ohash_init(&mpages, 6, &mpages_info);
1.83      schwarze  461:                                ohash_init(&mlinks, 6, &mlinks_info);
1.66      schwarze  462:                        }
                    463:
1.59      schwarze  464:                        if (0 == set_basedir(dirs.paths[j]))
1.50      kristaps  465:                                goto out;
1.59      schwarze  466:                        if (0 == treescan())
1.50      kristaps  467:                                goto out;
1.59      schwarze  468:                        if (0 == set_basedir(dirs.paths[j]))
1.50      kristaps  469:                                goto out;
1.59      schwarze  470:                        if (0 == dbopen(0))
1.50      kristaps  471:                                goto out;
1.52      kristaps  472:
1.78      schwarze  473:                        mpages_merge(mc, mp, warnings && !use_all);
1.59      schwarze  474:                        dbclose(0);
1.66      schwarze  475:
                    476:                        if (j + 1 < dirs.sz) {
1.80      schwarze  477:                                mpages_free();
1.78      schwarze  478:                                ohash_delete(&mpages);
1.83      schwarze  479:                                ohash_delete(&mlinks);
1.66      schwarze  480:                        }
1.50      kristaps  481:                }
                    482:        }
                    483: out:
1.59      schwarze  484:        set_basedir(NULL);
1.50      kristaps  485:        manpath_free(&dirs);
                    486:        mchars_free(mc);
                    487:        mparse_free(mp);
1.80      schwarze  488:        mpages_free();
1.78      schwarze  489:        ohash_delete(&mpages);
1.83      schwarze  490:        ohash_delete(&mlinks);
1.59      schwarze  491:        return(exitcode);
1.50      kristaps  492: usage:
                    493:        fprintf(stderr, "usage: %s [-anvW] [-C file]\n"
                    494:                        "       %s [-anvW] dir ...\n"
                    495:                        "       %s [-nvW] -d dir [file ...]\n"
                    496:                        "       %s [-nvW] -u dir [file ...]\n"
                    497:                        "       %s -t file ...\n",
                    498:                       progname, progname, progname,
                    499:                       progname, progname);
                    500:
1.59      schwarze  501:        return((int)MANDOCLEVEL_BADARG);
1.50      kristaps  502: }
                    503:
                    504: /*
1.59      schwarze  505:  * Scan a directory tree rooted at "basedir" for manpages.
1.50      kristaps  506:  * We use fts(), scanning directory parts along the way for clues to our
                    507:  * section and architecture.
                    508:  *
                    509:  * If use_all has been specified, grok all files.
                    510:  * If not, sanitise paths to the following:
                    511:  *
                    512:  *   [./]man*[/<arch>]/<name>.<section>
                    513:  *   or
                    514:  *   [./]cat<section>[/<arch>]/<name>.0
                    515:  *
                    516:  * TODO: accomodate for multi-language directories.
                    517:  */
                    518: static int
1.59      schwarze  519: treescan(void)
1.50      kristaps  520: {
                    521:        FTS             *f;
                    522:        FTSENT          *ff;
1.84      schwarze  523:        struct mlink    *mlink;
1.50      kristaps  524:        int              dform;
1.84      schwarze  525:        char            *fsec;
1.66      schwarze  526:        const char      *dsec, *arch, *cp, *path;
1.50      kristaps  527:        const char      *argv[2];
                    528:
                    529:        argv[0] = ".";
                    530:        argv[1] = (char *)NULL;
                    531:
                    532:        /*
                    533:         * Walk through all components under the directory, using the
                    534:         * logical descent of files.
                    535:         */
                    536:        f = fts_open((char * const *)argv, FTS_LOGICAL, NULL);
                    537:        if (NULL == f) {
1.59      schwarze  538:                exitcode = (int)MANDOCLEVEL_SYSERR;
                    539:                say("", NULL);
1.50      kristaps  540:                return(0);
                    541:        }
                    542:
                    543:        dsec = arch = NULL;
                    544:        dform = FORM_NONE;
                    545:
                    546:        while (NULL != (ff = fts_read(f))) {
                    547:                path = ff->fts_path + 2;
                    548:                /*
1.83      schwarze  549:                 * If we're a regular file, add an mlink by using the
1.50      kristaps  550:                 * stored directory data and handling the filename.
                    551:                 * Disallow duplicate (hard-linked) files.
                    552:                 */
                    553:                if (FTS_F == ff->fts_info) {
1.60      schwarze  554:                        if (0 == strcmp(path, MANDOC_DB))
                    555:                                continue;
1.50      kristaps  556:                        if ( ! use_all && ff->fts_level < 2) {
1.56      schwarze  557:                                if (warnings)
1.59      schwarze  558:                                        say(path, "Extraneous file");
1.50      kristaps  559:                                continue;
1.84      schwarze  560:                        } else if (NULL == (fsec =
1.60      schwarze  561:                                        strrchr(ff->fts_name, '.'))) {
                    562:                                if ( ! use_all) {
1.56      schwarze  563:                                        if (warnings)
1.60      schwarze  564:                                                say(path,
                    565:                                                    "No filename suffix");
1.50      kristaps  566:                                        continue;
                    567:                                }
1.84      schwarze  568:                        } else if (0 == strcmp(++fsec, "html")) {
1.60      schwarze  569:                                if (warnings)
                    570:                                        say(path, "Skip html");
                    571:                                continue;
1.84      schwarze  572:                        } else if (0 == strcmp(fsec, "gz")) {
1.60      schwarze  573:                                if (warnings)
                    574:                                        say(path, "Skip gz");
                    575:                                continue;
1.84      schwarze  576:                        } else if (0 == strcmp(fsec, "ps")) {
1.60      schwarze  577:                                if (warnings)
                    578:                                        say(path, "Skip ps");
                    579:                                continue;
1.84      schwarze  580:                        } else if (0 == strcmp(fsec, "pdf")) {
1.60      schwarze  581:                                if (warnings)
                    582:                                        say(path, "Skip pdf");
                    583:                                continue;
                    584:                        } else if ( ! use_all &&
1.84      schwarze  585:                            ((FORM_SRC == dform && strcmp(fsec, dsec)) ||
                    586:                             (FORM_CAT == dform && strcmp(fsec, "0")))) {
1.60      schwarze  587:                                if (warnings)
                    588:                                        say(path, "Wrong filename suffix");
                    589:                                continue;
1.66      schwarze  590:                        } else
1.84      schwarze  591:                                fsec[-1] = '\0';
                    592:                        mlink = mandoc_calloc(1, sizeof(struct mlink));
                    593:                        strlcpy(mlink->file, path, sizeof(mlink->file));
                    594:                        mlink->dform = dform;
                    595:                        if (NULL != dsec)
                    596:                                mlink->dsec = mandoc_strdup(dsec);
                    597:                        if (NULL != arch)
                    598:                                mlink->arch = mandoc_strdup(arch);
                    599:                        mlink->name = mandoc_strdup(ff->fts_name);
                    600:                        if (NULL != fsec)
                    601:                                mlink->fsec = mandoc_strdup(fsec);
                    602:                        mlink_add(mlink, ff->fts_statp);
1.50      kristaps  603:                        continue;
1.84      schwarze  604:                } else if (FTS_D != ff->fts_info &&
1.60      schwarze  605:                                FTS_DP != ff->fts_info) {
                    606:                        if (warnings)
                    607:                                say(path, "Not a regular file");
1.50      kristaps  608:                        continue;
1.60      schwarze  609:                }
1.1       kristaps  610:
1.50      kristaps  611:                switch (ff->fts_level) {
                    612:                case (0):
                    613:                        /* Ignore the root directory. */
                    614:                        break;
                    615:                case (1):
                    616:                        /*
                    617:                         * This might contain manX/ or catX/.
                    618:                         * Try to infer this from the name.
                    619:                         * If we're not in use_all, enforce it.
                    620:                         */
                    621:                        dsec = NULL;
                    622:                        dform = FORM_NONE;
                    623:                        cp = ff->fts_name;
                    624:                        if (FTS_DP == ff->fts_info)
                    625:                                break;
1.1       kristaps  626:
1.50      kristaps  627:                        if (0 == strncmp(cp, "man", 3)) {
                    628:                                dform = FORM_SRC;
1.66      schwarze  629:                                dsec = cp + 3;
1.50      kristaps  630:                        } else if (0 == strncmp(cp, "cat", 3)) {
                    631:                                dform = FORM_CAT;
1.66      schwarze  632:                                dsec = cp + 3;
1.50      kristaps  633:                        }
1.1       kristaps  634:
1.50      kristaps  635:                        if (NULL != dsec || use_all)
                    636:                                break;
1.1       kristaps  637:
1.56      schwarze  638:                        if (warnings)
1.59      schwarze  639:                                say(path, "Unknown directory part");
1.50      kristaps  640:                        fts_set(f, ff, FTS_SKIP);
                    641:                        break;
                    642:                case (2):
                    643:                        /*
                    644:                         * Possibly our architecture.
                    645:                         * If we're descending, keep tabs on it.
                    646:                         */
                    647:                        arch = NULL;
                    648:                        if (FTS_DP != ff->fts_info && NULL != dsec)
1.66      schwarze  649:                                arch = ff->fts_name;
1.50      kristaps  650:                        break;
                    651:                default:
                    652:                        if (FTS_DP == ff->fts_info || use_all)
                    653:                                break;
1.56      schwarze  654:                        if (warnings)
1.59      schwarze  655:                                say(path, "Extraneous directory part");
1.50      kristaps  656:                        fts_set(f, ff, FTS_SKIP);
                    657:                        break;
1.5       kristaps  658:                }
1.50      kristaps  659:        }
                    660:
                    661:        fts_close(f);
                    662:        return(1);
                    663: }
1.5       kristaps  664:
1.50      kristaps  665: /*
                    666:  * Add a file to the file vector.
                    667:  * Do not verify that it's a "valid" looking manpage (we'll do that
                    668:  * later).
                    669:  *
                    670:  * Try to infer the manual section, architecture, and page name from the
                    671:  * path, assuming it looks like
                    672:  *
                    673:  *   [./]man*[/<arch>]/<name>.<section>
                    674:  *   or
                    675:  *   [./]cat<section>[/<arch>]/<name>.0
                    676:  *
1.83      schwarze  677:  * Stuff this information directly into the mlink vector.
1.50      kristaps  678:  * See treescan() for the fts(3) version of this.
                    679:  */
                    680: static void
1.59      schwarze  681: filescan(const char *file)
1.50      kristaps  682: {
1.59      schwarze  683:        char             buf[PATH_MAX];
1.84      schwarze  684:        struct stat      st;
                    685:        struct mlink    *mlink;
1.59      schwarze  686:        char            *p, *start;
1.5       kristaps  687:
1.50      kristaps  688:        assert(use_all);
1.5       kristaps  689:
1.50      kristaps  690:        if (0 == strncmp(file, "./", 2))
                    691:                file += 2;
1.5       kristaps  692:
1.59      schwarze  693:        if (NULL == realpath(file, buf)) {
                    694:                exitcode = (int)MANDOCLEVEL_BADARG;
                    695:                say(file, NULL);
                    696:                return;
1.72      schwarze  697:        } else if (OP_TEST != op && strstr(buf, basedir) != buf) {
1.59      schwarze  698:                exitcode = (int)MANDOCLEVEL_BADARG;
                    699:                say("", "%s: outside base directory", buf);
                    700:                return;
                    701:        } else if (-1 == stat(buf, &st)) {
                    702:                exitcode = (int)MANDOCLEVEL_BADARG;
                    703:                say(file, NULL);
1.50      kristaps  704:                return;
                    705:        } else if ( ! (S_IFREG & st.st_mode)) {
1.59      schwarze  706:                exitcode = (int)MANDOCLEVEL_BADARG;
                    707:                say(file, "Not a regular file");
1.50      kristaps  708:                return;
                    709:        }
1.59      schwarze  710:        start = buf + strlen(basedir);
1.84      schwarze  711:        mlink = mandoc_calloc(1, sizeof(struct mlink));
                    712:        strlcpy(mlink->file, start, sizeof(mlink->file));
1.17      schwarze  713:
1.50      kristaps  714:        /*
                    715:         * First try to guess our directory structure.
                    716:         * If we find a separator, try to look for man* or cat*.
                    717:         * If we find one of these and what's underneath is a directory,
                    718:         * assume it's an architecture.
                    719:         */
                    720:        if (NULL != (p = strchr(start, '/'))) {
                    721:                *p++ = '\0';
                    722:                if (0 == strncmp(start, "man", 3)) {
1.84      schwarze  723:                        mlink->dform = FORM_SRC;
                    724:                        mlink->dsec = mandoc_strdup(start + 3);
1.50      kristaps  725:                } else if (0 == strncmp(start, "cat", 3)) {
1.84      schwarze  726:                        mlink->dform = FORM_CAT;
                    727:                        mlink->dsec = mandoc_strdup(start + 3);
1.17      schwarze  728:                }
1.5       kristaps  729:
1.50      kristaps  730:                start = p;
1.84      schwarze  731:                if (NULL != mlink->dsec && NULL != (p = strchr(start, '/'))) {
1.50      kristaps  732:                        *p++ = '\0';
1.84      schwarze  733:                        mlink->arch = mandoc_strdup(start);
1.50      kristaps  734:                        start = p;
1.84      schwarze  735:                }
1.50      kristaps  736:        }
                    737:
                    738:        /*
                    739:         * Now check the file suffix.
                    740:         * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
                    741:         */
                    742:        p = strrchr(start, '\0');
                    743:        while (p-- > start && '/' != *p && '.' != *p)
                    744:                /* Loop. */ ;
                    745:
                    746:        if ('.' == *p) {
                    747:                *p++ = '\0';
1.84      schwarze  748:                mlink->fsec = mandoc_strdup(p);
1.5       kristaps  749:        }
                    750:
1.10      kristaps  751:        /*
1.50      kristaps  752:         * Now try to parse the name.
                    753:         * Use the filename portion of the path.
1.10      kristaps  754:         */
1.84      schwarze  755:        mlink->name = start;
1.50      kristaps  756:        if (NULL != (p = strrchr(start, '/'))) {
1.84      schwarze  757:                mlink->name = p + 1;
1.50      kristaps  758:                *p = '\0';
1.84      schwarze  759:        }
                    760:        mlink->name = mandoc_strdup(mlink->name);
1.50      kristaps  761:
1.84      schwarze  762:        mlink_add(mlink, &st);
1.50      kristaps  763: }
1.5       kristaps  764:
1.50      kristaps  765: static void
1.84      schwarze  766: mlink_add(struct mlink *mlink, const struct stat *st)
1.50      kristaps  767: {
1.83      schwarze  768:        struct inodev    inodev;
1.78      schwarze  769:        struct mpage    *mpage;
1.83      schwarze  770:        unsigned int     slot;
1.50      kristaps  771:
1.84      schwarze  772:        assert(NULL != mlink->file);
1.50      kristaps  773:
1.84      schwarze  774:        if (NULL == mlink->dsec)
                    775:                mlink->dsec = mandoc_strdup("");
                    776:        if (NULL == mlink->arch)
                    777:                mlink->arch = mandoc_strdup("");
                    778:        if (NULL == mlink->name)
                    779:                mlink->name = mandoc_strdup("");
                    780:        if (NULL == mlink->fsec)
                    781:                mlink->fsec = mandoc_strdup("");
                    782:
                    783:        if ('0' == *mlink->fsec) {
                    784:                free(mlink->fsec);
                    785:                mlink->fsec = mandoc_strdup(mlink->dsec);
                    786:                mlink->fform = FORM_CAT;
                    787:        } else if ('1' <= *mlink->fsec && '9' >= *mlink->fsec)
                    788:                mlink->fform = FORM_SRC;
1.74      schwarze  789:        else
1.84      schwarze  790:                mlink->fform = FORM_NONE;
1.82      schwarze  791:
1.83      schwarze  792:        slot = ohash_qlookup(&mlinks, mlink->file);
                    793:        assert(NULL == ohash_find(&mlinks, slot));
                    794:        ohash_insert(&mlinks, slot, mlink);
                    795:
                    796:        inodev.st_ino = st->st_ino;
                    797:        inodev.st_dev = st->st_dev;
                    798:        slot = ohash_lookup_memory(&mpages, (char *)&inodev,
                    799:            sizeof(struct inodev), inodev.st_ino);
                    800:        mpage = ohash_find(&mpages, slot);
                    801:        if (NULL == mpage) {
                    802:                mpage = mandoc_calloc(1, sizeof(struct mpage));
                    803:                mpage->inodev.st_ino = inodev.st_ino;
                    804:                mpage->inodev.st_dev = inodev.st_dev;
                    805:                ohash_insert(&mpages, slot, mpage);
                    806:        } else
1.85      schwarze  807:                mlink->next = mpage->mlinks;
1.82      schwarze  808:        mpage->mlinks = mlink;
                    809: }
                    810:
                    811: static void
                    812: mlink_free(struct mlink *mlink)
                    813: {
                    814:
                    815:        free(mlink->dsec);
                    816:        free(mlink->arch);
                    817:        free(mlink->name);
                    818:        free(mlink->fsec);
                    819:        free(mlink);
1.50      kristaps  820: }
1.3       kristaps  821:
1.50      kristaps  822: static void
1.78      schwarze  823: mpages_free(void)
1.50      kristaps  824: {
1.78      schwarze  825:        struct mpage    *mpage;
1.82      schwarze  826:        struct mlink    *mlink;
1.80      schwarze  827:        unsigned int     slot;
1.3       kristaps  828:
1.80      schwarze  829:        mpage = ohash_first(&mpages, &slot);
                    830:        while (NULL != mpage) {
1.82      schwarze  831:                while (NULL != (mlink = mpage->mlinks)) {
1.85      schwarze  832:                        mpage->mlinks = mlink->next;
1.82      schwarze  833:                        mlink_free(mlink);
                    834:                }
1.78      schwarze  835:                free(mpage->sec);
                    836:                free(mpage->arch);
1.82      schwarze  837:                free(mpage->title);
                    838:                free(mpage->desc);
1.78      schwarze  839:                free(mpage);
1.80      schwarze  840:                mpage = ohash_next(&mpages, &slot);
1.50      kristaps  841:        }
                    842: }
1.38      schwarze  843:
1.50      kristaps  844: /*
1.80      schwarze  845:  * Run through the files in the global vector "mpages"
                    846:  * and add them to the database specified in "basedir".
1.50      kristaps  847:  *
                    848:  * This handles the parsing scheme itself, using the cues of directory
                    849:  * and filename to determine whether the file is parsable or not.
                    850:  */
1.59      schwarze  851: static void
1.78      schwarze  852: mpages_merge(struct mchars *mc, struct mparse *mp, int check_reachable)
1.50      kristaps  853: {
1.69      schwarze  854:        struct ohash             title_table;
1.71      schwarze  855:        struct ohash_info        title_info, str_info;
1.69      schwarze  856:        char                     buf[PATH_MAX];
1.78      schwarze  857:        struct mpage            *mpage;
1.69      schwarze  858:        struct mdoc             *mdoc;
                    859:        struct man              *man;
                    860:        struct title            *title_entry;
                    861:        char                    *bufp, *title_str;
1.82      schwarze  862:        const char              *cp;
1.69      schwarze  863:        size_t                   sz;
                    864:        int                      match;
1.80      schwarze  865:        unsigned int             pslot, tslot;
1.69      schwarze  866:        enum mandoclevel         lvl;
                    867:
1.71      schwarze  868:        str_info.alloc = hash_alloc;
                    869:        str_info.halloc = hash_halloc;
                    870:        str_info.hfree = hash_free;
                    871:        str_info.key_offset = offsetof(struct str, key);
                    872:
1.69      schwarze  873:        if (check_reachable) {
                    874:                title_info.alloc = hash_alloc;
                    875:                title_info.halloc = hash_halloc;
                    876:                title_info.hfree = hash_free;
                    877:                title_info.key_offset = offsetof(struct title, title);
                    878:                ohash_init(&title_table, 6, &title_info);
                    879:        }
1.50      kristaps  880:
1.80      schwarze  881:        mpage = ohash_first(&mpages, &pslot);
                    882:        while (NULL != mpage) {
1.50      kristaps  883:                /*
                    884:                 * If we're a catpage (as defined by our path), then see
                    885:                 * if a manpage exists by the same name (ignoring the
                    886:                 * suffix).
                    887:                 * If it does, then we want to use it instead of our
                    888:                 * own.
                    889:                 */
1.82      schwarze  890:                if ( ! use_all && FORM_CAT == mpage->mlinks->dform) {
                    891:                        sz = strlcpy(buf, mpage->mlinks->file, PATH_MAX);
1.58      schwarze  892:                        if (sz >= PATH_MAX) {
1.56      schwarze  893:                                if (warnings)
1.82      schwarze  894:                                        say(mpage->mlinks->file,
1.78      schwarze  895:                                            "Filename too long");
1.80      schwarze  896:                                mpage = ohash_next(&mpages, &pslot);
1.50      kristaps  897:                                continue;
                    898:                        }
                    899:                        bufp = strstr(buf, "cat");
                    900:                        assert(NULL != bufp);
                    901:                        memcpy(bufp, "man", 3);
                    902:                        if (NULL != (bufp = strrchr(buf, '.')))
                    903:                                *++bufp = '\0';
1.82      schwarze  904:                        strlcat(buf, mpage->mlinks->dsec, PATH_MAX);
1.83      schwarze  905:                        if (NULL != ohash_find(&mlinks,
                    906:                                        ohash_qlookup(&mlinks, buf))) {
1.56      schwarze  907:                                if (warnings)
1.82      schwarze  908:                                        say(mpage->mlinks->file, "Man "
1.56      schwarze  909:                                            "source exists: %s", buf);
1.80      schwarze  910:                                mpage = ohash_next(&mpages, &pslot);
1.50      kristaps  911:                                continue;
                    912:                        }
                    913:                }
                    914:
1.71      schwarze  915:                ohash_init(&strings, 6, &str_info);
1.50      kristaps  916:                mparse_reset(mp);
                    917:                mdoc = NULL;
                    918:                man = NULL;
1.69      schwarze  919:                match = 1;
1.14      schwarze  920:
                    921:                /*
1.33      schwarze  922:                 * Try interpreting the file as mdoc(7) or man(7)
                    923:                 * source code, unless it is already known to be
                    924:                 * formatted.  Fall back to formatted mode.
1.14      schwarze  925:                 */
1.82      schwarze  926:                if (FORM_CAT != mpage->mlinks->dform ||
1.84      schwarze  927:                    FORM_CAT != mpage->mlinks->fform) {
1.82      schwarze  928:                        lvl = mparse_readfd(mp, -1, mpage->mlinks->file);
1.50      kristaps  929:                        if (lvl < MANDOCLEVEL_FATAL)
                    930:                                mparse_result(mp, &mdoc, &man);
1.76      schwarze  931:                }
1.14      schwarze  932:
                    933:                if (NULL != mdoc) {
1.82      schwarze  934:                        mpage->form = FORM_SRC;
                    935:                        mpage->sec =
                    936:                            mandoc_strdup(mdoc_meta(mdoc)->msec);
                    937:                        mpage->arch = mdoc_meta(mdoc)->arch;
1.84      schwarze  938:                        mpage->arch = mandoc_strdup(
                    939:                            NULL == mpage->arch ? "" : mpage->arch);
1.82      schwarze  940:                        mpage->title =
                    941:                            mandoc_strdup(mdoc_meta(mdoc)->title);
1.14      schwarze  942:                } else if (NULL != man) {
1.82      schwarze  943:                        mpage->form = FORM_SRC;
                    944:                        mpage->sec =
                    945:                            mandoc_strdup(man_meta(man)->msec);
                    946:                        mpage->arch =
                    947:                            mandoc_strdup(mpage->mlinks->arch);
                    948:                        mpage->title =
                    949:                            mandoc_strdup(man_meta(man)->title);
1.76      schwarze  950:                } else {
1.82      schwarze  951:                        mpage->form = FORM_CAT;
                    952:                        mpage->sec =
                    953:                            mandoc_strdup(mpage->mlinks->dsec);
                    954:                        mpage->arch =
                    955:                            mandoc_strdup(mpage->mlinks->arch);
                    956:                        mpage->title =
                    957:                            mandoc_strdup(mpage->mlinks->name);
1.76      schwarze  958:                }
1.50      kristaps  959:
1.12      schwarze  960:                /*
1.44      kristaps  961:                 * Check whether the manual section given in a file
                    962:                 * agrees with the directory where the file is located.
                    963:                 * Some manuals have suffixes like (3p) on their
                    964:                 * section number either inside the file or in the
                    965:                 * directory name, some are linked into more than one
                    966:                 * section, like encrypt(1) = makekey(8).  Do not skip
                    967:                 * manuals for such reasons.
1.12      schwarze  968:                 */
1.82      schwarze  969:                if (warnings && !use_all && FORM_SRC == mpage->form &&
                    970:                    strcasecmp(mpage->sec, mpage->mlinks->dsec)) {
1.69      schwarze  971:                        match = 0;
1.82      schwarze  972:                        say(mpage->mlinks->file, "Section \"%s\" "
1.78      schwarze  973:                                "manual in %s directory",
1.82      schwarze  974:                                mpage->sec, mpage->mlinks->dsec);
1.69      schwarze  975:                }
1.12      schwarze  976:
1.42      schwarze  977:                /*
                    978:                 * Manual page directories exist for each kernel
                    979:                 * architecture as returned by machine(1).
                    980:                 * However, many manuals only depend on the
                    981:                 * application architecture as returned by arch(1).
                    982:                 * For example, some (2/ARM) manuals are shared
                    983:                 * across the "armish" and "zaurus" kernel
                    984:                 * architectures.
                    985:                 * A few manuals are even shared across completely
                    986:                 * different architectures, for example fdformat(1)
                    987:                 * on amd64, i386, sparc, and sparc64.
                    988:                 * Thus, warn about architecture mismatches,
                    989:                 * but don't skip manuals for this reason.
                    990:                 */
1.82      schwarze  991:                if (warnings && !use_all &&
                    992:                    strcasecmp(mpage->arch, mpage->mlinks->arch)) {
1.69      schwarze  993:                        match = 0;
1.82      schwarze  994:                        say(mpage->mlinks->file, "Architecture \"%s\" "
1.47      schwarze  995:                                "manual in \"%s\" directory",
1.82      schwarze  996:                                mpage->arch, mpage->mlinks->arch);
1.69      schwarze  997:                }
1.82      schwarze  998:                if (warnings && !use_all &&
                    999:                    strcasecmp(mpage->title, mpage->mlinks->name))
1.69      schwarze 1000:                        match = 0;
1.12      schwarze 1001:
1.82      schwarze 1002:                putkey(mpage, mpage->mlinks->name, TYPE_Nm);
1.12      schwarze 1003:
1.50      kristaps 1004:                if (NULL != mdoc) {
                   1005:                        if (NULL != (cp = mdoc_meta(mdoc)->name))
1.78      schwarze 1006:                                putkey(mpage, cp, TYPE_Nm);
                   1007:                        assert(NULL == mpage->desc);
                   1008:                        parse_mdoc(mpage, mdoc_node(mdoc));
                   1009:                        putkey(mpage, NULL != mpage->desc ?
1.82      schwarze 1010:                            mpage->desc : mpage->mlinks->name, TYPE_Nd);
1.50      kristaps 1011:                } else if (NULL != man)
1.78      schwarze 1012:                        parse_man(mpage, man_node(man));
1.50      kristaps 1013:                else
1.78      schwarze 1014:                        parse_cat(mpage);
1.44      kristaps 1015:
1.69      schwarze 1016:                /*
                   1017:                 * Build a title string for the file.  If it matches
                   1018:                 * the location of the file, remember the title as
                   1019:                 * found; else, remember it as missing.
                   1020:                 */
                   1021:
                   1022:                if (check_reachable) {
1.82      schwarze 1023:                        if (-1 == asprintf(&title_str, "%s(%s%s%s)",
                   1024:                            mpage->title, mpage->sec,
                   1025:                            '\0' == *mpage->arch ? "" : "/",
                   1026:                            mpage->arch)) {
1.69      schwarze 1027:                                perror(NULL);
                   1028:                                exit((int)MANDOCLEVEL_SYSERR);
                   1029:                        }
1.80      schwarze 1030:                        tslot = ohash_qlookup(&title_table, title_str);
                   1031:                        title_entry = ohash_find(&title_table, tslot);
1.69      schwarze 1032:                        if (NULL == title_entry) {
                   1033:                                title_entry = mandoc_malloc(
                   1034:                                                sizeof(struct title));
                   1035:                                title_entry->title = title_str;
                   1036:                                title_entry->file = mandoc_strdup(
1.82      schwarze 1037:                                    match ? "" : mpage->mlinks->file);
1.80      schwarze 1038:                                ohash_insert(&title_table, tslot,
1.69      schwarze 1039:                                                title_entry);
                   1040:                        } else {
                   1041:                                if (match)
                   1042:                                        *title_entry->file = '\0';
                   1043:                                free(title_str);
                   1044:                        }
                   1045:                }
                   1046:
1.82      schwarze 1047:                dbindex(mpage, mc);
1.66      schwarze 1048:                ohash_delete(&strings);
1.80      schwarze 1049:                mpage = ohash_next(&mpages, &pslot);
1.69      schwarze 1050:        }
                   1051:
                   1052:        if (check_reachable) {
1.80      schwarze 1053:                title_entry = ohash_first(&title_table, &tslot);
1.69      schwarze 1054:                while (NULL != title_entry) {
                   1055:                        if ('\0' != *title_entry->file)
                   1056:                                say(title_entry->file,
                   1057:                                    "Probably unreachable, title is %s",
                   1058:                                    title_entry->title);
                   1059:                        free(title_entry->title);
                   1060:                        free(title_entry->file);
                   1061:                        free(title_entry);
1.80      schwarze 1062:                        title_entry = ohash_next(&title_table, &tslot);
1.69      schwarze 1063:                }
                   1064:                ohash_delete(&title_table);
1.50      kristaps 1065:        }
                   1066: }
1.12      schwarze 1067:
1.50      kristaps 1068: static void
1.78      schwarze 1069: parse_cat(struct mpage *mpage)
1.50      kristaps 1070: {
                   1071:        FILE            *stream;
                   1072:        char            *line, *p, *title;
                   1073:        size_t           len, plen, titlesz;
1.12      schwarze 1074:
1.82      schwarze 1075:        if (NULL == (stream = fopen(mpage->mlinks->file, "r"))) {
1.56      schwarze 1076:                if (warnings)
1.82      schwarze 1077:                        say(mpage->mlinks->file, NULL);
1.50      kristaps 1078:                return;
                   1079:        }
1.1       kristaps 1080:
1.50      kristaps 1081:        /* Skip to first blank line. */
1.1       kristaps 1082:
1.50      kristaps 1083:        while (NULL != (line = fgetln(stream, &len)))
                   1084:                if ('\n' == *line)
                   1085:                        break;
1.1       kristaps 1086:
1.50      kristaps 1087:        /*
                   1088:         * Assume the first line that is not indented
                   1089:         * is the first section header.  Skip to it.
                   1090:         */
1.1       kristaps 1091:
1.50      kristaps 1092:        while (NULL != (line = fgetln(stream, &len)))
                   1093:                if ('\n' != *line && ' ' != *line)
                   1094:                        break;
                   1095:
                   1096:        /*
                   1097:         * Read up until the next section into a buffer.
                   1098:         * Strip the leading and trailing newline from each read line,
                   1099:         * appending a trailing space.
                   1100:         * Ignore empty (whitespace-only) lines.
                   1101:         */
1.1       kristaps 1102:
1.50      kristaps 1103:        titlesz = 0;
                   1104:        title = NULL;
1.38      schwarze 1105:
1.50      kristaps 1106:        while (NULL != (line = fgetln(stream, &len))) {
                   1107:                if (' ' != *line || '\n' != line[len - 1])
                   1108:                        break;
                   1109:                while (len > 0 && isspace((unsigned char)*line)) {
                   1110:                        line++;
                   1111:                        len--;
                   1112:                }
                   1113:                if (1 == len)
1.38      schwarze 1114:                        continue;
1.50      kristaps 1115:                title = mandoc_realloc(title, titlesz + len);
                   1116:                memcpy(title + titlesz, line, len);
                   1117:                titlesz += len;
                   1118:                title[titlesz - 1] = ' ';
                   1119:        }
1.38      schwarze 1120:
1.50      kristaps 1121:        /*
                   1122:         * If no page content can be found, or the input line
                   1123:         * is already the next section header, or there is no
                   1124:         * trailing newline, reuse the page title as the page
                   1125:         * description.
                   1126:         */
1.44      kristaps 1127:
1.50      kristaps 1128:        if (NULL == title || '\0' == *title) {
1.56      schwarze 1129:                if (warnings)
1.82      schwarze 1130:                        say(mpage->mlinks->file,
                   1131:                            "Cannot find NAME section");
1.78      schwarze 1132:                assert(NULL == mpage->desc);
1.82      schwarze 1133:                mpage->desc = mandoc_strdup(mpage->mlinks->name);
                   1134:                putkey(mpage, mpage->mlinks->name, TYPE_Nd);
1.50      kristaps 1135:                fclose(stream);
                   1136:                free(title);
                   1137:                return;
                   1138:        }
1.1       kristaps 1139:
1.50      kristaps 1140:        title = mandoc_realloc(title, titlesz + 1);
                   1141:        title[titlesz] = '\0';
1.33      schwarze 1142:
1.50      kristaps 1143:        /*
                   1144:         * Skip to the first dash.
                   1145:         * Use the remaining line as the description (no more than 70
                   1146:         * bytes).
                   1147:         */
1.33      schwarze 1148:
1.50      kristaps 1149:        if (NULL != (p = strstr(title, "- "))) {
                   1150:                for (p += 2; ' ' == *p || '\b' == *p; p++)
                   1151:                        /* Skip to next word. */ ;
                   1152:        } else {
1.56      schwarze 1153:                if (warnings)
1.82      schwarze 1154:                        say(mpage->mlinks->file,
                   1155:                            "No dash in title line");
1.50      kristaps 1156:                p = title;
                   1157:        }
1.38      schwarze 1158:
1.50      kristaps 1159:        plen = strlen(p);
1.1       kristaps 1160:
1.50      kristaps 1161:        /* Strip backspace-encoding from line. */
1.1       kristaps 1162:
1.50      kristaps 1163:        while (NULL != (line = memchr(p, '\b', plen))) {
                   1164:                len = line - p;
                   1165:                if (0 == len) {
                   1166:                        memmove(line, line + 1, plen--);
                   1167:                        continue;
                   1168:                }
                   1169:                memmove(line - 1, line + 1, plen - len);
                   1170:                plen -= 2;
                   1171:        }
1.1       kristaps 1172:
1.78      schwarze 1173:        assert(NULL == mpage->desc);
                   1174:        mpage->desc = mandoc_strdup(p);
                   1175:        putkey(mpage, mpage->desc, TYPE_Nd);
1.50      kristaps 1176:        fclose(stream);
                   1177:        free(title);
                   1178: }
1.1       kristaps 1179:
1.50      kristaps 1180: /*
                   1181:  * Put a type/word pair into the word database for this particular file.
                   1182:  */
                   1183: static void
1.78      schwarze 1184: putkey(const struct mpage *mpage, const char *value, uint64_t type)
1.50      kristaps 1185: {
1.18      kristaps 1186:
1.50      kristaps 1187:        assert(NULL != value);
1.78      schwarze 1188:        putkeys(mpage, value, strlen(value), type);
1.3       kristaps 1189: }
                   1190:
                   1191: /*
1.50      kristaps 1192:  * Grok all nodes at or below a certain mdoc node into putkey().
1.3       kristaps 1193:  */
                   1194: static void
1.78      schwarze 1195: putmdockey(const struct mpage *mpage,
                   1196:        const struct mdoc_node *n, uint64_t m)
1.3       kristaps 1197: {
1.18      kristaps 1198:
1.50      kristaps 1199:        for ( ; NULL != n; n = n->next) {
                   1200:                if (NULL != n->child)
1.78      schwarze 1201:                        putmdockey(mpage, n->child, m);
1.50      kristaps 1202:                if (MDOC_TEXT == n->type)
1.78      schwarze 1203:                        putkey(mpage, n->string, m);
1.50      kristaps 1204:        }
                   1205: }
1.18      kristaps 1206:
1.61      schwarze 1207: static void
1.78      schwarze 1208: parse_man(struct mpage *mpage, const struct man_node *n)
1.50      kristaps 1209: {
                   1210:        const struct man_node *head, *body;
                   1211:        char            *start, *sv, *title;
                   1212:        char             byte;
                   1213:        size_t           sz, titlesz;
1.18      kristaps 1214:
1.50      kristaps 1215:        if (NULL == n)
1.61      schwarze 1216:                return;
1.18      kristaps 1217:
1.50      kristaps 1218:        /*
                   1219:         * We're only searching for one thing: the first text child in
                   1220:         * the BODY of a NAME section.  Since we don't keep track of
                   1221:         * sections in -man, run some hoops to find out whether we're in
                   1222:         * the correct section or not.
                   1223:         */
1.18      kristaps 1224:
1.50      kristaps 1225:        if (MAN_BODY == n->type && MAN_SH == n->tok) {
                   1226:                body = n;
                   1227:                assert(body->parent);
                   1228:                if (NULL != (head = body->parent->head) &&
                   1229:                                1 == head->nchild &&
                   1230:                                NULL != (head = (head->child)) &&
                   1231:                                MAN_TEXT == head->type &&
                   1232:                                0 == strcmp(head->string, "NAME") &&
                   1233:                                NULL != (body = body->child) &&
                   1234:                                MAN_TEXT == body->type) {
1.3       kristaps 1235:
1.50      kristaps 1236:                        title = NULL;
                   1237:                        titlesz = 0;
1.3       kristaps 1238:
1.50      kristaps 1239:                        /*
                   1240:                         * Suck the entire NAME section into memory.
                   1241:                         * Yes, we might run away.
                   1242:                         * But too many manuals have big, spread-out
                   1243:                         * NAME sections over many lines.
                   1244:                         */
1.3       kristaps 1245:
1.50      kristaps 1246:                        for ( ; NULL != body; body = body->next) {
                   1247:                                if (MAN_TEXT != body->type)
                   1248:                                        break;
                   1249:                                if (0 == (sz = strlen(body->string)))
                   1250:                                        continue;
                   1251:                                title = mandoc_realloc
                   1252:                                        (title, titlesz + sz + 1);
                   1253:                                memcpy(title + titlesz, body->string, sz);
                   1254:                                titlesz += sz + 1;
                   1255:                                title[titlesz - 1] = ' ';
                   1256:                        }
                   1257:                        if (NULL == title)
1.61      schwarze 1258:                                return;
1.18      kristaps 1259:
1.50      kristaps 1260:                        title = mandoc_realloc(title, titlesz + 1);
                   1261:                        title[titlesz] = '\0';
1.18      kristaps 1262:
1.50      kristaps 1263:                        /* Skip leading space.  */
1.18      kristaps 1264:
1.50      kristaps 1265:                        sv = title;
                   1266:                        while (isspace((unsigned char)*sv))
                   1267:                                sv++;
1.18      kristaps 1268:
1.50      kristaps 1269:                        if (0 == (sz = strlen(sv))) {
                   1270:                                free(title);
1.61      schwarze 1271:                                return;
1.50      kristaps 1272:                        }
1.1       kristaps 1273:
1.50      kristaps 1274:                        /* Erase trailing space. */
1.1       kristaps 1275:
1.50      kristaps 1276:                        start = &sv[sz - 1];
                   1277:                        while (start > sv && isspace((unsigned char)*start))
                   1278:                                *start-- = '\0';
1.1       kristaps 1279:
1.50      kristaps 1280:                        if (start == sv) {
                   1281:                                free(title);
1.61      schwarze 1282:                                return;
1.50      kristaps 1283:                        }
1.1       kristaps 1284:
1.50      kristaps 1285:                        start = sv;
1.18      kristaps 1286:
1.50      kristaps 1287:                        /*
                   1288:                         * Go through a special heuristic dance here.
                   1289:                         * Conventionally, one or more manual names are
                   1290:                         * comma-specified prior to a whitespace, then a
                   1291:                         * dash, then a description.  Try to puzzle out
                   1292:                         * the name parts here.
                   1293:                         */
1.18      kristaps 1294:
1.50      kristaps 1295:                        for ( ;; ) {
                   1296:                                sz = strcspn(start, " ,");
                   1297:                                if ('\0' == start[sz])
                   1298:                                        break;
1.1       kristaps 1299:
1.50      kristaps 1300:                                byte = start[sz];
                   1301:                                start[sz] = '\0';
1.1       kristaps 1302:
1.78      schwarze 1303:                                putkey(mpage, start, TYPE_Nm);
1.1       kristaps 1304:
1.50      kristaps 1305:                                if (' ' == byte) {
                   1306:                                        start += sz + 1;
                   1307:                                        break;
                   1308:                                }
1.1       kristaps 1309:
1.50      kristaps 1310:                                assert(',' == byte);
                   1311:                                start += sz + 1;
                   1312:                                while (' ' == *start)
                   1313:                                        start++;
                   1314:                        }
1.1       kristaps 1315:
1.50      kristaps 1316:                        if (sv == start) {
1.78      schwarze 1317:                                putkey(mpage, start, TYPE_Nm);
1.50      kristaps 1318:                                free(title);
1.61      schwarze 1319:                                return;
1.50      kristaps 1320:                        }
1.1       kristaps 1321:
1.50      kristaps 1322:                        while (isspace((unsigned char)*start))
                   1323:                                start++;
1.1       kristaps 1324:
1.50      kristaps 1325:                        if (0 == strncmp(start, "-", 1))
                   1326:                                start += 1;
                   1327:                        else if (0 == strncmp(start, "\\-\\-", 4))
                   1328:                                start += 4;
                   1329:                        else if (0 == strncmp(start, "\\-", 2))
                   1330:                                start += 2;
                   1331:                        else if (0 == strncmp(start, "\\(en", 4))
                   1332:                                start += 4;
                   1333:                        else if (0 == strncmp(start, "\\(em", 4))
                   1334:                                start += 4;
1.1       kristaps 1335:
1.50      kristaps 1336:                        while (' ' == *start)
                   1337:                                start++;
1.1       kristaps 1338:
1.78      schwarze 1339:                        assert(NULL == mpage->desc);
                   1340:                        mpage->desc = mandoc_strdup(start);
                   1341:                        putkey(mpage, mpage->desc, TYPE_Nd);
1.50      kristaps 1342:                        free(title);
1.61      schwarze 1343:                        return;
1.50      kristaps 1344:                }
                   1345:        }
1.1       kristaps 1346:
1.77      schwarze 1347:        for (n = n->child; n; n = n->next) {
1.78      schwarze 1348:                if (NULL != mpage->desc)
1.77      schwarze 1349:                        break;
1.78      schwarze 1350:                parse_man(mpage, n);
1.77      schwarze 1351:        }
1.1       kristaps 1352: }
                   1353:
                   1354: static void
1.78      schwarze 1355: parse_mdoc(struct mpage *mpage, const struct mdoc_node *n)
1.1       kristaps 1356: {
                   1357:
1.50      kristaps 1358:        assert(NULL != n);
                   1359:        for (n = n->child; NULL != n; n = n->next) {
                   1360:                switch (n->type) {
                   1361:                case (MDOC_ELEM):
                   1362:                        /* FALLTHROUGH */
                   1363:                case (MDOC_BLOCK):
                   1364:                        /* FALLTHROUGH */
                   1365:                case (MDOC_HEAD):
                   1366:                        /* FALLTHROUGH */
                   1367:                case (MDOC_BODY):
                   1368:                        /* FALLTHROUGH */
                   1369:                case (MDOC_TAIL):
                   1370:                        if (NULL != mdocs[n->tok].fp)
1.78      schwarze 1371:                               if (0 == (*mdocs[n->tok].fp)(mpage, n))
1.50      kristaps 1372:                                       break;
1.68      schwarze 1373:                        if (mdocs[n->tok].mask)
1.78      schwarze 1374:                                putmdockey(mpage, n->child,
                   1375:                                    mdocs[n->tok].mask);
1.50      kristaps 1376:                        break;
                   1377:                default:
                   1378:                        assert(MDOC_ROOT != n->type);
                   1379:                        continue;
                   1380:                }
                   1381:                if (NULL != n->child)
1.78      schwarze 1382:                        parse_mdoc(mpage, n);
1.1       kristaps 1383:        }
                   1384: }
                   1385:
1.25      schwarze 1386: static int
1.78      schwarze 1387: parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_node *n)
1.1       kristaps 1388: {
                   1389:        const char      *start, *end;
                   1390:        size_t           sz;
1.25      schwarze 1391:
1.50      kristaps 1392:        if (SEC_SYNOPSIS != n->sec ||
                   1393:                        NULL == (n = n->child) ||
                   1394:                        MDOC_TEXT != n->type)
1.25      schwarze 1395:                return(0);
1.1       kristaps 1396:
                   1397:        /*
                   1398:         * Only consider those `Fd' macro fields that begin with an
                   1399:         * "inclusion" token (versus, e.g., #define).
                   1400:         */
1.50      kristaps 1401:
1.1       kristaps 1402:        if (strcmp("#include", n->string))
1.25      schwarze 1403:                return(0);
1.1       kristaps 1404:
                   1405:        if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.25      schwarze 1406:                return(0);
1.1       kristaps 1407:
                   1408:        /*
                   1409:         * Strip away the enclosing angle brackets and make sure we're
                   1410:         * not zero-length.
                   1411:         */
                   1412:
                   1413:        start = n->string;
                   1414:        if ('<' == *start || '"' == *start)
                   1415:                start++;
                   1416:
                   1417:        if (0 == (sz = strlen(start)))
1.25      schwarze 1418:                return(0);
1.1       kristaps 1419:
                   1420:        end = &start[(int)sz - 1];
                   1421:        if ('>' == *end || '"' == *end)
                   1422:                end--;
                   1423:
1.50      kristaps 1424:        if (end > start)
1.78      schwarze 1425:                putkeys(mpage, start, end - start + 1, TYPE_In);
1.25      schwarze 1426:        return(1);
1.1       kristaps 1427: }
                   1428:
1.25      schwarze 1429: static int
1.78      schwarze 1430: parse_mdoc_In(struct mpage *mpage, const struct mdoc_node *n)
1.1       kristaps 1431: {
                   1432:
1.50      kristaps 1433:        if (NULL != n->child && MDOC_TEXT == n->child->type)
1.25      schwarze 1434:                return(0);
1.1       kristaps 1435:
1.78      schwarze 1436:        putkey(mpage, n->child->string, TYPE_In);
1.25      schwarze 1437:        return(1);
1.1       kristaps 1438: }
                   1439:
1.25      schwarze 1440: static int
1.78      schwarze 1441: parse_mdoc_Fn(struct mpage *mpage, const struct mdoc_node *n)
1.1       kristaps 1442: {
                   1443:        const char      *cp;
                   1444:
1.50      kristaps 1445:        if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.25      schwarze 1446:                return(0);
                   1447:
1.50      kristaps 1448:        /*
                   1449:         * Parse: .Fn "struct type *name" "char *arg".
                   1450:         * First strip away pointer symbol.
                   1451:         * Then store the function name, then type.
                   1452:         * Finally, store the arguments.
                   1453:         */
1.1       kristaps 1454:
1.50      kristaps 1455:        if (NULL == (cp = strrchr(n->string, ' ')))
                   1456:                cp = n->string;
1.1       kristaps 1457:
                   1458:        while ('*' == *cp)
                   1459:                cp++;
                   1460:
1.78      schwarze 1461:        putkey(mpage, cp, TYPE_Fn);
1.25      schwarze 1462:
1.50      kristaps 1463:        if (n->string < cp)
1.78      schwarze 1464:                putkeys(mpage, n->string, cp - n->string, TYPE_Ft);
1.25      schwarze 1465:
1.50      kristaps 1466:        for (n = n->next; NULL != n; n = n->next)
                   1467:                if (MDOC_TEXT == n->type)
1.78      schwarze 1468:                        putkey(mpage, n->string, TYPE_Fa);
1.25      schwarze 1469:
                   1470:        return(0);
1.1       kristaps 1471: }
                   1472:
1.25      schwarze 1473: static int
1.78      schwarze 1474: parse_mdoc_St(struct mpage *mpage, const struct mdoc_node *n)
1.1       kristaps 1475: {
1.25      schwarze 1476:
1.1       kristaps 1477:        if (NULL == n->child || MDOC_TEXT != n->child->type)
1.25      schwarze 1478:                return(0);
1.1       kristaps 1479:
1.78      schwarze 1480:        putkey(mpage, n->child->string, TYPE_St);
1.25      schwarze 1481:        return(1);
1.1       kristaps 1482: }
                   1483:
1.25      schwarze 1484: static int
1.78      schwarze 1485: parse_mdoc_Xr(struct mpage *mpage, const struct mdoc_node *n)
1.1       kristaps 1486: {
1.67      schwarze 1487:        char    *cp;
1.1       kristaps 1488:
                   1489:        if (NULL == (n = n->child))
1.25      schwarze 1490:                return(0);
1.1       kristaps 1491:
1.67      schwarze 1492:        if (NULL == n->next) {
1.78      schwarze 1493:                putkey(mpage, n->string, TYPE_Xr);
1.67      schwarze 1494:                return(0);
                   1495:        }
                   1496:
                   1497:        if (-1 == asprintf(&cp, "%s(%s)", n->string, n->next->string)) {
                   1498:                perror(NULL);
                   1499:                exit((int)MANDOCLEVEL_SYSERR);
                   1500:        }
1.78      schwarze 1501:        putkey(mpage, cp, TYPE_Xr);
1.67      schwarze 1502:        free(cp);
                   1503:        return(0);
1.1       kristaps 1504: }
                   1505:
1.25      schwarze 1506: static int
1.78      schwarze 1507: parse_mdoc_Nd(struct mpage *mpage, const struct mdoc_node *n)
1.1       kristaps 1508: {
1.50      kristaps 1509:        size_t           sz;
1.1       kristaps 1510:
                   1511:        if (MDOC_BODY != n->type)
1.25      schwarze 1512:                return(0);
1.1       kristaps 1513:
1.50      kristaps 1514:        /*
                   1515:         * Special-case the `Nd' because we need to put the description
                   1516:         * into the document table.
                   1517:         */
                   1518:
                   1519:        for (n = n->child; NULL != n; n = n->next) {
                   1520:                if (MDOC_TEXT == n->type) {
1.78      schwarze 1521:                        if (NULL != mpage->desc) {
                   1522:                                sz = strlen(mpage->desc) +
1.66      schwarze 1523:                                     strlen(n->string) + 2;
1.78      schwarze 1524:                                mpage->desc = mandoc_realloc(
                   1525:                                    mpage->desc, sz);
                   1526:                                strlcat(mpage->desc, " ", sz);
                   1527:                                strlcat(mpage->desc, n->string, sz);
1.66      schwarze 1528:                        } else
1.78      schwarze 1529:                                mpage->desc = mandoc_strdup(n->string);
1.50      kristaps 1530:                }
                   1531:                if (NULL != n->child)
1.78      schwarze 1532:                        parse_mdoc_Nd(mpage, n);
1.50      kristaps 1533:        }
1.25      schwarze 1534:        return(1);
1.1       kristaps 1535: }
                   1536:
1.25      schwarze 1537: static int
1.78      schwarze 1538: parse_mdoc_Nm(struct mpage *mpage, const struct mdoc_node *n)
1.1       kristaps 1539: {
                   1540:
1.25      schwarze 1541:        if (SEC_NAME == n->sec)
                   1542:                return(1);
                   1543:        else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                   1544:                return(0);
1.1       kristaps 1545:
1.25      schwarze 1546:        return(1);
1.1       kristaps 1547: }
                   1548:
1.25      schwarze 1549: static int
1.78      schwarze 1550: parse_mdoc_Sh(struct mpage *mpage, const struct mdoc_node *n)
1.1       kristaps 1551: {
                   1552:
1.25      schwarze 1553:        return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1       kristaps 1554: }
                   1555:
1.50      kristaps 1556: static int
1.78      schwarze 1557: parse_mdoc_head(struct mpage *mpage, const struct mdoc_node *n)
1.1       kristaps 1558: {
                   1559:
1.50      kristaps 1560:        return(MDOC_HEAD == n->type);
                   1561: }
1.1       kristaps 1562:
1.50      kristaps 1563: static int
1.78      schwarze 1564: parse_mdoc_body(struct mpage *mpage, const struct mdoc_node *n)
1.50      kristaps 1565: {
1.1       kristaps 1566:
1.50      kristaps 1567:        return(MDOC_BODY == n->type);
1.1       kristaps 1568: }
                   1569:
1.50      kristaps 1570: /*
1.66      schwarze 1571:  * Add a string to the hash table for the current manual.
                   1572:  * Each string has a bitmask telling which macros it belongs to.
                   1573:  * When we finish the manual, we'll dump the table.
1.50      kristaps 1574:  */
                   1575: static void
1.78      schwarze 1576: putkeys(const struct mpage *mpage,
                   1577:        const char *cp, size_t sz, uint64_t v)
1.50      kristaps 1578: {
                   1579:        struct str      *s;
1.65      schwarze 1580:        unsigned int     slot;
1.50      kristaps 1581:        const char      *end;
1.25      schwarze 1582:
1.50      kristaps 1583:        if (0 == sz)
                   1584:                return;
1.25      schwarze 1585:
1.65      schwarze 1586:        end = cp + sz;
                   1587:        slot = ohash_qlookupi(&strings, cp, &end);
                   1588:        s = ohash_find(&strings, slot);
1.25      schwarze 1589:
1.78      schwarze 1590:        if (NULL != s && mpage == s->mpage) {
1.50      kristaps 1591:                s->mask |= v;
                   1592:                return;
                   1593:        } else if (NULL == s) {
1.51      kristaps 1594:                s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
1.50      kristaps 1595:                memcpy(s->key, cp, sz);
1.65      schwarze 1596:                ohash_insert(&strings, slot, s);
1.1       kristaps 1597:        }
1.78      schwarze 1598:        s->mpage = mpage;
1.50      kristaps 1599:        s->mask = v;
1.1       kristaps 1600: }
                   1601:
1.50      kristaps 1602: /*
                   1603:  * Take a Unicode codepoint and produce its UTF-8 encoding.
                   1604:  * This isn't the best way to do this, but it works.
                   1605:  * The magic numbers are from the UTF-8 packaging.
                   1606:  * They're not as scary as they seem: read the UTF-8 spec for details.
                   1607:  */
                   1608: static size_t
                   1609: utf8(unsigned int cp, char out[7])
1.1       kristaps 1610: {
1.50      kristaps 1611:        size_t           rc;
1.1       kristaps 1612:
1.50      kristaps 1613:        rc = 0;
                   1614:        if (cp <= 0x0000007F) {
                   1615:                rc = 1;
                   1616:                out[0] = (char)cp;
                   1617:        } else if (cp <= 0x000007FF) {
                   1618:                rc = 2;
                   1619:                out[0] = (cp >> 6  & 31) | 192;
                   1620:                out[1] = (cp       & 63) | 128;
                   1621:        } else if (cp <= 0x0000FFFF) {
                   1622:                rc = 3;
                   1623:                out[0] = (cp >> 12 & 15) | 224;
                   1624:                out[1] = (cp >> 6  & 63) | 128;
                   1625:                out[2] = (cp       & 63) | 128;
                   1626:        } else if (cp <= 0x001FFFFF) {
                   1627:                rc = 4;
                   1628:                out[0] = (cp >> 18 &  7) | 240;
                   1629:                out[1] = (cp >> 12 & 63) | 128;
                   1630:                out[2] = (cp >> 6  & 63) | 128;
                   1631:                out[3] = (cp       & 63) | 128;
                   1632:        } else if (cp <= 0x03FFFFFF) {
                   1633:                rc = 5;
                   1634:                out[0] = (cp >> 24 &  3) | 248;
                   1635:                out[1] = (cp >> 18 & 63) | 128;
                   1636:                out[2] = (cp >> 12 & 63) | 128;
                   1637:                out[3] = (cp >> 6  & 63) | 128;
                   1638:                out[4] = (cp       & 63) | 128;
                   1639:        } else if (cp <= 0x7FFFFFFF) {
                   1640:                rc = 6;
                   1641:                out[0] = (cp >> 30 &  1) | 252;
                   1642:                out[1] = (cp >> 24 & 63) | 128;
                   1643:                out[2] = (cp >> 18 & 63) | 128;
                   1644:                out[3] = (cp >> 12 & 63) | 128;
                   1645:                out[4] = (cp >> 6  & 63) | 128;
                   1646:                out[5] = (cp       & 63) | 128;
                   1647:        } else
1.1       kristaps 1648:                return(0);
                   1649:
1.50      kristaps 1650:        out[rc] = '\0';
                   1651:        return(rc);
                   1652: }
1.1       kristaps 1653:
1.50      kristaps 1654: /*
                   1655:  * Store the UTF-8 version of a key, or alias the pointer if the key has
                   1656:  * no UTF-8 transcription marks in it.
                   1657:  */
                   1658: static void
                   1659: utf8key(struct mchars *mc, struct str *key)
                   1660: {
                   1661:        size_t           sz, bsz, pos;
                   1662:        char             utfbuf[7], res[5];
                   1663:        char            *buf;
                   1664:        const char      *seq, *cpp, *val;
                   1665:        int              len, u;
                   1666:        enum mandoc_esc  esc;
                   1667:
                   1668:        assert(NULL == key->utf8);
                   1669:
                   1670:        res[0] = '\\';
                   1671:        res[1] = '\t';
                   1672:        res[2] = ASCII_NBRSP;
                   1673:        res[3] = ASCII_HYPH;
                   1674:        res[4] = '\0';
1.1       kristaps 1675:
1.50      kristaps 1676:        val = key->key;
                   1677:        bsz = strlen(val);
1.46      kristaps 1678:
1.50      kristaps 1679:        /*
                   1680:         * Pre-check: if we have no stop-characters, then set the
                   1681:         * pointer as ourselvse and get out of here.
                   1682:         */
                   1683:        if (strcspn(val, res) == bsz) {
                   1684:                key->utf8 = key->key;
                   1685:                return;
                   1686:        }
1.46      kristaps 1687:
1.50      kristaps 1688:        /* Pre-allocate by the length of the input */
1.46      kristaps 1689:
1.50      kristaps 1690:        buf = mandoc_malloc(++bsz);
                   1691:        pos = 0;
1.46      kristaps 1692:
1.50      kristaps 1693:        while ('\0' != *val) {
                   1694:                /*
                   1695:                 * Halt on the first escape sequence.
                   1696:                 * This also halts on the end of string, in which case
                   1697:                 * we just copy, fallthrough, and exit the loop.
                   1698:                 */
                   1699:                if ((sz = strcspn(val, res)) > 0) {
                   1700:                        memcpy(&buf[pos], val, sz);
                   1701:                        pos += sz;
                   1702:                        val += sz;
                   1703:                }
1.46      kristaps 1704:
1.50      kristaps 1705:                if (ASCII_HYPH == *val) {
                   1706:                        buf[pos++] = '-';
                   1707:                        val++;
                   1708:                        continue;
                   1709:                } else if ('\t' == *val || ASCII_NBRSP == *val) {
                   1710:                        buf[pos++] = ' ';
                   1711:                        val++;
                   1712:                        continue;
                   1713:                } else if ('\\' != *val)
                   1714:                        break;
1.46      kristaps 1715:
1.50      kristaps 1716:                /* Read past the slash. */
1.46      kristaps 1717:
1.50      kristaps 1718:                val++;
                   1719:                u = 0;
1.46      kristaps 1720:
1.50      kristaps 1721:                /*
                   1722:                 * Parse the escape sequence and see if it's a
                   1723:                 * predefined character or special character.
                   1724:                 */
                   1725:                esc = mandoc_escape
                   1726:                        ((const char **)&val, &seq, &len);
                   1727:                if (ESCAPE_ERROR == esc)
                   1728:                        break;
1.1       kristaps 1729:
1.50      kristaps 1730:                if (ESCAPE_SPECIAL != esc)
                   1731:                        continue;
                   1732:                if (0 == (u = mchars_spec2cp(mc, seq, len)))
                   1733:                        continue;
1.1       kristaps 1734:
1.50      kristaps 1735:                /*
                   1736:                 * If we have a Unicode codepoint, try to convert that
                   1737:                 * to a UTF-8 byte string.
                   1738:                 */
                   1739:                cpp = utfbuf;
                   1740:                if (0 == (sz = utf8(u, utfbuf)))
                   1741:                        continue;
1.1       kristaps 1742:
1.50      kristaps 1743:                /* Copy the rendered glyph into the stream. */
1.1       kristaps 1744:
1.50      kristaps 1745:                sz = strlen(cpp);
                   1746:                bsz += sz;
1.1       kristaps 1747:
1.50      kristaps 1748:                buf = mandoc_realloc(buf, bsz);
1.1       kristaps 1749:
1.50      kristaps 1750:                memcpy(&buf[pos], cpp, sz);
                   1751:                pos += sz;
1.1       kristaps 1752:        }
                   1753:
1.50      kristaps 1754:        buf[pos] = '\0';
                   1755:        key->utf8 = buf;
1.1       kristaps 1756: }
                   1757:
1.14      schwarze 1758: /*
1.50      kristaps 1759:  * Flush the current page's terms (and their bits) into the database.
                   1760:  * Wrap the entire set of additions in a transaction to make sqlite be a
                   1761:  * little faster.
                   1762:  * Also, UTF-8-encode the description at the last possible moment.
1.14      schwarze 1763:  */
                   1764: static void
1.82      schwarze 1765: dbindex(const struct mpage *mpage, struct mchars *mc)
1.14      schwarze 1766: {
1.50      kristaps 1767:        struct str      *key;
                   1768:        const char      *desc;
                   1769:        int64_t          recno;
1.52      kristaps 1770:        size_t           i;
1.66      schwarze 1771:        unsigned int     slot;
1.14      schwarze 1772:
1.56      schwarze 1773:        if (verb)
1.82      schwarze 1774:                say(mpage->mlinks->file, "Adding to index");
1.43      kristaps 1775:
1.50      kristaps 1776:        if (nodb)
1.14      schwarze 1777:                return;
1.28      kristaps 1778:
1.50      kristaps 1779:        desc = "";
1.78      schwarze 1780:        if (NULL != mpage->desc && '\0' != *mpage->desc) {
1.65      schwarze 1781:                key = ohash_find(&strings,
1.78      schwarze 1782:                        ohash_qlookup(&strings, mpage->desc));
1.50      kristaps 1783:                assert(NULL != key);
                   1784:                if (NULL == key->utf8)
                   1785:                        utf8key(mc, key);
                   1786:                desc = key->utf8;
                   1787:        }
                   1788:
1.52      kristaps 1789:        SQL_EXEC("BEGIN TRANSACTION");
1.50      kristaps 1790:
1.52      kristaps 1791:        i = 1;
1.81      schwarze 1792:        /*
                   1793:         * XXX The following three lines are obsolete
                   1794:         * and only kept for backward compatibility
                   1795:         * until apropos(1) and friends have caught up.
                   1796:         */
1.82      schwarze 1797:        SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, mpage->mlinks->file);
                   1798:        SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, mpage->mlinks->dsec);
                   1799:        SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, mpage->mlinks->arch);
1.81      schwarze 1800:        SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, desc);
1.82      schwarze 1801:        SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, FORM_SRC == mpage->form);
1.81      schwarze 1802:        SQL_STEP(stmts[STMT_INSERT_PAGE]);
1.50      kristaps 1803:        recno = sqlite3_last_insert_rowid(db);
1.81      schwarze 1804:        sqlite3_reset(stmts[STMT_INSERT_PAGE]);
                   1805:
                   1806:        i = 1;
1.82      schwarze 1807:        SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mpage->mlinks->dsec);
                   1808:        SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mpage->mlinks->arch);
                   1809:        SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mpage->mlinks->file);
1.81      schwarze 1810:        SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, recno);
                   1811:        SQL_STEP(stmts[STMT_INSERT_LINK]);
                   1812:        sqlite3_reset(stmts[STMT_INSERT_LINK]);
1.50      kristaps 1813:
1.66      schwarze 1814:        for (key = ohash_first(&strings, &slot); NULL != key;
                   1815:             key = ohash_next(&strings, &slot)) {
1.78      schwarze 1816:                assert(key->mpage == mpage);
1.50      kristaps 1817:                if (NULL == key->utf8)
                   1818:                        utf8key(mc, key);
1.52      kristaps 1819:                i = 1;
                   1820:                SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);
                   1821:                SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->utf8);
                   1822:                SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, recno);
                   1823:                SQL_STEP(stmts[STMT_INSERT_KEY]);
1.50      kristaps 1824:                sqlite3_reset(stmts[STMT_INSERT_KEY]);
1.66      schwarze 1825:                if (key->utf8 != key->key)
                   1826:                        free(key->utf8);
                   1827:                free(key);
1.38      schwarze 1828:        }
1.28      kristaps 1829:
1.52      kristaps 1830:        SQL_EXEC("END TRANSACTION");
1.14      schwarze 1831: }
                   1832:
1.5       kristaps 1833: static void
1.59      schwarze 1834: dbprune(void)
1.5       kristaps 1835: {
1.78      schwarze 1836:        struct mpage    *mpage;
1.82      schwarze 1837:        struct mlink    *mlink;
1.52      kristaps 1838:        size_t           i;
1.80      schwarze 1839:        unsigned int     slot;
1.5       kristaps 1840:
1.50      kristaps 1841:        if (nodb)
                   1842:                return;
1.12      schwarze 1843:
1.80      schwarze 1844:        mpage = ohash_first(&mpages, &slot);
                   1845:        while (NULL != mpage) {
1.82      schwarze 1846:                mlink = mpage->mlinks;
1.52      kristaps 1847:                i = 1;
1.82      schwarze 1848:                SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE], i, mlink->file);
1.81      schwarze 1849:                SQL_STEP(stmts[STMT_DELETE_PAGE]);
                   1850:                sqlite3_reset(stmts[STMT_DELETE_PAGE]);
1.56      schwarze 1851:                if (verb)
1.82      schwarze 1852:                        say(mlink->file, "Deleted from index");
1.80      schwarze 1853:                mpage = ohash_next(&mpages, &slot);
1.5       kristaps 1854:        }
                   1855: }
                   1856:
1.4       kristaps 1857: /*
1.50      kristaps 1858:  * Close an existing database and its prepared statements.
                   1859:  * If "real" is not set, rename the temporary file into the real one.
1.4       kristaps 1860:  */
1.35      kristaps 1861: static void
1.59      schwarze 1862: dbclose(int real)
1.4       kristaps 1863: {
1.50      kristaps 1864:        size_t           i;
1.4       kristaps 1865:
1.50      kristaps 1866:        if (nodb)
1.38      schwarze 1867:                return;
1.50      kristaps 1868:
                   1869:        for (i = 0; i < STMT__MAX; i++) {
                   1870:                sqlite3_finalize(stmts[i]);
                   1871:                stmts[i] = NULL;
1.4       kristaps 1872:        }
                   1873:
1.50      kristaps 1874:        sqlite3_close(db);
                   1875:        db = NULL;
1.12      schwarze 1876:
1.50      kristaps 1877:        if (real)
                   1878:                return;
1.12      schwarze 1879:
1.63      schwarze 1880:        if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {
1.59      schwarze 1881:                exitcode = (int)MANDOCLEVEL_SYSERR;
                   1882:                say(MANDOC_DB, NULL);
                   1883:        }
1.50      kristaps 1884: }
1.14      schwarze 1885:
1.50      kristaps 1886: /*
                   1887:  * This is straightforward stuff.
                   1888:  * Open a database connection to a "temporary" database, then open a set
                   1889:  * of prepared statements we'll use over and over again.
                   1890:  * If "real" is set, we use the existing database; if not, we truncate a
                   1891:  * temporary one.
                   1892:  * Must be matched by dbclose().
                   1893:  */
                   1894: static int
1.59      schwarze 1895: dbopen(int real)
1.50      kristaps 1896: {
1.63      schwarze 1897:        const char      *file, *sql;
1.50      kristaps 1898:        int              rc, ofl;
1.12      schwarze 1899:
1.50      kristaps 1900:        if (nodb)
                   1901:                return(1);
1.12      schwarze 1902:
1.63      schwarze 1903:        ofl = SQLITE_OPEN_READWRITE;
                   1904:        if (0 == real) {
                   1905:                file = MANDOC_DB "~";
                   1906:                if (-1 == remove(file) && ENOENT != errno) {
                   1907:                        exitcode = (int)MANDOCLEVEL_SYSERR;
                   1908:                        say(file, NULL);
                   1909:                        return(0);
                   1910:                }
                   1911:                ofl |= SQLITE_OPEN_EXCLUSIVE;
                   1912:        } else
                   1913:                file = MANDOC_DB;
1.45      kristaps 1914:
1.50      kristaps 1915:        rc = sqlite3_open_v2(file, &db, ofl, NULL);
                   1916:        if (SQLITE_OK == rc)
1.57      schwarze 1917:                goto prepare_statements;
1.50      kristaps 1918:        if (SQLITE_CANTOPEN != rc) {
1.59      schwarze 1919:                exitcode = (int)MANDOCLEVEL_SYSERR;
                   1920:                say(file, NULL);
1.50      kristaps 1921:                return(0);
                   1922:        }
1.12      schwarze 1923:
1.50      kristaps 1924:        sqlite3_close(db);
                   1925:        db = NULL;
1.12      schwarze 1926:
1.50      kristaps 1927:        if (SQLITE_OK != (rc = sqlite3_open(file, &db))) {
1.59      schwarze 1928:                exitcode = (int)MANDOCLEVEL_SYSERR;
                   1929:                say(file, NULL);
1.50      kristaps 1930:                return(0);
                   1931:        }
1.12      schwarze 1932:
1.81      schwarze 1933:        /*
                   1934:         * XXX The first three columns in table mpages are obsolete
                   1935:         * and only kept for backward compatibility
                   1936:         * until apropos(1) and friends have caught up.
                   1937:         */
                   1938:        sql = "CREATE TABLE \"mpages\" (\n"
1.50      kristaps 1939:              " \"file\" TEXT NOT NULL,\n"
                   1940:              " \"sec\" TEXT NOT NULL,\n"
                   1941:              " \"arch\" TEXT NOT NULL,\n"
                   1942:              " \"desc\" TEXT NOT NULL,\n"
                   1943:              " \"form\" INTEGER NOT NULL,\n"
                   1944:              " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
                   1945:              ");\n"
                   1946:              "\n"
1.81      schwarze 1947:              "CREATE TABLE \"mlinks\" (\n"
                   1948:              " \"sec\" TEXT NOT NULL,\n"
                   1949:              " \"arch\" TEXT NOT NULL,\n"
                   1950:              " \"name\" TEXT NOT NULL,\n"
                   1951:              " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
                   1952:                "ON DELETE CASCADE,\n"
                   1953:              " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
                   1954:              ");\n"
                   1955:              "\n"
1.50      kristaps 1956:              "CREATE TABLE \"keys\" (\n"
                   1957:              " \"bits\" INTEGER NOT NULL,\n"
                   1958:              " \"key\" TEXT NOT NULL,\n"
1.81      schwarze 1959:              " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
                   1960:                "ON DELETE CASCADE,\n"
1.50      kristaps 1961:              " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
                   1962:              ");\n"
                   1963:              "\n"
                   1964:              "CREATE INDEX \"key_index\" ON keys (key);\n";
1.14      schwarze 1965:
1.50      kristaps 1966:        if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
1.59      schwarze 1967:                exitcode = (int)MANDOCLEVEL_SYSERR;
                   1968:                say(file, "%s", sqlite3_errmsg(db));
1.50      kristaps 1969:                return(0);
                   1970:        }
1.4       kristaps 1971:
1.57      schwarze 1972: prepare_statements:
                   1973:        SQL_EXEC("PRAGMA foreign_keys = ON");
1.81      schwarze 1974:        sql = "DELETE FROM mpages where file=?";
                   1975:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL);
                   1976:        sql = "INSERT INTO mpages "
1.50      kristaps 1977:                "(file,sec,arch,desc,form) VALUES (?,?,?,?,?)";
1.81      schwarze 1978:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL);
                   1979:        sql = "INSERT INTO mlinks "
                   1980:                "(sec,arch,name,pageid) VALUES (?,?,?,?)";
                   1981:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL);
1.50      kristaps 1982:        sql = "INSERT INTO keys "
1.81      schwarze 1983:                "(bits,key,pageid) VALUES (?,?,?)";
1.50      kristaps 1984:        sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL);
1.70      schwarze 1985:
                   1986: #ifndef __APPLE__
                   1987:        /*
                   1988:         * When opening a new database, we can turn off
                   1989:         * synchronous mode for much better performance.
                   1990:         */
                   1991:
                   1992:        if (real)
                   1993:                SQL_EXEC("PRAGMA synchronous = OFF");
                   1994: #endif
                   1995:
1.50      kristaps 1996:        return(1);
                   1997: }
1.5       kristaps 1998:
1.50      kristaps 1999: static void *
                   2000: hash_halloc(size_t sz, void *arg)
                   2001: {
1.12      schwarze 2002:
1.50      kristaps 2003:        return(mandoc_calloc(sz, 1));
                   2004: }
1.12      schwarze 2005:
1.50      kristaps 2006: static void *
                   2007: hash_alloc(size_t sz, void *arg)
                   2008: {
1.5       kristaps 2009:
1.50      kristaps 2010:        return(mandoc_malloc(sz));
                   2011: }
1.41      kristaps 2012:
1.50      kristaps 2013: static void
                   2014: hash_free(void *p, size_t sz, void *arg)
                   2015: {
1.4       kristaps 2016:
1.50      kristaps 2017:        free(p);
1.4       kristaps 2018: }
                   2019:
1.50      kristaps 2020: static int
1.59      schwarze 2021: set_basedir(const char *targetdir)
1.4       kristaps 2022: {
1.59      schwarze 2023:        static char      startdir[PATH_MAX];
                   2024:        static int       fd;
1.4       kristaps 2025:
1.59      schwarze 2026:        /*
                   2027:         * Remember where we started by keeping a fd open to the origin
                   2028:         * path component: throughout this utility, we chdir() a lot to
                   2029:         * handle relative paths, and by doing this, we can return to
                   2030:         * the starting point.
                   2031:         */
                   2032:        if ('\0' == *startdir) {
                   2033:                if (NULL == getcwd(startdir, PATH_MAX)) {
                   2034:                        exitcode = (int)MANDOCLEVEL_SYSERR;
                   2035:                        if (NULL != targetdir)
                   2036:                                say(".", NULL);
                   2037:                        return(0);
                   2038:                }
                   2039:                if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
                   2040:                        exitcode = (int)MANDOCLEVEL_SYSERR;
                   2041:                        say(startdir, NULL);
                   2042:                        return(0);
                   2043:                }
                   2044:                if (NULL == targetdir)
                   2045:                        targetdir = startdir;
                   2046:        } else {
                   2047:                if (-1 == fd)
                   2048:                        return(0);
                   2049:                if (-1 == fchdir(fd)) {
                   2050:                        close(fd);
                   2051:                        basedir[0] = '\0';
                   2052:                        exitcode = (int)MANDOCLEVEL_SYSERR;
                   2053:                        say(startdir, NULL);
                   2054:                        return(0);
                   2055:                }
                   2056:                if (NULL == targetdir) {
                   2057:                        close(fd);
                   2058:                        return(1);
                   2059:                }
                   2060:        }
                   2061:        if (NULL == realpath(targetdir, basedir)) {
                   2062:                basedir[0] = '\0';
                   2063:                exitcode = (int)MANDOCLEVEL_BADARG;
                   2064:                say(targetdir, NULL);
1.50      kristaps 2065:                return(0);
1.59      schwarze 2066:        } else if (-1 == chdir(basedir)) {
                   2067:                exitcode = (int)MANDOCLEVEL_BADARG;
                   2068:                say("", NULL);
1.50      kristaps 2069:                return(0);
1.4       kristaps 2070:        }
1.50      kristaps 2071:        return(1);
1.56      schwarze 2072: }
                   2073:
                   2074: static void
1.59      schwarze 2075: say(const char *file, const char *format, ...)
1.56      schwarze 2076: {
                   2077:        va_list          ap;
                   2078:
1.59      schwarze 2079:        if ('\0' != *basedir)
                   2080:                fprintf(stderr, "%s", basedir);
                   2081:        if ('\0' != *basedir && '\0' != *file)
                   2082:                fputs("//", stderr);
1.56      schwarze 2083:        if ('\0' != *file)
1.59      schwarze 2084:                fprintf(stderr, "%s", file);
1.56      schwarze 2085:        fputs(": ", stderr);
1.59      schwarze 2086:
                   2087:        if (NULL == format) {
                   2088:                perror(NULL);
                   2089:                return;
                   2090:        }
1.56      schwarze 2091:
                   2092:        va_start(ap, format);
                   2093:        vfprintf(stderr, format, ap);
                   2094:        va_end(ap);
                   2095:
                   2096:        fputc('\n', stderr);
1.1       kristaps 2097: }

CVSweb