written at Berkeley, use BSD copyright; lint, getopt(3)
[unix-history] / usr / src / libexec / getNAME / getNAME.c
CommitLineData
8b287fa3
KB
1/*-
2 * Copyright (c) 1980 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * %sccs.include.redist.c%
051b1e55
DF
6 */
7
12bd8a08 8#ifndef lint
8b287fa3
KB
9char 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
15static char sccsid[] = "@(#)getNAME.c 5.4 (Berkeley) %G%";
16#endif /* not lint */
12bd8a08
SL
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 */
add17efb 24#include <stdio.h>
6ebcb998 25#include <string.h>
add17efb
BJ
26
27int tocrc;
12bd8a08 28int intro;
add17efb
BJ
29
30main(argc, argv)
31 int argc;
8b287fa3 32 char **argv;
add17efb 33{
8b287fa3
KB
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;
add17efb 51
8b287fa3
KB
52 if (!*argv)
53 usage();
54
55 for (; *argv; ++argv)
56 getfrom(*argv);
add17efb
BJ
57 exit(0);
58}
59
60getfrom(name)
61 char *name;
62{
8b287fa3 63 int i = 0;
add17efb
BJ
64 char headbuf[BUFSIZ];
65 char linbuf[BUFSIZ];
add17efb
BJ
66
67 if (freopen(name, "r", stdin) == 0) {
68 perror(name);
fea5dc2a 69 return;
add17efb
BJ
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);
12bd8a08
SL
92 if (tocrc)
93 doname(name);
94 if (!intro)
95 printf("%s\t", headbuf);
add17efb
BJ
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);
12bd8a08
SL
106 if (intro) {
107 split(linbuf, name);
108 continue;
109 }
add17efb
BJ
110 if (i != 0)
111 printf(" ");
112 i++;
113 printf("%s", linbuf);
114 }
115 printf("\n");
116}
117
118trimln(cp)
119 register char *cp;
120{
121
122 while (*cp)
123 cp++;
124 if (*--cp == '\n')
125 *cp = 0;
126}
12bd8a08
SL
127
128doname(name)
129 char *name;
130{
131 register char *dp = name, *ep;
132
133again:
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
151split(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);
4cd9ee98 178 dorefname(name);
12bd8a08
SL
179 printf("\t%s", sp);
180 }
181}
4cd9ee98
SL
182
183dorefname(name)
184 char *name;
185{
186 register char *dp = name, *ep;
187
188again:
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}
8b287fa3
KB
203
204usage()
205{
206 (void)fprintf(stderr, "usage: getNAME [-it] file ...\n");
207 exit(1);
208}