Debian Codegraph @

src/task.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1990-2023 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1990.
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
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27
28 #include "minmax.h"
29 #include "xbinary-io.h"
30
31 bool recode_interrupted = 0; /* set by signal handler when some signal has been received */
32
33
34 /* Input and output helpers. */
35
36 /*-------------------------------------------------------------------.
37 | Read one byte from the input text of TASK, or EOF if none remain. |
38 `-------------------------------------------------------------------*/
39
40 int
41 recode_get_byte (RECODE_SUBTASK subtask)
42 {
43 if (subtask->input.file)
44 return getc (subtask->input.file);
45 else if (subtask->input.cursor == subtask->input.limit)
46 return EOF;
47 else
48 return (unsigned char) *subtask->input.cursor++;
49 }
50
51 /*-------------------------------------------------------------------.
52 | Read N bytes from the input text of TASK into DATA; return number |
53 | of bytes read. |
54 `-------------------------------------------------------------------*/
55
56 size_t
57 recode_get_bytes (RECODE_SUBTASK subtask, char *data, size_t n)
58 {
59 if (subtask->input.file)
60 return fread (data, 1, n, subtask->input.file);
61 else
62 {
63 size_t bytes_left = subtask->output.limit - subtask->output.cursor;
64 size_t bytes_to_copy = MIN (n, bytes_left);
65 memcpy (data, subtask->output.cursor, bytes_to_copy);
66 subtask->output.cursor += bytes_to_copy;
67 return bytes_to_copy;
68 }
69 }
70
71 /*-----------------------------------------.
72 | Write BYTE on the output text for TASK. |
73 `-----------------------------------------*/
74
75 void
76 recode_put_byte (char byte, RECODE_SUBTASK subtask)
77 {
78 if (subtask->output.file)
79 {
80 if (putc (byte, subtask->output.file) == EOF)
81 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
82 }
83 else if (subtask->output.cursor == subtask->output.limit)
84 recode_put_bytes (&byte, 1, subtask);
85 else
86 *subtask->output.cursor++ = byte;
87 }
88
89 /*-----------------------------------------------------.
90 | Write N bytes of DATA on the output text for TASK. |
91 `-----------------------------------------------------*/
92
93 void
94 recode_put_bytes (const char *data, size_t n, RECODE_SUBTASK subtask)
95 {
96 if (subtask->output.file)
97 {
98 if (fwrite (data, n, 1, subtask->output.file) != 1)
99 {
100 recode_perror (NULL, "fwrite ()");
101 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
102 }
103 }
104 else {
105 if (subtask->output.cursor + n > subtask->output.limit)
106 {
107 RECODE_OUTER outer = subtask->task->request->outer;
108 size_t old_size = subtask->output.limit - subtask->output.buffer;
109 size_t new_size = old_size * 3 / 2 + 40 + n;
110
111 if (REALLOC (subtask->output.buffer, new_size, char))
112 {
113 subtask->output.cursor = subtask->output.buffer + old_size;
114 subtask->output.limit = subtask->output.buffer + new_size;
115 }
116 else
117 {
118 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
119 return;
120 }
121 }
122 memcpy (subtask->output.cursor, data, n);
123 subtask->output.cursor += n;
124 }
125 }
126
127 /* Error processing. */
128
129 /*------------------------------------------------------------------------.
130 | Handle a given ERROR, while executing STEP within TASK. Return true if |
131 | the abort level has been reached. |
132 `------------------------------------------------------------------------*/
133
134 bool
135 recode_if_nogo (enum recode_error new_error, RECODE_SUBTASK subtask)
136 {
137 RECODE_TASK task = subtask->task;
138
139 if (new_error > task->error_so_far)
140 {
141 task->error_so_far = new_error;
142 task->error_at_step = subtask->step;
143 }
144 return task->error_so_far >= task->abort_level;
145 }
146
147 /* Recoding execution control. */
148
149 /*--------------.
150 | Copy a file. |
151 `--------------*/
152
153 static void
154 transform_mere_copy (RECODE_SUBTASK subtask)
155 {
156 if (subtask->input.file)
157 {
158 /* Reading from file. */
159
160 char buffer[BUFSIZ];
161 size_t size;
162
163 while (size = recode_get_bytes (subtask, buffer, BUFSIZ),
164 size == BUFSIZ)
165 recode_put_bytes (buffer, BUFSIZ, subtask);
166 if (size > 0)
167 recode_put_bytes (buffer, size, subtask);
168 }
169 else
170 /* Reading from buffer. */
171
172 if (subtask->input.cursor < subtask->input.limit)
173 recode_put_bytes (subtask->input.cursor,
174 (unsigned) (subtask->input.limit - subtask->input.cursor),
175 subtask);
176 }
177
178 /*--------------------------------------------------.
179 | Recode a file using a one-to-one recoding table. |
180 `--------------------------------------------------*/
181
182 bool
183 recode_transform_byte_to_byte (RECODE_SUBTASK subtask)
184 {
185 unsigned const char *table
186 = (unsigned const char *) subtask->step->step_table;
187 int input_char;
188
189 while (input_char = recode_get_byte (subtask), input_char != EOF)
190 recode_put_byte (table[input_char], subtask);
191
192 SUBTASK_RETURN (subtask);
193 }
194
195 /*---------------------------------------------------.
196 | Recode a file using a one-to-many recoding table. |
197 `---------------------------------------------------*/
198
199 bool
200 recode_transform_byte_to_variable (RECODE_SUBTASK subtask)
201 {
202 const char *const *table = (const char *const *) subtask->step->step_table;
203 int input_char;
204 const char *output_string;
205
206 /* Copy the file through the one to many recoding table. */
207
208 while (input_char = recode_get_byte (subtask), input_char != EOF)
209 if (output_string = table[input_char], output_string)
210 while (*output_string)
211 {
212 recode_put_byte (*output_string, subtask);
213 output_string++;
214 }
215 else
216 RETURN_IF_NOGO (RECODE_UNTRANSLATABLE, subtask);
217
218 SUBTASK_RETURN (subtask);
219 }
220
221 /*------------------------------------------------------------------------.
222 | Execute the conversion sequence for a recoding TASK. If no conversions |
223 | are needed, merely copy the input onto the output. |
224 | Returns zero if the recoding has been found to be non-reversible. |
225 | Tell what goes on if VERBOSE. |
226 `------------------------------------------------------------------------*/
227
228 bool
229 recode_perform_task (RECODE_TASK task)
230 {
231 RECODE_CONST_REQUEST request = task->request;
232 struct recode_subtask subtask_block;
233 RECODE_SUBTASK subtask = &subtask_block;
234
235 #if HAVE_PIPE
236 int pipe_pair[2]; /* pair of file descriptors for a pipe */
237 pid_t wait_status; /* status returned by wait() */
238 #endif
239
240 struct recode_read_write_text input;
241 struct recode_read_write_text output;
242 memset (&input, 0, sizeof (struct recode_read_write_text));
243 memset (&output, 0, sizeof (struct recode_read_write_text));
244
245 memset (subtask, 0, sizeof (struct recode_subtask));
246 subtask->task = task;
247 subtask->input = task->input;
248
249 /* Switch stdin and stdout to binary mode unless they are ttys, as this has
250 nasty side-effects on several DOSish systems. For example, the Ctrl-Z
251 character is no longer interpreted as EOF, and thus the poor user cannot
252 signal end of input; the INTR character also doesn't work, so they cannot
253 even interrupt the program, and are stuck. On the other hand, output to
254 the screen doesn't have to follow the end-of-line format exactly, since
255 it is going to be discarded anyway. */
256 if (task->input.name && !*task->input.name && !isatty (fileno (stdin)))
257 xset_binary_mode (fileno (stdin), O_BINARY);
258 if (task->output.name && !*task->output.name && !isatty (fileno (stdout)))
259 xset_binary_mode (fileno (stdout), O_BINARY);
260
261 /* Prepare the first input file. */
262
263 if (subtask->input.name)
264 {
265 if (!*subtask->input.name)
266 subtask->input.file = stdin;
267 else if (subtask->input.file = fopen (subtask->input.name, "rb"),
268 subtask->input.file == NULL)
269 {
270 recode_perror (NULL, "fopen (%s)", subtask->input.name);
271 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
272 SUBTASK_RETURN (subtask);
273 }
274 }
275
276 /* Execute one pass for each step of the sequence. */
277
278 int child_process = -1; /* child process number, -1 if process has not forked */
279 for (unsigned sequence_index = 0;
280 task->error_so_far < task->abort_level;
281 sequence_index++)
282 {
283 child_process = -1;
284
285 if (sequence_index > 0)
286 {
287 /* Select the input text for this step. */
288
289 subtask->input.buffer = input.buffer;
290 subtask->input.cursor = input.buffer;
291 subtask->input.limit = input.cursor;
292 subtask->input.file = input.file;
293 }
294
295 /* Select the output text for this step. */
296
297 if (sequence_index + 1 < (unsigned)request->sequence_length)
298 {
299 subtask->output = output;
300 subtask->output.cursor = subtask->output.buffer;
301
302 #if HAVE_PIPE
303 /* Create all subprocesses, from the first to the last, and
304 interconnect them. */
305
306 if (pipe (pipe_pair) < 0)
307 {
308 recode_perror (NULL, "pipe ()");
309 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
310 SUBTASK_RETURN (subtask);
311 }
312 xset_binary_mode (pipe_pair[0], O_BINARY);
313 xset_binary_mode (pipe_pair[1], O_BINARY);
314 if (child_process = fork (), child_process < 0)
315 {
316 recode_perror (NULL, "fork ()");
317 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
318 SUBTASK_RETURN (subtask);
319 }
320 if (child_process == 0)
321 {
322 /* The child executes its recoding step, reading from the
323 current input file and writing to the pipe; then it exits. */
324
325 if (close (pipe_pair[0]) < 0)
326 {
327 recode_perror (NULL, "close ()");
328 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
329 }
330 if (subtask->output.file = fdopen (pipe_pair[1], "w"),
331 subtask->output.file == NULL)
332 {
333 recode_perror (NULL, "fdopen ()");
334 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
335 }
336 }
337 else
338 {
339 /* The parent saves the read end of the pipe for the next step. */
340
341 if (input.file = fdopen (pipe_pair[0], "r"),
342 input.file == NULL)
343 {
344 recode_perror (NULL, "fdopen ()");
345 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
346 close (pipe_pair[0]);
347 close (pipe_pair[1]);
348 SUBTASK_RETURN (subtask);
349 }
350 if (close (pipe_pair[1]) < 0)
351 {
352 recode_perror (NULL, "close ()");
353 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
354 close (pipe_pair[0]);
355 fclose (input.file);
356 SUBTASK_RETURN (subtask);
357 }
358
359 /* Close the input file when we opened it. */
360
361 if (subtask->input.file && subtask->input.name &&
362 subtask->input.name[0])
363 fclose (subtask->input.file);
364 }
365 #endif
366 }
367 else
368 {
369 /* Prepare the final output file. */
370
371 subtask->output = task->output;
372 if (subtask->output.name)
373 {
374 if (!*subtask->output.name)
375 subtask->output.file = stdout;
376 else if (subtask->output.file = fopen (subtask->output.name, "wb"),
377 subtask->output.file == NULL)
378 {
379 recode_perror (NULL, "fopen (%s)", subtask->output.name);
380 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
381 goto exit;
382 }
383 }
384 }
385
386 /* Execute one recoding step. */
387
388 if (request->sequence_length == 0) {
389 transform_mere_copy (subtask);
390 break;
391 }
392
393 if (child_process <= 0)
394 {
395 subtask->step = request->sequence_array + sequence_index;
396 (*subtask->step->transform_routine) (subtask);
397
398 #if HAVE_PIPE
399 break; /* child/top-level process: escape from loop */
400 #else
401 /* Post-step clean up for memory sequence. */
402
403 if (subtask->input.file)
404 {
405 FILE *fp = subtask->input.file;
406
407 subtask->input.file = NULL;
408 if (fclose (fp) != 0)
409 {
410 recode_perror (NULL, "fclose (%s)", subtask->input.name);
411 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
412 goto exit;
413 }
414 }
415
416 /* Prepare for next step. */
417
418 task->swap_input = RECODE_SWAP_UNDECIDED;
419
420 if (sequence_index + 1 < (unsigned)request->sequence_length)
421 {
422 output = input;
423 input = subtask->output;
424 }
425 #endif
426 }
427
428 if (sequence_index + 1 == (unsigned)request->sequence_length)
429 break;
430 }
431
432 /* Final clean up. */
433 exit:
434
435 if (subtask->input.file && subtask->input.file != task->input.file && fclose (subtask->input.file) != 0)
436 {
437 recode_perror (NULL, "fclose (%s)", subtask->input.name ? subtask->input.name : "stdin");
438 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
439 }
440
441 if (subtask->output.file && subtask->output.file != task->output.file && fclose (subtask->output.file) != 0)
442 {
443 recode_perror (NULL, "fclose (%s)", subtask->output.name ? subtask->output.name : "stdout");
444 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
445 }
446
447 #if HAVE_PIPE
448 /* Child process exits here. */
449
450 if (child_process == 0)
451 exit (task->error_so_far < task->fail_level ? EXIT_SUCCESS
452 : EXIT_FAILURE);
453 else
454 {
455 /* Wait on all children, mainly to avoid synchronisation problems on
456 output file contents, but also to reduce the number of zombie
457 processes in case the user recodes many files at once. */
458
459 while (wait (&wait_status) > 0)
460 {
461 /* Diagnose and abort on any abnormally terminating child. */
462
463 if (!(WIFEXITED (wait_status)
464 || (WIFSIGNALED (wait_status)
465 && WTERMSIG (wait_status) == SIGPIPE)))
466 {
467 recode_error (NULL, _("Child process wait status is 0x%0.2x"),
468 wait_status);
469 recode_if_nogo (RECODE_SYSTEM_ERROR, subtask);
470 SUBTASK_RETURN (subtask);
471 }
472
473 /* Check for a nonzero exit from the terminating child. */
474
475 if (WIFEXITED (wait_status)
476 ? WEXITSTATUS (wait_status) != 0
477 : WTERMSIG (wait_status) != 0)
478 /* FIXME: It is not very clear what happened in sub-processes. */
479 if (task->error_so_far < task->fail_level)
480 {
481 task->error_so_far = task->fail_level;
482 task->error_at_step = request->sequence_array + (unsigned)request->sequence_length - 1;
483 }
484 }
485
486 if (recode_interrupted)
487 /* FIXME: It is not very clear what happened in sub-processes. */
488 if (task->error_so_far < task->fail_level)
489 {
490 task->error_so_far = task->fail_level;
491 task->error_at_step = request->sequence_array + (unsigned)request->sequence_length - 1;
492 }
493 }
494 #else
495 free (input.buffer);
496 free (output.buffer);
497 #endif
498
499 task->output = subtask->output;
500 SUBTASK_RETURN (subtask);
501 }
502
503 /* Library interface. */
504
505 /* See the recode manual for a more detailed description of the library
506 interface. */
507
508 /*-----------------------.
509 | TASK level functions. |
510 `-----------------------*/
511
512 RECODE_TASK
513 recode_new_task (RECODE_CONST_REQUEST request)
514 {
515 RECODE_OUTER outer = request->outer;
516 RECODE_TASK task;
517
518 if (!ALLOC (task, 1, struct recode_task))
519 return NULL;
520
521 task->request = request;
522 task->fail_level = RECODE_NOT_CANONICAL;
523 task->abort_level = RECODE_USER_ERROR;
524 task->error_so_far = RECODE_NO_ERROR;
525 task->swap_input = RECODE_SWAP_UNDECIDED;
526 task->byte_order_mark = true;
527
528 return task;
529 }
530
531 bool
532 recode_delete_task (RECODE_TASK task)
533 {
534 free (task);
535 return true;
536 }