xref: /minix3/usr.bin/man/man.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: man.c,v 1.62 2014/08/14 15:31:12 apb Exp $	*/
20c3983b2SBen Gras 
30c3983b2SBen Gras /*
40c3983b2SBen Gras  * Copyright (c) 1987, 1993, 1994, 1995
50c3983b2SBen Gras  *	The Regents of the University of California.  All rights reserved.
60c3983b2SBen Gras  *
70c3983b2SBen Gras  * Redistribution and use in source and binary forms, with or without
80c3983b2SBen Gras  * modification, are permitted provided that the following conditions
90c3983b2SBen Gras  * are met:
100c3983b2SBen Gras  * 1. Redistributions of source code must retain the above copyright
110c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer.
120c3983b2SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
130c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer in the
140c3983b2SBen Gras  *    documentation and/or other materials provided with the distribution.
150c3983b2SBen Gras  * 3. Neither the name of the University nor the names of its contributors
160c3983b2SBen Gras  *    may be used to endorse or promote products derived from this software
170c3983b2SBen Gras  *    without specific prior written permission.
180c3983b2SBen Gras  *
190c3983b2SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
200c3983b2SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
210c3983b2SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
220c3983b2SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
230c3983b2SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
240c3983b2SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
250c3983b2SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
260c3983b2SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
270c3983b2SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
280c3983b2SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
290c3983b2SBen Gras  * SUCH DAMAGE.
300c3983b2SBen Gras  */
310c3983b2SBen Gras 
320c3983b2SBen Gras #include <sys/cdefs.h>
330c3983b2SBen Gras 
340c3983b2SBen Gras #ifndef lint
350c3983b2SBen Gras __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994, 1995\
360c3983b2SBen Gras  The Regents of the University of California.  All rights reserved.");
370c3983b2SBen Gras #endif /* not lint */
380c3983b2SBen Gras 
390c3983b2SBen Gras #ifndef lint
400c3983b2SBen Gras #if 0
410c3983b2SBen Gras static char sccsid[] = "@(#)man.c	8.17 (Berkeley) 1/31/95";
420c3983b2SBen Gras #else
43*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: man.c,v 1.62 2014/08/14 15:31:12 apb Exp $");
440c3983b2SBen Gras #endif
450c3983b2SBen Gras #endif /* not lint */
460c3983b2SBen Gras 
470c3983b2SBen Gras #include <sys/param.h>
480c3983b2SBen Gras #include <sys/queue.h>
49b9a6a72bSLionel Sambuc #include <sys/stat.h>
500c3983b2SBen Gras #include <sys/utsname.h>
510c3983b2SBen Gras 
520c3983b2SBen Gras #include <ctype.h>
530c3983b2SBen Gras #include <err.h>
540c3983b2SBen Gras #include <errno.h>
550c3983b2SBen Gras #include <fcntl.h>
560c3983b2SBen Gras #include <fnmatch.h>
570c3983b2SBen Gras #include <glob.h>
580c3983b2SBen Gras #include <signal.h>
590c3983b2SBen Gras #include <stdio.h>
600c3983b2SBen Gras #include <stdlib.h>
610c3983b2SBen Gras #include <string.h>
620c3983b2SBen Gras #include <unistd.h>
630c3983b2SBen Gras #include <util.h>
640c3983b2SBen Gras #include <locale.h>
650c3983b2SBen Gras 
660c3983b2SBen Gras #include "manconf.h"
670c3983b2SBen Gras #include "pathnames.h"
680c3983b2SBen Gras 
690c3983b2SBen Gras #ifndef MAN_DEBUG
700c3983b2SBen Gras #define MAN_DEBUG 0		/* debug path output */
710c3983b2SBen Gras #endif
720c3983b2SBen Gras 
730c3983b2SBen Gras /*
740c3983b2SBen Gras  * manstate: structure collecting the current global state so we can
750c3983b2SBen Gras  * easily identify it and pass it to helper functions in one arg.
760c3983b2SBen Gras  */
770c3983b2SBen Gras struct manstate {
780c3983b2SBen Gras 	/* command line flags */
790c3983b2SBen Gras 	int all;		/* -a: show all matches rather than first */
800c3983b2SBen Gras 	int cat;		/* -c: do not use a pager */
810c3983b2SBen Gras 	char *conffile;		/* -C: use alternate config file */
820c3983b2SBen Gras 	int how;		/* -h: show SYNOPSIS only */
830c3983b2SBen Gras 	char *manpath;		/* -M: alternate MANPATH */
840c3983b2SBen Gras 	char *addpath;		/* -m: add these dirs to front of manpath */
850c3983b2SBen Gras 	char *pathsearch;	/* -S: path of man must contain this string */
860c3983b2SBen Gras 	char *sectionname;	/* -s: limit search to a given man section */
870c3983b2SBen Gras 	int where;		/* -w: just show paths of all matching files */
88b9a6a72bSLionel Sambuc 	int getpath;	/* -p: print the path of directories containing man pages */
890c3983b2SBen Gras 
900c3983b2SBen Gras 	/* important tags from the config file */
910c3983b2SBen Gras 	TAG *defaultpath;	/* _default: default MANPATH */
920c3983b2SBen Gras 	TAG *subdirs;		/* _subdir: default subdir search list */
930c3983b2SBen Gras 	TAG *suffixlist;	/* _suffix: for files that can be cat()'d */
940c3983b2SBen Gras 	TAG *buildlist;		/* _build: for files that must be built */
950c3983b2SBen Gras 
960c3983b2SBen Gras 	/* tags for internal use */
970c3983b2SBen Gras 	TAG *intmp;		/* _intmp: tmp files we must cleanup */
980c3983b2SBen Gras 	TAG *missinglist;	/* _missing: pages we couldn't find */
990c3983b2SBen Gras 	TAG *mymanpath;		/* _new_path: final version of MANPATH */
1000c3983b2SBen Gras 	TAG *section;		/* <sec>: tag for m.sectionname */
1010c3983b2SBen Gras 
1020c3983b2SBen Gras 	/* other misc stuff */
1030c3983b2SBen Gras 	const char *pager;	/* pager to use */
10484d9c625SLionel Sambuc 	size_t pagerlen;	/* length of the above */
1050c3983b2SBen Gras 	const char *machine;	/* machine */
1060c3983b2SBen Gras 	const char *machclass;	/* machine class */
1070c3983b2SBen Gras };
1080c3983b2SBen Gras 
1090c3983b2SBen Gras /*
1100c3983b2SBen Gras  * prototypes
1110c3983b2SBen Gras  */
11284d9c625SLionel Sambuc static void	 build_page(const char *, char **, struct manstate *);
11384d9c625SLionel Sambuc static void	 cat(const char *);
1140c3983b2SBen Gras static const char	*check_pager(const char *);
1150c3983b2SBen Gras static int	 cleanup(void);
11684d9c625SLionel Sambuc static void	 how(const char *);
11784d9c625SLionel Sambuc static void	 jump(char **, const char *, const char *) __dead;
1180c3983b2SBen Gras static int	 manual(char *, struct manstate *, glob_t *);
11984d9c625SLionel Sambuc static void	 onsig(int) __dead;
12084d9c625SLionel Sambuc static void	 usage(void) __dead;
1210c3983b2SBen Gras static void	 addpath(struct manstate *, const char *, size_t, const char *);
1220c3983b2SBen Gras static const char *getclass(const char *);
123b9a6a72bSLionel Sambuc static void printmanpath(struct manstate *);
1240c3983b2SBen Gras 
1250c3983b2SBen Gras /*
1260c3983b2SBen Gras  * main function
1270c3983b2SBen Gras  */
1280c3983b2SBen Gras int
main(int argc,char ** argv)1290c3983b2SBen Gras main(int argc, char **argv)
1300c3983b2SBen Gras {
13184d9c625SLionel Sambuc 	static struct manstate m;
1320c3983b2SBen Gras 	int ch, abs_section, found;
1330c3983b2SBen Gras 	ENTRY *esubd, *epath;
1340c3983b2SBen Gras 	char *p, **ap, *cmd;
1350c3983b2SBen Gras 	size_t len;
1360c3983b2SBen Gras 	glob_t pg;
1370c3983b2SBen Gras 
1380c3983b2SBen Gras 	setprogname(argv[0]);
1390c3983b2SBen Gras 	setlocale(LC_ALL, "");
1400c3983b2SBen Gras 	/*
1410c3983b2SBen Gras 	 * parse command line...
1420c3983b2SBen Gras 	 */
143b9a6a72bSLionel Sambuc 	while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:ps:S:w")) != -1)
1440c3983b2SBen Gras 		switch (ch) {
1450c3983b2SBen Gras 		case 'a':
1460c3983b2SBen Gras 			m.all = 1;
1470c3983b2SBen Gras 			break;
1480c3983b2SBen Gras 		case 'C':
1490c3983b2SBen Gras 			m.conffile = optarg;
1500c3983b2SBen Gras 			break;
1510c3983b2SBen Gras 		case 'c':
1520c3983b2SBen Gras 		case '-':	/* XXX: '-' is a deprecated version of '-c' */
1530c3983b2SBen Gras 			m.cat = 1;
1540c3983b2SBen Gras 			break;
1550c3983b2SBen Gras 		case 'h':
1560c3983b2SBen Gras 			m.how = 1;
1570c3983b2SBen Gras 			break;
1580c3983b2SBen Gras 		case 'm':
1590c3983b2SBen Gras 			m.addpath = optarg;
1600c3983b2SBen Gras 			break;
1610c3983b2SBen Gras 		case 'M':
1620c3983b2SBen Gras 		case 'P':	/* -P for backward compatibility */
1630c3983b2SBen Gras 			m.manpath = strdup(optarg);
1640c3983b2SBen Gras 			break;
165b9a6a72bSLionel Sambuc 		case 'p':
166b9a6a72bSLionel Sambuc 			m.getpath = 1;
167b9a6a72bSLionel Sambuc 			break;
1680c3983b2SBen Gras 		/*
1690c3983b2SBen Gras 		 * The -f and -k options are backward compatible,
1700c3983b2SBen Gras 		 * undocumented ways of calling whatis(1) and apropos(1).
1710c3983b2SBen Gras 		 */
1720c3983b2SBen Gras 		case 'f':
1730c3983b2SBen Gras 			jump(argv, "-f", "whatis");
1740c3983b2SBen Gras 			/* NOTREACHED */
1750c3983b2SBen Gras 		case 'k':
1760c3983b2SBen Gras 			jump(argv, "-k", "apropos");
1770c3983b2SBen Gras 			/* NOTREACHED */
1780c3983b2SBen Gras 		case 's':
1790c3983b2SBen Gras 			if (m.sectionname != NULL)
1800c3983b2SBen Gras 				usage();
1810c3983b2SBen Gras 			m.sectionname = optarg;
1820c3983b2SBen Gras 			break;
1830c3983b2SBen Gras 		case 'S':
1840c3983b2SBen Gras 			m.pathsearch = optarg;
1850c3983b2SBen Gras 			break;
1860c3983b2SBen Gras 		case 'w':
1870c3983b2SBen Gras 			m.all = m.where = 1;
1880c3983b2SBen Gras 			break;
1890c3983b2SBen Gras 		case '?':
1900c3983b2SBen Gras 		default:
1910c3983b2SBen Gras 			usage();
1920c3983b2SBen Gras 		}
1930c3983b2SBen Gras 	argc -= optind;
1940c3983b2SBen Gras 	argv += optind;
1950c3983b2SBen Gras 
196b9a6a72bSLionel Sambuc 	if (!m.getpath && !argc)
1970c3983b2SBen Gras 		usage();
1980c3983b2SBen Gras 
1990c3983b2SBen Gras 	/*
2000c3983b2SBen Gras 	 * read the configuration file and collect any other information
2010c3983b2SBen Gras 	 * we will need (machine type, pager, section [if specified
2020c3983b2SBen Gras 	 * without '-s'], and MANPATH through the environment).
2030c3983b2SBen Gras 	 */
2040c3983b2SBen Gras 	config(m.conffile);    /* exits on error ... */
2050c3983b2SBen Gras 
2060c3983b2SBen Gras 	if ((m.machine = getenv("MACHINE")) == NULL) {
2070c3983b2SBen Gras 		struct utsname utsname;
2080c3983b2SBen Gras 
2090c3983b2SBen Gras 		if (uname(&utsname) == -1)
2100c3983b2SBen Gras 			err(EXIT_FAILURE, "uname");
2110c3983b2SBen Gras 		m.machine = utsname.machine;
2120c3983b2SBen Gras 	}
2130c3983b2SBen Gras 
2140c3983b2SBen Gras 	m.machclass = getclass(m.machine);
2150c3983b2SBen Gras 
2160c3983b2SBen Gras 	if (!m.cat && !m.how && !m.where) {  /* if we need a pager ... */
2170c3983b2SBen Gras 		if (!isatty(STDOUT_FILENO)) {
2180c3983b2SBen Gras 			m.cat = 1;
2190c3983b2SBen Gras 		} else {
2200c3983b2SBen Gras 			if ((m.pager = getenv("PAGER")) != NULL &&
2210c3983b2SBen Gras 			    m.pager[0] != '\0')
2220c3983b2SBen Gras 				m.pager = check_pager(m.pager);
2230c3983b2SBen Gras 			else
2240c3983b2SBen Gras 				m.pager = _PATH_PAGER;
2250c3983b2SBen Gras 			m.pagerlen = strlen(m.pager);
2260c3983b2SBen Gras 		}
2270c3983b2SBen Gras 	}
2280c3983b2SBen Gras 
2290c3983b2SBen Gras 	/* do we need to set m.section to a non-null value? */
2300c3983b2SBen Gras 	if (m.sectionname) {
2310c3983b2SBen Gras 
2320c3983b2SBen Gras 		m.section = gettag(m.sectionname, 0); /* -s must be a section */
2330c3983b2SBen Gras 		if (m.section == NULL)
2340c3983b2SBen Gras 			errx(EXIT_FAILURE, "unknown section: %s", m.sectionname);
2350c3983b2SBen Gras 
2360c3983b2SBen Gras 	} else if (argc > 1) {
2370c3983b2SBen Gras 
2380c3983b2SBen Gras 		m.section = gettag(*argv, 0);  /* might be a section? */
2390c3983b2SBen Gras 		if (m.section) {
2400c3983b2SBen Gras 			argv++;
2410c3983b2SBen Gras 			argc--;
2420c3983b2SBen Gras 		}
2430c3983b2SBen Gras 
2440c3983b2SBen Gras 	}
2450c3983b2SBen Gras 
2460c3983b2SBen Gras 	if (m.manpath == NULL)
2470c3983b2SBen Gras 		m.manpath = getenv("MANPATH"); /* note: -M overrides getenv */
2480c3983b2SBen Gras 
2490c3983b2SBen Gras 
2500c3983b2SBen Gras 	/*
2510c3983b2SBen Gras 	 * get default values from config file, plus create the tags we
2520c3983b2SBen Gras 	 * use for keeping internal state.  make sure all our mallocs
2530c3983b2SBen Gras 	 * go through.
2540c3983b2SBen Gras 	 */
2550c3983b2SBen Gras 	/* from cfg file */
2560c3983b2SBen Gras 	m.defaultpath = gettag("_default", 1);
2570c3983b2SBen Gras 	m.subdirs = gettag("_subdir", 1);
2580c3983b2SBen Gras 	m.suffixlist = gettag("_suffix", 1);
2590c3983b2SBen Gras 	m.buildlist = gettag("_build", 1);
2600c3983b2SBen Gras 	/* internal use */
2610c3983b2SBen Gras 	m.mymanpath = gettag("_new_path", 1);
2620c3983b2SBen Gras 	m.missinglist = gettag("_missing", 1);
2630c3983b2SBen Gras 	m.intmp = gettag("_intmp", 1);
2640c3983b2SBen Gras 	if (!m.defaultpath || !m.subdirs || !m.suffixlist || !m.buildlist ||
2650c3983b2SBen Gras 	    !m.mymanpath || !m.missinglist || !m.intmp)
2660c3983b2SBen Gras 		errx(EXIT_FAILURE, "malloc failed");
2670c3983b2SBen Gras 
2680c3983b2SBen Gras 	/*
2690c3983b2SBen Gras 	 * are we using a section whose elements are all absolute paths?
2700c3983b2SBen Gras 	 * (we only need to look at the first entry on the section list,
2710c3983b2SBen Gras 	 * as config() will ensure that any additional entries will match
2720c3983b2SBen Gras 	 * the first one.)
2730c3983b2SBen Gras 	 */
2740c3983b2SBen Gras 	abs_section = (m.section != NULL &&
2750c3983b2SBen Gras 		!TAILQ_EMPTY(&m.section->entrylist) &&
2760c3983b2SBen Gras 	    		*(TAILQ_FIRST(&m.section->entrylist)->s) == '/');
2770c3983b2SBen Gras 
2780c3983b2SBen Gras 	/*
2790c3983b2SBen Gras 	 * now that we have all the data we need, we must determine the
2800c3983b2SBen Gras 	 * manpath we are going to use to find the requested entries using
2810c3983b2SBen Gras 	 * the following steps...
2820c3983b2SBen Gras 	 *
2830c3983b2SBen Gras 	 * [1] if the user specified a section and that section's elements
2840c3983b2SBen Gras 	 *     from the config file are all absolute paths, then we override
2850c3983b2SBen Gras 	 *     defaultpath and -M/MANPATH with the section's absolute paths.
2860c3983b2SBen Gras 	 */
2870c3983b2SBen Gras 	if (abs_section) {
2880c3983b2SBen Gras 		m.manpath = NULL;	   	/* ignore -M/MANPATH */
2890c3983b2SBen Gras 		m.defaultpath = m.section;	/* overwrite _default path */
2900c3983b2SBen Gras 		m.section = NULL;		/* promoted to defaultpath */
2910c3983b2SBen Gras 	}
2920c3983b2SBen Gras 
2930c3983b2SBen Gras 	/*
2940c3983b2SBen Gras 	 * [2] section can now only be non-null if the user asked for
2950c3983b2SBen Gras 	 *     a section and that section's elements did not have
2960c3983b2SBen Gras          *     absolute paths.  in this case we use the section's
2970c3983b2SBen Gras 	 *     elements to override _subdir from the config file.
2980c3983b2SBen Gras 	 *
2990c3983b2SBen Gras 	 * after this step, we are done processing "m.section"...
3000c3983b2SBen Gras 	 */
3010c3983b2SBen Gras 	if (m.section)
3020c3983b2SBen Gras 		m.subdirs = m.section;
3030c3983b2SBen Gras 
3040c3983b2SBen Gras 	/*
3050c3983b2SBen Gras 	 * [3] we need to setup the path we want to use (m.mymanpath).
3060c3983b2SBen Gras 	 *     if the user gave us a path (m.manpath) use it, otherwise
3070c3983b2SBen Gras 	 *     go with the default.   in either case we need to append
3080c3983b2SBen Gras 	 *     the subdir and machine spec to each element of the path.
3090c3983b2SBen Gras 	 *
3100c3983b2SBen Gras 	 *     for absolute section paths that come from the config file,
3110c3983b2SBen Gras 	 *     we only append the subdir spec if the path ends in
3120c3983b2SBen Gras 	 *     a '/' --- elements that do not end in '/' are assumed to
3130c3983b2SBen Gras 	 *     not have subdirectories.  this is mainly for backward compat,
3140c3983b2SBen Gras 	 *     but it allows non-subdir configs like:
3150c3983b2SBen Gras 	 *	sect3       /usr/share/man/{old/,}cat3
3160c3983b2SBen Gras 	 *	doc         /usr/{pkg,share}/doc/{sendmail/op,sendmail/intro}
3170c3983b2SBen Gras 	 *
3180c3983b2SBen Gras 	 *     note that we try and be careful to not put double slashes
3190c3983b2SBen Gras 	 *     in the path (e.g. we want /usr/share/man/man1, not
3200c3983b2SBen Gras 	 *     /usr/share/man//man1) because "more" will put the filename
3210c3983b2SBen Gras 	 *     we generate in its prompt and the double slashes look ugly.
3220c3983b2SBen Gras 	 */
3230c3983b2SBen Gras 	if (m.manpath) {
3240c3983b2SBen Gras 
3250c3983b2SBen Gras 		/* note: strtok is going to destroy m.manpath */
3260c3983b2SBen Gras 		for (p = strtok(m.manpath, ":") ; p ; p = strtok(NULL, ":")) {
3270c3983b2SBen Gras 			len = strlen(p);
3280c3983b2SBen Gras 			if (len < 1)
3290c3983b2SBen Gras 				continue;
3300c3983b2SBen Gras 			TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q)
3310c3983b2SBen Gras 				addpath(&m, p, len, esubd->s);
3320c3983b2SBen Gras 		}
3330c3983b2SBen Gras 
3340c3983b2SBen Gras 	} else {
3350c3983b2SBen Gras 
3360c3983b2SBen Gras 		TAILQ_FOREACH(epath, &m.defaultpath->entrylist, q) {
3370c3983b2SBen Gras 			/* handle trailing "/" magic here ... */
3380c3983b2SBen Gras 		  	if (abs_section && epath->s[epath->len - 1] != '/') {
3390c3983b2SBen Gras 				addpath(&m, "", 1, epath->s);
3400c3983b2SBen Gras 				continue;
3410c3983b2SBen Gras 			}
3420c3983b2SBen Gras 
3430c3983b2SBen Gras 			TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q)
3440c3983b2SBen Gras 				addpath(&m, epath->s, epath->len, esubd->s);
3450c3983b2SBen Gras 		}
3460c3983b2SBen Gras 
3470c3983b2SBen Gras 	}
3480c3983b2SBen Gras 
3490c3983b2SBen Gras 	/*
3500c3983b2SBen Gras 	 * [4] finally, prepend the "-m" m.addpath to mymanpath if it
3510c3983b2SBen Gras 	 *     was specified.   subdirs and machine are always applied to
3520c3983b2SBen Gras 	 *     m.addpath.
3530c3983b2SBen Gras 	 */
3540c3983b2SBen Gras 	if (m.addpath) {
3550c3983b2SBen Gras 
3560c3983b2SBen Gras 		/* note: strtok is going to destroy m.addpath */
3570c3983b2SBen Gras 		for (p = strtok(m.addpath, ":") ; p ; p = strtok(NULL, ":")) {
3580c3983b2SBen Gras 			len = strlen(p);
3590c3983b2SBen Gras 			if (len < 1)
3600c3983b2SBen Gras 				continue;
3610c3983b2SBen Gras 			TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q)
3620c3983b2SBen Gras 				addpath(&m, p, len, esubd->s);
3630c3983b2SBen Gras 		}
3640c3983b2SBen Gras 
3650c3983b2SBen Gras 	}
3660c3983b2SBen Gras 
367b9a6a72bSLionel Sambuc 	if (m.getpath)
368b9a6a72bSLionel Sambuc 		printmanpath(&m);
369b9a6a72bSLionel Sambuc 
3700c3983b2SBen Gras 	/*
3710c3983b2SBen Gras 	 * now m.mymanpath is complete!
3720c3983b2SBen Gras 	 */
3730c3983b2SBen Gras #if MAN_DEBUG
3740c3983b2SBen Gras 	printf("mymanpath:\n");
3750c3983b2SBen Gras 	TAILQ_FOREACH(epath, &m.mymanpath->entrylist, q) {
3760c3983b2SBen Gras 		printf("\t%s\n", epath->s);
3770c3983b2SBen Gras 	}
3780c3983b2SBen Gras #endif
3790c3983b2SBen Gras 
3800c3983b2SBen Gras 	/*
3810c3983b2SBen Gras 	 * start searching for matching files and format them if necessary.
3820c3983b2SBen Gras 	 * setup an interrupt handler so that we can ensure that temporary
3830c3983b2SBen Gras 	 * files go away.
3840c3983b2SBen Gras 	 */
3850c3983b2SBen Gras 	(void)signal(SIGINT, onsig);
3860c3983b2SBen Gras 	(void)signal(SIGHUP, onsig);
3870c3983b2SBen Gras 	(void)signal(SIGPIPE, onsig);
3880c3983b2SBen Gras 
3890c3983b2SBen Gras 	memset(&pg, 0, sizeof(pg));
3900c3983b2SBen Gras 	for (found = 0; *argv; ++argv)
3910c3983b2SBen Gras 		if (manual(*argv, &m, &pg)) {
3920c3983b2SBen Gras 			found = 1;
3930c3983b2SBen Gras 		}
3940c3983b2SBen Gras 
3950c3983b2SBen Gras 	/* if nothing found, we're done. */
3960c3983b2SBen Gras 	if (!found) {
3970c3983b2SBen Gras 		(void)cleanup();
3980c3983b2SBen Gras 		exit(EXIT_FAILURE);
3990c3983b2SBen Gras 	}
4000c3983b2SBen Gras 
4010c3983b2SBen Gras 	/*
4020c3983b2SBen Gras 	 * handle the simple display cases first (m.cat, m.how, m.where)
4030c3983b2SBen Gras 	 */
4040c3983b2SBen Gras 	if (m.cat) {
4050c3983b2SBen Gras 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
4060c3983b2SBen Gras 			if (**ap == '\0')
4070c3983b2SBen Gras 				continue;
4080c3983b2SBen Gras 			cat(*ap);
4090c3983b2SBen Gras 		}
4100c3983b2SBen Gras 		exit(cleanup());
4110c3983b2SBen Gras 	}
4120c3983b2SBen Gras 	if (m.how) {
4130c3983b2SBen Gras 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
4140c3983b2SBen Gras 			if (**ap == '\0')
4150c3983b2SBen Gras 				continue;
4160c3983b2SBen Gras 			how(*ap);
4170c3983b2SBen Gras 		}
4180c3983b2SBen Gras 		exit(cleanup());
4190c3983b2SBen Gras 	}
4200c3983b2SBen Gras 	if (m.where) {
4210c3983b2SBen Gras 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
4220c3983b2SBen Gras 			if (**ap == '\0')
4230c3983b2SBen Gras 				continue;
4240c3983b2SBen Gras 			(void)printf("%s\n", *ap);
4250c3983b2SBen Gras 		}
4260c3983b2SBen Gras 		exit(cleanup());
4270c3983b2SBen Gras 	}
4280c3983b2SBen Gras 
4290c3983b2SBen Gras 	/*
4300c3983b2SBen Gras 	 * normal case - we display things in a single command, so
4310c3983b2SBen Gras          * build a list of things to display.  first compute total
4320c3983b2SBen Gras 	 * length of buffer we will need so we can malloc it.
4330c3983b2SBen Gras 	 */
4340c3983b2SBen Gras 	for (ap = pg.gl_pathv, len = m.pagerlen + 1; *ap != NULL; ++ap) {
4350c3983b2SBen Gras 		if (**ap == '\0')
4360c3983b2SBen Gras 			continue;
4370c3983b2SBen Gras 		len += strlen(*ap) + 1;
4380c3983b2SBen Gras 	}
4390c3983b2SBen Gras 	if ((cmd = malloc(len)) == NULL) {
4400c3983b2SBen Gras 		warn("malloc");
4410c3983b2SBen Gras 		(void)cleanup();
4420c3983b2SBen Gras 		exit(EXIT_FAILURE);
4430c3983b2SBen Gras 	}
4440c3983b2SBen Gras 
4450c3983b2SBen Gras 	/* now build the command string... */
4460c3983b2SBen Gras 	p = cmd;
4470c3983b2SBen Gras 	len = m.pagerlen;
4480c3983b2SBen Gras 	memcpy(p, m.pager, len);
4490c3983b2SBen Gras 	p += len;
4500c3983b2SBen Gras 	*p++ = ' ';
4510c3983b2SBen Gras 	for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
4520c3983b2SBen Gras 		if (**ap == '\0')
4530c3983b2SBen Gras 			continue;
4540c3983b2SBen Gras 		len = strlen(*ap);
4550c3983b2SBen Gras 		memcpy(p, *ap, len);
4560c3983b2SBen Gras 		p += len;
4570c3983b2SBen Gras 		*p++ = ' ';
4580c3983b2SBen Gras 	}
4590c3983b2SBen Gras 	*--p = '\0';
4600c3983b2SBen Gras 
4610c3983b2SBen Gras 	/* Use system(3) in case someone's pager is "pager arg1 arg2". */
4620c3983b2SBen Gras 	(void)system(cmd);
4630c3983b2SBen Gras 
4640c3983b2SBen Gras 	exit(cleanup());
4650c3983b2SBen Gras }
4660c3983b2SBen Gras 
4670c3983b2SBen Gras static int
manual_find_literalfile(struct manstate * mp,char ** pv)46884d9c625SLionel Sambuc manual_find_literalfile(struct manstate *mp, char **pv)
4690c3983b2SBen Gras {
4700c3983b2SBen Gras 	ENTRY *suffix;
4710c3983b2SBen Gras 	int found;
47284d9c625SLionel Sambuc 	char buf[MAXPATHLEN];
47384d9c625SLionel Sambuc 	const char *p;
47484d9c625SLionel Sambuc 	int suflen;
47584d9c625SLionel Sambuc 
47684d9c625SLionel Sambuc 	found = 0;
47784d9c625SLionel Sambuc 
47884d9c625SLionel Sambuc 	/*
47984d9c625SLionel Sambuc 	 * Expand both '*' and suffix to force an actual
48084d9c625SLionel Sambuc 	 * match via fnmatch(3). Since the only match in pg
48184d9c625SLionel Sambuc 	 * is the literal file, the match is genuine.
48284d9c625SLionel Sambuc 	 */
48384d9c625SLionel Sambuc 
48484d9c625SLionel Sambuc 	TAILQ_FOREACH(suffix, &mp->buildlist->entrylist, q) {
48584d9c625SLionel Sambuc 		for (p = suffix->s, suflen = 0;
48684d9c625SLionel Sambuc 		    *p != '\0' && !isspace((unsigned char)*p);
48784d9c625SLionel Sambuc 		    ++p)
48884d9c625SLionel Sambuc 			++suflen;
48984d9c625SLionel Sambuc 		if (*p == '\0')
49084d9c625SLionel Sambuc 			continue;
49184d9c625SLionel Sambuc 
49284d9c625SLionel Sambuc 		(void)snprintf(buf, sizeof(buf), "*%.*s", suflen, suffix->s);
49384d9c625SLionel Sambuc 
49484d9c625SLionel Sambuc 		if (!fnmatch(buf, *pv, 0)) {
49584d9c625SLionel Sambuc 			if (!mp->where)
49684d9c625SLionel Sambuc 				build_page(p + 1, pv, mp);
49784d9c625SLionel Sambuc 			found = 1;
49884d9c625SLionel Sambuc 			break;
49984d9c625SLionel Sambuc 		}
50084d9c625SLionel Sambuc 	}
50184d9c625SLionel Sambuc 
50284d9c625SLionel Sambuc 	return found;
50384d9c625SLionel Sambuc }
50484d9c625SLionel Sambuc 
50584d9c625SLionel Sambuc static int
manual_find_buildkeyword(const char * prefix,const char * escpage,struct manstate * mp,char ** pv)50684d9c625SLionel Sambuc manual_find_buildkeyword(const char *prefix, const char *escpage,
50784d9c625SLionel Sambuc     struct manstate *mp, char **pv)
50884d9c625SLionel Sambuc {
50984d9c625SLionel Sambuc 	ENTRY *suffix;
51084d9c625SLionel Sambuc 	int found;
51184d9c625SLionel Sambuc 	char buf[MAXPATHLEN];
51284d9c625SLionel Sambuc 	const char *p;
51384d9c625SLionel Sambuc 	int suflen;
5140c3983b2SBen Gras 
5150c3983b2SBen Gras 	found = 0;
5160c3983b2SBen Gras 	/* Try the _build keywords next. */
5170c3983b2SBen Gras 	TAILQ_FOREACH(suffix, &mp->buildlist->entrylist, q) {
51884d9c625SLionel Sambuc 		for (p = suffix->s, suflen = 0;
5190c3983b2SBen Gras 		    *p != '\0' && !isspace((unsigned char)*p);
5200c3983b2SBen Gras 		    ++p)
52184d9c625SLionel Sambuc 			++suflen;
5220c3983b2SBen Gras 		if (*p == '\0')
5230c3983b2SBen Gras 			continue;
5240c3983b2SBen Gras 
52584d9c625SLionel Sambuc 		(void)snprintf(buf, sizeof(buf), "%s%s%.*s",
52684d9c625SLionel Sambuc 		    prefix, escpage, suflen, suffix->s);
52784d9c625SLionel Sambuc 		if (!fnmatch(buf, *pv, 0)) {
5280c3983b2SBen Gras 			if (!mp->where)
52984d9c625SLionel Sambuc 				build_page(p + 1, pv, mp);
5300c3983b2SBen Gras 			found = 1;
5310c3983b2SBen Gras 			break;
5320c3983b2SBen Gras 		}
5330c3983b2SBen Gras 	}
5340c3983b2SBen Gras 
5350c3983b2SBen Gras 	return found;
5360c3983b2SBen Gras }
5370c3983b2SBen Gras 
5380c3983b2SBen Gras /*
5390c3983b2SBen Gras  * manual --
5400c3983b2SBen Gras  *	Search the manuals for the pages.
5410c3983b2SBen Gras  */
5420c3983b2SBen Gras static int
manual(char * page,struct manstate * mp,glob_t * pg)5430c3983b2SBen Gras manual(char *page, struct manstate *mp, glob_t *pg)
5440c3983b2SBen Gras {
5450c3983b2SBen Gras 	ENTRY *suffix, *mdir;
5460c3983b2SBen Gras 	int anyfound, error, found;
5470c3983b2SBen Gras 	size_t cnt;
5480c3983b2SBen Gras 	char *p, buf[MAXPATHLEN], *escpage, *eptr;
5490c3983b2SBen Gras 	static const char escglob[] = "\\~?*{}[]";
5500c3983b2SBen Gras 
5510c3983b2SBen Gras 	anyfound = 0;
5520c3983b2SBen Gras 
5530c3983b2SBen Gras 	/*
5540c3983b2SBen Gras 	 * Fixup page which may contain glob(3) special characters, e.g.
5550c3983b2SBen Gras 	 * the famous "No man page for [" FAQ.
5560c3983b2SBen Gras 	 */
5570c3983b2SBen Gras 	if ((escpage = malloc((2 * strlen(page)) + 1)) == NULL) {
5580c3983b2SBen Gras 		warn("malloc");
5590c3983b2SBen Gras 		(void)cleanup();
5600c3983b2SBen Gras 		exit(EXIT_FAILURE);
5610c3983b2SBen Gras 	}
5620c3983b2SBen Gras 
5630c3983b2SBen Gras 	p = page;
5640c3983b2SBen Gras 	eptr = escpage;
5650c3983b2SBen Gras 
5660c3983b2SBen Gras 	while (*p) {
5670c3983b2SBen Gras 		if (strchr(escglob, *p) != NULL) {
5680c3983b2SBen Gras 			*eptr++ = '\\';
5690c3983b2SBen Gras 			*eptr++ = *p++;
5700c3983b2SBen Gras 		} else
5710c3983b2SBen Gras 			*eptr++ = *p++;
5720c3983b2SBen Gras 	}
5730c3983b2SBen Gras 
5740c3983b2SBen Gras 	*eptr = '\0';
5750c3983b2SBen Gras 
5760c3983b2SBen Gras 	/*
577*0a6a1f1dSLionel Sambuc 	 * If 'page' is given with an absolute path,
578*0a6a1f1dSLionel Sambuc 	 * or a relative path explicitly beginning with "./"
579*0a6a1f1dSLionel Sambuc 	 * or "../", then interpret it as a file specification.
5800c3983b2SBen Gras 	 */
581*0a6a1f1dSLionel Sambuc 	if ((page[0] == '/')
582*0a6a1f1dSLionel Sambuc 	    || (page[0] == '.' && page[1] == '/')
583*0a6a1f1dSLionel Sambuc 	    || (page[0] == '.' && page[1] == '.' && page[2] == '/')
584*0a6a1f1dSLionel Sambuc 	    ) {
5850c3983b2SBen Gras 		/* check if file actually exists */
5860c3983b2SBen Gras 		(void)strlcpy(buf, escpage, sizeof(buf));
5870c3983b2SBen Gras 		error = glob(buf, GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg);
5880c3983b2SBen Gras 		if (error != 0) {
5890c3983b2SBen Gras 			if (error == GLOB_NOMATCH) {
5900c3983b2SBen Gras 				goto notfound;
5910c3983b2SBen Gras 			} else {
5920c3983b2SBen Gras 				errx(EXIT_FAILURE, "glob failed");
5930c3983b2SBen Gras 			}
5940c3983b2SBen Gras 		}
5950c3983b2SBen Gras 
5960c3983b2SBen Gras 		if (pg->gl_matchc == 0)
5970c3983b2SBen Gras 			goto notfound;
5980c3983b2SBen Gras 
59984d9c625SLionel Sambuc 		/* literal file only yields one match */
60084d9c625SLionel Sambuc 		cnt = pg->gl_pathc - pg->gl_matchc;
6010c3983b2SBen Gras 
60284d9c625SLionel Sambuc 		if (manual_find_literalfile(mp, &pg->gl_pathv[cnt])) {
6030c3983b2SBen Gras 			anyfound = 1;
60484d9c625SLionel Sambuc 		} else {
6050c3983b2SBen Gras 			/* It's not a man page, forget about it. */
60684d9c625SLionel Sambuc 			*pg->gl_pathv[cnt] = '\0';
6070c3983b2SBen Gras 		}
6080c3983b2SBen Gras 
6090c3983b2SBen Gras   notfound:
6100c3983b2SBen Gras 		if (!anyfound) {
6110c3983b2SBen Gras 			if (addentry(mp->missinglist, page, 0) < 0) {
6120c3983b2SBen Gras 				warn("malloc");
6130c3983b2SBen Gras 				(void)cleanup();
6140c3983b2SBen Gras 				exit(EXIT_FAILURE);
6150c3983b2SBen Gras 			}
6160c3983b2SBen Gras 		}
6170c3983b2SBen Gras 		free(escpage);
6180c3983b2SBen Gras 		return anyfound;
6190c3983b2SBen Gras 	}
6200c3983b2SBen Gras 
6210c3983b2SBen Gras 	/* For each man directory in mymanpath ... */
6220c3983b2SBen Gras 	TAILQ_FOREACH(mdir, &mp->mymanpath->entrylist, q) {
6230c3983b2SBen Gras 
6240c3983b2SBen Gras 		/*
6250c3983b2SBen Gras 		 * use glob(3) to look in the filesystem for matching files.
6260c3983b2SBen Gras 		 * match any suffix here, as we will check that later.
6270c3983b2SBen Gras 		 */
6280c3983b2SBen Gras 		(void)snprintf(buf, sizeof(buf), "%s/%s.*", mdir->s, escpage);
6290c3983b2SBen Gras 		if ((error = glob(buf,
6300c3983b2SBen Gras 		    GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) {
6310c3983b2SBen Gras 			if (error == GLOB_NOMATCH)
6320c3983b2SBen Gras 				continue;
6330c3983b2SBen Gras 			else {
6340c3983b2SBen Gras 				warn("globbing");
6350c3983b2SBen Gras 				(void)cleanup();
6360c3983b2SBen Gras 				exit(EXIT_FAILURE);
6370c3983b2SBen Gras 			}
6380c3983b2SBen Gras 		}
6390c3983b2SBen Gras 		if (pg->gl_matchc == 0)
6400c3983b2SBen Gras 			continue;
6410c3983b2SBen Gras 
6420c3983b2SBen Gras 		/*
6430c3983b2SBen Gras 		 * start going through the matches glob(3) just found and
6440c3983b2SBen Gras 		 * use m.pathsearch (if present) to filter out pages we
6450c3983b2SBen Gras 		 * don't want.  then verify the suffix is valid, and build
6460c3983b2SBen Gras 		 * the page if we have a _build suffix.
6470c3983b2SBen Gras 		 */
6480c3983b2SBen Gras 		for (cnt = pg->gl_pathc - pg->gl_matchc;
6490c3983b2SBen Gras 		    cnt < pg->gl_pathc; ++cnt) {
6500c3983b2SBen Gras 
6510c3983b2SBen Gras 			/* filter on directory path name */
6520c3983b2SBen Gras 			if (mp->pathsearch) {
6530c3983b2SBen Gras 				p = strstr(pg->gl_pathv[cnt], mp->pathsearch);
6540c3983b2SBen Gras 				if (!p || strchr(p, '/') == NULL) {
65584d9c625SLionel Sambuc 					*pg->gl_pathv[cnt] = '\0'; /* zap! */
6560c3983b2SBen Gras 					continue;
6570c3983b2SBen Gras 				}
6580c3983b2SBen Gras 			}
6590c3983b2SBen Gras 
6600c3983b2SBen Gras 			/*
6610c3983b2SBen Gras 			 * Try the _suffix keywords first.
6620c3983b2SBen Gras 			 *
6630c3983b2SBen Gras 			 * XXX
66484d9c625SLionel Sambuc 			 * Older versions of man.conf didn't have the _suffix
6650c3983b2SBen Gras 			 * keywords, it was assumed that everything was a .0.
6660c3983b2SBen Gras 			 * We just test for .0 first, it's fast and probably
6670c3983b2SBen Gras 			 * going to hit.
6680c3983b2SBen Gras 			 */
6690c3983b2SBen Gras 			(void)snprintf(buf, sizeof(buf), "*/%s.0", escpage);
6700c3983b2SBen Gras 			if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
6710c3983b2SBen Gras 				goto next;
6720c3983b2SBen Gras 
6730c3983b2SBen Gras 			found = 0;
6740c3983b2SBen Gras 			TAILQ_FOREACH(suffix, &mp->suffixlist->entrylist, q) {
6750c3983b2SBen Gras 				(void)snprintf(buf,
6760c3983b2SBen Gras 				     sizeof(buf), "*/%s%s", escpage,
6770c3983b2SBen Gras 				     suffix->s);
6780c3983b2SBen Gras 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
6790c3983b2SBen Gras 					found = 1;
6800c3983b2SBen Gras 					break;
6810c3983b2SBen Gras 				}
6820c3983b2SBen Gras 			}
6830c3983b2SBen Gras 			if (found)
6840c3983b2SBen Gras 				goto next;
6850c3983b2SBen Gras 
6860c3983b2SBen Gras 			/* Try the _build keywords next. */
68784d9c625SLionel Sambuc 			found = manual_find_buildkeyword("*/", escpage,
68884d9c625SLionel Sambuc 				mp, &pg->gl_pathv[cnt]);
6890c3983b2SBen Gras 			if (found) {
6900c3983b2SBen Gras next:				anyfound = 1;
6910c3983b2SBen Gras 				if (!mp->all) {
6920c3983b2SBen Gras 					/* Delete any other matches. */
6930c3983b2SBen Gras 					while (++cnt< pg->gl_pathc)
69484d9c625SLionel Sambuc 						*pg->gl_pathv[cnt] = '\0';
6950c3983b2SBen Gras 					break;
6960c3983b2SBen Gras 				}
6970c3983b2SBen Gras 				continue;
6980c3983b2SBen Gras 			}
6990c3983b2SBen Gras 
7000c3983b2SBen Gras 			/* It's not a man page, forget about it. */
70184d9c625SLionel Sambuc 			*pg->gl_pathv[cnt] = '\0';
7020c3983b2SBen Gras 		}
7030c3983b2SBen Gras 
7040c3983b2SBen Gras 		if (anyfound && !mp->all)
7050c3983b2SBen Gras 			break;
7060c3983b2SBen Gras 	}
7070c3983b2SBen Gras 
7080c3983b2SBen Gras 	/* If not found, enter onto the missing list. */
7090c3983b2SBen Gras 	if (!anyfound) {
7100c3983b2SBen Gras 		if (addentry(mp->missinglist, page, 0) < 0) {
7110c3983b2SBen Gras 			warn("malloc");
7120c3983b2SBen Gras 			(void)cleanup();
7130c3983b2SBen Gras 			exit(EXIT_FAILURE);
7140c3983b2SBen Gras 		}
7150c3983b2SBen Gras 	}
7160c3983b2SBen Gras 
7170c3983b2SBen Gras 	free(escpage);
7180c3983b2SBen Gras 	return anyfound;
7190c3983b2SBen Gras }
7200c3983b2SBen Gras 
7210c3983b2SBen Gras /*
722*0a6a1f1dSLionel Sambuc  * A do-nothing counterpart to fmtcheck(3) that only supplies the
723*0a6a1f1dSLionel Sambuc  * __format_arg marker.  Actual fmtcheck(3) call is done once in
724*0a6a1f1dSLionel Sambuc  * config().
725*0a6a1f1dSLionel Sambuc  */
726*0a6a1f1dSLionel Sambuc __always_inline __format_arg(2)
727*0a6a1f1dSLionel Sambuc static inline const char *
fmtcheck_ok(const char * userfmt,const char * template)728*0a6a1f1dSLionel Sambuc fmtcheck_ok(const char *userfmt, const char *template)
729*0a6a1f1dSLionel Sambuc {
730*0a6a1f1dSLionel Sambuc 	return userfmt;
731*0a6a1f1dSLionel Sambuc }
732*0a6a1f1dSLionel Sambuc 
733*0a6a1f1dSLionel Sambuc /*
7340c3983b2SBen Gras  * build_page --
7350c3983b2SBen Gras  *	Build a man page for display.
7360c3983b2SBen Gras  */
7370c3983b2SBen Gras static void
build_page(const char * fmt,char ** pathp,struct manstate * mp)73884d9c625SLionel Sambuc build_page(const char *fmt, char **pathp, struct manstate *mp)
7390c3983b2SBen Gras {
7400c3983b2SBen Gras 	static int warned;
74184d9c625SLionel Sambuc 	int olddir, fd, n;
74284d9c625SLionel Sambuc 	size_t tmpdirlen;
7430c3983b2SBen Gras 	char *p, *b;
7440c3983b2SBen Gras 	char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
7450c3983b2SBen Gras 	const char *tmpdir;
7460c3983b2SBen Gras 
7470c3983b2SBen Gras 	/* Let the user know this may take awhile. */
7480c3983b2SBen Gras 	if (!warned) {
7490c3983b2SBen Gras 		warned = 1;
7500c3983b2SBen Gras 		warnx("Formatting manual page...");
7510c3983b2SBen Gras 	}
7520c3983b2SBen Gras 
7530c3983b2SBen Gras        /*
7540c3983b2SBen Gras         * Historically man chdir'd to the root of the man tree.
7550c3983b2SBen Gras         * This was used in man pages that contained relative ".so"
7560c3983b2SBen Gras         * directives (including other man pages for command aliases etc.)
7570c3983b2SBen Gras         * It even went one step farther, by examining the first line
7580c3983b2SBen Gras         * of the man page and parsing the .so filename so it would
7590c3983b2SBen Gras         * make hard(?) links to the cat'ted man pages for space savings.
7600c3983b2SBen Gras         * (We don't do that here, but we could).
7610c3983b2SBen Gras         */
7620c3983b2SBen Gras 
7630c3983b2SBen Gras        /* copy and find the end */
7640c3983b2SBen Gras        for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
7650c3983b2SBen Gras                continue;
7660c3983b2SBen Gras 
7670c3983b2SBen Gras 	/*
7680c3983b2SBen Gras 	 * skip the last two path components, page name and man[n] ...
7690c3983b2SBen Gras 	 * (e.g. buf will be "/usr/share/man" and p will be "man1/man.1")
7700c3983b2SBen Gras 	 * we also save a pointer to our current directory so that we
7710c3983b2SBen Gras 	 * can fchdir() back to it.  this allows relative MANDIR paths
7720c3983b2SBen Gras 	 * to work with multiple man pages... e.g. consider:
7730c3983b2SBen Gras 	 *   	cd /usr/share && man -M ./man cat ls
7740c3983b2SBen Gras 	 * when no "cat1" subdir files are present.
7750c3983b2SBen Gras 	 */
7760c3983b2SBen Gras 	olddir = -1;
7770c3983b2SBen Gras 	for (--b, --p, n = 2; b != buf; b--, p--)
7780c3983b2SBen Gras 		if (*b == '/')
7790c3983b2SBen Gras 			if (--n == 0) {
7800c3983b2SBen Gras 				*b = '\0';
7810c3983b2SBen Gras 				olddir = open(".", O_RDONLY);
7820c3983b2SBen Gras 				(void) chdir(buf);
7830c3983b2SBen Gras 				p++;
7840c3983b2SBen Gras 				break;
7850c3983b2SBen Gras 			}
7860c3983b2SBen Gras 
7870c3983b2SBen Gras 
78884d9c625SLionel Sambuc 	/* advance fmt past the suffix spec to the printf format string */
7890c3983b2SBen Gras 	for (; *fmt && isspace((unsigned char)*fmt); ++fmt)
7900c3983b2SBen Gras 		continue;
7910c3983b2SBen Gras 
7920c3983b2SBen Gras 	/*
7930c3983b2SBen Gras 	 * Get a temporary file and build a version of the file
7940c3983b2SBen Gras 	 * to display.  Replace the old file name with the new one.
7950c3983b2SBen Gras 	 */
7960c3983b2SBen Gras 	if ((tmpdir = getenv("TMPDIR")) == NULL)
7970c3983b2SBen Gras 		tmpdir = _PATH_TMP;
7980c3983b2SBen Gras 	tmpdirlen = strlen(tmpdir);
7990c3983b2SBen Gras 	(void)snprintf(tpath, sizeof (tpath), "%s%s%s", tmpdir,
80084d9c625SLionel Sambuc 	    (tmpdirlen > 0 && tmpdir[tmpdirlen-1] == '/') ? "" : "/", TMPFILE);
8010c3983b2SBen Gras 	if ((fd = mkstemp(tpath)) == -1) {
8020c3983b2SBen Gras 		warn("%s", tpath);
8030c3983b2SBen Gras 		(void)cleanup();
8040c3983b2SBen Gras 		exit(EXIT_FAILURE);
8050c3983b2SBen Gras 	}
8060c3983b2SBen Gras 	(void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
807*0a6a1f1dSLionel Sambuc 	(void)snprintf(cmd, sizeof(cmd), fmtcheck_ok(buf, "%s"), p);
8080c3983b2SBen Gras 	(void)system(cmd);
8090c3983b2SBen Gras 	(void)close(fd);
8100c3983b2SBen Gras 	if ((*pathp = strdup(tpath)) == NULL) {
8110c3983b2SBen Gras 		warn("malloc");
8120c3983b2SBen Gras 		(void)cleanup();
8130c3983b2SBen Gras 		exit(EXIT_FAILURE);
8140c3983b2SBen Gras 	}
8150c3983b2SBen Gras 
8160c3983b2SBen Gras 	/* Link the built file into the remove-when-done list. */
8170c3983b2SBen Gras 	if (addentry(mp->intmp, *pathp, 0) < 0) {
8180c3983b2SBen Gras 		warn("malloc");
8190c3983b2SBen Gras 		(void)cleanup();
8200c3983b2SBen Gras 		exit(EXIT_FAILURE);
8210c3983b2SBen Gras 	}
8220c3983b2SBen Gras 
8230c3983b2SBen Gras 	/* restore old directory so relative manpaths still work */
8240c3983b2SBen Gras 	if (olddir != -1) {
8250c3983b2SBen Gras 		fchdir(olddir);
8260c3983b2SBen Gras 		close(olddir);
8270c3983b2SBen Gras 	}
8280c3983b2SBen Gras }
8290c3983b2SBen Gras 
8300c3983b2SBen Gras /*
8310c3983b2SBen Gras  * how --
8320c3983b2SBen Gras  *	display how information
8330c3983b2SBen Gras  */
8340c3983b2SBen Gras static void
how(const char * fname)83584d9c625SLionel Sambuc how(const char *fname)
8360c3983b2SBen Gras {
8370c3983b2SBen Gras 	FILE *fp;
8380c3983b2SBen Gras 
8390c3983b2SBen Gras 	int lcnt, print;
84084d9c625SLionel Sambuc 	char buf[256];
84184d9c625SLionel Sambuc 	const char *p;
8420c3983b2SBen Gras 
8430c3983b2SBen Gras 	if (!(fp = fopen(fname, "r"))) {
8440c3983b2SBen Gras 		warn("%s", fname);
8450c3983b2SBen Gras 		(void)cleanup();
8460c3983b2SBen Gras 		exit(EXIT_FAILURE);
8470c3983b2SBen Gras 	}
8480c3983b2SBen Gras #define	S1	"SYNOPSIS"
8490c3983b2SBen Gras #define	S2	"S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
8500c3983b2SBen Gras #define	D1	"DESCRIPTION"
8510c3983b2SBen Gras #define	D2	"D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
8520c3983b2SBen Gras 	for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
8530c3983b2SBen Gras 		if (!strncmp(buf, S1, sizeof(S1) - 1) ||
8540c3983b2SBen Gras 		    !strncmp(buf, S2, sizeof(S2) - 1)) {
8550c3983b2SBen Gras 			print = 1;
8560c3983b2SBen Gras 			continue;
8570c3983b2SBen Gras 		} else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
8580c3983b2SBen Gras 		    !strncmp(buf, D2, sizeof(D2) - 1)) {
8590c3983b2SBen Gras 			if (fp)
8600c3983b2SBen Gras 				(void)fclose(fp);
8610c3983b2SBen Gras 			return;
8620c3983b2SBen Gras 		}
8630c3983b2SBen Gras 		if (!print)
8640c3983b2SBen Gras 			continue;
8650c3983b2SBen Gras 		if (*buf == '\n')
8660c3983b2SBen Gras 			++lcnt;
8670c3983b2SBen Gras 		else {
8680c3983b2SBen Gras 			for(; lcnt; --lcnt)
8690c3983b2SBen Gras 				(void)putchar('\n');
8700c3983b2SBen Gras 			for (p = buf; isspace((unsigned char)*p); ++p)
8710c3983b2SBen Gras 				continue;
8720c3983b2SBen Gras 			(void)fputs(p, stdout);
8730c3983b2SBen Gras 		}
8740c3983b2SBen Gras 	}
8750c3983b2SBen Gras 	(void)fclose(fp);
8760c3983b2SBen Gras }
8770c3983b2SBen Gras 
8780c3983b2SBen Gras /*
8790c3983b2SBen Gras  * cat --
8800c3983b2SBen Gras  *	cat out the file
8810c3983b2SBen Gras  */
8820c3983b2SBen Gras static void
cat(const char * fname)88384d9c625SLionel Sambuc cat(const char *fname)
8840c3983b2SBen Gras {
88584d9c625SLionel Sambuc 	int fd;
88684d9c625SLionel Sambuc 	ssize_t n;
8870c3983b2SBen Gras 	char buf[2048];
8880c3983b2SBen Gras 
8890c3983b2SBen Gras 	if ((fd = open(fname, O_RDONLY, 0)) < 0) {
8900c3983b2SBen Gras 		warn("%s", fname);
8910c3983b2SBen Gras 		(void)cleanup();
8920c3983b2SBen Gras 		exit(EXIT_FAILURE);
8930c3983b2SBen Gras 	}
8940c3983b2SBen Gras 	while ((n = read(fd, buf, sizeof(buf))) > 0)
89584d9c625SLionel Sambuc 		if (write(STDOUT_FILENO, buf, (size_t)n) != n) {
8960c3983b2SBen Gras 			warn("write");
8970c3983b2SBen Gras 			(void)cleanup();
8980c3983b2SBen Gras 			exit(EXIT_FAILURE);
8990c3983b2SBen Gras 		}
9000c3983b2SBen Gras 	if (n == -1) {
9010c3983b2SBen Gras 		warn("read");
9020c3983b2SBen Gras 		(void)cleanup();
9030c3983b2SBen Gras 		exit(EXIT_FAILURE);
9040c3983b2SBen Gras 	}
9050c3983b2SBen Gras 	(void)close(fd);
9060c3983b2SBen Gras }
9070c3983b2SBen Gras 
9080c3983b2SBen Gras /*
9090c3983b2SBen Gras  * check_pager --
9100c3983b2SBen Gras  *	check the user supplied page information
9110c3983b2SBen Gras  */
9120c3983b2SBen Gras static const char *
check_pager(const char * name)9130c3983b2SBen Gras check_pager(const char *name)
9140c3983b2SBen Gras {
9150c3983b2SBen Gras 	const char *p;
9160c3983b2SBen Gras 
9170c3983b2SBen Gras 	/*
9180c3983b2SBen Gras 	 * if the user uses "more", we make it "more -s"; watch out for
9190c3983b2SBen Gras 	 * PAGER = "mypager /usr/ucb/more"
9200c3983b2SBen Gras 	 */
9210c3983b2SBen Gras 	for (p = name; *p && !isspace((unsigned char)*p); ++p)
9220c3983b2SBen Gras 		continue;
9230c3983b2SBen Gras 	for (; p > name && *p != '/'; --p);
9240c3983b2SBen Gras 	if (p != name)
9250c3983b2SBen Gras 		++p;
9260c3983b2SBen Gras 
9270c3983b2SBen Gras 	/* make sure it's "more", not "morex" */
9280c3983b2SBen Gras 	if (!strncmp(p, "more", 4) && (!p[4] || isspace((unsigned char)p[4]))){
9290c3983b2SBen Gras 		char *newname;
9300c3983b2SBen Gras 		(void)asprintf(&newname, "%s %s", p, "-s");
9310c3983b2SBen Gras 		name = newname;
9320c3983b2SBen Gras 	}
9330c3983b2SBen Gras 
9340c3983b2SBen Gras 	return name;
9350c3983b2SBen Gras }
9360c3983b2SBen Gras 
9370c3983b2SBen Gras /*
9380c3983b2SBen Gras  * jump --
9390c3983b2SBen Gras  *	strip out flag argument and jump
9400c3983b2SBen Gras  */
9410c3983b2SBen Gras static void
jump(char ** argv,const char * flag,const char * name)94284d9c625SLionel Sambuc jump(char **argv, const char *flag, const char *name)
9430c3983b2SBen Gras {
9440c3983b2SBen Gras 	char **arg;
9450c3983b2SBen Gras 
94684d9c625SLionel Sambuc 	argv[0] = __UNCONST(name);
9470c3983b2SBen Gras 	for (arg = argv + 1; *arg; ++arg)
9480c3983b2SBen Gras 		if (!strcmp(*arg, flag))
9490c3983b2SBen Gras 			break;
9500c3983b2SBen Gras 	for (; *arg; ++arg)
9510c3983b2SBen Gras 		arg[0] = arg[1];
9520c3983b2SBen Gras 	execvp(name, argv);
9530c3983b2SBen Gras 	err(EXIT_FAILURE, "Cannot execute `%s'", name);
9540c3983b2SBen Gras }
9550c3983b2SBen Gras 
9560c3983b2SBen Gras /*
9570c3983b2SBen Gras  * onsig --
9580c3983b2SBen Gras  *	If signaled, delete the temporary files.
9590c3983b2SBen Gras  */
9600c3983b2SBen Gras static void
onsig(int signo)9610c3983b2SBen Gras onsig(int signo)
9620c3983b2SBen Gras {
9630c3983b2SBen Gras 
9640c3983b2SBen Gras 	(void)cleanup();
9650c3983b2SBen Gras 
9660c3983b2SBen Gras 	(void)raise_default_signal(signo);
9670c3983b2SBen Gras 
9680c3983b2SBen Gras 	/* NOTREACHED */
9690c3983b2SBen Gras 	exit(EXIT_FAILURE);
9700c3983b2SBen Gras }
9710c3983b2SBen Gras 
9720c3983b2SBen Gras /*
9730c3983b2SBen Gras  * cleanup --
9740c3983b2SBen Gras  *	Clean up temporary files, show any error messages.
9750c3983b2SBen Gras  */
9760c3983b2SBen Gras static int
cleanup(void)9770c3983b2SBen Gras cleanup(void)
9780c3983b2SBen Gras {
9790c3983b2SBen Gras 	TAG *intmpp, *missp;
9800c3983b2SBen Gras 	ENTRY *ep;
9810c3983b2SBen Gras 	int rval;
9820c3983b2SBen Gras 
9830c3983b2SBen Gras 	rval = EXIT_SUCCESS;
9840c3983b2SBen Gras 	/*
9850c3983b2SBen Gras 	 * note that _missing and _intmp were created by main(), so
9860c3983b2SBen Gras 	 * gettag() cannot return NULL here.
9870c3983b2SBen Gras 	 */
9880c3983b2SBen Gras 	missp = gettag("_missing", 0);	/* missing man pages */
9890c3983b2SBen Gras 	intmpp = gettag("_intmp", 0);	/* tmp files we need to unlink */
9900c3983b2SBen Gras 
9910c3983b2SBen Gras 	TAILQ_FOREACH(ep, &missp->entrylist, q) {
9920c3983b2SBen Gras 		warnx("no entry for %s in the manual.", ep->s);
9930c3983b2SBen Gras 		rval = EXIT_FAILURE;
9940c3983b2SBen Gras 	}
9950c3983b2SBen Gras 
9960c3983b2SBen Gras 	TAILQ_FOREACH(ep, &intmpp->entrylist, q)
9970c3983b2SBen Gras 		(void)unlink(ep->s);
9980c3983b2SBen Gras 
9990c3983b2SBen Gras 	return rval;
10000c3983b2SBen Gras }
10010c3983b2SBen Gras 
10020c3983b2SBen Gras static const char *
getclass(const char * machine)10030c3983b2SBen Gras getclass(const char *machine)
10040c3983b2SBen Gras {
10050c3983b2SBen Gras 	char buf[BUFSIZ];
10060c3983b2SBen Gras 	TAG *t;
10070c3983b2SBen Gras 	snprintf(buf, sizeof(buf), "_%s", machine);
10080c3983b2SBen Gras 	t = gettag(buf, 0);
10090c3983b2SBen Gras 	return t != NULL && !TAILQ_EMPTY(&t->entrylist) ?
10100c3983b2SBen Gras 	    TAILQ_FIRST(&t->entrylist)->s : NULL;
10110c3983b2SBen Gras }
10120c3983b2SBen Gras 
10130c3983b2SBen Gras static void
addpath(struct manstate * m,const char * dir,size_t len,const char * sub)10140c3983b2SBen Gras addpath(struct manstate *m, const char *dir, size_t len, const char *sub)
10150c3983b2SBen Gras {
10160c3983b2SBen Gras 	char buf[2 * MAXPATHLEN + 1];
10170c3983b2SBen Gras 	(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,%s%s%s}",
10180c3983b2SBen Gras 	     dir, (dir[len - 1] == '/') ? "" : "/", sub, m->machine,
10190c3983b2SBen Gras 	     m->machclass ? "/" : "", m->machclass ? m->machclass : "",
10200c3983b2SBen Gras 	     m->machclass ? "," : "");
10210c3983b2SBen Gras 	if (addentry(m->mymanpath, buf, 0) < 0)
10220c3983b2SBen Gras 		errx(EXIT_FAILURE, "malloc failed");
10230c3983b2SBen Gras }
10240c3983b2SBen Gras 
10250c3983b2SBen Gras /*
10260c3983b2SBen Gras  * usage --
10270c3983b2SBen Gras  *	print usage message and die
10280c3983b2SBen Gras  */
10290c3983b2SBen Gras static void
usage(void)10300c3983b2SBen Gras usage(void)
10310c3983b2SBen Gras {
10320c3983b2SBen Gras 	(void)fprintf(stderr, "Usage: %s [-acw|-h] [-C cfg] [-M path] "
10330c3983b2SBen Gras 	    "[-m path] [-S srch] [[-s] sect] name ...\n", getprogname());
10340c3983b2SBen Gras 	(void)fprintf(stderr,
10350c3983b2SBen Gras 	    "Usage: %s -k [-C cfg] [-M path] [-m path] keyword ...\n",
10360c3983b2SBen Gras 	    getprogname());
1037b9a6a72bSLionel Sambuc 	(void)fprintf(stderr, "Usage: %s -p\n", getprogname());
10380c3983b2SBen Gras 	exit(EXIT_FAILURE);
10390c3983b2SBen Gras }
1040b9a6a72bSLionel Sambuc 
1041b9a6a72bSLionel Sambuc /*
1042b9a6a72bSLionel Sambuc  * printmanpath --
1043b9a6a72bSLionel Sambuc  *	Prints a list of directories containing man pages.
1044b9a6a72bSLionel Sambuc  */
1045b9a6a72bSLionel Sambuc static void
printmanpath(struct manstate * m)1046b9a6a72bSLionel Sambuc printmanpath(struct manstate *m)
1047b9a6a72bSLionel Sambuc {
1048b9a6a72bSLionel Sambuc 	ENTRY *esubd;
1049b9a6a72bSLionel Sambuc 	char *defaultpath = NULL; /* _default tag value from man.conf. */
1050b9a6a72bSLionel Sambuc 	char *buf; /* for storing temporary values */
1051b9a6a72bSLionel Sambuc 	char **ap;
1052b9a6a72bSLionel Sambuc 	glob_t pg;
1053b9a6a72bSLionel Sambuc 	struct stat sb;
1054b9a6a72bSLionel Sambuc 	TAG *path = m->defaultpath;
1055b9a6a72bSLionel Sambuc 	TAG *subdirs = m->subdirs;
1056b9a6a72bSLionel Sambuc 
1057b9a6a72bSLionel Sambuc 	/* the tail queue is empty if no _default tag is defined in * man.conf */
1058b9a6a72bSLionel Sambuc 	if (TAILQ_EMPTY(&path->entrylist))
1059b9a6a72bSLionel Sambuc 		errx(EXIT_FAILURE, "Empty manpath");
1060b9a6a72bSLionel Sambuc 
1061b9a6a72bSLionel Sambuc 	defaultpath = TAILQ_LAST(&path->entrylist, tqh)->s;
1062b9a6a72bSLionel Sambuc 
1063b9a6a72bSLionel Sambuc 	if (glob(defaultpath, GLOB_BRACE | GLOB_NOSORT, NULL, &pg) != 0)
1064b9a6a72bSLionel Sambuc 		err(EXIT_FAILURE, "glob failed");
1065b9a6a72bSLionel Sambuc 
1066b9a6a72bSLionel Sambuc 	if (pg.gl_matchc == 0) {
1067b9a6a72bSLionel Sambuc 		warnx("Default path in %s doesn't exist", _PATH_MANCONF);
1068b9a6a72bSLionel Sambuc 		globfree(&pg);
1069b9a6a72bSLionel Sambuc 		return;
1070b9a6a72bSLionel Sambuc 	}
1071b9a6a72bSLionel Sambuc 
1072b9a6a72bSLionel Sambuc 	TAILQ_FOREACH(esubd, &subdirs->entrylist, q) {
1073b9a6a72bSLionel Sambuc 		/* Drop cat page directory, only sources are relevant. */
1074b9a6a72bSLionel Sambuc 		if (strncmp(esubd->s, "man", 3))
1075b9a6a72bSLionel Sambuc 			continue;
1076b9a6a72bSLionel Sambuc 
1077b9a6a72bSLionel Sambuc 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
1078b9a6a72bSLionel Sambuc 			if (asprintf(&buf, "%s%s", *ap, esubd->s) == -1)
1079b9a6a72bSLionel Sambuc 				err(EXIT_FAILURE, "memory allocation error");
1080b9a6a72bSLionel Sambuc 			/* Skip non-directories. */
1081b9a6a72bSLionel Sambuc 			if (stat(buf, &sb) == 0 && S_ISDIR(sb.st_mode))
1082b9a6a72bSLionel Sambuc 				printf("%s\n", buf);
1083b9a6a72bSLionel Sambuc 
1084b9a6a72bSLionel Sambuc 			free(buf);
1085b9a6a72bSLionel Sambuc 		}
1086b9a6a72bSLionel Sambuc 	}
1087b9a6a72bSLionel Sambuc 	globfree(&pg);
1088b9a6a72bSLionel Sambuc }
1089