diff --git a/include/parse.hpp b/include/parse.hpp index a0981c3..fdc5e1e 100644 --- a/include/parse.hpp +++ b/include/parse.hpp @@ -97,35 +97,32 @@ inline uint32_t skip_unread_noline(parse_context const& ct) { parse_context parse_heredocument(parse_context ctx); // list -// std::pair parse_list_until(parse_context ct, char end_c, const char* expecting=NULL); -// std::pair parse_list_until(parse_context ct, std::string const& end_word); -// std::tuple parse_list_until(parse_context ct, std::vector const& end_words, const char* expecting=NULL); -std::tuple parse_list_until(parse_context ct, list_parse_options opts={}); +std::tuple parse_list_until(parse_context ct, list_parse_options opts={}); // name -std::pair parse_var(parse_context ct, bool specialvars=true, bool array=false); +std::pair parse_var(parse_context ct, bool specialvars=true, bool array=false); // subarg parsers -std::pair parse_arithmetic(parse_context ct); -std::pair parse_manipulation(parse_context ct); +std::pair parse_arithmetic(parse_context ct); +std::pair parse_manipulation(parse_context ct); // arg parser -std::pair parse_arg(parse_context ct, const char* end=ARG_END, const char* unexpected=ARGLIST_END, bool doquote=true, const char* optimize=ARG_OPTIMIZE_ARG); +std::pair parse_arg(parse_context ct, const char* end=ARG_END, const char* unexpected=ARGLIST_END, bool doquote=true, const char* optimize=ARG_OPTIMIZE_ARG); // redirect parser -std::pair parse_redirect(parse_context ct); +std::pair parse_redirect(parse_context ct); // arglist parser -std::pair parse_arglist(parse_context ct, bool hard_error=false, std::vector* redirs=nullptr); +std::pair parse_arglist(parse_context ct, bool hard_error=false, std::vector* redirs=nullptr); // block parsers -std::pair parse_block(parse_context ct); -std::pair parse_cmd(parse_context ct); -std::pair parse_function(parse_context ct, const char* after="()"); -std::pair parse_subshell(parse_context ct); -std::pair parse_brace(parse_context ct); -std::pair parse_case(parse_context ct); -std::pair parse_if(parse_context ct); -std::pair parse_for(parse_context ct); -std::pair parse_while(parse_context ct); +std::pair parse_block(parse_context ct); +std::pair parse_cmd(parse_context ct); +std::pair parse_function(parse_context ct, const char* after="()"); +std::pair parse_subshell(parse_context ct); +std::pair parse_brace(parse_context ct); +std::pair parse_case(parse_context ct); +std::pair parse_if(parse_context ct); +std::pair parse_for(parse_context ct); +std::pair parse_while(parse_context ct); // pipeline parser -std::pair parse_pipeline(parse_context ct); +std::pair parse_pipeline(parse_context ct); // condlist parser -std::pair parse_condlist(parse_context ct); +std::pair parse_condlist(parse_context ct); #endif //PARSE_HPP diff --git a/include/processing.hpp b/include/processing.hpp index 26b0f12..bed76b9 100644 --- a/include/processing.hpp +++ b/include/processing.hpp @@ -60,7 +60,7 @@ std::regex fct_exclude_regex(std::string const& in); // varnames bool is_varname(std::string const& in); std::string get_varname(std::string const& in); -std::string get_varname(arg* in); +std::string get_varname(arg_t* in); // list objects void list_map(countmap_t const& map); diff --git a/include/recursive.hpp b/include/recursive.hpp index 8683c10..e29a380 100644 --- a/include/recursive.hpp +++ b/include/recursive.hpp @@ -19,59 +19,59 @@ void recurse(bool (&fct)(_obj*, Args...), _obj* o, Args... args) // recursive calls switch(o->type) { - case _obj::_variable : + case _obj::variable : { - variable* t = dynamic_cast(o); + variable_t* t = dynamic_cast(o); recurse(fct, t->index, args...); recurse(fct, t->manip, args...); break; } - case _obj::_redirect : + case _obj::redirect : { - redirect* t = dynamic_cast(o); + redirect_t* t = dynamic_cast(o); recurse(fct, t->target, args...); recurse(fct, t->here_document, args...); break; } - case _obj::_arg : + case _obj::arg : { - arg* t = dynamic_cast(o); + arg_t* t = dynamic_cast(o); for(auto it: t->sa) { recurse(fct, it, args...); } break; } - case _obj::_arglist : + case _obj::arglist : { - arglist* t = dynamic_cast(o); + arglist_t* t = dynamic_cast(o); for(auto it: t->args) { recurse(fct, it, args...); } break; } - case _obj::_pipeline : + case _obj::pipeline : { - pipeline* t = dynamic_cast(o); + pipeline_t* t = dynamic_cast(o); for(auto it: t->cmds) { recurse(fct, it, args...); } break; } - case _obj::_condlist : + case _obj::condlist : { - condlist* t = dynamic_cast(o); + condlist_t* t = dynamic_cast(o); for(auto it: t->pls) { recurse(fct, it, args...); } break; } - case _obj::_list : + case _obj::list : { - list* t = dynamic_cast(o); + list_t* t = dynamic_cast(o); for(auto it: t->cls) { recurse(fct, it, args...); @@ -80,7 +80,7 @@ void recurse(bool (&fct)(_obj*, Args...), _obj* o, Args... args) } case _obj::block_subshell : { - subshell* t = dynamic_cast(o); + subshell_t* t = dynamic_cast(o); recurse(fct, t->lst, args...); for(auto it: t->redirs) @@ -90,7 +90,7 @@ void recurse(bool (&fct)(_obj*, Args...), _obj* o, Args... args) } case _obj::block_brace : { - brace* t = dynamic_cast(o); + brace_t* t = dynamic_cast(o); recurse(fct, t->lst, args...); for(auto it: t->redirs) @@ -110,7 +110,7 @@ void recurse(bool (&fct)(_obj*, Args...), _obj* o, Args... args) } case _obj::block_function : { - function* t = dynamic_cast(o); + function_t* t = dynamic_cast(o); recurse(fct, t->lst, args...); for(auto it: t->redirs) @@ -120,7 +120,7 @@ void recurse(bool (&fct)(_obj*, Args...), _obj* o, Args... args) } case _obj::block_cmd : { - cmd* t = dynamic_cast(o); + cmd_t* t = dynamic_cast(o); recurse(fct, t->args, args...); for(auto it: t->var_assigns) { @@ -140,7 +140,7 @@ void recurse(bool (&fct)(_obj*, Args...), _obj* o, Args... args) } case _obj::block_case : { - case_block* t = dynamic_cast(o); + case_t* t = dynamic_cast(o); // carg recurse(fct, t->carg, args...); // cases @@ -160,7 +160,7 @@ void recurse(bool (&fct)(_obj*, Args...), _obj* o, Args... args) } case _obj::block_if : { - if_block* t = dynamic_cast(o); + if_t* t = dynamic_cast(o); // ifs for(auto sc: t->blocks) { @@ -179,7 +179,7 @@ void recurse(bool (&fct)(_obj*, Args...), _obj* o, Args... args) } case _obj::block_for : { - for_block* t = dynamic_cast(o); + for_t* t = dynamic_cast(o); // variable recurse(fct, t->var, args...); // iterations @@ -194,7 +194,7 @@ void recurse(bool (&fct)(_obj*, Args...), _obj* o, Args... args) } case _obj::block_while : { - while_block* t = dynamic_cast(o); + while_t* t = dynamic_cast(o); // condition recurse(fct, t->cond, args...); // operations @@ -207,50 +207,50 @@ void recurse(bool (&fct)(_obj*, Args...), _obj* o, Args... args) } case _obj::subarg_variable : { - variable_subarg* t = dynamic_cast(o); + subarg_variable_t* t = dynamic_cast(o); recurse(fct, t->var, args...); break; } case _obj::subarg_subshell : { - subshell_subarg* t = dynamic_cast(o); + subarg_subshell_t* t = dynamic_cast(o); recurse(fct, t->sbsh, args...); break; } case _obj::subarg_procsub : { - procsub_subarg* t = dynamic_cast(o); + subarg_procsub_t* t = dynamic_cast(o); recurse(fct, t->sbsh, args...); break; } case _obj::subarg_arithmetic : { - arithmetic_subarg* t = dynamic_cast(o); + subarg_arithmetic_t* t = dynamic_cast(o); recurse(fct, t->arith, args...); break; } case _obj::arithmetic_variable : { - variable_arithmetic* t = dynamic_cast(o); + arithmetic_variable_t* t = dynamic_cast(o); recurse(fct, t->var, args...); break; } case _obj::arithmetic_subshell : { - subshell_arithmetic* t = dynamic_cast(o); + arithmetic_subshell_t* t = dynamic_cast(o); recurse(fct, t->sbsh, args...); break; } case _obj::arithmetic_operation : { - operation_arithmetic* t = dynamic_cast(o); + arithmetic_operation_t* t = dynamic_cast(o); recurse(fct, t->val1, args...); recurse(fct, t->val2, args...); break; } case _obj::arithmetic_parenthesis : { - parenthesis_arithmetic* t = dynamic_cast(o); + arithmetic_parenthesis_t* t = dynamic_cast(o); recurse(fct, t->val, args...); break; } diff --git a/include/resolve.hpp b/include/resolve.hpp index 360506a..d479e2f 100644 --- a/include/resolve.hpp +++ b/include/resolve.hpp @@ -6,8 +6,8 @@ extern std::vector included; -std::vector> do_include_raw(condlist* cmd, parse_context ctx, std::string* ex_dir=nullptr); -std::pair do_resolve_raw(condlist* cmd, parse_context ctx, std::string* ex_dir=nullptr); +std::vector> do_include_raw(condlist_t* cmd, parse_context ctx, std::string* ex_dir=nullptr); +std::pair do_resolve_raw(condlist_t* cmd, parse_context ctx, std::string* ex_dir=nullptr); bool add_include(std::string const& file); diff --git a/include/struc.hpp b/include/struc.hpp index 70d7062..f0e195a 100644 --- a/include/struc.hpp +++ b/include/struc.hpp @@ -67,13 +67,13 @@ subarg: can be one of #define AND_OP false #define OR_OP true -class condlist; -class block; -class pipeline; -class arg; -class subarg; -class cmd; -class redirect; +class condlist_t; +class block_t; +class pipeline_t; +class arg_t; +class subarg_t; +class cmd_t; +class redirect_t; // structs @@ -88,12 +88,12 @@ struct parse_context { const char* here_doc=""; const char operator[](uint64_t a) { return data[a]; } bool has_errored=false; - redirect* here_document=nullptr; + redirect_t* here_document=nullptr; char* here_delimitor=NULL; }; struct generate_context { - arg* here_document=nullptr; + arg_t* here_document=nullptr; }; // exceptions @@ -124,24 +124,19 @@ private: // objects -// type pack of condlist -typedef std::vector arglist_t; - -extern std::string g_origin; - // meta object type class _obj { public: enum _objtype { subarg_string, subarg_variable, subarg_subshell, subarg_arithmetic, subarg_procsub, - _variable, - _redirect, - _arg, - _arglist, - _pipeline, - _condlist, - _list, + variable, + redirect, + arg, + arglist, + pipeline, + condlist, + list, arithmetic_operation, arithmetic_number, arithmetic_variable, arithmetic_parenthesis, arithmetic_subshell, block_subshell, block_brace, block_main, block_cmd, block_function, block_case, block_if, block_for, block_while }; _objtype type; @@ -151,41 +146,41 @@ public: }; // meta arithmetic type -class arithmetic : public _obj +class arithmetic_t : public _obj { public: virtual std::string generate(int ind)=0; }; // meta subarg type -class subarg : public _obj +class subarg_t : public _obj { public: - virtual ~subarg() {;} + virtual ~subarg_t() {;} virtual std::string generate(int ind)=0; bool quoted; }; -class arg : public _obj +class arg_t : public _obj { public: - arg() { type=_obj::_arg; forcequoted=false; } - arg(std::string const& str, bool fquote=false) { type=_obj::_arg; this->set(str); forcequoted=fquote; } - arg(subarg* in, bool fquote=false) { type=_obj::_arg; sa.push_back(in); forcequoted=fquote; } - ~arg() { for(auto it: sa) delete it; } + arg_t() { type=_obj::arg; forcequoted=false; } + arg_t(std::string const& str, bool fquote=false) { type=_obj::arg; this->set(str); forcequoted=fquote; } + arg_t(subarg_t* in, bool fquote=false) { type=_obj::arg; sa.push_back(in); forcequoted=fquote; } + ~arg_t() { for(auto it: sa) delete it; } void set(std::string const& str); - void insert(uint32_t i, subarg* val); - void insert(uint32_t i, arg const& a); + void insert(uint32_t i, subarg_t* val); + void insert(uint32_t i, arg_t const& a); void insert(uint32_t i, std::string const& in); - inline void add(subarg* in) { sa.push_back(in); } + inline void add(subarg_t* in) { sa.push_back(in); } void add(std::string const& in); inline size_t size() { return sa.size(); } - std::vector sa; + std::vector sa; // is forcequoted: var assign bool forcequoted; @@ -204,59 +199,59 @@ public: std::string generate(int ind); }; -class variable : public _obj +class variable_t : public _obj { public: - variable(std::string const& in="", arg* i=nullptr, bool def=false, bool ismanip=false, arg* m=nullptr) { type=_obj::_variable; varname=in; index=i; definition=def; is_manip=ismanip; precedence=false; manip=m; } - ~variable() { + variable_t(std::string const& in="", arg_t* i=nullptr, bool def=false, bool ismanip=false, arg_t* m=nullptr) { type=_obj::variable; varname=in; index=i; definition=def; is_manip=ismanip; precedence=false; manip=m; } + ~variable_t() { if(index!=nullptr) delete index; if(manip!=nullptr) delete manip; } std::string varname; bool definition; - arg* index; // for bash specific + arg_t* index; // for bash specific bool is_manip; bool precedence; - arg* manip; + arg_t* manip; std::string generate(int ind); }; // arglist -class arglist : public _obj +class arglist_t : 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); } + arglist_t() { type=_obj::arglist; } + arglist_t(arg_t* in) { type=_obj::arglist; this->add(in); } + ~arglist_t() { for( auto it: args ) delete it; } + inline void add(arg_t* in) { args.push_back(in); } - std::vector args; + std::vector args; std::vector strargs(uint32_t start); // potentially expands into more arguments than its size bool can_expand(); - void insert(uint32_t i, arg* val); - void insert(uint32_t i, arglist const& lst); + void insert(uint32_t i, arg_t* val); + void insert(uint32_t i, arglist_t const& lst); inline size_t size() { return args.size(); } std::string generate(int ind); }; -class redirect : public _obj +class redirect_t : public _obj { public: - redirect(std::string strop="") { type=_obj::_redirect; op=strop; target=nullptr; here_document=nullptr; } - redirect(arg* in) { type=_obj::_redirect; target=in; here_document=nullptr; } - redirect(std::string strop, arg* in) { type=_obj::_redirect; op=strop; target=in; here_document=nullptr; } - redirect(std::string strop, arg* in, arg* doc) { type=_obj::_redirect; op=strop; target=in; here_document=doc; } - ~redirect() { + redirect_t(std::string strop="") { type=_obj::redirect; op=strop; target=nullptr; here_document=nullptr; } + redirect_t(arg_t* in) { type=_obj::redirect; target=in; here_document=nullptr; } + redirect_t(std::string strop, arg_t* in) { type=_obj::redirect; op=strop; target=in; here_document=nullptr; } + redirect_t(std::string strop, arg_t* in, arg_t* doc) { type=_obj::redirect; op=strop; target=in; here_document=doc; } + ~redirect_t() { if(target != nullptr) delete target; if(here_document != nullptr) delete here_document; } @@ -264,21 +259,21 @@ public: std::string generate(int ind); std::string op; - arg* target; - arg* here_document; + arg_t* target; + arg_t* here_document; }; // Meta block -class block : public _obj +class block_t : public _obj { public: - block() { ; } - virtual ~block() { for(auto it: redirs) delete it; } + block_t() { ; } + virtual ~block_t() { for(auto it: redirs) delete it; } // cmd - std::vector redirs; + std::vector redirs; // subshell: return the containing cmd, if it is a single command - cmd* single_cmd(); + cmd_t* single_cmd(); std::string generate_redirs(int ind, std::string const& _str, generate_context* ctx); @@ -287,13 +282,13 @@ public: // PL -class pipeline : public _obj +class pipeline_t : public _obj { public: - pipeline(block* bl=nullptr) { type=_obj::_pipeline; if(bl!=nullptr) cmds.push_back(bl); negated=false; } - ~pipeline() { for(auto it: cmds) delete it; } - inline void add(block* bl) { this->cmds.push_back(bl); } - std::vector cmds; + pipeline_t(block_t* bl=nullptr) { type=_obj::pipeline; if(bl!=nullptr) cmds.push_back(bl); negated=false; } + ~pipeline_t() { for(auto it: cmds) delete it; } + inline void add(block_t* bl) { this->cmds.push_back(bl); } + std::vector cmds; bool negated; // negated return value (! at start) @@ -303,49 +298,49 @@ public: // CL -class condlist : public _obj +class condlist_t : public _obj { public: - condlist() { type=_obj::_condlist; parallel=false; } - condlist(pipeline* pl) { type=_obj::_condlist; parallel=false; this->add(pl); } - condlist(block* bl); - ~condlist() { for(auto it: pls) delete it; } + condlist_t() { type=_obj::condlist; parallel=false; } + condlist_t(pipeline_t* pl) { type=_obj::condlist; parallel=false; this->add(pl); } + condlist_t(block_t* bl); + ~condlist_t() { for(auto it: pls) delete it; } bool parallel; // & at the end - void add(pipeline* pl, bool or_op=false); + void add(pipeline_t* pl, bool or_op=false); // don't push_back here, use add() instead - std::vector pls; + std::vector pls; std::vector or_ops; // size of 1 less than pls, defines separator between pipelines void prune_first_cmd(); - block* first_block(); - cmd* first_cmd(); - cmd* get_cmd(std::string const& cmdname); + block_t* first_block(); + cmd_t* first_cmd(); + cmd_t* get_cmd(std::string const& cmdname); void negate(); std::string generate(int ind); }; -class list : public _obj +class list_t : public _obj { public: - list() { type=_obj::_list; } - list(condlist* in) { type=_obj::_list; this->add(in); } - ~list() { for(auto it: cls) delete it; } + list_t() { type=_obj::list; } + list_t(condlist_t* in) { type=_obj::list; this->add(in); } + ~list_t() { for(auto it: cls) delete it; } void clear() { for(auto it: cls) delete it; cls.resize(0); } - std::vector cls; - inline void add(condlist* in) { cls.push_back(in); } + std::vector cls; + inline void add(condlist_t* in) { cls.push_back(in); } - condlist* last_cond() { return cls[cls.size()-1]; } + condlist_t* last_cond() { return cls[cls.size()-1]; } - void insert(uint32_t i, condlist* val); - void insert(uint32_t i, list const& lst); + void insert(uint32_t i, condlist_t* val); + void insert(uint32_t i, list_t const& lst); size_t size() { return cls.size(); } @@ -353,22 +348,13 @@ public: std::string generate(int ind) { return this->generate(ind, true); } }; -// class redir -// { -// public: -// enum redirtype { none, write, append, read, raw } ; -// redir(redirtype in=none) { type=in; } -// redirtype type; -// arg val; -// }; - // block subtypes // -class cmd : public block +class cmd_t : public block_t { public: - cmd(arglist* in=nullptr) { type=_obj::block_cmd; args=in; is_cmdvar=false; } - ~cmd() { + cmd_t(arglist_t* in=nullptr) { type=_obj::block_cmd; args=in; is_cmdvar=false; } + ~cmd_t() { if(args!=nullptr) delete args; for(auto it: var_assigns) { delete it.first; @@ -386,36 +372,36 @@ public: size_t arglist_size(); - void add(arg* in); + void add(arg_t* in); // preceding var assigns - std::vector> var_assigns; + std::vector> var_assigns; // is a cmdvar type bool is_cmdvar; // var assigns on cmdvar - std::vector> cmd_var_assigns; + std::vector> cmd_var_assigns; // 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(); - std::vector subarg_vars(); + std::vector subarg_vars(); // returns true if command performs env var changes bool has_var_assign(); - arglist* args; + arglist_t* args; std::string generate(int ind, generate_context* ctx); std::string generate(int ind) { return this->generate(ind, nullptr); } }; -class shmain : public block +class shmain : public block_t { public: - shmain(list* in=nullptr) { type=_obj::block_main; lst=in; } + shmain(list_t* in=nullptr) { type=_obj::block_main; if(in == nullptr) lst = new list_t; else lst=in; } ~shmain() { if(lst!=nullptr) delete lst; } @@ -424,66 +410,66 @@ public: std::string filename; std::string shebang; - list* lst; + list_t* lst; std::string generate(bool print_shebang=true, int ind=0); std::string generate(int ind, generate_context* ctx); std::string generate(int ind) { return this->generate(ind, nullptr); } }; -class subshell : public block +class subshell_t : public block_t { public: - subshell(list* in=nullptr) { type=_obj::block_subshell; lst=in; } - subshell(block* in) { type=_obj::block_subshell; lst=new list(new condlist(in)); } - ~subshell() { + subshell_t(list_t* in=nullptr) { type=_obj::block_subshell; lst=in; } + subshell_t(block_t* in) { type=_obj::block_subshell; lst=new list_t(new condlist_t(in)); } + ~subshell_t() { if(lst!=nullptr) delete lst; } - cmd* single_cmd(); + cmd_t* single_cmd(); - list* lst; + list_t* lst; std::string generate(int ind, generate_context* ctx); std::string generate(int ind) { return this->generate(ind, nullptr); } }; -class brace : public block +class brace_t : public block_t { public: - brace(list* in=nullptr) { type=_obj::block_brace; lst=in; } - ~brace() { + brace_t(list_t* in=nullptr) { type=_obj::block_brace; lst=in; } + ~brace_t() { if(lst!=nullptr) delete lst; } - cmd* single_cmd(); + cmd_t* single_cmd(); - list* lst; + list_t* lst; std::string generate(int ind, generate_context* ctx); std::string generate(int ind) { return this->generate(ind, nullptr); } }; -class function : public block +class function_t : public block_t { public: - function(list* in=nullptr) { type=_obj::block_function; lst=in; } - ~function() { + function_t(list_t* in=nullptr) { type=_obj::block_function; lst=in; } + ~function_t() { if(lst!=nullptr) delete lst; } std::string name; - list* lst; + list_t* lst; std::string generate(int ind, generate_context* ctx); std::string generate(int ind) { return this->generate(ind, nullptr); } }; -class case_block : public block +class case_t : public block_t { public: - case_block(arg* in=nullptr) { type=_obj::block_case; carg=in; } - ~case_block() { + case_t(arg_t* in=nullptr) { type=_obj::block_case; carg=in; } + ~case_t() { if(carg!=nullptr) delete carg; for( auto cit : cases ) { @@ -493,18 +479,18 @@ public: } } - arg* carg; - std::vector< std::pair, list*> > cases; + arg_t* carg; + std::vector< std::pair, list_t*> > cases; std::string generate(int ind, generate_context* ctx); std::string generate(int ind) { return this->generate(ind, nullptr); } }; -class if_block : public block +class if_t : public block_t { public: - if_block() { type=_obj::block_if; else_lst=nullptr; } - ~if_block() { + if_t() { type=_obj::block_if; else_lst=nullptr; } + ~if_t() { for(auto ifb: blocks) { if(ifb.first!=nullptr) delete ifb.first; @@ -513,28 +499,28 @@ public: if(else_lst!=nullptr) delete else_lst; } - std::vector< std::pair > blocks; + std::vector< std::pair > blocks; - list* else_lst; + list_t* else_lst; std::string generate(int ind, generate_context* ctx); std::string generate(int ind) { return this->generate(ind, nullptr); } }; -class for_block : public block +class for_t : public block_t { public: - for_block(variable* in=nullptr, arglist* args=nullptr, list* lst=nullptr, bool ii=false) { type=_obj::block_for; var=in; iter=args; ops=lst; in_val=ii; } - ~for_block() { + for_t(variable_t* in=nullptr, arglist_t* args=nullptr, list_t* lst=nullptr, bool ii=false) { type=_obj::block_for; var=in; iter=args; ops=lst; in_val=ii; } + ~for_t() { if(iter!=nullptr) delete iter; if(ops!=nullptr) delete ops; if(var!=nullptr) delete var; } - variable* var; + variable_t* var; - arglist* iter; - list* ops; + arglist_t* iter; + list_t* ops; bool in_val; @@ -542,19 +528,19 @@ public: std::string generate(int ind) { return this->generate(ind, nullptr); } }; -class while_block : public block +class while_t : public block_t { public: - while_block(list* a=nullptr, list* b=nullptr) { type=_obj::block_while; cond=a; ops=b; } - ~while_block() { + while_t(list_t* a=nullptr, list_t* b=nullptr) { type=_obj::block_while; cond=a; ops=b; } + ~while_t() { if(cond!=nullptr) delete cond; if(ops!=nullptr) delete ops; } - condlist* real_condition() { return cond->last_cond(); } + condlist_t* real_condition() { return cond->last_cond(); } - list* cond; - list* ops; + list_t* cond; + list_t* ops; std::string generate(int ind, generate_context* ctx); std::string generate(int ind) { return this->generate(ind, nullptr); } @@ -562,130 +548,130 @@ public: // Subarg subtypes // -class string_subarg : public subarg +class subarg_string_t : public subarg_t { public: - string_subarg(std::string const& in="") { type=_obj::subarg_string; val=in; } - ~string_subarg() {;} + subarg_string_t(std::string const& in="") { type=_obj::subarg_string; val=in; } + ~subarg_string_t() {;} std::string val; std::string generate(int ind) { return val; } }; -class variable_subarg : public subarg +class subarg_variable_t : public subarg_t { public: - variable_subarg(variable* in=nullptr, bool inq=false) { type=_obj::subarg_variable; var=in; quoted=inq; } - ~variable_subarg() { + subarg_variable_t(variable_t* in=nullptr, bool inq=false) { type=_obj::subarg_variable; var=in; quoted=inq; } + ~subarg_variable_t() { if(var!=nullptr) delete var; } - variable* var; + variable_t* var; std::string generate(int ind) { return "$" + var->generate(ind); } }; -class arithmetic_subarg : public subarg +class subarg_arithmetic_t : public subarg_t { public: - arithmetic_subarg(arithmetic* a=nullptr) { type=_obj::subarg_arithmetic; arith=a; } - ~arithmetic_subarg() { + subarg_arithmetic_t(arithmetic_t* a=nullptr) { type=_obj::subarg_arithmetic; arith=a; } + ~subarg_arithmetic_t() { if(arith!=nullptr) delete arith; } - arithmetic* arith; + arithmetic_t* arith; std::string generate(int ind); }; -class subshell_subarg : public subarg +class subarg_subshell_t : public subarg_t { public: - subshell_subarg(subshell* in=nullptr, bool inq=false, bool is_backtick=false) { type=_obj::subarg_subshell; sbsh=in; quoted=inq; backtick=is_backtick; } - ~subshell_subarg() { if(sbsh != nullptr) delete sbsh; } + subarg_subshell_t(subshell_t* in=nullptr, bool inq=false, bool is_backtick=false) { type=_obj::subarg_subshell; sbsh=in; quoted=inq; backtick=is_backtick; } + ~subarg_subshell_t() { if(sbsh != nullptr) delete sbsh; } - subshell* sbsh; + subshell_t* sbsh; bool backtick; std::string generate(int ind); }; -class procsub_subarg : public subarg +class subarg_procsub_t : public subarg_t { public: - procsub_subarg() { type=_obj::subarg_procsub; sbsh=nullptr; is_output=false; } - procsub_subarg(bool output, subshell* in) { type=_obj::subarg_procsub; sbsh=in; is_output=output; } - ~procsub_subarg() { if(sbsh!=nullptr) delete sbsh; } + subarg_procsub_t() { type=_obj::subarg_procsub; sbsh=nullptr; is_output=false; } + subarg_procsub_t(bool output, subshell_t* in) { type=_obj::subarg_procsub; sbsh=in; is_output=output; } + ~subarg_procsub_t() { if(sbsh!=nullptr) delete sbsh; } bool is_output; - subshell* sbsh; + subshell_t* sbsh; std::string generate(int ind); }; // Arithmetic subtypes // -class operation_arithmetic : public arithmetic +class arithmetic_operation_t : public arithmetic_t { public: - operation_arithmetic(std::string op="", arithmetic* a=nullptr, arithmetic* b=nullptr, bool pre=false) { type=_obj::arithmetic_operation; oper=op; val1=a; val2=b; precedence=pre; } - ~operation_arithmetic() { + arithmetic_operation_t(std::string op="", arithmetic_t* a=nullptr, arithmetic_t* b=nullptr, bool pre=false) { type=_obj::arithmetic_operation; oper=op; val1=a; val2=b; precedence=pre; } + ~arithmetic_operation_t() { if(val1 != nullptr) delete val1; if(val2 != nullptr) delete val2; } std::string oper; bool precedence; - arithmetic *val1, *val2; + arithmetic_t *val1, *val2; std::string generate(int ind); }; -class subshell_arithmetic : public arithmetic +class arithmetic_subshell_t : public arithmetic_t { public: - subshell_arithmetic(subshell* a=nullptr) { type=_obj::arithmetic_subshell; sbsh=a; } - ~subshell_arithmetic() { + arithmetic_subshell_t(subshell_t* a=nullptr) { type=_obj::arithmetic_subshell; sbsh=a; } + ~arithmetic_subshell_t() { if(sbsh!=nullptr) delete sbsh; } - subshell* sbsh; + subshell_t* sbsh; std::string generate(int ind); }; -class parenthesis_arithmetic : public arithmetic +class arithmetic_parenthesis_t : public arithmetic_t { public: - parenthesis_arithmetic(arithmetic* a=nullptr) { type=_obj::arithmetic_parenthesis; val=a; } - ~parenthesis_arithmetic() { + arithmetic_parenthesis_t(arithmetic_t* a=nullptr) { type=_obj::arithmetic_parenthesis; val=a; } + ~arithmetic_parenthesis_t() { if(val!=nullptr) delete val; } - arithmetic* val; + arithmetic_t* val; std::string generate(int ind); }; -class number_arithmetic : public arithmetic +class arithmetic_number_t : public arithmetic_t { public: - number_arithmetic(std::string const& a) { type=_obj::arithmetic_number; val=a; } + arithmetic_number_t(std::string const& a) { type=_obj::arithmetic_number; val=a; } std::string val; std::string generate(int ind) { return val; } }; -class variable_arithmetic : public arithmetic +class arithmetic_variable_t : public arithmetic_t { public: - variable_arithmetic(variable* in=nullptr) { type=_obj::arithmetic_variable; var=in; } - ~variable_arithmetic() { + arithmetic_variable_t(variable_t* in=nullptr) { type=_obj::arithmetic_variable; var=in; } + ~arithmetic_variable_t() { if(var!=nullptr) delete var; } - variable* var; + variable_t* var; std::string generate(int ind); }; diff --git a/include/struc_helper.hpp b/include/struc_helper.hpp index c453904..ddbc5b8 100644 --- a/include/struc_helper.hpp +++ b/include/struc_helper.hpp @@ -4,43 +4,43 @@ #include "struc.hpp" // makers -arg* make_arg(std::string const& in); +arg_t* make_arg(std::string const& in); -cmd* make_cmd(std::vector const& args); -cmd* make_cmd(std::vector const& args); -cmd* make_cmd(std::vector const& args); -cmd* make_cmd(std::string const& in); +cmd_t* make_cmd(std::vector const& args); +cmd_t* make_cmd(std::vector const& args); +cmd_t* make_cmd(std::vector const& args); +cmd_t* make_cmd(std::string const& in); -pipeline* make_pipeline(std::vector const& bls); -pipeline* make_pipeline(std::string const& in); +pipeline_t* make_pipeline(std::vector const& bls); +pipeline_t* make_pipeline(std::string const& in); -condlist* make_condlist(std::string const& in); -list* make_list(std::string const& in); +condlist_t* make_condlist(std::string const& in); +list_t* make_list(std::string const& in); -block* make_block(std::string const& in); +block_t* make_block(std::string const& in); // copy -arg* copy(arg* in); -variable* copy(variable* in); +arg_t* copy(arg_t* in); +variable_t* copy(variable_t* in); // testers -bool arg_has_char(char c, arg* in); -bool possibly_expands(arg* in); -bool possibly_expands(arglist* in); +bool arg_has_char(char c, arg_t* in); +bool possibly_expands(arg_t* in); +bool possibly_expands(arglist_t* in); // modifiers -void force_quotes(arg* in); -void add_quotes(arg* in); +void force_quotes(arg_t* in); +void add_quotes(arg_t* 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)))); +cmd_t* make_printf(arg_t* in); +inline cmd_t* make_printf_variable(std::string const& name) { + return make_printf(new arg_t(new subarg_variable_t(new variable_t(name)))); } -arithmetic* make_arithmetic(arg* a); -arithmetic* make_arithmetic(arg* arg1, std::string op, arg* arg2); +arithmetic_t* make_arithmetic(arg_t* a); +arithmetic_t* make_arithmetic(arg_t* arg1, std::string op, arg_t* arg2); // operators -inline bool operator==(arg a, std::string const& b) { return a.equals(b); } +inline bool operator==(arg_t a, std::string const& b) { return a.equals(b); } #endif //STRUC_HELPER_HPP diff --git a/src/debashify.cpp b/src/debashify.cpp index d8a9ba6..aa9e8e3 100644 --- a/src/debashify.cpp +++ b/src/debashify.cpp @@ -22,9 +22,9 @@ EXPRESSION : gen_bashtest_cmd // [[ a == b ]] : replace == with = // [[ a = b* ]] : case a in b*) true;; *) false;; esac // [[ a =~ b ]] : expr a : "b" >/dev/null -block* gen_bashtest_cmd(std::vector args) +block_t* gen_bashtest_cmd(std::vector args) { - block* ret = nullptr; + block_t* ret = nullptr; std::string arg1replace; if(args.size() == 3) @@ -43,7 +43,7 @@ block* gen_bashtest_cmd(std::vector args) if(arg1replace != "") { delete args[1]; - args[1] = new arg(arg1replace); + args[1] = new arg_t(arg1replace); } if(args.size() == 3 && args[1]->string() == "=" && (arg_has_char('*', args[2]) || arg_has_char('?', args[2])) ) @@ -51,9 +51,9 @@ block* gen_bashtest_cmd(std::vector args) // glob matcher: do a case delete args[1]; args[1]=nullptr; - case_block* tc = new case_block(args[0]); - tc->cases.push_back( std::make_pair(std::vector({args[2]}), make_list("true")) ); - tc->cases.push_back( std::make_pair(std::vector({new arg("*")}), make_list("false")) ); + case_t* tc = new case_t(args[0]); + tc->cases.push_back( std::make_pair(std::vector({args[2]}), make_list("true")) ); + tc->cases.push_back( std::make_pair(std::vector({new arg_t("*")}), make_list("false")) ); ret = tc; } else if(args.size() == 3 && args[1]->string() == "=~") @@ -63,15 +63,15 @@ block* gen_bashtest_cmd(std::vector args) args[1]=nullptr; args[2]->insert(0, ".*"); add_quotes(args[2]); - ret = make_cmd( std::vector({ new arg("expr"), args[0], new arg(":"), args[2] }) ); - ret->redirs.push_back(new redirect(">", new arg("/dev/null") )); + ret = make_cmd( std::vector({ new arg_t("expr"), args[0], new arg_t(":"), args[2] }) ); + ret->redirs.push_back(new redirect_t(">", new arg_t("/dev/null") )); } else { // regular [ ] - cmd* t = make_cmd(args); - t->args->insert(0, new arg("[")); - t->add(new arg("]")); + cmd_t* t = make_cmd(args); + t->args->insert(0, new arg_t("[")); + t->add(new arg_t("]")); ret = t; } // arg oblivious replacements: @@ -85,22 +85,22 @@ block* gen_bashtest_cmd(std::vector args) } // [[ a && b ]] : [ a ] && [ b ] -bool debashify_bashtest(pipeline* pl) +bool debashify_bashtest(pipeline_t* pl) { if(pl->cmds.size()<=0) return false; if(pl->cmds[0]->type != _obj::block_cmd) return false; - cmd* in = dynamic_cast(pl->cmds[0]); + cmd_t* in = dynamic_cast(pl->cmds[0]); if(in->arg_string(0) == "[[") { - brace* br = new brace(new list); - condlist* cl = new condlist; + brace_t* br = new brace_t(new list_t); + condlist_t* cl = new condlist_t; br->lst->add(cl); - arg *a=nullptr; + arg_t* a=nullptr; uint32_t j=1; bool or_op=false; std::string tmpstr; @@ -112,8 +112,8 @@ bool debashify_bashtest(pipeline* pl) if(i >= in->args->size()-1 || logic_op) { - block* tbl = gen_bashtest_cmd(std::vector(in->args->args.begin()+j, in->args->args.begin()+i)); - cl->add(new pipeline(tbl), or_op); + block_t* tbl = gen_bashtest_cmd(std::vector(in->args->args.begin()+j, in->args->args.begin()+i)); + cl->add(new pipeline_t(tbl), or_op); or_op = tmpstr == "||"; j=i+1; if(logic_op) @@ -138,7 +138,7 @@ void warn(std::string const& in) std::cerr << "WARN: " << in << std::endl; } -std::string get_declare_opt(cmd* in) +std::string get_declare_opt(cmd_t* in) { if(in->cmd_var_assigns[0].second!=nullptr) { @@ -157,11 +157,11 @@ ztd::option_set gen_echo_opts() return ret; } -bool debashify_echo(pipeline* pl) +bool debashify_echo(pipeline_t* pl) { if(pl->cmds[0]->type != _obj::block_cmd) return false; - cmd* in = dynamic_cast(pl->cmds[0]); + cmd_t* in = dynamic_cast(pl->cmds[0]); std::string const& cmdstr=in->arg_string(0); if(cmdstr == "echo") @@ -234,9 +234,9 @@ bool debashify_echo(pipeline* pl) format_str += "\\n"; format_str += '\''; - in->args->insert(1, new arg(format_str)); + in->args->insert(1, new arg_t(format_str)); delete in->args->args[0]; - in->args->args[0] = new arg("printf"); + in->args->args[0] = new arg_t("printf"); } else { @@ -246,7 +246,7 @@ bool debashify_echo(pipeline* pl) else format_str = "%s"; - list* lst=nullptr; + list_t* lst=nullptr; // more than 1 arg and first arg can't expand: can split into two printf // printf '%s' arg1 @@ -254,15 +254,15 @@ bool debashify_echo(pipeline* pl) if(in->args->args.size()>2 && !in->args->args[1]->can_expand()) { // extract arg 1 - arg* arg1 = in->args->args[1]; + arg_t* arg1 = in->args->args[1]; in->args->args.erase(in->args->args.begin()+1); delete in->args->args[0]; - in->args->args[0] = new arg("printf"); + in->args->args[0] = new arg_t("printf"); - lst = new list; - lst->add(new condlist(make_cmd({new arg("printf"), new arg(format_str), arg1 }))); - in->args->insert(1, new arg("\\ "+format_str) ); - lst->add(new condlist(in)); + lst = new list_t; + lst->add(new condlist_t(make_cmd({new arg_t("printf"), new arg_t(format_str), arg1 }))); + in->args->insert(1, new arg_t("\\ "+format_str) ); + lst->add(new condlist_t(in)); } else { @@ -270,24 +270,24 @@ bool debashify_echo(pipeline* pl) if(newline) return has_processed_options; - in->args->insert(1, new arg(format_str+"\\ ")); + in->args->insert(1, new arg_t(format_str+"\\ ")); delete in->args->args[0]; - in->args->args[0] = new arg("printf"); + in->args->args[0] = new arg_t("printf"); } if(newline) { if(lst == nullptr) { - lst = new list; - lst->add(new condlist(in)); + lst = new list_t; + lst->add(new condlist_t(in)); } lst->add(make_condlist("echo")); } if(lst != nullptr) { - pl->cmds[0] = new brace(lst); + pl->cmds[0] = new brace_t(lst); } } } @@ -297,7 +297,7 @@ bool debashify_echo(pipeline* pl) return false; } -bool debashify_readonly(list* in) +bool debashify_readonly(list_t* in) { bool has_found=false; for(uint32_t i=0; icls.size(); i++) @@ -306,7 +306,7 @@ bool debashify_readonly(list* in) if(in->cls[i]->pls[0]->cmds[0]->type != _obj::block_cmd) continue; - cmd* c1 = dynamic_cast(in->cls[i]->pls[0]->cmds[0]); + cmd_t* c1 = dynamic_cast(in->cls[i]->pls[0]->cmds[0]); std::string const& cmdstr=c1->arg_string(0); if(cmdstr == "readonly") { @@ -333,7 +333,7 @@ bool debashify_readonly(list* in) else { delete c1->args; - c1->args = new arglist; + c1->args = new arglist_t; c1->var_assigns=c1->cmd_var_assigns; c1->cmd_var_assigns.resize(0); } @@ -344,7 +344,7 @@ bool debashify_readonly(list* in) return has_found; } -bool debashify_declare(list* in, debashify_params* params) +bool debashify_declare(list_t* in, debashify_params* params) { bool has_found=false; for(uint32_t i=0; icls.size(); i++) @@ -353,7 +353,7 @@ bool debashify_declare(list* in, debashify_params* params) if(in->cls[i]->pls[0]->cmds[0]->type != _obj::block_cmd) continue; - cmd* c1 = dynamic_cast(in->cls[i]->pls[0]->cmds[0]); + cmd_t* c1 = dynamic_cast(in->cls[i]->pls[0]->cmds[0]); std::string const& cmdstr=c1->arg_string(0); if(cmdstr == "declare" || cmdstr == "typeset") { @@ -383,11 +383,11 @@ bool debashify_declare(list* in, debashify_params* params) return has_found; } -cmd* make_cmd_varindex(std::string const& strcmd, std::string const& varname, arg* index) +cmd_t* make_cmd_varindex(std::string const& strcmd, std::string const& varname, arg_t* index) { - cmd* c = new cmd(new arglist); + cmd_t* c = new cmd_t(new arglist_t); // cmd - c->args->add( new arg(strcmd) ); + c->args->add( new arg_t(strcmd) ); // cmd "$VAR" c->args->add( make_arg("\"$"+varname+"\"") ); // cmd "$VAR" N @@ -395,22 +395,22 @@ cmd* make_cmd_varindex(std::string const& strcmd, std::string const& varname, ar return c; } -subshell* do_debashify_array_var_get(variable* in, debashify_params* params) +subshell_t* do_debashify_array_var_get(variable_t* in, debashify_params* params) { if(in->manip != nullptr) throw std::runtime_error("Cannot debashify manipulations on ${VAR[]}"); std::string varname = in->varname; - arg* index = in->index; + arg_t* index = in->index; in->index=nullptr; if(index->string() == "*") { delete index; - index = new arg("\\*"); + index = new arg_t("\\*"); } - cmd* c; + cmd_t* c; if(params->arrays[varname]) { c = make_cmd_varindex("_lxsh_map_get", varname, index); @@ -422,34 +422,34 @@ subshell* do_debashify_array_var_get(variable* in, debashify_params* params) params->require_fct("_lxsh_array_get"); } - return new subshell(c); + return new subshell_t(c); } -subshell* do_debashify_random(variable* in, debashify_params* params) +subshell_t* do_debashify_random(variable_t* in, debashify_params* params) { if(in->manip != nullptr) throw std::runtime_error("Cannot debashify manipulations on ${RANDOM}"); - cmd* c = make_cmd("_lxsh_random"); + cmd_t* c = make_cmd("_lxsh_random"); params->require_fct("_lxsh_random"); - return new subshell(c); + return new subshell_t(c); } // does multiple debashifies: // - array // - RANDOM -subshell_arithmetic* do_debashify_arithmetic(arithmetic* in, debashify_params* params) +arithmetic_subshell_t* do_debashify_arithmetic_t(arithmetic_t* in, debashify_params* params) { - subshell_arithmetic* ret = nullptr; + arithmetic_subshell_t* ret = nullptr; if(in->type == _obj::arithmetic_variable) { - variable_arithmetic* t = dynamic_cast(in); + arithmetic_variable_t* t = dynamic_cast(in); if(t->var != nullptr && t->var->varname == "RANDOM") { - ret = new subshell_arithmetic(do_debashify_random(t->var, params)); + ret = new arithmetic_subshell_t(do_debashify_random(t->var, params)); } else if(t->var != nullptr && t->var->index != nullptr) { - ret = new subshell_arithmetic(do_debashify_array_var_get(t->var, params)); + ret = new arithmetic_subshell_t(do_debashify_array_var_get(t->var, params)); } } return ret; @@ -461,8 +461,8 @@ bool debashify_arithmetic_replace(_obj* o, debashify_params* params) switch(o->type) { case _obj::subarg_arithmetic: { - arithmetic_subarg* t = dynamic_cast(o); - arithmetic* r = do_debashify_arithmetic(t->arith, params); + subarg_arithmetic_t* t = dynamic_cast(o); + arithmetic_t* r = do_debashify_arithmetic_t(t->arith, params); if(r!=nullptr) { ret=true; @@ -471,15 +471,15 @@ bool debashify_arithmetic_replace(_obj* o, debashify_params* params) } } break; case _obj::arithmetic_operation: { - operation_arithmetic* t = dynamic_cast(o); - arithmetic* r = do_debashify_arithmetic(t->val1, params); + arithmetic_operation_t* t = dynamic_cast(o); + arithmetic_t* r = do_debashify_arithmetic_t(t->val1, params); if(r!=nullptr) { ret=true; delete t->val1; t->val1 = r; } - r = do_debashify_arithmetic(t->val2, params); + r = do_debashify_arithmetic_t(t->val2, params); if(r!=nullptr) { ret=true; @@ -488,8 +488,8 @@ bool debashify_arithmetic_replace(_obj* o, debashify_params* params) } } break; case _obj::arithmetic_parenthesis: { - parenthesis_arithmetic* t = dynamic_cast(o); - arithmetic* r = do_debashify_arithmetic(t->val, params); + arithmetic_parenthesis_t* t = dynamic_cast(o); + arithmetic_t* r = do_debashify_arithmetic_t(t->val, params); if(r!=nullptr) { ret=true; @@ -502,23 +502,23 @@ bool debashify_arithmetic_replace(_obj* o, debashify_params* params) return ret; } -bool debashify_subarg_replace(arg* in, debashify_params* params) +bool debashify_subarg_replace(arg_t* in, debashify_params* params) { bool has_replaced=false; for(auto it=in->sa.begin() ; it!=in->sa.end() ; it++) { - subarg* replacer=nullptr; + subarg_t* replacer=nullptr; bool quoted=(*it)->quoted; if((*it)->type == _obj::subarg_variable) { - variable_subarg* t = dynamic_cast(*it); + subarg_variable_t* t = dynamic_cast(*it); if(t->var != nullptr && t->var->varname == "RANDOM") { - replacer = new subshell_subarg(do_debashify_random(t->var, params)); + replacer = new subarg_subshell_t(do_debashify_random(t->var, params)); } if(t->var != nullptr && t->var->is_manip && t->var->index != nullptr) { - replacer = new subshell_subarg(do_debashify_array_var_get(t->var, params)); + replacer = new subarg_subshell_t(do_debashify_array_var_get(t->var, params)); } } if(replacer != nullptr) @@ -532,7 +532,7 @@ bool debashify_subarg_replace(arg* in, debashify_params* params) return has_replaced; } -bool debashify_array_set(cmd* in, debashify_params* params) +bool debashify_array_set(cmd_t* in, debashify_params* params) { bool has_replaced=false; for(auto it = in->var_assigns.begin() ; it != in->var_assigns.end() ; it++) @@ -546,23 +546,23 @@ bool debashify_array_set(cmd* in, debashify_params* params) gen=gen.substr(2); gen.pop_back(); // create cmd out of arguments - arglist* args = parse_arglist( make_context(gen) ).first; - cmd* c = new cmd(args); + arglist_t* args = parse_arglist( make_context(gen) ).first; + cmd_t* c = new cmd_t(args); // cmd first argument is _lxsh_X_create if(params->arrays[varname]) { - c->args->insert(0, new arg("_lxsh_map_create") ); + c->args->insert(0, new arg_t("_lxsh_map_create") ); params->require_fct("_lxsh_map_create"); } else { - c->args->insert(0, new arg("_lxsh_array_create") ); + c->args->insert(0, new arg_t("_lxsh_array_create") ); params->require_fct("_lxsh_array_create"); } - subshell_subarg* sb = new subshell_subarg(new subshell(c)); + subarg_subshell_t* sb = new subarg_subshell_t(new subshell_t(c)); // insert new value delete it->second; - it->second = new arg("="); + it->second = new arg_t("="); it->second->add(sb); has_replaced=true; } @@ -571,11 +571,11 @@ bool debashify_array_set(cmd* in, debashify_params* params) // array value set: VAR[]= force_quotes(it->second); force_quotes(it->first->index); - string_subarg* tt=dynamic_cast(it->second->sa[0]); + subarg_string_t* tt=dynamic_cast(it->second->sa[0]); std::string varname = it->first->varname; - arg* index = it->first->index; - arg* value = it->second; + arg_t* index = it->first->index; + arg_t* value = it->second; it->first->index = nullptr; it->second = nullptr; @@ -585,7 +585,7 @@ bool debashify_array_set(cmd* in, debashify_params* params) tt->val = tt->val.substr(2); // remove += // create array get of value - cmd* c; + cmd_t* c; if(params->arrays[varname]) { c = make_cmd_varindex("_lxsh_map_get", varname, copy(index)); @@ -596,7 +596,7 @@ bool debashify_array_set(cmd* in, debashify_params* params) c = make_cmd_varindex("_lxsh_array_get", varname, copy(index)); params->require_fct("_lxsh_array_get"); } - subshell_subarg* sb = new subshell_subarg(new subshell(c)); + subarg_subshell_t* sb = new subarg_subshell_t(new subshell_t(c)); sb->quoted=true; value->insert(0, "\""); value->insert(0, sb); @@ -606,15 +606,15 @@ bool debashify_array_set(cmd* in, debashify_params* params) else tt->val = tt->val.substr(1); // remove = - cmd* c = new cmd(new arglist); + cmd_t* c = new cmd_t(new arglist_t); if(params->arrays[varname]) { - c->args->add(new arg("_lxsh_map_set") ); + c->args->add(new arg_t("_lxsh_map_set") ); params->require_fct("_lxsh_map_set"); } else { - c->args->add(new arg("_lxsh_array_set") ); + c->args->add(new arg_t("_lxsh_array_set") ); params->require_fct("_lxsh_array_set"); } // _lxsh_array_set "$VAR" @@ -624,9 +624,9 @@ bool debashify_array_set(cmd* in, debashify_params* params) // _lxsh_array_set "$VAR" N value c->args->add( value ); // $(_lxsh_array_set "$VAR" N value) - subshell_subarg* sb = new subshell_subarg(new subshell(c)); + subarg_subshell_t* sb = new subarg_subshell_t(new subshell_t(c)); - it->second = new arg("="); + it->second = new arg_t("="); it->second->add(sb); has_replaced=true; } @@ -642,8 +642,8 @@ bool debashify_array_set(cmd* in, debashify_params* params) gen=gen.substr(3); gen.pop_back(); // create cmd out of arguments - arglist* args = parse_arglist( make_context(gen) ).first; - cmd* c = new cmd(args); + arglist_t* args = parse_arglist( make_context(gen) ).first; + cmd_t* c = new cmd_t(args); // cmd first argument is _lxsh_array_create if(params->arrays[varname]) { @@ -651,15 +651,15 @@ bool debashify_array_set(cmd* in, debashify_params* params) } else { - c->args->insert(0, new arg("_lxsh_array_create") ); + c->args->insert(0, new arg_t("_lxsh_array_create") ); params->require_fct("_lxsh_array_create"); } // second arg is varname c->args->insert(1, make_arg("\"$"+varname+"\"") ); - subshell_subarg* sb = new subshell_subarg(new subshell(c)); + subarg_subshell_t* sb = new subarg_subshell_t(new subshell_t(c)); // insert new value delete it->second; - it->second = new arg("="); + it->second = new arg_t("="); it->second->add(sb); has_replaced=true; } @@ -667,18 +667,18 @@ bool debashify_array_set(cmd* in, debashify_params* params) return has_replaced; } -bool debashify_plusequal(cmd* in, debashify_params* params) +bool debashify_plusequal(cmd_t* in, debashify_params* params) { bool has_replaced=false; for(auto it = in->var_assigns.begin() ; it != in->var_assigns.end() ; it++) { if(it->first != nullptr && it->second != nullptr && it->second->first_sa_string().substr(0,2) == "+=") { - string_subarg* tt=dynamic_cast(it->second->sa[0]); - variable* v = new variable(it->first->varname); + subarg_string_t* tt=dynamic_cast(it->second->sa[0]); + variable_t* v = new variable_t(it->first->varname); v->is_manip=true; tt->val = tt->val.substr(2); // remove += - it->second->insert(0, new variable_subarg(v) ); + it->second->insert(0, new subarg_variable_t(v) ); it->second->insert(0, "="); } } @@ -686,17 +686,17 @@ bool debashify_plusequal(cmd* in, debashify_params* params) } // replace <<< foo by printf %s\n "foo" | -bool debashify_herestring(pipeline* pl) +bool debashify_herestring(pipeline_t* pl) { if(pl->cmds.size()>0) { - block* c=pl->cmds[0]; + block_t* 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"); + cmd_t* printcmd = make_cmd("printf %s\\\\n"); printcmd->add(c->redirs[i]->target); pl->cmds.insert(pl->cmds.begin(), printcmd); @@ -714,7 +714,7 @@ bool debashify_herestring(pipeline* pl) // replace &>, &>> and >&: // add 2>&1 as redirect -bool debashify_combined_redirects(block* in) +bool debashify_combined_redirects(block_t* in) { bool has_replaced=false; @@ -727,14 +727,14 @@ bool debashify_combined_redirects(block* in) if( in->redirs[i]->op == "&>>" ) newop = ">>"; // create new redir with target - redirect* newredir = new redirect(newop, in->redirs[i]->target); + redirect_t* newredir = new redirect_t(newop, in->redirs[i]->target); in->redirs[i]->target=nullptr; // replace old redir delete in->redirs[i]; in->redirs[i] = newredir; // insert merge redir i++; - in->redirs.insert(in->redirs.begin()+i, new redirect("2>&1")); + in->redirs.insert(in->redirs.begin()+i, new redirect_t("2>&1")); has_replaced=true; } @@ -751,12 +751,12 @@ REPLACE TO: ( {PSUB;} [>|<] "$fifoN" ; rm "$fifoN") & CMD "$fifoN" */ -bool debashify_procsub(list* lst, debashify_params* params) +bool debashify_procsub(list_t* lst, debashify_params* params) { bool has_replaced=false; for(uint32_t li=0; licls.size(); li++) { - std::vector> affected_args; + std::vector> affected_args; // iterate all applicable args of the cl for(auto plit: lst->cls[li]->pls) { @@ -764,14 +764,14 @@ bool debashify_procsub(list* lst, debashify_params* params) { if(cmit->type == _obj::block_cmd) { - cmd* t = dynamic_cast(cmit); + cmd_t* t = dynamic_cast(cmit); if(t->args != nullptr) { for(auto ait: t->args->args) { if(ait->size() == 1 && ait->sa[0]->type == _obj::subarg_procsub) { - procsub_subarg* st = dynamic_cast(ait->sa[0]); + subarg_procsub_t* st = dynamic_cast(ait->sa[0]); affected_args.push_back( std::make_pair(ait, st->is_output) ); } } @@ -784,7 +784,7 @@ bool debashify_procsub(list* lst, debashify_params* params) { params->require_fct("_lxsh_random_tmpfile"); has_replaced=true; - list* lst_insert = new list; + list_t* lst_insert = new list_t; std::string mkfifocmd="mkfifo"; for(uint32_t i=0; i "$fifoN" ; rm "$fifoN") & - subshell* psub = new subshell(new list); - procsub_subarg* st = dynamic_cast(affected_args[i].first->sa[0]); + subshell_t* psub = new subshell_t(new list_t); + subarg_procsub_t* st = dynamic_cast(affected_args[i].first->sa[0]); // {PSUB;} - brace* cbr = new brace(st->sbsh->lst); + brace_t* cbr = new brace_t(st->sbsh->lst); // deindex list for delete st->sbsh->lst=nullptr; // {PSUB;} > "$_lxshfifoN" - cbr->redirs.push_back( new redirect( affected_args[i].second ? "<" : ">", make_arg(strf("\"$_lxshfifo%u\"", i)) ) ); + cbr->redirs.push_back( new redirect_t( affected_args[i].second ? "<" : ">", make_arg(strf("\"$_lxshfifo%u\"", i)) ) ); // ( {PSUB;} > "$_lxshfifoN" ) - psub->lst->add( new condlist(cbr) ); + psub->lst->add( new condlist_t(cbr) ); // ( {PSUB;} > "$_lxshfifoN" ; rm "$_lxshfifoN" ) psub->lst->add( make_condlist(strf("rm \"$_lxshfifo%u\"", i)) ); // ( {PSUB;} > "$_lxshfifoN" ; rm "$_lxshfifoN" ) & - condlist* pscl = new condlist(psub); + condlist_t* pscl = new condlist_t(psub); pscl->parallel=true; lst_insert->add( pscl ); // replace the arg delete affected_args[i].first->sa[0]; - affected_args[i].first->sa[0] = new string_subarg("\""); - affected_args[i].first->add( new variable_subarg( new variable(strf("_lxshfifo%u", i)) ) ); + affected_args[i].first->sa[0] = new subarg_string_t("\""); + affected_args[i].first->add( new subarg_variable_t( new variable_t(strf("_lxshfifo%u", i)) ) ); affected_args[i].first->add( "\"" ); } lst->insert(li, *lst_insert ); @@ -830,9 +830,9 @@ bool debashify_procsub(list* lst, debashify_params* params) return has_replaced; } -condlist* debashify_manipulation_substring(variable* v, debashify_params* params) +condlist_t* debashify_manipulation_substring(variable_t* v, debashify_params* params) { - string_subarg* first = dynamic_cast(v->manip->sa[0]); + subarg_string_t* first = dynamic_cast(v->manip->sa[0]); first->val = first->val.substr(1); if(first->val == "") { @@ -840,7 +840,7 @@ condlist* debashify_manipulation_substring(variable* v, debashify_params* params v->manip->sa.erase(v->manip->sa.begin()); } std::string manip = v->manip->first_sa_string(); - arg *arg1=nullptr, *arg2=nullptr; + arg_t *arg1=nullptr, *arg2=nullptr; size_t colon_pos = manip.find(':'); if(colon_pos != std::string::npos || v->manip->sa.size()>1) { @@ -848,12 +848,12 @@ condlist* debashify_manipulation_substring(variable* v, debashify_params* params { if(v->manip->sa[i]->type == _obj::subarg_string) { - string_subarg* t = dynamic_cast(v->manip->sa[i]); + subarg_string_t* t = dynamic_cast(v->manip->sa[i]); size_t colon_pos = t->val.find(':'); if(colon_pos != std::string::npos) { - arg1 = new arg; - arg2 = new arg; + arg1 = new arg_t; + arg2 = new arg_t; for(uint32_t j=0; jadd(v->manip->sa[j]); std::string val=t->val.substr(0, colon_pos); @@ -883,15 +883,15 @@ condlist* debashify_manipulation_substring(variable* v, debashify_params* params 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")))); + pipeline_t* pl = new pipeline_t(make_printf_variable(v->varname)); + arg_t* retarg = new arg_t; + retarg->add(new subarg_arithmetic_t(make_arithmetic(arg1, "+", new arg_t("1")))); retarg->add("-"); - pl->add(make_cmd({new arg("cut"), new arg("-c"), retarg})); + pl->add(make_cmd({new arg_t("cut"), new arg_t("-c"), retarg})); if(arg2 != nullptr) { - retarg = new arg; + retarg = new arg_t; retarg->add("-"); for(auto it: arg2->sa) { @@ -900,56 +900,56 @@ condlist* debashify_manipulation_substring(variable* v, debashify_params* params arg2->sa.resize(0); delete arg2; arg2 = nullptr; - pl->add(make_cmd({new arg("cut"), new arg("-c"), retarg})); + pl->add(make_cmd({new arg_t("cut"), new arg_t("-c"), retarg})); } if(v->manip != nullptr) delete v->manip; v->manip = nullptr; - return new condlist(pl); + return new condlist_t(pl); } -bool debashify_manipulation(arg* in, debashify_params* params) +bool debashify_manipulation(arg_t* in, debashify_params* params) { bool has_replaced=false; for(uint32_t i=0; isa.size(); i++) { if(in->sa[i]->type == _obj::subarg_variable) { - variable* v = dynamic_cast(in->sa[i])->var; + variable_t* v = dynamic_cast(in->sa[i])->var; if(!v->is_manip || v->manip == nullptr) return false; std::string manip = v->manip->first_sa_string(); - subarg* r = nullptr; + subarg_t* r = nullptr; if(v->is_manip && v->precedence && v->manip->string() == "!") { - arg* eval_arg = new arg; + arg_t* eval_arg = new arg_t; eval_arg->add("\\\"\\${"); - eval_arg->add(new variable_subarg(new variable(v->varname))); + eval_arg->add(new subarg_variable_t(new variable_t(v->varname))); eval_arg->add("}\\\""); - cmd* eval_cmd = make_cmd({new arg("eval"), new arg("echo"), eval_arg}); - r = new subshell_subarg(new subshell(eval_cmd)); + cmd_t* eval_cmd = make_cmd({new arg_t("eval"), new arg_t("echo"), eval_arg}); + r = new subarg_subshell_t(new subshell_t(eval_cmd)); } else if(manip.size()>0 && manip[0] == '/') { - cmd* prnt = make_printf_variable(v->varname); + cmd_t* prnt = make_printf_variable(v->varname); // printf %s\\n "$var" - cmd* sed = make_cmd({std::string("sed")}); - arg* sedarg=v->manip; + cmd_t* sed = make_cmd({std::string("sed")}); + arg_t* sedarg=v->manip; v->manip = nullptr; sedarg->insert(0, "s"); sedarg->add("/"); force_quotes(sedarg); sed->add(sedarg); // sed "s///g" - pipeline* pl = new pipeline(prnt); + pipeline_t* pl = new pipeline_t(prnt); pl->add(sed); - r = new subshell_subarg(new subshell(new list(new condlist(pl)))); + r = new subarg_subshell_t(new subshell_t(new list_t(new condlist_t(pl)))); } else if(manip.size()>0 && manip[0] == ':' && !(manip.size()>1 && is_in(manip[1], "+-") ) ) { - r = new subshell_subarg(new subshell(new list(debashify_manipulation_substring(v, params)))); + r = new subarg_subshell_t(new subshell_t(new list_t(debashify_manipulation_substring(v, params)))); } if(r != nullptr) @@ -964,7 +964,7 @@ bool debashify_manipulation(arg* in, debashify_params* params) return has_replaced; } -bool debashify_var(variable* in, debashify_params* params) +bool debashify_var(variable_t* in, debashify_params* params) { return false; } @@ -975,39 +975,39 @@ bool r_debashify(_obj* o, debashify_params* params) debashify_arithmetic_replace(o, params); switch(o->type) { - case _obj::_variable: { - variable* t = dynamic_cast(o); + case _obj::variable: { + variable_t* t = dynamic_cast(o); debashify_var(t, params); } break; - case _obj::_arg: { - arg* t = dynamic_cast(o); + case _obj::arg: { + arg_t* t = dynamic_cast(o); debashify_subarg_replace(t, params); debashify_manipulation(t, params); } break; - case _obj::_list: { - list* t = dynamic_cast(o); + case _obj::list: { + list_t* t = dynamic_cast(o); debashify_declare(t, params); debashify_readonly(t); debashify_procsub(t, params); } break; - case _obj::_pipeline: { - pipeline* t = dynamic_cast(o); + case _obj::pipeline: { + pipeline_t* t = dynamic_cast(o); debashify_echo(t); debashify_herestring(t); debashify_bashtest(t); } break; case _obj::block_cmd: { - cmd* t = dynamic_cast(o); + cmd_t* t = dynamic_cast(o); debashify_combined_redirects(t); debashify_array_set(t, params); debashify_plusequal(t, params); } break; case _obj::block_subshell: { - subshell* t = dynamic_cast(o); + subshell_t* t = dynamic_cast(o); debashify_combined_redirects(t); } break; case _obj::block_brace: { - brace* t = dynamic_cast(o); + brace_t* t = dynamic_cast(o); debashify_combined_redirects(t); } break; case _obj::block_main: { @@ -1015,23 +1015,23 @@ bool r_debashify(_obj* o, debashify_params* params) debashify_combined_redirects(t); } break; case _obj::block_function: { - function* t = dynamic_cast(o); + function_t* t = dynamic_cast(o); debashify_combined_redirects(t); } break; case _obj::block_case: { - case_block* t = dynamic_cast(o); + case_t* t = dynamic_cast(o); debashify_combined_redirects(t); } break; case _obj::block_if: { - if_block* t = dynamic_cast(o); + if_t* t = dynamic_cast(o); debashify_combined_redirects(t); } break; case _obj::block_while: { - while_block* t = dynamic_cast(o); + while_t* t = dynamic_cast(o); debashify_combined_redirects(t); } break; case _obj::block_for: { - for_block* t = dynamic_cast(o); + for_t* t = dynamic_cast(o); debashify_combined_redirects(t); } break; default: break; diff --git a/src/exec.cpp b/src/exec.cpp index 4c9717a..2521517 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -18,9 +18,9 @@ #define PIPE_READ 0 #define PIPE_WRITE 1 -std::vector do_include_exec(condlist* cmd, parse_context ctx, FILE* fd) +std::vector do_include_exec(condlist_t* cmd, parse_context ctx, FILE* fd) { - std::vector ret; + std::vector ret; std::string dir; auto incs=do_include_raw(cmd, ctx, &dir); @@ -36,9 +36,9 @@ std::vector do_include_exec(condlist* cmd, parse_context ctx, FILE* f } // if first is nullptr: is a string -std::vector do_resolve_exec(condlist* cmd, parse_context ctx, FILE* fd) +std::vector do_resolve_exec(condlist_t* cmd, parse_context ctx, FILE* fd) { - std::vector ret; + std::vector ret; std::pair p; try @@ -61,9 +61,9 @@ std::vector do_resolve_exec(condlist* cmd, parse_context ctx, FILE* f // -- OBJECT CALLS -- -bool resolve_condlist_exec(condlist* in, parse_context ctx, FILE* fd) +bool resolve_condlist_exec(condlist_t* in, parse_context ctx, FILE* fd) { - cmd* tc = in->first_cmd(); + cmd_t* tc = in->first_cmd(); if(tc == nullptr) return false; @@ -83,7 +83,7 @@ bool resolve_condlist_exec(condlist* in, parse_context ctx, FILE* fd) } -bool resolve_exec(condlist* in, parse_context ctx, FILE* fd) +bool resolve_exec(condlist_t* in, parse_context ctx, FILE* fd) { if(!resolve_condlist_exec(in, ctx, fd)) { @@ -143,7 +143,7 @@ void parse_exec(FILE* fd, parse_context ctx) ctx.i=skip_unread(ctx); debashify_params debash_params; - list* t_lst=new list; + list_t* t_lst=new list_t; if(t_lst == nullptr) throw std::runtime_error("Alloc error"); while(ctx.i0 && !is_num(_str[_str.size()-1]); @@ -163,7 +163,7 @@ std::string block::generate_redirs(int ind, std::string const& _str, generate_co return ret; } -std::string if_block::generate(int ind, generate_context* ctx) +std::string if_t::generate(int ind, generate_context* ctx) { std::string ret; @@ -197,7 +197,7 @@ std::string if_block::generate(int ind, generate_context* ctx) return ret; } -std::string for_block::generate(int ind, generate_context* ctx) +std::string for_t::generate(int ind, generate_context* ctx) { std::string ret; @@ -218,7 +218,7 @@ std::string for_block::generate(int ind, generate_context* ctx) return ret; } -std::string while_block::generate(int ind, generate_context* ctx) +std::string while_t::generate(int ind, generate_context* ctx) { std::string ret; @@ -238,7 +238,7 @@ std::string while_block::generate(int ind, generate_context* ctx) return ret; } -std::string subshell::generate(int ind, generate_context* ctx) +std::string subshell_t::generate(int ind, generate_context* ctx) { std::string ret; // open subshell @@ -271,7 +271,7 @@ std::string shmain::generate(bool print_shebang, int ind) return ret; } -std::string brace::generate(int ind, generate_context* ctx) +std::string brace_t::generate(int ind, generate_context* ctx) { std::string ret; @@ -283,7 +283,7 @@ std::string brace::generate(int ind, generate_context* ctx) return ret; } -std::string function::generate(int ind, generate_context* ctx) +std::string function_t::generate(int ind, generate_context* ctx) { std::string ret; // function definition @@ -298,7 +298,7 @@ std::string function::generate(int ind, generate_context* ctx) return ret; } -std::string case_block::generate(int ind, generate_context* ctx) +std::string case_t::generate(int ind, generate_context* ctx) { std::string ret; ret += "case " + carg->generate(ind) + " in\n"; @@ -339,7 +339,7 @@ std::string case_block::generate(int ind, generate_context* ctx) return ret; } -std::string cmd::generate(int ind, generate_context* ctx) +std::string cmd_t::generate(int ind, generate_context* ctx) { std::string ret; @@ -397,7 +397,7 @@ std::string cmd::generate(int ind, generate_context* ctx) // SUBARG -std::string subshell_subarg::generate(int ind) +std::string subarg_subshell_t::generate(int ind) { std::string r = sbsh->generate(ind); if(backtick) { @@ -409,7 +409,7 @@ std::string subshell_subarg::generate(int ind) return '$' + r; } -std::string procsub_subarg::generate(int ind) +std::string subarg_procsub_t::generate(int ind) { if(is_output) return '>' + sbsh->generate(ind); @@ -417,7 +417,7 @@ std::string procsub_subarg::generate(int ind) return '<' + sbsh->generate(ind); } -std::string arithmetic_subarg::generate(int ind) +std::string subarg_arithmetic_t::generate(int ind) { std::string ret; ret += "$(("; @@ -430,7 +430,7 @@ std::string arithmetic_subarg::generate(int ind) // ARITHMETIC -std::string operation_arithmetic::generate(int ind) +std::string arithmetic_operation_t::generate(int ind) { std::string ret; if(precedence) @@ -450,7 +450,7 @@ std::string operation_arithmetic::generate(int ind) return ret; } -std::string parenthesis_arithmetic::generate(int ind) +std::string arithmetic_parenthesis_t::generate(int ind) { std::string ret; ret += '('; @@ -461,12 +461,12 @@ std::string parenthesis_arithmetic::generate(int ind) return ret; } -std::string subshell_arithmetic::generate(int ind) +std::string arithmetic_subshell_t::generate(int ind) { return '$' + sbsh->generate(ind); } -std::string variable_arithmetic::generate(int ind) +std::string arithmetic_variable_t::generate(int ind) { std::string ret=var->generate(ind); if(is_num(ret[0]) || is_in(ret[0], SPECIAL_VARS) || var->is_manip) @@ -474,7 +474,7 @@ std::string variable_arithmetic::generate(int ind) return ret; } -std::string variable::generate(int ind) +std::string variable_t::generate(int ind) { std::string ret; if(is_manip) diff --git a/src/main.cpp b/src/main.cpp index c3ef500..42d71bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -72,7 +72,7 @@ int main(int argc, char* argv[]) // parsing - sh = new shmain(new list); + sh = new shmain; bool is_exec = false; bool first_run = true; @@ -131,7 +131,6 @@ int main(int argc, char* argv[]) } // parse - g_origin=file; if(!add_include(file)) continue; diff --git a/src/minify.cpp b/src/minify.cpp index 9c72c2f..acd064e 100644 --- a/src/minify.cpp +++ b/src/minify.cpp @@ -6,9 +6,9 @@ #include "processing.hpp" #include "util.hpp" -std::vector cmd::subarg_vars() +std::vector cmd_t::subarg_vars() { - std::vector ret; + std::vector ret; if(args==nullptr || args->size()<=0) return ret; @@ -16,7 +16,7 @@ std::vector cmd::subarg_vars() { for(uint32_t i=1; isize(); i++) { - arg* ta = args->args[i]; + arg_t* ta = args->args[i]; if(ta->sa.size() < 1 || ta->sa[0]->type != _obj::subarg_string) continue; if(ta->sa.size() >= 1 && is_varname(ta->sa[0]->generate(0))) @@ -34,19 +34,19 @@ bool r_replace_fct(_obj* in, strmap_t* fctmap) switch(in->type) { case _obj::block_function: { - function* t = dynamic_cast(in); + function_t* t = dynamic_cast(in); auto el=fctmap->find(t->name); if(el!=fctmap->end()) t->name = el->second; }; break; case _obj::block_cmd: { - cmd* t = dynamic_cast(in); + cmd_t* t = dynamic_cast(in); std::string cmdname = t->arg_string(0); auto el=fctmap->find(cmdname); if(el!=fctmap->end()) { delete t->args->args[0]; - t->args->args[0] = new arg(el->second); + t->args->args[0] = new arg_t(el->second); } }; break; default: break; @@ -58,8 +58,8 @@ bool r_replace_var(_obj* in, strmap_t* varmap) { switch(in->type) { - case _obj::_variable: { - variable* t = dynamic_cast(in); + case _obj::variable: { + variable_t* t = dynamic_cast(in); auto el=varmap->find(t->varname); if(el!=varmap->end()) t->varname = el->second; @@ -121,7 +121,7 @@ bool is_varname(const char c) { return is_alphanum(c) || c == '_'; } -void do_minify_quotes(arg* in) +void do_minify_quotes(arg_t* in) { auto t = in->sa.begin(); // global loop @@ -152,7 +152,7 @@ void do_minify_quotes(arg* in) { // previous is alphanum var: removing quote can change varname if((*t)->type == _obj::subarg_variable) { - variable_subarg* vs = dynamic_cast(*t); + subarg_variable_t* vs = dynamic_cast(*t); if(vs->var != nullptr && !vs->var->is_manip && vs->var->varname.size()>0 && !(is_in(vs->var->varname[0], SPECIAL_VARS) || is_num(vs->var->varname[0]) ) ) prev_is_var = true; } @@ -164,7 +164,7 @@ void do_minify_quotes(arg* in) return; i=0; } - std::string& val = dynamic_cast(*t)->val; + std::string& val = dynamic_cast(*t)->val; while(itype == _obj::subarg_variable) { - variable_subarg* vs = dynamic_cast(*t); + subarg_variable_t* vs = dynamic_cast(*t); if(vs->var != nullptr && !vs->var->is_manip && vs->var->varname.size()>0 && !(is_in(vs->var->varname[0], SPECIAL_VARS) || is_num(vs->var->varname[0]) ) ) end_is_var = true; } @@ -208,7 +208,7 @@ void do_minify_quotes(arg* in) return; i=0; } - std::string& val = dynamic_cast(*t)->val; + std::string& val = dynamic_cast(*t)->val; if(doublequote) { while(ival; for(uint32_t i=0; itype) { - case _obj::_arg: { - arg* t = dynamic_cast(in); + case _obj::arg: { + arg_t* t = dynamic_cast(in); do_minify_quotes(t); }; break; case _obj::subarg_string: { - string_subarg* t = dynamic_cast(in); + subarg_string_t* t = dynamic_cast(in); do_minify_dollar(t); }; break; - case _obj::_redirect: { + case _obj::redirect: { // for redirects: don't minify quotes on here documents - redirect* t = dynamic_cast(in); + redirect_t* t = dynamic_cast(in); if(t->here_document != nullptr) { recurse(r_minify_useless_quotes, t->target); @@ -511,14 +511,14 @@ bool r_minify_empty_manip(_obj* in) { switch(in->type) { - case _obj::_arg: { - arg* t = dynamic_cast(in); + case _obj::arg: { + arg_t* t = dynamic_cast(in); for(uint32_t i=0; isa.size(); i++) { if(t->sa[i]->type == _obj::subarg_variable) { // has to be a variable - variable_subarg* ss = dynamic_cast(t->sa[i]); + subarg_variable_t* ss = dynamic_cast(t->sa[i]); if(ss->var->is_manip) { // if is a manip: possibility to skip it @@ -527,7 +527,7 @@ bool r_minify_empty_manip(_obj* in) if(i+1sa.size() && t->sa[i+1]->type == _obj::subarg_string) { // if next subarg is a string: check its first char - string_subarg* ss = dynamic_cast(t->sa[i+1]); + subarg_string_t* ss = dynamic_cast(t->sa[i+1]); char c = ss->val[0]; // if its first would extend the var name: skip if(is_alphanum(c) || c == '_') @@ -545,14 +545,14 @@ bool r_minify_empty_manip(_obj* in) return true; } -block* do_one_minify_single_block(block* in) +block_t* do_one_minify_single_block(block_t* in) { - block* ret=nullptr; - list* l=nullptr; + block_t* ret=nullptr; + list_t* l=nullptr; if(in->type == _obj::block_brace) - l = dynamic_cast(in)->lst; + l = dynamic_cast(in)->lst; else if(in->type == _obj::block_subshell) - l = dynamic_cast(in)->lst; + l = dynamic_cast(in)->lst; if(l == nullptr) return nullptr; @@ -574,17 +574,17 @@ bool r_minify_single_block(_obj* in) { switch(in->type) { - case _obj::_pipeline: { + case _obj::pipeline: { bool has_operated=false; do { // loop operating on current // (if has operated, current object has changed) has_operated=false; - pipeline* t = dynamic_cast(in); + pipeline_t* t = dynamic_cast(in); for(uint32_t i=0; icmds.size(); i++) { - block* ret = do_one_minify_single_block(t->cmds[i]); + block_t* ret = do_one_minify_single_block(t->cmds[i]); if(ret != nullptr) { // concatenate redirects for(uint32_t j=0; jcmds[i]->redirs.size(); j++) @@ -593,9 +593,9 @@ bool r_minify_single_block(_obj* in) // deindex t->cmds[i]->redirs.resize(0); if(t->cmds[i]->type == _obj::block_brace) - dynamic_cast(t->cmds[i])->lst->cls[0]->pls[0]->cmds[0] = nullptr; + dynamic_cast(t->cmds[i])->lst->cls[0]->pls[0]->cmds[0] = nullptr; else if(t->cmds[i]->type == _obj::block_subshell) - dynamic_cast(t->cmds[i])->lst->cls[0]->pls[0]->cmds[0] = nullptr; + dynamic_cast(t->cmds[i])->lst->cls[0]->pls[0]->cmds[0] = nullptr; // replace value delete t->cmds[i]; @@ -619,7 +619,7 @@ bool r_has_backtick(_obj* in, bool* r) switch(in->type) { case _obj::subarg_subshell: { - subshell_subarg* t = dynamic_cast(in); + subarg_subshell_t* t = dynamic_cast(in); if(t->backtick) { *r = true; return false; @@ -635,7 +635,7 @@ bool r_minify_backtick(_obj* in) switch(in->type) { case _obj::subarg_subshell: { - subshell_subarg* t = dynamic_cast(in); + subarg_subshell_t* t = dynamic_cast(in); if(!t->backtick) { bool has_backtick_child=false; recurse(r_has_backtick, t->sbsh, &has_backtick_child); diff --git a/src/options.cpp b/src/options.cpp index 1cc003e..4307e6e 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -77,22 +77,18 @@ void get_opts() ztd::option_set create_include_opts() { - ztd::option_set opts; - opts.add( + return std::vector({ ztd::option('C', false, "Don't cd to folder the file is in"), ztd::option('f', false, "Force include even if already included. Don't count as included") - ); - return opts; + }); } ztd::option_set create_resolve_opts() { - ztd::option_set opts; - opts.add( + return std::vector({ ztd::option('C', false, "Don't cd to folder this file is in"), ztd::option('f', false, "Ignore non-zero return values") - ); - return opts; + }); } void print_help(const char* arg0) @@ -125,7 +121,7 @@ void print_resolve_help() printf("Execute shell command and substitute output, from folder of current file\n"); printf(" - Default behaviour is to parse contents as shell code\n"); printf(" - Fails if return value is not 0. Can be ignored with -f\n"); - printf(" - `%%include` inside substitutions replaces the substitution and puts raw response\n"); + printf(" - `%%resolve` inside substitutions replaces the substitution and puts raw response\n"); printf("\n"); ztd::option_set opts=create_resolve_opts(); diff --git a/src/parse.cpp b/src/parse.cpp index ade4367..bf53ca1 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -207,9 +207,9 @@ std::pair get_word(parse_context ctx, const char* end_set) // parse fcts -std::pair parse_var(parse_context ctx, bool specialvars, bool array) +std::pair parse_var(parse_context ctx, bool specialvars, bool array) { - variable* ret=nullptr; + variable_t* ret=nullptr; std::string varname; uint32_t start=ctx.i; @@ -227,7 +227,7 @@ std::pair parse_var(parse_context ctx, bool specialvar } if(varname != "") { - ret = new variable(varname); + ret = new variable_t(varname); if(ctx.bash && array && ctx[ctx.i]=='[') { ctx.i++; @@ -264,9 +264,9 @@ std::pair get_operator(parse_context ctx) // parse an arithmetic // ends at )) // temporary, to improve -std::pair parse_arithmetic(parse_context ctx) +std::pair parse_arithmetic(parse_context ctx) { - arithmetic* ret = nullptr; + arithmetic_t* ret = nullptr; ctx.i = skip_chars(ctx, SEPARATORS); if(ctx.i>ctx.size || ctx[ctx.i] == ')') @@ -280,12 +280,12 @@ std::pair parse_arithmetic(parse_context ctx) { ctx.i = po.second; auto pa = parse_arithmetic(ctx); - ret = new operation_arithmetic(po.first, pa.first, nullptr, true); + ret = new arithmetic_operation_t(po.first, pa.first, nullptr, true); ctx=pa.second; } else { - variable_arithmetic* ttvar=nullptr; // for categorizing definitions + arithmetic_variable_t* ttvar=nullptr; // for categorizing definitions if(ctx[ctx.i]=='-' || is_num(ctx[ctx.i])) { uint32_t j=ctx.i; @@ -293,20 +293,20 @@ std::pair parse_arithmetic(parse_context ctx) ctx.i++; while(is_num(ctx[ctx.i])) ctx.i++; - ret = new number_arithmetic( std::string(ctx.data+j, ctx.i-j) ); + ret = new arithmetic_number_t( std::string(ctx.data+j, ctx.i-j) ); } else if(word_eq("$(", ctx)) { ctx.i+=2; auto ps = parse_subshell(ctx); - ret = new subshell_arithmetic(ps.first); + ret = new arithmetic_subshell_t(ps.first); ctx=ps.second; } else if(word_eq("${", ctx)) { ctx.i+=2; auto pm = parse_manipulation(ctx); - ret = new variable_arithmetic(pm.first); + ret = new arithmetic_variable_t(pm.first); ctx=pm.second; } else if(ctx[ctx.i] == '(') @@ -326,7 +326,7 @@ std::pair parse_arithmetic(parse_context ctx) ctx.i++; } auto pp = parse_var(ctx, specialvars, true); - ttvar = new variable_arithmetic(pp.first); + ttvar = new arithmetic_variable_t(pp.first); ret = ttvar; ctx=pp.second; } @@ -339,12 +339,12 @@ std::pair parse_arithmetic(parse_context ctx) { parse_error( "Unknown arithmetic operator: "+po.first, ctx); } - arithmetic* val1 = ret; + arithmetic_t* val1 = ret; ctx.i=po.second; auto pa = parse_arithmetic(ctx); - arithmetic* val2 = pa.first; + arithmetic_t* val2 = pa.first; ctx = pa.second; - ret = new operation_arithmetic(po.first, val1, val2); + ret = new arithmetic_operation_t(po.first, val1, val2); ctx.i = skip_chars(ctx, SEPARATORS); } @@ -366,10 +366,10 @@ std::pair parse_arithmetic(parse_context ctx) return std::make_pair(ret, ctx); } -std::pair parse_manipulation(parse_context ctx) +std::pair parse_manipulation(parse_context ctx) { - variable* ret = nullptr; - arg* precede = nullptr; + variable_t* ret = nullptr; + arg_t* precede = nullptr; uint32_t start=ctx.i; @@ -382,7 +382,7 @@ std::pair parse_manipulation(parse_context ctx) } std::string t; t+=ctx[ctx.i]; - precede = new arg( t ); + precede = new arg_t( t ); ctx.i++; } @@ -418,7 +418,7 @@ std::pair parse_manipulation(parse_context ctx) return std::make_pair(ret, ctx); } -inline parse_context do_one_subarg_step(arg* ret, parse_context ctx, uint32_t& j, bool is_quoted) +inline parse_context do_one_subarg_step(arg_t* ret, parse_context ctx, uint32_t& j, bool is_quoted) { if( ctx.i >= ctx.size) return ctx; @@ -444,7 +444,7 @@ inline parse_context do_one_subarg_step(arg* ret, parse_context ctx, uint32_t& j parse_context newct = ctx; newct.size=k; auto r=parse_list_until(newct); - ret->add(new subshell_subarg(new subshell(std::get<0>(r)), is_quoted, true)); + ret->add(new subarg_subshell_t(new subshell_t(std::get<0>(r)), is_quoted, true)); uint64_t tsize=ctx.size; ctx = std::get<1>(r); ctx.size = tsize; @@ -459,7 +459,7 @@ inline parse_context do_one_subarg_step(arg* ret, parse_context ctx, uint32_t& j // get arithmetic ctx.i+=3; auto r=parse_arithmetic(ctx); - arithmetic_subarg* tt = new arithmetic_subarg(r.first); + subarg_arithmetic_t* tt = new subarg_arithmetic_t(r.first); tt->quoted=is_quoted; ret->add(tt); ctx = r.second; @@ -481,7 +481,7 @@ inline parse_context do_one_subarg_step(arg* ret, parse_context ctx, uint32_t& j // get subshell ctx.i+=2; auto r=parse_subshell(ctx); - ret->add(new subshell_subarg(r.first, is_quoted)); + ret->add(new subarg_subshell_t(r.first, is_quoted)); ctx = r.second; j = ctx.i; } @@ -493,7 +493,7 @@ inline parse_context do_one_subarg_step(arg* ret, parse_context ctx, uint32_t& j // get manipulation ctx.i+=2; auto r=parse_manipulation(ctx); - ret->add(new variable_subarg(r.first, is_quoted)); + ret->add(new subarg_variable_t(r.first, is_quoted)); ctx = r.second; j = ctx.i; } @@ -508,7 +508,7 @@ inline parse_context do_one_subarg_step(arg* ret, parse_context ctx, uint32_t& j if(ctx.i-j>0) ret->add(std::string(ctx.data+j, ctx.i-j)); // add var - ret->add(new variable_subarg(r.first, is_quoted)); + ret->add(new subarg_variable_t(r.first, is_quoted)); ctx = r.second; j = ctx.i; } @@ -543,9 +543,9 @@ bool _optimize_skip_arg(parse_context& ctx, const char* str) { // parse one argument // must start at a read char // ends at either " \t|&;\n()" -std::pair parse_arg(parse_context ctx, const char* end, const char* unexpected, bool doquote, const char* optimize) +std::pair parse_arg(parse_context ctx, const char* end, const char* unexpected, bool doquote, const char* optimize) { - arg* ret = new arg; + arg_t* ret = new arg_t; // j : start of subarg , q = start of quote uint32_t j=ctx.i,q=ctx.i; @@ -638,7 +638,6 @@ parse_context parse_heredocument(parse_context ctx) { ctx.i = ctx.size; } - // std::string tmpparse=std::string(ctx.data+j, ctx.i-j); parse_context newctx = make_context(ctx, j); newctx.size = ctx.i; auto pval = parse_arg(newctx , NULL, NULL, false, ARG_OPTIMIZE_NULL); @@ -654,7 +653,7 @@ parse_context parse_heredocument(parse_context ctx) return ctx; } -std::pair parse_redirect(parse_context ctx) +std::pair parse_redirect(parse_context ctx) { bool is_redirect=false; bool needs_arg=false; @@ -741,9 +740,9 @@ std::pair parse_redirect(parse_context ctx) if(is_redirect) { - redirect* ret=nullptr; + redirect_t* ret=nullptr; - ret = new redirect; + ret = new redirect_t; ret->op = std::string(ctx.data+start, ctx.i-start); if(needs_arg) { @@ -797,9 +796,9 @@ std::pair parse_redirect(parse_context ctx) // must start at a read char // first char has to be read // ends at either &|;\n#() -std::pair parse_arglist(parse_context ctx, bool hard_error, std::vector* redirs) +std::pair parse_arglist(parse_context ctx, bool hard_error, std::vector* redirs) { - arglist* ret = nullptr; + arglist_t* ret = nullptr; if(word_eq("[[", ctx, ARG_END) ) // [[ bash specific parsing { @@ -810,14 +809,14 @@ std::pair parse_arglist(parse_context ctx, bool hard_er while(true) { if(ret == nullptr) - ret = new arglist; + ret = new arglist_t; auto pp=parse_arg(ctx, SEPARATORS, NULL, true, ARG_OPTIMIZE_BASHTEST); ret->add(pp.first); ctx = pp.second; ctx.i = skip_chars(ctx, SEPARATORS); if(word_eq("]]", ctx, ARG_END)) { - ret->add(new arg("]]")); + ret->add(new arg_t("]]")); ctx.i+=2; ctx.i = skip_chars(ctx, SPACES); if( !is_in(ctx[ctx.i], ARGLIST_END) ) @@ -857,9 +856,9 @@ std::pair parse_arglist(parse_context ctx, bool hard_er bool is_output = ctx[ctx.i] == '>'; ctx.i+=2; if(ret == nullptr) - ret = new arglist; + ret = new arglist_t; auto ps = parse_subshell(ctx); - ret->add(new arg(new procsub_subarg(is_output, ps.first))); + ret->add(new arg_t(new subarg_procsub_t(is_output, ps.first))); ctx=ps.second; } else if(redirs!=nullptr) @@ -877,7 +876,7 @@ std::pair parse_arglist(parse_context ctx, bool hard_er { argparse: if(ret == nullptr) - ret = new arglist; + ret = new arglist_t; auto pp=parse_arg(ctx); ret->add(pp.first); ctx = pp.second; @@ -905,9 +904,9 @@ std::pair parse_arglist(parse_context ctx, bool hard_er // must start at a read char // separated by | // ends at either &;\n#) -std::pair parse_pipeline(parse_context ctx) +std::pair parse_pipeline(parse_context ctx) { - pipeline* ret = new pipeline; + pipeline_t* ret = new pipeline_t; if(ctx[ctx.i] == '!' && ctx.i+1 parse_pipeline(parse_context ctx) // must start at a read char // separated by && or || // ends at either ;\n)# -std::pair parse_condlist(parse_context ctx) +std::pair parse_condlist(parse_context ctx) { - condlist* ret = new condlist; + condlist_t* ret = new condlist_t; ctx.i = skip_unread(ctx); bool optype=AND_OP; @@ -998,9 +997,9 @@ std::pair parse_condlist(parse_context ctx) return std::make_pair(ret, ctx); } -std::tuple parse_list_until(parse_context ctx, list_parse_options opts) +std::tuple parse_list_until(parse_context ctx, list_parse_options opts) { - list* ret = new list; + list_t* ret = new list_t; ctx.i=skip_unread(ctx); std::string found_end_word; @@ -1120,9 +1119,9 @@ std::tuple parse_list_until(parse_context ctx // parse a subshell // must start right after the opening ( // ends at ) and nothing else -std::pair parse_subshell(parse_context ctx) +std::pair parse_subshell(parse_context ctx) { - subshell* ret = new subshell; + subshell_t* ret = new subshell_t; uint32_t start=ctx.i; ctx.i = skip_unread(ctx); @@ -1142,9 +1141,9 @@ std::pair parse_subshell(parse_context ctx) // parse a brace block // must start right after the opening { // ends at } and nothing else -std::pair parse_brace(parse_context ctx) +std::pair parse_brace(parse_context ctx) { - brace* ret = new brace; + brace_t* ret = new brace_t; uint32_t start=ctx.i; ctx.i = skip_unread(ctx); @@ -1164,9 +1163,9 @@ std::pair parse_brace(parse_context ctx) // parse a function // must start right after the () // then parses a brace block -std::pair parse_function(parse_context ctx, const char* after) +std::pair parse_function(parse_context ctx, const char* after) { - function* ret = new function; + function_t* ret = new function_t; ctx.i=skip_unread(ctx); if(ctx[ctx.i] != '{') @@ -1192,7 +1191,7 @@ std::pair parse_function(parse_context ctx, const char } // parse only var assigns -parse_context parse_cmd_varassigns(cmd* in, parse_context ctx, bool cmdassign=false, std::string const& cmd="") +parse_context parse_cmd_varassigns(cmd_t* in, parse_context ctx, bool cmdassign=false, std::string const& cmd="") { bool forbid_assign=false; bool forbid_special=false; @@ -1201,7 +1200,7 @@ parse_context parse_cmd_varassigns(cmd* in, parse_context ctx, bool cmdassign=fa if(cmdassign && (forbid_special || cmd == "export") ) forbid_special=true; - std::vector>* ret=&in->var_assigns; + std::vector>* ret=&in->var_assigns; if(cmdassign) ret=&in->cmd_var_assigns; @@ -1237,7 +1236,7 @@ parse_context parse_cmd_varassigns(cmd* in, parse_context ctx, bool cmdassign=fa else ctx.i++; - arg* ta=nullptr; + arg_t* ta=nullptr; if(ctx[ctx.i] == '(') // bash var=() { if(!ctx.bash) @@ -1258,7 +1257,7 @@ parse_context parse_cmd_varassigns(cmd* in, parse_context ctx, bool cmdassign=fa } else if( is_in(ctx[ctx.i], ARG_END) ) // no value : give empty value { - ta = new arg; + ta = new arg_t; } else { @@ -1301,9 +1300,9 @@ parse_context parse_cmd_varassigns(cmd* in, parse_context ctx, bool cmdassign=fa } // must start at read char -std::pair parse_cmd(parse_context ctx) +std::pair parse_cmd(parse_context ctx) { - cmd* ret = new cmd; + cmd_t* ret = new cmd_t; ctx = parse_cmd_varassigns(ret, ctx); @@ -1316,8 +1315,8 @@ std::pair parse_cmd(parse_context ctx) parse_error("bash specific: "+wp.first, ctx); } - ret->args = new arglist; - ret->args->add(new arg(wp.first)); + ret->args = new arglist_t; + ret->args->add(new arg_t(wp.first)); ret->is_cmdvar=true; ctx.i = wp.second; ctx.i = skip_chars(ctx, SPACES); @@ -1342,9 +1341,9 @@ std::pair parse_cmd(parse_context ctx) // parse a case block // must start right after the case // ends at } and nothing else -std::pair parse_case(parse_context ctx) +std::pair parse_case(parse_context ctx) { - case_block* ret = new case_block; + case_t* ret = new case_t; ctx.i=skip_chars(ctx, SPACES); // get the treated argument @@ -1366,7 +1365,7 @@ std::pair parse_case(parse_context ctx) while(ctx.icases.push_back( std::make_pair(std::vector(), nullptr) ); + ret->cases.push_back( std::make_pair(std::vector(), nullptr) ); // iterator to last element auto cc = ret->cases.end()-1; @@ -1432,9 +1431,9 @@ std::pair parse_case(parse_context ctx) return std::make_pair(ret, ctx); } -std::pair parse_if(parse_context ctx) +std::pair parse_if(parse_context ctx) { - if_block* ret = new if_block; + if_t* ret = new if_t; while(true) { @@ -1491,9 +1490,9 @@ std::pair parse_if(parse_context ctx) return std::make_pair(ret, ctx); } -std::pair parse_for(parse_context ctx) +std::pair parse_for(parse_context ctx) { - for_block* ret = new for_block; + for_t* ret = new for_t; ctx.i = skip_chars(ctx, SPACES); auto wp = get_word(ctx, ARG_END); @@ -1502,7 +1501,7 @@ std::pair parse_for(parse_context ctx) { parse_error( strf("Bad variable name in for clause: '%s'", wp.first.c_str()), ctx ); } - ret->var = new variable(wp.first, nullptr, true); + ret->var = new variable_t(wp.first, nullptr, true); ctx.i = wp.second; ctx.i=skip_chars(ctx, SPACES); @@ -1555,9 +1554,9 @@ std::pair parse_for(parse_context ctx) return std::make_pair(ret, ctx); } -std::pair parse_while(parse_context ctx) +std::pair parse_while(parse_context ctx) { - while_block* ret = new while_block; + while_t* ret = new while_t; // cond parse_context oldctx = ctx; @@ -1587,10 +1586,10 @@ std::pair parse_while(parse_context ctx) } // detect if brace, subshell, case or other -std::pair parse_block(parse_context ctx) +std::pair parse_block(parse_context ctx) { ctx.i = skip_chars(ctx, SEPARATORS); - block* ret = nullptr; + block_t* ret = nullptr; if(ctx.i>=ctx.size) { @@ -1706,7 +1705,7 @@ std::pair parse_block(parse_context ctx) } - if(ret!=nullptr && ret->type != block::block_cmd) + if(ret!=nullptr && ret->type != block_t::block_cmd) { uint32_t j=skip_chars(ctx, SPACES); ctx.i=j; diff --git a/src/processing.cpp b/src/processing.cpp index c1a5291..c677a0c 100644 --- a/src/processing.cpp +++ b/src/processing.cpp @@ -155,7 +155,7 @@ std::string get_varname(std::string const& in) return in; } -std::string get_varname(arg* in) +std::string get_varname(arg_t* in) { if(in->sa.size() < 1 || in->sa[0]->type != _obj::subarg_string) return ""; @@ -170,12 +170,12 @@ bool cmd_is_argvar(std::string const& in) return is_in_set(in, posix_cmdvar) || is_in_set(in, bash_cmdvar); } -bool cmd::is_argvar() +bool cmd_t::is_argvar() { return is_cmdvar; } -bool cmd::is(std::string const& in) +bool cmd_t::is(std::string const& in) { return in == this->arg_string(0); } @@ -285,14 +285,14 @@ 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(new arg("unset")); + cmd_t* unset_cmd = new cmd_t; + unset_cmd->add(new arg_t("unset")); unset_cmd->is_cmdvar=true; for(auto it: m_vars) { - unset_cmd->cmd_var_assigns.push_back(std::make_pair(new variable(it.first), nullptr)); + unset_cmd->cmd_var_assigns.push_back(std::make_pair(new variable_t(it.first), nullptr)); } - condlist* cl = new condlist(unset_cmd); + condlist_t* cl = new condlist_t(unset_cmd); sh->lst->cls.insert(sh->lst->cls.begin(), cl); } } @@ -315,7 +315,7 @@ bool r_has_env_set(_obj* in, bool* result) return false; }; break; case _obj::block_cmd: { - cmd* t = dynamic_cast(in); + cmd_t* t = dynamic_cast(in); if(t->has_var_assign() || t->arg_string(0) == "cd") *result = true; } @@ -330,8 +330,8 @@ bool r_get_var(_obj* in, countmap_t* defmap, countmap_t* callmap) { switch(in->type) { - case _obj::_variable: { - variable* t = dynamic_cast(in); + case _obj::variable: { + variable_t* t = dynamic_cast(in); if(t->definition) { if(!defmap->insert( std::make_pair(t->varname, 1) ).second) @@ -353,7 +353,7 @@ bool r_get_unsets(_obj* in, set_t* unsets) switch(in->type) { case _obj::block_cmd: { - cmd* t = dynamic_cast(in); + cmd_t* t = dynamic_cast(in); if(t->is("unset")) { for(auto it: t->cmd_var_assigns) @@ -373,7 +373,7 @@ bool r_get_cmd(_obj* in, countmap_t* all_cmds) switch(in->type) { case _obj::block_cmd: { - cmd* t = dynamic_cast(in); + cmd_t* t = dynamic_cast(in); std::string cmdname = t->arg_string(0); if(cmdname != "" && !all_cmds->insert( std::make_pair(cmdname, 1) ).second) (*all_cmds)[cmdname]++; @@ -388,7 +388,7 @@ bool r_get_fct(_obj* in, countmap_t* fct_map) switch(in->type) { case _obj::block_function: { - function* t = dynamic_cast(in); + function_t* t = dynamic_cast(in); if(!fct_map->insert( std::make_pair(t->name, 1) ).second) (*fct_map)[t->name]++; }; break; @@ -418,14 +418,14 @@ bool r_delete_fct(_obj* in, set_t* fcts) { switch(in->type) { - case _obj::_list: { - list* t = dynamic_cast(in); + case _obj::list: { + list_t* t = dynamic_cast(in); for(uint32_t i=0; icls.size(); i++) { - block* tb = t->cls[i]->first_block(); + block_t* tb = t->cls[i]->first_block(); if(tb != nullptr && tb->type == _obj::block_function) { - function* fc = dynamic_cast(tb); + function_t* fc = dynamic_cast(tb); if(fcts->find(fc->name)!=fcts->end()) { delete t->cls[i]; @@ -444,16 +444,16 @@ bool r_delete_var(_obj* in, set_t* vars) { switch(in->type) { - case _obj::_list: { - list* t = dynamic_cast(in); + case _obj::list: { + list_t* t = dynamic_cast(in); for(uint32_t i=0; icls.size(); i++) { - block* tb = t->cls[i]->first_block(); + block_t* tb = t->cls[i]->first_block(); bool to_delete=false; bool has_deleted=false; if(tb != nullptr && tb->type == _obj::block_cmd) { - cmd* c = dynamic_cast(tb); + cmd_t* c = dynamic_cast(tb); for(uint32_t j=0; jvar_assigns.size(); j++) { @@ -549,7 +549,7 @@ bool r_do_string_processor(_obj* in) { if(in->type == _obj::subarg_string) { - string_subarg* t = dynamic_cast(in); + subarg_string_t* t = dynamic_cast(in); auto v = get_processors(t->val); if(v.find("LXSH_PARSE_MINIFY") != v.end()) { @@ -634,9 +634,9 @@ std::string gen_json_struc(_obj* o) std::vector> vec; switch(o->type) { - case _obj::_variable : + case _obj::variable : { - variable* t = dynamic_cast(o); + variable_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("variable") ) ); vec.push_back(std::make_pair(quote_string("varname"), quote_string(t->varname))); vec.push_back(std::make_pair(quote_string("definition"), boolstring(t->definition))); @@ -646,18 +646,18 @@ std::string gen_json_struc(_obj* o) vec.push_back(std::make_pair(quote_string("manip"), gen_json_struc(t->manip) ) ); break; } - case _obj::_redirect : + case _obj::redirect : { - redirect* t = dynamic_cast(o); + redirect_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("redirect") ) ); vec.push_back(std::make_pair(quote_string("op"), quote_string(t->op))); vec.push_back(std::make_pair(quote_string("target"), gen_json_struc(t->target))); vec.push_back(std::make_pair(quote_string("here_document"), gen_json_struc(t->here_document))); break; } - case _obj::_arg : + case _obj::arg : { - arg* t = dynamic_cast(o); + arg_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("arg") ) ); vec.push_back(std::make_pair(quote_string("forcequoted"), boolstring(t->forcequoted))); std::vector tvec; @@ -666,9 +666,9 @@ std::string gen_json_struc(_obj* o) vec.push_back(std::make_pair(quote_string("sa"), gen_json(tvec))); break; } - case _obj::_arglist : + case _obj::arglist : { - arglist* t = dynamic_cast(o); + arglist_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("arglist") ) ); std::vector tvec; for(auto it: t->args) @@ -676,9 +676,9 @@ std::string gen_json_struc(_obj* o) vec.push_back(std::make_pair(quote_string("args"), gen_json(tvec))); break; } - case _obj::_pipeline : + case _obj::pipeline : { - pipeline* t = dynamic_cast(o); + pipeline_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("pipeline") ) ); vec.push_back(std::make_pair(quote_string("negated"), boolstring(t->negated) ) ); std::vector tvec; @@ -687,9 +687,9 @@ std::string gen_json_struc(_obj* o) vec.push_back(std::make_pair(quote_string("cmds"), gen_json(tvec))); break; } - case _obj::_condlist : + case _obj::condlist : { - condlist* t = dynamic_cast(o); + condlist_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("condlist") ) ); vec.push_back(std::make_pair(quote_string("parallel"), boolstring(t->parallel) ) ); std::vector tvec; @@ -705,9 +705,9 @@ std::string gen_json_struc(_obj* o) vec.push_back(std::make_pair(quote_string("or_ops"), gen_json(ttvec))); break; } - case _obj::_list : + case _obj::list : { - list* t = dynamic_cast(o); + list_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("list") ) ); std::vector tvec; for(auto it: t->cls) @@ -717,7 +717,7 @@ std::string gen_json_struc(_obj* o) } case _obj::block_subshell : { - subshell* t = dynamic_cast(o); + subshell_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("subshell") ) ); vec.push_back(std::make_pair(quote_string("lst"), gen_json_struc(t->lst))); @@ -731,7 +731,7 @@ std::string gen_json_struc(_obj* o) } case _obj::block_brace : { - brace* t = dynamic_cast(o); + brace_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("brace") ) ); vec.push_back(std::make_pair(quote_string("lst"), gen_json_struc(t->lst))); @@ -760,7 +760,7 @@ std::string gen_json_struc(_obj* o) } case _obj::block_function : { - function* t = dynamic_cast(o); + function_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("function") ) ); vec.push_back(std::make_pair(quote_string("name"), quote_string(t->name) ) ); @@ -775,7 +775,7 @@ std::string gen_json_struc(_obj* o) } case _obj::block_cmd : { - cmd* t = dynamic_cast(o); + cmd_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("cmd") ) ); vec.push_back(std::make_pair(quote_string("args"), gen_json_struc(t->args))); @@ -809,7 +809,7 @@ std::string gen_json_struc(_obj* o) } case _obj::block_case : { - case_block* t = dynamic_cast(o); + case_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("case") ) ); vec.push_back(std::make_pair(quote_string("carg"), gen_json_struc(t->carg))); @@ -838,7 +838,7 @@ std::string gen_json_struc(_obj* o) } case _obj::block_if : { - if_block* t = dynamic_cast(o); + if_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("if") ) ); std::vector condblocks; @@ -863,7 +863,7 @@ std::string gen_json_struc(_obj* o) } case _obj::block_for : { - for_block* t = dynamic_cast(o); + for_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("for") ) ); vec.push_back(std::make_pair(quote_string("var"), gen_json_struc(t->var))); vec.push_back(std::make_pair(quote_string("iter"), gen_json_struc(t->iter))); @@ -878,7 +878,7 @@ std::string gen_json_struc(_obj* o) } case _obj::block_while : { - while_block* t = dynamic_cast(o); + while_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("while") ) ); vec.push_back(std::make_pair(quote_string("cond"), gen_json_struc(t->cond) ) ); vec.push_back(std::make_pair(quote_string("ops"), gen_json_struc(t->ops) ) ); @@ -892,21 +892,21 @@ std::string gen_json_struc(_obj* o) } case _obj::subarg_variable : { - variable_subarg* t = dynamic_cast(o); + subarg_variable_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("subarg_variable") ) ); vec.push_back(std::make_pair(quote_string("var"), gen_json_struc(t->var) ) ); break; } case _obj::subarg_subshell : { - subshell_subarg* t = dynamic_cast(o); + subarg_subshell_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("subarg_subshell") ) ); vec.push_back(std::make_pair(quote_string("sbsh"), gen_json_struc(t->sbsh) ) ); break; } case _obj::subarg_procsub : { - procsub_subarg* t = dynamic_cast(o); + subarg_procsub_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("subarg_procsub") ) ); vec.push_back(std::make_pair(quote_string("is_output"), boolstring(t->is_output) ) ); vec.push_back(std::make_pair(quote_string("sbsh"), gen_json_struc(t->sbsh) ) ); @@ -914,35 +914,35 @@ std::string gen_json_struc(_obj* o) } case _obj::subarg_arithmetic : { - arithmetic_subarg* t = dynamic_cast(o); + subarg_arithmetic_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("subarg_arithmetic") ) ); vec.push_back(std::make_pair(quote_string("arith"), gen_json_struc(t->arith) ) ); break; } case _obj::subarg_string : { - string_subarg* t = dynamic_cast(o); + subarg_string_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("subarg_string") ) ); vec.push_back(std::make_pair(quote_string("val"), quote_string(t->val) ) ); break; } case _obj::arithmetic_variable : { - variable_arithmetic* t = dynamic_cast(o); + arithmetic_variable_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("arithmetic_variable") ) ); vec.push_back(std::make_pair(quote_string("var"), gen_json_struc(t->var) ) ); break; } case _obj::arithmetic_subshell : { - subshell_arithmetic* t = dynamic_cast(o); + arithmetic_subshell_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("arithmetic_subshell") ) ); vec.push_back(std::make_pair(quote_string("sbsh"), gen_json_struc(t->sbsh) ) ); break; } case _obj::arithmetic_operation : { - operation_arithmetic* t = dynamic_cast(o); + arithmetic_operation_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("arithmetic_operation") ) ); vec.push_back(std::make_pair(quote_string("val1"), gen_json_struc(t->val1) ) ); vec.push_back(std::make_pair(quote_string("val2"), gen_json_struc(t->val2) ) ); @@ -950,14 +950,14 @@ std::string gen_json_struc(_obj* o) } case _obj::arithmetic_parenthesis : { - parenthesis_arithmetic* t = dynamic_cast(o); + arithmetic_parenthesis_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("arithmetic_parenthesis") ) ); vec.push_back(std::make_pair(quote_string("val"), gen_json_struc(t->val) ) ); break; } case _obj::arithmetic_number : { - number_arithmetic* t = dynamic_cast(o); + arithmetic_number_t* t = dynamic_cast(o); vec.push_back(std::make_pair(quote_string("type"), quote_string("arithmetic_number") ) ); vec.push_back(std::make_pair(quote_string("val"), quote_string(t->val) ) ); break; diff --git a/src/resolve.cpp b/src/resolve.cpp index 4249cef..210b901 100644 --- a/src/resolve.cpp +++ b/src/resolve.cpp @@ -59,7 +59,7 @@ void _cd(std::string const& dir) // -- COMMANDS -- // return [] -std::vector> do_include_raw(condlist* cmd, parse_context ctx, std::string* ex_dir) +std::vector> do_include_raw(condlist_t* cmd, parse_context ctx, std::string* ex_dir) { std::vector> ret; @@ -105,7 +105,7 @@ std::vector> do_include_raw(condlist* cmd, p } // -std::pair do_resolve_raw(condlist* cmd, parse_context ctx, std::string* ex_dir) +std::pair do_resolve_raw(condlist_t* cmd, parse_context ctx, std::string* ex_dir) { std::pair ret; @@ -152,9 +152,9 @@ std::pair do_resolve_raw(condlist* cmd, parse_context return ret; } -std::vector do_include_parse(condlist* cmd, parse_context ctx) +std::vector do_include_parse(condlist_t* cmd, parse_context ctx) { - std::vector ret; + std::vector ret; std::string dir; auto incs=do_include_raw(cmd, ctx, &dir); @@ -186,9 +186,9 @@ std::vector do_include_parse(condlist* cmd, parse_context ctx) } // if first is nullptr: is a string -std::vector do_resolve_parse(condlist* cmd, parse_context ctx) +std::vector do_resolve_parse(condlist_t* cmd, parse_context ctx) { - std::vector ret; + std::vector ret; std::pair p; try @@ -220,11 +220,11 @@ std::vector do_resolve_parse(condlist* cmd, parse_context ctx) // -- OBJECT CALLS -- -std::pair< std::vector , bool > resolve_condlist(condlist* in, parse_context ctx) +std::pair< std::vector , bool > resolve_condlist(condlist_t* in, parse_context ctx) { - cmd* tc = in->first_cmd(); + cmd_t* tc = in->first_cmd(); if(tc == nullptr) - return std::make_pair(std::vector(), false); + return std::make_pair(std::vector(), false); std::string const& strcmd=tc->arg_string(0); @@ -233,17 +233,17 @@ std::pair< std::vector , bool > resolve_condlist(condlist* in, parse_ else if(g_resolve && strcmd == "%resolve") return std::make_pair(do_resolve_parse(in, ctx), true); else - return std::make_pair(std::vector(), false); + return std::make_pair(std::vector(), false); } -std::pair< std::vector , bool > resolve_arg(arg* in, parse_context ctx, bool forcequote=false) +std::pair< std::vector , bool > resolve_arg(arg_t* in, parse_context ctx, bool forcequote=false) { - std::vector ret; + std::vector ret; if(in == nullptr) { return std::make_pair(ret, false); } - arg* ta=nullptr; + arg_t* ta=nullptr; bool has_resolved=false; uint32_t j=0; for(uint32_t i=0 ; isize() ; i++) @@ -251,11 +251,11 @@ std::pair< std::vector , bool > resolve_arg(arg* in, parse_context ctx, bo if(in->sa[i]->type != _obj::subarg_subshell) // skip if not subshell continue; - subshell_subarg* tsh = dynamic_cast(in->sa[i]); + subarg_subshell_t* tsh = dynamic_cast(in->sa[i]); if(tsh->sbsh->lst->cls.size() != 1) // skip if not one cl continue; - condlist* tc = tsh->sbsh->lst->cls[0]; - cmd* c = tc->first_cmd(); + condlist_t* tc = tsh->sbsh->lst->cls[0]; + cmd_t* c = tc->first_cmd(); if(c == nullptr) // skip if not cmd continue; std::string strcmd=c->arg_string(0); @@ -294,7 +294,7 @@ std::pair< std::vector , bool > resolve_arg(arg* in, parse_context ctx, bo { // replace with new subarg delete in->sa[i]; - in->sa[i] = new string_subarg(fulltext); + in->sa[i] = new subarg_string_t(fulltext); } else { @@ -305,21 +305,21 @@ std::pair< std::vector , bool > resolve_arg(arg* in, parse_context ctx, bo if(strargs.size() == 1) val = strargs[0]; delete in->sa[i]; - in->sa[i] = new string_subarg(val); + in->sa[i] = new subarg_string_t(val); } else // pack { if(ta == nullptr) - ta = new arg; + ta = new arg_t; ta->sa.insert(ta->sa.end(), in->sa.begin()+j, in->sa.begin()+i); - ta->add(new string_subarg(strargs[i])); + ta->add(new subarg_string_t(strargs[i])); j=i+1; delete in->sa[i]; for(uint32_t li=1 ; liadd(new string_subarg(strargs[li])); + ta = new arg_t; + ta->add(new subarg_string_t(strargs[li])); } } // end pack @@ -350,9 +350,9 @@ bool r_resolve(_obj* o, parse_context* ct) // check every sub-object // execute resolve manually // instruct parent resolve to not resolve - case _obj::_list : + case _obj::list : { - auto t = dynamic_cast(o); + auto t = dynamic_cast(o); for(uint32_t i=0 ; icls.size() ; i++) { auto r=resolve_condlist(t->cls[i], *ct); @@ -373,9 +373,9 @@ bool r_resolve(_obj* o, parse_context* ct) } return false; } break; - case _obj::_arglist : + case _obj::arglist : { - auto t = dynamic_cast(o); + auto t = dynamic_cast(o); for(uint32_t i=0 ; isize() ; i++) { auto r=resolve_arg(t->args[i], *ct); @@ -397,7 +397,7 @@ bool r_resolve(_obj* o, parse_context* ct) } break; case _obj::block_cmd : { - auto t = dynamic_cast(o); + auto t = dynamic_cast(o); for(auto it: t->var_assigns) // var assigns { resolve_arg(it.second, *ct, true); // force quoted @@ -415,7 +415,7 @@ bool r_resolve(_obj* o, parse_context* ct) }; break; case _obj::block_case : { - auto t = dynamic_cast(o); + auto t = dynamic_cast(o); for(auto sc: t->cases) { resolve_arg(t->carg, *ct, true); // force quoted diff --git a/src/shellcode.cpp b/src/shellcode.cpp index 84e7edf..d485fc0 100644 --- a/src/shellcode.cpp +++ b/src/shellcode.cpp @@ -7,7 +7,7 @@ const std::map lxsh_extend_fcts = { { "_lxsh_random", { "[K]", "Generate a random number between 0 and 2^(K*8). Default 2", RANDOM_SH} }, { "_lxsh_random_string", { "[N]", "Generate a random alphanumeric string of length N. Default 20", RANDOM_STRING_SH} }, - { "_lxsh_random_tmpfile", { "[N]", "Get a random TMP filepath, with N random chars. Default 20", RANDOM_TMPFILE_SH, {"_lxsh_random_string"} } + { "_lxsh_random_tmpfile", { "[PREFIX] [N]", "Get a random TMP filepath, with N random chars. Default 20", RANDOM_TMPFILE_SH, {"_lxsh_random_string"} } } }; diff --git a/src/struc.cpp b/src/struc.cpp index c2eeb40..8f2945f 100644 --- a/src/struc.cpp +++ b/src/struc.cpp @@ -5,13 +5,11 @@ #include -std::string g_origin=""; +const std::string cmd_t::empty_string=""; -const std::string cmd::empty_string=""; - -condlist::condlist(block* bl) +condlist_t::condlist_t(block_t* bl) { - type=_obj::_condlist; + type=_obj::condlist; parallel=false; - this->add(new pipeline(bl)); + this->add(new pipeline_t(bl)); } diff --git a/src/struc_helper.cpp b/src/struc_helper.cpp index e1e2a96..b47a5c9 100644 --- a/src/struc_helper.cpp +++ b/src/struc_helper.cpp @@ -7,107 +7,107 @@ // makers -arg* make_arg(std::string const& in) +arg_t* make_arg(std::string const& in) { return parse_arg(make_context(in)).first; } -cmd* make_cmd(std::vector const& args) +cmd_t* make_cmd(std::vector const& args) { - cmd* ret = new cmd; - ret->args = new arglist; + cmd_t* ret = new cmd_t; + ret->args = new arglist_t; for(auto it: args) - ret->args->add(new arg(it)); + ret->args->add(new arg_t(it)); return ret; } -cmd* make_cmd(std::vector const& args) +cmd_t* make_cmd(std::vector const& args) { - cmd* ret = new cmd; - ret->args = new arglist; + cmd_t* ret = new cmd_t; + ret->args = new arglist_t; for(auto it: args) - ret->args->add(new arg(it)); + ret->args->add(new arg_t(it)); return ret; } -cmd* make_cmd(std::vector const& args) +cmd_t* make_cmd(std::vector const& args) { - cmd* ret = new cmd; - ret->args = new arglist; + cmd_t* ret = new cmd_t; + ret->args = new arglist_t; for(auto it: args) ret->args->add(it); return ret; } -cmd* make_cmd(std::string const& in) +cmd_t* make_cmd(std::string const& in) { return parse_cmd(make_context(in)).first; } -pipeline* make_pipeline(std::vector const& bls) +pipeline_t* make_pipeline(std::vector const& bls) { - pipeline* ret = new pipeline; + pipeline_t* ret = new pipeline_t; for(auto it: bls) ret->add(it); return ret; } -pipeline* make_pipeline(std::string const& in) +pipeline_t* make_pipeline(std::string const& in) { return parse_pipeline(make_context(in)).first; } -condlist* make_condlist(std::string const& in) +condlist_t* make_condlist(std::string const& in) { return parse_condlist(make_context(in)).first; } -list* make_list(std::string const& in) +list_t* make_list(std::string const& in) { auto t = parse_list_until(make_context(in)); return std::get<0>(t); } -block* make_block(std::string const& in) +block_t* make_block(std::string const& in) { return parse_block(make_context(in)).first; } -cmd* make_printf(arg* in) +cmd_t* make_printf(arg_t* in) { - cmd* prnt = make_cmd(std::vector({"printf", "%s\\\\n"})); + cmd_t* prnt = make_cmd(std::vector({"printf", "%s\\\\n"})); force_quotes(in); prnt->add(in); return prnt; } -arithmetic* make_arithmetic(arg* a) +arithmetic_t* make_arithmetic(arg_t* a) { if(a->sa.size() != 1) { - cmd* prnt = make_printf(a); - return new subshell_arithmetic(new subshell(prnt)); + cmd_t* prnt = make_printf(a); + return new arithmetic_subshell_t(new subshell_t(prnt)); } - arithmetic* ret=nullptr; + arithmetic_t* ret=nullptr; switch(a->sa[0]->type) { case _obj::subarg_string : { - string_subarg* t = dynamic_cast(a->sa[0]); - ret = new number_arithmetic(t->val); + subarg_string_t* t = dynamic_cast(a->sa[0]); + ret = new arithmetic_number_t(t->val); }; break; case _obj::subarg_variable : { - variable_subarg* t = dynamic_cast(a->sa[0]); - ret = new variable_arithmetic(t->var); + subarg_variable_t* t = dynamic_cast(a->sa[0]); + ret = new arithmetic_variable_t(t->var); t->var = nullptr; }; break; case _obj::subarg_subshell : { - subshell_subarg* t = dynamic_cast(a->sa[0]); - ret = new subshell_arithmetic(t->sbsh); + subarg_subshell_t* t = dynamic_cast(a->sa[0]); + ret = new arithmetic_subshell_t(t->sbsh); t->sbsh = nullptr; }; break; case _obj::subarg_arithmetic : { - arithmetic_subarg* t = dynamic_cast(a->sa[0]); + subarg_arithmetic_t* t = dynamic_cast(a->sa[0]); ret = t->arith; t->arith = nullptr; }; break; @@ -117,22 +117,22 @@ arithmetic* make_arithmetic(arg* a) return ret; } -arithmetic* make_arithmetic(arg* arg1, std::string op, arg* arg2) +arithmetic_t* make_arithmetic(arg_t* arg1, std::string op, arg_t* arg2) { - return new operation_arithmetic(op, make_arithmetic(arg1), make_arithmetic(arg2)); + return new arithmetic_operation_t(op, make_arithmetic(arg1), make_arithmetic(arg2)); } // copy -arg* copy(arg* in) { +arg_t* copy(arg_t* in) { std::string str = in->generate(0); return parse_arg(make_context(str)).first; } // modifiers -void force_quotes(arg* in) +void force_quotes(arg_t* in) { for(uint32_t i=0; i < in->sa.size() ; i++) { @@ -146,24 +146,24 @@ void force_quotes(arg* in) } } -void add_quotes(arg* in) +void add_quotes(arg_t* in) { for(uint32_t i=0; i < in->sa.size() ; i++) in->sa[i]->quoted=true; - in->insert(0, new string_subarg("\"")); + in->insert(0, new subarg_string_t("\"")); in->add("\""); } // ** TESTERS ** // -bool arg_has_char(char c, arg* in) +bool arg_has_char(char c, arg_t* in) { for(auto it: in->sa) { if(it->type == _obj::subarg_string) { - string_subarg* t = dynamic_cast(it); + subarg_string_t* t = dynamic_cast(it); if(t->val.find(c) != std::string::npos) return true; } @@ -171,7 +171,7 @@ bool arg_has_char(char c, arg* in) return false; } -bool possibly_expands(arg* in) +bool possibly_expands(arg_t* in) { for(auto it: in->sa) if( (it->type == _obj::subarg_subshell || it->type == _obj::subarg_variable ) && it->quoted == false) @@ -179,7 +179,7 @@ bool possibly_expands(arg* in) return false; } -bool possibly_expands(arglist* in) +bool possibly_expands(arglist_t* in) { for(auto it: in->args) if(possibly_expands(it)) @@ -193,7 +193,7 @@ bool possibly_expands(arglist* in) // property getters -bool cmd::has_var_assign() +bool cmd_t::has_var_assign() { if(this->args == nullptr || this->args->size() == 0) { @@ -202,7 +202,7 @@ bool cmd::has_var_assign() return this->is_argvar(); } -size_t cmd::arglist_size() +size_t cmd_t::arglist_size() { if(args==nullptr) return 0; @@ -212,26 +212,26 @@ size_t cmd::arglist_size() // string getters -bool arg::is_string() +bool arg_t::is_string() { return sa.size() == 1 && sa[0]->type == _obj::subarg_string; } -std::string arg::string() +std::string arg_t::string() { if(!this->is_string()) return ""; - return dynamic_cast(sa[0])->val; + return dynamic_cast(sa[0])->val; } -std::string arg::first_sa_string() +std::string arg_t::first_sa_string() { if(sa.size() <=0 || sa[0]->type != _obj::subarg_string) return ""; - return dynamic_cast(sa[0])->val; + return dynamic_cast(sa[0])->val; } -bool arg::can_expand() +bool arg_t::can_expand() { for(auto it: sa) { @@ -241,7 +241,7 @@ bool arg::can_expand() return false; } -bool arglist::can_expand() +bool arglist_t::can_expand() { bool arg_expands=false; for(auto it: args) @@ -253,7 +253,7 @@ bool arglist::can_expand() return false; } -std::vector arglist::strargs(uint32_t start) +std::vector arglist_t::strargs(uint32_t start) { std::vector ret; bool t=opt_minify; @@ -266,16 +266,16 @@ std::vector arglist::strargs(uint32_t start) return ret; } -std::string const& cmd::arg_string(uint32_t n) +std::string const& cmd_t::arg_string(uint32_t n) { if(args!=nullptr && args->args.size()>n && args->args[n]->sa.size() == 1 && args->args[n]->sa[0]->type == _obj::subarg_string) - return dynamic_cast(args->args[n]->sa[0])->val; - return cmd::empty_string; + return dynamic_cast(args->args[n]->sa[0])->val; + return cmd_t::empty_string; } // subobject getters -block* condlist::first_block() +block_t* condlist_t::first_block() { if(pls.size() > 0 && pls[0]->cmds.size() > 0) return (pls[0]->cmds[0]); @@ -283,40 +283,40 @@ block* condlist::first_block() return nullptr; } -cmd* condlist::first_cmd() +cmd_t* condlist_t::first_cmd() { if(pls.size() > 0 && pls[0]->cmds.size() > 0 && pls[0]->cmds[0]->type == _obj::block_cmd) - return dynamic_cast(pls[0]->cmds[0]); + return dynamic_cast(pls[0]->cmds[0]); else return nullptr; } -cmd* brace::single_cmd() +cmd_t* brace_t::single_cmd() { if( lst->cls.size() == 1 && // only one condlist lst->cls[0]->pls.size() == 1 && // only one pipeline lst->cls[0]->pls[0]->cmds.size() == 1 && // only one block lst->cls[0]->pls[0]->cmds[0]->type == _obj::block_cmd) // block is a command - return dynamic_cast(lst->cls[0]->pls[0]->cmds[0]); // return command + return dynamic_cast(lst->cls[0]->pls[0]->cmds[0]); // return command return nullptr; } -cmd* subshell::single_cmd() +cmd_t* subshell_t::single_cmd() { if( lst->cls.size() == 1 && // only one condlist lst->cls[0]->pls.size() == 1 && // only one pipeline lst->cls[0]->pls[0]->cmds.size() == 1 && // only one block lst->cls[0]->pls[0]->cmds[0]->type == _obj::block_cmd) // block is a command - return dynamic_cast(lst->cls[0]->pls[0]->cmds[0]); // return command + return dynamic_cast(lst->cls[0]->pls[0]->cmds[0]); // return command return nullptr; } -cmd* block::single_cmd() +cmd_t* block_t::single_cmd() { if(this->type == _obj::block_subshell) - return dynamic_cast(this)->single_cmd(); + return dynamic_cast(this)->single_cmd(); if(this->type == _obj::block_brace) - return dynamic_cast(this)->single_cmd(); + return dynamic_cast(this)->single_cmd(); return nullptr; } @@ -324,15 +324,15 @@ cmd* block::single_cmd() // simple setters -void arg::set(std::string const& str) +void arg_t::set(std::string const& str) { for(auto it: sa) delete it; sa.resize(0); - sa.push_back(new string_subarg(str)); + sa.push_back(new subarg_string_t(str)); } -void condlist::prune_first_cmd() +void condlist_t::prune_first_cmd() { if(pls.size()>0 && pls[0]->cmds.size()>0) { @@ -343,40 +343,40 @@ void condlist::prune_first_cmd() // add/extend -void arg::insert(uint32_t i, std::string const& in) +void arg_t::insert(uint32_t i, std::string const& in) { if(i>0 && i<=sa.size() && sa[i-1]->type == _obj::subarg_string) { - string_subarg* t = dynamic_cast(sa[i-1]); + subarg_string_t* t = dynamic_cast(sa[i-1]); t->val += in; } else if(itype == _obj::subarg_string) { - string_subarg* t = dynamic_cast(sa[i]); + subarg_string_t* t = dynamic_cast(sa[i]); t->val = in + t->val; } else - sa.insert(sa.begin()+i, new string_subarg(in)); + sa.insert(sa.begin()+i, new subarg_string_t(in)); } -void arg::add(std::string const& in) +void arg_t::add(std::string const& in) { this->insert(this->size(), in); } -void arg::insert(uint32_t i, subarg* val) +void arg_t::insert(uint32_t i, subarg_t* val) { if(val->type == _obj::subarg_string) { - string_subarg* tval = dynamic_cast(val); + subarg_string_t* tval = dynamic_cast(val); if(i>0 && i<=sa.size() && sa[i-1]->type == _obj::subarg_string) { - string_subarg* t = dynamic_cast(sa[i-1]); + subarg_string_t* t = dynamic_cast(sa[i-1]); t->val += tval->val; delete val; } else if(itype == _obj::subarg_string) { - string_subarg* t = dynamic_cast(sa[i]); + subarg_string_t* t = dynamic_cast(sa[i]); t->val = tval->val + t->val; delete val; } @@ -386,43 +386,43 @@ void arg::insert(uint32_t i, subarg* val) else sa.insert(sa.begin()+i, val); } -void arg::insert(uint32_t i, arg const& a) +void arg_t::insert(uint32_t i, arg_t const& a) { sa.insert(sa.begin()+i, a.sa.begin(), a.sa.end()); } -void arglist::insert(uint32_t i, arg* val) +void arglist_t::insert(uint32_t i, arg_t* val) { args.insert(args.begin()+i, val); } -void arglist::insert(uint32_t i, arglist const& lst) +void arglist_t::insert(uint32_t i, arglist_t const& lst) { args.insert(args.begin()+i, lst.args.begin(), lst.args.end()); } -void cmd::add(arg* in) +void cmd_t::add(arg_t* in) { if(args==nullptr) - args = new arglist; + args = new arglist_t; args->add(in); } -void condlist::add(pipeline* pl, bool or_op) +void condlist_t::add(pipeline_t* pl, bool or_op) { if(pls.size() > 0) or_ops.push_back(or_op); pls.push_back(pl); } -void list::insert(uint32_t i, condlist* val) +void list_t::insert(uint32_t i, condlist_t* val) { if(i<0) cls.insert(cls.end(), val); else cls.insert(cls.begin()+i, val); } -void list::insert(uint32_t i, list const& lst) +void list_t::insert(uint32_t i, list_t const& lst) { if(i<0) cls.insert(cls.end(), lst.cls.begin(), lst.cls.end()); @@ -440,7 +440,7 @@ void shmain::concat(shmain* in) // special modifiers -void condlist::negate() +void condlist_t::negate() { // invert commands for(uint32_t i=0; i