xref: /csrg-svn/usr.bin/vgrind/vfontedpr.c (revision 1841)
1*1841Sroot #include <ctype.h>
2*1841Sroot #include <stdio.h>
3*1841Sroot #include <sys/types.h>
4*1841Sroot #include <sys/stat.h>
5*1841Sroot 
6*1841Sroot /*
7*1841Sroot  * Vfontedpr.
8*1841Sroot  *
9*1841Sroot  * Bill Joy, Apr. 1979.
10*1841Sroot  */
11*1841Sroot char	*ctime();
12*1841Sroot int	incomm;
13*1841Sroot int	instr;
14*1841Sroot int	nokeyw;
15*1841Sroot int	index;
16*1841Sroot int	margin;
17*1841Sroot 
18*1841Sroot main(argc, argv)
19*1841Sroot 	int argc;
20*1841Sroot 	char *argv[];
21*1841Sroot {
22*1841Sroot 	int lineno;
23*1841Sroot 	char *fname = "";
24*1841Sroot 	struct stat stbuf;
25*1841Sroot 	char buf[BUFSIZ];
26*1841Sroot 	int needbp = 0;
27*1841Sroot 
28*1841Sroot 	argc--, argv++;
29*1841Sroot 	do {
30*1841Sroot 		char *cp;
31*1841Sroot 
32*1841Sroot 		if (argc > 0) {
33*1841Sroot 			if (!strcmp(argv[0], "-h")) {
34*1841Sroot 				if (argc == 1) {
35*1841Sroot 					printf("'ds =H\n");
36*1841Sroot 					argc = 0;
37*1841Sroot 					goto rest;
38*1841Sroot 				}
39*1841Sroot 				printf("'ds =H %s\n", argv[1]);
40*1841Sroot 				argc -= 2;
41*1841Sroot 				argv += 2;
42*1841Sroot 				if (argc > 0)
43*1841Sroot 					continue;
44*1841Sroot 				goto rest;
45*1841Sroot 			}
46*1841Sroot 			if (!strcmp(argv[0], "-x")) {
47*1841Sroot 				index++;
48*1841Sroot 				argv[0] = "-n";
49*1841Sroot 			}
50*1841Sroot 			if (!strcmp(argv[0], "-n")) {
51*1841Sroot 				nokeyw++;
52*1841Sroot 				argc--, argv++;
53*1841Sroot 				continue;
54*1841Sroot 			}
55*1841Sroot 			if (freopen(argv[0], "r", stdin) == NULL) {
56*1841Sroot 				perror(argv[0]);
57*1841Sroot 				exit(1);
58*1841Sroot 			}
59*1841Sroot 			if (index)
60*1841Sroot 				printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
61*1841Sroot 			fname = argv[0];
62*1841Sroot 			argc--, argv++;
63*1841Sroot 		}
64*1841Sroot rest:
65*1841Sroot 		incomm = 0;
66*1841Sroot 		instr = 0;
67*1841Sroot 		printf(".ds =F %s\n", fname);
68*1841Sroot 		fstat(fileno(stdin), &stbuf);
69*1841Sroot 		cp = ctime(&stbuf.st_mtime);
70*1841Sroot 		cp[16] = '\0';
71*1841Sroot 		cp[24] = '\0';
72*1841Sroot 		printf(".ds =M %s %s\n", cp+4, cp+20);
73*1841Sroot 		if (needbp) {
74*1841Sroot 			needbp = 0;
75*1841Sroot 			printf(".()\n");
76*1841Sroot 			printf(".bp\n");
77*1841Sroot 		}
78*1841Sroot 		while (fgets(buf, sizeof buf, stdin) != NULL) {
79*1841Sroot 			if (buf[0] == '\f') {
80*1841Sroot 				printf(".bp\n");
81*1841Sroot 				continue;
82*1841Sroot 			}
83*1841Sroot 			putScp(buf);
84*1841Sroot 			if (buf[strlen(buf) - 2] != '\\')
85*1841Sroot 				instr = 0;
86*1841Sroot 			margin = 0;
87*1841Sroot 		}
88*1841Sroot 		needbp = 1;
89*1841Sroot 	} while (argc > 0);
90*1841Sroot 	exit(0);
91*1841Sroot }
92*1841Sroot 
93*1841Sroot #define	ps(x)	printf("%s", x)
94*1841Sroot #define isidchr(c) (isalnum(c) || (c) == '_')
95*1841Sroot 
96*1841Sroot putScp(os)
97*1841Sroot 	char *os;
98*1841Sroot {
99*1841Sroot 	register char *s = os;
100*1841Sroot 	register int i;
101*1841Sroot 	int xfld = 0;
102*1841Sroot 
103*1841Sroot 	if (nokeyw)
104*1841Sroot 		goto skip;
105*1841Sroot 	if ((*s == '_' || isalpha(*s)) && s[strlen(s) - 2] == ')') {
106*1841Sroot 		register char *t = s + 1;
107*1841Sroot 
108*1841Sroot 		while (isidchr(*t))
109*1841Sroot 			t++;
110*1841Sroot 		ps("'FN ");
111*1841Sroot 		while (s < t)
112*1841Sroot 			putchar(*s++);
113*1841Sroot 		ps("\n");
114*1841Sroot 	} else if (!strcmp(s, "}\n"))
115*1841Sroot 		ps("'-F\n");
116*1841Sroot skip:
117*1841Sroot 	while (*s) {
118*1841Sroot 		if (index) {
119*1841Sroot 			if (*s == ' ' || *s == '\t') {
120*1841Sroot 				if (xfld == 0)
121*1841Sroot 					printf("");
122*1841Sroot 				printf("\t");
123*1841Sroot 				xfld = 1;
124*1841Sroot 				while (*s == ' ' || *s == '\t')
125*1841Sroot 					s++;
126*1841Sroot 				continue;
127*1841Sroot 			}
128*1841Sroot 		}
129*1841Sroot 		if (!nokeyw && !incomm && *s == '"') {
130*1841Sroot 			if (instr) {
131*1841Sroot 				if (s[-1] != '\\')
132*1841Sroot 					instr = 0;
133*1841Sroot 			} else
134*1841Sroot 				if (s[-1] != '\'')
135*1841Sroot 					instr = 1;
136*1841Sroot 		}
137*1841Sroot 		if (incomm && s - os >= 2 && !strncmp("*/", s - 2, 2)) {
138*1841Sroot 			incomm = 0;
139*1841Sroot 			ps("\\c\n'-C\n");
140*1841Sroot 		} else if (!nokeyw && !incomm && !strncmp("/*", s, 2)) {
141*1841Sroot 			incomm = 1;
142*1841Sroot 			if (s != os)
143*1841Sroot 				ps("\\c");
144*1841Sroot 			ps("\\c\n'+C\n");
145*1841Sroot 			margin = width(os, s);
146*1841Sroot 			ps("\\*(/*");
147*1841Sroot 			s += 2;
148*1841Sroot 			continue;
149*1841Sroot 		}
150*1841Sroot 		if (*s == '\t') {
151*1841Sroot 			while (*s == '\t')
152*1841Sroot 				s++;
153*1841Sroot 			i = tabs(os, s) - margin / 8;
154*1841Sroot 			printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
155*1841Sroot 			continue;
156*1841Sroot 		}
157*1841Sroot /*
158*1841Sroot 		if (*s == '-' && s[1] == '>') {
159*1841Sroot 			s += 2;
160*1841Sroot 			ps("\\(->");
161*1841Sroot 			continue;
162*1841Sroot 		}
163*1841Sroot */
164*1841Sroot 		if (!nokeyw && !instr && (*s == '#' || isalpha(*s)) && (s == os || !isidchr(s[-1]))) {
165*1841Sroot 			i = iskw(s);
166*1841Sroot 			if (i > 0) {
167*1841Sroot 				ps("\\*(+K");
168*1841Sroot 				do
169*1841Sroot 					putcp(*s++);
170*1841Sroot 				while (--i > 0);
171*1841Sroot 				ps("\\*(-K");
172*1841Sroot 				continue;
173*1841Sroot 			}
174*1841Sroot 		}
175*1841Sroot 		putcp(*s++);
176*1841Sroot 	}
177*1841Sroot }
178*1841Sroot 
179*1841Sroot tabs(s, os)
180*1841Sroot 	char *s, *os;
181*1841Sroot {
182*1841Sroot 
183*1841Sroot 	return (width(s, os) / 8);
184*1841Sroot }
185*1841Sroot 
186*1841Sroot width(s, os)
187*1841Sroot 	register char *s, *os;
188*1841Sroot {
189*1841Sroot 	register int i = 0;
190*1841Sroot 
191*1841Sroot 	while (s < os) {
192*1841Sroot 		if (*s == '\t') {
193*1841Sroot 			i = (i + 8) &~ 7;
194*1841Sroot 			s++;
195*1841Sroot 			continue;
196*1841Sroot 		}
197*1841Sroot 		if (*s < ' ')
198*1841Sroot 			i += 2;
199*1841Sroot 		else
200*1841Sroot 			i++;
201*1841Sroot 		s++;
202*1841Sroot 	}
203*1841Sroot 	return (i);
204*1841Sroot }
205*1841Sroot 
206*1841Sroot putcp(c)
207*1841Sroot 	register int c;
208*1841Sroot {
209*1841Sroot 
210*1841Sroot 	switch(c) {
211*1841Sroot 
212*1841Sroot 	case '{':
213*1841Sroot 		ps("\\*(+K{\\*(-K");
214*1841Sroot 		break;
215*1841Sroot 
216*1841Sroot 	case '}':
217*1841Sroot 		ps("\\*(+K}\\*(-K");
218*1841Sroot 		break;
219*1841Sroot 
220*1841Sroot 	case '\\':
221*1841Sroot 		ps("\\e");
222*1841Sroot 		break;
223*1841Sroot 
224*1841Sroot 	case '_':
225*1841Sroot 		ps("\\*_");
226*1841Sroot 		break;
227*1841Sroot 
228*1841Sroot 	case '-':
229*1841Sroot 		ps("\\*-");
230*1841Sroot 		break;
231*1841Sroot 
232*1841Sroot 	case '`':
233*1841Sroot 		ps("\\`");
234*1841Sroot 		break;
235*1841Sroot 
236*1841Sroot 	case '\'':
237*1841Sroot 		ps("\\'");
238*1841Sroot 		break;
239*1841Sroot 
240*1841Sroot 	case '.':
241*1841Sroot 		ps("\\&.");
242*1841Sroot 		break;
243*1841Sroot 
244*1841Sroot 	default:
245*1841Sroot 		if (c < 040)
246*1841Sroot 			putchar('^'), c |= '@';
247*1841Sroot 	case '\t':
248*1841Sroot 	case '\n':
249*1841Sroot 		putchar(c);
250*1841Sroot 	}
251*1841Sroot }
252*1841Sroot 
253*1841Sroot char	*ckw[] = {
254*1841Sroot 	"asm",
255*1841Sroot 	"auto",
256*1841Sroot 	"break",
257*1841Sroot 	"case",
258*1841Sroot 	"char",
259*1841Sroot 	"continue",
260*1841Sroot 	"default",
261*1841Sroot 	"do",
262*1841Sroot 	"double",
263*1841Sroot 	"else",
264*1841Sroot 	"enum",
265*1841Sroot 	"extern",
266*1841Sroot 	"float",
267*1841Sroot 	"for",
268*1841Sroot 	"fortran",
269*1841Sroot 	"goto",
270*1841Sroot 	"if",
271*1841Sroot 	"int",
272*1841Sroot 	"long",
273*1841Sroot 	"register",
274*1841Sroot 	"return",
275*1841Sroot 	"short",
276*1841Sroot 	"sizeof",
277*1841Sroot 	"static",
278*1841Sroot 	"struct",
279*1841Sroot 	"switch",
280*1841Sroot 	"typedef",
281*1841Sroot 	"union",
282*1841Sroot 	"unsigned",
283*1841Sroot 	"while",
284*1841Sroot 	"#define",
285*1841Sroot 	"#else",
286*1841Sroot 	"#endif",
287*1841Sroot 	"#if",
288*1841Sroot 	"#ifdef",
289*1841Sroot 	"#ifndef",
290*1841Sroot 	"#include",
291*1841Sroot 	"#undef",
292*1841Sroot 	"#",
293*1841Sroot 	"define",
294*1841Sroot 	"else",
295*1841Sroot 	"endif",
296*1841Sroot 	"if",
297*1841Sroot 	"ifdef",
298*1841Sroot 	"ifndef",
299*1841Sroot 	"include",
300*1841Sroot 	"undef",
301*1841Sroot 	0,
302*1841Sroot };
303*1841Sroot 
304*1841Sroot iskw(s)
305*1841Sroot 	register char *s;
306*1841Sroot {
307*1841Sroot 	register char **ss = ckw;
308*1841Sroot 	register int i = 1;
309*1841Sroot 	register char *cp = s;
310*1841Sroot 
311*1841Sroot 	while (++cp, isidchr(*cp))
312*1841Sroot 		i++;
313*1841Sroot 	while (cp = *ss++)
314*1841Sroot 		if (*s == *cp && !strncmp(s, cp, i) && !isidchr(cp[i]))
315*1841Sroot 			return (i);
316*1841Sroot 	return (0);
317*1841Sroot }
318