Kill unused filter_post_select().
[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 #include <stdbool.h>
13
14 #include "para.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "fd.h"
18 #include "ggo.h"
19 #include "buffer_tree.h"
20 #include "filter.h"
21 #include "error.h"
22 #include "string.h"
23
24 /** The array of supported filters. */
25 struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
26
27 /**
28 * Call the init function of each supported filter.
29 * \sa filter::init
30 */
31 void filter_init(void)
32 {
33 int i;
34
35 FOR_EACH_SUPPORTED_FILTER(i)
36 filters[i].init(filters + i);
37 }
38
39 /**
40 * Close and destroy a filter callback.
41 *
42 * \param fcb The filter callback to close.
43 *
44 * This removes \a fcb from the list of filter callbacks and calls
45 * the close callback associated with \a fcb.
46 */
47 static void close_filter_callback(struct filter_callback *fcb)
48 {
49 PARA_NOTICE_LOG("closing filter_callback %p\n", fcb);
50 list_del(&fcb->node);
51 fcb->close(fcb);
52 }
53
54 /**
55 * Close all callbacks of a filter node.
56 *
57 * \param fn The filter node which contains the filter callbacks to be closed.
58 *
59 * Call close_filter_callback() for each entry in the filter callback list
60 * of \a fn.
61 */
62 static void close_callbacks(struct filter_node *fn)
63 {
64 struct filter_callback *fcb, *tmp;
65
66 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
67 PARA_INFO_LOG("closing %s filter callback\n",
68 filters[fn->filter_num].name);
69 close_filter_callback(fcb);
70 }
71 }
72
73 /**
74 * Close all filter nodes and their callbacks.
75 *
76 * \param fc The filter chain to close.
77 *
78 * For each filter node determined by \a fc, call the close function of each
79 * registered filter callback as well as the close function of the
80 * corresponding filter. Free all resources and destroy all callback lists and
81 * the filter node list.
82 *
83 * \sa filter::close, filter_callback::close
84 */
85 void close_filters(struct filter_chain *fc)
86 {
87 struct filter_node *fn;
88 int i;
89
90 if (!fc)
91 return;
92 PARA_NOTICE_LOG("closing filter chain %p\n", fc);
93 FOR_EACH_FILTER_NODE(fn, fc, i) {
94 struct filter *f = filters + fn->filter_num;
95 close_callbacks(fn);
96 PARA_INFO_LOG("closing %s filter\n", f->name);
97 f->close(fn);
98 free(fn->conf);
99 }
100 free(fc->filter_nodes);
101 }
102
103 /*
104 * If the filter has a command line parser and options is not NULL, run it.
105 * Returns filter_num on success, negative on errors
106 */
107 static int parse_filter_args(int filter_num, char *options, void **conf)
108 {
109 struct filter *f = &filters[filter_num];
110 int ret, i, argc = 2;
111 char **argv;
112
113 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
114 // options? options : "(none)", f->parse_config);
115 if (!f->parse_config)
116 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
117 // PARA_DEBUG_LOG("options: %s\n", options);
118 argc = create_argv(options, " \t", &argv);
119 if (argc < 0)
120 return -E_BAD_FILTER_OPTIONS;
121 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
122 for (i = argc - 1; i >= 0; i--)
123 argv[i + 1] = argv[i];
124 argv[0] = para_strdup(f->name);
125 argc++;
126 ret = f->parse_config(argc, argv, conf);
127 free(argv[argc - 1]);
128 argv[argc - 1] = NULL;
129 free_argv(argv);
130 return ret < 0? ret : filter_num;
131 }
132
133 /**
134 * Check the filter command line options.
135 *
136 * \param fa The command line options.
137 * \param conf Points to the filter configuration upon successful return.
138 *
139 * Check if \a fa starts with a the name of a supported filter, followed by
140 * a colon. If yes, call the command line parser of that filter.
141 *
142 * \return On success, the number of the filter is returned and \a conf
143 * is initialized to point to the filter configuration determined by \a fa.
144 * On errors, a negative value is returned.
145 *
146 * Note: If \a fa specifies a filter that has no command line parser success is
147 * returned, and \a conf is initialized to \p NULL.
148 *
149 * \sa filter::parse_config
150 */
151 int check_filter_arg(char *fa, void **conf)
152 {
153 int j;
154
155 *conf = NULL;
156 // PARA_DEBUG_LOG("arg: %s\n", fa);
157 FOR_EACH_SUPPORTED_FILTER(j) {
158 const char *name = filters[j].name;
159 size_t len = strlen(name);
160 char c;
161 if (strlen(fa) < len)
162 continue;
163 if (strncmp(name, fa, len))
164 continue;
165 c = fa[len];
166 if (c && c != ' ')
167 continue;
168 if (c && !filters[j].parse_config)
169 return -E_BAD_FILTER_OPTIONS;
170 return parse_filter_args(j, c? fa + len + 1 :
171 fa + strlen(fa), conf);
172 }
173 return -E_UNSUPPORTED_FILTER;
174 }
175
176 void print_filter_helps(int detailed)
177 {
178 int i;
179
180 printf_or_die("\nAvailable filters: \n\t");
181 FOR_EACH_SUPPORTED_FILTER(i)
182 printf_or_die("%s%s", i? " " : "", filters[i].name);
183 printf_or_die("\n\n");
184
185 FOR_EACH_SUPPORTED_FILTER(i) {
186 struct filter *f = filters + i;
187
188 if (!f->help.short_help)
189 continue;
190 printf_or_die("Options for %s:\n", f->name);
191 ggo_print_help(&f->help, detailed);
192 }
193
194 }
195
196 void generic_filter_pre_select(struct sched *s, struct task *t)
197 {
198 struct filter_node *fn = container_of(t, struct filter_node, task);
199
200 t->error = 0;
201 if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0) {
202 s->timeout.tv_sec = 0;
203 s->timeout.tv_usec = 1;
204 }
205 }
206