/*	XWPTERM.C
**
**	Simple terminal routine for XWP 
*/

#include "a:stdio.h"
#include "b:oxege.h"

int FLecho, FLtret, GLpnums;
int CUmaxright, CUmaxdown, CUline, CUcolumn;
char FLdial, *GLphone;
char GLdialpr[6];
extern char FLbclock, FLerror, scratch[];

/*	TERM - XWP SIMPLE TERMINAL SETUP
*/
term()
	{
	char csave;
	csave = FLbclock;
	FLbclock = NULL;
	FLecho = 0;
	savewindow( 0, 0, 23, 79, 0, 1 );
	fill( 0, 0, 0, 79, ' ' | ATTINV, ATTDIM | ATTUND );
	spoke( 0, 2, "XWP TERMINAL ", ATTDIM | ATTUND );
	spoke( 0, 40, " ECHO ", ATTDIM | ATTUND );
	spoke( 0, 50, " EXIT ", ATTDIM | ATTUND );
	spoke( 0, 60, " RETURN ", ATTDIM | ATTUND );
	message( 2, 0, "ESC FOR MENU " );
	defwindow( 1, 1, 0, 22, 79 );
	defcursor(3);
	co( 26 );
	tloop();	
	defcursor(0);
	savewindow( 0, 0, 23, 79, 1, 1 );
	FLbclock = csave;
	}

/*	TLOOP - TERMINAL LOOP
*/
tloop()
	{
	char c;
	CUmaxdown = 22;
	CUmaxright = 80;
	CUinit();
	while ( 1 ) 
		{
		FLtret = NULL;
		if( rxstat() != NULL )	
			{		
			CUout( rx() & 127 ); 
			}		
		if( cis() == CREADY )	
			{		
			c = ci();	
			if ( c == ESC )
				{
				tmenu();
				if ( FLtret == 1)
					{
					return;
					}
				}
			else
				{
				tx( c ); 
				if ( FLecho == 1 )
					{ CUout( c ); }
				} 
			}		
		tasks();
		}
	}

tmenu()
	{
	int choice;
	defcursor( 0 );
	setmenu( 1, 10, " ECHO " );
	setmenu( 2, 10, " EXIT " );
	setmenu( 3, 10, " RETURN " );
	choice = menu( 0, 40, 3, 1, 10, 0 );
	setwindow( 1 );

	switch( choice )
		{
		case 1:
			FLecho = !FLecho;
			if( FLecho )
				{
				spoke( 0, 70, " ECHO ON ", ATTDIM | ATTUND );
				}
			else
				{
				spoke( 0, 70, "         ", ATTDIM | ATTUND );
				}
			break;
		case 2:
			FLtret = 1;
			break;
		case 3:
			break;
		}
	CUrestore();
	defcursor( 3 );
	}


CUinit()
	{
	adr( 0, 0 );
	CUline = 0;
	CUcolumn = 0;
	}

CUout( character )
	int character;
	{
	if( iscntrl( character ))
		{
		switch( character )
			{
			case LF:
				CUline++;
				CUcolumn--;
				break;
			case CR:
				CUcolumn = NULL;
				CUcolumn--;
				break;
			default:
				return;
			}
		}
	co( character );
	CUcolumn++;
	if( CUcolumn > CUmaxright )
		{
		CUcolumn = 0;
		CUline++;
		}
	if( CUline > CUmaxdown )
		{
		CUline = CUmaxdown;
		}
	}

CUrestore()
	{
	adr( CUline, CUcolumn );
	}

/*	DIAL -- Dial a telephone number
**	
**	FLdial = status of dial initialization:
**		0   = not initialized
**		1   = initialized and ok
**		127 = attempt to initialize failed
**
**	GLphone data structure:
**		10 possible fields of:
**			10 bytes	identifier string
**			15 bytes	phone number
**	GLpnums = number of phone numbers found by dailinit()
*/
dial()
	{
	int counter, choice;
	char *pointer;
	/* First check FLdial status and initialize if necessary */
	if( FLdial == NULL )
		{
		dialinit();
		}
	if( FLdial == 127 )
		{
		error( "XWP.PHN NOT FOUND" );
		FLerror = 0;
		return;
		}
	/* Now set up menu areas */
	pointer = GLphone;
	counter = 0;
	while( counter < GLpnums )
		{
		strcpy( scratch, " " );
		strcat( scratch, pointer );
		setmenu( counter+1, 13, scratch );
		pointer += 25;
		++counter;
		}
	setmenu( counter+1, 13, " RETURN " );
	/* Save the environment */
	savewindow( 2, 63, 5+GLpnums, 77, 0, 1 );	
	/* Draw menu area */
	outline( 2, 63, 5+GLpnums, 77, OUTBLK, ATTDIM | ATTALT );
	fill( 3, 64, 3, 76, ' ' | ATTINV, ATTDIM | ATTUND );
	spoke( 3, 64, " AUTODIALER: ", ATTDIM | ATTUND );
	/* accept choice from menu */
	choice = menu( 4, 64, 1, GLpnums+1, 13, 1 );
	setwindow( 0 );
	/* restore environment  */
	savewindow( 2, 63, 5+GLpnums, 77, 1, 1);
	/* execute the choice */
	if( choice <= GLpnums )
		{
		pointer = GLphone-15;
		txstring( GLdialpr );
		txstring( pointer + ( choice * 25 ) );
		tx( CR );
		}
	}
 
dialinit()
	{
	int pdatfile, counter;
	char *pointer;
	if( ( pdatfile = fopen( "XWP.PHN", "r" ) ) == NULL )
		{
		FLdial = 127;
		return;
		}
	FLdial = 1;
	pointer = GLphone = malloc( 500 );
	counter = 0;
	/* read dial prefix */
	fscanf( pdatfile, "%s", GLdialpr );
	while( !feof( pdatfile ) )
		{
		fscanf( pdatfile, "%s %s", pointer, pointer+10 );
		pointer += 25;
		++counter;
		}
	GLpnums = counter-1;	/* subtract the last (null) line */
	}

txstring( string )
	char *string;
	{
	char *pointer;
	pointer = string;
	while( *pointer != NULL )
		{
		tx( *pointer );
		++pointer;
		}
	}
 *string;
	{
	char *pointer;
	pointer = string;
	while( *pointer != NULL )
		{
