Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept input files with DOS line endings #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
28 changes: 24 additions & 4 deletions form2hdr.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -75,7 +83,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");
Expand All @@ -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];
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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]);
Expand Down
15 changes: 6 additions & 9 deletions reject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]) {
Expand Down
15 changes: 10 additions & 5 deletions sub1.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
#include <ctype.h>
#include <stdarg.h>

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)
Expand All @@ -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') {
Expand Down Expand Up @@ -194,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);
Expand Down Expand Up @@ -637,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",
Expand All @@ -646,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]);
Expand All @@ -662,7 +667,7 @@ gch(void)
break;
}
}
if (c == EOF) {
if (c == WEOF) {
eof = TRUE;
return (0);
}
Expand Down