intermediary 1

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

60
cbake.l
View File

@ -3,7 +3,6 @@
#include <ctype.h> #include <ctype.h>
#define backspace(fs) fputs("\x08", fs)
#undef ECHO #undef ECHO
#define ECHO do { fprintf(stdout, yytext); if (gpipe) { fprintf(gpipe, yytext); } } while (0) #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 CHAR(c) do { fputc(c, stdout); if (gpipe) { fputc(c, gpipe); } } while (0)
@ -26,45 +25,35 @@ extern void args(int n);
extern void shorten(char * filename, int n); extern void shorten(char * filename, int n);
%} %}
SPACE [[:space:]] SPACE [ \t\r\v\f]
NUM [[:digit:]] NUM [[:digit:]]
%x FOUND STOP %x FOUND STOP PADDING
MACROS (@BAKE|@FILENAME|@FILE|@SHORT|@ARGS|@LINE|@STOP|$@|$*|$+)
%option nodefault noinput noyywrap %option nodefault noinput noyywrap
%% %%
@BAKE{SPACE} {
first_nl = 1;
if (gselect < 0) {
printf("%s:%d s%d: ", filename, line, ++nth);
BEGIN FOUND;
}
if (!--gselect) { BEGIN FOUND; }
}
\n { ++line; }
. { ; }
<FOUND>{ <FOUND>{
/* New behavior */
@BAKE { if (!gselect) { return 0; } yyless(yyleng - strlen("@BAKE")); BEGIN INITIAL; }
@FILENAME|@FILE|$@ { STRING(filename); } @FILENAME|@FILE|$@ { STRING(filename); }
@SHORT:{NUM}+ { shorten(filename, atoi(strrchr(yytext, ':')+1)); } @SHORT:{NUM}+ { shorten(filename, atoi(strrchr(yytext, ':')+1)); }
@SHORT|$\* { shorten(filename, 1); } @SHORT|$\* { shorten(filename, 1); }
@ARGS:{NUM}+ { args(atoi(strrchr(yytext, ':')+1)); } @ARGS:{NUM}+ { args(atoi(strrchr(yytext, ':')+1)); }
@ARGS|$\+ { args(-1); } @ARGS|$\+ { args(-1); }
@LINE { FORMAT("%d", line); } @LINE { FORMAT("%d", line); }
@STOP { CHAR('\n'); if (gpipe) { fprintf(stderr, "output: "); } if (!gselect) { return 0; } BEGIN INITIAL; } @STOP { CHAR('\n'); if (!gselect) { return 0; } BEGIN INITIAL; }
\\\n { CHAR('\n'); } \\\n { ++line; CHAR(' '); BEGIN PADDING; }
\\[@$] { ; } \\{MACROS} { putchar(yytext[1]); }
@\{ { ++expunge_depth; } @\{ { ++expunge_depth; }
\} { if (!expunge_depth--) { ECHO; } } \} { if (!expunge_depth--) { ECHO; } }
\n { ++line; if (first_nl) { first_nl = 0; BEGIN STOP; } } \n { ++line; if (first_nl) { first_nl = 0; BEGIN STOP; } }
{SPACE}+ { CHAR(' '); } {SPACE} { CHAR(' '); BEGIN PADDING; }
. { ECHO; } . { ECHO; }
} }
/* FIXME +1. fucked up line count when multiline
-2. dropping everything after @BAKE...\n */
<STOP>{ <STOP>{
@BAKE{SPACE} { @BAKE{SPACE} {
first_nl = 1; first_nl = 1;
@ -75,6 +64,22 @@ NUM [[:digit:]]
\n { yymore(); ++line; } \n { yymore(); ++line; }
.|\\@ { yymore(); } .|\\@ { yymore(); }
} }
<PADDING>{
{SPACE} { ; }
.|\n { yyless(0); BEGIN FOUND; }
}
@BAKE[[:space:]] {
first_nl = 1;
if (gselect < 0) { printf("%s:%d s%d: ", filename, line, ++nth); BEGIN FOUND; }
else if (gselect == 0) { BEGIN FOUND; }
else { --gselect; }
}
\n { ++line; }
. { ; }
%% %%
void root(char * filename) { void root(char * filename) {
@ -134,6 +139,7 @@ int main (int ac, char ** av) {
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 self placement */
filename = av[0]; filename = av[0];
@ -152,11 +158,17 @@ int main (int ac, char ** av) {
gpipe = popen("/bin/sh -e", "w"); gpipe = popen("/bin/sh -e", "w");
if (!gpipe) { fprintf(stderr, "%s: <gpipe> %s\n", av0, strerror(errno)); return 1; } if (!gpipe) { fprintf(stderr, "%s: <gpipe> %s\n", av0, strerror(errno)); return 1; }
} }
if (gselect > 0) { fprintf(stderr, "%s: ", av0); fflush(stderr); } if (gselect > 0) { fprintf(stderr, "%s: ", av0); fflush(stderr); }
yylex(); yylex();
if (gselect > 0) { fprintf(stderr, "%s: Out of range\n", av0); } /* putchar('\n'); */
fclose(fp); fclose(fp);
if (gselect > 0) { fprintf(stderr, "%s: Out of range\n", av0); }
if (run) { run = pclose(gpipe); if (run) { printf("%s: Exit code %d\n", av0, run); } return run; } if (!run) { return 0; }
return 0; fprintf(stderr, "output: "); fflush(stderr);
run = pclose(gpipe);
/* repurposed run */
if (run) { printf("%s: Exit code %d\n", av0, run); }
return run;
} }