Commit | Line | Data |
---|---|---|
0b79a041 BJ |
1 | #include <ctype.h> |
2 | #include <stdio.h> | |
3 | #include <sys/types.h> | |
4 | #include <sys/stat.h> | |
5 | ||
a388f2af BJ |
6 | #define boolean int |
7 | #define TRUE 1 | |
8 | #define FALSE 0 | |
9 | #define NIL 0 | |
10 | ||
0b79a041 BJ |
11 | /* |
12 | * Vfontedpr. | |
13 | * | |
a388f2af | 14 | * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy) |
f5fa559c BJ |
15 | * |
16 | */ | |
f5fa559c BJ |
17 | |
18 | #define STRLEN 10 /* length of strings introducing things */ | |
19 | #define PNAMELEN 40 /* length of a function/procedure name */ | |
a388f2af | 20 | #define PSMAX 20 /* size of procedure name stacking */ |
f5fa559c | 21 | |
a388f2af | 22 | /* regular expression routines */ |
b48967e6 | 23 | |
a388f2af BJ |
24 | char *expmatch(); /* match a string to an expression */ |
25 | char *STRNCMP(); /* a different kindof strncmp */ | |
26 | char *convexp(); /* convert expression to internal form */ | |
27 | ||
28 | boolean isproc(); | |
f5fa559c | 29 | |
f5fa559c | 30 | |
0b79a041 | 31 | char *ctime(); |
a388f2af BJ |
32 | |
33 | /* | |
34 | * The state variables | |
35 | */ | |
36 | ||
37 | boolean incomm; /* in a comment of the primary type */ | |
38 | boolean instr; /* in a string constant */ | |
39 | boolean inchr; /* in a string constant */ | |
40 | boolean nokeyw = FALSE; /* no keywords being flagged */ | |
41 | boolean index = FALSE; /* form an index */ | |
42 | boolean filter = FALSE; /* act as a filter (like eqn) */ | |
43 | boolean pass = FALSE; /* when acting as a filter, pass indicates | |
44 | * whether we are currently processing | |
45 | * input. | |
46 | */ | |
47 | boolean prccont; /* continue last procedure */ | |
0b79a041 | 48 | int margin; |
a388f2af BJ |
49 | int psptr; /* the stack index of the current procedure */ |
50 | char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */ | |
51 | int plstack[PSMAX]; /* the procedure nesting level stack */ | |
52 | int blklevel; /* current nesting level */ | |
53 | char *defsfile = "/usr/lib/vgrindefs"; /* name of language definitions file */ | |
54 | char pname[BUFSIZ+1]; | |
b48967e6 | 55 | |
a388f2af BJ |
56 | /* |
57 | * The language specific globals | |
58 | */ | |
59 | ||
60 | char *language = "c"; /* the language indicator */ | |
61 | char *l_keywds[BUFSIZ/2]; /* keyword table address */ | |
62 | char *l_prcbeg; /* regular expr for procedure begin */ | |
63 | char *l_combeg; /* string introducing a comment */ | |
64 | char *l_comend; /* string ending a comment */ | |
65 | char *l_blkbeg; /* string begining of a block */ | |
66 | char *l_blkend; /* string ending a block */ | |
67 | char *l_strbeg; /* delimiter for string constant */ | |
68 | char *l_strend; /* delimiter for string constant */ | |
69 | char *l_chrbeg; /* delimiter for character constant */ | |
70 | char *l_chrend; /* delimiter for character constant */ | |
71 | char l_escape; /* character used to escape characters */ | |
72 | boolean l_toplex; /* procedures only defined at top lex level */ | |
73 | ||
74 | /* | |
75 | * global variables also used by expmatch | |
76 | */ | |
77 | boolean _escaped; /* if last character was an escape */ | |
78 | char *_start; /* start of the current string */ | |
79 | boolean l_onecase; /* upper and lower case are equivalent */ | |
f5fa559c BJ |
80 | |
81 | #define ps(x) printf("%s", x) | |
0b79a041 BJ |
82 | |
83 | main(argc, argv) | |
a388f2af BJ |
84 | int argc; |
85 | char *argv[]; | |
0b79a041 | 86 | { |
a388f2af BJ |
87 | int lineno; |
88 | char *fname = ""; | |
89 | char *ptr; | |
90 | struct stat stbuf; | |
91 | char buf[BUFSIZ]; | |
92 | char strings[2 * BUFSIZ]; | |
93 | char defs[2 * BUFSIZ]; | |
94 | int needbp = 0; | |
95 | ||
96 | argc--, argv++; | |
97 | do { | |
98 | char *cp; | |
99 | int i; | |
100 | ||
101 | if (argc > 0) { | |
102 | if (!strcmp(argv[0], "-h")) { | |
103 | if (argc == 1) { | |
104 | printf("'ds =H\n"); | |
105 | argc = 0; | |
106 | goto rest; | |
0b79a041 | 107 | } |
a388f2af BJ |
108 | printf("'ds =H %s\n", argv[1]); |
109 | argc -= 2; | |
110 | argv += 2; | |
111 | if (argc > 0) | |
112 | continue; | |
113 | goto rest; | |
114 | } | |
115 | ||
116 | /* act as a filter like eqn */ | |
117 | if (!strcmp(argv[0], "-f")) { | |
118 | filter++; | |
119 | argv[0] = argv[argc-1]; | |
120 | argv[argc-1] = "-"; | |
121 | continue; | |
122 | } | |
123 | ||
124 | /* take input from the standard place */ | |
125 | if (!strcmp(argv[0], "-")) { | |
126 | argc = 0; | |
127 | goto rest; | |
128 | } | |
129 | ||
130 | /* build an index */ | |
131 | if (!strcmp(argv[0], "-x")) { | |
132 | index++; | |
133 | argv[0] = "-n"; | |
134 | } | |
135 | ||
136 | /* indicate no keywords */ | |
137 | if (!strcmp(argv[0], "-n")) { | |
138 | nokeyw++; | |
139 | argc--, argv++; | |
140 | continue; | |
141 | } | |
142 | ||
143 | /* specify the font size */ | |
144 | if (!strncmp(argv[0], "-s", 2)) { | |
145 | i = 0; | |
146 | cp = argv[0] + 2; | |
147 | while (*cp) | |
148 | i = i * 10 + (*cp++ - '0'); | |
149 | printf("'ps %d\n'vs %d\n", i, i+1); | |
150 | argc--, argv++; | |
151 | continue; | |
152 | } | |
153 | ||
154 | /* specify the language */ | |
155 | if (!strncmp(argv[0], "-l", 2)) { | |
156 | language = argv[0]+2; | |
157 | argc--, argv++; | |
158 | continue; | |
159 | } | |
160 | ||
161 | /* specify the language description file */ | |
162 | if (!strncmp(argv[0], "-d", 2)) { | |
163 | defsfile = argv[1]; | |
164 | argc--, argv++; | |
165 | argc--, argv++; | |
166 | continue; | |
167 | } | |
168 | ||
169 | /* open the file for input */ | |
170 | if (freopen(argv[0], "r", stdin) == NULL) { | |
171 | perror(argv[0]); | |
172 | exit(1); | |
173 | } | |
174 | if (index) | |
175 | printf("'ta 4i 4.25i 5.5iR\n'in .5i\n"); | |
176 | fname = argv[0]; | |
177 | argc--, argv++; | |
178 | } | |
179 | rest: | |
180 | ||
181 | /* | |
182 | * get the language definition from the defs file | |
183 | */ | |
184 | i = tgetent (defs, language, defsfile); | |
185 | if (i == 0) { | |
186 | fprintf (stderr, "no entry for language %s\n", language); | |
187 | exit (0); | |
188 | } else if (i < 0) { | |
189 | fprintf (stderr, "cannot find vgrindefs file %s\n", defsfile); | |
190 | exit (0); | |
191 | } | |
192 | cp = strings; | |
193 | if (tgetstr ("kw", &cp) == NIL) | |
194 | nokeyw = TRUE; | |
195 | else { | |
196 | char **cpp; | |
197 | ||
198 | cpp = l_keywds; | |
199 | cp = strings; | |
200 | while (*cp) { | |
201 | while (*cp == ' ' || *cp =='\t') | |
202 | *cp++ = NULL; | |
203 | if (*cp) | |
204 | *cpp++ = cp; | |
205 | while (*cp != ' ' && *cp != '\t' && *cp) | |
206 | cp++; | |
207 | } | |
208 | *cpp = NIL; | |
209 | } | |
210 | cp = buf; | |
211 | l_prcbeg = convexp (tgetstr ("pb", &cp)); | |
212 | cp = buf; | |
213 | l_combeg = convexp (tgetstr ("cb", &cp)); | |
214 | cp = buf; | |
215 | l_comend = convexp (tgetstr ("ce", &cp)); | |
216 | cp = buf; | |
217 | l_strbeg = convexp (tgetstr ("sb", &cp)); | |
218 | cp = buf; | |
219 | l_strend = convexp (tgetstr ("se", &cp)); | |
220 | cp = buf; | |
221 | l_blkbeg = convexp (tgetstr ("bb", &cp)); | |
222 | cp = buf; | |
223 | l_blkend = convexp (tgetstr ("be", &cp)); | |
224 | cp = buf; | |
225 | l_chrbeg = convexp (tgetstr ("lb", &cp)); | |
226 | cp = buf; | |
227 | l_chrend = convexp (tgetstr ("le", &cp)); | |
228 | l_escape = '\\'; | |
229 | l_onecase = tgetflag ("oc"); | |
230 | l_toplex = tgetflag ("tl"); | |
231 | /* initialize the program */ | |
232 | ||
233 | incomm = FALSE; | |
234 | instr = FALSE; | |
235 | inchr = FALSE; | |
236 | _escaped = FALSE; | |
237 | blklevel = 0; | |
238 | for (psptr=0; psptr<PSMAX; psptr++) { | |
239 | pstack[psptr][0] = NULL; | |
240 | plstack[psptr] = 0; | |
241 | } | |
242 | psptr = -1; | |
243 | ps("'-F\n"); | |
244 | if (!filter) { | |
245 | printf(".ds =F %s\n", fname); | |
246 | fstat(fileno(stdin), &stbuf); | |
247 | cp = ctime(&stbuf.st_mtime); | |
248 | cp[16] = '\0'; | |
249 | cp[24] = '\0'; | |
250 | printf(".ds =M %s %s\n", cp+4, cp+20); | |
251 | } | |
252 | if (needbp) { | |
253 | needbp = 0; | |
254 | printf(".()\n"); | |
255 | printf(".bp\n"); | |
256 | } | |
257 | ||
258 | /* | |
259 | * MAIN LOOP!!! | |
260 | */ | |
261 | while (fgets(buf, sizeof buf, stdin) != NULL) { | |
262 | if (buf[0] == '\f') { | |
263 | printf(".bp\n"); | |
264 | } | |
265 | if (buf[0] == '.') { | |
266 | printf("%s", buf); | |
267 | if (!strncmp (buf+1, "vS", 2)) | |
268 | pass = TRUE; | |
269 | if (!strncmp (buf+1, "vE", 2)) | |
270 | pass = FALSE; | |
271 | continue; | |
272 | } | |
273 | prccont = FALSE; | |
274 | if (!filter || pass) | |
275 | putScp(buf); | |
276 | else | |
277 | printf("%s", buf); | |
278 | if (prccont && (psptr >= 0)) { | |
279 | ps("'FC "); | |
280 | ps(pstack[psptr]); | |
281 | ps("\n"); | |
282 | } | |
283 | #ifdef DEBUG | |
284 | printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr); | |
285 | #endif | |
286 | margin = 0; | |
287 | } | |
288 | needbp = 1; | |
289 | } while (argc > 0); | |
290 | exit(0); | |
0b79a041 BJ |
291 | } |
292 | ||
0b79a041 BJ |
293 | #define isidchr(c) (isalnum(c) || (c) == '_') |
294 | ||
295 | putScp(os) | |
a388f2af | 296 | char *os; |
0b79a041 | 297 | { |
a388f2af BJ |
298 | register char *s = os; /* pointer to unmatched string */ |
299 | char dummy[BUFSIZ]; /* dummy to be used by expmatch */ | |
300 | int xfld = 0; | |
301 | char *comptr; /* end of a comment delimiter */ | |
302 | char *strptr; /* end of a string delimiter */ | |
303 | char *chrptr; /* end of a character const delimiter */ | |
304 | char *blksptr; /* end of a lexical block start */ | |
305 | char *blkeptr; /* end of a lexical block end */ | |
306 | ||
307 | _start = os; /* remember the start for expmatch */ | |
308 | _escaped = FALSE; | |
309 | if (nokeyw || incomm || instr) | |
310 | goto skip; | |
311 | if (isproc(s)) { | |
312 | ps("'FN "); | |
313 | ps(pname); | |
09aabb70 | 314 | ps("\n"); |
a388f2af BJ |
315 | if (psptr < PSMAX) { |
316 | ++psptr; | |
317 | strncpy (pstack[psptr], pname, PNAMELEN); | |
318 | pstack[psptr][PNAMELEN] = NULL; | |
a388f2af BJ |
319 | plstack[psptr] = blklevel; |
320 | } | |
321 | } | |
0b79a041 | 322 | skip: |
a388f2af BJ |
323 | do { |
324 | if (index) { | |
325 | if (*s == ' ' || *s == '\t') { | |
326 | if (xfld == 0) | |
327 | printf("\ 1"); | |
328 | printf("\t"); | |
329 | xfld = 1; | |
330 | while (*s == ' ' || *s == '\t') | |
331 | s++; | |
332 | continue; | |
333 | } | |
334 | } | |
335 | ||
336 | /* check for string, comment, blockstart, etc */ | |
337 | if (!incomm && !instr && !inchr) { | |
338 | ||
339 | blkeptr = expmatch (s, l_blkend, dummy); | |
340 | blksptr = expmatch (s, l_blkbeg, dummy); | |
341 | comptr = expmatch (s, l_combeg, dummy); | |
342 | strptr = expmatch (s, l_strbeg, dummy); | |
343 | chrptr = expmatch (s, l_chrbeg, dummy); | |
344 | ||
345 | /* start of a comment? */ | |
346 | if (comptr != NIL) | |
347 | if ((comptr < strptr || strptr == NIL) | |
348 | && (comptr < chrptr || chrptr == NIL) | |
349 | && (comptr < blksptr || blksptr == NIL) | |
350 | && (comptr < blkeptr || blkeptr == NIL)) { | |
351 | putKcp (s, comptr-1, FALSE); | |
352 | s = comptr; | |
353 | incomm = TRUE; | |
354 | if (s != os) | |
355 | ps ("\\c"); | |
356 | ps ("\\c\n'+C\n"); | |
357 | continue; | |
cca8d17e | 358 | } |
a388f2af BJ |
359 | |
360 | /* start of a string? */ | |
361 | if (strptr != NIL) | |
362 | if ((strptr < chrptr || chrptr == NIL) | |
d81ba424 BJ |
363 | && (strptr < blksptr || blksptr == NIL) |
364 | && (strptr < blkeptr || blkeptr == NIL)) { | |
a388f2af BJ |
365 | putKcp (s, strptr-1, FALSE); |
366 | s = strptr; | |
367 | instr = TRUE; | |
368 | continue; | |
0b79a041 | 369 | } |
b48967e6 | 370 | |
a388f2af BJ |
371 | /* start of a character string? */ |
372 | if (chrptr != NIL) | |
d81ba424 BJ |
373 | if ((chrptr < blksptr || blksptr == NIL) |
374 | && (chrptr < blkeptr || blkeptr == NIL)) { | |
a388f2af BJ |
375 | putKcp (s, chrptr-1, FALSE); |
376 | s = chrptr; | |
377 | inchr = TRUE; | |
378 | continue; | |
0b79a041 | 379 | } |
b48967e6 | 380 | |
a388f2af BJ |
381 | /* end of a lexical block */ |
382 | if (blkeptr != NIL) { | |
383 | if (blkeptr < blksptr || blksptr == NIL) { | |
384 | putKcp (s, blkeptr - 1, FALSE); | |
385 | s = blkeptr; | |
386 | blklevel--; | |
387 | if (psptr >= 0 && plstack[psptr] >= blklevel) { | |
b48967e6 | 388 | |
a388f2af | 389 | /* end of current procedure */ |
b48967e6 | 390 | if (s != os) |
a388f2af BJ |
391 | ps ("\\c"); |
392 | ps ("\\c\n'-F\n"); | |
393 | blklevel = plstack[psptr]; | |
394 | ||
395 | /* see if we should print the last proc name */ | |
396 | if (--psptr >= 0) | |
397 | prccont = TRUE; | |
398 | else | |
399 | psptr = -1; | |
400 | } | |
401 | continue; | |
0b79a041 | 402 | } |
a388f2af | 403 | } |
b48967e6 | 404 | |
a388f2af BJ |
405 | /* start of a lexical block */ |
406 | if (blksptr != NIL) { | |
407 | putKcp (s, blksptr - 1, FALSE); | |
408 | s = blksptr; | |
409 | blklevel++; | |
410 | continue; | |
411 | } | |
412 | ||
413 | /* check for end of comment */ | |
414 | } else if (incomm) { | |
415 | if ((comptr = expmatch (s, l_comend, dummy)) != NIL) { | |
416 | putKcp (s, comptr-1, TRUE); | |
417 | s = comptr; | |
418 | incomm = FALSE; | |
419 | ps("\\c\n'-C\n"); | |
420 | continue; | |
421 | } else { | |
422 | putKcp (s, s + strlen(s) -1); | |
423 | s = s + strlen(s); | |
424 | continue; | |
425 | } | |
426 | ||
427 | /* check for end of string */ | |
428 | } else if (instr) { | |
429 | if ((strptr = expmatch (s, l_strend, dummy)) != NIL) { | |
430 | putKcp (s, strptr-1, TRUE); | |
431 | s = strptr; | |
432 | instr = FALSE; | |
433 | continue; | |
434 | } else { | |
435 | putKcp (s, s+strlen(s)-1, TRUE); | |
436 | s = s + strlen(s); | |
437 | continue; | |
438 | } | |
439 | ||
440 | /* check for end of character string */ | |
441 | } else if (inchr) { | |
442 | if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) { | |
443 | putKcp (s, chrptr-1, TRUE); | |
444 | s = chrptr; | |
445 | inchr = FALSE; | |
446 | continue; | |
447 | } else { | |
448 | putKcp (s, s+strlen(s)-1, TRUE); | |
449 | s = s + strlen(s); | |
450 | continue; | |
451 | } | |
0b79a041 | 452 | } |
a388f2af BJ |
453 | |
454 | /* print out the line */ | |
455 | putKcp (s, s + strlen(s) -1, FALSE); | |
456 | s = s + strlen(s); | |
457 | } while (*s); | |
0b79a041 BJ |
458 | } |
459 | ||
a388f2af BJ |
460 | putKcp (start, end, force) |
461 | char *start; /* start of string to write */ | |
462 | char *end; /* end of string to write */ | |
463 | boolean force; /* true if we should force nokeyw */ | |
464 | { | |
465 | int i; | |
466 | ||
467 | while (start <= end) { | |
468 | ||
469 | /* take care of nice tab stops */ | |
470 | if (*start == '\t') { | |
471 | while (*start == '\t') | |
472 | start++; | |
473 | i = tabs(_start, start) - margin / 8; | |
474 | printf("\\h'|%dn'", i * 10 + 1 - margin % 8); | |
475 | continue; | |
476 | } | |
477 | ||
478 | if (!nokeyw && !force) | |
479 | if ((*start == '#' || isidchr(*start)) | |
480 | && (start == _start || !isidchr(start[-1]))) { | |
481 | i = iskw(start); | |
482 | if (i > 0) { | |
483 | ps("\\*(+K"); | |
484 | do | |
485 | putcp(*start++); | |
486 | while (--i > 0); | |
487 | ps("\\*(-K"); | |
488 | continue; | |
489 | } | |
490 | } | |
491 | ||
492 | putcp (*start++); | |
493 | } | |
494 | } | |
495 | ||
496 | ||
0b79a041 | 497 | tabs(s, os) |
a388f2af | 498 | char *s, *os; |
0b79a041 BJ |
499 | { |
500 | ||
a388f2af | 501 | return (width(s, os) / 8); |
0b79a041 BJ |
502 | } |
503 | ||
504 | width(s, os) | |
505 | register char *s, *os; | |
506 | { | |
507 | register int i = 0; | |
508 | ||
509 | while (s < os) { | |
510 | if (*s == '\t') { | |
511 | i = (i + 8) &~ 7; | |
512 | s++; | |
513 | continue; | |
514 | } | |
515 | if (*s < ' ') | |
516 | i += 2; | |
517 | else | |
518 | i++; | |
519 | s++; | |
520 | } | |
521 | return (i); | |
522 | } | |
523 | ||
524 | putcp(c) | |
525 | register int c; | |
526 | { | |
527 | ||
528 | switch(c) { | |
529 | ||
a388f2af BJ |
530 | case 0: |
531 | break; | |
532 | ||
533 | case '\f': | |
534 | break; | |
535 | ||
0b79a041 BJ |
536 | case '{': |
537 | ps("\\*(+K{\\*(-K"); | |
538 | break; | |
539 | ||
540 | case '}': | |
541 | ps("\\*(+K}\\*(-K"); | |
542 | break; | |
543 | ||
544 | case '\\': | |
545 | ps("\\e"); | |
546 | break; | |
547 | ||
548 | case '_': | |
549 | ps("\\*_"); | |
550 | break; | |
551 | ||
552 | case '-': | |
553 | ps("\\*-"); | |
554 | break; | |
555 | ||
556 | case '`': | |
557 | ps("\\`"); | |
558 | break; | |
559 | ||
560 | case '\'': | |
561 | ps("\\'"); | |
562 | break; | |
563 | ||
564 | case '.': | |
565 | ps("\\&."); | |
566 | break; | |
567 | ||
a388f2af BJ |
568 | case '/': |
569 | ps("\\*/"); | |
570 | break; | |
571 | ||
0b79a041 BJ |
572 | default: |
573 | if (c < 040) | |
574 | putchar('^'), c |= '@'; | |
575 | case '\t': | |
576 | case '\n': | |
577 | putchar(c); | |
578 | } | |
579 | } | |
580 | ||
a388f2af BJ |
581 | /* |
582 | * look for a process beginning on this line | |
b48967e6 | 583 | */ |
a388f2af BJ |
584 | boolean |
585 | isproc(s) | |
586 | char *s; | |
b48967e6 | 587 | { |
a388f2af BJ |
588 | pname[0] = NULL; |
589 | if (!l_toplex || blklevel == 0) | |
590 | if (expmatch (s, l_prcbeg, pname) != NIL) { | |
591 | return (TRUE); | |
592 | } | |
593 | return (FALSE); | |
b48967e6 BJ |
594 | } |
595 | ||
a388f2af | 596 | |
b48967e6 BJ |
597 | /* iskw - check to see if the next word is a keyword |
598 | */ | |
599 | ||
0b79a041 BJ |
600 | iskw(s) |
601 | register char *s; | |
602 | { | |
a388f2af | 603 | register char **ss = l_keywds; |
0b79a041 BJ |
604 | register int i = 1; |
605 | register char *cp = s; | |
606 | ||
607 | while (++cp, isidchr(*cp)) | |
608 | i++; | |
609 | while (cp = *ss++) | |
a388f2af | 610 | if (!STRNCMP(s,cp,i) && !isidchr(cp[i])) |
0b79a041 BJ |
611 | return (i); |
612 | return (0); | |
613 | } |