From abce171e94775c40ca91dd1d357eeaba7b7ba7f3 Mon Sep 17 00:00:00 2001 From: Mateo Feron Date: Thu, 14 Oct 2021 15:39:52 +0200 Subject: [PATCH] extend minify: minify subshells to backticks --- include/struc.hpp | 3 ++- src/generate.cpp | 9 ++++++++- src/minify.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/parse.cpp | 2 +- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/include/struc.hpp b/include/struc.hpp index 50b1bb9..2e5f3d0 100644 --- a/include/struc.hpp +++ b/include/struc.hpp @@ -599,10 +599,11 @@ public: class subshell_subarg : public subarg { public: - subshell_subarg(subshell* in=nullptr, bool inq=false) { type=_obj::subarg_subshell; sbsh=in; quoted=inq; } + subshell_subarg(subshell* in=nullptr, bool inq=false, bool is_backtick=false) { type=_obj::subarg_subshell; sbsh=in; quoted=inq; backtick=is_backtick; } ~subshell_subarg() { if(sbsh != nullptr) delete sbsh; } subshell* sbsh; + bool backtick; std::string generate(int ind); }; diff --git a/src/generate.cpp b/src/generate.cpp index 87f3c68..9411ebc 100644 --- a/src/generate.cpp +++ b/src/generate.cpp @@ -398,7 +398,14 @@ std::string cmd::generate(int ind, generate_context* ctx) std::string subshell_subarg::generate(int ind) { - return '$' + sbsh->generate(ind); + std::string r = sbsh->generate(ind); + if(backtick) { + r[0] = '`'; + r[r.size()-1] = '`'; + return r; + } + else + return '$' + r; } std::string procsub_subarg::generate(int ind) diff --git a/src/minify.cpp b/src/minify.cpp index ebc1b90..18a3123 100644 --- a/src/minify.cpp +++ b/src/minify.cpp @@ -523,6 +523,44 @@ bool r_minify_single_block(_obj* in) return true; } +bool r_has_backtick(_obj* in, bool* r) +{ + if(*r) + return false; + switch(in->type) + { + case _obj::subarg_subshell: { + subshell_subarg* t = dynamic_cast(in); + if(t->backtick) { + *r = true; + return false; + } + }; break; + default: break; + } + return true; +} + +bool r_minify_backtick(_obj* in) +{ + switch(in->type) + { + case _obj::subarg_subshell: { + subshell_subarg* t = dynamic_cast(in); + if(!t->backtick) { + bool has_backtick_child=false; + recurse(r_has_backtick, t->sbsh, &has_backtick_child); + if(has_backtick_child) + return false; + t->backtick = true; + } + return false; + }; break; + default: break; + } + return true; +} + bool r_minify(_obj* in) { r_minify_empty_manip(in); @@ -535,4 +573,5 @@ bool r_minify(_obj* in) void minify_generic(_obj* in) { recurse(r_minify, in); + recurse(r_minify_backtick, in); } diff --git a/src/parse.cpp b/src/parse.cpp index ba10709..1ca1f9b 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -444,7 +444,7 @@ inline parse_context do_one_subarg_step(arg* ret, parse_context ctx, uint32_t& j parse_context newct = ctx; newct.size=k; auto r=parse_list_until(newct); - ret->add(new subshell_subarg(new subshell(std::get<0>(r)), is_quoted)); + ret->add(new subshell_subarg(new subshell(std::get<0>(r)), is_quoted, true)); uint64_t tsize=ctx.size; ctx = std::get<1>(r); ctx.size = tsize;