xref: /original-bsd/libexec/getNAME/getNAME.c (revision a91856c6)
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.4 (Berkeley) 01/20/91";
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 <string.h>
26 
27 int tocrc;
28 int intro;
29 
30 main(argc, argv)
31 	int argc;
32 	char **argv;
33 {
34 	extern int optind;
35 	int ch;
36 
37 	while ((ch = getopt(argc, argv, "it")) != EOF)
38 		switch(ch) {
39 		case 'i':
40 			intro = 1;
41 			break;
42 		case 't':
43 			tocrc = 1;
44 			break;
45 		case '?':
46 		default:
47 			usage();
48 		}
49 	argc -= optind;
50 	argv += optind;
51 
52 	if (!*argv)
53 		usage();
54 
55 	for (; *argv; ++argv)
56 		getfrom(*argv);
57 	exit(0);
58 }
59 
60 getfrom(name)
61 	char *name;
62 {
63 	int i = 0;
64 	char headbuf[BUFSIZ];
65 	char linbuf[BUFSIZ];
66 
67 	if (freopen(name, "r", stdin) == 0) {
68 		perror(name);
69 		return;
70 	}
71 	for (;;) {
72 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
73 			return;
74 		if (headbuf[0] != '.')
75 			continue;
76 		if (headbuf[1] == 'T' && headbuf[2] == 'H')
77 			break;
78 		if (headbuf[1] == 't' && headbuf[2] == 'h')
79 			break;
80 	}
81 	for (;;) {
82 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
83 			return;
84 		if (linbuf[0] != '.')
85 			continue;
86 		if (linbuf[1] == 'S' && linbuf[2] == 'H')
87 			break;
88 		if (linbuf[1] == 's' && linbuf[2] == 'h')
89 			break;
90 	}
91 	trimln(headbuf);
92 	if (tocrc)
93 		doname(name);
94 	if (!intro)
95 		printf("%s\t", headbuf);
96 	for (;;) {
97 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
98 			break;
99 		if (linbuf[0] == '.') {
100 			if (linbuf[1] == 'S' && linbuf[2] == 'H')
101 				break;
102 			if (linbuf[1] == 's' && linbuf[2] == 'h')
103 				break;
104 		}
105 		trimln(linbuf);
106 		if (intro) {
107 			split(linbuf, name);
108 			continue;
109 		}
110 		if (i != 0)
111 			printf(" ");
112 		i++;
113 		printf("%s", linbuf);
114 	}
115 	printf("\n");
116 }
117 
118 trimln(cp)
119 	register char *cp;
120 {
121 
122 	while (*cp)
123 		cp++;
124 	if (*--cp == '\n')
125 		*cp = 0;
126 }
127 
128 doname(name)
129 	char *name;
130 {
131 	register char *dp = name, *ep;
132 
133 again:
134 	while (*dp && *dp != '.')
135 		putchar(*dp++);
136 	if (*dp)
137 		for (ep = dp+1; *ep; ep++)
138 			if (*ep == '.') {
139 				putchar(*dp++);
140 				goto again;
141 			}
142 	putchar('(');
143 	if (*dp)
144 		dp++;
145 	while (*dp)
146 		putchar (*dp++);
147 	putchar(')');
148 	putchar(' ');
149 }
150 
151 split(line, name)
152 	char *line, *name;
153 {
154 	register char *cp, *dp;
155 	char *sp, *sep;
156 
157 	cp = index(line, '-');
158 	if (cp == 0)
159 		return;
160 	sp = cp + 1;
161 	for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
162 		;
163 	*++cp = '\0';
164 	while (*sp && (*sp == ' ' || *sp == '\t'))
165 		sp++;
166 	for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
167 		cp = index(dp, ',');
168 		if (cp) {
169 			register char *tp;
170 
171 			for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
172 				;
173 			*++tp = '\0';
174 			for (++cp; *cp == ' ' || *cp == '\t'; cp++)
175 				;
176 		}
177 		printf("%s%s\t", sep, dp);
178 		dorefname(name);
179 		printf("\t%s", sp);
180 	}
181 }
182 
183 dorefname(name)
184 	char *name;
185 {
186 	register char *dp = name, *ep;
187 
188 again:
189 	while (*dp && *dp != '.')
190 		putchar(*dp++);
191 	if (*dp)
192 		for (ep = dp+1; *ep; ep++)
193 			if (*ep == '.') {
194 				putchar(*dp++);
195 				goto again;
196 			}
197 	putchar('.');
198 	if (*dp)
199 		dp++;
200 	while (*dp)
201 		putchar (*dp++);
202 }
203 
204 usage()
205 {
206 	(void)fprintf(stderr, "usage: getNAME [-it] file ...\n");
207 	exit(1);
208 }
209