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