Skip to content

Commit a790f00

Browse files
Add a new option -info-json-dir (#362)
* [Feat]: implement a new option `-info-json-dir` * [Add]: add tests * [Fix]: remove .github/workflows/dependabot.yml
1 parent 6ec1b51 commit a790f00

File tree

8 files changed

+178
-9
lines changed

8 files changed

+178
-9
lines changed

.github/workflows/dependabot.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.

cobj/cobj.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ unsigned char cb_default_byte = 0;
142142
#define OPTION_ID_DEFAULT_BYTE (1024)
143143
#define OPTION_ID_SINGLE_JAR (1025)
144144
#define OPTION_ID_JAR (1026)
145+
#define OPTION_ID_INFO_JSON (1027)
145146

146147
int external_flg = 0;
147148
int errorcount = 0;
@@ -184,6 +185,9 @@ char *cb_java_package_name = NULL;
184185
int cb_flag_jar = 0;
185186
char *cb_single_jar_name = NULL;
186187

188+
int cb_flag_info_json = 0;
189+
char *cb_info_json_dir = NULL;
190+
187191
char edit_code_command[512];
188192
char edit_code_command_is_set = 0;
189193

@@ -285,6 +289,7 @@ static const struct option long_options[] = {
285289
{"conf", required_argument, NULL, '&'},
286290
{"debug", no_argument, NULL, 'd'},
287291
{"class-file-dir", optional_argument, NULL, 'o'},
292+
{"info-json-dir", required_argument, NULL, OPTION_ID_INFO_JSON},
288293
{"java-source-dir", optional_argument, NULL, 'j'},
289294
{"jar", no_argument, NULL, OPTION_ID_JAR},
290295
{"single-jar", required_argument, NULL, OPTION_ID_SINGLE_JAR},
@@ -824,6 +829,8 @@ static void cobc_print_usage(void) {
824829
"representing a character"));
825830
puts(_(" * octodecimal 00..0377 "
826831
"representing a character"));
832+
puts(_(" -info-json-dir=<dir> Specify the directory path of "
833+
"JSON files that hold information of COBOL programs"));
827834
puts(_(" -java-package(=<package name>) Specify the package name of the "
828835
"generated source code"));
829836
puts(_(" -jar Create <PROGRAM-ID>.jar and "
@@ -1042,6 +1049,21 @@ static int process_command_line(const int argc, char *argv[]) {
10421049
cb_flag_jar = 1;
10431050
break;
10441051

1052+
case OPTION_ID_INFO_JSON:
1053+
cb_flag_info_json = 1;
1054+
if (optarg) {
1055+
int len = strlen(optarg);
1056+
if (len != 0) {
1057+
cb_info_json_dir = malloc(len + 1);
1058+
strcpy(cb_info_json_dir, optarg);
1059+
break;
1060+
}
1061+
}
1062+
fprintf(stderr,
1063+
"Warning - An invalid name of a directory for json files\n");
1064+
fflush(stderr);
1065+
break;
1066+
10451067
case OPTION_ID_SINGLE_JAR:
10461068
cb_flag_jar = 1;
10471069
if (optarg) {

cobj/cobj.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,8 @@ extern void cb_error(const char *fmt, ...) COB_A_FORMAT12;
375375

376376
extern int cb_verify(const enum cb_support tag, const char *feature);
377377

378+
/* others */
379+
extern int cb_flag_info_json;
380+
extern char *cb_info_json_dir;
381+
378382
#endif /* CB_COBC_H */

cobj/codegen.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4294,6 +4294,57 @@ struct call_parameter_list {
42944294
};
42954295
struct call_parameter_list *call_parameter_cache = NULL;
42964296

4297+
static void write_json_info(struct cb_program *prog) {
4298+
if (!cb_flag_info_json) {
4299+
return;
4300+
}
4301+
4302+
cb_tree l;
4303+
cb_tree parameter_list = prog->parameter_list;
4304+
char json_file_path[COB_MEDIUM_BUFF];
4305+
snprintf(json_file_path, COB_MEDIUM_BUFF, "%s/info_%s.json", cb_info_json_dir,
4306+
prog->program_id);
4307+
4308+
FILE *fp = fopen(json_file_path, "w");
4309+
if (!fp) {
4310+
fprintf(stderr, "Warning: failed to open a json file %s\n", json_file_path);
4311+
return;
4312+
}
4313+
4314+
fprintf(fp, "{\n");
4315+
fprintf(fp, " \"json_format_version\": \"1.0.0\",\n");
4316+
fprintf(fp, " \"opensourcecobol4j_version\": \"%s\",\n", PACKAGE_VERSION);
4317+
fprintf(fp, " \"program_id\": \"%s\",\n", prog->program_id);
4318+
fprintf(fp, " \"procedure_division_using_parameters\": [\n");
4319+
4320+
for (l = parameter_list; l; l = CB_CHAIN(l)) {
4321+
struct cb_field *arg_field = cb_field(CB_VALUE(l));
4322+
int type = cb_tree_type(CB_TREE(arg_field));
4323+
fprintf(fp, " {\n");
4324+
fprintf(fp, " \"name\": \"%s\",\n", arg_field->name);
4325+
fprintf(fp, " \"type\": ");
4326+
if (type & COB_TYPE_NUMERIC) {
4327+
if (arg_field->pic->scale > 0) {
4328+
fprintf(fp, "\"double\"");
4329+
} else {
4330+
fprintf(fp, "\"int\"");
4331+
}
4332+
} else {
4333+
fprintf(fp, "\"String\"");
4334+
}
4335+
fprintf(fp, "\n");
4336+
fprintf(fp, " }");
4337+
if (CB_CHAIN(l)) {
4338+
fprintf(fp, ",");
4339+
}
4340+
fprintf(fp, "\n");
4341+
}
4342+
fprintf(fp, " ]\n");
4343+
fprintf(fp, "}\n");
4344+
4345+
fclose(fp);
4346+
}
4347+
42974348
static void joutput_java_entrypoint(struct cb_program *prog,
42984349
cb_tree parameter_list) {
42994350
cb_tree l;
@@ -6006,6 +6057,7 @@ static void joutput_execution_entry_func() {
60066057

60076058
void codegen(struct cb_program *prog, const int nested, char **program_id_list,
60086059
char *java_source_dir, char *source_file) {
6060+
60096061
int i;
60106062
cb_tree l;
60116063
struct field_list *k;
@@ -6100,6 +6152,8 @@ void codegen(struct cb_program *prog, const int nested, char **program_id_list,
61006152
// output ("\n");
61016153
}
61026154

6155+
write_json_info(prog);
6156+
61036157
call_parameters = prog->parameter_list;
61046158
joutput_line("/* Generated by %s %s */", PACKAGE_NAME, PACKAGE_VERSION);
61056159

tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ command_line_options_DEPENDENCIES = \
164164
command-line-options.src/fserial-variable.at \
165165
command-line-options.src/fshort-variable.at \
166166
command-line-options.src/ffold-copy.at \
167+
command-line-options.src/info-java-dir.at \
167168
command-line-options.src/java-package.at \
168169
command-line-options.src/edit-code-command.at \
169170
command-line-options.src/file-path.at \

tests/Makefile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ command_line_options_DEPENDENCIES = \
705705
command-line-options.src/fserial-variable.at \
706706
command-line-options.src/fshort-variable.at \
707707
command-line-options.src/ffold-copy.at \
708+
command-line-options.src/info-java-dir.at \
708709
command-line-options.src/java-package.at \
709710
command-line-options.src/edit-code-command.at \
710711
command-line-options.src/file-path.at \

tests/command-line-options.at

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ m4_include([B.at])
99
m4_include([list-reserved.at])
1010
m4_include([ext.at])
1111
m4_include([assign_external.at])
12+
m4_include([info-java-dir.at])
1213
m4_include([java-package.at])
1314
m4_include([Wobsolete-Warchaic.at])
1415
m4_include([Wredefinition.at])
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
AT_SETUP([-info-java-dir])
2+
3+
AT_CHECK([${COBJ} --help | grep info-json-dir > /dev/null], [0])
4+
5+
AT_DATA([callee.cbl], [
6+
IDENTIFICATION DIVISION.
7+
PROGRAM-ID. callee.
8+
DATA DIVISION.
9+
WORKING-STORAGE SECTION.
10+
LINKAGE SECTION.
11+
01 P1 PIC X ANY LENGTH.
12+
01 P2 PIC 99.
13+
PROCEDURE DIVISION USING P1 P2.
14+
GOBACK.
15+
])
16+
17+
AT_DATA([prog.cbl], [
18+
IDENTIFICATION DIVISION.
19+
PROGRAM-ID. prog.
20+
DATA DIVISION.
21+
WORKING-STORAGE SECTION.
22+
LINKAGE SECTION.
23+
PROCEDURE DIVISION.
24+
STOP RUN.
25+
])
26+
27+
# a basic usecase
28+
AT_CHECK([${COBJ} -info-json-dir=./ callee.cbl prog.cbl])
29+
AT_CHECK([cat info_callee.json], [0],
30+
[{
31+
"json_format_version": "1.0.0",
32+
"opensourcecobol4j_version": "1.0.20",
33+
"program_id": "callee",
34+
"procedure_division_using_parameters": @<:@
35+
{
36+
"name": "P1",
37+
"type": "String"
38+
},
39+
{
40+
"name": "P2",
41+
"type": "int"
42+
}
43+
@:>@
44+
}
45+
])
46+
47+
AT_CHECK([cat info_prog.json], [0],
48+
[{
49+
"json_format_version": "1.0.0",
50+
"opensourcecobol4j_version": "1.0.20",
51+
"program_id": "prog",
52+
"procedure_division_using_parameters": @<:@
53+
@:>@
54+
}
55+
])
56+
57+
# specify an invalid directory
58+
AT_CHECK([${COBJ} -info-json-dir=aaa callee.cbl prog.cbl], [0], [],
59+
[Warning: failed to open a json file aaa/info_callee.json
60+
Warning: failed to open a json file aaa/info_prog.json
61+
])
62+
63+
# specify an existing directory
64+
AT_CHECK([mkdir bbb])
65+
AT_CHECK([${COBJ} -info-json-dir=bbb callee.cbl prog.cbl])
66+
67+
AT_CHECK([cat bbb/info_callee.json], [0],
68+
[{
69+
"json_format_version": "1.0.0",
70+
"opensourcecobol4j_version": "1.0.20",
71+
"program_id": "callee",
72+
"procedure_division_using_parameters": @<:@
73+
{
74+
"name": "P1",
75+
"type": "String"
76+
},
77+
{
78+
"name": "P2",
79+
"type": "int"
80+
}
81+
@:>@
82+
}
83+
])
84+
85+
AT_CHECK([cat bbb/info_prog.json], [0],
86+
[{
87+
"json_format_version": "1.0.0",
88+
"opensourcecobol4j_version": "1.0.20",
89+
"program_id": "prog",
90+
"procedure_division_using_parameters": @<:@
91+
@:>@
92+
}
93+
])
94+
95+
AT_CLEANUP

0 commit comments

Comments
 (0)