!
! File:   UNIXS
!
! UNIX - EMAS Communications Software
!
! Based on ERCC Microcomputer Support
! Group   Micro - EMAS routines.
!
! Author: Mark Taunton, ERCC
!
! Date:   9th September 1980
!
!
%external %routine %spec       copy     (%string(63) f1f2)
%external %routine %spec      setmode  (%string(63) mode)
%external %routine %spec      messages (%string(63) state)
%external %routine %spec      destroy  (%string(63) file)
%external %routine %spec      define   (%string(63) defs)
%external %routine %spec      clear    (%string(63) stno)
%external %integer %fn %spec  exist    (%string(63) file)

%routine  get file name (%string(63)%name fn)
   %external %routine %spec prompt (%string(15) ptext)
   %integer sym, p
   fn = ""
   setmode ("append=5");   ! Put <ENQ> on end of prompt
   prompt ("EMAS File:")
   sym = nl
   %while sym = ' ' %or sym = nl %cycle
      readsymbol(sym)
   %repeat
   p = 0
   %while ' ' # sym # nl %cycle
      %unless p = 63 %start
         p = p + 1
         charno(fn,p) = sym
      %finish
      read symbol(sym)
   %repeat
   length(fn) = p
%end


%external %routine  take file (%string(63) dummy parm)
   !
   ! Reads all input from terminal (in binary i/p mode)
   ! and writes it out to a temp file. If transfer is 
   ! successful (the UNIX end sends 'Y' at End-of-file),
   ! the temp file is copied to the target file, (which
   ! must not already exist). The temp file is destroyed
   ! whatever the outcome.
   ! Any character which is preceded by a DLE (character 16)
   ! is passed without examination. In this way the stopinput
   ! character ('?') can itself be read without significance.
   !

   %external %routine %spec      close stream (%integer stream)

   %const %string(10)  temp file = "ss#tmpunix"
   %const %integer     dle = 16, cr = 13

   %integer mode, i, chars, tf
   %string(63) fl1, fl2, file

   %routine abort (%string(63) reason)
      printstring("** Host -- ")
      printstring(reason)
      newline
      setmode ("append=0,flow=on")
      messages("on")
      %if tf # 0 %then destroy (temp file)
      %stop
   %end

   tf = 0
   messages ("off")
   get file name (file)
   tf = 0
   %if exist (file) # 0 %then  abort ("File already exists")

   ! Test for PD File member

   %if  file  ->  fl1.("_").fl2  %and exist(fl1) = 0 %then %c
      abort ("P.D. File does not exist.")

   define("1,".temp file)
   selectoutput(1)
   tf = 1;                    ! Temp file now exists
   messages("off")
   setmode("append=0,flow=off,stopinput=63"); ! set terminator = '?'
   chars = 0
   %cycle
      readch(i)
      %if  i = dle  %start
         readch(i);            ! next character transparent
      %finish %else %start
         %exit %if  i = 63;    ! Terminator
      %finish
      chars = chars + 1;
      printch(i)
   %repeat
   selectoutput(0)
   closestream(1)
   readsymbol(i);  ! Confirmation character.
   setmode ("flow=on")
   %if i # 'y' %then abort("Transfer failed")
   copy(temp file.",".file)
   destroy(temp file)
   printstring("** Host --")
   write(chars,0)
   printstring(" bytes received")
   messages("on")
%end


%external %routine  give file (%string(63) dummy parm)
   !
   !   Opens EMAS file, (having tested for its existence)
   !   as a store-map file (for speed) and shunts the
   !   contents a lump at a time down the line to UNIX.
   !   Protocol defined for Micros is followed. ie.
   !
   !   '*' is the escape character:
   !
   !     *B       Beginning of file
   !     *E       End of file.
   !     *C       Literal '*' character
   !     *?       Anything else implies abnormal
   !              termination - eg.
   !                 ** Host -- file does not exist   or
   !                 ** HOST DOWN  (!)
   !

   %external %integer %fn %spec  smaddr   (%integer chan,  %c
                                           %integername len)
   %external %routine %spec      closesm  (%integer chan)
   %system   %routine %spec      console  (%integer ser, %c
                                           %integername start, len)

   %const %integer  chan no = 2
   %const %string(1) chan id = "2";  ! Must be same No. as CHAN NO
   %const %string(3) S1 = "*B", S2 = "*C", S3 = "*E
"
   %integer startseq, endseq, starseq
   startseq = addr(s1) + 1
   starseq  = addr(s2) + 1
   endseq   = addr(s3) + 1
   %own %integer two = 2, three = 3

   %string(63) file
   %integer fstart;  ! For store-mapping
   %integer flen;    ! Length of file to be transfered
   %integer pos;     ! Address within store-map file
   %integer st, l;   ! Start and length of area for console call
   %byteinteger ch

   %routine abort(%string(70) reason)
      printstring("** Host -- ")
      printstring(reason)
      newline
      %stop
   %end


   get file name (file)
   %if length(file) > 30 %then abort("Invalid filename")
   %if exist (file) =  0 %then abort("File ".file. %c
                                     " does not exist, or no access")
   define("sm".chan id.",".file)
   fstart = smaddr(chan no,flen)
   %if flen = 0 %then abort("File is empty")
   messages ("off")
   setmode("flow=on")
   st = fstart
   console(10,startseq,two);       ! Printstring("*B")
   %cycle pos = fstart, 1, fstart + flen - 1
      ch = byteinteger(pos)
      %if ch = '*' %start
         l = pos - st
         console(10,st,l) %if l # 0
         console(10,starseq,two)
         st = pos + 1
      %finish
   %repeat
   l = pos - st
   console(10,st,l) %if l # 0
   console(10,endseq,three)
   readsymbol(ch);  ! Wait for acknowledgement
   closesm(chan no)
   clear(chan id)
   printstring("** Host --")
   write(flen,0)
   printstring(" bytes transmitted")
   newline
   messages("on")
%end



%end %of %file