peter@peterodding.comhttp://peterodding.com/code/lua/apr/
#include "lua_apr.h"
#include <apr_portable.h>
static int apr_was_initialized = 0;
static int mp_regidx = LUA_NOREF;
int luaopen_apr_core(lua_State *L)
{
apr_status_t status;
luaL_Reg functions[] = {
{ "platform_get", lua_apr_platform_get },
{ "version_get", lua_apr_version_get },
{ "os_default_encoding", lua_apr_os_default_encoding },
{ "os_locale_encoding", lua_apr_os_locale_encoding },
{ "type", lua_apr_type },
{ "base64_encode", lua_apr_base64_encode },
{ "base64_decode", lua_apr_base64_decode },
{ "md5_init", lua_apr_md5_init },
{ "md5_encode", lua_apr_md5_encode },
{ "password_get", lua_apr_password_get },
{ "password_validate", lua_apr_password_validate },
{ "sha1_init", lua_apr_sha1_init },
{ "date_parse_http", lua_apr_date_parse_http },
{ "date_parse_rfc", lua_apr_date_parse_rfc },
{ "dbm_open", lua_apr_dbm_open },
{ "dbm_getnames", lua_apr_dbm_getnames },
{ "env_get", lua_apr_env_get },
{ "env_set", lua_apr_env_set },
{ "env_delete", lua_apr_env_delete },
{ "filepath_root", lua_apr_filepath_root },
{ "filepath_parent", lua_apr_filepath_parent },
{ "filepath_name", lua_apr_filepath_name },
{ "filepath_merge", lua_apr_filepath_merge },
{ "filepath_list_split", lua_apr_filepath_list_split },
{ "filepath_list_merge", lua_apr_filepath_list_merge },
{ "filepath_get", lua_apr_filepath_get },
{ "filepath_set", lua_apr_filepath_set },
{ "fnmatch", lua_apr_fnmatch },
{ "fnmatch_test", lua_apr_fnmatch_test },
{ "temp_dir_get", lua_apr_temp_dir_get },
{ "dir_make", lua_apr_dir_make },
{ "dir_make_recursive", lua_apr_dir_make_recursive },
{ "dir_remove", lua_apr_dir_remove },
{ "dir_remove_recursive", lua_apr_dir_remove_recursive },
{ "dir_open", lua_apr_dir_open },
# if APR_MAJOR_VERSION > 1 || (APR_MAJOR_VERSION == 1 && APR_MINOR_VERSION >= 4)
{ "file_link", lua_apr_file_link },
# endif
{ "file_copy", lua_apr_file_copy },
{ "file_append", lua_apr_file_append },
{ "file_rename", lua_apr_file_rename },
{ "file_remove", lua_apr_file_remove },
{ "file_mtime_set", lua_apr_file_mtime_set },
{ "file_attrs_set", lua_apr_file_attrs_set },
{ "file_perms_set", lua_apr_file_perms_set },
{ "stat", lua_apr_stat },
{ "file_open", lua_apr_file_open },
{ "socket_create", lua_apr_socket_create },
{ "hostname_get", lua_apr_hostname_get },
{ "host_to_addr", lua_apr_host_to_addr },
{ "addr_to_host", lua_apr_addr_to_host },
{ "pipe_open_stdin", lua_apr_pipe_open_stdin },
{ "pipe_open_stdout", lua_apr_pipe_open_stdout },
{ "pipe_open_stderr", lua_apr_pipe_open_stderr },
{ "namedpipe_create", lua_apr_namedpipe_create },
{ "pipe_create", lua_apr_pipe_create },
{ "proc_create", lua_apr_proc_create },
{ "proc_detach", lua_apr_proc_detach },
# if APR_HAS_FORK
{ "proc_fork", lua_apr_proc_fork },
# endif
{ "strnatcmp", lua_apr_strnatcmp },
{ "strnatcasecmp", lua_apr_strnatcasecmp },
{ "strfsize", lua_apr_strfsize },
{ "tokenize_to_argv", lua_apr_tokenize_to_argv },
{ "sleep", lua_apr_sleep },
{ "time_now", lua_apr_time_now },
{ "time_explode", lua_apr_time_explode },
{ "time_implode", lua_apr_time_implode },
{ "time_format", lua_apr_time_format },
{ "uri_parse", lua_apr_uri_parse },
{ "uri_unparse", lua_apr_uri_unparse },
{ "uri_port_of_scheme", lua_apr_uri_port_of_scheme },
{ "user_get", lua_apr_user_get },
{ "user_homepath_get", lua_apr_user_homepath_get },
{ "uuid_get", lua_apr_uuid_get },
{ "uuid_format", lua_apr_uuid_format },
{ "uuid_parse", lua_apr_uuid_parse },
{ "xlate", lua_apr_xlate },
{ NULL, NULL }
};
if (!apr_was_initialized) {
if ((status = apr_initialize()) != APR_SUCCESS)
raise_error_status(L, status);
if (atexit(apr_terminate) != 0)
raise_error_message(L, "Lua/APR: Failed to register apr_terminate()");
apr_was_initialized = 1;
}
lua_createtable(L, 0, count(functions));
luaL_register(L, NULL, functions);
lua_pushboolean(L, APR_PROCATTR_USER_SET_REQUIRES_PASSWORD);
lua_setfield(L, -2, "user_set_requires_password");
lua_pushboolean(L, APR_HAVE_IPV6);
lua_setfield(L, -2, "socket_supports_ipv6");
return 1;
}
int lua_apr_platform_get(lua_State *L)
{
# if defined(WIN32)
lua_pushstring(L, "WIN32");
# elif defined(NETWARE)
lua_pushstring(L, "NETWARE");
# elif defined(OS2)
lua_pushstring(L, "OS2");
# else
lua_pushstring(L, "UNIX");
# endif
return 1;
}
http://en.wikipedia.org/wiki/Application_programming_interface
int lua_apr_version_get(lua_State *L)
{
lua_pushstring(L, apr_version_string());
lua_pushstring(L, apu_version_string());
return 2;
}
int lua_apr_os_default_encoding(lua_State *L)
{
lua_pushstring(L, apr_os_default_encoding(to_pool(L)));
return 1;
}
int lua_apr_os_locale_encoding(lua_State *L)
{
lua_pushstring(L, apr_os_locale_encoding(to_pool(L)));
return 1;
}
int lua_apr_type(lua_State *L)
{
lua_apr_objtype *types[] = {
&lua_apr_file_type,
&lua_apr_dir_type,
&lua_apr_socket_type,
&lua_apr_proc_type,
&lua_apr_dbm_type
};
int i;
lua_settop(L, 1);
luaL_checktype(L, 1, LUA_TUSERDATA);
lua_getmetatable(L, 1);
for (i = 0; i < count(types); i++) {
get_metatable(L, types[i]);
if (lua_rawequal(L, 2, 3)) {
lua_pushstring(L, types[i]->friendlyname);
return 1;
}
lua_pop(L, 1);
}
return 0;
}
apr_pool_t *to_pool(lua_State *L)
{
apr_pool_t *memory_pool;
apr_status_t status;
luaL_checkstack(L, 1, "not enough stack space to get memory pool");
if (mp_regidx == LUA_NOREF) {
status = apr_pool_create(&memory_pool, NULL);
if (status != APR_SUCCESS)
raise_error_status(L, status);
lua_pushlightuserdata(L, memory_pool);
mp_regidx = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
lua_rawgeti(L, LUA_REGISTRYINDEX, mp_regidx);
memory_pool = lua_touserdata(L, -1);
apr_pool_clear(memory_pool);
lua_pop(L, 1);
}
return memory_pool;
}
int status_to_message(lua_State *L, apr_status_t status)
{
char message[512];
apr_strerror(status, message, count(message));
lua_pushstring(L, message);
return 1;
}
int push_status(lua_State *L, apr_status_t status)
{
if (status == APR_SUCCESS) {
lua_pushboolean(L, 1);
return 1;
} else {
return push_error_status(L, status);
}
}
int push_error_status(lua_State *L, apr_status_t status)
{
lua_pushnil(L);
status_to_message(L, status);
status_to_name(L, status);
return 3;
}
void *new_object(lua_State *L, lua_apr_objtype *T)
{
void *object;
object = lua_newuserdata(L, T->objsize);
if (object != NULL) {
memset(object, 0, T->objsize);
get_metatable(L, T);
lua_setmetatable(L, -2);
getdefaultenv(L);
lua_setfenv(L, -2);
}
return object;
}
void getdefaultenv(lua_State *L)
{
const char *key = "Lua/APR default environment for userdata";
lua_getfield(L, LUA_REGISTRYINDEX, key);
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, key);
}
}
void *check_object(lua_State *L, int idx, lua_apr_objtype *T)
{
int valid = 0;
get_metatable(L, T);
lua_getmetatable(L, idx);
valid = lua_rawequal(L, -1, -2);
lua_pop(L, 2);
if (valid)
return lua_touserdata(L, idx);
luaL_typerror(L, idx, T->typename);
return NULL;
}
int get_metatable(lua_State *L, lua_apr_objtype *T)
{
luaL_getmetatable(L, T->typename);
if (lua_type(L, -1) != LUA_TTABLE) {
lua_pop(L, 1);
luaL_newmetatable(L, T->typename);
luaL_register(L, NULL, T->metamethods);
lua_newtable(L);
luaL_register(L, NULL, T->methods);
lua_setfield(L, -2, "__index");
}
return 1;
}