aac_afh.c: Kill global variable af
[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         HANDLE_VERSION_FLAG("filter", conf);
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 (filter_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 /**
139  * para_filter's main function.
140  *
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
143  * data to stdout.
144  *
145  * \param argc number of command line options
146  * \param argv vector of arguments
147  *
148  * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
149  */
150 int main(int argc, char *argv[])
151 {
152         int ret;
153         struct sched s;
154
155         stdin_set_defaults(sit);
156         sit->buf = para_malloc(sit->bufsize),
157
158         filter_init(filters);
159         ret = parse_config(argc, argv);
160         if (ret < 0)
161                 goto out;
162         ret = init_filter_chain();
163         if (ret < 0)
164                 goto out;
165
166         stdout_set_defaults(sot);
167         sot->buf = fc->outbuf;
168         sot->loaded = fc->out_loaded;
169         sot->input_eof = &fc->eof;
170
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;
176         ret = sched(&s);
177 out:
178         free(sit->buf);
179         close_filters(fc);
180         if (ret < 0)
181                 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
182         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
183 }