int inx = 0, iny = 2, outx = 0, outy = 19;



void tab(int x, int y)
{
 termtab(VT320,x,y);
}


void title(int x, int y, string s)
{
 int i;

 tab(x, y);

 tprints("{esc}[7m" + s);

 for(i=0;i<(80-slen(s));i++) tputc(' ');

 tprints("{esc}[m");
}


void output(int k)
{
 termline(VT320);                  // before using functions to write to the
                                   // the terminal, we have to put it back
                                   // online
 tprints("{esc}[19;24r");

 tab(outx, outy);

 sputc(k);
 tputc(k);

 outx = termcurs(VT320, 0);
 outy = termcurs(VT320, 1);

 termline(NOTERM);
}    


void input(int k)
{
 termline(VT320);

 tprints("{esc}[2;17r");

 tab(inx, iny);

 tputc(k);

 while((k = sgetc(0)) != -1) tputc(k);

 inx = termcurs(VT320, 0);
 iny = termcurs(VT320, 1);    

 tab(outx, outy);

 termline(NOTERM);
}


void main(void)
{
 int k;

 setterminal(ANSI);
 tprints("{esc}c");

 claimkeyboard(1);

 title(0, 1, "Received data");
 title(0, 18, "Transmitted data");

 tab(outx, outy);

 termline(NOTERM);           // we set the current terminal to none
                             // this is so it won't read bytes 
 while(1)
 {
  if((k = kgetc(0)) != -1) output(k);
  if((k = sgetc(0)) != -1) input(k);
 }
}

