replace link to the changelog by link to gitweb
[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 "gcc-compat.h"
21 #include "para.h"
22
23 #include "filter.cmdline.h"
24 #include "list.h"
25 #include "filter.h"
26 #include "error.h"
27 #include "string.h"
28
29 INIT_FILTER_ERRLISTS;
30
31 #define INBUF_SIZE 32 * 1024
32
33 static struct filter_chain_info filter_chain_info_struct;
34 static struct filter_chain_info *fci = &filter_chain_info_struct;
35
36 struct gengetopt_args_info conf;
37
38 __printf_2_3 void para_log(int ll, char* fmt,...)
39 {
40         va_list argp;
41
42         /* ignore log message if loglevel is not high enough */
43         if (ll < conf.loglevel_arg)
44                 return;
45         va_start(argp, fmt);
46         vfprintf(stderr, fmt, argp);
47         va_end(argp);
48 }
49
50 static char *inbuf;
51 static size_t loaded;
52 static int eof;
53
54 static int init_active_filter_list(void)
55 {
56         int i, filter_num;
57         struct filter_node *fn;
58
59         INIT_LIST_HEAD(&fci->filters);
60
61         fci->inbuf = inbuf;
62         fci->in_loaded = &loaded;
63         fci->eof = &eof;
64
65         for (i = 0; i < conf.filter_given; i++) {
66                 char *fa = para_strdup(conf.filter_arg[i]);
67                 fn = para_calloc(sizeof(struct filter_node));
68                 filter_num = check_filter_arg(fa, &fn->conf);
69                 if (filter_num < 0) {
70                         free(fn);
71                         return filter_num;
72                 }
73                 fn->fci = fci;
74                 INIT_LIST_HEAD(&fn->callbacks);
75                 fn->filter = &filters[filter_num];
76                 PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name);
77                 list_add_tail(&fn->node, &fci->filters);
78         }
79         if (list_empty(&fci->filters))
80                 return -E_NO_FILTERS;
81         return 1;
82 }
83
84 static void open_filters(void)
85 {
86         struct filter_node *fn;
87
88         list_for_each_entry(fn, &fci->filters, node) {
89                 fn->filter->open(fn);
90                 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
91                 fci->outbuf = fn->buf;
92                 fci->out_loaded = &fn->loaded;
93         }
94 }
95
96 static int parse_config(int argc, char *argv[])
97 {
98         static char *cf; /* config file */
99         struct stat statbuf;
100         int i;
101
102         if (cmdline_parser(argc, argv, &conf))
103                 return -E_FILTER_SYNTAX;
104         if (!cf) {
105                 char *home = para_homedir();
106                 cf = make_message("%s/.paraslash/filter.conf", home);
107                 free(home);
108         }
109         if (!stat(cf, &statbuf)) {
110                 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0))
111                         return -E_FILTER_SYNTAX;
112         }
113         if (!conf.list_filters_given)
114                 return 1;
115         printf("available filters: ");
116         for (i = 0; filters[i].name; i++)
117                 printf("%s%s", i? " " : "", filters[i].name);
118         printf("\nTry para_filter -f<filtername>:-h for help on <filtername>\n");
119         exit(EXIT_SUCCESS);
120 }
121
122 int main(int argc, char *argv[])
123 {
124         int converted, ret;
125         char *ib, *ob; /* input/output buffer */
126         size_t *il, *ol; /* number of loaded bytes in input/output buffer */
127
128         filter_init(filters);
129         ret = parse_config(argc, argv);
130         if (ret < 0)
131                 goto out;
132         inbuf = para_malloc(INBUF_SIZE);
133         ret = init_active_filter_list();
134         if (ret < 0)
135                 goto out;
136         open_filters();
137         ib = fci->inbuf;
138         ob = fci->outbuf;
139         il = fci->in_loaded;
140         ol = fci->out_loaded;
141         PARA_DEBUG_LOG("ib %p in, ob: %p\n", ib, ob);
142 again:
143         if (*il < INBUF_SIZE && !eof) {
144                 ret  = read(STDIN_FILENO, ib + *il, INBUF_SIZE - *il);
145                 PARA_DEBUG_LOG("read %d/%d\n", ret, INBUF_SIZE - *il);
146                 if (ret < 0)
147                         goto out;
148                 if (!ret)
149                         eof = 1;
150                 *il += ret;
151         }
152         ret = filter_io(fci);
153         if (ret < 0)
154                 goto out;
155         converted = ret;
156         if (*ol) {
157                 ret = write(STDOUT_FILENO, ob, *ol);
158                 PARA_DEBUG_LOG("wrote %d/%d\n", ret, *ol);
159                 if (ret <= 0)
160                         goto out;
161                 *ol -= ret;
162                 if (*ol) {
163                         PARA_NOTICE_LOG("short write: %d bytes left\n", *ol);
164                         memmove(ob, ob + ret, *ol);
165                 }
166         }
167         if (!eof || converted)
168                 goto again;
169         ret = 0;
170 out:
171         if (ret < 0)
172                 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
173         close_filters(fci);
174         return ret;
175 }