2 * Copyright (C) 2005-2008 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. */
11 #include "filter.cmdline.h"
20 /** The list of all status items used by para_{server,audiod,gui}. */
21 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
23 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
25 /** Initialize the array of errors for para_filter. */
28 /** The task that reads from stdin. */
29 static struct stdin_task stdin_task_struct;
30 /** pointer to the stdin task. */
31 static struct stdin_task *sit = &stdin_task_struct;
33 /** The task that filters the data. */
34 static struct filter_chain filter_chain_struct;
35 /** Pointer to the filter chain. */
36 static struct filter_chain *fc = &filter_chain_struct;
38 /** The task that writes converted data to stdout. */
39 static struct stdout_task stdout_task_struct;
40 /** Pointer to the stdout task. */
41 static struct stdout_task *sot = &stdout_task_struct;
43 /** Gengetopt struct that holds the command line args. */
44 static struct filter_args_info conf;
46 INIT_STDERR_LOGGING(conf.loglevel_arg);
48 static void open_filters(void)
51 struct filter_node *fn;
53 FOR_EACH_FILTER_NODE(fn, fc, i) {
54 struct filter *f = filters + fn->filter_num;
56 PARA_INFO_LOG("opened %s filter\n", f->name);
58 fc->out_loaded = &fn->loaded;
62 static int init_filter_chain(void)
65 struct filter_node *fn;
67 if (!conf.filter_given)
69 fc->num_filters = conf.filter_given;
70 fc->filter_nodes = para_malloc(fc->num_filters * sizeof(struct filter_node));
72 fc->in_loaded = &sit->loaded;
73 fc->input_error = &sit->task.error;
75 fc->output_error = &sot->task.error;
76 fc->task.pre_select = filter_pre_select;
77 sprintf(fc->task.status, "filter chain");
79 FOR_EACH_FILTER_NODE(fn, fc, i) {
80 char *fa = conf.filter_arg[i];
81 fn = fc->filter_nodes + i;
82 ret = check_filter_arg(fa, &fn->conf);
87 INIT_LIST_HEAD(&fn->callbacks);
88 PARA_DEBUG_LOG("filter #%d: %s\n", i, filters[fn->filter_num].name);
93 free(fc->filter_nodes);
97 static int parse_config(int argc, char *argv[])
99 static char *cf; /* config file */
103 if (filter_cmdline_parser(argc, argv, &conf))
104 return -E_FILTER_SYNTAX;
105 HANDLE_VERSION_FLAG("filter", conf);
107 char *home = para_homedir();
108 cf = make_message("%s/.paraslash/filter.conf", home);
111 if (!stat(cf, &statbuf)) {
112 struct filter_cmdline_parser_params params = {
118 if (filter_cmdline_parser_config_file(cf, &conf, ¶ms))
119 return -E_FILTER_SYNTAX;
121 if (!conf.list_filters_given)
123 printf("available filters: ");
124 for (i = 0; filters[i].name; i++)
125 printf("%s%s%s", i? " " : "", filters[i].name,
126 filters[i].parse_config? "*": "");
127 printf("\nFilters marked with \"*\" have further command line options. Try\n"
128 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
133 * The main function of para_filter.
135 * Para_filter reads data from stdin, converts it by using a chain
136 * of filters (specified on the command line) and writes the resulting
139 * \param argc Number of command line options.
140 * \param argv Vector of arguments.
142 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
144 int main(int argc, char *argv[])
147 static struct sched s;
149 stdin_set_defaults(sit);
150 sit->buf = para_malloc(sit->bufsize),
152 filter_init(filters);
153 ret = parse_config(argc, argv);
156 ret = init_filter_chain();
159 sit->output_error = &fc->task.error;
161 stdout_set_defaults(sot);
162 sot->buf = fc->outbuf;
163 sot->loaded = fc->out_loaded;
164 sot->input_error = &fc->task.error;
166 register_task(&sit->task);
167 register_task(&fc->task);
168 register_task(&sot->task);
169 s.default_timeout.tv_sec = 1;
170 s.default_timeout.tv_usec = 0;
176 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
177 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;