/*
 * Name:	MicroEMACS
 *		AmigaDOS terminal I/O
 * Version:	31
 * Compiler:	Manx Aztec C
 * Last edit:	19-Apr-86 ...!ihnp4!seismo!ut-sally!ut-ngp!mic
 * Created:	19-Apr-86 ...!ihnp4!seismo!ut-sally!ut-ngp!mic
 */
#include	<libraries/dos.h>
#include	<libraries/dosextens.h>
#undef TRUE
#undef FALSE
#include	"def.h"
#include	"sysdef.h"

#define	NIBUF	128			/* Probably excessive.		*/
#define	NOBUF	512			/* Not too big for 750/730.	*/

struct	FileHandle	*tty;
struct	FileHandle	*Open();
char	obuf[NOBUF];			/* Output buffer		*/
int	nobuf;				/* # of bytes in above		*/
char	ibuf[NIBUF];			/* Input buffer			*/
int	nibuf;				/* # of bytes in above		*/
int	nrow;				/* Terminal size, rows.		*/
int	ncol;				/* Terminal size, columns.	*/
#if	MANX
extern	int	Enable_Abort;
#endif

extern	char	*version[];	/* Defined in "version.c"	*/
/*
 * This routine gets called once, to set up the
 * terminal channel.
 */
ttopen()
{
	char WindowName[80];
	
	nrow = 23;
	ncol = 77;
	nobuf = nibuf = 0;
#if	MANX
	Enable_Abort = 0;	/* Disable ^C during file I/O */
#endif
	strcpy(WindowName,"RAW:1/1/639/199/");
	strcat(WindowName,version[0]);
	tty = Open(WindowName,MODE_NEWFILE);
	if (tty == (struct FileHandle *) 0) {
		printf("Can't open Emacs window!\n");
		exit(200);
	}
}

/*
 * This function gets called just
 * before we go back home to the command interpreter.
 * On the Amiga it closes up the virtual terminal window.
 */
ttclose()
{
	if (tty != (struct FileHandle *) 0L) {
		ttflush();
		Close(tty);
	}
	tty = (struct FileHandle *) 0L;
#if	MANX
	Enable_Abort = 1;
#endif
}

/*
 * Write a character to the display.
 * On the Amiga, terminal output is buffered, and
 * we just put the characters in the big array,
 * after cheching for overflow.
 */
ttputc(c)
{
	if (nobuf >= NOBUF)
		ttflush();
	obuf[nobuf++] = c;
}

/*
 * This function does the real work of
 * flushing out buffered I/O on the Amiga. All
 * we do is blast out the block with a write call.
 */
ttflush()
{
	if (nobuf > 0) {
		Write(tty,obuf,(long) nobuf);
		nobuf = 0;
	}
}

/*
 * Read a character from the terminal,
 * performing no editing and doing no echo at all.
 */
ttgetc()
{
	unsigned char c;	/* must be unsigned! */

	Read(tty,&c,1L);
	return ((int) c);
}
