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.
This commit is contained in:
Zack Middleton committed 2023-12-26 23:26:47 -05:00
1 parent b07ff2a3ca
commit 5ede35d8dd
5 files changed
+72 -2

No files matched your search

+22
View File
@@ -1553,12 +1553,32 @@ static char buf[BUFSIZ], *bp = buf;
static int ppercent = 0;
static int code = 0;
static void crlf_to_lf(char *buf, int bufmax) {
int i, count;
count = 0;
for (i = 0; i < bufmax; i++) {
if (buf[i] == '\r' && buf[i+1] == '\n') {
// skip '\r'
continue;
}
buf[count++] = buf[i];
if (buf[i] == '\0') {
break;
}
}
}
static int get(void) {
if (*bp == 0) {
bp = buf;
*bp = 0;
if (fgets(buf, sizeof buf, infp) == NULL)
return EOF;
crlf_to_lf(buf, sizeof buf);
yylineno++;
while (buf[0] == '%' && buf[1] == '{' && buf[2] == '\n') {
for (;;) {
@@ -1566,6 +1586,7 @@ static int get(void) {
yywarn("unterminated %{...%}\n");
return EOF;
}
crlf_to_lf(buf, sizeof buf);
yylineno++;
if (strcmp(buf, "%}\n") == 0)
break;
@@ -1573,6 +1594,7 @@ static int get(void) {
}
if (fgets(buf, sizeof buf, infp) == NULL)
return EOF;
crlf_to_lf(buf, sizeof buf);
yylineno++;
}
}
+22
View File
@@ -70,12 +70,32 @@ static char buf[BUFSIZ], *bp = buf;
static int ppercent = 0;
static int code = 0;
static void crlf_to_lf(char *buf, int bufmax) {
int i, count;
count = 0;
for (i = 0; i < bufmax; i++) {
if (buf[i] == '\r' && buf[i+1] == '\n') {
// skip '\r'
continue;
}
buf[count++] = buf[i];
if (buf[i] == '\0') {
break;
}
}
}
static int get(void) {
if (*bp == 0) {
bp = buf;
*bp = 0;
if (fgets(buf, sizeof buf, infp) == NULL)
return EOF;
crlf_to_lf(buf, sizeof buf);
yylineno++;
while (buf[0] == '%' && buf[1] == '{' && buf[2] == '\n') {
for (;;) {
@@ -83,6 +103,7 @@ static int get(void) {
yywarn("unterminated %{...%}\n");
return EOF;
}
crlf_to_lf(buf, sizeof buf);
yylineno++;
if (strcmp(buf, "%}\n") == 0)
break;
@@ -90,6 +111,7 @@ static int get(void) {
}
if (fgets(buf, sizeof buf, infp) == NULL)
return EOF;
crlf_to_lf(buf, sizeof buf);
yylineno++;
}
}
+2 -2
View File
@@ -56,14 +56,14 @@ int main(int argc, char *argv[]) {
} else if (infp == NULL) {
if (strcmp(argv[i], "-") == 0)
infp = stdin;
else if ((infp = fopen(argv[i], "r")) == NULL) {
else if ((infp = fopen(argv[i], "rb")) == NULL) {
yyerror("%s: can't read `%s'\n", argv[0], argv[i]);
exit(1);
}
} else if (outfp == NULL) {
if (strcmp(argv[i], "-") == 0)
outfp = stdout;
if ((outfp = fopen(argv[i], "w")) == NULL) {
if ((outfp = fopen(argv[i], "wb")) == NULL) {
yyerror("%s: can't write `%s'\n", argv[0], argv[i]);
exit(1);
}