/*
 * 
 *               filtr.c
 * 
 * This procedure removes any escape (Esc) commands that are used
 * as part of the RS-232 Instruction Syntax
 *
 *     Marek Krawus, PCC, March '89
 *     Don McCormick, April '89
 */

#include "defn.h"
#define ESC '\033'

filtr(op)
int  op;
{
    if (op == ESC)
    {
	if ((op = getc(stream)) == '.')
	{
	    if ((op = getc(stream)) == EOF)
	    {
		return;
	    }
	}
	else
	{
	    if (DEBUG)
		fprintf(stderr,
		    "Warning (filtr.c): Expecting a dot after escape character\n");
	    return;
	}
    }
    else if ((op != EOF) && (op != CR) && (op != LF)) {
	char prop;	/* a printable version of op */

	prop = op;
	prop += 64;
	fprintf(stderr,
	    "WARNING: strange character with decimal code -> %d (^%c)\n",
		(int)op, prop );
	return;
    }
    else
	return;

    switch (op)
    {
    case ')':
    case 'Z':
	look_for_plotter_on();
	break;
    case '(':
    case 'Y':
    case 'B':
    case 'E':
    case 'J':
    case 'L':
    case 'O':
    case 'R':
	break;

    case '@':
    case 'H':
    case 'I':
    case 'M':
    case 'N':
	while (((ch = getc(stream)) != EOF) && ch != ':');
	break;
    }
}


look_for_plotter_on()
{
    register int  zn2, zn3;

    if (DEBUG)
	fprintf(stderr, "PRINTER OFF SEQUENCE: ");

throw_away:
    while (((zn3 = getc(stream)) != EOF) && (zn3 != ESC))
	if (DEBUG)
	    fprintf(stderr, "%c", zn3);

    if(zn3 == ESC)
    {
	zn3 = getc(stream);
	if (zn3 == '.')
	{
	    zn2 = getc(stream);
	    if ((zn2 == 'Y') || (zn2 == '('))
	    {
		if (DEBUG)
		   fprintf(stderr, "\n");
		return;
	    }
	    else
		goto throw_away;
	}
	else 
	    goto throw_away;
    }
    else
	fprintf(stderr, "WARNING: I can't find a plotter on sequence\n");
}
