typo
[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 "gcc-compat.h"
22 #include "para.h"
23 #include "list.h"
24 #include "filter.h"
25 #include "error.h"
26 #include "string.h"
27
28 DEFINE_FILTER_ARRAY(filters);
29
30 /**
31 * call the init function of each supported filter
32 *
33 * \param all_filters the array of all supported filters
34 * \sa filter::init
35 */
36 void filter_init(struct filter *all_filters)
37 {
38 struct filter *f;
39
40 for (f = all_filters; f->name; f++)
41 f->init(f);
42 }
43
44 /**
45 * close and destroy a filter callback
46 *
47 * \param fcb the filter callback to close
48 *
49 * This removes \a fcb from the list of filter callbacks and calls
50 * the close callback associated with \a fcb.
51 */
52 static void close_filter_callback(struct filter_callback *fcb)
53 {
54 PARA_NOTICE_LOG("closing filter_callback %p, data: %p\n", fcb, fcb->data);
55 list_del(&fcb->node);
56 fcb->close(fcb);
57 }
58
59 /**
60 * close all callbacks of a filter node
61 *
62 * \param fn the filter node which contains the filter callbacks to be closed
63 *
64 * Call close_filter_callback() for each entry in the filter callback list
65 * of \a fn.
66 */
67 static void close_callbacks(struct filter_node *fn)
68 {
69 struct filter_callback *fcb, *tmp;
70
71 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node)
72 close_filter_callback(fcb);
73 }
74
75 static void call_callbacks(struct filter_node *fn, char *inbuf, size_t inlen,
76 char *outbuf, size_t outlen)
77 {
78 struct filter_callback *fcb, *tmp;
79 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
80 int ret;
81 if (inlen && fcb->input_cb) {
82 ret = fcb->input_cb(inbuf, inlen, fcb);
83 if (ret < 0) {
84 close_filter_callback(fcb);
85 continue;
86 }
87 }
88 if (!outlen || !fcb->output_cb)
89 continue;
90 ret = fcb->output_cb(outbuf, outlen, fcb);
91 if (ret < 0)
92 close_filter_callback(fcb);
93 }
94 }
95
96 /**
97 * call the convert function of each filter
98 *
99 * \param fci the filter chain containing the list of filter nodes.
100 *
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.
107 *
108 * \return The sum of output bytes produced by the convert functions on success,
109 * negative return value on errors.
110 *
111 * \sa filter_node, filter#convert, filter_callback
112 */
113 int filter_io(struct filter_chain_info *fci)
114 {
115 struct filter_node *fn;
116 char *ib;
117 size_t *loaded;
118 int conv, conv_total = 0;
119 again:
120 ib = fci->inbuf;
121 loaded = fci->in_loaded;
122 conv = 0;
123 list_for_each_entry(fn, &fci->filters, node) {
124 int ret;
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);
129 if (ret < 0) {
130 if (!fci->error)
131 fci->error = -ret;
132 return ret;
133 }
134 call_callbacks(fn, ib, ret, fn->buf + old_fn_loaded, fn->loaded - old_fn_loaded);
135 *loaded -= ret;
136 conv += ret;
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);
141 }
142 }
143 ib = fn->buf;
144 loaded = &fn->loaded;
145 }
146 // PARA_DEBUG_LOG("loaded: %d\n", *loaded);
147 conv_total += conv;
148 if (conv)
149 goto again;
150 return conv_total;
151 }
152
153 /**
154 * close all filter nodes and its callbacks
155 *
156 * \param fci the filter chain to close
157 *
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.
162 *
163 * \sa filter::close, filter_callback::close
164 */
165 void close_filters(struct filter_chain_info *fci)
166 {
167 struct filter_node *fn, *tmp;
168
169 if (!fci)
170 return;
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);
174 close_callbacks(fn);
175 PARA_NOTICE_LOG("closing %s filter (fci %p, fn %p)\n", fn->filter->name, fci, fn);
176 fn->filter->close(fn);
177 list_del(&fn->node);
178 free(fn);
179 }
180 }
181
182 /*
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
185 */
186 static int parse_filter_args(int filter_num, char *options, void **conf)
187 {
188 struct filter *f = &filters[filter_num];
189 int i, argc = 2;
190 char *dummy_args[] = {"", "", NULL};
191 char **argv;
192
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;
197 if (options) {
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];
203 argc += 2;
204 *conf = f->parse_config(argc, argv);
205 } else {
206 /* is it OK to have no options? */
207 *conf = f->parse_config(2, dummy_args);
208 }
209 return *conf? filter_num : -E_BAD_FILTER_OPTIONS;
210 }
211
212 /**
213 * check the filter command line options
214 *
215 * \param fa the command line options (values separated by colons)
216 * \param conf points to the filter configuration upon successful return
217 *
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.
220 *
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.
224 *
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.
227 *
228 * \sa filter::parse_config
229 */
230 int check_filter_arg(char *fa, void **conf)
231 {
232 int j;
233
234 *conf = NULL;
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);
239 char c;
240 if (strlen(fa) < len)
241 continue;
242 if (strncmp(name, fa, len))
243 continue;
244 c = fa[len];
245 if (c && c != ':')
246 continue;
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);
250 }
251 return -E_UNSUPPORTED_FILTER;
252 }
253