regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / epan / wslua / wslua_column.c
blob358ac57f0288331f19e94666b1eb614a217ab0db
1 /*
2 * wslua_column.c
4 * Wireshark's interface to the Lua Programming Language
6 * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
7 * (c) 2008, Balint Reczey <balint.reczey@ericsson.com>
8 * (c) 2011, Stig Bjorlykke <stig@bjorlykke.org>
9 * (c) 2014, Hadriel Kaplan <hadrielk@yahoo.com>
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
15 * SPDX-License-Identifier: GPL-2.0-or-later
17 #include "config.h"
19 #include "wslua_pinfo_common.h"
22 /* WSLUA_CONTINUE_MODULE Pinfo */
25 static GPtrArray* outstanding_Column;
26 static GPtrArray* outstanding_Columns;
28 CLEAR_OUTSTANDING(Column,expired, true)
29 CLEAR_OUTSTANDING(Columns,expired, true)
31 #define PUSH_COLUMN(L,c) {g_ptr_array_add(outstanding_Column,c);pushColumn(L,c);}
33 void Push_Columns(lua_State *L, Columns c)
35 g_ptr_array_add(outstanding_Columns, c);
36 pushColumns(L, c);
40 WSLUA_CLASS_DEFINE(Column,FAIL_ON_NULL("Column")); /* A Column in the packet list. */
42 struct col_names_t {
43 const char* name;
44 int id;
47 // Duplicated below in Columns__newindex.
48 static const struct col_names_t colnames[] = {
49 {"number",COL_NUMBER},
50 {"number_displayed",COL_NUMBER_DIS},
51 {"abs_time",COL_ABS_TIME},
52 {"utc_time",COL_UTC_TIME},
53 {"cls_time",COL_CLS_TIME},
54 {"rel_time",COL_REL_TIME},
55 {"date",COL_ABS_YMD_TIME},
56 {"date_doy",COL_ABS_YDOY_TIME},
57 {"utc_date",COL_UTC_YMD_TIME},
58 {"utc_date_doy",COL_UTC_YDOY_TIME},
59 {"delta_time",COL_DELTA_TIME},
60 {"delta_time_displayed",COL_DELTA_TIME_DIS},
61 {"src",COL_DEF_SRC},
62 {"src_res",COL_RES_SRC},
63 {"src_unres",COL_UNRES_SRC},
64 {"dl_src",COL_DEF_DL_SRC},
65 {"dl_src_res",COL_RES_DL_SRC},
66 {"dl_src_unres",COL_UNRES_DL_SRC},
67 {"net_src",COL_DEF_NET_SRC},
68 {"net_src_res",COL_RES_NET_SRC},
69 {"net_src_unres",COL_UNRES_NET_SRC},
70 {"dst",COL_DEF_DST},
71 {"dst_res",COL_RES_DST},
72 {"dst_unres",COL_UNRES_DST},
73 {"dl_dst",COL_DEF_DL_DST},
74 {"dl_dst_res",COL_RES_DL_DST},
75 {"dl_dst_unres",COL_UNRES_DL_DST},
76 {"net_dst",COL_DEF_NET_DST},
77 {"net_dst_res",COL_RES_NET_DST},
78 {"net_dst_unres",COL_UNRES_NET_DST},
79 {"src_port",COL_DEF_SRC_PORT},
80 {"src_port_res",COL_RES_SRC_PORT},
81 {"src_port_unres",COL_UNRES_SRC_PORT},
82 {"dst_port",COL_DEF_DST_PORT},
83 {"dst_port_res",COL_RES_DST_PORT},
84 {"dst_port_unres",COL_UNRES_DST_PORT},
85 {"protocol",COL_PROTOCOL},
86 {"info",COL_INFO},
87 {"packet_len",COL_PACKET_LENGTH},
88 {"cumulative_bytes",COL_CUMULATIVE_BYTES},
89 {"direction",COL_IF_DIR},
90 {"tx_rate",COL_TX_RATE},
91 {"rssi",COL_RSSI},
92 {NULL,0}
95 static int col_name_to_id(const char* name) {
96 const struct col_names_t* cn;
97 for(cn = colnames; cn->name; cn++) {
98 if (g_str_equal(cn->name,name)) {
99 return cn->id;
103 return 0;
106 static const char* col_id_to_name(int id) {
107 const struct col_names_t* cn;
108 for(cn = colnames; cn->name; cn++) {
109 if ( cn->id == id ) {
110 return cn->name;
113 return NULL;
117 WSLUA_METAMETHOD Column__tostring(lua_State *L) {
118 Column c = checkColumn(L,1);
119 const char* text;
121 if (!c->cinfo) {
122 text = col_id_to_name(c->col);
123 lua_pushfstring(L, "(%s)", text ? text : "unknown");
125 else {
126 text = col_get_text(c->cinfo, c->col);
127 lua_pushstring(L, text ? text : "(nil)");
130 WSLUA_RETURN(1); /* The column's string text (in parenthesis if not available). */
133 /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS */
134 static int Column__gc(lua_State* L) {
135 Column col = toColumn(L,1);
137 if (!col) return 0;
139 if (!col->expired)
140 col->expired = true;
141 else
142 g_free(col);
144 return 0;
148 WSLUA_METHOD Column_clear(lua_State *L) {
149 /* Clears a Column. */
150 Column c = checkColumn(L,1);
152 if (!(c->cinfo)) return 0;
154 col_clear(c->cinfo, c->col);
156 return 0;
159 WSLUA_METHOD Column_set(lua_State *L) {
160 /* Sets the text of a Column. */
161 #define WSLUA_ARG_Column_set_TEXT 2 /* The text to which to set the Column. */
162 Column c = checkColumn(L,1);
163 const char* s = luaL_checkstring(L,WSLUA_ARG_Column_set_TEXT);
165 if (!(c->cinfo))
166 return 0;
168 col_add_str(c->cinfo, c->col, s);
170 return 0;
173 WSLUA_METHOD Column_append(lua_State *L) {
174 /* Appends text to a Column. */
175 #define WSLUA_ARG_Column_append_TEXT 2 /* The text to append to the Column. */
176 Column c = checkColumn(L,1);
177 const char* s = luaL_checkstring(L,WSLUA_ARG_Column_append_TEXT);
179 if (!(c->cinfo))
180 return 0;
182 col_append_str(c->cinfo, c->col, s);
184 return 0;
187 WSLUA_METHOD Column_prepend(lua_State *L) {
188 /* Prepends text to a Column. */
189 #define WSLUA_ARG_Column_prepend_TEXT 2 /* The text to prepend to the Column. */
190 Column c = checkColumn(L,1);
191 const char* s = luaL_checkstring(L,WSLUA_ARG_Column_prepend_TEXT);
193 if (!(c->cinfo))
194 return 0;
196 col_prepend_fstr(c->cinfo, c->col, "%s",s);
198 return 0;
201 WSLUA_METHOD Column_fence(lua_State *L) {
202 /* Sets Column text fence, to prevent overwriting. */
203 Column c = checkColumn(L,1);
205 if (c->cinfo)
206 col_set_fence(c->cinfo, c->col);
208 return 0;
211 WSLUA_METHOD Column_clear_fence(lua_State *L) {
212 /* Clear Column text fence. */
213 Column c = checkColumn(L,1);
215 if (c->cinfo)
216 col_clear_fence(c->cinfo, c->col);
218 return 0;
222 WSLUA_METHODS Column_methods[] = {
223 WSLUA_CLASS_FNREG(Column,clear),
224 WSLUA_CLASS_FNREG(Column,set),
225 WSLUA_CLASS_FNREG(Column,append),
226 WSLUA_CLASS_FNREG(Column,prepend),
227 WSLUA_CLASS_FNREG_ALIAS(Column,preppend,prepend),
228 WSLUA_CLASS_FNREG(Column,fence),
229 WSLUA_CLASS_FNREG(Column,clear_fence),
230 { NULL, NULL }
234 WSLUA_META Column_meta[] = {
235 WSLUA_CLASS_MTREG(Column,tostring),
236 { NULL, NULL }
240 int Column_register(lua_State *L) {
241 WSLUA_REGISTER_CLASS(Column);
242 if (outstanding_Column != NULL) {
243 g_ptr_array_unref(outstanding_Column);
245 outstanding_Column = g_ptr_array_new();
246 return 0;
250 WSLUA_CLASS_DEFINE(Columns,NOP); /* The <<lua_class_Column,``Column``>>s of the packet list. */
252 WSLUA_METAMETHOD Columns__tostring(lua_State *L) {
253 lua_pushstring(L,"Columns");
254 WSLUA_RETURN(1);
255 /* The string "Columns". This has no real use aside from debugging. */
259 * To document this is very odd - it won't make sense to a person reading the
260 * API docs to see this metamethod as a method, but oh well.
262 WSLUA_METAMETHOD Columns__newindex(lua_State *L) { /*
263 Sets the text of a specific column.
264 Some columns cannot be modified, and no error is raised if attempted.
265 The columns that are known to allow modification are "info" and "protocol".
267 #define WSLUA_ARG_Columns__newindex_COLUMN 2 /*
268 The name of the column to set.
269 Valid values are:
271 [options="header"]
272 |===
273 |Name |Description
274 |number |Frame number
275 |abs_time |Absolute timestamp
276 |utc_time |UTC timestamp
277 |cls_time |CLS timestamp
278 |rel_time |Relative timestamp
279 |date |Absolute date and time
280 |date_doy |Absolute year, day of year, and time
281 |utc_date |UTC date and time
282 |utc_date_doy |UTC year, day of year, and time
283 |delta_time |Delta time from previous packet
284 |delta_time_displayed |Delta time from previous displayed packet
285 |src |Source address
286 |src_res |Resolved source address
287 |src_unres |Numeric source address
288 |dl_src |Source data link address
289 |dl_src_res |Resolved source data link address
290 |dl_src_unres |Numeric source data link address
291 |net_src |Source network address
292 |net_src_res |Resolved source network address
293 |net_src_unres |Numeric source network address
294 |dst |Destination address
295 |dst_res |Resolve destination address
296 |dst_unres |Numeric destination address
297 |dl_dst |Destination data link address
298 |dl_dst_res |Resolved destination data link address
299 |dl_dst_unres |Numeric destination data link address
300 |net_dst |Destination network address
301 |net_dst_res |Resolved destination network address
302 |net_dst_unres |Numeric destination network address
303 |src_port |Source port
304 |src_port_res |Resolved source port
305 |src_port_unres |Numeric source port
306 |dst_port |Destination port
307 |dst_port_res |Resolved destination port
308 |dst_port_unres |Numeric destination port
309 |protocol |Protocol name
310 |info |General packet information
311 |packet_len |Packet length
312 |cumulative_bytes |Cumulative bytes in the capture
313 |direction |Packet direction
314 |vsan |Virtual SAN
315 |tx_rate |Transmit rate
316 |rssi |RSSI value
317 |dce_call |DCE call
318 |===
320 ===== Example
321 pinfo.cols['info'] = 'foo bar'
323 -- syntactic sugar (equivalent to above)
324 pinfo.cols.info = 'foo bar'
326 #define WSLUA_ARG_Columns__newindex_TEXT 3 /* The text for the column. */
327 Columns cols = checkColumns(L,1);
328 const struct col_names_t* cn;
329 const char* colname;
330 const char* text;
332 if (!cols) return 0;
333 if (cols->expired) {
334 luaL_error(L,"expired column");
335 return 0;
338 colname = luaL_checkstring(L,WSLUA_ARG_Columns__newindex_COLUMN);
339 text = luaL_checkstring(L,WSLUA_ARG_Columns__newindex_TEXT);
341 for(cn = colnames; cn->name; cn++) {
342 if( g_str_equal(cn->name,colname) ) {
343 col_add_str(cols->cinfo, cn->id, text);
344 return 0;
348 WSLUA_ARG_ERROR(Columns__newindex,COLUMN,"the column name must be a valid column");
349 return 0;
352 WSLUA_METAMETHOD Columns__index(lua_State *L) {
353 /* Get a specific <<lua_class_Column,``Column``>>. */
354 Columns cols = checkColumns(L,1);
355 const struct col_names_t* cn;
356 const char* colname = luaL_checkstring(L,2);
358 if (!cols) {
359 Column c = (Column)g_malloc(sizeof(struct _wslua_col_info));
360 c->cinfo = NULL;
361 c->col = col_name_to_id(colname);
362 c->expired = false;
364 PUSH_COLUMN(L,c);
365 return 1;
369 if (cols->expired) {
370 luaL_error(L,"expired column");
371 return 0;
374 for(cn = colnames; cn->name; cn++) {
375 if( g_str_equal(cn->name,colname) ) {
376 Column c = (Column)g_malloc(sizeof(struct _wslua_col_info));
377 c->cinfo = cols->cinfo;
378 c->col = col_name_to_id(colname);
379 c->expired = false;
381 PUSH_COLUMN(L,c);
382 return 1;
386 return 0;
389 /* for internal use - used by Pinfo */
390 int get_Columns_index(lua_State *L)
392 return Columns__index(L);
396 /* Gets registered as metamethod automatically by WSLUA_REGISTER_META */
397 static int Columns__gc(lua_State* L) {
398 Columns cols = toColumns(L,1);
400 if (!cols) return 0;
402 if (!cols->expired)
403 cols->expired = true;
404 else
405 g_free(cols);
407 return 0;
412 WSLUA_META Columns_meta[] = {
413 WSLUA_CLASS_MTREG(Columns,tostring),
414 WSLUA_CLASS_MTREG(Columns,newindex),
415 WSLUA_CLASS_MTREG(Columns,index),
416 { NULL, NULL }
420 int Columns_register(lua_State *L) {
421 WSLUA_REGISTER_META(Columns);
422 if (outstanding_Columns != NULL) {
423 g_ptr_array_unref(outstanding_Columns);
425 outstanding_Columns = g_ptr_array_new();
426 return 0;
432 * Editor modelines - https://www.wireshark.org/tools/modelines.html
434 * Local variables:
435 * c-basic-offset: 4
436 * tab-width: 8
437 * indent-tabs-mode: nil
438 * End:
440 * vi: set shiftwidth=4 tabstop=8 expandtab:
441 * :indentSize=4:tabSize=8:noTabs=true: