jabberd2  2.2.16
sx/env.c
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
00004  *                    Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00021 #include "sx.h"
00022 
00023 sx_env_t sx_env_new(void) {
00024     sx_env_t env;
00025 
00026     env = (sx_env_t) calloc(1, sizeof(struct _sx_env_st));
00027 
00028     return env;
00029 }
00030 
00031 void sx_env_free(sx_env_t env) {
00032     int i;
00033 
00034     assert((int) (env != NULL));
00035 
00036     /* !!! usage counts */
00037 
00038     for(i = 0; i < env->nplugins; i++) {
00039         if(env->plugins[i]->unload != NULL)
00040             (env->plugins[i]->unload)(env->plugins[i]);
00041         free(env->plugins[i]);
00042     }
00043 
00044     free(env->plugins);
00045     free(env);
00046 }
00047 
00048 sx_plugin_t sx_env_plugin(sx_env_t env, sx_plugin_init_t init, ...) {
00049     sx_plugin_t p;
00050     int ret;
00051     va_list args;
00052 
00053     assert((int) (env != NULL));
00054     assert((int) (init != NULL));
00055 
00056     va_start(args, init);
00057 
00058     p = (sx_plugin_t) calloc(1, sizeof(struct _sx_plugin_st));
00059 
00060     p->env = env;
00061     p->index = env->nplugins;
00062 
00063     ret = (init)(env, p, args);
00064     va_end(args);
00065 
00066     if(ret != 0) {
00067         free(p);
00068         return NULL;
00069     }
00070 
00071     env->plugins = (sx_plugin_t *) realloc(env->plugins, sizeof(sx_plugin_t) * (env->nplugins + 1));
00072     env->plugins[env->nplugins] = p;
00073     env->nplugins++;
00074 
00075     _sx_debug(ZONE, "plugin initialised (index %d)", p->index);
00076 
00077     return p;
00078 }