use %zd instead of %d for size_t
[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", i? " " : "", filters[i].name);
117 printf("\nTry para_filter -f<filtername>:-h for help on <filtername>\n");
118 exit(EXIT_SUCCESS);
119 }
120
121 int main(int argc, char *argv[])
122 {
123 int converted, ret;
124 char *ib, *ob; /* input/output buffer */
125 size_t *il, *ol; /* number of loaded bytes in input/output buffer */
126
127 filter_init(filters);
128 ret = parse_config(argc, argv);
129 if (ret < 0)
130 goto out;
131 inbuf = para_malloc(INBUF_SIZE);
132 ret = init_active_filter_list();
133 if (ret < 0)
134 goto out;
135 open_filters();
136 ib = fci->inbuf;
137 ob = fci->outbuf;
138 il = fci->in_loaded;
139 ol = fci->out_loaded;
140 PARA_DEBUG_LOG("ib %p in, ob: %p\n", ib, ob);
141 again:
142 if (*il < INBUF_SIZE && !eof) {
143 ret = read(STDIN_FILENO, ib + *il, INBUF_SIZE - *il);
144 PARA_DEBUG_LOG("read %d/%zd\n", ret, INBUF_SIZE - *il);
145 if (ret < 0)
146 goto out;
147 if (!ret)
148 eof = 1;
149 *il += ret;
150 }
151 ret = filter_io(fci);
152 if (ret < 0)
153 goto out;
154 converted = ret;
155 if (*ol) {
156 ret = write(STDOUT_FILENO, ob, *ol);
157 PARA_DEBUG_LOG("wrote %d/%zd\n", ret, *ol);
158 if (ret <= 0)
159 goto out;
160 *ol -= ret;
161 if (*ol) {
162 PARA_NOTICE_LOG("short write: %zd bytes left\n", *ol);
163 memmove(ob, ob + ret, *ol);
164 }
165 }
166 if (!eof || converted)
167 goto again;
168 ret = 0;
169 out:
170 if (ret < 0)
171 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
172 close_filters(fci);
173 return ret;
174 }