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