xref: /illumos-gate/usr/src/cmd/mandoc/mandocdb.c (revision 4d131170)
1*4d131170SRobert Mustacchi /* $Id: mandocdb.c,v 1.269 2021/08/19 16:55:31 schwarze Exp $ */
2a40ea1a7SYuri Pankov /*
3*4d131170SRobert Mustacchi  * Copyright (c) 2011-2020 Ingo Schwarze <schwarze@openbsd.org>
4a40ea1a7SYuri Pankov  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
5a40ea1a7SYuri Pankov  * Copyright (c) 2016 Ed Maste <emaste@freebsd.org>
6a40ea1a7SYuri Pankov  *
7a40ea1a7SYuri Pankov  * Permission to use, copy, modify, and distribute this software for any
8a40ea1a7SYuri Pankov  * purpose with or without fee is hereby granted, provided that the above
9a40ea1a7SYuri Pankov  * copyright notice and this permission notice appear in all copies.
10a40ea1a7SYuri Pankov  *
11a40ea1a7SYuri Pankov  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12a40ea1a7SYuri Pankov  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13a40ea1a7SYuri Pankov  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14a40ea1a7SYuri Pankov  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15a40ea1a7SYuri Pankov  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16a40ea1a7SYuri Pankov  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17a40ea1a7SYuri Pankov  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*4d131170SRobert Mustacchi  *
19*4d131170SRobert Mustacchi  * Implementation of the makewhatis(8) program.
20a40ea1a7SYuri Pankov  */
21a40ea1a7SYuri Pankov #include "config.h"
22a40ea1a7SYuri Pankov 
23a40ea1a7SYuri Pankov #include <sys/types.h>
246640c13bSYuri Pankov #include <sys/mman.h>
25a40ea1a7SYuri Pankov #include <sys/stat.h>
26a40ea1a7SYuri Pankov 
27a40ea1a7SYuri Pankov #include <assert.h>
28a40ea1a7SYuri Pankov #include <ctype.h>
29a40ea1a7SYuri Pankov #if HAVE_ERR
30a40ea1a7SYuri Pankov #include <err.h>
31a40ea1a7SYuri Pankov #endif
32a40ea1a7SYuri Pankov #include <errno.h>
33a40ea1a7SYuri Pankov #include <fcntl.h>
34a40ea1a7SYuri Pankov #if HAVE_FTS
35a40ea1a7SYuri Pankov #include <fts.h>
36a40ea1a7SYuri Pankov #else
37a40ea1a7SYuri Pankov #include "compat_fts.h"
38a40ea1a7SYuri Pankov #endif
39a40ea1a7SYuri Pankov #include <limits.h>
40a40ea1a7SYuri Pankov #if HAVE_SANDBOX_INIT
41a40ea1a7SYuri Pankov #include <sandbox.h>
42a40ea1a7SYuri Pankov #endif
43a40ea1a7SYuri Pankov #include <stdarg.h>
44a40ea1a7SYuri Pankov #include <stddef.h>
45a40ea1a7SYuri Pankov #include <stdio.h>
46a40ea1a7SYuri Pankov #include <stdint.h>
47a40ea1a7SYuri Pankov #include <stdlib.h>
48a40ea1a7SYuri Pankov #include <string.h>
49a40ea1a7SYuri Pankov #include <unistd.h>
50a40ea1a7SYuri Pankov 
51a40ea1a7SYuri Pankov #include "mandoc_aux.h"
52a40ea1a7SYuri Pankov #include "mandoc_ohash.h"
53a40ea1a7SYuri Pankov #include "mandoc.h"
54a40ea1a7SYuri Pankov #include "roff.h"
55a40ea1a7SYuri Pankov #include "mdoc.h"
56a40ea1a7SYuri Pankov #include "man.h"
57cec8643bSMichal Nowak #include "mandoc_parse.h"
58a40ea1a7SYuri Pankov #include "manconf.h"
59a40ea1a7SYuri Pankov #include "mansearch.h"
60a40ea1a7SYuri Pankov #include "dba_array.h"
61a40ea1a7SYuri Pankov #include "dba.h"
62a40ea1a7SYuri Pankov 
63a40ea1a7SYuri Pankov extern const char *const mansearch_keynames[];
64a40ea1a7SYuri Pankov 
65a40ea1a7SYuri Pankov enum	op {
66a40ea1a7SYuri Pankov 	OP_DEFAULT = 0, /* new dbs from dir list or default config */
67a40ea1a7SYuri Pankov 	OP_CONFFILE, /* new databases from custom config file */
68a40ea1a7SYuri Pankov 	OP_UPDATE, /* delete/add entries in existing database */
69a40ea1a7SYuri Pankov 	OP_DELETE, /* delete entries from existing database */
70a40ea1a7SYuri Pankov 	OP_TEST /* change no databases, report potential problems */
71a40ea1a7SYuri Pankov };
72a40ea1a7SYuri Pankov 
73a40ea1a7SYuri Pankov struct	str {
74a40ea1a7SYuri Pankov 	const struct mpage *mpage; /* if set, the owning parse */
75a40ea1a7SYuri Pankov 	uint64_t	 mask; /* bitmask in sequence */
76a40ea1a7SYuri Pankov 	char		 key[]; /* rendered text */
77a40ea1a7SYuri Pankov };
78a40ea1a7SYuri Pankov 
79a40ea1a7SYuri Pankov struct	inodev {
80a40ea1a7SYuri Pankov 	ino_t		 st_ino;
81a40ea1a7SYuri Pankov 	dev_t		 st_dev;
82a40ea1a7SYuri Pankov };
83a40ea1a7SYuri Pankov 
84a40ea1a7SYuri Pankov struct	mpage {
85a40ea1a7SYuri Pankov 	struct inodev	 inodev;  /* used for hashing routine */
86a40ea1a7SYuri Pankov 	struct dba_array *dba;
87a40ea1a7SYuri Pankov 	char		*sec;     /* section from file content */
88a40ea1a7SYuri Pankov 	char		*arch;    /* architecture from file content */
89a40ea1a7SYuri Pankov 	char		*title;   /* title from file content */
90a40ea1a7SYuri Pankov 	char		*desc;    /* description from file content */
91a40ea1a7SYuri Pankov 	struct mpage	*next;    /* singly linked list */
92a40ea1a7SYuri Pankov 	struct mlink	*mlinks;  /* singly linked list */
93a40ea1a7SYuri Pankov 	int		 name_head_done;
94a40ea1a7SYuri Pankov 	enum form	 form;    /* format from file content */
95a40ea1a7SYuri Pankov };
96a40ea1a7SYuri Pankov 
97a40ea1a7SYuri Pankov struct	mlink {
98a40ea1a7SYuri Pankov 	char		 file[PATH_MAX]; /* filename rel. to manpath */
99a40ea1a7SYuri Pankov 	char		*dsec;    /* section from directory */
100a40ea1a7SYuri Pankov 	char		*arch;    /* architecture from directory */
101a40ea1a7SYuri Pankov 	char		*name;    /* name from file name (not empty) */
102a40ea1a7SYuri Pankov 	char		*fsec;    /* section from file name suffix */
103a40ea1a7SYuri Pankov 	struct mlink	*next;    /* singly linked list */
104a40ea1a7SYuri Pankov 	struct mpage	*mpage;   /* parent */
105a40ea1a7SYuri Pankov 	int		 gzip;	  /* filename has a .gz suffix */
106a40ea1a7SYuri Pankov 	enum form	 dform;   /* format from directory */
107a40ea1a7SYuri Pankov 	enum form	 fform;   /* format from file name suffix */
108a40ea1a7SYuri Pankov };
109a40ea1a7SYuri Pankov 
110a40ea1a7SYuri Pankov typedef	int (*mdoc_fp)(struct mpage *, const struct roff_meta *,
111a40ea1a7SYuri Pankov 			const struct roff_node *);
112a40ea1a7SYuri Pankov 
113a40ea1a7SYuri Pankov struct	mdoc_handler {
114a40ea1a7SYuri Pankov 	mdoc_fp		 fp; /* optional handler */
115a40ea1a7SYuri Pankov 	uint64_t	 mask;  /* set unless handler returns 0 */
116a40ea1a7SYuri Pankov 	int		 taboo;  /* node flags that must not be set */
117a40ea1a7SYuri Pankov };
118a40ea1a7SYuri Pankov 
119a40ea1a7SYuri Pankov 
120a40ea1a7SYuri Pankov int		 mandocdb(int, char *[]);
121a40ea1a7SYuri Pankov 
122a40ea1a7SYuri Pankov static	void	 dbadd(struct dba *, struct mpage *);
123*4d131170SRobert Mustacchi static	void	 dbadd_mlink(const struct mlink *);
124a40ea1a7SYuri Pankov static	void	 dbprune(struct dba *);
125a40ea1a7SYuri Pankov static	void	 dbwrite(struct dba *);
126a40ea1a7SYuri Pankov static	void	 filescan(const char *);
127a40ea1a7SYuri Pankov #if HAVE_FTS_COMPARE_CONST
128a40ea1a7SYuri Pankov static	int	 fts_compare(const FTSENT *const *, const FTSENT *const *);
129a40ea1a7SYuri Pankov #else
130a40ea1a7SYuri Pankov static	int	 fts_compare(const FTSENT **, const FTSENT **);
131a40ea1a7SYuri Pankov #endif
132a40ea1a7SYuri Pankov static	void	 mlink_add(struct mlink *, const struct stat *);
133a40ea1a7SYuri Pankov static	void	 mlink_check(struct mpage *, struct mlink *);
134a40ea1a7SYuri Pankov static	void	 mlink_free(struct mlink *);
135a40ea1a7SYuri Pankov static	void	 mlinks_undupe(struct mpage *);
136a40ea1a7SYuri Pankov static	void	 mpages_free(void);
137a40ea1a7SYuri Pankov static	void	 mpages_merge(struct dba *, struct mparse *);
138a40ea1a7SYuri Pankov static	void	 parse_cat(struct mpage *, int);
139a40ea1a7SYuri Pankov static	void	 parse_man(struct mpage *, const struct roff_meta *,
140a40ea1a7SYuri Pankov 			const struct roff_node *);
141a40ea1a7SYuri Pankov static	void	 parse_mdoc(struct mpage *, const struct roff_meta *,
142a40ea1a7SYuri Pankov 			const struct roff_node *);
143a40ea1a7SYuri Pankov static	int	 parse_mdoc_head(struct mpage *, const struct roff_meta *,
144a40ea1a7SYuri Pankov 			const struct roff_node *);
1456640c13bSYuri Pankov static	int	 parse_mdoc_Fa(struct mpage *, const struct roff_meta *,
1466640c13bSYuri Pankov 			const struct roff_node *);
147a40ea1a7SYuri Pankov static	int	 parse_mdoc_Fd(struct mpage *, const struct roff_meta *,
148a40ea1a7SYuri Pankov 			const struct roff_node *);
149a40ea1a7SYuri Pankov static	void	 parse_mdoc_fname(struct mpage *, const struct roff_node *);
150a40ea1a7SYuri Pankov static	int	 parse_mdoc_Fn(struct mpage *, const struct roff_meta *,
151a40ea1a7SYuri Pankov 			const struct roff_node *);
152a40ea1a7SYuri Pankov static	int	 parse_mdoc_Fo(struct mpage *, const struct roff_meta *,
153a40ea1a7SYuri Pankov 			const struct roff_node *);
154a40ea1a7SYuri Pankov static	int	 parse_mdoc_Nd(struct mpage *, const struct roff_meta *,
155a40ea1a7SYuri Pankov 			const struct roff_node *);
156a40ea1a7SYuri Pankov static	int	 parse_mdoc_Nm(struct mpage *, const struct roff_meta *,
157a40ea1a7SYuri Pankov 			const struct roff_node *);
158a40ea1a7SYuri Pankov static	int	 parse_mdoc_Sh(struct mpage *, const struct roff_meta *,
159a40ea1a7SYuri Pankov 			const struct roff_node *);
160a40ea1a7SYuri Pankov static	int	 parse_mdoc_Va(struct mpage *, const struct roff_meta *,
161a40ea1a7SYuri Pankov 			const struct roff_node *);
162a40ea1a7SYuri Pankov static	int	 parse_mdoc_Xr(struct mpage *, const struct roff_meta *,
163a40ea1a7SYuri Pankov 			const struct roff_node *);
164a40ea1a7SYuri Pankov static	void	 putkey(const struct mpage *, char *, uint64_t);
165a40ea1a7SYuri Pankov static	void	 putkeys(const struct mpage *, char *, size_t, uint64_t);
166a40ea1a7SYuri Pankov static	void	 putmdockey(const struct mpage *,
167a40ea1a7SYuri Pankov 			const struct roff_node *, uint64_t, int);
168*4d131170SRobert Mustacchi #ifdef READ_ALLOWED_PATH
169*4d131170SRobert Mustacchi static	int	 read_allowed(const char *);
170*4d131170SRobert Mustacchi #endif
171a40ea1a7SYuri Pankov static	int	 render_string(char **, size_t *);
172a40ea1a7SYuri Pankov static	void	 say(const char *, const char *, ...)
173a40ea1a7SYuri Pankov 			__attribute__((__format__ (__printf__, 2, 3)));
174a40ea1a7SYuri Pankov static	int	 set_basedir(const char *, int);
175a40ea1a7SYuri Pankov static	int	 treescan(void);
176a40ea1a7SYuri Pankov static	size_t	 utf8(unsigned int, char [7]);
177a40ea1a7SYuri Pankov 
178a40ea1a7SYuri Pankov static	int		 nodb; /* no database changes */
179a40ea1a7SYuri Pankov static	int		 mparse_options; /* abort the parse early */
180a40ea1a7SYuri Pankov static	int		 use_all; /* use all found files */
181a40ea1a7SYuri Pankov static	int		 debug; /* print what we're doing */
182a40ea1a7SYuri Pankov static	int		 warnings; /* warn about crap */
183a40ea1a7SYuri Pankov static	int		 write_utf8; /* write UTF-8 output; else ASCII */
184a40ea1a7SYuri Pankov static	int		 exitcode; /* to be returned by main */
185a40ea1a7SYuri Pankov static	enum op		 op; /* operational mode */
186a40ea1a7SYuri Pankov static	char		 basedir[PATH_MAX]; /* current base directory */
187*4d131170SRobert Mustacchi static	size_t		 basedir_len; /* strlen(basedir) */
188a40ea1a7SYuri Pankov static	struct mpage	*mpage_head; /* list of distinct manual pages */
189a40ea1a7SYuri Pankov static	struct ohash	 mpages; /* table of distinct manual pages */
190a40ea1a7SYuri Pankov static	struct ohash	 mlinks; /* table of directory entries */
191a40ea1a7SYuri Pankov static	struct ohash	 names; /* table of all names */
192a40ea1a7SYuri Pankov static	struct ohash	 strings; /* table of all strings */
193a40ea1a7SYuri Pankov static	uint64_t	 name_mask;
194a40ea1a7SYuri Pankov 
195cec8643bSMichal Nowak static	const struct mdoc_handler mdoc_handlers[MDOC_MAX - MDOC_Dd] = {
196a40ea1a7SYuri Pankov 	{ NULL, 0, NODE_NOPRT },  /* Dd */
197a40ea1a7SYuri Pankov 	{ NULL, 0, NODE_NOPRT },  /* Dt */
198a40ea1a7SYuri Pankov 	{ NULL, 0, NODE_NOPRT },  /* Os */
199a40ea1a7SYuri Pankov 	{ parse_mdoc_Sh, TYPE_Sh, 0 }, /* Sh */
200a40ea1a7SYuri Pankov 	{ parse_mdoc_head, TYPE_Ss, 0 }, /* Ss */
201a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Pp */
202a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* D1 */
203a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Dl */
204a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Bd */
205a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ed */
206a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Bl */
207a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* El */
208a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* It */
209a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ad */
210a40ea1a7SYuri Pankov 	{ NULL, TYPE_An, 0 },  /* An */
211c66b8046SYuri Pankov 	{ NULL, 0, 0 },  /* Ap */
212a40ea1a7SYuri Pankov 	{ NULL, TYPE_Ar, 0 },  /* Ar */
213a40ea1a7SYuri Pankov 	{ NULL, TYPE_Cd, 0 },  /* Cd */
214a40ea1a7SYuri Pankov 	{ NULL, TYPE_Cm, 0 },  /* Cm */
215a40ea1a7SYuri Pankov 	{ NULL, TYPE_Dv, 0 },  /* Dv */
216a40ea1a7SYuri Pankov 	{ NULL, TYPE_Er, 0 },  /* Er */
217a40ea1a7SYuri Pankov 	{ NULL, TYPE_Ev, 0 },  /* Ev */
218a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ex */
2196640c13bSYuri Pankov 	{ parse_mdoc_Fa, 0, 0 },  /* Fa */
220a40ea1a7SYuri Pankov 	{ parse_mdoc_Fd, 0, 0 },  /* Fd */
221a40ea1a7SYuri Pankov 	{ NULL, TYPE_Fl, 0 },  /* Fl */
222a40ea1a7SYuri Pankov 	{ parse_mdoc_Fn, 0, 0 },  /* Fn */
2236640c13bSYuri Pankov 	{ NULL, TYPE_Ft | TYPE_Vt, 0 },  /* Ft */
224a40ea1a7SYuri Pankov 	{ NULL, TYPE_Ic, 0 },  /* Ic */
225a40ea1a7SYuri Pankov 	{ NULL, TYPE_In, 0 },  /* In */
226a40ea1a7SYuri Pankov 	{ NULL, TYPE_Li, 0 },  /* Li */
227a40ea1a7SYuri Pankov 	{ parse_mdoc_Nd, 0, 0 },  /* Nd */
228a40ea1a7SYuri Pankov 	{ parse_mdoc_Nm, 0, 0 },  /* Nm */
229a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Op */
230a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ot */
231a40ea1a7SYuri Pankov 	{ NULL, TYPE_Pa, NODE_NOSRC },  /* Pa */
232a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Rv */
233a40ea1a7SYuri Pankov 	{ NULL, TYPE_St, 0 },  /* St */
234a40ea1a7SYuri Pankov 	{ parse_mdoc_Va, TYPE_Va, 0 },  /* Va */
235a40ea1a7SYuri Pankov 	{ parse_mdoc_Va, TYPE_Vt, 0 },  /* Vt */
236a40ea1a7SYuri Pankov 	{ parse_mdoc_Xr, 0, 0 },  /* Xr */
237a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %A */
238a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %B */
239a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %D */
240a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %I */
241a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %J */
242a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %N */
243a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %O */
244a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %P */
245a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %R */
246a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %T */
247a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %V */
248a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ac */
249a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ao */
250a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Aq */
251a40ea1a7SYuri Pankov 	{ NULL, TYPE_At, 0 },  /* At */
252a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Bc */
253a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Bf */
254a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Bo */
255a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Bq */
256a40ea1a7SYuri Pankov 	{ NULL, TYPE_Bsx, NODE_NOSRC },  /* Bsx */
257a40ea1a7SYuri Pankov 	{ NULL, TYPE_Bx, NODE_NOSRC },  /* Bx */
258a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Db */
259a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Dc */
260a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Do */
261a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Dq */
262a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ec */
263a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ef */
264a40ea1a7SYuri Pankov 	{ NULL, TYPE_Em, 0 },  /* Em */
265a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Eo */
266a40ea1a7SYuri Pankov 	{ NULL, TYPE_Fx, NODE_NOSRC },  /* Fx */
267a40ea1a7SYuri Pankov 	{ NULL, TYPE_Ms, 0 },  /* Ms */
268a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* No */
269a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ns */
270a40ea1a7SYuri Pankov 	{ NULL, TYPE_Nx, NODE_NOSRC },  /* Nx */
271a40ea1a7SYuri Pankov 	{ NULL, TYPE_Ox, NODE_NOSRC },  /* Ox */
272a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Pc */
273a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Pf */
274a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Po */
275a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Pq */
276a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Qc */
277a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ql */
278a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Qo */
279a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Qq */
280a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Re */
281a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Rs */
282a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Sc */
283a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* So */
284a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Sq */
285a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Sm */
286a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Sx */
287a40ea1a7SYuri Pankov 	{ NULL, TYPE_Sy, 0 },  /* Sy */
288a40ea1a7SYuri Pankov 	{ NULL, TYPE_Tn, 0 },  /* Tn */
289a40ea1a7SYuri Pankov 	{ NULL, 0, NODE_NOSRC },  /* Ux */
290a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Xc */
291a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Xo */
292a40ea1a7SYuri Pankov 	{ parse_mdoc_Fo, 0, 0 },  /* Fo */
293a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Fc */
294a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Oo */
295a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Oc */
296a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Bk */
297a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ek */
298a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Bt */
299a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Hf */
300a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Fr */
301a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ud */
302a40ea1a7SYuri Pankov 	{ NULL, TYPE_Lb, NODE_NOSRC },  /* Lb */
303a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Lp */
304a40ea1a7SYuri Pankov 	{ NULL, TYPE_Lk, 0 },  /* Lk */
305a40ea1a7SYuri Pankov 	{ NULL, TYPE_Mt, NODE_NOSRC },  /* Mt */
306a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Brq */
307a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Bro */
308a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Brc */
309a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %C */
310a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Es */
311a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* En */
312a40ea1a7SYuri Pankov 	{ NULL, TYPE_Dx, NODE_NOSRC },  /* Dx */
313a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %Q */
314a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* %U */
315a40ea1a7SYuri Pankov 	{ NULL, 0, 0 },  /* Ta */
316a40ea1a7SYuri Pankov };
317a40ea1a7SYuri Pankov 
318a40ea1a7SYuri Pankov 
319a40ea1a7SYuri Pankov int
mandocdb(int argc,char * argv[])320a40ea1a7SYuri Pankov mandocdb(int argc, char *argv[])
321a40ea1a7SYuri Pankov {
322a40ea1a7SYuri Pankov 	struct manconf	  conf;
323a40ea1a7SYuri Pankov 	struct mparse	 *mp;
324a40ea1a7SYuri Pankov 	struct dba	 *dba;
325a40ea1a7SYuri Pankov 	const char	 *path_arg, *progname;
326a40ea1a7SYuri Pankov 	size_t		  j, sz;
327a40ea1a7SYuri Pankov 	int		  ch, i;
328a40ea1a7SYuri Pankov 
329a40ea1a7SYuri Pankov #if HAVE_PLEDGE
3306640c13bSYuri Pankov 	if (pledge("stdio rpath wpath cpath", NULL) == -1) {
331a40ea1a7SYuri Pankov 		warn("pledge");
332a40ea1a7SYuri Pankov 		return (int)MANDOCLEVEL_SYSERR;
333a40ea1a7SYuri Pankov 	}
334a40ea1a7SYuri Pankov #endif
335a40ea1a7SYuri Pankov 
336a40ea1a7SYuri Pankov #if HAVE_SANDBOX_INIT
337a40ea1a7SYuri Pankov 	if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1) {
338a40ea1a7SYuri Pankov 		warnx("sandbox_init");
339a40ea1a7SYuri Pankov 		return (int)MANDOCLEVEL_SYSERR;
340a40ea1a7SYuri Pankov 	}
341a40ea1a7SYuri Pankov #endif
342a40ea1a7SYuri Pankov 
343a40ea1a7SYuri Pankov 	memset(&conf, 0, sizeof(conf));
344a40ea1a7SYuri Pankov 
345a40ea1a7SYuri Pankov 	/*
346a40ea1a7SYuri Pankov 	 * We accept a few different invocations.
347a40ea1a7SYuri Pankov 	 * The CHECKOP macro makes sure that invocation styles don't
348a40ea1a7SYuri Pankov 	 * clobber each other.
349a40ea1a7SYuri Pankov 	 */
350a40ea1a7SYuri Pankov #define	CHECKOP(_op, _ch) do \
351*4d131170SRobert Mustacchi 	if ((_op) != OP_DEFAULT) { \
352a40ea1a7SYuri Pankov 		warnx("-%c: Conflicting option", (_ch)); \
353a40ea1a7SYuri Pankov 		goto usage; \
354a40ea1a7SYuri Pankov 	} while (/*CONSTCOND*/0)
355a40ea1a7SYuri Pankov 
356cec8643bSMichal Nowak 	mparse_options = MPARSE_VALIDATE;
357a40ea1a7SYuri Pankov 	path_arg = NULL;
358a40ea1a7SYuri Pankov 	op = OP_DEFAULT;
359a40ea1a7SYuri Pankov 
360*4d131170SRobert Mustacchi 	while ((ch = getopt(argc, argv, "aC:Dd:npQT:tu:v")) != -1)
361a40ea1a7SYuri Pankov 		switch (ch) {
362a40ea1a7SYuri Pankov 		case 'a':
363a40ea1a7SYuri Pankov 			use_all = 1;
364a40ea1a7SYuri Pankov 			break;
365a40ea1a7SYuri Pankov 		case 'C':
366a40ea1a7SYuri Pankov 			CHECKOP(op, ch);
367a40ea1a7SYuri Pankov 			path_arg = optarg;
368a40ea1a7SYuri Pankov 			op = OP_CONFFILE;
369a40ea1a7SYuri Pankov 			break;
370a40ea1a7SYuri Pankov 		case 'D':
371a40ea1a7SYuri Pankov 			debug++;
372a40ea1a7SYuri Pankov 			break;
373a40ea1a7SYuri Pankov 		case 'd':
374a40ea1a7SYuri Pankov 			CHECKOP(op, ch);
375a40ea1a7SYuri Pankov 			path_arg = optarg;
376a40ea1a7SYuri Pankov 			op = OP_UPDATE;
377a40ea1a7SYuri Pankov 			break;
378a40ea1a7SYuri Pankov 		case 'n':
379a40ea1a7SYuri Pankov 			nodb = 1;
380a40ea1a7SYuri Pankov 			break;
381a40ea1a7SYuri Pankov 		case 'p':
382a40ea1a7SYuri Pankov 			warnings = 1;
383a40ea1a7SYuri Pankov 			break;
384a40ea1a7SYuri Pankov 		case 'Q':
385a40ea1a7SYuri Pankov 			mparse_options |= MPARSE_QUICK;
386a40ea1a7SYuri Pankov 			break;
387a40ea1a7SYuri Pankov 		case 'T':
388*4d131170SRobert Mustacchi 			if (strcmp(optarg, "utf8") != 0) {
389a40ea1a7SYuri Pankov 				warnx("-T%s: Unsupported output format",
390a40ea1a7SYuri Pankov 				    optarg);
391a40ea1a7SYuri Pankov 				goto usage;
392a40ea1a7SYuri Pankov 			}
393a40ea1a7SYuri Pankov 			write_utf8 = 1;
394a40ea1a7SYuri Pankov 			break;
395a40ea1a7SYuri Pankov 		case 't':
396a40ea1a7SYuri Pankov 			CHECKOP(op, ch);
397a40ea1a7SYuri Pankov 			dup2(STDOUT_FILENO, STDERR_FILENO);
398a40ea1a7SYuri Pankov 			op = OP_TEST;
399a40ea1a7SYuri Pankov 			nodb = warnings = 1;
400a40ea1a7SYuri Pankov 			break;
401a40ea1a7SYuri Pankov 		case 'u':
402a40ea1a7SYuri Pankov 			CHECKOP(op, ch);
403a40ea1a7SYuri Pankov 			path_arg = optarg;
404a40ea1a7SYuri Pankov 			op = OP_DELETE;
405a40ea1a7SYuri Pankov 			break;
406a40ea1a7SYuri Pankov 		case 'v':
407a40ea1a7SYuri Pankov 			/* Compatibility with espie@'s makewhatis. */
408a40ea1a7SYuri Pankov 			break;
409a40ea1a7SYuri Pankov 		default:
410a40ea1a7SYuri Pankov 			goto usage;
411a40ea1a7SYuri Pankov 		}
412a40ea1a7SYuri Pankov 
413a40ea1a7SYuri Pankov 	argc -= optind;
414a40ea1a7SYuri Pankov 	argv += optind;
415a40ea1a7SYuri Pankov 
416a40ea1a7SYuri Pankov #if HAVE_PLEDGE
417a40ea1a7SYuri Pankov 	if (nodb) {
418a40ea1a7SYuri Pankov 		if (pledge("stdio rpath", NULL) == -1) {
419a40ea1a7SYuri Pankov 			warn("pledge");
420a40ea1a7SYuri Pankov 			return (int)MANDOCLEVEL_SYSERR;
421a40ea1a7SYuri Pankov 		}
422a40ea1a7SYuri Pankov 	}
423a40ea1a7SYuri Pankov #endif
424a40ea1a7SYuri Pankov 
425*4d131170SRobert Mustacchi 	if (op == OP_CONFFILE && argc > 0) {
426a40ea1a7SYuri Pankov 		warnx("-C: Too many arguments");
427a40ea1a7SYuri Pankov 		goto usage;
428a40ea1a7SYuri Pankov 	}
429a40ea1a7SYuri Pankov 
430a40ea1a7SYuri Pankov 	exitcode = (int)MANDOCLEVEL_OK;
431a40ea1a7SYuri Pankov 	mchars_alloc();
432cec8643bSMichal Nowak 	mp = mparse_alloc(mparse_options, MANDOC_OS_OTHER, NULL);
433a40ea1a7SYuri Pankov 	mandoc_ohash_init(&mpages, 6, offsetof(struct mpage, inodev));
434a40ea1a7SYuri Pankov 	mandoc_ohash_init(&mlinks, 6, offsetof(struct mlink, file));
435a40ea1a7SYuri Pankov 
436*4d131170SRobert Mustacchi 	if (op == OP_UPDATE || op == OP_DELETE || op == OP_TEST) {
437a40ea1a7SYuri Pankov 
438a40ea1a7SYuri Pankov 		/*
439a40ea1a7SYuri Pankov 		 * Most of these deal with a specific directory.
440a40ea1a7SYuri Pankov 		 * Jump into that directory first.
441a40ea1a7SYuri Pankov 		 */
442*4d131170SRobert Mustacchi 		if (op != OP_TEST && set_basedir(path_arg, 1) == 0)
443a40ea1a7SYuri Pankov 			goto out;
444a40ea1a7SYuri Pankov 
445a40ea1a7SYuri Pankov 		dba = nodb ? dba_new(128) : dba_read(MANDOC_DB);
446a40ea1a7SYuri Pankov 		if (dba != NULL) {
447a40ea1a7SYuri Pankov 			/*
448a40ea1a7SYuri Pankov 			 * The existing database is usable.  Process
449a40ea1a7SYuri Pankov 			 * all files specified on the command-line.
450a40ea1a7SYuri Pankov 			 */
451a40ea1a7SYuri Pankov 			use_all = 1;
452a40ea1a7SYuri Pankov 			for (i = 0; i < argc; i++)
453a40ea1a7SYuri Pankov 				filescan(argv[i]);
454a40ea1a7SYuri Pankov 			if (nodb == 0)
455a40ea1a7SYuri Pankov 				dbprune(dba);
456a40ea1a7SYuri Pankov 		} else {
457a40ea1a7SYuri Pankov 			/* Database missing or corrupt. */
458a40ea1a7SYuri Pankov 			if (op != OP_UPDATE || errno != ENOENT)
459a40ea1a7SYuri Pankov 				say(MANDOC_DB, "%s: Automatically recreating"
460a40ea1a7SYuri Pankov 				    " from scratch", strerror(errno));
461a40ea1a7SYuri Pankov 			exitcode = (int)MANDOCLEVEL_OK;
462a40ea1a7SYuri Pankov 			op = OP_DEFAULT;
463*4d131170SRobert Mustacchi 			if (treescan() == 0)
464a40ea1a7SYuri Pankov 				goto out;
465a40ea1a7SYuri Pankov 			dba = dba_new(128);
466a40ea1a7SYuri Pankov 		}
467*4d131170SRobert Mustacchi 		if (op != OP_DELETE)
468a40ea1a7SYuri Pankov 			mpages_merge(dba, mp);
469a40ea1a7SYuri Pankov 		if (nodb == 0)
470a40ea1a7SYuri Pankov 			dbwrite(dba);
471a40ea1a7SYuri Pankov 		dba_free(dba);
472a40ea1a7SYuri Pankov 	} else {
473a40ea1a7SYuri Pankov 		/*
474a40ea1a7SYuri Pankov 		 * If we have arguments, use them as our manpaths.
475a40ea1a7SYuri Pankov 		 * If we don't, use man.conf(5).
476a40ea1a7SYuri Pankov 		 */
477a40ea1a7SYuri Pankov 		if (argc > 0) {
478a40ea1a7SYuri Pankov 			conf.manpath.paths = mandoc_reallocarray(NULL,
479a40ea1a7SYuri Pankov 			    argc, sizeof(char *));
480a40ea1a7SYuri Pankov 			conf.manpath.sz = (size_t)argc;
481a40ea1a7SYuri Pankov 			for (i = 0; i < argc; i++)
482a40ea1a7SYuri Pankov 				conf.manpath.paths[i] = mandoc_strdup(argv[i]);
483a40ea1a7SYuri Pankov 		} else
484a40ea1a7SYuri Pankov 			manconf_parse(&conf, path_arg, NULL, NULL);
485a40ea1a7SYuri Pankov 
486a40ea1a7SYuri Pankov 		if (conf.manpath.sz == 0) {
487a40ea1a7SYuri Pankov 			exitcode = (int)MANDOCLEVEL_BADARG;
488a40ea1a7SYuri Pankov 			say("", "Empty manpath");
489a40ea1a7SYuri Pankov 		}
490a40ea1a7SYuri Pankov 
491a40ea1a7SYuri Pankov 		/*
492a40ea1a7SYuri Pankov 		 * First scan the tree rooted at a base directory, then
493a40ea1a7SYuri Pankov 		 * build a new database and finally move it into place.
494a40ea1a7SYuri Pankov 		 * Ignore zero-length directories and strip trailing
495a40ea1a7SYuri Pankov 		 * slashes.
496a40ea1a7SYuri Pankov 		 */
497a40ea1a7SYuri Pankov 		for (j = 0; j < conf.manpath.sz; j++) {
498a40ea1a7SYuri Pankov 			sz = strlen(conf.manpath.paths[j]);
499a40ea1a7SYuri Pankov 			if (sz && conf.manpath.paths[j][sz - 1] == '/')
500a40ea1a7SYuri Pankov 				conf.manpath.paths[j][--sz] = '\0';
501*4d131170SRobert Mustacchi 			if (sz == 0)
502a40ea1a7SYuri Pankov 				continue;
503a40ea1a7SYuri Pankov 
504a40ea1a7SYuri Pankov 			if (j) {
505a40ea1a7SYuri Pankov 				mandoc_ohash_init(&mpages, 6,
506a40ea1a7SYuri Pankov 				    offsetof(struct mpage, inodev));
507a40ea1a7SYuri Pankov 				mandoc_ohash_init(&mlinks, 6,
508a40ea1a7SYuri Pankov 				    offsetof(struct mlink, file));
509a40ea1a7SYuri Pankov 			}
510a40ea1a7SYuri Pankov 
511*4d131170SRobert Mustacchi 			if (set_basedir(conf.manpath.paths[j], argc > 0) == 0)
512a40ea1a7SYuri Pankov 				continue;
513*4d131170SRobert Mustacchi 			if (treescan() == 0)
514a40ea1a7SYuri Pankov 				continue;
515a40ea1a7SYuri Pankov 			dba = dba_new(128);
516a40ea1a7SYuri Pankov 			mpages_merge(dba, mp);
517a40ea1a7SYuri Pankov 			if (nodb == 0)
518a40ea1a7SYuri Pankov 				dbwrite(dba);
519a40ea1a7SYuri Pankov 			dba_free(dba);
520a40ea1a7SYuri Pankov 
521a40ea1a7SYuri Pankov 			if (j + 1 < conf.manpath.sz) {
522a40ea1a7SYuri Pankov 				mpages_free();
523a40ea1a7SYuri Pankov 				ohash_delete(&mpages);
524a40ea1a7SYuri Pankov 				ohash_delete(&mlinks);
525a40ea1a7SYuri Pankov 			}
526a40ea1a7SYuri Pankov 		}
527a40ea1a7SYuri Pankov 	}
528a40ea1a7SYuri Pankov out:
529a40ea1a7SYuri Pankov 	manconf_free(&conf);
530a40ea1a7SYuri Pankov 	mparse_free(mp);
531a40ea1a7SYuri Pankov 	mchars_free();
532a40ea1a7SYuri Pankov 	mpages_free();
533a40ea1a7SYuri Pankov 	ohash_delete(&mpages);
534a40ea1a7SYuri Pankov 	ohash_delete(&mlinks);
535a40ea1a7SYuri Pankov 	return exitcode;
536a40ea1a7SYuri Pankov usage:
537a40ea1a7SYuri Pankov 	progname = getprogname();
538a40ea1a7SYuri Pankov 	fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
539a40ea1a7SYuri Pankov 			"       %s [-aDnpQ] [-Tutf8] dir ...\n"
540a40ea1a7SYuri Pankov 			"       %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
541a40ea1a7SYuri Pankov 			"       %s [-Dnp] -u dir [file ...]\n"
542a40ea1a7SYuri Pankov 			"       %s [-Q] -t file ...\n",
543a40ea1a7SYuri Pankov 		        progname, progname, progname, progname, progname);
544a40ea1a7SYuri Pankov 
545a40ea1a7SYuri Pankov 	return (int)MANDOCLEVEL_BADARG;
546a40ea1a7SYuri Pankov }
547a40ea1a7SYuri Pankov 
548a40ea1a7SYuri Pankov /*
549a40ea1a7SYuri Pankov  * To get a singly linked list in alpha order while inserting entries
550a40ea1a7SYuri Pankov  * at the beginning, process directory entries in reverse alpha order.
551a40ea1a7SYuri Pankov  */
552a40ea1a7SYuri Pankov static int
553a40ea1a7SYuri Pankov #if HAVE_FTS_COMPARE_CONST
fts_compare(const FTSENT * const * a,const FTSENT * const * b)554a40ea1a7SYuri Pankov fts_compare(const FTSENT *const *a, const FTSENT *const *b)
555a40ea1a7SYuri Pankov #else
556a40ea1a7SYuri Pankov fts_compare(const FTSENT **a, const FTSENT **b)
557a40ea1a7SYuri Pankov #endif
558a40ea1a7SYuri Pankov {
559a40ea1a7SYuri Pankov 	return -strcmp((*a)->fts_name, (*b)->fts_name);
560a40ea1a7SYuri Pankov }
561a40ea1a7SYuri Pankov 
562a40ea1a7SYuri Pankov /*
563a40ea1a7SYuri Pankov  * Scan a directory tree rooted at "basedir" for manpages.
564a40ea1a7SYuri Pankov  * We use fts(), scanning directory parts along the way for clues to our
565a40ea1a7SYuri Pankov  * section and architecture.
566a40ea1a7SYuri Pankov  *
567a40ea1a7SYuri Pankov  * If use_all has been specified, grok all files.
568a40ea1a7SYuri Pankov  * If not, sanitise paths to the following:
569a40ea1a7SYuri Pankov  *
570a40ea1a7SYuri Pankov  *   [./]man*[/<arch>]/<name>.<section>
571a40ea1a7SYuri Pankov  *   or
572a40ea1a7SYuri Pankov  *   [./]cat<section>[/<arch>]/<name>.0
573a40ea1a7SYuri Pankov  *
574a40ea1a7SYuri Pankov  * TODO: accommodate for multi-language directories.
575a40ea1a7SYuri Pankov  */
576a40ea1a7SYuri Pankov static int
treescan(void)577a40ea1a7SYuri Pankov treescan(void)
578a40ea1a7SYuri Pankov {
579a40ea1a7SYuri Pankov 	char		 buf[PATH_MAX];
580a40ea1a7SYuri Pankov 	FTS		*f;
581a40ea1a7SYuri Pankov 	FTSENT		*ff;
582a40ea1a7SYuri Pankov 	struct mlink	*mlink;
583a40ea1a7SYuri Pankov 	int		 gzip;
584a40ea1a7SYuri Pankov 	enum form	 dform;
585a40ea1a7SYuri Pankov 	char		*dsec, *arch, *fsec, *cp;
586a40ea1a7SYuri Pankov 	const char	*path;
587a40ea1a7SYuri Pankov 	const char	*argv[2];
588a40ea1a7SYuri Pankov 
589a40ea1a7SYuri Pankov 	argv[0] = ".";
590a40ea1a7SYuri Pankov 	argv[1] = NULL;
591a40ea1a7SYuri Pankov 
592a40ea1a7SYuri Pankov 	f = fts_open((char * const *)argv, FTS_PHYSICAL | FTS_NOCHDIR,
593a40ea1a7SYuri Pankov 	    fts_compare);
594a40ea1a7SYuri Pankov 	if (f == NULL) {
595a40ea1a7SYuri Pankov 		exitcode = (int)MANDOCLEVEL_SYSERR;
596a40ea1a7SYuri Pankov 		say("", "&fts_open");
597a40ea1a7SYuri Pankov 		return 0;
598a40ea1a7SYuri Pankov 	}
599a40ea1a7SYuri Pankov 
600a40ea1a7SYuri Pankov 	dsec = arch = NULL;
601a40ea1a7SYuri Pankov 	dform = FORM_NONE;
602a40ea1a7SYuri Pankov 
603a40ea1a7SYuri Pankov 	while ((ff = fts_read(f)) != NULL) {
604a40ea1a7SYuri Pankov 		path = ff->fts_path + 2;
605a40ea1a7SYuri Pankov 		switch (ff->fts_info) {
606a40ea1a7SYuri Pankov 
607a40ea1a7SYuri Pankov 		/*
608a40ea1a7SYuri Pankov 		 * Symbolic links require various sanity checks,
609a40ea1a7SYuri Pankov 		 * then get handled just like regular files.
610a40ea1a7SYuri Pankov 		 */
611a40ea1a7SYuri Pankov 		case FTS_SL:
612a40ea1a7SYuri Pankov 			if (realpath(path, buf) == NULL) {
613a40ea1a7SYuri Pankov 				if (warnings)
614a40ea1a7SYuri Pankov 					say(path, "&realpath");
615a40ea1a7SYuri Pankov 				continue;
616a40ea1a7SYuri Pankov 			}
617*4d131170SRobert Mustacchi 			if (strncmp(buf, basedir, basedir_len) != 0
618*4d131170SRobert Mustacchi #ifdef READ_ALLOWED_PATH
619*4d131170SRobert Mustacchi 			    && !read_allowed(buf)
620a40ea1a7SYuri Pankov #endif
621a40ea1a7SYuri Pankov 			) {
622a40ea1a7SYuri Pankov 				if (warnings) say("",
623a40ea1a7SYuri Pankov 				    "%s: outside base directory", buf);
624a40ea1a7SYuri Pankov 				continue;
625a40ea1a7SYuri Pankov 			}
626a40ea1a7SYuri Pankov 			/* Use logical inode to avoid mpages dupe. */
627a40ea1a7SYuri Pankov 			if (stat(path, ff->fts_statp) == -1) {
628a40ea1a7SYuri Pankov 				if (warnings)
629a40ea1a7SYuri Pankov 					say(path, "&stat");
630a40ea1a7SYuri Pankov 				continue;
631a40ea1a7SYuri Pankov 			}
632*4d131170SRobert Mustacchi 			if ((ff->fts_statp->st_mode & S_IFMT) != S_IFREG)
633*4d131170SRobert Mustacchi 				continue;
634a40ea1a7SYuri Pankov 			/* FALLTHROUGH */
635a40ea1a7SYuri Pankov 
636a40ea1a7SYuri Pankov 		/*
637a40ea1a7SYuri Pankov 		 * If we're a regular file, add an mlink by using the
638a40ea1a7SYuri Pankov 		 * stored directory data and handling the filename.
639a40ea1a7SYuri Pankov 		 */
640a40ea1a7SYuri Pankov 		case FTS_F:
641a40ea1a7SYuri Pankov 			if ( ! strcmp(path, MANDOC_DB))
642a40ea1a7SYuri Pankov 				continue;
643a40ea1a7SYuri Pankov 			if ( ! use_all && ff->fts_level < 2) {
644a40ea1a7SYuri Pankov 				if (warnings)
645a40ea1a7SYuri Pankov 					say(path, "Extraneous file");
646a40ea1a7SYuri Pankov 				continue;
647a40ea1a7SYuri Pankov 			}
648a40ea1a7SYuri Pankov 			gzip = 0;
649a40ea1a7SYuri Pankov 			fsec = NULL;
650a40ea1a7SYuri Pankov 			while (fsec == NULL) {
651a40ea1a7SYuri Pankov 				fsec = strrchr(ff->fts_name, '.');
652a40ea1a7SYuri Pankov 				if (fsec == NULL || strcmp(fsec+1, "gz"))
653a40ea1a7SYuri Pankov 					break;
654a40ea1a7SYuri Pankov 				gzip = 1;
655a40ea1a7SYuri Pankov 				*fsec = '\0';
656a40ea1a7SYuri Pankov 				fsec = NULL;
657a40ea1a7SYuri Pankov 			}
658a40ea1a7SYuri Pankov 			if (fsec == NULL) {
659a40ea1a7SYuri Pankov 				if ( ! use_all) {
660a40ea1a7SYuri Pankov 					if (warnings)
661a40ea1a7SYuri Pankov 						say(path,
662a40ea1a7SYuri Pankov 						    "No filename suffix");
663a40ea1a7SYuri Pankov 					continue;
664a40ea1a7SYuri Pankov 				}
665a40ea1a7SYuri Pankov 			} else if ( ! strcmp(++fsec, "html")) {
666a40ea1a7SYuri Pankov 				if (warnings)
667a40ea1a7SYuri Pankov 					say(path, "Skip html");
668a40ea1a7SYuri Pankov 				continue;
669a40ea1a7SYuri Pankov 			} else if ( ! strcmp(fsec, "ps")) {
670a40ea1a7SYuri Pankov 				if (warnings)
671a40ea1a7SYuri Pankov 					say(path, "Skip ps");
672a40ea1a7SYuri Pankov 				continue;
673a40ea1a7SYuri Pankov 			} else if ( ! strcmp(fsec, "pdf")) {
674a40ea1a7SYuri Pankov 				if (warnings)
675a40ea1a7SYuri Pankov 					say(path, "Skip pdf");
676a40ea1a7SYuri Pankov 				continue;
677a40ea1a7SYuri Pankov 			} else if ( ! use_all &&
678a40ea1a7SYuri Pankov 			    ((dform == FORM_SRC &&
679a40ea1a7SYuri Pankov 			      strncmp(fsec, dsec, strlen(dsec))) ||
680a40ea1a7SYuri Pankov 			     (dform == FORM_CAT && strcmp(fsec, "0")))) {
681a40ea1a7SYuri Pankov 				if (warnings)
682a40ea1a7SYuri Pankov 					say(path, "Wrong filename suffix");
683a40ea1a7SYuri Pankov 				continue;
684a40ea1a7SYuri Pankov 			} else
685a40ea1a7SYuri Pankov 				fsec[-1] = '\0';
686a40ea1a7SYuri Pankov 
687a40ea1a7SYuri Pankov 			mlink = mandoc_calloc(1, sizeof(struct mlink));
688a40ea1a7SYuri Pankov 			if (strlcpy(mlink->file, path,
689a40ea1a7SYuri Pankov 			    sizeof(mlink->file)) >=
690a40ea1a7SYuri Pankov 			    sizeof(mlink->file)) {
691a40ea1a7SYuri Pankov 				say(path, "Filename too long");
692a40ea1a7SYuri Pankov 				free(mlink);
693a40ea1a7SYuri Pankov 				continue;
694a40ea1a7SYuri Pankov 			}
695a40ea1a7SYuri Pankov 			mlink->dform = dform;
696a40ea1a7SYuri Pankov 			mlink->dsec = dsec;
697a40ea1a7SYuri Pankov 			mlink->arch = arch;
698a40ea1a7SYuri Pankov 			mlink->name = ff->fts_name;
699a40ea1a7SYuri Pankov 			mlink->fsec = fsec;
700a40ea1a7SYuri Pankov 			mlink->gzip = gzip;
701a40ea1a7SYuri Pankov 			mlink_add(mlink, ff->fts_statp);
702a40ea1a7SYuri Pankov 			continue;
703a40ea1a7SYuri Pankov 
704a40ea1a7SYuri Pankov 		case FTS_D:
705a40ea1a7SYuri Pankov 		case FTS_DP:
706a40ea1a7SYuri Pankov 			break;
707a40ea1a7SYuri Pankov 
708a40ea1a7SYuri Pankov 		default:
709a40ea1a7SYuri Pankov 			if (warnings)
710a40ea1a7SYuri Pankov 				say(path, "Not a regular file");
711a40ea1a7SYuri Pankov 			continue;
712a40ea1a7SYuri Pankov 		}
713a40ea1a7SYuri Pankov 
714a40ea1a7SYuri Pankov 		switch (ff->fts_level) {
715a40ea1a7SYuri Pankov 		case 0:
716a40ea1a7SYuri Pankov 			/* Ignore the root directory. */
717a40ea1a7SYuri Pankov 			break;
718a40ea1a7SYuri Pankov 		case 1:
719a40ea1a7SYuri Pankov 			/*
720a40ea1a7SYuri Pankov 			 * This might contain manX/ or catX/.
721a40ea1a7SYuri Pankov 			 * Try to infer this from the name.
722a40ea1a7SYuri Pankov 			 * If we're not in use_all, enforce it.
723a40ea1a7SYuri Pankov 			 */
724a40ea1a7SYuri Pankov 			cp = ff->fts_name;
725a40ea1a7SYuri Pankov 			if (ff->fts_info == FTS_DP) {
726a40ea1a7SYuri Pankov 				dform = FORM_NONE;
727a40ea1a7SYuri Pankov 				dsec = NULL;
728a40ea1a7SYuri Pankov 				break;
729a40ea1a7SYuri Pankov 			}
730a40ea1a7SYuri Pankov 
731a40ea1a7SYuri Pankov 			if ( ! strncmp(cp, "man", 3)) {
732a40ea1a7SYuri Pankov 				dform = FORM_SRC;
733a40ea1a7SYuri Pankov 				dsec = cp + 3;
734a40ea1a7SYuri Pankov 			} else if ( ! strncmp(cp, "cat", 3)) {
735a40ea1a7SYuri Pankov 				dform = FORM_CAT;
736a40ea1a7SYuri Pankov 				dsec = cp + 3;
737a40ea1a7SYuri Pankov 			} else {
738a40ea1a7SYuri Pankov 				dform = FORM_NONE;
739a40ea1a7SYuri Pankov 				dsec = NULL;
740a40ea1a7SYuri Pankov 			}
741a40ea1a7SYuri Pankov 
742a40ea1a7SYuri Pankov 			if (dsec != NULL || use_all)
743a40ea1a7SYuri Pankov 				break;
744a40ea1a7SYuri Pankov 
745a40ea1a7SYuri Pankov 			if (warnings)
746a40ea1a7SYuri Pankov 				say(path, "Unknown directory part");
747a40ea1a7SYuri Pankov 			fts_set(f, ff, FTS_SKIP);
748a40ea1a7SYuri Pankov 			break;
749a40ea1a7SYuri Pankov 		case 2:
750a40ea1a7SYuri Pankov 			/*
751a40ea1a7SYuri Pankov 			 * Possibly our architecture.
752a40ea1a7SYuri Pankov 			 * If we're descending, keep tabs on it.
753a40ea1a7SYuri Pankov 			 */
754a40ea1a7SYuri Pankov 			if (ff->fts_info != FTS_DP && dsec != NULL)
755a40ea1a7SYuri Pankov 				arch = ff->fts_name;
756a40ea1a7SYuri Pankov 			else
757a40ea1a7SYuri Pankov 				arch = NULL;
758a40ea1a7SYuri Pankov 			break;
759a40ea1a7SYuri Pankov 		default:
760a40ea1a7SYuri Pankov 			if (ff->fts_info == FTS_DP || use_all)
761a40ea1a7SYuri Pankov 				break;
762a40ea1a7SYuri Pankov 			if (warnings)
763a40ea1a7SYuri Pankov 				say(path, "Extraneous directory part");
764a40ea1a7SYuri Pankov 			fts_set(f, ff, FTS_SKIP);
765a40ea1a7SYuri Pankov 			break;
766a40ea1a7SYuri Pankov 		}
767a40ea1a7SYuri Pankov 	}
768a40ea1a7SYuri Pankov 
769a40ea1a7SYuri Pankov 	fts_close(f);
770a40ea1a7SYuri Pankov 	return 1;
771a40ea1a7SYuri Pankov }
772a40ea1a7SYuri Pankov 
773a40ea1a7SYuri Pankov /*
774a40ea1a7SYuri Pankov  * Add a file to the mlinks table.
775a40ea1a7SYuri Pankov  * Do not verify that it's a "valid" looking manpage (we'll do that
776a40ea1a7SYuri Pankov  * later).
777a40ea1a7SYuri Pankov  *
778a40ea1a7SYuri Pankov  * Try to infer the manual section, architecture, and page name from the
779a40ea1a7SYuri Pankov  * path, assuming it looks like
780a40ea1a7SYuri Pankov  *
781a40ea1a7SYuri Pankov  *   [./]man*[/<arch>]/<name>.<section>
782a40ea1a7SYuri Pankov  *   or
783a40ea1a7SYuri Pankov  *   [./]cat<section>[/<arch>]/<name>.0
784a40ea1a7SYuri Pankov  *
785a40ea1a7SYuri Pankov  * See treescan() for the fts(3) version of this.
786a40ea1a7SYuri Pankov  */
787a40ea1a7SYuri Pankov static void
filescan(const char * infile)788*4d131170SRobert Mustacchi filescan(const char *infile)
789a40ea1a7SYuri Pankov {
790a40ea1a7SYuri Pankov 	struct stat	 st;
791a40ea1a7SYuri Pankov 	struct mlink	*mlink;
792*4d131170SRobert Mustacchi 	char		*linkfile, *p, *realdir, *start, *usefile;
793*4d131170SRobert Mustacchi 	size_t		 realdir_len;
794a40ea1a7SYuri Pankov 
795a40ea1a7SYuri Pankov 	assert(use_all);
796a40ea1a7SYuri Pankov 
797*4d131170SRobert Mustacchi 	if (strncmp(infile, "./", 2) == 0)
798*4d131170SRobert Mustacchi 		infile += 2;
799a40ea1a7SYuri Pankov 
800a40ea1a7SYuri Pankov 	/*
801a40ea1a7SYuri Pankov 	 * We have to do lstat(2) before realpath(3) loses
802a40ea1a7SYuri Pankov 	 * the information whether this is a symbolic link.
803a40ea1a7SYuri Pankov 	 * We need to know that because for symbolic links,
804a40ea1a7SYuri Pankov 	 * we want to use the orginal file name, while for
805a40ea1a7SYuri Pankov 	 * regular files, we want to use the real path.
806a40ea1a7SYuri Pankov 	 */
807*4d131170SRobert Mustacchi 	if (lstat(infile, &st) == -1) {
808a40ea1a7SYuri Pankov 		exitcode = (int)MANDOCLEVEL_BADARG;
809*4d131170SRobert Mustacchi 		say(infile, "&lstat");
810a40ea1a7SYuri Pankov 		return;
811*4d131170SRobert Mustacchi 	} else if (S_ISREG(st.st_mode) == 0 && S_ISLNK(st.st_mode) == 0) {
812a40ea1a7SYuri Pankov 		exitcode = (int)MANDOCLEVEL_BADARG;
813*4d131170SRobert Mustacchi 		say(infile, "Not a regular file");
814a40ea1a7SYuri Pankov 		return;
815a40ea1a7SYuri Pankov 	}
816a40ea1a7SYuri Pankov 
817a40ea1a7SYuri Pankov 	/*
818a40ea1a7SYuri Pankov 	 * We have to resolve the file name to the real path
819a40ea1a7SYuri Pankov 	 * in any case for the base directory check.
820a40ea1a7SYuri Pankov 	 */
821*4d131170SRobert Mustacchi 	if ((usefile = realpath(infile, NULL)) == NULL) {
822a40ea1a7SYuri Pankov 		exitcode = (int)MANDOCLEVEL_BADARG;
823*4d131170SRobert Mustacchi 		say(infile, "&realpath");
824a40ea1a7SYuri Pankov 		return;
825a40ea1a7SYuri Pankov 	}
826a40ea1a7SYuri Pankov 
827*4d131170SRobert Mustacchi 	if (op == OP_TEST)
828*4d131170SRobert Mustacchi 		start = usefile;
829*4d131170SRobert Mustacchi 	else if (strncmp(usefile, basedir, basedir_len) == 0)
830*4d131170SRobert Mustacchi 		start = usefile + basedir_len;
831*4d131170SRobert Mustacchi #ifdef READ_ALLOWED_PATH
832*4d131170SRobert Mustacchi 	else if (read_allowed(usefile))
833*4d131170SRobert Mustacchi 		start = usefile;
834a40ea1a7SYuri Pankov #endif
835a40ea1a7SYuri Pankov 	else {
836a40ea1a7SYuri Pankov 		exitcode = (int)MANDOCLEVEL_BADARG;
837*4d131170SRobert Mustacchi 		say("", "%s: outside base directory", infile);
838*4d131170SRobert Mustacchi 		free(usefile);
839a40ea1a7SYuri Pankov 		return;
840a40ea1a7SYuri Pankov 	}
841a40ea1a7SYuri Pankov 
842a40ea1a7SYuri Pankov 	/*
843a40ea1a7SYuri Pankov 	 * Now we are sure the file is inside our tree.
844a40ea1a7SYuri Pankov 	 * If it is a symbolic link, ignore the real path
845a40ea1a7SYuri Pankov 	 * and use the original name.
846a40ea1a7SYuri Pankov 	 */
847*4d131170SRobert Mustacchi 	do {
848*4d131170SRobert Mustacchi 		if (S_ISLNK(st.st_mode) == 0)
849*4d131170SRobert Mustacchi 			break;
850*4d131170SRobert Mustacchi 
851*4d131170SRobert Mustacchi 		/*
852*4d131170SRobert Mustacchi 		 * Some implementations of realpath(3) may succeed
853*4d131170SRobert Mustacchi 		 * even if the target of the link does not exist,
854*4d131170SRobert Mustacchi 		 * so check again for extra safety.
855*4d131170SRobert Mustacchi 		 */
856*4d131170SRobert Mustacchi 		if (stat(usefile, &st) == -1) {
857a40ea1a7SYuri Pankov 			exitcode = (int)MANDOCLEVEL_BADARG;
858*4d131170SRobert Mustacchi 			say(infile, "&stat");
859*4d131170SRobert Mustacchi 			free(usefile);
860a40ea1a7SYuri Pankov 			return;
861a40ea1a7SYuri Pankov 		}
862*4d131170SRobert Mustacchi 		linkfile = mandoc_strdup(infile);
863*4d131170SRobert Mustacchi 		if (op == OP_TEST) {
864*4d131170SRobert Mustacchi 			free(usefile);
865*4d131170SRobert Mustacchi 			start = usefile = linkfile;
866*4d131170SRobert Mustacchi 			break;
867a40ea1a7SYuri Pankov 		}
868*4d131170SRobert Mustacchi 		if (strncmp(infile, basedir, basedir_len) == 0) {
869*4d131170SRobert Mustacchi 			free(usefile);
870*4d131170SRobert Mustacchi 			usefile = linkfile;
871*4d131170SRobert Mustacchi 			start = usefile + basedir_len;
872*4d131170SRobert Mustacchi 			break;
873*4d131170SRobert Mustacchi 		}
874*4d131170SRobert Mustacchi 
875*4d131170SRobert Mustacchi 		/*
876*4d131170SRobert Mustacchi 		 * This symbolic link points into the basedir
877*4d131170SRobert Mustacchi 		 * from the outside.  Let's see whether any of
878*4d131170SRobert Mustacchi 		 * the parent directories resolve to the basedir.
879*4d131170SRobert Mustacchi 		 */
880*4d131170SRobert Mustacchi 		p = strchr(linkfile, '\0');
881*4d131170SRobert Mustacchi 		do {
882*4d131170SRobert Mustacchi 			while (*--p != '/')
883*4d131170SRobert Mustacchi 				continue;
884*4d131170SRobert Mustacchi 			*p = '\0';
885*4d131170SRobert Mustacchi 			if ((realdir = realpath(linkfile, NULL)) == NULL) {
886*4d131170SRobert Mustacchi 				exitcode = (int)MANDOCLEVEL_BADARG;
887*4d131170SRobert Mustacchi 				say(infile, "&realpath");
888*4d131170SRobert Mustacchi 				free(linkfile);
889*4d131170SRobert Mustacchi 				free(usefile);
890*4d131170SRobert Mustacchi 				return;
891*4d131170SRobert Mustacchi 			}
892*4d131170SRobert Mustacchi 			realdir_len = strlen(realdir) + 1;
893*4d131170SRobert Mustacchi 			free(realdir);
894*4d131170SRobert Mustacchi 			*p = '/';
895*4d131170SRobert Mustacchi 		} while (realdir_len > basedir_len);
896*4d131170SRobert Mustacchi 
897*4d131170SRobert Mustacchi 		/*
898*4d131170SRobert Mustacchi 		 * If one of the directories resolves to the basedir,
899*4d131170SRobert Mustacchi 		 * use the rest of the original name.
900*4d131170SRobert Mustacchi 		 * Otherwise, the best we can do
901*4d131170SRobert Mustacchi 		 * is to use the filename pointed to.
902*4d131170SRobert Mustacchi 		 */
903*4d131170SRobert Mustacchi 		if (realdir_len == basedir_len) {
904*4d131170SRobert Mustacchi 			free(usefile);
905*4d131170SRobert Mustacchi 			usefile = linkfile;
906*4d131170SRobert Mustacchi 			start = p + 1;
907*4d131170SRobert Mustacchi 		} else {
908*4d131170SRobert Mustacchi 			free(linkfile);
909*4d131170SRobert Mustacchi 			start = usefile + basedir_len;
910*4d131170SRobert Mustacchi 		}
911*4d131170SRobert Mustacchi 	} while (/* CONSTCOND */ 0);
912a40ea1a7SYuri Pankov 
913a40ea1a7SYuri Pankov 	mlink = mandoc_calloc(1, sizeof(struct mlink));
914a40ea1a7SYuri Pankov 	mlink->dform = FORM_NONE;
915a40ea1a7SYuri Pankov 	if (strlcpy(mlink->file, start, sizeof(mlink->file)) >=
916a40ea1a7SYuri Pankov 	    sizeof(mlink->file)) {
917a40ea1a7SYuri Pankov 		say(start, "Filename too long");
918a40ea1a7SYuri Pankov 		free(mlink);
919