2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)vfontedpr.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/types.h>
52 #include "pathnames.h"
64 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
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 */
72 static int iskw(char *);
73 static boolean isproc(char *);
74 static void putKcp(char *, char *, boolean);
75 static void putScp(char *);
76 static void putcp(int);
77 static int tabs(char *, char *);
78 static int width(char *, char *);
84 static boolean filter = FALSE; /* act as a filter (like eqn) */
85 static boolean inchr; /* in a string constant */
86 static boolean incomm; /* in a comment of the primary type */
87 static boolean idx = FALSE; /* form an index */
88 static boolean instr; /* in a string constant */
89 static boolean nokeyw = FALSE; /* no keywords being flagged */
90 static boolean pass = FALSE; /*
91 * when acting as a filter, pass indicates
92 * whether we are currently processing
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 */
101 static int plstack[PSMAX]; /* the procedure nesting level stack */
102 static char pname[BUFSIZ+1];
103 static boolean 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 */
108 * The language specific globals
111 char *l_acmbeg; /* string introducing a comment */
112 char *l_acmend; /* string ending a comment */
113 char *l_blkbeg; /* string begining 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 boolean l_toplex; /* procedures only defined at top lex level */
126 const char *language = "c"; /* the language indicator */
128 #define ps(x) printf("%s", x)
135 const char *fname = "";
147 if (!strcmp(argv[0], "-h")) {
153 printf("'ds =H %s\n", argv[1]);
161 /* act as a filter like eqn */
162 if (!strcmp(argv[0], "-f")) {
164 argv[0] = argv[argc-1];
165 argv[argc-1] = strdup("-");
169 /* take input from the standard place */
170 if (!strcmp(argv[0], "-")) {
176 if (!strcmp(argv[0], "-x")) {
178 argv[0] = strdup("-n");
181 /* indicate no keywords */
182 if (!strcmp(argv[0], "-n")) {
188 /* specify the font size */
189 if (!strncmp(argv[0], "-s", 2)) {
193 i = i * 10 + (*cp++ - '0');
194 printf("'ps %d\n'vs %d\n", i, i+1);
199 /* specify the language */
200 if (!strncmp(argv[0], "-l", 2)) {
201 language = argv[0]+2;
206 /* specify the language description file */
207 if (!strncmp(argv[0], "-d", 2)) {
208 defsfile[0] = argv[1];
214 /* open the file for input */
215 if (freopen(argv[0], "r", stdin) == NULL)
216 err(1, "%s", argv[0]);
218 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
225 * get the language definition from the defs file
227 i = cgetent(&defs, defsfile, language);
229 fprintf (stderr, "no entry for language %s\n", language);
231 } else if (i == -2) { fprintf(stderr,
232 "cannot find vgrindefs file %s\n", defsfile[0]);
234 } else if (i == -3) { fprintf(stderr,
235 "potential reference loop detected in vgrindefs file %s\n",
239 if (cgetustr(defs, "kw", &cp) == -1)
246 while (*cp == ' ' || *cp =='\t')
250 while (*cp != ' ' && *cp != '\t' && *cp)
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);
280 l_onecase = (cgetcap(defs, "oc", ':') != NULL);
281 l_toplex = (cgetcap(defs, "tl", ':') != NULL);
283 /* initialize the program */
290 for (psptr=0; psptr<PSMAX; psptr++) {
291 pstack[psptr][0] = '\0';
297 printf(".ds =F %s\n", fname);
307 fstat(fileno(stdin), &stbuf);
308 cp = ctime(&stbuf.st_mtime);
311 printf(".ds =M %s %s\n", cp+4, cp+20);
317 while (fgets(buf, sizeof buf, stdin) != NULL) {
318 if (buf[0] == '\f') {
323 if (!strncmp (buf+1, "vS", 2))
325 if (!strncmp (buf+1, "vE", 2))
334 if (prccont && (psptr >= 0)) {
340 printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
349 #define isidchr(c) (isalnum(c) || (c) == '_')
355 register char *s = os; /* pointer to unmatched string */
356 char dummy[BUFSIZ]; /* dummy to be used by expmatch */
357 char *comptr; /* end of a comment delimiter */
358 char *acmptr; /* end of a comment delimiter */
359 char *strptr; /* end of a string delimiter */
360 char *chrptr; /* end of a character const delimiter */
361 char *blksptr; /* end of a lexical block start */
362 char *blkeptr; /* end of a lexical block end */
363 char *nocomptr; /* end of a non-comment delimiter */
365 s_start = os; /* remember the start for expmatch */
367 if (nokeyw || incomm || instr)
375 strncpy (pstack[psptr], pname, PNAMELEN);
376 pstack[psptr][PNAMELEN] = '\0';
377 plstack[psptr] = blklevel;
382 /* check for string, comment, blockstart, etc */
383 if (!incomm && !instr && !inchr) {
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 nocomptr = expmatch (s, l_nocom, dummy);
393 /* start of non-comment? */
395 if ((nocomptr <= comptr || comptr == NIL)
396 && (nocomptr <= acmptr || acmptr == NIL)) {
397 /* continue after non-comment */
398 putKcp (s, nocomptr-1, FALSE);
403 /* start of a comment? */
405 if ((comptr < strptr || strptr == NIL)
406 && (comptr < acmptr || acmptr == NIL)
407 && (comptr < chrptr || chrptr == NIL)
408 && (comptr < blksptr || blksptr == NIL)
409 && (comptr < blkeptr || blkeptr == NIL)) {
410 putKcp (s, comptr-1, FALSE);
420 /* start of a comment? */
422 if ((acmptr < strptr || strptr == NIL)
423 && (acmptr < chrptr || chrptr == NIL)
424 && (acmptr < blksptr || blksptr == NIL)
425 && (acmptr < blkeptr || blkeptr == NIL)) {
426 putKcp (s, acmptr-1, FALSE);
436 /* start of a string? */
438 if ((strptr < chrptr || chrptr == NIL)
439 && (strptr < blksptr || blksptr == NIL)
440 && (strptr < blkeptr || blkeptr == NIL)) {
441 putKcp (s, strptr-1, FALSE);
447 /* start of a character string? */
449 if ((chrptr < blksptr || blksptr == NIL)
450 && (chrptr < blkeptr || blkeptr == NIL)) {
451 putKcp (s, chrptr-1, FALSE);
457 /* end of a lexical block */
458 if (blkeptr != NIL) {
459 if (blkeptr < blksptr || blksptr == NIL) {
460 putKcp (s, blkeptr - 1, FALSE);
462 if (blklevel > 0 /* sanity */)
464 if (psptr >= 0 && plstack[psptr] >= blklevel) {
466 /* end of current procedure */
470 blklevel = plstack[psptr];
472 /* see if we should print the last proc name */
482 /* start of a lexical block */
483 if (blksptr != NIL) {
484 putKcp (s, blksptr - 1, FALSE);
490 /* check for end of comment */
492 comptr = expmatch (s, l_comend, dummy);
493 acmptr = expmatch (s, l_acmend, dummy);
494 if (((comtype == STANDARD) && (comptr != NIL)) ||
495 ((comtype == ALTERNATE) && (acmptr != NIL))) {
496 if (comtype == STANDARD) {
497 putKcp (s, comptr-1, TRUE);
500 putKcp (s, acmptr-1, TRUE);
507 putKcp (s, s + strlen(s) -1, TRUE);
512 /* check for end of string */
514 if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
515 putKcp (s, strptr-1, TRUE);
520 putKcp (s, s+strlen(s)-1, TRUE);
525 /* check for end of character string */
527 if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
528 putKcp (s, chrptr-1, TRUE);
533 putKcp (s, s+strlen(s)-1, TRUE);
539 /* print out the line */
540 putKcp (s, s + strlen(s) -1, FALSE);
546 putKcp (start, end, force)
547 char *start; /* start of string to write */
548 char *end; /* end of string to write */
549 boolean force; /* true if we should force nokeyw */
554 while (start <= end) {
556 if (*start == ' ' || *start == '\t') {
561 while (*start == ' ' || *start == '\t')
567 /* take care of nice tab stops */
568 if (*start == '\t') {
569 while (*start == '\t')
571 i = tabs(s_start, start) - margin / 8;
572 printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
576 if (!nokeyw && !force)
577 if ((*start == '#' || isidchr(*start))
578 && (start == s_start || !isidchr(start[-1]))) {
583 putcp((unsigned char)*start++);
590 putcp ((unsigned char)*start++);
600 return (width(s, os) / 8);
605 register char *s, *os;
677 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
682 putchar('^'), c |= '@';
690 * look for a process beginning on this line
697 if (!l_toplex || blklevel == 0)
698 if (expmatch (s, l_prcbeg, pname) != NIL) {
705 /* iskw - check to see if the next word is a keyword
712 register char **ss = l_keywds;
714 register char *cp = s;
716 while (++cp, isidchr(*cp))
719 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))