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. */
10 #include <sys/types.h>
22 /** The array of supported filters. */
23 struct filter filters
[NUM_SUPPORTED_FILTERS
] = {FILTER_ARRAY
};
26 * Call the init function of each supported filter.
29 void filter_init(void)
33 FOR_EACH_SUPPORTED_FILTER(i
)
34 filters
[i
].init(filters
+ i
);
38 * Close and destroy a filter callback.
40 * \param fcb The filter callback to close.
42 * This removes \a fcb from the list of filter callbacks and calls
43 * the close callback associated with \a fcb.
45 static void close_filter_callback(struct filter_callback
*fcb
)
47 PARA_NOTICE_LOG("closing filter_callback %p\n", fcb
);
53 * Close all callbacks of a filter node.
55 * \param fn The filter node which contains the filter callbacks to be closed.
57 * Call close_filter_callback() for each entry in the filter callback list
60 static void close_callbacks(struct filter_node
*fn
)
62 struct filter_callback
*fcb
, *tmp
;
64 list_for_each_entry_safe(fcb
, tmp
, &fn
->callbacks
, node
) {
65 PARA_INFO_LOG("closing %s filter callback\n",
66 filters
[fn
->filter_num
].name
);
67 close_filter_callback(fcb
);
71 static void call_callbacks(struct filter_node
*fn
, char *inbuf
, size_t inlen
,
72 char *outbuf
, size_t outlen
)
74 struct filter_callback
*fcb
, *tmp
;
75 list_for_each_entry_safe(fcb
, tmp
, &fn
->callbacks
, node
) {
77 if (inlen
&& fcb
->input_cb
) {
78 ret
= fcb
->input_cb(inbuf
, inlen
, fcb
);
80 close_filter_callback(fcb
);
84 if (!outlen
|| !fcb
->output_cb
)
86 ret
= fcb
->output_cb(outbuf
, outlen
, fcb
);
88 close_filter_callback(fcb
);
93 * Call the convert function of each filter.
96 * \param t The task identifying the filter chain.
98 * This is the core function of the filter subsystem. It loops over the list of
99 * filter nodes determined by \a t and calls the filter's convert function if
100 * there is input available for the filter node in question. If the convert
101 * function consumed some or all of its input data, all registered input
102 * callbacks are called. Similarly, if a convert function produced output, all
103 * registered output callbacks get called.
105 * On errors a (negative) error code is stored in t->error.
107 * \sa filter_node, filter#convert, filter_callback.
109 void filter_post_select(__a_unused
struct sched
*s
, struct task
*t
)
111 struct filter_chain
*fc
= container_of(t
, struct filter_chain
, task
);
112 struct filter_node
*fn
;
115 int i
, conv
, conv_total
= 0;
117 if (fc
->output_error
&& *fc
->output_error
< 0) {
118 t
->error
= *fc
->output_error
;
123 loaded
= fc
->in_loaded
;
125 FOR_EACH_FILTER_NODE(fn
, fc
, i
) {
126 struct filter
*f
= filters
+ fn
->filter_num
;
127 if (fn
->loaded
< fn
->bufsize
) {
128 size_t size
, old_fn_loaded
= fn
->loaded
;
129 t
->error
= f
->convert(ib
, *loaded
, fn
);
133 call_callbacks(fn
, ib
, size
, fn
->buf
+ old_fn_loaded
,
134 fn
->loaded
- old_fn_loaded
);
136 conv
+= size
+ fn
->loaded
- old_fn_loaded
;
138 memmove(ib
, ib
+ size
, *loaded
);
141 loaded
= &fn
->loaded
;
146 if (*fc
->input_error
>= 0)
150 if (*fc
->in_loaded
&& conv_total
)
152 t
->error
= -E_FC_EOF
;
156 * Close all filter nodes and their callbacks.
158 * \param fc The filter chain to close.
160 * For each filter node determined by \a fc, call the close function of each
161 * registered filter callback as well as the close function of the
162 * corresponding filter. Free all resources and destroy all callback lists and
163 * the filter node list.
165 * \sa filter::close, filter_callback::close
167 void close_filters(struct filter_chain
*fc
)
169 struct filter_node
*fn
;
174 PARA_NOTICE_LOG("closing filter chain %p\n", fc
);
175 FOR_EACH_FILTER_NODE(fn
, fc
, i
) {
176 struct filter
*f
= filters
+ fn
->filter_num
;
178 PARA_INFO_LOG("closing %s filter\n", f
->name
);
181 free(fc
->filter_nodes
);
185 * If the filter has a command line parser and options is not NULL, run it.
186 * Returns filter_num on success, negative on errors
188 static int parse_filter_args(int filter_num
, char *options
, void **conf
)
190 struct filter
*f
= &filters
[filter_num
];
191 int ret
, i
, argc
= 2;
194 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
195 // options? options : "(none)", f->parse_config);
196 if (!f
->parse_config
)
197 return strlen(options
)? -E_BAD_FILTER_OPTIONS
: filter_num
;
198 // PARA_DEBUG_LOG("options: %s\n", options);
199 argc
= create_argv(options
, " \t", &argv
);
201 return -E_BAD_FILTER_OPTIONS
;
202 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc
, argv
[0]);
203 for (i
= argc
- 1; i
>= 0; i
--)
204 argv
[i
+ 1] = argv
[i
];
205 argv
[0] = para_strdup(f
->name
);
207 ret
= f
->parse_config(argc
, argv
, conf
);
208 free(argv
[argc
- 1]);
209 argv
[argc
- 1] = NULL
;
211 return ret
< 0? ret
: filter_num
;
215 * Check the filter command line options.
217 * \param fa The command line options.
218 * \param conf Points to the filter configuration upon successful return.
220 * Check if \a fa starts with a the name of a supported filter, followed by
221 * a colon. If yes, call the command line parser of that filter.
223 * \return On success, the number of the filter is returned and \a conf
224 * is initialized to point to the filter configuration determined by \a fa.
225 * On errors, a negative value is returned.
227 * Note: If \a fa specifies a filter that has no command line parser success is
228 * returned, and \a conf is initialized to \p NULL.
230 * \sa filter::parse_config
232 int check_filter_arg(char *fa
, void **conf
)
237 // PARA_DEBUG_LOG("arg: %s\n", fa);
238 FOR_EACH_SUPPORTED_FILTER(j
) {
239 const char *name
= filters
[j
].name
;
240 size_t len
= strlen(name
);
242 if (strlen(fa
) < len
)
244 if (strncmp(name
, fa
, len
))
249 if (c
&& !filters
[j
].parse_config
)
250 return -E_BAD_FILTER_OPTIONS
;
251 return parse_filter_args(j
, c
? fa
+ len
+ 1 :
252 fa
+ strlen(fa
), conf
);
254 return -E_UNSUPPORTED_FILTER
;
257 void print_filter_helps(int detailed
)
261 printf_or_die("\nAvailable filters: \n\t");
262 FOR_EACH_SUPPORTED_FILTER(i
)
263 printf_or_die("%s%s", i
? " " : "", filters
[i
].name
);
264 printf_or_die("\n\n");
266 FOR_EACH_SUPPORTED_FILTER(i
) {
267 struct filter
*f
= filters
+ i
;
269 if (!f
->help
.short_help
)
271 printf_or_die("Options for %s:\n", f
->name
);
272 ggo_print_help(&f
->help
, detailed
);