2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
18 /** \file filter.c the stand-alone filter program */
20 #include "gcc-compat.h"
23 #include "filter.cmdline.h"
31 #define INBUF_SIZE 32 * 1024
33 static struct filter_chain_info filter_chain_info_struct
;
34 static struct filter_chain_info
*fci
= &filter_chain_info_struct
;
36 struct gengetopt_args_info conf
;
38 __printf_2_3
void para_log(int ll
, char* fmt
,...)
42 /* ignore log message if loglevel is not high enough */
43 if (ll
< conf
.loglevel_arg
)
46 vfprintf(stderr
, fmt
, argp
);
54 static int init_active_filter_list(void)
57 struct filter_node
*fn
;
59 INIT_LIST_HEAD(&fci
->filters
);
62 fci
->in_loaded
= &loaded
;
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
);
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
);
79 if (list_empty(&fci
->filters
))
84 static void open_filters(void)
86 struct filter_node
*fn
;
88 list_for_each_entry(fn
, &fci
->filters
, node
) {
90 PARA_INFO_LOG("opened %s filter\n", fn
->filter
->name
);
91 fci
->outbuf
= fn
->buf
;
92 fci
->out_loaded
= &fn
->loaded
;
96 static int parse_config(int argc
, char *argv
[])
98 static char *cf
; /* config file */
102 if (cmdline_parser(argc
, argv
, &conf
))
103 return -E_FILTER_SYNTAX
;
105 char *home
= para_homedir();
106 cf
= make_message("%s/.paraslash/filter.conf", home
);
109 if (!stat(cf
, &statbuf
)) {
110 if (cmdline_parser_configfile(cf
, &conf
, 0, 0, 0))
111 return -E_FILTER_SYNTAX
;
113 if (!conf
.list_filters_given
)
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");
122 int main(int argc
, char *argv
[])
125 char *ib
, *ob
; /* input/output buffer */
126 size_t *il
, *ol
; /* number of loaded bytes in input/output buffer */
128 filter_init(filters
);
129 ret
= parse_config(argc
, argv
);
132 inbuf
= para_malloc(INBUF_SIZE
);
133 ret
= init_active_filter_list();
140 ol
= fci
->out_loaded
;
141 PARA_DEBUG_LOG("ib %p in, ob: %p\n", ib
, ob
);
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
);
152 ret
= filter_io(fci
);
157 ret
= write(STDOUT_FILENO
, ob
, *ol
);
158 PARA_DEBUG_LOG("wrote %d/%d\n", ret
, *ol
);
163 PARA_NOTICE_LOG("short write: %d bytes left\n", *ol
);
164 memmove(ob
, ob
+ ret
, *ol
);
167 if (!eof
|| converted
)
172 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));