+typedef int (*mdoc_fp)(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+
+struct mdoc_handler {
+ mdoc_fp fp; /* optional handler */
+ uint64_t mask; /* set unless handler returns 0 */
+ int taboo; /* node flags that must not be set */
+};
+
+
+int mandocdb(int, char *[]);
+
+static void dbadd(struct dba *, struct mpage *);
+static void dbadd_mlink(const struct mlink *mlink);
+static void dbprune(struct dba *);
+static void dbwrite(struct dba *);
+static void filescan(const char *);
+#if HAVE_FTS_COMPARE_CONST
+static int fts_compare(const FTSENT *const *, const FTSENT *const *);
+#else
+static int fts_compare(const FTSENT **, const FTSENT **);
+#endif
+static void mlink_add(struct mlink *, const struct stat *);
+static void mlink_check(struct mpage *, struct mlink *);
+static void mlink_free(struct mlink *);
+static void mlinks_undupe(struct mpage *);
+static void mpages_free(void);
+static void mpages_merge(struct dba *, struct mparse *);
+static void parse_cat(struct mpage *, int);
+static void parse_man(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static void parse_mdoc(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static int parse_mdoc_head(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static int parse_mdoc_Fd(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static void parse_mdoc_fname(struct mpage *, const struct roff_node *);
+static int parse_mdoc_Fn(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static int parse_mdoc_Fo(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static int parse_mdoc_Nd(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static int parse_mdoc_Nm(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static int parse_mdoc_Sh(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static int parse_mdoc_Va(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static int parse_mdoc_Xr(struct mpage *, const struct roff_meta *,
+ const struct roff_node *);
+static void putkey(const struct mpage *, char *, uint64_t);
+static void putkeys(const struct mpage *, char *, size_t, uint64_t);
+static void putmdockey(const struct mpage *,
+ const struct roff_node *, uint64_t, int);
+static int render_string(char **, size_t *);
+static void say(const char *, const char *, ...)
+ __attribute__((__format__ (__printf__, 2, 3)));
+static int set_basedir(const char *, int);
+static int treescan(void);
+static size_t utf8(unsigned int, char [7]);
+
+static int nodb; /* no database changes */
+static int mparse_options; /* abort the parse early */
+static int use_all; /* use all found files */
+static int debug; /* print what we're doing */
+static int warnings; /* warn about crap */
+static int write_utf8; /* write UTF-8 output; else ASCII */
+static int exitcode; /* to be returned by main */
+static enum op op; /* operational mode */
+static char basedir[PATH_MAX]; /* current base directory */
+static struct mpage *mpage_head; /* list of distinct manual pages */
+static struct ohash mpages; /* table of distinct manual pages */
+static struct ohash mlinks; /* table of directory entries */
+static struct ohash names; /* table of all names */
+static struct ohash strings; /* table of all strings */
+static uint64_t name_mask;
+
+static const struct mdoc_handler __mdocs[MDOC_MAX - MDOC_Dd] = {
+ { NULL, 0, NODE_NOPRT }, /* Dd */
+ { NULL, 0, NODE_NOPRT }, /* Dt */
+ { NULL, 0, NODE_NOPRT }, /* Os */
+ { parse_mdoc_Sh, TYPE_Sh, 0 }, /* Sh */
+ { parse_mdoc_head, TYPE_Ss, 0 }, /* Ss */
+ { NULL, 0, 0 }, /* Pp */
+ { NULL, 0, 0 }, /* D1 */
+ { NULL, 0, 0 }, /* Dl */
+ { NULL, 0, 0 }, /* Bd */
+ { NULL, 0, 0 }, /* Ed */
+ { NULL, 0, 0 }, /* Bl */
+ { NULL, 0, 0 }, /* El */
+ { NULL, 0, 0 }, /* It */
+ { NULL, 0, 0 }, /* Ad */
+ { NULL, TYPE_An, 0 }, /* An */
+ { NULL, 0, 0 }, /* Ap */
+ { NULL, TYPE_Ar, 0 }, /* Ar */
+ { NULL, TYPE_Cd, 0 }, /* Cd */
+ { NULL, TYPE_Cm, 0 }, /* Cm */
+ { NULL, TYPE_Dv, 0 }, /* Dv */
+ { NULL, TYPE_Er, 0 }, /* Er */
+ { NULL, TYPE_Ev, 0 }, /* Ev */
+ { NULL, 0, 0 }, /* Ex */
+ { NULL, TYPE_Fa, 0 }, /* Fa */
+ { parse_mdoc_Fd, 0, 0 }, /* Fd */
+ { NULL, TYPE_Fl, 0 }, /* Fl */
+ { parse_mdoc_Fn, 0, 0 }, /* Fn */
+ { NULL, TYPE_Ft, 0 }, /* Ft */
+ { NULL, TYPE_Ic, 0 }, /* Ic */
+ { NULL, TYPE_In, 0 }, /* In */
+ { NULL, TYPE_Li, 0 }, /* Li */
+ { parse_mdoc_Nd, 0, 0 }, /* Nd */
+ { parse_mdoc_Nm, 0, 0 }, /* Nm */
+ { NULL, 0, 0 }, /* Op */
+ { NULL, 0, 0 }, /* Ot */
+ { NULL, TYPE_Pa, NODE_NOSRC }, /* Pa */
+ { NULL, 0, 0 }, /* Rv */
+ { NULL, TYPE_St, 0 }, /* St */
+ { parse_mdoc_Va, TYPE_Va, 0 }, /* Va */
+ { parse_mdoc_Va, TYPE_Vt, 0 }, /* Vt */
+ { parse_mdoc_Xr, 0, 0 }, /* Xr */
+ { NULL, 0, 0 }, /* %A */
+ { NULL, 0, 0 }, /* %B */
+ { NULL, 0, 0 }, /* %D */
+ { NULL, 0, 0 }, /* %I */
+ { NULL, 0, 0 }, /* %J */
+ { NULL, 0, 0 }, /* %N */
+ { NULL, 0, 0 }, /* %O */
+ { NULL, 0, 0 }, /* %P */
+ { NULL, 0, 0 }, /* %R */
+ { NULL, 0, 0 }, /* %T */
+ { NULL, 0, 0 }, /* %V */
+ { NULL, 0, 0 }, /* Ac */
+ { NULL, 0, 0 }, /* Ao */
+ { NULL, 0, 0 }, /* Aq */
+ { NULL, TYPE_At, 0 }, /* At */
+ { NULL, 0, 0 }, /* Bc */
+ { NULL, 0, 0 }, /* Bf */
+ { NULL, 0, 0 }, /* Bo */
+ { NULL, 0, 0 }, /* Bq */
+ { NULL, TYPE_Bsx, NODE_NOSRC }, /* Bsx */
+ { NULL, TYPE_Bx, NODE_NOSRC }, /* Bx */
+ { NULL, 0, 0 }, /* Db */
+ { NULL, 0, 0 }, /* Dc */
+ { NULL, 0, 0 }, /* Do */
+ { NULL, 0, 0 }, /* Dq */
+ { NULL, 0, 0 }, /* Ec */
+ { NULL, 0, 0 }, /* Ef */
+ { NULL, TYPE_Em, 0 }, /* Em */
+ { NULL, 0, 0 }, /* Eo */
+ { NULL, TYPE_Fx, NODE_NOSRC }, /* Fx */
+ { NULL, TYPE_Ms, 0 }, /* Ms */
+ { NULL, 0, 0 }, /* No */
+ { NULL, 0, 0 }, /* Ns */
+ { NULL, TYPE_Nx, NODE_NOSRC }, /* Nx */
+ { NULL, TYPE_Ox, NODE_NOSRC }, /* Ox */
+ { NULL, 0, 0 }, /* Pc */
+ { NULL, 0, 0 }, /* Pf */
+ { NULL, 0, 0 }, /* Po */
+ { NULL, 0, 0 }, /* Pq */
+ { NULL, 0, 0 }, /* Qc */
+ { NULL, 0, 0 }, /* Ql */
+ { NULL, 0, 0 }, /* Qo */
+ { NULL, 0, 0 }, /* Qq */
+ { NULL, 0, 0 }, /* Re */
+ { NULL, 0, 0 }, /* Rs */
+ { NULL, 0, 0 }, /* Sc */
+ { NULL, 0, 0 }, /* So */
+ { NULL, 0, 0 }, /* Sq */
+ { NULL, 0, 0 }, /* Sm */
+ { NULL, 0, 0 }, /* Sx */
+ { NULL, TYPE_Sy, 0 }, /* Sy */
+ { NULL, TYPE_Tn, 0 }, /* Tn */
+ { NULL, 0, NODE_NOSRC }, /* Ux */
+ { NULL, 0, 0 }, /* Xc */
+ { NULL, 0, 0 }, /* Xo */
+ { parse_mdoc_Fo, 0, 0 }, /* Fo */
+ { NULL, 0, 0 }, /* Fc */
+ { NULL, 0, 0 }, /* Oo */
+ { NULL, 0, 0 }, /* Oc */
+ { NULL, 0, 0 }, /* Bk */
+ { NULL, 0, 0 }, /* Ek */
+ { NULL, 0, 0 }, /* Bt */
+ { NULL, 0, 0 }, /* Hf */
+ { NULL, 0, 0 }, /* Fr */
+ { NULL, 0, 0 }, /* Ud */
+ { NULL, TYPE_Lb, NODE_NOSRC }, /* Lb */
+ { NULL, 0, 0 }, /* Lp */
+ { NULL, TYPE_Lk, 0 }, /* Lk */
+ { NULL, TYPE_Mt, NODE_NOSRC }, /* Mt */
+ { NULL, 0, 0 }, /* Brq */
+ { NULL, 0, 0 }, /* Bro */
+ { NULL, 0, 0 }, /* Brc */
+ { NULL, 0, 0 }, /* %C */
+ { NULL, 0, 0 }, /* Es */
+ { NULL, 0, 0 }, /* En */
+ { NULL, TYPE_Dx, NODE_NOSRC }, /* Dx */
+ { NULL, 0, 0 }, /* %Q */
+ { NULL, 0, 0 }, /* %U */
+ { NULL, 0, 0 }, /* Ta */