summaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2010-06-21 13:21:47 +0800
committerPeng Wu <alexepico@gmail.com>2010-06-21 13:21:47 +0800
commit5f70f6792d22758e37f2875db716ad5a2fefb6da (patch)
treef8427c56a99df3ffd48f57dce377978065760d6b /lua
parent0f0a0be50b9946429b06357d9f729bbfc358c109 (diff)
downloadibus-libpinyin-5f70f6792d22758e37f2875db716ad5a2fefb6da.tar.gz
ibus-libpinyin-5f70f6792d22758e37f2875db716ad5a2fefb6da.tar.xz
ibus-libpinyin-5f70f6792d22758e37f2875db716ad5a2fefb6da.zip
add stripped oslib for lua ext.
Diffstat (limited to 'lua')
-rw-r--r--lua/Makefile.am1
-rw-r--r--lua/lmyoslib.c163
-rw-r--r--lua/lua-ext-console.c9
-rw-r--r--lua/lua-plugin-init.c6
-rw-r--r--lua/lua-plugin.c4
-rw-r--r--lua/lua-plugin.h2
6 files changed, 176 insertions, 9 deletions
diff --git a/lua/Makefile.am b/lua/Makefile.am
index cc938f3..7ac6fda 100644
--- a/lua/Makefile.am
+++ b/lua/Makefile.am
@@ -26,6 +26,7 @@ libpylua_la_SOURCES = \
lua-plugin.h \
lua-plugin.c \
lua-plugin-init.c \
+ lmyoslib.c \
$(NULL)
libpylua_la_CFLAGS = \
diff --git a/lua/lmyoslib.c b/lua/lmyoslib.c
new file mode 100644
index 0000000..65da1f4
--- /dev/null
+++ b/lua/lmyoslib.c
@@ -0,0 +1,163 @@
+/*
+** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
+** Standard Operating System library
+** See Copyright Notice in lua.h
+*/
+
+
+#include <errno.h>
+#include <locale.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#define loslib_c
+#define LUA_LIB
+
+#include "lua.h"
+
+#include "lauxlib.h"
+#include "lualib.h"
+
+
+
+/*
+** {======================================================
+** Time/Date operations
+** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
+** wday=%w+1, yday=%j, isdst=? }
+** =======================================================
+*/
+
+static void setfield (lua_State *L, const char *key, int value) {
+ lua_pushinteger(L, value);
+ lua_setfield(L, -2, key);
+}
+
+static void setboolfield (lua_State *L, const char *key, int value) {
+ if (value < 0) /* undefined? */
+ return; /* does not set field */
+ lua_pushboolean(L, value);
+ lua_setfield(L, -2, key);
+}
+
+static int getboolfield (lua_State *L, const char *key) {
+ int res;
+ lua_getfield(L, -1, key);
+ res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
+ lua_pop(L, 1);
+ return res;
+}
+
+
+static int getfield (lua_State *L, const char *key, int d) {
+ int res;
+ lua_getfield(L, -1, key);
+ if (lua_isnumber(L, -1))
+ res = (int)lua_tointeger(L, -1);
+ else {
+ if (d < 0)
+ return luaL_error(L, "field " LUA_QS " missing in date table", key);
+ res = d;
+ }
+ lua_pop(L, 1);
+ return res;
+}
+
+
+static int os_date (lua_State *L) {
+ const char *s = luaL_optstring(L, 1, "%c");
+ time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
+ struct tm *stm;
+ if (*s == '!') { /* UTC? */
+ stm = gmtime(&t);
+ s++; /* skip `!' */
+ }
+ else
+ stm = localtime(&t);
+ if (stm == NULL) /* invalid date? */
+ lua_pushnil(L);
+ else if (strcmp(s, "*t") == 0) {
+ lua_createtable(L, 0, 9); /* 9 = number of fields */
+ setfield(L, "sec", stm->tm_sec);
+ setfield(L, "min", stm->tm_min);
+ setfield(L, "hour", stm->tm_hour);
+ setfield(L, "day", stm->tm_mday);
+ setfield(L, "month", stm->tm_mon+1);
+ setfield(L, "year", stm->tm_year+1900);
+ setfield(L, "wday", stm->tm_wday+1);
+ setfield(L, "yday", stm->tm_yday+1);
+ setboolfield(L, "isdst", stm->tm_isdst);
+ }
+ else {
+ char cc[3];
+ luaL_Buffer b;
+ cc[0] = '%'; cc[2] = '\0';
+ luaL_buffinit(L, &b);
+ for (; *s; s++) {
+ if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
+ luaL_addchar(&b, *s);
+ else {
+ size_t reslen;
+ char buff[200]; /* should be big enough for any conversion result */
+ cc[1] = *(++s);
+ reslen = strftime(buff, sizeof(buff), cc, stm);
+ luaL_addlstring(&b, buff, reslen);
+ }
+ }
+ luaL_pushresult(&b);
+ }
+ return 1;
+}
+
+
+static int os_time (lua_State *L) {
+ time_t t;
+ if (lua_isnoneornil(L, 1)) /* called without args? */
+ t = time(NULL); /* get current time */
+ else {
+ struct tm ts;
+ luaL_checktype(L, 1, LUA_TTABLE);
+ lua_settop(L, 1); /* make sure table is at the top */
+ ts.tm_sec = getfield(L, "sec", 0);
+ ts.tm_min = getfield(L, "min", 0);
+ ts.tm_hour = getfield(L, "hour", 12);
+ ts.tm_mday = getfield(L, "day", -1);
+ ts.tm_mon = getfield(L, "month", -1) - 1;
+ ts.tm_year = getfield(L, "year", -1) - 1900;
+ ts.tm_isdst = getboolfield(L, "isdst");
+ t = mktime(&ts);
+ }
+ if (t == (time_t)(-1))
+ lua_pushnil(L);
+ else
+ lua_pushnumber(L, (lua_Number)t);
+ return 1;
+}
+
+
+static int os_difftime (lua_State *L) {
+ lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
+ (time_t)(luaL_optnumber(L, 2, 0))));
+ return 1;
+}
+
+/* }====================================================== */
+
+
+static const luaL_Reg syslib[] = {
+ {"date", os_date},
+ {"difftime", os_difftime},
+ {"time", os_time},
+ {NULL, NULL}
+};
+
+/* }====================================================== */
+
+
+
+LUALIB_API int luaopen_myos (lua_State *L) {
+ luaL_register(L, LUA_OSLIBNAME, syslib);
+ return 1;
+}
+
diff --git a/lua/lua-ext-console.c b/lua/lua-ext-console.c
index 352f2af..a02b55a 100644
--- a/lua/lua-ext-console.c
+++ b/lua/lua-ext-console.c
@@ -44,23 +44,24 @@ int print_lua_call_result(IBusEnginePlugin * plugin, size_t num){
for ( i = 0; i < results->len; ++i) {
const lua_command_candidate_t * result = g_array_index(results, const lua_command_candidate_t *, i);
if (result->content)
- printf("%d.%s >\t", i, result->content);
+ printf("%d.%s >\t", (int)i, result->content);
else{
- printf("%d. %s [%s]\t", i, result->suggest, result->help);
+ printf("%d. %s [%s]\t", (int)i, result->suggest, result->help);
}
}
printf("\n");
}
+ return 0;
}
int do_lua_call(IBusEnginePlugin * plugin, const char * command_name, const char * argument){
const lua_command_t * command;
size_t num;
- g_return_if_fail(2 == strlen(command_name));
+ g_return_val_if_fail(2 == strlen(command_name), 2);
command = ibus_engine_plugin_lookup_command(plugin, command_name);
if ( NULL == command) {
- fprintf(stderr, "command %s doesn't exist.\n", command);
+ fprintf(stderr, "command %s doesn't exist.\n", command_name);
return 1;
}
diff --git a/lua/lua-plugin-init.c b/lua/lua-plugin-init.c
index d381308..901f62e 100644
--- a/lua/lua-plugin-init.c
+++ b/lua/lua-plugin-init.c
@@ -12,7 +12,7 @@ static const luaL_Reg lualibs[] = {
{"", luaopen_base},
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
- {LUA_OSLIBNAME, luaopen_os},
+ {LUA_OSLIBNAME, luaopen_myos},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_IMELIBNAME, luaopen_ime},
@@ -202,7 +202,7 @@ static int ime_register_trigger(lua_State * L){
for ( i = 0; i < num; ++i) {
lua_pushinteger(L, i + 1);
lua_gettable(L, 3);
- fprintf(stderr, "%d:%s\t", i + 1, lua_tostring(L, -1));
+ fprintf(stderr, "%d:%s\t", (int)i + 1, lua_tostring(L, -1));
lua_pop(L, 1);
}
fprintf(stderr, "\n");
@@ -213,7 +213,7 @@ static int ime_register_trigger(lua_State * L){
for ( i = 0; i < num; ++i) {
lua_pushinteger(L, i + 1);
lua_gettable(L, 4);
- fprintf(stderr, "%d:%s\t", i + 1, lua_tostring(L, -1));
+ fprintf(stderr, "%d:%s\t", (int) i + 1, lua_tostring(L, -1));
}
fprintf(stderr, "\n");
diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c
index 7d16bb4..e2c68f2 100644
--- a/lua/lua-plugin.c
+++ b/lua/lua-plugin.c
@@ -148,9 +148,9 @@ gboolean ibus_engine_plugin_add_command(IBusEnginePlugin * plugin, lua_command_t
lua_command_t new_command;
lua_command_clone(command, &new_command);
- g_array_append_val(priv->lua_commands, new_command);
+ g_array_append_val(lua_commands, new_command);
/* Note: need to improve speed here? */
- g_array_sort(priv->lua_commands, compare_command);
+ g_array_sort(lua_commands, compare_command);
return TRUE;
}
diff --git a/lua/lua-plugin.h b/lua/lua-plugin.h
index 86e5dee..11c024f 100644
--- a/lua/lua-plugin.h
+++ b/lua/lua-plugin.h
@@ -5,6 +5,8 @@
#define LUA_IMELIBNAME "ime"
LUALIB_API int (luaopen_ime) (lua_State * L);
+LUALIB_API int (luaopen_myos) (lua_State * L);
+
#define LUA_IMELIB_CONTEXT "__context"
typedef struct{