! Main program for IE / 4.2BSD UNIX

%externalstring(63) copyright %alias "MAIN_(C)_GDMR" %c
                              = "Copyright (C) 1985 George D M Ross"

%conststring(31) profile  variable = "IE_Profile"

%conststring(31) profile  path     = ".IE_Profile"

%conststring(31) memory   path     = ".IE_Memory"
%conststring(31) last     path     = ".IE_Last"

%conststring(31) tt type  variable = "IE_TT_type"        { Overrides TERM }

%conststring(47) keyboard root     = "/usr/local/IE/keyboards"
%conststring(31) keyboard variable = "IE_Keyboards"

%conststring(15) home     variable = "HOME"

%include "/usr/include/local/cliparse.inc"
!include cliparse

%include "ie.inc"
%include "terminal.inc"

%externalstring(255)%fnspec get env(%string(255) what)

%externalpredicatespec exists(%string(255) what)

%string(255)%fn next param
   %owninteger args = 0
   %owninteger this arg = 0
      args = argc %if args = 0
      this arg = this arg + 1
      %result = "" %if this arg >= args
      %result = get arg(this arg)
%end

%ownstring(127) in file = "", out file = ""

%constinteger last option = 6
%conststring(15)%array options(1 : last option) =
      "profile",
      "memory",
      "read",
      "create",
      "top",
      "last"

%integerfn action(%string(255) what)
   %string(15)%name option
   %integer which, i
      %if charno(what, 1) # '-' %start
         ! Must be a filename
         %if in file = "" %start
            %result = -2
         %else %if out file = ""
            %result = -1
         %else
            %result = 0
         %finish
      %finish
      %for which = 1, 1, last option %cycle
         option == options(which)
         -> no %if length(option) < length(what) - 1
         %for i = 2, 1, length(what) %cycle
            ! First is a '-', so skip it
            -> no %if charno(what, i) # charno(option, i - 1)
         %repeat
         %result = which
no:   %repeat
      %result = 0;  ! Not found
%end

%begin
   %string(255) home
   %string(255) profile = "", terminal = "", keyboards = ""
   %string(255) last file = ""
   %integer start line = 1, start column = 1
   %integer last start line, last start column
   %integer options
   %integer memory = 0, read only = 0, create file = 0
   %string(255) param
   %string(255) from memory = ""
   %switch act(-2 : last option)
      %on 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 %start
         reset terminal
         newline
         printstring("IE fails: ")
         printstring(event_message)
         newline
         %stop
      %finish

      home = get env(home variable)
      %if home = "" %start
         printstring("IE fails: cannot find HOME")
         newline
         %stop
      %finish

      %begin
         ! Read the memory file (if any)
         %on 3, 9 %start
            start line = 1
            start column = 1
            from memory = ""
            %return
         %finish
         open input(3, memory path)
         select input(3)
         read(start line)
         read(start column)
         read(from memory)
         close input
         select input(0)
      %end

      %begin
         ! Read the "last" file (if any)
         %on 3, 9 %start
            start line = 1
            start column = 1
            from memory = ""
            %return
         %finish
         open input(3, last path)
         select input(3)
         read(last start line)
         read(last start column)
         read(last file)
         close input
         select input(0)
      %end

      ! Parse the command line
      %cycle
         param = next param
         %exit %if param = ""
         -> act(action(param))

act(-2): in file = param
         start line = 1
         start column = 1
         %continue

act(-1): out file = param
         %continue

act(0):  printstring("IE fails: unknown option ")
         printstring(param)
         newline
         %stop

act(1):  ! Profile
         profile = next param
         %continue

act(2):  ! Memory
         memory = 1
         %continue
         
act(3):  ! Read
         read only = 1
         %continue

act(4):  ! Create
         create file = 1
         %continue

act(5):  ! Top
         start line = 1
         start column = 1
         %continue

act(6):  ! Last
         %if last file = "" %start
            printstring("IE fails: no last file")
            newline
            %stop
         %finish
         start line = last start line
         start column = last start column
         in file = last file
         %continue

      %repeat

      %if in file = "" %start
         %if memory # 0 %start
            printstring("IE fails: remember what?")
            newline
            %stop
         %finish
         %if from memory = "" %start
            printstring("IE fails: no file specified for editing")
            newline
            %stop
         %else %if create file # 0
            printstring("IE fails: cannot create from memory")
            newline
            %stop
         %finish
         in file = from memory
         memory = 1;  ! Force writeback
      %finish
      %if read only # 0 %start
         %if out file # "" %start
            printstring("IE fails: -read with output file?")
            newline
            %stop
         %else %if create file # 0
            printstring("IE fails: -read and -create?")
            newline
            %stop
         %finish
      %else
         %if create file # 0 %start
            %if out file # "" %start
               printstring("IE fails: -create with input and output files?")
               newline
               %stop
            %finish
            out file = in file
            in file = ""
         %else
            out file = in file %if out file = ""
         %finish
      %finish

      terminal = get env(tt type variable)
      terminal = get env("TERM") %if terminal = ""
      %if terminal = "vt220" %start
         terminal model = VT220
      %else %if terminal = "vt100"
         terminal model = VT100
      %else %if terminal = "vi200"
         terminal model = Visual 200
      %else %if terminal = "vi55"
         terminal model = Visual 55
      %else %if terminal = "wy75" %or %c
            (length(terminal) >= 6 %and substring(terminal, 1, 6) = "wyse75")
         terminal model = WY 75
         terminal = "wyse75"
      %else
         printstring("IE fails: can't drive a ")
         printstring(terminal)
         printstring(" (yet)")
         newline
         %stop
      %finish
      terminal width = 80
      keyboards = get env(keyboard variable)
      keyboards = keyboard root %if keyboards = ""
      %if profile = "" %start
         ! Wasn't explicitly set
         profile = get env(profile variable)
         %if profile = "" %start
            ! Still not defined.  Default it.
            %if exists(profile path) %start
               ! One in the current directory
               profile = profile path
            %else
               ! Not in current directlry -- try home directory
               profile = home . "/" . profile path
               profile = "" %unless exists(profile)
            %finish
         %finish
      %finish
      options = confirm

      set up terminal
      IE editor(in file, out file,
                0, 23,
                start line, start column,
                profile,
                keyboards . "/" . terminal,
                options) 
      reset terminal
      newline

      %if memory # 0 %and read only = 0 %start
         ! Don't remember read-only files
         %begin
            %on 3, 9 %start
               select output(0)
               printstring("IE fails: cannot write memory file")
               newline
               %return
            %finish
            open output(3, memory path)
            select output(3)
            write(start line, 1)
            write(start column, 1)
            space
            printstring(out file)
            newline
            close output
            select output(0)
         %end
      %finish
      %begin
         %on 3, 9 %start
            select output(0)
            printstring("IE fails: cannot write last file")
            newline
            %return
         %finish
         open output(3, last path)
         select output(3)
         write(start line, 1)
         write(start column, 1)
         space
         printstring(out file)
         newline
         close output
         select output(0)
      %end
%end %of %program
