home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume32 / xbbs / part09 / bbscio.c < prev    next >
C/C++ Source or Header  |  1992-09-08  |  2KB  |  145 lines

  1. /*  Version Feb 2 1988 */
  2. #define DEBUG 1
  3. typedef enum {FALSE, TRUE} BOOLEAN;
  4. #include <termio.h>
  5. #define lowbyte(w) ((w) & 0377)
  6. #define highbyte(w) lowbyte((w)>>8)
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #ifdef BUFSIZ
  11. #undef BUFSIZ
  12. #endif
  13. #define BUFSIZ 256
  14. #define EMPTY '\0'
  15. static char cbuf=EMPTY;
  16. static struct termio tbufsave;
  17. BOOLEAN cready()
  18. {
  19.     int c;
  20.     c = (int)cbuf;
  21.     if (c != EMPTY)
  22.         return(TRUE);
  23. cready_loop:
  24.     c = bufread();
  25.     switch (c) {
  26.     case -1:
  27.         if(errno == EINTR || errno == EPIPE)goto cready_loop;
  28.         syserr("read");
  29.     case 0:
  30.         cbuf = (char)EMPTY;
  31.         return(FALSE);
  32.     default:
  33.         cbuf = (char)c;
  34.         return(TRUE);
  35.     }
  36. }
  37.  
  38.  
  39. int cget()
  40. {
  41.     int  c;
  42.     char c1;
  43.     c = (int) cbuf;
  44.     if (c != EMPTY) {
  45.         c=(int)cbuf;
  46.         cbuf=(char)EMPTY;
  47.         return(c & 0377);
  48.     }
  49. cget_loop:
  50.     c = bufread() ;
  51.     switch(c) {
  52.     case -1:
  53.         if(errno == EINTR || errno == EPIPE)goto cget_loop;
  54.         syserr("read");
  55.     case 0:
  56.         goto cget_loop;
  57.     default:
  58.         return(c & 0377);
  59.     }
  60. }
  61. int cget_chat()
  62. {
  63.     int  c;
  64.     char c1;
  65.     c = (int) cbuf;
  66.     if (c != EMPTY) {
  67.         c=(int)cbuf;
  68.         cbuf=(char)EMPTY;
  69.         return(c & 0377);
  70.     }
  71. cget_loops:
  72.     c = bufread();
  73.     switch(c) {
  74.     case -1:
  75.         if(errno == EINTR || errno == EPIPE)return('\0');
  76.         syserr("read");
  77.     case 0:
  78.         goto cget_loops;
  79.     default:
  80.         return(c & 0377);
  81.     }
  82. }
  83.  
  84.  
  85. syserr(msg)
  86. char *msg;
  87. {
  88.     extern int errno,sys_nerr;
  89.     extern char *sys_errlist[];
  90.     
  91.     fprintf(stderr,"ERROR:%s (%d",msg,errno);
  92.     if (errno>0 && errno < sys_nerr)
  93.         fprintf(stderr,";%s)\n",sys_errlist[errno]);
  94.     else
  95.         fprintf(stderr,")\n");
  96.     exit(1);
  97. }
  98. fatal(msg)
  99. char *msg;
  100. {
  101.     fprintf(stderr,"ERROR:%s\n",msg);
  102.     exit(1);
  103. }
  104.  
  105. setraw()
  106. {
  107.     struct termio tbuf;
  108.     if(ioctl(0,TCGETA,&tbuf) == -1)
  109.         syserr("ioctl");
  110.     tbufsave=tbuf;
  111.     tbuf.c_iflag = (IXANY | IGNPAR | IXON | ISTRIP);
  112.     tbuf.c_lflag = ISIG;
  113.     tbuf.c_oflag = 0;
  114.     tbuf.c_cc[VINTR] = 0x0b;    /* Control K */
  115.     tbuf.c_cc[4] =1;
  116.     tbuf.c_cc[5] =5;
  117.     if(ioctl(0,TCSETA,&tbuf) == -1)
  118.         syserr("ioctl2");
  119.     if(ioctl(1,TCSETA,&tbuf) == -1)
  120.         syserr("ioctl2");
  121. }
  122.  
  123. restore()
  124. {
  125.     if (ioctl(0,TCSETAF,&tbufsave) == -1)
  126.         syserr("ioctl3");
  127. }
  128.  
  129. int bufread()
  130. {
  131.     static char sTdbuf[BUFSIZ+1];
  132.     static char *bufp = sTdbuf;
  133.     static int nb = 0;
  134.     int n;
  135.     if( nb == 0 )
  136.         {
  137.         n = read(0, sTdbuf, BUFSIZ);
  138.         if ( n <= 0 ) return(n);
  139.         nb = n;
  140.         bufp = sTdbuf;
  141.         }
  142.     --nb;
  143.     return(*bufp++);
  144. }
  145.