diff --git a/include/debashify.hpp b/include/debashify.hpp index 8c56b16..7fb96d0 100644 --- a/include/debashify.hpp +++ b/include/debashify.hpp @@ -6,13 +6,13 @@ #include #include -typedef struct debashify_params { +struct debashify_params { std::set required_fcts; void require_fct(std::string const& in) { required_fcts.insert(in); } // map of detected arrays // bool value: is associative std::map arrays; -} debashify_params; +}; bool r_debashify(_obj* o, debashify_params* params); diff --git a/include/errcodes.h b/include/errcodes.h index 25ce563..cf48b04 100644 --- a/include/errcodes.h +++ b/include/errcodes.h @@ -1,9 +1,9 @@ #ifndef ERRCODES_H #define ERRCODES_H -#define ERR_HELP 1001 -#define ERR_OPT 1002 -#define ERR_PARSE 1003 -#define ERR_RUNTIME 1004 +#define ERR_HELP 101 +#define ERR_OPT 102 +#define ERR_PARSE 103 +#define ERR_RUNTIME 104 #endif //ERRCODES_H diff --git a/include/parse.hpp b/include/parse.hpp index 6f9651c..e9eda8c 100644 --- a/include/parse.hpp +++ b/include/parse.hpp @@ -7,8 +7,6 @@ #include #include -#include - #define SPACES " \t" #define SEPARATORS " \t\n" #define ARG_END " \t\n;#()&|<>" @@ -27,7 +25,7 @@ #define ARRAY_ARG_END " \t\n;#()&|<>]" // macros -#define PARSE_ERROR(str, i) ztd::format_error(str, "", in, i) +#define PARSE_ERROR(str, i) format_error(str, "", in, i) // globals diff --git a/include/shellcode.hpp b/include/shellcode.hpp index aec424f..b6ab058 100644 --- a/include/shellcode.hpp +++ b/include/shellcode.hpp @@ -15,9 +15,9 @@ struct lxsh_fct { std::vector depends_on=std::vector(); }; -extern const std::map lxsh_extend_fcts; -extern const std::map lxsh_array_fcts; -extern const std::map lxsh_allfcts; +extern const std::map lxsh_extend_fcts; +extern const std::map lxsh_array_fcts; +extern const std::map lxsh_allfcts; void add_lxsh_fcts(shmain* sh, std::set fcts); diff --git a/include/struc.hpp b/include/struc.hpp index 7ffec33..a7b56f5 100644 --- a/include/struc.hpp +++ b/include/struc.hpp @@ -62,6 +62,31 @@ subarg: can be one of */ +// exceptions +class format_error : public std::exception +{ +public: + //! @brief Conctructor + inline format_error(const std::string& what, const std::string& origin, const std::string& data, int where) { desc=what; index=where; filename=origin; sdat=data; } + + //! @brief Error message + inline const char * what () const throw () {return desc.c_str();} + //! @brief Origin of the data, name of imported file, otherwise empty if generated + inline const char * origin() const throw () {return filename.c_str();} + //! @brief Data causing the exception + inline const char * data() const throw () {return sdat.c_str();} + //! @brief Where the error is located in the data + inline const int where () const throw () {return index;} +private: + std::string desc; + int index; + std::string filename; + std::string sdat; +}; + +// objects + + #define AND_OP false #define OR_OP true diff --git a/include/util.hpp b/include/util.hpp index 53c6f4c..3c2f871 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -12,8 +12,6 @@ #include #include -#include - #include "struc.hpp" extern std::string indenting_string; @@ -149,7 +147,7 @@ int _exec(std::string const& bin, std::vector const& args); std::string stringReplace(std::string subject, const std::string& search, const std::string& replace); -void printFormatError(ztd::format_error const& e, bool print_line=true); +void printFormatError(format_error const& e, bool print_line=true); void printErrorIndex(const char* in, const int index, const std::string& message, const std::string& origin, bool print_line=true); int execute(shmain* sh, std::vector& args); diff --git a/src/exec.cpp b/src/exec.cpp index 31bcc87..8ed7f20 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -51,9 +51,9 @@ std::vector do_resolve_exec(condlist* cmd, std::string const& filenam // cd back _cd(dir); } - catch(ztd::format_error& e) + catch(format_error& e) { - throw ztd::format_error(e.what(), '`'+p.first+'`', e.data(), e.where()); + throw format_error(e.what(), '`'+p.first+'`', e.data(), e.where()); } return ret; @@ -187,9 +187,9 @@ void parse_exec(FILE* fd, const char* in, uint32_t size, std::string const& file delete t_lst; #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { - throw ztd::format_error(e.what(), filename, in, e.where()); + throw format_error(e.what(), filename, in, e.where()); } #endif } diff --git a/src/main.cpp b/src/main.cpp index 82ef7d8..a8f9385 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,13 @@ #include #include +#include + +#include #include #include -#include - #include "util.hpp" #include "struc.hpp" #include "parse.hpp" @@ -29,6 +30,7 @@ int main(int argc, char* argv[]) bool optstop=false; + shmain *sh=nullptr, *tsh=nullptr; try { args=options.process(argc, argv, {.stop_on_argument=true, .output_doubledash=true} ); @@ -37,48 +39,41 @@ int main(int argc, char* argv[]) optstop=true; args.erase(args.begin()); } - } - catch(std::exception& e) - { - std::cerr << e.what() << std::endl; - return ERR_OPT; - } - oneshot_opt_process(argv[0]); - // resolve input - std::string file; - if(args.size() > 0) // argument provided - { - if(args[0] == "-" || args[0] == "/dev/stdin") //stdin + oneshot_opt_process(argv[0]); + + // resolve input + std::string file; + if(args.size() > 0) // argument provided { - file = "/dev/stdin"; + if(args[0] == "-" || args[0] == "/dev/stdin") //stdin + { + file = "/dev/stdin"; + } + else + { + file=args[0]; + } } else { - file=args[0]; + if(isatty(fileno(stdin))) // stdin is interactive + { + print_help(argv[0]); + return ERR_HELP; + } + else // is piped + { + file = "/dev/stdin"; + args.push_back("/dev/stdin"); + } } - } - else - { - if(isatty(fileno(stdin))) // stdin is interactive - { - print_help(argv[0]); - return ERR_HELP; - } - else // is piped - { - file = "/dev/stdin"; - args.push_back("/dev/stdin"); - } - } - // parsing + // parsing + + sh = new shmain(new list); - shmain* sh = new shmain(new list); - shmain* tsh = nullptr; - try - { bool is_exec = false; bool first_run = true; @@ -219,7 +214,7 @@ int main(int argc, char* argv[]) } } #ifndef NO_PARSE_CATCH - catch(ztd::format_error& e) + catch(format_error& e) { if(tsh != nullptr) delete tsh; @@ -228,11 +223,17 @@ int main(int argc, char* argv[]) return ERR_PARSE; } #endif + catch(ztd::option_error& e) + { + std::cerr << e.what() << std::endl; + return ERR_OPT; + } catch(std::runtime_error& e) { if(tsh != nullptr) delete tsh; - delete sh; + if(sh != nullptr) + delete sh; std::cerr << e.what() << std::endl; return ERR_RUNTIME; } diff --git a/src/options.cpp b/src/options.cpp index dce2c6c..dcb7e9b 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -7,55 +7,49 @@ #include "version.h" #include "g_version.h" -ztd::option_set options = gen_options(); bool opt_minify=false; +ztd::option_set options( { + ztd::option("\r [Help]"), + ztd::option('h', "help", false, "Display this help message"), + ztd::option("version", false, "Display version"), + ztd::option("help-link-commands", false, "Print help for linker commands"), + ztd::option("help-extend-fcts", false, "Print help for lxsh extension functions"), + ztd::option("\r [Output]"), + ztd::option('o', "output", true , "Output result script to file", "file"), + ztd::option('c', "stdout", false, "Output result script to stdout"), + ztd::option('e', "exec", false, "Directly execute script"), + ztd::option("no-shebang", false, "Don't output shebang"), + ztd::option('J', "json", false, "Output the json structure"), + ztd::option("\r [Processing]"), + ztd::option('m', "minify", false, "Minify code without changing functionality"), + ztd::option('M', "minify-full", false, "Enable all minifying features: -m --minify-quotes --minify-var --minify-fct --remove-unused"), + ztd::option("minify-quotes", false, "Remove unnecessary quotes"), + ztd::option('C', "no-cd", false, "Don't cd when doing %include and %resolve"), + ztd::option('I', "no-include", false, "Don't resolve %include commands"), + ztd::option('R', "no-resolve", false, "Don't resolve %resolve commands"), + ztd::option("no-extend", false, "Don't add lxsh extension functions"), + ztd::option("debashify", false, "Attempt to turn a bash-specific script into a POSIX shell script"), + ztd::option("\r [var/fct processing]"), + ztd::option("minify-var", false, "Minify variable names"), + ztd::option("minify-fct", false, "Minify function names"), + ztd::option("exclude-var", true, "List of matching regex to ignore for variable processing", "list"), + ztd::option("exclude-fct", true, "List of matching regex to ignore for function processing", "list"), + ztd::option("no-exclude-reserved",false, "Don't exclude reserved variables"), + ztd::option("list-var", false, "List all variables set and invoked in the script"), + ztd::option("list-var-def", false, "List all variables set in the script"), + ztd::option("list-var-call", false, "List all variables invoked in the script"), + ztd::option("list-fct", false, "List all functions defined in the script"), + ztd::option("list-cmd", false, "List all commands invoked in the script"), + ztd::option("remove-unused", false, "Remove unused functions and variables"), + ztd::option("unset-var", false, "Add 'unset' to all vars at the start of the script to avoid environment interference") +} ); + bool g_cd=false; bool g_include=true; bool g_resolve=true; bool g_shebang=true; -ztd::option_set gen_options() -{ - ztd::option_set ret; - ret.add( - ztd::option("\r [Help]"), - ztd::option('h', "help", false, "Display this help message"), - ztd::option("version", false, "Display version"), - ztd::option("help-link-commands", false, "Print help for linker commands"), - ztd::option("help-extend-fcts", false, "Print help for lxsh extension functions"), - ztd::option("\r [Output]"), - ztd::option('o', "output", true , "Output result script to file", "file"), - ztd::option('c', "stdout", false, "Output result script to stdout"), - ztd::option('e', "exec", false, "Directly execute script"), - ztd::option("no-shebang", false, "Don't output shebang"), - ztd::option('J', "json", false, "Output the json structure"), - ztd::option("\r [Processing]"), - ztd::option('m', "minify", false, "Minify code without changing functionality"), - ztd::option('M', "minify-full", false, "Enable all minifying features: -m --minify-quotes --minify-var --minify-fct --remove-unused"), - ztd::option("minify-quotes", false, "Remove unnecessary quotes"), - ztd::option('C', "no-cd", false, "Don't cd when doing %include and %resolve"), - ztd::option('I', "no-include", false, "Don't resolve %include commands"), - ztd::option('R', "no-resolve", false, "Don't resolve %resolve commands"), - ztd::option("no-extend", false, "Don't add lxsh extension functions"), - ztd::option("debashify", false, "Attempt to turn a bash-specific script into a POSIX shell script"), - ztd::option("\r [var/fct processing]"), - ztd::option("minify-var", false, "Minify variable names"), - ztd::option("minify-fct", false, "Minify function names"), - ztd::option("exclude-var", true, "List of matching regex to ignore for variable processing", "list"), - ztd::option("exclude-fct", true, "List of matching regex to ignore for function processing", "list"), - ztd::option("no-exclude-reserved",false, "Don't exclude reserved variables"), - ztd::option("list-var", false, "List all variables set and invoked in the script"), - ztd::option("list-var-def", false, "List all variables set in the script"), - ztd::option("list-var-call", false, "List all variables invoked in the script"), - ztd::option("list-fct", false, "List all functions defined in the script"), - ztd::option("list-cmd", false, "List all commands invoked in the script"), - ztd::option("remove-unused", false, "Remove unused functions and variables"), - ztd::option("unset-var", false, "Add 'unset' to all vars at the start of the script to avoid environment interference") - ); - return ret; -} - void get_opts() { g_cd=!options['C'].activated; diff --git a/src/parse.cpp b/src/parse.cpp index 4d58df4..bb8e7ef 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -259,7 +259,7 @@ std::pair parse_arithmetic(const char* in, uint32_t size, } #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -315,7 +315,7 @@ std::pair parse_manipulation(const char* in, uint32_t size, #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { if(ret != nullptr) delete ret; throw e; @@ -482,7 +482,7 @@ std::pair parse_arg(const char* in, uint32_t size, uint32_t star #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -624,7 +624,7 @@ std::pair parse_redirect(const char* in, uint32_t size, uin } #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { if(ret!=nullptr) delete ret; @@ -732,7 +732,7 @@ std::pair parse_arglist(const char* in, uint32_t size, uint3 #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { if(ret != nullptr) delete ret; @@ -773,7 +773,7 @@ std::pair parse_pipeline(const char* in, uint32_t size, uin } #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -829,7 +829,7 @@ std::pair parse_condlist(const char* in, uint32_t size, uin } #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -882,7 +882,7 @@ std::pair parse_list_until(const char* in, uint32_t size, uint3 } #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -936,7 +936,7 @@ std::pair parse_list_until(const char* in, uint32_t size, uint3 g_expecting=old_expect; #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -1004,7 +1004,7 @@ std::tuple parse_list_until(const char* in, uint32 g_expecting=old_expect; #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -1033,7 +1033,7 @@ std::pair parse_subshell(const char* in, uint32_t size, uin i++; #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -1063,7 +1063,7 @@ std::pair parse_brace(const char* in, uint32_t size, uint32_t i++; #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -1099,7 +1099,7 @@ std::pair parse_function(const char* in, uint32_t size, uin i++; #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -1236,7 +1236,7 @@ std::pair parse_cmd(const char* in, uint32_t size, uint32_t star #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -1322,7 +1322,7 @@ std::pair parse_case(const char* in, uint32_t size, uint3 i+=4; #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { if(ret != nullptr) delete ret; throw e; @@ -1377,7 +1377,7 @@ std::pair parse_if(const char* in, uint32_t size, uint32_t #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -1434,7 +1434,7 @@ std::pair parse_for(const char* in, uint32_t size, uint32_ i=lp.second; #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -1469,7 +1469,7 @@ std::pair parse_while(const char* in, uint32_t size, uin throw PARSE_ERROR("while is empty", i); #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; throw e; @@ -1595,7 +1595,7 @@ std::pair parse_block(const char* in, uint32_t size, uint32_t } #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { if(ret != nullptr) delete ret; throw e; @@ -1631,10 +1631,10 @@ shmain* parse_text(const char* in, uint32_t size, std::string const& filename) i=pp.second; #ifndef NO_PARSE_CATCH } - catch(ztd::format_error& e) + catch(format_error& e) { delete ret; - throw ztd::format_error(e.what(), filename, e.data(), e.where()); + throw format_error(e.what(), filename, e.data(), e.where()); } #endif return ret; diff --git a/src/processing.cpp b/src/processing.cpp index 9c105e5..9a7edbd 100644 --- a/src/processing.cpp +++ b/src/processing.cpp @@ -463,7 +463,7 @@ bool r_do_string_processor(_obj* in) require_rescan_all(); t->val='\'' + tsh->generate(false, 0) + '\''; } - catch(ztd::format_error& e) // if fail: skip processing + catch(format_error& e) // if fail: skip processing { std::cerr << "Exception caused in string processing LXSH_PARSE_MINIFY\n"; printFormatError(e); diff --git a/src/resolve.cpp b/src/resolve.cpp index 6f5e5f8..a9033a8 100644 --- a/src/resolve.cpp +++ b/src/resolve.cpp @@ -197,9 +197,9 @@ std::vector do_resolve_parse(condlist* cmd, std::string const& filena // cd back _cd(dir); } - catch(ztd::format_error& e) + catch(format_error& e) { - throw ztd::format_error(e.what(), '`'+p.first+'`', e.data(), e.where()); + throw format_error(e.what(), '`'+p.first+'`', e.data(), e.where()); } return ret; diff --git a/src/shellcode.cpp b/src/shellcode.cpp index 0d5a563..2c2a4bb 100644 --- a/src/shellcode.cpp +++ b/src/shellcode.cpp @@ -4,13 +4,13 @@ #include "processing.hpp" #include "struc_helper.hpp" -const std::map lxsh_extend_fcts = { +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"} } } }; -const std::map lxsh_array_fcts = { +const std::map lxsh_array_fcts = { { "_lxsh_array_create", { "", "Create an array out of input arguments", ARRAY_CREATE_SH} }, { "_lxsh_array_get", { " ", "Get value from array", ARRAY_GET_SH} }, { "_lxsh_array_set", { " ", "Set value of array", ARRAY_SET_SH} }, @@ -19,7 +19,7 @@ const std::map lxsh_array_fcts = { { "_lxsh_map_set", { " ", "Set value of map", MAP_SET_SH} } }; -std::map create_allfcts() +std::map create_allfcts() { auto r = lxsh_array_fcts; for(auto it: lxsh_extend_fcts) @@ -27,7 +27,7 @@ std::map create_allfcts() return r; } -const std::map lxsh_allfcts = create_allfcts(); +const std::map lxsh_allfcts = create_allfcts(); void add_lxsh_fcts(shmain* sh, std::set fcts) { diff --git a/src/util.cpp b/src/util.cpp index 4059c55..905a916 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -7,6 +7,7 @@ #include #include +#include #include @@ -210,7 +211,7 @@ std::string repeatString(std::string const& str, uint32_t n) return ret; } -void printFormatError(ztd::format_error const& e, bool print_line) +void printFormatError(format_error const& e, bool print_line) { printErrorIndex(e.data(), e.where(), e.what(), e.origin(), print_line); }