diff options
author | Luis Ontanon <luis.ontanon@gmail.com> | 2006-02-21 21:52:28 +0000 |
---|---|---|
committer | Luis Ontanon <luis.ontanon@gmail.com> | 2006-02-21 21:52:28 +0000 |
commit | 3db1ff91a7ad3824aea3b84c3e2be8c96e1efac5 (patch) | |
tree | 856cac1cde666b5a8d4fa8293b8da246fff884f7 | |
parent | f459143fb4df3788b031fc09d78d3b09aebcd5b1 (diff) |
- rename all the c files
- have elua_makereg.pl generating the declaration and registration Macros for each module
- start of elua_makedoc.pl that should generate the Reference Manual from the code.
svn path=/trunk/; revision=17362
-rw-r--r-- | plugins/lua/Makefile.am | 34 | ||||
-rw-r--r-- | plugins/lua/elua.c (renamed from plugins/lua/packet-lua.c) | 110 | ||||
-rw-r--r-- | plugins/lua/elua.h (renamed from plugins/lua/packet-lua.h) | 144 | ||||
-rw-r--r-- | plugins/lua/elua_dumper.c (renamed from plugins/lua/lua_dumper.c) | 18 | ||||
-rw-r--r-- | plugins/lua/elua_gui.c (renamed from plugins/lua/lua_gui.c) | 138 | ||||
-rw-r--r-- | plugins/lua/elua_makedoc.pl | 247 | ||||
-rw-r--r-- | plugins/lua/elua_makereg.pl | 60 | ||||
-rw-r--r-- | plugins/lua/elua_pinfo.c (renamed from plugins/lua/lua_pinfo.c) | 67 | ||||
-rw-r--r-- | plugins/lua/elua_plugin.c (renamed from plugins/lua/lua_plugin.c) | 0 | ||||
-rw-r--r-- | plugins/lua/elua_proto.c (renamed from plugins/lua/lua_proto.c) | 179 | ||||
-rw-r--r-- | plugins/lua/elua_tap.c (renamed from plugins/lua/lua_tap.c) | 35 | ||||
-rw-r--r-- | plugins/lua/elua_tree.c (renamed from plugins/lua/lua_tree.c) | 23 | ||||
-rw-r--r-- | plugins/lua/elua_tvb.c (renamed from plugins/lua/lua_tvb.c) | 62 |
13 files changed, 705 insertions, 412 deletions
diff --git a/plugins/lua/Makefile.am b/plugins/lua/Makefile.am index c692bb4d23..d628126837 100644 --- a/plugins/lua/Makefile.am +++ b/plugins/lua/Makefile.am @@ -27,17 +27,20 @@ plugindir = @plugindir@ plugin_LTLIBRARIES = lua.la +lua_modules = \ + elua_tvb.c \ + elua_proto.c \ + elua_tree.c \ + elua_pinfo.c \ + elua_tap.c \ + elua_gui.c \ + elua_dumper.c + lua_la_SOURCES = \ - lua_tvb.c \ - lua_proto.c \ - lua_tree.c \ - lua_pinfo.c \ - lua_tap.c \ - lua_gui.c \ - lua_dumper.c \ - packet-lua.c \ - packet-lua.h \ - lua_plugin.c + $(lua_modules) \ + elua.c \ + elua.h \ + elua_plugin.c lua_la_LDFLAGS = -module -avoid-version lua_la_LIBADD = @PLUGIN_LIBS@ @LUA_LIBS@ @@ -57,7 +60,16 @@ MAINTAINERCLEANFILES = \ Makefile.in EXTRA_DIST = \ - dummy + elua_register.h \ + elua_makereg.pl + +packet-lua.h: elua_register.h + +elua_register.h: elua_makereg.pl $(lua_modules) + $(PERL) elua_makereg.pl $(lua_modules) > elua_register.h + +doc: $(lua_modules) + $(PERL) elua_makedoc.pl $^ dummy: touch dummy diff --git a/plugins/lua/packet-lua.c b/plugins/lua/elua.c index 051182d999..1c27c684ce 100644 --- a/plugins/lua/packet-lua.c +++ b/plugins/lua/elua.c @@ -26,7 +26,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "packet-lua.h" +#include "elua.h" #include <epan/nstime.h> #include <math.h> #include <epan/expert.h> @@ -55,7 +55,7 @@ const gchar* lua_shiftstring(lua_State* L, int i) { } } -static int lua_format_date(lua_State* LS) { +static int elua_format_date(lua_State* LS) { lua_Number time = luaL_checknumber(LS,1); nstime_t then; gchar* str; @@ -68,7 +68,7 @@ static int lua_format_date(lua_State* LS) { return 1; } -static int lua_format_time(lua_State* LS) { +static int elua_format_time(lua_State* LS) { lua_Number time = luaL_checknumber(LS,1); nstime_t then; gchar* str; @@ -81,24 +81,24 @@ static int lua_format_time(lua_State* LS) { return 1; } -static int lua_report_failure(lua_State* LS) { +static int elua_report_failure(lua_State* LS) { const gchar* s = luaL_checkstring(LS,1); report_failure("%s",s); return 0; } -static int lua_not_register_menu(lua_State* L) { +static int elua_not_register_menu(lua_State* L) { luaL_error(L,"too late to register a menu"); return 0; } -static int lua_not_print(lua_State* L) { +static int elua_not_print(lua_State* L) { luaL_error(L,"do not use print use either a TextWindow or critical(),\n" "warn(), message(), info() or debug()"); return 0; } -int lua_log(lua_State* L, GLogLevelFlags log_level) { +int elua_log(lua_State* L, GLogLevelFlags log_level) { GString* str = g_string_new(""); int n = lua_gettop(L); /* number of arguments */ int i; @@ -125,11 +125,11 @@ int lua_log(lua_State* L, GLogLevelFlags log_level) { return 0; } -int lua_critical( lua_State* L ) { lua_log(L,G_LOG_LEVEL_CRITICAL); return 0; } -int lua_warn( lua_State* L ) { lua_log(L,G_LOG_LEVEL_WARNING); return 0; } -int lua_message( lua_State* L ) { lua_log(L,G_LOG_LEVEL_MESSAGE); return 0; } -int lua_info( lua_State* L ) { lua_log(L,G_LOG_LEVEL_INFO); return 0; } -int lua_debug( lua_State* L ) { lua_log(L,G_LOG_LEVEL_DEBUG); return 0; } +int elua_critical( lua_State* L ) { elua_log(L,G_LOG_LEVEL_CRITICAL); return 0; } +int elua_warn( lua_State* L ) { elua_log(L,G_LOG_LEVEL_WARNING); return 0; } +int elua_message( lua_State* L ) { elua_log(L,G_LOG_LEVEL_MESSAGE); return 0; } +int elua_info( lua_State* L ) { elua_log(L,G_LOG_LEVEL_INFO); return 0; } +int elua_debug( lua_State* L ) { elua_log(L,G_LOG_LEVEL_DEBUG); return 0; } void dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) { @@ -307,82 +307,30 @@ void register_lua(void) { G_LOG_LEVEL_DEBUG, ops ? ops->logger : basic_logger, NULL); - INIT_LUA(L); + ELUA_INIT(L); /* load ethereal's API */ - ProtoField_register(L); - ProtoFieldArray_register(L); - SubTree_register(L); - ByteArray_register(L); - Tvb_register(L); - TvbRange_register(L); - Proto_register(L); - Column_register(L); - Pinfo_register(L); - ProtoTree_register(L); - ProtoItem_register(L); - Dissector_register(L); - DissectorTable_register(L); - Field_register(L); - Columns_register(L); - Tap_register(L); - Address_register(L); - TextWindow_register(L); - PseudoHeader_register(L); - Dumper_register(L); - + ELUA_REGISTER_CLASSES(L); /* print has been changed to yield an error if used */ lua_pushstring(L, "print"); - lua_pushcfunction(L, lua_not_print); + lua_pushcfunction(L, elua_not_print); lua_settable(L, LUA_GLOBALSINDEX); /* logger functions */ - lua_pushstring(L, "critical"); - lua_pushcfunction(L, lua_critical); - lua_settable(L, LUA_GLOBALSINDEX); - - lua_pushstring(L, "warn"); - lua_pushcfunction(L, lua_warn); - lua_settable(L, LUA_GLOBALSINDEX); - - lua_pushstring(L, "message"); - lua_pushcfunction(L, lua_message); - lua_settable(L, LUA_GLOBALSINDEX); - - lua_pushstring(L, "info"); - lua_pushcfunction(L, lua_info); - lua_settable(L, LUA_GLOBALSINDEX); - - lua_pushstring(L, "debug"); - lua_pushcfunction(L, lua_debug); - lua_settable(L, LUA_GLOBALSINDEX); - - - /* functions not registered by any other module */ - lua_pushstring(L, "format_date"); - lua_pushcfunction(L, lua_format_date); - lua_settable(L, LUA_GLOBALSINDEX); - - lua_pushstring(L, "format_time"); - lua_pushcfunction(L, lua_format_time); - lua_settable(L, LUA_GLOBALSINDEX); - - lua_pushstring(L, "report_failure"); - lua_pushcfunction(L, lua_report_failure); - lua_settable(L, LUA_GLOBALSINDEX); - - lua_pushstring(L, "register_menu"); - lua_pushcfunction(L, lua_register_menu); - lua_settable(L, LUA_GLOBALSINDEX); - - lua_pushstring(L, "gui_enabled"); - lua_pushcfunction(L, lua_gui_enabled); - lua_settable(L, LUA_GLOBALSINDEX); - - lua_pushstring(L, "dialog"); - lua_pushcfunction(L, lua_new_dialog); - lua_settable(L, LUA_GLOBALSINDEX); + ELUA_REGISTER_FUNCTION(critical); + ELUA_REGISTER_FUNCTION(warn); + ELUA_REGISTER_FUNCTION(info); + ELUA_REGISTER_FUNCTION(message); + ELUA_REGISTER_FUNCTION(debug); + + /* Utility functions */ + ELUA_REGISTER_FUNCTION(format_date); + ELUA_REGISTER_FUNCTION(format_time); + ELUA_REGISTER_FUNCTION(report_failure); + + /* Functions declared in modules */ + ELUA_REGISTER_FUNCTIONS(); /* the init_routines table (accessible by the user) */ lua_pushstring(L, LUA_INIT_ROUTINES); @@ -446,7 +394,7 @@ void register_lua(void) { * disable the function to avoid weirdness */ lua_pushstring(L, "register_menu"); - lua_pushcfunction(L, lua_not_register_menu); + lua_pushcfunction(L, elua_not_register_menu); lua_settable(L, LUA_GLOBALSINDEX); /* set up some essential globals */ diff --git a/plugins/lua/packet-lua.h b/plugins/lua/elua.h index ac25ff932a..53d0b42a4d 100644 --- a/plugins/lua/packet-lua.h +++ b/plugins/lua/elua.h @@ -50,6 +50,8 @@ #include <epan/emem.h> #include <epan/funnel.h> +#include "elua_register.h" + #define LUA_DISSECTORS_TABLE "dissectors" #define LUA_INIT_ROUTINES "init_routines" #define LUA_HANDOFF_ROUTINES "handoff_routines" @@ -101,73 +103,39 @@ typedef struct _eth_proto_t { gboolean is_postdissector; } eth_proto_t; +struct _eth_distbl_t { + dissector_table_t table; + gchar* name; +}; + +struct _eth_col_info { + column_info* cinfo; + gint col; +}; typedef struct {const gchar* str; enum ftenum id; } eth_ft_types_t; -#define PROTO_FIELD "ProtoField" +typedef eth_pref_t* Pref; +typedef eth_pref_t* Prefs; typedef struct _eth_field_t* ProtoField; - -#define PROTO_FIELD_ARRAY "ProtoFieldArray" typedef GArray* ProtoFieldArray; - -#define SUBTREE "SubTree" typedef int* SubTree; - -#define PROTO "Protocol" typedef struct _eth_proto_t* Proto; - -#define DISSECTOR_TABLE "DissectorTable" -typedef struct _eth_distbl_t { - dissector_table_t table; - gchar* name; -}* DissectorTable; - -#define DISSECTOR "Dissector" +typedef struct _eth_distbl_t* DissectorTable; typedef dissector_handle_t Dissector; - -#define BYTE_ARRAY "ByteArray" typedef GByteArray* ByteArray; - -#define TVB "Tvb" typedef tvbuff_t* Tvb; - -#define TVB_RANGE "TvbRange" typedef struct _eth_tvbrange* TvbRange; - -#define COLUMN "Column" -typedef struct _eth_col_info { - column_info* cinfo; - gint col; -}* Column; - -#define COLUMNS "Columns" +typedef struct _eth_col_info* Column; typedef column_info* Columns; - -#define PINFO "Pinfo" typedef packet_info* Pinfo; - -#define PROTO_TREE "ProtoTree" typedef proto_tree* ProtoTree; - -#define ITEM "ProtoItem" typedef proto_item* ProtoItem; - -#define ADDRESS "Address" typedef address* Address; - -#define FIELD "Field" typedef header_field_info** Field; - -#define TAP "Tap" typedef struct _eth_tap* Tap; - -#define TEXT_WINDOW "TextWindow" typedef funnel_text_window_t* TextWindow; - -#define DUMPER "Dumper" typedef wtap_dumper* Dumper; - -#define PSEUDOHEADER "PseudoHeader" typedef struct lua_pseudo_header* PseudoHeader; #define NOP @@ -180,30 +148,30 @@ typedef struct lua_pseudo_header* PseudoHeader; * * LUA_CLASS_DEFINE must be used without trailing ';' */ -#define LUA_CLASS_DEFINE(C,CN,check_code) \ +#define ELUA_CLASS_DEFINE(C,check_code) \ C to##C(lua_State* L, int index) { \ C* v = (C*)lua_touserdata (L, index); \ - if (!v) luaL_typerror(L,index,CN); \ + if (!v) luaL_typerror(L,index,#C); \ return *v; \ } \ C check##C(lua_State* L, int index) { \ C* p; \ luaL_checktype(L,index,LUA_TUSERDATA); \ - p = (C*)luaL_checkudata(L, index, CN); \ + p = (C*)luaL_checkudata(L, index, #C); \ check_code; \ return p ? *p : NULL; \ } \ 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); \ + luaL_getmetatable(L, #C); 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)); \ + return (gboolean)(lua_isuserdata(L,i) && luaL_checkudata(L,3,#C)); \ } \ C shift##C(lua_State* L,int i) { \ C* p; \ - if ((p = (C*)luaL_checkudata(L, i, CN))) {\ + if ((p = (C*)luaL_checkudata(L, i, #C))) {\ lua_remove(L,i); \ return *p; \ } else { \ @@ -214,10 +182,10 @@ C shift##C(lua_State* L,int i) { \ #ifdef HAVE_LUA_5_1 -#define REGISTER_FULL_CLASS(CN,methods,meta) { \ - luaL_register (L, CN, methods); \ - luaL_newmetatable (L, CN); \ - luaL_register (L, NULL, meta); \ +#define ELUA_REGISTER_CLASS(C) { \ + luaL_register (L, #C, C ## _methods); \ + luaL_newmetatable (L, #C); \ + luaL_register (L, NULL, C ## _meta); \ lua_pushliteral(L, "__index"); \ lua_pushvalue(L, -3); \ lua_rawset(L, -3); \ @@ -227,16 +195,16 @@ C shift##C(lua_State* L,int i) { \ lua_pop(L, 1); \ } -#define REGISTER_META(CN,meta) luaL_newmetatable (L, CN); luaL_register (L, NULL, meta); +#define ELUA_REGISTER_META(C) luaL_newmetatable (L, #C); luaL_register (L, NULL, C ## _meta); -#define INIT_LUA(L) L = luaL_newstate(); luaL_openlibs(L); +#define ELUA_INIT(L) L = luaL_newstate(); luaL_openlibs(L); #else /* Lua 5.0 */ -#define REGISTER_FULL_CLASS(CN,methods,meta) { \ - luaL_openlib(L, CN, methods, 0); \ - luaL_newmetatable(L, CN); \ - luaL_openlib(L, 0, meta, 0); \ +#define ELUA_REGISTER_CLASS(C) { \ + luaL_openlib(L, #C, C ## _methods, 0); \ + luaL_newmetatable(L, #C); \ + luaL_openlib(L, 0, C ## _meta, 0); \ lua_pushliteral(L, "__index"); \ lua_pushvalue(L, -3); \ lua_rawset(L, -3); \ @@ -246,12 +214,32 @@ C shift##C(lua_State* L,int i) { \ lua_pop(L, 1); \ } -#define REGISTER_META(CN,meta) luaL_newmetatable (L, CN); luaL_openlib (L, NULL, meta, 0); +#define ELUA_REGISTER_META(C) luaL_newmetatable (L, #C); luaL_openlib (L, NULL, C ## _meta, 0); -#define INIT_LUA(L) L = lua_open(); luaopen_base(L); luaopen_table(L); luaopen_io(L); luaopen_string(L); +#define ELUA_INIT(L) L = lua_open(); luaopen_base(L); luaopen_table(L); luaopen_io(L); luaopen_string(L); #endif +#define ELUA_FUNCTION extern int +#define ELUA_REGISTER_FUNCTION(name) { lua_pushstring(L, #name); lua_pushcfunction(L, elua_## name); lua_settable(L, LUA_GLOBALSINDEX); } +#define ELUA_REGISTER extern int + +#define ELUA_METHOD static int +#define ELUA_CONSTRUCTOR static int +#define ELUA_ATTR_SET static int +#define ELUA_ATTR_GET static int +#define ELUA_METAMETHOD static int + +#define ELUA_METHODS static const luaL_reg +#define ELUA_META static const luaL_reg +#define ELUA_CLASS_FNREG(class,name) { #name, class##_##name } + +#define ELUA_ERROR(name,error) { luaL_error(L, #name ": " error); return 0; } +#define ELUA_ARG_ERROR(name,attr,error) { luaL_argerror(L,ELUA_ARG_ ## name ## _ ## attr, #name ": " error); return 0; } +#define ELUA_OPTARG_ERROR(name,attr,error) { luaL_argerror(L,ELUA_OPTARG_##name##_ ##attr, #name ": " error); return 0; } + +#define ELUA_RETURN(i) return (i); +#define ELUA_FINAL_RETURN(i) return (i); extern packet_info* lua_pinfo; extern proto_tree* lua_tree; @@ -261,7 +249,7 @@ extern dissector_handle_t lua_data_handle; extern gboolean lua_initialized; extern int lua_dissectors_table_ref; -#define LUA_CLASS_DECLARE(C,CN) \ +#define ELUA_CLASS_DECLARE(C) \ 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); \ @@ -269,28 +257,8 @@ extern int C##_register(lua_State* L); \ extern gboolean is##C(lua_State* L,int i); \ extern C shift##C(lua_State* L,int i) - - -LUA_CLASS_DECLARE(Tap,TAP); -LUA_CLASS_DECLARE(Field,FIELD); -LUA_CLASS_DECLARE(ProtoField,PROTO_FIELD); -LUA_CLASS_DECLARE(ProtoFieldArray,PROTO_FIELD_ARRAY); -LUA_CLASS_DECLARE(SubTree,SUBTREE); -LUA_CLASS_DECLARE(Proto,PROTO); -LUA_CLASS_DECLARE(ByteArray,BYTE_ARRAY); -LUA_CLASS_DECLARE(Tvb,TVB); -LUA_CLASS_DECLARE(TvbRange,TVB_RANGE); -LUA_CLASS_DECLARE(Column,COLUMN); -LUA_CLASS_DECLARE(Columns,COLUMNS); -LUA_CLASS_DECLARE(Pinfo,PINFO); -LUA_CLASS_DECLARE(ProtoTree,TREE); -LUA_CLASS_DECLARE(ProtoItem,ITEM); -LUA_CLASS_DECLARE(Dissector,DISSECTOR); -LUA_CLASS_DECLARE(DissectorTable,DISSECTOR_TABLE); -LUA_CLASS_DECLARE(Address,ADDRESS); -LUA_CLASS_DECLARE(TextWindow,TEXT_WINDOW); -LUA_CLASS_DECLARE(Dumper,DUMPER); -LUA_CLASS_DECLARE(PseudoHeader,PSEUDOHEADER); +ELUA_DECLARE_CLASSES(); +ELUA_DECLARE_FUNCTIONS(); extern void dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree); extern int lua_tap_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const void *data _U_); diff --git a/plugins/lua/lua_dumper.c b/plugins/lua/elua_dumper.c index af351935ad..e4379e3a36 100644 --- a/plugins/lua/lua_dumper.c +++ b/plugins/lua/elua_dumper.c @@ -5,7 +5,7 @@ * * (c) 2006, Luis E. Garcia Ontanon <luis.ontanon@gmail.com> * - * $Id: lua_tvb.c 17307 2006-02-15 02:10:07Z lego $ + * $Id$ * * Ethereal - Network traffic analyzer * By Gerald Combs <gerald@ethereal.com> @@ -26,11 +26,10 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "packet-lua.h" +#include "elua.h" #include <math.h> -LUA_CLASS_DEFINE(Dumper,DUMPER,NOP) -LUA_CLASS_DEFINE(PseudoHeader,PSEUDOHEADER,NOP) +ELUA_CLASS_DEFINE(PseudoHeader,NOP) enum lua_pseudoheader_type { PHDR_NONE, @@ -112,7 +111,7 @@ static int PseudoHeader_k12(lua_State* L) { luaL_error(L,"not implemented"); ret #endif int PseudoHeader_register(lua_State* L) { - luaL_newmetatable(L, PSEUDOHEADER); + luaL_newmetatable(L, "PseudoHeader"); lua_pushstring(L, "PH_MTP2"); lua_pushcfunction(L, PseudoHeader_mtp2); @@ -133,6 +132,11 @@ int PseudoHeader_register(lua_State* L) { return 0; } + + +ELUA_CLASS_DEFINE(Dumper,NOP) + + static GHashTable* dumper_encaps = NULL; #define DUMPER_ENCAP(d) GPOINTER_TO_INT(g_hash_table_lookup(dumper_encaps,d)) @@ -241,8 +245,6 @@ static int Dumper_new_for_current(lua_State* L) { int encap; int err = 0; - if (!d) return 0; - if (! lua_pinfo ) { luaL_error(L,"Dumper.new_for_current cannot be used outside a tap or a dissector"); return 0; @@ -321,6 +323,6 @@ static const luaL_reg Dumper_meta[] = int Dumper_register(lua_State* L) { dumper_encaps = g_hash_table_new(g_direct_hash,g_direct_equal); - REGISTER_FULL_CLASS(DUMPER, Dumper_methods, Dumper_meta) + ELUA_REGISTER_CLASS(Dumper); return 1; } diff --git a/plugins/lua/lua_gui.c b/plugins/lua/elua_gui.c index 5380f4ee46..0a98a5f962 100644 --- a/plugins/lua/lua_gui.c +++ b/plugins/lua/elua_gui.c @@ -24,9 +24,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "packet-lua.h" - -LUA_CLASS_DEFINE(TextWindow,TEXT_WINDOW,NOP) +#include "elua.h" static const funnel_ops_t* ops = NULL; @@ -59,15 +57,21 @@ void lua_menu_callback(gpointer data) { return; } -extern int lua_register_menu(lua_State* L) { - const gchar* name = luaL_checkstring(L,1); +ELUA_FUNCTION elua_register_menu(lua_State* L) { /* Register a menu item in the Statistics menu. */ +#define ELUA_ARG_register_menu_NAME 1 /* The name of the menu item. */ +#define ELUA_ARG_register_menu_ACTION 2 /* The function to be called when the menu item is invoked. */ +#define ELUA_OPTARG_register_menu_RETAP 3 /* Whether to rerun the packet list after the menu is invoked. */ +#define ELUA_OPTARG_register_menu_USERDATA 4 /* To be passed to the action. */ + + const gchar* name = luaL_checkstring(L,ELUA_ARG_register_menu_NAME); struct _lua_menu_data* md; gboolean retap = FALSE; - if (!lua_isfunction(L,2)) { - luaL_error(L,"register_menu takes a string, a function and another optional datum"); - return 0; - } + if(!name) + ELUA_ARG_ERROR(register_menu,NAME,"must be a string"); + + if (!lua_isfunction(L,ELUA_ARG_register_menu_ACTION)) + ELUA_ARG_ERROR(register_menu,ACTION,"must be a function"); md = g_malloc(sizeof(struct _lua_menu_data)); md->L = L; @@ -76,11 +80,11 @@ extern int lua_register_menu(lua_State* L) { md->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); if ( lua_gettop(L) > 2) { - retap = lua_toboolean(L,3); + retap = lua_toboolean(L,ELUA_OPTARG_register_menu_RETAP); } if ( lua_gettop(L) > 3) { - lua_pushvalue(L, 4); + lua_pushvalue(L, ELUA_OPTARG_register_menu_USERDATA); md->data_ref = luaL_ref(L, LUA_REGISTRYINDEX); } else { md->data_ref = LUA_NOREF; @@ -91,7 +95,8 @@ extern int lua_register_menu(lua_State* L) { lua_menu_callback, md, retap); - return 0; + + ELUA_FINAL_RETURN(0); } @@ -143,7 +148,11 @@ static void lua_dialog_cb(gchar** user_input, void* data) { } -extern int lua_new_dialog(lua_State* L) { +ELUA_FUNCTION elua_new_dialog(lua_State* L) { /* Pops up a new dialog */ +#define ELUA_ARG_new_dialog_TITLE 1 /* Title of the dialog's window. */ +#define ELUA_ARG_new_dialog_ACTION 1 /* Action to be performed when OKd. */ +/* ELUA_EXTRARGS new_dialog : a series of strings to be used as labels of the dialog */ + const gchar* title; int top = lua_gettop(L); int i; @@ -155,18 +164,18 @@ extern int lua_new_dialog(lua_State* L) { return 0; } - if (! (title = luaL_checkstring(L,1)) ) { - luaL_argerror(L,1,"the title must be a string"); + if (! (title = luaL_checkstring(L,ELUA_ARG_new_dialog_TITLE)) ) { + ELUA_ARG_ERROR(new_dialog,TITLE,"must be a string"); return 0; } - if (! lua_isfunction(L,2)) { - luaL_argerror(L,2,"must be a function"); + if (! lua_isfunction(L,ELUA_ARG_new_dialog_ACTION)) { + ELUA_ARG_ERROR(new_dialog,ACTION,"must be a function"); return 0; } if (top < 3) { - luaL_error(L,"too few arguments"); + ELUA_ERROR(new_dialog,"at least one field required"); return 0; } @@ -190,6 +199,11 @@ extern int lua_new_dialog(lua_State* L) { for (i = 1; i <= top; i++) { gchar* label = (void*)luaL_checkstring(L,i); + + /* XXX leaks labels on error */ + if (! label) + ELUA_ERROR(new_dialog,"fields must be strings"); + g_ptr_array_add(labels,label); } @@ -199,28 +213,24 @@ extern int lua_new_dialog(lua_State* L) { g_ptr_array_free(labels,TRUE); - return 0; + ELUA_FINAL_RETURN(0); } -/* - * TextWindow - */ -static int TextWindow_new(lua_State* L) { +ELUA_CLASS_DEFINE(TextWindow,NOP) /* Manages a text window. */ + +ELUA_CONSTRUCTOR TextWindow_new(lua_State* L) { /* Creates a new TextWindow. */ +#define ELUA_OPTARG_TextWindow_new_TITLE 1 /* Title of the new window. */ + const gchar* title; TextWindow tw; - - if (!ops) { - luaL_error(L,"GUI not available"); - return 0; - } - - title = luaL_optstring(L,1,"Untitled Window"); + + title = luaL_optstring(L,ELUA_OPTARG_TextWindow_new_TITLE,"Untitled Window"); tw = ops->new_text_window(title); pushTextWindow(L,tw); - return 1; + ELUA_FINAL_RETURN(1); /* A TextWindow object */ } struct _close_cb_data { @@ -258,16 +268,16 @@ static void text_win_close_cb(void* data) { } } -static int TextWindow_at_close(lua_State* L) { +ELUA_METHOD TextWindow_at_close(lua_State* L) { /* Set the function that will be called when the window closes */ +#define ELUA_ARG_TextWindow_at_close_ACTION 2 /* A function to be executed when the user closes the window */ + TextWindow tw = shiftTextWindow(L,1); struct _close_cb_data* cbd; lua_settop(L,2); - if (! lua_isfunction(L,1)) { - luaL_error(L,"Window's close callback must be a function"); - return 0; - } + if (! lua_isfunction(L,1)) + ELUA_ARG_ERROR(TextWindow_at_close,ACTION,"must be a function"); cbd = g_malloc(sizeof(struct _close_cb_data)); @@ -278,60 +288,67 @@ static int TextWindow_at_close(lua_State* L) { ops->set_close_cb(tw,text_win_close_cb,cbd); pushTextWindow(L,tw); - return 1; + ELUA_FINAL_RETURN(1); /* The TextWindow object. */ } -static int TextWindow_set_text(lua_State* L) { +ELUA_METHOD TextWindow_set(lua_State* L) { /* Sets the text. */ +#define ELUA_ARG_TextWindow_set_TEXT 2 /* The text to be used. */ + TextWindow tw = shiftTextWindow(L,1); const gchar* text = luaL_checkstring(L,1); - if (!text) return 0; + if (!text) + ELUA_ARG_ERROR(TextWindow_set,TEXT,"must be a string"); ops->set_text(tw,text); pushTextWindow(L,tw); - return 1; + ELUA_FINAL_RETURN(1); /* The TextWindow object. */ } -static int TextWindow_append_text(lua_State* L) { +ELUA_METHOD TextWindow_append(lua_State* L) { /* Appends text */ +#define ELUA_ARG_TextWindow_append_TEXT 2 /* The text to be appended */ TextWindow tw = shiftTextWindow(L,1); const gchar* text = luaL_checkstring(L,1); - if (!text) return 0; - + if (!text) + ELUA_ARG_ERROR(TextWindow_append,TEXT,"must be a string"); + ops->append_text(tw,text); pushTextWindow(L,tw); - return 1; + ELUA_FINAL_RETURN(1); /* The TextWindow object. */ } -static int TextWindow_prepend_text(lua_State* L) { +ELUA_METHOD TextWindow_prepend(lua_State* L) { /* Prepends text */ +#define ELUA_ARG_TextWindow_prepend_TEXT 2 /* The text to be appended */ TextWindow tw = shiftTextWindow(L,1); const gchar* text = luaL_checkstring(L,1); - if (!text) return 0; - + if (!text) + ELUA_ARG_ERROR(TextWindow_prepend,TEXT,"must be a string"); + ops->prepend_text(tw,text); pushTextWindow(L,tw); - return 1; + ELUA_FINAL_RETURN(1); /* The TextWindow object. */ } -static int TextWindow_clear_text(lua_State* L) { +ELUA_METHOD TextWindow_clear(lua_State* L) { /* Errases all text in the window. */ TextWindow tw = shiftTextWindow(L,1); ops->clear_text(tw); pushTextWindow(L,tw); - return 1; + ELUA_FINAL_RETURN(1); /* The TextWindow object. */ } -static int TextWindow_get_text(lua_State* L) { +ELUA_METHOD TextWindow_get_text(lua_State* L) { /* Get the text of the window */ TextWindow tw = shiftTextWindow(L,1); const gchar* text = ops->get_text(tw); lua_pushstring(L,text); - return 1; + ELUA_FINAL_RETURN(1); /* The TextWindow's text. */ } static int TextWindow_gc(lua_State* L) { @@ -342,13 +359,14 @@ static int TextWindow_gc(lua_State* L) { } -static const luaL_reg TextWindow_methods[] = { - {"new", TextWindow_new}, - {"set", TextWindow_set_text}, - {"append", TextWindow_append_text}, - {"prepend", TextWindow_prepend_text}, - {"clear", TextWindow_clear_text}, - {"at_close", TextWindow_at_close}, +ELUA_METHODS TextWindow_methods[] = { + ELUA_CLASS_FNREG(TextWindow,new), + ELUA_CLASS_FNREG(TextWindow,set), + ELUA_CLASS_FNREG(TextWindow,new), + ELUA_CLASS_FNREG(TextWindow,append), + ELUA_CLASS_FNREG(TextWindow,prepend), + ELUA_CLASS_FNREG(TextWindow,clear), + ELUA_CLASS_FNREG(TextWindow,at_close), {0, 0} }; @@ -362,7 +380,7 @@ int TextWindow_register(lua_State* L) { ops = funnel_get_funnel_ops(); - REGISTER_FULL_CLASS(TEXT_WINDOW, TextWindow_methods, TextWindow_meta); + ELUA_REGISTER_CLASS(TextWindow); return 1; } diff --git a/plugins/lua/elua_makedoc.pl b/plugins/lua/elua_makedoc.pl new file mode 100644 index 0000000000..91c7bcde61 --- /dev/null +++ b/plugins/lua/elua_makedoc.pl @@ -0,0 +1,247 @@ +#!/usr/bin/perl +# +# elua_makedoc.pl +# Reference Manual Generator +# +# $Id$ +# +# Ethereal - Network traffic analyzer +# By Gerald Combs <gerald@ethereal.com> +# Copyright 1998 Gerald Combs +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# (-: I don't even think writing this in Lua :-) + +use strict; +use V2P; + +sub deb { +#warn $_[0] if $_[0] =~ /^>e/; +} + +my $class; +my %classes; +my $function; +my @functions; + + +my %template = %{{ + class_header => "= %s =\n", + class_desc => "%s\n", + class_constructors_header => "== %s constructors ==\n", + class_methods_header => "== %s methods ==\n", + function_header => "=== %s ===\n", + function_descr => "%s\n", + function_arg_header => "==== %s ====\n", + function_arg_descr => "%s\n", + function_argerror => " * %s\n", + function_returns_header => "==== returns ====\n", + function_returns => " * %s\n", + function_errors_header => "==== errors ====\n", + function_errors => " * %s\n", + non_method_functions_header => "= Non method functions =\n", + +}}; + + +my @control = ( + +[ 'ELUA_CLASS_DEFINE\050\s*([A-Z][a-zA-Z]+)\s*,[^\051]*\051\s*(/\*(.*?)\*/)?', + sub { + deb ">c=$1=$2=$3=$4=$5=$6=$7=\n"; + $class = { name => $1, descr=> $3, constructors => [], methods => [] }; + $classes{$1} = $class; + }], + +[ 'ELUA_FUNCTION\s+elua_([a-z_]+)[^\173]*\173\s*(/\*(.*?)\*/)?', + sub { + deb ">f=$1=$2=$3=$4=$5=$6=$7=\n"; + $function = { returns => [], arglist => [], args => {}, name => $1, descr => $3, type => 'standalone' }; + push @functions, $function; + } ] , + +[ 'ELUA_CONSTRUCTOR\s+([A-Za-z]+)_([a-z_]+)[^\173]*\173\s*(/\*(.*?)\*/)?', + sub { + deb ">cc=$1=$2=$3=$4=$5=$6=$7=\n"; + $function = { returns => [], arglist => [], args => {}, name => "$1.$2", descr => $4, type => 'constructor' }; + push @{${$class}{constructors}}, $function; + } ] , + +[ 'ELUA_METHOD\s+([A-Za-z]+)_([a-z_]+)[^\173]*\173\s*(/\*(.*?)\*/)?', + sub { + deb ">cm=$1=$2=$3=$4=$5=$6=$7=\n"; + $function = { returns => [], arglist => [], args => {}, name => "$1:$2", descr => $4, type => 'method' }; + push @{${$class}{methods}}, $function; + } ] , + +[ '#define ELUA_(OPT)?ARG_([a-z_]+)_([A-Z0-9]+)\s+\d+\s*(/\*(.*?)\*/)?', + sub { + deb ">a=$1=$2=$3=$4=$5=$6=$7=\n"; + push @{${$function}{arglist}} , $3; + ${${$function}{args}}{$3} = {descr=>$5} + } ], + +[ '#define ELUA_(OPT)?ARG_([A-Za-z]+)_([a-z_]+)_([A-Z0-9]+)\s+\d+\s*(/\*(.*?)\*/)?', + sub { + deb ">ca=$1=$2=$3=$4=$5=$6=$7=\n"; + push @{${$function}{arglist}} , $4; + ${${$function}{args}}{$4} = {descr=>$6} + } ], + +[ 'ELUA_(FINAL_)?RETURN\050\s*.*?\s*\051\s*;\s*(/\*(.*?)\*/)?', + sub { + deb ">fr=$1=$2=$3=$4=$5=$6=$7=\n"; + push @{${$function}{returns}} , $3 if $3 ne ''; + } ], + +[ 'ELUA_(OPT)?ARG_ERROR\s*\050\s*(([A-Z][A-Za-z]+)_)?([a-z_]+)\s*,\s*([A-Z0-9]+)\s*,\s*"([^"]*)"', + sub { + deb ">ae=$1=$2=$3=$4=$5=$6=$7=\n"; + my $errors; + unless (exists ${${${$function}{args}}{$5}}{errors}) { + $errors = ${${${$function}{args}}{$5}}{errors} = []; + } else { + $errors = ${${${$function}{args}}{$5}}{errors}; + } + + push @{$errors}, $6; + } ] , + [ 'ELUA_ERROR\s*\050\s*(([A-Z][A-Za-z]+)_)?([a-z_]+),"([^"]*)"', + sub { + deb ">e=$1=$2=$3=$4=$5=$6=$7=\n"; + my $errors; + unless (exists ${$function}{errors}) { + $errors = ${$function}{errors} = []; + } else { + $errors = ${$function}{errors}; + } + + push @{$errors}, $4; + + } ], + +#[ 'ELUA_ATTR_GET\s+([A-Za-z]+)_get_([a-z_]+)[^\173]*\173\s*(/\*(.*?)\*/)?', +# sub { } ] , +#[ 'ELUA_ATTR_SET\s+([A-Za-z]+)_set_([a-z_]+)[^\173]*\173\s*(/\*(.*?)\*/)?', +# sub { } ] , +#['(.*?\n)', +# sub { print "--->$1" } ], +); +my $file; +while ( $file = shift) { + + my $docfile = $file; + $docfile =~ s/\.c$/.pod/; + + open C, "< $file"; + open D, "> doc/$docfile"; + + my $b = ''; + LINE: while (<C>) { + $b .= $_; + for (@control) { + my ($re,$f) = @{$_}; + while ( $b =~ s/$re//ms ) { + &{$f}(); + next LINE; + } + } + } + + for my $cname (sort keys %classes) { + my $class = $classes{$cname}; + printf D $template{class_header}, $cname; + printf D $template{class_desc} , ${$class}{descr} if ${$class}{descr}; + + if ( $#{${$class}{constructors}} >= 0) { + printf D $template{class_constructors_header}, $cname; + + for my $c (@{${$class}{constructors}}) { + function_descr($c); + } + + printf D $template{class_constructors_footer}, $cname; + } + + if ( $#{${$class}{methods}} >= 0) { + printf D $template{class_methods_header}, $cname; + + for my $m (@{${$class}{methods}}) { + function_descr($m); + } + + printf D $template{class_methods_footer}, $cname; + } + + } + + print D $template{non_method_functions_header}; + + for my $f (@functions) { + function_descr($f); + } + + %classes = (); + $class = undef; + $function = undef; + @functions = (); + close C; + close D; +} + +sub function_descr { + my $f = $_[0]; + my $arglist = ''; + + for (@{ ${$f}{arglist} }) { + my $a = $_; + $a =~ tr/A-Z/a-z/; + $arglist .= "$a, "; + } + + $arglist =~ s/, $//; + + printf D $template{function_header}, "${$f}{name}($arglist)"; + printf D $template{function_descr}, ${$f}{descr} if ${$f}{descr}; + + for my $argname (@{${$f}{arglist}}) { + my $arg = ${${$f}{args}}{$argname}; + $argname =~ tr/A-Z/a-z/; + + printf D $template{function_arg_header}, $argname; + printf D $template{function_arg_descr}, ${$arg}{descr} if ${$arg}{descr}; + + if ( $#{${$arg}{errors}} >= 0) { + printf D $template{function_argerrors_header}, $argname; + printf D $template{function_argerror}, $_ for @{${$arg}{errors}}; + printf D $template{function_argerrors_footer}, $argname; + } + + } + + if ( $#{${$f}{returns}} >= 0) { + printf D $template{function_returns_header}, ${$f}{name}; + printf D $template{function_returns}, $_ for @{${$f}{returns}}; + printf D $template{function_returns_footer}, ${$f}{name}; + } + + if ( $#{${$f}{errors}} >= 0) { + printf D $template{function_errors_header}, ${$f}{name}; + printf D $template{function_errors}, $_ for @{${$f}{errors}}; + printf D $template{function_errors_footer}, ${$f}{name}; + } + +} diff --git a/plugins/lua/elua_makereg.pl b/plugins/lua/elua_makereg.pl new file mode 100644 index 0000000000..a0c38b7719 --- /dev/null +++ b/plugins/lua/elua_makereg.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl +# +# elua_makereg.pl +# Registration Macros Generator +# +# $Id$ +# +# Ethereal - Network traffic analyzer +# By Gerald Combs <gerald@ethereal.com> +# Copyright 1998 Gerald Combs +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +use strict; + +my @classes = (); +my @functions = (); + +while (<>) { + push @classes, $1 if /ELUA_CLASS_DEFINE\050\s*([A-Za-z]+)/; + push @functions, $1 if /ELUA_FUNCTION\s+elua_([a-z_]+)/; +} + +print "/* This file is automatically genrated by elua_makereg.pl do not edit */\n\n"; + +print "#define ELUA_REGISTER_CLASSES(L) { \\\n"; +for (@classes) { + print "\t${_}_register(L);\\\n" +} +print "}\n\n"; + +print "#define ELUA_DECLARE_CLASSES() \\\n"; +for (@classes) { + print "\tELUA_CLASS_DECLARE($_);\\\n" +} +print "\n\n"; + +print "#define ELUA_DECLARE_FUNCTIONS() \\\n"; +for (@functions) { + print "\tELUA_FUNCTION elua_$_(lua_State* L);\\\n" +} +print "\n\n"; + +print "#define ELUA_REGISTER_FUNCTIONS() {\\\n"; +for (@functions) { + print "\t ELUA_REGISTER_FUNCTION($_); \\\n" +} +print "}\n\n"; diff --git a/plugins/lua/lua_pinfo.c b/plugins/lua/elua_pinfo.c index 4edee21531..eec1da1f89 100644 --- a/plugins/lua/lua_pinfo.c +++ b/plugins/lua/elua_pinfo.c @@ -26,15 +26,11 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "packet-lua.h" +#include "elua.h" + #include <epan/addr_resolv.h> #include <string.h> -LUA_CLASS_DEFINE(Column,COLUMN,if (! *p) luaL_error(L,"expired column")) -LUA_CLASS_DEFINE(Columns,COLUMNS,if (! *p) luaL_error(L,"expired columns")) -LUA_CLASS_DEFINE(Pinfo,PINFO,if (! *p) luaL_error(L,"expired pinfo")) -LUA_CLASS_DEFINE(Address,ADDRESS,NOP) - /* * NULLify lua userdata to avoid crashing when trying to @@ -60,10 +56,13 @@ void push_Pinfo(lua_State* L, Pinfo pinfo) { #define PUSH_COLUMN(L,c) g_ptr_array_add(outstanding_stuff,pushColumn(L,c)) #define PUSH_COLUMNS(L,c) g_ptr_array_add(outstanding_stuff,pushColumns(L,c)) -static int Address_ip(lua_State* L) { +ELUA_CLASS_DEFINE(Address,NOP) + +ELUA_CONSTRUCTOR Address_ip(lua_State* L) { /* Creates an Address Object representing an IP address. */ +#define ELUA_ARG_Address_ip_HOSTNAME 1 /* The address or name of the IP host. */ Address addr = g_malloc(sizeof(address)); guint32* ip_addr = g_malloc(sizeof(guint32)); - const gchar* name = luaL_checkstring(L,1); + const gchar* name = luaL_checkstring(L,ELUA_ARG_Address_ip_HOSTNAME); if (! get_host_ipaddr(name, (guint32*)ip_addr)) { *ip_addr = 0; @@ -71,7 +70,7 @@ static int Address_ip(lua_State* L) { SET_ADDRESS(addr, AT_IPv4, 4, ip_addr); pushAddress(L,addr); - return 1; + ELUA_RETURN(1); /* the Address object */ } #if 0 @@ -182,7 +181,7 @@ static int Address_tipc(lua_State* L) { } #endif -static const luaL_reg Address_methods[] = { +ELUA_METHODS Address_methods[] = { {"ip", Address_ip }, {"ipv4", Address_ip }, #if 0 @@ -203,12 +202,12 @@ static const luaL_reg Address_methods[] = { {0,0} }; -static int Address_tostring(lua_State* L) { +ELUA_METAMETHOD Address_tostring(lua_State* L) { Address addr = checkAddress(L,1); lua_pushstring(L,get_addr_name(addr)); - return 1; + ELUA_RETURN(1); /* The string representing the address. */ } static int Address_gc(lua_State* L) { @@ -222,7 +221,7 @@ static int Address_gc(lua_State* L) { return 0; } -static int Address_gt(lua_State* L) { +ELUA_METAMETHOD Address_gt(lua_State* L) { Address addr1 = checkAddress(L,1); Address addr2 = checkAddress(L,2); gboolean result = FALSE; @@ -235,7 +234,7 @@ static int Address_gt(lua_State* L) { return 1; } -static int Address_ge(lua_State* L) { +ELUA_METAMETHOD Address_ge(lua_State* L) { Address addr1 = checkAddress(L,1); Address addr2 = checkAddress(L,2); gboolean result = FALSE; @@ -248,7 +247,7 @@ static int Address_ge(lua_State* L) { return 1; } -static int Address_eq(lua_State* L) { +ELUA_METAMETHOD Address_eq(lua_State* L) { Address addr1 = checkAddress(L,1); Address addr2 = checkAddress(L,2); gboolean result = FALSE; @@ -261,7 +260,7 @@ static int Address_eq(lua_State* L) { return 1; } -static int Address_le(lua_State* L) { +ELUA_METAMETHOD Address_le(lua_State* L) { Address addr1 = checkAddress(L,1); Address addr2 = checkAddress(L,2); gboolean result = FALSE; @@ -274,7 +273,7 @@ static int Address_le(lua_State* L) { return 1; } -static int Address_lt(lua_State* L) { +ELUA_METAMETHOD Address_lt(lua_State* L) { Address addr1 = checkAddress(L,1); Address addr2 = checkAddress(L,2); gboolean result = FALSE; @@ -287,7 +286,7 @@ static int Address_lt(lua_State* L) { return 1; } -static const luaL_reg Address_meta[] = { +ELUA_META Address_meta[] = { {"__gc", Address_gc }, {"__tostring", Address_tostring }, {"__gt",Address_gt}, @@ -300,11 +299,13 @@ static const luaL_reg Address_meta[] = { int Address_register(lua_State *L) { - REGISTER_FULL_CLASS(ADDRESS, Address_methods, Address_meta); + ELUA_REGISTER_CLASS(Address); return 1; } -/* Column class */ + +ELUA_CLASS_DEFINE(Column,if (! *p) luaL_error(L,"expired column")) + struct col_names_t { const gchar* name; int id; @@ -382,7 +383,7 @@ static const gchar* col_id_to_name(gint id) { } -static int Column_tostring(lua_State *L) { +ELUA_METAMETHOD Column_tostring(lua_State *L) { Column c = checkColumn(L,1); const gchar* name; @@ -397,7 +398,7 @@ static int Column_tostring(lua_State *L) { return 1; } -static int Column_clear(lua_State *L) { +ELUA_METHOD Column_clear(lua_State *L) { Column c = checkColumn(L,1); if (!(c && c->cinfo)) return 0; @@ -408,7 +409,7 @@ static int Column_clear(lua_State *L) { return 0; } -static int Column_set(lua_State *L) { +ELUA_METHOD Column_set(lua_State *L) { Column c = checkColumn(L,1); const gchar* s = luaL_checkstring(L,2); @@ -420,7 +421,7 @@ static int Column_set(lua_State *L) { return 0; } -static int Column_append(lua_State *L) { +ELUA_METHOD Column_append(lua_State *L) { Column c = checkColumn(L,1); const gchar* s = luaL_checkstring(L,2); @@ -431,7 +432,8 @@ static int Column_append(lua_State *L) { return 0; } -static int Column_preppend(lua_State *L) { + +ELUA_METHOD Column_preppend(lua_State *L) { Column c = checkColumn(L,1); const gchar* s = luaL_checkstring(L,2); @@ -459,7 +461,7 @@ static const luaL_reg Column_meta[] = { int Column_register(lua_State *L) { - REGISTER_FULL_CLASS(COLUMN, Column_methods, Column_methods); + ELUA_REGISTER_CLASS(Column); return 1; } @@ -468,14 +470,14 @@ int Column_register(lua_State *L) { - +ELUA_CLASS_DEFINE(Columns,if (! *p) luaL_error(L,"expired columns")) static int Columns_tostring(lua_State *L) { lua_pushstring(L,"Columns"); return 1; } -static int Columns_newindex(lua_State *L) { +ELUA_METAMETHOD Columns_newindex(lua_State *L) { Columns cols = checkColumns(L,1); const struct col_names_t* cn; const char* colname; @@ -498,7 +500,7 @@ static int Columns_newindex(lua_State *L) { return 0; } -static int Columns_index(lua_State *L) { +ELUA_METAMETHOD Columns_index(lua_State *L) { Columns cols = checkColumns(L,1); const struct col_names_t* cn; const char* colname = luaL_checkstring(L,2); @@ -540,12 +542,13 @@ static const luaL_reg Columns_meta[] = { int Columns_register(lua_State *L) { - REGISTER_META(COLUMNS,Columns_meta); + ELUA_REGISTER_META(Columns); return 1; } -/* Pinfo class */ +ELUA_CLASS_DEFINE(Pinfo,if (! *p) luaL_error(L,"expired pinfo")) + static int Pinfo_tostring(lua_State *L) { lua_pushstring(L,"a Pinfo"); return 1; } #define PINFO_GET_NUMBER(name,val) static int name(lua_State *L) { \ @@ -801,7 +804,7 @@ static const luaL_reg Pinfo_meta[] = { }; int Pinfo_register(lua_State* L) { - REGISTER_META(PINFO,Pinfo_meta); + ELUA_REGISTER_META(Pinfo); outstanding_stuff = g_ptr_array_new(); return 1; } diff --git a/plugins/lua/lua_plugin.c b/plugins/lua/elua_plugin.c index b9b7caee2e..b9b7caee2e 100644 --- a/plugins/lua/lua_plugin.c +++ b/plugins/lua/elua_plugin.c diff --git a/plugins/lua/lua_proto.c b/plugins/lua/elua_proto.c index aaa73bf813..b13b52968a 100644 --- a/plugins/lua/lua_proto.c +++ b/plugins/lua/elua_proto.c @@ -26,20 +26,9 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "packet-lua.h" +#include "elua.h" -#define PREFS "Prefs" -#define PREF "Pref" -typedef eth_pref_t* Pref; -typedef eth_pref_t* Prefs; - -LUA_CLASS_DEFINE(ProtoField,PROTO_FIELD,if (! *p) luaL_error(L,"null ProtoField")) -LUA_CLASS_DEFINE(ProtoFieldArray,PROTO_FIELD_ARRAY,if (! *p) luaL_error(L,"null ProtoFieldArray")) -LUA_CLASS_DEFINE(Dissector,DISSECTOR,NOP) -LUA_CLASS_DEFINE(DissectorTable,DISSECTOR_TABLE,NOP) -LUA_CLASS_DEFINE(Pref,PREF,NOP) -LUA_CLASS_DEFINE(Prefs,PREFS,NOP) -LUA_CLASS_DEFINE(Proto,PROTO,NOP) +ELUA_CLASS_DEFINE(Pref,NOP) /* A preference of a Protocol. */ static int new_pref(lua_State* L, pref_type_t type) { const gchar* label = luaL_optstring(L,1,NULL); @@ -80,15 +69,15 @@ static int new_pref(lua_State* L, pref_type_t type) { } -static int Pref_bool(lua_State* L) { +ELUA_CONSTRUCTOR Pref_bool(lua_State* L) { return new_pref(L,PREF_BOOL); } -static int Pref_uint(lua_State* L) { +ELUA_CONSTRUCTOR Pref_uint(lua_State* L) { return new_pref(L,PREF_UINT); } -static int Pref_string(lua_State* L) { +ELUA_CONSTRUCTOR Pref_string(lua_State* L) { return new_pref(L,PREF_STRING); } @@ -105,37 +94,49 @@ static int Pref_gc(lua_State* L) { return 0; } -static const luaL_reg Pref_methods[] = { +ELUA_METHODS Pref_methods[] = { {"bool", Pref_bool}, {"uint", Pref_uint}, {"string", Pref_string}, {0,0} }; -static const luaL_reg Pref_meta[] = { +ELUA_META Pref_meta[] = { {"__gc", Pref_gc}, {0,0} }; -static int Pref_register(lua_State* L) { - REGISTER_FULL_CLASS(PREF, Pref_methods, Pref_meta); +ELUA_REGISTER Pref_register(lua_State* L) { + ELUA_REGISTER_CLASS(Pref); return 1; } +ELUA_CLASS_DEFINE(Prefs,NOP) /* The table of preferences of a protocol */ + +ELUA_METAMETHOD Prefs__newindex(lua_State* L) { /* creates a new preference */ +#define ELUA_ARG_Prefs__newindex_NAME 2 /* The abbreviation of this preference of */ +#define ELUA_ARG_Prefs__newindex_PREF 3 /* A valid still unassigned Pref object */ -static int Prefs_newindex(lua_State* L) { Pref prefs = checkPrefs(L,1); - const gchar* name = luaL_checkstring(L,2); - Pref pref = checkPref(L,3); + const gchar* name = luaL_checkstring(L,ELUA_ARG_Prefs__newindex_NAME); + Pref pref = checkPref(L,ELUA_ARG_Prefs__newindex_PREF); Pref p; - if (! ( name && prefs && pref) ) return 0; - - if (pref->name) { - luaL_error(L,"this preference has already been registered to another protocol"); - return 0; - } + + if (! prefs ) return 0; + + if (! name ) + ELUA_ARG_ERROR(Prefs__newindex,NAME,"must be a string"); + + if (! pref ) + ELUA_ARG_ERROR(Prefs__newindex,PREF,"must be a valid Pref"); + if (pref->name) + ELUA_ARG_ERROR(Prefs__newindex,NAME,"cannot change existing preference"); + + if (pref->proto) + ELUA_ARG_ERROR(Prefs__newindex,PREF,"cannot be added to more than one protocol"); + p = prefs; do { @@ -181,22 +182,23 @@ static int Prefs_newindex(lua_State* L) { &(pref->value.s)); break; default: - g_assert_not_reached(); - break; + ELUA_ERROR(Prefs__newindex,"unknow Pref type"); } pref->proto = p->proto; - return 0; + ELUA_RETURN(0); } } while (( p = p->next )); - g_assert_not_reached(); + luaL_error(L,"this should not happen!"); - return 0; + ELUA_FINAL_RETURN(0); } -static int Prefs_index(lua_State* L) { +ELUA_METAMETHOD Prefs__index(lua_State* L) { +#define ELUA_ARG_Prefs__index_NAME 2 /* The abbreviation of this preference */ + Pref prefs = checkPrefs(L,1); const gchar* name = luaL_checkstring(L,2); @@ -210,30 +212,29 @@ static int Prefs_index(lua_State* L) { case PREF_BOOL: lua_pushboolean(L, prefs->value.b); break; case PREF_UINT: lua_pushnumber(L,(lua_Number)prefs->value.u); break; case PREF_STRING: lua_pushstring(L,prefs->value.s); break; - default: g_assert_not_reached(); break; + default: ELUA_ERROR(Prefs__index,"unknow Pref type"); } - return 1; + ELUA_RETURN(1); /* the current value of the preference */ } } while (( prefs = prefs->next )); - luaL_error(L,"no such preference `%s'",name); - lua_pushnil(L); - return 1; + ELUA_ARG_ERROR(Prefs__index,NAME,"no preference named like this"); + ELUA_FINAL_RETURN(0); } -static const luaL_reg Prefs_meta[] = { - {"__newindex", Prefs_newindex}, - {"__index", Prefs_index}, +ELUA_META Prefs_meta[] = { + {"__newindex", Prefs__newindex}, + {"__index", Prefs__index}, {0,0} }; -static int Prefs_register(lua_State* L) { - REGISTER_META(PREFS, Prefs_meta); +ELUA_REGISTER Prefs_register(lua_State* L) { + ELUA_REGISTER_META(Prefs); return 1; } - +ELUA_CLASS_DEFINE(ProtoField,if (! *p) luaL_error(L,"null ProtoField")) /* * ProtoField class */ @@ -368,18 +369,26 @@ static value_string* value_string_from_table(lua_State* L, int idx) { } -static int ProtoField_new(lua_State* L) { +ELUA_CONSTRUCTOR ProtoField_new(lua_State* L) { /* Creates a new field to be used in a protocol. */ +#define ELUA_ARG_ProtoField_new_ABBR 1 /* abbreviated name of the field (the string used in filters). */ +#define ELUA_ARG_ProtoField_new_NAME 2 /* Actual name of the field (the string that appears in the tree). */ +#define ELUA_ARG_ProtoField_new_TYPE 3 /* Field Type (FT_*). */ +#define ELUA_OPTARG_ProtoField_new_VALUESTRING 5 /* a ValueString object. */ +#define ELUA_OPTARG_ProtoField_new_BASE 5 /* The representation BASE_*. */ +#define ELUA_OPTARG_ProtoField_new_MASK 6 /* the bitmask to be used. */ +#define ELUA_OPTARG_ProtoField_new_DESCR 7 /* The description of the field. */ + ProtoField f = g_malloc(sizeof(eth_field_t)); value_string* vs; /* will be using -2 as far as the field has not been added to an array then it will turn -1 */ f->hfid = -2; - f->name = g_strdup(luaL_checkstring(L,1)); - f->abbr = g_strdup(luaL_checkstring(L,2)); - f->type = get_ftenum(luaL_checkstring(L,3)); + f->name = g_strdup(luaL_checkstring(L,ELUA_ARG_ProtoField_new_ABBR)); + f->abbr = g_strdup(luaL_checkstring(L,ELUA_ARG_ProtoField_new_NAME)); + f->type = get_ftenum(luaL_checkstring(L,ELUA_ARG_ProtoField_new_TYPE)); if (f->type == FT_NONE) { - luaL_argerror(L, 3, "invalid FT_type"); + ELUA_ARG_ERROR(ProtoField_new,TYPE,"invalid FT_type"); return 0; } @@ -399,18 +408,16 @@ static int ProtoField_new(lua_State* L) { } /* XXX: need BASE_ERROR */ - f->base = string_to_base(luaL_optstring(L, 5, "BASE_NONE")); - f->mask = luaL_optint(L, 6, 0x0); - f->blob = g_strdup(luaL_optstring(L,7,"")); + f->base = string_to_base(luaL_optstring(L, ELUA_OPTARG_ProtoField_new_BASE, "BASE_NONE")); + f->mask = luaL_optint(L, ELUA_OPTARG_ProtoField_new_MASK, 0x0); + f->blob = g_strdup(luaL_optstring(L,ELUA_OPTARG_ProtoField_new_DESCR,"")); pushProtoField(L,f); - return 1; + ELUA_FINAL_RETURN(1); /* The newly created ProtoField object */ } - - static int ProtoField_integer(lua_State* L, enum ftenum type) { ProtoField f = g_malloc(sizeof(eth_field_t)); const gchar* abbr = luaL_checkstring(L,1); @@ -444,6 +451,23 @@ static int ProtoField_integer(lua_State* L, enum ftenum type) { #define PROTOFIELD_INTEGER(lower,FT) static int ProtoField_##lower(lua_State* L) { return ProtoField_integer(L,FT); } PROTOFIELD_INTEGER(uint8,FT_UINT8) +/* ELUA_SECTION Protofield integer constructors */ +/* ELUA_TEXT integer type ProtoField constructors use the following arguments */ +/* ELUA_ARG_DESC Protofield_integer ABBR abbreviated name of the field (the string used in filters) */ +/* ELUA_OPTARG_DESC Protofield_integer NAME Actual name of the field (the string that appears in the tree) */ +/* ELUA_ARGDESC Protofield_integer DESC description of the field */ +/* ELUA_RETURNS Protofield_integer a protofiled item to be added to a ProtoFieldArray */ +/* ELUA_CONSTRUCTOR ProtoField uint8 */ +/* ELUA_CONSTRUCTOR ProtoField uint16 */ +/* ELUA_CONSTRUCTOR ProtoField uint24 */ +/* ELUA_CONSTRUCTOR ProtoField uint32 */ +/* ELUA_CONSTRUCTOR ProtoField uint64 */ +/* ELUA_CONSTRUCTOR ProtoField int8 */ +/* ELUA_CONSTRUCTOR ProtoField int16 */ +/* ELUA_CONSTRUCTOR ProtoField int24 */ +/* ELUA_CONSTRUCTOR ProtoField int32 */ +/* ELUA_CONSTRUCTOR ProtoField int64 */ +/* ELUA_CONSTRUCTOR ProtoField framenum */ PROTOFIELD_INTEGER(uint16,FT_UINT16) PROTOFIELD_INTEGER(uint24,FT_UINT24) PROTOFIELD_INTEGER(uint32,FT_UINT32) @@ -567,7 +591,7 @@ int ProtoField_register(lua_State* L) { const eth_ft_types_t* ts; const struct base_display_string_t* b; -REGISTER_FULL_CLASS(PROTO_FIELD, ProtoField_methods, ProtoField_meta); +ELUA_REGISTER_CLASS(ProtoField); /* add a global FT_* variable for each FT_ type */ for (ts = ftenums; ts->str; ts++) { @@ -587,10 +611,7 @@ REGISTER_FULL_CLASS(PROTO_FIELD, ProtoField_methods, ProtoField_meta); } -/* - * ProtoFieldArray class - */ - +ELUA_CLASS_DEFINE(ProtoFieldArray,if (! *p) luaL_error(L,"null ProtoFieldArray")) static int ProtoFieldArray_new(lua_State* L) { ProtoFieldArray fa; @@ -690,18 +711,13 @@ static const luaL_reg ProtoFieldArray_meta[] = { }; int ProtoFieldArray_register(lua_State* L) { - REGISTER_FULL_CLASS(PROTO_FIELD_ARRAY, ProtoFieldArray_methods, ProtoFieldArray_meta); + ELUA_REGISTER_CLASS(ProtoFieldArray); return 1; } - - -/* - * Proto class - */ - +ELUA_CLASS_DEFINE(Proto,NOP) static int Proto_new(lua_State* L) { const gchar* name = luaL_checkstring(L,1); @@ -938,13 +954,13 @@ static const luaL_reg Proto_meta[] = { int Proto_register(lua_State* L) { - REGISTER_META(PROTO, Proto_meta); + ELUA_REGISTER_META(Proto); lua_pushstring(L, "register_postdissector"); lua_pushcfunction(L, Proto_register_postdissector); lua_settable(L, LUA_GLOBALSINDEX); - lua_pushstring(L, PROTO); + lua_pushstring(L, "Proto"); lua_pushcfunction(L, Proto_new); lua_settable(L, LUA_GLOBALSINDEX); @@ -954,9 +970,7 @@ int Proto_register(lua_State* L) { return 1; } -/* - * Dissector class - */ +ELUA_CLASS_DEFINE(Dissector,NOP) static int Dissector_get (lua_State *L) { const gchar* name = luaL_checkstring(L,1); @@ -1015,14 +1029,13 @@ static const luaL_reg Dissector_meta[] = { }; int Dissector_register(lua_State* L) { - REGISTER_FULL_CLASS(DISSECTOR, Dissector_methods, Dissector_meta); + ELUA_REGISTER_CLASS(Dissector); return 1; } -/* - * DissectorTable class - */ +ELUA_CLASS_DEFINE(DissectorTable,NOP) + static int DissectorTable_new (lua_State *L) { gchar* name = (void*)luaL_checkstring(L,1); gchar* ui_name = (void*)luaL_optstring(L,2,name); @@ -1102,7 +1115,7 @@ static int DissectorTable_add (lua_State *L) { } else if ( isDissector(L,3) ) { handle = toDissector(L,3); } else { - luaL_argerror(L,3,"Must be either " PROTO " or " DISSECTOR ); + luaL_argerror(L,3,"Must be either Proto or Dissector" ); return 0; } @@ -1129,10 +1142,10 @@ static int DissectorTable_remove (lua_State *L) { if (!dt) return 0; - if(( p = luaL_checkudata(L,3,PROTO) )) { + if(( p = luaL_checkudata(L,3,"Proto") )) { handle = p->handle; - } else if (! ( handle = luaL_checkudata(L,3,DISSECTOR) )) { - luaL_argerror(L,3,"Must be either " PROTO " or " DISSECTOR ); + } else if (! ( handle = luaL_checkudata(L,3,"Dissector") )) { + luaL_argerror(L,3,"Must be either Proto or Dissector" ); return 0; } @@ -1269,7 +1282,7 @@ static const luaL_reg DissectorTable_meta[] = { }; int DissectorTable_register(lua_State* L) { - REGISTER_FULL_CLASS(DISSECTOR_TABLE, DissectorTable_methods, DissectorTable_meta); + ELUA_REGISTER_CLASS(DissectorTable); return 1; } diff --git a/plugins/lua/lua_tap.c b/plugins/lua/elua_tap.c index a1cef0f71f..de4a0934fc 100644 --- a/plugins/lua/lua_tap.c +++ b/plugins/lua/elua_tap.c @@ -26,10 +26,9 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "packet-lua.h" +#include "elua.h" -LUA_CLASS_DEFINE(Tap,TAP,NOP) -LUA_CLASS_DEFINE(Field,FIELD,NOP) +ELUA_CLASS_DEFINE(Field,NOP) static GPtrArray* wanted_fields = NULL; @@ -201,7 +200,7 @@ int Field_register(lua_State* L) { wanted_fields = g_ptr_array_new(); - REGISTER_META(FIELD, Field_meta); + ELUA_REGISTER_META(Field); lua_pushstring(L, "Field"); lua_pushcfunction(L, Field_get); @@ -212,10 +211,7 @@ int Field_register(lua_State* L) { - -/* - * Tap - */ +ELUA_CLASS_DEFINE(Tap,NOP) struct _eth_tap { gchar* name; @@ -233,12 +229,15 @@ int tap_packet_cb_error_handler(lua_State* L) { static gchar* last_error = NULL; static int repeated = 0; static int next = 2; - + const gchar* where = (lua_pinfo) ? + ep_strdup_printf("Lua: on packet %i Error During execution of Tap Packet Callback",lua_pinfo->fd->num) : + ep_strdup_printf("Lua: Error During execution of Tap Packet Callback") ; + /* show the error the 1st, 3rd, 5th, 9th, 17th, 33th... time it appears to avoid window flooding */ /* XXX the last series of identical errors won't be shown (the user however gets at least one message) */ if (! last_error) { - report_failure("Lua: on packet %i Error During execution of Tap Packet Callback:\n %s",lua_pinfo->fd->num,error); + report_failure("%s:\n%s",where,error); last_error = g_strdup(error); repeated = 0; next = 2; @@ -248,16 +247,16 @@ int tap_packet_cb_error_handler(lua_State* L) { if (g_str_equal(last_error,error) ) { repeated++; if ( repeated == next ) { - report_failure("Lua: on packet %i Error During execution of Tap Packet Callback happened %i times:\n %s",lua_pinfo->fd->num,repeated,error); + report_failure("%s happened %i times:\n %s",where,repeated,error); next *= 2; } } else { - report_failure("Lua: on packet %i Error During execution of Tap Packet Callback happened %i times:\n %s",lua_pinfo->fd->num,repeated,last_error); + report_failure("%s happened %i times:\n %s",where,repeated,last_error); g_free(last_error); last_error = g_strdup(error); repeated = 0; next = 2; - report_failure("Lua: on packet %i Error During execution of Tap Packet Callback:\n %s",lua_pinfo->fd->num,error); + report_failure("%s:\n %s",where,error); } return 0; @@ -316,7 +315,7 @@ int lua_tap_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const int tap_reset_cb_error_handler(lua_State* L) { const gchar* error = lua_tostring(L,1); report_failure("Lua: Error During execution of Tap init Callback:\n %s",error); - return 0; + return 1; } void lua_tap_reset(void *tapdata) { @@ -346,7 +345,7 @@ void lua_tap_reset(void *tapdata) { int tap_draw_cb_error_handler(lua_State* L) { const gchar* error = lua_tostring(L,1); report_failure("Lua: Error During execution of Tap Draw Callback:\n %s",error); - return 0; + return 1; } void lua_tap_draw(void *tapdata) { @@ -475,11 +474,15 @@ static const luaL_reg Tap_meta[] = { }; int Tap_register(lua_State* L) { - REGISTER_META(TAP, Tap_meta); + ELUA_REGISTER_META(Tap); lua_pushstring(L, "Tap"); lua_pushcfunction(L, Tap_new); lua_settable(L, LUA_GLOBALSINDEX); + + lua_pushstring(L, "tap_remove"); + lua_pushcfunction(L, Tap_remove); + lua_settable(L, LUA_GLOBALSINDEX); return 1; } diff --git a/plugins/lua/lua_tree.c b/plugins/lua/elua_tree.c index 178c21a9f6..4b3f2e1da2 100644 --- a/plugins/lua/lua_tree.c +++ b/plugins/lua/elua_tree.c @@ -26,13 +26,9 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "packet-lua.h" +#include "elua.h" #include <epan/expert.h> -LUA_CLASS_DEFINE(ProtoTree,PROTO_TREE,NOP) -LUA_CLASS_DEFINE(ProtoItem,ITEM,NOP) -LUA_CLASS_DEFINE(SubTree,SUBTREE,NOP) - static GPtrArray* outstanding_stuff = NULL; #define PUSH_PROTOITEM(L,i) g_ptr_array_add(outstanding_stuff,pushProtoItem(L,i)) @@ -50,10 +46,7 @@ void clear_outstanding_trees(void) { } } -/* - * SubTree class - */ - +ELUA_CLASS_DEFINE(SubTree,NOP) static GArray* lua_etts = NULL; static gint lua_ett = -1; @@ -110,10 +103,11 @@ static const luaL_reg SubTree_meta[] = { }; int SubTree_register(lua_State* L) { - REGISTER_FULL_CLASS(SUBTREE, SubTree_methods, SubTree_meta); + ELUA_REGISTER_CLASS(SubTree); return 1; } +ELUA_CLASS_DEFINE(ProtoTree,NOP) /* ProtoTree class */ static int ProtoTree_add_item_any(lua_State *L, gboolean little_endian) { @@ -279,11 +273,12 @@ static const luaL_reg ProtoTree_meta[] = { }; int ProtoTree_register(lua_State* L) { - REGISTER_FULL_CLASS(PROTO_TREE, ProtoTree_methods, ProtoTree_meta); + ELUA_REGISTER_CLASS(ProtoTree); return 1; } -/* ProtoItem class */ +ELUA_CLASS_DEFINE(ProtoItem,NOP) + static int ProtoItem_tostring(lua_State *L) { ProtoItem item = checkProtoItem(L,1); lua_pushstring(L,ep_strdup_printf("ProtoItem %p",item)); @@ -294,7 +289,7 @@ static int ProtoItem_add_subtree(lua_State *L) { ProtoItem item = checkProtoItem(L,1); if (item) { - SubTree* ett = luaL_checkudata(L,2,SUBTREE); + SubTree* ett = luaL_checkudata(L,2,"SubTree"); ProtoTree tree; if (ett && *ett) { @@ -461,7 +456,7 @@ static const luaL_reg ProtoItem_meta[] = { int ProtoItem_register(lua_State *L) { const struct _expert_severity* s; - REGISTER_FULL_CLASS(ITEM, ProtoItem_methods, ProtoItem_meta); + ELUA_REGISTER_CLASS(ProtoItem); outstanding_stuff = g_ptr_array_new(); for(s = severities; s->str; s++) { diff --git a/plugins/lua/lua_tvb.c b/plugins/lua/elua_tvb.c index 9fab0db625..49a85e3628 100644 --- a/plugins/lua/lua_tvb.c +++ b/plugins/lua/elua_tvb.c @@ -26,11 +26,12 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "packet-lua.h" +#include "elua.h" -LUA_CLASS_DEFINE(ByteArray,BYTE_ARRAY,if (! *p) luaL_argerror(L,index,"null bytearray")) +ELUA_CLASS_DEFINE(ByteArray,if (! *p) luaL_argerror(L,index,"null bytearray")) -static int ByteArray_new(lua_State* L) { +ELUA_CONSTRUCTOR ByteArray_new(lua_State* L) { /* creates a ByteArray Object */ +#define ELUA_OPTARG_ByteArray_new_HEXBYTES 1 /* A string consisting of hexadecimal bytes like "00 B1 A2" or "1a2b3c4d" */ GByteArray* ba = g_byte_array_new(); const gchar* s; int nibble[2]; @@ -38,10 +39,10 @@ static int ByteArray_new(lua_State* L) { gchar c; if (lua_gettop(L) == 1) { - s = luaL_checkstring(L,1); + s = luaL_checkstring(L,ELUA_OPTARG_ByteArray_new_HEXBYTES); if (!s) { - luaL_argerror(L,1,"not a string"); + ELUA_OPTARG_ERROR(ByteArray_new,HEXBYTES,"must be a string"); return 0; } @@ -71,7 +72,7 @@ static int ByteArray_new(lua_State* L) { pushByteArray(L,ba); - return 1; + ELUA_FINAL_RETURN(1); /* The new ByteArray object. */ } static int ByteArray_gc(lua_State* L) { @@ -83,27 +84,50 @@ static int ByteArray_gc(lua_State* L) { return 0; } -static int ByteArray_append(lua_State* L) { +ELUA_METAMETHOD ByteArray__concat(lua_State* L) { +#define ELUA_ARG_ByteArray__cat_FIRST 1 +#define ELUA_ARG_ByteArray__cat_SECOND 1 + ByteArray ba = checkByteArray(L,1); ByteArray ba2 = checkByteArray(L,2); + if (! (ba && ba2) ) + ELUA_ERROR(ByteArray__cat,"both arguments must be ByteArrays"); + g_byte_array_append(ba,ba2->data,ba2->len); pushByteArray(L,ba); + ELUA_FINAL_RETURN(1); /* The new composite ByteArray. */ +} + +ELUA_METHOD ByteArray_prepend(lua_State* L) { +#define ELUA_ARG_ByteArray_prepend_BYTES 1 + ByteArray ba = checkByteArray(L,1); + ByteArray ba2 = checkByteArray(L,2); + + if (! (ba && ba2) ) + ELUA_ERROR(ByteArray_prepend,"both arguments must be ByteArrays"); + + g_byte_array_prepend(ba,ba2->data,ba2->len); + + pushByteArray(L,ba); return 1; } -static int ByteArray_prepend(lua_State* L) { +ELUA_METHOD ByteArray_append(lua_State* L) { ByteArray ba = checkByteArray(L,1); ByteArray ba2 = checkByteArray(L,2); + if (! (ba && ba2) ) + ELUA_ERROR(ByteArray_prepend,"both arguments must be ByteArrays"); + g_byte_array_prepend(ba,ba2->data,ba2->len); pushByteArray(L,ba); return 1; } -static int ByteArray_set_size(lua_State* L) { +ELUA_METHOD ByteArray_set_size(lua_State* L) { ByteArray ba = checkByteArray(L,1); int siz = luaL_checkint(L,2); @@ -113,7 +137,7 @@ static int ByteArray_set_size(lua_State* L) { return 0; } -static int ByteArray_set_index(lua_State* L) { +ELUA_METHOD ByteArray_set_index(lua_State* L) { ByteArray ba = checkByteArray(L,1); int idx = luaL_checkint(L,2); int v = luaL_checkint(L,3); @@ -141,7 +165,7 @@ static int ByteArray_set_index(lua_State* L) { } -static int ByteArray_get_index(lua_State* L) { +ELUA_METHOD ByteArray_get_index(lua_State* L) { ByteArray ba = checkByteArray(L,1); int idx = luaL_checkint(L,2); @@ -161,7 +185,7 @@ static int ByteArray_get_index(lua_State* L) { return 1; } -static int ByteArray_len(lua_State* L) { +ELUA_METHOD ByteArray_len(lua_State* L) { ByteArray ba = checkByteArray(L,1); if (!ba) return 0; @@ -171,7 +195,7 @@ static int ByteArray_len(lua_State* L) { return 1; } -static int ByteArray_subset(lua_State* L) { +ELUA_METHOD ByteArray_subset(lua_State* L) { ByteArray ba = checkByteArray(L,1); int offset = luaL_checkint(L,2); int len = luaL_checkint(L,3); @@ -247,13 +271,13 @@ static const luaL_reg ByteArray_methods[] = { static const luaL_reg ByteArray_meta[] = { {"__tostring", ByteArray_tostring}, {"__gc", ByteArray_gc}, - {"__concat", ByteArray_append}, + {"__concat", ByteArray__concat}, {"__call",ByteArray_subset}, {0, 0} }; int ByteArray_register(lua_State* L) { - REGISTER_FULL_CLASS(BYTE_ARRAY, ByteArray_methods, ByteArray_methods); + ELUA_REGISTER_CLASS(ByteArray); return 1; } @@ -277,8 +301,7 @@ int ByteArray_register(lua_State* L) { * and report an error to the lua machine if it happens to be NULLified. */ -LUA_CLASS_DEFINE(Tvb,TVB,if (! *p) luaL_error(L,"expired tvb")) -LUA_CLASS_DEFINE(TvbRange,TVB_RANGE,if (! *p) luaL_error(L,"expired tvbrange")) +ELUA_CLASS_DEFINE(Tvb,if (! *p) luaL_error(L,"expired tvb")) static GPtrArray* outstanding_stuff = NULL; @@ -401,7 +424,7 @@ static const luaL_reg Tvb_meta[] = { }; int Tvb_register(lua_State* L) { - REGISTER_FULL_CLASS(TVB, Tvb_methods, Tvb_meta); + ELUA_REGISTER_CLASS(Tvb); return 1; } @@ -452,6 +475,7 @@ static int Tvb_range(lua_State* L) { } +ELUA_CLASS_DEFINE(TvbRange,if (! *p) luaL_error(L,"expired tvbrange")) /* * read access to tvbr's data @@ -708,6 +732,6 @@ static const luaL_reg TvbRange_meta[] = { int TvbRange_register(lua_State* L) { outstanding_stuff = g_ptr_array_new(); - REGISTER_FULL_CLASS(TVB_RANGE, TvbRange_methods, TvbRange_meta); + ELUA_REGISTER_CLASS(TvbRange); return 1; } |