fix echo debashify

This commit is contained in:
zawz 2021-02-26 17:06:15 +01:00
parent e61a2e9c85
commit 06eac33b0a
3 changed files with 37 additions and 7 deletions

View file

@ -23,8 +23,9 @@ arg* copy(arg* in);
variable* copy(variable* in); variable* copy(variable* in);
// testers // testers
bool arg_has_char(char c, arg* in); bool arg_has_char(char c, arg* in);
bool possibly_expands(arg* in);
bool possibly_expands(arglist* in);
// modifiers // modifiers
void force_quotes(arg* in); void force_quotes(arg* in);

View file

@ -215,13 +215,26 @@ bool debashify_echo(pipeline* pl)
{ {
delete in->args->args[0]; delete in->args->args[0];
in->args->args[0] = new arg("printf"); in->args->args[0] = new arg("printf");
in->args->insert(1, new arg("%s\\ ")); if(possibly_expands(in->args->args[2]) )
if(newline) // newline: add a newline command at the end
{ {
brace* br = new brace(new list); in->args->insert(1, new arg("%s\\ "));
br->lst->add(new condlist(in)); if(newline) // newline: add a newline command at the end
br->lst->add(make_condlist("echo")); {
pl->cmds[0] = br; brace* br = new brace(new list);
br->lst->add(new condlist(in));
br->lst->add(make_condlist("echo"));
pl->cmds[0] = br;
}
}
else
{
std::string printfarg="'%s";
for(uint32_t i=2; i<in->args->size(); i++)
printfarg+=" %s";
if(newline)
printfarg+="\\n";
printfarg+="'";
in->args->insert(1, new arg(printfarg));
} }
} }

View file

@ -115,6 +115,22 @@ bool arg_has_char(char c, arg* in)
return false; return false;
} }
bool possibly_expands(arg* in)
{
for(auto it: in->sa)
if( (it->type == _obj::subarg_subshell || it->type == _obj::subarg_variable ) && it->quoted == false)
return true;
return false;
}
bool possibly_expands(arglist* in)
{
for(auto it: in->args)
if(possibly_expands(it))
return true;
return false;
}
// ** CLASS EXTENSIONS ** // // ** CLASS EXTENSIONS ** //
/// GETTERS /// /// GETTERS ///