home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / uniflex.zip / ufconn.c < prev    next >
C/C++ Source or Header  |  1993-08-23  |  8KB  |  223 lines

  1. #include "ufk.h"
  2. #include <signal.h>
  3. #include <setjmp.h>
  4.  
  5. jmp_buf child_env;
  6.  
  7.  
  8. /*
  9.  *  c o n n e c t
  10.  *
  11.  *  Establish a virtual terminal connection with the remote host
  12.  *
  13.  */
  14.  
  15. connect()
  16. {
  17.    int   pid;                             /* Child process ID */
  18.    int   toglog();
  19.    char  c,
  20.          got_escape,
  21.          nul = '\0',
  22.          save_descr[20];
  23.  
  24.    nooutput = FALSE;                      /* we should see something */
  25.    strcpy(save_descr,tty_descr);          /* Save old port */
  26.    if (numprm > 1)
  27.    {
  28.       close_port(FALSE,TRUE);             /* must close old one first */
  29.       call_baud = TRUE;                   /* baudrate must be set again */
  30.       strcpy(tty_descr,params[1]);        /* Get new port */
  31.       if (!open_port(TRUE,FALSE))         /* Try to open the port */
  32.       {
  33.          prterr(ER_POPNERR);
  34.          strcpy(tty_descr,save_descr);    /* Restore old port */
  35.          return;                          /* Error in port setting */
  36.       }
  37.    }
  38.    else
  39.       if (!open_port(TRUE,FALSE))
  40.       {
  41.          prterr(ER_POPNERR);
  42.          return;
  43.       }
  44.    if (remote)
  45.    {
  46.       close_port(FALSE,TRUE);
  47.       prterr(ER_NOLINE);                  /* We're remote.... */
  48.    }
  49.    else
  50.    {
  51.       if (logfileopen)
  52.          logging = TRUE;                  /* if file available, turn log on */
  53.       pid = fork();                       /* Start child task */
  54.       if (pid)
  55.       {                                   /* Parent task */
  56.          fputs("Connected...\n\l",stdout);
  57.          cflg = TRUE;                     /* Put us in "connect mode" */
  58.          purgeline(ttyfd);                /* eat pending input */
  59.          got_escape = FALSE;              /* no escape char received yet */
  60.          while (cflg)                     /* do as long as we're connected */
  61.          {
  62.             read(0,&c,1);                 /* get character from console */
  63.             if (((c&0177) == escchr) || (got_escape)) /* chk for escape char */
  64.             {
  65.                if (!(got_escape = ~got_escape))
  66.                {
  67.                   if ((c&=0177) == escchr)/* maybe another one */
  68.                   {
  69.                      write(ttyfd,&c,1);   /* yes, echo it */
  70.                      if (!fulldup)
  71.                      {
  72.                         write(1,&c,1);    /* echo if half duplex */
  73.                         write_log(c);     /* send to log file as well */
  74.                      }
  75.                   }
  76.                   else
  77.                      switch (tolower(c&0177)) /* special character */
  78.                      {
  79.                         case '0':
  80.                            write(ttyfd,&nul,1); /* send NULL */
  81.                            break;
  82.  
  83.                         case 'b':
  84.                            send_break(ttyfd); /* send BREAK */
  85.                            break;
  86.  
  87.                         case 'c':
  88.                            cflg = FALSE;      /* disconnect */
  89.                            kwrite(1,"\r\l",0);
  90.                            break;
  91.  
  92.                         case 'h':
  93.                            kwrite(1,"\r\lYes, I'm still here...\r\l",0);
  94.                            break;            /* tell him i'm still available */
  95.  
  96.                         case 'q':
  97.                            if (logfileopen)
  98.                            {
  99.                               if (logging)
  100.                               {
  101.                                  logging = FALSE;
  102.                                  kill(pid,12);    /* Send interrupt to child */
  103.                               }
  104.                               kwrite(1,"\r\lLogging suspended\r\l",0);
  105.                            }
  106.                            else
  107.                               kwrite(1,"\r\lLog file is not open\r\l",0);
  108.                            break;
  109.  
  110.                         case 'r':
  111.                            if (logfileopen)
  112.                            {
  113.                               if (!logging)
  114.                               {
  115.                                  logging = TRUE;
  116.                                  kill(pid,12);    /* Send interrupt to child */
  117.                               }
  118.                               kwrite(1,"\r\lLogging resumed\r\l",0);
  119.                            }
  120.                            else
  121.                               kwrite(1,"\r\lLog file is not open\r\l",0);
  122.                            break;
  123.  
  124.                         case 's':
  125.                            kwrite(1,"\r\lLog file status: ",0);
  126.                            if (logfileopen)
  127.                               kwrite(1,"open\r\l",0);
  128.                            else
  129.                               kwrite(1,"closed\r\l",0);
  130.                            kwrite(1,"Logging status:  ",0);
  131.                            if (logging)
  132.                               kwrite(1,"on\r\l",0);
  133.                            else
  134.                               kwrite(1,"off\r\l",0);
  135.                            break;
  136.  
  137.                         case '?':         /* display commands */
  138.                            kwrite(1,"\r\lArguments available:\r\l\l",0);
  139.                            kwrite(1,"? - Show this help message\r\l",0);
  140.                            kwrite(1,"0 - Transmit NULL\r\l",0);
  141.                            kwrite(1,"b - Transmit 'BREAK'\r\l",0);
  142.                            kwrite(1,"c - Close connection\r\l",0);
  143.                            kwrite(1,"h - Report availability\r\l",0);
  144.                            kwrite(1,"q - Quit logging (if started)\r\l",0);
  145.                            kwrite(1,"r - Resume logging\r\l",0);
  146.                            kwrite(1,"s - Show status of connection\r\l",0);
  147.                            break;
  148.  
  149.                         default:
  150.                            write(ttyfd,&c,1);  /* write it out */
  151.                            if (!fulldup)
  152.                            {
  153.                               write(1,&c,1);   /* to console if half duplex */
  154.                               write_log(c);    /* send to logfile */
  155.                            }
  156.                            break;
  157.                   }
  158.                }
  159.             }
  160.             else
  161.             {
  162.                write(ttyfd,&c,1);              /* write it out */
  163.                if (!fulldup)
  164.                {
  165.                   write(1,&c,1);               /* to console if half duplex */
  166.                   write_log(c);                /* send to logfile */
  167.                }
  168.             }
  169.          }
  170.          kill(pid,SIGKILL);                    /* Done, kill the child */
  171.          wait(0);
  172.          if (numprm > 1)
  173.             close_port(FALSE,TRUE);     /* Real close for commandline port */
  174.          else
  175.             close_port(FALSE,FALSE);
  176.          fputs("Disconnected.\n",stdout);
  177.          strcpy(tty_descr,save_descr);         /* Restore old port */
  178.          return;
  179.       }
  180.       else
  181.       {
  182.          setjmp(child_env);                /* Re-enter here after interrupt */
  183.          signal(12,toglog);
  184.          while(TRUE)
  185.          {
  186.             read(ttyfd,&c,1);              /* get character */
  187.             write(1,&c,1);                 /* send to local screen */
  188.             write_log(c);                  /* and to logfile */
  189.          }
  190.       }
  191.    }
  192. }
  193.  
  194. toglog()
  195. {
  196.    if (logging)
  197.       logging = FALSE;                         /* Toggle logging flag */
  198.    else
  199.       logging = TRUE;
  200.    longjmp(child_env,0);                       /* Start again */
  201. }
  202.  
  203. /*
  204.  * Write data to log file
  205.  */
  206.  
  207. write_log(chr)
  208. char chr;
  209. {
  210.    if (logging && logfileopen && chr != CR)
  211.    {
  212.       if (chr == LF)                         /* convert LF to CR */
  213.          chr = CR;
  214.       if ((putc(chr, lfp) == EOF) && (chr != 0xff)) /* write to file */
  215.       {
  216.          prterr(ER_LOGWRTER);                /* Log file write error */
  217.          fclose(lfp);
  218.          logging = logfileopen = FALSE;      /* no more logging */
  219.          logfile[0] = '\0';                  /* Zap filename */
  220.       }
  221.    }
  222. }
  223.