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. 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 $
34 #include <sys/types.h>
42 #include "pathnames.h"
54 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
58 #define STRLEN 10 /* length of strings introducing things */
59 #define PNAMELEN 40 /* length of a function/procedure name */
60 #define PSMAX 20 /* size of procedure name stacking */
62 static int iskw(char *);
63 static boolean
isproc(char *);
64 static void putKcp(char *, char *, boolean
);
65 static void putScp(char *);
66 static void putcp(int);
67 static int tabs(char *, char *);
68 static int width(char *, char *);
74 static boolean filter
= FALSE
; /* act as a filter (like eqn) */
75 static boolean inchr
; /* in a string constant */
76 static boolean incomm
; /* in a comment of the primary type */
77 static boolean idx
= FALSE
; /* form an index */
78 static boolean instr
; /* in a string constant */
79 static boolean nokeyw
= FALSE
; /* no keywords being flagged */
80 static boolean pass
= FALSE
; /*
81 * when acting as a filter, pass indicates
82 * whether we are currently processing
86 static int blklevel
; /* current nesting level */
87 static int comtype
; /* type of comment */
88 static char *defsfile
[2] = { _PATH_VGRINDEFS
, 0 };
89 /* name of language definitions file */
91 static int plstack
[PSMAX
]; /* the procedure nesting level stack */
92 static char pname
[BUFSIZ
+1];
93 static boolean prccont
; /* continue last procedure */
94 static int psptr
; /* the stack index of the current procedure */
95 static char pstack
[PSMAX
][PNAMELEN
+1]; /* the procedure name stack */
98 * The language specific globals
101 char *l_acmbeg
; /* string introducing a comment */
102 char *l_acmend
; /* string ending a comment */
103 char *l_blkbeg
; /* string begining of a block */
104 char *l_blkend
; /* string ending a block */
105 char *l_chrbeg
; /* delimiter for character constant */
106 char *l_chrend
; /* delimiter for character constant */
107 char *l_combeg
; /* string introducing a comment */
108 char *l_comend
; /* string ending a comment */
109 char l_escape
; /* character used to escape characters */
110 char *l_keywds
[BUFSIZ
/2]; /* keyword table address */
111 char *l_nocom
; /* regexp for non-comments */
112 char *l_prcbeg
; /* regular expr for procedure begin */
113 char *l_strbeg
; /* delimiter for string constant */
114 char *l_strend
; /* delimiter for string constant */
115 boolean l_toplex
; /* procedures only defined at top lex level */
116 char *language
= "c"; /* the language indicator */
118 #define ps(x) printf("%s", x)
121 main(int argc
, char **argv
)
135 if (!strcmp(argv
[0], "-h")) {
141 printf("'ds =H %s\n", argv
[1]);
149 /* act as a filter like eqn */
150 if (!strcmp(argv
[0], "-f")) {
152 argv
[0] = argv
[argc
-1];
157 /* take input from the standard place */
158 if (!strcmp(argv
[0], "-")) {
164 if (!strcmp(argv
[0], "-x")) {
169 /* indicate no keywords */
170 if (!strcmp(argv
[0], "-n")) {
176 /* specify the font size */
177 if (!strncmp(argv
[0], "-s", 2)) {
181 i
= i
* 10 + (*cp
++ - '0');
182 printf("'ps %d\n'vs %d\n", i
, i
+1);
187 /* specify the language */
188 if (!strncmp(argv
[0], "-l", 2)) {
189 language
= argv
[0]+2;
194 /* specify the language description file */
195 if (!strncmp(argv
[0], "-d", 2)) {
196 defsfile
[0] = argv
[1];
202 /* open the file for input */
203 if (freopen(argv
[0], "r", stdin
) == NULL
)
204 err(1, "%s", argv
[0]);
206 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
213 * get the language definition from the defs file
215 i
= cgetent(&defs
, defsfile
, language
);
217 fprintf (stderr
, "no entry for language %s\n", language
);
219 } else if (i
== -2) { fprintf(stderr
,
220 "cannot find vgrindefs file %s\n", defsfile
[0]);
222 } else if (i
== -3) { fprintf(stderr
,
223 "potential reference loop detected in vgrindefs file %s\n",
227 if (cgetustr(defs
, "kw", &cp
) == -1)
234 while (*cp
== ' ' || *cp
=='\t')
238 while (*cp
!= ' ' && *cp
!= '\t' && *cp
)
243 cgetustr(defs
, "pb", &cp
);
244 l_prcbeg
= convexp(cp
);
245 cgetustr(defs
, "cb", &cp
);
246 l_combeg
= convexp(cp
);
247 cgetustr(defs
, "ce", &cp
);
248 l_comend
= convexp(cp
);
249 cgetustr(defs
, "ab", &cp
);
250 l_acmbeg
= convexp(cp
);
251 cgetustr(defs
, "ae", &cp
);
252 l_acmend
= convexp(cp
);
253 cgetustr(defs
, "sb", &cp
);
254 l_strbeg
= convexp(cp
);
255 cgetustr(defs
, "se", &cp
);
256 l_strend
= convexp(cp
);
257 cgetustr(defs
, "bb", &cp
);
258 l_blkbeg
= convexp(cp
);
259 cgetustr(defs
, "be", &cp
);
260 l_blkend
= convexp(cp
);
261 cgetustr(defs
, "lb", &cp
);
262 l_chrbeg
= convexp(cp
);
263 cgetustr(defs
, "le", &cp
);
264 l_chrend
= convexp(cp
);
265 if (cgetustr(defs
, "nc", &cp
) >= 0)
266 l_nocom
= convexp(cp
);
268 l_onecase
= (cgetcap(defs
, "oc", ':') != NULL
);
269 l_toplex
= (cgetcap(defs
, "tl", ':') != NULL
);
271 /* initialize the program */
278 for (psptr
=0; psptr
<PSMAX
; psptr
++) {
279 pstack
[psptr
][0] = '\0';
285 printf(".ds =F %s\n", fname
);
295 fstat(fileno(stdin
), &stbuf
);
296 cp
= ctime(&stbuf
.st_mtime
);
299 printf(".ds =M %s %s\n", cp
+4, cp
+20);
305 while (fgets(buf
, sizeof buf
, stdin
) != NULL
) {
306 if (buf
[0] == '\f') {
311 if (!strncmp (buf
+1, "vS", 2))
313 if (!strncmp (buf
+1, "vE", 2))
322 if (prccont
&& (psptr
>= 0)) {
328 printf ("com %o str %o chr %o ptr %d\n", incomm
, instr
, inchr
, psptr
);
337 #define isidchr(c) (isalnum(c) || (c) == '_')
342 char *s
= os
; /* pointer to unmatched string */
343 char dummy
[BUFSIZ
]; /* dummy to be used by expmatch */
344 char *comptr
; /* end of a comment delimiter */
345 char *acmptr
; /* end of a comment delimiter */
346 char *strptr
; /* end of a string delimiter */
347 char *chrptr
; /* end of a character const delimiter */
348 char *blksptr
; /* end of a lexical block start */
349 char *blkeptr
; /* end of a lexical block end */
350 char *nocomptr
; /* end of a non-comment delimiter */
352 s_start
= os
; /* remember the start for expmatch */
354 if (nokeyw
|| incomm
|| instr
)
362 strncpy (pstack
[psptr
], pname
, PNAMELEN
);
363 pstack
[psptr
][PNAMELEN
] = '\0';
364 plstack
[psptr
] = blklevel
;
369 /* check for string, comment, blockstart, etc */
370 if (!incomm
&& !instr
&& !inchr
) {
372 blkeptr
= expmatch (s
, l_blkend
, dummy
);
373 blksptr
= expmatch (s
, l_blkbeg
, dummy
);
374 comptr
= expmatch (s
, l_combeg
, dummy
);
375 acmptr
= expmatch (s
, l_acmbeg
, dummy
);
376 strptr
= expmatch (s
, l_strbeg
, dummy
);
377 chrptr
= expmatch (s
, l_chrbeg
, dummy
);
378 nocomptr
= expmatch (s
, l_nocom
, dummy
);
380 /* start of non-comment? */
382 if ((nocomptr
<= comptr
|| comptr
== NIL
)
383 && (nocomptr
<= acmptr
|| acmptr
== NIL
)) {
384 /* continue after non-comment */
385 putKcp (s
, nocomptr
-1, FALSE
);
390 /* start of a comment? */
392 if ((comptr
< strptr
|| strptr
== NIL
)
393 && (comptr
< acmptr
|| acmptr
== NIL
)
394 && (comptr
< chrptr
|| chrptr
== NIL
)
395 && (comptr
< blksptr
|| blksptr
== NIL
)
396 && (comptr
< blkeptr
|| blkeptr
== NIL
)) {
397 putKcp (s
, comptr
-1, FALSE
);
407 /* start of a comment? */
409 if ((acmptr
< strptr
|| strptr
== NIL
)
410 && (acmptr
< chrptr
|| chrptr
== NIL
)
411 && (acmptr
< blksptr
|| blksptr
== NIL
)
412 && (acmptr
< blkeptr
|| blkeptr
== NIL
)) {
413 putKcp (s
, acmptr
-1, FALSE
);
423 /* start of a string? */
425 if ((strptr
< chrptr
|| chrptr
== NIL
)
426 && (strptr
< blksptr
|| blksptr
== NIL
)
427 && (strptr
< blkeptr
|| blkeptr
== NIL
)) {
428 putKcp (s
, strptr
-1, FALSE
);
434 /* start of a character string? */
436 if ((chrptr
< blksptr
|| blksptr
== NIL
)
437 && (chrptr
< blkeptr
|| blkeptr
== NIL
)) {
438 putKcp (s
, chrptr
-1, FALSE
);
444 /* end of a lexical block */
445 if (blkeptr
!= NIL
) {
446 if (blkeptr
< blksptr
|| blksptr
== NIL
) {
447 putKcp (s
, blkeptr
- 1, FALSE
);
449 if (blklevel
> 0 /* sanity */)
451 if (psptr
>= 0 && plstack
[psptr
] >= blklevel
) {
453 /* end of current procedure */
457 blklevel
= plstack
[psptr
];
459 /* see if we should print the last proc name */
469 /* start of a lexical block */
470 if (blksptr
!= NIL
) {
471 putKcp (s
, blksptr
- 1, FALSE
);
477 /* check for end of comment */
479 comptr
= expmatch (s
, l_comend
, dummy
);
480 acmptr
= expmatch (s
, l_acmend
, dummy
);
481 if (((comtype
== STANDARD
) && (comptr
!= NIL
)) ||
482 ((comtype
== ALTERNATE
) && (acmptr
!= NIL
))) {
483 if (comtype
== STANDARD
) {
484 putKcp (s
, comptr
-1, TRUE
);
487 putKcp (s
, acmptr
-1, TRUE
);
494 putKcp (s
, s
+ strlen(s
) -1, TRUE
);
499 /* check for end of string */
501 if ((strptr
= expmatch (s
, l_strend
, dummy
)) != NIL
) {
502 putKcp (s
, strptr
-1, TRUE
);
507 putKcp (s
, s
+strlen(s
)-1, TRUE
);
512 /* check for end of character string */
514 if ((chrptr
= expmatch (s
, l_chrend
, dummy
)) != NIL
) {
515 putKcp (s
, chrptr
-1, TRUE
);
520 putKcp (s
, s
+strlen(s
)-1, TRUE
);
526 /* print out the line */
527 putKcp (s
, s
+ strlen(s
) -1, FALSE
);
532 /* force: true if we should force nokeyw */
534 putKcp(char *start
, char *end
, boolean force
)
539 while (start
<= end
) {
541 if (*start
== ' ' || *start
== '\t') {
546 while (*start
== ' ' || *start
== '\t')
552 /* take care of nice tab stops */
553 if (*start
== '\t') {
554 while (*start
== '\t')
556 i
= tabs(s_start
, start
) - margin
/ 8;
557 printf("\\h'|%dn'", i
* 10 + 1 - margin
% 8);
561 if (!nokeyw
&& !force
)
562 if ((*start
== '#' || isidchr(*start
))
563 && (start
== s_start
|| !isidchr(start
[-1]))) {
568 putcp((unsigned char)*start
++);
575 putcp ((unsigned char)*start
++);
581 tabs(char *s
, char *os
)
584 return (width(s
, os
) / 8);
588 width(char *s
, char *os
)
659 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
664 putchar('^'), c
|= '@';
672 * look for a process beginning on this line
678 if (!l_toplex
|| blklevel
== 0)
679 if (expmatch (s
, l_prcbeg
, pname
) != NIL
) {
686 /* iskw - check to see if the next word is a keyword
692 char **ss
= l_keywds
;
696 while (++cp
, isidchr(*cp
))
698 while ((cp
= *ss
++) != NULL
)
699 if (!STRNCMP(s
,cp
,i
) && !isidchr(cp
[i
]))