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