mirror of
https://github.com/zeek/zeek.git
synced 2025-10-08 09:38:19 +00:00
Add support for enum with explicit enumerator values.
* Adding support for enums with explicit enumerator values (see doc below) to bifcl and policy layer. * Bifcl: remove (partially written) output files on error and do a nice exit(1) instead of harsh abort() on parse errors. * CMakeText: if bifcl fails, remove output files (failsafe, in case bifcl fails to clean up after itself). Enum description ---------------- Enum's are supported in .bif and .bro scripts. An enum in a bif will become available in the event engine and the policy layer. Enums are "C-style". The first element in an enum will have a value of 0, the next value will be 1, etc. It is possible to assign an enumerator value to an element. If next element does not have an explicit value, its values will be the value of the last element + 1 Example:: type foo: enum { BAR_A, # value will be 0 BAR_B, # value will be 1 BAR_C = 10, # value will be 10 BAR_D, # value will be 11 }; Enumerator values can only by positive integer literals. The literals can be specified in (0x....), but not in octal (bro policy layer limitation). So, do not use 0123 as value in bifs! Each enumerator value can only be used once per enum (C allows to use the same value multiple times). This makes reverse mapping from value to name (e.g., in %s format strings) unambigious. This is enforced in by the policy script. Enums can be redef'ed, i.e., extended. Enumerator values will continue to increment. If there are multiple redefs in different policy scripts, then name <-> value mappings will obviously depend on the order in which scripts are loaded (which might not be obvious). Example:: redef enum foo += { BAR_E, # value will be 12 BAR_F = 5, # value will be 5 BAR_G, # value will be 6 };
This commit is contained in:
parent
2f7fa3470b
commit
72454c230b
6 changed files with 188 additions and 103 deletions
54
src/Type.cc
54
src/Type.cc
|
@ -1082,10 +1082,9 @@ bool FileType::DoUnserialize(UnserialInfo* info)
|
|||
return yield != 0;
|
||||
}
|
||||
|
||||
EnumType::EnumType(bool arg_is_export)
|
||||
EnumType::EnumType()
|
||||
: BroType(TYPE_ENUM)
|
||||
{
|
||||
is_export = arg_is_export;
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
|
@ -1095,9 +1094,18 @@ EnumType::~EnumType()
|
|||
delete [] iter->first;
|
||||
}
|
||||
|
||||
int EnumType::AddName(const string& module_name, const char* name)
|
||||
bro_int_t EnumType::AddName(const string& module_name, const char* name, bool is_export)
|
||||
{
|
||||
ID* id = lookup_ID(name, module_name.c_str());
|
||||
return AddName(module_name, name, counter, is_export);
|
||||
}
|
||||
|
||||
bro_int_t EnumType::AddName(const string& module_name, const char* name, bro_int_t val, bool is_export)
|
||||
{
|
||||
ID *id;
|
||||
if ( Lookup(val) )
|
||||
return -1;
|
||||
|
||||
id = lookup_ID(name, module_name.c_str());
|
||||
if ( ! id )
|
||||
{
|
||||
id = install_ID(name, module_name.c_str(), true, is_export);
|
||||
|
@ -1105,32 +1113,15 @@ int EnumType::AddName(const string& module_name, const char* name)
|
|||
id->SetEnumConst();
|
||||
}
|
||||
else
|
||||
{
|
||||
debug_msg("identifier already exists: %s\n", name);
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
|
||||
string fullname = make_full_var_name(module_name.c_str(), name);
|
||||
names[copy_string(fullname.c_str())] = counter;
|
||||
return counter++;
|
||||
names[copy_string(fullname.c_str())] = val;
|
||||
counter = val + 1;
|
||||
return val;
|
||||
}
|
||||
|
||||
int EnumType::AddNamesFrom(const string& module_name, EnumType* et)
|
||||
{
|
||||
int last_added = counter;
|
||||
for ( NameMap::iterator iter = et->names.begin();
|
||||
iter != et->names.end(); ++iter )
|
||||
{
|
||||
ID* id = lookup_ID(iter->first, module_name.c_str());
|
||||
id->SetType(this->Ref());
|
||||
names[copy_string(id->Name())] = counter;
|
||||
last_added = counter++;
|
||||
}
|
||||
|
||||
return last_added;
|
||||
}
|
||||
|
||||
int EnumType::Lookup(const string& module_name, const char* name)
|
||||
bro_int_t EnumType::Lookup(const string& module_name, const char* name)
|
||||
{
|
||||
NameMap::iterator pos =
|
||||
names.find(make_full_var_name(module_name.c_str(), name).c_str());
|
||||
|
@ -1141,7 +1132,7 @@ int EnumType::Lookup(const string& module_name, const char* name)
|
|||
return pos->second;
|
||||
}
|
||||
|
||||
const char* EnumType::Lookup(int value)
|
||||
const char* EnumType::Lookup(bro_int_t value)
|
||||
{
|
||||
for ( NameMap::iterator iter = names.begin();
|
||||
iter != names.end(); ++iter )
|
||||
|
@ -1157,9 +1148,7 @@ bool EnumType::DoSerialize(SerialInfo* info) const
|
|||
{
|
||||
DO_SERIALIZE(SER_ENUM_TYPE, BroType);
|
||||
|
||||
// I guess we don't really need both ...
|
||||
if ( ! (SERIALIZE(counter) && SERIALIZE((unsigned int) names.size()) &&
|
||||
SERIALIZE(is_export)) )
|
||||
if ( ! (SERIALIZE(counter) && SERIALIZE((unsigned int) names.size())) )
|
||||
return false;
|
||||
|
||||
for ( NameMap::const_iterator iter = names.begin();
|
||||
|
@ -1178,14 +1167,13 @@ bool EnumType::DoUnserialize(UnserialInfo* info)
|
|||
|
||||
unsigned int len;
|
||||
if ( ! UNSERIALIZE(&counter) ||
|
||||
! UNSERIALIZE(&len) ||
|
||||
! UNSERIALIZE(&is_export) )
|
||||
! UNSERIALIZE(&len) )
|
||||
return false;
|
||||
|
||||
while ( len-- )
|
||||
{
|
||||
const char* name;
|
||||
int val;
|
||||
bro_int_t val;
|
||||
if ( ! (UNSERIALIZE_STR(&name, 0) && UNSERIALIZE(&val)) )
|
||||
return false;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue