HAMMER 2.0:02 - rmdir, stability
[dragonfly/netmp.git] / usr.bin / vgrind / vfontedpr.c
blobe0fe928891e4401a2a0193d684b7742ce5234c6f
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)vfontedpr.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/vgrind/vfontedpr.c,v 1.12 1999/08/28 01:07:21 peter Exp $
36 * $DragonFly: src/usr.bin/vgrind/vfontedpr.c,v 1.3 2003/10/04 20:36:54 hmp Exp $
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <ctype.h>
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <time.h>
47 #include "pathnames.h"
48 #include "extern.h"
50 #define FALSE 0
51 #define TRUE !(FALSE)
52 #define NIL 0
53 #define STANDARD 0
54 #define ALTERNATE 1
57 * Vfontedpr.
59 * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
63 #define STRLEN 10 /* length of strings introducing things */
64 #define PNAMELEN 40 /* length of a function/procedure name */
65 #define PSMAX 20 /* size of procedure name stacking */
67 static int iskw(char *);
68 static boolean isproc(char *);
69 static void putKcp(char *, char *, boolean);
70 static void putScp(char *);
71 static void putcp(int);
72 static int tabs(char *, char *);
73 static int width(char *, char *);
76 * The state variables
79 static boolean filter = FALSE; /* act as a filter (like eqn) */
80 static boolean inchr; /* in a string constant */
81 static boolean incomm; /* in a comment of the primary type */
82 static boolean idx = FALSE; /* form an index */
83 static boolean instr; /* in a string constant */
84 static boolean nokeyw = FALSE; /* no keywords being flagged */
85 static boolean pass = FALSE; /*
86 * when acting as a filter, pass indicates
87 * whether we are currently processing
88 * input.
91 static int blklevel; /* current nesting level */
92 static int comtype; /* type of comment */
93 static char *defsfile[2] = { _PATH_VGRINDEFS, 0 };
94 /* name of language definitions file */
95 static int margin;
96 static int plstack[PSMAX]; /* the procedure nesting level stack */
97 static char pname[BUFSIZ+1];
98 static boolean prccont; /* continue last procedure */
99 static int psptr; /* the stack index of the current procedure */
100 static char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
103 * The language specific globals
106 char *l_acmbeg; /* string introducing a comment */
107 char *l_acmend; /* string ending a comment */
108 char *l_blkbeg; /* string begining of a block */
109 char *l_blkend; /* string ending a block */
110 char *l_chrbeg; /* delimiter for character constant */
111 char *l_chrend; /* delimiter for character constant */
112 char *l_combeg; /* string introducing a comment */
113 char *l_comend; /* string ending a comment */
114 char l_escape; /* character used to escape characters */
115 char *l_keywds[BUFSIZ/2]; /* keyword table address */
116 char *l_nocom; /* regexp for non-comments */
117 char *l_prcbeg; /* regular expr for procedure begin */
118 char *l_strbeg; /* delimiter for string constant */
119 char *l_strend; /* delimiter for string constant */
120 boolean l_toplex; /* procedures only defined at top lex level */
121 char *language = "c"; /* the language indicator */
123 #define ps(x) printf("%s", x)
126 main(int argc, char **argv)
128 char *fname = "";
129 struct stat stbuf;
130 char buf[BUFSIZ];
131 char *defs;
132 int needbp = 0;
134 argc--, argv++;
135 do {
136 char *cp;
137 int i;
139 if (argc > 0) {
140 if (!strcmp(argv[0], "-h")) {
141 if (argc == 1) {
142 printf("'ds =H\n");
143 argc = 0;
144 goto rest;
146 printf("'ds =H %s\n", argv[1]);
147 argc--, argv++;
148 argc--, argv++;
149 if (argc > 0)
150 continue;
151 goto rest;
154 /* act as a filter like eqn */
155 if (!strcmp(argv[0], "-f")) {
156 filter++;
157 argv[0] = argv[argc-1];
158 argv[argc-1] = "-";
159 continue;
162 /* take input from the standard place */
163 if (!strcmp(argv[0], "-")) {
164 argc = 0;
165 goto rest;
168 /* build an index */
169 if (!strcmp(argv[0], "-x")) {
170 idx++;
171 argv[0] = "-n";
174 /* indicate no keywords */
175 if (!strcmp(argv[0], "-n")) {
176 nokeyw++;
177 argc--, argv++;
178 continue;
181 /* specify the font size */
182 if (!strncmp(argv[0], "-s", 2)) {
183 i = 0;
184 cp = argv[0] + 2;
185 while (*cp)
186 i = i * 10 + (*cp++ - '0');
187 printf("'ps %d\n'vs %d\n", i, i+1);
188 argc--, argv++;
189 continue;
192 /* specify the language */
193 if (!strncmp(argv[0], "-l", 2)) {
194 language = argv[0]+2;
195 argc--, argv++;
196 continue;
199 /* specify the language description file */
200 if (!strncmp(argv[0], "-d", 2)) {
201 defsfile[0] = argv[1];
202 argc--, argv++;
203 argc--, argv++;
204 continue;
207 /* open the file for input */
208 if (freopen(argv[0], "r", stdin) == NULL)
209 err(1, "%s", argv[0]);
210 if (idx)
211 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
212 fname = argv[0];
213 argc--, argv++;
215 rest:
218 * get the language definition from the defs file
220 i = cgetent(&defs, defsfile, language);
221 if (i == -1) {
222 fprintf (stderr, "no entry for language %s\n", language);
223 exit (0);
224 } else if (i == -2) { fprintf(stderr,
225 "cannot find vgrindefs file %s\n", defsfile[0]);
226 exit (0);
227 } else if (i == -3) { fprintf(stderr,
228 "potential reference loop detected in vgrindefs file %s\n",
229 defsfile[0]);
230 exit(0);
232 if (cgetustr(defs, "kw", &cp) == -1)
233 nokeyw = TRUE;
234 else {
235 char **cpp;
237 cpp = l_keywds;
238 while (*cp) {
239 while (*cp == ' ' || *cp =='\t')
240 *cp++ = '\0';
241 if (*cp)
242 *cpp++ = cp;
243 while (*cp != ' ' && *cp != '\t' && *cp)
244 cp++;
246 *cpp = NIL;
248 cgetustr(defs, "pb", &cp);
249 l_prcbeg = convexp(cp);
250 cgetustr(defs, "cb", &cp);
251 l_combeg = convexp(cp);
252 cgetustr(defs, "ce", &cp);
253 l_comend = convexp(cp);
254 cgetustr(defs, "ab", &cp);
255 l_acmbeg = convexp(cp);
256 cgetustr(defs, "ae", &cp);
257 l_acmend = convexp(cp);
258 cgetustr(defs, "sb", &cp);
259 l_strbeg = convexp(cp);
260 cgetustr(defs, "se", &cp);
261 l_strend = convexp(cp);
262 cgetustr(defs, "bb", &cp);
263 l_blkbeg = convexp(cp);
264 cgetustr(defs, "be", &cp);
265 l_blkend = convexp(cp);
266 cgetustr(defs, "lb", &cp);
267 l_chrbeg = convexp(cp);
268 cgetustr(defs, "le", &cp);
269 l_chrend = convexp(cp);
270 if (cgetustr(defs, "nc", &cp) >= 0)
271 l_nocom = convexp(cp);
272 l_escape = '\\';
273 l_onecase = (cgetcap(defs, "oc", ':') != NULL);
274 l_toplex = (cgetcap(defs, "tl", ':') != NULL);
276 /* initialize the program */
278 incomm = FALSE;
279 instr = FALSE;
280 inchr = FALSE;
281 _escaped = FALSE;
282 blklevel = 0;
283 for (psptr=0; psptr<PSMAX; psptr++) {
284 pstack[psptr][0] = '\0';
285 plstack[psptr] = 0;
287 psptr = -1;
288 ps("'-F\n");
289 if (!filter) {
290 printf(".ds =F %s\n", fname);
291 ps("'wh 0 vH\n");
292 ps("'wh -1i vF\n");
294 if (needbp) {
295 needbp = 0;
296 printf(".()\n");
297 printf(".bp\n");
299 if (!filter) {
300 fstat(fileno(stdin), &stbuf);
301 cp = ctime(&stbuf.st_mtime);
302 cp[16] = '\0';
303 cp[24] = '\0';
304 printf(".ds =M %s %s\n", cp+4, cp+20);
308 * MAIN LOOP!!!
310 while (fgets(buf, sizeof buf, stdin) != NULL) {
311 if (buf[0] == '\f') {
312 printf(".bp\n");
314 if (buf[0] == '.') {
315 printf("%s", buf);
316 if (!strncmp (buf+1, "vS", 2))
317 pass = TRUE;
318 if (!strncmp (buf+1, "vE", 2))
319 pass = FALSE;
320 continue;
322 prccont = FALSE;
323 if (!filter || pass)
324 putScp(buf);
325 else
326 printf("%s", buf);
327 if (prccont && (psptr >= 0)) {
328 ps("'FC ");
329 ps(pstack[psptr]);
330 ps("\n");
332 #ifdef DEBUG
333 printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
334 #endif
335 margin = 0;
337 needbp = 1;
338 } while (argc > 0);
339 exit(0);
342 #define isidchr(c) (isalnum(c) || (c) == '_')
344 static void
345 putScp(char *os)
347 register char *s = os; /* pointer to unmatched string */
348 char dummy[BUFSIZ]; /* dummy to be used by expmatch */
349 char *comptr; /* end of a comment delimiter */
350 char *acmptr; /* end of a comment delimiter */
351 char *strptr; /* end of a string delimiter */
352 char *chrptr; /* end of a character const delimiter */
353 char *blksptr; /* end of a lexical block start */
354 char *blkeptr; /* end of a lexical block end */
355 char *nocomptr; /* end of a non-comment delimiter */
357 s_start = os; /* remember the start for expmatch */
358 _escaped = FALSE;
359 if (nokeyw || incomm || instr)
360 goto skip;
361 if (isproc(s)) {
362 ps("'FN ");
363 ps(pname);
364 ps("\n");
365 if (psptr < PSMAX) {
366 ++psptr;
367 strncpy (pstack[psptr], pname, PNAMELEN);
368 pstack[psptr][PNAMELEN] = '\0';
369 plstack[psptr] = blklevel;
372 skip:
373 do {
374 /* check for string, comment, blockstart, etc */
375 if (!incomm && !instr && !inchr) {
377 blkeptr = expmatch (s, l_blkend, dummy);
378 blksptr = expmatch (s, l_blkbeg, dummy);
379 comptr = expmatch (s, l_combeg, dummy);
380 acmptr = expmatch (s, l_acmbeg, dummy);
381 strptr = expmatch (s, l_strbeg, dummy);
382 chrptr = expmatch (s, l_chrbeg, dummy);
383 nocomptr = expmatch (s, l_nocom, dummy);
385 /* start of non-comment? */
386 if (nocomptr != NIL)
387 if ((nocomptr <= comptr || comptr == NIL)
388 && (nocomptr <= acmptr || acmptr == NIL)) {
389 /* continue after non-comment */
390 putKcp (s, nocomptr-1, FALSE);
391 s = nocomptr;
392 continue;
395 /* start of a comment? */
396 if (comptr != NIL)
397 if ((comptr < strptr || strptr == NIL)
398 && (comptr < acmptr || acmptr == NIL)
399 && (comptr < chrptr || chrptr == NIL)
400 && (comptr < blksptr || blksptr == NIL)
401 && (comptr < blkeptr || blkeptr == NIL)) {
402 putKcp (s, comptr-1, FALSE);
403 s = comptr;
404 incomm = TRUE;
405 comtype = STANDARD;
406 if (s != os)
407 ps ("\\c");
408 ps ("\\c\n'+C\n");
409 continue;
412 /* start of a comment? */
413 if (acmptr != NIL)
414 if ((acmptr < strptr || strptr == NIL)
415 && (acmptr < chrptr || chrptr == NIL)
416 && (acmptr < blksptr || blksptr == NIL)
417 && (acmptr < blkeptr || blkeptr == NIL)) {
418 putKcp (s, acmptr-1, FALSE);
419 s = acmptr;
420 incomm = TRUE;
421 comtype = ALTERNATE;
422 if (s != os)
423 ps ("\\c");
424 ps ("\\c\n'+C\n");
425 continue;
428 /* start of a string? */
429 if (strptr != NIL)
430 if ((strptr < chrptr || chrptr == NIL)
431 && (strptr < blksptr || blksptr == NIL)
432 && (strptr < blkeptr || blkeptr == NIL)) {
433 putKcp (s, strptr-1, FALSE);
434 s = strptr;
435 instr = TRUE;
436 continue;
439 /* start of a character string? */
440 if (chrptr != NIL)
441 if ((chrptr < blksptr || blksptr == NIL)
442 && (chrptr < blkeptr || blkeptr == NIL)) {
443 putKcp (s, chrptr-1, FALSE);
444 s = chrptr;
445 inchr = TRUE;
446 continue;
449 /* end of a lexical block */
450 if (blkeptr != NIL) {
451 if (blkeptr < blksptr || blksptr == NIL) {
452 putKcp (s, blkeptr - 1, FALSE);
453 s = blkeptr;
454 if (blklevel > 0 /* sanity */)
455 blklevel--;
456 if (psptr >= 0 && plstack[psptr] >= blklevel) {
458 /* end of current procedure */
459 if (s != os)
460 ps ("\\c");
461 ps ("\\c\n'-F\n");
462 blklevel = plstack[psptr];
464 /* see if we should print the last proc name */
465 if (--psptr >= 0)
466 prccont = TRUE;
467 else
468 psptr = -1;
470 continue;
474 /* start of a lexical block */
475 if (blksptr != NIL) {
476 putKcp (s, blksptr - 1, FALSE);
477 s = blksptr;
478 blklevel++;
479 continue;
482 /* check for end of comment */
483 } else if (incomm) {
484 comptr = expmatch (s, l_comend, dummy);
485 acmptr = expmatch (s, l_acmend, dummy);
486 if (((comtype == STANDARD) && (comptr != NIL)) ||
487 ((comtype == ALTERNATE) && (acmptr != NIL))) {
488 if (comtype == STANDARD) {
489 putKcp (s, comptr-1, TRUE);
490 s = comptr;
491 } else {
492 putKcp (s, acmptr-1, TRUE);
493 s = acmptr;
495 incomm = FALSE;
496 ps("\\c\n'-C\n");
497 continue;
498 } else {
499 putKcp (s, s + strlen(s) -1, TRUE);
500 s = s + strlen(s);
501 continue;
504 /* check for end of string */
505 } else if (instr) {
506 if ((strptr = expmatch (s, l_strend, dummy)) != NIL) {
507 putKcp (s, strptr-1, TRUE);
508 s = strptr;
509 instr = FALSE;
510 continue;
511 } else {
512 putKcp (s, s+strlen(s)-1, TRUE);
513 s = s + strlen(s);
514 continue;
517 /* check for end of character string */
518 } else if (inchr) {
519 if ((chrptr = expmatch (s, l_chrend, dummy)) != NIL) {
520 putKcp (s, chrptr-1, TRUE);
521 s = chrptr;
522 inchr = FALSE;
523 continue;
524 } else {
525 putKcp (s, s+strlen(s)-1, TRUE);
526 s = s + strlen(s);
527 continue;
531 /* print out the line */
532 putKcp (s, s + strlen(s) -1, FALSE);
533 s = s + strlen(s);
534 } while (*s);
537 /* force: true if we should force nokeyw */
538 static void
539 putKcp(char *start, char *end, boolean force)
541 int i;
542 int xfld = 0;
544 while (start <= end) {
545 if (idx) {
546 if (*start == ' ' || *start == '\t') {
547 if (xfld == 0)
548 printf("\001");
549 printf("\t");
550 xfld = 1;
551 while (*start == ' ' || *start == '\t')
552 start++;
553 continue;
557 /* take care of nice tab stops */
558 if (*start == '\t') {
559 while (*start == '\t')
560 start++;
561 i = tabs(s_start, start) - margin / 8;
562 printf("\\h'|%dn'", i * 10 + 1 - margin % 8);
563 continue;
566 if (!nokeyw && !force)
567 if ((*start == '#' || isidchr(*start))
568 && (start == s_start || !isidchr(start[-1]))) {
569 i = iskw(start);
570 if (i > 0) {
571 ps("\\*(+K");
573 putcp((unsigned char)*start++);
574 while (--i > 0);
575 ps("\\*(-K");
576 continue;
580 putcp ((unsigned char)*start++);
585 static int
586 tabs(char *s, char *os)
589 return (width(s, os) / 8);
592 static int
593 width(register char *s, register char *os)
595 register int i = 0;
597 while (s < os) {
598 if (*s == '\t') {
599 i = (i + 8) &~ 7;
600 s++;
601 continue;
603 if (*s < ' ')
604 i += 2;
605 else
606 i++;
607 s++;
609 return (i);
612 static void
613 putcp(register int c)
616 switch(c) {
618 case 0:
619 break;
621 case '\f':
622 break;
624 case '\r':
625 break;
627 case '{':
628 ps("\\*(+K{\\*(-K");
629 break;
631 case '}':
632 ps("\\*(+K}\\*(-K");
633 break;
635 case '\\':
636 ps("\\e");
637 break;
639 case '_':
640 ps("\\*_");
641 break;
643 case '-':
644 ps("\\*-");
645 break;
647 case '`':
648 ps("\\`");
649 break;
651 case '\'':
652 ps("\\'");
653 break;
655 case '.':
656 ps("\\&.");
657 break;
659 case '*':
660 ps("\\fI*\\fP");
661 break;
663 case '/':
664 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
665 break;
667 default:
668 if (c < 040)
669 putchar('^'), c |= '@';
670 case '\t':
671 case '\n':
672 putchar(c);
677 * look for a process beginning on this line
679 static boolean
680 isproc(char *s)
682 pname[0] = '\0';
683 if (!l_toplex || blklevel == 0)
684 if (expmatch (s, l_prcbeg, pname) != NIL) {
685 return (TRUE);
687 return (FALSE);
691 /* iskw - check to see if the next word is a keyword
694 static int
695 iskw(register char *s)
697 register char **ss = l_keywds;
698 register int i = 1;
699 register char *cp = s;
701 while (++cp, isidchr(*cp))
702 i++;
703 while (cp = *ss++)
704 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))
705 return (i);
706 return (0);