Rem Start utility for programs v1.0 PD 2000.

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 start.exe calc.exe;edit.com"
   Color 7,0
   End
Endif

' locate swap program
If Dir$("shroom.com")="" Then
   Color 14,0
   Print "Error loading shroom."
   Color 7,0
   End
Endif

' 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$ = ""
   Endif
   Exec.Command$ = Ltrim$(Exec.Command$)
   Exec.Command$ = Rtrim$(Exec.Command$)
   If Exec.Command$ = "" Then
      Exit Do
   Endif

   ' 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
