285efe68d56cefac4ea05dc3f6102db4cda89fa4
[paraslash.git] / filter_common.c
1 /*
2 * Copyright (C) 2005-2009 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 "ggo.h"
17 #include "filter.h"
18 #include "error.h"
19 #include "string.h"
20
21 /** The array of supported filters. */
22 struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
23
24 /**
25 * Call the init function of each supported filter.
26 *
27 * \param all_filters the array of all supported filters.
28 *
29 * \sa filter::init
30 */
31 void filter_init(void)
32 {
33 int i;
34
35 FOR_EACH_SUPPORTED_FILTER(i)
36 filters[i].init(filters + i);
37 }
38
39 /**
40 * Close and destroy a filter callback.
41 *
42 * \param fcb The filter callback to close.
43 *
44 * This removes \a fcb from the list of filter callbacks and calls
45 * the close callback associated with \a fcb.
46 */
47 static void close_filter_callback(struct filter_callback *fcb)
48 {
49 PARA_NOTICE_LOG("closing filter_callback %p, data: %p\n", fcb, fcb->data);
50 list_del(&fcb->node);
51 fcb->close(fcb);
52 }
53
54 /**
55 * Close all callbacks of a filter node.
56 *
57 * \param fn The filter node which contains the filter callbacks to be closed.
58 *
59 * Call close_filter_callback() for each entry in the filter callback list
60 * of \a fn.
61 */
62 static void close_callbacks(struct filter_node *fn)
63 {
64 struct filter_callback *fcb, *tmp;
65
66 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
67 PARA_INFO_LOG("closing %s filter callback\n",
68 filters[fn->filter_num].name);
69 close_filter_callback(fcb);
70 }
71 }
72
73 static void call_callbacks(struct filter_node *fn, char *inbuf, size_t inlen,
74 char *outbuf, size_t outlen)
75 {
76 struct filter_callback *fcb, *tmp;
77 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
78 int ret;
79 if (inlen && fcb->input_cb) {
80 ret = fcb->input_cb(inbuf, inlen, fcb);
81 if (ret < 0) {
82 close_filter_callback(fcb);
83 continue;
84 }
85 }
86 if (!outlen || !fcb->output_cb)
87 continue;
88 ret = fcb->output_cb(outbuf, outlen, fcb);
89 if (ret < 0)
90 close_filter_callback(fcb);
91 }
92 }
93
94 /**
95 * Call the convert function of each filter.
96 *
97 * \param s Unused.
98 * \param t The task identifying the filter chain.
99 *
100 * This is the core function of the filter subsystem. It loops over the list of
101 * filter nodes determined by \a t 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.
106 *
107 * On errors a (negative) error code is stored in t->error.
108 *
109 * \sa filter_node, filter#convert, filter_callback.
110 */
111 void filter_pre_select(__a_unused struct sched *s, struct task *t)
112 {
113 struct filter_chain *fc = container_of(t, struct filter_chain, task);
114 struct filter_node *fn;
115 char *ib;
116 size_t *loaded;
117 int i, conv, conv_total = 0;
118
119 if (fc->output_error && *fc->output_error < 0) {
120 t->error = *fc->output_error;
121 return;
122 }
123 again:
124 ib = fc->inbuf;
125 loaded = fc->in_loaded;
126 conv = 0;
127 FOR_EACH_FILTER_NODE(fn, fc, i) {
128 struct filter *f = filters + fn->filter_num;
129 if (fn->loaded < fn->bufsize) {
130 size_t size, old_fn_loaded = fn->loaded;
131 // PARA_DEBUG_LOG("fc %p loaded: %zd, calling %s convert\n",
132 // fc, *loaded, fn->filter->name);
133 t->error = f->convert(ib, *loaded, fn);
134 if (t->error < 0)
135 return;
136 size = t->error;
137 call_callbacks(fn, ib, size, fn->buf + old_fn_loaded,
138 fn->loaded - old_fn_loaded);
139 *loaded -= size;
140 conv += size;
141 if (*loaded && size) {
142 // PARA_DEBUG_LOG("moving %zd bytes in input "
143 // "buffer for %s filter\n",
144 // *loaded, fn->filter->name);
145 memmove(ib, ib + size, *loaded);
146 }
147 }
148 ib = fn->buf;
149 loaded = &fn->loaded;
150 }
151 conv_total += conv;
152 // PARA_DEBUG_LOG("eof (in/out/fc): %d/%d/%d out_loaded: %zd, "
153 // "conv: %d, conv_total: %d\n", *fc->input_eof,
154 // fc->output_eof? *fc->output_eof : -42,
155 // fc->eof, *fc->out_loaded, conv, conv_total);
156 if (conv)
157 goto again;
158 if (*fc->input_error >= 0)
159 return;
160 if (*fc->out_loaded)
161 return;
162 if (*fc->in_loaded && conv_total)
163 return;
164 t->error = -E_FC_EOF;
165 }
166
167 /**
168 * Close all filter nodes and their callbacks.
169 *
170 * \param fc The filter chain to close.
171 *
172 * For each filter node determined by \a fc, call the close function of each
173 * registered filter callback as well as the close function of the
174 * corresponding filter. Free all resources and destroy all callback lists and
175 * the filter node list.
176 *
177 * \sa filter::close, filter_callback::close
178 */
179 void close_filters(struct filter_chain *fc)
180 {
181 struct filter_node *fn;
182 int i;
183
184 if (!fc)
185 return;
186 PARA_NOTICE_LOG("closing filter chain %p\n", fc);
187 FOR_EACH_FILTER_NODE(fn, fc, i) {
188 struct filter *f = filters + fn->filter_num;
189 close_callbacks(fn);
190 PARA_INFO_LOG("closing %s filter\n", f->name);
191 f->close(fn);
192 }
193 free(fc->filter_nodes);
194 }
195
196 /*
197 * If the filter has a command line parser and options is not NULL, run it.
198 * Returns filter_num on success, negative on errors
199 */
200 static int parse_filter_args(int filter_num, char *options, void **conf)
201 {
202 struct filter *f = &filters[filter_num];
203 int ret, i, argc = 2;
204 char **argv;
205
206 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
207 // options? options : "(none)", f->parse_config);
208 if (!f->parse_config)
209 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
210 // PARA_DEBUG_LOG("options: %s\n", options);
211 argc = split_args(options, &argv, " \t");
212 // PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
213 for (i = argc - 1; i >= 0; i--)
214 argv[i + 1] = argv[i];
215 argv[0] = para_strdup(f->name);
216 argc += 1;
217 ret = f->parse_config(argc, argv, conf);
218 free(argv[0]);
219 free(argv);
220 return ret < 0? ret : filter_num;
221 }
222
223 /**
224 * Check the filter command line options.
225 *
226 * \param fa The command line options.
227 * \param conf Points to the filter configuration upon successful return.
228 *
229 * Check if \a fa starts with a the name of a supported filter, followed by
230 * a colon. If yes, call the command line parser of that filter.
231 *
232 * \return On success, the number of the filter is returned and \a conf
233 * is initialized to point to the filter configuration determined by \a fa.
234 * On errors, a negative value is returned.
235 *
236 * Note: If \a fa specifies a filter that has no command line parser success is
237 * returned, and \a conf is initialized to \p NULL.
238 *
239 * \sa filter::parse_config
240 */
241 int check_filter_arg(char *fa, void **conf)
242 {
243 int j;
244
245 *conf = NULL;
246 // PARA_DEBUG_LOG("arg: %s\n", fa);
247 FOR_EACH_SUPPORTED_FILTER(j) {
248 const char *name = filters[j].name;
249 size_t len = strlen(name);
250 char c;
251 if (strlen(fa) < len)
252 continue;
253 if (strncmp(name, fa, len))
254 continue;
255 c = fa[len];
256 if (c && c != ' ')
257 continue;
258 if (c && !filters[j].parse_config)
259 return -E_BAD_FILTER_OPTIONS;
260 return parse_filter_args(j, c? fa + len + 1 :
261 fa + strlen(fa), conf);
262 }
263 return -E_UNSUPPORTED_FILTER;
264 }
265
266 void print_filter_helps(int detailed)
267 {
268 int i;
269
270 printf_or_die("\nAvailable filters: \n\t");
271 FOR_EACH_SUPPORTED_FILTER(i)
272 printf_or_die("%s%s", i? " " : "", filters[i].name);
273 printf_or_die("\n\n");
274
275 FOR_EACH_SUPPORTED_FILTER(i) {
276 struct filter *f = filters + i;
277
278 if (!f->help.short_help)
279 continue;
280 printf_or_die("Options for %s:\n", f->name);
281 ggo_print_help(&f->help, detailed);
282 }
283
284 }