Home | History | Annotate | Line # | Download | only in man
man.c revision 1.27.2.1
      1  1.27.2.1        he /*	$NetBSD: man.c,v 1.27.2.1 2002/11/03 13:49:13 he 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.27.2.1        he __RCSID("$NetBSD: man.c,v 1.27.2.1 2002/11/03 13:49:13 he 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.27.2.1        he #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.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.27      groo 	char *p, buf[MAXPATHLEN], *escpage, *eptr;
    401      1.27      groo 	static const char escglob[] = "\\~?*{}[]";
    402       1.1       cgd 
    403       1.1       cgd 	anyfound = 0;
    404       1.1       cgd 	buf[0] = '*';
    405       1.1       cgd 
    406      1.27      groo 	/*
    407      1.27      groo 	 * Fixup page which may contain glob(3) special characters, e.g.
    408      1.27      groo 	 * the famous "No man page for [" FAQ.
    409      1.27      groo 	 */
    410      1.27      groo 	if ((escpage = malloc((2 * strlen(page)) + 1)) == NULL) {
    411      1.27      groo 		warn("malloc");
    412      1.27      groo 		(void)cleanup();
    413      1.27      groo 		exit(1);
    414      1.27      groo 	}
    415      1.27      groo 
    416      1.27      groo 	p = page;
    417      1.27      groo 	eptr = escpage;
    418      1.27      groo 
    419      1.27      groo 	while (*p) {
    420      1.27      groo 		if (strchr(escglob, *p) != NULL) {
    421      1.27      groo 			*eptr++ = '\\';
    422      1.27      groo 			*eptr++ = *p++;
    423      1.27      groo 		} else
    424      1.27      groo 			*eptr++ = *p++;
    425      1.27      groo 	}
    426      1.27      groo 
    427      1.27      groo 	*eptr = '\0';
    428      1.27      groo 
    429       1.1       cgd 	/* For each element in the list... */
    430       1.1       cgd 	e_tag = tag == NULL ? NULL : tag->list.tqh_first;
    431       1.1       cgd 	for (; e_tag != NULL; e_tag = e_tag->q.tqe_next) {
    432      1.27      groo 		(void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, escpage);
    433      1.12    kleink 		if ((error = glob(buf,
    434      1.12    kleink 		    GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) {
    435      1.12    kleink 			if (error == GLOB_NOMATCH)
    436      1.12    kleink 				continue;
    437      1.12    kleink 			else {
    438      1.12    kleink 				warn("globbing");
    439      1.12    kleink 				(void)cleanup();
    440      1.12    kleink 				exit(1);
    441      1.12    kleink 			}
    442       1.1       cgd 		}
    443       1.1       cgd 		if (pg->gl_matchc == 0)
    444       1.1       cgd 			continue;
    445       1.1       cgd 
    446       1.1       cgd 		/* Find out if it's really a man page. */
    447       1.1       cgd 		for (cnt = pg->gl_pathc - pg->gl_matchc;
    448       1.1       cgd 		    cnt < pg->gl_pathc; ++cnt) {
    449       1.1       cgd 
    450      1.23  jdolecek 			if (pathsearch) {
    451      1.23  jdolecek 				p = strstr(pg->gl_pathv[cnt], pathsearch);
    452      1.23  jdolecek 				if (!p || strchr(p, '/') == NULL) {
    453      1.23  jdolecek 					pg->gl_pathv[cnt] = "";
    454      1.23  jdolecek 					continue;
    455      1.23  jdolecek 				}
    456      1.23  jdolecek 			}
    457      1.23  jdolecek 
    458       1.1       cgd 			/*
    459       1.1       cgd 			 * Try the _suffix key words first.
    460       1.1       cgd 			 *
    461       1.1       cgd 			 * XXX
    462       1.1       cgd 			 * Older versions of man.conf didn't have the suffix
    463       1.1       cgd 			 * key words, it was assumed that everything was a .0.
    464       1.1       cgd 			 * We just test for .0 first, it's fast and probably
    465       1.1       cgd 			 * going to hit.
    466       1.1       cgd 			 */
    467      1.27      groo 			(void)snprintf(buf, sizeof(buf), "*/%s.0", escpage);
    468       1.1       cgd 			if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
    469       1.3       cgd 				goto next;
    470       1.1       cgd 
    471       1.1       cgd 			e_sufp = (sufp = getlist("_suffix")) == NULL ?
    472       1.1       cgd 			    NULL : sufp->list.tqh_first;
    473       1.1       cgd 			for (found = 0;
    474       1.1       cgd 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
    475       1.1       cgd 				(void)snprintf(buf,
    476      1.27      groo 				     sizeof(buf), "*/%s%s", escpage,
    477      1.27      groo 				     e_sufp->s);
    478       1.1       cgd 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    479       1.1       cgd 					found = 1;
    480       1.1       cgd 					break;
    481       1.1       cgd 				}
    482       1.1       cgd 			}
    483       1.3       cgd 			if (found)
    484       1.3       cgd 				goto next;
    485       1.1       cgd 
    486       1.1       cgd 			/* Try the _build key words next. */
    487       1.1       cgd 			e_sufp = (sufp = getlist("_build")) == NULL ?
    488       1.1       cgd 			    NULL : sufp->list.tqh_first;
    489       1.1       cgd 			for (found = 0;
    490       1.1       cgd 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
    491       1.1       cgd 				for (p = e_sufp->s;
    492      1.15  christos 				    *p != '\0' && !isspace((unsigned char)*p); ++p);
    493       1.1       cgd 				if (*p == '\0')
    494       1.1       cgd 					continue;
    495       1.1       cgd 				*p = '\0';
    496       1.1       cgd 				(void)snprintf(buf,
    497      1.27      groo 				     sizeof(buf), "*/%s%s", escpage,
    498      1.27      groo 				     e_sufp->s);
    499       1.1       cgd 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    500       1.1       cgd 					if (!f_where)
    501       1.1       cgd 						build_page(p + 1,
    502       1.1       cgd 						    &pg->gl_pathv[cnt]);
    503       1.1       cgd 					*p = ' ';
    504       1.1       cgd 					found = 1;
    505       1.1       cgd 					break;
    506       1.1       cgd 				}
    507       1.1       cgd 				*p = ' ';
    508       1.1       cgd 			}
    509       1.1       cgd 			if (found) {
    510       1.3       cgd next:				anyfound = 1;
    511       1.3       cgd 				if (!f_all) {
    512       1.3       cgd 					/* Delete any other matches. */
    513       1.3       cgd 					while (++cnt< pg->gl_pathc)
    514       1.3       cgd 						pg->gl_pathv[cnt] = "";
    515       1.1       cgd 					break;
    516       1.3       cgd 				}
    517       1.1       cgd 				continue;
    518       1.1       cgd 			}
    519       1.1       cgd 
    520       1.1       cgd 			/* It's not a man page, forget about it. */
    521       1.1       cgd 			pg->gl_pathv[cnt] = "";
    522       1.1       cgd 		}
    523       1.1       cgd 
    524       1.1       cgd 		if (anyfound && !f_all)
    525       1.1       cgd 			break;
    526       1.1       cgd 	}
    527       1.1       cgd 
    528       1.1       cgd 	/* If not found, enter onto the missing list. */
    529       1.1       cgd 	if (!anyfound) {
    530       1.1       cgd 		if ((missp = getlist("_missing")) == NULL)
    531       1.1       cgd 			missp = addlist("_missing");
    532       1.1       cgd 		if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    533       1.1       cgd 		    (ep->s = strdup(page)) == NULL) {
    534       1.9     mikel 			warn("malloc");
    535       1.1       cgd 			(void)cleanup();
    536       1.1       cgd 			exit(1);
    537       1.1       cgd 		}
    538       1.1       cgd 		TAILQ_INSERT_TAIL(&missp->list, ep, q);
    539       1.1       cgd 	}
    540      1.27      groo 
    541      1.27      groo 	free(escpage);
    542       1.1       cgd 	return (anyfound);
    543       1.1       cgd }
    544       1.1       cgd 
    545       1.1       cgd /*
    546       1.1       cgd  * build_page --
    547       1.1       cgd  *	Build a man page for display.
    548       1.1       cgd  */
    549       1.1       cgd static void
    550       1.1       cgd build_page(fmt, pathp)
    551       1.1       cgd 	char *fmt, **pathp;
    552       1.1       cgd {
    553       1.1       cgd 	static int warned;
    554       1.1       cgd 	ENTRY *ep;
    555       1.1       cgd 	TAG *intmpp;
    556       1.4       jtc 	int fd, n;
    557       1.4       jtc 	char *p, *b;
    558      1.19    kleink 	char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
    559      1.19    kleink 	const char *tmpdir;
    560       1.1       cgd 
    561       1.1       cgd 	/* Let the user know this may take awhile. */
    562       1.1       cgd 	if (!warned) {
    563       1.1       cgd 		warned = 1;
    564       1.1       cgd 		warnx("Formatting manual page...");
    565       1.1       cgd 	}
    566       1.1       cgd 
    567       1.7       tls        /*
    568       1.7       tls         * Historically man chdir'd to the root of the man tree.
    569       1.7       tls         * This was used in man pages that contained relative ".so"
    570       1.7       tls         * directives (including other man pages for command aliases etc.)
    571       1.7       tls         * It even went one step farther, by examining the first line
    572       1.7       tls         * of the man page and parsing the .so filename so it would
    573       1.7       tls         * make hard(?) links to the cat'ted man pages for space savings.
    574       1.7       tls         * (We don't do that here, but we could).
    575       1.7       tls         */
    576       1.7       tls 
    577       1.7       tls        /* copy and find the end */
    578       1.7       tls        for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
    579       1.7       tls                continue;
    580       1.7       tls 
    581      1.10        tv 	/* skip the last two path components, page name and man[n] */
    582      1.10        tv 	for (--b, --p, n = 2; b != buf; b--, p--)
    583      1.10        tv 		if (*b == '/')
    584      1.10        tv 			if (--n == 0) {
    585      1.10        tv 				*b = '\0';
    586      1.10        tv 				(void) chdir(buf);
    587      1.10        tv 				p++;
    588      1.10        tv 				break;
    589      1.10        tv 			}
    590       1.4       jtc 
    591       1.4       jtc 
    592       1.1       cgd 	/* Add a remove-when-done list. */
    593       1.1       cgd 	if ((intmpp = getlist("_intmp")) == NULL)
    594       1.1       cgd 		intmpp = addlist("_intmp");
    595       1.1       cgd 
    596       1.1       cgd 	/* Move to the printf(3) format string. */
    597      1.15  christos 	for (; *fmt && isspace((unsigned char)*fmt); ++fmt)
    598      1.15  christos 		continue;
    599       1.1       cgd 
    600       1.1       cgd 	/*
    601       1.1       cgd 	 * Get a temporary file and build a version of the file
    602       1.1       cgd 	 * to display.  Replace the old file name with the new one.
    603       1.1       cgd 	 */
    604      1.19    kleink 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    605      1.19    kleink 		tmpdir = _PATH_TMP;
    606      1.19    kleink 	(void)snprintf(tpath, sizeof (tpath), "%s/%s", tmpdir, TMPFILE);
    607       1.1       cgd 	if ((fd = mkstemp(tpath)) == -1) {
    608       1.1       cgd 		warn("%s", tpath);
    609       1.1       cgd 		(void)cleanup();
    610       1.1       cgd 		exit(1);
    611       1.1       cgd 	}
    612       1.1       cgd 	(void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
    613      1.10        tv 	(void)snprintf(cmd, sizeof(cmd), buf, p);
    614       1.1       cgd 	(void)system(cmd);
    615       1.1       cgd 	(void)close(fd);
    616       1.1       cgd 	if ((*pathp = strdup(tpath)) == NULL) {
    617       1.9     mikel 		warn("malloc");
    618       1.1       cgd 		(void)cleanup();
    619       1.1       cgd 		exit(1);
    620       1.1       cgd 	}
    621       1.1       cgd 
    622       1.1       cgd 	/* Link the built file into the remove-when-done list. */
    623       1.1       cgd 	if ((ep = malloc(sizeof(ENTRY))) == NULL) {
    624       1.9     mikel 		warn("malloc");
    625       1.1       cgd 		(void)cleanup();
    626       1.1       cgd 		exit(1);
    627       1.1       cgd 	}
    628       1.1       cgd 	ep->s = *pathp;
    629       1.1       cgd 	TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
    630       1.1       cgd }
    631       1.1       cgd 
    632       1.1       cgd /*
    633       1.1       cgd  * how --
    634       1.1       cgd  *	display how information
    635       1.1       cgd  */
    636       1.1       cgd static void
    637       1.1       cgd how(fname)
    638       1.1       cgd 	char *fname;
    639       1.1       cgd {
    640       1.1       cgd 	FILE *fp;
    641       1.1       cgd 
    642       1.1       cgd 	int lcnt, print;
    643       1.1       cgd 	char *p, buf[256];
    644       1.1       cgd 
    645       1.1       cgd 	if (!(fp = fopen(fname, "r"))) {
    646       1.1       cgd 		warn("%s", fname);
    647       1.1       cgd 		(void)cleanup();
    648       1.1       cgd 		exit (1);
    649       1.1       cgd 	}
    650       1.1       cgd #define	S1	"SYNOPSIS"
    651       1.1       cgd #define	S2	"S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
    652       1.1       cgd #define	D1	"DESCRIPTION"
    653       1.1       cgd #define	D2	"D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
    654       1.1       cgd 	for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
    655       1.1       cgd 		if (!strncmp(buf, S1, sizeof(S1) - 1) ||
    656       1.1       cgd 		    !strncmp(buf, S2, sizeof(S2) - 1)) {
    657       1.1       cgd 			print = 1;
    658       1.1       cgd 			continue;
    659       1.1       cgd 		} else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
    660       1.1       cgd 		    !strncmp(buf, D2, sizeof(D2) - 1))
    661       1.1       cgd 			return;
    662       1.1       cgd 		if (!print)
    663       1.1       cgd 			continue;
    664       1.1       cgd 		if (*buf == '\n')
    665       1.1       cgd 			++lcnt;
    666       1.1       cgd 		else {
    667       1.1       cgd 			for(; lcnt; --lcnt)
    668       1.1       cgd 				(void)putchar('\n');
    669      1.15  christos 			for (p = buf; isspace((unsigned char)*p); ++p)
    670      1.15  christos 				continue;
    671       1.1       cgd 			(void)fputs(p, stdout);
    672       1.1       cgd 		}
    673       1.1       cgd 	}
    674       1.1       cgd 	(void)fclose(fp);
    675       1.1       cgd }
    676       1.1       cgd 
    677       1.1       cgd /*
    678       1.1       cgd  * cat --
    679       1.1       cgd  *	cat out the file
    680       1.1       cgd  */
    681       1.1       cgd static void
    682       1.1       cgd cat(fname)
    683       1.1       cgd 	char *fname;
    684       1.1       cgd {
    685       1.1       cgd 	int fd, n;
    686       1.1       cgd 	char buf[2048];
    687       1.1       cgd 
    688       1.1       cgd 	if ((fd = open(fname, O_RDONLY, 0)) < 0) {
    689       1.1       cgd 		warn("%s", fname);
    690       1.1       cgd 		(void)cleanup();
    691       1.1       cgd 		exit(1);
    692       1.1       cgd 	}
    693       1.1       cgd 	while ((n = read(fd, buf, sizeof(buf))) > 0)
    694       1.1       cgd 		if (write(STDOUT_FILENO, buf, n) != n) {
    695       1.1       cgd 			warn("write");
    696       1.1       cgd 			(void)cleanup();
    697       1.1       cgd 			exit (1);
    698       1.1       cgd 		}
    699       1.1       cgd 	if (n == -1) {
    700       1.1       cgd 		warn("read");
    701       1.1       cgd 		(void)cleanup();
    702       1.1       cgd 		exit(1);
    703       1.1       cgd 	}
    704       1.1       cgd 	(void)close(fd);
    705       1.1       cgd }
    706       1.1       cgd 
    707       1.1       cgd /*
    708       1.1       cgd  * check_pager --
    709       1.1       cgd  *	check the user supplied page information
    710       1.1       cgd  */
    711      1.23  jdolecek static const char *
    712       1.1       cgd check_pager(name)
    713      1.23  jdolecek 	const char *name;
    714       1.1       cgd {
    715      1.24   thorpej 	const char *p;
    716       1.1       cgd 
    717       1.1       cgd 	/*
    718       1.1       cgd 	 * if the user uses "more", we make it "more -s"; watch out for
    719       1.1       cgd 	 * PAGER = "mypager /usr/ucb/more"
    720       1.1       cgd 	 */
    721      1.15  christos 	for (p = name; *p && !isspace((unsigned char)*p); ++p)
    722      1.15  christos 		continue;
    723       1.1       cgd 	for (; p > name && *p != '/'; --p);
    724       1.1       cgd 	if (p != name)
    725       1.1       cgd 		++p;
    726       1.1       cgd 
    727       1.1       cgd 	/* make sure it's "more", not "morex" */
    728      1.15  christos 	if (!strncmp(p, "more", 4) && (!p[4] || isspace((unsigned char)p[4]))){
    729      1.23  jdolecek 		char *newname;
    730      1.24   thorpej 		(void)asprintf(&newname, "%s %s", p, "-s");
    731      1.23  jdolecek 		name = newname;
    732       1.1       cgd 	}
    733      1.23  jdolecek 
    734      1.23  jdolecek 	return (name);
    735       1.1       cgd }
    736       1.1       cgd 
    737       1.1       cgd /*
    738       1.1       cgd  * jump --
    739       1.1       cgd  *	strip out flag argument and jump
    740       1.1       cgd  */
    741       1.1       cgd static void
    742       1.1       cgd jump(argv, flag, name)
    743       1.1       cgd 	char **argv, *flag, *name;
    744       1.1       cgd {
    745       1.1       cgd 	char **arg;
    746       1.1       cgd 
    747       1.1       cgd 	argv[0] = name;
    748       1.1       cgd 	for (arg = argv + 1; *arg; ++arg)
    749       1.1       cgd 		if (!strcmp(*arg, flag))
    750       1.1       cgd 			break;
    751       1.1       cgd 	for (; *arg; ++arg)
    752       1.1       cgd 		arg[0] = arg[1];
    753       1.1       cgd 	execvp(name, argv);
    754       1.1       cgd 	(void)fprintf(stderr, "%s: Command not found.\n", name);
    755       1.1       cgd 	exit(1);
    756       1.1       cgd }
    757       1.1       cgd 
    758       1.1       cgd /*
    759       1.1       cgd  * onsig --
    760       1.1       cgd  *	If signaled, delete the temporary files.
    761       1.1       cgd  */
    762       1.1       cgd static void
    763       1.1       cgd onsig(signo)
    764       1.1       cgd 	int signo;
    765       1.1       cgd {
    766      1.18     itohy 	sigset_t set;
    767      1.18     itohy 
    768       1.1       cgd 	(void)cleanup();
    769       1.1       cgd 
    770       1.1       cgd 	(void)signal(signo, SIG_DFL);
    771      1.18     itohy 
    772      1.18     itohy 	/* unblock the signal */
    773      1.18     itohy 	sigemptyset(&set);
    774      1.18     itohy 	sigaddset(&set, signo);
    775      1.18     itohy 	sigprocmask(SIG_UNBLOCK, &set, (sigset_t *) NULL);
    776      1.18     itohy 
    777       1.1       cgd 	(void)kill(getpid(), signo);
    778       1.1       cgd 
    779       1.1       cgd 	/* NOTREACHED */
    780       1.1       cgd 	exit (1);
    781       1.1       cgd }
    782       1.1       cgd 
    783       1.1       cgd /*
    784       1.1       cgd  * cleanup --
    785       1.1       cgd  *	Clean up temporary files, show any error messages.
    786       1.1       cgd  */
    787       1.1       cgd static int
    788       1.1       cgd cleanup()
    789       1.1       cgd {
    790       1.1       cgd 	TAG *intmpp, *missp;
    791       1.1       cgd 	ENTRY *ep;
    792       1.1       cgd 	int rval;
    793       1.1       cgd 
    794       1.1       cgd 	rval = 0;
    795       1.1       cgd 	ep = (missp = getlist("_missing")) == NULL ?
    796       1.1       cgd 	    NULL : missp->list.tqh_first;
    797       1.1       cgd 	if (ep != NULL)
    798       1.1       cgd 		for (; ep != NULL; ep = ep->q.tqe_next) {
    799       1.1       cgd 			warnx("no entry for %s in the manual.", ep->s);
    800       1.1       cgd 			rval = 1;
    801       1.1       cgd 		}
    802       1.1       cgd 
    803       1.1       cgd 	ep = (intmpp = getlist("_intmp")) == NULL ?
    804       1.1       cgd 	    NULL : intmpp->list.tqh_first;
    805       1.1       cgd 	for (; ep != NULL; ep = ep->q.tqe_next)
    806       1.1       cgd 		(void)unlink(ep->s);
    807       1.1       cgd 	return (rval);
    808       1.1       cgd }
    809       1.1       cgd 
    810       1.1       cgd /*
    811       1.1       cgd  * usage --
    812       1.1       cgd  *	print usage message and die
    813       1.1       cgd  */
    814       1.1       cgd static void
    815       1.1       cgd usage()
    816       1.1       cgd {
    817      1.26       cgd 
    818      1.25    simonb 	(void)fprintf(stderr, "Usage: %s [-achw] [-C file] [-M path] [-m path]"
    819      1.26       cgd 	    "[-S srch] [[-s] section] title ...\n", getprogname());
    820       1.1       cgd 	exit(1);
    821       1.1       cgd }
    822