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/types.h>
41 #include "pathnames.h"
50 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
54 #define STRLEN 10 /* length of strings introducing things */
55 #define PNAMELEN 40 /* length of a function/procedure name */
56 #define PSMAX 20 /* size of procedure name stacking */
58 static int iskw(char *);
59 static bool isproc(char *);
60 static void putKcp(char *, char *, bool);
61 static void putScp(char *);
62 static void putcp(int);
63 static int tabs(char *, char *);
64 static int width(char *, char *);
70 static bool filter = false; /* act as a filter (like eqn) */
71 static bool inchr; /* in a string constant */
72 static bool incomm; /* in a comment of the primary type */
73 static bool idx = false; /* form an index */
74 static bool instr; /* in a string constant */
75 static bool nokeyw = false; /* no keywords being flagged */
76 static bool pass = false; /*
77 * when acting as a filter, pass indicates
78 * whether we are currently processing
82 static int blklevel; /* current nesting level */
83 static int comtype; /* type of comment */
84 static char * defsfile[2] = { _PATH_VGRINDEFS, 0 };
85 /* name of language definitions file */
87 static int plstack[PSMAX]; /* the procedure nesting level stack */
88 static char pname[BUFSIZ+1];
89 static bool prccont; /* continue last procedure */
90 static int psptr; /* the stack index of the current procedure */
91 static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
94 * The language specific globals
97 char *l_acmbeg; /* string introducing a comment */
98 char *l_acmend; /* string ending a comment */
99 char *l_blkbeg; /* string beginning of a block */
100 char *l_blkend; /* string ending a block */
101 char *l_chrbeg; /* delimiter for character constant */
102 char *l_chrend; /* delimiter for character constant */
103 char *l_combeg; /* string introducing a comment */
104 char *l_comend; /* string ending a comment */
105 char l_escape; /* character used to escape characters */
106 char *l_keywds[BUFSIZ/2]; /* keyword table address */
107 char *l_nocom; /* regexp for non-comments */
108 char *l_prcbeg; /* regular expr for procedure begin */
109 char *l_strbeg; /* delimiter for string constant */
110 char *l_strend; /* delimiter for string constant */
111 bool l_toplex; /* procedures only defined at top lex level */
112 const char *language = "c"; /* the language indicator */
114 #define ps(x) printf("%s", x)
115 static char minus[] = "-";
116 static char minusn[] = "-n";
119 main(int argc, char **argv)
121 const char *fname = "";
133 if (!strcmp(argv[0], "-h")) {
139 printf("'ds =H %s\n", argv[1]);
147 /* act as a filter like eqn */
148 if (!strcmp(argv[0], "-f")) {
150 argv[0] = argv[argc-1];
151 argv[argc-1] = minus;
155 /* take input from the standard place */
156 if (!strcmp(argv[0], "-")) {
162 if (!strcmp(argv[0], "-x")) {
167 /* indicate no keywords */
168 if (!strcmp(argv[0], "-n")) {
174 /* specify the font size */
175 if (!strncmp(argv[0], "-s", 2)) {
179 i = i * 10 + (*cp++ - '0');
180 printf("'ps %d\n'vs %d\n", i, i+1);
185 /* specify the language */
186 if (!strncmp(argv[0], "-l", 2)) {
187 language = argv[0]+2;
192 /* specify the language description file */
193 if (!strncmp(argv[0], "-d", 2)) {
194 defsfile[0] = argv[1];
200 /* open the file for input */
201 if (freopen(argv[0], "r", stdin) == NULL)
202 err(1, "%s", argv[0]);
204 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
211 * get the language definition from the defs file
213 i = cgetent(&defs, defsfile, language);
215 fprintf (stderr, "no entry for language %s\n", language);
217 } else if (i == -2) { fprintf(stderr,
218 "cannot find vgrindefs file %s\n", defsfile[0]);
220 } else if (i == -3) { fprintf(stderr,
221 "potential reference loop detected in vgrindefs file %s\n",
225 if (cgetustr(defs, "kw", &cp) == -1)
232 while (*cp == ' ' || *cp =='\t')
236 while (*cp != ' ' && *cp != '\t' && *cp)
241 cgetustr(defs, "pb", &cp);
242 l_prcbeg = convexp(cp);
243 cgetustr(defs, "cb", &cp);
244 l_combeg = convexp(cp);
245 cgetustr(defs, "ce", &cp);
246 l_comend = convexp(cp);
247 cgetustr(defs, "ab", &cp);
248 l_acmbeg = convexp(cp);
249 cgetustr(defs, "ae", &cp);
250 l_acmend = convexp(cp);
251 cgetustr(defs, "sb", &cp);
252 l_strbeg = convexp(cp);
253 cgetustr(defs, "se", &cp);
254 l_strend = convexp(cp);
255 cgetustr(defs, "bb", &cp);
256 l_blkbeg = convexp(cp);
257 cgetustr(defs, "be", &cp);
258 l_blkend = convexp(cp);
259 cgetustr(defs, "lb", &cp);
260 l_chrbeg = convexp(cp);
261 cgetustr(defs, "le", &cp);
262 l_chrend = convexp(cp);
263 if (cgetustr(defs, "nc", &cp) >= 0)
264 l_nocom = convexp(cp);
266 l_onecase = (cgetcap(defs, "oc", ':') != NULL);
267 l_toplex = (cgetcap(defs, "tl", ':') != NULL);
269 /* initialize the program */
276 for (psptr=0; psptr<PSMAX; psptr++) {
277 pstack[psptr][0] = '\0';
283 printf(".ds =F %s\n", fname);
293 fstat(fileno(stdin), &stbuf);
294 cp = ctime(&stbuf.st_mtime);
297 printf(".ds =M %s %s\n", cp+4, cp+20);
303 while (fgets(buf, sizeof buf, stdin) != NULL) {
304 if (buf[0] == '\f') {
309 if (!strncmp (buf+1, "vS", 2))
311 if (!strncmp (buf+1, "vE", 2))
320 if (prccont && (psptr >= 0)) {
326 printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
335 #define isidchr(c) (isalnum(c) || (c) == '_')
340 register char *s = os; /* pointer to unmatched string */
341 char dummy[BUFSIZ]; /* dummy to be used by expmatch */
342 char *comptr; /* end of a comment delimiter */
343 char *acmptr; /* end of a comment delimiter */
344 char *strptr; /* end of a string delimiter */
345 char *chrptr; /* end of a character const delimiter */
346 char *blksptr; /* end of a lexical block start */
347 char *blkeptr; /* end of a lexical block end */
348 char *nocomptr; /* end of a non-comment delimiter */
350 s_start = os; /* remember the start for expmatch */
352 if (nokeyw || incomm || instr)
360 strncpy (pstack[psptr], pname, PNAMELEN);
361 pstack[psptr][PNAMELEN] = '\0';
362 plstack[psptr] = blklevel;
367 /* check for string, comment, blockstart, etc */
368 if (!incomm && !instr && !inchr) {
370 blkeptr = expmatch(s, l_blkend, dummy);
371 blksptr = expmatch(s, l_blkbeg, dummy);
372 comptr = expmatch(s, l_combeg, dummy);
373 acmptr = expmatch(s, l_acmbeg, dummy);
374 strptr = expmatch(s, l_strbeg, dummy);
375 chrptr = expmatch(s, l_chrbeg, dummy);
376 nocomptr = expmatch (s, l_nocom, dummy);
378 /* start of non-comment? */
379 if (nocomptr != NULL)
380 if ((nocomptr <= comptr || comptr == NULL)
381 && (nocomptr <= acmptr || acmptr == NULL)) {
382 /* continue after non-comment */
383 putKcp (s, nocomptr-1, false);
388 /* start of a comment? */
390 if ((comptr < strptr || strptr == NULL)
391 && (comptr < acmptr || acmptr == NULL)
392 && (comptr < chrptr || chrptr == NULL)
393 && (comptr < blksptr || blksptr == NULL)
394 && (comptr < blkeptr || blkeptr == NULL)) {
395 putKcp(s, comptr-1, false);
405 /* start of a comment? */
407 if ((acmptr < strptr || strptr == NULL)
408 && (acmptr < chrptr || chrptr == NULL)
409 && (acmptr < blksptr || blksptr == NULL)
410 && (acmptr < blkeptr || blkeptr == NULL)) {
411 putKcp(s, acmptr-1, false);
421 /* start of a string? */
423 if ((strptr < chrptr || chrptr == NULL)
424 && (strptr < blksptr || blksptr == NULL)
425 && (strptr < blkeptr || blkeptr == NULL)) {
426 putKcp(s, strptr-1, false);
432 /* start of a character string? */
434 if ((chrptr < blksptr || blksptr == NULL)
435 && (chrptr < blkeptr || blkeptr == NULL)) {
436 putKcp(s, chrptr-1, false);
442 /* end of a lexical block */
443 if (blkeptr != NULL) {
444 if (blkeptr < blksptr || blksptr == NULL) {
445 putKcp(s, blkeptr - 1, false);
447 if (blklevel > 0 /* sanity */)
449 if (psptr >= 0 && plstack[psptr] >= blklevel) {
451 /* end of current procedure */
455 blklevel = plstack[psptr];
457 /* see if we should print the last proc name */
467 /* start of a lexical block */
468 if (blksptr != NULL) {
469 putKcp(s, blksptr - 1, false);
475 /* check for end of comment */
477 comptr = expmatch(s, l_comend, dummy);
478 acmptr = expmatch(s, l_acmend, dummy);
479 if (((comtype == STANDARD) && (comptr != NULL)) ||
480 ((comtype == ALTERNATE) && (acmptr != NULL))) {
481 if (comtype == STANDARD) {
482 putKcp(s, comptr-1, true);
485 putKcp(s, acmptr-1, true);
492 putKcp(s, s + strlen(s) -1, true);
497 /* check for end of string */
499 if ((strptr = expmatch(s, l_strend, dummy)) != NULL) {
500 putKcp(s, strptr-1, true);
505 putKcp(s, s+strlen(s)-1, true);
510 /* check for end of character string */
512 if ((chrptr = expmatch(s, l_chrend, dummy)) != NULL) {
513 putKcp(s, chrptr-1, true);
518 putKcp(s, s+strlen(s)-1, true);
524 /* print out the line */
525 putKcp(s, s + strlen(s) -1, false);
531 * start: start of string to write
532 * end: end of string to write
533 * force: true if we should force nokeyw
536 putKcp(char *start, char *end, bool force)
541 while (start <= end) {
543 if (*start == ' ' || *start == '\t') {
548 while (*start == ' ' || *start == '\t')
554 /* take care of nice tab stops */
555 if (*start == '\t') {
556 while (*start == '\t')
558 i = tabs(s_start, start) - margin / 8;
559 printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
563 if (!nokeyw && !force)
564 if ((*start == '#' || isidchr(*start))
565 && (start == s_start || !isidchr(start[-1]))) {
570 putcp((unsigned char)*start++);
577 putcp((unsigned char)*start++);
583 tabs(char *s, char *os)
586 return (width(s, os) / 8);
590 width(register char *s, register char *os)
610 putcp(register int c)
661 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
666 putchar('^'), c |= '@';
674 * look for a process beginning on this line
680 if (!l_toplex || blklevel == 0)
681 if (expmatch(s, l_prcbeg, pname) != NULL) {
688 /* iskw - check to see if the next word is a keyword
692 iskw(register char *s)
694 register char **ss = l_keywds;
696 register char *cp = s;
698 while (++cp, isidchr(*cp))
701 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))