]> git.tuebingen.mpg.de Git - paraslash.git/blob - filter.c
doxify para_filter
[paraslash.git] / filter.c
1 /*
2  * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18 /** \file filter.c the stand-alone filter program */
19
20 #include "para.h"
21
22 #include "filter.cmdline.h"
23 #include "list.h"
24 #include "sched.h"
25 #include "filter.h"
26 #include "string.h"
27 #include "stdin.h"
28 #include "stdout.h"
29 #include "error.h"
30
31 /** init the array of errors for para_filter */
32 INIT_FILTER_ERRLISTS;
33
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;
38
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;
43
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;
48
49 /** gengetopt struct that holds the command line args */
50 static struct filter_args_info conf;
51
52
53 /**
54  * standard log function that always writes to stderr
55  *
56  * \param ll loglevel. If the loglevel of the current message
57  * is less than \a ll, the message is going to be ignored.
58  *
59  * \param fmt the format string describing the log message.
60  *
61  */
62 __printf_2_3 void para_log(int ll, const char* fmt,...)
63 {
64         va_list argp;
65
66         /* ignore log message if loglevel is not high enough */
67         if (ll < conf.loglevel_arg)
68                 return;
69         va_start(argp, fmt);
70         vfprintf(stderr, fmt, argp);
71         va_end(argp);
72 }
73
74 static void filter_event_handler(struct task *t)
75 {
76         PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
77         unregister_task(t);
78 }
79
80 static void open_filters(void)
81 {
82         struct filter_node *fn;
83
84         list_for_each_entry(fn, &fc->filters, node) {
85                 fn->filter->open(fn);
86                 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
87                 fc->outbuf = fn->buf;
88                 fc->out_loaded = &fn->loaded;
89         }
90 }
91
92 static int init_filter_chain(void)
93 {
94         int i, filter_num;
95         struct filter_node *fn;
96
97         INIT_LIST_HEAD(&fc->filters);
98
99         fc->inbuf = sit->buf;
100         fc->in_loaded = &sit->loaded;
101         fc->input_eof = &sit->eof;
102         fc->eof = 0;
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");
108
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) {
114                         free(fn);
115                         return filter_num;
116                 }
117                 fn->fc = fc;
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);
122         }
123         if (list_empty(&fc->filters))
124                 return -E_NO_FILTERS;
125         open_filters();
126         return 1;
127 }
128
129 static int parse_config(int argc, char *argv[])
130 {
131         static char *cf; /* config file */
132         struct stat statbuf;
133         int i;
134
135         if (filter_cmdline_parser(argc, argv, &conf))
136                 return -E_FILTER_SYNTAX;
137         if (!cf) {
138                 char *home = para_homedir();
139                 cf = make_message("%s/.paraslash/filter.conf", home);
140                 free(home);
141         }
142         if (!stat(cf, &statbuf)) {
143                 if (filter_cmdline_parser_configfile(cf, &conf, 0, 0, 0))
144                         return -E_FILTER_SYNTAX;
145         }
146         if (!conf.list_filters_given)
147                 return 1;
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");
154         exit(EXIT_SUCCESS);
155 }
156
157 /**
158  * para_filter's main function.
159  *
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
162  * data to stdout.
163  *
164  * \param argc number of command line options
165  * \param argv vector of arguments
166  *
167  * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
168  */
169 int main(int argc, char *argv[])
170 {
171         int ret;
172         struct sched s;
173
174         stdin_set_defaults(sit);
175         sit->buf = para_malloc(sit->bufsize),
176
177         filter_init(filters);
178         ret = parse_config(argc, argv);
179         if (ret < 0)
180                 goto out;
181         ret = init_filter_chain();
182         if (ret < 0)
183                 goto out;
184
185         stdout_set_defaults(sot);
186         sot->buf = fc->outbuf;
187         sot->loaded = fc->out_loaded;
188         sot->input_eof = &fc->eof;
189
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;
195         ret = sched(&s);
196 out:
197         free(sit->buf);
198         close_filters(fc);
199         if (ret < 0)
200                 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
201         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
202 }