Skip to content

Commit

Permalink
Lua: add plugin version info
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
hadrielk authored and AndersBroman committed Jul 23, 2015
1 parent 2c534a9 commit 2316352
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
40 changes: 34 additions & 6 deletions epan/wslua/init_wslua.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions epan/wslua/wslua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/*
Expand Down
64 changes: 64 additions & 0 deletions epan/wslua/wslua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2316352

Please sign in to comment.