From d10be4116aa9dff40a72f18ab15630d257142711 Mon Sep 17 00:00:00 2001 From: zawwz Date: Fri, 10 Dec 2021 15:33:05 +0100 Subject: [PATCH] fix(parse): fix incorrect parse of heredocuments on broken lists --- src/minify.cpp | 6 +++--- src/parse.cpp | 46 +++++++++++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/minify.cpp b/src/minify.cpp index 8023067..cdad5f1 100644 --- a/src/minify.cpp +++ b/src/minify.cpp @@ -591,14 +591,14 @@ bool r_minify_single_block(_obj* in) if(ret != nullptr) { // concatenate redirects for(uint32_t j=0; jcmds[i]->redirs.size(); j++) - ret->redirs.insert(ret->redirs.begin()+j, t->cmds[i]->redirs[j]); + ret->redirs.insert(ret->redirs.begin()+j, t->cmds[i]->redirs[j]); // deindex t->cmds[i]->redirs.resize(0); if(t->cmds[i]->type == _obj::block_brace) - dynamic_cast(t->cmds[i])->lst->cls[0]->pls[0]->cmds[0] = nullptr; + dynamic_cast(t->cmds[i])->lst->cls[0]->pls[0]->cmds[0] = nullptr; else if(t->cmds[i]->type == _obj::block_subshell) - dynamic_cast(t->cmds[i])->lst->cls[0]->pls[0]->cmds[0] = nullptr; + dynamic_cast(t->cmds[i])->lst->cls[0]->pls[0]->cmds[0] = nullptr; // replace value delete t->cmds[i]; diff --git a/src/parse.cpp b/src/parse.cpp index 58b4ad3..428b30f 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -1085,27 +1085,35 @@ std::tuple parse_list_until(parse_context c if(ctx.here_document != nullptr) { - uint8_t do_twice=2; - // case of : cat << EOF ; - while(do_twice>0) + bool has_parsed=false; + parse_context t_ctx=ctx; + if(t_ctx[t_ctx.i] == '\n') { - if(ctx[ctx.i] == '\n') - { - ctx = parse_heredocument(ctx+1); - break; - } - else if(ctx[ctx.i] == '#') - { - ctx.i = skip_until(ctx, "\n"); //skip to endline - ctx = parse_heredocument(ctx+1); - break; - } - skip_chars(ctx, SPACES); - do_twice--; + t_ctx = parse_heredocument(t_ctx+1); + has_parsed=true; } - // case of : cat << EOF ; ; - if(do_twice==0 && is_in(ctx[ctx.i], COMMAND_SEPARATOR)) - parse_error( unexpected_token(ctx[ctx.i]), ctx); + else if(t_ctx[t_ctx.i] == '#') + { + t_ctx.i = skip_until(t_ctx, "\n"); //skip to endline + t_ctx = parse_heredocument(t_ctx+1); + has_parsed=true; + } + else if(t_ctx[t_ctx.i] == ';') { + t_ctx.i = skip_chars(t_ctx+1, SPACES); + if(t_ctx[t_ctx.i] == '\n') + { + t_ctx = parse_heredocument(t_ctx+1); + has_parsed=true; + } + else if(t_ctx[t_ctx.i] == '#') + { + t_ctx.i = skip_until(t_ctx, "\n"); //skip to endline + t_ctx = parse_heredocument(t_ctx+1); + has_parsed=true; + } + } + if(has_parsed) + ctx = t_ctx; } if(is_in(ctx[ctx.i], COMMAND_SEPARATOR))