Mon, 19 Mar 2018 16:36:14 +0100
regex parser was not properly reset before each file, sometimes resulting in wrong line counts, when the previous scanned file ended with a match
universe@27 | 1 | /* |
universe@34 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@48 | 3 | * Copyright 2017 Mike Becker. All rights reserved. |
universe@34 | 4 | * |
universe@34 | 5 | * Redistribution and use in source and binary forms, with or without |
universe@34 | 6 | * modification, are permitted provided that the following conditions are met: |
universe@34 | 7 | * |
universe@34 | 8 | * 1. Redistributions of source code must retain the above copyright |
universe@34 | 9 | * notice, this list of conditions and the following disclaimer. |
universe@34 | 10 | * |
universe@34 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@34 | 12 | * notice, this list of conditions and the following disclaimer in the |
universe@34 | 13 | * documentation and/or other materials provided with the distribution. |
universe@34 | 14 | * |
universe@34 | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@34 | 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@34 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
universe@34 | 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
universe@34 | 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
universe@34 | 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
universe@34 | 21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
universe@34 | 22 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
universe@34 | 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
universe@34 | 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
universe@34 | 25 | * |
universe@27 | 26 | * regex_parser.c |
universe@27 | 27 | * |
universe@27 | 28 | * Created on: 26.01.2012 |
universe@29 | 29 | * Author: Mike |
universe@27 | 30 | */ |
universe@27 | 31 | |
universe@27 | 32 | #include "regex_parser.h" |
universe@27 | 33 | |
universe@27 | 34 | regex_parser_t* new_regex_parser_t() { |
universe@27 | 35 | regex_parser_t* ret = malloc(sizeof(regex_parser_t)); |
universe@27 | 36 | if (ret != NULL) { |
universe@27 | 37 | ret->pattern_list = new_string_list_t(); |
universe@27 | 38 | ret->matched_lines = 0; |
universe@27 | 39 | ret->pattern_match = 0; |
universe@27 | 40 | ret->compiled_patterns = NULL; |
universe@28 | 41 | ret->compiled_pattern_count = 0; |
universe@27 | 42 | } |
universe@27 | 43 | return ret; |
universe@27 | 44 | } |
universe@27 | 45 | |
universe@54 | 46 | void regex_parser_reset(regex_parser_t* parser) { |
universe@54 | 47 | parser->pattern_match = parser->matched_lines = 0; |
universe@54 | 48 | } |
universe@54 | 49 | |
universe@28 | 50 | void regex_destcomppats(regex_parser_t* parser) { |
universe@28 | 51 | if (parser->compiled_patterns != NULL) { |
universe@28 | 52 | for (int i = 0 ; i < parser->compiled_pattern_count ; i++) { |
universe@28 | 53 | if (parser->compiled_patterns[i] != NULL) { |
universe@28 | 54 | free(parser->compiled_patterns[i]); |
universe@28 | 55 | } |
universe@28 | 56 | } |
universe@28 | 57 | free(parser->compiled_patterns); |
universe@28 | 58 | parser->compiled_patterns = NULL; |
universe@28 | 59 | parser->compiled_pattern_count = 0; |
universe@28 | 60 | } |
universe@28 | 61 | } |
universe@28 | 62 | |
universe@27 | 63 | void destroy_regex_parser_t(regex_parser_t* parser) { |
universe@28 | 64 | regex_destcomppats(parser); |
universe@27 | 65 | destroy_string_list_t(parser->pattern_list); |
universe@27 | 66 | free(parser); |
universe@27 | 67 | } |
universe@27 | 68 | |
universe@27 | 69 | bool regex_parser_matching(regex_parser_t* parser) { |
universe@27 | 70 | return parser->pattern_match > 0; |
universe@27 | 71 | } |
universe@27 | 72 | |
universe@28 | 73 | int regex_parser_do(regex_parser_t* parser, char* input) { |
universe@28 | 74 | int err = REG_NOMATCH; |
universe@28 | 75 | if (parser->compiled_pattern_count > 0) { |
universe@28 | 76 | regmatch_t match; |
universe@27 | 77 | |
universe@28 | 78 | if (regex_parser_matching(parser)) { |
universe@28 | 79 | parser->matched_lines++; |
universe@28 | 80 | |
universe@28 | 81 | err = regexec(parser->compiled_patterns[parser->pattern_match], |
universe@28 | 82 | input, 1, &match, 0); |
universe@28 | 83 | if (err > 0 && err != REG_NOMATCH) { |
universe@28 | 84 | fprintf(stderr, "Regex-Error: 0x%08x", err); |
universe@28 | 85 | } |
universe@28 | 86 | if (err == 0) { |
universe@28 | 87 | parser->pattern_match = 0; |
universe@28 | 88 | /* do not match line, if it does not end with the pattern */ |
universe@28 | 89 | if (match.rm_eo < strlen(input)) { |
universe@28 | 90 | parser->matched_lines--; |
universe@28 | 91 | } |
universe@28 | 92 | } |
universe@28 | 93 | } else { |
universe@28 | 94 | for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) { |
universe@28 | 95 | err = regexec(parser->compiled_patterns[i], input, 1, &match, 0); |
universe@28 | 96 | if (err > 0 && err != REG_NOMATCH) { |
universe@28 | 97 | fprintf(stderr, "Regex-Error: 0x%08x", err); |
universe@28 | 98 | } |
universe@28 | 99 | if (err == 0) { |
universe@28 | 100 | parser->pattern_match = i+1; |
universe@28 | 101 | parser->matched_lines = 0; |
universe@28 | 102 | /* Check, if end pattern is also in this line */ |
universe@28 | 103 | regex_parser_do(parser, input); |
universe@28 | 104 | /* do not match line, if it does not start with the pattern */ |
universe@28 | 105 | if (match.rm_so > 0 && parser->matched_lines > 0) { |
universe@28 | 106 | parser->matched_lines--; |
universe@28 | 107 | } |
universe@28 | 108 | break; |
universe@28 | 109 | } |
universe@27 | 110 | } |
universe@27 | 111 | } |
universe@27 | 112 | } |
universe@28 | 113 | return err; |
universe@27 | 114 | } |
universe@28 | 115 | |
universe@28 | 116 | bool regex_compile_all(regex_parser_t* parser) { |
universe@28 | 117 | bool success = true; |
universe@28 | 118 | size_t pcount = parser->pattern_list->count; |
universe@28 | 119 | if (pcount > 0) { |
universe@28 | 120 | regex_destcomppats(parser); |
universe@28 | 121 | parser->compiled_patterns = calloc(pcount, sizeof(regex_t)); |
universe@28 | 122 | parser->compiled_pattern_count = pcount; |
universe@28 | 123 | |
universe@28 | 124 | regex_t* re; |
universe@28 | 125 | for (int i = 0 ; i < pcount ; i++) { |
universe@28 | 126 | re = malloc(sizeof(regex_t)); |
universe@28 | 127 | if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) { |
universe@28 | 128 | parser->compiled_patterns[i] = re; |
universe@28 | 129 | } else { |
universe@28 | 130 | fprintf(stderr, "Cannot compile pattern: %s\n", |
universe@28 | 131 | (parser->pattern_list->items[i])); |
universe@28 | 132 | parser->compiled_patterns[i] = NULL; |
universe@28 | 133 | success = false; |
universe@28 | 134 | } |
universe@28 | 135 | } |
universe@28 | 136 | } |
universe@28 | 137 | return success; |
universe@28 | 138 | } |