79ae4dcbd22ef01e9ae44cdb90d02c9d6c5c570e
[paraslash.git] / filter.c
1 /*
2  * Copyright (C) 2005-2006 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_FILTER_ERRLISTS;
32
33 #define INBUF_SIZE 32 * 1024
34
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;
41
42 struct gengetopt_args_info conf;
43
44 __printf_2_3 void para_log(int ll, const char* fmt,...)
45 {
46         va_list argp;
47
48         /* ignore log message if loglevel is not high enough */
49         if (ll < conf.loglevel_arg)
50                 return;
51         va_start(argp, fmt);
52         vfprintf(stderr, fmt, argp);
53         va_end(argp);
54 }
55
56 void filter_event_handler(struct task *t)
57 {
58         PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
59         unregister_task(t);
60 }
61
62 static void open_filters(void)
63 {
64         struct filter_node *fn;
65
66         list_for_each_entry(fn, &fc->filters, node) {
67                 fn->filter->open(fn);
68                 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
69                 fc->outbuf = fn->buf;
70                 fc->out_loaded = &fn->loaded;
71         }
72 }
73
74
75 static int init_filter_chain(void)
76 {
77         int i, filter_num;
78         struct filter_node *fn;
79
80         INIT_LIST_HEAD(&fc->filters);
81
82         fc->inbuf = sit->buf;
83         fc->in_loaded = &sit->loaded;
84         fc->reader_eof = &sit->eof;
85
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);
90                 if (filter_num < 0) {
91                         free(fn);
92                         return filter_num;
93                 }
94                 fn->fc = fc;
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);
99         }
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");
106         open_filters();
107         return 1;
108 }
109
110 static int parse_config(int argc, char *argv[])
111 {
112         static char *cf; /* config file */
113         struct stat statbuf;
114         int i;
115
116         if (cmdline_parser(argc, argv, &conf))
117                 return -E_FILTER_SYNTAX;
118         if (!cf) {
119                 char *home = para_homedir();
120                 cf = make_message("%s/.paraslash/filter.conf", home);
121                 free(home);
122         }
123         if (!stat(cf, &statbuf)) {
124                 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0))
125                         return -E_FILTER_SYNTAX;
126         }
127         if (!conf.list_filters_given)
128                 return 1;
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");
135         exit(EXIT_SUCCESS);
136 }
137
138 int main(int argc, char *argv[])
139 {
140         int ret;
141         struct sched s;
142
143         init_sched();
144         stdin_set_defaults(sit);
145         sit->buf = para_malloc(sit->bufsize),
146
147         filter_init(filters);
148         ret = parse_config(argc, argv);
149         if (ret < 0)
150                 goto out;
151         ret = init_filter_chain();
152         if (ret < 0)
153                 goto out;
154
155         stdout_set_defaults(sot);
156         sot->buf = fc->outbuf;
157         sot->loaded = fc->out_loaded;
158         sot->eof = &fc->eof;
159
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;
165         ret = sched(&s);
166 out:
167         free(sit->buf);
168         if (ret < 0)
169                 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
170         close_filters(fc);
171         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
172 }