]> git.tuebingen.mpg.de Git - paraslash.git/blob - filter_chain.c
2510e49de1debf6122f52117702eb38fe7e472c3
[paraslash.git] / filter_chain.c
1 /*
2  * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file filter_chain.c common helper functions for filter input/output */
20
21 #include "para.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "fd.h"
25 #include "filter.h"
26 #include "error.h"
27 #include "string.h"
28
29 DEFINE_FILTER_ARRAY(filters);
30
31 /**
32  * call the init function of each supported filter
33  *
34  * \param all_filters the array of all supported filters
35  * \sa filter::init
36  */
37 void filter_init(struct filter *all_filters)
38 {
39         struct filter *f;
40
41         for (f = all_filters; f->name; f++)
42                 f->init(f);
43 }
44
45 /**
46  * close and destroy a filter callback
47  *
48  * \param fcb the filter callback to close
49  *
50  * This removes \a fcb from the list of filter callbacks and calls
51  * the close callback associated with \a fcb.
52  */
53 static void close_filter_callback(struct filter_callback *fcb)
54 {
55         PARA_NOTICE_LOG("closing filter_callback %p, data: %p\n", fcb, fcb->data);
56         list_del(&fcb->node);
57         fcb->close(fcb);
58 }
59
60 /**
61  * close all callbacks of a filter node
62  *
63  * \param fn the filter node which contains the filter callbacks to be closed
64  *
65  * Call close_filter_callback() for each entry in the filter callback list
66  * of \a fn.
67  */
68 static void close_callbacks(struct filter_node *fn)
69 {
70         struct filter_callback *fcb, *tmp;
71
72         list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node)
73                 close_filter_callback(fcb);
74 }
75
76 static void call_callbacks(struct filter_node *fn, char *inbuf, size_t inlen,
77         char *outbuf, size_t outlen)
78 {
79         struct filter_callback *fcb, *tmp;
80         list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
81                 int ret;
82                 if (inlen && fcb->input_cb) {
83                         ret = fcb->input_cb(inbuf, inlen, fcb);
84                         if (ret < 0) {
85                                 close_filter_callback(fcb);
86                                 continue;
87                         }
88                 }
89                 if (!outlen || !fcb->output_cb)
90                         continue;
91                 ret = fcb->output_cb(outbuf, outlen, fcb);
92                 if (ret < 0)
93                         close_filter_callback(fcb);
94         }
95 }
96
97 /**
98  * call the convert function of each filter
99  *
100  * This is the core function of the filter subsystem. It loops over the list of
101  * filter nodes determined by \a t and calls the filter's convert function if
102  * there is input available for the filter node in question. If the convert
103  * function consumed some or all of its input data, all registered input
104  * callbacks are called.  Similarly, if a convert function produced output, all
105  * registerd output callbacks get called.
106  *
107  * \return The sum of output bytes produced by the convert functions on
108  * success, negative return value on errors (the return value is stored in
109  * t->ret).
110  *
111  * \sa filter_node, filter#convert, filter_callback
112  */
113 void filter_pre_select(__a_unused struct sched *s, struct task *t)
114 {
115         struct filter_chain *fc = t->private_data;
116         struct filter_node *fn;
117         char *ib;
118         size_t *loaded;
119         int conv, conv_total = 0;
120 again:
121         ib = fc->inbuf;
122         loaded = fc->in_loaded;
123         conv = 0;
124         list_for_each_entry(fn, &fc->filters, node) {
125                 if (*loaded && fn->loaded < fn->bufsize) {
126                         size_t old_fn_loaded = fn->loaded;
127                         PARA_DEBUG_LOG("fc %p loaded: %zd, calling %s convert\n",
128                                 fc, *loaded, fn->filter->name);
129                         t->ret = fn->filter->convert(ib, *loaded, fn);
130                         if (t->ret < 0)
131                                 return;
132                         call_callbacks(fn, ib, t->ret, fn->buf + old_fn_loaded,
133                                 fn->loaded - old_fn_loaded);
134                         *loaded -= t->ret;
135                         conv += t->ret;
136                         if (*loaded && t->ret) {
137                                 PARA_DEBUG_LOG("moving %zd bytes in input buffer for %s filter\n",
138                                         *loaded,  fn->filter->name);
139                                 memmove(ib, ib + t->ret, *loaded);
140                         }
141                 }
142                 ib = fn->buf;
143                 loaded = &fn->loaded;
144         }
145         conv_total += conv;
146         PARA_DEBUG_LOG("reader eof: %d, eof: %d out_loaded: %d, conv: %d, conv_total: %d\n", *fc->reader_eof,
147                 fc->eof, *fc->out_loaded, conv, conv_total);
148         if (conv)
149                 goto again;
150         t->ret = 1;
151         if (!*fc->reader_eof)
152                 return;
153         if (*fc->out_loaded)
154                 return;
155         if (*fc->in_loaded && conv_total)
156                 return;
157         t->ret = -E_FC_EOF;
158         fc->eof = 1;
159 }
160
161 /**
162  * close all filter nodes and its callbacks
163  *
164  * \param fc the filter chain to close
165  *
166  * For each filter node determined by \a fc, call the close function of each
167  * registered filter callback as well as the close function of the
168  * corresponding filter.  Free all resources and destroy all callback lists and
169  * the filter node list.
170  *
171  * \sa filter::close, filter_callback::close
172  */
173 void close_filters(struct filter_chain *fc)
174 {
175         struct filter_node *fn, *tmp;
176
177         if (!fc)
178                 return;
179         PARA_DEBUG_LOG("closing filter chain %p\n", fc);
180         list_for_each_entry_safe(fn, tmp, &fc->filters, node) {
181                 PARA_NOTICE_LOG("closing %s filter callbacks (fc %p, fn %p)\n", fn->filter->name, fc, fn);
182                 close_callbacks(fn);
183                 PARA_NOTICE_LOG("closing %s filter (fc %p, fn %p)\n", fn->filter->name, fc, fn);
184                 fn->filter->close(fn);
185                 list_del(&fn->node);
186                 free(fn);
187         }
188 }
189
190 /*
191  * If the filter has a command line parser and options is not NULL, run it.
192  * Returns filter_num on success, negative on errors
193  */
194 static int parse_filter_args(int filter_num, char *options, void **conf)
195 {
196         struct filter *f = &filters[filter_num];
197         int i, argc = 2;
198         char **argv;
199
200 //      PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
201 //              options? options : "(none)", f->parse_config);
202         if (!f->parse_config)
203                 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
204 //      PARA_DEBUG_LOG("options: %s\n", options);
205         argc = split_args(options, &argv, " \t");
206 //              PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
207         for (i = argc - 1; i >= 0; i--)
208                 argv[i + 1] = argv[i];
209         argv[0] = para_strdup(f->name);
210         argc += 1;
211         *conf = f->parse_config(argc, argv);
212         return *conf? filter_num : -E_BAD_FILTER_OPTIONS;
213 }
214
215 /**
216  * check the filter command line options
217  *
218  * \param fa the command line options
219  * \param conf points to the filter configuration upon successful return
220  *
221  * Check if \a fa starts with a the name of a supported filter, followed by
222  * a colon. If yes, call the command line parser of that filter.
223  *
224  * \return On success, the number of the filter is returned and \a conf
225  * is initialized to point to the filter configuration determined by \a fa.
226  * On errors, a negative value is returned.
227  *
228  * Note: If \a fa specifies a filter that has no command line parser success is
229  * returned, and \a conf is initialized to \p NULL.
230  *
231  * \sa filter::parse_config
232  */
233 int check_filter_arg(char *fa, void **conf)
234 {
235         int j;
236
237         *conf = NULL;
238 //      PARA_DEBUG_LOG("arg: %s\n", fa);
239         for (j = 0; filters[j].name; j++) {
240                 const char *name = filters[j].name;
241                 size_t len = strlen(name);
242                 char c;
243                 if (strlen(fa) < len)
244                         continue;
245                 if (strncmp(name, fa, len))
246                         continue;
247                 c = fa[len];
248                 if (c && c != ' ')
249                         continue;
250                 if (c && !filters[j].parse_config)
251                         return -E_BAD_FILTER_OPTIONS;
252                 return parse_filter_args(j, c? fa + len + 1 :
253                         fa + strlen(fa), conf);
254         }
255         return -E_UNSUPPORTED_FILTER;
256 }
257