2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static char copyright[] =
36 "@(#) Copyright (c) 1987, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)man.c 8.16 (Berkeley) 4/16/94";
44 #include <sys/param.h>
45 #include <sys/queue.h>
60 #include "pathnames.h"
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));
81 TAG *defp, *defnewp, *section, *sectnewp, *subp;
82 ENTRY *e_defp, *e_sectp, *e_subp, *ep;
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];
90 conffile = p_add = p_path = NULL;
91 while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:w")) != EOF)
100 case '-': /* Deprecated. */
110 case 'P': /* Backward compatibility. */
114 * The -f and -k options are backward compatible,
115 * undocumented ways of calling whatis(1) and apropos(1).
118 jump(argv, "-f", "whatis");
121 jump(argv, "-k", "apropos");
136 if (!f_cat && !f_how && !f_where)
139 else if ((pager = getenv("PAGER")) != NULL)
140 pager = check_pager(pager);
144 /* Read the configuration file. */
147 /* Get the machine type. */
148 if ((machine = getenv("MACHINE")) == NULL)
151 /* If there's no _default list, create an empty one. */
152 if ((defp = getlist("_default")) == NULL)
153 defp = addlist("_default");
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.
161 p_path = getenv("MANPATH");
162 if (p_path != NULL) {
163 while ((e_defp = defp->list.tqh_first) != NULL) {
165 TAILQ_REMOVE(&defp->list, e_defp, q);
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)
178 TAILQ_INSERT_TAIL(&defp->list, ep, q);
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.
189 else if ((section = getlist(*argv)) != NULL)
191 if (p_path == NULL && section == NULL) {
192 defnewp = addlist("_default_new");
194 defp->list.tqh_first == NULL ? NULL : defp->list.tqh_first;
195 for (; e_defp != NULL; e_defp = e_defp->q.tqe_next) {
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)
206 TAILQ_INSERT_TAIL(&defnewp->list, ep, q);
209 defp = getlist("_default");
210 while ((e_defp = defp->list.tqh_first) != NULL) {
212 TAILQ_REMOVE(&defp->list, e_defp, q);
215 TAILQ_REMOVE(&head, defp, q);
216 defnewp = getlist("_default_new");
218 defnewp->s = "_default";
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
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)
238 TAILQ_INSERT_HEAD(&defp->list, ep, q);
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
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)
258 TAILQ_INSERT_TAIL(§newp->list, ep, q);
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)
269 TAILQ_INSERT_TAIL(§newp->list, ep, q);
272 sectnewp->s = section->s;
274 TAILQ_REMOVE(&head, section, q);
278 * 5: Search for the files. Set up an interrupt handler, so the
279 * temporary files go away.
281 (void)signal(SIGINT, onsig);
282 (void)signal(SIGHUP, onsig);
284 memset(&pg, 0, sizeof(pg));
285 for (found = 0; *argv; ++argv)
286 if (manual(*argv, defp, &pg))
289 /* 6: If nothing found, we're done. */
295 /* 7: If it's simple, display it fast. */
297 for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
305 for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
313 for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
316 (void)printf("%s\n", *ap);
322 * 8: We display things in a single command; build a list of things
325 for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
328 len += strlen(*ap) + 1;
330 if ((cmd = malloc(len)) == NULL) {
337 memmove(p, pager, len);
340 for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
344 memmove(p, *ap, len);
350 /* Use system(3) in case someone's pager is "pager arg1 arg2". */
358 * Search the manuals for the pages.
361 manual(page, tag, pg)
366 ENTRY *ep, *e_sufp, *e_tag;
368 int anyfound, cnt, found;
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);
379 GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
385 if (pg->gl_matchc == 0)
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) {
393 * Try the _suffix key words first.
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
401 (void)snprintf(buf, sizeof(buf), "*/%s.0", page);
402 if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
405 e_sufp = (sufp = getlist("_suffix")) == NULL ?
406 NULL : sufp->list.tqh_first;
408 e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
410 sizeof(buf), "*/%s%s", page, e_sufp->s);
411 if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
419 /* Try the _build key words next. */
420 e_sufp = (sufp = getlist("_build")) == NULL ?
421 NULL : sufp->list.tqh_first;
423 e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
425 *p != '\0' && !isspace(*p); ++p);
430 sizeof(buf), "*/%s%s", page, e_sufp->s);
431 if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
444 /* Delete any other matches. */
445 while (++cnt< pg->gl_pathc)
446 pg->gl_pathv[cnt] = "";
452 /* It's not a man page, forget about it. */
453 pg->gl_pathv[cnt] = "";
456 if (anyfound && !f_all)
460 /* If not found, enter onto the missing list. */
462 if ((missp = getlist("_missing")) == NULL)
463 missp = addlist("_missing");
464 if ((ep = malloc(sizeof(ENTRY))) == NULL ||
465 (ep->s = strdup(page)) == NULL) {
470 TAILQ_INSERT_TAIL(&missp->list, ep, q);
477 * Build a man page for display.
480 build_page(fmt, pathp)
487 char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[sizeof(_PATH_TMP)];
489 /* Let the user know this may take awhile. */
492 warnx("Formatting manual page...");
495 /* Add a remove-when-done list. */
496 if ((intmpp = getlist("_intmp")) == NULL)
497 intmpp = addlist("_intmp");
499 /* Move to the printf(3) format string. */
500 for (; *fmt && isspace(*fmt); ++fmt);
503 * Get a temporary file and build a version of the file
504 * to display. Replace the old file name with the new one.
506 (void)strcpy(tpath, _PATH_TMP);
507 if ((fd = mkstemp(tpath)) == -1) {
512 (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
513 (void)snprintf(cmd, sizeof(cmd), buf, *pathp);
516 if ((*pathp = strdup(tpath)) == NULL) {
522 /* Link the built file into the remove-when-done list. */
523 if ((ep = malloc(sizeof(ENTRY))) == NULL) {
529 TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
534 * display how information
545 if (!(fp = fopen(fname, "r"))) {
550 #define S1 "SYNOPSIS"
551 #define S2 "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
552 #define D1 "DESCRIPTION"
553 #define D2 "D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
554 for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
555 if (!strncmp(buf, S1, sizeof(S1) - 1) ||
556 !strncmp(buf, S2, sizeof(S2) - 1)) {
559 } else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
560 !strncmp(buf, D2, sizeof(D2) - 1))
569 for (p = buf; isspace(*p); ++p);
570 (void)fputs(p, stdout);
587 if ((fd = open(fname, O_RDONLY, 0)) < 0) {
592 while ((n = read(fd, buf, sizeof(buf))) > 0)
593 if (write(STDOUT_FILENO, buf, n) != n) {
608 * check the user supplied page information
617 * if the user uses "more", we make it "more -s"; watch out for
618 * PAGER = "mypager /usr/ucb/more"
620 for (p = name; *p && !isspace(*p); ++p);
621 for (; p > name && *p != '/'; --p);
625 /* make sure it's "more", not "morex" */
626 if (!strncmp(p, "more", 4) && (!p[4] || isspace(p[4]))){
628 /* allocate space to add the "-s" */
630 malloc((u_int)(strlen(save) + sizeof("-s") + 1))))
632 (void)sprintf(name, "%s %s", save, "-s");
639 * strip out flag argument and jump
642 jump(argv, flag, name)
643 char **argv, *flag, *name;
648 for (arg = argv + 1; *arg; ++arg)
649 if (!strcmp(*arg, flag))
654 (void)fprintf(stderr, "%s: Command not found.\n", name);
660 * If signaled, delete the temporary files.
668 (void)signal(signo, SIG_DFL);
669 (void)kill(getpid(), signo);
677 * Clean up temporary files, show any error messages.
687 ep = (missp = getlist("_missing")) == NULL ?
688 NULL : missp->list.tqh_first;
690 for (; ep != NULL; ep = ep->q.tqe_next) {
691 warnx("no entry for %s in the manual.", ep->s);
695 ep = (intmpp = getlist("_intmp")) == NULL ?
696 NULL : intmpp->list.tqh_first;
697 for (; ep != NULL; ep = ep->q.tqe_next)
704 * print usage message and die
709 (void)fprintf(stderr,
710 "usage: man [-achw] [-C file] [-M path] [-m path] [section] title ...\n");