Add replacing API calls for dynamic pages

This commit is contained in:
logicog
2025-08-01 23:34:58 +02:00
parent 42c8f05afe
commit 498d34e72f
+70 -7
View File
@@ -16,7 +16,7 @@
// Use a 4MB buffer, the same as the flash rom size // Use a 4MB buffer, the same as the flash rom size
#define BUFFER_SIZE 0x400000 #define BUFFER_SIZE 0x400000
unsigned char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
FILE *inptr, *dataptr, *ofile; FILE *inptr, *dataptr, *ofile;
int outptr; int outptr;
#define PATH_SIZE 1024 #define PATH_SIZE 1024
@@ -25,8 +25,14 @@ int outptr;
char pathbuffer[PATH_SIZE]; char pathbuffer[PATH_SIZE];
char ibuf[INDEX_SIZE]; char ibuf[INDEX_SIZE];
char dbuf[DEF_SIZE]; char dbuf[DEF_SIZE];
char fbuf[DEF_SIZE];
char xbuf[DEF_SIZE];
int defbuf_p; int defbuf_p;
int ibuf_p; int ibuf_p;
int fbuf_p;
int xbuf_p;
bool addsDir = false;
int callNum = 0;
const char *argp_program_version = "fileadder 0.1"; const char *argp_program_version = "fileadder 0.1";
const char *argp_program_bug_address = "<git@logicog.de>"; const char *argp_program_bug_address = "<git@logicog.de>";
@@ -125,10 +131,55 @@ int addidx(const char *name, int addr, int len)
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_START_%s 0x%x\n", s, addr); defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_START_%s 0x%x\n", s, addr);
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_SIZE_%s %d\n", s, len); defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_SIZE_%s %d\n", s, len);
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, " {\"%s\", FDATA_START_%s, FDATA_SIZE_%s},\n", name, s, s); ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, " {\"/%s\", FDATA_START_%s, FDATA_SIZE_%s},\n", name, s, s);
if (!strcmp(name, "index.html"))
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, " {\"/\", FDATA_START_%s, FDATA_SIZE_%s},\n", s, s);
return 0; return 0;
} }
/*
* Replaces calls of the type #{function} in .html files being added
* It is assumed that the possibly expanded string will fit into the buffer
* Returns the new length of the string
*/
int replaceCalls(int pos)
{
int i = 0;
char function_buf[256];
while (buffer[pos + i]) {
if ((buffer[pos + i]) == '#' && (buffer[pos + i + 1] == '{')) {
int j = 2;
while (buffer[pos + i + j]) {
if (buffer[pos + i + j] == '}')
break;
function_buf[j - 2] = buffer[pos + i + j];
j++;
if (j >= 255) {
function_buf[255] = '\0';
fprintf(stderr, "Function not terminated %s ", function_buf);
exit(5);
}
}
function_buf[j - 2] = '\0';
if (!buffer[pos + i + j])
return i + j;
// Because the buffers overlap we cannot use strcpy
memmove(&buffer[pos + i + 5], &buffer[pos + i + j], strlen(&buffer[pos + i + j]) + 1);
sprintf(&buffer[pos + i + 2], "%03d", callNum);
buffer[pos + i + 5] = '}';
i += 6;
fbuf_p += snprintf(&fbuf[fbuf_p], DEF_SIZE - fbuf_p, " %s,\n", function_buf);
xbuf_p += snprintf(&xbuf[xbuf_p], DEF_SIZE - xbuf_p, "extern uint16_t %s(void);\n", function_buf);
callNum++;
}
i++;
}
return strlen(&buffer[pos]);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@@ -180,18 +231,20 @@ int main(int argc, char **argv)
} }
// Add preambles to index and header file // Add preambles to index and header file
defbuf_p = ibuf_p = 0; defbuf_p = ibuf_p = xbuf_p = fbuf_p = 0;
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#ifndef FDATA_DEFS_H\n\n"); defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#ifndef FDATA_DEFS_H\n\n");
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_DEFS_H\n\n"); defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_DEFS_H\n\n");
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#include <stdint.h>\n\n"); defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#include <stdint.h>\n\n");
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "struct f_data {\n char *file;\n uint32_t start;\n uint16_t end;\n};\n\n"); defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "struct f_data {\n __code char *file;\n uint32_t start;\n uint16_t len;\n};\n\n");
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "typedef uint16_t (*fcall_ptr)(void);\n\n");
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "// This file is automatically generated, do not edit!\n\n"); ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "// This file is automatically generated, do not edit!\n\n");
if (arguments.prefix) if (arguments.prefix)
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "#include \"%s.h\"\n\n", arguments.prefix); ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "#include \"%s.h\"\n\n", arguments.prefix);
if (arguments.bank) if (arguments.bank)
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "#pragma codeseg %s\n", arguments.bank); ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "#pragma codeseg %s\n", arguments.bank);
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "struct f_data f_data[] = {\n"); ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "__code struct f_data f_data[] = {\n");
fbuf_p += snprintf(&fbuf[fbuf_p], DEF_SIZE - fbuf_p, "\n__code fcall_ptr f_calls[] = {\n");
// Now that the beginning of the buffer is filled with out image, optionally resize the image // Now that the beginning of the buffer is filled with out image, optionally resize the image
if (filesize) if (filesize)
@@ -199,6 +252,7 @@ int main(int argc, char **argv)
if (stat(arguments.data_file, &s) == 0 && s.st_mode & S_IFDIR) { if (stat(arguments.data_file, &s) == 0 && s.st_mode & S_IFDIR) {
printf("Adding entries in directory %s\n", arguments.data_file); printf("Adding entries in directory %s\n", arguments.data_file);
addsDir = true;
DIR *dirptr = opendir(arguments.data_file); DIR *dirptr = opendir(arguments.data_file);
if (!dirptr) { if (!dirptr) {
fprintf(stderr, "%s: ", arguments.data_file); fprintf(stderr, "%s: ", arguments.data_file);
@@ -223,6 +277,10 @@ int main(int argc, char **argv)
buffer[arguments.address + data_read++ + 1] = '0'; buffer[arguments.address + data_read++ + 1] = '0';
printf("Data inserted from %s at 0x%x, size: %ld\n", pathbuffer, addr, data_read); printf("Data inserted from %s at 0x%x, size: %ld\n", pathbuffer, addr, data_read);
} }
int old_len = data_read;
data_read = replaceCalls(addr);
if (old_len > data_read)
memset(buffer + addr + data_read, 0, old_len - data_read);
addidx(in_file->d_name, addr, data_read); addidx(in_file->d_name, addr, data_read);
addr += data_read; addr += data_read;
} }
@@ -236,7 +294,8 @@ int main(int argc, char **argv)
} }
} }
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "};\n"); ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, " {0, 0, 0}\n};\n");
fbuf_p += snprintf(&fbuf[fbuf_p], DEF_SIZE - fbuf_p, "};\n");
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#endif\n"); defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#endif\n");
if (filesize) { if (filesize) {
@@ -280,10 +339,14 @@ int main(int argc, char **argv)
return 5; return 5;
} }
fputs(ibuf, ofile); fputs(ibuf, ofile);
fputs(xbuf, ofile);
fputs(fbuf, ofile);
fclose(ofile); fclose(ofile);
} else { } else if (addsDir){
puts(dbuf); puts(dbuf);
puts(xbuf);
puts(ibuf); puts(ibuf);
puts(fbuf);
} }
return 0; return 0;
} }