xref: /csrg-svn/libexec/getNAME/getNAME.c (revision 25245)
119818Sdist /*
219818Sdist  * Copyright (c) 1980 Regents of the University of California.
319818Sdist  * All rights reserved.  The Berkeley software License Agreement
419818Sdist  * specifies the terms and conditions for redistribution.
519818Sdist  */
619818Sdist 
713912Ssam #ifndef lint
8*25245Skarels static char sccsid[] = "@(#)getNAME.c	5.2 (Berkeley) 10/21/85";
919818Sdist #endif not lint
1013912Ssam 
1113912Ssam /*
1213912Ssam  * Get name sections from manual pages.
1313912Ssam  *	-t	for building toc
1413912Ssam  *	-i	for building intro entries
1513912Ssam  *	other	apropos database
1613912Ssam  */
1713912Ssam #include <strings.h>
181022Sbill #include <stdio.h>
191022Sbill 
201022Sbill int tocrc;
2113912Ssam int intro;
221022Sbill 
231022Sbill main(argc, argv)
241022Sbill 	int argc;
251022Sbill 	char *argv[];
261022Sbill {
271022Sbill 
281022Sbill 	argc--, argv++;
291022Sbill 	if (!strcmp(argv[0], "-t"))
301022Sbill 		argc--, argv++, tocrc++;
3113912Ssam 	if (!strcmp(argv[0], "-i"))
3213912Ssam 		argc--, argv++, intro++;
331022Sbill 	while (argc > 0)
341022Sbill 		getfrom(*argv++), argc--;
351022Sbill 	exit(0);
361022Sbill }
371022Sbill 
381022Sbill getfrom(name)
391022Sbill 	char *name;
401022Sbill {
411022Sbill 	char headbuf[BUFSIZ];
421022Sbill 	char linbuf[BUFSIZ];
431022Sbill 	register char *cp;
441022Sbill 	int i = 0;
451022Sbill 
461022Sbill 	if (freopen(name, "r", stdin) == 0) {
471022Sbill 		perror(name);
48*25245Skarels 		return;
491022Sbill 	}
501022Sbill 	for (;;) {
511022Sbill 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
521022Sbill 			return;
531022Sbill 		if (headbuf[0] != '.')
541022Sbill 			continue;
551022Sbill 		if (headbuf[1] == 'T' && headbuf[2] == 'H')
561022Sbill 			break;
571022Sbill 		if (headbuf[1] == 't' && headbuf[2] == 'h')
581022Sbill 			break;
591022Sbill 	}
601022Sbill 	for (;;) {
611022Sbill 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
621022Sbill 			return;
631022Sbill 		if (linbuf[0] != '.')
641022Sbill 			continue;
651022Sbill 		if (linbuf[1] == 'S' && linbuf[2] == 'H')
661022Sbill 			break;
671022Sbill 		if (linbuf[1] == 's' && linbuf[2] == 'h')
681022Sbill 			break;
691022Sbill 	}
701022Sbill 	trimln(headbuf);
7113912Ssam 	if (tocrc)
7213912Ssam 		doname(name);
7313912Ssam 	if (!intro)
7413912Ssam 		printf("%s\t", headbuf);
751022Sbill 	for (;;) {
761022Sbill 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
771022Sbill 			break;
781022Sbill 		if (linbuf[0] == '.') {
791022Sbill 			if (linbuf[1] == 'S' && linbuf[2] == 'H')
801022Sbill 				break;
811022Sbill 			if (linbuf[1] == 's' && linbuf[2] == 'h')
821022Sbill 				break;
831022Sbill 		}
841022Sbill 		trimln(linbuf);
8513912Ssam 		if (intro) {
8613912Ssam 			split(linbuf, name);
8713912Ssam 			continue;
8813912Ssam 		}
891022Sbill 		if (i != 0)
901022Sbill 			printf(" ");
911022Sbill 		i++;
921022Sbill 		printf("%s", linbuf);
931022Sbill 	}
941022Sbill 	printf("\n");
951022Sbill }
961022Sbill 
971022Sbill trimln(cp)
981022Sbill 	register char *cp;
991022Sbill {
1001022Sbill 
1011022Sbill 	while (*cp)
1021022Sbill 		cp++;
1031022Sbill 	if (*--cp == '\n')
1041022Sbill 		*cp = 0;
1051022Sbill }
10613912Ssam 
10713912Ssam doname(name)
10813912Ssam 	char *name;
10913912Ssam {
11013912Ssam 	register char *dp = name, *ep;
11113912Ssam 
11213912Ssam again:
11313912Ssam 	while (*dp && *dp != '.')
11413912Ssam 		putchar(*dp++);
11513912Ssam 	if (*dp)
11613912Ssam 		for (ep = dp+1; *ep; ep++)
11713912Ssam 			if (*ep == '.') {
11813912Ssam 				putchar(*dp++);
11913912Ssam 				goto again;
12013912Ssam 			}
12113912Ssam 	putchar('(');
12213912Ssam 	if (*dp)
12313912Ssam 		dp++;
12413912Ssam 	while (*dp)
12513912Ssam 		putchar (*dp++);
12613912Ssam 	putchar(')');
12713912Ssam 	putchar(' ');
12813912Ssam }
12913912Ssam 
13013912Ssam split(line, name)
13113912Ssam 	char *line, *name;
13213912Ssam {
13313912Ssam 	register char *cp, *dp;
13413912Ssam 	char *sp, *sep;
13513912Ssam 
13613912Ssam 	cp = index(line, '-');
13713912Ssam 	if (cp == 0)
13813912Ssam 		return;
13913912Ssam 	sp = cp + 1;
14013912Ssam 	for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
14113912Ssam 		;
14213912Ssam 	*++cp = '\0';
14313912Ssam 	while (*sp && (*sp == ' ' || *sp == '\t'))
14413912Ssam 		sp++;
14513912Ssam 	for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
14613912Ssam 		cp = index(dp, ',');
14713912Ssam 		if (cp) {
14813912Ssam 			register char *tp;
14913912Ssam 
15013912Ssam 			for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
15113912Ssam 				;
15213912Ssam 			*++tp = '\0';
15313912Ssam 			for (++cp; *cp == ' ' || *cp == '\t'; cp++)
15413912Ssam 				;
15513912Ssam 		}
15613912Ssam 		printf("%s%s\t", sep, dp);
15713913Ssam 		dorefname(name);
15813912Ssam 		printf("\t%s", sp);
15913912Ssam 	}
16013912Ssam }
16113913Ssam 
16213913Ssam dorefname(name)
16313913Ssam 	char *name;
16413913Ssam {
16513913Ssam 	register char *dp = name, *ep;
16613913Ssam 
16713913Ssam again:
16813913Ssam 	while (*dp && *dp != '.')
16913913Ssam 		putchar(*dp++);
17013913Ssam 	if (*dp)
17113913Ssam 		for (ep = dp+1; *ep; ep++)
17213913Ssam 			if (*ep == '.') {
17313913Ssam 				putchar(*dp++);
17413913Ssam 				goto again;
17513913Ssam 			}
17613913Ssam 	putchar('.');
17713913Ssam 	if (*dp)
17813913Ssam 		dp++;
17913913Ssam 	while (*dp)
18013913Ssam 		putchar (*dp++);
18113913Ssam }
182