Home | History | Annotate | Line # | Download | only in makewhatis
makewhatis.c revision 1.48
      1  1.48  christos /*	$NetBSD: makewhatis.c,v 1.48 2013/06/24 12:56:22 christos Exp $	*/
      2   1.1      tron 
      3   1.1      tron /*-
      4   1.1      tron  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.1      tron  * All rights reserved.
      6   1.1      tron  *
      7   1.1      tron  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      tron  * by Matthias Scheler.
      9   1.1      tron  *
     10   1.1      tron  * Redistribution and use in source and binary forms, with or without
     11   1.1      tron  * modification, are permitted provided that the following conditions
     12   1.1      tron  * are met:
     13   1.1      tron  * 1. Redistributions of source code must retain the above copyright
     14   1.1      tron  *    notice, this list of conditions and the following disclaimer.
     15   1.1      tron  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      tron  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      tron  *    documentation and/or other materials provided with the distribution.
     18   1.1      tron  *
     19   1.1      tron  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1      tron  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1      tron  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.18  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1      tron  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1      tron  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1      tron  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1      tron  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1      tron  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1      tron  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1      tron  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      tron  */
     31   1.1      tron 
     32  1.30     lukem #if HAVE_NBTOOL_CONFIG_H
     33  1.30     lukem #include "nbtool_config.h"
     34  1.30     lukem #endif
     35  1.30     lukem 
     36   1.1      tron #include <sys/cdefs.h>
     37  1.30     lukem #if !defined(lint)
     38  1.44     lukem __COPYRIGHT("@(#) Copyright (c) 1999\
     39  1.44     lukem  The NetBSD Foundation, Inc.  All rights reserved.");
     40  1.48  christos __RCSID("$NetBSD: makewhatis.c,v 1.48 2013/06/24 12:56:22 christos Exp $");
     41   1.1      tron #endif /* not lint */
     42  1.21        tv 
     43   1.1      tron #include <sys/types.h>
     44   1.8      tron #include <sys/param.h>
     45  1.23  jdolecek #include <sys/queue.h>
     46   1.1      tron #include <sys/stat.h>
     47   1.8      tron #include <sys/wait.h>
     48   1.1      tron 
     49   1.1      tron #include <ctype.h>
     50  1.21        tv #include <err.h>
     51   1.1      tron #include <errno.h>
     52  1.10      tron #include <fcntl.h>
     53  1.21        tv #include <fts.h>
     54  1.23  jdolecek #include <glob.h>
     55   1.1      tron #include <locale.h>
     56   1.8      tron #include <paths.h>
     57  1.11      tron #include <signal.h>
     58   1.1      tron #include <stdio.h>
     59   1.1      tron #include <stdlib.h>
     60   1.1      tron #include <string.h>
     61   1.1      tron #include <unistd.h>
     62   1.1      tron #include <zlib.h>
     63  1.40  christos #include <util.h>
     64   1.1      tron 
     65  1.26   thorpej #include <man/manconf.h>
     66  1.23  jdolecek #include <man/pathnames.h>
     67  1.23  jdolecek 
     68  1.28       wiz #ifndef NROFF
     69  1.28       wiz #define NROFF "nroff"
     70  1.28       wiz #endif
     71  1.28       wiz 
     72   1.1      tron typedef struct manpagestruct manpage;
     73   1.1      tron struct manpagestruct {
     74  1.46  dholland 	manpage *mp_left, *mp_right;
     75   1.1      tron 	ino_t	 mp_inode;
     76  1.22  jdolecek 	size_t	 mp_sdoff;
     77  1.22  jdolecek 	size_t	 mp_sdlen;
     78  1.18  christos 	char	 mp_name[1];
     79   1.1      tron };
     80   1.1      tron 
     81   1.1      tron typedef struct whatisstruct whatis;
     82   1.1      tron struct whatisstruct {
     83  1.46  dholland 	whatis	*wi_left, *wi_right;
     84   1.1      tron 	char	*wi_data;
     85  1.22  jdolecek 	char	wi_prefix[1];
     86   1.1      tron };
     87   1.1      tron 
     88  1.37      tron int		main(int, char * const *);
     89  1.37      tron static char	*findwhitespace(char *);
     90  1.46  dholland static char	*strmove(char *, char *);
     91  1.37      tron static char	*GetS(gzFile, char *, size_t);
     92  1.37      tron static int	pathnamesection(const char *, const char *);
     93  1.37      tron static int	manpagesection(char *);
     94  1.37      tron static char	*createsectionstring(char *);
     95  1.37      tron static void	addmanpage(manpage **, ino_t, char *, size_t, size_t);
     96  1.37      tron static void	addwhatis(whatis **, char *, char *);
     97  1.37      tron static char	*makesection(int);
     98  1.37      tron static char	*makewhatisline(const char *, const char *, const char *);
     99  1.37      tron static void	catpreprocess(char *);
    100  1.37      tron static char	*parsecatpage(const char *, gzFile *);
    101  1.37      tron static int	manpreprocess(char *);
    102  1.37      tron static char	*nroff(const char *, gzFile *);
    103  1.37      tron static char	*parsemanpage(const char *, gzFile *, int);
    104  1.37      tron static char	*getwhatisdata(char *);
    105  1.46  dholland static void	processmanpages(manpage **, whatis **);
    106  1.37      tron static void	dumpwhatis(FILE *, whatis *);
    107  1.37      tron static int	makewhatis(char * const *manpath);
    108   1.1      tron 
    109  1.37      tron static char * const default_manpath[] = {
    110   1.1      tron 	"/usr/share/man",
    111   1.1      tron 	NULL
    112   1.1      tron };
    113   1.1      tron 
    114  1.37      tron static const char	*sectionext = "0123456789ln";
    115  1.37      tron static const char	*whatisdb   = _PATH_WHATIS;
    116  1.42  dholland static const char	*whatisdb_new = _PATH_WHATIS ".new";
    117  1.37      tron static int		dowarn      = 0;
    118  1.37      tron 
    119  1.37      tron #define	ISALPHA(c)	isalpha((unsigned char)(c))
    120  1.37      tron #define	ISDIGIT(c)	isdigit((unsigned char)(c))
    121  1.37      tron #define	ISSPACE(c)	isspace((unsigned char)(c))
    122   1.1      tron 
    123   1.1      tron int
    124  1.22  jdolecek main(int argc, char *const *argv)
    125   1.1      tron {
    126  1.37      tron 	char * const	*manpath;
    127  1.37      tron 	int		c, dofork;
    128  1.37      tron 	const char	*conffile;
    129  1.37      tron 	ENTRY		*ep;
    130  1.37      tron 	TAG		*tp;
    131  1.37      tron 	int		rv, jobs, status;
    132  1.37      tron 	glob_t		pg;
    133  1.37      tron 	char		*paths[2], **p, *sl;
    134  1.37      tron 	int		retval;
    135  1.37      tron 
    136  1.37      tron 	dofork = 1;
    137  1.37      tron 	conffile = NULL;
    138  1.37      tron 	jobs = 0;
    139  1.37      tron 	retval = EXIT_SUCCESS;
    140  1.23  jdolecek 
    141  1.23  jdolecek 	(void)setlocale(LC_ALL, "");
    142  1.23  jdolecek 
    143  1.37      tron 	while ((c = getopt(argc, argv, "C:fw")) != -1) {
    144  1.37      tron 		switch (c) {
    145  1.23  jdolecek 		case 'C':
    146  1.23  jdolecek 			conffile = optarg;
    147  1.23  jdolecek 			break;
    148  1.23  jdolecek 		case 'f':
    149  1.23  jdolecek 			/* run all processing on foreground */
    150  1.23  jdolecek 			dofork = 0;
    151  1.23  jdolecek 			break;
    152  1.34  christos 		case 'w':
    153  1.34  christos 			dowarn++;
    154  1.34  christos 			break;
    155  1.23  jdolecek 		default:
    156  1.35       wiz 			fprintf(stderr, "Usage: %s [-fw] [-C file] [manpath ...]\n",
    157  1.23  jdolecek 				getprogname());
    158  1.23  jdolecek 			exit(EXIT_FAILURE);
    159  1.23  jdolecek 		}
    160  1.23  jdolecek 	}
    161  1.23  jdolecek 	argc -= optind;
    162  1.23  jdolecek 	argv += optind;
    163  1.46  dholland 
    164  1.24  jdolecek 	if (argc >= 1) {
    165  1.23  jdolecek 		manpath = &argv[0];
    166  1.46  dholland 
    167  1.23  jdolecek 	    mkwhatis:
    168  1.37      tron 		return makewhatis(manpath);
    169  1.23  jdolecek 	}
    170  1.23  jdolecek 
    171  1.23  jdolecek 	/*
    172  1.23  jdolecek 	 * Try read config file, fallback to default_manpath[]
    173  1.23  jdolecek 	 * if man.conf not available.
    174  1.23  jdolecek 	 */
    175  1.23  jdolecek 	config(conffile);
    176  1.39     chuck 	if ((tp = gettag("_whatdb", 0)) == NULL) {
    177  1.23  jdolecek 		manpath = default_manpath;
    178  1.23  jdolecek 		goto mkwhatis;
    179  1.23  jdolecek 	}
    180  1.23  jdolecek 
    181  1.23  jdolecek 	/* Build individual databases */
    182  1.23  jdolecek 	paths[1] = NULL;
    183  1.39     chuck 	TAILQ_FOREACH(ep, &tp->entrylist, q) {
    184  1.23  jdolecek 		if ((rv = glob(ep->s,
    185  1.23  jdolecek 		    GLOB_BRACE | GLOB_NOSORT | GLOB_ERR | GLOB_NOCHECK,
    186  1.23  jdolecek 		    NULL, &pg)) != 0)
    187  1.23  jdolecek 			err(EXIT_FAILURE, "glob('%s')", ep->s);
    188  1.23  jdolecek 
    189  1.23  jdolecek 		/* We always have something to work with here */
    190  1.23  jdolecek 		for (p = pg.gl_pathv; *p; p++) {
    191  1.23  jdolecek 			sl = strrchr(*p, '/');
    192  1.37      tron 			if (sl == NULL) {
    193  1.37      tron 				err(EXIT_FAILURE, "glob: _whatdb entry '%s' "
    194  1.37      tron 				    "doesn't contain slash", ep->s);
    195  1.37      tron 			}
    196  1.23  jdolecek 
    197  1.23  jdolecek 			/*
    198  1.23  jdolecek 			 * Cut the last component of path, leaving just
    199  1.23  jdolecek 			 * the directory. We will use the result as root
    200  1.23  jdolecek 			 * for manpage search.
    201  1.23  jdolecek 			 * glob malloc()s space for the paths, so it's
    202  1.23  jdolecek 			 * okay to change it in-place.
    203  1.23  jdolecek 			 */
    204  1.23  jdolecek 			*sl = '\0';
    205  1.23  jdolecek 			paths[0] = *p;
    206  1.23  jdolecek 
    207  1.23  jdolecek 			if (!dofork) {
    208  1.23  jdolecek 				/* Do not fork child */
    209  1.23  jdolecek 				makewhatis(paths);
    210  1.23  jdolecek 				continue;
    211  1.23  jdolecek 			}
    212  1.23  jdolecek 
    213  1.23  jdolecek 			switch (fork()) {
    214  1.23  jdolecek 			case 0:
    215  1.23  jdolecek 				exit(makewhatis(paths));
    216  1.23  jdolecek 				break;
    217  1.23  jdolecek 			case -1:
    218  1.23  jdolecek 				warn("fork");
    219  1.23  jdolecek 				makewhatis(paths);
    220  1.23  jdolecek 				break;
    221  1.23  jdolecek 			default:
    222  1.23  jdolecek 				jobs++;
    223  1.23  jdolecek 				break;
    224  1.23  jdolecek 			}
    225  1.46  dholland 
    226  1.23  jdolecek 		}
    227  1.23  jdolecek 
    228  1.23  jdolecek 		globfree(&pg);
    229  1.23  jdolecek 	}
    230  1.23  jdolecek 
    231  1.23  jdolecek 	/* Wait for the childern to finish */
    232  1.37      tron 	while (jobs > 0) {
    233  1.37      tron 		(void)wait(&status);
    234  1.23  jdolecek 		if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS)
    235  1.23  jdolecek 			retval = EXIT_FAILURE;
    236  1.23  jdolecek 		jobs--;
    237  1.23  jdolecek 	}
    238  1.46  dholland 
    239  1.37      tron 	return retval;
    240  1.23  jdolecek }
    241  1.23  jdolecek 
    242  1.23  jdolecek static int
    243  1.23  jdolecek makewhatis(char * const * manpath)
    244  1.23  jdolecek {
    245   1.1      tron 	FTS	*fts;
    246   1.1      tron 	FTSENT	*fe;
    247  1.18  christos 	manpage *source;
    248   1.1      tron 	whatis	*dest;
    249   1.1      tron 	FILE	*out;
    250  1.22  jdolecek 	size_t	sdoff, sdlen;
    251  1.45  dholland 	int	outfd;
    252  1.45  dholland 	struct stat st_before, st_after;
    253   1.1      tron 
    254  1.18  christos 	if ((fts = fts_open(manpath, FTS_LOGICAL, NULL)) == NULL)
    255  1.18  christos 		err(EXIT_FAILURE, "Cannot open `%s'", *manpath);
    256   1.1      tron 
    257   1.1      tron 	source = NULL;
    258   1.1      tron 	while ((fe = fts_read(fts)) != NULL) {
    259   1.1      tron 		switch (fe->fts_info) {
    260   1.1      tron 		case FTS_F:
    261  1.22  jdolecek 			if (manpagesection(fe->fts_path) >= 0) {
    262  1.22  jdolecek 				/*
    263  1.22  jdolecek 				 * Get manpage subdirectory prefix. Most
    264  1.22  jdolecek 				 * commonly, this is arch-specific subdirectory.
    265  1.22  jdolecek 				 */
    266  1.22  jdolecek 				if (fe->fts_level >= 3) {
    267  1.37      tron 					int		sl;
    268  1.37      tron 					const char	*s, *lsl;
    269  1.22  jdolecek 
    270  1.37      tron 					lsl = NULL;
    271  1.37      tron 					s = &fe->fts_path[fe->fts_pathlen - 1];
    272  1.37      tron 					for(sl = fe->fts_level - 1; sl > 0;
    273  1.37      tron 					    sl--) {
    274  1.22  jdolecek 						s--;
    275  1.37      tron 						while (s[0] != '/')
    276  1.37      tron 							s--;
    277  1.37      tron 						if (lsl == NULL)
    278  1.22  jdolecek 							lsl = s;
    279  1.22  jdolecek 					}
    280  1.46  dholland 
    281  1.46  dholland 					/*
    282  1.46  dholland 					 * Include trailing '/', so we get
    283  1.46  dholland 					 * 'arch/'.
    284  1.46  dholland 					 */
    285  1.22  jdolecek 					sdoff = s + 1 - fe->fts_path;
    286  1.22  jdolecek 					sdlen = lsl - s + 1;
    287  1.22  jdolecek 				} else {
    288  1.22  jdolecek 					sdoff = 0;
    289  1.22  jdolecek 					sdlen = 0;
    290  1.22  jdolecek 				}
    291  1.22  jdolecek 
    292  1.18  christos 				addmanpage(&source, fe->fts_statp->st_ino,
    293  1.22  jdolecek 				    fe->fts_path, sdoff, sdlen);
    294  1.22  jdolecek 			}
    295  1.18  christos 			/*FALLTHROUGH*/
    296   1.1      tron 		case FTS_D:
    297   1.4      tron 		case FTS_DC:
    298   1.4      tron 		case FTS_DEFAULT:
    299   1.1      tron 		case FTS_DP:
    300  1.38  christos 		case FTS_SL:
    301  1.38  christos 		case FTS_DOT:
    302  1.38  christos 		case FTS_W:
    303  1.38  christos 		case FTS_NSOK:
    304  1.38  christos 		case FTS_INIT:
    305  1.38  christos 			break;
    306   1.4      tron 		case FTS_SLNONE:
    307  1.38  christos 			warnx("Symbolic link with no target: `%s'",
    308  1.38  christos 			    fe->fts_path);
    309  1.38  christos 			break;
    310  1.38  christos 		case FTS_DNR:
    311  1.38  christos 			warnx("Unreadable directory: `%s'", fe->fts_path);
    312  1.38  christos 			break;
    313  1.38  christos 		case FTS_NS:
    314  1.38  christos 			errno = fe->fts_errno;
    315  1.38  christos 			warn("Cannot stat `%s'", fe->fts_path);
    316  1.38  christos 			break;
    317  1.38  christos 		case FTS_ERR:
    318  1.38  christos 			errno = fe->fts_errno;
    319  1.38  christos 			warn("Error reading `%s'", fe->fts_path);
    320   1.1      tron 			break;
    321   1.1      tron 		default:
    322  1.38  christos 			errx(EXIT_FAILURE, "Unknown info %d returned from fts "
    323  1.38  christos 			    " for path: `%s'", fe->fts_info, fe->fts_path);
    324   1.1      tron 		}
    325   1.1      tron 	}
    326   1.1      tron 
    327   1.1      tron 	(void)fts_close(fts);
    328   1.1      tron 
    329   1.1      tron 	dest = NULL;
    330   1.1      tron 	processmanpages(&source, &dest);
    331   1.1      tron 
    332  1.18  christos 	if (chdir(manpath[0]) == -1)
    333  1.18  christos 		err(EXIT_FAILURE, "Cannot change dir to `%s'", manpath[0]);
    334   1.1      tron 
    335  1.45  dholland 	/*
    336  1.45  dholland 	 * makewhatis runs unattended, so it needs to be able to
    337  1.45  dholland 	 * recover if the last run crashed out. Therefore, if
    338  1.45  dholland 	 * whatisdb_new exists and is more than (arbitrarily) sixteen
    339  1.45  dholland 	 * hours old, nuke it. If it exists but is not so old, refuse
    340  1.45  dholland 	 * to run until it's cleaned up, in case another makewhatis is
    341  1.45  dholland 	 * already running. Also, open the output with O_EXCL to make
    342  1.45  dholland 	 * sure we get our own, in case two copies start exactly at
    343  1.45  dholland 	 * once. (Unlikely? Maybe, maybe not, if two copies of cron
    344  1.45  dholland 	 * end up running.)
    345  1.45  dholland 	 *
    346  1.45  dholland 	 * Similarly, before renaming the file after we finish writing
    347  1.45  dholland 	 * to it, make sure it's still the same file we opened. This
    348  1.45  dholland 	 * can't be completely race-free, but getting caught by it
    349  1.45  dholland 	 * would require an unexplained sixteen-hour-or-more lag
    350  1.45  dholland 	 * between the last mtime update when we wrote to it and when
    351  1.45  dholland 	 * we get to the stat call *and* another makewhatis starting
    352  1.45  dholland 	 * out to write at exactly the wrong moment. Not impossible,
    353  1.45  dholland 	 * but not likely enough to worry about.
    354  1.45  dholland 	 *
    355  1.45  dholland 	 * This is maybe unnecessarily elaborate, but generating
    356  1.45  dholland 	 * corrupted output isn't so good either.
    357  1.45  dholland 	 */
    358  1.45  dholland 
    359  1.45  dholland 	if (stat(whatisdb_new, &st_before) == 0) {
    360  1.45  dholland 		if (st_before.st_mtime - time(NULL) > 16*60*60) {
    361  1.45  dholland 			/* Don't complain if someone else just removed it. */
    362  1.45  dholland 			if (unlink(whatisdb_new) == -1 && errno != ENOENT) {
    363  1.45  dholland 				err(EXIT_FAILURE, "Could not remove `%s'",
    364  1.45  dholland 				    whatisdb_new);
    365  1.45  dholland 			} else {
    366  1.45  dholland 				warnx("Removed stale `%s'", whatisdb_new);
    367  1.45  dholland 			}
    368  1.45  dholland 		} else {
    369  1.45  dholland 			errx(EXIT_FAILURE, "The file `%s' already exists "
    370  1.45  dholland 			    "-- am I already running?", whatisdb_new);
    371  1.45  dholland 		}
    372  1.45  dholland 	} else if (errno != ENOENT) {
    373  1.45  dholland 		/* Something unexpected happened. */
    374  1.45  dholland 		err(EXIT_FAILURE, "Cannot stat `%s'", whatisdb_new);
    375  1.45  dholland 	}
    376  1.45  dholland 
    377  1.45  dholland 	outfd = open(whatisdb_new, O_WRONLY|O_CREAT|O_EXCL,
    378  1.45  dholland 	    S_IRUSR|S_IRGRP|S_IROTH);
    379  1.45  dholland 	if (outfd < 0)
    380  1.43  dholland 		err(EXIT_FAILURE, "Cannot open `%s'", whatisdb_new);
    381   1.1      tron 
    382  1.45  dholland 	if (fstat(outfd, &st_before) == -1)
    383  1.45  dholland 		err(EXIT_FAILURE, "Cannot fstat `%s'", whatisdb_new);
    384  1.45  dholland 
    385  1.45  dholland 	if ((out = fdopen(outfd, "w")) == NULL)
    386  1.45  dholland 		err(EXIT_FAILURE, "Cannot fdopen `%s'", whatisdb_new);
    387  1.45  dholland 
    388  1.18  christos 	dumpwhatis(out, dest);
    389  1.18  christos 	if (fchmod(fileno(out), S_IRUSR|S_IRGRP|S_IROTH) == -1)
    390  1.42  dholland 		err(EXIT_FAILURE, "Cannot chmod `%s'", whatisdb_new);
    391  1.18  christos 	if (fclose(out) != 0)
    392  1.42  dholland 		err(EXIT_FAILURE, "Cannot close `%s'", whatisdb_new);
    393  1.42  dholland 
    394  1.45  dholland 	if (stat(whatisdb_new, &st_after) == -1)
    395  1.45  dholland 		err(EXIT_FAILURE, "Cannot stat `%s' (after writing)",
    396  1.45  dholland 		    whatisdb_new);
    397  1.45  dholland 
    398  1.45  dholland 	if (st_before.st_dev != st_after.st_dev ||
    399  1.45  dholland 	    st_before.st_ino != st_after.st_ino) {
    400  1.45  dholland 		errx(EXIT_FAILURE, "The file `%s' changed under me; giving up",
    401  1.45  dholland 		    whatisdb_new);
    402  1.45  dholland 	}
    403  1.45  dholland 
    404  1.42  dholland 	if (rename(whatisdb_new, whatisdb) == -1)
    405  1.42  dholland 		err(EXIT_FAILURE, "Could not rename `%s' to `%s'",
    406  1.42  dholland 		    whatisdb_new, whatisdb);
    407   1.1      tron 
    408   1.1      tron 	return EXIT_SUCCESS;
    409   1.1      tron }
    410   1.1      tron 
    411  1.37      tron static char *
    412  1.18  christos findwhitespace(char *str)
    413   1.6      tron {
    414  1.37      tron 	while (!ISSPACE(*str))
    415   1.6      tron 		if (*str++ == '\0') {
    416   1.6      tron 			str = NULL;
    417   1.6      tron 			break;
    418   1.6      tron 		}
    419   1.6      tron 
    420   1.6      tron 	return str;
    421   1.6      tron }
    422   1.6      tron 
    423  1.46  dholland static char *
    424  1.46  dholland strmove(char *dest, char *src)
    425  1.14      tron {
    426  1.14      tron 	return memmove(dest, src, strlen(src) + 1);
    427  1.14      tron }
    428  1.14      tron 
    429  1.37      tron static char *
    430  1.18  christos GetS(gzFile in, char *buffer, size_t length)
    431   1.5      tron {
    432   1.5      tron 	char	*ptr;
    433   1.5      tron 
    434  1.18  christos 	if (((ptr = gzgets(in, buffer, (int)length)) != NULL) && (*ptr == '\0'))
    435   1.5      tron 		ptr = NULL;
    436   1.5      tron 
    437   1.5      tron 	return ptr;
    438   1.5      tron }
    439   1.5      tron 
    440  1.37      tron static char *
    441  1.34  christos makesection(int s)
    442  1.34  christos {
    443  1.34  christos 	char sectionbuffer[24];
    444  1.34  christos 	if (s == -1)
    445  1.34  christos 		return NULL;
    446  1.34  christos 	(void)snprintf(sectionbuffer, sizeof(sectionbuffer),
    447  1.34  christos 		" (%c) - ", sectionext[s]);
    448  1.34  christos 	return estrdup(sectionbuffer);
    449  1.34  christos }
    450  1.34  christos 
    451  1.37      tron static int
    452  1.34  christos pathnamesection(const char *pat, const char *name)
    453  1.34  christos {
    454  1.34  christos 	char *ptr, *ext;
    455  1.34  christos 	size_t len = strlen(pat);
    456  1.34  christos 
    457  1.34  christos 
    458  1.34  christos 	while ((ptr = strstr(name, pat)) != NULL) {
    459  1.34  christos 		if ((ext = strchr(sectionext, ptr[len])) != NULL) {
    460  1.34  christos 			return ext - sectionext;
    461  1.34  christos 		}
    462  1.34  christos 		name = ptr + 1;
    463  1.34  christos 	}
    464  1.34  christos 	return -1;
    465  1.34  christos }
    466  1.34  christos 
    467  1.34  christos 
    468  1.37      tron static int
    469   1.1      tron manpagesection(char *name)
    470   1.1      tron {
    471   1.1      tron 	char	*ptr;
    472   1.1      tron 
    473   1.1      tron 	if ((ptr = strrchr(name, '/')) != NULL)
    474   1.1      tron 		ptr++;
    475   1.1      tron 	else
    476   1.1      tron 		ptr = name;
    477   1.1      tron 
    478   1.4      tron 	while ((ptr = strchr(ptr, '.')) != NULL) {
    479   1.4      tron 		int section;
    480   1.4      tron 
    481   1.4      tron 		ptr++;
    482  1.34  christos 		section = 0;
    483   1.4      tron 		while (sectionext[section] != '\0')
    484   1.4      tron 			if (sectionext[section] == *ptr)
    485   1.4      tron 				return section;
    486   1.4      tron 			else
    487   1.4      tron 				section++;
    488   1.4      tron 	}
    489   1.1      tron 	return -1;
    490   1.1      tron }
    491   1.1      tron 
    492  1.37      tron static char *
    493  1.18  christos createsectionstring(char *section_id)
    494  1.14      tron {
    495  1.29    itojun 	char *section;
    496  1.29    itojun 
    497  1.29    itojun 	if (asprintf(&section, " (%s) - ", section_id) < 0)
    498  1.29    itojun 		err(EXIT_FAILURE, "malloc failed");
    499  1.14      tron 	return section;
    500  1.14      tron }
    501  1.14      tron 
    502  1.37      tron static void
    503  1.46  dholland addmanpage(manpage **tree, ino_t inode, char *name, size_t sdoff, size_t sdlen)
    504   1.1      tron {
    505  1.18  christos 	manpage *mp;
    506   1.1      tron 
    507   1.1      tron 	while ((mp = *tree) != NULL) {
    508   1.1      tron 		if (mp->mp_inode == inode)
    509  1.18  christos 			return;
    510  1.18  christos 		tree = inode < mp->mp_inode ? &mp->mp_left : &mp->mp_right;
    511   1.1      tron 	}
    512   1.1      tron 
    513  1.18  christos 	mp = emalloc(sizeof(manpage) + strlen(name));
    514   1.1      tron 	mp->mp_left = NULL;
    515   1.1      tron 	mp->mp_right = NULL;
    516   1.1      tron 	mp->mp_inode = inode;
    517  1.22  jdolecek 	mp->mp_sdoff = sdoff;
    518  1.22  jdolecek 	mp->mp_sdlen = sdlen;
    519  1.18  christos 	(void)strcpy(mp->mp_name, name);
    520   1.1      tron 	*tree = mp;
    521   1.1      tron }
    522   1.1      tron 
    523  1.37      tron static void
    524  1.22  jdolecek addwhatis(whatis **tree, char *data, char *prefix)
    525   1.1      tron {
    526   1.1      tron 	whatis *wi;
    527   1.1      tron 	int result;
    528   1.1      tron 
    529  1.37      tron 	while (ISSPACE(*data))
    530   1.7      tron 		data++;
    531   1.7      tron 
    532   1.7      tron 	if (*data == '/') {
    533   1.7      tron 		char *ptr;
    534   1.7      tron 
    535   1.7      tron 		ptr = ++data;
    536  1.37      tron 		while ((*ptr != '\0') && !ISSPACE(*ptr))
    537   1.7      tron 			if (*ptr++ == '/')
    538   1.7      tron 				data = ptr;
    539   1.7      tron 	}
    540   1.7      tron 
    541   1.1      tron 	while ((wi = *tree) != NULL) {
    542  1.18  christos 		result = strcmp(data, wi->wi_data);
    543  1.47       apb 		if (result == 0) result = strcmp(prefix, wi->wi_prefix);
    544  1.18  christos 		if (result == 0) return;
    545  1.18  christos 		tree = result < 0 ? &wi->wi_left : &wi->wi_right;
    546   1.1      tron 	}
    547   1.1      tron 
    548  1.22  jdolecek 	wi = emalloc(sizeof(whatis) + strlen(prefix));
    549   1.1      tron 
    550   1.1      tron 	wi->wi_left = NULL;
    551   1.1      tron 	wi->wi_right = NULL;
    552   1.1      tron 	wi->wi_data = data;
    553  1.22  jdolecek 	if (prefix[0] != '\0')
    554  1.22  jdolecek 		(void) strcpy(wi->wi_prefix, prefix);
    555  1.22  jdolecek 	else
    556  1.22  jdolecek 		wi->wi_prefix[0] = '\0';
    557   1.1      tron 	*tree = wi;
    558   1.1      tron }
    559   1.1      tron 
    560  1.37      tron static void
    561   1.1      tron catpreprocess(char *from)
    562   1.1      tron {
    563   1.1      tron 	char	*to;
    564   1.1      tron 
    565   1.1      tron 	to = from;
    566  1.37      tron 	while (ISSPACE(*from)) from++;
    567   1.1      tron 
    568   1.1      tron 	while (*from != '\0')
    569  1.37      tron 		if (ISSPACE(*from)) {
    570  1.37      tron 			while (ISSPACE(*++from));
    571   1.1      tron 			if (*from != '\0')
    572   1.1      tron 				*to++ = ' ';
    573   1.1      tron 		}
    574  1.32  christos 		else if (*(from + 1) == '\b')
    575   1.1      tron 			from += 2;
    576   1.1      tron 		else
    577   1.1      tron 			*to++ = *from++;
    578   1.1      tron 
    579   1.1      tron 	*to = '\0';
    580   1.1      tron }
    581   1.1      tron 
    582  1.37      tron static char *
    583  1.34  christos makewhatisline(const char *file, const char *line, const char *section)
    584   1.1      tron {
    585  1.34  christos 	static const char *del[] = {
    586  1.34  christos 		" - ",
    587  1.34  christos 		" -- ",
    588  1.34  christos 		"- ",
    589  1.34  christos 		" -",
    590  1.34  christos 		NULL
    591  1.34  christos 	};
    592  1.34  christos 	size_t i, pos;
    593  1.34  christos 	size_t llen, slen, dlen;
    594  1.34  christos 	char *result, *ptr;
    595   1.1      tron 
    596  1.36     lukem 	ptr = NULL;
    597  1.34  christos 	if (section == NULL) {
    598  1.34  christos 		if (dowarn)
    599  1.34  christos 			warnx("%s: No section provided for `%s'", file, line);
    600  1.34  christos 		return estrdup(line);
    601  1.34  christos 	}
    602   1.1      tron 
    603  1.34  christos 	for (i = 0; del[i]; i++)
    604  1.34  christos 		if ((ptr = strstr(line, del[i])) != NULL)
    605  1.34  christos 			break;
    606   1.1      tron 
    607  1.34  christos 	if (del[i] == NULL) {
    608  1.34  christos 		if (dowarn)
    609  1.34  christos 			warnx("%s: Bad format line `%s'", file, line);
    610  1.34  christos 		return estrdup(line);
    611  1.34  christos 	}
    612  1.34  christos 
    613  1.34  christos 	slen = strlen(section);
    614  1.34  christos 	llen = strlen(line);
    615  1.34  christos 	dlen = strlen(del[i]);
    616  1.34  christos 
    617  1.34  christos 	result = emalloc(llen - dlen + slen + 1);
    618  1.34  christos 	pos = ptr - line;
    619  1.34  christos 
    620  1.34  christos 	(void)memcpy(result, line, pos);
    621  1.34  christos 	(void)memcpy(&result[pos], section, slen);
    622  1.34  christos 	(void)strcpy(&result[pos + slen], &line[pos + dlen]);
    623   1.1      tron 	return result;
    624   1.1      tron }
    625   1.1      tron 
    626  1.37      tron static char *
    627  1.34  christos parsecatpage(const char *name, gzFile *in)
    628   1.1      tron {
    629  1.18  christos 	char	 buffer[8192];
    630   1.1      tron 	char	*section, *ptr, *last;
    631  1.18  christos 	size_t	 size;
    632   1.1      tron 
    633   1.1      tron 	do {
    634   1.5      tron 		if (GetS(in, buffer, sizeof(buffer)) == NULL)
    635   1.1      tron 			return NULL;
    636   1.1      tron 	}
    637   1.1      tron 	while (buffer[0] == '\n');
    638   1.1      tron 
    639   1.1      tron 	section = NULL;
    640   1.1      tron 	if ((ptr = strchr(buffer, '(')) != NULL) {
    641   1.1      tron 		if ((last = strchr(ptr + 1, ')')) !=NULL) {
    642  1.18  christos 			size_t	length;
    643   1.1      tron 
    644   1.1      tron 			length = last - ptr + 1;
    645  1.18  christos 			section = emalloc(length + 5);
    646   1.1      tron 			*section = ' ';
    647   1.1      tron 			(void) memcpy(section + 1, ptr, length);
    648   1.1      tron 			(void) strcpy(section + 1 + length, " - ");
    649   1.1      tron 		}
    650   1.1      tron 	}
    651   1.1      tron 
    652   1.1      tron 	for (;;) {
    653   1.5      tron 		if (GetS(in, buffer, sizeof(buffer)) == NULL) {
    654   1.1      tron 			free(section);
    655   1.1      tron 			return NULL;
    656   1.1      tron 		}
    657  1.16      tron 		catpreprocess(buffer);
    658  1.16      tron 		if (strncmp(buffer, "NAME", 4) == 0)
    659   1.1      tron 			break;
    660   1.1      tron 	}
    661  1.34  christos 	if (section == NULL)
    662  1.34  christos 		section = makesection(pathnamesection("/cat", name));
    663   1.1      tron 
    664   1.1      tron 	ptr = last = buffer;
    665   1.1      tron 	size = sizeof(buffer) - 1;
    666   1.5      tron 	while ((size > 0) && (GetS(in, ptr, size) != NULL)) {
    667   1.1      tron 		int	 length;
    668   1.1      tron 
    669   1.1      tron 		catpreprocess(ptr);
    670   1.1      tron 
    671   1.1      tron 		length = strlen(ptr);
    672   1.1      tron 		if (length == 0) {
    673   1.1      tron 			*last = '\0';
    674   1.1      tron 
    675  1.34  christos 			ptr = makewhatisline(name, buffer, section);
    676   1.1      tron 			free(section);
    677   1.1      tron 			return ptr;
    678   1.1      tron 		}
    679   1.1      tron 		if ((length > 1) && (ptr[length - 1] == '-') &&
    680  1.37      tron 		    ISALPHA(ptr[length - 2]))
    681   1.1      tron 			last = &ptr[--length];
    682   1.1      tron 		else {
    683   1.1      tron 			last = &ptr[length++];
    684   1.1      tron 			*last = ' ';
    685   1.1      tron 		}
    686   1.1      tron 
    687   1.1      tron 		ptr += length;
    688   1.1      tron 		size -= length;
    689   1.1      tron 	}
    690   1.1      tron 
    691   1.1      tron 	free(section);
    692   1.1      tron 
    693   1.1      tron 	return NULL;
    694   1.1      tron }
    695   1.1      tron 
    696  1.37      tron static int
    697   1.1      tron manpreprocess(char *line)
    698   1.1      tron {
    699   1.1      tron 	char	*from, *to;
    700   1.1      tron 
    701   1.1      tron 	to = from = line;
    702  1.37      tron 	while (ISSPACE(*from))
    703  1.37      tron 		from++;
    704   1.1      tron 	if (strncmp(from, ".\\\"", 3) == 0)
    705   1.1      tron 		return 1;
    706   1.1      tron 
    707   1.1      tron 	while (*from != '\0')
    708  1.37      tron 		if (ISSPACE(*from)) {
    709  1.37      tron 			while (ISSPACE(*++from));
    710   1.1      tron 			if ((*from != '\0') && (*from != ','))
    711   1.1      tron 				*to++ = ' ';
    712  1.37      tron 		} else if (*from == '\\') {
    713   1.1      tron 			switch (*++from) {
    714   1.1      tron 			case '\0':
    715   1.1      tron 			case '-':
    716   1.1      tron 				break;
    717  1.14      tron 			case 'f':
    718   1.7      tron 			case 's':
    719  1.14      tron 				from++;
    720   1.7      tron 				if ((*from=='+') || (*from=='-'))
    721   1.7      tron 					from++;
    722  1.37      tron 				while (ISDIGIT(*from))
    723   1.7      tron 					from++;
    724   1.7      tron 				break;
    725   1.1      tron 			default:
    726   1.1      tron 				from++;
    727   1.1      tron 			}
    728  1.37      tron 		} else {
    729   1.1      tron 			if (*from == '"')
    730   1.1      tron 				from++;
    731   1.1      tron 			else
    732   1.1      tron 				*to++ = *from++;
    733  1.37      tron 		}
    734   1.1      tron 
    735   1.1      tron 	*to = '\0';
    736   1.1      tron 
    737   1.1      tron 	if (strncasecmp(line, ".Xr", 3) == 0) {
    738   1.1      tron 		char	*sect;
    739   1.1      tron 
    740   1.1      tron 		from = line + 3;
    741  1.37      tron 		if (ISSPACE(*from))
    742   1.1      tron 			from++;
    743   1.1      tron 
    744   1.6      tron 		if ((sect = findwhitespace(from)) != NULL) {
    745  1.19      tron 			size_t	length;
    746  1.19      tron 			char	*trail;
    747   1.1      tron 
    748   1.1      tron 			*sect++ = '\0';
    749  1.19      tron 			if ((trail = findwhitespace(sect)) != NULL)
    750  1.19      tron 				*trail++ = '\0';
    751   1.1      tron 			length = strlen(from);
    752   1.1      tron 			(void) memmove(line, from, length);
    753   1.1      tron 			line[length++] = '(';
    754   1.1      tron 			to = &line[length];
    755   1.1      tron 			length = strlen(sect);
    756   1.1      tron 			(void) memmove(to, sect, length);
    757  1.19      tron 			if (trail == NULL) {
    758  1.19      tron 				(void) strcpy(&to[length], ")");
    759  1.19      tron 			} else {
    760  1.19      tron 				to += length;
    761  1.19      tron 				*to++ = ')';
    762  1.19      tron 				length = strlen(trail);
    763  1.19      tron 				(void) memmove(to, trail, length + 1);
    764  1.19      tron 			}
    765   1.1      tron 		}
    766   1.1      tron 	}
    767   1.1      tron 
    768   1.1      tron 	return 0;
    769   1.1      tron }
    770   1.1      tron 
    771  1.37      tron static char *
    772  1.34  christos nroff(const char *inname, gzFile *in)
    773   1.8      tron {
    774   1.9      tron 	char tempname[MAXPATHLEN], buffer[65536], *data;
    775   1.9      tron 	int tempfd, bytes, pipefd[2], status;
    776  1.10      tron 	static int devnull = -1;
    777   1.8      tron 	pid_t child;
    778   1.8      tron 
    779  1.18  christos 	if (gzrewind(in) < 0)
    780  1.18  christos 		err(EXIT_FAILURE, "Cannot rewind pipe");
    781   1.8      tron 
    782  1.10      tron 	if ((devnull < 0) &&
    783  1.18  christos 	    ((devnull = open(_PATH_DEVNULL, O_WRONLY, 0)) < 0))
    784  1.18  christos 		err(EXIT_FAILURE, "Cannot open `/dev/null'");
    785  1.10      tron 
    786  1.29    itojun 	(void)strlcpy(tempname, _PATH_TMP "makewhatis.XXXXXX",
    787  1.29    itojun 	    sizeof(tempname));
    788  1.18  christos 	if ((tempfd = mkstemp(tempname)) == -1)
    789  1.18  christos 		err(EXIT_FAILURE, "Cannot create temp file");
    790   1.8      tron 
    791   1.8      tron 	while ((bytes = gzread(in, buffer, sizeof(buffer))) > 0)
    792  1.18  christos 		if (write(tempfd, buffer, (size_t)bytes) != bytes) {
    793   1.8      tron 			bytes = -1;
    794   1.8      tron 			break;
    795   1.8      tron 		}
    796   1.8      tron 
    797  1.18  christos 	if (bytes < 0) {
    798  1.18  christos 		(void)close(tempfd);
    799  1.18  christos 		(void)unlink(tempname);
    800  1.18  christos 		err(EXIT_FAILURE, "Read from pipe failed");
    801  1.18  christos 	}
    802  1.18  christos 	if (lseek(tempfd, (off_t)0, SEEK_SET) == (off_t)-1) {
    803  1.18  christos 		(void)close(tempfd);
    804  1.18  christos 		(void)unlink(tempname);
    805  1.18  christos 		err(EXIT_FAILURE, "Cannot rewind temp file");
    806  1.18  christos 	}
    807  1.18  christos 	if (pipe(pipefd) == -1) {
    808   1.8      tron 		(void)close(tempfd);
    809   1.8      tron 		(void)unlink(tempname);
    810  1.18  christos 		err(EXIT_FAILURE, "Cannot create pipe");
    811   1.8      tron 	}
    812   1.8      tron 
    813   1.8      tron 	switch (child = vfork()) {
    814   1.8      tron 	case -1:
    815   1.8      tron 		(void)close(pipefd[1]);
    816   1.8      tron 		(void)close(pipefd[0]);
    817   1.8      tron 		(void)close(tempfd);
    818   1.8      tron 		(void)unlink(tempname);
    819  1.18  christos 		err(EXIT_FAILURE, "Fork failed");
    820   1.8      tron 		/* NOTREACHED */
    821   1.8      tron 	case 0:
    822   1.8      tron 		(void)close(pipefd[0]);
    823  1.10      tron 		if (tempfd != STDIN_FILENO) {
    824  1.10      tron 			(void)dup2(tempfd, STDIN_FILENO);
    825  1.10      tron 			(void)close(tempfd);
    826  1.10      tron 		}
    827   1.8      tron 		if (pipefd[1] != STDOUT_FILENO) {
    828   1.8      tron 			(void)dup2(pipefd[1], STDOUT_FILENO);
    829   1.8      tron 			(void)close(pipefd[1]);
    830   1.8      tron 		}
    831  1.10      tron 		if (devnull != STDERR_FILENO) {
    832  1.10      tron 			(void)dup2(devnull, STDERR_FILENO);
    833  1.10      tron 			(void)close(devnull);
    834  1.10      tron 		}
    835  1.28       wiz 		(void)execlp(NROFF, NROFF, "-S", "-man", NULL);
    836   1.8      tron 		_exit(EXIT_FAILURE);
    837  1.18  christos 		/*NOTREACHED*/
    838   1.8      tron 	default:
    839   1.8      tron 		(void)close(pipefd[1]);
    840   1.8      tron 		(void)close(tempfd);
    841  1.18  christos 		break;
    842   1.8      tron 	}
    843   1.8      tron 
    844   1.8      tron 	if ((in = gzdopen(pipefd[0], "r")) == NULL) {
    845   1.8      tron 		if (errno == 0)
    846   1.8      tron 			errno = ENOMEM;
    847  1.11      tron 		(void)close(pipefd[0]);
    848  1.11      tron 		(void)kill(child, SIGTERM);
    849  1.11      tron 		while (waitpid(child, NULL, 0) != child);
    850   1.8      tron 		(void)unlink(tempname);
    851  1.18  christos 		err(EXIT_FAILURE, "Cannot read from pipe");
    852   1.8      tron 	}
    853   1.8      tron 
    854  1.34  christos 	data = parsecatpage(inname, in);
    855   1.9      tron 	while (gzread(in, buffer, sizeof(buffer)) > 0);
    856   1.9      tron 	(void)gzclose(in);
    857   1.9      tron 
    858   1.9      tron 	while (waitpid(child, &status, 0) != child);
    859   1.9      tron 	if ((data != NULL) &&
    860   1.9      tron 	    !(WIFEXITED(status) && (WEXITSTATUS(status) == 0))) {
    861   1.9      tron 		free(data);
    862  1.28       wiz 		errx(EXIT_FAILURE, NROFF " on `%s' exited with %d status",
    863  1.27       wiz 		    inname, WEXITSTATUS(status));
    864   1.9      tron 	}
    865   1.8      tron 
    866   1.8      tron 	(void)unlink(tempname);
    867   1.8      tron 	return data;
    868   1.8      tron }
    869   1.8      tron 
    870  1.37      tron static char *
    871  1.34  christos parsemanpage(const char *name, gzFile *in, int defaultsection)
    872   1.1      tron {
    873   1.1      tron 	char	*section, buffer[8192], *ptr;
    874  1.48  christos 	static const char POD[] = ".\\\" Automatically generated by Pod";
    875  1.48  christos 	static const char IX[] = ".IX TITLE";
    876   1.1      tron 
    877   1.1      tron 	section = NULL;
    878   1.1      tron 	do {
    879   1.5      tron 		if (GetS(in, buffer, sizeof(buffer) - 1) == NULL) {
    880   1.1      tron 			free(section);
    881   1.1      tron 			return NULL;
    882   1.1      tron 		}
    883  1.48  christos 
    884  1.48  christos 		/*
    885  1.48  christos 		 * Skip over lines in man pages that have been generated
    886  1.48  christos 		 * by Pod, until we find the TITLE. This makewhatis cannot
    887  1.48  christos 		 * deal with it (and as * a result the man page would not
    888  1.48  christos 		 * appear in whatis.db)
    889  1.48  christos 		 * Modified by Robert Kovacic, June 23 2013.
    890  1.48  christos 		 */
    891  1.48  christos 		if (strncasecmp(buffer, POD, sizeof(POD) - 1) == 0) {
    892  1.48  christos 			do {
    893  1.48  christos 				if (GetS(in, buffer, sizeof(buffer) - 1)
    894  1.48  christos 				    == NULL) {
    895  1.48  christos 					free(section);
    896  1.48  christos 					return NULL;
    897  1.48  christos 				}
    898  1.48  christos 			} while (strncasecmp(buffer, IX, sizeof(IX) - 1) != 0);
    899  1.48  christos 		}
    900  1.48  christos 
    901   1.1      tron 		if (manpreprocess(buffer))
    902   1.1      tron 			continue;
    903   1.1      tron 		if (strncasecmp(buffer, ".Dt", 3) == 0) {
    904   1.1      tron 			char	*end;
    905   1.1      tron 
    906   1.1      tron 			ptr = &buffer[3];
    907  1.37      tron 			if (ISSPACE(*ptr))
    908   1.1      tron 				ptr++;
    909   1.6      tron 			if ((ptr = findwhitespace(ptr)) == NULL)
    910   1.1      tron 				continue;
    911   1.1      tron 
    912   1.6      tron 			if ((end = findwhitespace(++ptr)) != NULL)
    913   1.1      tron 				*end = '\0';
    914   1.1      tron 
    915   1.1      tron 			free(section);
    916  1.14      tron 			section = createsectionstring(ptr);
    917  1.14      tron 		}
    918  1.14      tron 		else if (strncasecmp(buffer, ".TH", 3) == 0) {
    919  1.14      tron 			ptr = &buffer[3];
    920  1.37      tron 			while (ISSPACE(*ptr))
    921  1.14      tron 				ptr++;
    922  1.14      tron 			if ((ptr = findwhitespace(ptr)) != NULL) {
    923  1.14      tron 				char *next;
    924  1.14      tron 
    925  1.37      tron 				while (ISSPACE(*ptr))
    926  1.14      tron 					ptr++;
    927  1.14      tron 				if ((next = findwhitespace(ptr)) != NULL)
    928  1.14      tron 					*next = '\0';
    929  1.14      tron 				free(section);
    930  1.14      tron 				section = createsectionstring(ptr);
    931   1.1      tron 			}
    932   1.1      tron 		}
    933  1.14      tron 		else if (strncasecmp(buffer, ".Ds", 3) == 0) {
    934  1.14      tron 			free(section);
    935  1.14      tron 			return NULL;
    936  1.14      tron 		}
    937  1.14      tron 	} while (strncasecmp(buffer, ".Sh NAME", 8) != 0);
    938   1.1      tron 
    939   1.1      tron 	do {
    940   1.5      tron 		if (GetS(in, buffer, sizeof(buffer) - 1) == NULL) {
    941   1.1      tron 			free(section);
    942   1.1      tron 			return NULL;
    943   1.1      tron 		}
    944   1.1      tron 	} while (manpreprocess(buffer));
    945   1.1      tron 
    946   1.1      tron 	if (strncasecmp(buffer, ".Nm", 3) == 0) {
    947  1.18  christos 		size_t	length, offset;
    948   1.1      tron 
    949   1.1      tron 		ptr = &buffer[3];
    950  1.37      tron 		while (ISSPACE(*ptr))
    951   1.1      tron 			ptr++;
    952   1.1      tron 
    953   1.1      tron 		length = strlen(ptr);
    954   1.1      tron 		if ((length > 1) && (ptr[length - 1] == ',') &&
    955  1.37      tron 		    ISSPACE(ptr[length - 2])) {
    956   1.1      tron 			ptr[--length] = '\0';
    957   1.1      tron 			ptr[length - 1] = ',';
    958   1.1      tron 		}
    959   1.1      tron 		(void) memmove(buffer, ptr, length + 1);
    960   1.1      tron 
    961   1.1      tron 		offset = length + 3;
    962   1.1      tron 		ptr = &buffer[offset];
    963   1.1      tron 		for (;;) {
    964  1.18  christos 			size_t	 more;
    965   1.1      tron 
    966   1.1      tron 			if ((sizeof(buffer) == offset) ||
    967  1.18  christos 			    (GetS(in, ptr, sizeof(buffer) - offset)
    968   1.1      tron 			       == NULL)) {
    969   1.1      tron 				free(section);
    970   1.1      tron 				return NULL;
    971   1.1      tron 			}
    972   1.1      tron 			if (manpreprocess(ptr))
    973   1.1      tron 				continue;
    974   1.1      tron 
    975   1.1      tron 			if (strncasecmp(ptr, ".Nm", 3) != 0) break;
    976   1.1      tron 
    977   1.1      tron 			ptr += 3;
    978  1.37      tron 			if (ISSPACE(*ptr))
    979   1.1      tron 				ptr++;
    980   1.1      tron 
    981   1.1      tron 			buffer[length++] = ' ';
    982   1.1      tron 			more = strlen(ptr);
    983   1.1      tron 			if ((more > 1) && (ptr[more - 1] == ',') &&
    984  1.37      tron 			    ISSPACE(ptr[more - 2])) {
    985   1.1      tron 				ptr[--more] = '\0';
    986   1.1      tron 				ptr[more - 1] = ',';
    987   1.1      tron 			}
    988   1.1      tron 
    989   1.1      tron 			(void) memmove(&buffer[length], ptr, more + 1);
    990   1.1      tron 			length += more;
    991   1.1      tron 			offset = length + 3;
    992   1.1      tron 
    993   1.1      tron 			ptr = &buffer[offset];
    994   1.1      tron 		}
    995   1.1      tron 
    996   1.1      tron 		if (strncasecmp(ptr, ".Nd", 3) == 0) {
    997  1.29    itojun 			(void) strlcpy(&buffer[length], " -",
    998  1.29    itojun 			    sizeof(buffer) - length);
    999   1.1      tron 
   1000   1.1      tron 			while (strncasecmp(ptr, ".Sh", 3) != 0) {
   1001   1.1      tron 				int	 more;
   1002   1.1      tron 
   1003   1.1      tron 				if (*ptr == '.') {
   1004   1.1      tron 					char	*space;
   1005   1.1      tron 
   1006  1.37      tron 					if (strncasecmp(ptr, ".Nd", 3) != 0 ||
   1007  1.37      tron 					    strchr(ptr, '[') != NULL) {
   1008  1.15      tron 						free(section);
   1009  1.15      tron 						return NULL;
   1010  1.15      tron 					}
   1011  1.14      tron 					space = findwhitespace(ptr);
   1012  1.37      tron 					if (space == NULL) {
   1013   1.1      tron 						ptr = "";
   1014  1.37      tron 					} else {
   1015   1.1      tron 						space++;
   1016  1.14      tron 						(void) strmove(ptr, space);
   1017   1.1      tron 					}
   1018   1.1      tron 				}
   1019   1.1      tron 
   1020   1.1      tron 				if (*ptr != '\0') {
   1021   1.1      tron 					buffer[offset - 1] = ' ';
   1022   1.1      tron 					more = strlen(ptr) + 1;
   1023   1.1      tron 					offset += more;
   1024   1.1      tron 				}
   1025   1.1      tron 				ptr = &buffer[offset];
   1026   1.1      tron 				if ((sizeof(buffer) == offset) ||
   1027  1.18  christos 				    (GetS(in, ptr, sizeof(buffer) - offset)
   1028   1.1      tron 					== NULL)) {
   1029   1.1      tron 					free(section);
   1030   1.1      tron 					return NULL;
   1031   1.1      tron 				}
   1032   1.1      tron 				if (manpreprocess(ptr))
   1033   1.1      tron 					*ptr = '\0';
   1034   1.1      tron 			}
   1035   1.1      tron 		}
   1036   1.1      tron 	}
   1037   1.1      tron 	else {
   1038   1.1      tron 		int	 offset;
   1039   1.1      tron 
   1040   1.1      tron 		if (*buffer == '.') {
   1041   1.1      tron 			char	*space;
   1042   1.1      tron 
   1043  1.14      tron 			if ((space = findwhitespace(&buffer[1])) == NULL) {
   1044   1.1      tron 				free(section);
   1045   1.1      tron 				return NULL;
   1046   1.1      tron 			}
   1047   1.1      tron 			space++;
   1048  1.14      tron 			(void) strmove(buffer, space);
   1049   1.1      tron 		}
   1050   1.1      tron 
   1051   1.1      tron 		offset = strlen(buffer) + 1;
   1052   1.1      tron 		for (;;) {
   1053   1.1      tron 			int	 more;
   1054   1.1      tron 
   1055   1.1      tron 			ptr = &buffer[offset];
   1056   1.1      tron 			if ((sizeof(buffer) == offset) ||
   1057  1.18  christos 			    (GetS(in, ptr, sizeof(buffer) - offset)
   1058   1.1      tron 				== NULL)) {
   1059   1.1      tron 				free(section);
   1060   1.1      tron 				return NULL;
   1061   1.1      tron 			}
   1062   1.1      tron 			if (manpreprocess(ptr) || (*ptr == '\0'))
   1063   1.1      tron 				continue;
   1064   1.1      tron 
   1065   1.5      tron 			if ((strncasecmp(ptr, ".Sh", 3) == 0) ||
   1066   1.5      tron 			    (strncasecmp(ptr, ".Ss", 3) == 0))
   1067   1.1      tron 				break;
   1068   1.1      tron 
   1069   1.1      tron 			if (*ptr == '.') {
   1070   1.1      tron 				char	*space;
   1071   1.1      tron 
   1072   1.6      tron 				if ((space = findwhitespace(ptr)) == NULL) {
   1073   1.1      tron 					continue;
   1074   1.5      tron 				}
   1075   1.5      tron 
   1076   1.1      tron 				space++;
   1077   1.5      tron 				(void) memmove(ptr, space, strlen(space) + 1);
   1078   1.1      tron 			}
   1079   1.1      tron 
   1080   1.1      tron 			buffer[offset - 1] = ' ';
   1081   1.1      tron 			more = strlen(ptr);
   1082   1.1      tron 			if ((more > 1) && (ptr[more - 1] == ',') &&
   1083  1.37      tron 			    ISSPACE(ptr[more - 2])) {
   1084   1.1      tron 				ptr[more - 1] = '\0';
   1085   1.1      tron 				ptr[more - 2] = ',';
   1086   1.1      tron 			}
   1087   1.1      tron 			else more++;
   1088   1.1      tron 			offset += more;
   1089   1.1      tron 		}
   1090   1.1      tron 	}
   1091   1.1      tron 
   1092  1.34  christos 	if (section == NULL)
   1093  1.34  christos 		section = makesection(defaultsection);
   1094   1.1      tron 
   1095  1.34  christos 	ptr = makewhatisline(name, buffer, section);
   1096  1.34  christos 	free(section);
   1097   1.1      tron 	return ptr;
   1098   1.1      tron }
   1099   1.1      tron 
   1100  1.37      tron static char *
   1101   1.1      tron getwhatisdata(char *name)
   1102   1.1      tron {
   1103   1.1      tron 	gzFile	*in;
   1104   1.1      tron 	char	*data;
   1105   1.1      tron 	int	 section;
   1106   1.1      tron 
   1107   1.1      tron 	if ((in = gzopen(name, "r")) == NULL) {
   1108  1.18  christos 		if (errno == 0)
   1109  1.18  christos 			errno = ENOMEM;
   1110  1.18  christos 		err(EXIT_FAILURE, "Cannot open `%s'", name);
   1111   1.1      tron 		/* NOTREACHED */
   1112   1.1      tron 	}
   1113   1.1      tron 
   1114   1.1      tron 	section = manpagesection(name);
   1115  1.34  christos 	if (section == 0) {
   1116  1.34  christos 		data = parsecatpage(name, in);
   1117  1.34  christos 	} else {
   1118  1.34  christos 		data = parsemanpage(name, in, section);
   1119  1.14      tron 		if (data == NULL)
   1120  1.34  christos 			data = nroff(name, in);
   1121  1.14      tron 	}
   1122   1.1      tron 
   1123   1.1      tron 	(void) gzclose(in);
   1124   1.1      tron 	return data;
   1125   1.1      tron }
   1126   1.1      tron 
   1127  1.37      tron static void
   1128   1.1      tron processmanpages(manpage **source, whatis **dest)
   1129   1.1      tron {
   1130  1.18  christos 	manpage *mp;
   1131  1.22  jdolecek 	char sd[128];
   1132   1.1      tron 
   1133   1.1      tron 	mp = *source;
   1134   1.1      tron 	*source = NULL;
   1135   1.1      tron 
   1136   1.1      tron 	while (mp != NULL) {
   1137   1.1      tron 		manpage *obsolete;
   1138   1.1      tron 		char *data;
   1139   1.1      tron 
   1140   1.1      tron 		if (mp->mp_left != NULL)
   1141  1.46  dholland 			processmanpages(&mp->mp_left, dest);
   1142   1.1      tron 
   1143  1.22  jdolecek 		if ((data = getwhatisdata(mp->mp_name)) != NULL) {
   1144  1.22  jdolecek 			/* Pass eventual directory prefix to addwhatis() */
   1145  1.22  jdolecek 			if (mp->mp_sdlen > 0 && mp->mp_sdlen < sizeof(sd)-1)
   1146  1.22  jdolecek 				strlcpy(sd, &mp->mp_name[mp->mp_sdoff],
   1147  1.22  jdolecek 					mp->mp_sdlen);
   1148  1.22  jdolecek 			else
   1149  1.22  jdolecek 				sd[0] = '\0';
   1150  1.22  jdolecek 
   1151  1.22  jdolecek 			addwhatis(dest, data, sd);
   1152  1.22  jdolecek 		}
   1153   1.1      tron 
   1154   1.1      tron 		obsolete = mp;
   1155   1.1      tron 		mp = mp->mp_right;
   1156   1.1      tron 		free(obsolete);
   1157   1.1      tron 	}
   1158   1.1      tron }
   1159   1.1      tron 
   1160  1.37      tron static void
   1161  1.18  christos dumpwhatis(FILE *out, whatis *tree)
   1162   1.1      tron {
   1163   1.1      tron 	while (tree != NULL) {
   1164   1.1      tron 		if (tree->wi_left)
   1165  1.18  christos 			dumpwhatis(out, tree->wi_left);
   1166   1.1      tron 
   1167  1.22  jdolecek 		if ((tree->wi_data[0] && fputs(tree->wi_prefix, out) == EOF) ||
   1168  1.22  jdolecek 		    (fputs(tree->wi_data, out) == EOF) ||
   1169   1.1      tron 		    (fputc('\n', out) == EOF))
   1170  1.18  christos 			err(EXIT_FAILURE, "Write failed");
   1171   1.1      tron 
   1172   1.1      tron 		tree = tree->wi_right;
   1173   1.1      tron 	}
   1174  1.18  christos }
   1175