#include "kermit.h"

/*
 *      c o n n e c t
 *
 *      establish a virtual terminal connection
 *      with the remote host, over a tty line.
 *
 */

connect()
{
  int parent;
  char c = NULL, r = '\r';

  if (host)             /* if in host mode, nothing to connect to */
   {
    fprintf(stderr,"Kermit: nothing to connect to\n");
    return;
   }

  parent = fork();      /* start fork to get typeout from remote host */

  if (parent)           /* parent passes typein to remote host */
   {
    printf("Kermit: connected.\r\n");
    stty(0,&rawmode);
    read(0,&c,1);
    while (c != escchr)
     {
      write(remfd,&c,1);
      c = NULL;
      read(0,&c,1);
     }
    kill(parent,9);
    stty(0,&cookedmode);
    printf("\nKermit: disconnected.\n");
    return;
   }
  else                  /* child does the reading from the remote host */
   {
    while(1)
     {
      read(remfd,&c,1);
      write(1,&c,1);
     }
   }
}
