2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
18 /** \file filter.c the stand-alone filter program */
22 #include "filter.cmdline.h"
33 #define INBUF_SIZE 32 * 1024
35 static struct stdin_task stdin_task_struct;
36 static struct stdin_task *sit = &stdin_task_struct;
37 static struct filter_chain filter_chain_struct;
38 static struct filter_chain *fc = &filter_chain_struct;
39 static struct stdout_task stdout_task_struct;
40 static struct stdout_task *sot = &stdout_task_struct;
42 struct gengetopt_args_info conf;
44 __printf_2_3 void para_log(int ll, const char* fmt,...)
48 /* ignore log message if loglevel is not high enough */
49 if (ll < conf.loglevel_arg)
52 vfprintf(stderr, fmt, argp);
56 void filter_event_handler(struct task *t)
58 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
62 static void open_filters(void)
64 struct filter_node *fn;
66 list_for_each_entry(fn, &fc->filters, node) {
68 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
70 fc->out_loaded = &fn->loaded;
75 static int init_filter_chain(void)
78 struct filter_node *fn;
80 INIT_LIST_HEAD(&fc->filters);
83 fc->in_loaded = &sit->loaded;
84 fc->reader_eof = &sit->eof;
86 for (i = 0; i < conf.filter_given; i++) {
87 char *fa = conf.filter_arg[i];
88 fn = para_calloc(sizeof(struct filter_node));
89 filter_num = check_filter_arg(fa, &fn->conf);
95 INIT_LIST_HEAD(&fn->callbacks);
96 fn->filter = &filters[filter_num];
97 PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name);
98 list_add_tail(&fn->node, &fc->filters);
100 if (list_empty(&fc->filters))
101 return -E_NO_FILTERS;
102 fc->task.private_data = fc;
103 fc->task.pre_select = filter_pre_select;
104 fc->task.event_handler = filter_event_handler;
105 sprintf(fc->task.status, "filter chain");
110 static int parse_config(int argc, char *argv[])
112 static char *cf; /* config file */
116 if (cmdline_parser(argc, argv, &conf))
117 return -E_FILTER_SYNTAX;
119 char *home = para_homedir();
120 cf = make_message("%s/.paraslash/filter.conf", home);
123 if (!stat(cf, &statbuf)) {
124 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0))
125 return -E_FILTER_SYNTAX;
127 if (!conf.list_filters_given)
129 printf("available filters: ");
130 for (i = 0; filters[i].name; i++)
131 printf("%s%s%s", i? " " : "", filters[i].name,
132 filters[i].parse_config? "*": "");
133 printf("\nFilters marked with \"*\" have further command line options. Try\n"
134 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
138 int main(int argc, char *argv[])
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;
160 register_task(&sot->task);
161 register_task(&fc->task);
162 register_task(&sit->task);
163 s.default_timeout.tv_sec = 1;
164 s.default_timeout.tv_usec = 0;
169 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
171 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;