From 1e90f4e927ab7aab5ae5ad5acf8c2a68c67ad923 Mon Sep 17 00:00:00 2001
From: Simon Rose <simon.rose@ess.eu>
Date: Mon, 15 May 2023 10:13:57 +0200
Subject: [PATCH] Remove superfluous static firstTime checks

There are two changes here:
* The initHookRegistrar was being run when initially when require registers
  itself. But this is just telling it to run `fillModuleListRecord` later,
  which seems cleaner to do as a part of registering require instead of
  indirectly. Note that the debug check can never actually happen as the
  variable `requireDebug` will always be 0 at this point in time.
* The environment variables being set is slightly different, in the sense
  that if you did not `require foo` at all you did not set these environment
  variables at all. However, it seems to me to make more sense to either set
  them independent of loading a module, or perhaps not at all; either which
  way it seems odd that it depended on us loading a module.
---
 require-ess/src/require.c | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)

diff --git a/require-ess/src/require.c b/require-ess/src/require.c
index 79844332..acc94ae5 100644
--- a/require-ess/src/require.c
+++ b/require-ess/src/require.c
@@ -121,7 +121,11 @@ void set_require_env() {
   sprintf(epicsRelease, "%s.%s.%s", epics_version_major, epics_version_middle,
           epics_version_minor);
   targetArch = getenv("EPICS_HOST_ARCH");
-  return;
+
+  putenvprintf("T_A=%s", targetArch);
+  putenvprintf("EPICS_HOST_ARCH=%s", targetArch);
+  putenvprintf("EPICS_RELEASE=%s", epicsRelease);
+  putenvprintf("OS_CLASS=%s", osClass);
 }
 
 static HMODULE loadlib(const char *libname) {
@@ -375,16 +379,9 @@ void registerModule(const char *module, const char *version,
   char *absLocationRequire = NULL;
   char *argstring = NULL;
   const char *mylocation;
-  static int firstTime = 1;
 
   debug("require: registerModule(%s,%s,%s)\n", module, version, location);
 
-  if (firstTime) {
-    initHookRegister(fillModuleListRecord);
-    debug("require: initHookRegister\n");
-    firstTime = 0;
-  }
-
   if (!version) version = "";
 
   if (location) {
@@ -706,18 +703,6 @@ static int require_priv(const char *module, const char *version);
 
 int require(const char *module, const char *version) {
   int status;
-  static int firstTime = 1;
-
-  if (firstTime) {
-    firstTime = 0;
-
-    set_require_env();
-
-    putenvprintf("T_A=%s", targetArch);
-    putenvprintf("EPICS_HOST_ARCH=%s", targetArch);
-    putenvprintf("EPICS_RELEASE=%s", epicsRelease);
-    putenvprintf("OS_CLASS=%s", osClass);
-  }
 
   if (module == NULL) {
     printf("Usage: require \"<module>\" [, \"<version>\" ]\n");
@@ -1197,6 +1182,9 @@ static void requireRegister(void) {
     iocshRegister(&ldDef, ldFunc);
     iocshRegister(&pathAddDef, pathAddFunc);
     registerExternalModules();
+
+    set_require_env();
+    initHookRegister(fillModuleListRecord);
   }
 }
 
-- 
GitLab