X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=filter.c;h=cfa2e94fad98aa255d38bb22dc9906f6fe863c3f;hp=46b48251d757c91360126d927469deab08769e88;hb=b0cad48a864fe3f621138e717ff025060c396fad;hpb=418339759697ff6884e77c809805645c5f4db5b5 diff --git a/filter.c b/filter.c index 46b48251..cfa2e94f 100644 --- a/filter.c +++ b/filter.c @@ -21,20 +21,27 @@ #include "filter.cmdline.h" #include "list.h" +#include "sched.h" #include "filter.h" -#include "error.h" #include "string.h" +#include "stdin.h" +#include "stdout.h" +#include "error.h" INIT_FILTER_ERRLISTS; #define INBUF_SIZE 32 * 1024 -static struct filter_chain_info filter_chain_info_struct; -static struct filter_chain_info *fci = &filter_chain_info_struct; +static struct stdin_task stdin_task_struct; +static struct stdin_task *sit = &stdin_task_struct; +static struct filter_chain filter_chain_struct; +static struct filter_chain *fc = &filter_chain_struct; +static struct stdout_task stdout_task_struct; +static struct stdout_task *sot = &stdout_task_struct; struct gengetopt_args_info conf; -__printf_2_3 void para_log(int ll, char* fmt,...) +__printf_2_3 void para_log(int ll, const char* fmt,...) { va_list argp; @@ -46,52 +53,62 @@ __printf_2_3 void para_log(int ll, char* fmt,...) va_end(argp); } -static char *inbuf; -static size_t loaded; -static int eof; +void filter_event_handler(struct task *t) +{ + PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret)); + unregister_task(t); +} + +static void open_filters(void) +{ + struct filter_node *fn; + + list_for_each_entry(fn, &fc->filters, node) { + fn->filter->open(fn); + PARA_INFO_LOG("opened %s filter\n", fn->filter->name); + fc->outbuf = fn->buf; + fc->out_loaded = &fn->loaded; + } +} + -static int init_active_filter_list(void) +static int init_filter_chain(void) { int i, filter_num; struct filter_node *fn; - INIT_LIST_HEAD(&fci->filters); + INIT_LIST_HEAD(&fc->filters); - fci->inbuf = inbuf; - fci->in_loaded = &loaded; - fci->eof = &eof; + fc->inbuf = sit->buf; + fc->in_loaded = &sit->loaded; + fc->input_eof = &sit->eof; + fc->eof = 0; + fc->output_eof = &sot->eof; + fc->task.private_data = fc; + fc->task.pre_select = filter_pre_select; + fc->task.event_handler = filter_event_handler; + sprintf(fc->task.status, "filter chain"); for (i = 0; i < conf.filter_given; i++) { - char *fa = para_strdup(conf.filter_arg[i]); + char *fa = conf.filter_arg[i]; fn = para_calloc(sizeof(struct filter_node)); filter_num = check_filter_arg(fa, &fn->conf); if (filter_num < 0) { free(fn); return filter_num; } - fn->fci = fci; + fn->fc = fc; INIT_LIST_HEAD(&fn->callbacks); fn->filter = &filters[filter_num]; PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name); - list_add_tail(&fn->node, &fci->filters); + list_add_tail(&fn->node, &fc->filters); } - if (list_empty(&fci->filters)) + if (list_empty(&fc->filters)) return -E_NO_FILTERS; + open_filters(); return 1; } -static void open_filters(void) -{ - struct filter_node *fn; - - list_for_each_entry(fn, &fci->filters, node) { - fn->filter->open(fn); - PARA_INFO_LOG("opened %s filter\n", fn->filter->name); - fci->outbuf = fn->buf; - fci->out_loaded = &fn->loaded; - } -} - static int parse_config(int argc, char *argv[]) { static char *cf; /* config file */ @@ -122,55 +139,38 @@ static int parse_config(int argc, char *argv[]) int main(int argc, char *argv[]) { - int converted, ret; - char *ib, *ob; /* input/output buffer */ - size_t *il, *ol; /* number of loaded bytes in input/output buffer */ + int ret; + struct sched s; + + init_sched(); + stdin_set_defaults(sit); + sit->buf = para_malloc(sit->bufsize), filter_init(filters); ret = parse_config(argc, argv); if (ret < 0) goto out; - inbuf = para_malloc(INBUF_SIZE); - ret = init_active_filter_list(); - if (ret < 0) - goto out; - open_filters(); - ib = fci->inbuf; - ob = fci->outbuf; - il = fci->in_loaded; - ol = fci->out_loaded; - PARA_DEBUG_LOG("ib %p in, ob: %p\n", ib, ob); -again: - if (*il < INBUF_SIZE && !eof) { - ret = read(STDIN_FILENO, ib + *il, INBUF_SIZE - *il); - PARA_DEBUG_LOG("read %d/%zd\n", ret, INBUF_SIZE - *il); - if (ret < 0) - goto out; - if (!ret) - eof = 1; - *il += ret; - } - ret = filter_io(fci); + ret = init_filter_chain(); if (ret < 0) goto out; - converted = ret; - if (*ol) { - ret = write(STDOUT_FILENO, ob, *ol); - PARA_DEBUG_LOG("wrote %d/%zd\n", ret, *ol); - if (ret <= 0) - goto out; - *ol -= ret; - if (*ol) { - PARA_NOTICE_LOG("short write: %zd bytes left\n", *ol); - memmove(ob, ob + ret, *ol); - } - } - if (!eof || converted) - goto again; - ret = 0; + + stdout_set_defaults(sot); + PARA_EMERG_LOG("fc->output_eof: %d\n", *fc->output_eof); + sot->buf = fc->outbuf; + sot->loaded = fc->out_loaded; + sot->input_eof = &fc->eof; + + register_task(&sot->task); + register_task(&fc->task); + register_task(&sit->task); + s.default_timeout.tv_sec = 1; + s.default_timeout.tv_usec = 0; + PARA_EMERG_LOG("fc->output_eof: %d\n", *fc->output_eof); + ret = sched(&s); out: + free(sit->buf); if (ret < 0) PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret)); - close_filters(fci); - return ret; + close_filters(fc); + return ret < 0? EXIT_FAILURE : EXIT_SUCCESS; }