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.
|
|
|
|
*
|
2024-02-26 18:34:36 -05:00
|
|
|
* @BAKE cc -std=c99 -O2 $@ -o $* $+ # @STOP
|
2023-09-28 02:20:48 -04:00
|
|
|
*/
|
2023-09-27 01:28:54 -04:00
|
|
|
|
2023-11-13 00:37:03 -05:00
|
|
|
#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>
|
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>
|
2024-02-26 18:34:36 -05:00
|
|
|
#include <sys/wait.h>
|
2023-09-28 14:32:53 -04:00
|
|
|
#include <unistd.h>
|
2023-09-27 01:28:54 -04:00
|
|
|
|
2024-01-19 16:04:32 -05:00
|
|
|
#include "config.h"
|
2023-10-09 20:10:14 -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
|
|
|
|
|
|
|
#define DESC \
|
|
|
|
"Options [Must always be first]\n" \
|
|
|
|
"\t" DIM "-h --help" RESET", " BOLD "-n --dry-run\n" RESET \
|
|
|
|
"Expansions\n" \
|
|
|
|
"\t" YELLOW "$@" RESET " returns target-file (abc.x.txt)\n" \
|
|
|
|
"\t" YELLOW "$*" RESET " returns target-file without suffix (^-> abc.x)\n" \
|
2024-01-21 08:50:21 -05:00
|
|
|
"\t" YELLOW "$+" RESET " returns " GREEN "arguments" RESET "\n"
|
2023-09-27 01:28:54 -04:00
|
|
|
|
2024-02-24 01:52:54 -05:00
|
|
|
typedef struct {
|
|
|
|
size_t len;
|
|
|
|
char * buf;
|
|
|
|
} string_t;
|
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
typedef string_t map_t;
|
|
|
|
|
|
|
|
static string_t START = { 5, "@BAKE" };
|
|
|
|
static string_t STOP = { 5, "@STOP" };
|
2024-02-24 01:52:54 -05:00
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
/*** Utility functions ***/
|
2023-09-27 18:50:55 -04:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
#define strneq(a,b,n) (!strncmp(a,b,n))
|
|
|
|
#define streq(a,b) (!strcmp(a,b))
|
|
|
|
|
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 *
|
2024-02-24 01:52:54 -05:00
|
|
|
find(string_t x, char * buf, char * end) {
|
|
|
|
for (; (buf < end) && x.len < (size_t)(end - buf); ++buf) {
|
|
|
|
if (!strncmp(buf, x.buf, x.len))
|
2023-10-16 02:31:34 -04:00
|
|
|
{ return buf; }
|
|
|
|
}
|
|
|
|
return NULL;
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
static void
|
|
|
|
insert(char * str, size_t slen, char * new, size_t len, size_t shift) {
|
|
|
|
memmove(str + len, str + shift, slen - len - shift);
|
|
|
|
memcpy(str, new, len);
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
/*** g_short, g_all Functions ***/
|
|
|
|
|
2024-02-24 01:52:54 -05:00
|
|
|
#define GLOBALS_COUNT 3
|
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
static string_t globals[GLOBALS_COUNT];
|
2024-02-24 01:52:54 -05:00
|
|
|
|
|
|
|
#define g_filename globals[0]
|
|
|
|
#define g_short globals[1]
|
|
|
|
#define g_all globals[2]
|
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
static string_t
|
|
|
|
shorten(string_t s) {
|
|
|
|
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';
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|
2024-02-26 18:34:36 -05:00
|
|
|
return (string_t) { last, sh ? sh : calloc(0,0) };
|
2023-09-27 18:50:55 -04:00
|
|
|
}
|
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
static string_t
|
|
|
|
all_args(int argc, char ** argv) {
|
2023-09-27 18:50:55 -04:00
|
|
|
char * all = NULL;
|
2024-02-26 18:34:36 -05:00
|
|
|
size_t len = 0;
|
2023-10-16 02:31:34 -04:00
|
|
|
if (argc > 2) {
|
2024-02-26 18:34:36 -05:00
|
|
|
int i;
|
|
|
|
len = (size_t) argc;
|
2023-11-13 00:13:35 -05:00
|
|
|
|
|
|
|
for (i = 2; i < argc; ++i)
|
|
|
|
{ len += strlen(argv[i]); }
|
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
all = malloc(len);
|
|
|
|
if (all) {
|
|
|
|
all[len - 1] = '\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 (string_t) { len, all ? all : calloc(0,0) };
|
|
|
|
}
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
#if 0
|
|
|
|
static string_t
|
|
|
|
all_args(int argc, char ** argv) {
|
|
|
|
string_t s = (string_t) { 0, NULL };
|
|
|
|
if (argc > 2) {
|
|
|
|
size_t i;
|
|
|
|
for (i = (size_t) argc; i > 2; --i) {
|
|
|
|
s.len += strlen(argv[i]) + 1;
|
|
|
|
}
|
|
|
|
s.buf = malloc(s.len + (size_t) argc);
|
|
|
|
if (s.buf) {
|
|
|
|
size_t len;
|
|
|
|
s.buf[s.len - 1] = '\0';
|
|
|
|
for (len = 0, i = (size_t) argc; i > 2; --i) {
|
|
|
|
strcpy(s.buf + len, argv[i]);
|
|
|
|
len += strlen(argv[i]) + 1;
|
|
|
|
s.buf[len - 1] = ' ';
|
|
|
|
}
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|
|
|
|
}
|
2024-02-26 18:34:36 -05:00
|
|
|
return s;
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|
2024-02-26 18:34:36 -05:00
|
|
|
#endif
|
2023-09-27 01:28:54 -04:00
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
/*** Map ***/
|
|
|
|
|
|
|
|
static map_t
|
|
|
|
map(char * fn) {
|
|
|
|
struct stat s;
|
|
|
|
int fd;
|
2023-11-13 00:37:03 -05:00
|
|
|
map_t m = (map_t) {0};
|
2023-10-16 02:31:34 -04:00
|
|
|
fd = open(fn, O_RDONLY);
|
|
|
|
if (fd != -1) {
|
|
|
|
if (!fstat(fd,&s)
|
|
|
|
&& s.st_mode & S_IFREG
|
|
|
|
&& s.st_size) {
|
2023-11-13 00:37:03 -05:00
|
|
|
m.len = (size_t) s.st_size;
|
2024-02-26 18:34:36 -05:00
|
|
|
m.buf = (char *) mmap(NULL, m.len, PROT_READ, MAP_SHARED, fd, 0);
|
2023-10-16 02:31:34 -04:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2023-11-13 00:12:18 -05:00
|
|
|
return m;
|
2023-10-16 02:31:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*** Important Functions ***/
|
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
static string_t
|
|
|
|
find_region(map_t m, string_t startsym, string_t stopsym) {
|
2023-11-13 00:37:03 -05:00
|
|
|
extern char * strndup(const char * s, size_t n); /* for splint */
|
2023-11-13 00:13:35 -05:00
|
|
|
char * buf = NULL, * start, * stop;
|
2024-02-26 18:34:36 -05:00
|
|
|
size_t len;
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
start = find(startsym, m.buf, m.buf + m.len);
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
if (start) {
|
2024-02-26 18:34:36 -05:00
|
|
|
start += startsym.len;
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
#ifdef REQUIRE_SPACE
|
|
|
|
if (!isspace(*start)) {
|
2024-02-26 18:34:36 -05:00
|
|
|
fprintf(stderr, RED "%s" RESET ": Found start without suffix spacing.\n", g_filename.buf);
|
|
|
|
return (string_t) { 0 , buf };
|
2023-10-16 02:31:34 -04:00
|
|
|
}
|
2023-10-16 15:16:46 -04:00
|
|
|
#endif /* REQUIRE_SPACE */
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
stop = find(stopsym, start, start + m.len - (start - m.buf));
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
if (!stop) {
|
|
|
|
stop = start;
|
2023-11-13 00:13:35 -05:00
|
|
|
while (*stop && *stop != '\n') {
|
2023-10-16 02:31:34 -04:00
|
|
|
if (stop[0] == '\\'
|
2023-11-13 00:13:35 -05:00
|
|
|
&& stop[1] == '\n') { stop += 2; }
|
2023-10-16 02:31:34 -04:00
|
|
|
++stop;
|
|
|
|
}
|
|
|
|
}
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2023-10-16 15:16:46 -04:00
|
|
|
if (stop)
|
2024-02-24 01:52:54 -05:00
|
|
|
{
|
2024-02-26 18:34:36 -05:00
|
|
|
len = (size_t) (stop - m.buf) - (start - m.buf);
|
|
|
|
buf = strndup(start, len);
|
2024-02-24 01:52:54 -05:00
|
|
|
}
|
2023-10-16 02:31:34 -04:00
|
|
|
}
|
2024-02-26 18:34:36 -05:00
|
|
|
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;
|
2023-10-16 02:31:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2024-02-26 18:34:36 -05:00
|
|
|
root(string_t s) {
|
2023-11-13 00:37:03 -05:00
|
|
|
char x[1] = {'\0'};
|
2024-02-26 18:34:36 -05:00
|
|
|
char * root = s.buf;
|
|
|
|
size_t len = s.len;
|
2023-10-16 02:31:34 -04:00
|
|
|
int ret;
|
2023-11-13 00:13:35 -05:00
|
|
|
|
|
|
|
while (len && root[len] != '/')
|
2023-10-16 02:31:34 -04:00
|
|
|
{ --len; }
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
if (!len) { return 0; }
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2023-10-16 02:31:34 -04:00
|
|
|
swap(root + len, x);
|
|
|
|
ret = chdir(root);
|
|
|
|
swap(root + len, x);
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
/* *rootp += len + 1; */
|
2023-10-16 02:31:34 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:52:54 -05:00
|
|
|
#define ARRLEN(x) (sizeof(x) / sizeof(x[0]))
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
static string_t
|
|
|
|
expand(string_t s) {
|
2024-02-24 01:52:54 -05:00
|
|
|
enum {
|
|
|
|
MACRO_FILENAME = 0,
|
|
|
|
MACRO_SHORT = 1,
|
|
|
|
MACRO_ARGS = 2,
|
|
|
|
};
|
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
string_t macro[] = {
|
|
|
|
[MACRO_FILENAME] = { 2, "$@" },
|
|
|
|
[MACRO_SHORT ] = { 2, "$*" },
|
|
|
|
[MACRO_ARGS ] = { 2, "$+" },
|
2024-02-24 01:52:54 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t i, f;
|
|
|
|
|
|
|
|
{
|
2024-02-26 18:34:36 -05:00
|
|
|
size_t max = s.len;
|
|
|
|
for (i = 0; i < s.len; ++i) {
|
2024-02-24 01:52:54 -05:00
|
|
|
for (f = 0; f < ARRLEN(macro); ++f) {
|
2024-02-26 18:34:36 -05:00
|
|
|
if (!strncmp(s.buf + i, macro[f].buf, macro[f].len)) {
|
|
|
|
max += globals[f].len;
|
2024-02-24 01:52:54 -05:00
|
|
|
}
|
2023-09-28 01:02:34 -04:00
|
|
|
}
|
|
|
|
}
|
2024-02-26 18:34:36 -05:00
|
|
|
s.buf = realloc(s.buf, max + 27); /* I don't know, man */
|
|
|
|
if (!s.buf) { return (string_t) { 0, NULL}; }
|
|
|
|
memset(s.buf + s.len, 0, max - s.len);
|
|
|
|
s.len = max;
|
2023-09-28 01:02:34 -04:00
|
|
|
}
|
2024-02-26 18:34:36 -05:00
|
|
|
for (i = 0; i < s.len; ++i) {
|
2024-02-24 01:52:54 -05:00
|
|
|
for (f = 0; f < ARRLEN(macro); ++f) {
|
2024-02-26 18:34:36 -05:00
|
|
|
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);
|
2023-09-27 19:31:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-26 18:34:36 -05:00
|
|
|
return s;
|
2023-09-27 19:31:21 -04:00
|
|
|
}
|
|
|
|
|
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
|
2024-02-26 18:34:36 -05:00
|
|
|
strip(string_t s) {
|
|
|
|
size_t i = s.len;
|
2023-11-13 00:13:35 -05:00
|
|
|
|
|
|
|
if (!i)
|
|
|
|
{ return 0; }
|
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
while (isspace(s.buf[i]))
|
2023-11-13 00:13:35 -05:00
|
|
|
{ --i; }
|
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
s.buf[i] = '\0';
|
|
|
|
for (i = 0; isspace(s.buf[i]); ++i);
|
2023-11-13 00:13:35 -05:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
return i - (s.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) {
|
2024-02-26 18:34:36 -05:00
|
|
|
int ret = 127;
|
2024-01-19 15:57:41 -05:00
|
|
|
fputs(GREEN "output" RESET ":\n", stderr);
|
2024-02-26 18:34:36 -05:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (!pid) {
|
|
|
|
execl("/bin/sh", "-c", "'ls bake.c'", NULL);
|
|
|
|
} else {
|
|
|
|
int status;
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
if (!WIFEXITED(status)) { ret = 126; }
|
|
|
|
else { ret = WEXITSTATUS(status); }
|
|
|
|
}
|
|
|
|
return ret;
|
2023-09-28 02:14:17 -04:00
|
|
|
}
|
|
|
|
|
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;
|
2024-02-26 18:34:36 -05:00
|
|
|
string_t s = { 0, NULL };
|
2023-10-09 23:37:26 -04:00
|
|
|
|
2023-09-28 02:14:17 -04:00
|
|
|
if (argc < 2
|
2024-02-26 18:34:36 -05:00
|
|
|
|| streq(argv[1], "-h")
|
|
|
|
|| streq(argv[1], "--help"))
|
2023-09-29 23:04:53 -04:00
|
|
|
{ goto help; }
|
2023-09-28 02:14:17 -04:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
g_filename = (string_t) { strlen(argv[1]), argv[1] };
|
2023-09-28 02:14:17 -04:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
if (streq(argv[1], "-n")
|
|
|
|
|| streq(argv[1], "--dry-run")) {
|
|
|
|
if (argc > 2) {
|
|
|
|
ret = 1;
|
|
|
|
g_filename = (string_t) { strlen(argv[2]), argv[2] };
|
|
|
|
}
|
2023-10-16 02:31:34 -04:00
|
|
|
else { goto help; }
|
2023-09-29 03:05:40 -04:00
|
|
|
}
|
2023-09-28 02:14:17 -04:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
s = file_find_region(g_filename.buf, START, STOP);
|
2023-10-09 23:04:59 -04:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
if (!s.buf) {
|
2024-02-24 01:52:54 -05:00
|
|
|
if (errno)
|
2024-02-26 18:34:36 -05:00
|
|
|
{ fprintf(stderr, BOLD RED "%s" RESET ": %s\n", g_filename.buf, strerror(errno)); }
|
2024-02-24 01:52:54 -05:00
|
|
|
else
|
2024-02-26 18:34:36 -05:00
|
|
|
{ fprintf(stderr, BOLD RED "%s" RESET ": File unrecognized.\n", g_filename.buf); }
|
2023-09-29 03:05:40 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2023-09-28 02:14:17 -04:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
g_all = all_args(argc, argv);
|
2024-02-24 01:52:54 -05:00
|
|
|
g_short = shorten(g_filename);
|
2024-02-26 18:34:36 -05:00
|
|
|
root(g_filename);
|
|
|
|
|
|
|
|
s = expand(s);
|
|
|
|
free(g_short.buf); free(g_all.buf);
|
|
|
|
if (!s.buf) { return 1; }
|
2023-11-13 00:12:18 -05:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
fprintf(stderr, GREEN "%s" RESET ": %s\n", argv[0], s.buf + strip(s));
|
|
|
|
ret = ret ? 0 : run(s.buf);
|
2023-11-13 00:12:18 -05:00
|
|
|
if (ret)
|
2024-02-26 18:34:36 -05:00
|
|
|
{ fprintf(stderr, RED "result" RESET ": " BOLD "%d, %s\n" RESET, errno, strerror(errno)); }
|
2023-09-28 02:14:17 -04:00
|
|
|
|
2024-02-26 18:34:36 -05:00
|
|
|
free(s.buf);
|
2023-09-27 01:28:54 -04:00
|
|
|
return ret;
|
2023-09-29 23:04:53 -04:00
|
|
|
help:
|
2024-01-19 15:57:41 -05:00
|
|
|
fprintf(stderr, YELLOW "%s" RESET ": %s", argv[0], HELP DESC);
|
2023-09-29 23:04:53 -04:00
|
|
|
return 1;
|
2023-09-27 01:28:54 -04:00
|
|
|
}
|