home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / dvipsk / squeeze.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  144 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. /*
  17.  *   This next routine writes out a `special' character.  In this case,
  18.  *   we simply put it out, since any special character terminates the
  19.  *   preceding token.
  20.  */
  21. void specialout(c)
  22. char c ;
  23. {
  24.    if (linepos + 1 > LINELENGTH) {
  25.       putchar('\n') ;
  26.       linepos = 0 ;
  27.    }
  28.    putchar(c) ;
  29.    linepos++ ;
  30.    lastspecial = 1 ;
  31. }
  32. void strout(s)
  33. char *s ;
  34. {
  35.    if (linepos + strlen(s) > LINELENGTH) {
  36.       putchar('\n') ;
  37.       linepos = 0 ;
  38.    }
  39.    linepos += strlen(s) ;
  40.    while (*s != 0)
  41.       putchar(*s++) ;
  42.    lastspecial = 1 ;
  43. }
  44. void cmdout(s)
  45. char *s ;
  46. {
  47.    int l ;
  48.  
  49.    l = strlen(s) ;
  50.    if (linepos + l + 1 > LINELENGTH) {
  51.       putchar('\n') ;
  52.       linepos = 0 ;
  53.       lastspecial = 1 ;
  54.    }
  55.    if (! lastspecial) {
  56.       putchar(' ') ;
  57.       linepos++ ;
  58.    }
  59.    while (*s != 0) {
  60.       putchar(*s++) ;
  61.    }
  62.    linepos += l ;
  63.    lastspecial = 0 ;
  64. }
  65. char buf[BUFLENGTH] ;
  66. int
  67. main(argc, argv)
  68. int argc ;
  69. char *argv[] ;
  70. {
  71.    int c ;
  72.    char *b ;
  73.    char seeking ;
  74.    extern void exit() ;
  75.  
  76.    if (argc > 3 || (in=(argc < 2 ? stdin : fopen(argv[1], "r")))==NULL ||
  77.                     (out=(argc < 3 ? stdout : fopen(argv[2], "w")))==NULL) {
  78.       (void)fprintf(stderr, "Usage:  squeeze [infile [outfile]]\n") ;
  79.       exit(1) ;
  80.    }
  81.    (void)fprintf(out, "%%!\n") ;
  82.    while (1) {
  83.       c = getc(in) ;
  84.       if (c==EOF)
  85.          break ;
  86.       if (c=='%') {
  87.          while ((c=getc(in))!='\n') ;
  88.       }
  89.       if (c <= ' ')
  90.          continue ;
  91.       switch (c) {
  92. case '{' :
  93. case '}' :
  94. case '[' :
  95. case ']' :
  96.          specialout(c) ;
  97.          break ;
  98. case '<' :
  99. case '(' :
  100.          if (c=='(')
  101.             seeking = ')' ;
  102.          else
  103.             seeking = '>' ;
  104.          b = buf ;
  105.          *b++ = c ;
  106.          do {
  107.             c = getc(in) ;
  108.             if (b > buf + BUFLENGTH-2) {
  109.                (void)fprintf(stderr, "Overran buffer seeking %c", seeking) ;
  110.                exit(1) ;
  111.             }
  112.             *b++ = c ;
  113.             if (c=='\\')
  114.                *b++ = getc(in) ;
  115.          } while (c != seeking) ;
  116.          *b++ = 0 ;
  117.          strout(buf) ;
  118.          break ;
  119. default:
  120.          b = buf ;
  121.          while ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||
  122.                 (c>='0'&&c<='9')||(c=='/')||(c=='@')||
  123.                 (c=='!')||(c=='"')||(c=='&')||(c=='*')||(c==':')||
  124.                 (c==',')||(c==';')||(c=='?')||(c=='^')||(c=='~')||
  125.                 (c=='-')||(c=='.')||(c=='#')||(c=='|')||(c=='_')||
  126.                 (c=='=')||(c=='$')||(c=='+')) {
  127.             *b++ = c ;
  128.             c = getc(in) ;
  129.          }
  130.          if (b == buf) {
  131.             (void)fprintf(stderr, "Oops!  Missed a case: %c.\n", c) ;
  132.             exit(1) ;
  133.          }
  134.          *b++ = 0 ;
  135.          (void)ungetc(c, in) ;
  136.          cmdout(buf) ;
  137.       }
  138.    }
  139.    if (linepos != 0)
  140.       putchar('\n') ;
  141.    return 0;
  142.    /*NOTREACHED*/
  143. }
  144.