diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2013-10-22 17:04:20 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2013-10-22 17:04:20 +0100 |
commit | b7a0873dac545a79778133152a9b6d9b4e47cc42 (patch) | |
tree | d389696b986920f8e8f66df01519f6f6d55a02b5 /src/lua | |
parent | 41e269801406c374a041da0fd0c6b4eff6ba4f3d (diff) | |
download | rspamd-b7a0873dac545a79778133152a9b6d9b4e47cc42.tar.gz rspamd-b7a0873dac545a79778133152a9b6d9b4e47cc42.zip |
Replace RCL to UCL from libucl to avoid duplicity in the code.
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/lua_cfg_file.c | 7 | ||||
-rw-r--r-- | src/lua/lua_common.h | 6 | ||||
-rw-r--r-- | src/lua/lua_config.c | 6 | ||||
-rw-r--r-- | src/lua/lua_rcl.c | 78 |
4 files changed, 45 insertions, 52 deletions
diff --git a/src/lua/lua_cfg_file.c b/src/lua/lua_cfg_file.c index 4f7a3bca5..0e22ec8f2 100644 --- a/src/lua/lua_cfg_file.c +++ b/src/lua/lua_cfg_file.c @@ -125,7 +125,7 @@ lua_post_load_config (struct config_file *cfg) const gchar *name, *val; gchar *sym; struct expression *expr, *old_expr; - rspamd_cl_object_t *obj; + ucl_object_t *obj; gsize keylen; /* First check all module options that may be overriden in 'config' global */ @@ -140,10 +140,7 @@ lua_post_load_config (struct config_file *cfg) if (name != NULL && lua_istable (L, -1)) { obj = lua_rcl_obj_get (L, -1); if (obj != NULL) { - obj->key = memory_pool_alloc (cfg->cfg_pool, keylen + 1); - memcpy (obj->key, name, keylen); - obj->key[keylen] = '\0'; - HASH_ADD_KEYPTR (hh, cfg->rcl_obj->value.ov, obj->key, keylen, obj); + cfg->rcl_obj = ucl_object_insert_key (cfg->rcl_obj, obj, name, keylen, true); } } } diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index c6ba06160..c878a50f3 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -6,7 +6,7 @@ #include "main.h" #include "cfg_file.h" -#include "rcl/rcl.h" +#include "ucl.h" #include <lua.h> #include <lauxlib.h> #include <lualib.h> @@ -107,14 +107,14 @@ void free_lua_locked (struct lua_locked_state *st); * @param L lua state * @param obj object to push */ -gint lua_rcl_obj_push (lua_State *L, rspamd_cl_object_t *obj); +gint lua_rcl_obj_push (lua_State *L, ucl_object_t *obj); /** * Extract rcl object from lua object * @param L * @return */ -rspamd_cl_object_t * lua_rcl_obj_get (lua_State *L, gint idx); +ucl_object_t * lua_rcl_obj_get (lua_State *L, gint idx); /** * Open libraries functions diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index d3b7c6af9..df8526d0f 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -154,7 +154,7 @@ lua_config_get_module_opt (lua_State * L) { struct config_file *cfg = lua_check_config (L); const gchar *mname, *optname; - rspamd_cl_object_t *obj; + ucl_object_t *obj; if (cfg) { mname = luaL_checkstring (L, 2); @@ -190,13 +190,13 @@ lua_config_get_all_opt (lua_State * L) { struct config_file *cfg = lua_check_config (L); const gchar *mname; - rspamd_cl_object_t *obj; + ucl_object_t *obj; if (cfg) { mname = luaL_checkstring (L, 2); if (mname) { - obj = rspamd_cl_obj_get_key (cfg->rcl_obj, mname); + obj = ucl_obj_get_key (cfg->rcl_obj, mname); if (obj != NULL) { return lua_rcl_obj_push (L, obj); } diff --git a/src/lua/lua_rcl.c b/src/lua/lua_rcl.c index 91f1fff79..b695f4f11 100644 --- a/src/lua/lua_rcl.c +++ b/src/lua/lua_rcl.c @@ -22,16 +22,15 @@ */ #include "lua_common.h" -#include "rcl/rcl.h" /** * @file lua rcl bindings */ -static gint lua_rcl_obj_push_array (lua_State *L, rspamd_cl_object_t *obj); -static gint lua_rcl_obj_push_simple (lua_State *L, rspamd_cl_object_t *obj); -static void lua_rcl_table_get (lua_State *L, rspamd_cl_object_t *top, gint idx); -static void lua_rcl_elt_get (lua_State *L, rspamd_cl_object_t *top, gint idx); +static gint lua_rcl_obj_push_array (lua_State *L, ucl_object_t *obj); +static gint lua_rcl_obj_push_simple (lua_State *L, ucl_object_t *obj); +static void lua_rcl_table_get (lua_State *L, ucl_object_t *top, gint idx); +static void lua_rcl_elt_get (lua_State *L, ucl_object_t *top, gint idx); /** * Push a single element of an object to lua @@ -40,7 +39,7 @@ static void lua_rcl_elt_get (lua_State *L, rspamd_cl_object_t *top, gint idx); * @param obj */ static void -lua_rcl_obj_push_elt (lua_State *L, const char *key, rspamd_cl_object_t *obj) +lua_rcl_obj_push_elt (lua_State *L, const char *key, ucl_object_t *obj) { lua_pushstring (L, key); lua_rcl_obj_push (L, obj); @@ -54,9 +53,9 @@ lua_rcl_obj_push_elt (lua_State *L, const char *key, rspamd_cl_object_t *obj) * @return */ static gint -lua_rcl_obj_push_obj (lua_State *L, rspamd_cl_object_t *obj) +lua_rcl_obj_push_obj (lua_State *L, ucl_object_t *obj) { - rspamd_cl_object_t *cur, *tmp; + ucl_object_t *cur, *tmp; if (obj->next != NULL) { /* Actually we need to push this as an array */ @@ -65,7 +64,7 @@ lua_rcl_obj_push_obj (lua_State *L, rspamd_cl_object_t *obj) lua_newtable (L); HASH_ITER (hh, obj, cur, tmp) { - lua_rcl_obj_push_elt (L, obj->key, obj); + lua_rcl_obj_push_elt (L, ucl_object_key (obj), obj); } return 1; @@ -78,9 +77,9 @@ lua_rcl_obj_push_obj (lua_State *L, rspamd_cl_object_t *obj) * @return */ static gint -lua_rcl_obj_push_array (lua_State *L, rspamd_cl_object_t *obj) +lua_rcl_obj_push_array (lua_State *L, ucl_object_t *obj) { - rspamd_cl_object_t *cur; + ucl_object_t *cur; gint i = 1; lua_newtable (L); @@ -98,7 +97,7 @@ lua_rcl_obj_push_array (lua_State *L, rspamd_cl_object_t *obj) * Push a simple object to lua depending on its actual type */ static gint -lua_rcl_obj_push_simple (lua_State *L, rspamd_cl_object_t *obj) +lua_rcl_obj_push_simple (lua_State *L, ucl_object_t *obj) { if (obj->next != NULL) { /* Actually we need to push this as an array */ @@ -106,22 +105,22 @@ lua_rcl_obj_push_simple (lua_State *L, rspamd_cl_object_t *obj) } switch (obj->type) { - case RSPAMD_CL_BOOLEAN: - lua_pushboolean (L, rspamd_cl_obj_toboolean (obj)); + case UCL_BOOLEAN: + lua_pushboolean (L, ucl_obj_toboolean (obj)); break; - case RSPAMD_CL_STRING: - lua_pushstring (L, rspamd_cl_obj_tostring (obj)); + case UCL_STRING: + lua_pushstring (L, ucl_obj_tostring (obj)); break; - case RSPAMD_CL_INT: + case UCL_INT: #if LUA_VERSION_NUM >= 501 - lua_pushinteger (L, rspamd_cl_obj_toint (obj)); + lua_pushinteger (L, ucl_obj_toint (obj)); #else - lua_pushnumber (L, rspamd_cl_obj_toint (obj)); + lua_pushnumber (L, ucl_obj_toint (obj)); #endif break; - case RSPAMD_CL_FLOAT: - case RSPAMD_CL_TIME: - lua_pushnumber (L, rspamd_cl_obj_todouble (obj)); + case UCL_FLOAT: + case UCL_TIME: + lua_pushnumber (L, ucl_obj_todouble (obj)); break; default: lua_pushnil (L); @@ -137,12 +136,12 @@ lua_rcl_obj_push_simple (lua_State *L, rspamd_cl_object_t *obj) * @param obj object to push */ gint -lua_rcl_obj_push (lua_State *L, rspamd_cl_object_t *obj) +lua_rcl_obj_push (lua_State *L, ucl_object_t *obj) { switch (obj->type) { - case RSPAMD_CL_OBJECT: + case UCL_OBJECT: return lua_rcl_obj_push_obj (L, obj->value.ov); - case RSPAMD_CL_ARRAY: + case UCL_ARRAY: return lua_rcl_obj_push_array (L, obj->value.ov); default: return lua_rcl_obj_push_simple (L, obj); @@ -156,9 +155,9 @@ lua_rcl_obj_push (lua_State *L, rspamd_cl_object_t *obj) * @param idx */ static void -lua_rcl_table_get (lua_State *L, rspamd_cl_object_t *top, gint idx) +lua_rcl_table_get (lua_State *L, ucl_object_t *top, gint idx) { - rspamd_cl_object_t *obj; + ucl_object_t *obj; gsize keylen; const gchar *k; @@ -168,13 +167,10 @@ lua_rcl_table_get (lua_State *L, rspamd_cl_object_t *top, gint idx) while (lua_next (L, -2) != 0) { /* copy key to avoid modifications */ lua_pushvalue (L, -2); - obj = rspamd_cl_object_new (); + obj = ucl_object_new (); if (obj != NULL) { k = lua_tolstring (L, -1, &keylen); - obj->key = g_malloc (keylen + 1); - memcpy (obj->key, k, keylen); - obj->key[keylen] = '\0'; - HASH_ADD_KEYPTR (hh, top->value.ov, obj->key, keylen, obj); + ucl_object_insert_key (top, obj, k, keylen, true); lua_rcl_elt_get (L, obj, -2); } @@ -190,7 +186,7 @@ lua_rcl_table_get (lua_State *L, rspamd_cl_object_t *top, gint idx) * @param idx */ static void -lua_rcl_elt_get (lua_State *L, rspamd_cl_object_t *obj, gint idx) +lua_rcl_elt_get (lua_State *L, ucl_object_t *obj, gint idx) { gint type; @@ -199,23 +195,23 @@ lua_rcl_elt_get (lua_State *L, rspamd_cl_object_t *obj, gint idx) switch (type) { case LUA_TFUNCTION: lua_pushvalue (L, idx); - obj->type = RSPAMD_CL_USERDATA; + obj->type = UCL_USERDATA; obj->value.ud = GINT_TO_POINTER (luaL_ref (L, LUA_REGISTRYINDEX)); break; case LUA_TSTRING: - obj->type = RSPAMD_CL_STRING; + obj->type = UCL_STRING; obj->value.sv = g_strdup (lua_tostring (L, idx)); break; case LUA_TNUMBER: - obj->type = RSPAMD_CL_FLOAT; + obj->type = UCL_FLOAT; obj->value.dv = lua_tonumber (L, idx); break; case LUA_TBOOLEAN: - obj->type = RSPAMD_CL_BOOLEAN; + obj->type = UCL_BOOLEAN; obj->value.iv = lua_toboolean (L, idx); break; case LUA_TTABLE: - obj->type = RSPAMD_CL_OBJECT; + obj->type = UCL_OBJECT; lua_rcl_table_get (L, obj, idx); break; } @@ -226,13 +222,13 @@ lua_rcl_elt_get (lua_State *L, rspamd_cl_object_t *obj, gint idx) * @param L * @return */ -rspamd_cl_object_t * +ucl_object_t * lua_rcl_obj_get (lua_State *L, gint idx) { - rspamd_cl_object_t *obj; + ucl_object_t *obj; gint t; - obj = rspamd_cl_object_new (); + obj = ucl_object_new (); if (obj != NULL) { t = lua_type (L, idx); |