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

Annotation of mandoc/mandocdb.c, Revision 1.250

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

CVSweb