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"
62 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
66 #define STRLEN 10 /* length of strings introducing things */
67 #define PNAMELEN 40 /* length of a function/procedure name */
68 #define PSMAX 20 /* size of procedure name stacking */
70 static int iskw(char *);
71 static bool isproc(char *);
72 static void putKcp(char *, char *, bool);
73 static void putScp(char *);
74 static void putcp(int);
75 static int tabs(char *, char *);
76 static int width(char *, char *);
82 static bool filter = false; /* act as a filter (like eqn) */
83 static bool inchr; /* in a string constant */
84 static bool incomm; /* in a comment of the primary type */
85 static bool idx = false; /* form an index */
86 static bool instr; /* in a string constant */
87 static bool nokeyw = false; /* no keywords being flagged */
88 static bool pass = false; /*
89 * when acting as a filter, pass indicates
90 * whether we are currently processing
94 static int blklevel; /* current nesting level */
95 static int comtype; /* type of comment */
96 static char * defsfile[2] = { _PATH_VGRINDEFS, 0 };
97 /* name of language definitions file */
99 static int plstack[PSMAX]; /* the procedure nesting level stack */
100 static char pname[BUFSIZ+1];
101 static bool prccont; /* continue last procedure */
102 static int psptr; /* the stack index of the current procedure */
103 static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
106 * The language specific globals
109 char *l_acmbeg; /* string introducing a comment */
110 char *l_acmend; /* string ending a comment */
111 char *l_blkbeg; /* string beginning of a block */
112 char *l_blkend; /* string ending a block */
113 char *l_chrbeg; /* delimiter for character constant */
114 char *l_chrend; /* delimiter for character constant */
115 char *l_combeg; /* string introducing a comment */
116 char *l_comend; /* string ending a comment */
117 char l_escape; /* character used to escape characters */
118 char *l_keywds[BUFSIZ/2]; /* keyword table address */
119 char *l_nocom; /* regexp for non-comments */
120 char *l_prcbeg; /* regular expr for procedure begin */
121 char *l_strbeg; /* delimiter for string constant */
122 char *l_strend; /* delimiter for string constant */
123 bool l_toplex; /* procedures only defined at top lex level */
124 const char *language = "c"; /* the language indicator */
126 #define ps(x) printf("%s", x)
127 static char minus[] = "-";
128 static char minusn[] = "-n";
131 main(int argc, char **argv)
133 const char *fname = "";
145 if (!strcmp(argv[0], "-h")) {
151 printf("'ds =H %s\n", argv[1]);
159 /* act as a filter like eqn */
160 if (!strcmp(argv[0], "-f")) {
162 argv[0] = argv[argc-1];
163 argv[argc-1] = minus;
167 /* take input from the standard place */
168 if (!strcmp(argv[0], "-")) {
174 if (!strcmp(argv[0], "-x")) {
179 /* indicate no keywords */
180 if (!strcmp(argv[0], "-n")) {
186 /* specify the font size */
187 if (!strncmp(argv[0], "-s", 2)) {
191 i = i * 10 + (*cp++ - '0');
192 printf("'ps %d\n'vs %d\n", i, i+1);
197 /* specify the language */
198 if (!strncmp(argv[0], "-l", 2)) {
199 language = argv[0]+2;
204 /* specify the language description file */
205 if (!strncmp(argv[0], "-d", 2)) {
206 defsfile[0] = argv[1];
212 /* open the file for input */
213 if (freopen(argv[0], "r", stdin) == NULL)
214 err(1, "%s", argv[0]);
216 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
223 * get the language definition from the defs file
225 i = cgetent(&defs, defsfile, language);
227 fprintf (stderr, "no entry for language %s\n", language);
229 } else if (i == -2) { fprintf(stderr,
230 "cannot find vgrindefs file %s\n", defsfile[0]);
232 } else if (i == -3) { fprintf(stderr,
233 "potential reference loop detected in vgrindefs file %s\n",
237 if (cgetustr(defs, "kw", &cp) == -1)
244 while (*cp == ' ' || *cp =='\t')
248 while (*cp != ' ' && *cp != '\t' && *cp)
253 cgetustr(defs, "pb", &cp);
254 l_prcbeg = convexp(cp);
255 cgetustr(defs, "cb", &cp);
256 l_combeg = convexp(cp);
257 cgetustr(defs, "ce", &cp);
258 l_comend = convexp(cp);
259 cgetustr(defs, "ab", &cp);
260 l_acmbeg = convexp(cp);
261 cgetustr(defs, "ae", &cp);
262 l_acmend = convexp(cp);
263 cgetustr(defs, "sb", &cp);
264 l_strbeg = convexp(cp);
265 cgetustr(defs, "se", &cp);
266 l_strend = convexp(cp);
267 cgetustr(defs, "bb", &cp);
268 l_blkbeg = convexp(cp);
269 cgetustr(defs, "be", &cp);
270 l_blkend = convexp(cp);
271 cgetustr(defs, "lb", &cp);
272 l_chrbeg = convexp(cp);
273 cgetustr(defs, "le", &cp);
274 l_chrend = convexp(cp);
275 if (cgetustr(defs, "nc", &cp) >= 0)
276 l_nocom = convexp(cp);
278 l_onecase = (cgetcap(defs, "oc", ':') != NULL);
279 l_toplex = (cgetcap(defs, "tl", ':') != NULL);
281 /* initialize the program */
288 for (psptr=0; psptr<PSMAX; psptr++) {
289 pstack[psptr][0] = '\0';
295 printf(".ds =F %s\n", fname);
305 fstat(fileno(stdin), &stbuf);
306 cp = ctime(&stbuf.st_mtime);
309 printf(".ds =M %s %s\n", cp+4, cp+20);
315 while (fgets(buf, sizeof buf, stdin) != NULL) {
316 if (buf[0] == '\f') {
321 if (!strncmp (buf+1, "vS", 2))
323 if (!strncmp (buf+1, "vE", 2))
332 if (prccont && (psptr >= 0)) {
338 printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
347 #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? */
391 if (nocomptr != NULL)
392 if ((nocomptr <= comptr || comptr == NULL)
393 && (nocomptr <= acmptr || acmptr == NULL)) {
394 /* continue after non-comment */
395 putKcp (s, nocomptr-1, false);
400 /* start of a comment? */
402 if ((comptr < strptr || strptr == NULL)
403 && (comptr < acmptr || acmptr == NULL)
404 && (comptr < chrptr || chrptr == NULL)
405 && (comptr < blksptr || blksptr == NULL)
406 && (comptr < blkeptr || blkeptr == NULL)) {
407 putKcp(s, comptr-1, false);
417 /* start of a comment? */
419 if ((acmptr < strptr || strptr == NULL)
420 && (acmptr < chrptr || chrptr == NULL)
421 && (acmptr < blksptr || blksptr == NULL)
422 && (acmptr < blkeptr || blkeptr == NULL)) {
423 putKcp(s, acmptr-1, false);
433 /* start of a string? */
435 if ((strptr < chrptr || chrptr == NULL)
436 && (strptr < blksptr || blksptr == NULL)
437 && (strptr < blkeptr || blkeptr == NULL)) {
438 putKcp(s, strptr-1, false);
444 /* start of a character string? */
446 if ((chrptr < blksptr || blksptr == NULL)
447 && (chrptr < blkeptr || blkeptr == NULL)) {
448 putKcp(s, chrptr-1, false);
454 /* end of a lexical block */
455 if (blkeptr != NULL) {
456 if (blkeptr < blksptr || blksptr == NULL) {
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 != NULL) {
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 != NULL)) ||
492 ((comtype == ALTERNATE) && (acmptr != NULL))) {
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)) != NULL) {
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)) != NULL) {
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)
622 putcp(register int c)
673 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
678 putchar('^'), c |= '@';
686 * look for a process beginning on this line
692 if (!l_toplex || blklevel == 0)
693 if (expmatch(s, l_prcbeg, pname) != NULL) {
700 /* iskw - check to see if the next word is a keyword
704 iskw(register char *s)
706 register char **ss = l_keywds;
708 register char *cp = s;
710 while (++cp, isidchr(*cp))
713 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))