2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file filter.c the stand-alone filter program */
10 #include "filter.cmdline.h"
19 /** init the array of errors for para_filter */
22 /** the task that reads from stdin */
23 static struct stdin_task stdin_task_struct;
24 /** pointer to the stdin task */
25 static struct stdin_task *sit = &stdin_task_struct;
27 /** the task that filters the data */
28 static struct filter_chain filter_chain_struct;
29 /** pointer to the filter chain */
30 static struct filter_chain *fc = &filter_chain_struct;
32 /** the task that writes converted data to stdout */
33 static struct stdout_task stdout_task_struct;
34 /** pointer to the stdout task */
35 static struct stdout_task *sot = &stdout_task_struct;
37 /** gengetopt struct that holds the command line args */
38 static struct filter_args_info conf;
40 INIT_STDERR_LOGGING(conf.loglevel_arg);
42 static void open_filters(void)
45 struct filter_node *fn;
47 FOR_EACH_FILTER_NODE(fn, fc, i) {
48 struct filter *f = filters + fn->filter_num;
50 PARA_INFO_LOG("opened %s filter\n", f->name);
52 fc->out_loaded = &fn->loaded;
56 static int init_filter_chain(void)
59 struct filter_node *fn;
61 if (!conf.filter_given)
63 fc->num_filters = conf.filter_given;
64 fc->filter_nodes = para_malloc(fc->num_filters * sizeof(struct filter_node));
66 fc->in_loaded = &sit->loaded;
67 fc->input_error = &sit->task.error;
69 fc->output_error = &sot->task.error;
70 fc->task.pre_select = filter_pre_select;
71 sprintf(fc->task.status, "filter chain");
73 FOR_EACH_FILTER_NODE(fn, fc, i) {
74 char *fa = conf.filter_arg[i];
75 fn = fc->filter_nodes + i;
76 ret = check_filter_arg(fa, &fn->conf);
81 INIT_LIST_HEAD(&fn->callbacks);
82 PARA_DEBUG_LOG("filter #%d: %s\n", i, filters[fn->filter_num].name);
87 free(fc->filter_nodes);
91 static int parse_config(int argc, char *argv[])
93 static char *cf; /* config file */
97 if (filter_cmdline_parser(argc, argv, &conf))
98 return -E_FILTER_SYNTAX;
99 HANDLE_VERSION_FLAG("filter", conf);
101 char *home = para_homedir();
102 cf = make_message("%s/.paraslash/filter.conf", home);
105 if (!stat(cf, &statbuf)) {
106 struct filter_cmdline_parser_params params = {
112 if (filter_cmdline_parser_config_file(cf, &conf, ¶ms))
113 return -E_FILTER_SYNTAX;
115 if (!conf.list_filters_given)
117 printf("available filters: ");
118 for (i = 0; filters[i].name; i++)
119 printf("%s%s%s", i? " " : "", filters[i].name,
120 filters[i].parse_config? "*": "");
121 printf("\nFilters marked with \"*\" have further command line options. Try\n"
122 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
127 * para_filter's main function.
129 * para_filter reads data from stdin, converts it by using a chain
130 * of filters (specified on the command line) and writes the resulting
133 * \param argc number of command line options
134 * \param argv vector of arguments
136 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
138 int main(int argc, char *argv[])
141 static struct sched s;
143 stdin_set_defaults(sit);
144 sit->buf = para_malloc(sit->bufsize),
146 filter_init(filters);
147 ret = parse_config(argc, argv);
150 ret = init_filter_chain();
154 stdout_set_defaults(sot);
155 sot->buf = fc->outbuf;
156 sot->loaded = fc->out_loaded;
157 sot->input_error = &fc->task.error;
159 register_task(&sit->task);
160 register_task(&fc->task);
161 register_task(&sot->task);
162 s.default_timeout.tv_sec = 1;
163 s.default_timeout.tv_usec = 0;
169 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
170 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;