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. */
12 #include "filter.cmdline.h"
22 /** The list of all status items used by para_{server,audiod,gui}. */
23 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
25 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
27 /** Initialize the array of errors for para_filter. */
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;
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;
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;
45 /** Gengetopt struct that holds the command line args. */
46 static struct filter_args_info conf;
49 INIT_STDERR_LOGGING(loglevel);
51 static void open_filters(void)
54 struct filter_node *fn;
56 FOR_EACH_FILTER_NODE(fn, fc, i) {
57 struct filter *f = filters + fn->filter_num;
59 PARA_INFO_LOG("opened %s filter\n", f->name);
60 fc->outbufp = &fn->buf;
61 fc->out_loaded = &fn->loaded;
65 static void free_filter_confs(void)
68 struct filter_node *fn;
70 FOR_EACH_FILTER_NODE(fn, fc, i)
74 static int init_filter_chain(void)
77 struct filter_node *fn;
79 if (!conf.filter_given)
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;
87 fc->output_error = &sot->task.error;
88 fc->task.post_select = filter_post_select;
89 sprintf(fc->task.status, "filter chain");
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);
99 INIT_LIST_HEAD(&fn->callbacks);
100 PARA_DEBUG_LOG("filter #%d: %s\n", i, filters[fn->filter_num].name);
106 free(fc->filter_nodes);
110 __noreturn static void print_help_and_die(void)
112 int d = conf.detailed_help_given;
113 const char **p = d? filter_args_info_detailed_help
114 : filter_args_info_help;
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);
120 printf_or_die("%s\n", *p);
121 print_filter_helps(d);
125 static int parse_config(int argc, char *argv[])
127 static char *cf; /* config file */
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);
137 char *home = para_homedir();
138 cf = make_message("%s/.paraslash/filter.conf", home);
141 if (!stat(cf, &statbuf)) {
142 struct filter_cmdline_parser_params params = {
146 .check_ambiguity = 0,
149 if (filter_cmdline_parser_config_file(cf, &conf, ¶ms))
150 return -E_FILTER_SYNTAX;
156 * The main function of para_filter.
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
162 * \param argc Number of command line options.
163 * \param argv Vector of arguments.
165 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
167 int main(int argc, char *argv[])
170 static struct sched s;
172 stdin_set_defaults(sit);
173 sit->buf = para_malloc(sit->bufsize),
176 ret = parse_config(argc, argv);
179 ret = init_filter_chain();
182 sit->output_error = &fc->task.error;
184 stdout_set_defaults(sot);
185 sot->bufp = fc->outbufp;
186 sot->loaded = fc->out_loaded;
187 sot->input_error = &fc->task.error;
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;
200 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
201 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;