xref: /titanic_41/usr/src/cmd/man/makewhatis.c (revision 32a712da90cea6ff9a05f51e7844944ccfa28d5e)
1*32a712daSGarrett D'Amore /*
2*32a712daSGarrett D'Amore  * Copyright (c) 2002 John Rochester
3*32a712daSGarrett D'Amore  * All rights reserved.
4*32a712daSGarrett D'Amore  *
5*32a712daSGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
6*32a712daSGarrett D'Amore  * modification, are permitted provided that the following conditions
7*32a712daSGarrett D'Amore  * are met:
8*32a712daSGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
9*32a712daSGarrett D'Amore  *    notice, this list of conditions and the following disclaimer,
10*32a712daSGarrett D'Amore  *    in this position and unchanged.
11*32a712daSGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
12*32a712daSGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
13*32a712daSGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
14*32a712daSGarrett D'Amore  * 3. The name of the author may not be used to endorse or promote products
15*32a712daSGarrett D'Amore  *    derived from this software without specific prior written permission
16*32a712daSGarrett D'Amore  *
17*32a712daSGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*32a712daSGarrett D'Amore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*32a712daSGarrett D'Amore  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*32a712daSGarrett D'Amore  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*32a712daSGarrett D'Amore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*32a712daSGarrett D'Amore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*32a712daSGarrett D'Amore  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*32a712daSGarrett D'Amore  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*32a712daSGarrett D'Amore  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*32a712daSGarrett D'Amore  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*32a712daSGarrett D'Amore  */
28*32a712daSGarrett D'Amore 
29*32a712daSGarrett D'Amore /*
30*32a712daSGarrett D'Amore  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
31*32a712daSGarrett D'Amore  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
32*32a712daSGarrett D'Amore  */
33*32a712daSGarrett D'Amore 
34*32a712daSGarrett D'Amore #include <sys/types.h>
35*32a712daSGarrett D'Amore #include <sys/stat.h>
36*32a712daSGarrett D'Amore #include <sys/param.h>
37*32a712daSGarrett D'Amore 
38*32a712daSGarrett D'Amore #include <ctype.h>
39*32a712daSGarrett D'Amore #include <dirent.h>
40*32a712daSGarrett D'Amore #include <err.h>
41*32a712daSGarrett D'Amore #include <signal.h>
42*32a712daSGarrett D'Amore #include <stddef.h>
43*32a712daSGarrett D'Amore #include <stdio.h>
44*32a712daSGarrett D'Amore #include <stdlib.h>
45*32a712daSGarrett D'Amore #include <string.h>
46*32a712daSGarrett D'Amore #include <unistd.h>
47*32a712daSGarrett D'Amore 
48*32a712daSGarrett D'Amore #include "man.h"
49*32a712daSGarrett D'Amore #include "stringlist.h"
50*32a712daSGarrett D'Amore 
51*32a712daSGarrett D'Amore 
52*32a712daSGarrett D'Amore /* Information collected about each man page in a section */
53*32a712daSGarrett D'Amore struct page_info {
54*32a712daSGarrett D'Amore 	char	*filename;
55*32a712daSGarrett D'Amore 	char	*name;
56*32a712daSGarrett D'Amore 	char	*suffix;
57*32a712daSGarrett D'Amore 	ino_t	inode;
58*32a712daSGarrett D'Amore };
59*32a712daSGarrett D'Amore 
60*32a712daSGarrett D'Amore /* An expanding string */
61*32a712daSGarrett D'Amore struct sbuf {
62*32a712daSGarrett D'Amore 	char	*content;	/* the start of the buffer */
63*32a712daSGarrett D'Amore 	char	*end;		/* just past the end of the content */
64*32a712daSGarrett D'Amore 	char	*last;		/* the last allocated character */
65*32a712daSGarrett D'Amore };
66*32a712daSGarrett D'Amore 
67*32a712daSGarrett D'Amore /* Remove the last amount characters from the sbuf */
68*32a712daSGarrett D'Amore #define	sbuf_retract(sbuf, amount) ((sbuf)->end -= (amount))
69*32a712daSGarrett D'Amore /* Return the length of the sbuf content */
70*32a712daSGarrett D'Amore #define	sbuf_length(sbuf) ((sbuf)->end - (sbuf)->content)
71*32a712daSGarrett D'Amore 
72*32a712daSGarrett D'Amore typedef char *edited_copy(char *from, char *to, int length);
73*32a712daSGarrett D'Amore 
74*32a712daSGarrett D'Amore /*
75*32a712daSGarrett D'Amore  * While the whatis line is being formed, it is stored in whatis_proto.
76*32a712daSGarrett D'Amore  * When finished, it is reformatted into whatis_final and then appended
77*32a712daSGarrett D'Amore  * to whatis_lines.
78*32a712daSGarrett D'Amore  */
79*32a712daSGarrett D'Amore static struct sbuf	*whatis_proto;
80*32a712daSGarrett D'Amore static struct sbuf	*whatis_final;
81*32a712daSGarrett D'Amore static stringlist	*whatis_lines;	/* collected output lines */
82*32a712daSGarrett D'Amore 
83*32a712daSGarrett D'Amore static char tempfile[MAXPATHLEN];	/* path of temporary file, if any */
84*32a712daSGarrett D'Amore 
85*32a712daSGarrett D'Amore #define	MDOC_COMMANDS	"ArDvErEvFlLiNmPa"
86*32a712daSGarrett D'Amore 
87*32a712daSGarrett D'Amore 
88*32a712daSGarrett D'Amore /* Free a struct page_info and its content */
89*32a712daSGarrett D'Amore static void
free_page_info(struct page_info * info)90*32a712daSGarrett D'Amore free_page_info(struct page_info *info)
91*32a712daSGarrett D'Amore {
92*32a712daSGarrett D'Amore 
93*32a712daSGarrett D'Amore 	free(info->filename);
94*32a712daSGarrett D'Amore 	free(info->name);
95*32a712daSGarrett D'Amore 	free(info->suffix);
96*32a712daSGarrett D'Amore 	free(info);
97*32a712daSGarrett D'Amore }
98*32a712daSGarrett D'Amore 
99*32a712daSGarrett D'Amore /*
100*32a712daSGarrett D'Amore  * Allocate and fill in a new struct page_info given the
101*32a712daSGarrett D'Amore  * name of the man section directory and the dirent of the file.
102*32a712daSGarrett D'Amore  * If the file is not a man page, return NULL.
103*32a712daSGarrett D'Amore  */
104*32a712daSGarrett D'Amore static struct page_info *
new_page_info(char * dir,struct dirent * dirent)105*32a712daSGarrett D'Amore new_page_info(char *dir, struct dirent *dirent)
106*32a712daSGarrett D'Amore {
107*32a712daSGarrett D'Amore 	struct page_info *info;
108*32a712daSGarrett D'Amore 	int		basename_length;
109*32a712daSGarrett D'Amore 	char		*suffix;
110*32a712daSGarrett D'Amore 	struct stat	st;
111*32a712daSGarrett D'Amore 
112*32a712daSGarrett D'Amore 	if ((info = malloc(sizeof (struct page_info))) == NULL)
113*32a712daSGarrett D'Amore 		err(1, "malloc");
114*32a712daSGarrett D'Amore 	basename_length = strlen(dirent->d_name);
115*32a712daSGarrett D'Amore 	suffix = &dirent->d_name[basename_length];
116*32a712daSGarrett D'Amore 	if (asprintf(&info->filename, "%s/%s", dir, dirent->d_name) == -1)
117*32a712daSGarrett D'Amore 		err(1, "asprintf");
118*32a712daSGarrett D'Amore 	for (;;) {
119*32a712daSGarrett D'Amore 		if (--suffix == dirent->d_name || !isalnum(*suffix)) {
120*32a712daSGarrett D'Amore 			if (*suffix == '.')
121*32a712daSGarrett D'Amore 				break;
122*32a712daSGarrett D'Amore 			free(info->filename);
123*32a712daSGarrett D'Amore 			free(info);
124*32a712daSGarrett D'Amore 			return (NULL);
125*32a712daSGarrett D'Amore 		}
126*32a712daSGarrett D'Amore 	}
127*32a712daSGarrett D'Amore 	*suffix++ = '\0';
128*32a712daSGarrett D'Amore 	info->name = strdup(dirent->d_name);
129*32a712daSGarrett D'Amore 	info->suffix = strdup(suffix);
130*32a712daSGarrett D'Amore 	if (stat(info->filename, &st) < 0) {
131*32a712daSGarrett D'Amore 		warn("%s", info->filename);
132*32a712daSGarrett D'Amore 		free_page_info(info);
133*32a712daSGarrett D'Amore 		return (NULL);
134*32a712daSGarrett D'Amore 	}
135*32a712daSGarrett D'Amore 	if (!S_ISREG(st.st_mode)) {
136*32a712daSGarrett D'Amore 		free_page_info(info);
137*32a712daSGarrett D'Amore 		return (NULL);
138*32a712daSGarrett D'Amore 	}
139*32a712daSGarrett D'Amore 	info->inode = st.st_ino;
140*32a712daSGarrett D'Amore 	return (info);
141*32a712daSGarrett D'Amore }
142*32a712daSGarrett D'Amore 
143*32a712daSGarrett D'Amore /*
144*32a712daSGarrett D'Amore  * Reset sbuf length to 0.
145*32a712daSGarrett D'Amore  */
146*32a712daSGarrett D'Amore static void
sbuf_clear(struct sbuf * sbuf)147*32a712daSGarrett D'Amore sbuf_clear(struct sbuf *sbuf)
148*32a712daSGarrett D'Amore {
149*32a712daSGarrett D'Amore 
150*32a712daSGarrett D'Amore 	sbuf->end = sbuf->content;
151*32a712daSGarrett D'Amore }
152*32a712daSGarrett D'Amore 
153*32a712daSGarrett D'Amore /*
154*32a712daSGarrett D'Amore  * Allocate a new sbuf.
155*32a712daSGarrett D'Amore  */
156*32a712daSGarrett D'Amore static struct sbuf *
new_sbuf(void)157*32a712daSGarrett D'Amore new_sbuf(void)
158*32a712daSGarrett D'Amore {
159*32a712daSGarrett D'Amore 	struct sbuf	*sbuf;
160*32a712daSGarrett D'Amore 
161*32a712daSGarrett D'Amore 	if ((sbuf = malloc(sizeof (struct sbuf))) == NULL)
162*32a712daSGarrett D'Amore 		err(1, "malloc");
163*32a712daSGarrett D'Amore 	if ((sbuf->content = (char *)malloc(LINE_ALLOC)) == NULL)
164*32a712daSGarrett D'Amore 		err(1, "malloc");
165*32a712daSGarrett D'Amore 	sbuf->last = sbuf->content + LINE_ALLOC - 1;
166*32a712daSGarrett D'Amore 	sbuf_clear(sbuf);
167*32a712daSGarrett D'Amore 
168*32a712daSGarrett D'Amore 	return (sbuf);
169*32a712daSGarrett D'Amore }
170*32a712daSGarrett D'Amore 
171*32a712daSGarrett D'Amore /*
172*32a712daSGarrett D'Amore  * Ensure that there is enough room in the sbuf
173*32a712daSGarrett D'Amore  * for nchars more characters.
174*32a712daSGarrett D'Amore  */
175*32a712daSGarrett D'Amore static void
sbuf_need(struct sbuf * sbuf,int nchars)176*32a712daSGarrett D'Amore sbuf_need(struct sbuf *sbuf, int nchars)
177*32a712daSGarrett D'Amore {
178*32a712daSGarrett D'Amore 	char *new_content;
179*32a712daSGarrett D'Amore 	size_t size, cntsize;
180*32a712daSGarrett D'Amore 	size_t grow = 128;
181*32a712daSGarrett D'Amore 
182*32a712daSGarrett D'Amore 	while (grow < nchars) {
183*32a712daSGarrett D'Amore 		grow += 128;	/* we grow in chunks of 128 bytes */
184*32a712daSGarrett D'Amore 	}
185*32a712daSGarrett D'Amore 
186*32a712daSGarrett D'Amore 	/* Grow if the buffer isn't big enough */
187*32a712daSGarrett D'Amore 	if (sbuf->end + nchars > sbuf->last) {
188*32a712daSGarrett D'Amore 		size = sbuf->last + 1 - sbuf->content;
189*32a712daSGarrett D'Amore 		size += grow;
190*32a712daSGarrett D'Amore 		cntsize = sbuf->end - sbuf->content;
191*32a712daSGarrett D'Amore 
192*32a712daSGarrett D'Amore 		if ((new_content = realloc(sbuf->content, size)) == NULL) {
193*32a712daSGarrett D'Amore 			perror("realloc");
194*32a712daSGarrett D'Amore 			if (tempfile[0] != '\0')
195*32a712daSGarrett D'Amore 				(void) unlink(tempfile);
196*32a712daSGarrett D'Amore 			exit(1);
197*32a712daSGarrett D'Amore 		}
198*32a712daSGarrett D'Amore 		sbuf->content = new_content;
199*32a712daSGarrett D'Amore 		sbuf->end = new_content + cntsize;
200*32a712daSGarrett D'Amore 		sbuf->last = new_content + size - 1;
201*32a712daSGarrett D'Amore 	}
202*32a712daSGarrett D'Amore }
203*32a712daSGarrett D'Amore 
204*32a712daSGarrett D'Amore /*
205*32a712daSGarrett D'Amore  * Append a string of a given length to the sbuf.
206*32a712daSGarrett D'Amore  */
207*32a712daSGarrett D'Amore static void
sbuf_append(struct sbuf * sbuf,const char * text,int length)208*32a712daSGarrett D'Amore sbuf_append(struct sbuf *sbuf, const char *text, int length)
209*32a712daSGarrett D'Amore {
210*32a712daSGarrett D'Amore 	if (length > 0) {
211*32a712daSGarrett D'Amore 		sbuf_need(sbuf, length);
212*32a712daSGarrett D'Amore 		(void) memcpy(sbuf->end, text, length);
213*32a712daSGarrett D'Amore 		sbuf->end += length;
214*32a712daSGarrett D'Amore 	}
215*32a712daSGarrett D'Amore }
216*32a712daSGarrett D'Amore 
217*32a712daSGarrett D'Amore /*
218*32a712daSGarrett D'Amore  * Append a null-terminated string to the sbuf.
219*32a712daSGarrett D'Amore  */
220*32a712daSGarrett D'Amore static void
sbuf_append_str(struct sbuf * sbuf,char * text)221*32a712daSGarrett D'Amore sbuf_append_str(struct sbuf *sbuf, char *text)
222*32a712daSGarrett D'Amore {
223*32a712daSGarrett D'Amore 
224*32a712daSGarrett D'Amore 	sbuf_append(sbuf, text, strlen(text));
225*32a712daSGarrett D'Amore }
226*32a712daSGarrett D'Amore 
227*32a712daSGarrett D'Amore /*
228*32a712daSGarrett D'Amore  * Append an edited null-terminated string to the sbuf.
229*32a712daSGarrett D'Amore  */
230*32a712daSGarrett D'Amore static void
sbuf_append_edited(struct sbuf * sbuf,char * text,edited_copy copy)231*32a712daSGarrett D'Amore sbuf_append_edited(struct sbuf *sbuf, char *text, edited_copy copy)
232*32a712daSGarrett D'Amore {
233*32a712daSGarrett D'Amore 	int	length;
234*32a712daSGarrett D'Amore 
235*32a712daSGarrett D'Amore 	if ((length = strlen(text)) > 0) {
236*32a712daSGarrett D'Amore 		sbuf_need(sbuf, length);
237*32a712daSGarrett D'Amore 		sbuf->end = copy(text, sbuf->end, length);
238*32a712daSGarrett D'Amore 	}
239*32a712daSGarrett D'Amore }
240*32a712daSGarrett D'Amore 
241*32a712daSGarrett D'Amore /*
242*32a712daSGarrett D'Amore  * Strip any of a set of chars from the end of the sbuf.
243*32a712daSGarrett D'Amore  */
244*32a712daSGarrett D'Amore static void
sbuf_strip(struct sbuf * sbuf,const char * set)245*32a712daSGarrett D'Amore sbuf_strip(struct sbuf *sbuf, const char *set)
246*32a712daSGarrett D'Amore {
247*32a712daSGarrett D'Amore 
248*32a712daSGarrett D'Amore 	while (sbuf->end > sbuf->content && strchr(set, sbuf->end[-1]) != NULL)
249*32a712daSGarrett D'Amore 		sbuf->end--;
250*32a712daSGarrett D'Amore }
251*32a712daSGarrett D'Amore 
252*32a712daSGarrett D'Amore /*
253*32a712daSGarrett D'Amore  * Return the null-terminated string built by the sbuf.
254*32a712daSGarrett D'Amore  */
255*32a712daSGarrett D'Amore static char *
sbuf_content(struct sbuf * sbuf)256*32a712daSGarrett D'Amore sbuf_content(struct sbuf *sbuf)
257*32a712daSGarrett D'Amore {
258*32a712daSGarrett D'Amore 
259*32a712daSGarrett D'Amore 	*sbuf->end = '\0';
260*32a712daSGarrett D'Amore 	return (sbuf->content);
261*32a712daSGarrett D'Amore }
262*32a712daSGarrett D'Amore 
263*32a712daSGarrett D'Amore /*
264*32a712daSGarrett D'Amore  * Return true if no man page exists in the directory with
265*32a712daSGarrett D'Amore  * any of the names in the stringlist.
266*32a712daSGarrett D'Amore  */
267*32a712daSGarrett D'Amore static int
no_page_exists(char * dir,stringlist * names,char * suffix)268*32a712daSGarrett D'Amore no_page_exists(char *dir, stringlist *names, char *suffix)
269*32a712daSGarrett D'Amore {
270*32a712daSGarrett D'Amore 	char	path[MAXPATHLEN];
271*32a712daSGarrett D'Amore 	char	*suffixes[] = { "", ".gz", ".bz2", NULL };
272*32a712daSGarrett D'Amore 	size_t	i;
273*32a712daSGarrett D'Amore 	int	j;
274*32a712daSGarrett D'Amore 
275*32a712daSGarrett D'Amore 	for (i = 0; i < names->sl_cur; i++) {
276*32a712daSGarrett D'Amore 		for (j = 0; suffixes[j] != NULL; j++) {
277*32a712daSGarrett D'Amore 			(void) snprintf(path, MAXPATHLEN, "%s/%s.%s%s",
278*32a712daSGarrett D'Amore 			    dir, names->sl_str[i], suffix, suffixes[j]);
279*32a712daSGarrett D'Amore 			if (access(path, F_OK) == 0) {
280*32a712daSGarrett D'Amore 				return (0);
281*32a712daSGarrett D'Amore 			}
282*32a712daSGarrett D'Amore 		}
283*32a712daSGarrett D'Amore 	}
284*32a712daSGarrett D'Amore 	return (1);
285*32a712daSGarrett D'Amore }
286*32a712daSGarrett D'Amore 
287*32a712daSGarrett D'Amore /* ARGSUSED sig */
288*32a712daSGarrett D'Amore static void
trap_signal(int sig)289*32a712daSGarrett D'Amore trap_signal(int sig)
290*32a712daSGarrett D'Amore {
291*32a712daSGarrett D'Amore 
292*32a712daSGarrett D'Amore 	if (tempfile[0] != '\0')
293*32a712daSGarrett D'Amore 		(void) unlink(tempfile);
294*32a712daSGarrett D'Amore 
295*32a712daSGarrett D'Amore 	exit(1);
296*32a712daSGarrett D'Amore }
297*32a712daSGarrett D'Amore 
298*32a712daSGarrett D'Amore /*
299*32a712daSGarrett D'Amore  * Attempt to open an output file.
300*32a712daSGarrett D'Amore  * Return NULL if unsuccessful.
301*32a712daSGarrett D'Amore  */
302*32a712daSGarrett D'Amore static FILE *
open_output(char * name)303*32a712daSGarrett D'Amore open_output(char *name)
304*32a712daSGarrett D'Amore {
305*32a712daSGarrett D'Amore 	FILE	*output;
306*32a712daSGarrett D'Amore 
307*32a712daSGarrett D'Amore 	whatis_lines = sl_init();
308*32a712daSGarrett D'Amore 	(void) snprintf(tempfile, MAXPATHLEN, "%s.tmp", name);
309*32a712daSGarrett D'Amore 	name = tempfile;
310*32a712daSGarrett D'Amore 	if ((output = fopen(name, "w")) == NULL) {
311*32a712daSGarrett D'Amore 		warn("%s", name);
312*32a712daSGarrett D'Amore 		return (NULL);
313*32a712daSGarrett D'Amore 	}
314*32a712daSGarrett D'Amore 	return (output);
315*32a712daSGarrett D'Amore }
316*32a712daSGarrett D'Amore 
317*32a712daSGarrett D'Amore static int
linesort(const void * a,const void * b)318*32a712daSGarrett D'Amore linesort(const void *a, const void *b)
319*32a712daSGarrett D'Amore {
320*32a712daSGarrett D'Amore 
321*32a712daSGarrett D'Amore 	return (strcmp((*(const char * const *)a), (*(const char * const *)b)));
322*32a712daSGarrett D'Amore }
323*32a712daSGarrett D'Amore 
324*32a712daSGarrett D'Amore /*
325*32a712daSGarrett D'Amore  * Write the unique sorted lines to the output file.
326*32a712daSGarrett D'Amore  */
327*32a712daSGarrett D'Amore static void
finish_output(FILE * output,char * name)328*32a712daSGarrett D'Amore finish_output(FILE *output, char *name)
329*32a712daSGarrett D'Amore {
330*32a712daSGarrett D'Amore 	size_t	i;
331*32a712daSGarrett D'Amore 	char	*prev = NULL;
332*32a712daSGarrett D'Amore 
333*32a712daSGarrett D'Amore 	qsort(whatis_lines->sl_str, whatis_lines->sl_cur, sizeof (char *),
334*32a712daSGarrett D'Amore 	    linesort);
335*32a712daSGarrett D'Amore 	for (i = 0; i < whatis_lines->sl_cur; i++) {
336*32a712daSGarrett D'Amore 		char *line = whatis_lines->sl_str[i];
337*32a712daSGarrett D'Amore 		if (i > 0 && strcmp(line, prev) == 0)
338*32a712daSGarrett D'Amore 			continue;
339*32a712daSGarrett D'Amore 		prev = line;
340*32a712daSGarrett D'Amore 		(void) fputs(line, output);
341*32a712daSGarrett D'Amore 		(void) putc('\n', output);
342*32a712daSGarrett D'Amore 	}
343*32a712daSGarrett D'Amore 	(void) fclose(output);
344*32a712daSGarrett D'Amore 	sl_free(whatis_lines, 1);
345*32a712daSGarrett D'Amore 	(void) rename(tempfile, name);
346*32a712daSGarrett D'Amore 	(void) unlink(tempfile);
347*32a712daSGarrett D'Amore }
348*32a712daSGarrett D'Amore 
349*32a712daSGarrett D'Amore static FILE *
open_whatis(char * mandir)350*32a712daSGarrett D'Amore open_whatis(char *mandir)
351*32a712daSGarrett D'Amore {
352*32a712daSGarrett D'Amore 	char	filename[MAXPATHLEN];
353*32a712daSGarrett D'Amore 
354*32a712daSGarrett D'Amore 	(void) snprintf(filename, MAXPATHLEN, "%s/%s", mandir, WHATIS);
355*32a712daSGarrett D'Amore 	return (open_output(filename));
356*32a712daSGarrett D'Amore }
357*32a712daSGarrett D'Amore 
358*32a712daSGarrett D'Amore static void
finish_whatis(FILE * output,char * mandir)359*32a712daSGarrett D'Amore finish_whatis(FILE *output, char *mandir)
360*32a712daSGarrett D'Amore {
361*32a712daSGarrett D'Amore 	char	filename[MAXPATHLEN];
362*32a712daSGarrett D'Amore 
363*32a712daSGarrett D'Amore 	(void) snprintf(filename, MAXPATHLEN, "%s/%s", mandir, WHATIS);
364*32a712daSGarrett D'Amore 	finish_output(output, filename);
365*32a712daSGarrett D'Amore }
366*32a712daSGarrett D'Amore 
367*32a712daSGarrett D'Amore /*
368*32a712daSGarrett D'Amore  * Remove trailing spaces from a string, returning a pointer to just
369*32a712daSGarrett D'Amore  * beyond the new last character.
370*32a712daSGarrett D'Amore  */
371*32a712daSGarrett D'Amore static char *
trim_rhs(char * str)372*32a712daSGarrett D'Amore trim_rhs(char *str)
373*32a712daSGarrett D'Amore {
374*32a712daSGarrett D'Amore 	char	*rhs;
375*32a712daSGarrett D'Amore 
376*32a712daSGarrett D'Amore 	rhs = &str[strlen(str)];
377*32a712daSGarrett D'Amore 	while (--rhs > str && isspace(*rhs))
378*32a712daSGarrett D'Amore 		;
379*32a712daSGarrett D'Amore 	*++rhs = '\0';
380*32a712daSGarrett D'Amore 	return (rhs);
381*32a712daSGarrett D'Amore }
382*32a712daSGarrett D'Amore 
383*32a712daSGarrett D'Amore /*
384*32a712daSGarrett D'Amore  * Return a pointer to the next non-space character in the string.
385*32a712daSGarrett D'Amore  */
386*32a712daSGarrett D'Amore static char *
skip_spaces(char * s)387*32a712daSGarrett D'Amore skip_spaces(char *s)
388*32a712daSGarrett D'Amore {
389*32a712daSGarrett D'Amore 
390*32a712daSGarrett D'Amore 	while (*s != '\0' && isspace(*s))
391*32a712daSGarrett D'Amore 		s++;
392*32a712daSGarrett D'Amore 
393*32a712daSGarrett D'Amore 	return (s);
394*32a712daSGarrett D'Amore }
395*32a712daSGarrett D'Amore 
396*32a712daSGarrett D'Amore /*
397*32a712daSGarrett D'Amore  * Return whether the line is of one of the forms:
398*32a712daSGarrett D'Amore  *	.Sh NAME
399*32a712daSGarrett D'Amore  *	.Sh "NAME"
400*32a712daSGarrett D'Amore  *	etc.
401*32a712daSGarrett D'Amore  * assuming that section_start is ".Sh".
402*32a712daSGarrett D'Amore  */
403*32a712daSGarrett D'Amore static int
name_section_line(char * line,const char * section_start)404*32a712daSGarrett D'Amore name_section_line(char *line, const char *section_start)
405*32a712daSGarrett D'Amore {
406*32a712daSGarrett D'Amore 	char		*rhs;
407*32a712daSGarrett D'Amore 
408*32a712daSGarrett D'Amore 	if (strncmp(line, section_start, 3) != 0)
409*32a712daSGarrett D'Amore 		return (0);
410*32a712daSGarrett D'Amore 	line = skip_spaces(line + 3);
411*32a712daSGarrett D'Amore 	rhs = trim_rhs(line);
412*32a712daSGarrett D'Amore 	if (*line == '"') {
413*32a712daSGarrett D'Amore 		line++;
414*32a712daSGarrett D'Amore 		if (*--rhs == '"')
415*32a712daSGarrett D'Amore 			*rhs = '\0';
416*32a712daSGarrett D'Amore 	}
417*32a712daSGarrett D'Amore 	if (strcmp(line, "NAME") == 0)
418*32a712daSGarrett D'Amore 		return (1);
419*32a712daSGarrett D'Amore 
420*32a712daSGarrett D'Amore 	return (0);
421*32a712daSGarrett D'Amore }
422*32a712daSGarrett D'Amore 
423*32a712daSGarrett D'Amore /*
424*32a712daSGarrett D'Amore  * Copy characters while removing the most common nroff/troff markup:
425*32a712daSGarrett D'Amore  *	\(em, \(mi, \s[+-N], \&
426*32a712daSGarrett D'Amore  *	\fF, \f(fo, \f[font]
427*32a712daSGarrett D'Amore  *	\*s, \*(st, \*[stringvar]
428*32a712daSGarrett D'Amore  */
429*32a712daSGarrett D'Amore static char *
de_nroff_copy(char * from,char * to,int fromlen)430*32a712daSGarrett D'Amore de_nroff_copy(char *from, char *to, int fromlen)
431*32a712daSGarrett D'Amore {
432*32a712daSGarrett D'Amore 	char	*from_end = &from[fromlen];
433*32a712daSGarrett D'Amore 
434*32a712daSGarrett D'Amore 	while (from < from_end) {
435*32a712daSGarrett D'Amore 		switch (*from) {
436*32a712daSGarrett D'Amore 		case '\\':
437*32a712daSGarrett D'Amore 			switch (*++from) {
438*32a712daSGarrett D'Amore 			case '(':
439*32a712daSGarrett D'Amore 				if (strncmp(&from[1], "em", 2) == 0 ||
440*32a712daSGarrett D'Amore 				    strncmp(&from[1], "mi", 2) == 0) {
441*32a712daSGarrett D'Amore 					from += 3;
442*32a712daSGarrett D'Amore 					continue;
443*32a712daSGarrett D'Amore 				}
444*32a712daSGarrett D'Amore 				break;
445*32a712daSGarrett D'Amore 			case 's':
446*32a712daSGarrett D'Amore 				if (*++from == '-')
447*32a712daSGarrett D'Amore 					from++;
448*32a712daSGarrett D'Amore 				while (isdigit(*from))
449*32a712daSGarrett D'Amore 					from++;
450*32a712daSGarrett D'Amore 				continue;
451*32a712daSGarrett D'Amore 			case 'f':
452*32a712daSGarrett D'Amore 			case '*':
453*32a712daSGarrett D'Amore 				if (*++from == '(') {
454*32a712daSGarrett D'Amore 					from += 3;
455*32a712daSGarrett D'Amore 				} else if (*from == '[') {
456*32a712daSGarrett D'Amore 					while (*++from != ']' &&
457*32a712daSGarrett D'Amore 					    from < from_end)
458*32a712daSGarrett D'Amore 						;
459*32a712daSGarrett D'Amore 					from++;
460*32a712daSGarrett D'Amore 				} else {
461*32a712daSGarrett D'Amore 					from++;
462*32a712daSGarrett D'Amore 				}
463*32a712daSGarrett D'Amore 				continue;
464*32a712daSGarrett D'Amore 			case '&':
465*32a712daSGarrett D'Amore 				from++;
466*32a712daSGarrett D'Amore 				continue;
467*32a712daSGarrett D'Amore 			}
468*32a712daSGarrett D'Amore 			break;
469*32a712daSGarrett D'Amore 		}
470*32a712daSGarrett D'Amore 		*to++ = *from++;
471*32a712daSGarrett D'Amore 	}
472*32a712daSGarrett D'Amore 	return (to);
473*32a712daSGarrett D'Amore }
474*32a712daSGarrett D'Amore 
475*32a712daSGarrett D'Amore /*
476*32a712daSGarrett D'Amore  * Append a string with the nroff formatting removed.
477*32a712daSGarrett D'Amore  */
478*32a712daSGarrett D'Amore static void
add_nroff(char * text)479*32a712daSGarrett D'Amore add_nroff(char *text)
480*32a712daSGarrett D'Amore {
481*32a712daSGarrett D'Amore 
482*32a712daSGarrett D'Amore 	sbuf_append_edited(whatis_proto, text, de_nroff_copy);
483*32a712daSGarrett D'Amore }
484*32a712daSGarrett D'Amore 
485*32a712daSGarrett D'Amore /*
486*32a712daSGarrett D'Amore  * Appends "name(suffix), " to whatis_final
487*32a712daSGarrett D'Amore  */
488*32a712daSGarrett D'Amore static void
add_whatis_name(char * name,char * suffix)489*32a712daSGarrett D'Amore add_whatis_name(char *name, char *suffix)
490*32a712daSGarrett D'Amore {
491*32a712daSGarrett D'Amore 
492*32a712daSGarrett D'Amore 	if (*name != '\0') {
493*32a712daSGarrett D'Amore 		sbuf_append_str(whatis_final, name);
494*32a712daSGarrett D'Amore 		sbuf_append(whatis_final, "(", 1);
495*32a712daSGarrett D'Amore 		sbuf_append_str(whatis_final, suffix);
496*32a712daSGarrett D'Amore 		sbuf_append(whatis_final, "), ", 3);
497*32a712daSGarrett D'Amore 	}
498*32a712daSGarrett D'Amore }
499*32a712daSGarrett D'Amore 
500*32a712daSGarrett D'Amore /*
501*32a712daSGarrett D'Amore  * Processes an old-style man(7) line. This ignores commands with only
502*32a712daSGarrett D'Amore  * a single number argument.
503*32a712daSGarrett D'Amore  */
504*32a712daSGarrett D'Amore static void
process_man_line(char * line)505*32a712daSGarrett D'Amore process_man_line(char *line)
506*32a712daSGarrett D'Amore {
507*32a712daSGarrett D'Amore 	char	*p;
508*32a712daSGarrett D'Amore 
509*32a712daSGarrett D'Amore 	if (*line == '.') {
510*32a712daSGarrett D'Amore 		while (isalpha(*++line))
511*32a712daSGarrett D'Amore 			;
512*32a712daSGarrett D'Amore 		p = line = skip_spaces(line);
513*32a712daSGarrett D'Amore 		while (*p != '\0') {
514*32a712daSGarrett D'Amore 			if (!isdigit(*p))
515*32a712daSGarrett D'Amore 				break;
516*32a712daSGarrett D'Amore 			p++;
517*32a712daSGarrett D'Amore 		}
518*32a712daSGarrett D'Amore 		if (*p == '\0')
519*32a712daSGarrett D'Amore 			return;
520*32a712daSGarrett D'Amore 	} else
521*32a712daSGarrett D'Amore 		line = skip_spaces(line);
522*32a712daSGarrett D'Amore 	if (*line != '\0') {
523*32a712daSGarrett D'Amore 		add_nroff(line);
524*32a712daSGarrett D'Amore 		sbuf_append(whatis_proto, " ", 1);
525*32a712daSGarrett D'Amore 	}
526*32a712daSGarrett D'Amore }
527*32a712daSGarrett D'Amore 
528*32a712daSGarrett D'Amore /*
529*32a712daSGarrett D'Amore  * Processes a new-style mdoc(7) line.
530*32a712daSGarrett D'Amore  */
531*32a712daSGarrett D'Amore static void
process_mdoc_line(char * line)532*32a712daSGarrett D'Amore process_mdoc_line(char *line)
533*32a712daSGarrett D'Amore {
534*32a712daSGarrett D'Amore 	int	xref;
535*32a712daSGarrett D'Amore 	int	arg = 0;
536*32a712daSGarrett D'Amore 	char	*line_end = &line[strlen(line)];
537*32a712daSGarrett D'Amore 	int	orig_length = sbuf_length(whatis_proto);
538*32a712daSGarrett D'Amore 	char	*next;
539*32a712daSGarrett D'Amore 
540*32a712daSGarrett D'Amore 	if (*line == '\0')
541*32a712daSGarrett D'Amore 		return;
542*32a712daSGarrett D'Amore 	if (line[0] != '.' || !isupper(line[1]) || !islower(line[2])) {
543*32a712daSGarrett D'Amore 		add_nroff(skip_spaces(line));
544*32a712daSGarrett D'Amore 		sbuf_append(whatis_proto, " ", 1);
545*32a712daSGarrett D'Amore 		return;
546*32a712daSGarrett D'Amore 	}
547*32a712daSGarrett D'Amore 	xref = strncmp(line, ".Xr", 3) == 0;
548*32a712daSGarrett D'Amore 	line += 3;
549*32a712daSGarrett D'Amore 	while ((line = skip_spaces(line)) < line_end) {
550*32a712daSGarrett D'Amore 		if (*line == '"') {
551*32a712daSGarrett D'Amore 			next = ++line;
552*32a712daSGarrett D'Amore 			for (;;) {
553*32a712daSGarrett D'Amore 				next = strchr(next, '"');
554*32a712daSGarrett D'Amore 				if (next == NULL)
555*32a712daSGarrett D'Amore 					break;
556*32a712daSGarrett D'Amore 				(void) memmove(next, next + 1, strlen(next));
557*32a712daSGarrett D'Amore 				line_end--;
558*32a712daSGarrett D'Amore 				if (*next != '"')
559*32a712daSGarrett D'Amore 					break;
560*32a712daSGarrett D'Amore 				next++;
561*32a712daSGarrett D'Amore 			}
562*32a712daSGarrett D'Amore 		} else {
563*32a712daSGarrett D'Amore 			next = strpbrk(line, " \t");
564*32a712daSGarrett D'Amore 		}
565*32a712daSGarrett D'Amore 		if (next != NULL)
566*32a712daSGarrett D'Amore 			*next++ = '\0';
567*32a712daSGarrett D'Amore 		else
568*32a712daSGarrett D'Amore 			next = line_end;
569*32a712daSGarrett D'Amore 		if (isupper(*line) && islower(line[1]) && line[2] == '\0') {
570*32a712daSGarrett D'Amore 			if (strcmp(line, "Ns") == 0) {
571*32a712daSGarrett D'Amore 				arg = 0;
572*32a712daSGarrett D'Amore 				line = next;
573*32a712daSGarrett D'Amore 				continue;
574*32a712daSGarrett D'Amore 			}
575*32a712daSGarrett D'Amore 			if (strstr(line, MDOC_COMMANDS) != NULL) {
576*32a712daSGarrett D'Amore 				line = next;
577*32a712daSGarrett D'Amore 				continue;
578*32a712daSGarrett D'Amore 			}
579*32a712daSGarrett D'Amore 		}
580*32a712daSGarrett D'Amore 		if (arg > 0 && strchr(",.:;?!)]", *line) == 0) {
581*32a712daSGarrett D'Amore 			if (xref) {
582*32a712daSGarrett D'Amore 				sbuf_append(whatis_proto, "(", 1);
583*32a712daSGarrett D'Amore 				add_nroff(line);
584*32a712daSGarrett D'Amore 				sbuf_append(whatis_proto, ")", 1);
585*32a712daSGarrett D'Amore 				xref = 0;
586*32a712daSGarrett D'Amore 			} else {
587*32a712daSGarrett D'Amore 				sbuf_append(whatis_proto, " ", 1);
588*32a712daSGarrett D'Amore 			}
589*32a712daSGarrett D'Amore 		}
590*32a712daSGarrett D'Amore 		add_nroff(line);
591*32a712daSGarrett D'Amore 		arg++;
592*32a712daSGarrett D'Amore 		line = next;
593*32a712daSGarrett D'Amore 	}
594*32a712daSGarrett D'Amore 	if (sbuf_length(whatis_proto) > orig_length)
595*32a712daSGarrett D'Amore 		sbuf_append(whatis_proto, " ", 1);
596*32a712daSGarrett D'Amore }
597*32a712daSGarrett D'Amore 
598*32a712daSGarrett D'Amore /*
599*32a712daSGarrett D'Amore  * Collect a list of comma-separated names from the text.
600*32a712daSGarrett D'Amore  */
601*32a712daSGarrett D'Amore static void
collect_names(stringlist * names,char * text)602*32a712daSGarrett D'Amore collect_names(stringlist *names, char *text)
603*32a712daSGarrett D'Amore {
604*32a712daSGarrett D'Amore 	char	*arg;
605*32a712daSGarrett D'Amore 
606*32a712daSGarrett D'Amore 	for (;;) {
607*32a712daSGarrett D'Amore 		arg = text;
608*32a712daSGarrett D'Amore 		text = strchr(text, ',');
609*32a712daSGarrett D'Amore 		if (text != NULL)
610*32a712daSGarrett D'Amore 			*text++ = '\0';
611*32a712daSGarrett D'Amore 		(void) sl_add(names, arg);
612*32a712daSGarrett D'Amore 		if (text == NULL)
613*32a712daSGarrett D'Amore 			return;
614*32a712daSGarrett D'Amore 		if (*text == ' ')
615*32a712daSGarrett D'Amore 			text++;
616*32a712daSGarrett D'Amore 	}
617*32a712daSGarrett D'Amore }
618*32a712daSGarrett D'Amore 
619*32a712daSGarrett D'Amore enum { STATE_UNKNOWN, STATE_MANSTYLE, STATE_MDOCNAME, STATE_MDOCDESC };
620*32a712daSGarrett D'Amore 
621*32a712daSGarrett D'Amore /*
622*32a712daSGarrett D'Amore  * Process a man page source into a single whatis line and add it
623*32a712daSGarrett D'Amore  * to whatis_lines.
624*32a712daSGarrett D'Amore  */
625*32a712daSGarrett D'Amore static void
process_page(struct page_info * page,char * section_dir)626*32a712daSGarrett D'Amore process_page(struct page_info *page, char *section_dir)
627*32a712daSGarrett D'Amore {
628*32a712daSGarrett D'Amore 	FILE		*fp;
629*32a712daSGarrett D'Amore 	stringlist	*names;
630*32a712daSGarrett D'Amore 	char		*descr;
631*32a712daSGarrett D'Amore 	int		state = STATE_UNKNOWN;
632*32a712daSGarrett D'Amore 	size_t		i;
633*32a712daSGarrett D'Amore 	char		*line = NULL;
634*32a712daSGarrett D'Amore 	size_t		linecap = 0;
635*32a712daSGarrett D'Amore 
636*32a712daSGarrett D'Amore 	sbuf_clear(whatis_proto);
637*32a712daSGarrett D'Amore 	if ((fp = fopen(page->filename, "r")) == NULL) {
638*32a712daSGarrett D'Amore 		warn("%s", page->filename);
639*32a712daSGarrett D'Amore 		return;
640*32a712daSGarrett D'Amore 	}
641*32a712daSGarrett D'Amore 	while (getline(&line, &linecap, fp) > 0) {
642*32a712daSGarrett D'Amore 		/* Skip comments */
643*32a712daSGarrett D'Amore 		if (strncmp(line, ".\\\"", 3) == 0)
644*32a712daSGarrett D'Amore 			continue;
645*32a712daSGarrett D'Amore 		switch (state) {
646*32a712daSGarrett D'Amore 		/* Haven't reached the NAME section yet */
647*32a712daSGarrett D'Amore 		case STATE_UNKNOWN:
648*32a712daSGarrett D'Amore 			if (name_section_line(line, ".SH"))
649*32a712daSGarrett D'Amore 				state = STATE_MANSTYLE;
650*32a712daSGarrett D'Amore 			else if (name_section_line(line, ".Sh"))
651*32a712daSGarrett D'Amore 				state = STATE_MDOCNAME;
652*32a712daSGarrett D'Amore 			continue;
653*32a712daSGarrett D'Amore 		/* Inside an old-style .SH NAME section */
654*32a712daSGarrett D'Amore 		case STATE_MANSTYLE:
655*32a712daSGarrett D'Amore 			if (strncmp(line, ".SH", 3) == 0 ||
656*32a712daSGarrett D'Amore 			    strncmp(line, ".SS", 3) == 0)
657*32a712daSGarrett D'Amore 				break;
658*32a712daSGarrett D'Amore 			(void) trim_rhs(line);
659*32a712daSGarrett D'Amore 			if (strcmp(line, ".") == 0)
660*32a712daSGarrett D'Amore 				continue;
661*32a712daSGarrett D'Amore 			if (strncmp(line, ".IX", 3) == 0) {
662*32a712daSGarrett D'Amore 				line += 3;
663*32a712daSGarrett D'Amore 				line = skip_spaces(line);
664*32a712daSGarrett D'Amore 			}
665*32a712daSGarrett D'Amore 			process_man_line(line);
666*32a712daSGarrett D'Amore 			continue;
667*32a712daSGarrett D'Amore 		/* Inside a new-style .Sh NAME section (the .Nm part) */
668*32a712daSGarrett D'Amore 		case STATE_MDOCNAME:
669*32a712daSGarrett D'Amore 			(void) trim_rhs(line);
670*32a712daSGarrett D'Amore 			if (strncmp(line, ".Nm", 3) == 0) {
671*32a712daSGarrett D'Amore 				process_mdoc_line(line);
672*32a712daSGarrett D'Amore 				continue;
673*32a712daSGarrett D'Amore 			} else {
674*32a712daSGarrett D'Amore 				if (strcmp(line, ".") == 0)
675*32a712daSGarrett D'Amore 					continue;
676*32a712daSGarrett D'Amore 				sbuf_append(whatis_proto, "- ", 2);
677*32a712daSGarrett D'Amore 				state = STATE_MDOCDESC;
678*32a712daSGarrett D'Amore 			}
679*32a712daSGarrett D'Amore 			/* FALLTHROUGH */
680*32a712daSGarrett D'Amore 		/* Inside a new-style .Sh NAME section (after the .Nm-s) */
681*32a712daSGarrett D'Amore 		case STATE_MDOCDESC:
682*32a712daSGarrett D'Amore 			if (strncmp(line, ".Sh", 3) == 0)
683*32a712daSGarrett D'Amore 				break;
684*32a712daSGarrett D'Amore 			(void) trim_rhs(line);
685*32a712daSGarrett D'Amore 			if (strcmp(line, ".") == 0)
686*32a712daSGarrett D'Amore 				continue;
687*32a712daSGarrett D'Amore 			process_mdoc_line(line);
688*32a712daSGarrett D'Amore 			continue;
689*32a712daSGarrett D'Amore 		}
690*32a712daSGarrett D'Amore 		break;
691*32a712daSGarrett D'Amore 	}
692*32a712daSGarrett D'Amore 	(void) fclose(fp);
693*32a712daSGarrett D'Amore 	sbuf_strip(whatis_proto, " \t.-");
694*32a712daSGarrett D'Amore 	line = sbuf_content(whatis_proto);
695*32a712daSGarrett D'Amore 	/*
696*32a712daSGarrett D'Amore 	 * Line now contains the appropriate data, but without the
697*32a712daSGarrett D'Amore 	 * proper indentation or the section appended to each name.
698*32a712daSGarrett D'Amore 	 */
699*32a712daSGarrett D'Amore 	descr = strstr(line, " - ");
700*32a712daSGarrett D'Amore 	if (descr == NULL) {
701*32a712daSGarrett D'Amore 		descr = strchr(line, ' ');
702*32a712daSGarrett D'Amore 		if (descr == NULL)
703*32a712daSGarrett D'Amore 			return;
704*32a712daSGarrett D'Amore 		*descr++ = '\0';
705*32a712daSGarrett D'Amore 	} else {
706*32a712daSGarrett D'Amore 		*descr = '\0';
707*32a712daSGarrett D'Amore 		descr += 3;
708*32a712daSGarrett D'Amore 	}
709*32a712daSGarrett D'Amore 	names = sl_init();
710*32a712daSGarrett D'Amore 	collect_names(names, line);
711*32a712daSGarrett D'Amore 	sbuf_clear(whatis_final);
712*32a712daSGarrett D'Amore 	if (!sl_find(names, page->name) &&
713*32a712daSGarrett D'Amore 	    no_page_exists(section_dir, names, page->suffix)) {
714*32a712daSGarrett D'Amore 		/*
715*32a712daSGarrett D'Amore 		 * Add the page name since that's the only
716*32a712daSGarrett D'Amore 		 * thing that man(1) will find.
717*32a712daSGarrett D'Amore 		 */
718*32a712daSGarrett D'Amore 		add_whatis_name(page->name, page->suffix);
719*32a712daSGarrett D'Amore 	}
720*32a712daSGarrett D'Amore 	for (i = 0; i < names->sl_cur; i++)
721*32a712daSGarrett D'Amore 		add_whatis_name(names->sl_str[i], page->suffix);
722*32a712daSGarrett D'Amore 	sl_free(names, 0);
723*32a712daSGarrett D'Amore 	/* Remove last ", " */
724*32a712daSGarrett D'Amore 	sbuf_retract(whatis_final, 2);
725*32a712daSGarrett D'Amore 	while (sbuf_length(whatis_final) < INDENT)
726*32a712daSGarrett D'Amore 		sbuf_append(whatis_final, " ", 1);
727*32a712daSGarrett D'Amore 	sbuf_append(whatis_final, " - ", 3);
728*32a712daSGarrett D'Amore 	sbuf_append_str(whatis_final, skip_spaces(descr));
729*32a712daSGarrett D'Amore 	(void) sl_add(whatis_lines, strdup(sbuf_content(whatis_final)));
730*32a712daSGarrett D'Amore }
731*32a712daSGarrett D'Amore 
732*32a712daSGarrett D'Amore /*
733*32a712daSGarrett D'Amore  * Sort pages first by inode number, then by name.
734*32a712daSGarrett D'Amore  */
735*32a712daSGarrett D'Amore static int
pagesort(const void * a,const void * b)736*32a712daSGarrett D'Amore pagesort(const void *a, const void *b)
737*32a712daSGarrett D'Amore {
738*32a712daSGarrett D'Amore 	const struct page_info *p1 = *(struct page_info * const *) a;
739*32a712daSGarrett D'Amore 	const struct page_info *p2 = *(struct page_info * const *) b;
740*32a712daSGarrett D'Amore 
741*32a712daSGarrett D'Amore 	if (p1->inode == p2->inode)
742*32a712daSGarrett D'Amore 		return (strcmp(p1->name, p2->name));
743*32a712daSGarrett D'Amore 
744*32a712daSGarrett D'Amore 	return (p1->inode - p2->inode);
745*32a712daSGarrett D'Amore }
746*32a712daSGarrett D'Amore 
747*32a712daSGarrett D'Amore /*
748*32a712daSGarrett D'Amore  * Process a single man section.
749*32a712daSGarrett D'Amore  */
750*32a712daSGarrett D'Amore static void
process_section(char * section_dir)751*32a712daSGarrett D'Amore process_section(char *section_dir)
752*32a712daSGarrett D'Amore {
753*32a712daSGarrett D'Amore 	struct dirent	**entries;
754*32a712daSGarrett D'Amore 	int		nentries;
755*32a712daSGarrett D'Amore 	struct page_info **pages;
756*32a712daSGarrett D'Amore 	int		npages = 0;
757*32a712daSGarrett D'Amore 	int		i;
758*32a712daSGarrett D'Amore 	ino_t		prev_inode = 0;
759*32a712daSGarrett D'Amore 
760*32a712daSGarrett D'Amore 	/* Scan the man section directory for pages */
761*32a712daSGarrett D'Amore 	nentries = scandir(section_dir, &entries, NULL, alphasort);
762*32a712daSGarrett D'Amore 
763*32a712daSGarrett D'Amore 	/* Collect information about man pages */
764*32a712daSGarrett D'Amore 	pages = (struct page_info **)calloc(nentries,
765*32a712daSGarrett D'Amore 	    sizeof (struct page_info *));
766*32a712daSGarrett D'Amore 	for (i = 0; i < nentries; i++) {
767*32a712daSGarrett D'Amore 		struct page_info *info = new_page_info(section_dir, entries[i]);
768*32a712daSGarrett D'Amore 		if (info != NULL)
769*32a712daSGarrett D'Amore 			pages[npages++] = info;
770*32a712daSGarrett D'Amore 		free(entries[i]);
771*32a712daSGarrett D'Amore 	}
772*32a712daSGarrett D'Amore 	free(entries);
773*32a712daSGarrett D'Amore 	qsort(pages, npages, sizeof (struct page_info *), pagesort);
774*32a712daSGarrett D'Amore 
775*32a712daSGarrett D'Amore 	/* Process each unique page */
776*32a712daSGarrett D'Amore 	for (i = 0; i < npages; i++) {
777*32a712daSGarrett D'Amore 		struct page_info *page = pages[i];
778*32a712daSGarrett D'Amore 		if (page->inode != prev_inode) {
779*32a712daSGarrett D'Amore 			prev_inode = page->inode;
780*32a712daSGarrett D'Amore 			process_page(page, section_dir);
781*32a712daSGarrett D'Amore 		}
782*32a712daSGarrett D'Amore 		free_page_info(page);
783*32a712daSGarrett D'Amore 	}
784*32a712daSGarrett D'Amore 	free(pages);
785*32a712daSGarrett D'Amore }
786*32a712daSGarrett D'Amore 
787*32a712daSGarrett D'Amore /*
788*32a712daSGarrett D'Amore  * Return whether the directory entry is a man page section.
789*32a712daSGarrett D'Amore  */
790*32a712daSGarrett D'Amore static int
select_sections(const struct dirent * entry)791*32a712daSGarrett D'Amore select_sections(const struct dirent *entry)
792*32a712daSGarrett D'Amore {
793*32a712daSGarrett D'Amore 	const char	*p = &entry->d_name[3];
794*32a712daSGarrett D'Amore 
795*32a712daSGarrett D'Amore 	if (strncmp(entry->d_name, "man", 3) != 0)
796*32a712daSGarrett D'Amore 		return (0);
797*32a712daSGarrett D'Amore 	while (*p != '\0') {
798*32a712daSGarrett D'Amore 		if (!isalnum(*p++))
799*32a712daSGarrett D'Amore 			return (0);
800*32a712daSGarrett D'Amore 	}
801*32a712daSGarrett D'Amore 	return (1);
802*32a712daSGarrett D'Amore }
803*32a712daSGarrett D'Amore 
804*32a712daSGarrett D'Amore /*
805*32a712daSGarrett D'Amore  * Process a single top-level man directory by finding all the
806*32a712daSGarrett D'Amore  * sub-directories named man* and processing each one in turn.
807*32a712daSGarrett D'Amore  */
808*32a712daSGarrett D'Amore void
mwpath(char * path)809*32a712daSGarrett D'Amore mwpath(char *path)
810*32a712daSGarrett D'Amore {
811*32a712daSGarrett D'Amore 	FILE		*fp = NULL;
812*32a712daSGarrett D'Amore 	struct dirent	**entries;
813*32a712daSGarrett D'Amore 	int		nsections;
814*32a712daSGarrett D'Amore 	int		i;
815*32a712daSGarrett D'Amore 
816*32a712daSGarrett D'Amore 	(void) signal(SIGINT, trap_signal);
817*32a712daSGarrett D'Amore 	(void) signal(SIGHUP, trap_signal);
818*32a712daSGarrett D'Amore 	(void) signal(SIGQUIT, trap_signal);
819*32a712daSGarrett D'Amore 	(void) signal(SIGTERM, trap_signal);
820*32a712daSGarrett D'Amore 
821*32a712daSGarrett D'Amore 	whatis_proto = new_sbuf();
822*32a712daSGarrett D'Amore 	whatis_final = new_sbuf();
823*32a712daSGarrett D'Amore 
824*32a712daSGarrett D'Amore 	nsections = scandir(path, &entries, select_sections, alphasort);
825*32a712daSGarrett D'Amore 	if ((fp = open_whatis(path)) == NULL)
826*32a712daSGarrett D'Amore 		return;
827*32a712daSGarrett D'Amore 	for (i = 0; i < nsections; i++) {
828*32a712daSGarrett D'Amore 		char	section_dir[MAXPATHLEN];
829*32a712daSGarrett D'Amore 
830*32a712daSGarrett D'Amore 		(void) snprintf(section_dir, MAXPATHLEN, "%s/%s",
831*32a712daSGarrett D'Amore 		    path, entries[i]->d_name);
832*32a712daSGarrett D'Amore 		process_section(section_dir);
833*32a712daSGarrett D'Amore 		free(entries[i]);
834*32a712daSGarrett D'Amore 	}
835*32a712daSGarrett D'Amore 	free(entries);
836*32a712daSGarrett D'Amore 	finish_whatis(fp, path);
837*32a712daSGarrett D'Amore }
838