change email address from tu-darmstadt to systemlinux.org
[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 INIT_STDERR_LOGGING(conf.loglevel_arg);
53
54 static void filter_event_handler(struct task *t)
55 {
56         PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
57         unregister_task(t);
58 }
59
60 static void open_filters(void)
61 {
62         struct filter_node *fn;
63
64         list_for_each_entry(fn, &fc->filters, node) {
65                 fn->filter->open(fn);
66                 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
67                 fc->outbuf = fn->buf;
68                 fc->out_loaded = &fn->loaded;
69         }
70 }
71
72 static int init_filter_chain(void)
73 {
74         int i, filter_num;
75         struct filter_node *fn;
76
77         INIT_LIST_HEAD(&fc->filters);
78
79         fc->inbuf = sit->buf;
80         fc->in_loaded = &sit->loaded;
81         fc->input_eof = &sit->eof;
82         fc->eof = 0;
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");
88
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);
93                 if (filter_num < 0) {
94                         free(fn);
95                         return filter_num;
96                 }
97                 fn->fc = fc;
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);
102         }
103         if (list_empty(&fc->filters))
104                 return -E_NO_FILTERS;
105         open_filters();
106         return 1;
107 }
108
109 static int parse_config(int argc, char *argv[])
110 {
111         static char *cf; /* config file */
112         struct stat statbuf;
113         int i;
114
115         if (filter_cmdline_parser(argc, argv, &conf))
116                 return -E_FILTER_SYNTAX;
117         if (!cf) {
118                 char *home = para_homedir();
119                 cf = make_message("%s/.paraslash/filter.conf", home);
120                 free(home);
121         }
122         if (!stat(cf, &statbuf)) {
123                 if (filter_cmdline_parser_configfile(cf, &conf, 0, 0, 0))
124                         return -E_FILTER_SYNTAX;
125         }
126         if (!conf.list_filters_given)
127                 return 1;
128         printf("available filters: ");
129         for (i = 0; filters[i].name; i++)
130                 printf("%s%s%s", i? " " : "", filters[i].name,
131                         filters[i].parse_config? "*": "");
132         printf("\nFilters marked with \"*\" have further command line options. Try\n"
133                 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
134         exit(EXIT_SUCCESS);
135 }
136
137 /**
138  * para_filter's main function.
139  *
140  * para_filter reads data from stdin, converts it by using a chain
141  * of filters (specified on the command line) and writes the resulting
142  * data to stdout.
143  *
144  * \param argc number of command line options
145  * \param argv vector of arguments
146  *
147  * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
148  */
149 int main(int argc, char *argv[])
150 {
151         int ret;
152         struct sched s;
153
154         stdin_set_defaults(sit);
155         sit->buf = para_malloc(sit->bufsize),
156
157         filter_init(filters);
158         ret = parse_config(argc, argv);
159         if (ret < 0)
160                 goto out;
161         ret = init_filter_chain();
162         if (ret < 0)
163                 goto out;
164
165         stdout_set_defaults(sot);
166         sot->buf = fc->outbuf;
167         sot->loaded = fc->out_loaded;
168         sot->input_eof = &fc->eof;
169
170         register_task(&sot->task);
171         register_task(&fc->task);
172         register_task(&sit->task);
173         s.default_timeout.tv_sec = 1;
174         s.default_timeout.tv_usec = 0;
175         ret = sched(&s);
176 out:
177         free(sit->buf);
178         close_filters(fc);
179         if (ret < 0)
180                 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
181         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
182 }