Add --unset-var option

This commit is contained in:
zawwz 2020-11-27 11:03:55 +01:00
parent b4a99d366d
commit 6620fb42ff
7 changed files with 69 additions and 17 deletions

View file

@ -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

View file

@ -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<std::pair<std::string,arg*>> var_assigns;

View file

@ -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);

View file

@ -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;
}

View file

@ -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 //

View file

@ -9,17 +9,6 @@ std::string g_origin="";
const std::string cmd::empty_string="";
cmd* make_cmd(std::vector<std::string> args)
{
cmd* ret = new cmd();
ret->args = new arglist();
for(auto it: args)
{
ret->args->add(new arg(it));
}
return ret;
}
std::vector<std::string> arglist::strargs(uint32_t start)
{
std::vector<std::string> ret;
@ -48,7 +37,6 @@ std::string arg::string()
return dynamic_cast<string_subarg*>(sa[0])->val;
}
void condlist::prune_first_cmd()
{
if(pls.size()>0 && pls[0]->cmds.size()>0)

34
src/struc_helper.cpp Normal file
View file

@ -0,0 +1,34 @@
#include "struc.hpp"
cmd* make_cmd(std::vector<std::string> 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);
}