diff options
author | Christian Pointner <equinox@anylike.org> | 2009-12-26 16:14:02 +0000 |
---|---|---|
committer | Christian Pointner <equinox@anylike.org> | 2009-12-26 16:14:02 +0000 |
commit | 7821bd5c0ebe8c291fa8a3b3084e363353f4187c (patch) | |
tree | 52623af75b944a80d08c02bd6d3b34c83c6dfc43 | |
parent | added log targets string list to opt tables as well (diff) |
moved options table functions to options.c
-rw-r--r-- | src/anylike.c | 57 | ||||
-rw-r--r-- | src/options.c | 57 | ||||
-rw-r--r-- | src/options.h | 7 |
3 files changed, 66 insertions, 55 deletions
diff --git a/src/anylike.c b/src/anylike.c index 223eef5..1fd8442 100644 --- a/src/anylike.c +++ b/src/anylike.c @@ -122,60 +122,6 @@ int init_main_loop(lua_State *L) return 0; } -void push_opt_value_string(lua_State* L, const int tidx, const char* key, const char* value) -{ - lua_pushstring(L, key); - lua_pushstring(L, value); - lua_settable(L, tidx); -} - -void push_opt_value_int(lua_State* L, const int tidx, const char* key, const u_int32_t value) -{ - lua_pushstring(L, key); - lua_pushinteger(L, value); - lua_settable(L, tidx); -} - -void push_opt_value_boolean(lua_State* L, const int tidx, const char* key, const u_int32_t value) -{ - lua_pushstring(L, key); - lua_pushboolean(L, value); - lua_settable(L, tidx); -} - -void push_opt_string_list(lua_State* L, const int tidx, string_list_t* lst) -{ - if(!lst) - return; - - string_list_element_t* tmp = lst->first_; - if(tmp) { - lua_pushstring(L, "log_targets"); - lua_newtable(L); - int i = 1; - while(tmp) { - lua_pushinteger(L, i++); - lua_pushstring(L, tmp->string_); - lua_settable(L, -3); - tmp = tmp->next_; - } - lua_settable(L, tidx); - } -} - -void push_opt_table(lua_State* L, options_t* opt) -{ - lua_newtable(L); - - push_opt_value_string(L, -3, "progname", opt->progname_); - push_opt_value_boolean(L, -3, "daemonize", opt->daemonize_); - push_opt_value_string(L, -3, "username", opt->username_); - push_opt_value_string(L, -3, "groupname", opt->groupname_); - push_opt_value_string(L, -3, "chroot_dir", opt->chroot_dir_); - push_opt_value_string(L, -3, "pid_file", opt->pid_file_); - push_opt_string_list(L, -3, &(opt->log_targets_)); -} - int call_main_loop(lua_State* L, options_t* opt) { lua_getglobal(L, LUA_MAIN_LOOP_FUNC); @@ -183,7 +129,8 @@ int call_main_loop(lua_State* L, options_t* opt) log_printf(ERROR, "there is no function '%s' in '%s'", LUA_MAIN_LOOP_FUNC, LUA_MAIN_LOOP_FILE); return -1; }; - push_opt_table(L, opt); + + options_lua_push(opt, L); int ret = lua_pcall(L, 1, LUA_MULTRET, 0); if(ret) { diff --git a/src/options.c b/src/options.c index 8684181..dde60da 100644 --- a/src/options.c +++ b/src/options.c @@ -23,6 +23,9 @@ * along with anylike. If not, see <http://www.gnu.org/licenses/>. */ +#include <lua5.1/lua.h> +#include <lua5.1/lauxlib.h> + #include "datatypes.h" #include "version.h" @@ -204,6 +207,60 @@ void options_default(options_t* opt) string_list_init(&opt->log_targets_); } +void options_lua_push_string(lua_State* L, const int tidx, const char* key, const char* value) +{ + lua_pushstring(L, key); + lua_pushstring(L, value); + lua_settable(L, tidx); +} + +void options_lua_push_int(lua_State* L, const int tidx, const char* key, const u_int32_t value) +{ + lua_pushstring(L, key); + lua_pushinteger(L, value); + lua_settable(L, tidx); +} + +void options_lua_push_boolean(lua_State* L, const int tidx, const char* key, const u_int32_t value) +{ + lua_pushstring(L, key); + lua_pushboolean(L, value); + lua_settable(L, tidx); +} + +void options_lua_push_string_list(lua_State* L, const int tidx, string_list_t* lst) +{ + if(!lst) + return; + + string_list_element_t* tmp = lst->first_; + if(tmp) { + lua_pushstring(L, "log_targets"); + lua_newtable(L); + int i = 1; + while(tmp) { + lua_pushinteger(L, i++); + lua_pushstring(L, tmp->string_); + lua_settable(L, -3); + tmp = tmp->next_; + } + lua_settable(L, tidx); + } +} + +void options_lua_push(options_t* opt, lua_State* L) +{ + lua_newtable(L); + + options_lua_push_string(L, -3, "progname", opt->progname_); + options_lua_push_boolean(L, -3, "daemonize", opt->daemonize_); + options_lua_push_string(L, -3, "username", opt->username_); + options_lua_push_string(L, -3, "groupname", opt->groupname_); + options_lua_push_string(L, -3, "chroot_dir", opt->chroot_dir_); + options_lua_push_string(L, -3, "pid_file", opt->pid_file_); + options_lua_push_string_list(L, -3, &(opt->log_targets_)); +} + void options_clear(options_t* opt) { if(!opt) diff --git a/src/options.h b/src/options.h index 1ffebda..a2c5597 100644 --- a/src/options.h +++ b/src/options.h @@ -26,6 +26,8 @@ #ifndef ANYLIKE_options_h_INCLUDED #define ANYLIKE_options_h_INCLUDED +#include <lua5.1/lua.h> + #include "string_list.h" #include "datatypes.h" @@ -45,6 +47,11 @@ int options_parse_hex_string(const char* hex, buffer_t* buffer); int options_parse(options_t* opt, int argc, char* argv[]); void options_parse_post(options_t* opt); void options_default(options_t* opt); +void options_lua_push_string(lua_State* L, const int tidx, const char* key, const char* value); +void options_lua_push_int(lua_State* L, const int tidx, const char* key, const u_int32_t value); +void options_lua_push_boolean(lua_State* L, const int tidx, const char* key, const u_int32_t value); +void options_lua_push_string_list(lua_State* L, const int tidx, string_list_t* lst); +void options_lua_push(options_t* opt, lua_State* L); void options_clear(options_t* opt); void options_print_usage(); void options_print(options_t* opt); |