From: Josh Date: Thu, 3 Jul 2014 19:12:02 +0000 (-0400) Subject: Merge branch 'snort2lua' X-Git-Tag: 3.0.0-233~1175^2~25 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3df4d3d43d66191240a80d0e225a35bd07f7eb5c;p=thirdparty%2Fsnort3.git Merge branch 'snort2lua' --- 3df4d3d43d66191240a80d0e225a35bd07f7eb5c diff --cc tools/snort2lua/snort2lua.cc index c7279c041,d2feaacf7..f7acede8a --- a/tools/snort2lua/snort2lua.cc +++ b/tools/snort2lua/snort2lua.cc @@@ -22,68 -22,93 +22,93 @@@ #include #include - #include "converter.h" + #include "util/converter.h" #include "init_state.h" - #include "snort2lua_util.h" + #include "util/util.h" + #include "option_parser.h" - static bool convert(std::ifstream& in, std::ofstream& out) + + /**************************************************** + ************ OPTION INFORMATION ***************** + ****************************************************/ + + namespace { - Converter cv; - cv.reset_state(); - std::string orig_text; - while(!in.eof()) - { - std::string tmp; - std::getline(in, tmp); - util::ltrim(tmp); - orig_text += ' ' + tmp; - util::trim(orig_text); + struct Arg: public option::Arg + { + static void printError(const char* msg1, const option::Option& opt, const char* msg2) + { + fprintf(stderr, "%s", msg1); + fwrite(opt.name, opt.namelen, 1, stderr); + fprintf(stderr, "%s", msg2); + } + + static option::ArgStatus Unknown(const option::Option& option, bool msg) + { + if (msg) printError("Unknown option '", option, "'\n"); + return option::ARG_ILLEGAL; + } + + static option::ArgStatus Required(const option::Option& option, bool msg) + { + if (option.arg != 0) + return option::ARG_OK; + + if (msg) printError("Option '", option, "' requires an argument\n"); + return option::ARG_ILLEGAL; + } + + static option::ArgStatus NonEmpty(const option::Option& option, bool msg) + { + if (option.arg != 0 && option.arg[0] != 0) + return option::ARG_OK; + + if (msg) printError("Option '", option, "' requires a non-empty argument\n"); + return option::ARG_ILLEGAL; + } + + static option::ArgStatus Numeric(const option::Option& option, bool msg) + { + char* endptr = 0; + if (option.arg != 0 && strtol(option.arg, &endptr, 10)){}; + if (endptr != option.arg && *endptr == 0) + return option::ARG_OK; + + if (msg) printError("Option '", option, "' requires a numeric argument\n"); + return option::ARG_ILLEGAL; + } + }; + + enum OptionType { + OPT_ENABLE, + OPT_DIABLE + }; + + enum OptionIndex { + INPUT_FILE, + OUTPUT_FILE, + HELP, + READ_INCLUDE_FILES, + UNKOWN, + }; + const option::Descriptor usage[] = + { + {HELP, 0, "", "help", Arg::None, " --help\t\tprint usage and exit"}, - {INPUT_FILE, 0, "c", "input", Arg::Required, " -i --input-file, \t\tsnort configuration file. Specify as many files as you would like"}, ++ {INPUT_FILE, 0, "c", "conf-file", Arg::Required, " --conf-file, -c, \t\tsnort configuration file. Specify as many files as you would like"}, + {OUTPUT_FILE, 0, "o", "output", Arg::Required, " --output-file, -o \t\tThe new Snort++ configuration file name"}, + {READ_INCLUDE_FILES, OPT_ENABLE, "", "enable-reading-includes", Arg::None, " enable-reading-includes \t\t Every time the 'include' keywords appears, open and parse that file"}, + {READ_INCLUDE_FILES, OPT_DIABLE, "", "disable-reading-includes", Arg::None, " disable-reading-includes \t\tEvery time the 'include' keywords appears, open and parse that file"}, + {0,0,0,0,0,0} + }; - if (orig_text.empty()) - { - cv.add_comment_to_file(""); - } - else if (orig_text.front() == '#') - { - orig_text.erase(orig_text.begin()); - cv.add_comment_to_file(orig_text); - orig_text.clear(); - } - else if ( orig_text.back() == '\\') - { - orig_text.pop_back(); - util::rtrim(orig_text); - } - else - { - std::stringstream data_stream(orig_text); - while(data_stream.tellg() != -1) - { - if (!cv.convert_line(data_stream)) - { - cv.log_error("Failed to entirely convert: " + orig_text); - // data_stream.setstate(std::basic_ios::eofbit); - break; - } - } - - orig_text.clear(); - cv.reset_state(); - } - } + } // anonymous - // finally, lets print the converter to file - out << "require(\"snort_config\") -- for loading" << std::endl; - out << cv; - return true; - } + /********************************************* + ************** MAIN FILES ***************** + *********************************************/ - static void show_usage() - { - std::cout << "usage: snort2lua " << std::endl; - } int main (int argc, char* argv[]) {