Debian Codegraph @

libgcobol/charmaps.h

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of the Symas Corporation nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef CHARMAPS_H
32 #define CHARMAPS_H
33
34 #include <unistd.h>
35
36 /* There are four distinct codeset domains in the COBOL compiler.
37 *
38 * First is the codeset of the console. Established by looking at what
39 * setlocale() reports, this can be either UTF-8 or some ASCII based code
40 * page. (We assume CP1252). Data coming from the console or the system,
41 * ACCEPT statements; redirected console input, getenv() and other system
42 * calls are in the "console" domain.
43 *
44 * Second is the internal single-byte-coded codeset of the data, in memory,
45 * being manipulated by the generated code of the cobol executable. The actual
46 * codeset of "internal" is either EBCDIC (in the form of Code Page 1140 or
47 * ASCII (Code Page 1252)
48 *
49 * Third is the C++ source code of the GCOBOL compiler; this comment is
50 * in that environment. We neither know, nor care, if this code is encoded in
51 * in UTF-8 (as is probable, in these enlighted days of 2022) or something like
52 * Code Page1252. We are going to regard it as "ascii" under the
53 * assumption that there is no reason for any character in the compiler's
54 * source code to have a code point outside of the plain vanilla 0x20 through
55 * 0x7F range.
56 *
57 * Fourth is the "raw" COBOL source code that is the input to the GCOBOL
58 * compiler. This domain can be either UTF-8 or something like CodePage1252.
59 * Which encoding is relevant; The literal string MOVE "<euro>1234" is seven
60 * bytes long in UTF-8, and five bytes long in CP1252. We start with an
61 * assumption that it is UTF-8 and switch to CP1252 upon encountering a byte
62 * sequence with values above 0x80 that can't be UTF-8. We have provision for
63 * forcing it to be one or the other. Codepoints in that domain are referenced
64 * as "raw". Codepoint in the "raw" domain don't last long; they are be
65 * converted to either "ascii" or "internal" early on, as necessary.
66 */
67
68
69 /* Notes on character codesets:
70
71 This library is implemented to handle "native" codesets of either ASCII (in
72 the form of a single-byte-coded codeset like code page 1252) or EBCDIC (in
73 the form of a single-byte-coded codeset like code page 1140).
74
75 This C/C++ source code, however, is assumed to be an ASCII-based codeset,
76 so that a character constant like a space is assumed to encode as 0x20.
77
78 Furthermore, we assume that the codeset of the COBOL source code being
79 compiled is also ASCII-based, even if it is actually UTF-8. Said another
80 way, characters encoded between zero and 127 are regarded as ASCII.
81
82 This means that we are not going to try to compile EBCDIC COBOL source code;
83 any such will have to be externally converted to ASCII before feeding it
84 through this compiler on an ASCII based Linux system.
85
86 This situation is rife for confusion here in the source code for the
87 library.
88
89 To help reduce that confusion, we are going to eschew character constants
90 in the C/C++ source code. Instead, we use symbolic versions. In general,
91 "source_space" means 0x20, while "internal_space" will be either 0x20
92 when using the ASCII-based native codeset, or it will be 0x40 when using
93 the EBCDIC-based native codeset.
94
95 Maintaining one's sanity while learning and working with this C/C++ code
96 will require a firm grip on context. You'll have to keep track of whether
97 the character is being used to analyze the ASCII-based COBOL source, or
98 whether the character in question is part of the native COBOL cobol data
99 that is being analyzed or generated.
100
101 For example, when a PICTURE string has in it a source_nine, the generated
102 result in the variable is based on character_zero.
103
104 Stay alert! */
105
106
107 extern bool __gg__ebcdic_codeset_in_use;
108 #define internal_is_ebcdic (__gg__ebcdic_codeset_in_use)
109
110 extern unsigned short const *__gg__internal_codeset_map;
111
112 #define NULLCH ('\0')
113 #define DEGENERATE_HIGH_VALUE 0xFF
114 #define DEGENERATE_LOW_VALUE 0x00
115
116 #define ascii_A ((uint8_t)('A'))
117 #define ascii_B ((uint8_t)('B'))
118 #define ascii_C ((uint8_t)('C'))
119 #define ascii_D ((uint8_t)('D'))
120 #define ascii_E ((uint8_t)('E'))
121 #define ascii_F ((uint8_t)('F'))
122 #define ascii_G ((uint8_t)('G'))
123 #define ascii_H ((uint8_t)('H'))
124 #define ascii_I ((uint8_t)('I'))
125 #define ascii_J ((uint8_t)('J'))
126 #define ascii_K ((uint8_t)('K'))
127 #define ascii_L ((uint8_t)('L'))
128 #define ascii_M ((uint8_t)('M'))
129 #define ascii_N ((uint8_t)('N'))
130 #define ascii_O ((uint8_t)('O'))
131 #define ascii_P ((uint8_t)('P'))
132 #define ascii_Q ((uint8_t)('Q'))
133 #define ascii_R ((uint8_t)('R'))
134 #define ascii_S ((uint8_t)('S'))
135 #define ascii_T ((uint8_t)('T'))
136 #define ascii_U ((uint8_t)('U'))
137 #define ascii_V ((uint8_t)('V'))
138 #define ascii_W ((uint8_t)('W'))
139 #define ascii_X ((uint8_t)('X'))
140 #define ascii_Y ((uint8_t)('Y'))
141 #define ascii_Z ((uint8_t)('Z'))
142 #define ascii_a ((uint8_t)('a'))
143 #define ascii_b ((uint8_t)('b'))
144 #define ascii_c ((uint8_t)('c'))
145 #define ascii_d ((uint8_t)('d'))
146 #define ascii_e ((uint8_t)('e'))
147 #define ascii_f ((uint8_t)('f'))
148 #define ascii_g ((uint8_t)('g'))
149 #define ascii_h ((uint8_t)('h'))
150 #define ascii_i ((uint8_t)('i'))
151 #define ascii_j ((uint8_t)('j'))
152 #define ascii_k ((uint8_t)('k'))
153 #define ascii_l ((uint8_t)('l'))
154 #define ascii_m ((uint8_t)('m'))
155 #define ascii_n ((uint8_t)('n'))
156 #define ascii_o ((uint8_t)('o'))
157 #define ascii_p ((uint8_t)('p'))
158 #define ascii_q ((uint8_t)('q'))
159 #define ascii_r ((uint8_t)('r'))
160 #define ascii_s ((uint8_t)('s'))
161 #define ascii_t ((uint8_t)('t'))
162 #define ascii_u ((uint8_t)('u'))
163 #define ascii_v ((uint8_t)('v'))
164 #define ascii_w ((uint8_t)('w'))
165 #define ascii_x ((uint8_t)('x'))
166 #define ascii_y ((uint8_t)('y'))
167 #define ascii_z ((uint8_t)('z'))
168 #define ascii_space ((uint8_t)(' '))
169 #define ascii_zero ((uint8_t)('0'))
170 #define ascii_0 ((uint8_t)('0'))
171 #define ascii_1 ((uint8_t)('1'))
172 #define ascii_2 ((uint8_t)('2'))
173 #define ascii_3 ((uint8_t)('3'))
174 #define ascii_4 ((uint8_t)('4'))
175 #define ascii_5 ((uint8_t)('5'))
176 #define ascii_6 ((uint8_t)('6'))
177 #define ascii_7 ((uint8_t)('7'))
178 #define ascii_8 ((uint8_t)('8'))
179 #define ascii_9 ((uint8_t)('9'))
180 #define ascii_nine ((uint8_t)('9'))
181 #define ascii_period ((uint8_t)('.'))
182 #define ascii_colon ((uint8_t)(':'))
183 #define ascii_comma ((uint8_t)(','))
184 #define ascii_dollar_sign ((uint8_t)('$'))
185 #define ascii_dquote ((uint8_t)('"'))
186 #define ascii_oparen ((uint8_t)('('))
187 #define ascii_caret ((uint8_t)('^'))
188 #define ascii_slash ((uint8_t)('/'))
189 #define ascii_plus ((uint8_t)('+'))
190 #define ascii_minus ((uint8_t)('-'))
191 #define ascii_hyphen ((uint8_t)('-'))
192 #define ascii_underscore ((uint8_t)('_'))
193 #define ascii_asterisk ((uint8_t)('*'))
194 #define ascii_query ((uint8_t)('?'))
195 #define ascii_cr ((uint8_t)('\r'))
196 #define ascii_ff ((uint8_t)('\f'))
197 #define ascii_newline ((uint8_t)('\n'))
198 #define ascii_return ((uint8_t)('\r'))
199
200 #define internal_space ((uint8_t)__gg__internal_codeset_map[ascii_space])
201 #define internal_zero ((uint8_t)__gg__internal_codeset_map[ascii_zero])
202 #define internal_period ((uint8_t)__gg__internal_codeset_map[ascii_period])
203 #define internal_comma ((uint8_t)__gg__internal_codeset_map[ascii_comma])
204 #define internal_dquote ((uint8_t)__gg__internal_codeset_map[ascii_dquote])
205 #define internal_asterisk ((uint8_t)__gg__internal_codeset_map[ascii_asterisk])
206 #define internal_plus ((uint8_t)__gg__internal_codeset_map[ascii_plus])
207 #define internal_minus ((uint8_t)__gg__internal_codeset_map[ascii_minus])
208 #define internal_cr ((uint8_t)__gg__internal_codeset_map[ascii_cr])
209 #define internal_ff ((uint8_t)__gg__internal_codeset_map[ascii_ff])
210 #define internal_newline ((uint8_t)__gg__internal_codeset_map[ascii_newline])
211 #define internal_return ((uint8_t)__gg__internal_codeset_map[ascii_return])
212 #define internal_0 ((uint8_t)__gg__internal_codeset_map[ascii_0])
213 #define internal_1 ((uint8_t)__gg__internal_codeset_map[ascii_1])
214 #define internal_2 ((uint8_t)__gg__internal_codeset_map[ascii_2])
215 #define internal_3 ((uint8_t)__gg__internal_codeset_map[ascii_3])
216 #define internal_4 ((uint8_t)__gg__internal_codeset_map[ascii_4])
217 #define internal_5 ((uint8_t)__gg__internal_codeset_map[ascii_5])
218 #define internal_6 ((uint8_t)__gg__internal_codeset_map[ascii_6])
219 #define internal_7 ((uint8_t)__gg__internal_codeset_map[ascii_7])
220 #define internal_8 ((uint8_t)__gg__internal_codeset_map[ascii_8])
221 #define internal_9 ((uint8_t)__gg__internal_codeset_map[ascii_9])
222 #define internal_colon ((uint8_t)__gg__internal_codeset_map[ascii_colon])
223 #define internal_query ((uint8_t)__gg__internal_codeset_map[ascii_query])
224 #define internal_A ((uint8_t)__gg__internal_codeset_map[ascii_A])
225 #define internal_B ((uint8_t)__gg__internal_codeset_map[ascii_B])
226 #define internal_C ((uint8_t)__gg__internal_codeset_map[ascii_C])
227 #define internal_D ((uint8_t)__gg__internal_codeset_map[ascii_D])
228 #define internal_E ((uint8_t)__gg__internal_codeset_map[ascii_E])
229 #define internal_F ((uint8_t)__gg__internal_codeset_map[ascii_F])
230 #define internal_G ((uint8_t)__gg__internal_codeset_map[ascii_G])
231 #define internal_H ((uint8_t)__gg__internal_codeset_map[ascii_H])
232 #define internal_I ((uint8_t)__gg__internal_codeset_map[ascii_I])
233 #define internal_J ((uint8_t)__gg__internal_codeset_map[ascii_J])
234 #define internal_K ((uint8_t)__gg__internal_codeset_map[ascii_K])
235 #define internal_L ((uint8_t)__gg__internal_codeset_map[ascii_L])
236 #define internal_M ((uint8_t)__gg__internal_codeset_map[ascii_M])
237 #define internal_N ((uint8_t)__gg__internal_codeset_map[ascii_N])
238 #define internal_O ((uint8_t)__gg__internal_codeset_map[ascii_O])
239 #define internal_P ((uint8_t)__gg__internal_codeset_map[ascii_P])
240 #define internal_Q ((uint8_t)__gg__internal_codeset_map[ascii_Q])
241 #define internal_R ((uint8_t)__gg__internal_codeset_map[ascii_R])
242 #define internal_S ((uint8_t)__gg__internal_codeset_map[ascii_S])
243 #define internal_T ((uint8_t)__gg__internal_codeset_map[ascii_T])
244 #define internal_U ((uint8_t)__gg__internal_codeset_map[ascii_U])
245 #define internal_V ((uint8_t)__gg__internal_codeset_map[ascii_V])
246 #define internal_W ((uint8_t)__gg__internal_codeset_map[ascii_W])
247 #define internal_X ((uint8_t)__gg__internal_codeset_map[ascii_X])
248 #define internal_Y ((uint8_t)__gg__internal_codeset_map[ascii_Y])
249 #define internal_Z ((uint8_t)__gg__internal_codeset_map[ascii_Z])
250 #define internal_a ((uint8_t)__gg__internal_codeset_map[ascii_a])
251 #define internal_b ((uint8_t)__gg__internal_codeset_map[ascii_b])
252 #define internal_c ((uint8_t)__gg__internal_codeset_map[ascii_c])
253 #define internal_d ((uint8_t)__gg__internal_codeset_map[ascii_d])
254 #define internal_e ((uint8_t)__gg__internal_codeset_map[ascii_e])
255 #define internal_f ((uint8_t)__gg__internal_codeset_map[ascii_f])
256 #define internal_g ((uint8_t)__gg__internal_codeset_map[ascii_g])
257 #define internal_h ((uint8_t)__gg__internal_codeset_map[ascii_h])
258 #define internal_i ((uint8_t)__gg__internal_codeset_map[ascii_i])
259 #define internal_j ((uint8_t)__gg__internal_codeset_map[ascii_j])
260 #define internal_k ((uint8_t)__gg__internal_codeset_map[ascii_k])
261 #define internal_l ((uint8_t)__gg__internal_codeset_map[ascii_l])
262 #define internal_m ((uint8_t)__gg__internal_codeset_map[ascii_m])
263 #define internal_n ((uint8_t)__gg__internal_codeset_map[ascii_n])
264 #define internal_o ((uint8_t)__gg__internal_codeset_map[ascii_o])
265 #define internal_p ((uint8_t)__gg__internal_codeset_map[ascii_p])
266 #define internal_q ((uint8_t)__gg__internal_codeset_map[ascii_q])
267 #define internal_r ((uint8_t)__gg__internal_codeset_map[ascii_r])
268 #define internal_s ((uint8_t)__gg__internal_codeset_map[ascii_s])
269 #define internal_t ((uint8_t)__gg__internal_codeset_map[ascii_t])
270 #define internal_u ((uint8_t)__gg__internal_codeset_map[ascii_u])
271 #define internal_v ((uint8_t)__gg__internal_codeset_map[ascii_v])
272 #define internal_w ((uint8_t)__gg__internal_codeset_map[ascii_w])
273 #define internal_x ((uint8_t)__gg__internal_codeset_map[ascii_x])
274 #define internal_y ((uint8_t)__gg__internal_codeset_map[ascii_y])
275 #define internal_z ((uint8_t)__gg__internal_codeset_map[ascii_z])
276
277
278 enum text_device_t
279 {
280 td_default_e,
281 td_sourcecode_e,
282 td_console_e,
283 };
284
285 enum text_codeset_t
286 {
287 cs_default_e,
288 cs_utf8_e,
289 cs_cp1252_e,
290 cs_cp1140_e
291 };
292
293
294 extern unsigned char __gg__data_space[1] ;
295 extern unsigned char __gg__data_low_values[1] ;
296 extern unsigned char __gg__data_zeros[1] ;
297 extern unsigned char __gg__data_high_values[1] ;
298 extern unsigned char __gg__data_quotes[1] ;
299 extern unsigned char __gg__data_upsi_0[2] ;
300 extern short __gg__data_return_code ;
301
302 // These are the various hardcoded tables used for conversions.
303 extern const unsigned short __gg__one_to_one_values[256];
304 extern const unsigned short __gg__cp1252_to_cp1140_values[256];
305 extern const unsigned short __gg__cp1140_to_cp1252_values[256];
306
307 // These are the two standard collations.
308 extern const unsigned short __gg__cp1252_to_ebcdic_collation[256];
309 extern const unsigned short __gg__ebcdic_to_cp1252_collation[256];
310
311 // As described above, we have a number of operations we need to accomplish. But
312 // the actual routines are dependent on whether EBCDIC or ASCII is in use. We
313 // implement that by having a function pointer for each function; those pointers
314 // are established when the __gg__ebcdic_codeset_in_use variable is established.
315
316 // These routines convert a single ASCII character to either ASCII or EBCDIC
317
318 extern "C"
319 char __gg__ascii_to_ascii_chr(char ch);
320 extern "C"
321 char __gg__ascii_to_ebcdic_chr(char ch);
322 extern "C"
323 char (*__gg__ascii_to_internal_chr)(char);
324 #define ascii_to_internal(a) ((*__gg__ascii_to_internal_chr)(a))
325
326 extern "C"
327 void __gg__ascii_to_ascii(char *str, size_t length);
328 extern "C"
329 void __gg__ascii_to_ebcdic(char *str, size_t length);
330 extern "C"
331 void (*__gg__ascii_to_internal_str)(char *str, size_t length);
332 #define ascii_to_internal_str(a, b) ((*__gg__ascii_to_internal_str)((a), (b)))
333
334 extern "C"
335 char *__gg__raw_to_ascii(char **dest, size_t *dest_size, const char *str, size_t length);
336 extern "C"
337 char *__gg__raw_to_ebcdic(char **dest, size_t *dest_size, const char *in, size_t length);
338 extern "C"
339 char *(*__gg__raw_to_internal)(char **dest, size_t *dest_length, const char *in, size_t length);
340 #define raw_to_internal(a, b, c, d) ((*__gg__raw_to_internal)((a), (b), (c), (d)))
341
342 extern "C"
343 char *__gg__ascii_to_console(char **dest, size_t *dest_size, char const * const str, const size_t length);
344 extern "C"
345 char *__gg__ebcdic_to_console(char **dest, size_t *dest_size, char const * const str, const size_t length);
346 extern "C"
347 char *(*__gg__internal_to_console_cm)(char **dest, size_t *dest_size, const char *in, size_t length);
348 #define internal_to_console(a, b, c, d) ((*__gg__internal_to_console_cm)((a), (b), (c), (d)))
349
350 extern "C"
351 void __gg__console_to_ascii(char * const str, size_t length);
352 extern "C"
353 void __gg__console_to_ebcdic(char * const str, size_t length);
354 extern "C"
355 void (*__gg__console_to_internal_cm)(char * const str, size_t length);
356 #define console_to_internal(a, b) ((*__gg__console_to_internal_cm)((a), (b)))
357
358 extern "C"
359 void __gg__ebcdic_to_ascii(char *str, const size_t length);
360 extern "C"
361 void (*__gg__internal_to_ascii)(char *str, size_t length);
362 #define internal_to_ascii(a, b) ((*__gg__internal_to_ascii)((a), (b)))
363
364 extern "C" void __gg__set_internal_codeset(int use_ebcdic);
365
366 extern "C"
367 void __gg__text_conversion_override(text_device_t device,
368 text_codeset_t codeset);
369
370 #endif