Home | History | Annotate | Line # | Download | only in man
man.c revision 1.13
      1 /*	$NetBSD: man.c,v 1.13 1998/08/25 20:59:39 ross 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.13 1998/08/25 20:59:39 ross Exp $");
     48 #endif
     49 #endif /* not lint */
     50 
     51 #include <sys/param.h>
     52 #include <sys/queue.h>
     53 
     54 #include <ctype.h>
     55 #include <err.h>
     56 #include <errno.h>
     57 #include <fcntl.h>
     58 #include <fnmatch.h>
     59 #include <glob.h>
     60 #include <signal.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 
     66 #include "config.h"
     67 #include "pathnames.h"
     68 
     69 int f_all, f_where;
     70 
     71 int		 main __P((int, char **));
     72 static void	 build_page __P((char *, char **));
     73 static void	 cat __P((char *));
     74 static char	*check_pager __P((char *));
     75 static int	 cleanup __P((void));
     76 static void	 how __P((char *));
     77 static void	 jump __P((char **, char *, char *));
     78 static int	 manual __P((char *, TAG *, glob_t *));
     79 static void	 onsig __P((int));
     80 static void	 usage __P((void));
     81 
     82 int
     83 main(argc, argv)
     84 	int argc;
     85 	char *argv[];
     86 {
     87 	TAG *defp, *defnewp, *section, *sectnewp, *subp;
     88 	ENTRY *e_defp, *e_sectp, *e_subp, *ep;
     89 	glob_t pg;
     90 	size_t len;
     91 	int ch, f_cat, f_how, found;
     92 	char **ap, *cmd, *machine, *p, *p_add, *p_path, *pager, *slashp;
     93 	char *conffile, buf[MAXPATHLEN * 2];
     94 
     95 #ifdef __GNUC__
     96 	pager = NULL;		/* XXX gcc -Wuninitialized */
     97 #endif
     98 
     99 	f_cat = f_how = 0;
    100 	conffile = p_add = p_path = NULL;
    101 	while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:w")) != -1)
    102 		switch (ch) {
    103 		case 'a':
    104 			f_all = 1;
    105 			break;
    106 		case 'C':
    107 			conffile = optarg;
    108 			break;
    109 		case 'c':
    110 		case '-':		/* Deprecated. */
    111 			f_cat = 1;
    112 			break;
    113 		case 'h':
    114 			f_how = 1;
    115 			break;
    116 		case 'm':
    117 			p_add = optarg;
    118 			break;
    119 		case 'M':
    120 		case 'P':		/* Backward compatibility. */
    121 			p_path = optarg;
    122 			break;
    123 		/*
    124 		 * The -f and -k options are backward compatible,
    125 		 * undocumented ways of calling whatis(1) and apropos(1).
    126 		 */
    127 		case 'f':
    128 			jump(argv, "-f", "whatis");
    129 			/* NOTREACHED */
    130 		case 'k':
    131 			jump(argv, "-k", "apropos");
    132 			/* NOTREACHED */
    133 		case 'w':
    134 			f_all = f_where = 1;
    135 			break;
    136 		case '?':
    137 		default:
    138 			usage();
    139 		}
    140 	argc -= optind;
    141 	argv += optind;
    142 
    143 	if (!*argv)
    144 		usage();
    145 
    146 	if (!f_cat && !f_how && !f_where) {
    147 		if (!isatty(1))
    148 			f_cat = 1;
    149 		else {
    150 			if ((pager = getenv("PAGER")) != NULL)
    151 				pager = check_pager(pager);
    152 			else	pager = _PATH_PAGER;
    153 		}
    154 	}
    155 	/* Read the configuration file. */
    156 	config(conffile);
    157 
    158 	/* Get the machine type. */
    159 	if ((machine = getenv("MACHINE")) == NULL)
    160 		machine = MACHINE;
    161 
    162 	/* If there's no _default list, create an empty one. */
    163 	if ((defp = getlist("_default")) == NULL)
    164 		defp = addlist("_default");
    165 
    166 	/*
    167 	 * 1: If the user specified a MANPATH variable, or set the -M
    168 	 *    option, we replace the _default list with the user's list,
    169 	 *    appending the entries in the _subdir list and the machine.
    170 	 */
    171 	if (p_path == NULL)
    172 		p_path = getenv("MANPATH");
    173 	if (p_path != NULL) {
    174 		while ((e_defp = defp->list.tqh_first) != NULL) {
    175 			free(e_defp->s);
    176 			TAILQ_REMOVE(&defp->list, e_defp, q);
    177 		}
    178 		for (p = strtok(p_path, ":");
    179 		    p != NULL; p = strtok(NULL, ":")) {
    180 			slashp = p[strlen(p) - 1] == '/' ? "" : "/";
    181 			e_subp = (subp = getlist("_subdir")) == NULL ?
    182 			    NULL : subp->list.tqh_first;
    183 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
    184 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
    185 				    p, slashp, e_subp->s, machine);
    186 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    187 				    (ep->s = strdup(buf)) == NULL)
    188 					err(1, "malloc");
    189 				TAILQ_INSERT_TAIL(&defp->list, ep, q);
    190 			}
    191 		}
    192 	}
    193 
    194 	/*
    195 	 * 2: If the user did not specify MANPATH, -M or a section, rewrite
    196 	 *    the _default list to include the _subdir list and the machine.
    197 	 */
    198 	if (argv[1] == NULL)
    199 		section = NULL;
    200 	else if ((section = getlist(*argv)) != NULL)
    201 		++argv;
    202 	if (p_path == NULL && section == NULL) {
    203 		defnewp = addlist("_default_new");
    204 		e_defp =
    205 		    defp->list.tqh_first == NULL ? NULL : defp->list.tqh_first;
    206 		for (; e_defp != NULL; e_defp = e_defp->q.tqe_next) {
    207 			slashp =
    208 			    e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
    209 			e_subp = (subp = getlist("_subdir")) == NULL ?
    210 			    NULL : subp->list.tqh_first;
    211 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
    212 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
    213 				e_defp->s, slashp, e_subp->s, machine);
    214 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    215 				    (ep->s = strdup(buf)) == NULL)
    216 					err(1, "malloc");
    217 				TAILQ_INSERT_TAIL(&defnewp->list, ep, q);
    218 			}
    219 		}
    220 		defp = getlist("_default");
    221 		while ((e_defp = defp->list.tqh_first) != NULL) {
    222 			free(e_defp->s);
    223 			TAILQ_REMOVE(&defp->list, e_defp, q);
    224 		}
    225 		free(defp->s);
    226 		TAILQ_REMOVE(&head, defp, q);
    227 		defnewp = getlist("_default_new");
    228 		free(defnewp->s);
    229 		defnewp->s = "_default";
    230 		defp = defnewp;
    231 	}
    232 
    233 	/*
    234 	 * 3: If the user set the -m option, insert the user's list before
    235 	 *    whatever list we have, again appending the _subdir list and
    236 	 *    the machine.
    237 	 */
    238 	if (p_add != NULL)
    239 		for (p = strtok(p_add, ":"); p != NULL; p = strtok(NULL, ":")) {
    240 			slashp = p[strlen(p) - 1] == '/' ? "" : "/";
    241 			e_subp = (subp = getlist("_subdir")) == NULL ?
    242 			    NULL : subp->list.tqh_first;
    243 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
    244 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
    245 				    p, slashp, e_subp->s, machine);
    246 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    247 				    (ep->s = strdup(buf)) == NULL)
    248 					err(1, "malloc");
    249 				TAILQ_INSERT_HEAD(&defp->list, ep, q);
    250 			}
    251 		}
    252 
    253 	/*
    254 	 * 4: If no -m was specified, and a section was, rewrite the section's
    255 	 *    paths (if they have a trailing slash) to append the _subdir list
    256 	 *    and the machine.  This then becomes the _default list.
    257 	 */
    258 	if (p_add == NULL && section != NULL) {
    259 		sectnewp = addlist("_section_new");
    260 		for (e_sectp = section->list.tqh_first;
    261 		    e_sectp != NULL; e_sectp = e_sectp->q.tqe_next) {
    262 			if (e_sectp->s[strlen(e_sectp->s) - 1] != '/') {
    263 				(void)snprintf(buf, sizeof(buf),
    264 				    "%s{/%s,}", e_sectp->s, machine);
    265 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    266 				    (ep->s = strdup(buf)) == NULL)
    267 					err(1, "malloc");
    268 				TAILQ_INSERT_TAIL(&sectnewp->list, ep, q);
    269 				continue;
    270 			}
    271 			e_subp = (subp = getlist("_subdir")) == NULL ?
    272 			    NULL : subp->list.tqh_first;
    273 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
    274 				(void)snprintf(buf, sizeof(buf), "%s%s{/%s,}",
    275 				    e_sectp->s, e_subp->s, machine);
    276 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    277 				    (ep->s = strdup(buf)) == NULL)
    278 					err(1, "malloc");
    279 				TAILQ_INSERT_TAIL(&sectnewp->list, ep, q);
    280 			}
    281 		}
    282 		sectnewp->s = section->s;
    283 		defp = sectnewp;
    284 		TAILQ_REMOVE(&head, section, q);
    285 	}
    286 
    287 	/*
    288 	 * 5: Search for the files.  Set up an interrupt handler, so the
    289 	 *    temporary files go away.
    290 	 */
    291 	(void)signal(SIGINT, onsig);
    292 	(void)signal(SIGHUP, onsig);
    293 
    294 	memset(&pg, 0, sizeof(pg));
    295 	for (found = 0; *argv; ++argv)
    296 		if (manual(*argv, defp, &pg))
    297 			found = 1;
    298 
    299 	/* 6: If nothing found, we're done. */
    300 	if (!found) {
    301 		(void)cleanup();
    302 		exit (1);
    303 	}
    304 
    305 	/* 7: If it's simple, display it fast. */
    306 	if (f_cat) {
    307 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    308 			if (**ap == '\0')
    309 				continue;
    310 			cat(*ap);
    311 		}
    312 		exit (cleanup());
    313 	}
    314 	if (f_how) {
    315 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    316 			if (**ap == '\0')
    317 				continue;
    318 			how(*ap);
    319 		}
    320 		exit(cleanup());
    321 	}
    322 	if (f_where) {
    323 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    324 			if (**ap == '\0')
    325 				continue;
    326 			(void)printf("%s\n", *ap);
    327 		}
    328 		exit(cleanup());
    329 	}
    330 
    331 	/*
    332 	 * 8: We display things in a single command; build a list of things
    333 	 *    to display.
    334 	 */
    335 	for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
    336 		if (**ap == '\0')
    337 			continue;
    338 		len += strlen(*ap) + 1;
    339 	}
    340 	if ((cmd = malloc(len)) == NULL) {
    341 		warn("malloc");
    342 		(void)cleanup();
    343 		exit(1);
    344 	}
    345 	p = cmd;
    346 	len = strlen(pager);
    347 	memmove(p, pager, len);
    348 	p += len;
    349 	*p++ = ' ';
    350 	for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    351 		if (**ap == '\0')
    352 			continue;
    353 		len = strlen(*ap);
    354 		memmove(p, *ap, len);
    355 		p += len;
    356 		*p++ = ' ';
    357 	}
    358 	*p = '\0';
    359 
    360 	/* Use system(3) in case someone's pager is "pager arg1 arg2". */
    361 	(void)system(cmd);
    362 
    363 	exit(cleanup());
    364 }
    365 
    366 /*
    367  * manual --
    368  *	Search the manuals for the pages.
    369  */
    370 static int
    371 manual(page, tag, pg)
    372 	char *page;
    373 	TAG *tag;
    374 	glob_t *pg;
    375 {
    376 	ENTRY *ep, *e_sufp, *e_tag;
    377 	TAG *missp, *sufp;
    378 	int anyfound, cnt, error, found;
    379 	char *p, buf[MAXPATHLEN];
    380 
    381 	anyfound = 0;
    382 	buf[0] = '*';
    383 
    384 	/* For each element in the list... */
    385 	e_tag = tag == NULL ? NULL : tag->list.tqh_first;
    386 	for (; e_tag != NULL; e_tag = e_tag->q.tqe_next) {
    387 		(void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
    388 		if ((error = glob(buf,
    389 		    GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) {
    390 			if (error == GLOB_NOMATCH)
    391 				continue;
    392 			else {
    393 				warn("globbing");
    394 				(void)cleanup();
    395 				exit(1);
    396 			}
    397 		}
    398 		if (pg->gl_matchc == 0)
    399 			continue;
    400 
    401 		/* Find out if it's really a man page. */
    402 		for (cnt = pg->gl_pathc - pg->gl_matchc;
    403 		    cnt < pg->gl_pathc; ++cnt) {
    404 
    405 			/*
    406 			 * Try the _suffix key words first.
    407 			 *
    408 			 * XXX
    409 			 * Older versions of man.conf didn't have the suffix
    410 			 * key words, it was assumed that everything was a .0.
    411 			 * We just test for .0 first, it's fast and probably
    412 			 * going to hit.
    413 			 */
    414 			(void)snprintf(buf, sizeof(buf), "*/%s.0", page);
    415 			if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
    416 				goto next;
    417 
    418 			e_sufp = (sufp = getlist("_suffix")) == NULL ?
    419 			    NULL : sufp->list.tqh_first;
    420 			for (found = 0;
    421 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
    422 				(void)snprintf(buf,
    423 				     sizeof(buf), "*/%s%s", page, e_sufp->s);
    424 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    425 					found = 1;
    426 					break;
    427 				}
    428 			}
    429 			if (found)
    430 				goto next;
    431 
    432 			/* Try the _build key words next. */
    433 			e_sufp = (sufp = getlist("_build")) == NULL ?
    434 			    NULL : sufp->list.tqh_first;
    435 			for (found = 0;
    436 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
    437 				for (p = e_sufp->s;
    438 				    *p != '\0' && !isspace(*p); ++p);
    439 				if (*p == '\0')
    440 					continue;
    441 				*p = '\0';
    442 				(void)snprintf(buf,
    443 				     sizeof(buf), "*/%s%s", page, e_sufp->s);
    444 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    445 					if (!f_where)
    446 						build_page(p + 1,
    447 						    &pg->gl_pathv[cnt]);
    448 					*p = ' ';
    449 					found = 1;
    450 					break;
    451 				}
    452 				*p = ' ';
    453 			}
    454 			if (found) {
    455 next:				anyfound = 1;
    456 				if (!f_all) {
    457 					/* Delete any other matches. */
    458 					while (++cnt< pg->gl_pathc)
    459 						pg->gl_pathv[cnt] = "";
    460 					break;
    461 				}
    462 				continue;
    463 			}
    464 
    465 			/* It's not a man page, forget about it. */
    466 			pg->gl_pathv[cnt] = "";
    467 		}
    468 
    469 		if (anyfound && !f_all)
    470 			break;
    471 	}
    472 
    473 	/* If not found, enter onto the missing list. */
    474 	if (!anyfound) {
    475 		if ((missp = getlist("_missing")) == NULL)
    476 			missp = addlist("_missing");
    477 		if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    478 		    (ep->s = strdup(page)) == NULL) {
    479 			warn("malloc");
    480 			(void)cleanup();
    481 			exit(1);
    482 		}
    483 		TAILQ_INSERT_TAIL(&missp->list, ep, q);
    484 	}
    485 	return (anyfound);
    486 }
    487 
    488 /*
    489  * build_page --
    490  *	Build a man page for display.
    491  */
    492 static void
    493 build_page(fmt, pathp)
    494 	char *fmt, **pathp;
    495 {
    496 	static int warned;
    497 	ENTRY *ep;
    498 	TAG *intmpp;
    499 	int fd, n;
    500 	char *p, *b;
    501 	char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[sizeof(_PATH_TMP)];
    502 
    503 	/* Let the user know this may take awhile. */
    504 	if (!warned) {
    505 		warned = 1;
    506 		warnx("Formatting manual page...");
    507 	}
    508 
    509        /*
    510         * Historically man chdir'd to the root of the man tree.
    511         * This was used in man pages that contained relative ".so"
    512         * directives (including other man pages for command aliases etc.)
    513         * It even went one step farther, by examining the first line
    514         * of the man page and parsing the .so filename so it would
    515         * make hard(?) links to the cat'ted man pages for space savings.
    516         * (We don't do that here, but we could).
    517         */
    518 
    519        /* copy and find the end */
    520        for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
    521                continue;
    522 
    523 	/* skip the last two path components, page name and man[n] */
    524 	for (--b, --p, n = 2; b != buf; b--, p--)
    525 		if (*b == '/')
    526 			if (--n == 0) {
    527 				*b = '\0';
    528 				(void) chdir(buf);
    529 				p++;
    530 				break;
    531 			}
    532 
    533 
    534 	/* Add a remove-when-done list. */
    535 	if ((intmpp = getlist("_intmp")) == NULL)
    536 		intmpp = addlist("_intmp");
    537 
    538 	/* Move to the printf(3) format string. */
    539 	for (; *fmt && isspace(*fmt); ++fmt);
    540 
    541 	/*
    542 	 * Get a temporary file and build a version of the file
    543 	 * to display.  Replace the old file name with the new one.
    544 	 */
    545 	(void)strcpy(tpath, _PATH_TMP);
    546 	if ((fd = mkstemp(tpath)) == -1) {
    547 		warn("%s", tpath);
    548 		(void)cleanup();
    549 		exit(1);
    550 	}
    551 	(void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
    552 	(void)snprintf(cmd, sizeof(cmd), buf, p);
    553 	(void)system(cmd);
    554 	(void)close(fd);
    555 	if ((*pathp = strdup(tpath)) == NULL) {
    556 		warn("malloc");
    557 		(void)cleanup();
    558 		exit(1);
    559 	}
    560 
    561 	/* Link the built file into the remove-when-done list. */
    562 	if ((ep = malloc(sizeof(ENTRY))) == NULL) {
    563 		warn("malloc");
    564 		(void)cleanup();
    565 		exit(1);
    566 	}
    567 	ep->s = *pathp;
    568 	TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
    569 }
    570 
    571 /*
    572  * how --
    573  *	display how information
    574  */
    575 static void
    576 how(fname)
    577 	char *fname;
    578 {
    579 	FILE *fp;
    580 
    581 	int lcnt, print;
    582 	char *p, buf[256];
    583 
    584 	if (!(fp = fopen(fname, "r"))) {
    585 		warn("%s", fname);
    586 		(void)cleanup();
    587 		exit (1);
    588 	}
    589 #define	S1	"SYNOPSIS"
    590 #define	S2	"S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
    591 #define	D1	"DESCRIPTION"
    592 #define	D2	"D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
    593 	for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
    594 		if (!strncmp(buf, S1, sizeof(S1) - 1) ||
    595 		    !strncmp(buf, S2, sizeof(S2) - 1)) {
    596 			print = 1;
    597 			continue;
    598 		} else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
    599 		    !strncmp(buf, D2, sizeof(D2) - 1))
    600 			return;
    601 		if (!print)
    602 			continue;
    603 		if (*buf == '\n')
    604 			++lcnt;
    605 		else {
    606 			for(; lcnt; --lcnt)
    607 				(void)putchar('\n');
    608 			for (p = buf; isspace(*p); ++p);
    609 			(void)fputs(p, stdout);
    610 		}
    611 	}
    612 	(void)fclose(fp);
    613 }
    614 
    615 /*
    616  * cat --
    617  *	cat out the file
    618  */
    619 static void
    620 cat(fname)
    621 	char *fname;
    622 {
    623 	int fd, n;
    624 	char buf[2048];
    625 
    626 	if ((fd = open(fname, O_RDONLY, 0)) < 0) {
    627 		warn("%s", fname);
    628 		(void)cleanup();
    629 		exit(1);
    630 	}
    631 	while ((n = read(fd, buf, sizeof(buf))) > 0)
    632 		if (write(STDOUT_FILENO, buf, n) != n) {
    633 			warn("write");
    634 			(void)cleanup();
    635 			exit (1);
    636 		}
    637 	if (n == -1) {
    638 		warn("read");
    639 		(void)cleanup();
    640 		exit(1);
    641 	}
    642 	(void)close(fd);
    643 }
    644 
    645 /*
    646  * check_pager --
    647  *	check the user supplied page information
    648  */
    649 static char *
    650 check_pager(name)
    651 	char *name;
    652 {
    653 	char *p, *save;
    654 
    655 	/*
    656 	 * if the user uses "more", we make it "more -s"; watch out for
    657 	 * PAGER = "mypager /usr/ucb/more"
    658 	 */
    659 	for (p = name; *p && !isspace(*p); ++p);
    660 	for (; p > name && *p != '/'; --p);
    661 	if (p != name)
    662 		++p;
    663 
    664 	/* make sure it's "more", not "morex" */
    665 	if (!strncmp(p, "more", 4) && (!p[4] || isspace(p[4]))){
    666 		save = name;
    667 		/* allocate space to add the "-s" */
    668 		if (!(name =
    669 		    malloc((u_int)(strlen(save) + sizeof("-s") + 1))))
    670 			err(1, "malloc");
    671 		(void)sprintf(name, "%s %s", save, "-s");
    672 	}
    673 	return(name);
    674 }
    675 
    676 /*
    677  * jump --
    678  *	strip out flag argument and jump
    679  */
    680 static void
    681 jump(argv, flag, name)
    682 	char **argv, *flag, *name;
    683 {
    684 	char **arg;
    685 
    686 	argv[0] = name;
    687 	for (arg = argv + 1; *arg; ++arg)
    688 		if (!strcmp(*arg, flag))
    689 			break;
    690 	for (; *arg; ++arg)
    691 		arg[0] = arg[1];
    692 	execvp(name, argv);
    693 	(void)fprintf(stderr, "%s: Command not found.\n", name);
    694 	exit(1);
    695 }
    696 
    697 /*
    698  * onsig --
    699  *	If signaled, delete the temporary files.
    700  */
    701 static void
    702 onsig(signo)
    703 	int signo;
    704 {
    705 	(void)cleanup();
    706 
    707 	(void)signal(signo, SIG_DFL);
    708 	(void)kill(getpid(), signo);
    709 
    710 	/* NOTREACHED */
    711 	exit (1);
    712 }
    713 
    714 /*
    715  * cleanup --
    716  *	Clean up temporary files, show any error messages.
    717  */
    718 static int
    719 cleanup()
    720 {
    721 	TAG *intmpp, *missp;
    722 	ENTRY *ep;
    723 	int rval;
    724 
    725 	rval = 0;
    726 	ep = (missp = getlist("_missing")) == NULL ?
    727 	    NULL : missp->list.tqh_first;
    728 	if (ep != NULL)
    729 		for (; ep != NULL; ep = ep->q.tqe_next) {
    730 			warnx("no entry for %s in the manual.", ep->s);
    731 			rval = 1;
    732 		}
    733 
    734 	ep = (intmpp = getlist("_intmp")) == NULL ?
    735 	    NULL : intmpp->list.tqh_first;
    736 	for (; ep != NULL; ep = ep->q.tqe_next)
    737 		(void)unlink(ep->s);
    738 	return (rval);
    739 }
    740 
    741 /*
    742  * usage --
    743  *	print usage message and die
    744  */
    745 static void
    746 usage()
    747 {
    748 	(void)fprintf(stderr,
    749     "usage: man [-achw] [-C file] [-M path] [-m path] [section] title ...\n");
    750 	exit(1);
    751 }
    752