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

  1. /*
  2.  * connect command for QNX
  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 <io.h>
  14. #include <dev.h>
  15. #include <stdio.h>
  16.  
  17. #define TRUE    1
  18. #define FALSE    0
  19. #define ctl(x) ((x) ^ 64)
  20. #define unpar(ch) ((ch) & 127)
  21. #define KILL        0x0100
  22. #define EXC_BREAK    0x0002
  23.  
  24. extern int remote,
  25.             lecho,
  26.             speed;
  27.  
  28. extern char escchr, *ttyname;
  29.  
  30. connect()
  31. {
  32.     int pid,                     /* process id of child task */
  33.         exstat,                    /* exit status of child */
  34.         connected;                /* Boolean connect flag */
  35.     char bel = '\07',           /* DING !! */
  36.     c,
  37.     term[5];                    /* the controlling terminal */
  38.  
  39.     unsigned oldconr, oldconw;    /* [???] should this be here? */
  40.  
  41.     if (remote)                    /* Nothing to connect to in remote */
  42.     {                            /* mode, so just return */
  43.         printmsg("No line specified for connection.");
  44.         return;
  45.     }
  46.     speed = 0;                    /* Don't set the console's speed */
  47.  
  48.     sprintf(term, "$tty%d", fdevno(stderr));
  49.  
  50.     pid = fork();              /* Start fork to get typeout from remote host */
  51.  
  52.     if (pid)    /* parent: read from terminal, send out port */
  53.     {
  54.         FILE *tt2r, *ttyw;
  55.  
  56.         if ((tt2r = fopen(term,"r")) == NULL) {
  57.             fprintf(stderr,"Can't open console\n");    /* Open up the console */
  58.             return;
  59.         }
  60.         ttbin(tt2r,&oldconr);
  61.         if ((ttyw = fopen(ttyname, "wu")) == NULL) {
  62.             fprintf(stderr,"Can't open tty line\n");
  63.             return;
  64.         }
  65.         printmsg("connected...\r");
  66.         connected = TRUE;            /* Put us in "connect mode" */
  67.         while (connected)
  68.         {
  69.             c = getc(tt2r);            /* Get a character */
  70.             c = unpar(c);            /* Turn off the parity */
  71.             if (c == escchr)        /* Check for escape character */
  72.             {
  73.                 c = getc(tt2r);
  74.                 c = unpar(c);        /* Turn off the parity */
  75.                 if (c == escchr)
  76.                 {
  77.                     c = dopar(c);    /* Do parity if the user requested */
  78.                     putc(c, ttyw);  /* write to tty line */
  79.                 }
  80.                 else
  81.                     switch (toupper(c))
  82.                     {
  83.                         case 'C':
  84.                             connected = FALSE;
  85.                             fprintf(stdout,"\r\n");
  86.                             break;
  87.  
  88.                         case 'H':
  89.                         {
  90.                             char hlpbuf[100],e;
  91.                             sprintf(hlpbuf,
  92.                             "\n\n C to close the connection\n B to send a break\n");
  93.  
  94.                             fprintf(stdout, hlpbuf);
  95.                             e = escchr;
  96.                             if (e < ' ') {
  97.                                 putc('^', stderr);
  98.                                 e = ctl(e);
  99.                             }
  100.                             sprintf(hlpbuf," %c to send itself\r\n",e);
  101.                             fprintf(stdout, hlpbuf);
  102.                             break;
  103.                         }
  104.                         case 'B':
  105.                             combreak(ttyw);
  106.                             break;
  107.  
  108.                         default:
  109.                               putc(bel, stderr);
  110.                             break;
  111.                       }
  112.             }
  113.             else
  114.             {                                /* If not escape charater, */
  115.                 if (lecho) putc(c, stdout);    /* Echo char if requested */
  116.                 c = dopar(c);                /* Do parity if the user requested */
  117.                 putc(c, ttyw);                /* write it out */
  118.                 c = NULL;                    /* Nullify it (why?) */
  119.             }
  120.         }
  121.         set_exception(pid, KILL, 0);        /* Done, kill the child */
  122.         wait(&exstat);                        /* and bury him */
  123.         ttres(tt2r,&oldconr);
  124.         printmsg("disconnected.");
  125.         return;                                /* Done */
  126.     }
  127.     else    /* child:  read remote input, output to console */  
  128.     {
  129.         char c;
  130.         FILE *ttyr;
  131.         if ((ttyr   = fopen(ttyname , "r")) == NULL)
  132.             fprintf(stderr,"Can't open tty line in interior fork\n");
  133.         stdout = fopen(term,"w");
  134.         while (1)
  135.         {
  136.             c = getc(ttyr);            /* read a character */
  137.             putc(c, stdout);        /* write to terminal */
  138.             fflush(stdout);
  139.         }
  140.     }
  141. }
  142.