diff options
author | stig <stig@f5534014-38df-0310-8fa8-9805f1628bb7> | 2009-10-23 17:52:18 +0000 |
---|---|---|
committer | stig <stig@f5534014-38df-0310-8fa8-9805f1628bb7> | 2009-10-23 17:52:18 +0000 |
commit | b1b32cf54f17300e932344b584c117f2be5ec3ab (patch) | |
tree | d3488b143471421bc4d9f3a1e6b05ea19e9a435c /epan/wslua | |
parent | 410b33766cfe1206866b340e37f36085cce823e8 (diff) |
Load lua scripts in the plugins directory, both global and personal.
List loaded lua scripts in Help->About->Plugins.
git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@30675 f5534014-38df-0310-8fa8-9805f1628bb7
Diffstat (limited to 'epan/wslua')
-rw-r--r-- | epan/wslua/init_wslua.c | 93 | ||||
-rw-r--r-- | epan/wslua/init_wslua.h | 38 |
2 files changed, 123 insertions, 8 deletions
diff --git a/epan/wslua/init_wslua.c b/epan/wslua/init_wslua.c index 59f1d65f9b..1f7d2c5204 100644 --- a/epan/wslua/init_wslua.c +++ b/epan/wslua/init_wslua.c @@ -34,6 +34,7 @@ #include <epan/ex-opt.h> #include <wsutil/privileges.h> #include <wsutil/file_util.h> +#include "init_wslua.h" static lua_State* L = NULL; @@ -42,6 +43,7 @@ struct _wslua_treeitem* lua_tree; tvbuff_t* lua_tvb; int lua_malformed; int lua_dissectors_table_ref; +wslua_plugin *wslua_plugin_list = NULL; dissector_handle_t lua_data_handle; @@ -197,34 +199,60 @@ static int lua_main_error_handler(lua_State* LS) { return 0; } -static void lua_load_script(const gchar* filename) { +static void wslua_add_plugin(gchar *name, gchar *version) +{ + wslua_plugin *new_plug, *lua_plug; + + lua_plug = wslua_plugin_list; + new_plug = (wslua_plugin *)g_malloc(sizeof(wslua_plugin)); + + if (!lua_plug) { /* the list is empty */ + wslua_plugin_list = new_plug; + } else { + while (lua_plug->next != NULL) { + lua_plug = lua_plug->next; + } + lua_plug->next = new_plug; + } + + new_plug->name = name; + new_plug->version = version; + new_plug->next = NULL; +} + +static gboolean lua_load_script(const gchar* filename) { FILE* file; + int error; if (! ( file = ws_fopen(filename,"r")) ) { report_open_failure(filename,errno,FALSE); - return; + return FALSE; } lua_settop(L,0); lua_pushcfunction(L,lua_main_error_handler); - switch (lua_load(L,getF,file,filename)) { + error = lua_load(L,getF,file,filename); + switch (error) { case 0: lua_pcall(L,0,0,1); fclose(file); - return; + return TRUE; case LUA_ERRSYNTAX: { report_failure("Lua: syntax error during precompilation of `%s':\n%s",filename,lua_tostring(L,-1)); fclose(file); - return; + return FALSE; } case LUA_ERRMEM: report_failure("Lua: memory allocation error during execution of %s",filename); fclose(file); - return; + return FALSE; } + report_failure("Lua: unknown error during execution of %s: %d",filename,error); + fclose(file); + return FALSE; } static void basic_logger(const gchar *log_domain _U_, @@ -239,6 +267,47 @@ static int wslua_panic(lua_State* LS) { return 0; } +static void lua_load_plugins (gboolean global) +{ + WS_DIR *dir; /* scanned directory */ + WS_DIRENT *file; /* current file */ + gchar *persdir, *filename, *dot; + const gchar *dirname, *name; + + if (global) { + persdir = NULL; + dirname = get_plugin_dir(); + } else { + persdir = get_plugins_pers_dir(); + dirname = persdir; + } + + if ((dir = ws_dir_open(dirname, 0, NULL)) != NULL) { + while ((file = ws_dir_read_name(dir)) != NULL) { + name = ws_dir_get_name(file); + + if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) + continue; /* skip "." and ".." */ + + /* skip anything but files with .lua suffix */ + dot = strrchr(name, '.'); + if (dot == NULL || strcmp(dot+1, "lua") != 0) + continue; + + filename = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dirname, name); + if (file_exists(filename)) { + if (lua_load_script(filename)) { + wslua_add_plugin(g_strdup(name), g_strdup("")); + } + } + g_free (filename); + } + } + + if (persdir) + g_free(persdir); +} + int wslua_init(lua_State* LS) { gchar* filename; const funnel_ops_t* ops = funnel_get_funnel_ops(); @@ -300,6 +369,9 @@ int wslua_init(lua_State* LS) { return 0; } + /* load global scripts */ + lua_load_plugins(TRUE); + /* check whether we should run other scripts even if running superuser */ lua_pushstring(L,"run_user_scripts_when_superuser"); lua_gettable(L, LUA_GLOBALSINDEX); @@ -315,10 +387,15 @@ int wslua_init(lua_State* LS) { if (( file_exists(filename))) { lua_load_script(filename); - g_free(filename); - filename = NULL; } + g_free(filename); + filename = NULL; + + /* load user scripts */ + lua_load_plugins(FALSE); + + /* load scripts from command line */ while((filename = (gchar *)ex_opt_get_next("lua_script"))) { lua_load_script(filename); } diff --git a/epan/wslua/init_wslua.h b/epan/wslua/init_wslua.h new file mode 100644 index 0000000000..d77becfd78 --- /dev/null +++ b/epan/wslua/init_wslua.h @@ -0,0 +1,38 @@ +/* init_wslua.h + * definitions for wslua plugins structures + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __INIT_WSLUA_H__ +#define __INIT_WSLUA_H__ + +#include <glib.h> + +typedef struct _wslua_plugin { + gchar *name; /* plugin name */ + gchar *version; /* plugin version */ + struct _wslua_plugin *next; +} wslua_plugin; + +WS_VAR_IMPORT wslua_plugin *wslua_plugin_list; + +#endif /* __INIT_WSLUA_H__ */ |