s/retrive/retrieve/
[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   plugin_a = g_strconcat (string, G_DIR_SEPARATOR_S "libgplugin_a", NULL);
58   plugin_b = g_strconcat (string, G_DIR_SEPARATOR_S "libgplugin_b", NULL);
59
60   g_free (string);
61
62   /* module handles
63    */
64   g_print ("get main module handle\n");
65   module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
66   if (!module_self)
67     {
68       g_print ("error: %s\n", g_module_error ());
69       return 1;
70     }
71   g_print ("check that not yet bound symbols in shared libraries of main module are retrievable:\n");
72   string = "g_module_close";
73   basename = g_path_get_basename (g_module_name (module_self));
74   g_print ("retrieve symbol `%s' from \"%s\":\n", string, basename);
75   g_free (basename);
76   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
77     {
78       g_print ("error: %s\n", g_module_error ());
79       return 1;
80     }
81   g_print ("retrieved symbol `%s' as %p\n", string, f_self);
82   g_print ("load plugin from \"%s\"\n", plugin_a);
83   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
84   if (!module_a)
85     {
86       g_print ("error: %s\n", g_module_error ());
87       return 1;
88     }
89   g_print ("load plugin from \"%s\"\n", plugin_b);
90   module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
91   if (!module_b)
92     {
93       g_print ("error: %s\n", g_module_error ());
94       return 1;
95     }
96
97   /* get plugin specific symbols and call them
98    */
99   string = "gplugin_a_func";
100   basename = g_path_get_basename (g_module_name (module_a));
101   g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
102   g_free (basename);
103   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
104     {
105       g_print ("error: %s\n", g_module_error ());
106       return 1;
107     }
108   string = "gplugin_b_func";
109   basename = g_path_get_basename (g_module_name (module_b));
110   g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
111   g_free (basename);
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   basename = g_path_get_basename (g_module_name (module_self));
126   g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
127   g_free (basename);
128   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
129     {
130       g_print ("error: %s\n", g_module_error ());
131       return 1;
132     }
133   basename = g_path_get_basename (g_module_name (module_a));
134   g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
135   g_free (basename);
136   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
137     {
138       g_print ("error: %s\n", g_module_error ());
139       return 1;
140     }
141   basename = g_path_get_basename (g_module_name (module_b));
142   g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
143   g_free (basename);
144   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
145     {
146       g_print ("error: %s\n", g_module_error ());
147       return 1;
148     }
149   g_print ("call plugin function(%p) self: ", f_self);
150   f_self ();
151   g_print ("call plugin function(%p) A: ", f_a);
152   f_a ();
153   g_print ("call plugin function(%p) B: ", f_b);
154   f_b ();
155
156   /* get and call clashing plugin functions
157    */
158   string = "gplugin_clash_func";
159   basename = g_path_get_basename (g_module_name (module_self));
160   g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
161   g_free (basename);
162   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
163     f_self = NULL;
164   g_print ("retrieved function `%s' from self: %p\n", string, f_self);
165   basename = g_path_get_basename (g_module_name (module_a));
166   g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
167   g_free (basename);
168   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
169     {
170       g_print ("error: %s\n", g_module_error ());
171       return 1;
172     }
173   basename = g_path_get_basename (g_module_name (module_b));
174   g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
175   g_free (basename);
176   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
177     {
178       g_print ("error: %s\n", g_module_error ());
179       return 1;
180     }
181   g_print ("call plugin function(%p) A: ", f_a);
182   plugin_clash_func = f_a;
183   plugin_clash_func ();
184   g_print ("call plugin function(%p) B: ", f_b);
185   plugin_clash_func = f_b;
186   plugin_clash_func ();
187
188   /* call gmodule function form A
189    */
190   string = "gplugin_a_module_func";
191   basename = g_path_get_basename (g_module_name (module_a));
192   g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
193   g_free (basename);
194   if (!g_module_symbol (module_a, string, (gpointer) &gmod_f))
195     {
196       g_print ("error: %s\n", g_module_error ());
197       return 1;
198     }
199   g_print ("call plugin A's module function(%p):\n{\n", gmod_f);
200   gmod_f (module_b);
201   g_print ("}\n");
202
203   
204   /* unload plugins
205    */
206   g_print ("unload plugin A:\n");
207   if (!g_module_close (module_a))
208     g_print ("error: %s\n", g_module_error ());
209   g_print ("unload plugin B:\n");
210   if (!g_module_close (module_b))
211     g_print ("error: %s\n", g_module_error ());
212
213 #if 0
214   g_log_set_fatal_mask ("GModule", G_LOG_FATAL_MASK|G_LOG_LEVEL_WARNING);
215   g_module_symbol (0, 0, 0);
216   g_warning("jahooo");
217   g_on_error_query (".libs/testgmodule");
218 #endif
219   
220   return 0;
221 }