Home | History | Annotate | Line # | Download | only in makewhatis
makewhatis.c revision 1.13
      1  1.13   cgd /*	$NetBSD: makewhatis.c,v 1.13 2001/02/19 22:46:14 cgd 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.1  tron  * 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.1  tron #include <sys/cdefs.h>
     40   1.1  tron #ifndef lint
     41   1.1  tron __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
     42   1.1  tron 	All rights reserved.\n");
     43   1.1  tron #endif /* not lint */
     44   1.1  tron 
     45   1.1  tron #ifndef lint
     46  1.13   cgd __RCSID("$NetBSD: makewhatis.c,v 1.13 2001/02/19 22:46:14 cgd Exp $");
     47   1.1  tron #endif /* not lint */
     48   1.1  tron 
     49   1.1  tron #include <sys/types.h>
     50   1.8  tron #include <sys/param.h>
     51   1.1  tron #include <sys/stat.h>
     52   1.8  tron #include <sys/wait.h>
     53   1.1  tron 
     54   1.1  tron #include <ctype.h>
     55   1.1  tron #include <err.h>
     56   1.1  tron #include <errno.h>
     57  1.10  tron #include <fcntl.h>
     58   1.1  tron #include <fts.h>
     59   1.1  tron #include <locale.h>
     60   1.8  tron #include <paths.h>
     61  1.11  tron #include <signal.h>
     62   1.1  tron #include <stdio.h>
     63   1.1  tron #include <stdlib.h>
     64   1.1  tron #include <string.h>
     65   1.1  tron #include <unistd.h>
     66   1.1  tron #include <zlib.h>
     67   1.1  tron 
     68   1.1  tron typedef struct manpagestruct manpage;
     69   1.1  tron struct manpagestruct {
     70   1.1  tron 	manpage *mp_left,*mp_right;
     71   1.1  tron 	ino_t	 mp_inode;
     72   1.1  tron 	char     mp_name[1];
     73   1.1  tron };
     74   1.1  tron 
     75   1.1  tron typedef struct whatisstruct whatis;
     76   1.1  tron struct whatisstruct {
     77   1.1  tron 	whatis	*wi_left,*wi_right;
     78   1.1  tron 	char	*wi_data;
     79   1.1  tron };
     80   1.1  tron 
     81   1.1  tron int              main (int, char **);
     82   1.6  tron char		*findwhitespace(char *);
     83   1.5  tron char		*GetS(gzFile, char *, int);
     84   1.1  tron int		 manpagesection (char *);
     85   1.1  tron int		 addmanpage (manpage **, ino_t, char *);
     86   1.1  tron int		 addwhatis (whatis **, char *);
     87   1.1  tron char		*replacestring (char *, char *, char *);
     88   1.1  tron void		 catpreprocess (char *);
     89   1.1  tron char		*parsecatpage (gzFile *);
     90   1.1  tron int		 manpreprocess (char *);
     91   1.8  tron char		*nroff (gzFile *);
     92   1.1  tron char		*parsemanpage (gzFile *, int);
     93   1.1  tron char		*getwhatisdata (char *);
     94   1.1  tron void		 processmanpages (manpage **,whatis **);
     95   1.1  tron int		 dumpwhatis (FILE *, whatis *);
     96   1.1  tron 
     97   1.1  tron char *default_manpath[] = {
     98   1.1  tron 	"/usr/share/man",
     99   1.1  tron 	NULL
    100   1.1  tron };
    101   1.1  tron 
    102   1.4  tron char sectionext[] = "0123456789ln";
    103   1.4  tron char whatisdb[]   = "whatis.db";
    104   1.1  tron 
    105   1.1  tron int
    106   1.1  tron main(int argc,char **argv)
    107   1.1  tron {
    108   1.1  tron 	char	**manpath;
    109   1.1  tron 	FTS	*fts;
    110   1.1  tron 	FTSENT	*fe;
    111   1.1  tron 	manpage	*source;
    112   1.1  tron 	whatis	*dest;
    113   1.1  tron 	FILE	*out;
    114   1.1  tron 
    115   1.1  tron 	(void)setlocale(LC_ALL, "");
    116   1.1  tron 
    117   1.1  tron 	manpath = (argc < 2) ? default_manpath : &argv[1];
    118   1.1  tron 
    119   1.1  tron 	if ((fts = fts_open(manpath, FTS_LOGICAL, NULL)) == NULL) {
    120  1.13   cgd 		perror(getprogname());
    121   1.1  tron 		return EXIT_FAILURE;
    122   1.1  tron 	}
    123   1.1  tron 
    124   1.1  tron 	source = NULL;
    125   1.1  tron 	while ((fe = fts_read(fts)) != NULL) {
    126   1.1  tron 		switch (fe->fts_info) {
    127   1.1  tron 		case FTS_F:
    128   1.1  tron 			if (manpagesection(fe->fts_path) >= 0)
    129   1.1  tron 				if (!addmanpage(&source,
    130   1.1  tron 					fe->fts_statp->st_ino,
    131   1.1  tron 					fe->fts_path))
    132   1.1  tron 					err(EXIT_FAILURE, NULL);
    133   1.1  tron 		case FTS_D:
    134   1.4  tron 		case FTS_DC:
    135   1.4  tron 		case FTS_DEFAULT:
    136   1.1  tron 		case FTS_DP:
    137   1.4  tron 		case FTS_SLNONE:
    138   1.1  tron 			break;
    139   1.1  tron 		default:
    140   1.1  tron 			errx(EXIT_FAILURE, "%s: %s", fe->fts_path,
    141   1.1  tron 			    strerror(fe->fts_errno));
    142   1.8  tron 
    143   1.1  tron 		}
    144   1.1  tron 	}
    145   1.1  tron 
    146   1.1  tron 	(void)fts_close(fts);
    147   1.1  tron 
    148   1.1  tron 	dest = NULL;
    149   1.1  tron 	processmanpages(&source, &dest);
    150   1.1  tron 
    151   1.1  tron 	if (chdir(manpath[0]) < 0)
    152   1.1  tron 		errx(EXIT_FAILURE, "%s: %s", manpath[0], strerror(errno));
    153   1.1  tron 
    154   1.1  tron 	if ((out = fopen(whatisdb, "w")) == NULL)
    155   1.1  tron 		errx(EXIT_FAILURE, "%s: %s", whatisdb, strerror(errno));
    156   1.1  tron 
    157   1.3  tron 	if (!(dumpwhatis(out, dest) ||
    158   1.3  tron 	    (fclose(out) < 0)) ||
    159   1.3  tron 	    (chmod(whatisdb, S_IRUSR|S_IRGRP|S_IROTH) < 0))
    160   1.1  tron 		errx(EXIT_FAILURE, "%s: %s", whatisdb, strerror(errno));
    161   1.1  tron 
    162   1.1  tron 	return EXIT_SUCCESS;
    163   1.1  tron }
    164   1.1  tron 
    165   1.5  tron char
    166   1.6  tron *findwhitespace(char *str)
    167   1.6  tron 
    168   1.6  tron {
    169   1.6  tron 	while (!isspace(*str))
    170   1.6  tron 		if (*str++ == '\0') {
    171   1.6  tron 			str = NULL;
    172   1.6  tron 			break;
    173   1.6  tron 		}
    174   1.6  tron 
    175   1.6  tron 	return str;
    176   1.6  tron }
    177   1.6  tron 
    178   1.6  tron char
    179   1.5  tron *GetS(gzFile in, char *buffer, int length)
    180   1.5  tron 
    181   1.5  tron {
    182   1.5  tron 	char	*ptr;
    183   1.5  tron 
    184   1.5  tron 	if (((ptr = gzgets(in, buffer, length)) != NULL) && (*ptr == '\0'))
    185   1.5  tron 		ptr = NULL;
    186   1.5  tron 
    187   1.5  tron 	return ptr;
    188   1.5  tron }
    189   1.5  tron 
    190   1.1  tron int
    191   1.1  tron manpagesection(char *name)
    192   1.1  tron {
    193   1.1  tron 	char	*ptr;
    194   1.1  tron 
    195   1.1  tron 	if ((ptr = strrchr(name, '/')) != NULL)
    196   1.1  tron 		ptr++;
    197   1.1  tron 	else
    198   1.1  tron 		ptr = name;
    199   1.1  tron 
    200   1.4  tron 	while ((ptr = strchr(ptr, '.')) != NULL) {
    201   1.4  tron 		int section;
    202   1.4  tron 
    203   1.4  tron 		ptr++;
    204   1.4  tron 		section=0;
    205   1.4  tron 		while (sectionext[section] != '\0')
    206   1.4  tron 			if (sectionext[section] == *ptr)
    207   1.4  tron 				return section;
    208   1.4  tron 			else
    209   1.4  tron 				section++;
    210   1.4  tron 	}
    211   1.1  tron 
    212   1.1  tron 	return -1;
    213   1.1  tron }
    214   1.1  tron 
    215   1.1  tron int
    216   1.1  tron addmanpage(manpage **tree,ino_t inode,char *name)
    217   1.1  tron {
    218   1.1  tron 	manpage	*mp;
    219   1.1  tron 
    220   1.1  tron 	while ((mp = *tree) != NULL) {
    221   1.1  tron 		if (mp->mp_inode == inode)
    222   1.1  tron 			return 1;
    223   1.1  tron 		tree = &((inode < mp->mp_inode) ? mp->mp_left : mp->mp_right);
    224   1.1  tron 	}
    225   1.1  tron 
    226   1.1  tron 	if ((mp = malloc(sizeof(manpage) + strlen(name))) == NULL)
    227   1.1  tron 		return 0;
    228   1.1  tron 
    229   1.1  tron 	mp->mp_left = NULL;
    230   1.1  tron 	mp->mp_right = NULL;
    231   1.1  tron 	mp->mp_inode = inode;
    232   1.1  tron 	(void) strcpy(mp->mp_name, name);
    233   1.1  tron 	*tree = mp;
    234   1.1  tron 
    235   1.1  tron 	return 1;
    236   1.1  tron }
    237   1.1  tron 
    238   1.1  tron int
    239   1.1  tron addwhatis(whatis **tree, char *data)
    240   1.1  tron {
    241   1.1  tron 	whatis *wi;
    242   1.1  tron 	int result;
    243   1.1  tron 
    244   1.7  tron 	while (isspace(*data))
    245   1.7  tron 		data++;
    246   1.7  tron 
    247   1.7  tron 	if (*data == '/') {
    248   1.7  tron 		char *ptr;
    249   1.7  tron 
    250   1.7  tron 		ptr = ++data;
    251   1.7  tron 		while ((*ptr != '\0') && !isspace(*ptr))
    252   1.7  tron 			if (*ptr++ == '/')
    253   1.7  tron 				data = ptr;
    254   1.7  tron 	}
    255   1.7  tron 
    256   1.1  tron 	while ((wi = *tree) != NULL) {
    257   1.1  tron 		result=strcmp(data, wi->wi_data);
    258   1.1  tron 		if (result == 0) return 1;
    259   1.1  tron 		tree = &((result < 0) ? wi->wi_left : wi->wi_right);
    260   1.1  tron 	}
    261   1.1  tron 
    262   1.1  tron 	if ((wi = malloc(sizeof(whatis) + strlen(data))) == NULL)
    263   1.1  tron 		return 0;
    264   1.1  tron 
    265   1.1  tron 	wi->wi_left = NULL;
    266   1.1  tron 	wi->wi_right = NULL;
    267   1.1  tron 	wi->wi_data = data;
    268   1.1  tron 	*tree = wi;
    269   1.1  tron 
    270   1.1  tron 	return 1;
    271   1.1  tron }
    272   1.1  tron 
    273   1.1  tron void
    274   1.1  tron catpreprocess(char *from)
    275   1.1  tron {
    276   1.1  tron 	char	*to;
    277   1.1  tron 
    278   1.1  tron 	to = from;
    279   1.1  tron 	while (isspace(*from)) from++;
    280   1.1  tron 
    281   1.1  tron 	while (*from != '\0')
    282   1.1  tron 		if (isspace(*from)) {
    283   1.1  tron 			while (isspace(*++from));
    284   1.1  tron 			if (*from != '\0')
    285   1.1  tron 				*to++ = ' ';
    286   1.1  tron 		}
    287   1.1  tron 		else if (*(from + 1) == '\10')
    288   1.1  tron 			from += 2;
    289   1.1  tron 		else
    290   1.1  tron 			*to++ = *from++;
    291   1.1  tron 
    292   1.1  tron 	*to = '\0';
    293   1.1  tron }
    294   1.1  tron 
    295   1.1  tron char *
    296   1.1  tron replacestring(char *string, char *old, char *new)
    297   1.1  tron 
    298   1.1  tron {
    299   1.1  tron 	char	*ptr, *result;
    300   1.1  tron 	int	 slength, olength, nlength, pos;
    301   1.1  tron 
    302   1.1  tron 	if (new == NULL)
    303   1.1  tron 		return strdup(string);
    304   1.1  tron 
    305   1.1  tron 	ptr = strstr(string, old);
    306   1.1  tron 	if (ptr == NULL)
    307   1.1  tron 		return strdup(string);
    308   1.1  tron 
    309   1.1  tron 	slength = strlen(string);
    310   1.1  tron 	olength = strlen(old);
    311   1.1  tron 	nlength = strlen(new);
    312   1.1  tron 	if ((result = malloc(slength - olength + nlength + 1)) == NULL)
    313   1.1  tron 		return NULL;
    314   1.1  tron 
    315   1.1  tron 	pos = ptr - string;
    316   1.1  tron 	(void) memcpy(result, string, pos);
    317   1.1  tron 	(void) memcpy(&result[pos], new, nlength);
    318   1.1  tron 	(void) strcpy(&result[pos + nlength], &string[pos + olength]);
    319   1.1  tron 
    320   1.1  tron 	return result;
    321   1.1  tron }
    322   1.1  tron 
    323   1.1  tron char *
    324   1.1  tron parsecatpage(gzFile *in)
    325   1.1  tron {
    326   1.1  tron 	char 	 buffer[8192];
    327   1.1  tron 	char	*section, *ptr, *last;
    328   1.1  tron 	int	 size;
    329   1.1  tron 
    330   1.1  tron 	do {
    331   1.5  tron 		if (GetS(in, buffer, sizeof(buffer)) == NULL)
    332   1.1  tron 			return NULL;
    333   1.1  tron 	}
    334   1.1  tron 	while (buffer[0] == '\n');
    335   1.1  tron 
    336   1.1  tron 	section = NULL;
    337   1.1  tron 	if ((ptr = strchr(buffer, '(')) != NULL) {
    338   1.1  tron 		if ((last = strchr(ptr + 1, ')')) !=NULL) {
    339   1.1  tron 			int 	length;
    340   1.1  tron 
    341   1.1  tron 			length = last - ptr + 1;
    342   1.1  tron 			if ((section = malloc(length + 5)) == NULL)
    343   1.1  tron 				return NULL;
    344   1.1  tron 
    345   1.1  tron 			*section = ' ';
    346   1.1  tron 			(void) memcpy(section + 1, ptr, length);
    347   1.1  tron 			(void) strcpy(section + 1 + length, " - ");
    348   1.1  tron 		}
    349   1.1  tron 	}
    350   1.1  tron 
    351   1.1  tron 	for (;;) {
    352   1.5  tron 		if (GetS(in, buffer, sizeof(buffer)) == NULL) {
    353   1.1  tron 			free(section);
    354   1.1  tron 			return NULL;
    355   1.1  tron 		}
    356   1.1  tron 		if (strncmp(buffer, "N\10NA\10AM\10ME\10E", 12) == 0)
    357   1.1  tron 			break;
    358   1.1  tron 	}
    359   1.1  tron 
    360   1.1  tron 	ptr = last = buffer;
    361   1.1  tron 	size = sizeof(buffer) - 1;
    362   1.5  tron 	while ((size > 0) && (GetS(in, ptr, size) != NULL)) {
    363   1.1  tron 		int	 length;
    364   1.1  tron 
    365   1.1  tron 		catpreprocess(ptr);
    366   1.1  tron 
    367   1.1  tron 		length = strlen(ptr);
    368   1.1  tron 		if (length == 0) {
    369   1.1  tron 			*last = '\0';
    370   1.1  tron 
    371   1.1  tron 			ptr = replacestring(buffer, " - ", section);
    372   1.1  tron 			free(section);
    373   1.1  tron 			return ptr;
    374   1.1  tron 		}
    375   1.1  tron 		if ((length > 1) && (ptr[length - 1] == '-') &&
    376   1.1  tron 		    isalpha(ptr[length - 2]))
    377   1.1  tron 			last = &ptr[--length];
    378   1.1  tron 		else {
    379   1.1  tron 			last = &ptr[length++];
    380   1.1  tron 			*last = ' ';
    381   1.1  tron 		}
    382   1.1  tron 
    383   1.1  tron 		ptr += length;
    384   1.1  tron 		size -= length;
    385   1.1  tron 	}
    386   1.1  tron 
    387   1.1  tron 	free(section);
    388   1.1  tron 
    389   1.1  tron 	return NULL;
    390   1.1  tron }
    391   1.1  tron 
    392   1.1  tron int
    393   1.1  tron manpreprocess(char *line)
    394   1.1  tron {
    395   1.1  tron 	char	*from, *to;
    396   1.1  tron 
    397   1.1  tron 	to = from = line;
    398   1.1  tron 	while (isspace(*from)) from++;
    399   1.1  tron 	if (strncmp(from, ".\\\"", 3) == 0)
    400   1.1  tron 		return 1;
    401   1.1  tron 
    402   1.1  tron 	while (*from != '\0')
    403   1.1  tron 		if (isspace(*from)) {
    404   1.1  tron 			while (isspace(*++from));
    405   1.1  tron 			if ((*from != '\0') && (*from != ','))
    406   1.1  tron 				*to++ = ' ';
    407   1.1  tron 		}
    408   1.1  tron 		else if (*from == '\\')
    409   1.1  tron 			switch (*++from) {
    410   1.1  tron 			case '\0':
    411   1.1  tron 			case '-':
    412   1.1  tron 				break;
    413   1.7  tron 			case 's':
    414   1.7  tron 				if ((*from=='+') || (*from=='-'))
    415   1.7  tron 					from++;
    416   1.7  tron 				while (isdigit(*from))
    417   1.7  tron 					from++;
    418   1.7  tron 				break;
    419   1.1  tron 			default:
    420   1.1  tron 				from++;
    421   1.1  tron 			}
    422   1.1  tron 		else
    423   1.1  tron 			if (*from == '"')
    424   1.1  tron 				from++;
    425   1.1  tron 			else
    426   1.1  tron 				*to++ = *from++;
    427   1.1  tron 
    428   1.1  tron 	*to = '\0';
    429   1.1  tron 
    430   1.1  tron 	if (strncasecmp(line, ".Xr", 3) == 0) {
    431   1.1  tron 		char	*sect;
    432   1.1  tron 
    433   1.1  tron 		from = line + 3;
    434   1.1  tron 		if (isspace(*from))
    435   1.1  tron 			from++;
    436   1.1  tron 
    437   1.6  tron 		if ((sect = findwhitespace(from)) != NULL) {
    438   1.1  tron 			int	 length;
    439   1.1  tron 
    440   1.1  tron 			*sect++ = '\0';
    441   1.1  tron 			length = strlen(from);
    442   1.1  tron 			(void) memmove(line, from, length);
    443   1.1  tron 			line[length++] = '(';
    444   1.1  tron 			to = &line[length];
    445   1.1  tron 			length = strlen(sect);
    446   1.1  tron 			(void) memmove(to, sect, length);
    447   1.1  tron 			(void) strcpy(&to[length], ")");
    448   1.1  tron 		}
    449   1.1  tron 	}
    450   1.1  tron 
    451   1.1  tron 	return 0;
    452   1.1  tron }
    453   1.1  tron 
    454   1.1  tron char *
    455   1.8  tron nroff(gzFile *in)
    456   1.8  tron {
    457   1.9  tron 	char tempname[MAXPATHLEN], buffer[65536], *data;
    458   1.9  tron 	int tempfd, bytes, pipefd[2], status;
    459  1.10  tron 	static int devnull = -1;
    460   1.8  tron 	pid_t child;
    461   1.8  tron 
    462   1.8  tron 	if (gzrewind(in) < 0) {
    463  1.13   cgd 		perror(getprogname());
    464   1.8  tron 		return NULL;
    465   1.8  tron 	}
    466   1.8  tron 
    467  1.10  tron 	if ((devnull < 0) &&
    468  1.10  tron 	    ((devnull = open(_PATH_DEVNULL, O_WRONLY, 0)) < 0)) {
    469  1.13   cgd 		perror(getprogname());
    470  1.10  tron 		return NULL;
    471  1.10  tron 	}
    472  1.10  tron 
    473   1.8  tron 	(void)strcpy(tempname, _PATH_TMP "makewhatis.XXXXXX");
    474   1.8  tron 	if ((tempfd = mkstemp(tempname)) < 0) {
    475  1.13   cgd 		perror(getprogname());
    476   1.8  tron 		return NULL;
    477   1.8  tron 	}
    478   1.8  tron 
    479   1.8  tron 	while ((bytes = gzread(in, buffer, sizeof(buffer))) > 0)
    480   1.8  tron 		if (write(tempfd, buffer, bytes) != bytes) {
    481   1.8  tron 			bytes = -1;
    482   1.8  tron 			break;
    483   1.8  tron 		}
    484   1.8  tron 
    485   1.8  tron 	if ((bytes < 0) ||
    486   1.8  tron             (lseek(tempfd, 0, SEEK_SET) < 0) ||
    487   1.8  tron             (pipe(pipefd) < 0)) {
    488  1.13   cgd 		perror(getprogname());
    489   1.8  tron 		(void)close(tempfd);
    490   1.8  tron 		(void)unlink(tempname);
    491   1.8  tron 		return NULL;
    492   1.8  tron 	}
    493   1.8  tron 
    494   1.8  tron 	switch (child = vfork()) {
    495   1.8  tron 	case -1:
    496  1.13   cgd 		perror(getprogname());
    497   1.8  tron 		(void)close(pipefd[1]);
    498   1.8  tron 		(void)close(pipefd[0]);
    499   1.8  tron 		(void)close(tempfd);
    500   1.8  tron 		(void)unlink(tempname);
    501   1.8  tron 		return NULL;
    502   1.8  tron 		/* NOTREACHED */
    503   1.8  tron 	case 0:
    504   1.8  tron 		(void)close(pipefd[0]);
    505  1.10  tron 		if (tempfd != STDIN_FILENO) {
    506  1.10  tron 			(void)dup2(tempfd, STDIN_FILENO);
    507  1.10  tron 			(void)close(tempfd);
    508  1.10  tron 		}
    509   1.8  tron 		if (pipefd[1] != STDOUT_FILENO) {
    510   1.8  tron 			(void)dup2(pipefd[1], STDOUT_FILENO);
    511   1.8  tron 			(void)close(pipefd[1]);
    512   1.8  tron 		}
    513  1.10  tron 		if (devnull != STDERR_FILENO) {
    514  1.10  tron 			(void)dup2(devnull, STDERR_FILENO);
    515  1.10  tron 			(void)close(devnull);
    516  1.10  tron 		}
    517  1.12  tron 		(void)execlp("nroff", "nroff", "-S", "-man", NULL);
    518   1.8  tron 		_exit(EXIT_FAILURE);
    519   1.8  tron 	default:
    520   1.8  tron 		(void)close(pipefd[1]);
    521   1.8  tron 		(void)close(tempfd);
    522   1.8  tron 		/* NOTREACHED */
    523   1.8  tron 	}
    524   1.8  tron 
    525   1.8  tron 	if ((in = gzdopen(pipefd[0], "r")) == NULL) {
    526   1.8  tron 		if (errno == 0)
    527   1.8  tron 			errno = ENOMEM;
    528  1.13   cgd 		perror(getprogname());
    529  1.11  tron 		(void)close(pipefd[0]);
    530  1.11  tron 		(void)kill(child, SIGTERM);
    531  1.11  tron 		while (waitpid(child, NULL, 0) != child);
    532   1.8  tron 		(void)unlink(tempname);
    533   1.8  tron 		return NULL;
    534   1.8  tron 	}
    535   1.8  tron 
    536   1.8  tron 	data = parsecatpage(in);
    537   1.9  tron 	while (gzread(in, buffer, sizeof(buffer)) > 0);
    538   1.9  tron 	(void)gzclose(in);
    539   1.9  tron 
    540   1.9  tron 	while (waitpid(child, &status, 0) != child);
    541   1.9  tron 	if ((data != NULL) &&
    542   1.9  tron 	    !(WIFEXITED(status) && (WEXITSTATUS(status) == 0))) {
    543   1.9  tron 		free(data);
    544   1.9  tron 		data = NULL;
    545   1.9  tron 	}
    546   1.8  tron 
    547   1.8  tron 	(void)unlink(tempname);
    548   1.8  tron 
    549   1.8  tron 	return data;
    550   1.8  tron }
    551   1.8  tron 
    552   1.8  tron char *
    553   1.1  tron parsemanpage(gzFile *in, int defaultsection)
    554   1.1  tron {
    555   1.1  tron 	char	*section, buffer[8192], *ptr;
    556   1.1  tron 
    557   1.1  tron 	section = NULL;
    558   1.1  tron 	do {
    559   1.5  tron 		if (GetS(in, buffer, sizeof(buffer) - 1) == NULL) {
    560   1.1  tron 			free(section);
    561   1.1  tron 			return NULL;
    562   1.1  tron 		}
    563   1.1  tron 		if (manpreprocess(buffer))
    564   1.1  tron 			continue;
    565   1.1  tron 		if (strncasecmp(buffer, ".Dt", 3) == 0) {
    566   1.1  tron 			char	*end;
    567   1.1  tron 
    568   1.1  tron 			ptr = &buffer[3];
    569   1.1  tron 			if (isspace(*ptr))
    570   1.1  tron 				ptr++;
    571   1.6  tron 			if ((ptr = findwhitespace(ptr)) == NULL)
    572   1.1  tron 				continue;
    573   1.1  tron 
    574   1.6  tron 			if ((end = findwhitespace(++ptr)) != NULL)
    575   1.1  tron 				*end = '\0';
    576   1.1  tron 
    577   1.1  tron 			free(section);
    578   1.1  tron 			if ((section = malloc(strlen(ptr) + 7)) != NULL) {
    579   1.1  tron 				section[0] = ' ';
    580   1.1  tron 				section[1] = '(';
    581   1.1  tron 				(void) strcpy(&section[2], ptr);
    582   1.1  tron 				(void) strcat(&section[2], ") - ");
    583   1.1  tron 			}
    584   1.1  tron 		}
    585   1.8  tron 		else if (strncasecmp(buffer, ".Ds", 3) == 0)
    586   1.8  tron 			return nroff(in);
    587   1.1  tron 	} while ((strncasecmp(buffer, ".Sh NAME", 8) != 0));
    588   1.1  tron 
    589   1.1  tron 	do {
    590   1.5  tron 		if (GetS(in, buffer, sizeof(buffer) - 1) == NULL) {
    591   1.1  tron 			free(section);
    592   1.1  tron 			return NULL;
    593   1.1  tron 		}
    594   1.1  tron 	} while (manpreprocess(buffer));
    595   1.1  tron 
    596   1.1  tron 	if (strncasecmp(buffer, ".Nm", 3) == 0) {
    597   1.1  tron 		int	length, offset;
    598   1.1  tron 
    599   1.1  tron 		ptr = &buffer[3];
    600   1.7  tron 		while (isspace(*ptr))
    601   1.1  tron 			ptr++;
    602   1.1  tron 
    603   1.1  tron 		length = strlen(ptr);
    604   1.1  tron 		if ((length > 1) && (ptr[length - 1] == ',') &&
    605   1.1  tron 		    isspace(ptr[length - 2])) {
    606   1.1  tron 			ptr[--length] = '\0';
    607   1.1  tron 			ptr[length - 1] = ',';
    608   1.1  tron 		}
    609   1.1  tron 		(void) memmove(buffer, ptr, length + 1);
    610   1.1  tron 
    611   1.1  tron 		offset = length + 3;
    612   1.1  tron 		ptr = &buffer[offset];
    613   1.1  tron 		for (;;) {
    614   1.1  tron 			int	 more;
    615   1.1  tron 
    616   1.1  tron 			if ((sizeof(buffer) == offset) ||
    617   1.5  tron 		            (GetS(in, ptr, sizeof(buffer) - offset)
    618   1.1  tron 			       == NULL)) {
    619   1.1  tron 				free(section);
    620   1.1  tron 				return NULL;
    621   1.1  tron 			}
    622   1.1  tron 			if (manpreprocess(ptr))
    623   1.1  tron 				continue;
    624   1.1  tron 
    625   1.1  tron 			if (strncasecmp(ptr, ".Nm", 3) != 0) break;
    626   1.1  tron 
    627   1.1  tron 			ptr += 3;
    628   1.1  tron 			if (isspace(*ptr))
    629   1.1  tron 				ptr++;
    630   1.1  tron 
    631   1.1  tron 			buffer[length++] = ' ';
    632   1.1  tron 			more = strlen(ptr);
    633   1.1  tron 			if ((more > 1) && (ptr[more - 1] == ',') &&
    634   1.1  tron 			    isspace(ptr[more - 2])) {
    635   1.1  tron 				ptr[--more] = '\0';
    636   1.1  tron 				ptr[more - 1] = ',';
    637   1.1  tron 			}
    638   1.1  tron 
    639   1.1  tron 			(void) memmove(&buffer[length], ptr, more + 1);
    640   1.1  tron 			length += more;
    641   1.1  tron 			offset = length + 3;
    642   1.1  tron 
    643   1.1  tron 			ptr = &buffer[offset];
    644   1.1  tron 		}
    645   1.1  tron 
    646   1.1  tron 		if (strncasecmp(ptr, ".Nd", 3) == 0) {
    647   1.1  tron 			(void) strcpy(&buffer[length], " -");
    648   1.1  tron 
    649   1.1  tron 			while (strncasecmp(ptr, ".Sh", 3) != 0) {
    650   1.1  tron 				int	 more;
    651   1.1  tron 
    652   1.1  tron 				if (*ptr == '.') {
    653   1.1  tron 					char	*space;
    654   1.1  tron 
    655   1.6  tron 					if ((space = findwhitespace(ptr)) == NULL)
    656   1.1  tron 						ptr = "";
    657   1.1  tron 					else {
    658   1.1  tron 						space++;
    659   1.1  tron 						(void) memmove(ptr, space,
    660   1.1  tron 							   strlen(space) + 1);
    661   1.1  tron 					}
    662   1.1  tron 				}
    663   1.1  tron 
    664   1.1  tron 				if (*ptr != '\0') {
    665   1.1  tron 					buffer[offset - 1] = ' ';
    666   1.1  tron 					more = strlen(ptr) + 1;
    667   1.1  tron 					offset += more;
    668   1.1  tron 				}
    669   1.1  tron 				ptr = &buffer[offset];
    670   1.1  tron 				if ((sizeof(buffer) == offset) ||
    671   1.5  tron 			            (GetS(in, ptr, sizeof(buffer) - offset)
    672   1.1  tron 					== NULL)) {
    673   1.1  tron 					free(section);
    674   1.1  tron 					return NULL;
    675   1.1  tron 				}
    676   1.1  tron 				if (manpreprocess(ptr))
    677   1.1  tron 					*ptr = '\0';
    678   1.1  tron 			}
    679   1.1  tron 		}
    680   1.1  tron 	}
    681   1.1  tron 	else {
    682   1.1  tron 		int	 offset;
    683   1.1  tron 
    684   1.1  tron 		if (*buffer == '.') {
    685   1.1  tron 			char	*space;
    686   1.1  tron 
    687   1.6  tron 			if ((space = findwhitespace(buffer)) == NULL) {
    688   1.1  tron 				free(section);
    689   1.1  tron 				return NULL;
    690   1.1  tron 			}
    691   1.1  tron 			space++;
    692   1.5  tron 			(void) memmove(buffer, space, strlen(space) + 1);
    693   1.1  tron 		}
    694   1.1  tron 
    695   1.1  tron 		offset = strlen(buffer) + 1;
    696   1.1  tron 		for (;;) {
    697   1.1  tron 			int	 more;
    698   1.1  tron 
    699   1.1  tron 			ptr = &buffer[offset];
    700   1.1  tron 			if ((sizeof(buffer) == offset) ||
    701   1.5  tron 		            (GetS(in, ptr, sizeof(buffer) - offset)
    702   1.1  tron 				== NULL)) {
    703   1.1  tron 				free(section);
    704   1.1  tron 				return NULL;
    705   1.1  tron 			}
    706   1.1  tron 			if (manpreprocess(ptr) || (*ptr == '\0'))
    707   1.1  tron 				continue;
    708   1.1  tron 
    709   1.5  tron 			if ((strncasecmp(ptr, ".Sh", 3) == 0) ||
    710   1.5  tron 			    (strncasecmp(ptr, ".Ss", 3) == 0))
    711   1.1  tron 				break;
    712   1.1  tron 
    713   1.1  tron 			if (*ptr == '.') {
    714   1.1  tron 				char	*space;
    715   1.1  tron 
    716   1.6  tron 				if ((space = findwhitespace(ptr)) == NULL) {
    717   1.1  tron 					continue;
    718   1.5  tron 				}
    719   1.5  tron 
    720   1.1  tron 				space++;
    721   1.5  tron 				(void) memmove(ptr, space, strlen(space) + 1);
    722   1.1  tron 			}
    723   1.1  tron 
    724   1.1  tron 			buffer[offset - 1] = ' ';
    725   1.1  tron 			more = strlen(ptr);
    726   1.1  tron 			if ((more > 1) && (ptr[more - 1] == ',') &&
    727   1.1  tron 			    isspace(ptr[more - 2])) {
    728   1.1  tron 				ptr[more - 1] = '\0';
    729   1.1  tron 				ptr[more - 2] = ',';
    730   1.1  tron 			}
    731   1.1  tron 			else more++;
    732   1.1  tron 			offset += more;
    733   1.1  tron 		}
    734   1.1  tron 	}
    735   1.1  tron 
    736   1.1  tron 	if (section == NULL) {
    737   1.1  tron 		char sectionbuffer[24];
    738   1.1  tron 
    739   1.4  tron 		(void) sprintf(sectionbuffer, " (%c) - ",
    740   1.4  tron 			sectionext[defaultsection]);
    741   1.1  tron 		ptr = replacestring(buffer, " - ", sectionbuffer);
    742   1.1  tron 	}
    743   1.1  tron 	else {
    744   1.1  tron 		ptr = replacestring(buffer, " - ", section);
    745   1.1  tron 		free(section);
    746   1.1  tron 	}
    747   1.1  tron 	return ptr;
    748   1.1  tron }
    749   1.1  tron 
    750   1.1  tron char *
    751   1.1  tron getwhatisdata(char *name)
    752   1.1  tron {
    753   1.1  tron 	gzFile	*in;
    754   1.1  tron 	char	*data;
    755   1.1  tron 	int	 section;
    756   1.1  tron 
    757   1.1  tron 	if ((in = gzopen(name, "r")) == NULL) {
    758   1.1  tron 		errx(EXIT_FAILURE, "%s: %s",
    759   1.1  tron 		    name,
    760   1.1  tron 		    strerror((errno == 0) ? ENOMEM : errno));
    761   1.1  tron 		/* NOTREACHED */
    762   1.1  tron 	}
    763   1.1  tron 
    764   1.1  tron 	section = manpagesection(name);
    765   1.1  tron 	data = (section == 0) ? parsecatpage(in) : parsemanpage(in, section);
    766   1.1  tron 
    767   1.1  tron 	(void) gzclose(in);
    768   1.1  tron 	return data;
    769   1.1  tron }
    770   1.1  tron 
    771   1.1  tron void
    772   1.1  tron processmanpages(manpage **source, whatis **dest)
    773   1.1  tron {
    774   1.1  tron 	manpage	*mp;
    775   1.1  tron 
    776   1.1  tron 	mp = *source;
    777   1.1  tron 	*source = NULL;
    778   1.1  tron 
    779   1.1  tron 	while (mp != NULL) {
    780   1.1  tron 		manpage *obsolete;
    781   1.1  tron 		char *data;
    782   1.1  tron 
    783   1.1  tron 		if (mp->mp_left != NULL)
    784   1.1  tron 			processmanpages(&mp->mp_left,dest);
    785   1.1  tron 
    786   1.1  tron 		if ((data = getwhatisdata(mp->mp_name)) != NULL) {
    787   1.1  tron 			if (!addwhatis(dest,data))
    788   1.1  tron 				err(EXIT_FAILURE, NULL);
    789   1.1  tron 		}
    790   1.1  tron 
    791   1.1  tron 		obsolete = mp;
    792   1.1  tron 		mp = mp->mp_right;
    793   1.1  tron 		free(obsolete);
    794   1.1  tron 	}
    795   1.1  tron }
    796   1.1  tron 
    797   1.1  tron int
    798   1.1  tron dumpwhatis (FILE *out, whatis *tree)
    799   1.1  tron {
    800   1.1  tron 	while (tree != NULL) {
    801   1.1  tron 		if (tree->wi_left)
    802   1.1  tron 			if (!dumpwhatis(out, tree->wi_left)) return 0;
    803   1.1  tron 
    804   1.1  tron 		if ((fputs(tree->wi_data, out) == EOF) ||
    805   1.1  tron 		    (fputc('\n', out) == EOF))
    806   1.1  tron 			return 0;
    807   1.1  tron 
    808   1.1  tron 		tree = tree->wi_right;
    809   1.1  tron 	}
    810   1.1  tron 
    811   1.1  tron 	return 1;
    812   1.1  tron }
    813