From af3fa90e8bb41c306c5fe2d2cf105db6bbabd1d9 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 19 Oct 2021 10:53:45 -0700 Subject: Feat: word operators and more robust crashing Added the length and concatenate operators. Slightly improved the robustness on syntax errors. --- sys/cmd/rc/lex.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'sys/cmd/rc/lex.c') diff --git a/sys/cmd/rc/lex.c b/sys/cmd/rc/lex.c index b4a2e8c..06737ba 100644 --- a/sys/cmd/rc/lex.c +++ b/sys/cmd/rc/lex.c @@ -2,6 +2,7 @@ #include "parse.h" static int advance(void); + // ----------------------------------------------------------------------- // lexer @@ -14,10 +15,14 @@ struct Lexer static struct Lexer lexer = { .c={0, EOF}, .doprompt=1 }; +#define put1(b) lexer.buf[0] = (b), lexer.buf[1] = 0; +#define put2(b0,b1) lexer.buf[0] = (b0), lexer.buf[1] = (b1), lexer.buf[2] = 0; +#define put3(b0,b1,b2) lexer.buf[0] = (b0), lexer.buf[1] = (b1), lexer.buf[2] = b2, lexer.buf[3] = 0; + void yyerror(const char *msg) { - print(shell.err, "\nrc:%d: ", runner->line); + print(shell.err, "rc:%d: ", runner->line); if(lexer.buf[0] && lexer.buf[0]!='\n') print(shell.err, "%q: ", lexer.buf); @@ -48,7 +53,6 @@ readc(void) exit(1); // XXX: hack for signal handling right now... c = get(runner->cmd.io); - lexer.doprompt = lexer.doprompt || c=='\n' || c==EOF; if(c==EOF) @@ -180,12 +184,25 @@ yylex(void) skipws(); switch(c=advance()){ case EOF: + put3('E','O','F'); return EOF; + case '&': - lexer.buf[0] = '&'; - lexer.buf[1] = 0; + put1('&'); return '&'; + case '$': + if(nextis('#')){ + put2('$','#'); + return Tcount; + } + if(nextis('^')){ + put2('$','^'); + return Tflat; + } + put1('$'); + return '$'; + default: ; } -- cgit v1.2.1