getNAME.c revision 1.8 1 1.8 mrg /* $NetBSD: getNAME.c,v 1.8 1997/11/01 15:03:54 mrg 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.8 mrg __RCSID("$NetBSD: getNAME.c,v 1.8 1997/11/01 15:03:54 mrg 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.8 mrg * -w for querying type of manual source
52 1.1 cgd * other apropos database
53 1.1 cgd */
54 1.1 cgd #include <stdio.h>
55 1.3 tls #include <stdlib.h>
56 1.1 cgd #include <string.h>
57 1.1 cgd
58 1.1 cgd int tocrc;
59 1.1 cgd int intro;
60 1.3 tls int typeflag;
61 1.1 cgd
62 1.3 tls void doname __P((char *));
63 1.3 tls void dorefname __P((char *));
64 1.3 tls void getfrom __P((char *));
65 1.3 tls void split __P((char *, char *));
66 1.3 tls void trimln __P((char *));
67 1.3 tls void usage __P((void));
68 1.4 mrg int main __P((int, char *[]));
69 1.3 tls
70 1.3 tls int
71 1.1 cgd main(argc, argv)
72 1.1 cgd int argc;
73 1.3 tls char *argv[];
74 1.1 cgd {
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.8 mrg char *name, *loc, *s, *t;
109 1.1 cgd char headbuf[BUFSIZ];
110 1.1 cgd char linbuf[BUFSIZ];
111 1.8 mrg char savebuf[BUFSIZ];
112 1.1 cgd
113 1.3 tls if (freopen(pathname, "r", stdin) == 0) {
114 1.3 tls perror(pathname);
115 1.1 cgd return;
116 1.1 cgd }
117 1.4 mrg if ((name = strrchr(pathname, '/')))
118 1.3 tls name++;
119 1.3 tls else
120 1.3 tls name = pathname;
121 1.1 cgd for (;;) {
122 1.3 tls if (fgets(headbuf, sizeof headbuf, stdin) == NULL) {
123 1.3 tls if (typeflag)
124 1.3 tls printf("%-60s UNKNOWN\n", pathname);
125 1.1 cgd return;
126 1.3 tls }
127 1.1 cgd if (headbuf[0] != '.')
128 1.1 cgd continue;
129 1.3 tls if ((headbuf[1] == 'T' && headbuf[2] == 'H') ||
130 1.3 tls (headbuf[1] == 't' && headbuf[2] == 'h'))
131 1.1 cgd break;
132 1.3 tls if (headbuf[1] == 'D' && headbuf[2] == 't') {
133 1.3 tls if (typeflag) {
134 1.3 tls printf("%-60s NEW\n", pathname);
135 1.3 tls return;
136 1.3 tls }
137 1.3 tls goto newman;
138 1.3 tls }
139 1.3 tls }
140 1.3 tls if (typeflag) {
141 1.3 tls printf("%-60s OLD\n", pathname);
142 1.3 tls return;
143 1.1 cgd }
144 1.1 cgd for (;;) {
145 1.1 cgd if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
146 1.1 cgd return;
147 1.1 cgd if (linbuf[0] != '.')
148 1.1 cgd continue;
149 1.1 cgd if (linbuf[1] == 'S' && linbuf[2] == 'H')
150 1.1 cgd break;
151 1.1 cgd if (linbuf[1] == 's' && linbuf[2] == 'h')
152 1.1 cgd break;
153 1.1 cgd }
154 1.1 cgd trimln(headbuf);
155 1.1 cgd if (tocrc)
156 1.1 cgd doname(name);
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.8 mrg /* change the \- into (N) - */
173 1.8 mrg if ((s = strstr(linbuf, "\\-")) != NULL) {
174 1.8 mrg strncpy(savebuf, s+1, BUFSIZ);
175 1.8 mrg if ((t = strchr(name, '.')) != NULL) {
176 1.8 mrg t++;
177 1.8 mrg *s++ = '(';
178 1.8 mrg while (*t)
179 1.8 mrg *s++ = *t++;
180 1.8 mrg *s++ = ')';
181 1.8 mrg *s++ = ' ';
182 1.8 mrg *s++ = '\0';
183 1.8 mrg }
184 1.8 mrg strcat(linbuf, savebuf);
185 1.8 mrg }
186 1.3 tls }
187 1.3 tls if (intro)
188 1.3 tls split(linbuf, name);
189 1.3 tls else
190 1.3 tls printf("%s\n", linbuf);
191 1.3 tls return;
192 1.3 tls
193 1.3 tls newman:
194 1.3 tls for (;;) {
195 1.3 tls if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
196 1.3 tls return;
197 1.3 tls if (linbuf[0] != '.')
198 1.1 cgd continue;
199 1.3 tls if (linbuf[1] == 'S' && linbuf[2] == 'h')
200 1.3 tls break;
201 1.3 tls }
202 1.3 tls trimln(headbuf);
203 1.3 tls if (tocrc)
204 1.3 tls doname(name);
205 1.3 tls linbuf[0] = '\0';
206 1.3 tls for (;;) {
207 1.3 tls if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
208 1.3 tls break;
209 1.3 tls if (headbuf[0] == '.') {
210 1.3 tls if (headbuf[1] == 'S' && headbuf[2] == 'h')
211 1.3 tls break;
212 1.1 cgd }
213 1.1 cgd if (i != 0)
214 1.3 tls strcat(linbuf, " ");
215 1.1 cgd i++;
216 1.3 tls trimln(headbuf);
217 1.3 tls for (loc = strchr(headbuf, ' '); loc; loc = strchr(loc, ' '))
218 1.3 tls if (loc[1] == ',')
219 1.3 tls strcpy(loc, &loc[1]);
220 1.3 tls else
221 1.3 tls loc++;
222 1.3 tls if (headbuf[0] != '.') {
223 1.3 tls strcat(linbuf, headbuf);
224 1.3 tls } else {
225 1.3 tls /*
226 1.3 tls * Get rid of quotes in macros.
227 1.3 tls */
228 1.3 tls for (loc = strchr(&headbuf[4], '"'); loc; ) {
229 1.3 tls strcpy(loc, &loc[1]);
230 1.3 tls loc = strchr(loc, '"');
231 1.3 tls }
232 1.3 tls /*
233 1.3 tls * Handle cross references
234 1.3 tls */
235 1.3 tls if (headbuf[1] == 'X' && headbuf[2] == 'r') {
236 1.3 tls for (loc = &headbuf[4]; *loc != ' '; loc++)
237 1.3 tls continue;
238 1.3 tls loc[0] = '(';
239 1.3 tls loc[2] = ')';
240 1.3 tls loc[3] = '\0';
241 1.3 tls }
242 1.8 mrg
243 1.3 tls /*
244 1.8 mrg * Put section and dash between names and description.
245 1.3 tls */
246 1.8 mrg if (headbuf[1] == 'N' && headbuf[2] == 'd') {
247 1.8 mrg if ((t = strchr(name, '.')) != NULL) {
248 1.8 mrg strcat(linbuf, "(");
249 1.8 mrg strcat(linbuf, t+1);
250 1.8 mrg strcat(linbuf, ") ");
251 1.8 mrg }
252 1.8 mrg strcat(linbuf, "- ");
253 1.8 mrg }
254 1.3 tls /*
255 1.3 tls * Skip over macro names.
256 1.3 tls */
257 1.3 tls strcat(linbuf, &headbuf[4]);
258 1.3 tls }
259 1.1 cgd }
260 1.3 tls if (intro)
261 1.3 tls split(linbuf, name);
262 1.3 tls else
263 1.3 tls printf("%s\n", linbuf);
264 1.1 cgd }
265 1.1 cgd
266 1.3 tls void
267 1.1 cgd trimln(cp)
268 1.1 cgd register char *cp;
269 1.1 cgd {
270 1.1 cgd
271 1.1 cgd while (*cp)
272 1.1 cgd cp++;
273 1.1 cgd if (*--cp == '\n')
274 1.1 cgd *cp = 0;
275 1.1 cgd }
276 1.1 cgd
277 1.3 tls void
278 1.1 cgd doname(name)
279 1.1 cgd char *name;
280 1.1 cgd {
281 1.1 cgd register char *dp = name, *ep;
282 1.1 cgd
283 1.1 cgd again:
284 1.1 cgd while (*dp && *dp != '.')
285 1.1 cgd putchar(*dp++);
286 1.1 cgd if (*dp)
287 1.1 cgd for (ep = dp+1; *ep; ep++)
288 1.1 cgd if (*ep == '.') {
289 1.1 cgd putchar(*dp++);
290 1.1 cgd goto again;
291 1.1 cgd }
292 1.1 cgd putchar('(');
293 1.1 cgd if (*dp)
294 1.1 cgd dp++;
295 1.1 cgd while (*dp)
296 1.1 cgd putchar (*dp++);
297 1.1 cgd putchar(')');
298 1.1 cgd putchar(' ');
299 1.1 cgd }
300 1.1 cgd
301 1.3 tls void
302 1.1 cgd split(line, name)
303 1.1 cgd char *line, *name;
304 1.1 cgd {
305 1.1 cgd register char *cp, *dp;
306 1.1 cgd char *sp, *sep;
307 1.1 cgd
308 1.3 tls cp = strchr(line, '-');
309 1.1 cgd if (cp == 0)
310 1.1 cgd return;
311 1.1 cgd sp = cp + 1;
312 1.1 cgd for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
313 1.1 cgd ;
314 1.1 cgd *++cp = '\0';
315 1.1 cgd while (*sp && (*sp == ' ' || *sp == '\t'))
316 1.1 cgd sp++;
317 1.1 cgd for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
318 1.3 tls cp = strchr(dp, ',');
319 1.1 cgd if (cp) {
320 1.1 cgd register char *tp;
321 1.1 cgd
322 1.1 cgd for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
323 1.1 cgd ;
324 1.1 cgd *++tp = '\0';
325 1.1 cgd for (++cp; *cp == ' ' || *cp == '\t'; cp++)
326 1.1 cgd ;
327 1.1 cgd }
328 1.1 cgd printf("%s%s\t", sep, dp);
329 1.1 cgd dorefname(name);
330 1.1 cgd printf("\t%s", sp);
331 1.1 cgd }
332 1.1 cgd }
333 1.1 cgd
334 1.3 tls void
335 1.1 cgd dorefname(name)
336 1.1 cgd char *name;
337 1.1 cgd {
338 1.1 cgd register char *dp = name, *ep;
339 1.1 cgd
340 1.1 cgd again:
341 1.1 cgd while (*dp && *dp != '.')
342 1.1 cgd putchar(*dp++);
343 1.1 cgd if (*dp)
344 1.1 cgd for (ep = dp+1; *ep; ep++)
345 1.1 cgd if (*ep == '.') {
346 1.1 cgd putchar(*dp++);
347 1.1 cgd goto again;
348 1.1 cgd }
349 1.1 cgd putchar('.');
350 1.1 cgd if (*dp)
351 1.1 cgd dp++;
352 1.1 cgd while (*dp)
353 1.1 cgd putchar (*dp++);
354 1.1 cgd }
355 1.1 cgd
356 1.3 tls void
357 1.1 cgd usage()
358 1.1 cgd {
359 1.1 cgd (void)fprintf(stderr, "usage: getNAME [-it] file ...\n");
360 1.1 cgd exit(1);
361 1.1 cgd }
362