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