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