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

Annotation of mandoc/mandocdb.c, Revision 1.61

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

CVSweb