minor optimization.
[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
20 /*
21  * Modified by the GLib Team and others 1997-1999.  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 *plugin_a, *plugin_b;
50   SimpleFunc f_a, f_b, f_self;
51   GModuleFunc gmod_f;
52
53   string = g_get_current_dir ();
54   g_print ("testgmodule (%s):\n", string);
55
56 #ifdef G_OS_WIN32
57   plugin_a = g_strconcat (string, "\\libgplugin_a.dll", NULL);
58   plugin_b = g_strconcat (string, "\\libgplugin_b.dll", NULL);
59 #elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
60   plugin_a = g_strconcat (string, "/.libs/", "libgplugin_a.sl", NULL);
61   plugin_b = g_strconcat (string, "/.libs/", "libgplugin_b.sl", NULL);
62 #else /* G_MODULE_IMPL != G_MODULE_IMPL_DLD && !G_OS_WIN32 */
63   plugin_a = g_strconcat (string, "/.libs/", "libgplugin_a.so", NULL);
64   plugin_b = g_strconcat (string, "/.libs/", "libgplugin_b.so", NULL);
65 #endif /* G_OS_WIN32 */
66   g_free (string);
67
68   /* module handles
69    */
70   g_print ("get main module handle\n");
71   module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
72   if (!module_self)
73     {
74       g_print ("error: %s\n", g_module_error ());
75       return 1;
76     }
77   g_print ("check that not yet bound symbols in shared libraries of main module are retrievable:\n");
78   string = "g_module_close";
79   g_print ("retrive symbol `%s' from \"%s\":\n", string, g_basename (g_module_name (module_self)));
80   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
81     {
82       g_print ("error: %s\n", g_module_error ());
83       return 1;
84     }
85   g_print ("retrived symbol `%s' as %p\n", string, f_self);
86   g_print ("load plugin from \"%s\"\n", plugin_a);
87   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
88   if (!module_a)
89     {
90       g_print ("error: %s\n", g_module_error ());
91       return 1;
92     }
93   g_print ("load plugin from \"%s\"\n", plugin_b);
94   module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
95   if (!module_b)
96     {
97       g_print ("error: %s\n", g_module_error ());
98       return 1;
99     }
100
101   /* get plugin specific symbols and call them
102    */
103   string = "gplugin_a_func";
104   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
105   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
106     {
107       g_print ("error: %s\n", g_module_error ());
108       return 1;
109     }
110   string = "gplugin_b_func";
111   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
112   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
113     {
114       g_print ("error: %s\n", g_module_error ());
115       return 1;
116     }
117   g_print ("call plugin function(%p) A: ", f_a);
118   f_a ();
119   g_print ("call plugin function(%p) B: ", f_b);
120   f_b ();
121
122   /* get and call globally clashing functions
123    */
124   string = "g_clash_func";
125   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_self)));
126   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
127     {
128       g_print ("error: %s\n", g_module_error ());
129       return 1;
130     }
131   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
132   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
133     {
134       g_print ("error: %s\n", g_module_error ());
135       return 1;
136     }
137   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
138   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
139     {
140       g_print ("error: %s\n", g_module_error ());
141       return 1;
142     }
143   g_print ("call plugin function(%p) self: ", f_self);
144   f_self ();
145   g_print ("call plugin function(%p) A: ", f_a);
146   f_a ();
147   g_print ("call plugin function(%p) B: ", f_b);
148   f_b ();
149
150   /* get and call clashing plugin functions
151    */
152   string = "gplugin_clash_func";
153   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_self)));
154   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
155     f_self = NULL;
156   g_print ("retrived function `%s' from self: %p\n", string, f_self);
157   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
158   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
159     {
160       g_print ("error: %s\n", g_module_error ());
161       return 1;
162     }
163   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
164   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
165     {
166       g_print ("error: %s\n", g_module_error ());
167       return 1;
168     }
169   g_print ("call plugin function(%p) A: ", f_a);
170   plugin_clash_func = f_a;
171   plugin_clash_func ();
172   g_print ("call plugin function(%p) B: ", f_b);
173   plugin_clash_func = f_b;
174   plugin_clash_func ();
175
176   /* call gmodule function form A
177    */
178   string = "gplugin_a_module_func";
179   g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
180   if (!g_module_symbol (module_a, string, (gpointer) &gmod_f))
181     {
182       g_print ("error: %s\n", g_module_error ());
183       return 1;
184     }
185   g_print ("call plugin A's module function(%p):\n{\n", gmod_f);
186   gmod_f (module_b);
187   g_print ("}\n");
188
189   
190   /* unload plugins
191    */
192   g_print ("unload plugin A:\n");
193   if (!g_module_close (module_a))
194     g_print ("error: %s\n", g_module_error ());
195   g_print ("unload plugin B:\n");
196   if (!g_module_close (module_b))
197     g_print ("error: %s\n", g_module_error ());
198
199 #if 0
200   g_log_set_fatal_mask ("GModule", G_LOG_FATAL_MASK|G_LOG_LEVEL_WARNING);
201   g_module_symbol (0, 0, 0);
202   g_warning("jahooo");
203   g_on_error_query (".libs/testgmodule");
204 #endif
205   
206   return 0;
207 }