From 9900164c327b5476de2aff08b4b1d006c3b6e9de Mon Sep 17 00:00:00 2001 From: Zack Middleton Date: Tue, 26 Dec 2023 07:24:06 -0600 Subject: [PATCH] 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. --- code/tools/lcc/cpp/lex.c | 20 ++++++++++++++++++++ code/tools/lcc/cpp/unix.c | 6 ++++++ code/tools/lcc/lburg/gram.c | 22 ++++++++++++++++++++++ code/tools/lcc/lburg/gram.y | 22 ++++++++++++++++++++++ code/tools/lcc/lburg/lburg.c | 4 ++-- 5 files changed, 72 insertions(+), 2 deletions(-) diff --git a/code/tools/lcc/cpp/lex.c b/code/tools/lcc/cpp/lex.c index 8030354efc..66092e1b9d 100644 --- a/code/tools/lcc/cpp/lex.c +++ b/code/tools/lcc/cpp/lex.c @@ -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) { @@ -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; diff --git a/code/tools/lcc/cpp/unix.c b/code/tools/lcc/cpp/unix.c index bac841d8bd..56f1fff5c1 100644 --- a/code/tools/lcc/cpp/unix.c +++ b/code/tools/lcc/cpp/unix.c @@ -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