2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
modules.c : irssi
|
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
Copyright (C) 1999-2001 Timo Sirainen
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "modules.h"
|
2000-07-02 18:04:00 -04:00
|
|
|
#include "signals.h"
|
|
|
|
|
|
|
|
GSList *modules;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
static GHashTable *uniqids, *uniqstrids;
|
|
|
|
static GHashTable *idlookup, *stridlookup;
|
|
|
|
static int next_uniq_id;
|
|
|
|
|
2000-08-30 18:29:55 -04:00
|
|
|
void *module_check_cast(void *object, int type_pos, const char *id)
|
|
|
|
{
|
2000-09-02 14:53:58 -04:00
|
|
|
return object == NULL || module_find_id(id,
|
|
|
|
G_STRUCT_MEMBER(int, object, type_pos)) == -1 ? NULL : object;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *module_check_cast_module(void *object, int type_pos,
|
|
|
|
const char *module, const char *id)
|
|
|
|
{
|
2000-09-02 15:29:32 -04:00
|
|
|
const char *str;
|
|
|
|
|
|
|
|
if (object == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
str = module_find_id_str(module,
|
|
|
|
G_STRUCT_MEMBER(int, object, type_pos));
|
|
|
|
return str == NULL || strcmp(str, id) != 0 ? NULL : object;
|
2000-08-30 18:29:55 -04:00
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* return unique number across all modules for `id' */
|
|
|
|
int module_get_uniq_id(const char *module, int id)
|
|
|
|
{
|
|
|
|
GHashTable *ids;
|
2000-07-16 16:18:05 -04:00
|
|
|
gpointer origkey, uniqid, idp;
|
2000-04-26 04:03:38 -04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail(module != NULL, -1);
|
|
|
|
|
|
|
|
ids = g_hash_table_lookup(idlookup, module);
|
|
|
|
if (ids == NULL) {
|
|
|
|
/* new module */
|
2000-07-16 16:18:05 -04:00
|
|
|
ids = g_hash_table_new((GHashFunc) g_direct_hash,
|
|
|
|
(GCompareFunc) g_direct_equal);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_hash_table_insert(idlookup, g_strdup(module), ids);
|
|
|
|
}
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
idp = GINT_TO_POINTER(id);
|
|
|
|
if (!g_hash_table_lookup_extended(ids, idp, &origkey, &uniqid)) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* not found */
|
|
|
|
ret = next_uniq_id++;
|
2000-07-16 16:18:05 -04:00
|
|
|
g_hash_table_insert(ids, idp, GINT_TO_POINTER(ret));
|
|
|
|
g_hash_table_insert(uniqids, GINT_TO_POINTER(ret), idp);
|
2000-04-26 04:03:38 -04:00
|
|
|
} else {
|
|
|
|
ret = GPOINTER_TO_INT(uniqid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return unique number across all modules for `id' */
|
|
|
|
int module_get_uniq_id_str(const char *module, const char *id)
|
|
|
|
{
|
|
|
|
GHashTable *ids;
|
|
|
|
gpointer origkey, uniqid;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail(module != NULL, -1);
|
|
|
|
|
|
|
|
ids = g_hash_table_lookup(stridlookup, module);
|
|
|
|
if (ids == NULL) {
|
|
|
|
/* new module */
|
2000-07-16 16:18:05 -04:00
|
|
|
ids = g_hash_table_new((GHashFunc) g_str_hash,
|
|
|
|
(GCompareFunc) g_str_equal);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_hash_table_insert(stridlookup, g_strdup(module), ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_hash_table_lookup_extended(ids, id, &origkey, &uniqid)) {
|
|
|
|
/* not found */
|
|
|
|
char *saveid;
|
|
|
|
|
|
|
|
saveid = g_strdup(id);
|
|
|
|
ret = next_uniq_id++;
|
|
|
|
g_hash_table_insert(ids, saveid, GINT_TO_POINTER(ret));
|
|
|
|
g_hash_table_insert(uniqstrids, GINT_TO_POINTER(ret), saveid);
|
|
|
|
} else {
|
|
|
|
ret = GPOINTER_TO_INT(uniqid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns the original module specific id, -1 = not found */
|
|
|
|
int module_find_id(const char *module, int uniqid)
|
|
|
|
{
|
2000-07-16 16:18:05 -04:00
|
|
|
GHashTable *idlist;
|
2000-04-26 04:03:38 -04:00
|
|
|
gpointer origkey, id;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail(module != NULL, -1);
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
if (!g_hash_table_lookup_extended(uniqids, GINT_TO_POINTER(uniqid),
|
|
|
|
&origkey, &id))
|
|
|
|
return -1;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
/* check that module matches */
|
|
|
|
idlist = g_hash_table_lookup(idlookup, module);
|
|
|
|
if (idlist == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = GPOINTER_TO_INT(id);
|
|
|
|
if (!g_hash_table_lookup_extended(idlist, id, &origkey, &id) ||
|
|
|
|
GPOINTER_TO_INT(id) != uniqid)
|
|
|
|
ret = -1;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns the original module specific id, NULL = not found */
|
|
|
|
const char *module_find_id_str(const char *module, int uniqid)
|
|
|
|
{
|
2000-07-16 16:18:05 -04:00
|
|
|
GHashTable *idlist;
|
2000-04-26 04:03:38 -04:00
|
|
|
gpointer origkey, id;
|
|
|
|
const char *ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail(module != NULL, NULL);
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
if (!g_hash_table_lookup_extended(uniqstrids, GINT_TO_POINTER(uniqid),
|
|
|
|
&origkey, &id))
|
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
/* check that module matches */
|
|
|
|
idlist = g_hash_table_lookup(stridlookup, module);
|
|
|
|
if (idlist == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = id;
|
|
|
|
if (!g_hash_table_lookup_extended(idlist, id, &origkey, &id) ||
|
|
|
|
GPOINTER_TO_INT(id) != uniqid)
|
|
|
|
ret = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
static void uniq_destroy(gpointer key, gpointer value)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
g_hash_table_remove(uniqids, value);
|
|
|
|
}
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
static void uniq_destroy_str(gpointer key, gpointer value)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
g_hash_table_remove(uniqstrids, value);
|
|
|
|
g_free(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy unique IDs from `module'. This function is automatically called
|
|
|
|
when module is destroyed with module's name as the parameter. */
|
|
|
|
void module_uniq_destroy(const char *module)
|
|
|
|
{
|
2000-07-16 16:18:05 -04:00
|
|
|
GHashTable *idlist;
|
2000-04-26 04:03:38 -04:00
|
|
|
gpointer key;
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
if (g_hash_table_lookup_extended(idlookup, module, &key,
|
|
|
|
(gpointer *) &idlist)) {
|
2000-04-26 04:03:38 -04:00
|
|
|
g_hash_table_remove(idlookup, key);
|
|
|
|
g_free(key);
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
g_hash_table_foreach(idlist, (GHFunc) uniq_destroy, NULL);
|
|
|
|
g_hash_table_destroy(idlist);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
if (g_hash_table_lookup_extended(stridlookup, module, &key,
|
|
|
|
(gpointer *) &idlist)) {
|
2000-04-26 04:03:38 -04:00
|
|
|
g_hash_table_remove(stridlookup, key);
|
|
|
|
g_free(key);
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
g_hash_table_foreach(idlist, (GHFunc) uniq_destroy_str, NULL);
|
|
|
|
g_hash_table_destroy(idlist);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
/* Register a new module. The `name' is the root module name, `submodule'
|
|
|
|
specifies the current module to be registered (eg. "perl", "fe").
|
|
|
|
The module is registered as statically loaded by default. */
|
|
|
|
MODULE_FILE_REC *module_register_full(const char *name, const char *submodule,
|
|
|
|
const char *defined_module_name)
|
2000-07-02 18:04:00 -04:00
|
|
|
{
|
2001-08-13 20:41:59 -04:00
|
|
|
MODULE_REC *module;
|
|
|
|
MODULE_FILE_REC *file;
|
2000-07-02 18:04:00 -04:00
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
module = module_find(name);
|
|
|
|
if (module == NULL) {
|
|
|
|
module = g_new0(MODULE_REC, 1);
|
|
|
|
module->name = g_strdup(name);
|
2000-07-02 18:04:00 -04:00
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
modules = g_slist_append(modules, module);
|
2000-07-02 18:04:00 -04:00
|
|
|
}
|
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
file = module_file_find(module, submodule);
|
|
|
|
if (file != NULL)
|
|
|
|
return file;
|
2000-07-02 18:04:00 -04:00
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
file = g_new0(MODULE_FILE_REC, 1);
|
|
|
|
file->root = module;
|
|
|
|
file->name = g_strdup(submodule);
|
|
|
|
file->defined_module_name = g_strdup(defined_module_name);
|
2000-07-02 18:04:00 -04:00
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
module->files = g_slist_append(module->files, file);
|
|
|
|
return file;
|
2000-07-02 18:04:00 -04:00
|
|
|
}
|
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
MODULE_REC *module_find(const char *name)
|
2000-07-02 18:04:00 -04:00
|
|
|
{
|
2001-08-13 20:41:59 -04:00
|
|
|
GSList *tmp;
|
2000-07-16 16:18:05 -04:00
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
for (tmp = modules; tmp != NULL; tmp = tmp->next) {
|
|
|
|
MODULE_REC *rec = tmp->data;
|
2000-07-02 18:04:00 -04:00
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
if (g_strcasecmp(rec->name, name) == 0)
|
|
|
|
return rec;
|
2000-07-02 18:04:00 -04:00
|
|
|
}
|
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
return NULL;
|
2000-07-02 18:04:00 -04:00
|
|
|
}
|
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
MODULE_FILE_REC *module_file_find(MODULE_REC *module, const char *name)
|
2000-07-16 16:18:05 -04:00
|
|
|
{
|
2001-08-13 20:41:59 -04:00
|
|
|
GSList *tmp;
|
2000-07-16 16:18:05 -04:00
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
for (tmp = module->files; tmp != NULL; tmp = tmp->next) {
|
|
|
|
MODULE_FILE_REC *rec = tmp->data;
|
2000-07-16 16:18:05 -04:00
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
if (strcmp(rec->name, name) == 0)
|
|
|
|
return rec;
|
2001-02-17 05:35:35 -05:00
|
|
|
}
|
|
|
|
|
2001-08-13 20:41:59 -04:00
|
|
|
return NULL;
|
2000-07-02 18:04:00 -04:00
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static void uniq_get_modules(char *key, void *value, GSList **list)
|
|
|
|
{
|
2001-07-25 15:29:50 -04:00
|
|
|
*list = g_slist_append(*list, g_strdup(key));
|
2000-08-26 11:39:44 -04:00
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
void modules_init(void)
|
|
|
|
{
|
2000-07-02 18:04:00 -04:00
|
|
|
modules = NULL;
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
idlookup = g_hash_table_new((GHashFunc) g_str_hash,
|
|
|
|
(GCompareFunc) g_str_equal);
|
|
|
|
uniqids = g_hash_table_new((GHashFunc) g_direct_hash,
|
|
|
|
(GCompareFunc) g_direct_equal);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
stridlookup = g_hash_table_new((GHashFunc) g_str_hash,
|
|
|
|
(GCompareFunc) g_str_equal);
|
|
|
|
uniqstrids = g_hash_table_new((GHashFunc) g_direct_hash,
|
|
|
|
(GCompareFunc) g_direct_equal);
|
2000-04-26 04:03:38 -04:00
|
|
|
next_uniq_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void modules_deinit(void)
|
|
|
|
{
|
2000-08-26 11:39:44 -04:00
|
|
|
GSList *list;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
list = NULL;
|
|
|
|
g_hash_table_foreach(idlookup, (GHFunc) uniq_get_modules, &list);
|
|
|
|
g_hash_table_foreach(stridlookup, (GHFunc) uniq_get_modules, &list);
|
|
|
|
|
|
|
|
while (list != NULL) {
|
|
|
|
module_uniq_destroy(list->data);
|
2001-07-25 15:29:50 -04:00
|
|
|
g_free(list->data);
|
2000-08-26 11:39:44 -04:00
|
|
|
list = g_slist_remove(list, list->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_hash_table_destroy(idlookup);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_hash_table_destroy(stridlookup);
|
2000-08-26 11:39:44 -04:00
|
|
|
g_hash_table_destroy(uniqids);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_hash_table_destroy(uniqstrids);
|
|
|
|
}
|