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