para_server: Reformat help text of the --afs_database_dir option.
[paraslash.git] / filter_chain.c
1 /*
2  * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file filter_chain.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 "filter.h"
17 #include "error.h"
18 #include "string.h"
19
20 DEFINE_FILTER_ARRAY(filters);
21
22 /**
23  * Call the init function of each supported filter.
24  *
25  * \param all_filters the array of all supported filters.
26  *
27  * \sa filter::init
28  */
29 void filter_init(struct filter *all_filters)
30 {
31         struct filter *f;
32
33         for (f = all_filters; f->name; f++)
34                 f->init(f);
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, data: %p\n", fcb, fcb->data);
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  * registerd 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->inbuf;
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 //                      PARA_DEBUG_LOG("fc %p loaded: %zd, calling %s convert\n",
130 //                              fc, *loaded, fn->filter->name);
131                         t->error = f->convert(ib, *loaded, fn);
132                         if (t->error < 0)
133                                 return;
134                         size = t->error;
135                         call_callbacks(fn, ib, size, fn->buf + old_fn_loaded,
136                                 fn->loaded - old_fn_loaded);
137                         *loaded -= size;
138                         conv += size;
139                         if (*loaded && size) {
140 //                              PARA_DEBUG_LOG("moving %zd bytes in input "
141 //                                      "buffer for %s filter\n",
142 //                                      *loaded,  fn->filter->name);
143                                 memmove(ib, ib + size, *loaded);
144                         }
145                 }
146                 ib = fn->buf;
147                 loaded = &fn->loaded;
148         }
149         conv_total += conv;
150 //      PARA_DEBUG_LOG("eof (in/out/fc): %d/%d/%d out_loaded: %zd, "
151 //              "conv: %d, conv_total: %d\n", *fc->input_eof,
152 //              fc->output_eof? *fc->output_eof : -42,
153 //              fc->eof, *fc->out_loaded, conv, conv_total);
154         if (conv)
155                 goto again;
156         if (!*fc->input_error)
157                 return;
158         if (*fc->out_loaded)
159                 return;
160         if (*fc->in_loaded && conv_total)
161                 return;
162         t->error = -E_FC_EOF;
163 }
164
165 /**
166  * Close all filter nodes and their callbacks.
167  *
168  * \param fc The filter chain to close.
169  *
170  * For each filter node determined by \a fc, call the close function of each
171  * registered filter callback as well as the close function of the
172  * corresponding filter.  Free all resources and destroy all callback lists and
173  * the filter node list.
174  *
175  * \sa filter::close, filter_callback::close
176  */
177 void close_filters(struct filter_chain *fc)
178 {
179         struct filter_node *fn;
180         int i;
181
182         if (!fc)
183                 return;
184         PARA_NOTICE_LOG("closing filter chain %p\n", fc);
185         FOR_EACH_FILTER_NODE(fn, fc, i) {
186                 struct filter *f = filters + fn->filter_num;
187                 close_callbacks(fn);
188                 PARA_INFO_LOG("closing %s filter\n", f->name);
189                 f->close(fn);
190         }
191         free(fc->filter_nodes);
192 }
193
194 /*
195  * If the filter has a command line parser and options is not NULL, run it.
196  * Returns filter_num on success, negative on errors
197  */
198 static int parse_filter_args(int filter_num, char *options, void **conf)
199 {
200         struct filter *f = &filters[filter_num];
201         int i, argc = 2;
202         char **argv;
203
204 //      PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
205 //              options? options : "(none)", f->parse_config);
206         if (!f->parse_config)
207                 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
208 //      PARA_DEBUG_LOG("options: %s\n", options);
209         argc = split_args(options, &argv, " \t");
210 //              PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
211         for (i = argc - 1; i >= 0; i--)
212                 argv[i + 1] = argv[i];
213         argv[0] = para_strdup(f->name);
214         argc += 1;
215         *conf = f->parse_config(argc, argv);
216         free(argv[0]);
217         free(argv);
218         return *conf? filter_num : -E_BAD_FILTER_OPTIONS;
219 }
220
221 /**
222  * Check the filter command line options.
223  *
224  * \param fa The command line options.
225  * \param conf Points to the filter configuration upon successful return.
226  *
227  * Check if \a fa starts with a the name of a supported filter, followed by
228  * a colon. If yes, call the command line parser of that filter.
229  *
230  * \return On success, the number of the filter is returned and \a conf
231  * is initialized to point to the filter configuration determined by \a fa.
232  * On errors, a negative value is returned.
233  *
234  * Note: If \a fa specifies a filter that has no command line parser success is
235  * returned, and \a conf is initialized to \p NULL.
236  *
237  * \sa filter::parse_config
238  */
239 int check_filter_arg(char *fa, void **conf)
240 {
241         int j;
242
243         *conf = NULL;
244 //      PARA_DEBUG_LOG("arg: %s\n", fa);
245         for (j = 0; filters[j].name; j++) {
246                 const char *name = filters[j].name;
247                 size_t len = strlen(name);
248                 char c;
249                 if (strlen(fa) < len)
250                         continue;
251                 if (strncmp(name, fa, len))
252                         continue;
253                 c = fa[len];
254                 if (c && c != ' ')
255                         continue;
256                 if (c && !filters[j].parse_config)
257                         return -E_BAD_FILTER_OPTIONS;
258                 return parse_filter_args(j, c? fa + len + 1 :
259                         fa + strlen(fa), conf);
260         }
261         return -E_UNSUPPORTED_FILTER;
262 }
263