Annotation of src/usr.bin/man/man.c, Revision 1.42
1.42 ! schwarze 1: /* $OpenBSD: man.c,v 1.41 2011/05/25 14:36:04 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: man.c,v 1.7 1995/09/28 06:05:34 tls Exp $ */
3:
4: /*
1.42 ! schwarze 5: * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.39 schwarze 6: *
7: * Permission to use, copy, modify, and distribute this software for any
8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
10: *
11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18: */
19:
20: /*
1.1 deraadt 21: * Copyright (c) 1987, 1993, 1994, 1995
22: * The Regents of the University of California. All rights reserved.
23: *
24: * Redistribution and use in source and binary forms, with or without
25: * modification, are permitted provided that the following conditions
26: * are met:
27: * 1. Redistributions of source code must retain the above copyright
28: * notice, this list of conditions and the following disclaimer.
29: * 2. Redistributions in binary form must reproduce the above copyright
30: * notice, this list of conditions and the following disclaimer in the
31: * documentation and/or other materials provided with the distribution.
1.25 millert 32: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 33: * may be used to endorse or promote products derived from this software
34: * without specific prior written permission.
35: *
36: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46: * SUCH DAMAGE.
47: */
48:
1.42 ! schwarze 49: #include <sys/types.h>
1.1 deraadt 50: #include <sys/param.h>
51: #include <sys/queue.h>
1.42 ! schwarze 52: #include <sys/stat.h>
1.1 deraadt 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.39 schwarze 71: static char gbuf[MAXPATHLEN * 2];
72: static TAG *section;
1.1 deraadt 73:
1.8 millert 74: extern char *__progname;
75:
1.39 schwarze 76: static void clearlist(TAG *);
77: static void parse_path(TAG *, char *);
78: static void append_subdirs(TAG *, const char *);
1.20 millert 79: static void build_page(char *, char **);
80: static void cat(char *);
81: static char *check_pager(char *);
1.41 deraadt 82: static int cleanup(int);
1.20 millert 83: static void how(char *);
84: static void jump(char **, char *, char *);
85: static int manual(char *, TAG *, glob_t *);
1.42 ! schwarze 86: static void check_companion(char **);
1.20 millert 87: static void onsig(int);
88: static void usage(void);
1.1 deraadt 89:
1.19 deraadt 90: sigset_t blocksigs;
91:
1.1 deraadt 92: int
1.21 deraadt 93: main(int argc, char *argv[])
1.1 deraadt 94: {
95: extern char *optarg;
96: extern int optind;
1.39 schwarze 97: TAG *searchlist;
98: ENTRY *ep;
1.1 deraadt 99: glob_t pg;
100: size_t len;
101: int ch, f_cat, f_how, found;
1.39 schwarze 102: char **ap, *cmd, *machine, *p, *p_add, *p_path, *pager, *sflag;
103: char *conffile;
1.11 deraadt 104:
105: if (argv[1] == NULL && strcmp(basename(__progname), "help") == 0) {
106: static char *nargv[3];
107: nargv[0] = "man";
1.15 aaron 108: nargv[1] = "help";
1.11 deraadt 109: nargv[2] = NULL;
110: argv = nargv;
111: argc = 2;
112: }
1.1 deraadt 113:
1.7 millert 114: machine = sflag = NULL;
1.1 deraadt 115: f_cat = f_how = 0;
116: conffile = p_add = p_path = NULL;
1.23 millert 117: while ((ch = getopt(argc, argv, "aC:cfhkM:m:P:s:S:w-")) != -1)
1.1 deraadt 118: switch (ch) {
119: case 'a':
120: f_all = 1;
121: break;
122: case 'C':
123: conffile = optarg;
124: break;
125: case 'c':
126: case '-': /* Deprecated. */
127: f_cat = 1;
128: break;
129: case 'h':
130: f_how = 1;
131: break;
132: case 'm':
133: p_add = optarg;
134: break;
135: case 'M':
136: case 'P': /* Backward compatibility. */
137: p_path = optarg;
138: break;
1.7 millert 139: case 's': /* SVR4 compatibility. */
140: sflag = optarg;
141: break;
142: case 'S':
143: machine = optarg;
144: break;
1.1 deraadt 145: /*
1.22 espie 146: * The -f and -k options are backward compatible
147: * ways of calling whatis(1) and apropos(1).
1.1 deraadt 148: */
149: case 'f':
150: jump(argv, "-f", "whatis");
151: /* NOTREACHED */
152: case 'k':
153: jump(argv, "-k", "apropos");
154: /* NOTREACHED */
155: case 'w':
156: f_all = f_where = 1;
157: break;
158: case '?':
159: default:
160: usage();
161: }
162: argc -= optind;
163: argv += optind;
164:
165: if (!*argv)
166: usage();
167:
1.17 millert 168: if (!f_cat && !f_how && !f_where) {
1.1 deraadt 169: if (!isatty(1))
170: f_cat = 1;
1.27 millert 171: else if ((pager = getenv("MANPAGER")) != NULL &&
172: (*pager != '\0'))
173: pager = check_pager(pager);
1.13 pjanzen 174: else if ((pager = getenv("PAGER")) != NULL && (*pager != '\0'))
1.1 deraadt 175: pager = check_pager(pager);
176: else
177: pager = _PATH_PAGER;
1.17 millert 178: }
1.1 deraadt 179:
180: /* Read the configuration file. */
181: config(conffile);
182:
1.39 schwarze 183: /*
184: * 1: If the user specified a section,
185: * use the section list from the configuration file.
186: * Otherwise, fall back to the default list or to an empty list.
187: */
188: if (sflag && (section = getlist(sflag)) == NULL)
189: errx(1, "unknown manual section `%s'", sflag);
190: else if (argv[1] && (section = getlist(*argv)) != NULL)
191: ++argv;
192:
193: searchlist = section;
194: if (searchlist == NULL)
195: searchlist = getlist("_default");
196: if (searchlist == NULL)
197: searchlist = addlist("_default");
1.1 deraadt 198:
199: /*
1.39 schwarze 200: * 2: If the user set the -M option or defined the MANPATH variable,
201: * clear what we have and take the user's list instead.
1.1 deraadt 202: */
203: if (p_path == NULL)
204: p_path = getenv("MANPATH");
205:
1.39 schwarze 206: if (p_path) {
207: clearlist(searchlist);
208: parse_path(searchlist, p_path);
1.1 deraadt 209: }
210:
211: /*
212: * 3: If the user set the -m option, insert the user's list before
1.39 schwarze 213: * whatever list we have.
1.1 deraadt 214: */
1.39 schwarze 215: if (p_add)
216: parse_path(searchlist, p_add);
217:
1.1 deraadt 218: /*
1.39 schwarze 219: * 4: Append the _subdir list where appropriate,
220: * and always append the machine type.
1.1 deraadt 221: */
1.39 schwarze 222: if (machine || (machine = getenv("MACHINE")))
223: for (p = machine; *p; ++p)
224: *p = tolower(*p);
225: else
226: machine = MACHINE;
227:
228: append_subdirs(searchlist, machine);
1.1 deraadt 229:
230: /*
231: * 5: Search for the files. Set up an interrupt handler, so the
232: * temporary files go away.
233: */
234: (void)signal(SIGINT, onsig);
235: (void)signal(SIGHUP, onsig);
236:
1.19 deraadt 237: sigemptyset(&blocksigs);
238: sigaddset(&blocksigs, SIGINT);
239: sigaddset(&blocksigs, SIGHUP);
240:
1.1 deraadt 241: memset(&pg, 0, sizeof(pg));
242: for (found = 0; *argv; ++argv)
1.39 schwarze 243: if (manual(*argv, searchlist, &pg))
1.1 deraadt 244: found = 1;
245:
246: /* 6: If nothing found, we're done. */
247: if (!found) {
1.41 deraadt 248: (void)cleanup(0);
1.1 deraadt 249: exit (1);
250: }
251:
252: /* 7: If it's simple, display it fast. */
253: if (f_cat) {
254: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
255: if (**ap == '\0')
256: continue;
257: cat(*ap);
258: }
1.41 deraadt 259: exit (cleanup(0));
1.1 deraadt 260: }
261: if (f_how) {
262: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
263: if (**ap == '\0')
264: continue;
265: how(*ap);
266: }
1.41 deraadt 267: exit(cleanup(0));
1.1 deraadt 268: }
269: if (f_where) {
270: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
271: if (**ap == '\0')
272: continue;
1.17 millert 273: (void)puts(*ap);
1.1 deraadt 274: }
1.41 deraadt 275: exit(cleanup(0));
1.1 deraadt 276: }
1.17 millert 277:
1.1 deraadt 278: /*
279: * 8: We display things in a single command; build a list of things
280: * to display.
281: */
282: for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
283: if (**ap == '\0')
284: continue;
285: len += strlen(*ap) + 1;
286: }
287: if ((cmd = malloc(len)) == NULL) {
288: warn(NULL);
1.41 deraadt 289: (void)cleanup(0);
1.1 deraadt 290: exit(1);
291: }
292: p = cmd;
293: len = strlen(pager);
1.17 millert 294: memcpy(p, pager, len);
1.1 deraadt 295: p += len;
296: *p++ = ' ';
297: for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
298: if (**ap == '\0')
299: continue;
300: len = strlen(*ap);
1.17 millert 301: memcpy(p, *ap, len);
1.1 deraadt 302: p += len;
303: *p++ = ' ';
304: }
1.12 deraadt 305: *--p = '\0';
1.1 deraadt 306:
307: /* Use system(3) in case someone's pager is "pager arg1 arg2". */
308: (void)system(cmd);
309:
1.41 deraadt 310: exit(cleanup(0));
1.39 schwarze 311: }
312:
313: /*
314: * clearlist --
315: * Remove all entries from a list,
316: * but leave the list header intact.
317: */
318: static void
319: clearlist(TAG *t)
320: {
321: ENTRY *e;
322:
323: while ((e = TAILQ_FIRST(&t->list)) != NULL) {
324: free(e->s);
325: TAILQ_REMOVE(&t->list, e, q);
326: free(e);
327: }
328: }
329:
330: /*
331: * parse_path --
332: * Split the -M or -m argument or the MANPATH variable at colons,
333: * and insert the parts into the searchlist.
334: */
335: static void
336: parse_path(TAG *t, char *path)
337: {
338: ENTRY *eplast = NULL, *ep;
339: char *p, *slashp;
340:
341: while ((p = strsep(&path, ":")) != NULL) {
342: if ((ep = malloc(sizeof(ENTRY))) == NULL)
343: err(1, NULL);
344:
345: /*
346: * Bring section specific paths to the same format as
347: * used in the configuration file, ending in /{cat,man}N.
348: */
349: if (section) {
350: slashp = p[strlen(p) - 1] == '/' ? "" : "/";
351: (void)snprintf(gbuf, sizeof(gbuf),
352: "%s%s{cat,man}%s", p, slashp, t->s);
353: if ((ep->s = strdup(gbuf)) == NULL)
354: err(1, NULL);
355: }
356:
357: /* Without a section, subdirs will be appended later. */
358: else
359: if ((ep->s = strdup(p)) == NULL)
360: err(1, NULL);
361:
362: /*
363: * Even in case of -M, inserting in front is fine:
364: * We have just cleared the list.
365: */
366: if (eplast)
367: TAILQ_INSERT_AFTER(&t->list, eplast, ep, q);
368: else
369: TAILQ_INSERT_HEAD(&t->list, ep, q);
370: eplast = ep;
371: }
372: }
373:
374: /*
375: * append_subdirs --
376: * Iterate the searchlist and append section and machine
377: * subdirectories as needed.
378: */
379: static void
380: append_subdirs(TAG *t, const char *machine)
381: {
382: TAG *tsub;
383: ENTRY *eold, *elast, *enew, *esub;
384: char *slashp;
385:
386: eold = elast = TAILQ_FIRST(&t->list);
387: while (eold) {
388:
389: /*
390: * Section subdirectories *not* ending in a slash
391: * only get the machine suffix: They already had
392: * the {cat,man}N part in the configuration file
393: * or got it in parse_path().
394: */
395: if (section && eold->s[strlen(eold->s)-1] != '/') {
396: (void)snprintf(gbuf, sizeof(gbuf), "%s{/%s,}",
397: eold->s, machine);
398: free(eold->s);
399: if ((eold->s = strdup(gbuf)) == NULL)
400: err(1, NULL);
401: eold = elast = TAILQ_NEXT(eold, q);
402: continue;
403: }
404:
405: /*
406: * Without a section, expand each entry using the
407: * subdir list, then drop the original entry.
408: */
409: esub = (tsub = getlist("_subdir")) == NULL ?
410: NULL : TAILQ_FIRST(&tsub->list);
411: while (esub) {
412: slashp = eold->s[strlen(eold->s)-1] == '/' ? "" : "/";
413: (void)snprintf(gbuf, sizeof(gbuf), "%s%s%s{/%s,}",
414: eold->s, slashp, esub->s, machine);
415: if ((enew = malloc(sizeof(ENTRY))) == NULL ||
416: (enew->s = strdup(gbuf)) == NULL)
417: err(1, NULL);
418: TAILQ_INSERT_AFTER(&t->list, elast, enew, q);
419: elast = enew;
420: esub = TAILQ_NEXT(esub, q);
421: }
422: elast = TAILQ_NEXT(elast, q);
423: TAILQ_REMOVE(&t->list, eold, q);
424: eold = elast;
425: }
1.1 deraadt 426: }
427:
428: /*
429: * manual --
430: * Search the manuals for the pages.
431: */
432: static int
1.26 deraadt 433: manual(char *page, TAG *tag, glob_t *pg)
1.1 deraadt 434: {
435: ENTRY *ep, *e_sufp, *e_tag;
436: TAG *missp, *sufp;
437: int anyfound, cnt, found;
1.6 deraadt 438: char *p, buf[MAXPATHLEN];
1.1 deraadt 439:
440: anyfound = 0;
441: buf[0] = '*';
442:
443: /* For each element in the list... */
1.29 otto 444: e_tag = tag == NULL ? NULL : TAILQ_FIRST(&tag->list);
445: for (; e_tag != NULL; e_tag = TAILQ_NEXT(e_tag, q)) {
1.1 deraadt 446: (void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
447: if (glob(buf,
448: GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
449: NULL, pg)) {
450: warn("globbing");
1.41 deraadt 451: (void)cleanup(0);
1.1 deraadt 452: exit(1);
453: }
454: if (pg->gl_matchc == 0)
455: continue;
456:
457: /* Find out if it's really a man page. */
458: for (cnt = pg->gl_pathc - pg->gl_matchc;
459: cnt < pg->gl_pathc; ++cnt) {
460:
1.42 ! schwarze 461: if (!f_all)
! 462: check_companion(pg->gl_pathv + cnt);
! 463:
1.1 deraadt 464: /*
465: * Try the _suffix key words first.
466: *
467: * XXX
468: * Older versions of man.conf didn't have the suffix
469: * key words, it was assumed that everything was a .0.
470: * We just test for .0 first, it's fast and probably
471: * going to hit.
472: */
473: (void)snprintf(buf, sizeof(buf), "*/%s.0", page);
474: if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
475: goto next;
476:
477: e_sufp = (sufp = getlist("_suffix")) == NULL ?
1.29 otto 478: NULL : TAILQ_FIRST(&sufp->list);
1.1 deraadt 479: for (found = 0;
1.29 otto 480: e_sufp != NULL; e_sufp = TAILQ_NEXT(e_sufp, q)) {
1.1 deraadt 481: (void)snprintf(buf,
482: sizeof(buf), "*/%s%s", page, e_sufp->s);
483: if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
484: found = 1;
485: break;
486: }
487: }
488: if (found)
489: goto next;
490:
491: /* Try the _build key words next. */
492: e_sufp = (sufp = getlist("_build")) == NULL ?
1.29 otto 493: NULL : TAILQ_FIRST(&sufp->list);
1.1 deraadt 494: for (found = 0;
1.29 otto 495: e_sufp != NULL; e_sufp = TAILQ_NEXT(e_sufp, q)) {
1.1 deraadt 496: for (p = e_sufp->s;
497: *p != '\0' && !isspace(*p); ++p);
498: if (*p == '\0')
499: continue;
500: *p = '\0';
501: (void)snprintf(buf,
502: sizeof(buf), "*/%s%s", page, e_sufp->s);
503: if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
504: if (!f_where)
505: build_page(p + 1,
506: &pg->gl_pathv[cnt]);
507: *p = ' ';
508: found = 1;
509: break;
510: }
511: *p = ' ';
512: }
513: if (found) {
514: next: anyfound = 1;
515: if (!f_all) {
516: /* Delete any other matches. */
517: while (++cnt< pg->gl_pathc)
518: pg->gl_pathv[cnt] = "";
519: break;
520: }
521: continue;
522: }
523:
524: /* It's not a man page, forget about it. */
525: pg->gl_pathv[cnt] = "";
526: }
527:
528: if (anyfound && !f_all)
529: break;
530: }
531:
532: /* If not found, enter onto the missing list. */
533: if (!anyfound) {
1.19 deraadt 534: sigset_t osigs;
535:
536: sigprocmask(SIG_BLOCK, &blocksigs, &osigs);
537:
1.1 deraadt 538: if ((missp = getlist("_missing")) == NULL)
539: missp = addlist("_missing");
540: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
541: (ep->s = strdup(page)) == NULL) {
542: warn(NULL);
1.41 deraadt 543: (void)cleanup(0);
1.1 deraadt 544: exit(1);
545: }
546: TAILQ_INSERT_TAIL(&missp->list, ep, q);
1.19 deraadt 547: sigprocmask(SIG_SETMASK, &osigs, NULL);
1.1 deraadt 548: }
549: return (anyfound);
1.42 ! schwarze 550: }
! 551:
! 552: /*
! 553: * check_companion --
! 554: * Check for a companion [un]formatted page
! 555: * and use the newer one of the two.
! 556: */
! 557: static void
! 558: check_companion(char **orig) {
! 559: struct stat sb_orig, sb_comp;
! 560: char *p, *pext, comp[MAXPATHLEN];
! 561: size_t len;
! 562: int found;
! 563:
! 564: len = strlcpy(comp, *orig, sizeof(comp));
! 565: /* The minus 2 avoids a buffer overrun in case of a trailing dot. */
! 566: p = comp + len - 2;
! 567:
! 568: /* Locate the file name extension. */
! 569: while (p > comp && *p != '.' && *p != '/')
! 570: p--;
! 571: if (*p != '.')
! 572: return;
! 573: pext = p + 1;
! 574:
! 575: /* Search for slashes. */
! 576: for (found = 0; 1; p--) {
! 577:
! 578: /* Did not find /{cat,man}. */
! 579: if (p == comp)
! 580: return;
! 581:
! 582: /* Pass over one slash, the one before "page". */
! 583: if (*p != '/' || !found++)
! 584: continue;
! 585:
! 586: /* Rewrite manN/page.N <-> catN/page.0. */
! 587: if (!strncmp(p+1, "man", 3)) {
! 588: memcpy(++p, "cat", 3);
! 589: *pext++ = '0';
! 590: break;
! 591: } else if (!strncmp(p+1, "cat", 3)) {
! 592: memcpy(++p, "man", 3);
! 593: p += 3;
! 594: while (*p != '/' && pext < comp + sizeof(comp) - 1)
! 595: *pext++ = *p++;
! 596: break;
! 597:
! 598: /* Accept one architecture subdir, but not more. */
! 599: } else if (found > 2)
! 600: return;
! 601: }
! 602: *pext = '\0';
! 603:
! 604: /* Check whether both files exist. */
! 605: if (stat(*orig, &sb_orig) || stat(comp, &sb_comp))
! 606: return;
! 607:
! 608: /* No action if the companion file is older. */
! 609: if (sb_orig.st_mtim.tv_sec > sb_comp.st_mtim.tv_sec || (
! 610: sb_orig.st_mtim.tv_sec == sb_comp.st_mtim.tv_sec &&
! 611: sb_orig.st_mtim.tv_nsec > sb_comp.st_mtim.tv_nsec))
! 612: return;
! 613:
! 614: /* The companion file is newer, use it. */
! 615: free(*orig);
! 616: if ((p = strdup(comp)) == NULL) {
! 617: warn(NULL);
! 618: (void)cleanup(0);
! 619: exit(1);
! 620: }
! 621: *orig = p;
1.1 deraadt 622: }
623:
1.17 millert 624: /*
1.1 deraadt 625: * build_page --
626: * Build a man page for display.
627: */
628: static void
1.21 deraadt 629: build_page(char *fmt, char **pathp)
1.1 deraadt 630: {
631: ENTRY *ep;
632: TAG *intmpp;
633: int fd, n;
634: char *p, *b;
1.4 deraadt 635: char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
1.19 deraadt 636: sigset_t osigs;
1.1 deraadt 637:
638: /*
1.17 millert 639: * Historically man chdir'd to the root of the man tree.
1.1 deraadt 640: * This was used in man pages that contained relative ".so"
641: * directives (including other man pages for command aliases etc.)
642: * It even went one step farther, by examining the first line
643: * of the man page and parsing the .so filename so it would
644: * make hard(?) links to the cat'ted man pages for space savings.
645: * (We don't do that here, but we could).
646: */
1.17 millert 647:
1.1 deraadt 648: /* copy and find the end */
649: for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
650: continue;
1.17 millert 651:
1.1 deraadt 652: /* skip the last two path components, page name and man[n] */
653: for (--b, n = 2; b != buf; b--)
654: if (*b == '/')
655: if (--n == 0) {
656: *b = '\0';
657: (void) chdir(buf);
658: }
659:
660:
661: /* Add a remove-when-done list. */
1.19 deraadt 662: sigprocmask(SIG_BLOCK, &blocksigs, &osigs);
1.1 deraadt 663: if ((intmpp = getlist("_intmp")) == NULL)
664: intmpp = addlist("_intmp");
1.19 deraadt 665: sigprocmask(SIG_SETMASK, &osigs, NULL);
1.1 deraadt 666:
667: /* Move to the printf(3) format string. */
1.16 deraadt 668: for (; *fmt && isspace(*fmt); ++fmt)
669: ;
1.1 deraadt 670:
671: /*
672: * Get a temporary file and build a version of the file
673: * to display. Replace the old file name with the new one.
674: */
1.18 deraadt 675: (void)strlcpy(tpath, _PATH_TMPFILE, sizeof(tpath));
1.1 deraadt 676: if ((fd = mkstemp(tpath)) == -1) {
677: warn("%s", tpath);
1.41 deraadt 678: (void)cleanup(0);
1.1 deraadt 679: exit(1);
680: }
681: (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
682: (void)snprintf(cmd, sizeof(cmd), buf, *pathp);
683: (void)system(cmd);
684: (void)close(fd);
685: if ((*pathp = strdup(tpath)) == NULL) {
686: warn(NULL);
1.41 deraadt 687: (void)cleanup(0);
1.1 deraadt 688: exit(1);
689: }
690:
691: /* Link the built file into the remove-when-done list. */
692: if ((ep = malloc(sizeof(ENTRY))) == NULL) {
693: warn(NULL);
1.41 deraadt 694: (void)cleanup(0);
1.1 deraadt 695: exit(1);
696: }
697: ep->s = *pathp;
1.41 deraadt 698:
699: sigprocmask(SIG_BLOCK, &blocksigs, &osigs);
1.1 deraadt 700: TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
1.41 deraadt 701: sigprocmask(SIG_SETMASK, &osigs, NULL);
1.1 deraadt 702: }
703:
704: /*
705: * how --
706: * display how information
707: */
708: static void
1.21 deraadt 709: how(char *fname)
1.1 deraadt 710: {
711: FILE *fp;
712:
713: int lcnt, print;
714: char *p, buf[256];
715:
716: if (!(fp = fopen(fname, "r"))) {
717: warn("%s", fname);
1.41 deraadt 718: (void)cleanup(0);
1.1 deraadt 719: exit (1);
720: }
721: #define S1 "SYNOPSIS"
722: #define S2 "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
723: for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
724: if (!strncmp(buf, S1, sizeof(S1) - 1) ||
725: !strncmp(buf, S2, sizeof(S2) - 1)) {
726: print = 1;
727: continue;
1.34 otto 728: } else if (print) {
729: char *p = buf;
730: int allcaps = 0;
731:
732: while (*p) {
733: if (!allcaps && isalpha(*p))
734: allcaps = 1;
735: if (isalpha(*p) && !isupper(*p)) {
736: allcaps = 0;
737: break;
738: }
739: p++;
740: }
741: if (allcaps) {
742: (void)fclose(fp);
743: return;
744: }
1.32 jasper 745: }
1.1 deraadt 746: if (!print)
747: continue;
748: if (*buf == '\n')
749: ++lcnt;
750: else {
1.17 millert 751: while (lcnt) {
752: --lcnt;
1.1 deraadt 753: (void)putchar('\n');
1.17 millert 754: }
755: for (p = buf; isspace(*p); ++p)
756: ;
1.1 deraadt 757: (void)fputs(p, stdout);
758: }
759: }
760: (void)fclose(fp);
761: }
762:
763: /*
764: * cat --
765: * cat out the file
766: */
767: static void
1.21 deraadt 768: cat(char *fname)
1.1 deraadt 769: {
770: int fd, n;
771: char buf[2048];
772:
773: if ((fd = open(fname, O_RDONLY, 0)) < 0) {
774: warn("%s", fname);
1.41 deraadt 775: (void)cleanup(0);
1.1 deraadt 776: exit(1);
777: }
778: while ((n = read(fd, buf, sizeof(buf))) > 0)
779: if (write(STDOUT_FILENO, buf, n) != n) {
780: warn("write");
1.41 deraadt 781: (void)cleanup(0);
1.1 deraadt 782: exit (1);
783: }
784: if (n == -1) {
785: warn("read");
1.41 deraadt 786: (void)cleanup(0);
1.1 deraadt 787: exit(1);
788: }
789: (void)close(fd);
790: }
791:
792: /*
793: * check_pager --
794: * check the user supplied page information
795: */
796: static char *
1.21 deraadt 797: check_pager(char *name)
1.1 deraadt 798: {
799: char *p, *save;
800:
801: /*
802: * if the user uses "more", we make it "more -s"; watch out for
1.17 millert 803: * PAGER = "mypager /usr/bin/more"
1.1 deraadt 804: */
1.16 deraadt 805: for (p = name; *p && !isspace(*p); ++p)
806: ;
807: for (; p > name && *p != '/'; --p)
808: ;
1.1 deraadt 809: if (p != name)
810: ++p;
811:
812: /* make sure it's "more", not "morex" */
1.27 millert 813: if (!strncmp(p, "more", 4) && (p[4] == '\0' || isspace(p[4]))){
1.1 deraadt 814: save = name;
815: /* allocate space to add the "-s" */
1.31 deraadt 816: if (asprintf(&name, "%s -s", save) == -1)
817: err(1, "asprintf");
1.1 deraadt 818: }
819: return(name);
820: }
821:
822: /*
823: * jump --
824: * strip out flag argument and jump
825: */
826: static void
1.21 deraadt 827: jump(char **argv, char *flag, char *name)
1.1 deraadt 828: {
829: char **arg;
830:
831: argv[0] = name;
832: for (arg = argv + 1; *arg; ++arg)
833: if (!strcmp(*arg, flag))
834: break;
835: for (; *arg; ++arg)
836: arg[0] = arg[1];
837: execvp(name, argv);
838: (void)fprintf(stderr, "%s: Command not found.\n", name);
839: exit(1);
840: }
841:
1.17 millert 842: /*
1.1 deraadt 843: * onsig --
844: * If signaled, delete the temporary files.
845: */
846: static void
1.21 deraadt 847: onsig(int signo)
1.1 deraadt 848: {
1.41 deraadt 849: (void)cleanup(1);
1.1 deraadt 850:
851: (void)signal(signo, SIG_DFL);
852: (void)kill(getpid(), signo);
853:
854: /* NOTREACHED */
1.19 deraadt 855: _exit(1);
1.1 deraadt 856: }
857:
858: /*
859: * cleanup --
860: * Clean up temporary files, show any error messages.
861: */
862: static int
1.41 deraadt 863: cleanup(int insig)
1.1 deraadt 864: {
865: TAG *intmpp, *missp;
866: ENTRY *ep;
1.41 deraadt 867: int rval = 0;
1.1 deraadt 868:
1.41 deraadt 869: if (insig == 0) {
870: ep = (missp = getlist("_missing")) == NULL ?
871: NULL : TAILQ_FIRST(&missp->list);
872: if (ep != NULL)
873: for (; ep != NULL; ep = TAILQ_NEXT(ep, q)) {
874: if (section)
875: warnx("no entry for %s in "
876: "section %s of the manual.",
877: ep->s, section->s);
878: else
879: warnx("no entry for %s in the manual.",
880: ep->s);
881: rval = 1;
882: }
883: }
1.1 deraadt 884:
885: ep = (intmpp = getlist("_intmp")) == NULL ?
1.29 otto 886: NULL : TAILQ_FIRST(&intmpp->list);
887: for (; ep != NULL; ep = TAILQ_NEXT(ep, q))
1.1 deraadt 888: (void)unlink(ep->s);
889: return (rval);
890: }
891:
892: /*
893: * usage --
894: * print usage message and die
895: */
896: static void
1.21 deraadt 897: usage(void)
1.1 deraadt 898: {
1.8 millert 899: (void)fprintf(stderr, "usage: %s [-achw] [-C file] [-M path] [-m path] "
1.35 sobrado 900: "[-S subsection] [-s section]\n\t [section] name ...\n",
1.28 jmc 901: __progname);
1.36 jmc 902: (void)fprintf(stderr, " %s -f command ...\n", __progname);
903: (void)fprintf(stderr, " %s -k keyword ...\n", __progname);
1.1 deraadt 904: exit(1);
905: }