#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <malloc.h>
#include <ctrlc.h>
#include <rs232.h>


int main(void)
{
       unsigned int c;

       ctrlc_dis();
       if(! comm_init())
               exit(1);

       while(1)
       {
               if( c = comm_rcv())
                       putchar((unsigned char)c);


               if(ctrlc) {
                       ctrlc--;
                       comm_send(0x03);
               }
               if(ctrls) {
                       ctrls--;
                       comm_send(0x13);
               }
               if(ctrlq) {
                       ctrlq--;
                       comm_send(0x10);
               }

               if(kbhit()) {
                       c = getch();
                       if(c == 27)
                               break;
                       comm_send(c);
               }
       }

       comm_exit();
       ctrlc_ena();
}


comm_init()
{
       long far *p;

       p = MK_FP(0, 0x0180);
       if(*p == 0L)
       {
               printf("Error: COMMTSR not installed!\n");
               return 0;
       }

       rs.portadr = 0x2f8;
       rs.irq = 3;
       rs.baudrate = 9600;
       rs.parity = 0;
       rs.wordlen = 8;
       rs.stopbits = 1;

       asm     push es
       asm     push di

       _ES = FP_SEG(&rs);
       _DI = FP_OFF(&rs);
       _AH = COMM_INIT;
       geninterrupt(0x60);

       asm     pop di
       asm     pop es
       return 1;
}


comm_exit()
{
       _AH = COMM_EXIT;
       geninterrupt(0x60);
}


comm_send(unsigned char c)
{
       while(1) {
               _AH = COMM_SEND;
               _AL = c;
               geninterrupt(0x60);
               if(_AX != 0) break;
               if(kbhit()) break;
       }
}


comm_rcv()
{
       _AH = COMM_RCV;
       geninterrupt(0x60);
}