Debian Codegraph @

src/mixed.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1990-2023 Free Software Foundation, Inc.
3 François Pinard <[email protected]>, 1990.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any later
8 version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 along with this program; if not, see <https://www.gnu.org/licenses/>.
17 */
18
19 #include "config.h"
20 #include "common.h"
21
22 /*================================\
23 | Mixed charset file processors. |
24 \================================*/
25
26 struct mixed
27 {
28 /* File names saved from original task. */
29 const char *input_name;
30 const char *output_name;
31
32 /* Subtask, from input directly to either output or in memory buffer. */
33 struct recode_subtask subtask;
34
35 /* In memory buffer. */
36 struct recode_read_write_text buffer;
37 };
38
39 static bool
40 open_mixed (struct mixed *mixed, RECODE_TASK task)
41 {
42 mixed->input_name = task->input.name;
43 mixed->output_name = task->output.name;
44 task->byte_order_mark = false;
45
46 /* Open both files ourselves. */
47
48 if (!*(task->input.name))
49 task->input.file = stdin;
50 else if (task->input.file = fopen (mixed->input_name, "rb"),
51 !task->input.file)
52 {
53 recode_perror (NULL, "fopen (%s)", task->input.name);
54 return false;
55 }
56 task->input.name = NULL;
57
58 if (!*(task->output.name))
59 task->output.file = stdout;
60 else if (task->output.file = fopen (mixed->output_name, "wb"),
61 !task->output.file)
62 {
63 recode_perror (NULL, "fopen (%s)", task->output.name);
64 fclose (task->input.file);
65 return false;
66 }
67 task->output.name = NULL;
68
69 /* Prepare for dynamic plumbing copy. */
70
71 memset (&mixed->subtask, 0, sizeof (struct recode_subtask));
72 mixed->subtask.task = task;
73 mixed->subtask.input = task->input;
74 mixed->subtask.output = task->output;
75 memset (&mixed->buffer, 0, sizeof (struct recode_read_write_text));
76
77 return true;
78 }
79
80 static bool
81 close_mixed (struct mixed *mixed)
82 {
83 /* Clean up. */
84
85 if (mixed->subtask.task->input.file
86 && fclose (mixed->subtask.task->input.file) != 0)
87 return false;
88 if (mixed->subtask.task->output.file
89 && fclose (mixed->subtask.task->output.file) != 0)
90 return false;
91 return true;
92 }
93
94 static void
95 start_accumulation (struct mixed *mixed)
96 {
97 mixed->buffer.cursor = mixed->buffer.buffer;
98 mixed->subtask.output = mixed->buffer;
99 }
100
101 static bool
102 recode_accumulated (struct mixed *mixed)
103 {
104 RECODE_TASK task = mixed->subtask.task;
105 struct recode_read_only_text saved_input = task->input;
106 bool status;
107
108 mixed->buffer = mixed->subtask.output;
109
110 task->input.file = NULL;
111 task->input.buffer = mixed->buffer.buffer;
112 task->input.cursor = mixed->buffer.buffer;
113 task->input.limit = mixed->buffer.cursor;
114
115 status = recode_perform_task (task);
116
117 task->input = saved_input;
118 mixed->subtask.output = task->output;
119 return status;
120 }
121
122 /*-----------------------------------------------------------------------.
123 | Transform only strings or comments in an C source, expected in ASCII. |
124 `-----------------------------------------------------------------------*/
125
126 bool
127 recode_transform_c_source (RECODE_TASK task)
128 {
129 struct mixed mixed;
130 int character;
131
132 if (!open_mixed (&mixed, task))
133 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
134 else
135 {
136 character = recode_get_byte (&mixed.subtask);
137 while (character != EOF)
138 switch (character)
139 {
140 case '\'':
141 /* Skip character constant, while copying it untranslated. */
142
143 recode_put_byte ('\'', &mixed.subtask);
144 character = recode_get_byte (&mixed.subtask);
145
146 if (character == EOF)
147 {
148 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
149 break;
150 }
151
152 if (character == '\\')
153 {
154 recode_put_byte ('\\', &mixed.subtask);
155 character = recode_get_byte (&mixed.subtask);
156 if (character == EOF)
157 {
158 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
159 break;
160 }
161 recode_put_byte (character, &mixed.subtask);
162 character = recode_get_byte (&mixed.subtask);
163 }
164
165 if (character == '\'')
166 {
167 recode_put_byte ('\'', &mixed.subtask);
168 character = recode_get_byte (&mixed.subtask);
169 }
170 else
171 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
172 break;
173
174 case '"':
175 /* Copy the string, translated. */
176
177 recode_put_byte ('"', &mixed.subtask);
178 character = recode_get_byte (&mixed.subtask);
179
180 /* Read in string. */
181
182 start_accumulation (&mixed);
183
184 while (true)
185 {
186 if (character == EOF)
187 {
188 recode_accumulated (&mixed);
189 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
190 break;
191 }
192 if (character == '"')
193 break;
194
195 if (character == '\\')
196 {
197 recode_put_byte ('\\', &mixed.subtask);
198 character = recode_get_byte (&mixed.subtask);
199 if (character == EOF)
200 {
201 recode_accumulated (&mixed);
202 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
203 break;
204 }
205 }
206 recode_put_byte (character, &mixed.subtask);
207 character = recode_get_byte (&mixed.subtask);
208 }
209 if (character == EOF)
210 break;
211
212 /* Translate string and dump it. */
213
214 if (!recode_accumulated (&mixed))
215 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
216 recode_put_byte ('"', &mixed.subtask);
217 character = recode_get_byte (&mixed.subtask);
218 break;
219
220 case '/':
221 recode_put_byte ('/', &mixed.subtask);
222 character = recode_get_byte (&mixed.subtask);
223 if (character == EOF)
224 break;
225 if (character == '*')
226 {
227 /* Copy the comment, translated. */
228
229 recode_put_byte ('*', &mixed.subtask);
230 character = recode_get_byte (&mixed.subtask);
231
232 /* Read in comment. */
233
234 start_accumulation (&mixed);
235 while (true)
236 {
237 if (character == EOF)
238 {
239 recode_accumulated (&mixed);
240 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
241 break;
242 }
243 if (character == '*')
244 {
245 character = recode_get_byte (&mixed.subtask);
246 if (character == EOF)
247 {
248 recode_accumulated (&mixed);
249 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
250 recode_put_byte ('*', &mixed.subtask);
251 break;
252 }
253 if (character == '/')
254 break;
255 recode_put_byte ('*', &mixed.subtask);
256 }
257 else
258 {
259 recode_put_byte (character, &mixed.subtask);
260 character = recode_get_byte (&mixed.subtask);
261 }
262 }
263
264 if (character == EOF)
265 break;
266
267 /* Translate comment and dump it. */
268
269 if (!recode_accumulated (&mixed))
270 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
271 recode_put_byte ('*', &mixed.subtask);
272 recode_put_byte ('/', &mixed.subtask);
273 character = recode_get_byte (&mixed.subtask);
274 }
275 break;
276
277 default:
278 recode_put_byte (character, &mixed.subtask);
279 character = recode_get_byte (&mixed.subtask);
280 break;
281 }
282 }
283
284 if (!close_mixed (&mixed))
285 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
286 SUBTASK_RETURN (&mixed.subtask);
287 }
288
289 /*------------------------------------------------------------------------.
290 | Transform only strings or comments in an PO source, expected in ASCII. |
291 `------------------------------------------------------------------------*/
292
293 /* There is a limitation to -Spo: if Recode converts some `msgstr' in a way
294 that might produce quotes (or backslashes), these should then be requoted.
295 Doing this would then also require to fully unquote the original `msgstr'
296 string. But it seems that such a need does not occur in most cases I can
297 imagine as practical, as the ASCII subset is generally invariant under
298 recoding. My guess is that we should wait for someone to report the bug
299 with a real case, to believe it is worth adding the complexity. */
300
301 bool
302 recode_transform_po_source (RECODE_TASK task)
303 {
304 struct mixed mixed;
305 bool recode = false;
306 bool msgstr = false;
307
308 if (!open_mixed (&mixed, task))
309 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
310 else
311 {
312 int character = recode_get_byte (&mixed.subtask);
313 while (character != EOF)
314 switch (character)
315 {
316 case '#':
317 /* Copy a comment, recoding only those written by translators. */
318
319 recode_put_byte ('#', &mixed.subtask);
320 character = recode_get_byte (&mixed.subtask);
321 if (character == EOF)
322 break;
323 recode = character == ' ' || character == '\t';
324
325 if (recode)
326 start_accumulation (&mixed);
327
328 while (character != '\n' && character != EOF)
329 {
330 recode_put_byte (character, &mixed.subtask);
331 character = recode_get_byte (&mixed.subtask);
332 }
333
334 if (recode && !recode_accumulated (&mixed))
335 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
336
337 if (character == EOF)
338 break;
339 recode_put_byte ('\n', &mixed.subtask);
340 character = recode_get_byte (&mixed.subtask);
341 break;
342
343 case 'm':
344 /* Attempt to recognise `msgstr'. */
345
346 msgstr = false;
347
348 recode_put_byte ('m', &mixed.subtask);
349 character = recode_get_byte (&mixed.subtask);
350 if (character != 's')
351 break;
352 recode_put_byte ('s', &mixed.subtask);
353 character = recode_get_byte (&mixed.subtask);
354 if (character != 'g')
355 break;
356 recode_put_byte ('g', &mixed.subtask);
357 character = recode_get_byte (&mixed.subtask);
358 if (character != 's')
359 break;
360 recode_put_byte ('s', &mixed.subtask);
361 character = recode_get_byte (&mixed.subtask);
362 if (character != 't')
363 break;
364 recode_put_byte ('t', &mixed.subtask);
365 character = recode_get_byte (&mixed.subtask);
366 if (character != 'r')
367 break;
368 recode_put_byte ('r', &mixed.subtask);
369 character = recode_get_byte (&mixed.subtask);
370
371 msgstr = true;
372 break;
373
374 case '"':
375 /* Copy the string, translating only the `msgstr' ones. */
376
377 recode_put_byte ('"', &mixed.subtask);
378 character = recode_get_byte (&mixed.subtask);
379 recode = msgstr;
380
381 if (recode)
382 start_accumulation (&mixed);
383
384 while (true)
385 {
386 if (character == EOF)
387 {
388 if (recode)
389 {
390 recode_accumulated (&mixed);
391 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
392 }
393 break;
394 }
395 if (character == '"')
396 break;
397
398 if (character == '\\')
399 {
400 recode_put_byte ('\\', &mixed.subtask);
401 character = recode_get_byte (&mixed.subtask);
402 if (character == EOF)
403 {
404 if (recode)
405 {
406 recode_accumulated (&mixed);
407 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
408 }
409 break;
410 }
411 }
412 recode_put_byte (character, &mixed.subtask);
413 character = recode_get_byte (&mixed.subtask);
414 }
415
416 if (character == EOF)
417 break;
418
419 if (recode && !recode_accumulated (&mixed))
420 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
421
422 recode_put_byte ('"', &mixed.subtask);
423 character = recode_get_byte (&mixed.subtask);
424 break;
425
426 default:
427 recode_put_byte (character, &mixed.subtask);
428 character = recode_get_byte (&mixed.subtask);
429 break;
430 }
431 }
432
433 if (!close_mixed (&mixed))
434 recode_if_nogo (RECODE_SYSTEM_ERROR, &mixed.subtask);
435 SUBTASK_RETURN (&mixed.subtask);
436 }