getNAME.c revision 1.20 1 1.20 christos /* $NetBSD: getNAME.c,v 1.20 2002/01/31 17:37:25 christos Exp $ */
2 1.4 mrg
3 1.1 cgd /*-
4 1.10 christos * Copyright (c) 1997, Christos Zoulas
5 1.3 tls * Copyright (c) 1980, 1993
6 1.3 tls * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.10 christos * This product includes software developed by Christos Zoulas.
21 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
22 1.1 cgd * may be used to endorse or promote products derived from this software
23 1.1 cgd * without specific prior written permission.
24 1.1 cgd *
25 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 cgd * SUCH DAMAGE.
36 1.1 cgd */
37 1.1 cgd
38 1.4 mrg #include <sys/cdefs.h>
39 1.1 cgd #ifndef lint
40 1.4 mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
41 1.4 mrg The Regents of the University of California. All rights reserved.\n");
42 1.3 tls #if 0
43 1.3 tls static char sccsid[] = "@(#)getNAME.c 8.1 (Berkeley) 6/30/93";
44 1.4 mrg #else
45 1.20 christos __RCSID("$NetBSD: getNAME.c,v 1.20 2002/01/31 17:37:25 christos Exp $");
46 1.3 tls #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * Get name sections from manual pages.
51 1.1 cgd * -t for building toc
52 1.1 cgd * -i for building intro entries
53 1.8 mrg * -w for querying type of manual source
54 1.14 christos * -v verbose
55 1.1 cgd * other apropos database
56 1.1 cgd */
57 1.11 perry #include <err.h>
58 1.14 christos #include <ctype.h>
59 1.1 cgd #include <stdio.h>
60 1.3 tls #include <stdlib.h>
61 1.1 cgd #include <string.h>
62 1.11 perry #include <unistd.h>
63 1.10 christos
64 1.10 christos static int tocrc;
65 1.10 christos static int intro;
66 1.10 christos static int typeflag;
67 1.14 christos static int verbose;
68 1.10 christos
69 1.10 christos #define SLOP 10 /* strlen(" () - ") < 10 */
70 1.10 christos
71 1.10 christos static char *linebuf = NULL;
72 1.10 christos static size_t maxlen = 0;
73 1.10 christos
74 1.10 christos
75 1.10 christos static void doname __P((char *));
76 1.10 christos static void dorefname __P((char *));
77 1.10 christos static void getfrom __P((char *));
78 1.10 christos static void oldman __P((char *, char *));
79 1.10 christos static void newman __P((char *, char *));
80 1.10 christos static void remcomma __P((char *, size_t *));
81 1.10 christos static void remquote __P((char *, size_t *));
82 1.10 christos static void fixxref __P((char *, size_t *));
83 1.10 christos static void split __P((char *, char *));
84 1.10 christos static void usage __P((void));
85 1.1 cgd
86 1.4 mrg int main __P((int, char *[]));
87 1.3 tls
88 1.16 augustss /* The .SH NAMEs that are allowed. */
89 1.16 augustss char *names[] = { "name", "namn", 0 };
90 1.16 augustss
91 1.3 tls int
92 1.1 cgd main(argc, argv)
93 1.1 cgd int argc;
94 1.3 tls char *argv[];
95 1.1 cgd {
96 1.1 cgd int ch;
97 1.1 cgd
98 1.14 christos while ((ch = getopt(argc, argv, "itvw")) != -1)
99 1.6 enami switch (ch) {
100 1.1 cgd case 'i':
101 1.1 cgd intro = 1;
102 1.1 cgd break;
103 1.1 cgd case 't':
104 1.1 cgd tocrc = 1;
105 1.1 cgd break;
106 1.14 christos case 'v':
107 1.14 christos verbose = 1;
108 1.14 christos break;
109 1.3 tls case 'w':
110 1.3 tls typeflag = 1;
111 1.3 tls break;
112 1.1 cgd case '?':
113 1.1 cgd default:
114 1.1 cgd usage();
115 1.1 cgd }
116 1.1 cgd argc -= optind;
117 1.1 cgd argv += optind;
118 1.1 cgd
119 1.1 cgd if (!*argv)
120 1.1 cgd usage();
121 1.1 cgd
122 1.1 cgd for (; *argv; ++argv)
123 1.1 cgd getfrom(*argv);
124 1.1 cgd exit(0);
125 1.1 cgd }
126 1.1 cgd
127 1.3 tls void
128 1.3 tls getfrom(pathname)
129 1.3 tls char *pathname;
130 1.1 cgd {
131 1.10 christos char *name;
132 1.10 christos char *line;
133 1.10 christos size_t len;
134 1.1 cgd
135 1.3 tls if (freopen(pathname, "r", stdin) == 0) {
136 1.10 christos warn("Cannot open `%s'", pathname);
137 1.1 cgd return;
138 1.1 cgd }
139 1.10 christos if ((name = strrchr(pathname, '/')) != NULL)
140 1.3 tls name++;
141 1.3 tls else
142 1.3 tls name = pathname;
143 1.1 cgd for (;;) {
144 1.10 christos if ((line = fgetln(stdin, &len)) == NULL) {
145 1.3 tls if (typeflag)
146 1.10 christos printf("%-60s\tUNKNOWN\n", pathname);
147 1.1 cgd return;
148 1.3 tls }
149 1.20 christos if (len < 3)
150 1.20 christos continue;
151 1.10 christos if (line[0] != '.')
152 1.1 cgd continue;
153 1.10 christos if ((line[1] == 'T' && line[2] == 'H') ||
154 1.10 christos (line[1] == 't' && line[2] == 'h'))
155 1.10 christos return oldman(pathname, name);
156 1.10 christos if (line[1] == 'D' && line[2] == 't')
157 1.10 christos return newman(pathname, name);
158 1.3 tls }
159 1.14 christos if (verbose)
160 1.14 christos warnx("missing .TH or .Dt section in `%s'", pathname);
161 1.10 christos }
162 1.10 christos
163 1.10 christos static void
164 1.10 christos oldman(pathname, name)
165 1.10 christos char *pathname, *name;
166 1.10 christos {
167 1.10 christos char *line, *ext, *s;
168 1.10 christos size_t len, i, extlen;
169 1.10 christos size_t curlen = 0;
170 1.10 christos
171 1.3 tls if (typeflag) {
172 1.10 christos printf("%-60s\tOLD\n", pathname);
173 1.3 tls return;
174 1.1 cgd }
175 1.1 cgd for (;;) {
176 1.14 christos if ((line = fgetln(stdin, &len)) == NULL) {
177 1.14 christos if (verbose)
178 1.14 christos warnx("missing .SH section in `%s'", pathname);
179 1.1 cgd return;
180 1.14 christos }
181 1.20 christos if (len < 4)
182 1.20 christos continue;
183 1.10 christos if (line[0] != '.')
184 1.1 cgd continue;
185 1.10 christos if (line[1] == 'S' && line[2] == 'H')
186 1.1 cgd break;
187 1.10 christos if (line[1] == 's' && line[2] == 'h')
188 1.1 cgd break;
189 1.1 cgd }
190 1.10 christos
191 1.14 christos for (s = &line[3]; s < &line[len] &&
192 1.14 christos (isspace((unsigned char) *s) || *s == '"' || *s == '\''); s++)
193 1.14 christos continue;
194 1.14 christos if (s == &line[len]) {
195 1.14 christos warnx("missing argument to .SH in `%s'", pathname);
196 1.14 christos return;
197 1.14 christos }
198 1.16 augustss for (i = 0; names[i]; i++)
199 1.16 augustss if (strncasecmp(s, names[i], strlen(names[i])) == 0)
200 1.16 augustss break;
201 1.16 augustss if (names[i] == NULL) {
202 1.14 christos warnx("first .SH section is not \"NAME\" in `%s'", pathname);
203 1.14 christos return;
204 1.14 christos }
205 1.14 christos
206 1.1 cgd if (tocrc)
207 1.1 cgd doname(name);
208 1.10 christos
209 1.10 christos for (i = 0;; i++) {
210 1.10 christos if ((line = fgetln(stdin, &len)) == NULL)
211 1.1 cgd break;
212 1.10 christos if (line[0] == '.') {
213 1.13 fair if (line[1] == '\\' && line[2] == '"')
214 1.13 fair continue; /* [nt]roff comment */
215 1.10 christos if (line[1] == 'S' && line[2] == 'H')
216 1.1 cgd break;
217 1.10 christos if (line[1] == 's' && line[2] == 'h')
218 1.12 mrg break;
219 1.12 mrg if (line[1] == 'P' && line[2] == 'P')
220 1.1 cgd break;
221 1.1 cgd }
222 1.10 christos if (line[len - 1] == '\n') {
223 1.10 christos line[len - 1] = '\0';
224 1.10 christos len--;
225 1.10 christos }
226 1.10 christos if ((ext = strrchr(name, '.')) != NULL) {
227 1.10 christos ext++;
228 1.10 christos extlen = strlen(ext);
229 1.10 christos }
230 1.10 christos else
231 1.10 christos extlen = 0;
232 1.10 christos
233 1.10 christos if (maxlen + extlen < curlen + len + SLOP) {
234 1.10 christos maxlen = 2 * (curlen + len) + SLOP + extlen;
235 1.10 christos if ((linebuf = realloc(linebuf, maxlen)) == NULL)
236 1.17 drochner err(1, NULL);
237 1.10 christos }
238 1.3 tls if (i != 0)
239 1.10 christos linebuf[curlen++] = ' ';
240 1.10 christos (void)memcpy(&linebuf[curlen], line, len);
241 1.10 christos curlen += len;
242 1.10 christos linebuf[curlen] = '\0';
243 1.15 hubertf
244 1.15 hubertf if(!tocrc && !intro) {
245 1.15 hubertf /* change the \- into (N) - */
246 1.15 hubertf if ((s = strstr(linebuf, "\\-")) != NULL) {
247 1.15 hubertf (void)memmove(s + extlen + 3, s + 1,
248 1.15 hubertf curlen - (s + 1 - linebuf));
249 1.15 hubertf curlen--;
250 1.15 hubertf if (extlen) {
251 1.15 hubertf *s++ = '(';
252 1.15 hubertf while (*ext)
253 1.15 hubertf *s++ = *ext++;
254 1.15 hubertf *s++ = ')';
255 1.15 hubertf *s++ = ' ';
256 1.15 hubertf curlen += extlen + 3;
257 1.15 hubertf }
258 1.15 hubertf linebuf[curlen] = '\0';
259 1.8 mrg }
260 1.8 mrg }
261 1.3 tls }
262 1.10 christos
263 1.3 tls if (intro)
264 1.10 christos split(linebuf, name);
265 1.3 tls else
266 1.10 christos printf("%s\n", linebuf);
267 1.3 tls return;
268 1.10 christos }
269 1.10 christos
270 1.10 christos static void
271 1.10 christos newman(pathname, name)
272 1.10 christos char *pathname, *name;
273 1.10 christos {
274 1.14 christos char *line, *ext, *s;
275 1.10 christos size_t len, i, extlen;
276 1.10 christos size_t curlen = 0;
277 1.3 tls
278 1.9 lukem if (typeflag) {
279 1.10 christos printf("%-60s\tNEW\n", pathname);
280 1.9 lukem return;
281 1.9 lukem }
282 1.3 tls for (;;) {
283 1.14 christos if ((line = fgetln(stdin, &len)) == NULL) {
284 1.14 christos if (verbose)
285 1.14 christos warnx("missing .Sh section in `%s'", pathname);
286 1.3 tls return;
287 1.14 christos }
288 1.10 christos if (line[0] != '.')
289 1.1 cgd continue;
290 1.10 christos if (line[1] == 'S' && line[2] == 'h')
291 1.3 tls break;
292 1.14 christos }
293 1.14 christos
294 1.14 christos for (s = &line[3]; s < &line[len] && isspace((unsigned char) *s); s++)
295 1.14 christos continue;
296 1.14 christos if (s == &line[len]) {
297 1.14 christos warnx("missing argument to .Sh in `%s'", pathname);
298 1.14 christos return;
299 1.14 christos }
300 1.16 augustss for (i = 0; names[i]; i++)
301 1.16 augustss if (strncasecmp(s, names[i], strlen(names[i])) == 0)
302 1.16 augustss break;
303 1.16 augustss if (names[i] == NULL) {
304 1.16 augustss warnx("first .SH section is not \"NAME\" in `%s'", pathname);
305 1.14 christos return;
306 1.3 tls }
307 1.10 christos
308 1.3 tls if (tocrc)
309 1.3 tls doname(name);
310 1.10 christos
311 1.10 christos for (i = 0;; i++) {
312 1.10 christos if ((line = fgetln(stdin, &len)) == NULL)
313 1.3 tls break;
314 1.10 christos
315 1.10 christos if (line[0] == '.') {
316 1.13 fair if (line[1] == '\\' && line[2] == '"')
317 1.13 fair continue; /* [nt]roff comment */
318 1.10 christos if (line[1] == 'S' && line[2] == 'h')
319 1.3 tls break;
320 1.1 cgd }
321 1.10 christos
322 1.10 christos if (line[len - 1] == '\n') {
323 1.10 christos line[len - 1] = '\0';
324 1.10 christos len--;
325 1.10 christos }
326 1.10 christos
327 1.10 christos if ((ext = strrchr(name, '.')) != NULL) {
328 1.10 christos ext++;
329 1.10 christos extlen = strlen(ext);
330 1.10 christos }
331 1.10 christos else
332 1.10 christos extlen = 0;
333 1.10 christos
334 1.10 christos if (maxlen + extlen < curlen + len + SLOP) {
335 1.10 christos maxlen = 2 * (curlen + len) + SLOP + extlen;
336 1.10 christos if ((linebuf = realloc(linebuf, maxlen)) == NULL)
337 1.17 drochner err(1, NULL);
338 1.10 christos }
339 1.10 christos
340 1.1 cgd if (i != 0)
341 1.10 christos linebuf[curlen++] = ' ';
342 1.10 christos
343 1.10 christos remcomma(line, &len);
344 1.10 christos
345 1.10 christos if (line[0] != '.') {
346 1.10 christos (void)memcpy(&linebuf[curlen], line, len);
347 1.10 christos curlen += len;
348 1.10 christos }
349 1.10 christos else {
350 1.10 christos remquote(line, &len);
351 1.10 christos fixxref(line, &len);
352 1.8 mrg
353 1.3 tls /*
354 1.8 mrg * Put section and dash between names and description.
355 1.3 tls */
356 1.10 christos if (line[1] == 'N' && line[2] == 'd') {
357 1.15 hubertf if(!tocrc && !intro) {
358 1.15 hubertf if (extlen) {
359 1.15 hubertf linebuf[curlen++] = '(';
360 1.15 hubertf while (*ext)
361 1.15 hubertf linebuf[curlen++] = *ext++;
362 1.15 hubertf linebuf[curlen++] = ')';
363 1.15 hubertf linebuf[curlen++] = ' ';
364 1.15 hubertf }
365 1.8 mrg }
366 1.10 christos linebuf[curlen++] = '-';
367 1.10 christos linebuf[curlen++] = ' ';
368 1.8 mrg }
369 1.3 tls /*
370 1.3 tls * Skip over macro names.
371 1.3 tls */
372 1.10 christos if (len <= 4)
373 1.10 christos continue;
374 1.10 christos (void)memcpy(&linebuf[curlen], &line[4], len - 4);
375 1.10 christos curlen += len - 4;
376 1.3 tls }
377 1.1 cgd }
378 1.10 christos linebuf[curlen] = '\0';
379 1.3 tls if (intro)
380 1.10 christos split(linebuf, name);
381 1.3 tls else
382 1.10 christos printf("%s\n", linebuf);
383 1.1 cgd }
384 1.1 cgd
385 1.10 christos /*
386 1.10 christos * convert " ," -> " "
387 1.10 christos */
388 1.10 christos static void
389 1.10 christos remcomma(line, len)
390 1.10 christos char *line;
391 1.10 christos size_t *len;
392 1.10 christos {
393 1.10 christos char *pline = line, *loc;
394 1.10 christos size_t plen = *len;
395 1.10 christos
396 1.10 christos while ((loc = memchr(pline, ' ', plen)) != NULL) {
397 1.10 christos plen -= loc - pline + 1;
398 1.10 christos pline = loc;
399 1.10 christos if (loc[1] == ',') {
400 1.10 christos (void)memcpy(loc, &loc[1], plen);
401 1.10 christos (*len)--;
402 1.10 christos }
403 1.10 christos else
404 1.10 christos pline++;
405 1.10 christos }
406 1.10 christos }
407 1.10 christos
408 1.10 christos /*
409 1.10 christos * Get rid of quotes in macros.
410 1.10 christos */
411 1.10 christos static
412 1.10 christos void remquote(line, len)
413 1.10 christos char *line;
414 1.10 christos size_t *len;
415 1.10 christos {
416 1.10 christos char *loc;
417 1.10 christos char *pline = &line[4];
418 1.10 christos size_t plen = *len - 4;
419 1.10 christos
420 1.10 christos if (*len < 4)
421 1.10 christos return;
422 1.10 christos
423 1.10 christos while ((loc = memchr(pline, '"', plen)) != NULL) {
424 1.10 christos plen -= loc - pline + 1;
425 1.10 christos pline = loc;
426 1.10 christos (void)memcpy(loc, &loc[1], plen);
427 1.10 christos (*len)--;
428 1.10 christos }
429 1.10 christos }
430 1.10 christos
431 1.10 christos /*
432 1.10 christos * Handle cross references
433 1.10 christos */
434 1.10 christos static void
435 1.10 christos fixxref(line, len)
436 1.10 christos char *line;
437 1.10 christos size_t *len;
438 1.1 cgd {
439 1.10 christos char *loc;
440 1.10 christos char *pline = &line[4];
441 1.10 christos size_t plen = *len - 4;
442 1.1 cgd
443 1.10 christos if (*len < 4)
444 1.10 christos return;
445 1.10 christos
446 1.10 christos if (line[1] == 'X' && line[2] == 'r') {
447 1.10 christos if ((loc = memchr(pline, ' ', plen)) != NULL) {
448 1.10 christos *loc++ = '(';
449 1.10 christos loc++;
450 1.10 christos *loc++ = ')';
451 1.10 christos *len = loc - line;
452 1.10 christos }
453 1.10 christos }
454 1.1 cgd }
455 1.1 cgd
456 1.10 christos static void
457 1.1 cgd doname(name)
458 1.1 cgd char *name;
459 1.1 cgd {
460 1.9 lukem char *dp = name, *ep;
461 1.1 cgd
462 1.1 cgd again:
463 1.1 cgd while (*dp && *dp != '.')
464 1.1 cgd putchar(*dp++);
465 1.1 cgd if (*dp)
466 1.1 cgd for (ep = dp+1; *ep; ep++)
467 1.1 cgd if (*ep == '.') {
468 1.1 cgd putchar(*dp++);
469 1.1 cgd goto again;
470 1.1 cgd }
471 1.1 cgd putchar('(');
472 1.1 cgd if (*dp)
473 1.1 cgd dp++;
474 1.1 cgd while (*dp)
475 1.1 cgd putchar (*dp++);
476 1.1 cgd putchar(')');
477 1.1 cgd putchar(' ');
478 1.1 cgd }
479 1.1 cgd
480 1.10 christos static void
481 1.1 cgd split(line, name)
482 1.1 cgd char *line, *name;
483 1.1 cgd {
484 1.9 lukem char *cp, *dp;
485 1.1 cgd char *sp, *sep;
486 1.1 cgd
487 1.3 tls cp = strchr(line, '-');
488 1.1 cgd if (cp == 0)
489 1.1 cgd return;
490 1.1 cgd sp = cp + 1;
491 1.1 cgd for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
492 1.1 cgd ;
493 1.1 cgd *++cp = '\0';
494 1.1 cgd while (*sp && (*sp == ' ' || *sp == '\t'))
495 1.1 cgd sp++;
496 1.1 cgd for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
497 1.3 tls cp = strchr(dp, ',');
498 1.1 cgd if (cp) {
499 1.9 lukem char *tp;
500 1.1 cgd
501 1.1 cgd for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
502 1.1 cgd ;
503 1.1 cgd *++tp = '\0';
504 1.1 cgd for (++cp; *cp == ' ' || *cp == '\t'; cp++)
505 1.1 cgd ;
506 1.1 cgd }
507 1.1 cgd printf("%s%s\t", sep, dp);
508 1.1 cgd dorefname(name);
509 1.15 hubertf printf("\t- %s", sp);
510 1.1 cgd }
511 1.15 hubertf putchar('\n');
512 1.1 cgd }
513 1.1 cgd
514 1.10 christos static void
515 1.1 cgd dorefname(name)
516 1.1 cgd char *name;
517 1.1 cgd {
518 1.9 lukem char *dp = name, *ep;
519 1.1 cgd
520 1.1 cgd again:
521 1.1 cgd while (*dp && *dp != '.')
522 1.1 cgd putchar(*dp++);
523 1.1 cgd if (*dp)
524 1.1 cgd for (ep = dp+1; *ep; ep++)
525 1.1 cgd if (*ep == '.') {
526 1.1 cgd putchar(*dp++);
527 1.1 cgd goto again;
528 1.1 cgd }
529 1.1 cgd putchar('.');
530 1.1 cgd if (*dp)
531 1.1 cgd dp++;
532 1.1 cgd while (*dp)
533 1.1 cgd putchar (*dp++);
534 1.1 cgd }
535 1.1 cgd
536 1.10 christos static void
537 1.1 cgd usage()
538 1.1 cgd {
539 1.19 cgd
540 1.19 cgd (void)fprintf(stderr, "Usage: %s [-itw] file ...\n", getprogname());
541 1.1 cgd exit(1);
542 1.1 cgd }
543