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