From 369cba5f938d3f804210b6e09d36c6f7ecb017fa Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Fri, 7 Jan 2022 20:50:00 +0000 Subject: rc: read heredoc when receiving '\n' (thanks Eckard Brauer) Eckard's test case: cat <type!=WORD){ - yyerror("Bad here tag"); - return 0; - } pprompt(); out = openiostr(); m = tag->str; @@ -47,6 +43,34 @@ readhere(tree *tag, io *in) return closeiostr(out); } +static tree *head, *tail; + +void +heredoc(tree *redir) +{ + if(redir->child[0]->type!=WORD){ + yyerror("Bad here tag"); + return; + } + redir->child[2]=0; + if(head) + tail->child[2]=redir; + else + head=redir; + tail=redir; +} + +void +readhere(io *in) +{ + while(head){ + tail=head->child[2]; + head->child[2]=0; + head->str=readhere1(head->child[0], in); + head=tail; + } +} + void psubst(io *f, unsigned char *s) { diff --git a/sys/src/cmd/rc/syn.y b/sys/src/cmd/rc/syn.y index 7aeb7f571..c29241f55 100644 --- a/sys/src/cmd/rc/syn.y +++ b/sys/src/cmd/rc/syn.y @@ -22,7 +22,7 @@ %type WORD REDIR DUP PIPE %% rc: { return 1;} -| line '\n' {return !compile($1);} +| line '\n' {readhere(lex->input); return !compile($1);} line: cmd | cmdsa line {$$=tree2(';', $1, $2);} body: cmd @@ -30,13 +30,13 @@ body: cmd cmdsa: cmd ';' | cmd '&' {$$=tree1('&', $1);} cmdsan: cmdsa -| cmd '\n' +| cmd '\n' {readhere(lex->input);} brace: '{' body '}' {$$=tree1(BRACE, $2);} paren: '(' body ')' {$$=tree1(PCMD, $2);} assign: first '=' word {$$=tree2('=', $1, $3);} epilog: {$$=0;} | redir epilog {$$=mung2($1, $1->child[0], $2);} -redir: REDIR word {($$=mung1($1, $2))->str=$1->rtype==HERE?readhere($2,lex->input):0;} +redir: REDIR word {$$=mung1($1, $2); if($$->rtype==HERE) heredoc($$);} | DUP cmd: {$$=0;} | brace epilog {$$=epimung($1, $2);} -- cgit v1.2.3