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