| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: makewhatis.c,v 1.45 2008/11/16 06:17:05 dholland Exp $ */ | | 1 | /* $NetBSD: makewhatis.c,v 1.46 2008/11/16 06:26:12 dholland Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 1999 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 1999 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Matthias Scheler. | | 8 | * by Matthias Scheler. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -27,27 +27,27 @@ | | | @@ -27,27 +27,27 @@ |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #if HAVE_NBTOOL_CONFIG_H | | 32 | #if HAVE_NBTOOL_CONFIG_H |
33 | #include "nbtool_config.h" | | 33 | #include "nbtool_config.h" |
34 | #endif | | 34 | #endif |
35 | | | 35 | |
36 | #include <sys/cdefs.h> | | 36 | #include <sys/cdefs.h> |
37 | #if !defined(lint) | | 37 | #if !defined(lint) |
38 | __COPYRIGHT("@(#) Copyright (c) 1999\ | | 38 | __COPYRIGHT("@(#) Copyright (c) 1999\ |
39 | The NetBSD Foundation, Inc. All rights reserved."); | | 39 | The NetBSD Foundation, Inc. All rights reserved."); |
40 | __RCSID("$NetBSD: makewhatis.c,v 1.45 2008/11/16 06:17:05 dholland Exp $"); | | 40 | __RCSID("$NetBSD: makewhatis.c,v 1.46 2008/11/16 06:26:12 dholland Exp $"); |
41 | #endif /* not lint */ | | 41 | #endif /* not lint */ |
42 | | | 42 | |
43 | #include <sys/types.h> | | 43 | #include <sys/types.h> |
44 | #include <sys/param.h> | | 44 | #include <sys/param.h> |
45 | #include <sys/queue.h> | | 45 | #include <sys/queue.h> |
46 | #include <sys/stat.h> | | 46 | #include <sys/stat.h> |
47 | #include <sys/wait.h> | | 47 | #include <sys/wait.h> |
48 | | | 48 | |
49 | #include <ctype.h> | | 49 | #include <ctype.h> |
50 | #include <err.h> | | 50 | #include <err.h> |
51 | #include <errno.h> | | 51 | #include <errno.h> |
52 | #include <fcntl.h> | | 52 | #include <fcntl.h> |
53 | #include <fts.h> | | 53 | #include <fts.h> |
| @@ -61,58 +61,58 @@ __RCSID("$NetBSD: makewhatis.c,v 1.45 20 | | | @@ -61,58 +61,58 @@ __RCSID("$NetBSD: makewhatis.c,v 1.45 20 |
61 | #include <unistd.h> | | 61 | #include <unistd.h> |
62 | #include <zlib.h> | | 62 | #include <zlib.h> |
63 | #include <util.h> | | 63 | #include <util.h> |
64 | | | 64 | |
65 | #include <man/manconf.h> | | 65 | #include <man/manconf.h> |
66 | #include <man/pathnames.h> | | 66 | #include <man/pathnames.h> |
67 | | | 67 | |
68 | #ifndef NROFF | | 68 | #ifndef NROFF |
69 | #define NROFF "nroff" | | 69 | #define NROFF "nroff" |
70 | #endif | | 70 | #endif |
71 | | | 71 | |
72 | typedef struct manpagestruct manpage; | | 72 | typedef struct manpagestruct manpage; |
73 | struct manpagestruct { | | 73 | struct manpagestruct { |
74 | manpage *mp_left,*mp_right; | | 74 | manpage *mp_left, *mp_right; |
75 | ino_t mp_inode; | | 75 | ino_t mp_inode; |
76 | size_t mp_sdoff; | | 76 | size_t mp_sdoff; |
77 | size_t mp_sdlen; | | 77 | size_t mp_sdlen; |
78 | char mp_name[1]; | | 78 | char mp_name[1]; |
79 | }; | | 79 | }; |
80 | | | 80 | |
81 | typedef struct whatisstruct whatis; | | 81 | typedef struct whatisstruct whatis; |
82 | struct whatisstruct { | | 82 | struct whatisstruct { |
83 | whatis *wi_left,*wi_right; | | 83 | whatis *wi_left, *wi_right; |
84 | char *wi_data; | | 84 | char *wi_data; |
85 | char wi_prefix[1]; | | 85 | char wi_prefix[1]; |
86 | }; | | 86 | }; |
87 | | | 87 | |
88 | int main(int, char * const *); | | 88 | int main(int, char * const *); |
89 | static char *findwhitespace(char *); | | 89 | static char *findwhitespace(char *); |
90 | static char *strmove(char *,char *); | | 90 | static char *strmove(char *, char *); |
91 | static char *GetS(gzFile, char *, size_t); | | 91 | static char *GetS(gzFile, char *, size_t); |
92 | static int pathnamesection(const char *, const char *); | | 92 | static int pathnamesection(const char *, const char *); |
93 | static int manpagesection(char *); | | 93 | static int manpagesection(char *); |
94 | static char *createsectionstring(char *); | | 94 | static char *createsectionstring(char *); |
95 | static void addmanpage(manpage **, ino_t, char *, size_t, size_t); | | 95 | static void addmanpage(manpage **, ino_t, char *, size_t, size_t); |
96 | static void addwhatis(whatis **, char *, char *); | | 96 | static void addwhatis(whatis **, char *, char *); |
97 | static char *makesection(int); | | 97 | static char *makesection(int); |
98 | static char *makewhatisline(const char *, const char *, const char *); | | 98 | static char *makewhatisline(const char *, const char *, const char *); |
99 | static void catpreprocess(char *); | | 99 | static void catpreprocess(char *); |
100 | static char *parsecatpage(const char *, gzFile *); | | 100 | static char *parsecatpage(const char *, gzFile *); |
101 | static int manpreprocess(char *); | | 101 | static int manpreprocess(char *); |
102 | static char *nroff(const char *, gzFile *); | | 102 | static char *nroff(const char *, gzFile *); |
103 | static char *parsemanpage(const char *, gzFile *, int); | | 103 | static char *parsemanpage(const char *, gzFile *, int); |
104 | static char *getwhatisdata(char *); | | 104 | static char *getwhatisdata(char *); |
105 | static void processmanpages(manpage **,whatis **); | | 105 | static void processmanpages(manpage **, whatis **); |
106 | static void dumpwhatis(FILE *, whatis *); | | 106 | static void dumpwhatis(FILE *, whatis *); |
107 | static int makewhatis(char * const *manpath); | | 107 | static int makewhatis(char * const *manpath); |
108 | | | 108 | |
109 | static char * const default_manpath[] = { | | 109 | static char * const default_manpath[] = { |
110 | "/usr/share/man", | | 110 | "/usr/share/man", |
111 | NULL | | 111 | NULL |
112 | }; | | 112 | }; |
113 | | | 113 | |
114 | static const char *sectionext = "0123456789ln"; | | 114 | static const char *sectionext = "0123456789ln"; |
115 | static const char *whatisdb = _PATH_WHATIS; | | 115 | static const char *whatisdb = _PATH_WHATIS; |
116 | static const char *whatisdb_new = _PATH_WHATIS ".new"; | | 116 | static const char *whatisdb_new = _PATH_WHATIS ".new"; |
117 | static int dowarn = 0; | | 117 | static int dowarn = 0; |
118 | | | 118 | |
| @@ -150,30 +150,30 @@ main(int argc, char *const *argv) | | | @@ -150,30 +150,30 @@ main(int argc, char *const *argv) |
150 | dofork = 0; | | 150 | dofork = 0; |
151 | break; | | 151 | break; |
152 | case 'w': | | 152 | case 'w': |
153 | dowarn++; | | 153 | dowarn++; |
154 | break; | | 154 | break; |
155 | default: | | 155 | default: |
156 | fprintf(stderr, "Usage: %s [-fw] [-C file] [manpath ...]\n", | | 156 | fprintf(stderr, "Usage: %s [-fw] [-C file] [manpath ...]\n", |
157 | getprogname()); | | 157 | getprogname()); |
158 | exit(EXIT_FAILURE); | | 158 | exit(EXIT_FAILURE); |
159 | } | | 159 | } |
160 | } | | 160 | } |
161 | argc -= optind; | | 161 | argc -= optind; |
162 | argv += optind; | | 162 | argv += optind; |
163 | | | 163 | |
164 | if (argc >= 1) { | | 164 | if (argc >= 1) { |
165 | manpath = &argv[0]; | | 165 | manpath = &argv[0]; |
166 | | | 166 | |
167 | mkwhatis: | | 167 | mkwhatis: |
168 | return makewhatis(manpath); | | 168 | return makewhatis(manpath); |
169 | } | | 169 | } |
170 | | | 170 | |
171 | /* | | 171 | /* |
172 | * Try read config file, fallback to default_manpath[] | | 172 | * Try read config file, fallback to default_manpath[] |
173 | * if man.conf not available. | | 173 | * if man.conf not available. |
174 | */ | | 174 | */ |
175 | config(conffile); | | 175 | config(conffile); |
176 | if ((tp = gettag("_whatdb", 0)) == NULL) { | | 176 | if ((tp = gettag("_whatdb", 0)) == NULL) { |
177 | manpath = default_manpath; | | 177 | manpath = default_manpath; |
178 | goto mkwhatis; | | 178 | goto mkwhatis; |
179 | } | | 179 | } |
| @@ -212,40 +212,40 @@ main(int argc, char *const *argv) | | | @@ -212,40 +212,40 @@ main(int argc, char *const *argv) |
212 | | | 212 | |
213 | switch (fork()) { | | 213 | switch (fork()) { |
214 | case 0: | | 214 | case 0: |
215 | exit(makewhatis(paths)); | | 215 | exit(makewhatis(paths)); |
216 | break; | | 216 | break; |
217 | case -1: | | 217 | case -1: |
218 | warn("fork"); | | 218 | warn("fork"); |
219 | makewhatis(paths); | | 219 | makewhatis(paths); |
220 | break; | | 220 | break; |
221 | default: | | 221 | default: |
222 | jobs++; | | 222 | jobs++; |
223 | break; | | 223 | break; |
224 | } | | 224 | } |
225 | | | 225 | |
226 | } | | 226 | } |
227 | | | 227 | |
228 | globfree(&pg); | | 228 | globfree(&pg); |
229 | } | | 229 | } |
230 | | | 230 | |
231 | /* Wait for the childern to finish */ | | 231 | /* Wait for the childern to finish */ |
232 | while (jobs > 0) { | | 232 | while (jobs > 0) { |
233 | (void)wait(&status); | | 233 | (void)wait(&status); |
234 | if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) | | 234 | if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) |
235 | retval = EXIT_FAILURE; | | 235 | retval = EXIT_FAILURE; |
236 | jobs--; | | 236 | jobs--; |
237 | } | | 237 | } |
238 | | | 238 | |
239 | return retval; | | 239 | return retval; |
240 | } | | 240 | } |
241 | | | 241 | |
242 | static int | | 242 | static int |
243 | makewhatis(char * const * manpath) | | 243 | makewhatis(char * const * manpath) |
244 | { | | 244 | { |
245 | FTS *fts; | | 245 | FTS *fts; |
246 | FTSENT *fe; | | 246 | FTSENT *fe; |
247 | manpage *source; | | 247 | manpage *source; |
248 | whatis *dest; | | 248 | whatis *dest; |
249 | FILE *out; | | 249 | FILE *out; |
250 | size_t sdoff, sdlen; | | 250 | size_t sdoff, sdlen; |
251 | int outfd; | | 251 | int outfd; |
| @@ -267,29 +267,31 @@ makewhatis(char * const * manpath) | | | @@ -267,29 +267,31 @@ makewhatis(char * const * manpath) |
267 | int sl; | | 267 | int sl; |
268 | const char *s, *lsl; | | 268 | const char *s, *lsl; |
269 | | | 269 | |
270 | lsl = NULL; | | 270 | lsl = NULL; |
271 | s = &fe->fts_path[fe->fts_pathlen - 1]; | | 271 | s = &fe->fts_path[fe->fts_pathlen - 1]; |
272 | for(sl = fe->fts_level - 1; sl > 0; | | 272 | for(sl = fe->fts_level - 1; sl > 0; |
273 | sl--) { | | 273 | sl--) { |
274 | s--; | | 274 | s--; |
275 | while (s[0] != '/') | | 275 | while (s[0] != '/') |
276 | s--; | | 276 | s--; |
277 | if (lsl == NULL) | | 277 | if (lsl == NULL) |
278 | lsl = s; | | 278 | lsl = s; |
279 | } | | 279 | } |
280 | | | 280 | |
281 | /* Include trailing '/', so we get | | 281 | /* |
282 | * 'arch/'. */ | | 282 | * Include trailing '/', so we get |
| | | 283 | * 'arch/'. |
| | | 284 | */ |
283 | sdoff = s + 1 - fe->fts_path; | | 285 | sdoff = s + 1 - fe->fts_path; |
284 | sdlen = lsl - s + 1; | | 286 | sdlen = lsl - s + 1; |
285 | } else { | | 287 | } else { |
286 | sdoff = 0; | | 288 | sdoff = 0; |
287 | sdlen = 0; | | 289 | sdlen = 0; |
288 | } | | 290 | } |
289 | | | 291 | |
290 | addmanpage(&source, fe->fts_statp->st_ino, | | 292 | addmanpage(&source, fe->fts_statp->st_ino, |
291 | fe->fts_path, sdoff, sdlen); | | 293 | fe->fts_path, sdoff, sdlen); |
292 | } | | 294 | } |
293 | /*FALLTHROUGH*/ | | 295 | /*FALLTHROUGH*/ |
294 | case FTS_D: | | 296 | case FTS_D: |
295 | case FTS_DC: | | 297 | case FTS_DC: |
| @@ -408,28 +410,28 @@ makewhatis(char * const * manpath) | | | @@ -408,28 +410,28 @@ makewhatis(char * const * manpath) |
408 | | | 410 | |
409 | static char * | | 411 | static char * |
410 | findwhitespace(char *str) | | 412 | findwhitespace(char *str) |
411 | { | | 413 | { |
412 | while (!ISSPACE(*str)) | | 414 | while (!ISSPACE(*str)) |
413 | if (*str++ == '\0') { | | 415 | if (*str++ == '\0') { |
414 | str = NULL; | | 416 | str = NULL; |
415 | break; | | 417 | break; |
416 | } | | 418 | } |
417 | | | 419 | |
418 | return str; | | 420 | return str; |
419 | } | | 421 | } |
420 | | | 422 | |
421 | static char | | 423 | static char * |
422 | *strmove(char *dest,char *src) | | 424 | strmove(char *dest, char *src) |
423 | { | | 425 | { |
424 | return memmove(dest, src, strlen(src) + 1); | | 426 | return memmove(dest, src, strlen(src) + 1); |
425 | } | | 427 | } |
426 | | | 428 | |
427 | static char * | | 429 | static char * |
428 | GetS(gzFile in, char *buffer, size_t length) | | 430 | GetS(gzFile in, char *buffer, size_t length) |
429 | { | | 431 | { |
430 | char *ptr; | | 432 | char *ptr; |
431 | | | 433 | |
432 | if (((ptr = gzgets(in, buffer, (int)length)) != NULL) && (*ptr == '\0')) | | 434 | if (((ptr = gzgets(in, buffer, (int)length)) != NULL) && (*ptr == '\0')) |
433 | ptr = NULL; | | 435 | ptr = NULL; |
434 | | | 436 | |
435 | return ptr; | | 437 | return ptr; |
| @@ -488,27 +490,27 @@ manpagesection(char *name) | | | @@ -488,27 +490,27 @@ manpagesection(char *name) |
488 | } | | 490 | } |
489 | | | 491 | |
490 | static char * | | 492 | static char * |
491 | createsectionstring(char *section_id) | | 493 | createsectionstring(char *section_id) |
492 | { | | 494 | { |
493 | char *section; | | 495 | char *section; |
494 | | | 496 | |
495 | if (asprintf(§ion, " (%s) - ", section_id) < 0) | | 497 | if (asprintf(§ion, " (%s) - ", section_id) < 0) |
496 | err(EXIT_FAILURE, "malloc failed"); | | 498 | err(EXIT_FAILURE, "malloc failed"); |
497 | return section; | | 499 | return section; |
498 | } | | 500 | } |
499 | | | 501 | |
500 | static void | | 502 | static void |
501 | addmanpage(manpage **tree,ino_t inode,char *name, size_t sdoff, size_t sdlen) | | 503 | addmanpage(manpage **tree, ino_t inode, char *name, size_t sdoff, size_t sdlen) |
502 | { | | 504 | { |
503 | manpage *mp; | | 505 | manpage *mp; |
504 | | | 506 | |
505 | while ((mp = *tree) != NULL) { | | 507 | while ((mp = *tree) != NULL) { |
506 | if (mp->mp_inode == inode) | | 508 | if (mp->mp_inode == inode) |
507 | return; | | 509 | return; |
508 | tree = inode < mp->mp_inode ? &mp->mp_left : &mp->mp_right; | | 510 | tree = inode < mp->mp_inode ? &mp->mp_left : &mp->mp_right; |
509 | } | | 511 | } |
510 | | | 512 | |
511 | mp = emalloc(sizeof(manpage) + strlen(name)); | | 513 | mp = emalloc(sizeof(manpage) + strlen(name)); |
512 | mp->mp_left = NULL; | | 514 | mp->mp_left = NULL; |
513 | mp->mp_right = NULL; | | 515 | mp->mp_right = NULL; |
514 | mp->mp_inode = inode; | | 516 | mp->mp_inode = inode; |
| @@ -1105,27 +1107,27 @@ static void | | | @@ -1105,27 +1107,27 @@ static void |
1105 | processmanpages(manpage **source, whatis **dest) | | 1107 | processmanpages(manpage **source, whatis **dest) |
1106 | { | | 1108 | { |
1107 | manpage *mp; | | 1109 | manpage *mp; |
1108 | char sd[128]; | | 1110 | char sd[128]; |
1109 | | | 1111 | |
1110 | mp = *source; | | 1112 | mp = *source; |
1111 | *source = NULL; | | 1113 | *source = NULL; |
1112 | | | 1114 | |
1113 | while (mp != NULL) { | | 1115 | while (mp != NULL) { |
1114 | manpage *obsolete; | | 1116 | manpage *obsolete; |
1115 | char *data; | | 1117 | char *data; |
1116 | | | 1118 | |
1117 | if (mp->mp_left != NULL) | | 1119 | if (mp->mp_left != NULL) |
1118 | processmanpages(&mp->mp_left,dest); | | 1120 | processmanpages(&mp->mp_left, dest); |
1119 | | | 1121 | |
1120 | if ((data = getwhatisdata(mp->mp_name)) != NULL) { | | 1122 | if ((data = getwhatisdata(mp->mp_name)) != NULL) { |
1121 | /* Pass eventual directory prefix to addwhatis() */ | | 1123 | /* Pass eventual directory prefix to addwhatis() */ |
1122 | if (mp->mp_sdlen > 0 && mp->mp_sdlen < sizeof(sd)-1) | | 1124 | if (mp->mp_sdlen > 0 && mp->mp_sdlen < sizeof(sd)-1) |
1123 | strlcpy(sd, &mp->mp_name[mp->mp_sdoff], | | 1125 | strlcpy(sd, &mp->mp_name[mp->mp_sdoff], |
1124 | mp->mp_sdlen); | | 1126 | mp->mp_sdlen); |
1125 | else | | 1127 | else |
1126 | sd[0] = '\0'; | | 1128 | sd[0] = '\0'; |
1127 | | | 1129 | |
1128 | addwhatis(dest, data, sd); | | 1130 | addwhatis(dest, data, sd); |
1129 | } | | 1131 | } |
1130 | | | 1132 | |
1131 | obsolete = mp; | | 1133 | obsolete = mp; |