xref: /csrg-svn/libexec/getNAME/getNAME.c (revision 63688)
146048Sbostic /*-
2*63688Sbostic  * Copyright (c) 1980, 1993
3*63688Sbostic  *	The Regents of the University of California.  All rights reserved.
446048Sbostic  *
546048Sbostic  * %sccs.include.redist.c%
619818Sdist  */
719818Sdist 
813912Ssam #ifndef lint
9*63688Sbostic static char copyright[] =
10*63688Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*63688Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1246048Sbostic #endif /* not lint */
1313912Ssam 
1446048Sbostic #ifndef lint
15*63688Sbostic static char sccsid[] = "@(#)getNAME.c	8.1 (Berkeley) 06/30/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>
2560088Sbostic #include <stdlib.h>
2642028Sbostic #include <string.h>
271022Sbill 
281022Sbill int tocrc;
2913912Ssam int intro;
3063443Smckusick int typeflag;
311022Sbill 
3260088Sbostic void doname __P((char *));
3360088Sbostic void dorefname __P((char *));
3460088Sbostic void getfrom __P((char *));
3560088Sbostic void split __P((char *, char *));
3660088Sbostic void trimln __P((char *));
3760088Sbostic void usage __P((void));
3860088Sbostic 
3960088Sbostic int
main(argc,argv)401022Sbill main(argc, argv)
411022Sbill 	int argc;
4260088Sbostic 	char *argv[];
431022Sbill {
4446048Sbostic 	extern int optind;
4546048Sbostic 	int ch;
461022Sbill 
4763443Smckusick 	while ((ch = getopt(argc, argv, "itw")) != EOF)
4846048Sbostic 		switch(ch) {
4946048Sbostic 		case 'i':
5046048Sbostic 			intro = 1;
5146048Sbostic 			break;
5246048Sbostic 		case 't':
5346048Sbostic 			tocrc = 1;
5446048Sbostic 			break;
5563443Smckusick 		case 'w':
5663443Smckusick 			typeflag = 1;
5763443Smckusick 			break;
5846048Sbostic 		case '?':
5946048Sbostic 		default:
6046048Sbostic 			usage();
6146048Sbostic 		}
6246048Sbostic 	argc -= optind;
6346048Sbostic 	argv += optind;
6446048Sbostic 
6546048Sbostic 	if (!*argv)
6646048Sbostic 		usage();
6746048Sbostic 
6846048Sbostic 	for (; *argv; ++argv)
6946048Sbostic 		getfrom(*argv);
701022Sbill 	exit(0);
711022Sbill }
721022Sbill 
7360088Sbostic void
getfrom(pathname)7463443Smckusick getfrom(pathname)
7563443Smckusick 	char *pathname;
761022Sbill {
7746048Sbostic 	int i = 0;
7863443Smckusick 	char *name, *loc;
791022Sbill 	char headbuf[BUFSIZ];
801022Sbill 	char linbuf[BUFSIZ];
811022Sbill 
8263443Smckusick 	if (freopen(pathname, "r", stdin) == 0) {
8363443Smckusick 		perror(pathname);
8425245Skarels 		return;
851022Sbill 	}
8663443Smckusick 	if (name = strrchr(pathname, '/'))
8763443Smckusick 		name++;
8863443Smckusick 	else
8963443Smckusick 		name = pathname;
901022Sbill 	for (;;) {
9163443Smckusick 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL) {
9263443Smckusick 			if (typeflag)
9363443Smckusick 				printf("%-60s	UNKNOWN\n", pathname);
941022Sbill 			return;
9563443Smckusick 		}
961022Sbill 		if (headbuf[0] != '.')
971022Sbill 			continue;
9863443Smckusick 		if ((headbuf[1] == 'T' && headbuf[2] == 'H') ||
9963443Smckusick 		    (headbuf[1] == 't' && headbuf[2] == 'h'))
1001022Sbill 			break;
10163443Smckusick 		if (headbuf[1] == 'D' && headbuf[2] == 't') {
10263443Smckusick 			if (typeflag) {
10363443Smckusick 				printf("%-60s	NEW\n", pathname);
10463443Smckusick 				return;
10563443Smckusick 			}
10663443Smckusick 			goto newman;
10763443Smckusick 		}
1081022Sbill 	}
10963443Smckusick 	if (typeflag) {
11063443Smckusick 		printf("%-60s	OLD\n", pathname);
11163443Smckusick 		return;
11263443Smckusick 	}
1131022Sbill 	for (;;) {
1141022Sbill 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
1151022Sbill 			return;
1161022Sbill 		if (linbuf[0] != '.')
1171022Sbill 			continue;
1181022Sbill 		if (linbuf[1] == 'S' && linbuf[2] == 'H')
1191022Sbill 			break;
1201022Sbill 		if (linbuf[1] == 's' && linbuf[2] == 'h')
1211022Sbill 			break;
1221022Sbill 	}
1231022Sbill 	trimln(headbuf);
12413912Ssam 	if (tocrc)
12513912Ssam 		doname(name);
12663687Smckusick 	if (!tocrc && !intro)
12713912Ssam 		printf("%s\t", headbuf);
12863443Smckusick 	linbuf[0] = '\0';
1291022Sbill 	for (;;) {
13063443Smckusick 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
1311022Sbill 			break;
13263443Smckusick 		if (headbuf[0] == '.') {
13363443Smckusick 			if (headbuf[1] == 'S' && headbuf[2] == 'H')
1341022Sbill 				break;
13563443Smckusick 			if (headbuf[1] == 's' && headbuf[2] == 'h')
1361022Sbill 				break;
1371022Sbill 		}
13863443Smckusick 		if (i != 0)
13963443Smckusick 			strcat(linbuf, " ");
14063443Smckusick 		i++;
14163443Smckusick 		trimln(headbuf);
14263443Smckusick 		strcat(linbuf, headbuf);
14363443Smckusick 	}
14463443Smckusick 	if (intro)
14563443Smckusick 		split(linbuf, name);
14663443Smckusick 	else
14763443Smckusick 		printf("%s\n", linbuf);
14863443Smckusick 	return;
14963443Smckusick 
15063443Smckusick newman:
15163443Smckusick 	for (;;) {
15263443Smckusick 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
15363443Smckusick 			return;
15463443Smckusick 		if (linbuf[0] != '.')
15513912Ssam 			continue;
15663443Smckusick 		if (linbuf[1] == 'S' && linbuf[2] == 'h')
15763443Smckusick 			break;
15863443Smckusick 	}
15963443Smckusick 	trimln(headbuf);
16063443Smckusick 	if (tocrc)
16163443Smckusick 		doname(name);
16263443Smckusick 	if (!tocrc && !intro)
16363443Smckusick 		printf(".TH%s\t", &headbuf[3]);
16463443Smckusick 	linbuf[0] = '\0';
16563443Smckusick 	for (;;) {
16663443Smckusick 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
16763443Smckusick 			break;
16863443Smckusick 		if (headbuf[0] == '.') {
16963443Smckusick 			if (headbuf[1] == 'S' && headbuf[2] == 'h')
17063443Smckusick 				break;
17113912Ssam 		}
1721022Sbill 		if (i != 0)
17363443Smckusick 			strcat(linbuf, " ");
1741022Sbill 		i++;
17563443Smckusick 		trimln(headbuf);
17663530Smckusick 		for (loc = strchr(headbuf, ' '); loc; loc = strchr(loc, ' '))
17763443Smckusick 			if (loc[1] == ',')
17863443Smckusick 				strcpy(loc, &loc[1]);
17963443Smckusick 			else
18063443Smckusick 				loc++;
18163443Smckusick 		if (headbuf[0] != '.') {
18263443Smckusick 			strcat(linbuf, headbuf);
18363443Smckusick 		} else {
18463530Smckusick 			/*
18563530Smckusick 			 * Get rid of quotes in macros.
18663530Smckusick 			 */
18763530Smckusick 			for (loc = strchr(&headbuf[4], '"'); loc; ) {
18863530Smckusick 				strcpy(loc, &loc[1]);
18963530Smckusick 				loc = strchr(loc, '"');
19063530Smckusick 			}
19163530Smckusick 			/*
19263530Smckusick 			 * Handle cross references
19363530Smckusick 			 */
19463530Smckusick 			if (headbuf[1] == 'X' && headbuf[2] == 'r') {
19563530Smckusick 				for (loc = &headbuf[4]; *loc != ' '; loc++)
19663530Smckusick 					continue;
19763530Smckusick 				loc[0] = '(';
19863530Smckusick 				loc[2] = ')';
19963530Smckusick 				loc[3] = '\0';
20063530Smckusick 			}
20163530Smckusick 			/*
20263530Smckusick 			 * Put dash between names and description.
20363530Smckusick 			 */
20463443Smckusick 			if (headbuf[1] == 'N' && headbuf[2] == 'd')
20563443Smckusick 				strcat(linbuf, "\\- ");
20663530Smckusick 			/*
20763530Smckusick 			 * Skip over macro names.
20863530Smckusick 			 */
20963443Smckusick 			strcat(linbuf, &headbuf[4]);
21063443Smckusick 		}
2111022Sbill 	}
21263443Smckusick 	if (intro)
21363443Smckusick 		split(linbuf, name);
21463443Smckusick 	else
21563443Smckusick 		printf("%s\n", linbuf);
2161022Sbill }
2171022Sbill 
21860088Sbostic void
trimln(cp)2191022Sbill trimln(cp)
2201022Sbill 	register char *cp;
2211022Sbill {
2221022Sbill 
2231022Sbill 	while (*cp)
2241022Sbill 		cp++;
2251022Sbill 	if (*--cp == '\n')
2261022Sbill 		*cp = 0;
2271022Sbill }
22813912Ssam 
22960088Sbostic void
doname(name)23013912Ssam doname(name)
23113912Ssam 	char *name;
23213912Ssam {
23313912Ssam 	register char *dp = name, *ep;
23413912Ssam 
23513912Ssam again:
23613912Ssam 	while (*dp && *dp != '.')
23713912Ssam 		putchar(*dp++);
23813912Ssam 	if (*dp)
23913912Ssam 		for (ep = dp+1; *ep; ep++)
24013912Ssam 			if (*ep == '.') {
24113912Ssam 				putchar(*dp++);
24213912Ssam 				goto again;
24313912Ssam 			}
24413912Ssam 	putchar('(');
24513912Ssam 	if (*dp)
24613912Ssam 		dp++;
24713912Ssam 	while (*dp)
24813912Ssam 		putchar (*dp++);
24913912Ssam 	putchar(')');
25013912Ssam 	putchar(' ');
25113912Ssam }
25213912Ssam 
25360088Sbostic void
split(line,name)25413912Ssam split(line, name)
25513912Ssam 	char *line, *name;
25613912Ssam {
25713912Ssam 	register char *cp, *dp;
25813912Ssam 	char *sp, *sep;
25913912Ssam 
26060088Sbostic 	cp = strchr(line, '-');
26113912Ssam 	if (cp == 0)
26213912Ssam 		return;
26313912Ssam 	sp = cp + 1;
26413912Ssam 	for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
26513912Ssam 		;
26613912Ssam 	*++cp = '\0';
26713912Ssam 	while (*sp && (*sp == ' ' || *sp == '\t'))
26813912Ssam 		sp++;
26913912Ssam 	for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
27060088Sbostic 		cp = strchr(dp, ',');
27113912Ssam 		if (cp) {
27213912Ssam 			register char *tp;
27313912Ssam 
27413912Ssam 			for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
27513912Ssam 				;
27613912Ssam 			*++tp = '\0';
27713912Ssam 			for (++cp; *cp == ' ' || *cp == '\t'; cp++)
27813912Ssam 				;
27913912Ssam 		}
28013912Ssam 		printf("%s%s\t", sep, dp);
28113913Ssam 		dorefname(name);
28213912Ssam 		printf("\t%s", sp);
28313912Ssam 	}
28413912Ssam }
28513913Ssam 
28660088Sbostic void
dorefname(name)28713913Ssam dorefname(name)
28813913Ssam 	char *name;
28913913Ssam {
29013913Ssam 	register char *dp = name, *ep;
29113913Ssam 
29213913Ssam again:
29313913Ssam 	while (*dp && *dp != '.')
29413913Ssam 		putchar(*dp++);
29513913Ssam 	if (*dp)
29613913Ssam 		for (ep = dp+1; *ep; ep++)
29713913Ssam 			if (*ep == '.') {
29813913Ssam 				putchar(*dp++);
29913913Ssam 				goto again;
30013913Ssam 			}
30113913Ssam 	putchar('.');
30213913Ssam 	if (*dp)
30313913Ssam 		dp++;
30413913Ssam 	while (*dp)
30513913Ssam 		putchar (*dp++);
30613913Ssam }
30746048Sbostic 
30860088Sbostic void
usage()30946048Sbostic usage()
31046048Sbostic {
31146048Sbostic 	(void)fprintf(stderr, "usage: getNAME [-it] file ...\n");
31246048Sbostic 	exit(1);
31346048Sbostic }
314