2 * Copyright (C) 2005-2007 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 filter_event_handler(struct task *t)
44 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
48 static void open_filters(void)
50 struct filter_node *fn;
52 list_for_each_entry(fn, &fc->filters, node) {
54 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
56 fc->out_loaded = &fn->loaded;
60 static int init_filter_chain(void)
63 struct filter_node *fn;
65 INIT_LIST_HEAD(&fc->filters);
68 fc->in_loaded = &sit->loaded;
69 fc->input_eof = &sit->eof;
71 fc->output_eof = &sot->eof;
72 fc->task.private_data = fc;
73 fc->task.pre_select = filter_pre_select;
74 fc->task.event_handler = filter_event_handler;
75 sprintf(fc->task.status, "filter chain");
77 for (i = 0; i < conf.filter_given; i++) {
78 char *fa = conf.filter_arg[i];
79 fn = para_calloc(sizeof(struct filter_node));
80 filter_num = check_filter_arg(fa, &fn->conf);
86 INIT_LIST_HEAD(&fn->callbacks);
87 fn->filter = &filters[filter_num];
88 PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name);
89 list_add_tail(&fn->node, &fc->filters);
91 if (list_empty(&fc->filters))
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 * para_filter's main function.
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[])
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();
160 stdout_set_defaults(sot);
161 sot->buf = fc->outbuf;
162 sot->loaded = fc->out_loaded;
163 sot->input_eof = &fc->eof;
165 register_task(&sit->task);
166 register_task(&fc->task);
167 register_task(&sot->task);
168 s.default_timeout.tv_sec = 1;
169 s.default_timeout.tv_usec = 0;
175 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
176 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;