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