diff --git a/include/processing.hpp b/include/processing.hpp index 84ba12b..dc51260 100644 --- a/include/processing.hpp +++ b/include/processing.hpp @@ -41,7 +41,7 @@ void varmap_get(_obj* in, std::regex const& exclude); void fctmap_get(_obj* in, std::regex const& exclude); void cmdmap_get(_obj* in, std::regex const& exclude); -/** functions **/ +/** util functions **/ // gen regexes std::regex var_exclude_regex(std::string const& in, bool include_reserved); @@ -67,4 +67,8 @@ bool r_get_fct(_obj* in, countmap_t* fct_map); bool r_delete_fct(_obj* in, set_t* fcts); bool r_delete_var(_obj* in, set_t* vars); +/** Processing **/ + +void add_unset_variables(shmain* sh, std::regex const& exclude); + #endif //PROCESSING_HPP diff --git a/include/struc.hpp b/include/struc.hpp index 8168987..7447686 100644 --- a/include/struc.hpp +++ b/include/struc.hpp @@ -116,7 +116,8 @@ public: // return if is a string and only one subarg std::string string(); - bool equals(std::string const& in) { return this->string() == in; } + + inline bool equals(std::string const& in) { return this->string() == in; } std::string generate(int ind); }; @@ -129,6 +130,7 @@ class arglist : public _obj { public: arglist() { type=_obj::_arglist; } + arglist(arg* in) { type=_obj::_arglist; this->add(in); } ~arglist() { for( auto it: args ) delete it; } inline void add(arg* in) { args.push_back(in); } inline void push_back(arg* in) { args.push_back(in); } @@ -192,7 +194,9 @@ public: class condlist : public _obj { public: - condlist(pipeline* pl=nullptr) { type=_obj::_condlist; parallel=false; if(pl!=nullptr) this->add(pl); } + condlist() { type=_obj::_condlist; parallel=false; } + condlist(pipeline* pl); + condlist(block* bl); ~condlist() { for(auto it: pls) delete it; } bool parallel; // & at the end @@ -256,6 +260,8 @@ public: size_t arglist_size(); + void add_arg(arg* in); + // preceding var assigns std::vector> var_assigns; diff --git a/src/main.cpp b/src/main.cpp index e4b5290..95adffe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -149,6 +149,7 @@ int main(int argc, char* argv[]) } // processing before output + // minimize if(options['m']) opt_minimize=true; if(options["remove-unused"]) @@ -157,6 +158,9 @@ int main(int argc, char* argv[]) minimize_var( sh, re_var_exclude ); if(options["minimize-fct"]) minimize_fct( sh, re_fct_exclude ); + // other processing + if(options["unset-var"]) + add_unset_variables( sh, re_var_exclude ); if(options["list-var"]) list_vars(sh, re_var_exclude); diff --git a/src/options.cpp b/src/options.cpp index 5cd5e61..078a6e7 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -37,8 +37,8 @@ ztd::option_set gen_options() ztd::option("list-var-call", false, "List all variables invoked in the script"), ztd::option("list-fct", false, "List all functions defined in the script"), ztd::option("list-cmd", false, "List all commands invoked in the script"), - ztd::option("remove-unused", false, "Remove unused functions and variables") - // ztd::option("unset-var", false, "Add 'unset' to all vars at the start of the script to avoid environment interference") + ztd::option("remove-unused", false, "Remove unused functions and variables"), + ztd::option("unset-var", false, "Add 'unset' to all vars at the start of the script to avoid environment interference") ); return ret; } diff --git a/src/processing.cpp b/src/processing.cpp index a4a1c0d..a6a12a8 100644 --- a/src/processing.cpp +++ b/src/processing.cpp @@ -227,6 +227,22 @@ void list_cmds(_obj* in, std::regex const& exclude) list_map(m_cmds); } +/** FUNCTIONS **/ + +void add_unset_variables(shmain* sh, std::regex const& exclude) +{ + varmap_get(sh, exclude); + if(m_vars.size()>0) + { + cmd* unset_cmd = new cmd; + unset_cmd->add_arg(new arg("unset")); + for(auto it: m_vars) + unset_cmd->add_arg(new arg(it.first)); + condlist* cl = new condlist(unset_cmd); + sh->lst->cls.insert(sh->lst->cls.begin(), cl); + } +} + /** RECURSIVES **/ // GET // diff --git a/src/struc.cpp b/src/struc.cpp index 3f7cac3..4da20e7 100644 --- a/src/struc.cpp +++ b/src/struc.cpp @@ -9,17 +9,6 @@ std::string g_origin=""; const std::string cmd::empty_string=""; -cmd* make_cmd(std::vector args) -{ - cmd* ret = new cmd(); - ret->args = new arglist(); - for(auto it: args) - { - ret->args->add(new arg(it)); - } - return ret; -} - std::vector arglist::strargs(uint32_t start) { std::vector ret; @@ -48,7 +37,6 @@ std::string arg::string() return dynamic_cast(sa[0])->val; } - void condlist::prune_first_cmd() { if(pls.size()>0 && pls[0]->cmds.size()>0) diff --git a/src/struc_helper.cpp b/src/struc_helper.cpp new file mode 100644 index 0000000..03ebc19 --- /dev/null +++ b/src/struc_helper.cpp @@ -0,0 +1,34 @@ +#include "struc.hpp" + +cmd* make_cmd(std::vector args) +{ + cmd* ret = new cmd(); + ret->args = new arglist(); + for(auto it: args) + { + ret->args->add(new arg(it)); + } + return ret; +} + +condlist::condlist(pipeline* pl) +{ + type=_obj::_condlist; + parallel=false; + if(pl!=nullptr) this->add(pl); +} + +condlist::condlist(block* bl) +{ + type=_obj::_condlist; + parallel=false; + this->add(new pipeline(bl)); +} + +void cmd::add_arg(arg* in) +{ + if(args==nullptr) + args = new arglist; + + args->push_back(in); +}