Merge from vendor branch BINUTILS:
[dragonfly.git] / libexec / getNAME / getNAME.c
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  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)getNAME.c        8.1 (Berkeley) 6/30/93
35  * $FreeBSD: src/libexec/getNAME/getNAME.c,v 1.7 1999/08/28 00:09:33 peter Exp $
36  * $DragonFly: src/libexec/getNAME/Attic/getNAME.c,v 1.3 2003/11/14 03:54:30 dillon Exp $
37  */
38
39 /*
40  * Get name sections from manual pages.
41  *      -t      for building toc
42  *      -i      for building intro entries
43  *      other   apropos database
44  */
45 #include <err.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 int tocrc;
52 int intro;
53 int typeflag;
54
55 void doname (char *);
56 void dorefname (char *);
57 void getfrom (char *);
58 void split (char *, char *);
59 void trimln (char *);
60 static void usage (void);
61
62 int
63 main(argc, argv)
64         int argc;
65         char *argv[];
66 {
67         int ch;
68
69         while ((ch = getopt(argc, argv, "itw")) != -1)
70                 switch(ch) {
71                 case 'i':
72                         intro = 1;
73                         break;
74                 case 't':
75                         tocrc = 1;
76                         break;
77                 case 'w':
78                         typeflag = 1;
79                         break;
80                 default:
81                         usage();
82                 }
83         argc -= optind;
84         argv += optind;
85
86         if (!*argv)
87                 usage();
88
89         for (; *argv; ++argv)
90                 getfrom(*argv);
91         exit(0);
92 }
93
94 void
95 getfrom(pathname)
96         char *pathname;
97 {
98         int i = 0;
99         char *name, *loc;
100         char headbuf[BUFSIZ];
101         char linbuf[BUFSIZ];
102
103         if (freopen(pathname, "r", stdin) == 0) {
104                 warn("%s", pathname);
105                 return;
106         }
107         if ((name = strrchr(pathname, '/')))
108                 name++;
109         else
110                 name = pathname;
111         for (;;) {
112                 if (fgets(headbuf, sizeof headbuf, stdin) == NULL) {
113                         if (typeflag)
114                                 printf("%-60s   UNKNOWN\n", pathname);
115                         return;
116                 }
117                 if (headbuf[0] != '.')
118                         continue;
119                 if ((headbuf[1] == 'T' && headbuf[2] == 'H') ||
120                     (headbuf[1] == 't' && headbuf[2] == 'h'))
121                         break;
122                 if (headbuf[1] == 'D' && headbuf[2] == 't') {
123                         if (typeflag) {
124                                 printf("%-60s   NEW\n", pathname);
125                                 return;
126                         }
127                         goto newman;
128                 }
129         }
130         if (typeflag) {
131                 printf("%-60s   OLD\n", pathname);
132                 return;
133         }
134         for (;;) {
135                 if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
136                         return;
137                 if (linbuf[0] != '.')
138                         continue;
139                 if (linbuf[1] == 'S' && linbuf[2] == 'H')
140                         break;
141                 if (linbuf[1] == 's' && linbuf[2] == 'h')
142                         break;
143         }
144         trimln(headbuf);
145         if (tocrc)
146                 doname(name);
147         if (!tocrc && !intro)
148                 printf("%s\t", headbuf);
149         linbuf[0] = '\0';
150         for (;;) {
151                 if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
152                         break;
153                 if (headbuf[0] == '.') {
154                         if (headbuf[1] == 'S' && headbuf[2] == 'H')
155                                 break;
156                         if (headbuf[1] == 's' && headbuf[2] == 'h')
157                                 break;
158                 }
159                 if (i != 0)
160                         strcat(linbuf, " ");
161                 i++;
162                 trimln(headbuf);
163                 strcat(linbuf, headbuf);
164         }
165         if (intro)
166                 split(linbuf, name);
167         else
168                 printf("%s\n", linbuf);
169         return;
170
171 newman:
172         for (;;) {
173                 if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
174                         return;
175                 if (linbuf[0] != '.')
176                         continue;
177                 if (linbuf[1] == 'S' && linbuf[2] == 'h')
178                         break;
179         }
180         trimln(headbuf);
181         if (tocrc)
182                 doname(name);
183         if (!tocrc && !intro)
184                 printf(".TH%s\t", &headbuf[3]);
185         linbuf[0] = '\0';
186         for (;;) {
187                 if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
188                         break;
189                 if (headbuf[0] == '.') {
190                         if (headbuf[1] == 'S' && headbuf[2] == 'h')
191                                 break;
192                 }
193                 if (i != 0)
194                         strcat(linbuf, " ");
195                 i++;
196                 trimln(headbuf);
197                 for (loc = strchr(headbuf, ' '); loc; loc = strchr(loc, ' '))
198                         if (loc[1] == ',')
199                                 strcpy(loc, &loc[1]);
200                         else
201                                 loc++;
202                 if (headbuf[0] != '.') {
203                         strcat(linbuf, headbuf);
204                 } else {
205                         /*
206                          * Get rid of quotes in macros.
207                          */
208                         for (loc = strchr(&headbuf[4], '"'); loc; ) {
209                                 strcpy(loc, &loc[1]);
210                                 loc = strchr(loc, '"');
211                         }
212                         /*
213                          * Handle cross references
214                          */
215                         if (headbuf[1] == 'X' && headbuf[2] == 'r') {
216                                 for (loc = &headbuf[4]; *loc != ' '; loc++)
217                                         continue;
218                                 loc[0] = '(';
219                                 loc[2] = ')';
220                                 loc[3] = '\0';
221                         }
222                         /*
223                          * Put dash between names and description.
224                          */
225                         if (headbuf[1] == 'N' && headbuf[2] == 'd')
226                                 strcat(linbuf, "\\- ");
227                         /*
228                          * Skip over macro names.
229                          */
230                         strcat(linbuf, &headbuf[4]);
231                 }
232         }
233         if (intro)
234                 split(linbuf, name);
235         else
236                 printf("%s\n", linbuf);
237 }
238
239 void
240 trimln(cp)
241         register char *cp;
242 {
243
244         while (*cp)
245                 cp++;
246         if (*--cp == '\n')
247                 *cp = 0;
248 }
249
250 void
251 doname(name)
252         char *name;
253 {
254         register char *dp = name, *ep;
255
256 again:
257         while (*dp && *dp != '.')
258                 putchar(*dp++);
259         if (*dp)
260                 for (ep = dp+1; *ep; ep++)
261                         if (*ep == '.') {
262                                 putchar(*dp++);
263                                 goto again;
264                         }
265         putchar('(');
266         if (*dp)
267                 dp++;
268         while (*dp)
269                 putchar (*dp++);
270         putchar(')');
271         putchar(' ');
272 }
273
274 void
275 split(line, name)
276         char *line, *name;
277 {
278         register char *cp, *dp;
279         char *sp, *sep;
280
281         cp = strchr(line, '-');
282         if (cp == 0)
283                 return;
284         sp = cp + 1;
285         for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
286                 ;
287         *++cp = '\0';
288         while (*sp && (*sp == ' ' || *sp == '\t'))
289                 sp++;
290         for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
291                 cp = strchr(dp, ',');
292                 if (cp) {
293                         register char *tp;
294
295                         for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
296                                 ;
297                         *++tp = '\0';
298                         for (++cp; *cp == ' ' || *cp == '\t'; cp++)
299                                 ;
300                 }
301                 printf("%s%s\t", sep, dp);
302                 dorefname(name);
303                 printf("\t%s", sp);
304         }
305 }
306
307 void
308 dorefname(name)
309         char *name;
310 {
311         register char *dp = name, *ep;
312
313 again:
314         while (*dp && *dp != '.')
315                 putchar(*dp++);
316         if (*dp)
317                 for (ep = dp+1; *ep; ep++)
318                         if (*ep == '.') {
319                                 putchar(*dp++);
320                                 goto again;
321                         }
322         putchar('.');
323         if (*dp)
324                 dp++;
325         while (*dp)
326                 putchar (*dp++);
327 }
328
329 static void
330 usage()
331 {
332         (void)fprintf(stderr, "usage: getNAME [-itw] file ...\n");
333         exit(1);
334 }