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_NOTICE_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->input_eof = &sit->eof;
86 fc->output_eof = &sot->eof;
87 fc->task.private_data = fc;
88 fc->task.pre_select = filter_pre_select;
89 fc->task.event_handler = filter_event_handler;
90 sprintf(fc->task.status, "filter chain");
92 for (i = 0; i < conf.filter_given; i++) {
93 char *fa = conf.filter_arg[i];
94 fn = para_calloc(sizeof(struct filter_node));
95 filter_num = check_filter_arg(fa, &fn->conf);
101 INIT_LIST_HEAD(&fn->callbacks);
102 fn->filter = &filters[filter_num];
103 PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name);
104 list_add_tail(&fn->node, &fc->filters);
106 if (list_empty(&fc->filters))
107 return -E_NO_FILTERS;
112 static int parse_config(int argc, char *argv[])
114 static char *cf; /* config file */
118 if (cmdline_parser(argc, argv, &conf))
119 return -E_FILTER_SYNTAX;
121 char *home = para_homedir();
122 cf = make_message("%s/.paraslash/filter.conf", home);
125 if (!stat(cf, &statbuf)) {
126 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0))
127 return -E_FILTER_SYNTAX;
129 if (!conf.list_filters_given)
131 printf("available filters: ");
132 for (i = 0; filters[i].name; i++)
133 printf("%s%s%s", i? " " : "", filters[i].name,
134 filters[i].parse_config? "*": "");
135 printf("\nFilters marked with \"*\" have further command line options. Try\n"
136 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
140 int main(int argc, char *argv[])
146 stdin_set_defaults(sit);
147 sit->buf = para_malloc(sit->bufsize),
149 filter_init(filters);
150 ret = parse_config(argc, argv);
153 ret = init_filter_chain();
157 stdout_set_defaults(sot);
158 PARA_EMERG_LOG("fc->output_eof: %d\n", *fc->output_eof);
159 sot->buf = fc->outbuf;
160 sot->loaded = fc->out_loaded;
161 sot->input_eof = &fc->eof;
163 register_task(&sot->task);
164 register_task(&fc->task);
165 register_task(&sit->task);
166 s.default_timeout.tv_sec = 1;
167 s.default_timeout.tv_usec = 0;
168 PARA_EMERG_LOG("fc->output_eof: %d\n", *fc->output_eof);
174 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
175 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;