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
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)vfontedpr.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/vgrind/vfontedpr.c,v 1.12 1999/08/28 01:07:21 peter Exp $
36 * $DragonFly: src/usr.bin/vgrind/vfontedpr.c,v 1.2 2003/06/17 04:29:33 dillon Exp $
39 #include <sys/types.h>
47 #include "pathnames.h"
59 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
63 #define STRLEN 10 /* length of strings introducing things */
64 #define PNAMELEN 40 /* length of a function/procedure name */
65 #define PSMAX 20 /* size of procedure name stacking */
67 static int iskw __P((char *));
68 static boolean isproc __P((char *));
69 static void putKcp __P((char *, char *, boolean));
70 static void putScp __P((char *));
71 static void putcp __P((int));
72 static int tabs __P((char *, char *));
73 static int width __P((char *, char *));
79 static boolean filter = FALSE; /* act as a filter (like eqn) */
80 static boolean inchr; /* in a string constant */
81 static boolean incomm; /* in a comment of the primary type */
82 static boolean idx = FALSE; /* form an index */
83 static boolean instr; /* in a string constant */
84 static boolean nokeyw = FALSE; /* no keywords being flagged */
85 static boolean pass = FALSE; /*
86 * when acting as a filter, pass indicates
87 * whether we are currently processing
91 static int blklevel; /* current nesting level */
92 static int comtype; /* type of comment */
93 static char *defsfile[2] = { _PATH_VGRINDEFS, 0 };
94 /* name of language definitions file */
96 static int plstack[PSMAX]; /* the procedure nesting level stack */
97 static char pname[BUFSIZ+1];
98 static boolean prccont; /* continue last procedure */
99 static int psptr; /* the stack index of the current procedure */
100 static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
103 * The language specific globals
106 char *l_acmbeg; /* string introducing a comment */
107 char *l_acmend; /* string ending a comment */
108 char *l_blkbeg; /* string begining of a block */
109 char *l_blkend; /* string ending a block */
110 char *l_chrbeg; /* delimiter for character constant */
111 char *l_chrend; /* delimiter for character constant */
112 char *l_combeg; /* string introducing a comment */
113 char *l_comend; /* string ending a comment */
114 char l_escape; /* character used to escape characters */
115 char *l_keywds[BUFSIZ/2]; /* keyword table address */
116 char *l_nocom; /* regexp for non-comments */
117 char *l_prcbeg; /* regular expr for procedure begin */
118 char *l_strbeg; /* delimiter for string constant */
119 char *l_strend; /* delimiter for string constant */
120 boolean l_toplex; /* procedures only defined at top lex level */
121 char *language = "c"; /* the language indicator */
123 #define ps(x) printf("%s", x)
142 if (!strcmp(argv[0], "-h")) {
148 printf("'ds =H %s\n", argv[1]);
156 /* act as a filter like eqn */
157 if (!strcmp(argv[0], "-f")) {
159 argv[0] = argv[argc-1];
164 /* take input from the standard place */
165 if (!strcmp(argv[0], "-")) {
171 if (!strcmp(argv[0], "-x")) {
176 /* indicate no keywords */
177 if (!strcmp(argv[0], "-n")) {
183 /* specify the font size */
184 if (!strncmp(argv[0], "-s", 2)) {
188 i = i * 10 + (*cp++ - '0');
189 printf("'ps %d\n'vs %d\n", i, i+1);
194 /* specify the language */
195 if (!strncmp(argv[0], "-l", 2)) {
196 language = argv[0]+2;
201 /* specify the language description file */
202 if (!strncmp(argv[0], "-d", 2)) {
203 defsfile[0] = argv[1];
209 /* open the file for input */
210 if (freopen(argv[0], "r", stdin) == NULL)
211 err(1, "%s", argv[0]);
213 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
220 * get the language definition from the defs file
222 i = cgetent(&defs, defsfile, language);
224 fprintf (stderr, "no entry for language %s\n", language);
226 } else if (i == -2) { fprintf(stderr,
227 "cannot find vgrindefs file %s\n", defsfile[0]);
229 } else if (i == -3) { fprintf(stderr,
230 "potential reference loop detected in vgrindefs file %s\n",
234 if (cgetustr(defs, "kw", &cp) == -1)
241 while (*cp == ' ' || *cp =='\t')
245 while (*cp != ' ' && *cp != '\t' && *cp)
250 cgetustr(defs, "pb", &cp);
251 l_prcbeg = convexp(cp);
252 cgetustr(defs, "cb", &cp);
253 l_combeg = convexp(cp);
254 cgetustr(defs, "ce", &cp);
255 l_comend = convexp(cp);
256 cgetustr(defs, "ab", &cp);
257 l_acmbeg = convexp(cp);
258 cgetustr(defs, "ae", &cp);
259 l_acmend = convexp(cp);
260 cgetustr(defs, "sb", &cp);
261 l_strbeg = convexp(cp);
262 cgetustr(defs, "se", &cp);
263 l_strend = convexp(cp);
264 cgetustr(defs, "bb", &cp);
265 l_blkbeg = convexp(cp);
266 cgetustr(defs, "be", &cp);
267 l_blkend = convexp(cp);
268 cgetustr(defs, "lb", &cp);
269 l_chrbeg = convexp(cp);
270 cgetustr(defs, "le", &cp);
271 l_chrend = convexp(cp);
272 if (cgetustr(defs, "nc", &cp) >= 0)
273 l_nocom = convexp(cp);
275 l_onecase = (cgetcap(defs, "oc", ':') != NULL);
276 l_toplex = (cgetcap(defs, "tl", ':') != NULL);
278 /* initialize the program */
285 for (psptr=0; psptr<PSMAX; psptr++) {
286 pstack[psptr][0] = '\0';
292 printf(".ds =F %s\n", fname);
302 fstat(fileno(stdin), &stbuf);
303 cp = ctime(&stbuf.st_mtime);
306 printf(".ds =M %s %s\n", cp+4, cp+20);
312 while (fgets(buf, sizeof buf, stdin) != NULL) {
313 if (buf[0] == '\f') {
318 if (!strncmp (buf+1, "vS", 2))
320 if (!strncmp (buf+1, "vE", 2))
329 if (prccont && (psptr >= 0)) {
335 printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
344 #define isidchr(c) (isalnum(c) || (c) == '_')
350 register char *s = os; /* pointer to unmatched string */
351 char dummy[BUFSIZ]; /* dummy to be used by expmatch */
352 char *comptr; /* end of a comment delimiter */
353 char *acmptr; /* end of a comment delimiter */
354 char *strptr; /* end of a string delimiter */
355 char *chrptr; /* end of a character const delimiter */
356 char *blksptr; /* end of a lexical block start */
357 char *blkeptr; /* end of a lexical block end */
358 char *nocomptr; /* end of a non-comment delimiter */
360 s_start = os; /* remember the start for expmatch */
362 if (nokeyw || incomm || instr)
370 strncpy (pstack[psptr], pname, PNAMELEN);
371 pstack[psptr][PNAMELEN] = '\0';
372 plstack[psptr] = blklevel;
377 /* check for string, comment, blockstart, etc */
378 if (!incomm && !instr && !inchr) {
380 blkeptr = expmatch (s, l_blkend, dummy);
381 blksptr = expmatch (s, l_blkbeg, dummy);
382 comptr = expmatch (s, l_combeg, dummy);
383 acmptr = expmatch (s, l_acmbeg, dummy);
384 strptr = expmatch (s, l_strbeg, dummy);
385 chrptr = expmatch (s, l_chrbeg, dummy);
386 nocomptr = expmatch (s, l_nocom, dummy);
388 /* start of non-comment? */
390 if ((nocomptr <= comptr || comptr == NIL)
391 && (nocomptr <= acmptr || acmptr == NIL)) {
392 /* continue after non-comment */
393 putKcp (s, nocomptr-1, FALSE);
398 /* start of a comment? */
400 if ((comptr < strptr || strptr == NIL)
401 && (comptr < acmptr || acmptr == NIL)
402 && (comptr < chrptr || chrptr == NIL)
403 && (comptr < blksptr || blksptr == NIL)
404 && (comptr < blkeptr || blkeptr == NIL)) {
405 putKcp (s, comptr-1, FALSE);
415 /* start of a comment? */
417 if ((acmptr < strptr || strptr == NIL)
418 && (acmptr < chrptr || chrptr == NIL)
419 && (acmptr < blksptr || blksptr == NIL)
420 && (acmptr < blkeptr || blkeptr == NIL)) {
421 putKcp (s, acmptr-1, FALSE);
431 /* start of a string? */
433 if ((strptr < chrptr || chrptr == NIL)
434 && (strptr < blksptr || blksptr == NIL)
435 && (strptr < blkeptr || blkeptr == NIL)) {
436 putKcp (s, strptr-1, FALSE);
442 /* start of a character string? */
444 if ((chrptr < blksptr || blksptr == NIL)
445 && (chrptr < blkeptr || blkeptr == NIL)) {
446 putKcp (s, chrptr-1, FALSE);
452 /* end of a lexical block */
453 if (blkeptr != NIL) {
454 if (blkeptr < blksptr || blksptr == NIL) {
455 putKcp (s, blkeptr - 1, FALSE);
457 if (blklevel > 0 /* sanity */)
459 if (psptr >= 0 && plstack[psptr] >= blklevel) {
461 /* end of current procedure */
465 blklevel = plstack[psptr];
467 /* see if we should print the last proc name */
477 /* start of a lexical block */
478 if (blksptr != NIL) {
479 putKcp (s, blksptr - 1, FALSE);
485 /* check for end of comment */
487 comptr = expmatch (s, l_comend, dummy);
488 acmptr = expmatch (s, l_acmend, dummy);
489 if (((comtype == STANDARD) && (comptr != NIL)) ||
490 ((comtype == ALTERNATE) && (acmptr != NIL))) {
491 if (comtype == STANDARD) {
492 putKcp (s, comptr-1, TRUE);
495 putKcp (s, acmptr-1, TRUE);
502 putKcp (s, s + strlen(s) -1, TRUE);
507 /* check for end of string */
509 if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
510 putKcp (s, strptr-1, TRUE);
515 putKcp (s, s+strlen(s)-1, TRUE);
520 /* check for end of character string */
522 if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
523 putKcp (s, chrptr-1, TRUE);
528 putKcp (s, s+strlen(s)-1, TRUE);
534 /* print out the line */
535 putKcp (s, s + strlen(s) -1, FALSE);
541 putKcp (start, end, force)
542 char *start; /* start of string to write */
543 char *end; /* end of string to write */
544 boolean force; /* true if we should force nokeyw */
549 while (start <= end) {
551 if (*start == ' ' || *start == '\t') {
556 while (*start == ' ' || *start == '\t')
562 /* take care of nice tab stops */
563 if (*start == '\t') {
564 while (*start == '\t')
566 i = tabs(s_start, start) - margin / 8;
567 printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
571 if (!nokeyw && !force)
572 if ((*start == '#' || isidchr(*start))
573 && (start == s_start || !isidchr(start[-1]))) {
578 putcp((unsigned char)*start++);
585 putcp ((unsigned char)*start++);
595 return (width(s, os) / 8);
600 register char *s, *os;
672 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
677 putchar('^'), c |= '@';
685 * look for a process beginning on this line
692 if (!l_toplex || blklevel == 0)
693 if (expmatch (s, l_prcbeg, pname) != NIL) {
700 /* iskw - check to see if the next word is a keyword
707 register char **ss = l_keywds;
709 register char *cp = s;
711 while (++cp, isidchr(*cp))
714 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))