applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[platform/upstream/glib.git] / gmodule / testgmodule.c
1 /* testgmodule.c - test program for GMODULE
2  * Copyright (C) 1998 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #undef  G_LOG_DOMAIN
28 #include        <gmodule.h>
29 #include        "gmoduleconf.h"
30
31
32 G_MODULE_EXPORT void
33 g_clash_func (void)
34 {
35   g_print ("GModule: Hello global clash\n");
36 }
37
38 typedef void (*SimpleFunc) (void);
39 typedef void (*GModuleFunc) (GModule *);
40
41 static SimpleFunc plugin_clash_func = NULL;
42
43 int
44 main (int   arg,
45       char *argv[])
46 {
47   GModule *module_self, *module_a, *module_b;
48   gchar *string;
49   gchar *basename;
50   gchar *plugin_a, *plugin_b;
51   SimpleFunc f_a, f_b, f_self;
52   GModuleFunc gmod_f;
53
54   string = g_get_current_dir ();
55   g_print ("testgmodule (%s):\n", string);
56
57 #if (G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
58   plugin_a = g_strconcat (string, "\\libgplugin_a.dll", NULL);
59   plugin_b = g_strconcat (string, "\\libgplugin_b.dll", NULL);
60 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
61   plugin_a = g_strconcat (string, "/.libs/", "libgplugin_a.sl", NULL);
62   plugin_b = g_strconcat (string, "/.libs/", "libgplugin_b.sl", NULL);
63 #else /* neither DLD nor WIN32 */
64   plugin_a = g_strconcat (string, "/.libs/", "libgplugin_a.so", NULL);
65   plugin_b = g_strconcat (string, "/.libs/", "libgplugin_b.so", NULL);
66 #endif
67   g_free (string);
68
69   /* module handles
70    */
71   g_print ("get main module handle\n");
72   module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
73   if (!module_self)
74     {
75       g_print ("error: %s\n", g_module_error ());
76       return 1;
77     }
78   g_print ("check that not yet bound symbols in shared libraries of main module are retrievable:\n");
79   string = "g_module_close";
80   basename = g_path_get_basename (g_module_name (module_self));
81   g_print ("retrive symbol `%s' from \"%s\":\n", string, basename);
82   g_free (basename);
83   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
84     {
85       g_print ("error: %s\n", g_module_error ());
86       return 1;
87     }
88   g_print ("retrived symbol `%s' as %p\n", string, f_self);
89   g_print ("load plugin from \"%s\"\n", plugin_a);
90   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
91   if (!module_a)
92     {
93       g_print ("error: %s\n", g_module_error ());
94       return 1;
95     }
96   g_print ("load plugin from \"%s\"\n", plugin_b);
97   module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
98   if (!module_b)
99     {
100       g_print ("error: %s\n", g_module_error ());
101       return 1;
102     }
103
104   /* get plugin specific symbols and call them
105    */
106   string = "gplugin_a_func";
107   basename = g_path_get_basename (g_module_name (module_a));
108   g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
109   g_free (basename);
110   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
111     {
112       g_print ("error: %s\n", g_module_error ());
113       return 1;
114     }
115   string = "gplugin_b_func";
116   basename = g_path_get_basename (g_module_name (module_b));
117   g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
118   g_free (basename);
119   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
120     {
121       g_print ("error: %s\n", g_module_error ());
122       return 1;
123     }
124   g_print ("call plugin function(%p) A: ", f_a);
125   f_a ();
126   g_print ("call plugin function(%p) B: ", f_b);
127   f_b ();
128
129   /* get and call globally clashing functions
130    */
131   string = "g_clash_func";
132   basename = g_path_get_basename (g_module_name (module_self));
133   g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
134   g_free (basename);
135   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
136     {
137       g_print ("error: %s\n", g_module_error ());
138       return 1;
139     }
140   basename = g_path_get_basename (g_module_name (module_a));
141   g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
142   g_free (basename);
143   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
144     {
145       g_print ("error: %s\n", g_module_error ());
146       return 1;
147     }
148   basename = g_path_get_basename (g_module_name (module_b));
149   g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
150   g_free (basename);
151   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
152     {
153       g_print ("error: %s\n", g_module_error ());
154       return 1;
155     }
156   g_print ("call plugin function(%p) self: ", f_self);
157   f_self ();
158   g_print ("call plugin function(%p) A: ", f_a);
159   f_a ();
160   g_print ("call plugin function(%p) B: ", f_b);
161   f_b ();
162
163   /* get and call clashing plugin functions
164    */
165   string = "gplugin_clash_func";
166   basename = g_path_get_basename (g_module_name (module_self));
167   g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
168   g_free (basename);
169   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
170     f_self = NULL;
171   g_print ("retrived function `%s' from self: %p\n", string, f_self);
172   basename = g_path_get_basename (g_module_name (module_a));
173   g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
174   g_free (basename);
175   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
176     {
177       g_print ("error: %s\n", g_module_error ());
178       return 1;
179     }
180   basename = g_path_get_basename (g_module_name (module_b));
181   g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
182   g_free (basename);
183   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
184     {
185       g_print ("error: %s\n", g_module_error ());
186       return 1;
187     }
188   g_print ("call plugin function(%p) A: ", f_a);
189   plugin_clash_func = f_a;
190   plugin_clash_func ();
191   g_print ("call plugin function(%p) B: ", f_b);
192   plugin_clash_func = f_b;
193   plugin_clash_func ();
194
195   /* call gmodule function form A
196    */
197   string = "gplugin_a_module_func";
198   basename = g_path_get_basename (g_module_name (module_a));
199   g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
200   g_free (basename);
201   if (!g_module_symbol (module_a, string, (gpointer) &gmod_f))
202     {
203       g_print ("error: %s\n", g_module_error ());
204       return 1;
205     }
206   g_print ("call plugin A's module function(%p):\n{\n", gmod_f);
207   gmod_f (module_b);
208   g_print ("}\n");
209
210   
211   /* unload plugins
212    */
213   g_print ("unload plugin A:\n");
214   if (!g_module_close (module_a))
215     g_print ("error: %s\n", g_module_error ());
216   g_print ("unload plugin B:\n");
217   if (!g_module_close (module_b))
218     g_print ("error: %s\n", g_module_error ());
219
220 #if 0
221   g_log_set_fatal_mask ("GModule", G_LOG_FATAL_MASK|G_LOG_LEVEL_WARNING);
222   g_module_symbol (0, 0, 0);
223   g_warning("jahooo");
224   g_on_error_query (".libs/testgmodule");
225 #endif
226   
227   return 0;
228 }