d22ef28eed3cbaa1e38f1b15f2f0b73a1ce7b011
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 */
21 #include "gcc-compat.h"
28 DEFINE_FILTER_ARRAY(filters
);
31 * call the init function of each supported filter
33 * \param all_filters the array of all supported filters
36 void filter_init(struct filter
*all_filters
)
40 for (f
= all_filters
; f
->name
; f
++)
45 * close and destroy a filter callback
47 * \param fcb the filter callback to close
49 * This removes \a fcb from the list of filter callbacks and calls
50 * the close callback associated with \a fcb.
52 static void close_filter_callback(struct filter_callback
*fcb
)
54 PARA_NOTICE_LOG("closing filter_callback %p, data: %p\n", fcb
, fcb
->data
);
60 * close all callbacks of a filter node
62 * \param fn the filter node which contains the filter callbacks to be closed
64 * Call close_filter_callback() for each entry in the filter callback list
67 static void close_callbacks(struct filter_node
*fn
)
69 struct filter_callback
*fcb
, *tmp
;
71 list_for_each_entry_safe(fcb
, tmp
, &fn
->callbacks
, node
)
72 close_filter_callback(fcb
);
75 static void call_callbacks(struct filter_node
*fn
, char *inbuf
, size_t inlen
,
76 char *outbuf
, size_t outlen
)
78 struct filter_callback
*fcb
, *tmp
;
79 list_for_each_entry_safe(fcb
, tmp
, &fn
->callbacks
, node
) {
81 if (inlen
&& fcb
->input_cb
) {
82 ret
= fcb
->input_cb(inbuf
, inlen
, fcb
);
84 close_filter_callback(fcb
);
88 if (!outlen
|| !fcb
->output_cb
)
90 ret
= fcb
->output_cb(outbuf
, outlen
, fcb
);
92 close_filter_callback(fcb
);
97 * call the convert function of each filter
99 * \param fci the filter chain containing the list of filter nodes.
101 * This is the core function of the filter subsystem. It loops over the list of
102 * filter nodes determined by \a fci and calls the filter's convert function if
103 * there is input available for the filter node in question. If the convert
104 * function consumed some or all of its input data, all registered input
105 * callbacks are called. Similarly, if a convert function produced output, all
106 * registerd output callbacks get called.
108 * \return The sum of output bytes produced by the convert functions on success,
109 * negative return value on errors.
111 * \sa filter_node, filter#convert, filter_callback
113 int filter_io(struct filter_chain_info
*fci
)
115 struct filter_node
*fn
;
118 int conv
, conv_total
= 0;
121 loaded
= fci
->in_loaded
;
123 list_for_each_entry(fn
, &fci
->filters
, node
) {
125 if (*loaded
&& fn
->loaded
< fn
->bufsize
) {
126 size_t old_fn_loaded
= fn
->loaded
;
127 PARA_DEBUG_LOG("fc %p loaded: %d, calling %s convert\n", fci
, *loaded
, fn
->filter
->name
);
128 ret
= fn
->filter
->convert(ib
, *loaded
, fn
);
134 call_callbacks(fn
, ib
, ret
, fn
->buf
+ old_fn_loaded
, fn
->loaded
- old_fn_loaded
);
137 if (*loaded
&& ret
) {
138 PARA_DEBUG_LOG("moving %d bytes in input buffer for %s filter\n",
139 *loaded
, fn
->filter
->name
);
140 memmove(ib
, ib
+ ret
, *loaded
);
144 loaded
= &fn
->loaded
;
146 // PARA_DEBUG_LOG("loaded: %d\n", *loaded);
154 * close all filter nodes and its callbacks
156 * \param fci the filter chain to close
158 * For each filter node determined by \a fci, call the close function of each
159 * registered filter callback as well as the close function of the
160 * corresponding filter. Free all resources and destroy all callback lists and
161 * the filter node list.
163 * \sa filter::close, filter_callback::close
165 void close_filters(struct filter_chain_info
*fci
)
167 struct filter_node
*fn
, *tmp
;
171 PARA_DEBUG_LOG("closing filter chain %p\n", fci
);
172 list_for_each_entry_safe(fn
, tmp
, &fci
->filters
, node
) {
173 PARA_NOTICE_LOG("closing %s filter callbacks (fci %p, fn %p)\n", fn
->filter
->name
, fci
, fn
);
175 PARA_NOTICE_LOG("closing %s filter (fci %p, fn %p)\n", fn
->filter
->name
, fci
, fn
);
176 fn
->filter
->close(fn
);
183 * If the filter has a command line parser and options is not NULL, run it.
184 * Returns filter_num on success, negative on errors
186 static int parse_filter_args(int filter_num
, char *options
, void **conf
)
188 struct filter
*f
= &filters
[filter_num
];
190 char *dummy_args
[] = {"", "", NULL
};
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 options
? -E_BAD_FILTER_OPTIONS
: filter_num
;
198 // PARA_DEBUG_LOG("options: %s\n", options);
199 argc
= split_args(options
, &argv
, ':');
200 // PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
201 for (i
= argc
; i
>= 0; i
--)
202 argv
[i
+ 1] = argv
[i
];
204 *conf
= f
->parse_config(argc
, argv
);
206 /* is it OK to have no options? */
207 *conf
= f
->parse_config(2, dummy_args
);
209 return *conf
? filter_num
: -E_BAD_FILTER_OPTIONS
;
213 * check the filter command line options
215 * \param fa the command line options (values separated by colons)
216 * \param conf points to the filter configuration upon successful return
218 * Check if \a fa starts with a the name of a supported filter, followed by
219 * a colon. If yes, call the command line parser of that filter.
221 * \return On success, the number of the filter is returned and \a conf
222 * is initialized to point to the filter configuration determined by \a fa.
223 * On errors, a negative value is returned.
225 * Note: If \a fa specifies a filter that has no command line parser success is
226 * returned, and \a conf is initialized to \p NULL.
228 * \sa filter::parse_config
230 int check_filter_arg(char *fa
, void **conf
)
235 // PARA_DEBUG_LOG("arg: %s\n", fa);
236 for (j
= 0; filters
[j
].name
; j
++) {
237 const char *name
= filters
[j
].name
;
238 size_t len
= strlen(name
);
240 if (strlen(fa
) < len
)
242 if (strncmp(name
, fa
, len
))
247 if (c
&& !filters
[j
].parse_config
)
248 return -E_BAD_FILTER_OPTIONS
;
249 return parse_filter_args(j
, c
? fa
+ len
+ 1 : NULL
, conf
);
251 return -E_UNSUPPORTED_FILTER
;