string.h is ANSI C include file
[unix-history] / usr / src / libexec / getNAME / getNAME.c
CommitLineData
051b1e55
DF
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
12bd8a08 7#ifndef lint
6ebcb998 8static char sccsid[] = "@(#)getNAME.c 5.3 (Berkeley) %G%";
051b1e55 9#endif not lint
12bd8a08
SL
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 */
add17efb 17#include <stdio.h>
6ebcb998 18#include <string.h>
add17efb
BJ
19
20int tocrc;
12bd8a08 21int intro;
add17efb
BJ
22
23main(argc, argv)
24 int argc;
25 char *argv[];
26{
27
28 argc--, argv++;
29 if (!strcmp(argv[0], "-t"))
30 argc--, argv++, tocrc++;
12bd8a08
SL
31 if (!strcmp(argv[0], "-i"))
32 argc--, argv++, intro++;
add17efb
BJ
33 while (argc > 0)
34 getfrom(*argv++), argc--;
35 exit(0);
36}
37
38getfrom(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);
fea5dc2a 48 return;
add17efb
BJ
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);
12bd8a08
SL
71 if (tocrc)
72 doname(name);
73 if (!intro)
74 printf("%s\t", headbuf);
add17efb
BJ
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);
12bd8a08
SL
85 if (intro) {
86 split(linbuf, name);
87 continue;
88 }
add17efb
BJ
89 if (i != 0)
90 printf(" ");
91 i++;
92 printf("%s", linbuf);
93 }
94 printf("\n");
95}
96
97trimln(cp)
98 register char *cp;
99{
100
101 while (*cp)
102 cp++;
103 if (*--cp == '\n')
104 *cp = 0;
105}
12bd8a08
SL
106
107doname(name)
108 char *name;
109{
110 register char *dp = name, *ep;
111
112again:
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
130split(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);
4cd9ee98 157 dorefname(name);
12bd8a08
SL
158 printf("\t%s", sp);
159 }
160}
4cd9ee98
SL
161
162dorefname(name)
163 char *name;
164{
165 register char *dp = name, *ep;
166
167again:
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}