extend minify: minify subshells to backticks

This commit is contained in:
Mateo Feron 2021-10-14 15:39:52 +02:00
parent 92d5f83b2f
commit abce171e94
4 changed files with 50 additions and 3 deletions

View file

@ -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);
};

View file

@ -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)

View file

@ -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<subshell_subarg*>(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<subshell_subarg*>(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);
}

View file

@ -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;