Merge remote-tracking branch 'origin/run'

This commit is contained in:
Emil Williams 2024-02-27 04:24:57 +00:00
commit abc3ee6114
2 changed files with 176 additions and 160 deletions

324
bake.c
View File

@ -3,7 +3,7 @@
* *
* Licensed under the GNU Public License version 3 only, see LICENSE. * Licensed under the GNU Public License version 3 only, see LICENSE.
* *
* @BAKE cc -std=c89 -O2 -I. $@ -o $* $+ # @STOP * @BAKE cc -std=c99 -O2 $@ -o $* $+ # @STOP
*/ */
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
@ -20,13 +20,11 @@
#include <string.h> #include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include "config.h" #include "config.h"
#define START "@BAKE"
#define STOP "@STOP"
#define HELP \ #define HELP \
BOLD "[option] target-file" RESET " [" GREEN "arguments" RESET " ...]\n" \ BOLD "[option] target-file" RESET " [" GREEN "arguments" RESET " ...]\n" \
"Use the format `" BOLD "@BAKE" RESET " cmd ...' within the target-file, this will execute the\n" \ "Use the format `" BOLD "@BAKE" RESET " cmd ...' within the target-file, this will execute the\n" \
@ -40,8 +38,21 @@
"\t" YELLOW "$*" RESET " returns target-file without suffix (^-> abc.x)\n" \ "\t" YELLOW "$*" RESET " returns target-file without suffix (^-> abc.x)\n" \
"\t" YELLOW "$+" RESET " returns " GREEN "arguments" RESET "\n" "\t" YELLOW "$+" RESET " returns " GREEN "arguments" RESET "\n"
typedef struct {
size_t len;
char * buf;
} string_t;
typedef string_t map_t;
static string_t START = { 5, "@BAKE" };
static string_t STOP = { 5, "@STOP" };
/*** Utility functions ***/ /*** Utility functions ***/
#define strneq(a,b,n) (!strncmp(a,b,n))
#define streq(a,b) (!strcmp(a,b))
static void static void
swap(char * a, char * b) { swap(char * a, char * b) {
*a ^= *b; *a ^= *b;
@ -50,74 +61,72 @@ swap(char * a, char * b) {
} }
static char * static char *
find(char * x, char * buf, char * end) { find(string_t x, char * buf, char * end) {
size_t len = strlen(x); for (; (buf < end) && x.len < (size_t)(end - buf); ++buf) {
for (; (buf < end) && len < (size_t)(end - buf); ++buf) { if (!strncmp(buf, x.buf, x.len))
if (!strncmp(buf, x, len))
{ return buf; } { return buf; }
} }
return NULL; return NULL;
} }
static char * static void
insert(char * new, char * str, size_t offset, size_t shift) { insert(char * str, size_t slen, char * new, size_t len, size_t shift) {
size_t len, max; memmove(str + len, str + shift, slen - len - shift);
len = strlen(new); memcpy(str, new, len);
max = (strlen(str) + 1 - offset - shift);
memmove(str + offset + len, str + offset + shift, max);
memcpy(str + offset, new, len);
return str;
} }
/*** g_short, g_all Functions ***/ /*** g_short, g_all Functions ***/
static char * g_filename, * g_short, * g_all; #define GLOBALS_COUNT 3
static char * static string_t globals[GLOBALS_COUNT];
shorten(char * fn) {
size_t i, last = 0, len; #define g_filename globals[0]
char * sh; #define g_short globals[1]
len = strlen(fn); #define g_all globals[2]
sh = malloc(len + 1);
if (!sh) { return NULL; } static string_t
for (i = 0; i < len; ++i) { shorten(string_t s) {
if (fn[i] == '.') { last = i; } size_t i, last = 0, len = s.len;
char * sh, * fn = s.buf;
sh = malloc(len);
if (sh) {
for (i = 0; i < len; ++i) {
if (fn[i] == '.') { last = i; }
}
last = last ? last : i;
strncpy(sh, fn, last);
sh[last] = '\0';
} }
last = last ? last : i; return (string_t) { last, sh ? sh : calloc(0,0) };
strncpy(sh, fn, last);
sh[last] = '\0';
return sh;
} }
static char * static string_t
all_args(size_t argc, char ** argv) { all_args(int argc, char ** argv) {
char * all = NULL; string_t s = (string_t) { 0, NULL };
if (argc > 2) { if (argc > 2) {
size_t i, len = argc; size_t i, len = 0;
for (i = 2; i < (size_t) argc; ++i) {
for (i = 2; i < argc; ++i) len += strlen(argv[i]);
{ len += strlen(argv[i]); } }
s.buf = malloc(len);
all = malloc(len + 1); s.len = len;
if (!all) { return NULL; } if (s.buf) {
for (len = 0, i = 2; i < (size_t) argc; ++i) {
all[len] = '\0'; strcpy(s.buf + len, argv[i]);
for (len = 0, i = 2; i < argc; ++i) { len += strlen(argv[i]);
strcpy(all + len, argv[i]); if (i + 1 < argc) {
len += strlen(argv[i]) + 1; s.buf[len - 1] = ' ';
if (i + 1 < argc) { all[len - 1] = ' '; } len++;
}
}
} }
} }
return all; return s;
} }
/*** Map ***/ /*** Map ***/
typedef struct {
char * str;
size_t len;
} map_t;
static map_t static map_t
map(char * fn) { map(char * fn) {
struct stat s; struct stat s;
@ -129,7 +138,7 @@ map(char * fn) {
&& s.st_mode & S_IFREG && s.st_mode & S_IFREG
&& s.st_size) { && s.st_size) {
m.len = (size_t) s.st_size; m.len = (size_t) s.st_size;
m.str = (char *) mmap(NULL, m.len, PROT_READ, MAP_SHARED, fd, 0); m.buf = (char *) mmap(NULL, m.len, PROT_READ, MAP_SHARED, fd, 0);
} }
close(fd); close(fd);
} }
@ -138,24 +147,25 @@ map(char * fn) {
/*** Important Functions ***/ /*** Important Functions ***/
static char * static string_t
find_region(map_t m) { find_region(map_t m, string_t startsym, string_t stopsym) {
extern char * strndup(const char * s, size_t n); /* for splint */ extern char * strndup(const char * s, size_t n); /* for splint */
char * buf = NULL, * start, * stop; char * buf = NULL, * start, * stop;
size_t len;
start = find(START, m.str, m.str + m.len); start = find(startsym, m.buf, m.buf + m.len);
if (start) { if (start) {
start += strlen(START); start += startsym.len;
#ifdef REQUIRE_SPACE #ifdef REQUIRE_SPACE
if (!isspace(*start)) { if (!isspace(*start)) {
fprintf(stderr, RED "%s" RESET ": Found start without suffix spacing.\n", g_filename); fprintf(stderr, RED "%s" RESET ": Found start without suffix spacing.\n", g_filename.buf);
return buf; return (string_t) { 0 , buf };
} }
#endif /* REQUIRE_SPACE */ #endif /* REQUIRE_SPACE */
stop = find(STOP, start, start + m.len - (start - m.str)); stop = find(stopsym, start, start + m.len - (start - m.buf));
if (!stop) { if (!stop) {
stop = start; stop = start;
@ -167,16 +177,30 @@ find_region(map_t m) {
} }
if (stop) if (stop)
{ buf = strndup(start, (size_t) (stop - m.str) - (start - m.str)); } {
len = (size_t) (stop - m.buf) - (start - m.buf);
buf = strndup(start, len);
}
} }
return buf; return (string_t) { len, buf };
}
static string_t
file_find_region(char * fn, string_t start, string_t stop) {
string_t s = { 0, NULL };
map_t m = map(fn);
if (m.buf) {
s = find_region(m, start, stop);
munmap(m.buf, m.len);
}
return s;
} }
static int static int
root(char ** rootp) { root(string_t s) {
char x[1] = {'\0'}; char x[1] = {'\0'};
char * root = *rootp; char * root = s.buf;
size_t len = strlen(root); size_t len = s.len;
int ret; int ret;
while (len && root[len] != '/') while (len && root[len] != '/')
@ -188,139 +212,131 @@ root(char ** rootp) {
ret = chdir(root); ret = chdir(root);
swap(root + len, x); swap(root + len, x);
*rootp += len + 1; /* *rootp += len + 1; */
return ret; return ret;
} }
static size_t #define ARRLEN(x) (sizeof(x) / sizeof(x[0]))
expand_size(char * buf, int argc, char ** argv) {
size_t i, len, max;
len = max = strlen(buf) + 1; static string_t
expand(string_t s) {
enum {
MACRO_FILENAME = 0,
MACRO_SHORT = 1,
MACRO_ARGS = 2,
};
for (i = 0; i < len; ++i) { string_t macro[] = {
if (buf[i] == '\\') { [MACRO_FILENAME] = { 2, "$@" },
i += 2; [MACRO_SHORT ] = { 2, "$*" },
continue; [MACRO_ARGS ] = { 2, "$+" },
} else if (buf[i] == '$') { };
switch (buf[++i]) {
case '@': size_t i, f;
max += strlen(g_filename);
break; {
case '*': size_t max = s.len;
if (!g_short) for (i = 0; i < s.len; ++i) {
{ g_short = shorten(g_filename); } for (f = 0; f < ARRLEN(macro); ++f) {
max += g_short ? strlen(g_short) : 0; if (!strncmp(s.buf + i, macro[f].buf, macro[f].len)) {
break; max += globals[f].len;
case '+': }
if (!g_all) }
{ g_all = all_args((size_t) argc, argv); } }
max += g_all ? strlen(g_all) : 0; s.buf = realloc(s.buf, max + 27); /* I don't know, man */
break; if (!s.buf) { return (string_t) { 0, NULL}; }
memset(s.buf + s.len, 0, max - s.len);
s.len = max;
}
for (i = 0; i < s.len; ++i) {
for (f = 0; f < ARRLEN(macro); ++f) {
if (!strncmp(s.buf + i, macro[f].buf, macro[f].len)) {
insert(s.buf + i, s.len - i, globals[f].buf, globals[f].len, 2);
} }
} }
} }
return max; return s;
}
static char *
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]) {
case '@':
ptr = g_filename;
break;
case '*':
ptr = g_short;
break;
case '+':
ptr = g_all ? g_all : "";
break;
default: continue;
}
buf = insert(ptr, buf, i - 1, 2);
}
}
free(g_short); free(g_all);
return buf;
} }
/* Strips all prefixing and leading whitespace. /* Strips all prefixing and leading whitespace.
* Except if the last character beforehand is a newline. */ * Except if the last character beforehand is a newline. */
static size_t static size_t
strip(char * buf) { strip(string_t s) {
size_t i = strlen(buf); size_t i = s.len;
if (!i) if (!i)
{ return 0; } { return 0; }
while (isspace(buf[i - 1])) while (isspace(s.buf[i]))
{ --i; } { --i; }
buf[i] = '\0'; s.buf[i] = '\0';
for (i = 0; isspace(buf[i]); ++i); for (i = 0; isspace(s.buf[i]); ++i);
return i - (buf[i - 1] == '\n'); return i - (s.buf[i - 1] == '\n');
} }
static int static int
run(char * buf) { run(char * buf) {
fputs(GREEN "output" RESET ":\n", stderr); int ret = 127;
return system(buf); fputs(BOLD GREEN "output" RESET ":\n", stderr);
pid_t pid = fork();
if (!pid) {
execl("/bin/sh", "sh", "-c", buf, NULL);
} else {
int status;
waitpid(pid, &status, 0);
if (!WIFEXITED(status)) { ret = 126; }
else { ret = WEXITSTATUS(status); }
}
return ret;
} }
int int
main(int argc, char ** argv) { main(int argc, char ** argv) {
int ret = 0; int ret = 0;
char * buf = NULL; string_t s = { 0, NULL };
if (argc < 2 if (argc < 2
|| !strcmp(argv[1], "-h") || streq(argv[1], "-h")
|| !strcmp(argv[1], "--help")) || streq(argv[1], "--help"))
{ goto help; } { goto help; }
g_filename = argv[1]; g_filename = (string_t) { strlen(argv[1]), argv[1] };
if (!strcmp(argv[1], "-n") if (streq(argv[1], "-n")
|| !strcmp(argv[1], "--dry-run")) { || streq(argv[1], "--dry-run")) {
if (argc > 2) { ret = 1; g_filename = argv[2]; } if (argc > 2) {
ret = 1;
g_filename = (string_t) { strlen(argv[2]), argv[2] };
}
else { goto help; } else { goto help; }
} }
{ map_t m = map(g_filename); s = file_find_region(g_filename.buf, START, STOP);
if (m.str) {
buf = find_region(m);
munmap(m.str, m.len);
}
}
if (!buf) { if (!s.buf) {
if (errno) { fprintf(stderr, BOLD RED "%s" RESET ": %s\n", g_filename, strerror(errno)); } if (errno)
else { fprintf(stderr, BOLD RED "%s" RESET ": File unrecognized.\n", g_filename); } { fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' %s\n", argv[0], g_filename.buf, strerror(errno)); }
else
{ fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' File unrecognized.\n", argv[0], g_filename.buf); }
return 1; return 1;
} }
root(&g_filename); g_all = all_args(argc, argv);
{ char * buf2 = buf; g_short = shorten(g_filename);
buf = realloc(buf, expand_size(buf, argc, argv)); root(g_filename);
if (!buf)
{ free(buf2); free(g_short); free(g_all); return 1; }
}
buf = expand(buf);
fprintf(stderr, GREEN "%s" RESET ": %s\n", argv[0], buf + strip(buf)); s = expand(s);
ret = ret ? 0 : run(buf); free(g_short.buf); free(g_all.buf);
if (!s.buf) { return 1; }
fprintf(stderr, BOLD GREEN "%s" RESET ": %s\n", argv[0], s.buf + strip(s));
ret = ret ? 0 : run(s.buf);
if (ret) if (ret)
{ fprintf(stderr, RED "result" RESET ": " BOLD "%d\n" RESET, ret); } { fprintf(stderr, BOLD RED "result" RESET ": " BOLD "%d\n" RESET, ret); }
free(buf); free(s.buf);
return ret; return ret;
help: help:
fprintf(stderr, YELLOW "%s" RESET ": %s", argv[0], HELP DESC); fprintf(stderr, YELLOW "%s" RESET ": %s", argv[0], HELP DESC);

View File

@ -4,12 +4,12 @@
#define ENABLE_COLOR #define ENABLE_COLOR
#ifdef ENABLE_COLOR #ifdef ENABLE_COLOR
# define RED "\e[91m" # define RED "\033[91m"
# define GREEN "\e[92m" # define GREEN "\033[92m"
# define YELLOW "\e[93m" # define YELLOW "\033[93m"
# define DIM "\e[2m" # define DIM "\033[2m"
# define BOLD "\e[1m" # define BOLD "\033[1m"
# define RESET "\e[0m" # define RESET "\033[0m"
#else #else
# define RED # define RED
# define GREEN # define GREEN