xref: /csrg-svn/usr.bin/man/man.c (revision 65292)
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*65292Sbostic static char sccsid[] = "@(#)man.c	8.12 (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 
3865289Sbostic static void	 build_page __P((char *, char **));
3965283Sbostic static void	 cat __P((char *));
4065283Sbostic static char	*check_pager __P((char *));
4165291Sbostic static int	 cleanup __P((void));
4265283Sbostic static void	 how __P((char *));
4365283Sbostic static void	 jump __P((char **, char *, char *));
4465291Sbostic static int	 manual __P((char *, TAG *, 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;
5565291Sbostic 	TAG *defp, *defnewp, *section, *sectnewp, *subp;
5665291Sbostic 	ENTRY *e_defp, *e_sectp, *e_subp, *ep;
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;
6165284Sbostic 	char *conffile, buf[MAXPATHLEN * 2];
6231703Sbostic 
6365283Sbostic 	f_cat = f_how = 0;
64*65292Sbostic 	conffile = p_add = p_path = NULL;
6565284Sbostic 	while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:w")) != EOF)
6665283Sbostic 		switch (ch) {
6740390Sbostic 		case 'a':
6840390Sbostic 			f_all = 1;
6931703Sbostic 			break;
7065284Sbostic 		case 'C':
7165284Sbostic 			conffile = optarg;
7265284Sbostic 			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;
11365289Sbostic 		else if ((pager = getenv("PAGER")) != NULL)
11440390Sbostic 			pager = check_pager(pager);
11531779Sbostic 		else
11637892Sbostic 			pager = _PATH_PAGER;
11740390Sbostic 
11865283Sbostic 	/* Read the configuration file. */
11965284Sbostic 	config(conffile);
12065283Sbostic 
12165291Sbostic 	/* Get the machine type. */
12265291Sbostic 	if ((machine = getenv("MACHINE")) == NULL)
12365291Sbostic 		machine = MACHINE;
12465291Sbostic 
12565283Sbostic 	/* If there's no _default list, create an empty one. */
12665283Sbostic 	if ((defp = getlist("_default")) == NULL)
12765283Sbostic 		defp = addlist("_default");
12865283Sbostic 
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) {
13765291Sbostic 		while ((e_defp = defp->list.tqh_first) != NULL) {
13865291Sbostic 			free(e_defp->s);
13965291Sbostic 			TAILQ_REMOVE(&defp->list, e_defp, q);
14065283Sbostic 		}
14165283Sbostic 		for (p = strtok(p_path, ":");
14265283Sbostic 		    p != NULL; p = strtok(NULL, ":")) {
14365283Sbostic 			slashp = p[strlen(p) - 1] == '/' ? "" : "/";
14465291Sbostic 			e_subp = (subp = getlist("_subdir")) == NULL ?
14565291Sbostic 			    NULL : subp->list.tqh_first;
14665291Sbostic 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
14765283Sbostic 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
14865291Sbostic 				    p, slashp, e_subp->s, machine);
14965291Sbostic 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
15065291Sbostic 				    (ep->s = strdup(buf)) == NULL)
15165283Sbostic 					err(1, NULL);
15265291Sbostic 				TAILQ_INSERT_TAIL(&defp->list, ep, q);
15365283Sbostic 			}
15465283Sbostic 		}
15542402Sbostic 	}
15640390Sbostic 
15765283Sbostic 	/*
15865283Sbostic 	 * 2: If the user did not specify MANPATH, -M or a section, rewrite
15965283Sbostic 	 *    the _default list to include the _subdir list and the machine.
16065283Sbostic 	 */
16165283Sbostic 	if ((section = getlist(*argv)) != NULL)
16265283Sbostic 		++argv;
16365283Sbostic 	if (p_path == NULL && section == NULL) {
16465283Sbostic 		defnewp = addlist("_default_new");
16565291Sbostic 		e_defp =
16665291Sbostic 		    defp->list.tqh_first == NULL ? NULL : defp->list.tqh_first;
16765291Sbostic 		for (; e_defp != NULL; e_defp = e_defp->q.tqe_next) {
16865291Sbostic 			slashp =
16965291Sbostic 			    e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
17065291Sbostic 			e_subp = (subp = getlist("_subdir")) == NULL ?
17165291Sbostic 			    NULL : subp->list.tqh_first;
17265291Sbostic 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
17365283Sbostic 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
17465291Sbostic 				e_defp->s, slashp, e_subp->s, machine);
17565291Sbostic 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
17665291Sbostic 				    (ep->s = strdup(buf)) == NULL)
17765283Sbostic 					err(1, NULL);
17865291Sbostic 				TAILQ_INSERT_TAIL(&defnewp->list, ep, q);
17965283Sbostic 			}
18065283Sbostic 		}
18165283Sbostic 		defp = getlist("_default");
18265291Sbostic 		while ((e_defp = defp->list.tqh_first) != NULL) {
18365291Sbostic 			free(e_defp->s);
18465291Sbostic 			TAILQ_REMOVE(&defp->list, e_defp, q);
18565283Sbostic 		}
18665283Sbostic 		free(defp->s);
18765291Sbostic 		TAILQ_REMOVE(&head, defp, q);
18865283Sbostic 		defnewp = getlist("_default_new");
18965283Sbostic 		free(defnewp->s);
19065283Sbostic 		defnewp->s = "_default";
19165283Sbostic 		defp = defnewp;
19265283Sbostic 	}
19344418Strent 
19465283Sbostic 	/*
19565283Sbostic 	 * 3: If the user set the -m option, insert the user's list before
19665283Sbostic 	 *    whatever list we have, again appending the _subdir list and
19765283Sbostic 	 *    the machine.
19865283Sbostic 	 */
19965283Sbostic 	if (p_add != NULL)
20065283Sbostic 		for (p = strtok(p_add, ":"); p != NULL; p = strtok(NULL, ":")) {
20165283Sbostic 			slashp = p[strlen(p) - 1] == '/' ? "" : "/";
20265291Sbostic 			e_subp = (subp = getlist("_subdir")) == NULL ?
20365291Sbostic 			    NULL : subp->list.tqh_first;
20465291Sbostic 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
20565283Sbostic 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
20665291Sbostic 				    p, slashp, e_subp->s, machine);
20765291Sbostic 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
20865291Sbostic 				    (ep->s = strdup(buf)) == NULL)
20965283Sbostic 					err(1, NULL);
21065291Sbostic 				TAILQ_INSERT_HEAD(&defp->list, ep, q);
21165283Sbostic 			}
21244418Strent 		}
21365283Sbostic 
21465283Sbostic 	/*
21565283Sbostic 	 * 4: If none of MANPATH, -M, or -m were specified, and a section was,
21665283Sbostic 	 *    rewrite the section's paths (if they have a trailing slash) to
21765283Sbostic 	 *    append the _subdir list and the machine.  This then becomes the
21865283Sbostic 	 *    _default list.
21965283Sbostic 	 */
22065283Sbostic 	if (p_path == NULL && p_add == NULL && section != NULL) {
22165283Sbostic 		sectnewp = addlist("_section_new");
22265291Sbostic 		for (e_sectp = section->list.tqh_first;
22365291Sbostic 		    e_sectp != NULL; e_sectp = e_sectp->q.tqe_next) {
22465291Sbostic 			if (e_sectp->s[strlen(e_sectp->s) - 1] != '/') {
22565285Sbostic 				(void)snprintf(buf, sizeof(buf),
22665291Sbostic 				    "%s{/%s,}", e_sectp->s, machine);
22765291Sbostic 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
22865291Sbostic 				    (ep->s = strdup(buf)) == NULL)
22965285Sbostic 					err(1, NULL);
23065291Sbostic 				TAILQ_INSERT_TAIL(&sectnewp->list, ep, q);
23165283Sbostic 				continue;
23265283Sbostic 			}
23365291Sbostic 			e_subp = (subp = getlist("_subdir")) == NULL ?
23465291Sbostic 			    NULL : subp->list.tqh_first;
23565291Sbostic 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
23665291Sbostic 				(void)snprintf(buf, sizeof(buf), "%s%s{/%s,}",
23765291Sbostic 				    e_sectp->s, e_subp->s, machine);
23865291Sbostic 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
23965291Sbostic 				    (ep->s = strdup(buf)) == NULL)
24065283Sbostic 					err(1, NULL);
24165291Sbostic 				TAILQ_INSERT_TAIL(&sectnewp->list, ep, q);
24265283Sbostic 			}
24365283Sbostic 		}
24465283Sbostic 		sectnewp->s = section->s;
24565283Sbostic 		defp = sectnewp;
24665291Sbostic 		TAILQ_REMOVE(&head, section, q);
24742402Sbostic 	}
24840390Sbostic 
24965283Sbostic 	/*
25065283Sbostic 	 * 5: Search for the files.  Set up an interrupt handler, so the
25165283Sbostic 	 *    temporary files go away.
25265283Sbostic 	 */
25365283Sbostic 	(void)signal(SIGINT, onsig);
25465291Sbostic 	(void)signal(SIGHUP, onsig);
25565283Sbostic 
25665283Sbostic 	memset(&pg, 0, sizeof(pg));
25765283Sbostic 	for (found = 0; *argv; ++argv)
25865283Sbostic 		if (manual(*argv, defp, &pg))
25965283Sbostic 			found = 1;
26065283Sbostic 
26165290Sbostic 	/* 6: If nothing found, we're done. */
26265283Sbostic 	if (!found) {
26365291Sbostic 		(void)cleanup();
26465283Sbostic 		exit (1);
26542402Sbostic 	}
26642402Sbostic 
26765290Sbostic 	/* 7: If it's simple, display it fast. */
26865283Sbostic 	if (f_cat) {
26965283Sbostic 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
27065287Sbostic 			if (**ap == '\0')
27165283Sbostic 				continue;
27265283Sbostic 			cat(*ap);
27365283Sbostic 		}
27465291Sbostic 		exit (cleanup());
27565283Sbostic 	}
27665283Sbostic 	if (f_how) {
27765283Sbostic 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
27865287Sbostic 			if (**ap == '\0')
27965283Sbostic 				continue;
28065283Sbostic 			how(*ap);
28165283Sbostic 		}
28265291Sbostic 		exit(cleanup());
28365283Sbostic 	}
28465283Sbostic 	if (f_where) {
28565283Sbostic 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
28665287Sbostic 			if (**ap == '\0')
28765283Sbostic 				continue;
28865283Sbostic 			(void)printf("%s\n", *ap);
28965283Sbostic 		}
29065291Sbostic 		exit(cleanup());
29165283Sbostic 	}
29265283Sbostic 
29365283Sbostic 	/*
29465290Sbostic 	 * 8: We display things in a single command; build a list of things
29565283Sbostic 	 *    to display.
29665283Sbostic 	 */
29765283Sbostic 	for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
29865283Sbostic 		if (**ap == '\0')
29965283Sbostic 			continue;
30065283Sbostic 		len += strlen(*ap) + 1;
30165283Sbostic 	}
30265283Sbostic 	if ((cmd = malloc(len)) == NULL) {
30365291Sbostic 		warn(NULL);
30465291Sbostic 		(void)cleanup();
30565291Sbostic 		exit(1);
30665283Sbostic 	}
30765283Sbostic 	p = cmd;
30865283Sbostic 	len = strlen(pager);
30965283Sbostic 	memmove(p, pager, len);
31065283Sbostic 	p += len;
31165283Sbostic 	*p++ = ' ';
31265283Sbostic 	for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
31365283Sbostic 		if (**ap == '\0')
31465283Sbostic 			continue;
31565283Sbostic 		len = strlen(*ap);
31665283Sbostic 		memmove(p, *ap, len);
31765283Sbostic 		p += len;
31865283Sbostic 		*p++ = ' ';
31965283Sbostic 	}
32065283Sbostic 	*p = '\0';
32165283Sbostic 
32265283Sbostic 	/* Use system(3) in case someone's pager is "pager arg1 arg2". */
32365283Sbostic 	(void)system(cmd);
32465283Sbostic 
32565291Sbostic 	exit(cleanup());
32633354Sbostic }
32731703Sbostic 
32840390Sbostic /*
32931778Sbostic  * manual --
33065283Sbostic  *	Search the manuals for the pages.
33131778Sbostic  */
33265283Sbostic static int
33365291Sbostic manual(page, tag, pg)
33465283Sbostic 	char *page;
33565291Sbostic 	TAG *tag;
33665283Sbostic 	glob_t *pg;
33731703Sbostic {
33865291Sbostic 	ENTRY *ep, *e_sufp, *e_tag;
33965291Sbostic 	TAG *missp, *sufp;
34065283Sbostic 	int anyfound, cnt, found;
34165283Sbostic 	char *p, buf[128];
34231703Sbostic 
34365283Sbostic 	anyfound = 0;
34465283Sbostic 	buf[0] = '*';
34565283Sbostic 
34665283Sbostic 	/* For each element in the list... */
347*65292Sbostic 	e_tag = tag == NULL ? NULL : tag->list.tqh_first;
34865291Sbostic 	for (; e_tag != NULL; e_tag = e_tag->q.tqe_next) {
34965291Sbostic 		(void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
35065283Sbostic 		if (glob(buf,
35165286Sbostic 		    GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
35265286Sbostic 		    NULL, pg)) {
35365291Sbostic 			warn("globbing");
35465291Sbostic 			(void)cleanup();
35565291Sbostic 			exit(1);
35665283Sbostic 		}
35765283Sbostic 		if (pg->gl_matchc == 0)
35865283Sbostic 			continue;
35965283Sbostic 
36065283Sbostic 		/* Find out if it's really a man page. */
36165287Sbostic 		for (cnt = pg->gl_pathc - pg->gl_matchc;
36265287Sbostic 		    cnt < pg->gl_pathc; ++cnt) {
36365283Sbostic 
36465285Sbostic 			/*
36565285Sbostic 			 * Try the _suffix key words first.
36665285Sbostic 			 *
36765285Sbostic 			 * XXX
36865285Sbostic 			 * Older versions of man.conf didn't have the suffix
36965285Sbostic 			 * key words, it was assumed that everything was a .0.
37065285Sbostic 			 * We just test for .0 first, it's fast and probably
37165285Sbostic 			 * going to hit.
37265285Sbostic 			 */
37365287Sbostic 			(void)snprintf(buf, sizeof(buf), "*/%s.0", page);
37465287Sbostic 			if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
37565285Sbostic 				goto easy;
37665285Sbostic 
37765291Sbostic 			e_sufp = (sufp = getlist("_suffix")) == NULL ?
37865291Sbostic 			    NULL : sufp->list.tqh_first;
37965283Sbostic 			for (found = 0;
38065291Sbostic 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
38165283Sbostic 				(void)snprintf(buf,
38265291Sbostic 				     sizeof(buf), "*/%s%s", page, e_sufp->s);
38365287Sbostic 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
38465283Sbostic 					found = 1;
38565283Sbostic 					break;
38665283Sbostic 				}
38765283Sbostic 			}
38865283Sbostic 			if (found) {
38965285Sbostic easy:				anyfound = 1;
39065283Sbostic 				if (!f_all)
39165283Sbostic 					break;
39242402Sbostic 				continue;
39363515Sbostic 			}
39465283Sbostic 
39565283Sbostic 			/* Try the _build key words next. */
39665291Sbostic 			e_sufp = (sufp = getlist("_build")) == NULL ?
39765291Sbostic 			    NULL : sufp->list.tqh_first;
39865283Sbostic 			for (found = 0;
39965291Sbostic 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
40065291Sbostic 				for (p = e_sufp->s;
40165283Sbostic 				    *p != '\0' && !isspace(*p); ++p);
40265283Sbostic 				if (*p == '\0')
40365283Sbostic 					continue;
40465283Sbostic 				*p = '\0';
40565283Sbostic 				(void)snprintf(buf,
40665291Sbostic 				     sizeof(buf), "*/%s%s", page, e_sufp->s);
40765287Sbostic 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
40865289Sbostic 					if (!f_where)
40965283Sbostic 						build_page(p + 1,
41065289Sbostic 						    &pg->gl_pathv[cnt]);
41165283Sbostic 					*p = ' ';
41265283Sbostic 					found = 1;
41365283Sbostic 					break;
41465283Sbostic 				}
41565283Sbostic 				*p = ' ';
41665283Sbostic 			}
41765283Sbostic 			if (found) {
41865283Sbostic 				anyfound = 1;
41965283Sbostic 				if (!f_all)
42065283Sbostic 					break;
42142402Sbostic 				continue;
42263515Sbostic 			}
42365283Sbostic 
42465283Sbostic 			/* It's not a man page, forget about it. */
42565287Sbostic 			pg->gl_pathv[cnt] = "";
42642402Sbostic 		}
42742402Sbostic 
42865283Sbostic 		if (anyfound && !f_all)
42965283Sbostic 			break;
43031703Sbostic 	}
43165283Sbostic 
43265283Sbostic 	/* If not found, enter onto the missing list. */
43365283Sbostic 	if (!anyfound) {
43465283Sbostic 		if ((missp = getlist("_missing")) == NULL)
43565283Sbostic 			missp = addlist("_missing");
43665291Sbostic 		if ((ep = malloc(sizeof(ENTRY))) == NULL ||
43765291Sbostic 		    (ep->s = strdup(page)) == NULL) {
43865291Sbostic 			warn(NULL);
43965291Sbostic 			(void)cleanup();
44065291Sbostic 			exit(1);
44165283Sbostic 		}
44265291Sbostic 		TAILQ_INSERT_TAIL(&missp->list, ep, q);
44365283Sbostic 	}
44465283Sbostic 	return (anyfound);
44531703Sbostic }
44631703Sbostic 
44765283Sbostic /*
44865283Sbostic  * build_page --
44965283Sbostic  *	Build a man page for display.
45065283Sbostic  */
45165283Sbostic static void
45265289Sbostic build_page(fmt, pathp)
45365289Sbostic 	char *fmt, **pathp;
45465283Sbostic {
45565283Sbostic 	static int warned;
45665291Sbostic 	ENTRY *ep;
45765291Sbostic 	TAG *intmpp;
45865283Sbostic 	int fd;
45965283Sbostic 	char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[sizeof(_PATH_TMP)];
46065283Sbostic 
46165283Sbostic 	/* Let the user know this may take awhile. */
46265283Sbostic 	if (!warned) {
46365283Sbostic 		warned = 1;
46465283Sbostic 		warnx("Formatting manual page...");
46565283Sbostic 	}
46665283Sbostic 
46765289Sbostic 	/* Add a remove-when-done list. */
46865283Sbostic 	if ((intmpp = getlist("_intmp")) == NULL)
46965283Sbostic 		intmpp = addlist("_intmp");
47065283Sbostic 
47165283Sbostic 	/* Move to the printf(3) format string. */
47265283Sbostic 	for (; *fmt && isspace(*fmt); ++fmt);
47365283Sbostic 
47465283Sbostic 	/*
47565289Sbostic 	 * Get a temporary file and build a version of the file
47665289Sbostic 	 * to display.  Replace the old file name with the new one.
47765283Sbostic 	 */
47865283Sbostic 	(void)strcpy(tpath, _PATH_TMP);
47965283Sbostic 	if ((fd = mkstemp(tpath)) == -1) {
48065291Sbostic 		warn("%s", tpath);
48165291Sbostic 		(void)cleanup();
48265291Sbostic 		exit(1);
48365283Sbostic 	}
48465283Sbostic 	(void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
48565289Sbostic 	(void)snprintf(cmd, sizeof(cmd), buf, *pathp);
48665283Sbostic 	(void)system(cmd);
48765283Sbostic 	(void)close(fd);
48865289Sbostic 	if ((*pathp = strdup(tpath)) == NULL) {
48965291Sbostic 		warn(NULL);
49065291Sbostic 		(void)cleanup();
49165291Sbostic 		exit(1);
49265283Sbostic 	}
49365289Sbostic 
49465289Sbostic 	/* Link the built file into the remove-when-done list. */
49565291Sbostic 	if ((ep = malloc(sizeof(ENTRY))) == NULL) {
49665291Sbostic 		warn(NULL);
49765291Sbostic 		(void)cleanup();
49865291Sbostic 		exit(1);
49965289Sbostic 	}
50065291Sbostic 	ep->s = *pathp;
50165291Sbostic 	TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
50265283Sbostic }
50365283Sbostic 
50431778Sbostic /*
50544938Sbostic  * how --
50644938Sbostic  *	display how information
50744938Sbostic  */
50865283Sbostic static void
50944938Sbostic how(fname)
51044938Sbostic 	char *fname;
51144938Sbostic {
51265291Sbostic 	FILE *fp;
51344938Sbostic 
51465291Sbostic 	int lcnt, print;
51565291Sbostic 	char *p, buf[256];
51644938Sbostic 
51744938Sbostic 	if (!(fp = fopen(fname, "r"))) {
51865291Sbostic 		warn("%s", fname);
51965291Sbostic 		(void)cleanup();
52065291Sbostic 		exit (1);
52144938Sbostic 	}
52244938Sbostic #define	S1	"SYNOPSIS"
52344938Sbostic #define	S2	"S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
52444938Sbostic #define	D1	"DESCRIPTION"
52544938Sbostic #define	D2	"D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
52644938Sbostic 	for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
52744938Sbostic 		if (!strncmp(buf, S1, sizeof(S1) - 1) ||
52844938Sbostic 		    !strncmp(buf, S2, sizeof(S2) - 1)) {
52944938Sbostic 			print = 1;
53044938Sbostic 			continue;
53144938Sbostic 		} else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
53244938Sbostic 		    !strncmp(buf, D2, sizeof(D2) - 1))
53344938Sbostic 			return;
53444938Sbostic 		if (!print)
53544938Sbostic 			continue;
53644938Sbostic 		if (*buf == '\n')
53744938Sbostic 			++lcnt;
53844938Sbostic 		else {
53944938Sbostic 			for(; lcnt; --lcnt)
54044938Sbostic 				(void)putchar('\n');
54144938Sbostic 			for (p = buf; isspace(*p); ++p);
54244938Sbostic 			(void)fputs(p, stdout);
54344938Sbostic 		}
54444938Sbostic 	}
54544938Sbostic 	(void)fclose(fp);
54644938Sbostic }
54765283Sbostic 
54844938Sbostic /*
54933809Sbostic  * cat --
55033809Sbostic  *	cat out the file
55131778Sbostic  */
55265283Sbostic static void
55333809Sbostic cat(fname)
55433809Sbostic 	char *fname;
55531703Sbostic {
55665291Sbostic 	int fd, n;
55765291Sbostic 	char buf[2048];
55831703Sbostic 
55944938Sbostic 	if ((fd = open(fname, O_RDONLY, 0)) < 0) {
56065291Sbostic 		warn("%s", fname);
56165291Sbostic 		(void)cleanup();
56265291Sbostic 		exit(1);
56331703Sbostic 	}
56433809Sbostic 	while ((n = read(fd, buf, sizeof(buf))) > 0)
56565283Sbostic 		if (write(STDOUT_FILENO, buf, n) != n) {
56665291Sbostic 			warn("write");
56765291Sbostic 			(void)cleanup();
56865291Sbostic 			exit (1);
56933809Sbostic 		}
57033809Sbostic 	if (n == -1) {
57165291Sbostic 		warn("read");
57265291Sbostic 		(void)cleanup();
57365291Sbostic 		exit(1);
57433809Sbostic 	}
57533809Sbostic 	(void)close(fd);
57631703Sbostic }
57731703Sbostic 
57831778Sbostic /*
57940390Sbostic  * check_pager --
58040390Sbostic  *	check the user supplied page information
58140390Sbostic  */
58265283Sbostic static char *
58340390Sbostic check_pager(name)
58440390Sbostic 	char *name;
58540390Sbostic {
58665291Sbostic 	char *p, *save;
58740390Sbostic 
58840390Sbostic 	/*
58940390Sbostic 	 * if the user uses "more", we make it "more -s"; watch out for
59040390Sbostic 	 * PAGER = "mypager /usr/ucb/more"
59140390Sbostic 	 */
59240390Sbostic 	for (p = name; *p && !isspace(*p); ++p);
59340390Sbostic 	for (; p > name && *p != '/'; --p);
59440390Sbostic 	if (p != name)
59540390Sbostic 		++p;
59640390Sbostic 
59740390Sbostic 	/* make sure it's "more", not "morex" */
59840390Sbostic 	if (!strncmp(p, "more", 4) && (!p[4] || isspace(p[4]))){
59940390Sbostic 		save = name;
60040390Sbostic 		/* allocate space to add the "-s" */
60140390Sbostic 		if (!(name =
60240390Sbostic 		    malloc((u_int)(strlen(save) + sizeof("-s") + 1))))
60365283Sbostic 			err(1, NULL);
60440390Sbostic 		(void)sprintf(name, "%s %s", save, "-s");
60540390Sbostic 	}
60640390Sbostic 	return(name);
60740390Sbostic }
60840390Sbostic 
60940390Sbostic /*
61034057Sbostic  * jump --
61134057Sbostic  *	strip out flag argument and jump
61234057Sbostic  */
61365283Sbostic static void
61434057Sbostic jump(argv, flag, name)
61565283Sbostic 	char **argv, *flag, *name;
61634057Sbostic {
61765283Sbostic 	char **arg;
61834057Sbostic 
61934057Sbostic 	argv[0] = name;
62034057Sbostic 	for (arg = argv + 1; *arg; ++arg)
62134057Sbostic 		if (!strcmp(*arg, flag))
62234057Sbostic 			break;
62334057Sbostic 	for (; *arg; ++arg)
62434057Sbostic 		arg[0] = arg[1];
62534057Sbostic 	execvp(name, argv);
62642402Sbostic 	(void)fprintf(stderr, "%s: Command not found.\n", name);
62734057Sbostic 	exit(1);
62834057Sbostic }
62934057Sbostic 
63065283Sbostic /*
63165283Sbostic  * onsig --
63265283Sbostic  *	If signaled, delete the temporary files.
63365283Sbostic  */
63465283Sbostic static void
63565283Sbostic onsig(signo)
63665283Sbostic 	int signo;
63765283Sbostic {
63865291Sbostic 	(void)cleanup();
63965283Sbostic 
64065291Sbostic 	(void)signal(signo, SIG_DFL);
64165291Sbostic 	(void)kill(getpid(), signo);
64265291Sbostic 
64365291Sbostic 	/* NOTREACHED */
64465291Sbostic 	exit (1);
64565283Sbostic }
64665283Sbostic 
64734057Sbostic /*
64865283Sbostic  * cleanup --
64965283Sbostic  *	Clean up temporary files, show any error messages.
65065283Sbostic  */
65165291Sbostic static int
65265283Sbostic cleanup()
65365283Sbostic {
65465291Sbostic 	TAG *intmpp, *missp;
65565291Sbostic 	ENTRY *ep;
65665291Sbostic 	int rval;
65765283Sbostic 
65865291Sbostic 	rval = 0;
65965291Sbostic 	ep = (missp = getlist("_missing")) == NULL ?
66065291Sbostic 	    NULL : missp->list.tqh_first;
66165291Sbostic 	if (ep != NULL)
66265291Sbostic 		for (; ep != NULL; ep = ep->q.tqe_next) {
66365291Sbostic 			warnx("no entry for %s in the manual.", ep->s);
66465291Sbostic 			rval = 1;
66565291Sbostic 		}
66665283Sbostic 
66765291Sbostic 	ep = (intmpp = getlist("_intmp")) == NULL ?
66865291Sbostic 	    NULL : intmpp->list.tqh_first;
66965291Sbostic 	for (; ep != NULL; ep = ep->q.tqe_next)
67065291Sbostic 		(void)unlink(ep->s);
67165291Sbostic 	return (rval);
67265283Sbostic }
67365283Sbostic 
67465283Sbostic /*
67534057Sbostic  * usage --
67640390Sbostic  *	print usage message and die
67734057Sbostic  */
67865283Sbostic static void
67934057Sbostic usage()
68034057Sbostic {
68140390Sbostic 	(void)fprintf(stderr,
68265284Sbostic     "usage: man [-ac] [-C file] [-M path] [-m path] [section] title ...\n");
68334057Sbostic 	exit(1);
68434057Sbostic }
685