* packet-lua.h
authorLuis Ontanon <luis.ontanon@gmail.com>
Sun, 29 Jan 2006 03:13:26 +0000 (03:13 -0000)
committerLuis Ontanon <luis.ontanon@gmail.com>
Sun, 29 Jan 2006 03:13:26 +0000 (03:13 -0000)
   * add an isXxx() declaration

* packet-lua.c
   * move the dissectors, handoffs and inits tables to the registry

* lua_proto.c
   * new_protocol is now called Protocol
   * move the dissectors, handoffs and inits tables to the registry

svn path=/trunk/; revision=17117

plugins/lua/lua_proto.c
plugins/lua/packet-lua.c
plugins/lua/packet-lua.h

index 501511df82b39d0dba1ae0fb8273b7efe190ed77..82b742b2acfb2089858d6c024c6fa5f2276633b6 100644 (file)
@@ -840,7 +840,9 @@ static int Proto_set_dissector(lua_State* L) {
     if (lua_isfunction(L,3)) {
         /* insert the dissector into the dissectors table */
        
-        lua_getglobal(L, LUA_DISSECTORS_TABLE);
+        lua_pushstring(L, LUA_DISSECTORS_TABLE);
+        lua_gettable(L, LUA_REGISTRYINDEX);
+
         lua_replace(L, 1);
         lua_pushstring(L,proto->name);
         lua_replace(L, 2);
@@ -867,7 +869,8 @@ static int Proto_set_init(lua_State* L) {
     
     if (lua_isfunction(L,3)) {
         /* insert the dissector into the dissectors table */
-        lua_getglobal(L, LUA_INIT_ROUTINES);
+        lua_pushstring(L, LUA_INIT_ROUTINES);
+        lua_gettable(L, LUA_REGISTRYINDEX);
         lua_replace(L, 1);
         lua_pushstring(L,proto->name);
         lua_replace(L, 2);
@@ -886,7 +889,8 @@ static int Proto_set_handoff(lua_State* L) {
     
     if (lua_isfunction(L,3)) {
         /* insert the dissector into the dissectors table */
-        lua_getglobal(L, LUA_HANDOFF_ROUTINES);
+        lua_pushstring(L, LUA_HANDOFF_ROUTINES);
+        lua_gettable(L, LUA_REGISTRYINDEX);
         lua_replace(L, 1);
         lua_pushstring(L,proto->name);
         lua_replace(L, 2);
@@ -983,7 +987,7 @@ int Proto_register(lua_State* L) {
     lua_pushcfunction(L, Proto_register_postdissector);
     lua_settable(L, LUA_GLOBALSINDEX);
 
-    lua_pushstring(L, "new_protocol");
+    lua_pushstring(L, PROTO);
     lua_pushcfunction(L, Proto_new);
     lua_settable(L, LUA_GLOBALSINDEX);
     
@@ -1133,15 +1137,18 @@ static int DissectorTable_get (lua_State *L) {
 
 static int DissectorTable_add (lua_State *L) {
     DissectorTable dt = checkDissectorTable(L,1);
-    Proto p;
     ftenum_t type;
     Dissector handle;
 
     if (!dt) return 0;
 
-    if(( p = luaL_checkudata(L,3,PROTO) )) {
+    if( isProto(L,3) ) {
+        Proto p;
+        p = toProto(L,3);
         handle = p->handle;
-    } else if (! ( handle  = luaL_checkudata(L,3,DISSECTOR) )) {
+    } else if ( isDissector(L,3) ) {
+        handle = toDissector(L,3);
+    } else {
         luaL_argerror(L,3,"Must be either " PROTO " or " DISSECTOR );
         return 0;
     }
index 71bf1ede36727c8ea15c9ac083a90b2578f6d367..c79f22198f05cd358dc3071484572eccd3b2be58 100644 (file)
@@ -203,7 +203,8 @@ void dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) {
     
     lua_settop(L,0);
 
-    lua_getglobal(L, LUA_DISSECTORS_TABLE);
+    lua_pushstring(L, LUA_DISSECTORS_TABLE);
+    lua_gettable(L, LUA_REGISTRYINDEX);
     
     if (!lua_istable(L, -1)) {
         proto_item* pi = proto_tree_add_text(tree,tvb,0,0,"Lua: either `" LUA_DISSECTORS_TABLE "' does not exist or it is not a table!");
@@ -249,8 +250,9 @@ static void iter_table_and_call(lua_State* LS, const gchar* table_name, lua_CFun
     lua_settop(LS,0);
     
     lua_pushcfunction(LS,error_handler);
-    lua_getglobal(LS, table_name);
-
+    lua_pushstring(LS, table_name);
+    lua_gettable(LS, LUA_REGISTRYINDEX);
+    
     if (!lua_istable(LS, 2)) {
         report_failure("Lua: either `%s' does not exist or it is not a table!\n",table_name);
         lua_close(LS);
@@ -396,18 +398,20 @@ void proto_register_lua(void)
     lua_pushstring(L, "report_failure");
     lua_pushcfunction(L, lua_report_failure);
     lua_settable(L, LUA_GLOBALSINDEX);
-            
+    
+    
+    
     lua_pushstring(L, LUA_HANDOFF_ROUTINES);
     lua_newtable (L);
-    lua_settable(L, LUA_GLOBALSINDEX);
+    lua_settable(L, LUA_REGISTRYINDEX);
     
     lua_pushstring(L, LUA_INIT_ROUTINES);
     lua_newtable (L);
-    lua_settable(L, LUA_GLOBALSINDEX);
+    lua_settable(L, LUA_REGISTRYINDEX);
     
     lua_pushstring(L, LUA_DISSECTORS_TABLE);
     lua_newtable (L);
-    lua_settable(L, LUA_GLOBALSINDEX);
+    lua_settable(L, LUA_REGISTRYINDEX);
     
     lua_pushstring(L, LUA_TAP_PACKET);
     lua_newtable (L);
index aa82898a3401497d17f230a8caf852f143d13d9f..508f79c3eca923f5449ac3ba47a36c753789d577 100644 (file)
@@ -107,7 +107,7 @@ typedef GArray* ProtoFieldArray;
 #define SUBTREE "SubTree"
 typedef int* SubTree;
 
-#define PROTO "Proto"
+#define PROTO "Protocol"
 typedef struct _eth_proto_t* Proto;
 
 #define DISSECTOR_TABLE "DissectorTable"
@@ -158,6 +158,12 @@ typedef struct _eth_tap {
 
 #define NOP
 
+/*
+ * toXxx(L,idx) gets a Xxx from an index (Lua Error if fails)
+ * checkXxx(L,idx) gets a Xxx from an index after calling check_code (No Lua Error if it fails)
+ * pushXxx(L,xxx) pushes an Xxx into the stack
+ * isXxx(L,idx) tests whether we have an Xxx at idx
+ */
 #define LUA_CLASS_DEFINE(C,CN,check_code) \
 C to##C(lua_State* L, int index) { \
     C* v = (C*)lua_touserdata (L, index); \
@@ -175,8 +181,12 @@ C* push##C(lua_State* L, C v) { \
     C* p = lua_newuserdata(L,sizeof(C)); *p = v; \
     luaL_getmetatable(L, CN); lua_setmetatable(L, -2); \
     return p; \
+}\
+gboolean is##C(lua_State* L,int i) { \
+        return (gboolean)(lua_isuserdata(L,i) && luaL_checkudata(L,3,CN)); \
 }
 
+
 extern packet_info* lua_pinfo;
 extern proto_tree* lua_tree;
 extern tvbuff_t* lua_tvb;
@@ -189,7 +199,9 @@ extern gboolean lua_initialized;
 extern C to##C(lua_State* L, int index); \
 extern C check##C(lua_State* L, int index); \
 extern C* push##C(lua_State* L, C v); \
-extern int C##_register(lua_State* L);
+extern int C##_register(lua_State* L); \
+extern gboolean is##C(lua_State* L,int i)
+
 
 LUA_CLASS_DECLARE(Tap,TAP);
 LUA_CLASS_DECLARE(Field,FIELD);
@@ -217,8 +229,4 @@ extern GString* lua_register_all_taps(void);
 extern void lua_prime_all_fields(proto_tree* tree);
 void lua_register_subtrees(void);
 
-#if 0
-#define WARNSTACK(s) {int i; for (i = 1; i <= lua_gettop(L); i++) if (lua_isstring(L,i)) printf("-%s-> %i `%s'\n",s,i , lua_tostring(L,i)); else printf("-%s-> %i %s\n",s,i , lua_typename(L,lua_type(L,i))); }
-#endif
-
 #endif