4 * Wireshark's interface to the Lua Programming Language
5 * for various libwiretap utility functions.
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * SPDX-License-Identifier: GPL-2.0-or-later
16 /* WSLUA_MODULE Wtap Wtap Functions For Handling Capture File Types */
21 #include <wiretap/wtap.h>
24 * Solely for the function that gets the table of backwards-compatibility
25 * Lua names for file types/subtypes.
27 #include <wiretap/wtap-int.h>
29 WSLUA_FUNCTION
wslua_wtap_file_type_subtype_description(lua_State
* LS
) {
31 Get a string describing a capture file type, given a filetype
32 value for that file type.
34 #define WSLUA_ARG_wtap_file_type_subtype_description_FILETYPE 1 /* The type for which the description is to be fetched - a number returned by `wtap_name_to_file_type_subtype()`. */
35 lua_Integer filetype
= luaL_checkinteger(LS
,WSLUA_ARG_wtap_file_type_subtype_description_FILETYPE
);
36 /* wtap_file_type_subtype_description()'s name isn't really descriptive. */
37 if (filetype
> INT_MAX
) {
41 const char* str
= wtap_file_type_subtype_description((int)filetype
);
45 lua_pushstring(LS
,str
);
47 WSLUA_RETURN(1); /* The description of the file type with that filetype value, or nil if there is no such file type. */
50 WSLUA_FUNCTION
wslua_wtap_file_type_subtype_name(lua_State
* LS
) {
52 Get a string giving the name for a capture file type, given a filetype
53 value for that file type.
55 #define WSLUA_ARG_wtap_file_type_subtype_name_FILETYPE 1 /* The type for which the name is to be fetched - a number returned by `wtap_name_to_file_type_subtype()`. */
56 lua_Integer filetype
= luaL_checkinteger(LS
,WSLUA_ARG_wtap_file_type_subtype_name_FILETYPE
);
57 /* wtap_file_type_subtype_description()'s name isn't really descriptive. */
58 if (filetype
> INT_MAX
) {
62 const char* str
= wtap_file_type_subtype_name((int)filetype
);
66 lua_pushstring(LS
,str
);
68 WSLUA_RETURN(1); /* The name of the file type with that filetype value, or nil if there is no such file type. */
71 WSLUA_FUNCTION
wslua_wtap_name_to_file_type_subtype(lua_State
* LS
) {
73 Get a filetype value for a file type, given the name for that
76 #define WSLUA_ARG_wtap_name_to_file_type_subtype_NAME 1 /* The name of a file type. */
77 const char* name
= luaL_checkstring(LS
,WSLUA_ARG_wtap_name_to_file_type_subtype_NAME
);
78 lua_Integer filetype
= wtap_name_to_file_type_subtype(name
);
82 lua_pushinteger(LS
,filetype
);
83 WSLUA_RETURN(1); /* The filetype value for the file type with that name, or nil if there is no such file type. */
86 WSLUA_FUNCTION
wslua_wtap_pcap_file_type_subtype(lua_State
* LS
) {
87 /* Get the filetype value for pcap files. */
88 lua_Integer filetype
= wtap_pcap_file_type_subtype();
89 lua_pushinteger(LS
,filetype
);
90 WSLUA_RETURN(1); /* The filetype value for pcap files. */
93 WSLUA_FUNCTION
wslua_wtap_pcap_nsec_file_type_subtype(lua_State
* LS
) {
94 /* Get the filetype value for nanosecond-resolution pcap files. */
95 lua_Integer filetype
= wtap_pcap_nsec_file_type_subtype();
96 lua_pushinteger(LS
,filetype
);
97 WSLUA_RETURN(1); /* The filetype value for nanosecond-resolution pcap files. */
100 WSLUA_FUNCTION
wslua_wtap_pcapng_file_type_subtype(lua_State
* LS
) {
101 /* Get the filetype value for pcapng files. */
102 lua_Integer filetype
= wtap_pcapng_file_type_subtype();
103 lua_pushinteger(LS
,filetype
);
104 WSLUA_RETURN(1); /* The filetype value for pcapng files. */
108 * init.wslua-only function to return a table to assign to
111 extern void wslua_init_wtap_filetypes(lua_State
* LS
) {
112 /* Get the GArray from which we initialize this. */
113 const GArray
*table
= get_backwards_compatibility_lua_table();
116 * Create the table; it's indexted by strings, not numbers,
117 * so none of the entries will be in a sequence.
119 lua_createtable(LS
,0,table
->len
);
120 for (unsigned i
= 0; i
< table
->len
; i
++) {
121 struct backwards_compatibiliity_lua_name
*entry
;
123 entry
= &g_array_index(table
,
124 struct backwards_compatibiliity_lua_name
, i
);
126 * Push the name and the ft, in order, so that the ft,
127 * which should be the value at the top of the stack,
128 * is at the top of the stack, and the name, which should
129 * be the value just below that, is the value just below
132 lua_pushstring(LS
, entry
->name
);
133 lua_pushinteger(LS
, entry
->ft
);
135 * The -3 is the index, relative to the top of the stack, of
136 * the table; the two elements on top of it are the ft and
137 * the name, so it's -3.
139 lua_settable(LS
, -3);
141 lua_setglobal(LS
, "wtap_filetypes");