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.3 2003/10/04 20:36:54 hmp 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(char *);
68 static boolean
isproc(char *);
69 static void putKcp(char *, char *, boolean
);
70 static void putScp(char *);
71 static void putcp(int);
72 static int tabs(char *, char *);
73 static int width(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)
126 main(int argc
, char **argv
)
140 if (!strcmp(argv
[0], "-h")) {
146 printf("'ds =H %s\n", argv
[1]);
154 /* act as a filter like eqn */
155 if (!strcmp(argv
[0], "-f")) {
157 argv
[0] = argv
[argc
-1];
162 /* take input from the standard place */
163 if (!strcmp(argv
[0], "-")) {
169 if (!strcmp(argv
[0], "-x")) {
174 /* indicate no keywords */
175 if (!strcmp(argv
[0], "-n")) {
181 /* specify the font size */
182 if (!strncmp(argv
[0], "-s", 2)) {
186 i
= i
* 10 + (*cp
++ - '0');
187 printf("'ps %d\n'vs %d\n", i
, i
+1);
192 /* specify the language */
193 if (!strncmp(argv
[0], "-l", 2)) {
194 language
= argv
[0]+2;
199 /* specify the language description file */
200 if (!strncmp(argv
[0], "-d", 2)) {
201 defsfile
[0] = argv
[1];
207 /* open the file for input */
208 if (freopen(argv
[0], "r", stdin
) == NULL
)
209 err(1, "%s", argv
[0]);
211 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
218 * get the language definition from the defs file
220 i
= cgetent(&defs
, defsfile
, language
);
222 fprintf (stderr
, "no entry for language %s\n", language
);
224 } else if (i
== -2) { fprintf(stderr
,
225 "cannot find vgrindefs file %s\n", defsfile
[0]);
227 } else if (i
== -3) { fprintf(stderr
,
228 "potential reference loop detected in vgrindefs file %s\n",
232 if (cgetustr(defs
, "kw", &cp
) == -1)
239 while (*cp
== ' ' || *cp
=='\t')
243 while (*cp
!= ' ' && *cp
!= '\t' && *cp
)
248 cgetustr(defs
, "pb", &cp
);
249 l_prcbeg
= convexp(cp
);
250 cgetustr(defs
, "cb", &cp
);
251 l_combeg
= convexp(cp
);
252 cgetustr(defs
, "ce", &cp
);
253 l_comend
= convexp(cp
);
254 cgetustr(defs
, "ab", &cp
);
255 l_acmbeg
= convexp(cp
);
256 cgetustr(defs
, "ae", &cp
);
257 l_acmend
= convexp(cp
);
258 cgetustr(defs
, "sb", &cp
);
259 l_strbeg
= convexp(cp
);
260 cgetustr(defs
, "se", &cp
);
261 l_strend
= convexp(cp
);
262 cgetustr(defs
, "bb", &cp
);
263 l_blkbeg
= convexp(cp
);
264 cgetustr(defs
, "be", &cp
);
265 l_blkend
= convexp(cp
);
266 cgetustr(defs
, "lb", &cp
);
267 l_chrbeg
= convexp(cp
);
268 cgetustr(defs
, "le", &cp
);
269 l_chrend
= convexp(cp
);
270 if (cgetustr(defs
, "nc", &cp
) >= 0)
271 l_nocom
= convexp(cp
);
273 l_onecase
= (cgetcap(defs
, "oc", ':') != NULL
);
274 l_toplex
= (cgetcap(defs
, "tl", ':') != NULL
);
276 /* initialize the program */
283 for (psptr
=0; psptr
<PSMAX
; psptr
++) {
284 pstack
[psptr
][0] = '\0';
290 printf(".ds =F %s\n", fname
);
300 fstat(fileno(stdin
), &stbuf
);
301 cp
= ctime(&stbuf
.st_mtime
);
304 printf(".ds =M %s %s\n", cp
+4, cp
+20);
310 while (fgets(buf
, sizeof buf
, stdin
) != NULL
) {
311 if (buf
[0] == '\f') {
316 if (!strncmp (buf
+1, "vS", 2))
318 if (!strncmp (buf
+1, "vE", 2))
327 if (prccont
&& (psptr
>= 0)) {
333 printf ("com %o str %o chr %o ptr %d\n", incomm
, instr
, inchr
, psptr
);
342 #define isidchr(c) (isalnum(c) || (c) == '_')
347 register char *s
= os
; /* pointer to unmatched string */
348 char dummy
[BUFSIZ
]; /* dummy to be used by expmatch */
349 char *comptr
; /* end of a comment delimiter */
350 char *acmptr
; /* end of a comment delimiter */
351 char *strptr
; /* end of a string delimiter */
352 char *chrptr
; /* end of a character const delimiter */
353 char *blksptr
; /* end of a lexical block start */
354 char *blkeptr
; /* end of a lexical block end */
355 char *nocomptr
; /* end of a non-comment delimiter */
357 s_start
= os
; /* remember the start for expmatch */
359 if (nokeyw
|| incomm
|| instr
)
367 strncpy (pstack
[psptr
], pname
, PNAMELEN
);
368 pstack
[psptr
][PNAMELEN
] = '\0';
369 plstack
[psptr
] = blklevel
;
374 /* check for string, comment, blockstart, etc */
375 if (!incomm
&& !instr
&& !inchr
) {
377 blkeptr
= expmatch (s
, l_blkend
, dummy
);
378 blksptr
= expmatch (s
, l_blkbeg
, dummy
);
379 comptr
= expmatch (s
, l_combeg
, dummy
);
380 acmptr
= expmatch (s
, l_acmbeg
, dummy
);
381 strptr
= expmatch (s
, l_strbeg
, dummy
);
382 chrptr
= expmatch (s
, l_chrbeg
, dummy
);
383 nocomptr
= expmatch (s
, l_nocom
, dummy
);
385 /* start of non-comment? */
387 if ((nocomptr
<= comptr
|| comptr
== NIL
)
388 && (nocomptr
<= acmptr
|| acmptr
== NIL
)) {
389 /* continue after non-comment */
390 putKcp (s
, nocomptr
-1, FALSE
);
395 /* start of a comment? */
397 if ((comptr
< strptr
|| strptr
== NIL
)
398 && (comptr
< acmptr
|| acmptr
== NIL
)
399 && (comptr
< chrptr
|| chrptr
== NIL
)
400 && (comptr
< blksptr
|| blksptr
== NIL
)
401 && (comptr
< blkeptr
|| blkeptr
== NIL
)) {
402 putKcp (s
, comptr
-1, FALSE
);
412 /* start of a comment? */
414 if ((acmptr
< strptr
|| strptr
== NIL
)
415 && (acmptr
< chrptr
|| chrptr
== NIL
)
416 && (acmptr
< blksptr
|| blksptr
== NIL
)
417 && (acmptr
< blkeptr
|| blkeptr
== NIL
)) {
418 putKcp (s
, acmptr
-1, FALSE
);
428 /* start of a string? */
430 if ((strptr
< chrptr
|| chrptr
== NIL
)
431 && (strptr
< blksptr
|| blksptr
== NIL
)
432 && (strptr
< blkeptr
|| blkeptr
== NIL
)) {
433 putKcp (s
, strptr
-1, FALSE
);
439 /* start of a character string? */
441 if ((chrptr
< blksptr
|| blksptr
== NIL
)
442 && (chrptr
< blkeptr
|| blkeptr
== NIL
)) {
443 putKcp (s
, chrptr
-1, FALSE
);
449 /* end of a lexical block */
450 if (blkeptr
!= NIL
) {
451 if (blkeptr
< blksptr
|| blksptr
== NIL
) {
452 putKcp (s
, blkeptr
- 1, FALSE
);
454 if (blklevel
> 0 /* sanity */)
456 if (psptr
>= 0 && plstack
[psptr
] >= blklevel
) {
458 /* end of current procedure */
462 blklevel
= plstack
[psptr
];
464 /* see if we should print the last proc name */
474 /* start of a lexical block */
475 if (blksptr
!= NIL
) {
476 putKcp (s
, blksptr
- 1, FALSE
);
482 /* check for end of comment */
484 comptr
= expmatch (s
, l_comend
, dummy
);
485 acmptr
= expmatch (s
, l_acmend
, dummy
);
486 if (((comtype
== STANDARD
) && (comptr
!= NIL
)) ||
487 ((comtype
== ALTERNATE
) && (acmptr
!= NIL
))) {
488 if (comtype
== STANDARD
) {
489 putKcp (s
, comptr
-1, TRUE
);
492 putKcp (s
, acmptr
-1, TRUE
);
499 putKcp (s
, s
+ strlen(s
) -1, TRUE
);
504 /* check for end of string */
506 if ((strptr
= expmatch (s
, l_strend
, dummy
)) != NIL
) {
507 putKcp (s
, strptr
-1, TRUE
);
512 putKcp (s
, s
+strlen(s
)-1, TRUE
);
517 /* check for end of character string */
519 if ((chrptr
= expmatch (s
, l_chrend
, dummy
)) != NIL
) {
520 putKcp (s
, chrptr
-1, TRUE
);
525 putKcp (s
, s
+strlen(s
)-1, TRUE
);
531 /* print out the line */
532 putKcp (s
, s
+ strlen(s
) -1, FALSE
);
537 /* force: true if we should force nokeyw */
539 putKcp(char *start
, char *end
, boolean force
)
544 while (start
<= end
) {
546 if (*start
== ' ' || *start
== '\t') {
551 while (*start
== ' ' || *start
== '\t')
557 /* take care of nice tab stops */
558 if (*start
== '\t') {
559 while (*start
== '\t')
561 i
= tabs(s_start
, start
) - margin
/ 8;
562 printf("\\h'|%dn'", i
* 10 + 1 - margin
% 8);
566 if (!nokeyw
&& !force
)
567 if ((*start
== '#' || isidchr(*start
))
568 && (start
== s_start
|| !isidchr(start
[-1]))) {
573 putcp((unsigned char)*start
++);
580 putcp ((unsigned char)*start
++);
586 tabs(char *s
, char *os
)
589 return (width(s
, os
) / 8);
593 width(register char *s
, register char *os
)
613 putcp(register int c
)
664 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
669 putchar('^'), c
|= '@';
677 * look for a process beginning on this line
683 if (!l_toplex
|| blklevel
== 0)
684 if (expmatch (s
, l_prcbeg
, pname
) != NIL
) {
691 /* iskw - check to see if the next word is a keyword
695 iskw(register char *s
)
697 register char **ss
= l_keywds
;
699 register char *cp
= s
;
701 while (++cp
, isidchr(*cp
))
704 if (!STRNCMP(s
,cp
,i
) && !isidchr(cp
[i
]))