bake/baked.c

325 lines
7.4 KiB
C
Raw Normal View History

2023-09-28 02:20:48 -04:00
/* baked.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.
*
* EXEC:cc $@ -o $* -std=gnu89 -O2 -Wall -Wextra -Wpedantic -pipe $CFLAGS:STOP
* @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-09-27 01:28:54 -04:00
#include <stdarg.h>
#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
#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"
2023-09-29 03:05:40 -04:00
# define SLEN 5
2023-09-27 01:28:54 -04:00
#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"
2023-09-29 03:05:40 -04:00
# define SLEN 12
2023-09-27 01:28:54 -04:00
#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-28 01:02:34 -04:00
#define local_assert(expr, ret) do { assert(expr); if (!expr) { return ret; }} while (0)
static char * g_filename, * g_short, * g_all;
2023-09-27 18:50:55 -04:00
static char *
2023-09-29 03:05:40 -04:00
find_region(const char * fn)
2023-09-27 01:28:54 -04:00
{
2023-09-29 03:05:40 -04:00
struct stat s;
int fd;
2023-09-27 18:50:55 -04:00
char * buf = NULL;
2023-09-29 03:05:40 -04:00
fd = open(fn, O_RDONLY);
if ( fd != -1
&& !fstat(fd,&s)
&& s.st_mode & S_IFREG
&& s.st_size)
2023-09-27 01:28:54 -04:00
{
2023-09-29 03:05:40 -04:00
char * start, * stop, * addr;
/* hypothetically mmap can return -1 (MAP_FAILED) on failure,
this will not be due to permission, noexist, or ifreg.
Checks ensures that all these possibilities are impossible.
However, other issues may occur, such as a unexpected
change to permission after the fstat call had succeeded.
Then, this call may fail, Under such condition the for loop
will safely lead to a bail with a possible bad call of munmap. */
addr = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
for (start = addr; *start; ++start)
2023-09-28 01:02:34 -04:00
{
2023-09-29 03:05:40 -04:00
if (s.st_size - (start - addr) > SLEN)
{
if (!strncmp(start,START,SLEN))
{
start += strlen(START);
for (stop = start; *stop; ++stop)
{
if (s.st_size - (stop - addr) > SLEN)
{
if (!strncmp(stop,STOP,SLEN))
{
size_t len = (stop - addr) - (start - addr);
buf = malloc(len + 1);
assert(buf);
if (!buf)
{ goto stop; }
strncpy(buf, start, len);
buf[len] = '\0';
goto stop;
}
}
else goto stop;
}
goto stop;
}
}
else goto stop;
2023-09-28 01:02:34 -04:00
}
2023-09-29 03:05:40 -04:00
stop:
if (addr != MAP_FAILED)
{ munmap(addr, s.st_size); }
2023-09-27 01:28:54 -04:00
}
2023-09-27 18:50:55 -04:00
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)
{
int ret;
2023-09-28 01:02:34 -04:00
char x[1] = "\0";
2023-09-27 01:28:54 -04:00
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)
{
2023-09-28 01:02:34 -04:00
size_t len, max;
local_assert(new, str);
local_assert(str, NULL);
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;
}
static char *
2023-09-27 18:50:55 -04:00
shorten(char * fn)
2023-09-27 01:28:54 -04:00
{
2023-09-28 01:02:34 -04:00
size_t i, last = 0, len;
char * sh;
local_assert(fn, NULL);
len = strlen(fn);
sh = malloc(len + 1);
local_assert(sh, NULL);
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);
2023-09-29 03:05:40 -04:00
local_assert(all, NULL);
2023-09-27 18:50:55 -04:00
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
}
2023-09-28 01:02:34 -04:00
static size_t
2023-09-28 22:27:55 -04:00
expand_size(char * buf, int argc, char ** argv)
2023-09-28 01:02:34 -04:00
{
2023-09-28 22:27:55 -04:00
size_t i, len, max;
len = max = strlen(buf) + 1;
2023-09-28 01:02:34 -04:00
for (i = 0; i < len; ++i)
{
if (buf[i] == '\\')
{ i += 2; continue; }
else if (buf[i] == '$')
{
switch (buf[++i])
{
case '@':
max += strlen(g_filename);
break;
case '*':
if (!g_short)
2023-09-28 02:14:17 -04:00
{ 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 '+':
if (!g_all)
{ g_all = all_args((size_t) argc, argv); }
max += g_all ? strlen(g_all) : 0;
break;
}
}
}
return max;
}
static char *
2023-09-29 03:05:40 -04:00
expand(char * buf)
{
2023-09-28 01:02:34 -04:00
size_t i;
char * ptr = NULL;
2023-09-28 22:27:55 -04:00
for (i = 0; buf[i]; ++i)
{
if (buf[i] == '\\')
{ i += 2; continue; }
else if (buf[i] == '$')
{
switch (buf[++i])
{
case '@':
2023-09-29 03:05:40 -04:00
/* against the advice of -fanalyzer
*
* -- Supposed use of NULL is IMPOSSIBLE by standards definition:
*
* If the value of argc is greater than zero, the array members
* argv[0] through argv[argc-1] inclusive shall contain pointers
* to strings [...]
*
* -- Under the codition that argc is either not <2 and under
* the correct context, >2, the prorgam will not continue under
* either of these failure states and hence this is without
* doubt an impossibility.
* Unless an unaccounted for UB or a manual control flow error
* exists, for which may interfere with these conditions, this
* must certainly be a false-positive.
*/
2023-09-28 01:02:34 -04:00
ptr = g_filename;
break;
case '*':
2023-09-28 01:02:34 -04:00
ptr = g_short;
break;
case '+':
2023-09-28 01:02:34 -04:00
ptr = g_all ? g_all : "";
break;
default: continue;
}
buf = insert(ptr, buf, i - 1, 2);
}
}
2023-09-28 01:02:34 -04:00
free(g_short); free(g_all);
return buf;
}
2023-09-28 14:32:53 -04:00
static size_t
strip(char * buf)
{
size_t i = strlen(buf);
while (isspace(buf[i - 1]))
{ --i; }
buf[i] = '\0';
for (i = 0; isspace(buf[i]); ++i);
return i;
}
2023-09-28 02:14:17 -04:00
static int
run(const char * buf)
{
fputs("Output:\n", stderr);
root(g_filename);
return system(buf);
}
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;
2023-09-27 18:50:55 -04:00
char * buf;
2023-09-28 02:14:17 -04:00
if (argc < 2
|| !strcmp(argv[1], "-h"))
{ fprintf(stderr, "%s: %s", argv[0], HELP DESC); return 1; }
2023-09-28 02:14:17 -04:00
g_filename = argv[1];
if (!strcmp(argv[1], "-n"))
2023-09-29 03:05:40 -04:00
{
if (argc > 2)
{ ret = 1; g_filename = argv[2]; }
else
{ return 1; }
}
2023-09-28 02:14:17 -04:00
buf = find_region(g_filename);
if (!buf)
2023-09-29 03:05:40 -04:00
{
if (errno)
{ perror(argv[0]); }
else
{ fprintf(stderr, "%s: File unrecognized.\n", argv[0]); }
return 1;
}
2023-09-28 02:14:17 -04:00
2023-09-29 03:05:40 -04:00
buf = realloc(buf, expand_size(buf, argc, argv));
local_assert(buf, 1);
buf = expand(buf);
2023-09-28 14:32:53 -04:00
fprintf(stderr, "Exec: %s\n", buf + strip(buf) - (buf[0] == '\n'));
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;
}