diff --git a/trunk/configure.in b/trunk/configure.in index 09df792..c4c7206 100644 --- a/trunk/configure.in +++ b/trunk/configure.in @@ -872,6 +872,7 @@ AC_CONFIG_FILES([ include/sj3compat/Makefile include/sj3core/Makefile include/sj3lib/Makefile + include/sj3lua/Makefile include/sj3rkcv/Makefile dict/Makefile doc/Makefile @@ -881,6 +882,7 @@ AC_CONFIG_FILES([ lib/sj3compat/Makefile lib/sj3core/Makefile lib/sj3lib/Makefile + lib/sj3lua/Makefile lib/lua/Makefile lib/lua/doc/Makefile lib/lua/etc/Makefile diff --git a/trunk/include/Makefile.am b/trunk/include/Makefile.am index b501b41..57e3ff4 100644 --- a/trunk/include/Makefile.am +++ b/trunk/include/Makefile.am @@ -3,4 +3,5 @@ SUBDIRS = \ sj3compat \ sj3core \ sj3lib \ + sj3lua \ sj3rkcv diff --git a/trunk/include/sj3lua/sj3lua.h b/trunk/include/sj3lua/sj3lua.h new file mode 100644 index 0000000..217e376 --- /dev/null +++ b/trunk/include/sj3lua/sj3lua.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2004 Iwata + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef SJ3LUA_H +#define SJ3LUA_H + +#include +#include + +int sj3lua_check_table(lua_State *); +int lua2sj_boolean(lua_State *, char *, int, int, int *); +int lua2sj_integer(lua_State *, char *, int, int, int *); +char *lua2sj_string(lua_State *, char *, int, char *, char *, size_t); +void sj2lua_boolean(lua_State *, char *, int); +void sj2lua_integer(lua_State *, char *, int); +void sj2lua_string(lua_State *, char *, char *); + +#endif + diff --git a/trunk/lib/Makefile.am b/trunk/lib/Makefile.am index e775da0..47ccb8e 100644 --- a/trunk/lib/Makefile.am +++ b/trunk/lib/Makefile.am @@ -2,6 +2,7 @@ SUBDIRS = \ sj3compat \ sj3core \ sj3lib \ + sj3lua \ sj3rkcv if SJ3_INTERNAL_LUA diff --git a/trunk/lib/sj3lua/Makefile.am b/trunk/lib/sj3lua/Makefile.am new file mode 100644 index 0000000..fe532fd --- /dev/null +++ b/trunk/lib/sj3lua/Makefile.am @@ -0,0 +1,16 @@ +INCLUDES = \ + -I$(top_srcdir)/include/sj3compat \ + -I$(top_srcdir)/include/sj3lua + +AM_CFLAGS = $(lua_CFLAGS) + +if SJ3_INTERNAL_LUA +AM_CFLAGS += -I$(top_builddir)/lib/lua/src +else +AM_CFLAGS += $(LUA_CFLAGS) +endif + +noinst_LTLIBRARIES = libsj3lua.la + +libsj3lua_la_SOURCES = \ + sj3lua.c diff --git a/trunk/lib/sj3lua/sj3lua.c b/trunk/lib/sj3lua/sj3lua.c new file mode 100644 index 0000000..41ab8ae --- /dev/null +++ b/trunk/lib/sj3lua/sj3lua.c @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2004 Iwata + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include + +#include +#include +#include + +#include "sj3lua.h" + +int +sj3lua_check_table(lua_State *lstate) +{ + if (!lua_istable(lstate, 1)) { + fprintf(stderr, "Illegal type of table (type=%s).\n", + lua_typename(lstate, lua_type(lstate, 1))); + return 0; + } + return 1; +} + +int +lua2sj_boolean(lua_State *lstate, char *name, int idx, int default_val, int *i) +{ + lua_getfield(lstate, idx, name); + lua_pop(lstate, idx); + + if (lua_isboolean(lstate, idx - 1)) { + *i = (int)lua_toboolean(lstate, idx - 1); + return *i; + } else if (lua_isnil(lstate, idx - 1)) { + *i = default_val; + return *i; + } else { + fprintf(stderr, "Illegal type of '%s'(%s), must be boolean.\n", + name, lua_typename(lstate, lua_type(lstate, idx - 1))); + exit(1); + } +} + +int +lua2sj_integer(lua_State *lstate, char *name, int idx, int default_val, int *i) +{ + lua_getfield(lstate, idx, name); + lua_pop(lstate, idx); + + if (lua_isnumber(lstate, idx - 1)) { + *i = (int)lua_tonumber(lstate, idx - 1); + return *i; + } else if (lua_isnil(lstate, idx - 1)) { + *i = default_val; + return *i; + } else { + fprintf(stderr, "Illegal type of '%s'(%s), must be integer.\n", + name, lua_typename(lstate, lua_type(lstate, idx - 1))); + exit(1); + } +} + +char * +lua2sj_string(lua_State *lstate, char *name, int idx, char *default_val, char *str, size_t len) +{ + lua_getfield(lstate, idx, name); + lua_pop(lstate, idx); + + if (lua_isstring(lstate, idx - 1)) { + strlcpy(str, lua_tostring(lstate, idx - 1), len); + return str; + } else if (lua_isnil(lstate, idx - 1)) { + if (default_val == NULL) + str[0] = '\0'; + else + strlcpy(str, default_val, len); + return str; + } else { + fprintf(stderr, "Illegal type of '%s'(%s), must be string.\n", + name, lua_typename(lstate, lua_type(lstate, idx - 1))); + exit(1); + } +} + +void +sj2lua_boolean(lua_State *lstate, char *name, int i) +{ + lua_pushstring(lstate, name); + lua_pushboolean(lstate, i); + lua_settable(lstate, -3); +} + +void +sj2lua_integer(lua_State *lstate, char *name, int i) +{ + lua_pushstring(lstate, name); + lua_pushnumber(lstate, i); + lua_settable(lstate, -3); +} + +void +sj2lua_string(lua_State *lstate, char *name, char *s) +{ + lua_pushstring(lstate, name); + lua_pushstring(lstate, s); + lua_settable(lstate, -3); +} diff --git a/trunk/src/sj3serv/Makefile.am b/trunk/src/sj3serv/Makefile.am index ed6b816..2789c48 100644 --- a/trunk/src/sj3serv/Makefile.am +++ b/trunk/src/sj3serv/Makefile.am @@ -10,11 +10,13 @@ AM_CFLAGS = $(lua_CFLAGS) INCLUDES = \ -I$(top_srcdir)/include/sj3compat \ -I$(top_srcdir)/include/sj3common \ - -I$(top_srcdir)/include/sj3core + -I$(top_srcdir)/include/sj3core \ + -I$(top_srcdir)/include/sj3lua LDADD = \ $(top_srcdir)/lib/sj3core/libsj3core.la \ - $(top_srcdir)/lib/sj3compat/libsj3compat.la + $(top_srcdir)/lib/sj3compat/libsj3compat.la \ + $(top_srcdir)/lib/sj3lua/libsj3lua.la if SJ3_INTERNAL_LUA AM_CFLAGS += -I$(top_builddir)/lib/lua/src diff --git a/trunk/src/sj3serv/setup.c b/trunk/src/sj3serv/setup.c index 6f6dd20..718780c 100644 --- a/trunk/src/sj3serv/setup.c +++ b/trunk/src/sj3serv/setup.c @@ -48,6 +48,8 @@ #include #include +#include "sj3lua.h" + #include "sj_typedef.h" #include "sj_const.h" #include "sj_var.h" @@ -114,105 +116,10 @@ struct allow_user_entry { static int opt_daemon_disable = 0; static lua_State *lua_state; -static int -check_table(lua_State *lstate) -{ - if (!lua_istable(lstate, 1)) { - fprintf(stderr, "Illegal type of table (type=%s).\n", - lua_typename(lstate, lua_type(lstate, 1))); - return 0; - } - return 1; -} - -static int -lua2sj_boolean(lua_State *lstate, char *name, int idx, int default_val, int *i) -{ - lua_getfield(lstate, idx, name); - lua_pop(lstate, idx); - - if (lua_isboolean(lstate, idx - 1)) { - *i = (int)lua_toboolean(lstate, idx - 1); - return *i; - } else if (lua_isnil(lstate, idx - 1)) { - *i = default_val; - return *i; - } else { - fprintf(stderr, "Illegal type of '%s'(%s), must be boolean.\n", - name, lua_typename(lstate, lua_type(lstate, idx - 1))); - exit(1); - } -} - -static int -lua2sj_integer(lua_State *lstate, char *name, int idx, int default_val, int *i) -{ - lua_getfield(lstate, idx, name); - lua_pop(lstate, idx); - - if (lua_isnumber(lstate, idx - 1)) { - *i = (int)lua_tonumber(lstate, idx - 1); - return *i; - } else if (lua_isnil(lstate, idx - 1)) { - *i = default_val; - return *i; - } else { - fprintf(stderr, "Illegal type of '%s'(%s), must be integer.\n", - name, lua_typename(lstate, lua_type(lstate, idx - 1))); - exit(1); - } -} - -static char * -lua2sj_string(lua_State *lstate, char *name, int idx, char *default_val, char *str, size_t len) -{ - lua_getfield(lstate, idx, name); - lua_pop(lstate, idx); - - if (lua_isstring(lstate, idx - 1)) { - strlcpy(str, lua_tostring(lstate, idx - 1), len); - return str; - } else if (lua_isnil(lstate, idx - 1)) { - if (default_val == NULL) - str[0] = '\0'; - else - strlcpy(str, default_val, len); - return str; - } else { - fprintf(stderr, "Illegal type of '%s'(%s), must be string.\n", - name, lua_typename(lstate, lua_type(lstate, idx - 1))); - exit(1); - } -} - -static void -sj2lua_boolean(lua_State *lstate, char *name, int i) -{ - lua_pushstring(lstate, name); - lua_pushboolean(lstate, i); - lua_settable(lstate, -3); -} - -static void -sj2lua_integer(lua_State *lstate, char *name, int i) -{ - lua_pushstring(lstate, name); - lua_pushnumber(lstate, i); - lua_settable(lstate, -3); -} - -static void -sj2lua_string(lua_State *lstate, char *name, char *s) -{ - lua_pushstring(lstate, name); - lua_pushstring(lstate, s); - lua_settable(lstate, -3); -} - static int set_server(lua_State *lstate) { - if (!check_table(lstate)) + if (!sj3lua_check_table(lstate)) return 0; lua2sj_boolean(lstate, "daemon", 1, FORKFLAG, &daemon_enable); lua2sj_integer(lstate, "max_client", 1, MAXCLIENTNUM, &max_client); @@ -237,7 +144,7 @@ get_server(lua_State *lstate) static int set_inet(lua_State *lstate) { - if (!check_table(lstate)) + if (!sj3lua_check_table(lstate)) return 0; lua2sj_boolean(lstate, "enable", 1, 0, &inet_enable); lua2sj_string(lstate, "address_family", 1, ADDRESSFAMILY, inet_address_family, sizeof(inet_address_family)); @@ -260,7 +167,7 @@ get_inet(lua_State *lstate) static int set_domain(lua_State *lstate) { - if (!check_table(lstate)) + if (!sj3lua_check_table(lstate)) return 0; lua2sj_string(lstate, "socket_name", 1, SOCKETNAME, domain_socket_name, sizeof(domain_socket_name)); @@ -281,7 +188,7 @@ append_readdict(lua_State *lstate) char dict[MAXPATHLEN]; char passwd[BUFSIZ]; - if (!check_table(lstate)) + if (!sj3lua_check_table(lstate)) return 0; lua2sj_string(lstate, "file", 1, NULL, dict, sizeof(dict)); lua2sj_string(lstate, "passwd", 1, NULL, passwd, sizeof(passwd)); @@ -300,7 +207,7 @@ append_opendict(lua_State *lstate) char dict[MAXPATHLEN]; char passwd[BUFSIZ]; - if (!check_table(lstate)) + if (!sj3lua_check_table(lstate)) return 0; lua2sj_string(lstate, "file", 1, NULL, dict, sizeof(dict)); lua2sj_string(lstate, "passwd", 1, NULL, passwd, sizeof(passwd)); @@ -316,7 +223,7 @@ append_opendict(lua_State *lstate) static int set_log(lua_State *lstate) { - if (!check_table(lstate)) + if (!sj3lua_check_table(lstate)) return 0; lua2sj_string(lstate, "file", 1, LOGOUTFILE, log_file, sizeof(log_file)); @@ -334,7 +241,7 @@ get_log(lua_State *lstate) static int set_debug(lua_State *lstate) { - if (!check_table(lstate)) + if (!sj3lua_check_table(lstate)) return 0; lua2sj_string(lstate, "file", 1, DEBUGOUTFILE, debug_file, sizeof(debug_file)); lua2sj_integer(lstate, "level", 1, DEBUGLEVEL, &debug_level); @@ -354,7 +261,7 @@ get_debug(lua_State *lstate) static int set_error(lua_State *lstate) { - if (!check_table(lstate)) + if (!sj3lua_check_table(lstate)) return 0; lua2sj_string(lstate, "file", 1, ERROROUTFILE, error_file, sizeof(error_file)); @@ -367,7 +274,7 @@ append_allowuser(lua_State *lstate) char username[BUFSIZ]; char hostname[MAXHOSTNAMELEN]; - if (!check_table(lstate)) + if (!sj3lua_check_table(lstate)) return 0; lua2sj_string(lstate, "user", 1, NULL, username, sizeof(username)); lua2sj_string(lstate, "host", 1, NULL, hostname, sizeof(hostname));