2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 static const char copyright[] =
38 "@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static const char sccsid[] = "@(#)vfontedpr.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/types.h>
55 #include "pathnames.h"
64 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
68 #define STRLEN 10 /* length of strings introducing things */
69 #define PNAMELEN 40 /* length of a function/procedure name */
70 #define PSMAX 20 /* size of procedure name stacking */
72 static int iskw(char *);
73 static bool isproc(char *);
74 static void putKcp(char *, char *, bool);
75 static void putScp(char *);
76 static void putcp(int);
77 static int tabs(char *, char *);
78 static int width(char *, char *);
84 static bool filter = false; /* act as a filter (like eqn) */
85 static bool inchr; /* in a string constant */
86 static bool incomm; /* in a comment of the primary type */
87 static bool idx = false; /* form an index */
88 static bool instr; /* in a string constant */
89 static bool nokeyw = false; /* no keywords being flagged */
90 static bool pass = false; /*
91 * when acting as a filter, pass indicates
92 * whether we are currently processing
96 static int blklevel; /* current nesting level */
97 static int comtype; /* type of comment */
98 static char * defsfile[2] = { _PATH_VGRINDEFS, 0 };
99 /* name of language definitions file */
101 static int plstack[PSMAX]; /* the procedure nesting level stack */
102 static char pname[BUFSIZ+1];
103 static bool prccont; /* continue last procedure */
104 static int psptr; /* the stack index of the current procedure */
105 static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
108 * The language specific globals
111 char *l_acmbeg; /* string introducing a comment */
112 char *l_acmend; /* string ending a comment */
113 char *l_blkbeg; /* string beginning of a block */
114 char *l_blkend; /* string ending a block */
115 char *l_chrbeg; /* delimiter for character constant */
116 char *l_chrend; /* delimiter for character constant */
117 char *l_combeg; /* string introducing a comment */
118 char *l_comend; /* string ending a comment */
119 char l_escape; /* character used to escape characters */
120 char *l_keywds[BUFSIZ/2]; /* keyword table address */
121 char *l_nocom; /* regexp for non-comments */
122 char *l_prcbeg; /* regular expr for procedure begin */
123 char *l_strbeg; /* delimiter for string constant */
124 char *l_strend; /* delimiter for string constant */
125 bool l_toplex; /* procedures only defined at top lex level */
126 const char *language = "c"; /* the language indicator */
128 #define ps(x) printf("%s", x)
129 static char minus[] = "-";
130 static char minusn[] = "-n";
133 main(int argc, char **argv)
135 const char *fname = "";
147 if (!strcmp(argv[0], "-h")) {
153 printf("'ds =H %s\n", argv[1]);
161 /* act as a filter like eqn */
162 if (!strcmp(argv[0], "-f")) {
164 argv[0] = argv[argc-1];
165 argv[argc-1] = minus;
169 /* take input from the standard place */
170 if (!strcmp(argv[0], "-")) {
176 if (!strcmp(argv[0], "-x")) {
181 /* indicate no keywords */
182 if (!strcmp(argv[0], "-n")) {
188 /* specify the font size */
189 if (!strncmp(argv[0], "-s", 2)) {
193 i = i * 10 + (*cp++ - '0');
194 printf("'ps %d\n'vs %d\n", i, i+1);
199 /* specify the language */
200 if (!strncmp(argv[0], "-l", 2)) {
201 language = argv[0]+2;
206 /* specify the language description file */
207 if (!strncmp(argv[0], "-d", 2)) {
208 defsfile[0] = argv[1];
214 /* open the file for input */
215 if (freopen(argv[0], "r", stdin) == NULL)
216 err(1, "%s", argv[0]);
218 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
225 * get the language definition from the defs file
227 i = cgetent(&defs, defsfile, language);
229 fprintf (stderr, "no entry for language %s\n", language);
231 } else if (i == -2) { fprintf(stderr,
232 "cannot find vgrindefs file %s\n", defsfile[0]);
234 } else if (i == -3) { fprintf(stderr,
235 "potential reference loop detected in vgrindefs file %s\n",
239 if (cgetustr(defs, "kw", &cp) == -1)
246 while (*cp == ' ' || *cp =='\t')
250 while (*cp != ' ' && *cp != '\t' && *cp)
255 cgetustr(defs, "pb", &cp);
256 l_prcbeg = convexp(cp);
257 cgetustr(defs, "cb", &cp);
258 l_combeg = convexp(cp);
259 cgetustr(defs, "ce", &cp);
260 l_comend = convexp(cp);
261 cgetustr(defs, "ab", &cp);
262 l_acmbeg = convexp(cp);
263 cgetustr(defs, "ae", &cp);
264 l_acmend = convexp(cp);
265 cgetustr(defs, "sb", &cp);
266 l_strbeg = convexp(cp);
267 cgetustr(defs, "se", &cp);
268 l_strend = convexp(cp);
269 cgetustr(defs, "bb", &cp);
270 l_blkbeg = convexp(cp);
271 cgetustr(defs, "be", &cp);
272 l_blkend = convexp(cp);
273 cgetustr(defs, "lb", &cp);
274 l_chrbeg = convexp(cp);
275 cgetustr(defs, "le", &cp);
276 l_chrend = convexp(cp);
277 if (cgetustr(defs, "nc", &cp) >= 0)
278 l_nocom = convexp(cp);
280 l_onecase = (cgetcap(defs, "oc", ':') != NULL);
281 l_toplex = (cgetcap(defs, "tl", ':') != NULL);
283 /* initialize the program */
290 for (psptr=0; psptr<PSMAX; psptr++) {
291 pstack[psptr][0] = '\0';
297 printf(".ds =F %s\n", fname);
307 fstat(fileno(stdin), &stbuf);
308 cp = ctime(&stbuf.st_mtime);
311 printf(".ds =M %s %s\n", cp+4, cp+20);
317 while (fgets(buf, sizeof buf, stdin) != NULL) {
318 if (buf[0] == '\f') {
323 if (!strncmp (buf+1, "vS", 2))
325 if (!strncmp (buf+1, "vE", 2))
334 if (prccont && (psptr >= 0)) {
340 printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
349 #define isidchr(c) (isalnum(c) || (c) == '_')
354 register char *s = os; /* pointer to unmatched string */
355 char dummy[BUFSIZ]; /* dummy to be used by expmatch */
356 char *comptr; /* end of a comment delimiter */
357 char *acmptr; /* end of a comment delimiter */
358 char *strptr; /* end of a string delimiter */
359 char *chrptr; /* end of a character const delimiter */
360 char *blksptr; /* end of a lexical block start */
361 char *blkeptr; /* end of a lexical block end */
362 char *nocomptr; /* end of a non-comment delimiter */
364 s_start = os; /* remember the start for expmatch */
366 if (nokeyw || incomm || instr)
374 strncpy (pstack[psptr], pname, PNAMELEN);
375 pstack[psptr][PNAMELEN] = '\0';
376 plstack[psptr] = blklevel;
381 /* check for string, comment, blockstart, etc */
382 if (!incomm && !instr && !inchr) {
384 blkeptr = expmatch(s, l_blkend, dummy);
385 blksptr = expmatch(s, l_blkbeg, dummy);
386 comptr = expmatch(s, l_combeg, dummy);
387 acmptr = expmatch(s, l_acmbeg, dummy);
388 strptr = expmatch(s, l_strbeg, dummy);
389 chrptr = expmatch(s, l_chrbeg, dummy);
390 nocomptr = expmatch (s, l_nocom, dummy);
392 /* start of non-comment? */
393 if (nocomptr != NULL)
394 if ((nocomptr <= comptr || comptr == NULL)
395 && (nocomptr <= acmptr || acmptr == NULL)) {
396 /* continue after non-comment */
397 putKcp (s, nocomptr-1, false);
402 /* start of a comment? */
404 if ((comptr < strptr || strptr == NULL)
405 && (comptr < acmptr || acmptr == NULL)
406 && (comptr < chrptr || chrptr == NULL)
407 && (comptr < blksptr || blksptr == NULL)
408 && (comptr < blkeptr || blkeptr == NULL)) {
409 putKcp(s, comptr-1, false);
419 /* start of a comment? */
421 if ((acmptr < strptr || strptr == NULL)
422 && (acmptr < chrptr || chrptr == NULL)
423 && (acmptr < blksptr || blksptr == NULL)
424 && (acmptr < blkeptr || blkeptr == NULL)) {
425 putKcp(s, acmptr-1, false);
435 /* start of a string? */
437 if ((strptr < chrptr || chrptr == NULL)
438 && (strptr < blksptr || blksptr == NULL)
439 && (strptr < blkeptr || blkeptr == NULL)) {
440 putKcp(s, strptr-1, false);
446 /* start of a character string? */
448 if ((chrptr < blksptr || blksptr == NULL)
449 && (chrptr < blkeptr || blkeptr == NULL)) {
450 putKcp(s, chrptr-1, false);
456 /* end of a lexical block */
457 if (blkeptr != NULL) {
458 if (blkeptr < blksptr || blksptr == NULL) {
459 putKcp(s, blkeptr - 1, false);
461 if (blklevel > 0 /* sanity */)
463 if (psptr >= 0 && plstack[psptr] >= blklevel) {
465 /* end of current procedure */
469 blklevel = plstack[psptr];
471 /* see if we should print the last proc name */
481 /* start of a lexical block */
482 if (blksptr != NULL) {
483 putKcp(s, blksptr - 1, false);
489 /* check for end of comment */
491 comptr = expmatch(s, l_comend, dummy);
492 acmptr = expmatch(s, l_acmend, dummy);
493 if (((comtype == STANDARD) && (comptr != NULL)) ||
494 ((comtype == ALTERNATE) && (acmptr != NULL))) {
495 if (comtype == STANDARD) {
496 putKcp(s, comptr-1, true);
499 putKcp(s, acmptr-1, true);
506 putKcp(s, s + strlen(s) -1, true);
511 /* check for end of string */
513 if ((strptr = expmatch(s, l_strend, dummy)) != NULL) {
514 putKcp(s, strptr-1, true);
519 putKcp(s, s+strlen(s)-1, true);
524 /* check for end of character string */
526 if ((chrptr = expmatch(s, l_chrend, dummy)) != NULL) {
527 putKcp(s, chrptr-1, true);
532 putKcp(s, s+strlen(s)-1, true);
538 /* print out the line */
539 putKcp(s, s + strlen(s) -1, false);
545 * start: start of string to write
546 * end: end of string to write
547 * force: true if we should force nokeyw
550 putKcp(char *start, char *end, bool force)
555 while (start <= end) {
557 if (*start == ' ' || *start == '\t') {
562 while (*start == ' ' || *start == '\t')
568 /* take care of nice tab stops */
569 if (*start == '\t') {
570 while (*start == '\t')
572 i = tabs(s_start, start) - margin / 8;
573 printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
577 if (!nokeyw && !force)
578 if ((*start == '#' || isidchr(*start))
579 && (start == s_start || !isidchr(start[-1]))) {
584 putcp((unsigned char)*start++);
591 putcp((unsigned char)*start++);
597 tabs(char *s, char *os)
600 return (width(s, os) / 8);
604 width(register char *s, register char *os)
624 putcp(register int c)
675 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
680 putchar('^'), c |= '@';
688 * look for a process beginning on this line
694 if (!l_toplex || blklevel == 0)
695 if (expmatch(s, l_prcbeg, pname) != NULL) {
702 /* iskw - check to see if the next word is a keyword
706 iskw(register char *s)
708 register char **ss = l_keywds;
710 register char *cp = s;
712 while (++cp, isidchr(*cp))
715 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))