fix bug on minimizing collisions on 'unset' with bash

This commit is contained in:
zawwz 2020-12-11 12:28:09 +01:00
parent ed2ebba6ff
commit ebce56c29c
7 changed files with 41 additions and 13 deletions

View file

@ -61,6 +61,7 @@ void list_fcts(_obj* in, std::regex const& exclude);
void list_cmds(_obj* in, std::regex const& exclude); void list_cmds(_obj* in, std::regex const& exclude);
// recursives // recursives
bool r_get_unsets(_obj* in, set_t* unsets);
bool r_get_var(_obj* in, countmap_t* defmap, countmap_t* callmap); bool r_get_var(_obj* in, countmap_t* defmap, countmap_t* callmap);
bool r_get_cmd(_obj* in, countmap_t* all_cmds); bool r_get_cmd(_obj* in, countmap_t* all_cmds);
bool r_get_fct(_obj* in, countmap_t* fct_map); bool r_get_fct(_obj* in, countmap_t* fct_map);

View file

@ -262,10 +262,13 @@ public:
void add_arg(arg* in); void add_arg(arg* in);
// preceding var assigns // preceding var assigns
std::vector<std::pair<std::string,arg*>> var_assigns; std::vector<std::pair<std::string,arg*>> var_assigns;
// get var assigns in special cmds (export, unset, read) // check if cmd is this (ex: unset)
bool is(std::string const& in);
// for var assigns in special cmds (export, unset, read, local)
bool is_argvar(); bool is_argvar();
std::vector<subarg*> subarg_vars(); std::vector<subarg*> subarg_vars();

View file

@ -111,7 +111,14 @@ std::set<T> map_to_set(std::map<T,T2> in)
return ret; return ret;
} }
void concat_sets(std::set<std::string>& a, std::set<std::string> const& b); template <class T>
void concat_sets(std::set<T>& a, std::set<T> const& b)
{
for(auto it: b)
{
a.insert( it );
}
}
std::set<std::string> prune_matching(std::set<std::string>& in, std::regex re); std::set<std::string> prune_matching(std::set<std::string>& in, std::regex re);

View file

@ -1,6 +1,6 @@
#ifndef VERSION_H #ifndef VERSION_H
#define VERSION_H #define VERSION_H
#define VERSION_STRING "v0.2" #define VERSION_STRING "v0.2.1"
#endif //VERSION_H #endif //VERSION_H

View file

@ -179,14 +179,16 @@ void minimize_var(_obj* in, std::regex const& exclude)
void minimize_fct(_obj* in, std::regex const& exclude) void minimize_fct(_obj* in, std::regex const& exclude)
{ {
// countmap_t fcts, cmdmap; // countmap_t fcts, cmdmap;
set_t allcmds, excluded; set_t allcmds, excluded, unsets;
strmap_t fctmap; strmap_t fctmap;
// get fcts and cmds // get fcts and cmds
fctmap_get(in, exclude); fctmap_get(in, exclude);
cmdmap_get(in, regex_null); cmdmap_get(in, regex_null);
recurse(r_get_unsets, in, &unsets);
// concatenate cmds and excluded commands // concatenate cmds and excluded commands
allcmds=map_to_set(m_cmds); allcmds=map_to_set(m_cmds);
concat_sets(allcmds, m_excluded_fct); concat_sets(allcmds, m_excluded_fct);
concat_sets(allcmds, unsets);
// create mapping // create mapping
fctmap=gen_minimal_map(m_fcts, allcmds); fctmap=gen_minimal_map(m_fcts, allcmds);
// perform replace // perform replace

View file

@ -161,6 +161,11 @@ bool cmd::is_argvar()
return cmd_is_argvar(this->firstarg_string()); return cmd_is_argvar(this->firstarg_string());
} }
bool cmd::is(std::string const& in)
{
return in == this->firstarg_string();
}
/** GETTERS **/ /** GETTERS **/
void varmap_get(_obj* in, std::regex const& exclude) void varmap_get(_obj* in, std::regex const& exclude)
@ -196,7 +201,6 @@ void cmdmap_get(_obj* in, std::regex const& exclude)
/** OUTPUT **/ /** OUTPUT **/
void list_vars(_obj* in, std::regex const& exclude) void list_vars(_obj* in, std::regex const& exclude)
{ {
varmap_get(in, exclude); varmap_get(in, exclude);
@ -286,6 +290,25 @@ bool r_get_var(_obj* in, countmap_t* defmap, countmap_t* callmap)
return true; return true;
} }
bool r_get_unsets(_obj* in, set_t* unsets)
{
switch(in->type)
{
case _obj::block_cmd: {
cmd* t = dynamic_cast<cmd*>(in);
if(t->is("unset"))
{
for(uint32_t i=1; i<t->args->size(); i++)
{
unsets->insert(t->args->args[i]->string());
}
}
}; break;
default: break;
}
return true;
}
bool r_get_cmd(_obj* in, countmap_t* all_cmds) bool r_get_cmd(_obj* in, countmap_t* all_cmds)
{ {
switch(in->type) switch(in->type)

View file

@ -114,14 +114,6 @@ std::string concatargs(std::vector<std::string> const& args)
return ret; return ret;
} }
void concat_sets(std::set<std::string>& a, std::set<std::string> const& b)
{
for(auto it: b)
{
a.insert( it );
}
}
std::set<std::string> prune_matching(std::set<std::string>& in, std::regex re) std::set<std::string> prune_matching(std::set<std::string>& in, std::regex re)
{ {
std::set<std::string> ret; std::set<std::string> ret;