!***********************************************************************
!*
!*                          The SLEEP command
!*
!*      Copyright (C) R.D. Eager   University of Kent   MCMLXXXIV
!*
!***********************************************************************
!
!
!***********************************************************************
!*
!*          Constants
!*
!***********************************************************************
!
constantinteger  quantum = 10;          ! Seconds per doze
!
!
!***********************************************************************
!*
!*          Subsystem references
!*
!***********************************************************************
!
systemstringfunctionspec  failuremessage(integer  mess)
systemintegerfunctionspec  parmap
externalroutinespec  prompt(string (255) s)
systemintegerfunctionspec  pstoi(string (63) s)
systemroutinespec  setfname(string (63) s)
systemroutinespec  setpar(string (255) s)
externalroutinespec  set return code(integer  i)
systemstringfunctionspec  spar(integer  n)
!
!
!***********************************************************************
!*
!*          Director references
!*
!***********************************************************************
!
externalintegerfunctionspec  ddelay(integer  secs)
!
!
!***********************************************************************
!*
!*          Service routines
!*
!***********************************************************************
!
routine  readline(stringname  s)
integer  c
!
s = ""
cycle 
   cycle 
      readsymbol(c)
      exit  if  c = nl
      s <- s.tostring(c)
   repeat 
   while  length(s) > 0 and  charno(s,length(s)) = ' ' cycle 
      length(s) = length(s) - 1
   repeat 
   exit  unless  s = ""
repeat 
end ;   ! of readline
!
!
!***********************************************************************
!*
!*          S L E E P
!*
!***********************************************************************
!
externalroutine  sleep(string (255) parms)
integer  flag,seconds,interval
string (255) secstring
!
setpar(parms)
if  parmap > 1 then  start 
   flag = 263;                          ! Wrong number of parameters
   -> err
finish 
!
if  parmap = 1 and  spar(1) = "?" then  start 
   printstring("There is one parameter: the number of seconds to sleep")
   newline
   set return code(0)
   return 
finish 
!
if  parmap = 0 then  start 
   prompt("Seconds: ")
   readline(secstring)
else 
   secstring = spar(1)
finish 
!
seconds = pstoi(secstring)
unless  1 <= seconds <= 7200 then  start 
   setfname(secstring)
   flag = 202;                          ! Invalid parameter
   -> err
finish 
!
! Since  the  'ddelay'  call may not be interrupted, the sleep is broken
! into 'dozes' of maximum length 'quantum' seconds.  The user will  have
! to  wait  a maximum of 'quantum' seconds (until the end of the current
! doze) before an interrupt will take effect.
!
while  seconds > 0 cycle ;              ! Once round for each doze
   if  seconds < quantum then  interval = seconds else  interval = quantum
   flag = ddelay(interval)
   -> err if  flag # 0
   seconds = seconds - interval
repeat 
!
set return code(0)
return 
!
err:
set return code(flag)
newline
printstring("SLEEP fails -".failuremessage(flag))
stop 
end ;   ! of sleep
endoffile