[BACK]Return to mandocdb.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mandocdb.c, Revision 1.71

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