Annotation of src/usr.bin/vgrind/vfontedpr.c, Revision 1.6
1.6 ! millert 1: /* $OpenBSD: vfontedpr.c,v 1.5 2001/11/19 19:02:17 mpech Exp $ */
1.2 deraadt 2: /* $NetBSD: vfontedpr.c,v 1.4 1996/03/21 18:08:30 jtc 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.6 ! millert 47: static char rcsid[] = "$OpenBSD: vfontedpr.c,v 1.5 2001/11/19 19:02:17 mpech 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.1 deraadt 135: main(argc, argv)
136: int argc;
137: char *argv[];
138: {
139: char *fname = "";
140: struct stat stbuf;
141: char buf[BUFSIZ];
142: char *defs;
143: int needbp = 0;
144:
145: argc--, argv++;
146: do {
147: char *cp;
148: int i;
149:
150: if (argc > 0) {
151: if (!strcmp(argv[0], "-h")) {
152: if (argc == 1) {
153: printf("'ds =H\n");
154: argc = 0;
155: goto rest;
156: }
157: printf("'ds =H %s\n", argv[1]);
158: argc--, argv++;
159: argc--, argv++;
160: if (argc > 0)
161: continue;
162: goto rest;
163: }
164:
165: /* act as a filter like eqn */
166: if (!strcmp(argv[0], "-f")) {
167: filter++;
168: argv[0] = argv[argc-1];
169: argv[argc-1] = "-";
170: continue;
171: }
172:
173: /* take input from the standard place */
174: if (!strcmp(argv[0], "-")) {
175: argc = 0;
176: goto rest;
177: }
178:
179: /* build an index */
180: if (!strcmp(argv[0], "-x")) {
181: idx++;
182: argv[0] = "-n";
183: }
184:
185: /* indicate no keywords */
186: if (!strcmp(argv[0], "-n")) {
187: nokeyw++;
188: argc--, argv++;
189: continue;
190: }
191:
192: /* specify the font size */
193: if (!strncmp(argv[0], "-s", 2)) {
194: i = 0;
195: cp = argv[0] + 2;
196: while (*cp)
197: i = i * 10 + (*cp++ - '0');
198: printf("'ps %d\n'vs %d\n", i, i+1);
199: argc--, argv++;
200: continue;
201: }
202:
203: /* specify the language */
204: if (!strncmp(argv[0], "-l", 2)) {
205: language = argv[0]+2;
206: argc--, argv++;
207: continue;
208: }
209:
210: /* specify the language description file */
211: if (!strncmp(argv[0], "-d", 2)) {
212: defsfile[0] = argv[1];
213: argc--, argv++;
214: argc--, argv++;
215: continue;
216: }
217:
218: /* open the file for input */
219: if (freopen(argv[0], "r", stdin) == NULL) {
220: perror(argv[0]);
221: exit(1);
222: }
223: if (idx)
224: printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
225: fname = argv[0];
226: argc--, argv++;
227: }
228: rest:
229:
230: /*
231: * get the language definition from the defs file
232: */
233: i = cgetent(&defs, defsfile, language);
234: if (i == -1) {
235: fprintf (stderr, "no entry for language %s\n", language);
236: exit (0);
237: } else if (i == -2) { fprintf(stderr,
238: "cannot find vgrindefs file %s\n", defsfile[0]);
239: exit (0);
240: } else if (i == -3) { fprintf(stderr,
241: "potential reference loop detected in vgrindefs file %s\n",
242: defsfile[0]);
243: exit(0);
244: }
245: if (cgetustr(defs, "kw", &cp) == -1)
246: nokeyw = TRUE;
247: else {
248: char **cpp;
249:
250: cpp = l_keywds;
251: while (*cp) {
252: while (*cp == ' ' || *cp =='\t')
253: *cp++ = NULL;
254: if (*cp)
255: *cpp++ = cp;
256: while (*cp != ' ' && *cp != '\t' && *cp)
257: cp++;
258: }
259: *cpp = NIL;
260: }
261: cgetustr(defs, "pb", &cp);
262: l_prcbeg = convexp(cp);
263: cgetustr(defs, "cb", &cp);
264: l_combeg = convexp(cp);
265: cgetustr(defs, "ce", &cp);
266: l_comend = convexp(cp);
267: cgetustr(defs, "ab", &cp);
268: l_acmbeg = convexp(cp);
269: cgetustr(defs, "ae", &cp);
270: l_acmend = convexp(cp);
271: cgetustr(defs, "sb", &cp);
272: l_strbeg = convexp(cp);
273: cgetustr(defs, "se", &cp);
274: l_strend = convexp(cp);
275: cgetustr(defs, "bb", &cp);
276: l_blkbeg = convexp(cp);
277: cgetustr(defs, "be", &cp);
278: l_blkend = convexp(cp);
279: cgetustr(defs, "lb", &cp);
280: l_chrbeg = convexp(cp);
281: cgetustr(defs, "le", &cp);
282: l_chrend = convexp(cp);
283: l_escape = '\\';
284: l_onecase = (cgetcap(defs, "oc", ':') != NULL);
285: l_toplex = (cgetcap(defs, "tl", ':') != NULL);
286:
287: /* initialize the program */
288:
289: incomm = FALSE;
290: instr = FALSE;
291: inchr = FALSE;
292: _escaped = FALSE;
293: blklevel = 0;
294: for (psptr=0; psptr<PSMAX; psptr++) {
295: pstack[psptr][0] = NULL;
296: plstack[psptr] = 0;
297: }
298: psptr = -1;
299: ps("'-F\n");
300: if (!filter) {
301: printf(".ds =F %s\n", fname);
302: ps("'wh 0 vH\n");
303: ps("'wh -1i vF\n");
304: }
305: if (needbp) {
306: needbp = 0;
307: printf(".()\n");
308: printf(".bp\n");
309: }
310: if (!filter) {
311: fstat(fileno(stdin), &stbuf);
312: cp = ctime(&stbuf.st_mtime);
313: cp[16] = '\0';
314: cp[24] = '\0';
315: printf(".ds =M %s %s\n", cp+4, cp+20);
316: }
317:
318: /*
319: * MAIN LOOP!!!
320: */
321: while (fgets(buf, sizeof buf, stdin) != NULL) {
322: if (buf[0] == '\f') {
323: printf(".bp\n");
324: }
325: if (buf[0] == '.') {
326: printf("%s", buf);
327: if (!strncmp (buf+1, "vS", 2))
328: pass = TRUE;
329: if (!strncmp (buf+1, "vE", 2))
330: pass = FALSE;
331: continue;
332: }
333: prccont = FALSE;
334: if (!filter || pass)
335: putScp(buf);
336: else
337: printf("%s", buf);
338: if (prccont && (psptr >= 0)) {
339: ps("'FC ");
340: ps(pstack[psptr]);
341: ps("\n");
342: }
343: #ifdef DEBUG
344: printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
345: #endif
346: margin = 0;
347: }
348: needbp = 1;
349: } while (argc > 0);
350: exit(0);
351: }
352:
353: #define isidchr(c) (isalnum(c) || (c) == '_')
354:
355: static void
356: putScp(os)
357: char *os;
358: {
1.5 mpech 359: char *s = os; /* pointer to unmatched string */
1.1 deraadt 360: char dummy[BUFSIZ]; /* dummy to be used by expmatch */
361: char *comptr; /* end of a comment delimiter */
362: char *acmptr; /* end of a comment delimiter */
363: char *strptr; /* end of a string delimiter */
364: char *chrptr; /* end of a character const delimiter */
365: char *blksptr; /* end of a lexical block start */
366: char *blkeptr; /* end of a lexical block end */
367:
368: _start = os; /* remember the start for expmatch */
369: _escaped = FALSE;
370: if (nokeyw || incomm || instr)
371: goto skip;
372: if (isproc(s)) {
373: ps("'FN ");
374: ps(pname);
375: ps("\n");
376: if (psptr < PSMAX) {
377: ++psptr;
378: strncpy (pstack[psptr], pname, PNAMELEN);
379: pstack[psptr][PNAMELEN] = NULL;
380: plstack[psptr] = blklevel;
381: }
382: }
383: skip:
384: do {
385: /* check for string, comment, blockstart, etc */
386: if (!incomm && !instr && !inchr) {
387:
388: blkeptr = expmatch (s, l_blkend, dummy);
389: blksptr = expmatch (s, l_blkbeg, dummy);
390: comptr = expmatch (s, l_combeg, dummy);
391: acmptr = expmatch (s, l_acmbeg, dummy);
392: strptr = expmatch (s, l_strbeg, dummy);
393: chrptr = expmatch (s, l_chrbeg, dummy);
394:
395: /* start of a comment? */
396: if (comptr != NIL)
397: if ((comptr < strptr || strptr == NIL)
398: && (comptr < acmptr || acmptr == NIL)
399: && (comptr < chrptr || chrptr == NIL)
400: && (comptr < blksptr || blksptr == NIL)
401: && (comptr < blkeptr || blkeptr == NIL)) {
402: putKcp (s, comptr-1, FALSE);
403: s = comptr;
404: incomm = TRUE;
405: comtype = STANDARD;
406: if (s != os)
407: ps ("\\c");
408: ps ("\\c\n'+C\n");
409: continue;
410: }
411:
412: /* start of a comment? */
413: if (acmptr != NIL)
414: if ((acmptr < strptr || strptr == NIL)
415: && (acmptr < chrptr || chrptr == NIL)
416: && (acmptr < blksptr || blksptr == NIL)
417: && (acmptr < blkeptr || blkeptr == NIL)) {
418: putKcp (s, acmptr-1, FALSE);
419: s = acmptr;
420: incomm = TRUE;
421: comtype = ALTERNATE;
422: if (s != os)
423: ps ("\\c");
424: ps ("\\c\n'+C\n");
425: continue;
426: }
427:
428: /* start of a string? */
429: if (strptr != NIL)
430: if ((strptr < chrptr || chrptr == NIL)
431: && (strptr < blksptr || blksptr == NIL)
432: && (strptr < blkeptr || blkeptr == NIL)) {
433: putKcp (s, strptr-1, FALSE);
434: s = strptr;
435: instr = TRUE;
436: continue;
437: }
438:
439: /* start of a character string? */
440: if (chrptr != NIL)
441: if ((chrptr < blksptr || blksptr == NIL)
442: && (chrptr < blkeptr || blkeptr == NIL)) {
443: putKcp (s, chrptr-1, FALSE);
444: s = chrptr;
445: inchr = TRUE;
446: continue;
447: }
448:
449: /* end of a lexical block */
450: if (blkeptr != NIL) {
451: if (blkeptr < blksptr || blksptr == NIL) {
452: putKcp (s, blkeptr - 1, FALSE);
453: s = blkeptr;
454: blklevel--;
455: if (psptr >= 0 && plstack[psptr] >= blklevel) {
456:
457: /* end of current procedure */
458: if (s != os)
459: ps ("\\c");
460: ps ("\\c\n'-F\n");
461: blklevel = plstack[psptr];
462:
463: /* see if we should print the last proc name */
464: if (--psptr >= 0)
465: prccont = TRUE;
466: else
467: psptr = -1;
468: }
469: continue;
470: }
471: }
472:
473: /* start of a lexical block */
474: if (blksptr != NIL) {
475: putKcp (s, blksptr - 1, FALSE);
476: s = blksptr;
477: blklevel++;
478: continue;
479: }
480:
481: /* check for end of comment */
482: } else if (incomm) {
483: comptr = expmatch (s, l_comend, dummy);
484: acmptr = expmatch (s, l_acmend, dummy);
485: if (((comtype == STANDARD) && (comptr != NIL)) ||
486: ((comtype == ALTERNATE) && (acmptr != NIL))) {
487: if (comtype == STANDARD) {
488: putKcp (s, comptr-1, TRUE);
489: s = comptr;
490: } else {
491: putKcp (s, acmptr-1, TRUE);
492: s = acmptr;
493: }
494: incomm = FALSE;
495: ps("\\c\n'-C\n");
496: continue;
497: } else {
498: putKcp (s, s + strlen(s) -1, TRUE);
499: s = s + strlen(s);
500: continue;
501: }
502:
503: /* check for end of string */
504: } else if (instr) {
505: if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
506: putKcp (s, strptr-1, TRUE);
507: s = strptr;
508: instr = FALSE;
509: continue;
510: } else {
511: putKcp (s, s+strlen(s)-1, TRUE);
512: s = s + strlen(s);
513: continue;
514: }
515:
516: /* check for end of character string */
517: } else if (inchr) {
518: if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
519: putKcp (s, chrptr-1, TRUE);
520: s = chrptr;
521: inchr = FALSE;
522: continue;
523: } else {
524: putKcp (s, s+strlen(s)-1, TRUE);
525: s = s + strlen(s);
526: continue;
527: }
528: }
529:
530: /* print out the line */
531: putKcp (s, s + strlen(s) -1, FALSE);
532: s = s + strlen(s);
533: } while (*s);
534: }
535:
536: static void
537: putKcp (start, end, force)
538: char *start; /* start of string to write */
539: char *end; /* end of string to write */
540: boolean force; /* true if we should force nokeyw */
541: {
542: int i;
543: int xfld = 0;
544:
545: while (start <= end) {
546: if (idx) {
547: if (*start == ' ' || *start == '\t') {
548: if (xfld == 0)
549: printf("&");
550: printf("\t");
551: xfld = 1;
552: while (*start == ' ' || *start == '\t')
553: start++;
554: continue;
555: }
556: }
557:
558: /* take care of nice tab stops */
559: if (*start == '\t') {
560: while (*start == '\t')
561: start++;
562: i = tabs(_start, start) - margin / 8;
563: printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
564: continue;
565: }
566:
567: if (!nokeyw && !force)
568: if ((*start == '#' || isidchr(*start))
569: && (start == _start || !isidchr(start[-1]))) {
570: i = iskw(start);
571: if (i > 0) {
572: ps("\\*(+K");
573: do
574: putcp(*start++);
575: while (--i > 0);
576: ps("\\*(-K");
577: continue;
578: }
579: }
580:
581: putcp (*start++);
582: }
583: }
584:
585:
586: static int
587: tabs(s, os)
588: char *s, *os;
589: {
590:
591: return (width(s, os) / 8);
592: }
593:
594: static int
595: width(s, os)
1.5 mpech 596: char *s, *os;
1.1 deraadt 597: {
1.5 mpech 598: int i = 0;
1.1 deraadt 599:
600: while (s < os) {
601: if (*s == '\t') {
602: i = (i + 8) &~ 7;
603: s++;
604: continue;
605: }
606: if (*s < ' ')
607: i += 2;
608: else
609: i++;
610: s++;
611: }
612: return (i);
613: }
614:
615: static void
616: putcp(c)
1.5 mpech 617: int c;
1.1 deraadt 618: {
619:
620: switch(c) {
621:
622: case 0:
623: break;
624:
625: case '\f':
626: break;
627:
628: case '{':
629: ps("\\*(+K{\\*(-K");
630: break;
631:
632: case '}':
633: ps("\\*(+K}\\*(-K");
634: break;
635:
636: case '\\':
637: ps("\\e");
638: break;
639:
640: case '_':
641: ps("\\*_");
642: break;
643:
644: case '-':
645: ps("\\*-");
646: break;
647:
648: case '`':
649: ps("\\`");
650: break;
651:
652: case '\'':
653: ps("\\'");
654: break;
655:
656: case '.':
657: ps("\\&.");
658: break;
659:
660: case '*':
661: ps("\\fI*\\fP");
662: break;
663:
664: case '/':
665: ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
666: break;
667:
668: default:
669: if (c < 040)
670: putchar('^'), c |= '@';
671: case '\t':
672: case '\n':
673: putchar(c);
674: }
675: }
676:
677: /*
678: * look for a process beginning on this line
679: */
680: static boolean
681: isproc(s)
682: char *s;
683: {
684: pname[0] = NULL;
685: if (!l_toplex || blklevel == 0)
686: if (expmatch (s, l_prcbeg, pname) != NIL) {
687: return (TRUE);
688: }
689: return (FALSE);
690: }
691:
692:
693: /* iskw - check to see if the next word is a keyword
694: */
695:
696: static int
697: iskw(s)
1.5 mpech 698: char *s;
1.1 deraadt 699: {
1.5 mpech 700: char **ss = l_keywds;
701: int i = 1;
702: char *cp = s;
1.1 deraadt 703:
704: while (++cp, isidchr(*cp))
705: i++;
1.4 deraadt 706: while ((cp = *ss++))
1.1 deraadt 707: if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))
708: return (i);
709: return (0);
710: }
711: