stdin: Abort if the consumer terminates.
[paraslash.git] / filter.c
index a035c6e..adfadea 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -1,95 +1,97 @@
 /*
- * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
  */
-/** \file filter.c the stand-alone filter program */
+
+/** \file filter.c The stand-alone filter program. */
 
 #include "para.h"
 
 #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"
 
+/** The list of all status items used by para_{server,audiod,gui}. */
+const char *status_item_list[] = {STATUS_ITEM_ARRAY};
+
+char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
+
+/** Initialize the array of errors for para_filter. */
 INIT_FILTER_ERRLISTS;
 
-#define INBUF_SIZE 32 * 1024
+/** The task that reads from stdin. */
+static struct stdin_task stdin_task_struct;
+/** pointer to the stdin task. */
+static struct stdin_task *sit = &stdin_task_struct;
 
-static struct filter_chain_info filter_chain_info_struct;
-static struct filter_chain_info *fci = &filter_chain_info_struct;
+/** The task that filters the data. */
+static struct filter_chain filter_chain_struct;
+/** Pointer to the filter chain. */
+static struct filter_chain *fc = &filter_chain_struct;
 
-struct gengetopt_args_info conf;
+/** The task that writes converted data to stdout. */
+static struct stdout_task stdout_task_struct;
+/** Pointer to the stdout task. */
+static struct stdout_task *sot = &stdout_task_struct;
 
-__printf_2_3 void para_log(int ll, const char* fmt,...)
-{
-       va_list argp;
-
-       /* ignore log message if loglevel is not high enough */
-       if (ll < conf.loglevel_arg)
-               return;
-       va_start(argp, fmt);
-       vfprintf(stderr, fmt, argp);
-       va_end(argp);
-}
+/** Gengetopt struct that holds the command line args. */
+static struct filter_args_info conf;
 
-static char *inbuf;
-static size_t loaded;
-static int eof;
+INIT_STDERR_LOGGING(conf.loglevel_arg);
 
-static int init_active_filter_list(void)
+static void open_filters(void)
 {
-       int i, filter_num;
+       int i;
        struct filter_node *fn;
 
-       INIT_LIST_HEAD(&fci->filters);
-
-       fci->inbuf = inbuf;
-       fci->in_loaded = &loaded;
-       fci->eof = &eof;
-
-       for (i = 0; i < conf.filter_given; i++) {
-               char *fa = para_strdup(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;
-               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);
+       FOR_EACH_FILTER_NODE(fn, fc, i) {
+               struct filter *f = filters + fn->filter_num;
+               f->open(fn);
+               PARA_INFO_LOG("opened %s filter\n", f->name);
+               fc->outbuf = fn->buf;
+               fc->out_loaded = &fn->loaded;
        }
-       if (list_empty(&fci->filters))
-               return -E_NO_FILTERS;
-       return 1;
 }
 
-static void open_filters(void)
+static int init_filter_chain(void)
 {
+       int i, ret;
        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;
+       if (!conf.filter_given)
+               return -E_NO_FILTERS;
+       fc->num_filters = conf.filter_given;
+       fc->filter_nodes = para_malloc(fc->num_filters * sizeof(struct filter_node));
+       fc->inbuf = sit->buf;
+       fc->in_loaded = &sit->loaded;
+       fc->input_error = &sit->task.error;
+       fc->task.error = 0;
+       fc->output_error = &sot->task.error;
+       fc->task.pre_select = filter_pre_select;
+       sprintf(fc->task.status, "filter chain");
+
+       FOR_EACH_FILTER_NODE(fn, fc, i) {
+               char *fa = conf.filter_arg[i];
+               fn = fc->filter_nodes + i;
+               ret = check_filter_arg(fa, &fn->conf);
+               if (ret < 0)
+                       goto err;
+               fn->filter_num = ret;
+               fn->fc = fc;
+               INIT_LIST_HEAD(&fn->callbacks);
+               PARA_DEBUG_LOG("filter #%d: %s\n", i, filters[fn->filter_num].name);
        }
+       open_filters();
+       return 1;
+err:
+       free(fc->filter_nodes);
+       return ret;
 }
 
 static int parse_config(int argc, char *argv[])
@@ -98,15 +100,22 @@ static int parse_config(int argc, char *argv[])
        struct stat statbuf;
        int i;
 
-       if (cmdline_parser(argc, argv, &conf))
+       if (filter_cmdline_parser(argc, argv, &conf))
                return -E_FILTER_SYNTAX;
+       HANDLE_VERSION_FLAG("filter", conf);
        if (!cf) {
                char *home = para_homedir();
                cf = make_message("%s/.paraslash/filter.conf", home);
                free(home);
        }
        if (!stat(cf, &statbuf)) {
-               if (cmdline_parser_configfile(cf, &conf, 0, 0, 0))
+               struct filter_cmdline_parser_params params = {
+                       .override = 0,
+                       .initialize = 0,
+                       .check_required = 0,
+                       .check_ambiguity = 0
+               };
+               if (filter_cmdline_parser_config_file(cf, &conf, &params))
                        return -E_FILTER_SYNTAX;
        }
        if (!conf.list_filters_given)
@@ -120,57 +129,50 @@ static int parse_config(int argc, char *argv[])
        exit(EXIT_SUCCESS);
 }
 
+/**
+ * The main function of para_filter.
+ *
+ * Para_filter reads data from stdin, converts it by using a chain
+ * of filters (specified on the command line) and writes the resulting
+ * data to stdout.
+ *
+ * \param argc Number of command line options.
+ * \param argv Vector of arguments.
+ *
+ * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
+ */
 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;
+       static struct sched s;
+
+       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;
+       sit->output_error = &fc->task.error;
+
+       stdout_set_defaults(sot);
+       sot->buf = fc->outbuf;
+       sot->loaded = fc->out_loaded;
+       sot->input_error = &fc->task.error;
+
+       register_task(&sit->task);
+       register_task(&fc->task);
+       register_task(&sot->task);
+       s.default_timeout.tv_sec = 1;
+       s.default_timeout.tv_usec = 0;
+       ret = schedule(&s);
+       close_filters(fc);
 out:
+       free(sit->buf);
        if (ret < 0)
-               PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
-       close_filters(fci);
-       return ret;
+               PARA_EMERG_LOG("%s\n", para_strerror(-ret));
+       return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
 }