From e5b9c564af9e927a62c71dffb31ffdcf9c4dae9b Mon Sep 17 00:00:00 2001 From: ceriel Date: Mon, 25 Jan 1988 16:14:48 +0000 Subject: [PATCH] Added the Epilogue module --- lang/m2/libm2/.distr | 1 + lang/m2/libm2/Epilogue.def | 13 +++++++++++++ lang/m2/libm2/Makefile | 3 ++- lang/m2/libm2/catch.c | 1 + lang/m2/libm2/halt.c | 26 ++++++++++++++++++++++++++ lang/m2/libm2/head_m2.e | 3 ++- 6 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 lang/m2/libm2/Epilogue.def diff --git a/lang/m2/libm2/.distr b/lang/m2/libm2/.distr index 5f441a030..dd1754cef 100644 --- a/lang/m2/libm2/.distr +++ b/lang/m2/libm2/.distr @@ -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 index 000000000..d6ba14f37 --- /dev/null +++ b/lang/m2/libm2/Epilogue.def @@ -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. diff --git a/lang/m2/libm2/Makefile b/lang/m2/libm2/Makefile index af175bcb7..432029aba 100644 --- a/lang/m2/libm2/Makefile +++ b/lang/m2/libm2/Makefile @@ -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: diff --git a/lang/m2/libm2/catch.c b/lang/m2/libm2/catch.c index 2e6a4ae6d..5014a2fd3 100644 --- a/lang/m2/libm2/catch.c +++ b/lang/m2/libm2/catch.c @@ -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} }; diff --git a/lang/m2/libm2/halt.c b/lang/m2/libm2/halt.c index 5a93c56e4..3c8497957 100644 --- a/lang/m2/libm2/halt.c +++ b/lang/m2/libm2/halt.c @@ -1,3 +1,29 @@ +#define MAXPROCS 20 +#include + +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); diff --git a/lang/m2/libm2/head_m2.e b/lang/m2/libm2/head_m2.e index 00b7f431d..b990ddfbd 100644 --- a/lang/m2/libm2/head_m2.e +++ b/lang/m2/libm2/head_m2.e @@ -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