debashify manipulation substring
This commit is contained in:
parent
22ab42da08
commit
20e47ab620
3 changed files with 138 additions and 24 deletions
|
|
@ -32,6 +32,14 @@ bool possibly_expands(arglist* in);
|
||||||
void force_quotes(arg* in);
|
void force_quotes(arg* in);
|
||||||
void add_quotes(arg* in);
|
void add_quotes(arg* in);
|
||||||
|
|
||||||
|
cmd* make_printf(arg* in);
|
||||||
|
inline cmd* make_printf_variable(std::string const& name) {
|
||||||
|
return make_printf(new arg(new variable_subarg(new variable(name))));
|
||||||
|
}
|
||||||
|
|
||||||
|
arithmetic* make_arithmetic(arg* a);
|
||||||
|
arithmetic* make_arithmetic(arg* arg1, std::string op, arg* arg2);
|
||||||
|
|
||||||
// operators
|
// operators
|
||||||
inline bool operator==(arg a, std::string const& b) { return a.equals(b); }
|
inline bool operator==(arg a, std::string const& b) { return a.equals(b); }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -826,30 +826,76 @@ bool debashify_procsub(list* lst, debashify_params* params)
|
||||||
return has_replaced;
|
return has_replaced;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool debashify_variable_substitution(arg* in, debashify_params* params)
|
condlist* debashify_manipulation_substring(variable* v, debashify_params* params)
|
||||||
{
|
{
|
||||||
bool has_replaced=false;
|
string_subarg* first = dynamic_cast<string_subarg*>(v->manip->sa[0]);
|
||||||
for(uint32_t i=0; i<in->sa.size(); i++)
|
first->val = first->val.substr(1);
|
||||||
|
if(first->val == "")
|
||||||
{
|
{
|
||||||
if(in->sa[i]->type == _obj::subarg_variable)
|
delete v->manip->sa[0];
|
||||||
|
v->manip->sa.erase(v->manip->sa.begin());
|
||||||
|
}
|
||||||
|
std::string manip = v->manip->first_sa_string();
|
||||||
|
arg *arg1=nullptr, *arg2=nullptr;
|
||||||
|
size_t colon_pos = manip.find(':');
|
||||||
|
if(colon_pos != std::string::npos || v->manip->sa.size()>1)
|
||||||
{
|
{
|
||||||
variable* v = dynamic_cast<variable_subarg*>(in->sa[i])->var;
|
for(uint32_t i=0; i<v->manip->sa.size(); i++)
|
||||||
if(v->is_manip && v->precedence && v->manip->string() == "!")
|
|
||||||
{
|
{
|
||||||
arg* eval_arg = new arg;
|
if(v->manip->sa[i]->type == _obj::subarg_string)
|
||||||
eval_arg->add(new string_subarg("\\\"\\${"));
|
{
|
||||||
eval_arg->add(new variable_subarg(new variable(v->varname)));
|
string_subarg* t = dynamic_cast<string_subarg*>(v->manip->sa[i]);
|
||||||
eval_arg->add(new string_subarg("}\\\""));
|
size_t colon_pos = t->val.find(':');
|
||||||
cmd* eval_cmd = make_cmd({new arg("eval"), new arg("echo"), eval_arg});
|
if(colon_pos != std::string::npos)
|
||||||
subshell_subarg* r = new subshell_subarg(new subshell(eval_cmd));
|
{
|
||||||
r->quoted = in->sa[i]->quoted;
|
arg1 = new arg;
|
||||||
delete in->sa[i];
|
arg2 = new arg;
|
||||||
in->sa[i] = r;
|
for(uint32_t j=0; j<i; j++)
|
||||||
has_replaced=true;
|
arg1->add(v->manip->sa[j]);
|
||||||
|
std::string val=t->val.substr(0, colon_pos);
|
||||||
|
if(val != "")
|
||||||
|
arg1->add(new string_subarg(val));
|
||||||
|
val=t->val.substr(colon_pos+1);
|
||||||
|
if(val != "")
|
||||||
|
arg2->add(new string_subarg(val));
|
||||||
|
for(uint32_t j=i+1; j<v->manip->sa.size(); j++)
|
||||||
|
arg2->add(v->manip->sa[j]);
|
||||||
|
break;
|
||||||
|
// TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return has_replaced;
|
if(arg1 == nullptr)
|
||||||
|
arg1 = v->manip;
|
||||||
|
v->manip = nullptr;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
arg1 = v->manip;
|
||||||
|
v->manip = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
pipeline* pl = new pipeline(make_printf_variable(v->varname));
|
||||||
|
arg* retarg = new arg;
|
||||||
|
retarg->add(new arithmetic_subarg(make_arithmetic(arg1, "+", new arg("1"))));
|
||||||
|
retarg->add(new string_subarg("-"));
|
||||||
|
pl->add(make_cmd({new arg("cut"), new arg("-c"), retarg}));
|
||||||
|
|
||||||
|
if(arg2 != nullptr)
|
||||||
|
{
|
||||||
|
retarg = new arg;
|
||||||
|
retarg->add(new string_subarg("-"));
|
||||||
|
for(auto it: arg2->sa)
|
||||||
|
{
|
||||||
|
retarg->add(it);
|
||||||
|
}
|
||||||
|
arg2->sa.resize(0);
|
||||||
|
delete arg2;
|
||||||
|
arg2 = nullptr;
|
||||||
|
pl->add(make_cmd({new arg("cut"), new arg("-c"), retarg}));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new condlist(pl);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool debashify_manipulation(arg* in, debashify_params* params)
|
bool debashify_manipulation(arg* in, debashify_params* params)
|
||||||
|
|
@ -863,12 +909,19 @@ bool debashify_manipulation(arg* in, debashify_params* params)
|
||||||
if(!v->is_manip || v->manip == nullptr)
|
if(!v->is_manip || v->manip == nullptr)
|
||||||
return false;
|
return false;
|
||||||
std::string manip = v->manip->first_sa_string();
|
std::string manip = v->manip->first_sa_string();
|
||||||
if(manip.size()>0 && manip[0] == '/')
|
subarg* r = nullptr;
|
||||||
|
if(v->is_manip && v->precedence && v->manip->string() == "!")
|
||||||
{
|
{
|
||||||
cmd* prnt = make_cmd(std::vector<const char*>({"printf", "%s\\\\n"}));
|
arg* eval_arg = new arg;
|
||||||
arg* var = new arg(new variable_subarg(new variable(v->varname)));
|
eval_arg->add(new string_subarg("\\\"\\${"));
|
||||||
force_quotes(var);
|
eval_arg->add(new variable_subarg(new variable(v->varname)));
|
||||||
prnt->add(var);
|
eval_arg->add(new string_subarg("}\\\""));
|
||||||
|
cmd* eval_cmd = make_cmd({new arg("eval"), new arg("echo"), eval_arg});
|
||||||
|
r = new subshell_subarg(new subshell(eval_cmd));
|
||||||
|
}
|
||||||
|
else if(manip.size()>0 && manip[0] == '/')
|
||||||
|
{
|
||||||
|
cmd* prnt = make_printf_variable(v->varname);
|
||||||
// printf %s\\n "$var"
|
// printf %s\\n "$var"
|
||||||
cmd* sed = make_cmd({std::string("sed")});
|
cmd* sed = make_cmd({std::string("sed")});
|
||||||
arg* sedarg=v->manip;
|
arg* sedarg=v->manip;
|
||||||
|
|
@ -880,7 +933,15 @@ bool debashify_manipulation(arg* in, debashify_params* params)
|
||||||
// sed "s///g"
|
// sed "s///g"
|
||||||
pipeline* pl = new pipeline(prnt);
|
pipeline* pl = new pipeline(prnt);
|
||||||
pl->add(sed);
|
pl->add(sed);
|
||||||
subshell_subarg* r = new subshell_subarg(new subshell(new list(new condlist(pl))));
|
r = new subshell_subarg(new subshell(new list(new condlist(pl))));
|
||||||
|
}
|
||||||
|
else if(manip.size()>0 && manip[0] == ':')
|
||||||
|
{
|
||||||
|
r = new subshell_subarg(new subshell(new list(debashify_manipulation_substring(v, params))));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(r != nullptr)
|
||||||
|
{
|
||||||
r->quoted = in->sa[i]->quoted;
|
r->quoted = in->sa[i]->quoted;
|
||||||
delete in->sa[i];
|
delete in->sa[i];
|
||||||
in->sa[i] = r;
|
in->sa[i] = r;
|
||||||
|
|
@ -909,7 +970,6 @@ bool r_debashify(_obj* o, debashify_params* params)
|
||||||
case _obj::_arg: {
|
case _obj::_arg: {
|
||||||
arg* t = dynamic_cast<arg*>(o);
|
arg* t = dynamic_cast<arg*>(o);
|
||||||
debashify_subarg_replace(t, params);
|
debashify_subarg_replace(t, params);
|
||||||
debashify_variable_substitution(t, params);
|
|
||||||
debashify_manipulation(t, params);
|
debashify_manipulation(t, params);
|
||||||
} break;
|
} break;
|
||||||
case _obj::_list: {
|
case _obj::_list: {
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,52 @@ block* make_block(std::string const& in)
|
||||||
return parse_block(make_context(in)).first;
|
return parse_block(make_context(in)).first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmd* make_printf(arg* in)
|
||||||
|
{
|
||||||
|
cmd* prnt = make_cmd(std::vector<const char*>({"printf", "%s\\\\n"}));
|
||||||
|
force_quotes(in);
|
||||||
|
prnt->add(in);
|
||||||
|
return prnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
arithmetic* make_arithmetic(arg* a)
|
||||||
|
{
|
||||||
|
if(a->sa.size() != 1)
|
||||||
|
{
|
||||||
|
cmd* prnt = make_printf(a);
|
||||||
|
return new subshell_arithmetic(new subshell(prnt));
|
||||||
|
}
|
||||||
|
arithmetic* ret=nullptr;
|
||||||
|
switch(a->sa[0]->type) {
|
||||||
|
case _obj::subarg_string : {
|
||||||
|
string_subarg* t = dynamic_cast<string_subarg*>(a->sa[0]);
|
||||||
|
ret = new number_arithmetic(t->val);
|
||||||
|
}; break;
|
||||||
|
case _obj::subarg_variable : {
|
||||||
|
variable_subarg* t = dynamic_cast<variable_subarg*>(a->sa[0]);
|
||||||
|
ret = new variable_arithmetic(t->var);
|
||||||
|
t->var = nullptr;
|
||||||
|
}; break;
|
||||||
|
case _obj::subarg_subshell : {
|
||||||
|
subshell_subarg* t = dynamic_cast<subshell_subarg*>(a->sa[0]);
|
||||||
|
ret = new subshell_arithmetic(t->sbsh);
|
||||||
|
t->sbsh = nullptr;
|
||||||
|
}; break;
|
||||||
|
case _obj::subarg_arithmetic : {
|
||||||
|
arithmetic_subarg* t = dynamic_cast<arithmetic_subarg*>(a->sa[0]);
|
||||||
|
ret = t->arith;
|
||||||
|
t->arith = nullptr;
|
||||||
|
}; break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
arithmetic* make_arithmetic(arg* arg1, std::string op, arg* arg2)
|
||||||
|
{
|
||||||
|
return new operation_arithmetic(op, make_arithmetic(arg1), make_arithmetic(arg2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// copy
|
// copy
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue