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