Back to index

tetex-bin  3.0
squeeze.c
Go to the documentation of this file.
00001 /*
00002  *   This software is Copyright 1988 by Radical Eye Software.
00003  */
00004 /*
00005  *   This routine squeezes a PostScript file down to its
00006  *   minimum.  We parse and then output it.
00007  */
00008 
00009 #ifdef KPATHSEA
00010 #include <kpathsea/config.h>
00011 #include <kpathsea/c-fopen.h>
00012 #include <c-auto.h>
00013 #else
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include "dvips.h"
00018 #endif
00019 #ifdef fopen
00020 #undef fopen
00021 #endif
00022 
00023 #define LINELENGTH (72)
00024 #define BUFLENGTH (1000)
00025 #ifdef putchar
00026 #undef putchar
00027 #endif
00028 #define putchar(a) (void)putc(a, out) ;
00029 FILE *in, *out ;
00030 static int linepos = 0 ;
00031 static int lastspecial = 1 ;
00032 /*
00033  *   This next routine writes out a `special' character.  In this case,
00034  *   we simply put it out, since any special character terminates the
00035  *   preceding token.
00036  */
00037 void specialout P1C(char, c)
00038 {
00039    if (linepos + 1 > LINELENGTH) {
00040       putchar('\n') ;
00041       linepos = 0 ;
00042    }
00043    putchar(c) ;
00044    linepos++ ;
00045    lastspecial = 1 ;
00046 }
00047 void strout P1C(char *, s)
00048 {
00049    if (linepos + strlen(s) > LINELENGTH) {
00050       putchar('\n') ;
00051       linepos = 0 ;
00052    }
00053    linepos += strlen(s) ;
00054    while (*s != 0)
00055       putchar(*s++) ;
00056    lastspecial = 1 ;
00057 }
00058 void cmdout P1C(char *, s)
00059 {
00060    int l ;
00061 
00062    l = strlen(s) ;
00063    if (linepos + l + 1 > LINELENGTH) {
00064       putchar('\n') ;
00065       linepos = 0 ;
00066       lastspecial = 1 ;
00067    }
00068    if (! lastspecial && *s != '/') {
00069       putchar(' ') ;
00070       linepos++ ;
00071    }
00072    while (*s != 0) {
00073       putchar(*s++) ;
00074    }
00075    linepos += l ;
00076    lastspecial = 0 ;
00077 }
00078 char buf[BUFLENGTH] ;
00079 int main P2C(int, argc, char **, argv)
00080 {
00081    int c ;
00082    char *b ;
00083    char seeking ;
00084 
00085    if (argc > 3 || (in=(argc < 2 ? stdin : fopen(argv[1], "r")))==NULL ||
00086                     (out=(argc < 3 ? stdout : fopen(argv[2], "w")))==NULL) {
00087       (void)fprintf(stderr, "Usage:  squeeze [infile [outfile]]\n") ;
00088       exit(1) ;
00089    }
00090    /* Binary output is safer (for those systems which care to know the
00091       difference) since PostScript can include non-printable characters.  */
00092    if (O_BINARY && !isatty(fileno(in)))
00093       SET_BINARY(fileno(in)) ;
00094    if (O_BINARY && !isatty(fileno(out)))
00095       SET_BINARY(fileno(out)) ;
00096 
00097    (void)fprintf(out, "%%!\n") ;
00098    while (1) {
00099       c = getc(in) ;
00100       if (c==EOF)
00101          break ;
00102       if (c=='%') {
00103          while ((c=getc(in))!='\n') ;
00104       }
00105       if (c <= ' ')
00106          continue ;
00107       switch (c) {
00108 case '{' :
00109 case '}' :
00110 case '[' :
00111 case ']' :
00112          specialout(c) ;
00113          break ;
00114 case '<' :
00115 case '(' :
00116          if (c=='(')
00117             seeking = ')' ;
00118          else
00119             seeking = '>' ;
00120          b = buf ;
00121          *b++ = c ;
00122          do {
00123             c = getc(in) ;
00124             if (b > buf + BUFLENGTH-2) {
00125                (void)fprintf(stderr, "Overran buffer seeking %c", seeking) ;
00126                exit(1) ;
00127             }
00128             *b++ = c ;
00129             if (c=='\\')
00130                *b++ = getc(in) ;
00131          } while (c != seeking) ;
00132          *b++ = 0 ;
00133          strout(buf) ;
00134          break ;
00135 default:
00136          b = buf ;
00137          while ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||
00138                 (c>='0'&&c<='9')||(c=='/')||(c=='@')||
00139                 (c=='!')||(c=='"')||(c=='&')||(c=='*')||(c==':')||
00140                 (c==',')||(c==';')||(c=='?')||(c=='^')||(c=='~')||
00141                 (c=='-')||(c=='.')||(c=='#')||(c=='|')||(c=='_')||
00142                 (c=='=')||(c=='$')||(c=='+')) {
00143             *b++ = c ;
00144             c = getc(in) ;
00145          }
00146          if (b == buf) {
00147             (void)fprintf(stderr, "Oops!  Missed a case: %c.\n", c) ;
00148             exit(1) ;
00149          }
00150          *b++ = 0 ;
00151          (void)ungetc(c, in) ;
00152          cmdout(buf) ;
00153       }
00154    }
00155    if (linepos != 0)
00156       putchar('\n') ;
00157    exit(0) ;
00158    /*NOTREACHED*/
00159 }