Code refactorization

This commit is contained in:
Ervin Hegedus 2024-03-27 16:19:38 +01:00
parent 4085ff5536
commit ca7c0ae0b9
No known key found for this signature in database
GPG Key ID: 5FA5BC3F5EC41F61

View File

@ -32,61 +32,39 @@ void print_help(const char *name) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
modsecurity::RulesSet *rules; std::unique_ptr<modsecurity::RulesSet> rules (new modsecurity::RulesSet());
char **args = argv; char **args = argv;
rules = new modsecurity::RulesSet();
int ret = 0; int ret = 0;
args++; args++;
if (*args == NULL) { if (*args == NULL) {
print_help(argv[0]); print_help(argv[0]);
delete rules;
return 0; return 0;
} }
while (*args != NULL) { while (*args != NULL) {
struct stat buffer; struct stat buffer;
std::string argFull(""); std::string arg = (*args);
const char *arg = *args;
std::string err; std::string err;
int r; int r;
bool need_free = false;
if (argFull.empty() == false) { // strip arg from leading and trailing '"' chars
if (arg[strlen(arg)-1] == '\"') { arg.erase(arg.find_last_not_of('\"')+1);
argFull.append(arg, strlen(arg)-1); arg.erase(0, arg.find_first_not_of('\"'));
goto next;
} else {
argFull.append(arg);
goto next;
}
}
if (arg[0] == '\"' && argFull.empty() == true) { if (arg.empty() == true) {
if (arg[strlen(arg)-1] == '\"') { args++;
argFull.append(arg+1, strlen(arg) - 2); continue;
} else {
argFull.append(arg+1);
goto next;
}
}
if (argFull.empty() == false) {
arg = strdup(argFull.c_str());
need_free = true;
argFull.clear();
} }
std::cout << " : " << arg << " -- "; std::cout << " : " << arg << " -- ";
if (stat(arg, &buffer) == 0) { if (stat(arg.c_str(), &buffer) == 0) {
r = rules->loadFromUri(arg); r = rules->loadFromUri(arg.c_str());
} else { } else {
r = rules->load(arg); r = rules->load(arg.c_str());
}
if(need_free == true && arg != nullptr) {
free((void*)arg);
} }
if (r < 0) { if (r < 0) {
err.assign(rules->m_parserError.str()); err.assign(rules->m_parserError.str());
rules->m_parserError.str(""); rules->m_parserError.str("");
@ -97,12 +75,10 @@ int main(int argc, char **argv) {
if (err.empty() == false) { if (err.empty() == false) {
std::cerr << " " << err << std::endl; std::cerr << " " << err << std::endl;
} }
next:
args++; args++;
} }
delete rules;
if (ret < 0) { if (ret < 0) {
std::cout << "Test failed." << std::endl; std::cout << "Test failed." << std::endl;
} else { } else {