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