Debian Codegraph @

src/ibmpc.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1990-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1988.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 3 of the
8 License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the Recode Library; see the file `COPYING.LIB'.
17 If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include "common.h"
22 #include "decsteps.h"
23
24 /* Previously, the CR-LF surface was built-in. It is now handled through a
25 separate, optional step. An optimisation might use a flag to trigger old
26 built-in code. For now, depend on a variable preset to false. */
27 static const bool auto_crlf = false;
28
29 #define DOS_EOF 26 /* oldish end of file */
30 #define DOS_CR 13 /* carriage return */
31 #define DOS_LF 10 /* line feed */
32
33 /* Correspondance for IBM PC ruler graphics characters into ASCII graphics
34 approximations. The current principles are:
35
36 - Single horizontal rulers are made up of dashes.
37 - Double horizontal rulers are made up of equal signs.
38 - Both single and double vertical are made up with `|'.
39 - Both upper corners are rounded down with periods.
40 - Lower corners are rounded up with grave/acute accent on left/right.
41 - Other crossing rulers are approximated with plus signs, with exceptions
42 for double horizontal ruler crossings but not at corners: they are
43 equal signs inside a table, and `|' at left or right margin.
44 */
45
46 unsigned char convert_rulers[48] =
47 {
48 '#', /* 176 */
49 '#', /* 177 */
50 '#', /* 178 */
51 '|', /* 179 */
52 '+', /* 180 */
53 '|', /* 181 */
54 '+', /* 182 */
55 '.', /* 183 */
56 '.', /* 184 */
57 '|', /* 185 */
58 '|', /* 186 */
59 '.', /* 187 */
60 '\'', /* 188 */
61 '\'', /* 189 */
62 '\'', /* 190 */
63 '.', /* 191 */
64 '`', /* 192 */
65 '+', /* 193 */
66 '+', /* 194 */
67 '+', /* 195 */
68 '-', /* 196 */
69 '+', /* 197 */
70 '|', /* 198 */
71 '+', /* 199 */
72 '`', /* 200 */
73 '.', /* 201 */
74 '=', /* 202 */
75 '=', /* 203 */
76 '|', /* 204 */
77 '=', /* 205 */
78 '=', /* 206 */
79 '=', /* 207 */
80 '+', /* 208 */
81 '=', /* 209 */
82 '+', /* 210 */
83 '`', /* 211 */
84 '`', /* 212 */
85 '.', /* 213 */
86 '.', /* 214 */
87 '+', /* 215 */
88 '=', /* 216 */
89 '\'', /* 217 */
90 '.', /* 218 */
91 '#', /* 219 */
92 '#', /* 220 */
93 '#', /* 221 */
94 '#', /* 222 */
95 '#', /* 223 */
96 };
97
98 /* Data for IBM PC to ISO Latin-1 code conversions. */
99
100 static struct recode_known_pair known_pairs[] =
101 {
102 { 20, 182}, /* pilcrow sign */
103 { 21, 167}, /* section sign */
104
105 {128, 199}, /* capital letter C with cedilla */
106 {129, 252}, /* small letter u with diaeresis */
107 {130, 233}, /* small letter e with acute accent */
108 {131, 226}, /* small letter a with circumflex accent */
109 {132, 228}, /* small letter a with diaeresis */
110 {133, 224}, /* small letter a with grave accent */
111 {134, 229}, /* small letter a with ring above */
112 {135, 231}, /* small letter c with cedilla */
113 {136, 234}, /* small letter e with circumflex accent */
114 {137, 235}, /* small letter e with diaeresis */
115 {138, 232}, /* small letter e with grave accent */
116 {139, 239}, /* small letter i with diaeresis */
117 {140, 238}, /* small letter i with circumflex accent */
118 {141, 236}, /* small letter i with grave accent */
119 {142, 196}, /* capital letter A with diaeresis */
120 {143, 197}, /* capital letter A with ring above */
121 {144, 201}, /* capital letter E with acute accent */
122 {145, 230}, /* small ligature a with e */
123 {146, 198}, /* capital ligature A with E */
124 {147, 244}, /* small letter o with circumblex accent */
125 {148, 246}, /* small letter o with diaeresis */
126 {149, 242}, /* small letter o with grave accent */
127 {150, 251}, /* small letter u with circumflex accent */
128 {151, 249}, /* small letter u with grave accent */
129 {152, 255}, /* small letter y with diaeresis */
130 {153, 214}, /* capital letter O with diaeresis */
131 {154, 220}, /* capital letter U with diaeresis */
132 {155, 162}, /* cent sign */
133 {156, 163}, /* pound sign */
134 {157, 165}, /* yen sign */
135
136 {160, 225}, /* small letter a with acute accent */
137 {161, 237}, /* small letter i with acute accent */
138 {162, 243}, /* small letter o with acute accent */
139 {163, 250}, /* small letter u with acute accent */
140 {164, 241}, /* small letter n with tilde */
141 {165, 209}, /* capital letter N with tilde */
142 {166, 170}, /* feminine ordinal indicator */
143 {167, 186}, /* masculine ordinal indicator */
144 {168, 191}, /* inverted question mark */
145
146 {170, 172}, /* not sign */
147 {171, 189}, /* vulgar fraction one half */
148 {172, 188}, /* vulgar fraction one quarter */
149 {173, 161}, /* inverted exclamation mark */
150 {174, 171}, /* left angle quotation mark */
151 {175, 187}, /* right angle quotation mark */
152
153 {225, 223}, /* small german letter sharp s */
154
155 {230, 181}, /* small Greek letter mu micro sign */
156
157 {241, 177}, /* plus-minus sign */
158
159 {246, 247}, /* division sign */
160
161 {248, 176}, /* degree sign */
162
163 {250, 183}, /* middle dot */
164
165 {253, 178}, /* superscript two */
166
167 {255, 160}, /* no-break space */
168 };
169 #define NUMBER_OF_PAIRS \
170 (sizeof (known_pairs) / sizeof (struct recode_known_pair))
171
172 static bool
173 transform_latin1_ibmpc (RECODE_SUBTASK subtask)
174 {
175 if (subtask->step->fallback_routine == recode_reversibility)
176 {
177 const unsigned char *table
178 = (const unsigned char *) subtask->step->step_table;
179 int input_char;
180
181 while (input_char = recode_get_byte (subtask), input_char != EOF)
182 if (input_char == '\n')
183 {
184 recode_put_byte (DOS_CR, subtask);
185 recode_put_byte (DOS_LF, subtask);
186 }
187 else
188 recode_put_byte (table[input_char], subtask);
189 }
190 else
191 {
192 const char *const *table
193 = (const char *const *) subtask->step->step_table;
194 int input_char;
195
196 while (input_char = recode_get_byte (subtask), input_char != EOF)
197 if (input_char == '\n')
198 {
199 recode_put_byte (DOS_CR, subtask);
200 recode_put_byte (DOS_LF, subtask);
201 }
202 else if (table[input_char])
203 recode_put_byte (*table[input_char], subtask);
204 else
205 RETURN_IF_NOGO (RECODE_UNTRANSLATABLE, subtask);
206 }
207 SUBTASK_RETURN (subtask);
208 }
209
210 static bool
211 transform_ibmpc_latin1 (RECODE_SUBTASK subtask)
212 {
213 if (subtask->step->fallback_routine == recode_reversibility)
214 {
215 const unsigned char *table
216 = (const unsigned char *) subtask->step->step_table;
217 int input_char = recode_get_byte (subtask);
218
219 while (input_char != EOF)
220 switch (input_char)
221 {
222 case DOS_EOF:
223 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
224 input_char = EOF;
225 break;
226
227 case DOS_CR:
228 input_char = recode_get_byte (subtask);
229 if (input_char == DOS_LF)
230 {
231 recode_put_byte ('\n', subtask);
232 input_char = recode_get_byte (subtask);
233 }
234 else
235 recode_put_byte (table[DOS_CR], subtask);
236 break;
237
238 case DOS_LF:
239 RETURN_IF_NOGO (RECODE_AMBIGUOUS_OUTPUT, subtask);
240 FALLTHROUGH;
241
242 default:
243 recode_put_byte (table[input_char], subtask);
244 input_char = recode_get_byte (subtask);
245 }
246 }
247 else
248 {
249 const char *const *table
250 = (const char *const *) subtask->step->step_table;
251 int input_char = recode_get_byte (subtask);
252
253 while (input_char != EOF)
254 switch (input_char)
255 {
256 case DOS_EOF:
257 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
258 input_char = EOF;
259 break;
260
261 case DOS_CR:
262 input_char = recode_get_byte (subtask);
263 if (input_char == DOS_LF)
264 {
265 recode_put_byte ('\n', subtask);
266 input_char = recode_get_byte (subtask);
267 }
268 else if (table[DOS_CR])
269 recode_put_byte (*table[DOS_CR], subtask);
270 else
271 RETURN_IF_NOGO (RECODE_UNTRANSLATABLE, subtask);
272 break;
273
274 case DOS_LF:
275 RETURN_IF_NOGO (RECODE_AMBIGUOUS_OUTPUT, subtask);
276 FALLTHROUGH;
277
278 default:
279 if (table[input_char])
280 recode_put_byte (*table[input_char], subtask);
281 else
282 RETURN_IF_NOGO (RECODE_UNTRANSLATABLE, subtask);
283 input_char = recode_get_byte (subtask);
284 }
285 }
286
287 SUBTASK_RETURN (subtask);
288 }
289
290 static bool
291 init_latin1_ibmpc (RECODE_STEP step,
292 RECODE_CONST_REQUEST request,
293 RECODE_CONST_OPTION_LIST before_options,
294 RECODE_CONST_OPTION_LIST after_options)
295 {
296 if (before_options || after_options)
297 return false;
298
299 if (!recode_complete_pairs (request->outer, step,
300 known_pairs, NUMBER_OF_PAIRS, true, true))
301 return false;
302
303 if (auto_crlf)
304 step->transform_routine = transform_latin1_ibmpc;
305 else if (step->fallback_routine == recode_reversibility)
306 step->transform_routine = recode_transform_byte_to_byte;
307 else
308 step->transform_routine = recode_transform_byte_to_variable;
309
310 return true;
311 }
312
313 static bool
314 init_ibmpc_latin1 (RECODE_STEP step,
315 RECODE_CONST_REQUEST request,
316 RECODE_CONST_OPTION_LIST before_options,
317 RECODE_CONST_OPTION_LIST after_options)
318 {
319 RECODE_OUTER outer = request->outer;
320 unsigned char *table;
321
322 if (before_options || after_options)
323 return false;
324
325 if (!recode_complete_pairs (outer, step,
326 known_pairs, NUMBER_OF_PAIRS, true, false))
327 return false;
328
329 if (auto_crlf)
330 step->transform_routine = transform_ibmpc_latin1;
331 else if (step->fallback_routine == recode_reversibility)
332 step->transform_routine = recode_transform_byte_to_byte;
333 else
334 step->transform_routine = recode_transform_byte_to_variable;
335
336 /* FIXME: Allow ascii_graphics even with strict mapping. Reported by
337 David E. A. Wilson <[email protected]>. */
338
339 if (request->ascii_graphics)
340 {
341 if (!ALLOC (table, 256, unsigned char))
342 return false;
343 memcpy (table, step->step_table, 256);
344 memcpy (table + 176, convert_rulers, 48);
345 free (step->step_table);
346 step->step_table = table;
347 }
348
349 return true;
350 }
351
352 bool
353 module_ibmpc (RECODE_OUTER outer)
354 {
355 RECODE_ALIAS alias;
356
357 if (!recode_declare_single (outer, "Latin-1", "IBM-PC",
358 outer->quality_byte_to_variable,
359 init_latin1_ibmpc, transform_latin1_ibmpc))
360 return false;
361 if (!recode_declare_single (outer, "IBM-PC", "Latin-1",
362 outer->quality_variable_to_variable,
363 init_ibmpc_latin1, transform_ibmpc_latin1))
364 return false;
365
366 if (alias = recode_declare_alias (outer, "IBM-PC", "IBM-PC"), !alias)
367 return false;
368 if (!recode_declare_implied_surface (outer, alias, outer->crlf_surface))
369 return false;
370
371 if (alias = recode_declare_alias (outer, "dos", "IBM-PC"), !alias)
372 return false;
373 if (!recode_declare_implied_surface (outer, alias, outer->crlf_surface))
374 return false;
375
376 if (alias = recode_declare_alias (outer, "MSDOS", "IBM-PC"), !alias)
377 return false;
378 if (!recode_declare_implied_surface (outer, alias, outer->crlf_surface))
379 return false;
380
381 if (alias = recode_declare_alias (outer, "pc", "IBM-PC"), !alias)
382 return false;
383 if (!recode_declare_implied_surface (outer, alias, outer->crlf_surface))
384 return false;
385
386 return true;
387 }
388
389 void
390 delmodule_ibmpc (_GL_UNUSED RECODE_OUTER outer)
391 {
392 }