! Low-level asynchronous character i/o routines
! J. Butler 6 Jun 84

include  "inc:atdecs.imp"

!Keyboard buffer starts at @3660 and is 100 bytes long
constshortinteger  kbbeg = 16_3660
constshortinteger  kbend = kbbeg+100
!Line buffer starts at end of keyboard buffer and is 96 bytes long
!Empty when lbeg = lend (lpos then undefined). Exhausted when lpos = lend
constshortinteger  lbeg  = kbend


! INPUT PENDING returns the sum of the number of characters buffered
! in the line and keyboard buffers. 

integerfn  INPUT PENDING
   integer  chrs
   chrs = kbin - kbex      
   chrs = chrs + 100 if  chrs < 0
   chrs = chrs + lend - lpos unless  lbeg = lend
   result  = chrs
end 


! PROBE SYMBOL examines the line buffer/keyboard buffer and returns %true if
! character ch is present.

predicate  PROBE SYMBOL(integer  ch)
   shortinteger  p
   unless  lbeg = lend start 
      !Scan line buffer unless its empty
      p = lpos
      while  p # lend cycle 
         true  if  byteinteger(p) = ch
      repeat 
   finish 

   !Then scan keyboard buffer
   p = kbex
   while  p # kbin cycle 
      true  if  byteinteger(p) = ch
      p = p + 1; p = kbbeg if  p = kbend
   repeat 
   false 
end 


! PENDSYMBOL is to testsymbol as nextsymbol is to readsymbol.
! If the line buffer and keyboard buffer are both empty, pendsymbol returns -1.
! If one or other is not, then pendsymbol returns the value of the first buffered
! character but doesn't advance the buffer pointer

integerfn  PENDSYMBOL
   if  lbeg = lend or  lpos = lend start  ;!Line buffer empty or exhausted
      result  = -1 if  kbin = kbex
      result  = byteinteger(kbex)
   finish 

   result  = byteinteger(lpos)
end 

! CANCEL INPUT vacuum cleans input and keyboard buffers of all characters.

routine  cancel input
   kbin = kbex
   lpos = lend
end 


endoffile