Mercurial > ~mikael > mcabber > hg
comparison mcabber/src/commands.c @ 1627:ff22a18abfe6
Load module only once
* do not load module, that is already loaded
* coding style improvements
author | Myhailo Danylenko <isbear@ukrpost.net> |
---|---|
date | Wed, 21 Oct 2009 00:17:45 +0300 |
parents | 055ea3cdbcd3 |
children | 546868650bbf |
comparison
equal
deleted
inserted
replaced
1626:055ea3cdbcd3 | 1627:ff22a18abfe6 |
---|---|
2968 g_string_free(sbuf, TRUE); | 2968 g_string_free(sbuf, TRUE); |
2969 g_slist_free(bm); | 2969 g_slist_free(bm); |
2970 } | 2970 } |
2971 | 2971 |
2972 #ifdef MODULES_ENABLE | 2972 #ifdef MODULES_ENABLE |
2973 static gint module_list_comparator(gconstpointer arg1, gconstpointer arg2) | |
2974 { | |
2975 const loaded_module_t *module = arg1; | |
2976 const char *name = arg2; | |
2977 return g_strcmp0(module->name, name); | |
2978 } | |
2979 | |
2973 static void do_load(char *arg) | 2980 static void do_load(char *arg) |
2974 { | 2981 { |
2982 GModule *mod; | |
2983 GSList *lmod; | |
2984 char *mdir, *path; | |
2975 if (!arg || !*arg) { | 2985 if (!arg || !*arg) { |
2976 scr_LogPrint (LPRINT_LOGNORM, "Missing modulename."); | 2986 scr_LogPrint(LPRINT_LOGNORM, "Missing modulename."); |
2977 return; | 2987 return; |
2978 } | 2988 } |
2979 char *mdir = expand_filename (settings_opt_get ("modules_dir")); | 2989 lmod = g_slist_find_custom(loaded_modules, arg, module_list_comparator); |
2980 char *path = g_module_build_path (mdir, arg); | 2990 if (lmod) { |
2981 GModule *mod = g_module_open (path, G_MODULE_BIND_LAZY); | 2991 scr_LogPrint(LPRINT_LOGNORM, "Module %s is already loaded.", arg); |
2992 return; | |
2993 } | |
2994 mdir = expand_filename(settings_opt_get("modules_dir")); | |
2995 path = g_module_build_path(mdir, arg); | |
2996 mod = g_module_open(path, G_MODULE_BIND_LAZY); | |
2982 if (!mod) | 2997 if (!mod) |
2983 scr_LogPrint (LPRINT_LOGNORM, "Module %s loading failed: %s", | 2998 scr_LogPrint(LPRINT_LOGNORM, "Module loading failed: %s", |
2984 path, g_module_error ()); | 2999 g_module_error()); |
2985 else { | 3000 else { |
2986 loaded_module_t *module = g_new (loaded_module_t, 1); | 3001 loaded_module_t *module = g_new(loaded_module_t, 1); |
2987 module->name = g_strdup (arg); | 3002 module->name = g_strdup(arg); |
2988 module->module = mod; | 3003 module->module = mod; |
2989 loaded_modules = g_slist_prepend (loaded_modules, module); | 3004 loaded_modules = g_slist_prepend(loaded_modules, module); |
2990 scr_LogPrint (LPRINT_LOGNORM, "Loaded module %s", arg); | 3005 scr_LogPrint(LPRINT_LOGNORM, "Loaded module %s.", arg); |
2991 } | 3006 } |
2992 g_free (path); | 3007 g_free(path); |
2993 if (mdir) | 3008 g_free(mdir); |
2994 g_free (mdir); | |
2995 } | |
2996 | |
2997 static int module_list_comparator (loaded_module_t *module, const char *name) | |
2998 { | |
2999 return g_strcmp0 (module->name, name); | |
3000 } | 3009 } |
3001 | 3010 |
3002 static void do_unload(char *arg) | 3011 static void do_unload(char *arg) |
3003 { | 3012 { |
3013 GSList *module; | |
3004 if (!arg || !*arg) { | 3014 if (!arg || !*arg) { |
3005 scr_LogPrint (LPRINT_LOGNORM, "Missing modulename."); | 3015 scr_LogPrint(LPRINT_LOGNORM, "Missing modulename."); |
3006 return; | 3016 return; |
3007 } | 3017 } |
3008 GSList *module = g_slist_find_custom (loaded_modules, arg, (GCompareFunc) module_list_comparator); | 3018 module = g_slist_find_custom(loaded_modules, arg, module_list_comparator); |
3009 if (module) { | 3019 if (module) { |
3010 loaded_module_t *mod = module->data; | 3020 loaded_module_t *mod = module->data; |
3011 if (!g_module_close ((GModule *) mod->module)) | 3021 if (!g_module_close(mod->module)) |
3012 scr_LogPrint (LPRINT_LOGNORM, "Module unloading failed: %s", | 3022 scr_LogPrint(LPRINT_LOGNORM, "Module unloading failed: %s", |
3013 g_module_error ()); | 3023 g_module_error()); |
3014 else { | 3024 else { |
3015 g_free (mod->name); | 3025 g_free(mod->name); |
3016 g_free (mod); | 3026 g_free(mod); |
3017 loaded_modules = g_slist_delete_link (loaded_modules, module); | 3027 loaded_modules = g_slist_delete_link(loaded_modules, module); |
3028 scr_LogPrint(LPRINT_LOGNORM, "Unloaded module %s.", arg); | |
3018 } | 3029 } |
3019 } else | 3030 } else |
3020 scr_LogPrint (LPRINT_LOGNORM, "Module %s not loaded.", arg); | 3031 scr_LogPrint(LPRINT_LOGNORM, "Module %s not loaded.", arg); |
3021 } | 3032 } |
3022 #endif | 3033 #endif |
3023 | 3034 |
3024 static void do_room(char *arg) | 3035 static void do_room(char *arg) |
3025 { | 3036 { |