diff options
-rw-r--r-- | mbbsd/bbs.c | 7 | ||||
-rw-r--r-- | mbbsd/bbslua.c | 108 | ||||
-rw-r--r-- | mbbsd/bbsluaext.c | 83 |
3 files changed, 189 insertions, 9 deletions
diff --git a/mbbsd/bbs.c b/mbbsd/bbs.c index 0bc8f1fe..de049be2 100644 --- a/mbbsd/bbs.c +++ b/mbbsd/bbs.c @@ -2240,7 +2240,7 @@ cite_post(int ent, const fileheader_t * fhdr, const char *direct) int edit_title(int ent, fileheader_t * fhdr, const char *direct) { - char genbuf[200]; + char genbuf[200] = ""; fileheader_t tmpfhdr = *fhdr; int dirty = 0; int allow = 0; @@ -2257,7 +2257,10 @@ edit_title(int ent, fileheader_t * fhdr, const char *direct) if (!allow) return DONOTHING; - if (getdata(b_lines - 1, 0, "¼ÐÃD¡G", genbuf, TTLEN, DOECHO)) { + if (fhdr && fhdr->title[0]) + strlcpy(genbuf, fhdr->title, TTLEN+1); + + if (getdata_buf(b_lines - 1, 0, "¼ÐÃD¡G", genbuf, TTLEN, DOECHO)) { strlcpy(tmpfhdr.title, genbuf, sizeof(tmpfhdr.title)); dirty++; } diff --git a/mbbsd/bbslua.c b/mbbsd/bbslua.c index 93975ad8..cac4a9d1 100644 --- a/mbbsd/bbslua.c +++ b/mbbsd/bbslua.c @@ -1,5 +1,5 @@ ////////////////////////////////////////////////////////////////////////// -// BBS Lua Project +// BBS-Lua Project // // Author: Hung-Te Lin(piaip), Jan. 2008. // <piaip@csie.ntu.edu.tw> @@ -19,6 +19,7 @@ // 10.provide local storage // 11.provide money // 12.os.date(), os.exit(), abort(), os.time() +// 13.memory free issue in C library level? // // BBSLUA 2.0 // 1. 2 people communication @@ -48,12 +49,14 @@ ////////////////////////////////////////////////////////////////////////// #define BLAPI_PROTO int +#define BLCONF_BREAK_KEY Ctrl('C') #define BLCONF_EXEC_COUNT (5000) +#define BLCONF_PEEK_TIME (0.01) #define BLCONF_KBHIT_TMIN (0.05f) #define BLCONF_KBHIT_TMAX (60*10) -#define BLCONF_PEEK_TIME (0.01) -#define BLCONF_BREAK_KEY Ctrl('C') -#define BLCONF_CLOCK_SEC (1000000L) +#define BLCONF_SLEEP_TMIN (BLCONF_PEEK_TIME) +#define BLCONF_SLEEP_TMAX (BLCONF_KBHIT_TMAX) +#define BLCONF_U_SECOND (1000000L) ////////////////////////////////////////////////////////////////////////// // GLOBAL VARIABLES @@ -61,6 +64,25 @@ static int abortBBSLua = 0; ////////////////////////////////////////////////////////////////////////// +// UTILITIES +////////////////////////////////////////////////////////////////////////// + +static void +bl_double2tv(double d, struct timeval *tv) +{ + tv->tv_sec = d; + tv->tv_usec = (d - tv->tv_sec) * BLCONF_U_SECOND; +} + +static double +bl_tv2double(const struct timeval *tv) +{ + double d = tv->tv_sec; + d += tv->tv_usec / (double)BLCONF_U_SECOND; + return d; +} + +////////////////////////////////////////////////////////////////////////// // BBSLUA API IMPLEMENTATION ////////////////////////////////////////////////////////////////////////// @@ -91,7 +113,7 @@ bl_move(lua_State* L) y = lua_tointeger(L, 1); if (n > 1) x = lua_tointeger(L, 2); - move(y, x); + move_ansi(y, x); return 0; } @@ -254,6 +276,7 @@ bl_kbhit(lua_State *L) if (f < BLCONF_KBHIT_TMIN) f = BLCONF_KBHIT_TMIN; if (f > BLCONF_KBHIT_TMAX) f = BLCONF_KBHIT_TMAX; + refresh(); if (num_in_buf() || wait_input(f, 0)) lua_pushboolean(L, 1); else @@ -262,6 +285,68 @@ bl_kbhit(lua_State *L) } BLAPI_PROTO +bl_kbreset(lua_State *L) +{ + // peek input queue first! + if (peek_input(BLCONF_PEEK_TIME, BLCONF_BREAK_KEY)) + { + drop_input(); + abortBBSLua = 1; + return lua_yield(L, 0); + } + + drop_input(); + return 0; +} + +BLAPI_PROTO +bl_sleep(lua_State *L) +{ + int n = lua_gettop(L); + double us = 0, nus = 0; + struct timeval tp, tdest; + struct timezone tz; + + if (n > 0) + us = lua_tonumber(L, 1); + if (us < BLCONF_SLEEP_TMIN) + us = BLCONF_SLEEP_TMIN; + if (us > BLCONF_SLEEP_TMAX) + us = BLCONF_SLEEP_TMAX; + nus = us; + + refresh(); + memset(&tz, 0, sizeof(tz)); + gettimeofday(&tp, &tz); + + // nus is the destination time + nus = bl_tv2double(&tp) + us; + bl_double2tv(nus, &tdest); + + // use peek_input + while ( (tp.tv_sec < tdest.tv_sec) || + ((tp.tv_sec == tdest.tv_sec) && (tp.tv_usec < tdest.tv_usec))) + { + // calculate new peek time + us = nus - bl_tv2double(&tp); + + // check if input key is system break key. + if (peek_input(us, BLCONF_BREAK_KEY)) + { + drop_input(); + abortBBSLua = 1; + return lua_yield(L, 0); + } + + // check time + gettimeofday(&tp, &tz); + } + + return 0; +} + + +BLAPI_PROTO bl_pause(lua_State* L) { int n = lua_gettop(L); @@ -352,8 +437,7 @@ bl_clock(lua_State *L) double d = 0; memset(&tz, 0, sizeof(tz)); gettimeofday(&tp, &tz); - d = tp.tv_sec; - d += tp.tv_usec / (double)(BLCONF_CLOCK_SEC); + d = bl_tv2double(&tp); lua_pushnumber(L, d); return 1; } @@ -388,6 +472,7 @@ static const struct luaL_reg lib_bbslua [] = { { "getdata", bl_getstr }, { "getstr", bl_getstr }, { "kbhit", bl_kbhit }, + { "kbreset", bl_kbreset }, /* BBS utilities */ { "pause", bl_pause }, { "title", bl_title }, @@ -397,6 +482,7 @@ static const struct luaL_reg lib_bbslua [] = { { "now", bl_time }, { "clock", bl_clock }, { "ctime", bl_ctime }, + { "sleep", bl_sleep }, /* ANSI helpers */ { "ANSI_COLOR", bl_ansi_color }, { "color", bl_attrset }, @@ -404,7 +490,11 @@ static const struct luaL_reg lib_bbslua [] = { { NULL, NULL}, }; +// non-standard modules in bbsluaext.c +LUALIB_API int luaopen_bit (lua_State *L); + static const luaL_Reg mylualibs[] = { + // standard modules {"", luaopen_base}, // {LUA_LOADLIBNAME, luaopen_package}, @@ -414,6 +504,9 @@ static const luaL_Reg mylualibs[] = { {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, // {LUA_DBLIBNAME, luaopen_debug}, + + // bbslua-ext modules + {"bit", luaopen_bit}, {NULL, NULL} }; @@ -636,6 +729,7 @@ bbslua(const char *fpath) } lua_close(L); + drop_input(); grayout(0, b_lines, GRAYOUT_DARK); move(b_lines, 0); clrtoeol(); diff --git a/mbbsd/bbsluaext.c b/mbbsd/bbsluaext.c new file mode 100644 index 00000000..ee3521ea --- /dev/null +++ b/mbbsd/bbsluaext.c @@ -0,0 +1,83 @@ +/* This file contains non-standard modules which + * are fundamental in BBSLua framework. + */ + +/* Bitwise operations library */ +/* (c) Reuben Thomas 2000-2007 */ +/* + bitlib release 24 + ----------------- + by Reuben Thomas <rrt@sc3d.org> + http://luaforge.net/projects/bitlib + + +bitlib is a C library for Lua 5.x that provides bitwise operations. It +is copyright Reuben Thomas 2000-2007, and is released under the MIT +license, like Lua (see http://www.lua.org/copyright.html; it's +basically the same as the BSD license). There is no warranty. + +Please report bugs and make suggestions to the email address above, or +use the LuaForge trackers. + +Thanks to John Passaniti for his bitwise operations library, some of +whose ideas I used, to Shmuel Zeigerman for the test suite, to +Thatcher Ulrich for portability fixes, and to John Stiles for a bug +fix. +*/ + +#include <inttypes.h> +#include <lauxlib.h> +#include <lua.h> + +typedef int32_t Integer; +typedef uint32_t UInteger; + +#define checkUInteger(L, n) ((UInteger)luaL_checknumber((L), (n))) + +#define TDYADIC(name, op, type1, type2) \ + static int bit_ ## name(lua_State* L) { \ + lua_pushnumber(L, (Integer)((type1)checkUInteger(L, 1) op (type2)checkUInteger(L, 2))); \ + return 1; \ + } + +#define MONADIC(name, op, type) \ + static int bit_ ## name(lua_State* L) { \ + lua_pushnumber(L, (Integer)(op (type)checkUInteger(L, 1))); \ + return 1; \ + } + +#define VARIADIC(name, op, type) \ + static int bit_ ## name(lua_State *L) { \ + int n = lua_gettop(L), i; \ + Integer w = (type)checkUInteger(L, 1); \ + for (i = 2; i <= n; i++) \ + w op (type)checkUInteger(L, i); \ + lua_pushnumber(L, (Integer)w); \ + return 1; \ + } + +MONADIC(cast, +, Integer) +MONADIC(bnot, ~, Integer) +VARIADIC(band, &=, Integer) +VARIADIC(bor, |=, Integer) +VARIADIC(bxor, ^=, Integer) +TDYADIC(lshift, <<, Integer, UInteger) +TDYADIC(rshift, >>, UInteger, UInteger) +TDYADIC(arshift, >>, Integer, UInteger) + +static const struct luaL_reg bitlib[] = { + {"cast", bit_cast}, + {"bnot", bit_bnot}, + {"band", bit_band}, + {"bor", bit_bor}, + {"bxor", bit_bxor}, + {"lshift", bit_lshift}, + {"rshift", bit_rshift}, + {"arshift", bit_arshift}, + {NULL, NULL} +}; + +LUALIB_API int luaopen_bit (lua_State *L) { + luaL_openlib(L, "bit", bitlib, 0); + return 1; +} |