Home | History | Annotate | Line # | Download | only in man
man.c revision 1.48
      1  1.48  christos /*	$NetBSD: man.c,v 1.48 2013/07/18 15:39:08 christos Exp $	*/
      2   1.7       tls 
      3   1.1       cgd /*
      4   1.7       tls  * Copyright (c) 1987, 1993, 1994, 1995
      5   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.30       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.9     mikel #include <sys/cdefs.h>
     33   1.9     mikel 
     34   1.1       cgd #ifndef lint
     35  1.37     lukem __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994, 1995\
     36  1.37     lukem  The Regents of the University of California.  All rights reserved.");
     37   1.1       cgd #endif /* not lint */
     38   1.1       cgd 
     39   1.1       cgd #ifndef lint
     40   1.7       tls #if 0
     41   1.7       tls static char sccsid[] = "@(#)man.c	8.17 (Berkeley) 1/31/95";
     42   1.7       tls #else
     43  1.48  christos __RCSID("$NetBSD: man.c,v 1.48 2013/07/18 15:39:08 christos Exp $");
     44   1.7       tls #endif
     45   1.1       cgd #endif /* not lint */
     46   1.1       cgd 
     47   1.1       cgd #include <sys/param.h>
     48   1.1       cgd #include <sys/queue.h>
     49  1.42     joerg #include <sys/stat.h>
     50  1.22   tsutsui #include <sys/utsname.h>
     51   1.1       cgd 
     52   1.1       cgd #include <ctype.h>
     53   1.1       cgd #include <err.h>
     54   1.1       cgd #include <errno.h>
     55   1.1       cgd #include <fcntl.h>
     56   1.1       cgd #include <fnmatch.h>
     57   1.1       cgd #include <glob.h>
     58   1.1       cgd #include <signal.h>
     59   1.1       cgd #include <stdio.h>
     60   1.1       cgd #include <stdlib.h>
     61   1.1       cgd #include <string.h>
     62   1.1       cgd #include <unistd.h>
     63  1.36     lukem #include <util.h>
     64  1.40  christos #include <locale.h>
     65   1.1       cgd 
     66  1.29   thorpej #include "manconf.h"
     67   1.1       cgd #include "pathnames.h"
     68   1.1       cgd 
     69  1.33     chuck #ifndef MAN_DEBUG
     70  1.33     chuck #define MAN_DEBUG 0		/* debug path output */
     71  1.33     chuck #endif
     72   1.1       cgd 
     73  1.33     chuck /*
     74  1.33     chuck  * manstate: structure collecting the current global state so we can
     75  1.33     chuck  * easily identify it and pass it to helper functions in one arg.
     76  1.33     chuck  */
     77  1.33     chuck struct manstate {
     78  1.33     chuck 	/* command line flags */
     79  1.33     chuck 	int all;		/* -a: show all matches rather than first */
     80  1.33     chuck 	int cat;		/* -c: do not use a pager */
     81  1.33     chuck 	char *conffile;		/* -C: use alternate config file */
     82  1.33     chuck 	int how;		/* -h: show SYNOPSIS only */
     83  1.33     chuck 	char *manpath;		/* -M: alternate MANPATH */
     84  1.33     chuck 	char *addpath;		/* -m: add these dirs to front of manpath */
     85  1.33     chuck 	char *pathsearch;	/* -S: path of man must contain this string */
     86  1.33     chuck 	char *sectionname;	/* -s: limit search to a given man section */
     87  1.33     chuck 	int where;		/* -w: just show paths of all matching files */
     88  1.42     joerg 	int getpath;	/* -p: print the path of directories containing man pages */
     89  1.42     joerg 
     90  1.33     chuck 	/* important tags from the config file */
     91  1.33     chuck 	TAG *defaultpath;	/* _default: default MANPATH */
     92  1.33     chuck 	TAG *subdirs;		/* _subdir: default subdir search list */
     93  1.33     chuck 	TAG *suffixlist;	/* _suffix: for files that can be cat()'d */
     94  1.33     chuck 	TAG *buildlist;		/* _build: for files that must be built */
     95  1.33     chuck 
     96  1.33     chuck 	/* tags for internal use */
     97  1.33     chuck 	TAG *intmp;		/* _intmp: tmp files we must cleanup */
     98  1.33     chuck 	TAG *missinglist;	/* _missing: pages we couldn't find */
     99  1.33     chuck 	TAG *mymanpath;		/* _new_path: final version of MANPATH */
    100  1.33     chuck 	TAG *section;		/* <sec>: tag for m.sectionname */
    101  1.33     chuck 
    102  1.33     chuck 	/* other misc stuff */
    103  1.33     chuck 	const char *pager;	/* pager to use */
    104  1.45       uwe 	size_t pagerlen;	/* length of the above */
    105  1.40  christos 	const char *machine;	/* machine */
    106  1.40  christos 	const char *machclass;	/* machine class */
    107  1.33     chuck };
    108  1.33     chuck 
    109  1.33     chuck /*
    110  1.33     chuck  * prototypes
    111  1.33     chuck  */
    112  1.33     chuck static void	 build_page(char *, char **, struct manstate *);
    113  1.33     chuck static void	 cat(char *);
    114  1.33     chuck static const char	*check_pager(const char *);
    115  1.33     chuck static int	 cleanup(void);
    116  1.33     chuck static void	 how(char *);
    117  1.48  christos static void	 jump(char **, const char *, const char *);
    118  1.33     chuck static int	 manual(char *, struct manstate *, glob_t *);
    119  1.33     chuck static void	 onsig(int);
    120  1.40  christos static void	 usage(void) __attribute__((__noreturn__));
    121  1.40  christos static void	 addpath(struct manstate *, const char *, size_t, const char *);
    122  1.40  christos static const char *getclass(const char *);
    123  1.42     joerg static void printmanpath(struct manstate *);
    124   1.1       cgd 
    125  1.48  christos static char EMPTY[1];
    126  1.48  christos 
    127  1.33     chuck /*
    128  1.33     chuck  * main function
    129  1.33     chuck  */
    130   1.1       cgd int
    131  1.33     chuck main(int argc, char **argv)
    132   1.1       cgd {
    133  1.48  christos 	static struct manstate m;
    134  1.33     chuck 	int ch, abs_section, found;
    135  1.33     chuck 	ENTRY *esubd, *epath;
    136  1.40  christos 	char *p, **ap, *cmd;
    137  1.33     chuck 	size_t len;
    138   1.1       cgd 	glob_t pg;
    139   1.1       cgd 
    140  1.40  christos 	setprogname(argv[0]);
    141  1.40  christos 	setlocale(LC_ALL, "");
    142  1.33     chuck 	/*
    143  1.33     chuck 	 * parse command line...
    144  1.33     chuck 	 */
    145  1.42     joerg 	while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:ps:S:w")) != -1)
    146   1.1       cgd 		switch (ch) {
    147   1.1       cgd 		case 'a':
    148  1.33     chuck 			m.all = 1;
    149   1.1       cgd 			break;
    150   1.1       cgd 		case 'C':
    151  1.33     chuck 			m.conffile = optarg;
    152   1.1       cgd 			break;
    153   1.1       cgd 		case 'c':
    154  1.33     chuck 		case '-':	/* XXX: '-' is a deprecated version of '-c' */
    155  1.33     chuck 			m.cat = 1;
    156   1.1       cgd 			break;
    157   1.1       cgd 		case 'h':
    158  1.33     chuck 			m.how = 1;
    159   1.1       cgd 			break;
    160   1.1       cgd 		case 'm':
    161  1.33     chuck 			m.addpath = optarg;
    162   1.1       cgd 			break;
    163   1.1       cgd 		case 'M':
    164  1.33     chuck 		case 'P':	/* -P for backward compatibility */
    165  1.33     chuck 			m.manpath = strdup(optarg);
    166   1.1       cgd 			break;
    167  1.42     joerg 		case 'p':
    168  1.42     joerg 			m.getpath = 1;
    169  1.42     joerg 			break;
    170   1.1       cgd 		/*
    171   1.1       cgd 		 * The -f and -k options are backward compatible,
    172   1.1       cgd 		 * undocumented ways of calling whatis(1) and apropos(1).
    173   1.1       cgd 		 */
    174   1.1       cgd 		case 'f':
    175   1.1       cgd 			jump(argv, "-f", "whatis");
    176   1.1       cgd 			/* NOTREACHED */
    177   1.1       cgd 		case 'k':
    178   1.1       cgd 			jump(argv, "-k", "apropos");
    179   1.1       cgd 			/* NOTREACHED */
    180  1.25    simonb 		case 's':
    181  1.33     chuck 			if (m.sectionname != NULL)
    182  1.25    simonb 				usage();
    183  1.33     chuck 			m.sectionname = optarg;
    184  1.25    simonb 			break;
    185  1.23  jdolecek 		case 'S':
    186  1.33     chuck 			m.pathsearch = optarg;
    187  1.23  jdolecek 			break;
    188   1.1       cgd 		case 'w':
    189  1.33     chuck 			m.all = m.where = 1;
    190   1.1       cgd 			break;
    191   1.1       cgd 		case '?':
    192   1.1       cgd 		default:
    193   1.1       cgd 			usage();
    194   1.1       cgd 		}
    195   1.1       cgd 	argc -= optind;
    196   1.1       cgd 	argv += optind;
    197   1.1       cgd 
    198  1.42     joerg 	if (!m.getpath && !argc)
    199   1.1       cgd 		usage();
    200   1.1       cgd 
    201  1.33     chuck 	/*
    202  1.33     chuck 	 * read the configuration file and collect any other information
    203  1.33     chuck 	 * we will need (machine type, pager, section [if specified
    204  1.33     chuck 	 * without '-s'], and MANPATH through the environment).
    205  1.33     chuck 	 */
    206  1.33     chuck 	config(m.conffile);    /* exits on error ... */
    207   1.1       cgd 
    208  1.40  christos 	if ((m.machine = getenv("MACHINE")) == NULL) {
    209  1.22   tsutsui 		struct utsname utsname;
    210  1.22   tsutsui 
    211  1.40  christos 		if (uname(&utsname) == -1)
    212  1.40  christos 			err(EXIT_FAILURE, "uname");
    213  1.40  christos 		m.machine = utsname.machine;
    214  1.22   tsutsui 	}
    215   1.1       cgd 
    216  1.40  christos 	m.machclass = getclass(m.machine);
    217  1.40  christos 
    218  1.33     chuck 	if (!m.cat && !m.how && !m.where) {  /* if we need a pager ... */
    219  1.33     chuck 		if (!isatty(STDOUT_FILENO)) {
    220  1.33     chuck 			m.cat = 1;
    221  1.33     chuck 		} else {
    222  1.33     chuck 			if ((m.pager = getenv("PAGER")) != NULL &&
    223  1.33     chuck 			    m.pager[0] != '\0')
    224  1.33     chuck 				m.pager = check_pager(m.pager);
    225  1.33     chuck 			else
    226  1.33     chuck 				m.pager = _PATH_PAGER;
    227  1.33     chuck 			m.pagerlen = strlen(m.pager);
    228  1.33     chuck 		}
    229  1.33     chuck 	}
    230  1.33     chuck 
    231  1.33     chuck 	/* do we need to set m.section to a non-null value? */
    232  1.33     chuck 	if (m.sectionname) {
    233   1.1       cgd 
    234  1.33     chuck 		m.section = gettag(m.sectionname, 0); /* -s must be a section */
    235  1.33     chuck 		if (m.section == NULL)
    236  1.38    cegger 			errx(EXIT_FAILURE, "unknown section: %s", m.sectionname);
    237  1.23  jdolecek 
    238  1.33     chuck 	} else if (argc > 1) {
    239  1.33     chuck 
    240  1.33     chuck 		m.section = gettag(*argv, 0);  /* might be a section? */
    241  1.33     chuck 		if (m.section) {
    242  1.25    simonb 			argv++;
    243  1.25    simonb 			argc--;
    244  1.25    simonb 		}
    245   1.1       cgd 
    246  1.33     chuck 	}
    247  1.33     chuck 
    248  1.33     chuck 	if (m.manpath == NULL)
    249  1.33     chuck 		m.manpath = getenv("MANPATH"); /* note: -M overrides getenv */
    250  1.23  jdolecek 
    251  1.23  jdolecek 
    252  1.23  jdolecek 	/*
    253  1.33     chuck 	 * get default values from config file, plus create the tags we
    254  1.33     chuck 	 * use for keeping internal state.  make sure all our mallocs
    255  1.33     chuck 	 * go through.
    256  1.33     chuck 	 */
    257  1.33     chuck 	/* from cfg file */
    258  1.33     chuck 	m.defaultpath = gettag("_default", 1);
    259  1.33     chuck 	m.subdirs = gettag("_subdir", 1);
    260  1.33     chuck 	m.suffixlist = gettag("_suffix", 1);
    261  1.33     chuck 	m.buildlist = gettag("_build", 1);
    262  1.33     chuck 	/* internal use */
    263  1.33     chuck 	m.mymanpath = gettag("_new_path", 1);
    264  1.33     chuck 	m.missinglist = gettag("_missing", 1);
    265  1.33     chuck 	m.intmp = gettag("_intmp", 1);
    266  1.33     chuck 	if (!m.defaultpath || !m.subdirs || !m.suffixlist || !m.buildlist ||
    267  1.33     chuck 	    !m.mymanpath || !m.missinglist || !m.intmp)
    268  1.38    cegger 		errx(EXIT_FAILURE, "malloc failed");
    269  1.33     chuck 
    270  1.33     chuck 	/*
    271  1.33     chuck 	 * are we using a section whose elements are all absolute paths?
    272  1.33     chuck 	 * (we only need to look at the first entry on the section list,
    273  1.33     chuck 	 * as config() will ensure that any additional entries will match
    274  1.33     chuck 	 * the first one.)
    275  1.33     chuck 	 */
    276  1.33     chuck 	abs_section = (m.section != NULL &&
    277  1.33     chuck 		!TAILQ_EMPTY(&m.section->entrylist) &&
    278  1.33     chuck 	    		*(TAILQ_FIRST(&m.section->entrylist)->s) == '/');
    279  1.33     chuck 
    280  1.33     chuck 	/*
    281  1.33     chuck 	 * now that we have all the data we need, we must determine the
    282  1.33     chuck 	 * manpath we are going to use to find the requested entries using
    283  1.33     chuck 	 * the following steps...
    284  1.33     chuck 	 *
    285  1.33     chuck 	 * [1] if the user specified a section and that section's elements
    286  1.33     chuck 	 *     from the config file are all absolute paths, then we override
    287  1.33     chuck 	 *     defaultpath and -M/MANPATH with the section's absolute paths.
    288  1.23  jdolecek 	 */
    289  1.23  jdolecek 	if (abs_section) {
    290  1.33     chuck 		m.manpath = NULL;	   	/* ignore -M/MANPATH */
    291  1.33     chuck 		m.defaultpath = m.section;	/* overwrite _default path */
    292  1.33     chuck 		m.section = NULL;		/* promoted to defaultpath */
    293   1.1       cgd 	}
    294   1.1       cgd 
    295  1.23  jdolecek 	/*
    296  1.33     chuck 	 * [2] section can now only be non-null if the user asked for
    297  1.33     chuck 	 *     a section and that section's elements did not have
    298  1.33     chuck          *     absolute paths.  in this case we use the section's
    299  1.33     chuck 	 *     elements to override _subdir from the config file.
    300  1.33     chuck 	 *
    301  1.33     chuck 	 * after this step, we are done processing "m.section"...
    302  1.23  jdolecek 	 */
    303  1.33     chuck 	if (m.section)
    304  1.33     chuck 		m.subdirs = m.section;
    305  1.23  jdolecek 
    306   1.1       cgd 	/*
    307  1.33     chuck 	 * [3] we need to setup the path we want to use (m.mymanpath).
    308  1.33     chuck 	 *     if the user gave us a path (m.manpath) use it, otherwise
    309  1.33     chuck 	 *     go with the default.   in either case we need to append
    310  1.33     chuck 	 *     the subdir and machine spec to each element of the path.
    311  1.23  jdolecek 	 *
    312  1.33     chuck 	 *     for absolute section paths that come from the config file,
    313  1.33     chuck 	 *     we only append the subdir spec if the path ends in
    314  1.33     chuck 	 *     a '/' --- elements that do not end in '/' are assumed to
    315  1.33     chuck 	 *     not have subdirectories.  this is mainly for backward compat,
    316  1.33     chuck 	 *     but it allows non-subdir configs like:
    317  1.33     chuck 	 *	sect3       /usr/share/man/{old/,}cat3
    318  1.33     chuck 	 *	doc         /usr/{pkg,share}/doc/{sendmail/op,sendmail/intro}
    319  1.33     chuck 	 *
    320  1.33     chuck 	 *     note that we try and be careful to not put double slashes
    321  1.33     chuck 	 *     in the path (e.g. we want /usr/share/man/man1, not
    322  1.33     chuck 	 *     /usr/share/man//man1) because "more" will put the filename
    323  1.33     chuck 	 *     we generate in its prompt and the double slashes look ugly.
    324  1.33     chuck 	 */
    325  1.33     chuck 	if (m.manpath) {
    326  1.33     chuck 
    327  1.33     chuck 		/* note: strtok is going to destroy m.manpath */
    328  1.33     chuck 		for (p = strtok(m.manpath, ":") ; p ; p = strtok(NULL, ":")) {
    329  1.33     chuck 			len = strlen(p);
    330  1.33     chuck 			if (len < 1)
    331  1.33     chuck 				continue;
    332  1.40  christos 			TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q)
    333  1.40  christos 				addpath(&m, p, len, esubd->s);
    334   1.1       cgd 		}
    335  1.33     chuck 
    336  1.23  jdolecek 	} else {
    337  1.23  jdolecek 
    338  1.33     chuck 		TAILQ_FOREACH(epath, &m.defaultpath->entrylist, q) {
    339  1.23  jdolecek 			/* handle trailing "/" magic here ... */
    340  1.40  christos 		  	if (abs_section && epath->s[epath->len - 1] != '/') {
    341  1.40  christos 				addpath(&m, "", 1, epath->s);
    342   1.1       cgd 				continue;
    343   1.1       cgd 			}
    344  1.23  jdolecek 
    345  1.40  christos 			TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q)
    346  1.40  christos 				addpath(&m, epath->s, epath->len, esubd->s);
    347  1.23  jdolecek 		}
    348  1.23  jdolecek 
    349  1.33     chuck 	}
    350  1.23  jdolecek 
    351  1.23  jdolecek 	/*
    352  1.33     chuck 	 * [4] finally, prepend the "-m" m.addpath to mymanpath if it
    353  1.33     chuck 	 *     was specified.   subdirs and machine are always applied to
    354  1.33     chuck 	 *     m.addpath.
    355  1.33     chuck 	 */
    356  1.33     chuck 	if (m.addpath) {
    357  1.33     chuck 
    358  1.33     chuck 		/* note: strtok is going to destroy m.addpath */
    359  1.33     chuck 		for (p = strtok(m.addpath, ":") ; p ; p = strtok(NULL, ":")) {
    360  1.33     chuck 			len = strlen(p);
    361  1.33     chuck 			if (len < 1)
    362  1.33     chuck 				continue;
    363  1.40  christos 			TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q)
    364  1.40  christos 				addpath(&m, p, len, esubd->s);
    365   1.1       cgd 		}
    366  1.33     chuck 
    367   1.1       cgd 	}
    368   1.1       cgd 
    369  1.42     joerg 	if (m.getpath)
    370  1.42     joerg 		printmanpath(&m);
    371  1.42     joerg 
    372  1.33     chuck 	/*
    373  1.33     chuck 	 * now m.mymanpath is complete!
    374  1.33     chuck 	 */
    375  1.35     jwise #if MAN_DEBUG
    376  1.35     jwise 	printf("mymanpath:\n");
    377  1.35     jwise 	TAILQ_FOREACH(epath, &m.mymanpath->entrylist, q) {
    378  1.35     jwise 		printf("\t%s\n", epath->s);
    379  1.33     chuck 	}
    380  1.35     jwise #endif
    381  1.23  jdolecek 
    382   1.1       cgd 	/*
    383  1.33     chuck 	 * start searching for matching files and format them if necessary.
    384  1.33     chuck 	 * setup an interrupt handler so that we can ensure that temporary
    385  1.33     chuck 	 * files go away.
    386   1.1       cgd 	 */
    387   1.1       cgd 	(void)signal(SIGINT, onsig);
    388   1.1       cgd 	(void)signal(SIGHUP, onsig);
    389  1.17     itohy 	(void)signal(SIGPIPE, onsig);
    390   1.1       cgd 
    391   1.1       cgd 	memset(&pg, 0, sizeof(pg));
    392   1.1       cgd 	for (found = 0; *argv; ++argv)
    393  1.33     chuck 		if (manual(*argv, &m, &pg)) {
    394   1.1       cgd 			found = 1;
    395  1.33     chuck 		}
    396   1.1       cgd 
    397  1.33     chuck 	/* if nothing found, we're done. */
    398   1.1       cgd 	if (!found) {
    399   1.1       cgd 		(void)cleanup();
    400  1.38    cegger 		exit(EXIT_FAILURE);
    401   1.1       cgd 	}
    402   1.1       cgd 
    403  1.33     chuck 	/*
    404  1.33     chuck 	 * handle the simple display cases first (m.cat, m.how, m.where)
    405  1.33     chuck 	 */
    406  1.33     chuck 	if (m.cat) {
    407   1.1       cgd 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    408   1.1       cgd 			if (**ap == '\0')
    409   1.1       cgd 				continue;
    410   1.1       cgd 			cat(*ap);
    411   1.1       cgd 		}
    412  1.38    cegger 		exit(cleanup());
    413   1.1       cgd 	}
    414  1.33     chuck 	if (m.how) {
    415   1.1       cgd 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    416   1.1       cgd 			if (**ap == '\0')
    417   1.1       cgd 				continue;
    418   1.1       cgd 			how(*ap);
    419   1.1       cgd 		}
    420   1.1       cgd 		exit(cleanup());
    421   1.1       cgd 	}
    422  1.33     chuck 	if (m.where) {
    423   1.1       cgd 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    424   1.1       cgd 			if (**ap == '\0')
    425   1.1       cgd 				continue;
    426   1.1       cgd 			(void)printf("%s\n", *ap);
    427   1.1       cgd 		}
    428   1.1       cgd 		exit(cleanup());
    429   1.1       cgd 	}
    430   1.1       cgd 
    431   1.1       cgd 	/*
    432  1.33     chuck 	 * normal case - we display things in a single command, so
    433  1.33     chuck          * build a list of things to display.  first compute total
    434  1.33     chuck 	 * length of buffer we will need so we can malloc it.
    435   1.1       cgd 	 */
    436  1.33     chuck 	for (ap = pg.gl_pathv, len = m.pagerlen + 1; *ap != NULL; ++ap) {
    437   1.1       cgd 		if (**ap == '\0')
    438   1.1       cgd 			continue;
    439   1.1       cgd 		len += strlen(*ap) + 1;
    440   1.1       cgd 	}
    441   1.1       cgd 	if ((cmd = malloc(len)) == NULL) {
    442   1.9     mikel 		warn("malloc");
    443   1.1       cgd 		(void)cleanup();
    444  1.38    cegger 		exit(EXIT_FAILURE);
    445   1.1       cgd 	}
    446  1.33     chuck 
    447  1.33     chuck 	/* now build the command string... */
    448   1.1       cgd 	p = cmd;
    449  1.33     chuck 	len = m.pagerlen;
    450  1.33     chuck 	memcpy(p, m.pager, len);
    451   1.1       cgd 	p += len;
    452   1.1       cgd 	*p++ = ' ';
    453   1.1       cgd 	for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    454   1.1       cgd 		if (**ap == '\0')
    455   1.1       cgd 			continue;
    456   1.1       cgd 		len = strlen(*ap);
    457  1.33     chuck 		memcpy(p, *ap, len);
    458   1.1       cgd 		p += len;
    459   1.1       cgd 		*p++ = ' ';
    460   1.1       cgd 	}
    461  1.14  wsanchez 	*--p = '\0';
    462   1.1       cgd 
    463   1.1       cgd 	/* Use system(3) in case someone's pager is "pager arg1 arg2". */
    464   1.1       cgd 	(void)system(cmd);
    465   1.1       cgd 
    466   1.1       cgd 	exit(cleanup());
    467   1.1       cgd }
    468   1.1       cgd 
    469  1.48  christos static void
    470  1.48  christos fixstring(char *buf, size_t len, const char *fmt, const char *str)
    471  1.48  christos {
    472  1.48  christos 	const char *ptr = strstr(fmt, "%s");
    473  1.48  christos 	size_t l;
    474  1.48  christos 	if (ptr == NULL) {
    475  1.48  christos 		strlcpy(buf, fmt, len);
    476  1.48  christos 		return;
    477  1.48  christos 	}
    478  1.48  christos 	l = (size_t)(ptr - fmt) + 1;
    479  1.48  christos 	strlcpy(buf, fmt, MIN(l, len));
    480  1.48  christos 	strlcat(buf, str, len);
    481  1.48  christos 	strlcat(buf, ptr + 2, len);
    482  1.48  christos }
    483  1.48  christos 
    484  1.39    cegger static int
    485  1.39    cegger manual_find_buildkeyword(char *escpage, const char *fmt,
    486  1.39    cegger 	struct manstate *mp, glob_t *pg, size_t cnt)
    487  1.39    cegger {
    488  1.39    cegger 	ENTRY *suffix;
    489  1.39    cegger 	int found;
    490  1.39    cegger 	char *p, buf[MAXPATHLEN];
    491  1.39    cegger 
    492  1.39    cegger 	found = 0;
    493  1.39    cegger 	/* Try the _build key words next. */
    494  1.39    cegger 	TAILQ_FOREACH(suffix, &mp->buildlist->entrylist, q) {
    495  1.39    cegger 		for (p = suffix->s;
    496  1.39    cegger 		    *p != '\0' && !isspace((unsigned char)*p);
    497  1.39    cegger 		    ++p)
    498  1.39    cegger 			continue;
    499  1.39    cegger 		if (*p == '\0')
    500  1.39    cegger 			continue;
    501  1.39    cegger 
    502  1.39    cegger 		*p = '\0';
    503  1.48  christos 		fixstring(buf, sizeof(buf), fmt, escpage);
    504  1.39    cegger 		if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    505  1.39    cegger 			if (!mp->where)
    506  1.39    cegger 				build_page(p + 1, &pg->gl_pathv[cnt], mp);
    507  1.39    cegger 			*p = ' ';
    508  1.39    cegger 			found = 1;
    509  1.39    cegger 			break;
    510  1.39    cegger 		}
    511  1.39    cegger 		*p = ' ';
    512  1.39    cegger 	}
    513  1.39    cegger 
    514  1.39    cegger 	return found;
    515  1.39    cegger }
    516  1.39    cegger 
    517   1.1       cgd /*
    518   1.1       cgd  * manual --
    519   1.1       cgd  *	Search the manuals for the pages.
    520   1.1       cgd  */
    521   1.1       cgd static int
    522  1.33     chuck manual(char *page, struct manstate *mp, glob_t *pg)
    523   1.1       cgd {
    524  1.33     chuck 	ENTRY *suffix, *mdir;
    525  1.32  christos 	int anyfound, error, found;
    526  1.32  christos 	size_t cnt;
    527  1.27      groo 	char *p, buf[MAXPATHLEN], *escpage, *eptr;
    528  1.27      groo 	static const char escglob[] = "\\~?*{}[]";
    529   1.1       cgd 
    530   1.1       cgd 	anyfound = 0;
    531   1.1       cgd 
    532  1.27      groo 	/*
    533  1.27      groo 	 * Fixup page which may contain glob(3) special characters, e.g.
    534  1.27      groo 	 * the famous "No man page for [" FAQ.
    535  1.27      groo 	 */
    536  1.27      groo 	if ((escpage = malloc((2 * strlen(page)) + 1)) == NULL) {
    537  1.27      groo 		warn("malloc");
    538  1.27      groo 		(void)cleanup();
    539  1.38    cegger 		exit(EXIT_FAILURE);
    540  1.27      groo 	}
    541  1.27      groo 
    542  1.27      groo 	p = page;
    543  1.27      groo 	eptr = escpage;
    544  1.27      groo 
    545  1.27      groo 	while (*p) {
    546  1.27      groo 		if (strchr(escglob, *p) != NULL) {
    547  1.27      groo 			*eptr++ = '\\';
    548  1.27      groo 			*eptr++ = *p++;
    549  1.27      groo 		} else
    550  1.27      groo 			*eptr++ = *p++;
    551  1.27      groo 	}
    552  1.27      groo 
    553  1.27      groo 	*eptr = '\0';
    554  1.27      groo 
    555  1.39    cegger 	/*
    556  1.39    cegger 	 * If 'page' is given with a full or relative path
    557  1.39    cegger 	 * then interpret it as a file specification.
    558  1.39    cegger 	 */
    559  1.39    cegger 	if ((page[0] == '/') || (page[0] == '.')) {
    560  1.39    cegger 		/* check if file actually exists */
    561  1.39    cegger 		(void)strlcpy(buf, escpage, sizeof(buf));
    562  1.39    cegger 		error = glob(buf, GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg);
    563  1.39    cegger 		if (error != 0) {
    564  1.39    cegger 			if (error == GLOB_NOMATCH) {
    565  1.39    cegger 				goto notfound;
    566  1.39    cegger 			} else {
    567  1.39    cegger 				errx(EXIT_FAILURE, "glob failed");
    568  1.39    cegger 			}
    569  1.39    cegger 		}
    570  1.39    cegger 
    571  1.39    cegger 		if (pg->gl_matchc == 0)
    572  1.39    cegger 			goto notfound;
    573  1.39    cegger 
    574  1.39    cegger 		/* clip suffix for the suffix check below */
    575  1.39    cegger 		p = strrchr(escpage, '.');
    576  1.39    cegger 		if (p && p[0] == '.' && isdigit((unsigned char)p[1]))
    577  1.39    cegger 			p[0] = '\0';
    578  1.39    cegger 
    579  1.39    cegger 		found = 0;
    580  1.39    cegger 		for (cnt = pg->gl_pathc - pg->gl_matchc;
    581  1.39    cegger 		    cnt < pg->gl_pathc; ++cnt)
    582  1.39    cegger 		{
    583  1.39    cegger 			found = manual_find_buildkeyword(escpage, "%s%s",
    584  1.39    cegger 				mp, pg, cnt);
    585  1.39    cegger 			if (found) {
    586  1.39    cegger 				anyfound = 1;
    587  1.39    cegger 				if (!mp->all) {
    588  1.39    cegger 					/* Delete any other matches. */
    589  1.39    cegger 					while (++cnt< pg->gl_pathc)
    590  1.48  christos 						pg->gl_pathv[cnt] = EMPTY;
    591  1.39    cegger 					break;
    592  1.39    cegger 				}
    593  1.39    cegger 				continue;
    594  1.39    cegger 			}
    595  1.39    cegger 
    596  1.39    cegger 			/* It's not a man page, forget about it. */
    597  1.48  christos 			pg->gl_pathv[cnt] = EMPTY;
    598  1.39    cegger 		}
    599  1.39    cegger 
    600  1.39    cegger   notfound:
    601  1.39    cegger 		if (!anyfound) {
    602  1.39    cegger 			if (addentry(mp->missinglist, page, 0) < 0) {
    603  1.39    cegger 				warn("malloc");
    604  1.39    cegger 				(void)cleanup();
    605  1.39    cegger 				exit(EXIT_FAILURE);
    606  1.39    cegger 			}
    607  1.39    cegger 		}
    608  1.39    cegger 		free(escpage);
    609  1.39    cegger 		return anyfound;
    610  1.39    cegger 	}
    611  1.39    cegger 
    612  1.33     chuck 	/* For each man directory in mymanpath ... */
    613  1.33     chuck 	TAILQ_FOREACH(mdir, &mp->mymanpath->entrylist, q) {
    614  1.33     chuck 
    615  1.33     chuck 		/*
    616  1.33     chuck 		 * use glob(3) to look in the filesystem for matching files.
    617  1.33     chuck 		 * match any suffix here, as we will check that later.
    618  1.33     chuck 		 */
    619  1.33     chuck 		(void)snprintf(buf, sizeof(buf), "%s/%s.*", mdir->s, escpage);
    620  1.12    kleink 		if ((error = glob(buf,
    621  1.12    kleink 		    GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) {
    622  1.12    kleink 			if (error == GLOB_NOMATCH)
    623  1.12    kleink 				continue;
    624  1.12    kleink 			else {
    625  1.12    kleink 				warn("globbing");
    626  1.12    kleink 				(void)cleanup();
    627  1.38    cegger 				exit(EXIT_FAILURE);
    628  1.12    kleink 			}
    629   1.1       cgd 		}
    630   1.1       cgd 		if (pg->gl_matchc == 0)
    631   1.1       cgd 			continue;
    632   1.1       cgd 
    633  1.33     chuck 		/*
    634  1.33     chuck 		 * start going through the matches glob(3) just found and
    635  1.33     chuck 		 * use m.pathsearch (if present) to filter out pages we
    636  1.33     chuck 		 * don't want.  then verify the suffix is valid, and build
    637  1.33     chuck 		 * the page if we have a _build suffix.
    638  1.33     chuck 		 */
    639   1.1       cgd 		for (cnt = pg->gl_pathc - pg->gl_matchc;
    640   1.1       cgd 		    cnt < pg->gl_pathc; ++cnt) {
    641   1.1       cgd 
    642  1.33     chuck 			/* filter on directory path name */
    643  1.33     chuck 			if (mp->pathsearch) {
    644  1.33     chuck 				p = strstr(pg->gl_pathv[cnt], mp->pathsearch);
    645  1.23  jdolecek 				if (!p || strchr(p, '/') == NULL) {
    646  1.48  christos 					pg->gl_pathv[cnt] = EMPTY; /* zap! */
    647  1.23  jdolecek 					continue;
    648  1.23  jdolecek 				}
    649  1.23  jdolecek 			}
    650  1.23  jdolecek 
    651   1.1       cgd 			/*
    652   1.1       cgd 			 * Try the _suffix key words first.
    653   1.1       cgd 			 *
    654   1.1       cgd 			 * XXX
    655   1.1       cgd 			 * Older versions of man.conf didn't have the suffix
    656   1.1       cgd 			 * key words, it was assumed that everything was a .0.
    657   1.1       cgd 			 * We just test for .0 first, it's fast and probably
    658   1.1       cgd 			 * going to hit.
    659   1.1       cgd 			 */
    660  1.27      groo 			(void)snprintf(buf, sizeof(buf), "*/%s.0", escpage);
    661   1.1       cgd 			if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
    662   1.3       cgd 				goto next;
    663   1.1       cgd 
    664  1.28     lukem 			found = 0;
    665  1.33     chuck 			TAILQ_FOREACH(suffix, &mp->suffixlist->entrylist, q) {
    666   1.1       cgd 				(void)snprintf(buf,
    667  1.27      groo 				     sizeof(buf), "*/%s%s", escpage,
    668  1.33     chuck 				     suffix->s);
    669   1.1       cgd 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    670   1.1       cgd 					found = 1;
    671   1.1       cgd 					break;
    672   1.1       cgd 				}
    673   1.1       cgd 			}
    674   1.3       cgd 			if (found)
    675   1.3       cgd 				goto next;
    676   1.1       cgd 
    677   1.1       cgd 			/* Try the _build key words next. */
    678  1.39    cegger 			found = manual_find_buildkeyword(escpage, "*/%s%s",
    679  1.39    cegger 				mp, pg, cnt);
    680   1.1       cgd 			if (found) {
    681   1.3       cgd next:				anyfound = 1;
    682  1.33     chuck 				if (!mp->all) {
    683   1.3       cgd 					/* Delete any other matches. */
    684   1.3       cgd 					while (++cnt< pg->gl_pathc)
    685  1.48  christos 						pg->gl_pathv[cnt] = EMPTY;
    686   1.1       cgd 					break;
    687   1.3       cgd 				}
    688   1.1       cgd 				continue;
    689   1.1       cgd 			}
    690   1.1       cgd 
    691   1.1       cgd 			/* It's not a man page, forget about it. */
    692  1.48  christos 			pg->gl_pathv[cnt] = EMPTY;
    693   1.1       cgd 		}
    694   1.1       cgd 
    695  1.33     chuck 		if (anyfound && !mp->all)
    696   1.1       cgd 			break;
    697   1.1       cgd 	}
    698   1.1       cgd 
    699   1.1       cgd 	/* If not found, enter onto the missing list. */
    700   1.1       cgd 	if (!anyfound) {
    701  1.33     chuck 		if (addentry(mp->missinglist, page, 0) < 0) {
    702   1.9     mikel 			warn("malloc");
    703   1.1       cgd 			(void)cleanup();
    704  1.38    cegger 			exit(EXIT_FAILURE);
    705   1.1       cgd 		}
    706   1.1       cgd 	}
    707  1.27      groo 
    708  1.27      groo 	free(escpage);
    709  1.38    cegger 	return anyfound;
    710   1.1       cgd }
    711   1.1       cgd 
    712   1.1       cgd /*
    713   1.1       cgd  * build_page --
    714   1.1       cgd  *	Build a man page for display.
    715   1.1       cgd  */
    716   1.1       cgd static void
    717  1.33     chuck build_page(char *fmt, char **pathp, struct manstate *mp)
    718   1.1       cgd {
    719   1.1       cgd 	static int warned;
    720  1.48  christos 	int olddir, fd, n;
    721  1.48  christos 	size_t tmpdirlen;
    722   1.4       jtc 	char *p, *b;
    723  1.19    kleink 	char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
    724  1.19    kleink 	const char *tmpdir;
    725   1.1       cgd 
    726   1.1       cgd 	/* Let the user know this may take awhile. */
    727   1.1       cgd 	if (!warned) {
    728   1.1       cgd 		warned = 1;
    729   1.1       cgd 		warnx("Formatting manual page...");
    730   1.1       cgd 	}
    731   1.1       cgd 
    732   1.7       tls        /*
    733   1.7       tls         * Historically man chdir'd to the root of the man tree.
    734   1.7       tls         * This was used in man pages that contained relative ".so"
    735   1.7       tls         * directives (including other man pages for command aliases etc.)
    736   1.7       tls         * It even went one step farther, by examining the first line
    737   1.7       tls         * of the man page and parsing the .so filename so it would
    738   1.7       tls         * make hard(?) links to the cat'ted man pages for space savings.
    739   1.7       tls         * (We don't do that here, but we could).
    740   1.7       tls         */
    741   1.7       tls 
    742   1.7       tls        /* copy and find the end */
    743   1.7       tls        for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
    744   1.7       tls                continue;
    745   1.7       tls 
    746  1.33     chuck 	/*
    747  1.33     chuck 	 * skip the last two path components, page name and man[n] ...
    748  1.33     chuck 	 * (e.g. buf will be "/usr/share/man" and p will be "man1/man.1")
    749  1.33     chuck 	 * we also save a pointer to our current directory so that we
    750  1.33     chuck 	 * can fchdir() back to it.  this allows relative MANDIR paths
    751  1.33     chuck 	 * to work with multiple man pages... e.g. consider:
    752  1.33     chuck 	 *   	cd /usr/share && man -M ./man cat ls
    753  1.33     chuck 	 * when no "cat1" subdir files are present.
    754  1.33     chuck 	 */
    755  1.33     chuck 	olddir = -1;
    756  1.10        tv 	for (--b, --p, n = 2; b != buf; b--, p--)
    757  1.10        tv 		if (*b == '/')
    758  1.10        tv 			if (--n == 0) {
    759  1.10        tv 				*b = '\0';
    760  1.33     chuck 				olddir = open(".", O_RDONLY);
    761  1.10        tv 				(void) chdir(buf);
    762  1.10        tv 				p++;
    763  1.10        tv 				break;
    764  1.10        tv 			}
    765   1.4       jtc 
    766   1.4       jtc 
    767  1.33     chuck 	/* advance fmt pass the suffix spec to the printf format string */
    768  1.15  christos 	for (; *fmt && isspace((unsigned char)*fmt); ++fmt)
    769  1.15  christos 		continue;
    770   1.1       cgd 
    771   1.1       cgd 	/*
    772   1.1       cgd 	 * Get a temporary file and build a version of the file
    773   1.1       cgd 	 * to display.  Replace the old file name with the new one.
    774   1.1       cgd 	 */
    775  1.19    kleink 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    776  1.19    kleink 		tmpdir = _PATH_TMP;
    777  1.33     chuck 	tmpdirlen = strlen(tmpdir);
    778  1.33     chuck 	(void)snprintf(tpath, sizeof (tpath), "%s%s%s", tmpdir,
    779  1.33     chuck 	    (tmpdirlen && tmpdir[tmpdirlen-1] == '/') ? "" : "/", TMPFILE);
    780   1.1       cgd 	if ((fd = mkstemp(tpath)) == -1) {
    781   1.1       cgd 		warn("%s", tpath);
    782   1.1       cgd 		(void)cleanup();
    783  1.38    cegger 		exit(EXIT_FAILURE);
    784   1.1       cgd 	}
    785   1.1       cgd 	(void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
    786  1.48  christos 	fixstring(cmd, sizeof(cmd), buf, p);
    787   1.1       cgd 	(void)system(cmd);
    788   1.1       cgd 	(void)close(fd);
    789   1.1       cgd 	if ((*pathp = strdup(tpath)) == NULL) {
    790   1.9     mikel 		warn("malloc");
    791   1.1       cgd 		(void)cleanup();
    792  1.38    cegger 		exit(EXIT_FAILURE);
    793   1.1       cgd 	}
    794   1.1       cgd 
    795   1.1       cgd 	/* Link the built file into the remove-when-done list. */
    796  1.33     chuck 	if (addentry(mp->intmp, *pathp, 0) < 0) {
    797   1.9     mikel 		warn("malloc");
    798   1.1       cgd 		(void)cleanup();
    799  1.38    cegger 		exit(EXIT_FAILURE);
    800   1.1       cgd 	}
    801  1.33     chuck 
    802  1.33     chuck 	/* restore old directory so relative manpaths still work */
    803  1.33     chuck 	if (olddir != -1) {
    804  1.33     chuck 		fchdir(olddir);
    805  1.33     chuck 		close(olddir);
    806  1.33     chuck 	}
    807   1.1       cgd }
    808   1.1       cgd 
    809   1.1       cgd /*
    810   1.1       cgd  * how --
    811   1.1       cgd  *	display how information
    812   1.1       cgd  */
    813   1.1       cgd static void
    814  1.33     chuck how(char *fname)
    815   1.1       cgd {
    816   1.1       cgd 	FILE *fp;
    817   1.1       cgd 
    818   1.1       cgd 	int lcnt, print;
    819   1.1       cgd 	char *p, buf[256];
    820   1.1       cgd 
    821   1.1       cgd 	if (!(fp = fopen(fname, "r"))) {
    822   1.1       cgd 		warn("%s", fname);
    823   1.1       cgd 		(void)cleanup();
    824  1.38    cegger 		exit(EXIT_FAILURE);
    825   1.1       cgd 	}
    826   1.1       cgd #define	S1	"SYNOPSIS"
    827   1.1       cgd #define	S2	"S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
    828   1.1       cgd #define	D1	"DESCRIPTION"
    829   1.1       cgd #define	D2	"D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
    830   1.1       cgd 	for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
    831   1.1       cgd 		if (!strncmp(buf, S1, sizeof(S1) - 1) ||
    832   1.1       cgd 		    !strncmp(buf, S2, sizeof(S2) - 1)) {
    833   1.1       cgd 			print = 1;
    834   1.1       cgd 			continue;
    835   1.1       cgd 		} else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
    836  1.34  christos 		    !strncmp(buf, D2, sizeof(D2) - 1)) {
    837  1.34  christos 			if (fp)
    838  1.34  christos 				(void)fclose(fp);
    839   1.1       cgd 			return;
    840  1.34  christos 		}
    841   1.1       cgd 		if (!print)
    842   1.1       cgd 			continue;
    843   1.1       cgd 		if (*buf == '\n')
    844   1.1       cgd 			++lcnt;
    845   1.1       cgd 		else {
    846   1.1       cgd 			for(; lcnt; --lcnt)
    847   1.1       cgd 				(void)putchar('\n');
    848  1.15  christos 			for (p = buf; isspace((unsigned char)*p); ++p)
    849  1.15  christos 				continue;
    850   1.1       cgd 			(void)fputs(p, stdout);
    851   1.1       cgd 		}
    852   1.1       cgd 	}
    853   1.1       cgd 	(void)fclose(fp);
    854   1.1       cgd }
    855   1.1       cgd 
    856   1.1       cgd /*
    857   1.1       cgd  * cat --
    858   1.1       cgd  *	cat out the file
    859   1.1       cgd  */
    860   1.1       cgd static void
    861  1.33     chuck cat(char *fname)
    862   1.1       cgd {
    863  1.48  christos 	int fd;
    864  1.48  christos 	ssize_t n;
    865   1.1       cgd 	char buf[2048];
    866   1.1       cgd 
    867   1.1       cgd 	if ((fd = open(fname, O_RDONLY, 0)) < 0) {
    868   1.1       cgd 		warn("%s", fname);
    869   1.1       cgd 		(void)cleanup();
    870  1.38    cegger 		exit(EXIT_FAILURE);
    871   1.1       cgd 	}
    872   1.1       cgd 	while ((n = read(fd, buf, sizeof(buf))) > 0)
    873  1.48  christos 		if (write(STDOUT_FILENO, buf, (size_t)n) != n) {
    874   1.1       cgd 			warn("write");
    875   1.1       cgd 			(void)cleanup();
    876  1.38    cegger 			exit(EXIT_FAILURE);
    877   1.1       cgd 		}
    878   1.1       cgd 	if (n == -1) {
    879   1.1       cgd 		warn("read");
    880   1.1       cgd 		(void)cleanup();
    881  1.38    cegger 		exit(EXIT_FAILURE);
    882   1.1       cgd 	}
    883   1.1       cgd 	(void)close(fd);
    884   1.1       cgd }
    885   1.1       cgd 
    886   1.1       cgd /*
    887   1.1       cgd  * check_pager --
    888   1.1       cgd  *	check the user supplied page information
    889   1.1       cgd  */
    890  1.23  jdolecek static const char *
    891  1.33     chuck check_pager(const char *name)
    892   1.1       cgd {
    893  1.24   thorpej 	const char *p;
    894   1.1       cgd 
    895   1.1       cgd 	/*
    896   1.1       cgd 	 * if the user uses "more", we make it "more -s"; watch out for
    897   1.1       cgd 	 * PAGER = "mypager /usr/ucb/more"
    898   1.1       cgd 	 */
    899  1.15  christos 	for (p = name; *p && !isspace((unsigned char)*p); ++p)
    900  1.15  christos 		continue;
    901   1.1       cgd 	for (; p > name && *p != '/'; --p);
    902   1.1       cgd 	if (p != name)
    903   1.1       cgd 		++p;
    904   1.1       cgd 
    905   1.1       cgd 	/* make sure it's "more", not "morex" */
    906  1.15  christos 	if (!strncmp(p, "more", 4) && (!p[4] || isspace((unsigned char)p[4]))){
    907  1.23  jdolecek 		char *newname;
    908  1.24   thorpej 		(void)asprintf(&newname, "%s %s", p, "-s");
    909  1.23  jdolecek 		name = newname;
    910   1.1       cgd 	}
    911  1.23  jdolecek 
    912  1.38    cegger 	return name;
    913   1.1       cgd }
    914   1.1       cgd 
    915   1.1       cgd /*
    916   1.1       cgd  * jump --
    917   1.1       cgd  *	strip out flag argument and jump
    918   1.1       cgd  */
    919   1.1       cgd static void
    920  1.48  christos jump(char **argv, const char *flag, const char *name)
    921   1.1       cgd {
    922   1.1       cgd 	char **arg;
    923   1.1       cgd 
    924  1.48  christos 	argv[0] = __UNCONST(name);
    925   1.1       cgd 	for (arg = argv + 1; *arg; ++arg)
    926   1.1       cgd 		if (!strcmp(*arg, flag))
    927   1.1       cgd 			break;
    928   1.1       cgd 	for (; *arg; ++arg)
    929   1.1       cgd 		arg[0] = arg[1];
    930   1.1       cgd 	execvp(name, argv);
    931  1.40  christos 	err(EXIT_FAILURE, "Cannot execute `%s'", name);
    932   1.1       cgd }
    933   1.1       cgd 
    934   1.1       cgd /*
    935   1.1       cgd  * onsig --
    936   1.1       cgd  *	If signaled, delete the temporary files.
    937   1.1       cgd  */
    938   1.1       cgd static void
    939  1.33     chuck onsig(int signo)
    940   1.1       cgd {
    941  1.18     itohy 
    942   1.1       cgd 	(void)cleanup();
    943   1.1       cgd 
    944  1.36     lukem 	(void)raise_default_signal(signo);
    945   1.1       cgd 
    946   1.1       cgd 	/* NOTREACHED */
    947  1.38    cegger 	exit(EXIT_FAILURE);
    948   1.1       cgd }
    949   1.1       cgd 
    950   1.1       cgd /*
    951   1.1       cgd  * cleanup --
    952   1.1       cgd  *	Clean up temporary files, show any error messages.
    953   1.1       cgd  */
    954   1.1       cgd static int
    955  1.38    cegger cleanup(void)
    956   1.1       cgd {
    957   1.1       cgd 	TAG *intmpp, *missp;
    958   1.1       cgd 	ENTRY *ep;
    959   1.1       cgd 	int rval;
    960   1.1       cgd 
    961  1.38    cegger 	rval = EXIT_SUCCESS;
    962  1.33     chuck 	/*
    963  1.33     chuck 	 * note that _missing and _intmp were created by main(), so
    964  1.33     chuck 	 * gettag() cannot return NULL here.
    965  1.33     chuck 	 */
    966  1.33     chuck 	missp = gettag("_missing", 0);	/* missing man pages */
    967  1.33     chuck 	intmpp = gettag("_intmp", 0);	/* tmp files we need to unlink */
    968  1.33     chuck 
    969  1.33     chuck 	TAILQ_FOREACH(ep, &missp->entrylist, q) {
    970  1.33     chuck 		warnx("no entry for %s in the manual.", ep->s);
    971  1.38    cegger 		rval = EXIT_FAILURE;
    972  1.33     chuck 	}
    973  1.33     chuck 
    974  1.33     chuck 	TAILQ_FOREACH(ep, &intmpp->entrylist, q)
    975  1.33     chuck 		(void)unlink(ep->s);
    976   1.1       cgd 
    977  1.38    cegger 	return rval;
    978   1.1       cgd }
    979   1.1       cgd 
    980  1.40  christos static const char *
    981  1.40  christos getclass(const char *machine)
    982  1.40  christos {
    983  1.40  christos 	char buf[BUFSIZ];
    984  1.40  christos 	TAG *t;
    985  1.40  christos 	snprintf(buf, sizeof(buf), "_%s", machine);
    986  1.40  christos 	t = gettag(buf, 0);
    987  1.41  christos 	return t != NULL && !TAILQ_EMPTY(&t->entrylist) ?
    988  1.41  christos 	    TAILQ_FIRST(&t->entrylist)->s : NULL;
    989  1.40  christos }
    990  1.40  christos 
    991  1.40  christos static void
    992  1.40  christos addpath(struct manstate *m, const char *dir, size_t len, const char *sub)
    993  1.40  christos {
    994  1.40  christos 	char buf[2 * MAXPATHLEN + 1];
    995  1.40  christos 	(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,%s%s%s}",
    996  1.40  christos 	     dir, (dir[len - 1] == '/') ? "" : "/", sub, m->machine,
    997  1.40  christos 	     m->machclass ? "/" : "", m->machclass ? m->machclass : "",
    998  1.40  christos 	     m->machclass ? "," : "");
    999  1.40  christos 	if (addentry(m->mymanpath, buf, 0) < 0)
   1000  1.40  christos 		errx(EXIT_FAILURE, "malloc failed");
   1001  1.40  christos }
   1002  1.40  christos 
   1003   1.1       cgd /*
   1004   1.1       cgd  * usage --
   1005   1.1       cgd  *	print usage message and die
   1006   1.1       cgd  */
   1007   1.1       cgd static void
   1008  1.38    cegger usage(void)
   1009   1.1       cgd {
   1010  1.40  christos 	(void)fprintf(stderr, "Usage: %s [-acw|-h] [-C cfg] [-M path] "
   1011  1.33     chuck 	    "[-m path] [-S srch] [[-s] sect] name ...\n", getprogname());
   1012  1.33     chuck 	(void)fprintf(stderr,
   1013  1.40  christos 	    "Usage: %s -k [-C cfg] [-M path] [-m path] keyword ...\n",
   1014  1.33     chuck 	    getprogname());
   1015  1.43       wiz 	(void)fprintf(stderr, "Usage: %s -p\n", getprogname());
   1016  1.38    cegger 	exit(EXIT_FAILURE);
   1017   1.1       cgd }
   1018  1.42     joerg 
   1019  1.42     joerg /*
   1020  1.42     joerg  * printmanpath --
   1021  1.42     joerg  *	Prints a list of directories containing man pages.
   1022  1.42     joerg  */
   1023  1.42     joerg static void
   1024  1.42     joerg printmanpath(struct manstate *m)
   1025  1.42     joerg {
   1026  1.42     joerg 	ENTRY *esubd;
   1027  1.42     joerg 	char *defaultpath = NULL; /* _default tag value from man.conf. */
   1028  1.42     joerg 	char *buf; /* for storing temporary values */
   1029  1.42     joerg 	char **ap;
   1030  1.42     joerg 	glob_t pg;
   1031  1.42     joerg 	struct stat sb;
   1032  1.42     joerg 	TAG *path = m->defaultpath;
   1033  1.42     joerg 	TAG *subdirs = m->subdirs;
   1034  1.42     joerg 
   1035  1.42     joerg 	/* the tail queue is empty if no _default tag is defined in * man.conf */
   1036  1.42     joerg 	if (TAILQ_EMPTY(&path->entrylist))
   1037  1.42     joerg 		errx(EXIT_FAILURE, "Empty manpath");
   1038  1.42     joerg 
   1039  1.42     joerg 	defaultpath = TAILQ_LAST(&path->entrylist, tqh)->s;
   1040  1.42     joerg 
   1041  1.42     joerg 	if (glob(defaultpath, GLOB_BRACE | GLOB_NOSORT, NULL, &pg) != 0)
   1042  1.42     joerg 		err(EXIT_FAILURE, "glob failed");
   1043  1.44     joerg 
   1044  1.44     joerg 	if (pg.gl_matchc == 0) {
   1045  1.44     joerg 		warnx("Default path in %s doesn't exist", _PATH_MANCONF);
   1046  1.44     joerg 		globfree(&pg);
   1047  1.44     joerg 		return;
   1048  1.44     joerg 	}
   1049  1.44     joerg 
   1050  1.42     joerg 	TAILQ_FOREACH(esubd, &subdirs->entrylist, q) {
   1051  1.42     joerg 		/* Drop cat page directory, only sources are relevant. */
   1052  1.42     joerg 		if (strncmp(esubd->s, "man", 3))
   1053  1.42     joerg 			continue;
   1054  1.42     joerg 
   1055  1.42     joerg 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
   1056  1.42     joerg 			if (asprintf(&buf, "%s%s", *ap, esubd->s) == -1)
   1057  1.42     joerg 				err(EXIT_FAILURE, "memory allocation error");
   1058  1.42     joerg 			/* Skip non-directories. */
   1059  1.42     joerg 			if (stat(buf, &sb) == 0 && S_ISDIR(sb.st_mode))
   1060  1.42     joerg 				printf("%s\n", buf);
   1061  1.42     joerg 
   1062  1.42     joerg 			free(buf);
   1063  1.42     joerg 		}
   1064  1.42     joerg 	}
   1065  1.42     joerg 	globfree(&pg);
   1066  1.42     joerg }
   1067