From 06eac33b0a6e36be7c59e105831dfe2f40bda717 Mon Sep 17 00:00:00 2001 From: zawz Date: Fri, 26 Feb 2021 17:06:15 +0100 Subject: [PATCH] fix echo debashify --- include/struc_helper.hpp | 3 ++- src/debashify.cpp | 25 +++++++++++++++++++------ src/struc_helper.cpp | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/include/struc_helper.hpp b/include/struc_helper.hpp index 8b48845..34b088f 100644 --- a/include/struc_helper.hpp +++ b/include/struc_helper.hpp @@ -23,8 +23,9 @@ arg* copy(arg* in); variable* copy(variable* in); // testers - bool arg_has_char(char c, arg* in); +bool possibly_expands(arg* in); +bool possibly_expands(arglist* in); // modifiers void force_quotes(arg* in); diff --git a/src/debashify.cpp b/src/debashify.cpp index 0c0e66e..b69feb1 100644 --- a/src/debashify.cpp +++ b/src/debashify.cpp @@ -215,13 +215,26 @@ bool debashify_echo(pipeline* pl) { delete in->args->args[0]; in->args->args[0] = new arg("printf"); - in->args->insert(1, new arg("%s\\ ")); - if(newline) // newline: add a newline command at the end + if(possibly_expands(in->args->args[2]) ) { - brace* br = new brace(new list); - br->lst->add(new condlist(in)); - br->lst->add(make_condlist("echo")); - pl->cmds[0] = br; + in->args->insert(1, new arg("%s\\ ")); + if(newline) // newline: add a newline command at the end + { + 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; iargs->size(); i++) + printfarg+=" %s"; + if(newline) + printfarg+="\\n"; + printfarg+="'"; + in->args->insert(1, new arg(printfarg)); } } diff --git a/src/struc_helper.cpp b/src/struc_helper.cpp index 68e18a5..8ef3c7e 100644 --- a/src/struc_helper.cpp +++ b/src/struc_helper.cpp @@ -115,6 +115,22 @@ bool arg_has_char(char c, arg* in) 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 ** // /// GETTERS ///