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