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

Annotation of mandoc/mandocdb.c, Revision 1.89

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

CVSweb