Add btr support to filter code.
[paraslash.git] / filter.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.c The stand-alone filter program. */
8
9 #include <regex.h>
10 #include <stdbool.h>
11
12 #include "para.h"
13 #include "filter.cmdline.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "string.h"
20 #include "stdin.h"
21 #include "stdout.h"
22 #include "error.h"
23
24 /** The list of all status items used by para_{server,audiod,gui}. */
25 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
26
27 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
28
29 /** Initialize the array of errors for para_filter. */
30 INIT_FILTER_ERRLISTS;
31
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;
36
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;
41
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;
46
47 /** Gengetopt struct that holds the command line args. */
48 static struct filter_args_info conf;
49
50 static int loglevel;
51 INIT_STDERR_LOGGING(loglevel);
52
53 static void open_filters(void)
54 {
55 int i;
56 struct filter_node *fn;
57
58 FOR_EACH_FILTER_NODE(fn, fc, i) {
59 struct filter *f = filters + fn->filter_num;
60 f->open(fn);
61 PARA_INFO_LOG("opened %s filter\n", f->name);
62 fc->outbufp = &fn->buf;
63 fc->out_loaded = &fn->loaded;
64 }
65 }
66
67 static void free_filter_confs(void)
68 {
69 int i;
70 struct filter_node *fn;
71
72 FOR_EACH_FILTER_NODE(fn, fc, i)
73 free(fn->conf);
74 }
75
76 static int init_filter_chain(void)
77 {
78 int i, ret;
79 struct filter_node *fn;
80
81 if (!conf.filter_given)
82 return -E_NO_FILTERS;
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;
88 fc->task.error = 0;
89 fc->output_error = &sot->task.error;
90 fc->task.post_select = filter_post_select;
91 sprintf(fc->task.status, "filter chain");
92
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);
97 if (ret < 0)
98 goto err;
99 fn->filter_num = ret;
100 fn->fc = fc;
101 INIT_LIST_HEAD(&fn->callbacks);
102 PARA_DEBUG_LOG("filter #%d: %s\n", i, filters[fn->filter_num].name);
103 }
104 open_filters();
105 return 1;
106 err:
107 free_filter_confs();
108 free(fc->filter_nodes);
109 return ret;
110 }
111
112 __noreturn static void print_help_and_die(void)
113 {
114 int d = conf.detailed_help_given;
115 const char **p = d? filter_args_info_detailed_help
116 : filter_args_info_help;
117
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);
121 for (; *p; p++)
122 printf_or_die("%s\n", *p);
123 print_filter_helps(d);
124 exit(0);
125 }
126
127 static int parse_config(int argc, char *argv[])
128 {
129 static char *cf; /* config file */
130 struct stat statbuf;
131
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);
138 if (!cf) {
139 char *home = para_homedir();
140 cf = make_message("%s/.paraslash/filter.conf", home);
141 free(home);
142 }
143 if (!stat(cf, &statbuf)) {
144 struct filter_cmdline_parser_params params = {
145 .override = 0,
146 .initialize = 0,
147 .check_required = 0,
148 .check_ambiguity = 0,
149 .print_errors = 1
150 };
151 if (filter_cmdline_parser_config_file(cf, &conf, &params))
152 return -E_FILTER_SYNTAX;
153 }
154 return 1;
155 }
156
157 /* TODO: support more than one filter, actually parse options */
158 static int __noreturn main_btr(int argc, char *argv[])
159 {
160 static struct sched s;
161 struct filter_node *fn = para_calloc(sizeof(*fn));
162 int ret;
163 struct filter *f;
164
165 sit->btrn = btr_new_node("stdin", NULL, NULL, NULL);
166 stdin_set_defaults(sit);
167 register_task(&sit->task);
168
169 ret = check_filter_arg("mp3dec ", &fn->conf);
170 if (ret < 0)
171 goto err;
172 fn->filter_num = ret;
173 f = filters + fn->filter_num;
174 fn->btrn = btr_new_node(f->name, sit->btrn, f->execute, fn);
175 fn->task.pre_select = f->pre_select;
176 fn->task.post_select = f->post_select;
177 sprintf(fn->task.status, "mp3dec");
178 f->open(fn);
179 register_task(&fn->task);
180
181 sot->btrn = btr_new_node("stdout", fn->btrn, NULL, NULL);
182 stdout_set_defaults(sot);
183 register_task(&sot->task);
184
185 s.default_timeout.tv_sec = 1;
186 s.default_timeout.tv_usec = 0;
187 ret = schedule(&s);
188 err:
189 free(fn);
190 if (ret < 0)
191 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
192 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
193 }
194
195 /**
196 * The main function of para_filter.
197 *
198 * Para_filter reads data from stdin, converts it by using a chain
199 * of filters (specified on the command line) and writes the resulting
200 * data to stdout.
201 *
202 * \param argc Number of command line options.
203 * \param argv Vector of arguments.
204 *
205 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
206 */
207 int main(int argc, char *argv[])
208 {
209 int ret;
210 static struct sched s;
211
212 filter_init();
213 ret = parse_config(argc, argv);
214 if (ret < 0)
215 goto out;
216 if (conf.buffer_tree_given) {
217 ret = main_btr(argc, argv);
218 goto out;
219 }
220 stdin_set_defaults(sit);
221 sit->buf = para_malloc(sit->bufsize),
222
223 ret = init_filter_chain();
224 if (ret < 0)
225 goto out;
226 sit->output_error = &fc->task.error;
227
228 stdout_set_defaults(sot);
229 sot->bufp = fc->outbufp;
230 sot->loaded = fc->out_loaded;
231 sot->input_error = &fc->task.error;
232
233 register_task(&sit->task);
234 register_task(&sot->task);
235 register_task(&fc->task);
236 s.default_timeout.tv_sec = 1;
237 s.default_timeout.tv_usec = 0;
238 ret = schedule(&s);
239 free_filter_confs();
240 close_filters(fc);
241 out:
242 free(sit->buf);
243 if (ret < 0)
244 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
245 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
246 }