-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvcrlf.c
More file actions
659 lines (512 loc) · 28.8 KB
/
Copy pathconvcrlf.c
File metadata and controls
659 lines (512 loc) · 28.8 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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
BSD 3-Clause License
Copyright (c) 2026, John Howie ([email protected])
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
** File: convcrlf.c
**
** Description
**
** This module takes an input file and creates an output file, line-by-
** line. It replaces CF LF in quotes with <<CR><LF>>, LF with <<LF>> and vice
** versa. How it does this can be controlled with command line options.
**
** Modifications
**
** 2026-05-25 John Howie Original.
**
*/
# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
# include <fcntl.h>
# include <unistd.h>
# include <string.h>
# include <strings.h>
# include <sys/errno.h>
# include "iohandler.h"
# include "getseparatororquote.h"
int main (int argc, char *argv []);
bool convertfile (const char *crtoken, const char *lftoken, int quotechar, bool convfromcrlf, bool convtocrlf, bool ignorequotes);
bool processpossibleCRLFtoken (const char *crtoken, const char *lftoken);
void DisplayHelp (void);
extern int errno;
int main (int argc, char *argv [])
{
int quotechar = (int) '"';
char c, *crtoken = "<CR>", *lftoken = "<LF>";
bool ignorequotes = false, convfromcrlf = false, convtocrlf = false;
int retval;
// Process the command line arguments
while ((c = getopt (argc, argv, "c:fhil:q:t")) != -1) {
// Process the argument
switch (c) {
case 'c':
// The user wants to specify the string that is
// used to replace or search for CR
crtoken = optarg;
break;
case 'f':
// The user wants to force replacement of \r\n
// and \n to <<CR><LF>> and <<LF>> (or to the
// specified strings) respectively, but not
// other way
convfromcrlf = true;
break;
case 'h':
// The user wants to get help, so display help
// and exit
DisplayHelp ();
exit (EXIT_SUCCESS);
break;
case 'i':
// The user wants to convert all CR LF and/or
// LF, even those not in quotes
ignorequotes = true;
break;
case 'l':
// The user wants to specify the string that is
// user to replace or search for LF
lftoken = optarg;
break;
case 'q':
// The user wants to specify the quote
// character value
if ((quotechar = getseparatororquote(optarg)) == -1) {
// We did not get a valid quote on the
// command line, so display an error
// and then the help
(void) fprintf (stderr, "Invalid quote character %s\n", optarg);
DisplayHelp ();
exit (-1);
}
break;
case 't':
// The user wants to force replacement of
// <<CR><LF>> and <<LF>> (or the respective
// strings) to \r\n and \n, but not the other
// way
convtocrlf = true;
break;
default:
// We got a flag we do not recognize, so
// display help and exit
DisplayHelp ();
exit (-1);
break;
}
}
// Update argc and argv with the flags we processed. Note that the
// argc count is the number of remaining arguments (not including the
// program name), and argv are the remaining arguments (not including
// the program name)
argc -= optind;
argv += optind;
// Now we need to check and see if we have other non-flag parameters,
// which we assume to be optional input and output files. There should
// never be more than two parameters, so check
if (argc > 2) {
// We have extraneous parameters. All we can do is display a
// help message and exit
DisplayHelp ();
exit (-1);
}
if (argc >= 1) {
// We have at least one argument. We always assume the first
// argument is an input file
if (! OpenInputOrOutputFile (0, argv [0])) {
// An error occurred, and we could not redirect stdin
perror ("redirect stdin");
exit errno;
}
}
if (argc == 2) {
// We also need to open an output file
if (! OpenInputOrOutputFile (1, argv [1])) {
// An error occurred, and we coudl not redirect stdout
perror ("redirect stdout");
exit errno;
}
}
// Call the function that converts the separator in the input file to
// those we want in the output file
retval = convertfile (crtoken, lftoken, quotechar, convfromcrlf, convtocrlf, ignorequotes);
if (retval == false) {
// An error occurred. Display an error message and exit
perror ("Unable to convert file");
exit (EXIT_FAILURE);
}
// Exit a success
exit (EXIT_SUCCESS);
return 0; // Redundant
}
/* void DisplayHelp (void)
**
** This function is called do display some basic information for the user, when
** they request it or call this program incorrectly
*/
void DisplayHelp (void)
{
printf ("Usage:\n\n");
printf ("\tconvcrlf [-c CR] [-f] [-i] [-l LF] [-q quote] [-t] [input] [output]\n");
printf ("\n");
printf ("This program converts embedded \\r\\n (CRLF) and \\n (LF) found in quotes ('\"') in\n");
printf ("the input stream to <<CR><LF>> and <<LF>> respectively to the output stream when\n");
printf ("the -f flag is used, and the reverse when the -t flag is used. Use of both flags\n");
printf ("simultaneously will cause all \\r\\n and \\n to be converted to <<CR><LF>> and\n");
printf ("<<LF>>, and <<CR><LF>> and <<LF>> to \\r\\n and \\r simultaneoously.\n");
printf ("\n");
printf ("The substitution for \\r (<CR>) and \\n (<LF>) can be overridden by use of the -c\n");
printf ("and -l flags. It is used in conversions both ways (from and to \\r\\n and \\n)\n");
printf ("\n");
printf ("The -i flag can be used to force the program to ignore quotes, and convert all\n");
printf ("\\r\\n and \\n (or string equivalents) found in the input stream. The quote\n");
printf ("character can be specified using the -q flag, and can be a character or a number\n");
printf ("in hexadecimal (\\xHH) or octal (\\nnn)format.\n");
}
/* bool convertfile (const char *crtoken, const char *lftoken, int quotechar, bool convfromcrlf, bool convtocrlf, bool ignorequotes)
**
** This functions reads from the standard input looking for quotes (unless the
** -i flag is used) then for carriage returns "\r" and newlines "\n" and for
** <CR> (or user specified) and <LF> (or user specified) strings depending on
** the use of the -f (convfromcrlf) and -t (convtocrlf) flags.
*/
bool convertfile (const char *crtoken, const char *lftoken, int quotechar, bool convfromcrlf, bool convtocrlf, bool ignorequotes)
{
int charin, inspectionpos;
bool inquotedsection = false, foundcrlf = false;
// We go through the input stream, character by character, looking for
// various tokens in the input stream
while ((charin = fgetc (stdin)) != EOF) {
// Check what we got - see if it is a token we are looking for
switch (charin) {
case '<':
// We have possibly found the beginning of the string
// representation of CRLF or LF. We only care if we
// are in a quoted section or are ignoring quotes, and
// converting to \r and \n
if ((inquotedsection || ignorequotes) && convtocrlf) {
// Call a function we use to process the tokens
// following this one, to see if we need to
// write out one or more \r and/or \n
if (! processpossibleCRLFtoken (crtoken, lftoken)) {
// We were unable to process the
// possible CRLF or LF token, so all we
// can do is return false
return false;
}
}
else {
// We need to write out the '<' character as it
// is not the start of a token
if (fputc ((int) '<', stdout) == EOF) {
// An error occurred, just return an
// error
return false;
}
}
break;
case '\r':
case '\n':
// We found a carriage return or a line feed. Check if
// we are in a quoted section or if we are ignoring
// quoted sections, and that we are converting CRLF and
// LF
if ((inquotedsection || ignorequotes) && convfromcrlf) {
// We need to process the carriage return or
// line feed. We start by checking if we are
// already processing CRLF / LF
if (! foundcrlf) {
// This is the first CRLF or LF
foundcrlf = true;
// Write out the token ('<') that we
// look for in the input stream when we
// convert back to carriage returns and
// line feeds
if (fputc ((int) '<', stdout) == EOF) {
// An error occurred. There is
// nothing more we can do, so
// return false
return false;
}
}
// Write out the CR string or the LF string as
// appropriate
if (fprintf (stdout, "%s", ((charin == (int) '\n') ? lftoken : crtoken)) < 0) {
// A error occurred, and we could not
// write out the string representation
// of the CR or LF character, so we
// just return false
return false;
}
}
else {
// We are not converting the carriage return
// or new line, so just write it out
if (fputc (charin, stdout) == EOF) {
// An error occurred, and we can only
// return false
return false;
}
}
break;
default:
// We found something other than <, \r, or \n. First
// thing we do is check if we were processing \r and/or
// \n to CR and/or LF strings, and close out the
// conversion by writing '>'
if (foundcrlf) {
// Write out the closing '>'
if (fputc ((int) '>', stdout) == EOF) {
// An error occurred, and we could not
// write out the end of sequence
// character. All we can do is return
// false
return false;
}
// Clear the foundcrlf flag
foundcrlf = false;
}
// Check if we found the quote character. We cannot do
// that in the switch statement, above, as we can only
// check for fixed values
if (charin == quotechar) {
// We found the quote character, so flip the
// flag we use to track whether or not we are
// in a quoted section
inquotedsection ^= true;
}
// Write out the character we just read in
if (fputc (charin, stdout) == EOF) {
// An error occurred and we could not write out
// the character we just read in. All we can do
// is return false
return false;
}
break;
}
}
// Check that we reached the legitimate end of the input stream, and
// did not encounter an error
if (! feof (stdin)) {
// We had a read error. Just return false
return false;
}
// We finished - return true
return true;
}
/* bool processpossibleCRLFtoken (const char *crtoken, const char *lftoken)
**
** This function is called to process a possible CR or LF string token, to be
** converted into \r and \n respectively. It actually does more - it looks for
** <CR> and/or <LF> (or user-specified strings) all the way up to a '>' after
** the last token
*/
bool processpossibleCRLFtoken (const char *crtoken, const char *lftoken)
{
int charin, tokenpos = 0, inputpos;
bool possiblecr = true, possiblelf = true, writtencrlf = false;
// We go into a loop reading characters until we find '>'. We process
// what we find, checking for <CR> and/or <LF> tokens (or user-
// specified tokens), and writing our \r and/or \n as required
while ((charin = fgetc (stdin)) != EOF) {
// Check if the character we just read in is '>'
if (charin == (int) '>') {
// Check if we have just written out a \r or \n, and
// we have not begun processing a new token, as that
// means we have completed the token(s). We can just
// return
if (writtencrlf && (tokenpos == 0))
return true;
}
// Check to see if the character we read matches the next
// expected character in the token strings <CR> or <LF> (or the
// equivalent strings specified by the user)
// Start with the CR token
if (possiblecr) {
// Check if the character we read is the next expected
// character from the CR token
if (*(crtoken + tokenpos) == (char) charin) {
// Check if this is the last character in the
// token
if (*(crtoken + tokenpos +1) == (char) 0) {
// We have reached the end of the CR
// token, so write out the carriage
// return
if (fputc ((int) '\r', stdout) == EOF) {
// We ran into an error, and
// were unable to write out
// the carriage return
return false;
}
// Reset the the various flags and
// counters we use
possiblecr = true; // Unecessary
possiblelf = true;
writtencrlf = true;
tokenpos = 0;
continue;
}
}
else {
// The character we read is not the next
// expected one in the token. Check if we are
// still looking for a LF token
possiblecr = false;
if (! possiblelf) {
// We are not looking for a LF token,
// so we write out what we read in the
// CR token so far (and an initial '<')
if (writtencrlf) {
// We are in a quandary. We
// successfully processed one
// or more CR and/or LF tokens,
// which means the input stream
// is corrupted as we did not
// get the trailing '>'. All we
// can do is return false
return false;
}
// Write out everything we have read
// up-to this character beginning with
// the '<'
if (fputc ((int) '<', stdout) == EOF) {
// We were unable to write out
// the '<' character
return false;
}
// Go into a loop, writing out the
// characters in the CR token up to the
// position of the mismatching
// character
for (inputpos = 0; inputpos < tokenpos; inputpos ++) {
if (fputc ((int) *(crtoken + inputpos), stdout) == EOF) {
// An error occurred,
// and we were unable
// to write out the
// character. Just
// return false
return false;
}
}
// Write out the character we just read
if (fputc (charin, stdout) == EOF) {
// We were unable to write out
// the character we just read,
// so return an error
return false;
}
// We can return success, as we did not
// find a CR or LF token, and wrote out
// the characters we read instead
return true;
}
}
}
// Now we do the same, again, for the LF token
if (possiblelf) {
// Check if the character we read is the next expected
// character from the CR token
if (*(lftoken + tokenpos) == (char) charin) {
// Check if this is the last character in the
// token
if (*(lftoken + tokenpos +1) == (char) 0) {
// We have reached the end of the CR
// token, so write out the carriage
// return
if (fputc ((int) '\n', stdout) == EOF) {
// We ran into an error, and
// were unable to write out
// the carriage return
return false;
}
// Reset the the various flags and
// counters we use
possiblecr = true;
possiblelf = true; // Unecessary
writtencrlf = true;
tokenpos = 0;
continue;
}
}
else {
// The character we read is not the next
// expected one in the token. Check if we are
// still looking for a LF token
possiblelf = false;
if (! possiblecr) {
// We are not looking for a CR token,
// so we write out what we read in the
// LF token so far (and an initial '<')
if (writtencrlf) {
// We are in a quandary. We
// successfully processed one
// or more CR and/or LF tokens,
// which means the input stream
// is corrupted as we did not
// get the trailing '>'. All we
// can do is return false
return false;
}
// Write out everything we have read
// up-to this character beginning with
// the '<'
if (fputc ((int) '<', stdout) == EOF) {
// We were unable to write out
// the '<' character
return false;
}
// Go into a loop, writing out the
// characters in the CR token up to the
// position of the mismatching
// character
for (inputpos = 0; inputpos < tokenpos; inputpos ++) {
if (fputc ((int) *(lftoken + inputpos), stdout) == EOF) {
// An error occurred,
// and we were unable
// to write out the
// character. Just
// return false
return false;
}
}
// Write out the character we just read
if (fputc (charin, stdout) == EOF) {
// We were unable to write out
// the character we just read,
// so return an error
return false;
}
// We can return success, as we did not
// find a CR or LF token, and wrote out
// the characters we read instead
return true;
}
}
}
// Increment tokenpos
tokenpos ++;
}
// If we got here, an error occurred. It does nto matter if it is the
// end of file or read error. We should have exited the loop, returning
// to the caller, when we found a valid '>'
return false;
}