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