home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / pastex13.lha / DVIPS / dvips5519.lha / dvips / squeeze.c < prev    next >
C/C++ Source or Header  |  1993-09-04  |  3KB  |  159 lines

  1. /*
  2.  *   This software is Copyright 1988 by Radical Eye Software.
  3.  */
  4. /*
  5.  *   This routine squeezes a PostScript file down to its
  6.  *   minimum.  We parse and then output it.
  7.  */
  8. #include <stdio.h>
  9. #define LINELENGTH (72)
  10. #define BUFLENGTH (1000)
  11. #undef putchar
  12. #define putchar(a) (void)putc(a, out) ;
  13. FILE *in, *out ;
  14. static int linepos = 0 ;
  15. static int lastspecial = 1 ;
  16. extern int strlen() ;
  17. /*
  18.  *   This next routine writes out a `special' character.  In this case,
  19.  *   we simply put it out, since any special character terminates the
  20.  *   preceding token.
  21.  */
  22. void specialout(c)
  23. char c ;
  24. {
  25.    if (linepos + 1 > LINELENGTH) {
  26.       putchar('\n') ;
  27.       linepos = 0 ;
  28.    }
  29.    putchar(c) ;
  30.    linepos++ ;
  31.    lastspecial = 1 ;
  32. }
  33. void strout(s)
  34. char *s ;
  35. {
  36.    if (linepos + strlen(s) > LINELENGTH) {
  37.       putchar('\n') ;
  38.       linepos = 0 ;
  39.    }
  40.    linepos += strlen(s) ;
  41. #ifdef AMIGA
  42.    while (*s != 0) {
  43.       putchar(*s) ;
  44.       s++;
  45.    }
  46. #else
  47.    while (*s != 0)
  48.       putchar(*s++) ;
  49. #endif
  50.    lastspecial = 1 ;
  51. }
  52. void cmdout(s)
  53. char *s ;
  54. {
  55.    int l ;
  56.  
  57.    l = strlen(s) ;
  58.    if (linepos + l + 1 > LINELENGTH) {
  59.       putchar('\n') ;
  60.       linepos = 0 ;
  61.       lastspecial = 1 ;
  62.    }
  63.    if (! lastspecial) {
  64.       putchar(' ') ;
  65.       linepos++ ;
  66.    }
  67.    while (*s != 0) {
  68. #ifdef AMIGA
  69.       putchar(*s) ;
  70.       s++;
  71. #else
  72.       putchar(*s++) ;
  73. #endif
  74.    }
  75.    linepos += l ;
  76.    lastspecial = 0 ;
  77. }
  78. char buf[BUFLENGTH] ;
  79. #ifndef VMS
  80. void
  81. #endif
  82. main(argc, argv)
  83. int argc ;
  84. char *argv[] ;
  85. {
  86.    int c ;
  87.    char *b ;
  88.    char seeking ;
  89.    extern void exit() ;
  90.  
  91.    if (argc > 3 || (in=(argc < 2 ? stdin : fopen(argv[1], "r")))==NULL ||
  92.                     (out=(argc < 3 ? stdout : fopen(argv[2], "w")))==NULL) {
  93.       (void)fprintf(stderr, "Usage:  squeeze [infile [outfile]]\n") ;
  94.       exit(1) ;
  95.    }
  96.    (void)fprintf(out, "%%!\n") ;
  97.    while (1) {
  98.       c = getc(in) ;
  99.       if (c==EOF)
  100.          break ;
  101.       if (c=='%') {
  102.          while ((c=getc(in))!='\n') ;
  103.       }
  104.       if (c <= ' ')
  105.          continue ;
  106.       switch (c) {
  107. case '{' :
  108. case '}' :
  109. case '[' :
  110. case ']' :
  111.          specialout(c) ;
  112.          break ;
  113. case '<' :
  114. case '(' :
  115.          if (c=='(')
  116.             seeking = ')' ;
  117.          else
  118.             seeking = '>' ;
  119.          b = buf ;
  120.          *b++ = c ;
  121.          do {
  122.             c = getc(in) ;
  123.             if (b > buf + BUFLENGTH-2) {
  124.                (void)fprintf(stderr, "Overran buffer seeking %c", seeking) ;
  125.                exit(1) ;
  126.             }
  127.             *b++ = c ;
  128.             if (c=='\\')
  129.                *b++ = getc(in) ;
  130.          } while (c != seeking) ;
  131.          *b++ = 0 ;
  132.          strout(buf) ;
  133.          break ;
  134. default:
  135.          b = buf ;
  136.          while ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||
  137.                 (c>='0'&&c<='9')||(c=='/')||(c=='@')||
  138.                 (c=='!')||(c=='"')||(c=='&')||(c=='*')||(c==':')||
  139.                 (c==',')||(c==';')||(c=='?')||(c=='^')||(c=='~')||
  140.                 (c=='-')||(c=='.')||(c=='#')||(c=='|')||(c=='_')||
  141.                 (c=='=')||(c=='$')||(c=='+')) {
  142.             *b++ = c ;
  143.             c = getc(in) ;
  144.          }
  145.          if (b == buf) {
  146.             (void)fprintf(stderr, "Oops!  Missed a case: %c.\n", c) ;
  147.             exit(1) ;
  148.          }
  149.          *b++ = 0 ;
  150.          (void)ungetc(c, in) ;
  151.          cmdout(buf) ;
  152.       }
  153.    }
  154.    if (linepos != 0)
  155.       putchar('\n') ;
  156.    exit(0) ;
  157.    /*NOTREACHED*/
  158. }
  159.