Skip to content

Commit

Permalink
added command line option for the alphanumeric literals encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedMaher309 committed Sep 29, 2024
1 parent a97d0c5 commit 7efa05c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 25 deletions.
40 changes: 35 additions & 5 deletions cobc/cobc.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ enum compile_level {
#define CB_FLAG_GETOPT_COPY_FILE 18
#define CB_FLAG_GETOPT_INCLUDE_FILE 19
#define CB_FLAG_GETOPT_SOURCE_ENCODE 20
#define CB_FLAG_GETOPT_ALPHANUMERIC_ENCODE 21


/* Info display limits */
Expand Down Expand Up @@ -346,6 +347,11 @@ initialize_cb_iconv() {
strncpy(cb_iconv.source, "ISO-8859-15", sizeof(cb_iconv.source) - 1);
cb_iconv.source[sizeof(cb_iconv.source) - 1] = '\0';
#endif
/* set the alphanumeric_source encoding to a default value
to avoid converting in cb_build_alphanumeric
if it didn't change by the command line*/
strncpy (cb_iconv.alphanumeric_source, "NONE", sizeof(cb_iconv.alphanumeric_source) - 1);
cb_iconv.alphanumeric_source[sizeof(cb_iconv.alphanumeric_source) - 1] = '\0';
}
#endif

Expand Down Expand Up @@ -3885,10 +3891,6 @@ process_command_line (const int argc, char **argv)
break;



/* -fsource-encode-alphanumeric=encoding */


case CB_FLAG_GETOPT_SOURCE_ENCODE: {
/* -fsource-encode=encoding */
const char* valid_encodings[] = {
Expand Down Expand Up @@ -3918,6 +3920,34 @@ process_command_line (const int argc, char **argv)
break;
}


/* -falphanumeric-encode */
case CB_FLAG_GETOPT_ALPHANUMERIC_ENCODE:{
const char* valid_encodings[] = {
"ASCII",
"ISO-8859-1",
"ISO-8859-15",
"CP1525"
};
const int num_encodings = sizeof(valid_encodings) / sizeof(valid_encodings[0]);
int i, encoding_valid = 0;
for (i = 0; i < num_encodings; i++) {
if (strcmp(cob_optarg, valid_encodings[i]) == 0) {
encoding_valid = 1;
break;
}
}
if (encoding_valid) {
#ifdef HAVE_ICONV
strncpy(cb_iconv.alphanumeric_source, cob_optarg, sizeof(cb_iconv.alphanumeric_source) - 1);
cb_iconv.alphanumeric_source[sizeof(cb_iconv.alphanumeric_source) - 1] = '\0';
#endif
} else {
cobc_err_exit(COBC_INV_PAR, "-falphanumeric-encode");
}
break;
}

case CB_FLAG_GETOPT_TTITLE: {
/* -fttitle=<title> : Title for listing */
const size_t len = strlen (cob_optarg);
Expand Down Expand Up @@ -9441,7 +9471,7 @@ main (int argc, char **argv)

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

cb_iconv.national = iconv_open("UTF-16LE", cb_iconv.source);
Expand Down
1 change: 1 addition & 0 deletions cobc/cobc.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ struct cb_iconv_t{
iconv_t national;
iconv_t utf8;
char source[100];
char alphanumeric_source[100];
};

extern struct cb_iconv_t cb_iconv;
Expand Down
4 changes: 4 additions & 0 deletions cobc/flag.def
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ CB_FLAG_RQ (cb_source_encode, 1, "source-encode", 0, CB_FLAG_GETOPT_SOURCE_ENCOD
_(" -fsource-encode=[encoding]\tdefine the source file encoding\n"
" * default: ISO-8859-15"))

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


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

Expand Down
19 changes: 19 additions & 0 deletions cobc/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2787,10 +2787,19 @@ cb_build_alphanumeric_literal (const void *data, const size_t size)
{
cb_tree l;


#ifdef HAVE_ICONV
size_t outsize = size;
void * outdata = cobc_malloc (outsize);

/* check if there is a command line input for the alphanumeric*/
if(strcmp(cb_iconv.alphanumeric_source, "NONE") == 0){
l = CB_TREE (build_literal (CB_CATEGORY_ALPHANUMERIC, data, size));
l->source_file = cb_source_file;
l->source_line = cb_source_line;
return l;
}

if (cb_iconv.alphanumeric == (iconv_t)-1) {
cobc_err_msg (_("iconv_open failed"));
} else {
Expand Down Expand Up @@ -2871,6 +2880,11 @@ cb_build_national_literal (const void *data, const size_t size)
cobc_err_msg(_("iconv failed: Unknown error"));
break;
}
cobc_free (outdata);
l = CB_TREE (build_literal (CB_CATEGORY_NATIONAL, data, size));
l->source_file = cb_source_file;
l->source_line = cb_source_line;
return l;
} else {
outsize -= outbytesleft;
outdata = cobc_realloc (outdata, outsize); /* Resize the outdata to the actual size */
Expand Down Expand Up @@ -2940,6 +2954,11 @@ cb_build_UTF8_literal (const void *data, const size_t size)
cobc_err_msg (_("iconv failed: Unknown error"));
break;
}
cobc_free (outdata);
l = CB_TREE (build_literal (CB_CATEGORY_UTF8, data, size));
l->source_file = cb_source_file;
l->source_line = cb_source_line;
return l;
} else {
outsize -= outbytesleft;
outdata = cobc_realloc (outdata, outsize); /* Resize the outdata to the actual size */
Expand Down
3 changes: 2 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ testsuite_sources = \
testsuite.src/data_pointer.at \
testsuite.src/backcomp.at \
testsuite.src/numeric-dump.cob \
testsuite.src/numeric-display.cob
testsuite.src/numeric-display.cob \
testsuite.src/iso885915.cob

testsuite_manual_sources = \
testsuite.src/run_manual_screen.at
Expand Down
17 changes: 17 additions & 0 deletions tests/testsuite.src/iso885915.cob
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. prog.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 HEXX PIC X(35).
88 HEXX-FILLER VALUE ALL "-".
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM DO-CHECK.
GOBACK.

DO-CHECK.
SET HEXX-FILLER TO TRUE
STRING FUNCTION HEX-OF (N"€€")
DELIMITED BY SIZE INTO HEXX.
IF HEXX NOT = "AC20AC20---------------------------"
DISPLAY "UNEXPECTED HEX-VALUE OF N'€€': " HEXX.
19 changes: 0 additions & 19 deletions tests/testsuite.src/run_functions.at
Original file line number Diff line number Diff line change
Expand Up @@ -626,25 +626,6 @@ AT_KEYWORDS([literal functions hex-of])

# TODO: move this to iso885915.cob in the file system
# add to tests/Makefile.am for distribution
AT_DATA([prog.cob], [
IDENTIFICATION DIVISION.
PROGRAM-ID. prog.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 HEXX PIC X(35).
88 HEXX-FILLER VALUE ALL "-".
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM DO-CHECK.
GOBACK.

DO-CHECK.
SET HEXX-FILLER TO TRUE
STRING FUNCTION HEX-OF (N"€€")
DELIMITED BY SIZE INTO HEXX.
IF HEXX NOT = "AC20AC20---------------------------"
DISPLAY "UNEXPECTED HEX-VALUE OF N'€€': " HEXX.
])

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

0 comments on commit 7efa05c

Please sign in to comment.