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