Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * 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.
20  *
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.
32  *
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.2 2003/06/17 04:29:33 dillon Exp $
37  */
38
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"
49
50 #define FALSE 0
51 #define TRUE !(FALSE)
52 #define NIL 0
53 #define STANDARD 0
54 #define ALTERNATE 1
55
56 /*
57  * Vfontedpr.
58  *
59  * Dave Presotto 1/12/81 (adapted from an earlier version by Bill Joy)
60  *
61  */
62
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 */
66
67 static int       iskw __P((char *));
68 static boolean   isproc __P((char *));
69 static void      putKcp __P((char *, char *, boolean));
70 static void      putScp __P((char *));
71 static void      putcp __P((int));
72 static int       tabs __P((char *, char *));
73 static int       width __P((char *, char *));
74
75 /*
76  *      The state variables
77  */
78
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.
89                                  */
90
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 */
101
102 /*
103  *      The language specific globals
104  */
105
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 */
122
123 #define ps(x)   printf("%s", x)
124
125 int
126 main(argc, argv)
127     int argc;
128     char *argv[];
129 {
130     char *fname = "";
131     struct stat stbuf;
132     char buf[BUFSIZ];
133     char *defs;
134     int needbp = 0;
135
136     argc--, argv++;
137     do {
138         char *cp;
139         int i;
140
141         if (argc > 0) {
142             if (!strcmp(argv[0], "-h")) {
143                 if (argc == 1) {
144                     printf("'ds =H\n");
145                     argc = 0;
146                     goto rest;
147                 }
148                 printf("'ds =H %s\n", argv[1]);
149                 argc--, argv++;
150                 argc--, argv++;
151                 if (argc > 0)
152                     continue;
153                 goto rest;
154             }
155
156             /* act as a filter like eqn */
157             if (!strcmp(argv[0], "-f")) {
158                 filter++;
159                 argv[0] = argv[argc-1];
160                 argv[argc-1] = "-";
161                 continue;
162             }
163
164             /* take input from the standard place */
165             if (!strcmp(argv[0], "-")) {
166                 argc = 0;
167                 goto rest;
168             }
169
170             /* build an index */
171             if (!strcmp(argv[0], "-x")) {
172                 idx++;
173                 argv[0] = "-n";
174             }
175
176             /* indicate no keywords */
177             if (!strcmp(argv[0], "-n")) {
178                 nokeyw++;
179                 argc--, argv++;
180                 continue;
181             }
182
183             /* specify the font size */
184             if (!strncmp(argv[0], "-s", 2)) {
185                 i = 0;
186                 cp = argv[0] + 2;
187                 while (*cp)
188                     i = i * 10 + (*cp++ - '0');
189                 printf("'ps %d\n'vs %d\n", i, i+1);
190                 argc--, argv++;
191                 continue;
192             }
193
194             /* specify the language */
195             if (!strncmp(argv[0], "-l", 2)) {
196                 language = argv[0]+2;
197                 argc--, argv++;
198                 continue;
199             }
200
201             /* specify the language description file */
202             if (!strncmp(argv[0], "-d", 2)) {
203                 defsfile[0] = argv[1];
204                 argc--, argv++;
205                 argc--, argv++;
206                 continue;
207             }
208
209             /* open the file for input */
210             if (freopen(argv[0], "r", stdin) == NULL)
211                 err(1, "%s", argv[0]);
212             if (idx)
213                 printf("'ta 4i 4.25i 5.5iR\n'in .5i\n");
214             fname = argv[0];
215             argc--, argv++;
216         }
217     rest:
218
219         /*
220          *  get the  language definition from the defs file
221          */
222         i = cgetent(&defs, defsfile, language);
223         if (i == -1) {
224             fprintf (stderr, "no entry for language %s\n", language);
225             exit (0);
226         } else  if (i == -2) { fprintf(stderr,
227             "cannot find vgrindefs file %s\n", defsfile[0]);
228             exit (0);
229         } else if (i == -3) { fprintf(stderr,
230             "potential reference loop detected in vgrindefs file %s\n",
231             defsfile[0]);
232             exit(0);
233         }
234         if (cgetustr(defs, "kw", &cp) == -1)
235             nokeyw = TRUE;
236         else  {
237             char **cpp;
238
239             cpp = l_keywds;
240             while (*cp) {
241                 while (*cp == ' ' || *cp =='\t')
242                     *cp++ = '\0';
243                 if (*cp)
244                     *cpp++ = cp;
245                 while (*cp != ' ' && *cp  != '\t' && *cp)
246                     cp++;
247             }
248             *cpp = NIL;
249         }
250         cgetustr(defs, "pb", &cp);
251         l_prcbeg = convexp(cp);
252         cgetustr(defs, "cb", &cp);
253         l_combeg = convexp(cp);
254         cgetustr(defs, "ce", &cp);
255         l_comend = convexp(cp);
256         cgetustr(defs, "ab", &cp);
257         l_acmbeg = convexp(cp);
258         cgetustr(defs, "ae", &cp);
259         l_acmend = convexp(cp);
260         cgetustr(defs, "sb", &cp);
261         l_strbeg = convexp(cp);
262         cgetustr(defs, "se", &cp);
263         l_strend = convexp(cp);
264         cgetustr(defs, "bb", &cp);
265         l_blkbeg = convexp(cp);
266         cgetustr(defs, "be", &cp);
267         l_blkend = convexp(cp);
268         cgetustr(defs, "lb", &cp);
269         l_chrbeg = convexp(cp);
270         cgetustr(defs, "le", &cp);
271         l_chrend = convexp(cp);
272         if (cgetustr(defs, "nc", &cp) >= 0)
273                 l_nocom = convexp(cp);
274         l_escape = '\\';
275         l_onecase = (cgetcap(defs, "oc", ':') != NULL);
276         l_toplex = (cgetcap(defs, "tl", ':') != NULL);
277
278         /* initialize the program */
279
280         incomm = FALSE;
281         instr = FALSE;
282         inchr = FALSE;
283         _escaped = FALSE;
284         blklevel = 0;
285         for (psptr=0; psptr<PSMAX; psptr++) {
286             pstack[psptr][0] = '\0';
287             plstack[psptr] = 0;
288         }
289         psptr = -1;
290         ps("'-F\n");
291         if (!filter) {
292             printf(".ds =F %s\n", fname);
293             ps("'wh 0 vH\n");
294             ps("'wh -1i vF\n");
295         }
296         if (needbp) {
297             needbp = 0;
298             printf(".()\n");
299             printf(".bp\n");
300         }
301         if (!filter) {
302             fstat(fileno(stdin), &stbuf);
303             cp = ctime(&stbuf.st_mtime);
304             cp[16] = '\0';
305             cp[24] = '\0';
306             printf(".ds =M %s %s\n", cp+4, cp+20);
307         }
308
309         /*
310          *      MAIN LOOP!!!
311          */
312         while (fgets(buf, sizeof buf, stdin) != NULL) {
313             if (buf[0] == '\f') {
314                 printf(".bp\n");
315             }
316             if (buf[0] == '.') {
317                 printf("%s", buf);
318                 if (!strncmp (buf+1, "vS", 2))
319                     pass = TRUE;
320                 if (!strncmp (buf+1, "vE", 2))
321                     pass = FALSE;
322                 continue;
323             }
324             prccont = FALSE;
325             if (!filter || pass)
326                 putScp(buf);
327             else
328                 printf("%s", buf);
329             if (prccont && (psptr >= 0)) {
330                 ps("'FC ");
331                 ps(pstack[psptr]);
332                 ps("\n");
333             }
334 #ifdef DEBUG
335             printf ("com %o str %o chr %o ptr %d\n", incomm, instr, inchr, psptr);
336 #endif
337             margin = 0;
338         }
339         needbp = 1;
340     } while (argc > 0);
341     exit(0);
342 }
343
344 #define isidchr(c) (isalnum(c) || (c) == '_')
345
346 static void
347 putScp(os)
348     char *os;
349 {
350     register char *s = os;              /* pointer to unmatched string */
351     char dummy[BUFSIZ];                 /* dummy to be used by expmatch */
352     char *comptr;                       /* end of a comment delimiter */
353     char *acmptr;                       /* end of a comment delimiter */
354     char *strptr;                       /* end of a string delimiter */
355     char *chrptr;                       /* end of a character const delimiter */
356     char *blksptr;                      /* end of a lexical block start */
357     char *blkeptr;                      /* end of a lexical block end */
358     char *nocomptr;                     /* end of a non-comment delimiter */
359
360     s_start = os;                       /* remember the start for expmatch */
361     _escaped = FALSE;
362     if (nokeyw || incomm || instr)
363         goto skip;
364     if (isproc(s)) {
365         ps("'FN ");
366         ps(pname);
367         ps("\n");
368         if (psptr < PSMAX) {
369             ++psptr;
370             strncpy (pstack[psptr], pname, PNAMELEN);
371             pstack[psptr][PNAMELEN] = '\0';
372             plstack[psptr] = blklevel;
373         }
374     }
375 skip:
376     do {
377         /* check for string, comment, blockstart, etc */
378         if (!incomm && !instr && !inchr) {
379
380             blkeptr = expmatch (s, l_blkend, dummy);
381             blksptr = expmatch (s, l_blkbeg, dummy);
382             comptr = expmatch (s, l_combeg, dummy);
383             acmptr = expmatch (s, l_acmbeg, dummy);
384             strptr = expmatch (s, l_strbeg, dummy);
385             chrptr = expmatch (s, l_chrbeg, dummy);
386             nocomptr = expmatch (s, l_nocom, dummy);
387
388             /* start of non-comment? */
389             if (nocomptr != NIL)
390                 if ((nocomptr <= comptr || comptr == NIL)
391                   && (nocomptr <= acmptr || acmptr == NIL)) {
392                     /* continue after non-comment */
393                     putKcp (s, nocomptr-1, FALSE);
394                     s = nocomptr;
395                     continue;
396                 }
397
398             /* start of a comment? */
399             if (comptr != NIL)
400                 if ((comptr < strptr || strptr == NIL)
401                   && (comptr < acmptr || acmptr == NIL)
402                   && (comptr < chrptr || chrptr == NIL)
403                   && (comptr < blksptr || blksptr == NIL)
404                   && (comptr < blkeptr || blkeptr == NIL)) {
405                     putKcp (s, comptr-1, FALSE);
406                     s = comptr;
407                     incomm = TRUE;
408                     comtype = STANDARD;
409                     if (s != os)
410                         ps ("\\c");
411                     ps ("\\c\n'+C\n");
412                     continue;
413                 }
414
415             /* start of a comment? */
416             if (acmptr != NIL)
417                 if ((acmptr < strptr || strptr == NIL)
418                   && (acmptr < chrptr || chrptr == NIL)
419                   && (acmptr < blksptr || blksptr == NIL)
420                   && (acmptr < blkeptr || blkeptr == NIL)) {
421                     putKcp (s, acmptr-1, FALSE);
422                     s = acmptr;
423                     incomm = TRUE;
424                     comtype = ALTERNATE;
425                     if (s != os)
426                         ps ("\\c");
427                     ps ("\\c\n'+C\n");
428                     continue;
429                 }
430
431             /* start of a string? */
432             if (strptr != NIL)
433                 if ((strptr < chrptr || chrptr == NIL)
434                   && (strptr < blksptr || blksptr == NIL)
435                   && (strptr < blkeptr || blkeptr == NIL)) {
436                     putKcp (s, strptr-1, FALSE);
437                     s = strptr;
438                     instr = TRUE;
439                     continue;
440                 }
441
442             /* start of a character string? */
443             if (chrptr != NIL)
444                 if ((chrptr < blksptr || blksptr == NIL)
445                   && (chrptr < blkeptr || blkeptr == NIL)) {
446                     putKcp (s, chrptr-1, FALSE);
447                     s = chrptr;
448                     inchr = TRUE;
449                     continue;
450                 }
451
452             /* end of a lexical block */
453             if (blkeptr != NIL) {
454                 if (blkeptr < blksptr || blksptr == NIL) {
455                     putKcp (s, blkeptr - 1, FALSE);
456                     s = blkeptr;
457                     if (blklevel > 0 /* sanity */)
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("\001");
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(s_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 == s_start || !isidchr(start[-1]))) {
574                 i = iskw(start);
575                 if (i > 0) {
576                     ps("\\*(+K");
577                     do
578                         putcp((unsigned char)*start++);
579                     while (--i > 0);
580                     ps("\\*(-K");
581                     continue;
582                 }
583             }
584
585         putcp ((unsigned char)*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         register char *s, *os;
601 {
602         register 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         register int c;
622 {
623
624         switch(c) {
625
626         case 0:
627                 break;
628
629         case '\f':
630                 break;
631
632         case '\r':
633                 break;
634
635         case '{':
636                 ps("\\*(+K{\\*(-K");
637                 break;
638
639         case '}':
640                 ps("\\*(+K}\\*(-K");
641                 break;
642
643         case '\\':
644                 ps("\\e");
645                 break;
646
647         case '_':
648                 ps("\\*_");
649                 break;
650
651         case '-':
652                 ps("\\*-");
653                 break;
654
655         case '`':
656                 ps("\\`");
657                 break;
658
659         case '\'':
660                 ps("\\'");
661                 break;
662
663         case '.':
664                 ps("\\&.");
665                 break;
666
667         case '*':
668                 ps("\\fI*\\fP");
669                 break;
670
671         case '/':
672                 ps("\\fI\\h'\\w' 'u-\\w'/'u'/\\fP");
673                 break;
674
675         default:
676                 if (c < 040)
677                         putchar('^'), c |= '@';
678         case '\t':
679         case '\n':
680                 putchar(c);
681         }
682 }
683
684 /*
685  *      look for a process beginning on this line
686  */
687 static boolean
688 isproc(s)
689     char *s;
690 {
691     pname[0] = '\0';
692     if (!l_toplex || blklevel == 0)
693         if (expmatch (s, l_prcbeg, pname) != NIL) {
694             return (TRUE);
695         }
696     return (FALSE);
697 }
698
699
700 /*  iskw -      check to see if the next word is a keyword
701  */
702
703 static int
704 iskw(s)
705         register char *s;
706 {
707         register char **ss = l_keywds;
708         register int i = 1;
709         register char *cp = s;
710
711         while (++cp, isidchr(*cp))
712                 i++;
713         while (cp = *ss++)
714                 if (!STRNCMP(s,cp,i) && !isidchr(cp[i]))
715                         return (i);
716         return (0);
717 }
718