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

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