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
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static const char sccsid[] = "@(#)vfontedpr.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/types.h>
52 #include "pathnames.h"
61 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
65 #define STRLEN 10 /* length of strings introducing things */
66 #define PNAMELEN 40 /* length of a function/procedure name */
67 #define PSMAX 20 /* size of procedure name stacking */
69 static int iskw(char *);
70 static bool isproc(char *);
71 static void putKcp(char *, char *, bool);
72 static void putScp(char *);
73 static void putcp(int);
74 static int tabs(char *, char *);
75 static int width(char *, char *);
81 static bool filter = false; /* act as a filter (like eqn) */
82 static bool inchr; /* in a string constant */
83 static bool incomm; /* in a comment of the primary type */
84 static bool idx = false; /* form an index */
85 static bool instr; /* in a string constant */
86 static bool nokeyw = false; /* no keywords being flagged */
87 static bool pass = false; /*
88 * when acting as a filter, pass indicates
89 * whether we are currently processing
93 static int blklevel; /* current nesting level */
94 static int comtype; /* type of comment */
95 static char * defsfile[2] = { _PATH_VGRINDEFS, 0 };
96 /* name of language definitions file */
98 static int plstack[PSMAX]; /* the procedure nesting level stack */
99 static char pname[BUFSIZ+1];
100 static bool prccont; /* continue last procedure */
101 static int psptr; /* the stack index of the current procedure */
102 static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
105 * The language specific globals
108 char *l_acmbeg; /* string introducing a comment */
109 char *l_acmend; /* string ending a comment */
110 char *l_blkbeg; /* string beginning of a block */
111 char *l_blkend; /* string ending a block */
112 char *l_chrbeg; /* delimiter for character constant */
113 char *l_chrend; /* delimiter for character constant */
114 char *l_combeg; /* string introducing a comment */
115 char *l_comend; /* string ending a comment */
116 char l_escape; /* character used to escape characters */
117 char *l_keywds[BUFSIZ/2]; /* keyword table address */
118 char *l_nocom; /* regexp for non-comments */
119 char *l_prcbeg; /* regular expr for procedure begin */
120 char *l_strbeg; /* delimiter for string constant */
121 char *l_strend; /* delimiter for string constant */
122 bool l_toplex; /* procedures only defined at top lex level */
123 const char *language = "c"; /* the language indicator */
125 #define ps(x) printf("%s", x)
126 static char minus[] = "-";
127 static char minusn[] = "-n";
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] = minus;
166 /* take input from the standard place */
167 if (!strcmp(argv[0], "-")) {
173 if (!strcmp(argv[0], "-x")) {
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) == '_')
351 register char *s = os; /* pointer to unmatched string */
352 char dummy[BUFSIZ]; /* dummy to be used by expmatch */
353 char *comptr; /* end of a comment delimiter */
354 char *acmptr; /* end of a comment delimiter */
355 char *strptr; /* end of a string delimiter */
356 char *chrptr; /* end of a character const delimiter */
357 char *blksptr; /* end of a lexical block start */
358 char *blkeptr; /* end of a lexical block end */
359 char *nocomptr; /* end of a non-comment delimiter */
361 s_start = os; /* remember the start for expmatch */
363 if (nokeyw || incomm || instr)
371 strncpy (pstack[psptr], pname, PNAMELEN);
372 pstack[psptr][PNAMELEN] = '\0';
373 plstack[psptr] = blklevel;
378 /* check for string, comment, blockstart, etc */
379 if (!incomm && !instr && !inchr) {
381 blkeptr = expmatch(s, l_blkend, dummy);
382 blksptr = expmatch(s, l_blkbeg, dummy);
383 comptr = expmatch(s, l_combeg, dummy);
384 acmptr = expmatch(s, l_acmbeg, dummy);
385 strptr = expmatch(s, l_strbeg, dummy);
386 chrptr = expmatch(s, l_chrbeg, dummy);
387 nocomptr = expmatch (s, l_nocom, dummy);
389 /* start of non-comment? */
390 if (nocomptr != NULL)
391 if ((nocomptr <= comptr || comptr == NULL)
392 && (nocomptr <= acmptr || acmptr == NULL)) {
393 /* continue after non-comment */
394 putKcp (s, nocomptr-1, false);
399 /* start of a comment? */
401 if ((comptr < strptr || strptr == NULL)
402 && (comptr < acmptr || acmptr == NULL)
403 && (comptr < chrptr || chrptr == NULL)
404 && (comptr < blksptr || blksptr == NULL)
405 && (comptr < blkeptr || blkeptr == NULL)) {
406 putKcp(s, comptr-1, false);
416 /* start of a comment? */
418 if ((acmptr < strptr || strptr == NULL)
419 && (acmptr < chrptr || chrptr == NULL)
420 && (acmptr < blksptr || blksptr == NULL)
421 && (acmptr < blkeptr || blkeptr == NULL)) {
422 putKcp(s, acmptr-1, false);
432 /* start of a string? */
434 if ((strptr < chrptr || chrptr == NULL)
435 && (strptr < blksptr || blksptr == NULL)
436 && (strptr < blkeptr || blkeptr == NULL)) {
437 putKcp(s, strptr-1, false);
443 /* start of a character string? */
445 if ((chrptr < blksptr || blksptr == NULL)
446 && (chrptr < blkeptr || blkeptr == NULL)) {
447 putKcp(s, chrptr-1, false);
453 /* end of a lexical block */
454 if (blkeptr != NULL) {
455 if (blkeptr < blksptr || blksptr == NULL) {
456 putKcp(s, blkeptr - 1, false);
458 if (blklevel > 0 /* sanity */)
460 if (psptr >= 0 && plstack[psptr] >= blklevel) {
462 /* end of current procedure */
466 blklevel = plstack[psptr];
468 /* see if we should print the last proc name */
478 /* start of a lexical block */
479 if (blksptr != NULL) {
480 putKcp(s, blksptr - 1, false);
486 /* check for end of comment */
488 comptr = expmatch(s, l_comend, dummy);
489 acmptr = expmatch(s, l_acmend, dummy);
490 if (((comtype == STANDARD) && (comptr != NULL)) ||
491 ((comtype == ALTERNATE) && (acmptr != NULL))) {
492 if (comtype == STANDARD) {
493 putKcp(s, comptr-1, true);
496 putKcp(s, acmptr-1, true);
503 putKcp(s, s + strlen(s) -1, true);
508 /* check for end of string */
510 if ((strptr = expmatch(s, l_strend, dummy)) != NULL) {
511 putKcp(s, strptr-1, true);
516 putKcp(s, s+strlen(s)-1, true);
521 /* check for end of character string */
523 if ((chrptr = expmatch(s, l_chrend, dummy)) != NULL) {
524 putKcp(s, chrptr-1, true);
529 putKcp(s, s+strlen(s)-1, true);
535 /* print out the line */
536 putKcp(s, s + strlen(s) -1, false);
542 * start: start of string to write
543 * end: end of string to write
544 * force: true if we should force nokeyw
547 putKcp(char *start, char *end, bool force)
552 while (start <= end) {
554 if (*start == ' ' || *start == '\t') {
559 while (*start == ' ' || *start == '\t')
565 /* take care of nice tab stops */
566 if (*start == '\t') {
567 while (*start == '\t')
569 i = tabs(s_start, start) - margin / 8;
570 printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
574 if (!nokeyw && !force)
575 if ((*start == '#' || isidchr(*start))
576 && (start == s_start || !isidchr(start[-1]))) {
581 putcp((unsigned char)*start++);
588 putcp((unsigned char)*start++);
594 tabs(char *s, char *os)
597 return (width(s, os) / 8);
601 width(register char *s, register char *os)
621 putcp(register int c)
672 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
677 putchar('^'), c |= '@';
685 * look for a process beginning on this line
691 if (!l_toplex || blklevel == 0)
692 if (expmatch(s, l_prcbeg, pname) != NULL) {
699 /* iskw - check to see if the next word is a keyword
703 iskw(register char *s)
705 register char **ss = l_keywds;
707 register char *cp = s;
709 while (++cp, isidchr(*cp))
712 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))