intermediary 2

This commit is contained in:
Chad C. Starz 2024-09-26 22:14:56 +00:00
parent f493890ec8
commit dc5b390587
No known key found for this signature in database
GPG Key ID: CEEBC9208C287297

145
cbake.l
View File

@ -1,87 +1,48 @@
/* cbake.l @BAKE flex @FILE && cc -std=c99 -D_GNU_SOURCE -o @{@SHORT} lex.yy.c @ARGS -lfl @STOP */ /* cbake.l @BAKE flex @FILE && cc -std=c99 -D_GNU_SOURCE -o @{@SHORT} lex.yy.c @ARGS -lfl @STOP */
%{ %{
#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;
char * filename; char * filename;
int gac; int gac; char ** gav;
char ** gav; int gmax = 0, line = 1, expunge_depth = 0;
/* Options */
int gselect = 1;
/* accum */
int line = 1, expunge_depth = 0, first_nl, nth = 0;
extern void root(char * filename);
extern void args(int n); extern void args(int n);
extern void shorten(char * filename, int n); extern void shorten(char * filename, int n);
%} %}
%x FOUND PADDING
SPACE [ \t\r\v\f]
NUM [[:digit:]]
%x FOUND STOP PADDING
MACROS (@BAKE|@FILENAME|@FILE|@SHORT|@ARGS|@LINE|@STOP|$@|$*|$+)
%option nodefault noinput noyywrap %option nodefault noinput noyywrap
%% %%
<FOUND>{ <FOUND>{
/* New behavior */ @FILENAME|@FILE|$@ { printf(filename); }
@BAKE { if (!gselect) { return 0; } yyless(yyleng - strlen("@BAKE")); BEGIN INITIAL; } @SHORT:[[:digit:]]+ { shorten(filename, atoi(strrchr(yytext, ':')+1)); }
@FILENAME|@FILE|$@ { STRING(filename); }
@SHORT:{NUM}+ { shorten(filename, atoi(strrchr(yytext, ':')+1)); }
@SHORT|$\* { shorten(filename, 1); } @SHORT|$\* { shorten(filename, 1); }
@ARGS:{NUM}+ { args(atoi(strrchr(yytext, ':')+1)); } @ARGS:[[:digit:]]+ { args(atoi(strrchr(yytext, ':')+1)); }
@ARGS|$\+ { args(-1); } @ARGS|$\+ { args(-1); }
@LINE { FORMAT("%d", line); } @LINE { printf("%d", line); }
@STOP { CHAR('\n'); if (!gselect) { return 0; } BEGIN INITIAL; } @BAKE { yyless(yyleng - strlen("@BAKE")); BEGIN INITIAL; }
\\\n { ++line; CHAR(' '); BEGIN PADDING; } @STOP { printf("@STOP\n"); BEGIN INITIAL; }
\\{MACROS} { putchar(yytext[1]); } \\[@$] { putchar(yytext[1]); }
\\\n { ++line; }
@\{ { ++expunge_depth; } @\{ { ++expunge_depth; }
\} { if (!expunge_depth--) { ECHO; } } \} { if (!expunge_depth--) { ECHO; } }
\n { ++line; if (first_nl) { first_nl = 0; BEGIN STOP; } } \n { ++line; putchar(yytext[0]); }
{SPACE} { CHAR(' '); BEGIN PADDING; } [ \v\t\r\f] { putchar(' '); BEGIN PADDING; }
. { ECHO; } . { putchar(yytext[0]); }
} }
<STOP>{
@BAKE{SPACE} {
first_nl = 1;
if (gselect < 0) { printf("\n%s:%d s%d: ", filename, line, ++nth); }
BEGIN FOUND;
}
@STOP { yyless(-1); BEGIN FOUND; }
\n { yymore(); ++line; }
.|\\@ { yymore(); }
}
<PADDING>{ <PADDING>{
{SPACE} { ; } [ \v\t\r\f] { ; }
.|\n { yyless(0); BEGIN FOUND; } .|\n { yyless(0); BEGIN FOUND; }
} }
\\[^\n] { ; }
@BAKE[[:space:]] { \\\n { ++line; }
first_nl = 1; @BAKE[ \t\r\v\f] { goto bake; }
if (gselect < 0) { printf("%s:%d s%d: ", filename, line, ++nth); BEGIN FOUND; } @BAKE\n { ++line; bake: ++gmax; printf("@BAKE "); BEGIN FOUND; }
else if (gselect == 0) { BEGIN FOUND; }
else { --gselect; }
}
\n { ++line; } \n { ++line; }
. { ; } . { ; }
%% %%
#include <ctype.h>
void help(void) { puts("See bake(1) - \"Buy high. Sell low.\"\n"); }
void root(char * filename) { void root(char * filename) {
char * path, * terminator; char * path, * terminator;
if (!(path = realpath(filename, NULL))) { return; } if (!(path = realpath(filename, NULL))) { return; }
@ -93,26 +54,21 @@ void root(char * filename) {
} }
void args(int n) { void args(int n) {
if (n < 0) { for (int i = 0; i < gac; ++i) { STRING(gav[i]); if (i + 1 < gac) { CHAR(' '); } } } if (n < 0) { for (int i = 0; i < gac; ++i) { printf(gav[i]); if (i + 1 < gac) { putchar(' '); } } }
else if (n < gac) { STRING(gav[n]); } else if (n < gac) { printf(gav[n]); }
} }
void shorten(char * filename, int n) { void shorten(char * filename, int n) {
char * end = filename + strlen(filename); char * end = filename + strlen(filename);
while (n && (end = memrchr(filename, '.', end - filename))) { --n; } while (n && (end = memrchr(filename, '.', end - filename))) { --n; }
if (!end) { fprintf(stderr, "<SHORTEN> context error: Argument out of range.\n"); STRING("idiot"); return; } if (!end) { fprintf(stderr, "<SHORTEN> context error: Argument out of range.\n"); printf("idiot"); return; }
FWRITE(filename, end - filename); fwrite(filename, 1, end - filename, stdout);
} }
#define HELP \
"bake(1) - \"Buy high. Sell low.\"\n"
void help(void) { puts(HELP); }
int main (int ac, char ** av) { int main (int ac, char ** av) {
int run = 1; int run = 1, select = 1, list = 0;
char * av0 = av[0]; char * av0 = av[0];
FILE * fp, * op; FILE * fp;
while (++av, --ac) { while (++av, --ac) {
int i; int i;
if (av[0][0] != '-') { goto start; } if (av[0][0] != '-') { goto start; }
@ -128,47 +84,44 @@ int main (int ac, char ** av) {
for (i = 1; i < strlen(av[0]); ++i) { for (i = 1; i < strlen(av[0]); ++i) {
switch (av[0][i]) { switch (av[0][i]) {
opt_dry_run: case 'n': run = 0; break; opt_dry_run: case 'n': run = 0; break;
opt_select: case 's': gselect = atoi(av[0]+2+(av[0][2] == '=')); i = strlen(av[0]); break; opt_select: case 's': select = atoi(av[0]+2+(av[0][2] == '=')); i = strlen(av[0]); break;
opt_list: case 'l': run = 0; gselect = -1; break; opt_list: case 'l': run = 0; select = -1; break;
opt_help: case 'h': help(); return 0; opt_help: case 'h': help(); return 0;
opt_default: default: fprintf(stderr, "%s: Unknown option '%s'\n", av0, av[0]); return 1; 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; opt_arg: fprintf(stderr, "%s: Argument missing for '%s'\n", av0, av[0]); return 1;
} }
} }
} }
start: start:
if (!ac) { fprintf(stderr, "%s: Missing filename\n", av0); return 1; } if (!ac) { fprintf(stderr, "%s: Missing filename\n", av0); return 1; }
if (gselect == 0) { fprintf(stderr, "%s: Out of range\n", av0); return 1; }
/* filename and self placement */ /* filename and renaming */
filename = av[0]; filename = av[0];
root(filename); root(filename);
{ {
char * tmp = strrchr(filename, '/'); char * tmp = strrchr(filename, '/');
if (tmp) { filename = tmp+1; } if (tmp) { filename = tmp+1; }
} }
/* open and prepare ac, av */ /* open and prepare ac, av */
if (!(yyin = fp = fopen(filename, "rb"))) if (!(yyin = fp = fopen(filename, "rb")))
{ fprintf(stderr, "%s: '%s' %s\n", av0, filename, strerror(errno)); return 1; } { fprintf(stderr, "%s: '%s' %s\n", av0, filename, strerror(errno)); return 1; }
gac = --ac, gav = ++av; gac = --ac, gav = ++av;
{
if (run) { FILE * bp = fmemopen(buf, 1 << 16, "wb+"), * stdout_old = stdout;
gpipe = popen("/bin/sh -e", "w"); stdout = bp;
if (!gpipe) { fprintf(stderr, "%s: <gpipe> %s\n", av0, strerror(errno)); return 1; }
}
if (gselect > 0) { fprintf(stderr, "%s: ", av0); fflush(stderr); }
yylex(); yylex();
/* putchar('\n'); */ stdout = stdout_old;
fclose(fp); fclose(bp);
if (gselect > 0) { fprintf(stderr, "%s: Out of range\n", av0); } }
if (select > gmax) { fprintf(stderr, "%s: Out of range\n", av0); return 1; }
if (!run) { return 0; } if (!list) {
fprintf(stderr, "output: "); fflush(stderr); fprintf(stderr, "%s: ", av0); fflush(stderr);
run = pclose(gpipe); fwrite(buf,1,1000,stdout);
/* repurposed run */ if (run) {
if (run) { printf("%s: Exit code %d\n", av0, run); } FILE * gpipe = popen("/bin/sh -e", "w");
return run; if (!gpipe) { fprintf(stderr, "%s: <gpipe> %s\n", av0, strerror(errno)); return 1; }
run = pclose(gpipe); if (run) { printf("%s: Exit code %d\n", av0, run); } return run;
}
}
} }