mirror of
https://github.com/zeek/zeek.git
synced 2025-10-06 16:48:19 +00:00
Reformat Zeek in Spicy style
This largely copies over Spicy's `.clang-format` configuration file. The one place where we deviate is header include order since Zeek depends on headers being included in a certain order.
This commit is contained in:
parent
7b8e7ed72c
commit
f5a76c1aed
786 changed files with 131714 additions and 153609 deletions
|
@ -7,167 +7,144 @@
|
|||
#include "zeek/RE.h"
|
||||
#include "zeek/Reporter.h"
|
||||
|
||||
namespace zeek
|
||||
{
|
||||
namespace zeek {
|
||||
|
||||
EventRegistry::EventRegistry() = default;
|
||||
EventRegistry::~EventRegistry() noexcept = default;
|
||||
|
||||
EventHandlerPtr EventRegistry::Register(std::string_view name, bool is_from_script)
|
||||
{
|
||||
// If there already is an entry in the registry, we have a
|
||||
// local handler on the script layer.
|
||||
EventHandler* h = event_registry->Lookup(name);
|
||||
EventHandlerPtr EventRegistry::Register(std::string_view name, bool is_from_script) {
|
||||
// If there already is an entry in the registry, we have a
|
||||
// local handler on the script layer.
|
||||
EventHandler* h = event_registry->Lookup(name);
|
||||
|
||||
if ( h )
|
||||
{
|
||||
if ( ! is_from_script )
|
||||
not_only_from_script.insert(std::string(name));
|
||||
if ( h ) {
|
||||
if ( ! is_from_script )
|
||||
not_only_from_script.insert(std::string(name));
|
||||
|
||||
h->SetUsed();
|
||||
return h;
|
||||
}
|
||||
h->SetUsed();
|
||||
return h;
|
||||
}
|
||||
|
||||
h = new EventHandler(std::string(name));
|
||||
event_registry->Register(h, is_from_script);
|
||||
h = new EventHandler(std::string(name));
|
||||
event_registry->Register(h, is_from_script);
|
||||
|
||||
h->SetUsed();
|
||||
h->SetUsed();
|
||||
|
||||
return h;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
void EventRegistry::Register(EventHandlerPtr handler, bool is_from_script)
|
||||
{
|
||||
std::string name = handler->Name();
|
||||
void EventRegistry::Register(EventHandlerPtr handler, bool is_from_script) {
|
||||
std::string name = handler->Name();
|
||||
|
||||
handlers[name] = std::unique_ptr<EventHandler>(handler.Ptr());
|
||||
handlers[name] = std::unique_ptr<EventHandler>(handler.Ptr());
|
||||
|
||||
if ( ! is_from_script )
|
||||
not_only_from_script.insert(name);
|
||||
}
|
||||
if ( ! is_from_script )
|
||||
not_only_from_script.insert(name);
|
||||
}
|
||||
|
||||
EventHandler* EventRegistry::Lookup(std::string_view name)
|
||||
{
|
||||
auto it = handlers.find(name);
|
||||
if ( it != handlers.end() )
|
||||
return it->second.get();
|
||||
EventHandler* EventRegistry::Lookup(std::string_view name) {
|
||||
auto it = handlers.find(name);
|
||||
if ( it != handlers.end() )
|
||||
return it->second.get();
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool EventRegistry::NotOnlyRegisteredFromScript(std::string_view name)
|
||||
{
|
||||
return not_only_from_script.count(std::string(name)) > 0;
|
||||
}
|
||||
bool EventRegistry::NotOnlyRegisteredFromScript(std::string_view name) {
|
||||
return not_only_from_script.count(std::string(name)) > 0;
|
||||
}
|
||||
|
||||
EventRegistry::string_list EventRegistry::Match(RE_Matcher* pattern)
|
||||
{
|
||||
string_list names;
|
||||
EventRegistry::string_list EventRegistry::Match(RE_Matcher* pattern) {
|
||||
string_list names;
|
||||
|
||||
for ( const auto& entry : handlers )
|
||||
{
|
||||
EventHandler* v = entry.second.get();
|
||||
if ( v->GetFunc() && pattern->MatchExactly(v->Name()) )
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
for ( const auto& entry : handlers ) {
|
||||
EventHandler* v = entry.second.get();
|
||||
if ( v->GetFunc() && pattern->MatchExactly(v->Name()) )
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
EventRegistry::string_list EventRegistry::UnusedHandlers()
|
||||
{
|
||||
string_list names;
|
||||
EventRegistry::string_list EventRegistry::UnusedHandlers() {
|
||||
string_list names;
|
||||
|
||||
for ( const auto& entry : handlers )
|
||||
{
|
||||
EventHandler* v = entry.second.get();
|
||||
if ( v->GetFunc() && ! v->Used() )
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
for ( const auto& entry : handlers ) {
|
||||
EventHandler* v = entry.second.get();
|
||||
if ( v->GetFunc() && ! v->Used() )
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
EventRegistry::string_list EventRegistry::UsedHandlers()
|
||||
{
|
||||
string_list names;
|
||||
EventRegistry::string_list EventRegistry::UsedHandlers() {
|
||||
string_list names;
|
||||
|
||||
for ( const auto& entry : handlers )
|
||||
{
|
||||
EventHandler* v = entry.second.get();
|
||||
if ( v->GetFunc() && v->Used() )
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
for ( const auto& entry : handlers ) {
|
||||
EventHandler* v = entry.second.get();
|
||||
if ( v->GetFunc() && v->Used() )
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
EventRegistry::string_list EventRegistry::AllHandlers()
|
||||
{
|
||||
string_list names;
|
||||
EventRegistry::string_list EventRegistry::AllHandlers() {
|
||||
string_list names;
|
||||
|
||||
for ( const auto& entry : handlers )
|
||||
{
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
for ( const auto& entry : handlers ) {
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
void EventRegistry::PrintDebug()
|
||||
{
|
||||
for ( const auto& entry : handlers )
|
||||
{
|
||||
EventHandler* v = entry.second.get();
|
||||
fprintf(stderr, "Registered event %s (%s handler / %s)\n", v->Name(),
|
||||
v->GetFunc() ? "local" : "no", *v ? "active" : "not active");
|
||||
}
|
||||
}
|
||||
void EventRegistry::PrintDebug() {
|
||||
for ( const auto& entry : handlers ) {
|
||||
EventHandler* v = entry.second.get();
|
||||
fprintf(stderr, "Registered event %s (%s handler / %s)\n", v->Name(), v->GetFunc() ? "local" : "no",
|
||||
*v ? "active" : "not active");
|
||||
}
|
||||
}
|
||||
|
||||
void EventRegistry::SetErrorHandler(std::string_view name)
|
||||
{
|
||||
EventHandler* eh = Lookup(name);
|
||||
void EventRegistry::SetErrorHandler(std::string_view name) {
|
||||
EventHandler* eh = Lookup(name);
|
||||
|
||||
if ( eh )
|
||||
{
|
||||
eh->SetErrorHandler();
|
||||
return;
|
||||
}
|
||||
if ( eh ) {
|
||||
eh->SetErrorHandler();
|
||||
return;
|
||||
}
|
||||
|
||||
reporter->InternalWarning("unknown event handler '%s' in SetErrorHandler()",
|
||||
std::string(name).c_str());
|
||||
}
|
||||
reporter->InternalWarning("unknown event handler '%s' in SetErrorHandler()", std::string(name).c_str());
|
||||
}
|
||||
|
||||
void EventRegistry::ActivateAllHandlers()
|
||||
{
|
||||
auto event_names = AllHandlers();
|
||||
for ( const auto& name : event_names )
|
||||
{
|
||||
if ( auto event = Lookup(name) )
|
||||
event->SetGenerateAlways();
|
||||
}
|
||||
}
|
||||
void EventRegistry::ActivateAllHandlers() {
|
||||
auto event_names = AllHandlers();
|
||||
for ( const auto& name : event_names ) {
|
||||
if ( auto event = Lookup(name) )
|
||||
event->SetGenerateAlways();
|
||||
}
|
||||
}
|
||||
|
||||
EventGroupPtr EventRegistry::RegisterGroup(EventGroupKind kind, std::string_view name)
|
||||
{
|
||||
auto key = std::pair{kind, std::string{name}};
|
||||
if ( const auto& it = event_groups.find(key); it != event_groups.end() )
|
||||
return it->second;
|
||||
EventGroupPtr EventRegistry::RegisterGroup(EventGroupKind kind, std::string_view name) {
|
||||
auto key = std::pair{kind, std::string{name}};
|
||||
if ( const auto& it = event_groups.find(key); it != event_groups.end() )
|
||||
return it->second;
|
||||
|
||||
auto group = std::make_shared<EventGroup>(kind, name);
|
||||
return event_groups.emplace(key, group).first->second;
|
||||
}
|
||||
auto group = std::make_shared<EventGroup>(kind, name);
|
||||
return event_groups.emplace(key, group).first->second;
|
||||
}
|
||||
|
||||
EventGroupPtr EventRegistry::LookupGroup(EventGroupKind kind, std::string_view name)
|
||||
{
|
||||
auto key = std::pair{kind, std::string{name}};
|
||||
if ( const auto& it = event_groups.find(key); it != event_groups.end() )
|
||||
return it->second;
|
||||
EventGroupPtr EventRegistry::LookupGroup(EventGroupKind kind, std::string_view name) {
|
||||
auto key = std::pair{kind, std::string{name}};
|
||||
if ( const auto& it = event_groups.find(key); it != event_groups.end() )
|
||||
return it->second;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
EventGroup::EventGroup(EventGroupKind kind, std::string_view name) : kind(kind), name(name) { }
|
||||
EventGroup::EventGroup(EventGroupKind kind, std::string_view name) : kind(kind), name(name) {}
|
||||
|
||||
// Run through all ScriptFunc instances associated with this group and
|
||||
// update their bodies after a group's enable/disable state has changed.
|
||||
|
@ -177,50 +154,36 @@ EventGroup::EventGroup(EventGroupKind kind, std::string_view name) : kind(kind),
|
|||
// EventGroup is private friend with Func, so fiddling with the bodies
|
||||
// and private members works and keeps the logic out of Func and away
|
||||
// from the public zeek:: namespace.
|
||||
void EventGroup::UpdateFuncBodies()
|
||||
{
|
||||
static auto is_group_disabled = [](const auto& g)
|
||||
{
|
||||
return g->IsDisabled();
|
||||
};
|
||||
void EventGroup::UpdateFuncBodies() {
|
||||
static auto is_group_disabled = [](const auto& g) { return g->IsDisabled(); };
|
||||
|
||||
for ( auto& func : funcs )
|
||||
{
|
||||
for ( auto& b : func->bodies )
|
||||
b.disabled = std::any_of(b.groups.cbegin(), b.groups.cend(), is_group_disabled);
|
||||
for ( auto& func : funcs ) {
|
||||
for ( auto& b : func->bodies )
|
||||
b.disabled = std::any_of(b.groups.cbegin(), b.groups.cend(), is_group_disabled);
|
||||
|
||||
static auto is_body_enabled = [](const auto& b)
|
||||
{
|
||||
return ! b.disabled;
|
||||
};
|
||||
func->has_enabled_bodies = std::any_of(func->bodies.cbegin(), func->bodies.cend(),
|
||||
is_body_enabled);
|
||||
}
|
||||
}
|
||||
static auto is_body_enabled = [](const auto& b) { return ! b.disabled; };
|
||||
func->has_enabled_bodies = std::any_of(func->bodies.cbegin(), func->bodies.cend(), is_body_enabled);
|
||||
}
|
||||
}
|
||||
|
||||
void EventGroup::Enable()
|
||||
{
|
||||
if ( enabled )
|
||||
return;
|
||||
void EventGroup::Enable() {
|
||||
if ( enabled )
|
||||
return;
|
||||
|
||||
enabled = true;
|
||||
enabled = true;
|
||||
|
||||
UpdateFuncBodies();
|
||||
}
|
||||
UpdateFuncBodies();
|
||||
}
|
||||
|
||||
void EventGroup::Disable()
|
||||
{
|
||||
if ( ! enabled )
|
||||
return;
|
||||
void EventGroup::Disable() {
|
||||
if ( ! enabled )
|
||||
return;
|
||||
|
||||
enabled = false;
|
||||
enabled = false;
|
||||
|
||||
UpdateFuncBodies();
|
||||
}
|
||||
UpdateFuncBodies();
|
||||
}
|
||||
|
||||
void EventGroup::AddFunc(detail::ScriptFuncPtr f)
|
||||
{
|
||||
funcs.insert(f);
|
||||
}
|
||||
void EventGroup::AddFunc(detail::ScriptFuncPtr f) { funcs.insert(f); }
|
||||
|
||||
} // namespace zeek
|
||||
} // namespace zeek
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue