2023-10-15 20:56:16 -04:00
|
|
|
/* 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.
|
|
|
|
*
|
2023-10-16 03:53:44 -04:00
|
|
|
* For Bake
|
|
|
|
* @EXEC cc $@ -o $* -std=gnu89 -O2 -Wall -Wextra -Wpedantic -pipe $CFLAGS @STOP
|
|
|
|
*
|
|
|
|
* For Shake
|
|
|
|
* @COMPILECMD cc $@ -o $* -std=gnu89 -O2 -Wall -Wextra -Wpedantic -pipe $CFLAGS
|
2023-09-28 02:20:48 -04:00
|
|
|
*/
|
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>
|
2023-10-09 21:59:43 -04:00
|
|
|
#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>
|
2023-09-28 14:32:53 -04:00
|
|
|
#include <unistd.h>
|
2023-09-27 01:28:54 -04:00
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
/* Require space after COMPILECMD/EXEC and before STOP (no space required around newline) */
|
2023-10-09 20:10:14 -04:00
|
|
|
#define REQUIRE_SPACE
|
2023-10-15 20:56:16 -04:00
|
|
|
/* May be be left undefined, comes second */
|
2023-10-16 03:53:44 -04:00
|
|
|
/* #define OTHER_START "@COMPILECMD" */
|
2023-10-09 20:10:14 -04:00
|
|
|
|
2023-10-16 03:53:44 -04:00
|
|
|
#define START "@EXEC"
|
2023-10-16 01:48:43 -04:00
|
|
|
#define STOP "@STOP"
|
2023-10-09 22:19:18 -04:00
|
|
|
#define HELP \
|
2023-10-09 21:29:56 -04:00
|
|
|
"target-file [arguments ...]\n" \
|
|
|
|
"Use the format `@EXEC cmd ...' within the target-file, this will execute the\n" \
|
2023-10-16 01:48:43 -04:00
|
|
|
"rest of line, or if found within the file, until the @STOP marker. You may use\n" \
|
2023-10-09 21:29:56 -04:00
|
|
|
"@COMPILECMD instead of @EXEC. Whitespace is required after and before both\n" \
|
|
|
|
"operators always.\n"
|
|
|
|
|
2023-10-09 20:10:14 -04:00
|
|
|
#define DESC \
|
|
|
|
"Options [Must always be first]\n" \
|
2023-10-09 22:19:18 -04:00
|
|
|
"\t-h --help, -n --dry-run\n" \
|
2023-10-09 20:10:14 -04:00
|
|
|
"Expansions\n" \
|
|
|
|
"\t$@ returns target-file (abc.x.txt)\n" \
|
|
|
|
"\t$* returns target-file without suffix (^-> abc.x)\n" \
|
2023-09-27 01:28:54 -04:00
|
|
|
"\t$+ returns arguments\n"
|
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
static char * g_filename;
|
2023-10-09 23:04:59 -04:00
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
/*** Utility functions ***/
|
2023-09-27 18:50:55 -04:00
|
|
|
|
2023-09-27 01:28:54 -04:00
|
|
|
static void
|
2023-10-16 02:31:34 -04:00
|
|
|
swap(char * a, char * b) {
|
2023-09-27 01:28:54 -04:00
|
|
|
*a ^= *b;
|
|
|
|
*b ^= *a;
|
|
|
|
*a ^= *b;
|
|
|
|
}
|
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
static char *
|
|
|
|
find(char * x, char * buf, char * end) {
|
|
|
|
size_t len = strlen(x);
|
|
|
|
for (; (buf < end) && len < (size_t)(end - buf); ++buf) {
|
|
|
|
if (!strncmp(buf, x, len))
|
|
|
|
{ return buf; }
|
|
|
|
}
|
|
|
|
return NULL;
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2023-10-16 02:31:34 -04:00
|
|
|
insert(char * new, char * str, size_t offset, size_t shift) {
|
2023-09-28 01:02:34 -04:00
|
|
|
size_t len, max;
|
|
|
|
len = strlen(new);
|
2023-09-28 22:27:55 -04:00
|
|
|
max = (strlen(str) + 1 - offset - shift);
|
|
|
|
memmove(str + offset + len, str + offset + shift, max);
|
2023-09-27 01:28:54 -04:00
|
|
|
memcpy(str + offset, new, len);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
/*** g_short, g_all Functions ***/
|
|
|
|
|
|
|
|
static char * g_short, * g_all;
|
|
|
|
|
2023-09-27 01:28:54 -04:00
|
|
|
static char *
|
2023-10-16 02:31:34 -04:00
|
|
|
shorten(char * fn) {
|
2023-09-28 01:02:34 -04:00
|
|
|
size_t i, last = 0, len;
|
|
|
|
char * sh;
|
|
|
|
len = strlen(fn);
|
|
|
|
sh = malloc(len + 1);
|
2023-10-16 02:31:34 -04:00
|
|
|
if (!sh) { return NULL; }
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (fn[i] == '.') { last = i; }
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|
|
|
|
last = last ? last : i;
|
2023-09-27 18:50:55 -04:00
|
|
|
strncpy(sh, fn, last);
|
|
|
|
sh[last] = '\0';
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2023-10-16 02:31:34 -04:00
|
|
|
all_args(size_t argc, char ** argv) {
|
2023-09-27 18:50:55 -04:00
|
|
|
char * all = NULL;
|
2023-10-16 02:31:34 -04:00
|
|
|
if (argc > 2) {
|
2023-10-09 20:10:14 -04:00
|
|
|
size_t i, len = argc;
|
2023-10-16 02:31:34 -04:00
|
|
|
for (i = 2; i < argc; ++i) { len += strlen(argv[i]); }
|
2023-09-27 18:50:55 -04:00
|
|
|
all = malloc(len + 1);
|
2023-10-16 02:31:34 -04:00
|
|
|
if (!all) { return NULL; }
|
2023-09-27 18:50:55 -04:00
|
|
|
all[len] = '\0';
|
2023-09-27 01:28:54 -04:00
|
|
|
len = 0;
|
2023-10-16 02:31:34 -04:00
|
|
|
for (i = 2; i < argc; ++i) {
|
2023-09-27 18:50:55 -04:00
|
|
|
strcpy(all + len, argv[i]);
|
2023-10-09 20:10:14 -04:00
|
|
|
len += strlen(argv[i]) + 1;
|
2023-10-16 02:31:34 -04:00
|
|
|
if (i + 1 < argc) { all[len - 1] = ' '; }
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|
|
|
|
}
|
2023-09-27 18:50:55 -04:00
|
|
|
return all;
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
/*** Map ***/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char * str;
|
|
|
|
size_t len;
|
|
|
|
} map_t;
|
|
|
|
|
|
|
|
static map_t
|
|
|
|
map(char * fn) {
|
|
|
|
struct stat s;
|
|
|
|
int fd;
|
|
|
|
map_t fs = {0};
|
|
|
|
fd = open(fn, O_RDONLY);
|
|
|
|
if (fd != -1) {
|
|
|
|
if (!fstat(fd,&s)
|
|
|
|
&& s.st_mode & S_IFREG
|
|
|
|
&& s.st_size) {
|
|
|
|
fs.len = s.st_size;
|
|
|
|
fs.str = (char *) mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** Important Functions ***/
|
|
|
|
|
|
|
|
static char *
|
|
|
|
find_region(map_t m) {
|
|
|
|
char * buf = NULL;
|
|
|
|
char * start, * stop;
|
|
|
|
start = find(START, m.str, m.str + m.len);
|
|
|
|
#ifdef OTHER_START
|
|
|
|
if (!start) {
|
|
|
|
start = find(OTHER_START, m.str, m.str + m.len);
|
|
|
|
start = (char *) /* DON'T QUESTION IT */
|
|
|
|
((ptrdiff_t) (start - strlen(START) + strlen(OTHER_START)) * (start != 0));
|
|
|
|
}
|
|
|
|
#endif /* OTHER_START */
|
|
|
|
if (start) {
|
|
|
|
start += strlen(START);
|
|
|
|
#ifdef REQUIRE_SPACE
|
|
|
|
if (!isspace(*start)) {
|
|
|
|
fprintf(stderr, "ERROR: Found start without suffix spacing.\n");
|
|
|
|
return buf;
|
|
|
|
}
|
2023-10-16 15:16:46 -04:00
|
|
|
#endif /* REQUIRE_SPACE */
|
2023-10-16 02:31:34 -04:00
|
|
|
stop = find(STOP, start, start + m.len - (start - m.str));
|
|
|
|
if (!stop) {
|
|
|
|
stop = start;
|
|
|
|
while (*stop
|
|
|
|
&& *stop != '\n') {
|
|
|
|
if (stop[0] == '\\'
|
|
|
|
&& stop[1] == '\n') { stop += 2; }
|
|
|
|
++stop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef REQUIRE_SPACE
|
2023-10-16 15:16:46 -04:00
|
|
|
else if (!isspace(*(stop - 1))) {
|
|
|
|
fprintf(stderr, "ERROR: Found stop without prefixing spacing.\n");
|
|
|
|
return buf;
|
2023-10-16 02:31:34 -04:00
|
|
|
}
|
2023-10-16 15:16:46 -04:00
|
|
|
#endif /* REQUIRE_SPACE */
|
|
|
|
if (stop)
|
|
|
|
{ buf = strndup(start, (stop - m.str) - (start - m.str)); }
|
2023-10-16 02:31:34 -04:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
*rootp += len + 1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-09-28 01:02:34 -04:00
|
|
|
static size_t
|
2023-10-16 02:31:34 -04:00
|
|
|
expand_size(char * buf, int argc, char ** argv) {
|
2023-09-28 22:27:55 -04:00
|
|
|
size_t i, len, max;
|
|
|
|
len = max = strlen(buf) + 1;
|
2023-10-16 02:31:34 -04:00
|
|
|
for (i = 0; i < len; ++i) {
|
2023-10-16 15:16:46 -04:00
|
|
|
if (buf[i] == '\\')
|
|
|
|
{ i += 2; continue; }
|
2023-10-16 02:31:34 -04:00
|
|
|
else if (buf[i] == '$') {
|
|
|
|
switch (buf[++i]) {
|
2023-09-28 01:02:34 -04:00
|
|
|
case '@':
|
|
|
|
max += strlen(g_filename);
|
|
|
|
break;
|
|
|
|
case '*':
|
2023-10-16 02:31:34 -04:00
|
|
|
if (!g_short) { g_short = shorten(g_filename); }
|
2023-09-29 03:05:40 -04:00
|
|
|
max += g_short ? strlen(g_short) : 0;
|
2023-09-28 01:02:34 -04:00
|
|
|
break;
|
|
|
|
case '+':
|
2023-10-16 02:31:34 -04:00
|
|
|
if (!g_all) { g_all = all_args((size_t) argc, argv); }
|
2023-09-28 01:02:34 -04:00
|
|
|
max += g_all ? strlen(g_all) : 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2023-09-27 19:31:21 -04:00
|
|
|
static char *
|
2023-10-16 02:31:34 -04:00
|
|
|
expand(char * buf) {
|
2023-09-28 01:02:34 -04:00
|
|
|
size_t i;
|
|
|
|
char * ptr = NULL;
|
2023-10-16 02:31:34 -04:00
|
|
|
for (i = 0; buf[i]; ++i) {
|
2023-10-16 15:16:46 -04:00
|
|
|
if (buf[i] == '\\')
|
|
|
|
{ i += 2; continue; }
|
2023-10-16 02:31:34 -04:00
|
|
|
else if (buf[i] == '$') {
|
|
|
|
switch (buf[++i]) {
|
2023-09-27 19:31:21 -04:00
|
|
|
case '@':
|
2023-09-28 01:02:34 -04:00
|
|
|
ptr = g_filename;
|
2023-09-27 19:31:21 -04:00
|
|
|
break;
|
|
|
|
case '*':
|
2023-09-28 01:02:34 -04:00
|
|
|
ptr = g_short;
|
2023-09-27 19:31:21 -04:00
|
|
|
break;
|
|
|
|
case '+':
|
2023-09-28 01:02:34 -04:00
|
|
|
ptr = g_all ? g_all : "";
|
2023-09-27 19:31:21 -04:00
|
|
|
break;
|
|
|
|
default: continue;
|
|
|
|
}
|
|
|
|
buf = insert(ptr, buf, i - 1, 2);
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 01:02:34 -04:00
|
|
|
free(g_short); free(g_all);
|
2023-09-27 19:31:21 -04:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2023-10-16 03:53:44 -04:00
|
|
|
/* Strips all prefixing and leading whitespace.
|
|
|
|
* Except if the last character beforehand is a newline. */
|
2023-09-28 14:32:53 -04:00
|
|
|
static size_t
|
2023-10-16 02:31:34 -04:00
|
|
|
strip(char * buf) {
|
2023-09-28 14:32:53 -04:00
|
|
|
size_t i = strlen(buf);
|
2023-10-16 02:31:34 -04:00
|
|
|
if (!i) { return 0; }
|
|
|
|
while (isspace(buf[i - 1])) { --i; }
|
2023-09-28 14:32:53 -04:00
|
|
|
buf[i] = '\0';
|
|
|
|
for (i = 0; isspace(buf[i]); ++i);
|
2023-10-11 07:58:58 -04:00
|
|
|
return i - (buf[i - 1] == '\n');
|
2023-09-28 14:32:53 -04:00
|
|
|
}
|
|
|
|
|
2023-09-28 02:14:17 -04:00
|
|
|
static int
|
2023-10-16 02:31:34 -04:00
|
|
|
run(char * buf) {
|
2023-09-28 02:14:17 -04:00
|
|
|
fputs("Output:\n", stderr);
|
|
|
|
return system(buf);
|
|
|
|
}
|
|
|
|
|
2023-09-27 01:28:54 -04:00
|
|
|
int
|
2023-10-16 02:31:34 -04:00
|
|
|
main(int argc, char ** argv) {
|
2023-09-28 02:14:17 -04:00
|
|
|
int ret = 0;
|
2023-10-16 02:31:34 -04:00
|
|
|
char * buf = NULL;
|
2023-10-09 23:37:26 -04:00
|
|
|
|
2023-10-16 01:48:43 -04:00
|
|
|
setlocale(LC_ALL, "C");
|
2023-10-06 22:17:40 -04:00
|
|
|
|
2023-09-28 02:14:17 -04:00
|
|
|
if (argc < 2
|
2023-10-09 22:19:18 -04:00
|
|
|
|| !strcmp(argv[1], "-h")
|
|
|
|
|| !strcmp(argv[1], "--help"))
|
2023-09-29 23:04:53 -04:00
|
|
|
{ goto help; }
|
2023-09-28 02:14:17 -04:00
|
|
|
|
|
|
|
g_filename = argv[1];
|
|
|
|
|
2023-10-09 22:19:18 -04:00
|
|
|
if (!strcmp(argv[1], "-n")
|
2023-10-16 02:31:34 -04:00
|
|
|
|| !strcmp(argv[1], "--dry-run")) {
|
|
|
|
if (argc > 2) { ret = 1; g_filename = argv[2]; }
|
|
|
|
else { goto help; }
|
2023-09-29 03:05:40 -04:00
|
|
|
}
|
2023-09-28 02:14:17 -04:00
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
{ map_t m = map(argv[1]);
|
|
|
|
if (m.str) {
|
|
|
|
buf = find_region(m);
|
|
|
|
munmap(m.str, m.len);
|
|
|
|
}
|
|
|
|
}
|
2023-10-09 23:04:59 -04:00
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
if (!buf) {
|
|
|
|
if (errno) { perror(argv[0]); }
|
|
|
|
else { fprintf(stderr, "%s: File unrecognized.\n", argv[0]); }
|
2023-09-29 03:05:40 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2023-09-28 02:14:17 -04:00
|
|
|
|
2023-10-16 01:48:43 -04:00
|
|
|
root(&g_filename);
|
2023-10-16 15:16:46 -04:00
|
|
|
{ char * buf2 = buf;
|
|
|
|
buf = realloc(buf, expand_size(buf, argc, argv));
|
|
|
|
if (!buf)
|
|
|
|
{ free(buf2); free(g_short); free(g_all); return 1; }
|
|
|
|
}
|
2023-09-29 03:05:40 -04:00
|
|
|
buf = expand(buf);
|
2023-10-11 07:58:58 -04:00
|
|
|
fprintf(stderr, "Exec: %s\n", buf + strip(buf));
|
2023-09-28 02:14:17 -04:00
|
|
|
if ((ret = ret ? 0 : run(buf)))
|
|
|
|
{ fprintf(stderr, "Result: %d\n", ret); }
|
|
|
|
|
2023-09-27 01:28:54 -04:00
|
|
|
free(buf);
|
|
|
|
return ret;
|
2023-09-29 23:04:53 -04:00
|
|
|
help:
|
|
|
|
fprintf(stderr, "%s: %s", argv[0], HELP DESC);
|
|
|
|
return 1;
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|