(*----------------------------------------------------------------------*)
(*     Error_Handler --- Handle program aborts by exiting gracefully    *)
(*----------------------------------------------------------------------*)

PROCEDURE Error_Handler( ErrNo , ErrAddr : INTEGER );

(*----------------------------------------------------------------------*)
(*                                                                      *)
(*     Procedure:  Error_Handler                                        *)
(*                                                                      *)
(*     Purpose:    Handle PibTerm program aborts by halting gracefully  *)
(*                                                                      *)
(*     Calling Sequence:                                                *)
(*                                                                      *)
(*        Error_Handler( ErrNo , ErrAddr : INTEGER );                   *)
(*                                                                      *)
(*           ErrNo    --- error number/type                             *)
(*           ErrAddr  --- address where error occurred                  *)
(*                                                                      *)
(*     Remarks:                                                         *)
(*                                                                      *)
(*        This routine needs to be cleaned up to handle file closing    *)
(*        better at some point.                                         *)
(*                                                                      *)
(*----------------------------------------------------------------------*)

BEGIN (* Error_Handler *)
                                   (* Send notice of fatal error (also to  *)
                                   (* caller in host mode) and quit.       *)
   IF Host_Mode THEN
      BEGIN
         Async_Send_String(' ');
         Async_Send_String('Sorry, there has been a system error, please call back later');
         Async_Send_String('Disconnecting....');
         WRITELN;
         WRITELN('Sorry, there has been a system error, please call back later');
         WRITELN('Disconnecting....');
      END
   ELSE
      BEGIN
         WRITELN;
         WRITELN('Sorry, there has been a serious PibTerm error.');
      END;

                                   (* Report error number and type *)

   WRITE('Error number: ',LO( ErrNo ), ' = ' );

   CASE HI( ErrNo ) OF
      0: WRITE('User break');
      1: WRITE('I/O error');
      2: WRITE('Run time error');
      ELSE
         WRITE('Unknown Error');
   END (* CASE *);

   WRITELN(' at address ',ErrAddr,' (decimal)');

                                   (* Turn off serial port I/O     *)
   Async_Close;
                                   (* Turn off fatal error handler *)
   Int24Off;
                                   (* Quit                         *)

   WRITELN('PibTerm aborting ... ');

   HALT( 2 );

END   (* Error_Handler *);
