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