Skip to content

Commit

Permalink
Fix building QVMs on Linux with Windows line endings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zturtleman committed Dec 27, 2023
1 parent b07ff2a commit 5ede35d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
20 changes: 20 additions & 0 deletions code/tools/lcc/cpp/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,25 @@ foldline(Source *s)
return 0;
}

// This doesn't have proper tracking across read() to only remove \r from \r\n sequence.
// The lexer doesn't correctly handle standalone \r anyway though.
int
crlf_to_lf(unsigned char *buf, int n) {
int i, count;

count = 0;

for (i = 0; i < n; i++) {
if (buf[i] == '\r') {
continue;
}

buf[count++] = buf[i];
}

return count;
}

int
fillbuf(Source *s)
{
Expand All @@ -521,6 +540,7 @@ fillbuf(Source *s)
error(FATAL, "Input buffer overflow");
if (s->fd<0 || (n=read(s->fd, (char *)s->inl, INS/8)) <= 0)
n = 0;
n = crlf_to_lf(s->inl, n);
if ((*s->inp&0xff) == EOB) /* sentinel character appears in input */
*s->inp = EOFC;
s->inl += n;
Expand Down
6 changes: 6 additions & 0 deletions code/tools/lcc/cpp/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ setup(int argc, char **argv)
fp = (char*)newstring((uchar*)argv[optind], strlen(argv[optind]), 0);
if ((fd = open(fp, 0)) <= 0)
error(FATAL, "Can't open input file %s", fp);
#ifdef WIN32
_setmode(fd, _O_BINARY);
#endif
}
if (optind+1<argc) {
int fdo;
Expand All @@ -75,6 +78,9 @@ setup(int argc, char **argv)
#endif
if (fdo<0)
error(FATAL, "Can't open output file %s", argv[optind+1]);
#ifdef WIN32
_setmode(fdo, _O_BINARY);
#endif
dup2(fdo, 1);
}
if(Mflag)
Expand Down
22 changes: 22 additions & 0 deletions code/tools/lcc/lburg/gram.c
Original file line number Diff line number Diff line change
Expand Up @@ -1553,26 +1553,48 @@ 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 (;;) {
if (fgets(buf, sizeof buf, infp) == NULL) {
yywarn("unterminated %{...%}\n");
return EOF;
}
crlf_to_lf(buf, sizeof buf);
yylineno++;
if (strcmp(buf, "%}\n") == 0)
break;
fputs(buf, outfp);
}
if (fgets(buf, sizeof buf, infp) == NULL)
return EOF;
crlf_to_lf(buf, sizeof buf);
yylineno++;
}
}
Expand Down
22 changes: 22 additions & 0 deletions code/tools/lcc/lburg/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,48 @@ 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 (;;) {
if (fgets(buf, sizeof buf, infp) == NULL) {
yywarn("unterminated %{...%}\n");
return EOF;
}
crlf_to_lf(buf, sizeof buf);
yylineno++;
if (strcmp(buf, "%}\n") == 0)
break;
fputs(buf, outfp);
}
if (fgets(buf, sizeof buf, infp) == NULL)
return EOF;
crlf_to_lf(buf, sizeof buf);
yylineno++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions code/tools/lcc/lburg/lburg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 5ede35d

Please sign in to comment.