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