Browse Source

Suggested changes around binary files, restructured map

tags/v20240302
Emil 7 months ago
parent
commit
500b7047d4
No known key found for this signature in database GPG Key ID: 5432DB986FDBCF8A
1 changed files with 144 additions and 188 deletions
  1. +144
    -188
      bake.c

+ 144
- 188
bake.c View File

@@ -36,7 +36,6 @@
"@COMPILECMD instead of @EXEC. Whitespace is required after and before both\n" \
"operators always.\n"


#define DESC \
"Options [Must always be first]\n" \
"\t-h --help, -n --dry-run\n" \
@@ -45,137 +44,30 @@
"\t$* returns target-file without suffix (^-> abc.x)\n" \
"\t$+ returns arguments\n"

#define local_assert(expr, ret) do { assert(expr); if (!expr) { return ret; }} while (0)

static char * g_filename, * g_short, * g_all;

static char *
map(char * fn, size_t * len)
{
struct stat s;
int fd;
char * addr = NULL;
fd = open(fn, O_RDONLY);
if (fd != -1)
{
if (!fstat(fd,&s)
&& s.st_mode & S_IFREG
&& s.st_size)
{
*len = s.st_size;
addr = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
}
close(fd);
}
return addr;
}
static char * g_filename;

static char *
find(char * x, char * buf, size_t max, size_t min)
{
char * start = buf;
for (; *buf; ++buf)
{
if (max - (buf - start) > min
&& !strncmp(buf, x, min))
{ return buf; }
}
return NULL;
}

static char *
find_region(char * fn)
{
size_t len = 0;
char * buf = NULL, * addr;
char * start, * stop;
addr = map(fn, &len);
if ((ptrdiff_t) addr > 0)
{
start = find(START, addr, len, strlen(START));
#ifdef OTHER_START
if (!start)
{
start = find(OTHER_START, addr, len, strlen(OTHER_START));
start = (char *) /* DON'T QUESTION IT */
((ptrdiff_t) (start - strlen(START) + strlen(OTHER_START)) * (start != 0));
}
#endif /* OTHER_START */
if (start)
{
start += strlen(START);
#ifdef REQUIRE_SPACE
if (!isspace(*start))
{
fprintf(stderr, "ERROR: Found start without suffix spacing.\n");
goto stop;
}
#endif
stop = find(STOP, start, len - (start - addr), strlen(STOP));
if (!stop)
{
stop = start;
while (*stop
&& *stop != '\n')
{
if (stop[0] == '\\'
&& stop[1] == '\n')
{ stop += 2; }
++stop;
}
}
#ifdef REQUIRE_SPACE
else
{
if (!isspace(*(stop - 1)))
{
fprintf(stderr, "ERROR: Found stop without prefixing spacing.\n");
goto stop;
}
}
#endif
if (stop)
{ buf = strndup(start, (stop - addr) - (start - addr)); }
}
stop:
munmap(addr, len);
}
return buf;
}
/*** Utility functions ***/

static void
swap(char * a, char * b)
{
swap(char * a, char * b) {
*a ^= *b;
*b ^= *a;
*a ^= *b;
}

static int
root(char ** rootp)
{
char x[1] = "\0";
char * root = *rootp;
size_t len = strlen(root);
int ret;
while (len
&& root[len] != '/')
{ --len; }
if (!len)
{ return 0; }
swap(root + len, x);
ret = chdir(root);
swap(root + len, x);
*rootp += len + 1;
return ret;
static char *
find(char * x, char * buf, char * end) {
size_t len = strlen(x);
for (; (buf < end) && len < (size_t)(end - buf); ++buf) {
if (!strncmp(buf, x, len))
{ return buf; }
}
return NULL;
}

static char *
insert(char * new, char * str, size_t offset, size_t shift)
{
insert(char * new, char * str, size_t offset, size_t shift) {
size_t len, max;
local_assert(new, str);
local_assert(str, NULL);
len = strlen(new);
max = (strlen(str) + 1 - offset - shift);
memmove(str + offset + len, str + offset + shift, max);
@@ -183,19 +75,19 @@ insert(char * new, char * str, size_t offset, size_t shift)
return str;
}

/*** g_short, g_all Functions ***/

static char * g_short, * g_all;

static char *
shorten(char * fn)
{
shorten(char * fn) {
size_t i, last = 0, len;
char * sh;
local_assert(fn, NULL);
len = strlen(fn);
sh = malloc(len + 1);
local_assert(sh, NULL);
for (i = 0; i < len; ++i)
{
if (fn[i] == '.')
{ last = i; }
if (!sh) { return NULL; }
for (i = 0; i < len; ++i) {
if (fn[i] == '.') { last = i; }
}
last = last ? last : i;
strncpy(sh, fn, last);
@@ -204,53 +96,128 @@ shorten(char * fn)
}

static char *
all_args(size_t argc, char ** argv)
{
all_args(size_t argc, char ** argv) {
char * all = NULL;
if (argc > 2)
{
if (argc > 2) {
size_t i, len = argc;
for (i = 2; i < argc; ++i)
{ len += strlen(argv[i]); }
for (i = 2; i < argc; ++i) { len += strlen(argv[i]); }
all = malloc(len + 1);
local_assert(all, NULL);
if (!all) { return NULL; }
all[len] = '\0';
len = 0;
for (i = 2; i < argc; ++i)
{
for (i = 2; i < argc; ++i) {
strcpy(all + len, argv[i]);
len += strlen(argv[i]) + 1;
if (i + 1 < argc)
{ all[len - 1] = ' '; }
if (i + 1 < argc) { all[len - 1] = ' '; }
}
}
return all;
}

/*** Map ***/

typedef struct {
char * str;
size_t len;
} map_t;

static map_t
map(char * fn) {
struct stat s;
int fd;
map_t fs = {0};
fd = open(fn, O_RDONLY);
if (fd != -1) {
if (!fstat(fd,&s)
&& s.st_mode & S_IFREG
&& s.st_size) {
fs.len = s.st_size;
fs.str = (char *) mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
}
close(fd);
}
return fs;
}

/*** Important Functions ***/

static char *
find_region(map_t m) {
char * buf = NULL;
char * start, * stop;
start = find(START, m.str, m.str + m.len);
#ifdef OTHER_START
if (!start) {
start = find(OTHER_START, m.str, m.str + m.len);
start = (char *) /* DON'T QUESTION IT */
((ptrdiff_t) (start - strlen(START) + strlen(OTHER_START)) * (start != 0));
}
#endif /* OTHER_START */
if (start) {
start += strlen(START);
#ifdef REQUIRE_SPACE
if (!isspace(*start)) {
fprintf(stderr, "ERROR: Found start without suffix spacing.\n");
return buf;
}
#endif
stop = find(STOP, start, start + m.len - (start - m.str));
if (!stop) {
stop = start;
while (*stop
&& *stop != '\n') {
if (stop[0] == '\\'
&& stop[1] == '\n') { stop += 2; }
++stop;
}
}
#ifdef REQUIRE_SPACE
else {
if (!isspace(*(stop - 1))) {
fprintf(stderr, "ERROR: Found stop without prefixing spacing.\n");
return buf;
}
}
#endif
if (stop) { buf = strndup(start, (stop - m.str) - (start - m.str)); }
}
return buf;
}

static int
root(char ** rootp) {
char x[1] = "\0";
char * root = *rootp;
size_t len = strlen(root);
int ret;
while (len
&& root[len] != '/')
{ --len; }
if (!len) { return 0; }
swap(root + len, x);
ret = chdir(root);
swap(root + len, x);
*rootp += len + 1;
return ret;
}

static size_t
expand_size(char * buf, int argc, char ** argv)
{
expand_size(char * buf, int argc, char ** argv) {
size_t i, len, max;
len = max = strlen(buf) + 1;
for (i = 0; i < len; ++i)
{
if (buf[i] == '\\')
{ i += 2; continue; }
else if (buf[i] == '$')
{
switch (buf[++i])
{
for (i = 0; i < len; ++i) {
if (buf[i] == '\\') { i += 2; continue; }
else if (buf[i] == '$') {
switch (buf[++i]) {
case '@':
max += strlen(g_filename);
break;
case '*':
if (!g_short)
{ g_short = shorten(g_filename); }
if (!g_short) { g_short = shorten(g_filename); }
max += g_short ? strlen(g_short) : 0;
break;
case '+':
if (!g_all)
{ g_all = all_args((size_t) argc, argv); }
if (!g_all) { g_all = all_args((size_t) argc, argv); }
max += g_all ? strlen(g_all) : 0;
break;
}
@@ -260,18 +227,13 @@ expand_size(char * buf, int argc, char ** argv)
}

static char *
expand(char * buf)
{
expand(char * buf) {
size_t i;
char * ptr = NULL;
for (i = 0; buf[i]; ++i)
{
if (buf[i] == '\\')
{ i += 2; continue; }
else if (buf[i] == '$')
{
switch (buf[++i])
{
for (i = 0; buf[i]; ++i) {
if (buf[i] == '\\') { i += 2; continue; }
else if (buf[i] == '$') {
switch (buf[++i]) {
case '@':
ptr = g_filename;
break;
@@ -291,30 +253,25 @@ expand(char * buf)
}

static size_t
strip(char * buf)
{
strip(char * buf) {
size_t i = strlen(buf);
if (!i)
{ return 0; }
while (isspace(buf[i - 1]))
{ --i; }
if (!i) { return 0; }
while (isspace(buf[i - 1])) { --i; }
buf[i] = '\0';
for (i = 0; isspace(buf[i]); ++i);
return i - (buf[i - 1] == '\n');
}

static int
run(char * buf)
{
run(char * buf) {
fputs("Output:\n", stderr);
return system(buf);
}

int
main(int argc, char ** argv)
{
main(int argc, char ** argv) {
int ret = 0;
char * buf;
char * buf = NULL;

setlocale(LC_ALL, "C");

@@ -326,28 +283,27 @@ main(int argc, char ** argv)
g_filename = argv[1];

if (!strcmp(argv[1], "-n")
|| !strcmp(argv[1], "--dry-run"))
{
if (argc > 2)
{ ret = 1; g_filename = argv[2]; }
else
{ goto help; }
|| !strcmp(argv[1], "--dry-run")) {
if (argc > 2) { ret = 1; g_filename = argv[2]; }
else { goto help; }
}

buf = find_region(g_filename);
{ map_t m = map(argv[1]);
if (m.str) {
buf = find_region(m);
munmap(m.str, m.len);
}
}

if (!buf)
{
if (errno)
{ perror(argv[0]); }
else
{ fprintf(stderr, "%s: File unrecognized.\n", argv[0]); }
if (!buf) {
if (errno) { perror(argv[0]); }
else { fprintf(stderr, "%s: File unrecognized.\n", argv[0]); }
return 1;
}

root(&g_filename);
buf = realloc(buf, expand_size(buf, argc, argv));
local_assert(buf, 1);
if (!buf) { free(g_short); free(g_all); return 1; }
buf = expand(buf);
fprintf(stderr, "Exec: %s\n", buf + strip(buf));
if ((ret = ret ? 0 : run(buf)))


Loading…
Cancel
Save