home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ENTERPRS / CPM / TERMS / ZMODEM.ZIP / RBSB.C next >
Text File  |  1987-09-28  |  5KB  |  218 lines

  1. /*% shar zmodem.h zm.c sz.c>/tmp/rzsz1.sh;shar rz.c rbsb.c rz.1 sz.1 gz>/tmp/rzsz2.sh
  2.  * -rev 05-28-86
  3.  * mode function and most of the rest of the system dependent
  4.  * stuff for rb.c and sb.c   This file is #included so the includer
  5.  * can set parameters such as HOWMANY.  See the main file (rz.c/sz.c)
  6.  * for compile instructions.
  7.  */
  8.  
  9. #ifdef V7
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <sgtty.h>
  13. #define OS "V7/BSD"
  14. #endif
  15.  
  16. #ifndef OS
  17. #ifndef USG
  18. #define USG
  19. #endif
  20. #endif
  21.  
  22. #ifdef USG
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <termio.h>
  26. #include <sys/ioctl.h>
  27. #define OS "SYS III/V"
  28. #endif
  29.  
  30.  
  31. struct {
  32.     unsigned baudr;
  33.     int speedcode;
  34. } speeds[] = {
  35.     110,    B110,
  36.     300,    B300,
  37.     600,    B600,
  38.     1200,    B1200,
  39.     2400,    B2400,
  40.     4800,    B4800,
  41.     9600,    B9600,
  42.     19200,    EXTA,
  43.     9600,    EXTB,
  44.     0,
  45. };
  46.  
  47. int Twostop;        /* Use two stop bits */
  48.  
  49. static unsigned
  50. getspeed(code)
  51. {
  52.     register n;
  53.  
  54.     for (n=0; speeds[n].baudr; ++n)
  55.         if (speeds[n].speedcode == code)
  56.             return speeds[n].baudr;
  57.     return 0;
  58. }
  59.  
  60.  
  61.  
  62. #ifdef ICANON
  63. struct termio oldtty, tty;
  64. #else
  65. struct sgttyb oldtty, tty;
  66. struct tchars oldtch, tch;
  67. #endif
  68.  
  69. int iofd = 0;        /* File descriptor for ioctls & reads */
  70.  
  71. /*
  72.  * mode(n)
  73.  *  2: set a cbreak, XON/XOFF control mode if using Pro-YAM's -g option
  74.  *  1: save old tty stat, set raw mode 
  75.  *  0: restore original tty mode
  76.  */
  77. mode(n)
  78. {
  79.     static did0 = FALSE;
  80.  
  81.     vfile("mode:%d", n);
  82.     switch(n) {
  83. #ifdef USG
  84.     case 2:    /* Cbreak mode used by sb when -g detected */
  85.         if(!did0)
  86.             (void) ioctl(iofd, TCGETA, &oldtty);
  87.         tty = oldtty;
  88.  
  89.         tty.c_iflag = BRKINT|IXON;
  90.  
  91.         tty.c_oflag = 0;    /* Transparent output */
  92.  
  93.         tty.c_cflag &= ~PARENB;    /* Disable parity */
  94.         tty.c_cflag |= CS8;    /* Set character size = 8 */
  95.         if (Twostop)
  96.             tty.c_cflag |= CSTOPB;    /* Set two stop bits */
  97.  
  98. #ifdef XCLUDE
  99.         tty.c_lflag = XCLUDE | ISIG;
  100. #else
  101.         tty.c_lflag = ISIG;
  102. #endif
  103.  
  104.         tty.c_cc[VINTR] = Zmodem ? 03:030;    /* Interrupt char */
  105.         tty.c_cc[VMIN] = 1;
  106.  
  107.         (void) ioctl(iofd, TCSETAW, &tty);
  108.         did0 = TRUE;
  109.         return OK;
  110.     case 1:
  111.         if(!did0)
  112.             (void) ioctl(iofd, TCGETA, &oldtty);
  113.         tty = oldtty;
  114.  
  115.         tty.c_iflag = IGNBRK;
  116.  
  117.          /* No echo, crlf mapping, INTR, QUIT, delays, no erase/kill */
  118.         tty.c_lflag &= ~(ECHO | ICANON | ISIG);
  119. #ifdef XCLUDE
  120.         tty.c_lflag |= XCLUDE;
  121. #endif
  122.  
  123.         tty.c_oflag = 0;    /* Transparent output */
  124.  
  125.         tty.c_cflag &= ~PARENB;    /* Leave baud rate alone, disable parity */
  126.         tty.c_cflag |= CS8;    /* Set character size = 8 */
  127.         if (Twostop)
  128.             tty.c_cflag |= CSTOPB;    /* Set two stop bits */
  129.         tty.c_cc[VMIN] = HOWMANY; /* Satisfy reads when this many chars in */
  130.         tty.c_cc[VTIME] = 1;    /* ... or in this many tenths of seconds */
  131.         (void) ioctl(iofd, TCSETAW, &tty);
  132.         did0 = TRUE;
  133.         Baudrate = getspeed(tty.c_cflag & CBAUD);
  134.         return OK;
  135. #endif
  136. #ifdef V7
  137.     case 2:    /*  This doesn't work ... */
  138.         printf("No mode(2) in V7/BSD!"); bibi(99);
  139.         if(!did0) {
  140.             ioctl(iofd, TIOCEXCL, 0);
  141.             ioctl(iofd, TIOCGETP, &oldtty);
  142.             ioctl(iofd, TIOCGETC, &oldtch);
  143.         }
  144.         tty = oldtty;
  145.         tch = oldtch;
  146.         tch.t_intrc = Zmodem ? 03:030;    /* Interrupt char */
  147.         tty.sg_flags |= (ODDP|EVENP|CBREAK);
  148.         tty.sg_flags &= ~(ALLDELAY|CRMOD|ECHO|LCASE);
  149.         ioctl(iofd, TIOCSETP, &tty);
  150.         ioctl(iofd, TIOCSETC, &tch);
  151.         did0 = TRUE;
  152.         return OK;
  153.     case 1:
  154.         if(!did0) {
  155.             ioctl(iofd, TIOCEXCL, 0);
  156.             ioctl(iofd, TIOCGETP, &oldtty);
  157.             ioctl(iofd, TIOCGETC, &oldtch);
  158.         }
  159.         tty = oldtty;
  160.         tty.sg_flags |= RAW;
  161.         tty.sg_flags &= ~ECHO;
  162.         ioctl(iofd, TIOCSETP, &tty);
  163.         did0 = TRUE;
  164.         Baudrate = getspeed(tty.sg_ospeed);
  165.         return OK;
  166. #endif
  167.     case 0:
  168.         if(!did0)
  169.             return ERROR;
  170. #ifdef USG
  171.         (void) ioctl(iofd, TCSBRK, 1);    /* Wait for output to drain */
  172.         (void) ioctl(iofd, TCFLSH, 1);    /* Flush input queue */
  173.         (void) ioctl(iofd, TCSETAW, &oldtty);    /* Restore original modes */
  174.         (void) ioctl(iofd, TCXONC,1);    /* Restart output */
  175. #endif
  176. #ifdef V7
  177.         ioctl(iofd, TIOCSETP, &oldtty);
  178.         ioctl(iofd, TIOCSETC, &oldtch);
  179.         ioctl(iofd, TIOCNXCL, 0);
  180. #endif
  181.         return OK;
  182.     default:
  183.         return ERROR;
  184.     }
  185. }
  186.  
  187. sendbrk()
  188. {
  189. #ifdef V7
  190. #ifdef TIOCSBRK
  191. #define CANBREAK
  192.     sleep(1);
  193.     ioctl(iofd, TIOCSBRK, 0);
  194.     sleep(1);
  195.     ioctl(iofd, TIOCCBRK, 0);
  196. #endif
  197. #endif
  198. #ifdef USG
  199. #define CANBREAK
  200.     ioctl(iofd, TCSBRK, 0);
  201. #endif
  202. }
  203.  
  204. #ifdef FIONREAD
  205. #define READCHECK
  206. /*
  207.  *  Return non 0 iff something to read from io descriptor f
  208.  */
  209. rdchk(f)
  210. {
  211.     static long lf;
  212.  
  213.     ioctl(f, FIONREAD, &lf);
  214.     return ((int) lf);
  215. }
  216. #endif
  217.  
  218.