Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
syntax-tree.h
Go to the documentation of this file.
1/*
2 * Wireshark - Network traffic analyzer
3 * By Gerald Combs <gerald@wireshark.org>
4 * Copyright 2001 Gerald Combs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#ifndef SYNTAX_TREE_H
10#define SYNTAX_TREE_H
11
12#include <stdio.h>
13#include <inttypes.h>
14
15#include <wsutil/ws_assert.h>
16#include <wsutil/wslog.h>
17#include <epan/ftypes/ftypes.h>
18#include "dfilter-loc.h"
19
23#ifdef __cplusplus
24extern "C" {
25#endif /* __cplusplus */
26
27#define ASSERT_STTYPE_NOT_REACHED(st) \
28 ws_error("Invalid syntax node type '%s'.", sttype_name(st))
29
30#define ASSERT_STNODE_OP_NOT_REACHED(op) \
31 ws_error("Invalid stnode op '%s'.", stnode_op_name(op))
32
54
55
61typedef void *(*STTypeNewFunc)(void *data);
62
68typedef void *(*STTypeDupFunc)(const void *data);
69
74typedef void (*STTypeFreeFunc)(void *data);
75
82typedef char *(*STTypeToStrFunc)(const void *data, bool pretty);
83
84
95
96
106
107
109#define STFLAG_UNPARSED (1 << 0)
110
111
124
125
154
155
164
165/* These are the sttype_t registration function prototypes. */
169void sttype_register_field(void);
170
174void sttype_register_function(void);
175
179void sttype_register_number(void);
180
184void sttype_register_pointer(void);
185
189void sttype_register_set(void);
190
194void sttype_register_slice(void);
195
199void sttype_register_string(void);
200
204void sttype_register_opers(void);
205
212void
213sttype_init(void);
214
222void
223sttype_cleanup(void);
224
232void
234
241WS_DLL_PUBLIC
242const char *
243sttype_name(const sttype_id_t type);
244
245
252WS_DLL_PUBLIC
253const char *
255
265WS_DLL_PUBLIC
267stnode_new(sttype_id_t type_id, void *data, char *token, df_loc_t loc);
268
275WS_DLL_PUBLIC
278
287WS_DLL_PUBLIC
289stnode_dup(const stnode_t *org);
290
298WS_DLL_PUBLIC
299void
301
311WS_DLL_PUBLIC
312void
313stnode_init(stnode_t *node, sttype_id_t type_id, void *data, char *token, df_loc_t loc);
314
322WS_DLL_PUBLIC
323void
324stnode_replace(stnode_t *node, sttype_id_t type_id, void *data);
325
332WS_DLL_PUBLIC
333void
334stnode_mutate(stnode_t *node, sttype_id_t type_id);
335
341WS_DLL_PUBLIC
342void
343stnode_free(stnode_t *node);
344
351WS_DLL_PUBLIC
352const char*
353stnode_type_name(const stnode_t *node);
354
361WS_DLL_PUBLIC
363stnode_type_id(const stnode_t *node);
364
371WS_DLL_PUBLIC
372void *
373stnode_data(stnode_t *node);
374
381WS_DLL_PUBLIC
382GString *
384
391WS_DLL_PUBLIC
392void *
394
395WS_DLL_PUBLIC
396
403const char *
404stnode_token(const stnode_t *node);
405
412WS_DLL_PUBLIC
414stnode_location(const stnode_t *node);
415
422WS_DLL_PUBLIC
423void
425
433WS_DLL_PUBLIC
434bool
435stnode_get_flags(stnode_t *node, uint16_t flags);
436
443WS_DLL_PUBLIC
444void
445stnode_set_flags(stnode_t *node, uint16_t flags);
446
456void
458
466WS_DLL_PUBLIC
467const char *
468stnode_tostr(stnode_t *node, bool pretty);
469
478#define stnode_todisplay(node) stnode_tostr(node, true)
479
488#define stnode_todebug(node) stnode_tostr(node, false)
489
500void
501log_node_full(enum ws_log_level level,
502 const char *file, int line, const char *func,
503 stnode_t *node, const char *msg);
504
515void
516log_test_full(enum ws_log_level level,
517 const char *file, int line, const char *func,
518 stnode_t *node, const char *msg);
519
520#ifdef WS_DEBUG
521#define log_node(node) \
522 log_node_full(LOG_LEVEL_NOISY, __FILE__, __LINE__, __func__, node, #node)
523#define log_test(node) \
524 log_test_full(LOG_LEVEL_NOISY, __FILE__, __LINE__, __func__, node, #node)
525#define LOG_NODE(node) \
526 do { \
527 if (stnode_type_id(node) == STTYPE_TEST) \
528 log_test(node); \
529 else \
530 log_node(node); \
531 } while (0)
532#else
533#define log_node(node) (void)0
534#define log_test(node) (void)0
535#define LOG_NODE(node) (void)0
536#endif
537
544char *
546
555void
556log_syntax_tree(enum ws_log_level level, stnode_t *root, const char *msg, char **cache_ptr);
557
558#ifdef WS_DEBUG
559#define ws_assert_magic(obj, mnum) \
560 do { \
561 ws_assert(obj); \
562 if ((obj)->magic != (mnum)) { \
563 ws_log_full(LOG_DOMAIN_DFILTER, LOG_LEVEL_ERROR, \
564 __FILE__, __LINE__, __func__, \
565 "Magic num is 0x%08" PRIx32", " \
566 "but should be 0x%08" PRIx32, \
567 (obj)->magic, (mnum)); \
568 } \
569 } while(0)
570#else
571#define ws_assert_magic(obj, mnum) (void)0
572#endif
573
574#ifdef __cplusplus
575}
576#endif /* __cplusplus */
577
578#endif /* SYNTAX_TREE_H */
Represents a location (column start and length) within a display filter string.
Definition dfilter-loc.h:19
A single node instance in the display filter syntax tree.
Definition syntax-tree.h:115
df_loc_t location
Definition syntax-tree.h:121
uint16_t flags
Definition syntax-tree.h:122
void * data
Definition syntax-tree.h:117
char * repr_debug
Definition syntax-tree.h:120
char * repr_token
Definition syntax-tree.h:118
char * repr_display
Definition syntax-tree.h:119
sttype_t * type
Definition syntax-tree.h:116
Describes a syntax tree node type, including its lifecycle and serialization callbacks.
Definition syntax-tree.h:88
sttype_id_t id
Definition syntax-tree.h:89
STTypeToStrFunc func_tostr
Definition syntax-tree.h:93
STTypeNewFunc func_new
Definition syntax-tree.h:90
STTypeFreeFunc func_free
Definition syntax-tree.h:91
STTypeDupFunc func_dup
Definition syntax-tree.h:92
void sttype_cleanup(void)
Cleans up resources associated with the syntax tree types.
Definition syntax-tree.c:38
WS_DLL_PUBLIC void stnode_init(stnode_t *node, sttype_id_t type_id, void *data, char *token, df_loc_t loc)
Initialize a syntax tree node.
Definition syntax-tree.c:206
WS_DLL_PUBLIC void stnode_set_location(stnode_t *node, df_loc_t loc)
Set the location for a syntax tree node.
Definition syntax-tree.c:351
void log_test_full(enum ws_log_level level, const char *file, int line, const char *func, stnode_t *node, const char *msg)
Logs a full test message for a syntax tree node.
Definition syntax-tree.c:475
WS_DLL_PUBLIC void stnode_set_flags(stnode_t *node, uint16_t flags)
Sets flags for a syntax tree node.
Definition syntax-tree.c:363
void sttype_register_set(void)
Register the set type in the syntax tree type system.
Definition sttype-set.c:92
void sttype_register(sttype_t *type)
Registers a syntax tree type.
Definition syntax-tree.c:45
WS_DLL_PUBLIC void stnode_free(stnode_t *node)
Frees a syntax tree node.
Definition syntax-tree.c:295
WS_DLL_PUBLIC stnode_t * stnode_new_empty(sttype_id_t type_id)
Creates a new empty syntax tree node.
Definition syntax-tree.c:265
void sttype_register_slice(void)
Register the slice type in the syntax tree type system.
Definition sttype-slice.c:168
void sttype_init(void)
Initialize the syntax tree types.
Definition syntax-tree.c:25
WS_DLL_PUBLIC const char * sttype_name(const sttype_id_t type)
Get the name of a syntax tree type.
Definition syntax-tree.c:77
WS_DLL_PUBLIC void stnode_mutate(stnode_t *node, sttype_id_t type_id)
Mutates the type of a syntax tree node.
Definition syntax-tree.c:249
stnumber_t
Numeric sub-type tag for STTYPE_NUMBER nodes.
Definition syntax-tree.h:100
@ STNUM_FLOAT
Definition syntax-tree.h:104
@ STNUM_NONE
Definition syntax-tree.h:101
@ STNUM_UNSIGNED
Definition syntax-tree.h:103
@ STNUM_INTEGER
Definition syntax-tree.h:102
void log_syntax_tree(enum ws_log_level level, stnode_t *root, const char *msg, char **cache_ptr)
Logs a syntax tree with an optional message and caches the result.
Definition syntax-tree.c:593
stmatch_t
Quantifier controlling how many field values must satisfy a match condition.
Definition syntax-tree.h:159
@ STNODE_MATCH_ANY
Definition syntax-tree.h:161
@ STNODE_MATCH_DEF
Definition syntax-tree.h:160
@ STNODE_MATCH_ALL
Definition syntax-tree.h:162
WS_DLL_PUBLIC const char * stnode_tostr(stnode_t *node, bool pretty)
Convert a syntax tree node to a string representation.
Definition syntax-tree.c:414
WS_DLL_PUBLIC stnode_t * stnode_dup(const stnode_t *org)
Duplicates a syntax tree node.
Definition syntax-tree.c:272
sttype_id_t
Type identifier tags for syntax tree nodes in the display filter compiler.
Definition syntax-tree.h:36
@ STTYPE_SET
Definition syntax-tree.h:49
@ STTYPE_PCRE
Definition syntax-tree.h:50
@ STTYPE_SLICE
Definition syntax-tree.h:47
@ STTYPE_STRING
Definition syntax-tree.h:42
@ STTYPE_UNINITIALIZED
Definition syntax-tree.h:37
@ STTYPE_CHARCONST
Definition syntax-tree.h:43
@ STTYPE_NUM_TYPES
Definition syntax-tree.h:52
@ STTYPE_TEST
Definition syntax-tree.h:38
@ STTYPE_REFERENCE
Definition syntax-tree.h:41
@ STTYPE_FUNCTION
Definition syntax-tree.h:48
@ STTYPE_LITERAL
Definition syntax-tree.h:40
@ STTYPE_FIELD
Definition syntax-tree.h:45
@ STTYPE_NUMBER
Definition syntax-tree.h:44
@ STTYPE_ARITHMETIC
Definition syntax-tree.h:51
@ STTYPE_FVALUE
Definition syntax-tree.h:46
@ STTYPE_UNPARSED
Definition syntax-tree.h:39
WS_DLL_PUBLIC GString * stnode_string(stnode_t *node)
Retrieves the string data from a syntax tree node.
Definition syntax-tree.c:323
void sttype_register_pointer(void)
Register the pointer type in the syntax tree type system.
Definition sttype-pointer.c:125
WS_DLL_PUBLIC const char * stnode_token(const stnode_t *node)
Retrieves the token representation of a syntax tree node.
Definition syntax-tree.c:339
void *(* STTypeNewFunc)(void *data)
Allocates and initializes type-specific data for a syntax tree node.
Definition syntax-tree.h:61
WS_DLL_PUBLIC void stnode_clear(stnode_t *node)
Clears the contents of a syntax tree node.
Definition syntax-tree.c:181
WS_DLL_PUBLIC bool stnode_get_flags(stnode_t *node, uint16_t flags)
Get flags from a syntax tree node.
Definition syntax-tree.c:357
void sttype_register_string(void)
Register the string type in the syntax tree type system.
Definition sttype-string.c:53
WS_DLL_PUBLIC void * stnode_data(stnode_t *node)
Retrieves the data associated with a syntax tree node.
Definition syntax-tree.c:317
void log_node_full(enum ws_log_level level, const char *file, int line, const char *func, stnode_t *node, const char *msg)
Logs a full message with detailed information about a syntax tree node.
Definition syntax-tree.c:453
WS_DLL_PUBLIC sttype_id_t stnode_type_id(const stnode_t *node)
Retrieves the type ID of a syntax tree node.
Definition syntax-tree.c:308
char *(* STTypeToStrFunc)(const void *data, bool pretty)
Converts type-specific node data to a human-readable string.
Definition syntax-tree.h:82
void sttype_register_number(void)
Register the number type in the syntax tree type system.
Definition sttype-number.c:146
void sttype_register_opers(void)
Register the operator types in the syntax tree type system.
Definition sttype-op.c:304
void sttype_register_field(void)
Register the field type in the syntax tree type system.
Definition sttype-field.c:229
WS_DLL_PUBLIC void stnode_replace(stnode_t *node, sttype_id_t type_id, void *data)
Replaces a node in the syntax tree with a new type and data.
Definition syntax-tree.c:238
void sttype_register_function(void)
Register the function type in the syntax tree type system.
Definition sttype-function.c:151
WS_DLL_PUBLIC const char * stnode_type_name(const stnode_t *node)
Get the type name of a syntax tree node.
Definition syntax-tree.c:302
void *(* STTypeDupFunc)(const void *data)
Performs a deep copy of type-specific node data.
Definition syntax-tree.h:68
WS_DLL_PUBLIC df_loc_t stnode_location(const stnode_t *node)
Get the location of a syntax tree node.
Definition syntax-tree.c:345
void stnode_merge_location(stnode_t *dst, stnode_t *n1, stnode_t *n2)
Merges location information from two syntax tree nodes into a destination node.
Definition syntax-tree.c:372
char * dump_syntax_tree_str(stnode_t *root)
Dumps a syntax tree to a string.
Definition syntax-tree.c:584
struct stnode stnode_t
A single node instance in the display filter syntax tree.
stnode_op_t
Operator types for test and arithmetic syntax tree nodes.
Definition syntax-tree.h:129
@ STNODE_OP_ALL_NE
Definition syntax-tree.h:136
@ STNODE_OP_MULTIPLY
Definition syntax-tree.h:150
@ STNODE_OP_UNARY_MINUS
Definition syntax-tree.h:147
@ STNODE_OP_SUBTRACT
Definition syntax-tree.h:149
@ STNODE_OP_OR
Definition syntax-tree.h:133
@ STNODE_OP_GT
Definition syntax-tree.h:138
@ STNODE_OP_BITWISE_AND
Definition syntax-tree.h:146
@ STNODE_OP_DIVIDE
Definition syntax-tree.h:151
@ STNODE_OP_LT
Definition syntax-tree.h:140
@ STNODE_OP_NOT
Definition syntax-tree.h:131
@ STNODE_OP_AND
Definition syntax-tree.h:132
@ STNODE_OP_MATCHES
Definition syntax-tree.h:143
@ STNODE_OP_MODULO
Definition syntax-tree.h:152
@ STNODE_OP_CONTAINS
Definition syntax-tree.h:142
@ STNODE_OP_LE
Definition syntax-tree.h:141
@ STNODE_OP_IN
Definition syntax-tree.h:144
@ STNODE_OP_ANY_NE
Definition syntax-tree.h:137
@ STNODE_OP_UNINITIALIZED
Definition syntax-tree.h:130
@ STNODE_OP_GE
Definition syntax-tree.h:139
@ STNODE_OP_ADD
Definition syntax-tree.h:148
@ STNODE_OP_NOT_IN
Definition syntax-tree.h:145
@ STNODE_OP_ANY_EQ
Definition syntax-tree.h:135
@ STNODE_OP_ALL_EQ
Definition syntax-tree.h:134
WS_DLL_PUBLIC stnode_t * stnode_new(sttype_id_t type_id, void *data, char *token, df_loc_t loc)
Creates a new syntax tree node with the given type ID, data, token, and location.
Definition syntax-tree.c:257
WS_DLL_PUBLIC const char * stnode_op_name(const stnode_op_t op)
Get the name of an operation based on its type.
Definition syntax-tree.c:101
void(* STTypeFreeFunc)(void *data)
Releases type-specific data associated with a syntax tree node.
Definition syntax-tree.h:74
WS_DLL_PUBLIC void * stnode_steal_data(stnode_t *node)
Steals and returns the data associated with a syntax tree node.
Definition syntax-tree.c:330