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) */ 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); 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 (!lua_isfunction(L,WSLUA_ARG_register_menu_ACTION)) 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); funnel_register_menu(name, group, lua_menu_callback, md, retap); WSLUA_RETURN(0); }
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 */ const gchar* title; int top = lua_gettop(L); int i; GPtrArray* labels; struct _dlg_cb_data* dcbd; if (! ops) { luaL_error(L,"the GUI facility has to be enabled"); return 0; } if (! (title = luaL_checkstring(L,WSLUA_ARG_new_dialog_TITLE)) ) { WSLUA_ARG_ERROR(new_dialog,TITLE,"Must be a string"); } if (! lua_isfunction(L,WSLUA_ARG_new_dialog_ACTION)) { WSLUA_ARG_ERROR(new_dialog,ACTION,"Must be a function"); } if (top < 3) { WSLUA_ERROR(new_dialog,"At least one field required"); } dcbd = g_malloc(sizeof(struct _dlg_cb_data)); dcbd->L = L; lua_remove(L,1); lua_pushvalue(L, 1); dcbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX); lua_remove(L,1); labels = g_ptr_array_new(); top -= 2; 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"); g_ptr_array_add(labels,label); } g_ptr_array_add(labels,NULL); ops->new_dialog(title, (const gchar**)labels->pdata, lua_dialog_cb, dcbd); g_ptr_array_free(labels,FALSE); WSLUA_RETURN(0); }
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 */ Columns cols = checkColumns(L,1); const struct col_names_t* cn; const char* colname; const char* text; if (!cols) return 0; if (cols->expired) { luaL_error(L,"expired column"); return 0; } colname = luaL_checkstring(L,WSLUA_ARG_Columns__newindex_COLUMN); text = luaL_checkstring(L,WSLUA_ARG_Columns__newindex_TEXT); for(cn = colnames; cn->name; cn++) { if( g_str_equal(cn->name,colname) ) { col_add_str(cols->cinfo, cn->id, text); return 0; } } WSLUA_ARG_ERROR(Columns__newindex,COLUMN,"the column name must be a valid column"); }
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. */ 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"); } 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; } }
WSLUA_METHOD TextWindow_prepend(lua_State* L) { /* Prepends text */ #define WSLUA_ARG_TextWindow_prepend_TEXT 2 /* The text to be appended */ 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->expired) WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); 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_CONSTRUCTOR Dir_open(lua_State* L) { /* Opens a directory and returns a `Dir` object representing the files in the directory. @code for filename in Dir.open(path) do ... end @endcode */ #define WSLUA_ARG_Dir_open_PATHNAME 1 /* The pathname of the directory. */ #define WSLUA_OPTARG_Dir_open_EXTENSION 2 /* If given, only files with this extension will be returned. */ 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"); return 0; } if (!test_for_directory(dirname_clean)) { g_free(dirname_clean); WSLUA_ARG_ERROR(Dir_open,PATHNAME, "must be a directory"); return 0; } dir = (Dir)g_malloc(sizeof(struct _wslua_dir)); dir->dir = g_dir_open(dirname_clean, 0, dir->dummy); g_free(dirname_clean); dir->ext = extension ? g_strdup(extension) : NULL; dir->dummy = (GError **)g_malloc(sizeof(GError *)); *(dir->dummy) = NULL; if (dir->dir == NULL) { g_free(dir->dummy); g_free(dir); WSLUA_ARG_ERROR(Dir_open,PATHNAME,"could not open directory"); return 0; } pushDir(L,dir); WSLUA_RETURN(1); /* the `Dir` object. */ }
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 */ 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"); filename = wslua_get_actual_filename(given_fname); 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; }
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 */ 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; 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"); 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); 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; 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); } WSLUA_RETURN(1); /* The TextWindow object. */ }
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->expired) WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Expired TextWindow"); ops->clear_text(tw->ws_tw); WSLUA_RETURN(1); /* The TextWindow object. */ }
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. */ const gchar* name = luaL_checkstring(L,WSLUA_ARG_Dissector_get_NAME); Dissector d; if ((d = find_dissector(name))) { pushDissector(L, d); WSLUA_RETURN(1); /* The Dissector reference. */ } WSLUA_ARG_ERROR(Dissector_get,NAME,"No such dissector"); return 0; }
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 */ 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"); dirname_clean = wslua_get_actual_filename(dirname); if (!dirname_clean) WSLUA_ARG_ERROR(Dir_open,PATHNAME,"directory does not exist"); if (!test_for_directory(dirname_clean)) { g_free(dirname_clean); WSLUA_ARG_ERROR(Dir_open,PATHNAME, "must be a directory"); } 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 (dir->dir == NULL) { g_free(dir->dummy); g_free(dir); WSLUA_ARG_ERROR(Dir_open,PATHNAME,"could not open directory"); } pushDir(L,dir); WSLUA_RETURN(1); /* the Dir object */ }
WSLUA_METHOD TextWindow_get_text(lua_State* L) { /* Get the text of the window */ TextWindow tw = checkTextWindow(L,1); const gchar* text; 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"); text = ops->get_text(tw->ws_tw); lua_pushstring(L,text); WSLUA_RETURN(1); /* The TextWindow's text. */ }
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 */ Column c = checkColumn(L,1); const gchar* s = luaL_checkstring(L,WSLUA_ARG_Column_set_TEXT); if (!(c && c->cinfo)) return 0; if (!s) WSLUA_ARG_ERROR(Column_set,TEXT,"must be a string"); col_add_str(c->cinfo, c->col, s); return 0; }
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 */ Column c = checkColumn(L,1); const gchar* s = luaL_checkstring(L,WSLUA_ARG_Column_append_TEXT); if (!(c && c->cinfo)) return 0; if (!s) WSLUA_ARG_ERROR(Column_append,TEXT,"must be a string"); col_append_str(c->cinfo, c->col, s); return 0; }
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. */ 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 (!url) { WSLUA_ARG_ERROR(browser_open_url,URL,"Must be a string"); } ops->browser_open_url(url); return 0; }
WSLUA_FUNCTION wslua_set_filter(lua_State* L) { /* Set the main filter text */ #define WSLUA_ARG_set_filter_TEXT 1 /* The filter's 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 (!filter_str) { WSLUA_ARG_ERROR(set_filter,TEXT,"Must be a string"); } ops->set_filter(filter_str); return 0; }
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. */ 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 (!file) { WSLUA_ARG_ERROR(browser_open_data_file,FILENAME,"Must be a string"); } ops->browser_open_data_file(file); return 0; }
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 */ 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"); if (ops->set_editable) ops->set_editable(tw->ws_tw,editable); WSLUA_RETURN(1); /* The TextWindow object. */ }
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. */ 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"); } gstr = g_string_new(copied_str); ops->copy_to_clipboard(gstr); g_string_free(gstr,TRUE); return 0; }
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 */ const char *given_fname = luaL_checkstring(L, WSLUA_ARG_loadfile_FILENAME); char* filename; filename = wslua_get_actual_filename(given_fname); 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; } }
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 */ 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"); lua_settop(L,2); if (! lua_isfunction(L,2)) WSLUA_ARG_ERROR(TextWindow_at_close,ACTION,"Must be a function"); cbd = g_malloc(sizeof(struct _close_cb_data)); cbd->L = L; cbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX); cbd->wslua_tw = tw; ops->set_close_cb(tw->ws_tw,text_win_close_cb,cbd); WSLUA_RETURN(1); /* The TextWindow object. */ }