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