2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file filter.c The stand-alone filter program. */
12 #include "filter.cmdline.h"
16 #include "buffer_tree.h"
24 /** The list of all status items used by para_{server,audiod,gui}. */
25 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
28 * Dummy version which only contains NULL pointers.
30 * This is used by the amp filter which first tries to obtain the amplification
31 * value from an element in this array.
33 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
35 /** Initialize the array of errors for para_filter. */
38 /** The task that reads from stdin. */
39 static struct stdin_task stdin_task_struct;
40 /** pointer to the stdin task. */
41 static struct stdin_task *sit = &stdin_task_struct;
43 /** The task that writes converted data to stdout. */
44 static struct stdout_task stdout_task_struct;
45 /** Pointer to the stdout task. */
46 static struct stdout_task *sot = &stdout_task_struct;
48 /** Gengetopt struct that holds the command line args. */
49 static struct filter_args_info conf;
52 INIT_STDERR_LOGGING(loglevel);
54 __noreturn static void print_help_and_die(void)
56 struct ggo_help h = DEFINE_GGO_HELP(filter);
57 bool d = conf.detailed_help_given;
59 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
60 print_filter_helps(d? GPH_MODULE_FLAGS_DETAILED : GPH_MODULE_FLAGS);
64 static int parse_config(void)
66 static char *cf; /* config file */
69 version_handle_flag("filter", conf.version_given);
70 if (conf.help_given || conf.detailed_help_given)
73 char *home = para_homedir();
74 cf = make_message("%s/.paraslash/filter.conf", home);
77 if (!stat(cf, &statbuf)) {
78 struct filter_cmdline_parser_params params = {
85 filter_cmdline_parser_config_file(cf, &conf, ¶ms);
86 loglevel = get_loglevel_by_name(conf.loglevel_arg);
88 if (!conf.filter_given)
94 * The main function of para_filter.
96 * Para_filter reads data from stdin, converts it by using a chain
97 * of filters (specified on the command line) and writes the resulting
100 * \param argc Number of command line options.
101 * \param argv Vector of arguments.
103 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
105 int main(int argc, char *argv[])
107 static struct sched s;
109 const struct filter *f;
110 struct btr_node *parent;
111 struct filter_node **fns;
113 filter_cmdline_parser(argc, argv, &conf); /* aborts on errors */
114 loglevel = get_loglevel_by_name(conf.loglevel_arg);
116 ret = parse_config();
119 sit->btrn = btr_new_node(&(struct btr_node_description)
120 EMBRACE(.name = "stdin"));
121 stdin_task_register(sit, &s);
123 fns = para_malloc(conf.filter_given * sizeof(*fns));
124 for (i = 0, parent = sit->btrn; i < conf.filter_given; i++) {
125 char *fa = conf.filter_arg[i];
126 struct filter_node *fn;
129 fn = fns[i] = para_calloc(sizeof(*fn));
130 ret = check_filter_arg(fa, &fn->conf);
135 fn->filter_num = ret;
136 f = filter_get(fn->filter_num);
137 PARA_DEBUG_LOG("filter #%d: %s\n", i, f->name);
138 fn->btrn = btr_new_node(&(struct btr_node_description)
139 EMBRACE(.name = f->name, .parent = parent,
140 .handler = f->execute, .context = fn));
142 ti.pre_select = f->pre_select;
143 ti.post_select = f->post_select;
146 fn->task = task_register(&ti, &s);
149 sot->btrn = btr_new_node(&(struct btr_node_description)
150 EMBRACE(.name = "stdout", .parent = parent));
151 stdout_task_register(sot, &s);
153 s.default_timeout.tv_sec = 1;
154 s.default_timeout.tv_usec = 0;
155 btr_log_tree(sit->btrn, LL_INFO);
159 for (i--; i >= 0; i--) {
160 struct filter_node *fn = fns[i];
162 f = filter_get(fn->filter_num);
165 btr_remove_node(&fn->btrn);
167 f->free_config(fn->conf);
171 btr_remove_node(&sit->btrn);
172 btr_remove_node(&sot->btrn);
175 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
176 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);