| 1 | /* |
| 2 | * filter-visitor-ir-normalize-glob-patterns.c |
| 3 | * |
| 4 | * LTTng filter IR normalize string |
| 5 | * |
| 6 | * Copyright 2017 - Philippe Proulx <pproulx@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> |
| 29 | |
| 30 | #include <common/macros.h> |
| 31 | #include <common/string-utils/string-utils.h> |
| 32 | |
| 33 | #include "filter-ast.h" |
| 34 | #include "filter-parser.h" |
| 35 | #include "filter-ir.h" |
| 36 | |
| 37 | static |
| 38 | int normalize_glob_patterns(struct ir_op *node) |
| 39 | { |
| 40 | switch (node->op) { |
| 41 | case IR_OP_UNKNOWN: |
| 42 | default: |
| 43 | fprintf(stderr, "[error] %s: unknown op type\n", __func__); |
| 44 | return -EINVAL; |
| 45 | |
| 46 | case IR_OP_ROOT: |
| 47 | return normalize_glob_patterns(node->u.root.child); |
| 48 | case IR_OP_LOAD: |
| 49 | { |
| 50 | if (node->data_type == IR_DATA_STRING) { |
| 51 | enum ir_load_string_type type = |
| 52 | node->u.load.u.string.type; |
| 53 | if (type == IR_LOAD_STRING_TYPE_GLOB_STAR_END || |
| 54 | type == IR_LOAD_STRING_TYPE_GLOB_STAR) { |
| 55 | assert(node->u.load.u.string.value); |
| 56 | strutils_normalize_star_glob_pattern( |
| 57 | node->u.load.u.string.value); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | case IR_OP_UNARY: |
| 64 | return normalize_glob_patterns(node->u.unary.child); |
| 65 | case IR_OP_BINARY: |
| 66 | { |
| 67 | int ret = normalize_glob_patterns(node->u.binary.left); |
| 68 | |
| 69 | if (ret) |
| 70 | return ret; |
| 71 | return normalize_glob_patterns(node->u.binary.right); |
| 72 | } |
| 73 | case IR_OP_LOGICAL: |
| 74 | { |
| 75 | int ret; |
| 76 | |
| 77 | ret = normalize_glob_patterns(node->u.logical.left); |
| 78 | if (ret) |
| 79 | return ret; |
| 80 | return normalize_glob_patterns(node->u.logical.right); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * This function normalizes all the globbing literal strings with |
| 87 | * utils_normalize_glob_pattern(). See the documentation of |
| 88 | * utils_normalize_glob_pattern() for more details. |
| 89 | */ |
| 90 | LTTNG_HIDDEN |
| 91 | int filter_visitor_ir_normalize_glob_patterns(struct filter_parser_ctx *ctx) |
| 92 | { |
| 93 | return normalize_glob_patterns(ctx->ir_root); |
| 94 | } |