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