Annotation of src/usr.bin/man/man.c, Revision 1.36
1.36 ! jmc 1: /* $OpenBSD: man.c,v 1.35 2007/08/06 19:16:06 sobrado 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.36 ! jmc 43: static char rcsid[] = "$OpenBSD: man.c,v 1.35 2007/08/06 19:16:06 sobrado 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.27 millert 162: else if ((pager = getenv("MANPAGER")) != NULL &&
163: (*pager != '\0'))
164: pager = check_pager(pager);
1.13 pjanzen 165: else if ((pager = getenv("PAGER")) != NULL && (*pager != '\0'))
1.1 deraadt 166: pager = check_pager(pager);
167: else
168: pager = _PATH_PAGER;
1.17 millert 169: }
1.1 deraadt 170:
171: /* Read the configuration file. */
172: config(conffile);
173:
1.7 millert 174: /* Get the machine type unless specified by -S. */
175: if (machine == NULL && (machine = getenv("MACHINE")) == NULL)
1.1 deraadt 176: machine = MACHINE;
177:
178: /* If there's no _default list, create an empty one. */
179: if ((defp = getlist("_default")) == NULL)
180: defp = addlist("_default");
181:
182: /*
183: * 1: If the user specified a MANPATH variable, or set the -M
184: * option, we replace the _default list with the user's list,
185: * appending the entries in the _subdir list and the machine.
186: */
187: if (p_path == NULL)
188: p_path = getenv("MANPATH");
189: if (p_path != NULL) {
1.29 otto 190: while ((e_defp = TAILQ_FIRST(&defp->list)) != NULL) {
1.1 deraadt 191: free(e_defp->s);
192: TAILQ_REMOVE(&defp->list, e_defp, q);
193: }
194: for (p = strtok(p_path, ":");
195: p != NULL; p = strtok(NULL, ":")) {
196: slashp = p[strlen(p) - 1] == '/' ? "" : "/";
197: e_subp = (subp = getlist("_subdir")) == NULL ?
1.29 otto 198: NULL : TAILQ_FIRST(&subp->list);
199: for (; e_subp != NULL; e_subp = TAILQ_NEXT(e_subp, q)) {
1.1 deraadt 200: (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
201: p, slashp, e_subp->s, machine);
202: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
203: (ep->s = strdup(buf)) == NULL)
204: err(1, NULL);
205: TAILQ_INSERT_TAIL(&defp->list, ep, q);
206: }
207: }
208: }
209:
210: /*
211: * 2: If the user did not specify MANPATH, -M or a section, rewrite
212: * the _default list to include the _subdir list and the machine.
213: */
1.7 millert 214: if (sflag == NULL && argv[1] == NULL)
1.1 deraadt 215: section = NULL;
1.7 millert 216: else {
217: if (sflag != NULL && (section = getlist(sflag)) == NULL)
218: errx(1, "unknown manual section `%s'", sflag);
219: else if (sflag == NULL && (section = getlist(*argv)) != NULL)
220: ++argv;
221: }
1.1 deraadt 222: if (p_path == NULL && section == NULL) {
223: defnewp = addlist("_default_new");
1.29 otto 224: e_defp = TAILQ_FIRST(&defp->list);
225: for (; e_defp != NULL; e_defp = TAILQ_NEXT(e_defp, q)) {
1.1 deraadt 226: slashp =
227: e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
228: e_subp = (subp = getlist("_subdir")) == NULL ?
1.29 otto 229: NULL : TAILQ_FIRST(&subp->list);
230: for (; e_subp != NULL; e_subp = TAILQ_NEXT(e_subp, q)) {
1.1 deraadt 231: (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
232: e_defp->s, slashp, e_subp->s, machine);
233: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
234: (ep->s = strdup(buf)) == NULL)
235: err(1, NULL);
236: TAILQ_INSERT_TAIL(&defnewp->list, ep, q);
237: }
238: }
239: defp = getlist("_default");
1.29 otto 240: while ((e_defp = TAILQ_FIRST(&defp->list)) != NULL) {
1.1 deraadt 241: free(e_defp->s);
242: TAILQ_REMOVE(&defp->list, e_defp, q);
243: }
244: free(defp->s);
245: TAILQ_REMOVE(&head, defp, q);
246: defnewp = getlist("_default_new");
247: free(defnewp->s);
248: defnewp->s = "_default";
249: defp = defnewp;
250: }
251:
252: /*
253: * 3: If the user set the -m option, insert the user's list before
254: * whatever list we have, again appending the _subdir list and
1.17 millert 255: * the machine.
1.1 deraadt 256: */
1.3 michaels 257: if (p_add != NULL) {
258: e_sectp = NULL;
1.1 deraadt 259: for (p = strtok(p_add, ":"); p != NULL; p = strtok(NULL, ":")) {
260: slashp = p[strlen(p) - 1] == '/' ? "" : "/";
261: e_subp = (subp = getlist("_subdir")) == NULL ?
1.29 otto 262: NULL : TAILQ_FIRST(&subp->list);
263: for (; e_subp != NULL; e_subp = TAILQ_NEXT(e_subp, q)) {
1.1 deraadt 264: (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
265: p, slashp, e_subp->s, machine);
266: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
267: (ep->s = strdup(buf)) == NULL)
268: err(1, NULL);
1.30 otto 269:
1.21 deraadt 270: if (e_sectp == NULL)
1.30 otto 271: TAILQ_INSERT_HEAD(&defp->list, ep, q);
272: else
273: TAILQ_INSERT_AFTER(&defp->list, e_sectp,
274: ep, q);
275: e_sectp = ep;
1.1 deraadt 276: }
1.3 michaels 277: }
1.17 millert 278: }
1.1 deraadt 279: /*
280: * 4: If no -m was specified, and a section was, rewrite the section's
281: * paths (if they have a trailing slash) to append the _subdir list
282: * and the machine. This then becomes the _default list.
283: */
284: if (p_add == NULL && section != NULL) {
285: sectnewp = addlist("_section_new");
1.29 otto 286: TAILQ_FOREACH(e_sectp, §ion->list, q) {
1.1 deraadt 287: if (e_sectp->s[strlen(e_sectp->s) - 1] != '/') {
288: (void)snprintf(buf, sizeof(buf),
289: "%s{/%s,}", e_sectp->s, machine);
290: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
291: (ep->s = strdup(buf)) == NULL)
292: err(1, NULL);
293: TAILQ_INSERT_TAIL(§newp->list, ep, q);
294: continue;
295: }
296: e_subp = (subp = getlist("_subdir")) == NULL ?
1.29 otto 297: NULL : TAILQ_FIRST(&subp->list);
298: for (; e_subp != NULL; e_subp = TAILQ_NEXT(e_subp, q)) {
1.1 deraadt 299: (void)snprintf(buf, sizeof(buf), "%s%s{/%s,}",
300: e_sectp->s, e_subp->s, machine);
301: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
302: (ep->s = strdup(buf)) == NULL)
303: err(1, NULL);
304: TAILQ_INSERT_TAIL(§newp->list, ep, q);
305: }
306: }
307: sectnewp->s = section->s;
308: defp = sectnewp;
309: TAILQ_REMOVE(&head, section, q);
310: }
311:
312: /*
313: * 5: Search for the files. Set up an interrupt handler, so the
314: * temporary files go away.
315: */
316: (void)signal(SIGINT, onsig);
317: (void)signal(SIGHUP, onsig);
318:
1.19 deraadt 319: sigemptyset(&blocksigs);
320: sigaddset(&blocksigs, SIGINT);
321: sigaddset(&blocksigs, SIGHUP);
322:
1.1 deraadt 323: memset(&pg, 0, sizeof(pg));
324: for (found = 0; *argv; ++argv)
325: if (manual(*argv, defp, &pg))
326: found = 1;
327:
328: /* 6: If nothing found, we're done. */
329: if (!found) {
330: (void)cleanup();
331: exit (1);
332: }
333:
334: /* 7: If it's simple, display it fast. */
335: if (f_cat) {
336: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
337: if (**ap == '\0')
338: continue;
339: cat(*ap);
340: }
341: exit (cleanup());
342: }
343: if (f_how) {
344: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
345: if (**ap == '\0')
346: continue;
347: how(*ap);
348: }
349: exit(cleanup());
350: }
351: if (f_where) {
352: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
353: if (**ap == '\0')
354: continue;
1.17 millert 355: (void)puts(*ap);
1.1 deraadt 356: }
357: exit(cleanup());
358: }
1.17 millert 359:
1.1 deraadt 360: /*
361: * 8: We display things in a single command; build a list of things
362: * to display.
363: */
364: for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
365: if (**ap == '\0')
366: continue;
367: len += strlen(*ap) + 1;
368: }
369: if ((cmd = malloc(len)) == NULL) {
370: warn(NULL);
371: (void)cleanup();
372: exit(1);
373: }
374: p = cmd;
375: len = strlen(pager);
1.17 millert 376: memcpy(p, pager, len);
1.1 deraadt 377: p += len;
378: *p++ = ' ';
379: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
380: if (**ap == '\0')
381: continue;
382: len = strlen(*ap);
1.17 millert 383: memcpy(p, *ap, len);
1.1 deraadt 384: p += len;
385: *p++ = ' ';
386: }
1.12 deraadt 387: *--p = '\0';
1.1 deraadt 388:
389: /* Use system(3) in case someone's pager is "pager arg1 arg2". */
390: (void)system(cmd);
391:
392: exit(cleanup());
393: }
394:
395: /*
396: * manual --
397: * Search the manuals for the pages.
398: */
399: static int
1.26 deraadt 400: manual(char *page, TAG *tag, glob_t *pg)
1.1 deraadt 401: {
402: ENTRY *ep, *e_sufp, *e_tag;
403: TAG *missp, *sufp;
404: int anyfound, cnt, found;
1.6 deraadt 405: char *p, buf[MAXPATHLEN];
1.1 deraadt 406:
407: anyfound = 0;
408: buf[0] = '*';
409:
410: /* For each element in the list... */
1.29 otto 411: e_tag = tag == NULL ? NULL : TAILQ_FIRST(&tag->list);
412: for (; e_tag != NULL; e_tag = TAILQ_NEXT(e_tag, q)) {
1.1 deraadt 413: (void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
414: if (glob(buf,
415: GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
416: NULL, pg)) {
417: warn("globbing");
418: (void)cleanup();
419: exit(1);
420: }
421: if (pg->gl_matchc == 0)
422: continue;
423:
424: /* Find out if it's really a man page. */
425: for (cnt = pg->gl_pathc - pg->gl_matchc;
426: cnt < pg->gl_pathc; ++cnt) {
427:
428: /*
429: * Try the _suffix key words first.
430: *
431: * XXX
432: * Older versions of man.conf didn't have the suffix
433: * key words, it was assumed that everything was a .0.
434: * We just test for .0 first, it's fast and probably
435: * going to hit.
436: */
437: (void)snprintf(buf, sizeof(buf), "*/%s.0", page);
438: if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
439: goto next;
440:
441: e_sufp = (sufp = getlist("_suffix")) == NULL ?
1.29 otto 442: NULL : TAILQ_FIRST(&sufp->list);
1.1 deraadt 443: for (found = 0;
1.29 otto 444: e_sufp != NULL; e_sufp = TAILQ_NEXT(e_sufp, q)) {
1.1 deraadt 445: (void)snprintf(buf,
446: sizeof(buf), "*/%s%s", page, e_sufp->s);
447: if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
448: found = 1;
449: break;
450: }
451: }
452: if (found)
453: goto next;
454:
455: /* Try the _build key words next. */
456: e_sufp = (sufp = getlist("_build")) == NULL ?
1.29 otto 457: NULL : TAILQ_FIRST(&sufp->list);
1.1 deraadt 458: for (found = 0;
1.29 otto 459: e_sufp != NULL; e_sufp = TAILQ_NEXT(e_sufp, q)) {
1.1 deraadt 460: for (p = e_sufp->s;
461: *p != '\0' && !isspace(*p); ++p);
462: if (*p == '\0')
463: continue;
464: *p = '\0';
465: (void)snprintf(buf,
466: sizeof(buf), "*/%s%s", page, e_sufp->s);
467: if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
468: if (!f_where)
469: build_page(p + 1,
470: &pg->gl_pathv[cnt]);
471: *p = ' ';
472: found = 1;
473: break;
474: }
475: *p = ' ';
476: }
477: if (found) {
478: next: anyfound = 1;
479: if (!f_all) {
480: /* Delete any other matches. */
481: while (++cnt< pg->gl_pathc)
482: pg->gl_pathv[cnt] = "";
483: break;
484: }
485: continue;
486: }
487:
488: /* It's not a man page, forget about it. */
489: pg->gl_pathv[cnt] = "";
490: }
491:
492: if (anyfound && !f_all)
493: break;
494: }
495:
496: /* If not found, enter onto the missing list. */
497: if (!anyfound) {
1.19 deraadt 498: sigset_t osigs;
499:
500: sigprocmask(SIG_BLOCK, &blocksigs, &osigs);
501:
1.1 deraadt 502: if ((missp = getlist("_missing")) == NULL)
503: missp = addlist("_missing");
504: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
505: (ep->s = strdup(page)) == NULL) {
506: warn(NULL);
507: (void)cleanup();
508: exit(1);
509: }
510: TAILQ_INSERT_TAIL(&missp->list, ep, q);
1.19 deraadt 511: sigprocmask(SIG_SETMASK, &osigs, NULL);
1.1 deraadt 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
1.21 deraadt 521: build_page(char *fmt, char **pathp)
1.1 deraadt 522: {
523: static int warned;
524: ENTRY *ep;
525: TAG *intmpp;
526: int fd, n;
527: char *p, *b;
1.4 deraadt 528: char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
1.19 deraadt 529: sigset_t osigs;
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. */
1.19 deraadt 561: sigprocmask(SIG_BLOCK, &blocksigs, &osigs);
1.1 deraadt 562: if ((intmpp = getlist("_intmp")) == NULL)
563: intmpp = addlist("_intmp");
1.19 deraadt 564: sigprocmask(SIG_SETMASK, &osigs, NULL);
1.1 deraadt 565:
566: /* Move to the printf(3) format string. */
1.16 deraadt 567: for (; *fmt && isspace(*fmt); ++fmt)
568: ;
1.1 deraadt 569:
570: /*
571: * Get a temporary file and build a version of the file
572: * to display. Replace the old file name with the new one.
573: */
1.18 deraadt 574: (void)strlcpy(tpath, _PATH_TMPFILE, sizeof(tpath));
1.1 deraadt 575: if ((fd = mkstemp(tpath)) == -1) {
576: warn("%s", tpath);
577: (void)cleanup();
578: exit(1);
579: }
580: (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
581: (void)snprintf(cmd, sizeof(cmd), buf, *pathp);
582: (void)system(cmd);
583: (void)close(fd);
584: if ((*pathp = strdup(tpath)) == NULL) {
585: warn(NULL);
586: (void)cleanup();
587: exit(1);
588: }
589:
590: /* Link the built file into the remove-when-done list. */
591: if ((ep = malloc(sizeof(ENTRY))) == NULL) {
592: warn(NULL);
593: (void)cleanup();
594: exit(1);
595: }
596: ep->s = *pathp;
597: TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
598: }
599:
600: /*
601: * how --
602: * display how information
603: */
604: static void
1.21 deraadt 605: how(char *fname)
1.1 deraadt 606: {
607: FILE *fp;
608:
609: int lcnt, print;
610: char *p, buf[256];
611:
612: if (!(fp = fopen(fname, "r"))) {
613: warn("%s", fname);
614: (void)cleanup();
615: exit (1);
616: }
617: #define S1 "SYNOPSIS"
618: #define S2 "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
619: for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
620: if (!strncmp(buf, S1, sizeof(S1) - 1) ||
621: !strncmp(buf, S2, sizeof(S2) - 1)) {
622: print = 1;
623: continue;
1.34 otto 624: } else if (print) {
625: char *p = buf;
626: int allcaps = 0;
627:
628: while (*p) {
629: if (!allcaps && isalpha(*p))
630: allcaps = 1;
631: if (isalpha(*p) && !isupper(*p)) {
632: allcaps = 0;
633: break;
634: }
635: p++;
636: }
637: if (allcaps) {
638: (void)fclose(fp);
639: return;
640: }
1.32 jasper 641: }
1.1 deraadt 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
1.21 deraadt 664: cat(char *fname)
1.1 deraadt 665: {
666: int fd, n;
667: char buf[2048];
668:
669: if ((fd = open(fname, O_RDONLY, 0)) < 0) {
670: warn("%s", fname);
671: (void)cleanup();
672: exit(1);
673: }
674: while ((n = read(fd, buf, sizeof(buf))) > 0)
675: if (write(STDOUT_FILENO, buf, n) != n) {
676: warn("write");
677: (void)cleanup();
678: exit (1);
679: }
680: if (n == -1) {
681: warn("read");
682: (void)cleanup();
683: exit(1);
684: }
685: (void)close(fd);
686: }
687:
688: /*
689: * check_pager --
690: * check the user supplied page information
691: */
692: static char *
1.21 deraadt 693: check_pager(char *name)
1.1 deraadt 694: {
695: char *p, *save;
696:
697: /*
698: * if the user uses "more", we make it "more -s"; watch out for
1.17 millert 699: * PAGER = "mypager /usr/bin/more"
1.1 deraadt 700: */
1.16 deraadt 701: for (p = name; *p && !isspace(*p); ++p)
702: ;
703: for (; p > name && *p != '/'; --p)
704: ;
1.1 deraadt 705: if (p != name)
706: ++p;
707:
708: /* make sure it's "more", not "morex" */
1.27 millert 709: if (!strncmp(p, "more", 4) && (p[4] == '\0' || isspace(p[4]))){
1.1 deraadt 710: save = name;
711: /* allocate space to add the "-s" */
1.31 deraadt 712: if (asprintf(&name, "%s -s", save) == -1)
713: err(1, "asprintf");
1.1 deraadt 714: }
715: return(name);
716: }
717:
718: /*
719: * jump --
720: * strip out flag argument and jump
721: */
722: static void
1.21 deraadt 723: jump(char **argv, char *flag, char *name)
1.1 deraadt 724: {
725: char **arg;
726:
727: argv[0] = name;
728: for (arg = argv + 1; *arg; ++arg)
729: if (!strcmp(*arg, flag))
730: break;
731: for (; *arg; ++arg)
732: arg[0] = arg[1];
733: execvp(name, argv);
734: (void)fprintf(stderr, "%s: Command not found.\n", name);
735: exit(1);
736: }
737:
1.17 millert 738: /*
1.1 deraadt 739: * onsig --
740: * If signaled, delete the temporary files.
741: */
742: static void
1.21 deraadt 743: onsig(int signo)
1.1 deraadt 744: {
1.21 deraadt 745: (void)cleanup(); /* XXX signal race */
1.1 deraadt 746:
747: (void)signal(signo, SIG_DFL);
748: (void)kill(getpid(), signo);
749:
750: /* NOTREACHED */
1.19 deraadt 751: _exit(1);
1.1 deraadt 752: }
753:
754: /*
755: * cleanup --
756: * Clean up temporary files, show any error messages.
757: */
758: static int
1.21 deraadt 759: cleanup(void)
1.1 deraadt 760: {
761: TAG *intmpp, *missp;
762: ENTRY *ep;
763: int rval;
764:
765: rval = 0;
766: ep = (missp = getlist("_missing")) == NULL ?
1.29 otto 767: NULL : TAILQ_FIRST(&missp->list);
1.1 deraadt 768: if (ep != NULL)
1.29 otto 769: for (; ep != NULL; ep = TAILQ_NEXT(ep, q)) {
1.9 espie 770: if (section)
771: warnx("no entry for %s in section %s of the manual.",
772: ep->s, section->s);
773: else
774: warnx("no entry for %s in the manual.", ep->s);
1.1 deraadt 775: rval = 1;
776: }
777:
778: ep = (intmpp = getlist("_intmp")) == NULL ?
1.29 otto 779: NULL : TAILQ_FIRST(&intmpp->list);
780: for (; ep != NULL; ep = TAILQ_NEXT(ep, q))
1.1 deraadt 781: (void)unlink(ep->s);
782: return (rval);
783: }
784:
785: /*
786: * usage --
787: * print usage message and die
788: */
789: static void
1.21 deraadt 790: usage(void)
1.1 deraadt 791: {
1.8 millert 792: (void)fprintf(stderr, "usage: %s [-achw] [-C file] [-M path] [-m path] "
1.35 sobrado 793: "[-S subsection] [-s section]\n\t [section] name ...\n",
1.28 jmc 794: __progname);
1.36 ! jmc 795: (void)fprintf(stderr, " %s -f command ...\n", __progname);
! 796: (void)fprintf(stderr, " %s -k keyword ...\n", __progname);
1.1 deraadt 797: exit(1);
798: }