fbc5b66b857d72c730c298737fc7394d2c8c4261
[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 * \sa filter::init
27 */
28 void filter_init(void)
29 {
30 int i;
31
32 FOR_EACH_SUPPORTED_FILTER(i)
33 filters[i].init(filters + i);
34 }
35
36 /**
37 * Close and destroy a filter callback.
38 *
39 * \param fcb The filter callback to close.
40 *
41 * This removes \a fcb from the list of filter callbacks and calls
42 * the close callback associated with \a fcb.
43 */
44 static void close_filter_callback(struct filter_callback *fcb)
45 {
46 PARA_NOTICE_LOG("closing filter_callback %p\n", fcb);
47 list_del(&fcb->node);
48 fcb->close(fcb);
49 }
50
51 /**
52 * Close all callbacks of a filter node.
53 *
54 * \param fn The filter node which contains the filter callbacks to be closed.
55 *
56 * Call close_filter_callback() for each entry in the filter callback list
57 * of \a fn.
58 */
59 static void close_callbacks(struct filter_node *fn)
60 {
61 struct filter_callback *fcb, *tmp;
62
63 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
64 PARA_INFO_LOG("closing %s filter callback\n",
65 filters[fn->filter_num].name);
66 close_filter_callback(fcb);
67 }
68 }
69
70 static void call_callbacks(struct filter_node *fn, char *inbuf, size_t inlen,
71 char *outbuf, size_t outlen)
72 {
73 struct filter_callback *fcb, *tmp;
74 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
75 int ret;
76 if (inlen && fcb->input_cb) {
77 ret = fcb->input_cb(inbuf, inlen, fcb);
78 if (ret < 0) {
79 close_filter_callback(fcb);
80 continue;
81 }
82 }
83 if (!outlen || !fcb->output_cb)
84 continue;
85 ret = fcb->output_cb(outbuf, outlen, fcb);
86 if (ret < 0)
87 close_filter_callback(fcb);
88 }
89 }
90
91 /**
92 * Call the convert function of each filter.
93 *
94 * \param s Unused.
95 * \param t The task identifying the filter chain.
96 *
97 * This is the core function of the filter subsystem. It loops over the list of
98 * filter nodes determined by \a t and calls the filter's convert function if
99 * there is input available for the filter node in question. If the convert
100 * function consumed some or all of its input data, all registered input
101 * callbacks are called. Similarly, if a convert function produced output, all
102 * registered output callbacks get called.
103 *
104 * On errors a (negative) error code is stored in t->error.
105 *
106 * \sa filter_node, filter#convert, filter_callback.
107 */
108 void filter_pre_select(__a_unused struct sched *s, struct task *t)
109 {
110 struct filter_chain *fc = container_of(t, struct filter_chain, task);
111 struct filter_node *fn;
112 char *ib;
113 size_t *loaded;
114 int i, conv, conv_total = 0;
115
116 if (fc->output_error && *fc->output_error < 0) {
117 t->error = *fc->output_error;
118 return;
119 }
120 again:
121 ib = *fc->inbufp;
122 loaded = fc->in_loaded;
123 conv = 0;
124 FOR_EACH_FILTER_NODE(fn, fc, i) {
125 struct filter *f = filters + fn->filter_num;
126 if (fn->loaded < fn->bufsize) {
127 size_t size, old_fn_loaded = fn->loaded;
128 t->error = f->convert(ib, *loaded, fn);
129 if (t->error < 0)
130 return;
131 size = t->error;
132 call_callbacks(fn, ib, size, fn->buf + old_fn_loaded,
133 fn->loaded - old_fn_loaded);
134 *loaded -= size;
135 conv += size + fn->loaded - old_fn_loaded;
136 if (*loaded && size)
137 memmove(ib, ib + size, *loaded);
138 }
139 ib = fn->buf;
140 loaded = &fn->loaded;
141 }
142 conv_total += conv;
143 if (conv)
144 goto again;
145 if (conv_total) {
146 /*
147 * Other pre_select functions might have already been called by
148 * now and decided to do nothing, e.g. because their output
149 * buffer was full or the input buffer was empty. We just
150 * converted something which caused these buffers to change but
151 * we can't make the other tasks reconsider their decision at
152 * this point. So force a minimal timeout for the next select
153 * call to avoid unnecessary delays.
154 */
155 s->timeout.tv_sec = 0;
156 s->timeout.tv_usec = 1;
157 }
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 = create_argv(options, " \t", &argv);
212 if (argc < 0)
213 return -E_BAD_FILTER_OPTIONS;
214 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
215 for (i = argc - 1; i >= 0; i--)
216 argv[i + 1] = argv[i];
217 argv[0] = para_strdup(f->name);
218 argc++;
219 ret = f->parse_config(argc, argv, conf);
220 free(argv[argc - 1]);
221 argv[argc - 1] = NULL;
222 free_argv(argv);
223 return ret < 0? ret : filter_num;
224 }
225
226 /**
227 * Check the filter command line options.
228 *
229 * \param fa The command line options.
230 * \param conf Points to the filter configuration upon successful return.
231 *
232 * Check if \a fa starts with a the name of a supported filter, followed by
233 * a colon. If yes, call the command line parser of that filter.
234 *
235 * \return On success, the number of the filter is returned and \a conf
236 * is initialized to point to the filter configuration determined by \a fa.
237 * On errors, a negative value is returned.
238 *
239 * Note: If \a fa specifies a filter that has no command line parser success is
240 * returned, and \a conf is initialized to \p NULL.
241 *
242 * \sa filter::parse_config
243 */
244 int check_filter_arg(char *fa, void **conf)
245 {
246 int j;
247
248 *conf = NULL;
249 // PARA_DEBUG_LOG("arg: %s\n", fa);
250 FOR_EACH_SUPPORTED_FILTER(j) {
251 const char *name = filters[j].name;
252 size_t len = strlen(name);
253 char c;
254 if (strlen(fa) < len)
255 continue;
256 if (strncmp(name, fa, len))
257 continue;
258 c = fa[len];
259 if (c && c != ' ')
260 continue;
261 if (c && !filters[j].parse_config)
262 return -E_BAD_FILTER_OPTIONS;
263 return parse_filter_args(j, c? fa + len + 1 :
264 fa + strlen(fa), conf);
265 }
266 return -E_UNSUPPORTED_FILTER;
267 }
268
269 void print_filter_helps(int detailed)
270 {
271 int i;
272
273 printf_or_die("\nAvailable filters: \n\t");
274 FOR_EACH_SUPPORTED_FILTER(i)
275 printf_or_die("%s%s", i? " " : "", filters[i].name);
276 printf_or_die("\n\n");
277
278 FOR_EACH_SUPPORTED_FILTER(i) {
279 struct filter *f = filters + i;
280
281 if (!f->help.short_help)
282 continue;
283 printf_or_die("Options for %s:\n", f->name);
284 ggo_print_help(&f->help, detailed);
285 }
286
287 }