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