bake/baked.c

212 lines
4.3 KiB
C
Raw Normal View History

2023-09-27 18:50:55 -04:00
/* EXEC:cc $@ -o $* -std=gnu89 -O2 -Wall -Wextra -Wpedantic -pipe $CFLAGS:STOP */
2023-09-27 01:28:54 -04:00
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#ifndef SHAKE_COMPAT
2023-09-27 18:50:55 -04:00
# define HELP \
"target-file [arguments ...]\n" \
2023-09-27 01:28:54 -04:00
"Use the format `EXEC:command ...:STOP' within the target-file\n"
# define START "EXEC:"
# define STOP ":STOP"
#else
2023-09-27 18:50:55 -04:00
# define HELP \
"target-file [arguments ...]\n" \
2023-09-27 01:28:54 -04:00
"Use the format `@COMPILECMD command ...\n' within the target-file\n"
2023-09-27 01:46:28 -04:00
# define START "@COMPILECMD "
2023-09-27 01:28:54 -04:00
# define STOP "\n"
#endif
#define DESC \
2023-09-27 18:50:55 -04:00
"\t$@ returns target-file\n" \
"\t$* returns target-file without suffix\n" \
2023-09-27 01:28:54 -04:00
"\t$+ returns arguments\n"
2023-09-27 18:50:55 -04:00
static char *
find(char * buf, const char * token)
2023-09-27 01:28:54 -04:00
{
2023-09-27 18:50:55 -04:00
size_t len = strlen(token);
const char * stop = buf + strlen(buf);
do if (!strncmp(buf,token,len))
{ return buf + len; }
while (buf < stop && ++buf);
return NULL;
2023-09-27 01:28:54 -04:00
}
static char *
load(const char * fn)
{
2023-09-27 18:50:55 -04:00
char * buf = NULL;
2023-09-27 01:28:54 -04:00
FILE * fp = fopen(fn, "rb");
2023-09-27 18:50:55 -04:00
if (fp)
2023-09-27 01:28:54 -04:00
{
2023-09-27 18:50:55 -04:00
struct stat s;
2023-09-27 13:23:30 -04:00
size_t len;
2023-09-27 18:50:55 -04:00
if (! stat(fn,&s)
&& s.st_mode & S_IFREG
&& (len = s.st_size)
&& (buf = malloc(len + 1))
&& fread(buf, 1, len, fp) > strlen(STOP) + strlen(START))
{ buf[len] = '\0'; }
2023-09-27 01:28:54 -04:00
fclose(fp);
}
2023-09-27 18:50:55 -04:00
return buf;
}
static char *
find_region(const char * fn)
{
size_t len;
char * buf, * start, * stop;
if (!(buf = load(fn)))
{ fprintf(stderr, errno ? "cannot access '%s':" : "'%s': file too short", fn); return NULL; }
if (!(start = find(buf, START))
|| !(stop = find(start, STOP)))
{ fprintf(stderr, "No usable format located in '%s'", fn); return NULL; }
len = stop - start - strlen(STOP);
memmove(buf, start, len);
buf = realloc(buf, len + 1);
buf[len] = '\0';
return buf;
2023-09-27 01:28:54 -04:00
}
static void
swap(char * a, char * b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
static int
root(char * root)
{
char x[1] = "\0";
int ret;
size_t len = strlen(root);
while (len && root[len] != '/')
{ --len; }
if (!len)
{ return 0; }
swap(root + len, x);
ret = chdir(root);
swap(root + len, x);
return ret;
}
static char *
insert(const char * new, char * str, size_t offset, size_t shift)
{
size_t len = strlen(new);
size_t max = strlen(str) + 1;
2023-09-27 18:50:55 -04:00
/* str = realloc(str, max + len); */
2023-09-27 01:28:54 -04:00
memmove(str + offset + len, str + offset + shift, max - offset - shift);
memcpy(str + offset, new, len);
return str;
}
static char *
2023-09-27 18:50:55 -04:00
expand(char * buf, char ** str)
2023-09-27 01:28:54 -04:00
{
2023-09-27 18:50:55 -04:00
size_t i, len = strlen(buf);
int x;
buf = realloc(buf, 500);
2023-09-27 01:28:54 -04:00
for (i = 0; i < len; ++i)
{
if (buf[i] == '\\')
2023-09-27 18:50:55 -04:00
{ i += 2; continue; }
else if (buf[i] == '$')
2023-09-27 01:28:54 -04:00
{
switch (buf[++i])
{
2023-09-27 18:50:55 -04:00
case '@': x = 0; break;
case '*': x = 1; break;
case '+': x = 2; break;
2023-09-27 01:28:54 -04:00
default: continue;
}
2023-09-27 18:50:55 -04:00
buf = insert(str[x], buf, i - 1, 2);
2023-09-27 01:28:54 -04:00
len = strlen(buf);
}
}
return buf;
}
2023-09-27 18:50:55 -04:00
static char *
shorten(char * fn)
2023-09-27 01:28:54 -04:00
{
2023-09-27 18:50:55 -04:00
size_t i, last = 0, len = strlen(fn);
char * sh = malloc(len + 1);
2023-09-27 01:28:54 -04:00
for (i = 0; i < len; ++i)
{
2023-09-27 18:50:55 -04:00
if (fn[i] == '.')
2023-09-27 01:28:54 -04:00
{ last = i; }
}
last = last ? last : i;
2023-09-27 18:50:55 -04:00
strncpy(sh, fn, last);
sh[last] = '\0';
return sh;
}
static char *
all_args(size_t argc, char ** argv)
{
char * all = NULL;
2023-09-27 01:28:54 -04:00
if (argc > 2)
{
2023-09-27 18:50:55 -04:00
size_t i, len = 0;
for (i = 2; i < argc; ++i)
{ len += strlen(argv[i]); }
all = malloc(len + 1);
all[len] = '\0';
2023-09-27 01:28:54 -04:00
len = 0;
2023-09-27 18:50:55 -04:00
for (i = 2; i < argc; ++i)
2023-09-27 01:28:54 -04:00
{
2023-09-27 18:50:55 -04:00
strcpy(all + len, argv[i]);
2023-09-27 01:28:54 -04:00
len += strlen(argv[i]);
}
}
2023-09-27 18:50:55 -04:00
return all;
2023-09-27 01:28:54 -04:00
}
int
main(int argc, char ** argv)
{
2023-09-27 18:50:55 -04:00
int ret;
char * str[3];
char * buf;
2023-09-27 01:28:54 -04:00
if (argc < 2)
2023-09-27 18:50:55 -04:00
{
fprintf(stderr, "%s: %s", argv[0], HELP DESC);
return 1;
}
2023-09-27 01:28:54 -04:00
2023-09-27 18:50:55 -04:00
/* filename */
str[0] = argv[1];
2023-09-27 01:28:54 -04:00
2023-09-27 18:50:55 -04:00
/* sh */
str[1] = shorten(argv[1]);
2023-09-27 01:58:31 -04:00
2023-09-27 18:50:55 -04:00
/* all */
2023-09-27 01:58:31 -04:00
2023-09-27 18:50:55 -04:00
str[2] = all_args((size_t) argc, argv);
buf = find_region(argv[1]);
root(argv[1]);
buf = expand(buf, str);
2023-09-27 01:28:54 -04:00
fprintf(stderr, "Exec: %s\nOutput:\n", buf);
fprintf(stderr, "Result: %d\n", (ret = system(buf)));
2023-09-27 18:50:55 -04:00
free(str[2]);
free(str[1]);
2023-09-27 01:28:54 -04:00
free(buf);
return ret;
}