no alloc reimplementation

This commit is contained in:
Chad C. Starz 2024-04-08 23:45:48 +00:00
parent 282de09301
commit d9ce89a386
No known key found for this signature in database
GPG Key ID: CEEBC9208C287297

90
bake.c
View File

@ -68,6 +68,13 @@ enum {
BAKE_MISSING_SUFFIX, BAKE_MISSING_SUFFIX,
}; };
enum {
BAKE_RUN = 0,
BAKE_NORUN = (1 << 0),
BAKE_EXPUNGE = (1 << 1),
};
#define ARRLEN(a) (sizeof(a) / sizeof(a[0])) #define ARRLEN(a) (sizeof(a) / sizeof(a[0]))
#if INCLUDE_AUTONOMOUS_COMPILE #if INCLUDE_AUTONOMOUS_COMPILE
@ -78,8 +85,8 @@ enum {
static int bake_errno; static int bake_errno;
typedef struct { typedef struct {
char * buf;
size_t len; size_t len;
char * buf;
} string_t; } string_t;
typedef string_t map_t; typedef string_t map_t;
@ -199,8 +206,8 @@ get_region(string_t m, char * findstart, char * findstop) {
static char * static char *
file_get_region(char * fn, char * start, char * stop) { file_get_region(char * fn, char * start, char * stop) {
char * buf = NULL;
map_t m = map(fn); map_t m = map(fn);
char * buf = NULL;
if (m.buf) { if (m.buf) {
buf = get_region(m, start, stop); buf = get_region(m, start, stop);
@ -347,30 +354,38 @@ bake_expand(char * buf, char * filename, int argc, char ** argv) {
return buf; return buf;
} }
static char * static void
remove_expand(char * buf) { remove_expand(char * buf, char * argv0, int rm, char * start, char * stop) {
size_t i, f, plen = 0, len = 1, end = strlen(buf); size_t i, f, end = strlen(buf);
char * l = NULL; size_t startlen = strlen(start), stoplen = strlen(stop);
char x[1] = {'\0'};
for (i = 0; i < end; ++i) { for (i = 0; i < end; ++i) {
if (!strncmp(buf + i, EXPUNGE_START, strlen(EXPUNGE_START))) { if (!strncmp(buf + i, start, startlen)) {
if (buf + i > buf && buf[i - 1] == '\\') { if (buf + i > buf && buf[i - 1] == '\\') {
continue; continue;
} }
for (f = i; f < end; ++f) { for (f = i; f < end; ++f) {
if (!strncmp(buf + f, EXPUNGE_STOP, strlen(EXPUNGE_STOP))) { if (!strncmp(buf + f, stop, stoplen)) {
if (buf + f > buf && buf[f - 1] == '\\') { if (buf + f > buf && buf[f - 1] == '\\') {
continue; continue;
} }
insert(buf + f, "", end - f, 0, 1); insert(buf + f, "", end - f, 0, 1);
i += strlen(EXPUNGE_START); i += startlen;
plen = (len != 1) * (len - 1);
len += f - i + 1; if (rm & BAKE_EXPUNGE) {
l = realloc(l, len); swap(buf + i + (f - i), x);
memcpy(l + plen, buf + i, f - i); printf("%s: %sremoving '%s'\n",
l[plen + f - i] = '\0'; argv0, rm & BAKE_NORUN ? "not " : "", buf + i);
if (!(rm & BAKE_NORUN)) {
remove(buf + i);
}
swap(buf + i + (f - i), x);
}
goto next; goto next;
} }
} }
@ -382,13 +397,7 @@ remove_expand(char * buf) {
} }
stop: stop:
expand(buf, EXPUNGE_START, ""); expand(buf, start, "");
if (l) {
l[len - 1] = '\0';
}
return l;
} }
/*** strip, run ***/ /*** strip, run ***/
@ -451,12 +460,10 @@ run(char * buf, char * argv0) {
int int
main(int argc, char ** argv) { main(int argc, char ** argv) {
enum { RET_RUN = 0, RET_NORUN = (1 << 0), RET_EXPUNGE = (1 << 1) }; int ret = BAKE_RUN;
int ret = RET_RUN;
char * buf = NULL, char * buf = NULL,
* filename, * filename,
* argv0; * argv0;
char * rem;
argv0 = argv[0]; argv0 = argv[0];
@ -475,9 +482,9 @@ main(int argc, char ** argv) {
} else if (!strcmp(argv[0], "version")) { } else if (!strcmp(argv[0], "version")) {
goto version; goto version;
} else if (!strcmp(argv[0], "expunge")) { } else if (!strcmp(argv[0], "expunge")) {
ret |= RET_EXPUNGE; ret |= BAKE_EXPUNGE;
} else if (!strcmp(argv[0], "dry-run")) { } else if (!strcmp(argv[0], "dry-run")) {
ret |= RET_NORUN; ret |= BAKE_NORUN;
} else { } else {
puts("UNKNOWN"); puts("UNKNOWN");
goto help; goto help;
@ -491,11 +498,11 @@ main(int argc, char ** argv) {
goto version; goto version;
case 'x': case 'x':
ret |= RET_EXPUNGE; ret |= BAKE_EXPUNGE;
break; break;
case 'n': case 'n':
ret |= RET_NORUN; ret |= BAKE_NORUN;
break; break;
case 0 : case 0 :
@ -524,11 +531,11 @@ main(int argc, char ** argv) {
root(&filename); root(&filename);
buf = file_get_region(filename, START, STOP); buf = file_get_region(filename, START, STOP);
if (!buf) {
char * error[2]; char * error[2];
error[0] = "File Unrecognized"; error[0] = "File Unrecognized";
error[1] = "Found start without suffix spacing"; error[1] = "Found start without suffix spacing";
if (!buf) {
fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' %s.\n", fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' %s.\n",
argv0, filename, errno ? strerror(errno) : error[bake_errno]); argv0, filename, errno ? strerror(errno) : error[bake_errno]);
return BAKE_ERROR; return BAKE_ERROR;
@ -538,7 +545,7 @@ main(int argc, char ** argv) {
printf(BOLD GREEN "%s" RESET ": %s\n", argv0, buf + strip(buf)); printf(BOLD GREEN "%s" RESET ": %s\n", argv0, buf + strip(buf));
rem = remove_expand(buf); remove_expand(buf, argv0, ret, EXPUNGE_START, EXPUNGE_STOP);
if (!ret) { if (!ret) {
ret = run(buf, argv0); ret = run(buf, argv0);
@ -546,29 +553,8 @@ main(int argc, char ** argv) {
if (ret) { if (ret) {
printf(BOLD RED "result" RESET ": " BOLD "%d\n" RESET, ret); printf(BOLD RED "result" RESET ": " BOLD "%d\n" RESET, ret);
} }
} else if (ret & RET_EXPUNGE) { } else { ret = 0; }
if (rem) {
char * s = rem;
while (*s) {
printf("%s: %sremoving '%s'\n", argv0, ret & RET_NORUN ? "not " : "", s);
if (!(ret & RET_NORUN)) {
remove(s);
}
s += strlen(s) + 1;
}
ret = 0;
}
if (ret & RET_NORUN) {
ret = 0;
}
}
free(rem);
free(buf); free(buf);
return ret; return ret;
help: help: