1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
10 /* 1. Redistributions of source code must retain the above copyright */
11 /* notice, this list of conditions and the following disclaimer. */
12 /* 2. Redistributions in binary form must reproduce the above copyright */
13 /* notice, this list of conditions and the following disclaimer in the */
14 /* documentation and/or other materials provided with the distribution. */
15 /* 3. The names of its contributors may not be used to endorse or promote */
16 /* products derived from this software without specific prior written */
19 /* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
21 /* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
22 /* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */
23 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
24 /* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
25 /* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */
26 /* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
27 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */
28 /* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */
29 /* THE POSSIBILITY OF SUCH DAMAGE. */
30 /******************************************************************************/
33 * Copyright © 2008 Florent Bruneau
40 static filter_runner_t runners[FTK_count];
41 static filter_constructor_t constructors[FTK_count];
42 static filter_destructor_t destructors[FTK_count];
43 static bool hooks[FTK_count][HTK_count];
44 static bool params[FTK_count][ATK_count];
46 static filter_context_constructor_t ctx_constructors[FTK_count];
47 static filter_context_destructor_t ctx_destructors[FTK_count];
49 static const filter_hook_t default_hook = {
51 .value = (char*)"DUNNO",
57 static const filter_hook_t async_hook = {
65 filter_type_t filter_register(const char *type, filter_constructor_t constructor,
66 filter_destructor_t destructor, filter_runner_t runner,
67 filter_context_constructor_t context_constructor,
68 filter_context_destructor_t context_destructor)
70 filter_token tok = filter_tokenize(type, m_strlen(type));
73 runners[tok] = runner;
74 constructors[tok] = constructor;
75 destructors[tok] = destructor;
77 ctx_constructors[tok] = context_constructor;
78 ctx_destructors[tok] = context_destructor;
82 filter_result_t filter_hook_register(filter_type_t filter,
85 filter_result_t tok = hook_tokenize(name, m_strlen(name));
89 hooks[filter][tok] = true;
93 filter_param_id_t filter_param_register(filter_type_t filter,
96 filter_param_id_t tok = param_tokenize(name, m_strlen(name));
100 params[filter][tok] = true;
104 bool filter_build(filter_t *filter)
107 if (filter->type == FTK_UNKNOWN || filter->name == NULL) {
110 if (filter->hooks.len > 0) {
111 # define QSORT_TYPE filter_hook_t
112 # define QSORT_BASE filter->hooks.data
113 # define QSORT_NELT filter->hooks.len
114 # define QSORT_LT(a,b) a->type < b->type
117 filter_constructor_t constructor = constructors[filter->type];
119 ret = constructor(filter);
121 array_deep_wipe(filter->params, filter_params_wipe);
125 bool filter_update_references(filter_t *filter, A(filter_t) *filter_list)
127 foreach (filter_hook_t *hook, filter->hooks) {
128 if (!hook->postfix) {
129 hook->filter_id = filter_find_with_name(filter_list, hook->value);
130 if (hook->filter_id == -1) {
131 err("invalid filter name %s for hook %s",
132 hook->value, htokens[hook->type]);
135 p_delete(&hook->value);
141 static inline bool filter_check_loop(filter_t *filter, A(filter_t) *array, int level)
143 if (filter->last_seen == level) {
146 filter->last_seen = level;
147 foreach (filter_hook_t *hook, filter->hooks) {
151 if (hook->filter_id == level) {
154 if (!filter_check_loop(array_ptr(*array, hook->filter_id), array, level)) {
161 bool filter_check_safety(A(filter_t) *array)
163 foreach (filter_t *filter, *array) {
164 if (!filter_check_loop(filter, array, __Ai)) {
165 err("the filter tree contains a loop");
172 void filter_wipe(filter_t *filter)
174 filter_destructor_t destructor = destructors[filter->type];
178 array_deep_wipe(filter->hooks, filter_hook_wipe);
179 array_deep_wipe(filter->params, filter_params_wipe);
180 p_delete(&filter->name);
183 const filter_hook_t *filter_run(const filter_t *filter, const query_t *query,
184 filter_context_t *context)
187 int end = filter->hooks.len;
188 debug("running filter %s (%s)", filter->name, ftokens[filter->type]);
189 filter_result_t res = runners[filter->type](filter, query, context);
191 context->current_filter = NULL;
193 debug("filter run, result is %s", htokens[res]);
194 if (res == HTK_ABORT) {
197 if (res == HTK_ASYNC) {
198 context->current_filter = filter;
202 while (start < end) {
203 int mid = (start + end) / 2;
204 filter_hook_t *hook = array_ptr(filter->hooks, mid);
205 if (hook->type == res) {
206 debug("return hook of type %s, value %s",
207 htokens[hook->type], hook->value);
209 } else if (res < hook->type) {
215 warn("missing hook %s for filter %s", htokens[res], filter->name);
216 return &default_hook;
219 bool filter_test(const filter_t *filter, const query_t *query,
220 filter_context_t *context, filter_result_t result)
222 return !!(runners[filter->type](filter, query, context) == result);
225 void filter_set_name(filter_t *filter, const char *name, int len)
227 filter->name = p_dupstr(name, len);
230 bool filter_set_type(filter_t *filter, const char *type, int len)
232 filter->type = filter_tokenize(type, len);
233 return filter->type != FTK_UNKNOWN;
236 bool filter_add_param(filter_t *filter, const char *name, int name_len,
237 const char *value, int value_len)
239 filter_param_t param;
240 param.type = param_tokenize(name, name_len);
241 if (param.type == ATK_UNKNOWN) {
242 err("unknown parameter %.*s", name_len, name);
245 if (!params[filter->type][param.type]) {
246 err("hook %s is not valid for filter %s",
247 atokens[param.type], ftokens[filter->type]);
250 param.value = p_dupstr(value, value_len);
251 param.value_len = value_len;
252 array_add(filter->params, param);
256 bool filter_add_hook(filter_t *filter, const char *name, int name_len,
257 const char *value, int value_len)
260 hook.type = hook_tokenize(name, name_len);
261 if (hook.type == HTK_UNKNOWN) {
262 err("unknown hook type %.*s", name_len, name);
265 if (!hooks[filter->type][hook.type] || hook.type == HTK_ABORT) {
266 err("hook %s not is valid for filter %s",
267 htokens[hook.type], ftokens[filter->type]);
271 hook.postfix = (strncmp(value, "postfix:", 8) == 0);
272 hook.value = m_strdup(hook.postfix ? value + 8 : value);
274 array_add(filter->hooks, hook);
278 void filter_context_prepare(filter_context_t *context, void *qctx)
280 for (int i = 0 ; i < FTK_count ; ++i) {
281 if (ctx_constructors[i] != NULL) {
282 context->contexts[i] = ctx_constructors[i]();
285 context->current_filter = NULL;
286 context->data = qctx;
289 void filter_context_wipe(filter_context_t *context)
291 for (int i = 0 ; i < FTK_count ; ++i) {
292 if (ctx_destructors[i] != NULL) {
293 ctx_destructors[i](context->contexts[i]);