unit uExit;

      {=================================================================}
      {                                                                 }
      {  Obsluha UEXIT                                                  }
      {                                                                 }
      {  Zaverecne nastaveni datovych struktur a hardware               }
      {                                                                 }
      {  (C) SofCon s.r.o.,                                             }
      {                                                                 }
      {=================================================================}

{$I Sets.inc}

interface

procedure Exit;

{-----------------------------------------------------}
implementation
uses
  Kernel,
{$ifdef VerMc}
  RtcTime,
{$else}
  Dos,
{$endif}
  LocGlb,
  NumToStr;

procedure Exit; { EXIT bez o.s. ReTOS }
begin
{$ifdef VerPc}
  Rewrite(F,1);
  BlockWrite(F,Glb^,SizeOf(tGlb));
{$endif}
end;

var
  SavePointer : Pointer;
  X           : word;
  PomSe       : string[10];

{$F+}
procedure MyExit;
{$F-}
begin
  ExitProc:=SavePointer;
  if ExitCode<>0 then
  begin
    if Glb^.gRStruct.ARst>MaxRst then
      Glb^.gRStruct.ARst:=0;
    with  Glb^.gRStruct,Rst[ARst],rTime do
    begin
       rErr :=ExitCode;
       rAddr:=ErrorAddr;
       GetDate(Year,Month,Day,X);
       GetTime(Hour,Min,Sec,X);
       if ARst>=MaxRst
       then ARst:=0
       else Inc(ARst);
    end;
{$ifdef DebError}
    PomSe:=WordStrDec(ExitCode,3);
    WriteLn('Error = '+PomSe+', Addr = '+WordStrHex(Seg(ErrorAddr^))+':'+WordStrHex(Ofs(ErrorAddr^)));
{$endif}


{$ifdef DebStop}
    repeat
    until false;
{$endif}
  end;
end;

begin
  SavePointer:=ExitProc;
  ExitProc:=@MyExit;
end.