2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file filter.c The stand-alone filter program. */
13 #include "filter.cmdline.h"
17 #include "buffer_tree.h"
24 /** The list of all status items used by para_{server,audiod,gui}. */
25 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
27 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
29 /** Initialize the array of errors for para_filter. */
32 /** The task that reads from stdin. */
33 static struct stdin_task stdin_task_struct;
34 /** pointer to the stdin task. */
35 static struct stdin_task *sit = &stdin_task_struct;
37 /** The task that filters the data. */
38 static struct filter_chain filter_chain_struct;
39 /** Pointer to the filter chain. */
40 static struct filter_chain *fc = &filter_chain_struct;
42 /** The task that writes converted data to stdout. */
43 static struct stdout_task stdout_task_struct;
44 /** Pointer to the stdout task. */
45 static struct stdout_task *sot = &stdout_task_struct;
47 /** Gengetopt struct that holds the command line args. */
48 static struct filter_args_info conf;
51 INIT_STDERR_LOGGING(loglevel);
53 static void open_filters(void)
56 struct filter_node *fn;
58 FOR_EACH_FILTER_NODE(fn, fc, i) {
59 struct filter *f = filters + fn->filter_num;
61 PARA_INFO_LOG("opened %s filter\n", f->name);
62 fc->outbufp = &fn->buf;
63 fc->out_loaded = &fn->loaded;
67 static void free_filter_confs(void)
70 struct filter_node *fn;
72 FOR_EACH_FILTER_NODE(fn, fc, i)
76 static int init_filter_chain(void)
79 struct filter_node *fn;
81 if (!conf.filter_given)
83 fc->num_filters = conf.filter_given;
84 fc->filter_nodes = para_calloc(fc->num_filters * sizeof(struct filter_node));
85 fc->inbufp = &sit->buf;
86 fc->in_loaded = &sit->loaded;
87 fc->input_error = &sit->task.error;
89 fc->output_error = &sot->task.error;
90 fc->task.post_select = filter_post_select;
91 sprintf(fc->task.status, "filter chain");
93 FOR_EACH_FILTER_NODE(fn, fc, i) {
94 char *fa = conf.filter_arg[i];
95 fn = fc->filter_nodes + i;
96 ret = check_filter_arg(fa, &fn->conf);
101 INIT_LIST_HEAD(&fn->callbacks);
102 PARA_DEBUG_LOG("filter #%d: %s\n", i, filters[fn->filter_num].name);
108 free(fc->filter_nodes);
112 __noreturn static void print_help_and_die(void)
114 int d = conf.detailed_help_given;
115 const char **p = d? filter_args_info_detailed_help
116 : filter_args_info_help;
118 printf_or_die("%s\n\n", FILTER_CMDLINE_PARSER_PACKAGE "-"
119 FILTER_CMDLINE_PARSER_VERSION);
120 printf_or_die("%s\n\n", filter_args_info_usage);
122 printf_or_die("%s\n", *p);
123 print_filter_helps(d);
127 static int parse_config(int argc, char *argv[])
129 static char *cf; /* config file */
132 if (filter_cmdline_parser(argc, argv, &conf))
133 return -E_FILTER_SYNTAX;
134 HANDLE_VERSION_FLAG("filter", conf);
135 if (conf.help_given || conf.detailed_help_given)
136 print_help_and_die();
137 loglevel = get_loglevel_by_name(conf.loglevel_arg);
139 char *home = para_homedir();
140 cf = make_message("%s/.paraslash/filter.conf", home);
143 if (!stat(cf, &statbuf)) {
144 struct filter_cmdline_parser_params params = {
148 .check_ambiguity = 0,
151 if (filter_cmdline_parser_config_file(cf, &conf, ¶ms))
152 return -E_FILTER_SYNTAX;
157 /* TODO: support more than one filter, actually parse options */
158 static int __noreturn main_btr(void)
160 static struct sched s;
163 struct btr_node *parent;
164 struct filter_node **fns;
166 sit->btrn = btr_new_node("stdin", NULL, NULL, NULL);
167 stdin_set_defaults(sit);
168 register_task(&sit->task);
170 fns = para_malloc(conf.filter_given * sizeof(*fns));
171 for (i = 0, parent = sit->btrn; i < conf.filter_given; i++) {
172 char *fa = conf.filter_arg[i];
173 struct filter_node *fn;
175 fn = fns[i] = para_calloc(sizeof(*fn));
176 ret = check_filter_arg(fa, &fn->conf);
181 fn->filter_num = ret;
182 f = filters + fn->filter_num;
183 sprintf(fn->task.status, "%s", f->name);
184 PARA_DEBUG_LOG("filter #%d: %s\n", i, f->name);
185 fn->btrn = btr_new_node(f->name, parent, f->execute, fn);
186 fn->task.pre_select = f->pre_select;
187 fn->task.post_select = f->post_select;
189 register_task(&fn->task);
192 sot->btrn = btr_new_node("stdout", parent, NULL, NULL);
193 stdout_set_defaults(sot);
194 register_task(&sot->task);
196 s.default_timeout.tv_sec = 1;
197 s.default_timeout.tv_usec = 0;
198 btr_log_tree(sit->btrn, LL_INFO);
201 for (i--; i >= 0; i--) {
202 struct filter_node *fn = fns[i];
204 f = filters + fn->filter_num;
206 btr_free_node(fn->btrn);
211 btr_free_node(sit->btrn);
212 btr_free_node(sot->btrn);
214 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
215 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
219 * The main function of para_filter.
221 * Para_filter reads data from stdin, converts it by using a chain
222 * of filters (specified on the command line) and writes the resulting
225 * \param argc Number of command line options.
226 * \param argv Vector of arguments.
228 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
230 int main(int argc, char *argv[])
233 static struct sched s;
236 ret = parse_config(argc, argv);
239 if (conf.buffer_tree_given) {
243 stdin_set_defaults(sit);
244 sit->buf = para_malloc(sit->bufsize),
246 ret = init_filter_chain();
249 sit->output_error = &fc->task.error;
251 stdout_set_defaults(sot);
252 sot->bufp = fc->outbufp;
253 sot->loaded = fc->out_loaded;
254 sot->input_error = &fc->task.error;
256 register_task(&sit->task);
257 register_task(&sot->task);
258 register_task(&fc->task);
259 s.default_timeout.tv_sec = 1;
260 s.default_timeout.tv_usec = 0;
266 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
267 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;