From 909e2fba710544943ff4595aa89ab1e63a96fa63 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Fri, 12 Aug 2022 07:48:44 +0000 Subject: [PATCH 1/7] accept input files with DOS line endings fixes #4 --- sub1.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sub1.c b/sub1.c index d0e9633..a28b729 100644 --- a/sub1.c +++ b/sub1.c @@ -41,6 +41,11 @@ #include #include +static int isnl(int c) +{ + return c == '\r' || c == '\n'; +} + /* * return next line of input, throw away trailing '\n' * and also throw away trailing blanks (spaces and tabs) @@ -55,7 +60,7 @@ getl(CHR *p) int blank = 0; t = s = p; - while (((c = gch()) != 0) && c != '\n') { + while (((c = gch()) != 0) && !isnl(c)) { if (t >= &p[BUF_SIZ]) error("definitions too long"); if (c == ' ' || c == '\t') { From 9065faa01ca2c7282018f04648b795ea4da34d7f Mon Sep 17 00:00:00 2001 From: rofl0r Date: Fri, 26 Aug 2022 13:16:49 +0000 Subject: [PATCH 2/7] form2hdr: open forms in binary mode otherwise windows will turn every \n into a \r\n tuple, which when output later gives mixed line endings in the output file. --- form2hdr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/form2hdr.c b/form2hdr.c index b5643db..e92d738 100644 --- a/form2hdr.c +++ b/form2hdr.c @@ -75,7 +75,7 @@ int main(int argc, char** argv) { default: usage(); } unsigned cpl = 77; - FILE *f = fopen(argv[f_arg], "r"); + FILE *f = fopen(argv[f_arg], "rb"); if(!f) { perror("fopen"); return 1; } if(!skip_header(f)) { fprintf(stderr, "error: form start marker %s not found!\n", "START_INCLUDE"); From 55d244fee58dc7a1b8da6f693e8758a3160f9901 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Sat, 27 Aug 2022 23:27:18 +0000 Subject: [PATCH 3/7] reject.c: fix prototypes for Pelles C --- reject.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/reject.c b/reject.c index 31928e7..5f8bd33 100644 --- a/reject.c +++ b/reject.c @@ -79,15 +79,16 @@ extern unsigned char yytext[]; extern int yyleng; #endif -#if defined(__cplusplus) || defined(__STDC__) -extern int yyback(int *, int); -extern int YYINPUT(void); -extern void YYUNPUT(int); #ifdef EUC static int yyracc(int); #else extern int yyracc(int); #endif + +#if defined(__cplusplus) || defined(__STDC__) +extern int yyback(int *, int); +extern int YYINPUT(void); +extern void YYUNPUT(int); #ifdef EOPTION extern size_t wcstombs(char *, const wchar_t *, size_t); #endif @@ -132,15 +133,11 @@ YYREJECT() return (-1); } -#ifdef EUC +#ifdef EUC static #endif -#if defined(__cplusplus) || defined(__STDC__) int yyracc(int m) -#else -yyracc(m) -#endif { yyolsp = yylsp; if (yyextra[m]) { From f5c4358b2e1721ab65c58fa4ecaa89c93f2da717 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Sat, 27 Aug 2022 23:29:34 +0000 Subject: [PATCH 4/7] form2hdr: fix for Pelles C several fixes are needed for PellesC: - before including stdio.h we need a magic define to get fmemopen() prototype to produce include file PellesC can then actually use we need to - add size for unsigned char[] member, otherwise PellesC throws error #2139 - too many initializers. - turn string literal data into { 0, 1, 2...} style numeric array for unsigned char[] member. theoretically, form2hdr could have been compiled with a different compiler, but we simply assume that when it is used it's used for both form2hdr compilation and for files generated by it. --- form2hdr.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/form2hdr.c b/form2hdr.c index e92d738..fbfa3b5 100644 --- a/form2hdr.c +++ b/form2hdr.c @@ -1,5 +1,13 @@ /* (C) 2019 rofl0r */ /* released into the public domain, or at your choice 0BSD or WTFPL */ + +#ifdef __POCC__ +/* pelles C fails to use string literal as unsigned char[] destination... */ +#define SIMPLE_OUTPUT +/* required to get fmemopen() prototype from stdio.h */ +#define __STDC_WANT_LIB_EXT2__ 1 +#endif + #include #include #include @@ -100,19 +108,21 @@ int main(int argc, char** argv) { { FILE *foo = fopen("debug", "w"); fwrite(outb, 1, outsize, foo); fclose(foo); } #endif fclose(f); - f = fmemopen(outb, outsize, "r"); + f = fmemopen(outb, outsize, "rb"); } char *p = strrchr(argv[f_arg], '/'); if(!p) p = argv[f_arg]; else p++; printf( "const struct { unsigned clen, ulen;\n" - "const unsigned char data[]; } %s = { %zu, %zu,\n", - p, outsize, insize); + "const unsigned char data[%zu]; } %s = { %zu, %zu,\n", + outsize, p, outsize, insize); } int ch, dirty; unsigned cnt = 0; + +#ifndef SIMPLE_OUTPUT while((ch = fgetc(f)) != EOF) { if(cnt == 0) { printf("\""); dirty = 0; } char buf[5]; @@ -132,7 +142,17 @@ int main(int argc, char** argv) { } } if(cnt) printf("\"\n"); +#else + printf("{\n"); + while((ch = fgetc(f)) != EOF) { + ++cnt; + printf("%u,", ch); + if(cnt % cpl == 0) printf("\n"); + } + printf("}\n"); +#endif printf("};\n"); + fclose(f); return 0; } From 044ac5c322b3a5325869d8b15bde4eba10bc78d4 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Sat, 27 Aug 2022 23:36:10 +0000 Subject: [PATCH 5/7] fix some more fopen()s to use binary mode for windows compat this makes sure our \n's come out as \n, not \r\n, so output of lex is identical whether it was compiled for windos or a proper OS, i.e. using proper unix line endings. --- main.c | 4 ++-- sub1.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 4557c57..6ae4a71 100644 --- a/main.c +++ b/main.c @@ -117,7 +117,7 @@ main(int argc, char **argv) "lex: -Q should be followed by [y/n]"); break; case 'o': - fout = fopen(optarg, "w"); + fout = fopen(optarg, "wb"); if (!fout) error( "lex: could not open %s for writing", @@ -165,7 +165,7 @@ main(int argc, char **argv) if (strcmp(argv[optind], "-") == 0) fin = stdin; else { - fin = fopen(argv[optind], "r"); + fin = fopen(argv[optind], "rb"); if (fin == NULL) error( "Can't open input file -- %s", argv[optind]); diff --git a/sub1.c b/sub1.c index a28b729..f7a9fa9 100644 --- a/sub1.c +++ b/sub1.c @@ -199,7 +199,7 @@ lgate(void) lgatflg = 1; if (fout == NULL) { sprintf(fname, "lex.yy.%c", ratfor ? 'r' : 'c'); - fout = fopen(fname, "w"); + fout = fopen(fname, "wb"); } if (fout == NULL) error("Can't open %s", fname); @@ -651,7 +651,7 @@ gch(void) yyline = 0; if (fin != stdin) fclose(fin); - fin = fopen(sargv[++optind], "r"); + fin = fopen(sargv[++optind], "rb"); if (fin == NULL) error("Cannot open file -- %s", sargv[optind]); From 13a92183822ebd19dd36868c76a50b9409b0c7ed Mon Sep 17 00:00:00 2001 From: rofl0r Date: Sat, 27 Aug 2022 23:39:00 +0000 Subject: [PATCH 6/7] on windos, EOF != WEOF and WEOF is actually the right to use in combination with getwc(). we should actually also use wint_t rather than int to pick widechars off the input stream, but this remains on the TODO list for now. --- sub1.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sub1.c b/sub1.c index f7a9fa9..12d8d23 100644 --- a/sub1.c +++ b/sub1.c @@ -642,7 +642,7 @@ gch(void) prev = pres; c = pres = peek; peek = pushptr > pushc ? *--pushptr : getwc(fin); - while (peek == EOF) { + while (peek == WEOF) { if (no_input) { if (!yyline) error("Cannot read from -- %s", @@ -667,7 +667,7 @@ gch(void) break; } } - if (c == EOF) { + if (c == WEOF) { eof = TRUE; return (0); } From 9ee957be20d828f792a45ebaaf8d1c69812e4b45 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Sat, 27 Aug 2022 23:44:46 +0000 Subject: [PATCH 7/7] tweak Makefile for use with pellescc/wine - let user specify config.mak to override defaults - add a couple variables so config.mak can contain the following setup for my pellescc[0] project: ifeq ($(WIN),1) CC = pellescc AR = pellesar RANLIB = true HOSTRUN = wine EXE_EXT = .exe endif and make WIN=1 then does the right thing. [0]: https://github.com/rofl0r/pellescc --- Makefile | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 2193918..b05b5e9 100644 --- a/Makefile +++ b/Makefile @@ -21,18 +21,20 @@ INSTALL = install HOSTCC = $(CC) +-include config.mak + .c.o: ; $(CC) -c $(CFLAGS) $(CPPFLAGS) $(WARN) $< .y.c: ; $(YACC) -o $@ $< -all: lex libl.a +all: lex$(EXE_EXT) libl.a -form2hdr: form2hdr.c +form2hdr$(EXE_EXT): form2hdr.c $(HOSTCC) $(HOSTCFLAGS) form2hdr.c -o $@ -$(GENH): %.h: % ; ./form2hdr -c $< > $@ +$(GENH): %.h: % ; $(HOSTRUN) ./form2hdr$(EXE_EXT) -c $< > $@ -lex: $(XOBJ) - $(CC) $(LDFLAGS) $(XOBJ) $(LIBS) -o lex +lex$(EXE_EXT): $(XOBJ) + $(CC) $(LDFLAGS) $(XOBJ) $(LIBS) -o $@ libl.a: $(LOBJ) rm -f $@ @@ -60,7 +62,7 @@ install: lex lex.1 libl.a $(INSTALL) -D -m 644 lex.1 $(DESTDIR)$(MANDIR)/man1/lex.1 clean: - rm -f lex libl.a $(XOBJ) $(LOBJ) $(GENH) parser.c form2hdr core log *~ + rm -f lex libl.a $(XOBJ) $(LOBJ) $(GENH) parser.c form2hdr$(EXE_EXT) core log *~ mrproper: clean @@ -77,7 +79,7 @@ yyless.o: yyless.c yywrap.o: yywrap.c lsearch.o: search.h wcio.o: ldefs.h -$(GENH): form2hdr +$(GENH): form2hdr$(EXE_EXT) .PHONY: all clean mrproper install # prevent GNU make from deleting parser.c after "all" finishes