add string parse minifying

This commit is contained in:
zawz 2021-04-16 16:36:02 +02:00
parent af5d276ff0
commit 1f75f18fba
8 changed files with 80 additions and 15 deletions

View file

@ -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

View file

@ -76,4 +76,6 @@ bool r_delete_var(_obj* in, set_t* vars);
std::set<std::string> find_lxsh_commands(shmain* sh);
void add_unset_variables(shmain* sh, std::regex const& exclude);
void string_processors(_obj* in);
#endif //PROCESSING_HPP

View file

@ -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

View file

@ -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");

View file

@ -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"])

View file

@ -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<std::string> find_lxsh_commands(shmain* sh)
return ret;
}
std::set<std::string> get_processors(std::string const& in)
{
std::set<std::string> 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<string_subarg*>(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)

View file

@ -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;
}

View file

@ -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;
}
}