Annotation of src/usr.bin/vgrind/vfontedpr.c, Revision 1.8
1.8 ! deraadt 1: /* $OpenBSD: vfontedpr.c,v 1.7 2003/02/19 07:32:36 deraadt Exp $ */
1.7 deraadt 2: /* $NetBSD: vfontedpr.c,v 1.7 1998/12/19 23:41:53 christos Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1980, 1993\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)vfontedpr.c 8.1 (Berkeley) 6/6/93";
46: #endif
1.8 ! deraadt 47: static char rcsid[] = "$OpenBSD: vfontedpr.c,v 1.7 2003/02/19 07:32:36 deraadt Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <sys/types.h>
51: #include <sys/stat.h>
52: #include <time.h>
53: #include <ctype.h>
54: #include <stdlib.h>
55: #include <string.h>
56: #include <stdio.h>
57: #include "pathnames.h"
58: #include "extern.h"
59:
60: #define FALSE 0
61: #define TRUE !(FALSE)
62: #define NIL 0
63: #define STANDARD 0
64: #define ALTERNATE 1
65:
66: /*
67: * Vfontedpr.
68: *
69: * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
70: *
71: */
72:
73: #define STRLEN 10 /* length of strings introducing things */
74: #define PNAMELEN 40 /* length of a function/procedure name */
75: #define PSMAX 20 /* size of procedure name stacking */
76:
1.6 millert 77: static int iskw(char *);
78: static boolean isproc(char *);
79: static void putKcp(char *, char *, boolean);
80: static void putScp(char *);
81: static void putcp(int);
82: static int tabs(char *, char *);
83: static int width(char *, char *);
1.1 deraadt 84:
85: /*
86: * The state variables
87: */
88:
89: static boolean filter = FALSE; /* act as a filter (like eqn) */
90: static boolean inchr; /* in a string constant */
91: static boolean incomm; /* in a comment of the primary type */
92: static boolean idx = FALSE; /* form an index */
93: static boolean instr; /* in a string constant */
94: static boolean nokeyw = FALSE; /* no keywords being flagged */
95: static boolean pass = FALSE; /*
96: * when acting as a filter, pass indicates
97: * whether we are currently processing
98: * input.
99: */
100:
101: static int blklevel; /* current nesting level */
102: static int comtype; /* type of comment */
103: static char *defsfile[2] = { _PATH_VGRINDEFS, 0 };
104: /* name of language definitions file */
105: static int margin;
106: static int plstack[PSMAX]; /* the procedure nesting level stack */
107: static char pname[BUFSIZ+1];
108: static boolean prccont; /* continue last procedure */
109: static int psptr; /* the stack index of the current procedure */
110: static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
111:
112: /*
113: * The language specific globals
114: */
115:
116: char *l_acmbeg; /* string introducing a comment */
117: char *l_acmend; /* string ending a comment */
118: char *l_blkbeg; /* string begining of a block */
119: char *l_blkend; /* string ending a block */
120: char *l_chrbeg; /* delimiter for character constant */
121: char *l_chrend; /* delimiter for character constant */
122: char *l_combeg; /* string introducing a comment */
123: char *l_comend; /* string ending a comment */
124: char l_escape; /* character used to escape characters */
125: char *l_keywds[BUFSIZ/2]; /* keyword table address */
126: char *l_prcbeg; /* regular expr for procedure begin */
127: char *l_strbeg; /* delimiter for string constant */
128: char *l_strend; /* delimiter for string constant */
129: boolean l_toplex; /* procedures only defined at top lex level */
130: char *language = "c"; /* the language indicator */
131:
132: #define ps(x) printf("%s", x)
133:
1.2 deraadt 134: int
1.8 ! deraadt 135: main(int argc, char *argv[])
1.1 deraadt 136: {
137: char *fname = "";
138: struct stat stbuf;
139: char buf[BUFSIZ];
140: char *defs;
141: int needbp = 0;
142:
143: argc--, argv++;
144: do {
145: char *cp;
146: int i;
147:
148: if (argc > 0) {
149: if (!strcmp(argv[0], "-h")) {
150: if (argc == 1) {
151: printf("'ds =H\n");
152: argc = 0;
153: goto rest;
154: }
155: printf("'ds =H %s\n", argv[1]);
156: argc--, argv++;
157: argc--, argv++;
158: if (argc > 0)
159: continue;
160: goto rest;
161: }
162:
163: /* act as a filter like eqn */
164: if (!strcmp(argv[0], "-f")) {
165: filter++;
166: argv[0] = argv[argc-1];
167: argv[argc-1] = "-";
168: continue;
169: }
170:
171: /* take input from the standard place */
172: if (!strcmp(argv[0], "-")) {
173: argc = 0;
174: goto rest;
175: }
176:
177: /* build an index */
178: if (!strcmp(argv[0], "-x")) {
179: idx++;
180: argv[0] = "-n";
181: }
182:
183: /* indicate no keywords */
184: if (!strcmp(argv[0], "-n")) {
185: nokeyw++;
186: argc--, argv++;
187: continue;
188: }
189:
190: /* specify the font size */
191: if (!strncmp(argv[0], "-s", 2)) {
192: i = 0;
193: cp = argv[0] + 2;
194: while (*cp)
195: i = i * 10 + (*cp++ - '0');
196: printf("'ps %d\n'vs %d\n", i, i+1);
197: argc--, argv++;
198: continue;
199: }
200:
201: /* specify the language */
202: if (!strncmp(argv[0], "-l", 2)) {
203: language = argv[0]+2;
204: argc--, argv++;
205: continue;
206: }
207:
208: /* specify the language description file */
209: if (!strncmp(argv[0], "-d", 2)) {
210: defsfile[0] = argv[1];
211: argc--, argv++;
212: argc--, argv++;
213: continue;
214: }
215:
216: /* open the file for input */
217: if (freopen(argv[0], "r", stdin) == NULL) {
218: perror(argv[0]);
219: exit(1);
220: }
221: if (idx)
222: printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
223: fname = argv[0];
224: argc--, argv++;
225: }
226: rest:
227:
228: /*
229: * get the language definition from the defs file
230: */
231: i = cgetent(&defs, defsfile, language);
232: if (i == -1) {
233: fprintf (stderr, "no entry for language %s\n", language);
234: exit (0);
235: } else if (i == -2) { fprintf(stderr,
236: "cannot find vgrindefs file %s\n", defsfile[0]);
237: exit (0);
238: } else if (i == -3) { fprintf(stderr,
239: "potential reference loop detected in vgrindefs file %s\n",
240: defsfile[0]);
241: exit(0);
242: }
243: if (cgetustr(defs, "kw", &cp) == -1)
244: nokeyw = TRUE;
245: else {
246: char **cpp;
247:
248: cpp = l_keywds;
249: while (*cp) {
250: while (*cp == ' ' || *cp =='\t')
251: *cp++ = NULL;
252: if (*cp)
253: *cpp++ = cp;
254: while (*cp != ' ' && *cp != '\t' && *cp)
255: cp++;
256: }
257: *cpp = NIL;
258: }
259: cgetustr(defs, "pb", &cp);
260: l_prcbeg = convexp(cp);
261: cgetustr(defs, "cb", &cp);
262: l_combeg = convexp(cp);
263: cgetustr(defs, "ce", &cp);
264: l_comend = convexp(cp);
265: cgetustr(defs, "ab", &cp);
266: l_acmbeg = convexp(cp);
267: cgetustr(defs, "ae", &cp);
268: l_acmend = convexp(cp);
269: cgetustr(defs, "sb", &cp);
270: l_strbeg = convexp(cp);
271: cgetustr(defs, "se", &cp);
272: l_strend = convexp(cp);
273: cgetustr(defs, "bb", &cp);
274: l_blkbeg = convexp(cp);
275: cgetustr(defs, "be", &cp);
276: l_blkend = convexp(cp);
277: cgetustr(defs, "lb", &cp);
278: l_chrbeg = convexp(cp);
279: cgetustr(defs, "le", &cp);
280: l_chrend = convexp(cp);
281: l_escape = '\\';
282: l_onecase = (cgetcap(defs, "oc", ':') != NULL);
283: l_toplex = (cgetcap(defs, "tl", ':') != NULL);
284:
285: /* initialize the program */
286:
287: incomm = FALSE;
288: instr = FALSE;
289: inchr = FALSE;
1.7 deraadt 290: x_escaped = FALSE;
1.1 deraadt 291: blklevel = 0;
292: for (psptr=0; psptr<PSMAX; psptr++) {
293: pstack[psptr][0] = NULL;
294: plstack[psptr] = 0;
295: }
296: psptr = -1;
297: ps("'-F\n");
298: if (!filter) {
299: printf(".ds =F %s\n", fname);
300: ps("'wh 0 vH\n");
301: ps("'wh -1i vF\n");
302: }
303: if (needbp) {
304: needbp = 0;
305: printf(".()\n");
306: printf(".bp\n");
307: }
308: if (!filter) {
309: fstat(fileno(stdin), &stbuf);
310: cp = ctime(&stbuf.st_mtime);
311: cp[16] = '\0';
312: cp[24] = '\0';
313: printf(".ds =M %s %s\n", cp+4, cp+20);
314: }
315:
316: /*
317: * MAIN LOOP!!!
318: */
319: while (fgets(buf, sizeof buf, stdin) != NULL) {
320: if (buf[0] == '\f') {
321: printf(".bp\n");
322: }
323: if (buf[0] == '.') {
324: printf("%s", buf);
325: if (!strncmp (buf+1, "vS", 2))
326: pass = TRUE;
327: if (!strncmp (buf+1, "vE", 2))
328: pass = FALSE;
329: continue;
330: }
331: prccont = FALSE;
332: if (!filter || pass)
333: putScp(buf);
334: else
335: printf("%s", buf);
336: if (prccont && (psptr >= 0)) {
337: ps("'FC ");
338: ps(pstack[psptr]);
339: ps("\n");
340: }
341: #ifdef DEBUG
342: printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
343: #endif
344: margin = 0;
345: }
346: needbp = 1;
347: } while (argc > 0);
348: exit(0);
349: }
350:
351: #define isidchr(c) (isalnum(c) || (c) == '_')
352:
353: static void
1.8 ! deraadt 354: putScp(char *os)
1.1 deraadt 355: {
1.5 mpech 356: char *s = os; /* pointer to unmatched string */
1.1 deraadt 357: char dummy[BUFSIZ]; /* dummy to be used by expmatch */
358: char *comptr; /* end of a comment delimiter */
359: char *acmptr; /* end of a comment delimiter */
360: char *strptr; /* end of a string delimiter */
361: char *chrptr; /* end of a character const delimiter */
362: char *blksptr; /* end of a lexical block start */
363: char *blkeptr; /* end of a lexical block end */
364:
1.7 deraadt 365: x_start = os; /* remember the start for expmatch */
366: x_escaped = FALSE;
1.1 deraadt 367: if (nokeyw || incomm || instr)
368: goto skip;
369: if (isproc(s)) {
370: ps("'FN ");
371: ps(pname);
372: ps("\n");
373: if (psptr < PSMAX) {
374: ++psptr;
375: strncpy (pstack[psptr], pname, PNAMELEN);
376: pstack[psptr][PNAMELEN] = NULL;
377: plstack[psptr] = blklevel;
378: }
379: }
380: skip:
381: do {
382: /* check for string, comment, blockstart, etc */
383: if (!incomm && !instr && !inchr) {
384:
385: blkeptr = expmatch (s, l_blkend, dummy);
386: blksptr = expmatch (s, l_blkbeg, dummy);
387: comptr = expmatch (s, l_combeg, dummy);
388: acmptr = expmatch (s, l_acmbeg, dummy);
389: strptr = expmatch (s, l_strbeg, dummy);
390: chrptr = expmatch (s, l_chrbeg, dummy);
391:
392: /* start of a comment? */
393: if (comptr != NIL)
394: if ((comptr < strptr || strptr == NIL)
395: && (comptr < acmptr || acmptr == NIL)
396: && (comptr < chrptr || chrptr == NIL)
397: && (comptr < blksptr || blksptr == NIL)
398: && (comptr < blkeptr || blkeptr == NIL)) {
399: putKcp (s, comptr-1, FALSE);
400: s = comptr;
401: incomm = TRUE;
402: comtype = STANDARD;
403: if (s != os)
404: ps ("\\c");
405: ps ("\\c\n'+C\n");
406: continue;
407: }
408:
409: /* start of a comment? */
410: if (acmptr != NIL)
411: if ((acmptr < strptr || strptr == NIL)
412: && (acmptr < chrptr || chrptr == NIL)
413: && (acmptr < blksptr || blksptr == NIL)
414: && (acmptr < blkeptr || blkeptr == NIL)) {
415: putKcp (s, acmptr-1, FALSE);
416: s = acmptr;
417: incomm = TRUE;
418: comtype = ALTERNATE;
419: if (s != os)
420: ps ("\\c");
421: ps ("\\c\n'+C\n");
422: continue;
423: }
424:
425: /* start of a string? */
426: if (strptr != NIL)
427: if ((strptr < chrptr || chrptr == NIL)
428: && (strptr < blksptr || blksptr == NIL)
429: && (strptr < blkeptr || blkeptr == NIL)) {
430: putKcp (s, strptr-1, FALSE);
431: s = strptr;
432: instr = TRUE;
433: continue;
434: }
435:
436: /* start of a character string? */
437: if (chrptr != NIL)
438: if ((chrptr < blksptr || blksptr == NIL)
439: && (chrptr < blkeptr || blkeptr == NIL)) {
440: putKcp (s, chrptr-1, FALSE);
441: s = chrptr;
442: inchr = TRUE;
443: continue;
444: }
445:
446: /* end of a lexical block */
447: if (blkeptr != NIL) {
448: if (blkeptr < blksptr || blksptr == NIL) {
449: putKcp (s, blkeptr - 1, FALSE);
450: s = blkeptr;
451: blklevel--;
452: if (psptr >= 0 && plstack[psptr] >= blklevel) {
453:
454: /* end of current procedure */
455: if (s != os)
456: ps ("\\c");
457: ps ("\\c\n'-F\n");
458: blklevel = plstack[psptr];
459:
460: /* see if we should print the last proc name */
461: if (--psptr >= 0)
462: prccont = TRUE;
463: else
464: psptr = -1;
465: }
466: continue;
467: }
468: }
469:
470: /* start of a lexical block */
471: if (blksptr != NIL) {
472: putKcp (s, blksptr - 1, FALSE);
473: s = blksptr;
474: blklevel++;
475: continue;
476: }
477:
478: /* check for end of comment */
479: } else if (incomm) {
480: comptr = expmatch (s, l_comend, dummy);
481: acmptr = expmatch (s, l_acmend, dummy);
482: if (((comtype == STANDARD) && (comptr != NIL)) ||
483: ((comtype == ALTERNATE) && (acmptr != NIL))) {
484: if (comtype == STANDARD) {
485: putKcp (s, comptr-1, TRUE);
486: s = comptr;
487: } else {
488: putKcp (s, acmptr-1, TRUE);
489: s = acmptr;
490: }
491: incomm = FALSE;
492: ps("\\c\n'-C\n");
493: continue;
494: } else {
495: putKcp (s, s + strlen(s) -1, TRUE);
496: s = s + strlen(s);
497: continue;
498: }
499:
500: /* check for end of string */
501: } else if (instr) {
502: if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
503: putKcp (s, strptr-1, TRUE);
504: s = strptr;
505: instr = FALSE;
506: continue;
507: } else {
508: putKcp (s, s+strlen(s)-1, TRUE);
509: s = s + strlen(s);
510: continue;
511: }
512:
513: /* check for end of character string */
514: } else if (inchr) {
515: if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
516: putKcp (s, chrptr-1, TRUE);
517: s = chrptr;
518: inchr = FALSE;
519: continue;
520: } else {
521: putKcp (s, s+strlen(s)-1, TRUE);
522: s = s + strlen(s);
523: continue;
524: }
525: }
526:
527: /* print out the line */
528: putKcp (s, s + strlen(s) -1, FALSE);
529: s = s + strlen(s);
530: } while (*s);
531: }
532:
533: static void
1.8 ! deraadt 534: putKcp(char *start, char *end, boolean force)
1.1 deraadt 535: {
536: int i;
537: int xfld = 0;
538:
539: while (start <= end) {
540: if (idx) {
541: if (*start == ' ' || *start == '\t') {
542: if (xfld == 0)
543: printf("&");
544: printf("\t");
545: xfld = 1;
546: while (*start == ' ' || *start == '\t')
547: start++;
548: continue;
549: }
550: }
551:
552: /* take care of nice tab stops */
553: if (*start == '\t') {
554: while (*start == '\t')
555: start++;
1.7 deraadt 556: i = tabs(x_start, start) - margin / 8;
1.1 deraadt 557: printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
558: continue;
559: }
560:
561: if (!nokeyw && !force)
562: if ((*start == '#' || isidchr(*start))
1.7 deraadt 563: && (start == x_start || !isidchr(start[-1]))) {
1.1 deraadt 564: i = iskw(start);
565: if (i > 0) {
566: ps("\\*(+K");
567: do
568: putcp(*start++);
569: while (--i > 0);
570: ps("\\*(-K");
571: continue;
572: }
573: }
574:
575: putcp (*start++);
576: }
577: }
578:
579:
580: static int
1.8 ! deraadt 581: tabs(char *s, char *os)
1.1 deraadt 582: {
583:
584: return (width(s, os) / 8);
585: }
586:
587: static int
1.8 ! deraadt 588: width(char *s, char *os)
1.1 deraadt 589: {
1.5 mpech 590: int i = 0;
1.1 deraadt 591:
592: while (s < os) {
593: if (*s == '\t') {
594: i = (i + 8) &~ 7;
595: s++;
596: continue;
597: }
598: if (*s < ' ')
599: i += 2;
600: else
601: i++;
602: s++;
603: }
604: return (i);
605: }
606:
607: static void
1.8 ! deraadt 608: putcp(int c)
1.1 deraadt 609: {
610:
611: switch(c) {
612:
613: case 0:
614: break;
615:
616: case '\f':
617: break;
618:
619: case '{':
620: ps("\\*(+K{\\*(-K");
621: break;
622:
623: case '}':
624: ps("\\*(+K}\\*(-K");
625: break;
626:
627: case '\\':
628: ps("\\e");
629: break;
630:
631: case '_':
632: ps("\\*_");
633: break;
634:
635: case '-':
636: ps("\\*-");
637: break;
638:
639: case '`':
640: ps("\\`");
641: break;
642:
643: case '\'':
644: ps("\\'");
645: break;
646:
647: case '.':
648: ps("\\&.");
649: break;
650:
651: case '*':
652: ps("\\fI*\\fP");
653: break;
654:
655: case '/':
656: ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
657: break;
658:
659: default:
660: if (c < 040)
661: putchar('^'), c |= '@';
662: case '\t':
663: case '\n':
664: putchar(c);
665: }
666: }
667:
668: /*
669: * look for a process beginning on this line
670: */
671: static boolean
1.8 ! deraadt 672: isproc(char *s)
1.1 deraadt 673: {
674: pname[0] = NULL;
675: if (!l_toplex || blklevel == 0)
676: if (expmatch (s, l_prcbeg, pname) != NIL) {
677: return (TRUE);
678: }
679: return (FALSE);
680: }
681:
682:
683: /* iskw - check to see if the next word is a keyword
684: */
685:
686: static int
1.8 ! deraadt 687: iskw(char *s)
1.1 deraadt 688: {
1.5 mpech 689: char **ss = l_keywds;
690: int i = 1;
691: char *cp = s;
1.1 deraadt 692:
693: while (++cp, isidchr(*cp))
694: i++;
1.4 deraadt 695: while ((cp = *ss++))
1.1 deraadt 696: if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))
697: return (i);
698: return (0);
699: }
700: