about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile4
-rw-r--r--src/lcurseslib.c40
-rw-r--r--src/linit.c1
-rw-r--r--src/lua.c1
-rw-r--r--src/lualib.h3
5 files changed, 46 insertions, 3 deletions
diff --git a/src/Makefile b/src/Makefile
index 3ef73bc..560781e 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -26,8 +26,8 @@ LUA_A=	liblua.a
 CORE_O=	lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
 	lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o  \
 	lundump.o lvm.o lzio.o
-LIB_O=	lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
-	lstrlib.o loadlib.o linit.o
+LIB_O=	lauxlib.o lbaselib.o lcurseslib.o ldblib.o liolib.o lmathlib.o \
+	loslib.o ltablib.o lstrlib.o loadlib.o linit.o
 
 LUA_T=	lua
 LUA_O=	lua.o
diff --git a/src/lcurseslib.c b/src/lcurseslib.c
new file mode 100644
index 0000000..d8667fc
--- /dev/null
+++ b/src/lcurseslib.c
@@ -0,0 +1,40 @@
+#include <ncurses.h>
+
+#include "lua.h"
+
+#include "lauxlib.h"
+#include "lualib.h"
+
+
+static int Pcols(lua_State *L) {
+  lua_pushinteger(L, COLS);
+  return 1;
+}
+
+
+static const struct luaL_Reg curseslib [] = {
+  {"cols", Pcols},
+  {NULL, NULL}
+};
+
+
+static void curses_newwin (lua_State *L, WINDOW *nw) {
+  if (nw) {
+    WINDOW **w = lua_newuserdata(L, sizeof(WINDOW*));
+    luaL_getmetatable(L, "meta.window");
+    lua_setmetatable(L, -2);
+    *w = nw;
+  }
+  else {
+    lua_pushliteral(L, "failed to create window");
+    lua_error(L);
+  }
+}
+
+
+LUALIB_API int luaopen_curses (lua_State *L) {
+  luaL_register(L, "curses", curseslib);
+  curses_newwin(L, stdscr);
+  return 1;
+}
+
diff --git a/src/linit.c b/src/linit.c
index c1f90df..6211f39 100644
--- a/src/linit.c
+++ b/src/linit.c
@@ -22,6 +22,7 @@ static const luaL_Reg lualibs[] = {
   {LUA_OSLIBNAME, luaopen_os},
   {LUA_STRLIBNAME, luaopen_string},
   {LUA_MATHLIBNAME, luaopen_math},
+  {LUA_CURSESLIBNAME, luaopen_curses},
   {LUA_DBLIBNAME, luaopen_debug},
   {NULL, NULL}
 };
diff --git a/src/lua.c b/src/lua.c
index 5dcc492..a766e65 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -466,7 +466,6 @@ int main (int argc, char **argv) {
   /* stack: metatable */
   luaL_register(L, NULL, array_methods);  /* register array_methods in metatable */
   luaL_register(L, "array", arraylib_functions);
-//?   luaL_register(L, "curses.window", curses_window_fns);
   initscr();
   echo();
   s.argc = argc;
diff --git a/src/lualib.h b/src/lualib.h
index 469417f..02924dd 100644
--- a/src/lualib.h
+++ b/src/lualib.h
@@ -33,6 +33,9 @@ LUALIB_API int (luaopen_string) (lua_State *L);
 #define LUA_MATHLIBNAME	"math"
 LUALIB_API int (luaopen_math) (lua_State *L);
 
+#define LUA_CURSESLIBNAME	"curses"
+LUALIB_API int (luaopen_curses) (lua_State *L);
+
 #define LUA_DBLIBNAME	"debug"
 LUALIB_API int (luaopen_debug) (lua_State *L);