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 /** Initialize the array of errors for para_filter. */
23 /** The task that reads from stdin. */
24 static struct stdin_task stdin_task_struct;
25 /** pointer to the stdin task. */
26 static struct stdin_task *sit = &stdin_task_struct;
28 /** The task that filters the data. */
29 static struct filter_chain filter_chain_struct;
30 /** Pointer to the filter chain. */
31 static struct filter_chain *fc = &filter_chain_struct;
33 /** The task that writes converted data to stdout. */
34 static struct stdout_task stdout_task_struct;
35 /** Pointer to the stdout task. */
36 static struct stdout_task *sot = &stdout_task_struct;
38 /** Gengetopt struct that holds the command line args. */
39 static struct filter_args_info conf;
41 INIT_STDERR_LOGGING(conf.loglevel_arg);
43 static void open_filters(void)
46 struct filter_node *fn;
48 FOR_EACH_FILTER_NODE(fn, fc, i) {
49 struct filter *f = filters + fn->filter_num;
51 PARA_INFO_LOG("opened %s filter\n", f->name);
53 fc->out_loaded = &fn->loaded;
57 static int init_filter_chain(void)
60 struct filter_node *fn;
62 if (!conf.filter_given)
64 fc->num_filters = conf.filter_given;
65 fc->filter_nodes = para_malloc(fc->num_filters * sizeof(struct filter_node));
67 fc->in_loaded = &sit->loaded;
68 fc->input_error = &sit->task.error;
70 fc->output_error = &sot->task.error;
71 fc->task.pre_select = filter_pre_select;
72 sprintf(fc->task.status, "filter chain");
74 FOR_EACH_FILTER_NODE(fn, fc, i) {
75 char *fa = conf.filter_arg[i];
76 fn = fc->filter_nodes + i;
77 ret = check_filter_arg(fa, &fn->conf);
82 INIT_LIST_HEAD(&fn->callbacks);
83 PARA_DEBUG_LOG("filter #%d: %s\n", i, filters[fn->filter_num].name);
88 free(fc->filter_nodes);
92 static int parse_config(int argc, char *argv[])
94 static char *cf; /* config file */
98 if (filter_cmdline_parser(argc, argv, &conf))
99 return -E_FILTER_SYNTAX;
100 HANDLE_VERSION_FLAG("filter", conf);
102 char *home = para_homedir();
103 cf = make_message("%s/.paraslash/filter.conf", home);
106 if (!stat(cf, &statbuf)) {
107 struct filter_cmdline_parser_params params = {
113 if (filter_cmdline_parser_config_file(cf, &conf, ¶ms))
114 return -E_FILTER_SYNTAX;
116 if (!conf.list_filters_given)
118 printf("available filters: ");
119 for (i = 0; filters[i].name; i++)
120 printf("%s%s%s", i? " " : "", filters[i].name,
121 filters[i].parse_config? "*": "");
122 printf("\nFilters marked with \"*\" have further command line options. Try\n"
123 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
128 * The main function of para_filter.
130 * Para_filter reads data from stdin, converts it by using a chain
131 * of filters (specified on the command line) and writes the resulting
134 * \param argc Number of command line options.
135 * \param argv Vector of arguments.
137 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
139 int main(int argc, char *argv[])
142 static struct sched s;
144 stdin_set_defaults(sit);
145 sit->buf = para_malloc(sit->bufsize),
147 filter_init(filters);
148 ret = parse_config(argc, argv);
151 ret = init_filter_chain();
155 stdout_set_defaults(sot);
156 sot->buf = fc->outbuf;
157 sot->loaded = fc->out_loaded;
158 sot->input_error = &fc->task.error;
160 register_task(&sit->task);
161 register_task(&fc->task);
162 register_task(&sot->task);
163 s.default_timeout.tv_sec = 1;
164 s.default_timeout.tv_usec = 0;
170 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
171 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;