Skip to content

Commit 8398c5e

Browse files
Add a new option -fvar-name-hex (#501)
1 parent a70fa1f commit 8398c5e

File tree

7 files changed

+120
-18
lines changed

7 files changed

+120
-18
lines changed

cobj/codegen.c

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -234,31 +234,56 @@ static void get_java_identifier_helper(struct cb_field *f, char *buf) {
234234
}
235235

236236
static void strcpy_identifier_cobol_to_java(char *buf, const char *identifier) {
237-
int all_ascii = 1;
238-
unsigned char *p = (unsigned char *)identifier;
239-
for (; *p; ++p) {
240-
unsigned char c = *p;
241-
if (c < 0x0A || 0x80 <= c) {
242-
all_ascii = 0;
243-
break;
237+
if (cb_flag_var_name_hex) {
238+
int all_ascii = 1;
239+
unsigned char *p = (unsigned char *)identifier;
240+
for (; *p; ++p) {
241+
unsigned char c = *p;
242+
if (c < 0x0A || 0x80 <= c) {
243+
all_ascii = 0;
244+
break;
245+
}
244246
}
245-
}
246247

247-
if (all_ascii) {
248-
for (; *identifier; ++identifier, ++buf) {
249-
if (*identifier == '-') {
250-
*buf = '_';
251-
} else {
252-
*buf = *identifier;
248+
if (all_ascii) {
249+
for (; *identifier; ++identifier, ++buf) {
250+
if (*identifier == '-') {
251+
*buf = '_';
252+
} else {
253+
*buf = *identifier;
254+
}
255+
}
256+
} else {
257+
for (; *identifier; ++identifier) {
258+
sprintf(buf, "%02x", (unsigned char)*identifier);
259+
buf += 2;
253260
}
254261
}
262+
*buf = '\0';
255263
} else {
256-
for (; *identifier; ++identifier) {
257-
sprintf(buf, "%02x", (unsigned char)*identifier);
258-
buf += 2;
264+
unsigned char *src = (unsigned char *)identifier;
265+
unsigned char *dst = (unsigned char *)buf;
266+
while (*src) {
267+
// ASCII characters
268+
if (*src < 0x80) {
269+
*dst = (*src == '-') ? '_' : *src;
270+
++dst;
271+
++src;
272+
// Shift_JIS characters
273+
} else {
274+
if (src[0] == 0x81 && src[1] == 0x7C) {
275+
dst[0] = 0x81;
276+
dst[1] = 0x51;
277+
} else {
278+
dst[0] = src[0];
279+
dst[1] = src[1];
280+
}
281+
dst += 2;
282+
src += 2;
283+
}
259284
}
285+
*dst = '\0';
260286
}
261-
*buf = '\0';
262287
}
263288

264289
struct cb_label_id_map {

cobj/flag-help.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,8 @@ CB_FLAG (cb_flag_functions_all, "functions-all",
5656
CB_FLAG (cb_flag_mfcomment, "mfcomment",
5757
N_("'*' or '/' in column 1 treated as comment (FIXED only)"))
5858

59+
CB_FLAG (cb_flag_var_name_hex, "var-name-hex",
60+
N_("Use hexdecimal representation for variable names that contains non-ASCII characters"))
61+
5962
//CB_FLAG (cb_flag_no_cobol_comment, "no-cobol-comment",
6063
// N_("Do not generate COBOL comments into Java programs"))

cobj/flag.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,8 @@ CB_FLAG (cb_flag_serial_variable, "serial-variable",
8181
CB_FLAG (cb_flag_short_variable, "short-variable",
8282
N_("Use short variable names in Java source code. This feature may cause compilation errors"))
8383

84+
CB_FLAG (cb_flag_var_name_hex, "var-name-hex",
85+
N_("Use hexdecimal representation for variable names that contains non-ASCII characters"))
86+
8487
//CB_FLAG (cb_flag_no_cobol_comment, "no-cobol-comment",
8588
// N_("Do not generate COBOL comments into Java programs"))

tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ command_line_options_DEPENDENCIES = \
163163
command-line-options.src/Wtruncate.at \
164164
command-line-options.src/Wunreachable.at \
165165
command-line-options.src/ftrace-ftraceall.at \
166+
command-line-options.src/fvar-name-hex.at \
166167
command-line-options.src/fsyntax-only.at \
167168
command-line-options.src/fserial-variable.at \
168169
command-line-options.src/fshort-variable.at \

tests/Makefile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ command_line_options_DEPENDENCIES = \
704704
command-line-options.src/Wtruncate.at \
705705
command-line-options.src/Wunreachable.at \
706706
command-line-options.src/ftrace-ftraceall.at \
707+
command-line-options.src/fvar-name-hex.at \
707708
command-line-options.src/fsyntax-only.at \
708709
command-line-options.src/fserial-variable.at \
709710
command-line-options.src/fshort-variable.at \

tests/command-line-options.at

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ m4_include([Wterminator.at])
2323
m4_include([Wtruncate.at])
2424
m4_include([Wunreachable.at])
2525
m4_include([ftrace-ftraceall.at])
26+
m4_include([fvar-name-hex.at])
2627
m4_include([fsyntax-only.at])
2728
m4_include([fserial-variable.at])
2829
m4_include([ffold-copy.at])
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
AT_SETUP([-fvar-name-hex])
2+
3+
AT_DATA([prog.cbl], [ IDENTIFICATION DIVISION.
4+
PROGRAM-ID. prog.
5+
6+
DATA DIVISION.
7+
WORKING-STORAGE SECTION.
8+
01 �V�h PIC x(5) VALUE "00000".
9+
01 ���l�|�P PIC x(5) VALUE "11111".
10+
01 �b�{�|2 PIC x(5) VALUE "22222".
11+
01 �F�s�{�P PIC x(5) VALUE "33333".
12+
01 ���2 PIC x(5) VALUE "44444".
13+
PROCEDURE DIVISION.
14+
�������x��.
15+
DISPLAY �V�h.
16+
�_�ސ�|�P���x��.
17+
DISPLAY ���l�|�P.
18+
�R���|2���x��.
19+
DISPLAY �b�{�|2.
20+
�Ȗ؂P���x��.
21+
DISPLAY �F�s�{�P.
22+
�{��2���x��.
23+
DISPLAY ���2.
24+
])
25+
26+
27+
AT_CHECK([${COMPILE} prog.cbl])
28+
AT_CHECK([${RUN_MODULE} prog], [0],
29+
[00000
30+
11111
31+
22222
32+
33333
33+
44444
34+
])
35+
AT_CHECK([cat prog.java | grep "�V�h" > /dev/null])
36+
AT_CHECK([cat prog.java | grep "���l�|�P" > /dev/null])
37+
AT_CHECK([cat prog.java | grep "�b�{�|2" > /dev/null])
38+
AT_CHECK([cat prog.java | grep "�F�s�{�P" > /dev/null])
39+
AT_CHECK([cat prog.java | grep "���2" > /dev/null])
40+
41+
AT_CHECK([cat prog.java | grep "�������x��" > /dev/null])
42+
AT_CHECK([cat prog.java | grep "�_�ސ�|�P���x��" > /dev/null])
43+
AT_CHECK([cat prog.java | grep "�R���|2���x��" > /dev/null])
44+
AT_CHECK([cat prog.java | grep "�Ȗ؂P���x��" > /dev/null])
45+
AT_CHECK([cat prog.java | grep "�{��2���x��" > /dev/null])
46+
47+
AT_CHECK([${COMPILE} -fvar-name-hex prog.cbl])
48+
AT_CHECK([${RUN_MODULE} prog], [0],
49+
[00000
50+
11111
51+
22222
52+
33333
53+
44444
54+
])
55+
56+
AT_CHECK([cat prog.java | grep "90568f68" > /dev/null])
57+
AT_CHECK([cat prog.java | grep "89a1956c817c8250" > /dev/null])
58+
AT_CHECK([cat prog.java | grep "8d62957b817c32" > /dev/null])
59+
AT_CHECK([cat prog.java | grep "894693738b7b8250" > /dev/null])
60+
AT_CHECK([cat prog.java | grep "90e591e432" > /dev/null])
61+
62+
AT_CHECK([cat prog.java | grep "938c8b9e83898378838b" > /dev/null])
63+
AT_CHECK([cat prog.java | grep "905f93de90ec817c825083898378838b" > /dev/null])
64+
AT_CHECK([cat prog.java | grep "8e52979c817c3283898378838b" > /dev/null])
65+
AT_CHECK([cat prog.java | grep "93c896d8825083898378838b" > /dev/null])
66+
AT_CHECK([cat prog.java | grep "8b7b8fe93283898378838b" > /dev/null])
67+
68+
AT_CLEANUP

0 commit comments

Comments
 (0)