REM Start utility for programs v1.0 PD 2006.

REM This program runs a program using shroom.

REM Shroom copies the program to a area file when
REM  it shells to another program.

REM This program is swapped out of memory when
REM  it calls shroom.

REM $DYNAMIC
DEFINT A-Z

' define structures
TYPE RegTypeX
 AX AS INTEGER
 BX AS INTEGER
 CX AS INTEGER
 DX AS INTEGER
 BP AS INTEGER
 SI AS INTEGER
 DI AS INTEGER
 Flags AS INTEGER
 DS AS INTEGER
 ES AS INTEGER
END TYPE

TYPE SwapPackType
 StatExec AS INTEGER
 ProgName AS STRING * 128
 CmdLine AS STRING * 126
 SaveFile AS STRING * 128
END TYPE

' declare functions
DECLARE FUNCTION Swapbas% (SEG SwapPack AS ANY)
DECLARE SUB InterruptX (N AS INTEGER, I AS RegTypeX, O AS RegTypeX)

' standard error trap
ON ERROR GOTO Error0

' declare structures
DIM Swapit AS SwapPackType, InregsX AS RegTypeX, OutregsX AS RegTypeX

' read command line from psp
Command.Line$ = ""
InregsX.AX = &H6200
CALL InterruptX(&H21, InregsX, OutregsX)
PSPsegment = OutregsX.BX
PSPoffset = 128
DEF SEG = PSPsegment
FOR Count = 1 TO 127
   Char = PEEK(PSPoffset + Count)
   SELECT CASE Char
   CASE 0, 10, 13
      EXIT FOR
   CASE ELSE
      Command.Line$ = Command.Line$ + CHR$(Char)
   END SELECT
NEXT
DEF SEG

' check command line
IF Command.Line$ = "" THEN
   COLOR 15, 0
   PRINT "Runprog utility for progams usage:"
   COLOR 14, 0
   PRINT "   Runprog <commands>"
   COLOR 15, 0
   PRINT "Where:"
   COLOR 14, 0
   PRINT "   <commands> are the programs"
   PRINT "   to run separated by semicolons."
   COLOR 15, 0
   PRINT "Example:"
   COLOR 14, 0
   PRINT "   Runprog calc.exe;edit.com"
   COLOR 7, 0
   END
END IF

' locate swap program
IF DIR$("shroom.com") = "" THEN
   COLOR 14, 0
   PRINT "Error loading shroom."
   COLOR 7, 0
   END
END IF

' loop through all commands
Comspec$ = ENVIRON$("COMSPEC")
Comspec$ = RTRIM$(Comspec$)
Current.Dir$ = CURDIR$
DO
   Exec.Command$ = ""
   Parse = INSTR(Command.Line$, ";")
   IF Parse THEN
      Exec.Command$ = LEFT$(Command.Line$, Parse - 1)
      Command.Line$ = MID$(Command.Line$, Parse + 1)
   ELSE
      Exec.Command$ = Command.Line$
      Command.Line$ = ""
   END IF
   Exec.Command$ = LTRIM$(Exec.Command$)
   Exec.Command$ = RTRIM$(Exec.Command$)
   IF Exec.Command$ = "" THEN
      EXIT DO
   END IF

   ' store command line
   Exec.Command.Line$ = "/c shroom.com -q " + Exec.Command$

   ' store swap structure
   Swapit.StatExec = 0
   Swapit.ProgName = Comspec$ + CHR$(0)
   Swapit.CmdLine = Exec.Command.Line$ + CHR$(0)
   Swap.Count = 0
   DO
      Filename$ = "PROG" + RIGHT$(STR$(Swap.Count + 10000), 4) + ".SWP"
      IF DIR$(Filename$) = "" THEN
         EXIT DO
      END IF
      Swap.Count = Swap.Count + 1
      IF Swap.Count > 9999 THEN
         PRINT "Error assigning swap filename."
         EXIT DO
      END IF
   LOOP
   IF Swap.Count > 9999 THEN
      EXIT DO
   END IF
   Swapit.SaveFile = Filename$ + CHR$(0)

   ' call swap program
   RetCode% = Swapbas(Swapit)

   'display any errors
   'IF RetCode% THEN
   '   PRINT "SWAPBAS error code:";RetCode%
   'ELSE
   '   IF SWAPIT.statEXEC THEN
   '      PRINT "EXEC PROGRAM return code:";SWAPIT.statEXEC
   '   ELSE
   '      PRINT "All appears to be successful!"
   '   ENDIF
   'ENDIF
LOOP
CHDRIVE Current.Dir$
CHDIR Current.Dir$
END

' standard error trap
Error0:
 COLOR 14, 0
 PRINT "Error loading program"; ERR; "."
 COLOR 7, 0
 END
