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