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