home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / pastex13.lha / DVIPS / dvips5519.lha / dvips / squeeze.c.bak < prev    next >
Text File  |  1993-09-04  |  3KB  |  154 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.       putchar(*s++) ;
  69.    }
  70.    linepos += l ;
  71.    lastspecial = 0 ;
  72. }
  73. char buf[BUFLENGTH] ;
  74. #ifndef VMS
  75. void
  76. #endif
  77. main(argc, argv)
  78. int argc ;
  79. char *argv[] ;
  80. {
  81.    int c ;
  82.    char *b ;
  83.    char seeking ;
  84.    extern void exit() ;
  85.  
  86.    if (argc > 3 || (in=(argc < 2 ? stdin : fopen(argv[1], "r")))==NULL ||
  87.                     (out=(argc < 3 ? stdout : fopen(argv[2], "w")))==NULL) {
  88.       (void)fprintf(stderr, "Usage:  squeeze [infile [outfile]]\n") ;
  89.       exit(1) ;
  90.    }
  91.    (void)fprintf(out, "%%!\n") ;
  92.    while (1) {
  93.       c = getc(in) ;
  94.       if (c==EOF)
  95.          break ;
  96.       if (c=='%') {
  97.          while ((c=getc(in))!='\n') ;
  98.       }
  99.       if (c <= ' ')
  100.          continue ;
  101.       switch (c) {
  102. case '{' :
  103. case '}' :
  104. case '[' :
  105. case ']' :
  106.          specialout(c) ;
  107.          break ;
  108. case '<' :
  109. case '(' :
  110.          if (c=='(')
  111.             seeking = ')' ;
  112.          else
  113.             seeking = '>' ;
  114.          b = buf ;
  115.          *b++ = c ;
  116.          do {
  117.             c = getc(in) ;
  118.             if (b > buf + BUFLENGTH-2) {
  119.                (void)fprintf(stderr, "Overran buffer seeking %c", seeking) ;
  120.                exit(1) ;
  121.             }
  122.             *b++ = c ;
  123.             if (c=='\\')
  124.                *b++ = getc(in) ;
  125.          } while (c != seeking) ;
  126.          *b++ = 0 ;
  127.          strout(buf) ;
  128.          break ;
  129. default:
  130.          b = buf ;
  131.          while ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||
  132.                 (c>='0'&&c<='9')||(c=='/')||(c=='@')||
  133.                 (c=='!')||(c=='"')||(c=='&')||(c=='*')||(c==':')||
  134.                 (c==',')||(c==';')||(c=='?')||(c=='^')||(c=='~')||
  135.                 (c=='-')||(c=='.')||(c=='#')||(c=='|')||(c=='_')||
  136.                 (c=='=')||(c=='$')||(c=='+')) {
  137.             *b++ = c ;
  138.             c = getc(in) ;
  139.          }
  140.          if (b == buf) {
  141.             (void)fprintf(stderr, "Oops!  Missed a case: %c.\n", c) ;
  142.             exit(1) ;
  143.          }
  144.          *b++ = 0 ;
  145.          (void)ungetc(c, in) ;
  146.          cmdout(buf) ;
  147.       }
  148.    }
  149.    if (linepos != 0)
  150.       putchar('\n') ;
  151.    exit(0) ;
  152.    /*NOTREACHED*/
  153. }
  154.