! EDWIN Utility procedures for the Apollo running AEGIS

%from Imp %include Formats, Lognames
%external %routine %spec SIO CONTROL %alias "SIO_$CONTROL" %c
   (%short %name STRID, OPTION, %integer %name VAL ADR, STATUS)
%from Edwin %include Device, Consts

%own %short %integer sio input sync = 7
%own %short %integer sio flush out  = 32
%own %integer true = -1
%own %short out handle
%own %string(255) Buffer = "", Last Dev = ""
%own %short Plot Stream = 0

%external %routine TT Mode %alias "EDWIN_TTMODE" ( %integer M )
%end

%external %predicate Plotter Device %alias "EDWIN_PLOTTER_DEVICE" -
   (%string (255) Name)
   %if Name # "" %start
      Name = Translate (Name) %if Name -> ("_")
      %true %if length(Name)>4 %and substring(Name, 1, 4)="/dev"
   %finish
   %false
%end

%external %routine Set Device %alias "EDWIN_SET_DEVICE" (%string(255) Dev Name)
   %integer ist, ost, status
   Viewing = 0
   Plot Stream = 0
   %if Dev Name # "" %start
      Dev Name = Translate (Dev Name) %if Dev Name -> ("_")
      %unless Plotter Device (Dev Name) %start
         %begin
            %on 9 %start
               Oper Message ("Error opening file ".dev name." for graphics output")
               %signal 14,1
            %finish
            Viewing = 3 { since this is not the console! }
            ost = Output Stream
            %if Dev Data_Dev No = HP Plotter %start
               Dev Name = Dev Name.".hp" %unless Dev Name -> (".")
               Open Output (Viewing, Dev Name)
            %else %if Dev Data_Dev No = Dev Bitmap %and Dev Data_Type = 300
               Dev Name = Dev Name.".lis" %unless Dev Name -> (".")
               Open Output (Viewing, Dev Name)
            %else %if Dev Data_Dev No = Postscript
               Dev Name = Dev Name.".ps" %unless Dev Name -> (".")
               Open Output (Viewing, Dev Name)
            %else
               Dev Name = Dev Name.".bin" %unless Dev Name -> (".")
               Open Binary Output (Viewing, Dev Name)
            %finish
            select output (ost)
         %end
         %return
      %finish
      Plot Stream = 7
      Last Dev = Dev Name
      ist = input stream
      ost = output stream
      open output (7, Dev Name)
      open input (7, Dev Name)
      select output (7)
      out handle = Out Scb_handle
      sio control (out handle, sio input sync, true, status)
      select output (ost)
      select input (ist)
   %finish
%end

%external %routine Flush Output %alias "EDWIN_FLUSH"
   %integer I, status, Stream
   %if Buffer # "" %start
      Stream = Output Stream
      %if Viewing # 0 %then Select Output (Viewing) -
                      %else Select Output (plot stream)
      print string (buffer)
      %if Viewing = 0 %and Plot Stream # 0 %start
         print symbol (10)
         Close Output
         Open Output (Plot Stream, last dev)
      %finish
      select output (Stream)
      Buffer = ""
   %finish
%end

%routine Flush
   %integer I = Output Stream
   %if Viewing # 0 %then Select Output (Viewing) -
                   %else Select Output (plot stream)
   print string (buffer)
   select output (i)
   Buffer = ""
%end

%external %routine TT Put %alias "EDWIN_TTPUT" ( %integer Char )
   Flush %if Length(Buffer) = 255
   Buffer = Buffer . Char
%end

%external %integer %function TT Get %alias "EDWIN_TTGET"
   %integer Sym = 0
   %result = Sym
%end

%external %integer %function TT Read %alias "EDWIN_TTREAD"
   %integer Sym = 0, IS = Input Stream
   Select Input (Plot Stream)
   Read Symbol (Sym)
   Select Input (Is)
   %result = Sym
%end

%external %routine OPER MESSAGE %alias "EDWIN_OPER_MESSAGE" (%string (255) S)
   %integer OS
   OS = Output Stream
   Select output (0)
   Print string (S)
   Newline
   Select output (OS)
%end

%external %routine OPER INTERACT %alias "EDWIN_OPER_INTERACT" (%string (255) S)
   %string (127) OLD PROMPT = "Data: "
   %integer IS, REPLY
   %on 9 %start
       Prompt (OLD PROMPT)
       %signal 9
   %finish
   IS = Input Stream
   Select input (0)
!   OLD PROMPT = CURRENT PROMPT
   Prompt (S)
   Read symbol (REPLY) %until REPLY&95='Y'
   Read symbol (REPLY) %until REPLY=NL
   Select input (IS)
   Prompt (OLD PROMPT)
%end

%external %routine TEK INPUT %alias "EDWIN___TEK_INPUT" (%integer %name A, X, Y, %integer cursor)
   %integer b, c, d, e, f, os
   ttput (27);   ttput (cursor);   flush output
   os = Input Stream
   select Input (0)
   read(A);   read(B);   read(C);   read(D);   read(E)
   read (F) %until F <' '
   Select Input (os)
   X = (B&31)<<5!C&31
   Y = (D&31)<<5!E&31
%end

%external %integer %fn muldiv %alias "EDWIN___MUL_DIV" (%integer A, B, C)
   %result = 0 %if A=0 %or B=0 %or C=0
   %result = Round (Float(A)*Float(B)/C)
%end

%end %of %file
