home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / OSKBox.lzh / MAILBOX / CC / hostube.c < prev    next >
C/C++ Source or Header  |  1990-03-17  |  7KB  |  258 lines

  1. #include <stdio.h>
  2. #include <sgstat.h>
  3. #include <signal.h>
  4. #include <errno.h>
  5.  
  6. #define ERROR -1
  7. #define ESC 27
  8. #define CMD '\\'
  9.  
  10. int tnc_in, tnc_out, ctl_in, ctl_out, hostcmd;
  11. int pid;
  12. struct sgbuf term_opt, oldt_opt;
  13.  
  14. #define RXSIZE 2048
  15. char rxbuff[RXSIZE], *rxptr = rxbuff;
  16. int rxfile;
  17.     
  18. main (argc, argv)
  19. char *argv[];
  20. {
  21.      int i;
  22.      char str[40];
  23.  
  24.     if (argc < 2) {
  25.         printf ("No port supplied.\n");
  26.         exit (0);
  27.         }
  28.     strcpy (str, "/pipe/command.");
  29.     strcat (str, argv[1]);
  30.     if ((hostcmd = open (str, 2)) < 0) {
  31.         printf ("Can't find command channel\n");
  32.         exit (0);
  33.         }
  34.     pid = getpid ();
  35.     sprintf (str, "o%d\n", pid);
  36.     write (hostcmd, str, strlen (str));
  37.     sprintf (str, "/pipe/data_in.%d", pid);
  38.     for (i = 0; i < 10; i++) {
  39.         if ((tnc_in = open (str, 3)) != -1) break;
  40.         sleep (1);
  41.         }
  42.     if (tnc_in == -1) {
  43.         printf ("No response from host\n");
  44.         exit (0);
  45.         }
  46.     sprintf (str, "/pipe/data_out.%d", pid);
  47.     tnc_out = open (str, 3);
  48.     sprintf (str, "/pipe/ctl_in.%d", pid);
  49.     ctl_in = open (str, 3);
  50.     sprintf (str, "/pipe/ctl_out.%d", pid);
  51.     ctl_out = open (str, 3);
  52.     printf ("Connection established...\n\n");
  53.      _gs_opt (0, &oldt_opt);
  54.      movmem (&oldt_opt, &term_opt, sizeof (term_opt));
  55.      term_opt.sg_kbich = 0;
  56.      term_opt.sg_kbach = 0;
  57.      term_opt.sg_eofch = 0;
  58.      term_opt.sg_pause = 0;
  59.      _ss_opt (0, &term_opt);
  60.      if (tube ()) exit (errno);
  61. }
  62.  
  63. tube ()
  64. {
  65.      int tx = 0;
  66.      int rx = 0;
  67.      int txfile;
  68.      char fname[80], buffer[256], *p;
  69.      int len;
  70.      char out[256], *p2;
  71.       int i, j;
  72.  
  73.      errno = 0;
  74.      while (1) {
  75.           if (tx)
  76.               if (read (txfile, buffer, 1) == 0) {
  77.                    close (txfile);
  78.                    tx = 0;
  79.                    printf ("\nTransmission completed\n");
  80.               } else {
  81.                    write (tnc_out, buffer, 1);
  82.                    writeln (1, buffer, 1);
  83.                    }
  84.           if ((len = _gs_rdy (0)) != ERROR) {
  85.                len = readln (0, buffer, 256);
  86.                if (*buffer == CMD) {
  87.                         switch (*(buffer+1)) {
  88.     
  89.                    case 't':
  90.                    case 'T':
  91.                              if (tx) {
  92.                                   close (txfile);
  93.                                   tx = 0;
  94.                                   printf ("Transmission aborted\n");
  95.                              } else {
  96.                                   printf ("File to transmit? ");
  97.                                   if (gets (fname, sizeof fname) != 0)
  98.                                        if ((txfile = open (fname, 1)) > 0)
  99.                                             tx = 1;
  100.                                        else
  101.                                             printf ("Error opening file\n");
  102.                              }
  103.                              break;
  104.     
  105.                    case 'r':
  106.                    case 'R':
  107.                              if (rx) {
  108.                                   if (rxptr-rxbuff) {
  109.                                        write (rxfile, rxbuff, rxptr-rxbuff);
  110.                                        rxptr = rxbuff;
  111.                                        }
  112.                                   write (rxfile, "\n", 1);
  113.                                   close (rxfile);
  114.                                   rx = 0;
  115.                                   printf ("Receiving terminated\n");
  116.                              } else {
  117.                                   printf ("File to receive? ");
  118.                                   if (gets (fname, sizeof fname) != 0) {
  119.                                        if ((rxfile = creat (fname, 3)) < 0)
  120.                                                       printf ("Error creating file\n");
  121.                                                   else {
  122.                                            rx = 1;
  123.                                            rxptr = rxbuff;
  124.                                                       }
  125.                                                   }
  126.                                   }
  127.                              break;
  128.  
  129.                    case 's':
  130.                    case 'S':
  131.                                 _ss_opt (0, &oldt_opt);
  132.                              system ("");
  133.                              _ss_opt (0, &term_opt);
  134.                              break;
  135.  
  136.                    case 'c':
  137.                    case 'C':
  138.                              printf ("\nCHD to? ");
  139.                              if (gets (fname, sizeof fname) != 0) {
  140.                                   if (chdir (fname) == -1)
  141.                                        printf ("error during CHD\n");
  142.                                   }
  143.                              break;
  144.  
  145.                    case 'q':
  146.                    case 'Q': 
  147.                             goto quit;
  148.                    
  149.                         }
  150.                } else if (*buffer == ESC)
  151.                     write (ctl_out, buffer+1, len-1);
  152.                else if (write (tnc_out, buffer, len) == ERROR) {
  153.                     goto quit;
  154.                     }
  155.           } else
  156.                if (errno == 246)
  157.                     errno = 0;
  158.                else {
  159.                     goto quit;
  160.                     }
  161.           if (_gs_rdy (ctl_in) != -1) {
  162.                len = readln (ctl_in, buffer, 256);
  163.                writeln (1, buffer, len);
  164.                }
  165.           if ((len = _gs_rdy (tnc_in)) != -1) {
  166.                if ((len = read (tnc_in, buffer, min (len, 256))) == ERROR)
  167.                     if (errno == 244) {
  168.                          errno = 0;
  169.                          write (1, "\007", 1);
  170.                     } else {
  171.                          goto quit;
  172.                          }
  173.                for (p = buffer, p2 = out; p-buffer < len; p++, p2++)
  174.                     *p2 = (*p == '\t') ? oldt_opt.sg_tabcr : *p;
  175.                for (p = out; p - out < len; )
  176.                     p += writeln (1, p, len - (p - out));
  177.                if (rx)
  178.                    for (p = buffer; p-buffer < len; p++) {
  179.                        *p &= 0x7f;
  180.                         writerx (*p);
  181.                    }
  182.           }
  183.           if (!tx && _gs_rdy (0) == ERROR && 
  184.                      _gs_rdy (tnc_in) == ERROR &&
  185.                      _gs_rdy (ctl_in) == ERROR) {
  186.                _ss_ssig (0, SIGWAKE);
  187. /*               _ss_ssig (tnc_in, SIGWAKE);
  188.                _ss_ssig (ctl_in, SIGWAKE); */
  189.                tsleep (6);
  190.                _ss_rel (0);
  191. /*               _ss_rel (tnc_in);
  192.                _ss_rel (ctl_in); */
  193.                errno = 0;
  194.                }
  195.      }
  196. quit:_ss_opt (0, &oldt_opt);
  197.      sprintf (buffer, "c%d\n", pid);
  198.      write (hostcmd, buffer, strlen (buffer));
  199.      close (hostcmd);
  200.      close (tnc_in);
  201.      close (tnc_out);
  202.      close (ctl_in);
  203.      close (ctl_out);
  204.       j = errno;
  205.         sprintf (fname, "/pipe/data_in.%d", pid);
  206.         for (i = 0; i < 10; i++) {
  207.             if (access (fname, 0) == -1) break;
  208.             sleep (1);
  209.             }
  210.         if (i == 10)
  211.             printf ("Host will not close.\n");
  212.         errno = j;
  213.      if (rx) {
  214.           if (rxptr-rxbuff) {
  215.                write (rxfile, rxbuff, rxptr-rxbuff);
  216.                rxptr = rxbuff;
  217.           }
  218.           write (rxfile, "\n", 1);
  219.           close (rxfile);
  220.      }
  221.      return (errno);
  222. }
  223.  
  224. writerx (c)
  225. {
  226.     if (c == '\l')
  227.         return;
  228.     else if (c < ' ' && c != '\t' && c != '\n')
  229.         return;
  230.     *rxptr++ = c;
  231.     if (rxptr-rxbuff == RXSIZE) {
  232.         write (rxfile, rxbuff, RXSIZE);
  233.         rxptr = rxbuff;
  234.         }
  235.     }
  236.  
  237. min (a, b)
  238. {
  239.     return ((a < b) ? a : b);
  240.     }
  241.  
  242. movmem (from, to, count)
  243. register char *from, *to;
  244. register int count;
  245. {
  246.     if (from > to)
  247.         while (count--)
  248.             *to++ = *from++;
  249.     else {
  250.         from += count;
  251.         to += count;
  252.         while (count--)
  253.             *--to = *--from;
  254.         }
  255.     }
  256.  
  257.  
  258.