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