diff options
author | John Ankarström <john@ankarstrom.se> | 2021-09-17 20:57:05 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-09-17 20:57:05 +0200 |
commit | 361bb55f01ba28960a8127b0a16c1e7a7a8de61a (patch) | |
tree | ff0479af1c9283c8a0546025022ebc54616a9a75 /mktpl/mktpl.lex | |
parent | 2810ad72aeb973a898034d6875922284062c5ce0 (diff) | |
download | cforum-361bb55f01ba28960a8127b0a16c1e7a7a8de61a.tar.gz |
Rewrite mktpl script in C
The Perl version didn't handle multiple interpolations on the same line.
Diffstat (limited to 'mktpl/mktpl.lex')
-rw-r--r-- | mktpl/mktpl.lex | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/mktpl/mktpl.lex b/mktpl/mktpl.lex new file mode 100644 index 0000000..e530e45 --- /dev/null +++ b/mktpl/mktpl.lex @@ -0,0 +1,86 @@ +%x PRNT +%x EVAL + + #include <err.h> + #include <stdio.h> + #include <stdlib.h> + #include <string.h> + + void apnd(); + void eval(); + void text(); + void prnt(); + + char *buf; + int len; + int sz; + +%% +(.|\n) apnd(); +\<%= text(); BEGIN(PRNT); +\<%[^=] text(); BEGIN(EVAL); + +<PRNT>. apnd(); +<PRNT>%\> prnt(); BEGIN(0); + +<EVAL>(.|\n) apnd(); +<EVAL>%\> eval(); BEGIN(0); + +<PRNT><<EOF>> prnt(); yyterminate(); +<EVAL><<EOF>> eval(); yyterminate(); +<<EOF>> text(); yyterminate(); +%% + +int +main(int argc, char *argv[]) +{ + len = 0; + sz = 512; + if(!(buf = malloc(sz))) + err(1, "malloc"); + + yylex(); +} + +void +apnd() +{ + if(len+1 > sz){ + sz += 512; + if(!(buf = realloc(buf, sz))) + err(1, "realloc"); + } + + buf[len++] = *yytext; + buf[len+1] = 0; +} + +void +eval() +{ + printf("%s\n", buf); + len = 0; +} + +void +text() +{ + char *t; + + printf("printf(\""); + for(t = buf; *t; t++){ + if(*t == '\\') printf("\\\\"); + else if(*t == '"') printf("\\\""); + else if(*t == '\n') printf("\\n"); + else printf("%c", *t); + } + printf("\");\n"); + len = 0; +} + +void +prnt() +{ + printf("printf(\"%%s\", %s);\n", buf); + len = 0; +}
\ No newline at end of file |