bifcl: Run clang-format on c++ code embedded in builtin-func.{l,y}

This commit is contained in:
Tim Wojtulewicz 2025-08-21 15:12:59 +00:00
parent 31b65f70da
commit 22d82edc74
2 changed files with 391 additions and 441 deletions

View file

@ -143,11 +143,10 @@ HEX [0-9a-fA-F]+
} }
%% %%
int yywrap() int yywrap() {
{ yy_delete_buffer(YY_CURRENT_BUFFER);
yy_delete_buffer(YY_CURRENT_BUFFER); return 1;
return 1; }
}
extern int yyparse(); extern int yyparse();
char* input_filename = nullptr; char* input_filename = nullptr;
@ -171,297 +170,272 @@ void close_if_open(FILE **fpp);
void close_all_output_files(void); void close_all_output_files(void);
FILE* open_output_file(const char* surfix) FILE* open_output_file(const char* surfix) {
{ char fn[1024];
char fn[1024]; FILE* fp;
FILE* fp;
snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix); snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix);
if ( (fp = fopen(fn, "w")) == NULL ) if ( (fp = fopen(fn, "w")) == NULL ) {
{ fprintf(stderr, "Error: cannot open file: %s\n", fn);
fprintf(stderr, "Error: cannot open file: %s\n", fn); err_exit();
err_exit(); }
}
return fp; return fp;
} }
void usage() void usage() {
{ fprintf(stderr, "usage: bifcl [-p <plugin> | -s] *.bif\n");
fprintf(stderr, "usage: bifcl [-p <plugin> | -s] *.bif\n"); exit(1);
exit(1); }
}
void init_alternative_mode() void init_alternative_mode() {
{ fp_zeek_init = open_output_file("zeek");
fp_zeek_init = open_output_file("zeek"); fp_func_h = open_output_file("h");
fp_func_h = open_output_file("h"); fp_func_def = open_output_file("cc");
fp_func_def = open_output_file("cc"); fp_func_init = open_output_file("init.cc");
fp_func_init = open_output_file("init.cc"); fp_func_register = plugin ? open_output_file("register.cc") : nullptr;
fp_func_register = plugin ? open_output_file("register.cc") : nullptr;
fp_netvar_h = fp_func_h; fp_netvar_h = fp_func_h;
fp_netvar_def = fp_func_def; fp_netvar_def = fp_func_def;
fp_netvar_init = fp_func_init; fp_netvar_init = fp_func_init;
int n = 1024 + strlen(input_filename); int n = 1024 + strlen(input_filename);
auto auto_gen_comment_buf = std::make_unique<char[]>(n); auto auto_gen_comment_buf = std::make_unique<char[]>(n);
auto auto_gen_comment = auto_gen_comment_buf.get(); auto auto_gen_comment = auto_gen_comment_buf.get();
snprintf(auto_gen_comment, n, snprintf(auto_gen_comment, n, "This file was automatically generated by bifcl from %s (%s mode).",
"This file was automatically generated by bifcl from %s (%s mode).", input_filename_with_path, plugin ? "plugin" : "alternative");
input_filename_with_path, plugin ? "plugin" : "alternative");
fprintf(fp_zeek_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_def, "// %s\n\n", auto_gen_comment);
fprintf(fp_func_h, "// %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_h, "#pragma once\n\n");
fprintf(fp_func_init, "// %s\n\n", auto_gen_comment); fprintf(fp_func_init, "// %s\n\n", auto_gen_comment);
if ( fp_func_register ) if ( fp_func_register )
fprintf(fp_func_register, "// %s\n\n", auto_gen_comment); fprintf(fp_func_register, "// %s\n\n", auto_gen_comment);
static char guard[1024]; static char guard[1024];
if ( getcwd(guard, sizeof(guard)) == NULL ) if ( getcwd(guard, sizeof(guard)) == NULL ) {
{ fprintf(stderr, "Error: cannot get current working directory\n");
fprintf(stderr, "Error: cannot get current working directory\n"); err_exit();
err_exit(); }
} strncat(guard, "/", sizeof(guard) - strlen(guard) - 1);
strncat(guard, "/", sizeof(guard) - strlen(guard) - 1); strncat(guard, input_filename, sizeof(guard) - strlen(guard) - 1);
strncat(guard, input_filename, sizeof(guard) - strlen(guard) - 1);
for ( char* p = guard; *p; p++ ) for ( char* p = guard; *p; p++ ) {
{ if ( ! isalnum(*p) )
if ( ! isalnum(*p) ) *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 ZEEK_IN_NETVAR\n");
fprintf(fp_func_h, "#ifndef %s\n", guard); fprintf(fp_func_h, "#ifndef %s\n", guard);
fprintf(fp_func_h, "#define %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, "#include \"zeek/zeek-bif.h\"\n");
fprintf(fp_func_h, "#endif\n"); fprintf(fp_func_h, "#endif\n");
fprintf(fp_func_h, "#endif\n"); fprintf(fp_func_h, "#endif\n");
fprintf(fp_func_h, "\n"); fprintf(fp_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 \"%s.h\"\n", input_filename);
fprintf(fp_func_def, "#include \"zeek/Func.h\"\n"); fprintf(fp_func_def, "#include \"zeek/Func.h\"\n");
fprintf(fp_func_def, "\n"); fprintf(fp_func_def, "\n");
static char name[1024]; static char name[1024];
strncpy(name, input_filename, sizeof(name) - 1); strncpy(name, input_filename, sizeof(name) - 1);
name[sizeof(name) - 1] = '\0'; name[sizeof(name) - 1] = '\0';
char* dot = strchr(name, '.'); char* dot = strchr(name, '.');
if ( dot ) if ( dot )
*dot = '\0'; *dot = '\0';
if ( plugin ) if ( plugin ) {
{ static char plugin_canon[1024];
static char plugin_canon[1024]; strncpy(plugin_canon, plugin, sizeof(plugin_canon) - 1);
strncpy(plugin_canon, plugin, sizeof(plugin_canon) - 1); plugin_canon[sizeof(plugin_canon) - 1] = '\0';
plugin_canon[sizeof(plugin_canon) - 1] = '\0'; char* colon = strstr(plugin_canon, "::");
char* colon = strstr(plugin_canon, "::");
if ( colon ) { if ( colon ) {
*colon = '_'; *colon = '_';
memmove(colon + 1, colon + 2, plugin_canon + strlen(plugin_canon) - colon); memmove(colon + 1, colon + 2, plugin_canon + strlen(plugin_canon) - colon);
}
fprintf(fp_func_init, "\n");
fprintf(fp_func_init, "#include <list>\n");
fprintf(fp_func_init, "#include <string>\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");
} }
}
void finish_alternative_mode() fprintf(fp_func_init, "\n");
{ fprintf(fp_func_init, "#include <list>\n");
fprintf(fp_func_h, "\n"); fprintf(fp_func_init, "#include <string>\n");
fprintf(fp_func_h, "#endif\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_register, "#include \"zeek/plugin/Manager.h\"\n");
{ fprintf(fp_func_register, "\n");
fprintf(fp_func_init, "\n"); fprintf(fp_func_register, "namespace plugin::%s {\n", plugin_canon);
fprintf(fp_func_init, "\t}\n"); fprintf(fp_func_register, "void __bif_%s_init(zeek::plugin::Plugin* plugin);\n", name);
fprintf(fp_func_init, "}\n"); fprintf(fp_func_register, "zeek::plugin::detail::__RegisterBif __register_bifs_%s_%s(\"%s\", __bif_%s_init);\n",
fprintf(fp_func_init, "\n"); plugin_canon, name, plugin, name);
fprintf(fp_func_init, "\n"); 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 // GCC uses __SANITIZE_ADDRESS__, Clang uses __has_feature
#if defined(__SANITIZE_ADDRESS__) #if defined(__SANITIZE_ADDRESS__)
#define USING_ASAN #define USING_ASAN
#endif #endif
#if defined(__has_feature) #if defined(__has_feature)
#if __has_feature(address_sanitizer) #if __has_feature(address_sanitizer)
#define USING_ASAN #define USING_ASAN
#endif #endif
#endif #endif
// FreeBSD doesn't support LeakSanitizer // FreeBSD doesn't support LeakSanitizer
#if defined(USING_ASAN) && !defined(__FreeBSD__) #if defined(USING_ASAN) && ! defined(__FreeBSD__)
#include <sanitizer/lsan_interface.h> #include <sanitizer/lsan_interface.h>
#define BIFCL_LSAN_DISABLE() __lsan_disable() #define BIFCL_LSAN_DISABLE() __lsan_disable()
#else #else
#define BIFCL_LSAN_DISABLE() #define BIFCL_LSAN_DISABLE()
#endif #endif
int main(int argc, char* argv[]) int main(int argc, char* argv[]) {
{ // We generally do not care at all if bifcl is leaking and the default
// 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.
// behavior of LSAN to treat leaks as errors only trips up Zeek's build. BIFCL_LSAN_DISABLE();
BIFCL_LSAN_DISABLE();
int opt; int opt;
while ( (opt = getopt(argc, argv, "p:s")) != -1 ) while ( (opt = getopt(argc, argv, "p:s")) != -1 ) {
{ switch ( opt ) {
switch ( opt ) { case 'p':
case 'p': alternative_mode = true;
alternative_mode = true; plugin = (char*)optarg;
plugin = (char*) optarg; break;
break;
case 's': case 's': alternative_mode = true; break;
alternative_mode = true;
break;
default: default: usage();
usage(); }
} }
}
for ( int i = optind; i < argc; i++ ) for ( int i = optind; i < argc; i++ ) {
{ FILE* fp_input;
FILE* fp_input;
input_filename = input_filename_with_path = argv[i]; input_filename = input_filename_with_path = argv[i];
char* slash = strrchr(input_filename, '/'); char* slash = strrchr(input_filename, '/');
if ( (fp_input = fopen(input_filename, "r")) == NULL ) if ( (fp_input = fopen(input_filename, "r")) == NULL ) {
{ fprintf(stderr, "Error: cannot open file: %s\n", input_filename);
fprintf(stderr, "Error: cannot open file: %s\n", input_filename); /* no output files open. can simply exit */
/* no output files open. can simply exit */ exit(1);
exit(1); }
}
if ( slash ) if ( slash )
input_filename = slash + 1; input_filename = slash + 1;
if ( ! alternative_mode ) if ( ! alternative_mode ) {
{ fp_zeek_init = open_output_file("zeek");
fp_zeek_init = open_output_file("zeek"); fp_func_h = open_output_file("func_h");
fp_func_h = open_output_file("func_h"); fp_func_def = open_output_file("func_def");
fp_func_def = open_output_file("func_def"); fp_func_init = open_output_file("func_init");
fp_func_init = open_output_file("func_init"); fp_netvar_h = open_output_file("netvar_h");
fp_netvar_h = open_output_file("netvar_h"); fp_netvar_def = open_output_file("netvar_def");
fp_netvar_def = open_output_file("netvar_def"); fp_netvar_init = open_output_file("netvar_init");
fp_netvar_init = open_output_file("netvar_init");
int n = 1024 + strlen(input_filename); int n = 1024 + strlen(input_filename);
auto auto_gen_comment_buf = std::make_unique<char[]>(n); auto auto_gen_comment_buf = std::make_unique<char[]>(n);
auto auto_gen_comment = auto_gen_comment_buf.get(); auto auto_gen_comment = auto_gen_comment_buf.get();
snprintf(auto_gen_comment, n, snprintf(auto_gen_comment, n, "This file was automatically generated by bifcl from %s.", input_filename);
"This file was automatically generated by bifcl from %s.",
input_filename);
fprintf(fp_zeek_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_def, "// %s\n\n", auto_gen_comment);
fprintf(fp_func_h, "// %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_h, "#pragma once\n\n");
fprintf(fp_func_init, "// %s\n\n", auto_gen_comment); fprintf(fp_func_init, "// %s\n\n", auto_gen_comment);
fprintf(fp_netvar_def, "// %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, "// %s\n\n", auto_gen_comment);
fprintf(fp_netvar_h, "#pragma once\n\n"); fprintf(fp_netvar_h, "#pragma once\n\n");
fprintf(fp_netvar_init, "// %s\n\n", auto_gen_comment); fprintf(fp_netvar_init, "// %s\n\n", auto_gen_comment);
} }
else else
init_alternative_mode(); init_alternative_mode();
fprintf(fp_netvar_init, "#ifdef __GNUC__\n"); fprintf(fp_netvar_init, "#ifdef __GNUC__\n");
fprintf(fp_netvar_init, "#pragma GCC diagnostic push\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, "#pragma GCC diagnostic ignored \"-Wdeprecated-declarations\"\n\n");
fprintf(fp_netvar_init, "#endif\n"); fprintf(fp_netvar_init, "#endif\n");
yy_switch_to_buffer(yy_create_buffer(fp_input, YY_BUF_SIZE)); yy_switch_to_buffer(yy_create_buffer(fp_input, YY_BUF_SIZE));
yyparse(); yyparse();
fprintf(fp_netvar_init, "#ifdef __GNUC__\n"); fprintf(fp_netvar_init, "#ifdef __GNUC__\n");
fprintf(fp_netvar_init, "\n\n#pragma GCC diagnostic pop\n"); fprintf(fp_netvar_init, "\n\n#pragma GCC diagnostic pop\n");
fprintf(fp_netvar_init, "#endif\n"); fprintf(fp_netvar_init, "#endif\n");
if ( alternative_mode ) if ( alternative_mode )
finish_alternative_mode(); finish_alternative_mode();
fclose(fp_input); fclose(fp_input);
close_all_output_files(); close_all_output_files();
}
}
} void close_if_open(FILE** fpp) {
} if ( *fpp )
fclose(*fpp);
*fpp = nullptr;
}
void close_if_open(FILE **fpp) void close_all_output_files(void) {
{ close_if_open(&fp_zeek_init);
if (*fpp) close_if_open(&fp_func_h);
fclose(*fpp); close_if_open(&fp_func_def);
*fpp = nullptr; close_if_open(&fp_func_init);
} close_if_open(&fp_func_register);
void close_all_output_files(void) if ( ! alternative_mode ) {
{ close_if_open(&fp_netvar_h);
close_if_open(&fp_zeek_init); close_if_open(&fp_netvar_def);
close_if_open(&fp_func_h); close_if_open(&fp_netvar_init);
close_if_open(&fp_func_def); }
close_if_open(&fp_func_init); }
close_if_open(&fp_func_register);
if ( ! alternative_mode ) void remove_file(const char* surfix) {
{ char fn[1024];
close_if_open(&fp_netvar_h);
close_if_open(&fp_netvar_def);
close_if_open(&fp_netvar_init);
}
}
void remove_file(const char *surfix) snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix);
{ unlink(fn);
char fn[1024]; }
snprintf(fn, sizeof(fn), "%s.%s", input_filename, surfix); void err_exit(void) {
unlink(fn); close_all_output_files();
} /* clean up. remove all output files we've generated so far */
remove_file("zeek");
void err_exit(void) remove_file("func_h");
{ remove_file("func_def");
close_all_output_files(); remove_file("func_init");
/* clean up. remove all output files we've generated so far */ remove_file("func_register");
remove_file("zeek"); remove_file("netvar_h");
remove_file("func_h"); remove_file("netvar_def");
remove_file("func_def"); remove_file("netvar_init");
remove_file("func_init"); exit(1);
remove_file("func_register"); }
remove_file("netvar_h");
remove_file("netvar_def");
remove_file("netvar_init");
exit(1);
}

View file

@ -65,92 +65,88 @@ struct decl_struct {
string enqueue_c_fullname; string enqueue_c_fullname;
} decl; } decl;
void set_definition_type(int type, const char *arg_type_name) void set_definition_type(int type, const char* arg_type_name) {
{ definition_type = type;
definition_type = type; if ( type == TYPE_DEF && arg_type_name )
if ( type == TYPE_DEF && arg_type_name ) type_name = string(arg_type_name);
type_name = string(arg_type_name); else
else type_name = "";
type_name = ""; }
}
void set_decl_name(const char *name) void set_decl_name(const char* name) {
{ decl.bare_name = extract_var_name(name);
decl.bare_name = extract_var_name(name);
// make_full_var_name prepends the correct module, if any // make_full_var_name prepends the correct module, if any
// then we can extract the module name again. // then we can extract the module name again.
string varname = make_full_var_name(current_module.c_str(), name); string varname = make_full_var_name(current_module.c_str(), name);
decl.module_name = extract_module_name(varname.c_str()); decl.module_name = extract_module_name(varname.c_str());
decl.c_namespace_start = ""; decl.c_namespace_start = "";
decl.c_namespace_end = ""; decl.c_namespace_end = "";
decl.c_fullname = ""; decl.c_fullname = "";
decl.zeek_fullname = ""; decl.zeek_fullname = "";
decl.zeek_name = ""; decl.zeek_name = "";
decl.enqueue_c_fullname = ""; decl.enqueue_c_fullname = "";
decl.enqueue_c_barename = string("enqueue_") + decl.bare_name; decl.enqueue_c_barename = string("enqueue_") + decl.bare_name;
decl.enqueue_c_namespace_start = ""; decl.enqueue_c_namespace_start = "";
decl.enqueue_c_namespace_end = ""; decl.enqueue_c_namespace_end = "";
switch ( definition_type ) { switch ( definition_type ) {
case TYPE_DEF: case TYPE_DEF:
decl.c_namespace_start = "BifType::" + type_name + ""; decl.c_namespace_start = "BifType::" + type_name + "";
decl.c_fullname = "BifType::" + type_name + "::"; decl.c_fullname = "BifType::" + type_name + "::";
break; break;
case CONST_DEF: case CONST_DEF:
decl.c_namespace_start = "BifConst"; decl.c_namespace_start = "BifConst";
decl.c_fullname = "BifConst::"; decl.c_fullname = "BifConst::";
break; break;
case FUNC_DEF: case FUNC_DEF:
decl.c_namespace_start = "BifFunc"; decl.c_namespace_start = "BifFunc";
decl.c_fullname = "BifFunc::"; decl.c_fullname = "BifFunc::";
break; break;
case EVENT_DEF: case EVENT_DEF:
decl.c_namespace_start = ""; decl.c_namespace_start = "";
decl.c_namespace_end = ""; decl.c_namespace_end = "";
decl.c_fullname = "::"; // need this for namespace qualified events due do event_c_body decl.c_fullname = "::"; // need this for namespace qualified events due do event_c_body
decl.enqueue_c_namespace_start = "BifEvent"; decl.enqueue_c_namespace_start = "BifEvent";
decl.enqueue_c_fullname = "zeek::BifEvent::"; decl.enqueue_c_fullname = "zeek::BifEvent::";
break; break;
default: default: break;
break; }
}
if ( decl.module_name != GLOBAL_MODULE_NAME ) if ( decl.module_name != GLOBAL_MODULE_NAME ) {
{ if ( decl.c_namespace_start.empty() ) {
if ( decl.c_namespace_start.empty() ) { decl.c_namespace_start += "namespace " + decl.module_name + " { ";
decl.c_namespace_start += "namespace " + decl.module_name + " { "; decl.c_namespace_end += " }";
decl.c_namespace_end += " }"; }
} else {
else { decl.c_namespace_start += "::" + decl.module_name;
decl.c_namespace_start += "::" + decl.module_name; decl.c_namespace_end = "";
decl.c_namespace_end = ""; }
} decl.c_fullname += decl.module_name + "::";
decl.c_fullname += decl.module_name + "::"; decl.zeek_fullname += decl.module_name + "::";
decl.zeek_fullname += decl.module_name + "::";
if ( decl.enqueue_c_namespace_start.empty() ) { if ( decl.enqueue_c_namespace_start.empty() ) {
decl.enqueue_c_namespace_start += "namespace " + decl.module_name + " { "; decl.enqueue_c_namespace_start += "namespace " + decl.module_name + " { ";
decl.enqueue_c_namespace_end += " } "; decl.enqueue_c_namespace_end += " } ";
} }
else { else {
decl.enqueue_c_namespace_start += "::" + decl.module_name; decl.enqueue_c_namespace_start += "::" + decl.module_name;
decl.enqueue_c_namespace_end = ""; decl.enqueue_c_namespace_end = "";
} }
decl.enqueue_c_fullname += decl.module_name + "::"; decl.enqueue_c_fullname += decl.module_name + "::";
} }
decl.zeek_fullname += decl.bare_name; decl.zeek_fullname += decl.bare_name;
decl.c_fullname += decl.bare_name; decl.c_fullname += decl.bare_name;
decl.zeek_name += name; decl.zeek_name += name;
decl.enqueue_c_fullname += decl.enqueue_c_barename; decl.enqueue_c_fullname += decl.enqueue_c_barename;
} }
const char* arg_list_name = "BiF_ARGS"; const char* arg_list_name = "BiF_ARGS";
@ -174,16 +170,13 @@ static struct {
#undef DEFINE_BIF_TYPE #undef DEFINE_BIF_TYPE
}; };
int get_type_index(const char *type_name) int get_type_index(const char* type_name) {
{ for ( int i = 0; builtin_types[i].bif_type[0] != '\0'; ++i ) {
for ( int i = 0; builtin_types[i].bif_type[0] != '\0'; ++i ) if ( strcmp(builtin_types[i].bif_type, type_name) == 0 )
{ return i;
if ( strcmp(builtin_types[i].bif_type, type_name) == 0 ) }
return i; return TYPE_OTHER;
} }
return TYPE_OTHER;
}
int var_arg; // whether the number of arguments is variable int var_arg; // whether the number of arguments is variable
std::vector<BuiltinFuncArg*> args; std::vector<BuiltinFuncArg*> args;
@ -192,108 +185,94 @@ extern int yyerror(const char[]);
extern int yywarn(const char msg[]); extern int yywarn(const char msg[]);
extern int yylex(); extern int yylex();
char* concat(const char* str1, const char* str2) char* concat(const char* str1, const char* str2) {
{ int len1 = strlen(str1);
int len1 = strlen(str1); int len2 = strlen(str2);
int len2 = strlen(str2);
char* s = new char[len1 + len2 +1]; char* s = new char[len1 + len2 + 1];
memcpy(s, str1, len1); memcpy(s, str1, len1);
memcpy(s + len1, str2, len2); 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) static void print_event_c_prototype_args(FILE * fp) {
{ for ( auto i = 0u; i < args.size(); ++i ) {
for ( auto i = 0u; i < args.size(); ++i ) if ( i > 0 )
{ fprintf(fp, ", ");
if ( i > 0 )
fprintf(fp, ", ");
args[i]->PrintCArg(fp, i); args[i]->PrintCArg(fp, i);
} }
} }
static void print_event_c_prototype_header(FILE* fp) static void print_event_c_prototype_header(FILE * fp) {
{ fprintf(fp, "namespace zeek::%s { void %s(zeek::analyzer::Analyzer* analyzer%s",
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() ? ", " : "");
decl.enqueue_c_namespace_start.c_str(),
decl.enqueue_c_barename.c_str(),
args.size() ? ", " : "" );
print_event_c_prototype_args(fp); print_event_c_prototype_args(fp);
fprintf(fp, ")"); fprintf(fp, ")");
fprintf(fp, "; %s }\n", decl.enqueue_c_namespace_end.c_str()); fprintf(fp, "; %s }\n", decl.enqueue_c_namespace_end.c_str());
} }
static void print_event_c_prototype_impl(FILE* fp) static void print_event_c_prototype_impl(FILE * fp) {
{ fprintf(fp, "void %s(zeek::analyzer::Analyzer* analyzer%s", decl.enqueue_c_fullname.c_str(),
fprintf(fp, "void %s(zeek::analyzer::Analyzer* analyzer%s", args.size() ? ", " : "");
decl.enqueue_c_fullname.c_str(),
args.size() ? ", " : "" );
print_event_c_prototype_args(fp); print_event_c_prototype_args(fp);
fprintf(fp, ")"); fprintf(fp, ")");
fprintf(fp, "\n"); fprintf(fp, "\n");
} }
static void print_event_c_body(FILE* fp) static void print_event_c_body(FILE * fp) {
{ fprintf(fp, "\t{\n");
fprintf(fp, "\t{\n"); fprintf(fp, "\t// Note that it is intentional that here we do not\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// check if %s is NULL, which should happen *before*\n", fprintf(fp, "\t// %s is called to avoid unnecessary Val\n", decl.enqueue_c_fullname.c_str());
decl.c_fullname.c_str()); fprintf(fp, "\t// allocation.\n");
fprintf(fp, "\t// %s is called to avoid unnecessary Val\n", fprintf(fp, "\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 ) for ( int i = 0; i < (int)args.size(); ++i ) {
{ fprintf(fp, "\t ");
fprintf(fp, "\t "); args[i]->PrintValConstructor(fp);
args[i]->PrintValConstructor(fp); fprintf(fp, ",\n");
fprintf(fp, ",\n");
if ( args[i]->Type() == TYPE_CONNECTION ) if ( args[i]->Type() == TYPE_CONNECTION ) {
{ if ( connection_arg == nullptr )
if ( connection_arg == nullptr ) connection_arg = args[i];
connection_arg = args[i]; else {
else // We are seeing two connection type arguments.
{ yywarn(
// We are seeing two connection type arguments. "Warning: with more than connection-type "
yywarn("Warning: with more than connection-type " "event arguments, bifcl only passes "
"event arguments, bifcl only passes " "the first one to EventMgr as cookie.");
"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 ) if ( connection_arg )
// Pass the connection to the EventMgr as the "cookie" // Pass the connection to the EventMgr as the "cookie"
fprintf(fp, ", %s", connection_arg->Name()); fprintf(fp, ", %s", connection_arg->Name());
fprintf(fp, ");\n"); fprintf(fp, ");\n");
fprintf(fp, "\t}\n\n"); fprintf(fp, "\t}\n\n");
//fprintf(fp, "%s // end namespace\n", decl.enqueue_c_namespace_end.c_str()); // fprintf(fp, "%s // end namespace\n", decl.enqueue_c_namespace_end.c_str());
} }
void record_bif_item(const char* id, const char* type) void record_bif_item(const char* id, const char* type) {
{ if ( ! plugin )
if ( ! plugin ) return;
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; extern int line_number;
void err_exit(void); void err_exit(void);
void print_msg(const char msg[]) void print_msg(const char msg[]) {
{ int msg_len = strlen(msg) + strlen(yytext) + 64;
int msg_len = strlen(msg) + strlen(yytext) + 64; char* msgbuf = new char[msg_len];
char* msgbuf = new char[msg_len];
if ( yytext[0] == '\n' ) if ( yytext[0] == '\n' )
snprintf(msgbuf, msg_len, "%s, on previous line", msg); snprintf(msgbuf, msg_len, "%s, on previous line", msg);
else if ( yytext[0] == '\0' ) else if ( yytext[0] == '\0' )
snprintf(msgbuf, msg_len, "%s, at end of file", msg); snprintf(msgbuf, msg_len, "%s, at end of file", msg);
else else
snprintf(msgbuf, msg_len, "%s, at or near \"%s\"", msg, yytext); snprintf(msgbuf, msg_len, "%s, at or near \"%s\"", msg, yytext);
/* /*
extern int column; extern int column;
sprintf(msgbuf, "%*s\n%*s\n", column, "^", column, msg); sprintf(msgbuf, "%*s\n%*s\n", column, "^", column, msg);
*/ */
if ( input_filename ) if ( input_filename )
fprintf(stderr, "%s:%d: ", input_filename, line_number); fprintf(stderr, "%s:%d: ", input_filename, line_number);
else else
fprintf(stderr, "line %d: ", line_number); fprintf(stderr, "line %d: ", line_number);
fprintf(stderr, "%s\n", msgbuf); fprintf(stderr, "%s\n", msgbuf);
delete [] msgbuf; delete[] msgbuf;
} }
int yywarn(const char msg[]) int yywarn(const char msg[]) {
{ print_msg(msg);
print_msg(msg); return 0;
return 0; }
}
int yyerror(const char msg[]) int yyerror(const char msg[]) {
{ print_msg(msg);
print_msg(msg);
err_exit(); err_exit();
return 0; return 0;
} }