Home | History | Annotate | Line # | Download | only in man
man.c revision 1.27.2.2
      1 /*	$NetBSD: man.c,v 1.27.2.2 2002/11/03 19:46:26 he Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1993, 1994, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 
     38 #ifndef lint
     39 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
     40 	The Regents of the University of California.  All rights reserved.\n");
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 #if 0
     45 static char sccsid[] = "@(#)man.c	8.17 (Berkeley) 1/31/95";
     46 #else
     47 __RCSID("$NetBSD: man.c,v 1.27.2.2 2002/11/03 19:46:26 he Exp $");
     48 #endif
     49 #endif /* not lint */
     50 
     51 #include <sys/param.h>
     52 #include <sys/queue.h>
     53 #include <sys/utsname.h>
     54 
     55 #include <ctype.h>
     56 #include <err.h>
     57 #include <errno.h>
     58 #include <fcntl.h>
     59 #include <fnmatch.h>
     60 #include <glob.h>
     61 #include <signal.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 
     67 #include "manconf.h"
     68 #include "pathnames.h"
     69 
     70 int f_all, f_where;
     71 
     72 int		 main __P((int, char **));
     73 static void	 build_page __P((char *, char **));
     74 static void	 cat __P((char *));
     75 static const char	*check_pager __P((const char *));
     76 static int	 cleanup __P((void));
     77 static void	 how __P((char *));
     78 static void	 jump __P((char **, char *, char *));
     79 static int	 manual __P((char *, TAG *, glob_t *, const char *));
     80 static void	 onsig __P((int));
     81 static void	 usage __P((void));
     82 
     83 int
     84 main(argc, argv)
     85 	int argc;
     86 	char *argv[];
     87 {
     88 	TAG *defp, *section, *newpathp, *subp;
     89 	ENTRY *e_defp, *e_subp;
     90 	glob_t pg;
     91 	size_t len;
     92 	int ch, f_cat, f_how, found, abs_section;
     93 	char **ap, *cmd, *p, *p_add, *p_path;
     94 	const char *machine, *pager, *conffile, *pathsearch, *sectionname;
     95 	char buf[MAXPATHLEN * 2];
     96 
     97 #ifdef __GNUC__
     98 	pager = NULL;		/* XXX gcc -Wuninitialized */
     99 #endif
    100 
    101 	f_cat = f_how = 0;
    102 	sectionname = pathsearch = conffile = p_add = p_path = NULL;
    103 	while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:s:S:w")) != -1)
    104 		switch (ch) {
    105 		case 'a':
    106 			f_all = 1;
    107 			break;
    108 		case 'C':
    109 			conffile = optarg;
    110 			break;
    111 		case 'c':
    112 		case '-':		/* Deprecated. */
    113 			f_cat = 1;
    114 			break;
    115 		case 'h':
    116 			f_how = 1;
    117 			break;
    118 		case 'm':
    119 			p_add = optarg;
    120 			break;
    121 		case 'M':
    122 		case 'P':		/* Backward compatibility. */
    123 			p_path = strdup(optarg);
    124 			break;
    125 		/*
    126 		 * The -f and -k options are backward compatible,
    127 		 * undocumented ways of calling whatis(1) and apropos(1).
    128 		 */
    129 		case 'f':
    130 			jump(argv, "-f", "whatis");
    131 			/* NOTREACHED */
    132 		case 'k':
    133 			jump(argv, "-k", "apropos");
    134 			/* NOTREACHED */
    135 		case 's':
    136 			if (sectionname != NULL)
    137 				usage();
    138 			sectionname = optarg;
    139 			break;
    140 		case 'S':
    141 			pathsearch = optarg;
    142 			break;
    143 		case 'w':
    144 			f_all = f_where = 1;
    145 			break;
    146 		case '?':
    147 		default:
    148 			usage();
    149 		}
    150 	argc -= optind;
    151 	argv += optind;
    152 
    153 	if (!argc)
    154 		usage();
    155 
    156 	if (!f_cat && !f_how && !f_where) {
    157 		if (!isatty(STDOUT_FILENO)) {
    158 			f_cat = 1;
    159 		} else {
    160 			if ((pager = getenv("PAGER")) != NULL &&
    161 			    pager[0] != '\0')
    162 				pager = check_pager(pager);
    163 			else
    164 				pager = _PATH_PAGER;
    165 		}
    166 	}
    167 
    168 	/* Read the configuration file. */
    169 	config(conffile);
    170 
    171 	/* Get the machine type. */
    172 	if ((machine = getenv("MACHINE")) == NULL) {
    173 		struct utsname utsname;
    174 
    175 		if (uname(&utsname) == -1) {
    176 			perror("uname");
    177 			exit(1);
    178 		}
    179 		machine = utsname.machine;
    180 	}
    181 
    182 	/* create an empty _default list if the config file didn't have one */
    183 	defp = getlist("_default", 1);
    184 
    185 	/* if -M wasn't specified, check for MANPATH */
    186 	if (p_path == NULL)
    187 		p_path = getenv("MANPATH");
    188 
    189 	/*
    190 	 * get section.  abs_section will be non-zero iff the user
    191 	 * specified a section and it had absolute (rather than
    192 	 * relative) paths in the man.conf file.
    193 	 */
    194 	if ((argc > 1 || sectionname != NULL) &&
    195 	    (section = getlist(sectionname ? sectionname : *argv, 0)) != NULL) {
    196 		if (sectionname == NULL) {
    197 			argv++;
    198 			argc--;
    199 		}
    200 		abs_section = (! TAILQ_EMPTY(&section->list) &&
    201 		    *(TAILQ_FIRST(&section->list)->s) == '/');
    202 	} else {
    203 		section = NULL;
    204 		abs_section = 0;
    205 	}
    206 
    207 	/* get subdir list */
    208 	subp = getlist("_subdir", 1);
    209 
    210 	/*
    211 	 * now that we have all the inputs we must generate a search path.
    212 	 */
    213 
    214 	/*
    215 	 * 1: If user specified a section and it has absolute paths
    216 	 * in the config file, then that overrides _default, MANPATH and
    217 	 * path passed via -M.
    218 	 */
    219 	if (abs_section) {
    220 		p_path = NULL;		/* zap -M/MANPATH */
    221 		defp = section;		/* zap _default */
    222 		section = NULL;		/* promoted to defp */
    223 	}
    224 
    225 
    226 	/*
    227 	 * 2: Section can be non-null only if a section was specified
    228 	 * and the config file has relative paths - the section list
    229 	 * overrides _subdir in this case.
    230 	 */
    231 	if (section)
    232 		subp = section;
    233 
    234 
    235 	/*
    236 	 * 3: now we either have text string path (p_path) or a tag
    237 	 * based path (defp).   we need to append subp and machine
    238 	 * to each element in the path.
    239 	 *
    240 	 * for backward compat, we do not append subp if abs_section
    241 	 * and the path does not end in "/".
    242 	 */
    243 	newpathp = getlist("_new_path", 1);
    244 	if (p_path) {
    245 		/* use p_path */
    246 		for (; (p = strtok(p_path, ":")) != NULL; p_path = NULL) {
    247 			TAILQ_FOREACH(e_subp, &subp->list, q) {
    248 				snprintf(buf, sizeof(buf), "%s/%s{/%s,}",
    249 					 p, e_subp->s, machine);
    250 				addentry(newpathp, buf, 0);
    251 			}
    252 		}
    253 	} else {
    254 		/* use defp rather than p_path */
    255 		TAILQ_FOREACH(e_defp, &defp->list, q) {
    256 
    257 			/* handle trailing "/" magic here ... */
    258 		  	if (abs_section &&
    259 			    e_defp->s[strlen(e_defp->s) - 1] != '/') {
    260 
    261 				(void)snprintf(buf, sizeof(buf),
    262 				    "%s{/%s,}", e_defp->s, machine);
    263 				addentry(newpathp, buf, 0);
    264 				continue;
    265 			}
    266 
    267 			TAILQ_FOREACH(e_subp, &subp->list, q) {
    268 				snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
    269 					 e_defp->s, (abs_section) ? "" : "/",
    270 					 e_subp->s, machine);
    271 				addentry(newpathp, buf, 0);
    272 			}
    273 		}
    274 	}				/* using defp ... */
    275 
    276 	/* now replace the current path with the new one */
    277 	defp = newpathp;
    278 
    279 	/*
    280 	 * 4: prepend the "-m" path, if specified.   we always add
    281 	 * subp and machine to this part of the path.
    282 	 */
    283 
    284 	if (p_add) {
    285 		for (p = strtok(p_add, ":") ; p ; p = strtok(NULL, ":")) {
    286 			TAILQ_FOREACH(e_subp, &subp->list, q) {
    287 				snprintf(buf, sizeof(buf), "%s/%s{/%s,}",
    288 					 p, e_subp->s, machine);
    289 				addentry(newpathp, buf, 1);
    290 			}
    291 		}
    292 	}
    293 
    294 
    295 	/*
    296 	 * 5: Search for the files.  Set up an interrupt handler, so the
    297 	 *    temporary files go away.
    298 	 */
    299 	(void)signal(SIGINT, onsig);
    300 	(void)signal(SIGHUP, onsig);
    301 	(void)signal(SIGPIPE, onsig);
    302 
    303 	memset(&pg, 0, sizeof(pg));
    304 	for (found = 0; *argv; ++argv)
    305 		if (manual(*argv, defp, &pg, pathsearch))
    306 			found = 1;
    307 
    308 	/* 6: If nothing found, we're done. */
    309 	if (!found) {
    310 		(void)cleanup();
    311 		exit (1);
    312 	}
    313 
    314 	/* 7: If it's simple, display it fast. */
    315 	if (f_cat) {
    316 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    317 			if (**ap == '\0')
    318 				continue;
    319 			cat(*ap);
    320 		}
    321 		exit (cleanup());
    322 	}
    323 	if (f_how) {
    324 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    325 			if (**ap == '\0')
    326 				continue;
    327 			how(*ap);
    328 		}
    329 		exit(cleanup());
    330 	}
    331 	if (f_where) {
    332 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    333 			if (**ap == '\0')
    334 				continue;
    335 			(void)printf("%s\n", *ap);
    336 		}
    337 		exit(cleanup());
    338 	}
    339 
    340 	/*
    341 	 * 8: We display things in a single command; build a list of things
    342 	 *    to display.
    343 	 */
    344 	for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
    345 		if (**ap == '\0')
    346 			continue;
    347 		len += strlen(*ap) + 1;
    348 	}
    349 	if ((cmd = malloc(len)) == NULL) {
    350 		warn("malloc");
    351 		(void)cleanup();
    352 		exit(1);
    353 	}
    354 	p = cmd;
    355 	len = strlen(pager);
    356 	memmove(p, pager, len);
    357 	p += len;
    358 	*p++ = ' ';
    359 	for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    360 		if (**ap == '\0')
    361 			continue;
    362 		len = strlen(*ap);
    363 		memmove(p, *ap, len);
    364 		p += len;
    365 		*p++ = ' ';
    366 	}
    367 	*--p = '\0';
    368 
    369 	/* Use system(3) in case someone's pager is "pager arg1 arg2". */
    370 	(void)system(cmd);
    371 
    372 	exit(cleanup());
    373 }
    374 
    375 /*
    376  * manual --
    377  *	Search the manuals for the pages.
    378  */
    379 static int
    380 manual(page, tag, pg, pathsearch)
    381 	char *page;
    382 	TAG *tag;
    383 	glob_t *pg;
    384 	const char *pathsearch;
    385 {
    386 	ENTRY *ep, *e_sufp, *e_tag;
    387 	TAG *missp, *sufp;
    388 	int anyfound, cnt, error, found;
    389 	char *p, buf[MAXPATHLEN], *escpage, *eptr;
    390 	static const char escglob[] = "\\~?*{}[]";
    391 
    392 	anyfound = 0;
    393 	buf[0] = '*';
    394 
    395 	/*
    396 	 * Fixup page which may contain glob(3) special characters, e.g.
    397 	 * the famous "No man page for [" FAQ.
    398 	 */
    399 	if ((escpage = malloc((2 * strlen(page)) + 1)) == NULL) {
    400 		warn("malloc");
    401 		(void)cleanup();
    402 		exit(1);
    403 	}
    404 
    405 	p = page;
    406 	eptr = escpage;
    407 
    408 	while (*p) {
    409 		if (strchr(escglob, *p) != NULL) {
    410 			*eptr++ = '\\';
    411 			*eptr++ = *p++;
    412 		} else
    413 			*eptr++ = *p++;
    414 	}
    415 
    416 	*eptr = '\0';
    417 
    418 	/* For each element in the list... */
    419 	TAILQ_FOREACH(e_tag, &tag->list, q) {
    420 		(void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, escpage);
    421 		if ((error = glob(buf,
    422 		    GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) {
    423 			if (error == GLOB_NOMATCH)
    424 				continue;
    425 			else {
    426 				warn("globbing");
    427 				(void)cleanup();
    428 				exit(1);
    429 			}
    430 		}
    431 		if (pg->gl_matchc == 0)
    432 			continue;
    433 
    434 		/* Find out if it's really a man page. */
    435 		for (cnt = pg->gl_pathc - pg->gl_matchc;
    436 		    cnt < pg->gl_pathc; ++cnt) {
    437 
    438 			if (pathsearch) {
    439 				p = strstr(pg->gl_pathv[cnt], pathsearch);
    440 				if (!p || strchr(p, '/') == NULL) {
    441 					pg->gl_pathv[cnt] = "";
    442 					continue;
    443 				}
    444 			}
    445 
    446 			/*
    447 			 * Try the _suffix key words first.
    448 			 *
    449 			 * XXX
    450 			 * Older versions of man.conf didn't have the suffix
    451 			 * key words, it was assumed that everything was a .0.
    452 			 * We just test for .0 first, it's fast and probably
    453 			 * going to hit.
    454 			 */
    455 			(void)snprintf(buf, sizeof(buf), "*/%s.0", escpage);
    456 			if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
    457 				goto next;
    458 
    459 			sufp = getlist("_suffix", 1);
    460 			found = 0;
    461 			TAILQ_FOREACH(e_sufp, &sufp->list, q) {
    462 				(void)snprintf(buf,
    463 				     sizeof(buf), "*/%s%s", escpage,
    464 				     e_sufp->s);
    465 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    466 					found = 1;
    467 					break;
    468 				}
    469 			}
    470 			if (found)
    471 				goto next;
    472 
    473 			/* Try the _build key words next. */
    474 			sufp = getlist("_build", 1);
    475 			found = 0;
    476 			TAILQ_FOREACH(e_sufp, &sufp->list, q) {
    477 				for (p = e_sufp->s;
    478 				    *p != '\0' && !isspace((unsigned char)*p);
    479 				    ++p)
    480 					continue;
    481 				if (*p == '\0')
    482 					continue;
    483 				*p = '\0';
    484 				(void)snprintf(buf,
    485 				     sizeof(buf), "*/%s%s", escpage,
    486 				     e_sufp->s);
    487 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    488 					if (!f_where)
    489 						build_page(p + 1,
    490 						    &pg->gl_pathv[cnt]);
    491 					*p = ' ';
    492 					found = 1;
    493 					break;
    494 				}
    495 				*p = ' ';
    496 			}
    497 			if (found) {
    498 next:				anyfound = 1;
    499 				if (!f_all) {
    500 					/* Delete any other matches. */
    501 					while (++cnt< pg->gl_pathc)
    502 						pg->gl_pathv[cnt] = "";
    503 					break;
    504 				}
    505 				continue;
    506 			}
    507 
    508 			/* It's not a man page, forget about it. */
    509 			pg->gl_pathv[cnt] = "";
    510 		}
    511 
    512 		if (anyfound && !f_all)
    513 			break;
    514 	}
    515 
    516 	/* If not found, enter onto the missing list. */
    517 	if (!anyfound) {
    518 		missp = getlist("_missing", 1);
    519 		if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    520 		    (ep->s = strdup(page)) == NULL) {
    521 			warn("malloc");
    522 			(void)cleanup();
    523 			exit(1);
    524 		}
    525 		TAILQ_INSERT_TAIL(&missp->list, ep, q);
    526 	}
    527 
    528 	free(escpage);
    529 	return (anyfound);
    530 }
    531 
    532 /*
    533  * build_page --
    534  *	Build a man page for display.
    535  */
    536 static void
    537 build_page(fmt, pathp)
    538 	char *fmt, **pathp;
    539 {
    540 	static int warned;
    541 	ENTRY *ep;
    542 	TAG *intmpp;
    543 	int fd, n;
    544 	char *p, *b;
    545 	char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
    546 	const char *tmpdir;
    547 
    548 	/* Let the user know this may take awhile. */
    549 	if (!warned) {
    550 		warned = 1;
    551 		warnx("Formatting manual page...");
    552 	}
    553 
    554        /*
    555         * Historically man chdir'd to the root of the man tree.
    556         * This was used in man pages that contained relative ".so"
    557         * directives (including other man pages for command aliases etc.)
    558         * It even went one step farther, by examining the first line
    559         * of the man page and parsing the .so filename so it would
    560         * make hard(?) links to the cat'ted man pages for space savings.
    561         * (We don't do that here, but we could).
    562         */
    563 
    564        /* copy and find the end */
    565        for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
    566                continue;
    567 
    568 	/* skip the last two path components, page name and man[n] */
    569 	for (--b, --p, n = 2; b != buf; b--, p--)
    570 		if (*b == '/')
    571 			if (--n == 0) {
    572 				*b = '\0';
    573 				(void) chdir(buf);
    574 				p++;
    575 				break;
    576 			}
    577 
    578 
    579 	/* Add a remove-when-done list. */
    580 	intmpp = getlist("_intmp", 1);
    581 
    582 	/* Move to the printf(3) format string. */
    583 	for (; *fmt && isspace((unsigned char)*fmt); ++fmt)
    584 		continue;
    585 
    586 	/*
    587 	 * Get a temporary file and build a version of the file
    588 	 * to display.  Replace the old file name with the new one.
    589 	 */
    590 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    591 		tmpdir = _PATH_TMP;
    592 	(void)snprintf(tpath, sizeof (tpath), "%s/%s", tmpdir, TMPFILE);
    593 	if ((fd = mkstemp(tpath)) == -1) {
    594 		warn("%s", tpath);
    595 		(void)cleanup();
    596 		exit(1);
    597 	}
    598 	(void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
    599 	(void)snprintf(cmd, sizeof(cmd), buf, p);
    600 	(void)system(cmd);
    601 	(void)close(fd);
    602 	if ((*pathp = strdup(tpath)) == NULL) {
    603 		warn("malloc");
    604 		(void)cleanup();
    605 		exit(1);
    606 	}
    607 
    608 	/* Link the built file into the remove-when-done list. */
    609 	if ((ep = malloc(sizeof(ENTRY))) == NULL) {
    610 		warn("malloc");
    611 		(void)cleanup();
    612 		exit(1);
    613 	}
    614 	ep->s = *pathp;
    615 	TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
    616 }
    617 
    618 /*
    619  * how --
    620  *	display how information
    621  */
    622 static void
    623 how(fname)
    624 	char *fname;
    625 {
    626 	FILE *fp;
    627 
    628 	int lcnt, print;
    629 	char *p, buf[256];
    630 
    631 	if (!(fp = fopen(fname, "r"))) {
    632 		warn("%s", fname);
    633 		(void)cleanup();
    634 		exit (1);
    635 	}
    636 #define	S1	"SYNOPSIS"
    637 #define	S2	"S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
    638 #define	D1	"DESCRIPTION"
    639 #define	D2	"D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
    640 	for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
    641 		if (!strncmp(buf, S1, sizeof(S1) - 1) ||
    642 		    !strncmp(buf, S2, sizeof(S2) - 1)) {
    643 			print = 1;
    644 			continue;
    645 		} else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
    646 		    !strncmp(buf, D2, sizeof(D2) - 1))
    647 			return;
    648 		if (!print)
    649 			continue;
    650 		if (*buf == '\n')
    651 			++lcnt;
    652 		else {
    653 			for(; lcnt; --lcnt)
    654 				(void)putchar('\n');
    655 			for (p = buf; isspace((unsigned char)*p); ++p)
    656 				continue;
    657 			(void)fputs(p, stdout);
    658 		}
    659 	}
    660 	(void)fclose(fp);
    661 }
    662 
    663 /*
    664  * cat --
    665  *	cat out the file
    666  */
    667 static void
    668 cat(fname)
    669 	char *fname;
    670 {
    671 	int fd, n;
    672 	char buf[2048];
    673 
    674 	if ((fd = open(fname, O_RDONLY, 0)) < 0) {
    675 		warn("%s", fname);
    676 		(void)cleanup();
    677 		exit(1);
    678 	}
    679 	while ((n = read(fd, buf, sizeof(buf))) > 0)
    680 		if (write(STDOUT_FILENO, buf, n) != n) {
    681 			warn("write");
    682 			(void)cleanup();
    683 			exit (1);
    684 		}
    685 	if (n == -1) {
    686 		warn("read");
    687 		(void)cleanup();
    688 		exit(1);
    689 	}
    690 	(void)close(fd);
    691 }
    692 
    693 /*
    694  * check_pager --
    695  *	check the user supplied page information
    696  */
    697 static const char *
    698 check_pager(name)
    699 	const char *name;
    700 {
    701 	const char *p;
    702 
    703 	/*
    704 	 * if the user uses "more", we make it "more -s"; watch out for
    705 	 * PAGER = "mypager /usr/ucb/more"
    706 	 */
    707 	for (p = name; *p && !isspace((unsigned char)*p); ++p)
    708 		continue;
    709 	for (; p > name && *p != '/'; --p);
    710 	if (p != name)
    711 		++p;
    712 
    713 	/* make sure it's "more", not "morex" */
    714 	if (!strncmp(p, "more", 4) && (!p[4] || isspace((unsigned char)p[4]))){
    715 		char *newname;
    716 		(void)asprintf(&newname, "%s %s", p, "-s");
    717 		name = newname;
    718 	}
    719 
    720 	return (name);
    721 }
    722 
    723 /*
    724  * jump --
    725  *	strip out flag argument and jump
    726  */
    727 static void
    728 jump(argv, flag, name)
    729 	char **argv, *flag, *name;
    730 {
    731 	char **arg;
    732 
    733 	argv[0] = name;
    734 	for (arg = argv + 1; *arg; ++arg)
    735 		if (!strcmp(*arg, flag))
    736 			break;
    737 	for (; *arg; ++arg)
    738 		arg[0] = arg[1];
    739 	execvp(name, argv);
    740 	(void)fprintf(stderr, "%s: Command not found.\n", name);
    741 	exit(1);
    742 }
    743 
    744 /*
    745  * onsig --
    746  *	If signaled, delete the temporary files.
    747  */
    748 static void
    749 onsig(signo)
    750 	int signo;
    751 {
    752 	sigset_t set;
    753 
    754 	(void)cleanup();
    755 
    756 	(void)signal(signo, SIG_DFL);
    757 
    758 	/* unblock the signal */
    759 	sigemptyset(&set);
    760 	sigaddset(&set, signo);
    761 	sigprocmask(SIG_UNBLOCK, &set, (sigset_t *) NULL);
    762 
    763 	(void)kill(getpid(), signo);
    764 
    765 	/* NOTREACHED */
    766 	exit (1);
    767 }
    768 
    769 /*
    770  * cleanup --
    771  *	Clean up temporary files, show any error messages.
    772  */
    773 static int
    774 cleanup()
    775 {
    776 	TAG *intmpp, *missp;
    777 	ENTRY *ep;
    778 	int rval;
    779 
    780 	rval = 0;
    781 		/*
    782 		 * get missing list, but don't create missing _missing,
    783 		 * as we don't want to try & allocate memory in getlist()
    784 		 */
    785 	if ((missp = getlist("_missing", 0)) != NULL)
    786 		TAILQ_FOREACH(ep, &missp->list, q) {
    787 			warnx("no entry for %s in the manual.", ep->s);
    788 			rval = 1;
    789 		}
    790 
    791 		/*
    792 		 * get tempfile list, but don't create missing _intmp,
    793 		 * as we don't want to try & allocate memory in getlist()
    794 		 */
    795 	if ((intmpp = getlist("_intmp", 0)) != NULL)
    796 		TAILQ_FOREACH(ep, &intmpp->list, q)
    797 			(void)unlink(ep->s);
    798 	return (rval);
    799 }
    800 
    801 /*
    802  * usage --
    803  *	print usage message and die
    804  */
    805 static void
    806 usage()
    807 {
    808 
    809 	(void)fprintf(stderr, "Usage: %s [-achw] [-C file] [-M path] [-m path]"
    810 	    "[-S srch] [[-s] section] title ...\n", getprogname());
    811 	exit(1);
    812 }
    813