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