Added the Epilogue module
authorceriel <none@none>
Mon, 25 Jan 1988 16:14:48 +0000 (16:14 +0000)
committerceriel <none@none>
Mon, 25 Jan 1988 16:14:48 +0000 (16:14 +0000)
lang/m2/libm2/.distr
lang/m2/libm2/Epilogue.def [new file with mode: 0644]
lang/m2/libm2/Makefile
lang/m2/libm2/catch.c
lang/m2/libm2/halt.c
lang/m2/libm2/head_m2.e

index 5f441a0..dd1754c 100644 (file)
@@ -22,3 +22,4 @@ head_m2.e
 random.def
 Traps.def
 CSP.def
+Epilogue.def
diff --git a/lang/m2/libm2/Epilogue.def b/lang/m2/libm2/Epilogue.def
new file mode 100644 (file)
index 0000000..d6ba14f
--- /dev/null
@@ -0,0 +1,13 @@
+(*$Foreign*)
+DEFINITION MODULE Epilogue;
+(* MODULA-2 offers a facility for the initialization of modules, but there
+   is no mechanism to have some code executed when the program finishes.
+   This module is a feeble attempt at solving this problem.
+*)
+  PROCEDURE CallAtEnd(p: PROC);
+  (* Add procedure "p" to the list of procedures that must be executed when
+     the program finishes.
+     When the program finishes, these procedures are executed in the order in
+     which they were added to the list.
+  *)
+END Epilogue.
index af175bc..432029a 100644 (file)
@@ -5,7 +5,8 @@ SOURCES =       ASCII.def EM.def MathLib0.def Processes.def \
                RealInOut.def Storage.def Arguments.def Conversion.def \
                random.def Semaphores.def Unix.def RealConver.def \
                Strings.def InOut.def Terminal.def TTY.def \
-               Mathlib.def PascalIO.def Traps.def CSP.def
+               Mathlib.def PascalIO.def Traps.def CSP.def \
+               Epilogue.def
 
 all:
 
index 2e6a4ae..5014a2f 100644 (file)
@@ -35,6 +35,7 @@ static struct errm {
        { M2_NORESULT,  "no RETURN from procedure function"},
        { M2_UOVFL,     "cardinal overflow"},
        { M2_FORCH,     "Warning: FOR-loop control variable was changed in the body"},
+       { M2_ENDPROCS,  "too many procedures to be called on program termination"},
        { -1,           0}
 };
 
index 5a93c56..3c84979 100644 (file)
@@ -1,3 +1,29 @@
+#define MAXPROCS 20
+#include <m2_traps.h>
+
+static int callindex;
+static int (*proclist[MAXPROCS])();
+
+_cleanup()
+{
+       register int i;
+
+       for (i = 0; i < callindex; i++) {
+               (*proclist[i])();
+       }
+}
+
+CallAtEnd(p)
+       int (*p)();
+{
+       if (callindex >= MAXPROCS) {
+               TRP(M2_ENDPROCS);
+       }
+       else {
+               proclist[callindex++] = p;
+       }
+}
+
 _halt()
 {
        exit(0);
index 00b7f43..b990ddf 100644 (file)
@@ -91,6 +91,7 @@ mainroutine
  sig
  asp EM_PSIZE
  cal $_M2M
- loc 0
+ cal $_halt
+ loc 0                 ; should not get here
  ret EM_WSIZE
  end