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