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 / UTILS / S / UZI.ARK / DEVTTY.C < prev    next >
Text File  |  1988-11-30  |  4KB  |  208 lines

  1. /**************************************************
  2. UZI (Unix Z80 Implementation) Kernel:  devtty.c
  3. ***************************************************/
  4.  
  5.  
  6. #include "unix.h"
  7. extern struct u_data udata;
  8.  
  9. #define TTYSIZ 132
  10.  
  11. char ttyinbuf[TTYSIZ];
  12.  
  13. struct s_queue ttyinq = {
  14.     ttyinbuf,
  15.     ttyinbuf,
  16.     ttyinbuf,
  17.     TTYSIZ,
  18.     0,
  19.     TTYSIZ/2
  20. };
  21.  
  22. int stopflag;   /* Flag for ^S/^Q */
  23. int flshflag;   /* Flag for ^O */
  24.  
  25. tty_read(minor, rawflag)
  26. int16 minor;
  27. int16 rawflag;
  28. {
  29.     int nread;
  30.  
  31.     nread = 0;
  32.     while (nread < udata.u_count)
  33.     {
  34.         for (;;)
  35.         {
  36.             di();
  37.             if (remq(&ttyinq,udata.u_base))
  38.                 break;
  39.             psleep(&ttyinq);
  40.             if (udata.u_cursig || udata.u_ptab->p_pending)  /* messy */
  41.             {
  42.                 udata.u_error = EINTR;
  43.                 return(-1);
  44.             }
  45.         }
  46.         ei();   
  47.  
  48.         if (nread++ == 0 && *udata.u_base == '\004')   /* ^D */
  49.             return(0);
  50.  
  51.         if (*udata.u_base == '\n')
  52.             break;
  53.         ++udata.u_base;
  54.     } 
  55.     return(nread);
  56. }
  57.  
  58.  
  59.  
  60. tty_write(minor, rawflag)
  61. int16 minor;
  62. int16 rawflag;
  63. {
  64.     int towrite;
  65.  
  66.     towrite = udata.u_count;
  67.  
  68.     while (udata.u_count-- != 0)
  69.     {
  70.         for (;;)        /* Wait on the ^S/^Q flag */
  71.         {
  72.             di();
  73.             ifnot (stopflag)    
  74.                 break;
  75.             psleep(&stopflag);
  76.             if (udata.u_cursig || udata.u_ptab->p_pending)  /* messy */
  77.             {
  78.                 udata.u_error = EINTR;
  79.                 return(-1);
  80.             }
  81.         }
  82.         ei();   
  83.         
  84.         ifnot (flshflag)
  85.         {
  86.             if (*udata.u_base == '\n')
  87.                 _putc('\r');
  88.             _putc(*udata.u_base);
  89.         }
  90.         ++udata.u_base;
  91.     }
  92.     return(towrite);
  93. }
  94.  
  95.  
  96.  
  97. tty_open(minor)
  98. int minor;
  99. {
  100.     return(0);
  101. }
  102.  
  103.  
  104. tty_close(minor)
  105. int minor;
  106. {
  107.     return(0);
  108. }
  109.  
  110.  
  111. tty_ioctl(minor)
  112. int minor;
  113. {
  114.     return(-1);
  115. }
  116.  
  117.  
  118.  
  119. /* This tty interrupt routine checks to see if the uart receiver actually
  120. caused the interrupt.  If so it adds the character to the tty input
  121. queue, echoing and processing backspace and carriage return.  If the queue 
  122. contains a full line, it wakes up anything waiting on it.  If it is totally
  123. full, it beeps at the user. */
  124.  
  125. tty_int()
  126. {
  127.     register char c;
  128.     register found;
  129.     char oc;
  130.  
  131.     found = 0;
  132.  
  133. again:
  134.     if( (in(0x72)&0x81) != 0x81 )
  135.        return (found);
  136.     c = in(0x73) & 0x7f;
  137.  
  138.     if (c==0x1a) /* ^Z */
  139.         idump();        /* For debugging */
  140.  
  141.     if (c == '\003')  /* ^C */
  142.         sendsig(NULL, SIGINT);
  143.     else if (c == '\017')  /* ^O */
  144.         flshflag = !flshflag;
  145.     else if (c == '\023')   /* ^S */
  146.         stopflag = 1;
  147.     else if (c == '\021')  /* ^Q */
  148.     {
  149.         stopflag = 0;
  150.         wakeup(&stopflag);
  151.     }
  152.     else if (c == '\b')
  153.     {
  154.         if (uninsq(&ttyinq,&oc))
  155.         {
  156.             if (oc == '\n')
  157.                 insq(&ttyinq,oc);   /* Don't erase past newline */
  158.             else
  159.             {
  160.                 _putc('\b');
  161.                 _putc(' ');
  162.                 _putc('\b');
  163.             }
  164.         }
  165.     }
  166.     else
  167.     {
  168.         if (c == '\r' || c == '\n')
  169.         {
  170.             c = '\n';
  171.             _putc('\r');
  172.         }
  173.  
  174.         if (insq(&ttyinq,c))
  175.             _putc(c);
  176.         else
  177.             _putc('\007');              /* Beep if no more room */
  178.     }
  179.  
  180.     if (c == '\n' || c == '\004')   /* ^D */
  181.         wakeup(&ttyinq);
  182.  
  183.     found = 1;
  184.     goto again;         /* Loop until the uart has no data ready */
  185. }
  186.  
  187. #ifdef vax
  188.  
  189. _putc(c)
  190. char c;
  191. {
  192.     write(1,&c,1);
  193. }
  194.  
  195. #else
  196.  
  197. _putc(c)
  198. char c;
  199. {
  200.     while(!(in(0x72)&02))
  201.         ;
  202.     out(c,0x73);
  203. }
  204.     
  205. #endif
  206.  
  207.  
  208.