Move privileges.c and unicode-utils.c from epan to wsutil (so things like
[metze/wireshark/wip.git] / epan / plugins.c
index 4841751311990fe6b45532af44974374269bdc6b..ed8aaa3f8c2c199fb18a6e90304b6ab8e3175786 100644 (file)
@@ -49,8 +49,8 @@
 #endif
 
 #include "filesystem.h"
-#include "privileges.h"
-#include <wiretap/file_util.h>
+#include <wsutil/privileges.h>
+#include <wsutil/file_util.h>
 #include "report_err.h"
 
 /* linked list of all plugins */
@@ -69,7 +69,8 @@ static int
 add_plugin(void *handle, gchar *name, gchar *version,
           void (*register_protoinfo)(void), void (*reg_handoff)(void),
           void (*register_tap_listener)(void),
-          void (*register_wtap_module)(void))
+          void (*register_wtap_module)(void),
+          void (*register_codec_module)(void))
 {
     plugin *new_plug, *pt_plug;
 
@@ -108,6 +109,7 @@ add_plugin(void *handle, gchar *name, gchar *version,
     new_plug->reg_handoff = reg_handoff;
     new_plug->register_tap_listener = register_tap_listener;
     new_plug->register_wtap_module = register_wtap_module;
+    new_plug->register_codec_module = register_codec_module;
     new_plug->next = NULL;
 
     return 0;
@@ -133,10 +135,6 @@ plugins_scan_dir(const char *dirname)
     ETH_DIR       *dir;             /* scanned directory */
     ETH_DIRENT    *file;            /* current file */
     const char    *name;
-#if GLIB_MAJOR_VERSION < 2
-    gchar         *hack_path;       /* pathname used to construct lt_lib_ext */
-    gchar         *lt_lib_ext;      /* extension for loadable modules */
-#endif
     gchar          filename[FILENAME_LEN];   /* current file name */
     GModule       *handle;          /* handle returned by dlopen */
     gchar         *version;
@@ -145,50 +143,18 @@ plugins_scan_dir(const char *dirname)
     void         (*reg_handoff)(void);
     void         (*register_tap_listener)(void);
     void (*register_wtap_module)(void);
+    void (*register_codec_module)(void);
 
     gchar         *dot;
     int            cr;
 
-#if GLIB_MAJOR_VERSION < 2
-    /*
-     * We find the extension used on this platform for loadable modules
-     * by the sneaky hack of calling "g_module_build_path" to build
-     * the pathname for a module with an empty directory name and
-     * empty module name, and then search for the last "." and use
-     * everything from the last "." on.
-     */
-    hack_path = g_module_build_path("", "");
-    lt_lib_ext = strrchr(hack_path, '.');
-    if (lt_lib_ext == NULL)
+    if ((dir = ws_dir_open(dirname, 0, NULL)) != NULL)
     {
-       /*
-        * Does this mean there *is* no extension?  Assume so.
-        *
-        * XXX - the code below assumes that all loadable modules have
-        * an extension....
-        */
-       lt_lib_ext = "";
-    }
-#endif
 
-    if ((dir = eth_dir_open(dirname, 0, NULL)) != NULL)
-    {
-    while ((file = eth_dir_read_name(dir)) != NULL)
+    while ((file = ws_dir_read_name(dir)) != NULL)
        {
-           name = eth_dir_get_name(file);
+           name = ws_dir_get_name(file);
 
-#if GLIB_MAJOR_VERSION < 2
-           /* don't try to open "." and ".." */
-           if (!(strcmp(name, "..") &&
-                 strcmp(name, ".")))
-               continue;
-
-            /* skip anything but files with lt_lib_ext */
-            dot = strrchr(name, '.');
-            if (dot == NULL || strcmp(dot, lt_lib_ext) != 0)
-               continue;
-
-#else /* GLIB 2 */
            /*
             * GLib 2.x defines G_MODULE_SUFFIX as the extension used on
             * this platform for loadable modules.
@@ -198,7 +164,6 @@ plugins_scan_dir(const char *dirname)
             if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0)
                continue;
 
-#endif
            g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
                dirname, name);
            if ((handle = g_module_open(filename, 0)) == NULL)
@@ -215,7 +180,7 @@ plugins_scan_dir(const char *dirname)
                continue;
            }
            version = gp;
-           
+
            /*
             * Do we have a register routine?
             */
@@ -298,7 +263,7 @@ plugins_scan_dir(const char *dirname)
                        g_module_close(handle);
                        continue;
            }
-           
+
            /*
                 * Do we have a register_wtap_module routine?
                 */
@@ -308,19 +273,30 @@ plugins_scan_dir(const char *dirname)
            } else {
                register_wtap_module = NULL;
            }
-           
+
+           /*
+                * Do we have a register_codec_module routine?
+                */
+           if (g_module_symbol(handle, "register_codec_module", &gp))
+           {
+               register_codec_module = gp;
+           } else {
+               register_codec_module = NULL;
+           }
+
           /*
                * Does this dissector do anything useful?
                */
            if (register_protoinfo == NULL &&
-                   register_tap_listener == NULL && 
-                   register_wtap_module == NULL )
+                   register_tap_listener == NULL &&
+                   register_wtap_module == NULL &&
+                   register_codec_module == NULL )
            {
                /*
                 * No.
                 */
                report_failure("The plugin '%s' has neither a register routine, "
-                                          "a register_tap_listener or a register_wtap_module routine",
+                                          "a register_tap_listener or a register_wtap_module or a register_codec_module routine",
                    name);
                g_module_close(handle);
                continue;
@@ -331,7 +307,7 @@ plugins_scan_dir(const char *dirname)
             */
            if ((cr = add_plugin(handle, g_strdup(name), version,
                                 register_protoinfo, reg_handoff,
-                                register_tap_listener,register_wtap_module)))
+                                register_tap_listener,register_wtap_module,register_codec_module)))
            {
                if (cr == EEXIST)
                    fprintf(stderr, "The plugin %s, version %s\n"
@@ -343,28 +319,17 @@ plugins_scan_dir(const char *dirname)
                g_module_close(handle);
                continue;
            }
-               
-           /*
-            * Call its register routine if it has one.
-            * XXX - just save this and call it with the built-in
-            * dissector register routines?
-            */
-           if (register_protoinfo != NULL)
-                   register_protoinfo();
 
        }
-       eth_dir_close(dir);
+       ws_dir_close(dir);
        }
-#if GLIB_MAJOR_VERSION < 2
-    g_free(hack_path);
-#endif
 }
 
 /* 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);
+    return get_persconffile_path(PLUGINS_DIR_NAME, FALSE, FALSE);
 }
 
 /*
@@ -392,9 +357,9 @@ init_plugins(void)
         */
        plugin_dir = get_plugin_dir();
        if (running_in_build_directory()) {
-           if ((dir = eth_dir_open(plugin_dir, 0, NULL)) != NULL) {
-               while ((file = eth_dir_read_name(dir)) != NULL) {
-                   name = eth_dir_get_name(file);
+           if ((dir = ws_dir_open(plugin_dir, 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 ".." */
                    /*
@@ -438,6 +403,29 @@ init_plugins(void)
            g_free(plugins_pers_dir);
        }
     }
+       register_all_wiretap_modules();
+       register_all_codecs();
+}
+
+void
+register_all_plugin_registrations(void)
+{
+    plugin *pt_plug;
+
+    /*
+     * For all plugins with register-handoff routines, call the routines.
+     * This is called from "proto_init()"; it must be called after
+     * "register_all_protocols()" and "init_plugins()" are called,
+     * in case one plugin registers itself either with a built-in
+     * dissector or with another plugin; we must first register all
+     * dissectors, whether built-in or plugin, so their dissector tables
+     * are initialized, and only then register all handoffs.
+     */
+    for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
+    {
+               if (pt_plug->register_protoinfo)
+                       (pt_plug->register_protoinfo)();
+    }
 }
 
 void
@@ -492,4 +480,20 @@ register_all_wiretap_modules(void)
                        (pt_plug->register_wtap_module)();
     }
 }
+
+void
+register_all_codecs(void)
+{
+    plugin *pt_plug;
+
+    /*
+     * For all plugins with register_wtap_module routines, call the
+     * routines.
+     */
+    for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
+    {
+               if (pt_plug->register_codec_module)
+                       (pt_plug->register_codec_module)();
+    }
+}
 #endif