/* W0.C		System dependent I/O routines			31 Oct 1987 */

#include <ws.h>
#include <stdio.h>

#define BUFMAX 80

char		buffer[BUFMAX];	/* circular input buffer	*/
unsigned	i_in=0;		/* input index			*/
unsigned	i_out=0;	/* output index			*/


FLAG KeyHit()		/* returns TRUE iff console input is waiting	*/
			/* reads waiting char and puts it in buffer	*/

{
	unsigned	_abdos();		/* accu after BDOS call	*/

	if ((buffer[i_in] = _abdos(6,0xFF))) {	/* Direct console I/O	*/
		if (++i_in >= BUFMAX) i_in=0;
		return TRUE;
	}
	return FALSE;
}


unsigned GetKey()	/* returns input char either from input buffer	*/
			/* or from keyboard				*/
{
	static unsigned	c;
	unsigned	_abdos();		/* accu after BDOS call	*/

	if (i_in != i_out) {
		c = buffer[i_out];		/* read from buffer	*/
		if (++i_out>=BUFMAX) i_out=0;
	} else					/* wait for console char */
		while ((c=_abdos(6,0xFF)) == NULL) ;
	return c;
}


FLAG InpWait()		/* returns TRUE iff input is waiting		*/
			/* either in the buffer or at the console	*/
{
	FLAG	KeyHit();	/* TRUE iff input waiting at console	*/

	return (i_in!=i_out) || KeyHit();
}


#define OFFSET	0x1F

char	*curstr = "\33F  ";

void CursorTo(x,y)	/* positions cursor at x,y on screen */
unsigned x,y;
{
	void	OutStr();		/* writes string to screen	*/

	*(curstr+2) = y+OFFSET; *(curstr+3) = x+OFFSET;
	OutStr(curstr);
	return;
}

char	*cofstr = "\33A ";

void CursOff()		/* turns the cursor off		*/
{
	void	OutStr();		/* writes string to screen	*/

	OutStr(cofstr);
	return;
}

char	*constr = "\33A\"";

void CursOn()		/* turns the block cursor back on	*/
{
	void	OutStr();		/* writes string to screen	*/

	OutStr(constr);
	return;
}

char	*lovstr = "\33R";

void LoVideo()		/* switches video attribute to Low */
{
	void	OutStr();		/* writes string to screen	*/

	OutStr(lovstr);
	return;
}

char	*hivstr = "\33S";

void HiVideo()		/* switches video attribute to High */
{
	void	OutStr();		/* writes string to screen	*/

	OutStr(hivstr);
	return;
}

char	*cldstr = "\33Z";

void ClrEol()		/* clears to end of line	*/
{
	void	OutStr();		/* writes string to screen	*/

	OutStr(cldstr);
	return;
}

char	*clsstr = "\33[";

void ClrEos()		/* clears to end of screen	*/
{
	void	OutStr();		/* writes string to screen	*/

	OutStr(clsstr);
	return;
}

char	*delstr = "\33]";

void DelLine()		/* deletes (removes) one line	*/
{
	void	OutStr();		/* writes string to screen	*/

	OutStr(delstr);
	return;
}

char	*insstr = "\33\\";

void InsLine()		/* inserts (adds) one line	*/
{
	void	OutStr();		/* writes string to screen	*/

	OutStr(insstr);
	return;
}

char	*ctlstr = "^ ";

void OutCtl(ch)		/* displays representation of control char ch */
char ch;
{
	void	OutStr();		/* writes string to screen	*/

	*(ctlstr+1) = ch+0x40;
	OutStr(ctlstr);
	return;
}


void OutChar(ch)	/* writes character ch to the screen		*/
char ch;
{
	FLAG	KeyHit();	/* TRUE iff input is waiting		*/

	putchar(ch);
	if (KeyHit()) ;		/* make sure not to miss any input	*/
	return;
}


void OutStr(str)		/* writes string str to screen	*/
char	*str;
{
	FLAG	KeyHit();	/* TRUE iff input is waiting		*/

	printf("%s",str);
	if (KeyHit()) ;		/* make sure not to miss any input	*/
	return;
}
