]> git.draconx.ca Git - upkg.git/blob - src/module.c
module: add missing return value to module_exit.
[upkg.git] / src / module.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <glib-object.h>
6 #include <ltdl.h>
7
8 #include "module.h"
9 #include "avl.h"
10
11 static unsigned initialized;
12 static struct avl_table *package_tree;
13
14 static char *str_cpy_lower(char *dst, const char *src)
15 {
16         size_t i;
17
18         for (i = 0; src[i]; i++)
19                 dst[i] = tolower(src[i]);
20         dst[i] = 0;
21
22         return dst;
23 }
24
25 G_DEFINE_TYPE(UPkgModule, upkg_module, G_TYPE_TYPE_MODULE);
26
27 static void dl_print_errors(const char *prefix)
28 {
29         const char *err;
30         while (err = lt_dlerror()) {
31                 if (prefix) fprintf(stderr, "%s: ", prefix);
32                 puts(err);
33         }
34 }
35
36 static gboolean module_load(GTypeModule *m)
37 {
38         UPkgModule *mod = UPKG_MODULE(m);
39         int (*init_func)(GTypeModule *);
40
41         mod->dl = lt_dlopenext(G_TYPE_MODULE(m)->name);
42         if (!mod->dl) {
43                 dl_print_errors(__func__);
44                 return FALSE;
45         }
46
47         init_func = lt_dlsym(mod->dl, "init");
48         if (!init_func || init_func(m) != 0) {
49                 dl_print_errors(__func__);
50                 lt_dlclose(mod->dl);
51                 return FALSE;
52         }
53
54         return TRUE;
55 }
56
57 static void module_unload(GTypeModule *m)
58 {
59         UPkgModule *mod = UPKG_MODULE(m);
60
61         if (lt_dlclose(mod->dl) != 0) {
62                 dl_print_errors(__func__);
63         }
64 }
65
66 static void upkg_module_init(UPkgModule *mod)
67 {
68 }
69
70 static void upkg_module_class_init(UPkgModuleClass *class)
71 {
72         GTypeModuleClass *modclass = G_TYPE_MODULE_CLASS(class);
73
74         modclass->load   = module_load;
75         modclass->unload = module_unload;
76 }
77
78 UPkgModule *upkg_module_new(const char *name)
79 {
80         char *name2;
81
82         if (!name) {
83                 return NULL;
84         }
85
86         name2 = malloc(strlen(name)+1);
87         if (!name2) {
88                 return NULL;
89         }
90
91         UPkgModule *mod = g_object_new(UPKG_MODULE_TYPE, NULL);
92         if (!mod) {
93                 free(name2);
94                 return NULL;
95         }
96
97         G_TYPE_MODULE(mod)->name = str_cpy_lower(name2, name);
98         return mod;
99 }
100
101 static int modcmp(const void *a, const void *b, void *_data)
102 {
103         const GTypeModule *ma = a;
104         const GTypeModule *mb = b;
105
106         return strcmp(ma->name, mb->name);
107 }
108
109 int module_init(void)
110 {
111         if (!initialized) {
112                 package_tree = avl_create(modcmp, NULL, NULL);
113                 if (!package_tree) {
114                         fprintf(stderr, "%s: failed to create package tree.\n", __func__);
115                         return -1;
116                 }
117
118                 if (lt_dlinit() != 0) {
119                         avl_destroy(package_tree, NULL);
120                         dl_print_errors(__func__);
121                         return -1;
122                 }
123
124                 g_type_init();
125         }
126
127         initialized++;
128         return 0;
129 }
130
131 int module_exit(void)
132 {
133         if (--initialized)
134                 return 0;
135
136         if (lt_dlexit() != 0) {
137                 dl_print_errors(__func__);
138                 return -1;
139         }
140
141         return 0;
142 }
143
144 GType module_get_class(const char *package, const char *class)
145 {
146         char buf[strlen(package) + strlen(class) + 1];
147         GTypeModule search = { .name = str_cpy_lower(buf, package) };
148
149         GTypeModule *mod = avl_find(package_tree, &search);
150         if (!mod) {
151                 void **p;
152
153                 mod = G_TYPE_MODULE(upkg_module_new(package));
154                 if (!mod) {
155                         return 0;
156                 }
157
158                 p = avl_probe(package_tree, mod);
159                 if (!p) {
160                         g_object_unref(mod);
161                         return 0;
162                 }
163         }
164
165         if (!g_type_module_use(mod))
166                 return 0;
167
168         str_cpy_lower(buf+strlen(package), class);
169         buf[0] = toupper(buf[0]);
170         buf[strlen(package)] = toupper(buf[strlen(package)]);
171         return g_type_from_name(buf);
172 }