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