From e0505cf094e68c3d03d4c03745ca21d837481f9a Mon Sep 17 00:00:00 2001 From: Nikita Ivanov Date: Mon, 23 May 2022 06:27:10 +0500 Subject: [PATCH] Prefix every script with helpers.sh --- Makefile | 8 ++------ embed/embed.c | 49 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 1c4c3e4..a140fdc 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ SRC := $(wildcard *.c) OBJ := $(SRC:.c=.o) DEP := $(OBJ:.o=.d) PRE := $(wildcard prev/*.sh) -GEN := gen/prev/scripts.h gen/prev/helpers.h +GEN := gen/prev/scripts.h CFLAGS += -Os -MD -Wall -Wextra -Wno-unused-parameter LDFLAGS += -lmagic @@ -37,11 +37,7 @@ ctpv.c: $(GEN) gen/prev/scripts.h: embed/embed $(PRE) @mkdir -p $(@D) - embed/embed -p prev_scr_ $(PRE) > $@ - -gen/prev/helpers.h: embed/embed helpers.sh - @mkdir -p $(@D) - embed/embed -p prev_ helpers.sh > $@ + embed/embed -p prev_scr_ -h helpers.sh $(PRE) > $@ embed/embed: make_embed @: diff --git a/embed/embed.c b/embed/embed.c index e46d11f..cf24e58 100644 --- a/embed/embed.c +++ b/embed/embed.c @@ -19,54 +19,77 @@ void getvarname(char *res, char *prefix, char *filename) res += prefix_len; } - for (int c, i = 0; s[i] != 0; i++) { + int c, i = 0; + for (; s[i] != 0; i++) { c = s[i]; if (!isalnum(c)) c = '_'; res[i] = c; } + + res[i] = '\0'; } -void embed_file(char *prefix, char *filename) +void print_byte(char c) { - FILE *f = fopen(filename, "r"); - if (!f) { - fprintf(stderr, "failed to open %s: %s\n", filename, strerror(errno)); + printf("0x%x, ", c); +} + +void print_file(char *f) +{ + int c; + FILE *file = fopen(f, "r"); + + if (!file) { + fprintf(stderr, "failed to open %s: %s\n", f, strerror(errno)); exit(EXIT_FAILURE); } + while ((c = fgetc(file)) != EOF) + print_byte(c); + + fclose(file); +} + +void embed_file(char *prefix, char *filename, char *helpers) +{ + static char varname[FILENAME_MAX]; getvarname(varname, prefix, filename); printf("char %s[] = { ", varname); - int c; - while ((c = fgetc(f)) != EOF) - printf("0x%x, ", c); + if (helpers) { + print_file(helpers); + print_byte('\n'); + } + + print_file(filename); puts("0 };"); - - fclose(f); } int main(int argc, char *argv[]) { - char *prefix = NULL; + char *prefix = NULL, *helpers = NULL; int c; - while ((c = getopt(argc, argv, "p:")) != -1) { + while ((c = getopt(argc, argv, "p:h:")) != -1) { switch (c) { case 'p': prefix = optarg; break; + case 'h': + helpers = optarg; + break; default: return EXIT_FAILURE; } } for (int i = optind; i < argc; i++) - embed_file(prefix, argv[i]); + embed_file(prefix, argv[i], helpers); return EXIT_SUCCESS; }