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