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