Give lua a minimal gui.

svn path=/trunk/; revision=17150
This commit is contained in:
Luis Ontanon 2006-02-04 20:00:58 +00:00
parent e487660dfc
commit fedb9b5c92
5 changed files with 150 additions and 1 deletions

View File

@ -33,6 +33,7 @@ lua_la_SOURCES = \
lua_tree.c \
lua_pinfo.c \
lua_tap.c \
lua_gui.c \
packet-lua.c \
packet-lua.h \
plugin.c

125
plugins/lua/lua_gui.c Normal file
View File

@ -0,0 +1,125 @@
/*
* lua_gui.c
*
*
* Created by L. E. G. O. on 2006/02/04.
* Copyright 2006 __MyCompanyName__. All rights reserved.
*
*/
#include "packet-lua.h"
LUA_CLASS_DEFINE(TextWindow,TEXT_WINDOW,NOP)
static const funnel_ops_t* ops = NULL;
static int TextWindow_new(lua_State* L) {
const gchar* title;
TextWindow tw;
if (!ops) {
luaL_error(L,"GUI system not available");
return 0;
}
title = luaL_optstring(L,1,"Untitled Window");
tw = ops->new_text_window(title);
pushTextWindow(L,tw);
return 1;
}
static int TextWindow_set_text(lua_State* L) {
TextWindow tw = shiftTextWindow(L,1);
const gchar* text = luaL_checkstring(L,1);
if (!text) return 0;
ops->set_text(tw,text);
pushTextWindow(L,tw);
return 1;
}
static int TextWindow_append_text(lua_State* L) {
TextWindow tw = shiftTextWindow(L,1);
const gchar* text = luaL_checkstring(L,1);
if (!text) return 0;
ops->append_text(tw,text);
pushTextWindow(L,tw);
return 1;
}
static int TextWindow_prepend_text(lua_State* L) {
TextWindow tw = shiftTextWindow(L,1);
const gchar* text = luaL_checkstring(L,1);
if (!text) return 0;
ops->prepend_text(tw,text);
pushTextWindow(L,tw);
return 1;
}
static int TextWindow_clear_text(lua_State* L) {
TextWindow tw = shiftTextWindow(L,1);
ops->clear_text(tw);
pushTextWindow(L,tw);
return 1;
}
static int TextWindow_get_text(lua_State* L) {
TextWindow tw = shiftTextWindow(L,1);
const gchar* text = ops->get_text(tw);
lua_pushstring(L,text);
return 1;
}
static int TextWindow_gc(lua_State* L) {
TextWindow tw = shiftTextWindow(L,1);
ops->destroy_text_window(tw);
return 1;
}
static const luaL_reg TextWindow_methods[] = {
{"new", TextWindow_new},
{"set", TextWindow_set_text},
{"append", TextWindow_append_text},
{"prepend", TextWindow_prepend_text},
{"clear", TextWindow_clear_text},
{0, 0}
};
static const luaL_reg TextWindow_meta[] = {
{"__tostring", TextWindow_get_text},
{"__GC", TextWindow_gc},
{0, 0}
};
int TextWindow_register(lua_State* L) {
ops = funnel_get_funnel_ops();
luaL_openlib(L, TEXT_WINDOW, TextWindow_methods, 0);
luaL_newmetatable(L, TEXT_WINDOW);
luaL_openlib(L, 0, TextWindow_meta, 0);
lua_pushliteral(L, "__index");
lua_pushvalue(L, -3);
lua_rawset(L, -3);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, -3);
lua_rawset(L, -3);
lua_pop(L, 1);
return 1;
}

View File

@ -239,7 +239,7 @@ static int ByteArray_tostring(lua_State* L) {
static int Tvb_new_real (lua_State *L);
static const luaL_reg ByteArray_methods[] = {
{"new", ByteArray_new},
{"new", ByteArray_new},
{"len", ByteArray_len},
{"prepend", ByteArray_prepend},
{"append", ByteArray_append},

View File

@ -39,6 +39,18 @@ tvbuff_t* lua_tvb;
int lua_malformed;
dissector_handle_t lua_data_handle;
const gchar* lua_shiftstring(lua_State* L, int i) {
const gchar* p = luaL_checkstring(L, i);
if (p) {
lua_remove(L,i);
return p;
} else {
return NULL;
}
}
static int lua_format_date(lua_State* LS) {
lua_Number time = luaL_checknumber(LS,1);
nstime_t then;
@ -292,6 +304,9 @@ static int init_error_handler(lua_State* L) {
static void init_lua(void) {
if ( ! lua_initialized ) {
TextWindow_register(L);
GString* tap_error = lua_register_all_taps();
if ( tap_error ) {

View File

@ -47,6 +47,7 @@
#include <epan/filesystem.h>
#include <epan/report_err.h>
#include <epan/emem.h>
#include <epan/funnel.h>
#define LUA_DISSECTORS_TABLE "dissectors"
#define LUA_INIT_ROUTINES "init_routines"
@ -166,6 +167,9 @@ typedef struct _eth_tap {
gboolean registered;
}* Tap;
#define TEXT_WINDOW "TextWindow"
typedef funnel_text_window_t* TextWindow;
#define NOP
/*
@ -242,12 +246,16 @@ LUA_CLASS_DECLARE(ProtoItem,ITEM);
LUA_CLASS_DECLARE(Dissector,DISSECTOR);
LUA_CLASS_DECLARE(DissectorTable,DISSECTOR_TABLE);
LUA_CLASS_DECLARE(Address,ADDRESS);
LUA_CLASS_DECLARE(TextWindow,TEXT_WINDOW);
extern void dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree);
extern int lua_tap_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const void *data _U_);
extern void lua_tap_reset(void *tapdata);
extern void lua_tap_draw(void *tapdata);
extern const gchar* lua_shiftstring(lua_State* L,int idx);
extern GString* lua_register_all_taps(void);
extern void lua_prime_all_fields(proto_tree* tree);
void lua_register_subtrees(void);