Readible formatting

This commit is contained in:
Emil 2023-11-13 05:13:35 +00:00
parent 1a7e71a98a
commit 62125c878b
No known key found for this signature in database
GPG Key ID: 5432DB986FDBCF8A

67
bake.c
View File

@ -39,8 +39,6 @@
"\t$* returns target-file without suffix (^-> abc.x)\n" \ "\t$* returns target-file without suffix (^-> abc.x)\n" \
"\t$+ returns arguments\n" "\t$+ returns arguments\n"
static char * g_filename;
/*** Utility functions ***/ /*** Utility functions ***/
static void static void
@ -72,7 +70,7 @@ insert(char * new, char * str, size_t offset, size_t shift) {
/*** g_short, g_all Functions ***/ /*** g_short, g_all Functions ***/
static char * g_short, * g_all; static char * g_filename, * g_short, * g_all;
static char * static char *
shorten(char * fn) { shorten(char * fn) {
@ -95,12 +93,15 @@ all_args(size_t argc, char ** argv) {
char * all = NULL; char * all = NULL;
if (argc > 2) { if (argc > 2) {
size_t i, len = argc; 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); all = malloc(len + 1);
if (!all) { return NULL; } if (!all) { return NULL; }
all[len] = '\0'; all[len] = '\0';
len = 0; for (len = 0, i = 2; i < argc; ++i) {
for (i = 2; i < argc; ++i) {
strcpy(all + len, argv[i]); strcpy(all + len, argv[i]);
len += strlen(argv[i]) + 1; len += strlen(argv[i]) + 1;
if (i + 1 < argc) { all[len - 1] = ' '; } if (i + 1 < argc) { all[len - 1] = ' '; }
@ -138,27 +139,31 @@ map(char * fn) {
static char * static char *
find_region(map_t m) { find_region(map_t m) {
char * buf = NULL; char * buf = NULL, * start, * stop;
char * start, * stop;
start = find(START, m.str, m.str + m.len); start = find(START, m.str, m.str + m.len);
if (start) { if (start) {
start += strlen(START); start += strlen(START);
#ifdef REQUIRE_SPACE #ifdef REQUIRE_SPACE
if (!isspace(*start)) { if (!isspace(*start)) {
fprintf(stderr, "ERROR: Found start without suffix spacing.\n"); fprintf(stderr, "%s: Found start without suffix spacing.\n", g_filename);
return buf; return buf;
} }
#endif /* REQUIRE_SPACE */ #endif /* REQUIRE_SPACE */
stop = find(STOP, start, start + m.len - (start - m.str)); stop = find(STOP, start, start + m.len - (start - m.str));
if (!stop) { if (!stop) {
stop = start; stop = start;
while (*stop while (*stop && *stop != '\n') {
&& *stop != '\n') {
if (stop[0] == '\\' if (stop[0] == '\\'
&& stop[1] == '\n') { stop += 2; } && stop[1] == '\n') { stop += 2; }
++stop; ++stop;
} }
} }
if (stop) if (stop)
{ buf = strndup(start, (stop - m.str) - (start - m.str)); } { buf = strndup(start, (stop - m.str) - (start - m.str)); }
} }
@ -171,13 +176,16 @@ root(char ** rootp) {
char * root = *rootp; char * root = *rootp;
size_t len = strlen(root); size_t len = strlen(root);
int ret; int ret;
while (len
&& root[len] != '/') while (len && root[len] != '/')
{ --len; } { --len; }
if (!len) { return 0; } if (!len) { return 0; }
swap(root + len, x); swap(root + len, x);
ret = chdir(root); ret = chdir(root);
swap(root + len, x); swap(root + len, x);
*rootp += len + 1; *rootp += len + 1;
return ret; return ret;
} }
@ -185,21 +193,26 @@ root(char ** rootp) {
static size_t static size_t
expand_size(char * buf, int argc, char ** argv) { expand_size(char * buf, int argc, char ** argv) {
size_t i, len, max; size_t i, len, max;
len = max = strlen(buf) + 1; len = max = strlen(buf) + 1;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
if (buf[i] == '\\') if (buf[i] == '\\') {
{ i += 2; continue; } i += 2;
else if (buf[i] == '$') { continue;
} else if (buf[i] == '$') {
switch (buf[++i]) { switch (buf[++i]) {
case '@': case '@':
max += strlen(g_filename); max += strlen(g_filename);
break; break;
case '*': case '*':
if (!g_short) { g_short = shorten(g_filename); } if (!g_short)
{ g_short = shorten(g_filename); }
max += g_short ? strlen(g_short) : 0; max += g_short ? strlen(g_short) : 0;
break; break;
case '+': 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; max += g_all ? strlen(g_all) : 0;
break; break;
} }
@ -212,10 +225,12 @@ static char *
expand(char * buf) { expand(char * buf) {
size_t i; size_t i;
char * ptr = NULL; char * ptr = NULL;
for (i = 0; buf[i]; ++i) { for (i = 0; buf[i]; ++i) {
if (buf[i] == '\\') if (buf[i] == '\\') {
{ i += 2; continue; } i += 2;
else if (buf[i] == '$') { continue;
} else if (buf[i] == '$') {
switch (buf[++i]) { switch (buf[++i]) {
case '@': case '@':
ptr = g_filename; ptr = g_filename;
@ -240,10 +255,16 @@ expand(char * buf) {
static size_t static size_t
strip(char * buf) { strip(char * buf) {
size_t i = strlen(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'; buf[i] = '\0';
for (i = 0; isspace(buf[i]); ++i); for (i = 0; isspace(buf[i]); ++i);
return i - (buf[i - 1] == '\n'); return i - (buf[i - 1] == '\n');
} }