Load lua scripts in the plugins directory, both global and personal.
authorstig <stig@f5534014-38df-0310-8fa8-9805f1628bb7>
Fri, 23 Oct 2009 17:52:18 +0000 (17:52 +0000)
committerstig <stig@f5534014-38df-0310-8fa8-9805f1628bb7>
Fri, 23 Oct 2009 17:52:18 +0000 (17:52 +0000)
List loaded lua scripts in Help->About->Plugins.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@30675 f5534014-38df-0310-8fa8-9805f1628bb7

docbook/wsluarm.xml
epan/filesystem.c
epan/filesystem.h
epan/libwireshark.def
epan/plugins.c
epan/plugins.h
epan/wslua/init_wslua.c
epan/wslua/init_wslua.h [new file with mode: 0644]
gtk/about_dlg.c
gtk/plugins_dlg.c

index 3db2b9cc9cc8a24b5e685157d4eeeec8ad76e881..3a24e8a18f86f6a57c2ee91d00cd9af3ec6c5e8c 100644 (file)
          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.       
index 596c39962d2dddf29dc89b959732771184d9836e..2154f9a11a1330df0ecfe22588830fa6586d78c0 100644 (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)
index 792209af2e8771f1f6b95ac0708ef466cd9e470a..507271d2d2e780cc3093f1387cd5fcdbf5d8971a 100644 (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,
index 1fe47f79fce028be7164a18dc6bbd10a58e3406f..4db3ef758c6a39edf6dcb6a7b8618bdeaa2f1a20 100644 (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
index ac7f7ece271f20cfec8a95a02c4b572eb37b4867..563baeae6fd2d6c0c2424d1eb504deddd089caae 100644 (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
index 961cb1c02438f02e381f0f1614f8df00bd658555..02fbfa83dbfed73ada6da64c3fbec81dd7545f39 100644 (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__ */
index 59f1d65f9b2b23109cc0f041dfc0c8e3a1c86d46..1f7d2c520419a70d7719c6d2c62da86651d7559c 100644 (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);
         }
diff --git a/epan/wslua/init_wslua.h b/epan/wslua/init_wslua.h
new file mode 100644 (file)
index 0000000..d77becf
--- /dev/null
@@ -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__ */
index c095ce7b8b3964f16da758a8d752e86fbe3c98e9..5510c8d600d184e2695b75ae226905275de56b15 100644 (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);
index 87b8ff1f20c687ed7785ce6b8899d170e59ae86b..0f86604aef70d5442608434c65bf160a787e16e6 100644 (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
 
 #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.
 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 */