home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD1.bin / useful / dev / obero / oberon-a / source / library / errors.mod < prev    next >
Encoding:
Text File  |  1995-03-08  |  5.8 KB  |  207 lines

  1. (***************************************************************************
  2.  
  3.      $RCSfile: Errors.mod $
  4.   Description: Error handling and reporting
  5.  
  6.    Created by: fjc (Frank Copeland)
  7.     $Revision: 1.12 $
  8.       $Author: fjc $
  9.         $Date: 1995/03/08 20:35:51 $
  10.  
  11.   Copyright © 1994, Frank Copeland.
  12.   This file is part of the Oberon-A Library.
  13.   See Oberon-A.doc for conditions of use and distribution.
  14.  
  15. ***************************************************************************)
  16.  
  17. <* STANDARD- *> <* MAIN- *> <*$ LongVars+ *> <*$ NilChk- *>
  18.  
  19. MODULE Errors;
  20.  
  21. IMPORT SYS := SYSTEM, Kernel, e := Exec, d := Dos, i := Intuition;
  22.  
  23. CONST
  24.  
  25.   (* Error codes understood by this module. *)
  26.  
  27.   (*
  28.   noMemory *       =  95;
  29.   invariant *      =  96;
  30.   *)
  31.   preCondition *   =  97;
  32.   postCondition *  =  98;
  33.   notImplemented * =  99;
  34.   noLibrary *      = 100;
  35.  
  36. VAR
  37.   Traps : ARRAY 26 OF e.LSTRPTR;
  38.  
  39. CONST
  40.   Title = "Oberon-A Error Handler";
  41.  
  42.  
  43. (*------------------------------------*)
  44. PROCEDURE* Report (msg1, msg2 : ARRAY OF CHAR);
  45.  
  46.   VAR
  47.     output : d.FileHandlePtr; es : i.EasyStruct;
  48.     msg3 : ARRAY 80 OF CHAR;
  49.  
  50.   (*------------------------------------*)
  51.   PROCEDURE WriteStr (str : ARRAY OF CHAR);
  52.   <*$CopyArrays-*>
  53.   BEGIN (* WriteStr *)
  54.     SYS.PUTREG (0, d.Write (output, str, SYS.STRLEN (str)))
  55.   END WriteStr;
  56.  
  57.   (*------------------------------------*)
  58.   PROCEDURE WriteLn;
  59.   BEGIN (* WriteLn *)
  60.     SYS.PUTREG (0, d.Write (output, "\n", 1))
  61.   END WriteLn;
  62.  
  63. <*$CopyArrays-*>
  64. BEGIN (* Report *)
  65.   IF i.base.libNode.version >= 37 THEN
  66.     es.structSize := SIZE (i.EasyStruct);
  67.     es.flags := {};
  68.     es.title := SYS.ADR (Title);
  69.     es.gadgetFormat := SYS.ADR ("Halt program");
  70.     IF Kernel.errModule = "" THEN
  71.       es.textFormat := SYS.ADR ("%s\n%s");
  72.       IF i.EasyRequest
  73.         ( NIL, SYS.ADR (es), NIL,
  74.           SYS.ADR (msg1), SYS.ADR (msg2),
  75.           NIL ) = 0
  76.       THEN
  77.       END
  78.     ELSE
  79.       es.textFormat := SYS.ADR ("%s\nModule %s, line %ld, col %ld\n%s");
  80.       IF i.EasyRequest
  81.         ( NIL, SYS.ADR (es), NIL,
  82.           SYS.ADR (msg1),
  83.           SYS.ADR (Kernel.errModule), Kernel.errLine, Kernel.errCol,
  84.           SYS.ADR (msg2),
  85.           NIL ) = 0
  86.       THEN
  87.       END
  88.     END;
  89.   ELSE
  90.     output := d.Output();
  91.     IF output # NIL THEN
  92.       WriteStr (Title); WriteLn;
  93.       WriteStr (msg1); WriteLn;
  94.       WriteStr (msg2); WriteLn;
  95.     END
  96.   END
  97. END Report;
  98.  
  99.  
  100. (*------------------------------------*)
  101. PROCEDURE Abort * (msg : ARRAY OF CHAR);
  102.  
  103. <*$CopyArrays-*>
  104. BEGIN (* Abort *)
  105.   Report ("Errors.Abort", msg);
  106.   HALT (20)
  107. END Abort;
  108.  
  109.  
  110. (*------------------------------------*)
  111. PROCEDURE Assert * (condition : BOOLEAN; msg : ARRAY OF CHAR);
  112.  
  113. <*$CopyArrays-*>
  114. BEGIN (* Assert *)
  115.   IF ~condition THEN
  116.     Report ("Errors.Assert", msg);
  117.     HALT (20)
  118.   END
  119. END Assert;
  120.  
  121. (*------------------------------------*)
  122. PROCEDURE* PutCh ();
  123.  
  124. <*$EntryExitCode-*>
  125. BEGIN (* PutCh *)
  126.   SYS.INLINE (16C0H,   (* MOVE.B D0,(A3)+ *)
  127.               4E75H)   (* RTS             *)
  128. END PutCh;
  129.  
  130. <*$LongVars-*>
  131.  
  132. (*------------------------------------*)
  133. PROCEDURE* ReportRC (VAR rc : LONGINT);
  134.  
  135.   CONST RunTimeError = "Run-time error detected";
  136.  
  137.   VAR line2 : e.LSTRPTR; str : ARRAY 80 OF CHAR;
  138.  
  139. BEGIN (* ReportRC *)
  140.   Kernel.RemoveTrapHandler();
  141.   IF (rc > 20) THEN
  142.     IF rc = 21 THEN
  143.       line2 := SYS.ADR ("Freeing unallocated memory")
  144.     ELSIF rc = 22 THEN
  145.       line2 := SYS.ADR ("Not enough memory")
  146.     ELSIF rc = 23 THEN
  147.       line2 := SYS.ADR ("Could not allocate user traps")
  148.     ELSIF rc = 95 THEN
  149.       line2 := SYS.ADR ("Not enough memory")
  150.     ELSIF rc = 96 THEN
  151.       line2 := SYS.ADR ("Invariant violated")
  152.     ELSIF rc = 97 THEN
  153.       line2 := SYS.ADR ("Pre-condition not satisfied")
  154.     ELSIF rc = 98 THEN
  155.       line2 := SYS.ADR ("Post-condition not satisfied")
  156.     ELSIF rc = 99 THEN
  157.       line2 := SYS.ADR ("Procedure or method not implemented")
  158.     ELSIF rc = 100 THEN
  159.       line2 := SYS.ADR ("Failed to open shared library")
  160.     ELSIF (rc >= 102) & (rc <= 111) THEN
  161.       line2 := Traps [rc - 102]
  162.     ELSIF (rc >= 132) & (rc <= 147) THEN
  163.       line2 := Traps [rc - 122]
  164.     ELSE
  165.       line2 := SYS.ADR (str);
  166.       e.OldRawDoFmtL ("Error code = %ld", rc, PutCh, line2);
  167.     END;
  168.     Report (RunTimeError, line2^)
  169.   END;
  170. END ReportRC;
  171.  
  172.  
  173. PROCEDURE Init*;
  174. BEGIN (* Init *)
  175.   Traps [0]  := SYS.ADR ("Trap #2 : Bus Error");
  176.   Traps [1]  := SYS.ADR ("Trap #3 : Address Error");
  177.   Traps [2]  := SYS.ADR ("Trap #4 : Illegal Instruction");
  178.   Traps [3]  := SYS.ADR ("Trap #5 : Divide by zero");
  179.   Traps [4]  := SYS.ADR ("Trap #6 : CHK instruction");
  180.   Traps [5]  := SYS.ADR ("Trap #7 : TRAPV instruction");
  181.   Traps [6]  := SYS.ADR ("Trap #8 : Privilege violation");
  182.   Traps [7]  := SYS.ADR ("Trap #9 : Trace bit trap");
  183.   Traps [8]  := SYS.ADR ("Trap #10 : Line 1010 emulator");
  184.   Traps [9]  := SYS.ADR ("Trap #11 : Line 1111 emulator");
  185.   Traps [10] := SYS.ADR ("Trap #32 : Compiler index check failed");
  186.   Traps [11] := SYS.ADR ("Trap #33 : Compiler type check failed");
  187.   Traps [12] := SYS.ADR ("Trap #34 : Compiler NIL check failed");
  188.   Traps [13] := SYS.ADR ("Trap #35 : Compiler case check failed");
  189.   Traps [14] := SYS.ADR ("Trap #36 : RETURN missing in function");
  190.   Traps [15] := SYS.ADR ("Trap #37 : Compiler stack check failed");
  191.   Traps [16] := SYS.ADR ("Trap #38 : Overflow/range check failed");
  192.   Traps [17] := SYS.ADR ("Trap #39 : Unspecified user trap");
  193.   Traps [18] := SYS.ADR ("Trap #40 : Unspecified user trap");
  194.   Traps [19] := SYS.ADR ("Trap #41 : Unspecified user trap");
  195.   Traps [20] := SYS.ADR ("Trap #42 : Unspecified user trap");
  196.   Traps [21] := SYS.ADR ("Trap #43 : Unspecified user trap");
  197.   Traps [22] := SYS.ADR ("Trap #44 : Unspecified user trap");
  198.   Traps [23] := SYS.ADR ("Trap #45 : Unspecified user trap");
  199.   Traps [24] := SYS.ADR ("Trap #46 : Unspecified user trap");
  200.   Traps [25] := SYS.ADR ("Trap #47 : Unspecified user trap");
  201.   Kernel.InstallTrapHandler();
  202.   Kernel.SetCleanup (ReportRC);
  203. END Init;
  204.  
  205. END Errors.
  206.  
  207.