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