bake/bake.c

623 lines
13 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.
*
2024-04-06 19:59:15 -04:00
* @BAKE cc -std=c89 -O2 @FILENAME -o @{@SHORT} @ARGS @STOP
2023-09-28 02:20:48 -04:00
*/
2023-09-27 01:28:54 -04:00
#define _GNU_SOURCE
#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"
2024-04-14 01:22:07 -04:00
#define VERSION "20240413"
2024-03-24 16:33:57 -04:00
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
2024-04-07 01:48:12 -04:00
#define DESC \
"Options [Must always be put first, may be merged together]\n" \
"\t" DIM "-v --version" RESET ", " DIM "-h --help" RESET ", " \
BOLD "-n --dry-run" RESET ", " BOLD "-x --expunge\n" RESET ", " \
BOLD "-c --color" RESET \
2024-04-07 01:48:12 -04:00
"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" \
"Additional Features And Notes\n" \
"\t" YELLOW "@{" RESET BOLD "EXPUNGE_THIS_FILE" YELLOW "}" RESET \
" inline region to delete this or many files or directories,\n" \
"\tnon-recursive, only one file per block, removed from left to right. This has no\n" \
"\tinfluence on the normal command execution.\n" \
"\t" YELLOW "\\" RESET \
"SPECIAL_NAME will result in SPECIAL_NAME in the executed shell command.\n" \
2024-04-06 19:59:15 -04:00
"Backslashing is applicable to all meaningful symbols in Bake, it is ignored otherwise."
2024-04-06 19:36:07 -04:00
#define COPYRIGHT "2023 Emil Williams"
#define LICENSE "Licensed under the GNU Public License version 3 only, see LICENSE."
2023-09-27 01:28:54 -04:00
#define FILENAME_LIMIT (FILENAME_MAX)
2024-03-02 05:13:24 -05:00
#define BAKE_ERROR 127
enum {
BAKE_UNRECOGNIZED,
2024-04-08 20:19:15 -04:00
BAKE_MISSING_SUFFIX
2024-03-02 05:13:24 -05:00
};
2024-04-08 19:45:48 -04:00
enum {
BAKE_RUN = 0,
BAKE_NORUN = (1 << 0),
2024-04-08 20:19:15 -04:00
BAKE_EXPUNGE = (1 << 1)
2024-04-08 19:45:48 -04:00
};
#define ARRLEN(a) (sizeof(a) / sizeof(a[0]))
#if INCLUDE_AUTONOMOUS_COMPILE
2024-04-04 22:08:50 -04:00
__attribute__((__section__(".text"))) static char autonomous_compile[] =
"@BAKE cc -std=c89 -O2 $@.c -o $@ $+ @STOP";
#endif
2024-02-26 18:34:36 -05:00
2024-03-02 05:13:24 -05:00
static int bake_errno;
typedef struct {
size_t len;
2024-04-08 19:45:48 -04:00
char * buf;
2024-04-04 22:08:50 -04:00
} string_t;
typedef string_t map_t;
/*** nocolor printf ***/
#if ENABLE_COLOR
# define color_printf(...) color_fprintf(stdout, __VA_ARGS__)
/* not perfect, too simple, doesn't work with a var, only a literal. */
# define color_puts(msg) color_fprintf(stdout, msg "\n")
int color = ENABLE_COLOR;
static char * expand(char * buf, char * macro, char * with);
color_fprintf(FILE * fp, char * format, ...) {
va_list ap;
char * buf;
va_start(ap, format);
if (!color) {
vasprintf(&buf, format, ap);
if (buf) {
expand(buf, RED, "");
expand(buf, GREEN, "");
expand(buf, YELLOW, "");
expand(buf, DIM, "");
expand(buf, BOLD, "");
expand(buf, RESET, "");
fwrite(buf, strlen(buf), 1, fp);
}
free(buf);
} else {
vfprintf(fp, format, ap);
}
va_end(ap);
}
#else
# define color_printf(...) fprintf(stdout, __VA_ARGS__)
# define color_puts(msg) puts(msg)
#endif
/*** 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;
2024-04-04 22:08:50 -04:00
while (len && root[len] != '/') {
--len;
}
2024-04-04 22:08:50 -04:00
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;
2024-04-08 20:19:15 -04:00
map_t m;
m.buf = NULL;
m.len = 0;
fd = open(fn, O_RDONLY);
2024-04-04 22:08:50 -04:00
if (fd != -1) {
2024-04-04 22:08:50 -04:00
if (!fstat(fd, &s)
&& s.st_mode & S_IFREG
&& s.st_size) {
m.len = (size_t) s.st_size;
2024-03-02 05:13:24 -05:00
m.buf = (char *) mmap(NULL, m.len, PROT_READ, MAP_SHARED, fd, 0);
}
2024-04-04 22:08:50 -04:00
close(fd);
}
2024-04-04 22:08:50 -04:00
return m;
}
static char *
find(char * buf, char * x, char * end) {
size_t xlen = strlen(x);
char * start = buf;
2024-04-04 22:08:50 -04:00
for (; buf <= end - xlen; ++buf) {
if (!strncmp(buf, x, xlen)) {
2024-04-04 22:08:50 -04:00
if (start < buf && buf[-1] == '\\') {
continue;
} else {
return buf;
}
}
}
2024-04-04 22:08:50 -04:00
return NULL;
}
2023-11-13 00:13:35 -05:00
static char *
2024-04-06 19:36:07 -04:00
get_region(string_t m, char * findstart, char * findstop) {
2024-03-02 05:13:24 -05:00
char * buf = NULL, * start, * stop, * end = m.len + m.buf;
start = find(m.buf, 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
2024-04-04 22:08:50 -04:00
if (!isspace(*start)) {
2024-03-02 05:13:24 -05:00
bake_errno = BAKE_MISSING_SUFFIX;
return NULL;
}
2024-04-04 22:08:50 -04:00
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;
2024-04-04 22:08:50 -04:00
while (stop < end && *stop != '\n') {
if (stop[0] == '\\'
2024-04-04 22:08:50 -04:00
&& stop[1] == '\n') {
stop += 2;
}
++stop;
}
}
2023-11-13 00:13:35 -05:00
2024-04-06 19:36:07 -04:00
buf = strndup(start, (size_t)(stop - start));
}
2024-04-04 22:08:50 -04:00
return buf;
2024-02-26 18:34:36 -05:00
}
static char *
2024-04-06 19:36:07 -04:00
file_get_region(char * fn, char * start, char * stop) {
2024-02-26 18:34:36 -05:00
map_t m = map(fn);
2024-04-08 19:45:48 -04:00
char * buf = NULL;
2024-04-04 22:08:50 -04:00
2024-03-02 05:13:24 -05:00
if (m.buf) {
2024-04-06 19:36:07 -04:00
buf = get_region(m, start, stop);
2024-03-02 05:13:24 -05:00
munmap(m.buf, m.len);
2024-02-26 18:34:36 -05:00
}
2024-04-04 22:08:50 -04:00
return buf;
}
2024-03-02 05:13:24 -05:00
/*** g_short, g_all ***/
static char *
shorten(char * fn) {
size_t i, last, len;
static char sh[FILENAME_LIMIT];
len = strlen(fn);
2024-04-04 22:08:50 -04:00
2024-03-02 05:13:24 -05:00
for (last = i = 0; i < len; ++i) {
2024-04-04 22:08:50 -04:00
if (fn[i] == '.') {
last = i;
}
2024-03-02 05:13:24 -05:00
}
2024-04-04 22:08:50 -04:00
2024-03-02 05:13:24 -05:00
last = last ? last : i;
strncpy(sh, fn, last);
sh[last] = '\0';
return sh;
}
static char *
all_args(size_t argc, char ** argv) {
char * all = NULL;
2024-04-04 22:08:50 -04:00
if (argc > 0) {
2024-03-02 05:13:24 -05:00
size_t i, len = argc;
2024-04-04 22:08:50 -04:00
for (i = 0; i < argc; ++i) {
len += strlen(argv[i]);
}
2024-03-02 05:13:24 -05:00
all = malloc(len + 1);
all[len] = '\0';
2024-04-04 22:08:50 -04:00
for (len = 0, i = 0; i < argc; ++i) {
2024-03-02 05:13:24 -05:00
strcpy(all + len, argv[i]);
len += strlen(argv[i]) + 1;
2024-04-04 22:08:50 -04:00
if (i + 1 < argc) {
all[len - 1] = ' ';
}
2024-03-02 05:13:24 -05:00
}
}
2024-04-04 22:08:50 -04:00
return all ? all : calloc(1, 1);
2024-03-02 05:13:24 -05:00
}
/*** 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,
2024-04-04 22:08:50 -04:00
blen = strlen(buf),
mlen = strlen(macro),
wlen = strlen(with),
nlen;
fflush(stdout);
2024-04-04 22:08:50 -04:00
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;
2024-04-04 22:08:50 -04:00
if (nlen > 0) {
2024-04-04 22:08:50 -04:00
buf = realloc(buf, blen + nlen);
}
2024-04-04 22:08:50 -04:00
insert(buf + i, with, blen - i, wlen, mlen);
blen += (nlen > 0) * nlen;
}
}
}
2024-04-04 22:08:50 -04:00
return buf;
}
2023-11-13 00:13:35 -05:00
static char *
2024-03-02 05:13:24 -05:00
bake_expand(char * buf, char * filename, int argc, char ** argv) {
enum {
MACRO_FILENAME,
MACRO_SHORT,
MACRO_ARGS,
MACRO_STOP,
MACRO_NONE
};
char * macro[MACRO_NONE],
2024-04-08 20:19:15 -04:00
* macro_old[MACRO_STOP],
* global[MACRO_NONE];
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 ] = "$+";
2024-03-02 05:13:24 -05:00
global[MACRO_FILENAME] = filename;
global[MACRO_SHORT ] = shorten(filename);
global[MACRO_ARGS ] = all_args((size_t) argc, argv);
global[MACRO_STOP ] = "";
#if NEW_MACROS
2024-04-04 22:08:50 -04:00
for (i = 0; i < ARRLEN(macro); ++i) {
buf = expand(buf, macro[i], global[i]);
2023-09-28 01:02:34 -04:00
}
2024-04-04 22:08:50 -04:00
#endif
#if OLD_MACROS
2024-04-04 22:08:50 -04:00
for (i = 0; i < ARRLEN(macro_old); ++i) {
buf = expand(buf, macro_old[i], global[i]);
}
2024-04-04 22:08:50 -04:00
#endif
2024-03-02 05:13:24 -05:00
free(global[MACRO_ARGS]);
2023-11-13 00:13:35 -05:00
2024-03-02 05:13:24 -05:00
return buf;
}
2023-11-13 00:13:35 -05:00
2024-04-08 19:45:48 -04:00
static void
remove_expand(char * buf, char * argv0, int rm, char * start, char * stop) {
size_t i, f, end = strlen(buf);
size_t startlen = strlen(start), stoplen = strlen(stop);
char x[1] = {'\0'};
2024-04-06 19:36:07 -04:00
for (i = 0; i < end; ++i) {
2024-04-08 19:45:48 -04:00
if (!strncmp(buf + i, start, startlen)) {
2024-04-06 19:36:07 -04:00
if (buf + i > buf && buf[i - 1] == '\\') {
continue;
}
for (f = i; f < end; ++f) {
2024-04-08 19:45:48 -04:00
if (!strncmp(buf + f, stop, stoplen)) {
2024-04-06 19:36:07 -04:00
if (buf + f > buf && buf[f - 1] == '\\') {
continue;
}
insert(buf + f, "", end - f, 0, 1);
2024-04-08 19:45:48 -04:00
i += startlen;
if (rm & BAKE_EXPUNGE) {
swap(buf + i + (f - i), x);
2024-04-08 20:19:15 -04:00
#if !ENABLE_EXPUNGE_REMOVE
color_printf("%s: %sremoving '%s'\n",
argv0, rm & BAKE_NORUN ? "not " : "", buf + i);
2024-04-08 19:45:48 -04:00
if (!(rm & BAKE_NORUN)) {
remove(buf + i);
}
2024-04-08 20:19:15 -04:00
#else
color_printf("%s: not removing '%s'\n", argv0, buf + i);
2024-04-08 20:19:15 -04:00
#endif
2024-04-08 19:45:48 -04:00
swap(buf + i + (f - i), x);
}
2024-04-06 19:36:07 -04:00
goto next;
}
}
goto stop;
}
2024-04-08 20:19:15 -04:00
next:;
2024-04-06 19:36:07 -04:00
}
stop:
2024-04-08 19:45:48 -04:00
expand(buf, start, "");
2024-04-06 19:36:07 -04:00
}
2024-04-04 22:08:50 -04: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);
2024-04-04 22:08:50 -04:00
if (!i) {
return 0;
}
while (i && isspace(buf[i - 1])) {
--i;
}
buf[i] = '\0';
2024-04-04 22:08:50 -04:00
for (i = 0; isspace(buf[i]); ++i);
2024-04-04 22:08:50 -04:00
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
2024-03-02 05:13:24 -05:00
run(char * buf, char * argv0) {
pid_t pid;
color_puts(BOLD GREEN "output" RESET ":\n");
2024-04-04 22:08:50 -04:00
2024-03-02 05:13:24 -05:00
if ((pid = fork()) == 0) {
execl("/bin/sh", "sh", "-c", buf, NULL);
return 0; /* execl overwrites the process anyways */
} else if (pid == -1) {
color_fprintf(stderr, BOLD RED "%s" RESET ": %s, %s\n",
argv0, "Fork Error", strerror(errno));
2024-03-02 05:13:24 -05:00
return BAKE_ERROR;
2024-02-26 18:34:36 -05:00
} else {
int status;
2024-04-04 22:08:50 -04:00
if (waitpid(pid, &status, 0) < 0) {
color_fprintf(stderr, BOLD RED "%s" RESET ": %s, %s\n",
argv0, "Wait PID Error", strerror(errno));
2024-04-04 22:08:50 -04:00
return BAKE_ERROR;
}
if (!WIFEXITED(status)) {
2024-03-02 05:13:24 -05:00
return BAKE_ERROR;
}
2024-04-04 22:08:50 -04:00
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) {
2024-04-08 19:45:48 -04:00
int ret = BAKE_RUN;
char * buf = NULL,
* filename,
* argv0;
argv0 = argv[0];
2024-04-06 19:36:07 -04:00
if (argc < 2) {
goto help;
}
2024-04-04 22:08:50 -04:00
while (++argv, --argc && argv[0][0] == '-') {
++argv[0];
if (argv[0][1] == '-') {
++argv[0];
if (!strcmp(argv[0], "help")) {
goto help;
} else if (!strcmp(argv[0], "version")) {
goto version;
} else if (!strcmp(argv[0], "expunge")) {
2024-04-08 19:45:48 -04:00
ret |= BAKE_EXPUNGE;
2024-04-04 22:08:50 -04:00
} else if (!strcmp(argv[0], "dry-run")) {
2024-04-08 19:45:48 -04:00
ret |= BAKE_NORUN;
} else if (!strcmp(argv[0], "color")) {
#if ENABLE_COLOR
color = 0;
#endif
2024-04-04 22:08:50 -04:00
} else {
goto help;
}
} else do {
switch (argv[0][0]) {
case 'h':
goto help;
case 'v':
goto version;
case 'x':
2024-04-08 19:45:48 -04:00
ret |= BAKE_EXPUNGE;
2024-04-04 22:08:50 -04:00
break;
case 'n':
2024-04-08 19:45:48 -04:00
ret |= BAKE_NORUN;
2024-04-04 22:08:50 -04:00
break;
#if ENABLE_COLOR
case 'c':
color = 0;
break;
#endif
2024-04-04 22:08:50 -04:00
case 0 :
goto next;
default :
color_puts("UNKNOWN");
2024-04-04 22:08:50 -04:00
goto help;
}
} while (++(argv[0]));
2024-04-08 20:19:15 -04:00
next:;
2023-09-29 03:05:40 -04:00
}
2023-09-28 02:14:17 -04:00
2024-04-04 22:08:50 -04:00
filename = argv[0];
++argv, --argc;
2024-03-02 05:13:24 -05:00
if (strlen(filename) > FILENAME_LIMIT) {
color_fprintf(stderr, BOLD RED "%s" RESET
": Filename too long (exceeds %d)\n",
argv0,
FILENAME_LIMIT);
2024-03-02 05:13:24 -05:00
return BAKE_ERROR;
}
root(&filename);
2024-04-06 19:36:07 -04:00
buf = file_get_region(filename, START, STOP);
if (!buf) {
2024-04-08 19:45:48 -04:00
char * error[2];
error[0] = "File Unrecognized";
error[1] = "Found start without suffix spacing";
color_fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' %s.\n",
argv0, filename, errno ? strerror(errno) : error[bake_errno]);
2024-03-02 05:13:24 -05:00
return BAKE_ERROR;
2023-09-29 03:05:40 -04:00
}
2023-09-28 02:14:17 -04:00
2024-03-02 05:13:24 -05:00
buf = bake_expand(buf, filename, argc, argv);
color_printf(BOLD GREEN "%s" RESET ": %s\n", argv0, buf + strip(buf));
2023-09-28 02:14:17 -04:00
2024-04-08 19:45:48 -04:00
remove_expand(buf, argv0, ret, EXPUNGE_START, EXPUNGE_STOP);
2024-04-04 22:08:50 -04:00
if (!ret) {
ret = run(buf, argv0);
if (ret) {
color_printf(BOLD RED "result" RESET ": " BOLD "%d\n" RESET, ret);
2024-04-04 22:08:50 -04:00
}
2024-04-08 19:45:48 -04:00
} else { ret = 0; }
2024-04-04 22:08:50 -04:00
free(buf);
2023-09-27 01:28:54 -04:00
return ret;
help:
color_fprintf(stderr, YELLOW "%s" RESET ": %s\n", argv0, HELP DESC);
2024-03-02 05:13:24 -05:00
return BAKE_ERROR;
2024-03-24 16:33:57 -04:00
version:
color_fprintf(stderr,
YELLOW "%s" RESET ": v" VERSION "\n"
"Copyright " COPYRIGHT "\n" LICENSE "\n",
argv0);
2024-03-24 16:33:57 -04:00
return BAKE_ERROR;
2023-09-27 01:28:54 -04:00
}