diff options
author | Hadriel Kaplan <hadrielk@yahoo.com> | 2015-07-10 11:34:04 -0400 |
---|---|---|
committer | Anders Broman <a.broman58@gmail.com> | 2015-07-23 09:22:44 +0000 |
commit | 23163520ad2a96aa9c22ebae8f3fcc91e93d2461 (patch) | |
tree | f9fb4d78a8b235b4daa6101d58eeff079f483999 | |
parent | 2c534a93acc64703921b4170023e4b8bae507bc7 (diff) |
Lua: add plugin version info
Expose a "set_plugin_info" global function to set the Lua plugin's
version information. Also, put info about Lua scripts loaded from
the command-line into the help output, not just for scripts loaded
from plugin direvtories.
Bug: 11315
Change-Id: I8bc425ed1ed0dfdc1d05178754f44d44e0b209b5
Reviewed-on: https://code.wireshark.org/review/9593
Petri-Dish: Hadriel Kaplan <hadrielk@yahoo.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
-rw-r--r-- | epan/wslua/init_wslua.c | 40 | ||||
-rw-r--r-- | epan/wslua/wslua.h | 3 | ||||
-rw-r--r-- | epan/wslua/wslua_util.c | 64 |
3 files changed, 101 insertions, 6 deletions
diff --git a/epan/wslua/init_wslua.c b/epan/wslua/init_wslua.c index fd42080ada..f0d64b3e3f 100644 --- a/epan/wslua/init_wslua.c +++ b/epan/wslua/init_wslua.c @@ -492,6 +492,7 @@ static void set_file_environment(const gchar* filename, const gchar* dirname) { g_free(personal); } + /* If file_count > 0 then it's a command-line-added user script, and the count * represents which user script it is (first=1, second=2, etc.). * If dirname != NULL, then it's a user script and the dirname will get put in a file environment @@ -545,6 +546,32 @@ static gboolean lua_load_script(const gchar* filename, const gchar* dirname, con } } +/* This one is used to load the init.lua scripts, or anything else + * that shouldn't really be considered a real plugin. + */ +static gboolean lua_load_internal_script(const gchar* filename) { + return lua_load_script(filename, NULL, 0); +} + +/* This one is used to load plugins: either from the plugin directories, + * or from the command line. + */ +static gboolean lua_load_plugin_script(const gchar* name, + const gchar* filename, + const gchar* dirname, + const int file_count) +{ + if (lua_load_script(filename, dirname, file_count)) { + wslua_add_plugin(g_strdup(name), + g_strdup(get_current_plugin_version()), + g_strdup(filename)); + clear_current_plugin_version(); + return TRUE; + } + return FALSE; +} + + static void basic_logger(const gchar *log_domain _U_, GLogLevelFlags log_level _U_, const gchar *message, @@ -598,9 +625,7 @@ static int lua_load_plugins(const char *dirname, register_cb cb, gpointer client if (!count_only) { if (cb) (*cb)(RA_LUA_PLUGINS, name, client_data); - if (lua_load_script(filename, is_user ? dirname : NULL, 0)) { - wslua_add_plugin(g_strdup(name), g_strdup(""), g_strdup(filename)); - } + lua_load_plugin_script(name, filename, is_user ? dirname : NULL, 0); } plugins_counter++; } @@ -840,7 +865,7 @@ int wslua_init(register_cb cb, gpointer client_data) { } if (( file_exists(filename))) { - lua_load_script(filename, NULL, 0); + lua_load_internal_script(filename); } g_free(filename); @@ -875,7 +900,7 @@ int wslua_init(register_cb cb, gpointer client_data) { if ((file_exists(filename))) { if (cb) (*cb)(RA_LUA_PLUGINS, get_basename(filename), client_data); - lua_load_script(filename, NULL, 0); + lua_load_internal_script(filename); } g_free(filename); @@ -892,7 +917,10 @@ int wslua_init(register_cb cb, gpointer client_data) { if (cb) (*cb)(RA_LUA_PLUGINS, get_basename(script_filename), client_data); - lua_load_script(script_filename, dname ? dname : "", file_count); + lua_load_plugin_script(ws_dir_get_name(script_filename), + script_filename, + dname ? dname : "", + file_count); file_count++; g_free(dirname); } diff --git a/epan/wslua/wslua.h b/epan/wslua/wslua.h index 268d498dd2..b19725d258 100644 --- a/epan/wslua/wslua.h +++ b/epan/wslua/wslua.h @@ -730,6 +730,9 @@ extern int wslua_bin2hex(lua_State* L, const guint8* data, const guint len, cons extern int wslua_hex2bin(lua_State* L, const char* data, const guint len, const gchar* sep); extern int luaopen_rex_glib(lua_State *L); +extern const gchar* get_current_plugin_version(void); +extern void clear_current_plugin_version(void); + #endif /* diff --git a/epan/wslua/wslua_util.c b/epan/wslua/wslua_util.c index f26adefced..4175c0f170 100644 --- a/epan/wslua/wslua_util.c +++ b/epan/wslua/wslua_util.c @@ -37,6 +37,70 @@ WSLUA_FUNCTION wslua_get_version(lua_State* L) { /* Gets a string of the Wiresha WSLUA_RETURN(1); /* version string */ } + +static gchar* current_plugin_version = NULL; + +const gchar* get_current_plugin_version(void) { + return current_plugin_version ? current_plugin_version : ""; +} + +void clear_current_plugin_version(void) { + if (current_plugin_version != NULL) { + g_free(current_plugin_version); + current_plugin_version = NULL; + } +} + +WSLUA_FUNCTION wslua_set_plugin_info(lua_State* L) { + /* Set a Lua table with meta-data about the plugin, such as version. + + The passed-in Lua table entries need to be keyed/indexed by the following: + * "version" with a string value identifying the plugin version (required) + * "description" with a string value describing the plugin (optional) + * "author" with a string value of the author's name(s) (optional) + * "repository" with a string value of a URL to a repository (optional) + + Not all of the above key entries need to be in the table. The 'version' + entry is required, however. The others are not currently used for anything, but + might be in the future and thus using them might be useful. Table entries keyed + by other strings are ignored, and do not cause an error. + + Example: + @code + local my_info = { + version = "1.0.1", + author = "Jane Doe", + repository = "https://github.com/octocat/Spoon-Knife" + } + + set_plugin_info(my_info) + @endcode + + @since 1.99.9 + */ +#define WSLUA_ARG_set_plugin_info_TABLE 1 /* The Lua table of information. */ + + if ( lua_istable(L,WSLUA_ARG_set_plugin_info_TABLE) ) { + int top; + lua_getfield(L, WSLUA_ARG_set_plugin_info_TABLE, "version"); + top = lua_gettop(L); + if (lua_isstring(L, top)) { + clear_current_plugin_version(); + current_plugin_version = g_strdup( luaL_checkstring(L, top) ); + /* pop the string */ + lua_pop(L, 1); + } + else { + return luaL_error(L,"the Lua table must have a 'version' key entry with a string value"); + } + } else { + return luaL_error(L,"a Lua table with at least a 'version' string entry"); + } + + return 0; +} + + WSLUA_FUNCTION wslua_format_date(lua_State* LS) { /* Formats an absolute timestamp into a human readable date. */ #define WSLUA_ARG_format_date_TIMESTAMP 1 /* A timestamp value to convert. */ lua_Number timestamp = luaL_checknumber(LS,WSLUA_ARG_format_date_TIMESTAMP); |