2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file filter_common.c Common helper functions for filter input/output. */
21 /** The array of supported filters. */
22 struct filter filters
[NUM_SUPPORTED_FILTERS
] = {FILTER_ARRAY
};
25 * Call the init function of each supported filter.
28 void filter_init(void)
32 FOR_EACH_SUPPORTED_FILTER(i
)
33 filters
[i
].init(filters
+ i
);
37 * Close and destroy a filter callback.
39 * \param fcb The filter callback to close.
41 * This removes \a fcb from the list of filter callbacks and calls
42 * the close callback associated with \a fcb.
44 static void close_filter_callback(struct filter_callback
*fcb
)
46 PARA_NOTICE_LOG("closing filter_callback %p, data: %p\n", fcb
, fcb
->data
);
52 * Close all callbacks of a filter node.
54 * \param fn The filter node which contains the filter callbacks to be closed.
56 * Call close_filter_callback() for each entry in the filter callback list
59 static void close_callbacks(struct filter_node
*fn
)
61 struct filter_callback
*fcb
, *tmp
;
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
);
70 static void call_callbacks(struct filter_node
*fn
, char *inbuf
, size_t inlen
,
71 char *outbuf
, size_t outlen
)
73 struct filter_callback
*fcb
, *tmp
;
74 list_for_each_entry_safe(fcb
, tmp
, &fn
->callbacks
, node
) {
76 if (inlen
&& fcb
->input_cb
) {
77 ret
= fcb
->input_cb(inbuf
, inlen
, fcb
);
79 close_filter_callback(fcb
);
83 if (!outlen
|| !fcb
->output_cb
)
85 ret
= fcb
->output_cb(outbuf
, outlen
, fcb
);
87 close_filter_callback(fcb
);
92 * Call the convert function of each filter.
95 * \param t The task identifying the filter chain.
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 * registerd output callbacks get called.
104 * On errors a (negative) error code is stored in t->error.
106 * \sa filter_node, filter#convert, filter_callback.
108 void filter_pre_select(__a_unused
struct sched
*s
, struct task
*t
)
110 struct filter_chain
*fc
= container_of(t
, struct filter_chain
, task
);
111 struct filter_node
*fn
;
114 int i
, conv
, conv_total
= 0;
116 if (fc
->output_error
&& *fc
->output_error
< 0) {
117 t
->error
= *fc
->output_error
;
122 loaded
= fc
->in_loaded
;
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 // PARA_DEBUG_LOG("fc %p loaded: %zd, calling %s convert\n",
129 // fc, *loaded, fn->filter->name);
130 t
->error
= f
->convert(ib
, *loaded
, fn
);
134 call_callbacks(fn
, ib
, size
, fn
->buf
+ old_fn_loaded
,
135 fn
->loaded
- old_fn_loaded
);
138 if (*loaded
&& size
) {
139 // PARA_DEBUG_LOG("moving %zd bytes in input "
140 // "buffer for %s filter\n",
141 // *loaded, fn->filter->name);
142 memmove(ib
, ib
+ size
, *loaded
);
146 loaded
= &fn
->loaded
;
149 // PARA_DEBUG_LOG("eof (in/out/fc): %d/%d/%d out_loaded: %zd, "
150 // "conv: %d, conv_total: %d\n", *fc->input_eof,
151 // fc->output_eof? *fc->output_eof : -42,
152 // fc->eof, *fc->out_loaded, conv, conv_total);
155 if (*fc
->input_error
>= 0)
159 if (*fc
->in_loaded
&& conv_total
)
161 t
->error
= -E_FC_EOF
;
165 * Close all filter nodes and their callbacks.
167 * \param fc The filter chain to close.
169 * For each filter node determined by \a fc, call the close function of each
170 * registered filter callback as well as the close function of the
171 * corresponding filter. Free all resources and destroy all callback lists and
172 * the filter node list.
174 * \sa filter::close, filter_callback::close
176 void close_filters(struct filter_chain
*fc
)
178 struct filter_node
*fn
;
183 PARA_NOTICE_LOG("closing filter chain %p\n", fc
);
184 FOR_EACH_FILTER_NODE(fn
, fc
, i
) {
185 struct filter
*f
= filters
+ fn
->filter_num
;
187 PARA_INFO_LOG("closing %s filter\n", f
->name
);
190 free(fc
->filter_nodes
);
194 * If the filter has a command line parser and options is not NULL, run it.
195 * Returns filter_num on success, negative on errors
197 static int parse_filter_args(int filter_num
, char *options
, void **conf
)
199 struct filter
*f
= &filters
[filter_num
];
200 int ret
, i
, argc
= 2;
203 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
204 // options? options : "(none)", f->parse_config);
205 if (!f
->parse_config
)
206 return strlen(options
)? -E_BAD_FILTER_OPTIONS
: filter_num
;
207 // PARA_DEBUG_LOG("options: %s\n", options);
208 argc
= split_args(options
, &argv
, " \t");
209 // PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
210 for (i
= argc
- 1; i
>= 0; i
--)
211 argv
[i
+ 1] = argv
[i
];
212 argv
[0] = para_strdup(f
->name
);
214 ret
= f
->parse_config(argc
, argv
, conf
);
217 return ret
< 0? ret
: filter_num
;
221 * Check the filter command line options.
223 * \param fa The command line options.
224 * \param conf Points to the filter configuration upon successful return.
226 * Check if \a fa starts with a the name of a supported filter, followed by
227 * a colon. If yes, call the command line parser of that filter.
229 * \return On success, the number of the filter is returned and \a conf
230 * is initialized to point to the filter configuration determined by \a fa.
231 * On errors, a negative value is returned.
233 * Note: If \a fa specifies a filter that has no command line parser success is
234 * returned, and \a conf is initialized to \p NULL.
236 * \sa filter::parse_config
238 int check_filter_arg(char *fa
, void **conf
)
243 // PARA_DEBUG_LOG("arg: %s\n", fa);
244 FOR_EACH_SUPPORTED_FILTER(j
) {
245 const char *name
= filters
[j
].name
;
246 size_t len
= strlen(name
);
248 if (strlen(fa
) < len
)
250 if (strncmp(name
, fa
, len
))
255 if (c
&& !filters
[j
].parse_config
)
256 return -E_BAD_FILTER_OPTIONS
;
257 return parse_filter_args(j
, c
? fa
+ len
+ 1 :
258 fa
+ strlen(fa
), conf
);
260 return -E_UNSUPPORTED_FILTER
;
263 void print_filter_helps(int detailed
)
267 printf_or_die("\nAvailable filters: \n\t");
268 FOR_EACH_SUPPORTED_FILTER(i
)
269 printf_or_die("%s%s", i
? " " : "", filters
[i
].name
);
270 printf_or_die("\n\n");
272 FOR_EACH_SUPPORTED_FILTER(i
) {
273 struct filter
*f
= filters
+ i
;
275 if (!f
->help
.short_help
)
277 printf_or_die("Options for %s:\n", f
->name
);
278 ggo_print_help(&f
->help
, detailed
);