diff options
Diffstat (limited to 'epan/wslua')
-rw-r--r-- | epan/wslua/init_wslua.c | 65 | ||||
-rw-r--r-- | epan/wslua/wslua_dumper.c | 229 | ||||
-rw-r--r-- | epan/wslua/wslua_field.c | 134 | ||||
-rw-r--r-- | epan/wslua/wslua_gui.c | 636 | ||||
-rw-r--r-- | epan/wslua/wslua_listener.c | 72 | ||||
-rw-r--r-- | epan/wslua/wslua_pinfo.c | 221 | ||||
-rw-r--r-- | epan/wslua/wslua_proto.c | 299 | ||||
-rw-r--r-- | epan/wslua/wslua_tree.c | 92 | ||||
-rw-r--r-- | epan/wslua/wslua_tvb.c | 368 | ||||
-rw-r--r-- | epan/wslua/wslua_util.c | 506 |
10 files changed, 1050 insertions, 1572 deletions
diff --git a/epan/wslua/init_wslua.c b/epan/wslua/init_wslua.c index e83764aad4..5c59cc95bc 100644 --- a/epan/wslua/init_wslua.c +++ b/epan/wslua/init_wslua.c @@ -47,8 +47,7 @@ wslua_plugin *wslua_plugin_list = NULL; dissector_handle_t lua_data_handle; -static void -lua_frame_end(void) +static void lua_frame_end(void) { clear_outstanding_Tvb(); clear_outstanding_Pinfo(); @@ -57,15 +56,12 @@ lua_frame_end(void) clear_outstanding_TreeItem(); } -static int -wslua_not_register_menu(lua_State* LS) { +static int wslua_not_register_menu(lua_State* LS) { luaL_error(LS,"too late to register a menu"); return 0; } -int -dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) -{ +int dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) { int consumed_bytes = tvb->length; lua_pinfo = pinfo; lua_tvb = tvb; @@ -129,9 +125,7 @@ dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) } -static void -iter_table_and_call(lua_State* LS, int env, const gchar* table_name, lua_CFunction error_handler) -{ +static void iter_table_and_call(lua_State* LS, int env, const gchar* table_name, lua_CFunction error_handler) { lua_settop(LS,0); lua_pushcfunction(LS,error_handler); @@ -168,18 +162,14 @@ iter_table_and_call(lua_State* LS, int env, const gchar* table_name, lua_CFuncti } -static int -init_error_handler(lua_State* LS) -{ +static int init_error_handler(lua_State* LS) { const gchar* error = lua_tostring(LS,1); report_failure("Lua: Error During execution of Initialization:\n %s",error); return 0; } -static void -wslua_init_routine(void) -{ +static void wslua_init_routine(void) { static gboolean initialized = FALSE; if ( ! initialized ) { @@ -194,8 +184,7 @@ wslua_init_routine(void) } -static const char * -getF(lua_State *LS _U_, void *ud, size_t *size) +static const char *getF(lua_State *LS _U_, void *ud, size_t *size) { FILE *f=(FILE *)ud; static char buff[512]; @@ -204,16 +193,13 @@ getF(lua_State *LS _U_, void *ud, size_t *size) return (*size>0) ? buff : NULL; } -static int -lua_main_error_handler(lua_State* LS) -{ +static int lua_main_error_handler(lua_State* LS) { const gchar* error = lua_tostring(LS,1); report_failure("Lua: Error during loading:\n %s",error); return 0; } -static void -wslua_add_plugin(gchar *name, gchar *version) +static void wslua_add_plugin(gchar *name, gchar *version) { wslua_plugin *new_plug, *lua_plug; @@ -234,9 +220,7 @@ wslua_add_plugin(gchar *name, gchar *version) new_plug->next = NULL; } -static gboolean -lua_load_script(const gchar* filename) -{ +static gboolean lua_load_script(const gchar* filename) { FILE* file; int error; @@ -271,22 +255,19 @@ lua_load_script(const gchar* filename) return FALSE; } -static void -basic_logger(const gchar *log_domain _U_, GLogLevelFlags log_level _U_, - const gchar *message, gpointer user_data _U_) -{ - fputs(message, stderr); +static void basic_logger(const gchar *log_domain _U_, + GLogLevelFlags log_level _U_, + const gchar *message, + gpointer user_data _U_) { + fputs(message,stderr); } -static int -wslua_panic(lua_State* LS) -{ - g_error("LUA PANIC: %s", lua_tostring(LS,-1)); +static int wslua_panic(lua_State* LS) { + g_error("LUA PANIC: %s",lua_tostring(LS,-1)); return 0; } -static void -lua_load_plugins (const char *dirname) +static void lua_load_plugins (const char *dirname) { WS_DIR *dir; /* scanned directory */ WS_DIRENT *file; /* current file */ @@ -333,9 +314,7 @@ lua_load_plugins (const char *dirname) } } -int -wslua_init(lua_State* LS) -{ +int wslua_init(lua_State* LS) { gchar* filename; const funnel_ops_t* ops = funnel_get_funnel_ops(); gboolean run_anyway = FALSE; @@ -453,9 +432,5 @@ wslua_init(lua_State* LS) return 0; } -lua_State* -wslua_state() -{ - return L; -} +lua_State* wslua_state() { return L; } diff --git a/epan/wslua/wslua_dumper.c b/epan/wslua/wslua_dumper.c index 568ce1a436..9fffd5406e 100644 --- a/epan/wslua/wslua_dumper.c +++ b/epan/wslua/wslua_dumper.c @@ -31,10 +31,10 @@ #include "wslua.h" #include <math.h> +WSLUA_CLASS_DEFINE(PseudoHeader,NOP,NOP); /* - * A pseudoheader to be used to save captured frames. + A pseudoheader to be used to save captured frames. */ -WSLUA_CLASS_DEFINE(PseudoHeader,NOP,NOP); enum lua_pseudoheader_type { PHDR_NONE, @@ -54,31 +54,28 @@ enum lua_pseudoheader_type { struct lua_pseudo_header { enum lua_pseudoheader_type type; - union wtap_pseudo_header *wph; + union wtap_pseudo_header* wph; }; -WSLUA_CONSTRUCTOR -PseudoHeader_none(lua_State *L) -{ - /* - * Creates a "no" pseudoheader. - * - */ +WSLUA_CONSTRUCTOR PseudoHeader_none(lua_State* L) { + /* + Creates a "no" pseudoheader. + */ PseudoHeader ph = g_malloc(sizeof(struct lua_pseudo_header)); ph->type = PHDR_NONE; ph->wph = NULL; pushPseudoHeader(L,ph); - WSLUA_RETURN(1); /* A null pseudoheader */ + + WSLUA_RETURN(1); + /* A null pseudoheader */ } -WSLUA_CONSTRUCTOR -PseudoHeader_eth(lua_State *L) -{ - /* - * Creates an ethernet pseudoheader - */ +WSLUA_CONSTRUCTOR PseudoHeader_eth(lua_State* L) { + /* + Creates an ethernet pseudoheader + */ #define WSLUA_OPTARG_PseudoHeader_eth_FCSLEN 1 /* The fcs length */ @@ -89,15 +86,13 @@ PseudoHeader_eth(lua_State *L) pushPseudoHeader(L,ph); - WSLUA_RETURN(1); /* The ethernet pseudoheader */ + WSLUA_RETURN(1); /* The ethernet pseudoheader */ } -WSLUA_CONSTRUCTOR -PseudoHeader_atm(lua_State *L) -{ - /* - * Creates an ATM pseudoheader - */ +WSLUA_CONSTRUCTOR PseudoHeader_atm(lua_State* L) { + /* + Creates an ATM pseudoheader + */ #define WSLUA_OPTARG_PseudoHeader_atm_AAL 1 /* AAL number */ #define WSLUA_OPTARG_PseudoHeader_atm_VPI 2 /* VPI */ #define WSLUA_OPTARG_PseudoHeader_atm_VCI 3 /* VCI */ @@ -118,13 +113,12 @@ PseudoHeader_atm(lua_State *L) ph->wph->atm.aal5t_len = luaL_optint(L,WSLUA_OPTARG_PseudoHeader_atm_AAL5LEN,0); pushPseudoHeader(L,ph); - WSLUA_RETURN(1); /* The ATM pseudoheader */ + WSLUA_RETURN(1); + /* The ATM pseudoheader */ } -WSLUA_CONSTRUCTOR -PseudoHeader_mtp2(lua_State *L) -{ - /* Creates an MTP2 PseudoHeader */ +WSLUA_CONSTRUCTOR PseudoHeader_mtp2(lua_State* L) { + /* Creates an MTP2 PseudoHeader */ #define WSLUA_OPTARG_PseudoHeader_mtp2_SENT 1 /* True if the packet is sent, False if received. */ #define WSLUA_OPTARG_PseudoHeader_mtp2_ANNEXA 2 /* True if annex A is used */ #define WSLUA_OPTARG_PseudoHeader_mtp2_LINKNUM 3 /* Link Number */ @@ -136,18 +130,18 @@ PseudoHeader_mtp2(lua_State *L) ph->wph->mtp2.link_number = luaL_optint(L,WSLUA_OPTARG_PseudoHeader_mtp2_LINKNUM,0); pushPseudoHeader(L,ph); - WSLUA_RETURN(1); /* The MTP2 pseudoheader */ + WSLUA_RETURN(1); /* The MTP2 pseudoheader */ } #if 0 -static int PseudoHeader_x25(lua_State *L) { luaL_error(L,"not implemented"); return 0; } -static int PseudoHeader_isdn(lua_State *L) { luaL_error(L,"not implemented"); return 0; } -static int PseudoHeader_ascend(lua_State *L) { luaL_error(L,"not implemented"); return 0; } -static int PseudoHeader_wifi(lua_State *L) { luaL_error(L,"not implemented"); return 0; } -static int PseudoHeader_cosine(lua_State *L) { luaL_error(L,"not implemented"); return 0; } -static int PseudoHeader_irda(lua_State *L) { luaL_error(L,"not implemented"); return 0; } -static int PseudoHeader_nettl(lua_State *L) { luaL_error(L,"not implemented"); return 0; } -static int PseudoHeader_k12(lua_State *L) { luaL_error(L,"not implemented"); return 0; } +static int PseudoHeader_x25(lua_State* L) { luaL_error(L,"not implemented"); return 0; } +static int PseudoHeader_isdn(lua_State* L) { luaL_error(L,"not implemented"); return 0; } +static int PseudoHeader_ascend(lua_State* L) { luaL_error(L,"not implemented"); return 0; } +static int PseudoHeader_wifi(lua_State* L) { luaL_error(L,"not implemented"); return 0; } +static int PseudoHeader_cosine(lua_State* L) { luaL_error(L,"not implemented"); return 0; } +static int PseudoHeader_irda(lua_State* L) { luaL_error(L,"not implemented"); return 0; } +static int PseudoHeader_nettl(lua_State* L) { luaL_error(L,"not implemented"); return 0; } +static int PseudoHeader_k12(lua_State* L) { luaL_error(L,"not implemented"); return 0; } #endif WSLUA_METHODS PseudoHeader_methods[] = { @@ -162,28 +156,24 @@ WSLUA_META PseudoHeader_meta[] = { {0,0} }; -int -PseudoHeader_register(lua_State *L) -{ - WSLUA_REGISTER_CLASS(PseudoHeader) +int PseudoHeader_register(lua_State* L) { + WSLUA_REGISTER_CLASS(PseudoHeader) return 0; } WSLUA_CLASS_DEFINE(Dumper,FAIL_ON_NULL("Dumper already closed"),NOP); -static GHashTable *dumper_encaps = NULL; +static GHashTable* dumper_encaps = NULL; #define DUMPER_ENCAP(d) GPOINTER_TO_INT(g_hash_table_lookup(dumper_encaps,d)) -static const char* -cross_plat_fname(const char *fname) -{ +static const char* cross_plat_fname(const char* fname) { static char fname_clean[256]; - char *f; + char* f; strncpy(fname_clean,fname,256); - for (f = fname_clean; *f; f++) { + for(f = fname_clean; *f; f++) { switch(*f) { case '/': case '\\': *f = *(G_DIR_SEPARATOR_S); @@ -196,26 +186,24 @@ cross_plat_fname(const char *fname) return fname_clean; } -WSLUA_CONSTRUCTOR -Dumper_new(lua_State *L) -{ - /* - * Creates a file to write packets. - * Dumper:new_for_current() will probably be a better choice. - */ +WSLUA_CONSTRUCTOR Dumper_new(lua_State* L) { + /* + Creates a file to write packets. + Dumper:new_for_current() will probably be a better choice. + */ #define WSLUA_ARG_Dumper_new_FILENAME 1 /* The name of the capture file to be created */ #define WSLUA_OPTARG_Dumper_new_FILETYPE 2 /* The type of the file to be created */ #define WSLUA_OPTARG_Dumper_new_ENCAP 3 /* The encapsulation to be used in the file to be created */ Dumper d; - const char *fname = luaL_checkstring(L,WSLUA_ARG_Dumper_new_FILENAME); + const char* fname = luaL_checkstring(L,WSLUA_ARG_Dumper_new_FILENAME); int filetype = luaL_optint(L,WSLUA_OPTARG_Dumper_new_FILETYPE,WTAP_FILE_PCAP); int encap = luaL_optint(L,WSLUA_OPTARG_Dumper_new_ENCAP,WTAP_ENCAP_ETHERNET); int err = 0; - const char *filename; + const char* filename; if (! fname) return 0; - filename = cross_plat_fname(fname); + filename = cross_plat_fname(fname); if (!wtap_dump_can_write_encap(filetype, encap)) WSLUA_ERROR(Dumper_new,"Not every filetype handles every encap"); @@ -223,8 +211,9 @@ Dumper_new(lua_State *L) d = wtap_dump_open(filename, filetype, encap,0 , FALSE, &err); if (! d ) { - /* WSLUA_ERROR("Error while opening file for writing"); */ - luaL_error(L,"error while opening `%s': %s", filename, + /* WSLUA_ERROR("Error while opening file for writing"); */ + luaL_error(L,"error while opening `%s': %s", + filename, wtap_strerror(err)); return 0; } @@ -232,37 +221,35 @@ Dumper_new(lua_State *L) g_hash_table_insert(dumper_encaps,d,GINT_TO_POINTER(encap)); pushDumper(L,d); - WSLUA_RETURN(1); /* The newly created Dumper object */ + WSLUA_RETURN(1); + /* The newly created Dumper object */ } -WSLUA_METHOD -Dumper_close(lua_State *L) -{ - /* Closes a dumper */ - Dumper *dp = (Dumper*)luaL_checkudata(L, 1, "Dumper"); +WSLUA_METHOD Dumper_close(lua_State* L) { + /* Closes a dumper */ + Dumper* dp = (Dumper*)luaL_checkudata(L, 1, "Dumper"); int err; if (! *dp) - WSLUA_ERROR(Dumper_close,"Cannot operate on a closed dumper"); + WSLUA_ERROR(Dumper_close,"Cannot operate on a closed dumper"); - g_hash_table_remove(dumper_encaps, *dp); + g_hash_table_remove(dumper_encaps,*dp); if (!wtap_dump_close(*dp, &err)) { - luaL_error(L,"error closing: %s", wtap_strerror(err)); + luaL_error(L,"error closing: %s", + wtap_strerror(err)); } - /* this way if we close a dumper any attempt to use it (for everything but GC) will yield an error */ - *dp = NULL; + /* this way if we close a dumper any attempt to use it (for everything but GC) will yield an error */ + *dp = NULL; return 0; } -WSLUA_METHOD -Dumper_flush(lua_State *L) -{ - /* - * Writes all unsaved data of a dumper to the disk. - */ +WSLUA_METHOD Dumper_flush(lua_State* L) { + /* + Writes all unsaved data of a dumper to the disk. + */ Dumper d = checkDumper(L,1); if (!d) return 0; @@ -272,18 +259,16 @@ Dumper_flush(lua_State *L) return 0; } -WSLUA_METHOD -Dumper_dump(lua_State *L) -{ - /* - * Dumps an arbitrary packet. - * Note: Dumper:dump_current() will fit best in most cases. - */ +WSLUA_METHOD Dumper_dump(lua_State* L) { + /* + Dumps an arbitrary packet. + Note: Dumper:dump_current() will fit best in most cases. + */ #define WSLUA_ARG_Dumper_dump_TIMESTAMP 2 /* The absolute timestamp the packet will have */ #define WSLUA_ARG_Dumper_dump_PSEUDOHEADER 3 /* The Pseudoheader to use. */ #define WSLUA_ARG_Dumper_dump_BYTEARRAY 4 /* the data to be saved */ - Dumper d = checkDumper(L,1); + Dumper d = checkDumper(L,1); PseudoHeader ph; ByteArray ba; struct wtap_pkthdr pkthdr; @@ -299,42 +284,42 @@ Dumper_dump(lua_State *L) ba = checkByteArray(L,WSLUA_ARG_Dumper_dump_BYTEARRAY); - if (! ba) WSLUA_ARG_ERROR(Dumper_dump,BYTEARRAY,"must be a ByteArray"); + if (! ba) WSLUA_ARG_ERROR(Dumper_dump,BYTEARRAY,"must be a ByteArray"); - pkthdr.ts.secs = (unsigned)floor(ts); - pkthdr.ts.nsecs = (unsigned)floor((ts - (double)pkthdr.ts.secs) * 1000000000); + pkthdr.ts.secs = (unsigned)floor(ts); + pkthdr.ts.nsecs = (unsigned)floor((ts - (double)pkthdr.ts.secs) * 1000000000); pkthdr.len = ba->len; pkthdr.caplen = ba->len; pkthdr.pkt_encap = DUMPER_ENCAP(d); if (! wtap_dump(d, &pkthdr, ph->wph, ba->data, &err)) { - luaL_error(L,"error while dumping: %s", wtap_strerror(err)); + luaL_error(L,"error while dumping: %s", + wtap_strerror(err)); } return 0; + } -WSLUA_METHOD -Dumper_new_for_current(lua_State *L) -{ - /* - * Creates a capture file using the same encapsulation as the one of the cuurrent packet - */ +WSLUA_METHOD Dumper_new_for_current(lua_State* L) { + /* + Creates a capture file using the same encapsulation as the one of the cuurrent packet + */ #define WSLUA_OPTARG_Dumper_new_for_current_FILETYPE 2 /* The file type. Defaults to pcap. */ - Dumper d; - const char *fname = luaL_checkstring(L,1); + Dumper d; + const char* fname = luaL_checkstring(L,1); int filetype = luaL_optint(L,WSLUA_OPTARG_Dumper_new_for_current_FILETYPE,WTAP_FILE_PCAP); int encap; int err = 0; - const char *filename; + const char* filename; if (! fname) return 0; - filename = cross_plat_fname(fname); + filename = cross_plat_fname(fname); if (! lua_pinfo ) - WSLUA_ERROR(Dumper_new_for_current,"Cannot be used outside a tap or a dissector"); + WSLUA_ERROR(Dumper_new_for_current,"Cannot be used outside a tap or a dissector"); encap = lua_pinfo->fd->lnk_t; @@ -347,32 +332,32 @@ Dumper_new_for_current(lua_State *L) d = wtap_dump_open(filename, filetype, encap, 0 , FALSE, &err); - if (! d) { + if (! d ) { luaL_error(L,"error while opening `%s': %s", - filename, wtap_strerror(err)); + filename, + wtap_strerror(err)); return 0; } pushDumper(L,d); WSLUA_RETURN(1); /* The newly created Dumper Object */ + } -WSLUA_METHOD -Dumper_dump_current(lua_State *L) -{ - /* - * Dumps the current packet as it is. - */ +WSLUA_METHOD Dumper_dump_current(lua_State* L) { + /* + Dumps the current packet as it is. + */ Dumper d = checkDumper(L,1); struct wtap_pkthdr pkthdr; - const guchar *data; - tvbuff_t *tvb; + const guchar* data; + tvbuff_t* tvb; data_source *data_src; int err = 0; if (!d) return 0; - if (! lua_pinfo ) WSLUA_ERROR(Dumper_new_for_current,"Cannot be used outside a tap or a dissector"); + if (! lua_pinfo ) WSLUA_ERROR(Dumper_new_for_current,"Cannot be used outside a tap or a dissector"); data_src = (data_source*) (lua_pinfo->data_src->data); if (!data_src) @@ -389,27 +374,27 @@ Dumper_dump_current(lua_State *L) data = ep_tvb_memdup(tvb,0,pkthdr.caplen); if (! wtap_dump(d, &pkthdr, lua_pinfo->pseudo_header, data, &err)) { - luaL_error(L,"error while dumping: %s", wtap_strerror(err)); + luaL_error(L,"error while dumping: %s", + wtap_strerror(err)); } return 0; } -static int -Dumper__gc(lua_State *L) -{ - Dumper *dp = (Dumper*)luaL_checkudata(L, 1, "Dumper"); +static int Dumper__gc(lua_State* L) { + Dumper* dp = (Dumper*)luaL_checkudata(L, 1, "Dumper"); int err; /* If we are Garbage Collected it means the Dumper is no longer usable. Close it */ if (! *dp) - return 0; /* already closed, nothing to do! */ + return 0; /* already closed, nothing to do! */ - g_hash_table_remove(dumper_encaps, *dp); + g_hash_table_remove(dumper_encaps,*dp); if (!wtap_dump_close(*dp, &err)) { - luaL_error(L,"error closing: %s", wtap_strerror(err)); + luaL_error(L,"error closing: %s", + wtap_strerror(err)); } return 0; @@ -427,13 +412,11 @@ WSLUA_METHODS Dumper_methods[] = { }; WSLUA_META Dumper_meta[] = { - {"__gc", Dumper__gc}, + {"__gc", Dumper__gc}, {0, 0} }; -int -Dumper_register(lua_State *L) -{ +int Dumper_register(lua_State* L) { dumper_encaps = g_hash_table_new(g_direct_hash,g_direct_equal); WSLUA_REGISTER_CLASS(Dumper); return 1; diff --git a/epan/wslua/wslua_field.c b/epan/wslua/wslua_field.c index f9b384843c..2effed0215 100644 --- a/epan/wslua/wslua_field.c +++ b/epan/wslua/wslua_field.c @@ -30,33 +30,33 @@ #include "wslua.h" +WSLUA_CLASS_DEFINE(FieldInfo,NOP,NOP); /* An extracted Field */ -WSLUA_CLASS_DEFINE(FieldInfo,NOP,NOP); -/* Obtain the Length of the field */ -WSLUA_METAMETHOD -FieldInfo__len(lua_State* L) -{ +WSLUA_METAMETHOD FieldInfo__len(lua_State* L) { + /* + Obtain the Length of the field + */ FieldInfo fi = checkFieldInfo(L,1); lua_pushnumber(L,fi->length); return 1; } -/* Obtain the Offset of the field */ -WSLUA_METAMETHOD -FieldInfo__unm(lua_State* L) -{ +WSLUA_METAMETHOD FieldInfo__unm(lua_State* L) { + /* + Obtain the Offset of the field + */ FieldInfo fi = checkFieldInfo(L,1); lua_pushnumber(L,fi->start); return 1; } -/* Obtain the Value of the field */ -WSLUA_METAMETHOD -FieldInfo__call(lua_State* L) -{ +WSLUA_METAMETHOD FieldInfo__call(lua_State* L) { + /* + Obtain the Value of the field + */ FieldInfo fi = checkFieldInfo(L,1); switch(fi->hfinfo->type) { @@ -153,10 +153,8 @@ FieldInfo__call(lua_State* L) } } -/* The string representation of the field */ -WSLUA_METAMETHOD -FieldInfo__tostring(lua_State* L) -{ +WSLUA_METAMETHOD FieldInfo__tostring(lua_State* L) { + /* The string representation of the field */ FieldInfo fi = checkFieldInfo(L,1); if (fi) { if (fi->value.ftype->val_to_string_repr) { @@ -171,10 +169,8 @@ FieldInfo__tostring(lua_State* L) return 1; } -/* The TvbRange covering this field */ -static int -FieldInfo_get_range(lua_State* L) -{ +static int FieldInfo_get_range(lua_State* L) { + /* The TvbRange covering this field */ FieldInfo fi = checkFieldInfo(L,1); TvbRange r = ep_alloc(sizeof(struct _wslua_tvbrange)); r->tvb = ep_alloc(sizeof(struct _wslua_tvb)); @@ -187,19 +183,15 @@ FieldInfo_get_range(lua_State* L) return 1; } -/* Whether this field was marked as generated. */ -static int -FieldInfo_get_generated(lua_State* L) -{ +static int FieldInfo_get_generated(lua_State* L) { + /* Whether this field was marked as generated. */ FieldInfo fi = checkFieldInfo(L,1); lua_pushboolean(L,FI_GET_FLAG(fi, FI_GENERATED)); return 1; } -/* The filter name of this field. */ -static int -FieldInfo_get_name(lua_State* L) -{ +static int FieldInfo_get_name(lua_State* L) { + /* The filter name of this field. */ FieldInfo fi = checkFieldInfo(L,1); lua_pushstring(L,fi->hfinfo->abbrev); return 1; @@ -224,10 +216,10 @@ static const luaL_reg FieldInfo_get[] = { { NULL, NULL } }; -/* Other attributes: */ -static int -FieldInfo__index(lua_State* L) -{ +static int FieldInfo__index(lua_State* L) { + /* + Other attributes: + */ const gchar* idx = luaL_checkstring(L,2); const luaL_reg* r; @@ -242,10 +234,8 @@ FieldInfo__index(lua_State* L) return 0; } -/* Checks whether lhs is within rhs */ -WSLUA_METAMETHOD -FieldInfo__eq(lua_State* L) -{ +WSLUA_METAMETHOD FieldInfo__eq(lua_State* L) { + /* Checks whether lhs is within rhs */ FieldInfo l = checkFieldInfo(L,1); FieldInfo r = checkFieldInfo(L,2); @@ -260,10 +250,8 @@ FieldInfo__eq(lua_State* L) } } -/* Checks whether the end byte of lhs is before the end of rhs */ -WSLUA_METAMETHOD -FieldInfo__le(lua_State* L) -{ +WSLUA_METAMETHOD FieldInfo__le(lua_State* L) { + /* Checks whether the end byte of lhs is before the end of rhs */ FieldInfo l = checkFieldInfo(L,1); FieldInfo r = checkFieldInfo(L,2); @@ -278,10 +266,8 @@ FieldInfo__le(lua_State* L) } } -/* Checks whether the end byte of rhs is before the beginning of rhs */ -WSLUA_METAMETHOD -FieldInfo__lt(lua_State* L) -{ +WSLUA_METAMETHOD FieldInfo__lt(lua_State* L) { + /* Checks whether the end byte of rhs is before the beginning of rhs */ FieldInfo l = checkFieldInfo(L,1); FieldInfo r = checkFieldInfo(L,2); @@ -309,17 +295,14 @@ static const luaL_reg FieldInfo_meta[] = { { NULL, NULL } }; -int -FieldInfo_register(lua_State* L) -{ +int FieldInfo_register(lua_State* L) { WSLUA_REGISTER_META(FieldInfo); return 1; } -/* Obtain all fields from the current tree */ -WSLUA_FUNCTION wslua_all_field_infos(lua_State* L) -{ +WSLUA_FUNCTION wslua_all_field_infos(lua_State* L) { + /* Obtain all fields from the current tree */ GPtrArray* found; int items_found = 0; guint i; @@ -342,10 +325,10 @@ WSLUA_FUNCTION wslua_all_field_infos(lua_State* L) return items_found; } +WSLUA_CLASS_DEFINE(Field,NOP,NOP); /* - * A Field extractor to to obtain field values. + A Field extractor to to obtain field values. */ -WSLUA_CLASS_DEFINE(Field,NOP,NOP); static GPtrArray* wanted_fields = NULL; @@ -360,9 +343,7 @@ static GPtrArray* wanted_fields = NULL; * after the fields are primed. */ -void -lua_prime_all_fields(proto_tree* tree _U_) -{ +void lua_prime_all_fields(proto_tree* tree _U_) { GString* fake_tap_filter = g_string_new("frame"); guint i; static gboolean fake_tap = FALSE; @@ -405,21 +386,21 @@ lua_prime_all_fields(proto_tree* tree _U_) } -/* Create a Field extractor */ +WSLUA_CONSTRUCTOR Field_new(lua_State *L) { + /* + Create a Field extractor + */ #define WSLUA_ARG_Field_new_FIELDNAME 1 /* The filter name of the field (e.g. ip.addr) */ -WSLUA_CONSTRUCTOR -Field_new(lua_State *L) -{ const gchar* name = luaL_checkstring(L,WSLUA_ARG_Field_new_FIELDNAME); Field f; if (!name) return 0; - if (!proto_registrar_get_byname(name)) - WSLUA_ARG_ERROR(Field_new,FIELDNAME,"a field with this name must exist"); + if (!proto_registrar_get_byname(name)) + WSLUA_ARG_ERROR(Field_new,FIELDNAME,"a field with this name must exist"); if (!wanted_fields) - WSLUA_ERROR(Field_get,"A Field extractor must be defined before Taps or Dissectors get called"); + WSLUA_ERROR(Field_get,"A Field extractor must be defined before Taps or Dissectors get called"); f = g_malloc(sizeof(void*)); *f = (header_field_info*)g_strdup(name); /* cheating */ @@ -430,13 +411,11 @@ Field_new(lua_State *L) WSLUA_RETURN(1); /* The field extractor */ } -/* Obtain all values (see FieldInfo) for this field. */ -WSLUA_METAMETHOD -Field__call (lua_State* L) -{ +WSLUA_METAMETHOD Field__call (lua_State* L) { + /* Obtain all values (see FieldInfo) for this field. */ Field f = checkField(L,1); header_field_info* in = *f; - int items_found = 0; + int items_found = 0; if (! in) { luaL_error(L,"invalid field"); @@ -448,24 +427,22 @@ Field__call (lua_State* L) return 0; } - for (; in ; in = in->same_name_next) { + for (;in;in = in->same_name_next) { GPtrArray* found = proto_get_finfo_ptr_array(lua_tree->tree, in->id); guint i; if (found) { for (i=0; i<found->len; i++) { - pushFieldInfo(L,g_ptr_array_index(found,i)); - items_found++; - } + pushFieldInfo(L,g_ptr_array_index(found,i)); + items_found++; + } } } WSLUA_RETURN(items_found); /* All the values of this field */ } -/* Obtain a srting with the field name */ -WSLUA_METAMETHOD -Field_tostring(lua_State* L) -{ +WSLUA_METAMETHOD Field_tostring(lua_State* L) { + /* Obtain a srting with the field name */ Field f = checkField(L,1); if ( !(f && *f) ) { @@ -493,9 +470,7 @@ static const luaL_reg Field_meta[] = { { NULL, NULL } }; -int -Field_register(lua_State* L) -{ +int Field_register(lua_State* L) { wanted_fields = g_ptr_array_new(); @@ -504,3 +479,4 @@ Field_register(lua_State* L) return 1; } + diff --git a/epan/wslua/wslua_gui.c b/epan/wslua/wslua_gui.c index 577d352be7..3f34069fc5 100644 --- a/epan/wslua/wslua_gui.c +++ b/epan/wslua/wslua_gui.c @@ -35,96 +35,73 @@ struct _lua_menu_data { int cb_ref; }; -static int -menu_cb_error_handler(lua_State* L) -{ +static int menu_cb_error_handler(lua_State* L) { const gchar* error = lua_tostring(L,1); report_failure("Lua: Error During execution of Menu Callback:\n %s",error); return 0; } -/* Checks whether the GUI facility is enabled. */ -WSLUA_FUNCTION wslua_gui_enabled(lua_State* L) -{ +WSLUA_FUNCTION wslua_gui_enabled(lua_State* L) { /* Checks whether the GUI facility is enabled. */ lua_pushboolean(L,GPOINTER_TO_INT(ops && ops->add_button)); WSLUA_RETURN(1); /* A boolean: true if it is enabled, false if it isn't. */ } -static void -lua_menu_callback(gpointer data) -{ +static void lua_menu_callback(gpointer data) { struct _lua_menu_data* md = data; - lua_State* L = md->L; + lua_State* L = md->L; - lua_settop(L,0); + lua_settop(L,0); lua_pushcfunction(L,menu_cb_error_handler); lua_rawgeti(L, LUA_REGISTRYINDEX, md->cb_ref); - switch ( lua_pcall(L,0,0,1) ) { - case 0: - break; - case LUA_ERRRUN: - g_warning("Runtime error while calling menu callback"); - break; - case LUA_ERRMEM: - g_warning("Memory alloc error while calling menu callback"); - break; - default: - g_assert_not_reached(); - break; + switch ( lua_pcall(L,0,0,1) ) { + case 0: + break; + case LUA_ERRRUN: + g_warning("Runtime error while calling menu callback"); + break; + case LUA_ERRMEM: + g_warning("Memory alloc error while calling menu callback"); + break; + default: + g_assert_not_reached(); + break; } return; } +WSLUA_FUNCTION wslua_register_menu(lua_State* L) { /* Register a menu item in one of the main menus. */ +#define WSLUA_ARG_register_menu_NAME 1 /* The name of the menu item. The submenus are to be separated by '/'s. (string) */ +#define WSLUA_ARG_register_menu_ACTION 2 /* The function to be called when the menu item is invoked. (function taking no arguments and returning nothing) */ +#define WSLUA_OPTARG_register_menu_GROUP 3 /* The menu group into which the menu item is to be inserted. If omitted, defaults to MENU_STAT_GENERIC. One of MENU_STAT_UNSORTED (Statistics), MENU_STAT_GENERIC (Statistics, first section), MENU_STAT_CONVERSATION (Statistics/Conversation List), MENU_STAT_ENDPOINT (Statistics/Endpoint List), MENU_STAT_RESPONSE (Statistics/Service Response Time), MENU_STAT_TELEPHONY (Telephony), MENU_ANALYZE (Analyze), MENU_ANALYZE_CONVERSATION (Analyze/Conversation Filter), MENU_TOOLS_UNSORTED (Tools). (number) */ -/* Register a menu item in one of the main menus. */ - -/* The name of the menu item. The submenus are to be separated by '/'s. - * (string) - */ -#define WSLUA_ARG_register_menu_NAME 1 -/* The function to be called when the menu item is invoked. (function taking - * no arguments and returning nothing) - */ -#define WSLUA_ARG_register_menu_ACTION 2 -/* The menu group into which the menu item is to be inserted. If omitted, - * defaults to MENU_STAT_GENERIC. One of: - * - MENU_STAT_UNSORTED (Statistics) - * - MENU_STAT_GENERIC (Statistics, first section) - * - MENU_STAT_CONVERSATION (Statistics/Conversation List) - * - MENU_STAT_ENDPOINT (Statistics/Endpoint List) - * - MENU_STAT_RESPONSE (Statistics/Service Response Time) - * - MENU_STAT_TELEPHONY (Telephony), MENU_ANALYZE (Analyze) - * - MENU_ANALYZE_CONVERSATION (Analyze/Conversation Filter) - * - MENU_TOOLS_UNSORTED (Tools). - * (number) - */ -#define WSLUA_OPTARG_register_menu_GROUP 3 -WSLUA_FUNCTION wslua_register_menu(lua_State* L) -{ const gchar* name = luaL_checkstring(L,WSLUA_ARG_register_menu_NAME); struct _lua_menu_data* md; gboolean retap = FALSE; - register_stat_group_t group = (int)luaL_optnumber(L, WSLUA_OPTARG_register_menu_GROUP, REGISTER_STAT_GROUP_GENERIC); + register_stat_group_t group = (int)luaL_optnumber(L,WSLUA_OPTARG_register_menu_GROUP,REGISTER_STAT_GROUP_GENERIC); - if ( group > REGISTER_TOOLS_GROUP_UNSORTED) - WSLUA_OPTARG_ERROR(register_menu,GROUP, "Must be a defined MENU_* (see init.lua)"); + if ( group > REGISTER_TOOLS_GROUP_UNSORTED) + WSLUA_OPTARG_ERROR(register_menu,GROUP,"Must be a defined MENU_* (see init.lua)"); - if(!name) - WSLUA_ARG_ERROR(register_menu,NAME,"Must be a string"); + if(!name) + WSLUA_ARG_ERROR(register_menu,NAME,"Must be a string"); if (!lua_isfunction(L,WSLUA_ARG_register_menu_ACTION)) - WSLUA_ARG_ERROR(register_menu,ACTION,"Must be a function"); + WSLUA_ARG_ERROR(register_menu,ACTION,"Must be a function"); md = g_malloc(sizeof(struct _lua_menu_data)); md->L = L; - lua_pushvalue(L, 2); - md->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_remove(L,2); + lua_pushvalue(L, 2); + md->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_remove(L,2); - funnel_register_menu(name, group, lua_menu_callback, md, retap); + funnel_register_menu(name, + group, + lua_menu_callback, + md, + retap); WSLUA_RETURN(0); } @@ -137,17 +114,13 @@ struct _dlg_cb_data { int func_ref; }; -static int -dlg_cb_error_handler(lua_State* L) -{ +static int dlg_cb_error_handler(lua_State* L) { const gchar* error = lua_tostring(L,1); report_failure("Lua: Error During execution of dialog callback:\n %s",error); return 0; } -static void -lua_dialog_cb(gchar** user_input, void* data) -{ +static void lua_dialog_cb(gchar** user_input, void* data) { struct _dlg_cb_data* dcbd = data; int i = 0; gchar* input; @@ -187,17 +160,13 @@ struct _close_cb_data { }; -static int -text_win_close_cb_error_handler(lua_State* L) -{ +static int text_win_close_cb_error_handler(lua_State* L) { const gchar* error = lua_tostring(L,1); report_failure("Lua: Error During execution of TextWindow close callback:\n %s",error); return 0; } -static void -text_win_close_cb(void* data) -{ +static void text_win_close_cb(void* data) { struct _close_cb_data* cbd = data; lua_State* L = cbd->L; @@ -229,12 +198,11 @@ text_win_close_cb(void* data) } -/* Pops up a new dialog */ +WSLUA_FUNCTION wslua_new_dialog(lua_State* L) { /* Pops up a new dialog */ #define WSLUA_ARG_new_dialog_TITLE 1 /* Title of the dialog's window. */ #define WSLUA_ARG_new_dialog_ACTION 2 /* Action to be performed when OKd. */ /* WSLUA_MOREARGS new_dialog A series of strings to be used as labels of the dialog's fields */ -WSLUA_FUNCTION wslua_new_dialog(lua_State* L) -{ + const gchar* title; int top = lua_gettop(L); int i; @@ -278,9 +246,9 @@ WSLUA_FUNCTION wslua_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) - WSLUA_ERROR(new_dialog,"All fields must be strings"); + /* XXX leaks labels on error */ + if (! label) + WSLUA_ERROR(new_dialog,"All fields must be strings"); g_ptr_array_add(labels,label); } @@ -298,15 +266,12 @@ WSLUA_FUNCTION wslua_new_dialog(lua_State* L) WSLUA_CLASS_DEFINE(ProgDlg,NOP,NOP); /* Manages a progress bar dialog. */ -/* Creates a new TextWindow. */ +WSLUA_CONSTRUCTOR ProgDlg_new(lua_State* L) { /* Creates a new TextWindow. */ #define WSLUA_OPTARG_ProgDlg_new_TITLE 2 /* Title of the new window, defaults to "Progress". */ #define WSLUA_OPTARG_ProgDlg_new_TASK 3 /* Current task, defaults to "". */ -WSLUA_CONSTRUCTOR -ProgDlg_new(lua_State* L) -{ ProgDlg pd = g_malloc(sizeof(struct _wslua_progdlg)); - pd->title = g_strdup(luaL_optstring(L, WSLUA_OPTARG_ProgDlg_new_TITLE, "Progress")); - pd->task = g_strdup(luaL_optstring(L, WSLUA_OPTARG_ProgDlg_new_TASK, "")); + pd->title = g_strdup(luaL_optstring(L,WSLUA_OPTARG_ProgDlg_new_TITLE,"Progress")); + pd->task = g_strdup(luaL_optstring(L,WSLUA_OPTARG_ProgDlg_new_TASK,"")); pd->stopped = FALSE; if (ops->new_progress_window) { @@ -315,73 +280,62 @@ ProgDlg_new(lua_State* L) pushProgDlg(L,pd); - WSLUA_RETURN(1); /* The newly created TextWindow object. */ + WSLUA_RETURN(1); /* The newly created TextWindow object. */ } -/* Appends text */ +WSLUA_METHOD ProgDlg_update(lua_State* L) { /* Appends text */ #define WSLUA_ARG_ProgDlg_update_PROGRESS 2 /* Part done ( e.g. 0.75 ). */ #define WSLUA_OPTARG_ProgDlg_update_TASK 3 /* Current task, defaults to "". */ -WSLUA_METHOD -ProgDlg_update(lua_State* L) -{ - ProgDlg pd = checkProgDlg(L, 1); - double pr = lua_tonumber(L, WSLUA_ARG_ProgDlg_update_PROGRESS); - const gchar* task = luaL_optstring(L, WSLUA_OPTARG_ProgDlg_update_TASK, ""); + ProgDlg pd = checkProgDlg(L,1); + double pr = lua_tonumber(L,WSLUA_ARG_ProgDlg_update_PROGRESS); + const gchar* task = luaL_optstring(L,WSLUA_OPTARG_ProgDlg_update_TASK,""); g_free(pd->task); pd->task = g_strdup(task); - if (!pd) { - WSLUA_ERROR(ProgDlg_update, "Cannot be called for something not a ProgDlg"); + if (!pd) { + WSLUA_ERROR(ProgDlg_update,"Cannot be called for something not a ProgDlg"); } if (pr >= 0.0 || pr <= 1.0) { ops->update_progress(pd->pw, (float) pr, task); } else { - WSLUA_ERROR(ProgDlg_update, "Progress value out of range (must be between 0.0 and 1.0)"); + WSLUA_ERROR(ProgDlg_update,"Progress value out of range (must be between 0.0 and 1.0)"); } return 0; } -/* Checks wheher the user has pressed the stop button. */ -WSLUA_METHOD -ProgDlg_stopped(lua_State* L) -{ +WSLUA_METHOD ProgDlg_stopped(lua_State* L) { /* Checks wheher the user has pressed the stop button. */ ProgDlg pd = checkProgDlg(L,1); - if (!pd) { - WSLUA_ERROR(ProgDlg_stopped,"Cannot be called for something not a ProgDlg"); + if (!pd) { + WSLUA_ERROR(ProgDlg_stopped,"Cannot be called for something not a ProgDlg"); } lua_pushboolean(L,pd->stopped); - WSLUA_RETURN(1); /* true if the user has asked to stop the progress. */ + WSLUA_RETURN(1); /* true if the user has asked to stop the progress. */ } -/* Appends text */ -WSLUA_METHOD -ProgDlg_close(lua_State* L) -{ + +WSLUA_METHOD ProgDlg_close(lua_State* L) { /* Appends text */ ProgDlg pd = checkProgDlg(L,1); - if (!pd) { - WSLUA_ERROR(ProgDlg_update,"Cannot be called for something not a ProgDlg"); + if (!pd) { + WSLUA_ERROR(ProgDlg_update,"Cannot be called for something not a ProgDlg"); } if (pd->pw) { ops->destroy_progress_window(pd->pw); pd->pw = NULL; } - return 0; } -static int -ProgDlg__tostring(lua_State* L) -{ +static int ProgDlg__tostring(lua_State* L) { ProgDlg pd = checkProgDlg(L,1); if (pd) { @@ -393,9 +347,7 @@ ProgDlg__tostring(lua_State* L) return 0; } -static int -ProgDlg__gc(lua_State* L) -{ +static int ProgDlg__gc(lua_State* L) { ProgDlg pd = checkProgDlg(L,1); if (pd) { @@ -424,12 +376,11 @@ WSLUA_META ProgDlg_meta[] = { {0, 0} }; -int -ProgDlg_register(lua_State* L) -{ +int ProgDlg_register(lua_State* L) { + ops = funnel_get_funnel_ops(); - WSLUA_REGISTER_CLASS(ProgDlg); + WSLUA_REGISTER_CLASS(ProgDlg); return 1; } @@ -442,11 +393,9 @@ WSLUA_CLASS_DEFINE(TextWindow,NOP,NOP); /* Manages a text window. */ /* XXX: lua callback function and TextWindow are not garbage collected because they stay in LUA_REGISTRYINDEX forever */ -/* Creates a new TextWindow. */ +WSLUA_CONSTRUCTOR TextWindow_new(lua_State* L) { /* Creates a new TextWindow. */ #define WSLUA_OPTARG_TextWindow_new_TITLE 1 /* Title of the new window. */ -WSLUA_CONSTRUCTOR -TextWindow_new(lua_State* L) -{ + const gchar* title; TextWindow tw = NULL; struct _close_cb_data* default_cbd; @@ -466,19 +415,17 @@ TextWindow_new(lua_State* L) pushTextWindow(L,tw); - WSLUA_RETURN(1); /* The newly created TextWindow object. */ + WSLUA_RETURN(1); /* The newly created TextWindow object. */ } -/* Set the function that will be called when the window closes */ +WSLUA_METHOD TextWindow_set_atclose(lua_State* L) { /* Set the function that will be called when the window closes */ #define WSLUA_ARG_TextWindow_at_close_ACTION 2 /* A function to be executed when the user closes the window */ -WSLUA_METHOD -TextWindow_set_atclose(lua_State* L) -{ + TextWindow tw = checkTextWindow(L,1); struct _close_cb_data* cbd; - if (!tw) - WSLUA_ERROR(TextWindow_at_close,"Cannot be called for something not a TextWindow"); + if (!tw) + WSLUA_ERROR(TextWindow_at_close,"Cannot be called for something not a TextWindow"); lua_settop(L,2); @@ -493,147 +440,130 @@ TextWindow_set_atclose(lua_State* L) ops->set_close_cb(tw->ws_tw,text_win_close_cb,cbd); - WSLUA_RETURN(1); /* The TextWindow object. */ + WSLUA_RETURN(1); /* The TextWindow object. */ } -/* Sets the text. */ +WSLUA_METHOD TextWindow_set(lua_State* L) { /* Sets the text. */ #define WSLUA_ARG_TextWindow_set_TEXT 2 /* The text to be used. */ -WSLUA_METHOD -TextWindow_set(lua_State* L) -{ + TextWindow tw = checkTextWindow(L,1); const gchar* text = luaL_checkstring(L,WSLUA_ARG_TextWindow_set_TEXT); - if (!tw) - WSLUA_ERROR(TextWindow_set,"Cannot be called for something not a TextWindow"); + if (!tw) + WSLUA_ERROR(TextWindow_set,"Cannot be called for something not a TextWindow"); if (tw->expired) - WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); + WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); if (!text) - WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Must be a string"); + WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Must be a string"); ops->set_text(tw->ws_tw,text); - WSLUA_RETURN(1); /* The TextWindow object. */ + WSLUA_RETURN(1); /* The TextWindow object. */ } -/* Appends text */ +WSLUA_METHOD TextWindow_append(lua_State* L) { /* Appends text */ #define WSLUA_ARG_TextWindow_append_TEXT 2 /* The text to be appended */ -WSLUA_METHOD -TextWindow_append(lua_State* L) -{ TextWindow tw = checkTextWindow(L,1); const gchar* text = luaL_checkstring(L,WSLUA_ARG_TextWindow_append_TEXT); - if (!tw) - WSLUA_ERROR(TextWindow_append,"Cannot be called for something not a TextWindow"); + if (!tw) + WSLUA_ERROR(TextWindow_append,"Cannot be called for something not a TextWindow"); if (tw->expired) - WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); + WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); - if (!text) - WSLUA_ARG_ERROR(TextWindow_append,TEXT,"Must be a string"); + if (!text) + WSLUA_ARG_ERROR(TextWindow_append,TEXT,"Must be a string"); ops->append_text(tw->ws_tw,text); - WSLUA_RETURN(1); /* The TextWindow object. */ + WSLUA_RETURN(1); /* The TextWindow object. */ } -/* Prepends text */ +WSLUA_METHOD TextWindow_prepend(lua_State* L) { /* Prepends text */ #define WSLUA_ARG_TextWindow_prepend_TEXT 2 /* The text to be appended */ -WSLUA_METHOD -TextWindow_prepend(lua_State* L) -{ TextWindow tw = checkTextWindow(L,1); const gchar* text = luaL_checkstring(L,WSLUA_ARG_TextWindow_prepend_TEXT); - if (!tw) - WSLUA_ERROR(TextWindow_prepend,"Cannot be called for something not a TextWindow"); + if (!tw) + WSLUA_ERROR(TextWindow_prepend,"Cannot be called for something not a TextWindow"); if (tw->expired) - WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); + WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); - if (!text) - WSLUA_ARG_ERROR(TextWindow_prepend,TEXT,"Must be a string"); + if (!text) + WSLUA_ARG_ERROR(TextWindow_prepend,TEXT,"Must be a string"); ops->prepend_text(tw->ws_tw,text); - WSLUA_RETURN(1); /* The TextWindow object. */ + WSLUA_RETURN(1); /* The TextWindow object. */ } -/* Erases all text in the window. */ -WSLUA_METHOD -TextWindow_clear(lua_State* L) -{ +WSLUA_METHOD TextWindow_clear(lua_State* L) { /* Erases all text in the window. */ TextWindow tw = checkTextWindow(L,1); - if (!tw) - WSLUA_ERROR(TextWindow_clear,"Cannot be called for something not a TextWindow"); + if (!tw) + WSLUA_ERROR(TextWindow_clear,"Cannot be called for something not a TextWindow"); if (tw->expired) - WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); + WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); ops->clear_text(tw->ws_tw); - WSLUA_RETURN(1); /* The TextWindow object. */ + WSLUA_RETURN(1); /* The TextWindow object. */ } -/* Get the text of the window */ -WSLUA_METHOD -TextWindow_get_text(lua_State* L) -{ +WSLUA_METHOD TextWindow_get_text(lua_State* L) { /* Get the text of the window */ TextWindow tw = checkTextWindow(L,1); - const gchar* text; + const gchar* text; - if (!tw) - WSLUA_ERROR(TextWindow_get_text,"Cannot be called for something not a TextWindow"); + if (!tw) + WSLUA_ERROR(TextWindow_get_text,"Cannot be called for something not a TextWindow"); if (tw->expired) - WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); + WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); - text = ops->get_text(tw->ws_tw); + text = ops->get_text(tw->ws_tw); lua_pushstring(L,text); - WSLUA_RETURN(1); /* The TextWindow's text. */ + WSLUA_RETURN(1); /* The TextWindow's text. */ } -static int -TextWindow__gc(lua_State* L) -{ +static int TextWindow__gc(lua_State* L) { TextWindow tw = checkTextWindow(L,1); - if (!tw) - return 0; + if (!tw) + return 0; + + if (!tw->expired) { + tw->expired = TRUE; + ops->destroy_text_window(tw->ws_tw); + } else { + g_free(tw); + } - if (!tw->expired) { - tw->expired = TRUE; - ops->destroy_text_window(tw->ws_tw); - } else { - g_free(tw); - } return 0; } -/* Make this window editable */ +WSLUA_METHOD TextWindow_set_editable(lua_State* L) { /* Make this window editable */ #define WSLUA_OPTARG_TextWindow_set_editable_EDITABLE 2 /* A boolean flag, defaults to true */ -WSLUA_METHOD -TextWindow_set_editable(lua_State* L) -{ - TextWindow tw = checkTextWindow(L,1); - gboolean editable = wslua_optbool(L,WSLUA_OPTARG_TextWindow_set_editable_EDITABLE,TRUE); - if (!tw) - WSLUA_ERROR(TextWindow_set_editable,"Cannot be called for something not a TextWindow"); + TextWindow tw = checkTextWindow(L,1); + gboolean editable = wslua_optbool(L,WSLUA_OPTARG_TextWindow_set_editable_EDITABLE,TRUE); + + if (!tw) + WSLUA_ERROR(TextWindow_set_editable,"Cannot be called for something not a TextWindow"); if (tw->expired) - WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); + WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); - if (ops->set_editable) - ops->set_editable(tw->ws_tw,editable); + if (ops->set_editable) + ops->set_editable(tw->ws_tw,editable); - WSLUA_RETURN(1); /* The TextWindow object. */ + WSLUA_RETURN(1); /* The TextWindow object. */ } typedef struct _wslua_bt_cb_t { @@ -642,75 +572,71 @@ typedef struct _wslua_bt_cb_t { int wslua_tw_ref; } wslua_bt_cb_t; -static gboolean -wslua_button_callback(funnel_text_window_t* ws_tw, void* data) -{ - wslua_bt_cb_t* cbd = data; - lua_State* L = cbd->L; - (void) ws_tw; /* ws_tw is unused since we need wslua_tw_ref and it is stored in cbd */ - - lua_settop(L,0); - lua_pushcfunction(L,dlg_cb_error_handler); - lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->func_ref); - lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->wslua_tw_ref); - - switch ( lua_pcall(L,1,0,1) ) { - case 0: - break; - case LUA_ERRRUN: - g_warning("Runtime error while calling button callback"); - break; - case LUA_ERRMEM: - g_warning("Memory alloc error while calling button callback"); - break; - default: - g_assert_not_reached(); - break; - } - - return TRUE; -} - +static gboolean wslua_button_callback(funnel_text_window_t* ws_tw, void* data) { + wslua_bt_cb_t* cbd = data; + lua_State* L = cbd->L; + (void) ws_tw; /* ws_tw is unused since we need wslua_tw_ref and it is stored in cbd */ + + lua_settop(L,0); + lua_pushcfunction(L,dlg_cb_error_handler); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->func_ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->wslua_tw_ref); + + switch ( lua_pcall(L,1,0,1) ) { + case 0: + break; + case LUA_ERRRUN: + g_warning("Runtime error while calling button callback"); + break; + case LUA_ERRMEM: + g_warning("Memory alloc error while calling button callback"); + break; + default: + g_assert_not_reached(); + break; + } + + return TRUE; +} + +WSLUA_METHOD TextWindow_add_button(lua_State* L) { #define WSLUA_ARG_TextWindow_add_button_LABEL 2 /* The label of the button */ #define WSLUA_ARG_TextWindow_add_button_FUNCTION 3 /* The function to be called when clicked */ -WSLUA_METHOD -TextWindow_add_button(lua_State* L) -{ - TextWindow tw = checkTextWindow(L,1); - const gchar* label = luaL_checkstring(L,WSLUA_ARG_TextWindow_add_button_LABEL); + TextWindow tw = checkTextWindow(L,1); + const gchar* label = luaL_checkstring(L,WSLUA_ARG_TextWindow_add_button_LABEL); - funnel_bt_t* fbt; - wslua_bt_cb_t* cbd; + funnel_bt_t* fbt; + wslua_bt_cb_t* cbd; - if (!tw) - WSLUA_ERROR(TextWindow_add_button,"Cannot be called for something not a TextWindow"); + if (!tw) + WSLUA_ERROR(TextWindow_add_button,"Cannot be called for something not a TextWindow"); if (tw->expired) - WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); + WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); - if (! lua_isfunction(L,WSLUA_ARG_TextWindow_add_button_FUNCTION) ) - WSLUA_ARG_ERROR(TextWindow_add_button,FUNCTION,"must be a function"); + if (! lua_isfunction(L,WSLUA_ARG_TextWindow_add_button_FUNCTION) ) + WSLUA_ARG_ERROR(TextWindow_add_button,FUNCTION,"must be a function"); - lua_settop(L,3); + lua_settop(L,3); - if (ops->add_button) { - fbt = g_malloc(sizeof(funnel_bt_t)); - cbd = g_malloc(sizeof(wslua_bt_cb_t)); + if (ops->add_button) { + fbt = g_malloc(sizeof(funnel_bt_t)); + cbd = g_malloc(sizeof(wslua_bt_cb_t)); - fbt->tw = tw->ws_tw; - fbt->func = wslua_button_callback; - fbt->data = cbd; - fbt->free_fcn = g_free; - fbt->free_data_fcn = g_free; + fbt->tw = tw->ws_tw; + fbt->func = wslua_button_callback; + fbt->data = cbd; + fbt->free_fcn = g_free; + fbt->free_data_fcn = g_free; - cbd->L = L; - cbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX); - cbd->wslua_tw_ref = luaL_ref(L, LUA_REGISTRYINDEX); + cbd->L = L; + cbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX); + cbd->wslua_tw_ref = luaL_ref(L, LUA_REGISTRYINDEX); - ops->add_button(tw->ws_tw,fbt,label); - } + ops->add_button(tw->ws_tw,fbt,label); + } - WSLUA_RETURN(1); /* The TextWindow object. */ + WSLUA_RETURN(1); /* The TextWindow object. */ } WSLUA_METHODS TextWindow_methods[] = { @@ -724,7 +650,7 @@ WSLUA_METHODS TextWindow_methods[] = { WSLUA_CLASS_FNREG(TextWindow,set_editable), WSLUA_CLASS_FNREG(TextWindow,get_text), WSLUA_CLASS_FNREG(TextWindow,add_button), - {0, 0} + {0, 0} }; WSLUA_META TextWindow_meta[] = { @@ -733,165 +659,155 @@ WSLUA_META TextWindow_meta[] = { {0, 0} }; -int -TextWindow_register(lua_State* L) -{ +int TextWindow_register(lua_State* L) { + ops = funnel_get_funnel_ops(); - WSLUA_REGISTER_CLASS(TextWindow); + WSLUA_REGISTER_CLASS(TextWindow); return 1; } -/* Rescan all packets and just run taps - don't reconstruct the display. */ -WSLUA_FUNCTION wslua_retap_packets(lua_State* L) -{ - if ( ops->retap_packets ) { - ops->retap_packets(); - } else { - WSLUA_ERROR(wslua_retap_packets, "Does not work on TShark"); - } +WSLUA_FUNCTION wslua_retap_packets(lua_State* L) { + /* + Rescan all packets and just run taps - don't reconstruct the display. + */ + if ( ops->retap_packets ) { + ops->retap_packets(); + } else { + WSLUA_ERROR(wslua_retap_packets, "Does not work on TShark"); + } - return 0; + return 0; } -/* Copy a string into the clipboard */ +WSLUA_FUNCTION wslua_copy_to_clipboard(lua_State* L) { /* Copy a string into the clipboard */ #define WSLUA_ARG_copy_to_clipboard_TEXT 1 /* The string to be copied into the clipboard. */ -WSLUA_FUNCTION wslua_copy_to_clipboard(lua_State* L) -{ - const char* copied_str = luaL_checkstring(L,WSLUA_ARG_copy_to_clipboard_TEXT); - GString* gstr; - if (!ops->copy_to_clipboard) { - WSLUA_ERROR(wslua_copy_to_clipboard, "Does not work on TShark"); - } + const char* copied_str = luaL_checkstring(L,WSLUA_ARG_copy_to_clipboard_TEXT); + GString* gstr; + if (!ops->copy_to_clipboard) { + WSLUA_ERROR(wslua_copy_to_clipboard, "Does not work on TShark"); + } - if (!copied_str) { - WSLUA_ARG_ERROR(copy_to_clipboard,TEXT,"Must be a string"); - } + if (!copied_str) { + WSLUA_ARG_ERROR(copy_to_clipboard,TEXT,"Must be a string"); + } - gstr = g_string_new(copied_str); + gstr = g_string_new(copied_str); - ops->copy_to_clipboard(gstr); + ops->copy_to_clipboard(gstr); - g_string_free(gstr,TRUE); + g_string_free(gstr,TRUE); - return 0; + return 0; } -/* Open and display a capture file */ +WSLUA_FUNCTION wslua_open_capture_file(lua_State* L) { /* Open and display a capture file */ #define WSLUA_ARG_open_capture_file_FILENAME 1 /* The name of the file to be opened. */ #define WSLUA_ARG_open_capture_file_FILTER 2 /* A filter to be applied as the file gets opened. */ -WSLUA_FUNCTION wslua_open_capture_file(lua_State* L) -{ - const char* fname = luaL_checkstring(L,WSLUA_ARG_open_capture_file_FILENAME); - const char* filter = luaL_optstring(L,WSLUA_ARG_open_capture_file_FILTER,NULL); - const char* error = NULL; - - if (!ops->open_file) { - WSLUA_ERROR(wslua_open_capture_file, "Does not work on TShark"); - } - if (!fname) { - WSLUA_ARG_ERROR(open_capture_file,FILENAME,"Must be a string"); - } + const char* fname = luaL_checkstring(L,WSLUA_ARG_open_capture_file_FILENAME); + const char* filter = luaL_optstring(L,WSLUA_ARG_open_capture_file_FILTER,NULL); + const char* error = NULL; - if (! ops->open_file(fname,filter,&error) ) { - lua_pushboolean(L,FALSE); + if (!ops->open_file) { + WSLUA_ERROR(wslua_open_capture_file, "Does not work on TShark"); + } - if (error) - lua_pushstring(L,error); - else - lua_pushnil(L); + if (!fname) { + WSLUA_ARG_ERROR(open_capture_file,FILENAME,"Must be a string"); + } - return 2; - } else { - lua_pushboolean(L,TRUE); - return 1; - } + if (! ops->open_file(fname,filter,&error) ) { + lua_pushboolean(L,FALSE); + + if (error) + lua_pushstring(L,error); + else + lua_pushnil(L); + + return 2; + } else { + lua_pushboolean(L,TRUE); + return 1; + } } -/* Set the main filter text */ +WSLUA_FUNCTION wslua_set_filter(lua_State* L) { /* Set the main filter text */ #define WSLUA_ARG_set_filter_TEXT 1 /* The filter's text. */ -WSLUA_FUNCTION wslua_set_filter(lua_State* L) -{ - const char* filter_str = luaL_checkstring(L,WSLUA_ARG_set_filter_TEXT); + const char* filter_str = luaL_checkstring(L,WSLUA_ARG_set_filter_TEXT); - if (!ops->set_filter) { - WSLUA_ERROR(wslua_set_filter, "Does not work on TShark"); - } + if (!ops->set_filter) { + WSLUA_ERROR(wslua_set_filter, "Does not work on TShark"); + } - if (!filter_str) { - WSLUA_ARG_ERROR(set_filter,TEXT,"Must be a string"); - } + if (!filter_str) { + WSLUA_ARG_ERROR(set_filter,TEXT,"Must be a string"); + } - ops->set_filter(filter_str); + ops->set_filter(filter_str); - return 0; + return 0; } -/* Apply the filter in the main filter box */ -WSLUA_FUNCTION wslua_apply_filter(lua_State* L) -{ - if (!ops->apply_filter) { - WSLUA_ERROR(wslua_apply_filter, "Does not work on TShark"); - } +WSLUA_FUNCTION wslua_apply_filter(lua_State* L) { /* Apply the filter in the main filter box */ + if (!ops->apply_filter) { + WSLUA_ERROR(wslua_apply_filter, "Does not work on TShark"); + } - ops->apply_filter(); + ops->apply_filter(); - return 0; + return 0; } -/* Reload the current capture file */ -WSLUA_FUNCTION wslua_reload(lua_State* L) -{ - if (!ops->reload) { - WSLUA_ERROR(wslua_reload, "Does not work on TShark"); - } +WSLUA_FUNCTION wslua_reload(lua_State* L) { /* Reload the current capture file */ - ops->reload(); + if (!ops->reload) { + WSLUA_ERROR(wslua_reload, "Does not work on TShark"); + } - return 0; + ops->reload(); + + return 0; } -/* Open an url in a browser */ +WSLUA_FUNCTION wslua_browser_open_url(lua_State* L) { /* Open an url in a browser */ #define WSLUA_ARG_browser_open_url_URL 1 /* The url. */ -WSLUA_FUNCTION wslua_browser_open_url(lua_State* L) -{ - const char* url = luaL_checkstring(L,WSLUA_ARG_browser_open_url_URL); + const char* url = luaL_checkstring(L,WSLUA_ARG_browser_open_url_URL); - if (!ops->browser_open_url) { - WSLUA_ERROR(browser_open_url, "Does not work on TShark"); - } + if (!ops->browser_open_url) { + WSLUA_ERROR(browser_open_url, "Does not work on TShark"); + } - if (!url) { - WSLUA_ARG_ERROR(browser_open_url,URL,"Must be a string"); - } + if (!url) { + WSLUA_ARG_ERROR(browser_open_url,URL,"Must be a string"); + } - ops->browser_open_url(url); + ops->browser_open_url(url); - return 0; + return 0; } -/* Open an file in a browser */ +WSLUA_FUNCTION wslua_browser_open_data_file(lua_State* L) { /* Open an file in a browser */ #define WSLUA_ARG_browser_open_data_file_FILENAME 1 /* The url. */ -WSLUA_FUNCTION wslua_browser_open_data_file(lua_State* L) -{ - const char* file = luaL_checkstring(L,WSLUA_ARG_browser_open_data_file_FILENAME); + const char* file = luaL_checkstring(L,WSLUA_ARG_browser_open_data_file_FILENAME); - if (!ops->browser_open_data_file) { - WSLUA_ERROR(browser_open_data_file, "Does not work on TShark"); - } + if (!ops->browser_open_data_file) { + WSLUA_ERROR(browser_open_data_file, "Does not work on TShark"); + } - if (!file) { - WSLUA_ARG_ERROR(browser_open_data_file,FILENAME,"Must be a string"); - } + if (!file) { + WSLUA_ARG_ERROR(browser_open_data_file,FILENAME,"Must be a string"); + } - ops->browser_open_data_file(file); + ops->browser_open_data_file(file); - return 0; + return 0; } + + diff --git a/epan/wslua/wslua_listener.c b/epan/wslua/wslua_listener.c index 8d30b42bad..cbd46475ad 100644 --- a/epan/wslua/wslua_listener.c +++ b/epan/wslua/wslua_listener.c @@ -32,16 +32,14 @@ #include "wslua.h" +WSLUA_CLASS_DEFINE(Listener,NOP,NOP); /* - * A Listener, is called once for every packet that matches a certain filter or has a certain tap. - * It can read the tree, the packet's Tvb eventually the tapped data but it cannot - * add elements to the tree. + A Listener, is called once for every packet that matches a certain filter or has a certain tap. + It can read the tree, the packet's Tvb eventually the tapped data but it cannot + add elements to the tree. */ -WSLUA_CLASS_DEFINE(Listener,NOP,NOP); -static int -tap_packet_cb_error_handler(lua_State* L) -{ +static int tap_packet_cb_error_handler(lua_State* L) { const gchar* error = lua_tostring(L,1); static gchar* last_error = NULL; static int repeated = 0; @@ -81,9 +79,7 @@ tap_packet_cb_error_handler(lua_State* L) } -static int -lua_tap_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const void *data) -{ +static int lua_tap_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const void *data) { Listener tap = tapdata; int retval = 0; @@ -134,17 +130,13 @@ lua_tap_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const voi return retval; } -static int -tap_reset_cb_error_handler(lua_State* L) -{ +static int tap_reset_cb_error_handler(lua_State* L) { const gchar* error = lua_tostring(L,1); report_failure("Lua: Error During execution of Listener init Callback:\n %s",error); return 1; } -static void -lua_tap_reset(void *tapdata) -{ +static void lua_tap_reset(void *tapdata) { Listener tap = tapdata; if (tap->init_ref == LUA_NOREF) return; @@ -167,9 +159,7 @@ lua_tap_reset(void *tapdata) } } -static void -lua_tap_draw(void *tapdata) -{ +static void lua_tap_draw(void *tapdata) { Listener tap = tapdata; const gchar* error; if (tap->draw_ref == LUA_NOREF) return; @@ -194,12 +184,10 @@ lua_tap_draw(void *tapdata) } } -/* Creates a new Listener listener */ +WSLUA_CONSTRUCTOR Listener_new(lua_State* L) { + /* Creates a new Listener listener */ #define WSLUA_OPTARG_Listener_new_TAP 1 /* The name of this tap */ #define WSLUA_OPTARG_Listener_new_FILTER 2 /* A filter that when matches the tap.packet function gets called (use nil to be called for every packet) */ -WSLUA_CONSTRUCTOR -Listener_new(lua_State* L) -{ const gchar* tap_type = luaL_optstring(L,WSLUA_OPTARG_Listener_new_TAP,"frame"); const gchar* filter = luaL_optstring(L,WSLUA_OPTARG_Listener_new_FILTER,NULL); @@ -238,10 +226,8 @@ Listener_new(lua_State* L) WSLUA_RETURN(1); /* The newly created Listener listener object */ } -/* Removes a tap listener */ -WSLUA_METHOD -Listener_remove(lua_State* L) -{ +WSLUA_METHOD Listener_remove(lua_State* L) { + /* Removes a tap listener */ Listener tap = checkListener(L,1); if (!tap) return 0; @@ -251,9 +237,7 @@ Listener_remove(lua_State* L) return 0; } -WSLUA_METAMETHOD -Listener_tostring(lua_State* L) -{ +WSLUA_METAMETHOD Listener_tostring(lua_State* L) { Listener tap = checkListener(L,1); gchar* str; @@ -266,22 +250,20 @@ Listener_tostring(lua_State* L) } -static int -Listener_newindex(lua_State* L) -{ +static int Listener_newindex(lua_State* L) { /* WSLUA_ATTRIBUTE Listener_packet WO A function that will be called once every packet matches the Listener listener filter. - * - * function tap.packet(pinfo,tvb,userdata) ... end - */ + + function tap.packet(pinfo,tvb,userdata) ... end + */ /* WSLUA_ATTRIBUTE Listener_draw WO A function that will be called once every few seconds to redraw the gui objects - * in tshark this funtion is called oly at the very end of the capture file. - * - * function tap.draw(userdata) ... end - */ + in tshark this funtion is called oly at the very end of the capture file. + + function tap.draw(userdata) ... end + */ /* WSLUA_ATTRIBUTE Listener_reset WO A function that will be called at the end of the capture run. - * - * function tap.reset(userdata) ... end - */ + + function tap.reset(userdata) ... end + */ Listener tap = shiftListener(L,1); const gchar* idx = lua_shiftstring(L,1); int* refp = NULL; @@ -323,9 +305,7 @@ static const luaL_reg Listener_meta[] = { { NULL, NULL } }; -int -Listener_register(lua_State* L) -{ +int Listener_register(lua_State* L) { wslua_set_tap_enums(L); WSLUA_REGISTER_CLASS(Listener); return 1; diff --git a/epan/wslua/wslua_pinfo.c b/epan/wslua/wslua_pinfo.c index 5822fda806..d5a5d6389f 100644 --- a/epan/wslua/wslua_pinfo.c +++ b/epan/wslua/wslua_pinfo.c @@ -49,9 +49,7 @@ CLEAR_OUTSTANDING(Pinfo,expired, TRUE) CLEAR_OUTSTANDING(Column,expired, TRUE) CLEAR_OUTSTANDING(Columns,expired, TRUE) -Pinfo* -push_Pinfo(lua_State* L, packet_info* ws_pinfo) -{ +Pinfo* push_Pinfo(lua_State* L, packet_info* ws_pinfo) { Pinfo pinfo = NULL; if (ws_pinfo) { pinfo = g_malloc(sizeof(struct _wslua_pinfo)); @@ -59,7 +57,6 @@ push_Pinfo(lua_State* L, packet_info* ws_pinfo) pinfo->expired = FALSE; g_ptr_array_add(outstanding_Pinfo,pinfo); } - return pushPinfo(L,pinfo); } @@ -68,12 +65,10 @@ push_Pinfo(lua_State* L, packet_info* ws_pinfo) WSLUA_CLASS_DEFINE(Address,NOP,NOP); /* Represents an address */ -/* Creates an Address Object representing an IP address. */ -#define WSLUA_ARG_Address_ip_HOSTNAME 1 /* The address or name of the IP host. */ -WSLUA_CONSTRUCTOR -Address_ip(lua_State* L) -{ +WSLUA_CONSTRUCTOR Address_ip(lua_State* L) { + /* Creates an Address Object representing an IP address. */ +#define WSLUA_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,WSLUA_ARG_Address_ip_HOSTNAME); @@ -89,9 +84,7 @@ Address_ip(lua_State* L) #if 0 /* TODO */ -static int -Address_ipv6(lua_State* L) -{ +static int Address_ipv6(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -99,9 +92,7 @@ Address_ipv6(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_ss7(lua_State* L) -{ +static int Address_ss7(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -109,9 +100,7 @@ Address_ss7(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_eth(lua_State* L) -{ +static int Address_eth(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -119,9 +108,7 @@ Address_eth(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_sna(lua_State* L) -{ +static int Address_sna(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -129,9 +116,7 @@ Address_sna(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_atalk(lua_State* L) -{ +static int Address_atalk(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -139,9 +124,7 @@ Address_atalk(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_vines(lua_State* L) -{ +static int Address_vines(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -149,9 +132,7 @@ Address_vines(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_osi(lua_State* L) -{ +static int Address_osi(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -159,9 +140,7 @@ Address_osi(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_arcnet(lua_State* L) -{ +static int Address_arcnet(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -169,9 +148,7 @@ Address_arcnet(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_fc(lua_State* L) -{ +static int Address_fc(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -179,9 +156,7 @@ Address_fc(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_string(lua_State* L) -{ +static int Address_string(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -189,9 +164,7 @@ Address_string(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_eui64(lua_State* L) -{ +static int Address_eui64(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -199,9 +172,7 @@ Address_eui64(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_uri(lua_State* L) -{ +static int Address_uri(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -209,9 +180,7 @@ Address_uri(lua_State* L) pushAddress(L,addr); return 1; } -static int -Address_tipc(lua_State* L) -{ +static int Address_tipc(lua_State* L) { Address addr = g_malloc(sizeof(address)); SET_ADDRESS(addr, AT_NONE, 4, g_malloc(4)); @@ -242,9 +211,7 @@ WSLUA_METHODS Address_methods[] = { {0,0} }; -WSLUA_METAMETHOD -Address__tostring(lua_State* L) -{ +WSLUA_METAMETHOD Address__tostring(lua_State* L) { Address addr = checkAddress(L,1); lua_pushstring(L,get_addr_name(addr)); @@ -252,9 +219,7 @@ Address__tostring(lua_State* L) WSLUA_RETURN(1); /* The string representing the address. */ } -static int -Address__gc(lua_State* L) -{ +static int Address__gc(lua_State* L) { Address addr = checkAddress(L,1); if (addr) { @@ -265,10 +230,7 @@ Address__gc(lua_State* L) return 0; } -/* Compares two Addresses */ -WSLUA_METAMETHOD -Address__eq(lua_State* L) -{ +WSLUA_METAMETHOD Address__eq(lua_State* L) { /* Compares two Addresses */ Address addr1 = checkAddress(L,1); Address addr2 = checkAddress(L,2); gboolean result = FALSE; @@ -281,10 +243,7 @@ Address__eq(lua_State* L) return 1; } -/* Compares two Addresses */ -WSLUA_METAMETHOD -Address__le(lua_State* L) -{ +WSLUA_METAMETHOD Address__le(lua_State* L) { /* Compares two Addresses */ Address addr1 = checkAddress(L,1); Address addr2 = checkAddress(L,2); gboolean result = FALSE; @@ -297,10 +256,7 @@ Address__le(lua_State* L) return 1; } -/* Compares two Addresses */ -WSLUA_METAMETHOD -Address__lt(lua_State* L) -{ +WSLUA_METAMETHOD Address__lt(lua_State* L) { /* Compares two Addresses */ Address addr1 = checkAddress(L,1); Address addr2 = checkAddress(L,2); gboolean result = FALSE; @@ -380,9 +336,7 @@ static const struct col_names_t colnames[] = { {NULL,0} }; -static gint -col_name_to_id(const gchar* name) -{ +static gint col_name_to_id(const gchar* name) { const struct col_names_t* cn; for(cn = colnames; cn->name; cn++) { if (g_str_equal(cn->name,name)) { @@ -393,9 +347,7 @@ col_name_to_id(const gchar* name) return 0; } -static const gchar* -col_id_to_name(gint id) -{ +static const gchar* col_id_to_name(gint id) { const struct col_names_t* cn; for(cn = colnames; cn->name; cn++) { if ( cn->id == id ) { @@ -406,9 +358,7 @@ col_id_to_name(gint id) } -WSLUA_METAMETHOD -Column__tostring(lua_State *L) -{ +WSLUA_METAMETHOD Column__tostring(lua_State *L) { Column c = checkColumn(L,1); const gchar* name; @@ -423,9 +373,7 @@ Column__tostring(lua_State *L) WSLUA_RETURN(1); /* A string representing the column */ } -static int -Column__gc(lua_State* L) -{ +static int Column__gc(lua_State* L) { Column col = checkColumn(L,1); if (!col) return 0; @@ -439,10 +387,8 @@ Column__gc(lua_State* L) } -/* Clears a Column */ -WSLUA_METHOD -Column_clear(lua_State *L) -{ +WSLUA_METHOD Column_clear(lua_State *L) { + /* Clears a Column */ Column c = checkColumn(L,1); if (!(c && c->cinfo)) return 0; @@ -452,11 +398,9 @@ Column_clear(lua_State *L) return 0; } -/* Sets the text of a Column */ +WSLUA_METHOD Column_set(lua_State *L) { + /* Sets the text of a Column */ #define WSLUA_ARG_Column_set_TEXT 2 /* The text to which to set the Column */ -WSLUA_METHOD -Column_set(lua_State *L) -{ Column c = checkColumn(L,1); const gchar* s = luaL_checkstring(L,WSLUA_ARG_Column_set_TEXT); @@ -470,11 +414,9 @@ Column_set(lua_State *L) return 0; } -/* Appends text to a Column */ +WSLUA_METHOD Column_append(lua_State *L) { + /* Appends text to a Column */ #define WSLUA_ARG_Column_append_TEXT 2 /* The text to append to the Column */ -WSLUA_METHOD -Column_append(lua_State *L) -{ Column c = checkColumn(L,1); const gchar* s = luaL_checkstring(L,WSLUA_ARG_Column_append_TEXT); @@ -488,11 +430,9 @@ Column_append(lua_State *L) return 0; } -/* Prepends text to a Column */ +WSLUA_METHOD Column_preppend(lua_State *L) { + /* Prepends text to a Column */ #define WSLUA_ARG_Column_prepend_TEXT 2 /* The text to prepend to the Column */ -WSLUA_METHOD -Column_preppend(lua_State *L) -{ Column c = checkColumn(L,1); const gchar* s = luaL_checkstring(L,WSLUA_ARG_Column_prepend_TEXT); @@ -523,8 +463,7 @@ WSLUA_META Column_meta[] = { }; -int -Column_register(lua_State *L) { +int Column_register(lua_State *L) { WSLUA_REGISTER_CLASS(Column); return 1; } @@ -534,22 +473,19 @@ Column_register(lua_State *L) { -/* The Columns of the packet list. */ WSLUA_CLASS_DEFINE(Columns,NOP,NOP); +/* The Columns of the packet list. */ -WSLUA_METAMETHOD -Columns__tostring(lua_State *L) -{ +WSLUA_METAMETHOD Columns__tostring(lua_State *L) { lua_pushstring(L,"Columns"); - WSLUA_RETURN(1); /* The string "Columns", no real use, just for debugging purposes. */ + WSLUA_RETURN(1); + /* The string "Columns", no real use, just for debugging purposes. */ } -/* Sets the text of a specific column */ +WSLUA_METAMETHOD Columns__newindex(lua_State *L) { + /* Sets the text of a specific column */ #define WSLUA_ARG_Columns__newindex_COLUMN 2 /* The name of the column to set */ #define WSLUA_ARG_Columns__newindex_TEXT 3 /* The text for the column */ -WSLUA_METAMETHOD -Columns__newindex(lua_State *L) -{ Columns cols = checkColumns(L,1); const struct col_names_t* cn; const char* colname; @@ -571,14 +507,12 @@ Columns__newindex(lua_State *L) } } - WSLUA_ARG_ERROR(Columns__newindex,COLUMN,"the column name must be a valid column"); + WSLUA_ARG_ERROR(Columns__newindex,COLUMN,"the column name must be a valid column"); return 0; } -WSLUA_METAMETHOD -Columns_index(lua_State *L) -{ +WSLUA_METAMETHOD Columns_index(lua_State *L) { Columns cols = checkColumns(L,1); const struct col_names_t* cn; const char* colname = luaL_checkstring(L,2); @@ -616,9 +550,7 @@ Columns_index(lua_State *L) return 0; } -static int -Columns_gc(lua_State* L) -{ +static int Columns_gc(lua_State* L) { Columns cols = checkColumns(L,1); if (!cols) return 0; @@ -642,23 +574,16 @@ static const luaL_reg Columns_meta[] = { }; -int -Columns_register(lua_State *L) -{ +int Columns_register(lua_State *L) { WSLUA_REGISTER_META(Columns); return 1; } -/* Packet information */ WSLUA_CLASS_DEFINE(Pinfo,FAIL_ON_NULL("expired pinfo"),NOP); +/* Packet information */ -static int -Pinfo_tostring(lua_State *L) -{ - lua_pushstring(L,"a Pinfo"); - return 1; -} +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) { \ Pinfo pinfo = checkPinfo(L,1); \ @@ -734,9 +659,7 @@ PINFO_GET_ADDRESS(Pinfo_dst,dst) PINFO_GET_LIGHTUSERDATA(Pinfo_private_data, pinfo->ws_pinfo->private_data) -static int -Pinfo_visited(lua_State *L) -{ +static int Pinfo_visited(lua_State *L) { Pinfo pinfo = checkPinfo(L,1); if (!pinfo) return 0; if (pinfo->expired) { @@ -748,9 +671,7 @@ Pinfo_visited(lua_State *L) } -static int -Pinfo_match(lua_State *L) -{ +static int Pinfo_match(lua_State *L) { Pinfo pinfo = checkPinfo(L,1); if (!pinfo) return 0; @@ -768,9 +689,7 @@ Pinfo_match(lua_State *L) return 1; } -static int -Pinfo_columns(lua_State *L) -{ +static int Pinfo_columns(lua_State *L) { Columns cols = NULL; Pinfo pinfo = checkPinfo(L,1); const gchar* colname = luaL_optstring(L,2,NULL); @@ -812,16 +731,12 @@ typedef enum { PARAM_PORT_TYPE } pinfo_param_type_t; -static int -pushnil_param(lua_State* L, packet_info* pinfo _U_, pinfo_param_type_t pt _U_ ) -{ +static int pushnil_param(lua_State* L, packet_info* pinfo _U_, pinfo_param_type_t pt _U_ ) { lua_pushnil(L); return 1; } -static int -Pinfo_set_addr(lua_State* L, packet_info* pinfo, pinfo_param_type_t pt) -{ +static int Pinfo_set_addr(lua_State* L, packet_info* pinfo, pinfo_param_type_t pt) { const address* from = checkAddress(L,1); address* to; @@ -863,9 +778,7 @@ Pinfo_set_addr(lua_State* L, packet_info* pinfo, pinfo_param_type_t pt) return 0; } -static int -Pinfo_set_int(lua_State* L, packet_info* pinfo, pinfo_param_type_t pt) -{ +static int Pinfo_set_int(lua_State* L, packet_info* pinfo, pinfo_param_type_t pt) { gint64 v = luaL_checkint(L,1); if (!pinfo) { @@ -903,9 +816,7 @@ typedef struct _pinfo_method_t { pinfo_param_type_t param; } pinfo_method_t; -static int -Pinfo_hi(lua_State *L) -{ +static int Pinfo_hi(lua_State *L) { Pinfo pinfo = checkPinfo(L,1); Address addr = g_malloc(sizeof(address)); @@ -925,9 +836,7 @@ Pinfo_hi(lua_State *L) return 1; } -static int -Pinfo_lo(lua_State *L) -{ +static int Pinfo_lo(lua_State *L) { Pinfo pinfo = checkPinfo(L,1); Address addr = g_malloc(sizeof(address)); @@ -1038,16 +947,12 @@ static const pinfo_method_t Pinfo_methods[] = { }; -static int -pushnil(lua_State* L) -{ +static int pushnil(lua_State* L) { lua_pushnil(L); return 1; } -static int -Pinfo_index(lua_State* L) -{ +static int Pinfo_index(lua_State* L) { Pinfo pinfo = checkPinfo(L,1); const gchar* name = luaL_checkstring(L,2); lua_CFunction method = pushnil; @@ -1073,9 +978,7 @@ Pinfo_index(lua_State* L) return method(L); } -static int -Pinfo_setindex(lua_State* L) -{ +static int Pinfo_setindex(lua_State* L) { Pinfo pinfo = checkPinfo(L,1); const gchar* name = luaL_checkstring(L,2); int (*method)(lua_State*, packet_info* pinfo, pinfo_param_type_t) = pushnil_param; @@ -1103,9 +1006,7 @@ Pinfo_setindex(lua_State* L) return method(L,pinfo->ws_pinfo,param_type); } -static int -Pinfo_gc(lua_State* L) -{ +static int Pinfo_gc(lua_State* L) { Pinfo pinfo = checkPinfo(L,1); if (!pinfo) return 0; @@ -1127,9 +1028,7 @@ static const luaL_reg Pinfo_meta[] = { { NULL, NULL } }; -int -Pinfo_register(lua_State* L) -{ +int Pinfo_register(lua_State* L) { WSLUA_REGISTER_META(Pinfo); outstanding_Pinfo = g_ptr_array_new(); outstanding_Column = g_ptr_array_new(); diff --git a/epan/wslua/wslua_proto.c b/epan/wslua/wslua_proto.c index abd8b7be06..3437f00343 100644 --- a/epan/wslua/wslua_proto.c +++ b/epan/wslua/wslua_proto.c @@ -33,16 +33,14 @@ WSLUA_CLASS_DEFINE(Pref,NOP,NOP); /* A preference of a Protocol. */ -static range_t* -get_range(lua_State *L, int idx_r, int idx_m) +static range_t* get_range(lua_State *L, int idx_r, int idx_m) { static range_t *ret; range_convert_str(&ret,g_strdup(lua_tostring(L, idx_r)),(guint32)lua_tonumber(L, idx_m)); return ret; } -static enum_val_t* -get_enum(lua_State *L, int idx) +static enum_val_t* get_enum(lua_State *L, int idx) { double seq; const gchar *str1, *str2; @@ -101,9 +99,7 @@ get_enum(lua_State *L, int idx) return ret; } -static int -new_pref(lua_State* L, pref_type_t type) -{ +static int new_pref(lua_State* L, pref_type_t type) { const gchar* label = luaL_optstring(L,1,NULL); const gchar* descr = luaL_optstring(L,3,""); @@ -161,72 +157,58 @@ new_pref(lua_State* L, pref_type_t type) return 1; } -/* Creates a boolean preference to be added to a Protocol's prefs table. */ +WSLUA_CONSTRUCTOR Pref_bool(lua_State* L) { + /* Creates a boolean preference to be added to a Protocol's prefs table. */ #define WSLUA_ARG_Pref_bool_LABEL 1 /* The Label (text in the right side of the preference input) for this preference */ #define WSLUA_ARG_Pref_bool_DEFAULT 2 /* The default value for this preference */ #define WSLUA_ARG_Pref_bool_DESCR 3 /* A description of what this preference is */ -WSLUA_CONSTRUCTOR -Pref_bool(lua_State* L) -{ return new_pref(L,PREF_BOOL); } -/* Creates an (unsigned) integer preference to be added to a Protocol's prefs table. */ +WSLUA_CONSTRUCTOR Pref_uint(lua_State* L) { + /* Creates an (unsigned) integer preference to be added to a Protocol's prefs table. */ #define WSLUA_ARG_Pref_uint_LABEL 1 /* The Label (text in the right side of the preference input) for this preference */ #define WSLUA_ARG_Pref_uint_DEFAULT 2 /* The default value for this preference */ #define WSLUA_ARG_Pref_uint_DESCR 3 /* A description of what this preference is */ -WSLUA_CONSTRUCTOR -Pref_uint(lua_State* L) -{ return new_pref(L,PREF_UINT); } -/* Creates a string preference to be added to a Protocol's prefs table. */ +WSLUA_CONSTRUCTOR Pref_string(lua_State* L) { + /* Creates a string preference to be added to a Protocol's prefs table. */ #define WSLUA_ARG_Pref_string_LABEL 1 /* The Label (text in the right side of the preference input) for this preference */ #define WSLUA_ARG_Pref_string_DEFAULT 2 /* The default value for this preference */ #define WSLUA_ARG_Pref_string_DESCR 3 /* A description of what this preference is */ -WSLUA_CONSTRUCTOR -Pref_string(lua_State* L) -{ return new_pref(L,PREF_STRING); } -/* Creates an enum preference to be added to a Protocol's prefs table. */ +WSLUA_CONSTRUCTOR Pref_enum(lua_State* L) { + /* Creates an enum preference to be added to a Protocol's prefs table. */ #define WSLUA_ARG_Pref_enum_LABEL 1 /* The Label (text in the right side of the preference input) for this preference */ #define WSLUA_ARG_Pref_enum_DEFAULT 2 /* The default value for this preference */ #define WSLUA_ARG_Pref_enum_DESCR 3 /* A description of what this preference is */ #define WSLUA_ARG_Pref_enum_ENUM 4 /* A enum table */ #define WSLUA_ARG_Pref_enum_RADIO 5 /* Radio button (true) or Combobox (false) */ -WSLUA_CONSTRUCTOR -Pref_enum(lua_State* L) -{ return new_pref(L,PREF_ENUM); } -/* Creates a range preference to be added to a Protocol's prefs table. */ +WSLUA_CONSTRUCTOR Pref_range(lua_State* L) { + /* Creates a range preference to be added to a Protocol's prefs table. */ #define WSLUA_ARG_Pref_range_LABEL 1 /* The Label (text in the right side of the preference input) for this preference */ #define WSLUA_ARG_Pref_range_DEFAULT 2 /* The default value for this preference */ #define WSLUA_ARG_Pref_range_DESCR 3 /* A description of what this preference is */ #define WSLUA_ARG_Pref_range_RANGE 4 /* The range */ #define WSLUA_ARG_Pref_range_MAX 5 /* The maximum value */ -WSLUA_CONSTRUCTOR -Pref_range(lua_State* L) -{ return new_pref(L,PREF_RANGE); } -/* Creates a static text preference to be added to a Protocol's prefs table. */ +WSLUA_CONSTRUCTOR Pref_statictext(lua_State* L) { + /* Creates a static text preference to be added to a Protocol's prefs table. */ #define WSLUA_ARG_Pref_statictext_LABEL 1 /* The static text */ #define WSLUA_ARG_Pref_statictext_DESCR 2 /* The static text description */ -WSLUA_CONSTRUCTOR -Pref_statictext(lua_State* L) -{ return new_pref(L,PREF_STATIC_TEXT); } -static int -Pref_gc(lua_State* L) -{ +static int Pref_gc(lua_State* L) { Pref pref = checkPref(L,1); if (pref && ! pref->name) { @@ -256,21 +238,17 @@ WSLUA_META Pref_meta[] = { }; -WSLUA_REGISTER -Pref_register(lua_State* L) -{ +WSLUA_REGISTER Pref_register(lua_State* L) { WSLUA_REGISTER_CLASS(Pref); return 1; } WSLUA_CLASS_DEFINE(Prefs,NOP,NOP); /* The table of preferences of a protocol */ -/* Creates a new preference */ +WSLUA_METAMETHOD Prefs__newindex(lua_State* L) { + /* Creates a new preference */ #define WSLUA_ARG_Prefs__newindex_NAME 2 /* The abbreviation of this preference */ #define WSLUA_ARG_Prefs__newindex_PREF 3 /* A valid but still unassigned Pref object */ -WSLUA_METAMETHOD -Prefs__newindex(lua_State* L) -{ Pref prefs_p = checkPrefs(L,1); const gchar* name = luaL_checkstring(L,WSLUA_ARG_Prefs__newindex_NAME); @@ -299,18 +277,18 @@ Prefs__newindex(lua_State* L) luaL_error(L,"a preference named %s exists already",name); return 0; } - /* + /* * Make sure that only lower-case ASCII letters, numbers, * underscores, and dots appear in the preference name. - */ - for (c = name; *c != '\0'; c++) { - if (!isascii((guchar)*c) || - (!islower((guchar)*c) && !isdigit((guchar)*c) && *c != '_' && *c != '.')) - { - luaL_error(L,"illegal preference name \"%s\", only lower-case ASCII letters, numbers, underscores and dots may be used",name); - return 0; - } - } + */ + for (c = name; *c != '\0'; c++) { + if (!isascii((guchar)*c) || + (!islower((guchar)*c) && !isdigit((guchar)*c) && *c != '_' && *c != '.')) + { + luaL_error(L,"illegal preference name \"%s\", only lower-case ASCII letters, numbers, underscores and dots may be used",name); + return 0; + } + } if ( ! p->next) { p->next = pref; @@ -384,11 +362,9 @@ Prefs__newindex(lua_State* L) WSLUA_RETURN(0); } -/* Get the value of a preference setting */ +WSLUA_METAMETHOD Prefs__index(lua_State* L) { + /* Get the value of a preference setting */ #define WSLUA_ARG_Prefs__index_NAME 2 /* The abbreviation of this preference */ -WSLUA_METAMETHOD -Prefs__index(lua_State* L) -{ Pref prefs_p = checkPrefs(L,1); const gchar* name = luaL_checkstring(L,WSLUA_ARG_Prefs__index_NAME); @@ -426,8 +402,8 @@ WSLUA_REGISTER Prefs_register(lua_State* L) { return 1; } -/* A Protocol field (to be used when adding items to the dissection tree) */ WSLUA_CLASS_DEFINE(ProtoField,FAIL_ON_NULL("null ProtoField"),NOP); + /* A Protocol field (to be used when adding items to the dissection tree) */ static const wslua_ft_types_t ftenums[] = { {"FT_BOOLEAN",FT_BOOLEAN}, @@ -457,9 +433,7 @@ static const wslua_ft_types_t ftenums[] = { {NULL,FT_NONE} }; -static enum ftenum -get_ftenum(const gchar* type) -{ +static enum ftenum get_ftenum(const gchar* type) { const wslua_ft_types_t* ts; for (ts = ftenums; ts->str; ts++) { if ( g_str_equal(ts->str,type) ) { @@ -469,9 +443,7 @@ get_ftenum(const gchar* type) return FT_NONE; } -static const gchar* -ftenum_to_string(enum ftenum ft) -{ +static const gchar* ftenum_to_string(enum ftenum ft) { const wslua_ft_types_t* ts; for (ts = ftenums; ts->str; ts++) { if ( ts->id == ft ) { @@ -501,9 +473,7 @@ static const struct base_display_string_t base_displays[] = { {NULL,0} }; -static const gchar* -base_to_string(base_display_e base) -{ +static const gchar* base_to_string(base_display_e base) { const struct base_display_string_t* b; for (b=base_displays;b->str;b++) { if ( base == b->base) @@ -512,9 +482,7 @@ base_to_string(base_display_e base) return NULL; } -static base_display_e -string_to_base(const gchar* str) -{ +static base_display_e string_to_base(const gchar* str) { const struct base_display_string_t* b; for (b=base_displays;b->str;b++) { if ( g_str_equal(str,b->str)) @@ -523,9 +491,7 @@ string_to_base(const gchar* str) return BASE_NONE; } -static value_string* -value_string_from_table(lua_State* L, int idx) -{ +static value_string* value_string_from_table(lua_State* L, int idx) { GArray* vs = g_array_new(TRUE,TRUE,sizeof(value_string)); value_string* ret; @@ -569,9 +535,7 @@ value_string_from_table(lua_State* L, int idx) return ret; } -static true_false_string* -true_false_string_from_table(lua_State* L, int idx) -{ +static true_false_string* true_false_string_from_table(lua_State* L, int idx) { GArray* tfs = g_array_new(TRUE,TRUE,sizeof(true_false_string)); true_false_string* ret; true_false_string tf = { "True", "False" }; @@ -619,7 +583,7 @@ true_false_string_from_table(lua_State* L, int idx) return ret; } -/* Creates a new field to be used in a protocol. */ +WSLUA_CONSTRUCTOR ProtoField_new(lua_State* L) { /* Creates a new field to be used in a protocol. */ #define WSLUA_ARG_ProtoField_new_NAME 1 /* Actual name of the field (the string that appears in the tree). */ #define WSLUA_ARG_ProtoField_new_ABBR 2 /* Filter name of the field (the string that is used in filters). */ #define WSLUA_ARG_ProtoField_new_TYPE 3 /* Field Type (FT_*). */ @@ -627,9 +591,6 @@ true_false_string_from_table(lua_State* L, int idx) #define WSLUA_OPTARG_ProtoField_new_BASE 5 /* The representation BASE_*. */ #define WSLUA_OPTARG_ProtoField_new_MASK 6 /* The bitmask to be used. */ #define WSLUA_OPTARG_ProtoField_new_DESCR 7 /* The description of the field. */ -WSLUA_CONSTRUCTOR -ProtoField_new(lua_State* L) -{ ProtoField f = g_malloc(sizeof(wslua_field_t)); value_string* vs = NULL; @@ -685,9 +646,7 @@ ProtoField_new(lua_State* L) WSLUA_RETURN(1); /* The newly created ProtoField object */ } -static int -ProtoField_integer(lua_State* L, enum ftenum type) -{ +static int ProtoField_integer(lua_State* L, enum ftenum type) { ProtoField f = g_malloc(sizeof(wslua_field_t)); const gchar* abbr = luaL_checkstring(L,1); const gchar* name = luaL_optstring(L,2,abbr); @@ -833,9 +792,7 @@ PROTOFIELD_INTEGER(int32,FT_INT32) PROTOFIELD_INTEGER(int64,FT_INT64) PROTOFIELD_INTEGER(framenum,FT_FRAMENUM) -static int -ProtoField_boolean(lua_State* L, enum ftenum type) -{ +static int ProtoField_boolean(lua_State* L, enum ftenum type) { ProtoField f = g_malloc(sizeof(wslua_field_t)); const gchar* abbr = luaL_checkstring(L,1); const gchar* name = luaL_optstring(L,2,abbr); @@ -888,9 +845,7 @@ ProtoField_boolean(lua_State* L, enum ftenum type) /* XXX: T/F strings */ PROTOFIELD_BOOL(bool,FT_BOOLEAN) -static int -ProtoField_other(lua_State* L,enum ftenum type) -{ +static int ProtoField_other(lua_State* L,enum ftenum type) { ProtoField f = g_malloc(sizeof(wslua_field_t)); const gchar* abbr = luaL_checkstring(L,1); const gchar* name = luaL_optstring(L,2,abbr); @@ -1001,9 +956,7 @@ PROTOFIELD_OTHER(ubytes,FT_UINT_BYTES) PROTOFIELD_OTHER(guid,FT_GUID) PROTOFIELD_OTHER(oid,FT_OID) -WSLUA_METAMETHOD -ProtoField__tostring(lua_State* L) -{ +WSLUA_METAMETHOD ProtoField__tostring(lua_State* L) { /* Returns a string with info about a protofield (for debugging purposes) */ ProtoField f = checkProtoField(L,1); gchar* s = ep_strdup_printf("ProtoField(%i): %s %s %s %s %p %.8x %s",f->hfid,f->name,f->abbr,ftenum_to_string(f->type),base_to_string(f->base),f->vs,f->mask,f->blob); @@ -1011,9 +964,7 @@ ProtoField__tostring(lua_State* L) return 1; } -static int -ProtoField_gc(lua_State* L) -{ +static int ProtoField_gc(lua_State* L) { ProtoField f = checkProtoField(L,1); /* @@ -1072,27 +1023,23 @@ static const luaL_reg ProtoField_meta[] = { { NULL, NULL } }; -int -ProtoField_register(lua_State* L) -{ +int ProtoField_register(lua_State* L) { WSLUA_REGISTER_CLASS(ProtoField); return 1; } +WSLUA_CLASS_DEFINE(Proto,NOP,NOP); /* - * A new protocol in wireshark. Protocols have more uses, the main one is to dissect - * a protocol. But they can be just dummies used to register preferences for - * other purposes. + A new protocol in wireshark. Protocols have more uses, the main one is to dissect + a protocol. But they can be just dummies used to register preferences for + other purposes. */ -WSLUA_CLASS_DEFINE(Proto,NOP,NOP); static int protocols_table_ref = LUA_NOREF; +WSLUA_CONSTRUCTOR Proto_new(lua_State* L) { #define WSLUA_ARG_Proto_new_NAME 1 /* The name of the protocol */ #define WSLUA_ARG_Proto_new_DESC 2 /* A Long Text description of the protocol (usually lowercase) */ -WSLUA_CONSTRUCTOR -Proto_new(lua_State* L) -{ const gchar* name = luaL_checkstring(L,WSLUA_ARG_Proto_new_NAME); const gchar* desc = luaL_checkstring(L,WSLUA_ARG_Proto_new_DESC); @@ -1146,9 +1093,7 @@ Proto_new(lua_State* L) return 0; } -static int -Proto_tostring(lua_State* L) -{ +static int Proto_tostring(lua_State* L) { Proto proto = checkProto(L,1); gchar* s; @@ -1160,10 +1105,9 @@ Proto_tostring(lua_State* L) return 1; } -/* Make a protocol (with a dissector) a postdissector. It will be called for every frame after dissection */ +WSLUA_FUNCTION wslua_register_postdissector(lua_State* L) { + /* Make a protocol (with a dissector) a postdissector. It will be called for every frame after dissection */ #define WSLUA_ARG_register_postdissector_PROTO 1 /* the protocol to be used as postdissector */ -WSLUA_FUNCTION wslua_register_postdissector(lua_State* L) -{ Proto proto = checkProto(L,WSLUA_ARG_register_postdissector_PROTO); if (!proto) return 0; @@ -1180,9 +1124,7 @@ WSLUA_FUNCTION wslua_register_postdissector(lua_State* L) return 0; } -static int -Proto_get_dissector(lua_State* L) -{ +static int Proto_get_dissector(lua_State* L) { Proto proto = toProto(L,1); if (proto->handle) { @@ -1194,9 +1136,7 @@ Proto_get_dissector(lua_State* L) } } -static int -Proto_set_dissector(lua_State* L) -{ +static int Proto_set_dissector(lua_State* L) { Proto proto = toProto(L,1); if (lua_isfunction(L,3)) { @@ -1221,17 +1161,13 @@ Proto_set_dissector(lua_State* L) } } -static int -Proto_get_prefs(lua_State* L) -{ +static int Proto_get_prefs(lua_State* L) { Proto proto = toProto(L,1); pushPrefs(L,&proto->prefs); return 1; } -static int -Proto_set_init(lua_State* L) -{ +static int Proto_set_init(lua_State* L) { Proto proto = toProto(L,1); if (lua_isfunction(L,3)) { @@ -1250,25 +1186,19 @@ Proto_set_init(lua_State* L) } } -static int -Proto_get_name(lua_State* L) -{ +static int Proto_get_name(lua_State* L) { Proto proto = toProto(L,1); lua_pushstring(L,proto->name); return 1; } -static int -Proto_get_fields(lua_State* L) -{ +static int Proto_get_fields(lua_State* L) { Proto proto = toProto(L,1); lua_rawgeti(L, LUA_REGISTRYINDEX, proto->fields); return 1; } -void -wslua_print_stack(char* s, lua_State* L) -{ +void wslua_print_stack(char* s, lua_State* L) { int i; for (i=1;i<=lua_gettop(L);i++) { @@ -1277,13 +1207,11 @@ wslua_print_stack(char* s, lua_State* L) printf("\n"); } +static int Proto_set_fields(lua_State* L) { + Proto proto = toProto(L,1); #define FIELDS_TABLE 2 #define NEW_TABLE 3 #define NEW_FIELD 3 -static int -Proto_set_fields(lua_State* L) -{ - Proto proto = toProto(L,1); lua_rawgeti(L, LUA_REGISTRYINDEX, proto->fields); lua_replace(L,FIELDS_TABLE); @@ -1333,9 +1261,7 @@ static const proto_actions_t proto_actions[] = { {NULL,NULL,NULL} }; -static int -Proto_index(lua_State* L) -{ +static int Proto_index(lua_State* L) { Proto proto = checkProto(L,1); const gchar* name = luaL_checkstring(L,2); const proto_actions_t* pa; @@ -1357,9 +1283,7 @@ Proto_index(lua_State* L) return 0; } -static int -Proto_newindex(lua_State* L) -{ +static int Proto_newindex(lua_State* L) { Proto proto = checkProto(L,1); const gchar* name = luaL_checkstring(L,2); const proto_actions_t* pa; @@ -1388,9 +1312,7 @@ static const luaL_reg Proto_meta[] = { { NULL, NULL } }; -int -Proto_register(lua_State* L) -{ +int Proto_register(lua_State* L) { WSLUA_REGISTER_META(Proto); @@ -1407,9 +1329,7 @@ Proto_register(lua_State* L) return 1; } -int -Proto_commit(lua_State* L) -{ +int Proto_commit(lua_State* L) { lua_settop(L,0); lua_rawgeti(L, LUA_REGISTRYINDEX, protocols_table_ref); @@ -1446,16 +1366,14 @@ Proto_commit(lua_State* L) return 0; } +WSLUA_CLASS_DEFINE(Dissector,NOP,NOP); /* - * A refererence to a dissector, used to call a dissector against a packet or a part of it. + A refererence to a dissector, used to call a dissector against a packet or a part of it. */ -WSLUA_CLASS_DEFINE(Dissector,NOP,NOP); -/* Obtains a dissector reference by name */ +WSLUA_CONSTRUCTOR Dissector_get (lua_State *L) { + /* Obtains a dissector reference by name */ #define WSLUA_ARG_Dissector_get_NAME 1 /* The name of the dissector */ -WSLUA_CONSTRUCTOR -Dissector_get (lua_State *L) -{ const gchar* name = luaL_checkstring(L,WSLUA_ARG_Dissector_get_NAME); Dissector d; @@ -1469,13 +1387,11 @@ Dissector_get (lua_State *L) WSLUA_ARG_ERROR(Dissector_get,NAME,"No such dissector"); } -/* Calls a dissector against a given packet (or part of it) */ +WSLUA_METHOD Dissector_call(lua_State* L) { + /* Calls a dissector against a given packet (or part of it) */ #define WSLUA_ARG_Dissector_call_TVB 2 /* The buffer to dissect */ #define WSLUA_ARG_Dissector_call_PINFO 3 /* The packet info */ #define WSLUA_ARG_Dissector_call_TREE 4 /* The tree on which to add the protocol items */ -WSLUA_METHOD -Dissector_call(lua_State* L) -{ Dissector d = checkDissector(L,1); Tvb tvb = checkTvb(L,WSLUA_ARG_Dissector_call_TVB); @@ -1498,9 +1414,7 @@ Dissector_call(lua_State* L) return 0; } -WSLUA_METAMETHOD -Dissector_tostring(lua_State* L) -{ +WSLUA_METAMETHOD Dissector_tostring(lua_State* L) { Dissector d = checkDissector(L,1); if (!d) return 0; lua_pushstring(L,dissector_handle_get_short_name(d)); @@ -1518,27 +1432,23 @@ static const luaL_reg Dissector_meta[] = { { NULL, NULL } }; -int -Dissector_register(lua_State* L) -{ +int Dissector_register(lua_State* L) { WSLUA_REGISTER_CLASS(Dissector); return 1; } +WSLUA_CLASS_DEFINE(DissectorTable,NOP,NOP); /* - * A table of subdissectors of a particular protocol (e.g. TCP subdissectors like http, smtp, sip are added to table "tcp.port"). - * Useful to add more dissectors to a table so that they appear in the Decode As... dialog. + A table of subdissectors of a particular protocol (e.g. TCP subdissectors like http, smtp, sip are added to table "tcp.port"). + Useful to add more dissectors to a table so that they appear in the Decode As... dialog. */ -WSLUA_CLASS_DEFINE(DissectorTable,NOP,NOP); -/* Creates a new DissectorTable for your dissector's use. */ +WSLUA_CONSTRUCTOR DissectorTable_new (lua_State *L) { + /* Creates a new DissectorTable for your dissector's use. */ #define WSLUA_ARG_DissectorTable_new_TABLENAME 1 /* The short name of the table. */ #define WSLUA_OPTARG_DissectorTable_new_UINAME 2 /* The name of the table in the User Interface (defaults to the name given). */ #define WSLUA_OPTARG_DissectorTable_new_TYPE 3 /* Either FT_UINT* or FT_STRING (defaults to FT_UINT32) */ #define WSLUA_OPTARG_DissectorTable_new_BASE 4 /* Either BASE_NONE, BASE_DEC, BASE_HEX, BASE_OCT, BASE_DEC_HEX or BASE_HEX_DEC (defaults to BASE_DEC) */ -WSLUA_CONSTRUCTOR -DissectorTable_new (lua_State *L) -{ gchar* name = (void*)luaL_checkstring(L,WSLUA_ARG_DissectorTable_new_TABLENAME); gchar* ui_name = (void*)luaL_optstring(L,WSLUA_OPTARG_DissectorTable_new_UINAME,name); enum ftenum type = luaL_optint(L,WSLUA_OPTARG_DissectorTable_new_TYPE,FT_UINT32); @@ -1562,19 +1472,19 @@ DissectorTable_new (lua_State *L) dt->table = register_dissector_table(name, ui_name, type, base); dt->name = name; pushDissectorTable(L, dt); - WSLUA_RETURN(1); /* The newly created DissectorTable */ } + WSLUA_RETURN(1); /* The newly created DissectorTable */ default: WSLUA_OPTARG_ERROR(DissectorTable_new,TYPE,"must be FTUINT* or FT_STRING"); } return 0; } -/* Obtain a reference to an existing dissector table. */ +WSLUA_CONSTRUCTOR DissectorTable_get (lua_State *L) { + /* + Obtain a reference to an existing dissector table. + */ #define WSLUA_ARG_DissectorTable_get_TABLENAME 1 /* The short name of the table. */ -WSLUA_CONSTRUCTOR -DissectorTable_get (lua_State *L) -{ const gchar* name = luaL_checkstring(L,WSLUA_ARG_DissectorTable_get_TABLENAME); dissector_table_t table; @@ -1595,12 +1505,12 @@ DissectorTable_get (lua_State *L) } -/* Add a dissector to a table. */ +WSLUA_METHOD DissectorTable_add (lua_State *L) { + /* + Add a dissector to a table. + */ #define WSLUA_ARG_DissectorTable_add_PATTERN 2 /* The pattern to match (either an integer or a string depending on the table's type). */ #define WSLUA_ARG_DissectorTable_add_DISSECTOR 3 /* The dissector to add (either an Proto or a Dissector). */ -WSLUA_METHOD -DissectorTable_add (lua_State *L) -{ DissectorTable dt = checkDissectorTable(L,1); ftenum_t type; @@ -1636,12 +1546,12 @@ DissectorTable_add (lua_State *L) return 0; } -/* Remove a dissector from a table */ +WSLUA_METHOD DissectorTable_remove (lua_State *L) { + /* + Remove a dissector from a table + */ #define WSLUA_ARG_DissectorTable_remove_PATTERN 2 /* The pattern to match (either an integer or a string depending on the table's type). */ #define WSLUA_ARG_DissectorTable_remove_DISSECTOR 3 /* The dissector to add (either an Proto or a Dissector). */ -WSLUA_METHOD -DissectorTable_remove (lua_State *L) -{ DissectorTable dt = checkDissectorTable(L,1); ftenum_t type; Dissector handle; @@ -1671,14 +1581,14 @@ DissectorTable_remove (lua_State *L) return 0; } -/* Try to call a dissector from a table */ +WSLUA_METHOD DissectorTable_try (lua_State *L) { + /* + Try to call a dissector from a table + */ #define WSLUA_ARG_DissectorTable_try_PATTERN 2 /* The pattern to be matched (either an integer or a string depending on the table's type). */ #define WSLUA_ARG_DissectorTable_try_TVB 3 /* The buffer to dissect */ #define WSLUA_ARG_DissectorTable_try_PINFO 4 /* The packet info */ #define WSLUA_ARG_DissectorTable_try_TREE 5 /* The tree on which to add the protocol items */ -WSLUA_METHOD -DissectorTable_try (lua_State *L) -{ DissectorTable dt = checkDissectorTable(L,1); Tvb tvb = checkTvb(L,3); Pinfo pinfo = checkPinfo(L,4); @@ -1723,11 +1633,12 @@ DissectorTable_try (lua_State *L) return 0; } -/* Try to obtain a dissector from a table. */ +WSLUA_METHOD DissectorTable_get_dissector (lua_State *L) { + /* + Try to obtain a dissector from a table. + */ #define WSLUA_ARG_DissectorTable_try_PATTERN 2 /* The pattern to be matched (either an integer or a string depending on the table's type). */ -WSLUA_METHOD -DissectorTable_get_dissector (lua_State *L) -{ + DissectorTable dt = checkDissectorTable(L,1); ftenum_t type; dissector_handle_t handle = lua_data_handle; @@ -1756,10 +1667,8 @@ DissectorTable_get_dissector (lua_State *L) } } +WSLUA_METAMETHOD DissectorTable_tostring(lua_State* L) { /**/ -WSLUA_METAMETHOD -DissectorTable_tostring(lua_State* L) -{ /* XXX It would be nice to iterate and print which dissectors it has */ DissectorTable dt = checkDissectorTable(L,1); GString* s; @@ -1809,9 +1718,7 @@ static const luaL_reg DissectorTable_meta[] = { { NULL, NULL } }; -int -DissectorTable_register(lua_State* L) -{ +int DissectorTable_register(lua_State* L) { WSLUA_REGISTER_CLASS(DissectorTable); return 1; } diff --git a/epan/wslua/wslua_tree.c b/epan/wslua/wslua_tree.c index 1b01017c1d..eb48103cf2 100644 --- a/epan/wslua/wslua_tree.c +++ b/epan/wslua/wslua_tree.c @@ -38,23 +38,18 @@ static GPtrArray* outstanding_TreeItem = NULL; #define PUSH_TREEITEM(L,i) {g_ptr_array_add(outstanding_TreeItem,i);pushTreeItem(L,i);} -TreeItem* -push_TreeItem(lua_State*L, TreeItem t) -{ +TreeItem* push_TreeItem(lua_State*L, TreeItem t) { g_ptr_array_add(outstanding_TreeItem,t); return pushTreeItem(L,t); } CLEAR_OUTSTANDING(TreeItem, expired, TRUE) -/* TreeItems represent information in the packet-details pane. - * A root TreeItem is passed to dissectors as first argument. - */ WSLUA_CLASS_DEFINE(TreeItem,NOP,NOP); +/* TreeItems represent information in the packet-details pane. + A root TreeItem is passed to dissectors as first argument. */ -static int -TreeItem_add_item_any(lua_State *L, gboolean little_endian) -{ +static int TreeItem_add_item_any(lua_State *L, gboolean little_endian) { TvbRange tvbr; Proto proto; ProtoField field; @@ -197,34 +192,27 @@ TreeItem_add_item_any(lua_State *L, gboolean little_endian) } -/* - * Adds an child item to a given item, returning the child. - * tree_item:add([proto_field | proto], [tvbrange], [label], ...) - * if the proto_field represents a numeric value (int, uint or float) is to be - * treated as a Big Endian (network order) Value. - */ -WSLUA_METHOD -TreeItem_add(lua_State *L) -{ +WSLUA_METHOD TreeItem_add(lua_State *L) { + /* + Adds an child item to a given item, returning the child. + tree_item:add([proto_field | proto], [tvbrange], [label], ...) + if the proto_field represents a numeric value (int, uint or float) is to be treated as a Big Endian (network order) Value. + */ WSLUA_RETURN(TreeItem_add_item_any(L,FALSE)); /* The child item */ } -/* - * Adds (and returns) an child item to a given item, returning the child. - * tree_item:add([proto_field | proto], [tvbrange], [label], ...) - * if the proto_field represents a numeric value (int, uint or float) is to be treated as a Little Endian Value. - */ -WSLUA_METHOD -TreeItem_add_le(lua_State *L) -{ +WSLUA_METHOD TreeItem_add_le(lua_State *L) { + /* + Adds (and returns) an child item to a given item, returning the child. + tree_item:add([proto_field | proto], [tvbrange], [label], ...) + if the proto_field represents a numeric value (int, uint or float) is to be treated as a Little Endian Value. + */ WSLUA_RETURN(TreeItem_add_item_any(L,TRUE)); /* The child item */ } -/* Sets the text of the label */ +WSLUA_METHOD TreeItem_set_text(lua_State *L) { + /* Sets the text of the label */ #define WSLUA_ARG_TreeItem_set_text_TEXT 2 /* The text to be used. */ -WSLUA_METHOD -TreeItem_set_text(lua_State *L) -{ TreeItem ti = checkTreeItem(L,1); const gchar* s; @@ -241,11 +229,9 @@ TreeItem_set_text(lua_State *L) return 0; } -/* Appends text to the label */ +WSLUA_METHOD TreeItem_append_text(lua_State *L) { + /* Appends text to the label */ #define WSLUA_ARG_TreeItem_append_text_TEXT 2 /* The text to be appended. */ -WSLUA_METHOD -TreeItem_append_text(lua_State *L) -{ TreeItem ti = checkTreeItem(L,1); const gchar* s; @@ -261,12 +247,10 @@ TreeItem_append_text(lua_State *L) return 0; } -/* Sets the expert flags of the item. */ +WSLUA_METHOD TreeItem_set_expert_flags(lua_State *L) { + /* Sets the expert flags of the item. */ #define WSLUA_OPTARG_TreeItem_set_expert_flags_GROUP 2 /* One of PI_CHECKSUM, PI_SEQUENCE, PI_RESPONSE_CODE, PI_REQUEST_CODE, PI_UNDECODED, PI_REASSEMBLE, PI_MALFORMED or PI_DEBUG */ #define WSLUA_OPTARG_TreeItem_set_expert_flags_SEVERITY 3 /* One of PI_CHAT, PI_NOTE, PI_WARN, PI_ERROR */ -WSLUA_METHOD -TreeItem_set_expert_flags(lua_State *L) -{ TreeItem ti = checkTreeItem(L,1); int group = luaL_optint(L,WSLUA_OPTARG_TreeItem_set_expert_flags_GROUP,PI_DEBUG); int severity = luaL_optint(L,WSLUA_OPTARG_TreeItem_set_expert_flags_SEVERITY,PI_CHAT); @@ -282,13 +266,11 @@ TreeItem_set_expert_flags(lua_State *L) return 0; } -/* Sets the expert flags of the item and adds expert info to the packet. */ +WSLUA_METHOD TreeItem_add_expert_info(lua_State *L) { + /* Sets the expert flags of the item and adds expert info to the packet. */ #define WSLUA_OPTARG_TreeItem_add_expert_info_GROUP 2 /* One of PI_CHECKSUM, PI_SEQUENCE, PI_RESPONSE_CODE, PI_REQUEST_CODE, PI_UNDECODED, PI_REASSEMBLE, PI_MALFORMED or PI_DEBUG */ #define WSLUA_OPTARG_TreeItem_add_expert_info_SEVERITY 3 /* One of PI_CHAT, PI_NOTE, PI_WARN, PI_ERROR */ #define WSLUA_OPTARG_TreeItem_add_expert_info_TEXT 4 /* The text for the expert info */ -WSLUA_METHOD -TreeItem_add_expert_info(lua_State *L) -{ TreeItem ti = checkTreeItem(L,1); int group = luaL_optint(L,WSLUA_OPTARG_TreeItem_add_expert_info_GROUP,PI_DEBUG); int severity = luaL_optint(L,WSLUA_OPTARG_TreeItem_add_expert_info_SEVERITY,PI_CHAT); @@ -305,10 +287,8 @@ TreeItem_add_expert_info(lua_State *L) return 0; } -/* Marks the TreeItem as a generated field (with data infered but not contained in the packet). */ -WSLUA_METHOD -TreeItem_set_generated(lua_State *L) -{ +WSLUA_METHOD TreeItem_set_generated(lua_State *L) { + /* Marks the TreeItem as a generated field (with data infered but not contained in the packet). */ TreeItem ti = checkTreeItem(L,1); if (ti) { if (ti->expired) { @@ -321,10 +301,8 @@ TreeItem_set_generated(lua_State *L) } -/* Should not be used */ -WSLUA_METHOD -TreeItem_set_hidden(lua_State *L) -{ +WSLUA_METHOD TreeItem_set_hidden(lua_State *L) { + /* Should not be used */ TreeItem ti = checkTreeItem(L,1); if (ti) { if (ti->expired) { @@ -336,11 +314,9 @@ TreeItem_set_hidden(lua_State *L) return 0; } -/* Set TreeItem's length inside tvb, after it has already been created. */ +WSLUA_METHOD TreeItem_set_len(lua_State *L) { + /* Set TreeItem's length inside tvb, after it has already been created. */ #define WSLUA_ARG_TreeItem_set_len_LEN 2 /* The length to be used. */ -WSLUA_METHOD -TreeItem_set_len(lua_State *L) -{ TreeItem ti = checkTreeItem(L,1); gint len; @@ -357,9 +333,7 @@ TreeItem_set_len(lua_State *L) return 0; } -static int -TreeItem_gc(lua_State* L) -{ +static int TreeItem_gc(lua_State* L) { TreeItem ti = checkTreeItem(L,1); if (!ti) return 0; if (!ti->expired) @@ -387,9 +361,7 @@ static const luaL_reg TreeItem_meta[] = { { NULL, NULL } }; -int -TreeItem_register(lua_State *L) -{ +int TreeItem_register(lua_State *L) { gint* etts[] = { &wslua_ett }; WSLUA_REGISTER_CLASS(TreeItem); outstanding_TreeItem = g_ptr_array_new(); diff --git a/epan/wslua/wslua_tvb.c b/epan/wslua/wslua_tvb.c index faef2f8516..5df3b9fa74 100644 --- a/epan/wslua/wslua_tvb.c +++ b/epan/wslua/wslua_tvb.c @@ -34,11 +34,8 @@ WSLUA_CLASS_DEFINE(ByteArray,FAIL_ON_NULL("null bytearray"),NOP); -/* Creates a ByteArray Object */ +WSLUA_CONSTRUCTOR ByteArray_new(lua_State* L) { /* Creates a ByteArray Object */ #define WSLUA_OPTARG_ByteArray_new_HEXBYTES 1 /* A string consisting of hexadecimal bytes like "00 B1 A2" or "1a2b3c4d" */ -WSLUA_CONSTRUCTOR -ByteArray_new(lua_State* L) -{ GByteArray* ba = g_byte_array_new(); const gchar* s; int nibble[2]; @@ -82,9 +79,7 @@ ByteArray_new(lua_State* L) WSLUA_RETURN(1); /* The new ByteArray object. */ } -static int -ByteArray_gc(lua_State* L) -{ +static int ByteArray_gc(lua_State* L) { ByteArray ba = checkByteArray(L,1); if (!ba) return 0; @@ -93,12 +88,10 @@ ByteArray_gc(lua_State* L) return 0; } -/* Concatenate two ByteArrays */ +WSLUA_METAMETHOD ByteArray__concat(lua_State* L) { + /* Concatenate two ByteArrays */ #define WSLUA_ARG_ByteArray__cat_FIRST 1 /* First array */ #define WSLUA_ARG_ByteArray__cat_SECOND 2 /* Second array */ -WSLUA_METAMETHOD -ByteArray__concat(lua_State* L) -{ ByteArray ba = checkByteArray(L,WSLUA_ARG_ByteArray__cat_FIRST); ByteArray ba2 = checkByteArray(L,WSLUA_ARG_ByteArray__cat_SECOND); @@ -112,11 +105,9 @@ ByteArray__concat(lua_State* L) WSLUA_RETURN(1); /* The new composite ByteArray. */ } -/* Prepend a ByteArray to this ByteArray */ +WSLUA_METHOD ByteArray_prepend(lua_State* L) { + /* Prepend a ByteArray to this ByteArray */ #define WSLUA_ARG_ByteArray_prepend_PREPENDED 2 /* Array to be prepended */ -WSLUA_METHOD -ByteArray_prepend(lua_State* L) -{ ByteArray ba = checkByteArray(L,1); ByteArray ba2 = checkByteArray(L,WSLUA_ARG_ByteArray_prepend_PREPENDED); @@ -129,11 +120,9 @@ ByteArray_prepend(lua_State* L) return 1; } -/* Append a ByteArray to this ByteArray */ +WSLUA_METHOD ByteArray_append(lua_State* L) { + /* Append a ByteArray to this ByteArray */ #define WSLUA_ARG_ByteArray_append_APPENDED 2 /* Array to be appended */ -WSLUA_METHOD -ByteArray_append(lua_State* L) -{ ByteArray ba = checkByteArray(L,1); ByteArray ba2 = checkByteArray(L,WSLUA_ARG_ByteArray_append_APPENDED); @@ -146,11 +135,9 @@ ByteArray_append(lua_State* L) return 1; } -/* Sets the size of a ByteArray, either truncating it or filling it with zeros. */ +WSLUA_METHOD ByteArray_set_size(lua_State* L) { + /* Sets the size of a ByteArray, either truncating it or filling it with zeros. */ #define WSLUA_ARG_ByteArray_set_size_SIZE 2 /* New size of the array*/ -WSLUA_METHOD -ByteArray_set_size(lua_State* L) -{ ByteArray ba = checkByteArray(L,1); int siz = luaL_checkint(L,WSLUA_ARG_ByteArray_set_size_SIZE); @@ -172,12 +159,10 @@ ByteArray_set_size(lua_State* L) return 0; } -/* Sets the value of an index of a ByteArray. */ +WSLUA_METHOD ByteArray_set_index(lua_State* L) { + /* Sets the value of an index of a ByteArray. */ #define WSLUA_ARG_ByteArray_set_index_INDEX 2 /* The position of the byte to be set */ #define WSLUA_ARG_ByteArray_set_index_VALUE 3 /* The char value to set [0-255] */ -WSLUA_METHOD -ByteArray_set_index(lua_State* L) -{ ByteArray ba = checkByteArray(L,1); int idx = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_INDEX); int v = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_VALUE); @@ -205,11 +190,9 @@ ByteArray_set_index(lua_State* L) } -/* Get the value of a byte in a ByteArray */ +WSLUA_METHOD ByteArray_get_index(lua_State* L) { + /* Get the value of a byte in a ByteArray */ #define WSLUA_ARG_ByteArray_get_index_INDEX 2 /* The position of the byte to get */ -WSLUA_METHOD -ByteArray_get_index(lua_State* L) -{ ByteArray ba = checkByteArray(L,1); int idx = luaL_checkint(L,WSLUA_ARG_ByteArray_get_index_INDEX); @@ -229,10 +212,8 @@ ByteArray_get_index(lua_State* L) WSLUA_RETURN(1); /* The value [0-255] of the byte. */ } -/* Obtain the length of a ByteArray */ -WSLUA_METHOD -ByteArray_len(lua_State* L) -{ +WSLUA_METHOD ByteArray_len(lua_State* L) { + /* Obtain the length of a ByteArray */ ByteArray ba = checkByteArray(L,1); if (!ba) return 0; @@ -242,12 +223,10 @@ ByteArray_len(lua_State* L) WSLUA_RETURN(1); /* The length of the ByteArray. */ } -/* Obtain a segment of a ByteArray */ +WSLUA_METHOD ByteArray_subset(lua_State* L) { + /* Obtain a segment of a ByteArray */ #define WSLUA_ARG_ByteArray_set_index_OFFSET 2 /* The position of the first byte */ #define WSLUA_ARG_ByteArray_set_index_LENGTH 3 /* The length of the segment */ -WSLUA_METHOD -ByteArray_subset(lua_State* L) -{ ByteArray ba = checkByteArray(L,1); int offset = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_OFFSET); int len = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_LENGTH); @@ -268,10 +247,8 @@ ByteArray_subset(lua_State* L) WSLUA_RETURN(1); /* A ByteArray contaning the requested segment. */ } -/* Obtain a string containing the bytes in a ByteArray so that it can be used in display filters (e.g. "01:23:45:67:89:AB") */ -static int -ByteArray_tostring(lua_State* L) -{ +static int ByteArray_tostring(lua_State* L) { + /* Obtain a string containing the bytes in a ByteArray so that it can be used in display filters (e.g. "01:23:45:67:89:AB") */ static const gchar* byte_to_str[] = { "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F", "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F", @@ -331,9 +308,7 @@ static const luaL_reg ByteArray_meta[] = { { NULL, NULL } }; -int -ByteArray_register(lua_State* L) -{ +int ByteArray_register(lua_State* L) { WSLUA_REGISTER_CLASS(ByteArray); return 1; } @@ -362,14 +337,12 @@ ByteArray_register(lua_State* L) * */ -/* A Tvb represents the packet's buffer. It is passed as an argument to listeners and dissectors, - * and can be used to extract information (via TvbRange) from the packet's data. Beware that Tvbs are usable only by - * the current listener or dissector call and are destroyed as soon as the listener/dissector returns, so references - * to them are unusable once the function has returned. - * To create a tvbrange the tvb must be called with offset and length as optional arguments ( the offset defaults to - * 0 and the length to tvb:len() ) - */ WSLUA_CLASS_DEFINE(Tvb,FAIL_ON_NULL("expired tvb"),NOP); +/* A Tvb represents the packet's buffer. It is passed as an argument to listeners and dissectors, +and can be used to extract information (via TvbRange) from the packet's data. Beware that Tvbs are usable only by the current +listener or dissector call and are destroyed as soon as the listener/dissector returns, so references +to them are unusable once the function has returned. +To create a tvbrange the tvb must be called with offset and length as optional arguments ( the offset defaults to 0 and the length to tvb:len() )*/ static GPtrArray* outstanding_Tvb = NULL; static GPtrArray* outstanding_TvbRange = NULL; @@ -380,9 +353,7 @@ static GPtrArray* outstanding_TvbRange = NULL; CLEAR_OUTSTANDING(Tvb,expired, TRUE) -Tvb* -push_Tvb(lua_State* L, tvbuff_t* ws_tvb) -{ +Tvb* push_Tvb(lua_State* L, tvbuff_t* ws_tvb) { Tvb tvb = g_malloc(sizeof(struct _wslua_tvb)); tvb->ws_tvb = ws_tvb; tvb->expired = FALSE; @@ -395,12 +366,10 @@ push_Tvb(lua_State* L, tvbuff_t* ws_tvb) /* * Tvb_new_real(bytearray,name) */ -/* Creates a new Tvb from a bytearray (it gets added to the current frame too) */ +WSLUA_CONSTRUCTOR Tvb_new_real (lua_State *L) { + /* Creates a new Tvb from a bytearray (it gets added to the current frame too) */ #define WSLUA_ARG_Tvb_new_real_BYTEARRAY 1 /* The data source for this Tvb. */ #define WSLUA_ARG_Tvb_new_real_NAME 2 /* The name to be given to the new data-source. */ -WSLUA_CONSTRUCTOR -Tvb_new_real (lua_State *L) -{ ByteArray ba = checkByteArray(L,1); const gchar* name = luaL_optstring(L,WSLUA_ARG_Tvb_new_real_NAME,"Unnamed") ; guint8* data; @@ -425,11 +394,9 @@ Tvb_new_real (lua_State *L) WSLUA_RETURN(1); /* The created Tvb. */ } -/* Creates a (sub)Tvb from using a TvbRange */ +WSLUA_CONSTRUCTOR Tvb_tvb (lua_State *L) { + /* Creates a (sub)Tvb from using a TvbRange */ #define WSLUA_ARG_Tvb_new_subset_RANGE 1 /* The TvbRange from which to create the new Tvb. */ -WSLUA_CONSTRUCTOR -Tvb_tvb (lua_State *L) -{ TvbRange tvbr = checkTvbRange(L,WSLUA_ARG_Tvb_new_subset_RANGE); Tvb tvb; @@ -452,12 +419,8 @@ Tvb_tvb (lua_State *L) } } -/* Convert the bytes of a Tvb into a string, to be used for debugging purposes - * as '...' will be appended in case the string is too long. - */ -WSLUA_METAMETHOD -Tvb__tostring(lua_State* L) -{ +WSLUA_METAMETHOD Tvb__tostring(lua_State* L) { + /* Convert the bytes of a Tvb into a string, to be used for debugging purposes as '...' will be appended in case the string is too long. */ Tvb tvb = checkTvb(L,1); int len; gchar* str; @@ -474,9 +437,7 @@ Tvb__tostring(lua_State* L) WSLUA_RETURN(1); /* The string. */ } -static int -Tvb__gc(lua_State* L) -{ +static int Tvb__gc(lua_State* L) { Tvb tvb = checkTvb(L,1); if (!tvb) return 0; @@ -490,10 +451,8 @@ Tvb__gc(lua_State* L) } -/* Obtain the length of a TVB */ -WSLUA_METHOD -Tvb_len(lua_State* L) -{ +WSLUA_METHOD Tvb_len(lua_State* L) { + /* Obtain the length of a TVB */ Tvb tvb = checkTvb(L,1); if (!tvb) return 0; @@ -506,10 +465,8 @@ Tvb_len(lua_State* L) WSLUA_RETURN(1); /* The length of the Tvb. */ } -/* Returns the raw offset (from the beginning of the source Tvb) of a sub Tvb. */ -WSLUA_METHOD -Tvb_offset(lua_State* L) -{ +WSLUA_METHOD Tvb_offset(lua_State* L) { + /* Returns the raw offset (from the beginning of the source Tvb) of a sub Tvb. */ Tvb tvb = checkTvb(L,1); if (!tvb) return 0; @@ -524,18 +481,14 @@ Tvb_offset(lua_State* L) #if USED_FOR_DOC_PURPOSES -WSLUA_METAMETHOD -Tvb__call(lua_State* L) -{ +WSLUA_METAMETHOD Tvb__call(lua_State* L) { /* Equivalent to tvb:range(...) */ return 0; } #endif -/* Concatenate two objects to a string */ -WSLUA_METAMETHOD -wslua__concat(lua_State* L) -{ +WSLUA_METAMETHOD wslua__concat(lua_State* L) { + /* Concatenate two objects to a string */ if (!luaL_callmeta(L,1,"__tostring")) lua_pushvalue(L,1); if (!luaL_callmeta(L,2,"__tostring")) @@ -546,17 +499,13 @@ wslua__concat(lua_State* L) return 1; } +WSLUA_CLASS_DEFINE(TvbRange,FAIL_ON_NULL("expired tvbrange"),NOP); /* - * A TvbRange represents an usable range of a Tvb and is used to extract data - * from the Tvb that generated it TvbRanges are created by calling a tvb (e.g. - * tvb(offset,length)). If the TvbRange span is outside the Tvb's range the - * creation will cause a runtime error. + A TvbRange represents an usable range of a Tvb and is used to extract data from the Tvb that generated it + TvbRanges are created by calling a tvb (e.g. tvb(offset,length)). If the TvbRange span is outside the Tvb's range the creation will cause a runtime error. */ -WSLUA_CLASS_DEFINE(TvbRange,FAIL_ON_NULL("expired tvbrange"),NOP); -static TvbRange -new_TvbRange(lua_State* L, tvbuff_t* ws_tvb, int offset, int len) -{ +static TvbRange new_TvbRange(lua_State* L, tvbuff_t* ws_tvb, int offset, int len) { TvbRange tvbr; @@ -587,12 +536,10 @@ new_TvbRange(lua_State* L, tvbuff_t* ws_tvb, int offset, int len) } -/* Creates a tvbr from this Tvb. This is used also as the Tvb:__call() metamethod. */ +WSLUA_METHOD Tvb_range(lua_State* L) { + /* Creates a tvbr from this Tvb. This is used also as the Tvb:__call() metamethod. */ #define WSLUA_OPTARG_Tvb_range_OFFSET 2 /* The offset (in octets) from the begining of the Tvb. Defaults to 0. */ #define WSLUA_OPTARG_Tvb_range_LENGTH 3 /* The length (in octets) of the range. Defaults to until the end of the Tvb. */ -WSLUA_METHOD -Tvb_range(lua_State* L) -{ Tvb tvb = checkTvb(L,1); int offset = luaL_optint(L,WSLUA_OPTARG_Tvb_range_OFFSET,0); @@ -627,18 +574,17 @@ static const luaL_reg Tvb_meta[] = { { NULL, NULL } }; -int -Tvb_register(lua_State* L) -{ +int Tvb_register(lua_State* L) { WSLUA_REGISTER_CLASS(Tvb); return 1; } -/* Get a Big Endian (network order) unsigned integer from a TvbRange. The range must be 1, 2, 3 or 4 octets long. */ -WSLUA_METHOD -TvbRange_uint(lua_State* L) -{ +/* + * get a Blefuscuoan unsigned integer from a tvb + */ +WSLUA_METHOD TvbRange_uint(lua_State* L) { + /* Get a Big Endian (network order) unsigned integer from a TvbRange. The range must be 1, 2, 3 or 4 octets long. */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { @@ -672,10 +618,11 @@ TvbRange_uint(lua_State* L) } } -/* Get a Little Endian unsigned integer from a TvbRange. The range must be 1, 2, 3 or 4 octets long. */ -WSLUA_METHOD -TvbRange_le_uint(lua_State* L) -{ +/* + * get a Lilliputian unsigned integer from a tvb + */ +WSLUA_METHOD TvbRange_le_uint(lua_State* L) { + /* Get a Little Endian unsigned integer from a TvbRange. The range must be 1, 2, 3 or 4 octets long. */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { @@ -703,10 +650,11 @@ TvbRange_le_uint(lua_State* L) } } -/* Get a Big Endian (network order) unsigned 64 bit integer from a TvbRange. The range must be 1-8 octets long. */ -WSLUA_METHOD -TvbRange_uint64(lua_State* L) -{ +/* + * get a Blefuscuoan unsigned 64 bit integer from a tvb + */ +WSLUA_METHOD TvbRange_uint64(lua_State* L) { + /* Get a Big Endian (network order) unsigned 64 bit integer from a TvbRange. The range must be 1-8 octets long. */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { @@ -734,10 +682,11 @@ TvbRange_uint64(lua_State* L) } } -/* Get a Little Endian unsigned 64 bit integer from a TvbRange. The range must be 1-8 octets long. */ -WSLUA_METHOD -TvbRange_le_uint64(lua_State* L) -{ +/* + * get a Lilliputian unsigned 64 bit integer from a tvb + */ +WSLUA_METHOD TvbRange_le_uint64(lua_State* L) { + /* Get a Little Endian unsigned 64 bit integer from a TvbRange. The range must be 1-8 octets long. */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { @@ -765,10 +714,11 @@ TvbRange_le_uint64(lua_State* L) } } -/* Get a Big Endian (network order) signed integer from a TvbRange. The range must be 1, 2 or 4 octets long. */ -WSLUA_METHOD -TvbRange_int(lua_State* L) -{ +/* + * get a Blefuscuoan signed integer from a tvb + */ +WSLUA_METHOD TvbRange_int(lua_State* L) { + /* Get a Big Endian (network order) signed integer from a TvbRange. The range must be 1, 2 or 4 octets long. */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { @@ -799,10 +749,11 @@ TvbRange_int(lua_State* L) } } -/* Get a Little Endian signed integer from a TvbRange. The range must be 1, 2 or 4 octets long. */ -WSLUA_METHOD -TvbRange_le_int(lua_State* L) -{ +/* + * get a Lilliputian signed integer from a tvb + */ +WSLUA_METHOD TvbRange_le_int(lua_State* L) { + /* Get a Little Endian signed integer from a TvbRange. The range must be 1, 2 or 4 octets long. */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { @@ -826,10 +777,11 @@ TvbRange_le_int(lua_State* L) } } -/* Get a Big Endian (network order) signed 64 bit integer from a TvbRange. The range must be 1-8 octets long. */ -WSLUA_METHOD -TvbRange_int64(lua_State* L) -{ +/* + * get a Blefuscuoan signed 64 bit integer from a tvb + */ +WSLUA_METHOD TvbRange_int64(lua_State* L) { + /* Get a Big Endian (network order) signed 64 bit integer from a TvbRange. The range must be 1-8 octets long. */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { @@ -857,10 +809,11 @@ TvbRange_int64(lua_State* L) } } -/* Get a Little Endian signed 64 bit integer from a TvbRange. The range must be 1-8 octets long. */ -WSLUA_METHOD -TvbRange_le_int64(lua_State* L) -{ +/* + * get a Lilliputian signed 64 bit integer from a tvb + */ +WSLUA_METHOD TvbRange_le_int64(lua_State* L) { + /* Get a Little Endian signed 64 bit integer from a TvbRange. The range must be 1-8 octets long. */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { @@ -888,10 +841,11 @@ TvbRange_le_int64(lua_State* L) } } -/* Get a Big Endian (network order) floating point number from a TvbRange. The range must be 4 or 8 octets long. */ -WSLUA_METHOD -TvbRange_float(lua_State* L) -{ +/* + * get a Blefuscuoan float + */ +WSLUA_METHOD TvbRange_float(lua_State* L) { + /* Get a Big Endian (network order) floating point number from a TvbRange. The range must be 4 or 8 octets long. */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; if (tvbr->tvb->expired) { @@ -912,10 +866,11 @@ TvbRange_float(lua_State* L) } } -/* Get a Little Endian floating point number from a TvbRange. The range must be 4 or 8 octets long. */ -WSLUA_METHOD -TvbRange_le_float(lua_State* L) -{ +/* + * get a Lilliputian float + */ +WSLUA_METHOD TvbRange_le_float(lua_State* L) { + /* Get a Little Endian floating point number from a TvbRange. The range must be 4 or 8 octets long. */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; @@ -932,10 +887,8 @@ TvbRange_le_float(lua_State* L) } } -/* Get an IPv4 Address from a TvbRange. */ -WSLUA_METHOD -TvbRange_ipv4(lua_State* L) -{ +WSLUA_METHOD TvbRange_ipv4(lua_State* L) { + /* Get an IPv4 Address from a TvbRange. */ TvbRange tvbr = checkTvbRange(L,1); Address addr; guint32* ip_addr; @@ -960,10 +913,8 @@ TvbRange_ipv4(lua_State* L) WSLUA_RETURN(1); /* The IPv4 Address */ } -/* Get an Little Endian IPv4 Address from a TvbRange. */ -WSLUA_METHOD -TvbRange_le_ipv4(lua_State* L) -{ +WSLUA_METHOD TvbRange_le_ipv4(lua_State* L) { + /* Get an Little Endian IPv4 Address from a TvbRange. */ TvbRange tvbr = checkTvbRange(L,1); Address addr; guint32* ip_addr; @@ -989,10 +940,8 @@ TvbRange_le_ipv4(lua_State* L) WSLUA_RETURN(1); /* The IPv4 Address */ } -/* Get an Ethernet Address from a TvbRange. */ -WSLUA_METHOD -TvbRange_ether(lua_State* L) -{ +WSLUA_METHOD TvbRange_ether(lua_State* L) { + /* Get an Ethernet Address from a TvbRange. */ TvbRange tvbr = checkTvbRange(L,1); Address addr; guint8* buff; @@ -1017,10 +966,8 @@ TvbRange_ether(lua_State* L) } -/* Obtain a string from a TvbRange */ -WSLUA_METHOD -TvbRange_string(lua_State* L) -{ +WSLUA_METHOD TvbRange_string(lua_State* L) { + /* Obtain a string from a TvbRange */ TvbRange tvbr = checkTvbRange(L,1); if ( !(tvbr && tvbr->tvb)) return 0; @@ -1034,10 +981,8 @@ TvbRange_string(lua_State* L) WSLUA_RETURN(1); /* The string */ } -/* Obtain a zero terminated string from a TvbRange */ -WSLUA_METHOD -TvbRange_stringz(lua_State* L) -{ +WSLUA_METHOD TvbRange_stringz(lua_State* L) { + /* Obtain a zero terminated string from a TvbRange */ TvbRange tvbr = checkTvbRange(L,1); if ( !(tvbr && tvbr->tvb)) return 0; @@ -1051,10 +996,8 @@ TvbRange_stringz(lua_State* L) WSLUA_RETURN(1); /* The zero terminated string */ } -/* Obtain a ByteArray */ -WSLUA_METHOD -TvbRange_bytes(lua_State* L) -{ +WSLUA_METHOD TvbRange_bytes(lua_State* L) { + /* Obtain a ByteArray */ TvbRange tvbr = checkTvbRange(L,1); GByteArray* ba; @@ -1072,12 +1015,10 @@ TvbRange_bytes(lua_State* L) WSLUA_RETURN(1); /* The ByteArray */ } -/* Get a bitfield from a TvbRange. */ +WSLUA_METHOD TvbRange_bitfield(lua_State* L) { + /* Get a bitfield from a TvbRange. */ #define WSLUA_OPTARG_TvbRange_bitfield_POSITION 2 /* The bit offset from the begining of the TvbRange. Defaults to 0. */ #define WSLUA_OPTARG_TvbRange_bitfield_LENGTH 3 /* The length (in bits) of the field. Defaults to 1. */ -WSLUA_METHOD -TvbRange_bitfield(lua_State* L) -{ TvbRange tvbr = checkTvbRange(L,1); int pos = luaL_optint(L,WSLUA_OPTARG_TvbRange_bitfield_POSITION,0); @@ -1114,12 +1055,10 @@ TvbRange_bitfield(lua_State* L) } } -/* Creates a sub-TvbRange from this TvbRange. This is used also as the TvbRange:__call() metamethod. */ +WSLUA_METHOD TvbRange_range(lua_State* L) { + /* Creates a sub-TvbRange from this TvbRange. This is used also as the TvbRange:__call() metamethod. */ #define WSLUA_OPTARG_TvbRange_range_OFFSET 2 /* The offset (in octets) from the begining of the TvbRange. Defaults to 0. */ #define WSLUA_OPTARG_TvbRange_range_LENGTH 3 /* The length (in octets) of the range. Defaults to until the end of the TvbRange. */ -WSLUA_METHOD -TvbRange_range(lua_State* L) -{ TvbRange tvbr = checkTvbRange(L,1); int offset = luaL_optint(L,WSLUA_OPTARG_TvbRange_range_OFFSET,0); @@ -1144,10 +1083,8 @@ TvbRange_range(lua_State* L) return 0; } -/* Obtain the length of a TvbRange */ -WSLUA_METHOD -TvbRange_len(lua_State* L) -{ +WSLUA_METHOD TvbRange_len(lua_State* L) { + /* Obtain the length of a TvbRange */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; @@ -1155,15 +1092,12 @@ TvbRange_len(lua_State* L) luaL_error(L,"expired tvb"); return 0; } - - lua_pushnumber(L,(lua_Number)tvbr->len); - return 1; + lua_pushnumber(L,(lua_Number)tvbr->len); + return 1; } -/* Obtain the offset in a TvbRange */ -WSLUA_METHOD -TvbRange_offset(lua_State* L) -{ +WSLUA_METHOD TvbRange_offset(lua_State* L) { + /* Obtain the offset in a TvbRange */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; @@ -1171,19 +1105,15 @@ TvbRange_offset(lua_State* L) luaL_error(L,"expired tvb"); return 0; } - - lua_pushnumber(L,(lua_Number)tvbr->offset); - return 1; + lua_pushnumber(L,(lua_Number)tvbr->offset); + return 1; } -/* Converts the TvbRange into a string. As the string gets truncated - * you should use this only for debugging purposes - * or if what you want is to have a truncated string in the format 67:89:AB:... - */ -WSLUA_METAMETHOD -TvbRange__tostring(lua_State* L) -{ +WSLUA_METAMETHOD TvbRange__tostring(lua_State* L) { + /* Converts the TvbRange into a string. As the string gets truncated + you should use this only for debugging purposes + or if what you want is to have a truncated string in the format 67:89:AB:... */ TvbRange tvbr = checkTvbRange(L,1); if (!(tvbr && tvbr->tvb)) return 0; @@ -1228,36 +1158,30 @@ static const luaL_reg TvbRange_meta[] = { { NULL, NULL } }; -int -TvbRange_register(lua_State* L) -{ +int TvbRange_register(lua_State* L) { outstanding_Tvb = g_ptr_array_new(); outstanding_TvbRange = g_ptr_array_new(); WSLUA_REGISTER_CLASS(TvbRange); return 1; } +WSLUA_CLASS_DEFINE(Int64,FAIL_ON_NULL("null int64"),NOP); /* - * Int64 represents a 64 bit integer. - * Lua uses one single number representation which can be chosen at compile - * time and since it is often set to IEEE 754 double precision floating point, - * we cannot store a 64 bit integer with full precision. - * For details, see: http://lua-users.org/wiki/FloatingPoint + Int64 represents a 64 bit integer. + Lua uses one single number representation which can be chosen at compile time and since + it is often set to IEEE 754 double precision floating point, we cannot store a 64 bit integer + with full precision. + For details, see: http://lua-users.org/wiki/FloatingPoint */ -WSLUA_CLASS_DEFINE(Int64,FAIL_ON_NULL("null int64"),NOP); -/* Converts the Int64 into a string */ -WSLUA_METAMETHOD -Int64__tostring(lua_State* L) -{ +WSLUA_METAMETHOD Int64__tostring(lua_State* L) { + /* Converts the Int64 into a string */ Int64 num = checkInt64(L,1); lua_pushstring(L,ep_strdup_printf("%" G_GINT64_MODIFIER "d",(gint64)*(num))); return 1; } -static int -Int64__gc(lua_State* L) -{ +static int Int64__gc(lua_State* L) { Int64 num = checkInt64(L,1); if (!num) return 0; @@ -1278,28 +1202,22 @@ static const luaL_reg Int64_meta[] = { { NULL, NULL } }; -int -Int64_register(lua_State* L) -{ +int Int64_register(lua_State* L) { WSLUA_REGISTER_CLASS(Int64); return 1; } -/* UInt64 represents a 64 bit unsigned integer. */ WSLUA_CLASS_DEFINE(UInt64,FAIL_ON_NULL("null uint64"),NOP); + /* UInt64 represents a 64 bit unsigned integer. */ -/* Converts the UInt64 into a string */ -WSLUA_METAMETHOD -UInt64__tostring(lua_State* L) -{ +WSLUA_METAMETHOD UInt64__tostring(lua_State* L) { + /* Converts the UInt64 into a string */ UInt64 num = checkUInt64(L,1); lua_pushstring(L,ep_strdup_printf("%" G_GINT64_MODIFIER "u",(guint64)*(num))); return 1; } -static int -UInt64__gc(lua_State* L) -{ +static int UInt64__gc(lua_State* L) { UInt64 num = checkUInt64(L,1); if (!num) return 0; @@ -1320,9 +1238,7 @@ static const luaL_reg UInt64_meta[] = { { NULL, NULL } }; -int -UInt64_register(lua_State* L) -{ +int UInt64_register(lua_State* L) { WSLUA_REGISTER_CLASS(UInt64); return 1; } diff --git a/epan/wslua/wslua_util.c b/epan/wslua/wslua_util.c index 309778f853..04d9a699bb 100644 --- a/epan/wslua/wslua_util.c +++ b/epan/wslua/wslua_util.c @@ -30,88 +30,74 @@ #include <math.h> #include <epan/stat_cmd_args.h> -WSLUA_API gboolean -wslua_optbool(lua_State* L, int n, gboolean def) -{ - gboolean val = FALSE; - - if ( lua_isboolean(L,n) ) { - val = lua_toboolean(L,n); - } else if ( lua_isnil(L,n) || lua_gettop(L) < n ){ - val = def; - } else { - luaL_argerror(L,n,"must be a boolean"); - } +WSLUA_API gboolean wslua_optbool(lua_State* L, int n, gboolean def) { + gboolean val = FALSE; + + if ( lua_isboolean(L,n) ) { + val = lua_toboolean(L,n); + } else if ( lua_isnil(L,n) || lua_gettop(L) < n ){ + val = def; + } else { + luaL_argerror(L,n,"must be a boolean"); + } - return val; + return val; } -WSLUA_API const gchar* -lua_shiftstring(lua_State* L, int i) -{ +WSLUA_API const gchar* lua_shiftstring(lua_State* L, int i) { const gchar* p = luaL_checkstring(L, i); if (p) { - lua_remove(L,i); - return p; + lua_remove(L,i); + return p; } else { - return NULL; + return NULL; } } -/* Get Wireshark version */ -WSLUA_FUNCTION wslua_get_version(lua_State* L) -{ +WSLUA_FUNCTION wslua_get_version(lua_State* L) { /* Get Wireshark version */ gchar* str = VERSION; lua_pushstring(L,str); WSLUA_RETURN(1); /* version string */ } -/* Formats an absolute timestamp into a human readable date */ +WSLUA_FUNCTION wslua_format_date(lua_State* LS) { /* Formats an absolute timestamp into a human readable date */ #define WSLUA_ARG_format_date_TIMESTAMP 1 /* A timestamp value to convert. */ -WSLUA_FUNCTION wslua_format_date(lua_State* LS) -{ - lua_Number timestamp = luaL_checknumber(LS,WSLUA_ARG_format_date_TIMESTAMP); - nstime_t then; - gchar* str; - - then.secs = (guint32)floor(timestamp); - then.nsecs = (guint32) ( (timestamp-(double)(then.secs))*1000000000); - str = abs_time_to_str(&then, ABSOLUTE_TIME_LOCAL, TRUE); - lua_pushstring(LS,str); - - WSLUA_RETURN(1); /* A string with the formated date */ + lua_Number timestamp = luaL_checknumber(LS,WSLUA_ARG_format_date_TIMESTAMP); + nstime_t then; + gchar* str; + + then.secs = (guint32)floor(timestamp); + then.nsecs = (guint32) ( (timestamp-(double)(then.secs))*1000000000); + str = abs_time_to_str(&then, ABSOLUTE_TIME_LOCAL, TRUE); + lua_pushstring(LS,str); + + WSLUA_RETURN(1); /* A string with the formated date */ } -/* Formats a relative timestamp in a human readable form */ +WSLUA_FUNCTION wslua_format_time(lua_State* LS) { /* Formats a relative timestamp in a human readable form */ #define WSLUA_ARG_format_time_TIMESTAMP 1 /* A timestamp value to convert */ -WSLUA_FUNCTION wslua_format_time(lua_State* LS) -{ - lua_Number timestamp = luaL_checknumber(LS,WSLUA_ARG_format_time_TIMESTAMP); - nstime_t then; - gchar* str; - - then.secs = (guint32)floor(timestamp); - then.nsecs = (guint32) ( (timestamp-(double)(then.secs))*1000000000); - str = rel_time_to_str(&then); - lua_pushstring(LS,str); - - WSLUA_RETURN(1); /* A string with the formated time */ + lua_Number timestamp = luaL_checknumber(LS,WSLUA_ARG_format_time_TIMESTAMP); + nstime_t then; + gchar* str; + + then.secs = (guint32)floor(timestamp); + then.nsecs = (guint32) ( (timestamp-(double)(then.secs))*1000000000); + str = rel_time_to_str(&then); + lua_pushstring(LS,str); + + WSLUA_RETURN(1); /* A string with the formated time */ } -/* Reports a failure to the user */ +WSLUA_FUNCTION wslua_report_failure(lua_State* LS) { /* Reports a failure to the user */ #define WSLUA_ARG_report_failure_TEXT 1 /* Message */ -WSLUA_FUNCTION wslua_report_failure(lua_State* LS) -{ - const gchar* s = luaL_checkstring(LS,WSLUA_ARG_report_failure_TEXT); - report_failure("%s",s); - return 0; + const gchar* s = luaL_checkstring(LS,WSLUA_ARG_report_failure_TEXT); + report_failure("%s",s); + return 0; } -static int -wslua_log(lua_State* L, GLogLevelFlags log_level) -{ +static int wslua_log(lua_State* L, GLogLevelFlags log_level) { GString* str = g_string_new(""); int n = lua_gettop(L); /* Number of arguments */ int i; @@ -138,273 +124,248 @@ wslua_log(lua_State* L, GLogLevelFlags log_level) return 0; } -/* Will add a log entry with critical severity */ -WSLUA_FUNCTION wslua_critical( lua_State* L ) -{ - /* WSLUA_MOREARGS critical objects to be printed */ - wslua_log(L,G_LOG_LEVEL_CRITICAL); - return 0; +WSLUA_FUNCTION wslua_critical( lua_State* L ) { /* Will add a log entry with critical severity*/ +/* WSLUA_MOREARGS critical objects to be printed */ + wslua_log(L,G_LOG_LEVEL_CRITICAL); + return 0; } -/* Will add a log entry with warn severity */ -WSLUA_FUNCTION wslua_warn( lua_State* L ) -{ - /* WSLUA_MOREARGS warn objects to be printed */ - wslua_log(L,G_LOG_LEVEL_WARNING); - return 0; +WSLUA_FUNCTION wslua_warn( lua_State* L ) { /* Will add a log entry with warn severity */ +/* WSLUA_MOREARGS warn objects to be printed */ + wslua_log(L,G_LOG_LEVEL_WARNING); + return 0; } -/* Will add a log entry with message severity */ -WSLUA_FUNCTION wslua_message( lua_State* L ) -{ - /* WSLUA_MOREARGS message objects to be printed */ - wslua_log(L,G_LOG_LEVEL_MESSAGE); - return 0; +WSLUA_FUNCTION wslua_message( lua_State* L ) { /* Will add a log entry with message severity */ +/* WSLUA_MOREARGS message objects to be printed */ + wslua_log(L,G_LOG_LEVEL_MESSAGE); + return 0; } -/* Will add a log entry with info severity */ -WSLUA_FUNCTION wslua_info( lua_State* L ) -{ - /* WSLUA_MOREARGS info objects to be printed */ - wslua_log(L,G_LOG_LEVEL_INFO); - return 0; +WSLUA_FUNCTION wslua_info( lua_State* L ) { /* Will add a log entry with info severity */ +/* WSLUA_MOREARGS info objects to be printed */ + wslua_log(L,G_LOG_LEVEL_INFO); + return 0; } -/* Will add a log entry with debug severity */ -WSLUA_FUNCTION wslua_debug( lua_State* L ) -{ - /* WSLUA_MOREARGS debug objects to be printed */ - wslua_log(L,G_LOG_LEVEL_DEBUG); - return 0; +WSLUA_FUNCTION wslua_debug( lua_State* L ) { /* Will add a log entry with debug severity */ +/* WSLUA_MOREARGS debug objects to be printed */ + wslua_log(L,G_LOG_LEVEL_DEBUG); + return 0; } /* The returned filename is g_malloc()'d so the caller must free it */ /* except when NULL is returned if file doesn't exist */ -static char* -wslua_get_actual_filename(const char* fname) -{ - char fname_clean[256]; - char* f; - char* filename; - - strncpy(fname_clean,fname,255); - fname_clean[255] = '\0'; - - for(f = fname_clean; *f; f++) { - switch(*f) { - case '/': case '\\': - *f = *(G_DIR_SEPARATOR_S); - break; - default: - break; +static char* wslua_get_actual_filename(const char* fname) { + char fname_clean[256]; + char* f; + char* filename; + + strncpy(fname_clean,fname,255); + fname_clean[255] = '\0'; + + for(f = fname_clean; *f; f++) { + switch(*f) { + case '/': case '\\': + *f = *(G_DIR_SEPARATOR_S); + break; + default: + break; + } } - } - if ( file_exists(fname_clean) ) { - return g_strdup(fname_clean); - } + if ( file_exists(fname_clean) ) { + return g_strdup(fname_clean); + } - filename = get_persconffile_path(fname_clean,FALSE,FALSE); + filename = get_persconffile_path(fname_clean,FALSE,FALSE); - if ( file_exists(filename) ) { - return filename; - } - g_free(filename); + if ( file_exists(filename) ) { + return filename; + } + g_free(filename); - filename = get_datafile_path(fname_clean); - if ( file_exists(filename) ) { - return filename; - } - g_free(filename); + filename = get_datafile_path(fname_clean); + if ( file_exists(filename) ) { + return filename; + } + g_free(filename); - return NULL; + return NULL; } -/* Lua's loadfile() has been modified so that if a file does not exist - * in the current directory it will look for it in wireshark's user and system directories - */ +WSLUA_FUNCTION wslua_loadfile(lua_State* L) { + /* Lua's loadfile() has been modified so that if a file does not exist + in the current directory it will look for it in wireshark's user and system directories */ #define WSLUA_ARG_loadfile_FILENAME 1 /* Name of the file to be loaded */ -WSLUA_FUNCTION wslua_loadfile(lua_State* L) -{ - const char *given_fname = luaL_checkstring(L, WSLUA_ARG_loadfile_FILENAME); - char* filename; + const char *given_fname = luaL_checkstring(L, WSLUA_ARG_loadfile_FILENAME); + char* filename; - filename = wslua_get_actual_filename(given_fname); + filename = wslua_get_actual_filename(given_fname); - if (!filename) WSLUA_ARG_ERROR(loadfile,FILENAME,"file does not exist"); + if (!filename) WSLUA_ARG_ERROR(loadfile,FILENAME,"file does not exist"); - if (luaL_loadfile(L, filename) == 0) { - g_free(filename); - return 1; - } else { - g_free(filename); - lua_pushnil(L); - lua_insert(L, -2); - return 2; - } + if (luaL_loadfile(L, filename) == 0) { + g_free(filename); + return 1; + } else { + g_free(filename); + lua_pushnil(L); + lua_insert(L, -2); + return 2; + } } -/* Lua's dofile() has been modified so that if a file does not exist - * in the current directory it will look for it in wireshark's user and system directories - */ +WSLUA_FUNCTION wslua_dofile(lua_State* L) { + /* Lua's dofile() has been modified so that if a file does not exist + in the current directory it will look for it in wireshark's user and system directories */ #define WSLUA_ARG_dofile_FILENAME 1 /* Name of the file to be run */ -WSLUA_FUNCTION wslua_dofile(lua_State* L) -{ - const char *given_fname = luaL_checkstring(L, WSLUA_ARG_dofile_FILENAME); - char* filename; - int n; + const char *given_fname = luaL_checkstring(L, WSLUA_ARG_dofile_FILENAME); + char* filename; + int n; - if (!given_fname) WSLUA_ARG_ERROR(dofile,FILENAME,"must be a string"); + if (!given_fname) WSLUA_ARG_ERROR(dofile,FILENAME,"must be a string"); - filename = wslua_get_actual_filename(given_fname); + filename = wslua_get_actual_filename(given_fname); - if (!filename) WSLUA_ARG_ERROR(dofile,FILENAME,"file does not exist"); + if (!filename) WSLUA_ARG_ERROR(dofile,FILENAME,"file does not exist"); - n = lua_gettop(L); - if (luaL_loadfile(L, filename) != 0) lua_error(L); - g_free(filename); - lua_call(L, 0, LUA_MULTRET); - return lua_gettop(L) - n; + n = lua_gettop(L); + if (luaL_loadfile(L, filename) != 0) lua_error(L); + g_free(filename); + lua_call(L, 0, LUA_MULTRET); + return lua_gettop(L) - n; } +WSLUA_FUNCTION wslua_persconffile_path(lua_State* L) { #define WSLUA_OPTARG_persconffile_path_FILENAME 1 /* A filename */ -WSLUA_FUNCTION wslua_persconffile_path(lua_State* L) -{ - const char *fname = luaL_optstring(L, WSLUA_OPTARG_persconffile_path_FILENAME,""); - const char* filename = get_persconffile_path(fname,FALSE,FALSE); + const char *fname = luaL_optstring(L, WSLUA_OPTARG_persconffile_path_FILENAME,""); + const char* filename = get_persconffile_path(fname,FALSE,FALSE); - lua_pushstring(L,filename); - WSLUA_RETURN(1); /* The full pathname for a file in the personal configuration directory */ + lua_pushstring(L,filename); + WSLUA_RETURN(1); /* The full pathname for a file in the personal configuration directory */ } +WSLUA_FUNCTION wslua_datafile_path(lua_State* L) { #define WSLUA_OPTARG_datafile_path_FILENAME 1 /* A filename */ -WSLUA_FUNCTION wslua_datafile_path(lua_State* L) -{ - const char *fname = luaL_optstring(L, WSLUA_OPTARG_datafile_path_FILENAME,""); - const char* filename = get_datafile_path(fname); + const char *fname = luaL_optstring(L, WSLUA_OPTARG_datafile_path_FILENAME,""); + const char* filename = get_datafile_path(fname); - lua_pushstring(L,filename); - WSLUA_RETURN(1); /* The full pathname for a file in wireshark's configuration directory */ + lua_pushstring(L,filename); + WSLUA_RETURN(1); /* The full pathname for a file in wireshark's configuration directory */ } WSLUA_CLASS_DEFINE(Dir,NOP,NOP); /* A Directory */ -/* Usage: for filename in Dir.open(path) do ... end */ +WSLUA_CONSTRUCTOR Dir_open(lua_State* L) { + /* Usage: for filename in Dir.open(path) do ... end */ #define WSLUA_ARG_Dir_open_PATHNAME 1 /* The pathname of the directory */ #define WSLUA_OPTARG_Dir_open_EXTENSION 2 /* If given, only file with this extension will be returned */ -WSLUA_CONSTRUCTOR -Dir_open(lua_State* L) -{ - const char* dirname = luaL_checkstring(L,WSLUA_ARG_Dir_open_PATHNAME); - const char* extension = luaL_optstring(L,WSLUA_OPTARG_Dir_open_EXTENSION,NULL); - Dir dir; - char* dirname_clean; - if (!dirname) WSLUA_ARG_ERROR(Dir_open,PATHNAME,"must be a string"); + const char* dirname = luaL_checkstring(L,WSLUA_ARG_Dir_open_PATHNAME); + const char* extension = luaL_optstring(L,WSLUA_OPTARG_Dir_open_EXTENSION,NULL); + Dir dir; + char* dirname_clean; - dirname_clean = wslua_get_actual_filename(dirname); - if (!dirname_clean) WSLUA_ARG_ERROR(Dir_open,PATHNAME,"directory does not exist"); + if (!dirname) WSLUA_ARG_ERROR(Dir_open,PATHNAME,"must be a string"); - if (!test_for_directory(dirname_clean)) { - g_free(dirname_clean); - WSLUA_ARG_ERROR(Dir_open,PATHNAME, "must be a directory"); - } + dirname_clean = wslua_get_actual_filename(dirname); + if (!dirname_clean) WSLUA_ARG_ERROR(Dir_open,PATHNAME,"directory does not exist"); - dir = g_malloc(sizeof(struct _wslua_dir)); - dir->dir = OPENDIR_OP(dirname_clean); - g_free(dirname_clean); - dir->ext = extension ? g_strdup(extension) : NULL; - dir->dummy = g_malloc(sizeof(GError *)); - *(dir->dummy) = NULL; + if (!test_for_directory(dirname_clean)) { + g_free(dirname_clean); + WSLUA_ARG_ERROR(Dir_open,PATHNAME, "must be a directory"); + } - if (dir->dir == NULL) { - g_free(dir->dummy); - g_free(dir); + dir = g_malloc(sizeof(struct _wslua_dir)); + dir->dir = OPENDIR_OP(dirname_clean); + g_free(dirname_clean); + dir->ext = extension ? g_strdup(extension) : NULL; + dir->dummy = g_malloc(sizeof(GError *)); + *(dir->dummy) = NULL; - WSLUA_ARG_ERROR(Dir_open,PATHNAME,"could not open directory"); - return 0; - } + if (dir->dir == NULL) { + g_free(dir->dummy); + g_free(dir); - pushDir(L,dir); - WSLUA_RETURN(1); /* the Dir object */ + WSLUA_ARG_ERROR(Dir_open,PATHNAME,"could not open directory"); + return 0; + } + + pushDir(L,dir); + WSLUA_RETURN(1); /* the Dir object */ } -/* At every invocation will return one file (nil when done) */ -WSLUA_METAMETHOD -Dir__call(lua_State* L) -{ - Dir dir = checkDir(L,1); - const FILE_T* file; - const gchar* filename; - const char* ext; - - if (!dir) { - luaL_argerror(L,1,"must be a Dir"); - return 0; - } +WSLUA_METAMETHOD Dir__call(lua_State* L) { + /* At every invocation will return one file (nil when done) */ - if (!dir->dir) { - return 0; - } + Dir dir = checkDir(L,1); + const FILE_T* file; + const gchar* filename; + const char* ext; - if ( ! ( file = DIRGETNEXT_OP(dir->dir ) )) { - CLOSEDIR_OP(dir->dir); - dir->dir = NULL; - return 0; - } + if (!dir) { + luaL_argerror(L,1,"must be a Dir"); + return 0; + } + if (!dir->dir) { + return 0; + } - if ( ! dir->ext ) { - filename = GETFNAME_OP(file); - lua_pushstring(L,filename); - return 1; - } + if ( ! ( file = DIRGETNEXT_OP(dir->dir ) )) { + CLOSEDIR_OP(dir->dir); + dir->dir = NULL; + return 0; + } - do { - filename = GETFNAME_OP(file); - /* XXX strstr returns ptr to first match, - * this fails ext=".xxx" filename="aaa.xxxz.xxx" - */ - if ( ( ext = strstr(filename,dir->ext)) && g_str_equal(ext,dir->ext) ) { - lua_pushstring(L,filename); - return 1; + if ( ! dir->ext ) { + filename = GETFNAME_OP(file); + lua_pushstring(L,filename); + return 1; } - } while(( file = DIRGETNEXT_OP(dir->dir) )); - CLOSEDIR_OP(dir->dir); - dir->dir = NULL; - return 0; -} + do { + filename = GETFNAME_OP(file); -/* Closes the directory */ -WSLUA_METHOD -Dir_close(lua_State* L) -{ - Dir dir = checkDir(L,1); + /* XXX strstr returns ptr to first match, + this fails ext=".xxx" filename="aaa.xxxz.xxx" */ + if ( ( ext = strstr(filename,dir->ext)) && g_str_equal(ext,dir->ext) ) { + lua_pushstring(L,filename); + return 1; + } + } while(( file = DIRGETNEXT_OP(dir->dir) )); - if (dir->dir) { CLOSEDIR_OP(dir->dir); dir->dir = NULL; - } + return 0; +} - return 0; +WSLUA_METHOD Dir_close(lua_State* L) { + /* Closes the directory */ + Dir dir = checkDir(L,1); + + if (dir->dir) { + CLOSEDIR_OP(dir->dir); + dir->dir = NULL; + } + + return 0; } -static int -wslua_Dir__gc(lua_State* L) -{ - Dir dir = checkDir(L,1); +static int wslua_Dir__gc(lua_State* L) { + Dir dir = checkDir(L,1); - if (dir->dir) { - CLOSEDIR_OP(dir->dir); - } + if (dir->dir) { + CLOSEDIR_OP(dir->dir); + } - g_free(dir->dummy); + g_free(dir->dummy); - if (dir->ext) g_free(dir->ext); + if (dir->ext) g_free(dir->ext); - g_free(dir); + g_free(dir); - return 0; + return 0; } static const luaL_reg Dir_methods[] = { @@ -419,9 +380,7 @@ static const luaL_reg Dir_meta[] = { { NULL, NULL } }; -int -Dir_register(lua_State* L) -{ +int Dir_register(lua_State* L) { WSLUA_REGISTER_CLASS(Dir); @@ -434,24 +393,20 @@ typedef struct _statcmd_t { int func_ref; } statcmd_t; -static int -statcmd_init_cb_error_handler(lua_State* L) -{ - (void)L; - return 0; +static int statcmd_init_cb_error_handler(lua_State* L) { + (void)L; + return 0; } -static void -statcmd_init(const char *optarg, void* userdata) -{ - statcmd_t* sc = userdata; +static void statcmd_init(const char *optarg, void* userdata) { + statcmd_t* sc = userdata; lua_State* L = sc->L; lua_settop(L,0); lua_pushcfunction(L,statcmd_init_cb_error_handler); lua_rawgeti(L, LUA_REGISTRYINDEX, sc->func_ref); - lua_pushstring(L,optarg); + lua_pushstring(L,optarg); switch ( lua_pcall(L,1,0,1) ) { case 0: @@ -469,19 +424,18 @@ statcmd_init(const char *optarg, void* userdata) } -/* Register a function to handle a -z option */ +WSLUA_FUNCTION wslua_register_stat_cmd_arg(lua_State* L) { + /* Register a function to handle a -z option */ #define WSLUA_ARG_register_stat_cmd_arg_ARGUMENT 1 /* Argument */ #define WSLUA_OPTARG_register_stat_cmd_arg_ACTION 2 /* Action */ -WSLUA_FUNCTION wslua_register_stat_cmd_arg(lua_State* L) -{ - const char* arg = luaL_checkstring(L,WSLUA_ARG_register_stat_cmd_arg_ARGUMENT); - statcmd_t* sc = g_malloc0(sizeof(statcmd_t)); /* XXX leaked */ - - sc->L = L; - lua_pushvalue(L, WSLUA_OPTARG_register_stat_cmd_arg_ACTION); - sc->func_ref = luaL_ref(L, LUA_REGISTRYINDEX); + const char* arg = luaL_checkstring(L,WSLUA_ARG_register_stat_cmd_arg_ARGUMENT); + statcmd_t* sc = g_malloc0(sizeof(statcmd_t)); /* XXX leaked */ + + sc->L = L; + lua_pushvalue(L, WSLUA_OPTARG_register_stat_cmd_arg_ACTION); + sc->func_ref = luaL_ref(L, LUA_REGISTRYINDEX); lua_remove(L,1); - register_stat_cmd_arg(arg, statcmd_init, sc); - return 0; + register_stat_cmd_arg(arg, statcmd_init, sc); + return 0; } |