{***********************************************************************}
{									}
{ __ExitRPgm  Terminate the process and remain resident 		}
{									}
{ procedure __ExitRPgm(ErrorLevel : byte; HeapSize : longint);		}
{									}
{	      ErrorLevel The exit code.  This value may be inspected by }
{			 the parent process, or can inspected at the	}
{			 DOS command level using the ErrorLevel value.	}
{	      HeapSize	 The size of the heap desired for the memory	}
{			 resident application.	Specifying a negative	}
{			 value, causes the value _MinHeap in the	}
{			 global record structure _ExecMemory to be	}
{			 used.						}
{									}
{ Description __ExitRPgm terminates the current process returning con-	}
{	      trol to the parent process.  The exit code, which can be	}
{	      be inspected by the parent process (see __WaitPgm), is	}
{	      set to the value in ErrorLevel. Unlike __ExitPgm, the	}
{	      program remains resident as a part of DOS.  So that the	}
{	      entire program is saved in memory, the size of the	}
{	      desired heap for the program must also be specified.	}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc.  1987		}
{_______________________________________________________________________}

procedure __ExitRPgm(ErrorLevel : byte; HeapSize : longint);

  var
    NewMaxAvail  : longint;
    NewHeapSize  : longint;
    CurHeapSize  : longint;
    FreeSize	 : word;
    ErrorCode	 : word;

  begin {__ExitRPgm}

    { If the HeapSize is negative, the smaller of value specified in	}
    { _ExecMemory and the current heap size is chosen.			}

    if (HeapSize < 0) then
       NewHeapSize := _ExecMemory._MinHeap
    else
       NewHeapSize := HeapSize;
    CurHeapSize := __TopHpMem(FreeSize);
    if (CurHeapSize < NewHeapSize) then
       NewHeapSize := CurHeapSize;

    __HeapMem(NewHeapSize,NewMaxAvail,ErrorCode);

    { Now restore the vectors Turbo Pascal has set; they are stored	}
    { in global variables so that they can be reset if needed.		}

    GetIntVec($00,_CurVector00);       { Save the interrupt vectors	}
    GetIntVec($02,_CurVector02);       { which Turbo Pascal has altered.}
    GetIntVec($1B,_CurVector1B);
    GetIntVec($23,_CurVector23);
    GetIntVec($24,_CurVector24);
    GetIntVec($75,_CurVector75);

    SetIntVec($00,SaveInt00);	       { Set the original values. DOS	}
    SetIntVec($02,SaveInt02);	       { automatically restores $23 and }
    SetIntVec($75,SaveInt75);	       { and $24.			}

    Keep(ErrorLevel)		       { Terminate but stay resident	}

  end;	{__ExitRPgm}
