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
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)vfontedpr.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/types.h>
53 #include "pathnames.h"
63 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
67 #define STRLEN 10 /* length of strings introducing things */
68 #define PNAMELEN 40 /* length of a function/procedure name */
69 #define PSMAX 20 /* size of procedure name stacking */
71 static int iskw(char *);
72 static bool isproc(char *);
73 static void putKcp(char *, char *, bool);
74 static void putScp(char *);
75 static void putcp(int);
76 static int tabs(char *, char *);
77 static int width(char *, char *);
83 static bool filter = false; /* act as a filter (like eqn) */
84 static bool inchr; /* in a string constant */
85 static bool incomm; /* in a comment of the primary type */
86 static bool idx = false; /* form an index */
87 static bool instr; /* in a string constant */
88 static bool nokeyw = false; /* no keywords being flagged */
89 static bool pass = false; /*
90 * when acting as a filter, pass indicates
91 * whether we are currently processing
95 static int blklevel; /* current nesting level */
96 static int comtype; /* type of comment */
97 static char * defsfile[2] = { _PATH_VGRINDEFS, 0 };
98 /* name of language definitions file */
100 static int plstack[PSMAX]; /* the procedure nesting level stack */
101 static char pname[BUFSIZ+1];
102 static bool prccont; /* continue last procedure */
103 static int psptr; /* the stack index of the current procedure */
104 static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
107 * The language specific globals
110 char *l_acmbeg; /* string introducing a comment */
111 char *l_acmend; /* string ending a comment */
112 char *l_blkbeg; /* string beginning of a block */
113 char *l_blkend; /* string ending a block */
114 char *l_chrbeg; /* delimiter for character constant */
115 char *l_chrend; /* delimiter for character constant */
116 char *l_combeg; /* string introducing a comment */
117 char *l_comend; /* string ending a comment */
118 char l_escape; /* character used to escape characters */
119 char *l_keywds[BUFSIZ/2]; /* keyword table address */
120 char *l_nocom; /* regexp for non-comments */
121 char *l_prcbeg; /* regular expr for procedure begin */
122 char *l_strbeg; /* delimiter for string constant */
123 char *l_strend; /* delimiter for string constant */
124 bool l_toplex; /* procedures only defined at top lex level */
125 const char *language = "c"; /* the language indicator */
127 #define ps(x) printf("%s", x)
130 main(int argc, char **argv)
132 const char *fname = "";
144 if (!strcmp(argv[0], "-h")) {
150 printf("'ds =H %s\n", argv[1]);
158 /* act as a filter like eqn */
159 if (!strcmp(argv[0], "-f")) {
161 argv[0] = argv[argc-1];
162 argv[argc-1] = strdup("-");
166 /* take input from the standard place */
167 if (!strcmp(argv[0], "-")) {
173 if (!strcmp(argv[0], "-x")) {
175 argv[0] = strdup("-n");
178 /* indicate no keywords */
179 if (!strcmp(argv[0], "-n")) {
185 /* specify the font size */
186 if (!strncmp(argv[0], "-s", 2)) {
190 i = i * 10 + (*cp++ - '0');
191 printf("'ps %d\n'vs %d\n", i, i+1);
196 /* specify the language */
197 if (!strncmp(argv[0], "-l", 2)) {
198 language = argv[0]+2;
203 /* specify the language description file */
204 if (!strncmp(argv[0], "-d", 2)) {
205 defsfile[0] = argv[1];
211 /* open the file for input */
212 if (freopen(argv[0], "r", stdin) == NULL)
213 err(1, "%s", argv[0]);
215 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
222 * get the language definition from the defs file
224 i = cgetent(&defs, defsfile, language);
226 fprintf (stderr, "no entry for language %s\n", language);
228 } else if (i == -2) { fprintf(stderr,
229 "cannot find vgrindefs file %s\n", defsfile[0]);
231 } else if (i == -3) { fprintf(stderr,
232 "potential reference loop detected in vgrindefs file %s\n",
236 if (cgetustr(defs, "kw", &cp) == -1)
243 while (*cp == ' ' || *cp =='\t')
247 while (*cp != ' ' && *cp != '\t' && *cp)
252 cgetustr(defs, "pb", &cp);
253 l_prcbeg = convexp(cp);
254 cgetustr(defs, "cb", &cp);
255 l_combeg = convexp(cp);
256 cgetustr(defs, "ce", &cp);
257 l_comend = convexp(cp);
258 cgetustr(defs, "ab", &cp);
259 l_acmbeg = convexp(cp);
260 cgetustr(defs, "ae", &cp);
261 l_acmend = convexp(cp);
262 cgetustr(defs, "sb", &cp);
263 l_strbeg = convexp(cp);
264 cgetustr(defs, "se", &cp);
265 l_strend = convexp(cp);
266 cgetustr(defs, "bb", &cp);
267 l_blkbeg = convexp(cp);
268 cgetustr(defs, "be", &cp);
269 l_blkend = convexp(cp);
270 cgetustr(defs, "lb", &cp);
271 l_chrbeg = convexp(cp);
272 cgetustr(defs, "le", &cp);
273 l_chrend = convexp(cp);
274 if (cgetustr(defs, "nc", &cp) >= 0)
275 l_nocom = convexp(cp);
277 l_onecase = (cgetcap(defs, "oc", ':') != NULL);
278 l_toplex = (cgetcap(defs, "tl", ':') != NULL);
280 /* initialize the program */
287 for (psptr=0; psptr<PSMAX; psptr++) {
288 pstack[psptr][0] = '\0';
294 printf(".ds =F %s\n", fname);
304 fstat(fileno(stdin), &stbuf);
305 cp = ctime(&stbuf.st_mtime);
308 printf(".ds =M %s %s\n", cp+4, cp+20);
314 while (fgets(buf, sizeof buf, stdin) != NULL) {
315 if (buf[0] == '\f') {
320 if (!strncmp (buf+1, "vS", 2))
322 if (!strncmp (buf+1, "vE", 2))
331 if (prccont && (psptr >= 0)) {
337 printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
346 #define isidchr(c) (isalnum(c) || (c) == '_')
352 register char *s = os; /* pointer to unmatched string */
353 char dummy[BUFSIZ]; /* dummy to be used by expmatch */
354 char *comptr; /* end of a comment delimiter */
355 char *acmptr; /* end of a comment delimiter */
356 char *strptr; /* end of a string delimiter */
357 char *chrptr; /* end of a character const delimiter */
358 char *blksptr; /* end of a lexical block start */
359 char *blkeptr; /* end of a lexical block end */
360 char *nocomptr; /* end of a non-comment delimiter */
362 s_start = os; /* remember the start for expmatch */
364 if (nokeyw || incomm || instr)
372 strncpy (pstack[psptr], pname, PNAMELEN);
373 pstack[psptr][PNAMELEN] = '\0';
374 plstack[psptr] = blklevel;
379 /* check for string, comment, blockstart, etc */
380 if (!incomm && !instr && !inchr) {
382 blkeptr = expmatch (s, l_blkend, dummy);
383 blksptr = expmatch (s, l_blkbeg, dummy);
384 comptr = expmatch (s, l_combeg, dummy);
385 acmptr = expmatch (s, l_acmbeg, dummy);
386 strptr = expmatch (s, l_strbeg, dummy);
387 chrptr = expmatch (s, l_chrbeg, dummy);
388 nocomptr = expmatch (s, l_nocom, dummy);
390 /* start of non-comment? */
392 if ((nocomptr <= comptr || comptr == NIL)
393 && (nocomptr <= acmptr || acmptr == NIL)) {
394 /* continue after non-comment */
395 putKcp (s, nocomptr-1, false);
400 /* start of a comment? */
402 if ((comptr < strptr || strptr == NIL)
403 && (comptr < acmptr || acmptr == NIL)
404 && (comptr < chrptr || chrptr == NIL)
405 && (comptr < blksptr || blksptr == NIL)
406 && (comptr < blkeptr || blkeptr == NIL)) {
407 putKcp (s, comptr-1, false);
417 /* start of a comment? */
419 if ((acmptr < strptr || strptr == NIL)
420 && (acmptr < chrptr || chrptr == NIL)
421 && (acmptr < blksptr || blksptr == NIL)
422 && (acmptr < blkeptr || blkeptr == NIL)) {
423 putKcp (s, acmptr-1, false);
433 /* start of a string? */
435 if ((strptr < chrptr || chrptr == NIL)
436 && (strptr < blksptr || blksptr == NIL)
437 && (strptr < blkeptr || blkeptr == NIL)) {
438 putKcp(s, strptr-1, false);
444 /* start of a character string? */
446 if ((chrptr < blksptr || blksptr == NIL)
447 && (chrptr < blkeptr || blkeptr == NIL)) {
448 putKcp(s, chrptr-1, false);
454 /* end of a lexical block */
455 if (blkeptr != NIL) {
456 if (blkeptr < blksptr || blksptr == NIL) {
457 putKcp(s, blkeptr - 1, false);
459 if (blklevel > 0 /* sanity */)
461 if (psptr >= 0 && plstack[psptr] >= blklevel) {
463 /* end of current procedure */
467 blklevel = plstack[psptr];
469 /* see if we should print the last proc name */
479 /* start of a lexical block */
480 if (blksptr != NIL) {
481 putKcp(s, blksptr - 1, false);
487 /* check for end of comment */
489 comptr = expmatch (s, l_comend, dummy);
490 acmptr = expmatch (s, l_acmend, dummy);
491 if (((comtype == STANDARD) && (comptr != NIL)) ||
492 ((comtype == ALTERNATE) && (acmptr != NIL))) {
493 if (comtype == STANDARD) {
494 putKcp(s, comptr-1, true);
497 putKcp(s, acmptr-1, true);
504 putKcp(s, s + strlen(s) -1, true);
509 /* check for end of string */
511 if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
512 putKcp(s, strptr-1, true);
517 putKcp(s, s+strlen(s)-1, true);
522 /* check for end of character string */
524 if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
525 putKcp(s, chrptr-1, true);
530 putKcp(s, s+strlen(s)-1, true);
536 /* print out the line */
537 putKcp(s, s + strlen(s) -1, false);
543 * start: start of string to write
544 * end: end of string to write
545 * force: true if we should force nokeyw
548 putKcp(char *start, char *end, bool force)
553 while (start <= end) {
555 if (*start == ' ' || *start == '\t') {
560 while (*start == ' ' || *start == '\t')
566 /* take care of nice tab stops */
567 if (*start == '\t') {
568 while (*start == '\t')
570 i = tabs(s_start, start) - margin / 8;
571 printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
575 if (!nokeyw && !force)
576 if ((*start == '#' || isidchr(*start))
577 && (start == s_start || !isidchr(start[-1]))) {
582 putcp((unsigned char)*start++);
589 putcp ((unsigned char)*start++);
595 tabs(char *s, char *os)
598 return (width(s, os) / 8);
602 width(register char *s, register char *os)
674 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
679 putchar('^'), c |= '@';
687 * look for a process beginning on this line
693 if (!l_toplex || blklevel == 0)
694 if (expmatch (s, l_prcbeg, pname) != NIL) {
701 /* iskw - check to see if the next word is a keyword
705 iskw(register char *s)
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]))