From 964aba4831b5e034cb337635707af11c7fa95784 Mon Sep 17 00:00:00 2001 From: n64 Date: Mon, 22 Jun 2020 23:27:24 +0200 Subject: [PATCH] Pipe input to textconv This avoids a situation when make thinks it has successfully built the target, after textconv fails for some reason. --- Makefile | 6 ++-- tools/textconv.c | 79 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 0587363..80e5f9e 100644 --- a/Makefile +++ b/Makefile @@ -590,12 +590,10 @@ endif endif $(BUILD_DIR)/text/%/define_courses.inc.c: text/define_courses.inc.c text/%/courses.h - $(CPP) $(VERSION_CFLAGS) $< -o $@ -I text/$*/ - $(TEXTCONV) charmap.txt $@ $@ + $(CPP) $(VERSION_CFLAGS) $< -o - -I text/$*/ | $(TEXTCONV) charmap.txt - $@ $(BUILD_DIR)/text/%/define_text.inc.c: text/define_text.inc.c text/%/courses.h text/%/dialogs.h - $(CPP) $(VERSION_CFLAGS) $< -o $@ -I text/$*/ - $(TEXTCONV) charmap.txt $@ $@ + $(CPP) $(VERSION_CFLAGS) $< -o - -I text/$*/ | $(TEXTCONV) charmap.txt - $@ RSP_DIRS := $(BUILD_DIR)/rsp ALL_DIRS := $(BUILD_DIR) $(addprefix $(BUILD_DIR)/,$(SRC_DIRS) $(ASM_DIRS) $(GODDARD_SRC_DIRS) $(ULTRA_SRC_DIRS) $(ULTRA_ASM_DIRS) $(ULTRA_BIN_DIRS) $(BIN_DIRS) $(TEXTURE_DIRS) $(TEXT_DIRS) $(SOUND_SAMPLE_DIRS) $(addprefix levels/,$(LEVEL_DIRS)) include) $(MIO0_DIR) $(addprefix $(MIO0_DIR)/,$(VERSION)) $(SOUND_BIN_DIR) $(SOUND_BIN_DIR)/sequences/$(VERSION) $(RSP_DIRS) diff --git a/tools/textconv.c b/tools/textconv.c index 595835a..867a944 100644 --- a/tools/textconv.c +++ b/tools/textconv.c @@ -54,31 +54,69 @@ static void parse_error(const char *filename, int lineNum, const char *msgfmt, . // Reads the whole file and returns a null-terminated buffer with its contents void *read_text_file(const char *filename) { - FILE *file = fopen(filename, "rb"); - uint8_t *buffer; - size_t size; + if (strcmp(filename, "-") != 0) + { + FILE *file = fopen(filename, "rb"); + uint8_t *buffer; + size_t size; - if (file == NULL) - fatal_error("failed to open file '%s' for reading: %s", filename, strerror(errno)); + if (file == NULL) + fatal_error("failed to open file '%s' for reading: %s", filename, strerror(errno)); - // get size - fseek(file, 0, SEEK_END); - size = ftell(file); + // get size + fseek(file, 0, SEEK_END); + size = ftell(file); - // allocate buffer - buffer = malloc(size + 1); + // allocate buffer + buffer = malloc(size + 1); + if (buffer == NULL) + fatal_error("could not allocate buffer of size %u", (uint32_t)(size + 1)); - // read file - fseek(file, 0, SEEK_SET); - if (fread(buffer, size, 1, file) != 1) - fatal_error("error reading from file '%s': %s", filename, strerror(errno)); + // read file + fseek(file, 0, SEEK_SET); + if (fread(buffer, size, 1, file) != 1) + fatal_error("error reading from file '%s': %s", filename, strerror(errno)); - // null-terminate the buffer - buffer[size] = 0; + // null-terminate the buffer + buffer[size] = 0; - fclose(file); + fclose(file); - return buffer; + return buffer; + } + else + { + size_t size = 0; + size_t capacity = 1024; + uint8_t *buffer = malloc(capacity + 1); + + if (buffer == NULL) + fatal_error("could not allocate buffer of size %u", (uint32_t)(capacity + 1)); + + for (;;) + { + size += fread(buffer + size, 1, capacity - size, stdin); + if (size == capacity) + { + capacity *= 2; + buffer = realloc(buffer, capacity + 1); + if (buffer == NULL) + fatal_error("could not allocate buffer of size %u", (uint32_t)(capacity + 1)); + } + else if (feof(stdin)) + { + break; + } + else + { + fatal_error("error reading from stdin: %s", strerror(errno)); + } + } + + // null-terminate the buffer + buffer[size] = 0; + return buffer; + } } static char *skip_whitespace(char *str) @@ -351,7 +389,7 @@ static char *convert_string(char *pos, FILE *fout, const char *inputFileName, ch static void convert_file(const char *infilename, const char *outfilename) { char *in = read_text_file(infilename); - FILE *fout = fopen(outfilename, "wb"); + FILE *fout = strcmp(outfilename, "-") != 0 ? fopen(outfilename, "wb") : stdout; if (fout == NULL) fatal_error("failed to open file '%s' for writing: %s", strerror(errno)); @@ -436,7 +474,8 @@ static void convert_file(const char *infilename, const char *outfilename) eof: fwrite(start, pos - start, 1, fout); - fclose(fout); + if (strcmp(outfilename, "-") != 0) + fclose(fout); free(in); }