Skip to content

Commit b420a19

Browse files
Implement the system routine C$SLEEP (#669)
* feat: add C$SLEEP * refactor: suppress a PMD warning * fix: remove error message of C$SLEEP * fix: CobolSystemRoutine.C$SLEEP * test: add a test of C$SLEEP
1 parent 80eedf5 commit b420a19

File tree

7 files changed

+82
-4
lines changed

7 files changed

+82
-4
lines changed

cobj/system.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ COB_SYSTEM_GEN ("C$JUSTIFY", 1, cob_acuw_justify)
6161
COB_SYSTEM_GEN ("C$CALLEDBY", 1, calledBy)
6262
COB_SYSTEM_GEN ("C$MAKEDIR", 1, cob_acuw_mkdir)
6363
COB_SYSTEM_GEN ("C$NARG", 1, cob_return_args)
64-
COB_SYSTEM_GEN ("C$SLEEP", 1, cob_acuw_sleep)
64+
COB_SYSTEM_GEN ("C$SLEEP", 1, C$SLEEP)
6565
COB_SYSTEM_GEN ("C$PARAMSIZE", 1, cob_parameter_size)
6666
COB_SYSTEM_GEN ("C$TOUPPER", 2, CBL_TOUPPER)
6767
COB_SYSTEM_GEN ("C$TOLOWER", 2, CBL_TOLOWER)

cobj/typeck.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,6 @@ void cb_emit_call(cb_tree prog, cb_tree cb_using, cb_tree returning,
31303130
case C$JUSTIFY:
31313131
case C$MAKEDIR:
31323132
case C$NARG:
3133-
case C$SLEEP:
31343133
case C$PARAMSIZE:
31353134
cb_error(_("%s not implemented"), data);
31363135
return;

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/call/CobolSystemRoutine.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,29 @@ public static int SYSTEM(AbstractCobolField cmd) {
135135
return SYSTEM(cmd.getString());
136136
}
137137

138+
/**
139+
* 組み込み関数すうC$SLEEPの実装
140+
* opensource COBOLのlibcob/common.cのcob_acuw_sleep関数に相当する
141+
*
142+
* @param data C$SLEEPの引数として指定されたCOBOL変数のバイト列。
143+
*/
144+
@SuppressWarnings("PMD.AvoidDollarSigns")
145+
public static int C$SLEEP(CobolDataStorage data) {
146+
CobolUtil.COB_CHK_PARMS("C$SLEEP", 3);
147+
List<AbstractCobolField> params = CobolModule.getCurrentModule().cob_procedure_parameters;
148+
if (!params.isEmpty() && params.get(0) != null) {
149+
int n = params.get(0).getInt();
150+
if (n > 0 && n < 3600 * 24 * 7) {
151+
try {
152+
Thread.sleep(n * 1000L);
153+
} catch (InterruptedException e) {
154+
return 0;
155+
}
156+
}
157+
}
158+
return 0;
159+
}
160+
138161
private interface Calculater {
139162
byte calc(byte b1, byte b2);
140163
}

tests/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ run_DEPENDENCIES = \
7373
run.src/subscripts.at \
7474
run.src/extensions.at \
7575
run.src/return-code.at \
76-
run.src/functions.at
76+
run.src/functions.at \
77+
run.src/system-routines.at
7778

7879
data_rep_DEPENDENCIES = \
7980
data-rep.at \

tests/Makefile.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,8 @@ run_DEPENDENCIES = \
612612
run.src/subscripts.at \
613613
run.src/extensions.at \
614614
run.src/return-code.at \
615-
run.src/functions.at
615+
run.src/functions.at \
616+
run.src/system-routines.at
616617

617618
data_rep_DEPENDENCIES = \
618619
data-rep.at \

tests/run.at

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ m4_include([miscellaneous.at])
3333
m4_include([extensions.at])
3434
m4_include([return-code.at])
3535
m4_include([functions.at])
36+
m4_include([system-routines.at])

tests/run.src/system-routines.at

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
AT_SETUP([C$SLEEP])
2+
3+
AT_DATA([prog1.cbl], [
4+
IDENTIFICATION DIVISION.
5+
PROGRAM-ID. prog1.
6+
PROCEDURE DIVISION.
7+
DISPLAY "Start".
8+
CALL "C$SLEEP" USING 4.
9+
DISPLAY "End".
10+
STOP RUN.
11+
])
12+
13+
AT_CHECK([${COMPILE} prog1.cbl])
14+
15+
# Test that the prog1ram takes at least 2 seconds to execute
16+
AT_CHECK([
17+
start_time=$(date +%s);
18+
java prog1;
19+
end_time=$(date +%s);
20+
duration=$((end_time - start_time));
21+
test $duration -ge 2], [0],
22+
[Start
23+
End
24+
])
25+
26+
27+
AT_DATA([prog2.cbl], [
28+
IDENTIFICATION DIVISION.
29+
PROGRAM-ID. prog2.
30+
DATA DIVISION.
31+
WORKING-STORAGE SECTION.
32+
01 WS-TIMEOUT PIC 9(4) COMP-3 VALUE 4.
33+
PROCEDURE DIVISION.
34+
DISPLAY "Start".
35+
CALL "C$SLEEP" USING WS-TIMEOUT.
36+
DISPLAY "End".
37+
STOP RUN.
38+
])
39+
40+
AT_CHECK([${COMPILE} prog2.cbl])
41+
42+
# Test that the prog2ram takes at least 2 seconds to execute
43+
AT_CHECK([
44+
start_time=$(date +%s);
45+
java prog2;
46+
end_time=$(date +%s);
47+
duration=$((end_time - start_time));
48+
test $duration -ge 2], [0],
49+
[Start
50+
End
51+
])
52+
53+
AT_CLEANUP

0 commit comments

Comments
 (0)