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