]> git.tuebingen.mpg.de Git - paraslash.git/blob - filter.c
First preparations for btr support in para_filter.
[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 "filter.h"
18 #include "string.h"
19 #include "stdin.h"
20 #include "stdout.h"
21 #include "error.h"
22
23 /** The list of all status items used by para_{server,audiod,gui}. */
24 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
25
26 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
27
28 /** Initialize the array of errors for para_filter. */
29 INIT_FILTER_ERRLISTS;
30
31 /** The task that reads from stdin. */
32 static struct stdin_task stdin_task_struct;
33 /** pointer to the stdin task. */
34 static struct stdin_task *sit = &stdin_task_struct;
35
36 /** The task that filters the data. */
37 static struct filter_chain filter_chain_struct;
38 /** Pointer to the filter chain. */
39 static struct filter_chain *fc = &filter_chain_struct;
40
41 /** The task that writes converted data to stdout. */
42 static struct stdout_task stdout_task_struct;
43 /** Pointer to the stdout task. */
44 static struct stdout_task *sot = &stdout_task_struct;
45
46 /** Gengetopt struct that holds the command line args. */
47 static struct filter_args_info conf;
48
49 static int loglevel;
50 INIT_STDERR_LOGGING(loglevel);
51
52 static void open_filters(void)
53 {
54         int i;
55         struct filter_node *fn;
56
57         FOR_EACH_FILTER_NODE(fn, fc, i) {
58                 struct filter *f = filters + fn->filter_num;
59                 f->open(fn);
60                 PARA_INFO_LOG("opened %s filter\n", f->name);
61                 fc->outbufp = &fn->buf;
62                 fc->out_loaded = &fn->loaded;
63         }
64 }
65
66 static void free_filter_confs(void)
67 {
68         int i;
69         struct filter_node *fn;
70
71         FOR_EACH_FILTER_NODE(fn, fc, i)
72                 free(fn->conf);
73 }
74
75 static int init_filter_chain(void)
76 {
77         int i, ret;
78         struct filter_node *fn;
79
80         if (!conf.filter_given)
81                 return -E_NO_FILTERS;
82         fc->num_filters = conf.filter_given;
83         fc->filter_nodes = para_calloc(fc->num_filters * sizeof(struct filter_node));
84         fc->inbufp = &sit->buf;
85         fc->in_loaded = &sit->loaded;
86         fc->input_error = &sit->task.error;
87         fc->task.error = 0;
88         fc->output_error = &sot->task.error;
89         fc->task.post_select = filter_post_select;
90         sprintf(fc->task.status, "filter chain");
91
92         FOR_EACH_FILTER_NODE(fn, fc, i) {
93                 char *fa = conf.filter_arg[i];
94                 fn = fc->filter_nodes + i;
95                 ret = check_filter_arg(fa, &fn->conf);
96                 if (ret < 0)
97                         goto err;
98                 fn->filter_num = ret;
99                 fn->fc = fc;
100                 INIT_LIST_HEAD(&fn->callbacks);
101                 PARA_DEBUG_LOG("filter #%d: %s\n", i, filters[fn->filter_num].name);
102         }
103         open_filters();
104         return 1;
105 err:
106         free_filter_confs();
107         free(fc->filter_nodes);
108         return ret;
109 }
110
111 __noreturn static void print_help_and_die(void)
112 {
113         int d = conf.detailed_help_given;
114         const char **p = d? filter_args_info_detailed_help
115                 : filter_args_info_help;
116
117         printf_or_die("%s\n\n", FILTER_CMDLINE_PARSER_PACKAGE "-"
118                 FILTER_CMDLINE_PARSER_VERSION);
119         printf_or_die("%s\n\n", filter_args_info_usage);
120         for (; *p; p++)
121                 printf_or_die("%s\n", *p);
122         print_filter_helps(d);
123         exit(0);
124 }
125
126 static int parse_config(int argc, char *argv[])
127 {
128         static char *cf; /* config file */
129         struct stat statbuf;
130
131         if (filter_cmdline_parser(argc, argv, &conf))
132                 return -E_FILTER_SYNTAX;
133         HANDLE_VERSION_FLAG("filter", conf);
134         if (conf.help_given || conf.detailed_help_given)
135                 print_help_and_die();
136         loglevel = get_loglevel_by_name(conf.loglevel_arg);
137         if (!cf) {
138                 char *home = para_homedir();
139                 cf = make_message("%s/.paraslash/filter.conf", home);
140                 free(home);
141         }
142         if (!stat(cf, &statbuf)) {
143                 struct filter_cmdline_parser_params params = {
144                         .override = 0,
145                         .initialize = 0,
146                         .check_required = 0,
147                         .check_ambiguity = 0,
148                         .print_errors = 1
149                 };
150                 if (filter_cmdline_parser_config_file(cf, &conf, &params))
151                         return -E_FILTER_SYNTAX;
152         }
153         return 1;
154 }
155
156 /* TODO: support more than one filter, actually parse options */
157 static int main_btr(void)
158 {
159         return 42;
160 }
161
162 /**
163  * The main function of para_filter.
164  *
165  * Para_filter reads data from stdin, converts it by using a chain
166  * of filters (specified on the command line) and writes the resulting
167  * data to stdout.
168  *
169  * \param argc Number of command line options.
170  * \param argv Vector of arguments.
171  *
172  * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
173  */
174 int main(int argc, char *argv[])
175 {
176         int ret;
177         static struct sched s;
178
179         filter_init();
180         ret = parse_config(argc, argv);
181         if (ret < 0)
182                 goto out;
183         if (conf.buffer_tree_given) {
184                 ret = main_btr();
185                 goto out;
186         }
187         stdin_set_defaults(sit);
188         sit->buf = para_malloc(sit->bufsize),
189
190         ret = init_filter_chain();
191         if (ret < 0)
192                 goto out;
193         sit->output_error = &fc->task.error;
194
195         stdout_set_defaults(sot);
196         sot->bufp = fc->outbufp;
197         sot->loaded = fc->out_loaded;
198         sot->input_error = &fc->task.error;
199
200         register_task(&sit->task);
201         register_task(&sot->task);
202         register_task(&fc->task);
203         s.default_timeout.tv_sec = 1;
204         s.default_timeout.tv_usec = 0;
205         ret = schedule(&s);
206         free_filter_confs();
207         close_filters(fc);
208 out:
209         free(sit->buf);
210         if (ret < 0)
211                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
212         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
213 }