xref: /original-bsd/libexec/getNAME/getNAME.c (revision 1b4ef7de)
1 /*-
2  * Copyright (c) 1980 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)getNAME.c	5.5 (Berkeley) 05/17/93";
16 #endif /* not lint */
17 
18 /*
19  * Get name sections from manual pages.
20  *	-t	for building toc
21  *	-i	for building intro entries
22  *	other	apropos database
23  */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 int tocrc;
29 int intro;
30 
31 void doname __P((char *));
32 void dorefname __P((char *));
33 void getfrom __P((char *));
34 void split __P((char *, char *));
35 void trimln __P((char *));
36 void usage __P((void));
37 
38 int
39 main(argc, argv)
40 	int argc;
41 	char *argv[];
42 {
43 	extern int optind;
44 	int ch;
45 
46 	while ((ch = getopt(argc, argv, "it")) != EOF)
47 		switch(ch) {
48 		case 'i':
49 			intro = 1;
50 			break;
51 		case 't':
52 			tocrc = 1;
53 			break;
54 		case '?':
55 		default:
56 			usage();
57 		}
58 	argc -= optind;
59 	argv += optind;
60 
61 	if (!*argv)
62 		usage();
63 
64 	for (; *argv; ++argv)
65 		getfrom(*argv);
66 	exit(0);
67 }
68 
69 void
70 getfrom(name)
71 	char *name;
72 {
73 	int i = 0;
74 	char headbuf[BUFSIZ];
75 	char linbuf[BUFSIZ];
76 
77 	if (freopen(name, "r", stdin) == 0) {
78 		perror(name);
79 		return;
80 	}
81 	for (;;) {
82 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
83 			return;
84 		if (headbuf[0] != '.')
85 			continue;
86 		if (headbuf[1] == 'T' && headbuf[2] == 'H')
87 			break;
88 		if (headbuf[1] == 't' && headbuf[2] == 'h')
89 			break;
90 	}
91 	for (;;) {
92 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
93 			return;
94 		if (linbuf[0] != '.')
95 			continue;
96 		if (linbuf[1] == 'S' && linbuf[2] == 'H')
97 			break;
98 		if (linbuf[1] == 's' && linbuf[2] == 'h')
99 			break;
100 	}
101 	trimln(headbuf);
102 	if (tocrc)
103 		doname(name);
104 	if (!intro)
105 		printf("%s\t", headbuf);
106 	for (;;) {
107 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
108 			break;
109 		if (linbuf[0] == '.') {
110 			if (linbuf[1] == 'S' && linbuf[2] == 'H')
111 				break;
112 			if (linbuf[1] == 's' && linbuf[2] == 'h')
113 				break;
114 		}
115 		trimln(linbuf);
116 		if (intro) {
117 			split(linbuf, name);
118 			continue;
119 		}
120 		if (i != 0)
121 			printf(" ");
122 		i++;
123 		printf("%s", linbuf);
124 	}
125 	printf("\n");
126 }
127 
128 void
129 trimln(cp)
130 	register char *cp;
131 {
132 
133 	while (*cp)
134 		cp++;
135 	if (*--cp == '\n')
136 		*cp = 0;
137 }
138 
139 void
140 doname(name)
141 	char *name;
142 {
143 	register char *dp = name, *ep;
144 
145 again:
146 	while (*dp && *dp != '.')
147 		putchar(*dp++);
148 	if (*dp)
149 		for (ep = dp+1; *ep; ep++)
150 			if (*ep == '.') {
151 				putchar(*dp++);
152 				goto again;
153 			}
154 	putchar('(');
155 	if (*dp)
156 		dp++;
157 	while (*dp)
158 		putchar (*dp++);
159 	putchar(')');
160 	putchar(' ');
161 }
162 
163 void
164 split(line, name)
165 	char *line, *name;
166 {
167 	register char *cp, *dp;
168 	char *sp, *sep;
169 
170 	cp = strchr(line, '-');
171 	if (cp == 0)
172 		return;
173 	sp = cp + 1;
174 	for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
175 		;
176 	*++cp = '\0';
177 	while (*sp && (*sp == ' ' || *sp == '\t'))
178 		sp++;
179 	for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
180 		cp = strchr(dp, ',');
181 		if (cp) {
182 			register char *tp;
183 
184 			for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
185 				;
186 			*++tp = '\0';
187 			for (++cp; *cp == ' ' || *cp == '\t'; cp++)
188 				;
189 		}
190 		printf("%s%s\t", sep, dp);
191 		dorefname(name);
192 		printf("\t%s", sp);
193 	}
194 }
195 
196 void
197 dorefname(name)
198 	char *name;
199 {
200 	register char *dp = name, *ep;
201 
202 again:
203 	while (*dp && *dp != '.')
204 		putchar(*dp++);
205 	if (*dp)
206 		for (ep = dp+1; *ep; ep++)
207 			if (*ep == '.') {
208 				putchar(*dp++);
209 				goto again;
210 			}
211 	putchar('.');
212 	if (*dp)
213 		dp++;
214 	while (*dp)
215 		putchar (*dp++);
216 }
217 
218 void
219 usage()
220 {
221 	(void)fprintf(stderr, "usage: getNAME [-it] file ...\n");
222 	exit(1);
223 }
224