Skip to content

Commit 5ede35d

Browse files
committed
Fix building QVMs on Linux with Windows line endings
On non-Windows, compiling QVM tools failed if dagcheck.md had CRLF line endings and compiling QVMs failed if game source had CRLF line endings. Also made Windows open the files as binary (no automatic CRLF to LF) so it behaves the same as on non-Windows.
1 parent b07ff2a commit 5ede35d

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

code/tools/lcc/cpp/lex.c

+20
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,25 @@ foldline(Source *s)
511511
return 0;
512512
}
513513

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+
514533
int
515534
fillbuf(Source *s)
516535
{
@@ -521,6 +540,7 @@ fillbuf(Source *s)
521540
error(FATAL, "Input buffer overflow");
522541
if (s->fd<0 || (n=read(s->fd, (char *)s->inl, INS/8)) <= 0)
523542
n = 0;
543+
n = crlf_to_lf(s->inl, n);
524544
if ((*s->inp&0xff) == EOB) /* sentinel character appears in input */
525545
*s->inp = EOFC;
526546
s->inl += n;

code/tools/lcc/cpp/unix.c

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ setup(int argc, char **argv)
6565
fp = (char*)newstring((uchar*)argv[optind], strlen(argv[optind]), 0);
6666
if ((fd = open(fp, 0)) <= 0)
6767
error(FATAL, "Can't open input file %s", fp);
68+
#ifdef WIN32
69+
_setmode(fd, _O_BINARY);
70+
#endif
6871
}
6972
if (optind+1<argc) {
7073
int fdo;
@@ -75,6 +78,9 @@ setup(int argc, char **argv)
7578
#endif
7679
if (fdo<0)
7780
error(FATAL, "Can't open output file %s", argv[optind+1]);
81+
#ifdef WIN32
82+
_setmode(fdo, _O_BINARY);
83+
#endif
7884
dup2(fdo, 1);
7985
}
8086
if(Mflag)

code/tools/lcc/lburg/gram.c

+22
Original file line numberDiff line numberDiff line change
@@ -1553,26 +1553,48 @@ static char buf[BUFSIZ], *bp = buf;
15531553
static int ppercent = 0;
15541554
static int code = 0;
15551555

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+
15561575
static int get(void) {
15571576
if (*bp == 0) {
15581577
bp = buf;
15591578
*bp = 0;
15601579
if (fgets(buf, sizeof buf, infp) == NULL)
15611580
return EOF;
1581+
crlf_to_lf(buf, sizeof buf);
15621582
yylineno++;
15631583
while (buf[0] == '%' && buf[1] == '{' && buf[2] == '\n') {
15641584
for (;;) {
15651585
if (fgets(buf, sizeof buf, infp) == NULL) {
15661586
yywarn("unterminated %{...%}\n");
15671587
return EOF;
15681588
}
1589+
crlf_to_lf(buf, sizeof buf);
15691590
yylineno++;
15701591
if (strcmp(buf, "%}\n") == 0)
15711592
break;
15721593
fputs(buf, outfp);
15731594
}
15741595
if (fgets(buf, sizeof buf, infp) == NULL)
15751596
return EOF;
1597+
crlf_to_lf(buf, sizeof buf);
15761598
yylineno++;
15771599
}
15781600
}

code/tools/lcc/lburg/gram.y

+22
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,48 @@ static char buf[BUFSIZ], *bp = buf;
7070
static int ppercent = 0;
7171
static int code = 0;
7272

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+
7392
static int get(void) {
7493
if (*bp == 0) {
7594
bp = buf;
7695
*bp = 0;
7796
if (fgets(buf, sizeof buf, infp) == NULL)
7897
return EOF;
98+
crlf_to_lf(buf, sizeof buf);
7999
yylineno++;
80100
while (buf[0] == '%' && buf[1] == '{' && buf[2] == '\n') {
81101
for (;;) {
82102
if (fgets(buf, sizeof buf, infp) == NULL) {
83103
yywarn("unterminated %{...%}\n");
84104
return EOF;
85105
}
106+
crlf_to_lf(buf, sizeof buf);
86107
yylineno++;
87108
if (strcmp(buf, "%}\n") == 0)
88109
break;
89110
fputs(buf, outfp);
90111
}
91112
if (fgets(buf, sizeof buf, infp) == NULL)
92113
return EOF;
114+
crlf_to_lf(buf, sizeof buf);
93115
yylineno++;
94116
}
95117
}

code/tools/lcc/lburg/lburg.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ int main(int argc, char *argv[]) {
5656
} else if (infp == NULL) {
5757
if (strcmp(argv[i], "-") == 0)
5858
infp = stdin;
59-
else if ((infp = fopen(argv[i], "r")) == NULL) {
59+
else if ((infp = fopen(argv[i], "rb")) == NULL) {
6060
yyerror("%s: can't read `%s'\n", argv[0], argv[i]);
6161
exit(1);
6262
}
6363
} else if (outfp == NULL) {
6464
if (strcmp(argv[i], "-") == 0)
6565
outfp = stdout;
66-
if ((outfp = fopen(argv[i], "w")) == NULL) {
66+
if ((outfp = fopen(argv[i], "wb")) == NULL) {
6767
yyerror("%s: can't write `%s'\n", argv[0], argv[i]);
6868
exit(1);
6969
}

0 commit comments

Comments
 (0)