Cleanup: use LTTNG_HIDDEN in lttng-ctl filter lib
[lttng-tools.git] / src / lib / lttng-ctl / filter / filter-visitor-xml.c
CommitLineData
953192ba
MD
1/*
2 * filter-visitor-xml.c
3 *
4 * LTTng filter XML pretty printer visitor
5 *
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <unistd.h>
24#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
27#include <errno.h>
28#include <inttypes.h>
953192ba 29#include "filter-ast.h"
95b9bd90 30#include "filter-parser.h"
953192ba 31
a187da1a
DG
32#include <common/macros.h>
33
953192ba
MD
34#define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
35
36static
37int recursive_visit_print(struct filter_node *node, FILE *stream, int indent);
38
39static
40void print_tabs(FILE *fd, int depth)
41{
42 int i;
43
44 for (i = 0; i < depth; i++)
45 fprintf(fd, "\t");
46}
47
48static
49int recursive_visit_print_expression(struct filter_node *node,
50 FILE *stream, int indent)
51{
52 if (!node) {
53 fprintf(stderr, "[error] %s: NULL child\n", __func__);
54 return -EINVAL;
55 }
56 switch (node->u.expression.type) {
57 case AST_EXP_UNKNOWN:
58 default:
59 fprintf(stderr, "[error] %s: unknown expression\n", __func__);
60 return -EINVAL;
61 case AST_EXP_STRING:
62 print_tabs(stream, indent);
63 fprintf(stream, "<string value=\"%s\"/>\n",
64 node->u.expression.u.string);
65 break;
66 case AST_EXP_CONSTANT:
67 print_tabs(stream, indent);
68 fprintf(stream, "<constant value=\"%" PRIu64 "\"/>\n",
69 node->u.expression.u.constant);
70 break;
e90d8561
MD
71 case AST_EXP_FLOAT_CONSTANT:
72 print_tabs(stream, indent);
73 fprintf(stream, "<float_constant value=\"%lg\"/>\n",
74 node->u.expression.u.float_constant);
75 break;
953192ba
MD
76 case AST_EXP_IDENTIFIER:
77 print_tabs(stream, indent);
78 fprintf(stream, "<identifier value=\"%s\"/>\n",
79 node->u.expression.u.identifier);
80 while (node->u.expression.next) {
81 print_tabs(stream, indent);
82 fprintf(stream, "<link type=\"");
83 switch (node->u.expression.pre_op) {
84 case AST_LINK_UNKNOWN:
85 default:
86 fprintf(stderr, "[error] %s: unknown link\n", __func__);
87 return -EINVAL;
88 case AST_LINK_DOT:
89 fprintf(stream, ".");
90 break;
91 case AST_LINK_RARROW:
92 fprintf(stream, "->");
93 break;
94 }
95 fprintf(stream, "\"/>\n");
96
97 node = node->u.expression.next;
98 if (node->type != NODE_EXPRESSION ||
99 node->u.expression.type != AST_EXP_IDENTIFIER) {
100 fprintf(stderr, "[error] %s: expecting identifier before link\n", __func__);
101 return -EINVAL;
102 }
103
104 print_tabs(stream, indent);
105 fprintf(stream, "<identifier value=\"%s\"/>\n",
106 node->u.expression.u.identifier);
107 }
108 break;
109 case AST_EXP_NESTED:
110 return recursive_visit_print(node->u.expression.u.child,
111 stream, indent + 1);
112 }
113 return 0;
114}
115
116
117static
118int recursive_visit_print(struct filter_node *node, FILE *stream, int indent)
119{
120 int ret;
121
122 if (!node) {
123 fprintf(stderr, "[error] %s: NULL child\n", __func__);
124 return -EINVAL;
125 }
126 switch (node->type) {
127 case NODE_UNKNOWN:
128 default:
129 fprintf(stderr, "[error] %s: unknown node type\n", __func__);
130 return -EINVAL;
131 case NODE_ROOT:
132 print_tabs(stream, indent);
133 fprintf(stream, "<root>\n");
134 ret = recursive_visit_print(node->u.root.child, stream,
135 indent + 1);
136 print_tabs(stream, indent);
137 fprintf(stream, "</root>\n");
138 return ret;
139 case NODE_EXPRESSION:
140 print_tabs(stream, indent);
141 fprintf(stream, "<expression>\n");
142 ret = recursive_visit_print_expression(node, stream,
143 indent + 1);
144 print_tabs(stream, indent);
145 fprintf(stream, "</expression>\n");
146 return ret;
147 case NODE_OP:
148 print_tabs(stream, indent);
149 fprintf(stream, "<op type=");
150 switch (node->u.op.type) {
151 case AST_OP_UNKNOWN:
152 default:
153 fprintf(stderr, "[error] %s: unknown op\n", __func__);
154 return -EINVAL;
155 case AST_OP_MUL:
156 fprintf(stream, "\"*\"");
157 break;
158 case AST_OP_DIV:
159 fprintf(stream, "\"/\"");
160 break;
161 case AST_OP_MOD:
162 fprintf(stream, "\"%%\"");
163 break;
164 case AST_OP_PLUS:
165 fprintf(stream, "\"+\"");
166 break;
167 case AST_OP_MINUS:
168 fprintf(stream, "\"-\"");
169 break;
170 case AST_OP_RSHIFT:
171 fprintf(stream, "\">>\"");
172 break;
173 case AST_OP_LSHIFT:
174 fprintf(stream, "\"<<\"");
175 break;
176 case AST_OP_AND:
177 fprintf(stream, "\"&&\"");
178 break;
179 case AST_OP_OR:
180 fprintf(stream, "\"||\"");
181 break;
182 case AST_OP_BIN_AND:
183 fprintf(stream, "\"&\"");
184 break;
185 case AST_OP_BIN_OR:
186 fprintf(stream, "\"|\"");
187 break;
188 case AST_OP_BIN_XOR:
189 fprintf(stream, "\"^\"");
190 break;
191
192 case AST_OP_EQ:
193 fprintf(stream, "\"==\"");
194 break;
195 case AST_OP_NE:
196 fprintf(stream, "\"!=\"");
197 break;
198 case AST_OP_GT:
199 fprintf(stream, "\">\"");
200 break;
201 case AST_OP_LT:
202 fprintf(stream, "\"<\"");
203 break;
204 case AST_OP_GE:
205 fprintf(stream, "\">=\"");
206 break;
207 case AST_OP_LE:
208 fprintf(stream, "\"<=\"");
209 break;
210 }
211 fprintf(stream, ">\n");
212 ret = recursive_visit_print(node->u.op.lchild,
213 stream, indent + 1);
214 if (ret)
215 return ret;
216 ret = recursive_visit_print(node->u.op.rchild,
217 stream, indent + 1);
218 if (ret)
219 return ret;
220 print_tabs(stream, indent);
221 fprintf(stream, "</op>\n");
222 return ret;
223 case NODE_UNARY_OP:
224 print_tabs(stream, indent);
225 fprintf(stream, "<unary_op type=");
226 switch (node->u.unary_op.type) {
227 case AST_UNARY_UNKNOWN:
228 default:
229 fprintf(stderr, "[error] %s: unknown unary_op\n", __func__);
230 return -EINVAL;
231 case AST_UNARY_PLUS:
232 fprintf(stream, "\"+\"");
233 break;
234 case AST_UNARY_MINUS:
235 fprintf(stream, "\"-\"");
236 break;
237 case AST_UNARY_NOT:
238 fprintf(stream, "\"!\"");
239 break;
d804b36b
CB
240 case AST_UNARY_BIN_NOT:
241 fprintf(stream, "\"~\"");
242 break;
953192ba
MD
243 }
244 fprintf(stream, ">\n");
245 ret = recursive_visit_print(node->u.unary_op.child,
246 stream, indent + 1);
247 print_tabs(stream, indent);
248 fprintf(stream, "</unary_op>\n");
249 return ret;
250 }
251 return 0;
252}
253
a187da1a 254LTTNG_HIDDEN
953192ba
MD
255int filter_visitor_print_xml(struct filter_parser_ctx *ctx, FILE *stream,
256 int indent)
257{
258 return recursive_visit_print(&ctx->ast->root, stream, indent);
259}
This page took 0.033809 seconds and 4 git commands to generate.