Load lua scripts in the plugins directory, both global and personal.

List loaded lua scripts in Help->About->Plugins.

svn path=/trunk/; revision=30675
This commit is contained in:
Stig Bjørlykke 2009-10-23 17:52:18 +00:00
parent c65f2d42f8
commit 3a5c9ec3b2
10 changed files with 179 additions and 35 deletions

View File

@ -26,6 +26,10 @@
Wireshark will try to load a file named <command>init.lua</command> in the user's
directory.
</para>
<para>
Wireshark will also load all files with <command>.lua</command> suffix from both the
global and the personal plugins directory.
</para>
<para>
The command line option <command>-X lua_script:&lt;file.lua&gt;</command> can be used to
load lua scripts as well.

View File

@ -77,6 +77,8 @@
#include <wiretap/wtap.h> /* for WTAP_ERR_SHORT_WRITE */
#define PROFILES_DIR "profiles"
#define PLUGINS_DIR_NAME "plugins"
#define U3_MY_CAPTURES "\\My Captures"
char *persconffile_dir = NULL;
@ -765,7 +767,7 @@ get_wspython_dir(void)
}
#ifdef HAVE_PLUGINS
#if defined(HAVE_PLUGINS) || defined(HAVE_LUA_5_1)
/*
* Find the directory where the plugins are stored.
*
@ -844,7 +846,7 @@ init_plugin_dir(void)
}
#endif
}
#endif /* HAVE_PLUGINS */
#endif /* HAVE_PLUGINS || HAVE_LUA_5_1 */
/*
* Get the directory in which the plugins are stored.
@ -852,7 +854,7 @@ init_plugin_dir(void)
const char *
get_plugin_dir(void)
{
#ifdef HAVE_PLUGINS
#if defined(HAVE_PLUGINS) || defined(HAVE_LUA_5_1)
if (!plugin_dir) init_plugin_dir();
return plugin_dir;
#else
@ -1491,6 +1493,14 @@ get_datafile_path(const char *filename)
filename);
}
/* Get the personal plugin dir */
/* Return value is malloced so the caller should g_free() it. */
char *
get_plugins_pers_dir(void)
{
return get_persconffile_path(PLUGINS_DIR_NAME, FALSE, FALSE);
}
/* Delete a file */
gboolean
deletefile(const char *path)

View File

@ -78,6 +78,11 @@ extern const char *get_datafile_dir(void);
*/
extern char *get_datafile_path(const char *filename);
/*
* Get the personal plugin dir.
*/
extern char *get_plugins_pers_dir(void);
/*
* Get the directory in which files that, at least on UNIX, are
* system files (such as "/etc/ethers") are stored; on Windows,

View File

@ -1068,6 +1068,7 @@ vals_pdu_type DATA
vals_status DATA
value_is_in_range
write_prefs
wslua_plugin_list DATA
wtap_nstime_to_sec
xml_escape
xml_get_attrib

View File

@ -5,7 +5,7 @@
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1999 Gerald Combs
* 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
@ -55,9 +55,7 @@
#include "report_err.h"
/* linked list of all plugins */
plugin *plugin_list;
#define PLUGINS_DIR_NAME "plugins"
plugin *plugin_list = NULL;
/*
* add a new plugin to the list
@ -333,12 +331,6 @@ plugins_scan_dir(const char *dirname)
}
}
/* get the personal plugin dir */
/* Return value is malloced so the caller should g_free() it. */
char *get_plugins_pers_dir(void)
{
return get_persconffile_path(PLUGINS_DIR_NAME, FALSE, FALSE);
}
/*
* init plugins

View File

@ -5,7 +5,7 @@
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1999 Gerald Combs
* 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
@ -51,8 +51,4 @@ extern void register_all_plugin_tap_listeners(void);
extern void register_all_wiretap_modules(void);
extern void register_all_codecs(void);
/* get the personal plugin dir */
/* Return value is g_malloced so the caller should g_free() it. */
extern char *get_plugins_pers_dir(void);
#endif /* __PLUGINS_H__ */

View File

@ -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);
}

38
epan/wslua/init_wslua.h Normal file
View File

@ -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__ */

View File

@ -6,7 +6,7 @@
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 2000 Gerald Combs
* 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
@ -420,7 +420,7 @@ about_folders_page_new(void)
about_folders_row(table, "Program", constpath,
"program files");
#ifdef HAVE_PLUGINS
#if defined(HAVE_PLUGINS) || defined(HAVE_LUA_5_1)
/* pers plugins */
path = get_plugins_pers_dir();
about_folders_row(table, "Personal Plugins", path,
@ -487,7 +487,7 @@ about_wireshark_cb( GtkWidget *w _U_, gpointer data _U_ )
GtkWidget *main_vb, *main_nb, *bbox, *ok_btn;
GtkWidget *page_lb, *about_page, *folders_page;
#ifdef HAVE_PLUGINS
#if defined(HAVE_PLUGINS) || defined(HAVE_LUA_5_1)
GtkWidget *plugins_page;
#endif
@ -531,7 +531,7 @@ about_wireshark_cb( GtkWidget *w _U_, gpointer data _U_ )
page_lb = gtk_label_new("Folders");
gtk_notebook_append_page(GTK_NOTEBOOK(main_nb), folders_page, page_lb);
#ifdef HAVE_PLUGINS
#if defined(HAVE_PLUGINS) || defined(HAVE_LUA_5_1)
plugins_page = about_plugins_page_new();
page_lb = gtk_label_new("Plugins");
gtk_notebook_append_page(GTK_NOTEBOOK(main_nb), plugins_page, page_lb);

View File

@ -5,7 +5,7 @@
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1999 Gerald Combs
* 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
@ -28,7 +28,10 @@
#include <gtk/gtk.h>
#include <epan/plugins.h>
#include "epan/plugins.h"
#ifdef HAVE_LUA_5_1
#include "epan/wslua/init_wslua.h"
#endif
#include "../globals.h"
@ -37,7 +40,7 @@
#include "gtk/plugins_dlg.h"
#ifdef HAVE_PLUGINS
#if defined(HAVE_PLUGINS) || defined(HAVE_LUA_5_1)
/*
* Fill the list widget with a list of the plugin modules.
@ -45,10 +48,16 @@
static void
plugins_scan(GtkWidget *list)
{
#ifdef HAVE_PLUGINS
plugin *pt_plug;
const char *sep;
#endif
#ifdef HAVE_LUA_5_1
wslua_plugin *lua_plug;
#endif
GString *type;
const char *sep;
#ifdef HAVE_PLUGINS
for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
{
type = g_string_new("");
@ -79,6 +88,18 @@ plugins_scan(GtkWidget *list)
2, type->str, -1);
g_string_free(type, TRUE);
}
#endif
#ifdef HAVE_LUA_5_1
for (lua_plug = wslua_plugin_list; lua_plug != NULL; lua_plug = lua_plug->next)
{
type = g_string_new("");
type = g_string_append(type, "lua script");
simple_list_append(list, 0, lua_plug->name, 1, lua_plug->version, 2, type->str, -1);
g_string_free(type, TRUE);
}
#endif
}
@ -102,4 +123,4 @@ about_plugins_page_new(void)
return scrolledwindow;
}
#endif /* HAVE_PLUGINS */
#endif /* HAVE_PLUGINS || HAVE_LUA_5_1 */