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
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)vfontedpr.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/vgrind/vfontedpr.c,v 1.12 1999/08/28 01:07:21 peter Exp $
32 * $DragonFly: src/usr.bin/vgrind/vfontedpr.c,v 1.4 2008/10/16 01:52:34 swildner Exp $
35 #include <sys/types.h>
43 #include "pathnames.h"
55 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
59 #define STRLEN 10 /* length of strings introducing things */
60 #define PNAMELEN 40 /* length of a function/procedure name */
61 #define PSMAX 20 /* size of procedure name stacking */
63 static int iskw(char *);
64 static boolean isproc(char *);
65 static void putKcp(char *, char *, boolean);
66 static void putScp(char *);
67 static void putcp(int);
68 static int tabs(char *, char *);
69 static int width(char *, char *);
75 static boolean filter = FALSE; /* act as a filter (like eqn) */
76 static boolean inchr; /* in a string constant */
77 static boolean incomm; /* in a comment of the primary type */
78 static boolean idx = FALSE; /* form an index */
79 static boolean instr; /* in a string constant */
80 static boolean nokeyw = FALSE; /* no keywords being flagged */
81 static boolean pass = FALSE; /*
82 * when acting as a filter, pass indicates
83 * whether we are currently processing
87 static int blklevel; /* current nesting level */
88 static int comtype; /* type of comment */
89 static char *defsfile[2] = { _PATH_VGRINDEFS, 0 };
90 /* name of language definitions file */
92 static int plstack[PSMAX]; /* the procedure nesting level stack */
93 static char pname[BUFSIZ+1];
94 static boolean prccont; /* continue last procedure */
95 static int psptr; /* the stack index of the current procedure */
96 static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
99 * The language specific globals
102 char *l_acmbeg; /* string introducing a comment */
103 char *l_acmend; /* string ending a comment */
104 char *l_blkbeg; /* string begining of a block */
105 char *l_blkend; /* string ending a block */
106 char *l_chrbeg; /* delimiter for character constant */
107 char *l_chrend; /* delimiter for character constant */
108 char *l_combeg; /* string introducing a comment */
109 char *l_comend; /* string ending a comment */
110 char l_escape; /* character used to escape characters */
111 char *l_keywds[BUFSIZ/2]; /* keyword table address */
112 char *l_nocom; /* regexp for non-comments */
113 char *l_prcbeg; /* regular expr for procedure begin */
114 char *l_strbeg; /* delimiter for string constant */
115 char *l_strend; /* delimiter for string constant */
116 boolean l_toplex; /* procedures only defined at top lex level */
117 char *language = "c"; /* the language indicator */
119 #define ps(x) printf("%s", x)
122 main(int argc, char **argv)
136 if (!strcmp(argv[0], "-h")) {
142 printf("'ds =H %s\n", argv[1]);
150 /* act as a filter like eqn */
151 if (!strcmp(argv[0], "-f")) {
153 argv[0] = argv[argc-1];
158 /* take input from the standard place */
159 if (!strcmp(argv[0], "-")) {
165 if (!strcmp(argv[0], "-x")) {
170 /* indicate no keywords */
171 if (!strcmp(argv[0], "-n")) {
177 /* specify the font size */
178 if (!strncmp(argv[0], "-s", 2)) {
182 i = i * 10 + (*cp++ - '0');
183 printf("'ps %d\n'vs %d\n", i, i+1);
188 /* specify the language */
189 if (!strncmp(argv[0], "-l", 2)) {
190 language = argv[0]+2;
195 /* specify the language description file */
196 if (!strncmp(argv[0], "-d", 2)) {
197 defsfile[0] = argv[1];
203 /* open the file for input */
204 if (freopen(argv[0], "r", stdin) == NULL)
205 err(1, "%s", argv[0]);
207 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
214 * get the language definition from the defs file
216 i = cgetent(&defs, defsfile, language);
218 fprintf (stderr, "no entry for language %s\n", language);
220 } else if (i == -2) { fprintf(stderr,
221 "cannot find vgrindefs file %s\n", defsfile[0]);
223 } else if (i == -3) { fprintf(stderr,
224 "potential reference loop detected in vgrindefs file %s\n",
228 if (cgetustr(defs, "kw", &cp) == -1)
235 while (*cp == ' ' || *cp =='\t')
239 while (*cp != ' ' && *cp != '\t' && *cp)
244 cgetustr(defs, "pb", &cp);
245 l_prcbeg = convexp(cp);
246 cgetustr(defs, "cb", &cp);
247 l_combeg = convexp(cp);
248 cgetustr(defs, "ce", &cp);
249 l_comend = convexp(cp);
250 cgetustr(defs, "ab", &cp);
251 l_acmbeg = convexp(cp);
252 cgetustr(defs, "ae", &cp);
253 l_acmend = convexp(cp);
254 cgetustr(defs, "sb", &cp);
255 l_strbeg = convexp(cp);
256 cgetustr(defs, "se", &cp);
257 l_strend = convexp(cp);
258 cgetustr(defs, "bb", &cp);
259 l_blkbeg = convexp(cp);
260 cgetustr(defs, "be", &cp);
261 l_blkend = convexp(cp);
262 cgetustr(defs, "lb", &cp);
263 l_chrbeg = convexp(cp);
264 cgetustr(defs, "le", &cp);
265 l_chrend = convexp(cp);
266 if (cgetustr(defs, "nc", &cp) >= 0)
267 l_nocom = convexp(cp);
269 l_onecase = (cgetcap(defs, "oc", ':') != NULL);
270 l_toplex = (cgetcap(defs, "tl", ':') != NULL);
272 /* initialize the program */
279 for (psptr=0; psptr<PSMAX; psptr++) {
280 pstack[psptr][0] = '\0';
286 printf(".ds =F %s\n", fname);
296 fstat(fileno(stdin), &stbuf);
297 cp = ctime(&stbuf.st_mtime);
300 printf(".ds =M %s %s\n", cp+4, cp+20);
306 while (fgets(buf, sizeof buf, stdin) != NULL) {
307 if (buf[0] == '\f') {
312 if (!strncmp (buf+1, "vS", 2))
314 if (!strncmp (buf+1, "vE", 2))
323 if (prccont && (psptr >= 0)) {
329 printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
338 #define isidchr(c) (isalnum(c) || (c) == '_')
343 char *s = os; /* pointer to unmatched string */
344 char dummy[BUFSIZ]; /* dummy to be used by expmatch */
345 char *comptr; /* end of a comment delimiter */
346 char *acmptr; /* end of a comment delimiter */
347 char *strptr; /* end of a string delimiter */
348 char *chrptr; /* end of a character const delimiter */
349 char *blksptr; /* end of a lexical block start */
350 char *blkeptr; /* end of a lexical block end */
351 char *nocomptr; /* end of a non-comment delimiter */
353 s_start = os; /* remember the start for expmatch */
355 if (nokeyw || incomm || instr)
363 strncpy (pstack[psptr], pname, PNAMELEN);
364 pstack[psptr][PNAMELEN] = '\0';
365 plstack[psptr] = blklevel;
370 /* check for string, comment, blockstart, etc */
371 if (!incomm && !instr && !inchr) {
373 blkeptr = expmatch (s, l_blkend, dummy);
374 blksptr = expmatch (s, l_blkbeg, dummy);
375 comptr = expmatch (s, l_combeg, dummy);
376 acmptr = expmatch (s, l_acmbeg, dummy);
377 strptr = expmatch (s, l_strbeg, dummy);
378 chrptr = expmatch (s, l_chrbeg, dummy);
379 nocomptr = expmatch (s, l_nocom, dummy);
381 /* start of non-comment? */
383 if ((nocomptr <= comptr || comptr == NIL)
384 && (nocomptr <= acmptr || acmptr == NIL)) {
385 /* continue after non-comment */
386 putKcp (s, nocomptr-1, FALSE);
391 /* start of a comment? */
393 if ((comptr < strptr || strptr == NIL)
394 && (comptr < acmptr || acmptr == NIL)
395 && (comptr < chrptr || chrptr == NIL)
396 && (comptr < blksptr || blksptr == NIL)
397 && (comptr < blkeptr || blkeptr == NIL)) {
398 putKcp (s, comptr-1, FALSE);
408 /* start of a comment? */
410 if ((acmptr < strptr || strptr == NIL)
411 && (acmptr < chrptr || chrptr == NIL)
412 && (acmptr < blksptr || blksptr == NIL)
413 && (acmptr < blkeptr || blkeptr == NIL)) {
414 putKcp (s, acmptr-1, FALSE);
424 /* start of a string? */
426 if ((strptr < chrptr || chrptr == NIL)
427 && (strptr < blksptr || blksptr == NIL)
428 && (strptr < blkeptr || blkeptr == NIL)) {
429 putKcp (s, strptr-1, FALSE);
435 /* start of a character string? */
437 if ((chrptr < blksptr || blksptr == NIL)
438 && (chrptr < blkeptr || blkeptr == NIL)) {
439 putKcp (s, chrptr-1, FALSE);
445 /* end of a lexical block */
446 if (blkeptr != NIL) {
447 if (blkeptr < blksptr || blksptr == NIL) {
448 putKcp (s, blkeptr - 1, FALSE);
450 if (blklevel > 0 /* sanity */)
452 if (psptr >= 0 && plstack[psptr] >= blklevel) {
454 /* end of current procedure */
458 blklevel = plstack[psptr];
460 /* see if we should print the last proc name */
470 /* start of a lexical block */
471 if (blksptr != NIL) {
472 putKcp (s, blksptr - 1, FALSE);
478 /* check for end of comment */
480 comptr = expmatch (s, l_comend, dummy);
481 acmptr = expmatch (s, l_acmend, dummy);
482 if (((comtype == STANDARD) && (comptr != NIL)) ||
483 ((comtype == ALTERNATE) && (acmptr != NIL))) {
484 if (comtype == STANDARD) {
485 putKcp (s, comptr-1, TRUE);
488 putKcp (s, acmptr-1, TRUE);
495 putKcp (s, s + strlen(s) -1, TRUE);
500 /* check for end of string */
502 if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
503 putKcp (s, strptr-1, TRUE);
508 putKcp (s, s+strlen(s)-1, TRUE);
513 /* check for end of character string */
515 if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
516 putKcp (s, chrptr-1, TRUE);
521 putKcp (s, s+strlen(s)-1, TRUE);
527 /* print out the line */
528 putKcp (s, s + strlen(s) -1, FALSE);
533 /* force: true if we should force nokeyw */
535 putKcp(char *start, char *end, boolean force)
540 while (start <= end) {
542 if (*start == ' ' || *start == '\t') {
547 while (*start == ' ' || *start == '\t')
553 /* take care of nice tab stops */
554 if (*start == '\t') {
555 while (*start == '\t')
557 i = tabs(s_start, start) - margin / 8;
558 printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
562 if (!nokeyw && !force)
563 if ((*start == '#' || isidchr(*start))
564 && (start == s_start || !isidchr(start[-1]))) {
569 putcp((unsigned char)*start++);
576 putcp ((unsigned char)*start++);
582 tabs(char *s, char *os)
585 return (width(s, os) / 8);
589 width(char *s, char *os)
660 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
665 putchar('^'), c |= '@';
673 * look for a process beginning on this line
679 if (!l_toplex || blklevel == 0)
680 if (expmatch (s, l_prcbeg, pname) != NIL) {
687 /* iskw - check to see if the next word is a keyword
693 char **ss = l_keywds;
697 while (++cp, isidchr(*cp))
700 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))