xref: /csrg-svn/libexec/getNAME/getNAME.c (revision 1022)
1*1022Sbill static char *sccsid = "@(#)getNAME.c	4.1 (Berkeley) 10/01/80";
2*1022Sbill #include <stdio.h>
3*1022Sbill 
4*1022Sbill int tocrc;
5*1022Sbill 
6*1022Sbill main(argc, argv)
7*1022Sbill 	int argc;
8*1022Sbill 	char *argv[];
9*1022Sbill {
10*1022Sbill 
11*1022Sbill 	argc--, argv++;
12*1022Sbill 	if (!strcmp(argv[0], "-t"))
13*1022Sbill 		argc--, argv++, tocrc++;
14*1022Sbill 	while (argc > 0)
15*1022Sbill 		getfrom(*argv++), argc--;
16*1022Sbill 	exit(0);
17*1022Sbill }
18*1022Sbill 
19*1022Sbill getfrom(name)
20*1022Sbill 	char *name;
21*1022Sbill {
22*1022Sbill 	char headbuf[BUFSIZ];
23*1022Sbill 	char linbuf[BUFSIZ];
24*1022Sbill 	register char *cp;
25*1022Sbill 	int i = 0;
26*1022Sbill 
27*1022Sbill 	if (freopen(name, "r", stdin) == 0) {
28*1022Sbill 		perror(name);
29*1022Sbill 		exit(1);
30*1022Sbill 	}
31*1022Sbill 	for (;;) {
32*1022Sbill 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
33*1022Sbill 			return;
34*1022Sbill 		if (headbuf[0] != '.')
35*1022Sbill 			continue;
36*1022Sbill 		if (headbuf[1] == 'T' && headbuf[2] == 'H')
37*1022Sbill 			break;
38*1022Sbill 		if (headbuf[1] == 't' && headbuf[2] == 'h')
39*1022Sbill 			break;
40*1022Sbill 	}
41*1022Sbill 	for (;;) {
42*1022Sbill 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
43*1022Sbill 			return;
44*1022Sbill 		if (linbuf[0] != '.')
45*1022Sbill 			continue;
46*1022Sbill 		if (linbuf[1] == 'S' && linbuf[2] == 'H')
47*1022Sbill 			break;
48*1022Sbill 		if (linbuf[1] == 's' && linbuf[2] == 'h')
49*1022Sbill 			break;
50*1022Sbill 	}
51*1022Sbill 	trimln(headbuf);
52*1022Sbill 	if (tocrc) {
53*1022Sbill 		register char *dp = name, *ep;
54*1022Sbill 
55*1022Sbill again:
56*1022Sbill 		while (*dp && *dp != '.')
57*1022Sbill 			putchar(*dp++);
58*1022Sbill 		if (*dp)
59*1022Sbill 			for (ep = dp+1; *ep; ep++)
60*1022Sbill 				if (*ep == '.') {
61*1022Sbill 					putchar(*dp++);
62*1022Sbill 					goto again;
63*1022Sbill 				}
64*1022Sbill 		putchar('(');
65*1022Sbill 		if (*dp)
66*1022Sbill 			dp++;
67*1022Sbill 		while (*dp)
68*1022Sbill 			putchar (*dp++);
69*1022Sbill 		putchar(')');
70*1022Sbill 		putchar(' ');
71*1022Sbill 	}
72*1022Sbill 	printf("%s\t", headbuf);
73*1022Sbill 	for (;;) {
74*1022Sbill 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
75*1022Sbill 			break;
76*1022Sbill 		if (linbuf[0] == '.') {
77*1022Sbill 			if (linbuf[1] == 'S' && linbuf[2] == 'H')
78*1022Sbill 				break;
79*1022Sbill 			if (linbuf[1] == 's' && linbuf[2] == 'h')
80*1022Sbill 				break;
81*1022Sbill 		}
82*1022Sbill 		trimln(linbuf);
83*1022Sbill 		if (i != 0)
84*1022Sbill 			printf(" ");
85*1022Sbill 		i++;
86*1022Sbill 		printf("%s", linbuf);
87*1022Sbill 	}
88*1022Sbill 	printf("\n");
89*1022Sbill }
90*1022Sbill 
91*1022Sbill trimln(cp)
92*1022Sbill 	register char *cp;
93*1022Sbill {
94*1022Sbill 
95*1022Sbill 	while (*cp)
96*1022Sbill 		cp++;
97*1022Sbill 	if (*--cp == '\n')
98*1022Sbill 		*cp = 0;
99*1022Sbill }
100