Annotation of src/usr.bin/man/man.c, Revision 1.21
1.21 ! deraadt 1: /* $OpenBSD: man.c,v 1.20 2002/02/16 21:27:48 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.21 ! deraadt 47: static char rcsid[] = "$OpenBSD: man.c,v 1.20 2002/02/16 21:27:48 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.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
1.21 ! deraadt 88: main(int argc, char *argv[])
1.1 deraadt 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.21 ! deraadt 271: /*
! 272: * puts it at the end, should be at the top,
! 273: * but then the added entries would be in
! 274: * reverse order, fix later when all are added
! 275: */
1.17 millert 276: TAILQ_INSERT_TAIL(&defp->list, ep, q);
1.21 ! deraadt 277: if (e_sectp == NULL)
! 278: /* save first added, to-be the new top */
1.3 michaels 279: e_sectp = ep;
1.1 deraadt 280: }
281: }
1.3 michaels 282: if (e_sectp != NULL) { /* entries added, fix order */
1.21 ! deraadt 283: /* save original head */
! 284: ep->q.tqe_next = defp->list.tqh_first;
! 285: /* first added entry, new top */
! 286: defp->list.tqh_first = e_sectp;
! 287: /* terminate list */
! 288: *e_sectp->q.tqe_prev = NULL;
1.3 michaels 289: }
1.17 millert 290: }
1.1 deraadt 291: /*
292: * 4: If no -m was specified, and a section was, rewrite the section's
293: * paths (if they have a trailing slash) to append the _subdir list
294: * and the machine. This then becomes the _default list.
295: */
296: if (p_add == NULL && section != NULL) {
297: sectnewp = addlist("_section_new");
298: for (e_sectp = section->list.tqh_first;
299: e_sectp != NULL; e_sectp = e_sectp->q.tqe_next) {
300: if (e_sectp->s[strlen(e_sectp->s) - 1] != '/') {
301: (void)snprintf(buf, sizeof(buf),
302: "%s{/%s,}", e_sectp->s, machine);
303: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
304: (ep->s = strdup(buf)) == NULL)
305: err(1, NULL);
306: TAILQ_INSERT_TAIL(§newp->list, ep, q);
307: continue;
308: }
309: e_subp = (subp = getlist("_subdir")) == NULL ?
310: NULL : subp->list.tqh_first;
311: for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
312: (void)snprintf(buf, sizeof(buf), "%s%s{/%s,}",
313: e_sectp->s, e_subp->s, machine);
314: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
315: (ep->s = strdup(buf)) == NULL)
316: err(1, NULL);
317: TAILQ_INSERT_TAIL(§newp->list, ep, q);
318: }
319: }
320: sectnewp->s = section->s;
321: defp = sectnewp;
322: TAILQ_REMOVE(&head, section, q);
323: }
324:
325: /*
326: * 5: Search for the files. Set up an interrupt handler, so the
327: * temporary files go away.
328: */
329: (void)signal(SIGINT, onsig);
330: (void)signal(SIGHUP, onsig);
331:
1.19 deraadt 332: sigemptyset(&blocksigs);
333: sigaddset(&blocksigs, SIGINT);
334: sigaddset(&blocksigs, SIGHUP);
335:
1.1 deraadt 336: memset(&pg, 0, sizeof(pg));
337: for (found = 0; *argv; ++argv)
338: if (manual(*argv, defp, &pg))
339: found = 1;
340:
341: /* 6: If nothing found, we're done. */
342: if (!found) {
343: (void)cleanup();
344: exit (1);
345: }
346:
347: /* 7: If it's simple, display it fast. */
348: if (f_cat) {
349: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
350: if (**ap == '\0')
351: continue;
352: cat(*ap);
353: }
354: exit (cleanup());
355: }
356: if (f_how) {
357: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
358: if (**ap == '\0')
359: continue;
360: how(*ap);
361: }
362: exit(cleanup());
363: }
364: if (f_where) {
365: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
366: if (**ap == '\0')
367: continue;
1.17 millert 368: (void)puts(*ap);
1.1 deraadt 369: }
370: exit(cleanup());
371: }
1.17 millert 372:
1.1 deraadt 373: /*
374: * 8: We display things in a single command; build a list of things
375: * to display.
376: */
377: for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
378: if (**ap == '\0')
379: continue;
380: len += strlen(*ap) + 1;
381: }
382: if ((cmd = malloc(len)) == NULL) {
383: warn(NULL);
384: (void)cleanup();
385: exit(1);
386: }
387: p = cmd;
388: len = strlen(pager);
1.17 millert 389: memcpy(p, pager, len);
1.1 deraadt 390: p += len;
391: *p++ = ' ';
392: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
393: if (**ap == '\0')
394: continue;
395: len = strlen(*ap);
1.17 millert 396: memcpy(p, *ap, len);
1.1 deraadt 397: p += len;
398: *p++ = ' ';
399: }
1.12 deraadt 400: *--p = '\0';
1.1 deraadt 401:
402: /* Use system(3) in case someone's pager is "pager arg1 arg2". */
403: (void)system(cmd);
404:
405: exit(cleanup());
406: }
407:
408: /*
409: * manual --
410: * Search the manuals for the pages.
411: */
412: static int
413: manual(page, tag, pg)
414: char *page;
415: TAG *tag;
416: glob_t *pg;
417: {
418: ENTRY *ep, *e_sufp, *e_tag;
419: TAG *missp, *sufp;
420: int anyfound, cnt, found;
1.6 deraadt 421: char *p, buf[MAXPATHLEN];
1.1 deraadt 422:
423: anyfound = 0;
424: buf[0] = '*';
425:
426: /* For each element in the list... */
427: e_tag = tag == NULL ? NULL : tag->list.tqh_first;
428: for (; e_tag != NULL; e_tag = e_tag->q.tqe_next) {
429: (void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
430: if (glob(buf,
431: GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
432: NULL, pg)) {
433: warn("globbing");
434: (void)cleanup();
435: exit(1);
436: }
437: if (pg->gl_matchc == 0)
438: continue;
439:
440: /* Find out if it's really a man page. */
441: for (cnt = pg->gl_pathc - pg->gl_matchc;
442: cnt < pg->gl_pathc; ++cnt) {
443:
444: /*
445: * Try the _suffix key words first.
446: *
447: * XXX
448: * Older versions of man.conf didn't have the suffix
449: * key words, it was assumed that everything was a .0.
450: * We just test for .0 first, it's fast and probably
451: * going to hit.
452: */
453: (void)snprintf(buf, sizeof(buf), "*/%s.0", page);
454: if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
455: goto next;
456:
457: e_sufp = (sufp = getlist("_suffix")) == NULL ?
458: NULL : sufp->list.tqh_first;
459: for (found = 0;
460: e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
461: (void)snprintf(buf,
462: sizeof(buf), "*/%s%s", page, e_sufp->s);
463: if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
464: found = 1;
465: break;
466: }
467: }
468: if (found)
469: goto next;
470:
471: /* Try the _build key words next. */
472: e_sufp = (sufp = getlist("_build")) == NULL ?
473: NULL : sufp->list.tqh_first;
474: for (found = 0;
475: e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
476: for (p = e_sufp->s;
477: *p != '\0' && !isspace(*p); ++p);
478: if (*p == '\0')
479: continue;
480: *p = '\0';
481: (void)snprintf(buf,
482: sizeof(buf), "*/%s%s", page, e_sufp->s);
483: if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
484: if (!f_where)
485: build_page(p + 1,
486: &pg->gl_pathv[cnt]);
487: *p = ' ';
488: found = 1;
489: break;
490: }
491: *p = ' ';
492: }
493: if (found) {
494: next: anyfound = 1;
495: if (!f_all) {
496: /* Delete any other matches. */
497: while (++cnt< pg->gl_pathc)
498: pg->gl_pathv[cnt] = "";
499: break;
500: }
501: continue;
502: }
503:
504: /* It's not a man page, forget about it. */
505: pg->gl_pathv[cnt] = "";
506: }
507:
508: if (anyfound && !f_all)
509: break;
510: }
511:
512: /* If not found, enter onto the missing list. */
513: if (!anyfound) {
1.19 deraadt 514: sigset_t osigs;
515:
516: sigprocmask(SIG_BLOCK, &blocksigs, &osigs);
517:
1.1 deraadt 518: if ((missp = getlist("_missing")) == NULL)
519: missp = addlist("_missing");
520: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
521: (ep->s = strdup(page)) == NULL) {
522: warn(NULL);
523: (void)cleanup();
524: exit(1);
525: }
526: TAILQ_INSERT_TAIL(&missp->list, ep, q);
1.19 deraadt 527: sigprocmask(SIG_SETMASK, &osigs, NULL);
1.1 deraadt 528: }
529: return (anyfound);
530: }
531:
1.17 millert 532: /*
1.1 deraadt 533: * build_page --
534: * Build a man page for display.
535: */
536: static void
1.21 ! deraadt 537: build_page(char *fmt, char **pathp)
1.1 deraadt 538: {
539: static int warned;
540: ENTRY *ep;
541: TAG *intmpp;
542: int fd, n;
543: char *p, *b;
1.4 deraadt 544: char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
1.19 deraadt 545: sigset_t osigs;
1.1 deraadt 546:
547: /* Let the user know this may take awhile. */
548: if (!warned) {
549: warned = 1;
550: warnx("Formatting manual page...");
551: }
552:
553: /*
1.17 millert 554: * Historically man chdir'd to the root of the man tree.
1.1 deraadt 555: * This was used in man pages that contained relative ".so"
556: * directives (including other man pages for command aliases etc.)
557: * It even went one step farther, by examining the first line
558: * of the man page and parsing the .so filename so it would
559: * make hard(?) links to the cat'ted man pages for space savings.
560: * (We don't do that here, but we could).
561: */
1.17 millert 562:
1.1 deraadt 563: /* copy and find the end */
564: for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
565: continue;
1.17 millert 566:
1.1 deraadt 567: /* skip the last two path components, page name and man[n] */
568: for (--b, n = 2; b != buf; b--)
569: if (*b == '/')
570: if (--n == 0) {
571: *b = '\0';
572: (void) chdir(buf);
573: }
574:
575:
576: /* Add a remove-when-done list. */
1.19 deraadt 577: sigprocmask(SIG_BLOCK, &blocksigs, &osigs);
1.1 deraadt 578: if ((intmpp = getlist("_intmp")) == NULL)
579: intmpp = addlist("_intmp");
1.19 deraadt 580: sigprocmask(SIG_SETMASK, &osigs, NULL);
1.1 deraadt 581:
582: /* Move to the printf(3) format string. */
1.16 deraadt 583: for (; *fmt && isspace(*fmt); ++fmt)
584: ;
1.1 deraadt 585:
586: /*
587: * Get a temporary file and build a version of the file
588: * to display. Replace the old file name with the new one.
589: */
1.18 deraadt 590: (void)strlcpy(tpath, _PATH_TMPFILE, sizeof(tpath));
1.1 deraadt 591: if ((fd = mkstemp(tpath)) == -1) {
592: warn("%s", tpath);
593: (void)cleanup();
594: exit(1);
595: }
596: (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
597: (void)snprintf(cmd, sizeof(cmd), buf, *pathp);
598: (void)system(cmd);
599: (void)close(fd);
600: if ((*pathp = strdup(tpath)) == NULL) {
601: warn(NULL);
602: (void)cleanup();
603: exit(1);
604: }
605:
606: /* Link the built file into the remove-when-done list. */
607: if ((ep = malloc(sizeof(ENTRY))) == NULL) {
608: warn(NULL);
609: (void)cleanup();
610: exit(1);
611: }
612: ep->s = *pathp;
613: TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
614: }
615:
616: /*
617: * how --
618: * display how information
619: */
620: static void
1.21 ! deraadt 621: how(char *fname)
1.1 deraadt 622: {
623: FILE *fp;
624:
625: int lcnt, print;
626: char *p, buf[256];
627:
628: if (!(fp = fopen(fname, "r"))) {
629: warn("%s", fname);
630: (void)cleanup();
631: exit (1);
632: }
633: #define S1 "SYNOPSIS"
634: #define S2 "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
635: #define D1 "DESCRIPTION"
636: #define D2 "D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
637: for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
638: if (!strncmp(buf, S1, sizeof(S1) - 1) ||
639: !strncmp(buf, S2, sizeof(S2) - 1)) {
640: print = 1;
641: continue;
642: } else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
643: !strncmp(buf, D2, sizeof(D2) - 1))
644: return;
645: if (!print)
646: continue;
647: if (*buf == '\n')
648: ++lcnt;
649: else {
1.17 millert 650: while (lcnt) {
651: --lcnt;
1.1 deraadt 652: (void)putchar('\n');
1.17 millert 653: }
654: for (p = buf; isspace(*p); ++p)
655: ;
1.1 deraadt 656: (void)fputs(p, stdout);
657: }
658: }
659: (void)fclose(fp);
660: }
661:
662: /*
663: * cat --
664: * cat out the file
665: */
666: static void
1.21 ! deraadt 667: cat(char *fname)
1.1 deraadt 668: {
669: int fd, n;
670: char buf[2048];
671:
672: if ((fd = open(fname, O_RDONLY, 0)) < 0) {
673: warn("%s", fname);
674: (void)cleanup();
675: exit(1);
676: }
677: while ((n = read(fd, buf, sizeof(buf))) > 0)
678: if (write(STDOUT_FILENO, buf, n) != n) {
679: warn("write");
680: (void)cleanup();
681: exit (1);
682: }
683: if (n == -1) {
684: warn("read");
685: (void)cleanup();
686: exit(1);
687: }
688: (void)close(fd);
689: }
690:
691: /*
692: * check_pager --
693: * check the user supplied page information
694: */
695: static char *
1.21 ! deraadt 696: check_pager(char *name)
1.1 deraadt 697: {
698: char *p, *save;
699:
700: /*
701: * if the user uses "more", we make it "more -s"; watch out for
1.17 millert 702: * PAGER = "mypager /usr/bin/more"
1.1 deraadt 703: */
1.16 deraadt 704: for (p = name; *p && !isspace(*p); ++p)
705: ;
706: for (; p > name && *p != '/'; --p)
707: ;
1.1 deraadt 708: if (p != name)
709: ++p;
710:
711: /* make sure it's "more", not "morex" */
712: if (!strncmp(p, "more", 4) && (!p[4] || isspace(p[4]))){
713: save = name;
714: /* allocate space to add the "-s" */
715: if (!(name =
1.17 millert 716: malloc(strlen(save) + 1 + sizeof("-s"))))
1.1 deraadt 717: err(1, NULL);
718: (void)sprintf(name, "%s %s", save, "-s");
719: }
720: return(name);
721: }
722:
723: /*
724: * jump --
725: * strip out flag argument and jump
726: */
727: static void
1.21 ! deraadt 728: jump(char **argv, char *flag, char *name)
1.1 deraadt 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
1.21 ! deraadt 748: onsig(int signo)
1.1 deraadt 749: {
1.21 ! deraadt 750: (void)cleanup(); /* XXX signal race */
1.1 deraadt 751:
752: (void)signal(signo, SIG_DFL);
753: (void)kill(getpid(), signo);
754:
755: /* NOTREACHED */
1.19 deraadt 756: _exit(1);
1.1 deraadt 757: }
758:
759: /*
760: * cleanup --
761: * Clean up temporary files, show any error messages.
762: */
763: static int
1.21 ! deraadt 764: cleanup(void)
1.1 deraadt 765: {
766: TAG *intmpp, *missp;
767: ENTRY *ep;
768: int rval;
769:
770: rval = 0;
771: ep = (missp = getlist("_missing")) == NULL ?
772: NULL : missp->list.tqh_first;
773: if (ep != NULL)
774: for (; ep != NULL; ep = ep->q.tqe_next) {
1.9 espie 775: if (section)
776: warnx("no entry for %s in section %s of the manual.",
777: ep->s, section->s);
778: else
779: warnx("no entry for %s in the manual.", ep->s);
1.1 deraadt 780: rval = 1;
781: }
782:
783: ep = (intmpp = getlist("_intmp")) == NULL ?
784: NULL : intmpp->list.tqh_first;
785: for (; ep != NULL; ep = ep->q.tqe_next)
786: (void)unlink(ep->s);
787: return (rval);
788: }
789:
790: /*
791: * usage --
792: * print usage message and die
793: */
794: static void
1.21 ! deraadt 795: usage(void)
1.1 deraadt 796: {
1.8 millert 797: (void)fprintf(stderr, "usage: %s [-achw] [-C file] [-M path] [-m path] "
798: "[-s section] [-S subsection] [section] title ...\n", __progname);
1.10 deraadt 799: (void)fprintf(stderr, "usage: %s -k keyword\n", __progname);
800: (void)fprintf(stderr, "usage: %s -f command\n", __progname);
1.1 deraadt 801: exit(1);
802: }