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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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.
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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)vfontedpr.c 8.1 (Berkeley) 6/6/93";
48 #include <sys/types.h>
56 #include "pathnames.h"
68 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
72 #define STRLEN 10 /* length of strings introducing things */
73 #define PNAMELEN 40 /* length of a function/procedure name */
74 #define PSMAX 20 /* size of procedure name stacking */
76 static int iskw(char *);
77 static boolean isproc(char *);
78 static void putKcp(char *, char *, boolean);
79 static void putScp(char *);
80 static void putcp(int);
81 static int tabs(char *, char *);
82 static int width(char *, char *);
88 static boolean filter = FALSE; /* act as a filter (like eqn) */
89 static boolean inchr; /* in a string constant */
90 static boolean incomm; /* in a comment of the primary type */
91 static boolean idx = FALSE; /* form an index */
92 static boolean instr; /* in a string constant */
93 static boolean nokeyw = FALSE; /* no keywords being flagged */
94 static boolean pass = FALSE; /*
95 * when acting as a filter, pass indicates
96 * whether we are currently processing
100 static int blklevel; /* current nesting level */
101 static int comtype; /* type of comment */
102 static const char *defsfile[2] = { _PATH_VGRINDEFS, 0 };
103 /* name of language definitions file */
105 static int plstack[PSMAX]; /* the procedure nesting level stack */
106 static char pname[BUFSIZ+1];
107 static boolean prccont; /* continue last procedure */
108 static int psptr; /* the stack index of the current procedure */
109 static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
112 * The language specific globals
115 char *l_acmbeg; /* string introducing a comment */
116 char *l_acmend; /* string ending a comment */
117 char *l_blkbeg; /* string begining of a block */
118 char *l_blkend; /* string ending a block */
119 char *l_chrbeg; /* delimiter for character constant */
120 char *l_chrend; /* delimiter for character constant */
121 char *l_combeg; /* string introducing a comment */
122 char *l_comend; /* string ending a comment */
123 char l_escape; /* character used to escape characters */
124 char *l_keywds[BUFSIZ/2]; /* keyword table address */
125 char *l_nocom; /* regexp for non-comments */
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 const char *language = "c"; /* the language indicator */
132 #define ps(x) printf("%s", x)
139 const char *fname = "";
151 if (!strcmp(argv[0], "-h")) {
157 printf("'ds =H %s\n", argv[1]);
165 /* act as a filter like eqn */
166 if (!strcmp(argv[0], "-f")) {
168 argv[0] = argv[argc-1];
169 argv[argc-1] = strdup("-");
173 /* take input from the standard place */
174 if (!strcmp(argv[0], "-")) {
180 if (!strcmp(argv[0], "-x")) {
182 argv[0] = strdup("-n");
185 /* indicate no keywords */
186 if (!strcmp(argv[0], "-n")) {
192 /* specify the font size */
193 if (!strncmp(argv[0], "-s", 2)) {
197 i = i * 10 + (*cp++ - '0');
198 printf("'ps %d\n'vs %d\n", i, i+1);
203 /* specify the language */
204 if (!strncmp(argv[0], "-l", 2)) {
205 language = argv[0]+2;
210 /* specify the language description file */
211 if (!strncmp(argv[0], "-d", 2)) {
212 defsfile[0] = argv[1];
218 /* open the file for input */
219 if (freopen(argv[0], "r", stdin) == NULL)
220 err(1, "%s", argv[0]);
222 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
229 * get the language definition from the defs file
231 i = cgetent(&defs, defsfile, language);
233 fprintf (stderr, "no entry for language %s\n", language);
235 } else if (i == -2) { fprintf(stderr,
236 "cannot find vgrindefs file %s\n", defsfile[0]);
238 } else if (i == -3) { fprintf(stderr,
239 "potential reference loop detected in vgrindefs file %s\n",
243 if (cgetustr(defs, "kw", &cp) == -1)
250 while (*cp == ' ' || *cp =='\t')
254 while (*cp != ' ' && *cp != '\t' && *cp)
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 if (cgetustr(defs, "nc", &cp) >= 0)
282 l_nocom = convexp(cp);
284 l_onecase = (cgetcap(defs, "oc", ':') != NULL);
285 l_toplex = (cgetcap(defs, "tl", ':') != NULL);
287 /* initialize the program */
294 for (psptr=0; psptr<PSMAX; psptr++) {
295 pstack[psptr][0] = '\0';
301 printf(".ds =F %s\n", fname);
311 fstat(fileno(stdin), &stbuf);
312 cp = ctime(&stbuf.st_mtime);
315 printf(".ds =M %s %s\n", cp+4, cp+20);
321 while (fgets(buf, sizeof buf, stdin) != NULL) {
322 if (buf[0] == '\f') {
327 if (!strncmp (buf+1, "vS", 2))
329 if (!strncmp (buf+1, "vE", 2))
338 if (prccont && (psptr >= 0)) {
344 printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
353 #define isidchr(c) (isalnum(c) || (c) == '_')
359 register char *s = os; /* pointer to unmatched string */
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 char *nocomptr; /* end of a non-comment delimiter */
369 s_start = os; /* remember the start for expmatch */
371 if (nokeyw || incomm || instr)
379 strncpy (pstack[psptr], pname, PNAMELEN);
380 pstack[psptr][PNAMELEN] = '\0';
381 plstack[psptr] = blklevel;
386 /* check for string, comment, blockstart, etc */
387 if (!incomm && !instr && !inchr) {
389 blkeptr = expmatch (s, l_blkend, dummy);
390 blksptr = expmatch (s, l_blkbeg, dummy);
391 comptr = expmatch (s, l_combeg, dummy);
392 acmptr = expmatch (s, l_acmbeg, dummy);
393 strptr = expmatch (s, l_strbeg, dummy);
394 chrptr = expmatch (s, l_chrbeg, dummy);
395 nocomptr = expmatch (s, l_nocom, dummy);
397 /* start of non-comment? */
399 if ((nocomptr <= comptr || comptr == NIL)
400 && (nocomptr <= acmptr || acmptr == NIL)) {
401 /* continue after non-comment */
402 putKcp (s, nocomptr-1, FALSE);
407 /* start of a comment? */
409 if ((comptr < strptr || strptr == NIL)
410 && (comptr < acmptr || acmptr == NIL)
411 && (comptr < chrptr || chrptr == NIL)
412 && (comptr < blksptr || blksptr == NIL)
413 && (comptr < blkeptr || blkeptr == NIL)) {
414 putKcp (s, comptr-1, FALSE);
424 /* start of a comment? */
426 if ((acmptr < strptr || strptr == NIL)
427 && (acmptr < chrptr || chrptr == NIL)
428 && (acmptr < blksptr || blksptr == NIL)
429 && (acmptr < blkeptr || blkeptr == NIL)) {
430 putKcp (s, acmptr-1, FALSE);
440 /* start of a string? */
442 if ((strptr < chrptr || chrptr == NIL)
443 && (strptr < blksptr || blksptr == NIL)
444 && (strptr < blkeptr || blkeptr == NIL)) {
445 putKcp (s, strptr-1, FALSE);
451 /* start of a character string? */
453 if ((chrptr < blksptr || blksptr == NIL)
454 && (chrptr < blkeptr || blkeptr == NIL)) {
455 putKcp (s, chrptr-1, FALSE);
461 /* end of a lexical block */
462 if (blkeptr != NIL) {
463 if (blkeptr < blksptr || blksptr == NIL) {
464 putKcp (s, blkeptr - 1, FALSE);
466 if (blklevel > 0 /* sanity */)
468 if (psptr >= 0 && plstack[psptr] >= blklevel) {
470 /* end of current procedure */
474 blklevel = plstack[psptr];
476 /* see if we should print the last proc name */
486 /* start of a lexical block */
487 if (blksptr != NIL) {
488 putKcp (s, blksptr - 1, FALSE);
494 /* check for end of comment */
496 comptr = expmatch (s, l_comend, dummy);
497 acmptr = expmatch (s, l_acmend, dummy);
498 if (((comtype == STANDARD) && (comptr != NIL)) ||
499 ((comtype == ALTERNATE) && (acmptr != NIL))) {
500 if (comtype == STANDARD) {
501 putKcp (s, comptr-1, TRUE);
504 putKcp (s, acmptr-1, TRUE);
511 putKcp (s, s + strlen(s) -1, TRUE);
516 /* check for end of string */
518 if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
519 putKcp (s, strptr-1, TRUE);
524 putKcp (s, s+strlen(s)-1, TRUE);
529 /* check for end of character string */
531 if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
532 putKcp (s, chrptr-1, TRUE);
537 putKcp (s, s+strlen(s)-1, TRUE);
543 /* print out the line */
544 putKcp (s, s + strlen(s) -1, FALSE);
550 putKcp (start, end, force)
551 char *start; /* start of string to write */
552 char *end; /* end of string to write */
553 boolean force; /* true if we should force nokeyw */
558 while (start <= end) {
560 if (*start == ' ' || *start == '\t') {
565 while (*start == ' ' || *start == '\t')
571 /* take care of nice tab stops */
572 if (*start == '\t') {
573 while (*start == '\t')
575 i = tabs(s_start, start) - margin / 8;
576 printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
580 if (!nokeyw && !force)
581 if ((*start == '#' || isidchr(*start))
582 && (start == s_start || !isidchr(start[-1]))) {
587 putcp((unsigned char)*start++);
594 putcp ((unsigned char)*start++);
604 return (width(s, os) / 8);
609 register char *s, *os;
681 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
686 putchar('^'), c |= '@';
694 * look for a process beginning on this line
701 if (!l_toplex || blklevel == 0)
702 if (expmatch (s, l_prcbeg, pname) != NIL) {
709 /* iskw - check to see if the next word is a keyword
716 register char **ss = l_keywds;
718 register char *cp = s;
720 while (++cp, isidchr(*cp))
723 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))