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