changed START::STOP to @START STOP@, revised find to how it was formerly, removed SLEN (better compat with shake), handle spacing more effectively (shake compat++), fixed bug with $+ where more than 1 arg was concatted instead of being spaced apart

This commit is contained in:
Emil 2023-10-10 00:10:14 +00:00
parent 981a1931d4
commit a101d14f7c
No known key found for this signature in database
GPG Key ID: 5432DB986FDBCF8A

110
baked.c
View File

@ -3,7 +3,7 @@
* *
* Licensed under the GNU Public License version 3 only, see LICENSE. * Licensed under the GNU Public License version 3 only, see LICENSE.
* *
* EXEC:cc $@ -o $* -std=gnu89 -O2 -Wall -Wextra -Wpedantic -pipe $CFLAGS:STOP * @EXEC cc $@ -o $* -std=gnu89 -O2 -Wall -Wextra -Wpedantic -pipe $CFLAGS STOP@
* @COMPILECMD cc $@ -o $* -std=gnu89 -O2 -Wall -Wextra -Wpedantic -pipe $CFLAGS * @COMPILECMD cc $@ -o $* -std=gnu89 -O2 -Wall -Wextra -Wpedantic -pipe $CFLAGS
*/ */
@ -20,34 +20,49 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#ifndef SHAKE_COMPAT #define REQUIRE_SPACE
#define NEWLINE "\n"
#ifdef SHAKE_COMPAT
# define START "@COMPILECMD"
# define STOP "STOP@"
# define HELP \ # define HELP \
"target-file [arguments ...]\n" \ "target-file [arguments ...]\n" \
"Use the format `EXEC:command ...:STOP' within the target-file\n" "Use the format `@COMPILECMD command ...\n' (STOP@ suffix supported) within the target-file\n"
# define START "EXEC:"
# define STOP ":STOP"
# define SLEN 5
#else #else
/* Neo-format, as suggested by Anon, as it seems to be an unspoken standard
to prefix external special macros with @ (see code analyzing tools, doctools, etc.) */
# define START "@EXEC"
# define STOP "STOP@"
# define HELP \ # define HELP \
"target-file [arguments ...]\n" \ "target-file [arguments ...]\n" \
"Use the format `@COMPILECMD command ...\n' within the target-file\n" "Use the format `@EXEC command ... STOP@' within the target-file\n"
# define START "@COMPILECMD "
# define STOP "\n"
# define SLEN 12
#endif #endif
#define DESC \ #define DESC \
"Options [Must always be first]\n" \ "Options [Must always be first]\n" \
"\t-h, this message, -n dryrun\n" \ "\t-h, this message, -n dryrun\n" \
"In-file expansions\n" \ "Expansions\n" \
"\t$@ returns target-file\n" \ "\t$@ returns target-file (abc.x.txt)\n" \
"\t$* returns target-file without suffix\n" \ "\t$* returns target-file without suffix (^-> abc.x)\n" \
"\t$+ returns arguments\n" "\t$+ returns arguments\n"
#define local_assert(expr, ret) do { assert(expr); if (!expr) { return ret; }} while (0) #define local_assert(expr, ret) do { assert(expr); if (!expr) { return ret; }} while (0)
static char * g_filename, * g_short, * g_all; static char * g_filename, * g_short, * g_all;
static const char *
find(const char * x, const char * buf, const size_t max, const size_t min)
{
const char * start = buf;
for (; *buf; ++buf)
{
if (max - (buf - start) > min && !strncmp(buf, x, min))
{ return buf; }
}
return NULL;
}
static char * static char *
find_region(const char * fn) find_region(const char * fn)
{ {
@ -63,41 +78,40 @@ find_region(const char * fn)
&& s.st_mode & S_IFREG && s.st_mode & S_IFREG
&& s.st_size) && s.st_size)
{ {
char * start, * stop, * addr; const char * start, * stop;
char * addr;
addr = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0); addr = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (addr != MAP_FAILED) if (addr != MAP_FAILED)
{ {
for (start = addr; *start; ++start) start = find(START, addr, s.st_size, strlen(START));
{ if (start)
if (s.st_size - (start - addr) > SLEN)
{
if (!strncmp(start,START,SLEN))
{ {
start += strlen(START); start += strlen(START);
for (stop = start; *stop; ++stop) #ifdef REQUIRE_SPACE
if (!isspace(*start))
{ {
if (s.st_size - (stop - addr) > SLEN) fprintf(stderr, "ERROR: Found @START token without suffix spacing.\n");
return NULL;
}
#endif
stop = find(STOP, start, s.st_size - (start - addr), strlen(STOP));
/* NOTE this is assuming the last line of the a file is
terminated with a newline, per unix tradition. */
if (!stop)
{ stop = find(NEWLINE, start, s.st_size - (start - addr), 1); }
#ifdef REQUIRE_SPACE
else
{ {
if (!strncmp(stop,STOP,SLEN)) if (!isspace(*(stop - 1)))
{ {
size_t len = (stop - addr) - (start - addr); fprintf(stderr, "ERROR: Found STOP@ token without prefixing spacing.\n");
buf = malloc(len + 1); return NULL;
assert(buf);
if (!buf)
{ goto stop; }
strncpy(buf, start, len);
buf[len] = '\0';
goto stop;
} }
} }
else goto stop; #endif
if (stop)
{ buf = strndup(start, (stop - addr) - (start - addr)); }
} }
goto stop;
}
}
else goto stop;
}
stop:
munmap(addr, s.st_size); munmap(addr, s.st_size);
} }
} }
@ -169,7 +183,7 @@ all_args(size_t argc, char ** argv)
char * all = NULL; char * all = NULL;
if (argc > 2) if (argc > 2)
{ {
size_t i, len = 0; size_t i, len = argc;
for (i = 2; i < argc; ++i) for (i = 2; i < argc; ++i)
{ len += strlen(argv[i]); } { len += strlen(argv[i]); }
all = malloc(len + 1); all = malloc(len + 1);
@ -179,7 +193,9 @@ all_args(size_t argc, char ** argv)
for (i = 2; i < argc; ++i) for (i = 2; i < argc; ++i)
{ {
strcpy(all + len, argv[i]); strcpy(all + len, argv[i]);
len += strlen(argv[i]); len += strlen(argv[i]) + 1;
if (i + 1 < argc)
{ all[len - 1] = ' '; }
} }
} }
return all; return all;
@ -231,22 +247,6 @@ expand(char * buf)
switch (buf[++i]) switch (buf[++i])
{ {
case '@': case '@':
/* 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.
*/
ptr = g_filename; ptr = g_filename;
break; break;
case '*': case '*':