reminder on how to enable debugging in luaixp
[wmiirc-lua.git] / luaixp / lixp_main.c
blob263dfcf3cbc7a84cef9147691ae3b81459b9c8fb
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <time.h>
7 #include <signal.h>
9 #include <ixp.h>
11 #include <lua.h>
12 #include <lauxlib.h>
14 #include "lixp_debug.h"
15 #include "lixp_util.h"
16 #include "lixp_instance.h"
19 /* ------------------------------------------------------------------------
20 * lua: x = ixp.new("unix!/tmp/ns.bart.:0/wmii") -- create a new ixp object
22 static int l_new (lua_State *L)
24 const char *adr;
25 IxpClient *cli;
26 struct ixp *ixp;
28 adr = luaL_checkstring (L, 1);
30 DBGF("** ixp.new ([%s]) **\n", adr);
32 cli = ixp_mount(adr);
33 if (!cli)
34 return lixp_pusherror (L, "could not open ixp connection");
36 ixp = (struct ixp*)lua_newuserdata(L, sizeof (struct ixp));
38 luaL_getmetatable (L, L_IXP_MT);
39 lua_setmetatable (L, -2);
41 ixp->address = strdup (adr);
42 ixp->client = cli;
44 return 1;
47 static int l_ixp_gc (lua_State *L)
49 struct ixp *ixp = lixp_checkixp (L, 1);
51 DBGF("** ixp:__gc (%p [%s]) **\n", ixp, ixp->address);
53 ixp_unmount (ixp->client);
54 free ((char*)ixp->address);
56 return 0;
59 /* ------------------------------------------------------------------------
60 * the class method table
62 static const luaL_reg class_table[] =
64 { "new", l_new },
66 { NULL, NULL },
69 /* ------------------------------------------------------------------------
70 * the instance method table
72 static const luaL_reg instance_table[] =
74 { "__tostring", l_ixp_tostring },
75 { "__gc", l_ixp_gc },
77 { "write", l_ixp_write },
78 { "read", l_ixp_read },
80 { "create", l_ixp_create },
81 { "remove", l_ixp_remove },
83 { "iread", l_ixp_iread },
84 { "idir", l_ixp_idir },
86 { "stat", l_ixp_stat },
88 { NULL, NULL },
91 /* ------------------------------------------------------------------------
92 * the class metatable
94 static int lixp_init_ixp_class (lua_State *L)
96 luaL_newmetatable(L, L_IXP_MT);
98 // setup the __index and __gc field
99 lua_pushstring (L, "__index");
100 lua_pushvalue (L, -2); // pushes the new metatable
101 lua_settable (L, -3); // metatable.__index = metatable
103 luaL_openlib (L, NULL, instance_table, 0);
104 luaL_openlib (L, "ixp", class_table, 0);
106 #if 0
107 luaL_register (L, MYNAME, R);
108 lua_pushliteral (L, "version");
109 lua_pushliteral (L, MYVERSION);
110 lua_settable (L, -3);
111 #endif
112 return 1;
115 /* ------------------------------------------------------------------------
116 * library entry
118 LUALIB_API int luaopen_ixp (lua_State *L)
120 lixp_init_iread_mt (L);
121 lixp_init_idir_mt (L);
123 return lixp_init_ixp_class (L);