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