diff --git a/cobj/typeck.c b/cobj/typeck.c index 0d75c0d0..1130c5da 100644 --- a/cobj/typeck.c +++ b/cobj/typeck.c @@ -7623,7 +7623,7 @@ void cb_emit_search_all(cb_tree table, cb_tree at_end, cb_tree when, cb_tree stm void cb_emit_setenv(cb_tree x, cb_tree y) { - cb_emit(cb_build_funcall_2("CobolTerminal.setEnvironment", x, y)); + cb_emit(cb_build_funcall_2("CobolUtil.setEnv", x, y)); } void cb_emit_set_to(cb_tree vars, cb_tree x) diff --git a/libcobj/src/jp/osscons/opensourcecobol/libcobj/call/CobolResolve.java b/libcobj/src/jp/osscons/opensourcecobol/libcobj/call/CobolResolve.java index 4bbe80fd..03358f53 100644 --- a/libcobj/src/jp/osscons/opensourcecobol/libcobj/call/CobolResolve.java +++ b/libcobj/src/jp/osscons/opensourcecobol/libcobj/call/CobolResolve.java @@ -29,6 +29,7 @@ import java.util.Map.Entry; import java.util.UUID; import jp.osscons.opensourcecobol.libcobj.common.CobolConstant; +import jp.osscons.opensourcecobol.libcobj.common.CobolUtil; import jp.osscons.opensourcecobol.libcobj.data.AbstractCobolField; import jp.osscons.opensourcecobol.libcobj.data.CobolDataStorage; import jp.osscons.opensourcecobol.libcobj.exceptions.CobolCallException; @@ -72,7 +73,7 @@ public static void CobolInitCall() { // call_entry_buff = cob_malloc (COB_SMALL_BUFF); // call_entry2_buff = cob_malloc (COB_SMALL_BUFF); - s = System.getenv("COB_LOAD_CASE"); + s = CobolUtil.getEnv("COB_LOAD_CASE"); if (s != null) { String sU = s.toUpperCase(); if (sU.equals("LOWER")) { @@ -82,7 +83,7 @@ public static void CobolInitCall() { } } - s = System.getenv("COB_LIBRARY_PATH"); + s = CobolUtil.getEnv("COB_LIBRARY_PATH"); if (s == null || s.equals("")) { buf = "." + System.getProperty("path.separator") + CobolConstant.COB_LIBRARY_PATH; } else { @@ -95,11 +96,11 @@ public static void CobolInitCall() { } setLibraryPath(buf); - s = System.getenv("COB_PACKAGE_PATH"); + s = CobolUtil.getEnv("COB_PACKAGE_PATH"); setPackagePath(s); // TODO プリロードの扱いを検討する - s = System.getenv("COB_PRE_LOAD"); + s = CobolUtil.getEnv("COB_PRE_LOAD"); // 用途不明 // call_buffer = cob_malloc (CALL_BUFF_SIZE); diff --git a/libcobj/src/jp/osscons/opensourcecobol/libcobj/common/CobolUtil.java b/libcobj/src/jp/osscons/opensourcecobol/libcobj/common/CobolUtil.java index 2c4d2eaf..244a2e71 100644 --- a/libcobj/src/jp/osscons/opensourcecobol/libcobj/common/CobolUtil.java +++ b/libcobj/src/jp/osscons/opensourcecobol/libcobj/common/CobolUtil.java @@ -21,6 +21,7 @@ import java.time.DateTimeException; import java.time.LocalDateTime; import java.util.Calendar; +import java.util.Properties; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -76,6 +77,8 @@ abstract class handlerlist { public static final int FERROR_CHAINING = 2; public static final int FERROR_STACK = 3; + private static Properties envVarTable = new Properties(); + /** * libcob/common.cのcob_check_envの実装 * @@ -88,7 +91,7 @@ public static int checkEnv(String name, String value) { return 0; } - String s = System.getenv(name); + String s = CobolUtil.getEnv(name); if (s != null) { if (s.contentEquals(value)) { return 1; @@ -108,10 +111,11 @@ public static void cob_init(String[] argv, boolean cob_initialized) { CobolInspect.initString(); CobolFile.cob_init_fileio(); CobolIntrinsic.init(); + CobolUtil.envVarTable = new Properties(); for (int i = 0; i < 8; ++i) { String envVariableName = String.format("COB_SWITCH_%d", i + 1); - String envValue = System.getenv(envVariableName); + String envValue = CobolUtil.getEnv(envVariableName); if (envValue == null) { CobolUtil.cobSwitch[i] = false; } else { @@ -121,7 +125,7 @@ public static void cob_init(String[] argv, boolean cob_initialized) { } cal = Calendar.getInstance(); - String s = System.getenv("COB_DATE"); + String s = CobolUtil.getEnv("COB_DATE"); if (s != null) { Scanner scan = new Scanner(s); Pattern p = Pattern.compile("([0-9]{4})/([0-9]{2})/([0-9]{2})"); @@ -150,7 +154,7 @@ public static void cob_init(String[] argv, boolean cob_initialized) { } } - s = System.getenv("COB_VERBOSE"); + s = CobolUtil.getEnv("COB_VERBOSE"); if (s != null && s.length() > 0 && (s.charAt(0) == 'y' || s.charAt(0) == 'Y')) { CobolUtil.cob_verbose = true; } @@ -226,7 +230,7 @@ public static void runtimeError(String s) { * @param envval */ public static void getEnvironment(AbstractCobolField envname, AbstractCobolField envval) { - String p = System.getenv(envname.fieldToString()); + String p = CobolUtil.getEnv(envname.fieldToString()); if (p == null) { CobolException.setException(CobolExceptionId.COB_EC_IMP_ACCEPT); p = " "; @@ -668,4 +672,34 @@ public static void setLocation( System.err.flush(); } } + + public static String getEnv(String envVarName) { + String envVarInTable = CobolUtil.envVarTable.getProperty(envVarName); + if (envVarInTable != null) { + return envVarInTable; + } else { + return System.getenv(envVarName); + } + } + + /** + * get environemnt variable + * + * @param envVarName the name of an environment variable. + * @return the value of envVarName, or null if the envVarName is not defined. + */ + public static void setEnv(String envVarName, String envVarValue) { + CobolUtil.envVarTable.setProperty(envVarName, envVarValue); + } + + /** + * Set environemnt variable + * + * @param envVarName the name of an environment variable. The leading and trailing spaces are + * ignored. + * @param envVarValue the value of an environment variable to be set. + */ + public static void setEnv(AbstractCobolField envVarName, AbstractCobolField envVarValue) { + CobolUtil.envVarTable.setProperty(envVarName.getString().trim(), envVarValue.getString()); + } } diff --git a/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java b/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java index f154d2e1..6cef8532 100644 --- a/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java +++ b/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java @@ -333,7 +333,7 @@ public static int invokeFun( byte[] tmpfnstatus = String.format("%02d", 0).getBytes(); byte[] pTmpfnstatus = tmpfnstatus; String p_excpcode = ""; - String s = System.getenv(TIS_DEFINE_USERFH); + String s = CobolUtil.getEnv(TIS_DEFINE_USERFH); int iRet = 0; char ret = '0'; int status1 = 0; @@ -645,7 +645,7 @@ public void open(int mode, int sharing, AbstractCobolField fnstatus) { file_open_env[j] = src[src_i + 1 + j]; } file_open_env[i - 1] = 0; - String p = System.getenv(new String(Arrays.copyOfRange(file_open_env, 0, i - 1))); + String p = CobolUtil.getEnv(new String(Arrays.copyOfRange(file_open_env, 0, i - 1))); if (p != null) { byte[] pbytes = p.getBytes(); for (int j = 0; j < pbytes.length; ++j) { @@ -669,7 +669,7 @@ public void open(int mode, int sharing, AbstractCobolField fnstatus) { for (i = 0; i < NUM_PREFIX; i++) { byte[] file_open_buff = String.format("%s%s", prefix[i], file_open_name).getBytes(); String p; - if ((p = System.getenv(new String(file_open_buff))) != null) { + if ((p = CobolUtil.getEnv(new String(file_open_buff))) != null) { file_open_name_bytes = p.getBytes(); break; } @@ -690,8 +690,8 @@ public void open(int mode, int sharing, AbstractCobolField fnstatus) { was_not_exist = true; if (mode != COB_OPEN_OUTPUT && !this.flag_optional - && (mode != COB_OPEN_I_O || !System.getenv(COB_IO_CREATES).equals("yes")) - && (mode != COB_OPEN_EXTEND || !System.getenv(COB_EXTEND_CREATES).equals("yes"))) { + && (mode != COB_OPEN_I_O || !CobolUtil.getEnv(COB_IO_CREATES).equals("yes")) + && (mode != COB_OPEN_EXTEND || !CobolUtil.getEnv(COB_EXTEND_CREATES).equals("yes"))) { saveStatus(COB_STATUS_35_NOT_EXISTS, fnstatus); return; } @@ -1319,7 +1319,7 @@ protected void cob_sync(CobolFile f, int mode) { /** libcob/fileio.cのcob_init_fileioの実装 */ public static void cob_init_fileio() { - String s = System.getenv("COB_SYNC"); + String s = CobolUtil.getEnv("COB_SYNC"); if (s != null) { if (s.charAt(0) == 'Y' || s.charAt(0) == 'y') { cob_do_sync = 1; @@ -1329,15 +1329,15 @@ public static void cob_init_fileio() { } } - cob_file_path = System.getenv("COB_FILE_PATH"); + cob_file_path = CobolUtil.getEnv("COB_FILE_PATH"); if (cob_file_path != null) { if (cob_file_path.charAt(0) == '\0' || cob_file_path.charAt(0) == ' ') { cob_file_path = null; } } - cob_ls_nulls = System.getenv("COB_LS_NULLS"); - cob_ls_fixed = System.getenv("COB_LS_FIXED"); + cob_ls_nulls = CobolUtil.getEnv("COB_LS_NULLS"); + cob_ls_fixed = CobolUtil.getEnv("COB_LS_FIXED"); file_open_env = new byte[COB_SMALL_BUFF]; // file_open_name = new byte[COB_SMALL_BUFF]; @@ -1474,7 +1474,7 @@ public void cob_delete_file(AbstractCobolField fnstatus) { file_open_env[j] = src[src_i + 1 + j]; } file_open_env[i - 1] = 0; - String p = System.getenv(new String(Arrays.copyOfRange(file_open_env, 0, i - 1))); + String p = CobolUtil.getEnv(new String(Arrays.copyOfRange(file_open_env, 0, i - 1))); if (p != null) { byte[] pbytes = p.getBytes(); for (int j = 0; j < pbytes.length; ++j) { @@ -1498,7 +1498,7 @@ public void cob_delete_file(AbstractCobolField fnstatus) { for (i = 0; i < NUM_PREFIX; i++) { byte[] file_open_buff = String.format("%s%s", prefix[i], file_open_name).getBytes(); String p; - if ((p = System.getenv(new String(file_open_buff))) != null) { + if ((p = CobolUtil.getEnv(new String(file_open_buff))) != null) { file_open_name_bytes = p.getBytes(); break; } diff --git a/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFileSort.java b/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFileSort.java index 13d6578f..4bbee6cd 100644 --- a/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFileSort.java +++ b/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFileSort.java @@ -167,9 +167,9 @@ private static CobolItem newItem(CobolSort hp) { private static FileIO tmpfile() { String s; FileIO fp = new FileIO(); - if ((s = System.getenv("TMPDIR")) == null - && (s = System.getenv("TMP")) == null - && (s = System.getenv("TEMP")) == null) { + if ((s = CobolUtil.getEnv("TMPDIR")) == null + && (s = CobolUtil.getEnv("TMP")) == null + && (s = CobolUtil.getEnv("TEMP")) == null) { s = "/tmp"; } if (cob_process_id.equals("")) { diff --git a/libcobj/src/jp/osscons/opensourcecobol/libcobj/termio/CobolTerminal.java b/libcobj/src/jp/osscons/opensourcecobol/libcobj/termio/CobolTerminal.java index 4e7049cf..e7da4e81 100644 --- a/libcobj/src/jp/osscons/opensourcecobol/libcobj/termio/CobolTerminal.java +++ b/libcobj/src/jp/osscons/opensourcecobol/libcobj/termio/CobolTerminal.java @@ -229,7 +229,7 @@ public static void displayEnvValue(AbstractCobolField f) { public static void acceptEnvironment(AbstractCobolField f) { String p = null; if (CobolUtil.cobLocalEnv != null) { - p = System.getenv(CobolUtil.cobLocalEnv); + p = CobolUtil.getEnv(CobolUtil.cobLocalEnv); } if (p == null) { diff --git a/tests/Makefile.am b/tests/Makefile.am index 512c6437..1a71b87a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -163,7 +163,8 @@ misc_DEPENDENCIES = \ misc.src/high-low-value.at \ misc.src/move-sign-leading-separate-to-signed-comp3.at \ misc.src/java-interface.at \ - misc.src/comp3-int.at + misc.src/comp3-int.at \ + misc.src/env.at EXTRA_DIST = $(srcdir)/package.m4 \ $(TESTS) \ diff --git a/tests/Makefile.in b/tests/Makefile.in index b8ce468a..7dc9475b 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -702,7 +702,8 @@ misc_DEPENDENCIES = \ misc.src/high-low-value.at \ misc.src/move-sign-leading-separate-to-signed-comp3.at \ misc.src/java-interface.at \ - misc.src/comp3-int.at + misc.src/comp3-int.at \ + misc.src/env.at EXTRA_DIST = $(srcdir)/package.m4 \ $(TESTS) \ diff --git a/tests/misc.at b/tests/misc.at index b31872a2..f42529d3 100644 --- a/tests/misc.at +++ b/tests/misc.at @@ -24,4 +24,5 @@ m4_include([comp3-is-numeric.at]) m4_include([high-low-value.at]) m4_include([move-sign-leading-separate-to-signed-comp3.at]) m4_include([java-interface.at]) -m4_include([comp3-int.at]) \ No newline at end of file +m4_include([comp3-int.at]) +m4_include([env.at]) diff --git a/tests/misc.src/env.at b/tests/misc.src/env.at new file mode 100644 index 00000000..f5768bdb --- /dev/null +++ b/tests/misc.src/env.at @@ -0,0 +1,35 @@ +AT_SETUP([Environment variable]) + +AT_DATA([prog.cbl],[ + IDENTIFICATION DIVISION. + PROGRAM-ID. prog. + DATA DIVISION. + WORKING-STORAGE SECTION. + 01 env-var-name PIC X(14) value "COB_ENV_TEST". + 01 env-var-name1 PIC X(12) value "COB_ENV_TEST". + 01 env-var-value PIC X(10). + PROCEDURE DIVISION. + accept env-var-value from environment env-var-name + end-accept. + display env-var-value. + + set environment env-var-name to "world". + accept env-var-value from environment env-var-name + end-accept. + display env-var-value. + + initialize env-var-value. + set environment env-var-name1 to "world". + accept env-var-value from environment env-var-name1 + end-accept. + display env-var-value. +]) + +AT_CHECK([${COBJ} prog.cbl]) +AT_CHECK([COB_ENV_TEST=hello java prog], [0], +[hello @&t@ +world @&t@ +world @&t@ +]) + +AT_CLEANUP