Annotation of src/usr.bin/man/man.c, Revision 1.10
1.10 ! deraadt 1: /* $OpenBSD: man.c,v 1.9 1998/07/01 11:23:40 espie Exp $ */
1.1 deraadt 2: /* $NetBSD: man.c,v 1.7 1995/09/28 06:05:34 tls Exp $ */
3:
4: /*
5: * Copyright (c) 1987, 1993, 1994, 1995
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) 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
1.10 ! deraadt 47: static char rcsid[] = "$OpenBSD: man.c,v 1.9 1998/07/01 11:23:40 espie Exp $";
1.1 deraadt 48: #endif
49: #endif /* not lint */
50:
51: #include <sys/param.h>
52: #include <sys/queue.h>
53:
54: #include <ctype.h>
55: #include <err.h>
56: #include <errno.h>
57: #include <fcntl.h>
58: #include <fnmatch.h>
59: #include <glob.h>
60: #include <signal.h>
61: #include <stdio.h>
62: #include <stdlib.h>
63: #include <string.h>
64: #include <unistd.h>
65:
66: #include "config.h"
67: #include "pathnames.h"
68:
69: int f_all, f_where;
1.9 espie 70: static TAG *section; /* could be passed to cleanup() instead */
1.1 deraadt 71:
1.8 millert 72: extern char *__progname;
73:
1.1 deraadt 74: static void build_page __P((char *, char **));
75: static void cat __P((char *));
76: static char *check_pager __P((char *));
77: static int cleanup __P((void));
78: static void how __P((char *));
79: static void jump __P((char **, char *, char *));
80: static int manual __P((char *, TAG *, glob_t *));
81: static void onsig __P((int));
82: static void usage __P((void));
83:
84: int
85: main(argc, argv)
86: int argc;
87: char *argv[];
88: {
89: extern char *optarg;
90: extern int optind;
1.9 espie 91: TAG *defp, *defnewp, *sectnewp, *subp;
1.1 deraadt 92: ENTRY *e_defp, *e_sectp, *e_subp, *ep;
93: glob_t pg;
94: size_t len;
95: int ch, f_cat, f_how, found;
1.7 millert 96: char **ap, *cmd, *machine, *p, *p_add, *p_path, *pager, *sflag, *slashp;
1.1 deraadt 97: char *conffile, buf[MAXPATHLEN * 2];
98:
1.7 millert 99: machine = sflag = NULL;
1.1 deraadt 100: f_cat = f_how = 0;
101: conffile = p_add = p_path = NULL;
1.7 millert 102: while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:s:S:w")) != -1)
1.1 deraadt 103: switch (ch) {
104: case 'a':
105: f_all = 1;
106: break;
107: case 'C':
108: conffile = optarg;
109: break;
110: case 'c':
111: case '-': /* Deprecated. */
112: f_cat = 1;
113: break;
114: case 'h':
115: f_how = 1;
116: break;
117: case 'm':
118: p_add = optarg;
119: break;
120: case 'M':
121: case 'P': /* Backward compatibility. */
122: p_path = optarg;
123: break;
1.7 millert 124: case 's': /* SVR4 compatibility. */
125: sflag = optarg;
126: break;
127: case 'S':
128: machine = optarg;
129: break;
1.1 deraadt 130: /*
131: * The -f and -k options are backward compatible,
132: * undocumented ways of calling whatis(1) and apropos(1).
133: */
134: case 'f':
135: jump(argv, "-f", "whatis");
136: /* NOTREACHED */
137: case 'k':
138: jump(argv, "-k", "apropos");
139: /* NOTREACHED */
140: case 'w':
141: f_all = f_where = 1;
142: break;
143: case '?':
144: default:
145: usage();
146: }
147: argc -= optind;
148: argv += optind;
149:
150: if (!*argv)
151: usage();
152:
153: if (!f_cat && !f_how && !f_where)
154: if (!isatty(1))
155: f_cat = 1;
156: else if ((pager = getenv("PAGER")) != NULL)
157: pager = check_pager(pager);
158: else
159: pager = _PATH_PAGER;
160:
161: /* Read the configuration file. */
162: config(conffile);
163:
1.7 millert 164: /* Get the machine type unless specified by -S. */
165: if (machine == NULL && (machine = getenv("MACHINE")) == NULL)
1.1 deraadt 166: machine = MACHINE;
167:
168: /* If there's no _default list, create an empty one. */
169: if ((defp = getlist("_default")) == NULL)
170: defp = addlist("_default");
171:
172: /*
173: * 1: If the user specified a MANPATH variable, or set the -M
174: * option, we replace the _default list with the user's list,
175: * appending the entries in the _subdir list and the machine.
176: */
177: if (p_path == NULL)
178: p_path = getenv("MANPATH");
179: if (p_path != NULL) {
180: while ((e_defp = defp->list.tqh_first) != NULL) {
181: free(e_defp->s);
182: TAILQ_REMOVE(&defp->list, e_defp, q);
183: }
184: for (p = strtok(p_path, ":");
185: p != NULL; p = strtok(NULL, ":")) {
186: slashp = p[strlen(p) - 1] == '/' ? "" : "/";
187: e_subp = (subp = getlist("_subdir")) == NULL ?
188: NULL : subp->list.tqh_first;
189: for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
190: (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
191: p, slashp, e_subp->s, machine);
192: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
193: (ep->s = strdup(buf)) == NULL)
194: err(1, NULL);
195: TAILQ_INSERT_TAIL(&defp->list, ep, q);
196: }
197: }
198: }
199:
200: /*
201: * 2: If the user did not specify MANPATH, -M or a section, rewrite
202: * the _default list to include the _subdir list and the machine.
203: */
1.7 millert 204: if (sflag == NULL && argv[1] == NULL)
1.1 deraadt 205: section = NULL;
1.7 millert 206: else {
207: if (sflag != NULL && (section = getlist(sflag)) == NULL)
208: errx(1, "unknown manual section `%s'", sflag);
209: else if (sflag == NULL && (section = getlist(*argv)) != NULL)
210: ++argv;
211: }
1.1 deraadt 212: if (p_path == NULL && section == NULL) {
213: defnewp = addlist("_default_new");
214: e_defp =
215: defp->list.tqh_first == NULL ? NULL : defp->list.tqh_first;
216: for (; e_defp != NULL; e_defp = e_defp->q.tqe_next) {
217: slashp =
218: e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
219: e_subp = (subp = getlist("_subdir")) == NULL ?
220: NULL : subp->list.tqh_first;
221: for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
222: (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
223: e_defp->s, slashp, e_subp->s, machine);
224: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
225: (ep->s = strdup(buf)) == NULL)
226: err(1, NULL);
227: TAILQ_INSERT_TAIL(&defnewp->list, ep, q);
228: }
229: }
230: defp = getlist("_default");
231: while ((e_defp = defp->list.tqh_first) != NULL) {
232: free(e_defp->s);
233: TAILQ_REMOVE(&defp->list, e_defp, q);
234: }
235: free(defp->s);
236: TAILQ_REMOVE(&head, defp, q);
237: defnewp = getlist("_default_new");
238: free(defnewp->s);
239: defnewp->s = "_default";
240: defp = defnewp;
241: }
242:
243: /*
244: * 3: If the user set the -m option, insert the user's list before
245: * whatever list we have, again appending the _subdir list and
1.3 michaels 246: * the machine.
1.1 deraadt 247: */
1.3 michaels 248: if (p_add != NULL) {
249: e_sectp = NULL;
1.1 deraadt 250: for (p = strtok(p_add, ":"); p != NULL; p = strtok(NULL, ":")) {
251: slashp = p[strlen(p) - 1] == '/' ? "" : "/";
252: e_subp = (subp = getlist("_subdir")) == NULL ?
253: NULL : subp->list.tqh_first;
254: for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
255: (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
256: p, slashp, e_subp->s, machine);
257: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
258: (ep->s = strdup(buf)) == NULL)
259: err(1, NULL);
1.3 michaels 260: /* puts it at the end, should be at the top, but then the added
261: entries would be in reverse order, fix later when all are added*/
262: TAILQ_INSERT_TAIL(&defp->list, ep, q);
263: if (e_sectp == NULL) /* save first added, to-be the new top */
264: e_sectp = ep;
1.1 deraadt 265: }
266: }
1.3 michaels 267: if (e_sectp != NULL) { /* entries added, fix order */
268: ep->q.tqe_next = defp->list.tqh_first; /* save original head */
269: defp->list.tqh_first = e_sectp; /* first added entry, new top */
270: *e_sectp->q.tqe_prev = NULL; /* terminate list */
271: }
272: }
1.1 deraadt 273: /*
274: * 4: If no -m was specified, and a section was, rewrite the section's
275: * paths (if they have a trailing slash) to append the _subdir list
276: * and the machine. This then becomes the _default list.
277: */
278: if (p_add == NULL && section != NULL) {
279: sectnewp = addlist("_section_new");
280: for (e_sectp = section->list.tqh_first;
281: e_sectp != NULL; e_sectp = e_sectp->q.tqe_next) {
282: if (e_sectp->s[strlen(e_sectp->s) - 1] != '/') {
283: (void)snprintf(buf, sizeof(buf),
284: "%s{/%s,}", e_sectp->s, machine);
285: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
286: (ep->s = strdup(buf)) == NULL)
287: err(1, NULL);
288: TAILQ_INSERT_TAIL(§newp->list, ep, q);
289: continue;
290: }
291: e_subp = (subp = getlist("_subdir")) == NULL ?
292: NULL : subp->list.tqh_first;
293: for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
294: (void)snprintf(buf, sizeof(buf), "%s%s{/%s,}",
295: e_sectp->s, e_subp->s, machine);
296: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
297: (ep->s = strdup(buf)) == NULL)
298: err(1, NULL);
299: TAILQ_INSERT_TAIL(§newp->list, ep, q);
300: }
301: }
302: sectnewp->s = section->s;
303: defp = sectnewp;
304: TAILQ_REMOVE(&head, section, q);
305: }
306:
307: /*
308: * 5: Search for the files. Set up an interrupt handler, so the
309: * temporary files go away.
310: */
311: (void)signal(SIGINT, onsig);
312: (void)signal(SIGHUP, onsig);
313:
314: memset(&pg, 0, sizeof(pg));
315: for (found = 0; *argv; ++argv)
316: if (manual(*argv, defp, &pg))
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(NULL);
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)
392: char *page;
393: TAG *tag;
394: glob_t *pg;
395: {
396: ENTRY *ep, *e_sufp, *e_tag;
397: TAG *missp, *sufp;
398: int anyfound, cnt, found;
1.6 deraadt 399: char *p, buf[MAXPATHLEN];
1.1 deraadt 400:
401: anyfound = 0;
402: buf[0] = '*';
403:
404: /* For each element in the list... */
405: e_tag = tag == NULL ? NULL : tag->list.tqh_first;
406: for (; e_tag != NULL; e_tag = e_tag->q.tqe_next) {
407: (void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
408: if (glob(buf,
409: GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
410: NULL, pg)) {
411: warn("globbing");
412: (void)cleanup();
413: exit(1);
414: }
415: if (pg->gl_matchc == 0)
416: continue;
417:
418: /* Find out if it's really a man page. */
419: for (cnt = pg->gl_pathc - pg->gl_matchc;
420: cnt < pg->gl_pathc; ++cnt) {
421:
422: /*
423: * Try the _suffix key words first.
424: *
425: * XXX
426: * Older versions of man.conf didn't have the suffix
427: * key words, it was assumed that everything was a .0.
428: * We just test for .0 first, it's fast and probably
429: * going to hit.
430: */
431: (void)snprintf(buf, sizeof(buf), "*/%s.0", page);
432: if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
433: goto next;
434:
435: e_sufp = (sufp = getlist("_suffix")) == NULL ?
436: NULL : sufp->list.tqh_first;
437: for (found = 0;
438: e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
439: (void)snprintf(buf,
440: sizeof(buf), "*/%s%s", page, e_sufp->s);
441: if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
442: found = 1;
443: break;
444: }
445: }
446: if (found)
447: goto next;
448:
449: /* Try the _build key words next. */
450: e_sufp = (sufp = getlist("_build")) == NULL ?
451: NULL : sufp->list.tqh_first;
452: for (found = 0;
453: e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
454: for (p = e_sufp->s;
455: *p != '\0' && !isspace(*p); ++p);
456: if (*p == '\0')
457: continue;
458: *p = '\0';
459: (void)snprintf(buf,
460: sizeof(buf), "*/%s%s", page, e_sufp->s);
461: if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
462: if (!f_where)
463: build_page(p + 1,
464: &pg->gl_pathv[cnt]);
465: *p = ' ';
466: found = 1;
467: break;
468: }
469: *p = ' ';
470: }
471: if (found) {
472: next: anyfound = 1;
473: if (!f_all) {
474: /* Delete any other matches. */
475: while (++cnt< pg->gl_pathc)
476: pg->gl_pathv[cnt] = "";
477: break;
478: }
479: continue;
480: }
481:
482: /* It's not a man page, forget about it. */
483: pg->gl_pathv[cnt] = "";
484: }
485:
486: if (anyfound && !f_all)
487: break;
488: }
489:
490: /* If not found, enter onto the missing list. */
491: if (!anyfound) {
492: if ((missp = getlist("_missing")) == NULL)
493: missp = addlist("_missing");
494: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
495: (ep->s = strdup(page)) == NULL) {
496: warn(NULL);
497: (void)cleanup();
498: exit(1);
499: }
500: TAILQ_INSERT_TAIL(&missp->list, ep, q);
501: }
502: return (anyfound);
503: }
504:
505: /*
506: * build_page --
507: * Build a man page for display.
508: */
509: static void
510: build_page(fmt, pathp)
511: char *fmt, **pathp;
512: {
513: static int warned;
514: ENTRY *ep;
515: TAG *intmpp;
516: int fd, n;
517: char *p, *b;
1.4 deraadt 518: char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
1.1 deraadt 519:
520: /* Let the user know this may take awhile. */
521: if (!warned) {
522: warned = 1;
523: warnx("Formatting manual page...");
524: }
525:
526: /*
527: * Historically man chdir'd to the root of the man tree.
528: * This was used in man pages that contained relative ".so"
529: * directives (including other man pages for command aliases etc.)
530: * It even went one step farther, by examining the first line
531: * of the man page and parsing the .so filename so it would
532: * make hard(?) links to the cat'ted man pages for space savings.
533: * (We don't do that here, but we could).
534: */
535:
536: /* copy and find the end */
537: for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
538: continue;
539:
540: /* skip the last two path components, page name and man[n] */
541: for (--b, n = 2; b != buf; b--)
542: if (*b == '/')
543: if (--n == 0) {
544: *b = '\0';
545: (void) chdir(buf);
546: }
547:
548:
549: /* Add a remove-when-done list. */
550: if ((intmpp = getlist("_intmp")) == NULL)
551: intmpp = addlist("_intmp");
552:
553: /* Move to the printf(3) format string. */
554: for (; *fmt && isspace(*fmt); ++fmt);
555:
556: /*
557: * Get a temporary file and build a version of the file
558: * to display. Replace the old file name with the new one.
559: */
1.4 deraadt 560: (void)strcpy(tpath, _PATH_TMPFILE);
1.1 deraadt 561: if ((fd = mkstemp(tpath)) == -1) {
562: warn("%s", tpath);
563: (void)cleanup();
564: exit(1);
565: }
566: (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
567: (void)snprintf(cmd, sizeof(cmd), buf, *pathp);
568: (void)system(cmd);
569: (void)close(fd);
570: if ((*pathp = strdup(tpath)) == NULL) {
571: warn(NULL);
572: (void)cleanup();
573: exit(1);
574: }
575:
576: /* Link the built file into the remove-when-done list. */
577: if ((ep = malloc(sizeof(ENTRY))) == NULL) {
578: warn(NULL);
579: (void)cleanup();
580: exit(1);
581: }
582: ep->s = *pathp;
583: TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
584: }
585:
586: /*
587: * how --
588: * display how information
589: */
590: static void
591: how(fname)
592: char *fname;
593: {
594: FILE *fp;
595:
596: int lcnt, print;
597: char *p, buf[256];
598:
599: if (!(fp = fopen(fname, "r"))) {
600: warn("%s", fname);
601: (void)cleanup();
602: exit (1);
603: }
604: #define S1 "SYNOPSIS"
605: #define S2 "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
606: #define D1 "DESCRIPTION"
607: #define D2 "D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
608: for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
609: if (!strncmp(buf, S1, sizeof(S1) - 1) ||
610: !strncmp(buf, S2, sizeof(S2) - 1)) {
611: print = 1;
612: continue;
613: } else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
614: !strncmp(buf, D2, sizeof(D2) - 1))
615: return;
616: if (!print)
617: continue;
618: if (*buf == '\n')
619: ++lcnt;
620: else {
621: for(; lcnt; --lcnt)
622: (void)putchar('\n');
623: for (p = buf; isspace(*p); ++p);
624: (void)fputs(p, stdout);
625: }
626: }
627: (void)fclose(fp);
628: }
629:
630: /*
631: * cat --
632: * cat out the file
633: */
634: static void
635: cat(fname)
636: char *fname;
637: {
638: int fd, n;
639: char buf[2048];
640:
641: if ((fd = open(fname, O_RDONLY, 0)) < 0) {
642: warn("%s", fname);
643: (void)cleanup();
644: exit(1);
645: }
646: while ((n = read(fd, buf, sizeof(buf))) > 0)
647: if (write(STDOUT_FILENO, buf, n) != n) {
648: warn("write");
649: (void)cleanup();
650: exit (1);
651: }
652: if (n == -1) {
653: warn("read");
654: (void)cleanup();
655: exit(1);
656: }
657: (void)close(fd);
658: }
659:
660: /*
661: * check_pager --
662: * check the user supplied page information
663: */
664: static char *
665: check_pager(name)
666: char *name;
667: {
668: char *p, *save;
669:
670: /*
671: * if the user uses "more", we make it "more -s"; watch out for
672: * PAGER = "mypager /usr/ucb/more"
673: */
674: for (p = name; *p && !isspace(*p); ++p);
675: for (; p > name && *p != '/'; --p);
676: if (p != name)
677: ++p;
678:
679: /* make sure it's "more", not "morex" */
680: if (!strncmp(p, "more", 4) && (!p[4] || isspace(p[4]))){
681: save = name;
682: /* allocate space to add the "-s" */
683: if (!(name =
684: malloc((u_int)(strlen(save) + sizeof("-s") + 1))))
685: err(1, NULL);
686: (void)sprintf(name, "%s %s", save, "-s");
687: }
688: return(name);
689: }
690:
691: /*
692: * jump --
693: * strip out flag argument and jump
694: */
695: static void
696: jump(argv, flag, name)
697: char **argv, *flag, *name;
698: {
699: char **arg;
700:
701: argv[0] = name;
702: for (arg = argv + 1; *arg; ++arg)
703: if (!strcmp(*arg, flag))
704: break;
705: for (; *arg; ++arg)
706: arg[0] = arg[1];
707: execvp(name, argv);
708: (void)fprintf(stderr, "%s: Command not found.\n", name);
709: exit(1);
710: }
711:
712: /*
713: * onsig --
714: * If signaled, delete the temporary files.
715: */
716: static void
717: onsig(signo)
718: int signo;
719: {
720: (void)cleanup();
721:
722: (void)signal(signo, SIG_DFL);
723: (void)kill(getpid(), signo);
724:
725: /* NOTREACHED */
726: exit (1);
727: }
728:
729: /*
730: * cleanup --
731: * Clean up temporary files, show any error messages.
732: */
733: static int
734: cleanup()
735: {
736: TAG *intmpp, *missp;
737: ENTRY *ep;
738: int rval;
739:
740: rval = 0;
741: ep = (missp = getlist("_missing")) == NULL ?
742: NULL : missp->list.tqh_first;
743: if (ep != NULL)
744: for (; ep != NULL; ep = ep->q.tqe_next) {
1.9 espie 745: if (section)
746: warnx("no entry for %s in section %s of the manual.",
747: ep->s, section->s);
748: else
749: warnx("no entry for %s in the manual.", ep->s);
1.1 deraadt 750: rval = 1;
751: }
752:
753: ep = (intmpp = getlist("_intmp")) == NULL ?
754: NULL : intmpp->list.tqh_first;
755: for (; ep != NULL; ep = ep->q.tqe_next)
756: (void)unlink(ep->s);
757: return (rval);
758: }
759:
760: /*
761: * usage --
762: * print usage message and die
763: */
764: static void
765: usage()
766: {
1.8 millert 767: (void)fprintf(stderr, "usage: %s [-achw] [-C file] [-M path] [-m path] "
768: "[-s section] [-S subsection] [section] title ...\n", __progname);
1.10 ! deraadt 769: (void)fprintf(stderr, "usage: %s -k keyword\n", __progname);
! 770: (void)fprintf(stderr, "usage: %s -f command\n", __progname);
1.1 deraadt 771: exit(1);
772: }