{Main program calling section for IE on APM}

%include "IE.INC"
%include "Terminal.Inc"

%include "Inc:UTIL.Imp"
!%include "Inc:DICT.Imp"

%begin
   %own %string (255) In File = "",
                      Preload List = "",
                      Out File = "",
                      Def Profile = "",
                      Profile File = "",
                      Keyboard File = ""
   %string (255) Orig Keyboard File
   !@ 16_3FA0 %byte rows, cols
   %constinteger rows = 24, cols = 80

   %integer SL = 1,
            SP = 1,
            IE Bits = 0,
            Memory Used = 0
   %constant %integer Memory     Flag = 2_10000000000000000000000000000000,
                      Last       Flag = 2_01000000000000000000000000000000,
                      Top        Flag = 2_00100000000000000000000000000000,
                      Save       Flag = 2_00010000000000000000000000000000,
                      Read       Flag = 2_00001000000000000000000000000000,
                      Create     Flag = 2_00000100000000000000000000000000,
                      Confirm    Flag = 2_00000010000000000000000000000000,
                      Silent     Flag = 2_00000001000000000000000000000000,
                      Recover    Flag = 2_00000000100000000000000000000000,
                      Ignore     Flag = 2_00000000010000000000000000000000,
                      Adopt      Flag = 2_00000000001000000000000000000000,
                      No Profile Flag = 2_00000000000100000000000000000000

   %constant %integer Default Flags = Confirm Flag!Save Flag

   %own %integer Bits = Default Flags

   %record %format IE Stuff fm (%string (127) Memory,
                                              Last,
                                              Journal File,
                                              Profile File,
                                              Keyboard File,
                                %byte Terminal,
                                %integer Line, Position)

   %record(IE stuff fm) TEMP IE STUFF

   %record (IE Stuff fm) %name IE Stuff
   %integer Stuff At

   %on 9, Abort %start
      {Event 9 comes from PAM}
      select input(0);  select output(0)
      %if Event_Event = Abort %start
         Reset Terminal
         %if Event_Message = "" %start
            Event_Message = "(to provide an error report)" 
         %finish
         Print String ("IE fails: " . Event_Message)
      %finish
      New Line
      %signal 0, 1;  ! IMN_trap
   %finish

   %routine Include Buffers {A quick GMC hack}
     %string(255) next

     %routine add buffer (%string(255)%name next)
        %record(Preload Fm)%name pF

        %return %if next = "" %or next = " "

        %if %not exists(next) %start
           %signal abort, 0, 0, "Preload file ".next." not found" 
        %finish

        pF == nil
        pF == new(pF)
        pF_Buffer Name = next
        pF_File Name = next

        %if Preloads == nil %start
          Preloads == pF
          Preloads_next == nil
        %else
          pF_next == Preloads
          Preloads == pF
        %finish
     %end

     Preloads == nil

     %if Preload List # "" %start

       next = ""
       %while Preload List -> next.(" ").Preload List %cycle
         add buffer(next)
       %repeat

       add buffer(Preload List)
     %finish

   %end

   select input(0);  select output(0)

   !Stuff At = Ref Name ("IE_Stuff", Sys Dict)
   !%if Stuff At = 0 %start
   !   Stuff At = Def Name("IE_Stuff", Sys Dict, Size Of (IE Stuff))
   !   IE Stuff == Record (Stuff At)
   !   IE Stuff =  0
   !%else
   !  IE Stuff == Record (Stuff At)
   !%finish
   IE stuff == TEMP IE stuff;  IE stuff = 0

   Define Param ("INPUT",
                 In File,
                 PAM Major ! PAM KeepCase)

   Define Param ("PREload list -- list of files for inclusion as buffers",
                 Preload List,
                 0)

   Define Param ("OUTPUT",
                 Out File,
                 PAM New Group ! PAM Major ! PAM KeepCase)

   Def Profile = Default Profile
   Profile File = Def Profile

   Define Param ("Profile",
                 Profile File,
                 PAM KeepCase)

   Journal File = Default Journal
   Define Param ("Journal",
                 Journal File,
                 PAM KeepCase)

   Define Boolean Params ( %c
"Memory,Last,Top,SAve_position,Read,CReate,COnfirm,SIlent,RECover,IGNore,ADOPT,NOProfile",
                          Bits,
                          0)

   Terminal Model = Default Terminal
   Define Enum Param ("V200,V55,VT100,VT220,Freedom100,WY50,WY75",
                      Terminal Model,
                      PAM MAjor)

   Terminal Width = Cols
   Define Int Param ("Width",
                     Terminal Width,
                     0)

   Orig Keyboard File = Default Keyboard
   Keyboard File = Orig Keyboard File
   Define Param ("Keyboard",
                 Keyboard File,
                 PAM KeepCase)

   Process Parameters (CLI Param)

   %if Bits&Last Flag # 0 %start
      %if in file # "" %start
         Print String (" Input file and " . To String (PAM_Key Flag) . "LAST?")
         New Line
         %stop
      %else
         %if IE Stuff_Last = "" %start
            Print String ("No Last File")
            New Line
            %stop
         %finish
         In File = IE Stuff_Last
      %finish
   %else
      %if in file = "" %start
         %if IE Stuff_Memory = "" %start
            Print String ("No Memory - please give file name")
            New Line
            %stop
         %finish
         In File = IE Stuff_Memory
         %if IE Stuff_Line # 0 %start
            SL = IE Stuff_Line
            SP = IE Stuff_Position
         %finish
         %if Bits&Top Flag # 0 %start
            SL = 1
            SP = 1
         %finish
         Memory Used = 1
      %finish
   %finish

   out file = in file %if out file = ""

   out file = "" %if Bits&Read Flag # 0
   in file  = "" %if Bits&Create Flag # 0

   %if out file = "" = in file %start
      Print String (To String (PAM_Key Flag) . "READ and " . -
                    To String (PAM_Key Flag) . "CREATE?")
      New Line
      %stop
   %finish

   IE Bits = IE Bits!Confirm %if Bits&Confirm Flag # 0
   IE Bits = IE Bits!Silent  %if Bits&Silent  Flag # 0
   IE Bits = IE Bits!Recover Edit %if Bits&Recover Flag # 0
   IE Bits = IE Bits!Ignore Differences %if Bits&Ignore Flag # 0
   IE Bits = IE Bits!Adopt Defaults %if Bits&Adopt Flag # 0

   %if Keyboard File = Orig Keyboard File %and %c
       Terminal Model # Visual 200 %start
      Keyboard File = Default Keyboard
   %finish

   IE Stuff_Journal File = Journal File
   %if Profile File # Def Profile %start
      {in the case the qualifier -Profile=... has been used to set it}
      IE Stuff_Profile File = Profile File
   %finish
   IE Stuff_Terminal = Terminal Model
   IE Stuff_Keyboard File = Keyboard File

   Profile File = "" %if Bits&NoProfile Flag # 0

   Set Up Terminal

   Include Buffers

   !! printstring("About to call IE: ");  printstring(in file)
   !! space;  printstring(out file)
   !! space;  printstring(profile file)
   !! space;  printstring(keyboard file)
   !! print symbol(13);  newline
   IE Editor (In File, Out File,
              0, Rows - 1,
              SL, SP,
              Profile File,
              Keyboard File,
              IE Bits)
   Reset Terminal

   %if Bits&Memory Flag # 0 %start
      IE Stuff_Memory = Out File
      Memory Used = 1
   %finish
   %if Bits&Save Flag # 0 %and -
       Memory Used # 0%start
      IE Stuff_Line = SL
      IE Stuff_Position = SP
   %finish
   IE Stuff_Last = Out File

   Select Output (0)
   New Line
%end

%end %of %file
