Prefix every script with helpers.sh

This commit is contained in:
Nikita Ivanov 2022-05-23 06:27:10 +05:00
parent 277ff54f8b
commit e0505cf094
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
2 changed files with 38 additions and 19 deletions

View File

@ -5,7 +5,7 @@ SRC := $(wildcard *.c)
OBJ := $(SRC:.c=.o) OBJ := $(SRC:.c=.o)
DEP := $(OBJ:.o=.d) DEP := $(OBJ:.o=.d)
PRE := $(wildcard prev/*.sh) 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 CFLAGS += -Os -MD -Wall -Wextra -Wno-unused-parameter
LDFLAGS += -lmagic LDFLAGS += -lmagic
@ -37,11 +37,7 @@ ctpv.c: $(GEN)
gen/prev/scripts.h: embed/embed $(PRE) gen/prev/scripts.h: embed/embed $(PRE)
@mkdir -p $(@D) @mkdir -p $(@D)
embed/embed -p prev_scr_ $(PRE) > $@ embed/embed -p prev_scr_ -h helpers.sh $(PRE) > $@
gen/prev/helpers.h: embed/embed helpers.sh
@mkdir -p $(@D)
embed/embed -p prev_ helpers.sh > $@
embed/embed: make_embed embed/embed: make_embed
@: @:

View File

@ -19,54 +19,77 @@ void getvarname(char *res, char *prefix, char *filename)
res += prefix_len; res += prefix_len;
} }
for (int c, i = 0; s[i] != 0; i++) { int c, i = 0;
for (; s[i] != 0; i++) {
c = s[i]; c = s[i];
if (!isalnum(c)) if (!isalnum(c))
c = '_'; c = '_';
res[i] = c; res[i] = c;
} }
res[i] = '\0';
} }
void embed_file(char *prefix, char *filename) void print_byte(char c)
{ {
FILE *f = fopen(filename, "r"); printf("0x%x, ", c);
if (!f) { }
fprintf(stderr, "failed to open %s: %s\n", filename, strerror(errno));
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); 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]; static char varname[FILENAME_MAX];
getvarname(varname, prefix, filename); getvarname(varname, prefix, filename);
printf("char %s[] = { ", varname); printf("char %s[] = { ", varname);
int c; if (helpers) {
while ((c = fgetc(f)) != EOF) print_file(helpers);
printf("0x%x, ", c); print_byte('\n');
}
print_file(filename);
puts("0 };"); puts("0 };");
fclose(f);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *prefix = NULL; char *prefix = NULL, *helpers = NULL;
int c; int c;
while ((c = getopt(argc, argv, "p:")) != -1) { while ((c = getopt(argc, argv, "p:h:")) != -1) {
switch (c) { switch (c) {
case 'p': case 'p':
prefix = optarg; prefix = optarg;
break; break;
case 'h':
helpers = optarg;
break;
default: default:
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
for (int i = optind; i < argc; i++) for (int i = optind; i < argc; i++)
embed_file(prefix, argv[i]); embed_file(prefix, argv[i], helpers);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }