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