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