Home | History | Annotate | Line # | Download | only in makewhatis
makewhatis.c revision 1.4.2.1
      1  1.4.2.1  wrstuden /*	$NetBSD: makewhatis.c,v 1.4.2.1 1999/12/27 18:30:17 wrstuden 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.4.2.1  wrstuden __RCSID("$NetBSD: makewhatis.c,v 1.4.2.1 1999/12/27 18:30:17 wrstuden Exp $");
     47      1.1      tron #endif /* not lint */
     48      1.1      tron 
     49      1.1      tron #include <sys/types.h>
     50      1.1      tron #include <sys/stat.h>
     51      1.1      tron 
     52      1.1      tron #include <ctype.h>
     53      1.1      tron #include <err.h>
     54      1.1      tron #include <errno.h>
     55      1.1      tron #include <fts.h>
     56      1.1      tron #include <locale.h>
     57      1.1      tron #include <stdio.h>
     58      1.1      tron #include <stdlib.h>
     59      1.1      tron #include <string.h>
     60      1.1      tron #include <unistd.h>
     61      1.1      tron #include <zlib.h>
     62      1.1      tron 
     63      1.1      tron typedef struct manpagestruct manpage;
     64      1.1      tron struct manpagestruct {
     65      1.1      tron 	manpage *mp_left,*mp_right;
     66      1.1      tron 	ino_t	 mp_inode;
     67      1.1      tron 	char     mp_name[1];
     68      1.1      tron };
     69      1.1      tron 
     70      1.1      tron typedef struct whatisstruct whatis;
     71      1.1      tron struct whatisstruct {
     72      1.1      tron 	whatis	*wi_left,*wi_right;
     73      1.1      tron 	char	*wi_data;
     74      1.1      tron };
     75      1.1      tron 
     76      1.1      tron int              main (int, char **);
     77      1.1      tron int		 manpagesection (char *);
     78      1.1      tron int		 addmanpage (manpage **, ino_t, char *);
     79      1.1      tron int		 addwhatis (whatis **, char *);
     80      1.1      tron char		*replacestring (char *, char *, char *);
     81      1.1      tron void		 catpreprocess (char *);
     82      1.1      tron char		*parsecatpage (gzFile *);
     83      1.1      tron int		 manpreprocess (char *);
     84      1.1      tron char		*parsemanpage (gzFile *, int);
     85      1.1      tron char		*getwhatisdata (char *);
     86      1.1      tron void		 processmanpages (manpage **,whatis **);
     87      1.1      tron int		 dumpwhatis (FILE *, whatis *);
     88      1.1      tron 
     89      1.1      tron char *default_manpath[] = {
     90      1.1      tron 	"/usr/share/man",
     91      1.1      tron 	NULL
     92      1.1      tron };
     93      1.1      tron 
     94      1.4      tron char sectionext[] = "0123456789ln";
     95      1.4      tron char whatisdb[]   = "whatis.db";
     96      1.1      tron 
     97      1.1      tron extern char *__progname;
     98      1.1      tron 
     99      1.1      tron int
    100      1.1      tron main(int argc,char **argv)
    101      1.1      tron {
    102      1.1      tron 	char	**manpath;
    103      1.1      tron 	FTS	*fts;
    104      1.1      tron 	FTSENT	*fe;
    105      1.1      tron 	manpage	*source;
    106      1.1      tron 	whatis	*dest;
    107      1.1      tron 	FILE	*out;
    108      1.1      tron 
    109      1.1      tron 	(void)setlocale(LC_ALL, "");
    110      1.1      tron 
    111      1.1      tron 	manpath = (argc < 2) ? default_manpath : &argv[1];
    112      1.1      tron 
    113      1.1      tron 	if ((fts = fts_open(manpath, FTS_LOGICAL, NULL)) == NULL) {
    114      1.1      tron 		perror(__progname);
    115      1.1      tron 		return EXIT_FAILURE;
    116      1.1      tron 	}
    117      1.1      tron 
    118      1.1      tron 	source = NULL;
    119      1.1      tron 	while ((fe = fts_read(fts)) != NULL) {
    120      1.1      tron 		switch (fe->fts_info) {
    121      1.1      tron 		case FTS_F:
    122      1.1      tron 			if (manpagesection(fe->fts_path) >= 0)
    123      1.1      tron 				if (!addmanpage(&source,
    124      1.1      tron 					fe->fts_statp->st_ino,
    125      1.1      tron 					fe->fts_path))
    126      1.1      tron 					err(EXIT_FAILURE, NULL);
    127      1.1      tron 		case FTS_D:
    128      1.4      tron 		case FTS_DC:
    129      1.4      tron 		case FTS_DEFAULT:
    130      1.1      tron 		case FTS_DP:
    131      1.4      tron 		case FTS_SLNONE:
    132      1.1      tron 			break;
    133      1.1      tron 		default:
    134      1.1      tron 			errx(EXIT_FAILURE, "%s: %s", fe->fts_path,
    135      1.1      tron 			    strerror(fe->fts_errno));
    136      1.1      tron 			/* NOTREACHED */
    137      1.1      tron 		}
    138      1.1      tron 	}
    139      1.1      tron 
    140      1.1      tron 	(void)fts_close(fts);
    141      1.1      tron 
    142      1.1      tron 	dest = NULL;
    143      1.1      tron 	processmanpages(&source, &dest);
    144      1.1      tron 
    145      1.1      tron 	if (chdir(manpath[0]) < 0)
    146      1.1      tron 		errx(EXIT_FAILURE, "%s: %s", manpath[0], strerror(errno));
    147      1.1      tron 
    148      1.1      tron 	if ((out = fopen(whatisdb, "w")) == NULL)
    149      1.1      tron 		errx(EXIT_FAILURE, "%s: %s", whatisdb, strerror(errno));
    150      1.1      tron 
    151      1.3      tron 	if (!(dumpwhatis(out, dest) ||
    152      1.3      tron 	    (fclose(out) < 0)) ||
    153      1.3      tron 	    (chmod(whatisdb, S_IRUSR|S_IRGRP|S_IROTH) < 0))
    154      1.1      tron 		errx(EXIT_FAILURE, "%s: %s", whatisdb, strerror(errno));
    155      1.1      tron 
    156      1.1      tron 	return EXIT_SUCCESS;
    157      1.1      tron }
    158      1.1      tron 
    159      1.1      tron int
    160      1.1      tron manpagesection(char *name)
    161      1.1      tron {
    162      1.1      tron 	char	*ptr;
    163      1.1      tron 
    164      1.1      tron 	if ((ptr = strrchr(name, '/')) != NULL)
    165      1.1      tron 		ptr++;
    166      1.1      tron 	else
    167      1.1      tron 		ptr = name;
    168      1.1      tron 
    169      1.4      tron 	while ((ptr = strchr(ptr, '.')) != NULL) {
    170      1.4      tron 		int section;
    171      1.4      tron 
    172      1.4      tron 		ptr++;
    173      1.4      tron 		section=0;
    174      1.4      tron 		while (sectionext[section] != '\0')
    175      1.4      tron 			if (sectionext[section] == *ptr)
    176      1.4      tron 				return section;
    177      1.4      tron 			else
    178      1.4      tron 				section++;
    179      1.4      tron 	}
    180      1.1      tron 
    181      1.1      tron 	return -1;
    182      1.1      tron }
    183      1.1      tron 
    184      1.1      tron int
    185      1.1      tron addmanpage(manpage **tree,ino_t inode,char *name)
    186      1.1      tron {
    187      1.1      tron 	manpage	*mp;
    188      1.1      tron 
    189      1.1      tron 	while ((mp = *tree) != NULL) {
    190      1.1      tron 		if (mp->mp_inode == inode)
    191      1.1      tron 			return 1;
    192      1.1      tron 		tree = &((inode < mp->mp_inode) ? mp->mp_left : mp->mp_right);
    193      1.1      tron 	}
    194      1.1      tron 
    195      1.1      tron 	if ((mp = malloc(sizeof(manpage) + strlen(name))) == NULL)
    196      1.1      tron 		return 0;
    197      1.1      tron 
    198      1.1      tron 	mp->mp_left = NULL;
    199      1.1      tron 	mp->mp_right = NULL;
    200      1.1      tron 	mp->mp_inode = inode;
    201      1.1      tron 	(void) strcpy(mp->mp_name, name);
    202      1.1      tron 	*tree = mp;
    203      1.1      tron 
    204      1.1      tron 	return 1;
    205      1.1      tron }
    206      1.1      tron 
    207      1.1      tron int
    208      1.1      tron addwhatis(whatis **tree, char *data)
    209      1.1      tron {
    210      1.1      tron 	whatis *wi;
    211      1.1      tron 	int result;
    212      1.1      tron 
    213      1.1      tron 	while ((wi = *tree) != NULL) {
    214      1.1      tron 		result=strcmp(data, wi->wi_data);
    215      1.1      tron 		if (result == 0) return 1;
    216      1.1      tron 		tree = &((result < 0) ? wi->wi_left : wi->wi_right);
    217      1.1      tron 	}
    218      1.1      tron 
    219      1.1      tron 	if ((wi = malloc(sizeof(whatis) + strlen(data))) == NULL)
    220      1.1      tron 		return 0;
    221      1.1      tron 
    222      1.1      tron 	wi->wi_left = NULL;
    223      1.1      tron 	wi->wi_right = NULL;
    224      1.1      tron 	wi->wi_data = data;
    225      1.1      tron 	*tree = wi;
    226      1.1      tron 
    227      1.1      tron 	return 1;
    228      1.1      tron }
    229      1.1      tron 
    230      1.1      tron void
    231      1.1      tron catpreprocess(char *from)
    232      1.1      tron {
    233      1.1      tron 	char	*to;
    234      1.1      tron 
    235      1.1      tron 	to = from;
    236      1.1      tron 	while (isspace(*from)) from++;
    237      1.1      tron 
    238      1.1      tron 	while (*from != '\0')
    239      1.1      tron 		if (isspace(*from)) {
    240      1.1      tron 			while (isspace(*++from));
    241      1.1      tron 			if (*from != '\0')
    242      1.1      tron 				*to++ = ' ';
    243      1.1      tron 		}
    244      1.1      tron 		else if (*(from + 1) == '\10')
    245      1.1      tron 			from += 2;
    246      1.1      tron 		else
    247      1.1      tron 			*to++ = *from++;
    248      1.1      tron 
    249      1.1      tron 	*to = '\0';
    250      1.1      tron }
    251      1.1      tron 
    252      1.1      tron char *
    253      1.1      tron replacestring(char *string, char *old, char *new)
    254      1.1      tron 
    255      1.1      tron {
    256      1.1      tron 	char	*ptr, *result;
    257      1.1      tron 	int	 slength, olength, nlength, pos;
    258      1.1      tron 
    259      1.1      tron 	if (new == NULL)
    260      1.1      tron 		return strdup(string);
    261      1.1      tron 
    262      1.1      tron 	ptr = strstr(string, old);
    263      1.1      tron 	if (ptr == NULL)
    264      1.1      tron 		return strdup(string);
    265      1.1      tron 
    266      1.1      tron 	slength = strlen(string);
    267      1.1      tron 	olength = strlen(old);
    268      1.1      tron 	nlength = strlen(new);
    269      1.1      tron 	if ((result = malloc(slength - olength + nlength + 1)) == NULL)
    270      1.1      tron 		return NULL;
    271      1.1      tron 
    272      1.1      tron 	pos = ptr - string;
    273      1.1      tron 	(void) memcpy(result, string, pos);
    274      1.1      tron 	(void) memcpy(&result[pos], new, nlength);
    275      1.1      tron 	(void) strcpy(&result[pos + nlength], &string[pos + olength]);
    276      1.1      tron 
    277      1.1      tron 	return result;
    278      1.1      tron }
    279      1.1      tron 
    280      1.1      tron char *
    281      1.1      tron parsecatpage(gzFile *in)
    282      1.1      tron {
    283      1.1      tron 	char 	 buffer[8192];
    284      1.1      tron 	char	*section, *ptr, *last;
    285      1.1      tron 	int	 size;
    286      1.1      tron 
    287      1.1      tron 	do {
    288      1.1      tron 		if (gzgets(in, buffer, sizeof(buffer)) == NULL)
    289      1.1      tron 			return NULL;
    290      1.1      tron 	}
    291      1.1      tron 	while (buffer[0] == '\n');
    292      1.1      tron 
    293      1.1      tron 	section = NULL;
    294      1.1      tron 	if ((ptr = strchr(buffer, '(')) != NULL) {
    295      1.1      tron 		if ((last = strchr(ptr + 1, ')')) !=NULL) {
    296      1.1      tron 			int 	length;
    297      1.1      tron 
    298      1.1      tron 			length = last - ptr + 1;
    299      1.1      tron 			if ((section = malloc(length + 5)) == NULL)
    300      1.1      tron 				return NULL;
    301      1.1      tron 
    302      1.1      tron 			*section = ' ';
    303      1.1      tron 			(void) memcpy(section + 1, ptr, length);
    304      1.1      tron 			(void) strcpy(section + 1 + length, " - ");
    305      1.1      tron 		}
    306      1.1      tron 	}
    307      1.1      tron 
    308      1.1      tron 	for (;;) {
    309      1.1      tron 		if (gzgets(in, buffer, sizeof(buffer)) == NULL) {
    310      1.1      tron 			free(section);
    311      1.1      tron 			return NULL;
    312      1.1      tron 		}
    313      1.1      tron 		if (strncmp(buffer, "N\10NA\10AM\10ME\10E", 12) == 0)
    314      1.1      tron 			break;
    315      1.1      tron 	}
    316      1.1      tron 
    317      1.1      tron 	ptr = last = buffer;
    318      1.1      tron 	size = sizeof(buffer) - 1;
    319      1.1      tron 	while ((size > 0) && (gzgets(in, ptr, size) != NULL)) {
    320      1.1      tron 		int	 length;
    321      1.1      tron 
    322      1.1      tron 		catpreprocess(ptr);
    323      1.1      tron 
    324      1.1      tron 		length = strlen(ptr);
    325      1.1      tron 		if (length == 0) {
    326      1.1      tron 			*last = '\0';
    327      1.1      tron 
    328      1.1      tron 			ptr = replacestring(buffer, " - ", section);
    329      1.1      tron 			free(section);
    330      1.1      tron 			return ptr;
    331      1.1      tron 		}
    332      1.1      tron 		if ((length > 1) && (ptr[length - 1] == '-') &&
    333      1.1      tron 		    isalpha(ptr[length - 2]))
    334      1.1      tron 			last = &ptr[--length];
    335      1.1      tron 		else {
    336      1.1      tron 			last = &ptr[length++];
    337      1.1      tron 			*last = ' ';
    338      1.1      tron 		}
    339      1.1      tron 
    340      1.1      tron 		ptr += length;
    341      1.1      tron 		size -= length;
    342      1.1      tron 	}
    343      1.1      tron 
    344      1.1      tron 	free(section);
    345      1.1      tron 
    346      1.1      tron 	return NULL;
    347      1.1      tron }
    348      1.1      tron 
    349      1.1      tron int
    350      1.1      tron manpreprocess(char *line)
    351      1.1      tron {
    352      1.1      tron 	char	*from, *to;
    353      1.1      tron 
    354      1.1      tron 	to = from = line;
    355      1.1      tron 	while (isspace(*from)) from++;
    356      1.1      tron 	if (strncmp(from, ".\\\"", 3) == 0)
    357      1.1      tron 		return 1;
    358      1.1      tron 
    359      1.1      tron 	while (*from != '\0')
    360      1.1      tron 		if (isspace(*from)) {
    361      1.1      tron 			while (isspace(*++from));
    362      1.1      tron 			if ((*from != '\0') && (*from != ','))
    363      1.1      tron 				*to++ = ' ';
    364      1.1      tron 		}
    365      1.1      tron 		else if (*from == '\\')
    366      1.1      tron 			switch (*++from) {
    367      1.1      tron 			case '\0':
    368      1.1      tron 			case '-':
    369      1.1      tron 				break;
    370      1.1      tron 			default:
    371      1.1      tron 				from++;
    372      1.1      tron 			}
    373      1.1      tron 		else
    374      1.1      tron 			if (*from == '"')
    375      1.1      tron 				from++;
    376      1.1      tron 			else
    377      1.1      tron 				*to++ = *from++;
    378      1.1      tron 
    379      1.1      tron 	*to = '\0';
    380      1.1      tron 
    381      1.1      tron 	if (strncasecmp(line, ".Xr", 3) == 0) {
    382      1.1      tron 		char	*sect;
    383      1.1      tron 
    384      1.1      tron 		from = line + 3;
    385      1.1      tron 		if (isspace(*from))
    386      1.1      tron 			from++;
    387      1.1      tron 
    388      1.1      tron 		if ((sect = strchr(from, ' ')) != NULL) {
    389      1.1      tron 			int	 length;
    390      1.1      tron 
    391      1.1      tron 			*sect++ = '\0';
    392      1.1      tron 			length = strlen(from);
    393      1.1      tron 			(void) memmove(line, from, length);
    394      1.1      tron 			line[length++] = '(';
    395      1.1      tron 			to = &line[length];
    396      1.1      tron 			length = strlen(sect);
    397      1.1      tron 			(void) memmove(to, sect, length);
    398      1.1      tron 			(void) strcpy(&to[length], ")");
    399      1.1      tron 		}
    400      1.1      tron 	}
    401      1.1      tron 
    402      1.1      tron 	return 0;
    403      1.1      tron }
    404      1.1      tron 
    405      1.1      tron char *
    406      1.1      tron parsemanpage(gzFile *in, int defaultsection)
    407      1.1      tron {
    408      1.1      tron 	char	*section, buffer[8192], *ptr;
    409      1.1      tron 
    410      1.1      tron 	section = NULL;
    411      1.1      tron 	do {
    412      1.1      tron 		if (gzgets(in, buffer, sizeof(buffer) - 1) == NULL) {
    413      1.1      tron 			free(section);
    414      1.1      tron 			return NULL;
    415      1.1      tron 		}
    416      1.1      tron 		if (manpreprocess(buffer))
    417      1.1      tron 			continue;
    418      1.1      tron 		if (strncasecmp(buffer, ".Dt", 3) == 0) {
    419      1.1      tron 			char	*end;
    420      1.1      tron 
    421      1.1      tron 			ptr = &buffer[3];
    422      1.1      tron 			if (isspace(*ptr))
    423      1.1      tron 				ptr++;
    424      1.1      tron 			if ((ptr = strchr(ptr, ' ')) == NULL)
    425      1.1      tron 				continue;
    426      1.1      tron 
    427      1.1      tron 			if ((end = strchr(++ptr, ' ')) != NULL)
    428      1.1      tron 				*end = '\0';
    429      1.1      tron 
    430      1.1      tron 			free(section);
    431      1.1      tron 			if ((section = malloc(strlen(ptr) + 7)) != NULL) {
    432      1.1      tron 				section[0] = ' ';
    433      1.1      tron 				section[1] = '(';
    434      1.1      tron 				(void) strcpy(&section[2], ptr);
    435      1.1      tron 				(void) strcat(&section[2], ") - ");
    436      1.1      tron 			}
    437      1.1      tron 		}
    438      1.1      tron 	} while ((strncasecmp(buffer, ".Sh NAME", 8) != 0));
    439      1.1      tron 
    440      1.1      tron 	do {
    441      1.1      tron 		if (gzgets(in, buffer, sizeof(buffer) - 1) == NULL) {
    442      1.1      tron 			free(section);
    443      1.1      tron 			return NULL;
    444      1.1      tron 		}
    445      1.1      tron 	} while (manpreprocess(buffer));
    446      1.1      tron 
    447      1.1      tron 	if (strncasecmp(buffer, ".Nm", 3) == 0) {
    448      1.1      tron 		int	length, offset;
    449      1.1      tron 
    450      1.1      tron 		ptr = &buffer[3];
    451      1.1      tron 		if (isspace(*ptr))
    452      1.1      tron 			ptr++;
    453      1.1      tron 
    454      1.1      tron 		length = strlen(ptr);
    455      1.1      tron 		if ((length > 1) && (ptr[length - 1] == ',') &&
    456      1.1      tron 		    isspace(ptr[length - 2])) {
    457      1.1      tron 			ptr[--length] = '\0';
    458      1.1      tron 			ptr[length - 1] = ',';
    459      1.1      tron 		}
    460      1.1      tron 		(void) memmove(buffer, ptr, length + 1);
    461      1.1      tron 
    462      1.1      tron 		offset = length + 3;
    463      1.1      tron 		ptr = &buffer[offset];
    464      1.1      tron 		for (;;) {
    465      1.1      tron 			int	 more;
    466      1.1      tron 
    467      1.1      tron 			if ((sizeof(buffer) == offset) ||
    468      1.1      tron 		            (gzgets(in, ptr, sizeof(buffer) - offset)
    469      1.1      tron 			       == NULL)) {
    470      1.1      tron 				free(section);
    471      1.1      tron 				return NULL;
    472      1.1      tron 			}
    473      1.1      tron 			if (manpreprocess(ptr))
    474      1.1      tron 				continue;
    475      1.1      tron 
    476      1.1      tron 			if (strncasecmp(ptr, ".Nm", 3) != 0) break;
    477      1.1      tron 
    478      1.1      tron 			ptr += 3;
    479      1.1      tron 			if (isspace(*ptr))
    480      1.1      tron 				ptr++;
    481      1.1      tron 
    482      1.1      tron 			buffer[length++] = ' ';
    483      1.1      tron 			more = strlen(ptr);
    484      1.1      tron 			if ((more > 1) && (ptr[more - 1] == ',') &&
    485      1.1      tron 			    isspace(ptr[more - 2])) {
    486      1.1      tron 				ptr[--more] = '\0';
    487      1.1      tron 				ptr[more - 1] = ',';
    488      1.1      tron 			}
    489      1.1      tron 
    490      1.1      tron 			(void) memmove(&buffer[length], ptr, more + 1);
    491      1.1      tron 			length += more;
    492      1.1      tron 			offset = length + 3;
    493      1.1      tron 
    494      1.1      tron 			ptr = &buffer[offset];
    495      1.1      tron 		}
    496      1.1      tron 
    497      1.1      tron 		if (strncasecmp(ptr, ".Nd", 3) == 0) {
    498      1.1      tron 			(void) strcpy(&buffer[length], " -");
    499      1.1      tron 
    500      1.1      tron 			while (strncasecmp(ptr, ".Sh", 3) != 0) {
    501      1.1      tron 				int	 more;
    502      1.1      tron 
    503      1.1      tron 				if (*ptr == '.') {
    504      1.1      tron 					char	*space;
    505      1.1      tron 
    506      1.1      tron 					if ((space = strchr(ptr, ' ')) == NULL)
    507      1.1      tron 						ptr = "";
    508      1.1      tron 					else {
    509      1.1      tron 						space++;
    510      1.1      tron 						(void) memmove(ptr, space,
    511      1.1      tron 							   strlen(space) + 1);
    512      1.1      tron 					}
    513      1.1      tron 				}
    514      1.1      tron 
    515      1.1      tron 				if (*ptr != '\0') {
    516      1.1      tron 					buffer[offset - 1] = ' ';
    517      1.1      tron 					more = strlen(ptr) + 1;
    518      1.1      tron 					offset += more;
    519      1.1      tron 				}
    520      1.1      tron 				ptr = &buffer[offset];
    521      1.1      tron 				if ((sizeof(buffer) == offset) ||
    522      1.1      tron 			            (gzgets(in, ptr, sizeof(buffer) - offset)
    523      1.1      tron 					== NULL)) {
    524      1.1      tron 					free(section);
    525      1.1      tron 					return NULL;
    526      1.1      tron 				}
    527      1.1      tron 				if (manpreprocess(ptr))
    528      1.1      tron 					*ptr = '\0';
    529      1.1      tron 			}
    530      1.1      tron 		}
    531      1.1      tron 	}
    532      1.1      tron 	else {
    533      1.1      tron 		int	 offset;
    534      1.1      tron 
    535      1.1      tron 		if (*buffer == '.') {
    536      1.1      tron 			char	*space;
    537      1.1      tron 
    538      1.1      tron 			if ((space = strchr(buffer, ' ')) == NULL) {
    539      1.1      tron 				free(section);
    540      1.1      tron 				return NULL;
    541      1.1      tron 			}
    542      1.1      tron 			space++;
    543      1.1      tron 			(void) memmove(buffer, space, strlen(space));
    544      1.1      tron 		}
    545      1.1      tron 
    546      1.1      tron 		offset = strlen(buffer) + 1;
    547      1.1      tron 		for (;;) {
    548      1.1      tron 			int	 more;
    549      1.1      tron 
    550      1.1      tron 			ptr = &buffer[offset];
    551      1.1      tron 			if ((sizeof(buffer) == offset) ||
    552      1.1      tron 		            (gzgets(in, ptr, sizeof(buffer) - offset)
    553      1.1      tron 				== NULL)) {
    554      1.1      tron 				free(section);
    555      1.1      tron 				return NULL;
    556      1.1      tron 			}
    557      1.1      tron 			if (manpreprocess(ptr) || (*ptr == '\0'))
    558      1.1      tron 				continue;
    559      1.1      tron 
    560      1.1      tron 			if (strncasecmp(ptr, ".Sh", 3) == 0)
    561      1.1      tron 				break;
    562      1.1      tron 
    563      1.1      tron 			if (*ptr == '.') {
    564      1.1      tron 				char	*space;
    565      1.1      tron 
    566      1.1      tron 				if ((space = strchr(ptr, ' ')) == NULL)
    567      1.1      tron 					continue;
    568      1.1      tron 				space++;
    569      1.1      tron 				(void) memmove(ptr, space, strlen(space));
    570      1.1      tron 			}
    571      1.1      tron 
    572      1.1      tron 			buffer[offset - 1] = ' ';
    573      1.1      tron 			more = strlen(ptr);
    574      1.1      tron 			if ((more > 1) && (ptr[more - 1] == ',') &&
    575      1.1      tron 			    isspace(ptr[more - 2])) {
    576      1.1      tron 				ptr[more - 1] = '\0';
    577      1.1      tron 				ptr[more - 2] = ',';
    578      1.1      tron 			}
    579      1.1      tron 			else more++;
    580      1.1      tron 			offset += more;
    581      1.1      tron 		}
    582      1.1      tron 	}
    583      1.1      tron 
    584      1.1      tron 	if (section == NULL) {
    585      1.1      tron 		char sectionbuffer[24];
    586      1.1      tron 
    587      1.4      tron 		(void) sprintf(sectionbuffer, " (%c) - ",
    588      1.4      tron 			sectionext[defaultsection]);
    589      1.1      tron 		ptr = replacestring(buffer, " - ", sectionbuffer);
    590      1.1      tron 	}
    591      1.1      tron 	else {
    592      1.1      tron 		ptr = replacestring(buffer, " - ", section);
    593      1.1      tron 		free(section);
    594      1.1      tron 	}
    595      1.1      tron 	return ptr;
    596      1.1      tron }
    597      1.1      tron 
    598      1.1      tron char *
    599      1.1      tron getwhatisdata(char *name)
    600      1.1      tron {
    601      1.1      tron 	gzFile	*in;
    602      1.1      tron 	char	*data;
    603      1.1      tron 	int	 section;
    604      1.1      tron 
    605      1.1      tron 	if ((in = gzopen(name, "r")) == NULL) {
    606      1.1      tron 		errx(EXIT_FAILURE, "%s: %s",
    607      1.1      tron 		    name,
    608      1.1      tron 		    strerror((errno == 0) ? ENOMEM : errno));
    609      1.1      tron 		/* NOTREACHED */
    610      1.1      tron 	}
    611      1.1      tron 
    612      1.1      tron 	section = manpagesection(name);
    613      1.1      tron 	data = (section == 0) ? parsecatpage(in) : parsemanpage(in, section);
    614      1.1      tron 
    615      1.1      tron 	(void) gzclose(in);
    616      1.1      tron 	return data;
    617      1.1      tron }
    618      1.1      tron 
    619      1.1      tron void
    620      1.1      tron processmanpages(manpage **source, whatis **dest)
    621      1.1      tron {
    622      1.1      tron 	manpage	*mp;
    623      1.1      tron 
    624      1.1      tron 	mp = *source;
    625      1.1      tron 	*source = NULL;
    626      1.1      tron 
    627      1.1      tron 	while (mp != NULL) {
    628      1.1      tron 		manpage *obsolete;
    629      1.1      tron 		char *data;
    630      1.1      tron 
    631      1.1      tron 		if (mp->mp_left != NULL)
    632      1.1      tron 			processmanpages(&mp->mp_left,dest);
    633      1.1      tron 
    634      1.1      tron 		if ((data = getwhatisdata(mp->mp_name)) != NULL) {
    635      1.1      tron 			if (!addwhatis(dest,data))
    636      1.1      tron 				err(EXIT_FAILURE, NULL);
    637      1.1      tron 		}
    638      1.1      tron 
    639      1.1      tron 		obsolete = mp;
    640      1.1      tron 		mp = mp->mp_right;
    641      1.1      tron 		free(obsolete);
    642      1.1      tron 	}
    643      1.1      tron }
    644      1.1      tron 
    645      1.1      tron int
    646      1.1      tron dumpwhatis (FILE *out, whatis *tree)
    647      1.1      tron {
    648      1.1      tron 	while (tree != NULL) {
    649      1.1      tron 		if (tree->wi_left)
    650      1.1      tron 			if (!dumpwhatis(out, tree->wi_left)) return 0;
    651      1.1      tron 
    652      1.1      tron 		if ((fputs(tree->wi_data, out) == EOF) ||
    653      1.1      tron 		    (fputc('\n', out) == EOF))
    654      1.1      tron 			return 0;
    655      1.1      tron 
    656      1.1      tron 		tree = tree->wi_right;
    657      1.1      tron 	}
    658      1.1      tron 
    659      1.1      tron 	return 1;
    660      1.1      tron }
    661