2 * Copyright (C) 2005-2007 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"
31 /** init the array of errors for para_filter */
34 /** the task that reads from stdin */
35 static struct stdin_task stdin_task_struct;
36 /** pointer to the stdin task */
37 static struct stdin_task *sit = &stdin_task_struct;
39 /** the task that filters the data */
40 static struct filter_chain filter_chain_struct;
41 /** pointer to the filter chain */
42 static struct filter_chain *fc = &filter_chain_struct;
44 /** the task that writes converted data to stdout */
45 static struct stdout_task stdout_task_struct;
46 /** pointer to the stdout task */
47 static struct stdout_task *sot = &stdout_task_struct;
49 /** gengetopt struct that holds the command line args */
50 static struct filter_args_info conf;
54 * standard log function that always writes to stderr
56 * \param ll loglevel. If the loglevel of the current message
57 * is less than \a ll, the message is going to be ignored.
59 * \param fmt the format string describing the log message.
62 __printf_2_3 void para_log(int ll, const char* fmt,...)
66 /* ignore log message if loglevel is not high enough */
67 if (ll < conf.loglevel_arg)
70 vfprintf(stderr, fmt, argp);
74 static void filter_event_handler(struct task *t)
76 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
80 static void open_filters(void)
82 struct filter_node *fn;
84 list_for_each_entry(fn, &fc->filters, node) {
86 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
88 fc->out_loaded = &fn->loaded;
92 static int init_filter_chain(void)
95 struct filter_node *fn;
97 INIT_LIST_HEAD(&fc->filters);
100 fc->in_loaded = &sit->loaded;
101 fc->input_eof = &sit->eof;
103 fc->output_eof = &sot->eof;
104 fc->task.private_data = fc;
105 fc->task.pre_select = filter_pre_select;
106 fc->task.event_handler = filter_event_handler;
107 sprintf(fc->task.status, "filter chain");
109 for (i = 0; i < conf.filter_given; i++) {
110 char *fa = conf.filter_arg[i];
111 fn = para_calloc(sizeof(struct filter_node));
112 filter_num = check_filter_arg(fa, &fn->conf);
113 if (filter_num < 0) {
118 INIT_LIST_HEAD(&fn->callbacks);
119 fn->filter = &filters[filter_num];
120 PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name);
121 list_add_tail(&fn->node, &fc->filters);
123 if (list_empty(&fc->filters))
124 return -E_NO_FILTERS;
129 static int parse_config(int argc, char *argv[])
131 static char *cf; /* config file */
135 if (filter_cmdline_parser(argc, argv, &conf))
136 return -E_FILTER_SYNTAX;
138 char *home = para_homedir();
139 cf = make_message("%s/.paraslash/filter.conf", home);
142 if (!stat(cf, &statbuf)) {
143 if (filter_cmdline_parser_configfile(cf, &conf, 0, 0, 0))
144 return -E_FILTER_SYNTAX;
146 if (!conf.list_filters_given)
148 printf("available filters: ");
149 for (i = 0; filters[i].name; i++)
150 printf("%s%s%s", i? " " : "", filters[i].name,
151 filters[i].parse_config? "*": "");
152 printf("\nFilters marked with \"*\" have further command line options. Try\n"
153 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
158 * para_filter's main function.
160 * para_filter reads data from stdin, converts it by using a chain
161 * of filters (specified on the command line) and writes the resulting
164 * \param argc number of command line options
165 * \param argv vector of arguments
167 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
169 int main(int argc, char *argv[])
174 stdin_set_defaults(sit);
175 sit->buf = para_malloc(sit->bufsize),
177 filter_init(filters);
178 ret = parse_config(argc, argv);
181 ret = init_filter_chain();
185 stdout_set_defaults(sot);
186 sot->buf = fc->outbuf;
187 sot->loaded = fc->out_loaded;
188 sot->input_eof = &fc->eof;
190 register_task(&sot->task);
191 register_task(&fc->task);
192 register_task(&sit->task);
193 s.default_timeout.tv_sec = 1;
194 s.default_timeout.tv_usec = 0;
200 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
201 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;