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. */
13 #include "filter.cmdline.h"
23 /** The list of all status items used by para_{server,audiod,gui}. */
24 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
26 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
28 /** Initialize the array of errors for para_filter. */
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;
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;
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;
46 /** Gengetopt struct that holds the command line args. */
47 static struct filter_args_info conf;
50 INIT_STDERR_LOGGING(loglevel);
52 static void open_filters(void)
55 struct filter_node *fn;
57 FOR_EACH_FILTER_NODE(fn, fc, i) {
58 struct filter *f = filters + fn->filter_num;
60 PARA_INFO_LOG("opened %s filter\n", f->name);
61 fc->outbufp = &fn->buf;
62 fc->out_loaded = &fn->loaded;
66 static void free_filter_confs(void)
69 struct filter_node *fn;
71 FOR_EACH_FILTER_NODE(fn, fc, i)
75 static int init_filter_chain(void)
78 struct filter_node *fn;
80 if (!conf.filter_given)
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;
88 fc->output_error = &sot->task.error;
89 fc->task.post_select = filter_post_select;
90 sprintf(fc->task.status, "filter chain");
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);
100 INIT_LIST_HEAD(&fn->callbacks);
101 PARA_DEBUG_LOG("filter #%d: %s\n", i, filters[fn->filter_num].name);
107 free(fc->filter_nodes);
111 __noreturn static void print_help_and_die(void)
113 int d = conf.detailed_help_given;
114 const char **p = d? filter_args_info_detailed_help
115 : filter_args_info_help;
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);
121 printf_or_die("%s\n", *p);
122 print_filter_helps(d);
126 static int parse_config(int argc, char *argv[])
128 static char *cf; /* config file */
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);
138 char *home = para_homedir();
139 cf = make_message("%s/.paraslash/filter.conf", home);
142 if (!stat(cf, &statbuf)) {
143 struct filter_cmdline_parser_params params = {
147 .check_ambiguity = 0,
150 if (filter_cmdline_parser_config_file(cf, &conf, ¶ms))
151 return -E_FILTER_SYNTAX;
157 * The main function of para_filter.
159 * Para_filter reads data from stdin, converts it by using a chain
160 * of filters (specified on the command line) and writes the resulting
163 * \param argc Number of command line options.
164 * \param argv Vector of arguments.
166 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
168 int main(int argc, char *argv[])
171 static struct sched s;
173 stdin_set_defaults(sit);
174 sit->buf = para_malloc(sit->bufsize),
177 ret = parse_config(argc, argv);
180 ret = init_filter_chain();
183 sit->output_error = &fc->task.error;
185 stdout_set_defaults(sot);
186 sot->bufp = fc->outbufp;
187 sot->loaded = fc->out_loaded;
188 sot->input_error = &fc->task.error;
190 register_task(&sit->task);
191 register_task(&sot->task);
192 register_task(&fc->task);
193 s.default_timeout.tv_sec = 1;
194 s.default_timeout.tv_usec = 0;
201 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
202 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;