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

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

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