-/*
- * Copyright (C) 2005-2006 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.
- */
-/** \file filter.c the stand-alone filter program */
+/* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
-#include "para.h"
+/** \file filter.c The stand-alone filter program. */
-#include "filter.cmdline.h"
+#include <regex.h>
+#include <lopsub.h>
+
+#include "filter.lsg.h"
+#include "filter_cmd.lsg.h"
+#include "para.h"
#include "list.h"
+#include "lsu.h"
+#include "sched.h"
+#include "buffer_tree.h"
#include "filter.h"
-#include "error.h"
#include "string.h"
+#include "stdin.h"
+#include "stdout.h"
+#include "error.h"
+#include "fd.h"
+#include "version.h"
-INIT_FILTER_ERRLISTS;
-
-#define INBUF_SIZE 32 * 1024
+/** Array of error strings. */
+DEFINE_PARA_ERRLIST;
+static struct lls_parse_result *lpr; /* command line options */
-static struct filter_chain_info filter_chain_info_struct;
-static struct filter_chain_info *fci = &filter_chain_info_struct;
+#define CMD_PTR (lls_cmd(0, filter_suite))
+#define OPT_RESULT(_name) \
+ (lls_opt_result(LSG_FILTER_PARA_FILTER_OPT_ ## _name, lpr))
+#define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
+#define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
-struct gengetopt_args_info conf;
+/** The list of all status items used by para_{server,audiod,gui}. */
+const char *status_item_list[] = {STATUS_ITEMS};
-__printf_2_3 void para_log(int ll, char* fmt,...)
-{
- va_list argp;
+/**
+ * Dummy version which only contains NULL pointers.
+ *
+ * This is used by the amp filter which first tries to obtain the amplification
+ * value from an element in this array.
+ */
+char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
- /* 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);
-}
+/** 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 char *inbuf;
-static size_t loaded;
-static int eof;
+/** 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;
-static int init_active_filter_list(void)
-{
- int i, filter_num;
- 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);
- }
- if (list_empty(&fci->filters))
- return -E_NO_FILTERS;
- return 1;
-}
+static int loglevel;
+INIT_STDERR_LOGGING(loglevel);
-static void open_filters(void)
+static void handle_help_flag(void)
{
- struct filter_node *fn;
+ char *help;
- 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 (OPT_GIVEN(DETAILED_HELP))
+ help = lls_long_help(CMD_PTR);
+ else if (OPT_GIVEN(HELP))
+ help = lls_short_help(CMD_PTR);
+ else
+ return;
+ printf("%s\n", help);
+ free(help);
+ print_filter_helps(OPT_GIVEN(DETAILED_HELP));
+ exit(EXIT_SUCCESS);
}
-static int parse_config(int argc, char *argv[])
+static int parse_config(void)
{
- static char *cf; /* config file */
- struct stat statbuf;
- int i;
-
- if (cmdline_parser(argc, argv, &conf))
- return -E_FILTER_SYNTAX;
- 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))
- return -E_FILTER_SYNTAX;
+ int ret;
+
+ version_handle_flag("filter", OPT_GIVEN(VERSION));
+ handle_help_flag();
+ ret = lsu_merge_config_file_options(NULL, "filter.conf",
+ &lpr, CMD_PTR, filter_suite, 0 /* default flags */);
+ if (ret < 0)
+ return ret;
+ loglevel = OPT_UINT32_VAL(LOGLEVEL);
+ if (!OPT_GIVEN(FILTER)) {
+ print_filter_list();
+ exit(EXIT_SUCCESS);
}
- if (!conf.list_filters_given)
- return 1;
- printf("available filters: ");
- for (i = 0; filters[i].name; i++)
- printf("%s%s", i? " " : "", filters[i].name);
- printf("\nTry para_filter -f<filtername>:-h for help on <filtername>\n");
- exit(EXIT_SUCCESS);
+ return 1;
}
+/**
+ * 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 */
-
- filter_init(filters);
- ret = parse_config(argc, argv);
+ static struct sched s;
+ int i, ret;
+ const struct filter *f;
+ struct btr_node *parent;
+ struct filter_node **fns;
+ struct lls_parse_result *filter_lpr; /* per-filter options */
+ char *errctx;
+
+ ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
if (ret < 0)
goto out;
- inbuf = para_malloc(INBUF_SIZE);
- ret = init_active_filter_list();
+ ret = parse_config();
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;
+ goto free_lpr;
+ sit->btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "stdin"));
+ stdin_task_register(sit, &s);
+
+ fns = para_malloc(OPT_GIVEN(FILTER) * sizeof(*fns));
+ for (i = 0, parent = sit->btrn; i < OPT_GIVEN(FILTER); i++) {
+ const char *fa = lls_string_val(i, OPT_RESULT(FILTER));
+ const char *name;
+ struct filter_node *fn;
+ struct task_info ti;
+
+ fn = fns[i] = para_calloc(sizeof(*fn));
+ fn->filter_num = filter_setup(fa, &fn->conf, &filter_lpr);
+ name = filter_name(fn->filter_num);
+ fn->lpr = filter_lpr;
+ PARA_DEBUG_LOG("filter #%d: %s\n", i, name);
+ f = filter_get(fn->filter_num);
+ fn->btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = name, .parent = parent,
+ .handler = f->execute, .context = fn));
+ ti.name = name;
+ ti.pre_select = f->pre_select;
+ ti.post_select = f->post_select;
+ ti.context = fn;
+ if (f->open)
+ f->open(fn);
+ fn->task = task_register(&ti, &s);
+ parent = fn->btrn;
}
- ret = filter_io(fci);
- 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);
- }
+ sot->btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "stdout", .parent = parent));
+ stdout_task_register(sot, &s);
+
+ s.default_timeout.tv_sec = 1;
+ s.default_timeout.tv_usec = 0;
+ btr_log_tree(sit->btrn, LL_INFO);
+ ret = schedule(&s);
+ sched_shutdown(&s);
+ for (i--; i >= 0; i--) {
+ struct filter_node *fn = fns[i];
+
+ f = filter_get(fn->filter_num);
+ if (f->close)
+ f->close(fn);
+ btr_remove_node(&fn->btrn);
+ if (f->teardown)
+ f->teardown(fn->lpr, fn->conf);
+ free(fn);
}
- if (!eof || converted)
- goto again;
- ret = 0;
+ free(fns);
+ btr_remove_node(&sit->btrn);
+ btr_remove_node(&sot->btrn);
+free_lpr:
+ lls_free_parse_result(lpr, CMD_PTR);
out:
- if (ret < 0)
- PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
- close_filters(fci);
- return ret;
+ if (ret < 0) {
+ if (errctx)
+ PARA_ERROR_LOG("%s\n", errctx);
+ free(errctx);
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ }
+ exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
}