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

Annotation of mandoc/mandocdb.c, Revision 1.59

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

CVSweb