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