1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file filter.c The stand-alone filter program. */
8 #include "filter.lsg.h"
9 #include "filter_cmd.lsg.h"
13 #include "buffer_tree.h"
22 /** Array of error strings. */
24 static struct lls_parse_result *lpr; /* command line options */
26 #define CMD_PTR (lls_cmd(0, filter_suite))
27 #define OPT_RESULT(_name) \
28 (lls_opt_result(LSG_FILTER_PARA_FILTER_OPT_ ## _name, lpr))
29 #define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
30 #define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
32 /** The list of all status items used by para_{server,audiod,gui}. */
33 const char *status_item_list[] = {STATUS_ITEMS};
36 * Dummy version which only contains NULL pointers.
38 * This is used by the amp filter which first tries to obtain the amplification
39 * value from an element in this array.
41 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
43 /** The task that reads from stdin. */
44 static struct stdin_task stdin_task_struct;
45 /** pointer to the stdin task. */
46 static struct stdin_task *sit = &stdin_task_struct;
48 /** The task that writes converted data to stdout. */
49 static struct stdout_task stdout_task_struct;
50 /** Pointer to the stdout task. */
51 static struct stdout_task *sot = &stdout_task_struct;
54 INIT_STDERR_LOGGING(loglevel);
56 static void handle_help_flag(void)
60 if (OPT_GIVEN(DETAILED_HELP))
61 help = lls_long_help(CMD_PTR);
62 else if (OPT_GIVEN(HELP))
63 help = lls_short_help(CMD_PTR);
68 print_filter_helps(OPT_GIVEN(DETAILED_HELP));
72 static int parse_config(void)
74 char *home, *cf; /* config file */
78 version_handle_flag("filter", OPT_GIVEN(VERSION));
80 home = para_homedir();
81 cf = make_message("%s/.paraslash/filter.conf", home);
83 if (!stat(cf, &statbuf)) {
87 char **cf_argv, *errctx;
88 struct lls_parse_result *cf_lpr, *merged_lpr;
90 ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
91 if (ret != -E_EMPTY) {
94 ret = lls(lls_convert_config(map, sz, NULL, &cf_argv,
98 PARA_ERROR_LOG("syntax error in %s\n", cf);
102 ret = lls(lls_parse(cf_argc, cf_argv, CMD_PTR, &cf_lpr,
104 lls_free_argv(cf_argv);
106 PARA_ERROR_LOG("parse error in %s\n", cf);
109 ret = lls(lls_merge(lpr, cf_lpr, CMD_PTR, &merged_lpr, &errctx));
110 lls_free_parse_result(cf_lpr, CMD_PTR);
113 lls_free_parse_result(lpr, CMD_PTR);
115 loglevel = OPT_UINT32_VAL(LOGLEVEL);
118 if (!OPT_GIVEN(FILTER)) {
126 * The main function of para_filter.
128 * Para_filter reads data from stdin, converts it by using a chain
129 * of filters (specified on the command line) and writes the resulting
132 * \param argc Number of command line options.
133 * \param argv Vector of arguments.
135 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
137 int main(int argc, char *argv[])
139 static struct sched s;
141 const struct filter *f;
142 struct btr_node *parent;
143 struct filter_node **fns;
144 struct lls_parse_result *filter_lpr; /* per-filter options */
147 ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
150 loglevel = OPT_UINT32_VAL(LOGLEVEL);
151 ret = parse_config();
154 sit->btrn = btr_new_node(&(struct btr_node_description)
155 EMBRACE(.name = "stdin"));
156 stdin_task_register(sit, &s);
158 fns = para_malloc(OPT_GIVEN(FILTER) * sizeof(*fns));
159 for (i = 0, parent = sit->btrn; i < OPT_GIVEN(FILTER); i++) {
160 const char *fa = lls_string_val(i, OPT_RESULT(FILTER));
162 struct filter_node *fn;
165 fn = fns[i] = para_calloc(sizeof(*fn));
166 fn->filter_num = filter_setup(fa, &fn->conf, &filter_lpr);
167 name = filter_name(fn->filter_num);
168 fn->lpr = filter_lpr;
169 PARA_DEBUG_LOG("filter #%d: %s\n", i, name);
170 f = filter_get(fn->filter_num);
171 fn->btrn = btr_new_node(&(struct btr_node_description)
172 EMBRACE(.name = name, .parent = parent,
173 .handler = f->execute, .context = fn));
175 ti.pre_select = f->pre_select;
176 ti.post_select = f->post_select;
180 fn->task = task_register(&ti, &s);
183 sot->btrn = btr_new_node(&(struct btr_node_description)
184 EMBRACE(.name = "stdout", .parent = parent));
185 stdout_task_register(sot, &s);
187 s.default_timeout.tv_sec = 1;
188 s.default_timeout.tv_usec = 0;
189 btr_log_tree(sit->btrn, LL_INFO);
192 for (i--; i >= 0; i--) {
193 struct filter_node *fn = fns[i];
195 f = filter_get(fn->filter_num);
198 btr_remove_node(&fn->btrn);
200 f->teardown(fn->lpr, fn->conf);
204 btr_remove_node(&sit->btrn);
205 btr_remove_node(&sot->btrn);
207 lls_free_parse_result(lpr, CMD_PTR);
211 PARA_ERROR_LOG("%s\n", errctx);
213 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
215 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);