xref: /csrg-svn/libexec/getNAME/getNAME.c (revision 60088)
146048Sbostic /*-
246048Sbostic  * Copyright (c) 1980 The Regents of the University of California.
346048Sbostic  * All rights reserved.
446048Sbostic  *
546048Sbostic  * %sccs.include.redist.c%
619818Sdist  */
719818Sdist 
813912Ssam #ifndef lint
946048Sbostic char copyright[] =
1046048Sbostic "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
1146048Sbostic  All rights reserved.\n";
1246048Sbostic #endif /* not lint */
1313912Ssam 
1446048Sbostic #ifndef lint
15*60088Sbostic static char sccsid[] = "@(#)getNAME.c	5.5 (Berkeley) 05/17/93";
1646048Sbostic #endif /* not lint */
1746048Sbostic 
1813912Ssam /*
1913912Ssam  * Get name sections from manual pages.
2013912Ssam  *	-t	for building toc
2113912Ssam  *	-i	for building intro entries
2213912Ssam  *	other	apropos database
2313912Ssam  */
241022Sbill #include <stdio.h>
25*60088Sbostic #include <stdlib.h>
2642028Sbostic #include <string.h>
271022Sbill 
281022Sbill int tocrc;
2913912Ssam int intro;
301022Sbill 
31*60088Sbostic void doname __P((char *));
32*60088Sbostic void dorefname __P((char *));
33*60088Sbostic void getfrom __P((char *));
34*60088Sbostic void split __P((char *, char *));
35*60088Sbostic void trimln __P((char *));
36*60088Sbostic void usage __P((void));
37*60088Sbostic 
38*60088Sbostic int
391022Sbill main(argc, argv)
401022Sbill 	int argc;
41*60088Sbostic 	char *argv[];
421022Sbill {
4346048Sbostic 	extern int optind;
4446048Sbostic 	int ch;
451022Sbill 
4646048Sbostic 	while ((ch = getopt(argc, argv, "it")) != EOF)
4746048Sbostic 		switch(ch) {
4846048Sbostic 		case 'i':
4946048Sbostic 			intro = 1;
5046048Sbostic 			break;
5146048Sbostic 		case 't':
5246048Sbostic 			tocrc = 1;
5346048Sbostic 			break;
5446048Sbostic 		case '?':
5546048Sbostic 		default:
5646048Sbostic 			usage();
5746048Sbostic 		}
5846048Sbostic 	argc -= optind;
5946048Sbostic 	argv += optind;
6046048Sbostic 
6146048Sbostic 	if (!*argv)
6246048Sbostic 		usage();
6346048Sbostic 
6446048Sbostic 	for (; *argv; ++argv)
6546048Sbostic 		getfrom(*argv);
661022Sbill 	exit(0);
671022Sbill }
681022Sbill 
69*60088Sbostic void
701022Sbill getfrom(name)
711022Sbill 	char *name;
721022Sbill {
7346048Sbostic 	int i = 0;
741022Sbill 	char headbuf[BUFSIZ];
751022Sbill 	char linbuf[BUFSIZ];
761022Sbill 
771022Sbill 	if (freopen(name, "r", stdin) == 0) {
781022Sbill 		perror(name);
7925245Skarels 		return;
801022Sbill 	}
811022Sbill 	for (;;) {
821022Sbill 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
831022Sbill 			return;
841022Sbill 		if (headbuf[0] != '.')
851022Sbill 			continue;
861022Sbill 		if (headbuf[1] == 'T' && headbuf[2] == 'H')
871022Sbill 			break;
881022Sbill 		if (headbuf[1] == 't' && headbuf[2] == 'h')
891022Sbill 			break;
901022Sbill 	}
911022Sbill 	for (;;) {
921022Sbill 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
931022Sbill 			return;
941022Sbill 		if (linbuf[0] != '.')
951022Sbill 			continue;
961022Sbill 		if (linbuf[1] == 'S' && linbuf[2] == 'H')
971022Sbill 			break;
981022Sbill 		if (linbuf[1] == 's' && linbuf[2] == 'h')
991022Sbill 			break;
1001022Sbill 	}
1011022Sbill 	trimln(headbuf);
10213912Ssam 	if (tocrc)
10313912Ssam 		doname(name);
10413912Ssam 	if (!intro)
10513912Ssam 		printf("%s\t", headbuf);
1061022Sbill 	for (;;) {
1071022Sbill 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
1081022Sbill 			break;
1091022Sbill 		if (linbuf[0] == '.') {
1101022Sbill 			if (linbuf[1] == 'S' && linbuf[2] == 'H')
1111022Sbill 				break;
1121022Sbill 			if (linbuf[1] == 's' && linbuf[2] == 'h')
1131022Sbill 				break;
1141022Sbill 		}
1151022Sbill 		trimln(linbuf);
11613912Ssam 		if (intro) {
11713912Ssam 			split(linbuf, name);
11813912Ssam 			continue;
11913912Ssam 		}
1201022Sbill 		if (i != 0)
1211022Sbill 			printf(" ");
1221022Sbill 		i++;
1231022Sbill 		printf("%s", linbuf);
1241022Sbill 	}
1251022Sbill 	printf("\n");
1261022Sbill }
1271022Sbill 
128*60088Sbostic void
1291022Sbill trimln(cp)
1301022Sbill 	register char *cp;
1311022Sbill {
1321022Sbill 
1331022Sbill 	while (*cp)
1341022Sbill 		cp++;
1351022Sbill 	if (*--cp == '\n')
1361022Sbill 		*cp = 0;
1371022Sbill }
13813912Ssam 
139*60088Sbostic void
14013912Ssam doname(name)
14113912Ssam 	char *name;
14213912Ssam {
14313912Ssam 	register char *dp = name, *ep;
14413912Ssam 
14513912Ssam again:
14613912Ssam 	while (*dp && *dp != '.')
14713912Ssam 		putchar(*dp++);
14813912Ssam 	if (*dp)
14913912Ssam 		for (ep = dp+1; *ep; ep++)
15013912Ssam 			if (*ep == '.') {
15113912Ssam 				putchar(*dp++);
15213912Ssam 				goto again;
15313912Ssam 			}
15413912Ssam 	putchar('(');
15513912Ssam 	if (*dp)
15613912Ssam 		dp++;
15713912Ssam 	while (*dp)
15813912Ssam 		putchar (*dp++);
15913912Ssam 	putchar(')');
16013912Ssam 	putchar(' ');
16113912Ssam }
16213912Ssam 
163*60088Sbostic void
16413912Ssam split(line, name)
16513912Ssam 	char *line, *name;
16613912Ssam {
16713912Ssam 	register char *cp, *dp;
16813912Ssam 	char *sp, *sep;
16913912Ssam 
170*60088Sbostic 	cp = strchr(line, '-');
17113912Ssam 	if (cp == 0)
17213912Ssam 		return;
17313912Ssam 	sp = cp + 1;
17413912Ssam 	for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
17513912Ssam 		;
17613912Ssam 	*++cp = '\0';
17713912Ssam 	while (*sp && (*sp == ' ' || *sp == '\t'))
17813912Ssam 		sp++;
17913912Ssam 	for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
180*60088Sbostic 		cp = strchr(dp, ',');
18113912Ssam 		if (cp) {
18213912Ssam 			register char *tp;
18313912Ssam 
18413912Ssam 			for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
18513912Ssam 				;
18613912Ssam 			*++tp = '\0';
18713912Ssam 			for (++cp; *cp == ' ' || *cp == '\t'; cp++)
18813912Ssam 				;
18913912Ssam 		}
19013912Ssam 		printf("%s%s\t", sep, dp);
19113913Ssam 		dorefname(name);
19213912Ssam 		printf("\t%s", sp);
19313912Ssam 	}
19413912Ssam }
19513913Ssam 
196*60088Sbostic void
19713913Ssam dorefname(name)
19813913Ssam 	char *name;
19913913Ssam {
20013913Ssam 	register char *dp = name, *ep;
20113913Ssam 
20213913Ssam again:
20313913Ssam 	while (*dp && *dp != '.')
20413913Ssam 		putchar(*dp++);
20513913Ssam 	if (*dp)
20613913Ssam 		for (ep = dp+1; *ep; ep++)
20713913Ssam 			if (*ep == '.') {
20813913Ssam 				putchar(*dp++);
20913913Ssam 				goto again;
21013913Ssam 			}
21113913Ssam 	putchar('.');
21213913Ssam 	if (*dp)
21313913Ssam 		dp++;
21413913Ssam 	while (*dp)
21513913Ssam 		putchar (*dp++);
21613913Ssam }
21746048Sbostic 
218*60088Sbostic void
21946048Sbostic usage()
22046048Sbostic {
22146048Sbostic 	(void)fprintf(stderr, "usage: getNAME [-it] file ...\n");
22246048Sbostic 	exit(1);
22346048Sbostic }
224