17f67058df88c9e128b7afdb5a2a3c0f801e90ad
[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                 PARA_INFO_LOG("closing %s filter callback\n",
74                         fn->filter->name);
75                 close_filter_callback(fcb);
76         }
77 }
78
79 static void call_callbacks(struct filter_node *fn, char *inbuf, size_t inlen,
80         char *outbuf, size_t outlen)
81 {
82         struct filter_callback *fcb, *tmp;
83         list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
84                 int ret;
85                 if (inlen && fcb->input_cb) {
86                         ret = fcb->input_cb(inbuf, inlen, fcb);
87                         if (ret < 0) {
88                                 close_filter_callback(fcb);
89                                 continue;
90                         }
91                 }
92                 if (!outlen || !fcb->output_cb)
93                         continue;
94                 ret = fcb->output_cb(outbuf, outlen, fcb);
95                 if (ret < 0)
96                         close_filter_callback(fcb);
97         }
98 }
99
100 /**
101  * call the convert function of each filter
102  *
103  * This is the core function of the filter subsystem. It loops over the list of
104  * filter nodes determined by \a t and calls the filter's convert function if
105  * there is input available for the filter node in question. If the convert
106  * function consumed some or all of its input data, all registered input
107  * callbacks are called.  Similarly, if a convert function produced output, all
108  * registerd output callbacks get called.
109  *
110  * \return The sum of output bytes produced by the convert functions on
111  * success, negative return value on errors (the return value is stored in
112  * t->ret).
113  *
114  * \sa filter_node, filter#convert, filter_callback
115  */
116 void filter_pre_select(__a_unused struct sched *s, struct task *t)
117 {
118         struct filter_chain *fc = t->private_data;
119         struct filter_node *fn;
120         char *ib;
121         size_t *loaded;
122         int conv, conv_total = 0;
123
124         t->ret = -E_FC_EOF;
125         if (*fc->output_eof)
126                 goto err_out;
127 again:
128         ib = fc->inbuf;
129         loaded = fc->in_loaded;
130         conv = 0;
131         list_for_each_entry(fn, &fc->filters, node) {
132                 if (*loaded && fn->loaded < fn->bufsize) {
133                         size_t old_fn_loaded = fn->loaded;
134                         PARA_DEBUG_LOG("fc %p loaded: %zd, calling %s convert\n",
135                                 fc, *loaded, fn->filter->name);
136                         t->ret = fn->filter->convert(ib, *loaded, fn);
137                         if (t->ret < 0)
138                                 goto err_out;
139                         call_callbacks(fn, ib, t->ret, fn->buf + old_fn_loaded,
140                                 fn->loaded - old_fn_loaded);
141                         *loaded -= t->ret;
142                         conv += t->ret;
143                         if (*loaded && t->ret) {
144                                 PARA_INFO_LOG("moving %zd bytes in input buffer for %s filter\n",
145                                         *loaded,  fn->filter->name);
146                                 memmove(ib, ib + t->ret, *loaded);
147                         }
148                 }
149                 ib = fn->buf;
150                 loaded = &fn->loaded;
151         }
152         conv_total += conv;
153         PARA_INFO_LOG("eof (in/out/fc): %d/%d/%d out_loaded: %d, conv: %d, conv_total: %d\n", *fc->input_eof,
154                 *fc->output_eof, fc->eof, *fc->out_loaded, conv, conv_total);
155         if (conv)
156                 goto again;
157         t->ret = 1;
158         if (!*fc->input_eof)
159                 return;
160         if (*fc->out_loaded)
161                 return;
162         if (*fc->in_loaded && conv_total)
163                 return;
164         t->ret = -E_FC_EOF;
165 err_out:
166         fc->eof = 1;
167 }
168
169 /**
170  * close all filter nodes and its callbacks
171  *
172  * \param fc the filter chain to close
173  *
174  * For each filter node determined by \a fc, call the close function of each
175  * registered filter callback as well as the close function of the
176  * corresponding filter.  Free all resources and destroy all callback lists and
177  * the filter node list.
178  *
179  * \sa filter::close, filter_callback::close
180  */
181 void close_filters(struct filter_chain *fc)
182 {
183         struct filter_node *fn, *tmp;
184
185         if (!fc)
186                 return;
187         PARA_NOTICE_LOG("closing filter chain %p\n", fc);
188         list_for_each_entry_safe(fn, tmp, &fc->filters, node) {
189                 close_callbacks(fn);
190                 PARA_INFO_LOG("closing %s filter\n", fn->filter->name);
191                 fn->filter->close(fn);
192                 list_del(&fn->node);
193                 free(fn);
194         }
195 }
196
197 /*
198  * If the filter has a command line parser and options is not NULL, run it.
199  * Returns filter_num on success, negative on errors
200  */
201 static int parse_filter_args(int filter_num, char *options, void **conf)
202 {
203         struct filter *f = &filters[filter_num];
204         int i, argc = 2;
205         char **argv;
206
207 //      PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
208 //              options? options : "(none)", f->parse_config);
209         if (!f->parse_config)
210                 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
211 //      PARA_DEBUG_LOG("options: %s\n", options);
212         argc = split_args(options, &argv, " \t");
213 //              PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
214         for (i = argc - 1; i >= 0; i--)
215                 argv[i + 1] = argv[i];
216         argv[0] = para_strdup(f->name);
217         argc += 1;
218         *conf = f->parse_config(argc, argv);
219         return *conf? filter_num : -E_BAD_FILTER_OPTIONS;
220 }
221
222 /**
223  * check the filter command line options
224  *
225  * \param fa the command line options
226  * \param conf points to the filter configuration upon successful return
227  *
228  * Check if \a fa starts with a the name of a supported filter, followed by
229  * a colon. If yes, call the command line parser of that filter.
230  *
231  * \return On success, the number of the filter is returned and \a conf
232  * is initialized to point to the filter configuration determined by \a fa.
233  * On errors, a negative value is returned.
234  *
235  * Note: If \a fa specifies a filter that has no command line parser success is
236  * returned, and \a conf is initialized to \p NULL.
237  *
238  * \sa filter::parse_config
239  */
240 int check_filter_arg(char *fa, void **conf)
241 {
242         int j;
243
244         *conf = NULL;
245 //      PARA_DEBUG_LOG("arg: %s\n", fa);
246         for (j = 0; filters[j].name; j++) {
247                 const char *name = filters[j].name;
248                 size_t len = strlen(name);
249                 char c;
250                 if (strlen(fa) < len)
251                         continue;
252                 if (strncmp(name, fa, len))
253                         continue;
254                 c = fa[len];
255                 if (c && c != ' ')
256                         continue;
257                 if (c && !filters[j].parse_config)
258                         return -E_BAD_FILTER_OPTIONS;
259                 return parse_filter_args(j, c? fa + len + 1 :
260                         fa + strlen(fa), conf);
261         }
262         return -E_UNSUPPORTED_FILTER;
263 }
264