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