From 22ab42da0831ab5207f65e5123b7072e6c91323f Mon Sep 17 00:00:00 2001 From: zawwz Date: Tue, 27 Jul 2021 18:03:37 +0200 Subject: [PATCH] debashify manipulation replace --- include/struc_helper.hpp | 1 + src/debashify.cpp | 44 ++++++++++++++++++++++++++++++++++++++-- src/struc_helper.cpp | 10 ++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/include/struc_helper.hpp b/include/struc_helper.hpp index 34b088f..9c13066 100644 --- a/include/struc_helper.hpp +++ b/include/struc_helper.hpp @@ -6,6 +6,7 @@ // makers arg* make_arg(std::string const& in); +cmd* make_cmd(std::vector const& args); cmd* make_cmd(std::vector const& args); cmd* make_cmd(std::vector const& args); cmd* make_cmd(std::string const& in); diff --git a/src/debashify.cpp b/src/debashify.cpp index e640090..7948073 100644 --- a/src/debashify.cpp +++ b/src/debashify.cpp @@ -837,10 +837,10 @@ bool debashify_variable_substitution(arg* in, debashify_params* params) if(v->is_manip && v->precedence && v->manip->string() == "!") { arg* eval_arg = new arg; - eval_arg->add(new string_subarg("echo \\\"\\${")); + eval_arg->add(new string_subarg("\\\"\\${")); eval_arg->add(new variable_subarg(new variable(v->varname))); eval_arg->add(new string_subarg("}\\\"")); - cmd* eval_cmd = make_cmd(std::vector({new arg("eval"), eval_arg})); + cmd* eval_cmd = make_cmd({new arg("eval"), new arg("echo"), eval_arg}); subshell_subarg* r = new subshell_subarg(new subshell(eval_cmd)); r->quoted = in->sa[i]->quoted; delete in->sa[i]; @@ -852,6 +852,45 @@ bool debashify_variable_substitution(arg* in, debashify_params* params) return has_replaced; } +bool debashify_manipulation(arg* in, debashify_params* params) +{ + bool has_replaced=false; + for(uint32_t i=0; isa.size(); i++) + { + if(in->sa[i]->type == _obj::subarg_variable) + { + variable* v = dynamic_cast(in->sa[i])->var; + if(!v->is_manip || v->manip == nullptr) + return false; + std::string manip = v->manip->first_sa_string(); + if(manip.size()>0 && manip[0] == '/') + { + cmd* prnt = make_cmd(std::vector({"printf", "%s\\\\n"})); + arg* var = new arg(new variable_subarg(new variable(v->varname))); + force_quotes(var); + prnt->add(var); + // printf %s\\n "$var" + cmd* sed = make_cmd({std::string("sed")}); + arg* sedarg=v->manip; + v->manip = nullptr; + sedarg->insert(0, new string_subarg("s")); + sedarg->add(new string_subarg("/")); + force_quotes(sedarg); + sed->add(sedarg); + // sed "s///g" + pipeline* pl = new pipeline(prnt); + pl->add(sed); + subshell_subarg* r = new subshell_subarg(new subshell(new list(new condlist(pl)))); + r->quoted = in->sa[i]->quoted; + delete in->sa[i]; + in->sa[i] = r; + has_replaced=true; + } + } + } + return has_replaced; +} + bool debashify_var(variable* in, debashify_params* params) { return false; @@ -871,6 +910,7 @@ bool r_debashify(_obj* o, debashify_params* params) arg* t = dynamic_cast(o); debashify_subarg_replace(t, params); debashify_variable_substitution(t, params); + debashify_manipulation(t, params); } break; case _obj::_list: { list* t = dynamic_cast(o); diff --git a/src/struc_helper.cpp b/src/struc_helper.cpp index 2c26ee2..57d0236 100644 --- a/src/struc_helper.cpp +++ b/src/struc_helper.cpp @@ -12,13 +12,21 @@ arg* make_arg(std::string const& in) return parse_arg(make_context(in)).first; } +cmd* make_cmd(std::vector const& args) +{ + cmd* ret = new cmd; + ret->args = new arglist; + for(auto it: args) + ret->args->add(new arg(it)); + return ret; +} + cmd* make_cmd(std::vector const& args) { cmd* ret = new cmd; ret->args = new arglist; for(auto it: args) ret->args->add(new arg(it)); - return ret; }