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