bake/cbake.l

153 lines
5.3 KiB
Plaintext
Raw Normal View History

2024-09-26 18:16:01 -04:00
/* cbake.l @BAKE flex @FILE && cc -Wall -Wextra -std=c99 -D_GNU_SOURCE -o @{@SHORT} lex.yy.c @ARGS -lfl @STOP */
/* TODO: implement expunge */
2024-09-22 17:21:10 -04:00
%{
2024-09-26 18:16:01 -04:00
#include <ctype.h>
#undef ECHO
#define ECHO do { fprintf(stdout, yytext); if (gpipe) { fprintf(gpipe, yytext); } } while (0)
#define CHAR(c) do { fputc(c, stdout); if (gpipe) { fputc(c, gpipe); } } while (0)
#define STRING(s) do { fputs(s, stdout); if (gpipe) { fputs(s, gpipe); } } while (0)
#define FORMAT(...) do { fprintf(stdout, __VA_ARGS__); if (gpipe) { fprintf(gpipe, __VA_ARGS__); } } while (0)
#define FWRITE(str, len) do { fwrite(str, 1, len, stdout); if (gpipe) { fwrite(str, 1, len, gpipe); } } while (0)
FILE * gpipe;
2024-09-22 17:21:10 -04:00
char * filename;
2024-09-26 18:16:01 -04:00
int gac;
char ** gav;
int gselect = 1, line = 1, first_nl, expunge_depth = 0;
int nth = 0, tmpline;
2024-09-22 17:21:10 -04:00
2024-09-26 18:16:01 -04:00
extern void root(char * filename);
2024-09-22 17:21:10 -04:00
extern void args(int n);
extern void shorten(char * filename, int n);
%}
2024-09-26 18:16:01 -04:00
SPACE [ \t\r\v\f]
MACROS (@BAKE|@FILENAME|@FILE|@SHORT|@ARGS|@LINE|@STOP|$@|$*|$+)
%x FOUND PADDING STOP
%option nodefault noinput nounput noyywrap
2024-09-22 17:21:10 -04:00
%%
2024-09-26 18:16:01 -04:00
@BAKE[[:space:]] { bake:
first_nl = 1;
if (yytext[yyleng-1] == '\n') { ++line; }
if (gselect < 0) { printf("\n%s:%d:s%d: ", filename, line, ++nth); BEGIN FOUND; }
else if (!--gselect) { BEGIN FOUND; }
}
\n { ++line; }
. {;}
2024-09-22 17:21:10 -04:00
<FOUND>{
2024-09-26 18:16:01 -04:00
@BAKE|@STOP { BEGIN INITIAL; yyless(0); if (first_nl) { putchar('\n'); } if (!gselect) { return 0; } }
@FILENAME|@FILE|$@ { STRING(filename); }
2024-09-26 18:14:56 -04:00
@SHORT:[[:digit:]]+ { shorten(filename, atoi(strrchr(yytext, ':')+1)); }
@SHORT|$\* { shorten(filename, 1); }
@ARGS:[[:digit:]]+ { args(atoi(strrchr(yytext, ':')+1)); }
@ARGS|$\+ { args(-1); }
2024-09-26 18:16:01 -04:00
@LINE { FORMAT("%d", line); }
2024-09-26 18:14:56 -04:00
@\{ { ++expunge_depth; }
\} { if (!expunge_depth--) { ECHO; } }
2024-09-26 18:16:01 -04:00
\\\n { ++line; CHAR(' '); BEGIN PADDING; }
\\{MACROS} { STRING(yytext + 1); }
\n { putchar('\n'); ++line; if (first_nl) { first_nl = 0; tmpline = 0; BEGIN STOP; } }
{SPACE} { BEGIN PADDING; CHAR(' '); }
. { ECHO; }
2024-09-22 17:21:10 -04:00
}
2024-09-26 18:14:37 -04:00
<PADDING>{
2024-09-26 18:16:01 -04:00
{SPACE} { ; }
.|\n { yyless(0); BEGIN FOUND; }
}
<STOP>{
@BAKE[[:space:]] { line += tmpline; goto bake; }
@STOP { yyless(0); BEGIN FOUND; }
\n { ++tmpline; yymore(); }
.|\\@ { yymore(); }
2024-09-26 18:14:37 -04:00
}
2024-09-26 18:14:56 -04:00
%%
2024-09-26 18:14:37 -04:00
2024-09-22 17:21:10 -04:00
void root(char * filename) {
char * path, * terminator;
if (!(path = realpath(filename, NULL))) { return; }
if (terminator = strrchr(path, '/')) {
*terminator = '\0';
chroot(path);
}
free(path);
}
void args(int n) {
2024-09-26 18:16:01 -04:00
if (n < 0) { for (int i = 0; i < gac; ++i) { STRING(gav[i]); if (i + 1 < gac) { CHAR(' '); } } }
else if (n < gac) { STRING(gav[n]); }
2024-09-22 17:21:10 -04:00
}
void shorten(char * filename, int n) {
char * end = filename + strlen(filename);
while (n && (end = memrchr(filename, '.', end - filename))) { --n; }
2024-09-26 18:16:01 -04:00
if (!end) { fprintf(stderr, "<SHORTEN> context error: Argument out of range.\n"); STRING("idiot"); return; }
FWRITE(filename, end - filename);
2024-09-22 17:21:10 -04:00
}
2024-09-26 18:16:01 -04:00
void help(void) { puts("bake(1) - \"Buy high. Sell low.\"\n"); }
2024-09-22 17:21:10 -04:00
int main (int ac, char ** av) {
2024-09-26 18:16:01 -04:00
int run = 1;
2024-09-22 17:21:10 -04:00
char * av0 = av[0];
2024-09-26 18:14:56 -04:00
FILE * fp;
2024-09-26 18:16:01 -04:00
2024-09-22 17:21:10 -04:00
while (++av, --ac) {
2024-09-26 18:16:01 -04:00
size_t i;
2024-09-22 17:21:10 -04:00
if (av[0][0] != '-') { goto start; }
if (av[0][1] == '-') {
if (av[0][2] == '\0') { ++av, --ac; goto start; }
if (!strcmp(av[0]+2, "dry-run")) { i = strlen(av[0]); goto opt_dry_run; }
if (!strcmp(av[0]+2, "select" )) { if (!ac-1 || isdigit(av[1][0])) { goto opt_arg; } ++av, --ac;
i = strlen(av[0]); goto opt_select; }
if (!strcmp(av[0]+2, "list" )) { i = strlen(av[0]); goto opt_list; }
if (!strcmp(av[0]+2, "help" )) { goto opt_help; }
goto opt_default;
}
for (i = 1; i < strlen(av[0]); ++i) {
switch (av[0][i]) {
opt_dry_run: case 'n': run = 0; break;
2024-09-26 18:16:01 -04:00
opt_select: case 's': gselect = atoi(av[0]+i+1+(av[0][i+1] == '=')); i = strlen(av[0]); break;
opt_list: case 'l': run = 0; gselect = -1; break;
2024-09-22 17:21:10 -04:00
opt_help: case 'h': help(); return 0;
opt_default: default: fprintf(stderr, "%s: Unknown option '%s'\n", av0, av[0]); return 1;
opt_arg: fprintf(stderr, "%s: Argument missing for '%s'\n", av0, av[0]); return 1;
}
}
}
2024-09-26 18:16:01 -04:00
start:
2024-09-22 17:21:10 -04:00
if (!ac) { fprintf(stderr, "%s: Missing filename\n", av0); return 1; }
2024-09-26 18:16:01 -04:00
if (!gselect) { fprintf(stderr, "%s: Out of range\n", av0); return 1; }
2024-09-22 17:21:10 -04:00
2024-09-26 18:16:01 -04:00
/* filename and self placement */
2024-09-22 17:21:10 -04:00
filename = av[0];
root(filename);
{
char * tmp = strrchr(filename, '/');
if (tmp) { filename = tmp+1; }
}
2024-09-26 18:16:01 -04:00
2024-09-22 17:21:10 -04:00
/* open and prepare ac, av */
if (!(yyin = fp = fopen(filename, "rb")))
{ fprintf(stderr, "%s: '%s' %s\n", av0, filename, strerror(errno)); return 1; }
gac = --ac, gav = ++av;
2024-09-26 18:16:01 -04:00
if (run) {
gpipe = popen("/bin/sh -e", "w");
if (!gpipe) { fprintf(stderr, "%s: <gpipe> %s\n", av0, strerror(errno)); return 1; }
2024-09-22 17:21:10 -04:00
}
2024-09-26 18:14:37 -04:00
2024-09-26 18:16:01 -04:00
if (gselect > 0) { fprintf(stderr, "%s: ", av0); fflush(stderr); }
yylex(); fflush(stdout);
fclose(fp);
if (gselect > 0) { fprintf(stderr, "%s: Out of range\n", av0); }
if (!run) { return 0; }
fprintf(stderr, "output: "); fflush(stderr);
run = pclose(gpipe); /* repurposed run */
if (run) { printf("%s: Exit code %d\n", av0, run); }
return run;
2024-09-22 17:21:10 -04:00
}