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