/* Console.c - Amiga specific console routines                         */
/* Copyright (c) 1990 by J.K. Lindsey                                  */
/* Additions to XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney     */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
/* You may give out copies of this software; for conditions see the    */
/* file COPYING included with this distribution.                       */

#include <proto/exec.h>
#include <proto/intuition.h>
#include "autil2.h"

OpenConsole(wrequest,rrequest,wport,rport,wname,rname,window)
struct IOStdReq **wrequest,**rrequest;
struct MsgPort **wport,**rport;
char *wname,*rname;
struct Window *window;{
   if(!(*wport=(struct MsgPort *)CreatePort(wname,0)))return(1);
   if(!(*wrequest=(struct IOStdReq *)CreateStdIO(*wport)))return(1);
   if(!(*rport=(struct MsgPort *)CreatePort(rname,0)))return(1);
   if(!(*rrequest=(struct IOStdReq *)CreateStdIO(*rport)))return(1);
   (*wrequest)->io_Data=(APTR)window;
   (*wrequest)->io_Length=sizeof(*window);
   if(OpenDevice("console.device",0,(struct IORequest *)*wrequest,0)!=0)return(1);
   (*rrequest)->io_Device=(*wrequest)->io_Device;
   (*rrequest)->io_Unit=(*wrequest)->io_Unit;
   return(0);}

void ConsoleWrite(request,string,length)
struct IOStdReq *request;
char *string;
int length;{
  request->io_Command=CMD_WRITE;
  request->io_Data=(APTR)string;
  request->io_Length=length;
  DoIO((struct IORequest *)request);}

char ConsoleRead(struct MsgPort *rport,char *string){
   register temp;
   struct IOStdReq *readreq;
   WaitPort(rport);
   readreq=(struct IOStdReq *)GetMsg(rport);
   temp=*string;
   QueueRead(readreq,string);
   return((char)temp);}

int ConsoleMayRead(struct MsgPort *rport,char *string){
   register temp;
   struct IOStdReq *readreq;
   if(!(readreq=(struct IOStdReq *)GetMsg(rport)))return(-1);
   temp=*string;
   QueueRead(readreq,string);
   return(temp);}

void QueueRead(request,buffer)
struct IOStdReq *request;
char *buffer;{
   request->io_Command=CMD_READ;
   request->io_Data=(APTR)buffer;
   request->io_Length=1;
   SendIO((struct IORequest *)request);}