1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #ifndef ZABBIX_JSONPATH_H
21 #define ZABBIX_JSONPATH_H
22 
23 #include "zbxalgo.h"
24 
25 typedef enum
26 {
27 	ZBX_JSONPATH_SEGMENT_UNKNOWN,
28 	ZBX_JSONPATH_SEGMENT_MATCH_ALL,
29 	ZBX_JSONPATH_SEGMENT_MATCH_LIST,
30 	ZBX_JSONPATH_SEGMENT_MATCH_RANGE,
31 	ZBX_JSONPATH_SEGMENT_MATCH_EXPRESSION,
32 	ZBX_JSONPATH_SEGMENT_FUNCTION
33 }
34 zbx_jsonpath_segment_type_t;
35 
36 /* specifies if the match list contains object property names or array indices */
37 typedef enum
38 {
39 	ZBX_JSONPATH_LIST_NAME = 1,
40 	ZBX_JSONPATH_LIST_INDEX
41 }
42 zbx_jsonpath_list_type_t;
43 
44 typedef enum
45 {
46 	ZBX_JSONPATH_FUNCTION_MIN = 1,
47 	ZBX_JSONPATH_FUNCTION_MAX,
48 	ZBX_JSONPATH_FUNCTION_AVG,
49 	ZBX_JSONPATH_FUNCTION_LENGTH,
50 	ZBX_JSONPATH_FUNCTION_FIRST,
51 	ZBX_JSONPATH_FUNCTION_SUM,
52 	/* the element name suffix '~' internally is treated as a function */
53 	ZBX_JSONPATH_FUNCTION_NAME
54 }
55 zbx_jsonpath_function_type_t;
56 
57 typedef struct zbx_jsonpath_list_item
58 {
59 	struct zbx_jsonpath_list_item	*next;
60 	/* the structure is always over-allocated so that either int */
61 	/* or a zero terminated string can be stored in data         */
62 	char				data[1];
63 }
64 zbx_jsonpath_list_node_t;
65 
66 typedef struct
67 {
68 	zbx_jsonpath_list_node_t	*values;
69 	zbx_jsonpath_list_type_t	type;
70 }
71 zbx_jsonpath_list_t;
72 
73 typedef struct
74 {
75 	int		start;
76 	int		end;
77 	unsigned int	flags;
78 }
79 zbx_jsonpath_range_t;
80 
81 /* expression tokens in postfix notation */
82 typedef struct
83 {
84 	zbx_vector_ptr_t	tokens;
85 }
86 zbx_jsonpath_expression_t;
87 
88 typedef struct
89 {
90 	zbx_jsonpath_function_type_t	type;
91 }
92 zbx_jsonpath_function_t;
93 
94 typedef union
95 {
96 	zbx_jsonpath_list_t		list;
97 	zbx_jsonpath_range_t		range;
98 	zbx_jsonpath_expression_t	expression;
99 	zbx_jsonpath_function_t		function;
100 }
101 zbx_jsonpath_data_t;
102 
103 struct zbx_jsonpath_segment
104 {
105 	zbx_jsonpath_segment_type_t	type;
106 	zbx_jsonpath_data_t		data;
107 
108 	/* set to 1 if the segment is 'detached' and can be anywhere in parent node tree */
109 	unsigned char			detached;
110 };
111 
112 /*                                                                            */
113 /* Token groups:                                                              */
114 /*   operand - constant value, jsonpath reference, result of () evaluation    */
115 /*   operator2 - binary operator (arithmetic or comparison)                   */
116 /*   operator1 - unary operator (negation !)                                  */
117 /*                                                                            */
118 typedef enum
119 {
120 	ZBX_JSONPATH_TOKEN_GROUP_NONE,
121 	ZBX_JSONPATH_TOKEN_GROUP_OPERAND,
122 	ZBX_JSONPATH_TOKEN_GROUP_OPERATOR2,	/* binary operator */
123 	ZBX_JSONPATH_TOKEN_GROUP_OPERATOR1	/* unary operator */
124 }
125 zbx_jsonpath_token_group_t;
126 
127 /* expression token types */
128 typedef enum
129 {
130 	ZBX_JSONPATH_TOKEN_PATH_ABSOLUTE = 1,
131 	ZBX_JSONPATH_TOKEN_PATH_RELATIVE,
132 	ZBX_JSONPATH_TOKEN_CONST_STR,
133 	ZBX_JSONPATH_TOKEN_CONST_NUM,
134 	ZBX_JSONPATH_TOKEN_PAREN_LEFT,
135 	ZBX_JSONPATH_TOKEN_PAREN_RIGHT,
136 	ZBX_JSONPATH_TOKEN_OP_PLUS,
137 	ZBX_JSONPATH_TOKEN_OP_MINUS,
138 	ZBX_JSONPATH_TOKEN_OP_MULT,
139 	ZBX_JSONPATH_TOKEN_OP_DIV,
140 	ZBX_JSONPATH_TOKEN_OP_EQ,
141 	ZBX_JSONPATH_TOKEN_OP_NE,
142 	ZBX_JSONPATH_TOKEN_OP_GT,
143 	ZBX_JSONPATH_TOKEN_OP_GE,
144 	ZBX_JSONPATH_TOKEN_OP_LT,
145 	ZBX_JSONPATH_TOKEN_OP_LE,
146 	ZBX_JSONPATH_TOKEN_OP_NOT,
147 	ZBX_JSONPATH_TOKEN_OP_AND,
148 	ZBX_JSONPATH_TOKEN_OP_OR,
149 	ZBX_JSONPATH_TOKEN_OP_REGEXP
150 }
151 zbx_jsonpath_token_type_t;
152 
153 typedef struct
154 {
155 	unsigned char	type;
156 	char		*data;
157 }
158 zbx_jsonpath_token_t;
159 
160 
161 #endif
162