Skip to content

Commit f027ab4

Browse files
Implement "SET ENVIRONMENT" statement (#72)
1 parent a5f1bf2 commit f027ab4

File tree

10 files changed

+101
-28
lines changed

10 files changed

+101
-28
lines changed

cobj/typeck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7623,7 +7623,7 @@ void cb_emit_search_all(cb_tree table, cb_tree at_end, cb_tree when, cb_tree stm
76237623

76247624
void cb_emit_setenv(cb_tree x, cb_tree y)
76257625
{
7626-
cb_emit(cb_build_funcall_2("CobolTerminal.setEnvironment", x, y));
7626+
cb_emit(cb_build_funcall_2("CobolUtil.setEnv", x, y));
76277627
}
76287628

76297629
void cb_emit_set_to(cb_tree vars, cb_tree x)

libcobj/src/jp/osscons/opensourcecobol/libcobj/call/CobolResolve.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Map.Entry;
3030
import java.util.UUID;
3131
import jp.osscons.opensourcecobol.libcobj.common.CobolConstant;
32+
import jp.osscons.opensourcecobol.libcobj.common.CobolUtil;
3233
import jp.osscons.opensourcecobol.libcobj.data.AbstractCobolField;
3334
import jp.osscons.opensourcecobol.libcobj.data.CobolDataStorage;
3435
import jp.osscons.opensourcecobol.libcobj.exceptions.CobolCallException;
@@ -72,7 +73,7 @@ public static void CobolInitCall() {
7273
// call_entry_buff = cob_malloc (COB_SMALL_BUFF);
7374
// call_entry2_buff = cob_malloc (COB_SMALL_BUFF);
7475

75-
s = System.getenv("COB_LOAD_CASE");
76+
s = CobolUtil.getEnv("COB_LOAD_CASE");
7677
if (s != null) {
7778
String sU = s.toUpperCase();
7879
if (sU.equals("LOWER")) {
@@ -82,7 +83,7 @@ public static void CobolInitCall() {
8283
}
8384
}
8485

85-
s = System.getenv("COB_LIBRARY_PATH");
86+
s = CobolUtil.getEnv("COB_LIBRARY_PATH");
8687
if (s == null || s.equals("")) {
8788
buf = "." + System.getProperty("path.separator") + CobolConstant.COB_LIBRARY_PATH;
8889
} else {
@@ -95,11 +96,11 @@ public static void CobolInitCall() {
9596
}
9697
setLibraryPath(buf);
9798

98-
s = System.getenv("COB_PACKAGE_PATH");
99+
s = CobolUtil.getEnv("COB_PACKAGE_PATH");
99100
setPackagePath(s);
100101

101102
// TODO プリロードの扱いを検討する
102-
s = System.getenv("COB_PRE_LOAD");
103+
s = CobolUtil.getEnv("COB_PRE_LOAD");
103104

104105
// 用途不明
105106
// call_buffer = cob_malloc (CALL_BUFF_SIZE);

libcobj/src/jp/osscons/opensourcecobol/libcobj/common/CobolUtil.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.time.DateTimeException;
2222
import java.time.LocalDateTime;
2323
import java.util.Calendar;
24+
import java.util.Properties;
2425
import java.util.Scanner;
2526
import java.util.regex.Matcher;
2627
import java.util.regex.Pattern;
@@ -76,6 +77,8 @@ abstract class handlerlist {
7677
public static final int FERROR_CHAINING = 2;
7778
public static final int FERROR_STACK = 3;
7879

80+
private static Properties envVarTable = new Properties();
81+
7982
/**
8083
* libcob/common.cのcob_check_envの実装
8184
*
@@ -88,7 +91,7 @@ public static int checkEnv(String name, String value) {
8891
return 0;
8992
}
9093

91-
String s = System.getenv(name);
94+
String s = CobolUtil.getEnv(name);
9295
if (s != null) {
9396
if (s.contentEquals(value)) {
9497
return 1;
@@ -108,10 +111,11 @@ public static void cob_init(String[] argv, boolean cob_initialized) {
108111
CobolInspect.initString();
109112
CobolFile.cob_init_fileio();
110113
CobolIntrinsic.init();
114+
CobolUtil.envVarTable = new Properties();
111115

112116
for (int i = 0; i < 8; ++i) {
113117
String envVariableName = String.format("COB_SWITCH_%d", i + 1);
114-
String envValue = System.getenv(envVariableName);
118+
String envValue = CobolUtil.getEnv(envVariableName);
115119
if (envValue == null) {
116120
CobolUtil.cobSwitch[i] = false;
117121
} else {
@@ -121,7 +125,7 @@ public static void cob_init(String[] argv, boolean cob_initialized) {
121125
}
122126

123127
cal = Calendar.getInstance();
124-
String s = System.getenv("COB_DATE");
128+
String s = CobolUtil.getEnv("COB_DATE");
125129
if (s != null) {
126130
Scanner scan = new Scanner(s);
127131
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) {
150154
}
151155
}
152156

153-
s = System.getenv("COB_VERBOSE");
157+
s = CobolUtil.getEnv("COB_VERBOSE");
154158
if (s != null && s.length() > 0 && (s.charAt(0) == 'y' || s.charAt(0) == 'Y')) {
155159
CobolUtil.cob_verbose = true;
156160
}
@@ -226,7 +230,7 @@ public static void runtimeError(String s) {
226230
* @param envval
227231
*/
228232
public static void getEnvironment(AbstractCobolField envname, AbstractCobolField envval) {
229-
String p = System.getenv(envname.fieldToString());
233+
String p = CobolUtil.getEnv(envname.fieldToString());
230234
if (p == null) {
231235
CobolException.setException(CobolExceptionId.COB_EC_IMP_ACCEPT);
232236
p = " ";
@@ -668,4 +672,34 @@ public static void setLocation(
668672
System.err.flush();
669673
}
670674
}
675+
676+
public static String getEnv(String envVarName) {
677+
String envVarInTable = CobolUtil.envVarTable.getProperty(envVarName);
678+
if (envVarInTable != null) {
679+
return envVarInTable;
680+
} else {
681+
return System.getenv(envVarName);
682+
}
683+
}
684+
685+
/**
686+
* get environemnt variable
687+
*
688+
* @param envVarName the name of an environment variable.
689+
* @return the value of envVarName, or null if the envVarName is not defined.
690+
*/
691+
public static void setEnv(String envVarName, String envVarValue) {
692+
CobolUtil.envVarTable.setProperty(envVarName, envVarValue);
693+
}
694+
695+
/**
696+
* Set environemnt variable
697+
*
698+
* @param envVarName the name of an environment variable. The leading and trailing spaces are
699+
* ignored.
700+
* @param envVarValue the value of an environment variable to be set.
701+
*/
702+
public static void setEnv(AbstractCobolField envVarName, AbstractCobolField envVarValue) {
703+
CobolUtil.envVarTable.setProperty(envVarName.getString().trim(), envVarValue.getString());
704+
}
671705
}

libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public static int invokeFun(
333333
byte[] tmpfnstatus = String.format("%02d", 0).getBytes();
334334
byte[] pTmpfnstatus = tmpfnstatus;
335335
String p_excpcode = "";
336-
String s = System.getenv(TIS_DEFINE_USERFH);
336+
String s = CobolUtil.getEnv(TIS_DEFINE_USERFH);
337337
int iRet = 0;
338338
char ret = '0';
339339
int status1 = 0;
@@ -645,7 +645,7 @@ public void open(int mode, int sharing, AbstractCobolField fnstatus) {
645645
file_open_env[j] = src[src_i + 1 + j];
646646
}
647647
file_open_env[i - 1] = 0;
648-
String p = System.getenv(new String(Arrays.copyOfRange(file_open_env, 0, i - 1)));
648+
String p = CobolUtil.getEnv(new String(Arrays.copyOfRange(file_open_env, 0, i - 1)));
649649
if (p != null) {
650650
byte[] pbytes = p.getBytes();
651651
for (int j = 0; j < pbytes.length; ++j) {
@@ -669,7 +669,7 @@ public void open(int mode, int sharing, AbstractCobolField fnstatus) {
669669
for (i = 0; i < NUM_PREFIX; i++) {
670670
byte[] file_open_buff = String.format("%s%s", prefix[i], file_open_name).getBytes();
671671
String p;
672-
if ((p = System.getenv(new String(file_open_buff))) != null) {
672+
if ((p = CobolUtil.getEnv(new String(file_open_buff))) != null) {
673673
file_open_name_bytes = p.getBytes();
674674
break;
675675
}
@@ -690,8 +690,8 @@ public void open(int mode, int sharing, AbstractCobolField fnstatus) {
690690
was_not_exist = true;
691691
if (mode != COB_OPEN_OUTPUT
692692
&& !this.flag_optional
693-
&& (mode != COB_OPEN_I_O || !System.getenv(COB_IO_CREATES).equals("yes"))
694-
&& (mode != COB_OPEN_EXTEND || !System.getenv(COB_EXTEND_CREATES).equals("yes"))) {
693+
&& (mode != COB_OPEN_I_O || !CobolUtil.getEnv(COB_IO_CREATES).equals("yes"))
694+
&& (mode != COB_OPEN_EXTEND || !CobolUtil.getEnv(COB_EXTEND_CREATES).equals("yes"))) {
695695
saveStatus(COB_STATUS_35_NOT_EXISTS, fnstatus);
696696
return;
697697
}
@@ -1319,7 +1319,7 @@ protected void cob_sync(CobolFile f, int mode) {
13191319

13201320
/** libcob/fileio.cのcob_init_fileioの実装 */
13211321
public static void cob_init_fileio() {
1322-
String s = System.getenv("COB_SYNC");
1322+
String s = CobolUtil.getEnv("COB_SYNC");
13231323
if (s != null) {
13241324
if (s.charAt(0) == 'Y' || s.charAt(0) == 'y') {
13251325
cob_do_sync = 1;
@@ -1329,15 +1329,15 @@ public static void cob_init_fileio() {
13291329
}
13301330
}
13311331

1332-
cob_file_path = System.getenv("COB_FILE_PATH");
1332+
cob_file_path = CobolUtil.getEnv("COB_FILE_PATH");
13331333
if (cob_file_path != null) {
13341334
if (cob_file_path.charAt(0) == '\0' || cob_file_path.charAt(0) == ' ') {
13351335
cob_file_path = null;
13361336
}
13371337
}
13381338

1339-
cob_ls_nulls = System.getenv("COB_LS_NULLS");
1340-
cob_ls_fixed = System.getenv("COB_LS_FIXED");
1339+
cob_ls_nulls = CobolUtil.getEnv("COB_LS_NULLS");
1340+
cob_ls_fixed = CobolUtil.getEnv("COB_LS_FIXED");
13411341

13421342
file_open_env = new byte[COB_SMALL_BUFF];
13431343
// file_open_name = new byte[COB_SMALL_BUFF];
@@ -1474,7 +1474,7 @@ public void cob_delete_file(AbstractCobolField fnstatus) {
14741474
file_open_env[j] = src[src_i + 1 + j];
14751475
}
14761476
file_open_env[i - 1] = 0;
1477-
String p = System.getenv(new String(Arrays.copyOfRange(file_open_env, 0, i - 1)));
1477+
String p = CobolUtil.getEnv(new String(Arrays.copyOfRange(file_open_env, 0, i - 1)));
14781478
if (p != null) {
14791479
byte[] pbytes = p.getBytes();
14801480
for (int j = 0; j < pbytes.length; ++j) {
@@ -1498,7 +1498,7 @@ public void cob_delete_file(AbstractCobolField fnstatus) {
14981498
for (i = 0; i < NUM_PREFIX; i++) {
14991499
byte[] file_open_buff = String.format("%s%s", prefix[i], file_open_name).getBytes();
15001500
String p;
1501-
if ((p = System.getenv(new String(file_open_buff))) != null) {
1501+
if ((p = CobolUtil.getEnv(new String(file_open_buff))) != null) {
15021502
file_open_name_bytes = p.getBytes();
15031503
break;
15041504
}

libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFileSort.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ private static CobolItem newItem(CobolSort hp) {
167167
private static FileIO tmpfile() {
168168
String s;
169169
FileIO fp = new FileIO();
170-
if ((s = System.getenv("TMPDIR")) == null
171-
&& (s = System.getenv("TMP")) == null
172-
&& (s = System.getenv("TEMP")) == null) {
170+
if ((s = CobolUtil.getEnv("TMPDIR")) == null
171+
&& (s = CobolUtil.getEnv("TMP")) == null
172+
&& (s = CobolUtil.getEnv("TEMP")) == null) {
173173
s = "/tmp";
174174
}
175175
if (cob_process_id.equals("")) {

libcobj/src/jp/osscons/opensourcecobol/libcobj/termio/CobolTerminal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public static void displayEnvValue(AbstractCobolField f) {
229229
public static void acceptEnvironment(AbstractCobolField f) {
230230
String p = null;
231231
if (CobolUtil.cobLocalEnv != null) {
232-
p = System.getenv(CobolUtil.cobLocalEnv);
232+
p = CobolUtil.getEnv(CobolUtil.cobLocalEnv);
233233
}
234234

235235
if (p == null) {

tests/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ misc_DEPENDENCIES = \
163163
misc.src/high-low-value.at \
164164
misc.src/move-sign-leading-separate-to-signed-comp3.at \
165165
misc.src/java-interface.at \
166-
misc.src/comp3-int.at
166+
misc.src/comp3-int.at \
167+
misc.src/env.at
167168

168169
EXTRA_DIST = $(srcdir)/package.m4 \
169170
$(TESTS) \

tests/Makefile.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,8 @@ misc_DEPENDENCIES = \
702702
misc.src/high-low-value.at \
703703
misc.src/move-sign-leading-separate-to-signed-comp3.at \
704704
misc.src/java-interface.at \
705-
misc.src/comp3-int.at
705+
misc.src/comp3-int.at \
706+
misc.src/env.at
706707

707708
EXTRA_DIST = $(srcdir)/package.m4 \
708709
$(TESTS) \

tests/misc.at

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ m4_include([comp3-is-numeric.at])
2424
m4_include([high-low-value.at])
2525
m4_include([move-sign-leading-separate-to-signed-comp3.at])
2626
m4_include([java-interface.at])
27-
m4_include([comp3-int.at])
27+
m4_include([comp3-int.at])
28+
m4_include([env.at])

tests/misc.src/env.at

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
AT_SETUP([Environment variable])
2+
3+
AT_DATA([prog.cbl],[
4+
IDENTIFICATION DIVISION.
5+
PROGRAM-ID. prog.
6+
DATA DIVISION.
7+
WORKING-STORAGE SECTION.
8+
01 env-var-name PIC X(14) value "COB_ENV_TEST".
9+
01 env-var-name1 PIC X(12) value "COB_ENV_TEST".
10+
01 env-var-value PIC X(10).
11+
PROCEDURE DIVISION.
12+
accept env-var-value from environment env-var-name
13+
end-accept.
14+
display env-var-value.
15+
16+
set environment env-var-name to "world".
17+
accept env-var-value from environment env-var-name
18+
end-accept.
19+
display env-var-value.
20+
21+
initialize env-var-value.
22+
set environment env-var-name1 to "world".
23+
accept env-var-value from environment env-var-name1
24+
end-accept.
25+
display env-var-value.
26+
])
27+
28+
AT_CHECK([${COBJ} prog.cbl])
29+
AT_CHECK([COB_ENV_TEST=hello java prog], [0],
30+
[hello @&t@
31+
world @&t@
32+
world @&t@
33+
])
34+
35+
AT_CLEANUP

0 commit comments

Comments
 (0)