2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 /** \file filter_chain.c common helper functions for filter input/output */
27 DEFINE_FILTER_ARRAY(filters);
30 * call the init function of each supported filter
32 * \param all_filters the array of all supported filters
35 void filter_init(struct filter *all_filters)
39 for (f = all_filters; f->name; f++)
44 * close and destroy a filter callback
46 * \param fcb the filter callback to close
48 * This removes \a fcb from the list of filter callbacks and calls
49 * the close callback associated with \a fcb.
51 static void close_filter_callback(struct filter_callback *fcb)
53 PARA_NOTICE_LOG("closing filter_callback %p, data: %p\n", fcb, fcb->data);
59 * close all callbacks of a filter node
61 * \param fn the filter node which contains the filter callbacks to be closed
63 * Call close_filter_callback() for each entry in the filter callback list
66 static void close_callbacks(struct filter_node *fn)
68 struct filter_callback *fcb, *tmp;
70 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node)
71 close_filter_callback(fcb);
74 static void call_callbacks(struct filter_node *fn, char *inbuf, size_t inlen,
75 char *outbuf, size_t outlen)
77 struct filter_callback *fcb, *tmp;
78 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
80 if (inlen && fcb->input_cb) {
81 ret = fcb->input_cb(inbuf, inlen, fcb);
83 close_filter_callback(fcb);
87 if (!outlen || !fcb->output_cb)
89 ret = fcb->output_cb(outbuf, outlen, fcb);
91 close_filter_callback(fcb);
96 * call the convert function of each filter
98 * \param fc the filter chain containing the list of filter nodes.
100 * This is the core function of the filter subsystem. It loops over the list of
101 * filter nodes determined by \a fc 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.
107 * \return The sum of output bytes produced by the convert functions on success,
108 * negative return value on errors.
110 * \sa filter_node, filter#convert, filter_callback
112 int filter_io(struct filter_chain *fc)
114 struct filter_node *fn;
117 int conv, conv_total = 0;
120 loaded = fc->in_loaded;
122 list_for_each_entry(fn, &fc->filters, node) {
124 if (*loaded && fn->loaded < fn->bufsize) {
125 size_t old_fn_loaded = fn->loaded;
126 PARA_DEBUG_LOG("fc %p loaded: %zd, calling %s convert\n",
127 fc, *loaded, fn->filter->name);
128 ret = fn->filter->convert(ib, *loaded, fn);
134 call_callbacks(fn, ib, ret, fn->buf + old_fn_loaded,
135 fn->loaded - old_fn_loaded);
138 if (*loaded && ret) {
139 PARA_DEBUG_LOG("moving %zd bytes in input buffer for %s filter\n",
140 *loaded, fn->filter->name);
141 memmove(ib, ib + ret, *loaded);
145 loaded = &fn->loaded;
147 // PARA_DEBUG_LOG("loaded: %d\n", *loaded);
155 * close all filter nodes and its callbacks
157 * \param fc the filter chain to close
159 * For each filter node determined by \a fc, call the close function of each
160 * registered filter callback as well as the close function of the
161 * corresponding filter. Free all resources and destroy all callback lists and
162 * the filter node list.
164 * \sa filter::close, filter_callback::close
166 void close_filters(struct filter_chain *fc)
168 struct filter_node *fn, *tmp;
172 PARA_DEBUG_LOG("closing filter chain %p\n", fc);
173 list_for_each_entry_safe(fn, tmp, &fc->filters, node) {
174 PARA_NOTICE_LOG("closing %s filter callbacks (fc %p, fn %p)\n", fn->filter->name, fc, fn);
176 PARA_NOTICE_LOG("closing %s filter (fc %p, fn %p)\n", fn->filter->name, fc, fn);
177 fn->filter->close(fn);
184 * If the filter has a command line parser and options is not NULL, run it.
185 * Returns filter_num on success, negative on errors
187 static int parse_filter_args(int filter_num, char *options, void **conf)
189 struct filter *f = &filters[filter_num];
193 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
194 // options? options : "(none)", f->parse_config);
195 if (!f->parse_config)
196 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
197 // PARA_DEBUG_LOG("options: %s\n", options);
198 argc = split_args(options, &argv, " \t");
199 // PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
200 for (i = argc - 1; i >= 0; i--)
201 argv[i + 1] = argv[i];
202 argv[0] = para_strdup(f->name);
204 *conf = f->parse_config(argc, argv);
205 return *conf? filter_num : -E_BAD_FILTER_OPTIONS;
209 * check the filter command line options
211 * \param fa the command line options
212 * \param conf points to the filter configuration upon successful return
214 * Check if \a fa starts with a the name of a supported filter, followed by
215 * a colon. If yes, call the command line parser of that filter.
217 * \return On success, the number of the filter is returned and \a conf
218 * is initialized to point to the filter configuration determined by \a fa.
219 * On errors, a negative value is returned.
221 * Note: If \a fa specifies a filter that has no command line parser success is
222 * returned, and \a conf is initialized to \p NULL.
224 * \sa filter::parse_config
226 int check_filter_arg(char *fa, void **conf)
231 // PARA_DEBUG_LOG("arg: %s\n", fa);
232 for (j = 0; filters[j].name; j++) {
233 const char *name = filters[j].name;
234 size_t len = strlen(name);
236 if (strlen(fa) < len)
238 if (strncmp(name, fa, len))
243 if (c && !filters[j].parse_config)
244 return -E_BAD_FILTER_OPTIONS;
245 return parse_filter_args(j, c? fa + len + 1 :
246 fa + strlen(fa), conf);
248 return -E_UNSUPPORTED_FILTER;