]> git.tuebingen.mpg.de Git - paraslash.git/blob - filter_common.c
Rename filter_chain.c to filter_common.c.
[paraslash.git] / filter_common.c
1 /*
2  * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file filter_common.c Common helper functions for filter input/output. */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "fd.h"
16 #include "filter.h"
17 #include "error.h"
18 #include "string.h"
19
20 /** The array of supported filters. */
21 struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
22
23 /**
24  * Call the init function of each supported filter.
25  *
26  * \param all_filters the array of all supported filters.
27  *
28  * \sa filter::init
29  */
30 void filter_init(struct filter *all_filters)
31 {
32         int i;
33
34         FOR_EACH_SUPPORTED_FILTER(i)
35                 all_filters[i].init(all_filters + i);
36 }
37
38 /**
39  * Close and destroy a filter callback.
40  *
41  * \param fcb The filter callback to close.
42  *
43  * This removes \a fcb from the list of filter callbacks and calls
44  * the close callback associated with \a fcb.
45  */
46 static void close_filter_callback(struct filter_callback *fcb)
47 {
48         PARA_NOTICE_LOG("closing filter_callback %p, data: %p\n", fcb, fcb->data);
49         list_del(&fcb->node);
50         fcb->close(fcb);
51 }
52
53 /**
54  * Close all callbacks of a filter node.
55  *
56  * \param fn The filter node which contains the filter callbacks to be closed.
57  *
58  * Call close_filter_callback() for each entry in the filter callback list
59  * of \a fn.
60  */
61 static void close_callbacks(struct filter_node *fn)
62 {
63         struct filter_callback *fcb, *tmp;
64
65         list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
66                 PARA_INFO_LOG("closing %s filter callback\n",
67                         filters[fn->filter_num].name);
68                 close_filter_callback(fcb);
69         }
70 }
71
72 static void call_callbacks(struct filter_node *fn, char *inbuf, size_t inlen,
73         char *outbuf, size_t outlen)
74 {
75         struct filter_callback *fcb, *tmp;
76         list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
77                 int ret;
78                 if (inlen && fcb->input_cb) {
79                         ret = fcb->input_cb(inbuf, inlen, fcb);
80                         if (ret < 0) {
81                                 close_filter_callback(fcb);
82                                 continue;
83                         }
84                 }
85                 if (!outlen || !fcb->output_cb)
86                         continue;
87                 ret = fcb->output_cb(outbuf, outlen, fcb);
88                 if (ret < 0)
89                         close_filter_callback(fcb);
90         }
91 }
92
93 /**
94  * Call the convert function of each filter.
95  *
96  * \param s Unused.
97  * \param t The task identifying the filter chain.
98  *
99  * This is the core function of the filter subsystem. It loops over the list of
100  * filter nodes determined by \a t and calls the filter's convert function if
101  * there is input available for the filter node in question. If the convert
102  * function consumed some or all of its input data, all registered input
103  * callbacks are called.  Similarly, if a convert function produced output, all
104  * registerd output callbacks get called.
105  *
106  * On errors a (negative) error code is stored in t->error.
107  *
108  * \sa filter_node, filter#convert, filter_callback.
109  */
110 void filter_pre_select(__a_unused struct sched *s, struct task *t)
111 {
112         struct filter_chain *fc = container_of(t, struct filter_chain, task);
113         struct filter_node *fn;
114         char *ib;
115         size_t *loaded;
116         int i, conv, conv_total = 0;
117
118         if (fc->output_error && *fc->output_error < 0) {
119                 t->error =  *fc->output_error;
120                 return;
121         }
122 again:
123         ib = fc->inbuf;
124         loaded = fc->in_loaded;
125         conv = 0;
126         FOR_EACH_FILTER_NODE(fn, fc, i) {
127                 struct filter *f = filters + fn->filter_num;
128                 if (fn->loaded < fn->bufsize) {
129                         size_t size, old_fn_loaded = fn->loaded;
130 //                      PARA_DEBUG_LOG("fc %p loaded: %zd, calling %s convert\n",
131 //                              fc, *loaded, fn->filter->name);
132                         t->error = f->convert(ib, *loaded, fn);
133                         if (t->error < 0)
134                                 return;
135                         size = t->error;
136                         call_callbacks(fn, ib, size, fn->buf + old_fn_loaded,
137                                 fn->loaded - old_fn_loaded);
138                         *loaded -= size;
139                         conv += size;
140                         if (*loaded && size) {
141 //                              PARA_DEBUG_LOG("moving %zd bytes in input "
142 //                                      "buffer for %s filter\n",
143 //                                      *loaded,  fn->filter->name);
144                                 memmove(ib, ib + size, *loaded);
145                         }
146                 }
147                 ib = fn->buf;
148                 loaded = &fn->loaded;
149         }
150         conv_total += conv;
151 //      PARA_DEBUG_LOG("eof (in/out/fc): %d/%d/%d out_loaded: %zd, "
152 //              "conv: %d, conv_total: %d\n", *fc->input_eof,
153 //              fc->output_eof? *fc->output_eof : -42,
154 //              fc->eof, *fc->out_loaded, conv, conv_total);
155         if (conv)
156                 goto again;
157         if (!*fc->input_error)
158                 return;
159         if (*fc->out_loaded)
160                 return;
161         if (*fc->in_loaded && conv_total)
162                 return;
163         t->error = -E_FC_EOF;
164 }
165
166 /**
167  * Close all filter nodes and their callbacks.
168  *
169  * \param fc The filter chain to close.
170  *
171  * For each filter node determined by \a fc, call the close function of each
172  * registered filter callback as well as the close function of the
173  * corresponding filter.  Free all resources and destroy all callback lists and
174  * the filter node list.
175  *
176  * \sa filter::close, filter_callback::close
177  */
178 void close_filters(struct filter_chain *fc)
179 {
180         struct filter_node *fn;
181         int i;
182
183         if (!fc)
184                 return;
185         PARA_NOTICE_LOG("closing filter chain %p\n", fc);
186         FOR_EACH_FILTER_NODE(fn, fc, i) {
187                 struct filter *f = filters + fn->filter_num;
188                 close_callbacks(fn);
189                 PARA_INFO_LOG("closing %s filter\n", f->name);
190                 f->close(fn);
191         }
192         free(fc->filter_nodes);
193 }
194
195 /*
196  * If the filter has a command line parser and options is not NULL, run it.
197  * Returns filter_num on success, negative on errors
198  */
199 static int parse_filter_args(int filter_num, char *options, void **conf)
200 {
201         struct filter *f = &filters[filter_num];
202         int ret, i, argc = 2;
203         char **argv;
204
205 //      PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
206 //              options? options : "(none)", f->parse_config);
207         if (!f->parse_config)
208                 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
209 //      PARA_DEBUG_LOG("options: %s\n", options);
210         argc = split_args(options, &argv, " \t");
211 //              PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
212         for (i = argc - 1; i >= 0; i--)
213                 argv[i + 1] = argv[i];
214         argv[0] = para_strdup(f->name);
215         argc += 1;
216         ret = f->parse_config(argc, argv, conf);
217         free(argv[0]);
218         free(argv);
219         return ret < 0? ret : filter_num;
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_EACH_SUPPORTED_FILTER(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