/*
 *   This is dvips, a freely redistributable PostScript driver
 *   for dvi files.  It is (C) Copyright 1987 by Tomas Rokicki.
 *   You may modify and use this program to your heart's content,
 *   so long as you send modifications to Tomas Rokicki.  It can
 *   be included in any distribution, commercial or otherwise, so
 *   long as the banner string defined in structures.h is not
 *   modified (except for the version number) and this banner is
 *   printed on program invocation, or can be printed on program
 *   invocation with the -? option.
 */
/*
 *   This routine squeezes a PostScript file down to it's
 *   minimum.  We parse and then output it.
 */
#include "stdio.h"
#define LINELENGTH (70)
static int linepos = 0 ;
static int instring ;
static int lastspecial = 1 ;
/*
 *   This next routine writes out a `special' character.  In this case,
 *   we simply put it out, since any special character terminates the
 *   preceding token.
 */
specialout(c)
char c ;
{
   if (linepos + 1 > LINELENGTH) {
      putchar('\n') ;
      linepos = 0 ;
   }
   putchar(c) ;
   linepos++ ;
   lastspecial = 1 ;
}
strout(s)
char *s ;
{
   if (linepos + strlen(s) > LINELENGTH) {
      putchar('\n') ;
      linepos = 0 ;
   }
   linepos += strlen(s) ;
   while (*s != 0)
      putchar(*s++) ;
   lastspecial = 1 ;
}
cmdout(s, space)
char *s ;
int space ;
{
   int l ;

   l = strlen(s) ;
   if (linepos + l + 1 > LINELENGTH) {
      putchar('\n') ;
      linepos = 0 ;
      lastspecial = 1 ;
   }
   if (! lastspecial && space) {
      putchar(' ') ;
      linepos++ ;
   }
   while (*s != 0) {
      putchar(*s++) ;
   }
   linepos += l ;
   lastspecial = 0 ;
}
char buf[1000] ;
main() {
   int c, space ;
   char *b ;
   char seeking ;

   printf("%%!\n") ;
   while (1) {
      c = getchar() ;
      if (c==EOF)
         break ;
      if (c=='%') {
         while ((c=getchar())!='\n') ;
      }
      if (c <= ' ')
         continue ;
      space = 1 ;
      switch (c) {
case '{' :
case '}' :
case '[' :
case ']' :
         specialout(c) ;
         break ;
case '<' :
case '(' :
         if (c=='(')
            seeking = ')' ;
         else
            seeking = '>' ;
         b = buf ;
         *b++ = c ;
         do {
            c = getchar() ;
            *b++ = c ;
            if (c=='\\')
               *b++ = getchar() ;
         } while (c != seeking) ;
         *b++ = 0 ;
         strout(buf) ;
         break ;
case '/':
	 space = 0 ;
	/*FALLTHROUGH*/
default:
         b = buf ;
         while ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||
                (c>='0'&&c<='9')||(c=='/')||(c=='@')||(c=='-')||
		(c=='.')||(c=='#')||(c=='_')||(c=='$')||(c=='&')) {
            *b++ = c ;
            c = getchar() ;
         }
         if (b == buf) {
            fprintf(stderr, "Oops!  Missed a case: %c.\n", c) ;
            exit(1) ;
         }
         *b++ = 0 ;
         ungetc(c, stdin) ;
         cmdout(buf,space) ;
      }
   }
   if (linepos != 0)
      putchar('\n') ;
   exit(0) ;
}
