This repository was archived by the owner on Dec 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.c
242 lines (218 loc) · 5.61 KB
/
rule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* rule.c - The Rule structure.
*
* Copyright (C) 2001-2005 Oskar Liljeblad
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
/* C89 */
#include <stdlib.h>
#include <string.h>
/* gnulib */
#include <regex.h>
#include <xalloc.h>
/* common */
#include "common/regex-utils.h"
#include "common/error.h"
/* regex-markup */
#include "remark.h"
static void *
new_rule(RuleType type, size_t size)
{
Rule *rule = xmalloc(size);
rule->type = type;
return rule;
}
void
dump_rule(Rule *anyrule, int indent)
{
int c;
for (c=0;c<indent;c++)printf(" ");
if (anyrule->type == RULE_MATCH) {
MatchRule *rule = (MatchRule *) anyrule;
printf("match rule\n");
dump_rule(rule->rule, indent+1);
}
else if (anyrule->type == RULE_MACRO) {
MacroRule *rule = (MacroRule *) anyrule;
printf("macro rule\n");
dump_rule(rule->macro->rule, indent+1);
}
else if (anyrule->type == RULE_STYLE) {
StyleRule *rule = (StyleRule *) anyrule;
printf("style rule. style=%s\n", rule->style->name);
}
else if (anyrule->type == RULE_MULTI) {
MultiRule *rule = (MultiRule *) anyrule;
printf("multi rule\n");
for (c = 0; c < rule->rule_count; c++)
dump_rule(rule->rules[c], indent+1);
}
else if (anyrule->type == RULE_ACTION) {
ActionRule *rule = (ActionRule *) anyrule;
printf("action rule. action=");
switch (rule->action) {
case ACTION_SKIP: puts("skip"); break;
case ACTION_CONTINUE: puts("continue"); break;
case ACTION_BREAK: puts("break"); break;
}
}
else if (anyrule->type == RULE_SUBSTITUTION) {
SubstitutionRule *rule = (SubstitutionRule *) anyrule;
printf("substitution rule. repl=%s\n", rule->replacement);
}
else if (anyrule->type == RULE_SET) {
SetRule *rule = (SetRule *) anyrule;
printf("set rule. repl=%s\n", rule->replacement);
}
}
void
free_rule(Rule *anyrule)
{
int c;
if (anyrule == NULL) {
return;
}
else if (anyrule->type == RULE_MATCH) {
MatchRule *rule = (MatchRule *) anyrule;
for (c = 0; c < rule->match_count; c++)
free_match(rule->matches[c]);
free(rule->matches);
}
else if (anyrule->type == RULE_MACRO) {
MacroRule *rule = (MacroRule *) anyrule;
free_macro(rule->macro);
}
else if (anyrule->type == RULE_STYLE) {
StyleRule *rule = (StyleRule *) anyrule;
free_style(rule->style);
}
else if (anyrule->type == RULE_MULTI) {
MultiRule *rule = (MultiRule *) anyrule;
for (c = 0; c < rule->rule_count; c++)
free_rule(rule->rules[c]);
free(rule->rules);
}
else if (anyrule->type == RULE_ACTION) {
/* no operation */
}
else if (anyrule->type == RULE_SUBSTITUTION) {
SubstitutionRule *rule = (SubstitutionRule *) anyrule;
regfree(&rule->regex);
free(rule->replacement);
}
else if (anyrule->type == RULE_SET) {
SetRule *rule = (SetRule *) anyrule;
free(rule->replacement);
}
free(anyrule);
}
Macro *
new_macro(const char *name, Rule *rule)
{
Macro *macro = xmalloc(sizeof(Macro));
macro->name = xstrdup(name);
macro->rule = rule;
macro->refs = 1;
return macro;
}
void
free_macro(Macro *macro)
{
if (--macro->refs <= 0) {
free(macro->name);
free_rule(macro->rule);
free(macro);
}
}
Rule *
new_macro_rule(Macro *macro)
{
MacroRule *rule = new_rule(RULE_MACRO, sizeof(MacroRule));
rule->macro = macro;
macro->refs++;
return (Rule *) rule;
}
Rule *
new_style_rule(Style *style)
{
StyleRule *rule = new_rule(RULE_STYLE, sizeof(StyleRule));
rule->style = style;
style->refs++;
return (Rule *) rule;
}
Rule *
new_multi_rule(LList *rules)
{
uint32_t count = llist_size(rules);
if (count == 1) {
Rule *rule = llist_get_first(rules);
llist_free(rules);
return rule;
} else {
MultiRule *rule = new_rule(RULE_MULTI, sizeof(MultiRule));
rule->rule_count = llist_size(rules);
rule->rules = (Rule **) llist_to_array(rules);
llist_free(rules);
return (Rule *) rule;
}
}
Rule *
new_match_rule(LList *matches, Rule *subrule)
{
MatchRule *rule = new_rule(RULE_MATCH, sizeof(MatchRule));
rule->match_count = llist_size(matches);
rule->matches = (Match **) llist_to_array(matches);
rule->rule = subrule;
llist_free(matches);
return (Rule *) rule;
}
Rule *
new_action_rule(ActionType action)
{
ActionRule *rule = new_rule(RULE_ACTION, sizeof(ActionRule));
rule->action = action;
return (Rule *) rule;
}
Rule *
new_substitution_rule(const char *match, const char *repl, RegexFlags flags)
{
SubstitutionRule *rule = new_rule(RULE_SUBSTITUTION, sizeof(SubstitutionRule));
int init_flags = REG_EXTENDED;
int rc;
memset(&rule->regex, 0, sizeof(regex_t));
rule->replacement = xstrdup(repl);
rule->flags = flags;
if (flags & REGEX_IGNORE_CASE)
init_flags |= REG_ICASE;
rc = regcomp(&rule->regex, match, init_flags);
if (rc != 0) {
char *msg = xregerror(rc, &rule->regex);
regfree(&rule->regex);
warn("cannot compile regex: %s", msg);
free(msg);
exit(1);
}
return (Rule *) rule;
}
Rule *
new_set_rule(const char *replacement)
{
SetRule *rule = new_rule(RULE_SET, sizeof(SetRule));
rule->replacement = xstrdup(replacement);
return (Rule *) rule;
}