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