Skip to content

Commit f43f05f

Browse files
committed
Updates after review
1 parent 5bd9321 commit f43f05f

File tree

11 files changed

+79
-105
lines changed

11 files changed

+79
-105
lines changed

build_windows/ocide/cobc.exe.cpj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<FILE NAME="..\..\cobc\config.c" TITLE="config.c" CLEAN="0"/>
8383
<FILE NAME="..\..\cobc\error.c" TITLE="error.c" CLEAN="0"/>
8484
<FILE NAME="..\..\cobc\field.c" TITLE="field.c" CLEAN="0"/>
85+
<FILE NAME="..\..\cobc\field.c" TITLE="gentable.c" CLEAN="0"/>
8586
<FILE NAME="..\..\cobc\help.c" TITLE="help.c" CLEAN="0"/>
8687
<FILE NAME="..\..\cobc\parser.y" TITLE="parser.y" CLEAN="0"/>
8788
<FILE NAME="..\..\cobc\ppparse.y" TITLE="ppparse.y" CLEAN="0"/>

build_windows/vs2010/cobc.vcxproj.filters

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<ClCompile Include="..\..\cobc\field.c">
5252
<Filter>Source Files</Filter>
5353
</ClCompile>
54+
<ClCompile Include="..\..\cobc\gentable.c">
55+
<Filter>Source Files</Filter>
56+
</ClCompile>
5457
<ClCompile Include="..\..\cobc\replace.c">
5558
<Filter>Source Files</Filter>
5659
</ClCompile>
@@ -277,4 +280,4 @@
277280
<Filter>Source Files</Filter>
278281
</CustomBuild>
279282
</ItemGroup>
280-
</Project>
283+
</Project>

build_windows/vs2012/cobc.vcxproj.filters

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<ClCompile Include="..\..\cobc\field.c">
5252
<Filter>Source Files</Filter>
5353
</ClCompile>
54+
<ClCompile Include="..\..\cobc\gentable.c">
55+
<Filter>Source Files</Filter>
56+
</ClCompile>
5457
<ClCompile Include="..\..\cobc\replace.c">
5558
<Filter>Source Files</Filter>
5659
</ClCompile>
@@ -277,4 +280,4 @@
277280
<Filter>Source Files</Filter>
278281
</CustomBuild>
279282
</ItemGroup>
280-
</Project>
283+
</Project>

build_windows/vs2013/cobc.vcxproj.filters

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<ClCompile Include="..\..\cobc\field.c">
5252
<Filter>Source Files</Filter>
5353
</ClCompile>
54+
<ClCompile Include="..\..\cobc\gentable.c">
55+
<Filter>Source Files</Filter>
56+
</ClCompile>
5457
<ClCompile Include="..\..\cobc\replace.c">
5558
<Filter>Source Files</Filter>
5659
</ClCompile>
@@ -277,4 +280,4 @@
277280
<Filter>Source Files</Filter>
278281
</CustomBuild>
279282
</ItemGroup>
280-
</Project>
283+
</Project>

build_windows/vs2015/cobc.vcxproj.filters

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<ClCompile Include="..\..\cobc\field.c">
5252
<Filter>Source Files</Filter>
5353
</ClCompile>
54+
<ClCompile Include="..\..\cobc\gentable.c">
55+
<Filter>Source Files</Filter>
56+
</ClCompile>
5457
<ClCompile Include="..\..\cobc\replace.c">
5558
<Filter>Source Files</Filter>
5659
</ClCompile>
@@ -277,4 +280,4 @@
277280
<Filter>Source Files</Filter>
278281
</CustomBuild>
279282
</ItemGroup>
280-
</Project>
283+
</Project>

build_windows/vs2017/cobc.vcxproj.filters

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<ClCompile Include="..\..\cobc\field.c">
5252
<Filter>Source Files</Filter>
5353
</ClCompile>
54+
<ClCompile Include="..\..\cobc\gentable.c">
55+
<Filter>Source Files</Filter>
56+
</ClCompile>
5457
<ClCompile Include="..\..\cobc\replace.c">
5558
<Filter>Source Files</Filter>
5659
</ClCompile>
@@ -277,4 +280,4 @@
277280
<Filter>Source Files</Filter>
278281
</CustomBuild>
279282
</ItemGroup>
280-
</Project>
283+
</Project>

build_windows/vs2019/cobc.vcxproj.filters

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<ClCompile Include="..\..\cobc\field.c">
5252
<Filter>Source Files</Filter>
5353
</ClCompile>
54+
<ClCompile Include="..\..\cobc\gentable.c">
55+
<Filter>Source Files</Filter>
56+
</ClCompile>
5457
<ClCompile Include="..\..\cobc\replace.c">
5558
<Filter>Source Files</Filter>
5659
</ClCompile>
@@ -277,4 +280,4 @@
277280
<Filter>Source Files</Filter>
278281
</CustomBuild>
279282
</ItemGroup>
280-
</Project>
283+
</Project>

cobc/cobc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,6 @@ extern unsigned char cb_toupper (const unsigned char);
695695
extern unsigned char cb_tolower (const unsigned char);
696696

697697
/* gentable.c */
698-
extern int gentable (FILE *stream, const char *fromcode, const char *tocode);
698+
extern int gentable (FILE *, const char *, const char *);
699699

700700
#endif /* CB_COBC_H */

cobc/gentable.c

Lines changed: 47 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -20,89 +20,47 @@
2020

2121
#include "config.h"
2222
#include "cobc.h"
23+
#include "tree.h" /* for cb_note */
2324

2425
#include <stdio.h>
2526

2627
#ifdef HAVE_ICONV
2728

2829
#include <iconv.h>
30+
#include <limits.h>
2931
#include <errno.h>
30-
#include <time.h>
3132

32-
#define TABLE_SIZE 256
3333
#define LINE_SIZE 16
3434
#define EBCDIC_SUBST_CHAR (char)0x3F
35-
36-
static int
37-
current_year (void)
38-
{
39-
time_t t = time (NULL);
40-
struct tm *tm = localtime (&t);
41-
return tm->tm_year + 1900;
42-
}
43-
44-
static void
45-
output_license (FILE *stream)
46-
{
47-
fprintf(stream,
48-
"# Copyright (C) %04d Free Software Foundation, Inc.\n"
49-
"# Written by David Declerck.\n"
50-
"#\n"
51-
"# This file is part of the GnuCOBOL runtime.\n"
52-
"#\n"
53-
"# The GnuCOBOL runtime is free software: you can redistribute it\n"
54-
"# and/or modify it under the terms of the GNU Lesser General Public License\n"
55-
"# as published by the Free Software Foundation, either version 3 of the\n"
56-
"# License, or (at your option) any later version.\n"
57-
"#\n"
58-
"# GnuCOBOL is distributed in the hope that it will be useful,\n"
59-
"# but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
60-
"# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
61-
"# GNU General Public License for more details.\n"
62-
"#\n"
63-
"# You should have received a copy of the GNU Lesser General Public License\n"
64-
"# along with GnuCOBOL. If not, see <https://www.gnu.org/licenses/>.\n",
65-
current_year ());
66-
}
35+
#define ASCII_SUBST_CHAR (char)0x1A
6736

6837
static void
6938
output_table (FILE *stream, const char *table)
7039
{
71-
int i;
72-
for (i = 0; i < TABLE_SIZE; ++i) {
40+
unsigned short i;
41+
for (i = 0; i <= UCHAR_MAX; ++i) {
7342
fprintf (stream, "%02X%c", (unsigned char)table[i],
74-
((i+1) % LINE_SIZE == 0) ? '\n' : ' ');
43+
((i + 1) % LINE_SIZE == 0) ? '\n' : ' ');
7544
}
7645
}
7746

78-
static int
79-
translate_single_char (iconv_t ic, char *ebcdic_char, char *ascii_char)
80-
{
81-
size_t ebcdic_size = 1, ascii_size = 1;
82-
size_t res = iconv (ic, &ebcdic_char, &ebcdic_size, &ascii_char, &ascii_size);
83-
iconv (ic, NULL, NULL, NULL, NULL);
84-
return (res == (size_t)0 ? 0 : -1);
85-
}
86-
87-
/* We assume fromcode to be an EBCDIC variant and tocode to be an ASCII-based encoding */
47+
/* Build a pair of EBCDIC/ASCII translation tables using iconv */
8848
int
89-
gentable (FILE *stream, const char *fromcode, const char *tocode)
49+
gentable (FILE *stream, const char *code_ebcdic, const char *code_ascii)
9050
{
91-
char ebcdic[TABLE_SIZE], ascii[TABLE_SIZE] = { 0 };
51+
char ebcdic[UCHAR_MAX + 1], ascii[UCHAR_MAX + 1] = { 0 };
9252
char *ebcdic_ptr = ebcdic, *ascii_ptr = ascii;
93-
size_t ebcdic_size = TABLE_SIZE, ascii_size = TABLE_SIZE;
94-
char ebcdic_subst = EBCDIC_SUBST_CHAR, ascii_subst = 0;
53+
size_t ebcdic_size = UCHAR_MAX + 1, ascii_size = UCHAR_MAX + 1;
54+
unsigned short i, nb_irreversible = 0;
9555
iconv_t ic;
96-
size_t res;
97-
int i, nb_irreversible = 0;
9856

99-
for (i = 0; i < TABLE_SIZE; ++i) {
57+
for (i = 0; i <= UCHAR_MAX; ++i) {
10058
ebcdic[i] = (char)i;
10159
}
10260

103-
ic = iconv_open (tocode, fromcode);
61+
ic = iconv_open (code_ascii, code_ebcdic);
10462
if (ic == (iconv_t)-1) {
105-
fprintf(stderr, "Conversion from %s to %s is not supported by your iconv implementation.\n", fromcode, tocode);
63+
cb_error (_("conversion from %s to %s is not supported by your iconv implementation"), code_ascii, code_ebcdic);
10664
return -1;
10765
}
10866

@@ -111,67 +69,59 @@ gentable (FILE *stream, const char *fromcode, const char *tocode)
11169
and returns the number of such non-reversible conversions performed.
11270
GNU iconv instead sets errno to EILSEQ (unless //TRANSLIT is used).
11371
To cope with these differences, we convert the table character by
114-
character and when encountering an untranslatable character, we
115-
"translate" it to the substitution character translated from the
116-
EBCDIC substitution character (0x3F). */
117-
118-
if (translate_single_char (ic, &ebcdic_subst, &ascii_subst) != 0) {
119-
fprintf(stderr, "Can not convert the substitution character from %s to %s.\n", fromcode, tocode);
120-
return -1;
121-
}
72+
character, and when encountering an untranslatable character, we
73+
map it to the ASCII substitution character (0x1A). This leaves a
74+
number of unused ASCII characters: in the reverse translation table,
75+
those are mapped to the EBCDIC substitution character (0x3F). */
12276

123-
for (i = 0; i < TABLE_SIZE; ++i) {
77+
for (i = 0; i <= UCHAR_MAX; ++i) {
78+
size_t res;
12479
ebcdic_size = 1;
12580
res = iconv (ic, &ebcdic_ptr, &ebcdic_size, &ascii_ptr, &ascii_size);
12681
if (res == (size_t)-1) {
127-
switch (errno) {
82+
if (errno == EILSEQ) {
12883
/* GNU iconv: an untranslatable character was met */
129-
case EILSEQ:
130-
*ascii_ptr = ascii_subst;
131-
++ebcdic_ptr; ++ascii_ptr;
132-
++ebcdic_size; ++ascii_size;
133-
++nb_irreversible;
134-
break;
135-
case EINVAL:
136-
case E2BIG:
137-
default:
138-
fprintf(stderr, "An error occurred after converting %ld characters.\n", (ebcdic_ptr - ebcdic));
139-
iconv_close (ic);
140-
return -1;
84+
*ascii_ptr = ASCII_SUBST_CHAR;
85+
++ebcdic_ptr; ++ascii_ptr;
86+
++ebcdic_size; ++ascii_size;
87+
++nb_irreversible;
88+
} else {
89+
cb_error (_("an error occurred after converting %ld characters"), (ebcdic_ptr - ebcdic));
90+
iconv_close (ic);
91+
return -1;
14192
}
14293
/* POSIX iconv: a substitution was performed */
14394
} else if (res != (size_t)0) {
144-
*(ascii_ptr - 1) = ascii_subst;
145-
nb_irreversible += (int)res;
95+
*(ascii_ptr - 1) = ASCII_SUBST_CHAR;
96+
nb_irreversible += (unsigned short)res;
14697
}
14798
}
14899

149100
iconv_close (ic);
150101

151-
fprintf(stream, "# GnuCOBOL %s/%s translation tables\n\n", fromcode, tocode);
152-
output_license (stream);
102+
fprintf (stream, "# GnuCOBOL %s <-> %s translation tables\n", code_ebcdic, code_ascii);
153103

154-
fprintf(stream, "\n# %s to %s translation table\n\n", fromcode, tocode);
104+
fprintf (stream, "\n# %s to %s translation table\n\n", code_ebcdic, code_ascii);
155105
output_table (stream, ascii);
156106

157-
fprintf(stream, "\n# %s to %s translation table\n\n", tocode, fromcode);
107+
fprintf (stream, "\n# %s to %s translation table\n\n", code_ascii, code_ebcdic);
158108
if (nb_irreversible <= 0) {
159-
fprintf(stream, "# This translation being symmetric, the table is built from the previous one.\n\n");
109+
fprintf (stream, "# This translation being symmetric, the table is built from the previous one.\n\n");
160110
} else {
161111
/* Build the (partial) reverse translation table */
162-
memset(ebcdic, ebcdic_subst, TABLE_SIZE);
163-
for (i = 0; i < TABLE_SIZE; ++i) {
112+
memset (ebcdic, EBCDIC_SUBST_CHAR, UCHAR_MAX + 1);
113+
for (i = 0; i <= UCHAR_MAX; ++i) {
164114
ebcdic[(unsigned char)ascii[i]] = (char)i;
165115
}
166116

167-
/* Restore the substitution character, as it is used
168-
several times, we can't just reverse the table */
169-
ebcdic[(unsigned char)ascii_subst] = ebcdic_subst;
117+
/* Restore the substitution character: as it is used several
118+
times, the loop above probably did not set it correctly */
119+
ebcdic[(unsigned char)ASCII_SUBST_CHAR] = EBCDIC_SUBST_CHAR;
170120

171121
output_table (stream, ebcdic);
172-
fprintf(stream, "\n");
122+
fprintf (stream, "\n");
173123

174-
fprintf(stderr, "Warning: %d non-reversible conversions were performed, you might want to review the generated table.\n", nb_irreversible);
124+
cb_note (COB_WARNOPT_NONE, 0, _("%d non-reversible conversions were performed, you might want to review the generated table"), nb_irreversible);
175125
}
176126

177127
return 0;
@@ -180,13 +130,14 @@ gentable (FILE *stream, const char *fromcode, const char *tocode)
180130
#else
181131

182132
int
183-
gentable (FILE *stream, const char *fromcode, const char *tocode)
133+
gentable (FILE *stream, const char *code_ebcdic, const char *code_ascii)
184134
{
185135
COB_UNUSED (stream);
186-
COB_UNUSED (fromcode);
187-
COB_UNUSED (tocode);
136+
COB_UNUSED (code_ebcdic);
137+
COB_UNUSED (code_ascii);
188138

189-
fprintf(stderr, "Error: GnuCOBOL was not compiled with iconv support; translation table generation is not available.\n");
139+
cb_error (_("runtime is not configured to support %s"), "iconv");
140+
cb_error (_("translation table generation is not available"));
190141

191142
return -1;
192143
}

cobc/help.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ cobc_print_usage_common_options (void)
150150
puts (_(" -MD output dependencies in .d files while compiling"));
151151
puts (_(" -ext <extension> add file extension for resolving COPY"));
152152
puts (_(" -fcopybook-deps output copybook names as dependencies"));
153-
puts (_(" -gentable=<enc-from>,<enc-to>\tbuild an EBCDIC-to-ASCII translation table between the given encodings"));
153+
puts (_(" --gentable=<enc-from>,<enc-to>\toutput to stdout an EBCDIC/ASCII translation table between the given encodings and exit"));
154154
putchar ('\n');
155155
}
156156

0 commit comments

Comments
 (0)