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

Annotation of mandoc/mandocdb.c, Revision 1.127

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

CVSweb