@@ -46,7 +46,7 @@ output_table (FILE *stream, const char *table)
4646
4747/* Build a pair of EBCDIC/ASCII translation tables using iconv */
4848int
49- gentable (FILE * stream , const char * code_ebcdic , const char * code_ascii )
49+ gentable (FILE * stream , const char * code_ebcdic , const char * code_ascii , char reversible )
5050{
5151 char ebcdic [UCHAR_MAX + 1 ], ascii [UCHAR_MAX + 1 ] = { 0 };
5252 char * ebcdic_ptr = ebcdic , * ascii_ptr = ascii ;
@@ -56,7 +56,7 @@ gentable (FILE *stream, const char *code_ebcdic, const char *code_ascii)
5656 iconv_t ic = iconv_open (code_ascii , code_ebcdic );
5757 if (ic == (iconv_t )- 1 ) {
5858 cb_error (_ ("conversion from %s to %s is not supported by your iconv implementation" ),
59- code_ascii , code_ebcdic );
59+ code_ebcdic , code_ascii );
6060 return -1 ;
6161 }
6262
@@ -102,15 +102,8 @@ gentable (FILE *stream, const char *code_ebcdic, const char *code_ascii)
102102
103103 iconv_close (ic );
104104
105- fprintf (stream , "# GnuCOBOL %s <-> %s translation tables\n" , code_ebcdic , code_ascii );
106-
107- fprintf (stream , "\n# %s to %s translation table\n\n" , code_ebcdic , code_ascii );
108- output_table (stream , ascii );
105+ if (nb_irreversible > 0 ) {
109106
110- fprintf (stream , "\n# %s to %s translation table\n\n" , code_ascii , code_ebcdic );
111- if (nb_irreversible == 0 ) {
112- fprintf (stream , "# This translation being symmetric, the table is built from the previous one.\n\n" );
113- } else {
114107 /* Build the (partial) reverse translation table */
115108 memset (ebcdic , EBCDIC_SUBST_CHAR , UCHAR_MAX + 1 );
116109 for (i = 0 ; i <= UCHAR_MAX ; ++ i ) {
@@ -121,12 +114,40 @@ gentable (FILE *stream, const char *code_ebcdic, const char *code_ascii)
121114 times, the loop above probably did not set it correctly */
122115 ebcdic [(unsigned char )ASCII_SUBST_CHAR ] = EBCDIC_SUBST_CHAR ;
123116
117+ if (reversible != 0 ) {
118+ /* If a reversible translation table is requested, we arbitrarily
119+ map together unused characters from both encodings */
120+ unsigned short j = 0 ;
121+ for (i = 0 ; i <= UCHAR_MAX ; ++ i ) {
122+ if ((ascii [i ] == ASCII_SUBST_CHAR ) && (i != EBCDIC_SUBST_CHAR )) {
123+ while ((ebcdic [j ] != EBCDIC_SUBST_CHAR ) || (j == ASCII_SUBST_CHAR )) {
124+ ++ j ;
125+ }
126+ ascii [i ] = (char )j ;
127+ ebcdic [j ] = (char )i ;
128+ }
129+ }
130+ cb_note (COB_WARNOPT_NONE , 0 ,
131+ _ ("%d non-reversible conversions were arbitrarily made reversible, you might want to review the generated table" ),
132+ nb_irreversible );
133+ } else {
134+ cb_note (COB_WARNOPT_NONE , 0 ,
135+ _ ("%d non-reversible conversions were performed, you might want to review the generated table" ),
136+ nb_irreversible );
137+ }
138+ }
139+
140+ fprintf (stream , "# GnuCOBOL %s <-> %s translation tables\n" , code_ebcdic , code_ascii );
141+
142+ fprintf (stream , "\n# %s to %s translation table\n\n" , code_ebcdic , code_ascii );
143+ output_table (stream , ascii );
144+
145+ fprintf (stream , "\n# %s to %s translation table\n\n" , code_ascii , code_ebcdic );
146+ if ((nb_irreversible == 0 ) || (reversible != 0 )) {
147+ fprintf (stream , "# This translation being symmetric, the table is built from the previous one.\n\n" );
148+ } else {
124149 output_table (stream , ebcdic );
125150 fprintf (stream , "\n" );
126-
127- cb_note (COB_WARNOPT_NONE , 0 ,
128- _ ("%d non-reversible conversions were performed, you might want to review the generated table" ),
129- nb_irreversible );
130151 }
131152
132153 return 0 ;
@@ -135,11 +156,12 @@ gentable (FILE *stream, const char *code_ebcdic, const char *code_ascii)
135156#else
136157
137158int
138- gentable (FILE * stream , const char * code_ebcdic , const char * code_ascii )
159+ gentable (FILE * stream , const char * code_ebcdic , const char * code_ascii , char reversible )
139160{
140161 COB_UNUSED (stream );
141162 COB_UNUSED (code_ebcdic );
142163 COB_UNUSED (code_ascii );
164+ COB_UNUSED (reversible );
143165
144166 cb_error (_ ("runtime is not configured to support %s" ), "iconv" );
145167 cb_error (_ ("translation table generation is not available" ));
0 commit comments