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