[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.48

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