home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / extra / uxconv.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  3KB  |  134 lines

  1. /*
  2.  * connect command for Venix
  3.  *
  4.  */
  5.  
  6. /*
  7.  *  c o n n e c t
  8.  *
  9.  *  Establish a virtual terminal connection with the remote host, over an
  10.  *  assigned tty line. 
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <sgtty.h>
  15. #include <ctype.h>
  16.  
  17. #define TRUE 1
  18. #define FALSE 0
  19. #define ctl(x) ((x) ^ 64)
  20. #define unpar(ch) ((ch) & 127)
  21.  
  22. extern int remote, ttyfd, lecho;
  23. extern char escchr;
  24.  
  25. connect()
  26. {
  27.     int pid,                /* Holds process id of child */
  28.     pid2,                /* process id of second child */
  29.     parray[2],
  30.     tt2fd,                /* FD for the console tty */
  31.     connected;            /* Boolean connect flag */
  32.     char bel = '\07',
  33.     c;
  34.  
  35.    struct sgttyb oldcon;
  36.  
  37.     if (remote)                /* Nothing to connect to in remote */
  38.     {                    /* mode, so just return */
  39.     printmsg("No line specified for connection.");
  40.     return;
  41.     }
  42.  
  43.     tt2fd = ttopen(0);            /* open up the console */
  44.     ttbin(tt2fd,&oldcon);        /* Put it in binary */
  45.  
  46.     pipe(parray);
  47.     pid = fork();            /* Start fork to get typeout */
  48.  
  49.     if (!pid)                /* child1: read remote output */
  50.     {
  51.      nice(-110);            /* real-time priority */
  52.      close(parray[0]);            /* don't need this side */
  53.      while (1)
  54.      {
  55.       struct sgttyb tmp;
  56.       char cbuf[300];
  57.       int cnt;
  58.       ioctl(ttyfd,TIOCQCNT,&tmp);
  59.       cnt = tmp.sg_ispeed;            /* how many chars in buffer */
  60.       if (cnt == 0) cnt=1;            /* if none, wait on read */
  61.       read(ttyfd,cbuf,cnt);            /* read a character */
  62.       write(parray[1],cbuf,cnt);        /* write to the pipe */
  63.      }
  64.     }
  65.     pid2 = fork();            /* make a second child */
  66.     if (!pid2)                /* child2: write remote to screen */
  67.     {
  68.      close(parray[1]);            /* don't need this side */
  69.      while (1)
  70.      {
  71.       int cnt;
  72.       char buf[100];
  73.       if (cnt = read(parray[0],buf,100)) write(1,buf,cnt);
  74.      }
  75.     }
  76. /* resume parent: read from terminal and send out port */
  77.       printmsg("connected...\r");
  78.       connected = TRUE;        /* Put us in "connect mode" */
  79.       while (connected)
  80.       {
  81.       read(tt2fd,&c,1);        /* Get a character */
  82.       c = unpar(c);        /* Turn off the parity */
  83.       if (c == escchr)        /* Check for escape character */
  84.       {
  85.           read(tt2fd,&c,1);
  86.           c = unpar(c);        /* Turn off the parity */
  87.           if (c == escchr)
  88.           {
  89.           c = dopar(c);    /* Do parity if the user requested */
  90.           write(ttyfd,&c,1);
  91.           }
  92.           else
  93.           switch (toupper(c))
  94.           {
  95.           case 'C':
  96.               connected = FALSE;
  97.               write(tt2fd,"\r\n",2);
  98.               break;
  99.  
  100.           case 'H':
  101.             {
  102.               char hlpbuf[100],e;
  103.               sprintf(hlpbuf,"\r\n C to close the connection\r\n");
  104.               write(tt2fd,hlpbuf,strlen(hlpbuf));
  105.               e = escchr;
  106.               if (e < ' ') {
  107.                 write(tt2fd,"^",1);
  108.                 e = ctl(e); }
  109.               sprintf(hlpbuf,"%c to send itself\r\n",e);
  110.                     write(tt2fd,hlpbuf,strlen(hlpbuf));
  111.             }
  112.               break;
  113.  
  114.           default:
  115.               write(tt2fd,&bel,1);
  116.               break;
  117.           }
  118.       }
  119.       else
  120.       {                /* If not escape charater, */
  121.           if (lecho) write(1,&c,1); /* Echo char if requested */
  122.           c = dopar(c);        /* Do parity if the user requested */
  123.           write(ttyfd,&c,1);    /* write it out */
  124.           c = NULL;        /* Nullify it (why?) */
  125.       }
  126.       }
  127.       kill(pid,9);            /* Done, kill the child */
  128.       kill(pid2,9);
  129.       while (wait(0) != -1);        /* and bury him */
  130.       ttres(tt2fd,&oldcon);
  131.       printmsg("disconnected.");
  132.       return;                /* Done */
  133. }
  134.