diff --git a/tools/bifcl/builtin-func.l b/tools/bifcl/builtin-func.l index 9b4d4628e1..48fed7509f 100644 --- a/tools/bifcl/builtin-func.l +++ b/tools/bifcl/builtin-func.l @@ -143,11 +143,10 @@ HEX [0-9a-fA-F]+ } %% -int yywrap() - { - yy_delete_buffer(YY_CURRENT_BUFFER); - return 1; - } +int yywrap() { + yy_delete_buffer(YY_CURRENT_BUFFER); + return 1; +} extern int yyparse(); char* input_filename = nullptr; @@ -171,297 +170,272 @@ void close_if_open(FILE **fpp); void close_all_output_files(void); -FILE* open_output_file(const char* surfix) - { - char fn[1024]; - FILE* fp; +FILE* open_output_file(const char* surfix) { + char fn[1024]; + FILE* fp; - snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix); - if ( (fp = fopen(fn, "w")) == NULL ) - { - fprintf(stderr, "Error: cannot open file: %s\n", fn); - err_exit(); - } + snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix); + if ( (fp = fopen(fn, "w")) == NULL ) { + fprintf(stderr, "Error: cannot open file: %s\n", fn); + err_exit(); + } - return fp; - } + return fp; +} -void usage() - { - fprintf(stderr, "usage: bifcl [-p | -s] *.bif\n"); - exit(1); - } +void usage() { + fprintf(stderr, "usage: bifcl [-p | -s] *.bif\n"); + exit(1); +} -void init_alternative_mode() - { - fp_zeek_init = open_output_file("zeek"); - fp_func_h = open_output_file("h"); - fp_func_def = open_output_file("cc"); - fp_func_init = open_output_file("init.cc"); - fp_func_register = plugin ? open_output_file("register.cc") : nullptr; +void init_alternative_mode() { + fp_zeek_init = open_output_file("zeek"); + fp_func_h = open_output_file("h"); + fp_func_def = open_output_file("cc"); + fp_func_init = open_output_file("init.cc"); + fp_func_register = plugin ? open_output_file("register.cc") : nullptr; - fp_netvar_h = fp_func_h; - fp_netvar_def = fp_func_def; - fp_netvar_init = fp_func_init; + fp_netvar_h = fp_func_h; + fp_netvar_def = fp_func_def; + fp_netvar_init = fp_func_init; - int n = 1024 + strlen(input_filename); - auto auto_gen_comment_buf = std::make_unique(n); - auto auto_gen_comment = auto_gen_comment_buf.get(); + int n = 1024 + strlen(input_filename); + auto auto_gen_comment_buf = std::make_unique(n); + auto auto_gen_comment = auto_gen_comment_buf.get(); - snprintf(auto_gen_comment, n, - "This file was automatically generated by bifcl from %s (%s mode).", - input_filename_with_path, plugin ? "plugin" : "alternative"); + snprintf(auto_gen_comment, n, "This file was automatically generated by bifcl from %s (%s mode).", + input_filename_with_path, plugin ? "plugin" : "alternative"); - fprintf(fp_zeek_init, "# %s\n\n", auto_gen_comment); - fprintf(fp_func_def, "// %s\n\n", auto_gen_comment); - fprintf(fp_func_h, "// %s\n\n", auto_gen_comment); - fprintf(fp_func_h, "#pragma once\n\n"); - fprintf(fp_func_init, "// %s\n\n", auto_gen_comment); + fprintf(fp_zeek_init, "# %s\n\n", auto_gen_comment); + fprintf(fp_func_def, "// %s\n\n", auto_gen_comment); + fprintf(fp_func_h, "// %s\n\n", auto_gen_comment); + fprintf(fp_func_h, "#pragma once\n\n"); + fprintf(fp_func_init, "// %s\n\n", auto_gen_comment); - if ( fp_func_register ) - fprintf(fp_func_register, "// %s\n\n", auto_gen_comment); + if ( fp_func_register ) + fprintf(fp_func_register, "// %s\n\n", auto_gen_comment); - static char guard[1024]; - if ( getcwd(guard, sizeof(guard)) == NULL ) - { - fprintf(stderr, "Error: cannot get current working directory\n"); - err_exit(); - } - strncat(guard, "/", sizeof(guard) - strlen(guard) - 1); - strncat(guard, input_filename, sizeof(guard) - strlen(guard) - 1); + static char guard[1024]; + if ( getcwd(guard, sizeof(guard)) == NULL ) { + fprintf(stderr, "Error: cannot get current working directory\n"); + err_exit(); + } + strncat(guard, "/", sizeof(guard) - strlen(guard) - 1); + strncat(guard, input_filename, sizeof(guard) - strlen(guard) - 1); - for ( char* p = guard; *p; p++ ) - { - if ( ! isalnum(*p) ) - *p = '_'; - } + for ( char* p = guard; *p; p++ ) { + if ( ! isalnum(*p) ) + *p = '_'; + } - fprintf(fp_func_h, "#if defined(ZEEK_IN_NETVAR) || ! defined(%s)\n", guard); + fprintf(fp_func_h, "#if defined(ZEEK_IN_NETVAR) || ! defined(%s)\n", guard); - fprintf(fp_func_h, "#ifndef ZEEK_IN_NETVAR\n"); - fprintf(fp_func_h, "#ifndef %s\n", guard); - fprintf(fp_func_h, "#define %s\n", guard); - fprintf(fp_func_h, "#include \"zeek/zeek-bif.h\"\n"); - fprintf(fp_func_h, "#endif\n"); - fprintf(fp_func_h, "#endif\n"); - fprintf(fp_func_h, "\n"); + fprintf(fp_func_h, "#ifndef ZEEK_IN_NETVAR\n"); + fprintf(fp_func_h, "#ifndef %s\n", guard); + fprintf(fp_func_h, "#define %s\n", guard); + fprintf(fp_func_h, "#include \"zeek/zeek-bif.h\"\n"); + fprintf(fp_func_h, "#endif\n"); + fprintf(fp_func_h, "#endif\n"); + fprintf(fp_func_h, "\n"); - fprintf(fp_func_def, "\n"); - fprintf(fp_func_def, "#include \"%s.h\"\n", input_filename); - fprintf(fp_func_def, "#include \"zeek/Func.h\"\n"); - fprintf(fp_func_def, "\n"); + fprintf(fp_func_def, "\n"); + fprintf(fp_func_def, "#include \"%s.h\"\n", input_filename); + fprintf(fp_func_def, "#include \"zeek/Func.h\"\n"); + fprintf(fp_func_def, "\n"); - static char name[1024]; - strncpy(name, input_filename, sizeof(name) - 1); - name[sizeof(name) - 1] = '\0'; - char* dot = strchr(name, '.'); - if ( dot ) - *dot = '\0'; + static char name[1024]; + strncpy(name, input_filename, sizeof(name) - 1); + name[sizeof(name) - 1] = '\0'; + char* dot = strchr(name, '.'); + if ( dot ) + *dot = '\0'; - if ( plugin ) - { - static char plugin_canon[1024]; - strncpy(plugin_canon, plugin, sizeof(plugin_canon) - 1); - plugin_canon[sizeof(plugin_canon) - 1] = '\0'; - char* colon = strstr(plugin_canon, "::"); + if ( plugin ) { + static char plugin_canon[1024]; + strncpy(plugin_canon, plugin, sizeof(plugin_canon) - 1); + plugin_canon[sizeof(plugin_canon) - 1] = '\0'; + char* colon = strstr(plugin_canon, "::"); - if ( colon ) { - *colon = '_'; - memmove(colon + 1, colon + 2, plugin_canon + strlen(plugin_canon) - colon); - } - - fprintf(fp_func_init, "\n"); - fprintf(fp_func_init, "#include \n"); - fprintf(fp_func_init, "#include \n"); - fprintf(fp_func_init, "#include \"zeek/plugin/Plugin.h\"\n"); - fprintf(fp_func_init, "#include \"zeek/Func.h\"\n"); - fprintf(fp_func_init, "#include \"%s.h\"\n", input_filename); - fprintf(fp_func_init, "\n"); - fprintf(fp_func_init, "namespace plugin::%s {\n", plugin_canon); - fprintf(fp_func_init, "\n"); - fprintf(fp_func_init, "void __bif_%s_init(zeek::plugin::Plugin* plugin)\n", name); - fprintf(fp_func_init, "\t{\n"); - - fprintf(fp_func_register, "#include \"zeek/plugin/Manager.h\"\n"); - fprintf(fp_func_register, "\n"); - fprintf(fp_func_register, "namespace plugin::%s {\n", plugin_canon); - fprintf(fp_func_register, "void __bif_%s_init(zeek::plugin::Plugin* plugin);\n", name); - fprintf(fp_func_register, "zeek::plugin::detail::__RegisterBif __register_bifs_%s_%s(\"%s\", __bif_%s_init);\n", plugin_canon, name, plugin, name); - fprintf(fp_func_register, "}\n"); + if ( colon ) { + *colon = '_'; + memmove(colon + 1, colon + 2, plugin_canon + strlen(plugin_canon) - colon); } - } -void finish_alternative_mode() - { - fprintf(fp_func_h, "\n"); - fprintf(fp_func_h, "#endif\n"); + fprintf(fp_func_init, "\n"); + fprintf(fp_func_init, "#include \n"); + fprintf(fp_func_init, "#include \n"); + fprintf(fp_func_init, "#include \"zeek/plugin/Plugin.h\"\n"); + fprintf(fp_func_init, "#include \"zeek/Func.h\"\n"); + fprintf(fp_func_init, "#include \"%s.h\"\n", input_filename); + fprintf(fp_func_init, "\n"); + fprintf(fp_func_init, "namespace plugin::%s {\n", plugin_canon); + fprintf(fp_func_init, "\n"); + fprintf(fp_func_init, "void __bif_%s_init(zeek::plugin::Plugin* plugin)\n", name); + fprintf(fp_func_init, "\t{\n"); - if ( plugin ) - { - fprintf(fp_func_init, "\n"); - fprintf(fp_func_init, "\t}\n"); - fprintf(fp_func_init, "}\n"); - fprintf(fp_func_init, "\n"); - fprintf(fp_func_init, "\n"); - } - } + fprintf(fp_func_register, "#include \"zeek/plugin/Manager.h\"\n"); + fprintf(fp_func_register, "\n"); + fprintf(fp_func_register, "namespace plugin::%s {\n", plugin_canon); + fprintf(fp_func_register, "void __bif_%s_init(zeek::plugin::Plugin* plugin);\n", name); + fprintf(fp_func_register, "zeek::plugin::detail::__RegisterBif __register_bifs_%s_%s(\"%s\", __bif_%s_init);\n", + plugin_canon, name, plugin, name); + fprintf(fp_func_register, "}\n"); + } +} + +void finish_alternative_mode() { + fprintf(fp_func_h, "\n"); + fprintf(fp_func_h, "#endif\n"); + + if ( plugin ) { + fprintf(fp_func_init, "\n"); + fprintf(fp_func_init, "\t}\n"); + fprintf(fp_func_init, "}\n"); + fprintf(fp_func_init, "\n"); + fprintf(fp_func_init, "\n"); + } +} // GCC uses __SANITIZE_ADDRESS__, Clang uses __has_feature #if defined(__SANITIZE_ADDRESS__) - #define USING_ASAN +#define USING_ASAN #endif #if defined(__has_feature) - #if __has_feature(address_sanitizer) - #define USING_ASAN - #endif +#if __has_feature(address_sanitizer) +#define USING_ASAN +#endif #endif // FreeBSD doesn't support LeakSanitizer -#if defined(USING_ASAN) && !defined(__FreeBSD__) - #include - #define BIFCL_LSAN_DISABLE() __lsan_disable() +#if defined(USING_ASAN) && ! defined(__FreeBSD__) +#include +#define BIFCL_LSAN_DISABLE() __lsan_disable() #else - #define BIFCL_LSAN_DISABLE() +#define BIFCL_LSAN_DISABLE() #endif -int main(int argc, char* argv[]) - { - // We generally do not care at all if bifcl is leaking and the default - // behavior of LSAN to treat leaks as errors only trips up Zeek's build. - BIFCL_LSAN_DISABLE(); +int main(int argc, char* argv[]) { + // We generally do not care at all if bifcl is leaking and the default + // behavior of LSAN to treat leaks as errors only trips up Zeek's build. + BIFCL_LSAN_DISABLE(); - int opt; + int opt; - while ( (opt = getopt(argc, argv, "p:s")) != -1 ) - { - switch ( opt ) { - case 'p': - alternative_mode = true; - plugin = (char*) optarg; - break; + while ( (opt = getopt(argc, argv, "p:s")) != -1 ) { + switch ( opt ) { + case 'p': + alternative_mode = true; + plugin = (char*)optarg; + break; - case 's': - alternative_mode = true; - break; + case 's': alternative_mode = true; break; - default: - usage(); - } - } + default: usage(); + } + } - for ( int i = optind; i < argc; i++ ) - { - FILE* fp_input; + for ( int i = optind; i < argc; i++ ) { + FILE* fp_input; - input_filename = input_filename_with_path = argv[i]; - char* slash = strrchr(input_filename, '/'); + input_filename = input_filename_with_path = argv[i]; + char* slash = strrchr(input_filename, '/'); - if ( (fp_input = fopen(input_filename, "r")) == NULL ) - { - fprintf(stderr, "Error: cannot open file: %s\n", input_filename); - /* no output files open. can simply exit */ - exit(1); - } + if ( (fp_input = fopen(input_filename, "r")) == NULL ) { + fprintf(stderr, "Error: cannot open file: %s\n", input_filename); + /* no output files open. can simply exit */ + exit(1); + } - if ( slash ) - input_filename = slash + 1; + if ( slash ) + input_filename = slash + 1; - if ( ! alternative_mode ) - { - fp_zeek_init = open_output_file("zeek"); - fp_func_h = open_output_file("func_h"); - fp_func_def = open_output_file("func_def"); - fp_func_init = open_output_file("func_init"); - fp_netvar_h = open_output_file("netvar_h"); - fp_netvar_def = open_output_file("netvar_def"); - fp_netvar_init = open_output_file("netvar_init"); + if ( ! alternative_mode ) { + fp_zeek_init = open_output_file("zeek"); + fp_func_h = open_output_file("func_h"); + fp_func_def = open_output_file("func_def"); + fp_func_init = open_output_file("func_init"); + fp_netvar_h = open_output_file("netvar_h"); + fp_netvar_def = open_output_file("netvar_def"); + fp_netvar_init = open_output_file("netvar_init"); - int n = 1024 + strlen(input_filename); - auto auto_gen_comment_buf = std::make_unique(n); - auto auto_gen_comment = auto_gen_comment_buf.get(); + int n = 1024 + strlen(input_filename); + auto auto_gen_comment_buf = std::make_unique(n); + auto auto_gen_comment = auto_gen_comment_buf.get(); - snprintf(auto_gen_comment, n, - "This file was automatically generated by bifcl from %s.", - input_filename); + snprintf(auto_gen_comment, n, "This file was automatically generated by bifcl from %s.", input_filename); - fprintf(fp_zeek_init, "# %s\n\n", auto_gen_comment); - fprintf(fp_func_def, "// %s\n\n", auto_gen_comment); - fprintf(fp_func_h, "// %s\n\n", auto_gen_comment); - fprintf(fp_func_h, "#pragma once\n\n"); - fprintf(fp_func_init, "// %s\n\n", auto_gen_comment); - fprintf(fp_netvar_def, "// %s\n\n", auto_gen_comment); - fprintf(fp_netvar_h, "// %s\n\n", auto_gen_comment); - fprintf(fp_netvar_h, "#pragma once\n\n"); - fprintf(fp_netvar_init, "// %s\n\n", auto_gen_comment); - } + fprintf(fp_zeek_init, "# %s\n\n", auto_gen_comment); + fprintf(fp_func_def, "// %s\n\n", auto_gen_comment); + fprintf(fp_func_h, "// %s\n\n", auto_gen_comment); + fprintf(fp_func_h, "#pragma once\n\n"); + fprintf(fp_func_init, "// %s\n\n", auto_gen_comment); + fprintf(fp_netvar_def, "// %s\n\n", auto_gen_comment); + fprintf(fp_netvar_h, "// %s\n\n", auto_gen_comment); + fprintf(fp_netvar_h, "#pragma once\n\n"); + fprintf(fp_netvar_init, "// %s\n\n", auto_gen_comment); + } - else - init_alternative_mode(); + else + init_alternative_mode(); - fprintf(fp_netvar_init, "#ifdef __GNUC__\n"); - fprintf(fp_netvar_init, "#pragma GCC diagnostic push\n"); - fprintf(fp_netvar_init, "#pragma GCC diagnostic ignored \"-Wdeprecated-declarations\"\n\n"); - fprintf(fp_netvar_init, "#endif\n"); + fprintf(fp_netvar_init, "#ifdef __GNUC__\n"); + fprintf(fp_netvar_init, "#pragma GCC diagnostic push\n"); + fprintf(fp_netvar_init, "#pragma GCC diagnostic ignored \"-Wdeprecated-declarations\"\n\n"); + fprintf(fp_netvar_init, "#endif\n"); - yy_switch_to_buffer(yy_create_buffer(fp_input, YY_BUF_SIZE)); - yyparse(); + yy_switch_to_buffer(yy_create_buffer(fp_input, YY_BUF_SIZE)); + yyparse(); - fprintf(fp_netvar_init, "#ifdef __GNUC__\n"); - fprintf(fp_netvar_init, "\n\n#pragma GCC diagnostic pop\n"); - fprintf(fp_netvar_init, "#endif\n"); + fprintf(fp_netvar_init, "#ifdef __GNUC__\n"); + fprintf(fp_netvar_init, "\n\n#pragma GCC diagnostic pop\n"); + fprintf(fp_netvar_init, "#endif\n"); - if ( alternative_mode ) - finish_alternative_mode(); + if ( alternative_mode ) + finish_alternative_mode(); - fclose(fp_input); - close_all_output_files(); + fclose(fp_input); + close_all_output_files(); + } +} - } - } +void close_if_open(FILE** fpp) { + if ( *fpp ) + fclose(*fpp); + *fpp = nullptr; +} -void close_if_open(FILE **fpp) - { - if (*fpp) - fclose(*fpp); - *fpp = nullptr; - } +void close_all_output_files(void) { + close_if_open(&fp_zeek_init); + close_if_open(&fp_func_h); + close_if_open(&fp_func_def); + close_if_open(&fp_func_init); + close_if_open(&fp_func_register); -void close_all_output_files(void) - { - close_if_open(&fp_zeek_init); - close_if_open(&fp_func_h); - close_if_open(&fp_func_def); - close_if_open(&fp_func_init); - close_if_open(&fp_func_register); + if ( ! alternative_mode ) { + close_if_open(&fp_netvar_h); + close_if_open(&fp_netvar_def); + close_if_open(&fp_netvar_init); + } +} - if ( ! alternative_mode ) - { - close_if_open(&fp_netvar_h); - close_if_open(&fp_netvar_def); - close_if_open(&fp_netvar_init); - } - } +void remove_file(const char* surfix) { + char fn[1024]; -void remove_file(const char *surfix) - { - char fn[1024]; + snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix); + unlink(fn); +} - snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix); - unlink(fn); - } - -void err_exit(void) - { - close_all_output_files(); - /* clean up. remove all output files we've generated so far */ - remove_file("zeek"); - remove_file("func_h"); - remove_file("func_def"); - remove_file("func_init"); - remove_file("func_register"); - remove_file("netvar_h"); - remove_file("netvar_def"); - remove_file("netvar_init"); - exit(1); - } +void err_exit(void) { + close_all_output_files(); + /* clean up. remove all output files we've generated so far */ + remove_file("zeek"); + remove_file("func_h"); + remove_file("func_def"); + remove_file("func_init"); + remove_file("func_register"); + remove_file("netvar_h"); + remove_file("netvar_def"); + remove_file("netvar_init"); + exit(1); +} diff --git a/tools/bifcl/builtin-func.y b/tools/bifcl/builtin-func.y index 239a3117b3..3c4b7bd221 100644 --- a/tools/bifcl/builtin-func.y +++ b/tools/bifcl/builtin-func.y @@ -65,92 +65,88 @@ struct decl_struct { string enqueue_c_fullname; } decl; -void set_definition_type(int type, const char *arg_type_name) - { - definition_type = type; - if ( type == TYPE_DEF && arg_type_name ) - type_name = string(arg_type_name); - else - type_name = ""; - } +void set_definition_type(int type, const char* arg_type_name) { + definition_type = type; + if ( type == TYPE_DEF && arg_type_name ) + type_name = string(arg_type_name); + else + type_name = ""; +} -void set_decl_name(const char *name) - { - decl.bare_name = extract_var_name(name); +void set_decl_name(const char* name) { + decl.bare_name = extract_var_name(name); - // make_full_var_name prepends the correct module, if any - // then we can extract the module name again. - string varname = make_full_var_name(current_module.c_str(), name); - decl.module_name = extract_module_name(varname.c_str()); + // make_full_var_name prepends the correct module, if any + // then we can extract the module name again. + string varname = make_full_var_name(current_module.c_str(), name); + decl.module_name = extract_module_name(varname.c_str()); - decl.c_namespace_start = ""; - decl.c_namespace_end = ""; - decl.c_fullname = ""; - decl.zeek_fullname = ""; - decl.zeek_name = ""; + decl.c_namespace_start = ""; + decl.c_namespace_end = ""; + decl.c_fullname = ""; + decl.zeek_fullname = ""; + decl.zeek_name = ""; - decl.enqueue_c_fullname = ""; - decl.enqueue_c_barename = string("enqueue_") + decl.bare_name; - decl.enqueue_c_namespace_start = ""; - decl.enqueue_c_namespace_end = ""; + decl.enqueue_c_fullname = ""; + decl.enqueue_c_barename = string("enqueue_") + decl.bare_name; + decl.enqueue_c_namespace_start = ""; + decl.enqueue_c_namespace_end = ""; - switch ( definition_type ) { - case TYPE_DEF: - decl.c_namespace_start = "BifType::" + type_name + ""; - decl.c_fullname = "BifType::" + type_name + "::"; - break; + switch ( definition_type ) { + case TYPE_DEF: + decl.c_namespace_start = "BifType::" + type_name + ""; + decl.c_fullname = "BifType::" + type_name + "::"; + break; - case CONST_DEF: - decl.c_namespace_start = "BifConst"; - decl.c_fullname = "BifConst::"; - break; + case CONST_DEF: + decl.c_namespace_start = "BifConst"; + decl.c_fullname = "BifConst::"; + break; - case FUNC_DEF: - decl.c_namespace_start = "BifFunc"; - decl.c_fullname = "BifFunc::"; - break; + case FUNC_DEF: + decl.c_namespace_start = "BifFunc"; + decl.c_fullname = "BifFunc::"; + break; - case EVENT_DEF: - decl.c_namespace_start = ""; - decl.c_namespace_end = ""; - decl.c_fullname = "::"; // need this for namespace qualified events due do event_c_body - decl.enqueue_c_namespace_start = "BifEvent"; - decl.enqueue_c_fullname = "zeek::BifEvent::"; - break; + case EVENT_DEF: + decl.c_namespace_start = ""; + decl.c_namespace_end = ""; + decl.c_fullname = "::"; // need this for namespace qualified events due do event_c_body + decl.enqueue_c_namespace_start = "BifEvent"; + decl.enqueue_c_fullname = "zeek::BifEvent::"; + break; - default: - break; - } + default: break; + } - if ( decl.module_name != GLOBAL_MODULE_NAME ) - { - if ( decl.c_namespace_start.empty() ) { - decl.c_namespace_start += "namespace " + decl.module_name + " { "; - decl.c_namespace_end += " }"; - } - else { - decl.c_namespace_start += "::" + decl.module_name; - decl.c_namespace_end = ""; - } - decl.c_fullname += decl.module_name + "::"; - decl.zeek_fullname += decl.module_name + "::"; + if ( decl.module_name != GLOBAL_MODULE_NAME ) { + if ( decl.c_namespace_start.empty() ) { + decl.c_namespace_start += "namespace " + decl.module_name + " { "; + decl.c_namespace_end += " }"; + } + else { + decl.c_namespace_start += "::" + decl.module_name; + decl.c_namespace_end = ""; + } + decl.c_fullname += decl.module_name + "::"; + decl.zeek_fullname += decl.module_name + "::"; - if ( decl.enqueue_c_namespace_start.empty() ) { - decl.enqueue_c_namespace_start += "namespace " + decl.module_name + " { "; - decl.enqueue_c_namespace_end += " } "; - } - else { - decl.enqueue_c_namespace_start += "::" + decl.module_name; - decl.enqueue_c_namespace_end = ""; - } - decl.enqueue_c_fullname += decl.module_name + "::"; - } + if ( decl.enqueue_c_namespace_start.empty() ) { + decl.enqueue_c_namespace_start += "namespace " + decl.module_name + " { "; + decl.enqueue_c_namespace_end += " } "; + } + else { + decl.enqueue_c_namespace_start += "::" + decl.module_name; + decl.enqueue_c_namespace_end = ""; + } + decl.enqueue_c_fullname += decl.module_name + "::"; + } - decl.zeek_fullname += decl.bare_name; - decl.c_fullname += decl.bare_name; - decl.zeek_name += name; - decl.enqueue_c_fullname += decl.enqueue_c_barename; - } + decl.zeek_fullname += decl.bare_name; + decl.c_fullname += decl.bare_name; + decl.zeek_name += name; + decl.enqueue_c_fullname += decl.enqueue_c_barename; +} const char* arg_list_name = "BiF_ARGS"; @@ -174,16 +170,13 @@ static struct { #undef DEFINE_BIF_TYPE }; -int get_type_index(const char *type_name) - { - for ( int i = 0; builtin_types[i].bif_type[0] != '\0'; ++i ) - { - if ( strcmp(builtin_types[i].bif_type, type_name) == 0 ) - return i; - } - return TYPE_OTHER; - } - +int get_type_index(const char* type_name) { + for ( int i = 0; builtin_types[i].bif_type[0] != '\0'; ++i ) { + if ( strcmp(builtin_types[i].bif_type, type_name) == 0 ) + return i; + } + return TYPE_OTHER; +} int var_arg; // whether the number of arguments is variable std::vector args; @@ -192,108 +185,94 @@ extern int yyerror(const char[]); extern int yywarn(const char msg[]); extern int yylex(); -char* concat(const char* str1, const char* str2) - { - int len1 = strlen(str1); - int len2 = strlen(str2); +char* concat(const char* str1, const char* str2) { + int len1 = strlen(str1); + int len2 = strlen(str2); - char* s = new char[len1 + len2 +1]; + char* s = new char[len1 + len2 + 1]; - memcpy(s, str1, len1); - memcpy(s + len1, str2, len2); + memcpy(s, str1, len1); + memcpy(s + len1, str2, len2); - s[len1+len2] = '\0'; + s[len1 + len2] = '\0'; - return s; - } + return s; +} -static void print_event_c_prototype_args(FILE* fp) - { - for ( auto i = 0u; i < args.size(); ++i ) - { - if ( i > 0 ) - fprintf(fp, ", "); +static void print_event_c_prototype_args(FILE * fp) { + for ( auto i = 0u; i < args.size(); ++i ) { + if ( i > 0 ) + fprintf(fp, ", "); - args[i]->PrintCArg(fp, i); - } - } + args[i]->PrintCArg(fp, i); + } +} -static void print_event_c_prototype_header(FILE* fp) - { - fprintf(fp, "namespace zeek::%s { void %s(zeek::analyzer::Analyzer* analyzer%s", - decl.enqueue_c_namespace_start.c_str(), - decl.enqueue_c_barename.c_str(), - args.size() ? ", " : "" ); +static void print_event_c_prototype_header(FILE * fp) { + fprintf(fp, "namespace zeek::%s { void %s(zeek::analyzer::Analyzer* analyzer%s", + decl.enqueue_c_namespace_start.c_str(), decl.enqueue_c_barename.c_str(), args.size() ? ", " : ""); - print_event_c_prototype_args(fp); - fprintf(fp, ")"); - fprintf(fp, "; %s }\n", decl.enqueue_c_namespace_end.c_str()); - } + print_event_c_prototype_args(fp); + fprintf(fp, ")"); + fprintf(fp, "; %s }\n", decl.enqueue_c_namespace_end.c_str()); +} -static void print_event_c_prototype_impl(FILE* fp) - { - fprintf(fp, "void %s(zeek::analyzer::Analyzer* analyzer%s", - decl.enqueue_c_fullname.c_str(), - args.size() ? ", " : "" ); +static void print_event_c_prototype_impl(FILE * fp) { + fprintf(fp, "void %s(zeek::analyzer::Analyzer* analyzer%s", decl.enqueue_c_fullname.c_str(), + args.size() ? ", " : ""); - print_event_c_prototype_args(fp); - fprintf(fp, ")"); - fprintf(fp, "\n"); - } + print_event_c_prototype_args(fp); + fprintf(fp, ")"); + fprintf(fp, "\n"); +} -static void print_event_c_body(FILE* fp) - { - fprintf(fp, "\t{\n"); - fprintf(fp, "\t// Note that it is intentional that here we do not\n"); - fprintf(fp, "\t// check if %s is NULL, which should happen *before*\n", - decl.c_fullname.c_str()); - fprintf(fp, "\t// %s is called to avoid unnecessary Val\n", - decl.enqueue_c_fullname.c_str()); - fprintf(fp, "\t// allocation.\n"); - fprintf(fp, "\n"); +static void print_event_c_body(FILE * fp) { + fprintf(fp, "\t{\n"); + fprintf(fp, "\t// Note that it is intentional that here we do not\n"); + fprintf(fp, "\t// check if %s is NULL, which should happen *before*\n", decl.c_fullname.c_str()); + fprintf(fp, "\t// %s is called to avoid unnecessary Val\n", decl.enqueue_c_fullname.c_str()); + fprintf(fp, "\t// allocation.\n"); + fprintf(fp, "\n"); - BuiltinFuncArg* connection_arg = nullptr; + BuiltinFuncArg* connection_arg = nullptr; - fprintf(fp, "\tzeek::event_mgr.Enqueue(%s, zeek::Args{\n", decl.c_fullname.c_str()); + fprintf(fp, "\tzeek::event_mgr.Enqueue(%s, zeek::Args{\n", decl.c_fullname.c_str()); - for ( int i = 0; i < (int) args.size(); ++i ) - { - fprintf(fp, "\t "); - args[i]->PrintValConstructor(fp); - fprintf(fp, ",\n"); + for ( int i = 0; i < (int)args.size(); ++i ) { + fprintf(fp, "\t "); + args[i]->PrintValConstructor(fp); + fprintf(fp, ",\n"); - if ( args[i]->Type() == TYPE_CONNECTION ) - { - if ( connection_arg == nullptr ) - connection_arg = args[i]; - else - { - // We are seeing two connection type arguments. - yywarn("Warning: with more than connection-type " - "event arguments, bifcl only passes " - "the first one to EventMgr as cookie."); - } - } - } + if ( args[i]->Type() == TYPE_CONNECTION ) { + if ( connection_arg == nullptr ) + connection_arg = args[i]; + else { + // We are seeing two connection type arguments. + yywarn( + "Warning: with more than connection-type " + "event arguments, bifcl only passes " + "the first one to EventMgr as cookie."); + } + } + } - fprintf(fp, "\t },\n\t zeek::util::detail::SOURCE_LOCAL, analyzer ? analyzer->GetID() : 0"); + fprintf(fp, "\t },\n\t zeek::util::detail::SOURCE_LOCAL, analyzer ? analyzer->GetID() : 0"); - if ( connection_arg ) - // Pass the connection to the EventMgr as the "cookie" - fprintf(fp, ", %s", connection_arg->Name()); + if ( connection_arg ) + // Pass the connection to the EventMgr as the "cookie" + fprintf(fp, ", %s", connection_arg->Name()); - fprintf(fp, ");\n"); - fprintf(fp, "\t}\n\n"); - //fprintf(fp, "%s // end namespace\n", decl.enqueue_c_namespace_end.c_str()); - } + fprintf(fp, ");\n"); + fprintf(fp, "\t}\n\n"); + // fprintf(fp, "%s // end namespace\n", decl.enqueue_c_namespace_end.c_str()); +} -void record_bif_item(const char* id, const char* type) - { - if ( ! plugin ) - return; +void record_bif_item(const char* id, const char* type) { + if ( ! plugin ) + return; - fprintf(fp_func_init, "\tplugin->AddBifItem(\"%s\", zeek::plugin::BifItem::%s);\n", id, type); - } + fprintf(fp_func_init, "\tplugin->AddBifItem(\"%s\", zeek::plugin::BifItem::%s);\n", id, type); +} %} @@ -792,44 +771,41 @@ extern char* input_filename; extern int line_number; void err_exit(void); -void print_msg(const char msg[]) - { - int msg_len = strlen(msg) + strlen(yytext) + 64; - char* msgbuf = new char[msg_len]; +void print_msg(const char msg[]) { + int msg_len = strlen(msg) + strlen(yytext) + 64; + char* msgbuf = new char[msg_len]; - if ( yytext[0] == '\n' ) - snprintf(msgbuf, msg_len, "%s, on previous line", msg); + if ( yytext[0] == '\n' ) + snprintf(msgbuf, msg_len, "%s, on previous line", msg); - else if ( yytext[0] == '\0' ) - snprintf(msgbuf, msg_len, "%s, at end of file", msg); + else if ( yytext[0] == '\0' ) + snprintf(msgbuf, msg_len, "%s, at end of file", msg); - else - snprintf(msgbuf, msg_len, "%s, at or near \"%s\"", msg, yytext); + else + snprintf(msgbuf, msg_len, "%s, at or near \"%s\"", msg, yytext); - /* - extern int column; - sprintf(msgbuf, "%*s\n%*s\n", column, "^", column, msg); - */ + /* + extern int column; + sprintf(msgbuf, "%*s\n%*s\n", column, "^", column, msg); + */ - if ( input_filename ) - fprintf(stderr, "%s:%d: ", input_filename, line_number); - else - fprintf(stderr, "line %d: ", line_number); - fprintf(stderr, "%s\n", msgbuf); + if ( input_filename ) + fprintf(stderr, "%s:%d: ", input_filename, line_number); + else + fprintf(stderr, "line %d: ", line_number); + fprintf(stderr, "%s\n", msgbuf); - delete [] msgbuf; - } + delete[] msgbuf; +} -int yywarn(const char msg[]) - { - print_msg(msg); - return 0; - } +int yywarn(const char msg[]) { + print_msg(msg); + return 0; +} -int yyerror(const char msg[]) - { - print_msg(msg); +int yyerror(const char msg[]) { + print_msg(msg); - err_exit(); - return 0; - } + err_exit(); + return 0; +}