! EDWIN Utility procedures for DEFINICON MS-DOS

%from Edwin %include Device, Consts, Icodes
%from Imp %include Lognames, Sys Specs, Text Utils

%record %format Dev Fm ( %integer Channel, %string(255) Device )

%own %record(Dev Fm) %name Device == 0
%own %short Plot Stream = 0
%own %string(255) Buffer = ""

%external %routine TT Mode %alias "EDWIN_TTMODE" ( %integer M )
   %if M > 0 %and Dev Data_Dev No = HP Plotter %and Viewing = 0 %start
      Plot Stream = 7
   %else
      Plot Stream = 0
   %finish
%end

%external %predicate Plotter Device %alias "EDWIN_PLOTTER_DEVICE" -
   (%string (255) Name)
   Name = "COM2" %if Name = ""
   Name = Translate (Name) %if Name -> ("_")
   To Upper (name)
   %true %if length(Name) = 4 %and Sub string (Name,1,3) = "COM"
   %false
%end

%external %routine Set Device %alias "EDWIN_SET_DEVICE" (%string(255) Dev Name)
   %integer Stream = Output Stream

!   Oper Message ("dev name is |".dev name."|".snl)
   Dev Name = "COM2" %if Dev Name = ""
   Dev Name = Translate (Dev Name) %if Dev Name -> ("_")
   %if Dev Name -> ("_") %start
       Oper Message ("Error opening graphics device ".dev name.nl)
       %signal 14,1
   %finish
   %if Device == Nil %start
      Device == New(Device) ; Device = 0
   %finish
   Device_Device = Dev Name
   Device_Channel = -1 { ie. not really a device }
   %begin
      %on 9 %start
         Oper Message ("Error opening file ".dev name." for graphics output")
         %signal 14,1
      %finish
      To Upper (Dev name)
      %if Plotter Device (Dev Name) %start
         Plot Stream = 7
         Open Output (Plot Stream, dev name)
      %else
         Viewing = 3
         %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
      %finish
      Select Output (Stream)
   %end
%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 Plot Stream # 0 %and Viewing = 0 %start
         ! The need for also checking Viewing is if people plot to a real
         ! device and then to a file within the same run of the program, ug!
         print symbol (10)
         Close Output
         Open Output (Plot Stream, device_device)
      %finish
      select output (Stream)
      Buffer = ""
   %finish
%end

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

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

%external %integer %function %spec SCR TT Get %alias "TTGet"
%external %integer %function TT Get %alias "EDWIN_TTGET"
   %result = SCR TT Get
%end

%external %integer %function TT Read %alias "EDWIN_TTREAD"
   %own %integer Count = 1
   Count = Count + 1
   %if Count&1 = 1 %start
      %result = '?'
   %finish
   %result = 10
%end

%integer %function Opt Read
   %integer Sym = 0
   %integer Status = Read(Device_Channel, Byte(Addr(Sym)), 1)
   %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
   ttput (27);   ttput (cursor);   flush output
   Set Device ("") %if Device == Nil
!!!   Set Cbreak (1);   Set Echo (1)
   A = opt read;   B = opt read;   C = opt read;   D = opt read;   E = opt read
   F = opt read
!!!   Set Cbreak (0)
   X = (B&31)<<5!C&31
   Y = (D&31)<<5!E&31
%end

%external %integer %fn MUL DIV %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
