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