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)
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
@:

View File

@ -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;
}