xref: /csrg-svn/libexec/getNAME/getNAME.c (revision 19818)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)getNAME.c	5.1 (Berkeley) 04/30/85";
9 #endif not lint
10 
11 /*
12  * Get name sections from manual pages.
13  *	-t	for building toc
14  *	-i	for building intro entries
15  *	other	apropos database
16  */
17 #include <strings.h>
18 #include <stdio.h>
19 
20 int tocrc;
21 int intro;
22 
23 main(argc, argv)
24 	int argc;
25 	char *argv[];
26 {
27 
28 	argc--, argv++;
29 	if (!strcmp(argv[0], "-t"))
30 		argc--, argv++, tocrc++;
31 	if (!strcmp(argv[0], "-i"))
32 		argc--, argv++, intro++;
33 	while (argc > 0)
34 		getfrom(*argv++), argc--;
35 	exit(0);
36 }
37 
38 getfrom(name)
39 	char *name;
40 {
41 	char headbuf[BUFSIZ];
42 	char linbuf[BUFSIZ];
43 	register char *cp;
44 	int i = 0;
45 
46 	if (freopen(name, "r", stdin) == 0) {
47 		perror(name);
48 		exit(1);
49 	}
50 	for (;;) {
51 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
52 			return;
53 		if (headbuf[0] != '.')
54 			continue;
55 		if (headbuf[1] == 'T' && headbuf[2] == 'H')
56 			break;
57 		if (headbuf[1] == 't' && headbuf[2] == 'h')
58 			break;
59 	}
60 	for (;;) {
61 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
62 			return;
63 		if (linbuf[0] != '.')
64 			continue;
65 		if (linbuf[1] == 'S' && linbuf[2] == 'H')
66 			break;
67 		if (linbuf[1] == 's' && linbuf[2] == 'h')
68 			break;
69 	}
70 	trimln(headbuf);
71 	if (tocrc)
72 		doname(name);
73 	if (!intro)
74 		printf("%s\t", headbuf);
75 	for (;;) {
76 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
77 			break;
78 		if (linbuf[0] == '.') {
79 			if (linbuf[1] == 'S' && linbuf[2] == 'H')
80 				break;
81 			if (linbuf[1] == 's' && linbuf[2] == 'h')
82 				break;
83 		}
84 		trimln(linbuf);
85 		if (intro) {
86 			split(linbuf, name);
87 			continue;
88 		}
89 		if (i != 0)
90 			printf(" ");
91 		i++;
92 		printf("%s", linbuf);
93 	}
94 	printf("\n");
95 }
96 
97 trimln(cp)
98 	register char *cp;
99 {
100 
101 	while (*cp)
102 		cp++;
103 	if (*--cp == '\n')
104 		*cp = 0;
105 }
106 
107 doname(name)
108 	char *name;
109 {
110 	register char *dp = name, *ep;
111 
112 again:
113 	while (*dp && *dp != '.')
114 		putchar(*dp++);
115 	if (*dp)
116 		for (ep = dp+1; *ep; ep++)
117 			if (*ep == '.') {
118 				putchar(*dp++);
119 				goto again;
120 			}
121 	putchar('(');
122 	if (*dp)
123 		dp++;
124 	while (*dp)
125 		putchar (*dp++);
126 	putchar(')');
127 	putchar(' ');
128 }
129 
130 split(line, name)
131 	char *line, *name;
132 {
133 	register char *cp, *dp;
134 	char *sp, *sep;
135 
136 	cp = index(line, '-');
137 	if (cp == 0)
138 		return;
139 	sp = cp + 1;
140 	for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
141 		;
142 	*++cp = '\0';
143 	while (*sp && (*sp == ' ' || *sp == '\t'))
144 		sp++;
145 	for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
146 		cp = index(dp, ',');
147 		if (cp) {
148 			register char *tp;
149 
150 			for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
151 				;
152 			*++tp = '\0';
153 			for (++cp; *cp == ' ' || *cp == '\t'; cp++)
154 				;
155 		}
156 		printf("%s%s\t", sep, dp);
157 		dorefname(name);
158 		printf("\t%s", sp);
159 	}
160 }
161 
162 dorefname(name)
163 	char *name;
164 {
165 	register char *dp = name, *ep;
166 
167 again:
168 	while (*dp && *dp != '.')
169 		putchar(*dp++);
170 	if (*dp)
171 		for (ep = dp+1; *ep; ep++)
172 			if (*ep == '.') {
173 				putchar(*dp++);
174 				goto again;
175 			}
176 	putchar('.');
177 	if (*dp)
178 		dp++;
179 	while (*dp)
180 		putchar (*dp++);
181 }
182