bsd.lib.mk: Add INTERNALLIBPROF knob.
[dragonfly.git] / usr.bin / vgrind / vfontedpr.c
blob9114512ad99ed210809dea30bd05d0c9ba449a7c
1 /*
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
7 * are met:
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 * 3. 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
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)vfontedpr.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/vgrind/vfontedpr.c,v 1.12 1999/08/28 01:07:21 peter Exp $
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include "pathnames.h"
43 #include "extern.h"
45 #define FALSE 0
46 #define TRUE !(FALSE)
47 #define NIL 0
48 #define STANDARD 0
49 #define ALTERNATE 1
52 * Vfontedpr.
54 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
58 #define STRLEN 10 /* length of strings introducing things */
59 #define PNAMELEN 40 /* length of a function/procedure name */
60 #define PSMAX 20 /* size of procedure name stacking */
62 static int iskw(char *);
63 static boolean isproc(char *);
64 static void putKcp(char *, char *, boolean);
65 static void putScp(char *);
66 static void putcp(int);
67 static int tabs(char *, char *);
68 static int width(char *, char *);
71 * The state variables
74 static boolean filter = FALSE; /* act as a filter (like eqn) */
75 static boolean inchr; /* in a string constant */
76 static boolean incomm; /* in a comment of the primary type */
77 static boolean idx = FALSE; /* form an index */
78 static boolean instr; /* in a string constant */
79 static boolean nokeyw = FALSE; /* no keywords being flagged */
80 static boolean pass = FALSE; /*
81 * when acting as a filter, pass indicates
82 * whether we are currently processing
83 * input.
86 static int blklevel; /* current nesting level */
87 static int comtype; /* type of comment */
88 static char *defsfile[2] = { _PATH_VGRINDEFS, 0 };
89 /* name of language definitions file */
90 static int margin;
91 static int plstack[PSMAX]; /* the procedure nesting level stack */
92 static char pname[BUFSIZ+1];
93 static boolean prccont; /* continue last procedure */
94 static int psptr; /* the stack index of the current procedure */
95 static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
98 * The language specific globals
101 char *l_acmbeg; /* string introducing a comment */
102 char *l_acmend; /* string ending a comment */
103 char *l_blkbeg; /* string begining of a block */
104 char *l_blkend; /* string ending a block */
105 char *l_chrbeg; /* delimiter for character constant */
106 char *l_chrend; /* delimiter for character constant */
107 char *l_combeg; /* string introducing a comment */
108 char *l_comend; /* string ending a comment */
109 char l_escape; /* character used to escape characters */
110 char *l_keywds[BUFSIZ/2]; /* keyword table address */
111 char *l_nocom; /* regexp for non-comments */
112 char *l_prcbeg; /* regular expr for procedure begin */
113 char *l_strbeg; /* delimiter for string constant */
114 char *l_strend; /* delimiter for string constant */
115 boolean l_toplex; /* procedures only defined at top lex level */
116 char *language = "c"; /* the language indicator */
118 #define ps(x) printf("%s", x)
121 main(int argc, char **argv)
123 char *fname = "";
124 struct stat stbuf;
125 char buf[BUFSIZ];
126 char *defs;
127 int needbp = 0;
129 argc--, argv++;
130 do {
131 char *cp;
132 int i;
134 if (argc > 0) {
135 if (!strcmp(argv[0], "-h")) {
136 if (argc == 1) {
137 printf("'ds =H\n");
138 argc = 0;
139 goto rest;
141 printf("'ds =H %s\n", argv[1]);
142 argc--, argv++;
143 argc--, argv++;
144 if (argc > 0)
145 continue;
146 goto rest;
149 /* act as a filter like eqn */
150 if (!strcmp(argv[0], "-f")) {
151 filter++;
152 argv[0] = argv[argc-1];
153 argv[argc-1] = "-";
154 continue;
157 /* take input from the standard place */
158 if (!strcmp(argv[0], "-")) {
159 argc = 0;
160 goto rest;
163 /* build an index */
164 if (!strcmp(argv[0], "-x")) {
165 idx++;
166 argv[0] = "-n";
169 /* indicate no keywords */
170 if (!strcmp(argv[0], "-n")) {
171 nokeyw++;
172 argc--, argv++;
173 continue;
176 /* specify the font size */
177 if (!strncmp(argv[0], "-s", 2)) {
178 i = 0;
179 cp = argv[0] + 2;
180 while (*cp)
181 i = i * 10 + (*cp++ - '0');
182 printf("'ps %d\n'vs %d\n", i, i+1);
183 argc--, argv++;
184 continue;
187 /* specify the language */
188 if (!strncmp(argv[0], "-l", 2)) {
189 language = argv[0]+2;
190 argc--, argv++;
191 continue;
194 /* specify the language description file */
195 if (!strncmp(argv[0], "-d", 2)) {
196 defsfile[0] = argv[1];
197 argc--, argv++;
198 argc--, argv++;
199 continue;
202 /* open the file for input */
203 if (freopen(argv[0], "r", stdin) == NULL)
204 err(1, "%s", argv[0]);
205 if (idx)
206 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
207 fname = argv[0];
208 argc--, argv++;
210 rest:
213 * get the language definition from the defs file
215 i = cgetent(&defs, defsfile, language);
216 if (i == -1) {
217 fprintf (stderr, "no entry for language %s\n", language);
218 exit (0);
219 } else if (i == -2) { fprintf(stderr,
220 "cannot find vgrindefs file %s\n", defsfile[0]);
221 exit (0);
222 } else if (i == -3) { fprintf(stderr,
223 "potential reference loop detected in vgrindefs file %s\n",
224 defsfile[0]);
225 exit(0);
227 if (cgetustr(defs, "kw", &cp) == -1)
228 nokeyw = TRUE;
229 else {
230 char **cpp;
232 cpp = l_keywds;
233 while (*cp) {
234 while (*cp == ' ' || *cp =='\t')
235 *cp++ = '\0';
236 if (*cp)
237 *cpp++ = cp;
238 while (*cp != ' ' && *cp != '\t' && *cp)
239 cp++;
241 *cpp = NIL;
243 cgetustr(defs, "pb", &cp);
244 l_prcbeg = convexp(cp);
245 cgetustr(defs, "cb", &cp);
246 l_combeg = convexp(cp);
247 cgetustr(defs, "ce", &cp);
248 l_comend = convexp(cp);
249 cgetustr(defs, "ab", &cp);
250 l_acmbeg = convexp(cp);
251 cgetustr(defs, "ae", &cp);
252 l_acmend = convexp(cp);
253 cgetustr(defs, "sb", &cp);
254 l_strbeg = convexp(cp);
255 cgetustr(defs, "se", &cp);
256 l_strend = convexp(cp);
257 cgetustr(defs, "bb", &cp);
258 l_blkbeg = convexp(cp);
259 cgetustr(defs, "be", &cp);
260 l_blkend = convexp(cp);
261 cgetustr(defs, "lb", &cp);
262 l_chrbeg = convexp(cp);
263 cgetustr(defs, "le", &cp);
264 l_chrend = convexp(cp);
265 if (cgetustr(defs, "nc", &cp) >= 0)
266 l_nocom = convexp(cp);
267 l_escape = '\\';
268 l_onecase = (cgetcap(defs, "oc", ':') != NULL);
269 l_toplex = (cgetcap(defs, "tl", ':') != NULL);
271 /* initialize the program */
273 incomm = FALSE;
274 instr = FALSE;
275 inchr = FALSE;
276 _escaped = FALSE;
277 blklevel = 0;
278 for (psptr=0; psptr<PSMAX; psptr++) {
279 pstack[psptr][0] = '\0';
280 plstack[psptr] = 0;
282 psptr = -1;
283 ps("'-F\n");
284 if (!filter) {
285 printf(".ds =F %s\n", fname);
286 ps("'wh 0 vH\n");
287 ps("'wh -1i vF\n");
289 if (needbp) {
290 needbp = 0;
291 printf(".()\n");
292 printf(".bp\n");
294 if (!filter) {
295 fstat(fileno(stdin), &stbuf);
296 cp = ctime(&stbuf.st_mtime);
297 cp[16] = '\0';
298 cp[24] = '\0';
299 printf(".ds =M %s %s\n", cp+4, cp+20);
303 * MAIN LOOP!!!
305 while (fgets(buf, sizeof buf, stdin) != NULL) {
306 if (buf[0] == '\f') {
307 printf(".bp\n");
309 if (buf[0] == '.') {
310 printf("%s", buf);
311 if (!strncmp (buf+1, "vS", 2))
312 pass = TRUE;
313 if (!strncmp (buf+1, "vE", 2))
314 pass = FALSE;
315 continue;
317 prccont = FALSE;
318 if (!filter || pass)
319 putScp(buf);
320 else
321 printf("%s", buf);
322 if (prccont && (psptr >= 0)) {
323 ps("'FC ");
324 ps(pstack[psptr]);
325 ps("\n");
327 #ifdef DEBUG
328 printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
329 #endif
330 margin = 0;
332 needbp = 1;
333 } while (argc > 0);
334 exit(0);
337 #define isidchr(c) (isalnum(c) || (c) == '_')
339 static void
340 putScp(char *os)
342 char *s = os; /* pointer to unmatched string */
343 char dummy[BUFSIZ]; /* dummy to be used by expmatch */
344 char *comptr; /* end of a comment delimiter */
345 char *acmptr; /* end of a comment delimiter */
346 char *strptr; /* end of a string delimiter */
347 char *chrptr; /* end of a character const delimiter */
348 char *blksptr; /* end of a lexical block start */
349 char *blkeptr; /* end of a lexical block end */
350 char *nocomptr; /* end of a non-comment delimiter */
352 s_start = os; /* remember the start for expmatch */
353 _escaped = FALSE;
354 if (nokeyw || incomm || instr)
355 goto skip;
356 if (isproc(s)) {
357 ps("'FN ");
358 ps(pname);
359 ps("\n");
360 if (psptr < PSMAX) {
361 ++psptr;
362 strncpy (pstack[psptr], pname, PNAMELEN);
363 pstack[psptr][PNAMELEN] = '\0';
364 plstack[psptr] = blklevel;
367 skip:
368 do {
369 /* check for string, comment, blockstart, etc */
370 if (!incomm && !instr && !inchr) {
372 blkeptr = expmatch (s, l_blkend, dummy);
373 blksptr = expmatch (s, l_blkbeg, dummy);
374 comptr = expmatch (s, l_combeg, dummy);
375 acmptr = expmatch (s, l_acmbeg, dummy);
376 strptr = expmatch (s, l_strbeg, dummy);
377 chrptr = expmatch (s, l_chrbeg, dummy);
378 nocomptr = expmatch (s, l_nocom, dummy);
380 /* start of non-comment? */
381 if (nocomptr != NIL)
382 if ((nocomptr <= comptr || comptr == NIL)
383 && (nocomptr <= acmptr || acmptr == NIL)) {
384 /* continue after non-comment */
385 putKcp (s, nocomptr-1, FALSE);
386 s = nocomptr;
387 continue;
390 /* start of a comment? */
391 if (comptr != NIL)
392 if ((comptr < strptr || strptr == NIL)
393 && (comptr < acmptr || acmptr == NIL)
394 && (comptr < chrptr || chrptr == NIL)
395 && (comptr < blksptr || blksptr == NIL)
396 && (comptr < blkeptr || blkeptr == NIL)) {
397 putKcp (s, comptr-1, FALSE);
398 s = comptr;
399 incomm = TRUE;
400 comtype = STANDARD;
401 if (s != os)
402 ps ("\\c");
403 ps ("\\c\n'+C\n");
404 continue;
407 /* start of a comment? */
408 if (acmptr != NIL)
409 if ((acmptr < strptr || strptr == NIL)
410 && (acmptr < chrptr || chrptr == NIL)
411 && (acmptr < blksptr || blksptr == NIL)
412 && (acmptr < blkeptr || blkeptr == NIL)) {
413 putKcp (s, acmptr-1, FALSE);
414 s = acmptr;
415 incomm = TRUE;
416 comtype = ALTERNATE;
417 if (s != os)
418 ps ("\\c");
419 ps ("\\c\n'+C\n");
420 continue;
423 /* start of a string? */
424 if (strptr != NIL)
425 if ((strptr < chrptr || chrptr == NIL)
426 && (strptr < blksptr || blksptr == NIL)
427 && (strptr < blkeptr || blkeptr == NIL)) {
428 putKcp (s, strptr-1, FALSE);
429 s = strptr;
430 instr = TRUE;
431 continue;
434 /* start of a character string? */
435 if (chrptr != NIL)
436 if ((chrptr < blksptr || blksptr == NIL)
437 && (chrptr < blkeptr || blkeptr == NIL)) {
438 putKcp (s, chrptr-1, FALSE);
439 s = chrptr;
440 inchr = TRUE;
441 continue;
444 /* end of a lexical block */
445 if (blkeptr != NIL) {
446 if (blkeptr < blksptr || blksptr == NIL) {
447 putKcp (s, blkeptr - 1, FALSE);
448 s = blkeptr;
449 if (blklevel > 0 /* sanity */)
450 blklevel--;
451 if (psptr >= 0 && plstack[psptr] >= blklevel) {
453 /* end of current procedure */
454 if (s != os)
455 ps ("\\c");
456 ps ("\\c\n'-F\n");
457 blklevel = plstack[psptr];
459 /* see if we should print the last proc name */
460 if (--psptr >= 0)
461 prccont = TRUE;
462 else
463 psptr = -1;
465 continue;
469 /* start of a lexical block */
470 if (blksptr != NIL) {
471 putKcp (s, blksptr - 1, FALSE);
472 s = blksptr;
473 blklevel++;
474 continue;
477 /* check for end of comment */
478 } else if (incomm) {
479 comptr = expmatch (s, l_comend, dummy);
480 acmptr = expmatch (s, l_acmend, dummy);
481 if (((comtype == STANDARD) && (comptr != NIL)) ||
482 ((comtype == ALTERNATE) && (acmptr != NIL))) {
483 if (comtype == STANDARD) {
484 putKcp (s, comptr-1, TRUE);
485 s = comptr;
486 } else {
487 putKcp (s, acmptr-1, TRUE);
488 s = acmptr;
490 incomm = FALSE;
491 ps("\\c\n'-C\n");
492 continue;
493 } else {
494 putKcp (s, s + strlen(s) -1, TRUE);
495 s = s + strlen(s);
496 continue;
499 /* check for end of string */
500 } else if (instr) {
501 if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
502 putKcp (s, strptr-1, TRUE);
503 s = strptr;
504 instr = FALSE;
505 continue;
506 } else {
507 putKcp (s, s+strlen(s)-1, TRUE);
508 s = s + strlen(s);
509 continue;
512 /* check for end of character string */
513 } else if (inchr) {
514 if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
515 putKcp (s, chrptr-1, TRUE);
516 s = chrptr;
517 inchr = FALSE;
518 continue;
519 } else {
520 putKcp (s, s+strlen(s)-1, TRUE);
521 s = s + strlen(s);
522 continue;
526 /* print out the line */
527 putKcp (s, s + strlen(s) -1, FALSE);
528 s = s + strlen(s);
529 } while (*s);
532 /* force: true if we should force nokeyw */
533 static void
534 putKcp(char *start, char *end, boolean force)
536 int i;
537 int xfld = 0;
539 while (start <= end) {
540 if (idx) {
541 if (*start == ' ' || *start == '\t') {
542 if (xfld == 0)
543 printf("\001");
544 printf("\t");
545 xfld = 1;
546 while (*start == ' ' || *start == '\t')
547 start++;
548 continue;
552 /* take care of nice tab stops */
553 if (*start == '\t') {
554 while (*start == '\t')
555 start++;
556 i = tabs(s_start, start) - margin / 8;
557 printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
558 continue;
561 if (!nokeyw && !force)
562 if ((*start == '#' || isidchr(*start))
563 && (start == s_start || !isidchr(start[-1]))) {
564 i = iskw(start);
565 if (i > 0) {
566 ps("\\*(+K");
568 putcp((unsigned char)*start++);
569 while (--i > 0);
570 ps("\\*(-K");
571 continue;
575 putcp ((unsigned char)*start++);
580 static int
581 tabs(char *s, char *os)
584 return (width(s, os) / 8);
587 static int
588 width(char *s, char *os)
590 int i = 0;
592 while (s < os) {
593 if (*s == '\t') {
594 i = (i + 8) &~ 7;
595 s++;
596 continue;
598 if (*s < ' ')
599 i += 2;
600 else
601 i++;
602 s++;
604 return (i);
607 static void
608 putcp(int c)
611 switch(c) {
613 case 0:
614 break;
616 case '\f':
617 break;
619 case '\r':
620 break;
622 case '{':
623 ps("\\*(+K{\\*(-K");
624 break;
626 case '}':
627 ps("\\*(+K}\\*(-K");
628 break;
630 case '\\':
631 ps("\\e");
632 break;
634 case '_':
635 ps("\\*_");
636 break;
638 case '-':
639 ps("\\*-");
640 break;
642 case '`':
643 ps("\\`");
644 break;
646 case '\'':
647 ps("\\'");
648 break;
650 case '.':
651 ps("\\&.");
652 break;
654 case '*':
655 ps("\\fI*\\fP");
656 break;
658 case '/':
659 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
660 break;
662 default:
663 if (c < 040)
664 putchar('^'), c |= '@';
665 case '\t':
666 case '\n':
667 putchar(c);
672 * look for a process beginning on this line
674 static boolean
675 isproc(char *s)
677 pname[0] = '\0';
678 if (!l_toplex || blklevel == 0)
679 if (expmatch (s, l_prcbeg, pname) != NIL) {
680 return (TRUE);
682 return (FALSE);
686 /* iskw - check to see if the next word is a keyword
689 static int
690 iskw(char *s)
692 char **ss = l_keywds;
693 int i = 1;
694 char *cp = s;
696 while (++cp, isidchr(*cp))
697 i++;
698 while ((cp = *ss++) != NULL)
699 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))
700 return (i);
701 return (0);