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 */
22 #include "filter.cmdline.h"
30 #define INBUF_SIZE 32 * 1024
32 static struct filter_chain filter_chain_struct
;
33 static struct filter_chain
*fc
= &filter_chain_struct
;
35 struct gengetopt_args_info conf
;
37 __printf_2_3
void para_log(int ll
, const char* fmt
,...)
41 /* ignore log message if loglevel is not high enough */
42 if (ll
< conf
.loglevel_arg
)
45 vfprintf(stderr
, fmt
, argp
);
53 static int init_active_filter_list(void)
56 struct filter_node
*fn
;
58 INIT_LIST_HEAD(&fc
->filters
);
61 fc
->in_loaded
= &loaded
;
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
);
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
, &fc
->filters
);
78 if (list_empty(&fc
->filters
))
83 static void open_filters(void)
85 struct filter_node
*fn
;
87 list_for_each_entry(fn
, &fc
->filters
, node
) {
89 PARA_INFO_LOG("opened %s filter\n", fn
->filter
->name
);
91 fc
->out_loaded
= &fn
->loaded
;
95 static int parse_config(int argc
, char *argv
[])
97 static char *cf
; /* config file */
101 if (cmdline_parser(argc
, argv
, &conf
))
102 return -E_FILTER_SYNTAX
;
104 char *home
= para_homedir();
105 cf
= make_message("%s/.paraslash/filter.conf", home
);
108 if (!stat(cf
, &statbuf
)) {
109 if (cmdline_parser_configfile(cf
, &conf
, 0, 0, 0))
110 return -E_FILTER_SYNTAX
;
112 if (!conf
.list_filters_given
)
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");
123 int main(int argc
, char *argv
[])
126 char *ib
, *ob
; /* input/output buffer */
127 size_t *il
, *ol
; /* number of loaded bytes in input/output buffer */
129 filter_init(filters
);
130 ret
= parse_config(argc
, argv
);
133 inbuf
= para_malloc(INBUF_SIZE
);
134 ret
= init_active_filter_list();
142 PARA_DEBUG_LOG("ib %p in, ob: %p\n", ib
, ob
);
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
);
158 ret
= write(STDOUT_FILENO
, ob
, *ol
);
159 PARA_DEBUG_LOG("wrote %d/%zd\n", ret
, *ol
);
164 PARA_NOTICE_LOG("short write: %zd bytes left\n", *ol
);
165 memmove(ob
, ob
+ ret
, *ol
);
168 if (!eof
|| converted
)
173 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));