bake/bake.c

378 lines
8.6 KiB
C
Raw Normal View History

/* bake.c - Ever burned a cake?
2023-09-28 14:32:53 -04:00
* Copyright 2023 Emil Williams
*
* Licensed under the GNU Public License version 3 only, see LICENSE.
*
* @BAKE cc -std=c89 -O2 -I. @FILENAME -o @SHORT @ARGS @STOP
2023-09-28 02:20:48 -04:00
*/
2023-09-27 01:28:54 -04:00
/*#define POSIXLY_CORRECT*/
#define _POSIX_C_SOURCE 200809L
2023-09-27 01:28:54 -04:00
#include <assert.h>
2023-09-28 14:32:53 -04:00
#include <ctype.h>
2023-09-27 01:28:54 -04:00
#include <errno.h>
2023-09-29 03:05:40 -04:00
#include <fcntl.h>
2023-10-06 22:17:40 -04:00
#include <locale.h>
2023-09-27 01:28:54 -04:00
#include <stdarg.h>
#include <stddef.h>
2023-09-27 01:28:54 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2023-09-29 03:05:40 -04:00
#include <sys/mman.h>
2023-09-27 01:28:54 -04:00
#include <sys/stat.h>
2024-02-26 18:34:36 -05:00
#include <sys/wait.h>
2023-09-28 14:32:53 -04:00
#include <unistd.h>
#include <limits.h>
2023-09-27 01:28:54 -04:00
2024-01-19 16:04:32 -05:00
#include "config.h"
#define START "@BAKE"
#define STOP "@STOP"
2024-01-21 08:50:21 -05:00
#define HELP \
BOLD "[option] target-file" RESET " [" GREEN "arguments" RESET " ...]\n" \
2024-01-19 19:16:54 -05:00
"Use the format `" BOLD "@BAKE" RESET " cmd ...' within the target-file, this will execute the\n" \
"rest of line, or if found within the file, until the " BOLD "@STOP" RESET " marker.\n" \
2024-01-19 15:57:41 -05:00
#define DESC \
"Options [Must always be first]\n" \
"\t" DIM "-h --help" RESET", " BOLD "-n --dry-run\n" RESET \
"Expansions\n" \
"\t" YELLOW "@FILENAME" RESET " returns target-file (abc.x.txt)\n" \
"\t" YELLOW "@SHORT " RESET " returns target-file without suffix (^-> abc.x)\n" \
"\t" YELLOW "@ARGS " RESET " returns " GREEN "arguments" RESET "\n"
2023-09-27 01:28:54 -04:00
#define FILENAME_LIMIT (FILENAME_MAX)
#define ARRLEN(a) (sizeof(a) / sizeof(a[0]))
#if INCLUDE_AUTONOMOUS_COMPILE
__attribute__((__section__(".text"))) static char autonomous_compile[] = "@BAKE cc -std=c89 $@.c -o $@ $+ @STOP";
#endif
2024-02-26 18:34:36 -05:00
static char * argv0;
static char * global[4];
2023-09-27 18:50:55 -04:00
#define g_filename global[0]
#define g_short global[1]
#define g_all global[2]
#define g_stop global[3]
typedef struct {
char * str;
size_t len;
} map_t;
/*** root ***/
2024-02-26 18:34:36 -05:00
2023-09-27 01:28:54 -04:00
static void
swap(char * a, char * b) {
2023-09-27 01:28:54 -04:00
*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);
2023-09-27 18:50:55 -04:00
*rootp += len + 1;
return ret;
2023-09-27 01:28:54 -04:00
}
/*** find region in file ***/
static map_t
map(char * fn) {
struct stat s;
int fd;
map_t m = (map_t) {0};
fd = open(fn, O_RDONLY);
if (fd != -1) {
if (!fstat(fd,&s)
&& s.st_mode & S_IFREG
&& s.st_size) {
m.len = (size_t) s.st_size;
m.str = (char *) mmap(NULL, m.len, PROT_READ, MAP_SHARED, fd, 0);
}
close(fd);
}
return m;
}
static char *
find(char * buf, char * x, char * end) {
size_t xlen = strlen(x);
char * start = buf;
for (; buf < end - xlen; ++buf) {
if (!strncmp(buf, x, xlen)) {
if (start < buf && buf[-1] == '\\') { continue; }
else { return buf; }
}
}
return NULL;
}
2023-11-13 00:13:35 -05:00
static char *
find_region(map_t m, char * findstart, char * findstop) {
char * buf = NULL, * start, * stop, * end = m.len + m.str;
start = find(m.str, findstart, end);
2023-11-13 00:13:35 -05:00
if (start) {
start += strlen(findstart);
2023-11-13 00:13:35 -05:00
#ifdef REQUIRE_SPACE
if (!isspace(*start)) {
fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' Found start without suffix spacing.\n", argv0, g_filename);
return buf;
}
2023-10-16 15:16:46 -04:00
#endif /* REQUIRE_SPACE */
2023-11-13 00:13:35 -05:00
stop = find(start, findstop, end);
2023-11-13 00:13:35 -05:00
if (!stop) {
stop = start;
while (stop < end && *stop != '\n') {
if (stop[0] == '\\'
2023-11-13 00:13:35 -05:00
&& stop[1] == '\n') { stop += 2; }
++stop;
}
}
2023-11-13 00:13:35 -05:00
2023-10-16 15:16:46 -04:00
if (stop)
{ buf = strndup(start, (size_t) (stop - m.str) - (start - m.str)); }
}
return buf;
2024-02-26 18:34:36 -05:00
}
static char *
file_find_region(char * fn, char * start, char * stop) {
char * buf = NULL;
2024-02-26 18:34:36 -05:00
map_t m = map(fn);
if (m.str) {
buf = find_region(m, start, stop);
munmap(m.str, m.len);
2024-02-26 18:34:36 -05:00
}
return buf;
}
/*** insert, expand, bake_expand ***/
2023-11-13 00:13:35 -05:00
static void
insert(char * str, char * new, size_t slen, size_t nlen, size_t shift) {
memmove(str + nlen, str + shift, slen + 1 - shift);
memcpy(str, new, nlen);
}
static char *
expand(char * buf, char * macro, char * with) {
ssize_t i,
blen = strlen(buf),
mlen = strlen(macro),
wlen = strlen(with),
nlen;
fflush(stdout);
for (i = 0; i < blen - mlen + 1; ++i) {
if (!strncmp(buf + i, macro, mlen)) {
if (i && buf[i - 1] == '\\') {
memmove(buf + i - 1, buf + i, blen - i);
buf[blen - 1] = '\0';
} else {
nlen = wlen - mlen + 1;
if (nlen > 0) {
buf = realloc(buf, blen + nlen);
}
insert(buf + i, with, blen - i, wlen, mlen);
blen += (nlen > 0) * nlen;
}
}
}
return buf;
}
2023-11-13 00:13:35 -05:00
static char *
bake_expand(char * buf) {
enum {
MACRO_FILENAME,
MACRO_SHORT,
MACRO_ARGS,
MACRO_STOP,
MACRO_NONE
};
char * macro[MACRO_NONE],
* macro_old[MACRO_STOP];
size_t i;
macro[MACRO_FILENAME] = "@FILENAME";
macro[MACRO_SHORT ] = "@SHORT";
macro[MACRO_ARGS ] = "@ARGS";
macro[MACRO_STOP ] = "@STOP";
macro_old[MACRO_FILENAME] = "$@";
macro_old[MACRO_SHORT ] = "$*";
macro_old[MACRO_ARGS ] = "$+";
#if NEW_MACROS
for (i = 0; i < ARRLEN(macro); ++i) {
buf = expand(buf, macro[i], global[i]);
2023-09-28 01:02:34 -04:00
}
#endif
#if OLD_MACROS
for (i = 0; i < ARRLEN(macro_old); ++i) {
buf = expand(buf, macro_old[i], global[i]);
}
#endif
return buf;
}
/*** g_short, g_all ***/
2023-11-13 00:13:35 -05:00
static char *
shorten(char * fn) {
size_t i, last, len;
static char sh[FILENAME_LIMIT];
len = strlen(fn);
for (last = i = 0; i < len; ++i) {
if (fn[i] == '.') { last = i; }
}
last = last ? last : i;
strncpy(sh, fn, last);
sh[last] = '\0';
return sh;
}
2023-11-13 00:13:35 -05:00
static char *
all_args(size_t argc, char ** argv) {
char * all = NULL;
if (argc > 2) {
size_t i, len = argc;
for (i = 2; i < argc; ++i)
{ len += strlen(argv[i]); }
all = malloc(len + 1);
all[len] = '\0';
for (len = 0, i = 2; i < argc; ++i) {
strcpy(all + len, argv[i]);
len += strlen(argv[i]) + 1;
if (i + 1 < argc) { all[len - 1] = ' '; }
}
}
return all ? all : calloc(1,1);
}
2023-11-13 00:13:35 -05:00
/*** strip, run ***/
2023-11-13 00:13:35 -05:00
/* Strips all prefixing and leading whitespace.
* Except if the last character beforehand is a newline. */
static size_t
strip(char * buf) {
size_t i = strlen(buf);
if (!i) { return 0; }
while (i && isspace(buf[i - 1])) { --i; }
buf[i] = '\0';
for (i = 0; isspace(buf[i]); ++i);
if (i && buf[i - 1] == '\n') { --i; }
return i;
2023-09-28 14:32:53 -04:00
}
2023-09-28 02:14:17 -04:00
static int
run(char * buf) {
2024-02-26 23:04:18 -05:00
fputs(BOLD GREEN "output" RESET ":\n", stderr);
2024-02-26 18:34:36 -05:00
pid_t pid = fork();
if (pid == 0) {
if (execl("/bin/sh", "sh", "-c", buf, NULL) == -1) {
fprintf(stderr, BOLD RED "%s" RESET ": %s, %s\n", argv0, "Exec Error", strerror(errno));
}
return 0;
} else if (pid == -1) {
fprintf(stderr, BOLD RED "%s" RESET ": %s, %s\n", argv0, "Fork Error", strerror(errno));
return -1;
2024-02-26 18:34:36 -05:00
} else {
int status;
if (waitpid(pid, &status, 0) < 0) {
fprintf(stderr, BOLD RED "%s" RESET ": %s, %s\n", argv0, "Wait PID Error", strerror(errno));
return -1;
}
if (!WIFEXITED(status)) { return -1; }
return WEXITSTATUS(status);
2024-02-26 18:34:36 -05:00
}
2023-09-28 02:14:17 -04:00
}
/*** main ***/
2023-09-27 01:28:54 -04:00
int
main(int argc, char ** argv) {
2023-09-28 02:14:17 -04:00
int ret = 0;
char * buf = NULL;
argv0 = argv[0];
2023-09-28 02:14:17 -04:00
if (argc < 2
|| !strcmp(argv[1], "-h")
|| !strcmp(argv[1], "--help"))
{ goto help; }
2023-09-28 02:14:17 -04:00
if (!strcmp(argv[1], "-n")
|| !strcmp(argv[1], "--dry-run")) {
2024-02-26 18:34:36 -05:00
if (argc > 2) {
ret = 1;
--argc;
++argv;
2024-02-26 18:34:36 -05:00
}
else { goto help; }
2023-09-29 03:05:40 -04:00
}
2023-09-28 02:14:17 -04:00
g_filename = argv[1];
if (strlen(g_filename) > FILENAME_LIMIT) { goto fnerr; }
root(&g_filename);
buf = file_find_region(g_filename, START, STOP);
if (!buf) {
if (errno)
{ fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' %s\n", argv0, g_filename, strerror(errno)); }
else
{ fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' File unrecognized.\n", argv0, g_filename); }
2023-09-29 03:05:40 -04:00
return 1;
}
2023-09-28 02:14:17 -04:00
g_short = shorten(g_filename);
g_all = all_args((size_t) argc, argv);
g_stop = "";
buf = bake_expand(buf);
2024-02-26 18:34:36 -05:00
free(g_all);
fprintf(stderr, BOLD GREEN "%s" RESET ": %s\n", argv0, buf + strip(buf));
ret = ret ? 0 : run(buf);
if (ret)
2024-02-26 23:04:18 -05:00
{ fprintf(stderr, BOLD RED "result" RESET ": " BOLD "%d\n" RESET, ret); }
2023-09-28 02:14:17 -04:00
free(buf);
2023-09-27 01:28:54 -04:00
return ret;
help:
fprintf(stderr, YELLOW "%s" RESET ": %s", argv0, HELP DESC);
return 1;
fnerr:
fprintf(stderr, BOLD RED "%s" RESET ": Filename too long (exceeds %d)\n", argv0, FILENAME_LIMIT);
2023-09-27 01:28:54 -04:00
}