From dc68fd2cde455260f28256ce8a29345c4061308b Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Mon, 29 Sep 2014 20:24:23 -0600 Subject: [PATCH] exp: get rid of the #line bug workaround We can use --no-lines to have bison/yacc not emit lines, hence not hitting the missing newline bug in bision 3.0.2. Signed-off-by: Jens Axboe --- Makefile | 10 +- exp/fixup-buggy-yacc-output.c | 170 ---------------------------------- 2 files changed, 2 insertions(+), 178 deletions(-) delete mode 100644 exp/fixup-buggy-yacc-output.c diff --git a/Makefile b/Makefile index 1968e31d..5b9cfcc6 100644 --- a/Makefile +++ b/Makefile @@ -272,19 +272,13 @@ y.tab.o: y.tab.c y.tab.h $(QUIET_CC)$(CC) -o $@ $(CFLAGS) $(CPPFLAGS) -c $< y.tab.c: exp/expression-parser.y - $(QUIET_CC)$(YACC) -d exp/expression-parser.y + $(QUIET_CC)$(YACC) --no-lines -d exp/expression-parser.y -y.tab.h: y.tab.c exp/fixup-buggy-yacc-output - @exp/fixup-buggy-yacc-output $@ +y.tab.h: y.tab.c lex.yy.c: exp/expression-parser.l $(QUIET_CC)$(LEX) exp/expression-parser.l -exp/fixup-buggy-yacc-output.o: exp/fixup-buggy-yacc-output.c - $(QUIET_CC)$(CC) -o $@ $(CFLAGS) $(CPPFLAGS) -c $< -exp/fixup-buggy-yacc-output: exp/fixup-buggy-yacc-output.o - $(QUIET_LINK)$(CC) $(LDFLAGS) $(CFLAGS) $< -o $@ - exp/test-expression-parser.o: exp/test-expression-parser.c $(QUIET_CC)$(CC) -o $@ $(CFLAGS) $(CPPFLAGS) -c $< exp/test-expression-parser: exp/test-expression-parser.o diff --git a/exp/fixup-buggy-yacc-output.c b/exp/fixup-buggy-yacc-output.c deleted file mode 100644 index ab9bd8e4..00000000 --- a/exp/fixup-buggy-yacc-output.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * (C) Copyright 2014, Stephen M. Cameron. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -static char *programname; - -static char *slurp_file(char *f, off_t *textsize) -{ - int fd; - struct stat statbuf; - off_t bytesleft, bytesread; - char *fileptr = NULL; - char *slurped_file = NULL; - - fd = open(f, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "%s: Cannot open '%s': %s\n", - programname, f, strerror(errno)); - return NULL; - } - if (fstat(fd, &statbuf) != 0) { - fprintf(stderr, "%s: Cannot stat '%s': %s\n", - programname, f, strerror(errno)); - close(fd); - return NULL; - } - bytesleft = statbuf.st_size; - slurped_file = malloc(bytesleft + 1); - fileptr = slurped_file; - if (!slurped_file) { - fprintf(stderr, "%s: malloc returned NULL, out of memory\n", - programname); - goto bail_out; - } - memset(slurped_file, 0, bytesleft + 1); - do { - bytesread = read(fd, fileptr, bytesleft); - if (bytesread < 0 && errno == EAGAIN) - continue; - if (bytesread < 0) { - fprintf(stderr, "%s: error reading '%s: %s'\n", - programname, f, strerror(errno)); - goto bail_out; - } - if (bytesread == 0) { - fprintf(stderr, "%s: unexpected EOF in %s\n", - programname, f); - goto bail_out; - } - fileptr += bytesread; - bytesleft -= bytesread; - } while (bytesleft > 0); - - *textsize = statbuf.st_size; - - close(fd); - return slurped_file; - -bail_out: - if (slurped_file) - free(slurped_file); - close(fd); - return NULL; -} - -static int detect_buggy_yacc(char *text) -{ - char *x; - - x = strstr(text, " #line "); - if (!x) - return 0; - return 1; -} - -static void fixup_buggy_yacc_file(char *f) -{ - char *slurped_file, *x; - off_t textsize; - char *newname; - int fd; - off_t bytesleft, byteswritten; - - newname = alloca(strlen(f) + 10); - strcpy(newname, "broken-"); - strcat(newname, f); - - slurped_file = slurp_file(f, &textsize); - if (!slurped_file) - return; - if (!detect_buggy_yacc(slurped_file)) - return; - - x = slurped_file; - - - /* - * Fixup the '#line' directives which yacc botched. - * Note: this is vulnerable to false positives, but - * since this program is just a hack to make this particular - * program work, it is sufficient for our purposes. - * regexp could make this better, but the real fix needs - * to be made in yacc/bison. - */ - while ((x = strstr(x, " #line ")) != NULL) { - *x = '\n'; - } - - if (rename(f, newname) != 0) { - fprintf(stderr, "%s: Failed to rename '%s' to '%s': %s\n", - programname, f, newname, strerror(errno)); - return; - } - fd = open(f, O_CREAT | O_TRUNC | O_RDWR, 0644); - if (fd < 0) { - fprintf(stderr, "%s: failed to create '%s': %s\n", - programname, f, strerror(errno)); - return; - } - - bytesleft = textsize; - x = slurped_file; - do { - byteswritten = write(fd, x, bytesleft); - if (byteswritten < 0) { - fprintf(stderr, "%s: Error writing '%s': %s\n", - programname, f, strerror(errno)); - return; - } - if (byteswritten == 0 && errno == EINTR) - continue; - x += byteswritten; - bytesleft -= byteswritten; - } while (bytesleft > 0); - close(fd); -} - -int main(int argc, char *argv[]) -{ - int i; - - programname = argv[0]; - - for (i = 1; i < argc; i++) - fixup_buggy_yacc_file(argv[i]); - return 0; -} -- 2.25.1