Add normalize_script_path() zeek::zeekygen::detail namespace

This commit is contained in:
Jon Siwek 2021-02-26 14:49:00 -08:00
parent 9ced370b48
commit c95a364ff7
3 changed files with 33 additions and 24 deletions

View file

@ -5,8 +5,9 @@
#include <utility> #include <utility>
#include <cstdlib> #include <cstdlib>
#include "zeek/plugin/Manager.h" #include "zeek/DebugLogger.h"
#include "zeek/util.h" #include "zeek/util.h"
#include "zeek/zeekygen/utils.h"
#include "zeek/zeekygen/Info.h" #include "zeek/zeekygen/Info.h"
#include "zeek/zeekygen/PackageInfo.h" #include "zeek/zeekygen/PackageInfo.h"
#include "zeek/zeekygen/ScriptInfo.h" #include "zeek/zeekygen/ScriptInfo.h"
@ -52,20 +53,6 @@ static string RemoveLeadingSpace(const string& s)
return rval; return rval;
} }
// Turns a script's full path into a shortened, normalized version that we
// use for indexing.
static string NormalizeScriptPath(const string& path)
{
if ( auto p = plugin_mgr->LookupPluginByPath(path) )
{
auto rval = util::detail::normalize_path(path);
auto prefix = util::SafeBasename(p->PluginDirectory()).result;
return prefix + "/" + rval.substr(p->PluginDirectory().size() + 1);
}
return util::detail::without_zeekpath_component(path);
}
Manager::Manager(const string& arg_config, const string& bro_command) Manager::Manager(const string& arg_config, const string& bro_command)
: disabled(), comment_buffer(), comment_buffer_map(), packages(), scripts(), : disabled(), comment_buffer(), comment_buffer_map(), packages(), scripts(),
identifiers(), all_info(), last_identifier_seen(), incomplete_type(), identifiers(), all_info(), last_identifier_seen(), incomplete_type(),
@ -135,7 +122,7 @@ void Manager::Script(const string& path)
if ( disabled ) if ( disabled )
return; return;
string name = NormalizeScriptPath(path); string name = normalize_script_path(path);
if ( scripts.GetInfo(name) ) if ( scripts.GetInfo(name) )
{ {
@ -179,8 +166,8 @@ void Manager::ScriptDependency(const string& path, const string& dep)
return; return;
} }
string name = NormalizeScriptPath(path); string name = normalize_script_path(path);
string depname = NormalizeScriptPath(dep); string depname = normalize_script_path(dep);
ScriptInfo* script_info = scripts.GetInfo(name); ScriptInfo* script_info = scripts.GetInfo(name);
if ( ! script_info ) if ( ! script_info )
@ -204,7 +191,7 @@ void Manager::ModuleUsage(const string& path, const string& module)
if ( disabled ) if ( disabled )
return; return;
string name = NormalizeScriptPath(path); string name = normalize_script_path(path);
ScriptInfo* script_info = scripts.GetInfo(name); ScriptInfo* script_info = scripts.GetInfo(name);
if ( ! script_info ) if ( ! script_info )
@ -263,7 +250,7 @@ void Manager::StartType(zeek::detail::IDPtr id)
return; return;
} }
string script = NormalizeScriptPath(id->GetLocationInfo()->filename); string script = normalize_script_path(id->GetLocationInfo()->filename);
ScriptInfo* script_info = scripts.GetInfo(script); ScriptInfo* script_info = scripts.GetInfo(script);
if ( ! script_info ) if ( ! script_info )
@ -327,7 +314,7 @@ void Manager::Identifier(zeek::detail::IDPtr id, bool from_redef)
return; return;
} }
string script = NormalizeScriptPath(id->GetLocationInfo()->filename); string script = normalize_script_path(id->GetLocationInfo()->filename);
ScriptInfo* script_info = scripts.GetInfo(script); ScriptInfo* script_info = scripts.GetInfo(script);
if ( ! script_info ) if ( ! script_info )
@ -357,7 +344,7 @@ void Manager::RecordField(const zeek::detail::ID* id, const TypeDecl* field,
return; return;
} }
string script = NormalizeScriptPath(path); string script = normalize_script_path(path);
idd->AddRecordField(field, script, comment_buffer, from_redef); idd->AddRecordField(field, script, comment_buffer, from_redef);
comment_buffer.clear(); comment_buffer.clear();
DBG_LOG(DBG_ZEEKYGEN, "Document record field %s, identifier %s, script %s", DBG_LOG(DBG_ZEEKYGEN, "Document record field %s, identifier %s, script %s",
@ -385,7 +372,7 @@ void Manager::Redef(const zeek::detail::ID* id, const string& path,
return; return;
} }
string from_script = NormalizeScriptPath(path); string from_script = normalize_script_path(path);
ScriptInfo* script_info = scripts.GetInfo(from_script); ScriptInfo* script_info = scripts.GetInfo(from_script);
if ( ! script_info ) if ( ! script_info )
@ -413,7 +400,7 @@ void Manager::SummaryComment(const string& script, const string& comment)
if ( disabled ) if ( disabled )
return; return;
string name = NormalizeScriptPath(script); string name = normalize_script_path(script);
ScriptInfo* info = scripts.GetInfo(name); ScriptInfo* info = scripts.GetInfo(name);
if ( info ) if ( info )

View file

@ -8,6 +8,7 @@
#include "zeek/ID.h" #include "zeek/ID.h"
#include "zeek/Scope.h" #include "zeek/Scope.h"
#include "zeek/Reporter.h" #include "zeek/Reporter.h"
#include "zeek/plugin/Manager.h"
using namespace std; using namespace std;
@ -137,4 +138,15 @@ string redef_indication(const string& from_script)
from_script.c_str()); from_script.c_str());
} }
std::string normalize_script_path(std::string_view path)
{
if ( auto p = plugin_mgr->LookupPluginByPath(path) )
{
auto rval = util::detail::normalize_path(path);
auto prefix = util::SafeBasename(p->PluginDirectory()).result;
return prefix + "/" + rval.substr(p->PluginDirectory().size() + 1);
}
return util::detail::without_zeekpath_component(path);
}
} // namespace zeek::zeekygen::detail } // namespace zeek::zeekygen::detail

View file

@ -64,4 +64,14 @@ bool is_all_whitespace(const std::string& s);
*/ */
std::string redef_indication(const std::string& from_script); std::string redef_indication(const std::string& from_script);
/**
* Turns a script's path into a shortened, normalized version that
* can be used for indexing and cross-referencing links.
*
* @param path the associate path to a Zeek script, which may be absolute.
*
* @return a normalized/shortened path (containing no ZEEKPATH components)
*
*/
std::string normalize_script_path(std::string_view path);
} // namespace zeek::zeekygen::detail } // namespace zeek::zeekygen::detail