Skip to content

Commit 5a2a864

Browse files
committed
added command line option for the alphanumeric literals encoding
1 parent b04dae4 commit 5a2a864

File tree

7 files changed

+78
-25
lines changed

7 files changed

+78
-25
lines changed

cobc/cobc.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ enum compile_level {
115115
#define CB_FLAG_GETOPT_COPY_FILE 18
116116
#define CB_FLAG_GETOPT_INCLUDE_FILE 19
117117
#define CB_FLAG_GETOPT_SOURCE_ENCODE 20
118+
#define CB_FLAG_GETOPT_ALPHANUMERIC_ENCODE 21
118119

119120

120121
/* Info display limits */
@@ -346,6 +347,11 @@ initialize_cb_iconv() {
346347
strncpy(cb_iconv.source, "ISO-8859-15", sizeof(cb_iconv.source) - 1);
347348
cb_iconv.source[sizeof(cb_iconv.source) - 1] = '\0';
348349
#endif
350+
/* set the alphanumeric_source encoding to a default value
351+
to avoid converting in cb_build_alphanumeric
352+
if it didn't change by the command line*/
353+
strncpy (cb_iconv.alphanumeric_source, "NONE", sizeof(cb_iconv.alphanumeric_source) - 1);
354+
cb_iconv.alphanumeric_source[sizeof(cb_iconv.alphanumeric_source) - 1] = '\0';
349355
}
350356
#endif
351357

@@ -3885,10 +3891,6 @@ process_command_line (const int argc, char **argv)
38853891
break;
38863892

38873893

3888-
3889-
/* -fsource-encode-alphanumeric=encoding */
3890-
3891-
38923894
case CB_FLAG_GETOPT_SOURCE_ENCODE: {
38933895
/* -fsource-encode=encoding */
38943896
const char* valid_encodings[] = {
@@ -3918,6 +3920,34 @@ process_command_line (const int argc, char **argv)
39183920
break;
39193921
}
39203922

3923+
3924+
/* -falphanumeric-encode */
3925+
case CB_FLAG_GETOPT_ALPHANUMERIC_ENCODE:{
3926+
const char* valid_encodings[] = {
3927+
"ASCII",
3928+
"ISO-8859-1",
3929+
"ISO-8859-15",
3930+
"CP1525"
3931+
};
3932+
const int num_encodings = sizeof(valid_encodings) / sizeof(valid_encodings[0]);
3933+
int i, encoding_valid = 0;
3934+
for (i = 0; i < num_encodings; i++) {
3935+
if (strcmp(cob_optarg, valid_encodings[i]) == 0) {
3936+
encoding_valid = 1;
3937+
break;
3938+
}
3939+
}
3940+
if (encoding_valid) {
3941+
#ifdef HAVE_ICONV
3942+
strncpy(cb_iconv.alphanumeric_source, cob_optarg, sizeof(cb_iconv.alphanumeric_source) - 1);
3943+
cb_iconv.alphanumeric_source[sizeof(cb_iconv.alphanumeric_source) - 1] = '\0';
3944+
#endif
3945+
} else {
3946+
cobc_err_exit(COBC_INV_PAR, "-falphanumeric-encode");
3947+
}
3948+
break;
3949+
}
3950+
39213951
case CB_FLAG_GETOPT_TTITLE: {
39223952
/* -fttitle=<title> : Title for listing */
39233953
const size_t len = strlen (cob_optarg);
@@ -9441,7 +9471,7 @@ main (int argc, char **argv)
94419471

94429472
/* initialize the iconv struct after reading the command line*/
94439473
#ifdef HAVE_ICONV
9444-
cb_iconv.alphanumeric = iconv_open("ISO-8859-15", cb_iconv.source);
9474+
cb_iconv.alphanumeric = iconv_open(cb_iconv.alphanumeric_source, cb_iconv.source);
94459475
/* move iconv_open check here */
94469476

94479477
cb_iconv.national = iconv_open("UTF-16LE", cb_iconv.source);

cobc/cobc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ struct cb_iconv_t{
337337
iconv_t national;
338338
iconv_t utf8;
339339
char source[100];
340+
char alphanumeric_source[100];
340341
};
341342

342343
extern struct cb_iconv_t cb_iconv;

cobc/flag.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ CB_FLAG_RQ (cb_source_encode, 1, "source-encode", 0, CB_FLAG_GETOPT_SOURCE_ENCOD
7272
_(" -fsource-encode=[encoding]\tdefine the source file encoding\n"
7373
" * default: ISO-8859-15"))
7474

75+
CB_FLAG_RQ (cb_alphanumeric_encode, 1, "alphanumeric-encode", 0, CB_FLAG_GETOPT_ALPHANUMERIC_ENCODE,
76+
_(" -falphanumeric-encode=[encoding]\tdefine the alphanumeric encoding\n"
77+
" * default: UTF-8"))
78+
7579

7680
/* Flags with required parameter and no associated variable */
7781

cobc/tree.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,10 +2787,19 @@ cb_build_alphanumeric_literal (const void *data, const size_t size)
27872787
{
27882788
cb_tree l;
27892789

2790+
27902791
#ifdef HAVE_ICONV
27912792
size_t outsize = size;
27922793
void * outdata = cobc_malloc (outsize);
27932794

2795+
/* check if there is a command line input for the alphanumeric*/
2796+
if(strcmp(cb_iconv.alphanumeric_source, "NONE") == 0){
2797+
l = CB_TREE (build_literal (CB_CATEGORY_ALPHANUMERIC, data, size));
2798+
l->source_file = cb_source_file;
2799+
l->source_line = cb_source_line;
2800+
return l;
2801+
}
2802+
27942803
if (cb_iconv.alphanumeric == (iconv_t)-1) {
27952804
cobc_err_msg (_("iconv_open failed"));
27962805
} else {
@@ -2871,6 +2880,11 @@ cb_build_national_literal (const void *data, const size_t size)
28712880
cobc_err_msg(_("iconv failed: Unknown error"));
28722881
break;
28732882
}
2883+
cobc_free (outdata);
2884+
l = CB_TREE (build_literal (CB_CATEGORY_NATIONAL, data, size));
2885+
l->source_file = cb_source_file;
2886+
l->source_line = cb_source_line;
2887+
return l;
28742888
} else {
28752889
outsize -= outbytesleft;
28762890
outdata = cobc_realloc (outdata, outsize); /* Resize the outdata to the actual size */
@@ -2940,6 +2954,11 @@ cb_build_UTF8_literal (const void *data, const size_t size)
29402954
cobc_err_msg (_("iconv failed: Unknown error"));
29412955
break;
29422956
}
2957+
cobc_free (outdata);
2958+
l = CB_TREE (build_literal (CB_CATEGORY_UTF8, data, size));
2959+
l->source_file = cb_source_file;
2960+
l->source_line = cb_source_line;
2961+
return l;
29432962
} else {
29442963
outsize -= outbytesleft;
29452964
outdata = cobc_realloc (outdata, outsize); /* Resize the outdata to the actual size */

tests/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ testsuite_sources = \
6565
testsuite.src/data_pointer.at \
6666
testsuite.src/backcomp.at \
6767
testsuite.src/numeric-dump.cob \
68-
testsuite.src/numeric-display.cob
68+
testsuite.src/numeric-display.cob \
69+
testsuite.src/iso885915.cob
6970

7071
testsuite_manual_sources = \
7172
testsuite.src/run_manual_screen.at

tests/testsuite.src/iso885915.cob

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
IDENTIFICATION DIVISION.
2+
PROGRAM-ID. prog.
3+
DATA DIVISION.
4+
WORKING-STORAGE SECTION.
5+
01 HEXX PIC X(35).
6+
88 HEXX-FILLER VALUE ALL "-".
7+
PROCEDURE DIVISION.
8+
MAIN-PROCEDURE.
9+
PERFORM DO-CHECK.
10+
GOBACK.
11+
12+
DO-CHECK.
13+
SET HEXX-FILLER TO TRUE
14+
STRING FUNCTION HEX-OF (N"€€")
15+
DELIMITED BY SIZE INTO HEXX.
16+
IF HEXX NOT = "AC20AC20---------------------------"
17+
DISPLAY "UNEXPECTED HEX-VALUE OF N'€€': " HEXX.

tests/testsuite.src/run_functions.at

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -626,25 +626,6 @@ AT_KEYWORDS([literal functions hex-of])
626626

627627
# TODO: move this to iso885915.cob in the file system
628628
# add to tests/Makefile.am for distribution
629-
AT_DATA([prog.cob], [
630-
IDENTIFICATION DIVISION.
631-
PROGRAM-ID. prog.
632-
DATA DIVISION.
633-
WORKING-STORAGE SECTION.
634-
01 HEXX PIC X(35).
635-
88 HEXX-FILLER VALUE ALL "-".
636-
PROCEDURE DIVISION.
637-
MAIN-PROCEDURE.
638-
PERFORM DO-CHECK.
639-
GOBACK.
640-
641-
DO-CHECK.
642-
SET HEXX-FILLER TO TRUE
643-
STRING FUNCTION HEX-OF (N"€€")
644-
DELIMITED BY SIZE INTO HEXX.
645-
IF HEXX NOT = "AC20AC20---------------------------"
646-
DISPLAY "UNEXPECTED HEX-VALUE OF N'€€': " HEXX.
647-
])
648629

649630
# we do override the source encoding here as the testsuite runs with LC_ALL=C,
650631
# which gets down to "ANSI_X3.4-1968" encoding from nl_langinfro;

0 commit comments

Comments
 (0)