From 0e9aa3b023f9f01820e03ea962246e7c3b704bff Mon Sep 17 00:00:00 2001 From: zawwz Date: Wed, 3 Nov 2021 14:37:49 +0100 Subject: [PATCH] add map options --- include/minify.hpp | 11 +++++++++-- src/main.cpp | 20 +++++++++++++++----- src/minify.cpp | 35 +++++++++++++++++++++++++++++++++-- src/options.cpp | 14 ++++++++++++++ 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/include/minify.hpp b/include/minify.hpp index b83bd5c..fc30307 100644 --- a/include/minify.hpp +++ b/include/minify.hpp @@ -2,12 +2,19 @@ #define MINIFY_HPP #include "struc.hpp" +#include "processing.hpp" #include #include -void minify_var(_obj* in, std::regex const& exclude); -void minify_fct(_obj* in, std::regex const& exclude); +std::string gen_minmap(strmap_t const& map, std::string const& prefix); +void read_minmap(std::string const& filepath, strmap_t* varmap, strmap_t* fctmap); + +bool r_replace_fct(_obj* in, strmap_t* fctmap); +bool r_replace_var(_obj* in, strmap_t* varmap); + +strmap_t minify_var(_obj* in, std::regex const& exclude); +strmap_t minify_fct(_obj* in, std::regex const& exclude); void delete_unused(_obj* in, std::regex const& var_exclude, std::regex const& fct_exclude); diff --git a/src/main.cpp b/src/main.cpp index 42d71bc..5516e98 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -194,22 +194,28 @@ int main(int argc, char* argv[]) // processing before output // minify + strmap_t varmap, fctmap; if(options['m']) { opt_minify=true; minify_generic(sh); } - if(options["minify-var"] && options["minify-fct"]) { + if(options['A']) { + read_minmap(options['A'].argument, &varmap, &fctmap); + recurse(r_replace_var, sh, &varmap); + recurse(r_replace_fct, sh, &fctmap); + } + else if(options["minify-var"] && options["minify-fct"]) { // optimization: get everything in one go allmaps_get(sh, re_var_exclude, re_fct_exclude, regex_null); - minify_var( sh, re_var_exclude ); - minify_fct( sh, re_fct_exclude ); + varmap = minify_var( sh, re_var_exclude ); + fctmap = minify_fct( sh, re_fct_exclude ); } else if(options["minify-var"]) { - minify_var( sh, re_var_exclude ); + varmap = minify_var( sh, re_var_exclude ); } else if(options["minify-fct"]) { - minify_fct( sh, re_fct_exclude ); + fctmap = minify_fct( sh, re_fct_exclude ); } // other processing if(options["unset-var"]) @@ -223,6 +229,10 @@ int main(int argc, char* argv[]) else #endif + if(options['P']) { + std::ofstream(options['P'].argument) << gen_minmap(varmap, "var") << gen_minmap(fctmap, "fct"); + } + if(options['o']) // file output { std::string destfile=options['o']; diff --git a/src/minify.cpp b/src/minify.cpp index acd064e..bef4ab4 100644 --- a/src/minify.cpp +++ b/src/minify.cpp @@ -1,5 +1,6 @@ #include "minify.hpp" +#include #include "parse.hpp" #include "recursive.hpp" @@ -390,7 +391,7 @@ strmap_t gen_minimal_map(countmap_t const& vars, set_t const& excluded) // calls -void minify_var(_obj* in, std::regex const& exclude) +strmap_t minify_var(_obj* in, std::regex const& exclude) { // countmap_t vars; set_t excluded; @@ -405,9 +406,10 @@ void minify_var(_obj* in, std::regex const& exclude) // perform replace recurse(r_replace_var, in, &varmap); require_rescan_var(); + return varmap; } -void minify_fct(_obj* in, std::regex const& exclude) +strmap_t minify_fct(_obj* in, std::regex const& exclude) { // countmap_t fcts, cmdmap; set_t excluded, unsets; @@ -428,6 +430,7 @@ void minify_fct(_obj* in, std::regex const& exclude) recurse(r_replace_fct, in, &fctmap); require_rescan_fct(); require_rescan_cmd(); + return fctmap; } bool delete_unused_fct(_obj* in, std::regex const& exclude) @@ -665,3 +668,31 @@ void minify_generic(_obj* in) recurse(r_minify_backtick, in); recurse(r_minify_useless_quotes, in); } + +std::string gen_minmap(strmap_t const& map, std::string const& prefix) +{ + std::string ret; + for(auto it: map) { + ret += strf("%s %s %s\n", prefix.c_str(), it.second.c_str(), it.first.c_str()); + } + return ret; +} + +void read_minmap(std::string const& filepath, strmap_t* varmap, strmap_t* fctmap) +{ + std::ifstream file(filepath); + std::string ln; + while(std::getline(file, ln)) { + size_t s1, s2, s3; + s1 = ln.find(' '); + s2 = ln.find(' ', s1+1); + s3 = ln.find(' ', s2+1); + std::string type = ln.substr(0, s1); + std::string from = ln.substr(s1+1, s2-s1-1); + std::string to = ln.substr(s2+1, s3-s2-1); + if(type == "var") + varmap->insert(std::make_pair(from, to)); + else if(type == "fct") + fctmap->insert(std::make_pair(from, to)); + } +} diff --git a/src/options.cpp b/src/options.cpp index 4307e6e..6c9204c 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -20,6 +20,8 @@ ztd::option_set options( { 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('P', "map", true , "Output var and fct minify map to given file", "file"), + ztd::option('A', "apply-map", true , "Apply map from given file", "file"), #ifdef DEBUG_MODE ztd::option("\r [Debugging]"), ztd::option('J', "json", false, "Output the json structure"), @@ -73,6 +75,18 @@ void get_opts() options["minify-fct"].activated=true; options["remove-unused"].activated=true; } + if(options['o'].argument == "-") + options['o'].argument = "/dev/stdout"; + if(options['P'].argument == "-") + options['P'].argument = "/dev/stdout"; + if(options['A'].argument == "-") + options['A'].argument = "/dev/stdin"; + if( + options['A'] && ( options['P'] || options["minify-var"] || options["minify-fct"] ) + ) { + printf("Incompatible options\n"); + exit(ERR_OPT); + } } ztd::option_set create_include_opts()