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

Annotation of mandoc/mandocdb.c, Revision 1.223

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

CVSweb