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