#include <stdio.h>
#include "gpmdefs.h"
#include "gpmmonitor.h"

extern int InputPtr,CurrentChar,LineNumber;
extern int Special[],*Stack;
extern int SpecialSet[];

int GetCh()
{
	int i;

	i=getchar();
	if (i=='\n') LineNumber++;
	return i;
}

#ifndef ESCAPE
void NextCh()
{
	if (InputPtr != InputStream) CurrentChar = Stack[InputPtr++];
	else
	{
		CurrentChar = GetCh();
		if (CurrentChar==SkipSymbol)
		{
			CurrentChar=GetCh();
			while ((CurrentChar == ' ') || (CurrentChar == '\t') ||
			(CurrentChar == '\n') || (CurrentChar == SkipSymbol))
			{
				CurrentChar = GetCh();
			}
		}
	}
}
#else
void NextCh()
{
/*
 * If a character is to be got from the stack, then
 * it has already been given whatever symbolic name
 * it requires. On the other hand, if it is coming
 * from a file or the keyboard (external stream) then
 * we decide here what kind of char it is.
 */
	if (InputPtr!=InputStream) CurrentChar=Stack[InputPtr++];
	else
	{
		CurrentChar=GetCh();

/*
 * Special processing for this character, if needed.
 * Add specialmark to the character *before* checking
 * if it is a particular special character, because
 * the symbolic names for the special characters
 * are inclusive of the Special Mark.
 */

		if (CurrentChar==EOF) return;
		if (SpecialSet[CurrentChar]) CurrentChar+=SpecialMark;

/*
 * Escape ==> Get the next character and that's it.
 * On end of file, give a warning and return '\n'.
 */

		if (CurrentChar==EscapeSymbol) 
		{ 
			CurrentChar=GetCh(); 
			if (CurrentChar==EOF)
			{
				Monitor(EscapeAtEOF);
				CurrentChar='\n';
			}
			return; 
		}

/*
 * Now skip whitespace if needed. The idea is that you never
 * return any whitespace or the skip layout character. So
 * now eat whitespace & layouts until you find a *real* character.
 * Then do all the special processing on that character ---
 * it won't be a skip layout, so that leaves escape and EOF
 * as special cases.
 */

		while (CurrentChar==SkipSymbol)
		{
/*
 * Eat characters until full.
 */

			CurrentChar=GetCh();
			if (CurrentChar==EOF) return;
			while ((CurrentChar == ' ') || (CurrentChar == '\t') ||
			(CurrentChar == '\n') || (CurrentChar == SkipSymbol))
			{
				CurrentChar=GetCh();
			}

/*
 * Special processing for EOF and escape.
 */

			if (CurrentChar==EOF) return;
			if (SpecialSet[CurrentChar]) CurrentChar+=SpecialMark;
			if (CurrentChar==EscapeSymbol) 
			{ 
				CurrentChar=GetCh(); 
				if (CurrentChar==EOF)
				{
					Monitor(EscapeAtEOF);
					CurrentChar='\n';
				}
				return; 
			}
		}
	}
}
#endif
