home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc095.zip / ckvcon.c < prev    next >
C/C++ Source or Header  |  1989-08-30  |  7KB  |  277 lines

  1. /* DEC/CMS REPLACEMENT HISTORY, Element CKVCON.C */
  2. /* *2    29-AUG-1989 00:31:38 BUDA "Remove hanging qiow" */
  3. /* *1    11-APR-1989 22:55:34 BUDA "Initial creation" */
  4. /* DEC/CMS REPLACEMENT HISTORY, Element CKVCON.C */
  5. char *connv = "Connect Command, V4.2(011) 23 Mar 89 for VAX/VMS";
  6.  
  7. /*  C K V C O N  --  Dumb terminal connection to remote system, for VMS  */
  8. /*
  9.  Adapted from the Unix C-Kermit connect module by S. Rubenstein for systems
  10.  without fork().  This version of conect() uses contti(&c, &src) to return
  11.  when a character is available from either the console or the comm line,
  12.  to allow sending/receiving without breaking connection.
  13. *
  14. * Edit by
  15. * 011 23-Mar-1989 mab    Clean up doesc() code.  Add malloc() in place of
  16.              static buffer space.  Also increase buffer space.
  17. * 010 06-Mar-1989 mab    General cleanup
  18. *
  19. */
  20.  
  21. #include "ckvvms.h"
  22. #include "ckcker.h"
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <signal.h>
  26. #include <setjmp.h>
  27.  
  28. #ifndef SIGUSR1
  29. #define SIGUSR1 16
  30. #endif
  31.  
  32. extern int local, speed, escape, duplex, parity, flow, seslog, mdmtyp;
  33. extern char ttname[], sesfil[];
  34.  
  35. int i, active;                /* Variables global to this module */
  36. char *chstr();
  37.  
  38. #define LBUFL 500            /* Line buffer */
  39. static char *lbuf = 0;
  40.  
  41.  
  42.  
  43. /*  C O N E C T  --  Perform terminal connection  */
  44.  
  45. conect() {
  46.     int pid,             /* process id of child (modem reader) */
  47.     parent_id,        /* process id of parent (keyboard reader) */
  48.     n;
  49.     int c;            /* c is a character, but must be signed 
  50.                    integer to pass thru -1, which is the
  51.                    modem disconnection signal, and is
  52.                    different from the character 0377 */
  53.     char errmsg[50], *erp, *cp;
  54.  
  55.     if (!local) {
  56.         printf("Sorry, you must 'set line' first\n");
  57.         return(-2);
  58.     }
  59.     if (speed < 0) {
  60.         printf("Sorry, you must 'set speed' first\n");
  61.         return(-2);
  62.         }
  63.     if ((escape < 0) || (escape > 0177)) {
  64.         printf("Your escape character is not ASCII - %d\n",escape);
  65.         return(-2);
  66.     }
  67.     if (ttopen(ttname,&local,mdmtyp) < 0) {
  68.         erp = errmsg;
  69.         sprintf(erp,"Sorry, can't open %s",ttname);
  70.         perror(errmsg);
  71.         return(-2);
  72.         }
  73.         printf("Connecting thru %s, speed %d.\r\n",ttname,speed);
  74.     printf("The escape character is %s (%d).\r\n",chstr(escape),escape);
  75.     printf("Type the escape character followed by C to get back,\r\n");
  76.     printf("or followed by ? to see other options.\r\n");
  77.     if (seslog) printf("(Session logged to %s.)\r\n",sesfil);
  78.  
  79. /* Condition console terminal and communication line */        
  80.  
  81.         if (conbin(escape) < 0) {
  82.         printf("Sorry, can't condition console terminal\n");
  83.         return(-2);
  84.         }
  85.     if (ttvt(speed,flow) < 0) {
  86.         conres();
  87.         printf("Sorry, Can't condition communication line\n");
  88.         return(-2);
  89.         }
  90.  
  91. /* cont'd... */
  92.  
  93.  
  94.     if (!(lbuf = malloc(LBUFL))) {
  95.         printf("Sorry, Can't allocate buffer space\n");
  96.         return(-2);
  97.     }
  98.  
  99. /* ...connect, cont'd */
  100.  
  101.     active = 1;
  102.     do {
  103.         int src;
  104.  
  105.         contti(&c, &src);
  106. /*
  107.  * src = -1 Line error
  108.  *        1 Character from comm line
  109.  *       0 Character from console
  110.  */
  111.         if (src < 0) {         /* Comm line hangup or other error */
  112. /*
  113.  * We should check WHY src < 0 and not just dive under for ANY
  114.  * reason.
  115.  */
  116.         printf("\r\nC-Kermit: Communications line failure\r\n");
  117.         active = 0;
  118.         }
  119.         else
  120.         if (!src) {
  121. /*
  122.  * Character from console
  123.  */
  124. #ifdef mabworkonthiscode
  125.             c &= 0177;
  126. #endif
  127.             if (c == escape) {    /* Look for escape char */
  128.             conresne();    /* Restore to normal attributes */
  129. #ifdef mabworkonthiscode
  130.             c = coninc(0) & 0177;
  131. #else
  132.             c = coninc(0);
  133. #endif
  134.             doesc(c);
  135.             conbin(escape);
  136.             }
  137.             else {        /* Ordinary character */
  138.             ttoc(dopar(c));
  139.             if (duplex) {    /* Half duplex? */
  140.                 conoc(c);    /* Yes, also echo it. */
  141.                 if (seslog) zchout(ZSFILE,c); /* Maybe log it. */
  142.             }            
  143.             }
  144.                 }
  145.             else
  146.         {
  147. /*
  148.  * Character from comm. line
  149.  */
  150. #ifdef mabworkonthiscode
  151.         c &= 0177;        /* Got a char, strip parity. */
  152. #endif
  153.         conoc(c);        /* Put it on the screen. */
  154.         if (seslog) zchout(ZSFILE,c); /* If logging, log it. */
  155.         n = ttchk();    /* Any more left in buffer? */
  156.  
  157. /*
  158.  * Check the routine ttchk and see if it eats a character when it checks the
  159.  * typeahead count.
  160.  */
  161.         if (n > 0) {
  162.             if (n > LBUFL) n = LBUFL;  /* Get them all at once. */
  163.             if ((n = ttxin(n,lbuf)) > 0) {
  164. #ifdef mabworkonthiscode
  165.                 for (i = 0, cp = lbuf; i < n; i++) *cp++ &= 0177;
  166. #endif
  167.                 conxo(n,lbuf);
  168.                 if (seslog) zsoutx(ZSFILE,lbuf,n);
  169.             }
  170.         }
  171.         }
  172.     } while (active);
  173.     cancio();
  174.     conres();
  175.     printf("\r\nC-Kermit Disconnected\r\n");
  176.     return(0);
  177. }
  178.  
  179.  
  180.  
  181. /*  H C O N N E  --  Give help message for connect.  */
  182.  
  183. hconne() {
  184.     int c;
  185.     static char *hlpmsg[] = {
  186. "\r\n ",
  187. "\r\nC to close the connection, or:",
  188. "\r\n  0 (zero) to send a null",
  189. "\r\n  B to send a BREAK",
  190. "\r\n  H to hangup and close connection",
  191. "\r\n  S for status",
  192. "\r\n  ? for help",
  193. "\r\n escape character twice to send the escape character.\r\n\r\n",
  194. " ",
  195. "" };
  196.  
  197. /*
  198.  * Need to save term characteristics/ allow disable binary mode
  199.  * print message, get text and then restore previous state.
  200.  */
  201.     conola(hlpmsg);            /* Print the help message. */
  202.     conol("Command>");            /* Prompt for command. */
  203.     c = coninc(0);
  204.     conoc(c);                /* Echo it. */
  205.     conoll("");
  206.     doesc(c);
  207. }
  208.  
  209.  
  210. /*  C H S T R  --  Make a printable string out of a character  */
  211.  
  212. char *
  213. chstr(c) int c; {
  214.     static char s[8];
  215.  
  216.     if (c < SP) {
  217.     sprintf(s,"CTRL-%c",ctl(c));
  218.     } else sprintf(s,"'%c'",c);
  219.     return(s);
  220. }
  221.  
  222.  
  223.  
  224. /*  D O E S C  --  Process an escape character argument  */
  225.  
  226. doesc(c) register unsigned char c; {
  227.     int d;
  228.     char sbuf[35];
  229.   
  230.     c &= 0177;                /* Make into a 7 bit character */
  231.  
  232.     if (c == escape) {              /* Send escape character */
  233.         d = dopar(c);
  234.         ttoc(d);
  235.     } else {
  236.                                     /* Or else look it up below. */
  237.         if (isupper(c)) c = tolower(c);
  238.         if (iscntrl(c)) c += 'a' - '\001';
  239.         
  240.     switch (c) {
  241.         case 'b':
  242.         ttsndb();        /* Send a BREAK */
  243.         break;
  244.         case 'c':            /* Close connection */
  245.         active = 0;
  246.         conol("\r\n");
  247.         break;
  248.         case 'h':            /* Hang up the line */
  249.         tthang();
  250.         break;
  251.         case 's':            /* Status */
  252.         sprintf(sbuf,"\r\nConnected thru %s",ttname);
  253.         conol(sbuf);
  254.         sprintf(sbuf,", speed: %d",speed);
  255.         conol(sbuf);
  256.         if (seslog) {
  257.             sprintf(sbuf,", logging file: %s",sesfil);
  258.             conol(sbuf);
  259.         }
  260.         conoll("");
  261.         break;
  262.         case '?':            /* Help */
  263.         hconne();
  264.         break;
  265.         case '0':            /* Send a NULL */
  266.         c = '\0';
  267.         d = dopar(c);
  268.         ttoc(d);
  269.         break;
  270.         case SP:            /* Ignore space */
  271.         break;
  272.         default:            /* Beep on everything else */
  273.         conoc(BEL);
  274.         }
  275.     }           
  276. }    
  277.