diff --git a/cobj/codegen.c b/cobj/codegen.c index 27d3edd5..99bc7b17 100644 --- a/cobj/codegen.c +++ b/cobj/codegen.c @@ -4012,13 +4012,20 @@ static void joutput_file_initialization(struct cb_file *f) { joutput_line (" byte_%s%s[i] = '0;'"); joutput_line ("}"); }*/ + if (f->external) { + joutput_line("%s%s = CobolFile.getExternalFile(\"%s\");", CB_PREFIX_FILE, + f->cname, f->cname); + joutput_line("if(%s%s == null) {", CB_PREFIX_FILE, f->cname); + joutput_indent_level += 2; + } joutput_line("%s%s = CobolFileFactory.makeCobolFileInstance(", CB_PREFIX_FILE, f->cname); joutput_line("/* select_name = */ \"%s\",", f->name); if (f->external && !f->file_status) { - joutput_line("/* file_status = */ cob_external_addr (\"%s%s_status\", 4),", - CB_PREFIX_FILE, f->cname); + joutput_line("/* file_status = */ CobolFile.getExternalFileStatus " + "(\"%s\"),", + f->cname); } else { joutput_line("/* file_status = */ %s%s_status,", CB_PREFIX_FILE, f->cname); } @@ -4076,26 +4083,15 @@ static void joutput_file_initialization(struct cb_file *f) { joutput_line(");"); if (f->external) { - // joutput_line ("%s%s = CobolExternal.getFileAddress (\"%s\");", - // CB_PREFIX_FILE, f->cname, f->cname); - joutput_line("if (CobolExternal.initialExternal)"); - joutput_indent("{"); - if (f->linage) { - joutput_line("%s%s.setLinorkeyptr(new Linage());", CB_PREFIX_FILE, - f->cname); - } - } else { - // joutput_line ("if (%s%s == null)", CB_PREFIX_FILE, f->cname); - // joutput_indent ("{"); - // joutput_line ("%s%s = new CobolFile();", CB_PREFIX_FILE, f->cname); - if (f->linage) { - joutput_line("%s%s.setLinorkeyptr(new Linage());", CB_PREFIX_FILE, - f->cname); - } - // joutput_indent ("}"); + joutput_line("CobolFile.putExternalFile(\"%s\", %s%s);", f->cname, + CB_PREFIX_FILE, f->cname); + joutput_indent_level -= 2; + joutput_line("}"); } if (f->linage) { + joutput_line("%s%s.setLinorkeyptr(new Linage());", CB_PREFIX_FILE, + f->cname); joutput_line("lingptr = (Linage)(%s%s.getLinorkeyptr());", CB_PREFIX_FILE, f->cname); joutput_prefix(); @@ -4135,9 +4131,6 @@ static void joutput_file_initialization(struct cb_file *f) { joutput_line("lingptr.setLinTop(0);"); joutput_line("lingptr.setLinBot(0);"); } - if (f->external) { - joutput_indent("}"); - } } /* diff --git a/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java b/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java index 3851ee49..a81cecd1 100755 --- a/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java +++ b/libcobj/src/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java @@ -29,7 +29,9 @@ import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import jp.osscons.opensourcecobol.libcobj.common.CobolModule; import jp.osscons.opensourcecobol.libcobj.common.CobolUtil; import jp.osscons.opensourcecobol.libcobj.data.AbstractCobolField; @@ -164,7 +166,7 @@ public class CobolFile { protected static String file_open_name; protected static byte[] file_open_buff = new byte[1024]; - protected static final String[] prefix = {"DD_", "dd_", ""}; + protected static final String[] prefix = { "DD_", "dd_", "" }; protected static final int NUM_PREFIX = prefix.length; protected static int eop_status = 0; @@ -173,16 +175,16 @@ public class CobolFile { private static List file_cache = new ArrayList(); protected static int[] status_exception = { - 0, - CobolExceptionId.COB_EC_I_O_AT_END, - CobolExceptionId.COB_EC_I_O_INVALID_KEY, - CobolExceptionId.COB_EC_I_O_PERMANENT_ERROR, - CobolExceptionId.COB_EC_I_O_LOGIC_ERROR, - CobolExceptionId.COB_EC_I_O_RECORD_OPERATION, - CobolExceptionId.COB_EC_I_O_FILE_SHARING, - CobolExceptionId.COB_EC_I_O, - CobolExceptionId.COB_EC_I_O, - CobolExceptionId.COB_EC_I_O_IMP + 0, + CobolExceptionId.COB_EC_I_O_AT_END, + CobolExceptionId.COB_EC_I_O_INVALID_KEY, + CobolExceptionId.COB_EC_I_O_PERMANENT_ERROR, + CobolExceptionId.COB_EC_I_O_LOGIC_ERROR, + CobolExceptionId.COB_EC_I_O_RECORD_OPERATION, + CobolExceptionId.COB_EC_I_O_FILE_SHARING, + CobolExceptionId.COB_EC_I_O, + CobolExceptionId.COB_EC_I_O, + CobolExceptionId.COB_EC_I_O_IMP }; public String select_name; public byte[] file_status; @@ -230,7 +232,8 @@ public void setLinorkeyptr(Linage ptr) { this.linorkeyptr = ptr; } - public CobolFile() {} + public CobolFile() { + } public CobolFile( String selectName, @@ -287,7 +290,8 @@ public CobolFile( } /** - * libcob/fileio.cのsave_statusの実装 RETURN_STATUSマクロは実装できないため,本メソッドの呼び出し後の次の文はreturn;を書くこと. + * libcob/fileio.cのsave_statusの実装 + * RETURN_STATUSマクロは実装できないため,本メソッドの呼び出し後の次の文はreturn;を書くこと. * * @param status * @param fnstatus @@ -351,8 +355,7 @@ protected boolean file_linage_check() { Linage lingptr = getLinorkeyptr(); lingptr.setLinLines(lingptr.getLinage().getInt()); - outer: - { + outer: { if (lingptr.getLinLines() < 1) { break outer; } @@ -765,25 +768,22 @@ public int open_(String filename, int mode, int sharing) throws IOException { fp = FileChannel.open(Paths.get(filename), StandardOpenOption.READ); break; case COB_OPEN_OUTPUT: - fp = - FileChannel.open( - Paths.get(filename), - StandardOpenOption.WRITE, - StandardOpenOption.CREATE, - StandardOpenOption.TRUNCATE_EXISTING); + fp = FileChannel.open( + Paths.get(filename), + StandardOpenOption.WRITE, + StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING); break; case COB_OPEN_I_O: - fp = - FileChannel.open( - Paths.get(filename), - StandardOpenOption.READ, - StandardOpenOption.WRITE, - StandardOpenOption.CREATE); + fp = FileChannel.open( + Paths.get(filename), + StandardOpenOption.READ, + StandardOpenOption.WRITE, + StandardOpenOption.CREATE); break; case COB_OPEN_EXTEND: - fp = - FileChannel.open( - Paths.get(filename), StandardOpenOption.APPEND, StandardOpenOption.CREATE); + fp = FileChannel.open( + Paths.get(filename), StandardOpenOption.APPEND, StandardOpenOption.CREATE); break; default: break; @@ -1073,8 +1073,7 @@ public void write(AbstractCobolField rec, int opt, AbstractCobolField fnstatus) } String openMode = openModeToString(this.last_open_mode); - if (invokeFun(COB_IO_WRITE, this, null, rec.getDataStorage(), fnstatus, openMode, null, null) - != 0) { + if (invokeFun(COB_IO_WRITE, this, null, rec.getDataStorage(), fnstatus, openMode, null, null) != 0) { return; } @@ -1532,4 +1531,27 @@ public byte[] getFileStatus() { // CobolFile cobolFile = new CobolFile(); return this.file_status; } + + private static Map externalFileStatusTable = new HashMap(); + + public static byte[] getExternalFileStatus(String key) { + byte[] bytes = externalFileStatusTable.get(key); + if (bytes == null) { + bytes = new byte[2]; + bytes[0] = 0; + bytes[1] = 0; + externalFileStatusTable.put(key, bytes); + } + return bytes; + } + + private static Map externalFileTable = new HashMap(); + + public static CobolFile getExternalFile(String key) { + return externalFileTable.get(key); + } + + public static CobolFile putExternalFile(String key, CobolFile value) { + return externalFileTable.put(key, value); + } } diff --git a/tests/Makefile.am b/tests/Makefile.am index 8da71112..b172a001 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -186,7 +186,8 @@ misc_DEPENDENCIES = \ misc.src/version-string-in-java.at \ misc.src/comp3-compute.at \ misc.src/index-file-status.at \ - misc.src/comp-n.at + misc.src/comp-n.at \ + misc.src/fd-external.at EXTRA_DIST = $(srcdir)/package.m4 \ diff --git a/tests/Makefile.in b/tests/Makefile.in index 5897a04b..6dd13cab 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -727,7 +727,8 @@ misc_DEPENDENCIES = \ misc.src/version-string-in-java.at \ misc.src/comp3-compute.at \ misc.src/index-file-status.at \ - misc.src/comp-n.at + misc.src/comp-n.at \ + misc.src/fd-external.at EXTRA_DIST = $(srcdir)/package.m4 \ $(TESTS) \ diff --git a/tests/misc.at b/tests/misc.at index af709743..39c0681b 100644 --- a/tests/misc.at +++ b/tests/misc.at @@ -38,4 +38,5 @@ m4_include([exit-perform-cycle.at]) m4_include([version-string-in-java.at]) m4_include([comp3-compute.at]) m4_include([index-file-status.at]) -m4_include([comp-n.at]) \ No newline at end of file +m4_include([comp-n.at]) +m4_include([fd-external.at]) diff --git a/tests/misc.src/fd-external.at b/tests/misc.src/fd-external.at new file mode 100644 index 00000000..78c01f25 --- /dev/null +++ b/tests/misc.src/fd-external.at @@ -0,0 +1,68 @@ +AT_SETUP([FD EXTERNAL]) + +AT_DATA([prog.cbl], [ + identification division. + program-id. prog. + environment division. + input-output section. + file-control. + select sequential-file assign to "sequential.txt" + organization is sequential + file status is sequential-file-status. + data division. + file section. + fd sequential-file is external. + 01 sequential-file-rec pic x(20). + working-storage section. + 01 sequential-file-status pic 99. + 01 command pic x(20). + procedure division. + main-rtn. + open output sequential-file. + move "seq-data" to sequential-file-rec. + move "write-sequential" to command. + call "sub" using command. + close sequential-file. + + open input sequential-file. + move space to sequential-file-rec. + move "read-sequential" to command. + call "sub" using command. + display "sequential-file-rec:" sequential-file-rec. + close sequential-file. + +]) + +AT_DATA([sub.cbl], [ + identification division. + program-id. sub. + environment division. + input-output section. + file-control. + select sequential-file assign to "sequential.txt" + organization is sequential + file status is sequential-file-status. + data division. + file section. + fd sequential-file is external. + 01 sequential-file-rec pic x(20). + working-storage section. + 01 sequential-file-status pic 99. + linkage section. + 01 command pic x(20). + procedure division using command. + main-rtn. + evaluate command + when "read-sequential" + read sequential-file + when "write-sequential" + write sequential-file-rec + end-evaluate. +]) + +AT_CHECK([${COBJ} prog.cbl sub.cbl]) +AT_CHECK([java prog], [0], +[sequential-file-rec:seq-data @&t@ +]) + +AT_CLEANUP