! PLAUTILS : a set of utilities shared by the ILAP PLA interface routines and programs.

%external %routine %spec disaster %alias "ILAP_DISASTER" (%string (255) REASON)
!%external %integer %fn %spec IN STREAM
!%external %integer %fn %spec OUT STREAM
!%external %string (127) %fn %spec IN FILE NAME
!%external %string (127) %fn %spec OUT FILE NAME
!%external %routine %spec PROMPT (%string (63) str)
!%external %routine %spec SET DEFAULT (%string (5) EXT)
%const %string (3) NULL = "NL:"      { VAX="NL:", EMAS=".NULL", APM=":N" }
%const %string (3) TTIN = "TT:"      { VAX="TT:", EMAS=".IN",   APM=":T" }
%const %string (3) TTOUT = "TT:"     { VAX="TT:", EMAS=".OUT",  APM=":T" }
%const %byte EXT SEP = '.'           { EMAS='#', others='.'

{#########################################################################}
{#                                                                       #}
{#      This program is part of the ILAP library, and was written in     #}
{#   The Department of Computer Science at the University of Edinburgh   #}
{#       (James Clerk Maxwell Building, Kings Buildings, Edinburgh)      #}
{#                                                                       #}
{#   This software is available free to other educational establisments  #}
{#   but the University of Edinburgh, retains all commercial rights.     #}
{#   It is a condition of having this software is that the sources are   #}
{#   not passed on to any other site, and that Edinburgh University is   #}
{#   given credit in any re-implementations of any of the algorithms     #}
{#   used, or articles published which refer to the software.            #}
{#                                                                       #}
{#   There is no formal support for this software, but any bugs should   #}
{#   be reported to Gordon Hughes or David Rees at the above address,    #}
{#   and these are likely to be fixed in a future release.               #}
{#                                                                       #}
{#########################################################################}

%routine find streams ( %integer %name outsperrs, insperrs )
   ! Just use stream 2, these will be 'pushed' in a later release of the APM OS.
   outsperrs = 2
   insperrs = 2
!  %integer stream, old
!  old = outstream
!  %for stream = 15, -1, 0 %cycle
!    select output (stream)
!    %if outfilename = NULL %then outsperrs = stream %and %exit
!  %repeat
!  %if stream = 0 %start;  !unsuccessful
!    disaster ("output streams are all used")
!  %finish
!  select output (old)
!  old = instream
!  %for stream = 15, -1, 0 %cycle
!    select input (stream)
!    %if infilename = NULL %then insperrs = stream %and %exit
!  %repeat
!  %if stream = 0 %start; !unsuccessful
!     disaster ("input streams are all used")
!  %finish
!  select input (old)
%end

!
! general purpose filespec parser
!

%external %routine  parse filespec %alias "ILAP_PARSE_FILESPEC" %c
   ( %string(255) c, ext1, ext2 , %string(*)%name fin, fout, %integer %name inn, outn )
  %integer oldin
  %integer oldout

  %on 9 %start
    disaster (event_message)
  %finish

  %routine oread string ( %string(*)%name s )
    %integer ch
    s = ""
    read symbol(ch) %until ch # ' '
    %while ch # nl %cycle
      s = s.to string (ch)
      read symbol(ch)
    %repeat
  %end

  old in = in stream
  old out = out stream
  %if c="" %start
      select input(0)
      prompt("$_file: ")
      oread string(c)
  %finish
  find streams( inn, outn )
  !
  ! do quick and dirty parse
  !
  %if c -> fin.("/").fout %or c -> fin.(" ").fout %or c -> fin.(",").fout %start
    fin = "" %if fin = TTIN
    fout = "" %if fout = TTOUT
  %finish %else %if ext2#"" %start
    fin = c
    fout = c
    %if fout -> ("]").fout %start; %finish
    %if fout -> (":").fout %start; %finish
    %if fout -> ("_").fout %start; %finish
!   %if fout -> (".").fout %start; %finish   { *** EMAS *** }
    %if fout -> fout.(".") %start; %finish   { *** VAX *** }
    %if fout -> fout.("#") %start; %finish
  %finish %else %start
    fin = c
    fout = ""
  %finish

  ext1 = to string (ext sep).ext1 %if ext1#"" %and charno(ext1,1)#ext sep
  %if fin # "" %start
!    set default (ext1)
fin = fin.ext1 %unless fin -> (".")
    open input( inn, fin )
    select input(inn)
!    fin = in file name
  %finish %else %start
    fin = TT IN
    inn = 0
  %finish

  ext2 = to string (ext sep).ext2 %if ext2#"" %and charno(ext2,1)#ext sep
  %if fout # "" %start
!    set default (ext2)
    fout = fout.ext2 %unless fout -> (".")
    open output( outn, fout)
    select output(outn)
!    fout = out file name
  %finish %else %start
    fout = TT OUT
    outn = 0
  %finish

  select input(oldin)
  select output(oldout)
%end

%external %routine read to terminator %alias "ILAP_READ_TO_TERMINATOR" %c
   ( %string(*)%name s , %integer %name last )
   %integer ch
   read symbol(ch) %until ch >= ' '
   s = ""
   %while ch # ' ' %and ch # ',' %and ch # nl %cycle
      ch = ch + 'a' - 'A' %if 'A' <= ch <= 'Z'
      s = s.to string (ch)
      read symbol(ch)
   %repeat
   last = ch
%end

%end %of %file
