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