xref: /openbsd-src/usr.bin/mandoc/main.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /*	$OpenBSD: main.c,v 1.221 2019/01/11 17:03:43 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2010-2012, 2014-2019 Ingo Schwarze <schwarze@openbsd.org>
5  * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.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 
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/param.h>	/* MACHINE */
23 #include <sys/termios.h>
24 #include <sys/wait.h>
25 
26 #include <assert.h>
27 #include <ctype.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <glob.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39 
40 #include "mandoc_aux.h"
41 #include "mandoc.h"
42 #include "mandoc_xr.h"
43 #include "roff.h"
44 #include "mdoc.h"
45 #include "man.h"
46 #include "mandoc_parse.h"
47 #include "tag.h"
48 #include "main.h"
49 #include "manconf.h"
50 #include "mansearch.h"
51 
52 enum	outmode {
53 	OUTMODE_DEF = 0,
54 	OUTMODE_FLN,
55 	OUTMODE_LST,
56 	OUTMODE_ALL,
57 	OUTMODE_ONE
58 };
59 
60 enum	outt {
61 	OUTT_ASCII = 0,	/* -Tascii */
62 	OUTT_LOCALE,	/* -Tlocale */
63 	OUTT_UTF8,	/* -Tutf8 */
64 	OUTT_TREE,	/* -Ttree */
65 	OUTT_MAN,	/* -Tman */
66 	OUTT_HTML,	/* -Thtml */
67 	OUTT_MARKDOWN,	/* -Tmarkdown */
68 	OUTT_LINT,	/* -Tlint */
69 	OUTT_PS,	/* -Tps */
70 	OUTT_PDF	/* -Tpdf */
71 };
72 
73 struct	curparse {
74 	struct mparse	 *mp;
75 	struct manoutput *outopts;	/* output options */
76 	void		 *outdata;	/* data for output */
77 	char		 *os_s;		/* operating system for display */
78 	int		  wstop;	/* stop after a file with a warning */
79 	enum mandoc_os	  os_e;		/* check base system conventions */
80 	enum outt	  outtype;	/* which output to use */
81 };
82 
83 
84 int			  mandocdb(int, char *[]);
85 
86 static	void		  check_xr(void);
87 static	int		  fs_lookup(const struct manpaths *,
88 				size_t ipath, const char *,
89 				const char *, const char *,
90 				struct manpage **, size_t *);
91 static	int		  fs_search(const struct mansearch *,
92 				const struct manpaths *, int, char**,
93 				struct manpage **, size_t *);
94 static	int		  koptions(int *, char *);
95 static	void		  moptions(int *, char *);
96 static	void		  outdata_alloc(struct curparse *);
97 static	void		  parse(struct curparse *, int, const char *);
98 static	void		  passthrough(const char *, int, int);
99 static	pid_t		  spawn_pager(struct tag_files *);
100 static	int		  toptions(struct curparse *, char *);
101 static	void		  usage(enum argmode) __attribute__((__noreturn__));
102 static	int		  woptions(struct curparse *, char *);
103 
104 static	const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
105 static	char		  help_arg[] = "help";
106 static	char		 *help_argv[] = {help_arg, NULL};
107 
108 
109 int
110 main(int argc, char *argv[])
111 {
112 	struct manconf	 conf;
113 	struct mansearch search;
114 	struct curparse	 curp;
115 	struct winsize	 ws;
116 	struct tag_files *tag_files;
117 	struct manpage	*res, *resp;
118 	const char	*progname, *sec, *thisarg;
119 	char		*conf_file, *defpaths, *auxpaths;
120 	char		*oarg, *tagarg;
121 	unsigned char	*uc;
122 	size_t		 i, sz;
123 	int		 prio, best_prio;
124 	enum outmode	 outmode;
125 	int		 fd, startdir;
126 	int		 show_usage;
127 	int		 options;
128 	int		 use_pager;
129 	int		 status, signum;
130 	int		 c;
131 	pid_t		 pager_pid, tc_pgid, man_pgid, pid;
132 
133 	progname = getprogname();
134 	mandoc_msg_setoutfile(stderr);
135 	if (strncmp(progname, "mandocdb", 8) == 0 ||
136 	    strncmp(progname, "makewhatis", 10) == 0)
137 		return mandocdb(argc, argv);
138 
139 	if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
140 		err((int)MANDOCLEVEL_SYSERR, "pledge");
141 
142 	/* Search options. */
143 
144 	memset(&conf, 0, sizeof(conf));
145 	conf_file = defpaths = NULL;
146 	auxpaths = NULL;
147 
148 	memset(&search, 0, sizeof(struct mansearch));
149 	search.outkey = "Nd";
150 	oarg = NULL;
151 
152 	if (strcmp(progname, "man") == 0)
153 		search.argmode = ARG_NAME;
154 	else if (strncmp(progname, "apropos", 7) == 0)
155 		search.argmode = ARG_EXPR;
156 	else if (strncmp(progname, "whatis", 6) == 0)
157 		search.argmode = ARG_WORD;
158 	else if (strncmp(progname, "help", 4) == 0)
159 		search.argmode = ARG_NAME;
160 	else
161 		search.argmode = ARG_FILE;
162 
163 	/* Parser and formatter options. */
164 
165 	memset(&curp, 0, sizeof(struct curparse));
166 	curp.outtype = OUTT_LOCALE;
167 	curp.outopts = &conf.output;
168 	options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
169 
170 	use_pager = 1;
171 	tag_files = NULL;
172 	show_usage = 0;
173 	outmode = OUTMODE_DEF;
174 
175 	while ((c = getopt(argc, argv,
176 	    "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
177 		if (c == 'i' && search.argmode == ARG_EXPR) {
178 			optind--;
179 			break;
180 		}
181 		switch (c) {
182 		case 'a':
183 			outmode = OUTMODE_ALL;
184 			break;
185 		case 'C':
186 			conf_file = optarg;
187 			break;
188 		case 'c':
189 			use_pager = 0;
190 			break;
191 		case 'f':
192 			search.argmode = ARG_WORD;
193 			break;
194 		case 'h':
195 			conf.output.synopsisonly = 1;
196 			use_pager = 0;
197 			outmode = OUTMODE_ALL;
198 			break;
199 		case 'I':
200 			if (strncmp(optarg, "os=", 3)) {
201 				warnx("-I %s: Bad argument", optarg);
202 				return (int)MANDOCLEVEL_BADARG;
203 			}
204 			if (curp.os_s != NULL) {
205 				warnx("-I %s: Duplicate argument", optarg);
206 				return (int)MANDOCLEVEL_BADARG;
207 			}
208 			curp.os_s = mandoc_strdup(optarg + 3);
209 			break;
210 		case 'K':
211 			if ( ! koptions(&options, optarg))
212 				return (int)MANDOCLEVEL_BADARG;
213 			break;
214 		case 'k':
215 			search.argmode = ARG_EXPR;
216 			break;
217 		case 'l':
218 			search.argmode = ARG_FILE;
219 			outmode = OUTMODE_ALL;
220 			break;
221 		case 'M':
222 			defpaths = optarg;
223 			break;
224 		case 'm':
225 			auxpaths = optarg;
226 			break;
227 		case 'O':
228 			oarg = optarg;
229 			break;
230 		case 'S':
231 			search.arch = optarg;
232 			break;
233 		case 's':
234 			search.sec = optarg;
235 			break;
236 		case 'T':
237 			if ( ! toptions(&curp, optarg))
238 				return (int)MANDOCLEVEL_BADARG;
239 			break;
240 		case 'W':
241 			if ( ! woptions(&curp, optarg))
242 				return (int)MANDOCLEVEL_BADARG;
243 			break;
244 		case 'w':
245 			outmode = OUTMODE_FLN;
246 			break;
247 		default:
248 			show_usage = 1;
249 			break;
250 		}
251 	}
252 
253 	if (show_usage)
254 		usage(search.argmode);
255 
256 	/* Postprocess options. */
257 
258 	if (outmode == OUTMODE_DEF) {
259 		switch (search.argmode) {
260 		case ARG_FILE:
261 			outmode = OUTMODE_ALL;
262 			use_pager = 0;
263 			break;
264 		case ARG_NAME:
265 			outmode = OUTMODE_ONE;
266 			break;
267 		default:
268 			outmode = OUTMODE_LST;
269 			break;
270 		}
271 	}
272 
273 	if (oarg != NULL) {
274 		if (outmode == OUTMODE_LST)
275 			search.outkey = oarg;
276 		else {
277 			while (oarg != NULL) {
278 				thisarg = oarg;
279 				if (manconf_output(&conf.output,
280 				    strsep(&oarg, ","), 0) == 0)
281 					continue;
282 				warnx("-O %s: Bad argument", thisarg);
283 				return (int)MANDOCLEVEL_BADARG;
284 			}
285 		}
286 	}
287 
288 	if (curp.outtype != OUTT_TREE || !curp.outopts->noval)
289 		options |= MPARSE_VALIDATE;
290 
291 	if (outmode == OUTMODE_FLN ||
292 	    outmode == OUTMODE_LST ||
293 	    !isatty(STDOUT_FILENO))
294 		use_pager = 0;
295 
296 	if (use_pager &&
297 	    (conf.output.width == 0 || conf.output.indent == 0) &&
298 	    ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 &&
299 	    ws.ws_col > 1) {
300 		if (conf.output.width == 0 && ws.ws_col < 79)
301 			conf.output.width = ws.ws_col - 1;
302 		if (conf.output.indent == 0 && ws.ws_col < 66)
303 			conf.output.indent = 3;
304 	}
305 
306 	if (!use_pager)
307 		if (pledge("stdio rpath", NULL) == -1)
308 			err((int)MANDOCLEVEL_SYSERR, "pledge");
309 
310 	/* Parse arguments. */
311 
312 	if (argc > 0) {
313 		argc -= optind;
314 		argv += optind;
315 	}
316 	resp = NULL;
317 
318 	/*
319 	 * Quirks for help(1)
320 	 * and for a man(1) section argument without -s.
321 	 */
322 
323 	if (search.argmode == ARG_NAME) {
324 		if (*progname == 'h') {
325 			if (argc == 0) {
326 				argv = help_argv;
327 				argc = 1;
328 			}
329 		} else if (argc > 1 &&
330 		    ((uc = (unsigned char *)argv[0]) != NULL) &&
331 		    ((isdigit(uc[0]) && (uc[1] == '\0' ||
332 		      (isalpha(uc[1]) && uc[2] == '\0'))) ||
333 		     (uc[0] == 'n' && uc[1] == '\0'))) {
334 			search.sec = (char *)uc;
335 			argv++;
336 			argc--;
337 		}
338 		if (search.arch == NULL)
339 			search.arch = getenv("MACHINE");
340 		if (search.arch == NULL)
341 			search.arch = MACHINE;
342 	}
343 
344 	/*
345 	 * Use the first argument for -O tag in addition to
346 	 * using it as a search term for man(1) or apropos(1).
347 	 */
348 
349 	if (conf.output.tag != NULL && *conf.output.tag == '\0') {
350 		tagarg = argc > 0 && search.argmode == ARG_EXPR ?
351 		    strchr(*argv, '=') : NULL;
352 		conf.output.tag = tagarg == NULL ? *argv : tagarg + 1;
353 	}
354 
355 	/* man(1), whatis(1), apropos(1) */
356 
357 	if (search.argmode != ARG_FILE) {
358 		if (search.argmode == ARG_NAME &&
359 		    outmode == OUTMODE_ONE)
360 			search.firstmatch = 1;
361 
362 		/* Access the mandoc database. */
363 
364 		manconf_parse(&conf, conf_file, defpaths, auxpaths);
365 		if ( ! mansearch(&search, &conf.manpath,
366 		    argc, argv, &res, &sz))
367 			usage(search.argmode);
368 
369 		if (sz == 0 && search.argmode == ARG_NAME)
370 			fs_search(&search, &conf.manpath,
371 			    argc, argv, &res, &sz);
372 
373 		if (search.argmode == ARG_NAME) {
374 			for (c = 0; c < argc; c++) {
375 				if (strchr(argv[c], '/') == NULL)
376 					continue;
377 				if (access(argv[c], R_OK) == -1) {
378 					warn("%s", argv[c]);
379 					continue;
380 				}
381 				res = mandoc_reallocarray(res,
382 				    sz + 1, sizeof(*res));
383 				res[sz].file = mandoc_strdup(argv[c]);
384 				res[sz].names = NULL;
385 				res[sz].output = NULL;
386 				res[sz].ipath = SIZE_MAX;
387 				res[sz].sec = 10;
388 				res[sz].form = FORM_SRC;
389 				sz++;
390 			}
391 		}
392 
393 		if (sz == 0) {
394 			if (search.argmode != ARG_NAME)
395 				warnx("nothing appropriate");
396 			mandoc_msg_setrc(MANDOCLEVEL_BADARG);
397 			goto out;
398 		}
399 
400 		/*
401 		 * For standard man(1) and -a output mode,
402 		 * prepare for copying filename pointers
403 		 * into the program parameter array.
404 		 */
405 
406 		if (outmode == OUTMODE_ONE) {
407 			argc = 1;
408 			best_prio = 20;
409 		} else if (outmode == OUTMODE_ALL)
410 			argc = (int)sz;
411 
412 		/* Iterate all matching manuals. */
413 
414 		resp = res;
415 		for (i = 0; i < sz; i++) {
416 			if (outmode == OUTMODE_FLN)
417 				puts(res[i].file);
418 			else if (outmode == OUTMODE_LST)
419 				printf("%s - %s\n", res[i].names,
420 				    res[i].output == NULL ? "" :
421 				    res[i].output);
422 			else if (outmode == OUTMODE_ONE) {
423 				/* Search for the best section. */
424 				sec = res[i].file;
425 				sec += strcspn(sec, "123456789");
426 				if (sec[0] == '\0')
427 					continue;
428 				prio = sec_prios[sec[0] - '1'];
429 				if (sec[1] != '/')
430 					prio += 10;
431 				if (prio >= best_prio)
432 					continue;
433 				best_prio = prio;
434 				resp = res + i;
435 			}
436 		}
437 
438 		/*
439 		 * For man(1), -a and -i output mode, fall through
440 		 * to the main mandoc(1) code iterating files
441 		 * and running the parsers on each of them.
442 		 */
443 
444 		if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
445 			goto out;
446 	}
447 
448 	/* mandoc(1) */
449 
450 	if (use_pager) {
451 		if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
452 			err((int)MANDOCLEVEL_SYSERR, "pledge");
453 	} else {
454 		if (pledge("stdio rpath", NULL) == -1)
455 			err((int)MANDOCLEVEL_SYSERR, "pledge");
456 	}
457 
458 	if (search.argmode == ARG_FILE)
459 		moptions(&options, auxpaths);
460 
461 	mchars_alloc();
462 	curp.mp = mparse_alloc(options, curp.os_e, curp.os_s);
463 
464 	if (argc < 1) {
465 		if (use_pager) {
466 			tag_files = tag_init();
467 			tag_files->tagname = conf.output.tag;
468 		}
469 		thisarg = "<stdin>";
470 		mandoc_msg_setinfilename(thisarg);
471 		parse(&curp, STDIN_FILENO, thisarg);
472 		mandoc_msg_setinfilename(NULL);
473 	}
474 
475 	/*
476 	 * Remember the original working directory, if possible.
477 	 * This will be needed if some names on the command line
478 	 * are page names and some are relative file names.
479 	 * Do not error out if the current directory is not
480 	 * readable: Maybe it won't be needed after all.
481 	 */
482 	startdir = open(".", O_RDONLY | O_DIRECTORY);
483 
484 	while (argc > 0) {
485 
486 		/*
487 		 * Changing directories is not needed in ARG_FILE mode.
488 		 * Do it on a best-effort basis.  Even in case of
489 		 * failure, some functionality may still work.
490 		 */
491 		if (resp != NULL) {
492 			if (resp->ipath != SIZE_MAX)
493 				(void)chdir(conf.manpath.paths[resp->ipath]);
494 			else if (startdir != -1)
495 				(void)fchdir(startdir);
496 			thisarg = resp->file;
497 		} else
498 			thisarg = *argv;
499 
500 		fd = mparse_open(curp.mp, thisarg);
501 		if (fd != -1) {
502 			if (use_pager) {
503 				use_pager = 0;
504 				tag_files = tag_init();
505 				tag_files->tagname = conf.output.tag;
506 			}
507 
508 			mandoc_msg_setinfilename(thisarg);
509 			if (resp == NULL || resp->form == FORM_SRC)
510 				parse(&curp, fd, thisarg);
511 			else
512 				passthrough(resp->file, fd,
513 				    conf.output.synopsisonly);
514 			mandoc_msg_setinfilename(NULL);
515 
516 			if (ferror(stdout)) {
517 				if (tag_files != NULL) {
518 					warn("%s", tag_files->ofn);
519 					tag_unlink();
520 					tag_files = NULL;
521 				} else
522 					warn("stdout");
523 				mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
524 				break;
525 			}
526 
527 			if (argc > 1 && curp.outtype <= OUTT_UTF8) {
528 				if (curp.outdata == NULL)
529 					outdata_alloc(&curp);
530 				terminal_sepline(curp.outdata);
531 			}
532 		} else
533 			mandoc_msg(MANDOCERR_FILE, 0, 0,
534 			    "%s: %s", thisarg, strerror(errno));
535 
536 		if (curp.wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
537 			break;
538 
539 		if (resp != NULL)
540 			resp++;
541 		else
542 			argv++;
543 		if (--argc)
544 			mparse_reset(curp.mp);
545 	}
546 	if (startdir != -1) {
547 		(void)fchdir(startdir);
548 		close(startdir);
549 	}
550 
551 	if (curp.outdata != NULL) {
552 		switch (curp.outtype) {
553 		case OUTT_HTML:
554 			html_free(curp.outdata);
555 			break;
556 		case OUTT_UTF8:
557 		case OUTT_LOCALE:
558 		case OUTT_ASCII:
559 			ascii_free(curp.outdata);
560 			break;
561 		case OUTT_PDF:
562 		case OUTT_PS:
563 			pspdf_free(curp.outdata);
564 			break;
565 		default:
566 			break;
567 		}
568 	}
569 	mandoc_xr_free();
570 	mparse_free(curp.mp);
571 	mchars_free();
572 
573 out:
574 	if (search.argmode != ARG_FILE) {
575 		manconf_free(&conf);
576 		mansearch_free(res, sz);
577 	}
578 
579 	free(curp.os_s);
580 
581 	/*
582 	 * When using a pager, finish writing both temporary files,
583 	 * fork it, wait for the user to close it, and clean up.
584 	 */
585 
586 	if (tag_files != NULL) {
587 		fclose(stdout);
588 		tag_write();
589 		man_pgid = getpgid(0);
590 		tag_files->tcpgid = man_pgid == getpid() ?
591 		    getpgid(getppid()) : man_pgid;
592 		pager_pid = 0;
593 		signum = SIGSTOP;
594 		for (;;) {
595 
596 			/* Stop here until moved to the foreground. */
597 
598 			tc_pgid = tcgetpgrp(tag_files->ofd);
599 			if (tc_pgid != man_pgid) {
600 				if (tc_pgid == pager_pid) {
601 					(void)tcsetpgrp(tag_files->ofd,
602 					    man_pgid);
603 					if (signum == SIGTTIN)
604 						continue;
605 				} else
606 					tag_files->tcpgid = tc_pgid;
607 				kill(0, signum);
608 				continue;
609 			}
610 
611 			/* Once in the foreground, activate the pager. */
612 
613 			if (pager_pid) {
614 				(void)tcsetpgrp(tag_files->ofd, pager_pid);
615 				kill(pager_pid, SIGCONT);
616 			} else
617 				pager_pid = spawn_pager(tag_files);
618 
619 			/* Wait for the pager to stop or exit. */
620 
621 			while ((pid = waitpid(pager_pid, &status,
622 			    WUNTRACED)) == -1 && errno == EINTR)
623 				continue;
624 
625 			if (pid == -1) {
626 				warn("wait");
627 				mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
628 				break;
629 			}
630 			if (!WIFSTOPPED(status))
631 				break;
632 
633 			signum = WSTOPSIG(status);
634 		}
635 		tag_unlink();
636 	}
637 	return (int)mandoc_msg_getrc();
638 }
639 
640 static void
641 usage(enum argmode argmode)
642 {
643 
644 	switch (argmode) {
645 	case ARG_FILE:
646 		fputs("usage: mandoc [-ac] [-I os=name] "
647 		    "[-K encoding] [-mdoc | -man] [-O options]\n"
648 		    "\t      [-T output] [-W level] [file ...]\n", stderr);
649 		break;
650 	case ARG_NAME:
651 		fputs("usage: man [-acfhklw] [-C file] [-M path] "
652 		    "[-m path] [-S subsection]\n"
653 		    "\t   [[-s] section] name ...\n", stderr);
654 		break;
655 	case ARG_WORD:
656 		fputs("usage: whatis [-afk] [-C file] "
657 		    "[-M path] [-m path] [-O outkey] [-S arch]\n"
658 		    "\t      [-s section] name ...\n", stderr);
659 		break;
660 	case ARG_EXPR:
661 		fputs("usage: apropos [-afk] [-C file] "
662 		    "[-M path] [-m path] [-O outkey] [-S arch]\n"
663 		    "\t       [-s section] expression ...\n", stderr);
664 		break;
665 	}
666 	exit((int)MANDOCLEVEL_BADARG);
667 }
668 
669 static int
670 fs_lookup(const struct manpaths *paths, size_t ipath,
671 	const char *sec, const char *arch, const char *name,
672 	struct manpage **res, size_t *ressz)
673 {
674 	glob_t		 globinfo;
675 	struct manpage	*page;
676 	char		*file;
677 	int		 globres;
678 	enum form	 form;
679 
680 	form = FORM_SRC;
681 	mandoc_asprintf(&file, "%s/man%s/%s.%s",
682 	    paths->paths[ipath], sec, name, sec);
683 	if (access(file, R_OK) != -1)
684 		goto found;
685 	free(file);
686 
687 	mandoc_asprintf(&file, "%s/cat%s/%s.0",
688 	    paths->paths[ipath], sec, name);
689 	if (access(file, R_OK) != -1) {
690 		form = FORM_CAT;
691 		goto found;
692 	}
693 	free(file);
694 
695 	if (arch != NULL) {
696 		mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
697 		    paths->paths[ipath], sec, arch, name, sec);
698 		if (access(file, R_OK) != -1)
699 			goto found;
700 		free(file);
701 	}
702 
703 	mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
704 	    paths->paths[ipath], sec, name);
705 	globres = glob(file, 0, NULL, &globinfo);
706 	if (globres != 0 && globres != GLOB_NOMATCH)
707 		warn("%s: glob", file);
708 	free(file);
709 	if (globres == 0)
710 		file = mandoc_strdup(*globinfo.gl_pathv);
711 	globfree(&globinfo);
712 	if (globres == 0)
713 		goto found;
714 	if (res != NULL || ipath + 1 != paths->sz)
715 		return 0;
716 
717 	mandoc_asprintf(&file, "%s.%s", name, sec);
718 	globres = access(file, R_OK);
719 	free(file);
720 	return globres != -1;
721 
722 found:
723 	warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s",
724 	    name, sec, paths->paths[ipath]);
725 	if (res == NULL) {
726 		free(file);
727 		return 1;
728 	}
729 	*res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
730 	page = *res + (*ressz - 1);
731 	page->file = file;
732 	page->names = NULL;
733 	page->output = NULL;
734 	page->ipath = ipath;
735 	page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
736 	page->form = form;
737 	return 1;
738 }
739 
740 static int
741 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
742 	int argc, char **argv, struct manpage **res, size_t *ressz)
743 {
744 	const char *const sections[] =
745 	    {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
746 	const size_t nsec = sizeof(sections)/sizeof(sections[0]);
747 
748 	size_t		 ipath, isec, lastsz;
749 
750 	assert(cfg->argmode == ARG_NAME);
751 
752 	if (res != NULL)
753 		*res = NULL;
754 	*ressz = lastsz = 0;
755 	while (argc) {
756 		for (ipath = 0; ipath < paths->sz; ipath++) {
757 			if (cfg->sec != NULL) {
758 				if (fs_lookup(paths, ipath, cfg->sec,
759 				    cfg->arch, *argv, res, ressz) &&
760 				    cfg->firstmatch)
761 					return 1;
762 			} else for (isec = 0; isec < nsec; isec++)
763 				if (fs_lookup(paths, ipath, sections[isec],
764 				    cfg->arch, *argv, res, ressz) &&
765 				    cfg->firstmatch)
766 					return 1;
767 		}
768 		if (res != NULL && *ressz == lastsz &&
769 		    strchr(*argv, '/') == NULL) {
770 			if (cfg->sec == NULL)
771 				warnx("No entry for %s in the manual.",
772 				    *argv);
773 			else
774 				warnx("No entry for %s in section %s "
775 				    "of the manual.", *argv, cfg->sec);
776 		}
777 		lastsz = *ressz;
778 		argv++;
779 		argc--;
780 	}
781 	return 0;
782 }
783 
784 static void
785 parse(struct curparse *curp, int fd, const char *file)
786 {
787 	struct roff_meta *meta;
788 
789 	/* Begin by parsing the file itself. */
790 
791 	assert(file);
792 	assert(fd >= 0);
793 
794 	mparse_readfd(curp->mp, fd, file);
795 	if (fd != STDIN_FILENO)
796 		close(fd);
797 
798 	/*
799 	 * With -Wstop and warnings or errors of at least the requested
800 	 * level, do not produce output.
801 	 */
802 
803 	if (curp->wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
804 		return;
805 
806 	if (curp->outdata == NULL)
807 		outdata_alloc(curp);
808 
809 	mandoc_xr_reset();
810 	meta = mparse_result(curp->mp);
811 
812 	/* Execute the out device, if it exists. */
813 
814 	if (meta->macroset == MACROSET_MDOC) {
815 		switch (curp->outtype) {
816 		case OUTT_HTML:
817 			html_mdoc(curp->outdata, meta);
818 			break;
819 		case OUTT_TREE:
820 			tree_mdoc(curp->outdata, meta);
821 			break;
822 		case OUTT_MAN:
823 			man_mdoc(curp->outdata, meta);
824 			break;
825 		case OUTT_PDF:
826 		case OUTT_ASCII:
827 		case OUTT_UTF8:
828 		case OUTT_LOCALE:
829 		case OUTT_PS:
830 			terminal_mdoc(curp->outdata, meta);
831 			break;
832 		case OUTT_MARKDOWN:
833 			markdown_mdoc(curp->outdata, meta);
834 			break;
835 		default:
836 			break;
837 		}
838 	}
839 	if (meta->macroset == MACROSET_MAN) {
840 		switch (curp->outtype) {
841 		case OUTT_HTML:
842 			html_man(curp->outdata, meta);
843 			break;
844 		case OUTT_TREE:
845 			tree_man(curp->outdata, meta);
846 			break;
847 		case OUTT_MAN:
848 			mparse_copy(curp->mp);
849 			break;
850 		case OUTT_PDF:
851 		case OUTT_ASCII:
852 		case OUTT_UTF8:
853 		case OUTT_LOCALE:
854 		case OUTT_PS:
855 			terminal_man(curp->outdata, meta);
856 			break;
857 		default:
858 			break;
859 		}
860 	}
861 	if (mandoc_msg_getmin() < MANDOCERR_STYLE)
862 		check_xr();
863 }
864 
865 static void
866 check_xr(void)
867 {
868 	static struct manpaths	 paths;
869 	struct mansearch	 search;
870 	struct mandoc_xr	*xr;
871 	size_t			 sz;
872 
873 	if (paths.sz == 0)
874 		manpath_base(&paths);
875 
876 	for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) {
877 		if (xr->line == -1)
878 			continue;
879 		search.arch = NULL;
880 		search.sec = xr->sec;
881 		search.outkey = NULL;
882 		search.argmode = ARG_NAME;
883 		search.firstmatch = 1;
884 		if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz))
885 			continue;
886 		if (fs_search(&search, &paths, 1, &xr->name, NULL, &sz))
887 			continue;
888 		if (xr->count == 1)
889 			mandoc_msg(MANDOCERR_XR_BAD, xr->line,
890 			    xr->pos + 1, "Xr %s %s", xr->name, xr->sec);
891 		else
892 			mandoc_msg(MANDOCERR_XR_BAD, xr->line,
893 			    xr->pos + 1, "Xr %s %s (%d times)",
894 			    xr->name, xr->sec, xr->count);
895 	}
896 }
897 
898 static void
899 outdata_alloc(struct curparse *curp)
900 {
901 	switch (curp->outtype) {
902 	case OUTT_HTML:
903 		curp->outdata = html_alloc(curp->outopts);
904 		break;
905 	case OUTT_UTF8:
906 		curp->outdata = utf8_alloc(curp->outopts);
907 		break;
908 	case OUTT_LOCALE:
909 		curp->outdata = locale_alloc(curp->outopts);
910 		break;
911 	case OUTT_ASCII:
912 		curp->outdata = ascii_alloc(curp->outopts);
913 		break;
914 	case OUTT_PDF:
915 		curp->outdata = pdf_alloc(curp->outopts);
916 		break;
917 	case OUTT_PS:
918 		curp->outdata = ps_alloc(curp->outopts);
919 		break;
920 	default:
921 		break;
922 	}
923 }
924 
925 static void
926 passthrough(const char *file, int fd, int synopsis_only)
927 {
928 	const char	 synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
929 	const char	 synr[] = "SYNOPSIS";
930 
931 	FILE		*stream;
932 	const char	*syscall;
933 	char		*line, *cp;
934 	size_t		 linesz;
935 	ssize_t		 len, written;
936 	int		 print;
937 
938 	line = NULL;
939 	linesz = 0;
940 
941 	if (fflush(stdout) == EOF) {
942 		syscall = "fflush";
943 		goto fail;
944 	}
945 
946 	if ((stream = fdopen(fd, "r")) == NULL) {
947 		close(fd);
948 		syscall = "fdopen";
949 		goto fail;
950 	}
951 
952 	print = 0;
953 	while ((len = getline(&line, &linesz, stream)) != -1) {
954 		cp = line;
955 		if (synopsis_only) {
956 			if (print) {
957 				if ( ! isspace((unsigned char)*cp))
958 					goto done;
959 				while (isspace((unsigned char)*cp)) {
960 					cp++;
961 					len--;
962 				}
963 			} else {
964 				if (strcmp(cp, synb) == 0 ||
965 				    strcmp(cp, synr) == 0)
966 					print = 1;
967 				continue;
968 			}
969 		}
970 		for (; len > 0; len -= written) {
971 			if ((written = write(STDOUT_FILENO, cp, len)) != -1)
972 				continue;
973 			fclose(stream);
974 			syscall = "write";
975 			goto fail;
976 		}
977 	}
978 
979 	if (ferror(stream)) {
980 		fclose(stream);
981 		syscall = "getline";
982 		goto fail;
983 	}
984 
985 done:
986 	free(line);
987 	fclose(stream);
988 	return;
989 
990 fail:
991 	free(line);
992 	warn("%s: SYSERR: %s", file, syscall);
993 	mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
994 }
995 
996 static int
997 koptions(int *options, char *arg)
998 {
999 
1000 	if ( ! strcmp(arg, "utf-8")) {
1001 		*options |=  MPARSE_UTF8;
1002 		*options &= ~MPARSE_LATIN1;
1003 	} else if ( ! strcmp(arg, "iso-8859-1")) {
1004 		*options |=  MPARSE_LATIN1;
1005 		*options &= ~MPARSE_UTF8;
1006 	} else if ( ! strcmp(arg, "us-ascii")) {
1007 		*options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
1008 	} else {
1009 		warnx("-K %s: Bad argument", arg);
1010 		return 0;
1011 	}
1012 	return 1;
1013 }
1014 
1015 static void
1016 moptions(int *options, char *arg)
1017 {
1018 
1019 	if (arg == NULL)
1020 		return;
1021 	if (strcmp(arg, "doc") == 0)
1022 		*options |= MPARSE_MDOC;
1023 	else if (strcmp(arg, "an") == 0)
1024 		*options |= MPARSE_MAN;
1025 }
1026 
1027 static int
1028 toptions(struct curparse *curp, char *arg)
1029 {
1030 
1031 	if (0 == strcmp(arg, "ascii"))
1032 		curp->outtype = OUTT_ASCII;
1033 	else if (0 == strcmp(arg, "lint")) {
1034 		curp->outtype = OUTT_LINT;
1035 		mandoc_msg_setoutfile(stdout);
1036 		mandoc_msg_setmin(MANDOCERR_BASE);
1037 	} else if (0 == strcmp(arg, "tree"))
1038 		curp->outtype = OUTT_TREE;
1039 	else if (0 == strcmp(arg, "man"))
1040 		curp->outtype = OUTT_MAN;
1041 	else if (0 == strcmp(arg, "html"))
1042 		curp->outtype = OUTT_HTML;
1043 	else if (0 == strcmp(arg, "markdown"))
1044 		curp->outtype = OUTT_MARKDOWN;
1045 	else if (0 == strcmp(arg, "utf8"))
1046 		curp->outtype = OUTT_UTF8;
1047 	else if (0 == strcmp(arg, "locale"))
1048 		curp->outtype = OUTT_LOCALE;
1049 	else if (0 == strcmp(arg, "ps"))
1050 		curp->outtype = OUTT_PS;
1051 	else if (0 == strcmp(arg, "pdf"))
1052 		curp->outtype = OUTT_PDF;
1053 	else {
1054 		warnx("-T %s: Bad argument", arg);
1055 		return 0;
1056 	}
1057 
1058 	return 1;
1059 }
1060 
1061 static int
1062 woptions(struct curparse *curp, char *arg)
1063 {
1064 	char		*v, *o;
1065 	const char	*toks[11];
1066 
1067 	toks[0] = "stop";
1068 	toks[1] = "all";
1069 	toks[2] = "base";
1070 	toks[3] = "style";
1071 	toks[4] = "warning";
1072 	toks[5] = "error";
1073 	toks[6] = "unsupp";
1074 	toks[7] = "fatal";
1075 	toks[8] = "openbsd";
1076 	toks[9] = "netbsd";
1077 	toks[10] = NULL;
1078 
1079 	while (*arg) {
1080 		o = arg;
1081 		switch (getsubopt(&arg, (char * const *)toks, &v)) {
1082 		case 0:
1083 			curp->wstop = 1;
1084 			break;
1085 		case 1:
1086 		case 2:
1087 			mandoc_msg_setmin(MANDOCERR_BASE);
1088 			break;
1089 		case 3:
1090 			mandoc_msg_setmin(MANDOCERR_STYLE);
1091 			break;
1092 		case 4:
1093 			mandoc_msg_setmin(MANDOCERR_WARNING);
1094 			break;
1095 		case 5:
1096 			mandoc_msg_setmin(MANDOCERR_ERROR);
1097 			break;
1098 		case 6:
1099 			mandoc_msg_setmin(MANDOCERR_UNSUPP);
1100 			break;
1101 		case 7:
1102 			mandoc_msg_setmin(MANDOCERR_MAX);
1103 			break;
1104 		case 8:
1105 			mandoc_msg_setmin(MANDOCERR_BASE);
1106 			curp->os_e = MANDOC_OS_OPENBSD;
1107 			break;
1108 		case 9:
1109 			mandoc_msg_setmin(MANDOCERR_BASE);
1110 			curp->os_e = MANDOC_OS_NETBSD;
1111 			break;
1112 		default:
1113 			warnx("-W %s: Bad argument", o);
1114 			return 0;
1115 		}
1116 	}
1117 	return 1;
1118 }
1119 
1120 static pid_t
1121 spawn_pager(struct tag_files *tag_files)
1122 {
1123 	const struct timespec timeout = { 0, 100000000 };  /* 0.1s */
1124 #define MAX_PAGER_ARGS 16
1125 	char		*argv[MAX_PAGER_ARGS];
1126 	const char	*pager;
1127 	char		*cp;
1128 	size_t		 cmdlen;
1129 	int		 argc, use_ofn;
1130 	pid_t		 pager_pid;
1131 
1132 	pager = getenv("MANPAGER");
1133 	if (pager == NULL || *pager == '\0')
1134 		pager = getenv("PAGER");
1135 	if (pager == NULL || *pager == '\0')
1136 		pager = "more -s";
1137 	cp = mandoc_strdup(pager);
1138 
1139 	/*
1140 	 * Parse the pager command into words.
1141 	 * Intentionally do not do anything fancy here.
1142 	 */
1143 
1144 	argc = 0;
1145 	while (argc + 5 < MAX_PAGER_ARGS) {
1146 		argv[argc++] = cp;
1147 		cp = strchr(cp, ' ');
1148 		if (cp == NULL)
1149 			break;
1150 		*cp++ = '\0';
1151 		while (*cp == ' ')
1152 			cp++;
1153 		if (*cp == '\0')
1154 			break;
1155 	}
1156 
1157 	/* For more(1) and less(1), use the tag file. */
1158 
1159 	use_ofn = 1;
1160 	if ((cmdlen = strlen(argv[0])) >= 4) {
1161 		cp = argv[0] + cmdlen - 4;
1162 		if (strcmp(cp, "less") == 0 || strcmp(cp, "more") == 0) {
1163 			argv[argc++] = mandoc_strdup("-T");
1164 			argv[argc++] = tag_files->tfn;
1165 			if (tag_files->tagname != NULL) {
1166 				argv[argc++] = mandoc_strdup("-t");
1167 				argv[argc++] = tag_files->tagname;
1168 				use_ofn = 0;
1169 			}
1170 		}
1171 	}
1172 	if (use_ofn)
1173 		argv[argc++] = tag_files->ofn;
1174 	argv[argc] = NULL;
1175 
1176 	switch (pager_pid = fork()) {
1177 	case -1:
1178 		err((int)MANDOCLEVEL_SYSERR, "fork");
1179 	case 0:
1180 		break;
1181 	default:
1182 		(void)setpgid(pager_pid, 0);
1183 		(void)tcsetpgrp(tag_files->ofd, pager_pid);
1184 		if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
1185 			err((int)MANDOCLEVEL_SYSERR, "pledge");
1186 		tag_files->pager_pid = pager_pid;
1187 		return pager_pid;
1188 	}
1189 
1190 	/* The child process becomes the pager. */
1191 
1192 	if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
1193 		err((int)MANDOCLEVEL_SYSERR, "pager stdout");
1194 	close(tag_files->ofd);
1195 	assert(tag_files->tfd == -1);
1196 
1197 	/* Do not start the pager before controlling the terminal. */
1198 
1199 	while (tcgetpgrp(STDOUT_FILENO) != getpid())
1200 		nanosleep(&timeout, NULL);
1201 
1202 	execvp(argv[0], argv);
1203 	err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);
1204 }
1205