xref: /csrg-svn/usr.bin/man/man.c (revision 65284)
131703Sbostic /*
263537Sbostic  * Copyright (c) 1987, 1993
363537Sbostic  *	The Regents of the University of California.  All rights reserved.
433054Sbostic  *
542741Sbostic  * %sccs.include.redist.c%
631703Sbostic  */
731703Sbostic 
831703Sbostic #ifndef lint
963537Sbostic static char copyright[] =
1063537Sbostic "@(#) Copyright (c) 1987, 1993\n\
1163537Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1233054Sbostic #endif /* not lint */
1331703Sbostic 
1431703Sbostic #ifndef lint
15*65284Sbostic static char sccsid[] = "@(#)man.c	8.4 (Berkeley) 01/02/94";
1633054Sbostic #endif /* not lint */
1731703Sbostic 
1831703Sbostic #include <sys/param.h>
1965283Sbostic #include <sys/queue.h>
2063536Sbostic 
2163536Sbostic #include <ctype.h>
2265283Sbostic #include <err.h>
2340390Sbostic #include <errno.h>
2463536Sbostic #include <fcntl.h>
2565283Sbostic #include <fnmatch.h>
2665283Sbostic #include <glob.h>
2765283Sbostic #include <signal.h>
2863536Sbostic #include <stdio.h>
2963536Sbostic #include <stdlib.h>
3040390Sbostic #include <string.h>
3163536Sbostic #include <unistd.h>
3263536Sbostic 
3365283Sbostic #include "config.h"
3437892Sbostic #include "pathnames.h"
3531703Sbostic 
3665283Sbostic int f_all, f_where;
3731703Sbostic 
3865283Sbostic static void	 build_page __P((char *, char *));
3965283Sbostic static void	 cat __P((char *));
4065283Sbostic static char	*check_pager __P((char *));
4165283Sbostic static void	 cleanup __P((void));
4265283Sbostic static void	 how __P((char *));
4365283Sbostic static void	 jump __P((char **, char *, char *));
4465283Sbostic static int	 manual __P((char *, ENTRY *, glob_t *));
4565283Sbostic static void	 onsig __P((int));
4665283Sbostic static void	 usage __P((void));
4734057Sbostic 
4865283Sbostic int
4931703Sbostic main(argc, argv)
5033354Sbostic 	int argc;
5165283Sbostic 	char *argv[];
5231703Sbostic {
5334057Sbostic 	extern char *optarg;
5434057Sbostic 	extern int optind;
5565283Sbostic 	ENTRY *defp, *defnewp, *intmpp;
5665283Sbostic 	ENTRY *section, *sectp, *sectnewp, *subp, *tp;
5765283Sbostic 	glob_t pg;
5865283Sbostic 	size_t len;
5965283Sbostic 	int ch, f_cat, f_how, found;
6065283Sbostic 	char **ap, *cmd, *machine, *p, *p_add, *p_path, *pager, *slashp;
61*65284Sbostic 	char *conffile, buf[MAXPATHLEN * 2];
6231703Sbostic 
63*65284Sbostic 	conffile = NULL;
6465283Sbostic 	f_cat = f_how = 0;
65*65284Sbostic 	while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:w")) != EOF)
6665283Sbostic 		switch (ch) {
6740390Sbostic 		case 'a':
6840390Sbostic 			f_all = 1;
6931703Sbostic 			break;
70*65284Sbostic 		case 'C':
71*65284Sbostic 			conffile = optarg;
72*65284Sbostic 			break;
7340390Sbostic 		case 'c':
7465283Sbostic 		case '-':		/* Deprecated. */
7540390Sbostic 			f_cat = 1;
7640390Sbostic 			break;
7744938Sbostic 		case 'h':
7844938Sbostic 			f_how = 1;
7944938Sbostic 			break;
8040390Sbostic 		case 'm':
8165283Sbostic 			p_add = optarg;
8240390Sbostic 			break;
8331703Sbostic 		case 'M':
8465283Sbostic 		case 'P':		/* Backward compatibility. */
8540390Sbostic 			p_path = optarg;
8631703Sbostic 			break;
8742402Sbostic 		/*
8865283Sbostic 		 * The -f and -k options are backward compatible,
8965283Sbostic 		 * undocumented ways of calling whatis(1) and apropos(1).
9042402Sbostic 		 */
9131703Sbostic 		case 'f':
9234057Sbostic 			jump(argv, "-f", "whatis");
9340390Sbostic 			/* NOTREACHED */
9431703Sbostic 		case 'k':
9534057Sbostic 			jump(argv, "-k", "apropos");
9640390Sbostic 			/* NOTREACHED */
9732565Sbostic 		case 'w':
9840390Sbostic 			f_all = f_where = 1;
9932565Sbostic 			break;
10031703Sbostic 		case '?':
10131703Sbostic 		default:
10234057Sbostic 			usage();
10331703Sbostic 		}
10465282Sbostic 	argc -= optind;
10534057Sbostic 	argv += optind;
10631703Sbostic 
10734057Sbostic 	if (!*argv)
10834057Sbostic 		usage();
10933354Sbostic 
11044938Sbostic 	if (!f_cat && !f_how)
11131703Sbostic 		if (!isatty(1))
11240390Sbostic 			f_cat = 1;
11340390Sbostic 		else if (pager = getenv("PAGER"))
11440390Sbostic 			pager = check_pager(pager);
11531779Sbostic 		else
11637892Sbostic 			pager = _PATH_PAGER;
11740390Sbostic 
11865283Sbostic 	/* Read the configuration file. */
119*65284Sbostic 	config(conffile);
12065283Sbostic 
12165283Sbostic 	/* If there's no _default list, create an empty one. */
12265283Sbostic 	if ((defp = getlist("_default")) == NULL)
12365283Sbostic 		defp = addlist("_default");
12465283Sbostic 
12565283Sbostic 	/* Get the machine type. */
12665283Sbostic 	if ((machine = getenv("MACHINE")) == NULL)
12731703Sbostic 		machine = MACHINE;
12840390Sbostic 
12965283Sbostic 	/*
13065283Sbostic 	 * 1: If the user specified a MANPATH variable, or set the -M
13165283Sbostic 	 *    option, we replace the _default list with the user's list,
13265283Sbostic 	 *    appending the entries in the _subdir list and the machine.
13365283Sbostic 	 */
13465283Sbostic 	if (p_path == NULL)
13565283Sbostic 		p_path = getenv("MANPATH");
13665283Sbostic 	if (p_path != NULL) {
13765283Sbostic 		while ((tp = defp->list.qe_next) != NULL) {
13865283Sbostic 			free(tp->s);
13965283Sbostic 			queue_remove(&defp->list, tp, ENTRY *, list);
14065283Sbostic 		}
14165283Sbostic 		for (p = strtok(p_path, ":");
14265283Sbostic 		    p != NULL; p = strtok(NULL, ":")) {
14365283Sbostic 			slashp = p[strlen(p) - 1] == '/' ? "" : "/";
14465283Sbostic 			subp = getlist("_subdir");
14565283Sbostic 			if (subp != NULL)
14665283Sbostic 				subp = subp->list.qe_next;
14765283Sbostic 			for (; subp != NULL; subp = subp->list.qe_next) {
14865283Sbostic 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
14965283Sbostic 				    p, slashp, subp->s, machine);
15065283Sbostic 				if ((tp = malloc(sizeof(ENTRY))) == NULL ||
15165283Sbostic 				    (tp->s = strdup(buf)) == NULL)
15265283Sbostic 					err(1, NULL);
15365283Sbostic 				queue_enter_tail(&defp->list,
15465283Sbostic 				    tp, ENTRY *, list);
15565283Sbostic 			}
15665283Sbostic 		}
15742402Sbostic 	}
15840390Sbostic 
15965283Sbostic 	/*
16065283Sbostic 	 * 2: If the user did not specify MANPATH, -M or a section, rewrite
16165283Sbostic 	 *    the _default list to include the _subdir list and the machine.
16265283Sbostic 	 */
16365283Sbostic 	if ((section = getlist(*argv)) != NULL)
16465283Sbostic 		++argv;
16565283Sbostic 	if (p_path == NULL && section == NULL) {
16665283Sbostic 		defnewp = addlist("_default_new");
16765283Sbostic 		if (defp->list.qe_next != NULL)
16865283Sbostic 			defp = defp->list.qe_next;
16965283Sbostic 		for (; defp != NULL; defp = defp->list.qe_next) {
17065283Sbostic 			slashp = defp->s[strlen(defp->s) - 1] == '/' ? "" : "/";
17165283Sbostic 			subp = getlist("_subdir");
17265283Sbostic 			if (subp != NULL)
17365283Sbostic 				subp = subp->list.qe_next;
17465283Sbostic 			for (; subp != NULL; subp = subp->list.qe_next) {
17565283Sbostic 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
17665283Sbostic 				defp->s, slashp, subp->s, machine);
17765283Sbostic 				if ((tp = malloc(sizeof(ENTRY))) == NULL ||
17865283Sbostic 				    (tp->s = strdup(buf)) == NULL)
17965283Sbostic 					err(1, NULL);
18065283Sbostic 				queue_enter_tail(&defnewp->list,
18165283Sbostic 				    tp, ENTRY *, list);
18265283Sbostic 			}
18365283Sbostic 		}
18465283Sbostic 		defp = getlist("_default");
18565283Sbostic 		while ((tp = defp->list.qe_next) != NULL) {
18665283Sbostic 			free(tp->s);
18765283Sbostic 			queue_remove(&defp->list, tp, ENTRY *, list);
18865283Sbostic 		}
18965283Sbostic 		free(defp->s);
19065283Sbostic 		queue_remove(&defp->tags, defp, ENTRY *, tags);
19165283Sbostic 		defnewp = getlist("_default_new");
19265283Sbostic 		free(defnewp->s);
19365283Sbostic 		defnewp->s = "_default";
19465283Sbostic 		defp = defnewp;
19565283Sbostic 	}
19644418Strent 
19765283Sbostic 	/*
19865283Sbostic 	 * 3: If the user set the -m option, insert the user's list before
19965283Sbostic 	 *    whatever list we have, again appending the _subdir list and
20065283Sbostic 	 *    the machine.
20165283Sbostic 	 */
20265283Sbostic 	if (p_add != NULL)
20365283Sbostic 		for (p = strtok(p_add, ":"); p != NULL; p = strtok(NULL, ":")) {
20465283Sbostic 			slashp = p[strlen(p) - 1] == '/' ? "" : "/";
20565283Sbostic 			subp = getlist("_subdir");
20665283Sbostic 			if (subp != NULL)
20765283Sbostic 				subp = subp->list.qe_next;
20865283Sbostic 			for (; subp != NULL; subp = subp->list.qe_next) {
20965283Sbostic 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
21065283Sbostic 				    p, slashp, subp->s, machine);
21165283Sbostic 				if ((tp = malloc(sizeof(ENTRY))) == NULL ||
21265283Sbostic 				    (tp->s = strdup(buf)) == NULL)
21365283Sbostic 					err(1, NULL);
21465283Sbostic 				queue_enter_head(&defp->list,
21565283Sbostic 				    tp, ENTRY *, list);
21665283Sbostic 			}
21744418Strent 		}
21865283Sbostic 
21965283Sbostic 	/*
22065283Sbostic 	 * 4: If none of MANPATH, -M, or -m were specified, and a section was,
22165283Sbostic 	 *    rewrite the section's paths (if they have a trailing slash) to
22265283Sbostic 	 *    append the _subdir list and the machine.  This then becomes the
22365283Sbostic 	 *    _default list.
22465283Sbostic 	 */
22565283Sbostic 	if (p_path == NULL && p_add == NULL && section != NULL) {
22665283Sbostic 		sectnewp = addlist("_section_new");
22765283Sbostic 		if ((sectp = section)->list.qe_next != NULL)
22865283Sbostic 			sectp = sectp->list.qe_next;
22965283Sbostic 		while (sectp != NULL) {
23065283Sbostic 			if (sectp->s[strlen(sectp->s) - 1] != '/') {
23165283Sbostic 				tp = sectp;
23265283Sbostic 				sectp = sectp->list.qe_next;
23365283Sbostic 				queue_remove(&section->list, tp, ENTRY *, list);
23465283Sbostic 				queue_enter_tail(&sectnewp->list,
23565283Sbostic 				    tp, ENTRY *, list);
23665283Sbostic 				continue;
23765283Sbostic 			}
23865283Sbostic 			subp = getlist("_subdir");
23965283Sbostic 			if (subp != NULL)
24065283Sbostic 				subp = subp->list.qe_next;
24165283Sbostic 			for (; subp != NULL; subp = subp->list.qe_next) {
24265283Sbostic 				(void)snprintf(buf, sizeof(buf),
24365283Sbostic 				    "%s%s{/%s,}", sectp->s, subp->s, machine);
24465283Sbostic 				if ((tp = malloc(sizeof(ENTRY))) == NULL ||
24565283Sbostic 				    (tp->s = strdup(buf)) == NULL)
24665283Sbostic 					err(1, NULL);
24765283Sbostic 				queue_enter_tail(&sectnewp->list,
24865283Sbostic 				    tp, ENTRY *, list);
24965283Sbostic 			}
25065283Sbostic 			sectp = sectp->list.qe_next;
25165283Sbostic 		}
25265283Sbostic 		sectnewp->s = section->s;
25365283Sbostic 		defp = sectnewp;
25465283Sbostic 		queue_remove(&section->tags, section, ENTRY *, tags);
25542402Sbostic 	}
25640390Sbostic 
25765283Sbostic 	/*
25865283Sbostic 	 * 5: Search for the files.  Set up an interrupt handler, so the
25965283Sbostic 	 *    temporary files go away.
26065283Sbostic 	 */
26165283Sbostic 	(void)signal(SIGINT, onsig);
26265283Sbostic 
26365283Sbostic 	memset(&pg, 0, sizeof(pg));
26465283Sbostic 	for (found = 0; *argv; ++argv)
26565283Sbostic 		if (manual(*argv, defp, &pg))
26665283Sbostic 			found = 1;
26765283Sbostic 
26865283Sbostic 	/*
26965283Sbostic 	 * 7: If nothing found, we're done.
27065283Sbostic 	 */
27165283Sbostic 	if (!found) {
27265283Sbostic 		cleanup();
27365283Sbostic 		exit (1);
27442402Sbostic 	}
27542402Sbostic 
27665283Sbostic 	/* 8: If it's simple, display it fast. */
27765283Sbostic 	if (f_cat) {
27865283Sbostic 		found = 0;
27965283Sbostic 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
28065283Sbostic 			if (*ap == '\0')
28165283Sbostic 				continue;
28265283Sbostic 			cat(*ap);
28365283Sbostic 			if (!f_all) {
28465283Sbostic 				found = 1;
28565283Sbostic 				break;
28665283Sbostic 			}
28765283Sbostic 		}
28865283Sbostic 		if (!found) {
28965283Sbostic 			if (intmpp != NULL)
29065283Sbostic 				intmpp = intmpp->list.qe_next;
29165283Sbostic 			for (; intmpp != NULL; intmpp = intmpp->list.qe_next) {
29265283Sbostic 				cat(intmpp->s);
29365283Sbostic 				if (!f_all)
29465283Sbostic 					break;
29565283Sbostic 			}
29665283Sbostic 		}
29765283Sbostic 		cleanup();
29865283Sbostic 		exit (0);
29965283Sbostic 	}
30065283Sbostic 	if (f_how) {
30165283Sbostic 		found = 0;
30265283Sbostic 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
30365283Sbostic 			if (*ap == '\0')
30465283Sbostic 				continue;
30565283Sbostic 			how(*ap);
30665283Sbostic 			if (!f_all) {
30765283Sbostic 				found = 1;
30865283Sbostic 				break;
30965283Sbostic 			}
31065283Sbostic 		}
31165283Sbostic 		if (!found) {
31265283Sbostic 			intmpp = getlist("_intmp");
31365283Sbostic 			if (intmpp != NULL)
31465283Sbostic 				intmpp = intmpp->list.qe_next;
31565283Sbostic 			for (; intmpp != NULL; intmpp = intmpp->list.qe_next) {
31665283Sbostic 				how(intmpp->s);
31765283Sbostic 				if (!f_all)
31865283Sbostic 					break;
31965283Sbostic 			}
32065283Sbostic 		}
32165283Sbostic 		cleanup();
32265283Sbostic 		exit (0);
32365283Sbostic 	}
32465283Sbostic 	if (f_where) {
32565283Sbostic 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
32665283Sbostic 			if (*ap == '\0')
32765283Sbostic 				continue;
32865283Sbostic 			(void)printf("%s\n", *ap);
32965283Sbostic 		}
33065283Sbostic 		intmpp = getlist("_intmp");
33165283Sbostic 		if (intmpp != NULL)
33265283Sbostic 			intmpp = intmpp->list.qe_next;
33365283Sbostic 		for (; intmpp != NULL; intmpp = intmpp->list.qe_next)
33465283Sbostic 			(void)printf("%s\n", intmpp->s);
33565283Sbostic 		cleanup();
33665283Sbostic 		exit (0);
33765283Sbostic 	}
33865283Sbostic 
33965283Sbostic 	/*
34065283Sbostic 	 * 9: We display things in a single command; build a list of things
34165283Sbostic 	 *    to display.
34265283Sbostic 	 */
34365283Sbostic 	found = 0;
34465283Sbostic 	for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
34565283Sbostic 		if (**ap == '\0')
34665283Sbostic 			continue;
34765283Sbostic 		len += strlen(*ap) + 1;
34865283Sbostic 		if (!f_all) {
34965283Sbostic 			found = 1;
35065283Sbostic 			break;
35165283Sbostic 		}
35265283Sbostic 	}
35365283Sbostic 	if (!found) {
35465283Sbostic 		intmpp = getlist("_intmp");
35565283Sbostic 		if (intmpp != NULL)
35665283Sbostic 			intmpp = intmpp->list.qe_next;
35765283Sbostic 		for (; intmpp != NULL; intmpp = intmpp->list.qe_next) {
35865283Sbostic 			len += strlen(intmpp->s);
35965283Sbostic 			if (!f_all)
36065283Sbostic 				break;
36165283Sbostic 		}
36265283Sbostic 	}
36365283Sbostic 
36465283Sbostic 	if ((cmd = malloc(len)) == NULL) {
36565283Sbostic 		cleanup();
36665283Sbostic 		err(1, NULL);
36765283Sbostic 	}
36865283Sbostic 	p = cmd;
36965283Sbostic 	len = strlen(pager);
37065283Sbostic 	memmove(p, pager, len);
37165283Sbostic 	p += len;
37265283Sbostic 	*p++ = ' ';
37365283Sbostic 	found = 0;
37465283Sbostic 	for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
37565283Sbostic 		if (**ap == '\0')
37665283Sbostic 			continue;
37765283Sbostic 		len = strlen(*ap);
37865283Sbostic 		memmove(p, *ap, len);
37965283Sbostic 		p += len;
38065283Sbostic 		*p++ = ' ';
38165283Sbostic 		if (!f_all) {
38265283Sbostic 			found = 1;
38365283Sbostic 			break;
38465283Sbostic 		}
38565283Sbostic 	}
38665283Sbostic 	if (!found) {
38765283Sbostic 		intmpp = getlist("_intmp");
38865283Sbostic 		if (intmpp != NULL)
38965283Sbostic 			intmpp = intmpp->list.qe_next;
39065283Sbostic 		for (; intmpp != NULL; intmpp = intmpp->list.qe_next) {
39165283Sbostic 			len = strlen(intmpp->s);
39265283Sbostic 			memmove(p, intmpp->s, len);
39365283Sbostic 			p += len;
39465283Sbostic 			*p++ = ' ';
39565283Sbostic 		}
39665283Sbostic 	}
39765283Sbostic 	*p = '\0';
39865283Sbostic 
39965283Sbostic 	/* Use system(3) in case someone's pager is "pager arg1 arg2". */
40065283Sbostic 	(void)system(cmd);
40165283Sbostic 
40265283Sbostic 	cleanup();
40333354Sbostic 	exit(0);
40433354Sbostic }
40531703Sbostic 
40640390Sbostic /*
40731778Sbostic  * manual --
40865283Sbostic  *	Search the manuals for the pages.
40931778Sbostic  */
41065283Sbostic static int
41165283Sbostic manual(page, list, pg)
41265283Sbostic 	char *page;
41365283Sbostic 	ENTRY *list;
41465283Sbostic 	glob_t *pg;
41531703Sbostic {
41665283Sbostic 	ENTRY *listp, *missp, *sufp, *tp;
41765283Sbostic 	int anyfound, cnt, found;
41865283Sbostic 	char *p, buf[128];
41931703Sbostic 
42065283Sbostic 	anyfound = 0;
42165283Sbostic 	buf[0] = '*';
42265283Sbostic 
42365283Sbostic 	/* For each element in the list... */
42465283Sbostic 	if (list != NULL)
42565283Sbostic 		list = list->list.qe_next;
42665283Sbostic 	for (listp = list; listp != NULL; listp = listp->list.qe_next) {
42765283Sbostic 		(void)snprintf(buf, sizeof(buf), "%s/%s.*", listp->s, page);
42865283Sbostic 		if (glob(buf,
42965283Sbostic 		    GLOB_APPEND | GLOB_NOSORT | GLOB_BRACE, NULL, pg)) {
43065283Sbostic 			cleanup();
43165283Sbostic 			err(1, "globbing");
43265283Sbostic 		}
43365283Sbostic 		if (pg->gl_matchc == 0)
43465283Sbostic 			continue;
43565283Sbostic 
43665283Sbostic 		/* Find out if it's really a man page. */
43765283Sbostic 		for (cnt = 1; cnt <= pg->gl_matchc; ++cnt) {
43865283Sbostic 
43965283Sbostic 			/* Try the _suffix key words first. */
44065283Sbostic 			sufp = getlist("_suffix");
44165283Sbostic 			if (sufp != NULL)
44265283Sbostic 				sufp = sufp->list.qe_next;
44365283Sbostic 			for (found = 0;
44465283Sbostic 			    sufp != NULL; sufp = sufp->list.qe_next) {
44565283Sbostic 				(void)snprintf(buf,
44665283Sbostic 				     sizeof(buf), "*%s", sufp->s);
44765283Sbostic 				if (!fnmatch(buf,
44865283Sbostic 				    pg->gl_pathv[pg->gl_pathc - cnt], 0)) {
44965283Sbostic 					found = 1;
45065283Sbostic 					break;
45165283Sbostic 				}
45265283Sbostic 			}
45365283Sbostic 			if (found) {
45465283Sbostic 				anyfound = 1;
45565283Sbostic 				if (!f_all)
45665283Sbostic 					break;
45742402Sbostic 				continue;
45863515Sbostic 			}
45965283Sbostic 
46065283Sbostic 			/* Try the _build key words next. */
46165283Sbostic 			sufp = getlist("_build");
46265283Sbostic 			if (sufp != NULL)
46365283Sbostic 				sufp = sufp->list.qe_next;
46465283Sbostic 			for (found = 0;
46565283Sbostic 			    sufp != NULL; sufp = sufp->list.qe_next) {
46665283Sbostic 				for (p = sufp->s;
46765283Sbostic 				    *p != '\0' && !isspace(*p); ++p);
46865283Sbostic 				if (*p == '\0')
46965283Sbostic 					continue;
47065283Sbostic 				*p = '\0';
47165283Sbostic 				(void)snprintf(buf,
47265283Sbostic 				     sizeof(buf), "*%s", sufp->s);
47365283Sbostic 				if (!fnmatch(buf,
47465283Sbostic 				    pg->gl_pathv[pg->gl_pathc - cnt], 0)) {
47565283Sbostic 					if (!f_where) {
47665283Sbostic 						build_page(p + 1,
47765283Sbostic 						    pg->gl_pathv[pg->gl_pathc -
47865283Sbostic 						    cnt]);
47965283Sbostic 						pg->gl_pathv[pg->gl_pathc -
48065283Sbostic 						    cnt] = "";
48165283Sbostic 					}
48265283Sbostic 					*p = ' ';
48365283Sbostic 					found = 1;
48465283Sbostic 					break;
48565283Sbostic 				}
48665283Sbostic 				*p = ' ';
48765283Sbostic 			}
48865283Sbostic 			if (found) {
48965283Sbostic 				anyfound = 1;
49065283Sbostic 				if (!f_all)
49165283Sbostic 					break;
49242402Sbostic 				continue;
49363515Sbostic 			}
49465283Sbostic 
49565283Sbostic 			/* It's not a man page, forget about it. */
49665283Sbostic 			pg->gl_pathv[pg->gl_pathc - cnt] = "";
49742402Sbostic 		}
49842402Sbostic 
49965283Sbostic 		if (anyfound && !f_all)
50065283Sbostic 			break;
50131703Sbostic 	}
50265283Sbostic 
50365283Sbostic 	/* If not found, enter onto the missing list. */
50465283Sbostic 	if (!anyfound) {
50565283Sbostic 		if ((missp = getlist("_missing")) == NULL)
50665283Sbostic 			missp = addlist("_missing");
50765283Sbostic 		if ((tp = malloc(sizeof(ENTRY))) == NULL ||
50865283Sbostic 		    (tp->s = strdup(page)) == NULL) {
50965283Sbostic 			cleanup();
51065283Sbostic 			err(1, NULL);
51165283Sbostic 		}
51265283Sbostic 		queue_enter_tail(&missp->list, tp, ENTRY *, list);
51365283Sbostic 	}
51465283Sbostic 	return (anyfound);
51531703Sbostic }
51631703Sbostic 
51765283Sbostic /*
51865283Sbostic  * build_page --
51965283Sbostic  *	Build a man page for display.
52065283Sbostic  */
52165283Sbostic static void
52265283Sbostic build_page(fmt, path)
52365283Sbostic 	char *fmt, *path;
52465283Sbostic {
52565283Sbostic 	static int warned;
52665283Sbostic 	ENTRY *intmpp, *tp;
52765283Sbostic 	int fd;
52865283Sbostic 	char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[sizeof(_PATH_TMP)];
52965283Sbostic 
53065283Sbostic 	/* Let the user know this may take awhile. */
53165283Sbostic 	if (!warned) {
53265283Sbostic 		warned = 1;
53365283Sbostic 		warnx("Formatting manual page...");
53465283Sbostic 	}
53565283Sbostic 
53665283Sbostic 	/* Add an "in tmp" list. */
53765283Sbostic 	if ((intmpp = getlist("_intmp")) == NULL)
53865283Sbostic 		intmpp = addlist("_intmp");
53965283Sbostic 
54065283Sbostic 	/* Move to the printf(3) format string. */
54165283Sbostic 	for (; *fmt && isspace(*fmt); ++fmt);
54265283Sbostic 
54365283Sbostic 	/*
54465283Sbostic 	 * Get a temporary file and build a version of the file to display.
54565283Sbostic 	 * Link the built file into the list.
54665283Sbostic 	 */
54765283Sbostic 	(void)strcpy(tpath, _PATH_TMP);
54865283Sbostic 	if ((fd = mkstemp(tpath)) == -1) {
54965283Sbostic 		cleanup();
55065283Sbostic 		err(1, "%s", tpath);
55165283Sbostic 	}
55265283Sbostic 	(void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
55365283Sbostic 	(void)snprintf(cmd, sizeof(cmd), buf, path);
55465283Sbostic 	(void)system(cmd);
55565283Sbostic 	(void)close(fd);
55665283Sbostic 	if ((tp = malloc(sizeof(ENTRY))) == NULL ||
55765283Sbostic 	    (tp->s = strdup(tpath)) == NULL) {
55865283Sbostic 		cleanup();
55965283Sbostic 		err(1, NULL);
56065283Sbostic 	}
56165283Sbostic 	queue_enter_tail(&intmpp->list, tp, ENTRY *, list);
56265283Sbostic }
56365283Sbostic 
56431778Sbostic /*
56544938Sbostic  * how --
56644938Sbostic  *	display how information
56744938Sbostic  */
56865283Sbostic static void
56944938Sbostic how(fname)
57044938Sbostic 	char *fname;
57144938Sbostic {
57244938Sbostic 	register FILE *fp;
57344938Sbostic 
57444938Sbostic 	register int lcnt, print;
57544938Sbostic 	register char *p;
57644938Sbostic 	char buf[BUFSIZ];
57744938Sbostic 
57844938Sbostic 	if (!(fp = fopen(fname, "r"))) {
57965283Sbostic 		cleanup();
58065283Sbostic 		err(1, "%s", fname);
58144938Sbostic 	}
58244938Sbostic #define	S1	"SYNOPSIS"
58344938Sbostic #define	S2	"S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
58444938Sbostic #define	D1	"DESCRIPTION"
58544938Sbostic #define	D2	"D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
58644938Sbostic 	for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
58744938Sbostic 		if (!strncmp(buf, S1, sizeof(S1) - 1) ||
58844938Sbostic 		    !strncmp(buf, S2, sizeof(S2) - 1)) {
58944938Sbostic 			print = 1;
59044938Sbostic 			continue;
59144938Sbostic 		} else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
59244938Sbostic 		    !strncmp(buf, D2, sizeof(D2) - 1))
59344938Sbostic 			return;
59444938Sbostic 		if (!print)
59544938Sbostic 			continue;
59644938Sbostic 		if (*buf == '\n')
59744938Sbostic 			++lcnt;
59844938Sbostic 		else {
59944938Sbostic 			for(; lcnt; --lcnt)
60044938Sbostic 				(void)putchar('\n');
60144938Sbostic 			for (p = buf; isspace(*p); ++p);
60244938Sbostic 			(void)fputs(p, stdout);
60344938Sbostic 		}
60444938Sbostic 	}
60544938Sbostic 	(void)fclose(fp);
60644938Sbostic }
60765283Sbostic 
60844938Sbostic /*
60933809Sbostic  * cat --
61033809Sbostic  *	cat out the file
61131778Sbostic  */
61265283Sbostic static void
61333809Sbostic cat(fname)
61433809Sbostic 	char *fname;
61531703Sbostic {
61633809Sbostic 	register int fd, n;
61733809Sbostic 	char buf[BUFSIZ];
61831703Sbostic 
61944938Sbostic 	if ((fd = open(fname, O_RDONLY, 0)) < 0) {
62065283Sbostic 		cleanup();
62165283Sbostic 		err(1, "%s", fname);
62231703Sbostic 	}
62333809Sbostic 	while ((n = read(fd, buf, sizeof(buf))) > 0)
62465283Sbostic 		if (write(STDOUT_FILENO, buf, n) != n) {
62565283Sbostic 			cleanup();
62665283Sbostic 			err(1, "write");
62733809Sbostic 		}
62833809Sbostic 	if (n == -1) {
62965283Sbostic 		cleanup();
63065283Sbostic 		err(1, "read");
63133809Sbostic 	}
63233809Sbostic 	(void)close(fd);
63331703Sbostic }
63431703Sbostic 
63531778Sbostic /*
63640390Sbostic  * check_pager --
63740390Sbostic  *	check the user supplied page information
63840390Sbostic  */
63965283Sbostic static char *
64040390Sbostic check_pager(name)
64140390Sbostic 	char *name;
64240390Sbostic {
64340390Sbostic 	register char *p;
64442402Sbostic 	char *save;
64540390Sbostic 
64640390Sbostic 	/*
64740390Sbostic 	 * if the user uses "more", we make it "more -s"; watch out for
64840390Sbostic 	 * PAGER = "mypager /usr/ucb/more"
64940390Sbostic 	 */
65040390Sbostic 	for (p = name; *p && !isspace(*p); ++p);
65140390Sbostic 	for (; p > name && *p != '/'; --p);
65240390Sbostic 	if (p != name)
65340390Sbostic 		++p;
65440390Sbostic 
65540390Sbostic 	/* make sure it's "more", not "morex" */
65640390Sbostic 	if (!strncmp(p, "more", 4) && (!p[4] || isspace(p[4]))){
65740390Sbostic 		save = name;
65840390Sbostic 		/* allocate space to add the "-s" */
65940390Sbostic 		if (!(name =
66040390Sbostic 		    malloc((u_int)(strlen(save) + sizeof("-s") + 1))))
66165283Sbostic 			err(1, NULL);
66240390Sbostic 		(void)sprintf(name, "%s %s", save, "-s");
66340390Sbostic 	}
66440390Sbostic 	return(name);
66540390Sbostic }
66640390Sbostic 
66740390Sbostic /*
66834057Sbostic  * jump --
66934057Sbostic  *	strip out flag argument and jump
67034057Sbostic  */
67165283Sbostic static void
67234057Sbostic jump(argv, flag, name)
67365283Sbostic 	char **argv, *flag, *name;
67434057Sbostic {
67565283Sbostic 	char **arg;
67634057Sbostic 
67734057Sbostic 	argv[0] = name;
67834057Sbostic 	for (arg = argv + 1; *arg; ++arg)
67934057Sbostic 		if (!strcmp(*arg, flag))
68034057Sbostic 			break;
68134057Sbostic 	for (; *arg; ++arg)
68234057Sbostic 		arg[0] = arg[1];
68334057Sbostic 	execvp(name, argv);
68442402Sbostic 	(void)fprintf(stderr, "%s: Command not found.\n", name);
68534057Sbostic 	exit(1);
68634057Sbostic }
68734057Sbostic 
68865283Sbostic /*
68965283Sbostic  * onsig --
69065283Sbostic  *	If signaled, delete the temporary files.
69165283Sbostic  */
69265283Sbostic static void
69365283Sbostic onsig(signo)
69465283Sbostic 	int signo;
69565283Sbostic {
69665283Sbostic 	cleanup();
69765283Sbostic 
69865283Sbostic 	(void)signal(SIGINT, SIG_DFL);
69965283Sbostic 	(void)kill(getpid(), SIGINT);
70065283Sbostic }
70165283Sbostic 
70234057Sbostic /*
70365283Sbostic  * cleanup --
70465283Sbostic  *	Clean up temporary files, show any error messages.
70565283Sbostic  */
70665283Sbostic static void
70765283Sbostic cleanup()
70865283Sbostic {
70965283Sbostic 	ENTRY *intmpp, *missp;
71065283Sbostic 	int sverrno;
71165283Sbostic 
71265283Sbostic 	sverrno = errno;
71365283Sbostic 
71465283Sbostic 	missp = getlist("_missing");
71565283Sbostic 	if (missp != NULL)
71665283Sbostic 		missp = missp->list.qe_next;
71765283Sbostic 	if (missp != NULL)
71865283Sbostic 		for (; missp != NULL; missp = missp->list.qe_next)
71965283Sbostic 			warnx("no entry for %s in the manual.", missp->s);
72065283Sbostic 
72165283Sbostic 	intmpp = getlist("_intmp");
72265283Sbostic 	if (intmpp != NULL)
72365283Sbostic 		intmpp = intmpp->list.qe_next;
72465283Sbostic 	for (; intmpp != NULL; intmpp = intmpp->list.qe_next)
72565283Sbostic 		(void)unlink(intmpp->s);
72665283Sbostic 
72765283Sbostic 	errno = sverrno;
72865283Sbostic }
72965283Sbostic 
73065283Sbostic /*
73134057Sbostic  * usage --
73240390Sbostic  *	print usage message and die
73334057Sbostic  */
73465283Sbostic static void
73534057Sbostic usage()
73634057Sbostic {
73740390Sbostic 	(void)fprintf(stderr,
738*65284Sbostic     "usage: man [-ac] [-C file] [-M path] [-m path] [section] title ...\n");
73934057Sbostic 	exit(1);
74034057Sbostic }
741