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