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;
52 INIT_STDERR_LOGGING(conf.loglevel_arg);
54 static void filter_event_handler(struct task *t)
56 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
60 static void open_filters(void)
62 struct filter_node *fn;
64 list_for_each_entry(fn, &fc->filters, node) {
66 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
68 fc->out_loaded = &fn->loaded;
72 static int init_filter_chain(void)
75 struct filter_node *fn;
77 INIT_LIST_HEAD(&fc->filters);
80 fc->in_loaded = &sit->loaded;
81 fc->input_eof = &sit->eof;
83 fc->output_eof = &sot->eof;
84 fc->task.private_data = fc;
85 fc->task.pre_select = filter_pre_select;
86 fc->task.event_handler = filter_event_handler;
87 sprintf(fc->task.status, "filter chain");
89 for (i = 0; i < conf.filter_given; i++) {
90 char *fa = conf.filter_arg[i];
91 fn = para_calloc(sizeof(struct filter_node));
92 filter_num = check_filter_arg(fa, &fn->conf);
98 INIT_LIST_HEAD(&fn->callbacks);
99 fn->filter = &filters[filter_num];
100 PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name);
101 list_add_tail(&fn->node, &fc->filters);
103 if (list_empty(&fc->filters))
104 return -E_NO_FILTERS;
109 static int parse_config(int argc, char *argv[])
111 static char *cf; /* config file */
115 if (filter_cmdline_parser(argc, argv, &conf))
116 return -E_FILTER_SYNTAX;
117 HANDLE_VERSION_FLAG("filter", conf);
119 char *home = para_homedir();
120 cf = make_message("%s/.paraslash/filter.conf", home);
123 if (!stat(cf, &statbuf)) {
124 if (filter_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");
139 * para_filter's main function.
141 * para_filter reads data from stdin, converts it by using a chain
142 * of filters (specified on the command line) and writes the resulting
145 * \param argc number of command line options
146 * \param argv vector of arguments
148 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
150 int main(int argc, char *argv[])
155 stdin_set_defaults(sit);
156 sit->buf = para_malloc(sit->bufsize),
158 filter_init(filters);
159 ret = parse_config(argc, argv);
162 ret = init_filter_chain();
166 stdout_set_defaults(sot);
167 sot->buf = fc->outbuf;
168 sot->loaded = fc->out_loaded;
169 sot->input_eof = &fc->eof;
171 register_task(&sit->task);
172 register_task(&fc->task);
173 register_task(&sot->task);
174 s.default_timeout.tv_sec = 1;
175 s.default_timeout.tv_usec = 0;
181 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
182 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;