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