getNAME.c revision 1.9 1 /* $NetBSD: getNAME.c,v 1.9 1997/11/02 00:23:37 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #if 0
41 static char sccsid[] = "@(#)getNAME.c 8.1 (Berkeley) 6/30/93";
42 #else
43 __RCSID("$NetBSD: getNAME.c,v 1.9 1997/11/02 00:23:37 lukem Exp $");
44 #endif
45 #endif /* not lint */
46
47 /*
48 * Get name sections from manual pages.
49 * -t for building toc
50 * -i for building intro entries
51 * -w for querying type of manual source
52 * other apropos database
53 */
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57
58 int tocrc;
59 int intro;
60 int typeflag;
61
62 void doname __P((char *));
63 void dorefname __P((char *));
64 void getfrom __P((char *));
65 void split __P((char *, char *));
66 void trimln __P((char *));
67 void usage __P((void));
68 int main __P((int, char *[]));
69
70 int
71 main(argc, argv)
72 int argc;
73 char *argv[];
74 {
75 int ch;
76
77 while ((ch = getopt(argc, argv, "itw")) != -1)
78 switch (ch) {
79 case 'i':
80 intro = 1;
81 break;
82 case 't':
83 tocrc = 1;
84 break;
85 case 'w':
86 typeflag = 1;
87 break;
88 case '?':
89 default:
90 usage();
91 }
92 argc -= optind;
93 argv += optind;
94
95 if (!*argv)
96 usage();
97
98 for (; *argv; ++argv)
99 getfrom(*argv);
100 exit(0);
101 }
102
103 void
104 getfrom(pathname)
105 char *pathname;
106 {
107 int i = 0;
108 char *name, *loc, *s, *t;
109 char headbuf[BUFSIZ];
110 char linbuf[BUFSIZ];
111 char savebuf[BUFSIZ];
112
113 if (freopen(pathname, "r", stdin) == 0) {
114 perror(pathname);
115 return;
116 }
117 if ((name = strrchr(pathname, '/')))
118 name++;
119 else
120 name = pathname;
121 for (;;) {
122 if (fgets(headbuf, sizeof headbuf, stdin) == NULL) {
123 if (typeflag)
124 printf("%-60s UNKNOWN\n", pathname);
125 return;
126 }
127 if (headbuf[0] != '.')
128 continue;
129 if ((headbuf[1] == 'T' && headbuf[2] == 'H') ||
130 (headbuf[1] == 't' && headbuf[2] == 'h'))
131 break;
132 if (headbuf[1] == 'D' && headbuf[2] == 't')
133 goto newman;
134 }
135 if (typeflag) {
136 printf("%-60s OLD\n", pathname);
137 return;
138 }
139 for (;;) {
140 if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
141 return;
142 if (linbuf[0] != '.')
143 continue;
144 if (linbuf[1] == 'S' && linbuf[2] == 'H')
145 break;
146 if (linbuf[1] == 's' && linbuf[2] == 'h')
147 break;
148 }
149 trimln(headbuf);
150 if (tocrc)
151 doname(name);
152 linbuf[0] = '\0';
153 for (;;) {
154 if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
155 break;
156 if (headbuf[0] == '.') {
157 if (headbuf[1] == 'S' && headbuf[2] == 'H')
158 break;
159 if (headbuf[1] == 's' && headbuf[2] == 'h')
160 break;
161 }
162 if (i != 0)
163 strcat(linbuf, " ");
164 i++;
165 trimln(headbuf);
166 strcat(linbuf, headbuf);
167 /* change the \- into (N) - */
168 if ((s = strstr(linbuf, "\\-")) != NULL) {
169 strncpy(savebuf, s+1, BUFSIZ);
170 if ((t = strchr(name, '.')) != NULL) {
171 t++;
172 *s++ = '(';
173 while (*t)
174 *s++ = *t++;
175 *s++ = ')';
176 *s++ = ' ';
177 *s++ = '\0';
178 }
179 strcat(linbuf, savebuf);
180 }
181 }
182 if (intro)
183 split(linbuf, name);
184 else
185 printf("%s\n", linbuf);
186 return;
187
188 newman:
189 if (typeflag) {
190 printf("%-60s NEW\n", pathname);
191 return;
192 }
193 for (;;) {
194 if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
195 return;
196 if (linbuf[0] != '.')
197 continue;
198 if (linbuf[1] == 'S' && linbuf[2] == 'h')
199 break;
200 }
201 trimln(headbuf);
202 if (tocrc)
203 doname(name);
204 linbuf[0] = '\0';
205 for (;;) {
206 if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
207 break;
208 if (headbuf[0] == '.') {
209 if (headbuf[1] == 'S' && headbuf[2] == 'h')
210 break;
211 }
212 if (i != 0)
213 strcat(linbuf, " ");
214 i++;
215 trimln(headbuf);
216 for (loc = strchr(headbuf, ' '); loc; loc = strchr(loc, ' '))
217 if (loc[1] == ',')
218 strcpy(loc, &loc[1]);
219 else
220 loc++;
221 if (headbuf[0] != '.') {
222 strcat(linbuf, headbuf);
223 } else {
224 /*
225 * Get rid of quotes in macros.
226 */
227 for (loc = strchr(&headbuf[4], '"'); loc; ) {
228 strcpy(loc, &loc[1]);
229 loc = strchr(loc, '"');
230 }
231 /*
232 * Handle cross references
233 */
234 if (headbuf[1] == 'X' && headbuf[2] == 'r') {
235 for (loc = &headbuf[4]; *loc != ' '; loc++)
236 continue;
237 loc[0] = '(';
238 loc[2] = ')';
239 loc[3] = '\0';
240 }
241
242 /*
243 * Put section and dash between names and description.
244 */
245 if (headbuf[1] == 'N' && headbuf[2] == 'd') {
246 if ((t = strchr(name, '.')) != NULL) {
247 strcat(linbuf, "(");
248 strcat(linbuf, t+1);
249 strcat(linbuf, ") ");
250 }
251 strcat(linbuf, "- ");
252 }
253 /*
254 * Skip over macro names.
255 */
256 strcat(linbuf, &headbuf[4]);
257 }
258 }
259 if (intro)
260 split(linbuf, name);
261 else
262 printf("%s\n", linbuf);
263 }
264
265 void
266 trimln(cp)
267 char *cp;
268 {
269
270 while (*cp)
271 cp++;
272 if (*--cp == '\n')
273 *cp = 0;
274 }
275
276 void
277 doname(name)
278 char *name;
279 {
280 char *dp = name, *ep;
281
282 again:
283 while (*dp && *dp != '.')
284 putchar(*dp++);
285 if (*dp)
286 for (ep = dp+1; *ep; ep++)
287 if (*ep == '.') {
288 putchar(*dp++);
289 goto again;
290 }
291 putchar('(');
292 if (*dp)
293 dp++;
294 while (*dp)
295 putchar (*dp++);
296 putchar(')');
297 putchar(' ');
298 }
299
300 void
301 split(line, name)
302 char *line, *name;
303 {
304 char *cp, *dp;
305 char *sp, *sep;
306
307 cp = strchr(line, '-');
308 if (cp == 0)
309 return;
310 sp = cp + 1;
311 for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
312 ;
313 *++cp = '\0';
314 while (*sp && (*sp == ' ' || *sp == '\t'))
315 sp++;
316 for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
317 cp = strchr(dp, ',');
318 if (cp) {
319 char *tp;
320
321 for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
322 ;
323 *++tp = '\0';
324 for (++cp; *cp == ' ' || *cp == '\t'; cp++)
325 ;
326 }
327 printf("%s%s\t", sep, dp);
328 dorefname(name);
329 printf("\t%s", sp);
330 }
331 }
332
333 void
334 dorefname(name)
335 char *name;
336 {
337 char *dp = name, *ep;
338
339 again:
340 while (*dp && *dp != '.')
341 putchar(*dp++);
342 if (*dp)
343 for (ep = dp+1; *ep; ep++)
344 if (*ep == '.') {
345 putchar(*dp++);
346 goto again;
347 }
348 putchar('.');
349 if (*dp)
350 dp++;
351 while (*dp)
352 putchar (*dp++);
353 }
354
355 void
356 usage()
357 {
358 (void)fprintf(stderr, "usage: getNAME [-itw] file ...\n");
359 exit(1);
360 }
361