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