/*
 * (C) 1988 by Adobe Systems Incorporated. All rights reserved.
 *
 * This file may be freely copied and redistributed as long as:
 *   1) This entire notice continues to be included in the file, 
 *   2) If the file has been modified in any way, a notice of such
 *      modification is conspicuously indicated.
 *
 * PostScript, Display PostScript, and Adobe are registered trademarks of
 * Adobe Systems Incorporated.
 * 
 * ************************************************************************
 * THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO CHANGE WITHOUT
 * NOTICE, AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY ADOBE SYSTEMS
 * INCORPORATED. ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY OR 
 * LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO WARRANTY OF ANY 
 * KIND (EXPRESS, IMPLIED OR STATUTORY) WITH RESPECT TO THIS INFORMATION, 
 * AND EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR PARTICULAR PURPOSES AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
 * ************************************************************************
 */

/*
 prettyprint.c				greid Tue Feb 16 23:09:47 1988

 uses "lex(1)" output to construct a pretty-printer for PostScript
 program syntax

 */

#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define TAFTVERSION 0

char	buff[256];
int	level;
short	lastopen, lastnewline, lastcomment;

initialize () {
	level = 0;
	lastopen = FALSE;
	lastnewline = TRUE;
	lastcomment = FALSE;
}

/*
    setlast() is just a maintenance proc for resetting
    a bunch of state variables that probably shouldn't exist
*/
setlast () {
	lastopen = FALSE;
	lastnewline = FALSE;
	lastcomment = FALSE;
}

structurecomment ( token ) {
	printf ("%s", token );
	fprintf ( stderr, "%s\n", token );
	setlast();
	lastcomment = TRUE;
}
firstcolcomment ( token ) {
	printf ("%s", token );
	setlast();
	lastcomment = TRUE;
}
comment ( token ) {
	printf ("\t%s", token );
	setlast();
	lastcomment = TRUE;
}

newline () {
	int i;
	printf("\n", level);
	for ( i=0; i<level; i++ ) {
		printf("    ");
	}
	lastnewline = TRUE;
}

opencurly () {
	if ( lastcomment || lastopen ) {
		newline();
	} else {
#if TAFTVERSION
		newline();
#endif TAFTVERSION
	}
	if ( !lastnewline ) printf(" ");
	sprintf (buff, "{") ;
	printf(buff);
	level++;
	setlast();
	lastopen = TRUE;
}

closecurly () {
	level--;
	if ( !lastnewline && !lastopen ) newline();
	sprintf (buff, "}");
	printf(buff);
	lastnewline = FALSE;
	lastopen = FALSE;
}

litname ( token ) {
	if ( lastopen ) newline();
	if ( lastnewline == FALSE ) printf(" ");
	sprintf(buff,"%s",token);
	printf(buff);
	setlast();
}

exname ( token ) {
	if (!lastnewline) printf(" ");
	lastnewline = FALSE;
	if (lastopen) newline();
	sprintf(buff, "%s",token);
	printf(buff);
	setlast();
}

/*
  This routine is special in order to be able to put newlines in after
  some keywords like "def" and "begin".  It's just to improve readability.
 */
specialexname ( token ) {
	if (!lastnewline) printf(" ");
	if (lastopen) newline();
	sprintf(buff, "%s",token);
	printf(buff);
	setlast();
	newline();
}

string ( token ) {
	if (lastopen) newline();
	if (!lastnewline) printf(" ");
	printf("%s", token);
	setlast();
}

hexstring ( token ) {
	if (lastopen) newline();
	if (!lastnewline) printf(" ");
	sprintf(buff, "%s", token);
	printf(buff);
	setlast();
}

integer ( token ) {
	if (!lastnewline) printf(" ");
	if (lastopen) newline();
	sprintf(buff, "%s",token);
	printf(buff);
	setlast();
}
radix ( token ) {
	if (!lastnewline) printf(" ");
	if (lastopen) newline();
	sprintf(buff, "%s",token);
	printf(buff);
	setlast();
}

