save and restore MODULE and MODULE_DIR after running a script, because that script may have called require

This commit is contained in:
2018-07-04 15:06:45 +02:00
parent 19971d6cf1
commit 90348614d5
2 changed files with 16 additions and 1 deletions
+1
View File
@@ -638,6 +638,7 @@ void registerModule(const char* module, const char* version, const char* locatio
putenvprintf("%s_VERSION=%s", module, version);
if (location)
{
putenvprintf("MODULE_DIR=%s", m->content+lm+lv);
putenvprintf("%s_DIR=%s", module, m->content+lm+lv);
pathAdd("SCRIPT_PATH", m->content+lm+lv);
}
+15 -1
View File
@@ -50,6 +50,9 @@ epicsShareFunc int epicsShareAPI iocshCmd(const char *cmd);
#include "expr.h"
#include "require.h"
#define SAVEENV(var) do { old_##var = getenv(#var); if (old_##var) old_##var=strdup(old_##var); } while(0)
#define RESTOREENV(var) do { if(old_##var) { putenvprintf("%s=%s", #var, old_##var); free(old_##var); }} while(0)
int runScriptDebug=0;
int runScript(const char* filename, const char* args)
@@ -63,6 +66,8 @@ int runScript(const char* filename, const char* args)
long len;
char** pairs;
int status = 0;
char* old_MODULE = NULL;
char* old_MODULE_DIR = NULL;
if (!filename)
{
@@ -161,8 +166,12 @@ int runScript(const char* filename, const char* args)
}
}
if (file == NULL) { perror(filename); return errno; }
/* save some environments variables */
SAVEENV(MODULE);
SAVEENV(MODULE_DIR);
/* line by line after expanding macros with arguments or environment */
/* execute script line by line after expanding macros with arguments or environment */
while (fgets(line_raw, line_raw_size, file))
{
char* p, *x;
@@ -246,6 +255,11 @@ end:
free(line_exp);
if (mac) macDeleteHandle(mac);
if (file) fclose(file);
/* restore environment */
RESTOREENV(MODULE);
RESTOREENV(MODULE_DIR);
return status;
}