File tree 5 files changed +72
-2
lines changed
5 files changed +72
-2
lines changed Original file line number Diff line number Diff line change @@ -511,6 +511,25 @@ foldline(Source *s)
511
511
return 0 ;
512
512
}
513
513
514
+ // This doesn't have proper tracking across read() to only remove \r from \r\n sequence.
515
+ // The lexer doesn't correctly handle standalone \r anyway though.
516
+ int
517
+ crlf_to_lf (unsigned char * buf , int n ) {
518
+ int i , count ;
519
+
520
+ count = 0 ;
521
+
522
+ for (i = 0 ; i < n ; i ++ ) {
523
+ if (buf [i ] == '\r' ) {
524
+ continue ;
525
+ }
526
+
527
+ buf [count ++ ] = buf [i ];
528
+ }
529
+
530
+ return count ;
531
+ }
532
+
514
533
int
515
534
fillbuf (Source * s )
516
535
{
@@ -521,6 +540,7 @@ fillbuf(Source *s)
521
540
error (FATAL , "Input buffer overflow" );
522
541
if (s -> fd < 0 || (n = read (s -> fd , (char * )s -> inl , INS /8 )) <= 0 )
523
542
n = 0 ;
543
+ n = crlf_to_lf (s -> inl , n );
524
544
if ((* s -> inp & 0xff ) == EOB ) /* sentinel character appears in input */
525
545
* s -> inp = EOFC ;
526
546
s -> inl += n ;
Original file line number Diff line number Diff line change @@ -65,6 +65,9 @@ setup(int argc, char **argv)
65
65
fp = (char * )newstring ((uchar * )argv [optind ], strlen (argv [optind ]), 0 );
66
66
if ((fd = open (fp , 0 )) <= 0 )
67
67
error (FATAL , "Can't open input file %s" , fp );
68
+ #ifdef WIN32
69
+ _setmode (fd , _O_BINARY );
70
+ #endif
68
71
}
69
72
if (optind + 1 < argc ) {
70
73
int fdo ;
@@ -75,6 +78,9 @@ setup(int argc, char **argv)
75
78
#endif
76
79
if (fdo < 0 )
77
80
error (FATAL , "Can't open output file %s" , argv [optind + 1 ]);
81
+ #ifdef WIN32
82
+ _setmode (fdo , _O_BINARY );
83
+ #endif
78
84
dup2 (fdo , 1 );
79
85
}
80
86
if (Mflag )
Original file line number Diff line number Diff line change @@ -1553,26 +1553,48 @@ static char buf[BUFSIZ], *bp = buf;
1553
1553
static int ppercent = 0 ;
1554
1554
static int code = 0 ;
1555
1555
1556
+ static void crlf_to_lf (char * buf , int bufmax ) {
1557
+ int i , count ;
1558
+
1559
+ count = 0 ;
1560
+
1561
+ for (i = 0 ; i < bufmax ; i ++ ) {
1562
+ if (buf [i ] == '\r' && buf [i + 1 ] == '\n' ) {
1563
+ // skip '\r'
1564
+ continue ;
1565
+ }
1566
+
1567
+ buf [count ++ ] = buf [i ];
1568
+
1569
+ if (buf [i ] == '\0' ) {
1570
+ break ;
1571
+ }
1572
+ }
1573
+ }
1574
+
1556
1575
static int get (void ) {
1557
1576
if (* bp == 0 ) {
1558
1577
bp = buf ;
1559
1578
* bp = 0 ;
1560
1579
if (fgets (buf , sizeof buf , infp ) == NULL )
1561
1580
return EOF ;
1581
+ crlf_to_lf (buf , sizeof buf );
1562
1582
yylineno ++ ;
1563
1583
while (buf [0 ] == '%' && buf [1 ] == '{' && buf [2 ] == '\n' ) {
1564
1584
for (;;) {
1565
1585
if (fgets (buf , sizeof buf , infp ) == NULL ) {
1566
1586
yywarn ("unterminated %{...%}\n" );
1567
1587
return EOF ;
1568
1588
}
1589
+ crlf_to_lf (buf , sizeof buf );
1569
1590
yylineno ++ ;
1570
1591
if (strcmp (buf , "%}\n" ) == 0 )
1571
1592
break ;
1572
1593
fputs (buf , outfp );
1573
1594
}
1574
1595
if (fgets (buf , sizeof buf , infp ) == NULL )
1575
1596
return EOF ;
1597
+ crlf_to_lf (buf , sizeof buf );
1576
1598
yylineno ++ ;
1577
1599
}
1578
1600
}
Original file line number Diff line number Diff line change @@ -70,26 +70,48 @@ static char buf[BUFSIZ], *bp = buf;
70
70
static int ppercent = 0 ;
71
71
static int code = 0 ;
72
72
73
+ static void crlf_to_lf (char *buf, int bufmax) {
74
+ int i, count;
75
+
76
+ count = 0 ;
77
+
78
+ for (i = 0 ; i < bufmax; i++) {
79
+ if (buf[i] == ' \r ' && buf[i+1 ] == ' \n ' ) {
80
+ // skip '\r'
81
+ continue ;
82
+ }
83
+
84
+ buf[count++] = buf[i];
85
+
86
+ if (buf[i] == ' \0 ' ) {
87
+ break ;
88
+ }
89
+ }
90
+ }
91
+
73
92
static int get (void ) {
74
93
if (*bp == 0 ) {
75
94
bp = buf;
76
95
*bp = 0 ;
77
96
if (fgets (buf, sizeof buf, infp) == NULL )
78
97
return EOF;
98
+ crlf_to_lf (buf, sizeof buf);
79
99
yylineno++;
80
100
while (buf[0 ] == ' %' && buf[1 ] == ' {' && buf[2 ] == ' \n ' ) {
81
101
for (;;) {
82
102
if (fgets (buf, sizeof buf, infp) == NULL ) {
83
103
yywarn (" unterminated %{...%}\n " );
84
104
return EOF;
85
105
}
106
+ crlf_to_lf (buf, sizeof buf);
86
107
yylineno++;
87
108
if (strcmp (buf, " %}\n " ) == 0 )
88
109
break ;
89
110
fputs (buf, outfp);
90
111
}
91
112
if (fgets (buf, sizeof buf, infp) == NULL )
92
113
return EOF;
114
+ crlf_to_lf (buf, sizeof buf);
93
115
yylineno++;
94
116
}
95
117
}
Original file line number Diff line number Diff line change @@ -56,14 +56,14 @@ int main(int argc, char *argv[]) {
56
56
} else if (infp == NULL ) {
57
57
if (strcmp (argv [i ], "-" ) == 0 )
58
58
infp = stdin ;
59
- else if ((infp = fopen (argv [i ], "r " )) == NULL ) {
59
+ else if ((infp = fopen (argv [i ], "rb " )) == NULL ) {
60
60
yyerror ("%s: can't read `%s'\n" , argv [0 ], argv [i ]);
61
61
exit (1 );
62
62
}
63
63
} else if (outfp == NULL ) {
64
64
if (strcmp (argv [i ], "-" ) == 0 )
65
65
outfp = stdout ;
66
- if ((outfp = fopen (argv [i ], "w " )) == NULL ) {
66
+ if ((outfp = fopen (argv [i ], "wb " )) == NULL ) {
67
67
yyerror ("%s: can't write `%s'\n" , argv [0 ], argv [i ]);
68
68
exit (1 );
69
69
}
You can’t perform that action at this time.
0 commit comments