| 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 | * SPDX-License-Identifier: LGPL-2.1-only |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <unistd.h> |
| 14 | #include <string.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <assert.h> |
| 17 | #include <errno.h> |
| 18 | #include <inttypes.h> |
| 19 | #include "filter-ast.h" |
| 20 | #include "filter-parser.h" |
| 21 | |
| 22 | #include <common/macros.h> |
| 23 | |
| 24 | #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args) |
| 25 | |
| 26 | static |
| 27 | int recursive_visit_print(struct filter_node *node, FILE *stream, int indent); |
| 28 | |
| 29 | static |
| 30 | void print_tabs(FILE *fd, int depth) |
| 31 | { |
| 32 | int i; |
| 33 | |
| 34 | for (i = 0; i < depth; i++) |
| 35 | fprintf(fd, "\t"); |
| 36 | } |
| 37 | |
| 38 | static |
| 39 | int recursive_visit_print_expression(struct filter_node *node, |
| 40 | FILE *stream, int indent) |
| 41 | { |
| 42 | struct filter_node *iter_node; |
| 43 | |
| 44 | if (!node) { |
| 45 | fprintf(stderr, "[error] %s: NULL child\n", __func__); |
| 46 | return -EINVAL; |
| 47 | } |
| 48 | switch (node->u.expression.type) { |
| 49 | case AST_EXP_UNKNOWN: |
| 50 | default: |
| 51 | fprintf(stderr, "[error] %s: unknown expression\n", __func__); |
| 52 | return -EINVAL; |
| 53 | case AST_EXP_STRING: |
| 54 | print_tabs(stream, indent); |
| 55 | fprintf(stream, "<string value=\"%s\"/>\n", |
| 56 | node->u.expression.u.string); |
| 57 | break; |
| 58 | case AST_EXP_CONSTANT: |
| 59 | print_tabs(stream, indent); |
| 60 | fprintf(stream, "<constant value=\"%" PRIu64 "\"/>\n", |
| 61 | node->u.expression.u.constant); |
| 62 | break; |
| 63 | case AST_EXP_FLOAT_CONSTANT: |
| 64 | print_tabs(stream, indent); |
| 65 | fprintf(stream, "<float_constant value=\"%lg\"/>\n", |
| 66 | node->u.expression.u.float_constant); |
| 67 | break; |
| 68 | case AST_EXP_IDENTIFIER: /* fall-through */ |
| 69 | case AST_EXP_GLOBAL_IDENTIFIER: |
| 70 | print_tabs(stream, indent); |
| 71 | fprintf(stream, "<%s value=\"%s\"/>\n", |
| 72 | node->u.expression.type == AST_EXP_IDENTIFIER ? |
| 73 | "identifier" : "global_identifier", |
| 74 | node->u.expression.u.identifier); |
| 75 | iter_node = node->u.expression.next; |
| 76 | while (iter_node) { |
| 77 | print_tabs(stream, indent); |
| 78 | fprintf(stream, "<bracket>\n"); |
| 79 | if (recursive_visit_print_expression(iter_node, |
| 80 | stream, indent + 1)) { |
| 81 | return -EINVAL; |
| 82 | } |
| 83 | print_tabs(stream, indent); |
| 84 | fprintf(stream, "</bracket>\n"); |
| 85 | iter_node = iter_node->u.expression.next; |
| 86 | |
| 87 | } |
| 88 | break; |
| 89 | case AST_EXP_NESTED: |
| 90 | return recursive_visit_print(node->u.expression.u.child, |
| 91 | stream, indent + 1); |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static |
| 98 | int recursive_visit_print(struct filter_node *node, FILE *stream, int indent) |
| 99 | { |
| 100 | int ret; |
| 101 | |
| 102 | if (!node) { |
| 103 | fprintf(stderr, "[error] %s: NULL child\n", __func__); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | switch (node->type) { |
| 107 | case NODE_UNKNOWN: |
| 108 | default: |
| 109 | fprintf(stderr, "[error] %s: unknown node type\n", __func__); |
| 110 | return -EINVAL; |
| 111 | case NODE_ROOT: |
| 112 | print_tabs(stream, indent); |
| 113 | fprintf(stream, "<root>\n"); |
| 114 | ret = recursive_visit_print(node->u.root.child, stream, |
| 115 | indent + 1); |
| 116 | print_tabs(stream, indent); |
| 117 | fprintf(stream, "</root>\n"); |
| 118 | return ret; |
| 119 | case NODE_EXPRESSION: |
| 120 | print_tabs(stream, indent); |
| 121 | fprintf(stream, "<expression>\n"); |
| 122 | ret = recursive_visit_print_expression(node, stream, |
| 123 | indent + 1); |
| 124 | print_tabs(stream, indent); |
| 125 | fprintf(stream, "</expression>\n"); |
| 126 | return ret; |
| 127 | case NODE_OP: |
| 128 | print_tabs(stream, indent); |
| 129 | fprintf(stream, "<op type="); |
| 130 | switch (node->u.op.type) { |
| 131 | case AST_OP_UNKNOWN: |
| 132 | default: |
| 133 | fprintf(stderr, "[error] %s: unknown op\n", __func__); |
| 134 | return -EINVAL; |
| 135 | case AST_OP_MUL: |
| 136 | fprintf(stream, "\"*\""); |
| 137 | break; |
| 138 | case AST_OP_DIV: |
| 139 | fprintf(stream, "\"/\""); |
| 140 | break; |
| 141 | case AST_OP_MOD: |
| 142 | fprintf(stream, "\"%%\""); |
| 143 | break; |
| 144 | case AST_OP_PLUS: |
| 145 | fprintf(stream, "\"+\""); |
| 146 | break; |
| 147 | case AST_OP_MINUS: |
| 148 | fprintf(stream, "\"-\""); |
| 149 | break; |
| 150 | case AST_OP_BIT_RSHIFT: |
| 151 | fprintf(stream, "\">>\""); |
| 152 | break; |
| 153 | case AST_OP_BIT_LSHIFT: |
| 154 | fprintf(stream, "\"<<\""); |
| 155 | break; |
| 156 | case AST_OP_AND: |
| 157 | fprintf(stream, "\"&&\""); |
| 158 | break; |
| 159 | case AST_OP_OR: |
| 160 | fprintf(stream, "\"||\""); |
| 161 | break; |
| 162 | case AST_OP_BIT_AND: |
| 163 | fprintf(stream, "\"&\""); |
| 164 | break; |
| 165 | case AST_OP_BIT_OR: |
| 166 | fprintf(stream, "\"|\""); |
| 167 | break; |
| 168 | case AST_OP_BIT_XOR: |
| 169 | fprintf(stream, "\"^\""); |
| 170 | break; |
| 171 | |
| 172 | case AST_OP_EQ: |
| 173 | fprintf(stream, "\"==\""); |
| 174 | break; |
| 175 | case AST_OP_NE: |
| 176 | fprintf(stream, "\"!=\""); |
| 177 | break; |
| 178 | case AST_OP_GT: |
| 179 | fprintf(stream, "\">\""); |
| 180 | break; |
| 181 | case AST_OP_LT: |
| 182 | fprintf(stream, "\"<\""); |
| 183 | break; |
| 184 | case AST_OP_GE: |
| 185 | fprintf(stream, "\">=\""); |
| 186 | break; |
| 187 | case AST_OP_LE: |
| 188 | fprintf(stream, "\"<=\""); |
| 189 | break; |
| 190 | } |
| 191 | fprintf(stream, ">\n"); |
| 192 | ret = recursive_visit_print(node->u.op.lchild, |
| 193 | stream, indent + 1); |
| 194 | if (ret) |
| 195 | return ret; |
| 196 | ret = recursive_visit_print(node->u.op.rchild, |
| 197 | stream, indent + 1); |
| 198 | if (ret) |
| 199 | return ret; |
| 200 | print_tabs(stream, indent); |
| 201 | fprintf(stream, "</op>\n"); |
| 202 | return ret; |
| 203 | case NODE_UNARY_OP: |
| 204 | print_tabs(stream, indent); |
| 205 | fprintf(stream, "<unary_op type="); |
| 206 | switch (node->u.unary_op.type) { |
| 207 | case AST_UNARY_UNKNOWN: |
| 208 | default: |
| 209 | fprintf(stderr, "[error] %s: unknown unary_op\n", __func__); |
| 210 | return -EINVAL; |
| 211 | case AST_UNARY_PLUS: |
| 212 | fprintf(stream, "\"+\""); |
| 213 | break; |
| 214 | case AST_UNARY_MINUS: |
| 215 | fprintf(stream, "\"-\""); |
| 216 | break; |
| 217 | case AST_UNARY_NOT: |
| 218 | fprintf(stream, "\"!\""); |
| 219 | break; |
| 220 | case AST_UNARY_BIT_NOT: |
| 221 | fprintf(stream, "\"~\""); |
| 222 | break; |
| 223 | } |
| 224 | fprintf(stream, ">\n"); |
| 225 | ret = recursive_visit_print(node->u.unary_op.child, |
| 226 | stream, indent + 1); |
| 227 | print_tabs(stream, indent); |
| 228 | fprintf(stream, "</unary_op>\n"); |
| 229 | return ret; |
| 230 | } |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | LTTNG_HIDDEN |
| 235 | int filter_visitor_print_xml(struct filter_parser_ctx *ctx, FILE *stream, |
| 236 | int indent) |
| 237 | { |
| 238 | return recursive_visit_print(&ctx->ast->root, stream, indent); |
| 239 | } |