From 1b0c97f5bb2e1e592a9106963aa8be379bd3ae40 Mon Sep 17 00:00:00 2001 From: zawwz Date: Fri, 8 Jan 2021 15:57:42 +0100 Subject: [PATCH] implement debashify on <<< herestring --- include/struc.hpp | 7 ++++--- include/struc_helper.hpp | 2 ++ src/debashify.cpp | 25 +++++++++++++++++++++++++ src/parse.cpp | 4 +++- src/struc_helper.cpp | 18 ++++++++++++++++++ 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/include/struc.hpp b/include/struc.hpp index 49f1f87..97bce64 100644 --- a/include/struc.hpp +++ b/include/struc.hpp @@ -103,6 +103,8 @@ class subarg : public _obj public: virtual ~subarg() {;} virtual std::string generate(int ind)=0; + + bool quoted; }; class arg : public _obj @@ -445,7 +447,7 @@ public: class variable_subarg : public subarg { public: - variable_subarg(std::string const& in="") { type=_obj::subarg_variable; varname=in; } + variable_subarg(std::string const& in="", bool inq=false) { type=_obj::subarg_variable; varname=in; quoted=inq; } ~variable_subarg() {;} std::string varname; @@ -471,7 +473,6 @@ public: ~subshell_subarg() { if(sbsh != nullptr) delete sbsh; } subshell* sbsh; - bool quoted; std::string generate(int ind); }; @@ -479,7 +480,7 @@ public: class manipulation_subarg : public subarg { public: - manipulation_subarg(arg* in=nullptr) { type=_obj::subarg_manipulation; size=false; manip=in; } + manipulation_subarg(std::string varname="", arg* in=nullptr, bool inq=false) { type=_obj::subarg_manipulation; size=false; manip=in; quoted=inq; } ~manipulation_subarg() { if(manip!=nullptr) delete manip; } bool size; diff --git a/include/struc_helper.hpp b/include/struc_helper.hpp index e91f078..85a6bf0 100644 --- a/include/struc_helper.hpp +++ b/include/struc_helper.hpp @@ -8,6 +8,8 @@ cmd* make_cmd(std::vector const& args); cmd* make_cmd(std::string const& in); condlist* make_condlist(std::string const& in); +void force_quotes(arg* in); + inline bool operator==(arg a, std::string const& b) { return a.equals(b); } #endif //STRUC_HELPER_HPP diff --git a/src/debashify.cpp b/src/debashify.cpp index 8fb1388..9182166 100644 --- a/src/debashify.cpp +++ b/src/debashify.cpp @@ -32,6 +32,27 @@ bool debashify_array_def(cmd* in) bool debashify_herestring(pipeline* pl) { + if(pl->cmds.size()>0) + { + block* c=pl->cmds[0]; + for(uint32_t i=0; iredirs.size() ; i++) + { + if(c->redirs[i]->op == "<<<") + { + force_quotes(c->redirs[i]->target); + cmd* printcmd = make_cmd("printf '%s\\n'"); + printcmd->add(c->redirs[i]->target); + pl->cmds.insert(pl->cmds.begin(), printcmd); + + // cleanup + c->redirs[i]->target=nullptr; + delete c->redirs[i]; + c->redirs.erase(pl->cmds[1]->redirs.begin()+i); + + return true; + } + } + } return false; } @@ -187,6 +208,10 @@ bool r_debashify(_obj* o, bool* need_random_func) if(debashify_procsub(t)) *need_random_func = true; } break; + case _obj::_pipeline: { + pipeline* t = dynamic_cast(o); + debashify_herestring(t); + } break; case _obj::block_cmd: { cmd* t = dynamic_cast(o); debashify_combined_redirects(t); diff --git a/src/parse.cpp b/src/parse.cpp index d96b9c8..4f301f8 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -252,6 +252,7 @@ std::pair parse_arg(const char* in, uint32_t size, uint32_t star ret->add(new string_subarg(tmpstr)); // get arithmetic auto r=parse_arithmetic(in, size, i+3); + r.first->quoted=true; ret->add(r.first); j = i = r.second; } @@ -274,6 +275,7 @@ std::pair parse_arg(const char* in, uint32_t size, uint32_t star ret->add(new string_subarg(tmpstr)); // get manipulation auto r=parse_manipulation(in, size, i+2); + r.first->quoted=true; ret->add(r.first); j = i = r.second; } @@ -287,7 +289,7 @@ std::pair parse_arg(const char* in, uint32_t size, uint32_t star if(tmpstr!="") ret->add(new string_subarg(tmpstr)); // add varname - ret->add(new variable_subarg(r.first)); + ret->add(new variable_subarg(r.first, true)); j = i = r.second; } else diff --git a/src/struc_helper.cpp b/src/struc_helper.cpp index cd169e1..60d7e8f 100644 --- a/src/struc_helper.cpp +++ b/src/struc_helper.cpp @@ -5,6 +5,8 @@ // ** FUNCTIONS ** // +// makers + arg* make_arg(std::string const& in) { return parse_arg(in.c_str(), in.size(), 0).first; @@ -31,6 +33,22 @@ condlist* make_condlist(std::string const& in) return parse_condlist(in.c_str(), in.size(), 0).first; } +// modifiers + +void force_quotes(arg* in) +{ + for(uint32_t i=0; i < in->sa.size() ; i++) + { + if(!in->sa[i]->quoted && (in->sa[i]->type == _obj::subarg_variable || in->sa[i]->type == _obj::subarg_manipulation || in->sa[i]->type == _obj::subarg_subshell) ) + { + in->sa[i]->quoted=true; + in->insert(i, new string_subarg("\"")); + i+=2; + in->insert(i, new string_subarg("\"")); + } + } +} + // ** CLASS EXTENSIONS ** // /// GETTERS ///