xref: /netbsd-src/usr.bin/man/man.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: man.c,v 1.26 2001/02/19 23:03:49 cgd 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.26 2001/02/19 23:03:49 cgd 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];
401 
402 	anyfound = 0;
403 	buf[0] = '*';
404 
405 	/* For each element in the list... */
406 	e_tag = tag == NULL ? NULL : tag->list.tqh_first;
407 	for (; e_tag != NULL; e_tag = e_tag->q.tqe_next) {
408 		(void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
409 		if ((error = glob(buf,
410 		    GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) {
411 			if (error == GLOB_NOMATCH)
412 				continue;
413 			else {
414 				warn("globbing");
415 				(void)cleanup();
416 				exit(1);
417 			}
418 		}
419 		if (pg->gl_matchc == 0)
420 			continue;
421 
422 		/* Find out if it's really a man page. */
423 		for (cnt = pg->gl_pathc - pg->gl_matchc;
424 		    cnt < pg->gl_pathc; ++cnt) {
425 
426 			if (pathsearch) {
427 				p = strstr(pg->gl_pathv[cnt], pathsearch);
428 				if (!p || strchr(p, '/') == NULL) {
429 					pg->gl_pathv[cnt] = "";
430 					continue;
431 				}
432 			}
433 
434 			/*
435 			 * Try the _suffix key words first.
436 			 *
437 			 * XXX
438 			 * Older versions of man.conf didn't have the suffix
439 			 * key words, it was assumed that everything was a .0.
440 			 * We just test for .0 first, it's fast and probably
441 			 * going to hit.
442 			 */
443 			(void)snprintf(buf, sizeof(buf), "*/%s.0", page);
444 			if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
445 				goto next;
446 
447 			e_sufp = (sufp = getlist("_suffix")) == NULL ?
448 			    NULL : sufp->list.tqh_first;
449 			for (found = 0;
450 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
451 				(void)snprintf(buf,
452 				     sizeof(buf), "*/%s%s", page, e_sufp->s);
453 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
454 					found = 1;
455 					break;
456 				}
457 			}
458 			if (found)
459 				goto next;
460 
461 			/* Try the _build key words next. */
462 			e_sufp = (sufp = getlist("_build")) == NULL ?
463 			    NULL : sufp->list.tqh_first;
464 			for (found = 0;
465 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
466 				for (p = e_sufp->s;
467 				    *p != '\0' && !isspace((unsigned char)*p); ++p);
468 				if (*p == '\0')
469 					continue;
470 				*p = '\0';
471 				(void)snprintf(buf,
472 				     sizeof(buf), "*/%s%s", page, e_sufp->s);
473 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
474 					if (!f_where)
475 						build_page(p + 1,
476 						    &pg->gl_pathv[cnt]);
477 					*p = ' ';
478 					found = 1;
479 					break;
480 				}
481 				*p = ' ';
482 			}
483 			if (found) {
484 next:				anyfound = 1;
485 				if (!f_all) {
486 					/* Delete any other matches. */
487 					while (++cnt< pg->gl_pathc)
488 						pg->gl_pathv[cnt] = "";
489 					break;
490 				}
491 				continue;
492 			}
493 
494 			/* It's not a man page, forget about it. */
495 			pg->gl_pathv[cnt] = "";
496 		}
497 
498 		if (anyfound && !f_all)
499 			break;
500 	}
501 
502 	/* If not found, enter onto the missing list. */
503 	if (!anyfound) {
504 		if ((missp = getlist("_missing")) == NULL)
505 			missp = addlist("_missing");
506 		if ((ep = malloc(sizeof(ENTRY))) == NULL ||
507 		    (ep->s = strdup(page)) == NULL) {
508 			warn("malloc");
509 			(void)cleanup();
510 			exit(1);
511 		}
512 		TAILQ_INSERT_TAIL(&missp->list, ep, q);
513 	}
514 	return (anyfound);
515 }
516 
517 /*
518  * build_page --
519  *	Build a man page for display.
520  */
521 static void
522 build_page(fmt, pathp)
523 	char *fmt, **pathp;
524 {
525 	static int warned;
526 	ENTRY *ep;
527 	TAG *intmpp;
528 	int fd, n;
529 	char *p, *b;
530 	char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
531 	const char *tmpdir;
532 
533 	/* Let the user know this may take awhile. */
534 	if (!warned) {
535 		warned = 1;
536 		warnx("Formatting manual page...");
537 	}
538 
539        /*
540         * Historically man chdir'd to the root of the man tree.
541         * This was used in man pages that contained relative ".so"
542         * directives (including other man pages for command aliases etc.)
543         * It even went one step farther, by examining the first line
544         * of the man page and parsing the .so filename so it would
545         * make hard(?) links to the cat'ted man pages for space savings.
546         * (We don't do that here, but we could).
547         */
548 
549        /* copy and find the end */
550        for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
551                continue;
552 
553 	/* skip the last two path components, page name and man[n] */
554 	for (--b, --p, n = 2; b != buf; b--, p--)
555 		if (*b == '/')
556 			if (--n == 0) {
557 				*b = '\0';
558 				(void) chdir(buf);
559 				p++;
560 				break;
561 			}
562 
563 
564 	/* Add a remove-when-done list. */
565 	if ((intmpp = getlist("_intmp")) == NULL)
566 		intmpp = addlist("_intmp");
567 
568 	/* Move to the printf(3) format string. */
569 	for (; *fmt && isspace((unsigned char)*fmt); ++fmt)
570 		continue;
571 
572 	/*
573 	 * Get a temporary file and build a version of the file
574 	 * to display.  Replace the old file name with the new one.
575 	 */
576 	if ((tmpdir = getenv("TMPDIR")) == NULL)
577 		tmpdir = _PATH_TMP;
578 	(void)snprintf(tpath, sizeof (tpath), "%s/%s", tmpdir, TMPFILE);
579 	if ((fd = mkstemp(tpath)) == -1) {
580 		warn("%s", tpath);
581 		(void)cleanup();
582 		exit(1);
583 	}
584 	(void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
585 	(void)snprintf(cmd, sizeof(cmd), buf, p);
586 	(void)system(cmd);
587 	(void)close(fd);
588 	if ((*pathp = strdup(tpath)) == NULL) {
589 		warn("malloc");
590 		(void)cleanup();
591 		exit(1);
592 	}
593 
594 	/* Link the built file into the remove-when-done list. */
595 	if ((ep = malloc(sizeof(ENTRY))) == NULL) {
596 		warn("malloc");
597 		(void)cleanup();
598 		exit(1);
599 	}
600 	ep->s = *pathp;
601 	TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
602 }
603 
604 /*
605  * how --
606  *	display how information
607  */
608 static void
609 how(fname)
610 	char *fname;
611 {
612 	FILE *fp;
613 
614 	int lcnt, print;
615 	char *p, buf[256];
616 
617 	if (!(fp = fopen(fname, "r"))) {
618 		warn("%s", fname);
619 		(void)cleanup();
620 		exit (1);
621 	}
622 #define	S1	"SYNOPSIS"
623 #define	S2	"S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
624 #define	D1	"DESCRIPTION"
625 #define	D2	"D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
626 	for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
627 		if (!strncmp(buf, S1, sizeof(S1) - 1) ||
628 		    !strncmp(buf, S2, sizeof(S2) - 1)) {
629 			print = 1;
630 			continue;
631 		} else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
632 		    !strncmp(buf, D2, sizeof(D2) - 1))
633 			return;
634 		if (!print)
635 			continue;
636 		if (*buf == '\n')
637 			++lcnt;
638 		else {
639 			for(; lcnt; --lcnt)
640 				(void)putchar('\n');
641 			for (p = buf; isspace((unsigned char)*p); ++p)
642 				continue;
643 			(void)fputs(p, stdout);
644 		}
645 	}
646 	(void)fclose(fp);
647 }
648 
649 /*
650  * cat --
651  *	cat out the file
652  */
653 static void
654 cat(fname)
655 	char *fname;
656 {
657 	int fd, n;
658 	char buf[2048];
659 
660 	if ((fd = open(fname, O_RDONLY, 0)) < 0) {
661 		warn("%s", fname);
662 		(void)cleanup();
663 		exit(1);
664 	}
665 	while ((n = read(fd, buf, sizeof(buf))) > 0)
666 		if (write(STDOUT_FILENO, buf, n) != n) {
667 			warn("write");
668 			(void)cleanup();
669 			exit (1);
670 		}
671 	if (n == -1) {
672 		warn("read");
673 		(void)cleanup();
674 		exit(1);
675 	}
676 	(void)close(fd);
677 }
678 
679 /*
680  * check_pager --
681  *	check the user supplied page information
682  */
683 static const char *
684 check_pager(name)
685 	const char *name;
686 {
687 	const char *p;
688 
689 	/*
690 	 * if the user uses "more", we make it "more -s"; watch out for
691 	 * PAGER = "mypager /usr/ucb/more"
692 	 */
693 	for (p = name; *p && !isspace((unsigned char)*p); ++p)
694 		continue;
695 	for (; p > name && *p != '/'; --p);
696 	if (p != name)
697 		++p;
698 
699 	/* make sure it's "more", not "morex" */
700 	if (!strncmp(p, "more", 4) && (!p[4] || isspace((unsigned char)p[4]))){
701 		char *newname;
702 		(void)asprintf(&newname, "%s %s", p, "-s");
703 		name = newname;
704 	}
705 
706 	return (name);
707 }
708 
709 /*
710  * jump --
711  *	strip out flag argument and jump
712  */
713 static void
714 jump(argv, flag, name)
715 	char **argv, *flag, *name;
716 {
717 	char **arg;
718 
719 	argv[0] = name;
720 	for (arg = argv + 1; *arg; ++arg)
721 		if (!strcmp(*arg, flag))
722 			break;
723 	for (; *arg; ++arg)
724 		arg[0] = arg[1];
725 	execvp(name, argv);
726 	(void)fprintf(stderr, "%s: Command not found.\n", name);
727 	exit(1);
728 }
729 
730 /*
731  * onsig --
732  *	If signaled, delete the temporary files.
733  */
734 static void
735 onsig(signo)
736 	int signo;
737 {
738 	sigset_t set;
739 
740 	(void)cleanup();
741 
742 	(void)signal(signo, SIG_DFL);
743 
744 	/* unblock the signal */
745 	sigemptyset(&set);
746 	sigaddset(&set, signo);
747 	sigprocmask(SIG_UNBLOCK, &set, (sigset_t *) NULL);
748 
749 	(void)kill(getpid(), signo);
750 
751 	/* NOTREACHED */
752 	exit (1);
753 }
754 
755 /*
756  * cleanup --
757  *	Clean up temporary files, show any error messages.
758  */
759 static int
760 cleanup()
761 {
762 	TAG *intmpp, *missp;
763 	ENTRY *ep;
764 	int rval;
765 
766 	rval = 0;
767 	ep = (missp = getlist("_missing")) == NULL ?
768 	    NULL : missp->list.tqh_first;
769 	if (ep != NULL)
770 		for (; ep != NULL; ep = ep->q.tqe_next) {
771 			warnx("no entry for %s in the manual.", ep->s);
772 			rval = 1;
773 		}
774 
775 	ep = (intmpp = getlist("_intmp")) == NULL ?
776 	    NULL : intmpp->list.tqh_first;
777 	for (; ep != NULL; ep = ep->q.tqe_next)
778 		(void)unlink(ep->s);
779 	return (rval);
780 }
781 
782 /*
783  * usage --
784  *	print usage message and die
785  */
786 static void
787 usage()
788 {
789 
790 	(void)fprintf(stderr, "Usage: %s [-achw] [-C file] [-M path] [-m path]"
791 	    "[-S srch] [[-s] section] title ...\n", getprogname());
792 	exit(1);
793 }
794