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