diff --git a/Makefile b/Makefile index 91049f4..992976b 100644 --- a/Makefile +++ b/Makefile @@ -18,17 +18,21 @@ CXXFLAGS= -I$(IDIR) -Wall -pedantic -std=c++20 ifeq ($(DEBUG),true) # debugging flags CC=clang++ - CXXFLAGS += -g -pg -D NO_PARSE_CATCH + CXXFLAGS += -g -D NO_PARSE_CATCH else # release flags CXXFLAGS += -Ofast endif +ifeq ($(PROFILE),true) + CXXFLAGS += -pg +endif + ifneq ($(RELEASE), true) VSUFFIX=-dev-$(SHA_SHORT) endif -ifeq ($(STATIC),true) +ifeq ($(STATIC),true) # static links LDFLAGS += -l:libztd.a else diff --git a/include/processing.hpp b/include/processing.hpp index d3f54a1..26ab7d6 100644 --- a/include/processing.hpp +++ b/include/processing.hpp @@ -76,4 +76,6 @@ bool r_delete_var(_obj* in, set_t* vars); std::set find_lxsh_commands(shmain* sh); void add_unset_variables(shmain* sh, std::regex const& exclude); +void string_processors(_obj* in); + #endif //PROCESSING_HPP diff --git a/include/version.h b/include/version.h index eb50c34..686ed20 100644 --- a/include/version.h +++ b/include/version.h @@ -1,6 +1,6 @@ #ifndef VERSION_H #define VERSION_H -#define VERSION_STRING "v1.1.0" +#define VERSION_STRING "v1.2.0" #endif //VERSION_H diff --git a/src/exec.cpp b/src/exec.cpp index 100dc2b..31bcc87 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -205,10 +205,6 @@ pid_t forkexec(const char* bin, char *const args[]) } if (child_pid == 0) // child process { - // char buf[1000] = {0}; - // read(STDIN_FILENO, buf, 1000); - // std::cout << std::string(buf) << std::endl; - // std::cout << dup2(tfd, STDIN_FILENO) << std::endl; setpgid(child_pid, child_pid); //Needed so negative PIDs can kill children of /bin/sh execv(bin, args); throw std::runtime_error("execv() failed"); diff --git a/src/main.cpp b/src/main.cpp index 775b587..82ef7d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -186,7 +186,10 @@ int main(int argc, char* argv[]) // processing before output // minify if(options['m']) + { opt_minify=true; + string_processors(sh); + } if(options["minify-quotes"]) minify_quotes(sh); if(options["minify-var"]) diff --git a/src/processing.cpp b/src/processing.cpp index 2707653..9c105e5 100644 --- a/src/processing.cpp +++ b/src/processing.cpp @@ -7,6 +7,10 @@ #include "util.hpp" #include "shellcode.hpp" #include "struc_helper.hpp" +#include "options.hpp" +#include "minify.hpp" + +#include "errcodes.h" // Global regex @@ -415,6 +419,66 @@ std::set find_lxsh_commands(shmain* sh) return ret; } +std::set get_processors(std::string const& in) +{ + std::set ret; + if(in.size()>2 && in[0] == '\'' && in[in.size()-1] == '\'') + { + uint32_t i=1; + while(true) + { + std::string ln = in.substr(i, in.find('\n', i)-i); + if(ln.size()>1 && ln[0] == '#' && is_alphanum(ln[1])) + { + i+=ln.size(); + ret.insert(get_word(ln.c_str(), ln.size(), 1, SEPARATORS).first); + } + else + break; + } + } + return ret; +} + +bool r_do_string_processor(_obj* in) +{ + if(in->type == _obj::subarg_string) + { + string_subarg* t = dynamic_cast(in); + auto v = get_processors(t->val); + if(v.find("LXSH_PARSE_MINIFY") != v.end()) + { + try + { + shmain* tsh = parse_text(t->val.substr(1, t->val.size()-2)); + require_rescan_all(); + if(options["remove-unused"]) + delete_unused( tsh, re_var_exclude, re_fct_exclude ); + if(options["minify-quotes"]) + minify_quotes(tsh); + if(options["minify-var"]) + minify_var( tsh, re_var_exclude ); + if(options["minify-fct"]) + minify_fct( tsh, re_fct_exclude ); + require_rescan_all(); + t->val='\'' + tsh->generate(false, 0) + '\''; + } + catch(ztd::format_error& e) // if fail: skip processing + { + std::cerr << "Exception caused in string processing LXSH_PARSE_MINIFY\n"; + printFormatError(e); + exit(ERR_RUNTIME); + } + } + } + return true; +} + +void string_processors(_obj* in) +{ + recurse(r_do_string_processor, in); +} + /** JSON **/ std::string quote_string(std::string const& in) diff --git a/src/resolve.cpp b/src/resolve.cpp index 59a47a6..d3cd3a1 100644 --- a/src/resolve.cpp +++ b/src/resolve.cpp @@ -34,7 +34,6 @@ bool add_include(std::string const& file) if(it == truepath) return false; } - // std::cout << truepath << std::endl; included.push_back(truepath); return true; } diff --git a/src/util.cpp b/src/util.cpp index dd3d5c4..4059c55 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -236,14 +236,11 @@ void printErrorIndex(const char* in, const int index, const std::string& message i++; } } - if(origin != "") + fprintf(stderr, "%s:%u:%u: %s\n", origin.c_str(), line, index-j+1, message.c_str()); + if(print_line) { - fprintf(stderr, "%s:%u:%u: %s\n", origin.c_str(), line, index-j+1, message.c_str()); - if(print_line) - { - std::cerr << std::string(in+j, i-j) << std::endl; - std::cerr << repeatString(" ", index-j) << '^' << std::endl; - } + std::cerr << std::string(in+j, i-j) << std::endl; + std::cerr << repeatString(" ", index-j) << '^' << std::endl; } }