osx_write.c: trivial simplifications
[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 static struct stdin_task stdin_task_struct;
34 static struct stdin_task *sit = &stdin_task_struct;
35 static struct filter_chain filter_chain_struct;
36 static struct filter_chain *fc = &filter_chain_struct;
37 static struct stdout_task stdout_task_struct;
38 static struct stdout_task *sot = &stdout_task_struct;
39
40 struct filter_args_info conf;
41
42 __printf_2_3 void para_log(int ll, const char* fmt,...)
43 {
44         va_list argp;
45
46         /* ignore log message if loglevel is not high enough */
47         if (ll < conf.loglevel_arg)
48                 return;
49         va_start(argp, fmt);
50         vfprintf(stderr, fmt, argp);
51         va_end(argp);
52 }
53
54 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
73 static int init_filter_chain(void)
74 {
75         int i, filter_num;
76         struct filter_node *fn;
77
78         INIT_LIST_HEAD(&fc->filters);
79
80         fc->inbuf = sit->buf;
81         fc->in_loaded = &sit->loaded;
82         fc->input_eof = &sit->eof;
83         fc->eof = 0;
84         fc->output_eof = &sot->eof;
85         fc->task.private_data = fc;
86         fc->task.pre_select = filter_pre_select;
87         fc->task.event_handler = filter_event_handler;
88         sprintf(fc->task.status, "filter chain");
89
90         for (i = 0; i < conf.filter_given; i++) {
91                 char *fa = conf.filter_arg[i];
92                 fn = para_calloc(sizeof(struct filter_node));
93                 filter_num = check_filter_arg(fa, &fn->conf);
94                 if (filter_num < 0) {
95                         free(fn);
96                         return filter_num;
97                 }
98                 fn->fc = fc;
99                 INIT_LIST_HEAD(&fn->callbacks);
100                 fn->filter = &filters[filter_num];
101                 PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name);
102                 list_add_tail(&fn->node, &fc->filters);
103         }
104         if (list_empty(&fc->filters))
105                 return -E_NO_FILTERS;
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 (filter_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 (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 int main(int argc, char *argv[])
139 {
140         int ret;
141         struct sched s;
142
143         stdin_set_defaults(sit);
144         sit->buf = para_malloc(sit->bufsize),
145
146         filter_init(filters);
147         ret = parse_config(argc, argv);
148         if (ret < 0)
149                 goto out;
150         ret = init_filter_chain();
151         if (ret < 0)
152                 goto out;
153
154         stdout_set_defaults(sot);
155         sot->buf = fc->outbuf;
156         sot->loaded = fc->out_loaded;
157         sot->input_eof = &fc->eof;
158
159         register_task(&sot->task);
160         register_task(&fc->task);
161         register_task(&sit->task);
162         s.default_timeout.tv_sec = 1;
163         s.default_timeout.tv_usec = 0;
164         ret = sched(&s);
165 out:
166         free(sit->buf);
167         close_filters(fc);
168         if (ret < 0)
169                 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
170         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
171 }