removed dummy structure definitions for struct _GCache, _GTree, _GTimer,
[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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #undef  G_LOG_DOMAIN
20 #include        <gmodule.h>
21
22
23 G_MODULE_EXPORT void
24 g_clash_func (void)
25 {
26   g_print ("GModule: Hello global clash\n");
27 }
28
29 typedef void (*SimpleFunc) (void);
30 typedef void (*GModuleFunc) (GModule *);
31
32 SimpleFunc gplugin_clash_func;
33
34 int
35 main (int   arg,
36       char *argv[])
37 {
38   GModule *module_self, *module_a, *module_b;
39   gchar *string;
40   gchar *plugin_a, *plugin_b;
41   SimpleFunc f_a, f_b, f_self;
42   GModuleFunc gmod_f;
43
44   string = g_get_current_dir ();
45   g_print ("testgmodule (%s):\n", string);
46
47 #ifdef NATIVE_WIN32
48   plugin_a = g_strconcat (string, "\\libgplugin_a.dll", NULL);
49   plugin_b = g_strconcat (string, "\\libgplugin_b.dll", NULL);
50 #else /* !NATIVE_WIN32 */
51   plugin_a = g_strconcat (string, "/.libs/", "libgplugin_a.so", NULL);
52   plugin_b = g_strconcat (string, "/.libs/", "libgplugin_b.so", NULL);
53 #endif /* NATIVE_WIN32 */
54   g_free (string);
55
56   /* module handles
57    */
58   g_print ("get main module handle\n");
59   module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
60   if (!module_self)
61     {
62       g_print ("error: %s\n", g_module_error ());
63       return 1;
64     }
65   g_print ("load plugin from \"%s\"\n", plugin_a);
66   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
67   if (!module_a)
68     {
69       g_print ("error: %s\n", g_module_error ());
70       return 1;
71     }
72   g_print ("load plugin from \"%s\"\n", plugin_b);
73   module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
74   if (!module_b)
75     {
76       g_print ("error: %s\n", g_module_error ());
77       return 1;
78     }
79
80   /* get plugin specific symbols and call them
81    */
82   string = "gplugin_a_func";
83   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
84   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
85     {
86       g_print ("error: %s\n", g_module_error ());
87       return 1;
88     }
89   string = "gplugin_b_func";
90   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
91   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
92     {
93       g_print ("error: %s\n", g_module_error ());
94       return 1;
95     }
96   g_print ("call plugin function(%p) A: ", f_a);
97   f_a ();
98   g_print ("call plugin function(%p) B: ", f_b);
99   f_b ();
100
101   /* get and call globally clashing functions
102    */
103   string = "g_clash_func";
104   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_self)));
105   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
106     {
107       g_print ("error: %s\n", g_module_error ());
108       return 1;
109     }
110   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
111   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
112     {
113       g_print ("error: %s\n", g_module_error ());
114       return 1;
115     }
116   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
117   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
118     {
119       g_print ("error: %s\n", g_module_error ());
120       return 1;
121     }
122   g_print ("call plugin function(%p) self: ", f_self);
123   f_self ();
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 clashing plugin functions
130    */
131   string = "gplugin_clash_func";
132   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
133   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
134     {
135       g_print ("error: %s\n", g_module_error ());
136       return 1;
137     }
138   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
139   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
140     {
141       g_print ("error: %s\n", g_module_error ());
142       return 1;
143     }
144   g_print ("call plugin function(%p) A: ", f_a);
145   gplugin_clash_func = f_a;
146   gplugin_clash_func ();
147   g_print ("call plugin function(%p) B: ", f_b);
148   gplugin_clash_func = f_b;
149   gplugin_clash_func ();
150
151   /* call gmodule function form A
152    */
153   string = "gplugin_a_module_func";
154   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
155   if (!g_module_symbol (module_a, string, (gpointer) &gmod_f))
156     {
157       g_print ("error: %s\n", g_module_error ());
158       return 1;
159     }
160   g_print ("call plugin A's module function(%p):\n{\n", gmod_f);
161   gmod_f (module_b);
162   g_print ("}\n");
163
164   
165   /* unload plugins
166    */
167   g_print ("unload plugin A:\n");
168   if (!g_module_close (module_a))
169     g_print ("error: %s\n", g_module_error ());
170   g_print ("unload plugin B:\n");
171   if (!g_module_close (module_b))
172     g_print ("error: %s\n", g_module_error ());
173
174 #if 0
175   g_log_set_fatal_mask ("GModule", G_LOG_FATAL_MASK|G_LOG_LEVEL_WARNING);
176   g_module_symbol (0, 0, 0);
177   g_warning("jahooo");
178   g_on_error_query (".libs/testgmodule");
179 #endif
180   
181   return 0;
182 }